Skip to content

Commit

Permalink
Return point for Z cmd should be the first pair of M coordinates, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 26, 2018
1 parent 7867a41 commit c86dd76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
15 changes: 7 additions & 8 deletions svg/pathdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type PathData struct {
o *Minifier

x, y float64
x0, y0 float64
coords [][]byte
coordFloats []float64

Expand All @@ -36,7 +37,6 @@ func NewPathData(o *Minifier) *PathData {
// ShortenPathData takes a full pathdata string and returns a shortened version. The original string is overwritten.
// It parses all commands (M, A, Z, ...) and coordinates (numbers) and calls copyInstruction for each command.
func (p *PathData) ShortenPathData(b []byte) []byte {
var x0, y0 float64
var cmd byte

p.x, p.y = 0.0, 0.0
Expand All @@ -52,13 +52,6 @@ func (p *PathData) ShortenPathData(b []byte) []byte {
} else if c >= 'A' && (cmd == 0 || cmd != c || c == 'M' || c == 'm') { // any command
if cmd != 0 {
j += p.copyInstruction(b[j:], cmd)
if cmd == 'M' || cmd == 'm' {
x0 = p.x
y0 = p.y
} else if cmd == 'Z' || cmd == 'z' {
p.x = x0
p.y = y0
}
}
cmd = c
p.coords = p.coords[:0]
Expand All @@ -82,6 +75,8 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
n := len(p.coords)
if n == 0 {
if cmd == 'Z' || cmd == 'z' {
p.x = p.x0
p.y = p.y0
b[0] = 'z'
return 1
}
Expand Down Expand Up @@ -191,6 +186,10 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
p.x = ax
p.y = ay
}
if i == 0 && (origCmd == 'M' || origCmd == 'm') {
p.x0 = p.x
p.y0 = p.y
}
}
return j
}
Expand Down
17 changes: 17 additions & 0 deletions svg/pathdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ func TestPathData(t *testing.T) {
}
}

func TestPathDataTruncated(t *testing.T) {
var pathDataTests = []struct {
pathData string
expected string
}{
{"m100 0 50 50zM100 0z", "m1e2.0 50 50zm0 0z"},
}

p := NewPathData(&Minifier{Decimals: 3})
for _, tt := range pathDataTests {
t.Run(tt.pathData, func(t *testing.T) {
path := p.ShortenPathData([]byte(tt.pathData))
test.Minify(t, tt.pathData, nil, string(path), tt.expected)
})
}
}

////////////////////////////////////////////////////////////////

func BenchmarkShortenPathData(b *testing.B) {
Expand Down

0 comments on commit c86dd76

Please sign in to comment.