Skip to content

Commit ebde545

Browse files
committed
Merge pull request #103 from olivierberten/post4
'post' format 4.0 support
2 parents 487b15f + ec8dccb commit ebde545

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Lib/fontTools/ttLib/tables/_p_o_s_t.py

+41
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def decompile(self, data, ttFont):
3737
self.decode_format_2_0(data, ttFont)
3838
elif self.formatType == 3.0:
3939
self.decode_format_3_0(data, ttFont)
40+
elif self.formatType == 4.0:
41+
self.decode_format_4_0(data, ttFont)
4042
else:
4143
# supported format
4244
raise ttLib.TTLibError("'post' table format %f not supported" % self.formatType)
@@ -49,6 +51,8 @@ def compile(self, ttFont):
4951
data = data + self.encode_format_2_0(ttFont)
5052
elif self.formatType == 3.0:
5153
pass # we're done
54+
elif self.formatType == 4.0:
55+
data = data + self.encode_format_4_0(ttFont)
5256
else:
5357
# supported format
5458
raise ttLib.TTLibError("'post' table format %f not supported" % self.formatType)
@@ -122,6 +126,25 @@ def decode_format_3_0(self, data, ttFont):
122126
# try and construct glyph names from a Unicode cmap table.
123127
self.glyphOrder = None
124128

129+
def decode_format_4_0(self, data, ttFont):
130+
from fontTools import agl
131+
numGlyphs = ttFont['maxp'].numGlyphs
132+
indices = array.array("H")
133+
indices.fromstring(data)
134+
if sys.byteorder != "big":
135+
indices.byteswap()
136+
# In some older fonts, the size of the post table doesn't match
137+
# the number of glyphs. Sometimes it's bigger, sometimes smaller.
138+
self.glyphOrder = glyphOrder = [''] * int(numGlyphs)
139+
for i in range(min(len(indices),numGlyphs)):
140+
if indices[i] == 0xFFFF:
141+
self.glyphOrder[i] = ''
142+
elif indices[i] in agl.UV2AGL:
143+
self.glyphOrder[i] = agl.UV2AGL[indices[i]]
144+
else:
145+
self.glyphOrder[i] = "uni%04X" % indices[i]
146+
self.build_psNameMapping(ttFont)
147+
125148
def encode_format_2_0(self, ttFont):
126149
numGlyphs = ttFont['maxp'].numGlyphs
127150
glyphOrder = ttFont.getGlyphOrder()
@@ -150,6 +173,24 @@ def encode_format_2_0(self, ttFont):
150173
indices.byteswap()
151174
return struct.pack(">H", numGlyphs) + indices.tostring() + packPStrings(extraNames)
152175

176+
def encode_format_4_0(self, ttFont):
177+
from fontTools import agl
178+
numGlyphs = ttFont['maxp'].numGlyphs
179+
glyphOrder = ttFont.getGlyphOrder()
180+
assert len(glyphOrder) == numGlyphs
181+
indices = array.array("H")
182+
for glyphID in glyphOrder:
183+
glyphID = glyphID.split('#')[0]
184+
if glyphID in agl.AGL2UV:
185+
indices.append(agl.AGL2UV[glyphID])
186+
elif len(glyphID) == 7 and glyphID[:3] == 'uni':
187+
indices.append(int(glyphID[3:],16))
188+
else:
189+
indices.append(0xFFFF)
190+
if sys.byteorder != "big":
191+
indices.byteswap()
192+
return indices.tostring()
193+
153194
def toXML(self, writer, ttFont):
154195
formatstring, names, fixes = sstruct.getformat(postFormat)
155196
for name in names:

0 commit comments

Comments
 (0)