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
|
import {
delimiter,
dots,
hyphen,
isPathLetter,
numbersWithDots,
pathLetters
} from '../modules/core/regex.js'
import Point from './Point.js'
import SVGArray from './SVGArray.js'
import parser from '../modules/core/parser.js'
import Box from './Box.js'
export function pathRegReplace (a, b, c, d) {
return c + d.replace(dots, ' .')
}
function arrayToString (a) {
for (var i = 0, il = a.length, s = ''; i < il; i++) {
s += a[i][0]
if (a[i][1] != null) {
s += a[i][1]
if (a[i][2] != null) {
s += ' '
s += a[i][2]
if (a[i][3] != null) {
s += ' '
s += a[i][3]
s += ' '
s += a[i][4]
if (a[i][5] != null) {
s += ' '
s += a[i][5]
s += ' '
s += a[i][6]
if (a[i][7] != null) {
s += ' '
s += a[i][7]
}
}
}
}
}
}
return s + ' '
}
const pathHandlers = {
M: function (c, p, p0) {
p.x = p0.x = c[0]
p.y = p0.y = c[1]
return [ 'M', p.x, p.y ]
},
L: function (c, p) {
p.x = c[0]
p.y = c[1]
return [ 'L', c[0], c[1] ]
},
H: function (c, p) {
p.x = c[0]
return [ 'H', c[0] ]
},
V: function (c, p) {
p.y = c[0]
return [ 'V', c[0] ]
},
C: function (c, p) {
p.x = c[4]
p.y = c[5]
return [ 'C', c[0], c[1], c[2], c[3], c[4], c[5] ]
},
S: function (c, p) {
p.x = c[2]
p.y = c[3]
return [ 'S', c[0], c[1], c[2], c[3] ]
},
Q: function (c, p) {
p.x = c[2]
p.y = c[3]
return [ 'Q', c[0], c[1], c[2], c[3] ]
},
T: function (c, p) {
p.x = c[0]
p.y = c[1]
return [ 'T', c[0], c[1] ]
},
Z: function (c, p, p0) {
p.x = p0.x
p.y = p0.y
return [ 'Z' ]
},
A: function (c, p) {
p.x = c[5]
p.y = c[6]
return [ 'A', c[0], c[1], c[2], c[3], c[4], c[5], c[6] ]
}
}
const mlhvqtcsaz = 'mlhvqtcsaz'.split('')
for (var i = 0, il = mlhvqtcsaz.length; i < il; ++i) {
pathHandlers[mlhvqtcsaz[i]] = (function (i) {
return function (c, p, p0) {
if (i === 'H') c[0] = c[0] + p.x
else if (i === 'V') c[0] = c[0] + p.y
else if (i === 'A') {
c[5] = c[5] + p.x
c[6] = c[6] + p.y
} else {
for (var j = 0, jl = c.length; j < jl; ++j) {
c[j] = c[j] + (j % 2 ? p.y : p.x)
}
}
return pathHandlers[i](c, p, p0)
}
})(mlhvqtcsaz[i].toUpperCase())
}
export default class PathArray extends SVGArray {
// Get bounding box of path
bbox () {
parser().path.setAttribute('d', this.toString())
return new Box(parser.nodes.path.getBBox())
}
// Move path string
move (x, y) {
// get bounding box of current situation
var box = this.bbox()
// get relative offset
x -= box.x
y -= box.y
if (!isNaN(x) && !isNaN(y)) {
// move every point
for (var l, i = this.length - 1; i >= 0; i--) {
l = this[i][0]
if (l === 'M' || l === 'L' || l === 'T') {
this[i][1] += x
this[i][2] += y
} else if (l === 'H') {
this[i][1] += x
} else if (l === 'V') {
this[i][1] += y
} else if (l === 'C' || l === 'S' || l === 'Q') {
this[i][1] += x
this[i][2] += y
this[i][3] += x
this[i][4] += y
if (l === 'C') {
this[i][5] += x
this[i][6] += y
}
} else if (l === 'A') {
this[i][6] += x
this[i][7] += y
}
}
}
return this
}
// Absolutize and parse path to array
parse (array = [ 'M', 0, 0 ]) {
// prepare for parsing
var s
var paramCnt = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 }
if (typeof array === 'string') {
array = array
.replace(numbersWithDots, pathRegReplace) // convert 45.123.123 to 45.123 .123
.replace(pathLetters, ' $& ') // put some room between letters and numbers
.replace(hyphen, '$1 -') // add space before hyphen
.trim() // trim
.split(delimiter) // split into array
} else {
// Flatten array
array = Array.prototype.concat.apply([], array)
}
// array now is an array containing all parts of a path e.g. ['M', '0', '0', 'L', '30', '30' ...]
var result = []
var p = new Point()
var p0 = new Point()
var index = 0
var len = array.length
do {
// Test if we have a path letter
if (isPathLetter.test(array[index])) {
s = array[index]
++index
// If last letter was a move command and we got no new, it defaults to [L]ine
} else if (s === 'M') {
s = 'L'
} else if (s === 'm') {
s = 'l'
}
result.push(pathHandlers[s].call(null,
array.slice(index, (index = index + paramCnt[s.toUpperCase()])).map(parseFloat),
p, p0
)
)
} while (len > index)
return result
}
// Resize path string
size (width, height) {
// get bounding box of current situation
var box = this.bbox()
var i, l
// If the box width or height is 0 then we ignore
// transformations on the respective axis
box.width = box.width === 0 ? 1 : box.width
box.height = box.height === 0 ? 1 : box.height
// recalculate position of all points according to new size
for (i = this.length - 1; i >= 0; i--) {
l = this[i][0]
if (l === 'M' || l === 'L' || l === 'T') {
this[i][1] = ((this[i][1] - box.x) * width) / box.width + box.x
this[i][2] = ((this[i][2] - box.y) * height) / box.height + box.y
} else if (l === 'H') {
this[i][1] = ((this[i][1] - box.x) * width) / box.width + box.x
} else if (l === 'V') {
this[i][1] = ((this[i][1] - box.y) * height) / box.height + box.y
} else if (l === 'C' || l === 'S' || l === 'Q') {
this[i][1] = ((this[i][1] - box.x) * width) / box.width + box.x
this[i][2] = ((this[i][2] - box.y) * height) / box.height + box.y
this[i][3] = ((this[i][3] - box.x) * width) / box.width + box.x
this[i][4] = ((this[i][4] - box.y) * height) / box.height + box.y
if (l === 'C') {
this[i][5] = ((this[i][5] - box.x) * width) / box.width + box.x
this[i][6] = ((this[i][6] - box.y) * height) / box.height + box.y
}
} else if (l === 'A') {
// resize radii
this[i][1] = (this[i][1] * width) / box.width
this[i][2] = (this[i][2] * height) / box.height
// move position values
this[i][6] = ((this[i][6] - box.x) * width) / box.width + box.x
this[i][7] = ((this[i][7] - box.y) * height) / box.height + box.y
}
}
return this
}
// Convert array to string
toString () {
return arrayToString(this)
}
}
|