def test_object_lib_prune(tmp_path: Path) -> None: ufo = Font() ufo.info.guidelines = [Guideline(x=100), Guideline(y=200)] _ = ufo.objectLib(ufo.info.guidelines[0]) guideline_lib = ufo.objectLib(ufo.info.guidelines[1]) guideline_lib["com.test.foo"] = 1234 ufo.lib["public.objectLibs"]["aaaa"] = {"1": 1} ufo.newGlyph("component") glyph = ufo.newGlyph("test") glyph.guidelines = [Guideline(x=300), Guideline(y=400)] _ = glyph.objectLib(glyph.guidelines[0]) glyph_guideline_lib = glyph.objectLib(glyph.guidelines[1]) glyph_guideline_lib["com.test.foo"] = 4321 glyph.lib["public.objectLibs"]["aaaa"] = {"1": 1} ufo.save(tmp_path / "test.ufo") # Roundtrip ufo_reload = Font.open(tmp_path / "test.ufo") assert set(ufo_reload.lib["public.objectLibs"].keys()) == { ufo.info.guidelines[1].identifier } assert set(ufo_reload["test"].lib["public.objectLibs"].keys()) == { glyph.guidelines[1].identifier } # Empty object libs are pruned from objectLibs, but the identifiers stay. assert ufo.info.guidelines[0].identifier is not None assert glyph.guidelines[0].identifier is not None
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
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)]
def test_font_defcon_behavior(ufo_UbuTestData): font = ufo_UbuTestData font.newGlyph("b") assert "b" in font glyph = Glyph("c") font.addGlyph(glyph) assert font["c"] is glyph font.renameGlyph("c", "d") assert font["d"] is glyph assert font["d"].name == "d" guideline = Guideline(x=1) font.appendGuideline(guideline) assert font.info.guidelines[-1] is guideline font.appendGuideline({"y": 1, "name": "asdf"}) assert font.info.guidelines[-1].name == "asdf" font.newLayer("abc") assert "abc" in font.layers font.renameLayer("abc", "def") assert "abc" not in font.layers assert "def" in font.layers
"foo": { "bar": { "data": "YmF6", "type": DATA_LIB_KEY }, }, "oof": [ { "data": "cmFi", "type": DATA_LIB_KEY }, "zab", ], }, ), (Guideline(x=0, name="foo"), { "x": 0, "name": "foo" }), (Guideline(y=1, name="bar"), { "y": 1, "name": "bar" }), ( Guideline(x=1, y=2, angle=45.0, name="baz", color="0,1,0.5,1", identifier="0001"), {
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)