Merge pull request #1508 from fonttools/svg-shapes

[svgLib] add support for importing shapes into paths
This commit is contained in:
Cosimo Lupo 2019-02-15 21:25:47 +00:00 committed by GitHub
commit da4ea5b906
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 201 additions and 2 deletions

View File

@ -5,6 +5,7 @@ from fontTools.misc.py23 import *
from fontTools.pens.transformPen import TransformPen from fontTools.pens.transformPen import TransformPen
from fontTools.misc import etree from fontTools.misc import etree
from .parser import parse_path from .parser import parse_path
from .shapes import PathBuilder
__all__ = [tostr(s) for s in ("SVGPath", "parse_path")] __all__ = [tostr(s) for s in ("SVGPath", "parse_path")]
@ -50,5 +51,10 @@ class SVGPath(object):
def draw(self, pen): def draw(self, pen):
if self.transform: if self.transform:
pen = TransformPen(pen, self.transform) pen = TransformPen(pen, self.transform)
for el in self.root.findall(".//{http://www.w3.org/2000/svg}path[@d]"): pb = PathBuilder()
parse_path(el.get("d"), pen) # xpath | doesn't seem to reliable work so just walk it
for el in self.root.iter():
pb.add_path_from_element(el)
for path in pb.paths:
parse_path(path, pen)

View File

@ -0,0 +1,125 @@
def _prefer_non_zero(*args):
for arg in args:
if arg != 0:
return arg
return 0.
def _ntos(n):
# %f likes to add unnecessary 0's, %g isn't consistent about # decimals
return ('%.3f' % n).rstrip('0').rstrip('.')
def _strip_xml_ns(tag):
# ElementTree API doesn't provide a way to ignore XML namespaces in tags
# so we here strip them ourselves: cf. https://bugs.python.org/issue18304
return tag.split('}', 1)[1] if '}' in tag else tag
class PathBuilder(object):
def __init__(self):
self.paths = []
def _start_path(self, initial_path=''):
self.paths.append(initial_path)
def _end_path(self):
self._add('z')
def _add(self, path_snippet):
path = self.paths[-1]
if path:
path += ' ' + path_snippet
else:
path = path_snippet
self.paths[-1] = path
def _move(self, c, x, y):
self._add('%s%s,%s' % (c, _ntos(x), _ntos(y)))
def M(self, x, y):
self._move('M', x, y)
def m(self, x, y):
self._move('m', x, y)
def _arc(self, c, rx, ry, x, y, large_arc):
self._add('%s%s,%s 0 %d 1 %s,%s' % (c, _ntos(rx), _ntos(ry), large_arc,
_ntos(x), _ntos(y)))
def A(self, rx, ry, x, y, large_arc=0):
self._arc('A', rx, ry, x, y, large_arc)
def a(self, rx, ry, x, y, large_arc=0):
self._arc('a', rx, ry, x, y, large_arc)
def _vhline(self, c, x):
self._add('%s%s' % (c, _ntos(x)))
def H(self, x):
self._vhline('H', x)
def h(self, x):
self._vhline('h', x)
def V(self, y):
self._vhline('V', y)
def v(self, y):
self._vhline('v', y)
def _parse_rect(self, rect):
x = float(rect.attrib.get('x', 0))
y = float(rect.attrib.get('y', 0))
w = float(rect.attrib.get('width'))
h = float(rect.attrib.get('height'))
rx = float(rect.attrib.get('rx', 0))
ry = float(rect.attrib.get('ry', 0))
rx = _prefer_non_zero(rx, ry)
ry = _prefer_non_zero(ry, rx)
# TODO there are more rules for adjusting rx, ry
self._start_path()
self.M(x + rx, y)
self.H(x + w - rx)
if rx > 0:
self.A(rx, ry, x + w, y + ry)
self.V(y + h - ry)
if rx > 0:
self.A(rx, ry, x + w - rx, y + h)
self.H(x + rx)
if rx > 0:
self.A(rx, ry, x, y + h - ry)
self.V(y + ry)
if rx > 0:
self.A(rx, ry, x + rx, y)
self._end_path()
def _parse_path(self, path):
if 'd' in path.attrib:
self._start_path(initial_path=path.attrib['d'])
def _parse_polygon(self, poly):
if 'points' in poly.attrib:
self._start_path('M' + poly.attrib['points'])
self._end_path()
def _parse_circle(self, circle):
cx = float(circle.attrib.get('cx', 0))
cy = float(circle.attrib.get('cy', 0))
r = float(circle.attrib.get('r'))
# arc doesn't seem to like being a complete shape, draw two halves
self._start_path()
self.M(cx - r, cy)
self.A(r, r, cx + r, cy, large_arc=1)
self.A(r, r, cx - r, cy, large_arc=1)
def add_path_from_element(self, el):
tag = _strip_xml_ns(el.tag)
parse_fn = getattr(self, '_parse_%s' % tag.lower(), None)
if not callable(parse_fn):
return False
parse_fn(el)
return True

View File

@ -0,0 +1,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