Add quadratic bezier support
This commit is contained in:
@@ -203,7 +203,7 @@ And the following SVG path commands are supported:
|
|||||||
| MoveTo | M, m | |
|
| MoveTo | M, m | |
|
||||||
| LineTo | L, l, H, h, V, v | |
|
| LineTo | L, l, H, h, V, v | |
|
||||||
| Cubic Bézier Curve | C, c, S, s | |
|
| Cubic Bézier Curve | C, c, S, s | |
|
||||||
| Quadratic Bézier Curve | | Q, q, T, t |
|
| Quadratic Bézier Curve | Q, q | T, t |
|
||||||
| Elliptical Arc Curve | | A, a |
|
| Elliptical Arc Curve | | A, a |
|
||||||
| ClosePath | Z, z | |
|
| ClosePath | Z, z | |
|
||||||
|
|
||||||
|
|||||||
@@ -136,6 +136,20 @@ class SVGPathParser():
|
|||||||
x = coordinates[-1][0]
|
x = coordinates[-1][0]
|
||||||
y = coordinates[-1][1]
|
y = coordinates[-1][1]
|
||||||
return coordinates
|
return coordinates
|
||||||
|
def quadratic_bezier(control_points):
|
||||||
|
nonlocal x
|
||||||
|
nonlocal y
|
||||||
|
control_points = np.array(control_points)
|
||||||
|
n = 500
|
||||||
|
curve = cf.bezier(control_points, quadratic=True)
|
||||||
|
lin = np.linspace(curve.start(0), curve.end(0), n)
|
||||||
|
coordinates = curve(lin)
|
||||||
|
coordinates = np.nan_to_num(coordinates)
|
||||||
|
coordinates = dropzeros(coordinates)
|
||||||
|
if len(coordinates) > 0:
|
||||||
|
x = coordinates[-1][0]
|
||||||
|
y = coordinates[-1][1]
|
||||||
|
return coordinates
|
||||||
|
|
||||||
while i < len(path):
|
while i < len(path):
|
||||||
w = path[i]
|
w = path[i]
|
||||||
@@ -208,7 +222,6 @@ class SVGPathParser():
|
|||||||
# Cubic Bézier Curve commands
|
# Cubic Bézier Curve commands
|
||||||
if (w == "C"):
|
if (w == "C"):
|
||||||
while True:
|
while True:
|
||||||
# https://github.com/sintef/Splipy/tree/master/examples
|
|
||||||
control_points = [(x,y),
|
control_points = [(x,y),
|
||||||
(getnum(),getnum()),
|
(getnum(),getnum()),
|
||||||
(getnum(),getnum()),
|
(getnum(),getnum()),
|
||||||
@@ -221,7 +234,6 @@ class SVGPathParser():
|
|||||||
continue
|
continue
|
||||||
if (w == "c"):
|
if (w == "c"):
|
||||||
while True:
|
while True:
|
||||||
# https://github.com/sintef/Splipy/tree/master/examples
|
|
||||||
control_points = [(x,y),
|
control_points = [(x,y),
|
||||||
(x + getnum(), y + getnum()),
|
(x + getnum(), y + getnum()),
|
||||||
(x + getnum(), y + getnum()),
|
(x + getnum(), y + getnum()),
|
||||||
@@ -238,9 +250,29 @@ class SVGPathParser():
|
|||||||
self.logger.error("SVG path parser '{}' not implemented".format(w))
|
self.logger.error("SVG path parser '{}' not implemented".format(w))
|
||||||
# Quadratic Bézier Curve commands
|
# Quadratic Bézier Curve commands
|
||||||
if (w == "Q"):
|
if (w == "Q"):
|
||||||
self.logger.error("SVG path parser '{}' not implemented".format(w))
|
while True:
|
||||||
|
control_points = [(x,y),
|
||||||
|
(getnum(),getnum()),
|
||||||
|
(getnum(),getnum()),
|
||||||
|
(getnum(),getnum())]
|
||||||
|
coordinates = quadratic_bezier(control_points)
|
||||||
|
appendpoints(coordinates)
|
||||||
|
if not nextisnum():
|
||||||
|
break
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
if (w == "q"):
|
if (w == "q"):
|
||||||
self.logger.error("SVG path parser '{}' not implemented".format(w))
|
while True:
|
||||||
|
control_points = [(x,y),
|
||||||
|
(x + getnum(), y + getnum()),
|
||||||
|
(x + getnum(), y + getnum()),
|
||||||
|
(x + getnum(), y + getnum())]
|
||||||
|
coordinates = quadratic_bezier(control_points)
|
||||||
|
appendpoints(coordinates)
|
||||||
|
if not nextisnum():
|
||||||
|
break
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
if (w == "T"):
|
if (w == "T"):
|
||||||
self.logger.error("SVG path parser '{}' not implemented".format(w))
|
self.logger.error("SVG path parser '{}' not implemented".format(w))
|
||||||
if (w == "t"):
|
if (w == "t"):
|
||||||
|
|||||||
Reference in New Issue
Block a user