unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

bidi/neutral.mbt
1///|2/// N1-N2: Neutral Type Resolution3///4/// These rules resolve neutral types (B, S, WS, ON, FSI, LRI, RLI, PDI)5/// after bracket pair resolution.67///|8/// Resolve neutral types within an isolating run sequence9fn resolve_neutral_types(10  types : Array[BidiClass],11  _levels : Array[Int32],12  seq : IsolatingRunSequence,13) -> Unit {14  let indices = seq.indices15  if indices.length() == 0 {16    return17  }1819  // Find runs of neutrals and resolve them20  let mut i = 021  while i < indices.length() {22    let idx = indices[i]23    let bc = types[idx]24    if bc.is_neutral_or_isolate() {25      // Found start of a neutral run26      let run_start = i2728      // Find end of neutral run29      let mut run_end = i30      while run_end + 1 < indices.length() {31        let next_bc = types[indices[run_end + 1]]32        if next_bc.is_neutral_or_isolate() {33          run_end += 134        } else {35          break36        }37      }3839      // Get preceding strong type40      let preceding = get_preceding_strong_type(41        types,42        indices,43        run_start,44        seq.sos,45      )4647      // Get following strong type48      let following = get_following_strong_type(49        types,50        indices,51        run_end,52        seq.eos,53      )5455      // N1: If preceding and following are same type, neutrals become that type56      // N2: Otherwise, neutrals become embedding direction57      let resolved : BidiClass = match preceding {58        L if following is L => BidiClass::L59        R if following is R => BidiClass::R60        _ => if seq.level % 2 == 0 { BidiClass::L } else { BidiClass::R }61      }6263      // Apply resolved type to all neutrals in the run64      for j in run_start..<=run_end {65        types[indices[j]] = resolved66      }67      i = run_end + 168    } else {69      i += 170    }71  }72}7374///|75/// Get the preceding strong type in the sequence76/// Returns effective strong type (L, R) - EN and AN become R77fn get_preceding_strong_type(78  types : Array[BidiClass],79  indices : Array[Int32],80  before_pos : Int32,81  sos : BidiClass,82) -> BidiClass {83  let mut i = before_pos - 184  while i >= 0 {85    let bc = types[indices[i]]86    match bc {87      L => return BidiClass::L88      R => return BidiClass::R89      EN | AN => return BidiClass::R // EN and AN are treated as R90      _ => i -= 191    }92  }93  sos94}9596///|97/// Get the following strong type in the sequence98/// Returns effective strong type (L, R) - EN and AN become R99fn get_following_strong_type(100  types : Array[BidiClass],101  indices : Array[Int32],102  after_pos : Int32,103  eos : BidiClass,104) -> BidiClass {105  let mut i = after_pos + 1106  while i < indices.length() {107    let bc = types[indices[i]]108    match bc {109      L => return BidiClass::L110      R => return BidiClass::R111      EN | AN => return BidiClass::R // EN and AN are treated as R112      _ => i += 1113    }114  }115  eos116}117