2018-03-30 18:42:32 -07:00
|
|
|
import itertools as it
|
|
|
|
import re
|
2018-03-30 18:47:57 -07:00
|
|
|
import string
|
2018-03-31 15:11:35 -07:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
from xml.dom import minidom
|
2016-04-17 00:31:38 -07:00
|
|
|
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
|
|
|
from manimlib.mobject.geometry import Circle
|
|
|
|
from manimlib.mobject.geometry import Rectangle
|
|
|
|
from manimlib.mobject.geometry import RoundedRectangle
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
|
|
|
from manimlib.utils.color import *
|
|
|
|
from manimlib.utils.config_ops import digest_config
|
|
|
|
from manimlib.utils.config_ops import digest_locals
|
2016-04-17 00:31:38 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-03-17 19:57:28 +01:00
|
|
|
def string_to_numbers(num_string):
|
2018-04-06 13:58:59 -07:00
|
|
|
num_string = num_string.replace("-", ",-")
|
|
|
|
num_string = num_string.replace("e,-", "e-")
|
2017-03-17 19:57:28 +01:00
|
|
|
return [
|
|
|
|
float(s)
|
|
|
|
for s in re.split("[ ,]", num_string)
|
|
|
|
if s != ""
|
|
|
|
]
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
class SVGMobject(VMobject):
|
2016-04-23 23:36:05 -07:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"should_center": True,
|
|
|
|
"height": 2,
|
|
|
|
"width": None,
|
|
|
|
# Must be filled in in a subclass, or when called
|
|
|
|
"file_name": None,
|
|
|
|
"unpack_groups": True, # if False, creates a hierarchy of VGroups
|
2019-10-08 10:32:04 +03:00
|
|
|
"stroke_width": DEFAULT_STROKE_WIDTH,
|
2018-04-12 23:19:09 +02:00
|
|
|
"fill_opacity": 1.0,
|
2017-07-06 10:45:50 -07:00
|
|
|
# "fill_color" : LIGHT_GREY,
|
2016-04-23 23:36:05 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-09-27 17:37:25 -07:00
|
|
|
def __init__(self, file_name=None, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
2018-12-01 14:28:51 -08:00
|
|
|
self.file_name = file_name or self.file_name
|
2016-07-12 10:34:35 -07:00
|
|
|
self.ensure_valid_file()
|
2016-04-17 00:31:38 -07:00
|
|
|
VMobject.__init__(self, **kwargs)
|
2016-04-17 19:29:27 -07:00
|
|
|
self.move_into_position()
|
2016-04-17 00:31:38 -07:00
|
|
|
|
2016-07-12 10:34:35 -07:00
|
|
|
def ensure_valid_file(self):
|
2016-11-07 11:05:41 -08:00
|
|
|
if self.file_name is None:
|
|
|
|
raise Exception("Must specify file for SVGMobject")
|
2016-07-12 10:34:35 -07:00
|
|
|
possible_paths = [
|
2019-06-03 23:41:05 -07:00
|
|
|
os.path.join(os.path.join("assets", "svg_images"), self.file_name),
|
|
|
|
os.path.join(os.path.join("assets", "svg_images"), self.file_name + ".svg"),
|
|
|
|
os.path.join(os.path.join("assets", "svg_images"), self.file_name + ".xdv"),
|
2018-02-26 23:35:40 -08:00
|
|
|
self.file_name,
|
2016-07-12 10:34:35 -07:00
|
|
|
]
|
|
|
|
for path in possible_paths:
|
|
|
|
if os.path.exists(path):
|
2016-12-07 18:37:56 -08:00
|
|
|
self.file_path = path
|
2016-07-12 10:34:35 -07:00
|
|
|
return
|
2018-04-06 13:58:59 -07:00
|
|
|
raise IOError("No file matching %s in image directory" %
|
|
|
|
self.file_name)
|
2016-07-12 10:34:35 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
def generate_points(self):
|
2016-12-07 18:37:56 -08:00
|
|
|
doc = minidom.parse(self.file_path)
|
2016-04-17 19:29:27 -07:00
|
|
|
self.ref_to_element = {}
|
|
|
|
for svg in doc.getElementsByTagName("svg"):
|
2018-01-28 15:02:57 +01:00
|
|
|
mobjects = self.get_mobjects_from(svg)
|
2018-04-06 13:58:59 -07:00
|
|
|
if self.unpack_groups:
|
|
|
|
self.add(*mobjects)
|
|
|
|
else:
|
|
|
|
self.add(*mobjects[0].submobjects)
|
2016-04-17 00:31:38 -07:00
|
|
|
doc.unlink()
|
|
|
|
|
2016-04-17 19:29:27 -07:00
|
|
|
def get_mobjects_from(self, element):
|
|
|
|
result = []
|
|
|
|
if not isinstance(element, minidom.Element):
|
|
|
|
return result
|
|
|
|
if element.tagName == 'defs':
|
|
|
|
self.update_ref_to_element(element)
|
|
|
|
elif element.tagName == 'style':
|
2018-04-06 13:58:59 -07:00
|
|
|
pass # TODO, handle style
|
2019-08-05 22:53:15 +08:00
|
|
|
elif element.tagName in ['g', 'svg', 'symbol']:
|
2016-04-17 19:29:27 -07:00
|
|
|
result += it.chain(*[
|
|
|
|
self.get_mobjects_from(child)
|
|
|
|
for child in element.childNodes
|
|
|
|
])
|
|
|
|
elif element.tagName == 'path':
|
2016-04-20 19:24:54 -07:00
|
|
|
result.append(self.path_string_to_mobject(
|
|
|
|
element.getAttribute('d')
|
|
|
|
))
|
2016-04-17 19:29:27 -07:00
|
|
|
elif element.tagName == 'use':
|
|
|
|
result += self.use_to_mobjects(element)
|
|
|
|
elif element.tagName == 'rect':
|
|
|
|
result.append(self.rect_to_mobject(element))
|
|
|
|
elif element.tagName == 'circle':
|
|
|
|
result.append(self.circle_to_mobject(element))
|
2018-01-28 14:55:17 +01:00
|
|
|
elif element.tagName == 'ellipse':
|
|
|
|
result.append(self.ellipse_to_mobject(element))
|
2017-01-20 09:26:14 -08:00
|
|
|
elif element.tagName in ['polygon', 'polyline']:
|
2017-01-17 17:14:32 -08:00
|
|
|
result.append(self.polygon_to_mobject(element))
|
2016-04-17 19:29:27 -07:00
|
|
|
else:
|
2018-04-06 13:58:59 -07:00
|
|
|
pass # TODO
|
2017-06-05 12:47:03 -07:00
|
|
|
# warnings.warn("Unknown element type: " + element.tagName)
|
2018-08-09 17:56:05 -07:00
|
|
|
result = [m for m in result if m is not None]
|
2019-02-06 21:16:26 -08:00
|
|
|
self.handle_transforms(element, VGroup(*result))
|
2018-01-28 15:02:57 +01:00
|
|
|
if len(result) > 1 and not self.unpack_groups:
|
|
|
|
result = [VGroup(*result)]
|
|
|
|
|
2016-04-17 19:29:27 -07:00
|
|
|
return result
|
|
|
|
|
|
|
|
def g_to_mobjects(self, g_element):
|
2019-02-06 21:16:26 -08:00
|
|
|
mob = VGroup(*self.get_mobjects_from(g_element))
|
2016-04-17 19:29:27 -07:00
|
|
|
self.handle_transforms(g_element, mob)
|
|
|
|
return mob.submobjects
|
|
|
|
|
2016-04-20 19:24:54 -07:00
|
|
|
def path_string_to_mobject(self, path_string):
|
|
|
|
return VMobjectFromSVGPathstring(path_string)
|
2016-04-17 19:29:27 -07:00
|
|
|
|
|
|
|
def use_to_mobjects(self, use_element):
|
2018-04-06 13:58:59 -07:00
|
|
|
# Remove initial "#" character
|
2016-04-17 00:31:38 -07:00
|
|
|
ref = use_element.getAttribute("xlink:href")[1:]
|
2016-04-20 19:24:54 -07:00
|
|
|
if ref not in self.ref_to_element:
|
2018-04-06 13:58:59 -07:00
|
|
|
warnings.warn("%s not recognized" % ref)
|
2019-02-06 21:16:26 -08:00
|
|
|
return VGroup()
|
2016-04-20 19:24:54 -07:00
|
|
|
return self.get_mobjects_from(
|
|
|
|
self.ref_to_element[ref]
|
|
|
|
)
|
2016-04-17 19:29:27 -07:00
|
|
|
|
2018-09-04 16:14:11 -07:00
|
|
|
def attribute_to_float(self, attr):
|
|
|
|
stripped_attr = "".join([
|
|
|
|
char for char in attr
|
|
|
|
if char in string.digits + "." + "-"
|
|
|
|
])
|
|
|
|
return float(stripped_attr)
|
|
|
|
|
2017-01-17 17:14:32 -08:00
|
|
|
def polygon_to_mobject(self, polygon_element):
|
2018-04-06 13:58:59 -07:00
|
|
|
# TODO, This seems hacky...
|
2017-01-17 17:14:32 -08:00
|
|
|
path_string = polygon_element.getAttribute("points")
|
|
|
|
for digit in string.digits:
|
|
|
|
path_string = path_string.replace(" " + digit, " L" + digit)
|
|
|
|
path_string = "M" + path_string
|
|
|
|
return self.path_string_to_mobject(path_string)
|
|
|
|
|
2016-04-17 19:29:27 -07:00
|
|
|
# <circle class="st1" cx="143.8" cy="268" r="22.6"/>
|
2016-04-17 00:31:38 -07:00
|
|
|
|
|
|
|
def circle_to_mobject(self, circle_element):
|
2016-04-17 19:29:27 -07:00
|
|
|
x, y, r = [
|
2018-09-04 16:14:11 -07:00
|
|
|
self.attribute_to_float(
|
|
|
|
circle_element.getAttribute(key)
|
|
|
|
)
|
2016-04-17 19:29:27 -07:00
|
|
|
if circle_element.hasAttribute(key)
|
|
|
|
else 0.0
|
2018-06-02 08:59:26 -04:00
|
|
|
for key in ("cx", "cy", "r")
|
2016-04-17 19:29:27 -07:00
|
|
|
]
|
2018-04-06 13:58:59 -07:00
|
|
|
return Circle(radius=r).shift(x * RIGHT + y * DOWN)
|
2016-04-17 00:31:38 -07:00
|
|
|
|
2018-01-28 14:55:17 +01:00
|
|
|
def ellipse_to_mobject(self, circle_element):
|
|
|
|
x, y, rx, ry = [
|
2018-09-04 16:14:11 -07:00
|
|
|
self.attribute_to_float(
|
|
|
|
circle_element.getAttribute(key)
|
|
|
|
)
|
2018-01-28 14:55:17 +01:00
|
|
|
if circle_element.hasAttribute(key)
|
|
|
|
else 0.0
|
2018-06-02 08:59:26 -04:00
|
|
|
for key in ("cx", "cy", "rx", "ry")
|
2018-01-28 14:55:17 +01:00
|
|
|
]
|
2018-04-06 13:58:59 -07:00
|
|
|
return Circle().scale(rx * RIGHT + ry * UP).shift(x * RIGHT + y * DOWN)
|
2018-01-28 14:55:17 +01:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
def rect_to_mobject(self, rect_element):
|
2018-04-12 18:33:16 +02:00
|
|
|
fill_color = rect_element.getAttribute("fill")
|
|
|
|
stroke_color = rect_element.getAttribute("stroke")
|
|
|
|
stroke_width = rect_element.getAttribute("stroke-width")
|
2018-04-12 23:19:09 +02:00
|
|
|
corner_radius = rect_element.getAttribute("rx")
|
2018-04-12 18:33:16 +02:00
|
|
|
|
|
|
|
# input preprocessing
|
|
|
|
if fill_color in ["", "none", "#FFF", "#FFFFFF"] or Color(fill_color) == Color(WHITE):
|
|
|
|
opacity = 0
|
2018-04-12 21:56:28 -07:00
|
|
|
fill_color = BLACK # shdn't be necessary but avoids error msgs
|
2018-04-12 18:33:16 +02:00
|
|
|
if fill_color in ["#000", "#000000"]:
|
|
|
|
fill_color = WHITE
|
|
|
|
if stroke_color in ["", "none", "#FFF", "#FFFFFF"] or Color(stroke_color) == Color(WHITE):
|
|
|
|
stroke_width = 0
|
|
|
|
stroke_color = BLACK
|
|
|
|
if stroke_color in ["#000", "#000000"]:
|
|
|
|
stroke_color = WHITE
|
|
|
|
if stroke_width in ["", "none", "0"]:
|
|
|
|
stroke_width = 0
|
|
|
|
|
2018-04-12 23:19:09 +02:00
|
|
|
if corner_radius in ["", "0", "none"]:
|
|
|
|
corner_radius = 0
|
|
|
|
|
|
|
|
corner_radius = float(corner_radius)
|
|
|
|
|
|
|
|
if corner_radius == 0:
|
|
|
|
mob = Rectangle(
|
2018-09-04 16:14:11 -07:00
|
|
|
width=self.attribute_to_float(
|
|
|
|
rect_element.getAttribute("width")
|
|
|
|
),
|
|
|
|
height=self.attribute_to_float(
|
|
|
|
rect_element.getAttribute("height")
|
|
|
|
),
|
2018-04-12 21:56:28 -07:00
|
|
|
stroke_width=stroke_width,
|
|
|
|
stroke_color=stroke_color,
|
|
|
|
fill_color=fill_color,
|
|
|
|
fill_opacity=opacity
|
2018-04-12 23:19:09 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
mob = RoundedRectangle(
|
2018-09-04 16:14:11 -07:00
|
|
|
width=self.attribute_to_float(
|
|
|
|
rect_element.getAttribute("width")
|
|
|
|
),
|
|
|
|
height=self.attribute_to_float(
|
|
|
|
rect_element.getAttribute("height")
|
|
|
|
),
|
2018-04-12 21:56:28 -07:00
|
|
|
stroke_width=stroke_width,
|
|
|
|
stroke_color=stroke_color,
|
|
|
|
fill_color=fill_color,
|
|
|
|
fill_opacity=opacity,
|
|
|
|
corner_radius=corner_radius
|
2018-04-12 23:19:09 +02:00
|
|
|
)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
mob.shift(mob.get_center() - mob.get_corner(UP + LEFT))
|
2016-04-17 00:31:38 -07:00
|
|
|
return mob
|
|
|
|
|
2016-04-17 19:29:27 -07:00
|
|
|
def handle_transforms(self, element, mobject):
|
2016-04-17 00:31:38 -07:00
|
|
|
x, y = 0, 0
|
2016-04-17 19:29:27 -07:00
|
|
|
try:
|
2018-09-04 16:14:11 -07:00
|
|
|
x = self.attribute_to_float(element.getAttribute('x'))
|
2018-04-06 13:58:59 -07:00
|
|
|
# Flip y
|
2018-09-04 16:14:11 -07:00
|
|
|
y = -self.attribute_to_float(element.getAttribute('y'))
|
2018-04-06 13:58:59 -07:00
|
|
|
mobject.shift(x * RIGHT + y * UP)
|
2016-04-17 19:29:27 -07:00
|
|
|
except:
|
|
|
|
pass
|
2017-03-17 19:57:28 +01:00
|
|
|
|
2018-02-12 22:35:51 +01:00
|
|
|
transform = element.getAttribute('transform')
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
try: # transform matrix
|
2017-03-17 19:57:28 +01:00
|
|
|
prefix = "matrix("
|
|
|
|
suffix = ")"
|
2018-04-06 13:58:59 -07:00
|
|
|
if not transform.startswith(prefix) or not transform.endswith(suffix):
|
|
|
|
raise Exception()
|
2017-03-17 19:57:28 +01:00
|
|
|
transform = transform[len(prefix):-len(suffix)]
|
|
|
|
transform = string_to_numbers(transform)
|
2018-04-06 13:58:59 -07:00
|
|
|
transform = np.array(transform).reshape([3, 2])
|
2018-02-12 22:35:51 +01:00
|
|
|
x = transform[2][0]
|
|
|
|
y = -transform[2][1]
|
2017-03-17 19:57:28 +01:00
|
|
|
matrix = np.identity(self.dim)
|
2018-04-06 13:58:59 -07:00
|
|
|
matrix[:2, :2] = transform[:2, :]
|
2018-02-12 22:35:51 +01:00
|
|
|
matrix[1] *= -1
|
2018-04-06 13:58:59 -07:00
|
|
|
matrix[:, 1] *= -1
|
2017-03-17 19:57:28 +01:00
|
|
|
|
|
|
|
for mob in mobject.family_members_with_points():
|
2018-02-12 22:35:51 +01:00
|
|
|
mob.points = np.dot(mob.points, matrix)
|
2018-04-06 13:58:59 -07:00
|
|
|
mobject.shift(x * RIGHT + y * UP)
|
2018-02-12 22:35:51 +01:00
|
|
|
except:
|
|
|
|
pass
|
2017-03-17 19:57:28 +01:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
try: # transform scale
|
2018-02-12 22:35:51 +01:00
|
|
|
prefix = "scale("
|
|
|
|
suffix = ")"
|
2018-04-06 13:58:59 -07:00
|
|
|
if not transform.startswith(prefix) or not transform.endswith(suffix):
|
|
|
|
raise Exception()
|
2018-02-12 22:35:51 +01:00
|
|
|
transform = transform[len(prefix):-len(suffix)]
|
2018-11-24 16:37:45 -05:00
|
|
|
scale_values = string_to_numbers(transform)
|
|
|
|
if len(scale_values) == 2:
|
|
|
|
scale_x, scale_y = scale_values
|
2018-12-04 18:08:52 -08:00
|
|
|
mobject.scale(np.array([scale_x, scale_y, 1]), about_point=ORIGIN)
|
2018-11-24 16:37:45 -05:00
|
|
|
elif len(scale_values) == 1:
|
|
|
|
scale = scale_values[0]
|
2018-12-04 18:08:52 -08:00
|
|
|
mobject.scale(np.array([scale, scale, 1]), about_point=ORIGIN)
|
2017-03-17 19:57:28 +01:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
try: # transform translate
|
2018-02-12 22:35:51 +01:00
|
|
|
prefix = "translate("
|
|
|
|
suffix = ")"
|
2018-04-06 13:58:59 -07:00
|
|
|
if not transform.startswith(prefix) or not transform.endswith(suffix):
|
|
|
|
raise Exception()
|
2018-02-12 22:35:51 +01:00
|
|
|
transform = transform[len(prefix):-len(suffix)]
|
|
|
|
x, y = string_to_numbers(transform)
|
2018-04-06 13:58:59 -07:00
|
|
|
mobject.shift(x * RIGHT + y * DOWN)
|
2018-02-12 22:35:51 +01:00
|
|
|
except:
|
|
|
|
pass
|
2018-04-06 13:58:59 -07:00
|
|
|
# TODO, ...
|
2016-04-17 00:31:38 -07:00
|
|
|
|
2019-08-05 22:53:15 +08:00
|
|
|
def flatten(self, input_list):
|
|
|
|
output_list = []
|
2019-08-16 15:53:36 +08:00
|
|
|
for i in input_list:
|
|
|
|
if isinstance(i, list):
|
|
|
|
output_list.extend(self.flatten(i))
|
|
|
|
else:
|
|
|
|
output_list.append(i)
|
2019-08-05 22:53:15 +08:00
|
|
|
return output_list
|
|
|
|
|
|
|
|
def get_all_childNodes_have_id(self, element):
|
|
|
|
all_childNodes_have_id = []
|
|
|
|
if not isinstance(element, minidom.Element):
|
|
|
|
return
|
|
|
|
if element.hasAttribute('id'):
|
2019-09-13 01:51:22 +08:00
|
|
|
return [element]
|
2019-08-05 22:53:15 +08:00
|
|
|
for e in element.childNodes:
|
|
|
|
all_childNodes_have_id.append(self.get_all_childNodes_have_id(e))
|
|
|
|
return self.flatten([e for e in all_childNodes_have_id if e])
|
|
|
|
|
2016-04-17 19:29:27 -07:00
|
|
|
def update_ref_to_element(self, defs):
|
2019-08-05 22:53:15 +08:00
|
|
|
new_refs = dict([(e.getAttribute('id'), e) for e in self.get_all_childNodes_have_id(defs)])
|
2016-04-17 19:29:27 -07:00
|
|
|
self.ref_to_element.update(new_refs)
|
2016-04-17 00:31:38 -07:00
|
|
|
|
|
|
|
def move_into_position(self):
|
2016-04-23 23:36:05 -07:00
|
|
|
if self.should_center:
|
|
|
|
self.center()
|
2017-06-20 14:05:48 -07:00
|
|
|
if self.height is not None:
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_height(self.height)
|
2017-06-20 14:05:48 -07:00
|
|
|
if self.width is not None:
|
2018-08-08 10:30:52 -07:00
|
|
|
self.set_width(self.width)
|
2017-06-20 14:05:48 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
class VMobjectFromSVGPathstring(VMobject):
|
|
|
|
def __init__(self, path_string, **kwargs):
|
|
|
|
digest_locals(self)
|
|
|
|
VMobject.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
def get_path_commands(self):
|
2016-04-17 19:29:27 -07:00
|
|
|
result = [
|
2018-04-06 13:58:59 -07:00
|
|
|
"M", # moveto
|
|
|
|
"L", # lineto
|
|
|
|
"H", # horizontal lineto
|
|
|
|
"V", # vertical lineto
|
|
|
|
"C", # curveto
|
|
|
|
"S", # smooth curveto
|
|
|
|
"Q", # quadratic Bezier curve
|
|
|
|
"T", # smooth quadratic Bezier curveto
|
|
|
|
"A", # elliptical Arc
|
|
|
|
"Z", # closepath
|
2016-04-17 00:31:38 -07:00
|
|
|
]
|
2018-08-09 17:56:05 -07:00
|
|
|
result += [s.lower() for s in result]
|
2016-04-17 19:29:27 -07:00
|
|
|
return result
|
2016-04-17 00:31:38 -07:00
|
|
|
|
|
|
|
def generate_points(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
pattern = "[%s]" % ("".join(self.get_path_commands()))
|
2018-08-09 17:56:05 -07:00
|
|
|
pairs = list(zip(
|
2016-04-17 00:31:38 -07:00
|
|
|
re.findall(pattern, self.path_string),
|
|
|
|
re.split(pattern, self.path_string)[1:]
|
2018-08-09 17:56:05 -07:00
|
|
|
))
|
2018-04-06 13:58:59 -07:00
|
|
|
# Which mobject should new points be added to
|
2019-02-05 11:02:15 -08:00
|
|
|
self = self
|
2016-04-17 00:31:38 -07:00
|
|
|
for command, coord_string in pairs:
|
|
|
|
self.handle_command(command, coord_string)
|
2018-04-06 13:58:59 -07:00
|
|
|
# people treat y-coordinate differently
|
|
|
|
self.rotate(np.pi, RIGHT, about_point=ORIGIN)
|
2016-04-17 00:31:38 -07:00
|
|
|
|
|
|
|
def handle_command(self, command, coord_string):
|
2016-04-17 19:29:27 -07:00
|
|
|
isLower = command.islower()
|
|
|
|
command = command.upper()
|
2018-04-06 13:58:59 -07:00
|
|
|
# new_points are the points that will be added to the curr_points
|
|
|
|
# list. This variable may get modified in the conditionals below.
|
2019-02-05 11:02:15 -08:00
|
|
|
points = self.points
|
2016-04-17 00:31:38 -07:00
|
|
|
new_points = self.string_to_points(coord_string)
|
2018-01-28 14:52:11 +01:00
|
|
|
|
2019-02-05 11:02:15 -08:00
|
|
|
if isLower and len(points) > 0:
|
|
|
|
new_points += points[-1]
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
if command == "M": # moveto
|
2019-02-05 11:02:15 -08:00
|
|
|
self.start_new_path(new_points[0])
|
2018-04-06 13:58:59 -07:00
|
|
|
if len(new_points) <= 1:
|
|
|
|
return
|
2018-01-28 14:52:11 +01:00
|
|
|
|
2019-02-19 21:33:03 -08:00
|
|
|
# Draw relative line-to values.
|
2019-02-05 11:02:15 -08:00
|
|
|
points = self.points
|
2018-01-28 14:52:11 +01:00
|
|
|
new_points = new_points[1:]
|
|
|
|
command = "L"
|
2019-02-19 21:33:03 -08:00
|
|
|
|
|
|
|
for p in new_points:
|
2019-09-13 01:51:22 +08:00
|
|
|
if isLower:
|
|
|
|
# Treat everything as relative line-to until empty
|
|
|
|
p[0] += self.points[-1, 0]
|
|
|
|
p[1] += self.points[-1, 1]
|
2019-02-19 21:33:03 -08:00
|
|
|
self.add_line_to(p)
|
|
|
|
return
|
|
|
|
|
2019-02-05 11:02:15 -08:00
|
|
|
elif command in ["L", "H", "V"]: # lineto
|
2016-04-17 00:31:38 -07:00
|
|
|
if command == "H":
|
2018-04-06 13:58:59 -07:00
|
|
|
new_points[0, 1] = points[-1, 1]
|
2016-04-17 00:31:38 -07:00
|
|
|
elif command == "V":
|
2017-02-08 13:29:32 -08:00
|
|
|
if isLower:
|
2018-04-06 13:58:59 -07:00
|
|
|
new_points[0, 0] -= points[-1, 0]
|
|
|
|
new_points[0, 0] += points[-1, 1]
|
|
|
|
new_points[0, 1] = new_points[0, 0]
|
|
|
|
new_points[0, 0] = points[-1, 0]
|
2019-02-05 11:02:15 -08:00
|
|
|
self.add_line_to(new_points[0])
|
|
|
|
return
|
|
|
|
|
|
|
|
if command == "C": # curveto
|
2018-04-06 13:58:59 -07:00
|
|
|
pass # Yay! No action required
|
|
|
|
elif command in ["S", "T"]: # smooth curveto
|
2019-02-05 11:02:15 -08:00
|
|
|
self.add_smooth_curve_to(*new_points)
|
|
|
|
# handle1 = points[-1] + (points[-1] - points[-2])
|
|
|
|
# new_points = np.append([handle1], new_points, axis=0)
|
|
|
|
return
|
|
|
|
elif command == "Q": # quadratic Bezier curve
|
2018-04-06 13:58:59 -07:00
|
|
|
# TODO, this is a suboptimal approximation
|
|
|
|
new_points = np.append([new_points[0]], new_points, axis=0)
|
|
|
|
elif command == "A": # elliptical Arc
|
2016-04-17 00:31:38 -07:00
|
|
|
raise Exception("Not implemented")
|
2018-04-06 13:58:59 -07:00
|
|
|
elif command == "Z": # closepath
|
2019-02-05 13:12:55 -08:00
|
|
|
return
|
2017-02-08 13:29:32 -08:00
|
|
|
|
2019-02-19 21:33:03 -08:00
|
|
|
# Add first three points
|
|
|
|
self.add_cubic_bezier_curve_to(*new_points[0:3])
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
# Handle situations where there's multiple relative control points
|
2019-02-19 21:33:03 -08:00
|
|
|
if len(new_points) > 3:
|
|
|
|
# Add subsequent offset points relatively.
|
2017-02-08 13:29:32 -08:00
|
|
|
for i in range(3, len(new_points), 3):
|
2019-02-19 21:33:03 -08:00
|
|
|
if isLower:
|
|
|
|
new_points[i:i + 3] -= points[-1]
|
|
|
|
new_points[i:i + 3] += new_points[i - 1]
|
|
|
|
self.add_cubic_bezier_curve_to(*new_points[i:i+3])
|
2016-04-17 00:31:38 -07:00
|
|
|
|
|
|
|
def string_to_points(self, coord_string):
|
2017-03-17 19:57:28 +01:00
|
|
|
numbers = string_to_numbers(coord_string)
|
2018-04-06 13:58:59 -07:00
|
|
|
if len(numbers) % 2 == 1:
|
2016-04-17 00:31:38 -07:00
|
|
|
numbers.append(0)
|
2018-08-10 15:12:49 -07:00
|
|
|
num_points = len(numbers) // 2
|
2016-04-17 00:31:38 -07:00
|
|
|
result = np.zeros((num_points, self.dim))
|
2018-04-06 13:58:59 -07:00
|
|
|
result[:, :2] = np.array(numbers).reshape((num_points, 2))
|
2016-04-17 00:31:38 -07:00
|
|
|
return result
|
|
|
|
|
|
|
|
def get_original_path_string(self):
|
|
|
|
return self.path_string
|