@ggraphic/editor 0.1.0-alpha.5 → 0.1.0-alpha.6
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/dist/CanvasEditorProvider.d.ts +6 -1
- package/dist/components/CanvasEditor/CanvasEditorShell.js +139 -36
- package/dist/components/CanvasEditor/editorShell.d.ts +6 -0
- package/dist/components/CanvasEditor/types.d.ts +28 -0
- package/dist/components/GenericCanvas.d.ts +19 -2
- package/dist/components/GenericCanvas.js +28 -3
- package/dist/components/PagePreview.d.ts +8 -1
- package/dist/components/PagePreview.js +4 -1
- package/dist/components/TopBar/DownloadSection.d.ts +24 -3
- package/dist/components/TopBar/DownloadSection.js +45 -7
- package/dist/components/TopBar/ShareDropdown.d.ts +4 -2
- package/dist/components/TopBar/ShareDropdown.js +2 -2
- package/dist/entries/export.d.ts +14 -1
- package/dist/entries/export.js +14 -1
- package/dist/entries/services.d.ts +1 -1
- package/dist/export/fonts/fontFiles.d.ts +100 -0
- package/dist/export/fonts/fontFiles.js +333 -0
- package/dist/export/fonts/glyphs.d.ts +113 -0
- package/dist/export/fonts/glyphs.js +120 -0
- package/dist/export/fonts/variation.d.ts +23 -0
- package/dist/export/fonts/variation.js +43 -0
- package/dist/export/pdf/pdfContent.d.ts +108 -0
- package/dist/export/pdf/pdfContent.js +566 -0
- package/dist/export/pdf/pdfFontkit.d.ts +54 -0
- package/dist/export/pdf/pdfFontkit.js +159 -0
- package/dist/export/pdf/pdfGeometry.d.ts +54 -0
- package/dist/export/pdf/pdfGeometry.js +228 -0
- package/dist/export/pdf/pdfResources.d.ts +109 -0
- package/dist/export/pdf/pdfResources.js +314 -0
- package/dist/export/pdf/writePdf.d.ts +34 -0
- package/dist/export/pdf/writePdf.js +165 -0
- package/dist/export/pptx/exportPptxDeck.d.ts +10 -2
- package/dist/export/pptx/exportPptxDeck.js +5 -2
- package/dist/export/pptx/svgPicture.d.ts +19 -0
- package/dist/export/pptx/svgPicture.js +64 -0
- package/dist/export/pptx/vectorizeElement.d.ts +9 -0
- package/dist/export/pptx/vectorizeElement.js +118 -0
- package/dist/export/pptx/writePptx.d.ts +13 -0
- package/dist/export/pptx/writePptx.js +17 -0
- package/dist/export/printPdf.d.ts +1 -0
- package/dist/export/printPdf.js +1 -1
- package/dist/export/scene/browserDeps.d.ts +32 -0
- package/dist/export/scene/browserDeps.js +174 -0
- package/dist/export/scene/imageBytes.d.ts +79 -0
- package/dist/export/scene/imageBytes.js +330 -0
- package/dist/export/scene/itemBounds.d.ts +9 -0
- package/dist/export/scene/itemBounds.js +40 -0
- package/dist/export/scene/matrix.d.ts +13 -0
- package/dist/export/scene/matrix.js +32 -0
- package/dist/export/scene/rasterLimit.d.ts +13 -0
- package/dist/export/scene/rasterLimit.js +24 -0
- package/dist/export/scene/rebaseOps.d.ts +8 -0
- package/dist/export/scene/rebaseOps.js +51 -0
- package/dist/export/scene/recordPageScene.d.ts +28 -0
- package/dist/export/scene/recordPageScene.js +224 -0
- package/dist/export/scene/recordingCanvas.d.ts +55 -0
- package/dist/export/scene/recordingCanvas.js +71 -0
- package/dist/export/scene/recordingContext.d.ts +162 -0
- package/dist/export/scene/recordingContext.js +1031 -0
- package/dist/export/scene/replayScene.d.ts +31 -0
- package/dist/export/scene/replayScene.js +213 -0
- package/dist/export/scene/resolveScene.d.ts +48 -0
- package/dist/export/scene/resolveScene.js +236 -0
- package/dist/export/scene/trustedSvgImages.d.ts +3 -0
- package/dist/export/scene/trustedSvgImages.js +21 -0
- package/dist/export/scene/types.d.ts +241 -0
- package/dist/export/scene/types.js +38 -0
- package/dist/export/slidesPdf.d.ts +10 -2
- package/dist/export/slidesPdf.js +8 -4
- package/dist/export/svg/__fixtures__/barChart.svg +1 -0
- package/dist/export/svg/__golden__/fixture-editable.svg +87 -0
- package/dist/export/svg/__golden__/fixture-outlines.svg +88 -0
- package/dist/export/svg/inlineSvg.d.ts +10 -0
- package/dist/export/svg/inlineSvg.js +119 -0
- package/dist/export/svg/scopeCss.d.ts +45 -0
- package/dist/export/svg/scopeCss.js +214 -0
- package/dist/export/svg/svgColor.d.ts +8 -0
- package/dist/export/svg/svgColor.js +46 -0
- package/dist/export/svg/svgImageScene.d.ts +41 -0
- package/dist/export/svg/svgImageScene.js +128 -0
- package/dist/export/svg/svgPathData.d.ts +31 -0
- package/dist/export/svg/svgPathData.js +466 -0
- package/dist/export/svg/svgToScene.d.ts +19 -0
- package/dist/export/svg/svgToScene.js +1183 -0
- package/dist/export/svg/writeSvg.d.ts +33 -0
- package/dist/export/svg/writeSvg.js +594 -0
- package/dist/export/svg/xml.d.ts +23 -0
- package/dist/export/svg/xml.js +49 -0
- package/dist/export/vectorDeps.d.ts +27 -0
- package/dist/export/vectorDeps.js +37 -0
- package/dist/export/vectorExport.d.ts +85 -0
- package/dist/export/vectorExport.js +287 -0
- package/dist/hostServices.d.ts +15 -0
- package/dist/i18n/de.d.ts +26 -9
- package/dist/i18n/de.js +26 -10
- package/dist/i18n/en.js +24 -0
- package/dist/primitives/AssetPrimitive.js +7 -2
- package/dist/primitives/CanvasIcons.js +6 -0
- package/dist/primitives/CanvasStage.js +2 -2
- package/dist/primitives/ChartPrimitive.d.ts +11 -1
- package/dist/primitives/ChartPrimitive.js +73 -19
- package/dist/primitives/IconPrimitive.js +3 -0
- package/dist/primitives/IllustrationPrimitive.js +14 -1
- package/dist/primitives/chartFont.d.ts +30 -0
- package/dist/primitives/chartFont.js +127 -0
- package/dist/sidebar/sections/ImageAdjustSection.js +5 -4
- package/dist/sidebar/sections/assets/ElementsPanel.js +4 -2
- package/dist/sidebar/sections/assets/assetAudience.d.ts +12 -0
- package/dist/sidebar/sections/assets/assetAudience.js +18 -0
- package/dist/sidebar/sections/assets/useElementCatalog.js +3 -2
- package/dist/styles/compiled.css +1 -1
- package/dist/utils/canvasAssets.d.ts +11 -1
- package/dist/utils/canvasAssets.js +8 -0
- package/dist/utils/canvasIcons.js +5 -3
- package/dist/utils/captureStage.d.ts +19 -0
- package/dist/utils/captureStage.js +14 -1
- package/dist/utils/externalIcons.d.ts +7 -0
- package/dist/utils/externalIcons.js +70 -3
- package/dist/utils/illustrations/svgCache.js +6 -1
- package/dist/utils/imageFilters.d.ts +13 -0
- package/dist/utils/imageFilters.js +11 -0
- package/dist/utils/zipPageImages.d.ts +8 -0
- package/dist/utils/zipPageImages.js +16 -4
- package/package.json +6 -2
- package/skills/ggraphic-embed/SKILL.md +33 -1
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* The geometry of SVG as scene path commands: path data, the basic shapes, `transform` lists and
|
|
4
|
+
* the bounding box a gradient needs. Pure functions; no DOM.
|
|
5
|
+
*/
|
|
6
|
+
import { IDENTITY, apply, multiply } from '../scene/matrix.js';
|
|
7
|
+
import { arcToBeziers } from '../scene/recordingContext.js';
|
|
8
|
+
// ---------------------------------------------------------------------------------------------
|
|
9
|
+
// Numbers
|
|
10
|
+
const NUMBER = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/y;
|
|
11
|
+
/** A whitespace/comma separated list of numbers; `null` if anything in it is not a number. */
|
|
12
|
+
export function parseNumberList(value) {
|
|
13
|
+
const items = value
|
|
14
|
+
.trim()
|
|
15
|
+
.split(/[\s,]+/)
|
|
16
|
+
.filter(Boolean);
|
|
17
|
+
const numbers = items.map((item) => /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(item) ? Number(item) : NaN);
|
|
18
|
+
return numbers.every(Number.isFinite) ? numbers : null;
|
|
19
|
+
}
|
|
20
|
+
// ---------------------------------------------------------------------------------------------
|
|
21
|
+
// Arcs
|
|
22
|
+
/**
|
|
23
|
+
* An SVG elliptical arc (endpoint parameterisation, SVG 1.1 F.6.5) as cubics. The caller is
|
|
24
|
+
* already at `(x1, y1)`. A zero radius is a line, radii too small for the chord are scaled up.
|
|
25
|
+
*/
|
|
26
|
+
export function arcCommands(x1, y1, rxIn, ryIn, rotationDegrees, largeArc, sweep, x2, y2) {
|
|
27
|
+
if (x1 === x2 && y1 === y2)
|
|
28
|
+
return [];
|
|
29
|
+
let rx = Math.abs(rxIn);
|
|
30
|
+
let ry = Math.abs(ryIn);
|
|
31
|
+
if (rx === 0 || ry === 0)
|
|
32
|
+
return [{ op: 'L', x: x2, y: y2 }];
|
|
33
|
+
const phi = (rotationDegrees * Math.PI) / 180;
|
|
34
|
+
const cos = Math.cos(phi);
|
|
35
|
+
const sin = Math.sin(phi);
|
|
36
|
+
const dx = (x1 - x2) / 2;
|
|
37
|
+
const dy = (y1 - y2) / 2;
|
|
38
|
+
const x1p = cos * dx + sin * dy;
|
|
39
|
+
const y1p = -sin * dx + cos * dy;
|
|
40
|
+
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
41
|
+
if (lambda > 1) {
|
|
42
|
+
const scale = Math.sqrt(lambda);
|
|
43
|
+
rx *= scale;
|
|
44
|
+
ry *= scale;
|
|
45
|
+
}
|
|
46
|
+
const numerator = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
|
47
|
+
const denominator = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
|
|
48
|
+
let coefficient = Math.sqrt(Math.max(0, numerator / denominator));
|
|
49
|
+
if (largeArc === sweep)
|
|
50
|
+
coefficient = -coefficient;
|
|
51
|
+
const cxp = (coefficient * rx * y1p) / ry;
|
|
52
|
+
const cyp = (-coefficient * ry * x1p) / rx;
|
|
53
|
+
const cx = cos * cxp - sin * cyp + (x1 + x2) / 2;
|
|
54
|
+
const cy = sin * cxp + cos * cyp + (y1 + y2) / 2;
|
|
55
|
+
const ux = (x1p - cxp) / rx;
|
|
56
|
+
const uy = (y1p - cyp) / ry;
|
|
57
|
+
const vx = (-x1p - cxp) / rx;
|
|
58
|
+
const vy = (-y1p - cyp) / ry;
|
|
59
|
+
const start = Math.atan2(uy, ux);
|
|
60
|
+
let delta = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
61
|
+
if (!sweep && delta > 0)
|
|
62
|
+
delta -= 2 * Math.PI;
|
|
63
|
+
else if (sweep && delta < 0)
|
|
64
|
+
delta += 2 * Math.PI;
|
|
65
|
+
const { curves } = arcToBeziers(cx, cy, rx, ry, phi, start, delta);
|
|
66
|
+
// The parameterisation lands a few ulps off the endpoint the data names.
|
|
67
|
+
const last = curves.at(-1);
|
|
68
|
+
if (last) {
|
|
69
|
+
last.x = x2;
|
|
70
|
+
last.y = y2;
|
|
71
|
+
}
|
|
72
|
+
return curves;
|
|
73
|
+
}
|
|
74
|
+
// ---------------------------------------------------------------------------------------------
|
|
75
|
+
// Path data
|
|
76
|
+
/**
|
|
77
|
+
* The commands of an SVG `d` attribute. Data after the first error is dropped and what came
|
|
78
|
+
* before it is kept, as the SVG error handling says. Relative commands are made absolute;
|
|
79
|
+
* `H`/`V`/`S`/`T`/`A` become `L`/`C`/`Q`/`C`.
|
|
80
|
+
*/
|
|
81
|
+
export function parsePathData(data) {
|
|
82
|
+
const out = [];
|
|
83
|
+
let i = 0;
|
|
84
|
+
const separators = () => {
|
|
85
|
+
while (i < data.length && /[\s,]/.test(data[i]))
|
|
86
|
+
i++;
|
|
87
|
+
};
|
|
88
|
+
const number = () => {
|
|
89
|
+
separators();
|
|
90
|
+
NUMBER.lastIndex = i;
|
|
91
|
+
const match = NUMBER.exec(data);
|
|
92
|
+
if (!match)
|
|
93
|
+
return null;
|
|
94
|
+
i = NUMBER.lastIndex;
|
|
95
|
+
const value = Number(match[0]);
|
|
96
|
+
return Number.isFinite(value) ? value : null;
|
|
97
|
+
};
|
|
98
|
+
const flag = () => {
|
|
99
|
+
separators();
|
|
100
|
+
const c = data[i];
|
|
101
|
+
if (c !== '0' && c !== '1')
|
|
102
|
+
return null;
|
|
103
|
+
i++;
|
|
104
|
+
return c === '1';
|
|
105
|
+
};
|
|
106
|
+
const numbers = (count) => {
|
|
107
|
+
const values = [];
|
|
108
|
+
for (let n = 0; n < count; n++) {
|
|
109
|
+
const v = number();
|
|
110
|
+
if (v === null)
|
|
111
|
+
return null;
|
|
112
|
+
values.push(v);
|
|
113
|
+
}
|
|
114
|
+
return values;
|
|
115
|
+
};
|
|
116
|
+
let x = 0;
|
|
117
|
+
let y = 0;
|
|
118
|
+
let startX = 0;
|
|
119
|
+
let startY = 0;
|
|
120
|
+
// The reflected control point of the previous curve, for S and T.
|
|
121
|
+
let control = null;
|
|
122
|
+
let command = '';
|
|
123
|
+
let closed = false;
|
|
124
|
+
const moveIfClosed = () => {
|
|
125
|
+
if (closed)
|
|
126
|
+
out.push({ op: 'M', x, y });
|
|
127
|
+
closed = false;
|
|
128
|
+
};
|
|
129
|
+
for (;;) {
|
|
130
|
+
separators();
|
|
131
|
+
if (i >= data.length)
|
|
132
|
+
break;
|
|
133
|
+
if (/[A-Za-z]/.test(data[i]))
|
|
134
|
+
command = data[i++];
|
|
135
|
+
else if (command === '' || command === 'Z' || command === 'z')
|
|
136
|
+
break;
|
|
137
|
+
const lower = command.toLowerCase();
|
|
138
|
+
if (out.length === 0 && lower !== 'm')
|
|
139
|
+
break;
|
|
140
|
+
const relative = command !== command.toUpperCase();
|
|
141
|
+
const ox = relative ? x : 0;
|
|
142
|
+
const oy = relative ? y : 0;
|
|
143
|
+
let next = null;
|
|
144
|
+
if (lower === 'z') {
|
|
145
|
+
if (out.length > 0 && !closed)
|
|
146
|
+
out.push({ op: 'Z' });
|
|
147
|
+
x = startX;
|
|
148
|
+
y = startY;
|
|
149
|
+
closed = true;
|
|
150
|
+
}
|
|
151
|
+
else if (lower === 'm') {
|
|
152
|
+
const v = numbers(2);
|
|
153
|
+
if (!v)
|
|
154
|
+
break;
|
|
155
|
+
x = v[0] + ox;
|
|
156
|
+
y = v[1] + oy;
|
|
157
|
+
startX = x;
|
|
158
|
+
startY = y;
|
|
159
|
+
closed = false;
|
|
160
|
+
out.push({ op: 'M', x, y });
|
|
161
|
+
// Further pairs after a moveto are linetos.
|
|
162
|
+
command = relative ? 'l' : 'L';
|
|
163
|
+
}
|
|
164
|
+
else if (lower === 'l') {
|
|
165
|
+
const v = numbers(2);
|
|
166
|
+
if (!v)
|
|
167
|
+
break;
|
|
168
|
+
moveIfClosed();
|
|
169
|
+
x = v[0] + ox;
|
|
170
|
+
y = v[1] + oy;
|
|
171
|
+
out.push({ op: 'L', x, y });
|
|
172
|
+
}
|
|
173
|
+
else if (lower === 'h') {
|
|
174
|
+
const v = numbers(1);
|
|
175
|
+
if (!v)
|
|
176
|
+
break;
|
|
177
|
+
moveIfClosed();
|
|
178
|
+
x = v[0] + ox;
|
|
179
|
+
out.push({ op: 'L', x, y });
|
|
180
|
+
}
|
|
181
|
+
else if (lower === 'v') {
|
|
182
|
+
const v = numbers(1);
|
|
183
|
+
if (!v)
|
|
184
|
+
break;
|
|
185
|
+
moveIfClosed();
|
|
186
|
+
y = v[0] + oy;
|
|
187
|
+
out.push({ op: 'L', x, y });
|
|
188
|
+
}
|
|
189
|
+
else if (lower === 'c' || lower === 's') {
|
|
190
|
+
const v = numbers(lower === 'c' ? 6 : 4);
|
|
191
|
+
if (!v)
|
|
192
|
+
break;
|
|
193
|
+
moveIfClosed();
|
|
194
|
+
let x1 = x;
|
|
195
|
+
let y1 = y;
|
|
196
|
+
if (lower === 'c') {
|
|
197
|
+
x1 = v[0] + ox;
|
|
198
|
+
y1 = v[1] + oy;
|
|
199
|
+
v.splice(0, 2);
|
|
200
|
+
}
|
|
201
|
+
else if (control?.kind === 'c') {
|
|
202
|
+
x1 = 2 * x - control.x;
|
|
203
|
+
y1 = 2 * y - control.y;
|
|
204
|
+
}
|
|
205
|
+
const [cx2, cy2, ex, ey] = [v[0] + ox, v[1] + oy, v[2] + ox, v[3] + oy];
|
|
206
|
+
out.push({ op: 'C', x1, y1, x2: cx2, y2: cy2, x: ex, y: ey });
|
|
207
|
+
next = { kind: 'c', x: cx2, y: cy2 };
|
|
208
|
+
x = ex;
|
|
209
|
+
y = ey;
|
|
210
|
+
}
|
|
211
|
+
else if (lower === 'q' || lower === 't') {
|
|
212
|
+
const v = numbers(lower === 'q' ? 4 : 2);
|
|
213
|
+
if (!v)
|
|
214
|
+
break;
|
|
215
|
+
moveIfClosed();
|
|
216
|
+
let x1 = x;
|
|
217
|
+
let y1 = y;
|
|
218
|
+
if (lower === 'q') {
|
|
219
|
+
x1 = v[0] + ox;
|
|
220
|
+
y1 = v[1] + oy;
|
|
221
|
+
v.splice(0, 2);
|
|
222
|
+
}
|
|
223
|
+
else if (control?.kind === 'q') {
|
|
224
|
+
x1 = 2 * x - control.x;
|
|
225
|
+
y1 = 2 * y - control.y;
|
|
226
|
+
}
|
|
227
|
+
const [ex, ey] = [v[0] + ox, v[1] + oy];
|
|
228
|
+
out.push({ op: 'Q', x1, y1, x: ex, y: ey });
|
|
229
|
+
next = { kind: 'q', x: x1, y: y1 };
|
|
230
|
+
x = ex;
|
|
231
|
+
y = ey;
|
|
232
|
+
}
|
|
233
|
+
else if (lower === 'a') {
|
|
234
|
+
const rx = number();
|
|
235
|
+
const ry = number();
|
|
236
|
+
const rotation = number();
|
|
237
|
+
const large = flag();
|
|
238
|
+
const sweep = flag();
|
|
239
|
+
const ex = number();
|
|
240
|
+
const ey = number();
|
|
241
|
+
if (rx === null ||
|
|
242
|
+
ry === null ||
|
|
243
|
+
rotation === null ||
|
|
244
|
+
large === null ||
|
|
245
|
+
sweep === null ||
|
|
246
|
+
ex === null ||
|
|
247
|
+
ey === null) {
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
moveIfClosed();
|
|
251
|
+
out.push(...arcCommands(x, y, rx, ry, rotation, large, sweep, ex + ox, ey + oy));
|
|
252
|
+
x = ex + ox;
|
|
253
|
+
y = ey + oy;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
control = next;
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
// ---------------------------------------------------------------------------------------------
|
|
263
|
+
// Shapes
|
|
264
|
+
/** A rectangle, with corner radii when `rx`/`ry` are positive (already clamped by the caller). */
|
|
265
|
+
export function rectCommands(x, y, width, height, rx = 0, ry = 0) {
|
|
266
|
+
if (!(rx > 0 && ry > 0)) {
|
|
267
|
+
return [
|
|
268
|
+
{ op: 'M', x, y },
|
|
269
|
+
{ op: 'L', x: x + width, y },
|
|
270
|
+
{ op: 'L', x: x + width, y: y + height },
|
|
271
|
+
{ op: 'L', x, y: y + height },
|
|
272
|
+
{ op: 'Z' },
|
|
273
|
+
];
|
|
274
|
+
}
|
|
275
|
+
const right = x + width;
|
|
276
|
+
const bottom = y + height;
|
|
277
|
+
return [
|
|
278
|
+
{ op: 'M', x: x + rx, y },
|
|
279
|
+
{ op: 'L', x: right - rx, y },
|
|
280
|
+
...arcCommands(right - rx, y, rx, ry, 0, false, true, right, y + ry),
|
|
281
|
+
{ op: 'L', x: right, y: bottom - ry },
|
|
282
|
+
...arcCommands(right, bottom - ry, rx, ry, 0, false, true, right - rx, bottom),
|
|
283
|
+
{ op: 'L', x: x + rx, y: bottom },
|
|
284
|
+
...arcCommands(x + rx, bottom, rx, ry, 0, false, true, x, bottom - ry),
|
|
285
|
+
{ op: 'L', x, y: y + ry },
|
|
286
|
+
...arcCommands(x, y + ry, rx, ry, 0, false, true, x + rx, y),
|
|
287
|
+
{ op: 'Z' },
|
|
288
|
+
];
|
|
289
|
+
}
|
|
290
|
+
/** A full ellipse, starting at its right-most point and running clockwise on screen. */
|
|
291
|
+
export function ellipseCommands(cx, cy, rx, ry) {
|
|
292
|
+
const { start, curves } = arcToBeziers(cx, cy, rx, ry, 0, 0, 2 * Math.PI);
|
|
293
|
+
const last = curves.at(-1);
|
|
294
|
+
if (last) {
|
|
295
|
+
last.x = start.x;
|
|
296
|
+
last.y = start.y;
|
|
297
|
+
}
|
|
298
|
+
return [{ op: 'M', x: start.x, y: start.y }, ...curves, { op: 'Z' }];
|
|
299
|
+
}
|
|
300
|
+
/** `points` of a polyline/polygon; an odd trailing number is dropped. */
|
|
301
|
+
export function pointsCommands(points, close) {
|
|
302
|
+
const numbers = [];
|
|
303
|
+
const re = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/g;
|
|
304
|
+
for (const match of points.matchAll(re))
|
|
305
|
+
numbers.push(Number(match[0]));
|
|
306
|
+
const out = [];
|
|
307
|
+
for (let n = 0; n + 1 < numbers.length; n += 2) {
|
|
308
|
+
out.push({ op: n === 0 ? 'M' : 'L', x: numbers[n], y: numbers[n + 1] });
|
|
309
|
+
}
|
|
310
|
+
if (close && out.length > 0)
|
|
311
|
+
out.push({ op: 'Z' });
|
|
312
|
+
return out;
|
|
313
|
+
}
|
|
314
|
+
// ---------------------------------------------------------------------------------------------
|
|
315
|
+
// Transforms of paths
|
|
316
|
+
export function transformCommands(commands, m) {
|
|
317
|
+
return commands.map((c) => {
|
|
318
|
+
switch (c.op) {
|
|
319
|
+
case 'Z':
|
|
320
|
+
return c;
|
|
321
|
+
case 'M':
|
|
322
|
+
case 'L':
|
|
323
|
+
return { op: c.op, ...apply(m, c.x, c.y) };
|
|
324
|
+
case 'Q': {
|
|
325
|
+
const p1 = apply(m, c.x1, c.y1);
|
|
326
|
+
const p = apply(m, c.x, c.y);
|
|
327
|
+
return { op: 'Q', x1: p1.x, y1: p1.y, x: p.x, y: p.y };
|
|
328
|
+
}
|
|
329
|
+
case 'C': {
|
|
330
|
+
const p1 = apply(m, c.x1, c.y1);
|
|
331
|
+
const p2 = apply(m, c.x2, c.y2);
|
|
332
|
+
const p = apply(m, c.x, c.y);
|
|
333
|
+
return { op: 'C', x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y, x: p.x, y: p.y };
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/** The interior roots in (0, 1) of the derivative of one coordinate of a bezier. */
|
|
339
|
+
function extrema(p0, p1, p2, p3) {
|
|
340
|
+
if (p3 === null) {
|
|
341
|
+
const denominator = p0 - 2 * p1 + p2;
|
|
342
|
+
const t = denominator === 0 ? NaN : (p0 - p1) / denominator;
|
|
343
|
+
return t > 0 && t < 1 ? [t] : [];
|
|
344
|
+
}
|
|
345
|
+
const a = -p0 + 3 * p1 - 3 * p2 + p3;
|
|
346
|
+
const b = 2 * (p0 - 2 * p1 + p2);
|
|
347
|
+
const c = p1 - p0;
|
|
348
|
+
const roots = [];
|
|
349
|
+
if (Math.abs(a) < 1e-12) {
|
|
350
|
+
if (b !== 0)
|
|
351
|
+
roots.push(-c / b);
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
const discriminant = b * b - 4 * a * c;
|
|
355
|
+
if (discriminant >= 0) {
|
|
356
|
+
const root = Math.sqrt(discriminant);
|
|
357
|
+
roots.push((-b + root) / (2 * a), (-b - root) / (2 * a));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return roots.filter((t) => t > 0 && t < 1);
|
|
361
|
+
}
|
|
362
|
+
/** The exact bounding box of the geometry (curve extrema included), or `null` for no points. */
|
|
363
|
+
export function commandBounds(commands) {
|
|
364
|
+
let minX = Infinity;
|
|
365
|
+
let minY = Infinity;
|
|
366
|
+
let maxX = -Infinity;
|
|
367
|
+
let maxY = -Infinity;
|
|
368
|
+
const add = (px, py) => {
|
|
369
|
+
minX = Math.min(minX, px);
|
|
370
|
+
minY = Math.min(minY, py);
|
|
371
|
+
maxX = Math.max(maxX, px);
|
|
372
|
+
maxY = Math.max(maxY, py);
|
|
373
|
+
};
|
|
374
|
+
let cx = 0;
|
|
375
|
+
let cy = 0;
|
|
376
|
+
for (const c of commands) {
|
|
377
|
+
switch (c.op) {
|
|
378
|
+
case 'M':
|
|
379
|
+
case 'L':
|
|
380
|
+
add(c.x, c.y);
|
|
381
|
+
cx = c.x;
|
|
382
|
+
cy = c.y;
|
|
383
|
+
break;
|
|
384
|
+
case 'Q': {
|
|
385
|
+
add(c.x, c.y);
|
|
386
|
+
const at = (t, a, b, e) => (1 - t) * (1 - t) * a + 2 * (1 - t) * t * b + t * t * e;
|
|
387
|
+
for (const t of extrema(cx, c.x1, c.x, null))
|
|
388
|
+
add(at(t, cx, c.x1, c.x), at(t, cy, c.y1, c.y));
|
|
389
|
+
for (const t of extrema(cy, c.y1, c.y, null))
|
|
390
|
+
add(at(t, cx, c.x1, c.x), at(t, cy, c.y1, c.y));
|
|
391
|
+
cx = c.x;
|
|
392
|
+
cy = c.y;
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case 'C': {
|
|
396
|
+
add(c.x, c.y);
|
|
397
|
+
const at = (t, a, b, e, f) => (1 - t) ** 3 * a + 3 * (1 - t) ** 2 * t * b + 3 * (1 - t) * t * t * e + t ** 3 * f;
|
|
398
|
+
const ts = [...extrema(cx, c.x1, c.x2, c.x), ...extrema(cy, c.y1, c.y2, c.y)];
|
|
399
|
+
for (const t of ts)
|
|
400
|
+
add(at(t, cx, c.x1, c.x2, c.x), at(t, cy, c.y1, c.y2, c.y));
|
|
401
|
+
cx = c.x;
|
|
402
|
+
cy = c.y;
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
case 'Z':
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return minX <= maxX ? { x: minX, y: minY, width: maxX - minX, height: maxY - minY } : null;
|
|
410
|
+
}
|
|
411
|
+
// ---------------------------------------------------------------------------------------------
|
|
412
|
+
// Transform lists
|
|
413
|
+
const TRANSFORM = /\s*(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)\s*,?/y;
|
|
414
|
+
/** The matrix of an SVG `transform` list; `null` when it does not parse. */
|
|
415
|
+
export function parseTransform(value) {
|
|
416
|
+
const text = value.trim();
|
|
417
|
+
let m = IDENTITY;
|
|
418
|
+
let i = 0;
|
|
419
|
+
while (i < text.length) {
|
|
420
|
+
TRANSFORM.lastIndex = i;
|
|
421
|
+
const match = TRANSFORM.exec(text);
|
|
422
|
+
if (!match)
|
|
423
|
+
return null;
|
|
424
|
+
i = TRANSFORM.lastIndex;
|
|
425
|
+
const args = parseNumberList(match[2]);
|
|
426
|
+
if (!args)
|
|
427
|
+
return null;
|
|
428
|
+
const step = transformStep(match[1], args);
|
|
429
|
+
if (!step)
|
|
430
|
+
return null;
|
|
431
|
+
m = multiply(m, step);
|
|
432
|
+
}
|
|
433
|
+
return m;
|
|
434
|
+
}
|
|
435
|
+
function transformStep(name, a) {
|
|
436
|
+
switch (name) {
|
|
437
|
+
case 'matrix':
|
|
438
|
+
return a.length === 6 ? [a[0], a[1], a[2], a[3], a[4], a[5]] : null;
|
|
439
|
+
case 'translate':
|
|
440
|
+
return a.length === 1 || a.length === 2 ? [1, 0, 0, 1, a[0], a[1] ?? 0] : null;
|
|
441
|
+
case 'scale':
|
|
442
|
+
return a.length === 1 || a.length === 2 ? [a[0], 0, 0, a[1] ?? a[0], 0, 0] : null;
|
|
443
|
+
case 'rotate': {
|
|
444
|
+
if (a.length !== 1 && a.length !== 3)
|
|
445
|
+
return null;
|
|
446
|
+
const angle = (a[0] * Math.PI) / 180;
|
|
447
|
+
const rotation = [
|
|
448
|
+
Math.cos(angle),
|
|
449
|
+
Math.sin(angle),
|
|
450
|
+
-Math.sin(angle),
|
|
451
|
+
Math.cos(angle),
|
|
452
|
+
0,
|
|
453
|
+
0,
|
|
454
|
+
];
|
|
455
|
+
if (a.length === 1)
|
|
456
|
+
return rotation;
|
|
457
|
+
return multiply(multiply([1, 0, 0, 1, a[1], a[2]], rotation), [1, 0, 0, 1, -a[1], -a[2]]);
|
|
458
|
+
}
|
|
459
|
+
case 'skewX':
|
|
460
|
+
return a.length === 1 ? [1, 0, Math.tan((a[0] * Math.PI) / 180), 1, 0, 0] : null;
|
|
461
|
+
case 'skewY':
|
|
462
|
+
return a.length === 1 ? [1, Math.tan((a[0] * Math.PI) / 180), 0, 1, 0, 0] : null;
|
|
463
|
+
default:
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { FontSpec, Matrix, SceneOp } from '../scene/types.js';
|
|
2
|
+
export interface SvgToSceneBox {
|
|
3
|
+
/** Maps the box (0..width, 0..height) to page px. */
|
|
4
|
+
matrix: Matrix;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
export interface SvgToSceneDefaults {
|
|
9
|
+
/** What the SVG inherits where it says nothing: family, fallback list, weight, style and size. */
|
|
10
|
+
font: FontSpec;
|
|
11
|
+
/** Advance of every code point of `text` in `font`; used to anchor and flow text. */
|
|
12
|
+
measure?: (text: string, font: FontSpec) => number[];
|
|
13
|
+
}
|
|
14
|
+
export interface SvgToSceneResult {
|
|
15
|
+
ops: SceneOp[];
|
|
16
|
+
/** Elements, properties and features that were skipped or could not be reproduced. */
|
|
17
|
+
unsupported: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare function svgToScene(markup: string, box: SvgToSceneBox, defaults: SvgToSceneDefaults): SvgToSceneResult;
|