Unicode bidirectional algorithm

Unicode Bidirectional Algorithm
StatusActive
Year started1999
Latest versionUnicode 17.0.0 (Revision 51, 13 August 2025)
OrganizationUnicode Consortium
EditorsManish Goregaokar, Robin Leroy
Websitewww.unicode.org/reports/tr9/

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.

Background

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.

Directional Formatting Characters

The UBA defines several categories of special control characters used to influence text direction:

Implicit Directional Marks

Lightweight, zero-width characters that act as directional anchors without affecting display:

AbbreviationCode PointName
LRMU+200ELEFT-TO-RIGHT MARK
RLMU+200FRIGHT-TO-LEFT MARK
ALMU+061CARABIC LETTER MARK

Explicit Directional Embeddings

Signal that a piece of text is to be treated as embedded in a given direction:

AbbreviationCode PointName
LREU+202ALEFT-TO-RIGHT EMBEDDING
RLEU+202BRIGHT-TO-LEFT EMBEDDING

Explicit Directional Overrides

Force characters to be treated as strongly directional, overriding their implicit types:

AbbreviationCode PointName
LROU+202DLEFT-TO-RIGHT OVERRIDE
RLOU+202ERIGHT-TO-LEFT OVERRIDE

Explicit Directional Isolates

Introduced in Unicode 6.3, isolates prevent the enclosed text from affecting the surrounding text's ordering:

AbbreviationCode PointName
LRIU+2066LEFT-TO-RIGHT ISOLATE
RLIU+2067RIGHT-TO-LEFT ISOLATE
FSIU+2068FIRST STRONG ISOLATE
PDIU+2069POP DIRECTIONAL ISOLATE

Terminating Characters

AbbreviationCode PointNameTerminates
PDFU+202CPOP DIRECTIONAL FORMATTINGLRE, RLE, LRO, RLO
PDIU+2069POP DIRECTIONAL ISOLATELRI, RLI, FSI

The Algorithm

The UBA processes text in four main phases:

1. Paragraph Separation

Text is split into paragraphs at paragraph separator characters (type B). Each paragraph is processed independently.

2. Initialization

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.

3. Resolving Embedding Levels

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.

4. Reordering

Rules L1–L4 reorder characters on each line for display:

Bidirectional Character Types

Characters are classified into the following categories:

CategoryTypeDescription
StrongLLeft-to-Right (e.g., Latin, Han)
RRight-to-Left (e.g., Hebrew)
ALRight-to-Left Arabic (e.g., Arabic, Syriac)
WeakENEuropean Number
ESEuropean Number Separator
ETEuropean Number Terminator
ANArabic Number
CSCommon Number Separator
NSMNonspacing Mark
NeutralBParagraph Separator
SSegment Separator
WSWhitespace
ONOther Neutrals

Conformance

A conforming implementation must:

Higher-Level Protocols

The UBA permits six higher-level protocol overrides (HL1–HL6), including:

HTML and CSS Equivalents

On web pages, Unicode directional formatting characters can be replaced by HTML5 and CSS3 markup:

UnicodeHTMLCSS
RLI...PDIdir="rtl"direction:rtl; unicode-bidi:isolate
LRI...PDIdir="ltr"direction:ltr; unicode-bidi:isolate
FSI...PDI<bdi>, dir="auto"unicode-bidi:plaintext

Security Considerations

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.

History

See also

References

  1. ↑ "Unicode Standard Annex #9: Unicode Bidirectional Algorithm". Unicode Consortium. Retrieved 2025-08-13.