xref: /aosp_15_r20/external/fonttools/Tests/svgLib/path/shapes_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.svgLib.path import shapes
2*e1fe3e4aSElliott Hughesfrom fontTools.misc import etree
3*e1fe3e4aSElliott Hughesimport pytest
4*e1fe3e4aSElliott Hughes
5*e1fe3e4aSElliott Hughes
6*e1fe3e4aSElliott Hughes@pytest.mark.parametrize(
7*e1fe3e4aSElliott Hughes    "svg_xml, expected_path, expected_transform",
8*e1fe3e4aSElliott Hughes    [
9*e1fe3e4aSElliott Hughes        # path: direct passthrough
10*e1fe3e4aSElliott Hughes        ("<path d='I love kittens'/>", "I love kittens", None),
11*e1fe3e4aSElliott Hughes        # path no @d
12*e1fe3e4aSElliott Hughes        ("<path duck='Mallard'/>", None, None),
13*e1fe3e4aSElliott Hughes        # line
14*e1fe3e4aSElliott Hughes        ('<line x1="10" x2="50" y1="110" y2="150"/>', "M10,110 L50,150", None),
15*e1fe3e4aSElliott Hughes        # line, decimal positioning
16*e1fe3e4aSElliott Hughes        (
17*e1fe3e4aSElliott Hughes            '<line x1="10.0" x2="50.5" y1="110.2" y2="150.7"/>',
18*e1fe3e4aSElliott Hughes            "M10,110.2 L50.5,150.7",
19*e1fe3e4aSElliott Hughes            None,
20*e1fe3e4aSElliott Hughes        ),
21*e1fe3e4aSElliott Hughes        # rect: minimal valid example
22*e1fe3e4aSElliott Hughes        ("<rect width='1' height='1'/>", "M0,0 H1 V1 H0 V0 z", None),
23*e1fe3e4aSElliott Hughes        # rect: sharp corners
24*e1fe3e4aSElliott Hughes        (
25*e1fe3e4aSElliott Hughes            "<rect x='10' y='11' width='17' height='11'/>",
26*e1fe3e4aSElliott Hughes            "M10,11 H27 V22 H10 V11 z",
27*e1fe3e4aSElliott Hughes            None,
28*e1fe3e4aSElliott Hughes        ),
29*e1fe3e4aSElliott Hughes        # rect: round corners
30*e1fe3e4aSElliott Hughes        (
31*e1fe3e4aSElliott Hughes            "<rect x='9' y='9' width='11' height='7' rx='2'/>",
32*e1fe3e4aSElliott Hughes            "M11,9 H18 A2,2 0 0 1 20,11 V14 A2,2 0 0 1 18,16 H11"
33*e1fe3e4aSElliott Hughes            " A2,2 0 0 1 9,14 V11 A2,2 0 0 1 11,9 z",
34*e1fe3e4aSElliott Hughes            None,
35*e1fe3e4aSElliott Hughes        ),
36*e1fe3e4aSElliott Hughes        # rect: simple
37*e1fe3e4aSElliott Hughes        (
38*e1fe3e4aSElliott Hughes            "<rect x='11.5' y='16' width='11' height='2'/>",
39*e1fe3e4aSElliott Hughes            "M11.5,16 H22.5 V18 H11.5 V16 z",
40*e1fe3e4aSElliott Hughes            None,
41*e1fe3e4aSElliott Hughes        ),
42*e1fe3e4aSElliott Hughes        # rect: the one above plus a rotation
43*e1fe3e4aSElliott Hughes        (
44*e1fe3e4aSElliott Hughes            "<rect x='11.5' y='16' transform='matrix(0.7071 -0.7071 0.7071 0.7071 -7.0416 16.9999)' width='11' height='2'/>",
45*e1fe3e4aSElliott Hughes            "M11.5,16 H22.5 V18 H11.5 V16 z",
46*e1fe3e4aSElliott Hughes            (0.7071, -0.7071, 0.7071, 0.7071, -7.0416, 16.9999),
47*e1fe3e4aSElliott Hughes        ),
48*e1fe3e4aSElliott Hughes        # polygon
49*e1fe3e4aSElliott Hughes        ("<polygon points='30,10 50,30 10,30'/>", "M30,10 50,30 10,30 z", None),
50*e1fe3e4aSElliott Hughes        # polyline
51*e1fe3e4aSElliott Hughes        ("<polyline points='30,10 50,30 10,30'/>", "M30,10 50,30 10,30", None),
52*e1fe3e4aSElliott Hughes        # circle, minimal valid example
53*e1fe3e4aSElliott Hughes        ("<circle r='1'/>", "M-1,0 A1,1 0 1 1 1,0 A1,1 0 1 1 -1,0", None),
54*e1fe3e4aSElliott Hughes        # circle
55*e1fe3e4aSElliott Hughes        (
56*e1fe3e4aSElliott Hughes            "<circle cx='600' cy='200' r='100'/>",
57*e1fe3e4aSElliott Hughes            "M500,200 A100,100 0 1 1 700,200 A100,100 0 1 1 500,200",
58*e1fe3e4aSElliott Hughes            None,
59*e1fe3e4aSElliott Hughes        ),
60*e1fe3e4aSElliott Hughes        # circle, decimal positioning
61*e1fe3e4aSElliott Hughes        (
62*e1fe3e4aSElliott Hughes            "<circle cx='12' cy='6.5' r='1.5'></circle>",
63*e1fe3e4aSElliott Hughes            "M10.5,6.5 A1.5,1.5 0 1 1 13.5,6.5 A1.5,1.5 0 1 1 10.5,6.5",
64*e1fe3e4aSElliott Hughes            None,
65*e1fe3e4aSElliott Hughes        ),
66*e1fe3e4aSElliott Hughes        # circle, with transform
67*e1fe3e4aSElliott Hughes        (
68*e1fe3e4aSElliott Hughes            '<circle transform="matrix(0.9871 -0.1602 0.1602 0.9871 -7.525 8.6516)" cx="49.9" cy="51" r="14.3"/>',
69*e1fe3e4aSElliott Hughes            "M35.6,51 A14.3,14.3 0 1 1 64.2,51 A14.3,14.3 0 1 1 35.6,51",
70*e1fe3e4aSElliott Hughes            (0.9871, -0.1602, 0.1602, 0.9871, -7.525, 8.6516),
71*e1fe3e4aSElliott Hughes        ),
72*e1fe3e4aSElliott Hughes        # ellipse
73*e1fe3e4aSElliott Hughes        (
74*e1fe3e4aSElliott Hughes            '<ellipse cx="100" cy="50" rx="100" ry="50"/>',
75*e1fe3e4aSElliott Hughes            "M0,50 A100,50 0 1 1 200,50 A100,50 0 1 1 0,50",
76*e1fe3e4aSElliott Hughes            None,
77*e1fe3e4aSElliott Hughes        ),
78*e1fe3e4aSElliott Hughes        # ellipse, decimal positioning
79*e1fe3e4aSElliott Hughes        (
80*e1fe3e4aSElliott Hughes            '<ellipse cx="100.5" cy="50" rx="10" ry="50.5"/>',
81*e1fe3e4aSElliott Hughes            "M90.5,50 A10,50.5 0 1 1 110.5,50 A10,50.5 0 1 1 90.5,50",
82*e1fe3e4aSElliott Hughes            None,
83*e1fe3e4aSElliott Hughes        ),
84*e1fe3e4aSElliott Hughes        # ellipse, with transform
85*e1fe3e4aSElliott Hughes        (
86*e1fe3e4aSElliott Hughes            '<ellipse transform="matrix(0.9557 -0.2945 0.2945 0.9557 -14.7694 20.1454)" cx="59.5" cy="59.1" rx="30.9" ry="11.9"/>',
87*e1fe3e4aSElliott Hughes            "M28.6,59.1 A30.9,11.9 0 1 1 90.4,59.1 A30.9,11.9 0 1 1 28.6,59.1",
88*e1fe3e4aSElliott Hughes            (0.9557, -0.2945, 0.2945, 0.9557, -14.7694, 20.1454),
89*e1fe3e4aSElliott Hughes        ),
90*e1fe3e4aSElliott Hughes    ],
91*e1fe3e4aSElliott Hughes)
92*e1fe3e4aSElliott Hughesdef test_el_to_path(svg_xml, expected_path, expected_transform):
93*e1fe3e4aSElliott Hughes    pb = shapes.PathBuilder()
94*e1fe3e4aSElliott Hughes    pb.add_path_from_element(etree.fromstring(svg_xml))
95*e1fe3e4aSElliott Hughes    if expected_path:
96*e1fe3e4aSElliott Hughes        expected_paths = [expected_path]
97*e1fe3e4aSElliott Hughes        expected_transforms = [expected_transform]
98*e1fe3e4aSElliott Hughes    else:
99*e1fe3e4aSElliott Hughes        expected_paths = []
100*e1fe3e4aSElliott Hughes        expected_transforms = []
101*e1fe3e4aSElliott Hughes    assert pb.paths == expected_paths
102*e1fe3e4aSElliott Hughes    assert pb.transforms == expected_transforms
103