PeachDrawing.Text

PeachDrawing.Text.Unicode

Bidi Class

The Unicode Bidirectional Algorithm (UAX #9): which direction each character of a paragraph reads in, and in what order the pieces of a line are drawn.

public static class Bidi

Inheritance System.Object → Bidi

Remarks

Work happens in two steps because the algorithm does. Analyze(string, BaseDirection, IReadOnlyList<EmbeddingSpan>) looks at a whole paragraph and resolves an embedding level for every character (rules P2 to I2, and L1 up to the line’s trailing whitespace). Only once a host has decided where the paragraph’s lines break can it call ReorderLine(byte[], int, int) for each one (rule L2), so that step takes levels and a range rather than text.

The implementation is checked against Unicode’s own BidiCharacterTest.txt conformance file. Rule L1’s last clause, which resets the trailing whitespace of each line, is left to the caller because it depends on the granularity (characters, words, glyphs) the caller lays out in.

Methods  
Analyze(string, BaseDirection, IReadOnlyList<EmbeddingSpan>) Resolves the embedding levels of one paragraph.
ClassOf(Rune) The Bidi_Class of a character.
Mirror(string, byte) Turns the text of one run into the text to draw left to right: for an odd level, the characters are reversed (rule L2) and every mirrorable one is replaced by its mirror image (rule L4).
ReorderLine(byte[], int, int) Puts the pieces of one line into the order they are drawn in, left to right (rule L2).
Reverse(string) Reverses a string by character, keeping surrogate pairs whole, and substitutes nothing.
TryGetMirror(Rune, Rune) Finds the character that draws as the mirror image of rune (a parenthesis and its counterpart, for instance), for right-to-left runs.