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