code wiki / (root) / nx_text_wrap.nx

nx_text_wrap.nx source

↩ module page · 22 lines · 1377 B

1// nx_text_wrap.nx -- SHARED word-level line breaking for the renderer. LAYOUT (height reservation in 2// nx_layout_block) and PAINT (glyph drawing in nx_paint_walk_layout) MUST agree on exactly where lines 3// break, else reserved line-count != painted line-count -> vertical overlap/gaps. So both call this ONE 4// function. Greedy word wrap: a line beginning at `start` extends as far as possible without exceeding 5// `cpl` chars, breaking AFTER the last space within [start, start+cpl]; a word longer than cpl is 6// hard-broken at start+cpl (so progress is guaranteed). Returns the line's END index (exclusive). 7// 100% sovereign. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9 10func tw_next_break(text: *u8, len: i64, cpl: i64, start: i64) -> i64 { 11 if start >= len { return len } 12 var c: i64 = cpl 13 if c < 1 { c = 1 } 14 if len - start <= c { return len } // the rest fits on one line 15 let limit: i64 = start + c // limit < len here (since len-start > c) 16 // find the LAST space in (start, limit] -> break after it so the next line starts at a word 17 var brk: i64 = 0 - 1 18 var i: i64 = limit 19 while i > start { if (text[i] & 0xff) == 32 { brk = i; i = start } else { i = i - 1 } } 20 if brk > start { return brk + 1 } 21 return limit // no space -> hard break (a single long word) 22}