Esempio n. 1
0
    def appendAnchor(self, anchor: Anchor | Mapping[str, Any]) -> None:
        """Appends an :class:`.Anchor` object to glyph's list of anchors.

        Args:
            anchor: An :class:`.Anchor` object or mapping for the Anchor constructor.
        """
        if not isinstance(anchor, Anchor):
            if not isinstance(anchor, Mapping):
                raise TypeError(
                    "Expected Anchor object or a Mapping for the ",
                    f"Anchor constructor, found {type(anchor).__name__}",
                )
            anchor = Anchor(**anchor)
        self.anchors.append(anchor)
Esempio n. 2
0
 def appendAnchor(self, anchor):
     if not isinstance(anchor, Anchor):
         anchor = Anchor(**anchor)
     self.anchors.append(anchor)
Esempio n. 3
0
def test_swap_glyph_names(data_dir):
    ufo = ufoLib2.Font.open(data_dir / "SwapGlyphNames" / "A.ufo")

    fontmake.instantiator.swap_glyph_names(ufo, "a", "a.swap")

    # Test swapped outlines.
    assert ufo["a"].unicode == 0x61
    assert len(ufo["a"]) == 1
    assert len(ufo["a"].contours[0]) == 8
    assert ufo["a"].width == 666
    assert ufo["a.swap"].unicode is None
    assert len(ufo["a.swap"]) == 1
    assert len(ufo["a.swap"].contours[0]) == 4
    assert ufo["a.swap"].width == 600

    # Test swapped components.
    assert sorted(c.baseGlyph for c in ufo["aaa"].components) == [
        "a.swap",
        "a.swap",
        "x",
    ]
    assert sorted(c.baseGlyph
                  for c in ufo["aaa.swap"].components) == ["a", "a", "y"]

    # Test swapped anchors.
    assert ufo["a"].anchors == [
        Anchor(x=153, y=0, name="bottom"),
        Anchor(x=153, y=316, name="top"),
    ]
    assert ufo["a.swap"].anchors == [
        Anchor(x=351, y=0, name="bottom"),
        Anchor(x=351, y=613, name="top"),
    ]

    # Test swapped glyph kerning.
    assert ufo.kerning == {
        ("public.kern1.a", "x"): 10,
        ("public.kern1.aswap", "x"): 20,
        ("a", "y"): 40,
        ("a.swap", "y"): 30,
        ("y", "a"): 60,
        ("y", "a.swap"): 50,
    }

    # Test swapped group membership.
    assert ufo.groups == {
        "public.kern1.a": ["a.swap"],
        "public.kern1.aswap": ["a"],
        "public.kern2.a": ["a.swap", "a"],
    }

    # Swap a second time.
    fontmake.instantiator.swap_glyph_names(ufo, "aaa", "aaa.swap")

    # Test swapped glyphs.
    assert sorted(c.baseGlyph
                  for c in ufo["aaa"].components) == ["a", "a", "y"]
    assert sorted(c.baseGlyph for c in ufo["aaa.swap"].components) == [
        "a.swap",
        "a.swap",
        "x",
    ]

    # Test for no leftover temporary glyphs.
    assert {g.name
            for g in ufo} == {
                "space",
                "a",
                "a.swap",
                "aaa",
                "aaa.swap",
                "x",
                "y",
            }

    with pytest.raises(fontmake.instantiator.InstantiatorError,
                       match="Cannot swap"):
        fontmake.instantiator.swap_glyph_names(ufo, "aaa", "aaa.swapa")