Documentation
¶
Overview ¶
Package bidi is a pure-Go, CGO=0, standard-library-only implementation of the Unicode Bidirectional Algorithm (UAX #9) for laying out mixed left-to-right / right-to-left text.
It has no dependency on golang.org/x/text: the Bidi_Class property and the paired-bracket property are shipped as generated lookup tables (see cmd/genbidi), so the package builds anywhere the standard library does.
Public API ¶
- ClassOf reports the Bidi_Class of a rune, and the Class enum enumerates every Bidi_Class value.
- ResolveLevels runs the algorithm over a paragraph and returns the resolved embedding Level of each rune.
- BaseLevel reports the paragraph embedding level chosen by rules P2/P3.
- Reorder applies rule L2 to a level slice, returning the visual-order index permutation.
- VisualOrder is a convenience that resolves and reorders a string in one call.
- Direction selects the base direction: LeftToRight, RightToLeft or Auto.
Implemented rules ¶
The algorithm is implemented through rule L2, which is the full extent verified by the Unicode conformance file BidiCharacterTest.txt:
- P2, P3: paragraph embedding level from the first strong character.
- X1–X8: explicit embeddings (LRE/RLE/LRO/RLO/PDF) and isolates (LRI/RLI/FSI/PDI), including overflow handling and the directional status stack, with FSI resolved per X5c.
- X9: the deprecated embedding/override/PDF formatting characters and BN are removed (reported as carrying the preceding character's level).
- X10: isolating run sequences with their sos/eos boundary types.
- W1–W7: the weak-type rules.
- N0: paired-bracket resolution (BD16), including the U+2329/U+232A ~ U+3008/U+3009 canonical equivalence.
- N1, N2: the neutral-type rules.
- I1, I2: the implicit level rules.
- L1: resetting separators and trailing whitespace to the paragraph level.
- L2: reordering to visual order.
The package is validated against the full BidiCharacterTest.txt (all cases pass); a curated subset is embedded under testdata for the committed test suite.
Deferred ¶
- L3 (combining marks) and L4 (mirroring of paired-bracket and other mirrored glyphs) are out of scope: they belong to the rendering/shaping stage. VisualOrder therefore does not substitute mirrored glyphs.
- Arabic cursive shaping and joining are the job of a shaper, not the bidirectional algorithm, and are out of scope here.
- Rule P1 (splitting text into paragraphs on Paragraph_Separator) is the caller's responsibility; the API operates on a single paragraph, though a Paragraph_Separator encountered inline is handled by rule X8/L1.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Reorder ¶
Reorder implements rule L2. Given the per-rune levels (as returned by ResolveLevels for the same text), it returns the visual left-to-right order as a permutation of indices into text.
func VisualOrder ¶
VisualOrder is a convenience that resolves levels for text and returns the characters in visual (left-to-right) order. Characters removed by rule X9 are dropped. Glyph mirroring (rule L4) is not applied; see the package documentation.
Types ¶
type Class ¶
type Class uint8
Class is a Unicode Bidi_Class property value, as defined by UAX #9 and UAX #44. Every rune has exactly one Bidi_Class.
const ( // Strong types. L Class = iota // Left_To_Right R // Right_To_Left AL // Arabic_Letter // Weak types. EN // European_Number ES // European_Separator ET // European_Terminator AN // Arabic_Number CS // Common_Separator NSM // Nonspacing_Mark BN // Boundary_Neutral // Neutral types. B // Paragraph_Separator S // Segment_Separator WS // White_Space ON // Other_Neutral // Explicit formatting types. LRE // Left_To_Right_Embedding RLE // Right_To_Left_Embedding LRO // Left_To_Right_Override RLO // Right_To_Left_Override PDF // Pop_Directional_Format LRI // Left_To_Right_Isolate RLI // Right_To_Left_Isolate FSI // First_Strong_Isolate PDI // Pop_Directional_Isolate )
The Bidi_Class values. The ordering matches the enumeration used throughout the Unicode Bidirectional Algorithm; do not rely on the numeric values other than for equality.
func ClassOf ¶
ClassOf returns the Bidi_Class of r. Runes outside the assigned ranges follow the Unicode defaults declared by the @missing lines of DerivedBidiClass.txt (L in general, R/AL in unassigned right-to-left blocks, ET in the currency block, BN for default-ignorable and non-characters).
It is named ClassOf rather than Class because Go does not allow a function to share the name of the Class type it returns.
type Direction ¶
type Direction int
Direction selects the base paragraph direction passed to the algorithm.
type Level ¶
type Level int
Level is a bidirectional embedding level as defined by UAX #9. Even levels are left-to-right; odd levels are right-to-left.
func BaseLevel ¶
BaseLevel returns the resolved paragraph embedding level chosen for text under base (rules P2/P3): 0 for left-to-right, 1 for right-to-left.
func ResolveLevels ¶
ResolveLevels runs the Unicode Bidirectional Algorithm over text and returns the resolved embedding level of every rune. Even levels are left-to-right, odd levels right-to-left. Characters removed by rule X9 (the deprecated embedding/override formatting characters and BN) carry the level of the preceding retained character.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
genbidi
command
Command genbidi fetches the Unicode Character Database DerivedBidiClass.txt and BidiBrackets.txt files and emits the compact Go lookup tables used by package bidi: bidiclass_table.go (a sorted Bidi_Class range table) and bidibrackets_table.go (the paired-bracket table for rule N0).
|
Command genbidi fetches the Unicode Character Database DerivedBidiClass.txt and BidiBrackets.txt files and emits the compact Go lookup tables used by package bidi: bidiclass_table.go (a sorted Bidi_Class range table) and bidibrackets_table.go (the paired-bracket table for rule N0). |