This commit is contained in:
arpruss
2018-07-17 08:53:20 -05:00
parent 85ef50c5b7
commit f3d6d31c4e
2 changed files with 23 additions and 7 deletions

View File

@@ -3,6 +3,17 @@ import sys
from zttf.ttfile import TTFile from zttf.ttfile import TTFile
from struct import unpack from struct import unpack
def glyphBox(f,g):
data = f.get_glyph_data(g)
if not len(data):
return (0,0,0,0)
return unpack(">hhhhh", data[:10])[1:]
def describeChar(c):
if c >= 32 and c<127 and c != ord('\\') and c != ord('"'):
return '"'+chr(c)+'"'
else:
return "chr(%d)" % c
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
@@ -34,7 +45,7 @@ if __name__ == '__main__':
print(chr(c),ascent) print(chr(c),ascent)
maxAscent = max(ascent,maxAscent) maxAscent = max(ascent,maxAscent)
""" """
maxAscent = f.tables[b'os2'].sTypoAscender ascent = f.tables[b'os2'].sTypoAscender
fontID = re.sub(r"[^0-9A-Za-z]", "_", f.name) fontID = re.sub(r"[^0-9A-Za-z]", "_", f.name)
if fontID[0].isdigit(): if fontID[0].isdigit():
@@ -44,18 +55,23 @@ if __name__ == '__main__':
%d], // style %d], // style
%d, // ascender %d, // ascender
%d, // descender %d, // descender
%d, // line gap
%f, // units_per_em %f, // units_per_em
[ [""" % (fontID, f.font_family, f.tables[b'head'].mac_style, ascent,
""" % (fontID, f.font_family, f.tables[b'head'].mac_style, maxAscent, f.tables[b'os2'].sTypoDescender, f.tables[b'os2'].sTypoDescender,
f.tables[b'os2'].typo_line_gap,
f.units_per_em)) f.units_per_em))
for c in chars: for c in chars:
glyph = f.char_to_glyph(c) glyph = f.char_to_glyph(c)
line = " [ chr(%d), %d, [" % ( c, f.glyph_metrics[glyph][0] ) box = glyphBox(f,glyph)
line = " [%s,%d,%d,%d,%d,%d,%d,[" % ( describeChar(c), f.glyph_metrics[glyph][0],
f.glyph_metrics[glyph][1], box[0], box[1], box[2], box[3] )
kerns=[]
for c2 in chars: for c2 in chars:
r = f.char_to_glyph(c2) r = f.char_to_glyph(c2)
if (glyph,r) in f.glyph_kern: if (glyph,r) in f.glyph_kern:
line += "[chr(%d),%d]," % (c2, f.glyph_kern[(glyph,r)]) kerns.append("[%s,%d]" % (describeChar(c2), f.glyph_kern[(glyph,r)]))
line += "] ]," line += ','.join(kerns)+"]],"
print(line) print(line)
print(" ]\n];\n") print(" ]\n];\n")

View File

@@ -208,7 +208,7 @@ class TTFont(object):
glyph_start = self.get_glyph_position(glyph) glyph_start = self.get_glyph_position(glyph)
glyph_length = self.get_glyph_position(glyph + 1) - glyph_start glyph_length = self.get_glyph_position(glyph + 1) - glyph_start
if glyph_length == 0: if glyph_length == 0:
print("Zero length glyph @ {}".format(glyph)) #print("Zero length glyph @ {}".format(glyph))
return b'' return b''
self._open() self._open()
self.file_handle.seek(data_start + glyph_start) self.file_handle.seek(data_start + glyph_start)