Skip to content

Commit

Permalink
Use clip viewbox via picosvg
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Dec 5, 2020
1 parent a7bd0c6 commit b39bf1c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 67 deletions.
2 changes: 0 additions & 2 deletions src/nanoemoji/color_glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,6 @@ def create(
# Grab the transform + (color, glyph) layers unless they aren't to be touched
painted_layers = None
if font_config.has_picosvgs:
if font_config.clip_to_viewbox:
svg = svg.clip_to_viewbox()
painted_layers = tuple(_painted_layers(filename, ufo.info.unitsPerEm, svg))
return ColorGlyph(
ufo, filename, glyph_name, glyph_id, codepoints, painted_layers, svg
Expand Down
17 changes: 13 additions & 4 deletions src/nanoemoji/nanoemoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ def rel_build(path: Path) -> Path:
return rel(build_dir(), path)


def _bool_flag(name):
def _get_bool_flag(name: str):
return getattr(FLAGS, name)


def _bool_flag(name: str, value: bool):
flag = " --"
if not getattr(FLAGS, name):
if not value:
flag += "no"
flag += name
return flag
Expand Down Expand Up @@ -104,7 +108,7 @@ def write_font_rule(rule_name, config):
module_rule(
"write_font",
f" --config {config}"
+ _bool_flag("ignore_reuse_error")
+ _bool_flag("ignore_reuse_error", _get_bool_flag("ignore_reuse_error"))
+ " -v 1"
+ " @$out.rsp",
rspfile="$out.rsp",
Expand All @@ -116,7 +120,12 @@ def write_font_rule(rule_name, config):
nw.comment("Generated by nanoemoji")
nw.newline()

nw.rule(f"picosvg", f"picosvg $in > $out")
nw.rule(
f"picosvg",
f"picosvg "
+ _bool_flag("clip_to_viewbox", font_config.clip_to_viewbox)
+ " $in > $out",
)
nw.newline()

module_rule(
Expand Down
File renamed without changes
48 changes: 35 additions & 13 deletions tests/nanoemoji_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

from fontTools.ttLib import TTFont
from lxml import etree # pytype: disable=import-error
from nanoemoji import config
from pathlib import Path
from picosvg.svg import SVG
import pytest
import subprocess
import tempfile
from test_helper import locate_test_file
from test_helper import assert_expected_ttx, color_font_config, locate_test_file


def _svg_element_names(xpath, svg_content):
Expand All @@ -31,15 +32,16 @@ def _svg_element_names(xpath, svg_content):
)


def _run(cmd):
tmp_dir = tempfile.mkdtemp()
def _run(cmd, tmp_dir=None):
if not tmp_dir:
tmp_dir = tempfile.mkdtemp()

cmd = (
"nanoemoji",
"--build_dir",
tmp_dir,
) + cmd
print(cmd) # very useful on failure
str(tmp_dir),
) + tuple(str(c) for c in cmd)
print("subprocess:", " ".join(cmd)) # very useful on failure
subprocess.run(cmd, check=True)

tmp_dir = Path(tmp_dir)
Expand All @@ -55,15 +57,35 @@ def test_build_static_font_default_config_cli_svg_list():
assert "fvar" not in font


def test_build_static_font():
cmd = (
"--config",
locate_test_file("minimal_static/config.toml"),
def _build_and_check_ttx(config_overrides, svgs, expected_ttx):
config_file = Path(tempfile.mkdtemp()) / "config.toml"
font_config, _ = color_font_config(
config_overrides, svgs, tmp_dir=str(config_file.parent)
)
tmp_dir = _run(cmd)
config.write(config_file, font_config)
print(config_file, font_config)

font = TTFont(tmp_dir / "Font.ttf")
assert "fvar" not in font
_run(("--config", str(config_file)), tmp_dir=config_file.parent)
font = TTFont(config_file.parent / "Font.ttf")
assert_expected_ttx(svgs, font, expected_ttx)


# Drop content outside viewbox
# https://github.com/googlefonts/nanoemoji/issues/200
@pytest.mark.usefixtures("absl_flags")
def test_build_static_font_clipped():
_build_and_check_ttx({}, ("emoji_u25fd.svg",), "outside_viewbox_clipped_colr_1.ttx")


# Retain content outside viewbox
# https://github.com/googlefonts/nanoemoji/issues/200
@pytest.mark.usefixtures("absl_flags")
def test_build_static_font_unclipped():
_build_and_check_ttx(
{"clip_to_viewbox": False},
("emoji_u25fd.svg",),
"outside_viewbox_not_clipped_colr_1.ttx",
)


def test_build_variable_font():
Expand Down
28 changes: 14 additions & 14 deletions tests/outside_viewbox_clipped_colr_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
<!-- 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="e000.0"/>
<GlyphID id="4" name="e000.1"/>
<GlyphID id="2" name="g_25fd"/>
<GlyphID id="3" name="g_25fd.0"/>
<GlyphID id="4" name="g_25fd.1"/>
</GlyphOrder>

<hmtx>
<mtx name=".notdef" width="0" lsb="0"/>
<mtx name=".space" width="100" lsb="0"/>
<mtx name="e000" width="100" lsb="0"/>
<mtx name="e000.0" width="100" lsb="20"/>
<mtx name="e000.1" width="100" lsb="70"/>
<mtx name="g_25fd" width="100" lsb="0"/>
<mtx name="g_25fd.0" width="100" lsb="20"/>
<mtx name="g_25fd.1" width="100" lsb="70"/>
</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="0x25fd" name="g_25fd"/><!-- WHITE MEDIUM SMALL SQUARE -->
</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="0x25fd" name="g_25fd"/><!-- WHITE MEDIUM SMALL SQUARE -->
</cmap_format_4>
</cmap>

Expand All @@ -43,15 +43,15 @@

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

<TTGlyph name="e000" xMin="0" yMin="0" xMax="100" yMax="100">
<TTGlyph name="g_25fd" xMin="0" yMin="0" xMax="100" yMax="100">
<contour>
<pt x="100" y="100" on="1"/>
<pt x="0" y="0" on="1"/>
</contour>
<instructions/>
</TTGlyph>

<TTGlyph name="e000.0" xMin="20" yMin="60" xMax="80" yMax="80">
<TTGlyph name="g_25fd.0" xMin="20" yMin="60" xMax="80" yMax="80">
<contour>
<pt x="20" y="80" on="1"/>
<pt x="20" y="60" on="1"/>
Expand All @@ -61,7 +61,7 @@
<instructions/>
</TTGlyph>

<TTGlyph name="e000.1" xMin="70" yMin="30" xMax="100" yMax="70">
<TTGlyph name="g_25fd.1" xMin="70" yMin="30" xMax="100" yMax="70">
<contour>
<pt x="70" y="50" on="1"/>
<pt x="70" y="44" on="0"/>
Expand Down Expand Up @@ -89,7 +89,7 @@
<BaseGlyphV1List>
<!-- BaseGlyphCount=1 -->
<BaseGlyphV1Record index="0">
<BaseGlyph value="e000"/>
<BaseGlyph value="g_25fd"/>
<Paint Format="1"><!-- PaintColrLayers -->
<NumLayers value="2"/>
<FirstLayerIndex value="0"/>
Expand All @@ -105,7 +105,7 @@
<Alpha value="1.0"/>
</Color>
</Paint>
<Glyph value="e000.0"/>
<Glyph value="g_25fd.0"/>
</Paint>
<Paint index="1" Format="5"><!-- PaintGlyph -->
<Paint Format="2"><!-- PaintSolid -->
Expand All @@ -114,7 +114,7 @@
<Alpha value="1.0"/>
</Color>
</Paint>
<Glyph value="e000.1"/>
<Glyph value="g_25fd.1"/>
</Paint>
</LayerV1List>
</COLR>
Expand Down
36 changes: 18 additions & 18 deletions tests/outside_viewbox_not_clipped_colr_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
<!-- 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="e000.0"/>
<GlyphID id="4" name="e000.1"/>
<GlyphID id="5" name="e000.2"/>
<GlyphID id="2" name="g_25fd"/>
<GlyphID id="3" name="g_25fd.0"/>
<GlyphID id="4" name="g_25fd.1"/>
<GlyphID id="5" name="g_25fd.2"/>
</GlyphOrder>

<hmtx>
<mtx name=".notdef" width="0" lsb="0"/>
<mtx name=".space" width="100" lsb="0"/>
<mtx name="e000" width="100" lsb="0"/>
<mtx name="e000.0" width="100" lsb="20"/>
<mtx name="e000.1" width="100" lsb="70"/>
<mtx name="e000.2" width="100" lsb="20"/>
<mtx name="g_25fd" width="100" lsb="0"/>
<mtx name="g_25fd.0" width="100" lsb="20"/>
<mtx name="g_25fd.1" width="100" lsb="70"/>
<mtx name="g_25fd.2" 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="0x25fd" name="g_25fd"/><!-- WHITE MEDIUM SMALL SQUARE -->
</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="0x25fd" name="g_25fd"/><!-- WHITE MEDIUM SMALL SQUARE -->
</cmap_format_4>
</cmap>

Expand All @@ -45,15 +45,15 @@

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

<TTGlyph name="e000" xMin="0" yMin="0" xMax="100" yMax="100">
<TTGlyph name="g_25fd" xMin="0" yMin="0" xMax="100" yMax="100">
<contour>
<pt x="100" y="100" on="1"/>
<pt x="0" y="0" on="1"/>
</contour>
<instructions/>
</TTGlyph>

<TTGlyph name="e000.0" xMin="20" yMin="60" xMax="80" yMax="80">
<TTGlyph name="g_25fd.0" xMin="20" yMin="60" xMax="80" yMax="80">
<contour>
<pt x="20" y="80" on="1"/>
<pt x="20" y="60" on="1"/>
Expand All @@ -63,7 +63,7 @@
<instructions/>
</TTGlyph>

<TTGlyph name="e000.1" xMin="70" yMin="30" xMax="110" yMax="70">
<TTGlyph name="g_25fd.1" xMin="70" yMin="30" xMax="110" yMax="70">
<contour>
<pt x="70" y="50" on="1"/>
<pt x="70" y="44" on="0"/>
Expand All @@ -85,7 +85,7 @@
<instructions/>
</TTGlyph>

<TTGlyph name="e000.2" xMin="20" yMin="-60" xMax="80" yMax="0">
<TTGlyph name="g_25fd.2" xMin="20" yMin="-60" xMax="80" yMax="0">
<contour>
<pt x="20" y="-30" on="1"/>
<pt x="20" y="-36" on="0"/>
Expand Down Expand Up @@ -120,7 +120,7 @@
<BaseGlyphV1List>
<!-- BaseGlyphCount=1 -->
<BaseGlyphV1Record index="0">
<BaseGlyph value="e000"/>
<BaseGlyph value="g_25fd"/>
<Paint Format="1"><!-- PaintColrLayers -->
<NumLayers value="3"/>
<FirstLayerIndex value="0"/>
Expand All @@ -136,7 +136,7 @@
<Alpha value="1.0"/>
</Color>
</Paint>
<Glyph value="e000.0"/>
<Glyph value="g_25fd.0"/>
</Paint>
<Paint index="1" Format="5"><!-- PaintGlyph -->
<Paint Format="2"><!-- PaintSolid -->
Expand All @@ -145,7 +145,7 @@
<Alpha value="1.0"/>
</Color>
</Paint>
<Glyph value="e000.1"/>
<Glyph value="g_25fd.1"/>
</Paint>
<Paint index="2" Format="5"><!-- PaintGlyph -->
<Paint Format="2"><!-- PaintSolid -->
Expand All @@ -154,7 +154,7 @@
<Alpha value="1.0"/>
</Color>
</Paint>
<Glyph value="e000.2"/>
<Glyph value="g_25fd.2"/>
</Paint>
</LayerV1List>
</COLR>
Expand Down
6 changes: 4 additions & 2 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ def picosvg(filename, locate=False):
return SVG.parse(filename).topicosvg()


def color_font_config(config_overrides, svgs):
def color_font_config(config_overrides, svgs, tmp_dir=None):
if tmp_dir is None:
tmp_dir = tempfile.gettempdir()
svgs = tuple(locate_test_file(s) for s in svgs)
fea_file = os.path.join(tempfile.gettempdir(), "test.fea")
fea_file = os.path.join(tmp_dir, "test.fea")
rgi_seqs = tuple(codepoints.from_filename(str(f)) for f in svgs)
with open(fea_file, "w") as f:
f.write(write_fea._generate_fea(rgi_seqs))
Expand Down
14 changes: 0 additions & 14 deletions tests/write_font_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,6 @@ def test_keep_glyph_names(svgs, color_format, keep_glyph_names):
"reused_shape_glyf.ttx",
{"color_format": "glyf"},
),
# Drop content outside viewbox
# https://github.com/googlefonts/nanoemoji/issues/200
(
("outside_viewbox.svg",),
"outside_viewbox_clipped_colr_1.ttx",
{"color_format": "glyf_colr_1"},
),
# Retain content outside viewbox
# https://github.com/googlefonts/nanoemoji/issues/200
(
("outside_viewbox.svg",),
"outside_viewbox_not_clipped_colr_1.ttx",
{"color_format": "glyf_colr_1", "clip_to_viewbox": False},
),
],
)
@pytest.mark.usefixtures("absl_flags")
Expand Down

0 comments on commit b39bf1c

Please sign in to comment.