maplibre/legacy/
bidi.rs

1//! Translated from the QT BIDI implementation https://github.com/maplibre/maplibre-native/blob/4add9ea/src/mbgl/bidi.cpp
2
3use std::collections::BTreeSet;
4
5use widestring::U16String;
6
7/// maplibre/maplibre-native#4add9ea original name: Char16
8pub type Char16 = u16; // was char16_t
9
10/// maplibre/maplibre-native#4add9ea original name: applyArabicShaping
11pub fn apply_arabic_shaping(str: &U16String) -> U16String {
12    // TODO: Add real implementation
13    str.clone()
14}
15
16// StyledText pairs each code point in a string with an integer indicating
17// the styling options to use for rendering that code point
18// The data structure is intended to accomodate the reordering/interleaving
19// of formatting that can happen when BiDi rearranges inputs
20/// maplibre/maplibre-native#4add9ea original name: StyledText
21pub type StyledText = (U16String, Vec<u8>);
22
23/// maplibre/maplibre-native#4add9ea original name: BiDi
24pub struct BiDi;
25
26impl BiDi {
27    // TODO: This implementation is from the QT backend and lacks ICU support
28    /// Given text in logical ordering and a set of line break points,
29    /// return a set of lines in visual order with bidi and line breaking applied
30    /// maplibre/maplibre-native#4add9ea original name: processText
31    pub fn process_text(
32        &self,
33        input: &U16String,
34        mut line_break_points: BTreeSet<usize>, // TODO: Make sure this is no output
35    ) -> Vec<U16String> {
36        line_break_points.insert(input.len());
37
38        let mut transformed_lines = Vec::new();
39        let mut start = 0;
40        for line_break_point in line_break_points {
41            transformed_lines.push(U16String::from(&input[start..line_break_point])); // TODO verify if this is correct
42            start = line_break_point;
43        }
44
45        transformed_lines
46    }
47
48    /// Same as processText but preserves per-code-point formatting information
49    /// maplibre/maplibre-native#4add9ea original name: processStyledText
50    pub fn process_styled_text(
51        &self,
52        input: &StyledText,
53        mut line_break_points: BTreeSet<usize>, // TODO: Make sure this is no output
54    ) -> Vec<StyledText> {
55        line_break_points.insert(input.0.len());
56
57        let mut transformed_lines = Vec::new();
58        let mut start = 0;
59        for line_break_point in line_break_points {
60            if line_break_point <= input.1.len() {
61                transformed_lines.push((
62                    U16String::from(&input.0[start..line_break_point]),
63                    Vec::from(&input.1[start..line_break_point]),
64                )); // TODO verify if this is correct
65                start = line_break_point;
66            }
67        }
68
69        transformed_lines
70    }
71}