@lightningjs/renderer
Version:
Lightning 3 Renderer
138 lines (137 loc) • 4.5 kB
TypeScript
import type { FontFamilyMap, FontMetrics, NormalizedFontMetrics, TrProps, FontLoadOptions } from './TextRenderer.js';
import type { ImageTexture } from '../textures/ImageTexture.js';
import type { Stage } from '../Stage.js';
/**
* SDF Font Data structure matching msdf-bmfont-xml output
*/
export interface SdfFontData {
pages: string[];
chars: Array<{
id: number;
char: string;
x: number;
y: number;
width: number;
height: number;
xoffset: number;
yoffset: number;
xadvance: number;
page: number;
chnl: number;
}>;
kernings: Array<{
first: number;
second: number;
amount: number;
}>;
info: {
face: string;
size: number;
bold: number;
italic: number;
charset: string[];
unicode: number;
stretchH: number;
smooth: number;
aa: number;
padding: [number, number, number, number];
spacing: [number, number];
outline: number;
};
common: {
lineHeight: number;
base: number;
scaleW: number;
scaleH: number;
pages: number;
packed: number;
alphaChnl: number;
redChnl: number;
greenChnl: number;
blueChnl: number;
};
distanceField: {
fieldType: 'sdf' | 'msdf';
distanceRange: number;
};
lightningMetrics?: FontMetrics;
}
/**
* Check if the SDF font handler can render a font
* @param {TrProps} trProps - Text rendering properties
* @returns {boolean} True if the font can be rendered
*/
export declare const canRenderFont: (trProps: TrProps) => boolean;
/**
* Load SDF font from JSON + PNG atlas
* @param {Object} options - Font loading options
* @param {string} options.fontFamily - Font family name
* @param {string} options.fontUrl - JSON font data URL (atlasDataUrl)
* @param {string} options.atlasUrl - PNG atlas texture URL
* @param {FontMetrics} options.metrics - Optional font metrics
*/
export declare const loadFont: (stage: Stage, options: FontLoadOptions) => Promise<void>;
/**
* Get the font families map for resolving fonts
*/
export declare const getFontFamilies: () => FontFamilyMap;
/**
* Initialize the SDF font handler
*/
export declare const init: (c?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => void;
export declare const type = "sdf";
/**
* Check if a font is already loaded by font family
*/
export declare const isFontLoaded: (fontFamily: string) => boolean;
/**
* Get normalized font metrics for a font family
*/
export declare const getFontMetrics: (fontFamily: string, fontSize: number) => NormalizedFontMetrics;
/**
* Set font metrics for a font family
*/
export declare const setFontMetrics: (fontFamily: string, metrics: NormalizedFontMetrics) => void;
/**
* Get glyph data for a character in a specific font
* @param {string} fontFamily - Font family name
* @param {number} codepoint - Character codepoint
* @returns {Object|null} Glyph data or null if not found
*/
export declare const getGlyph: (fontFamily: string, codepoint: number) => SdfFontData["chars"][0] | null;
/**
* Get kerning value between two glyphs
* @param {string} fontFamily - Font family name
* @param {number} firstGlyph - First glyph ID
* @param {number} secondGlyph - Second glyph ID
* @returns {number} Kerning value or 0
*/
export declare const getKerning: (fontFamily: string, firstGlyph: number, secondGlyph: number) => number;
/**
* Get atlas texture for a font family
* @param {string} fontFamily - Font family name
* @returns {ImageTexture|null} Atlas texture or null
*/
export declare const getAtlas: (fontFamily: string) => ImageTexture | null;
/**
* Get font data for a font family
* @param {string} fontFamily - Font family name
* @returns {SdfFontData|null} Font data or null
*/
export declare const getFontData: (fontFamily: string) => SdfFontData | null;
/**
* Get maximum character height for a font family
* @param {string} fontFamily - Font family name
* @returns {number} Max character height or 0
*/
export declare const getMaxCharHeight: (fontFamily: string) => number;
/**
* Get all loaded font families
* @returns {string[]} Array of font family names
*/
export declare const getLoadedFonts: () => string[];
/**
* Unload a font and free resources
* @param {string} fontFamily - Font family name
*/
export declare const unloadFont: (fontFamily: string) => void;