/** * Encoding Service * * Provides comprehensive encoding support for: * - GBK, GB2312, Big5 (Chinese) * - EUC-KR (Korean) * - Shift-JIS, ISO-2022-JP (Japanese) * - UTF-8 * * Handles character conversion for thermal printer output. * * @example * ```typescript * const service = new EncodingService(); * const bytes = service.encode('你好世界', 'GBK'); * const korean = service.encode('안녕하세요', 'EUC-KR'); * const japanese = service.encode('こんにちは', 'Shift-JIS'); * const detected = service.detectEncoding('Hello 世界'); * ``` */ /** * Supported encoding types */ export type SupportedEncoding = 'GBK' | 'GB2312' | 'BIG5' | 'UTF-8' | 'UTF8' | 'EUC-KR' | 'EUCKR' | 'SHIFT-JIS' | 'SHIFTJIS' | 'SJIS' | 'ISO-2022-JP' | 'ISO2022JP' | 'JIS'; /** * Encoding configuration options */ export interface EncodingConfig { /** Default encoding to use */ defaultEncoding: SupportedEncoding; /** Character to use when a character cannot be encoded */ fallbackChar: string; /** Whether to show warnings for unsupported characters */ showWarnings: boolean; } /** * Encoding result with metadata */ export interface EncodingResult { /** Encoded bytes */ bytes: Uint8Array; /** Number of characters that couldn't be encoded */ unsupportedCount: number; /** List of unsupported characters (if any) */ unsupportedChars: string[]; } /** * Encoding Service class * Provides methods for encoding text to various character sets */ export declare class EncodingService { private readonly logger; private config; private static utf8Encoder; /** * Creates a new EncodingService instance * @param config - Optional configuration */ constructor(config?: Partial); /** * Configure the encoding service * @param config - Configuration options */ configure(config: Partial): void; /** * Get current configuration * @returns Current configuration */ getConfig(): EncodingConfig; /** * Encode text to bytes using the specified encoding * * @param text - Text to encode * @param encoding - Target encoding (default: from config) * @returns Encoded bytes as Uint8Array * * @example * ```typescript * const service = new EncodingService(); * const bytes = service.encode('你好', 'GBK'); * ``` */ encode(text: string, encoding?: string): Uint8Array; /** * Encode text with detailed result including unsupported character info * * @param text - Text to encode * @param encoding - Target encoding * @returns Encoding result with metadata */ encodeWithInfo(text: string, encoding?: string): EncodingResult; /** * Check if an encoding is supported * * @param encoding - Encoding name to check * @returns true if supported * * @example * ```typescript * service.isSupported('GBK'); // true * service.isSupported('SHIFT-JIS'); // false * ``` */ isSupported(encoding: string): boolean; /** * Detect the best encoding for the given text * * Analyzes the text content and recommends the most appropriate encoding: * - UTF-8 for mixed content or when GBK coverage is insufficient * - GBK for simplified Chinese * - Big5 for traditional Chinese (when detected) * * @param text - Text to analyze * @returns Recommended encoding name * * @example * ```typescript * service.detectEncoding('Hello World'); // 'UTF-8' * service.detectEncoding('你好世界'); // 'GBK' * ``` */ detectEncoding(text: string): SupportedEncoding; /** * Get list of supported encodings * @returns Array of supported encoding names */ getSupportedEncodings(): SupportedEncoding[]; /** * Encode text to GBK bytes * @param text - Text to encode * @returns GBK encoded bytes */ private encodeGbk; /** * Encode text to Big5 bytes * @param text - Text to encode * @returns Big5 encoded bytes */ private encodeBig5; /** * Encode text to EUC-KR (Korean) bytes * * EUC-KR (KS X 1001) is the standard Korean character encoding. * Supports Hangul syllables, Hanja, and special Korean characters. * Uses TextEncoder when available for full coverage. * * @param text - Text to encode * @returns EUC-KR encoded bytes */ private encodeKorean; /** * Encode text to Shift-JIS (Japanese) bytes * * Shift-JIS (JIS X 0201/0208) is the standard Japanese encoding. * Supports Hiragana, Katakana, Kanji, and punctuation. * Uses TextEncoder when available for full coverage. * * @param text - Text to encode * @returns Shift-JIS encoded bytes */ private encodeShiftJis; /** * Encode text to ISO-2022-JP (Japanese) bytes * * ISO-2022-JP (JIS X 4081) uses escape sequences to switch character sets. * Format: ESC ( B for ASCII, ESC $ B for JIS X 0208 (Kanji/Katakana). * * @param text - Text to encode * @returns ISO-2022-JP encoded bytes with escape sequences */ private encodeIso2022Jp; /** * Normalize encoding name to standard format * @param encoding - Encoding name * @returns Normalized encoding name */ private normalizeEncoding; /** * Template method: encode text using a character-level strategy callback. * * Handles the common boilerplate shared by all encoding methods: * - Surrogate pair detection → fallback * - ASCII passthrough * - Delegation to `encodeNonAscii` for non-ASCII characters * * Optional hooks allow encodings that need special treatment * (e.g. ISO-2022-JP escape sequences) to customise every stage. * * @param text - Text to encode * @param encodeNonAscii - Strategy callback; returns encoded bytes or null for fallback * @param hooks - Optional hooks for pre/post processing and special ASCII/surrogate handling * @returns Encoded bytes */ private encodeWithStrategy; } export declare const encodingService: EncodingService;