1// What screen readers ask of a node's text, answered from its TextMap2// for every platform (decisions/F14.md, F14b): the unit holding3// an offset, moves by units, the boxes a range covers and the offset at4// a point. Offsets are bytes of the node's text on grapheme boundaries;5// each platform counts them its own way (unicode.alloy). Ranges are in6// the text's order, their boxes in the order they are drawn.78import std::string;9import std::vector;10import std::option;11import std::sort;12import std::math;1314import window;1516// What a screen reader moves by: a character is a grapheme, where the17// caret stops; a word runs to the next one's start, the spaces after it18// included; a line as drawn; a paragraph to a line break, the break19// included.20exp type TextUnit = enum {21 Character,22 Word,23 Line,24 Paragraph,25 Document,26};2728// Where a move ended, and how many units it went (negative backward):29// fewer than asked at the text's ends.30exp type Moved = struct {31 exp at: u32,32 exp count: i32,33};3435// The text a node's map is of: a field's value, else its label.36exp fn mapped_text(self node: &AccessNode) -> &[u8] {37 const s = &node.semantics;38 return if (s.role is ::TextInput) &s.value.view() else &s.label.view();39}4041// The node's map, for callers given only nodes with one.42pub fn map_of(node: &AccessNode) -> &TextMap {43 if (node.text is ::Some |&map|) {44 return ↦45 }46 panic "pkg::alloy::window: a node without a text map";47}4849// The text's length, as the map has it.50exp fn end(self map: &TextMap) -> u32 {51 return if (map.graphemes.length() > 0) map.graphemes.get(map.graphemes.length() - 1) else 0;52}5354// The offset moved back to a grapheme boundary, within the text: what a55// range made before an edit comes to.56exp fn clamped(self map: &TextMap, at: u32) -> u32 {57 var found: u32 = 0;58 for (map.graphemes.view()) |g| {59 if (g > at) {60 break;61 }62 found = g;63 }64 return found;65}6667// The unit's boundaries in order, the text's start and end among them.68fn boundaries(self map: &TextMap, text: &[u8], unit: TextUnit) -> Vector<u32> {69 const end = map.end();70 var out = Vector<u32>::empty();71 out.push(0);72 match (unit) {73 ::Character {74 for (map.graphemes.view()) |g| {75 out.push(g);76 }77 }78 ::Word {79 for (map.words.view()) |w| {80 out.push(w);81 }82 }83 ::Line {84 for (map.lines.view()) |&line| {85 out.push(line.start);86 }87 }88 ::Paragraph {89 for ([..text.length()]) |k| {90 if (text[k] == '\n' && k + 1 < text.length()) {91 out.push((k + 1) to u32);92 }93 }94 }95 ::Document {}96 }97 out.push(end);98 // each once, in order99 var unique = Vector<u32>::empty();100 for (out.view()) |b| {101 if (b <= end && (unique.length() == 0 || unique.get(unique.length() - 1) < b)) {102 unique.push(b);103 }104 }105 return move unique;106}107108// The unit holding the offset, as [start, end): at the text's end an109// empty character or word, and the last line or paragraph unless a line110// break ends the text, whose empty line follows it.111exp fn unit_at(self map: &TextMap, text: &[u8], at: u32, unit: TextUnit) -> [u32 : 2] {112 const end = map.end();113 if (unit is ::Document) {114 return [0, end];115 }116 const b = map.boundaries(&text, unit);117 if (at >= end) {118 const open = unit is ::Character || unit is ::Word || end == 0119 || text[(end - 1) to u64] == '\n';120 if (open || b.length() < 2) {121 return [end, end];122 }123 return [b.get(b.length() - 2), end];124 }125 var k: u64 = 0;126 while (k + 2 < b.length() && b.get(k + 1) <= at) {127 k += 1;128 }129 return [b.get(k), b.get(k + 1)];130}131132// The boundary `count` units on from the offset (back when negative),133// the text's ends stopping it.134exp fn moved(self map: &TextMap, text: &[u8], at: u32, unit: TextUnit, count: i32) -> Moved {135 const b = map.boundaries(&text, unit);136 var now = at;137 var went: i32 = 0;138 while (went != count) {139 var next = now;140 if (count > 0) {141 for (b.view()) |x| {142 if (x > now) {143 next = x;144 break;145 }146 }147 } else {148 for (b.view()) |x| {149 if (x < now) {150 next = x;151 }152 }153 }154 if (next == now) {155 break;156 }157 now = next;158 went += if (count > 0) 1 else -1;159 }160 return Moved { .at = now, .count = went };161}162163// The grapheme starting at the offset, or the one holding it.164fn grapheme_of(self map: &TextMap, at: u32) -> u64 {165 var low: u64 = 0;166 var high = map.graphemes.length();167 while (low < high) {168 const middle = (low + high) / 2;169 if (map.graphemes.get(middle) <= at) {170 low = middle + 1;171 } else {172 high = middle;173 }174 }175 return if (low == 0) 0 else low - 1;176}177178// The line holding the offset: a line's end is the next line's start,179// but the last line's and a line ended by a break's.180exp fn line_of(self map: &TextMap, at: u32) -> u64 {181 const count = map.lines.length();182 for ([..count]) |k| {183 const line = &map.lines.get(k);184 if (at < line.end || k + 1 == count) {185 return k;186 }187 if (at == line.end && map.lines.get(k + 1).start > at) {188 return k;189 }190 }191 return 0;192}193194// The boxes covering the range from start to end, in the node's pixels:195// one a line, more where the direction changes within one, from the196// left. An empty range is the caret's box, a pixel wide.197exp fn boxes(self map: &TextMap, start: u32, end: u32) -> Vector<Area> {198 var out = Vector<Area>::empty();199 if (map.lines.length() == 0) {200 return move out;201 }202 if (start >= end) {203 const k = map.line_of(start);204 const line = &map.lines.get(k);205 var x = if (line.right_to_left) line.box.x + line.box.width else line.box.x;206 if (start > line.start) {207 const edges = map.edges.get(map.grapheme_of(start - 1));208 x = if (line.right_to_left) edges[0] else edges[1];209 }210 out.push(Area { .x = x, .y = line.box.y, .width = 1.0, .height = line.box.height });211 return move out;212 }213 for (map.lines.view()) |&line| {214 if (line.end <= start || line.start >= end) {215 continue;216 }217 // the graphemes of the range on the line, left to right, joined218 // where they touch219 var spans = Vector<[f32 : 2]>::empty();220 var k = map.grapheme_of(max(start, line.start));221 while (k + 1 < map.graphemes.length() && map.graphemes.get(k) < min(end, line.end)) {222 spans.push(map.edges.get(k));223 k += 1;224 }225 sort_by(&var spans.items(), (a, b) => a[0] < b[0]);226 var open = false;227 for (spans.view()) |span| {228 if (open) {229 const last = &var out.get_var(out.length() - 1);230 if (span[0] <= last.x + last.width + 0.5) {231 last.width = max(last.width, span[1] - last.x);232 continue;233 }234 }235 out.push(Area {236 .x = span[0],237 .y = line.box.y,238 .width = span[1] - span[0],239 .height = line.box.height240 });241 open = true;242 }243 }244 return move out;245}246247// The line at the height: the first reaching below it, else the last.248fn line_at(self map: &TextMap, y: f32) -> u64 {249 for (map.lines.view(), [..map.lines.length()]) |&line, k| {250 if (y < line.box.y + line.box.height) {251 return k;252 }253 }254 return map.lines.length() - 1;255}256257// The grapheme boundary nearest the point, in the node's pixels, on the258// line at its height: a grapheme's start at its edge the line reads259// from, its end at the other.260exp fn offset_at(self map: &TextMap, x: f32, y: f32) -> u32 {261 if (map.lines.length() == 0) {262 return 0;263 }264 const line = &map.lines.get(map.line_at(y));265 var best = line.start;266 var distance: f32 = 1000000000.0;267 var k = map.grapheme_of(line.start);268 while (k + 1 < map.graphemes.length() && map.graphemes.get(k) < line.end) {269 const edges = map.edges.get(k);270 const ends: [u32 : 2] = [map.graphemes.get(k), map.graphemes.get(k + 1)];271 const sides: [f32 : 2] = if (line.right_to_left) [edges[1], edges[0]] else edges;272 for ([..2 to u64]) |side| {273 const d = if (sides[side] > x) sides[side] - x else x - sides[side];274 if (d < distance) {275 distance = d;276 best = ends[side];277 }278 }279 k += 1;280 }281 return best;282}283284// The grapheme drawn at the point, by its start: none past the text.285exp fn grapheme_at(self map: &TextMap, x: f32, y: f32) -> Option<u32> {286 for (map.lines.view()) |&line| {287 if (y < line.box.y || y >= line.box.y + line.box.height) {288 continue;289 }290 var k = map.grapheme_of(line.start);291 while (k + 1 < map.graphemes.length() && map.graphemes.get(k) < line.end) {292 const edges = map.edges.get(k);293 if (x >= edges[0] && x < edges[1]) {294 return ::Some(map.graphemes.get(k));295 }296 k += 1;297 }298 }299 return ::None;300}301302// The lines drawn within the height, from the top: what shows of a field.303exp fn visible_lines(self map: &TextMap, height: f32) -> [u32 : 2] {304 var first: Option<u32> = ::None;305 var last: u32 = 0;306 for (map.lines.view()) |&line| {307 if (line.box.y + line.box.height > 0.0 && line.box.y < height) {308 if (first is ::None) {309 first = ::Some(line.start);310 }311 last = line.end;312 }313 }314 return if (first is ::Some |f|) [f, last] else [0, 0];315}316317// A map for a text no layout was sent for (a combo box's value, a318// button's): a grapheme a character, one line over the node, no words.319exp fn TextMap::plain(text: &[u8], bounds: &Area) -> TextMap {320 var map = TextMap {321 .revision = 0,322 .graphemes = Vector<u32>::empty(),323 .edges = Vector<[f32 : 2]>::empty(),324 .words = Vector<u32>::empty(),325 .lines = Vector<TextLine>::empty(),326 .format = TextFormat {327 .family = String::empty(),328 .size = 0.0,329 .weight = 400,330 .color = [0, 0, 0, 255],331 .language = String::empty(),332 .heading = false,333 .read_only = true334 }335 };336 for ([..text.length()]) |k| {337 if ((text[k] & 0xC0) != 0x80) {338 map.graphemes.push(k to u32);339 map.edges.push([0.0, bounds.width]);340 }341 }342 map.graphemes.push(text.length() to u32);343 map.lines.push(TextLine {344 .start = 0,345 .end = text.length() to u32,346 .box = Area { .x = 0.0, .y = 0.0, .width = bounds.width, .height = bounds.height },347 .right_to_left = false348 });349 return move map;350}