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
feat(text): rich text in the SDF path — per-run color/size/bold (REARCH_GUI P1.4a)
esenginee73fad2f3f4a
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.tsindex c25fd0780..32b3a4f3d 100644--- a/sdk/src/ui/index.ts+++ b/sdk/src/ui/index.ts@@ -405,9 +405,11 @@ export { EngineAtlasPageStore } from './text/atlas-page-store';export {layoutLine,+ layoutRichLine,buildGlyphVertices,type TextLayout,type TextLayoutOptions,+ type RichTextLayoutOptions,type LaidGlyph,type RGBA,type GlyphVertexData,diff --git a/sdk/src/ui/text/layout.ts b/sdk/src/ui/text/layout.tsindex 8d5c6431b..be3637fb2 100644--- a/sdk/src/ui/text/layout.ts+++ b/sdk/src/ui/text/layout.ts@@ -13,6 +13,8 @@*/import type { GlyphAtlas } from './glyph-atlas';import { TEXT_VERTEX_FLOATS } from './submit';+import { parseRichText } from '../RichTextParser';+import { UI_TEXT_BOLD, UI_TEXT_ITALIC } from './ui-text';export interface TextLayoutOptions {/** Display font size in px. */@@ -21,12 +23,17 @@ export interface TextLayoutOptions {letterSpacing?: number;}-/** One positioned glyph quad: atlas UVs + local-space corners (y-up). */+/** RGBA color, channels in [0,1]. */+export type RGBA = readonly [number, number, number, number];++/** One positioned glyph quad: atlas UVs + local-space corners (y-up). `color` is+ * set only by rich text (per-run); single-style layout leaves it for the caller. */export interface LaidGlyph {u0: number; v0: number; u1: number; v1: number;x0: number; y0: number; // bottom-leftx1: number; y1: number; // top-rightpageId: number;+ color?: RGBA;}export interface TextLayout {@@ -72,9 +79,6 @@ export function layoutLine(return { glyphs, width: penX, lineHeight: opts.fontSizePx };}-/** RGBA color, channels in [0,1]. */-export type RGBA = readonly [number, number, number, number];-export interface GlyphVertexData {vertices: Float32Array; // TEXT_VERTEX_FLOATS per vertex, 4 verts/glyphindices: Uint16Array; // 6 per glyph@@ -96,10 +100,10 @@ export function buildGlyphVertices(const n = glyphs.length;const vertices = new Float32Array(n * 4 * TEXT_VERTEX_FLOATS);const indices = new Uint16Array(n * 6);- const [r, g, b, a] = color;for (let i = 0; i < n; i++) {const gl = glyphs[i];+ const [r, g, b, a] = gl.color ?? color; // per-glyph color (rich text) falls back to the batch colorconst x0 = gl.x0 + originX, x1 = gl.x1 + originX;const y0 = gl.y0 + originY, y1 = gl.y1 + originY;const vo = i * 4 * TEXT_VERTEX_FLOATS;@@ -125,3 +129,58 @@ function writeVertex(out[o] = x; out[o + 1] = y; out[o + 2] = u; out[o + 3] = v;out[o + 4] = r; out[o + 5] = g; out[o + 6] = b; out[o + 7] = a;}++export interface RichTextLayoutOptions extends TextLayoutOptions {+ /** Base color for runs that don't set their own <color=...>. */+ color: RGBA;+}++/**+ * Lay out a single line of rich text (REARCH_GUI P1.4): `<b>`, `<i>`,+ * `<color=#rrggbb[aa]>`, `<font size=N>` runs (parsed by parseRichText) become+ * glyphs carrying per-run color + size + bold/italic style. Image runs (`<img>`)+ * are skipped for now. Each run scales by its own fontSize / atlas.renderSize;+ * all runs share the baseline (y = 0). Pure → unit-testable.+ */+export function layoutRichLine(+ content: string,+ atlas: GlyphAtlas,+ fontFamily: string,+ opts: RichTextLayoutOptions,+ baseStyle = 0,+): TextLayout {+ const spacing = opts.letterSpacing ?? 0;+ const glyphs: LaidGlyph[] = [];+ let penX = 0;+ let lineHeight = opts.fontSizePx;++ for (const run of parseRichText(content)) {+ if (run.type !== 'text') continue; // embedded images: deferred+ const runSize = run.fontSize ?? opts.fontSizePx;+ const scale = runSize / atlas.renderSize;+ const style = baseStyle | (run.bold ? UI_TEXT_BOLD : 0) | (run.italic ? UI_TEXT_ITALIC : 0);+ const color: RGBA = run.color+ ? [run.color.r, run.color.g, run.color.b, run.color.a]+ : opts.color;+ if (runSize > lineHeight) lineHeight = runSize;++ for (const ch of run.text) {+ const cp = ch.codePointAt(0);+ if (cp === undefined) continue;+ const gph = atlas.getGlyph(cp, fontFamily, style);+ if (!gph) continue;+ if (gph.width > 0 && gph.height > 0) {+ const x0 = penX + gph.bearingX * scale;+ const y1 = gph.bearingY * scale;+ glyphs.push({+ u0: gph.u0, v0: gph.v0, u1: gph.u1, v1: gph.v1,+ x0, y0: y1 - gph.height * scale, x1: x0 + gph.width * scale, y1,+ pageId: gph.pageId, color,+ });+ }+ penX += gph.advance * scale + spacing;+ }+ }++ return { glyphs, width: penX, lineHeight };+}diff --git a/sdk/src/ui/text/plugin.ts b/sdk/src/ui/text/plugin.tsindex e5b33939b..d7b6540ca 100644--- a/sdk/src/ui/text/plugin.ts+++ b/sdk/src/ui/text/plugin.ts@@ -46,6 +46,7 @@ export class UITextPlugin implements Plugin {fontSizePx: t.fontSizePx,color: [t.color.r, t.color.g, t.color.b, t.color.a],style: t.style,+ richText: t.richText,},this.matrix_,entity as number,diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.tsindex fe99b142c..89887c904 100644--- a/sdk/src/ui/text/text-renderer.ts+++ b/sdk/src/ui/text/text-renderer.ts@@ -12,7 +12,7 @@ import type { ESEngineModule } from '../../wasm';import { GlyphAtlas } from './glyph-atlas';import { CanvasGlyphRasterizer, type CanvasGlyphRasterizerOptions } from './glyph-rasterizer';import { EngineAtlasPageStore } from './atlas-page-store';-import { layoutLine, buildGlyphVertices, type LaidGlyph, type RGBA } from './layout';+import { layoutLine, layoutRichLine, buildGlyphVertices, type LaidGlyph, type RGBA } from './layout';import { submitTextBatch } from './submit';/** Receives one (vertices, indices) batch per atlas page used by the text. */@@ -26,6 +26,8 @@ export interface DrawTextParams {originX?: number;originY?: number;style?: number;+ /** Parse `<b>/<i>/<color>/<font size>` markup (REARCH_GUI P1.4). */+ richText?: boolean;}/**@@ -34,7 +36,9 @@ export interface DrawTextParams {* and geometry are unit-testable.*/export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTextParams): void {- const layout = layoutLine(p.text, atlas, p.fontFamily, { fontSizePx: p.fontSizePx }, p.style ?? 0);+ const layout = p.richText+ ? layoutRichLine(p.text, atlas, p.fontFamily, { fontSizePx: p.fontSizePx, color: p.color }, p.style ?? 0)+ : layoutLine(p.text, atlas, p.fontFamily, { fontSizePx: p.fontSizePx }, p.style ?? 0);if (layout.glyphs.length === 0) return;// A string can reference glyphs across several atlas pages; each page is adiff --git a/sdk/src/ui/text/ui-text.ts b/sdk/src/ui/text/ui-text.tsindex 5c62c7aa1..ca314fbfa 100644--- a/sdk/src/ui/text/ui-text.ts+++ b/sdk/src/ui/text/ui-text.ts@@ -22,6 +22,8 @@ export interface UITextData {/** Bit flags: UI_TEXT_BOLD | UI_TEXT_ITALIC. */style: number;layer: number;+ /** Parse `<b>/<i>/<color>/<font size>` markup in `content`. */+ richText: boolean;}export const UIText = defineComponent<UITextData>('UIText', {@@ -31,6 +33,7 @@ export const UIText = defineComponent<UITextData>('UIText', {color: { r: 1, g: 1, b: 1, a: 1 },style: 0,layer: 0,+ richText: false,});/**diff --git a/sdk/tests/ui-text-richlayout.test.ts b/sdk/tests/ui-text-richlayout.test.tsnew file mode 100644index 000000000..398adacd8--- /dev/null+++ b/sdk/tests/ui-text-richlayout.test.ts@@ -0,0 +1,66 @@+/**+ * @file REARCH_GUI P1.4 — rich text layout for the SDF path. Verifies parseRichText+ * runs become glyphs carrying per-run color, size and bold/italic style.+ * Pure (mock atlas), so headless-tested.+ */+import { describe, it, expect } from 'vitest';+import {+ GlyphAtlas, type GlyphRasterizer, type AtlasPageStore, type RasterGlyph,+} from '../src/ui/text/glyph-atlas';+import { layoutRichLine, buildGlyphVertices } from '../src/ui/text/layout';+import { UI_TEXT_BOLD } from '../src/ui/text/ui-text';++function makeAtlas(calls: Array<{ cp: number; style: number }>): GlyphAtlas {+ const rasterizer: GlyphRasterizer = {+ renderSize: 48,+ rasterize: (cp: number, _f: string, style: number): RasterGlyph | null => {+ calls.push({ cp, style });+ return { pixels: new Uint8Array(10 * 12 * 4), width: 10, height: 12, advance: 11, bearingX: 1, bearingY: 10 };+ },+ };+ let next = 1;+ const store: AtlasPageStore = { createPage: () => next++, uploadSubRegion: () => {} };+ return new GlyphAtlas(rasterizer, store, { pageSize: 256, padding: 1 });+}++describe('REARCH_GUI P1.4: rich text layout', () => {+ it('applies per-run color, size and bold style', () => {+ const calls: Array<{ cp: number; style: number }> = [];+ const atlas = makeAtlas(calls);+ const layout = layoutRichLine(+ '<b>A</b><color=#ff0000>B</color><font size=48>C</font>',+ atlas, 'Arial',+ { fontSizePx: 24, color: [1, 1, 1, 1] },+ );++ expect(layout.glyphs.length).toBe(3);++ // A: base white, bold style passed to the rasterizer+ expect(layout.glyphs[0].color).toEqual([1, 1, 1, 1]);+ const aCall = calls.find(c => c.cp === 65)!;+ expect(aCall.style & UI_TEXT_BOLD).toBeTruthy();++ // B: red (<color=#ff0000>), non-bold+ const b = layout.glyphs[1].color!;+ expect(b[0]).toBeGreaterThan(0);+ expect(b[1]).toBe(0);+ expect(b[2]).toBe(0);+ expect(calls.find(c => c.cp === 66)!.style & UI_TEXT_BOLD).toBeFalsy();++ // C: <font size=48> ⇒ scale 48/48 = 1 (vs A's 24/48 = 0.5) ⇒ twice as wide+ const aW = layout.glyphs[0].x1 - layout.glyphs[0].x0;+ const cW = layout.glyphs[2].x1 - layout.glyphs[2].x0;+ expect(cW).toBeCloseTo(aW * 2);+ });++ it('buildGlyphVertices honors per-glyph color over the batch color', () => {+ const calls: Array<{ cp: number; style: number }> = [];+ const atlas = makeAtlas(calls);+ const layout = layoutRichLine('<color=#ff0000>B</color>', atlas, 'Arial', { fontSizePx: 24, color: [1, 1, 1, 1] });+ const { vertices } = buildGlyphVertices(layout.glyphs, [1, 1, 1, 1]);+ // vertex 0 color = the glyph's red, not the white batch color+ expect(vertices[4]).toBeGreaterThan(0); // r+ expect(vertices[5]).toBe(0); // g+ expect(vertices[6]).toBe(0); // b+ });+});