From 431d8812feafa753d3cacc0a25e14a531576a3dd Mon Sep 17 00:00:00 2001 From: isgav <51133755+isaranga@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:09:14 +0200 Subject: [PATCH 1/2] Check STRONG_TYPES once in the FSI scan, then pick the direction Same simplification as the P2 scan in determine_paragraph_embedding_level: _resolve_fsi_direction now tests STRONG_TYPES once and returns LRI for L, RLI for R or AL, in place of two separate branches. Co-Authored-By: Claude --- bidi.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bidi.py b/bidi.py index 34487bb..89d248d 100644 --- a/bidi.py +++ b/bidi.py @@ -188,10 +188,11 @@ def _resolve_fsi_direction(chars: list[CharacterInfo], fsi_index: int) -> str: if depth == 0: break elif depth == 1: - if t in ('R', 'AL'): - return 'RLI' - elif t == 'L': - return 'LRI' + if t in STRONG_TYPES: + if t == 'L': + return 'LRI' + else: # R or AL + return 'RLI' return 'LRI' # default From d77fba77c6ef808fabf529bf76bd5e110e193335 Mon Sep 17 00:00:00 2001 From: isgav <51133755+isaranga@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:17:19 +0200 Subject: [PATCH 2/2] Read original_type in the P2 scan, as the FSI scan does P2 looks at each character's Bidi_Class from the Unicode data. The scan in determine_paragraph_embedding_level read character_type, which only holds that value because no rule has run yet. It now reads original_type, like the same P2 scan in _resolve_fsi_direction (X5c), so it no longer depends on when it is called. Results are unchanged. Co-Authored-By: Claude --- bidi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bidi.py b/bidi.py index 89d248d..6b41bfa 100644 --- a/bidi.py +++ b/bidi.py @@ -120,7 +120,7 @@ def determine_paragraph_embedding_level(chars: list[CharacterInfo], verbose: boo first_strong_char = None # the character found by P2, if any for ci in chars: - char_type = ci.character_type + char_type = ci.original_type if char_type in ISOLATE_INITIATOR_TYPES: open_isolate_count += 1 elif char_type == 'PDI': @@ -145,7 +145,7 @@ def determine_paragraph_embedding_level(chars: list[CharacterInfo], verbose: boo print(f"--- Rule P2 --- first strong character outside isolates: " f"index {first_strong_char.index} " f"(U+{ord(first_strong_char.character):04X} {shown_char}, " - f"type {first_strong_char.character_type}) " + f"type {first_strong_char.original_type}) " f"-> level {paragraph_level}") else: print(f"--- Rule P3 --- no strong character outside isolates "