Bidi

object Bidi

The Unicode Bidirectional Algorithm (UAX #9), implicit subset: it resolves an embedding level per character and reorders a line from logical to visual order, so mixed Latin + Arabic/Hebrew + numbers display correctly. Lives in core so both the EPUB reflow engine and (later) PDF text extraction can share it.

Scope: a single paragraph with implicit directionality. Not handled (rare in book text, and each a large addition): explicit embedding/override/isolate controls (LRE/RLE/LRI/…), combining-mark reclassification (W1/NSM), and paired-bracket resolution (N0). Arabic/Hebrew combining marks are folded into their base script's strong class, which reorders acceptably.

Properties

Link copied to clipboard
const val AL: Int = 2
Link copied to clipboard
const val AN: Int = 6
Link copied to clipboard
const val B: Int = 8
Link copied to clipboard
const val CS: Int = 7
Link copied to clipboard
const val EN: Int = 3
Link copied to clipboard
const val ES: Int = 4
Link copied to clipboard
const val ET: Int = 5
Link copied to clipboard
const val L: Int = 0
Link copied to clipboard
const val ON: Int = 11
Link copied to clipboard
const val R: Int = 1
Link copied to clipboard
const val S: Int = 9
Link copied to clipboard
const val WS: Int = 10

Functions

Link copied to clipboard
fun baseLevel(cps: IntArray, explicit: Int? = null): Int

Base paragraph level: explicit 0/1 if forced, else the first strong char (UAX rule P2/P3).

Link copied to clipboard
fun classify(cp: Int): Int

Bidi class of a code point (range-based; covers Latin/Hebrew/Arabic/numbers/neutrals).

Link copied to clipboard

Reordering rule L2: given per-slot levels in logical order, return a permutation order where order[visualSlot] is the logical index to place there (left to right).

Link copied to clipboard
fun resolveLevels(cps: IntArray, paraLevel: Int): IntArray

Resolve an embedding level per code point for one paragraph at paraLevel.