Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/core/text-rendering/SdfTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const init = (stage: Stage): void => {
};

const font: FontHandler = SdfFontHandler;
const layoutCache = new Map<string, TextLayout>();

const getLayoutCacheKey = (props: CoreTextNodeProps): string =>
`${props.fontFamily}-${props.fontSize}-${props.letterSpacing}-${props.lineHeight}-${props.maxHeight}-${props.maxWidth}-${props.textAlign}-${props.text}`;

/**
* SDF text renderer using MSDF/SDF fonts with WebGL
Expand All @@ -49,6 +53,18 @@ const renderText = (props: CoreTextNodeProps): TextRenderInfo => {
};
}

const cacheKey = getLayoutCacheKey(props);
let layout = layoutCache.get(cacheKey);
if (layout !== undefined) {
return {
remainingLines: 0,
hasRemainingText: false,
width: layout.width,
height: layout.height,
layout,
};
}

// Get font cache for this font family
const fontData = SdfFontHandler.getFontData(props.fontFamily);
if (fontData === undefined) {
Expand All @@ -60,15 +76,16 @@ const renderText = (props: CoreTextNodeProps): TextRenderInfo => {
}

// Calculate text layout and generate glyph data for caching
const layout = generateTextLayout(props, fontData);
layout = generateTextLayout(props, fontData);
layoutCache.set(cacheKey, layout);

// For SDF renderer, ImageData is null since we render via WebGL
return {
remainingLines: 0,
hasRemainingText: false,
width: layout.width,
height: layout.height,
layout, // Cache layout for addQuads
layout,
};
};

Expand Down Expand Up @@ -302,14 +319,10 @@ const generateTextLayout = (

// Calculate glyph position and atlas coordinates (in design units)
const glyphLayout: GlyphLayout = {
codepoint,
glyphId: glyph.id,
x: currentX + glyph.xoffset,
y: currentY + glyph.yoffset,
width: glyph.width,
height: glyph.height,
xOffset: glyph.xoffset,
yOffset: glyph.yoffset,
atlasX: glyph.x * invAtlasWidth,
atlasY: glyph.y * invAtlasHeight,
atlasWidth: glyph.width * invAtlasWidth,
Expand Down
31 changes: 0 additions & 31 deletions src/core/text-rendering/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,41 +236,10 @@ export interface TrProps extends TrFontProps {
* Glyph layout information for WebGL rendering
*/
export interface GlyphLayout {
/**
* Unicode codepoint
*/
codepoint: number;
/**
* Glyph ID in the font atlas
*/
glyphId: number;
/**
* X position relative to text origin
*/
x: number;
/**
* Y position relative to text origin
*/
y: number;
/**
* Width of glyph in font units
*/
width: number;
/**
* Height of glyph in font units
*/
height: number;
/**
* X offset for glyph positioning
*/
xOffset: number;
/**
* Y offset for glyph positioning
*/
yOffset: number;
/**
* Atlas texture coordinates (normalized 0-1)
*/
atlasX: number;
atlasY: number;
atlasWidth: number;
Expand Down
Loading