@glissade/svg 0.6.0-pre.1
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/LICENSE +202 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +606 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Group, PathSeg, SceneModule } from "@glissade/scene";
|
|
2
|
+
|
|
3
|
+
//#region src/parser.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parse an SVG path `d` string into `PathSeg[]`. Unknown/degenerate inputs are
|
|
7
|
+
* skipped gracefully rather than throwing.
|
|
8
|
+
*/
|
|
9
|
+
declare function parseSvgPath(d: string): PathSeg[];
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/xml.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* A tiny XML reader for the SVG subset — elements, attributes (either quote
|
|
14
|
+
* style), nesting, self-closing tags. Comments / declarations / doctype are
|
|
15
|
+
* stripped; text content is ignored (glissade SVG import is shape-only). Not a
|
|
16
|
+
* general XML parser; just enough to walk an SVG's element tree.
|
|
17
|
+
*/
|
|
18
|
+
interface XmlNode {
|
|
19
|
+
tag: string;
|
|
20
|
+
attrs: Record<string, string>;
|
|
21
|
+
children: XmlNode[];
|
|
22
|
+
}
|
|
23
|
+
/** Parse SVG source into its root element tree, or null if there's no element. */
|
|
24
|
+
declare function parseXml(src: string): XmlNode | null;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/convert.d.ts
|
|
27
|
+
interface ConvertResult {
|
|
28
|
+
size: {
|
|
29
|
+
w: number;
|
|
30
|
+
h: number;
|
|
31
|
+
};
|
|
32
|
+
root: Group;
|
|
33
|
+
warnings: string[];
|
|
34
|
+
}
|
|
35
|
+
/** Convert a parsed SVG root element into a scene-ready Group + size. */
|
|
36
|
+
declare function convertSvg(svg: XmlNode): ConvertResult;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/index.d.ts
|
|
39
|
+
declare class SvgImportError extends Error {
|
|
40
|
+
constructor(message: string);
|
|
41
|
+
}
|
|
42
|
+
interface SvgImportResult {
|
|
43
|
+
/** Intrinsic size from width/height or viewBox (falls back to 100×100). */
|
|
44
|
+
size: {
|
|
45
|
+
w: number;
|
|
46
|
+
h: number;
|
|
47
|
+
};
|
|
48
|
+
/** The imported tree as a single Group node (positioned in SVG user units). */
|
|
49
|
+
root: Group;
|
|
50
|
+
/** Non-fatal feature gaps (dropped elements, unsupported paint/transform). */
|
|
51
|
+
warnings: string[];
|
|
52
|
+
/** Wrap the import as a static, renderable SceneModule. */
|
|
53
|
+
toSceneModule(): SceneModule;
|
|
54
|
+
}
|
|
55
|
+
/** Parse an SVG string and convert it to a glissade scene. Throws on no `<svg>`. */
|
|
56
|
+
declare function importSvg(svg: string): SvgImportResult;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { type ConvertResult, SvgImportError, SvgImportResult, type XmlNode, convertSvg, importSvg, parseSvgPath, parseXml };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { timeline } from "@glissade/core";
|
|
2
|
+
import { Circle, Group, Path, Rect, createScene, pathFromSegs } from "@glissade/scene";
|
|
3
|
+
//#region src/parser.ts
|
|
4
|
+
const TOKEN = /([MmLlHhVvCcSsQqTtAaZz])|(-?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?)/g;
|
|
5
|
+
function tokenize(d) {
|
|
6
|
+
const out = [];
|
|
7
|
+
let m;
|
|
8
|
+
TOKEN.lastIndex = 0;
|
|
9
|
+
while ((m = TOKEN.exec(d)) !== null) if (m[1] !== void 0) out.push({ cmd: m[1] });
|
|
10
|
+
else out.push({ num: parseFloat(m[2]) });
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
/** SVG endpoint-arc → glissade 'E' segment (center parameterization). */
|
|
14
|
+
function arcToE(x1, y1, rx, ry, phiDeg, large, sweep, x2, y2) {
|
|
15
|
+
if (rx === 0 || ry === 0) return null;
|
|
16
|
+
rx = Math.abs(rx);
|
|
17
|
+
ry = Math.abs(ry);
|
|
18
|
+
const phi = phiDeg * Math.PI / 180;
|
|
19
|
+
const cosP = Math.cos(phi);
|
|
20
|
+
const sinP = Math.sin(phi);
|
|
21
|
+
const dx = (x1 - x2) / 2;
|
|
22
|
+
const dy = (y1 - y2) / 2;
|
|
23
|
+
const x1p = cosP * dx + sinP * dy;
|
|
24
|
+
const y1p = -sinP * dx + cosP * dy;
|
|
25
|
+
const lambda = x1p * x1p / (rx * rx) + y1p * y1p / (ry * ry);
|
|
26
|
+
if (lambda > 1) {
|
|
27
|
+
const s = Math.sqrt(lambda);
|
|
28
|
+
rx *= s;
|
|
29
|
+
ry *= s;
|
|
30
|
+
}
|
|
31
|
+
const sign = large !== sweep ? 1 : -1;
|
|
32
|
+
const num = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
|
33
|
+
const den = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
|
|
34
|
+
const co = sign * Math.sqrt(Math.max(0, num / den));
|
|
35
|
+
const cxp = co * (rx * y1p) / ry;
|
|
36
|
+
const cyp = co * -(ry * x1p) / rx;
|
|
37
|
+
const cx = cosP * cxp - sinP * cyp + (x1 + x2) / 2;
|
|
38
|
+
const cy = sinP * cxp + cosP * cyp + (y1 + y2) / 2;
|
|
39
|
+
const ang = (ux, uy, vx, vy) => {
|
|
40
|
+
const dot = ux * vx + uy * vy;
|
|
41
|
+
const len = Math.hypot(ux, uy) * Math.hypot(vx, vy);
|
|
42
|
+
let a = Math.acos(Math.min(1, Math.max(-1, dot / len)));
|
|
43
|
+
if (ux * vy - uy * vx < 0) a = -a;
|
|
44
|
+
return a;
|
|
45
|
+
};
|
|
46
|
+
const theta1 = ang(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
47
|
+
let dTheta = ang((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
|
|
48
|
+
if (sweep === 0 && dTheta > 0) dTheta -= 2 * Math.PI;
|
|
49
|
+
else if (sweep === 1 && dTheta < 0) dTheta += 2 * Math.PI;
|
|
50
|
+
return [
|
|
51
|
+
"E",
|
|
52
|
+
cx,
|
|
53
|
+
cy,
|
|
54
|
+
rx,
|
|
55
|
+
ry,
|
|
56
|
+
phi,
|
|
57
|
+
theta1,
|
|
58
|
+
theta1 + dTheta
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parse an SVG path `d` string into `PathSeg[]`. Unknown/degenerate inputs are
|
|
63
|
+
* skipped gracefully rather than throwing.
|
|
64
|
+
*/
|
|
65
|
+
function parseSvgPath(d) {
|
|
66
|
+
const toks = tokenize(d);
|
|
67
|
+
const segs = [];
|
|
68
|
+
let i = 0;
|
|
69
|
+
let cx = 0;
|
|
70
|
+
let cy = 0;
|
|
71
|
+
let sx = 0;
|
|
72
|
+
let sy = 0;
|
|
73
|
+
let lastCtrlX = 0;
|
|
74
|
+
let lastCtrlY = 0;
|
|
75
|
+
let prevCmd = "";
|
|
76
|
+
const num = () => {
|
|
77
|
+
const t = toks[i++];
|
|
78
|
+
return t && "num" in t ? t.num : NaN;
|
|
79
|
+
};
|
|
80
|
+
const hasNum = () => {
|
|
81
|
+
const t = toks[i];
|
|
82
|
+
return !!t && "num" in t;
|
|
83
|
+
};
|
|
84
|
+
while (i < toks.length) {
|
|
85
|
+
const t = toks[i];
|
|
86
|
+
let cmd;
|
|
87
|
+
if (t && "cmd" in t) {
|
|
88
|
+
cmd = t.cmd;
|
|
89
|
+
i++;
|
|
90
|
+
} else {
|
|
91
|
+
cmd = prevCmd === "M" ? "L" : prevCmd === "m" ? "l" : prevCmd;
|
|
92
|
+
if (!cmd) break;
|
|
93
|
+
}
|
|
94
|
+
const rel = cmd === cmd.toLowerCase();
|
|
95
|
+
const C = cmd.toUpperCase();
|
|
96
|
+
switch (C) {
|
|
97
|
+
case "M": {
|
|
98
|
+
const x = num() + (rel ? cx : 0);
|
|
99
|
+
const y = num() + (rel ? cy : 0);
|
|
100
|
+
cx = x;
|
|
101
|
+
cy = y;
|
|
102
|
+
sx = x;
|
|
103
|
+
sy = y;
|
|
104
|
+
segs.push([
|
|
105
|
+
"M",
|
|
106
|
+
x,
|
|
107
|
+
y
|
|
108
|
+
]);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
case "L": {
|
|
112
|
+
const x = num() + (rel ? cx : 0);
|
|
113
|
+
const y = num() + (rel ? cy : 0);
|
|
114
|
+
cx = x;
|
|
115
|
+
cy = y;
|
|
116
|
+
segs.push([
|
|
117
|
+
"L",
|
|
118
|
+
x,
|
|
119
|
+
y
|
|
120
|
+
]);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "H": {
|
|
124
|
+
const x = num() + (rel ? cx : 0);
|
|
125
|
+
cx = x;
|
|
126
|
+
segs.push([
|
|
127
|
+
"L",
|
|
128
|
+
x,
|
|
129
|
+
cy
|
|
130
|
+
]);
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case "V": {
|
|
134
|
+
const y = num() + (rel ? cy : 0);
|
|
135
|
+
cy = y;
|
|
136
|
+
segs.push([
|
|
137
|
+
"L",
|
|
138
|
+
cx,
|
|
139
|
+
y
|
|
140
|
+
]);
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case "C": {
|
|
144
|
+
const x1 = num() + (rel ? cx : 0);
|
|
145
|
+
const y1 = num() + (rel ? cy : 0);
|
|
146
|
+
const x2 = num() + (rel ? cx : 0);
|
|
147
|
+
const y2 = num() + (rel ? cy : 0);
|
|
148
|
+
const x = num() + (rel ? cx : 0);
|
|
149
|
+
const y = num() + (rel ? cy : 0);
|
|
150
|
+
segs.push([
|
|
151
|
+
"C",
|
|
152
|
+
x1,
|
|
153
|
+
y1,
|
|
154
|
+
x2,
|
|
155
|
+
y2,
|
|
156
|
+
x,
|
|
157
|
+
y
|
|
158
|
+
]);
|
|
159
|
+
lastCtrlX = x2;
|
|
160
|
+
lastCtrlY = y2;
|
|
161
|
+
cx = x;
|
|
162
|
+
cy = y;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case "S": {
|
|
166
|
+
const reflect = prevCmd.toUpperCase() === "C" || prevCmd.toUpperCase() === "S";
|
|
167
|
+
const x1 = reflect ? 2 * cx - lastCtrlX : cx;
|
|
168
|
+
const y1 = reflect ? 2 * cy - lastCtrlY : cy;
|
|
169
|
+
const x2 = num() + (rel ? cx : 0);
|
|
170
|
+
const y2 = num() + (rel ? cy : 0);
|
|
171
|
+
const x = num() + (rel ? cx : 0);
|
|
172
|
+
const y = num() + (rel ? cy : 0);
|
|
173
|
+
segs.push([
|
|
174
|
+
"C",
|
|
175
|
+
x1,
|
|
176
|
+
y1,
|
|
177
|
+
x2,
|
|
178
|
+
y2,
|
|
179
|
+
x,
|
|
180
|
+
y
|
|
181
|
+
]);
|
|
182
|
+
lastCtrlX = x2;
|
|
183
|
+
lastCtrlY = y2;
|
|
184
|
+
cx = x;
|
|
185
|
+
cy = y;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case "Q": {
|
|
189
|
+
const x1 = num() + (rel ? cx : 0);
|
|
190
|
+
const y1 = num() + (rel ? cy : 0);
|
|
191
|
+
const x = num() + (rel ? cx : 0);
|
|
192
|
+
const y = num() + (rel ? cy : 0);
|
|
193
|
+
segs.push([
|
|
194
|
+
"Q",
|
|
195
|
+
x1,
|
|
196
|
+
y1,
|
|
197
|
+
x,
|
|
198
|
+
y
|
|
199
|
+
]);
|
|
200
|
+
lastCtrlX = x1;
|
|
201
|
+
lastCtrlY = y1;
|
|
202
|
+
cx = x;
|
|
203
|
+
cy = y;
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case "T": {
|
|
207
|
+
const reflect = prevCmd.toUpperCase() === "Q" || prevCmd.toUpperCase() === "T";
|
|
208
|
+
const x1 = reflect ? 2 * cx - lastCtrlX : cx;
|
|
209
|
+
const y1 = reflect ? 2 * cy - lastCtrlY : cy;
|
|
210
|
+
const x = num() + (rel ? cx : 0);
|
|
211
|
+
const y = num() + (rel ? cy : 0);
|
|
212
|
+
segs.push([
|
|
213
|
+
"Q",
|
|
214
|
+
x1,
|
|
215
|
+
y1,
|
|
216
|
+
x,
|
|
217
|
+
y
|
|
218
|
+
]);
|
|
219
|
+
lastCtrlX = x1;
|
|
220
|
+
lastCtrlY = y1;
|
|
221
|
+
cx = x;
|
|
222
|
+
cy = y;
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case "A": {
|
|
226
|
+
const rx = num();
|
|
227
|
+
const ry = num();
|
|
228
|
+
const rot = num();
|
|
229
|
+
const large = num();
|
|
230
|
+
const sweep = num();
|
|
231
|
+
const x = num() + (rel ? cx : 0);
|
|
232
|
+
const y = num() + (rel ? cy : 0);
|
|
233
|
+
const e = arcToE(cx, cy, rx, ry, rot, large, sweep, x, y);
|
|
234
|
+
segs.push(e ?? [
|
|
235
|
+
"L",
|
|
236
|
+
x,
|
|
237
|
+
y
|
|
238
|
+
]);
|
|
239
|
+
cx = x;
|
|
240
|
+
cy = y;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
case "Z":
|
|
244
|
+
segs.push(["Z"]);
|
|
245
|
+
cx = sx;
|
|
246
|
+
cy = sy;
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
i++;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
prevCmd = cmd;
|
|
253
|
+
if (C !== "Z" && !hasNum() && toks[i] && "cmd" in toks[i] === false) break;
|
|
254
|
+
}
|
|
255
|
+
return segs;
|
|
256
|
+
}
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/convert.ts
|
|
259
|
+
const num = (v, d = 0) => {
|
|
260
|
+
const n = parseFloat(v ?? "");
|
|
261
|
+
return Number.isFinite(n) ? n : d;
|
|
262
|
+
};
|
|
263
|
+
function color(v, warnings) {
|
|
264
|
+
if (v === void 0) return void 0;
|
|
265
|
+
const c = v.trim();
|
|
266
|
+
if (c === "" || c === "none") return void 0;
|
|
267
|
+
if (c.startsWith("url(")) {
|
|
268
|
+
warnings.push(`gradient/pattern paint not supported (${c}); dropped`);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
return c;
|
|
272
|
+
}
|
|
273
|
+
/** Resolve this element's paint, inheriting from the parent (SVG presentation). */
|
|
274
|
+
function resolvePaint(attrs, inh, warnings) {
|
|
275
|
+
const fill = "fill" in attrs ? color(attrs["fill"], warnings) : inh.fill;
|
|
276
|
+
const stroke = "stroke" in attrs ? color(attrs["stroke"], warnings) : inh.stroke;
|
|
277
|
+
const strokeWidth = "stroke-width" in attrs ? num(attrs["stroke-width"], 1) : inh.strokeWidth;
|
|
278
|
+
return {
|
|
279
|
+
...fill !== void 0 ? { fill } : {},
|
|
280
|
+
...stroke !== void 0 ? { stroke } : {},
|
|
281
|
+
...strokeWidth !== void 0 ? { strokeWidth } : {}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const shapeProps = (p) => ({
|
|
285
|
+
...p.fill !== void 0 ? { fill: p.fill } : {},
|
|
286
|
+
...p.stroke !== void 0 ? { stroke: p.stroke } : {},
|
|
287
|
+
...p.strokeWidth !== void 0 ? { strokeWidth: p.strokeWidth } : {}
|
|
288
|
+
});
|
|
289
|
+
/** Compose an SVG transform list into a node TRS (skew is dropped + warned). */
|
|
290
|
+
function parseTransform(s, warnings) {
|
|
291
|
+
if (!s) return null;
|
|
292
|
+
let M = [
|
|
293
|
+
1,
|
|
294
|
+
0,
|
|
295
|
+
0,
|
|
296
|
+
1,
|
|
297
|
+
0,
|
|
298
|
+
0
|
|
299
|
+
];
|
|
300
|
+
const mul = (m) => {
|
|
301
|
+
const [a, b, c, d, e, f] = M;
|
|
302
|
+
const [A, B, C, D, E, F] = m;
|
|
303
|
+
M = [
|
|
304
|
+
a * A + c * B,
|
|
305
|
+
b * A + d * B,
|
|
306
|
+
a * C + c * D,
|
|
307
|
+
b * C + d * D,
|
|
308
|
+
a * E + c * F + e,
|
|
309
|
+
b * E + d * F + f
|
|
310
|
+
];
|
|
311
|
+
};
|
|
312
|
+
const re = /(translate|scale|rotate|matrix|skewX|skewY)\s*\(([^)]*)\)/g;
|
|
313
|
+
let m;
|
|
314
|
+
let any = false;
|
|
315
|
+
while ((m = re.exec(s)) !== null) {
|
|
316
|
+
any = true;
|
|
317
|
+
const args = m[2].split(/[\s,]+/).filter(Boolean).map(parseFloat);
|
|
318
|
+
switch (m[1]) {
|
|
319
|
+
case "translate":
|
|
320
|
+
mul([
|
|
321
|
+
1,
|
|
322
|
+
0,
|
|
323
|
+
0,
|
|
324
|
+
1,
|
|
325
|
+
args[0] ?? 0,
|
|
326
|
+
args[1] ?? 0
|
|
327
|
+
]);
|
|
328
|
+
break;
|
|
329
|
+
case "scale":
|
|
330
|
+
mul([
|
|
331
|
+
args[0] ?? 1,
|
|
332
|
+
0,
|
|
333
|
+
0,
|
|
334
|
+
args[1] ?? args[0] ?? 1,
|
|
335
|
+
0,
|
|
336
|
+
0
|
|
337
|
+
]);
|
|
338
|
+
break;
|
|
339
|
+
case "rotate": {
|
|
340
|
+
const r = (args[0] ?? 0) * Math.PI / 180;
|
|
341
|
+
const rot = [
|
|
342
|
+
Math.cos(r),
|
|
343
|
+
Math.sin(r),
|
|
344
|
+
-Math.sin(r),
|
|
345
|
+
Math.cos(r),
|
|
346
|
+
0,
|
|
347
|
+
0
|
|
348
|
+
];
|
|
349
|
+
if (args.length >= 3) {
|
|
350
|
+
mul([
|
|
351
|
+
1,
|
|
352
|
+
0,
|
|
353
|
+
0,
|
|
354
|
+
1,
|
|
355
|
+
args[1],
|
|
356
|
+
args[2]
|
|
357
|
+
]);
|
|
358
|
+
mul(rot);
|
|
359
|
+
mul([
|
|
360
|
+
1,
|
|
361
|
+
0,
|
|
362
|
+
0,
|
|
363
|
+
1,
|
|
364
|
+
-args[1],
|
|
365
|
+
-args[2]
|
|
366
|
+
]);
|
|
367
|
+
} else mul(rot);
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case "matrix":
|
|
371
|
+
if (args.length === 6) mul(args);
|
|
372
|
+
break;
|
|
373
|
+
default: warnings.push(`transform ${m[1]}() (skew) not supported; dropped`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (!any) return null;
|
|
377
|
+
const [a, b, c, d, e, f] = M;
|
|
378
|
+
const sx = Math.hypot(a, b);
|
|
379
|
+
const det = a * d - b * c;
|
|
380
|
+
const sy = sx === 0 ? 0 : det / sx;
|
|
381
|
+
return {
|
|
382
|
+
position: [e, f],
|
|
383
|
+
rotation: Math.atan2(b, a) * 180 / Math.PI,
|
|
384
|
+
scale: [sx, sy]
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
const ellipsePath = (cx, cy, rx, ry) => pathFromSegs([
|
|
388
|
+
[
|
|
389
|
+
"M",
|
|
390
|
+
cx + rx,
|
|
391
|
+
cy
|
|
392
|
+
],
|
|
393
|
+
[
|
|
394
|
+
"E",
|
|
395
|
+
cx,
|
|
396
|
+
cy,
|
|
397
|
+
rx,
|
|
398
|
+
ry,
|
|
399
|
+
0,
|
|
400
|
+
0,
|
|
401
|
+
Math.PI * 2
|
|
402
|
+
],
|
|
403
|
+
["Z"]
|
|
404
|
+
]);
|
|
405
|
+
const pointsToSegs = (pts, close) => {
|
|
406
|
+
const n = pts.split(/[\s,]+/).filter(Boolean).map(parseFloat);
|
|
407
|
+
const segs = [];
|
|
408
|
+
for (let i = 0; i + 1 < n.length; i += 2) segs.push([
|
|
409
|
+
i === 0 ? "M" : "L",
|
|
410
|
+
n[i],
|
|
411
|
+
n[i + 1]
|
|
412
|
+
]);
|
|
413
|
+
if (close) segs.push(["Z"]);
|
|
414
|
+
return segs;
|
|
415
|
+
};
|
|
416
|
+
/** Convert one element (and its subtree) to a node, or null if it produces none. */
|
|
417
|
+
function convertElement(el, inh, warnings) {
|
|
418
|
+
const tag = el.tag.replace(/^.*:/, "");
|
|
419
|
+
const a = el.attrs;
|
|
420
|
+
const paint = resolvePaint(a, inh, warnings);
|
|
421
|
+
const wrap = (node) => {
|
|
422
|
+
const t = parseTransform(a["transform"], warnings);
|
|
423
|
+
return t ? new Group({
|
|
424
|
+
position: t.position,
|
|
425
|
+
rotation: t.rotation,
|
|
426
|
+
scale: t.scale,
|
|
427
|
+
children: [node]
|
|
428
|
+
}) : node;
|
|
429
|
+
};
|
|
430
|
+
switch (tag) {
|
|
431
|
+
case "g":
|
|
432
|
+
case "svg": {
|
|
433
|
+
const children = el.children.map((c) => convertElement(c, paint, warnings)).filter((n) => n !== null);
|
|
434
|
+
const t = parseTransform(a["transform"], warnings);
|
|
435
|
+
return new Group({
|
|
436
|
+
...t ? {
|
|
437
|
+
position: t.position,
|
|
438
|
+
rotation: t.rotation,
|
|
439
|
+
scale: t.scale
|
|
440
|
+
} : {},
|
|
441
|
+
children
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
case "path":
|
|
445
|
+
if (!a["d"]) return null;
|
|
446
|
+
return wrap(new Path({
|
|
447
|
+
data: pathFromSegs(parseSvgPath(a["d"])),
|
|
448
|
+
...shapeProps(paint)
|
|
449
|
+
}));
|
|
450
|
+
case "rect": {
|
|
451
|
+
const x = num(a["x"]);
|
|
452
|
+
const y = num(a["y"]);
|
|
453
|
+
const w = num(a["width"]);
|
|
454
|
+
const h = num(a["height"]);
|
|
455
|
+
const r = num(a["rx"] ?? a["ry"]);
|
|
456
|
+
return wrap(new Rect({
|
|
457
|
+
position: [x + w / 2, y + h / 2],
|
|
458
|
+
width: w,
|
|
459
|
+
height: h,
|
|
460
|
+
...r ? { cornerRadius: r } : {},
|
|
461
|
+
...shapeProps(paint)
|
|
462
|
+
}));
|
|
463
|
+
}
|
|
464
|
+
case "circle": return wrap(new Circle({
|
|
465
|
+
position: [num(a["cx"]), num(a["cy"])],
|
|
466
|
+
radius: num(a["r"]),
|
|
467
|
+
...shapeProps(paint)
|
|
468
|
+
}));
|
|
469
|
+
case "ellipse": return wrap(new Path({
|
|
470
|
+
data: ellipsePath(num(a["cx"]), num(a["cy"]), num(a["rx"]), num(a["ry"])),
|
|
471
|
+
...shapeProps(paint)
|
|
472
|
+
}));
|
|
473
|
+
case "line": return wrap(new Path({
|
|
474
|
+
data: pathFromSegs([[
|
|
475
|
+
"M",
|
|
476
|
+
num(a["x1"]),
|
|
477
|
+
num(a["y1"])
|
|
478
|
+
], [
|
|
479
|
+
"L",
|
|
480
|
+
num(a["x2"]),
|
|
481
|
+
num(a["y2"])
|
|
482
|
+
]]),
|
|
483
|
+
...shapeProps(paint)
|
|
484
|
+
}));
|
|
485
|
+
case "polyline": return wrap(new Path({
|
|
486
|
+
data: pathFromSegs(pointsToSegs(a["points"] ?? "", false)),
|
|
487
|
+
...shapeProps(paint)
|
|
488
|
+
}));
|
|
489
|
+
case "polygon": return wrap(new Path({
|
|
490
|
+
data: pathFromSegs(pointsToSegs(a["points"] ?? "", true)),
|
|
491
|
+
...shapeProps(paint)
|
|
492
|
+
}));
|
|
493
|
+
case "defs":
|
|
494
|
+
case "title":
|
|
495
|
+
case "desc":
|
|
496
|
+
case "metadata":
|
|
497
|
+
case "style": return null;
|
|
498
|
+
case "text":
|
|
499
|
+
case "image":
|
|
500
|
+
case "use":
|
|
501
|
+
case "linearGradient":
|
|
502
|
+
case "radialGradient":
|
|
503
|
+
case "filter":
|
|
504
|
+
case "mask":
|
|
505
|
+
case "clipPath":
|
|
506
|
+
case "pattern":
|
|
507
|
+
warnings.push(`<${tag}> not supported; dropped`);
|
|
508
|
+
return null;
|
|
509
|
+
default: return null;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
/** Convert a parsed SVG root element into a scene-ready Group + size. */
|
|
513
|
+
function convertSvg(svg) {
|
|
514
|
+
const warnings = [];
|
|
515
|
+
const a = svg.attrs;
|
|
516
|
+
let w = num(a["width"]);
|
|
517
|
+
let h = num(a["height"]);
|
|
518
|
+
if ((!w || !h) && a["viewBox"]) {
|
|
519
|
+
const vb = a["viewBox"].split(/[\s,]+/).filter(Boolean).map(parseFloat);
|
|
520
|
+
if (vb.length === 4) {
|
|
521
|
+
w = w || vb[2];
|
|
522
|
+
h = h || vb[3];
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
const root = convertElement(svg, {
|
|
526
|
+
fill: "black",
|
|
527
|
+
strokeWidth: 1
|
|
528
|
+
}, warnings);
|
|
529
|
+
return {
|
|
530
|
+
size: {
|
|
531
|
+
w: w || 100,
|
|
532
|
+
h: h || 100
|
|
533
|
+
},
|
|
534
|
+
root,
|
|
535
|
+
warnings
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
//#endregion
|
|
539
|
+
//#region src/xml.ts
|
|
540
|
+
const TAG = /<(\/?)([a-zA-Z][\w:.-]*)((?:\s+[\w:.-]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/?)>/g;
|
|
541
|
+
const ATTR = /([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
542
|
+
/** Parse SVG source into its root element tree, or null if there's no element. */
|
|
543
|
+
function parseXml(src) {
|
|
544
|
+
const clean = src.replace(/<!--[\s\S]*?-->/g, "").replace(/<\?[\s\S]*?\?>/g, "").replace(/<!DOCTYPE[\s\S]*?>/gi, "");
|
|
545
|
+
const root = {
|
|
546
|
+
tag: "#root",
|
|
547
|
+
attrs: {},
|
|
548
|
+
children: []
|
|
549
|
+
};
|
|
550
|
+
const stack = [root];
|
|
551
|
+
let m;
|
|
552
|
+
TAG.lastIndex = 0;
|
|
553
|
+
while ((m = TAG.exec(clean)) !== null) {
|
|
554
|
+
const [, close, tag, attrStr, selfClose] = m;
|
|
555
|
+
if (close) {
|
|
556
|
+
if (stack.length > 1) stack.pop();
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
const attrs = {};
|
|
560
|
+
let a;
|
|
561
|
+
ATTR.lastIndex = 0;
|
|
562
|
+
while ((a = ATTR.exec(attrStr)) !== null) attrs[a[1]] = a[2] ?? a[3] ?? "";
|
|
563
|
+
const node = {
|
|
564
|
+
tag,
|
|
565
|
+
attrs,
|
|
566
|
+
children: []
|
|
567
|
+
};
|
|
568
|
+
stack[stack.length - 1].children.push(node);
|
|
569
|
+
if (!selfClose) stack.push(node);
|
|
570
|
+
}
|
|
571
|
+
return root.children[0] ?? null;
|
|
572
|
+
}
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/index.ts
|
|
575
|
+
var SvgImportError = class extends Error {
|
|
576
|
+
constructor(message) {
|
|
577
|
+
super(message);
|
|
578
|
+
this.name = "SvgImportError";
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
/** Parse an SVG string and convert it to a glissade scene. Throws on no `<svg>`. */
|
|
582
|
+
function importSvg(svg) {
|
|
583
|
+
const root = parseXml(svg);
|
|
584
|
+
if (root === null || root.tag.replace(/^.*:/, "") !== "svg") throw new SvgImportError("[invalid-document] no root <svg> element found");
|
|
585
|
+
const { size, root: group, warnings } = convertSvg(root);
|
|
586
|
+
return {
|
|
587
|
+
size,
|
|
588
|
+
root: group,
|
|
589
|
+
warnings,
|
|
590
|
+
toSceneModule() {
|
|
591
|
+
return {
|
|
592
|
+
createScene: () => createScene({
|
|
593
|
+
size,
|
|
594
|
+
children: [group]
|
|
595
|
+
}),
|
|
596
|
+
timeline: timeline({
|
|
597
|
+
fps: 60,
|
|
598
|
+
duration: 1,
|
|
599
|
+
tracks: []
|
|
600
|
+
})
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
//#endregion
|
|
606
|
+
export { SvgImportError, convertSvg, importSvg, parseSvgPath, parseXml };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@glissade/svg",
|
|
3
|
+
"version": "0.6.0-pre.1",
|
|
4
|
+
"description": "glissade SVG import: parse an SVG into a deterministic scene module — <path d> and the basic shapes become Path/Rect/Circle nodes with fills, strokes, and transforms.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@glissade/core": "0.6.0-pre.1",
|
|
22
|
+
"@glissade/scene": "0.6.0-pre.1"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/tyevco/glissade.git",
|
|
27
|
+
"directory": "packages/svg"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsdown",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
32
|
+
}
|
|
33
|
+
}
|