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.

perf(text): wrap plain text with incremental glyph advances

yhhb83235701a4e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c3ed40fe2..8713a8d16 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Plain-text wrapping avoids repeated whole-line glyph measurements.** The atlas-backed wrap path appends glyph advances in their original order while sharing the greedy wrap decisions with arbitrary string measurement. Long words and wide lines stay linear in glyph queries; spaces, supplementary characters and signed spacing preserve prior line breaks and geometry. Canvas string measurement retains its existing behavior.
+
- **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.
diff --git a/sdk/src/ui/text/layout.ts b/sdk/src/ui/text/layout.ts
index ae05ebeab..439b6be0a 100644
--- a/sdk/src/ui/text/layout.ts
+++ b/sdk/src/ui/text/layout.ts
@@ -269,43 +269,70 @@ export function measurePrefixWidths(
* it, so measured heights match rendered wraps.
*/
export function wrapByMeasure(text: string, measure: (s: string) => number, maxWidth: number): string[] {
+ return wrapMeasured(text, measure, maxWidth);
+}
+
+function wrapMeasured(
+ text: string, measure: (s: string) => number, maxWidth: number,
+ appendWidth?: (width: number, suffix: string) => number,
+): string[] {
const out: string[] = [];
let cur = '';
- const flush = () => { const t = cur.replace(/\s+$/, ''); if (t) out.push(t); cur = ''; };
+ let curWidth = 0;
+ const flush = () => { const t = cur.replace(/\s+$/, ''); if (t) out.push(t); cur = ''; curWidth = 0; };
+ const appended = (suffix: string) => appendWidth ? appendWidth(curWidth, suffix) : measure(cur + suffix);
const charBreak = (token: string) => {
for (const ch of token) {
- if (cur && measure(cur + ch) > maxWidth) flush();
+ let width = cur ? appended(ch) : appendWidth?.(0, ch) ?? 0;
+ if (cur && width > maxWidth) {
+ flush();
+ width = appendWidth?.(0, ch) ?? 0;
+ }
cur += ch;
+ curWidth = width;
}
};
for (const token of text.split(/(\s+)/)) {
if (token === '') continue;
- if (/^\s+$/.test(token)) { if (cur) cur += token; continue; } // keep inter-word spaces, drop leading
+ if (/^\s+$/.test(token)) {
+ if (cur) { if (appendWidth) curWidth = appendWidth(curWidth, token); cur += token; }
+ continue;
+ }
if (!cur) {
- if (measure(token) <= maxWidth) cur = token;
+ const width = measure(token);
+ if (width <= maxWidth) { cur = token; curWidth = width; }
else charBreak(token);
- } else if (measure(cur + token) <= maxWidth) {
- cur += token;
} else {
- flush();
- if (measure(token) <= maxWidth) cur = token;
- else charBreak(token);
+ const width = appended(token);
+ if (width <= maxWidth) { cur += token; curWidth = width; }
+ else {
+ flush();
+ const tokenWidth = measure(token);
+ if (tokenWidth <= maxWidth) { cur = token; curWidth = tokenWidth; }
+ else charBreak(token);
+ }
}
}
flush();
return out.length ? out : [''];
}
-/**
- * Greedy word-wrap a single line to `maxWidth` (display px) using the glyph
- * atlas's advances. Thin wrapper over {@link wrapByMeasure}.
- */
+/** Greedy wrap using ordered glyph advances without remeasuring the current line. */
export function wrapLine(
text: string, atlas: GlyphAtlas, fontFamily: string, fontSizePx: number,
style: number, maxWidth: number, letterSpacing = 0,
): string[] {
- return wrapByMeasure(text, (s) => measureWidth(s, atlas, fontFamily, fontSizePx, style, letterSpacing), maxWidth);
+ const pixelSize = atlas.pixelSizeFor(fontSizePx);
+ const scale = fontSizePx / pixelSize;
+ const append = (width: number, suffix: string): number => {
+ for (const ch of suffix) {
+ const glyph = atlas.getGlyph(ch.codePointAt(0)!, fontFamily, style, pixelSize);
+ if (glyph) width += glyph.advance * scale + letterSpacing;
+ }
+ return width;
+ };
+ return wrapMeasured(text, s => append(0, s), maxWidth, append);
}
/**
diff --git a/sdk/tests/ui-text-wrap.test.ts b/sdk/tests/ui-text-wrap.test.ts
index 9d2530d6e..dd3fd5f87 100644
--- a/sdk/tests/ui-text-wrap.test.ts
+++ b/sdk/tests/ui-text-wrap.test.ts
@@ -9,7 +9,7 @@ import { describe, it, expect } from 'vitest';
import {
GlyphAtlas, type GlyphRasterizer, type AtlasPageStore, type RasterGlyph,
} from '../src/ui/text/glyph-atlas';
-import { wrapLine, wrapRichRuns, measureWidth, layoutText } from '../src/ui/text/layout';
+import { wrapLine, wrapByMeasure, wrapRichRuns, measureWidth, layoutText } from '../src/ui/text/layout';
import type { TextSegment } from '../src/ui/text/rich-text-parser';
function makeAtlas(): GlyphAtlas {
@@ -91,3 +91,23 @@ describe('REARCH_GUI P1.4c: word-wrap', () => {
expect(layout.glyphs[5].y0).toBeLessThan(layout.glyphs[0].y0);
});
});
+
+
+it.each(['A'.repeat(4000), 'word '.repeat(800)])('wraps long lines with linear glyph queries', text => {
+ const atlas = makeAtlas();const get = atlas.getGlyph.bind(atlas);let calls = 0;
+ atlas.getGlyph = (...args) => { calls++; return get(...args); };
+ const lines = wrapLine(text, atlas, 'Arial', F, 0, 11000);
+ expect(lines.join('').length).toBeGreaterThan(0);
+ expect(calls).toBeLessThan(text.length * 5);
+});
+
+it.each([-5, 0, 1 / 3])('preserves arbitrary-measure wrap decisions at exact boundaries (spacing=%s)', spacing => {
+ const atlas = makeAtlas();
+ for (const text of [' A😀 测试 B\tC ', '文'.repeat(45), 'word '.repeat(40), 'A\u2003B\u00a0😀 C']) {
+ const measure = (s: string) => measureWidth(s, atlas, 'Arial', F, 0, spacing);
+ for (const width of [1, 5.5, 25, measure('A😀 测试'), 111.1]) {
+ expect(wrapLine(text, atlas, 'Arial', F, 0, width, spacing))
+ .toEqual(wrapByMeasure(text, measure, width));
+ }
+ }
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 217b8cbba..315d7d2a4 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): wrap plain text with incremental glyph advances':
+ { note: 'Plain-text wrapping avoids repeated whole-line glyph measurements.' },
'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':