mln-rs-engine 0.0.19

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Lines and runs of text in visual order.
//!
//! The `unicode-bidi` crate resolves levels and visual runs (UAX #9), and
//! this module writes them: runs in visual order, RTL runs reversed by
//! character with each character replaced by its `Bidi_Mirroring_Glyph`
//! (rule L4, which unicode-bidi leaves to the renderer; `crate::text::bidi_mirroring`,
//! from the same Unicode version as unicode-bidi's tables), and BiDi controls
//! removed.
//!
//! The line splitters take line break points as offsets into the text, in
//! characters. Each must end a nonempty line within the text (`0 < point <=
//! len`); the end of each paragraph is a line break point whether or not the
//! caller passes it.

use std::ops::Range;

use mln_style::collections::VecSet;
use unicode_bidi::{BidiClass, BidiInfo};

use crate::text::bidi_mirroring::BIDI_MIRRORING_GLYPH;
use crate::text::tagged_string::{SectionIndex, StyledText};

/// Whether the first character in `text` with a strong direction runs right
/// to left.
#[must_use]
pub fn starts_right_to_left(text: &[char]) -> bool {
    for &ch in text {
        match unicode_bidi::bidi_class(ch) {
            BidiClass::L => return false,
            BidiClass::R | BidiClass::AL => return true,
            _ => {}
        }
    }
    false
}

/// The character's `Bidi_Mirroring_Glyph`, or the character itself if it has
/// none.
fn mirror(c: char) -> char {
    u16::try_from(u32::from(c))
        .ok()
        .and_then(|c| BIDI_MIRRORING_GLYPH.binary_search_by_key(&c, |&(from, _)| from).ok())
        .and_then(|i| char::from_u32(u32::from(BIDI_MIRRORING_GLYPH[i].1)))
        .unwrap_or(c)
}

/// Whether `c` is a BiDi control that visual-order text leaves out.
fn is_bidi_control(c: char) -> bool {
    matches!(
        c,
        // ZWNJ, ZWJ, LRM and RLM
        '\u{200C}'..='\u{200F}'
        // LRE, RLE, PDF, LRO and RLO
        | '\u{202A}'..='\u{202E}'
        // LRI, RLI, FSI and PDI
        | '\u{2066}'..='\u{2069}'
    )
}

/// Appends the run `src` to `out` in visual order without BiDi controls: an
/// RTL run reversed, each character mirrored, and an LTR run as it is.
fn write_run(src: &[char], rtl: bool, out: &mut Vec<char>) {
    let kept = |&&c: &&char| !is_bidi_control(c);
    if rtl {
        out.extend(src.iter().rev().filter(kept).map(|&c| mirror(c)));
    } else {
        out.extend(src.iter().filter(kept));
    }
}

/// A run of text in one direction, in logical order.
#[derive(Clone, Debug, PartialEq)]
pub struct BiDiRun {
    pub(crate) text: Vec<char>,
    pub(crate) rtl: bool,
}

/// The bidi analysis of text given as characters. unicode-bidi reads text
/// as UTF-8 or UTF-16 and indexes it by code unit, so the text goes through
/// UTF-8, and this maps between its byte offsets and the characters'
/// indices.
struct Analysis<'text> {
    info: BidiInfo<'text>,
    /// The byte offset of each character and of the end of the text; empty
    /// when the text is ASCII, where the two are the same.
    offsets: Vec<usize>,
}

impl<'text> Analysis<'text> {
    fn new(text: &[char], utf8: &'text mut String) -> Self {
        utf8.reserve(text.iter().map(|c| c.len_utf8()).sum());
        utf8.extend(text);
        let offsets = if utf8.len() == text.len() {
            Vec::new()
        } else {
            let mut offsets = Vec::with_capacity(text.len() + 1);
            offsets.push(0);
            offsets.extend(text.iter().scan(0, |offset, c| {
                *offset += c.len_utf8();
                Some(*offset)
            }));
            offsets
        };
        Analysis { info: BidiInfo::new(utf8, None), offsets }
    }

    /// The byte offset of the character at `index`, or of the text's end.
    fn byte(&self, index: usize) -> usize {
        if self.offsets.is_empty() { index } else { self.offsets[index] }
    }

    /// The index of the character that starts at byte `offset`, or of the
    /// text's end.
    fn char(&self, offset: usize) -> usize {
        if self.offsets.is_empty() { offset } else { self.offsets.partition_point(|&o| o < offset) }
    }

    fn chars(&self, bytes: Range<usize>) -> Range<usize> {
        self.char(bytes.start)..self.char(bytes.end)
    }

    /// The text's paragraphs, as ranges of characters.
    fn paragraphs(&self) -> impl Iterator<Item = Range<usize>> {
        self.info.paragraphs.iter().map(|paragraph| self.chars(paragraph.range.clone()))
    }

