Debian Codegraph @

tests/test_fontfeatures.py

raw
1 from fontFeatures import FontFeatures, Routine
2 from lxml import etree
3 import re
4
5
6 expected = """
7 <fontfeatures>
8 <namedclasses>
9 <class name="One">
10 <glyph>A</glyph>
11 <glyph>B</glyph>
12 <glyph>C</glyph>
13 </class>
14 <class name="Two">
15 <glyph>d</glyph>
16 <glyph>e</glyph>
17 <glyph>f</glyph>
18 </class>
19 </namedclasses>
20 <routines>
21 <routine name="One"/>
22 <routine name="Two"/>
23 </routines>
24 <features>
25 <feature name="onex">
26 <routinereference name="One"/>
27 </feature>
28 <feature name="twox">
29 <routinereference name="Two"/>
30 </feature>
31 </features>
32 <anchors>
33 <glyph name="a">
34 <anchor name="top" x="300" y="200"/>
35 </glyph>
36 </anchors>
37 <glyphclasses>
38 <glyph name="a" class="base"/>
39 <glyph name="d" class="mark"/>
40 </glyphclasses>
41 </fontfeatures>"""
42 expected = re.sub(r"(?m)^\s+", "", expected).replace("\n", "")
43
44
45 def test_add():
46 r1 = Routine(name="One")
47 r2 = Routine(name="Two")
48 f1 = FontFeatures()
49 f2 = FontFeatures()
50 f1.namedClasses["One"] = ["A", "B", "C"]
51 f1.glyphclasses["a"] = "base"
52 f1.addFeature("onex", [r1])
53 f2.namedClasses["Two"] = ["d", "e", "f"]
54 f2.addFeature("twox", [r2])
55 f2.glyphclasses["d"] = "mark"
56 f2.anchors["a"] = {"top": (300, 200)}
57
58 combined = f1 + f2
59 assert combined.namedClasses["One"] == ["A", "B", "C"]
60 assert combined.namedClasses["Two"] == ["d", "e", "f"]
61 assert "onex" in combined.features
62 assert combined.features["onex"][0].routine == r1
63 assert "twox" in combined.features
64 assert combined.features["twox"][0].routine == r2
65 assert combined.glyphclasses["a"] == "base"
66 assert combined.glyphclasses["d"] == "mark"
67
68 el = combined.toXML()
69 assert etree.tostring(el).decode() == expected
70
71
72 def test_fromXML():
73 ff = FontFeatures.fromXML(etree.fromstring(expected))
74 assert ff.namedClasses["One"] == ["A", "B", "C"]
75 assert ff.namedClasses["Two"] == ["d", "e", "f"]
76 assert "onex" in ff.features
77 assert ff.features["onex"][0].routine == ff.routines[0]
78 assert "twox" in ff.features
79 assert ff.features["twox"][0].routine == ff.routines[1]
80 assert ff.glyphclasses["a"] == "base"
81 assert ff.glyphclasses["d"] == "mark"
82 assert ff.anchors["a"] == {"top": (300, 200)}
83
84
85 def test_routine_named():
86 r1 = Routine(name="One")
87 f1 = FontFeatures()
88 f1.addFeature("onex", [r1])
89 assert f1.routineNamed("One") == r1