diff --git a/python/afdko/ttfcomponentizer.py b/python/afdko/ttfcomponentizer.py index 64d6eb222..6ec9e30f2 100644 --- a/python/afdko/ttfcomponentizer.py +++ b/python/afdko/ttfcomponentizer.py @@ -18,7 +18,7 @@ from defcon import Font -__version__ = '0.1.1' +__version__ = '0.2.0' PUBLIC_PSNAMES = "public.postscriptNames" @@ -176,12 +176,24 @@ class ComponentsData(object): def __init__(self): self.names = () self.positions = () + self.same_advwidth = False def add_component(self, name, pos): self.names += (name,) self.positions += (pos,) +def check_1st_comp_advwidth(glyph): + """ + Returns True if the advance width of the composite glyph is the same as + the advance width of its first component, and False otherwise. + This information is essential for setting the flag of the composite's + first component later on. + """ + font = glyph.getParent() + return glyph.width == font[glyph.components[0].baseGlyph].width + + def get_composites_data(ufo, ps_names): """ Iterate thru each glyph of a UFO and collect the names and positions of @@ -201,12 +213,14 @@ def get_composites_data(ufo, ps_names): if glyph.components and not len(glyph): components = ComponentsData() all_comps_are_basic = True - for comp in glyph.components: + for i, comp in enumerate(glyph.components): if comp.transformation[:4] != (1, 0, 0, 1): all_comps_are_basic = False break comp_name = ps_names.get(comp.baseGlyph, comp.baseGlyph) components.add_component(comp_name, comp.transformation[4:]) + if i == 0: + components.same_advwidth = check_1st_comp_advwidth(glyph) if all_comps_are_basic: glyf_name = ps_names.get(glyph.name, glyph.name) composites_data[glyf_name] = components @@ -222,7 +236,9 @@ def assemble_components(comps_data): component = getTableModule('glyf').GlyphComponent() component.glyphName = cname component.x, component.y = comps_data.positions[i] - component.flags = 0x204 if i == 0 else 0x4 + component.flags = 0x4 + if i == 0 and comps_data.same_advwidth: + component.flags = 0x204 components.append(component) return components diff --git a/tests/ttfcomponentizer_data/expected_output/ttfcomponentizer.ttx b/tests/ttfcomponentizer_data/expected_output/ttfcomponentizer.ttx index d63943d9d..174ad32c8 100644 --- a/tests/ttfcomponentizer_data/expected_output/ttfcomponentizer.ttx +++ b/tests/ttfcomponentizer_data/expected_output/ttfcomponentizer.ttx @@ -1,14 +1,14 @@ - + - + - - + + @@ -106,6 +106,11 @@ + + + + + diff --git a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ttf b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ttf index 33fe19577..a92dd9ff5 100644 Binary files a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ttf and b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ttf differ diff --git a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/aa.glif b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/aa.glif new file mode 100644 index 000000000..25e50c022 --- /dev/null +++ b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/aa.glif @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/contents.plist b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/contents.plist index 72ef25ebb..59c97ed09 100644 --- a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/contents.plist +++ b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/glyphs/contents.plist @@ -6,6 +6,8 @@ _notdef.glif a a.glif + aa + aa.glif aacute aacute.glif acaron diff --git a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/lib.plist b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/lib.plist index 00e1289d3..ad186c345 100644 --- a/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/lib.plist +++ b/tests/ttfcomponentizer_data/input/ttfcomponentizer.ufo/lib.plist @@ -18,6 +18,7 @@ tildecmb caroncmb dieresiscmb + aa public.postscriptNames diff --git a/tests/ttfcomponentizer_test.py b/tests/ttfcomponentizer_test.py index 6ec515736..4aa6410cd 100644 --- a/tests/ttfcomponentizer_test.py +++ b/tests/ttfcomponentizer_test.py @@ -99,7 +99,7 @@ def test_run_with_output_path(): gtable = TTFont(save_path)['glyf'] composites = [gname for gname in gtable.glyphs if ( gtable[gname].isComposite())] - assert sorted(composites) == ['aacute', 'uni01CE'] + assert sorted(composites) == ['aa', 'aacute', 'uni01CE'] def test_run_cli_with_output_path(): @@ -221,17 +221,19 @@ def test_get_composites_data(): comps_data = ttfcomp.get_composites_data(ufo, ps_names) comps_name_list = sorted(comps_data.keys()) comps_comp_list = [comps_data[gname] for gname in comps_name_list] - assert comps_name_list == ['aacute', 'adieresis', 'atilde', 'uni01CE'] - assert comps_comp_list[0].names == ('a', 'uni0301') - assert comps_comp_list[3].names == ('a', 'uni030C') - assert comps_comp_list[0].positions == ((0, 0), (263.35, 0)) - assert comps_comp_list[3].positions == ((0, 0), (263, 0)) + assert comps_name_list == ['aa', 'aacute', 'adieresis', 'atilde', + 'uni01CE'] + assert comps_comp_list[1].names == ('a', 'uni0301') + assert comps_comp_list[4].names == ('a', 'uni030C') + assert comps_comp_list[1].positions == ((0, 0), (263.35, 0)) + assert comps_comp_list[4].positions == ((0, 0), (263, 0)) def test_assemble_components(): comps_data = Object() setattr(comps_data, 'names', ('a', 'uni01CE')) setattr(comps_data, 'positions', ((0, 0), (263, 0))) + setattr(comps_data, 'same_advwidth', True) comp_one, comp_two = ttfcomp.assemble_components(comps_data) assert comp_one.glyphName == 'a' assert comp_two.glyphName == 'uni01CE'