embedded-react 0.19.0 → 0.20.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 +7 -3
- package/aot/compile.mjs +178 -58
- package/aot/compiler/animations.mjs +290 -281
- package/aot/compiler/boot.mjs +70 -33
- package/aot/compiler/branch-merge.mjs +4 -3
- package/aot/compiler/check.mjs +9 -9
- package/aot/compiler/collect.mjs +324 -205
- package/aot/compiler/control-flow.mjs +209 -197
- package/aot/compiler/diagnostics.mjs +22 -25
- package/aot/compiler/elements/activity-indicator.mjs +25 -24
- package/aot/compiler/elements/dial.mjs +91 -77
- package/aot/compiler/elements/flat-list.mjs +24 -22
- package/aot/compiler/elements/image.mjs +51 -50
- package/aot/compiler/elements/modal.mjs +31 -30
- package/aot/compiler/elements/props.mjs +23 -23
- package/aot/compiler/elements/switch.mjs +42 -34
- package/aot/compiler/elements/text-input.mjs +34 -32
- package/aot/compiler/expressions.mjs +372 -333
- package/aot/compiler/handlers.mjs +473 -387
- package/aot/compiler/ir/functions.mjs +11 -1
- package/aot/compiler/ir/nodes.mjs +4 -4
- package/aot/compiler/ir/reads.mjs +19 -19
- package/aot/compiler/ir/types.mjs +14 -14
- package/aot/compiler/ir/vectors.mjs +5 -1
- package/aot/compiler/keyboard.mjs +17 -7
- package/aot/compiler/lower.mjs +77 -80
- package/aot/compiler/nodes.mjs +169 -152
- package/aot/compiler/options.mjs +35 -30
- package/aot/compiler/out.mjs +18 -22
- package/aot/compiler/package/animations.mjs +669 -0
- package/aot/compiler/package/arena.mjs +252 -0
- package/aot/compiler/package/assemble.mjs +177 -0
- package/aot/compiler/package/bytecode.mjs +286 -0
- package/aot/compiler/package/checks.mjs +1022 -0
- package/aot/compiler/package/codegen.mjs +1329 -0
- package/aot/compiler/package/enums.mjs +138 -0
- package/aot/compiler/package/format.mjs +760 -0
- package/aot/compiler/package/layouts.mjs +136 -0
- package/aot/compiler/package/lower.mjs +1606 -0
- package/aot/compiler/package/props.mjs +118 -0
- package/aot/compiler/package/tables.mjs +103 -0
- package/aot/compiler/package/vectors.mjs +502 -0
- package/aot/compiler/package/writer.mjs +664 -0
- package/aot/compiler/pan-responder.mjs +69 -61
- package/aot/compiler/parse.mjs +21 -21
- package/aot/compiler/static-eval.mjs +25 -23
- package/aot/compiler/style-text.mjs +161 -172
- package/aot/compiler/svg.mjs +477 -251
- package/aot/screenshot-smoke.mjs +54 -40
- package/aot/style-map.mjs +102 -90
- package/assets/bake-font.mjs +123 -77
- package/assets/bake-image.mjs +39 -22
- package/assets/bake-svg.mjs +361 -262
- package/assets/build-builtin-font.mjs +11 -7
- package/assets/emit-c.mjs +77 -58
- package/assets/emit-container.mjs +123 -37
- package/assets/emit-pack.mjs +77 -67
- package/assets/font-config.mjs +9 -6
- package/assets/font-sizes.mjs +163 -123
- package/assets/glyph-coverage.mjs +74 -54
- package/assets/index.mjs +31 -9
- package/assets/rasterize.mjs +126 -99
- package/assets/svg-loader.mjs +8 -8
- package/build.mjs +14 -12
- package/cli.mjs +359 -172
- package/compile-bin.mjs +3 -3
- package/flash.mjs +537 -0
- package/hotreload/dev-device.mjs +90 -33
- package/hotreload/pack-app.mjs +55 -43
- package/hotreload/pack-split.mjs +46 -37
- package/hotreload/package-transfer.mjs +175 -0
- package/hotreload/protocol.mjs +50 -37
- package/hotreload/uploader.mjs +38 -27
- package/hotreload/watch-tree.mjs +19 -19
- package/pack-container.mjs +28 -20
- package/package.json +7 -8
- package/persist-transform.mjs +34 -28
- package/qjsc-wasm.mjs +25 -14
- package/sim/embedded-react.cjs +1 -1
- package/sim/embedded-react.js +1 -1
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +94 -85
- package/sim-server.mjs +101 -82
- package/src/embedded-react/Animated.js +24 -24
- package/src/embedded-react/Easing.js +1 -1
- package/src/embedded-react/FlatList.js +3 -3
- package/src/embedded-react/LayoutAnimation.js +8 -5
- package/src/embedded-react/PanResponder.js +75 -71
- package/src/embedded-react/Pressable.js +2 -8
- package/src/embedded-react/SectionList.js +6 -6
- package/src/embedded-react/StyleSheet.js +4 -3
- package/src/embedded-react/TouchableOpacity.js +13 -8
- package/src/embedded-react/index.d.ts +5 -5
- package/src/embedded-react/layout-anim-config.js +10 -4
- package/src/embedded-react/list-child.js +5 -5
- package/src/embedded-react/named-colors.js +1 -1
- package/src/embedded-react/split-style.js +11 -11
- package/src/embedded-react/svg-ops.js +760 -593
- package/src/embedded-react/useHostValue.js +3 -3
- package/src/embedded-react/usePersistentState.js +6 -6
- package/src/embedded-react/warn-props.js +1 -1
- package/src/host-config.js +60 -53
- package/src/props.js +49 -41
- package/src/renderer.js +7 -4
- package/aot/compiler/assemble.mjs +0 -944
- package/aot/compiler/c/animations.mjs +0 -436
- package/aot/compiler/c/boot.mjs +0 -127
- package/aot/compiler/c/expressions.mjs +0 -270
- package/aot/compiler/c/functions.mjs +0 -268
- package/aot/compiler/c/keyboard.mjs +0 -96
- package/aot/compiler/c/nodes.mjs +0 -303
- package/aot/compiler/c/pan-responder.mjs +0 -136
- package/aot/compiler/c/statements.mjs +0 -319
- package/aot/compiler/c/text.mjs +0 -83
- package/aot/compiler/c/vectors.mjs +0 -381
- package/aot/compiler/c-syntax.mjs +0 -127
package/aot/compiler/svg.mjs
CHANGED
|
@@ -18,14 +18,16 @@
|
|
|
18
18
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
19
19
|
import { check, checkNumber } from './check.mjs';
|
|
20
20
|
import { lowerStyle, NODE_TYPES } from '../style-map.mjs';
|
|
21
|
-
import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_PROPS, PAINT_STRIDE, scaleVectorArtifact, } from '../../src/embedded-react/svg-ops.js';
|
|
22
|
-
import { aotError } from './diagnostics.mjs';
|
|
21
|
+
import { flattenSvg, parseColor, parsePath, KAPPA, PAINT_PROPS, PAINT_STRIDE, scaleVectorArtifact, VOP_ARC, VOP_CUBIC, VOP_LINE, VOP_MOVE, VOP_QUAD, VOP_SHAPE, } from '../../src/embedded-react/svg-ops.js';
|
|
22
|
+
import { aotError, currentLoc } from './diagnostics.mjs';
|
|
23
|
+
import { bootSlotOf, bootSourceText, buildValueOf, containsBoot, foldBoot, isBoot, } from './boot.mjs';
|
|
23
24
|
import { evalStatic, withUndefined } from './static-eval.mjs';
|
|
24
25
|
import { lowerExpr } from './expressions.mjs';
|
|
25
|
-
import { lowerColorValue, collectStyleAssigns, styleWrites, } from './style-text.mjs';
|
|
26
|
+
import { lowerColorValue, bootStyleAssign, collectStyleAssigns, styleWrites, } from './style-text.mjs';
|
|
26
27
|
import { checkInterpolations, lowerRefBinds } from './nodes.mjs';
|
|
27
28
|
import { at, checkAttributes, checkNoChildren, elementSpec, svgShapeSpec, } from './elements/props.mjs';
|
|
28
|
-
import { MAX_GRADIENT_STOPS
|
|
29
|
+
import { MAX_GRADIENT_STOPS } from './ir/vectors.mjs';
|
|
30
|
+
import { FLOAT } from './ir/types.mjs';
|
|
29
31
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
32
|
- Constants
|
|
31
33
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -33,8 +35,23 @@ import { MAX_GRADIENT_STOPS, printTapeValue } from './c/vectors.mjs';
|
|
|
33
35
|
export const CAP_MAP = { butt: 0, round: 1, square: 2 };
|
|
34
36
|
/** strokeLinejoin → ERVectorPaint join. */
|
|
35
37
|
export const JOIN_MAP = { miter: 0, round: 1, bevel: 2 };
|
|
36
|
-
/**
|
|
37
|
-
const
|
|
38
|
+
/** SVG's stroke-miterlimit when a shape sets none. */
|
|
39
|
+
export const DEFAULT_MITER_LIMIT = 4;
|
|
40
|
+
/** Gradient types: 1 linear, 2 radial, 3 conic. */
|
|
41
|
+
const GRADIENT_LINEAR = 1;
|
|
42
|
+
const GRADIENT_CONIC = 3;
|
|
43
|
+
/** Where each field sits in a flattenSvg paint record. */
|
|
44
|
+
const PAINT_RECORD = {
|
|
45
|
+
fill: 0,
|
|
46
|
+
stroke: 1,
|
|
47
|
+
strokeWidth: 2,
|
|
48
|
+
miter: 3,
|
|
49
|
+
cap: 4,
|
|
50
|
+
join: 5,
|
|
51
|
+
fillRule: 6,
|
|
52
|
+
fillGradient: 7,
|
|
53
|
+
strokeGradient: 8,
|
|
54
|
+
};
|
|
38
55
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
39
56
|
- Implementation
|
|
40
57
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -58,7 +75,7 @@ function checkShape(child) {
|
|
|
58
75
|
}
|
|
59
76
|
}
|
|
60
77
|
/**
|
|
61
|
-
* Refuses a constant `
|
|
78
|
+
* Refuses a constant `fillGradient` / `strokeGradient` the engine cannot draw as written: the same type and stop rules
|
|
62
79
|
* a state-driven one meets (see lowerGradient). `null` is no gradient.
|
|
63
80
|
*
|
|
64
81
|
* @param name The attribute.
|
|
@@ -69,7 +86,9 @@ function checkStaticGradient(name, value) {
|
|
|
69
86
|
return;
|
|
70
87
|
const gradient = value;
|
|
71
88
|
if (typeof value !== 'object' ||
|
|
72
|
-
!
|
|
89
|
+
!Number.isInteger(gradient.type) ||
|
|
90
|
+
gradient.type < GRADIENT_LINEAR ||
|
|
91
|
+
gradient.type > GRADIENT_CONIC) {
|
|
73
92
|
throw aotError(`AOT: "${name}.type" must be 1 (linear), 2 (radial) or 3 (conic)`);
|
|
74
93
|
}
|
|
75
94
|
if (!Array.isArray(gradient.stops)) {
|
|
@@ -86,7 +105,7 @@ function checkStaticGradient(name, value) {
|
|
|
86
105
|
* Yields an <Svg> subtree's shape children in source order, inlining any `<>…</>` in place: a fragment
|
|
87
106
|
* carries no paint and no transform, so it is transparent to the tape (the Flow A walk in flattenSvg
|
|
88
107
|
* treats it the same way). Whitespace between elements and JSX comments are skipped; anything else
|
|
89
|
-
* that has no shape to contribute throws, rather than vanishing from the
|
|
108
|
+
* that has no shape to contribute throws, rather than vanishing from the package.
|
|
90
109
|
*
|
|
91
110
|
* @param children An <Svg>'s (or fragment's) children.
|
|
92
111
|
*
|
|
@@ -116,6 +135,32 @@ function* svgShapeChildren(children) {
|
|
|
116
135
|
}
|
|
117
136
|
}
|
|
118
137
|
}
|
|
138
|
+
/** A drawing's value worked out from `screen`, which a tape can't hold: a compile error, not a state-driven value. */
|
|
139
|
+
class SvgScreenError extends Error {
|
|
140
|
+
aotLoc;
|
|
141
|
+
aotHint;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Folds a value of a drawing. A drawing is baked once, so what it draws can't change with the panel: it fails the
|
|
145
|
+
* build, naming the expression, instead of adding a layout for every size.
|
|
146
|
+
*
|
|
147
|
+
* @param node The expression.
|
|
148
|
+
* @param scope Compile-time constants in reach.
|
|
149
|
+
*
|
|
150
|
+
* @returns The value.
|
|
151
|
+
*/
|
|
152
|
+
function evalDrawing(node, scope) {
|
|
153
|
+
const value = evalStatic(node, scope, 'boot');
|
|
154
|
+
if (!containsBoot(value))
|
|
155
|
+
return value;
|
|
156
|
+
const error = new SvgScreenError(`AOT: an <Svg> shape can't be calculated from \`screen\`: ${bootSourceText(node) ?? 'this value'}`);
|
|
157
|
+
error.aotHint =
|
|
158
|
+
'draw in a viewBox\'s units and size the <Svg> instead: <Svg viewBox="0 0 100 100" width={screen.width} …> ' +
|
|
159
|
+
'scales on every panel.';
|
|
160
|
+
if (node.loc)
|
|
161
|
+
error.aotLoc = node.loc.start;
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
119
164
|
/**
|
|
120
165
|
* Converts an SVG JSX element (Svg/Circle/Path/Rect/Line/Arc/G/…) to flattenSvg's `{type, props}` shape,
|
|
121
166
|
* statically evaluating every attribute; it serves the static path, so an unfoldable attribute throws.
|
|
@@ -131,28 +176,31 @@ function jsxToSvgElement(element, scope) {
|
|
|
131
176
|
// Fold every attribute; a bare attribute is `true`.
|
|
132
177
|
const type = element.openingElement.name.name;
|
|
133
178
|
const props = {};
|
|
134
|
-
for (const
|
|
135
|
-
if (
|
|
179
|
+
for (const attribute of element.openingElement.attributes) {
|
|
180
|
+
if (attribute.type !== 'JSXAttribute') {
|
|
136
181
|
throw new Error('AOT: spread attributes on an <Svg> element not supported');
|
|
137
182
|
}
|
|
138
|
-
const name =
|
|
183
|
+
const name = attribute.name.name;
|
|
139
184
|
if (name === 'ref' || name === 'key' || name === 'style')
|
|
140
185
|
continue; // the box's, not the drawing's
|
|
141
|
-
if (
|
|
186
|
+
if (attribute.value == null) {
|
|
142
187
|
props[name] = true;
|
|
143
188
|
}
|
|
144
|
-
else if (
|
|
145
|
-
props[name] =
|
|
189
|
+
else if (attribute.value.type === 'StringLiteral') {
|
|
190
|
+
props[name] = attribute.value.value;
|
|
146
191
|
}
|
|
147
|
-
else if (
|
|
148
|
-
props[name] =
|
|
192
|
+
else if (attribute.value.type === 'JSXExpressionContainer') {
|
|
193
|
+
props[name] =
|
|
194
|
+
type === 'Svg' && (name === 'width' || name === 'height')
|
|
195
|
+
? evalStatic(attribute.value.expression, scope, 'boot')
|
|
196
|
+
: evalDrawing(attribute.value.expression, scope);
|
|
149
197
|
}
|
|
150
198
|
else {
|
|
151
199
|
throw new Error(`AOT: unsupported <${type}> attribute value for "${name}"`);
|
|
152
200
|
}
|
|
153
201
|
}
|
|
154
202
|
// A baked gradient takes the same checks as a state-driven one.
|
|
155
|
-
for (const name of ['
|
|
203
|
+
for (const name of ['fillGradient', 'strokeGradient']) {
|
|
156
204
|
checkStaticGradient(name, props[name]);
|
|
157
205
|
}
|
|
158
206
|
// Convert the shape children the same way, with fragments inlined.
|
|
@@ -166,8 +214,8 @@ function jsxToSvgElement(element, scope) {
|
|
|
166
214
|
return { type, props };
|
|
167
215
|
}
|
|
168
216
|
/**
|
|
169
|
-
* A flattenSvg paint record [fill, stroke, w, miter, cap, join, rule,
|
|
170
|
-
*
|
|
217
|
+
* A flattenSvg paint record [fill, stroke, w, miter, cap, join, rule, fill_gradient, stroke_gradient] as a paint.
|
|
218
|
+
* fill_gradient/stroke_gradient (1-based gradient-table indices, 0 = solid) are absent on inline-<Svg> records (7-wide)
|
|
171
219
|
* and set on a baked <Svg source>.
|
|
172
220
|
*
|
|
173
221
|
* @param record The paint record.
|
|
@@ -175,18 +223,18 @@ function jsxToSvgElement(element, scope) {
|
|
|
175
223
|
* @returns The paint.
|
|
176
224
|
*/
|
|
177
225
|
export const vectorPaintOf = (record) => ({
|
|
178
|
-
fill: record[
|
|
179
|
-
stroke: record[
|
|
180
|
-
strokeWidth: record[
|
|
181
|
-
miter: record[
|
|
182
|
-
cap: record[
|
|
183
|
-
join: record[
|
|
184
|
-
fillRule: record[
|
|
185
|
-
fillGradient: (record[
|
|
186
|
-
strokeGradient: (record[
|
|
226
|
+
fill: record[PAINT_RECORD.fill] >>> 0,
|
|
227
|
+
stroke: record[PAINT_RECORD.stroke] >>> 0,
|
|
228
|
+
strokeWidth: record[PAINT_RECORD.strokeWidth],
|
|
229
|
+
miter: record[PAINT_RECORD.miter],
|
|
230
|
+
cap: record[PAINT_RECORD.cap] | 0,
|
|
231
|
+
join: record[PAINT_RECORD.join] | 0,
|
|
232
|
+
fillRule: record[PAINT_RECORD.fillRule] | 0,
|
|
233
|
+
fillGradient: (record[PAINT_RECORD.fillGradient] || 0) | 0,
|
|
234
|
+
strokeGradient: (record[PAINT_RECORD.strokeGradient] || 0) | 0,
|
|
187
235
|
});
|
|
188
236
|
/**
|
|
189
|
-
* Refuses a paint whose widths
|
|
237
|
+
* Refuses a paint whose widths aren't finite, where the paint is lowered rather than when its table is.
|
|
190
238
|
*
|
|
191
239
|
* @param paints The paints, in table order.
|
|
192
240
|
*/
|
|
@@ -197,7 +245,7 @@ export function checkVectorPaints(paints) {
|
|
|
197
245
|
}
|
|
198
246
|
}
|
|
199
247
|
/**
|
|
200
|
-
* Refuses baked tables holding a number
|
|
248
|
+
* Refuses baked tables holding a number that isn't finite, in the order the tables are written: the ops, the
|
|
201
249
|
* paints, then each gradient's kept stops, and geometry. An empty tape has no tables to check.
|
|
202
250
|
*
|
|
203
251
|
* @param tables The drawing.
|
|
@@ -205,8 +253,8 @@ export function checkVectorPaints(paints) {
|
|
|
205
253
|
function checkVectorTables(tables) {
|
|
206
254
|
if (!tables.ops.length)
|
|
207
255
|
return;
|
|
208
|
-
for (const
|
|
209
|
-
checkNumber(
|
|
256
|
+
for (const word of tables.ops) {
|
|
257
|
+
checkNumber(word);
|
|
210
258
|
}
|
|
211
259
|
checkVectorPaints(tables.paints);
|
|
212
260
|
for (const gradient of tables.gradients) {
|
|
@@ -246,7 +294,7 @@ export const svgNum = (value, fallback = 0) => {
|
|
|
246
294
|
return Number.isNaN(parsed) ? fallback : parsed;
|
|
247
295
|
};
|
|
248
296
|
/**
|
|
249
|
-
* True if an attribute value is a state-driven
|
|
297
|
+
* True if an attribute value is a state-driven expression (vs. a static number/string).
|
|
250
298
|
*
|
|
251
299
|
* @param value An attribute value from svgAttrs.
|
|
252
300
|
*
|
|
@@ -258,49 +306,55 @@ const isDynamicAttr = (value) => value != null && typeof value === 'object' && '
|
|
|
258
306
|
*
|
|
259
307
|
* @param openingElement The element's opening tag.
|
|
260
308
|
* @param scope Compile-time constants in reach.
|
|
261
|
-
* @param
|
|
309
|
+
* @param environment The expression environment.
|
|
262
310
|
*
|
|
263
311
|
* @returns The attributes, by name.
|
|
264
312
|
*/
|
|
265
|
-
function svgAttrs(openingElement, scope,
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
313
|
+
function svgAttrs(openingElement, scope, environment) {
|
|
314
|
+
const attributes = {};
|
|
315
|
+
const tagName = openingElement.name.name;
|
|
316
|
+
for (const attribute of openingElement.attributes) {
|
|
317
|
+
if (attribute.type !== 'JSXAttribute') {
|
|
269
318
|
throw new Error('AOT: spread attributes on an <Svg> element not supported');
|
|
270
319
|
}
|
|
271
320
|
// Not geometry or paint: the box's style and ref are lowerSvgBox's.
|
|
272
|
-
const name =
|
|
321
|
+
const name = attribute.name.name;
|
|
273
322
|
if (name === 'ref' || name === 'key' || name === 'style')
|
|
274
323
|
continue;
|
|
275
324
|
// Keep a gradient as its expression; gradAttr lowers it, conditions included.
|
|
276
|
-
const valueNode =
|
|
277
|
-
if (name === '
|
|
325
|
+
const valueNode = attribute.value;
|
|
326
|
+
if (name === 'fillGradient' || name === 'strokeGradient') {
|
|
278
327
|
if (valueNode == null || valueNode.type !== 'JSXExpressionContainer') {
|
|
279
328
|
throw new Error(`AOT: "${name}" must be an object expression`);
|
|
280
329
|
}
|
|
281
|
-
|
|
330
|
+
attributes[name] = { gradientNode: valueNode.expression };
|
|
282
331
|
continue;
|
|
283
332
|
}
|
|
284
333
|
// Fold everything else; an expression that does not fold is state-driven.
|
|
285
334
|
if (valueNode == null) {
|
|
286
|
-
|
|
335
|
+
attributes[name] = true;
|
|
287
336
|
}
|
|
288
337
|
else if (valueNode.type === 'StringLiteral') {
|
|
289
|
-
|
|
338
|
+
attributes[name] = valueNode.value;
|
|
290
339
|
}
|
|
291
340
|
else if (valueNode.type === 'JSXExpressionContainer') {
|
|
292
341
|
try {
|
|
293
|
-
|
|
342
|
+
attributes[name] =
|
|
343
|
+
tagName === 'Svg' && (name === 'width' || name === 'height')
|
|
344
|
+
? evalStatic(valueNode.expression, withUndefined(scope), 'boot')
|
|
345
|
+
: evalDrawing(valueNode.expression, withUndefined(scope));
|
|
294
346
|
}
|
|
295
|
-
catch {
|
|
347
|
+
catch (error) {
|
|
348
|
+
if (error instanceof SvgScreenError)
|
|
349
|
+
throw error;
|
|
296
350
|
// Defer a state-driven `d` so pathEntries raises the error naming the fix, not the expression's own error.
|
|
297
351
|
if (name === 'd') {
|
|
298
|
-
|
|
352
|
+
attributes[name] = { value: null, node: valueNode.expression };
|
|
299
353
|
continue;
|
|
300
354
|
}
|
|
301
355
|
// Keep the node too: fill/stroke lower to an ARGB color from it, not from the numeric value.
|
|
302
|
-
|
|
303
|
-
value: lowerExpr(valueNode.expression,
|
|
356
|
+
attributes[name] = {
|
|
357
|
+
value: lowerExpr(valueNode.expression, environment),
|
|
304
358
|
node: valueNode.expression,
|
|
305
359
|
};
|
|
306
360
|
}
|
|
@@ -309,7 +363,7 @@ function svgAttrs(openingElement, scope, env) {
|
|
|
309
363
|
throw new Error(`AOT: unsupported SVG attribute value for "${name}"`);
|
|
310
364
|
}
|
|
311
365
|
}
|
|
312
|
-
return
|
|
366
|
+
return attributes;
|
|
313
367
|
}
|
|
314
368
|
/**
|
|
315
369
|
* Lowers a `{ type, ax, ay, bx, by, r, stops: [{ color, offset }] }` gradient descriptor — the SAME shape
|
|
@@ -321,20 +375,20 @@ function svgAttrs(openingElement, scope, env) {
|
|
|
321
375
|
*
|
|
322
376
|
* @param node The descriptor's object literal.
|
|
323
377
|
* @param scope Compile-time constants in reach.
|
|
324
|
-
* @param
|
|
325
|
-
* @param
|
|
378
|
+
* @param environment The expression environment.
|
|
379
|
+
* @param attributeName The attribute, for errors.
|
|
326
380
|
*
|
|
327
381
|
* @returns The gradient.
|
|
328
382
|
*/
|
|
329
|
-
function lowerGradient(node, scope,
|
|
383
|
+
function lowerGradient(node, scope, environment, attributeName) {
|
|
330
384
|
if (!node || node.type !== 'ObjectExpression') {
|
|
331
|
-
throw new Error(`AOT: "${
|
|
385
|
+
throw new Error(`AOT: "${attributeName}" must be an object literal`);
|
|
332
386
|
}
|
|
333
387
|
// Index the descriptor's properties by key.
|
|
334
388
|
const props = {};
|
|
335
389
|
for (const property of node.properties) {
|
|
336
390
|
if (property.type !== 'ObjectProperty' || property.computed) {
|
|
337
|
-
throw new Error(`AOT: "${
|
|
391
|
+
throw new Error(`AOT: "${attributeName}" takes plain key: value pairs only`);
|
|
338
392
|
}
|
|
339
393
|
props[property.key.name ??
|
|
340
394
|
property.key.value] = property.value;
|
|
@@ -345,10 +399,12 @@ function lowerGradient(node, scope, env, attrName) {
|
|
|
345
399
|
return tapeNumber(fallback);
|
|
346
400
|
let value;
|
|
347
401
|
try {
|
|
348
|
-
value =
|
|
402
|
+
value = evalDrawing(valueNode, scope);
|
|
349
403
|
}
|
|
350
|
-
catch {
|
|
351
|
-
|
|
404
|
+
catch (error) {
|
|
405
|
+
if (error instanceof SvgScreenError)
|
|
406
|
+
throw error;
|
|
407
|
+
return { kind: 'float', value: lowerExpr(valueNode, environment) };
|
|
352
408
|
}
|
|
353
409
|
checkNumber(value);
|
|
354
410
|
return tapeNumber(Number(value));
|
|
@@ -359,24 +415,24 @@ function lowerGradient(node, scope, env, attrName) {
|
|
|
359
415
|
type = evalStatic(props.type, scope) | 0;
|
|
360
416
|
}
|
|
361
417
|
catch {
|
|
362
|
-
throw new Error(`AOT: "${
|
|
418
|
+
throw new Error(`AOT: "${attributeName}.type" must be a compile-time constant (1 linear, 2 radial, 3 conic)`);
|
|
363
419
|
}
|
|
364
|
-
if (type <
|
|
365
|
-
throw new Error(`AOT: "${
|
|
420
|
+
if (type < GRADIENT_LINEAR || type > GRADIENT_CONIC) {
|
|
421
|
+
throw new Error(`AOT: "${attributeName}.type" must be 1 (linear), 2 (radial) or 3 (conic)`);
|
|
366
422
|
}
|
|
367
423
|
const stopsNode = props.stops;
|
|
368
424
|
if (!stopsNode || stopsNode.type !== 'ArrayExpression') {
|
|
369
|
-
throw new Error(`AOT: "${
|
|
425
|
+
throw new Error(`AOT: "${attributeName}.stops" must be an array literal`);
|
|
370
426
|
}
|
|
371
427
|
check({
|
|
372
428
|
kind: 'gradientStops',
|
|
373
|
-
attribute:
|
|
429
|
+
attribute: attributeName,
|
|
374
430
|
count: stopsNode.elements.length,
|
|
375
431
|
});
|
|
376
432
|
// Lower each stop: a static color folds to its ARGB, a state-driven one to the color state chooses.
|
|
377
433
|
const stops = stopsNode.elements.map(stopNode => {
|
|
378
434
|
if (!stopNode || stopNode.type !== 'ObjectExpression') {
|
|
379
|
-
throw new Error(`AOT: each "${
|
|
435
|
+
throw new Error(`AOT: each "${attributeName}.stops" entry must be an object literal`);
|
|
380
436
|
}
|
|
381
437
|
const stopProps = {};
|
|
382
438
|
for (const property of stopNode.properties) {
|
|
@@ -387,11 +443,16 @@ function lowerGradient(node, scope, env, attrName) {
|
|
|
387
443
|
try {
|
|
388
444
|
color = {
|
|
389
445
|
kind: 'static',
|
|
390
|
-
argb: parseColor(
|
|
446
|
+
argb: parseColor(evalDrawing(stopProps.color, scope)) >>> 0,
|
|
391
447
|
};
|
|
392
448
|
}
|
|
393
|
-
catch {
|
|
394
|
-
|
|
449
|
+
catch (error) {
|
|
450
|
+
if (error instanceof SvgScreenError)
|
|
451
|
+
throw error;
|
|
452
|
+
color = {
|
|
453
|
+
kind: 'state',
|
|
454
|
+
value: lowerColorValue(stopProps.color, environment),
|
|
455
|
+
};
|
|
395
456
|
}
|
|
396
457
|
return { color, offset: floatField(stopProps.offset, 0) };
|
|
397
458
|
});
|
|
@@ -414,93 +475,99 @@ function lowerGradient(node, scope, env, attrName) {
|
|
|
414
475
|
* other state — the index is a compile-time constant, so "no gradient here" is not expressible by
|
|
415
476
|
* omission: a thermostat dial's Auto ramp would paint over its Cool and Heat modes as well.
|
|
416
477
|
*
|
|
417
|
-
* @param
|
|
478
|
+
* @param gradientAttribute The attribute, if the shape has it.
|
|
418
479
|
* @param scope Compile-time constants in reach.
|
|
419
|
-
* @param
|
|
420
|
-
* @param
|
|
480
|
+
* @param environment The expression environment.
|
|
481
|
+
* @param attributeName The attribute, for errors.
|
|
421
482
|
*
|
|
422
483
|
* @returns The gradient and its condition, or null when the shape has none.
|
|
423
484
|
*/
|
|
424
|
-
function lowerGradientAttr(
|
|
425
|
-
if (!
|
|
485
|
+
function lowerGradientAttr(gradientAttribute, scope, environment, attributeName) {
|
|
486
|
+
if (!gradientAttribute)
|
|
426
487
|
return null;
|
|
427
488
|
// Peel a conditional down to its object literal, keeping the condition.
|
|
428
|
-
let node =
|
|
489
|
+
let node = gradientAttribute.gradientNode;
|
|
429
490
|
let when = null;
|
|
430
491
|
const nullish = (branch) => branch.type === 'NullLiteral' ||
|
|
431
492
|
(branch.type === 'Identifier' && branch.name === 'undefined');
|
|
432
493
|
if (node.type === 'ConditionalExpression') {
|
|
433
494
|
if (node.consequent.type === 'ObjectExpression' &&
|
|
434
495
|
nullish(node.alternate)) {
|
|
435
|
-
when = { test: lowerExpr(node.test,
|
|
496
|
+
when = { test: lowerExpr(node.test, environment), negate: false };
|
|
436
497
|
node = node.consequent;
|
|
437
498
|
}
|
|
438
499
|
else if (node.alternate.type === 'ObjectExpression' &&
|
|
439
500
|
nullish(node.consequent)) {
|
|
440
|
-
when = { test: lowerExpr(node.test,
|
|
501
|
+
when = { test: lowerExpr(node.test, environment), negate: true };
|
|
441
502
|
node = node.alternate;
|
|
442
503
|
}
|
|
443
504
|
else {
|
|
444
|
-
throw new Error(`AOT: a conditional "${
|
|
505
|
+
throw new Error(`AOT: a conditional "${attributeName}" must be \`cond ? { … } : null\` (one branch an ` +
|
|
445
506
|
`object literal, the other null)`);
|
|
446
507
|
}
|
|
447
508
|
}
|
|
448
509
|
else if (node.type === 'LogicalExpression' &&
|
|
449
510
|
node.operator === '&&' &&
|
|
450
511
|
node.right.type === 'ObjectExpression') {
|
|
451
|
-
when = { test: lowerExpr(node.left,
|
|
512
|
+
when = { test: lowerExpr(node.left, environment), negate: false };
|
|
452
513
|
node = node.right;
|
|
453
514
|
}
|
|
454
|
-
return {
|
|
515
|
+
return {
|
|
516
|
+
gradient: lowerGradient(node, scope, environment, attributeName),
|
|
517
|
+
when,
|
|
518
|
+
};
|
|
455
519
|
}
|
|
456
520
|
/**
|
|
457
521
|
* Lowers a shape's paint and the gradients it uses:
|
|
458
522
|
* - fill / stroke may be state-driven (a color string, a ternary of them, or a folded theme token);
|
|
459
523
|
* - strokeWidth may be state-driven;
|
|
460
524
|
* - cap / join / miterlimit / fillRule must be static (a state-driven one throws a clear error);
|
|
461
|
-
* -
|
|
525
|
+
* - fillGradient / strokeGradient are lowered here; the caller gives them places in the drawing's table.
|
|
462
526
|
*
|
|
463
|
-
* @param
|
|
464
|
-
* @param
|
|
527
|
+
* @param attributes The shape's attributes.
|
|
528
|
+
* @param environment The expression environment.
|
|
465
529
|
* @param scope Compile-time constants in reach.
|
|
466
530
|
*
|
|
467
531
|
* @returns The paint, and the shape's gradients.
|
|
468
532
|
*/
|
|
469
|
-
function lowerShapePaint(
|
|
533
|
+
function lowerShapePaint(attributes, environment, scope) {
|
|
470
534
|
// Colors and the stroke width are the only paint attributes that may be state-driven.
|
|
471
|
-
const color = (
|
|
472
|
-
? {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
:
|
|
535
|
+
const color = (attributeValue, fallback) => isDynamicAttr(attributeValue)
|
|
536
|
+
? {
|
|
537
|
+
kind: 'state',
|
|
538
|
+
value: lowerColorValue(attributeValue.node, environment),
|
|
539
|
+
}
|
|
540
|
+
: { kind: 'static', argb: parseColor(attributeValue ?? fallback) >>> 0 };
|
|
541
|
+
const strokeWidth = isDynamicAttr(attributes.strokeWidth)
|
|
542
|
+
? { kind: 'float', value: attributes.strokeWidth.value }
|
|
543
|
+
: checkedTapeNumber(svgNum(attributes.strokeWidth, 1));
|
|
477
544
|
// The rest of the paint must be static.
|
|
478
|
-
for (const
|
|
545
|
+
for (const attributeName of [
|
|
479
546
|
'strokeLinecap',
|
|
480
547
|
'strokeLinejoin',
|
|
481
548
|
'strokeMiterlimit',
|
|
482
549
|
'fillRule',
|
|
483
550
|
]) {
|
|
484
|
-
if (isDynamicAttr(
|
|
485
|
-
throw new Error(`AOT: a state-driven <Svg> "${
|
|
551
|
+
if (isDynamicAttr(attributes[attributeName])) {
|
|
552
|
+
throw new Error(`AOT: a state-driven <Svg> "${attributeName}" is not supported (only fill / stroke / ` +
|
|
486
553
|
`strokeWidth can be state-driven)`);
|
|
487
554
|
}
|
|
488
555
|
}
|
|
489
|
-
const fill = color(
|
|
490
|
-
const stroke = color(
|
|
491
|
-
const miter = svgNum(
|
|
556
|
+
const fill = color(attributes.fill, 'black');
|
|
557
|
+
const stroke = color(attributes.stroke, 'none');
|
|
558
|
+
const miter = svgNum(attributes.strokeMiterlimit, DEFAULT_MITER_LIMIT);
|
|
492
559
|
checkNumber(miter);
|
|
493
560
|
const paint = {
|
|
494
561
|
fill,
|
|
495
562
|
stroke,
|
|
496
563
|
strokeWidth,
|
|
497
564
|
miter,
|
|
498
|
-
cap: CAP_MAP[
|
|
499
|
-
join: JOIN_MAP[
|
|
500
|
-
fillRule:
|
|
565
|
+
cap: CAP_MAP[attributes.strokeLinecap] ?? 0,
|
|
566
|
+
join: JOIN_MAP[attributes.strokeLinejoin] ?? 0,
|
|
567
|
+
fillRule: attributes.fillRule === 'evenodd' ? 1 : 0,
|
|
501
568
|
};
|
|
502
|
-
const fillGradient = lowerGradientAttr(
|
|
503
|
-
const strokeGradient = lowerGradientAttr(
|
|
569
|
+
const fillGradient = lowerGradientAttr(attributes.fillGradient, scope, environment, 'fillGradient');
|
|
570
|
+
const strokeGradient = lowerGradientAttr(attributes.strokeGradient, scope, environment, 'strokeGradient');
|
|
504
571
|
return { paint, fillGradient, strokeGradient };
|
|
505
572
|
}
|
|
506
573
|
// Per-shape op-tape geometry, shared by the JSX path and the imperative updateVector path, as in svg-ops.
|
|
@@ -509,7 +576,7 @@ const tapeNumber = (value) => ({ kind: 'number', value });
|
|
|
509
576
|
/** The zero slot. */
|
|
510
577
|
const TAPE_ZERO = tapeNumber(0);
|
|
511
578
|
/**
|
|
512
|
-
* A number from the app as a tape slot, checked now
|
|
579
|
+
* A number from the app as a tape slot, checked now, where the shape is lowered.
|
|
513
580
|
*
|
|
514
581
|
* @param value The number.
|
|
515
582
|
*
|
|
@@ -522,11 +589,11 @@ export const checkedTapeNumber = (value) => {
|
|
|
522
589
|
/**
|
|
523
590
|
* An opcode as a tape slot.
|
|
524
591
|
*
|
|
525
|
-
* @param
|
|
592
|
+
* @param operation The ER_VECTOR_OP_* name.
|
|
526
593
|
*
|
|
527
594
|
* @returns The slot.
|
|
528
595
|
*/
|
|
529
|
-
const tapeOp = (
|
|
596
|
+
const tapeOp = (operation) => ({ kind: 'op', operation });
|
|
530
597
|
/**
|
|
531
598
|
* `first`, then each operator and operand in turn.
|
|
532
599
|
*
|
|
@@ -538,17 +605,22 @@ const tapeOp = (op) => ({ kind: 'op', op });
|
|
|
538
605
|
const tapeChain = (first, ...rest) => ({
|
|
539
606
|
kind: 'chain',
|
|
540
607
|
first,
|
|
541
|
-
rest: rest.map(([
|
|
608
|
+
rest: rest.map(([operator, value]) => ({ operator, value })),
|
|
542
609
|
});
|
|
543
610
|
/**
|
|
544
|
-
* The value of a slot that
|
|
545
|
-
* text, the way the geometry folded before it was IR, so the generated C stays the same.
|
|
611
|
+
* The value of a slot that holds a known number, or null when state drives it.
|
|
546
612
|
*
|
|
547
613
|
* @param value The slot, if there is one.
|
|
548
614
|
*
|
|
549
615
|
* @returns Its value, or null.
|
|
550
616
|
*/
|
|
551
|
-
|
|
617
|
+
function tapeLiteral(value) {
|
|
618
|
+
if (value?.kind === 'number')
|
|
619
|
+
return value.value;
|
|
620
|
+
return value?.kind === 'float' && value.value.kind === 'number'
|
|
621
|
+
? value.value.value
|
|
622
|
+
: null;
|
|
623
|
+
}
|
|
552
624
|
/**
|
|
553
625
|
* An arc's tape slots. The engine measures angles in radians clockwise from 3 o'clock, so each angle is
|
|
554
626
|
* converted from degrees clockwise from the top.
|
|
@@ -562,7 +634,7 @@ const tapeLiteral = (value) => value == null ? null : cLiteralValue(printTapeVal
|
|
|
562
634
|
* @returns The slots.
|
|
563
635
|
*/
|
|
564
636
|
export const arcTape = (cx, cy, radius, startDeg, endDeg) => [
|
|
565
|
-
tapeOp('
|
|
637
|
+
tapeOp('ER_VECTOR_OP_ARC'),
|
|
566
638
|
cx,
|
|
567
639
|
cy,
|
|
568
640
|
radius,
|
|
@@ -580,35 +652,18 @@ export const arcTape = (cx, cy, radius, startDeg, endDeg) => [
|
|
|
580
652
|
* @returns The slots.
|
|
581
653
|
*/
|
|
582
654
|
export const circleTape = (cx, cy, radius) => [
|
|
583
|
-
tapeOp('
|
|
655
|
+
tapeOp('ER_VECTOR_OP_MOVE'),
|
|
584
656
|
tapeChain(cx, ['+', radius]),
|
|
585
657
|
cy,
|
|
586
|
-
tapeOp('
|
|
658
|
+
tapeOp('ER_VECTOR_OP_ARC'),
|
|
587
659
|
cx,
|
|
588
660
|
cy,
|
|
589
661
|
radius,
|
|
590
662
|
TAPE_ZERO,
|
|
591
663
|
{ kind: 'fullTurn' },
|
|
592
664
|
TAPE_ZERO,
|
|
593
|
-
tapeOp('
|
|
665
|
+
tapeOp('ER_VECTOR_OP_CLOSE'),
|
|
594
666
|
];
|
|
595
|
-
/**
|
|
596
|
-
* The numeric value of a C float literal (as produced by floatLit, or the `(float)(N)` cast the imperative
|
|
597
|
-
* updateVector path emits), or null for a state-driven expr.
|
|
598
|
-
*
|
|
599
|
-
* @param floatExpr C for a float.
|
|
600
|
-
*
|
|
601
|
-
* @returns Its value, or null when it is not a literal.
|
|
602
|
-
*/
|
|
603
|
-
const cLiteralValue = (floatExpr) => {
|
|
604
|
-
const trimmed = String(floatExpr).trim();
|
|
605
|
-
const cast = new RegExp(`^\\(float\\)\\((${C_NUMBER})\\)$`).exec(trimmed);
|
|
606
|
-
if (cast)
|
|
607
|
-
return parseFloat(cast[1]);
|
|
608
|
-
return new RegExp(`^${C_NUMBER}f$`).test(trimmed)
|
|
609
|
-
? parseFloat(trimmed)
|
|
610
|
-
: null;
|
|
611
|
-
};
|
|
612
667
|
/**
|
|
613
668
|
* A corner radius clamped to half its side, mirroring svg-ops rectRadii. Folded to a number when both the
|
|
614
669
|
* radius and the side are known — the usual case, since only x/y/width tend to be state-driven.
|
|
@@ -639,24 +694,24 @@ const clampRadius = (radius, side) => {
|
|
|
639
694
|
* @returns The slots.
|
|
640
695
|
*/
|
|
641
696
|
const sharpRectTape = (x, y, width, height) => [
|
|
642
|
-
tapeOp('
|
|
697
|
+
tapeOp('ER_VECTOR_OP_MOVE'),
|
|
643
698
|
x,
|
|
644
699
|
y,
|
|
645
|
-
tapeOp('
|
|
700
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
646
701
|
tapeChain(x, ['+', width]),
|
|
647
702
|
y,
|
|
648
|
-
tapeOp('
|
|
703
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
649
704
|
tapeChain(x, ['+', width]),
|
|
650
705
|
tapeChain(y, ['+', height]),
|
|
651
|
-
tapeOp('
|
|
706
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
652
707
|
x,
|
|
653
708
|
tapeChain(y, ['+', height]),
|
|
654
|
-
tapeOp('
|
|
709
|
+
tapeOp('ER_VECTOR_OP_CLOSE'),
|
|
655
710
|
];
|
|
656
711
|
/**
|
|
657
712
|
* A rectangle's tape slots. rx/ry are null when the rect has no corner radius at all. Corners are cubics, not
|
|
658
|
-
*
|
|
659
|
-
* endpoint. `tag` names the locals and must be unique within the caller's
|
|
713
|
+
* ER_VECTOR_OP_ARC, for the same reason svg-ops uses them: a corner must start at EXACTLY the preceding line's
|
|
714
|
+
* endpoint. `tag` names the locals and must be unique within the caller's block.
|
|
660
715
|
*
|
|
661
716
|
* @param x The left edge.
|
|
662
717
|
* @param y The top edge.
|
|
@@ -710,50 +765,50 @@ export const rectTape = (x, y, width, height, rx, ry, tag) => {
|
|
|
710
765
|
const right = tapeChain(x, ['+', width]);
|
|
711
766
|
const bottom = tapeChain(y, ['+', height]);
|
|
712
767
|
const entries = [
|
|
713
|
-
tapeOp('
|
|
768
|
+
tapeOp('ER_VECTOR_OP_MOVE'),
|
|
714
769
|
innerLeft,
|
|
715
770
|
y,
|
|
716
|
-
tapeOp('
|
|
771
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
717
772
|
innerRight,
|
|
718
773
|
y,
|
|
719
|
-
tapeOp('
|
|
774
|
+
tapeOp('ER_VECTOR_OP_CUBIC'),
|
|
720
775
|
tapeChain(innerRight, ['+', handleX]),
|
|
721
776
|
y,
|
|
722
777
|
right,
|
|
723
778
|
tapeChain(innerTop, ['-', handleY]),
|
|
724
779
|
right,
|
|
725
780
|
innerTop,
|
|
726
|
-
tapeOp('
|
|
781
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
727
782
|
right,
|
|
728
783
|
innerBottom,
|
|
729
|
-
tapeOp('
|
|
784
|
+
tapeOp('ER_VECTOR_OP_CUBIC'),
|
|
730
785
|
right,
|
|
731
786
|
tapeChain(innerBottom, ['+', handleY]),
|
|
732
787
|
tapeChain(innerRight, ['+', handleX]),
|
|
733
788
|
bottom,
|
|
734
789
|
innerRight,
|
|
735
790
|
bottom,
|
|
736
|
-
tapeOp('
|
|
791
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
737
792
|
innerLeft,
|
|
738
793
|
bottom,
|
|
739
|
-
tapeOp('
|
|
794
|
+
tapeOp('ER_VECTOR_OP_CUBIC'),
|
|
740
795
|
tapeChain(innerLeft, ['-', handleX]),
|
|
741
796
|
bottom,
|
|
742
797
|
x,
|
|
743
798
|
tapeChain(innerBottom, ['+', handleY]),
|
|
744
799
|
x,
|
|
745
800
|
innerBottom,
|
|
746
|
-
tapeOp('
|
|
801
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
747
802
|
x,
|
|
748
803
|
innerTop,
|
|
749
|
-
tapeOp('
|
|
804
|
+
tapeOp('ER_VECTOR_OP_CUBIC'),
|
|
750
805
|
x,
|
|
751
806
|
tapeChain(innerTop, ['-', handleY]),
|
|
752
807
|
tapeChain(innerLeft, ['-', handleX]),
|
|
753
808
|
y,
|
|
754
809
|
innerLeft,
|
|
755
810
|
y,
|
|
756
|
-
tapeOp('
|
|
811
|
+
tapeOp('ER_VECTOR_OP_CLOSE'),
|
|
757
812
|
];
|
|
758
813
|
return { entries, locals };
|
|
759
814
|
};
|
|
@@ -768,47 +823,47 @@ export const rectTape = (x, y, width, height, rx, ry, tag) => {
|
|
|
768
823
|
* @returns The slots.
|
|
769
824
|
*/
|
|
770
825
|
export const lineTape = (x1, y1, x2, y2) => [
|
|
771
|
-
tapeOp('
|
|
826
|
+
tapeOp('ER_VECTOR_OP_MOVE'),
|
|
772
827
|
x1,
|
|
773
828
|
y1,
|
|
774
|
-
tapeOp('
|
|
829
|
+
tapeOp('ER_VECTOR_OP_LINE'),
|
|
775
830
|
x2,
|
|
776
831
|
y2,
|
|
777
832
|
];
|
|
778
833
|
/**
|
|
779
834
|
* An SVG coordinate attribute as a tape slot: a number when static, the state-driven value read as a float.
|
|
780
835
|
*
|
|
781
|
-
* @param
|
|
836
|
+
* @param attributeValue The attribute's value.
|
|
782
837
|
* @param fallback The value to use when it is absent or not a number.
|
|
783
838
|
*
|
|
784
839
|
* @returns The slot.
|
|
785
840
|
*/
|
|
786
|
-
const attrTape = (
|
|
787
|
-
? { kind: 'float', value:
|
|
788
|
-
: checkedTapeNumber(svgNum(
|
|
841
|
+
const attrTape = (attributeValue, fallback = 0) => isDynamicAttr(attributeValue)
|
|
842
|
+
? { kind: 'float', value: attributeValue.value }
|
|
843
|
+
: checkedTapeNumber(svgNum(attributeValue, fallback));
|
|
789
844
|
// The JSX path: each shape's attributes resolve to tape slots via attrTape.
|
|
790
|
-
const arcShape = (
|
|
791
|
-
entries: arcTape(attrTape(
|
|
845
|
+
const arcShape = (attributes) => ({
|
|
846
|
+
entries: arcTape(attrTape(attributes.cx), attrTape(attributes.cy), attrTape(attributes.r), attrTape(attributes.startAngle), attrTape(attributes.endAngle)),
|
|
792
847
|
locals: [],
|
|
793
848
|
});
|
|
794
|
-
const circleShape = (
|
|
795
|
-
entries: circleTape(attrTape(
|
|
849
|
+
const circleShape = (attributes) => ({
|
|
850
|
+
entries: circleTape(attrTape(attributes.cx), attrTape(attributes.cy), attrTape(attributes.r)),
|
|
796
851
|
locals: [],
|
|
797
852
|
});
|
|
798
|
-
const rectShape = (
|
|
799
|
-
const lineShape = (
|
|
800
|
-
entries: lineTape(attrTape(
|
|
853
|
+
const rectShape = (attributes, tag) => rectTape(attrTape(attributes.x), attrTape(attributes.y), attrTape(attributes.width), attrTape(attributes.height), attributes.rx == null ? null : attrTape(attributes.rx), attributes.ry == null ? null : attrTape(attributes.ry), tag);
|
|
854
|
+
const lineShape = (attributes) => ({
|
|
855
|
+
entries: lineTape(attrTape(attributes.x1), attrTape(attributes.y1), attrTape(attributes.x2), attrTape(attributes.y2)),
|
|
801
856
|
locals: [],
|
|
802
857
|
});
|
|
803
|
-
const pathShape = (
|
|
804
|
-
if (
|
|
858
|
+
const pathShape = (attributes) => {
|
|
859
|
+
if (attributes.d == null)
|
|
805
860
|
return { entries: [], locals: [] };
|
|
806
|
-
if (isDynamicAttr(
|
|
861
|
+
if (isDynamicAttr(attributes.d)) {
|
|
807
862
|
throw new Error('AOT: a state-driven <Path d=…> is not yet supported (use Arc/Circle/Rect/Line for dynamic shapes)');
|
|
808
863
|
}
|
|
809
864
|
// Opcodes are encoded as float values 0..6, like coords.
|
|
810
865
|
return {
|
|
811
|
-
entries: parsePath(String(
|
|
866
|
+
entries: parsePath(String(attributes.d)).map((value) => checkedTapeNumber(value)),
|
|
812
867
|
locals: [],
|
|
813
868
|
};
|
|
814
869
|
};
|
|
@@ -840,14 +895,14 @@ function svgHasDynamic(element, scope) {
|
|
|
840
895
|
}
|
|
841
896
|
if (node.type !== 'JSXElement')
|
|
842
897
|
return;
|
|
843
|
-
for (const
|
|
844
|
-
if (
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
898
|
+
for (const attribute of node.openingElement.attributes) {
|
|
899
|
+
if (attribute.type === 'JSXAttribute' &&
|
|
900
|
+
attribute.name.name !== 'ref' &&
|
|
901
|
+
attribute.name.name !== 'key' &&
|
|
902
|
+
attribute.name.name !== 'style' &&
|
|
903
|
+
attribute.value?.type === 'JSXExpressionContainer') {
|
|
849
904
|
try {
|
|
850
|
-
evalStatic(
|
|
905
|
+
evalStatic(attribute.value.expression, scope, 'boot');
|
|
851
906
|
}
|
|
852
907
|
catch {
|
|
853
908
|
hasDynamic = true;
|
|
@@ -892,8 +947,10 @@ function svgPixels(field, value, scaled) {
|
|
|
892
947
|
* @returns The writes.
|
|
893
948
|
*/
|
|
894
949
|
function svgBoxSize(field, value, scaled) {
|
|
895
|
-
if (value == null)
|
|
950
|
+
if (value == null || svgBootSize(field, value))
|
|
896
951
|
return [];
|
|
952
|
+
if (isBoot(value))
|
|
953
|
+
return svgBoxSize(field, foldBoot(value), scaled);
|
|
897
954
|
if (isDynamicAttr(value)) {
|
|
898
955
|
throw aotError(`AOT: a state-driven <Svg ${field}> is not supported`, `give the <Svg> a constant ${field} and drive the size of a wrapping <View> from state instead.`);
|
|
899
956
|
}
|
|
@@ -903,13 +960,36 @@ function svgBoxSize(field, value, scaled) {
|
|
|
903
960
|
return [
|
|
904
961
|
{
|
|
905
962
|
field,
|
|
906
|
-
|
|
963
|
+
value: Math.round(svgPixels(field, value, scaled)),
|
|
907
964
|
},
|
|
908
965
|
];
|
|
909
966
|
}
|
|
967
|
+
/**
|
|
968
|
+
* An <Svg> width or height worked out at boot, as its runtime write. A drawing with a viewBox then scales on every
|
|
969
|
+
* panel with no layout of its own. A value boot can't work out is taken at the build's panel.
|
|
970
|
+
*
|
|
971
|
+
* @param field Which one.
|
|
972
|
+
* @param value Its value.
|
|
973
|
+
*
|
|
974
|
+
* @returns The write, or null when it is not one.
|
|
975
|
+
*/
|
|
976
|
+
function svgBootSize(field, value) {
|
|
977
|
+
return isBoot(value) ? bootStyleAssign(field, value) : null;
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* An <Svg>'s width and height, as the writes of those worked out at boot.
|
|
981
|
+
*
|
|
982
|
+
* @param width Its width.
|
|
983
|
+
* @param height Its height.
|
|
984
|
+
*
|
|
985
|
+
* @returns The writes.
|
|
986
|
+
*/
|
|
987
|
+
function svgBootSizes(width, height) {
|
|
988
|
+
return [svgBootSize('width', width), svgBootSize('height', height)].filter((size) => size !== null);
|
|
989
|
+
}
|
|
910
990
|
/**
|
|
911
991
|
* Lowers the box an <Svg> draws in: a node of `type` with its size, then its static style, its animated
|
|
912
|
-
* transform and its refs. No state drives the box: it is not a node
|
|
992
|
+
* transform and its refs. No state drives the box: it is not a node the update rewrites.
|
|
913
993
|
*
|
|
914
994
|
* @param nodeId The node's variable.
|
|
915
995
|
* @param type The engine node type: a vector node, or an image for a rasterized .svg.
|
|
@@ -917,29 +997,37 @@ function svgBoxSize(field, value, scaled) {
|
|
|
917
997
|
* @param height Its height writes.
|
|
918
998
|
* @param openingElement The <Svg>'s opening tag, for its style and ref.
|
|
919
999
|
* @param scope Compile-time constants in reach.
|
|
920
|
-
* @param
|
|
1000
|
+
* @param environment The expression environment.
|
|
1001
|
+
* @param bootSizes Its width and height writes worked out at boot.
|
|
921
1002
|
*
|
|
922
1003
|
* @returns The node, with its refs wired.
|
|
923
1004
|
*/
|
|
924
|
-
function lowerSvgBox(nodeId, type, width, height, openingElement, scope,
|
|
925
|
-
const { staticAssigns,
|
|
926
|
-
|
|
1005
|
+
function lowerSvgBox(nodeId, type, width, height, openingElement, scope, environment, bootSizes = []) {
|
|
1006
|
+
const { staticAssigns, dynamicAssigns, binds } = collectStyleAssigns(openingElement, scope, environment, elementSpec('Svg'));
|
|
1007
|
+
// A value worked out at boot is set once, so only one state drives is refused.
|
|
1008
|
+
const readsOnlyBoot = (assign) => (assign.value.kind === 'dimension' || assign.value.kind === 'opacity') &&
|
|
1009
|
+
assign.value.value.kind === 'boot';
|
|
1010
|
+
if (!dynamicAssigns.every(readsOnlyBoot)) {
|
|
927
1011
|
throw aotError('AOT: a state-driven style on an <Svg> is not supported', 'put the changing style on a wrapping <View>: <View style={…}><Svg …/></View>.');
|
|
928
1012
|
}
|
|
929
1013
|
// A style width or height wins over the attribute, in pixels or as a percentage of the parent.
|
|
930
|
-
const size = [...width, ...height].filter(assign => !styleWrites(staticAssigns, [], assign.field.replace(/
|
|
1014
|
+
const size = [...width, ...height].filter(assign => !styleWrites(staticAssigns, [], assign.field.replace(/_percent$/, '')));
|
|
931
1015
|
const node = {
|
|
932
1016
|
id: nodeId,
|
|
1017
|
+
location: currentLoc(),
|
|
933
1018
|
type,
|
|
934
1019
|
constantProps: [...size, ...staticAssigns],
|
|
935
|
-
stateProps: [
|
|
1020
|
+
stateProps: [
|
|
1021
|
+
...bootSizes.filter(assign => !styleWrites(staticAssigns, dynamicAssigns, assign.field.replace(/_percent$/, ''))),
|
|
1022
|
+
...dynamicAssigns,
|
|
1023
|
+
],
|
|
936
1024
|
placeholder: null,
|
|
937
1025
|
text: null,
|
|
938
1026
|
image: null,
|
|
939
1027
|
spans: null,
|
|
940
1028
|
wiring: [
|
|
941
1029
|
...binds.map((bind, index) => ({ kind: 'bind', bind, index })),
|
|
942
|
-
...lowerRefBinds(openingElement,
|
|
1030
|
+
...lowerRefBinds(openingElement, environment).map(slot => ({
|
|
943
1031
|
kind: 'ref',
|
|
944
1032
|
slot,
|
|
945
1033
|
})),
|
|
@@ -951,172 +1039,311 @@ function lowerSvgBox(nodeId, type, width, height, openingElement, scope, env) {
|
|
|
951
1039
|
}
|
|
952
1040
|
/**
|
|
953
1041
|
* <Svg> → ER_NODE_VECTOR. Static subtree → is a baked const op-tape, converted by flattenSvg (the same
|
|
954
|
-
* converter Flow A uses); any state-driven attr → a symbolic op-tape rebuilt
|
|
955
|
-
* build time and on every
|
|
1042
|
+
* converter Flow A uses); any state-driven attr → a symbolic op-tape rebuilt at
|
|
1043
|
+
* build time and on every update. <Svg source> draws a .svg baked ahead of time (bakeSvgArtifacts).
|
|
956
1044
|
*
|
|
957
1045
|
* @param element The <Svg>.
|
|
958
1046
|
* @param scope Compile-time constants in reach.
|
|
959
|
-
* @param
|
|
960
|
-
* @param
|
|
1047
|
+
* @param output Everything emitted so far.
|
|
1048
|
+
* @param environment The expression environment.
|
|
961
1049
|
* @param state The component's state table.
|
|
962
|
-
* @param
|
|
1050
|
+
* @param options How the element is placed.
|
|
963
1051
|
*
|
|
964
1052
|
* @returns The node.
|
|
965
1053
|
*/
|
|
966
|
-
export function emitSvg(element, scope,
|
|
967
|
-
if (
|
|
1054
|
+
export function emitSvg(element, scope, output, environment, state, options) {
|
|
1055
|
+
if (options.show) {
|
|
968
1056
|
throw new Error('AOT: an <Svg> inside a dynamic conditional is not yet supported');
|
|
969
1057
|
}
|
|
970
1058
|
// Pick the path: a baked .svg file, a state-driven tape, or a static one.
|
|
971
|
-
const
|
|
1059
|
+
const sourceAttribute = element.openingElement.attributes.find((candidate) => candidate.type === 'JSXAttribute' &&
|
|
972
1060
|
candidate.name &&
|
|
973
1061
|
candidate.name.name === 'source');
|
|
974
|
-
if (
|
|
975
|
-
return emitSvgSource(element,
|
|
1062
|
+
if (sourceAttribute)
|
|
1063
|
+
return emitSvgSource(element, sourceAttribute, scope, output, environment);
|
|
976
1064
|
return svgHasDynamic(element, scope)
|
|
977
|
-
? emitSvgDynamic(element, scope,
|
|
978
|
-
: emitSvgStatic(element, scope,
|
|
1065
|
+
? emitSvgDynamic(element, scope, output, environment, state)
|
|
1066
|
+
: emitSvgStatic(element, scope, output, environment);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* How much a drawing scales along one side: the box over the viewBox.
|
|
1070
|
+
*
|
|
1071
|
+
* @param size The box's side: a number, or a boot value.
|
|
1072
|
+
* @param view The viewBox's side.
|
|
1073
|
+
*
|
|
1074
|
+
* @returns The scale, or null when the side has no value boot can work out.
|
|
1075
|
+
*/
|
|
1076
|
+
function viewBoxScale(size, view) {
|
|
1077
|
+
if (!isBoot(size))
|
|
1078
|
+
return tapeNumber(svgNum(size, view) / view);
|
|
1079
|
+
const slot = bootSlotOf(size);
|
|
1080
|
+
if (!slot || (slot.type.kind !== 'int' && slot.type.kind !== 'float')) {
|
|
1081
|
+
return null;
|
|
1082
|
+
}
|
|
1083
|
+
return {
|
|
1084
|
+
kind: 'float',
|
|
1085
|
+
value: {
|
|
1086
|
+
kind: 'floatDivide',
|
|
1087
|
+
left: { kind: 'boot', slot, type: slot.type },
|
|
1088
|
+
right: { kind: 'number', value: view, type: FLOAT },
|
|
1089
|
+
type: FLOAT,
|
|
1090
|
+
},
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* A viewBox drawing sized at boot: drawn in the viewBox's units and scaled once, from the real size, as a drawing
|
|
1095
|
+
* state drives is rebuilt. So it takes one layout on every panel, where baking it would take one layout per size.
|
|
1096
|
+
*
|
|
1097
|
+
* @param element The <Svg>.
|
|
1098
|
+
* @param svgElement The <Svg>, folded.
|
|
1099
|
+
* @param scope Compile-time constants in reach.
|
|
1100
|
+
* @param output Everything emitted so far.
|
|
1101
|
+
* @param environment The expression environment.
|
|
1102
|
+
*
|
|
1103
|
+
* @returns The node, or null when no viewBox drawing is sized at boot.
|
|
1104
|
+
*/
|
|
1105
|
+
function emitSvgScaled(element, svgElement, scope, output, environment) {
|
|
1106
|
+
const { width, height, viewBox } = svgElement.props;
|
|
1107
|
+
if (!viewBox || !width || !height || (!isBoot(width) && !isBoot(height))) {
|
|
1108
|
+
return null;
|
|
1109
|
+
}
|
|
1110
|
+
const view = String(viewBox)
|
|
1111
|
+
.trim()
|
|
1112
|
+
.split(/[\s,]+/)
|
|
1113
|
+
.map(parseFloat);
|
|
1114
|
+
if (view.length !== 4 || !(view[2] > 0) || !(view[3] > 0))
|
|
1115
|
+
return null;
|
|
1116
|
+
const scaleX = viewBoxScale(width, view[2]);
|
|
1117
|
+
const scaleY = viewBoxScale(height, view[3]);
|
|
1118
|
+
if (!scaleX || !scaleY)
|
|
1119
|
+
return null;
|
|
1120
|
+
const { ops, paints, gradients } = flattenSvg({
|
|
1121
|
+
...svgElement.props,
|
|
1122
|
+
width: view[2],
|
|
1123
|
+
height: view[3],
|
|
1124
|
+
});
|
|
1125
|
+
const sideX = { kind: 'local', name: 'scale_x' };
|
|
1126
|
+
const sideY = { kind: 'local', name: 'scale_y' };
|
|
1127
|
+
const along = (value, side) => tapeChain(tapeNumber(value), ['*', side]);
|
|
1128
|
+
const operationNames = {
|
|
1129
|
+
[VOP_MOVE]: 'ER_VECTOR_OP_MOVE',
|
|
1130
|
+
[VOP_LINE]: 'ER_VECTOR_OP_LINE',
|
|
1131
|
+
[VOP_QUAD]: 'ER_VECTOR_OP_QUAD',
|
|
1132
|
+
[VOP_CUBIC]: 'ER_VECTOR_OP_CUBIC',
|
|
1133
|
+
[VOP_ARC]: 'ER_VECTOR_OP_ARC',
|
|
1134
|
+
};
|
|
1135
|
+
const tape = [];
|
|
1136
|
+
for (let index = 0; index < ops.length;) {
|
|
1137
|
+
const operation = ops[index++];
|
|
1138
|
+
if (operation === VOP_SHAPE) {
|
|
1139
|
+
tape.push(tapeOp('ER_VECTOR_OP_SHAPE'), tapeNumber(ops[index++]));
|
|
1140
|
+
}
|
|
1141
|
+
else if (operation === VOP_ARC) {
|
|
1142
|
+
tape.push(tapeOp(operationNames[operation]), along(ops[index++], sideX), along(ops[index++], sideY), along(ops[index++], sideX), tapeNumber(ops[index++]), tapeNumber(ops[index++]));
|
|
1143
|
+
}
|
|
1144
|
+
else if (operation === VOP_MOVE || operation === VOP_LINE) {
|
|
1145
|
+
tape.push(tapeOp(operationNames[operation]), along(ops[index++], sideX), along(ops[index++], sideY));
|
|
1146
|
+
}
|
|
1147
|
+
else if (operation === VOP_QUAD || operation === VOP_CUBIC) {
|
|
1148
|
+
tape.push(tapeOp(operationNames[operation]));
|
|
1149
|
+
for (let point = 0; point < (operation === VOP_QUAD ? 2 : 3); point++) {
|
|
1150
|
+
tape.push(along(ops[index++], sideX), along(ops[index++], sideY));
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
else {
|
|
1154
|
+
tape.push(tapeOp('ER_VECTOR_OP_CLOSE'));
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
// A stroke scales by the two sides' mean, as flattenSvg's does.
|
|
1158
|
+
const sideStroke = tapeChain(tapeChain(sideX, ['+', sideY]), [
|
|
1159
|
+
'*',
|
|
1160
|
+
tapeNumber(0.5),
|
|
1161
|
+
]);
|
|
1162
|
+
const nodeId = `n${output.allocateNodeId()}`;
|
|
1163
|
+
const vector = {
|
|
1164
|
+
id: output.svgCount++,
|
|
1165
|
+
locals: [
|
|
1166
|
+
{ name: 'scale_x', value: scaleX },
|
|
1167
|
+
{ name: 'scale_y', value: scaleY },
|
|
1168
|
+
],
|
|
1169
|
+
tape,
|
|
1170
|
+
paints: paintsOf(paints).map(paint => ({
|
|
1171
|
+
fill: { kind: 'static', argb: paint.fill },
|
|
1172
|
+
stroke: { kind: 'static', argb: paint.stroke },
|
|
1173
|
+
strokeWidth: tapeChain(tapeNumber(paint.strokeWidth), ['*', sideStroke]),
|
|
1174
|
+
miter: paint.miter,
|
|
1175
|
+
cap: paint.cap,
|
|
1176
|
+
join: paint.join,
|
|
1177
|
+
fillRule: paint.fillRule,
|
|
1178
|
+
fillGradient: { index: paint.fillGradient, when: null },
|
|
1179
|
+
strokeGradient: { index: paint.strokeGradient, when: null },
|
|
1180
|
+
})),
|
|
1181
|
+
gradients: gradients.map(gradient => ({
|
|
1182
|
+
type: gradient.type,
|
|
1183
|
+
stops: (gradient.stops || []).map(stop => ({
|
|
1184
|
+
color: { kind: 'static', argb: stop.color >>> 0 },
|
|
1185
|
+
offset: tapeNumber(stop.offset),
|
|
1186
|
+
})),
|
|
1187
|
+
ax: tapeNumber(gradient.ax || 0),
|
|
1188
|
+
ay: tapeNumber(gradient.ay || 0),
|
|
1189
|
+
bx: tapeNumber(gradient.bx || 0),
|
|
1190
|
+
by: tapeNumber(gradient.by || 0),
|
|
1191
|
+
r: tapeNumber(gradient.r || 0),
|
|
1192
|
+
})),
|
|
1193
|
+
};
|
|
1194
|
+
output.vectors.push({ kind: 'drawing', vector });
|
|
1195
|
+
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width', width, true), svgBoxSize('height', height, true), element.openingElement, scope, environment, svgBootSizes(width, height));
|
|
1196
|
+
node.wiring.push({ kind: 'stateDrivenVector', vector });
|
|
1197
|
+
return node;
|
|
979
1198
|
}
|
|
980
1199
|
/**
|
|
981
1200
|
* Static <Svg>: reuse flattenSvg (full feature set: viewBox, <G>, Path) and bake const arrays.
|
|
982
1201
|
*
|
|
983
1202
|
* @param element The <Svg>.
|
|
984
1203
|
* @param scope Compile-time constants in reach.
|
|
985
|
-
* @param
|
|
986
|
-
* @param
|
|
1204
|
+
* @param output Everything emitted so far.
|
|
1205
|
+
* @param environment The expression environment.
|
|
987
1206
|
*
|
|
988
1207
|
* @returns The node.
|
|
989
1208
|
*/
|
|
990
|
-
function emitSvgStatic(element, scope,
|
|
1209
|
+
function emitSvgStatic(element, scope, output, environment) {
|
|
991
1210
|
// Flatten the subtree and bake its ops and paints into const tables.
|
|
992
1211
|
const svgElement = jsxToSvgElement(element, scope);
|
|
993
|
-
const
|
|
994
|
-
|
|
1212
|
+
const scaled = emitSvgScaled(element, svgElement, scope, output, environment);
|
|
1213
|
+
if (scaled)
|
|
1214
|
+
return scaled;
|
|
1215
|
+
const { ops, paints, gradients } = flattenSvg({
|
|
1216
|
+
...svgElement.props,
|
|
1217
|
+
width: buildValueOf(svgElement.props.width),
|
|
1218
|
+
height: buildValueOf(svgElement.props.height),
|
|
1219
|
+
});
|
|
1220
|
+
const nodeId = `n${output.allocateNodeId()}`;
|
|
995
1221
|
const tables = {
|
|
996
|
-
id:
|
|
1222
|
+
id: output.svgCount++,
|
|
997
1223
|
ops: Array.from(ops),
|
|
998
1224
|
paints: paintsOf(paints),
|
|
999
1225
|
gradients,
|
|
1000
1226
|
};
|
|
1001
1227
|
checkVectorTables(tables);
|
|
1002
|
-
|
|
1228
|
+
output.vectors.push({ kind: 'tables', tables });
|
|
1003
1229
|
// Create the vector node and point it at the tables.
|
|
1004
|
-
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width', svgElement.props.width, svgElement.props.viewBox != null), svgBoxSize('height', svgElement.props.height, svgElement.props.viewBox != null), element.openingElement, scope,
|
|
1230
|
+
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width', svgElement.props.width, svgElement.props.viewBox != null), svgBoxSize('height', svgElement.props.height, svgElement.props.viewBox != null), element.openingElement, scope, environment, svgBootSizes(svgElement.props.width, svgElement.props.height));
|
|
1005
1231
|
node.wiring.push({ kind: 'vectorTables', tables });
|
|
1006
1232
|
return node;
|
|
1007
1233
|
}
|
|
1008
1234
|
/**
|
|
1009
1235
|
* Baked <Svg source={importedSvg}>: the CLI pre-bakes the .svg to a vector artifact (ops/paints/GRADIENTS) *
|
|
1010
|
-
* (bakeSvgArtifacts →
|
|
1236
|
+
* (bakeSvgArtifacts → options.svgArtifacts) since compilePackage is I/O-free. Scaled to the static
|
|
1011
1237
|
* width/height at compile time, then emitted as const tables. This is the path that carries gradients into
|
|
1012
1238
|
* Flow B.
|
|
1013
1239
|
*
|
|
1014
1240
|
* @param element The <Svg>.
|
|
1015
|
-
* @param
|
|
1241
|
+
* @param sourceAttribute Its `source` attribute.
|
|
1016
1242
|
* @param scope Compile-time constants in reach.
|
|
1017
|
-
* @param
|
|
1018
|
-
* @param
|
|
1243
|
+
* @param output Everything emitted so far.
|
|
1244
|
+
* @param environment The expression environment.
|
|
1019
1245
|
*
|
|
1020
1246
|
* @returns The node.
|
|
1021
1247
|
*/
|
|
1022
|
-
function emitSvgSource(element,
|
|
1248
|
+
function emitSvgSource(element, sourceAttribute, scope, output, environment) {
|
|
1023
1249
|
// Resolve the source to its imported .svg's baked artifact.
|
|
1024
|
-
const
|
|
1025
|
-
|
|
1250
|
+
const expression = sourceAttribute.value &&
|
|
1251
|
+
sourceAttribute.value.type === 'JSXExpressionContainer'
|
|
1252
|
+
? sourceAttribute.value.expression
|
|
1026
1253
|
: null;
|
|
1027
|
-
if (!
|
|
1254
|
+
if (!expression || expression.type !== 'Identifier') {
|
|
1028
1255
|
throw new Error('AOT: <Svg source> must reference an imported .svg (source={importedSvg})');
|
|
1029
1256
|
}
|
|
1030
|
-
const svgImport =
|
|
1257
|
+
const svgImport = environment.svgImports.get(expression.name);
|
|
1031
1258
|
if (!svgImport) {
|
|
1032
|
-
throw new Error(`AOT: <Svg source={${
|
|
1259
|
+
throw new Error(`AOT: <Svg source={${expression.name}}> — no matching \`import ${expression.name} from '...svg'\``);
|
|
1033
1260
|
}
|
|
1034
|
-
const artifact =
|
|
1261
|
+
const artifact = environment.svgArtifacts[svgImport.name];
|
|
1035
1262
|
if (!artifact) {
|
|
1036
1263
|
throw new Error(`AOT: vector artifact for "${svgImport.name}" was not baked (internal: opts.svgArtifacts missing it)`);
|
|
1037
1264
|
}
|
|
1038
1265
|
// Scale the artifact at compile time to a static width/height, defaulting to its intrinsic size.
|
|
1039
|
-
const numericAttr = (
|
|
1040
|
-
const
|
|
1266
|
+
const numericAttr = (attributeName, fallback) => {
|
|
1267
|
+
const attribute = element.openingElement.attributes.find((candidate) => candidate.type === 'JSXAttribute' &&
|
|
1041
1268
|
candidate.name &&
|
|
1042
|
-
candidate.name.name ===
|
|
1043
|
-
if (!
|
|
1269
|
+
candidate.name.name === attributeName);
|
|
1270
|
+
if (!attribute)
|
|
1044
1271
|
return fallback;
|
|
1045
1272
|
let value = true; // a bare attribute
|
|
1046
|
-
if (
|
|
1047
|
-
value =
|
|
1273
|
+
if (attribute.value?.type === 'StringLiteral') {
|
|
1274
|
+
value = attribute.value.value;
|
|
1048
1275
|
}
|
|
1049
|
-
else if (
|
|
1276
|
+
else if (attribute.value?.type === 'JSXExpressionContainer') {
|
|
1050
1277
|
try {
|
|
1051
|
-
value = evalStatic(
|
|
1278
|
+
value = evalStatic(attribute.value.expression, scope);
|
|
1052
1279
|
}
|
|
1053
1280
|
catch {
|
|
1054
|
-
throw at(aotError('AOT: <Svg source> width/height must be a static number'),
|
|
1281
|
+
throw at(aotError('AOT: <Svg source> width/height must be a static number'), attribute);
|
|
1055
1282
|
}
|
|
1056
1283
|
}
|
|
1057
1284
|
try {
|
|
1058
|
-
return svgPixels(
|
|
1285
|
+
return svgPixels(attributeName, value, true);
|
|
1059
1286
|
}
|
|
1060
1287
|
catch (error) {
|
|
1061
|
-
throw at(error,
|
|
1288
|
+
throw at(error, attribute);
|
|
1062
1289
|
}
|
|
1063
1290
|
};
|
|
1064
1291
|
// The drawing is scaled to the box, so a style size wins over the attribute, as in Flow A.
|
|
1065
|
-
const { staticAssigns: style } = collectStyleAssigns(element.openingElement, scope,
|
|
1292
|
+
const { staticAssigns: style } = collectStyleAssigns(element.openingElement, scope, environment, elementSpec('Svg'));
|
|
1066
1293
|
const boxSize = (field, fallback) => {
|
|
1067
|
-
if (style.some(assign => assign.field === `${field}
|
|
1294
|
+
if (style.some(assign => assign.field === `${field}_percent`)) {
|
|
1068
1295
|
throw aotError(`AOT: a percentage style ${field} on an <Svg source> is not supported`, 'the drawing is scaled to the box at build time, when the parent size is unknown; give the ' +
|
|
1069
1296
|
`<Svg> its ${field} in pixels.`);
|
|
1070
1297
|
}
|
|
1071
1298
|
const styled = style.find(assign => assign.field === field);
|
|
1072
|
-
return styled ? Number(styled.
|
|
1299
|
+
return styled ? Number(styled.value) : numericAttr(field, fallback);
|
|
1073
1300
|
};
|
|
1074
1301
|
// Raster fallback: a .svg with unsupported features was baked to a PNG; register it and emit an Image node.
|
|
1075
1302
|
const width = boxSize('width', artifact.width);
|
|
1076
1303
|
const height = boxSize('height', artifact.height);
|
|
1077
1304
|
if (artifact.kind === 'raster') {
|
|
1078
1305
|
if (artifact.png) {
|
|
1079
|
-
|
|
1306
|
+
output.images.set(artifact.name, artifact.png);
|
|
1080
1307
|
}
|
|
1081
|
-
const node = lowerSvgBox(`n${
|
|
1308
|
+
const node = lowerSvgBox(`n${output.allocateNodeId()}`, NODE_TYPES.Image, svgBoxSize('width', width, true), svgBoxSize('height', height, true), element.openingElement, scope, environment);
|
|
1082
1309
|
node.image = { kind: 'static', name: artifact.name };
|
|
1083
1310
|
return node;
|
|
1084
1311
|
}
|
|
1085
1312
|
// Scale the vector artifact to the box and bake its ops, paints and gradients into const tables.
|
|
1086
1313
|
const scaled = scaleVectorArtifact(artifact, width, height);
|
|
1087
|
-
const nodeId = `n${
|
|
1314
|
+
const nodeId = `n${output.allocateNodeId()}`;
|
|
1088
1315
|
const tables = {
|
|
1089
|
-
id:
|
|
1316
|
+
id: output.svgCount++,
|
|
1090
1317
|
ops: Array.from(scaled.ops),
|
|
1091
1318
|
paints: paintsOf(scaled.paints),
|
|
1092
1319
|
gradients: scaled.gradients || [],
|
|
1093
1320
|
};
|
|
1094
1321
|
checkVectorTables(tables);
|
|
1095
|
-
|
|
1322
|
+
output.vectors.push({ kind: 'tables', tables });
|
|
1096
1323
|
// Create the vector node and point it at the tables.
|
|
1097
|
-
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width', width, true), svgBoxSize('height', height, true), element.openingElement, scope,
|
|
1324
|
+
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width', width, true), svgBoxSize('height', height, true), element.openingElement, scope, environment);
|
|
1098
1325
|
node.wiring.push({ kind: 'vectorTables', tables });
|
|
1099
1326
|
return node;
|
|
1100
1327
|
}
|
|
1101
1328
|
/**
|
|
1102
1329
|
* State-driven <Svg> (flat Arc/Circle/Rect/Line/static-Path; no viewBox/<G>): emit a mutable op-tape
|
|
1103
|
-
*
|
|
1330
|
+
* that is recomputed from state, at build and on each update.
|
|
1104
1331
|
*
|
|
1105
1332
|
* @param element The <Svg>.
|
|
1106
1333
|
* @param scope Compile-time constants in reach.
|
|
1107
|
-
* @param
|
|
1108
|
-
* @param
|
|
1334
|
+
* @param output Everything emitted so far.
|
|
1335
|
+
* @param environment The expression environment.
|
|
1109
1336
|
* @param state The component's state table.
|
|
1110
1337
|
*
|
|
1111
1338
|
* @returns The node.
|
|
1112
1339
|
*/
|
|
1113
|
-
function emitSvgDynamic(element, scope,
|
|
1340
|
+
function emitSvgDynamic(element, scope, output, environment, state) {
|
|
1114
1341
|
// The root's attributes give the box; this path has no viewBox transform.
|
|
1115
|
-
const
|
|
1116
|
-
if (
|
|
1342
|
+
const rootAttributes = svgAttrs(element.openingElement, scope, environment);
|
|
1343
|
+
if (rootAttributes.viewBox != null) {
|
|
1117
1344
|
throw new Error('AOT: a viewBox on a state-driven <Svg> is not yet supported — size shapes in the width/height space');
|
|
1118
1345
|
}
|
|
1119
|
-
// Lower each shape to tape slots (after an
|
|
1346
|
+
// Lower each shape to tape slots (after an ER_VECTOR_OP_SHAPE marker naming its paint) and its paint.
|
|
1120
1347
|
const tape = [];
|
|
1121
1348
|
const locals = []; // declared ahead of the tape (rounded-rect radii)
|
|
1122
1349
|
const shapePaints = [];
|
|
@@ -1126,23 +1353,22 @@ function emitSvgDynamic(element, scope, out, env, state) {
|
|
|
1126
1353
|
if (!lowerShape) {
|
|
1127
1354
|
throw new Error(`AOT: <${type}> is not a supported shape in a state-driven <Svg> (no <G>/viewBox yet)`);
|
|
1128
1355
|
}
|
|
1129
|
-
const
|
|
1130
|
-
//
|
|
1131
|
-
const shape = lowerShape(
|
|
1356
|
+
const attributes = svgAttrs(child.openingElement, scope, environment);
|
|
1357
|
+
// The builder is this <Svg>'s own function, so the shape index alone keeps a local's name unique.
|
|
1358
|
+
const shape = lowerShape(attributes, `s${shapePaints.length}`);
|
|
1132
1359
|
if (!shape.entries.length)
|
|
1133
1360
|
continue;
|
|
1134
1361
|
locals.push(...shape.locals);
|
|
1135
|
-
tape.push(tapeOp('
|
|
1362
|
+
tape.push(tapeOp('ER_VECTOR_OP_SHAPE'), tapeNumber(shapePaints.length), ...shape.entries);
|
|
1136
1363
|
// The root's paint cascades to each shape, as flattenSvg does.
|
|
1137
|
-
const
|
|
1364
|
+
const paintAttributes = { ...attributes };
|
|
1138
1365
|
for (const name of PAINT_PROPS)
|
|
1139
|
-
|
|
1140
|
-
shapePaints.push(lowerShapePaint(
|
|
1366
|
+
paintAttributes[name] ??= rootAttributes[name];
|
|
1367
|
+
shapePaints.push(lowerShapePaint(paintAttributes, environment, scope));
|
|
1141
1368
|
}
|
|
1142
1369
|
// Allocate the node and this <Svg>'s table names.
|
|
1143
|
-
const nodeId = `n${
|
|
1144
|
-
const svgId =
|
|
1145
|
-
out.needsMath = true; // build_svg uses cosf/sinf/M_PI for arcs
|
|
1370
|
+
const nodeId = `n${output.allocateNodeId()}`;
|
|
1371
|
+
const svgId = output.svgCount++;
|
|
1146
1372
|
// Gather the gradients into one 1-based table per <Svg>, fill then stroke, shape by shape.
|
|
1147
1373
|
const gradients = [];
|
|
1148
1374
|
const place = (shapeGradient) => shapeGradient
|
|
@@ -1162,10 +1388,10 @@ function emitSvgDynamic(element, scope, out, env, state) {
|
|
|
1162
1388
|
})),
|
|
1163
1389
|
gradients,
|
|
1164
1390
|
};
|
|
1165
|
-
// Declare the tables and
|
|
1166
|
-
|
|
1167
|
-
// Create the vector node, build its tape once, and register it for
|
|
1168
|
-
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width',
|
|
1391
|
+
// Declare the tables and the builder, which rewrites the tape (and anything in the paints state drives).
|
|
1392
|
+
output.vectors.push({ kind: 'drawing', vector });
|
|
1393
|
+
// Create the vector node, build its tape once, and register it for the update to rebuild.
|
|
1394
|
+
const node = lowerSvgBox(nodeId, 'ER_NODE_VECTOR', svgBoxSize('width', rootAttributes.width, false), svgBoxSize('height', rootAttributes.height, false), element.openingElement, scope, environment, svgBootSizes(rootAttributes.width, rootAttributes.height));
|
|
1169
1395
|
node.wiring.push({ kind: 'stateDrivenVector', vector });
|
|
1170
1396
|
return node;
|
|
1171
1397
|
}
|