[svgLib] Complete support for matrix transforms

This commit is contained in:
Miguel Sousa 2019-04-02 00:30:09 -07:00
parent dedf14ac8a
commit d5adee46d9
3 changed files with 20 additions and 2 deletions

View File

@ -55,7 +55,10 @@ class SVGPath(object):
# xpath | doesn't seem to reliable work so just walk it
for el in self.root.iter():
pb.add_path_from_element(el)
original_pen = pen
for path, transform in zip(pb.paths, pb.transforms):
# TODO use transform
if transform:
pen = TransformPen(original_pen, transform)
else:
pen = original_pen
parse_path(path, pen)

View File

@ -19,6 +19,9 @@ def _strip_xml_ns(tag):
def _transform(raw_value):
# TODO assumes a 'matrix' transform.
# No other transform functions are supported at the moment.
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
# start simple: if you aren't exactly matrix(...) then no love
match = re.match(r'matrix\((.*)\)', raw_value)
if not match:

View File

@ -76,6 +76,12 @@ import pytest
"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
),
# 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)
),
# ellipse
(
'<ellipse cx="100" cy="50" rx="100" ry="50"/>',
@ -88,6 +94,12 @@ import pytest
'M90.5,50 A10,50.5 0 1 1 110.5,50 A10,50.5 0 1 1 90.5,50',
None
),
# 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)
),
]
)
def test_el_to_path(svg_xml, expected_path, expected_transform):