2017-09-08 17:36:55 +01:00
|
|
|
# SVG Path specification parser.
|
2017-09-08 17:38:32 +01:00
|
|
|
# This is an adaptation from 'svg.path' by Lennart Regebro (@regebro),
|
|
|
|
# modified so that the parser takes a FontTools Pen object instead of
|
|
|
|
# returning a list of svg.path Path objects.
|
|
|
|
# The original code can be found at:
|
2017-09-08 17:36:55 +01:00
|
|
|
# https://github.com/regebro/svg.path/blob/4f9b6e3/src/svg/path/parser.py
|
|
|
|
# Copyright (c) 2013-2014 Lennart Regebro
|
|
|
|
# License: MIT
|
|
|
|
|
2017-09-08 18:15:49 +01:00
|
|
|
from __future__ import (
|
|
|
|
print_function, division, absolute_import, unicode_literals)
|
|
|
|
from fontTools.misc.py23 import *
|
2017-09-08 17:36:55 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
COMMANDS = set('MmZzLlHhVvCcSsQqTtAa')
|
|
|
|
UPPERCASE = set('MZLHVCSQTA')
|
|
|
|
|
|
|
|
COMMAND_RE = re.compile("([MmZzLlHhVvCcSsQqTtAa])")
|
|
|
|
FLOAT_RE = re.compile("[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?")
|
|
|
|
|
|
|
|
|
|
|
|
def _tokenize_path(pathdef):
|
|
|
|
for x in COMMAND_RE.split(pathdef):
|
|
|
|
if x in COMMANDS:
|
|
|
|
yield x
|
|
|
|
for token in FLOAT_RE.findall(x):
|
|
|
|
yield token
|
|
|
|
|
|
|
|
|
2017-09-08 17:52:39 +01:00
|
|
|
def parse_path(pathdef, pen, current_pos=(0, 0)):
|
|
|
|
""" Parse SVG path definition (i.e. "d" attribute of <path> elements)
|
|
|
|
and call a 'pen' object's moveTo, lineTo, curveTo, qCurveTo and closePath
|
|
|
|
methods.
|
|
|
|
|
|
|
|
If 'current_pos' (2-float tuple) is provided, the initial moveTo will
|
|
|
|
be relative to that instead being absolute.
|
2018-01-05 13:07:57 +00:00
|
|
|
|
|
|
|
Arc segments (commands "A" or "a") are not currently supported, and raise
|
|
|
|
NotImplementedError.
|
2017-09-08 17:52:39 +01:00
|
|
|
"""
|
2017-09-08 17:36:55 +01:00
|
|
|
# In the SVG specs, initial movetos are absolute, even if
|
|
|
|
# specified as 'm'. This is the default behavior here as well.
|
|
|
|
# But if you pass in a current_pos variable, the initial moveto
|
|
|
|
# will be relative to that current_pos. This is useful.
|
2017-09-08 17:52:39 +01:00
|
|
|
current_pos = complex(*current_pos)
|
|
|
|
|
2017-09-08 17:36:55 +01:00
|
|
|
elements = list(_tokenize_path(pathdef))
|
|
|
|
# Reverse for easy use of .pop()
|
|
|
|
elements.reverse()
|
|
|
|
|
|
|
|
start_pos = None
|
|
|
|
command = None
|
2017-09-08 17:38:32 +01:00
|
|
|
last_control = None
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
while elements:
|
|
|
|
|
|
|
|
if elements[-1] in COMMANDS:
|
|
|
|
# New command.
|
|
|
|
last_command = command # Used by S and T
|
|
|
|
command = elements.pop()
|
|
|
|
absolute = command in UPPERCASE
|
|
|
|
command = command.upper()
|
|
|
|
else:
|
|
|
|
# If this element starts with numbers, it is an implicit command
|
|
|
|
# and we don't change the command. Check that it's allowed:
|
|
|
|
if command is None:
|
|
|
|
raise ValueError("Unallowed implicit command in %s, position %s" % (
|
|
|
|
pathdef, len(pathdef.split()) - len(elements)))
|
|
|
|
last_command = command # Used by S and T
|
|
|
|
|
|
|
|
if command == 'M':
|
|
|
|
# Moveto command.
|
|
|
|
x = elements.pop()
|
|
|
|
y = elements.pop()
|
|
|
|
pos = float(x) + float(y) * 1j
|
|
|
|
if absolute:
|
|
|
|
current_pos = pos
|
|
|
|
else:
|
|
|
|
current_pos += pos
|
2017-09-11 17:31:58 +01:00
|
|
|
|
|
|
|
# M is not preceded by Z; it's an open subpath
|
|
|
|
if start_pos is not None:
|
|
|
|
pen.endPath()
|
|
|
|
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.moveTo((current_pos.real, current_pos.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
# when M is called, reset start_pos
|
|
|
|
# This behavior of Z is defined in svg spec:
|
|
|
|
# http://www.w3.org/TR/SVG/paths.html#PathDataClosePathCommand
|
|
|
|
start_pos = current_pos
|
|
|
|
|
|
|
|
# Implicit moveto commands are treated as lineto commands.
|
|
|
|
# So we set command to lineto here, in case there are
|
|
|
|
# further implicit commands after this moveto.
|
|
|
|
command = 'L'
|
|
|
|
|
|
|
|
elif command == 'Z':
|
|
|
|
# Close path
|
|
|
|
if current_pos != start_pos:
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.lineTo((start_pos.real, start_pos.imag))
|
|
|
|
pen.closePath()
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = start_pos
|
|
|
|
start_pos = None
|
|
|
|
command = None # You can't have implicit commands after closing.
|
|
|
|
|
|
|
|
elif command == 'L':
|
|
|
|
x = elements.pop()
|
|
|
|
y = elements.pop()
|
|
|
|
pos = float(x) + float(y) * 1j
|
|
|
|
if not absolute:
|
|
|
|
pos += current_pos
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.lineTo((pos.real, pos.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = pos
|
|
|
|
|
|
|
|
elif command == 'H':
|
|
|
|
x = elements.pop()
|
|
|
|
pos = float(x) + current_pos.imag * 1j
|
|
|
|
if not absolute:
|
|
|
|
pos += current_pos.real
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.lineTo((pos.real, pos.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = pos
|
|
|
|
|
|
|
|
elif command == 'V':
|
|
|
|
y = elements.pop()
|
|
|
|
pos = current_pos.real + float(y) * 1j
|
|
|
|
if not absolute:
|
|
|
|
pos += current_pos.imag * 1j
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.lineTo((pos.real, pos.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = pos
|
|
|
|
|
|
|
|
elif command == 'C':
|
|
|
|
control1 = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
control2 = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
|
|
|
|
if not absolute:
|
|
|
|
control1 += current_pos
|
|
|
|
control2 += current_pos
|
|
|
|
end += current_pos
|
|
|
|
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.curveTo((control1.real, control1.imag),
|
|
|
|
(control2.real, control2.imag),
|
|
|
|
(end.real, end.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = end
|
2017-09-08 17:38:32 +01:00
|
|
|
last_control = control2
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
elif command == 'S':
|
|
|
|
# Smooth curve. First control point is the "reflection" of
|
|
|
|
# the second control point in the previous path.
|
|
|
|
|
|
|
|
if last_command not in 'CS':
|
|
|
|
# If there is no previous command or if the previous command
|
|
|
|
# was not an C, c, S or s, assume the first control point is
|
|
|
|
# coincident with the current point.
|
|
|
|
control1 = current_pos
|
|
|
|
else:
|
|
|
|
# The first control point is assumed to be the reflection of
|
|
|
|
# the second control point on the previous command relative
|
|
|
|
# to the current point.
|
2017-09-08 17:38:32 +01:00
|
|
|
control1 = current_pos + current_pos - last_control
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
control2 = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
|
|
|
|
if not absolute:
|
|
|
|
control2 += current_pos
|
|
|
|
end += current_pos
|
|
|
|
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.curveTo((control1.real, control1.imag),
|
|
|
|
(control2.real, control2.imag),
|
|
|
|
(end.real, end.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = end
|
2017-09-08 17:38:32 +01:00
|
|
|
last_control = control2
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
elif command == 'Q':
|
|
|
|
control = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
|
|
|
|
if not absolute:
|
|
|
|
control += current_pos
|
|
|
|
end += current_pos
|
|
|
|
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.qCurveTo((control.real, control.imag), (end.real, end.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = end
|
2017-09-08 17:38:32 +01:00
|
|
|
last_control = control
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
elif command == 'T':
|
|
|
|
# Smooth curve. Control point is the "reflection" of
|
|
|
|
# the second control point in the previous path.
|
|
|
|
|
|
|
|
if last_command not in 'QT':
|
|
|
|
# If there is no previous command or if the previous command
|
|
|
|
# was not an Q, q, T or t, assume the first control point is
|
|
|
|
# coincident with the current point.
|
|
|
|
control = current_pos
|
|
|
|
else:
|
|
|
|
# The control point is assumed to be the reflection of
|
|
|
|
# the control point on the previous command relative
|
|
|
|
# to the current point.
|
2017-09-08 17:38:32 +01:00
|
|
|
control = current_pos + current_pos - last_control
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
end = float(elements.pop()) + float(elements.pop()) * 1j
|
|
|
|
|
|
|
|
if not absolute:
|
|
|
|
end += current_pos
|
|
|
|
|
2017-09-08 17:38:32 +01:00
|
|
|
pen.qCurveTo((control.real, control.imag), (end.real, end.imag))
|
2017-09-08 17:36:55 +01:00
|
|
|
current_pos = end
|
2017-09-11 17:34:04 +01:00
|
|
|
last_control = control
|
2017-09-08 17:36:55 +01:00
|
|
|
|
|
|
|
elif command == 'A':
|
2018-01-05 13:07:57 +00:00
|
|
|
raise NotImplementedError('arcs are not supported')
|
2017-09-11 17:31:58 +01:00
|
|
|
|
|
|
|
# no final Z command, it's an open path
|
|
|
|
if start_pos is not None:
|
|
|
|
pen.endPath()
|