Skip to content

Commit

Permalink
[glyf] keep single-component glyphs if component is reused
Browse files Browse the repository at this point in the history
Fixes #114
  • Loading branch information
anthrotype committed Sep 11, 2020
1 parent 680b4bf commit b83df8a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/nanoemoji/write_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,15 @@ def _draw_glyph_extents(ufo: ufoLib2.Font, glyph: Glyph):
def _glyf_ufo(ufo, color_glyphs):
# glyphs by reuse_key
glyphs = {}
reused = set()
for color_glyph in color_glyphs:
logging.debug(
"%s %s %s",
ufo.info.familyName,
color_glyph.glyph_name,
color_glyph.transform_for_font_space(),
)
parent_glyph = ufo.get(color_glyph.glyph_name)
parent_glyph = ufo[color_glyph.glyph_name]
for painted_layer in color_glyph.as_painted_layers():
# if we've seen this shape before reuse it
reuse_key = _inter_glyph_reuse_key(painted_layer)
Expand All @@ -291,10 +292,16 @@ def _glyf_ufo(ufo, color_glyphs):
glyphs[reuse_key] = glyph
else:
glyph = glyphs[reuse_key]
reused.add(glyph.name)
parent_glyph.components.append(Component(baseGlyph=glyph.name))

# No great reason to keep single-component glyphs around
if len(parent_glyph.components) == 1:
for color_glyph in color_glyphs:
parent_glyph = ufo[color_glyph.glyph_name]
# No great reason to keep single-component glyphs around (unless reused)
if (
len(parent_glyph.components) == 1
and parent_glyph.components[0].baseGlyph not in reused
):
component = ufo[parent_glyph.components[0].baseGlyph]
del ufo[component.name]
component.unicode = parent_glyph.unicode
Expand Down
68 changes: 68 additions & 0 deletions tests/reused_rect_glyf.ttx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<ttFont sfntVersion="\x00\x01\x00\x00">

<GlyphOrder>
<!-- The 'id' attribute is only for humans; it is ignored when parsed. -->
<GlyphID id="0" name=".notdef"/>
<GlyphID id="1" name=".space"/>
<GlyphID id="2" name="e000"/>
<GlyphID id="3" name="e001"/>
<GlyphID id="4" name="e000.0"/>
</GlyphOrder>

<hmtx>
<mtx name=".notdef" width="0" lsb="0"/>
<mtx name=".space" width="100" lsb="0"/>
<mtx name="e000" width="100" lsb="20"/>
<mtx name="e000.0" width="100" lsb="20"/>
<mtx name="e001" width="100" lsb="20"/>
</hmtx>

<cmap>
<tableVersion version="0"/>
<cmap_format_4 platformID="0" platEncID="3" language="0">
<map code="0x20" name=".space"/><!-- SPACE -->
<map code="0xe000" name="e000"/><!-- ???? -->
<map code="0xe001" name="e001"/><!-- ???? -->
</cmap_format_4>
<cmap_format_4 platformID="3" platEncID="1" language="0">
<map code="0x20" name=".space"/><!-- SPACE -->
<map code="0xe000" name="e000"/><!-- ???? -->
<map code="0xe001" name="e001"/><!-- ???? -->
</cmap_format_4>
</cmap>

<loca>
<!-- The 'loca' table will be calculated by the compiler -->
</loca>

<glyf>

<!-- The xMin, yMin, xMax and yMax values
will be recalculated by the compiler. -->

<TTGlyph name=".notdef"/><!-- contains no outline data -->

<TTGlyph name=".space"/><!-- contains no outline data -->

<TTGlyph name="e000" xMin="20" yMin="60" xMax="80" yMax="80">
<component glyphName="e000.0" x="0" y="0" flags="0x204"/>
</TTGlyph>

<TTGlyph name="e000.0" xMin="20" yMin="60" xMax="80" yMax="80">
<contour>
<pt x="20" y="80" on="1"/>
<pt x="20" y="60" on="1"/>
<pt x="80" y="60" on="1"/>
<pt x="80" y="80" on="1"/>
</contour>
<instructions/>
</TTGlyph>

<TTGlyph name="e001" xMin="20" yMin="60" xMax="80" yMax="80">
<component glyphName="e000.0" x="0" y="0" flags="0x204"/>
</TTGlyph>

</glyf>

</ttFont>
2 changes: 2 additions & 0 deletions tests/write_font_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def test_keep_glyph_names(svgs, color_format, keep_glyph_names):
"untouchedsvg",
".ttf",
),
# keep single-component composites if component reused by more than one glyph
(("one_rect.svg", "one_rect.svg"), "reused_rect_glyf.ttx", "glyf", ".ttf"),
],
)
def test_write_font_binary(svgs, expected_ttx, color_format, output_format):
Expand Down

0 comments on commit b83df8a

Please sign in to comment.