1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2014, Nicolas P. Rougier
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
import re
import math
import numpy as np
from . import geometry
from . geometry import epsilon
from . transformable import Transformable
# ----------------------------------------------------------------- Command ---
class Command(object):
def __repr__(self):
s = '%s ' % self._command
for arg in self._args:
s += "%.2f " % arg
return s
def origin(self, current=None, previous=None):
relative = self._command in "mlvhcsqtaz"
if relative and current:
return current
else:
return 0.0, 0.0
# -------------------------------------------------------------------- Line ---
class Line(Command):
def __init__(self, x=0, y=0, relative=True):
self._command = 'l' if relative else 'L'
self._args = [x, y]
def vertices(self, current, previous=None):
ox, oy = self.origin(current)
x, y = self._args
self.previous = x, y
return (ox + x, oy + y),
# ------------------------------------------------------------------- VLine ---
class VLine(Command):
def __init__(self, y=0, relative=True):
self._command = 'v' if relative else 'V'
self._args = [y]
def vertices(self, current, previous=None):
ox, oy = self.origin(current)
y = self._args[0]
self.previous = ox, oy + y
return (ox, oy + y),
# ------------------------------------------------------------------- HLine ---
class HLine(Command):
def __init__(self, x=0, relative=True):
self._command = 'h' if relative else 'H'
self._args = [x]
def vertices(self, current, previous=None):
ox, oy = self.origin(current)
x = self._args[0]
self.previous = ox + x, oy
return (ox + x, oy),
# -------------------------------------------------------------------- Move ---
class Move(Command):
def __init__(self, x=0, y=0, relative=True):
self._command = 'm' if relative else 'M'
self._args = [x, y]
def vertices(self, current, previous=None):
ox, oy = self.origin(current)
x, y = self._args
x, y = x + ox, y + oy
self.previous = x, y
return (x, y),
# ------------------------------------------------------------------- Close ---
class Close(Command):
def __init__(self, relative=True):
self._command = 'z' if relative else 'Z'
self._args = []
def vertices(self, current, previous=None):
self.previous = current
return []
# --------------------------------------------------------------------- Arc ---
class Arc(Command):
def __init__(self, r1=1, r2=1, angle=2 * math.pi, large=True, sweep=True,
x=0, y=0, relative=True):
self._command = 'a' if relative else 'A'
self._args = [r1, r2, angle, large, sweep, x, y]
def vertices(self, current, previous=None):
ox, oy = self.origin(current)
rx, ry, angle, large, sweep, x, y = self._args
x, y = x + ox, y + oy
x0, y0 = current
self.previous = x, y
vertices = geometry.elliptical_arc(
x0, y0, rx, ry, angle, large, sweep, x, y)
return vertices[1:]
# ------------------------------------------------------------------- Cubic ---
class Cubic(Command):
def __init__(self, x1=0, y1=0, x2=0, y2=0, x3=0, y3=0, relative=True):
self._command = 'c' if relative else 'C'
self._args = [x1, y1, x2, y2, x3, y3]
def vertices(self, current, previous=None):
ox, oy = self.origin(current)
x0, y0 = current
x1, y1, x2, y2, x3, y3 = self._args
x1, y1 = x1 + ox, y1 + oy
x2, y2 = x2 + ox, y2 + oy
x3, y3 = x3 + ox, y3 + oy
self.previous = x2, y2
vertices = geometry.cubic((x0, y0), (x1, y1), (x2, y2), (x3, y3))
return vertices[1:]
# --------------------------------------------------------------- Quadratic ---
class Quadratic(Command):
def __init__(self, x1=0, y1=0, x2=0, y2=0, relative=True):
self._command = 'q' if relative else 'Q'
self._args = [x1, y1, x2, y2]
def vertices(self, current, last_control_point=None):
ox, oy = self.origin(current)
x1, y1, x2, y2 = self._args
x0, y0 = current
x1, y1 = x1 + ox, y1 + oy
x2, y2 = x2 + ox, y2 + oy
self.previous = x1, y1
vertices = geometry.quadratic((x0, y0), (x1, y1), (x2, y2))
return vertices[1:]
# ------------------------------------------------------------- SmoothCubic ---
class SmoothCubic(Command):
def __init__(self, x2=0, y2=0, x3=0, y3=0, relative=True):
self._command = 's' if relative else 'S'
self._args = [x2, y2, x3, y3]
def vertices(self, current, previous):
ox, oy = self.origin(current)
x0, y0 = current
x2, y2, x3, y3 = self._args
x2, y2 = x2 + ox, y2 + oy
x3, y3 = x3 + ox, y3 + oy
x1, y1 = 2 * x0 - previous[0], 2 * y0 - previous[1]
self.previous = x2, y2
vertices = geometry.cubic((x0, y0), (x1, y1), (x2, y2), (x3, y3))
return vertices[1:]
# --------------------------------------------------------- SmoothQuadratic ---
class SmoothQuadratic(Command):
def __init__(self, x2=0, y2=0, relative=True):
self._command = 't' if relative else 'T'
self._args = [x2, y2]
def vertices(self, current, previous):
ox, oy = self.origin(current)
x2, y2 = self._args
x0, y0 = current
x1, y1 = 2 * x0 - previous[0], 2 * y0 - previous[1]
x2, y2 = x2 + ox, y2 + oy
self.previous = x1, y1
vertices = geometry.quadratic((x0, y0), (x1, y1), (x2, y2))
return vertices[1:]
# -------------------------------------------------------------------- Path ---
class Path(Transformable):
def __init__(self, content=None, parent=None):
Transformable.__init__(self, content, parent)
self._paths = []
if not isinstance(content, str):
content = content.get("d", "")
commands = re.compile(
r"(?P<command>[MLVHCSQTAZmlvhcsqtaz])"
r"(?P<points>[+\-0-9.e, \n\t]*)")
path = []
for match in re.finditer(commands, content):
command = match.group("command")
points = match.group("points").replace(',', ' ')
points = [float(v) for v in points.split()]
relative = command in "mlvhcsqtaz"
command = command.upper()
while len(points) or command == 'Z':
if command == 'M':
if len(path):
self._paths.append(path)
path = []
path.append(Move(*points[:2], relative=relative))
points = points[2:]
elif command == 'L':
path.append(Line(*points[:2], relative=relative))
points = points[2:]
elif command == 'V':
path.append(VLine(*points[:1], relative=relative))
points = points[1:]
elif command == 'H':
path.append(HLine(*points[:1], relative=relative))
points = points[1:]
elif command == 'C':
path.append(Cubic(*points[:6], relative=relative))
points = points[6:]
elif command == 'S':
path.append(SmoothCubic(*points[:4], relative=relative))
points = points[4:]
elif command == 'Q':
path.append(Quadratic(*points[:4], relative=relative))
points = points[4:]
elif command == 'T':
path.append(
SmoothQuadratic(*points[2:], relative=relative))
points = points[2:]
elif command == 'A':
path.append(Arc(*points[:7], relative=relative))
points = points[7:]
elif command == 'Z':
path.append(Close(relative=relative))
self._paths.append(path)
path = []
break
else:
raise RuntimeError(
"Unknown SVG path command(%s)" % command)
if len(path):
self._paths.append(path)
def __repr__(self):
s = ""
for path in self._paths:
for item in path:
s += repr(item)
return s
@property
def xml(self):
return self._xml()
def _xml(self, prefix=""):
s = prefix + "<path "
s += 'id="%s" ' % self._id
s += self._style.xml
s += '\n'
t = ' ' + prefix + ' d="'
s += t
prefix = ' ' * len(t)
first = True
for i, path in enumerate(self._paths):
for j, item in enumerate(path):
if first:
s += repr(item)
first = False
else:
s += prefix + repr(item)
if i < len(self._paths) - 1 or j < len(path) - 1:
s += '\n'
s += '"/>\n'
return s
@property
def vertices(self):
self._vertices = []
current = 0, 0
previous = 0, 0
for path in self._paths:
vertices = []
for command in path:
V = command.vertices(current, previous)
previous = command.previous
vertices.extend(V)
if len(V) > 0:
current = V[-1]
else:
current = 0, 0
closed = False
if isinstance(command, Close):
closed = True
if len(vertices) > 2:
d = geometry.calc_sq_distance(vertices[-1][0], vertices[-1][1], # noqa
vertices[0][0], vertices[0][1]) # noqa
if d < epsilon:
vertices = vertices[:-1]
# Apply transformation
V = np.ones((len(vertices), 3))
V[:, :2] = vertices
V = np.dot(V, self.transform.matrix.T)
V[:, 2] = 0
self._vertices.append((V, closed))
return self._vertices
|