Skip to content

Commit

Permalink
Avoid producing the same destination for ditinct input files with the…
Browse files Browse the repository at this point in the history
… same name
  • Loading branch information
rsheeter committed Jan 15, 2022
1 parent 3d9a787 commit b237467
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/nanoemoji/nanoemoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,30 @@ def write_config_preamble(nw, font_config: FontConfig):
nw.newline()


def picosvg_dir(master_name: str) -> Path:
return build_dir() / "picosvg" / master_name


def picosvg_dest(master_name: str, clipped: bool, input_svg: Path) -> str:
out_dir = picosvg_dir(master_name)
def picosvg_dir() -> Path:
return build_dir() / "picosvg"


def picosvg_dest(clipped: bool, input_svg: Path) -> str:
if not hasattr(picosvg_dest, "names_seen"):
picosvg_dest.names_seen = {}

# If many different inputs have the same name disambiguate 1..N
# by including N in picosvg path
input_svg = input_svg.resolve()
nth_of_name = 0
while (
picosvg_dest.names_seen.get((nth_of_name, input_svg.name), input_svg)
!= input_svg
):
nth_of_name += 1
picosvg_dest.names_seen[(nth_of_name, input_svg.name)] = input_svg

out_dir = picosvg_dir()
if clipped:
out_dir = out_dir / "clipped"
if nth_of_name > 0:
out_dir = out_dir / str(nth_of_name)
return str(rel_build(out_dir / input_svg.name))


Expand All @@ -282,20 +298,20 @@ def diff_png_dest(input_svg: Path) -> str:


def write_picosvg_builds(
picosvg_builds: Set[str],
picosvg_builds: Set[Path],
nw: ninja_syntax.Writer,
clipped: bool,
master: MasterConfig,
):
rule_name = "picosvg_unclipped"
if clipped:
rule_name = "picosvg_clipped"
os.makedirs(str(picosvg_dir(master.name)), exist_ok=True)
for svg_file in master.sources:
dest = picosvg_dest(master.name, clipped, svg_file)
if dest in picosvg_builds:
svg_file = svg_file.resolve()
dest = picosvg_dest(clipped, svg_file)
if svg_file in picosvg_builds:
continue
picosvg_builds.add(dest)
picosvg_builds.add(svg_file)
nw.build(dest, rule_name, str(rel_build(svg_file)))


Expand Down Expand Up @@ -349,8 +365,7 @@ def write_svg_font_diff_build(
def _input_svgs(font_config: FontConfig, master: MasterConfig) -> List[str]:
if font_config.has_picosvgs:
svg_files = [
picosvg_dest(master.name, font_config.clip_to_viewbox, f)
for f in master.sources
picosvg_dest(font_config.clip_to_viewbox, f) for f in master.sources
]
else:
svg_files = [str(f.resolve()) for f in master.sources]
Expand All @@ -364,7 +379,7 @@ def _update_sources(font_config: FontConfig) -> FontConfig:
masters=tuple(
master._replace(
sources=tuple(
Path(picosvg_dest(master.name, font_config.clip_to_viewbox, s))
Path(picosvg_dest(font_config.clip_to_viewbox, s))
for s in master.sources
)
)
Expand Down Expand Up @@ -528,6 +543,7 @@ def _run(argv):

ninja_cmd = ["ninja", "-C", os.path.dirname(build_file)]
if FLAGS.exec_ninja:
os.makedirs(str(picosvg_dir()), exist_ok=True)
logging.info(" ".join(ninja_cmd))
subprocess.run(ninja_cmd, check=True)
else:
Expand Down
16 changes: 16 additions & 0 deletions tests/multi_toml/a.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

family = "A"
output_file = "A.ttf"
color_format = "glyf_colr_1"

[axis.wght]
name = "Weight"
default = 400

[master.regular]
style_name = "Regular"
srcs = [
"a/emoji_u270d.svg"]

[master.regular.position]
wght = 400
4 changes: 4 additions & 0 deletions tests/multi_toml/a/emoji_u270d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions tests/multi_toml/b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

family = "B"
output_file = "B.ttf"
color_format = "glyf_colr_1"

[axis.wght]
name = "Weight"
default = 400

[master.regular]
style_name = "Regular"
srcs = [
"b/emoji_u270d.svg"]

[master.regular.position]
wght = 400
5 changes: 5 additions & 0 deletions tests/multi_toml/b/emoji_u270d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/nanoemoji_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Integration tests for nanoemoji

from fontTools.ttLib import TTFont
from fontTools.ttLib.tables import otTables as ot
from lxml import etree # pytype: disable=import-error
from nanoemoji import config
import os
Expand Down Expand Up @@ -209,3 +210,27 @@ def test_omit_empty_color_glyphs():
"omit_empty_color_glyphs.ttx",
include_tables=["GlyphOrder", "cmap", "glyf", "COLR", "SVG "],
)


# https://github.com/googlefonts/nanoemoji/issues/367
def test_path_to_src_matters():
def _glyph(font):
assert font["COLR"].version == 1
colr_table = font["COLR"].table
assert colr_table.BaseGlyphList.BaseGlyphCount == 1
paint = colr_table.BaseGlyphList.BaseGlyphPaintRecord[0].Paint
assert paint.Format == ot.PaintFormat.PaintGlyph
return font["glyf"][paint.Glyph]

tomls = [
"multi_toml/a.toml",
"multi_toml/b.toml",
]

tmp_dir = _run(tuple(locate_test_file(toml) for toml in tomls))

font_a = TTFont(tmp_dir / "A.ttf")
font_b = TTFont(tmp_dir / "B.ttf")

# Each font should define a single PaintGlyph and the glyph it uses shouldn't be identical
assert _glyph(font_a) != _glyph(font_b)

0 comments on commit b237467

Please sign in to comment.