@geastack/engine 0.1.5

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.
Files changed (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +29 -0
  3. package/bitmap_font.cpp +165 -0
  4. package/canvas.cpp +4608 -0
  5. package/components/Button.d.ts +4 -0
  6. package/components/Button.tsx +32 -0
  7. package/components/Image.d.ts +3 -0
  8. package/components/Image.tsx +17 -0
  9. package/components/Text.d.ts +3 -0
  10. package/components/Text.tsx +9 -0
  11. package/components/View.d.ts +3 -0
  12. package/components/View.tsx +24 -0
  13. package/components/index.d.ts +8 -0
  14. package/components/index.ts +4 -0
  15. package/css/animation.h +252 -0
  16. package/css/declarative.h +131 -0
  17. package/css/easing.h +160 -0
  18. package/css/engine.h +223 -0
  19. package/css/interpolate.h +38 -0
  20. package/image_store.cpp +807 -0
  21. package/package.json +62 -0
  22. package/rasterized_font.cpp +570 -0
  23. package/touch_runtime.cpp +513 -0
  24. package/ui/absolute_leaf_refresh.cpp +279 -0
  25. package/ui/absolute_leaf_refresh.h +12 -0
  26. package/ui/canvas_element.cpp +2314 -0
  27. package/ui/canvas_element.h +353 -0
  28. package/ui/canvas_store.cpp +188 -0
  29. package/ui/canvas_store.h +59 -0
  30. package/ui/core.cpp +135 -0
  31. package/ui/css_atom.cpp +155 -0
  32. package/ui/css_atom.h +21 -0
  33. package/ui/dirty_regions.cpp +66 -0
  34. package/ui/dirty_regions.h +34 -0
  35. package/ui/display_invalidation.cpp +101 -0
  36. package/ui/display_invalidation.h +15 -0
  37. package/ui/document.cpp +356 -0
  38. package/ui/document.h +101 -0
  39. package/ui/image.h +16 -0
  40. package/ui/image_element.cpp +13 -0
  41. package/ui/image_node.cpp +165 -0
  42. package/ui/input.cpp +936 -0
  43. package/ui/input_render.cpp +213 -0
  44. package/ui/internal.h +567 -0
  45. package/ui/layout.cpp +2025 -0
  46. package/ui/layout_snapshot.cpp +82 -0
  47. package/ui/layout_snapshot.h +12 -0
  48. package/ui/mirror.h +25 -0
  49. package/ui/node.cpp +243 -0
  50. package/ui/node.h +154 -0
  51. package/ui/node_lifecycle.cpp +137 -0
  52. package/ui/node_lifecycle.h +13 -0
  53. package/ui/node_model.h +597 -0
  54. package/ui/refresh_perf.h +235 -0
  55. package/ui/refresh_trace.h +68 -0
  56. package/ui/render.cpp +12097 -0
  57. package/ui/root_scroll_refresh.cpp +859 -0
  58. package/ui/root_scroll_refresh.h +30 -0
  59. package/ui/state_init.h +21 -0
  60. package/ui/style.cpp +14994 -0
  61. package/ui/style.h +727 -0
  62. package/ui/style_values.h +31 -0
  63. package/ui/text.cpp +2010 -0
  64. package/ui/text.h +15 -0
  65. package/ui/text_element.cpp +15 -0
  66. package/ui/tree_events.cpp +621 -0
  67. package/ui/tree_events.h +69 -0
  68. package/ui/tree_internal.h +191 -0
  69. package/ui/tree_nodes.cpp +628 -0
  70. package/ui/tree_render.cpp +3138 -0
  71. package/ui/tree_scroll_mirror.cpp +101 -0
  72. package/ui/tree_state.cpp +574 -0
  73. package/ui/tree_state.h +235 -0
  74. package/ui/tree_style.cpp +698 -0
  75. package/ui/ui.h +9 -0
  76. package/ui/view.cpp +2232 -0
  77. package/ui/view.h +14 -0
  78. package/ui/view_element.cpp +13 -0
  79. package/ui/viewport_region.cpp +36 -0
  80. package/ui/viewport_region.h +12 -0
  81. package/ui/virtual_keyboard.cpp +908 -0
  82. package/ui/virtual_keyboard.h +59 -0
  83. package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
  84. package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
  85. package/vendor/stb/stb_image.h +7988 -0
  86. package/vendor/stb/stb_truetype.h +5079 -0
@@ -0,0 +1,597 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "pixel.h"
5
+
6
+ #include <cstdint>
7
+ #include <cstddef>
8
+ #include <deque>
9
+ #include <string>
10
+
11
+ namespace gea::embedded::ui {
12
+
13
+ // Style colours are stored in this board's native pixel format (pixel::native_t —
14
+ // RGB565 on esp32/geaos, RGBA8888 on iOS). Colours are born native: the CSS parser
15
+ // and geatsc fold both produce native_t, so nothing downstream converts.
16
+ using style_color_t = gea::framework::graphics::pixel::native_t;
17
+
18
+ // Per-node TreeState arrays scale linearly with this: sizeof(TreeState) is
19
+ // ~318 B/node, so 512 nodes = ~159 KB. That is designed to live in PSRAM (see
20
+ // treeState() in tree_state.cpp). A PSRAM-less board — the ESP32-C3 Xteink X3 —
21
+ // cannot host a 159 KB monolith in its largest ~113 KB SRAM bank, so it lowers
22
+ // this via -DGEA_EMBEDDED_MAX_NODES. The define is applied through the target's
23
+ // GEA_EMBEDDED_TARGET_COMPILE_DEFINITIONS, which reaches every framework TU (the
24
+ // framework is compiled per-app), so sizeof(TreeState) stays consistent across
25
+ // the whole link — an inconsistent value here would be silent memory corruption.
26
+ #ifndef GEA_EMBEDDED_MAX_NODES
27
+ #define GEA_EMBEDDED_MAX_NODES 512
28
+ #endif
29
+ inline constexpr int kMaxNodes = GEA_EMBEDDED_MAX_NODES;
30
+ inline constexpr int kUnset = -32768;
31
+ inline constexpr int kScrollDirtyWordCount = (kMaxNodes + 63) / 64;
32
+ inline constexpr int kMaxGridTracks = 8;
33
+ // Layout-time track capacity: IMPLICIT rows (an auto-flowing grid of N
34
+ // children) may far exceed the 8-track template arrays above — a 3-column
35
+ // library of 30 books needs 10 rows. Children past the row capacity were
36
+ // never positioned and rendered at stale coordinates (stacked on row one).
37
+ // Template-declared tracks stay capped at kMaxGridTracks; rows beyond the
38
+ // template are implicit (auto-sized), so only the layout locals grow.
39
+ inline constexpr int kMaxGridLayoutTracks = 96;
40
+
41
+ #ifndef GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES
42
+ #define GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES 0
43
+ #endif
44
+ // Compiler-driven hot-field placement: when the app (per geatsc's static knowledge of
45
+ // which style fields it uses) reads/writes transform/gradient/etc. every frame, set this
46
+ // so the "rare" fields live INLINE in ComputedStyle (a direct load, same cache line —
47
+ // spine's access pattern) instead of behind the rstyle() pool indirection (a call + branch
48
+ // + a separate cache line per node). For a transform+gradient-heavy app like css-3d-cube,
49
+ // the pool indirection is a measured per-node regression on every dirty/dlist/record walk.
50
+ #ifndef GEA_EMBEDDED_RARE_STYLE_INLINE
51
+ #define GEA_EMBEDDED_RARE_STYLE_INLINE 0
52
+ #endif
53
+ inline constexpr int8_t kDisplayBlock = 0;
54
+ inline constexpr int8_t kDisplayNone = 1;
55
+ inline constexpr int8_t kDisplayGrid = 2;
56
+ inline constexpr int8_t kDisplayFlex = 3;
57
+
58
+ enum class NodeType : int8_t {
59
+ View = 0,
60
+ Text = 1,
61
+ Image = 2,
62
+ Canvas = 3,
63
+ Button = 4,
64
+ VirtualList = 5,
65
+ Camera = 6,
66
+ Audio = 7
67
+ };
68
+
69
+ inline bool isViewLikeNodeType(NodeType type)
70
+ {
71
+ return type == NodeType::View || type == NodeType::Button || type == NodeType::VirtualList;
72
+ }
73
+
74
+ inline bool isNativeButtonNodeType(NodeType type)
75
+ {
76
+ return type == NodeType::Button;
77
+ }
78
+
79
+ // Cold/rare style fields. Pooled by default (referenced via ComputedStyle::rare_style),
80
+ // or embedded inline in ComputedStyle when GEA_EMBEDDED_RARE_STYLE_INLINE is set (defined
81
+ // BEFORE ComputedStyle so it can be a by-value member).
82
+ struct RareStyle {
83
+ // --- grid (display:grid track templates) ---
84
+ int8_t grid_column_count = 0;
85
+ int8_t grid_row_count = 0;
86
+ int8_t grid_column_type[kMaxGridTracks] = {};
87
+ int8_t grid_row_type[kMaxGridTracks] = {};
88
+ int16_t grid_column_value[kMaxGridTracks] = {};
89
+ int16_t grid_row_value[kMaxGridTracks] = {};
90
+
91
+ // --- transform / perspective (defaults must match NodeLifecycle::init:
92
+ // scale 1000 = 1.0x, origins 500 = 50% center; the rest 0) ---
93
+ int16_t transform_rotate = 0;
94
+ int16_t transform_rotate_x = 0;
95
+ int16_t transform_rotate_y = 0;
96
+ int16_t transform_translate_x = 0;
97
+ int16_t transform_translate_y = 0;
98
+ int16_t transform_translate_z = 0;
99
+ int16_t transform_translate_x_percent = 0;
100
+ int16_t transform_translate_y_percent = 0;
101
+ int16_t transform_scale_x = 1000;
102
+ int16_t transform_scale_y = 1000;
103
+ int16_t transform_origin_x = 500;
104
+ int16_t transform_origin_y = 500;
105
+ int16_t perspective = 0;
106
+ int16_t perspective_origin_x = 500;
107
+ int16_t perspective_origin_y = 500;
108
+
109
+ // --- filter / box-shadow (all default 0 = no effect) ---
110
+ int16_t filter_blur_radius = 0;
111
+ uint8_t box_shadow_inset = 0;
112
+ int16_t box_shadow_offset_x = 0;
113
+ int16_t box_shadow_offset_y = 0;
114
+ int16_t box_shadow_blur_radius = 0;
115
+ int16_t box_shadow_spread = 0;
116
+ style_color_t box_shadow_color = 0;
117
+ uint8_t box_shadow_alpha = 0;
118
+
119
+ // --- per-side borders (default white/opaque, matching NodeLifecycle::init;
120
+ // only relevant when a side width > 0, which is rare) ---
121
+ int16_t border_side_width[4] = {};
122
+ style_color_t border_side_color[4] = {
123
+ gea::framework::graphics::pixel::nativeColor(255, 255, 255),
124
+ gea::framework::graphics::pixel::nativeColor(255, 255, 255),
125
+ gea::framework::graphics::pixel::nativeColor(255, 255, 255),
126
+ gea::framework::graphics::pixel::nativeColor(255, 255, 255),
127
+ };
128
+
129
+ uint8_t border_side_alpha[4] = {255, 255, 255, 255};
130
+
131
+ // --- linear / overlay / radial gradients + background grid (defaults match
132
+ // NodeLifecycle::init: alphas 255, stops 500/1000, angle 1800, radial
133
+ // center/extent 500/1000) ---
134
+ style_color_t bg_gradient_from_color = 0;
135
+ style_color_t bg_gradient_mid_color = 0;
136
+ style_color_t bg_gradient_to_color = 0;
137
+ uint8_t bg_gradient_from_alpha = 255;
138
+ uint8_t bg_gradient_mid_alpha = 255;
139
+ uint8_t bg_gradient_to_alpha = 255;
140
+ uint16_t bg_gradient_mid_stop = 500;
141
+ uint16_t bg_gradient_to_stop = 1000;
142
+ uint8_t bg_gradient_has_mid = 0;
143
+ int16_t bg_gradient_angle = 1800;
144
+ uint8_t bg_overlay_gradient = 0;
145
+ style_color_t bg_overlay_gradient_from_color = 0;
146
+ style_color_t bg_overlay_gradient_mid_color = 0;
147
+ style_color_t bg_overlay_gradient_to_color = 0;
148
+ uint8_t bg_overlay_gradient_from_alpha = 255;
149
+ uint8_t bg_overlay_gradient_mid_alpha = 255;
150
+ uint8_t bg_overlay_gradient_to_alpha = 255;
151
+ uint16_t bg_overlay_gradient_mid_stop = 500;
152
+ uint16_t bg_overlay_gradient_to_stop = 1000;
153
+ uint8_t bg_overlay_gradient_has_mid = 0;
154
+ int16_t bg_overlay_gradient_angle = 1800;
155
+ uint8_t bg_radial_gradient = 0;
156
+ style_color_t bg_radial_gradient_from_color = 0;
157
+ style_color_t bg_radial_gradient_to_color = 0;
158
+ uint8_t bg_radial_gradient_from_alpha = 255;
159
+ uint8_t bg_radial_gradient_to_alpha = 255;
160
+ uint16_t bg_radial_gradient_stop = 1000;
161
+ int16_t bg_radial_gradient_cx = 500;
162
+ int16_t bg_radial_gradient_cy = 500;
163
+ int16_t bg_radial_gradient_rx = 1000;
164
+ int16_t bg_radial_gradient_ry = 1000;
165
+ uint8_t bg_grid_axes = 0;
166
+ style_color_t bg_grid_color = 0;
167
+ uint8_t bg_grid_alpha = 255;
168
+ uint16_t bg_grid_step_x = 0;
169
+ uint16_t bg_grid_step_y = 0;
170
+ uint8_t bg_grid_line_x = 0;
171
+ uint8_t bg_grid_line_y = 0;
172
+ };
173
+
174
+ struct ComputedStyle {
175
+ int8_t display;
176
+ // True iff the `display` property was explicitly authored (a CSS rule or
177
+ // inline style set it), as opposed to the kDisplayBlock default. Lets the
178
+ // inline-formatting heuristic distinguish an explicit `display:block` on an
179
+ // inline-level tag (e.g. <span style="display:block">, which is block-level
180
+ // and stacks) from a span's default inline behaviour. Mirrors
181
+ // flex_direction_explicit.
182
+ int8_t display_explicit;
183
+ int8_t flex_direction;
184
+ int8_t flex_direction_explicit;
185
+ int8_t flex_wrap;
186
+ int8_t justify_content;
187
+ int8_t align_items;
188
+ int8_t justify_items;
189
+ int8_t align_content;
190
+ int8_t align_self;
191
+ int16_t gap;
192
+
193
+ int16_t width, height;
194
+ int16_t width_percent, height_percent;
195
+ int16_t min_width, min_height;
196
+ int16_t max_width, max_height;
197
+ int16_t flex;
198
+ int16_t flex_shrink;
199
+ // CSS `flex-basis` as a definite main-axis length in px, or kUnset for `auto`
200
+ // (the default — main size comes from content / flex-grow). When definite, the
201
+ // flex item's main-axis size is set to this basis before grow/shrink, so
202
+ // `flex: 0 0 26px` yields a 26px item instead of stretching to fill or sizing
203
+ // to content. Percentage and `auto` bases resolve to kUnset (content-sized).
204
+ int16_t flex_basis;
205
+
206
+ int16_t padding[4];
207
+ int16_t margin[4];
208
+
209
+ int8_t position;
210
+ int16_t pos_offsets[4];
211
+ int16_t pos_offset_percent[4];
212
+ int16_t z_index;
213
+
214
+ style_color_t bg_color;
215
+ int8_t has_bg;
216
+ uint8_t bg_alpha;
217
+ int8_t bg_fill;
218
+ // linear/overlay/radial gradients + background-grid moved to RareStyle (rare).
219
+ style_color_t active_bg_color;
220
+ int8_t has_active_bg;
221
+ style_color_t text_color;
222
+ uint8_t text_alpha;
223
+ uint8_t opacity;
224
+ int16_t blink_interval_ms;
225
+ int32_t blink_started_ms;
226
+ uint8_t blink_visible;
227
+
228
+ int16_t border_width;
229
+ style_color_t border_color;
230
+ uint8_t border_alpha;
231
+ // per-side border width/color/alpha moved to RareStyle (rare); border_radius
232
+ // stays here (rounded rects are common).
233
+ int16_t border_radius[4];
234
+ int16_t border_radius_percent[4];
235
+
236
+ // transform / perspective / filter / box-shadow moved to RareStyle (rare).
237
+
238
+ int16_t font_id;
239
+ int16_t font_size;
240
+ int16_t font_weight;
241
+ int16_t line_height;
242
+ int8_t text_align;
243
+ int8_t overflow;
244
+ int8_t overflow_x;
245
+ int8_t overflow_y;
246
+ int16_t mask_right_fade_width;
247
+ int8_t image_fit;
248
+ // CSS `backface-visibility`: 0 = visible (default), 1 = hidden. When hidden, a
249
+ // transformed face/text whose projected winding points away from the viewer is
250
+ // culled — a closed opaque shape's back faces are occluded anyway, and this skips
251
+ // the wasted overdraw. Default 0 = never culled (lone/visible quads stay drawn).
252
+ int8_t backface_hidden;
253
+ // CSS `text-decoration` — 0 = none (default), 1 = underline, 2 =
254
+ // line-through. The text renderer paints a font-size-relative line at
255
+ // the appropriate y-offset for each decorated run.
256
+ int8_t text_decoration;
257
+ // CSS `text-transform` — 0 = none, 1 = uppercase, 2 = lowercase,
258
+ // 3 = capitalize. Rendering applies this without mutating Node::text.
259
+ int8_t text_transform;
260
+ // CSS `white-space` — 0 = normal (text wraps to fit the content width,
261
+ // the default), 1 = nowrap (the run is laid out as a single line and is
262
+ // never broken on the available width). Inherited, matching CSS. nowrap is
263
+ // the precondition for `text-overflow: ellipsis` to do anything.
264
+ int8_t white_space;
265
+ // CSS `text-overflow` — 0 = clip (default), 1 = ellipsis. Only consulted
266
+ // when the line cannot wrap (white-space: nowrap): an overflowing single
267
+ // line is truncated at a glyph boundary and an ASCII "..." is appended so
268
+ // the visible run fits within the content width. Not inherited.
269
+ int8_t text_overflow;
270
+ // CSS `pointer-events` — 0 = auto (default), 1 = none. A `none` node is never
271
+ // the target of a pointer event and its subtree is skipped in hit-testing, so
272
+ // clicks pass through to whatever is painted behind it (e.g. a decorative
273
+ // overlay image over interactive controls). Default 0 via zero-init.
274
+ int8_t pointer_events;
275
+
276
+ // Handle into the shared RareStyle pool for cold/rarely-used style fields
277
+ // (grid tracks, gradients, transforms, shadows, …) that most nodes never set.
278
+ // -1 = none, so a plain styled node pays 2 bytes instead of inlining ~200 B.
279
+ // Read via rstyle(style), write via rstyleMut(style). Aggregate-friendly: this
280
+ // is the only member with a default initializer; the rest stay zero-init.
281
+ #if GEA_EMBEDDED_RARE_STYLE_INLINE
282
+ // Compiler-driven hot placement: the rare fields live inline (direct load, same cache
283
+ // line — spine's access pattern), no rstyle() pool indirection per node. rare_style is
284
+ // kept but vestigial (always -1) so pool-handle call sites compile as harmless no-ops;
285
+ // the embedded `rare` is copied/freed with the ComputedStyle by struct assignment.
286
+ RareStyle rare;
287
+ int16_t rare_style = -1;
288
+ #else
289
+ int16_t rare_style = -1;
290
+ #endif
291
+ };
292
+
293
+ // (RareStyle is defined above ComputedStyle so it can be embedded inline when
294
+ // GEA_EMBEDDED_RARE_STYLE_INLINE is set; the pool below is used only when it isn't.)
295
+ class RareStylePool {
296
+ public:
297
+ RareStyle &operator[](std::size_t index)
298
+ {
299
+ #if GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES > 0
300
+ if (index < usedSram_) return sram_[index];
301
+ return spill_[index - usedSram_];
302
+ #else
303
+ return spill_[index];
304
+ #endif
305
+ }
306
+
307
+ const RareStyle &operator[](std::size_t index) const
308
+ {
309
+ #if GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES > 0
310
+ if (index < usedSram_) return sram_[index];
311
+ return spill_[index - usedSram_];
312
+ #else
313
+ return spill_[index];
314
+ #endif
315
+ }
316
+
317
+ std::size_t size() const
318
+ {
319
+ #if GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES > 0
320
+ return usedSram_ + spill_.size();
321
+ #else
322
+ return spill_.size();
323
+ #endif
324
+ }
325
+
326
+ void emplace_back()
327
+ {
328
+ #if GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES > 0
329
+ if (usedSram_ < static_cast<std::size_t>(GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES)) {
330
+ sram_[usedSram_++] = RareStyle{};
331
+ return;
332
+ }
333
+ #endif
334
+ spill_.emplace_back();
335
+ }
336
+
337
+ void clear()
338
+ {
339
+ #if GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES > 0
340
+ for (std::size_t i = 0; i < usedSram_; ++i) sram_[i] = RareStyle{};
341
+ usedSram_ = 0;
342
+ #endif
343
+ spill_.clear();
344
+ }
345
+
346
+ private:
347
+ #if GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES > 0
348
+ RareStyle sram_[GEA_EMBEDDED_RARE_STYLE_SRAM_POOL_ENTRIES]{};
349
+ std::size_t usedSram_ = 0;
350
+ #endif
351
+ std::deque<RareStyle> spill_;
352
+ };
353
+
354
+ // RareStyle accessors. rstyle() returns a read-only view (a shared zero default
355
+ // when the node has no RareStyle), so reads work from a const ComputedStyle&
356
+ // with no node id. rstyleMut() allocates a pool entry on first write. The
357
+ // compiler flags every write site (can't assign to the const rstyle() result).
358
+ // The shared cold-style pool. Defined inline HERE (not out-of-line in style.cpp) so
359
+ // the hot read path rstyle() inlines to a branch + pool index at every call site.
360
+ // An out-of-line call per per-frame style-field read (transform projection, reproject
361
+ // patch, dirty scan) was a measured regression on the spinning css-3d-cube: the
362
+ // RareStyle split moved transform/gradient fields off the hot ComputedStyle struct, so
363
+ // every `style.transform_*` read became a function call. The static local is one shared
364
+ // instance across TUs (inline-function rule); rstyleMut/release/reset (style.cpp) use it.
365
+ #if GEA_EMBEDDED_RARE_STYLE_INLINE
366
+ // Inline-embedded rare fields: rstyle() is a direct member access — no pool, no branch, no
367
+ // separate cache line (spine's access pattern). rstyleMut/release/reset are still defined
368
+ // out-of-line in style.cpp (their bodies #if'd) because rstyleMut also invalidates the
369
+ // transform-scan cache, which this header can't reach.
370
+ inline const RareStyle &rstyle(const ComputedStyle &style) { return style.rare; }
371
+ #else
372
+ inline RareStylePool &rareStylePool()
373
+ {
374
+ static RareStylePool pool;
375
+ return pool;
376
+ }
377
+ // Shared zero default for nodes with no RareStyle. Namespace-scope inline (C++17)
378
+ // so the hot rstyle() read has no function-local-static init guard to check per call.
379
+ inline const RareStyle kRareStyleZero{};
380
+ inline const RareStyle &rstyle(const ComputedStyle &style)
381
+ {
382
+ if (style.rare_style < 0) return kRareStyleZero;
383
+ return rareStylePool()[static_cast<std::size_t>(style.rare_style)];
384
+ }
385
+ #endif
386
+ RareStyle &rstyleMut(ComputedStyle &style);
387
+ void releaseRareStyle(int16_t &handle); // frees the entry (or no-op when embedded)
388
+ void resetRareStylePool(); // drops all entries (or no-op when embedded)
389
+
390
+ inline bool isDisplayNone(const ComputedStyle &style)
391
+ {
392
+ return style.display == kDisplayNone;
393
+ }
394
+
395
+ inline bool isDisplayGrid(const ComputedStyle &style)
396
+ {
397
+ return style.display == kDisplayGrid;
398
+ }
399
+
400
+ inline bool usesRowLayout(const ComputedStyle &style)
401
+ {
402
+ // `flex-direction` applies to FLEX CONTAINERS, and to nothing else --
403
+ // CSS Flexbox Level 1 SS5.1 states its "Applies to:" as exactly that. On a
404
+ // block container the property still computes, but it has no effect: block
405
+ // boxes stack in the block direction whatever it says. A browser lays
406
+ // `<div style="flex-direction: row">` out as a column, and so does this.
407
+ //
408
+ // Non-grid/non-none nodes (block included) all run through FlexLayoutPass
409
+ // (LayoutEngine::layoutChildren), which is an implementation detail of how
410
+ // block flow is computed here, not a licence to read a flex property on a
411
+ // box that is not a flex container. Honouring it regardless of `display`
412
+ // meant seven authored sites got a row a browser would never give them;
413
+ // each now says `display: flex`, which is what it always meant.
414
+ if (style.display != kDisplayFlex) return false;
415
+ // Within a flex container the property means what it says, and its initial
416
+ // value is `row` -- so an unstated direction on `display: flex` is a row.
417
+ return style.flex_direction_explicit ? style.flex_direction == 1 : true;
418
+ }
419
+
420
+ inline int8_t aggregateOverflow(int8_t overflowX, int8_t overflowY)
421
+ {
422
+ if (overflowX == 2 || overflowY == 2) return 2;
423
+ if (overflowX != 0 || overflowY != 0) return 1;
424
+ return 0;
425
+ }
426
+
427
+ inline bool scrollsOverflowX(const ComputedStyle &style)
428
+ {
429
+ return style.overflow_x == 2;
430
+ }
431
+
432
+ inline bool scrollsOverflowY(const ComputedStyle &style)
433
+ {
434
+ return style.overflow_y == 2;
435
+ }
436
+
437
+ inline bool hasSideBorder(const ComputedStyle &style)
438
+ {
439
+ if (style.rare_style < 0) return false; // no RareStyle => no per-side borders
440
+ const RareStyle &r = rstyle(style);
441
+ return r.border_side_width[0] > 0 ||
442
+ r.border_side_width[1] > 0 ||
443
+ r.border_side_width[2] > 0 ||
444
+ r.border_side_width[3] > 0;
445
+ }
446
+
447
+ inline bool hasAnyBorder(const ComputedStyle &style)
448
+ {
449
+ return style.border_width > 0 || hasSideBorder(style);
450
+ }
451
+
452
+ struct LayoutBox {
453
+ int16_t x, y;
454
+ int16_t width, height;
455
+
456
+ // Inline formatting: x offset, inside this box's content area, at which the
457
+ // run's FIRST line starts. A text run that begins part-way along a line box
458
+ // (after a <strong>, say) and wraps keeps a full-content-width box so its
459
+ // continuation lines are wrapped and drawn at the block's left edge, while
460
+ // its first line is indented to the pen position it inherited. Zero for
461
+ // every box that starts its own line, which is every box outside an inline
462
+ // formatting row.
463
+ int16_t inline_indent = 0;
464
+
465
+ int16_t previous_x, previous_y;
466
+ int16_t previous_width, previous_height;
467
+
468
+ // Scroll geometry must be 32-bit: a <virtual-list> scrolls over a virtual
469
+ // content height of itemCount * rowHeight (e.g. 5000 * 259 ≈ 1.29M px),
470
+ // which overflows int16_t. Truncation made scroll_content_height wrap
471
+ // negative, so scrollMaxY clamped to 0 and the list refused to scroll.
472
+ int32_t scroll_x;
473
+ int32_t scroll_y;
474
+ int32_t scroll_content_width;
475
+ int32_t scroll_content_height;
476
+ int32_t previous_scroll_x;
477
+ int32_t previous_scroll_y;
478
+
479
+ // Intra-pass layout memo: the flex engine re-measures whole subtrees after
480
+ // every parent resize (repositionChildren -> measureChildren -> layoutNode),
481
+ // visiting nodes ~100x per pass on nested-flex trees. layoutNode() skips a
482
+ // node already laid out THIS pass with the same available box. Two slots
483
+ // (MRU order) because flex items alternate between the parent's measure
484
+ // avail and their basis/grown avail — a single slot thrashes on exactly
485
+ // that pattern. Scoped to a single pass (serial bumped by beginLayoutPass),
486
+ // so no cross-frame invalidation is needed and the final
487
+ // absolute-coordinate resolve is unaffected.
488
+ // result_w/h: the node dims each slot's layout produced. The MRU slot's
489
+ // result is the node's live state, so its hit is unconditional; the older
490
+ // slot's layout has been overwritten in the tree, so its hit is only sound
491
+ // when it produced exactly the current dims (the subtree is then
492
+ // byte-identical — child avails derive from the node's own box).
493
+ int16_t memo_avail_w;
494
+ int16_t memo_avail_h;
495
+ int16_t memo_result_w;
496
+ int16_t memo_result_h;
497
+ uint32_t memo_pass;
498
+ int16_t memo2_avail_w;
499
+ int16_t memo2_avail_h;
500
+ int16_t memo2_result_w;
501
+ int16_t memo2_result_h;
502
+ uint32_t memo2_pass;
503
+ };
504
+
505
+ struct RenderState {
506
+ int16_t previous_transform_rotate;
507
+ int16_t previous_transform_rotate_x;
508
+ int16_t previous_transform_rotate_y;
509
+ int16_t previous_transform_translate_x;
510
+ int16_t previous_transform_translate_y;
511
+ int16_t previous_transform_translate_z;
512
+ int16_t previous_transform_translate_x_percent;
513
+ int16_t previous_transform_translate_y_percent;
514
+ int16_t previous_transform_scale_x;
515
+ int16_t previous_transform_scale_y;
516
+ int16_t previous_transform_origin_x;
517
+ int16_t previous_transform_origin_y;
518
+ int16_t previous_perspective;
519
+ int16_t previous_perspective_origin_x;
520
+ int16_t previous_perspective_origin_y;
521
+ int16_t previous_filter_blur_radius;
522
+
523
+ uint8_t dirty;
524
+ uint8_t scroll_dirty;
525
+ uint8_t non_scroll_dirty;
526
+ // Set alongside `dirty` when the change can affect GEOMETRY (layout
527
+ // property, box-changing text, structural mutation). Paint-only dirt (a
528
+ // background/color class toggle, stable-box text) leaves it clear, letting
529
+ // the refresh skip the relayout pass entirely and letting the scoped
530
+ // relayout compute its LCA over geometry-relevant nodes only. Cleared with
531
+ // `dirty`.
532
+ uint8_t layout_dirty;
533
+ uint8_t transform_dirty;
534
+ uint8_t bg_recolor_pending;
535
+ style_color_t bg_recolor_from;
536
+ style_color_t bg_recolor_to;
537
+ // Tree::setText pre-measures incoming text and sets this flag when the
538
+ // update is safe for the retained/direct-replay path: absolute text may
539
+ // update its measured bbox out of flow, while in-flow text must keep the
540
+ // same layout box (e.g. fixed-width temperature readouts). Cleared on
541
+ // LayoutSnapshot::capture() so it's always either freshly set by setText
542
+ // in the current frame or absent.
543
+ uint8_t text_layout_stable;
544
+ // Glyph-incremental text dirty (set by Tree::setText): when a content-only text
545
+ // change keeps the same layout box and only a middle run of glyphs differs
546
+ // (e.g. a ticking counter "58"->"57", a clock), the dirty collector flushes only
547
+ // [text_dirty_x0, text_dirty_x1] across the node's height instead of the whole
548
+ // box. A 26vmin badge re-render then copies just the changed digits (~one glyph)
549
+ // rather than ~the whole panel width, so it fits a TE-locked frame's slack.
550
+ // Cleared with text_layout_stable each frame (LayoutSnapshot::capture).
551
+ uint8_t text_partial_dirty;
552
+ int16_t text_dirty_x0;
553
+ int16_t text_dirty_x1;
554
+ // This box SHARES a line box with other inline content, so it was placed on
555
+ // that line's baseline (FlexLayoutPass::placeLineItems). TextRenderer::record
556
+ // then draws the run at its baseline instead of centring the string's ink in
557
+ // the line advance: ink centring shifts by however far that particular string's
558
+ // glyphs reach, so runs of different words — or different fonts — on one line
559
+ // end up at different heights. A run that owns its line keeps the centring.
560
+ uint8_t inline_baseline;
561
+ };
562
+
563
+ struct Node {
564
+ NodeType type;
565
+ ComputedStyle style;
566
+
567
+ int16_t parent;
568
+ int16_t first_child, last_child;
569
+ int16_t next_sibling, prev_sibling;
570
+
571
+ // Text content. std::string gives us dynamic sizing without the silent
572
+ // truncation a fixed buffer caused, and on every modern libstdc++ /
573
+ // libc++ build it has small-string optimization — strings up to ~22
574
+ // chars stay inline in the std::string header with no heap allocation,
575
+ // so the common short-label case (button text, counter values) doesn't
576
+ // pay an allocation cost. Apps that put paragraph copy in a single
577
+ // text node (e.g. typography sampler, ~160 chars) get the heap
578
+ // allocation they actually need rather than being chopped to 63 chars.
579
+ std::string text;
580
+ int16_t image_id;
581
+ // Interned tag id (index into the global tag table) rather than a 16-byte
582
+ // inline string — the tag vocabulary is ~15 distinct strings, so this saves
583
+ // 14 B/node. 0 = the empty tag. Use Tree::tagName(node) / tagFromId(tag_id)
584
+ // to read the string, internTag(...) to write.
585
+ int16_t tag_id = 0;
586
+
587
+ LayoutBox layout;
588
+ RenderState render;
589
+
590
+ // Blink-style "rare data" handle: index into the shared NodeRareData pool for
591
+ // the cold/optional per-node state most nodes never have (event listeners, and
592
+ // later attributes/custom-properties/etc.). -1 = none, so the common node pays
593
+ // 2 bytes instead of inlining those tables. Reset to -1 by Node{} / node reuse.
594
+ int16_t rare_data = -1;
595
+ };
596
+
597
+ } // namespace gea::embedded::ui