1 |
|
2 | 'use strict';
|
3 |
|
4 | const svgShapesToPath = {
|
5 | rectToPath: svgShapesToPathRectToPath,
|
6 | polylineToPath: svgShapesToPathPolylineToPath,
|
7 | lineToPath: svgShapesToPathLineToPath,
|
8 | circleToPath: svgShapesToPathCircleToPath,
|
9 | polygonToPath: svgShapesToPathPolygonToPath,
|
10 | };
|
11 |
|
12 | module.exports = svgShapesToPath;
|
13 |
|
14 |
|
15 | function svgShapesToPathRectToPath(attributes) {
|
16 | const x = 'undefined' !== typeof attributes.x ? parseFloat(attributes.x) : 0;
|
17 | const y = 'undefined' !== typeof attributes.y ? parseFloat(attributes.y) : 0;
|
18 | const width =
|
19 | 'undefined' !== typeof attributes.width ? parseFloat(attributes.width) : 0;
|
20 | const height =
|
21 | 'undefined' !== typeof attributes.height
|
22 | ? parseFloat(attributes.height)
|
23 | : 0;
|
24 | const rx =
|
25 | 'undefined' !== typeof attributes.rx
|
26 | ? parseFloat(attributes.rx)
|
27 | : 'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry) : 0;
|
28 | const ry =
|
29 | 'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry) : rx;
|
30 |
|
31 | return (
|
32 | '' +
|
33 |
|
34 | 'M' +
|
35 | (x + rx) +
|
36 | ' ' +
|
37 | y +
|
38 |
|
39 | 'h' +
|
40 | (width - rx * 2) +
|
41 |
|
42 | (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry : '') +
|
43 |
|
44 | 'v' +
|
45 | (height - ry * 2) +
|
46 |
|
47 | (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry : '') +
|
48 |
|
49 | 'h' +
|
50 | (width - rx * 2) * -1 +
|
51 |
|
52 | (rx || ry
|
53 | ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry * -1
|
54 | : '') +
|
55 |
|
56 | 'v' +
|
57 | (height - ry * 2) * -1 +
|
58 |
|
59 | (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry * -1 : '') +
|
60 |
|
61 | 'z'
|
62 | );
|
63 | }
|
64 |
|
65 | function svgShapesToPathPolylineToPath(attributes) {
|
66 | return 'M' + attributes.points;
|
67 | }
|
68 |
|
69 | function svgShapesToPathLineToPath(attributes) {
|
70 |
|
71 | return (
|
72 | '' +
|
73 | 'M' +
|
74 | (parseFloat(attributes.x1) || 0).toString(10) +
|
75 | ' ' +
|
76 | (parseFloat(attributes.y1) || 0).toString(10) +
|
77 | ' ' +
|
78 | ((parseFloat(attributes.x1) || 0) + 1).toString(10) +
|
79 | ' ' +
|
80 | ((parseFloat(attributes.y1) || 0) + 1).toString(10) +
|
81 | ' ' +
|
82 | ((parseFloat(attributes.x2) || 0) + 1).toString(10) +
|
83 | ' ' +
|
84 | ((parseFloat(attributes.y2) || 0) + 1).toString(10) +
|
85 | ' ' +
|
86 | (parseFloat(attributes.x2) || 0).toString(10) +
|
87 | ' ' +
|
88 | (parseFloat(attributes.y2) || 0).toString(10) +
|
89 | 'Z'
|
90 | );
|
91 | }
|
92 |
|
93 | function svgShapesToPathCircleToPath(attributes) {
|
94 | const cx = parseFloat(attributes.cx);
|
95 | const cy = parseFloat(attributes.cy);
|
96 | const rx =
|
97 | 'undefined' !== typeof attributes.rx
|
98 | ? parseFloat(attributes.rx)
|
99 | : parseFloat(attributes.r);
|
100 | const ry =
|
101 | 'undefined' !== typeof attributes.ry
|
102 | ? parseFloat(attributes.ry)
|
103 | : parseFloat(attributes.r);
|
104 |
|
105 |
|
106 | return (
|
107 | '' +
|
108 | 'M' +
|
109 | (cx - rx) +
|
110 | ',' +
|
111 | cy +
|
112 | 'A' +
|
113 | rx +
|
114 | ',' +
|
115 | ry +
|
116 | ' 0,0,0 ' +
|
117 | (cx + rx) +
|
118 | ',' +
|
119 | cy +
|
120 | 'A' +
|
121 | rx +
|
122 | ',' +
|
123 | ry +
|
124 | ' 0,0,0 ' +
|
125 | (cx - rx) +
|
126 | ',' +
|
127 | cy
|
128 | );
|
129 | }
|
130 |
|
131 | function svgShapesToPathPolygonToPath(attributes) {
|
132 | return 'M' + attributes.points + 'Z';
|
133 | }
|