aboutsummaryrefslogtreecommitdiff
path: root/Tests/svgLib/path/shapes_test.py
blob: ee9ddeae67b2f45a17b648bb7da3b5befcea23d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import print_function, absolute_import, division

from fontTools.misc.py23 import *
from fontTools.pens.recordingPen import RecordingPen
from fontTools.svgLib.path import shapes
from fontTools.misc import etree
import pytest


@pytest.mark.parametrize(
    "svg_xml, expected_path",
    [
        # path: direct passthrough
        (
          "<path d='I love kittens'/>",
          "I love kittens"
        ),
        # path no @d
        (
          "<path duck='Mallard'/>",
          None
        ),
        # rect: minimal valid example
        (
            "<rect width='1' height='1'/>",
            "M0,0 H1 V1 H0 V0 z",
        ),
        # rect: sharp corners
        (
            "<rect x='10' y='11' width='17' height='11'/>",
            "M10,11 H27 V22 H10 V11 z",
        ),
        # rect: round corners
        (
            "<rect x='9' y='9' width='11' height='7' rx='2'/>",
            "M11,9 H18 A2,2 0 0 1 20,11 V14 A2,2 0 0 1 18,16 H11"
            " A2,2 0 0 1 9,14 V11 A2,2 0 0 1 11,9 z",
        ),
        # polygon
        (
            "<polygon points='30,10 50,30 10,30'/>",
            "M30,10 50,30 10,30 z"
        ),
        # circle, minimal valid example
        (
            "<circle r='1'/>",
            "M-1,0 A1,1 0 1 1 1,0 A1,1 0 1 1 -1,0"
        ),
        # circle
        (
            "<circle cx='600' cy='200' r='100'/>",
            "M500,200 A100,100 0 1 1 700,200 A100,100 0 1 1 500,200"
        ),
        # circle, decimal positioning
        (
            "<circle cx='12' cy='6.5' r='1.5'></circle>",
            "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"
        )
    ]
)
def test_el_to_path(svg_xml, expected_path):
    pb = shapes.PathBuilder()
    pb.add_path_from_element(etree.fromstring(svg_xml))
    if expected_path:
        expected = [expected_path]
    else:
        expected = []
    assert pb.paths == expected