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): split long text before glyph indices wrap

yhh1ec69129a587
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61bf87bcd..f865b0d8c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ published separately; it ships inside the editor.
- **MRAID store clicks carry their destination.** `playableCta(url)` forwards an explicit store link, with host-provided `clickTag` as the MRAID fallback. Missing and malformed destinations no longer call `mraid.open` with no URL. HTTP(S), Android market and iOS App Store schemes are supported; other networks retain their host-selected store destinations.
+- **Long text preserves glyphs beyond the 16-bit index limit.** Oversized atlas-page runs split into batches of at most 16,384 quads while retaining positions, colors and effect ordering. The text-submit ABI stays unchanged, and raw glyph builders reject oversized batches instead of silently wrapping indices.
- **UI text fades reuse cached geometry.** Inherited opacity changes update vertex alpha without repeating glyph lookup, layout or index generation. Bitmap and SDF text retain their original fill, rich-text, shadow and outline alpha, restoring it without cumulative fading; authored color and layout changes still rebuild the cache.
diff --git a/sdk/src/ui/text/layout.ts b/sdk/src/ui/text/layout.ts
index aa70a97cc..65e686244 100644
--- a/sdk/src/ui/text/layout.ts
+++ b/sdk/src/ui/text/layout.ts
@@ -102,6 +102,9 @@ export interface GlyphVertexData {
indices: Uint16Array; // 6 per glyph
}
+/** Four vertices per glyph must fit the text-submit ABI's Uint16 indices. */
+export const MAX_TEXT_BATCH_GLYPHS = 0x10000 / 4;
+
/**
* Build interleaved glyph vertices and indices, with atlas v0 at screen-top.
* `originX/Y` positions the batch inside its text box. Disable per-glyph colors
@@ -116,6 +119,7 @@ export function buildGlyphVertices(
useGlyphColors = true,
): GlyphVertexData {
const n = glyphs.length;
+ if (n > MAX_TEXT_BATCH_GLYPHS) throw new RangeError('Glyph batch exceeds the Uint16 index range; split it before tessellation');
const vertices = new Float32Array(n * 4 * TEXT_VERTEX_FLOATS);
const indices = new Uint16Array(n * 6);
diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.ts
index dc3f3f693..04b2e5940 100644
--- a/sdk/src/ui/text/text-renderer.ts
+++ b/sdk/src/ui/text/text-renderer.ts
@@ -17,10 +17,10 @@ import { CanvasGlyphRasterizer, type CanvasGlyphRasterizerOptions } from './glyp
import { NativeGlyphRasterizer } from './native-glyph-rasterizer';
import { EngineAtlasPageStore } from './atlas-page-store';
import { TextAlign, TextVerticalAlign, TextOverflow } from '../core/text';
-import { layoutText, buildGlyphVertices, type LaidGlyph, type RGBA } from './layout';
+import { layoutText, buildGlyphVertices, MAX_TEXT_BATCH_GLYPHS, type LaidGlyph, type RGBA } from './layout';
import { submitTextBatch, TEXT_VERTEX_FLOATS } from './submit';
-/** Receives one (vertices, indices) batch per atlas page used by the text. */
+/** Receives index-safe glyph batches, each bound to one atlas page. */
export type GlyphBatchSink = (vertices: Float32Array, indices: Uint16Array, pageId: number) => void;
export interface DrawTextParams {
@@ -169,9 +169,8 @@ export function textBlockOriginY(
}
/**
- * Lay out `text` against `atlas` and emit one quad batch per atlas page to
- * `sink`. Pure given the atlas — no engine/Canvas dependency — so the grouping
- * and geometry are unit-testable.
+ * Lay out text against the atlas and emit index-safe quad batches to the sink.
+ * Pure given the atlas, so grouping and geometry are unit-testable.
*/
export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTextParams): void {
const opacity = p.opacity ?? 1;
@@ -201,20 +200,27 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex
);
// A string can reference glyphs across several atlas pages; each page is a
- // distinct texture, so group by page and emit one batch per page.
+ // distinct texture. Split oversized pages once, before emitting effects.
const byPage = new Map<number, LaidGlyph[]>();
for (const g of layout.glyphs) {
let arr = byPage.get(g.pageId);
if (!arr) { arr = []; byPage.set(g.pageId, arr); }
arr.push(g);
}
+ const chunks: Array<[number, readonly LaidGlyph[]]> = [];
+ for (const [pageId, glyphs] of byPage) {
+ for (let start = 0; start < glyphs.length; start += MAX_TEXT_BATCH_GLYPHS) {
+ chunks.push([pageId, glyphs.length <= MAX_TEXT_BATCH_GLYPHS
+ ? glyphs : glyphs.slice(start, start + MAX_TEXT_BATCH_GLYPHS)]);
+ }
+ }
const baseX = (p.originX ?? 0) + textBlockOffsetX(p.align, boxWidth, layout.width);
// Emit the glyph set once per page, recolored + offset. All passes are SDF
// glyphs in the same atlas/layer, so they batch and draw in submit order —
// shadow + outline first (behind), fill last (on top).
const emitPass = (color: RGBA, dx: number, dy: number, bias = 0, useGlyphColors = false): void => {
- for (const [pageId, glyphs] of byPage) {
+ for (const [pageId, glyphs] of chunks) {
const { vertices, indices } = buildGlyphVertices(glyphs, color, baseX + dx, originY + dy, bias, useGlyphColors);
if (opacity !== 1) {
for (let i = 7; i < vertices.length; i += TEXT_VERTEX_FLOATS) vertices[i] *= opacity;
diff --git a/sdk/tests/ui-text-layout.test.ts b/sdk/tests/ui-text-layout.test.ts
index 4dc247637..a43a21160 100644
--- a/sdk/tests/ui-text-layout.test.ts
+++ b/sdk/tests/ui-text-layout.test.ts
@@ -15,6 +15,12 @@ import {
import { layoutLine, buildGlyphVertices } from '../src/ui/text/layout';
const SPACE = 32;
+
+it('accepts the final Uint16 vertex and rejects an oversized raw glyph batch', () => {
+ const glyph = { pageId: 1, u0: 0, v0: 0, u1: 1, v1: 1, x0: 0, y0: 0, x1: 1, y1: 1 };
+ expect(buildGlyphVertices(new Array(16384).fill(glyph), [1, 1, 1, 1]).indices.at(-1)).toBe(65535);
+ expect(() => buildGlyphVertices(new Array(16385).fill(glyph), [1, 1, 1, 1])).toThrow(/Uint16 index range/);
+});
const UNKNOWN = 0xffff;
function makeAtlas(): GlyphAtlas {
diff --git a/sdk/tests/ui-text-renderer.test.ts b/sdk/tests/ui-text-renderer.test.ts
index 87c29be7b..810fa331f 100644
--- a/sdk/tests/ui-text-renderer.test.ts
+++ b/sdk/tests/ui-text-renderer.test.ts
@@ -1,9 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
- * @file REARCH_GUI P1.3b — drawTextWith orchestration: lays text out against a
- * (mock-backed) GlyphAtlas and emits one quad batch per atlas page. Pure
- * given the atlas, so the per-page grouping + geometry are headless-tested.
+ * @file drawTextWith grouping, index-safe batches and effect geometry over a
+ * mock-backed GlyphAtlas, without Canvas or GL.
*/
import { describe, it, expect, vi } from 'vitest';
@@ -29,6 +28,44 @@ function makeAtlas(pageSize: number, spread = 6): GlyphAtlas {
}
describe('REARCH_GUI P1.3b: drawTextWith', () => {
+ it.each([16384, 16385, 32769])('preserves every glyph when one atlas page carries %i quads', count => {
+ const batches: Array<{ vertices: Float32Array; indices: Uint16Array }> = [];
+ drawTextWith(makeAtlas(1024), (vertices, indices) => batches.push({ vertices, indices }), {
+ text: 'A'.repeat(count), fontFamily: 'Arial', fontSizePx: 24, color: [1, 1, 1, 1],
+ });
+ expect(batches).toHaveLength(Math.ceil(count / 16384));
+ let emitted = 0;
+ for (const { vertices, indices } of batches) {
+ const glyphs = vertices.length / (4 * TEXT_VERTEX_FLOATS);
+ expect(glyphs).toBeLessThanOrEqual(16384);
+ expect(indices).toHaveLength(glyphs * 6);
+ expect(indices[0]).toBe(0);
+ expect(indices.at(-1)).toBe(glyphs * 4 - 1);
+ expect(vertices[0]).toBeCloseTo(emitted * 5.5 + 0.5);
+ emitted += glyphs;
+ }
+ expect(emitted).toBe(count);
+ });
+
+ it('keeps all shadow and outline chunks behind an oversized fill', () => {
+ const sink = vi.fn();
+ drawTextWith(makeAtlas(1024), sink, {
+ text: 'A'.repeat(16385), fontFamily: 'Arial', fontSizePx: 24,
+ color: [1, 1, 1, 0.8], opacity: 0.5,
+ shadow: { color: [0, 0, 0, 0.6], dx: 2, dy: 2 },
+ outline: { color: [0, 1, 0, 0.4], width: 1 },
+ });
+ expect(sink).toHaveBeenCalledTimes(6);
+ const alpha = [0.3, 0.3, 0.2, 0.2, 0.4, 0.4];
+ for (let i = 0; i < 6; i++) {
+ const vertices = sink.mock.calls[i][0] as Float32Array;
+ const indices = sink.mock.calls[i][1] as Uint16Array;
+ expect(vertices.length / TEXT_VERTEX_FLOATS).toBeLessThanOrEqual(65536);
+ expect(indices.at(-1)).toBe(vertices.length / TEXT_VERTEX_FLOATS - 1);
+ expect(vertices[7]).toBeCloseTo(alpha[i]);
+ }
+ });
+
it('keeps rich fill colors out of shadow and outline passes on both atlas modes', () => {
for (const spread of [0, 6]) {
for (const blur of [0, 2]) {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 9577ca76c..d500abf8b 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -32,6 +32,8 @@ export const NOTED = {
{ note: 'MRAID store clicks carry their destination.' },
'fix(playable): observe MRAID readiness and viewability':
{ note: 'MRAID playables wait for ready and viewable hosts.' },
+ 'fix(ui): split long text before glyph indices wrap':
+ { note: 'Long text preserves glyphs beyond the 16-bit index limit.' },
'feat(playable): package TikTok and overseas Pangle creatives':
{ note: 'TikTok and overseas Pangle package the same playable project.' },
'perf(ui): reuse text geometry during inherited opacity fades':