@@ -37,6 +37,8 @@ def decompile(self, data, ttFont):
37
37
self .decode_format_2_0 (data , ttFont )
38
38
elif self .formatType == 3.0 :
39
39
self .decode_format_3_0 (data , ttFont )
40
+ elif self .formatType == 4.0 :
41
+ self .decode_format_4_0 (data , ttFont )
40
42
else :
41
43
# supported format
42
44
raise ttLib .TTLibError ("'post' table format %f not supported" % self .formatType )
@@ -49,6 +51,8 @@ def compile(self, ttFont):
49
51
data = data + self .encode_format_2_0 (ttFont )
50
52
elif self .formatType == 3.0 :
51
53
pass # we're done
54
+ elif self .formatType == 4.0 :
55
+ data = data + self .encode_format_4_0 (ttFont )
52
56
else :
53
57
# supported format
54
58
raise ttLib .TTLibError ("'post' table format %f not supported" % self .formatType )
@@ -122,6 +126,25 @@ def decode_format_3_0(self, data, ttFont):
122
126
# try and construct glyph names from a Unicode cmap table.
123
127
self .glyphOrder = None
124
128
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
+
125
148
def encode_format_2_0 (self , ttFont ):
126
149
numGlyphs = ttFont ['maxp' ].numGlyphs
127
150
glyphOrder = ttFont .getGlyphOrder ()
@@ -150,6 +173,24 @@ def encode_format_2_0(self, ttFont):
150
173
indices .byteswap ()
151
174
return struct .pack (">H" , numGlyphs ) + indices .tostring () + packPStrings (extraNames )
152
175
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
+
153
194
def toXML (self , writer , ttFont ):
154
195
formatstring , names , fixes = sstruct .getformat (postFormat )
155
196
for name in names :
0 commit comments