-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathpath.js
679 lines (616 loc) · 20.2 KB
/
path.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Geometric objects
import BoundingBox from './bbox.js';
/**
* A bézier path containing a set of path commands similar to a SVG path.
* Paths can be drawn on a context using `draw`.
* @exports opentype.Path
* @class
* @constructor
*/
function Path() {
this.commands = [];
this.fill = 'black';
this.stroke = null;
this.strokeWidth = 1;
}
const decimalRoundingCache = {};
function roundDecimal(float, places) {
const integerPart = Math.floor(float);
const decimalPart = float - integerPart;
if (!decimalRoundingCache[places]) {
decimalRoundingCache[places] = {};
}
if (decimalRoundingCache[places][decimalPart] !== undefined) {
const roundedDecimalPart = decimalRoundingCache[places][decimalPart];
return integerPart + roundedDecimalPart;
}
const roundedDecimalPart = +(Math.round(decimalPart + 'e+' + places) + 'e-' + places);
decimalRoundingCache[places][decimalPart] = roundedDecimalPart;
return integerPart + roundedDecimalPart;
}
function optimizeCommands(commands) {
// separate subpaths
let subpaths = [[]];
for (let i = 0; i < commands.length; i += 1) {
const subpath = subpaths[subpaths.length - 1];
const cmd = commands[i];
const firstCommand = subpath[0];
const secondCommand = subpath[1];
const previousCommand = subpath[subpath.length - 1];
subpath.push(cmd);
if (cmd.type === 'Z') {
// When closing at the same position as the path started,
// remove unnecessary line command
if (
firstCommand &&
secondCommand &&
previousCommand &&
firstCommand.type === 'M' &&
secondCommand.type === 'L' &&
previousCommand.type === 'L' &&
previousCommand.x === firstCommand.x &&
previousCommand.y === firstCommand.y
) {
subpath.shift();
subpath[0].type = 'M';
}
if (i + 1 < commands.length) {
subpaths.push([]);
}
} else if (cmd.type === 'L') {
// remove lines that lead to the same position as the previous command
if (previousCommand && previousCommand.x === cmd.x && previousCommand.y === cmd.y) {
subpath.pop();
}
}
}
commands = [].concat.apply([], subpaths); // flatten again
return commands;
}
/**
* Returns options merged with the default options for parsing SVG data
* @param {object} options (optional)
*/
function createSVGParsingOptions(options) {
const defaultOptions = {
decimalPlaces: 2,
optimize: true,
flipY: true,
flipYBase: undefined,
scale: 1,
x: 0,
y: 0
};
const newOptions = Object.assign({}, defaultOptions, options);
return newOptions;
}
/**
* Returns options merged with the default options for outputting SVG data
* @param {object} options (optional)
*/
function createSVGOutputOptions(options) {
// accept number for backwards compatibility
// and in that case set flipY to false
if (parseInt(options) === options) {
options = { decimalPlaces: options, flipY: false };
}
const defaultOptions = {
decimalPlaces: 2,
optimize: true,
flipY: true,
flipYBase: undefined
};
const newOptions = Object.assign({}, defaultOptions, options);
return newOptions;
}
/**
* Sets the path data from an SVG path element or path notation
* @param {string|SVGPathElement}
* @param {object}
*/
Path.prototype.fromSVG = function(pathData, options = {}) {
if (typeof SVGPathElement !== 'undefined' && pathData instanceof SVGPathElement) {
pathData = pathData.getAttribute('d');
}
// set/merge default options
options = createSVGParsingOptions(options);
this.commands = [];
// TODO: a generator function could possibly increase performance and reduce memory usage,
// but our current build process doesn't allow to use those yet.
const number = '0123456789';
const supportedCommands = 'MmLlQqCcZzHhVv';
const unsupportedCommands = 'SsTtAa';
const sign = '-+';
let command = {};
let buffer = [''];
let isUnexpected = false;
function parseBuffer(buffer) {
return buffer.filter(b => b.length).map(b => {
let float = parseFloat(b);
if (options.decimalPlaces || options.decimalPlaces === 0) {
float = roundDecimal(float, options.decimalPlaces);
}
return float;
});
}
function makeRelative(buffer) {
if (!this.commands.length) {
return buffer;
}
const lastCommand = this.commands[this.commands.length - 1];
for (let i = 0; i < buffer.length; i++) {
buffer[i] += lastCommand[i & 1 ? 'y' : 'x'];
}
return buffer;
}
function applyCommand() {
// ignore empty commands
if (command.type === undefined) {
return;
}
const commandType = command.type.toUpperCase();
const relative = commandType !== 'Z' && command.type.toUpperCase() !== command.type;
let parsedBuffer = parseBuffer(buffer);
buffer = [''];
if (!parsedBuffer.length && commandType !== 'Z') {
return;
}
if (relative && commandType !== 'H' && commandType !== 'V') {
parsedBuffer = makeRelative.apply(this, [parsedBuffer]);
}
const currentX = this.commands.length ? this.commands[this.commands.length - 1].x || 0 : 0;
const currentY = this.commands.length ? this.commands[this.commands.length - 1].y || 0 : 0;
switch (commandType) {
case 'M':
this.moveTo(...parsedBuffer);
break;
case 'L':
this.lineTo(...parsedBuffer);
break;
case 'V':
// multiple values interpreted as consecutive commands
for (let i = 0; i < parsedBuffer.length; i++) {
let offset = 0;
if (relative) {
offset = this.commands.length ? (this.commands[this.commands.length - 1].y || 0) : 0;
}
this.lineTo(currentX, parsedBuffer[i] + offset);
}
break;
case 'H':
// multiple values interpreted as consecutive commands
for (let i = 0; i < parsedBuffer.length; i++) {
let offset = 0;
if (relative) {
offset = this.commands.length ? (this.commands[this.commands.length - 1].x || 0) : 0;
}
this.lineTo(parsedBuffer[i] + offset, currentY);
}
break;
case 'C':
this.bezierCurveTo(...parsedBuffer);
break;
case 'Q':
this.quadraticCurveTo(...parsedBuffer);
break;
case 'Z':
if (this.commands.length < 1 || this.commands[this.commands.length - 1].type !== 'Z') {
this.close();
}
break;
}
if (this.commands.length) {
for (const prop in this.commands[this.commands.length - 1]) {
if (this.commands[this.commands.length - 1][prop] === undefined) {
this.commands[this.commands.length - 1][prop] = 0;
}
}
}
}
for (let i = 0; i < pathData.length; i++) {
const token = pathData.charAt(i);
const lastBuffer = buffer[buffer.length - 1];
if (number.indexOf(token) > -1) {
buffer[buffer.length - 1] += token;
} else if (sign.indexOf(token) > -1) {
if (!command.type && !this.commands.length) {
command.type = 'L';
}
if (token === '-') {
if (!command.type || lastBuffer.indexOf('-') > 0) {
isUnexpected = true;
} else if (lastBuffer.length) {
buffer.push('-');
} else {
buffer[buffer.length - 1] = token;
}
} else {
if (!command.type || lastBuffer.length > 0) {
isUnexpected = true;
} else {
continue;
}
}
} else if (supportedCommands.indexOf(token) > -1) {
if (command.type) {
applyCommand.apply(this);
command = { type: token };
} else {
command.type = token;
}
} else if (unsupportedCommands.indexOf(token) > -1) {
// TODO: try to interpolate commands not directly supported?
throw new Error('Unsupported path command: ' + token + '. Currently supported commands are ' + supportedCommands.split('').join(', ') + '.');
} else if (' ,\t\n\r\f\v'.indexOf(token) > -1) {
buffer.push('');
} else if (token === '.') {
if (!command.type || lastBuffer.indexOf(token) > -1) {
isUnexpected = true;
} else {
buffer[buffer.length - 1] += token;
}
} else {
isUnexpected = true;
}
if (isUnexpected) {
throw new Error('Unexpected character: ' + token + ' at offset ' + i);
}
}
applyCommand.apply(this);
if (options.optimize) {
this.commands = optimizeCommands(this.commands);
}
const flipY = options.flipY;
let flipYBase = options.flipYBase;
if (flipY === true && options.flipYBase === undefined) {
const boundingBox = this.getBoundingBox();
flipYBase = boundingBox.y1 + boundingBox.y2;
}
// apply x/y offset, flipping and scaling
for (const i in this.commands) {
const cmd = this.commands[i];
for (const prop in cmd) {
if (['x', 'x1', 'x2'].includes(prop)) {
this.commands[i][prop] = options.x + cmd[prop] * options.scale;
} else if (['y', 'y1', 'y2'].includes(prop)) {
this.commands[i][prop] = options.y + (flipY ? flipYBase - cmd[prop] : cmd[prop]) * options.scale;
}
}
}
return this;
};
/**
* Generates a new Path() from an SVG path element or path notation
* @param {string|SVGPathElement}
* @param {object}
*/
Path.fromSVG = function(path, options) {
const newPath = new Path();
return newPath.fromSVG(path, options);
};
/**
* @param {number} x
* @param {number} y
*/
Path.prototype.moveTo = function(x, y) {
this.commands.push({
type: 'M',
x: x,
y: y
});
};
/**
* @param {number} x
* @param {number} y
*/
Path.prototype.lineTo = function(x, y) {
this.commands.push({
type: 'L',
x: x,
y: y
});
};
/**
* Draws cubic curve
* @function
* curveTo
* @memberof opentype.Path.prototype
* @param {number} x1 - x of control 1
* @param {number} y1 - y of control 1
* @param {number} x2 - x of control 2
* @param {number} y2 - y of control 2
* @param {number} x - x of path point
* @param {number} y - y of path point
*/
/**
* Draws cubic curve
* @function
* bezierCurveTo
* @memberof opentype.Path.prototype
* @param {number} x1 - x of control 1
* @param {number} y1 - y of control 1
* @param {number} x2 - x of control 2
* @param {number} y2 - y of control 2
* @param {number} x - x of path point
* @param {number} y - y of path point
* @see curveTo
*/
Path.prototype.curveTo = Path.prototype.bezierCurveTo = function(x1, y1, x2, y2, x, y) {
this.commands.push({
type: 'C',
x1: x1,
y1: y1,
x2: x2,
y2: y2,
x: x,
y: y
});
};
/**
* Draws quadratic curve
* @function
* quadraticCurveTo
* @memberof opentype.Path.prototype
* @param {number} x1 - x of control
* @param {number} y1 - y of control
* @param {number} x - x of path point
* @param {number} y - y of path point
*/
/**
* Draws quadratic curve
* @function
* quadTo
* @memberof opentype.Path.prototype
* @param {number} x1 - x of control
* @param {number} y1 - y of control
* @param {number} x - x of path point
* @param {number} y - y of path point
*/
Path.prototype.quadTo = Path.prototype.quadraticCurveTo = function(x1, y1, x, y) {
this.commands.push({
type: 'Q',
x1: x1,
y1: y1,
x: x,
y: y
});
};
/**
* Closes the path
* @function closePath
* @memberof opentype.Path.prototype
*/
/**
* Close the path
* @function close
* @memberof opentype.Path.prototype
*/
Path.prototype.close = Path.prototype.closePath = function() {
this.commands.push({
type: 'Z'
});
};
/**
* Add the given path or list of commands to the commands of this path.
* @param {Array} pathOrCommands - another opentype.Path, an opentype.BoundingBox, or an array of commands.
*/
Path.prototype.extend = function(pathOrCommands) {
if (pathOrCommands.commands) {
pathOrCommands = pathOrCommands.commands;
} else if (pathOrCommands instanceof BoundingBox) {
const box = pathOrCommands;
this.moveTo(box.x1, box.y1);
this.lineTo(box.x2, box.y1);
this.lineTo(box.x2, box.y2);
this.lineTo(box.x1, box.y2);
this.close();
return;
}
Array.prototype.push.apply(this.commands, pathOrCommands);
};
/**
* Calculate the bounding box of the path.
* @returns {opentype.BoundingBox}
*/
Path.prototype.getBoundingBox = function() {
const box = new BoundingBox();
let startX = 0;
let startY = 0;
let prevX = 0;
let prevY = 0;
for (let i = 0; i < this.commands.length; i++) {
const cmd = this.commands[i];
switch (cmd.type) {
case 'M':
box.addPoint(cmd.x, cmd.y);
startX = prevX = cmd.x;
startY = prevY = cmd.y;
break;
case 'L':
box.addPoint(cmd.x, cmd.y);
prevX = cmd.x;
prevY = cmd.y;
break;
case 'Q':
box.addQuad(prevX, prevY, cmd.x1, cmd.y1, cmd.x, cmd.y);
prevX = cmd.x;
prevY = cmd.y;
break;
case 'C':
box.addBezier(prevX, prevY, cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);
prevX = cmd.x;
prevY = cmd.y;
break;
case 'Z':
prevX = startX;
prevY = startY;
break;
default:
throw new Error('Unexpected path command ' + cmd.type);
}
}
if (box.isEmpty()) {
box.addPoint(0, 0);
}
return box;
};
/**
* Draw the path to a 2D context.
* @param {CanvasRenderingContext2D} ctx - A 2D drawing context.
*/
Path.prototype.draw = function(ctx) {
ctx.beginPath();
for (let i = 0; i < this.commands.length; i += 1) {
const cmd = this.commands[i];
if (cmd.type === 'M') {
ctx.moveTo(cmd.x, cmd.y);
} else if (cmd.type === 'L') {
ctx.lineTo(cmd.x, cmd.y);
} else if (cmd.type === 'C') {
ctx.bezierCurveTo(cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);
} else if (cmd.type === 'Q') {
ctx.quadraticCurveTo(cmd.x1, cmd.y1, cmd.x, cmd.y);
} else if (cmd.type === 'Z') {
ctx.closePath();
}
}
if (this.fill) {
ctx.fillStyle = this.fill;
ctx.fill();
}
if (this.stroke) {
ctx.strokeStyle = this.stroke;
ctx.lineWidth = this.strokeWidth;
ctx.stroke();
}
};
/**
* Convert the Path to a string of path data instructions
* See http://www.w3.org/TR/SVG/paths.html#PathData
* @param {object|number} [options={decimalPlaces:2, optimize:true}] - Options object (or amount of decimal places for floating-point values for backwards compatibility)
* @return {string}
*/
Path.prototype.toPathData = function(options) {
// set/merge default options
options = createSVGOutputOptions(options);
function floatToString(v) {
const rounded = roundDecimal(v, options.decimalPlaces);
if (Math.round(v) === rounded) {
return '' + rounded;
} else {
return rounded.toFixed(options.decimalPlaces);
}
}
function packValues() {
let s = '';
for (let i = 0; i < arguments.length; i += 1) {
const v = arguments[i];
if (v >= 0 && i > 0) {
s += ' ';
}
s += floatToString(v);
}
return s;
}
let commandsCopy = this.commands;
if (options.optimize) {
// apply path optimizations
commandsCopy = JSON.parse(JSON.stringify(this.commands)); // make a deep clone
commandsCopy = optimizeCommands(commandsCopy);
}
const flipY = options.flipY;
let flipYBase = options.flipYBase;
if (flipY === true && flipYBase === undefined) {
const tempPath = new Path();
tempPath.extend(commandsCopy);
const boundingBox = tempPath.getBoundingBox();
flipYBase = boundingBox.y1 + boundingBox.y2;
}
let d = '';
for (let i = 0; i < commandsCopy.length; i += 1) {
const cmd = commandsCopy[i];
if (cmd.type === 'M') {
d += 'M' + packValues(
cmd.x,
flipY ? flipYBase - cmd.y : cmd.y
);
} else if (cmd.type === 'L') {
d += 'L' + packValues(
cmd.x,
flipY ? flipYBase - cmd.y : cmd.y
);
} else if (cmd.type === 'C') {
d += 'C' + packValues(
cmd.x1,
flipY ? flipYBase - cmd.y1 : cmd.y1,
cmd.x2,
flipY ? flipYBase - cmd.y2 : cmd.y2,
cmd.x,
flipY ? flipYBase - cmd.y : cmd.y
);
} else if (cmd.type === 'Q') {
d += 'Q' + packValues(
cmd.x1,
flipY ? flipYBase - cmd.y1 : cmd.y1,
cmd.x,
flipY ? flipYBase - cmd.y : cmd.y
);
} else if (cmd.type === 'Z') {
d += 'Z';
}
}
return d;
};
/**
* Convert the path to an SVG <path> element, as a string.
* @param {object|number} [options={decimalPlaces:2, optimize:true}] - Options object (or amount of decimal places for floating-point values for backwards compatibility)
* @param {string} - will be calculated automatically, but can be provided from Glyph's wrapper function
* @return {string}
*/
Path.prototype.toSVG = function(options, pathData) {
if (!pathData) {
pathData = this.toPathData(options);
}
let svg = '<path d="';
svg += pathData;
svg += '"';
if (this.fill !== undefined && this.fill !== 'black') {
if (this.fill === null) {
svg += ' fill="none"';
} else {
svg += ' fill="' + this.fill + '"';
}
}
if (this.stroke) {
svg += ' stroke="' + this.stroke + '" stroke-width="' + this.strokeWidth + '"';
}
svg += '/>';
return svg;
};
/**
* Convert the path to a DOM element.
* @param {object|number} [options={decimalPlaces:2, optimize:true}] - Options object (or amount of decimal places for floating-point values for backwards compatibility)
* @param {string} - will be calculated automatically, but can be provided from Glyph's wrapper functionions object (or amount of decimal places for floating-point values for backwards compatibility)
* @return {SVGPathElement}
*/
Path.prototype.toDOMElement = function(options, pathData) {
if (!pathData) {
pathData = this.toPathData(options);
}
const temporaryPath = pathData;
const newPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
newPath.setAttribute('d', temporaryPath);
if (this.fill !== undefined && this.fill !== 'black') {
if (this.fill === null) {
newPath.setAttribute('fill', 'none');
} else {
newPath.setAttribute('fill', this.fill);
}
}
if (this.stroke) {
newPath.setAttribute('stroke', this.stroke);
newPath.setAttribute('stroke-width', this.strokeWidth);
}
return newPath;
};
export default Path;