UNPKG

@mlightcad/shx-parser

Version:

A TypeScript library for parsing AutoCAD SHX font files

72 lines (71 loc) 3.81 kB
import { ShxAdvanceWidthStrategy } from './advanceWidthStrategy'; import { ShxFontContentData, ShxFontData } from './fontData'; import { ShxShape } from './shape'; /** * Scaled font metrics derived from shape #0 (`baseUp`, `baseDown`, `height`, `width`). * * These values describe the font cell at a target render size. Text renderers use them * to align mixed-font lines on a shared baseline without modifying individual glyphs. */ export interface ShxFontMetrics { /** Target render size (font cell height in drawing units) */ size: number; /** Distance from the baseline to the top of the cap band */ capHeight: number; /** Distance from the baseline downward into the descender band */ descenderHeight: number; /** Scaled cell width */ cellWidth: number; /** Sum of cap and descender bands (`capHeight + descenderHeight`) */ totalHeight: number; } /** * Computes scaled font metrics for a target render size. * * @param content - Parsed font content from shape #0 * @param size - Target font size in drawing units */ export declare function computeFontMetrics(content: ShxFontContentData, size: number): ShxFontMetrics; /** * True when a SHAPES text font (shape #0 present) stores glyphs in UNIFONT-style * top-origin coordinates: ink extends well below the descender band unless shifted * by {@link ShxFontMetrics.capHeight}. gdt.shx is a common example. */ export declare function shapeEncodedWithTopOrigin(fontData: ShxFontData, shape: ShxShape, metrics: ShxFontMetrics): boolean; /** * True when a UNIFONT glyph already encodes with the baseline at y = 0 and ink * above it (for example isocp.shx, tssdeng.shx with dualOrientation). * * These fonts must not receive the capHeight shift applied to top-origin UNIFONTs. */ export declare function unifontUsesBaselineOrigin(shape: ShxShape, metrics: ShxFontMetrics): boolean; /** * Detects the median baseline ink padding for a BIGFONT with no descender band. * * Extended-style BIGFONT files such as hztxt.shx encode full-width Han glyphs with * their lowest ink above y = 0. When paired with baseline-origin UNIFONT primaries * (tssdeng.shx), ASCII digits sit on y = 0 while CJK appears visually higher. * Layout shifts these BIGFONT glyphs down by the detected padding so mixed lines * share a common visual baseline without cross-font reference metrics. * * @param fontData - Parsed font header and content * @param getRawShape - Returns scaled raw geometry for a character code * @param size - Font size used when sampling glyph geometry (typically shape #0 height) * @returns Median minimum y of sampled body glyphs in font units at `size` */ export declare function detectBigfontBaselineInkPadding(fontData: ShxFontData, getRawShape: (code: number) => ShxShape | undefined, size: number): number; /** * Detects whether a UNIFONT file encodes horizontal glyphs with baseline at y = 0. * * Samples common ASCII letter/digit codes from the font. When any sample matches * {@link unifontUsesBaselineOrigin}, the whole font skips capHeight shifting so * punctuation such as hyphen and tilde keep their in-cell vertical positions. */ export declare function detectUnifontBaselineOriginFont(fontData: ShxFontData, getRawShape: (code: number) => ShxShape | undefined, size: number): boolean; /** * Applies font metrics to scaled SHX geometry for text layout. * * Raw {@link ShxFont.getCharShape} output keeps encoded coordinates from the SHX file; * this function repositions glyphs so mixed-font lines share a common baseline at y = 0. */ export declare function alignShxGlyphForLayout(shape: ShxShape, fontData: ShxFontData, size: number, advanceStrategy?: ShxAdvanceWidthStrategy, unifontBaselineOriginFont?: boolean, bigfontBaselineInkPaddingNative?: number): ShxShape;