fonttools/Tests/svgLib/path/shapes_test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3.7 KiB
Python
Raw Normal View History

2019-02-07 19:00:26 -08:00
from fontTools.svgLib.path import shapes
from fontTools.misc import etree
import pytest
@pytest.mark.parametrize(
2019-02-17 21:23:11 -08:00
"svg_xml, expected_path, expected_transform",
2019-02-07 19:00:26 -08:00
[
2019-02-08 11:37:00 -08:00
# path: direct passthrough
2019-02-17 21:23:11 -08:00
("<path d='I love kittens'/>", "I love kittens", None),
2019-02-08 11:37:00 -08:00
# path no @d
2019-02-17 21:23:11 -08:00
("<path duck='Mallard'/>", None, None),
2019-04-02 14:21:18 -07:00
# line
('<line x1="10" x2="50" y1="110" y2="150"/>', "M10,110 L50,150", None),
# line, decimal positioning
(
'<line x1="10.0" x2="50.5" y1="110.2" y2="150.7"/>',
"M10,110.2 L50.5,150.7",
None,
),
2019-02-08 11:37:00 -08:00
# rect: minimal valid example
2019-02-07 19:00:26 -08:00
("<rect width='1' height='1'/>", "M0,0 H1 V1 H0 V0 z", None),
2019-02-08 11:37:00 -08:00
# rect: sharp corners
2019-02-08 10:50:22 -08:00
(
"<rect x='10' y='11' width='17' height='11'/>",
"M10,11 H27 V22 H10 V11 z",
2019-02-17 21:23:11 -08:00
None,
2019-02-08 10:50:22 -08:00
),
2019-02-08 11:37:00 -08:00
# rect: round corners
2019-02-08 10:50:22 -08:00
(
"<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",
2019-02-17 21:23:11 -08:00
None,
),
# rect: simple
(
"<rect x='11.5' y='16' width='11' height='2'/>",
"M11.5,16 H22.5 V18 H11.5 V16 z",
None,
),
# rect: the one above plus a rotation
(
"<rect x='11.5' y='16' transform='matrix(0.7071 -0.7071 0.7071 0.7071 -7.0416 16.9999)' width='11' height='2'/>",
"M11.5,16 H22.5 V18 H11.5 V16 z",
(0.7071, -0.7071, 0.7071, 0.7071, -7.0416, 16.9999),
2019-02-08 11:37:00 -08:00
),
2019-02-08 13:11:33 -08:00
# polygon
("<polygon points='30,10 50,30 10,30'/>", "M30,10 50,30 10,30 z", None),
# polyline
("<polyline points='30,10 50,30 10,30'/>", "M30,10 50,30 10,30", None),
2019-02-08 13:11:33 -08:00
# circle, minimal valid example
2019-02-17 21:23:11 -08:00
("<circle r='1'/>", "M-1,0 A1,1 0 1 1 1,0 A1,1 0 1 1 -1,0", None),
2019-02-08 13:11:33 -08:00
# circle
(
"<circle cx='600' cy='200' r='100'/>",
2019-02-17 21:23:11 -08:00
"M500,200 A100,100 0 1 1 700,200 A100,100 0 1 1 500,200",
None,
2019-02-08 14:57:10 -08:00
),
# circle, decimal positioning
(
"<circle cx='12' cy='6.5' r='1.5'></circle>",
2019-02-17 21:23:11 -08:00
"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",
None,
2019-04-01 22:50:24 -07:00
),
# circle, with transform
(
'<circle transform="matrix(0.9871 -0.1602 0.1602 0.9871 -7.525 8.6516)" cx="49.9" cy="51" r="14.3"/>',
"M35.6,51 A14.3,14.3 0 1 1 64.2,51 A14.3,14.3 0 1 1 35.6,51",
(0.9871, -0.1602, 0.1602, 0.9871, -7.525, 8.6516),
),
2019-04-01 22:50:24 -07:00
# ellipse
(
'<ellipse cx="100" cy="50" rx="100" ry="50"/>',
2019-02-17 21:23:11 -08:00
"M0,50 A100,50 0 1 1 200,50 A100,50 0 1 1 0,50",
None,
2019-04-01 22:50:24 -07:00
),
# ellipse, decimal positioning
(
'<ellipse cx="100.5" cy="50" rx="10" ry="50.5"/>',
2019-02-17 21:23:11 -08:00
"M90.5,50 A10,50.5 0 1 1 110.5,50 A10,50.5 0 1 1 90.5,50",
None,
2019-04-01 22:50:24 -07:00
),
# ellipse, with transform
(
'<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"/>',
"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",
(0.9557, -0.2945, 0.2945, 0.9557, -14.7694, 20.1454),
),
2019-02-07 19:00:26 -08:00
],
)
2019-02-17 21:23:11 -08:00
def test_el_to_path(svg_xml, expected_path, expected_transform):
2019-02-07 19:00:26 -08:00
pb = shapes.PathBuilder()
2019-02-12 11:53:27 -08:00
pb.add_path_from_element(etree.fromstring(svg_xml))
2019-02-08 11:37:00 -08:00
if expected_path:
2019-02-17 21:23:11 -08:00
expected_paths = [expected_path]
expected_transforms = [expected_transform]
2019-02-08 11:37:00 -08:00
else:
2019-02-17 21:23:11 -08:00
expected_paths = []
expected_transforms = []
assert pb.paths == expected_paths
assert pb.transforms == expected_transforms