embedded-react 0.21.0 → 0.22.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 +2 -2
- package/aot/compile.mjs +1 -1
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/check.mjs +1 -1
- package/aot/compiler/collect.mjs +1 -1
- package/aot/compiler/elements/button.mjs +3 -3
- package/aot/compiler/elements/image-background.mjs +1 -1
- package/aot/compiler/elements/props.mjs +10 -6
- package/aot/compiler/elements/section-list.mjs +1 -1
- package/aot/compiler/expressions.mjs +29 -2
- package/aot/compiler/handlers.mjs +73 -16
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- package/aot/compiler/lower.mjs +12 -0
- package/aot/compiler/nodes.mjs +17 -9
- package/aot/compiler/out.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +36 -2
- package/aot/compiler/package/format.mjs +5 -3
- package/aot/compiler/package/lower.mjs +53 -19
- package/aot/compiler/scroll-to.mjs +112 -0
- package/aot/compiler/style-text.mjs +2 -2
- package/aot/compiler/svg.mjs +6 -3
- package/aot/compiler/updaters.mjs +1 -0
- package/aot/style-map.mjs +44 -1
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +84 -101
- package/cli.mjs +482 -621
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +144 -200
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +93 -123
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +155 -2
- package/src/embedded-react/index.js +26 -19
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -146
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/nativeModule.js +0 -462
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
package/assets/bake-svg.mjs
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
16
|
// Build-time SVG -> engine op-tape baker. Turns an .svg FILE into the same
|
|
18
17
|
// {ops, paints} op-tape an inline <Svg> compiles to, so `import logo from './logo.svg'` can render as a
|
|
19
18
|
// crisp on-device vector. Every transform (matrix/translate/rotate/scale/skew, nested) is BAKED into
|
|
@@ -25,28 +24,12 @@
|
|
|
25
24
|
// inheritance). Features the op-tape can't represent (<text>, <mask>, <filter>, <use>, …) are recorded in
|
|
26
25
|
// `dropped` so the caller can rasterize the whole SVG as a fallback image instead. Primitives are emitted as
|
|
27
26
|
// line/cubic paths (no native arc op) so an arbitrary matrix bakes cleanly.
|
|
28
|
-
import {parse} from 'svgson';
|
|
29
|
-
import {mkdirSync, writeFileSync} from 'node:fs';
|
|
30
|
-
import {join} from 'node:path';
|
|
31
|
-
import {tmpdir} from 'node:os';
|
|
32
|
-
import {createHash} from 'node:crypto';
|
|
33
|
-
import {
|
|
34
|
-
parsePath,
|
|
35
|
-
parseColor,
|
|
36
|
-
rectRadii,
|
|
37
|
-
VOP_SHAPE,
|
|
38
|
-
VOP_MOVE,
|
|
39
|
-
VOP_LINE,
|
|
40
|
-
VOP_QUAD,
|
|
41
|
-
VOP_CUBIC,
|
|
42
|
-
CAP,
|
|
43
|
-
JOIN,
|
|
44
|
-
PAINT_STRIDE,
|
|
45
|
-
GRAD_LINEAR,
|
|
46
|
-
GRAD_RADIAL,
|
|
47
|
-
GRAD_CONIC,
|
|
48
|
-
} from '../src/embedded-react/svg-ops.js';
|
|
49
|
-
|
|
27
|
+
import { parse } from 'svgson';
|
|
28
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
29
|
+
import { join } from 'node:path';
|
|
30
|
+
import { tmpdir } from 'node:os';
|
|
31
|
+
import { createHash } from 'node:crypto';
|
|
32
|
+
import { parsePath, parseColor, rectRadii, VOP_SHAPE, VOP_MOVE, VOP_LINE, VOP_QUAD, VOP_CUBIC, CAP, JOIN, PAINT_STRIDE, GRAD_LINEAR, GRAD_RADIAL, GRAD_CONIC, } from '../src/embedded-react/svg-ops.mjs';
|
|
50
33
|
const HALF_TURN_DEGREES = 180;
|
|
51
34
|
const PERCENT = 100;
|
|
52
35
|
const DEFAULT_SVG_SIZE = 100;
|
|
@@ -58,354 +41,343 @@ const GRADIENT_CENTER = 0.5; // objectBoundingBox defaults
|
|
|
58
41
|
const GRADIENT_RADIUS = 0.5;
|
|
59
42
|
const MIN_GRADIENT_STOPS = 2;
|
|
60
43
|
const RASTER_HASH_LENGTH = 8;
|
|
61
|
-
|
|
62
44
|
// --- 2D affine matrix [a, b, c, d, e, f]: x' = a*x + c*y + e ; y' = b*x + d*y + f ------------------
|
|
63
45
|
const IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
64
|
-
|
|
65
46
|
/** Compose outer ∘ inner (apply inner first, then outer). */
|
|
66
47
|
function mul(outer, inner) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
48
|
+
const [a1, b1, c1, d1, e1, f1] = outer;
|
|
49
|
+
const [a2, b2, c2, d2, e2, f2] = inner;
|
|
50
|
+
return [
|
|
51
|
+
a1 * a2 + c1 * b2,
|
|
52
|
+
b1 * a2 + d1 * b2,
|
|
53
|
+
a1 * c2 + c1 * d2,
|
|
54
|
+
b1 * c2 + d1 * d2,
|
|
55
|
+
a1 * e2 + c1 * f2 + e1,
|
|
56
|
+
b1 * e2 + d1 * f2 + f1,
|
|
57
|
+
];
|
|
77
58
|
}
|
|
78
|
-
|
|
79
59
|
/** Parses an SVG `transform` attribute (a chain of functions) into a single matrix. */
|
|
80
60
|
function parseTransform(transform) {
|
|
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
|
-
|
|
61
|
+
let matrix = IDENTITY;
|
|
62
|
+
if (!transform)
|
|
63
|
+
return matrix;
|
|
64
|
+
const pattern = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g;
|
|
65
|
+
let match;
|
|
66
|
+
while ((match = pattern.exec(transform)) !== null) {
|
|
67
|
+
const functionName = match[1];
|
|
68
|
+
const values = match[2]
|
|
69
|
+
.split(/[\s,]+/)
|
|
70
|
+
.map(Number)
|
|
71
|
+
.filter(value => !Number.isNaN(value));
|
|
72
|
+
let stepMatrix = IDENTITY;
|
|
73
|
+
if (functionName === 'matrix')
|
|
74
|
+
stepMatrix = [
|
|
75
|
+
values[0] || 0,
|
|
76
|
+
values[1] || 0,
|
|
77
|
+
values[2] || 0,
|
|
78
|
+
values[3] || 0,
|
|
79
|
+
values[4] || 0,
|
|
80
|
+
values[5] || 0,
|
|
81
|
+
];
|
|
82
|
+
else if (functionName === 'translate')
|
|
83
|
+
stepMatrix = [1, 0, 0, 1, values[0] || 0, values[1] || 0];
|
|
84
|
+
else if (functionName === 'scale')
|
|
85
|
+
stepMatrix = [
|
|
86
|
+
values[0] ?? 1,
|
|
87
|
+
0,
|
|
88
|
+
0,
|
|
89
|
+
values.length > 1 ? values[1] : (values[0] ?? 1),
|
|
90
|
+
0,
|
|
91
|
+
0,
|
|
92
|
+
];
|
|
93
|
+
else if (functionName === 'rotate') {
|
|
94
|
+
const radians = ((values[0] || 0) * Math.PI) / HALF_TURN_DEGREES;
|
|
95
|
+
const cos = Math.cos(radians);
|
|
96
|
+
const sin = Math.sin(radians);
|
|
97
|
+
const rotation = [cos, sin, -sin, cos, 0, 0];
|
|
98
|
+
// rotate(angle cx cy) rotates around a point.
|
|
99
|
+
stepMatrix =
|
|
100
|
+
values.length >= 3
|
|
101
|
+
? mul(mul([1, 0, 0, 1, values[1], values[2]], rotation), [
|
|
102
|
+
1,
|
|
103
|
+
0,
|
|
104
|
+
0,
|
|
105
|
+
1,
|
|
106
|
+
-values[1],
|
|
107
|
+
-values[2],
|
|
108
|
+
])
|
|
109
|
+
: rotation;
|
|
110
|
+
}
|
|
111
|
+
else if (functionName === 'skewX')
|
|
112
|
+
stepMatrix = [
|
|
113
|
+
1,
|
|
114
|
+
0,
|
|
115
|
+
Math.tan(((values[0] || 0) * Math.PI) / HALF_TURN_DEGREES),
|
|
116
|
+
1,
|
|
117
|
+
0,
|
|
118
|
+
0,
|
|
119
|
+
];
|
|
120
|
+
else if (functionName === 'skewY')
|
|
121
|
+
stepMatrix = [
|
|
122
|
+
1,
|
|
123
|
+
Math.tan(((values[0] || 0) * Math.PI) / HALF_TURN_DEGREES),
|
|
124
|
+
0,
|
|
125
|
+
1,
|
|
126
|
+
0,
|
|
127
|
+
0,
|
|
128
|
+
];
|
|
129
|
+
matrix = mul(matrix, stepMatrix);
|
|
130
|
+
}
|
|
131
|
+
return matrix;
|
|
150
132
|
}
|
|
151
|
-
|
|
152
133
|
/** Applies @p matrix to a point. */
|
|
153
134
|
function pt(matrix, x, y) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
135
|
+
return [
|
|
136
|
+
matrix[0] * x + matrix[2] * y + matrix[4],
|
|
137
|
+
matrix[1] * x + matrix[3] * y + matrix[5],
|
|
138
|
+
];
|
|
158
139
|
}
|
|
159
|
-
|
|
160
140
|
/** Transforms a path op array (no leading SHAPE op) by @p matrix. parsePath only emits MOVE/LINE/QUAD/
|
|
161
141
|
* CUBIC/CLOSE (it converts arcs to cubics), so every coordinate pair is a plain point — matrix-safe. */
|
|
162
142
|
function transformOps(ops, matrix) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
143
|
+
const transformed = [];
|
|
144
|
+
let index = 0;
|
|
145
|
+
while (index < ops.length) {
|
|
146
|
+
const opcode = ops[index++];
|
|
147
|
+
transformed.push(opcode);
|
|
148
|
+
if (opcode === VOP_MOVE || opcode === VOP_LINE) {
|
|
149
|
+
const [x, y] = pt(matrix, ops[index++], ops[index++]);
|
|
150
|
+
transformed.push(x, y);
|
|
151
|
+
}
|
|
152
|
+
else if (opcode === VOP_QUAD) {
|
|
153
|
+
const [controlX, controlY] = pt(matrix, ops[index++], ops[index++]);
|
|
154
|
+
const [x, y] = pt(matrix, ops[index++], ops[index++]);
|
|
155
|
+
transformed.push(controlX, controlY, x, y);
|
|
156
|
+
}
|
|
157
|
+
else if (opcode === VOP_CUBIC) {
|
|
158
|
+
const [control1X, control1Y] = pt(matrix, ops[index++], ops[index++]);
|
|
159
|
+
const [control2X, control2Y] = pt(matrix, ops[index++], ops[index++]);
|
|
160
|
+
const [x, y] = pt(matrix, ops[index++], ops[index++]);
|
|
161
|
+
transformed.push(control1X, control1Y, control2X, control2Y, x, y);
|
|
162
|
+
}
|
|
163
|
+
// VOP_CLOSE has no args.
|
|
180
164
|
}
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
return transformed;
|
|
165
|
+
return transformed;
|
|
184
166
|
}
|
|
185
|
-
|
|
186
167
|
/** Uniform stroke-width scale factor for a matrix (geometric mean of the axis scales). */
|
|
187
168
|
function scaleOf(matrix) {
|
|
188
|
-
|
|
189
|
-
Math.sqrt(Math.abs(matrix[0] * matrix[3] - matrix[1] * matrix[2])) || 1
|
|
190
|
-
);
|
|
169
|
+
return (Math.sqrt(Math.abs(matrix[0] * matrix[3] - matrix[1] * matrix[2])) || 1);
|
|
191
170
|
}
|
|
192
|
-
|
|
193
171
|
function num(value, fallback = 0) {
|
|
194
|
-
|
|
195
|
-
|
|
172
|
+
const parsed = parseFloat(value);
|
|
173
|
+
return Number.isNaN(parsed) ? fallback : parsed;
|
|
196
174
|
}
|
|
197
|
-
|
|
198
175
|
// --- Primitive shapes -> path `d` (emitted as lines/arcs; parsePath turns arcs into cubics) ----------
|
|
199
176
|
function rectPath(attributes) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
`M${x + rx} ${y}L${x + width - rx} ${y}A${rx} ${ry} 0 0 1 ${x + width} ${y + ry}` +
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
);
|
|
177
|
+
const x = num(attributes.x);
|
|
178
|
+
const y = num(attributes.y);
|
|
179
|
+
const width = num(attributes.width);
|
|
180
|
+
const height = num(attributes.height);
|
|
181
|
+
if (width <= 0 || height <= 0)
|
|
182
|
+
return '';
|
|
183
|
+
const [rx, ry] = rectRadii(attributes.rx, attributes.ry, width, height);
|
|
184
|
+
if (!rx || !ry)
|
|
185
|
+
return `M${x} ${y}L${x + width} ${y}L${x + width} ${y + height}L${x} ${y + height}Z`;
|
|
186
|
+
// Quarter-ellipse corners; parsePath lowers each `A` to the same single cubic the runtime emits.
|
|
187
|
+
return (`M${x + rx} ${y}L${x + width - rx} ${y}A${rx} ${ry} 0 0 1 ${x + width} ${y + ry}` +
|
|
188
|
+
`L${x + width} ${y + height - ry}A${rx} ${ry} 0 0 1 ${x + width - rx} ${y + height}` +
|
|
189
|
+
`L${x + rx} ${y + height}A${rx} ${ry} 0 0 1 ${x} ${y + height - ry}` +
|
|
190
|
+
`L${x} ${y + ry}A${rx} ${ry} 0 0 1 ${x + rx} ${y}Z`);
|
|
215
191
|
}
|
|
216
192
|
function circlePath(attributes) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
193
|
+
const cx = num(attributes.cx);
|
|
194
|
+
const cy = num(attributes.cy);
|
|
195
|
+
const r = num(attributes.r);
|
|
196
|
+
if (r <= 0)
|
|
197
|
+
return '';
|
|
198
|
+
return `M${cx - r} ${cy}A${r} ${r} 0 1 0 ${cx + r} ${cy}A${r} ${r} 0 1 0 ${cx - r} ${cy}Z`;
|
|
222
199
|
}
|
|
223
200
|
function ellipsePath(attributes) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
201
|
+
const cx = num(attributes.cx);
|
|
202
|
+
const cy = num(attributes.cy);
|
|
203
|
+
const rx = num(attributes.rx);
|
|
204
|
+
const ry = num(attributes.ry);
|
|
205
|
+
if (rx <= 0 || ry <= 0)
|
|
206
|
+
return '';
|
|
207
|
+
return `M${cx - rx} ${cy}A${rx} ${ry} 0 1 0 ${cx + rx} ${cy}A${rx} ${ry} 0 1 0 ${cx - rx} ${cy}Z`;
|
|
230
208
|
}
|
|
231
209
|
function linePath(attributes) {
|
|
232
|
-
|
|
210
|
+
return `M${num(attributes.x1)} ${num(attributes.y1)}L${num(attributes.x2)} ${num(attributes.y2)}`;
|
|
233
211
|
}
|
|
234
212
|
function polyPath(attributes, close) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
213
|
+
const coordinates = String(attributes.points || '')
|
|
214
|
+
.trim()
|
|
215
|
+
.split(/[\s,]+/)
|
|
216
|
+
.map(Number)
|
|
217
|
+
.filter(value => !Number.isNaN(value));
|
|
218
|
+
if (coordinates.length < 4)
|
|
219
|
+
return '';
|
|
220
|
+
let d = `M${coordinates[0]} ${coordinates[1]}`;
|
|
221
|
+
for (let index = 2; index + 1 < coordinates.length; index += 2)
|
|
222
|
+
d += `L${coordinates[index]} ${coordinates[index + 1]}`;
|
|
223
|
+
return close ? `${d}Z` : d;
|
|
245
224
|
}
|
|
246
|
-
|
|
247
225
|
// --- Paint resolution (presentation attrs + inline style + inheritance) ------------------------------
|
|
248
226
|
const PAINT_KEYS = [
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
227
|
+
'fill',
|
|
228
|
+
'stroke',
|
|
229
|
+
'stroke-width',
|
|
230
|
+
'stroke-linecap',
|
|
231
|
+
'stroke-linejoin',
|
|
232
|
+
'stroke-miterlimit',
|
|
233
|
+
'fill-rule',
|
|
234
|
+
'opacity',
|
|
235
|
+
'fill-opacity',
|
|
236
|
+
'stroke-opacity',
|
|
237
|
+
'stroke-dasharray',
|
|
260
238
|
];
|
|
261
239
|
const DEFAULT_PAINT = {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
240
|
+
fill: 'black',
|
|
241
|
+
stroke: 'none',
|
|
242
|
+
'stroke-width': '1',
|
|
243
|
+
'stroke-linecap': 'butt',
|
|
244
|
+
'stroke-linejoin': 'miter',
|
|
245
|
+
'stroke-miterlimit': '4',
|
|
246
|
+
'fill-rule': 'nonzero',
|
|
269
247
|
};
|
|
270
|
-
|
|
271
248
|
function parseStyle(style) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
249
|
+
const declarations = {};
|
|
250
|
+
if (!style)
|
|
251
|
+
return declarations;
|
|
252
|
+
for (const declaration of String(style).split(';')) {
|
|
253
|
+
const colon = declaration.indexOf(':');
|
|
254
|
+
if (colon > 0)
|
|
255
|
+
declarations[declaration.slice(0, colon).trim()] = declaration
|
|
256
|
+
.slice(colon + 1)
|
|
257
|
+
.trim();
|
|
258
|
+
}
|
|
259
|
+
return declarations;
|
|
282
260
|
}
|
|
283
|
-
|
|
284
261
|
function resolvePaint(attributes, inherited) {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
262
|
+
const style = parseStyle(attributes.style);
|
|
263
|
+
const paint = { ...inherited };
|
|
264
|
+
for (const key of PAINT_KEYS) {
|
|
265
|
+
const value = style[key] ?? attributes[key];
|
|
266
|
+
if (value != null)
|
|
267
|
+
paint[key] = value;
|
|
268
|
+
}
|
|
269
|
+
return paint;
|
|
292
270
|
}
|
|
293
|
-
|
|
294
271
|
/** Resolves a CSS color to a uint32 ARGB. url(...) paints (gradients/patterns) bake to nothing in v1. */
|
|
295
272
|
function colorOf(value) {
|
|
296
|
-
|
|
297
|
-
|
|
273
|
+
if (typeof value === 'string' && value.trim().startsWith('url('))
|
|
274
|
+
return 0;
|
|
275
|
+
return parseColor(value);
|
|
298
276
|
}
|
|
299
|
-
|
|
300
277
|
function clamp01(value) {
|
|
301
|
-
|
|
278
|
+
return value < 0 ? 0 : value > 1 ? 1 : value;
|
|
302
279
|
}
|
|
303
|
-
|
|
304
280
|
/** Multiplies an ARGB color's alpha by @p factor (SVG opacity / fill-opacity / stroke-opacity, baked in). */
|
|
305
281
|
function applyAlpha(argb, factor) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
282
|
+
if (factor >= 1)
|
|
283
|
+
return argb >>> 0;
|
|
284
|
+
const alpha = Math.round(((argb >>> 24) & 0xff) * clamp01(factor));
|
|
285
|
+
return ((alpha << 24) | (argb & 0xffffff)) >>> 0;
|
|
309
286
|
}
|
|
310
|
-
|
|
311
287
|
// --- Gradients (<linearGradient> / <radialGradient>) -------------------------------------------------
|
|
312
|
-
|
|
313
288
|
/** Parses <stop>s into [{ color: uint32 (stop-opacity baked in), offset: 0..1 }], in authored order. */
|
|
314
289
|
function parseStops(node) {
|
|
315
|
-
|
|
316
|
-
for (const child of node.children || []) {
|
|
317
|
-
if (child.type !== 'element' || child.name !== 'stop') continue;
|
|
318
|
-
const attributes = child.attributes || {};
|
|
319
|
-
const style = parseStyle(attributes.style);
|
|
320
|
-
const color = style['stop-color'] ?? attributes['stop-color'] ?? 'black';
|
|
321
|
-
const stopOpacity = clamp01(
|
|
322
|
-
num(style['stop-opacity'] ?? attributes['stop-opacity'], 1),
|
|
323
|
-
);
|
|
324
|
-
let offset = String(attributes.offset ?? '0').trim();
|
|
325
|
-
offset = offset.endsWith('%')
|
|
326
|
-
? parseFloat(offset) / PERCENT
|
|
327
|
-
: parseFloat(offset);
|
|
328
|
-
stops.push({
|
|
329
|
-
color: applyAlpha(parseColor(color), stopOpacity),
|
|
330
|
-
offset: clamp01(Number.isNaN(offset) ? 0 : offset),
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
return stops;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/** Collects every <linearGradient>/<radialGradient> by id (searched anywhere, including <defs>). */
|
|
337
|
-
function collectGradients(root) {
|
|
338
|
-
const gradients = new Map();
|
|
339
|
-
const visit = node => {
|
|
290
|
+
const stops = [];
|
|
340
291
|
for (const child of node.children || []) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
292
|
+
if (child.type !== 'element' || child.name !== 'stop')
|
|
293
|
+
continue;
|
|
294
|
+
const attributes = child.attributes || {};
|
|
295
|
+
const style = parseStyle(attributes.style);
|
|
296
|
+
const color = style['stop-color'] ?? attributes['stop-color'] ?? 'black';
|
|
297
|
+
const stopOpacity = clamp01(num(style['stop-opacity'] ?? attributes['stop-opacity'], 1));
|
|
298
|
+
const offsetText = String(attributes.offset ?? '0').trim();
|
|
299
|
+
const offset = offsetText.endsWith('%')
|
|
300
|
+
? parseFloat(offsetText) / PERCENT
|
|
301
|
+
: parseFloat(offsetText);
|
|
302
|
+
stops.push({
|
|
303
|
+
color: applyAlpha(parseColor(color), stopOpacity),
|
|
304
|
+
offset: clamp01(Number.isNaN(offset) ? 0 : offset),
|
|
353
305
|
});
|
|
354
|
-
visit(child);
|
|
355
306
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
307
|
+
return stops;
|
|
308
|
+
}
|
|
309
|
+
/** Collects every <linearGradient>/<radialGradient> by id (searched anywhere, including <defs>). */
|
|
310
|
+
function collectGradients(root) {
|
|
311
|
+
const gradients = new Map();
|
|
312
|
+
const visit = (node) => {
|
|
313
|
+
for (const child of node.children || []) {
|
|
314
|
+
if (child.type !== 'element')
|
|
315
|
+
continue;
|
|
316
|
+
if ((child.name === 'linearGradient' ||
|
|
317
|
+
child.name === 'radialGradient' ||
|
|
318
|
+
child.name === 'conicGradient') &&
|
|
319
|
+
child.attributes &&
|
|
320
|
+
child.attributes.id)
|
|
321
|
+
gradients.set(child.attributes.id, {
|
|
322
|
+
name: child.name,
|
|
323
|
+
attributes: child.attributes,
|
|
324
|
+
stops: parseStops(child),
|
|
325
|
+
});
|
|
326
|
+
visit(child);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
visit(root);
|
|
330
|
+
return gradients;
|
|
359
331
|
}
|
|
360
|
-
|
|
361
332
|
/** "url(#id)" -> "id" (else null). */
|
|
362
333
|
function urlRef(value) {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return match ? match[1] : null;
|
|
334
|
+
const match = typeof value === 'string' && value.trim().match(/^url\(\s*#([^)\s]+)\s*\)/);
|
|
335
|
+
return match ? match[1] : null;
|
|
366
336
|
}
|
|
367
|
-
|
|
368
337
|
/** A gradient coordinate: "50%" -> 0.5; a plain number passes through; NaN or infinite returns `fallback`. */
|
|
369
338
|
function gradCoord(value, fallback) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
339
|
+
if (value == null)
|
|
340
|
+
return fallback;
|
|
341
|
+
const text = String(value).trim();
|
|
342
|
+
const parsed = text.endsWith('%')
|
|
343
|
+
? parseFloat(text) / PERCENT
|
|
344
|
+
: parseFloat(text);
|
|
345
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
376
346
|
}
|
|
377
|
-
|
|
378
347
|
/** Local-space bounding box of a path-op array (no SHAPE header; MOVE/LINE/QUAD/CUBIC, CLOSE has no args). */
|
|
379
348
|
function opsBBox(ops) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
349
|
+
let minX = Infinity;
|
|
350
|
+
let minY = Infinity;
|
|
351
|
+
let maxX = -Infinity;
|
|
352
|
+
let maxY = -Infinity;
|
|
353
|
+
let index = 0;
|
|
354
|
+
while (index < ops.length) {
|
|
355
|
+
const opcode = ops[index++];
|
|
356
|
+
const coordinateCount = opcode === VOP_MOVE || opcode === VOP_LINE
|
|
357
|
+
? 2
|
|
358
|
+
: opcode === VOP_QUAD
|
|
359
|
+
? 4
|
|
360
|
+
: opcode === VOP_CUBIC
|
|
361
|
+
? 6
|
|
362
|
+
: 0;
|
|
363
|
+
for (let offset = 0; offset < coordinateCount; offset += 2) {
|
|
364
|
+
const x = ops[index + offset];
|
|
365
|
+
const y = ops[index + offset + 1];
|
|
366
|
+
if (x < minX)
|
|
367
|
+
minX = x;
|
|
368
|
+
if (x > maxX)
|
|
369
|
+
maxX = x;
|
|
370
|
+
if (y < minY)
|
|
371
|
+
minY = y;
|
|
372
|
+
if (y > maxY)
|
|
373
|
+
maxY = y;
|
|
374
|
+
}
|
|
375
|
+
index += coordinateCount;
|
|
402
376
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return {x: minX, y: minY, width: maxX - minX, height: maxY - minY};
|
|
377
|
+
if (!Number.isFinite(minX))
|
|
378
|
+
return { x: 0, y: 0, width: 0, height: 0 };
|
|
379
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
407
380
|
}
|
|
408
|
-
|
|
409
381
|
/**
|
|
410
382
|
* Bakes a gradient definition into an engine gradient descriptor in the path's BAKED coordinate space.
|
|
411
383
|
* objectBoundingBox units (the SVG default) map [0,1] onto the shape's local bbox; userSpaceOnUse uses the
|
|
@@ -416,258 +388,218 @@ function opsBBox(ops) {
|
|
|
416
388
|
* under a transform, and a non-square OBB contributes sqrt(w*h)). Conic likewise keeps only the rotation.
|
|
417
389
|
*/
|
|
418
390
|
function bakeGradient(definition, shapeMatrix, bbox) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
(attributes.
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
return pt(shapeMatrix, transformedX, transformedY);
|
|
432
|
-
};
|
|
433
|
-
if (definition.name === 'radialGradient') {
|
|
434
|
-
const [centerX, centerY] = toBaked(
|
|
435
|
-
gradCoord(attributes.cx, GRADIENT_CENTER),
|
|
436
|
-
gradCoord(attributes.cy, GRADIENT_CENTER),
|
|
437
|
-
);
|
|
438
|
-
// objectBoundingBox radius r=0.5 spans half the box; for a non-square box the geometric mean sqrt(w*h)
|
|
439
|
-
// is the best-fit circle (vs the diagonal, which oversizes a wide/tall box).
|
|
440
|
-
const userRadius = objectBoundingBox
|
|
441
|
-
? gradCoord(attributes.r, GRADIENT_RADIUS) *
|
|
442
|
-
Math.sqrt(bbox.width * bbox.height)
|
|
443
|
-
: gradCoord(attributes.r, GRADIENT_RADIUS);
|
|
444
|
-
return {
|
|
445
|
-
type: GRAD_RADIAL,
|
|
446
|
-
stops: definition.stops,
|
|
447
|
-
ax: centerX,
|
|
448
|
-
ay: centerY,
|
|
449
|
-
bx: 0,
|
|
450
|
-
by: 0,
|
|
451
|
-
r: userRadius * scaleOf(shapeMatrix) * scaleOf(gradientMatrix),
|
|
391
|
+
const attributes = definition.attributes;
|
|
392
|
+
const objectBoundingBox = (attributes.gradientUnits || 'objectBoundingBox') !== 'userSpaceOnUse';
|
|
393
|
+
const gradientMatrix = parseTransform(attributes.gradientTransform);
|
|
394
|
+
const toBaked = (gradientX, gradientY) => {
|
|
395
|
+
const userX = objectBoundingBox
|
|
396
|
+
? bbox.x + gradientX * bbox.width
|
|
397
|
+
: gradientX;
|
|
398
|
+
const userY = objectBoundingBox
|
|
399
|
+
? bbox.y + gradientY * bbox.height
|
|
400
|
+
: gradientY;
|
|
401
|
+
const [transformedX, transformedY] = pt(gradientMatrix, userX, userY);
|
|
402
|
+
return pt(shapeMatrix, transformedX, transformedY);
|
|
452
403
|
};
|
|
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
|
-
|
|
404
|
+
if (definition.name === 'radialGradient') {
|
|
405
|
+
const [centerX, centerY] = toBaked(gradCoord(attributes.cx, GRADIENT_CENTER), gradCoord(attributes.cy, GRADIENT_CENTER));
|
|
406
|
+
// objectBoundingBox radius r=0.5 spans half the box; for a non-square box the geometric mean sqrt(w*h)
|
|
407
|
+
// is the best-fit circle (vs the diagonal, which oversizes a wide/tall box).
|
|
408
|
+
const userRadius = objectBoundingBox
|
|
409
|
+
? gradCoord(attributes.r, GRADIENT_RADIUS) *
|
|
410
|
+
Math.sqrt(bbox.width * bbox.height)
|
|
411
|
+
: gradCoord(attributes.r, GRADIENT_RADIUS);
|
|
412
|
+
return {
|
|
413
|
+
type: GRAD_RADIAL,
|
|
414
|
+
stops: definition.stops,
|
|
415
|
+
ax: centerX,
|
|
416
|
+
ay: centerY,
|
|
417
|
+
bx: 0,
|
|
418
|
+
by: 0,
|
|
419
|
+
r: userRadius * scaleOf(shapeMatrix) * scaleOf(gradientMatrix),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
if (definition.name === 'conicGradient') {
|
|
423
|
+
const [centerX, centerY] = toBaked(gradCoord(attributes.cx, GRADIENT_CENTER), gradCoord(attributes.cy, GRADIENT_CENTER));
|
|
424
|
+
// `from` (degrees, clockwise from the top, CSS conic-gradient convention) -> radians, plus the rotation
|
|
425
|
+
// baked into the gradient + path transforms so the sweep orients correctly in the final coordinate space.
|
|
426
|
+
const rotation = Math.atan2(shapeMatrix[1], shapeMatrix[0]) +
|
|
427
|
+
Math.atan2(gradientMatrix[1], gradientMatrix[0]);
|
|
428
|
+
return {
|
|
429
|
+
type: GRAD_CONIC,
|
|
430
|
+
stops: definition.stops,
|
|
431
|
+
ax: centerX,
|
|
432
|
+
ay: centerY,
|
|
433
|
+
bx: 0,
|
|
434
|
+
by: 0,
|
|
435
|
+
r: (num(attributes.from, 0) * Math.PI) / HALF_TURN_DEGREES + rotation,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const [ax, ay] = toBaked(gradCoord(attributes.x1, 0), gradCoord(attributes.y1, 0));
|
|
439
|
+
const [bx, by] = toBaked(gradCoord(attributes.x2, 1), gradCoord(attributes.y2, 0));
|
|
440
|
+
return { type: GRAD_LINEAR, stops: definition.stops, ax, ay, bx, by, r: 0 };
|
|
483
441
|
}
|
|
484
|
-
|
|
485
442
|
/**
|
|
486
443
|
* Compiles an SVG document string into the engine vector op-tape.
|
|
487
444
|
*
|
|
488
|
-
* @param
|
|
489
|
-
* @returns
|
|
445
|
+
* @param svgString The full SVG file contents.
|
|
446
|
+
* @returns op-tape + intrinsic
|
|
490
447
|
* size (px). `ops`/`paints` are plain arrays ready to inline into the bundle; `width`/`height`
|
|
491
448
|
* are the SVG's rendered size for layout (a <Svg source> scales the tape to its box).
|
|
492
449
|
*/
|
|
493
450
|
export async function svgToVector(svgString) {
|
|
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
|
-
applyAlpha(
|
|
610
|
-
colorOf(childPaint.stroke ?? 'none'),
|
|
611
|
-
opacity * clamp01(num(childPaint['stroke-opacity'], 1)),
|
|
612
|
-
),
|
|
613
|
-
num(childPaint['stroke-width'], DEFAULT_STROKE_WIDTH) *
|
|
614
|
-
scaleOf(childMatrix),
|
|
615
|
-
num(childPaint['stroke-miterlimit'], DEFAULT_MITER_LIMIT),
|
|
616
|
-
CAP[childPaint['stroke-linecap']] ?? 0,
|
|
617
|
-
JOIN[childPaint['stroke-linejoin']] ?? 0,
|
|
618
|
-
childPaint['fill-rule'] === 'evenodd'
|
|
619
|
-
? FILL_RULE_EVEN_ODD
|
|
620
|
-
: FILL_RULE_NONZERO,
|
|
621
|
-
fillGradient,
|
|
622
|
-
strokeGradient,
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
};
|
|
626
|
-
|
|
627
|
-
walk(root, rootMatrix, DEFAULT_PAINT);
|
|
628
|
-
return {ops, paints, gradients, width, height, dropped: [...dropped].sort()};
|
|
451
|
+
const root = await parse(String(svgString));
|
|
452
|
+
const attributes = root.attributes || {};
|
|
453
|
+
let viewBox = String(attributes.viewBox || '')
|
|
454
|
+
.trim()
|
|
455
|
+
.split(/[\s,]+/)
|
|
456
|
+
.map(Number);
|
|
457
|
+
if (viewBox.length !== 4 || viewBox.some(value => Number.isNaN(value)))
|
|
458
|
+
viewBox = [
|
|
459
|
+
0,
|
|
460
|
+
0,
|
|
461
|
+
num(attributes.width, DEFAULT_SVG_SIZE),
|
|
462
|
+
num(attributes.height, DEFAULT_SVG_SIZE),
|
|
463
|
+
];
|
|
464
|
+
const [viewBoxX, viewBoxY, viewBoxWidth, viewBoxHeight] = viewBox;
|
|
465
|
+
const width = num(attributes.width, viewBoxWidth);
|
|
466
|
+
const height = num(attributes.height, viewBoxHeight);
|
|
467
|
+
// Root transform: map the viewBox user-units onto the intrinsic px box (like the inline <Svg> path).
|
|
468
|
+
const scaleX = viewBoxWidth ? width / viewBoxWidth : 1;
|
|
469
|
+
const scaleY = viewBoxHeight ? height / viewBoxHeight : 1;
|
|
470
|
+
const rootMatrix = mul([scaleX, 0, 0, scaleY, -viewBoxX * scaleX, -viewBoxY * scaleY], parseTransform(attributes.transform));
|
|
471
|
+
const ops = [];
|
|
472
|
+
const paints = [];
|
|
473
|
+
const gradients = [];
|
|
474
|
+
const gradientDefinitions = collectGradients(root);
|
|
475
|
+
// Elements we intentionally skip without losing visible content (gradient defs are collected separately,
|
|
476
|
+
// the rest are metadata). Anything else hitting the skip branch — text/image/use/mask/filter/pattern/… —
|
|
477
|
+
// is unsupported VISUAL content, recorded in `dropped` so the loader can warn + fall back to raster.
|
|
478
|
+
const SVG_IGNORABLE = new Set([
|
|
479
|
+
'defs',
|
|
480
|
+
'title',
|
|
481
|
+
'desc',
|
|
482
|
+
'metadata',
|
|
483
|
+
'linearGradient',
|
|
484
|
+
'radialGradient',
|
|
485
|
+
'conicGradient',
|
|
486
|
+
]);
|
|
487
|
+
const dropped = new Set();
|
|
488
|
+
const walk = (node, matrix, paint) => {
|
|
489
|
+
for (const child of node.children || []) {
|
|
490
|
+
if (child.type !== 'element')
|
|
491
|
+
continue;
|
|
492
|
+
const childAttributes = child.attributes || {};
|
|
493
|
+
const childMatrix = mul(matrix, parseTransform(childAttributes.transform));
|
|
494
|
+
const childPaint = resolvePaint(childAttributes, paint);
|
|
495
|
+
if (child.name === 'g' || child.name === 'svg') {
|
|
496
|
+
walk(child, childMatrix, childPaint);
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
let d = '';
|
|
500
|
+
if (child.name === 'path')
|
|
501
|
+
d = childAttributes.d || '';
|
|
502
|
+
else if (child.name === 'rect')
|
|
503
|
+
d = rectPath(childAttributes);
|
|
504
|
+
else if (child.name === 'circle')
|
|
505
|
+
d = circlePath(childAttributes);
|
|
506
|
+
else if (child.name === 'ellipse')
|
|
507
|
+
d = ellipsePath(childAttributes);
|
|
508
|
+
else if (child.name === 'line')
|
|
509
|
+
d = linePath(childAttributes);
|
|
510
|
+
else if (child.name === 'polygon')
|
|
511
|
+
d = polyPath(childAttributes, true);
|
|
512
|
+
else if (child.name === 'polyline')
|
|
513
|
+
d = polyPath(childAttributes, false);
|
|
514
|
+
else {
|
|
515
|
+
if (!SVG_IGNORABLE.has(child.name))
|
|
516
|
+
dropped.add(child.name); // unsupported visual element → raster
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (!d)
|
|
520
|
+
continue;
|
|
521
|
+
// A filter/mask/clip applied to an otherwise-bakeable shape still loses fidelity as a vector → flag it.
|
|
522
|
+
for (const feature of ['filter', 'mask', 'clip-path']) {
|
|
523
|
+
if (childAttributes[feature] && childAttributes[feature] !== 'none')
|
|
524
|
+
dropped.add(feature);
|
|
525
|
+
}
|
|
526
|
+
// A dashed stroke can't be represented in the op-tape (the stroke is baked solid). Flag it so the SVG
|
|
527
|
+
// falls back to raster (resvg renders the dashes) — but only when a visible stroke is actually dashed
|
|
528
|
+
// by a positive pattern, so a stray stroke-dasharray on an unstroked/zero-width shape isn't penalized.
|
|
529
|
+
const dash = childPaint['stroke-dasharray'];
|
|
530
|
+
const stroked = childPaint.stroke &&
|
|
531
|
+
childPaint.stroke !== 'none' &&
|
|
532
|
+
num(childPaint['stroke-width'], DEFAULT_STROKE_WIDTH) > 0;
|
|
533
|
+
if (stroked && dash && dash !== 'none' && /[1-9]/.test(dash))
|
|
534
|
+
dropped.add('stroke-dasharray');
|
|
535
|
+
const shapeOps = parsePath(d);
|
|
536
|
+
if (!shapeOps.length)
|
|
537
|
+
continue;
|
|
538
|
+
const paintIndex = paints.length / PAINT_STRIDE;
|
|
539
|
+
const opacity = clamp01(num(childPaint.opacity, 1));
|
|
540
|
+
// A url(#id) fill/stroke referencing a parsed gradient bakes to a 1-based gradient-table index; the
|
|
541
|
+
// solid color stays 0 (colorOf returns 0 for url()). The shape's bbox is computed once, lazily.
|
|
542
|
+
let bbox = null;
|
|
543
|
+
const gradientIndex = (reference) => {
|
|
544
|
+
if (!reference)
|
|
545
|
+
return 0;
|
|
546
|
+
const definition = gradientDefinitions.get(reference);
|
|
547
|
+
if (!definition || definition.stops.length < MIN_GRADIENT_STOPS)
|
|
548
|
+
return 0;
|
|
549
|
+
if (!bbox)
|
|
550
|
+
bbox = opsBBox(shapeOps);
|
|
551
|
+
gradients.push(bakeGradient(definition, childMatrix, bbox));
|
|
552
|
+
return gradients.length; // 1-based
|
|
553
|
+
};
|
|
554
|
+
const fillGradient = gradientIndex(urlRef(childPaint.fill));
|
|
555
|
+
const strokeGradient = gradientIndex(urlRef(childPaint.stroke));
|
|
556
|
+
ops.push(VOP_SHAPE, paintIndex, ...transformOps(shapeOps, childMatrix));
|
|
557
|
+
paints.push(applyAlpha(colorOf(childPaint.fill ?? 'black'), opacity * clamp01(num(childPaint['fill-opacity'], 1))), applyAlpha(colorOf(childPaint.stroke ?? 'none'), opacity * clamp01(num(childPaint['stroke-opacity'], 1))), num(childPaint['stroke-width'], DEFAULT_STROKE_WIDTH) *
|
|
558
|
+
scaleOf(childMatrix), num(childPaint['stroke-miterlimit'], DEFAULT_MITER_LIMIT), CAP[childPaint['stroke-linecap']] ?? 0, JOIN[childPaint['stroke-linejoin']] ?? 0, childPaint['fill-rule'] === 'evenodd'
|
|
559
|
+
? FILL_RULE_EVEN_ODD
|
|
560
|
+
: FILL_RULE_NONZERO, fillGradient, strokeGradient);
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
walk(root, rootMatrix, DEFAULT_PAINT);
|
|
564
|
+
return { ops, paints, gradients, width, height, dropped: [...dropped].sort() };
|
|
629
565
|
}
|
|
630
|
-
|
|
631
566
|
/**
|
|
632
567
|
* Rasterizes an SVG to an RGBA PNG at its intrinsic size — the raster fallback for SVGs that use features
|
|
633
568
|
* the vector baker can't represent (text, masks, filters, patterns, …). resvg is
|
|
634
569
|
* imported lazily, so vector-only builds never load it. The PNG flows through the normal image pipeline
|
|
635
570
|
* (bake-image → asset pack), and a <Svg source> whose artifact is {kind:'raster'} renders it as an image.
|
|
636
571
|
*
|
|
637
|
-
* @param
|
|
638
|
-
* @returns
|
|
572
|
+
* @param svgString The SVG document.
|
|
573
|
+
* @returns Rendered size and PNG bytes.
|
|
639
574
|
*/
|
|
640
575
|
export async function svgToRaster(svgString) {
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
576
|
+
const { Resvg } = await import('@resvg/resvg-js');
|
|
577
|
+
// resvg (usvg) requires the SVG namespace; svgson (the vector baker) is lenient and many hand-authored
|
|
578
|
+
// SVGs omit it, so inject it when absent or resvg rejects the document ("does not have a root node").
|
|
579
|
+
let svg = String(svgString);
|
|
580
|
+
if (!/<svg\b[^>]*\sxmlns\s*=/i.test(svg))
|
|
581
|
+
svg = svg.replace(/<svg\b/i, '<svg xmlns="http://www.w3.org/2000/svg"');
|
|
582
|
+
const resvg = new Resvg(svg, { background: 'rgba(0,0,0,0)' }); // transparent so it composites
|
|
583
|
+
const rendered = resvg.render();
|
|
584
|
+
const png = rendered.asPng();
|
|
585
|
+
const { width, height } = rendered;
|
|
586
|
+
// Only the wasm build has free().
|
|
587
|
+
rendered.free?.();
|
|
588
|
+
return { width, height, png };
|
|
653
589
|
}
|
|
654
|
-
|
|
655
590
|
/**
|
|
656
591
|
* Writes a rasterized-SVG PNG to a temp file and returns its path (content-hashed so rebuilds are stable).
|
|
657
592
|
* The image pipeline (bakeImage) reads PNGs by path, so the raster fallback writes one here and registers it
|
|
658
593
|
* as an asset. Shared by the Flow A loader and the Flow B (AOT) baker.
|
|
659
594
|
*
|
|
660
|
-
* @param
|
|
661
|
-
* @param
|
|
662
|
-
* @returns
|
|
595
|
+
* @param name Asset name (the SVG's basename) — used in the filename.
|
|
596
|
+
* @param png PNG bytes from svgToRaster.
|
|
597
|
+
* @returns Absolute path to the written .png.
|
|
663
598
|
*/
|
|
664
599
|
export function writeRasterPng(name, png) {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
);
|
|
671
|
-
writeFileSync(file, png);
|
|
672
|
-
return file;
|
|
600
|
+
const directory = join(tmpdir(), 'embedded-react-svg-raster');
|
|
601
|
+
mkdirSync(directory, { recursive: true });
|
|
602
|
+
const file = join(directory, `${name}-${createHash('sha1').update(png).digest('hex').slice(0, RASTER_HASH_LENGTH)}.png`);
|
|
603
|
+
writeFileSync(file, png);
|
|
604
|
+
return file;
|
|
673
605
|
}
|