diff options
Diffstat (limited to 'Lib/fontTools/svgLib/path/shapes.py')
-rw-r--r-- | Lib/fontTools/svgLib/path/shapes.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Lib/fontTools/svgLib/path/shapes.py b/Lib/fontTools/svgLib/path/shapes.py index a83274e4..4cc633ad 100644 --- a/Lib/fontTools/svgLib/path/shapes.py +++ b/Lib/fontTools/svgLib/path/shapes.py @@ -1,3 +1,6 @@ +import re + + def _prefer_non_zero(*args): for arg in args: if arg != 0: @@ -16,12 +19,28 @@ def _strip_xml_ns(tag): return tag.split('}', 1)[1] if '}' in tag else 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: + raise NotImplementedError + matrix = tuple(float(p) for p in re.split(r'\s+|,', match.group(1))) + if len(matrix) != 6: + raise ValueError('wrong # of terms in %s' % raw_value) + return matrix + + class PathBuilder(object): def __init__(self): self.paths = [] + self.transforms = [] def _start_path(self, initial_path=''): self.paths.append(initial_path) + self.transforms.append(None) def _end_path(self): self._add('z') @@ -68,6 +87,25 @@ class PathBuilder(object): def v(self, y): self._vhline('v', y) + def _line(self, c, x, y): + self._add('%s%s,%s' % (c, _ntos(x), _ntos(y))) + + def L(self, x, y): + self._line('L', x, y) + + def l(self, x, y): + self._line('l', x, y) + + def _parse_line(self, line): + x1 = float(line.attrib.get('x1', 0)) + y1 = float(line.attrib.get('y1', 0)) + x2 = float(line.attrib.get('x2', 0)) + y2 = float(line.attrib.get('y2', 0)) + + self._start_path() + self.M(x1, y1) + self.L(x2, y2) + def _parse_rect(self, rect): x = float(rect.attrib.get('x', 0)) y = float(rect.attrib.get('y', 0)) @@ -105,6 +143,10 @@ class PathBuilder(object): self._start_path('M' + poly.attrib['points']) self._end_path() + def _parse_polyline(self, poly): + if 'points' in poly.attrib: + self._start_path('M' + poly.attrib['points']) + def _parse_circle(self, circle): cx = float(circle.attrib.get('cx', 0)) cy = float(circle.attrib.get('cy', 0)) @@ -116,10 +158,24 @@ class PathBuilder(object): self.A(r, r, cx + r, cy, large_arc=1) self.A(r, r, cx - r, cy, large_arc=1) + def _parse_ellipse(self, ellipse): + cx = float(ellipse.attrib.get('cx', 0)) + cy = float(ellipse.attrib.get('cy', 0)) + rx = float(ellipse.attrib.get('rx')) + ry = float(ellipse.attrib.get('ry')) + + # arc doesn't seem to like being a complete shape, draw two halves + self._start_path() + self.M(cx - rx, cy) + self.A(rx, ry, cx + rx, cy, large_arc=1) + self.A(rx, ry, cx - rx, 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) + if 'transform' in el.attrib: + self.transforms[-1] = _transform(el.attrib['transform']) return True |