def test_insertGlyph(): g = Glyph() pen = g.getPen() pen.moveTo((0, 0)) pen.lineTo((1, 1)) pen.lineTo((0, 1)) pen.closePath() layer = Layer() layer.insertGlyph(g, "a") assert "a" in layer assert layer["a"].name == "a" assert layer["a"].contours == g.contours assert layer["a"] is not g layer.insertGlyph(g, "b") assert "b" in layer assert layer["b"].name == "b" assert layer["b"].contours == layer["a"].contours assert layer["b"] is not layer["a"] assert layer["b"] is not g assert g.name is None with pytest.raises(KeyError, match="glyph named 'a' already exists"): layer.insertGlyph(g, "a", overwrite=False) with pytest.raises(ValueError, match=".*Glyph .* has no name; can't add it"): layer.insertGlyph(g)
def test_newGlyph(): layer = Layer() a = layer.newGlyph("a") assert "a" in layer assert layer["a"] is a with pytest.raises(KeyError, match="glyph named 'a' already exists"): layer.newGlyph("a")
def test_change_default_layer_invalid() -> None: font = Font(layers=[Layer(), Layer("background")]) with pytest.raises( ValueError, match= "there's already a layer named 'public.default' which must stay default", ): font.layers.defaultLayer = font.layers["background"] with pytest.raises( ValueError, match="Layer .* not found in layer set; can't set as default"): font.layers.defaultLayer = Layer("foobar")
def test_custom_layerset(): default = Layer() ls1 = LayerSet.from_iterable([default]) assert next(iter(ls1)) is ls1.defaultLayer with pytest.raises(ValueError): ls1 = LayerSet.from_iterable([Layer(name="abc")]) ls2 = LayerSet.from_iterable([Layer(name="abc")], defaultLayerName="abc") assert ls2["abc"] is ls2.defaultLayer layers2 = OrderedDict() layers2["public.default"] = default LayerSet(layers=layers2, defaultLayer=default)
def test_glyph_get_bounds(): a = Glyph("a") pen = a.getPen() pen.moveTo((0, 0)) pen.curveTo((10, 10), (10, 20), (0, 20)) pen.closePath() b = Glyph("b", components=[Component("a", (1, 0, 0, 1, -50, 100))]) layer = Layer(glyphs=[a, b]) assert a.getBounds(layer) == BoundingBox(xMin=0, yMin=0, xMax=7.5, yMax=20) assert a.getControlBounds(layer) == BoundingBox(xMin=0, yMin=0, xMax=10, yMax=20) with pytest.raises( TypeError, match="layer is required to compute bounds of components"): b.getBounds() with pytest.raises( TypeError, match="layer is required to compute bounds of components"): b.getControlBounds() assert b.getBounds(layer) == (-50, 100, -42.5, 120 ) # namedtuple is a tuple assert b.getControlBounds(layer) == (-50, 100, -40, 120)
def test_renameGlyph() -> None: g = Glyph() layer = Layer(glyphs={"a": g}) assert g.name == "a" layer.renameGlyph("a", "a") # no-op assert g.name == "a" layer.renameGlyph("a", "b") assert g.name == "b" layer.insertGlyph(g, "a") with pytest.raises(KeyError, match="target glyph named 'a' already exists"): layer.renameGlyph("b", "a")
def layer() -> Layer: a = Glyph("a") pen = a.getPen() pen.moveTo((0, 0)) pen.curveTo((10, 10), (10, 20), (0, 20)) pen.closePath() layer = Layer(glyphs=[a]) return layer
def test_init_layer_with_glyphs_list(): a = Glyph("a") b = Glyph("b") layer = Layer(glyphs=[a, b]) assert layer["a"] is a assert layer["b"] is b with pytest.raises(KeyError, match=".*Glyph .* can't be added twice"): Layer(glyphs=[a, a]) c = Glyph() with pytest.raises(ValueError, match=".*Glyph .* has no name"): Layer(glyphs=[c]) with pytest.raises(KeyError, match="glyph named 'b' already exists"): Layer(glyphs=[a, b, Glyph("b")]) with pytest.raises(TypeError, match="Expected Glyph, found int"): Layer(glyphs=[1])
def test_change_default_layer() -> None: font = Font(layers=[Layer("foo", default=True), Layer("bar")]) assert font.layers.defaultLayer is font.layers["foo"] assert font.layers["foo"].default assert not font.layers["bar"].default font.layers.defaultLayer = font.layers["bar"] assert font.layers.defaultLayer is font.layers["bar"] assert not font.layers["foo"].default assert font.layers["bar"].default # type: ignore font.newLayer("baz", default=True) assert font.layers.defaultLayer is font.layers["baz"] assert font.layers["baz"].default assert not font.layers["foo"].default assert not font.layers["bar"].default font.renameLayer("foo", "public.default") assert "foo" not in font.layers assert font.layers.defaultLayer is font.layers["public.default"] assert font.layers["public.default"].default assert not font.layers["baz"].default
def test_init_layer_with_glyphs_dict(): a = Glyph() b = Glyph() layer = Layer("My Layer", {"a": a, "b": b}) assert layer.name == "My Layer" assert "a" in layer assert layer["a"] is a assert a.name == "a" assert "b" in layer assert layer["b"] is b assert b.name == "b" with pytest.raises( ValueError, match="glyph has incorrect name: expected 'a', found 'b'"): Layer(glyphs={"a": b}) with pytest.raises(KeyError, match=".*Glyph .* can't be added twice"): Layer(glyphs={"a": a, "b": a}) with pytest.raises(TypeError, match="Expected Glyph, found int"): Layer(glyphs={"a": 1})
def layer() -> Layer: a = Glyph("a") pen = a.getPen() pen.moveTo((8, 0)) pen.lineTo((18, 0)) pen.lineTo((18, 20)) pen.lineTo((8, 20)) pen.closePath() a.width = 30 a.appendAnchor({"x": 10, "y": 30, "name": "top"}) b = Glyph("b", width=a.width, components=[Component("a", (1, 0, 0, 1, 2, -5))]) layer = Layer(glyphs=[a, b]) return layer
def test_addGlyph(): a = Glyph("a") layer = Layer() layer.addGlyph(a) assert "a" in layer assert layer["a"] is a with pytest.raises(KeyError, match="glyph named 'a' already exists"): layer.addGlyph(a)
"values": [ { "language": "en", "text": "An example of WOFF packaging", }, { "language": "fr", "text": "Un exemple de l'empaquetage de WOFF", }, ], }], }], }, ), # 'public.default' is a special case, default=True by definition (Layer(), { "name": "public.default" }), (Layer("foo", default=True), { "name": "foo", "default": True }), (Layer("bar"), { "name": "bar" }), ( Layer( name="foreground", glyphs={ "a": Glyph(), "b": Glyph()
def test_custom_layerset() -> None: default = Layer() ls1 = LayerSet.from_iterable([default]) assert next(iter(ls1)) is ls1.defaultLayer with pytest.raises(ValueError, match="no layer marked as default"): LayerSet.from_iterable([Layer(name="abc")]) with pytest.raises(ValueError, match="no layer marked as default"): LayerSet({"abc": Layer(name="abc")}) with pytest.raises(ValueError, match="more than one layer marked as default"): LayerSet.from_iterable( [Layer("a", default=True), Layer("b", default=True)]) with pytest.raises(ValueError, match="more than one layer marked as default"): LayerSet({ "a": Layer("a", default=True), "b": Layer("b", default=True) }) with pytest.raises(KeyError, match="duplicate layer name"): LayerSet.from_iterable([Layer("a", default=True), Layer("a")]) with pytest.raises(ValueError, match="default layer .* must be in layer set"): LayerSet(layers={"public.default": Layer()}, defaultLayer=Layer()) # defaultLayerName is deprecated but still works for now ls2 = LayerSet.from_iterable([Layer(name="abc")], defaultLayerName="abc") assert ls2["abc"].default assert ls2["abc"] is ls2.defaultLayer # defaultLayer is set automatically based on Layer.default attribute ls3 = LayerSet.from_iterable( [Layer(name="abc", default=True), Layer("def")]) assert ls3["abc"].default assert ls3["abc"] is ls3.defaultLayer assert not ls3["def"].default # also for the default constructor, defaultLayer is guessed from Layer.default ls4 = LayerSet(layers={"foreground": Layer("foreground", default=True)}) assert ls4["foreground"] is ls4.defaultLayer # unless defaultLayer parameter is set explicitly defaultLayer = Layer("foreground", default=True) ls5 = LayerSet(layers={"foreground": defaultLayer}, defaultLayer=defaultLayer) assert ls5["foreground"] is ls5.defaultLayer assert ls5.defaultLayer is defaultLayer