@mlightcad/shx-parser
Version:
A TypeScript library for parsing AutoCAD SHX font files
133 lines (132 loc) • 5.28 kB
TypeScript
import { ShxFontData } from './fontData';
import { ShxShape } from './shape';
/**
* Scaling options for shape parsing
*/
export interface ScalingOptions {
/** Scale by a uniform factor */
factor?: number;
/** Scale by specific height and width */
height?: number;
width?: number;
}
/**
* Parses SHX font data into shapes on demand. To improve performance, the shape is parsed on demand by
* character code and font size. Parsed shapes are cached.
*/
export declare class ShxShapeParser {
/** Font data of the font file */
private readonly fontData;
/** Cached top-level shapes (flush trailing pen-down strokes at shape end). */
private shapeCache;
/** Cached subshape primitives (no end flush; parent may continue the stroke). */
private subshapeCache;
/** Shapes data. Key is the char code */
private shapeData;
constructor(fontData: ShxFontData);
/**
* Releases parsed shapes and cached shapes
*/
release(): void;
/**
* Parses a character's shape with the given font size.
* @param code - The character code
* @param size - The font size
* @returns The parsed shape or undefined if the character is not found
*/
getCharShape(code: number, size: number): ShxShape | undefined;
/**
* Parses a character's shape with scaling options
* @param code - The character code
* @param options - Scaling options (factor or height/width)
* @returns The parsed shape or undefined if the character is not found
*/
private parseAndScale;
/** Strips the embedded character-code prefix from dual-byte BIGFONT parent glyphs. */
private prepareBigfontGlyphBytecode;
/**
* Dual-orientation vertical BIGFONT files (e.g. gbcbig.shx) use 0x8e/0x8f markers on
* code 7 instead of real subshapes. Only the marker byte is skipped; following setup
* commands (push / pen up / xy origin) are executed normally.
*/
private isVerticalDualBigfontMarker;
/**
* Scales a shape according to the given scale factor
* @param shape - The shape to scale
* @param factor - The scale factor
* @returns The scaled shape
*/
private scaleShapeByFactor;
/**
* Scales a shape according to the given height and width
* @param shape - The shape to scale
* @param height - The target height
* @param width - The target width
* @returns The scaled shape
*/
private scaleShapeByHeightAndWidth;
/**
* Whether code 14 (0x0E) should skip the following command for the current layout.
* Defaults to horizontal text layout when no orientation is supplied.
*/
private shouldSkipVerticalFlagCommand;
/** Marks that bytecode explicitly defines horizontal advance. */
private markAdvanceDefined;
/** Records a terminal pen-up XY move (codes 8/9). */
private notePenUpPositioning;
/** Clears a pending advance when later bytecode supersedes the prior XY move. */
private clearPendingAdvance;
private stateHasInk;
/**
* Confirms terminal pen-up XY (codes 8/9) as advance definition.
* Ignores closure moves that return to the origin after drawing (e.g. txt `A`).
*/
private finalizeAdvanceFlag;
/**
* Parses the shape of a character.
* @param data - The data of the character
* @param options - Optional parse settings
* @returns The parsed shape
*/
private parseShape;
/** Builds a shape result, including any trailing pen-down polyline. */
private buildShapeFromState;
/**
* Please refer to special codes reference in the following link for more information.
* https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-06832147-16BE-4A66-A6D0-3ADF98DC8228
* @param command - The command byte
* @param data - The data of the character
* @param index - The index of the command byte
* @param state - The state of the parser
* @returns The index of the next command byte
*/
private handleSpecialCommand;
private handleVectorCommand;
/**
* Get the vector for the given direction code. Please refer to the following link for more information.
* https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-0A8E12A1-F4AB-44AD-8A9B-2140E0D5FD23
* @param dir - The direction code of the vector
* @returns Returns the vector for the given direction code
*/
private getVectorForDirection;
private handleSubshapeCommand;
private handleXYDisplacement;
private handleMultipleXYDisplacements;
private handleOctantArc;
private handleFractionalArc;
private handleBulgeArc;
private handleMultipleBulgeArcs;
private skipCode;
private getScaledSubshapeAtInsertPoint;
/**
* Handles drawing an arc segment with the given vector and bulge
* @param currentPoint The starting point of the arc
* @param vec The displacement vector
* @param bulge The bulge value (will be normalized by 127.0)
* @param scale The current scale factor
* @param isPenDown Whether the pen is currently down (drawing)
* @param currentPolyline The current polyline being built
* @returns The new current point after the arc
*/
private handleArcSegment;
}