Skip to content

Commit

Permalink
SVG: bugfix where C/S or Q/T commands were converted to straight line…
Browse files Browse the repository at this point in the history
…s invalidly, fixes #284
  • Loading branch information
tdewolff committed Jan 14, 2020
1 parent 7c4fdeb commit 3eb1409
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 5 additions & 6 deletions svg/pathdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ func (p *PathData) ShortenPathData(b []byte) []byte {
i += n - 1
}
}
if cmd != 0 {
j += p.copyInstruction(b[j:], cmd)
} else {
j = len(b)
if cmd == 0 {
return b
}
j += p.copyInstruction(b[j:], cmd)
return b[:j]
}

Expand Down Expand Up @@ -219,7 +218,7 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
// if control points overlap begin/end points, this is a straight line
// 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)
// only change to a lines if we are sure no 'S' or 's' follows
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) {
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) {
if isRelCmd {
cmd = 'l'
} else {
Expand Down Expand Up @@ -265,7 +264,7 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
// if control point overlaps begin/end points, this is a straight line
// 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)
// only change to a lines if we are sure no 'T' or 't' follows
if (cmd == 'Q' || cmd == 'q' || i+di >= n) && (cpx == p.x || cpx == ax) && (cpy == p.y || cpy == ay) {
if (cmd == 'Q' || cmd == 'q' || i+di >= n) && (cpx == p.x && cpy == p.y || cpx == ax && cpy == ay) {
if isRelCmd {
cmd = 'l'
} else {
Expand Down
4 changes: 4 additions & 0 deletions svg/pathdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestPathData(t *testing.T) {
{
"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",
"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
{
"m-3.5498-0.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389-0+4.9389-5.3027z",
"m-3.5498-.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389.0 4.9389-5.3027z"}, // #284
{"C10 0 0 10 10 10", "C10 0 0 10 10 10"},

// change/remove commands
{"M10 10L10 10L20 10z", "M10 10H20z"},
Expand Down

0 comments on commit 3eb1409

Please sign in to comment.