aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@behdad.org>2014-05-15 15:36:03 -0600
committerBehdad Esfahbod <behdad@behdad.org>2014-05-15 15:36:03 -0600
commitebde5454e509ca68fe5647ae9b45fbfdc6480159 (patch)
tree8ba0b5bb0ab1bf6722b9e72b8270220e8ba4d744
parent487b15fd9492fdb3e9d578866e539f726f12b4af (diff)
parentec8dccb7318988591aa65bdf3a70d521837d094b (diff)
downloadfonttools-ebde5454e509ca68fe5647ae9b45fbfdc6480159.tar.gz
Merge pull request #103 from olivierberten/post4
'post' format 4.0 support
-rw-r--r--Lib/fontTools/ttLib/tables/_p_o_s_t.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/fontTools/ttLib/tables/_p_o_s_t.py b/Lib/fontTools/ttLib/tables/_p_o_s_t.py
index ab5a9735..248983fc 100644
--- a/Lib/fontTools/ttLib/tables/_p_o_s_t.py
+++ b/Lib/fontTools/ttLib/tables/_p_o_s_t.py
@@ -37,6 +37,8 @@ class table__p_o_s_t(DefaultTable.DefaultTable):
self.decode_format_2_0(data, ttFont)
elif self.formatType == 3.0:
self.decode_format_3_0(data, ttFont)
+ elif self.formatType == 4.0:
+ self.decode_format_4_0(data, ttFont)
else:
# supported format
raise ttLib.TTLibError("'post' table format %f not supported" % self.formatType)
@@ -49,6 +51,8 @@ class table__p_o_s_t(DefaultTable.DefaultTable):
data = data + self.encode_format_2_0(ttFont)
elif self.formatType == 3.0:
pass # we're done
+ elif self.formatType == 4.0:
+ data = data + self.encode_format_4_0(ttFont)
else:
# supported format
raise ttLib.TTLibError("'post' table format %f not supported" % self.formatType)
@@ -122,6 +126,25 @@ class table__p_o_s_t(DefaultTable.DefaultTable):
# try and construct glyph names from a Unicode cmap table.
self.glyphOrder = None
+ def decode_format_4_0(self, data, ttFont):
+ from fontTools import agl
+ numGlyphs = ttFont['maxp'].numGlyphs
+ indices = array.array("H")
+ indices.fromstring(data)
+ if sys.byteorder != "big":
+ indices.byteswap()
+ # In some older fonts, the size of the post table doesn't match
+ # the number of glyphs. Sometimes it's bigger, sometimes smaller.
+ self.glyphOrder = glyphOrder = [''] * int(numGlyphs)
+ for i in range(min(len(indices),numGlyphs)):
+ if indices[i] == 0xFFFF:
+ self.glyphOrder[i] = ''
+ elif indices[i] in agl.UV2AGL:
+ self.glyphOrder[i] = agl.UV2AGL[indices[i]]
+ else:
+ self.glyphOrder[i] = "uni%04X" % indices[i]
+ self.build_psNameMapping(ttFont)
+
def encode_format_2_0(self, ttFont):
numGlyphs = ttFont['maxp'].numGlyphs
glyphOrder = ttFont.getGlyphOrder()
@@ -150,6 +173,24 @@ class table__p_o_s_t(DefaultTable.DefaultTable):
indices.byteswap()
return struct.pack(">H", numGlyphs) + indices.tostring() + packPStrings(extraNames)
+ def encode_format_4_0(self, ttFont):
+ from fontTools import agl
+ numGlyphs = ttFont['maxp'].numGlyphs
+ glyphOrder = ttFont.getGlyphOrder()
+ assert len(glyphOrder) == numGlyphs
+ indices = array.array("H")
+ for glyphID in glyphOrder:
+ glyphID = glyphID.split('#')[0]
+ if glyphID in agl.AGL2UV:
+ indices.append(agl.AGL2UV[glyphID])
+ elif len(glyphID) == 7 and glyphID[:3] == 'uni':
+ indices.append(int(glyphID[3:],16))
+ else:
+ indices.append(0xFFFF)
+ if sys.byteorder != "big":
+ indices.byteswap()
+ return indices.tostring()
+
def toXML(self, writer, ttFont):
formatstring, names, fixes = sstruct.getformat(postFormat)
for name in names: