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