forked from nidu/svg-transform-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-svg-transform.pegjs
104 lines (82 loc) · 2.23 KB
/
parse-svg-transform.pegjs
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
/*
* Parser for SVG transform.
* Based on http://www.w3.org/TR/SVG/coords.html#TransformAttribute
*/
transformList
= wsp* ts:transforms? wsp* { return ts; }
transforms
= t:transform commaWsp+ ts:transforms { return [t].concat(ts) }
/ t:transform { return [t] }
transform
= matrix
/ translate
/ scale
/ rotate
/ skewX
/ skewY
matrix
= "matrix" wsp* "(" wsp*
a:number commaWsp
b:number commaWsp
c:number commaWsp
d:number commaWsp
e:number commaWsp
f:number wsp* ")" {
return {op: 'matrix', args: {a: a, b: b, c: c, d: d, e: e, f: f}};
}
translate
= "translate" wsp* "(" wsp* tx:number ty:commaWspNumber? wsp* ")" {
var t = {tx: tx, ty: ty ? ty : 0};
return {op: 'translate', args: t};
}
scale
= "scale" wsp* "(" wsp* sx:number sy:commaWspNumber? wsp* ")" {
var s = {sx: sx, sy: sy ? sy : sx};
return {op: 'scale', args: s};
}
rotate
= "rotate" wsp* "(" wsp* angle:number c:commaWspTwoNumbers? wsp* ")" {
var r = {angle: angle};
if (c) {
r.cx = c[0];
r.cy = c[1];
}
return {op: 'rotate', args: r};
}
skewX
= "skewX" wsp* "(" wsp* angle:number wsp* ")" {
return {op: 'skewX', args: {angle: angle}};
}
skewY
= "skewY" wsp* "(" wsp* angle:number wsp* ")" {
return {op: 'skewY', args: {angle: angle}};
}
number
= s:sign? f:floatingPointConstant { return parseFloat([s, f.join("")].join("")); }
/ i:(sign? integerConstant) { return parseInt(i.join("")); }
commaWspNumber
= commaWsp n:number { return n; }
commaWspTwoNumbers
= commaWsp n1:number commaWsp n2:number { return [n1, n2]; }
commaWsp
= (wsp+ comma? wsp*) / (comma wsp*)
comma
= ","
integerConstant
= ds:digitSequence { return ds.join(""); }
floatingPointConstant
= fractionalConstant exponent?
/ digitSequence exponent
fractionalConstant "fractionalConstant"
= d1:digitSequence? "." d2:digitSequence { return [d1 ? d1.join("") : null, ".", d2.join("")].join(""); }
/ d:digitSequence "." { return d.join(""); }
exponent
= e:[eE] s:sign? d:digitSequence { return [e, s, d.join("")].join("") }
sign
= [+-]
digitSequence
= digit+
digit
= [0-9]
wsp
= [\u0020\u0009\u000D\u000A]