1// What a screen reader reads of a text as laid out (decisions/2// F14.md, F14b, questions 5B and 6A): a field's TextMap, or a text's3// laid out on several lines, made as it is laid out while one reads and4// sent with its node. Never on the web, whose layer the browser reads by5// its own units.67import std::string;8import std::vector;9import std::option;10import std::text as std_text;11import pkg::alloy::window;12import pkg::alloy::gpu;13import pkg::alloy::text as text;1415import pkg::alloy::ui;16import pkg::alloy::ui::tree;17import pkg::alloy::ui::languages;18import ui_access;1920// The map of the layout drawn at `origin` in its element, in the21// window's pixels at the Ui's scale, numbered anew.22pub fn text_map(23 self ui: &var Ui,24 laid: &text::Layout,25 origin: Point,26 format: TextFormat27) -> TextMap {28 const s = ui.scale;29 const t = &laid.text.view();30 const state = &var ui.access_state();31 state.revision += 1;32 var map = TextMap {33 .revision = state.revision,34 .graphemes = Vector<u32>::empty(),35 .edges = Vector<[f32 : 2]>::empty(),36 .words = text::word_starts(&t),37 .lines = Vector<TextLine>::with_capacity(laid.lines.length()),38 .format = move format39 };40 for (laid.lines.view()) |&line| {41 map.lines.push(TextLine {42 .start = line.start to u32,43 .end = line.end to u32,44 .box = Area {45 .x = (origin.x + line.x) * s,46 .y = (origin.y + line.top) * s,47 .width = line.width * s,48 .height = line.height * s49 },50 .right_to_left = line.direction is ::RightToLeft51 });52 }53 // the layout's graphemes in the text's order, and between them what54 // draws nothing, line breaks, at the end of their lines55 var at: u32 = 0;56 for (laid.graphemes_in_order().view()) |&g| {57 map.breaks(&t, at, g.start);58 map.graphemes.push(g.start);59 map.edges.push([(origin.x + g.left) * s, (origin.x + g.right) * s]);60 at = g.end;61 }62 map.breaks(&t, at, t.length() to u32);63 map.graphemes.push(t.length() to u32);64 return move map;65}6667// Adds the graphemes from `start` to `end`, which nothing draws: a line68// break each (CR LF together), at the end of the line it ends.69fn breaks(self map: &var TextMap, t: &[u8], start: u32, end: u32) {70 var at = start;71 while (at < end) {72 var k: u64 = 0;73 while (k + 1 < map.lines.length() && at >= map.lines.get(k).end) {74 k += 1;75 }76 var x: f32 = 0.0;77 if (map.lines.length() > 0) {78 const b = &map.lines.get(k);79 x = if (b.right_to_left) b.box.x else b.box.x + b.box.width;80 }81 map.graphemes.push(at);82 map.edges.push([x, x]);83 const crlf = t[at to u64] == '\r' && at + 1 < end && t[(at + 1) to u64] == '\n';84 at += if (crlf) 2 else 1;85 // a character of several bytes is one86 while (at < end && (t[at to u64] & 0xC0) == 0x80) {87 at += 1;88 }89 }90}9192// How a text is drawn, to a screen reader: its face's family (its93// PostScript name's, before the style), size in logical pixels, weight,94// color and the Ui's language.95pub fn text_format(96 self ui: &var Ui,97 laid: &text::Layout,98 size: f32,99 weight: u32,100 color: Color101) -> TextFormat {102 var family = String::empty();103 if (laid.glyphs.length() > 0) {104 const name = ui.fonts().face_name(laid.glyphs.get(0).face);105 const whole = &name.view();106 const cut = if (whole.find_byte('-', 0) is ::Some |dash|) dash else whole.length();107 family.append(&whole[..cut]);108 }109 return TextFormat {110 .family = move family,111 .size = size,112 .weight = weight,113 .color = [channel(color.r), channel(color.g), channel(color.b), channel(color.a)],114 .language = copy ui.spoken_language(),115 .heading = false,116 .read_only = false117 };118}119120fn channel(value: f32) -> u8 {121 const clamped = if (value < 0.0) 0.0 else if (value > 1.0) 1.0 else value;122 return (clamped * 255.0 + 0.5) to u8;123}