Change function name for getting name data from font to be more obvious

Add use of name data to example.py
Add additional fields to the maxp table
Correctly set the number of glyphs in a subfont
Adjust some extra information in a subfont
This commit is contained in:
david reid
2015-03-22 10:42:15 +00:00
parent 1388af6b76
commit bd80f270b6
5 changed files with 39 additions and 9 deletions

2
.gitignore vendored
View File

@@ -58,4 +58,4 @@ target/
# Ignore any fonts copied while testing!
*.ttf
extra/

View File

@@ -16,6 +16,7 @@ if __name__ == '__main__':
print(t.faces[0].font_family)
print(t.faces[0].name)
print(t.faces[0].italic_angle)
print(t.faces[0].get_name_attr(10))
subset = [ord('H'), ord('e'), ord('l'), ord('o')]
font_subset = t.faces[0].make_subset(subset)

View File

@@ -220,7 +220,7 @@ class TTF_hhea(PackedFormat):
{'name': 'line_gap', 'format': 'h'},
{'name': 'advance_width_max', 'format': 'H'},
{'name': 'min_left_side_bearing', 'format': 'h'},
{'name': 'min_right_dide_brearing', 'format': 'h'},
{'name': 'min_right_side_bearing', 'format': 'h'},
{'name': 'x_max_extant', 'format': 'h'},
{'name': 'caret_slope_rise', 'format': 'h'},
{'name': 'caret_slope_run', 'format': 'h'},
@@ -292,6 +292,19 @@ class TTF_maxp(PackedFormat):
FORMAT = [
{'name': 'version', 'format': 'I', 'convert': fixed_version},
{'name': 'num_glyphs', 'format': 'H'},
{'name': 'max_points', 'format': 'H'},
{'name': 'max_contours', 'format': 'H'},
{'name': 'max_component_points', 'format': 'H'},
{'name': 'max_component_contours', 'format': 'H'},
{'name': 'max_zones', 'format': 'H'},
{'name': 'max_twilight_points', 'format': 'H'},
{'name': 'max_storage', 'format': 'H'},
{'name': 'max_functiondefs', 'format': 'H'},
{'name': 'max_instructiondefs', 'format': 'H'},
{'name': 'max_stack_elements', 'format': 'H'},
{'name': 'max_size_of_instructions', 'format': 'H'},
{'name': 'max_component_elements', 'format': 'H'},
{'name': 'max_component_depth', 'format': 'H'},
]

View File

@@ -22,6 +22,7 @@ class TTFSubset:
self.required_glyphs = [0]
self.metrics = []
self.max_contours = 0
self.fh = None
@@ -57,7 +58,7 @@ class TTFSubset:
self.glyph_map = {}
for rg in self.required_glyphs:
glyph = len(self.glyph_map) + 1
glyph = len(self.glyph_map)
self.glyph_map[rg] = glyph
if rg in self.orig_glyph_to_char:
for cc in self.orig_glyph_to_char[rg]:
@@ -84,13 +85,20 @@ class TTFSubset:
hhea = self.parent.copy_table(b'hhea')
hhea.number_of_metrics = len(self.metrics)
hhea.advance_width_max = 0
hhea.min_left_side_bearing = 0
# todo calculate min_right_side_bearing...
for (aw, lsb) in self.metrics:
hhea.advance_width_max = max(hhea.advance_width_max, aw)
hhea.min_left_side_bearing = min(hhea.min_left_side_bearing, lsb * -1)
self.start_table(b'hhea', hhea.as_bytes())
maxp = self.parent.copy_table(b'maxp')
maxp.b_glyphs = len(self.required_glyphs)
maxp.num_glyphs = len(self.required_glyphs)
maxp.max_contours = self.max_contours
self.start_table(b'maxp', maxp.as_bytes())
self.start_table(b'os2', self.parent.copy_table(b'os2').as_bytes())
self.start_table(b'OS/2', self.parent.copy_table(b'os2').as_bytes())
# todo - is it worth finding a way to subset the GPOS and LTSH tables?
def build_cmap_ranges(self):
@@ -149,13 +157,16 @@ class TTFSubset:
def get_glyphs(self):
locations = []
self.metrics = []
self.max_contours = 0
buff = self.start_table(b'glyf')
for g in self.required_glyphs:
locations.append(int(buff.tell() / 2))
data = self.parent.get_glyph_data(g)
if data == b'':
continue
if unpack(">h", data[:2])[0] == -1:
n_contours = unpack(">h", data[:2])[0]
self.max_contours = max(self.max_contours, n_contours)
if n_contours == -1:
# need to adjust glyph index...
pos = 10
while True:
@@ -209,9 +220,9 @@ class TTFSubset:
self.find_glyph_subset()
self.add_kern_data()
self.copy_tables()
self.add_cmap_table()
self.get_glyphs()
self.copy_tables()
# self.dump_tables()
self.fh.close()

View File

@@ -69,7 +69,7 @@ class TTFont(object):
if item in self.COMMON_DATA:
how = self.COMMON_DATA[item]
if how[0] == b'name':
return self.get_name_table(*how[1:])
return self.get_name_attr(*how[1:])
if len(how) > 2:
return self.get_table_attr(*how[:3])
return self.get_table_attr(*how)
@@ -121,7 +121,12 @@ class TTFont(object):
return default
return getattr(self.tables[tbl], attr, default)
def get_name_table(self, n_attr, default=None):
def get_name_attr(self, n_attr, default=None):
""" Return the string from the name table with the number given.
:param n_attr: Number of name entry to return
:param default: Return value if no entry is available (deafult is None).
:return: String from name table. default if not available
"""
if b'name' not in self.tables:
return default
return self.tables[b'name'].get_name(n_attr, default)