Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(ui): invalidate text cache when overflow changes (#85)

YHH934ab0565a0f
diff --git a/examples/ui-localization-debug/README.md b/examples/ui-localization-debug/README.md
index f3cf16e49..0eef34a66 100644
--- a/examples/ui-localization-debug/README.md
+++ b/examples/ui-localization-debug/README.md
@@ -133,3 +133,7 @@ An outer UIDialog contains Runtime button and Nested confirm (another UIDialog).
Only Nested confirm should remain in the Tab ring. Tab and Shift+Tab stay there;
Enter dismisses it, and Tab returns to Runtime button in the outer modal.
R closes the fixture's dialogs. Independent modal stacking is not covered here.
+
+Click Inside, then press O repeatedly to cycle Clip → Ellipsis → Visible on
+the same long text. The layout must change without changing the text or box.
+Visible may still be clipped by the fixture's actual ancestor mask.
diff --git a/examples/ui-localization-debug/VALIDATION.md b/examples/ui-localization-debug/VALIDATION.md
index eba612bc7..3276464a8 100644
--- a/examples/ui-localization-debug/VALIDATION.md
+++ b/examples/ui-localization-debug/VALIDATION.md
@@ -364,3 +364,21 @@ snapshot; live-game-only dialogs and scripts need runtime acceptance separately.
- No local Electron/native runner launched. Device IME/accessibility,
independent modal stacking, custom-scheme, shaping and cache performance
remain open. RM-009 and RM-008 are still the two unfinished 0.94.0 cards.
+
+## Text overflow cache correctness — 2026-10-09
+
+- The cached renderer signature omitted overflow. Changing only Visible,
+ Clip or Ellipsis could reuse the old geometry indefinitely. Include overflow
+ in the signature, with omitted overflow equivalent to the Visible default.
+- The real renderer cache regression fails before the fix: cached Visible
+ geometry differs from fresh Clip layout. After the fix, all three modes
+ match fresh geometry; a second static entity retains the same batch and
+ performs no new glyph layout reads. 35 renderer/cache tests pass, along with
+ SDK build and 16 source/built API checks.
+- Built-in browser production Play/SDK/WASM: click Inside, then O changes
+ only the overflow mode after installing the same long text. Clip truncates
+ without an ellipsis, Ellipsis adds it, Visible extends beyond the label box
+ until the real ancestor mask clips it. No injected game state or event queue.
+- This is cache correctness evidence, not a performance benchmark. Same-device
+ cache/reflow timings, complex shaping and device acceptance remain open.
+ No local Electron/native runner launched; RM-009 remains In Progress.
diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.ts
index 1c35cc1a5..37394bfe0 100644
--- a/sdk/src/ui/text/text-renderer.ts
+++ b/sdk/src/ui/text/text-renderer.ts
@@ -345,6 +345,7 @@ export function signatureOf(p: DrawTextParams): string {
p.richText ? 1 : 0, p.align ?? 0, p.verticalAlign ?? 0,
p.lineHeight ?? 0, p.letterSpacing ?? 0,
p.maxWidth ?? 0, p.boxWidth ?? 0, p.boxHeight ?? 0,
+ p.overflow ?? TextOverflow.Visible,
p.originX ?? 0, p.originY ?? 0,
p.color.join(','), shadow, outline,
].join('|');
diff --git a/sdk/tests/ui-text-overflow-cache.test.ts b/sdk/tests/ui-text-overflow-cache.test.ts
new file mode 100644
index 000000000..2b5ea9b4c
--- /dev/null
+++ b/sdk/tests/ui-text-overflow-cache.test.ts
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: Apache-2.0
+import { expect, it, vi } from 'vitest';
+import { GlyphAtlas } from '../src/ui/text/glyph-atlas';
+import { SdfTextRenderer, drawTextWith, type DrawTextParams } from '../src/ui/text/text-renderer';
+import { TextOverflow } from '../src/ui/core/text';
+import { submitTextBatch } from '../src/ui/text/submit';
+vi.mock('../src/ui/text/submit', async importOriginal => ({
+ ...await importOriginal<typeof import('../src/ui/text/submit')>(), submitTextBatch: vi.fn(),
+}));
+
+it('rebuilds only changed overflow geometry while other static text keeps its cached batches', () => {
+ const atlas = new GlyphAtlas({ renderSize: 48, spread: 0, rasterize: () => ({
+ pixels: new Uint8Array(10 * 12 * 4), width: 10, height: 12,
+ advance: 11, bearingX: 1, bearingY: 10,
+ }) }, { createPage: () => 1000, uploadSubRegion: () => {} }, { pageSize: 1024, padding: 1, sdf: false });
+ // Inject an atlas and submission sink, preserving the real renderer cache path.
+ const renderer = Object.assign(Object.create(SdfTextRenderer.prototype), {
+ atlas, sdf: false, module: null, cache_: new Map(),
+ }) as SdfTextRenderer;
+ const transform = new Float32Array(16);
+ const params: DrawTextParams = { text: 'ABCDEF', fontFamily: 'Arial', fontSizePx: 48,
+ color: [1, 1, 1, 1], boxWidth: 24, boxHeight: 60, overflow: TextOverflow.Visible };
+ const submit = vi.mocked(submitTextBatch);
+ const draw = (entity: number, p = params) => {
+ submit.mockClear(); renderer.drawText(p, transform, entity, 0, 0);
+ return submit.mock.calls.map(call => call[1]);
+ };
+ const initial = draw(1), other = draw(2);
+ const reads = vi.spyOn(atlas, 'getGlyph');
+ expect(draw(1)[0]).toBe(initial[0]);
+ expect(reads).not.toHaveBeenCalled();
+ for (const overflow of [TextOverflow.Clip, TextOverflow.Ellipsis, TextOverflow.Visible]) {
+ const next = { ...params, overflow };
+ const batches = draw(1, next);
+ const expected: Float32Array[] = [];
+ drawTextWith(atlas, vertices => expected.push(vertices), next);
+ expect(batches.map(vertices => Array.from(vertices))).toEqual(expected.map(vertices => Array.from(vertices)));
+ reads.mockClear();
+ expect(draw(2)[0]).toBe(other[0]);
+ expect(reads).not.toHaveBeenCalled();
+ }
+});
diff --git a/sdk/tests/ui-text-renderer.test.ts b/sdk/tests/ui-text-renderer.test.ts
index 7bbad0b2b..b5a47b2e2 100644
--- a/sdk/tests/ui-text-renderer.test.ts
+++ b/sdk/tests/ui-text-renderer.test.ts
@@ -232,6 +232,7 @@ describe('text cache signature completeness', () => {
['maxWidth', { maxWidth: 200 }],
['boxWidth', { boxWidth: 400 }],
['boxHeight', { boxHeight: 100 }],
+ ['overflow', { overflow: 2 }],
['originX', { originX: 5 }],
['originY', { originY: 5 }],
['color', { color: [1, 0, 0, 1] }],
diff --git a/tools/ui-events-play-project.ts b/tools/ui-events-play-project.ts
index 3452a2e6e..4098447bd 100644
--- a/tools/ui-events-play-project.ts
+++ b/tools/ui-events-play-project.ts
@@ -15,6 +15,11 @@ addSystemToSchedule(Schedule.Update, defineSystem([Res(UIEvents), GetWorld(), Re
installed.set(world, () => {
// Fixture shortcuts must not react to letters typed into an engine field.
if (focus.focusedEntity !== null && world.has(focus.focusedEntity, TextInput)) return;
+ if (input.isKeyPressed('KeyO')) {
+ const text = world.get(inside, Text);
+ world.insert(inside, Text, { ...text, content: 'Overflow mode changes cached text',
+ wordWrap: false, overflow: (text.overflow + 1) % 3 });
+ }
if (input.isKeyPressed('KeyT')) {
if (textField) { textField.dispose(); textField = null; }
else {