From 0f2224ee4c407fb2a89cc797681165441724ff1e Mon Sep 17 00:00:00 2001 From: eXecutable <3399019+eXecutable@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:45:42 +0000 Subject: [PATCH 1/2] Allow for text renderers to provide their own cache key --- .../CoreTextNode.canvasTextureLeak.test.ts | 2 + src/core/CoreTextNode.test.ts | 33 +++++++++-- src/core/CoreTextNode.ts | 11 ++-- src/core/Stage.canvasTextLatency.test.ts | 2 + src/core/text-rendering/CanvasTextRenderer.ts | 3 +- src/core/text-rendering/SdfTextRenderer.ts | 1 + src/core/text-rendering/TextRenderer.ts | 11 ++++ src/core/text-rendering/Utils.ts | 51 +++++++++++++++++ src/core/textures/CanvasTextTexture.ts | 56 +------------------ 9 files changed, 104 insertions(+), 66 deletions(-) diff --git a/src/core/CoreTextNode.canvasTextureLeak.test.ts b/src/core/CoreTextNode.canvasTextureLeak.test.ts index 2761f5e6d..2ba86eb4f 100644 --- a/src/core/CoreTextNode.canvasTextureLeak.test.ts +++ b/src/core/CoreTextNode.canvasTextureLeak.test.ts @@ -29,6 +29,7 @@ import { CoreTextureManager } from './CoreTextureManager.js'; import { CoreNode } from './CoreNode.js'; import { CoreTextNode } from './CoreTextNode.js'; import { Texture, TextureType } from './textures/Texture.js'; +import { makeCanvasTextCacheKey } from './text-rendering/Utils.js'; import { TextureMemoryManager } from './TextureMemoryManager.js'; import type { TextureMemoryManagerSettings } from './TextureMemoryManager.js'; import { Stage } from './Stage.js'; @@ -79,6 +80,7 @@ function makeCanvasTextRenderer() { ), addQuads: vi.fn(), renderQuads: vi.fn(), + getCacheKey: makeCanvasTextCacheKey, } as unknown as TextRenderer; } diff --git a/src/core/CoreTextNode.test.ts b/src/core/CoreTextNode.test.ts index 7c1852095..4ccc11b26 100644 --- a/src/core/CoreTextNode.test.ts +++ b/src/core/CoreTextNode.test.ts @@ -342,7 +342,7 @@ describe('CoreTextNode', () => { type: 'canvas', width: 100, height: 20, - renderSource: () => ({} as ImageBitmap), + renderSource: () => ({}) as ImageBitmap, hasRemainingText: false, remainingLines: 0, }; @@ -557,7 +557,7 @@ describe('CoreTextNode', () => { type: 'canvas', width: 100, height: 20, - renderSource: () => ({} as ImageBitmap), + renderSource: () => ({}) as ImageBitmap, hasRemainingText: false, remainingLines: 0, }; @@ -572,6 +572,29 @@ describe('CoreTextNode', () => { expect(mockTextRenderer.renderQuads).not.toHaveBeenCalled(); expect(addQuad).toHaveBeenCalled(); }); + + it('keys the canvas texture with the renderer getCacheKey', () => { + const createTexture = vi.fn().mockReturnValue(stage.defaultTexture); + (stage as any).txManager = { createTexture }; + const getCacheKey = vi.fn().mockReturnValue('custom-key'); + const node = new CoreTextNode(stage, defaultTextProps, { + ...mockTextRenderer, + type: 'canvas', + getCacheKey, + }); + + (node as any).handleRenderResult({ + type: 'canvas', + width: 100, + height: 20, + renderSource: () => ({}) as ImageBitmap, + hasRemainingText: false, + remainingLines: 0, + }); + + expect(getCacheKey).toHaveBeenCalledWith(defaultTextProps); + expect(createTexture.mock.calls[0][1].key).toBe('custom-key'); + }); }); // ------------------------------------------------------------------------- @@ -661,9 +684,9 @@ describe('CoreTextNode', () => { mockTextRenderer, ); - (node)[prop] = undefined; + node[prop] = undefined; - expect((node)[prop]).toBe(fallback); + expect(node[prop]).toBe(fallback); }, ); @@ -679,7 +702,7 @@ describe('CoreTextNode', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access (node as any)[prop] = kept; - expect((node)[prop]).toBe(kept); + expect(node[prop]).toBe(kept); }, ); diff --git a/src/core/CoreTextNode.ts b/src/core/CoreTextNode.ts index 4d7241697..d28ba1df6 100644 --- a/src/core/CoreTextNode.ts +++ b/src/core/CoreTextNode.ts @@ -38,10 +38,7 @@ import type { import type { RectWithValid } from './lib/utils.js'; import type { CoreRenderer } from './renderers/CoreRenderer.js'; import type { TextureLoadedEventHandler } from './textures/Texture.js'; -import { - makeCanvasTextCacheKey, - type CanvasTextTexture, -} from './textures/CanvasTextTexture.js'; +import type { CanvasTextTexture } from './textures/CanvasTextTexture.js'; import { Matrix3d } from './lib/Matrix3d.js'; import type { WebGlCtxTexture } from './renderers/webgl/WebGlCtxTexture.js'; export interface CoreTextNodeProps extends CoreNodeProps, TrProps { @@ -140,7 +137,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps { * Release this node's reference to its canvas text texture. * * @remarks - * Textures are shared via the keyCache (see `makeCanvasTextCacheKey`), so + * Textures are shared via the keyCache (see `TextRenderer.getCacheKey`), so * ownership/cancel decisions live on the texture itself * (`CanvasTextTexture.releaseOwner`): this only drops the node-side * reference. Safe to call multiple times. @@ -284,7 +281,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps { * Override CoreNode's update method to handle text-specific updates */ override update(delta: number, parentClippingRect: RectWithValid): void { - this.renderText(this.textProps.forceLoad) + this.renderText(this.textProps.forceLoad); // First run the standard CoreNode update super.update(delta, parentClippingRect); } @@ -362,7 +359,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps { rich: this.textProps.richText === true, // textProps is passed by reference (no copy): the key builder only // reads it synchronously to build the key string. - key: makeCanvasTextCacheKey(tProps), + key: this.textRenderer.getCacheKey(tProps), }, ); this._canvasTexture = newTexture; diff --git a/src/core/Stage.canvasTextLatency.test.ts b/src/core/Stage.canvasTextLatency.test.ts index b94e2f6d2..a10d696b4 100644 --- a/src/core/Stage.canvasTextLatency.test.ts +++ b/src/core/Stage.canvasTextLatency.test.ts @@ -3,6 +3,7 @@ import { CoreTextureManager } from './CoreTextureManager.js'; import { CoreNode, UpdateType } from './CoreNode.js'; import { CoreTextNode } from './CoreTextNode.js'; import { ImageTexture } from './textures/ImageTexture.js'; +import { makeCanvasTextCacheKey } from './text-rendering/Utils.js'; import type { Texture } from './textures/Texture.js'; import { Stage } from './Stage.js'; import { createBound } from './lib/utils.js'; @@ -77,6 +78,7 @@ function makeCanvasTextRenderer() { ), addQuads: vi.fn(), renderQuads: vi.fn(), + getCacheKey: makeCanvasTextCacheKey, } as unknown as TextRenderer; } diff --git a/src/core/text-rendering/CanvasTextRenderer.ts b/src/core/text-rendering/CanvasTextRenderer.ts index 843e0973d..bec78bf86 100644 --- a/src/core/text-rendering/CanvasTextRenderer.ts +++ b/src/core/text-rendering/CanvasTextRenderer.ts @@ -29,7 +29,7 @@ import type { } from './TextRenderer.js'; import * as CanvasFontHandler from './CanvasFontHandler.js'; import type { CoreTextNodeProps } from '../CoreTextNode.js'; -import { hasZeroWidthSpace } from './Utils.js'; +import { hasZeroWidthSpace, makeCanvasTextCacheKey } from './Utils.js'; import { mapTextLayout } from './TextLayoutEngine.js'; import { parseRichText, ParseResult } from './RichTextParser.js'; import { @@ -729,6 +729,7 @@ const CanvasTextRenderer = { renderQuads, init, clearCache, + getCacheKey: makeCanvasTextCacheKey, }; export default CanvasTextRenderer; diff --git a/src/core/text-rendering/SdfTextRenderer.ts b/src/core/text-rendering/SdfTextRenderer.ts index 0e392f0b9..761341f3e 100644 --- a/src/core/text-rendering/SdfTextRenderer.ts +++ b/src/core/text-rendering/SdfTextRenderer.ts @@ -994,6 +994,7 @@ const SdfTextRenderer = { renderQuads, init, clearCache, + getCacheKey: getLayoutCacheKey, }; export default SdfTextRenderer; diff --git a/src/core/text-rendering/TextRenderer.ts b/src/core/text-rendering/TextRenderer.ts index c9a060844..192b6a5e7 100644 --- a/src/core/text-rendering/TextRenderer.ts +++ b/src/core/text-rendering/TextRenderer.ts @@ -531,6 +531,17 @@ export interface TextRenderer { ) => void; init: (stage: Stage) => void; clearCache: () => void; + /** + * Build the key under which this renderer's output for `props` is reused. + * + * @remarks + * Canvas, the keyCache key for the text raster. + * - it must cover every prop that changes the **drawn pixels** + * - return `null` for an uncached, single-owner texture + * SDF, the layout cache key. + * - it must cover every prop that changes the **layout**. + */ + getCacheKey: (props: CoreTextNodeProps) => string | null; } /** diff --git a/src/core/text-rendering/Utils.ts b/src/core/text-rendering/Utils.ts index 193e77171..184e5db6a 100644 --- a/src/core/text-rendering/Utils.ts +++ b/src/core/text-rendering/Utils.ts @@ -98,6 +98,57 @@ export function tokenizeString(tokenRegex: RegExp, text: string): string[] { return final.filter((word) => word != ''); } +/** + * Raster inputs that determine canvas text pixels. + * + * @remarks + * Keep in sync with what `CanvasTextRenderer` consumes: `contain`, + * `offsetY` and `verticalAlign` only affect node transforms (never pixels) + * and are intentionally excluded, as is `color` for plain text (plain + * rasters are white alpha masks tinted per node at draw time — rich text + * bakes span colors, so it includes `color`). + */ +export interface CanvasTextCacheKeyProps { + text: string; + richText: boolean; + fontFamily: string; + fontStyle: string; + fontSize: number; + letterSpacing: number; + lineHeight: number; + textAlign: string; + maxLines: number; + overflowSuffix: string; + wordBreak: string; + maxWidth: number; + maxHeight: number; + color: number; +} + +/** + * Build a stable keyCache key for a canvas text raster. + */ +export function makeCanvasTextCacheKey(props: CanvasTextCacheKeyProps): string { + return JSON.stringify([ + 'CanvasText', + props.richText === true ? 1 : 0, + props.fontFamily, + props.fontStyle, + props.fontSize, + props.letterSpacing, + props.lineHeight, + props.textAlign, + props.maxLines, + props.overflowSuffix, + props.wordBreak, + props.maxWidth, + props.maxHeight, + // Rich text bakes the node color into uncolored spans; plain text does not. + props.richText === true ? props.color : 0, + props.text, + ]); +} + export function getLayoutCacheKey(props: CoreTextNodeProps): string { return `${props.text}-${props.fontFamily}-${props.fontSize}-${props.letterSpacing}-${props.lineHeight}-${props.maxHeight}-${props.maxWidth}-${props.textAlign}-${props.wordBreak}-${props.maxLines}-${props.overflowSuffix}-${props.richText}`; } diff --git a/src/core/textures/CanvasTextTexture.ts b/src/core/textures/CanvasTextTexture.ts index 66ee2ae26..dd67920d2 100644 --- a/src/core/textures/CanvasTextTexture.ts +++ b/src/core/textures/CanvasTextTexture.ts @@ -34,7 +34,8 @@ export interface CanvasTextTextureProps { */ rich?: boolean; /** - * Stable cache key for the raster inputs (see {@link makeCanvasTextCacheKey}). + * Stable cache key for the raster inputs, built by the text renderer's + * `getCacheKey`. * * @remarks * Textures created with equal keys share one raster via the texture @@ -45,57 +46,6 @@ export interface CanvasTextTextureProps { key?: string | null; } -/** - * Raster inputs that determine canvas text pixels. - * - * @remarks - * Keep in sync with what `CanvasTextRenderer` consumes: `contain`, - * `offsetY` and `verticalAlign` only affect node transforms (never pixels) - * and are intentionally excluded, as is `color` for plain text (plain - * rasters are white alpha masks tinted per node at draw time — rich text - * bakes span colors, so it includes `color`). - */ -export interface CanvasTextCacheKeyProps { - text: string; - richText: boolean; - fontFamily: string; - fontStyle: string; - fontSize: number; - letterSpacing: number; - lineHeight: number; - textAlign: string; - maxLines: number; - overflowSuffix: string; - wordBreak: string; - maxWidth: number; - maxHeight: number; - color: number; -} - -/** - * Build a stable keyCache key for a canvas text raster. - */ -export function makeCanvasTextCacheKey(props: CanvasTextCacheKeyProps): string { - return JSON.stringify([ - 'CanvasText', - props.richText === true ? 1 : 0, - props.fontFamily, - props.fontStyle, - props.fontSize, - props.letterSpacing, - props.lineHeight, - props.textAlign, - props.maxLines, - props.overflowSuffix, - props.wordBreak, - props.maxWidth, - props.maxHeight, - // Rich text bakes the node color into uncolored spans; plain text does not. - props.richText === true ? props.color : 0, - props.text, - ]); -} - /** * Rasterized canvas text with a regenerable source. The draw callback must * capture a snapshot of the text layout, not a mutable CoreTextNode's props. @@ -182,7 +132,7 @@ export class CanvasTextTexture extends Texture { this.disposeSource(); } - // Textures with an explicit stable key (see makeCanvasTextCacheKey) are + // Textures with an explicit stable key (see TextRenderer.getCacheKey) are // shared via the manager's keyCache, which is stage-scoped; ownership is // tracked per node via renderableOwners. Key-less textures stay uncached. static override makeCacheKey(props: CanvasTextTextureProps): string | false { From 54aad9b90d88fc6e877c80a5d6b8969fd363df9f Mon Sep 17 00:00:00 2001 From: eXecutable <3399019+eXecutable@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:54:37 +0000 Subject: [PATCH 2/2] fix build --- src/core/CoreTextNode.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/CoreTextNode.test.ts b/src/core/CoreTextNode.test.ts index 4ccc11b26..1b262f9ec 100644 --- a/src/core/CoreTextNode.test.ts +++ b/src/core/CoreTextNode.test.ts @@ -593,7 +593,10 @@ describe('CoreTextNode', () => { }); expect(getCacheKey).toHaveBeenCalledWith(defaultTextProps); - expect(createTexture.mock.calls[0][1].key).toBe('custom-key'); + expect(createTexture).toHaveBeenCalledWith( + 'CanvasTextTexture', + expect.objectContaining({ key: 'custom-key' }), + ); }); });