From b811c51030fcee1d584d236aa9970f4d56d382b1 Mon Sep 17 00:00:00 2001 From: Rod S Date: Thu, 12 May 2022 20:59:49 -0700 Subject: [PATCH] Fix CI --- src/nanoemoji/maximum_color.py | 77 +++++++++++++++++++++++--- src/nanoemoji/svg.py | 8 +-- src/nanoemoji/write_font.py | 9 ++- tests/test_helper.py | 4 +- tests/transformed_gradient_reuse.ttx | 82 ++++++++++++++++------------ 5 files changed, 126 insertions(+), 54 deletions(-) diff --git a/src/nanoemoji/maximum_color.py b/src/nanoemoji/maximum_color.py index 8f1cc89a..f674a1bd 100644 --- a/src/nanoemoji/maximum_color.py +++ b/src/nanoemoji/maximum_color.py @@ -65,6 +65,7 @@ class WriteFontInputs(NamedTuple): glyphmap_file: Path config_file: Path + part_file: Path @property def table_tag(self) -> str: @@ -88,7 +89,11 @@ def color_format(self) -> str: @classmethod def for_tag(cls, table_tag: str) -> "WriteFontInputs": basename = table_tag.strip() - return cls(Path(basename + ".glyphmap"), Path(basename + ".toml")) + return cls( + Path(basename + ".glyphmap"), + Path(basename + ".toml"), + master_part_file_dest(), + ) def _vector_color_table(font: ttLib.TTFont) -> str: @@ -121,6 +126,14 @@ def picosvg_dest(input_svg: Path) -> Path: return picosvg_dir() / input_svg.name +def part_file_dest(picosvg_file: Path) -> Path: + return picosvg_file.with_suffix(".parts.json") + + +def master_part_file_dest() -> Path: + return Path("parts-merged.json") + + def bitmap_dir() -> Path: return build_dir() / "bitmap" @@ -163,7 +176,7 @@ def _write_preamble(nw: NinjaWriter): module_rule( nw, "write_font", - f"--glyphmap_file $glyphmap_file --config_file $config_file --output_file $out", + f"--config_file $config_file --glyphmap_file $glyphmap_file --part_file $part_file --output_file $out", ) nw.newline() @@ -173,6 +186,20 @@ def _write_preamble(nw: NinjaWriter): ) nw.newline() + module_rule( + nw, + "write_part_file", + f"--reuse_tolerance $reuse_tolerance --output_file $out $in", + ) + nw.newline() + + module_rule( + nw, + "write_combined_part_files", + f"--output_file $out $in", + ) + nw.newline() + # set height only, let width scale proportionally res = config.load().bitmap_resolution nw.rule( @@ -237,6 +264,27 @@ def _picosvgs(nw: NinjaWriter, svg_files: List[Path]) -> List[Path]: return picosvgs +def _part_file( + nw: NinjaWriter, reuse_tolerance: float, picosvg_files: List[Path] +) -> Path: + part_files = [part_file_dest(p) for p in picosvg_files] + for picosvg_file, part_file in zip(picosvg_files, part_files): + nw.build( + part_file, + "write_part_file", + picosvg_file, + variables={"reuse_tolerance": reuse_tolerance}, + ) + + nw.build( + master_part_file_dest(), + "write_combined_part_files", + sorted(part_files), + ) + + return master_part_file_dest() + + def _generate_additional_color_table( nw: NinjaWriter, input_font: Path, @@ -283,7 +331,10 @@ def _generate_additional_color_table( def _generate_svg_from_colr( - nw: NinjaWriter, input_font: Path, font: ttLib.TTFont + nw: NinjaWriter, + font_config: config.FontConfig, + input_font: Path, + font: ttLib.TTFont, ) -> Tuple[Path, List[Path]]: # generate svgs svg_files = [ @@ -294,6 +345,8 @@ def _generate_svg_from_colr( # create and merge an SVG table picosvgs = _picosvgs(nw, svg_files) + part_file = _part_file(nw, font_config.reuse_tolerance, picosvgs) + output_file = _generate_additional_color_table( nw, input_font, picosvgs + [input_font], "SVG ", input_font ) @@ -301,7 +354,10 @@ def _generate_svg_from_colr( def _generate_colr_from_svg( - nw: NinjaWriter, input_font: Path, font: ttLib.TTFont + nw: NinjaWriter, + font_config: config.FontConfig, + input_font: Path, + font: ttLib.TTFont, ) -> Tuple[Path, List[Path]]: # extract the svgs svg_files = [ @@ -312,6 +368,8 @@ def _generate_colr_from_svg( # create and merge a COLR table picosvgs = _picosvgs(nw, svg_files) + part_file = _part_file(nw, font_config.reuse_tolerance, picosvgs) + output_file = _generate_additional_color_table( nw, input_font, picosvgs + [input_font], "COLR", input_font ) @@ -371,7 +429,8 @@ def _run(argv): input_file = Path(argv[1]).resolve() # we need a non-relative path assert input_file.is_file() font = ttLib.TTFont(input_file) - final_output = Path(config.load().output_file) + font_config = config.load() + final_output = Path(font_config.output_file) assert ( input_file.resolve() != (build_dir() / final_output).resolve() ), "In == Out is bad" @@ -391,9 +450,13 @@ def _run(argv): # generate the missing vector table if color_table == "COLR": - wip_file, picosvg_files = _generate_svg_from_colr(nw, wip_file, font) + wip_file, picosvg_files = _generate_svg_from_colr( + nw, font_config, wip_file, font + ) else: - wip_file, picosvg_files = _generate_colr_from_svg(nw, wip_file, font) + wip_file, picosvg_files = _generate_colr_from_svg( + nw, font_config, wip_file, font + ) if FLAGS.bitmaps: wip_file = _generate_cbdt(nw, input_file, font, wip_file, picosvg_files) diff --git a/src/nanoemoji/svg.py b/src/nanoemoji/svg.py index 2e89a627..383ed146 100644 --- a/src/nanoemoji/svg.py +++ b/src/nanoemoji/svg.py @@ -506,8 +506,6 @@ def _add_glyph(svg: SVG, color_glyph: ColorGlyph, reuse_cache: ReuseCache): ), f"{paint.glyph} doesn't look like a path" maybe_reuse = reuse_cache.glyph_cache.try_reuse(paint.glyph) - print(glyph_name) - print(" ", maybe_reuse) # if we have a glyph for the shape already, use that if reuse_cache.glyph_cache.is_known_path(maybe_reuse.shape): @@ -519,7 +517,6 @@ def _add_glyph(svg: SVG, color_glyph: ColorGlyph, reuse_cache: ReuseCache): ) # the reused glyph will already exist, but it may need adjustment on grounds of reuse - # print(" ", "id needed", reused_glyph_name) reused_el = reuse_cache.glyph_elements[reused_glyph_name] reused_el.attrib["id"] = reused_glyph_name # hard to target w/o id @@ -532,9 +529,6 @@ def _add_glyph(svg: SVG, color_glyph: ColorGlyph, reuse_cache: ReuseCache): # https://github.com/googlefonts/nanoemoji/issues/337 # We don't know if #1 holds so to make life simpler just always # promote reused glyphs to defs - # if reuse_cache.reuse_spans_glyphs( - # glyph_instruction.path - # ) or _attrib_apply_paint_uses(el): _migrate_to_defs(svg, reused_el, reuse_cache, reused_glyph_name) # We must apply the inverse of the reuse transform to the children @@ -553,7 +547,7 @@ def _add_glyph(svg: SVG, color_glyph: ColorGlyph, reuse_cache: ReuseCache): _apply_paint( svg_defs, svg_use, - context.paint.paint, + paint.paint, upem_to_vbox, reuse_cache, inverse_reuse_transform, diff --git a/src/nanoemoji/write_font.py b/src/nanoemoji/write_font.py index db0bc74d..d0923be1 100644 --- a/src/nanoemoji/write_font.py +++ b/src/nanoemoji/write_font.py @@ -308,7 +308,7 @@ def _fix_nested_gradient(reuse_transform: Affine2D, paint: PaintGlyph) -> Paint: child_paint = paint.paint if is_transform(child_paint): child_transform = child_paint.gettransform() - child_paint = child_paint.paint + child_paint = child_paint.paint # pytype: disable=attribute-error # TODO: handle gradient anywhere in subtree, not only as direct child of # PaintGlyph or PaintTransform @@ -323,7 +323,9 @@ def _fix_nested_gradient(reuse_transform: Affine2D, paint: PaintGlyph) -> Paint: # skip reuse if combined transform overflows OT int bounds if fixed_safe(*gradient_fix_transform): try: - child_paint = child_paint.apply_transform(gradient_fix_transform) + child_paint = child_paint.apply_transform( + gradient_fix_transform + ) # pytype: disable=attribute-error except OverflowError: child_paint = transformed(gradient_fix_transform, child_paint) @@ -337,6 +339,8 @@ def _create_glyphs_for_svg_paths( if paint.format != PaintGlyph.format: return paint + paint = cast(PaintGlyph, paint) + if glyph_cache.is_known_glyph(paint.glyph): return paint @@ -345,7 +349,6 @@ def _create_glyphs_for_svg_paths( path_in_svg_space = paint.glyph maybe_reuse = glyph_cache.try_reuse(path_in_svg_space) - print(maybe_reuse) # if we have a glyph for the target already, use that if glyph_cache.is_known_path(maybe_reuse.shape): diff --git a/tests/test_helper.py b/tests/test_helper.py index b1fd8188..7ff02a07 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -232,7 +232,9 @@ def assert_expected_ttx( ) if actual != expected: - full_ttx_file = _save_ttx_in_tmp(expected_ttx.replace('.ttx', "-complete.ttx"), _ttx(ttfont)) + full_ttx_file = _save_ttx_in_tmp( + expected_ttx.replace(".ttx", "-complete.ttx"), _ttx(ttfont) + ) for line in difflib.unified_diff( expected.splitlines(keepends=True), diff --git a/tests/transformed_gradient_reuse.ttx b/tests/transformed_gradient_reuse.ttx index 248c1081..bb8f2dfa 100644 --- a/tests/transformed_gradient_reuse.ttx +++ b/tests/transformed_gradient_reuse.ttx @@ -7,6 +7,7 @@ + @@ -14,6 +15,7 @@ + @@ -79,6 +81,24 @@ + + + + + + + + + + + + + + + + + + @@ -126,44 +146,34 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - + + - - - - - - - - +