Example #1
0
def test_svg_reuse(s1, s2, expected_affine):
    # if we can get an affine we should normalize to same shape
    if expected_affine:
        assert normalize(s1) == normalize(s2)
    else:
        assert normalize(s1) != normalize(s2)

    assert affine_between(s1, s2) == expected_affine
Example #2
0
    def try_reuse(self, path: str) -> Optional[ReuseResult]:
        """Try to reproduce path as the transformation of another glyph.

        Path is expected to be in font units.

        Returns (glyph name, transform) if possible, None if not.
        """
        assert (
            not path in self._known_glyphs
        ), f"{path} isn't a path, it's a glyph name we've seen before"
        assert path.startswith("M"), f"{path} doesn't look like a path"

        if self._reuse_tolerance == -1:
            return None

        norm_path = normalize(SVGPath(d=path), self._normalize_tolerance).d
        if norm_path not in self._reusable_paths:
            return None

        glyph_name, glyph_path = self._reusable_paths[norm_path]
        affine = affine_between(
            SVGPath(d=glyph_path), SVGPath(d=path), self._reuse_tolerance
        )
        if affine is None:
            logging.warning("affine_between failed: %s %s ", glyph_path, path)
            return None

        # https://github.com/googlefonts/nanoemoji/issues/313 avoid out of bounds affines
        if not fixed_safe(*affine):
            logging.warning(
                "affine_between overflows Fixed: %s %s, %s", glyph_path, path, affine
            )
            return None

        return ReuseResult(glyph_name, affine)
Example #3
0
def test_svg_reuse(s1, s2, expected_affine, tolerance):
    # if we can get an affine we should normalize to same shape
    if expected_affine:
        assert normalize(s1, tolerance) == normalize(s2, tolerance)
    else:
        assert normalize(s1, tolerance) != normalize(s2, tolerance)

    affine = affine_between(s1, s2, tolerance)
    if expected_affine:
        assert (
            affine
        ), f"No affine found between {s1.d} and {s2.d}. Expected {expected_affine}"
        # Round because we've seen issues with different test environments when overly fine
        affine = affine.round(4)
    assert (affine == expected_affine
            ), f"Unexpected affine found between {s1.d} and {s2.d}."
Example #4
0
 def add_glyph(self, glyph_name, glyph_path):
     assert glyph_path.startswith("M"), f"{glyph_path} doesn't look like a path"
     if self._reuse_tolerance != -1:
         norm_path = normalize(SVGPath(d=glyph_path), self._normalize_tolerance).d
     else:
         norm_path = glyph_path
     self._reusable_paths[norm_path] = (glyph_name, glyph_path)
     self._known_glyphs.add(glyph_name)
Example #5
0
    def _in_glyph_reuse_key(self, shape: SVGPath) -> Tuple[Paint, SVGPath]:
        """Within a glyph reuse shapes only when painted consistently.

        paint+normalized shape ensures this."""
        return (self._paint(shape), normalize(shape))