xref: /aosp_15_r20/external/fonttools/Tests/pens/hashPointPen_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.misc.transform import Identity
2*e1fe3e4aSElliott Hughesfrom fontTools.pens.hashPointPen import HashPointPen
3*e1fe3e4aSElliott Hughesimport pytest
4*e1fe3e4aSElliott Hughes
5*e1fe3e4aSElliott Hughes
6*e1fe3e4aSElliott Hughesclass _TestGlyph(object):
7*e1fe3e4aSElliott Hughes    width = 500
8*e1fe3e4aSElliott Hughes
9*e1fe3e4aSElliott Hughes    def drawPoints(self, pen):
10*e1fe3e4aSElliott Hughes        pen.beginPath(identifier="abc")
11*e1fe3e4aSElliott Hughes        pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000")
12*e1fe3e4aSElliott Hughes        pen.addPoint((10, 110), "line", False, None, identifier="0001")
13*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
14*e1fe3e4aSElliott Hughes        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
15*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
16*e1fe3e4aSElliott Hughes        pen.endPath()
17*e1fe3e4aSElliott Hughes
18*e1fe3e4aSElliott Hughes
19*e1fe3e4aSElliott Hughesclass _TestGlyph2(_TestGlyph):
20*e1fe3e4aSElliott Hughes    def drawPoints(self, pen):
21*e1fe3e4aSElliott Hughes        pen.beginPath(identifier="abc")
22*e1fe3e4aSElliott Hughes        pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000")
23*e1fe3e4aSElliott Hughes        # Minor difference to _TestGlyph() is in the next line:
24*e1fe3e4aSElliott Hughes        pen.addPoint((101, 10), "line", False, None, identifier="0001")
25*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
26*e1fe3e4aSElliott Hughes        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
27*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
28*e1fe3e4aSElliott Hughes        pen.endPath()
29*e1fe3e4aSElliott Hughes
30*e1fe3e4aSElliott Hughes
31*e1fe3e4aSElliott Hughesclass _TestGlyph3(_TestGlyph):
32*e1fe3e4aSElliott Hughes    def drawPoints(self, pen):
33*e1fe3e4aSElliott Hughes        pen.beginPath(identifier="abc")
34*e1fe3e4aSElliott Hughes        pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000")
35*e1fe3e4aSElliott Hughes        pen.addPoint((10, 110), "line", False, None, identifier="0001")
36*e1fe3e4aSElliott Hughes        pen.endPath()
37*e1fe3e4aSElliott Hughes        # Same segment, but in a different path:
38*e1fe3e4aSElliott Hughes        pen.beginPath(identifier="pth2")
39*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
40*e1fe3e4aSElliott Hughes        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
41*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
42*e1fe3e4aSElliott Hughes        pen.endPath()
43*e1fe3e4aSElliott Hughes
44*e1fe3e4aSElliott Hughes
45*e1fe3e4aSElliott Hughesclass _TestGlyph4(_TestGlyph):
46*e1fe3e4aSElliott Hughes    def drawPoints(self, pen):
47*e1fe3e4aSElliott Hughes        pen.beginPath(identifier="abc")
48*e1fe3e4aSElliott Hughes        pen.addPoint((0.0, 0.0), "move", False, "start", identifier="0000")
49*e1fe3e4aSElliott Hughes        pen.addPoint((10, 110), "line", False, None, identifier="0001")
50*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
51*e1fe3e4aSElliott Hughes        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
52*e1fe3e4aSElliott Hughes        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
53*e1fe3e4aSElliott Hughes        pen.endPath()
54*e1fe3e4aSElliott Hughes
55*e1fe3e4aSElliott Hughes
56*e1fe3e4aSElliott Hughesclass _TestGlyph5(_TestGlyph):
57*e1fe3e4aSElliott Hughes    def drawPoints(self, pen):
58*e1fe3e4aSElliott Hughes        pen.addComponent("b", Identity)
59*e1fe3e4aSElliott Hughes
60*e1fe3e4aSElliott Hughes
61*e1fe3e4aSElliott Hughesclass HashPointPenTest(object):
62*e1fe3e4aSElliott Hughes    def test_addComponent(self):
63*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width, {"a": _TestGlyph()})
64*e1fe3e4aSElliott Hughes        pen.addComponent("a", (2, 0, 0, 3, -10, 5))
65*e1fe3e4aSElliott Hughes        assert pen.hash == "w500[l0+0l10+110o50+75o60+50c50+0|(+2+0+0+3-10+5)]"
66*e1fe3e4aSElliott Hughes
67*e1fe3e4aSElliott Hughes    def test_NestedComponents(self):
68*e1fe3e4aSElliott Hughes        pen = HashPointPen(
69*e1fe3e4aSElliott Hughes            _TestGlyph().width, {"a": _TestGlyph5(), "b": _TestGlyph()}
70*e1fe3e4aSElliott Hughes        )  # "a" contains "b" as a component
71*e1fe3e4aSElliott Hughes        pen.addComponent("a", (2, 0, 0, 3, -10, 5))
72*e1fe3e4aSElliott Hughes
73*e1fe3e4aSElliott Hughes        assert (
74*e1fe3e4aSElliott Hughes            pen.hash
75*e1fe3e4aSElliott Hughes            == "w500[[l0+0l10+110o50+75o60+50c50+0|(+1+0+0+1+0+0)](+2+0+0+3-10+5)]"
76*e1fe3e4aSElliott Hughes        )
77*e1fe3e4aSElliott Hughes
78*e1fe3e4aSElliott Hughes    def test_outlineAndComponent(self):
79*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width, {"a": _TestGlyph()})
80*e1fe3e4aSElliott Hughes        glyph = _TestGlyph()
81*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen)
82*e1fe3e4aSElliott Hughes        pen.addComponent("a", (2, 0, 0, 2, -10, 5))
83*e1fe3e4aSElliott Hughes
84*e1fe3e4aSElliott Hughes        assert (
85*e1fe3e4aSElliott Hughes            pen.hash
86*e1fe3e4aSElliott Hughes            == "w500l0+0l10+110o50+75o60+50c50+0|[l0+0l10+110o50+75o60+50c50+0|(+2+0+0+2-10+5)]"
87*e1fe3e4aSElliott Hughes        )
88*e1fe3e4aSElliott Hughes
89*e1fe3e4aSElliott Hughes    def test_addComponent_missing_raises(self):
90*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width, dict())
91*e1fe3e4aSElliott Hughes        with pytest.raises(KeyError) as excinfo:
92*e1fe3e4aSElliott Hughes            pen.addComponent("a", Identity)
93*e1fe3e4aSElliott Hughes        assert excinfo.value.args[0] == "a"
94*e1fe3e4aSElliott Hughes
95*e1fe3e4aSElliott Hughes    def test_similarGlyphs(self):
96*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width)
97*e1fe3e4aSElliott Hughes        glyph = _TestGlyph()
98*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen)
99*e1fe3e4aSElliott Hughes
100*e1fe3e4aSElliott Hughes        pen2 = HashPointPen(_TestGlyph2().width)
101*e1fe3e4aSElliott Hughes        glyph = _TestGlyph2()
102*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen2)
103*e1fe3e4aSElliott Hughes
104*e1fe3e4aSElliott Hughes        assert pen.hash != pen2.hash
105*e1fe3e4aSElliott Hughes
106*e1fe3e4aSElliott Hughes    def test_similarGlyphs2(self):
107*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width)
108*e1fe3e4aSElliott Hughes        glyph = _TestGlyph()
109*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen)
110*e1fe3e4aSElliott Hughes
111*e1fe3e4aSElliott Hughes        pen2 = HashPointPen(_TestGlyph3().width)
112*e1fe3e4aSElliott Hughes        glyph = _TestGlyph3()
113*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen2)
114*e1fe3e4aSElliott Hughes
115*e1fe3e4aSElliott Hughes        assert pen.hash != pen2.hash
116*e1fe3e4aSElliott Hughes
117*e1fe3e4aSElliott Hughes    def test_similarGlyphs3(self):
118*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width)
119*e1fe3e4aSElliott Hughes        glyph = _TestGlyph()
120*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen)
121*e1fe3e4aSElliott Hughes
122*e1fe3e4aSElliott Hughes        pen2 = HashPointPen(_TestGlyph4().width)
123*e1fe3e4aSElliott Hughes        glyph = _TestGlyph4()
124*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen2)
125*e1fe3e4aSElliott Hughes
126*e1fe3e4aSElliott Hughes        assert pen.hash != pen2.hash
127*e1fe3e4aSElliott Hughes
128*e1fe3e4aSElliott Hughes    def test_glyphVsComposite(self):
129*e1fe3e4aSElliott Hughes        # If a glyph contains a component, the decomposed glyph should still
130*e1fe3e4aSElliott Hughes        # compare false
131*e1fe3e4aSElliott Hughes        pen = HashPointPen(_TestGlyph().width, {"a": _TestGlyph()})
132*e1fe3e4aSElliott Hughes        pen.addComponent("a", Identity)
133*e1fe3e4aSElliott Hughes
134*e1fe3e4aSElliott Hughes        pen2 = HashPointPen(_TestGlyph().width)
135*e1fe3e4aSElliott Hughes        glyph = _TestGlyph()
136*e1fe3e4aSElliott Hughes        glyph.drawPoints(pen2)
137*e1fe3e4aSElliott Hughes
138*e1fe3e4aSElliott Hughes        assert pen.hash != pen2.hash
139