-
Notifications
You must be signed in to change notification settings - Fork 48
/
svgbasic.go
331 lines (317 loc) · 8.71 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright ©2023 The go-pdf Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
/*
* 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, adjustToPt float64) (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 {
seg.Arg[argJ] *= adjustToPt
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
}
// parseFloatWithUnit parses a float and its unit, e.g. "42pt".
//
// The result is converted into pt values wich is the default document unit.
// parseFloatWithUnit returns the factor to apply to positions or distances to
// convert their values in point units.
func parseFloatWithUnit(val string) (float64, float64, error) {
var adjustToPt float64
var removeUnitChar int
var floatValue float64
var err error
switch {
case strings.HasSuffix(val, "pt"):
removeUnitChar = 2
adjustToPt = 1.0
case strings.HasSuffix(val, "in"):
removeUnitChar = 2
adjustToPt = 72.0
case strings.HasSuffix(val, "mm"):
removeUnitChar = 2
adjustToPt = 72.0 / 25.4
case strings.HasSuffix(val, "cm"):
removeUnitChar = 2
adjustToPt = 72.0 / 2.54
case strings.HasSuffix(val, "pc"):
removeUnitChar = 2
adjustToPt = 12.0
default: // default is pixel
removeUnitChar = 0
adjustToPt = 1.0 / 96.0
}
floatValue, err = strconv.ParseFloat(val[:len(val)-removeUnitChar], 64)
if err != nil {
return 0.0, 0.0, err
}
return floatValue * adjustToPt, adjustToPt, nil
}
// 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). The document is returned with "pt" unit.
func SVGBasicParse(buf []byte) (sig SVGBasicType, err error) {
type pathType struct {
D string `xml:"d,attr"`
}
type rectType struct {
Width float64 `xml:"width,attr"`
Height float64 `xml:"height,attr"`
X float64 `xml:"x,attr"`
Y float64 `xml:"y,attr"`
}
type srcType struct {
Wd string `xml:"width,attr"`
Ht string `xml:"height,attr"`
Paths []pathType `xml:"path"`
Rects []rectType `xml:"rect"`
}
var src srcType
var wd float64
var ht float64
var adjustToPt float64
err = xml.Unmarshal(buf, &src)
if err == nil {
wd, adjustToPt, err = parseFloatWithUnit(src.Wd)
if err != nil {
return sig, err
}
ht, _, err = parseFloatWithUnit(src.Ht)
if err != nil {
return sig, err
}
if wd > 0 && ht > 0 {
sig.Wd, sig.Ht = wd, ht
var segs []SVGBasicSegmentType
for _, path := range src.Paths {
if err == nil {
segs, err = pathParse(path.D, adjustToPt)
if err == nil {
sig.Segments = append(sig.Segments, segs)
}
}
}
for _, rect := range src.Rects {
segs = nil
segs = append(segs, SVGBasicSegmentType{
Cmd: 'M',
Arg: [6]float64{rect.X * adjustToPt, rect.Y * adjustToPt},
})
segs = append(segs, SVGBasicSegmentType{
Cmd: 'L',
Arg: [6]float64{(rect.X + rect.Width) * adjustToPt, rect.Y * adjustToPt},
})
segs = append(segs, SVGBasicSegmentType{
Cmd: 'L',
Arg: [6]float64{(rect.X + rect.Width) * adjustToPt, (rect.Y + rect.Height) * adjustToPt},
})
segs = append(segs, SVGBasicSegmentType{
Cmd: 'L',
Arg: [6]float64{rect.X * adjustToPt, (rect.Y + rect.Height) * adjustToPt},
})
segs = append(segs, SVGBasicSegmentType{
Cmd: 'Z',
})
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
}