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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @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/text-transform';
function makeAtlas(calls: Array<{ cp: number; style: number }>): GlyphAtlas {
const rasterizer: GlyphRasterizer = {
renderSize: 48,
spread: 6,
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
});
});
describe('rich text layout — inline images', () => {
it('places an <img> on the pen after the preceding text and advances by its width', () => {
const atlas = makeAtlas([]);
// Each glyph advances 11 * (24/48) = 5.5px; "AB" ⇒ pen at 11 before the image.
const layout = layoutRichLine(
'AB<img src="heart" width=16 height=16/>C',
atlas, 'Arial', { fontSizePx: 24, color: [1, 1, 1, 1] });
expect(layout.images).toHaveLength(1);
const img = layout.images![0];
expect(img.src).toBe('heart');
expect(img.w).toBe(16);
expect(img.h).toBe(16);
expect(img.x).toBeCloseTo(11, 5);
expect(img.y).toBeCloseTo(0, 5); // baseline valign ⇒ bottom on the baseline
// The trailing "C" glyph starts after the 16px image (pen = 11 + 16 = 27).
expect(layout.glyphs[layout.glyphs.length - 1].x0).toBeGreaterThan(27 - 1);
});
it('scales the box and applies tint + offset', () => {
const atlas = makeAtlas([]);
const layout = layoutRichLine(
'<img src="coin" width=10 height=10 scale=2 offsetX=3 offsetY=-4 tint=#ff8000/>',
atlas, 'Arial', { fontSizePx: 24, color: [1, 1, 1, 1] });
const img = layout.images![0];
expect(img.w).toBe(20);
expect(img.h).toBe(20);
expect(img.x).toBeCloseTo(3, 5); // penX 0 + offsetX 3
expect(img.y).toBeCloseTo(-4, 5); // baseline 0 + offsetY -4
expect(img.tint).not.toBeNull();
expect(img.tint![0]).toBeCloseTo(1, 2); // r = ff
expect(img.tint![2]).toBeCloseTo(0, 2); // b = 00
});
it('valign top/baseline/bottom order the box vertically', () => {
const atlas = makeAtlas([]);
const mk = (v: string) => layoutRichLine(`<img src="x" width=16 height=16${v}/>`, atlas, 'Arial', { fontSizePx: 24, color: [1, 1, 1, 1] }).images![0].y;
const top = mk(' valign=top');
const base = mk(''); // baseline: bottom on y = 0
const bottom = mk(' valign=bottom');
expect(base).toBeCloseTo(0, 5);
expect(top).toBeGreaterThan(base); // y-up: top anchor raises the box
expect(bottom).toBeLessThan(base); // bottom anchor drops it below the baseline
});
it('plain rich text yields an empty images array', () => {
const atlas = makeAtlas([]);
const layout = layoutRichLine('<b>hi</b>', atlas, 'Arial', { fontSizePx: 24, color: [1, 1, 1, 1] });
expect(layout.images).toEqual([]);
});
});