svg-path-interpolator 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -22
- package/bin/svgpi.mjs +46 -0
- package/lib/SVGPathInterpolator.js +309 -0
- package/lib/index.js +19 -0
- package/lib/math/SVGTransform.js +201 -0
- package/lib/math/calculators.js +271 -0
- package/lib/math/utils.js +30 -0
- package/lib/sax-wasm/lib/index.js +304 -0
- package/lib/sax-wasm/lib/module/index.js +293 -0
- package/lib/sax-wasm/lib/sax-wasm.wasm +0 -0
- package/lib/sax-wasm/lib/umd/index.js +310 -0
- package/package.json +26 -6
- package/bin/svgpi.js +0 -49
- package/src/SVGPathInterpolator.js +0 -325
- package/src/math/SVGTransform.js +0 -236
- package/src/math/calculators.js +0 -312
- package/src/math/utils.js +0 -40
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["sax-wasm"] = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
class SaxEventType {
|
|
8
|
+
// 1
|
|
9
|
+
static Text = 0b1;
|
|
10
|
+
// 2
|
|
11
|
+
static ProcessingInstruction = 0b10;
|
|
12
|
+
// 4
|
|
13
|
+
static SGMLDeclaration = 0b100;
|
|
14
|
+
// 8
|
|
15
|
+
static Doctype = 0b1000;
|
|
16
|
+
// 16
|
|
17
|
+
static Comment = 0b10000;
|
|
18
|
+
// 32
|
|
19
|
+
static OpenTagStart = 0b100000;
|
|
20
|
+
// 64
|
|
21
|
+
static Attribute = 0b1000000;
|
|
22
|
+
// 128
|
|
23
|
+
static OpenTag = 0b10000000;
|
|
24
|
+
// 256
|
|
25
|
+
static CloseTag = 0b100000000;
|
|
26
|
+
// 512
|
|
27
|
+
static Cdata = 0b1000000000;
|
|
28
|
+
}
|
|
29
|
+
class Reader {
|
|
30
|
+
data;
|
|
31
|
+
cache = {};
|
|
32
|
+
ptr;
|
|
33
|
+
constructor(data, ptr = 0) {
|
|
34
|
+
this.data = data;
|
|
35
|
+
this.ptr = ptr;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
class Position {
|
|
39
|
+
line;
|
|
40
|
+
character;
|
|
41
|
+
constructor(line, character) {
|
|
42
|
+
this.line = line;
|
|
43
|
+
this.character = character;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.AttributeType = void 0;
|
|
47
|
+
(function (AttributeType) {
|
|
48
|
+
AttributeType[AttributeType["Normal"] = 0] = "Normal";
|
|
49
|
+
AttributeType[AttributeType["JSX"] = 1] = "JSX";
|
|
50
|
+
})(exports.AttributeType || (exports.AttributeType = {}));
|
|
51
|
+
class Attribute extends Reader {
|
|
52
|
+
type;
|
|
53
|
+
name;
|
|
54
|
+
value;
|
|
55
|
+
constructor(buffer, ptr = 0) {
|
|
56
|
+
super(buffer, ptr);
|
|
57
|
+
this.type = buffer[ptr];
|
|
58
|
+
ptr += 1;
|
|
59
|
+
const len = readU32(buffer, ptr);
|
|
60
|
+
ptr += 4;
|
|
61
|
+
this.name = new Text(buffer, ptr);
|
|
62
|
+
ptr += len;
|
|
63
|
+
this.value = new Text(buffer, ptr);
|
|
64
|
+
}
|
|
65
|
+
toJSON() {
|
|
66
|
+
const { name, value, type } = this;
|
|
67
|
+
return { name, value, type };
|
|
68
|
+
}
|
|
69
|
+
toString() {
|
|
70
|
+
const { name, value } = this;
|
|
71
|
+
return `${name}="${value}"`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
class ProcInst extends Reader {
|
|
75
|
+
target;
|
|
76
|
+
content;
|
|
77
|
+
constructor(buffer, ptr = 0) {
|
|
78
|
+
super(buffer, ptr);
|
|
79
|
+
ptr += 16;
|
|
80
|
+
const len = readU32(buffer, ptr);
|
|
81
|
+
ptr += 4;
|
|
82
|
+
this.target = new Text(buffer, ptr);
|
|
83
|
+
ptr += len;
|
|
84
|
+
this.content = new Text(buffer, ptr);
|
|
85
|
+
}
|
|
86
|
+
get start() {
|
|
87
|
+
return this.cache.start || (this.cache.start = readPosition(this.data, this.ptr));
|
|
88
|
+
}
|
|
89
|
+
get end() {
|
|
90
|
+
return this.cache.end || (this.cache.end = readPosition(this.data, this.ptr + 8));
|
|
91
|
+
}
|
|
92
|
+
toJSON() {
|
|
93
|
+
const { start, end, target, content } = this;
|
|
94
|
+
return { start, end, target, content };
|
|
95
|
+
}
|
|
96
|
+
toString() {
|
|
97
|
+
const { target, content } = this;
|
|
98
|
+
return `<? ${target} ${content} ?>`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
class Text extends Reader {
|
|
102
|
+
get start() {
|
|
103
|
+
return this.cache.start || (this.cache.start = readPosition(this.data, this.ptr));
|
|
104
|
+
}
|
|
105
|
+
get end() {
|
|
106
|
+
return this.cache.end || (this.cache.end = readPosition(this.data, this.ptr + 8));
|
|
107
|
+
}
|
|
108
|
+
get value() {
|
|
109
|
+
if (this.cache.value) {
|
|
110
|
+
return this.cache.value;
|
|
111
|
+
}
|
|
112
|
+
const valueLen = readU32(this.data, this.ptr + 16);
|
|
113
|
+
return (this.cache.value = readString(this.data, this.ptr + 20, valueLen));
|
|
114
|
+
}
|
|
115
|
+
toJSON() {
|
|
116
|
+
const { start, end, value } = this;
|
|
117
|
+
return { start, end, value };
|
|
118
|
+
}
|
|
119
|
+
toString() {
|
|
120
|
+
return this.value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
class Tag extends Reader {
|
|
124
|
+
get openStart() {
|
|
125
|
+
return this.cache.openStart || (this.cache.openStart = readPosition(this.data, this.ptr + 8));
|
|
126
|
+
}
|
|
127
|
+
get openEnd() {
|
|
128
|
+
return this.cache.openEnd || (this.cache.openEnd = readPosition(this.data, this.ptr + 16));
|
|
129
|
+
}
|
|
130
|
+
get closeStart() {
|
|
131
|
+
return this.cache.closeStart || (this.cache.closeStart = readPosition(this.data, this.ptr + 24));
|
|
132
|
+
}
|
|
133
|
+
get closeEnd() {
|
|
134
|
+
return this.cache.closeEnd || (this.cache.closeEnd = readPosition(this.data, this.ptr + 32));
|
|
135
|
+
}
|
|
136
|
+
get selfClosing() {
|
|
137
|
+
return !!this.data[this.ptr + 40];
|
|
138
|
+
}
|
|
139
|
+
get name() {
|
|
140
|
+
if (this.cache.name) {
|
|
141
|
+
return this.cache.name;
|
|
142
|
+
}
|
|
143
|
+
const nameLen = readU32(this.data, this.ptr + 41);
|
|
144
|
+
return (this.cache.name = readString(this.data, this.ptr + 45, nameLen));
|
|
145
|
+
}
|
|
146
|
+
get attributes() {
|
|
147
|
+
if (this.cache.attributes) {
|
|
148
|
+
return this.cache.attributes;
|
|
149
|
+
}
|
|
150
|
+
// starting location of the attribute block
|
|
151
|
+
let ptr = readU32(this.data, this.ptr);
|
|
152
|
+
const numAttrs = readU32(this.data, ptr);
|
|
153
|
+
ptr += 4;
|
|
154
|
+
const attributes = [];
|
|
155
|
+
for (let i = 0; i < numAttrs; i++) {
|
|
156
|
+
const attrLen = readU32(this.data, ptr);
|
|
157
|
+
ptr += 4;
|
|
158
|
+
attributes[i] = new Attribute(this.data, ptr);
|
|
159
|
+
ptr += attrLen;
|
|
160
|
+
}
|
|
161
|
+
return (this.cache.attributes = attributes);
|
|
162
|
+
}
|
|
163
|
+
get textNodes() {
|
|
164
|
+
if (this.cache.textNodes) {
|
|
165
|
+
return this.cache.textNodes;
|
|
166
|
+
}
|
|
167
|
+
// starting location of the text nodes block
|
|
168
|
+
let ptr = readU32(this.data, this.ptr + 4);
|
|
169
|
+
const numTextNodes = readU32(this.data, ptr);
|
|
170
|
+
const textNodes = [];
|
|
171
|
+
ptr += 4;
|
|
172
|
+
for (let i = 0; i < numTextNodes; i++) {
|
|
173
|
+
const textLen = readU32(this.data, ptr);
|
|
174
|
+
ptr += 4;
|
|
175
|
+
textNodes[i] = new Text(this.data, ptr);
|
|
176
|
+
ptr += textLen;
|
|
177
|
+
}
|
|
178
|
+
return (this.cache.textNodes = textNodes);
|
|
179
|
+
}
|
|
180
|
+
toJSON() {
|
|
181
|
+
const { openStart, openEnd, closeStart, closeEnd, name, attributes, textNodes, selfClosing } = this;
|
|
182
|
+
return { openStart, openEnd, closeStart, closeEnd, name, attributes, textNodes, selfClosing };
|
|
183
|
+
}
|
|
184
|
+
get value() {
|
|
185
|
+
return this.name;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
class SAXParser {
|
|
189
|
+
static textDecoder; // Web only
|
|
190
|
+
events;
|
|
191
|
+
wasmSaxParser;
|
|
192
|
+
eventHandler;
|
|
193
|
+
options;
|
|
194
|
+
writeBuffer;
|
|
195
|
+
constructor(events = 0, options = { highWaterMark: 32 * 1024 }) {
|
|
196
|
+
this.options = options;
|
|
197
|
+
const self = this;
|
|
198
|
+
Object.defineProperties(this, {
|
|
199
|
+
events: {
|
|
200
|
+
get: () => ~~events,
|
|
201
|
+
set: (value) => {
|
|
202
|
+
events = ~~value;
|
|
203
|
+
if (self.wasmSaxParser) {
|
|
204
|
+
self.wasmSaxParser.parser(events);
|
|
205
|
+
}
|
|
206
|
+
}, configurable: false, enumerable: true
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
write(chunk) {
|
|
211
|
+
if (!this.wasmSaxParser) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const { write, memory } = this.wasmSaxParser;
|
|
215
|
+
// Allocations within the WASM process
|
|
216
|
+
// invalidate reference to the memory buffer.
|
|
217
|
+
// We check for this and create a new Uint8Array
|
|
218
|
+
// with the new memory buffer reference if needed.
|
|
219
|
+
// **NOTE** These allocations can slow down parsing
|
|
220
|
+
// if they become excessive. Consider adjusting the
|
|
221
|
+
// highWaterMark in the options up or down to find the optimal
|
|
222
|
+
// memory allocation to prevent too many new Uint8Array instances.
|
|
223
|
+
if (!this.writeBuffer || this.writeBuffer.buffer !== memory.buffer) {
|
|
224
|
+
this.writeBuffer = new Uint8Array(memory.buffer);
|
|
225
|
+
}
|
|
226
|
+
this.writeBuffer.set(chunk, 0);
|
|
227
|
+
write(0, chunk.byteLength);
|
|
228
|
+
}
|
|
229
|
+
end() {
|
|
230
|
+
this.writeBuffer = undefined;
|
|
231
|
+
this.wasmSaxParser?.end();
|
|
232
|
+
}
|
|
233
|
+
async prepareWasm(saxWasm) {
|
|
234
|
+
const result = await WebAssembly.instantiate(saxWasm, {
|
|
235
|
+
env: {
|
|
236
|
+
memoryBase: 0,
|
|
237
|
+
tableBase: 0,
|
|
238
|
+
memory: new WebAssembly.Memory({ initial: 10 }),
|
|
239
|
+
table: new WebAssembly.Table({ initial: 1, element: 'anyfunc' }),
|
|
240
|
+
event_listener: this.eventTrap
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
if (result && typeof this.events === 'number') {
|
|
244
|
+
const { parser } = this.wasmSaxParser = result.instance.exports;
|
|
245
|
+
parser(this.events);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
throw new Error(`Failed to instantiate the parser.`);
|
|
249
|
+
}
|
|
250
|
+
eventTrap = (event, ptr, len) => {
|
|
251
|
+
if (!this.wasmSaxParser) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const uint8array = new Uint8Array(this.wasmSaxParser.memory.buffer, ptr, len).slice();
|
|
255
|
+
let detail;
|
|
256
|
+
switch (event) {
|
|
257
|
+
case SaxEventType.Attribute:
|
|
258
|
+
detail = new Attribute(uint8array);
|
|
259
|
+
break;
|
|
260
|
+
case SaxEventType.ProcessingInstruction:
|
|
261
|
+
detail = new ProcInst(uint8array);
|
|
262
|
+
break;
|
|
263
|
+
case SaxEventType.OpenTag:
|
|
264
|
+
case SaxEventType.CloseTag:
|
|
265
|
+
case SaxEventType.OpenTagStart:
|
|
266
|
+
detail = new Tag(uint8array);
|
|
267
|
+
break;
|
|
268
|
+
case SaxEventType.Text:
|
|
269
|
+
case SaxEventType.Cdata:
|
|
270
|
+
case SaxEventType.Comment:
|
|
271
|
+
case SaxEventType.Doctype:
|
|
272
|
+
case SaxEventType.SGMLDeclaration:
|
|
273
|
+
detail = new Text(uint8array);
|
|
274
|
+
break;
|
|
275
|
+
default:
|
|
276
|
+
throw new Error('No reader for this event type');
|
|
277
|
+
}
|
|
278
|
+
if (this.eventHandler) {
|
|
279
|
+
this.eventHandler(event, detail);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
const readString = (data, offset, length) => {
|
|
284
|
+
// Node
|
|
285
|
+
if (globalThis.hasOwnProperty('Buffer')) {
|
|
286
|
+
return Buffer.from(data.buffer, data.byteOffset + offset, length).toString();
|
|
287
|
+
}
|
|
288
|
+
// Web
|
|
289
|
+
return (SAXParser.textDecoder || (SAXParser.textDecoder = new TextDecoder()))
|
|
290
|
+
.decode(data.subarray(offset, offset + length));
|
|
291
|
+
};
|
|
292
|
+
const readU32 = (uint8Array, ptr) => (uint8Array[ptr + 3] << 24) | (uint8Array[ptr + 2] << 16) | (uint8Array[ptr + 1] << 8) | uint8Array[ptr];
|
|
293
|
+
const readPosition = (uint8Array, ptr = 0) => {
|
|
294
|
+
const line = readU32(uint8Array, ptr);
|
|
295
|
+
const character = readU32(uint8Array, ptr + 4);
|
|
296
|
+
return new Position(line, character);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
exports.Attribute = Attribute;
|
|
300
|
+
exports.Position = Position;
|
|
301
|
+
exports.ProcInst = ProcInst;
|
|
302
|
+
exports.Reader = Reader;
|
|
303
|
+
exports.SAXParser = SAXParser;
|
|
304
|
+
exports.SaxEventType = SaxEventType;
|
|
305
|
+
exports.Tag = Tag;
|
|
306
|
+
exports.Text = Text;
|
|
307
|
+
|
|
308
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
309
|
+
|
|
310
|
+
}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Justin Wilaby",
|
|
3
3
|
"name": "svg-path-interpolator",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "The SVG Path Interpolator produces point data representing interpolated values within an SVG path.",
|
|
7
7
|
"repository": {
|
|
@@ -9,13 +9,26 @@
|
|
|
9
9
|
"url": "https://github.com/justinwilaby/svg-path-interpolator.git"
|
|
10
10
|
},
|
|
11
11
|
"bin": {
|
|
12
|
-
"svgpi": "./bin/svgpi
|
|
12
|
+
"svgpi": "./bin/svgpi"
|
|
13
13
|
},
|
|
14
|
-
"
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=14.x"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"module": "./lib/index.js",
|
|
21
|
+
"import": "./lib/index.js",
|
|
22
|
+
"default": "./lib/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./*": "./lib/*"
|
|
25
|
+
},
|
|
26
|
+
"main": "./lib/index.js",
|
|
27
|
+
"browser": "./lib/index.js",
|
|
15
28
|
"files": [
|
|
16
|
-
"src",
|
|
17
29
|
"bin",
|
|
18
|
-
"config"
|
|
30
|
+
"config",
|
|
31
|
+
"lib"
|
|
19
32
|
],
|
|
20
33
|
"keywords": [
|
|
21
34
|
"svg",
|
|
@@ -30,7 +43,14 @@
|
|
|
30
43
|
"curves",
|
|
31
44
|
"polygon"
|
|
32
45
|
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "node import-path-plugin.mjs && node copy-deps.mjs"
|
|
48
|
+
},
|
|
33
49
|
"dependencies": {
|
|
34
|
-
"sax": "
|
|
50
|
+
"sax-wasm": "2.2.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"glob": "^8.0.3",
|
|
54
|
+
"typescript": "^4.7.2"
|
|
35
55
|
}
|
|
36
56
|
}
|
package/bin/svgpi.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
//https://pomax.github.io/bezierinfo/
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
|
|
6
|
-
const SVGPathInterpolator = require('../src/SVGPathInterpolator');
|
|
7
|
-
|
|
8
|
-
const configPath = process.argv[2];
|
|
9
|
-
const svgFile = process.argv[3];
|
|
10
|
-
const outputFile = process.argv[4];
|
|
11
|
-
|
|
12
|
-
function read(file) {
|
|
13
|
-
return new Promise((resolve, reject) => {
|
|
14
|
-
fs.access(file, fs.R_OK, (error) => {
|
|
15
|
-
if (error) {
|
|
16
|
-
console.log(error);
|
|
17
|
-
process.exit(1);
|
|
18
|
-
}
|
|
19
|
-
fs.readFile(file, 'utf8', (error, data) => {
|
|
20
|
-
if (data) {
|
|
21
|
-
resolve(data);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
reject(error);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function runJob(configJson) {
|
|
32
|
-
const config = JSON.parse(configJson) || process.exit(1);
|
|
33
|
-
const file = path.normalize(svgFile);
|
|
34
|
-
read(file).then(svg => {
|
|
35
|
-
const json = new SVGPathInterpolator(config).processSvg(svg);
|
|
36
|
-
const jsonStr = JSON.stringify(json, null, (config.pretty ? config.prettyIndent : 0));
|
|
37
|
-
if (outputFile) {
|
|
38
|
-
fs.writeFile(path.normalize(outputFile), jsonStr, err => {
|
|
39
|
-
process.exit(err);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
console.log(jsonStr);
|
|
44
|
-
process.exit(0);
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
read(configPath).then(runJob);
|