This article needs more citations. (May 2026) |
| Unicode Bidirectional Algorithm | |
|---|---|
| Status | Active |
| Year started | 1999 |
| Latest version | Unicode 17.0.0 (Revision 51, 13 August 2025) |
| Organization | Unicode Consortium |
| Editors | Manish Goregaokar, Robin Leroy |
| Website | www |
The Unicode Bidirectional Algorithm (UBA), formally defined in Unicode Standard Annex #9 (UAX #9), is a specification developed by the Unicode Consortium that determines how text containing a mixture of left-to-right and right-to-left scripts is displayed. It is a normative part of the Unicode Standard and is required for conformance wherever characters from right-to-left scripts such as Arabic or Hebrew are rendered.
Most writing systems display text from left to right, but several scripts—including Arabic, Hebrew, Thaana, and Syriac—are written from right to left. When text from both directions appears in the same document, the result is known as bidirectional text (or bidi text). Without a clear specification, ambiguities arise in determining the correct display order of characters.
The Unicode Standard prescribes a logical order for storing characters in memory, regardless of their visual direction. The UBA translates this logical order into a correct visual display order.
The UBA defines several categories of special control characters used to influence text direction:
Lightweight, zero-width characters that act as directional anchors without affecting display:
| Abbreviation | Code Point | Name |
|---|---|---|
| LRM | U+200E | LEFT-TO-RIGHT MARK |
| RLM | U+200F | RIGHT-TO-LEFT MARK |
| ALM | U+061C | ARABIC LETTER MARK |
Signal that a piece of text is to be treated as embedded in a given direction:
| Abbreviation | Code Point | Name |
|---|---|---|
| LRE | U+202A | LEFT-TO-RIGHT EMBEDDING |
| RLE | U+202B | RIGHT-TO-LEFT EMBEDDING |
Force characters to be treated as strongly directional, overriding their implicit types:
| Abbreviation | Code Point | Name |
|---|---|---|
| LRO | U+202D | LEFT-TO-RIGHT OVERRIDE |
| RLO | U+202E | RIGHT-TO-LEFT OVERRIDE |
Introduced in Unicode 6.3, isolates prevent the enclosed text from affecting the surrounding text's ordering:
| Abbreviation | Code Point | Name |
|---|---|---|
| LRI | U+2066 | LEFT-TO-RIGHT ISOLATE |
| RLI | U+2067 | RIGHT-TO-LEFT ISOLATE |
| FSI | U+2068 | FIRST STRONG ISOLATE |
| PDI | U+2069 | POP DIRECTIONAL ISOLATE |
| Abbreviation | Code Point | Name | Terminates |
|---|---|---|---|
| U+202C | POP DIRECTIONAL FORMATTING | LRE, RLE, LRO, RLO | |
| PDI | U+2069 | POP DIRECTIONAL ISOLATE | LRI, RLI, FSI |
The UBA processes text in four main phases:
Text is split into paragraphs at paragraph separator characters (type B). Each paragraph is processed independently.
Each character is assigned a bidirectional character type (e.g., L, R, AL, EN, AN) from the Unicode Character Database. An embedding level list is also initialized.
A series of rules resolves the embedding level of each character:
The maximum embedding depth is 125 levels, a value guaranteed not to change in future versions of the standard.
Rules L1–L4 reorder characters on each line for display:
Bidi_Mirrored property when their resolved direction is right-to-left (e.g., "(" becomes ")").Characters are classified into the following categories:
| Category | Type | Description |
|---|---|---|
| Strong | L | Left-to-Right (e.g., Latin, Han) |
| R | Right-to-Left (e.g., Hebrew) | |
| AL | Right-to-Left Arabic (e.g., Arabic, Syriac) | |
| Weak | EN | European Number |
| ES | European Number Separator | |
| ET | European Number Terminator | |
| AN | Arabic Number | |
| CS | Common Number Separator | |
| NSM | Nonspacing Mark | |
| Neutral | B | Paragraph Separator |
| S | Segment Separator | |
| WS | Whitespace | |
| ON | Other Neutrals |
A conforming implementation must:
The UBA permits six higher-level protocol overrides (HL1–HL6), including:
dir attribute).Bidi_Mirrored property.On web pages, Unicode directional formatting characters can be replaced by HTML5 and CSS3 markup:
| Unicode | HTML | CSS |
|---|---|---|
| RLI...PDI | dir="rtl" | direction:rtl; unicode-bidi:isolate |
| LRI...PDI | dir="ltr" | direction:ltr; unicode-bidi:isolate |
| FSI...PDI | <bdi>, dir="auto" | unicode-bidi:plaintext |
The misuse of bidirectional formatting characters poses significant security risks, as they can be used to make malicious code or text appear benign. This is documented in Unicode Technical Report #36 (UTR36). Directional overrides (LRO, RLO) are particularly dangerous and should be avoided where possible.