bidi/paragraph.mbt1///|2/// P1-P3: Paragraph Level Determination3///4/// P1: Split the text into separate paragraphs.5/// P2: In each paragraph, find the first character of type L, AL, or R6/// while skipping over any characters between an isolate initiator7/// and its matching PDI or, if it has no matching PDI, the end of the8/// paragraph.9/// P3: If a character is found in P2 and it is of type AL or R, then set10/// the paragraph embedding level to one; otherwise, set it to zero.1112///|13/// Determine the paragraph embedding level for text14/// P2-P3: Find first strong character not in isolate, return its level15fn determine_paragraph_level(text : String, default_level~ : Int32) -> Int32 {16 let mut isolate_count = 017 for c in text {18 let bc = lookup_bidi_class(c)1920 // Skip characters inside isolates21 if bc.is_isolate_initiator() {22 isolate_count += 123 continue24 }25 if bc is PDI {26 if isolate_count > 0 {27 isolate_count -= 128 }29 continue30 }3132 // Only look at characters outside isolates33 if isolate_count == 0 {34 match bc {35 // P3: If AL or R, paragraph level is 1 (RTL)36 AL | R => return 137 // P3: If L, paragraph level is 0 (LTR)38 L => return 039 _ => ()40 }41 }42 }4344 // P3: If no strong character found, use default45 default_level46}47
///|
/// P1-P3: Paragraph Level Determination
///
/// P1: Split the text into separate paragraphs.
/// P2: In each paragraph, find the first character of type L, AL, or R
/// while skipping over any characters between an isolate initiator
/// and its matching PDI or, if it has no matching PDI, the end of the
/// paragraph.
/// P3: If a character is found in P2 and it is of type AL or R, then set
/// the paragraph embedding level to one; otherwise, set it to zero.
///|
/// Determine the paragraph embedding level for text
/// P2-P3: Find first strong character not in isolate, return its level
fn determine_paragraph_level(text : String, default_level~ : Int32) -> Int32 {
let mut isolate_count = 0
for c in text {
let bc = lookup_bidi_class(c)
// Skip characters inside isolates
if bc.is_isolate_initiator() {
isolate_count += 1
continue
}
if bc is PDI {
if isolate_count > 0 {
isolate_count -= 1
}
continue
}
// Only look at characters outside isolates
if isolate_count == 0 {
match bc {
// P3: If AL or R, paragraph level is 1 (RTL)
AL | R => return 1
// P3: If L, paragraph level is 0 (LTR)
L => return 0
_ => ()
}
}
}
// P3: If no strong character found, use default
default_level
}