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