Skip to content

Commit 3eb1409

Browse files
committed
SVG: bugfix where C/S or Q/T commands were converted to straight lines invalidly, fixes #284
1 parent 7c4fdeb commit 3eb1409

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

svg/pathdata.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,10 @@ func (p *PathData) ShortenPathData(b []byte) []byte {
105105
i += n - 1
106106
}
107107
}
108-
if cmd != 0 {
109-
j += p.copyInstruction(b[j:], cmd)
110-
} else {
111-
j = len(b)
108+
if cmd == 0 {
109+
return b
112110
}
111+
j += p.copyInstruction(b[j:], cmd)
113112
return b[:j]
114113
}
115114

@@ -219,7 +218,7 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
219218
// if control points overlap begin/end points, this is a straight line
220219
// even though if the control points would be along the straight line, we won't minify that as the control points influence the speed along the curve (important for dashes for example)
221220
// only change to a lines if we are sure no 'S' or 's' follows
222-
if (cmd == 'C' || cmd == 'c' || i+di >= n) && (cp1x == p.x || cp1x == ax) && (cp1y == p.y || cp1y == ay) && (cp2x == p.x || cp2x == ax) && (cp2y == p.y || cp2y == ay) {
221+
if (cmd == 'C' || cmd == 'c' || i+di >= n) && (cp1x == p.x && cp1y == p.y || cp1x == ax && cp1y == ay) && (cp2x == p.x && cp2y == p.y || cp2x == ax && cp2y == ay) {
223222
if isRelCmd {
224223
cmd = 'l'
225224
} else {
@@ -265,7 +264,7 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
265264
// if control point overlaps begin/end points, this is a straight line
266265
// even though if the control point would be along the straight line, we won't minify that as the control point influences the speed along the curve (important for dashes for example)
267266
// only change to a lines if we are sure no 'T' or 't' follows
268-
if (cmd == 'Q' || cmd == 'q' || i+di >= n) && (cpx == p.x || cpx == ax) && (cpy == p.y || cpy == ay) {
267+
if (cmd == 'Q' || cmd == 'q' || i+di >= n) && (cpx == p.x && cpy == p.y || cpx == ax && cpy == ay) {
269268
if isRelCmd {
270269
cmd = 'l'
271270
} else {

svg/pathdata_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func TestPathData(t *testing.T) {
3939
{
4040
"q6.55 0 10.56-2.93a9.36 9.36 0 004-8 10 10 0 00-3.37-7.83q-3.37-3-9.9-4.79A25.38 25.38 0 0137.76 44",
4141
"q6.55.0 10.56-2.93a9.36 9.36.0 004-8 10 10 0 00-3.37-7.83q-3.37-3-9.9-4.79A25.38 25.38.0 0137.76 44"}, // #275
42+
{
43+
"m-3.5498-0.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389-0+4.9389-5.3027z",
44+
"m-3.5498-.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389.0 4.9389-5.3027z"}, // #284
45+
{"C10 0 0 10 10 10", "C10 0 0 10 10 10"},
4246

4347
// change/remove commands
4448
{"M10 10L10 10L20 10z", "M10 10H20z"},

0 commit comments

Comments
 (0)