xbintsc 0.7.8 → 0.7.9

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 (116) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/cli/help.js +1 -1
  3. package/dist/src/codegen/generator/context.d.ts +4 -3
  4. package/dist/src/codegen/generator/context.js +6 -5
  5. package/dist/src/codegen/generator/context.js.map +1 -1
  6. package/dist/src/codegen/generator/expressions/assignment.js +3 -3
  7. package/dist/src/driver/config.d.ts +1 -1
  8. package/dist/src/driver/options.d.ts +1 -1
  9. package/dist/src/driver/version.d.ts +1 -1
  10. package/dist/src/driver/version.js +1 -1
  11. package/dist/src/extensions/gui/features.d.ts +14 -6
  12. package/dist/src/extensions/gui/features.js +34 -6
  13. package/dist/src/extensions/gui/features.js.map +1 -1
  14. package/dist/src/extensions/gui/index.js +3 -3
  15. package/dist/tests/codegen/llvm.test.js +1 -1
  16. package/dist/tests/e2e/gui-audio.test.js +24 -0
  17. package/dist/tests/e2e/gui-audio.test.js.map +1 -1
  18. package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
  19. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
  20. package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
  21. package/dist/tests/e2e/gui-font-face.test.js +99 -7
  22. package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
  23. package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
  24. package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
  25. package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
  26. package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
  27. package/dist/tests/e2e/gui-initial-url.test.js +49 -0
  28. package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
  29. package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
  30. package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
  31. package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
  32. package/dist/tests/extensions/gui-features.test.js +27 -2
  33. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  34. package/doc/ai/extensions.md +10 -4
  35. package/doc/ai/zh-CN/extensions.md +8 -3
  36. package/doc/gui/api.md +35 -2
  37. package/doc/gui/build.md +34 -15
  38. package/doc/gui/known-issues.md +1 -1
  39. package/doc/gui/rendering.md +45 -8
  40. package/doc/gui/runtime.md +13 -0
  41. package/doc/gui/webgl.md +11 -2
  42. package/doc/gui.md +9 -4
  43. package/doc/implemented/codegen.md +3 -2
  44. package/doc/zh-CN/gui/api.md +25 -2
  45. package/doc/zh-CN/gui/build.md +20 -7
  46. package/doc/zh-CN/gui/known-issues.md +1 -1
  47. package/doc/zh-CN/gui/rendering.md +33 -8
  48. package/doc/zh-CN/gui/runtime.md +9 -0
  49. package/doc/zh-CN/gui/webgl.md +3 -2
  50. package/doc/zh-CN/gui.md +10 -5
  51. package/doc/zh-CN/implemented/codegen.md +3 -2
  52. package/package.json +7 -7
  53. package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
  54. package/runtime/ext_gui/audio_mixer.cpp +84 -163
  55. package/runtime/ext_gui/audio_mixer.h +59 -3
  56. package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
  57. package/runtime/ext_gui/audio_param_apply.cpp +160 -0
  58. package/runtime/ext_gui/browser.cpp +6 -19
  59. package/runtime/ext_gui/browser_location.cpp +78 -0
  60. package/runtime/ext_gui/css_sheet.cpp +4 -2
  61. package/runtime/ext_gui/document_animation.cpp +7 -0
  62. package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
  63. package/runtime/ext_gui/dom_api_webgl.cpp +31 -0
  64. package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
  65. package/runtime/ext_gui/dom_api_webgl_shader.cpp +19 -4
  66. package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -3
  67. package/runtime/ext_gui/gui.cpp +100 -1
  68. package/runtime/ext_gui/gui_events.cpp +8 -2
  69. package/runtime/ext_gui/perf.cpp +18 -6
  70. package/runtime/ext_gui/perf.h +37 -0
  71. package/runtime/ext_gui/style.cpp +27 -10
  72. package/runtime/ext_gui/style.h +10 -1
  73. package/runtime/ext_gui/text.cpp +4 -0
  74. package/runtime/ext_gui/text_font_bytes.cpp +117 -0
  75. package/runtime/ext_gui/text_font_bytes.h +65 -0
  76. package/runtime/ext_gui/text_font_db.cpp +5 -0
  77. package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
  78. package/runtime/ext_gui/text_font_sfnt.h +5 -2
  79. package/runtime/ext_gui/text_hb.cpp +34 -5
  80. package/runtime/ext_gui/text_stb.cpp +7 -30
  81. package/runtime/ext_gui/text_webfont.cpp +217 -0
  82. package/runtime/ext_gui/warnings.cpp +26 -0
  83. package/runtime/ext_gui/warnings.h +8 -0
  84. package/runtime/ext_gui/webfont_bridge.c +23 -0
  85. package/runtime/ext_gui/webgl.h +34 -39
  86. package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
  87. package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
  88. package/runtime/ext_gui/webgl_draw.cpp +72 -12
  89. package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
  90. package/runtime/ext_gui/webgl_draw_state.h +13 -2
  91. package/runtime/ext_gui/webgl_objects.h +4 -0
  92. package/runtime/ext_gui/webgl_program.h +64 -0
  93. package/runtime/ext_gui/webgl_state.cpp +7 -0
  94. package/runtime/ext_gui/webgl_uniform.cpp +78 -0
  95. package/runtime/ext_gui/window.cpp +6 -0
  96. package/runtime/rt_object.h +6 -0
  97. package/runtime/rt_profile.h +6 -0
  98. package/runtime/xt_containers/access.inc +2 -47
  99. package/runtime/xt_containers/ic_get.inc +70 -0
  100. package/runtime/xt_containers/method_cache.inc +24 -10
  101. package/runtime/xt_containers/objects.inc +12 -1
  102. package/runtime/xt_containers/shapes.inc +78 -0
  103. package/runtime/xt_containers.c +1 -0
  104. package/runtime/xt_profile.c +3 -0
  105. package/scripts/build-gui.ts +43 -49
  106. package/scripts/gui-engine-build.ts +58 -0
  107. package/scripts/gui-vendor-source.ts +53 -0
  108. package/scripts/gui-webfont-build.ts +138 -0
  109. package/src/cli/help.ts +1 -1
  110. package/src/codegen/generator/context.ts +6 -6
  111. package/src/codegen/generator/expressions/assignment.ts +3 -3
  112. package/src/driver/config.ts +1 -1
  113. package/src/driver/options.ts +1 -1
  114. package/src/driver/version.ts +1 -1
  115. package/src/extensions/gui/features.ts +34 -6
  116. package/src/extensions/gui/index.ts +3 -3
