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
use alloc::vec::Vec;
use super::char_data::{BidiClass::{self, *}, is_rtl};
use super::level::Level;
#[cfg_attr(feature = "flame_it", flamer::flame)]
pub fn compute(
text: &str,
para_level: Level,
original_classes: &[BidiClass],
levels: &mut [Level],
processing_classes: &mut [BidiClass],
) {
assert_eq!(text.len(), original_classes.len());
let mut stack = DirectionalStatusStack::new();
stack.push(para_level, OverrideStatus::Neutral);
let mut overflow_isolate_count = 0u32;
let mut overflow_embedding_count = 0u32;
let mut valid_isolate_count = 0u32;
for (i, c) in text.char_indices() {
match original_classes[i] {
RLE | LRE | RLO | LRO | RLI | LRI | FSI => {
let last_level = stack.last().level;
let is_isolate = match original_classes[i] {
RLI | LRI | FSI => true,
_ => false,
};
if is_isolate {
levels[i] = last_level;
match stack.last().status {
OverrideStatus::RTL => processing_classes[i] = R,
OverrideStatus::LTR => processing_classes[i] = L,
_ => {}
}
}
let new_level = if is_rtl(original_classes[i]) {
last_level.new_explicit_next_rtl()
} else {
last_level.new_explicit_next_ltr()
};
if new_level.is_ok() && overflow_isolate_count == 0 &&
overflow_embedding_count == 0
{
let new_level = new_level.unwrap();
stack.push(
new_level,
match original_classes[i] {
RLO => OverrideStatus::RTL,
LRO => OverrideStatus::LTR,
RLI | LRI | FSI => OverrideStatus::Isolate,
_ => OverrideStatus::Neutral,
},
);
if is_isolate {
valid_isolate_count += 1;
} else {
levels[i] = new_level;
}
} else if is_isolate {
overflow_isolate_count += 1;
} else if overflow_isolate_count == 0 {
overflow_embedding_count += 1;
}
}
PDI => {
if overflow_isolate_count > 0 {
overflow_isolate_count -= 1;
} else if valid_isolate_count > 0 {
overflow_embedding_count = 0;
loop {
match stack.vec.pop() {
None |
Some(Status { status: OverrideStatus::Isolate, .. }) => break,
_ => continue,
}
}
valid_isolate_count -= 1;
}
let last = stack.last();
levels[i] = last.level;
match last.status {
OverrideStatus::RTL => processing_classes[i] = R,
OverrideStatus::LTR => processing_classes[i] = L,
_ => {}
}
}
PDF => {
if overflow_isolate_count > 0 {
continue;
}
if overflow_embedding_count > 0 {
overflow_embedding_count -= 1;
continue;
}
if stack.last().status != OverrideStatus::Isolate && stack.vec.len() >= 2 {
stack.vec.pop();
}
levels[i] = stack.last().level;
}
B | BN => {}
_ => {
let last = stack.last();
levels[i] = last.level;
match last.status {
OverrideStatus::RTL => processing_classes[i] = R,
OverrideStatus::LTR => processing_classes[i] = L,
_ => {}
}
}
}
for j in 1..c.len_utf8() {
levels[i + j] = levels[i];
processing_classes[i + j] = processing_classes[i];
}
}
}
struct Status {
level: Level,
status: OverrideStatus,
}
#[derive(PartialEq)]
enum OverrideStatus {
Neutral,
RTL,
LTR,
Isolate,
}
struct DirectionalStatusStack {
vec: Vec<Status>,
}
impl DirectionalStatusStack {
fn new() -> Self {
DirectionalStatusStack { vec: Vec::with_capacity(Level::max_explicit_depth() as usize + 2) }
}
fn push(&mut self, level: Level, status: OverrideStatus) {
self.vec.push(Status { level, status });
}
fn last(&self) -> &Status {
self.vec.last().unwrap()
}
}