/** * Where a line may break, as the engine decides it: a port of PDFium's * character classes (`cpvt_section.cpp`), quirks included (every character * from U+0080 to U+0094 counts as punctuation). */ /** Per ASCII character: 0x01 Latin letter, 0x02 digit, 0x04 opening, 0x08 punctuation, 0x20 connective. */ // prettier-ignore const SPECIAL = [ 0x00, 0x0C, 0x08, 0x0C, 0x08, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x10, 0x00, 0x00, 0x28, 0x0C, 0x08, 0x00, 0x00, 0x28, 0x28, 0x28, 0x28, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x08, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0C, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0C, 0x00, 0x08, 0x00, 0x00, ]; const isLatin = (code: number): boolean => code <= 0x7f ? (SPECIAL[code]! & 0x01) !== 0 : (code >= 0x00c0 && code <= 0x00ff) || (code >= 0x0100 && code <= 0x024f) || (code >= 0x1e00 && code <= 0x1eff) || (code >= 0x2c60 && code <= 0x2c7f) || (code >= 0xa720 && code <= 0xa7ff) || (code >= 0xff21 && code <= 0xff3a) || (code >= 0xff41 && code <= 0xff5a); const isDigit = (code: number): boolean => code >= 0x30 && code <= 0x39; const CJK_SYMBOLS = new Set([ 0x3005, 0x3006, 0x3021, 0x3022, 0x3023, 0x3024, 0x3025, 0x3026, 0x3027, 0x3028, 0x3029, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, ]); function isCjk(code: number): boolean { if ( (code >= 0x1100 && code <= 0x11ff) || (code >= 0x2e80 && code <= 0x2fff) || (code >= 0x3040 && code <= 0x9fbf) || (code >= 0xac00 && code <= 0xd7af) || (code >= 0xf900 && code <= 0xfaff) || (code >= 0xfe30 && code <= 0xfe4f) || (code >= 0x20000 && code <= 0x2a6df) || (code >= 0x2f800 && code <= 0x2fa1f) ) { return true; } if (code >= 0x3000 && code <= 0x303f) return CJK_SYMBOLS.has(code); return code >= 0xff66 && code <= 0xff9d; } const GENERAL_PUNCTUATION = new Set([ 0x2010, 0x2011, 0x2012, 0x2013, 0x2018, 0x2019, 0x201a, 0x201b, 0x201c, 0x201d, 0x201e, 0x201f, 0x2032, 0x2033, 0x2034, 0x2035, 0x2036, 0x2037, 0x203c, 0x203d, 0x203e, 0x2044, ]); const CJK_PUNCTUATION = new Set([ 0x3001, 0x3002, 0x3003, 0x3005, 0x3009, 0x300a, 0x300b, 0x300c, 0x300d, 0x300f, 0x300e, 0x3010, 0x3011, 0x3014, 0x3015, 0x3016, 0x3017, 0x3018, 0x3019, 0x301a, 0x301b, 0x301d, 0x301e, 0x301f, ]); const FULLWIDTH_PUNCTUATION = new Set([ 0xff01, 0xff02, 0xff07, 0xff08, 0xff09, 0xff0c, 0xff0e, 0xff0f, 0xff1a, 0xff1b, 0xff1f, 0xff3b, 0xff3d, 0xff40, 0xff5b, 0xff5c, 0xff5d, 0xff61, 0xff62, 0xff63, 0xff64, 0xff65, 0xff9e, 0xff9f, ]); function isPunctuation(code: number): boolean { if (code <= 0x7f) return (SPECIAL[code]! & 0x08) !== 0; if (code >= 0x0080 && code <= 0x00ff) { // PDFium's own test: `word <= 0x0094` makes all of U+0080 to U+0094 punctuation. return code <= 0x0094 || code === 0x0096 || code === 0x00b4 || code === 0x00b8; } if (code >= 0x2000 && code <= 0x206f) return GENERAL_PUNCTUATION.has(code); if (code >= 0x3000 && code <= 0x303f) return CJK_PUNCTUATION.has(code); if (code >= 0xfe50 && code <= 0xfe6f) return (code >= 0xfe50 && code <= 0xfe5e) || code === 0xfe63; if (code >= 0xff00 && code <= 0xffef) return FULLWIDTH_PUNCTUATION.has(code); return false; } const isConnective = (code: number): boolean => code <= 0x7f && (SPECIAL[code]! & 0x20) !== 0; const OPENING = new Set([ 0x300a, 0x300c, 0x300e, 0x3010, 0x3014, 0x3016, 0x3018, 0x301a, 0xff08, 0xff3b, 0xff5b, 0xff62, ]); export const isOpenPunctuation = (code: number): boolean => code <= 0x7f ? (SPECIAL[code]! & 0x04) !== 0 : OPENING.has(code); const isCurrency = (code: number): boolean => code === 0x0024 || code === 0x0080 || code === 0x00a2 || code === 0x00a3 || code === 0x00a4 || code === 0x00a5 || (code >= 0x20a0 && code <= 0x20cf) || code === 0xfe69 || code === 0xff04 || code === 0xffe0 || code === 0xffe1 || code === 0xffe5 || code === 0xffe6; const isPrefix = (code: number): boolean => isCurrency(code) || code === 0x2116; export const isSpace = (code: number): boolean => code === 0x0020 || code === 0x3000; /** Whether a word may start at `current` after `previous` (CPVT's `NeedDivision`). */ export function needsBreak(previous: number, current: number): boolean { if ((isLatin(previous) || isDigit(previous)) && (isLatin(current) || isDigit(current))) { return false; } if (isSpace(current) || isPunctuation(current)) return false; if (isConnective(previous) || isConnective(current)) return false; if (isSpace(previous) || isPunctuation(previous)) return true; if (isPrefix(previous)) return false; if (isPrefix(current) || isCjk(current)) return true; return isCjk(previous); }