szn

Bug 541 — Thai rule-based reflow edge cases: spaced right-attaching marks (ๆ, ฯ) and Thanthakhat (์) orphan codas

541. Thai rule-based reflow edge cases in the absence of libthai

Severity: LOW
Status: ✅ FIXED
Files: src/thai.zig, src/grid.zig

Problem

When libthai is not installed or available on the host system, szn falls back to its internal rule-based Thai cluster and syllable reflow heuristic in src/grid.zig. While grapheme cluster integrity and MAI HAN AKAT protection were already enforced, two orthographic edge cases remained:

  1. Mai Yamok (ๆ U+0E46) and Paiyannoi (ฯ U+0E2F) with Spaces: Standard Thai typography and newspaper stylebooks (Royal Institute / Daily News StyleBook) require a space before Mai Yamok (e.g., ต่าง ๆ). During grid resizing, rewrap treated any space adjacent to Thai characters as a valid word boundary (space_wb = j + 1). If the line filled up right after the space, ๆ was placed at column 0 of the new line. In Thai typography, starting a line with ๆ or ฯ is strictly prohibited (ห้ามขึ้นต้นบรรทัดด้วยไม้ยมกหรือไปยาลน้อย).

  2. Thanthakhat / Garan (◌์ U+0E4C) Orphan Coda: In words ending with a silent letter (e.g., สัตว์, แพทย์, ศุกร์, จันทร์), if the physical line boundary landed right before the silent letter, the silent letter and its Thanthakhat mark wrapped alone to column 0 of the next line (e.g. สัต on line 1, and ว์ on line 2). A consonant marked with Thanthakhat represents the silent coda of a syllable and can never begin a syllable or a line.

Fix

  1. src/thai.zig:
    • Added cellHasThanthakhat(cell: Cell) bool to identify Thanthakhat (U+0E4C) in comb1/comb2 or as an unattached base cell.
    • Added cellHasVowelMark(cell: Cell) bool and isThaiVowelMark(cp: u21) bool to identify upper/lower Thai vowel marks.
    • Added findThaiSyllableStart(cells: []const Cell, pos: usize, min_idx: usize) usize to scan backward from a coda or right-attaching mark to locate the syllable’s initial consonant or leading vowel.
    • Added unit tests for cellHasThanthakhat, cellHasVowelMark, and findThaiSyllableStart.
  2. src/grid.zig:
    • In rewrap:
      • Disallowed breaking at a space if the immediately following character is a right-attaching mark (thai.isThaiRightAttaching), causing the space scanner to skip it and search for a preceding word boundary.
      • Added a right-attaching fallback check: if a break would place ๆ or ฯ at column 0, rewrap backtracks before the preceding word using findThaiSyllableStart.
      • Added a Thanthakhat fallback check: if breaking at i would place a silent letter (or double-silent cluster like ทร์) at column 0 of the next line, rewrap backtracks before the syllable start using findThaiSyllableStart.
    • Added unit tests:
      • "setSize reflow Thai right-attaching mark (ๆ) with space — bug #541"
      • "setSize reflow Thai Thanthakhat (์) protection — bug #541"