embedded-react 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +5 -0
- package/README.md +3 -2
- package/aot/app-file-loader.mjs +116 -0
- package/aot/compile.mjs +17 -47
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/animations.mjs +299 -57
- package/aot/compiler/boot.mjs +27 -2
- package/aot/compiler/check.mjs +62 -10
- package/aot/compiler/collect.mjs +517 -137
- package/aot/compiler/contexts.mjs +391 -0
- package/aot/compiler/control-flow.mjs +276 -117
- package/aot/compiler/custom-hooks.mjs +686 -0
- package/aot/compiler/elements/button.mjs +182 -0
- package/aot/compiler/elements/image-background.mjs +76 -0
- package/aot/compiler/elements/image.mjs +2 -1
- package/aot/compiler/elements/props.mjs +47 -8
- package/aot/compiler/elements/rewrite.mjs +118 -0
- package/aot/compiler/elements/section-list.mjs +370 -0
- package/aot/compiler/expressions.mjs +1378 -97
- package/aot/compiler/handlers.mjs +1807 -354
- package/aot/compiler/helper-values.mjs +312 -0
- package/aot/compiler/ir/nodes.mjs +40 -1
- package/aot/compiler/ir/reads.mjs +206 -5
- package/aot/compiler/ir/text.mjs +5 -1
- package/aot/compiler/ir/types.mjs +12 -0
- package/aot/compiler/kept-states.mjs +177 -0
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- 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 +203 -53
- 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 +45 -31
- package/aot/compiler/options.mjs +3 -2
- package/aot/compiler/out.mjs +18 -1
- package/aot/compiler/package/animations.mjs +133 -23
- package/aot/compiler/package/assemble.mjs +2 -4
- package/aot/compiler/package/bytecode.mjs +37 -8
- package/aot/compiler/package/checks.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +906 -33
- package/aot/compiler/package/enums.mjs +10 -0
- package/aot/compiler/package/format.mjs +16 -3
- package/aot/compiler/package/lower.mjs +707 -176
- package/aot/compiler/package/string-caps.mjs +515 -0
- package/aot/compiler/package/tables.mjs +24 -0
- package/aot/compiler/package/writer.mjs +22 -0
- package/aot/compiler/parse.mjs +3 -1
- 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/scroll-to.mjs +112 -0
- package/aot/compiler/static-eval.mjs +315 -7
- package/aot/compiler/style-text.mjs +20 -5
- package/aot/compiler/svg.mjs +13 -7
- 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 +321 -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/style-map.mjs +94 -3
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +85 -94
- package/cli.mjs +490 -586
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +146 -213
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +94 -116
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/Dimensions.mjs +32 -0
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +238 -5
- package/src/embedded-react/index.js +26 -17
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -148
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
|
@@ -1,1295 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2026 Cory Lamming
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
// Pure SVG -> engine op-tape compiler. No engine/React imports, so it is unit-testable in isolation.
|
|
18
|
-
//
|
|
19
|
-
// The engine's ER_NODE_VECTOR node takes a flat float "op-tape" plus a flat paint table (see the
|
|
20
|
-
// ER_VECTOR_OP_* contract in er_scene.h). This module compiles a declarative <Svg> subtree (Path/Circle/
|
|
21
|
-
// Rect/Line/Ellipse/G elements) into that form: it parses SVG `d` path data, converts primitive
|
|
22
|
-
// shapes to path ops, bakes viewBox + <G> translate/scale into coordinates, and resolves inherited
|
|
23
|
-
// paint. The opcodes below MUST stay in sync with er_scene.h.
|
|
24
|
-
|
|
25
|
-
import {NAMED_COLORS} from './named-colors.js';
|
|
26
|
-
|
|
27
|
-
// Op-tape vocabulary — exported so the build-time SVG baker (assets/bake-svg.mjs) can reuse it without
|
|
28
|
-
// duplicating the encoding (and without pulling its SVG-parser dependency into this runtime module).
|
|
29
|
-
export const VOP_SHAPE = 0;
|
|
30
|
-
export const VOP_MOVE = 1;
|
|
31
|
-
export const VOP_LINE = 2;
|
|
32
|
-
export const VOP_QUAD = 3;
|
|
33
|
-
export const VOP_CUBIC = 4;
|
|
34
|
-
export const VOP_ARC = 5;
|
|
35
|
-
export const VOP_CLOSE = 6;
|
|
36
|
-
|
|
37
|
-
// Control-point ratio for approximating a quarter (circular or elliptical) arc with one cubic:
|
|
38
|
-
// 4/3 * (sqrt(2) - 1). Used by rounded-rect corners.
|
|
39
|
-
export const KAPPA = 0.5522847498307936;
|
|
40
|
-
|
|
41
|
-
export const CAP = {butt: 0, round: 1, square: 2};
|
|
42
|
-
export const JOIN = {miter: 0, round: 1, bevel: 2};
|
|
43
|
-
|
|
44
|
-
// Paint record width in the flat paint table: [fill, stroke, strokeWidth, miter, cap, join, fillRule,
|
|
45
|
-
// fillGradient, strokeGradient]. fillGradient/strokeGradient are 1-based indices into the gradient table (0 = solid). MUST
|
|
46
|
-
// match the bridge's VEC_PAINT_STRIDE (native_ui_bridge.c) and the AOT's paint emission.
|
|
47
|
-
export const PAINT_STRIDE = 9;
|
|
48
|
-
|
|
49
|
-
// Gradient table encoding — a gradient descriptor is { type, stops: [{color, offset}], ax, ay, bx, by, r }.
|
|
50
|
-
// type 1 = linear (axis (ax,ay)->(bx,by)), 2 = radial (centre (ax,ay), radius r). The flat float record is
|
|
51
|
-
// [type, stopCount, (color, offset) × GRAD_MAX_STOPS, ax, ay, bx, by, r]; MUST match the bridge's
|
|
52
|
-
// VEC_GRADIENT_STRIDE and the engine's ER_VECTOR_GRADIENT_MAX_STOPS.
|
|
53
|
-
export const GRAD_MAX_STOPS = 8; // MUST match the engine's ER_VECTOR_GRADIENT_MAX_STOPS (er_scene.h)
|
|
54
|
-
export const GRAD_STRIDE = 2 + GRAD_MAX_STOPS * 2 + 5; // 23
|
|
55
|
-
export const GRAD_LINEAR = 1;
|
|
56
|
-
export const GRAD_RADIAL = 2;
|
|
57
|
-
export const GRAD_CONIC = 3;
|
|
58
|
-
|
|
59
|
-
const ALPHA_OPAQUE = 255;
|
|
60
|
-
const CHANNEL_MAX = 255;
|
|
61
|
-
const OPAQUE_BLACK = 0xff000000;
|
|
62
|
-
|
|
63
|
-
const HALF_TURN_DEGREES = 180;
|
|
64
|
-
const QUARTER_TURN_RADIANS = Math.PI / 2;
|
|
65
|
-
const FULL_TURN_RADIANS = 2 * Math.PI;
|
|
66
|
-
// Arc convenience angles start at 12 o'clock; the engine's start at +X, a quarter turn earlier.
|
|
67
|
-
const TWELVE_OCLOCK_DEGREES = 90;
|
|
68
|
-
|
|
69
|
-
const DEFAULT_STROKE_WIDTH = 1;
|
|
70
|
-
const DEFAULT_MITER_LIMIT = 4;
|
|
71
|
-
const FILL_RULE_NONZERO = 0;
|
|
72
|
-
const FILL_RULE_EVEN_ODD = 1;
|
|
73
|
-
|
|
74
|
-
// A shape's tape starts with VOP_SHAPE and its paint index.
|
|
75
|
-
const SHAPE_HEADER_LENGTH = 2;
|
|
76
|
-
// Where strokeWidth sits in a paint record (see PAINT_STRIDE).
|
|
77
|
-
const PAINT_STROKE_WIDTH_SLOT = 2;
|
|
78
|
-
|
|
79
|
-
/** Linearly interpolates two straight-alpha ARGB8888 colors, per channel. */
|
|
80
|
-
function lerpArgb(fromColor, toColor, fraction) {
|
|
81
|
-
const channel = shift => {
|
|
82
|
-
const fromChannel = (fromColor >>> shift) & 0xff;
|
|
83
|
-
const toChannel = (toColor >>> shift) & 0xff;
|
|
84
|
-
return (
|
|
85
|
-
Math.round(fromChannel + (toChannel - fromChannel) * fraction) & 0xff
|
|
86
|
-
);
|
|
87
|
-
};
|
|
88
|
-
return (
|
|
89
|
-
((channel(24) << 24) |
|
|
90
|
-
(channel(16) << 16) |
|
|
91
|
-
(channel(8) << 8) |
|
|
92
|
-
channel(0)) >>>
|
|
93
|
-
0
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** Gradient color at `position` (stops ascending by offset; clamps to the endpoint colors). */
|
|
98
|
-
function evalStopsAt(stops, position) {
|
|
99
|
-
if (!stops.length) return 0;
|
|
100
|
-
if (position <= stops[0].offset) return stops[0].color >>> 0;
|
|
101
|
-
const last = stops[stops.length - 1];
|
|
102
|
-
if (position >= last.offset) return last.color >>> 0;
|
|
103
|
-
for (let index = 0; index < stops.length - 1; index++) {
|
|
104
|
-
const before = stops[index];
|
|
105
|
-
const after = stops[index + 1];
|
|
106
|
-
if (position <= after.offset) {
|
|
107
|
-
const span = after.offset - before.offset;
|
|
108
|
-
return lerpArgb(
|
|
109
|
-
before.color >>> 0,
|
|
110
|
-
after.color >>> 0,
|
|
111
|
-
span > 0 ? (position - before.offset) / span : 0,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return last.color >>> 0;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** Reduces a stop list to at most `maxStops` entries, resampling evenly across the stop range when it's over. */
|
|
119
|
-
function capStops(stops, maxStops) {
|
|
120
|
-
if (stops.length <= maxStops) return stops;
|
|
121
|
-
const firstOffset = stops[0].offset;
|
|
122
|
-
const lastOffset = stops[stops.length - 1].offset;
|
|
123
|
-
const resampled = [];
|
|
124
|
-
for (let index = 0; index < maxStops; index++) {
|
|
125
|
-
const offset =
|
|
126
|
-
maxStops === 1
|
|
127
|
-
? firstOffset
|
|
128
|
-
: firstOffset + (lastOffset - firstOffset) * (index / (maxStops - 1));
|
|
129
|
-
resampled.push({color: evalStopsAt(stops, offset), offset: offset});
|
|
130
|
-
}
|
|
131
|
-
return resampled;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Encodes gradient descriptors into the flat float array NativeUI.setVectorOps takes (GRAD_STRIDE/gradient).
|
|
135
|
-
* A gradient with more than GRAD_MAX_STOPS stops is resampled down (not truncated) so it still ramps right. */
|
|
136
|
-
export function encodeVectorGradients(gradients) {
|
|
137
|
-
const encoded = [];
|
|
138
|
-
if (!gradients) return encoded;
|
|
139
|
-
for (const gradient of gradients) {
|
|
140
|
-
const stops = capStops(gradient.stops || [], GRAD_MAX_STOPS);
|
|
141
|
-
encoded.push(gradient.type, stops.length);
|
|
142
|
-
for (let stopIndex = 0; stopIndex < GRAD_MAX_STOPS; stopIndex++) {
|
|
143
|
-
const stop = stops[stopIndex];
|
|
144
|
-
encoded.push(stop ? stop.color >>> 0 : 0, stop ? stop.offset : 0);
|
|
145
|
-
}
|
|
146
|
-
encoded.push(
|
|
147
|
-
gradient.ax || 0,
|
|
148
|
-
gradient.ay || 0,
|
|
149
|
-
gradient.bx || 0,
|
|
150
|
-
gradient.by || 0,
|
|
151
|
-
gradient.r || 0,
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
return encoded;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// The shared named colors plus SVG's `none`; everything else goes through hex/rgb parsing.
|
|
158
|
-
const SVG_NAMED_COLORS = Object.freeze(
|
|
159
|
-
Object.assign(Object.create(null), NAMED_COLORS, {none: 0}),
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
// Cache string->uint32 results: a theme reuses a handful of color strings, so on a continuous drag
|
|
163
|
-
// (which recompiles the dial every move) this turns repeated hex parsing into a Map lookup.
|
|
164
|
-
const _colorCache = new Map();
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Parses a CSS color string to a uint32 ARGB8888. Returns 0 (fully transparent) for none/transparent/
|
|
168
|
-
* null, so the engine treats it as "no fill"/"no stroke". Accepts #rgb, #rgba, #rrggbb, #rrggbbaa,
|
|
169
|
-
* rgb()/rgba(), and the named colors above. String results are memoised (see _colorCache).
|
|
170
|
-
*/
|
|
171
|
-
export function parseColor(color) {
|
|
172
|
-
if (color == null || color === false) return 0;
|
|
173
|
-
if (typeof color === 'number') return color >>> 0;
|
|
174
|
-
const cached = _colorCache.get(color);
|
|
175
|
-
if (cached !== undefined) return cached;
|
|
176
|
-
const parsed = parseColorString(color);
|
|
177
|
-
_colorCache.set(color, parsed);
|
|
178
|
-
return parsed;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function parseColorString(color) {
|
|
182
|
-
let text = String(color).trim().toLowerCase();
|
|
183
|
-
if (text in SVG_NAMED_COLORS) return SVG_NAMED_COLORS[text] >>> 0;
|
|
184
|
-
if (text[0] === '#') {
|
|
185
|
-
text = text.slice(1);
|
|
186
|
-
let red,
|
|
187
|
-
green,
|
|
188
|
-
blue,
|
|
189
|
-
alpha = ALPHA_OPAQUE;
|
|
190
|
-
if (text.length === 3 || text.length === 4) {
|
|
191
|
-
red = parseInt(text[0] + text[0], 16);
|
|
192
|
-
green = parseInt(text[1] + text[1], 16);
|
|
193
|
-
blue = parseInt(text[2] + text[2], 16);
|
|
194
|
-
if (text.length === 4) alpha = parseInt(text[3] + text[3], 16);
|
|
195
|
-
} else if (text.length === 6 || text.length === 8) {
|
|
196
|
-
red = parseInt(text.slice(0, 2), 16);
|
|
197
|
-
green = parseInt(text.slice(2, 4), 16);
|
|
198
|
-
blue = parseInt(text.slice(4, 6), 16);
|
|
199
|
-
if (text.length === 8) alpha = parseInt(text.slice(6, 8), 16);
|
|
200
|
-
} else {
|
|
201
|
-
return OPAQUE_BLACK;
|
|
202
|
-
}
|
|
203
|
-
return ((alpha << 24) | (red << 16) | (green << 8) | blue) >>> 0;
|
|
204
|
-
}
|
|
205
|
-
if (text.startsWith('rgb')) {
|
|
206
|
-
const match = text.match(/rgba?\(([^)]+)\)/);
|
|
207
|
-
if (match) {
|
|
208
|
-
const parts = match[1].split(',').map(part => part.trim());
|
|
209
|
-
const red = parseInt(parts[0], 10) || 0;
|
|
210
|
-
const green = parseInt(parts[1], 10) || 0;
|
|
211
|
-
const blue = parseInt(parts[2], 10) || 0;
|
|
212
|
-
const alpha =
|
|
213
|
-
parts[3] != null
|
|
214
|
-
? Math.round(parseFloat(parts[3]) * CHANNEL_MAX)
|
|
215
|
-
: ALPHA_OPAQUE;
|
|
216
|
-
return ((alpha << 24) | (red << 16) | (green << 8) | blue) >>> 0;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return OPAQUE_BLACK; // unknown -> opaque black (SVG-ish default)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// --- SVG `d` path-data parser -------------------------------------------------------------------
|
|
223
|
-
|
|
224
|
-
// Hoisted so the pattern is compiled once at module load, not on every parsePath call. The /g lastIndex is reset below.
|
|
225
|
-
const PATH_RE = /([MmLlHhVvCcSsQqTtAaZz])|(-?\d*\.?\d+(?:[eE][-+]?\d+)?)/g;
|
|
226
|
-
|
|
227
|
-
/** Tokenizes a `d` string into {command, values[]} groups (one per command letter). */
|
|
228
|
-
function tokenizePath(d) {
|
|
229
|
-
const commands = [];
|
|
230
|
-
const pattern = PATH_RE;
|
|
231
|
-
pattern.lastIndex = 0;
|
|
232
|
-
let match;
|
|
233
|
-
let current = null;
|
|
234
|
-
while ((match = pattern.exec(d)) !== null) {
|
|
235
|
-
if (match[1]) {
|
|
236
|
-
current = {command: match[1], values: []};
|
|
237
|
-
commands.push(current);
|
|
238
|
-
} else if (current) {
|
|
239
|
-
current.values.push(parseFloat(match[2]));
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return commands;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** Converts an SVG elliptical arc (endpoint form) to a list of cubic bezier segments. */
|
|
246
|
-
function arcToCubics(x0, y0, rx, ry, rotationDegrees, largeArc, sweep, x, y) {
|
|
247
|
-
const cubics = [];
|
|
248
|
-
if (rx === 0 || ry === 0) {
|
|
249
|
-
cubics.push([x0, y0, x, y, x, y]); // degenerate -> straight line as a cubic
|
|
250
|
-
return cubics;
|
|
251
|
-
}
|
|
252
|
-
rx = Math.abs(rx);
|
|
253
|
-
ry = Math.abs(ry);
|
|
254
|
-
const rotation = (rotationDegrees * Math.PI) / HALF_TURN_DEGREES;
|
|
255
|
-
const cosRotation = Math.cos(rotation);
|
|
256
|
-
const sinRotation = Math.sin(rotation);
|
|
257
|
-
const halfDeltaX = (x0 - x) / 2;
|
|
258
|
-
const halfDeltaY = (y0 - y) / 2;
|
|
259
|
-
const primeX = cosRotation * halfDeltaX + sinRotation * halfDeltaY;
|
|
260
|
-
const primeY = -sinRotation * halfDeltaX + cosRotation * halfDeltaY;
|
|
261
|
-
let rxSquared = rx * rx;
|
|
262
|
-
let rySquared = ry * ry;
|
|
263
|
-
const primeXSquared = primeX * primeX;
|
|
264
|
-
const primeYSquared = primeY * primeY;
|
|
265
|
-
// Correct out-of-range radii.
|
|
266
|
-
const radiiScale = primeXSquared / rxSquared + primeYSquared / rySquared;
|
|
267
|
-
if (radiiScale > 1) {
|
|
268
|
-
const scale = Math.sqrt(radiiScale);
|
|
269
|
-
rx *= scale;
|
|
270
|
-
ry *= scale;
|
|
271
|
-
rxSquared = rx * rx;
|
|
272
|
-
rySquared = ry * ry;
|
|
273
|
-
}
|
|
274
|
-
let sign = largeArc !== sweep ? 1 : -1;
|
|
275
|
-
let numerator =
|
|
276
|
-
rxSquared * rySquared -
|
|
277
|
-
rxSquared * primeYSquared -
|
|
278
|
-
rySquared * primeXSquared;
|
|
279
|
-
if (numerator < 0) numerator = 0;
|
|
280
|
-
const coefficient =
|
|
281
|
-
sign *
|
|
282
|
-
Math.sqrt(
|
|
283
|
-
numerator / (rxSquared * primeYSquared + rySquared * primeXSquared || 1),
|
|
284
|
-
);
|
|
285
|
-
const primeCenterX = (coefficient * rx * primeY) / ry;
|
|
286
|
-
const primeCenterY = (-coefficient * ry * primeX) / rx;
|
|
287
|
-
const cx =
|
|
288
|
-
cosRotation * primeCenterX - sinRotation * primeCenterY + (x0 + x) / 2;
|
|
289
|
-
const cy =
|
|
290
|
-
sinRotation * primeCenterX + cosRotation * primeCenterY + (y0 + y) / 2;
|
|
291
|
-
const angleBetween = (fromX, fromY, toX, toY) => {
|
|
292
|
-
const dot = fromX * toX + fromY * toY;
|
|
293
|
-
const lengthProduct =
|
|
294
|
-
Math.sqrt((fromX * fromX + fromY * fromY) * (toX * toX + toY * toY)) || 1;
|
|
295
|
-
let angle = Math.acos(Math.max(-1, Math.min(1, dot / lengthProduct)));
|
|
296
|
-
if (fromX * toY - fromY * toX < 0) angle = -angle;
|
|
297
|
-
return angle;
|
|
298
|
-
};
|
|
299
|
-
const startAngle = angleBetween(
|
|
300
|
-
1,
|
|
301
|
-
0,
|
|
302
|
-
(primeX - primeCenterX) / rx,
|
|
303
|
-
(primeY - primeCenterY) / ry,
|
|
304
|
-
);
|
|
305
|
-
let sweepAngle = angleBetween(
|
|
306
|
-
(primeX - primeCenterX) / rx,
|
|
307
|
-
(primeY - primeCenterY) / ry,
|
|
308
|
-
(-primeX - primeCenterX) / rx,
|
|
309
|
-
(-primeY - primeCenterY) / ry,
|
|
310
|
-
);
|
|
311
|
-
if (!sweep && sweepAngle > 0) sweepAngle -= FULL_TURN_RADIANS;
|
|
312
|
-
if (sweep && sweepAngle < 0) sweepAngle += FULL_TURN_RADIANS;
|
|
313
|
-
const segmentCount = Math.ceil(Math.abs(sweepAngle) / QUARTER_TURN_RADIANS);
|
|
314
|
-
const segmentAngle = sweepAngle / segmentCount;
|
|
315
|
-
const handleScale = (4 / 3) * Math.tan(segmentAngle / 4);
|
|
316
|
-
let segmentStart = startAngle;
|
|
317
|
-
let previousX = x0;
|
|
318
|
-
let previousY = y0;
|
|
319
|
-
for (let segment = 0; segment < segmentCount; segment++) {
|
|
320
|
-
const segmentEnd = segmentStart + segmentAngle;
|
|
321
|
-
const cosStart = Math.cos(segmentStart);
|
|
322
|
-
const sinStart = Math.sin(segmentStart);
|
|
323
|
-
const cosEnd = Math.cos(segmentEnd);
|
|
324
|
-
const sinEnd = Math.sin(segmentEnd);
|
|
325
|
-
const startPointX =
|
|
326
|
-
cx + rx * cosRotation * cosStart - ry * sinRotation * sinStart;
|
|
327
|
-
const startPointY =
|
|
328
|
-
cy + rx * sinRotation * cosStart + ry * cosRotation * sinStart;
|
|
329
|
-
const endPointX =
|
|
330
|
-
cx + rx * cosRotation * cosEnd - ry * sinRotation * sinEnd;
|
|
331
|
-
const endPointY =
|
|
332
|
-
cy + rx * sinRotation * cosEnd + ry * cosRotation * sinEnd;
|
|
333
|
-
const startTangentX =
|
|
334
|
-
-rx * cosRotation * sinStart - ry * sinRotation * cosStart;
|
|
335
|
-
const startTangentY =
|
|
336
|
-
-rx * sinRotation * sinStart + ry * cosRotation * cosStart;
|
|
337
|
-
const endTangentX = -rx * cosRotation * sinEnd - ry * sinRotation * cosEnd;
|
|
338
|
-
const endTangentY = -rx * sinRotation * sinEnd + ry * cosRotation * cosEnd;
|
|
339
|
-
cubics.push([
|
|
340
|
-
previousX + handleScale * startTangentX,
|
|
341
|
-
previousY + handleScale * startTangentY,
|
|
342
|
-
endPointX - handleScale * endTangentX,
|
|
343
|
-
endPointY - handleScale * endTangentY,
|
|
344
|
-
endPointX,
|
|
345
|
-
endPointY,
|
|
346
|
-
]);
|
|
347
|
-
previousX = endPointX;
|
|
348
|
-
previousY = endPointY;
|
|
349
|
-
segmentStart = segmentEnd;
|
|
350
|
-
}
|
|
351
|
-
return cubics;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Parses an SVG `d` string into engine path ops (a flat number array, NO leading SHAPE op). Supports
|
|
356
|
-
* M/L/H/V/C/S/Q/T/A/Z and their relative (lowercase) forms; arcs are converted to cubics.
|
|
357
|
-
*
|
|
358
|
-
* Results are cached by `d` string, so the returned array is SHARED — read it, never mutate it.
|
|
359
|
-
*/
|
|
360
|
-
export function parsePath(d) {
|
|
361
|
-
return pathEntry(d).ops;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// Parsed `d` strings, keyed on the string itself. An <Svg> recompiles its whole tape whenever any
|
|
365
|
-
// part of it changes, so without this a moving needle re-runs the tokenizer and the arc->cubic
|
|
366
|
-
// conversion over every static path beside it, only for the engine to memcmp the result and throw
|
|
367
|
-
// the identical bytes away. Held to both an op budget and an entry count, and a path too big to fit
|
|
368
|
-
// the budget on its own is never stored, so nothing can push it over; an entry also holds one
|
|
369
|
-
// transformed copy (see lastTransform), so the real footprint is ~2x the op budget. The bridge caps a single
|
|
370
|
-
// <Svg> at 1024 ops and 16 shapes, so this holds several screens' worth.
|
|
371
|
-
const PATH_CACHE = new Map();
|
|
372
|
-
const PATH_CACHE_MAX_OPS = 4096;
|
|
373
|
-
const PATH_CACHE_MAX_ENTRIES = 128;
|
|
374
|
-
let _pathCacheOps = 0;
|
|
375
|
-
|
|
376
|
-
/** Cache record for one `d` string: `ops` are the parsed path ops, `lastTransform` the last transform applied
|
|
377
|
-
* to them (see transformCached). Coerces its key, so a non-string `d` still hits the cache instead
|
|
378
|
-
* of keying a fresh entry off object identity every call. */
|
|
379
|
-
function pathEntry(d) {
|
|
380
|
-
const key = typeof d === 'string' ? d : String(d);
|
|
381
|
-
const hit = PATH_CACHE.get(key);
|
|
382
|
-
if (hit !== undefined) {
|
|
383
|
-
// A Map iterates oldest-first, so re-inserting on a hit makes eviction LRU — which is what keeps
|
|
384
|
-
// a static face cached next to a needle whose `d` is rebuilt every frame.
|
|
385
|
-
PATH_CACHE.delete(key);
|
|
386
|
-
PATH_CACHE.set(key, hit);
|
|
387
|
-
return hit;
|
|
388
|
-
}
|
|
389
|
-
const entry = {ops: parsePathUncached(key), lastTransform: null};
|
|
390
|
-
// A path that alone exceeds the whole budget is parsed but not stored: caching it would blow the
|
|
391
|
-
// bound it is measured against, and at that size it is already past the bridge's per-<Svg> op cap.
|
|
392
|
-
if (entry.ops.length > PATH_CACHE_MAX_OPS) return entry;
|
|
393
|
-
PATH_CACHE.set(key, entry);
|
|
394
|
-
_pathCacheOps += entry.ops.length;
|
|
395
|
-
while (
|
|
396
|
-
_pathCacheOps > PATH_CACHE_MAX_OPS ||
|
|
397
|
-
PATH_CACHE.size > PATH_CACHE_MAX_ENTRIES
|
|
398
|
-
) {
|
|
399
|
-
const oldest = PATH_CACHE.keys().next().value;
|
|
400
|
-
_pathCacheOps -= PATH_CACHE.get(oldest).ops.length;
|
|
401
|
-
PATH_CACHE.delete(oldest);
|
|
402
|
-
}
|
|
403
|
-
return entry;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/** Drops every cached path. Only for tests that measure parse work. */
|
|
407
|
-
export function clearPathCache() {
|
|
408
|
-
PATH_CACHE.clear();
|
|
409
|
-
_pathCacheOps = 0;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
function parsePathUncached(d) {
|
|
413
|
-
const ops = [];
|
|
414
|
-
const tokens = tokenizePath(d);
|
|
415
|
-
let currentX = 0;
|
|
416
|
-
let currentY = 0;
|
|
417
|
-
let subpathStartX = 0;
|
|
418
|
-
let subpathStartY = 0; // subpath start
|
|
419
|
-
let previousControlX = 0;
|
|
420
|
-
let previousControlY = 0;
|
|
421
|
-
let previousCommand = '';
|
|
422
|
-
for (const token of tokens) {
|
|
423
|
-
const command = token.command;
|
|
424
|
-
const relative = command >= 'a' && command <= 'z';
|
|
425
|
-
const values = token.values;
|
|
426
|
-
const absoluteCommand = command.toUpperCase();
|
|
427
|
-
let index = 0;
|
|
428
|
-
const resolveX = value => (relative ? currentX + value : value);
|
|
429
|
-
const resolveY = value => (relative ? currentY + value : value);
|
|
430
|
-
if (absoluteCommand === 'M') {
|
|
431
|
-
// First pair is moveto; later pairs are implicit linetos.
|
|
432
|
-
currentX = resolveX(values[index++]);
|
|
433
|
-
currentY = resolveY(values[index++]);
|
|
434
|
-
subpathStartX = currentX;
|
|
435
|
-
subpathStartY = currentY;
|
|
436
|
-
ops.push(VOP_MOVE, currentX, currentY);
|
|
437
|
-
while (index + 1 < values.length) {
|
|
438
|
-
currentX = relative ? currentX + values[index++] : values[index++];
|
|
439
|
-
currentY = relative ? currentY + values[index++] : values[index++];
|
|
440
|
-
ops.push(VOP_LINE, currentX, currentY);
|
|
441
|
-
}
|
|
442
|
-
} else if (absoluteCommand === 'L') {
|
|
443
|
-
while (index + 1 < values.length) {
|
|
444
|
-
currentX = relative ? currentX + values[index++] : values[index++];
|
|
445
|
-
currentY = relative ? currentY + values[index++] : values[index++];
|
|
446
|
-
ops.push(VOP_LINE, currentX, currentY);
|
|
447
|
-
}
|
|
448
|
-
} else if (absoluteCommand === 'H') {
|
|
449
|
-
while (index < values.length) {
|
|
450
|
-
currentX = relative ? currentX + values[index++] : values[index++];
|
|
451
|
-
ops.push(VOP_LINE, currentX, currentY);
|
|
452
|
-
}
|
|
453
|
-
} else if (absoluteCommand === 'V') {
|
|
454
|
-
while (index < values.length) {
|
|
455
|
-
currentY = relative ? currentY + values[index++] : values[index++];
|
|
456
|
-
ops.push(VOP_LINE, currentX, currentY);
|
|
457
|
-
}
|
|
458
|
-
} else if (absoluteCommand === 'C') {
|
|
459
|
-
while (index + 5 < values.length) {
|
|
460
|
-
const control1X = resolveX(values[index++]);
|
|
461
|
-
const control1Y = resolveY(values[index++]);
|
|
462
|
-
const control2X = resolveX(values[index++]);
|
|
463
|
-
const control2Y = resolveY(values[index++]);
|
|
464
|
-
const endX = resolveX(values[index++]);
|
|
465
|
-
const endY = resolveY(values[index++]);
|
|
466
|
-
ops.push(
|
|
467
|
-
VOP_CUBIC,
|
|
468
|
-
control1X,
|
|
469
|
-
control1Y,
|
|
470
|
-
control2X,
|
|
471
|
-
control2Y,
|
|
472
|
-
endX,
|
|
473
|
-
endY,
|
|
474
|
-
);
|
|
475
|
-
previousControlX = control2X;
|
|
476
|
-
previousControlY = control2Y;
|
|
477
|
-
currentX = endX;
|
|
478
|
-
currentY = endY;
|
|
479
|
-
}
|
|
480
|
-
} else if (absoluteCommand === 'S') {
|
|
481
|
-
while (index + 3 < values.length) {
|
|
482
|
-
const reflected = previousCommand === 'C' || previousCommand === 'S';
|
|
483
|
-
const control1X = reflected
|
|
484
|
-
? 2 * currentX - previousControlX
|
|
485
|
-
: currentX;
|
|
486
|
-
const control1Y = reflected
|
|
487
|
-
? 2 * currentY - previousControlY
|
|
488
|
-
: currentY;
|
|
489
|
-
const control2X = resolveX(values[index++]);
|
|
490
|
-
const control2Y = resolveY(values[index++]);
|
|
491
|
-
const endX = resolveX(values[index++]);
|
|
492
|
-
const endY = resolveY(values[index++]);
|
|
493
|
-
ops.push(
|
|
494
|
-
VOP_CUBIC,
|
|
495
|
-
control1X,
|
|
496
|
-
control1Y,
|
|
497
|
-
control2X,
|
|
498
|
-
control2Y,
|
|
499
|
-
endX,
|
|
500
|
-
endY,
|
|
501
|
-
);
|
|
502
|
-
previousControlX = control2X;
|
|
503
|
-
previousControlY = control2Y;
|
|
504
|
-
currentX = endX;
|
|
505
|
-
currentY = endY;
|
|
506
|
-
}
|
|
507
|
-
} else if (absoluteCommand === 'Q') {
|
|
508
|
-
while (index + 3 < values.length) {
|
|
509
|
-
const controlX = resolveX(values[index++]);
|
|
510
|
-
const controlY = resolveY(values[index++]);
|
|
511
|
-
const endX = resolveX(values[index++]);
|
|
512
|
-
const endY = resolveY(values[index++]);
|
|
513
|
-
ops.push(VOP_QUAD, controlX, controlY, endX, endY);
|
|
514
|
-
previousControlX = controlX;
|
|
515
|
-
previousControlY = controlY;
|
|
516
|
-
currentX = endX;
|
|
517
|
-
currentY = endY;
|
|
518
|
-
}
|
|
519
|
-
} else if (absoluteCommand === 'T') {
|
|
520
|
-
while (index + 1 < values.length) {
|
|
521
|
-
const reflected = previousCommand === 'Q' || previousCommand === 'T';
|
|
522
|
-
const controlX = reflected ? 2 * currentX - previousControlX : currentX;
|
|
523
|
-
const controlY = reflected ? 2 * currentY - previousControlY : currentY;
|
|
524
|
-
const endX = resolveX(values[index++]);
|
|
525
|
-
const endY = resolveY(values[index++]);
|
|
526
|
-
ops.push(VOP_QUAD, controlX, controlY, endX, endY);
|
|
527
|
-
previousControlX = controlX;
|
|
528
|
-
previousControlY = controlY;
|
|
529
|
-
currentX = endX;
|
|
530
|
-
currentY = endY;
|
|
531
|
-
}
|
|
532
|
-
} else if (absoluteCommand === 'A') {
|
|
533
|
-
while (index + 6 < values.length) {
|
|
534
|
-
const radiusX = values[index++];
|
|
535
|
-
const radiusY = values[index++];
|
|
536
|
-
const rotation = values[index++];
|
|
537
|
-
const largeArcFlag = values[index++];
|
|
538
|
-
const sweepFlag = values[index++];
|
|
539
|
-
const endX = resolveX(values[index++]);
|
|
540
|
-
const endY = resolveY(values[index++]);
|
|
541
|
-
const cubics = arcToCubics(
|
|
542
|
-
currentX,
|
|
543
|
-
currentY,
|
|
544
|
-
radiusX,
|
|
545
|
-
radiusY,
|
|
546
|
-
rotation,
|
|
547
|
-
largeArcFlag,
|
|
548
|
-
sweepFlag,
|
|
549
|
-
endX,
|
|
550
|
-
endY,
|
|
551
|
-
);
|
|
552
|
-
for (const cubic of cubics)
|
|
553
|
-
ops.push(
|
|
554
|
-
VOP_CUBIC,
|
|
555
|
-
cubic[0],
|
|
556
|
-
cubic[1],
|
|
557
|
-
cubic[2],
|
|
558
|
-
cubic[3],
|
|
559
|
-
cubic[4],
|
|
560
|
-
cubic[5],
|
|
561
|
-
);
|
|
562
|
-
currentX = endX;
|
|
563
|
-
currentY = endY;
|
|
564
|
-
}
|
|
565
|
-
} else if (absoluteCommand === 'Z') {
|
|
566
|
-
ops.push(VOP_CLOSE);
|
|
567
|
-
currentX = subpathStartX;
|
|
568
|
-
currentY = subpathStartY;
|
|
569
|
-
}
|
|
570
|
-
previousCommand = absoluteCommand;
|
|
571
|
-
}
|
|
572
|
-
return ops;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
// --- Primitive shapes -> path ops ----------------------------------------------------------------
|
|
576
|
-
|
|
577
|
-
function num(value, fallback) {
|
|
578
|
-
const parsed = typeof value === 'number' ? value : parseFloat(value);
|
|
579
|
-
return isNaN(parsed) ? fallback : parsed;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
function circleOps(props) {
|
|
583
|
-
const cx = num(props.cx, 0);
|
|
584
|
-
const cy = num(props.cy, 0);
|
|
585
|
-
const r = num(props.r, 0);
|
|
586
|
-
// Start at angle 0, full circular arc.
|
|
587
|
-
return [
|
|
588
|
-
VOP_MOVE,
|
|
589
|
-
cx + r,
|
|
590
|
-
cy,
|
|
591
|
-
VOP_ARC,
|
|
592
|
-
cx,
|
|
593
|
-
cy,
|
|
594
|
-
r,
|
|
595
|
-
0,
|
|
596
|
-
FULL_TURN_RADIANS,
|
|
597
|
-
0,
|
|
598
|
-
VOP_CLOSE,
|
|
599
|
-
];
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
/** An ellipse as a `d` string: an arc-free path of four cubic Béziers (kappa) once parsePath lowers
|
|
603
|
-
* the two `A` commands. Returned as a string so it goes through the same parse cache as <Path d>. */
|
|
604
|
-
function ellipsePathD(props) {
|
|
605
|
-
const cx = num(props.cx, 0);
|
|
606
|
-
const cy = num(props.cy, 0);
|
|
607
|
-
const rx = num(props.rx, 0);
|
|
608
|
-
const ry = num(props.ry, 0);
|
|
609
|
-
return `M ${cx - rx} ${cy} A ${rx} ${ry} 0 1 0 ${cx + rx} ${cy} A ${rx} ${ry} 0 1 0 ${cx - rx} ${cy} Z`;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* SVG <rect> corner-radius resolution. A missing radius is `auto` and falls back to the other one; a
|
|
614
|
-
* NEGATIVE radius is invalid, which also means `auto`, so it falls back too — <rect rx="-3" ry="8">
|
|
615
|
-
* rounds at 8, matching what browsers render. An explicit ZERO is a valid radius and squares the
|
|
616
|
-
* corners, and each radius is clamped to half its own side. Returns [rx, ry] — [0, 0] for a
|
|
617
|
-
* square-cornered rect. Exported so the AOT and the SVG baker apply the same rules.
|
|
618
|
-
*/
|
|
619
|
-
export function rectRadii(rxAttr, ryAttr, width, height) {
|
|
620
|
-
let rx = num(rxAttr, -1);
|
|
621
|
-
let ry = num(ryAttr, -1);
|
|
622
|
-
if (rx < 0) rx = ry; // rx omitted -> ry
|
|
623
|
-
if (ry < 0) ry = rx; // ry omitted -> rx
|
|
624
|
-
rx = Math.min(rx, width / 2);
|
|
625
|
-
ry = Math.min(ry, height / 2);
|
|
626
|
-
return rx > 0 && ry > 0 ? [rx, ry] : [0, 0];
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
* Rect path ops, clockwise from the top-left corner. rx/ry must already be resolved by rectRadii();
|
|
631
|
-
* 0 gives the plain four-line rectangle. Corners are cubics rather than VOP_ARC so each one STARTS at
|
|
632
|
-
* exactly the preceding line's endpoint — an arc recomputes that point from cos/sin and can miss the
|
|
633
|
-
* engine's exact-equality join dedupe by a ULP, which spikes the miter join on a stroked rect.
|
|
634
|
-
*/
|
|
635
|
-
function rectOpsXY(x, y, width, height, rx, ry) {
|
|
636
|
-
if (rx <= 0 || ry <= 0)
|
|
637
|
-
return [
|
|
638
|
-
VOP_MOVE,
|
|
639
|
-
x,
|
|
640
|
-
y,
|
|
641
|
-
VOP_LINE,
|
|
642
|
-
x + width,
|
|
643
|
-
y,
|
|
644
|
-
VOP_LINE,
|
|
645
|
-
x + width,
|
|
646
|
-
y + height,
|
|
647
|
-
VOP_LINE,
|
|
648
|
-
x,
|
|
649
|
-
y + height,
|
|
650
|
-
VOP_CLOSE,
|
|
651
|
-
];
|
|
652
|
-
const controlOffsetX = rx * KAPPA;
|
|
653
|
-
const controlOffsetY = ry * KAPPA;
|
|
654
|
-
const innerLeft = x + rx; // where the top/bottom edges start, past the left corners
|
|
655
|
-
const innerRight = x + width - rx; // ... and end, before the right corners
|
|
656
|
-
const innerTop = y + ry;
|
|
657
|
-
const innerBottom = y + height - ry;
|
|
658
|
-
const right = x + width;
|
|
659
|
-
const bottom = y + height;
|
|
660
|
-
return [
|
|
661
|
-
VOP_MOVE,
|
|
662
|
-
innerLeft,
|
|
663
|
-
y,
|
|
664
|
-
VOP_LINE,
|
|
665
|
-
innerRight,
|
|
666
|
-
y,
|
|
667
|
-
VOP_CUBIC,
|
|
668
|
-
innerRight + controlOffsetX,
|
|
669
|
-
y,
|
|
670
|
-
right,
|
|
671
|
-
innerTop - controlOffsetY,
|
|
672
|
-
right,
|
|
673
|
-
innerTop,
|
|
674
|
-
VOP_LINE,
|
|
675
|
-
right,
|
|
676
|
-
innerBottom,
|
|
677
|
-
VOP_CUBIC,
|
|
678
|
-
right,
|
|
679
|
-
innerBottom + controlOffsetY,
|
|
680
|
-
innerRight + controlOffsetX,
|
|
681
|
-
bottom,
|
|
682
|
-
innerRight,
|
|
683
|
-
bottom,
|
|
684
|
-
VOP_LINE,
|
|
685
|
-
innerLeft,
|
|
686
|
-
bottom,
|
|
687
|
-
VOP_CUBIC,
|
|
688
|
-
innerLeft - controlOffsetX,
|
|
689
|
-
bottom,
|
|
690
|
-
x,
|
|
691
|
-
innerBottom + controlOffsetY,
|
|
692
|
-
x,
|
|
693
|
-
innerBottom,
|
|
694
|
-
VOP_LINE,
|
|
695
|
-
x,
|
|
696
|
-
innerTop,
|
|
697
|
-
VOP_CUBIC,
|
|
698
|
-
x,
|
|
699
|
-
innerTop - controlOffsetY,
|
|
700
|
-
innerLeft - controlOffsetX,
|
|
701
|
-
y,
|
|
702
|
-
innerLeft,
|
|
703
|
-
y,
|
|
704
|
-
VOP_CLOSE,
|
|
705
|
-
];
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
function rectOps(props) {
|
|
709
|
-
const x = num(props.x, 0);
|
|
710
|
-
const y = num(props.y, 0);
|
|
711
|
-
const width = num(props.width, 0);
|
|
712
|
-
const height = num(props.height, 0);
|
|
713
|
-
const [rx, ry] = rectRadii(props.rx, props.ry, width, height);
|
|
714
|
-
return rectOpsXY(x, y, width, height, rx, ry);
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
function lineOps(props) {
|
|
718
|
-
return [
|
|
719
|
-
VOP_MOVE,
|
|
720
|
-
num(props.x1, 0),
|
|
721
|
-
num(props.y1, 0),
|
|
722
|
-
VOP_LINE,
|
|
723
|
-
num(props.x2, 0),
|
|
724
|
-
num(props.y2, 0),
|
|
725
|
-
];
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
// Arc convenience: angles in DEGREES, clockwise from 12 o'clock (the gauge/dial convention). Emits a
|
|
729
|
-
// native VOP_ARC (no d-string regex / bezier conversion), so it is cheap enough to rebuild every drag
|
|
730
|
-
// frame — the key to fast imperative updates.
|
|
731
|
-
function arcOpsCW(cx, cy, r, startDegrees, endDegrees) {
|
|
732
|
-
// The engine's arc angle runs from +X (cos/sin); top-clockwise => subtract 90°.
|
|
733
|
-
const startRadians =
|
|
734
|
-
((startDegrees - TWELVE_OCLOCK_DEGREES) * Math.PI) / HALF_TURN_DEGREES;
|
|
735
|
-
const endRadians =
|
|
736
|
-
((endDegrees - TWELVE_OCLOCK_DEGREES) * Math.PI) / HALF_TURN_DEGREES;
|
|
737
|
-
return [VOP_ARC, cx, cy, r, startRadians, endRadians, 0];
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
// --- Imperative shapes -> op-tape (the fast path; no JSX, no React) -------------------------------
|
|
741
|
-
|
|
742
|
-
/**
|
|
743
|
-
* Compiles a flat array of primitive shape descriptors into {ops, paints} for NativeUI.setVectorOps.
|
|
744
|
-
* Each descriptor names one primitive plus paint fields, e.g.:
|
|
745
|
-
* { arc: [cx, cy, r, startDeg, endDeg], stroke: '#f4a261', strokeWidth: 14, cap: 'round' }
|
|
746
|
-
* { circle: [cx, cy, r], fill: '#16202f', stroke: '#f4a261', strokeWidth: 3 }
|
|
747
|
-
* { line: [x1, y1, x2, y2], stroke, strokeWidth }
|
|
748
|
-
* { rect: [x, y, width, height], fill } // optional [x, y, width, height, rx, ry] for rounded corners
|
|
749
|
-
* { path: 'M..A..', stroke } // d-string supported but slower (regex + bezier)
|
|
750
|
-
* Arc angles are degrees, clockwise from 12 o'clock. This avoids the d-string parser entirely, so it's
|
|
751
|
-
* cheap enough to call on every pointer move (the imperative drag path).
|
|
752
|
-
*/
|
|
753
|
-
// Reused output buffers — shapesToVector is on the drag hot path and its result is consumed
|
|
754
|
-
// synchronously by NativeUI.setVectorOps, so growing fresh arrays (plus per-shape arrays + spreads)
|
|
755
|
-
// every move is pure GC. Push directly into these instead.
|
|
756
|
-
const _vectorOps = [];
|
|
757
|
-
const _vectorPaints = [];
|
|
758
|
-
const _vectorGradients = [];
|
|
759
|
-
|
|
760
|
-
export function shapesToVector(shapes) {
|
|
761
|
-
const ops = _vectorOps;
|
|
762
|
-
const paints = _vectorPaints;
|
|
763
|
-
const gradients = _vectorGradients;
|
|
764
|
-
ops.length = 0;
|
|
765
|
-
paints.length = 0;
|
|
766
|
-
gradients.length = 0;
|
|
767
|
-
for (let shapeIndex = 0; shapeIndex < shapes.length; shapeIndex++) {
|
|
768
|
-
const shape = shapes[shapeIndex];
|
|
769
|
-
const opStart = ops.length;
|
|
770
|
-
const paintIndex = paints.length / PAINT_STRIDE;
|
|
771
|
-
ops.push(VOP_SHAPE, paintIndex);
|
|
772
|
-
if (shape.arc) {
|
|
773
|
-
const cx = shape.arc[0];
|
|
774
|
-
const cy = shape.arc[1];
|
|
775
|
-
const r = shape.arc[2];
|
|
776
|
-
const startRadians =
|
|
777
|
-
((shape.arc[3] - TWELVE_OCLOCK_DEGREES) * Math.PI) / HALF_TURN_DEGREES;
|
|
778
|
-
const endRadians =
|
|
779
|
-
((shape.arc[4] - TWELVE_OCLOCK_DEGREES) * Math.PI) / HALF_TURN_DEGREES;
|
|
780
|
-
ops.push(VOP_ARC, cx, cy, r, startRadians, endRadians, 0);
|
|
781
|
-
} else if (shape.circle) {
|
|
782
|
-
const cx = shape.circle[0];
|
|
783
|
-
const cy = shape.circle[1];
|
|
784
|
-
const r = shape.circle[2];
|
|
785
|
-
ops.push(
|
|
786
|
-
VOP_MOVE,
|
|
787
|
-
cx + r,
|
|
788
|
-
cy,
|
|
789
|
-
VOP_ARC,
|
|
790
|
-
cx,
|
|
791
|
-
cy,
|
|
792
|
-
r,
|
|
793
|
-
0,
|
|
794
|
-
FULL_TURN_RADIANS,
|
|
795
|
-
0,
|
|
796
|
-
VOP_CLOSE,
|
|
797
|
-
);
|
|
798
|
-
} else if (shape.rect) {
|
|
799
|
-
const x = shape.rect[0];
|
|
800
|
-
const y = shape.rect[1];
|
|
801
|
-
const width = shape.rect[2];
|
|
802
|
-
const height = shape.rect[3];
|
|
803
|
-
if (shape.rect.length > 4) {
|
|
804
|
-
// Rounded — the optional trailing [rx, ry]. Off the allocation-free path below, but a rounded
|
|
805
|
-
// rect is a chrome/backing shape, not something a drag rebuilds every move.
|
|
806
|
-
const [rx, ry] = rectRadii(shape.rect[4], shape.rect[5], width, height);
|
|
807
|
-
const roundedOps = rectOpsXY(x, y, width, height, rx, ry);
|
|
808
|
-
for (let index = 0; index < roundedOps.length; index++)
|
|
809
|
-
ops.push(roundedOps[index]);
|
|
810
|
-
} else {
|
|
811
|
-
ops.push(
|
|
812
|
-
VOP_MOVE,
|
|
813
|
-
x,
|
|
814
|
-
y,
|
|
815
|
-
VOP_LINE,
|
|
816
|
-
x + width,
|
|
817
|
-
y,
|
|
818
|
-
VOP_LINE,
|
|
819
|
-
x + width,
|
|
820
|
-
y + height,
|
|
821
|
-
VOP_LINE,
|
|
822
|
-
x,
|
|
823
|
-
y + height,
|
|
824
|
-
VOP_CLOSE,
|
|
825
|
-
);
|
|
826
|
-
}
|
|
827
|
-
} else if (shape.line) {
|
|
828
|
-
ops.push(
|
|
829
|
-
VOP_MOVE,
|
|
830
|
-
shape.line[0],
|
|
831
|
-
shape.line[1],
|
|
832
|
-
VOP_LINE,
|
|
833
|
-
shape.line[2],
|
|
834
|
-
shape.line[3],
|
|
835
|
-
);
|
|
836
|
-
} else if (shape.path) {
|
|
837
|
-
const pathOps = parsePath(shape.path);
|
|
838
|
-
for (let index = 0; index < pathOps.length; index++)
|
|
839
|
-
ops.push(pathOps[index]);
|
|
840
|
-
}
|
|
841
|
-
if (ops.length <= opStart + SHAPE_HEADER_LENGTH) {
|
|
842
|
-
ops.length = opStart; // nothing emitted — roll back the SHAPE header
|
|
843
|
-
continue;
|
|
844
|
-
}
|
|
845
|
-
// Optional gradients. `paint.fill_gradient` / `stroke_gradient` are 1-BASED indices into the gradient table
|
|
846
|
-
// (0 = solid), which is what lets one shape carry a smooth ramp instead of being chopped into many
|
|
847
|
-
// solid pieces. Stop colors may be strings here; the engine wants packed ARGB.
|
|
848
|
-
paints.push(
|
|
849
|
-
parseColor(shape.fill),
|
|
850
|
-
parseColor(shape.stroke),
|
|
851
|
-
num(shape.strokeWidth, DEFAULT_STROKE_WIDTH),
|
|
852
|
-
num(shape.miter, DEFAULT_MITER_LIMIT),
|
|
853
|
-
CAP[shape.cap] ?? CAP.butt,
|
|
854
|
-
JOIN[shape.join] ?? JOIN.miter,
|
|
855
|
-
shape.fillRule === 'evenodd' ? FILL_RULE_EVEN_ODD : FILL_RULE_NONZERO,
|
|
856
|
-
shape.fillGradient ? pushGrad(gradients, shape.fillGradient) : 0,
|
|
857
|
-
shape.strokeGradient ? pushGrad(gradients, shape.strokeGradient) : 0,
|
|
858
|
-
);
|
|
859
|
-
}
|
|
860
|
-
return {ops, paints, gradients};
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
/** Appends a gradient descriptor (normalizing string stop colours) and returns its 1-based index. */
|
|
864
|
-
function pushGrad(gradients, gradient) {
|
|
865
|
-
gradients.push({
|
|
866
|
-
type: gradient.type,
|
|
867
|
-
ax: gradient.ax,
|
|
868
|
-
ay: gradient.ay,
|
|
869
|
-
bx: gradient.bx,
|
|
870
|
-
by: gradient.by,
|
|
871
|
-
r: gradient.r,
|
|
872
|
-
stops: (gradient.stops || []).map(stop => ({
|
|
873
|
-
color:
|
|
874
|
-
typeof stop.color === 'string' ? parseColor(stop.color) : stop.color,
|
|
875
|
-
offset: stop.offset,
|
|
876
|
-
})),
|
|
877
|
-
});
|
|
878
|
-
return gradients.length;
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
// --- Flatten an <Svg> subtree --------------------------------------------------------------------
|
|
882
|
-
|
|
883
|
-
const PAINT_DEFAULT = {
|
|
884
|
-
fill: 'black',
|
|
885
|
-
stroke: 'none',
|
|
886
|
-
strokeWidth: DEFAULT_STROKE_WIDTH,
|
|
887
|
-
strokeLinecap: 'butt',
|
|
888
|
-
strokeLinejoin: 'miter',
|
|
889
|
-
strokeMiterlimit: DEFAULT_MITER_LIMIT,
|
|
890
|
-
fillRule: 'nonzero',
|
|
891
|
-
fillGradient: null,
|
|
892
|
-
strokeGradient: null,
|
|
893
|
-
};
|
|
894
|
-
|
|
895
|
-
/** The props that make up a shape's paint; a <G> or the <Svg> itself passes them down. */
|
|
896
|
-
export const PAINT_PROPS = Object.keys(PAINT_DEFAULT);
|
|
897
|
-
|
|
898
|
-
function mergePaint(base, props) {
|
|
899
|
-
const merged = {...base};
|
|
900
|
-
for (const key of PAINT_PROPS)
|
|
901
|
-
if (props[key] != null) merged[key] = props[key];
|
|
902
|
-
return merged;
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
/** Maps the resolved paint to the PAINT_STRIDE-number record [fill, stroke, strokeWidth, miter, cap, join, rule,
|
|
906
|
-
* fillGradient, strokeGradient]. Any gradient descriptor is appended to `gradients` and referenced by its
|
|
907
|
-
* 1-BASED index (0 = solid), the same encoding shapesToVector uses. */
|
|
908
|
-
function paintRecord(paint, scale, gradients) {
|
|
909
|
-
return [
|
|
910
|
-
parseColor(paint.fill),
|
|
911
|
-
parseColor(paint.stroke),
|
|
912
|
-
num(paint.strokeWidth, DEFAULT_STROKE_WIDTH) * scale,
|
|
913
|
-
num(paint.strokeMiterlimit, DEFAULT_MITER_LIMIT),
|
|
914
|
-
CAP[paint.strokeLinecap] ?? CAP.butt,
|
|
915
|
-
JOIN[paint.strokeLinejoin] ?? JOIN.miter,
|
|
916
|
-
paint.fillRule === 'evenodd' ? FILL_RULE_EVEN_ODD : FILL_RULE_NONZERO,
|
|
917
|
-
paint.fillGradient && gradients
|
|
918
|
-
? pushGrad(gradients, paint.fillGradient)
|
|
919
|
-
: 0,
|
|
920
|
-
paint.strokeGradient && gradients
|
|
921
|
-
? pushGrad(gradients, paint.strokeGradient)
|
|
922
|
-
: 0,
|
|
923
|
-
];
|
|
924
|
-
}
|
|
925
|
-
|
|
926
|
-
/** Applies a {scaleX, scaleY, translateX, translateY} transform to a path-op array (no leading SHAPE op). */
|
|
927
|
-
function transformOps(ops, transform) {
|
|
928
|
-
const transformed = [];
|
|
929
|
-
let index = 0;
|
|
930
|
-
const mapX = coordinate =>
|
|
931
|
-
coordinate * transform.scaleX + transform.translateX;
|
|
932
|
-
const mapY = coordinate =>
|
|
933
|
-
coordinate * transform.scaleY + transform.translateY;
|
|
934
|
-
while (index < ops.length) {
|
|
935
|
-
const opcode = ops[index++];
|
|
936
|
-
transformed.push(opcode);
|
|
937
|
-
if (opcode === VOP_MOVE || opcode === VOP_LINE) {
|
|
938
|
-
transformed.push(mapX(ops[index++]), mapY(ops[index++]));
|
|
939
|
-
} else if (opcode === VOP_QUAD) {
|
|
940
|
-
transformed.push(
|
|
941
|
-
mapX(ops[index++]),
|
|
942
|
-
mapY(ops[index++]),
|
|
943
|
-
mapX(ops[index++]),
|
|
944
|
-
mapY(ops[index++]),
|
|
945
|
-
);
|
|
946
|
-
} else if (opcode === VOP_CUBIC) {
|
|
947
|
-
transformed.push(
|
|
948
|
-
mapX(ops[index++]),
|
|
949
|
-
mapY(ops[index++]),
|
|
950
|
-
mapX(ops[index++]),
|
|
951
|
-
mapY(ops[index++]),
|
|
952
|
-
mapX(ops[index++]),
|
|
953
|
-
mapY(ops[index++]),
|
|
954
|
-
);
|
|
955
|
-
} else if (opcode === VOP_ARC) {
|
|
956
|
-
// center + radius scale (uniform scale assumed for arcs)
|
|
957
|
-
transformed.push(
|
|
958
|
-
mapX(ops[index++]),
|
|
959
|
-
mapY(ops[index++]),
|
|
960
|
-
ops[index++] * transform.scaleX,
|
|
961
|
-
ops[index++],
|
|
962
|
-
ops[index++],
|
|
963
|
-
ops[index++],
|
|
964
|
-
);
|
|
965
|
-
}
|
|
966
|
-
// VOP_CLOSE has no args
|
|
967
|
-
}
|
|
968
|
-
return transformed;
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
/**
|
|
972
|
-
* transformOps against a cached path, reusing the last result when the transform is unchanged. One
|
|
973
|
-
* slot per path: a shape's transform is the root viewBox scale composed with its <G>s, which almost
|
|
974
|
-
* never moves between commits, so an untouched shape costs a compare instead of a rebuild. The same
|
|
975
|
-
* `d` drawn under several different transforms just misses and pays the full transform.
|
|
976
|
-
*/
|
|
977
|
-
function transformCached(entry, transform) {
|
|
978
|
-
const lastTransform = entry.lastTransform;
|
|
979
|
-
if (
|
|
980
|
-
lastTransform &&
|
|
981
|
-
lastTransform.scaleX === transform.scaleX &&
|
|
982
|
-
lastTransform.scaleY === transform.scaleY &&
|
|
983
|
-
lastTransform.translateX === transform.translateX &&
|
|
984
|
-
lastTransform.translateY === transform.translateY
|
|
985
|
-
)
|
|
986
|
-
return lastTransform.ops;
|
|
987
|
-
const transformedOps = transformOps(entry.ops, transform);
|
|
988
|
-
entry.lastTransform = {
|
|
989
|
-
scaleX: transform.scaleX,
|
|
990
|
-
scaleY: transform.scaleY,
|
|
991
|
-
translateX: transform.translateX,
|
|
992
|
-
translateY: transform.translateY,
|
|
993
|
-
ops: transformedOps,
|
|
994
|
-
};
|
|
995
|
-
return transformedOps;
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
function isElement(child) {
|
|
999
|
-
return (
|
|
1000
|
-
child &&
|
|
1001
|
-
typeof child === 'object' &&
|
|
1002
|
-
child.type != null &&
|
|
1003
|
-
child.props != null
|
|
1004
|
-
);
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
// React's fragment tag. Read from the global symbol registry rather than importing React, which this
|
|
1008
|
-
// module deliberately does not depend on; `Symbol.for` returns the very symbol React itself uses.
|
|
1009
|
-
const REACT_FRAGMENT = Symbol.for('react.fragment');
|
|
1010
|
-
|
|
1011
|
-
/** Children JSX produces for "draw nothing": a bare `{cond && <Path/>}` that failed, a hole in a
|
|
1012
|
-
* list, and the whitespace JSX leaves between elements. Skipped without a word. */
|
|
1013
|
-
function isEmptyChild(child) {
|
|
1014
|
-
return (
|
|
1015
|
-
child == null ||
|
|
1016
|
-
typeof child === 'boolean' ||
|
|
1017
|
-
(typeof child === 'string' && child.trim() === '')
|
|
1018
|
-
);
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
// Shape/grouping tags flattenSvg knows. A tag outside this set is a mistake worth reporting; a known
|
|
1022
|
-
// tag that produces no geometry (a <Path> with no `d`, a zero-radius <Circle>) is not.
|
|
1023
|
-
const SVG_TAGS = ['Path', 'Circle', 'Ellipse', 'Rect', 'Line', 'Arc', 'G'];
|
|
1024
|
-
|
|
1025
|
-
let _warnedSvgChild = false;
|
|
1026
|
-
|
|
1027
|
-
/**
|
|
1028
|
-
* Warns ONCE about a child <Svg> cannot draw. The two common cases are a component (`<Needle/>` —
|
|
1029
|
-
* <Svg> owns its subtree, so React never renders it) and a non-shape host tag; both used to vanish
|
|
1030
|
-
* with no output at all. Once per app, like the other commit-path warnings: an <Svg> recompiles its
|
|
1031
|
-
* tape on every update, and one bad child would otherwise print on every frame.
|
|
1032
|
-
*
|
|
1033
|
-
* The way out of the component case is to CALL it — `{needle()}` puts whatever it returns (an element
|
|
1034
|
-
* or an array of them) straight into children, where the walk sees it. Having it return an array does
|
|
1035
|
-
* nothing for `<Needle/>`: that form is never invoked at all.
|
|
1036
|
-
*/
|
|
1037
|
-
function warnSvgChild(child) {
|
|
1038
|
-
if (_warnedSvgChild) return;
|
|
1039
|
-
_warnedSvgChild = true;
|
|
1040
|
-
const description = !isElement(child)
|
|
1041
|
-
? typeof child === 'string'
|
|
1042
|
-
? `the text ${JSON.stringify(child)}`
|
|
1043
|
-
: `a ${typeof child} child`
|
|
1044
|
-
: typeof child.type === 'function'
|
|
1045
|
-
? `<${child.type.displayName || child.type.name || 'Component'}>, a component`
|
|
1046
|
-
: `<${String(child.type)}>`;
|
|
1047
|
-
console.warn(
|
|
1048
|
-
`embedded-react: <Svg> can't draw ${description}, and skipped it. Children must be ${SVG_TAGS.join('/')} ` +
|
|
1049
|
-
`elements (arrays and fragments around them are unwrapped). <Svg> flattens its subtree instead of ` +
|
|
1050
|
-
`mounting it, so a component is never rendered — inline its shapes, or call it as a plain ` +
|
|
1051
|
-
`function: {needle()}.`,
|
|
1052
|
-
);
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
/**
|
|
1056
|
-
* Flattens an <Svg> element's props into {ops, paints, gradients} flat arrays for NativeUI.setVectorOps.
|
|
1057
|
-
* Handles Path/Circle/Ellipse/Rect/Line shapes, <G> grouping (inherited paint + translate/scale), and
|
|
1058
|
-
* a root viewBox -> width/height scale. Arrays and fragments are unwrapped at any depth, so a mapped
|
|
1059
|
-
* list of shapes draws like an inline one. A shape's fillGradient/strokeGradient descriptor lands in
|
|
1060
|
-
* `gradients` and is referenced 1-based from its paint record. Returns empty arrays when there is
|
|
1061
|
-
* nothing to draw.
|
|
1062
|
-
*/
|
|
1063
|
-
export function flattenSvg(props) {
|
|
1064
|
-
const ops = [];
|
|
1065
|
-
const paints = [];
|
|
1066
|
-
const gradients = [];
|
|
1067
|
-
|
|
1068
|
-
// Root transform from viewBox vs width/height.
|
|
1069
|
-
let root = {scaleX: 1, scaleY: 1, translateX: 0, translateY: 0};
|
|
1070
|
-
if (props.viewBox && props.width && props.height) {
|
|
1071
|
-
const viewBox = String(props.viewBox)
|
|
1072
|
-
.trim()
|
|
1073
|
-
.split(/[\s,]+/)
|
|
1074
|
-
.map(parseFloat);
|
|
1075
|
-
if (viewBox.length === 4 && viewBox[2] > 0 && viewBox[3] > 0) {
|
|
1076
|
-
const scaleX = num(props.width, viewBox[2]) / viewBox[2];
|
|
1077
|
-
const scaleY = num(props.height, viewBox[3]) / viewBox[3];
|
|
1078
|
-
root = {
|
|
1079
|
-
scaleX: scaleX,
|
|
1080
|
-
scaleY: scaleY,
|
|
1081
|
-
translateX: -viewBox[0] * scaleX,
|
|
1082
|
-
translateY: -viewBox[1] * scaleY,
|
|
1083
|
-
};
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
|
|
1087
|
-
// One child at a time, recursing through the wrappers, JSX inserts around shapes: an array (every
|
|
1088
|
-
// `{shapes.map(...)}`, and arrays of arrays when those nest) and a fragment, either of which can
|
|
1089
|
-
// sit anywhere a shape can. Both are transparent — they carry no paint and no transform.
|
|
1090
|
-
const walk = (child, paint, transform) => {
|
|
1091
|
-
if (isEmptyChild(child)) return;
|
|
1092
|
-
if (Array.isArray(child)) {
|
|
1093
|
-
for (const item of child) walk(item, paint, transform);
|
|
1094
|
-
return;
|
|
1095
|
-
}
|
|
1096
|
-
if (!isElement(child)) return warnSvgChild(child);
|
|
1097
|
-
const childProps = child.props || {};
|
|
1098
|
-
if (child.type === REACT_FRAGMENT)
|
|
1099
|
-
return walk(childProps.children, paint, transform);
|
|
1100
|
-
const merged = mergePaint(paint, childProps);
|
|
1101
|
-
if (child.type === 'G') {
|
|
1102
|
-
// Compose a translate/scale for the group, then recurse.
|
|
1103
|
-
const groupScale = num(childProps.scale, 1);
|
|
1104
|
-
const groupX = num(childProps.x ?? childProps.translateX, 0);
|
|
1105
|
-
const groupY = num(childProps.y ?? childProps.translateY, 0);
|
|
1106
|
-
const childTransform = {
|
|
1107
|
-
scaleX: transform.scaleX * groupScale,
|
|
1108
|
-
scaleY: transform.scaleY * groupScale,
|
|
1109
|
-
translateX: groupX * transform.scaleX + transform.translateX,
|
|
1110
|
-
translateY: groupY * transform.scaleY + transform.translateY,
|
|
1111
|
-
};
|
|
1112
|
-
return walk(childProps.children, merged, childTransform);
|
|
1113
|
-
}
|
|
1114
|
-
if (!SVG_TAGS.includes(child.type)) return warnSvgChild(child);
|
|
1115
|
-
|
|
1116
|
-
// Path/Ellipse geometry comes from a `d` string and is cached (parse + transform) by that string;
|
|
1117
|
-
// the primitives build a fixed handful of numbers, which is cheaper than any cache lookup.
|
|
1118
|
-
let entry = null;
|
|
1119
|
-
let shapeOps = null;
|
|
1120
|
-
if (child.type === 'Path' && childProps.d)
|
|
1121
|
-
entry = pathEntry(childProps.d); // pathEntry coerces the key
|
|
1122
|
-
else if (child.type === 'Ellipse')
|
|
1123
|
-
entry = pathEntry(ellipsePathD(childProps));
|
|
1124
|
-
else if (child.type === 'Circle') shapeOps = circleOps(childProps);
|
|
1125
|
-
else if (child.type === 'Rect') shapeOps = rectOps(childProps);
|
|
1126
|
-
else if (child.type === 'Line') shapeOps = lineOps(childProps);
|
|
1127
|
-
else if (child.type === 'Arc')
|
|
1128
|
-
shapeOps = arcOpsCW(
|
|
1129
|
-
num(childProps.cx, 0),
|
|
1130
|
-
num(childProps.cy, 0),
|
|
1131
|
-
num(childProps.r, 0),
|
|
1132
|
-
num(childProps.startAngle, 0),
|
|
1133
|
-
num(childProps.endAngle, 0),
|
|
1134
|
-
);
|
|
1135
|
-
const baseOps = entry ? entry.ops : shapeOps;
|
|
1136
|
-
if (!baseOps || baseOps.length === 0) return;
|
|
1137
|
-
|
|
1138
|
-
const shapeTape = entry
|
|
1139
|
-
? transformCached(entry, transform)
|
|
1140
|
-
: transformOps(baseOps, transform);
|
|
1141
|
-
const paintIndex = paints.length / PAINT_STRIDE;
|
|
1142
|
-
const scale = (transform.scaleX + transform.scaleY) / 2; // stroke-width scale (uniform assumed)
|
|
1143
|
-
paints.push(...paintRecord(merged, scale, gradients));
|
|
1144
|
-
// Appended one at a time: `shapeTape` can be a long cached tape, and spreading it into push() both
|
|
1145
|
-
// copies it into an argument list and risks the call-argument limit.
|
|
1146
|
-
ops.push(VOP_SHAPE, paintIndex);
|
|
1147
|
-
for (let index = 0; index < shapeTape.length; index++)
|
|
1148
|
-
ops.push(shapeTape[index]);
|
|
1149
|
-
};
|
|
1150
|
-
|
|
1151
|
-
// The <Svg>'s own paint cascades to every shape, like a <G> wrapping them all.
|
|
1152
|
-
walk(props.children, mergePaint(PAINT_DEFAULT, props), root);
|
|
1153
|
-
return {ops, paints, gradients};
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
// --- Imported SVG artifacts (<Svg source>) -------------------------------------------------------
|
|
1157
|
-
|
|
1158
|
-
/** Scales a full op-tape (with SHAPE headers) by (scaleX, scaleY): coordinates only, paint indices untouched. */
|
|
1159
|
-
function scaleTape(ops, scaleX, scaleY) {
|
|
1160
|
-
const scaled = [];
|
|
1161
|
-
let index = 0;
|
|
1162
|
-
while (index < ops.length) {
|
|
1163
|
-
const opcode = ops[index++];
|
|
1164
|
-
scaled.push(opcode);
|
|
1165
|
-
if (opcode === VOP_SHAPE)
|
|
1166
|
-
scaled.push(ops[index++]); // paint index — copy, do not scale
|
|
1167
|
-
else if (opcode === VOP_MOVE || opcode === VOP_LINE)
|
|
1168
|
-
scaled.push(ops[index++] * scaleX, ops[index++] * scaleY);
|
|
1169
|
-
else if (opcode === VOP_QUAD)
|
|
1170
|
-
scaled.push(
|
|
1171
|
-
ops[index++] * scaleX,
|
|
1172
|
-
ops[index++] * scaleY,
|
|
1173
|
-
ops[index++] * scaleX,
|
|
1174
|
-
ops[index++] * scaleY,
|
|
1175
|
-
);
|
|
1176
|
-
else if (opcode === VOP_CUBIC)
|
|
1177
|
-
scaled.push(
|
|
1178
|
-
ops[index++] * scaleX,
|
|
1179
|
-
ops[index++] * scaleY,
|
|
1180
|
-
ops[index++] * scaleX,
|
|
1181
|
-
ops[index++] * scaleY,
|
|
1182
|
-
ops[index++] * scaleX,
|
|
1183
|
-
ops[index++] * scaleY,
|
|
1184
|
-
);
|
|
1185
|
-
else if (opcode === VOP_ARC)
|
|
1186
|
-
scaled.push(
|
|
1187
|
-
ops[index++] * scaleX,
|
|
1188
|
-
ops[index++] * scaleY,
|
|
1189
|
-
ops[index++] * scaleX,
|
|
1190
|
-
ops[index++],
|
|
1191
|
-
ops[index++],
|
|
1192
|
-
ops[index++],
|
|
1193
|
-
);
|
|
1194
|
-
// VOP_CLOSE has no args.
|
|
1195
|
-
}
|
|
1196
|
-
return scaled;
|
|
1197
|
-
}
|
|
1198
|
-
|
|
1199
|
-
/**
|
|
1200
|
-
* Scales an imported vector artifact ({ops, paints, width, height} from the .svg baker) from its intrinsic
|
|
1201
|
-
* size to a target box, also scaling stroke widths. Returns the original arrays untouched when no scaling
|
|
1202
|
-
* is needed (the common <Svg source> case where the box equals the intrinsic size).
|
|
1203
|
-
*/
|
|
1204
|
-
export function scaleVectorArtifact(artifact, targetWidth, targetHeight) {
|
|
1205
|
-
const scaleX = artifact.width ? targetWidth / artifact.width : 1;
|
|
1206
|
-
const scaleY = artifact.height ? targetHeight / artifact.height : 1;
|
|
1207
|
-
const sourceGradients = artifact.gradients || [];
|
|
1208
|
-
if (scaleX === 1 && scaleY === 1)
|
|
1209
|
-
return {
|
|
1210
|
-
ops: artifact.ops,
|
|
1211
|
-
paints: artifact.paints,
|
|
1212
|
-
gradients: sourceGradients,
|
|
1213
|
-
};
|
|
1214
|
-
const strokeScale = Math.sqrt(Math.abs(scaleX * scaleY)) || 1; // uniform stroke-width / radius scale
|
|
1215
|
-
const paints = artifact.paints.slice();
|
|
1216
|
-
for (
|
|
1217
|
-
let index = PAINT_STROKE_WIDTH_SLOT;
|
|
1218
|
-
index < paints.length;
|
|
1219
|
-
index += PAINT_STRIDE
|
|
1220
|
-
)
|
|
1221
|
-
paints[index] *= strokeScale;
|
|
1222
|
-
// Gradient geometry lives in the same coordinate space as the path, so it scales with it (radius uniformly).
|
|
1223
|
-
const gradients = sourceGradients.map(gradient => ({
|
|
1224
|
-
...gradient,
|
|
1225
|
-
ax: (gradient.ax || 0) * scaleX,
|
|
1226
|
-
ay: (gradient.ay || 0) * scaleY,
|
|
1227
|
-
bx: (gradient.bx || 0) * scaleX,
|
|
1228
|
-
by: (gradient.by || 0) * scaleY,
|
|
1229
|
-
// Radial r is a length (scales); conic r is a start angle (invariant under uniform scale) — don't scale it.
|
|
1230
|
-
r:
|
|
1231
|
-
gradient.type === GRAD_CONIC
|
|
1232
|
-
? gradient.r || 0
|
|
1233
|
-
: (gradient.r || 0) * strokeScale,
|
|
1234
|
-
}));
|
|
1235
|
-
return {ops: scaleTape(artifact.ops, scaleX, scaleY), paints, gradients};
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
// --- Bridge-cap diagnostics ----------------------------------------------------------------------
|
|
1239
|
-
|
|
1240
|
-
let _warnedVectorOps = false;
|
|
1241
|
-
let _warnedVectorPaints = false;
|
|
1242
|
-
let _warnedVectorGradients = false;
|
|
1243
|
-
|
|
1244
|
-
/**
|
|
1245
|
-
* Warns (once per kind) when an <Svg>'s compiled geometry exceeds the native bridge's caps and will be
|
|
1246
|
-
* SILENTLY TRUNCATED. The bridge (native_ui_bridge.c) copies the op-tape into a fixed buffer and caps the
|
|
1247
|
-
* paint table; past the cap, geometry/shapes are dropped. The engine has its own pool diagnostics (on
|
|
1248
|
-
* stderr, debug builds) for what happens further in; this surfaces the JS->engine boundary truncation in
|
|
1249
|
-
* the developer's console. Pass the live caps from NativeUI.maxVectorOps / maxVectorPaints / maxVectorGrads
|
|
1250
|
-
* (undefined on an older bridge => no-op).
|
|
1251
|
-
*
|
|
1252
|
-
* @param {number} opsLength Op-tape length (flat float count).
|
|
1253
|
-
* @param {number} paintsLength Paint-table length (PAINT_STRIDE numbers per shape).
|
|
1254
|
-
* @param {number} maxOps Bridge op-tape cap (NativeUI.maxVectorOps).
|
|
1255
|
-
* @param {number} maxPaints Bridge paint cap, in SHAPES (NativeUI.maxVectorPaints).
|
|
1256
|
-
* @param {number} [gradientsLength] Gradient count (number of gradients referenced by the shapes).
|
|
1257
|
-
* @param {number} [maxGradients] Bridge gradient cap (NativeUI.maxVectorGrads).
|
|
1258
|
-
*/
|
|
1259
|
-
export function warnVectorCaps(
|
|
1260
|
-
opsLength,
|
|
1261
|
-
paintsLength,
|
|
1262
|
-
maxOps,
|
|
1263
|
-
maxPaints,
|
|
1264
|
-
gradientsLength,
|
|
1265
|
-
maxGradients,
|
|
1266
|
-
) {
|
|
1267
|
-
if (!_warnedVectorOps && maxOps > 0 && opsLength > maxOps) {
|
|
1268
|
-
_warnedVectorOps = true;
|
|
1269
|
-
console.warn(
|
|
1270
|
-
`embedded-react: an <Svg> op-tape is too long (${opsLength} > ${maxOps} floats) and will be truncated — ` +
|
|
1271
|
-
`the shape gets cut off. Simplify the path (fewer/coarser curves) or split it across <Svg> nodes; ` +
|
|
1272
|
-
`raising the limit needs ERUI_VECTOR_TAPE_MAX.`,
|
|
1273
|
-
);
|
|
1274
|
-
}
|
|
1275
|
-
const shapes = (paintsLength / PAINT_STRIDE) | 0;
|
|
1276
|
-
if (!_warnedVectorPaints && maxPaints > 0 && shapes > maxPaints) {
|
|
1277
|
-
_warnedVectorPaints = true;
|
|
1278
|
-
console.warn(
|
|
1279
|
-
`embedded-react: an <Svg> has ${shapes} shapes (> ${maxPaints}) — the extra shapes won't render. ` +
|
|
1280
|
-
`Split them across multiple <Svg> nodes; raising the limit needs ERUI_VECTOR_PAINTS_MAX.`,
|
|
1281
|
-
);
|
|
1282
|
-
}
|
|
1283
|
-
if (
|
|
1284
|
-
!_warnedVectorGradients &&
|
|
1285
|
-
maxGradients > 0 &&
|
|
1286
|
-
gradientsLength > maxGradients
|
|
1287
|
-
) {
|
|
1288
|
-
_warnedVectorGradients = true;
|
|
1289
|
-
console.warn(
|
|
1290
|
-
`embedded-react: an <Svg> references ${gradientsLength} gradients (> ${maxGradients}) — the extra gradients are ` +
|
|
1291
|
-
`dropped and shapes that use them fall back to solid fills/strokes. Reuse fewer distinct gradients across ` +
|
|
1292
|
-
`shapes; raising the limit needs ERUI_VECTOR_GRADIENTS_MAX.`,
|
|
1293
|
-
);
|
|
1294
|
-
}
|
|
1295
|
-
}
|