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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf(text): trim overflowing plain text with prefix advances
yhhcf0e4387573e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bfaa67309..c3ed40fe2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Long plain-text overflow trims with linear glyph measurement work.** Clip and Ellipsis reuse one pass of prefix advances instead of remeasuring every shorter candidate. Trimming retains whole code points, trailing-whitespace handling and signed letter spacing; matching prefix order preserves the existing glyph geometry.+- **Page-free text glyph caches stay bounded.** Missing and oversized glyphs, whitespace metrics and measurement-only entries use a 4096-entry least-recently-used cache. Historical fonts and sizes retire without touching live drawable pages or cached layout coordinates; recently used measurements remain cached and retired entries rasterize again on demand.### Addeddiff --git a/sdk/src/ui/text/layout.ts b/sdk/src/ui/text/layout.tsindex 65e686244..ae05ebeab 100644--- a/sdk/src/ui/text/layout.ts+++ b/sdk/src/ui/text/layout.ts@@ -194,24 +194,22 @@ export interface MultilineTextOptions extends TextLayoutOptions {/** The character appended to a line the ellipsis mode cut. */const ELLIPSIS = '\u2026';-/**- * Cut `line` down to `limit` px, appending an ellipsis when asked. Trimmed one- * code point at a time from the end rather than by a ratio: a proportional guess- * lands mid-glyph on any font that is not monospace, and the ellipsis has to fit- * INSIDE the limit or the trim it paid for was for nothing.- */+/** Trim at codepoint boundaries using shared prefix advances, including the mark. */function truncateLine(line: string, measure: (s: string) => number, limit: number, ellipsis: boolean,+ measurePrefixes: (s: string) => readonly number[],): string {if (limit <= 0 || measure(line) <= limit) return line;const tail = ellipsis ? ELLIPSIS : '';+ const tailWidth = measure(tail);+ const widths = measurePrefixes(line);const chars = [...line];- // Whole-glyph granularity, and the caller is told so: a partial glyph would- // need the renderer to scissor, which text does not go through.- while (chars.length > 0) {- chars.pop();- const candidate = chars.join('').trimEnd();- if (measure(candidate + tail) <= limit) return candidate + tail;+ const ends = [0];+ for (const ch of chars) ends.push(ends[ends.length - 1]! + ch.length);+ for (let count = chars.length - 1; count >= 0; count--) {+ while (count > 0 && chars[count - 1]!.trimEnd() === '') count--;+ const end = ends[count]!;+ if (widths[end]! + tailWidth <= limit) return line.slice(0, end) + tail;}return tail;}@@ -370,12 +368,13 @@ export function layoutText(const kept = plainLines.slice(0, maxLines === Infinity ? undefined : maxLines);const dropped = kept.length < plainLines.length;const measure = (t: string) => measureWidth(t, atlas, fontFamily, opts.fontSizePx, style, opts.letterSpacing ?? 0);+ const prefixes = (t: string) => measurePrefixWidths(t, atlas, fontFamily, opts.fontSizePx, style, opts.letterSpacing ?? 0);const cut = kept.map((line, i) => {const last = i === kept.length - 1;// The last kept line carries the ellipsis when lines were dropped, even// if it fits: the mark says "there is more", not "this line was long".- if (dropped && last && ellipsis) return truncateLine(line + ELLIPSIS, measure, widthLimit || Infinity, true);- return widthLimit > 0 ? truncateLine(line, measure, widthLimit, ellipsis) : line;+ if (dropped && last && ellipsis) return truncateLine(line + ELLIPSIS, measure, widthLimit || Infinity, true, prefixes);+ return widthLimit > 0 ? truncateLine(line, measure, widthLimit, ellipsis, prefixes) : line;});lineLayouts = cut.map(line => layoutLine(line, atlas, fontFamily, opts, style));}diff --git a/sdk/tests/ui-text-overflow.test.ts b/sdk/tests/ui-text-overflow.test.tsindex 164243763..419a279f5 100644--- a/sdk/tests/ui-text-overflow.test.ts+++ b/sdk/tests/ui-text-overflow.test.ts@@ -122,3 +122,40 @@ describe('TextOverflow', () => {expect(linesOf(l)).toBe(2);});});+++it.each([TextOverflow.Clip, TextOverflow.Ellipsis])('clips long text with linear glyph lookup work (mode=%s)', overflow => {+ const atlas = makeAtlas();+ const get = atlas.getGlyph.bind(atlas);+ let lookups = 0;+ atlas.getGlyph = (...args) => { lookups++; return get(...args); };+ const text = 'A'.repeat(3000);+ const result = layoutText(text, atlas, 'Arial', { fontSizePx: F, boxWidth: 30, overflow });+ expect(result.width).toBeLessThanOrEqual(30);+ expect(result.glyphs.length).toBeGreaterThan(0);+ expect(lookups).toBeLessThan(text.length * 3);+});++it.each([-8, 0, 2.25])('matches whole-codepoint trimming with spaces and signed spacing (%s)', letterSpacing => {+ const atlas = makeAtlas();+ for (const line of ['A😀 B C测试 '.repeat(4), '😀😀😀abc', 'A\u2003B\u00a0C']) {+ for (const overflow of [TextOverflow.Clip, TextOverflow.Ellipsis]) {+ const measure = (s: string) => measureWidth(s, atlas, 'Arial', F, 0, letterSpacing);+ for (const limit of [5, 13.5, 23, 50]) {+ const tail = overflow === TextOverflow.Ellipsis ? '…' : '';+ let expected = line;+ if (measure(line) > limit) {+ const chars = [...line]; expected = tail;+ while (chars.length) {+ chars.pop(); const candidate = chars.join('').trimEnd() + tail;+ if (measure(candidate) <= limit) { expected = candidate; break; }+ }+ }+ const opts = { fontSizePx: F, letterSpacing };+ const actual = layoutText(line, atlas, 'Arial', { ...opts, boxWidth: limit, overflow });+ const reference = layoutText(expected, atlas, 'Arial', opts);+ expect(actual).toEqual(reference);+ }+ }+ }+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex bbe006ad2..217b8cbba 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'perf(text): trim overflowing plain text with prefix advances':+ { note: 'Long plain-text overflow trims with linear glyph measurement work.' },'perf(text): bound page-free glyph caches':{ note: 'Page-free text glyph caches stay bounded.' },'feat(viewport): configure transform handle size':