    /// The lines of the text that end at `line_breaks` and at the ends of its
    /// paragraphs, so that no line crosses a paragraph boundary.
    fn lines(&self, mut line_breaks: VecSet<usize>) -> impl Iterator<Item = Range<usize>> {
        let len = self.char(self.info.text.len());
        debug_assert!(
            line_breaks.iter().all(|&point| 0 < point && point <= len),
            "line breaks {line_breaks:?} outside 1..={len}"
        );
        for paragraph in self.paragraphs() {
            line_breaks.insert(paragraph.end);
        }
        let mut start = 0;
        // A release build drops points outside the text rather than panic.
        line_breaks.into_iter().filter(move |&end| 0 < end && end <= len).map(move |end| {
            let line = start..end;
            start = end;
            line
        })
    }

    /// The visual runs of `line`, a nonempty line of the text within one
    /// paragraph: whether each runs right to left, and its logical range, in
    /// visual order.
    fn line_runs(&self, line: Range<usize>) -> Vec<(bool, Range<usize>)> {
        let line = self.byte(line.start)..self.byte(line.end);
        let paragraphs = &self.info.paragraphs;
        let paragraph = &paragraphs[paragraphs.partition_point(|p| p.range.end <= line.start)];
        let (levels, runs) = self.info.visual_runs(paragraph, line);
        runs.into_iter().map(|run| (levels[run.start].is_rtl(), self.chars(run))).collect()
    }
}

/// Splits `text`, in logical order, into lines at `line_breaks`, and writes
/// each line in visual order.
pub(crate) fn reorder_lines(text: &[char], line_breaks: VecSet<usize>) -> Vec<Vec<char>> {
    let mut utf8 = String::new();
    let analysis = Analysis::new(text, &mut utf8);
    analysis
        .lines(line_breaks)
        .map(|line| {
            let mut out = Vec::with_capacity(line.len());
            for (rtl, run) in analysis.line_runs(line) {
                write_run(&text[run], rtl, &mut out);
            }
            out
        })
        .collect()
}

/// Like [`reorder_lines`], with each character's style, except that LTR runs
/// keep their BiDi controls, and an RTL run's first character takes the
/// style of the chunk after it unless either style is a block.
pub(crate) fn reorder_styled_lines(input: &StyledText, line_breaks: VecSet<usize>) -> Vec<StyledText> {
    reorder_styled_lines_with_blocks(input, line_breaks, &input.text, &[])
}

/// Like [`reorder_styled_lines`], but the bidi analysis reads `bidi_text`,
/// which is as long as the input's text, and a right-to-left run keeps the
/// order of the text in the styles that `block_styles` marks, which a shaper
/// already laid out in visual order.
pub(crate) fn reorder_styled_lines_with_blocks(
    input: &StyledText,
    line_breaks: VecSet<usize>,
    bidi_text: &[char],
    block_styles: &[bool],
) -> Vec<StyledText> {
    debug_assert_eq!(bidi_text.len(), input.text.len());
    let input_text = &input.text;
    let style_indices = &input.styles;
    let is_block = |style: SectionIndex| block_styles.get(style as usize).copied().unwrap_or(false);

    let mut utf8 = String::new();
    let analysis = Analysis::new(bidi_text, &mut utf8);
    let mut out = Vec::new();
    for line_range in analysis.lines(line_breaks) {
        let mut line = StyledText { text: Vec::new(), styles: Vec::with_capacity(line_range.len()) };

        for (is_reversed, run) in analysis.line_runs(line_range) {
            let logical_start = run.start;
            let logical_end = run.end;
            if is_reversed {
                // Within this reversed section, iterate logically
                // backwards. Each time we see a change in style, render
                // a reversed chunk of everything since the last change.
                //
                let mut style_run_start = logical_end;
                let mut current_style_index = style_indices[style_run_start - 1];
                let mut add_chunk = |start: usize, end: usize, style: SectionIndex| {
                    // A block's text is already in visual order.
                    let mut reversed = Vec::new();
                    if is_block(style) {
                        reversed.extend_from_slice(&input_text[start..end]);
                    } else {
                        write_run(&input_text[start..end], true, &mut reversed);
                    }
                    line.text.extend_from_slice(&reversed);
                    line.styles.extend(std::iter::repeat_n(style, reversed.len()));
                };
                let mut i = logical_end - 1;
                while i >= logical_start {
                    if current_style_index != style_indices[i] || i == logical_start {
                        // The run's first character joins the chunk
                        // after it even when its style differs, as GL JS
                        // lays it out, except next to a block, which
                        // holds only its own text.
                        let split_first = i == logical_start
                            && current_style_index != style_indices[i]
                            && (is_block(current_style_index) || is_block(style_indices[i]));
                        let style_run_end = if i == logical_start && !split_first { i } else { i + 1 };
                        add_chunk(style_run_end, style_run_start, current_style_index);
                        current_style_index = style_indices[i];
                        style_run_start = style_run_end;
                        if split_first {
                            add_chunk(i, i + 1, current_style_index);
                            style_run_start = i;
                        }
                    }
                    if i == 0 {
                        break;
                    }
                    i -= 1;
                }
            } else {
                line.text.extend_from_slice(&input_text[logical_start..logical_end]);
                line.styles.extend_from_slice(&style_indices[logical_start..logical_end]);
            }
        }

        out.push(line);
    }
    out
}

