-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
369 lines (329 loc) · 8.69 KB
/
index.js
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"use strict";
const always = require("ramda/src/always");
const append = require("ramda/src/append");
const converge = require("ramda/src/converge");
const curry = require("ramda/src/curry");
const drop = require("ramda/src/drop");
const equals = require("ramda/src/equals");
const head = require("ramda/src/head");
const identity = require("ramda/src/identity");
const isNil = require("ramda/src/isNil");
const merge = require("ramda/src/merge");
const nth = require("ramda/src/nth");
const pair = require("ramda/src/pair");
const pipe = require("ramda/src/pipe");
const prepend = require("ramda/src/prepend");
const prop = require("ramda/src/prop");
const when = require("ramda/src/when");
const parse = require("parse-svg-path");
const R = require("ramda"); // Import Ramda
// SET_ABSOLUTE :: String
const SET_ABSOLUTE = "SET_ABSOLUTE";
// SET_RELATIVE :: String
const SET_RELATIVE = "SET_RELATIVE";
// type alias State = { x: String, y: String }
// initialState :: State
const initialState = { x: 0, y: 0 };
// state :: State
let state = Object.assign({}, initialState);
// reducer :: State -> String -> Object -> State
const reducer = curry((a, b, c) => {
switch (b) {
case SET_RELATIVE:
return Object.assign({}, a, {
x: a.x + Number(c.x),
y: a.y + Number(c.y),
});
case SET_ABSOLUTE:
return Object.assign({}, a, {
x: Number(when(isNil, always(a.x), c.x)),
y: Number(when(isNil, always(a.y), c.y)),
});
default:
return a;
}
});
// dispatch :: String -> Object -> State
const dispatch = curry((a, b) => {
state = reducer(state, a, b);
return state;
});
// roundFloat :: Number | String -> Number
const roundFloat = (a) => {
return Number(a * 100).toFixed() / 100;
};
// convertXY :: Array (Number | String) -> Object
const convertXY = (a) => {
const x = Number(nth(0, a));
const y = Number(nth(1, a));
return { x, y };
};
// convertCCXY :: Array (Number | String) -> Object
const convertCCXY = (a) => {
const x = Number(nth(4, a));
const y = Number(nth(5, a));
const cp1x = Number(nth(0, a));
const cp1y = Number(nth(1, a));
const cp2x = Number(nth(2, a));
const cp2y = Number(nth(3, a));
return { x, y, cp1x, cp1y, cp2x, cp2y };
};
// convertQCXY :: Array (Number | String) -> Object
const convertQCXY = (a) => {
const x = Number(nth(2, a));
const y = Number(nth(3, a));
const cpx = Number(nth(0, a));
const cpy = Number(nth(1, a));
return { x, y, cpx, cpy };
};
// convertArcXY :: Array (Number | String) -> Object
const convertArcXY = (a) => {
const x = Number(nth(5, a));
const y = Number(nth(6, a));
const rx = Number(nth(0, a));
const ry = Number(nth(1, a));
const cw = pipe(nth(4), equals(1))(a);
return { x, y, rx, ry, cw };
};
// beginShape :: Nothing -> String
const beginShape = (n) => {
return `struct SVGShape${n}: Shape {
func path(in rect: CGRect) -> Path {
var shape = Path()`;
};
const beginPath = (n) => {
return `let shape${n} = UIBezierPath()`;
};
// endShape :: String
const endShape = ` shape.closeSubpath()
return shape
}\n}`;
// cgPoint :: Object -> String
const cgPoint = (a) => {
return `CGPoint(x: ${roundFloat(a.x)}, y: ${roundFloat(a.y)})`;
};
// convertMove :: String -> String
const convertMove = (a) => {
return `shape.move(to: ${a})`;
};
// convertLine :: String -> String
const convertLine = (a) => {
return `shape.addLine(to: ${a})`;
};
// convertCubicCurve :: Object -> String
const convertCubicCurveU = (a) => {
const anchorPoint = pipe(
converge(pair, [prop("x"), prop("y")]),
convertXY,
cgPoint
)(a);
const controlPointOne = pipe(
converge(pair, [prop("cp1x"), prop("cp1y")]),
convertXY,
cgPoint
)(a);
const controlPointTwo = pipe(
converge(pair, [prop("cp2x"), prop("cp2y")]),
convertXY,
cgPoint
)(a);
return `shape.addCurve(to: ${anchorPoint}, controlPoint1: ${controlPointOne}, controlPoint2: ${controlPointTwo})`;
};
// convertCubicCurve :: Object -> String
const convertCubicCurve = (a) => {
const anchorPoint = pipe(
converge(pair, [prop("x"), prop("y")]),
convertXY,
cgPoint
)(a);
const controlPointOne = pipe(
converge(pair, [prop("cp1x"), prop("cp1y")]),
convertXY,
cgPoint
)(a);
const controlPointTwo = pipe(
converge(pair, [prop("cp2x"), prop("cp2y")]),
convertXY,
cgPoint
)(a);
return `shape.addCurve(to: ${anchorPoint}, control1: ${controlPointOne}, control2: ${controlPointTwo})`;
};
// convertQuadraticCurve :: Object -> String
const convertQuadraticCurve = (a) => {
const anchorPoint = pipe(
converge(pair, [prop("x"), prop("y")]),
convertXY,
cgPoint
)(a);
const controlPoint = pipe(
converge(pair, [prop("cpx"), prop("cpy")]),
convertXY,
cgPoint
)(a);
return `shape.addCurve(to: ${anchorPoint}, controlPoint: ${controlPoint})`;
};
// convertArc :: Object -> String
const convertArc = (a) => {
const anchor = pipe(
converge(pair, [prop("x"), prop("y")]),
convertXY,
cgPoint
)(a);
const radius = pipe(
converge(pair, [prop("rx"), prop("ry")]),
convertXY,
cgPoint
)(a);
const clockwise = prop("cw", a);
const startAngle = 0;
const endAngle = 360;
return `shape.addArc(withCenter: ${anchor}, radius: ${radius}, startAngle: ${startAngle}, endAngle: ${endAngle}, clockwise: ${clockwise})`;
};
// processPathData :: Array (Array (Number | String)) -> String
const processPathData = (a) => {
switch (head(a)) {
case "v":
return pipe(
drop(1),
prepend(0),
convertXY,
dispatch(SET_RELATIVE),
cgPoint,
convertLine
)(a);
case "V":
return pipe(
drop(1),
prepend(null),
convertXY,
dispatch(SET_ABSOLUTE),
cgPoint,
convertLine
)(a);
case "h":
return pipe(
drop(1),
append(0),
convertXY,
dispatch(SET_RELATIVE),
cgPoint,
convertLine
)(a);
case "H":
return pipe(
drop(1),
append(null),
convertXY,
dispatch(SET_ABSOLUTE),
cgPoint,
convertLine
)(a);
case "M":
return pipe(
drop(1),
convertXY,
dispatch(SET_ABSOLUTE),
cgPoint,
convertMove
)(a);
case "l":
return pipe(
drop(1),
convertXY,
dispatch(SET_RELATIVE),
cgPoint,
convertLine
)(a);
case "L":
return pipe(
drop(1),
convertXY,
dispatch(SET_ABSOLUTE),
cgPoint,
convertLine
)(a);
case "c":
return pipe(
drop(1),
convertCCXY,
converge(merge, [identity, dispatch(SET_RELATIVE)]),
convertCubicCurve
)(a);
case "C":
return pipe(
drop(1),
convertCCXY,
converge(merge, [identity, dispatch(SET_ABSOLUTE)]),
convertCubicCurve
)(a);
case "q":
return pipe(
drop(1),
convertQCXY,
converge(merge, [identity, dispatch(SET_RELATIVE)]),
convertQuadraticCurve
)(a);
case "Q":
return pipe(
drop(1),
convertQCXY,
converge(merge, [identity, dispatch(SET_ABSOLUTE)]),
convertQuadraticCurve
)(a);
case "A":
return pipe(
drop(1),
convertArcXY,
converge(merge, [identity, dispatch(SET_ABSOLUTE)]),
convertArc
)(a);
case "Z":
return undefined;
default:
return `// SVG parsing for ${head(a)} data isn't supported yet`;
}
};
// convertPoints :: Array (Array (Number | String)) -> Array String
const convertPoints = (a) => {
return a.map(processPathData);
};
const getPathsByAttribute = (svgText) => {
const parser = new DOMParser();
const svgDoc = parser.parseFromString(svgText, "image/svg+xml");
const pathElements = svgDoc.querySelectorAll(`path[d]`);
return Array.from(pathElements);
};
// swiftvg :: String -> Array String
module.exports = (pathData, mode) => {
let allPaths = getPathsByAttribute(pathData);
const processedResults = R.addIndex(R.map)((pathNode, index) => {
const dAttribute = pathNode.getAttribute("d");
const pathDrawing = pipe(
parse,
convertPoints,
R.reject(R.isNil)
)(dAttribute).map((line) => " " + line);
return pipe(prepend(beginShape(index)), append(endShape))(pathDrawing);
}, allPaths);
return processedResults.map((e) => e.join("\n"));
};
module.exports.SET_ABSOLUTE = SET_ABSOLUTE;
module.exports.SET_RELATIVE = SET_RELATIVE;
module.exports.initialState = initialState;
module.exports.reducer = reducer;
module.exports.dispatch = dispatch;
module.exports.roundFloat = roundFloat;
module.exports.cgPoint = cgPoint;
module.exports.beginShape = beginShape;
module.exports.endShape = endShape;
module.exports.convertXY = convertXY;
module.exports.convertCCXY = convertCCXY;
module.exports.convertQCXY = convertQCXY;
module.exports.convertArcXY = convertArcXY;
module.exports.convertMove = convertMove;
module.exports.convertLine = convertLine;
module.exports.convertCubicCurve = convertCubicCurve;
module.exports.convertQuadraticCurve = convertQuadraticCurve;
module.exports.convertArc = convertArc;
module.exports.processPathData = processPathData;
module.exports.convertPoints = convertPoints;