unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

bidi/explicit.mbt
1///|2/// X1-X10: Explicit Formatting Character Processing3///4/// This handles embeddings (LRE, RLE, LRO, RLO, PDF) and5/// isolates (LRI, RLI, FSI, PDI).67///|8/// Directional status stack entry9priv struct DirectionalStatus {10  level : Int3211  override_status : BidiClass // L, R, or ON (neutral = no override)12  isolate_status : Bool13}1415///|16/// Process explicit formatting characters (X1-X10)17/// Returns list of (isolate_initiator_index, pdi_index) pairs18fn process_explicit(19  types : Array[BidiClass],20  levels : Array[Int32],21  paragraph_level : Int32,22) -> Array[(Int32, Int32)] {23  // X1: Initialize directional status stack24  let stack : Array[DirectionalStatus] = [25    {26      level: paragraph_level,27      override_status: BidiClass::ON,28      isolate_status: false,29    },30  ]31  let mut overflow_isolate_count = 032  let mut overflow_embedding_count = 033  let mut valid_isolate_count = 03435  // Track isolate pairs for later run sequence computation36  let isolate_pairs : Array[(Int32, Int32)] = []37  let isolate_stack : Array[Int32] = [] // Stack of isolate initiator indices38  for i in 0..<types.length() {39    let bc = types[i]40    match bc {41      // X2: RLE - Right-to-Left Embedding42      RLE => {43        let new_level = next_odd_level(stack[stack.length() - 1].level)44        if new_level <= max_depth &&45          overflow_isolate_count == 0 &&46          overflow_embedding_count == 0 {47          stack.push({48            level: new_level,49            override_status: BidiClass::ON,50            isolate_status: false,51          })52        } else if overflow_isolate_count == 0 {53          overflow_embedding_count += 154        }55        // X9: Remove from output (set level, keep type for X9 removal)56        levels[i] = stack[stack.length() - 1].level57        types[i] = BidiClass::BN58      }5960      // X3: LRE - Left-to-Right Embedding61      LRE => {62        let new_level = next_even_level(stack[stack.length() - 1].level)63        if new_level <= max_depth &&64          overflow_isolate_count == 0 &&65          overflow_embedding_count == 0 {66          stack.push({67            level: new_level,68            override_status: BidiClass::ON,69            isolate_status: false,70          })71        } else if overflow_isolate_count == 0 {72          overflow_embedding_count += 173        }74        levels[i] = stack[stack.length() - 1].level75        types[i] = BidiClass::BN76      }7778      // X4: RLO - Right-to-Left Override79      RLO => {80        let new_level = next_odd_level(stack[stack.length() - 1].level)81        if new_level <= max_depth &&82          overflow_isolate_count == 0 &&83          overflow_embedding_count == 0 {84          stack.push({85            level: new_level,86            override_status: BidiClass::R,87            isolate_status: false,88          })89        } else if overflow_isolate_count == 0 {90          overflow_embedding_count += 191        }92        levels[i] = stack[stack.length() - 1].level93        types[i] = BidiClass::BN94      }9596      // X5: LRO - Left-to-Right Override97      LRO => {98        let new_level = next_even_level(stack[stack.length() - 1].level)99        if new_level <= max_depth &&100          overflow_isolate_count == 0 &&101          overflow_embedding_count == 0 {102          stack.push({103            level: new_level,104            override_status: BidiClass::L,105            isolate_status: false,106          })107        } else if overflow_isolate_count == 0 {108          overflow_embedding_count += 1109        }110        levels[i] = stack[stack.length() - 1].level111        types[i] = BidiClass::BN112      }113114      // X5a: RLI - Right-to-Left Isolate115      RLI => {116        levels[i] = stack[stack.length() - 1].level117        let ovr = stack[stack.length() - 1].override_status118        if !(ovr is ON) {119          types[i] = ovr120        }121        let new_level = next_odd_level(stack[stack.length() - 1].level)122        if new_level <= max_depth &&123          overflow_isolate_count == 0 &&124          overflow_embedding_count == 0 {125          valid_isolate_count += 1126          isolate_stack.push(i)127          stack.push({128            level: new_level,129            override_status: BidiClass::ON,130            isolate_status: true,131          })132        } else {133          overflow_isolate_count += 1134        }135      }136137      // X5b: LRI - Left-to-Right Isolate138      LRI => {139        levels[i] = stack[stack.length() - 1].level140        let ovr = stack[stack.length() - 1].override_status141        if !(ovr is ON) {142          types[i] = ovr143        }144        let new_level = next_even_level(stack[stack.length() - 1].level)145        if new_level <= max_depth &&146          overflow_isolate_count == 0 &&147          overflow_embedding_count == 0 {148          valid_isolate_count += 1149          isolate_stack.push(i)150          stack.push({151            level: new_level,152            override_status: BidiClass::ON,153            isolate_status: true,154          })155        } else {156          overflow_isolate_count += 1157        }158      }159160      // X5c: FSI - First Strong Isolate161      FSI => {162        levels[i] = stack[stack.length() - 1].level163        let ovr = stack[stack.length() - 1].override_status164        if !(ovr is ON) {165          types[i] = ovr166        }167        // Determine direction of isolate content168        let isolate_dir = determine_isolate_direction(types, i + 1)169        let new_level = if isolate_dir is RTL {170          next_odd_level(stack[stack.length() - 1].level)171        } else {172          next_even_level(stack[stack.length() - 1].level)173        }174        if new_level <= max_depth &&175          overflow_isolate_count == 0 &&176          overflow_embedding_count == 0 {177          valid_isolate_count += 1178          isolate_stack.push(i)179          stack.push({180            level: new_level,181            override_status: BidiClass::ON,182            isolate_status: true,183          })184        } else {185          overflow_isolate_count += 1186        }187      }188189      // X6a: PDI - Pop Directional Isolate190      PDI => {191        if overflow_isolate_count > 0 {192          overflow_isolate_count -= 1193        } else if valid_isolate_count > 0 {194          overflow_embedding_count = 0195          // Pop until isolate196          while stack.length() > 1 && !stack[stack.length() - 1].isolate_status {197            let _ = stack.pop()198          }199          // Pop the isolate entry itself200          if stack.length() > 1 {201            let _ = stack.pop()202          }203          valid_isolate_count -= 1204          // Record isolate pair205          if isolate_stack.pop() is Some(start) {206            isolate_pairs.push((start, i))207          }208        }209        levels[i] = stack[stack.length() - 1].level210        let ovr = stack[stack.length() - 1].override_status211        if !(ovr is ON) {212          types[i] = ovr213        }214      }215216      // X7: PDF - Pop Directional Formatting217      PDF => {218        if overflow_isolate_count > 0 {219          // Do nothing220        } else if overflow_embedding_count > 0 {221          overflow_embedding_count -= 1222        } else if stack.length() >= 2 &&223          !stack[stack.length() - 1].isolate_status {224          let _ = stack.pop()225        }226        levels[i] = stack[stack.length() - 1].level227        types[i] = BidiClass::BN228      }229230      // X6: For all other types231      _ => {232        levels[i] = stack[stack.length() - 1].level233        let ovr = stack[stack.length() - 1].override_status234        if !(ovr is ON) && !(bc is BN) {235          types[i] = ovr236        }237      }238    }239  }240  isolate_pairs241}242243///|244/// Ensure NSM levels follow the last non-X9 character.245fn adjust_nsm_levels(246  original_classes : Array[BidiClass],247  levels : Array[Int32],248) -> Unit {249  let mut last_idx = -1250  let mut saw_explicit = false251  for i, bc in original_classes {252    if bc is LRE ||253      bc is LRO ||254      bc is RLE ||255      bc is RLO ||256      bc is PDF ||257      bc.is_isolate_initiator() {258      saw_explicit = true259    }260    if bc is NSM && last_idx >= 0 {261      let last_bc = original_classes[last_idx]262      if !last_bc.is_isolate_initiator() && !(last_bc is PDI) && !saw_explicit {263        levels[i] = levels[last_idx]264      }265    }266    if !bc.is_x9_removed() {267      last_idx = i268      saw_explicit = false269    }270  }271}272273///|274/// Determine direction for FSI by finding first strong character275fn determine_isolate_direction(276  types : Array[BidiClass],277  start : Int32,278) -> Direction {279  let mut isolate_count = 0280  for i in start..<types.length() {281    let bc = types[i]282    if bc.is_isolate_initiator() {283      isolate_count += 1284    } else if bc is PDI {285      if isolate_count > 0 {286        isolate_count -= 1287      } else {288        // End of our isolate content289        break290      }291    } else if isolate_count == 0 {292      match bc {293        R | AL => return RTL294        L => return LTR295        _ => ()296      }297    }298  }299  LTR // Default300}301302///|303/// Get next odd level >= current304fn next_odd_level(level : Int32) -> Int32 {305  (level + 1) | 1306}307308///|309/// Get next even level >= current310fn next_even_level(level : Int32) -> Int32 {311  (level + 2) & (1).lnot()312}313314///|315/// Isolating run sequence - indices of characters that form a sequence316priv struct IsolatingRunSequence {317  indices : Array[Int32] // Character indices in this sequence318  sos : BidiClass // Start-of-sequence type (L or R)319  eos : BidiClass // End-of-sequence type (L or R)320  level : Int32 // Embedding level of the sequence321}322323///|324/// Compute isolating run sequences (X10)325fn compute_isolating_run_sequences(326  types : Array[BidiClass],327  levels : Array[Int32],328  original_classes : Array[BidiClass],329  paragraph_level : Int32,330  isolate_pairs : Array[(Int32, Int32)],331) -> Array[IsolatingRunSequence] {332  let len = types.length()333  if len == 0 {334    return []335  }336337  // Build a map from isolate initiator to PDI338  let pdi_of_isolate : Array[Int32] = Array::make(len, -1)339  for pair in isolate_pairs {340    pdi_of_isolate[pair.0] = pair.1341  }342  let sequences : Array[IsolatingRunSequence] = []343  let processed : Array[Bool] = Array::make(len, false)344345  // Find level runs and connect them via isolate pairs346  for i in 0..<len {347    if processed[i] {348      continue349    }350351    // Skip BN and explicit formatting characters352    let bc = types[i]353    if bc is BN {354      processed[i] = true355      continue356    }357358    // Start a new isolating run sequence359    let indices : Array[Int32] = []360    let seq_level = levels[i]361    let mut current = i362363    // Follow the run, linking through isolate pairs364    while current < len {365      // Add all characters at the same level in this run366      while current < len {367        if processed[current] {368          current += 1369          continue370        }371        if types[current] is BN {372          processed[current] = true373          current += 1374          continue375        }376        if levels[current] != seq_level {377          break378        }379        indices.push(current)380        processed[current] = true381        current += 1382      }383384      // Check if last added character is an isolate initiator385      if indices.length() > 0 {386        let last = indices[indices.length() - 1]387        let pdi_idx = pdi_of_isolate[last]388        if pdi_idx < 0 {389          break390        }391        // Continue sequence at the matching PDI run (include the PDI).392        current = pdi_idx393      } else {394        break395      }396    }397    if indices.length() == 0 {398      continue399    }400401    // Compute sos and eos402    let first_idx = indices[0]403    let last_idx = indices[indices.length() - 1]404405    // sos: higher of level before sequence and sequence level406    let level_before = if first_idx == 0 {407      paragraph_level408    } else {409      // Find previous character level (skip BN)410      let mut prev = first_idx - 1411      while prev > 0 && types[prev] is BN {412        prev -= 1413      }414      if prev < 0 || types[prev] is BN {415        paragraph_level416      } else if original_classes[prev] is B {417        paragraph_level418      } else {419        levels[prev]420      }421    }422    let sos_level = @cmp.maximum(level_before, seq_level)423    let sos : BidiClass = if sos_level % 2 == 0 {424      BidiClass::L425    } else {426      BidiClass::R427    }428429    // eos: higher of level after sequence and sequence level430    let level_after = if last_idx >= len - 1 {431      paragraph_level432    } else if original_classes[last_idx] is B {433      paragraph_level434    } else {435      // Find next character level (skip BN)436      let mut next = last_idx + 1437      while next < len && types[next] is BN {438        next += 1439      }440      // Check if this is an isolate initiator - look for matching PDI441      let last_type = types[last_idx]442      if last_type.is_isolate_initiator() {443        let pdi_idx = pdi_of_isolate[last_idx]444        if pdi_idx < 0 {445          paragraph_level446        } else {447          levels[pdi_idx]448        }449      } else if next >= len {450        paragraph_level451      } else if original_classes[next] is B {452        paragraph_level453      } else {454        levels[next]455      }456    }457    let eos_level = @cmp.maximum(level_after, seq_level)458    let eos : BidiClass = if eos_level % 2 == 0 {459      BidiClass::L460    } else {461      BidiClass::R462    }463    sequences.push({ indices, sos, eos, level: seq_level, })464  }465  sequences466}467468///|469/// The isolating run sequences of a paragraph without explicit formatting470/// characters: one sequence at the paragraph level holding every character471/// that X9 keeps, with sos and eos given by that level.472fn paragraph_run_sequence(473  types : Array[BidiClass],474  paragraph_level : Int32,475) -> Array[IsolatingRunSequence] {476  let indices : Array[Int32] = Array(capacity=types.length())477  for i, bc in types {478    if !(bc is BN) {479      indices.push(i)480    }481  }482  if indices.length() == 0 {483    return []484  }485  let direction : BidiClass = if paragraph_level % 2 == 0 {486    BidiClass::L487  } else {488    BidiClass::R489  }490  [{ indices, sos: direction, eos: direction, level: paragraph_level, }]491}492