Skip to main content

hpl_cjk_text/
rules.rs

1//! 避頭尾 (kinsoku) tables and the break predicate.
2
3/// Characters that must never start a line (TC tooling: `NO_LINE_START`).
4pub const NO_LINE_START: &[char] = &[
5    ',', '。', '、', '!', '?', ':', ';', '…', '‥', '」', '』', ')', '》', '〉', '】', '〕', '—',
6    '”', '’', '.', '・', '〜', '~',
7];
8/// Characters that must never end a line (TC tooling: `NO_LINE_END`).
9pub const NO_LINE_END: &[char] = &['「', '『', '(', '《', '〈', '【', '〔', '“', '‘'];
10
11/// Spaces that a line may break after but never before: ASCII space, ideographic space,
12/// en space (the TC build's wide/padding spaces).
13const BREAK_AFTER_SPACES: &[char] = &[' ', '\u{3000}', '\u{2002}'];
14
15/// CJK for line-breaking purposes (TC tooling: `is_cjk`).
16pub fn is_cjk(c: char) -> bool {
17    let o = c as u32;
18    (0x2E80..=0x9FFF).contains(&o)
19        || (0xF900..=0xFAFF).contains(&o)
20        || (0xFE30..=0xFE4F).contains(&o)
21        || (0xFF00..=0xFFEF).contains(&o)
22        || (0x20000..=0x3FFFF).contains(&o)
23        || NO_LINE_START.contains(&c)
24        || NO_LINE_END.contains(&c)
25}
26
27/// May a line break between `prev` and `next`?
28///
29/// - never before a no-line-start character or before a space;
30/// - never after a no-line-end character;
31/// - always after a space (the engine drops a space at a break);
32/// - between two CJK characters;
33/// - nowhere else (inside Latin words, or where Latin and CJK touch without a space).
34pub fn break_allowed(prev: char, next: char) -> bool {
35    if NO_LINE_START.contains(&next) || BREAK_AFTER_SPACES.contains(&next) {
36        return false;
37    }
38    if NO_LINE_END.contains(&prev) {
39        return false;
40    }
41    if BREAK_AFTER_SPACES.contains(&prev) {
42        return true;
43    }
44    is_cjk(prev) && is_cjk(next)
45}