szn

Bug 502 — findThaiClusterEnd splits เ-าะ compound vowel syllables

502. Single-vowel consumption splits compound vowels like เ-าะ

Severity: MEDIUM
Status: 🟢 FIXED
Files: src/thai.zig:152-156, src/grid.zig:560-566, docs/text_reflow.md:58

Problem

In src/thai.zig, findThaiClusterEnd groups Thai graphemes into indivisible visual clusters so that reflow never wraps across intra-cluster boundaries:

    // Check for an optional following vowel
    if (isThaiFollowingVowel(line[pos].char)) {
        pos += 1;
    }

The function uses an if check, consuming at most one following vowel. However, standard Thai orthography features compound vowels that use two consecutive following vowels. In particular, the เ-าะ pattern (e.g., เกาะ, เฉพาะ, เบาะ, เหมาะ) consists of:

  1. เ (U+0E40, SARA E — leading vowel)
  2. Base consonant, e.g. ก (U+0E01, KO KAI)
  3. า (U+0E32, SARA AA — following vowel)
  4. ะ (U+0E30, SARA A — following vowel)

Trace on “เกาะ”:

  1. Leading vowel เ is consumed (pos = 1).
  2. Base consonant ก is consumed (pos = 2).
  3. First following vowel า is consumed by if (isThaiFollowingVowel(...)) (pos = 3).
  4. Trailing right-attaching check misses ะ because U+0E30 is a following vowel, not right-attaching (U+0E2F / U+0E46).
  5. Cluster ends at index 3 ([เ, ก, า]).
  6. The next cluster scan begins at index 3 (ะ). Since isThaiBase(0x0E30) returns false, line 140 returns start + 1 = 4.
  7. ะ becomes an orphaned 1-cell pseudo-cluster.

During pane resize and reflow in src/grid.zig, wrap boundaries are calculated across cluster endings. Because ะ is in its own cluster, rewrap can legally break lines between เกา and ะ (wrapping as เกา|ะ).

Proposed Fix

  1. In src/thai.zig, replace the single if check with a while loop to consume all consecutive following vowels:
    // Consume consecutive following vowels (e.g. าะ in เ-าะ)
    while (pos < line.len and isThaiFollowingVowel(line[pos].char)) {
        pos += 1;
    }
    

    This is safe against over-consumption because following vowels (isThaiFollowingVowel) cannot validly start a syllable cluster anyway.

  2. Update the docstring of findThaiClusterEnd and the cluster syntax in docs/text_reflow.md:58 from [Following Vowel]? to [Following Vowels]*.
  3. Add a unit test in src/thai.zig verifying that a 4-cell word such as เกาะ forms a single cluster of length 4.

Resolution