Esempio n. 1
0
def test_object_lib_roundtrip(tmp_path):
    ufo = Font()

    ufo.info.guidelines = [Guideline(x=100), Guideline(y=200)]
    guideline_lib = ufo.objectLib(ufo.info.guidelines[1])
    guideline_lib["com.test.foo"] = 1234

    ufo.newGlyph("component")
    glyph = ufo.newGlyph("test")

    glyph.guidelines = [Guideline(x=300), Guideline(y=400)]
    glyph_guideline_lib = glyph.objectLib(glyph.guidelines[1])
    glyph_guideline_lib["com.test.foo"] = 4321

    glyph.anchors = [Anchor(x=1, y=2, name="top"), Anchor(x=3, y=4, name="bottom")]
    anchor_lib = glyph.objectLib(glyph.anchors[1])
    anchor_lib["com.test.anchorTool"] = True

    pen = glyph.getPen()
    pen.moveTo((0, 0))
    pen.lineTo((100, 200))
    pen.lineTo((200, 400))
    pen.closePath()
    pen.moveTo((1000, 1000))
    pen.lineTo((1000, 2000))
    pen.lineTo((2000, 4000))
    pen.closePath()
    pen.addComponent("component", (1, 0, 0, 1, 0, 0))
    pen.addComponent("component", (1, 0, 0, 1, 0, 0))

    contour_lib = glyph.objectLib(glyph.contours[0])
    contour_lib["com.test.foo"] = "abc"
    point_lib = glyph.objectLib(glyph.contours[1].points[0])
    point_lib["com.test.foo"] = "abc"
    component_lib = glyph.objectLib(glyph.components[0])
    component_lib["com.test.foo"] = "abc"

    ufo.save(tmp_path / "test.ufo")

    # Roundtrip
    ufo_reload = Font.open(tmp_path / "test.ufo")

    reload_guideline_lib = ufo_reload.objectLib(ufo_reload.info.guidelines[1])
    reload_glyph = ufo_reload["test"]
    reload_glyph_guideline_lib = reload_glyph.objectLib(reload_glyph.guidelines[1])
    reload_anchor_lib = reload_glyph.objectLib(reload_glyph.anchors[1])
    reload_contour_lib = reload_glyph.objectLib(reload_glyph.contours[0])
    reload_point_lib = reload_glyph.objectLib(reload_glyph.contours[1].points[0])
    reload_component_lib = reload_glyph.objectLib(reload_glyph.components[0])

    assert reload_guideline_lib == guideline_lib
    assert reload_glyph_guideline_lib == glyph_guideline_lib
    assert reload_anchor_lib == anchor_lib
    assert reload_contour_lib == contour_lib
    assert reload_point_lib == point_lib
    assert reload_component_lib == component_lib
Esempio n. 2
0
def test_glyph_defcon_behavior():
    glyph = Glyph()
    glyph.appendAnchor(Anchor(1, 2, "top"))
    glyph.appendAnchor({"x": 3, "y": 4, "name": "bottom"})
    assert glyph.anchors == [Anchor(1, 2, "top"), Anchor(3, 4, "bottom")]

    glyph = Glyph()
    glyph.appendContour(Contour([Point(1, 2)]))
    assert glyph.contours == [Contour([Point(1, 2)])]

    glyph = Glyph()
    glyph.appendGuideline(Guideline(x=1))
    glyph.appendGuideline({"x": 2})
    assert glyph.guidelines == [Guideline(x=1), Guideline(x=2)]
Esempio n. 3
0
    WoffMetadataLicensee,
    WoffMetadataText,
    WoffMetadataTrademark,
    WoffMetadataUniqueID,
    WoffMetadataVendor,
)

# isort: off
cattr = pytest.importorskip("cattr")
from ufoLib2.converters import register_hooks, structure, unstructure  # noqa: E402


@pytest.mark.parametrize(
    "obj, expected",
    [
        (Anchor(0, 0), {
            "x": 0,
            "y": 0
        }),
        (
            Anchor(1, -2, "top", "1,0,0,1", "01234"),
            {
                "x": 1,
                "y": -2,
                "name": "top",
                "color": "1,0,0,1",
                "identifier": "01234"
            },
        ),
        (Point(0, 0), {
            "x": 0,
Esempio n. 4
0
def test_copyDataFromGlyph(ufo_UbuTestData):
    font = ufo_UbuTestData

    a = font["a"]
    a.height = 500
    a.image = Image("a.png")
    a.note = "a note"
    a.lib = {"bar": [3, 2, 1]}
    a.anchors = [Anchor(250, 0, "bottom")]
    a.guidelines = [Guideline(y=500)]
    a.components = [Component("A")]

    b = Glyph("b")
    b.width = 350
    b.height = 1000
    b.image = Image("b.png")
    b.note = "b note"
    b.lib = {"foo": [1, 2, 3]}
    b.anchors = [Anchor(350, 800, "top")]
    b.guidelines = [Guideline(x=50)]

    assert b.name != a.name
    assert b.width != a.width
    assert b.height != a.height
    assert b.unicodes != a.unicodes
    assert b.image != a.image
    assert b.note != a.note
    assert b.lib != a.lib
    assert b.anchors != a.anchors
    assert b.guidelines != a.guidelines
    assert b.contours != a.contours
    assert b.components != a.components

    def _assert_equal_but_distinct_objects(glyph1, glyph2):
        assert glyph1.width == glyph2.width
        assert glyph1.height == glyph2.height
        assert glyph1.unicodes == glyph2.unicodes
        assert glyph1.unicodes is not glyph2.unicodes
        assert glyph1.image == glyph2.image
        assert glyph1.image is not glyph2.image
        assert glyph1.note == glyph2.note
        assert glyph1.lib == glyph2.lib
        assert glyph1.lib is not glyph2.lib
        assert glyph1.lib["bar"] == glyph2.lib["bar"]
        assert glyph1.lib["bar"] is not glyph2.lib["bar"]
        assert glyph1.anchors == glyph2.anchors
        assert glyph1.anchors is not glyph2.anchors
        assert glyph1.anchors[0] is not glyph2.anchors[0]
        assert glyph1.guidelines == glyph2.guidelines
        assert glyph1.guidelines is not glyph2.guidelines
        assert glyph1.guidelines[0] is not glyph2.guidelines[0]
        assert glyph1.contours == glyph2.contours
        assert glyph1.contours is not glyph2.contours
        assert glyph1.contours[0] is not glyph2.contours[0]
        assert glyph1.components == glyph2.components
        assert glyph1.components is not glyph2.components
        assert glyph1.components[0] is not glyph2.components[0]

    b.copyDataFromGlyph(a)
    assert b.name != a.name
    _assert_equal_but_distinct_objects(b, a)

    c = a.copy()
    assert c.name == a.name
    _assert_equal_but_distinct_objects(c, a)

    d = a.copy(name="d")
    assert d.name == "d"
    _assert_equal_but_distinct_objects(d, a)