@@ -403,6 +403,85 @@ extern "C" void xt_gui_pump_events(void) {
403
403
  while (SDL_PollEvent(&event)) handle_event(&event);
404
404
  }
405
405
 
406
+ namespace {
407
+
408
+ /** True while at least one window is still open (the loop's exit condition). */
409
+ bool any_window_open() {
410
+ for (int i = 0; i < g_window_count; i++) {
411
+ if (g_windows[i].open) return true;
412
+ }
413
+ return false;
414
+ }
415
+
416
+ /**
417
+ * The frame period `run()` paces to, in milliseconds; 0 means "uncapped" and
418
+ * restores the old busy loop.
419
+ *
420
+ * `XT_GUI_FRAME_MS` overrides: `0` disables pacing (tests/benchmarks that want
421
+ * the historical behaviour), a positive value fixes the period. Otherwise the
422
+ * loop follows the display refresh rate of the newest open window, falling back
423
+ * to 60 Hz when the backend cannot report a mode. Cached on first use: the
424
+ * period is a property of the session, not of a frame.
425
+ */
426
+ double run_frame_period_ms() {
427
+ static double cached = -1.0;
428
+ if (cached >= 0.0) return cached;
429
+ const char *env = SDL_getenv("XT_GUI_FRAME_MS");
430
+ if (env != NULL && env[0] != '\0') {
431
+ const double requested = SDL_atof(env);
432
+ if (!(requested > 0.0)) {
433
+ cached = 0.0;
434
+ } else if (requested < 1.0) {
435
+ cached = 1.0;
436
+ } else if (requested > 1000.0) {
437
+ cached = 1000.0;
438
+ } else {
439
+ cached = requested;
440
+ }
441
+ return cached;
442
+ }
443
+ float refresh = 0.0f;
444
+ for (int i = g_window_count - 1; i >= 0; i--) {
445
+ if (!g_windows[i].open || g_windows[i].window == NULL) continue;
446
+ const SDL_DisplayMode *mode =
447
+ SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_windows[i].window));
448
+ if (mode != NULL && mode->refresh_rate > refresh) refresh = mode->refresh_rate;
449
+ }
450
+ cached = refresh > 0.0f ? 1000.0 / (double)refresh : 1000.0 / 60.0;
451
+ return cached;
452
+ }
453
+
454
+ /**
455
+ * Block until `deadline_ns`, still dispatching SDL events and servicing the
456
+ * runtime's timers/microtasks.
457
+ *
458
+ * The old loop ran `SDL_Delay(1)` between frames and drained rAF callbacks on
459
+ * every iteration: a page that drives itself ran its logic, WebGL recording and
460
+ * DOM updates hundreds of times per second (and the GPU was asked to render
461
+ * frames the compositor never showed). This wait makes the loop behave like a
462
+ * browser's vsync tick: the page gets one frame per display refresh, and the
463
+ * CPU sleeps instead of spinning. Slices cap the timer/input latency at 4 ms.
464
+ */
465
+ void wait_for_frame(uint64_t deadline_ns) {
466
+ for (;;) {
467
+ if (g_quit || !any_window_open()) return;
468
+ const uint64_t now = SDL_GetTicksNS();
469
+ if (now >= deadline_ns) return;
470
+ uint64_t remaining_ms = (deadline_ns - now) / 1000000ull;
471
+ if (remaining_ms > 4) remaining_ms = 4;
472
+ SDL_Event event;
473
+ if (SDL_WaitEventTimeout(&event, (Sint32)remaining_ms)) {
474
+ handle_event(&event);
475
+ while (SDL_PollEvent(&event)) handle_event(&event);
476
+ }
477
+ xt_loop_poll(0);
478
+ xt_drain_microtasks();
479
+ xt_promise_report_unhandled();
480
+ }
481
+ }
482
+
483
+ } // namespace
484
+
406
485
  extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
