79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tui / width.zig
4.1 KB
1const tables = @import("width_tables.zig"); 2const utf8 = @import("../text.zig"); 3 4pub const Cluster = struct { text: []const u8, width: u8 }; 5 6fn inRanges(comptime ranges: []const tables.Range, cp: u21) bool { 7 var lo: usize = 0; 8 var hi: usize = ranges.len; 9 while (lo < hi) { 10 const mid = (lo + hi) / 2; 11 if (cp < ranges[mid].lo) hi = mid else if (cp > ranges[mid].hi) lo = mid + 1 else return true; 12 } 13 return false; 14} 15 16fn classOf(cp: u21) ?tables.Class { 17 var lo: usize = 0; 18 var hi: usize = tables.grapheme.len; 19 while (lo < hi) { 20 const mid = (lo + hi) / 2; 21 if (cp < tables.grapheme[mid].lo) { 22 hi = mid; 23 } else if (cp > tables.grapheme[mid].hi) { 24 lo = mid + 1; 25 } else { 26 return tables.grapheme[mid].class; 27 } 28 } 29 return null; 30} 31 32fn isZero(cp: u21) bool { 33 return inRanges(&tables.zero, cp); 34} 35 36pub fn charWidth(cp: u21) u8 { 37 if (cp < 0x20 or (cp >= 0x7f and cp < 0xa0)) return 0; 38 if (isZero(cp)) return 0; 39 if (inRanges(&tables.wide, cp)) return 2; 40 return 1; 41} 42 43fn isExtPict(cp: u21) bool { 44 return inRanges(&tables.extended_pictographic, cp); 45} 46fn isEmoji(cp: u21) bool { 47 return inRanges(&tables.emoji, cp); 48} 49fn isConsonant(cp: u21) bool { 50 return inRanges(&tables.consonant, cp); 51} 52fn isLinker(cp: u21) bool { 53 return inRanges(&tables.linker, cp); 54} 55 56const State = struct { 57 ri: u32 = 0, 58 ext_pict: bool = false, 59 zwj_ext_pict: bool = false, 60 indic: u2 = 0, 61}; 62 63fn startsWith(state: *State, cp: u21) void { 64 state.* = .{ 65 .ri = if (classOf(cp) == .regional_indicator) 1 else 0, 66 .ext_pict = isExtPict(cp), 67 .indic = if (isConsonant(cp)) 1 else 0, 68 }; 69} 70 71fn extends(state: *State, cur: u21) void { 72 const cur_class = classOf(cur); 73 state.ri = if (cur_class == .regional_indicator) state.ri + 1 else 0; 74 if (cur_class == .zwj) { 75 state.zwj_ext_pict = state.ext_pict; 76 } else if (cur_class != .extend and cur_class != .spacingmark) { 77 state.ext_pict = isExtPict(cur); 78 state.zwj_ext_pict = false; 79 } 80 if (isConsonant(cur)) { 81 state.indic = 1; 82 } else if (isLinker(cur) and state.indic >= 1) { 83 state.indic = 2; 84 } 85} 86 87fn noBreak(state: State, prev: u21, cur: u21) bool { 88 const a = classOf(prev); 89 const b = classOf(cur); 90 if (a == .cr and b == .lf) return true; 91 if (a == .cr or a == .lf or a == .control) return false; 92 if (b == .cr or b == .lf or b == .control) return false; 93 if (a == .l and (b == .l or b == .v or b == .lv or b == .lvt)) return true; 94 if ((a == .lv or a == .v) and (b == .v or b == .t)) return true; 95 if ((a == .lvt or a == .t) and b == .t) return true; 96 if (b == .extend or b == .zwj) return true; 97 if (b == .spacingmark) return true; 98 if (a == .prepend) return true; 99 if (a != null and a.? == .zwj and state.zwj_ext_pict and isExtPict(cur)) return true; 100 if (a == .regional_indicator and b == .regional_indicator and state.ri % 2 == 1) return true; 101 if (state.indic == 2 and isConsonant(cur)) return true; 102 return false; 103} 104 105fn clusterWidth(text: []const u8) u8 { 106 var width: u8 = 0; 107 var has_vs16 = false; 108 var base: u21 = 0; 109 var i: usize = 0; 110 var first = true; 111 while (i < text.len) { 112 const d = utf8.decodeAt(text, i) orelse break; 113 if (d.cp == 0xfe0f) has_vs16 = true; 114 if (first) { 115 base = d.cp; 116 first = false; 117 } 118 width = @max(width, charWidth(d.cp)); 119 i += d.len; 120 } 121 if (has_vs16 and isEmoji(base)) width = @max(width, 2); 122 return width; 123} 124 125pub fn nextCluster(text: []const u8, i: usize) ?Cluster { 126 const d = utf8.decodeAt(text, i) orelse return null; 127 var state = State{}; 128 startsWith(&state, d.cp); 129 var end = i + d.len; 130 var prev = d.cp; 131 while (end < text.len) { 132 const n = utf8.decodeAt(text, end) orelse break; 133 if (!noBreak(state, prev, n.cp)) break; 134 extends(&state, n.cp); 135 prev = n.cp; 136 end += n.len; 137 } 138 return .{ .text = text[i..end], .width = clusterWidth(text[i..end]) }; 139}