Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed May 13, 2022
1 parent e64d91e commit b811c51
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 54 deletions.
77 changes: 70 additions & 7 deletions src/nanoemoji/maximum_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
class WriteFontInputs(NamedTuple):
glyphmap_file: Path
config_file: Path
part_file: Path

@property
def table_tag(self) -> str:
Expand All @@ -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:
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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()

Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = [
Expand All @@ -294,14 +345,19 @@ 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
)
return output_file, picosvgs


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 = [
Expand All @@ -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
)
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions src/nanoemoji/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/nanoemoji/write_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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

Expand All @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
82 changes: 46 additions & 36 deletions tests/transformed_gradient_reuse.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
<GlyphID id="1" name=".space"/>
<GlyphID id="2" name="e000"/>
<GlyphID id="3" name="e000.0"/>
<GlyphID id="4" name="e000.1"/>
</GlyphOrder>

<hmtx>
<mtx name=".notdef" width="100" lsb="5"/>
<mtx name=".space" width="100" lsb="0"/>
<mtx name="e000" width="100" lsb="0"/>
<mtx name="e000.0" width="100" lsb="8"/>
<mtx name="e000.1" width="100" lsb="33"/>
</hmtx>

<cmap>
Expand Down Expand Up @@ -79,6 +81,24 @@
<instructions/>
</TTGlyph>

<TTGlyph name="e000.1" xMin="33" yMin="65" xMax="46" yMax="79">
<contour>
<pt x="46" y="72" on="1"/>
<pt x="46" y="75" on="0"/>
<pt x="42" y="79" on="0"/>
<pt x="39" y="79" on="1"/>
<pt x="37" y="79" on="0"/>
<pt x="33" y="75" on="0"/>
<pt x="33" y="72" on="1"/>
<pt x="33" y="69" on="0"/>
<pt x="37" y="65" on="0"/>
<pt x="39" y="65" on="1"/>
<pt x="42" y="65" on="0"/>
<pt x="46" y="69" on="0"/>
</contour>
<instructions/>
</TTGlyph>

</glyf>

<COLR>
Expand Down Expand Up @@ -126,44 +146,34 @@
</Paint>
<Glyph value="e000.0"/>
</Paint>
<Paint index="1" Format="12"><!-- PaintTransform -->
<Paint Format="10"><!-- PaintGlyph -->
<Paint Format="16"><!-- PaintScale -->
<Paint Format="6"><!-- PaintRadialGradient -->
<ColorLine>
<Extend value="pad"/>
<!-- StopCount=2 -->
<ColorStop index="0">
<StopOffset value="0.0"/>
<PaletteIndex value="0"/>
<Alpha value="0.8"/>
</ColorStop>
<ColorStop index="1">
<StopOffset value="0.9"/>
<PaletteIndex value="0"/>
<Alpha value="0.0"/>
</ColorStop>
</ColorLine>
<x0 value="21"/>
<y0 value="46"/>
<r0 value="0"/>
<x1 value="21"/>
<y1 value="46"/>
<r1 value="15"/>
</Paint>
<scaleX value="1.0"/>
<scaleY value="0.94995"/>
<Paint index="1" Format="10"><!-- PaintGlyph -->
<Paint Format="16"><!-- PaintScale -->
<Paint Format="6"><!-- PaintRadialGradient -->
<ColorLine>
<Extend value="pad"/>
<!-- StopCount=2 -->
<ColorStop index="0">
<StopOffset value="0.0"/>
<PaletteIndex value="0"/>
<Alpha value="0.8"/>
</ColorStop>
<ColorStop index="1">
<StopOffset value="0.9"/>
<PaletteIndex value="0"/>
<Alpha value="0.0"/>
</ColorStop>
</ColorLine>
<x0 value="39"/>
<y0 value="76"/>
<r0 value="0"/>
<x1 value="39"/>
<y1 value="76"/>
<r1 value="7"/>
</Paint>
<Glyph value="e000.0"/>
<scaleX value="1.0"/>
<scaleY value="0.94995"/>
</Paint>
<Transform>
<xx value="0.5"/>
<yx value="0.0"/>
<xy value="0.0"/>
<yy value="0.5"/>
<dx value="28.711"/>
<dy value="50.0"/>
</Transform>
<Glyph value="e000.1"/>
</Paint>
</LayerList>
<ClipList Format="1">
Expand Down

0 comments on commit b811c51

Please sign in to comment.