xref: /aosp_15_r20/external/fonttools/Tests/pens/roundingPen_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1from fontTools.misc.fixedTools import floatToFixedToFloat
2from fontTools.pens.recordingPen import RecordingPen, RecordingPointPen
3from fontTools.pens.roundingPen import RoundingPen, RoundingPointPen
4from functools import partial
5
6
7tt_scale_round = partial(floatToFixedToFloat, precisionBits=14)
8
9
10class RoundingPenTest(object):
11    def test_general(self):
12        recpen = RecordingPen()
13        roundpen = RoundingPen(recpen)
14        roundpen.moveTo((0.4, 0.6))
15        roundpen.lineTo((1.6, 2.5))
16        roundpen.qCurveTo((2.4, 4.6), (3.3, 5.7), (4.9, 6.1))
17        roundpen.curveTo((6.4, 8.6), (7.3, 9.7), (8.9, 10.1))
18        roundpen.addComponent("a", (1.5, 0, 0, 1.5, 10.5, -10.5))
19        assert recpen.value == [
20            ("moveTo", ((0, 1),)),
21            ("lineTo", ((2, 3),)),
22            ("qCurveTo", ((2, 5), (3, 6), (5, 6))),
23            ("curveTo", ((6, 9), (7, 10), (9, 10))),
24            ("addComponent", ("a", (1.5, 0, 0, 1.5, 11, -10))),
25        ]
26
27    def test_transform_round(self):
28        recpen = RecordingPen()
29        roundpen = RoundingPen(recpen, transformRoundFunc=tt_scale_round)
30        # The 0.913 is equal to 91.3% scale in a source editor
31        roundpen.addComponent("a", (0.9130000305, 0, 0, -1, 10.5, -10.5))
32        # The value should compare equal to its F2Dot14 representation
33        assert recpen.value == [
34            ("addComponent", ("a", (0.91302490234375, 0, 0, -1, 11, -10))),
35        ]
36
37
38class RoundingPointPenTest(object):
39    def test_general(self):
40        recpen = RecordingPointPen()
41        roundpen = RoundingPointPen(recpen)
42        roundpen.beginPath()
43        roundpen.addPoint((0.4, 0.6), "line")
44        roundpen.addPoint((1.6, 2.5), "line")
45        roundpen.addPoint((2.4, 4.6))
46        roundpen.addPoint((3.3, 5.7))
47        roundpen.addPoint((4.9, 6.1), "qcurve")
48        roundpen.endPath()
49        roundpen.addComponent("a", (1.5, 0, 0, 1.5, 10.5, -10.5))
50        assert recpen.value == [
51            ("beginPath", (), {}),
52            ("addPoint", ((0, 1), "line", False, None), {}),
53            ("addPoint", ((2, 3), "line", False, None), {}),
54            ("addPoint", ((2, 5), None, False, None), {}),
55            ("addPoint", ((3, 6), None, False, None), {}),
56            ("addPoint", ((5, 6), "qcurve", False, None), {}),
57            ("endPath", (), {}),
58            ("addComponent", ("a", (1.5, 0, 0, 1.5, 11, -10)), {}),
59        ]
60
61    def test_transform_round(self):
62        recpen = RecordingPointPen()
63        roundpen = RoundingPointPen(recpen, transformRoundFunc=tt_scale_round)
64        # The 0.913 is equal to 91.3% scale in a source editor
65        roundpen.addComponent("a", (0.913, 0, 0, -1, 10.5, -10.5))
66        # The value should compare equal to its F2Dot14 representation
67        assert recpen.value == [
68            ("addComponent", ("a", (0.91302490234375, 0, 0, -1, 11, -10)), {}),
69        ]
70