Add quadratic bezier support

This commit is contained in:
2023-03-31 11:42:25 +03:00
parent 360528808e
commit 6910d06c2e
2 changed files with 37 additions and 5 deletions

View File

@@ -203,7 +203,7 @@ And the following SVG path commands are supported:
| MoveTo | M, m | |
| LineTo | L, l, H, h, V, v | |
| 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 |
| ClosePath | Z, z | |

View File

@@ -136,6 +136,20 @@ class SVGPathParser():
x = coordinates[-1][0]
y = coordinates[-1][1]
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):
w = path[i]
@@ -208,7 +222,6 @@ class SVGPathParser():
# Cubic Bézier Curve commands
if (w == "C"):
while True:
# https://github.com/sintef/Splipy/tree/master/examples
control_points = [(x,y),
(getnum(),getnum()),
(getnum(),getnum()),
@@ -221,7 +234,6 @@ class SVGPathParser():
continue
if (w == "c"):
while True:
# https://github.com/sintef/Splipy/tree/master/examples
control_points = [(x,y),
(x + getnum(), y + getnum()),
(x + getnum(), y + getnum()),
@@ -238,9 +250,29 @@ class SVGPathParser():
self.logger.error("SVG path parser '{}' not implemented".format(w))
# Quadratic Bézier Curve commands
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"):
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"):
self.logger.error("SVG path parser '{}' not implemented".format(w))
if (w == "t"):