Implement early-exit optimization to avoid expensive width calculations when
we can determine that all remaining content will definitely fit on the current line.
The typesetter was incorrectly measuring line width when expressions contained
superscripts or subscripts (e.g., b²). After rendering a superscript, the line
is split into multiple display segments, but the width checking code was only
measuring the current segment, not the total visual line width.
Key changes:
- Use currentPosition.x to track actual horizontal position across all segments
- Calculate visualLineWidth = currentPosition.x + currentSegmentWidth
- Pass remainingWidth (maxWidth - currentPosition.x) to findBestBreakPoint
- Apply fix to both interatom breaking and inline text breaking
This fixes truncation issues where content like "Δ=b²-4ac avec a=1..." was
being clipped instead of wrapped to a new line.
Before: Each segment checked in isolation → segments appeared to fit individually
but total visual width exceeded maxWidth → content truncated/clipped
After: Total visual width tracked correctly → line breaking triggered when
actual visual width exceeds maxWidth → content wraps properly
Fixed line breaking that would split words like "équivaut" into "é" on one
line and "quivaut" on the next line, even though they're part of the same word.
Root cause analysis (from debug logging):
When text contains accented characters in decomposed form (e + combining
accent), the system processes them as separate atoms:
1. "é" is processed as an accent atom, composed, and added to currentLine
2. "quivaut " is processed as the next ordinary atom
3. Before adding "quivaut ", checkAndPerformInteratomLineBreak() is called
4. This function sees that adding "quivaut " would exceed maxWidth
5. It breaks and flushes the line with "é" at the end
6. "quivaut " starts on a new line
Result: "é" appears alone at the end of one line, "quivaut " on the next.
The fix:
Modified checkAndPerformInteratomLineBreak() in MTTypesetter.swift to detect
when we're about to break in the middle of a word.