@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
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@geastack/engine",
3
+ "version": "0.1.5",
4
+ "description": "GeaStack rendering engine, intrinsic JSX primitives, and native engine backends.",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/geastack/core.git",
9
+ "directory": "packages/engine"
10
+ },
11
+ "engines": {
12
+ "node": ">=20.19"
13
+ },
14
+ "type": "module",
15
+ "types": "./components/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./components/index.d.ts",
19
+ "default": "./components/index.ts"
20
+ },
21
+ "./components": {
22
+ "types": "./components/index.d.ts",
23
+ "default": "./components/index.ts"
24
+ },
25
+ "./components/View": {
26
+ "types": "./components/View.d.ts",
27
+ "default": "./components/View.tsx"
28
+ },
29
+ "./components/Text": {
30
+ "types": "./components/Text.d.ts",
31
+ "default": "./components/Text.tsx"
32
+ },
33
+ "./components/Image": {
34
+ "types": "./components/Image.d.ts",
35
+ "default": "./components/Image.tsx"
36
+ },
37
+ "./components/Button": {
38
+ "types": "./components/Button.d.ts",
39
+ "default": "./components/Button.tsx"
40
+ },
41
+ "./package.json": "./package.json",
42
+ "./ui/*": "./ui/*"
43
+ },
44
+ "files": [
45
+ "bitmap_font.cpp",
46
+ "canvas.cpp",
47
+ "components",
48
+ "css",
49
+ "image_store.cpp",
50
+ "rasterized_font.cpp",
51
+ "touch_runtime.cpp",
52
+ "ui",
53
+ "vendor"
54
+ ],
55
+ "publishConfig": {
56
+ "access": "public"
57
+ },
58
+ "sideEffects": false,
59
+ "dependencies": {
60
+ "@geastack/core": "^0.1.0"
61
+ }
62
+ }
@@ -0,0 +1,570 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "graphics/font.h"
3
+
4
+ #ifndef GEA_EMBEDDED_TTF_RUNTIME_FONTS
5
+ #define GEA_EMBEDDED_TTF_RUNTIME_FONTS 0
6
+ #endif
7
+
8
+ #if GEA_EMBEDDED_TTF_RUNTIME_FONTS
9
+ #include "memory.h"
10
+
11
+ #include <algorithm>
12
+ #include <cmath>
13
+ #include <cstdarg>
14
+ #include <cstddef>
15
+ #include <cstdio>
16
+ #include <cstring>
17
+ #include <new>
18
+
19
+ #ifndef GEA_EMBEDDED_TTF_RUNTIME_TRACE
20
+ #define GEA_EMBEDDED_TTF_RUNTIME_TRACE 0
21
+ #endif
22
+
23
+ namespace {
24
+
25
+ void *gea_runtime_stbtt_malloc(std::size_t size, void *)
26
+ {
27
+ if (size == 0) size = 1;
28
+ return gea::framework::memory::Allocator::allocatePreferSpiram(size, alignof(std::max_align_t));
29
+ }
30
+
31
+ void gea_runtime_stbtt_free(void *ptr, void *)
32
+ {
33
+ gea::framework::memory::Allocator::free(ptr);
34
+ }
35
+
36
+ } // namespace
37
+
38
+ #define STBTT_STATIC
39
+ #define STBTT_malloc(x, u) gea_runtime_stbtt_malloc((x), (u))
40
+ #define STBTT_free(x, u) gea_runtime_stbtt_free((x), (u))
41
+ #define STBTT_RASTERIZER_VERSION 1
42
+ #define STB_TRUETYPE_IMPLEMENTATION
43
+ #include "stb_truetype.h"
44
+ #endif
45
+
46
+ namespace gea::framework::graphics {
47
+
48
+ RasterizedFont::RasterizedFont(const RasterizedFontData *data)
49
+ : data_(data)
50
+ {
51
+ }
52
+
53
+ class RasterizedGlyphLookup {
54
+ public:
55
+ static const Glyph *find(const RasterizedFontData *font, int codepoint)
56
+ {
57
+ if (font->glyphCount > 0) {
58
+ const int first = font->glyphs[0].codepoint;
59
+ const int index = codepoint - first;
60
+ if (index >= 0 && index < font->glyphCount && font->glyphs[index].codepoint == codepoint) {
61
+ return &font->glyphs[index];
62
+ }
63
+ }
64
+ for (int i = 0; i < font->glyphCount; i++) {
65
+ if (font->glyphs[i].codepoint == codepoint) return &font->glyphs[i];
66
+ }
67
+ if (font->glyphCount > 0 && codepoint != '?') {
68
+ for (int i = 0; i < font->glyphCount; i++) {
69
+ if (font->glyphs[i].codepoint == '?') return &font->glyphs[i];
70
+ }
71
+ return &font->glyphs[0];
72
+ }
73
+ return nullptr;
74
+ }
75
+ };
76
+
77
+ namespace {
78
+
79
+ void ensureGeneratedFontsLinked()
80
+ {
81
+ #ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
82
+ generated::ensureLinked();
83
+ #endif
84
+ }
85
+
86
+ #if GEA_EMBEDDED_TTF_RUNTIME_FONTS
87
+
88
+ #if GEA_EMBEDDED_TTF_RUNTIME_TRACE
89
+ void runtimeTtfTrace(const char *fmt, ...)
90
+ {
91
+ std::printf("GEA_TTF ");
92
+ va_list args;
93
+ va_start(args, fmt);
94
+ std::vprintf(fmt, args);
95
+ va_end(args);
96
+ std::printf("\n");
97
+ std::fflush(stdout);
98
+ }
99
+ #else
100
+ void runtimeTtfTrace(const char *, ...) {}
101
+ #endif
102
+
103
+ constexpr int kRuntimeAsciiFirst = 0x20;
104
+ constexpr int kRuntimeAsciiLast = 0x7e;
105
+ constexpr int kRuntimeAsciiCount = kRuntimeAsciiLast - kRuntimeAsciiFirst + 1;
106
+ constexpr int kRuntimeExtraGlyphs = 1;
107
+ constexpr int kRuntimeGlyphCapacity = kRuntimeAsciiCount + kRuntimeExtraGlyphs;
108
+ constexpr int kRuntimeFontCacheSlots = 64;
109
+ constexpr int kRuntimeMaxAtlasWidth = 256;
110
+ constexpr int kRuntimeAtlasPadding = 1;
111
+ constexpr int kRuntimeMaxFontPx = 192;
112
+
113
+ struct RuntimeRasterizedFontSlot {
114
+ bool built = false;
115
+ bool ready = false;
116
+ int familyId = -1;
117
+ int requestedSizePx = 0;
118
+ unsigned long usedAt = 0;
119
+ const std::uint8_t *fontBytes = nullptr;
120
+ unsigned long fontLength = 0;
121
+ stbtt_fontinfo fontInfo{};
122
+ RasterizedFontData data{};
123
+ Glyph glyphs[kRuntimeGlyphCapacity]{};
124
+ std::uint8_t metricReady[kRuntimeGlyphCapacity]{};
125
+ std::uint8_t rasterized[kRuntimeGlyphCapacity]{};
126
+ std::uint8_t *atlas = nullptr;
127
+ int atlasCapacityHeight = 0;
128
+ int atlasCursorX = 0;
129
+ int atlasCursorY = 0;
130
+ int atlasRowHeight = 0;
131
+ };
132
+
133
+ int runtimeCodepointAt(int index)
134
+ {
135
+ if (index >= 0 && index < kRuntimeAsciiCount) return kRuntimeAsciiFirst + index;
136
+ if (index == kRuntimeAsciiCount) return 0x00b0;
137
+ return -1;
138
+ }
139
+
140
+ int roundToInt(float value)
141
+ {
142
+ return value >= 0.0f ? static_cast<int>(value + 0.5f) : static_cast<int>(value - 0.5f);
143
+ }
144
+
145
+ int ceilToInt(float value)
146
+ {
147
+ return static_cast<int>(std::ceil(value));
148
+ }
149
+
150
+ RuntimeRasterizedFontSlot *runtimeFontSlots()
151
+ {
152
+ static RuntimeRasterizedFontSlot *slots = nullptr;
153
+ if (!slots) {
154
+ void *storage = memory::Allocator::allocatePreferSpiram(
155
+ sizeof(RuntimeRasterizedFontSlot) * kRuntimeFontCacheSlots,
156
+ alignof(RuntimeRasterizedFontSlot));
157
+ if (!storage) return nullptr;
158
+ slots = static_cast<RuntimeRasterizedFontSlot *>(storage);
159
+ for (int i = 0; i < kRuntimeFontCacheSlots; i++) new (&slots[i]) RuntimeRasterizedFontSlot();
160
+ }
161
+ return slots;
162
+ }
163
+
164
+ unsigned long nextRuntimeFontClock()
165
+ {
166
+ static unsigned long clock = 1;
167
+ return clock++;
168
+ }
169
+
170
+ void releaseRuntimeFontSlot(RuntimeRasterizedFontSlot &slot)
171
+ {
172
+ if (slot.atlas) memory::Allocator::free(slot.atlas);
173
+ slot = RuntimeRasterizedFontSlot{};
174
+ }
175
+
176
+ bool buildRuntimeFontSlot(RuntimeRasterizedFontSlot &slot, int familyId, int sizePx)
177
+ {
178
+ runtimeTtfTrace("slot.begin family=%d size=%d", familyId, sizePx);
179
+ releaseRuntimeFontSlot(slot);
180
+ slot.built = true;
181
+ slot.familyId = familyId;
182
+ slot.requestedSizePx = sizePx;
183
+
184
+ unsigned long fontLength = 0;
185
+ const std::uint8_t *fontBytes = generated::lookupRuntimeTtfFontForFamily(familyId, &fontLength);
186
+ runtimeTtfTrace("slot.lookup family=%d bytes=%p len=%lu", familyId, static_cast<const void *>(fontBytes), fontLength);
187
+ if (!fontBytes || fontLength == 0) return false;
188
+
189
+ const int offset = stbtt_GetFontOffsetForIndex(fontBytes, 0);
190
+ runtimeTtfTrace("slot.offset family=%d offset=%d", familyId, offset);
191
+ if (offset < 0 || !stbtt_InitFont(&slot.fontInfo, fontBytes, offset)) return false;
192
+ runtimeTtfTrace("slot.init family=%d ok=1", familyId);
193
+
194
+ slot.fontBytes = fontBytes;
195
+ slot.fontLength = fontLength;
196
+ const int clampedSizePx = std::clamp(sizePx > 0 ? sizePx : 16, 1, kRuntimeMaxFontPx);
197
+ const float scale = stbtt_ScaleForMappingEmToPixels(&slot.fontInfo, static_cast<float>(clampedSizePx));
198
+ runtimeTtfTrace("slot.scale family=%d size=%d scale_x1000=%d", familyId, clampedSizePx, static_cast<int>(scale * 1000.0f));
199
+ if (scale <= 0.0f) return false;
200
+
201
+ int ascent = 0;
202
+ int descent = 0;
203
+ int lineGap = 0;
204
+ stbtt_GetFontVMetrics(&slot.fontInfo, &ascent, &descent, &lineGap);
205
+ const int ascender = ceilToInt(static_cast<float>(ascent) * scale);
206
+ const int descender = ceilToInt(static_cast<float>(-descent) * scale);
207
+ const int lineHeight = std::max(1, ascender + descender);
208
+
209
+ int glyphCount = 0;
210
+ for (int i = 0; i < kRuntimeGlyphCapacity; i++) {
211
+ const int codepoint = runtimeCodepointAt(i);
212
+ if (codepoint < 0) continue;
213
+ Glyph &glyph = slot.glyphs[glyphCount++];
214
+ glyph.codepoint = codepoint;
215
+ glyph.sourceX = 0;
216
+ glyph.sourceY = 0;
217
+ glyph.width = 0;
218
+ glyph.height = 0;
219
+ glyph.advance = std::max(1, clampedSizePx / 2);
220
+ glyph.bearingX = 0;
221
+ glyph.bearingY = 0;
222
+ }
223
+
224
+ if (glyphCount <= 0) return false;
225
+
226
+ slot.atlas = static_cast<std::uint8_t *>(memory::Allocator::allocatePreferSpiram(1, alignof(std::uint8_t)));
227
+ if (!slot.atlas) return false;
228
+ slot.atlas[0] = 0;
229
+
230
+ slot.data.id = 0x70000000 + familyId * 1000 + clampedSizePx;
231
+ slot.data.sizePx = clampedSizePx;
232
+ slot.data.lineHeight = lineHeight;
233
+ slot.data.ascender = ascender;
234
+ slot.data.descender = descender;
235
+ slot.data.glyphCount = glyphCount;
236
+ slot.data.glyphs = slot.glyphs;
237
+ slot.data.atlasWidth = 1;
238
+ slot.data.atlasHeight = 1;
239
+ slot.data.atlas = slot.atlas;
240
+ slot.atlasCapacityHeight = 1;
241
+ slot.ready = true;
242
+ runtimeTtfTrace("slot.ready family=%d size=%d glyphs=%d line=%d asc=%d desc=%d",
243
+ familyId,
244
+ clampedSizePx,
245
+ glyphCount,
246
+ lineHeight,
247
+ ascender,
248
+ descender);
249
+ return true;
250
+ }
251
+
252
+ RuntimeRasterizedFontSlot *runtimeSlotForData(const RasterizedFontData *data)
253
+ {
254
+ if (!data) return nullptr;
255
+ RuntimeRasterizedFontSlot *slots = runtimeFontSlots();
256
+ if (!slots) return nullptr;
257
+ for (int i = 0; i < kRuntimeFontCacheSlots; i++) {
258
+ if (slots[i].ready && &slots[i].data == data) return &slots[i];
259
+ }
260
+ return nullptr;
261
+ }
262
+
263
+ RuntimeRasterizedFontSlot *runtimeSlotForGlyph(const RasterizedFontData *data, int codepoint, int *indexOut)
264
+ {
265
+ RuntimeRasterizedFontSlot *slot = runtimeSlotForData(data);
266
+ if (!slot) return nullptr;
267
+ for (int i = 0; i < data->glyphCount; i++) {
268
+ if (slot->glyphs[i].codepoint != codepoint) continue;
269
+ if (indexOut) *indexOut = i;
270
+ return slot;
271
+ }
272
+ return nullptr;
273
+ }
274
+
275
+ bool reserveRuntimeAtlasRows(RuntimeRasterizedFontSlot &slot, int requiredHeight)
276
+ {
277
+ if (requiredHeight <= slot.atlasCapacityHeight && slot.data.atlasWidth == kRuntimeMaxAtlasWidth) return true;
278
+ const int newHeight = std::max(requiredHeight, slot.atlasCapacityHeight + 64);
279
+ runtimeTtfTrace("atlas.reserve family=%d size=%d old=%d new=%d",
280
+ slot.familyId,
281
+ slot.data.sizePx,
282
+ slot.atlasCapacityHeight,
283
+ newHeight);
284
+ const std::size_t newBytes = static_cast<std::size_t>(kRuntimeMaxAtlasWidth) * static_cast<std::size_t>(newHeight);
285
+ std::uint8_t *next = static_cast<std::uint8_t *>(memory::Allocator::allocatePreferSpiram(newBytes, alignof(std::uint8_t)));
286
+ if (!next) return false;
287
+ std::memset(next, 0, newBytes);
288
+ if (slot.atlas && slot.data.atlasWidth > 0 && slot.data.atlasHeight > 0) {
289
+ const int copyWidth = std::min(slot.data.atlasWidth, kRuntimeMaxAtlasWidth);
290
+ for (int row = 0; row < slot.data.atlasHeight && row < newHeight; row++) {
291
+ std::memcpy(next + row * kRuntimeMaxAtlasWidth,
292
+ slot.atlas + row * slot.data.atlasWidth,
293
+ static_cast<std::size_t>(copyWidth));
294
+ }
295
+ memory::Allocator::free(slot.atlas);
296
+ }
297
+ slot.atlas = next;
298
+ slot.atlasCapacityHeight = newHeight;
299
+ slot.data.atlas = slot.atlas;
300
+ slot.data.atlasWidth = kRuntimeMaxAtlasWidth;
301
+ slot.data.atlasHeight = newHeight;
302
+ return true;
303
+ }
304
+
305
+ void ensureRuntimeGlyphMetrics(const RasterizedFontData *data, int codepoint)
306
+ {
307
+ int index = -1;
308
+ RuntimeRasterizedFontSlot *slot = runtimeSlotForGlyph(data, codepoint, &index);
309
+ if (!slot || index < 0 || slot->metricReady[index]) return;
310
+ static int metricTraceCount = 0;
311
+ if (metricTraceCount < 48) {
312
+ runtimeTtfTrace("glyph.metrics.begin family=%d size=%d cp=%d", slot->familyId, slot->data.sizePx, codepoint);
313
+ metricTraceCount++;
314
+ }
315
+ Glyph &glyph = slot->glyphs[index];
316
+ slot->metricReady[index] = 1;
317
+ if (codepoint != '?' && codepoint != ' ' && stbtt_FindGlyphIndex(&slot->fontInfo, codepoint) == 0) return;
318
+
319
+ const float scale = stbtt_ScaleForMappingEmToPixels(&slot->fontInfo, static_cast<float>(slot->data.sizePx));
320
+ if (scale <= 0.0f) return;
321
+
322
+ int advanceWidth = 0;
323
+ int leftSideBearing = 0;
324
+ stbtt_GetCodepointHMetrics(&slot->fontInfo, codepoint, &advanceWidth, &leftSideBearing);
325
+
326
+ int x0 = 0;
327
+ int y0 = 0;
328
+ int x1 = 0;
329
+ int y1 = 0;
330
+ stbtt_GetCodepointBitmapBox(&slot->fontInfo, codepoint, scale, scale, &x0, &y0, &x1, &y1);
331
+
332
+ glyph.width = std::max(0, x1 - x0);
333
+ glyph.height = std::max(0, y1 - y0);
334
+ glyph.advance = std::max(1, roundToInt(static_cast<float>(advanceWidth) * scale));
335
+ glyph.bearingX = x0;
336
+ glyph.bearingY = -y0;
337
+ if (glyph.width <= 0 || glyph.height <= 0) return;
338
+
339
+ if (slot->atlasCursorX > 0 && slot->atlasCursorX + glyph.width + kRuntimeAtlasPadding > kRuntimeMaxAtlasWidth) {
340
+ slot->atlasCursorX = 0;
341
+ slot->atlasCursorY += slot->atlasRowHeight + kRuntimeAtlasPadding;
342
+ slot->atlasRowHeight = 0;
343
+ }
344
+ const int requiredHeight = slot->atlasCursorY + glyph.height;
345
+ if (!reserveRuntimeAtlasRows(*slot, requiredHeight)) {
346
+ glyph.width = 0;
347
+ glyph.height = 0;
348
+ return;
349
+ }
350
+ glyph.sourceX = slot->atlasCursorX;
351
+ glyph.sourceY = slot->atlasCursorY;
352
+ slot->atlasRowHeight = std::max(slot->atlasRowHeight, glyph.height);
353
+ slot->atlasCursorX += glyph.width + kRuntimeAtlasPadding;
354
+ if (metricTraceCount <= 48) {
355
+ runtimeTtfTrace("glyph.metrics.ready family=%d size=%d cp=%d adv=%d box=%dx%d bearing=%d,%d src=%d,%d",
356
+ slot->familyId,
357
+ slot->data.sizePx,
358
+ codepoint,
359
+ glyph.advance,
360
+ glyph.width,
361
+ glyph.height,
362
+ glyph.bearingX,
363
+ glyph.bearingY,
364
+ glyph.sourceX,
365
+ glyph.sourceY);
366
+ }
367
+ }
368
+
369
+ void ensureRuntimeGlyphCoverage(const RasterizedFontData *data, const Glyph &glyph)
370
+ {
371
+ int index = -1;
372
+ RuntimeRasterizedFontSlot *slot = runtimeSlotForGlyph(data, glyph.codepoint, &index);
373
+ if (!slot || !slot->atlas || data->atlasWidth <= 0 || index < 0) return;
374
+ ensureRuntimeGlyphMetrics(data, glyph.codepoint);
375
+ if (slot->rasterized[index]) return;
376
+ const Glyph &slotGlyph = slot->glyphs[index];
377
+ if (slotGlyph.width <= 0 || slotGlyph.height <= 0) return;
378
+ if (slotGlyph.sourceY + slotGlyph.height > slot->data.atlasHeight) return;
379
+ if (slotGlyph.sourceX + slotGlyph.width > slot->data.atlasWidth) return;
380
+ slot->rasterized[index] = 1;
381
+ static int rasterTraceCount = 0;
382
+ if (rasterTraceCount < 32) {
383
+ runtimeTtfTrace("glyph.raster family=%d size=%d cp=%d box=%dx%d",
384
+ slot->familyId,
385
+ slot->data.sizePx,
386
+ slotGlyph.codepoint,
387
+ slotGlyph.width,
388
+ slotGlyph.height);
389
+ rasterTraceCount++;
390
+ }
391
+ const float scale = stbtt_ScaleForMappingEmToPixels(&slot->fontInfo, static_cast<float>(slot->data.sizePx));
392
+ if (scale <= 0.0f) return;
393
+ stbtt_MakeCodepointBitmap(&slot->fontInfo,
394
+ slot->atlas + slotGlyph.sourceY * data->atlasWidth + slotGlyph.sourceX,
395
+ slotGlyph.width,
396
+ slotGlyph.height,
397
+ data->atlasWidth,
398
+ scale,
399
+ scale,
400
+ slotGlyph.codepoint);
401
+ }
402
+
403
+ const RasterizedFontData *runtimeTtfFontForFamily(int familyId, int sizePx)
404
+ {
405
+ if (familyId < 0) return nullptr;
406
+ const int requestedSizePx = std::clamp(sizePx > 0 ? sizePx : 16, 1, kRuntimeMaxFontPx);
407
+ RuntimeRasterizedFontSlot *slots = runtimeFontSlots();
408
+ if (!slots) return nullptr;
409
+ RuntimeRasterizedFontSlot *empty = nullptr;
410
+ RuntimeRasterizedFontSlot *lru = &slots[0];
411
+ const unsigned long now = nextRuntimeFontClock();
412
+
413
+ for (int i = 0; i < kRuntimeFontCacheSlots; i++) {
414
+ RuntimeRasterizedFontSlot &slot = slots[i];
415
+ if (slot.built && slot.familyId == familyId && slot.requestedSizePx == requestedSizePx) {
416
+ slot.usedAt = now;
417
+ return slot.ready ? &slot.data : nullptr;
418
+ }
419
+ if (!slot.built && !empty) empty = &slot;
420
+ if (slot.usedAt < lru->usedAt) lru = &slot;
421
+ }
422
+
423
+ RuntimeRasterizedFontSlot *slot = empty ? empty : lru;
424
+ const bool ready = buildRuntimeFontSlot(*slot, familyId, requestedSizePx);
425
+ slot->usedAt = now;
426
+ return ready ? &slot->data : nullptr;
427
+ }
428
+
429
+ #endif
430
+
431
+ } // namespace
432
+
433
+ bool RasterizedFont::valid() const
434
+ {
435
+ return data_ && data_->atlas && data_->glyphCount > 0;
436
+ }
437
+
438
+ int RasterizedFont::sizePx() const
439
+ {
440
+ return data_ ? data_->sizePx : 0;
441
+ }
442
+
443
+ int RasterizedFont::lineHeight() const
444
+ {
445
+ return data_ ? data_->lineHeight : 0;
446
+ }
447
+
448
+ int RasterizedFont::ascender() const
449
+ {
450
+ return data_ ? data_->ascender : 0;
451
+ }
452
+
453
+ bool RasterizedFont::glyph(int codepoint, Glyph *out) const
454
+ {
455
+ if (!out || !data_) return false;
456
+ const Glyph *glyph = RasterizedGlyphLookup::find(data_, codepoint);
457
+ if (!glyph) return false;
458
+ #if GEA_EMBEDDED_TTF_RUNTIME_FONTS
459
+ ensureRuntimeGlyphMetrics(data_, glyph->codepoint);
460
+ glyph = RasterizedGlyphLookup::find(data_, glyph->codepoint);
461
+ if (!glyph) return false;
462
+ #endif
463
+ *out = *glyph;
464
+ return true;
465
+ }
466
+
467
+ std::uint8_t RasterizedFont::coverage(const Glyph &glyph, int row, int col) const
468
+ {
469
+ if (!data_ || !data_->atlas) return 0;
470
+ if (row < 0 || row >= glyph.height || col < 0 || col >= glyph.width) return 0;
471
+ #if GEA_EMBEDDED_TTF_RUNTIME_FONTS
472
+ ensureRuntimeGlyphCoverage(data_, glyph);
473
+ #endif
474
+ const int x = glyph.sourceX + col;
475
+ const int y = glyph.sourceY + row;
476
+ // One predictable branch on a per-font constant — the 8-bit path below stays
477
+ // a single indexed load, as it was before 2-bit atlases existed. A 2-bit
478
+ // atlas packs four pixels per byte, rows byte-aligned, leftmost pixel in the
479
+ // most significant bit pair; see packLevelsTo2Bit in
480
+ // packages/core/scripts/generate-gea-embedded-fonts.mjs. Levels normally map
481
+ // back to the 0..255 coverage domain as level * 85. PaperS3 uses a calibrated
482
+ // compile-time LUT: its direct-drive panel pushed the nominal intermediate
483
+ // framebuffer grays (10 and 5) toward white and black, so its edge coverages
484
+ // are calibrated explicitly for the panel. The current pair lands at gray4
485
+ // levels 7 and 5. Endpoints remain
486
+ // exact, and the indexed load replaces rather than adds per-pixel arithmetic.
487
+ if (data_->atlasBits == 2) {
488
+ const int stride = (data_->atlasWidth + 3) >> 2;
489
+ const int level = (data_->atlas[y * stride + (x >> 2)] >> ((3 - (x & 3)) * 2)) & 0x3;
490
+ #if defined(GEA_EMBEDDED_FONT_COVERAGE_PAPERS3) && GEA_EMBEDDED_FONT_COVERAGE_PAPERS3
491
+ static constexpr std::uint8_t kCoverage[4] = {0, 144, 176, 255};
492
+ return kCoverage[level];
493
+ #else
494
+ return static_cast<std::uint8_t>(level * 85);
495
+ #endif
496
+ }
497
+ return data_->atlas[y * data_->atlasWidth + x];
498
+ }
499
+
500
+ RasterizedFont FontRegistry::rasterized(int fontId)
501
+ {
502
+ ensureGeneratedFontsLinked();
503
+ return RasterizedFont(generated::lookupFont(fontId));
504
+ }
505
+
506
+ RasterizedFont FontRegistry::rasterizedFamily(int familyId, int sizePx)
507
+ {
508
+ ensureGeneratedFontsLinked();
509
+ #if GEA_EMBEDDED_TTF_RUNTIME_FONTS
510
+ if (const RasterizedFontData *data = runtimeTtfFontForFamily(familyId, sizePx)) {
511
+ return RasterizedFont(data);
512
+ }
513
+ #endif
514
+ return RasterizedFont(generated::lookupFontForFamily(familyId, sizePx));
515
+ }
516
+
517
+ const BitmapFont8x16 &FontRegistry::bitmap8x16()
518
+ {
519
+ return BitmapFont8x16::instance();
520
+ }
521
+
522
+ int FontRegistry::familyId(const char *family)
523
+ {
524
+ ensureGeneratedFontsLinked();
525
+ return generated::lookupFontFamily(family);
526
+ }
527
+
528
+ const char *FontRegistry::familyName(int familyId)
529
+ {
530
+ ensureGeneratedFontsLinked();
531
+ return generated::lookupFontFamilyName(familyId);
532
+ }
533
+
534
+ namespace generated {
535
+
536
+ __attribute__((weak)) const RasterizedFontData *lookupFont(int fontId)
537
+ {
538
+ (void)fontId;
539
+ return nullptr;
540
+ }
541
+
542
+ __attribute__((weak)) const RasterizedFontData *lookupFontForFamily(int familyId, int sizePx)
543
+ {
544
+ (void)familyId;
545
+ (void)sizePx;
546
+ return nullptr;
547
+ }
548
+
549
+ __attribute__((weak)) int lookupFontFamily(const char *family)
550
+ {
551
+ (void)family;
552
+ return -1;
553
+ }
554
+
555
+ __attribute__((weak)) const char *lookupFontFamilyName(int familyId)
556
+ {
557
+ (void)familyId;
558
+ return nullptr;
559
+ }
560
+
561
+ __attribute__((weak)) const std::uint8_t *lookupRuntimeTtfFontForFamily(int familyId, unsigned long *length)
562
+ {
563
+ (void)familyId;
564
+ if (length) *length = 0;
565
+ return nullptr;
566
+ }
567
+
568
+ } // namespace generated
569
+
570
+ } // namespace gea::framework::graphics