-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvgpath.go
More file actions
173 lines (156 loc) · 3.14 KB
/
svgpath.go
File metadata and controls
173 lines (156 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Package svgpath provides functionality for working with SVG paths.
package svgpath
import (
"encoding/binary"
"fmt"
"strconv"
"github.com/hfoxy/svgpath"
)
// Compress compresses a SVG path into an optimised
// binary format. It may return the input value.
func Compress(path string) ([]byte, error) {
segs, err := svgpath.Parse(path)
if err != nil {
return nil, err
}
// The SVG 1.1 specification allows "moveto" commands
// to be followed by implicit "lineto" commands. It
// also allows for a continuation of arguments of the
// same command.
//
// Assume that the parser returns indivually commands
// with only a single set of arguments.
var buf []byte
for _, seg := range segs {
cmd := makeCommand(seg.Command)
if cmd == 0 {
return nil, fmt.Errorf("invalid command %q", seg.Command)
}
buf = binary.AppendVarint(buf, int64(cmd))
for _, f := range seg.Args {
buf = binary.AppendVarint(buf, int64(f*1000))
}
}
// don't make output bigger
if len(buf) > len(path) {
return []byte(path), nil
}
return buf, nil
}
const (
invalidCmd command = iota
cmdM
cmdZ
cmdL
cmdH
cmdV
cmdC
cmdS
cmdQ
cmdT
cmdA
)
type command int8
func makeCommand(r rune) (cmd command) {
switch r | ('a' - 'A') { // make lower case
case 'm':
cmd = cmdM
case 'z':
cmd = cmdZ
case 'l':
cmd = cmdL
case 'h':
cmd = cmdH
case 'v':
cmd = cmdV
case 'c':
cmd = cmdC
case 's':
cmd = cmdS
case 'q':
cmd = cmdQ
case 't':
cmd = cmdT
case 'a':
cmd = cmdA
}
if 'a' <= r && r <= 'z' {
cmd = -cmd
}
return cmd
}
// Decompress decompresses an encoded SVG path.
func Decompress(data string) (string, error) {
if data == "" {
return data, nil
}
// paths always start with a moveto command
// if found the data is uncompressed
if data[0] == 'M' || data[0] == 'm' {
return data, nil
}
buf := make([]byte, 0, len(data)*2)
off := 0
var lastCmd command
for {
if len(data[off:]) == 0 {
break
}
v, n := binary.Varint([]byte(data[off:]))
if n <= 0 {
return "", fmt.Errorf("error reading command at offset %d", off)
}
off += n
var letter byte
var nargs int
cmd := command(v)
if v < 0 {
cmd = -cmd
}
switch cmd {
case cmdM:
letter, nargs = 'M', 2
case cmdZ:
letter, nargs = 'Z', 0
case cmdL:
letter, nargs = 'L', 2
case cmdH:
letter, nargs = 'H', 1
case cmdV:
letter, nargs = 'V', 1
case cmdC:
letter, nargs = 'C', 6
case cmdS:
letter, nargs = 'S', 4
case cmdQ:
letter, nargs = 'Q', 4
case cmdT:
letter, nargs = 'T', 2
case cmdA:
letter, nargs = 'A', 7
}
if v < 0 {
// commmand is relative
// make letter lower case
letter |= ('a' - 'A')
}
if letter == 0 {
return "", fmt.Errorf("bad command at offset %d", off)
}
if lastCmd == 0 || lastCmd != cmd && lastCmd == cmdM && cmd != cmdL {
lastCmd = cmd
buf = append(buf, letter)
}
for i := 0; i < nargs; i++ {
v, n := binary.Varint([]byte(data[off:]))
if n <= 0 {
return "", fmt.Errorf("error reading argument at offset %d", off)
}
f := float64(v) / 1000
buf = append(buf, ' ')
buf = strconv.AppendFloat(buf, f, 'f', -1, 64)
off += n
}
}
return string(buf), nil
}