12
class table__h_d_m_x(DefaultTable.DefaultTable):
14
def decompile(self, data, ttFont):
15
numGlyphs = ttFont['maxp'].numGlyphs
16
glyphOrder = ttFont.getGlyphOrder()
17
dummy, data = sstruct.unpack2(hdmxHeaderFormat, data, self)
19
for i in range(self.numRecords):
21
maxSize = ord(data[1])
23
for glyphID in range(numGlyphs):
24
widths[glyphOrder[glyphID]] = ord(data[glyphID+2])
25
self.hdmx[ppem] = widths
26
data = data[self.recordSize:]
27
assert len(data) == 0, "too much hdmx data"
29
def compile(self, ttFont):
31
numGlyphs = ttFont['maxp'].numGlyphs
32
glyphOrder = ttFont.getGlyphOrder()
33
self.recordSize = 4 * ((2 + numGlyphs + 3) / 4)
34
pad = (self.recordSize - 2 - numGlyphs) * "\0"
35
self.numRecords = len(self.hdmx)
36
data = sstruct.pack(hdmxHeaderFormat, self)
37
items = self.hdmx.items()
39
for ppem, widths in items:
40
data = data + chr(ppem) + chr(max(widths.values()))
41
for glyphID in range(len(glyphOrder)):
42
width = widths[glyphOrder[glyphID]]
43
data = data + chr(width)
47
def toXML(self, writer, ttFont):
48
writer.begintag("hdmxData")
50
ppems = self.hdmx.keys()
55
widths = self.hdmx[ppem]
56
records.append(widths)
57
format = format + "%4d"
58
glyphNames = ttFont.getGlyphOrder()[:]
60
maxNameLen = max(map(len, glyphNames))
61
format = "%" + `maxNameLen` + 's:' + format + ' ;'
62
writer.write(format % (("ppem",) + tuple(ppems)))
65
for glyphName in glyphNames:
68
widths = self.hdmx[ppem]
69
row.append(widths[glyphName])
71
glyphName = "\\x3b".join(glyphName.split(";"))
72
writer.write(format % ((glyphName,) + tuple(row)))
74
writer.endtag("hdmxData")
77
def fromXML(self, (name, attrs, content), ttFont):
78
if name <> "hdmxData":
80
content = string.join(content, "")
81
lines = string.split(content, ";")
82
topRow = string.split(lines[0])
83
assert topRow[0] == "ppem:", "illegal hdmx format"
84
ppems = map(int, topRow[1:])
88
lines = map(string.split, lines[1:])
92
assert line[0][-1] == ":", "illegal hdmx format"
93
glyphName = line[0][:-1]
95
from fontTools.misc.textTools import safeEval
96
glyphName = safeEval('"""' + glyphName + '"""')
97
line = map(int, line[1:])
98
assert len(line) == len(ppems), "illegal hdmx format"
99
for i in range(len(ppems)):
100
hdmx[ppems[i]][glyphName] = line[i]