File: hashPointPen_test.py

package info (click to toggle)
fonttools 4.61.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,588 kB
  • sloc: python: 145,091; xml: 103; makefile: 24
file content (138 lines) | stat: -rw-r--r-- 4,897 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from fontTools.misc.transform import Identity
from fontTools.pens.hashPointPen import HashPointPen
import pytest


class _TestGlyph(object):
    width = 500

    def drawPoints(self, pen):
        pen.beginPath(identifier="abc")
        pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000")
        pen.addPoint((10, 110), "line", False, None, identifier="0001")
        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
        pen.endPath()


class _TestGlyph2(_TestGlyph):
    def drawPoints(self, pen):
        pen.beginPath(identifier="abc")
        pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000")
        # Minor difference to _TestGlyph() is in the next line:
        pen.addPoint((101, 10), "line", False, None, identifier="0001")
        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
        pen.endPath()


class _TestGlyph3(_TestGlyph):
    def drawPoints(self, pen):
        pen.beginPath(identifier="abc")
        pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000")
        pen.addPoint((10, 110), "line", False, None, identifier="0001")
        pen.endPath()
        # Same segment, but in a different path:
        pen.beginPath(identifier="pth2")
        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
        pen.endPath()


class _TestGlyph4(_TestGlyph):
    def drawPoints(self, pen):
        pen.beginPath(identifier="abc")
        pen.addPoint((0.0, 0.0), "move", False, "start", identifier="0000")
        pen.addPoint((10, 110), "line", False, None, identifier="0001")
        pen.addPoint((50.0, 75.0), None, False, None, identifier="0002")
        pen.addPoint((60.0, 50.0), None, False, None, identifier="0003")
        pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004")
        pen.endPath()


class _TestGlyph5(_TestGlyph):
    def drawPoints(self, pen):
        pen.addComponent("b", Identity)


class HashPointPenTest(object):
    def test_addComponent(self):
        pen = HashPointPen(_TestGlyph().width, {"a": _TestGlyph()})
        pen.addComponent("a", (2, 0, 0, 3, -10, 5))
        assert pen.hash == "w500[l0+0l10+110o50+75o60+50c50+0|(+2+0+0+3-10+5)]"

    def test_NestedComponents(self):
        pen = HashPointPen(
            _TestGlyph().width, {"a": _TestGlyph5(), "b": _TestGlyph()}
        )  # "a" contains "b" as a component
        pen.addComponent("a", (2, 0, 0, 3, -10, 5))

        assert (
            pen.hash
            == "w500[[l0+0l10+110o50+75o60+50c50+0|(+1+0+0+1+0+0)](+2+0+0+3-10+5)]"
        )

    def test_outlineAndComponent(self):
        pen = HashPointPen(_TestGlyph().width, {"a": _TestGlyph()})
        glyph = _TestGlyph()
        glyph.drawPoints(pen)
        pen.addComponent("a", (2, 0, 0, 2, -10, 5))

        assert (
            pen.hash
            == "w500l0+0l10+110o50+75o60+50c50+0|[l0+0l10+110o50+75o60+50c50+0|(+2+0+0+2-10+5)]"
        )

    def test_addComponent_missing_raises(self):
        pen = HashPointPen(_TestGlyph().width, dict())
        with pytest.raises(KeyError) as excinfo:
            pen.addComponent("a", Identity)
        assert excinfo.value.args[0] == "a"

    def test_similarGlyphs(self):
        pen = HashPointPen(_TestGlyph().width)
        glyph = _TestGlyph()
        glyph.drawPoints(pen)

        pen2 = HashPointPen(_TestGlyph2().width)
        glyph = _TestGlyph2()
        glyph.drawPoints(pen2)

        assert pen.hash != pen2.hash

    def test_similarGlyphs2(self):
        pen = HashPointPen(_TestGlyph().width)
        glyph = _TestGlyph()
        glyph.drawPoints(pen)

        pen2 = HashPointPen(_TestGlyph3().width)
        glyph = _TestGlyph3()
        glyph.drawPoints(pen2)

        assert pen.hash != pen2.hash

    def test_similarGlyphs3(self):
        pen = HashPointPen(_TestGlyph().width)
        glyph = _TestGlyph()
        glyph.drawPoints(pen)

        pen2 = HashPointPen(_TestGlyph4().width)
        glyph = _TestGlyph4()
        glyph.drawPoints(pen2)

        assert pen.hash != pen2.hash

    def test_glyphVsComposite(self):
        # If a glyph contains a component, the decomposed glyph should still
        # compare false
        pen = HashPointPen(_TestGlyph().width, {"a": _TestGlyph()})
        pen.addComponent("a", Identity)

        pen2 = HashPointPen(_TestGlyph().width)
        glyph = _TestGlyph()
        glyph.drawPoints(pen2)

        assert pen.hash != pen2.hash