/** * Text encoder for ESC/POS printers * Strategy: UTF-8 by default, ASCII removes Vietnamese tones * Note: UTF-8 encoding always succeeds, but printer may not have UTF-8 fonts * User must manually set encoding='ascii' if they see garbled text */ import {removeVietnameseTones} from './utils' import {selectCodepage} from './commands' /** * Encode text for thermal printer */ export function encodeVietnamese( text: string, encoding: 'utf8' | 'ascii', codepage?: number, ): { bytes: Uint8Array codepageCmd: Uint8Array } { const normalizedText = text.normalize('NFC') // Custom codepage (advanced users) if (codepage !== undefined) { const bytes = new TextEncoder().encode(normalizedText) const codepageCmd = selectCodepage(codepage) return {bytes, codepageCmd} } // UTF-8: No codepage command needed if (encoding === 'utf8') { const bytes = new TextEncoder().encode(normalizedText) return {bytes, codepageCmd: new Uint8Array(0)} } // ASCII: Remove Vietnamese tones const textWithoutTones = removeVietnameseTones(normalizedText) const bytes = new TextEncoder().encode(textWithoutTones) return {bytes, codepageCmd: new Uint8Array(0)} }