bidi/reorder.mbt1///|2/// L1-L4: Reordering Rules3///4/// These rules reorder characters for display based on resolved levels.56///|7/// L1: Reset levels of segment/paragraph separators and whitespace8fn reset_whitespace_levels(9 original_classes : Array[BidiClass],10 levels : Array[Int32],11 paragraph_level : Int32,12) -> Unit {13 let len = levels.length()1415 // L1: Reset levels for:16 // - Segment separators (S)17 // - Paragraph separators (B)18 // - Any whitespace (WS) or isolate formatting characters preceding S or B19 // - Any whitespace or isolate formatting at the end of the line2021 // First, handle trailing whitespace22 // Note: X9-removed characters (LRE, RLE, LRO, RLO, PDF, BN) should be skipped23 // when detecting trailing whitespace, as they are conceptually removed by X924 let mut i = len - 125 while i >= 0 {26 let bc = original_classes[i]27 match bc {28 S | B => {29 levels[i] = paragraph_level30 // Reset preceding whitespace and isolates (skipping X9-removed chars)31 let mut j = i - 132 while j >= 0 {33 let prev_bc = original_classes[j]34 match prev_bc {35 WS | LRI | RLI | FSI | PDI => {36 levels[j] = paragraph_level37 j -= 138 }39 // Skip X9-removed characters40 LRE | RLE | LRO | RLO | PDF | BN => j -= 141 _ => break42 }43 }44 i = j45 }46 WS | LRI | RLI | FSI | PDI => {47 // Trailing whitespace at end of paragraph48 levels[i] = paragraph_level49 i -= 150 }51 // Skip X9-removed characters when detecting trailing whitespace52 LRE | RLE | LRO | RLO | PDF | BN => i -= 153 _ => break54 }55 }5657 // Also reset any S or B in the middle, plus preceding whitespace/isolates58 for k, bc in original_classes {59 if bc is S || bc is B {60 levels[k] = paragraph_level61 // L1.3: Reset preceding whitespace and isolate formatting characters62 // (skipping X9-removed characters)63 let mut j = k - 164 while j >= 0 {65 let prev_bc = original_classes[j]66 match prev_bc {67 WS | LRI | RLI | FSI | PDI => {68 levels[j] = paragraph_level69 j -= 170 }71 // Skip X9-removed characters72 LRE | RLE | LRO | RLO | PDF | BN => j -= 173 _ => break74 }75 }76 }77 }78}7980///|81/// L2: Reorder text based on levels82/// Returns indices in visual order83fn apply_reordering(84 indices : Array[Int32],85 levels : Array[Int32],86 paragraph_level : Int32,87) -> Array[Int32] {88 let len = indices.length()89 if len == 0 {90 return []91 }9293 // Initialize result with logical order after X9 removal.94 let result = indices.copy()9596 // Find the maximum level in the remaining indices.97 let mut max_level = paragraph_level98 for idx in result {99 let level = levels[idx]100 if level > max_level {101 max_level = level102 }103 }104105 // L2: From the highest level to the lowest odd level, reverse each106 // contiguous sequence of characters at that level or higher.107 let mut level = max_level108 while level >= 1 {109 let mut i = 0110 while i < len {111 if levels[result[i]] >= level {112 let start = i113 while i < len && levels[result[i]] >= level {114 i += 1115 }116 reverse_range(result, start, i - 1)117 } else {118 i += 1119 }120 }121 level -= 1122 }123 result124}125126///|127/// Reverse a range in an array128fn reverse_range(arr : Array[Int32], start : Int32, end : Int32) -> Unit {129 let mut lo = start130 let mut hi = end131 while lo < hi {132 let tmp = arr[lo]133 arr[lo] = arr[hi]134 arr[hi] = tmp135 lo += 1136 hi -= 1137 }138}139
///|
/// L1-L4: Reordering Rules
///
/// These rules reorder characters for display based on resolved levels.
///|
/// L1: Reset levels of segment/paragraph separators and whitespace
fn reset_whitespace_levels(
original_classes : Array[BidiClass],
levels : Array[Int32],
paragraph_level : Int32,
) -> Unit {
let len = levels.length()
// L1: Reset levels for:
// - Segment separators (S)
// - Paragraph separators (B)
// - Any whitespace (WS) or isolate formatting characters preceding S or B
// - Any whitespace or isolate formatting at the end of the line
// First, handle trailing whitespace
// Note: X9-removed characters (LRE, RLE, LRO, RLO, PDF, BN) should be skipped
// when detecting trailing whitespace, as they are conceptually removed by X9
let mut i = len - 1
while i >= 0 {
let bc = original_classes[i]
match bc {
S | B => {
levels[i] = paragraph_level
// Reset preceding whitespace and isolates (skipping X9-removed chars)
let mut j = i - 1
while j >= 0 {
let prev_bc = original_classes[j]
match prev_bc {
WS | LRI | RLI | FSI | PDI => {
levels[j] = paragraph_level
j -= 1
}
// Skip X9-removed characters
LRE | RLE | LRO | RLO | PDF | BN => j -= 1
_ => break
}
}
i = j
}
WS | LRI | RLI | FSI | PDI => {
// Trailing whitespace at end of paragraph
levels[i] = paragraph_level
i -= 1
}
// Skip X9-removed characters when detecting trailing whitespace
LRE | RLE | LRO | RLO | PDF | BN => i -= 1
_ => break
}
}
// Also reset any S or B in the middle, plus preceding whitespace/isolates
for k, bc in original_classes {
if bc is S || bc is B {
levels[k] = paragraph_level
// L1.3: Reset preceding whitespace and isolate formatting characters
// (skipping X9-removed characters)
let mut j = k - 1
while j >= 0 {
let prev_bc = original_classes[j]
match prev_bc {
WS | LRI | RLI | FSI | PDI => {
levels[j] = paragraph_level
j -= 1
}
// Skip X9-removed characters
LRE | RLE | LRO | RLO | PDF | BN => j -= 1
_ => break
}
}
}
}
}
///|
/// L2: Reorder text based on levels
/// Returns indices in visual order
fn apply_reordering(
indices : Array[Int32],
levels : Array[Int32],
paragraph_level : Int32,
) -> Array[Int32] {
let len = indices.length()
if len == 0 {
return []
}
// Initialize result with logical order after X9 removal.
let result = indices.copy()
// Find the maximum level in the remaining indices.
let mut max_level = paragraph_level
for idx in result {
let level = levels[idx]
if level > max_level {
max_level = level
}
}
// L2: From the highest level to the lowest odd level, reverse each
// contiguous sequence of characters at that level or higher.
let mut level = max_level
while level >= 1 {
let mut i = 0
while i < len {
if levels[result[i]] >= level {
let start = i
while i < len && levels[result[i]] >= level {
i += 1
}
reverse_range(result, start, i - 1)
} else {
i += 1
}
}
level -= 1
}
result
}
///|
/// Reverse a range in an array
fn reverse_range(arr : Array[Int32], start : Int32, end : Int32) -> Unit {
let mut lo = start
let mut hi = end
while lo < hi {
let tmp = arr[lo]
arr[lo] = arr[hi]
arr[hi] = tmp
lo += 1
hi -= 1
}
}