-
Notifications
You must be signed in to change notification settings - Fork 47
/
svgbasic.go
246 lines (236 loc) · 6.26 KB
/
svgbasic.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (c) 2014 Kurt Jung (Gmail: kurt.w.jung)
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package fpdf
import (
"encoding/xml"
"fmt"
"os"
"strconv"
"strings"
)
var pathCmdSub *strings.Replacer
func init() {
// Handle permitted constructions like "100L200,230"
pathCmdSub = strings.NewReplacer(",", " ",
"L", " L ", "l", " l ",
"C", " C ", "c", " c ",
"M", " M ", "m", " m ",
"H", " H ", "h", " h ",
"V", " V ", "v", " v ",
"Q", " Q ", "q", " q ",
"Z", " Z ", "z", " z ")
}
// SVGBasicSegmentType describes a single curve or position segment
type SVGBasicSegmentType struct {
Cmd byte // See http://www.w3.org/TR/SVG/paths.html for path command structure
Arg [6]float64
}
func absolutizePath(segs []SVGBasicSegmentType) {
var x, y float64
var segPtr *SVGBasicSegmentType
adjust := func(pos int, adjX, adjY float64) {
segPtr.Arg[pos] += adjX
segPtr.Arg[pos+1] += adjY
}
for j, seg := range segs {
segPtr = &segs[j]
if j == 0 && seg.Cmd == 'm' {
segPtr.Cmd = 'M'
}
switch segPtr.Cmd {
case 'M':
x = seg.Arg[0]
y = seg.Arg[1]
case 'm':
adjust(0, x, y)
segPtr.Cmd = 'M'
x = segPtr.Arg[0]
y = segPtr.Arg[1]
case 'L':
x = seg.Arg[0]
y = seg.Arg[1]
case 'l':
adjust(0, x, y)
segPtr.Cmd = 'L'
x = segPtr.Arg[0]
y = segPtr.Arg[1]
case 'C':
x = seg.Arg[4]
y = seg.Arg[5]
case 'c':
adjust(0, x, y)
adjust(2, x, y)
adjust(4, x, y)
segPtr.Cmd = 'C'
x = segPtr.Arg[4]
y = segPtr.Arg[5]
case 'Q':
x = seg.Arg[2]
y = seg.Arg[3]
case 'q':
adjust(0, x, y)
adjust(2, x, y)
segPtr.Cmd = 'Q'
x = segPtr.Arg[2]
y = segPtr.Arg[3]
case 'H':
x = seg.Arg[0]
case 'h':
segPtr.Arg[0] += x
segPtr.Cmd = 'H'
x += seg.Arg[0]
case 'V':
y = seg.Arg[0]
case 'v':
segPtr.Arg[0] += y
segPtr.Cmd = 'V'
y += seg.Arg[0]
case 'z':
segPtr.Cmd = 'Z'
}
}
}
func pathParse(pathStr string) (segs []SVGBasicSegmentType, err error) {
var seg SVGBasicSegmentType
var j, argJ, argCount, prevArgCount int
setup := func(n int) {
// It is not strictly necessary to clear arguments, but result may be clearer
// to caller
for j := 0; j < len(seg.Arg); j++ {
seg.Arg[j] = 0.0
}
argJ = 0
argCount = n
prevArgCount = n
}
var str string
var c byte
pathStr = pathCmdSub.Replace(pathStr)
strList := strings.Fields(pathStr)
count := len(strList)
for j = 0; j < count && err == nil; j++ {
str = strList[j]
if argCount == 0 { // Look for path command or argument continuation
c = str[0]
if c == '-' || (c >= '0' && c <= '9') { // More arguments
if j > 0 {
setup(prevArgCount)
// Repeat previous action
if seg.Cmd == 'M' {
seg.Cmd = 'L'
} else if seg.Cmd == 'm' {
seg.Cmd = 'l'
}
} else {
err = fmt.Errorf("expecting SVG path command at first position, got %s", str)
}
}
}
if err == nil {
if argCount == 0 {
seg.Cmd = str[0]
switch seg.Cmd {
case 'M', 'm': // Absolute/relative moveto: x, y
setup(2)
case 'C', 'c': // Absolute/relative Bézier curve: cx0, cy0, cx1, cy1, x1, y1
setup(6)
case 'H', 'h': // Absolute/relative horizontal line to: x
setup(1)
case 'L', 'l': // Absolute/relative lineto: x, y
setup(2)
case 'Q', 'q': // Absolute/relative quadratic curve: x0, y0, x1, y1
setup(4)
case 'V', 'v': // Absolute/relative vertical line to: y
setup(1)
case 'Z', 'z': // closepath instruction (takes no arguments)
segs = append(segs, seg)
default:
err = fmt.Errorf("expecting SVG path command at position %d, got %s", j, str)
}
} else {
seg.Arg[argJ], err = strconv.ParseFloat(str, 64)
if err == nil {
argJ++
argCount--
if argCount == 0 {
segs = append(segs, seg)
}
}
}
}
}
if err == nil {
if argCount == 0 {
absolutizePath(segs)
} else {
err = fmt.Errorf("expecting additional (%d) numeric arguments", argCount)
}
}
return
}
// SVGBasicType aggregates the information needed to describe a multi-segment
// basic vector image
type SVGBasicType struct {
Wd, Ht float64
Segments [][]SVGBasicSegmentType
}
// SVGBasicParse parses a simple scalable vector graphics (SVG) buffer into a
// descriptor. Only a small subset of the SVG standard, in particular the path
// information generated by jSignature, is supported. The returned path data
// includes only the commands 'M' (absolute moveto: x, y), 'L' (absolute
// lineto: x, y), 'C' (absolute cubic Bézier curve: cx0, cy0, cx1, cy1,
// x1,y1), 'Q' (absolute quadratic Bézier curve: x0, y0, x1, y1) and 'Z'
// (closepath).
func SVGBasicParse(buf []byte) (sig SVGBasicType, err error) {
type pathType struct {
D string `xml:"d,attr"`
}
type srcType struct {
Wd float64 `xml:"width,attr"`
Ht float64 `xml:"height,attr"`
Paths []pathType `xml:"path"`
}
var src srcType
err = xml.Unmarshal(buf, &src)
if err == nil {
if src.Wd > 0 && src.Ht > 0 {
sig.Wd, sig.Ht = src.Wd, src.Ht
var segs []SVGBasicSegmentType
for _, path := range src.Paths {
if err == nil {
segs, err = pathParse(path.D)
if err == nil {
sig.Segments = append(sig.Segments, segs)
}
}
}
} else {
err = fmt.Errorf("unacceptable values for basic SVG extent: %.2f x %.2f",
sig.Wd, sig.Ht)
}
}
return
}
// SVGBasicFileParse parses a simple scalable vector graphics (SVG) file into a
// basic descriptor. The SVGBasicWrite() example demonstrates this method.
func SVGBasicFileParse(svgFileStr string) (sig SVGBasicType, err error) {
var buf []byte
buf, err = os.ReadFile(svgFileStr)
if err == nil {
sig, err = SVGBasicParse(buf)
}
return
}