/** * Token types used in MText parsing */ export declare enum TokenType { /** No token */ NONE = 0, /** Word token with string data */ WORD = 1, /** Stack token with [numerator, denominator, type] data */ STACK = 2, /** Space token with no data */ SPACE = 3, /** Non-breaking space token with no data */ NBSP = 4, /** Tab token with no data */ TABULATOR = 5, /** New paragraph token with no data */ NEW_PARAGRAPH = 6, /** New column token with no data */ NEW_COLUMN = 7, /** Wrap at dimension line token with no data */ WRAP_AT_DIMLINE = 8, /** Properties changed token with string data (full command) */ PROPERTIES_CHANGED = 9, /** AutoCAD percent-sign symbol code (`%%c`, `%%d`, `%%p`, `%%ddd`, `%%%`) */ PERCENT_SYMBOL = 10 } /** * AutoCAD percent-sign symbol emitted when {@link MTextParserOptions.yieldPercentSymbols} * is enabled. */ export type PercentSymbolData = { kind: 'named'; /** Percent code letter: `%%c`, `%%d`, or `%%p`. */ code: 'c' | 'd' | 'p'; /** Unicode expansion used for display (e.g. `%%c` → `Ø`). */ char: string; } | { kind: 'numeric'; /** Decimal code from `%%ddd` (0–255). */ charCode: number; /** `String.fromCharCode(charCode)`. */ char: string; } | { kind: 'literal'; /** Literal percent from `%%%`. */ char: '%'; }; /** * Represents a factor value that can be either absolute or relative. * Used for properties like height, width, and character tracking in MText formatting. */ export interface FactorValue { /** The numeric value of the factor */ value: number; /** Whether the value is relative (true) or absolute (false) */ isRelative: boolean; } /** * Format properties of MText word tokens. * This interface defines all the formatting properties that can be applied to MText content, * including text styling, colors, alignment, font properties, and paragraph formatting. */ export interface Properties { /** Whether text is underlined */ underline?: boolean; /** Whether text has an overline */ overline?: boolean; /** Whether text has strike-through */ strikeThrough?: boolean; /** AutoCAD Color Index (ACI) color value (0-256), or null if not set */ aci?: number | null; /** RGB color tuple [r, g, b], or null if not set */ rgb?: RGB | null; /** Line alignment for the text */ align?: MTextLineAlignment; /** Font face properties including family, style, and weight */ fontFace?: FontFace; /** Capital letter height factor (can be relative or absolute) */ capHeight?: FactorValue; /** Character width factor (can be relative or absolute) */ widthFactor?: FactorValue; /** Character tracking factor for spacing between characters (can be relative or absolute) */ charTrackingFactor?: FactorValue; /** Oblique angle in degrees for text slant */ oblique?: number; /** Paragraph formatting properties (partial to allow selective updates) */ paragraph?: Partial; } /** * Represents a change in MText properties, including the command, the changed properties, and the context depth. */ export interface ChangedProperties { /** * The property command that triggered the change (e.g., 'L', 'C', 'f'). * The command will be undefined if it is to restore context. */ command: string | undefined; /** * The set of properties that have changed as a result of the command. */ changes: Properties; /** * The current context stack depth when the property change occurs. * - 0: The change is global (applies outside of any `{}` block). * - >0: The change is local (applies within one or more nested `{}` blocks). */ depth: number; } /** * Type for token data based on token type */ export type TokenData = { [TokenType.NONE]: null; [TokenType.WORD]: string; [TokenType.STACK]: [string, string, string]; [TokenType.SPACE]: null; [TokenType.NBSP]: null; [TokenType.TABULATOR]: null; [TokenType.NEW_PARAGRAPH]: null; [TokenType.NEW_COLUMN]: null; [TokenType.WRAP_AT_DIMLINE]: null; [TokenType.PROPERTIES_CHANGED]: ChangedProperties; [TokenType.PERCENT_SYMBOL]: PercentSymbolData; }; /** * Line alignment options for MText */ export declare enum MTextLineAlignment { /** Align text to bottom */ BOTTOM = 0, /** Align text to middle */ MIDDLE = 1, /** Align text to top */ TOP = 2 } /** * Paragraph alignment options for MText */ export declare enum MTextParagraphAlignment { /** Default alignment */ DEFAULT = 0, /** Left alignment */ LEFT = 1, /** Right alignment */ RIGHT = 2, /** Center alignment */ CENTER = 3, /** Justified alignment */ JUSTIFIED = 4, /** Distributed alignment */ DISTRIBUTED = 5 } /** * Text stroke options for MText */ export declare enum MTextStroke { /** No stroke */ NONE = 0, /** Underline stroke */ UNDERLINE = 1, /** Overline stroke */ OVERLINE = 2, /** Strike-through stroke */ STRIKE_THROUGH = 4 } /** * RGB color tuple */ export type RGB = [number, number, number]; /** * Font style type */ export type FontStyle = 'Regular' | 'Italic'; /** * Font face properties */ export interface FontFace { /** Font family name */ family: string; /** Font style (e.g., 'Regular', 'Italic') */ style: FontStyle; /** Font weight (e.g., 400 for normal, 700 for bold) */ weight: number; } /** * Paragraph properties */ export interface ParagraphProperties { /** Indentation value */ indent: number; /** Left margin value */ left: number; /** Right margin value */ right: number; /** Paragraph alignment */ align: MTextParagraphAlignment; /** Tab stop positions and types */ tabs: (number | string)[]; } /** * Convert RGB tuple to integer color value * @param rgb - RGB color tuple * @returns Integer color value */ export declare function rgb2int(rgb: RGB): number; /** * Convert integer color value to RGB tuple * @param value - Integer color value * @returns RGB color tuple */ export declare function int2rgb(value: number): RGB; /** * Escape DXF line endings * @param text - Text to escape * @returns Escaped text */ export declare function escapeDxfLineEndings(text: string): string; /** * Check if text contains inline formatting codes * @param text - Text to check * @returns True if text contains formatting codes */ export declare function hasInlineFormattingCodes(text: string): boolean; /** * Extracts all unique font names used in an MText string. * This function searches for font commands in the format \f{fontname}| or \f{fontname}; and returns a set of unique font names. * Font names are converted to lowercase to ensure case-insensitive uniqueness. * * @param mtext - The MText string to analyze for font names * @param removeExtension - Whether to remove font file extensions (e.g., .ttf, .shx) from font names. Defaults to false. * @returns A Set containing all unique font names found in the MText string, converted to lowercase * @example * ```ts * const mtext = "\\fArial.ttf|Hello\\fTimes New Roman.otf|World"; * const fonts = getFonts(mtext, true); * // Returns: Set(2) { "arial", "times new roman" } * ``` */ export declare function getFonts(mtext: string, removeExtension?: boolean): Set; /** * Configuration options for the MText parser. * These options control how the parser behaves during tokenization and property handling. */ export interface MTextParserOptions { /** * Whether to yield PROPERTIES_CHANGED tokens when formatting properties change. * When true, the parser will emit tokens whenever properties like color, font, or alignment change. * When false, property changes are applied silently to the context without generating tokens. * @default false */ yieldPropertyCommands?: boolean; /** * Whether to reset paragraph parameters when encountering a new paragraph token. * When true, paragraph properties (indent, margins, alignment, tab stops) are reset to defaults * at the start of each new paragraph. * @default false */ resetParagraphParameters?: boolean; /** * Whether to emit {@link TokenType.PERCENT_SYMBOL} tokens for AutoCAD `%%` symbol * codes instead of expanding them into {@link TokenType.WORD} characters. * @default false */ yieldPercentSymbols?: boolean; /** * Custom decoder function for MIF (Multibyte Interchange Format) codes. * If provided, this function will be used instead of the default decodeMultiByteChar. * The function receives the hex code string and should return the decoded character. * @param hex - Hex code string (e.g., "C4E3" or "1A2B3") * @returns Decoded character or empty square (▯) if invalid * @default undefined (uses default decoder) */ mifDecoder?: (hex: string) => string; /** * The length of MIF hex codes to parse. MIF codes in AutoCAD can vary in length * depending on the specific SHX big font used (typically 4 or 5 digits). * If not specified, the parser will try to auto-detect the length by attempting * to match 4 digits first, then 5 digits if needed. * @default undefined (auto-detect) */ mifCodeLength?: 4 | 5 | 'auto'; } /** * Main parser class for MText content */ export declare class MTextParser { private scanner; private ctxStack; private continueStroke; private yieldPropertyCommands; private resetParagraphParameters; private yieldPercentSymbols; private inStackContext; private mifDecoder; private mifCodeLength; /** * Creates a new MTextParser instance * @param content - The MText content to parse * @param ctx - Optional initial MText context * @param options - Parser options */ constructor(content: string, ctx?: MTextContext, options?: MTextParserOptions); /** * Decode multi-byte character from hex code * @param hex - Hex code string (e.g. "C4E3" or "1A2B3") * @returns Decoded character or empty square if invalid */ private decodeMultiByteChar; /** * Extract MIF hex code from scanner * @param length - The length of the hex code to extract (4 or 5), or 'auto' to detect * @returns The extracted hex code, or null if not found */ private extractMifCode; /** * Push current context onto the stack */ private pushCtx; /** * Pop context from the stack */ private popCtx; /** * Parse stacking expression (numerator/denominator) * @returns Tuple of [TokenType.STACK, [numerator, denominator, type]] */ private parseStacking; /** * Parse MText properties * @param cmd - The property command to parse * @returns Property changes if yieldPropertyCommands is true and changes occurred */ private parseProperties; /** * Get property changes between two contexts * @param oldCtx - The old context * @param newCtx - The new context * @returns Object containing changed properties */ private getPropertyChanges; /** * Parse alignment property * @param ctx - The context to update */ private parseAlign; /** * Parse height property * @param ctx - The context to update */ private parseHeight; /** * Parse width property * @param ctx - The context to update */ private parseWidth; /** * Parse character tracking property * @param ctx - The context to update */ private parseCharTracking; /** * Parse float value or factor * @param value - Current value to apply factor to * @returns New value */ private parseFloatValueOrFactor; /** * Parse oblique angle property * @param ctx - The context to update */ private parseOblique; /** * Parse ACI color property * @param ctx - The context to update */ private parseAciColor; /** * Parse RGB color property * @param ctx - The context to update */ private parseRgbColor; /** * Extract float expression from scanner * @param relative - Whether to allow relative values (ending in 'x') * @returns Extracted expression */ private extractFloatExpression; /** * Extract integer expression from scanner * @returns Extracted expression */ private extractIntExpression; /** * Extract expression until semicolon or end * @param escape - Whether to handle escaped semicolons * @returns Extracted expression */ private extractExpression; /** * Parse font properties * @param ctx - The context to update */ private parseFontProperties; /** * Parse paragraph properties from the MText content * Handles properties like indentation, alignment, and tab stops * @param ctx - The context to update */ private parseParagraphProperties; /** * Consume optional terminator (semicolon) */ private consumeOptionalTerminator; /** * Builds {@link PercentSymbolData} for a recognized `%%` code letter. */ private buildPercentSymbolData; /** * Parse MText content into tokens * @yields MTextToken objects */ parse(): Generator; } /** * Text scanner for parsing MText content */ export declare class TextScanner { private text; private textLen; private _index; /** * Create a new text scanner * @param text - The text to scan */ constructor(text: string); /** * Get the current index in the text */ get currentIndex(): number; /** * Check if the scanner has reached the end of the text */ get isEmpty(): boolean; /** * Check if there is more text to scan */ get hasData(): boolean; /** * Get the next character and advance the index * @returns The next character, or empty string if at end */ get(): string; /** * Advance the index by the specified count * @param count - Number of characters to advance */ consume(count?: number): void; /** * Look at a character without advancing the index * @param offset - Offset from current position * @returns The character at the offset position, or empty string if out of bounds */ peek(offset?: number): string; /** * Find the next occurrence of a character * @param char - The character to find * @param escape - Whether to handle escaped characters * @returns Index of the character, or -1 if not found */ find(char: string, escape?: boolean): number; /** * Get the remaining text from the current position */ get tail(): string; /** * Check if the next character is a space */ isNextSpace(): boolean; /** * Consume spaces until a non-space character is found * @returns Number of spaces consumed */ consumeSpaces(): number; } /** * Class to handle ACI and RGB color logic for MText. * * This class encapsulates color state for MText, supporting both AutoCAD Color Index (ACI) and RGB color. * Only one color mode is active at a time: setting an RGB color disables ACI, and vice versa. * RGB is stored as a single 24-bit integer (0xRRGGBB) for efficient comparison and serialization. * * Example usage: * ```ts * const color1 = new MTextColor(1); // ACI color * const color2 = new MTextColor([255, 0, 0]); // RGB color * const color3 = new MTextColor(); // Default (ACI=256, "by layer") * ``` */ export declare class MTextColor { /** * The AutoCAD Color Index (ACI) value. Only used if no RGB color is set. * @default 256 ("by layer") */ private _aci; /** * The RGB color value as a single 24-bit integer (0xRRGGBB), or null if not set. * @default null */ private _rgbValue; /** * Create a new MTextColor instance. * @param color The initial color: number for ACI, [r,g,b] for RGB, or null/undefined for default (ACI=256). */ constructor(color?: number | RGB | null); /** * Get the current ACI color value. * @returns The ACI color (0-256), or null if using RGB. */ get aci(): number | null; /** * Set the ACI color value. Setting this disables any RGB color. * @param value The ACI color (0-256), or null to unset. * @throws Error if value is out of range. */ set aci(value: number | null); /** * Get the current RGB color as a tuple [r, g, b], or null if not set. * @returns The RGB color tuple, or null if using ACI. */ get rgb(): RGB | null; /** * Set the RGB color. Setting this disables ACI color. * @param value The RGB color tuple [r, g, b], or null to use ACI. */ set rgb(value: RGB | null); /** * Returns true if the color is set by RGB, false if by ACI. */ get isRgb(): boolean; /** * Returns true if the color is set by ACI, false if by RGB. */ get isAci(): boolean; /** * Get or set the internal RGB value as a number (0xRRGGBB), or null if not set. * Setting this will switch to RGB mode and set ACI to null. */ get rgbValue(): number | null; set rgbValue(val: number | null); /** * Returns a deep copy of this color. * @returns A new MTextColor instance with the same color state. */ copy(): MTextColor; /** * Returns a plain object for serialization. * @returns An object with aci, rgb (tuple), and rgbValue (number or null). */ toObject(): { aci: number | null; rgb: RGB | null; rgbValue: number | null; }; /** * Convert the current color to a CSS hex color string (#rrggbb). * Returns null if the color is ACI-based and has no RGB value. */ toCssColor(): string | null; /** * Create an MTextColor from a CSS color string. * Supports #rgb, #rrggbb, rgb(...), rgba(...). Returns null if invalid or transparent. */ static fromCssColor(value: string | null | undefined): MTextColor | null; /** * Equality check for color. * @param other The other MTextColor to compare. * @returns True if both ACI and RGB values are equal. */ equals(other: MTextColor): boolean; } /** * MText context class for managing text formatting state */ export declare class MTextContext { private _stroke; /** Whether to continue stroke formatting */ continueStroke: boolean; /** Color (ACI or RGB) */ color: MTextColor; /** Line alignment */ align: MTextLineAlignment; /** Font face properties */ fontFace: FontFace; /** Capital letter height */ private _capHeight; /** Character width factor */ private _widthFactor; /** * Character tracking factor a multiplier applied to the default spacing between characters in the MText object. * - Value = 1.0 → Normal spacing. * - Value < 1.0 → Characters are closer together. * - Value > 1.0 → Characters are spaced farther apart. */ private _charTrackingFactor; /** Oblique angle */ oblique: number; /** Paragraph properties */ paragraph: ParagraphProperties; /** * Get the capital letter height */ get capHeight(): FactorValue; /** * Set the capital letter height * @param value - Height value */ set capHeight(value: FactorValue); /** * Get the character width factor */ get widthFactor(): FactorValue; /** * Set the character width factor * @param value - Width factor value */ set widthFactor(value: FactorValue); /** * Get the character tracking factor */ get charTrackingFactor(): FactorValue; /** * Set the character tracking factor * @param value - Tracking factor value */ set charTrackingFactor(value: FactorValue); /** * Get the ACI color value */ get aci(): number | null; /** * Set the ACI color value * @param value - ACI color value (0-256) * @throws Error if value is out of range */ set aci(value: number); /** * Get the RGB color value */ get rgb(): RGB | null; /** * Set the RGB color value */ set rgb(value: RGB | null); /** * Gets whether the current text should be rendered in italic style. * @returns {boolean} True if the font style is 'Italic', otherwise false. */ get italic(): boolean; /** * Sets whether the current text should be rendered in italic style. * @param value - If true, sets the font style to 'Italic'; if false, sets it to 'Regular'. */ set italic(value: boolean); /** * Gets whether the current text should be rendered in bold style. * This is primarily used for mesh fonts and affects font selection. * @returns {boolean} True if the font weight is 700 or higher, otherwise false. */ get bold(): boolean; /** * Sets whether the current text should be rendered in bold style. * This is primarily used for mesh fonts and affects font selection. * @param value - If true, sets the font weight to 700; if false, sets it to 400. */ set bold(value: boolean); /** * Get whether text is underlined */ get underline(): boolean; /** * Set whether text is underlined * @param value - Whether to underline */ set underline(value: boolean); /** * Get whether text has strike-through */ get strikeThrough(): boolean; /** * Set whether text has strike-through * @param value - Whether to strike through */ set strikeThrough(value: boolean); /** * Get whether text has overline */ get overline(): boolean; /** * Set whether text has overline * @param value - Whether to overline */ set overline(value: boolean); /** * Check if any stroke formatting is active */ get hasAnyStroke(): boolean; /** * Set the state of a stroke type * @param stroke - The stroke type to set * @param state - Whether to enable or disable the stroke */ private _setStrokeState; /** * Create a copy of this context * @returns A new context with the same properties */ copy(): MTextContext; } /** * Token class for MText parsing */ export declare class MTextToken { type: TokenType; ctx: MTextContext; data: TokenData[TokenType]; /** * Create a new MText token * @param type - The token type * @param ctx - The text context at this token * @param data - Optional token data */ constructor(type: TokenType, ctx: MTextContext, data: TokenData[TokenType]); }