xref: /aosp_15_r20/external/fonttools/Tests/pens/cocoaPen_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesimport unittest
2*e1fe3e4aSElliott Hughes
3*e1fe3e4aSElliott Hughestry:
4*e1fe3e4aSElliott Hughes    from fontTools.pens.cocoaPen import CocoaPen
5*e1fe3e4aSElliott Hughes    from AppKit import NSBezierPathElementMoveTo, NSBezierPathElementLineTo
6*e1fe3e4aSElliott Hughes    from AppKit import NSBezierPathElementCurveTo, NSBezierPathElementClosePath
7*e1fe3e4aSElliott Hughes
8*e1fe3e4aSElliott Hughes    PATH_ELEMENTS = {
9*e1fe3e4aSElliott Hughes        # NSBezierPathElement key      desc
10*e1fe3e4aSElliott Hughes        NSBezierPathElementMoveTo: "moveto",
11*e1fe3e4aSElliott Hughes        NSBezierPathElementLineTo: "lineto",
12*e1fe3e4aSElliott Hughes        NSBezierPathElementCurveTo: "curveto",
13*e1fe3e4aSElliott Hughes        NSBezierPathElementClosePath: "close",
14*e1fe3e4aSElliott Hughes    }
15*e1fe3e4aSElliott Hughes
16*e1fe3e4aSElliott Hughes    PYOBJC_AVAILABLE = True
17*e1fe3e4aSElliott Hughesexcept ImportError:
18*e1fe3e4aSElliott Hughes    PYOBJC_AVAILABLE = False
19*e1fe3e4aSElliott Hughes
20*e1fe3e4aSElliott Hughes
21*e1fe3e4aSElliott Hughesdef draw(pen):
22*e1fe3e4aSElliott Hughes    pen.moveTo((50, 0))
23*e1fe3e4aSElliott Hughes    pen.lineTo((50, 500))
24*e1fe3e4aSElliott Hughes    pen.lineTo((200, 500))
25*e1fe3e4aSElliott Hughes    pen.curveTo((350, 500), (450, 400), (450, 250))
26*e1fe3e4aSElliott Hughes    pen.curveTo((450, 100), (350, 0), (200, 0))
27*e1fe3e4aSElliott Hughes    pen.closePath()
28*e1fe3e4aSElliott Hughes
29*e1fe3e4aSElliott Hughes
30*e1fe3e4aSElliott Hughesdef cocoaPathToString(path):
31*e1fe3e4aSElliott Hughes    num_elements = path.elementCount()
32*e1fe3e4aSElliott Hughes    output = []
33*e1fe3e4aSElliott Hughes    for i in range(num_elements - 1):
34*e1fe3e4aSElliott Hughes        elem_type, elem_points = path.elementAtIndex_associatedPoints_(i)
35*e1fe3e4aSElliott Hughes        elem_type = PATH_ELEMENTS[elem_type]
36*e1fe3e4aSElliott Hughes        path_points = " ".join([f"{p.x} {p.y}" for p in elem_points])
37*e1fe3e4aSElliott Hughes        output.append(f"{elem_type} {path_points}")
38*e1fe3e4aSElliott Hughes    return " ".join(output)
39*e1fe3e4aSElliott Hughes
40*e1fe3e4aSElliott Hughes
41*e1fe3e4aSElliott Hughes@unittest.skipUnless(PYOBJC_AVAILABLE, "pyobjc not installed")
42*e1fe3e4aSElliott Hughesclass CocoaPenTest(unittest.TestCase):
43*e1fe3e4aSElliott Hughes    def test_draw(self):
44*e1fe3e4aSElliott Hughes        pen = CocoaPen(None)
45*e1fe3e4aSElliott Hughes        draw(pen)
46*e1fe3e4aSElliott Hughes        self.assertEqual(
47*e1fe3e4aSElliott Hughes            "moveto 50.0 0.0 lineto 50.0 500.0 lineto 200.0 500.0 curveto 350.0 500.0 450.0 400.0 450.0 250.0 curveto 450.0 100.0 350.0 0.0 200.0 0.0 close ",
48*e1fe3e4aSElliott Hughes            cocoaPathToString(pen.path),
49*e1fe3e4aSElliott Hughes        )
50*e1fe3e4aSElliott Hughes
51*e1fe3e4aSElliott Hughes    def test_empty(self):
52*e1fe3e4aSElliott Hughes        pen = CocoaPen(None)
53*e1fe3e4aSElliott Hughes        self.assertEqual("", cocoaPathToString(pen.path))
54*e1fe3e4aSElliott Hughes
55*e1fe3e4aSElliott Hughes
56*e1fe3e4aSElliott Hughesif __name__ == "__main__":
57*e1fe3e4aSElliott Hughes    import sys
58*e1fe3e4aSElliott Hughes
59*e1fe3e4aSElliott Hughes    sys.exit(unittest.main())
60