1// Laying out text in either direction: a paragraph's levels by UAX #92// (bidi.alloy), and a line's glyphs put in visual order by them.34import std::vector;5import std::option;6import std::math;78import pkg::alloy::text;9import pkg::alloy::text::paragraphs;10import pkg::alloy::text::unicode::properties;11import script_tables;12import script_properties;13import bidi;1415// The levels of the paragraph's bytes (UAX #9): all 0 for a paragraph16// left to right with nothing in it right to left, as most are, which17// takes no more than a look at each character.18pub fn paragraph_levels(19 text: &[u8],20 start: u64,21 end: u64,22 direction: Direction23) -> ParagraphLevels {24 var out = ParagraphLevels { .level = 0, .runs = Vector<LevelRun>::empty(), .bidi = false };25 var plain = !(direction is ::RightToLeft);26 // AutoRightToLeft reads left to right only by a letter that does27 var left = !(direction is ::AutoRightToLeft);28 if (plain) {29 // ASCII has no right-to-left character, nor do most others30 var at = start;31 while (at < end && plain) {32 const c = text[at];33 if (c < 0x80) {34 left = left || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z');35 at += 1;36 continue;37 }38 const d = decode(&text, at);39 match (bidi_class(d.c)) {40 ::L {41 left = true;42 }43 ::R, ::AL, ::AN, ::LRE, ::RLE, ::LRO, ::RLO, ::PDF, ::LRI, ::RLI, ::FSI, ::PDI {44 plain = false;45 }46 else {}47 }48 at += d.length;49 }50 }51 if (plain && left) {52 out.runs.push(LevelRun { .end = end, .level = 0 });53 return move out;54 }55 var chars = Vector<u32>::with_capacity(end - start);56 var classes = Vector<BidiClass>::with_capacity(end - start);57 var offsets = Vector<u64>::with_capacity(end - start);58 var at = start;59 while (at < end) {60 const d = decode(&text, at);61 chars.push(d.c);62 classes.push(bidi_class(d.c));63 offsets.push(at);64 at += d.length;65 }66 const level: Option<u8> = match (direction) {67 ::Auto, ::AutoRightToLeft ::None;68 ::LeftToRight ::Some(0);69 ::RightToLeft ::Some(1);70 };71 const resolved = bidi_levels(&classes.view(), &chars.view(), level, unmarked_level(direction));72 out.level = resolved.paragraph;73 out.bidi = true;74 for (resolved.levels.view(), offsets.view()) |l, offset| {75 if (out.runs.length() > 0 && out.runs.last().level == l) {76 continue;77 }78 if (out.runs.length() > 0) {79 out.runs.get_var(out.runs.length() - 1).end = offset;80 }81 out.runs.push(LevelRun { .end = end, .level = l });82 }83 if (out.runs.length() == 0) {84 out.runs.push(LevelRun { .end = end, .level = out.level });85 }86 return move out;87}8889fn is_auto(direction: Direction) -> bool => direction is ::Auto || direction is ::AutoRightToLeft;9091// The level of a paragraph with no strong character.92fn unmarked_level(direction: Direction) -> u8 => if (93 direction is ::RightToLeft || direction is ::AutoRightToLeft94) 1 else 0;9596// The line's glyphs in visual order, as indices from its first, from97// the code points their clusters start with and their levels: its98// whitespace at the end and before tabs back at the paragraph's level99// (L1), each run of a level reversed (L2).100pub fn reorder_line(chars: &[u32], levels: &var [u8], paragraph: u8, order: &var [u64]) {101 var classes = Vector<BidiClass>::with_capacity(chars.length());102 for (chars) |c| {103 classes.push(bidi_class(c));104 }105 reset_whitespace(&classes.view(), &var levels, paragraph);106 var highest: u8 = 0;107 var lowest_odd: u8 = 255;108 for (levels) |l| {109 highest = max(highest, l);110 if (l % 2 == 1) {111 lowest_odd = min(lowest_odd, l);112 }113 }114 reorder(&var order, &levels, highest, lowest_odd);115}