407
486
  (void)argc;
408
487
  (void)argv;
@@ -413,7 +492,17 @@ extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
413
492
  deadline = SDL_GetTicks() + (Uint64)SDL_atoi(autoclose);
414
493
  }
415
494
  g_run_active = 1;
495
+ const double period_ms = run_frame_period_ms();
496
+ uint64_t next_frame_ns = 0;
416
497
  while (!g_quit) {
498
+ /* Wait for the display refresh before starting the next frame. The first
499
+ * frame runs immediately. A frame that overran its slot does not make the
500
+ * next one wait: `next_frame_ns` is clamped to now below. */
501
+ if (period_ms > 0.0 && next_frame_ns != 0) {
502
+ wait_for_frame(next_frame_ns);
503
+ if (g_quit) break;
504
+ }
505
+ const uint64_t frame_start_ns = SDL_GetTicksNS();
417
506
  SDL_Event event;
418
507
  int had_event = 0;
419
508
  while (SDL_PollEvent(&event)) {
@@ -438,7 +527,17 @@ extern "C" xt_value xt_gui_run(int32_t argc, xt_value *argv) {
438
527
  xt_promise_report_unhandled();
439
528
 
440
529
  if (deadline != 0 && SDL_GetTicks() >= deadline) break;
441
- if (!had_event) SDL_Delay(1);
530
+ if (period_ms > 0.0) {
531
+ const uint64_t period_ns = (uint64_t)(period_ms * 1000000.0);
532
+ next_frame_ns = frame_start_ns + period_ns;
533
+ const uint64_t now = SDL_GetTicksNS();
534
+ /* Behind schedule: run the next frame now instead of accumulating a
535
+ * growing debt (this is the "as fast as it can" path for a page slower
536
+ * than the refresh rate). */
537
+ if (next_frame_ns < now) next_frame_ns = now;
538
+ } else if (!had_event) {
539
+ SDL_Delay(1);
540
+ }
442
541
  }
443
542
 
444
543
  g_run_active = 0;
@@ -331,8 +331,14 @@ void xt_gui_render_window(XtGuiWindow *win) {
331
331
  * to be presented even when its callback changed nothing visible. */
332
332
  if (!win->animation_frames.empty()) win->present_dirty = 1;
333
333
  /* The timestamp is a DOMHighResTimeStamp: the same clock as
334
- * `performance.now()`, not SDL's machine-uptime tick. */
335
- xt_gui_run_animation_frames(win, xt_monotonic_ms());
334
+ * `performance.now()`, not SDL's machine-uptime tick. This is the page's own
335
+ * code (AOT-compiled `<script>`/rAF callbacks, including any WebGL command
336
+ * recording it does), so it gets its own phase: without it a script-heavy
337
+ * frame looked like unexplained time in `frame`. */
338
+ {
339
+ xtgui::PerfScope scope(&xtgui::g_perf_frame_ms.script);
340
+ xt_gui_run_animation_frames(win, xt_monotonic_ms());
341
+ }
336
342
  /* A callback may have closed the window (directly or through the
337
343
  * auto-close guard): `xt_gui_quit_window` released the document and the GPU
338
344
  * geometry, so there is nothing left to lay out or paint. */
@@ -67,29 +67,38 @@ void addCounters(PerfCounters *target, const PerfCounters &value) {
67
67
  target->webglPasses += value.webglPasses;
68
68
  target->webglPipelineMisses += value.webglPipelineMisses;
69
69
  target->webglBufferUploadBytes += value.webglBufferUploadBytes;
70
+ target->webglSetupMisses += value.webglSetupMisses;
71
+ target->webglSamplerBinds += value.webglSamplerBinds;
72
+ target->webglStateBinds += value.webglStateBinds;
70
73
  }
71
74
 
72
75
  void addTimings(PerfTimings *target, const PerfTimings &value) {
73
76
  target->frame += value.frame;
77
+ target->script += value.script;
74
78
  target->restyle += value.restyle;
75
79
  target->layout += value.layout;
76
80
  target->paint += value.paint;
77
81
  target->upload += value.upload;
78
82
  target->draw += value.draw;
83
+ target->webgl_draw += value.webgl_draw;
84
+ target->webgl_uniform += value.webgl_uniform;
79
85
  }
80
86
 
81
87
  void printLine(uint64_t frames, const PerfTimings &ms, const PerfCounters &counters) {
82
88
  double divisor = frames > 0 ? (double)frames : 1.0;
83
89
  std::fprintf(stderr,
84
- "xt_gui perf frames=%llu ms=%.3f restyle=%.3f layout=%.3f paint=%.3f "
85
- "upload=%.3f draw=%.3f restyles=%llu nodes=%llu rules=%llu media=%llu "
90
+ "xt_gui perf frames=%llu ms=%.3f script=%.3f restyle=%.3f layout=%.3f "
91
+ "paint=%.3f upload=%.3f draw=%.3f gldraw=%.3f gluni=%.3f "
92
+ "restyles=%llu nodes=%llu rules=%llu media=%llu "
86
93
  "layouts=%llu boxes=%llu text=%llu textHit=%llu ops=%llu runs=%llu "
87
94
  "draws=%llu uploads=%llu verts=%llu sync=%llu presents=%llu exposes=%llu "
88
95
  "deferred=%llu starved=%llu skip=%llu "
89
- "cdraw=%llu cups=%llu cbytes=%llu gldraw=%llu passes=%llu gmiss=%llu "
90
- "gbytes=%llu\n",
91
- (unsigned long long)frames, ms.frame / divisor, ms.restyle / divisor,
96
+ "cdraw=%llu cups=%llu cbytes=%llu gldraws=%llu passes=%llu gmiss=%llu "
97
+ "gbytes=%llu setupMiss=%llu samplerBinds=%llu stateBinds=%llu\n",
98
+ (unsigned long long)frames, ms.frame / divisor, ms.script / divisor,
99
+ ms.restyle / divisor,
92
100
  ms.layout / divisor, ms.paint / divisor, ms.upload / divisor, ms.draw / divisor,
101
+ ms.webgl_draw / divisor, ms.webgl_uniform / divisor,
93
102
  (unsigned long long)counters.restyles, (unsigned long long)counters.restyleNodes,
94
103
  (unsigned long long)counters.rulesEvaluated,
95
104
  (unsigned long long)counters.mediaEvaluated, (unsigned long long)counters.layouts,
@@ -109,7 +118,10 @@ void printLine(uint64_t frames, const PerfTimings &ms, const PerfCounters &count
109
118
  (unsigned long long)counters.webglDraws,
110
119
  (unsigned long long)counters.webglPasses,
111
120
  (unsigned long long)counters.webglPipelineMisses,
112
- (unsigned long long)counters.webglBufferUploadBytes);
121
+ (unsigned long long)counters.webglBufferUploadBytes,
122
+ (unsigned long long)counters.webglSetupMisses,
123
+ (unsigned long long)counters.webglSamplerBinds,
124
+ (unsigned long long)counters.webglStateBinds);
113
125
  }
114
126
 
115
127
  } // namespace
@@ -20,6 +20,8 @@
20
20
 
21
21
  #include <cstdint>
22
22
 
23
+ #include <SDL3/SDL.h>
24
+
23
25
  namespace xtgui {
24
26
 
25
27
  /** Counters for one frame; the same layout is reused for cumulative totals. */
@@ -76,16 +78,28 @@ struct PerfCounters {
76
78
  uint64_t webglPasses = 0;
77
79
  uint64_t webglPipelineMisses = 0;
78
80
  uint64_t webglBufferUploadBytes = 0;
81
+ /* Prepared-draw cache misses, sampler-set binds and dynamic-state binds. A
82
+ * steady scene should miss only on the first frame and bind samplers only
83
+ * when the texture/uniform set actually changes. */
84
+ uint64_t webglSetupMisses = 0;
85
+ uint64_t webglSamplerBinds = 0;
86
+ uint64_t webglStateBinds = 0;
79
87
  };
80
88
 
81
89
  /** Wall-clock phase timings in milliseconds. */
82
90
  struct PerfTimings {
83
91
  double frame = 0;
92
+ /** Time spent inside `requestAnimationFrame` callbacks (the page's own AOT
93
+ * code plus any WebGL command recording it does). */
94
+ double script = 0;
84
95
  double restyle = 0;
85
96
  double layout = 0;
86
97
  double paint = 0;
87
98
  double upload = 0;
88
99
  double draw = 0;
100
+ /** WebGL draw recording and uniform uploads; both happen inside `script`. */
101
+ double webgl_draw = 0;
102
+ double webgl_uniform = 0;
89
103
  };
90
104
 
91
105
  /** The frame being recorded; reset by `xt_perf_frame_begin`. */
@@ -122,6 +136,29 @@ class PerfScope {
122
136
  uint64_t start_ = 0;
123
137
  };
124
138
 
139
+ /** A `PerfScope` that only reads the clock while profiling is armed. Use it in
140
+ * paths that run thousands of times per frame (one draw, one uniform call): an
141
+ * always-on pair of `SDL_GetPerformanceCounter` reads would itself be measurable
142
+ * there. `win.stats()` therefore reports these two timings only for a frame
143
+ * captured with `XT_GUI_PERF` set. */
144
+ class PerfScopeActive {
145
+ public:
146
+ explicit PerfScopeActive(double *slot) {
147
+ if (!xt_perf_active()) return;
148
+ slot_ = slot;
149
+ start_ = SDL_GetPerformanceCounter();
150
+ }
151
+ ~PerfScopeActive() {
152
+ if (slot_ != nullptr) *slot_ += xt_perf_ms_since(start_);
153
+ }
154
+ PerfScopeActive(const PerfScopeActive &) = delete;
155
+ PerfScopeActive &operator=(const PerfScopeActive &) = delete;
156
+
157
+ private:
158
+ double *slot_ = nullptr;
159
+ uint64_t start_ = 0;
160
+ };
161
+
125
162
  } // namespace xtgui
126
163
 
127
164
  #endif // XT_GUI_PERF_H
@@ -177,8 +177,10 @@ void inheritFrom(XtStyle *style, const XtStyle *parent) {
177
177
  style->visible = parent->visible;
178
178
  style->pointer_events = parent->pointer_events;
179
179
  /* Most documents define no custom properties; copying the (empty) map for
180
- * every element is pure overhead. */
181
- if (!parent->variables.empty()) style->variables = parent->variables;
180
+ * every element is pure overhead. When they do, the map is shared: an
181
+ * inherited style only bumps a reference count, and a node that declares its
182
+ * own `--x` copies it once. */
183
+ if (parent->variables) style->variables = parent->variables;
182
184
  style->font_size = parent->font_size;
183
185
  style->font_weight = parent->font_weight;
184
186
  style->font_family = parent->font_family;
@@ -205,14 +207,25 @@ void applyCandidates(XtStyle *style, std::vector<Candidate> *candidates,
205
207
  * then available to every normal declaration on this element. */
206
208
  for (const Candidate &candidate : *candidates) {
207
209
  if (isCustomProperty(candidate.declaration->property)) {
208
- style->variables[candidate.declaration->property] = candidate.declaration->value;
210
+ /* Copy-on-write: the inherited map is shared with the parent (and every
211
+ * sibling that inherits it), so the first declaration on this node forks
212
+ * it instead of mutating the parent's set. */
213
+ if (!style->variables || style->variables.use_count() > 1) {
214
+ auto forked = std::make_shared<std::unordered_map<std::string, std::string>>();
215
+ if (style->variables) *forked = *style->variables;
216
+ style->variables = std::move(forked);
217
+ }
218
+ (*style->variables)[candidate.declaration->property] = candidate.declaration->value;
209
219
  }
210
220
  }
221
+ static const std::unordered_map<std::string, std::string> kEmptyVariables;
211
222
  auto resolvedValue = [&](const Candidate &candidate, std::string *out) {
212
223
  *out = candidate.declaration->value;
213
224
  if (out->find("var(") != std::string::npos) {
225
+ const std::unordered_map<std::string, std::string> &variables =
226
+ style->variables ? *style->variables : kEmptyVariables;
214
227
  std::string resolved;
215
- if (!substituteVariables(*out, style->variables, 0, &resolved)) return false;
228
+ if (!substituteVariables(*out, variables, 0, &resolved)) return false;
216
229
  *out = resolved;
217
230
  }
218
231
  return true;
@@ -473,7 +486,14 @@ void computeNode(const Node *node, const XtStyle *parent, const StyleSheet &shee
473
486
  ComputeScratch *scratch) {
474
487
  const XtStyle *childParent = parent;
475
488
  if (node->isElement() && !node->is_pseudo) {
476
- XtStyle style = xt_style_initial();
489
+ /* Compute into the map entry directly. Copying the finished style into
490
+ * `out[node]` duplicated every vector/string/map it owns — on a HUD-sized
491
+ * document that was one full XtStyle copy per element per restyle, for a
492
+ * value that is immediately overwritten again on the next restyle.
493
+ * `unordered_map` references stay valid across rehash (node-based), which
494
+ * is what `box->style` pointers rely on. */
495
+ XtStyle &style = out[node];
496
+ style = xt_style_initial();
477
497
  if (parent != nullptr) {
478
498
  inheritFrom(&style, parent);
479
499
  } else {
@@ -513,11 +533,8 @@ void computeNode(const Node *node, const XtStyle *parent, const StyleSheet &shee
513
533
  if (!style.transitions.empty()) scratch->has_transitions = true;
514
534
  if (!style.animations.empty()) scratch->has_animations = true;
515
535
 
516
- /* `emplace` keeps the address of an existing entry stable, which is what
517
- * `box->style` pointers rely on. */
518
- out[node] = style;
519
- childParent = &out[node];
520
- const XtStyle &computed = out[node];
536
+ childParent = &style;
537
+ const XtStyle &computed = style;
521
538
  if (pseudos != nullptr && sheet.has_pseudo_elements) {
522
539
  computePseudo(node, PseudoElement::Before, computed, sheet, applies, out, context, state,
523
540
  pseudos, scratch);
@@ -247,12 +247,21 @@ struct Edges {
247
247
  void set(Length value) { top = right = bottom = left = value; }
248
248
  };
249
249
 
250
+ /** CSS custom properties (`--name`) an element inherits.
251
+ *
252
+ * The map is shared by every style that inherits the same set: a token-heavy
253
+ * `:root` block (the common design-system pattern) was otherwise deep-copied
254
+ * into every element on every restyle, which dominated the cascade on such
255
+ * pages. A node that declares its own custom property copies the map once
256
+ * (copy-on-write); readers only ever see a const reference. */
257
+ using StyleVariables = std::shared_ptr<std::unordered_map<std::string, std::string>>;
258
+
250
259
  struct XtStyle {
251
260
  Display display = Display::Inline;
252
261
  BoxSizing box_sizing = BoxSizing::ContentBox;
253
262
  /* CSS custom properties (`--name`) are inherited and substituted into the
254
263
  * values of the other declarations during the cascade. */
255
- std::unordered_map<std::string, std::string> variables;
264
+ StyleVariables variables;
256
265
  Position position = Position::Static;
257
266
  Edges margin;
258
267
  /** Which margins were explicitly declared `auto` (the `Edges` default unit is
@@ -11,6 +11,7 @@
11
11
  #include "text_api.h"
12
12
 
13
13
  #include "perf.h"
14
+ #include "text_font_bytes.h"
14
15
  #include "text_fonts.h"
15
16
 
16
17
  namespace xtgui {
@@ -65,6 +66,9 @@ void xt_text_shutdown() {
65
66
  xt_text_glyph_cache_clear();
66
67
  xt_text_api()->shutdown();
67
68
  xt_font_db_shutdown();
69
+ /* The decoded font bytes are shared by the database and the backend faces,
70
+ * so they are the last thing released. */
71
+ xt_font_bytes_shutdown();
68
72
  }
69
73
 
70
74
  } // namespace xtgui
@@ -0,0 +1,117 @@
1
+ /*
2
+ * xbintsc GUI engine — font file loading and webfont decoding (see
3
+ * `text_font_bytes.h`).
4
+ *
5
+ * The process-wide byte cache is keyed by path and holds the *uncompressed*
6
+ * sfnt bytes, so a `.woff2` source is decompressed once and then shared by the
7
+ * metadata reader and whichever text backend draws it. A failed read or decode
8
+ * is cached as null: a page that points at a missing or corrupt font must not
9
+ * retry it on every restyle.
10
+ */
11
+
12
+ #include "text_font_bytes.h"
13
+
14
+ #include <cstdio>
15
+ #include <unordered_map>
16
+
17
+ #include "warnings.h"
18
+
19
+ namespace xtgui {
20
+
21
+ namespace {
22
+
23
+ struct ByteCache {
24
+ std::unordered_map<std::string, std::shared_ptr<const std::vector<unsigned char>>> files;
25
+ /** Whether a path is a WOFF/WOFF2 container (its first four bytes). */
26
+ std::unordered_map<std::string, bool> compressed;
27
+ };
28
+
29
+ ByteCache &byteCache() {
30
+ static ByteCache instance;
31
+ return instance;
32
+ }
33
+
34
+ const WebfontDecoder *g_decoder = nullptr;
35
+
36
+ std::shared_ptr<std::vector<unsigned char>> readFile(const std::string &path) {
37
+ FILE *file = std::fopen(path.c_str(), "rb");
38
+ if (file == nullptr) return nullptr;
39
+ if (std::fseek(file, 0, SEEK_END) != 0) {
40
+ std::fclose(file);
41
+ return nullptr;
42
+ }
43
+ const long length = std::ftell(file);
44
+ if (length <= 0 || std::fseek(file, 0, SEEK_SET) != 0) {
45
+ std::fclose(file);
46
+ return nullptr;
47
+ }
48
+ auto bytes = std::make_shared<std::vector<unsigned char>>((size_t)length);
49
+ const size_t read = std::fread(bytes->data(), 1, bytes->size(), file);
50
+ std::fclose(file);
51
+ if (read != bytes->size()) return nullptr;
52
+ return bytes;
53
+ }
54
+
55
+ } // namespace
56
+
57
+ void xt_font_webfont_install(const WebfontDecoder *decoder) { g_decoder = decoder; }
58
+
59
+ bool xt_font_webfont_available() { return g_decoder != nullptr; }
60
+
61
+ bool xt_font_is_webfont(const unsigned char *data, size_t size) {
62
+ if (data == nullptr || size < 4) return false;
63
+ const uint32_t tag = ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) |
64
+ ((uint32_t)data[2] << 8) | (uint32_t)data[3];
65
+ return tag == kWebfontWoffTag || tag == kWebfontWoff2Tag;
66
+ }
67
+
68
+ bool xt_font_file_is_compressed(const std::string &path) {
69
+ ByteCache &cache = byteCache();
70
+ const auto found = cache.compressed.find(path);
71
+ if (found != cache.compressed.end()) return found->second;
72
+ bool result = false;
73
+ FILE *file = std::fopen(path.c_str(), "rb");
74
+ if (file != nullptr) {
75
+ unsigned char header[4];
76
+ result = std::fread(header, 1, sizeof(header), file) == sizeof(header) &&
77
+ xt_font_is_webfont(header, sizeof(header));
78
+ std::fclose(file);
79
+ }
80
+ cache.compressed.emplace(path, result);
81
+ return result;
82
+ }
83
+
84
+ std::shared_ptr<const std::vector<unsigned char>> xt_font_file_bytes(const std::string &path) {
85
+ ByteCache &cache = byteCache();
86
+ const auto found = cache.files.find(path);
87
+ if (found != cache.files.end()) return found->second;
88
+
89
+ std::shared_ptr<const std::vector<unsigned char>> result;
90
+ const std::shared_ptr<std::vector<unsigned char>> raw = readFile(path);
91
+ if (raw != nullptr) {
92
+ if (xt_font_is_webfont(raw->data(), raw->size())) {
93
+ if (g_decoder == nullptr) {
94
+ xt_warn_webfont_unlinked(path);
95
+ } else {
96
+ auto decoded = std::make_shared<std::vector<unsigned char>>();
97
+ if (g_decoder->decode(*raw, decoded.get()) && !decoded->empty()) {
98
+ result = decoded;
99
+ } else {
100
+ xt_warn_webfont_invalid(path);
101
+ }
102
+ }
103
+ } else {
104
+ result = raw;
105
+ }
106
+ }
107
+ cache.files.emplace(path, result);
108
+ return result;
109
+ }
110
+
111
+ void xt_font_bytes_shutdown() {
112
+ ByteCache &cache = byteCache();
113
+ cache.files.clear();
114
+ cache.compressed.clear();
115
+ }
116
+
117
+ } // namespace xtgui
@@ -0,0 +1,65 @@
1
+ /*
2
+ * xbintsc GUI engine — font file bytes shared by the metadata reader and both
3
+ * text backends.
4
+ *
5
+ * A `.ttf`/`.ttc`/`.otf`/`.otc` file is already sfnt, so its bytes are handed
6
+ * out as read. A `.woff`/`.woff2` file stores the same tables deflated (WOFF) or
7
+ * Brotli-compressed behind a transformed table set (WOFF2), so it is decoded to
8
+ * a plain sfnt by the optional `webfonts` link feature before anyone parses it.
9
+ * The decoder is a function table installed by `webfont_bridge.c`:
10
+ * `text_webfont.cpp` is the only place that references miniz/Brotli/woff2, and
11
+ * without the bridge those members are never extracted from `gui.a`.
12
+ *
13
+ * Every path is decoded at most once per process: the font database parses the
14
+ * same bytes for its CSS metadata that the selected backend loads for outlines.
15
+ */
16
+ #ifndef XT_GUI_TEXT_FONT_BYTES_H
17
+ #define XT_GUI_TEXT_FONT_BYTES_H
18
+
19
+ #include <cstdint>
20
+ #include <memory>
21
+ #include <string>
22
+ #include <vector>
23
+
24
+ namespace xtgui {
25
+
26
+ /** The two compressed container signatures (`'wOFF'`, `'wOF2'`). */
27
+ constexpr uint32_t kWebfontWoffTag = 0x774f4646u;
28
+ constexpr uint32_t kWebfontWoff2Tag = 0x774f4632u;
29
+
30
+ /** The WOFF/WOFF2 decompressor installed by the `webfonts` link feature. */
31
+ struct WebfontDecoder {
32
+ /** Decode a compressed font file into a plain sfnt file in `out`. */
33
+ bool (*decode)(const std::vector<unsigned char> &source,
34
+ std::vector<unsigned char> *out);
35
+ };
36
+
37
+ /** Install the decoder. Called by `webfont_bridge.c`'s constructor; a program
38
+ * built without the feature leaves it null and compressed sources are skipped
39
+ * with a diagnostic. */
40
+ void xt_font_webfont_install(const WebfontDecoder *decoder);
41
+
42
+ /** True when a WOFF/WOFF2 decoder is installed (the `webfonts` feature is
43
+ * linked). Font-directory scans consult it so an undecodable file is not even
44
+ * indexed, and the compressed-source diagnostic stays with the author rule. */
45
+ bool xt_font_webfont_available();
46
+
47
+ /** True when `path` is a WOFF/WOFF2 file. A backend that can stream an sfnt file
48
+ * (FreeType and HarfBuzz both can) uses this to keep a large system font out of
49
+ * memory, while a compressed source always has to be decoded into a buffer. */
50
+ bool xt_font_file_is_compressed(const std::string &path);
51
+
52
+ /** The uncompressed sfnt bytes for `path`, decoded once per process. Returns
53
+ * null when the file cannot be read or a compressed container cannot be
54
+ * decoded; the failure is cached too, so a missing source is reported once. */
55
+ std::shared_ptr<const std::vector<unsigned char>> xt_font_file_bytes(const std::string &path);
56
+
57
+ /** True when `data` starts with a WOFF/WOFF2 signature. */
58
+ bool xt_font_is_webfont(const unsigned char *data, size_t size);
59
+
60
+ /** Drop the cached bytes (after the text backends released their faces). */
61
+ void xt_font_bytes_shutdown();
62
+
63
+ } // namespace xtgui
64
+
65
+ #endif /* XT_GUI_TEXT_FONT_BYTES_H */
@@ -9,6 +9,7 @@
9
9
 
10
10
  #include "text_fonts_internal.h"
11
11
 
12
+ #include "text_font_bytes.h"
12
13
  #include "text_font_paths.h"
13
14
  #include "text_font_sfnt.h"
14
15
 
@@ -130,6 +131,10 @@ bool hasFontExtension(const std::string &path) {
130
131
  const size_t dot = path.find_last_of('.');
131
132
  if (dot == std::string::npos) return false;
132
133
  const std::string ext = lowerAscii(path.substr(dot));
134
+ /* `.woff`/`.woff2` are only indexed when the `webfonts` feature can decode
135
+ * them: without it a scan would report every compressed file it met, while an
136
+ * author rule over an undecodable source still gets its own diagnostic. */
137
+ if (ext == ".woff" || ext == ".woff2") return xt_font_webfont_available();
133
138
  return ext == ".ttf" || ext == ".ttc" || ext == ".otf" || ext == ".otc";
134
139
  }
135
140