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(text): rasterize enlarged bitmap glyphs within atlas limits

yhh5c8f519faf4c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f639c21d4..50705513e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Bitmap glyphs survive enlarged text and camera zoom.** Canvas scratch surfaces grow to fit glyphs within the configured atlas dimensions instead of dropping tiles larger than their initial size. Resizing restores font, bold/italic style and alignment before drawing; smaller requests reuse the grown surface, and oversized tiles remain bounded by the atlas limit.
+
- **AudioPlugin checks platform initialization before subscribing to host mute.** Independent audio backend tests and direct construction no longer fail while reading an optional platform capability.
- **SDK text uploads only glyphs visible to the current draw pass.** Bitmap and SDF caches retain the complete layout while block bounds reject camera-excluded runs and partial runs compact vertices with rebased indices. Culling uses the actual camera or screen-overlay projection with the full model matrix, preserving shear, shadows and clip-edge intersections; invalid or omitted projections retain the legacy submission path.
diff --git a/sdk/src/ui/text/glyph-rasterizer.ts b/sdk/src/ui/text/glyph-rasterizer.ts
index 6c445aa87..0eaa3eafd 100644
--- a/sdk/src/ui/text/glyph-rasterizer.ts
+++ b/sdk/src/ui/text/glyph-rasterizer.ts
@@ -73,6 +73,8 @@ export interface CanvasGlyphRasterizerOptions {
padding?: number;
/** Produce an SDF (scalable) vs a plain-alpha bitmap (native AA). Default true. */
sdf?: boolean;
+ /** Maximum stored glyph dimensions, including padding. Default at least 1024. */
+ maxGlyphSize?: number;
}
type Canvas2D = PlatformCanvas;
@@ -92,6 +94,7 @@ export class CanvasGlyphRasterizer implements GlyphRasterizer {
private readonly module: ESEngineModule | null;
private readonly pad: number;
private readonly sdf: boolean;
+ private readonly maxGlyphSize: number;
private readonly canvas: Canvas2D;
private readonly ctx: Ctx2D | null;
@@ -102,10 +105,14 @@ export class CanvasGlyphRasterizer implements GlyphRasterizer {
this.renderSize = opts.renderSize ?? 48;
this.pad = opts.padding ?? 6;
this.sdf = opts.sdf ?? true;
+ const requestedMax = opts.maxGlyphSize;
+ this.maxGlyphSize = requestedMax !== undefined && Number.isFinite(requestedMax)
+ ? Math.max(0, Math.floor(requestedMax))
+ : Math.max(1024, Math.ceil(this.renderSize * 2 + this.pad * 2));
// Scratch canvas sized for the largest glyph (em + ascenders + padding),
// at supersampled resolution on the SDF path.
const ss = this.sdf ? SDF_SUPERSAMPLE : 1;
- const dim = Math.ceil((this.renderSize * 2 + this.pad * 2) * ss);
+ const dim = Math.max(1, Math.min(Math.ceil(this.renderSize * 2 + this.pad * 2), this.maxGlyphSize)) * ss;
this.canvas = platformCreateCanvas(dim, dim);
this.ctx = this.canvas.getContext('2d', { willReadFrequently: true });
}
@@ -120,7 +127,8 @@ export class CanvasGlyphRasterizer implements GlyphRasterizer {
const ss = this.sdf ? SDF_SUPERSAMPLE : 1;
const weight = (style & FONT_STYLE_BOLD) ? 'bold ' : '';
const italic = (style & FONT_STYLE_ITALIC) ? 'italic ' : '';
- ctx.font = `${italic}${weight}${pixelSize * ss}px ${fontFamily}`;
+ const font = `${italic}${weight}${pixelSize * ss}px ${fontFamily}`;
+ ctx.font = font;
ctx.textBaseline = 'alphabetic';
ctx.textAlign = 'left';
@@ -144,7 +152,15 @@ export class CanvasGlyphRasterizer implements GlyphRasterizer {
const h = inkH + pad * 2;
const wSS = w * ss;
const hSS = h * ss;
- if (wSS > this.canvas.width || hSS > this.canvas.height) return null; // glyph too large for scratch
+ if (w > this.maxGlyphSize || h > this.maxGlyphSize) return null;
+ if (wSS > this.canvas.width || hSS > this.canvas.height) {
+ if (wSS > this.canvas.width) this.canvas.width = wSS;
+ if (hSS > this.canvas.height) this.canvas.height = hSS;
+ // Canvas resizing resets drawing state, even when the context survives.
+ ctx.font = font;
+ ctx.textBaseline = 'alphabetic';
+ ctx.textAlign = 'left';
+ }
ctx.clearRect(0, 0, wSS, hSS);
ctx.fillStyle = '#ffffff';
diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.ts
index 9b343e1c3..6ea38f015 100644
--- a/sdk/src/ui/text/text-renderer.ts
+++ b/sdk/src/ui/text/text-renderer.ts
@@ -300,7 +300,7 @@ export class SdfTextRenderer {
this.sdf = opts.sdf ?? true;
const rasterizer = platformHasGlyphRasterizer()
? new NativeGlyphRasterizer({ renderSize: opts.renderSize, padding: opts.padding, sdf: this.sdf })
- : new CanvasGlyphRasterizer(module, opts);
+ : new CanvasGlyphRasterizer(module, { ...opts, maxGlyphSize: Math.min(opts.maxGlyphSize ?? Infinity, opts.pageSize ?? 1024) });
this.atlas = new GlyphAtlas(
rasterizer,
opts.metricsOnly ? null : new EngineAtlasPageStore(module),
diff --git a/sdk/tests/glyph-rasterizer-growth.test.ts b/sdk/tests/glyph-rasterizer-growth.test.ts
new file mode 100644
index 000000000..fee6a9732
--- /dev/null
+++ b/sdk/tests/glyph-rasterizer-growth.test.ts
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { beforeEach, expect, it, vi } from 'vitest';
+import type { PlatformCanvas, PlatformCanvas2DContext } from '../src/platform/types';
+import { CanvasGlyphRasterizer } from '../src/ui/text/glyph-rasterizer';
+
+const canvases = vi.hoisted(() => [] as Array<{canvas: PlatformCanvas; ctx: PlatformCanvas2DContext; draws: string[]}>);
+vi.mock('../src/platform', () => ({ platformCreateCanvas: (width: number, height: number) => makeCanvas(width, height) }));
+function makeCanvas(width: number, height: number): PlatformCanvas {
+ const draws: string[] = [];
+ const ctx: PlatformCanvas2DContext = {
+ font: '10px sans-serif', textBaseline: 'alphabetic', textAlign: 'start', fillStyle: '#000000',
+ clearRect: () => {}, drawImage: () => {},
+ measureText: () => {
+ const size = Number(/([\d.]+)px/.exec(ctx.font)![1]);
+ return { width: size * .8, actualBoundingBoxLeft: size * .2,
+ actualBoundingBoxRight: size * .75, actualBoundingBoxAscent: size * .7, actualBoundingBoxDescent: size * .2 };
+ },
+ fillText: () => { draws.push(`${ctx.font}|${ctx.textBaseline}|${ctx.textAlign}`); },
+ getImageData: (_x, _y, w, h) => ({ width: w, height: h, data: new Uint8ClampedArray(w * h * 4).fill(255) }),
+ };
+ const reset = () => { ctx.font = '10px sans-serif'; ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'start'; };
+ const canvas: PlatformCanvas = {
+ get width() { return width; }, set width(value) { width = value; reset(); },
+ get height() { return height; }, set height(value) { height = value; reset(); },
+ getContext: () => ctx,
+ };
+ canvases.push({ canvas, ctx, draws });
+ return canvas;
+}
+beforeEach(() => { canvases.length = 0; });
+
+it('grows for a large bitmap glyph and restores font, style and alignment after resizing', () => {
+ const rasterizer = new CanvasGlyphRasterizer(null, { renderSize: 64, sdf: false });
+ expect(rasterizer.rasterize(87, 'Arial', 3, 32)).not.toBeNull();
+ const large = rasterizer.rasterize(87, 'Arial', 3, 256)!;
+ expect(large).not.toBeNull();
+ expect(large.advance).toBeCloseTo(204.8);
+ expect(large.bearingX).toBeCloseTo(-57.2);
+ expect(large.pixels.length).toBe(large.width * large.height * 4);
+ expect(canvases[0].canvas.width).toBeGreaterThan(140);
+ expect(canvases[0].canvas.height).toBeGreaterThan(140);
+ expect(canvases[0].draws.at(-1)).toBe('italic bold 256px Arial|alphabetic|left');
+ const dimensions = [canvases[0].canvas.width, canvases[0].canvas.height];
+ expect(rasterizer.rasterize(87, 'Arial', 3, 32)).not.toBeNull();
+ expect([canvases[0].canvas.width, canvases[0].canvas.height]).toEqual(dimensions);
+});
+
+it('rejects tiles exceeding the atlas budget without growing the scratch canvas', () => {
+ const rasterizer = new CanvasGlyphRasterizer(null, { renderSize: 64, sdf: false, maxGlyphSize: 64 });
+ const dimensions = [canvases[0].canvas.width, canvases[0].canvas.height];
+ expect(dimensions).toEqual([64, 64]);
+ expect(rasterizer.rasterize(87, 'Arial', 0, 256)).toBeNull();
+ expect([canvases[0].canvas.width, canvases[0].canvas.height]).toEqual(dimensions);
+ expect(canvases[0].draws).toEqual([]);
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0bc3ffcf7..f4eea465f 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -30,6 +30,8 @@ export const NOTES_FLOOR = 'v0.65.0';
export const NOTED = {
'feat(storage): await asynchronous host persistence':
{ note: 'Storage exposes asynchronous write acknowledgment.' },
+ 'fix(text): rasterize enlarged bitmap glyphs within atlas limits':
+ { note: 'Bitmap glyphs survive enlarged text and camera zoom.' },
'feat(crazygames): add score submissions and saved training credits':
{ note: 'CrazyGames scores and saved training credits follow platform contracts.' },
'feat(settings): configure CrazyGames client score encryption key':