Bidi

object Bidi(source)

The Unicode Bidirectional Algorithm (UAX #9) of Unicode 17 (#320, #323): it resolves an embedding level for each character of a paragraph and reorders a line from logical to visual order, so that mixed Latin, Arabic, Hebrew and numbers display in the right order. It lives in core so that the EPUB layout and PDF text extraction can share it.

  • classify reads the Bidi_Class of a code point.

  • baseLevel finds the level of a paragraph (rules P2 and P3).

  • resolveLevels applies the explicit rules X1 to X10, the weak rules W1 to W7, the paired brackets of N0, the neutral rules N1 and N2, the implicit rules I1 and I2, and rule L1 to each line.

  • reorderVisually applies rule L2 to one line.

Every test of BidiCharacterTest.txt and BidiTest.txt of Unicode 17 passes.

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 BN: Int = 13
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 FSI: Int = 21
Link copied to clipboard
const val L: Int = 0
Link copied to clipboard
const val LRE: Int = 14
Link copied to clipboard
const val LRI: Int = 19
Link copied to clipboard
const val LRO: Int = 15
Link copied to clipboard
const val NSM: Int = 12
Link copied to clipboard
const val ON: Int = 11
Link copied to clipboard
const val PDF: Int = 18
Link copied to clipboard
const val PDI: Int = 22
Link copied to clipboard
const val R: Int = 1
Link copied to clipboard
const val RLE: Int = 16
Link copied to clipboard
const val RLI: Int = 20
Link copied to clipboard
const val RLO: Int = 17
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 character outside an isolate (P2, P3).

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

The Bidi_Class of cp, from DerivedBidiClass.txt of Unicode 17 with its defaults for unassigned code points.

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

Rule L1 for one line whose code points cps have the resolved levels: a segment or paragraph separator, the whitespace before it and the whitespace at the end of the line take paraLevel. Returns the new levels.

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

Resolves an embedding level for each code point of one paragraph at paraLevel, with rule L1 for the paragraph as one line. A character that rule X9 removes, such as ZWJ, takes the level of the character before it. When the paragraph breaks into lines, lineLevels applies L1 to each of them.