/// Splits text in logical order into runs of one direction each, and
/// returns the runs in visual order: each paragraph's runs in visual order,
/// the paragraphs in logical order. Each run keeps its logical order, so a
/// shaper such as HarfBuzz can lay it out in its direction. Text with no runs
/// is one LTR run.
#[must_use]
pub fn visual_runs(text: &[char]) -> Vec<BiDiRun> {
    let mut runs = Vec::new();
    if text.is_empty() {
        return runs;
    }

    let mut utf8 = String::new();
    let analysis = Analysis::new(text, &mut utf8);
    for paragraph in analysis.paragraphs() {
        for (rtl, run) in analysis.line_runs(paragraph) {
            if run.is_empty() {
                continue;
            }
            runs.push(BiDiRun { text: text[run].to_vec(), rtl });
        }
    }

    if runs.is_empty() {
        runs.push(BiDiRun { text: text.to_vec(), rtl: false });
    }
    runs
}

/// These tests mirror the unit tests in mapbox-gl-rtl-text's arabic.test.js.
#[cfg(test)]
mod bidi_test {
    use super::*;
    use crate::text::arabic::shape_arabic;

    fn u(s: &str) -> Vec<char> {
        s.chars().collect()
    }

    fn set(v: &[usize]) -> VecSet<usize> {
        v.iter().copied().collect()
    }

    fn styled(text: Vec<char>, styles: Vec<SectionIndex>) -> StyledText {
        StyledText { text, styles }
    }

    // gtest: BiDi.ReverseArabic
    #[test]
    fn reverse_arabic() {
        assert_eq!(reorder_lines(&shape_arabic(&u("سلام۳۹")), set(&[])), vec![u("۳۹ﻡﻼﺳ")]);
    }

    // gtest: BiDi.ReverseMixed
    #[test]
    fn reverse_mixed() {
        assert_eq!(
            reorder_lines(&shape_arabic(&u("مكتبة الإسكندرية\u{200E}\u{200E} Maktabat al-Iskandarīyah")), set(&[])),
            vec![u(" Maktabat al-Iskandarīyahﺔﻳﺭﺪﻨﻜﺳﻹﺍ ﺔﺒﺘﻜﻣ")]
        );
    }

    // gtest: BiDi.WithLineBreaks
    #[test]
    fn with_line_breaks() {
        let expected = vec![u(" ﺔﻳﺭﺪﻨﻜﺳﻹﺍ ﺔﺒﺘﻜﻣ"), u("Maktabat al-"), u("Iskandarīyah")];
        assert_eq!(
            reorder_lines(
                &shape_arabic(&u("مكتبة الإسكندرية\u{200E}\u{200E} Maktabat al-Iskandarīyah")),
                set(&[18, 30])
            ),
            expected
        );
    }

    // gtest: BiDi.StyledText
    #[test]
    fn styled_text() {
        // This test uses line breaks that intentionally split/reorder/
        // interleave styled blocks that are contiguous in the input
        let input = styled(
            shape_arabic(&u("مكتبة الإسكندرية\u{200E}\u{200E} Maktabat al-Iskandarīyah")).into_owned(),
            vec![
                0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6,
                6, 6, 6, 6, 6, 7, 7, 7,
            ],
        );

        let expected: Vec<StyledText> = vec![
            styled(u("ﺔﺒﺘﻜﻣ"), vec![0, 0, 0, 0, 0]),
            styled(u(" \u{200E}\u{200E}ﺔﻳﺭﺪﻨﻜﺳﻹﺍ "), vec![2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1]),
            styled(u("Maktabat al-"), vec![2, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6]),
            styled(u("Iskandarīyah"), vec![6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7]),
        ];
        for line in &expected {
            assert_eq!(line.text.len(), line.styles.len());
        }

        assert_eq!(reorder_styled_lines(&input, set(&[5, 18, 30])), expected);
    }

