Bidi
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.
Functions
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).
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.