@mlightcad/shx-parser
Version:
A TypeScript library for parsing AutoCAD SHX font files
108 lines (107 loc) • 5.26 kB
TypeScript
import { ShxFontData } from './fontData';
import { ShxAdvanceWidthStrategy } from './advanceWidthStrategy';
import { ShxFontMetrics } from './glyphLayout';
import { ShxShape } from './shape';
export { DEFAULT_INK_WIDTH_CELL_FACTOR, defaultAdvanceWidthStrategy, InkWidthAdvanceStrategy, ShxNativeAdvanceStrategy, ShxAdvanceWidthStrategy, } from './advanceWidthStrategy';
export { alignShxGlyphForLayout, computeFontMetrics, detectBigfontBaselineInkPadding, detectUnifontBaselineOriginFont, shapeEncodedWithTopOrigin, unifontUsesBaselineOrigin, type ShxFontMetrics, } from './glyphLayout';
/**
* Represents a SHX font and provides methods to parse and render its characters.
* This class handles the loading and parsing of SHX font files, and provides
* methods to extract character shapes for rendering.
*/
export declare class ShxFont {
/** The parsed font data containing header and content information */
readonly fontData: ShxFontData;
/** Parser for converting character codes to shapes */
private readonly shapeParser;
/** Cached UNIFONT baseline-origin detection (size-independent). */
private unifontBaselineOriginFont?;
/** Cached BIGFONT baseline ink padding in native font units (size-independent). */
private bigfontBaselineInkPaddingNative?;
/**
* Creates a new ShxFont instance.
* @param data - Either raw binary data of the SHX font file (ArrayBuffer) or pre-parsed font data (ShxFontData)
* @throws {Error} If the font data is invalid or cannot be parsed
*/
constructor(data: ShxFontData | ArrayBuffer);
/**
* Return true if this font contains glyph of the specified character. Otherwise, return false.
* @param char - The character to check
* @returns True if this font contains glyph of the specified character. Otherwise, return false.
*/
hasChar(code: number): boolean;
/**
* Return true if this font contains a shape with the specified name. Otherwise, return false.
* Shape names are matched case-insensitively.
* @param name - The shape name to check (for example, "GRS")
* @returns True if this font contains the named shape. Otherwise, return false.
*/
hasShape(name: string): boolean;
/**
* Gets the character code for a named shape.
* @param name - The shape name to look up
* @returns The character code, or undefined if the shape is not found
*/
getShapeCode(name: string): number | undefined;
/**
* Gets the shape name for a character code, if one is defined.
* @param code - The character code to look up
* @returns The shape name, or undefined if the code has no name
*/
getShapeName(code: number): string | undefined;
/**
* Returns scaled font metrics for a target render size.
* @param size - Target font size in drawing units
*/
getFontMetrics(size: number): ShxFontMetrics;
/**
* Returns a layout-ready glyph: scaled geometry with baseline alignment applied.
*
* Prefer this over {@link ShxFont.getCharShape} when placing text for display.
*
* @param code - The character code to get the shape for
* @param size - The desired font size
*/
getLayoutCharShape(code: number, size: number, advanceStrategy?: ShxAdvanceWidthStrategy): ShxShape | undefined;
/**
* Returns the cached median baseline ink padding for BIGFONT layout normalization.
*
* @returns Padding in native font units from shape #0 height, or 0 when not applicable
*/
private getBigfontBaselineInkPaddingNative;
/**
* Returns whether this UNIFONT encodes horizontal glyphs with baseline at y = 0.
*
* The result is computed once via {@link detectUnifontBaselineOriginFont} and cached,
* because baseline-origin detection is independent of render size.
*
* @param size - Font size used when sampling glyph geometry for detection
* @returns True when baseline-origin UNIFONT shifting should be skipped during layout
*/
private usesUnifontBaselineOriginFont;
/**
* Gets the shape data for a named shape at a given font size.
* Shape names are matched case-insensitively.
* @param name - The shape name to get the shape for
* @param size - The desired font size
* @returns The shape data for the named shape, or undefined if it is not found in the font
*/
getShapeByName(name: string, size: number): ShxShape | undefined;
/**
* Gets the scaled shape geometry for a character code.
*
* Returns the glyph as encoded in the SHX file, scaled to `size`. Vertical placement
* and mixed-font baseline alignment are the responsibility of the text renderer;
* see the `textLayout` module and {@link ShxFont.getFontMetrics}.
*
* @param code - The character code to get the shape for
* @param size - The desired font size
* @returns The shape data for the character, or undefined if the character is not found in the font
*/
getCharShape(code: number, size: number): ShxShape | undefined;
/**
* Releases resources used by the font.
* This should be called when the font is no longer needed to free up memory.
*/
release(): void;
}