    // gtest: BiDi.VisualRuns
    #[test]
    fn visual_runs_split_by_direction() {
        let runs = visual_runs(&u("Erbil هەولێر"));
        assert_eq!(runs.len(), 2);
        assert_eq!(runs[0].text, u("Erbil "));
        assert!(!runs[0].rtl);
        assert_eq!(runs[1].text, u("هەولێر"));
        assert!(runs[1].rtl);
    }

    // gtest: BiDi.VisualRunsRightToLeftParagraph
    #[test]
    fn visual_runs_right_to_left_paragraph() {
        // The runs come in visual order, left to right, each in logical order.
        let runs = visual_runs(&u("هەولێر Erbil"));
        assert_eq!(runs.len(), 2);
        assert_eq!(runs[0].text, u("Erbil"));
        assert!(!runs[0].rtl);
        assert_eq!(runs[1].text, u("هەولێر "));
        assert!(runs[1].rtl);
    }

    // gtest: BiDi.BlocksKeepTheirOrderInRightToLeftRuns
    #[test]
    fn blocks_keep_their_order_in_right_to_left_runs() {
        // "XY" stands for glyphs that a shaper already laid out right to
        // left. Bidi sees them as Hebrew letters, and moves them without
        // reversing them.
        let input = styled(u("XY Erbil"), vec![0, 0, 1, 1, 1, 1, 1, 1]);
        let lines = reorder_styled_lines_with_blocks(&input, set(&[]), &u("אא Erbil"), &[true, false]);
        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0].text, u("Erbil XY"));
        assert_eq!(lines[0].styles, vec![1, 1, 1, 1, 1, 1, 0, 0]);
    }

    // gtest: BiDi.BlockAtTheStartOfARunTakesOnlyItsOwnText
    #[test]
    fn block_at_the_start_of_a_run_takes_only_its_own_text() {
        // The run "-XY" reads right to left, and its first character, "-",
        // is not part of the block.
        let input = styled(u("-XY"), vec![0, 1, 1]);
        let lines = reorder_styled_lines_with_blocks(&input, set(&[]), &u("-אא"), &[false, true]);
        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0].text, u("XY-"));
        assert_eq!(lines[0].styles, vec![1, 1, 0]);
    }

    #[test]
    fn starts_right_to_left_at_the_first_strong_character() {
        assert!(!starts_right_to_left(&u("1 abc שלום")));
        assert!(starts_right_to_left(&u("1 שלום abc")));
        assert!(starts_right_to_left(&u("(سلام)")));
        // A supplementary letter (Adlam).
        assert!(starts_right_to_left(&u("\u{1E900}")));
        assert!(!starts_right_to_left(&u("123 !?")));
    }

    #[test]
    fn mirrors_the_characters_unicode_mirrors() {
        assert_eq!(mirror('('), ')');
        assert_eq!(mirror('<'), '>');
        assert_eq!(mirror('«'), '»');
        assert_eq!(mirror('≤'), '≥');
        assert_eq!(mirror('a'), 'a');
        assert_eq!(mirror('\u{1E900}'), '\u{1E900}');
    }

    #[test]
    fn writes_rtl_runs_mirrored() {
        let mut out = Vec::new();
        write_run(&u("א<ב(ג"), true, &mut out);
        assert_eq!(out, u("ג)ב>א"));
    }

    #[test]
    fn writes_runs_without_bidi_controls() {
        let mut out = Vec::new();
        write_run(&u("a\u{200D}b\u{202A}c\u{2069}(\u{200F}"), false, &mut out);
        assert_eq!(out, u("abc("));
    }

    #[test]
    fn reverses_rtl_runs_by_character() {
        let mut out = Vec::new();
        write_run(&u("א\u{1E900}ב"), true, &mut out);
        assert_eq!(out, u("ב\u{1E900}א"));
    }

    #[test]
    fn breaks_lines_by_character_beyond_ascii() {
        // Line breaks and runs count characters, whatever their length in
        // UTF-8.
        assert_eq!(reorder_lines(&u("é😀ab\nש😀"), set(&[2])), vec![u("é😀"), u("ab\n"), u("😀ש")]);
        let runs = visual_runs(&u("a😀 שלום x"));
        assert_eq!(
            runs.iter().map(|run| (run.text.clone(), run.rtl)).collect::<Vec<_>>(),
            [(u("a😀 "), false), (u("שלום"), true), (u(" x"), false)]
        );
    }

    #[test]
    fn mirroring_table_is_sorted_and_symmetric() {
        assert!(BIDI_MIRRORING_GLYPH.windows(2).all(|w| w[0].0 < w[1].0));
        for &(from, to) in &BIDI_MIRRORING_GLYPH {
            let (from, to) = (char::from_u32(from.into()).unwrap(), char::from_u32(to.into()).unwrap());
            assert_eq!(mirror(to), from, "U+{:04X}", u32::from(from));
        }
    }
}