lulupedia
буряад 版本暂未收录,当前展示 English 内容。

Bidirectional text

5760 words·24.09.2026·English
0

Bidirectional text (often abbreviated as bidi text) is text that contains both left-to-right (LTR) and right-to-left (RTL) writing systems, such as when a passage mixes Arabic or Hebrew with English or other LTR scripts.

Overview

Writing systems can be classified by their inherent directionality. Most scripts, including Latin, Cyrillic, and Greek, are written from left to right (LTR). Others, such as Arabic, Hebrew, Syriac, and Thaana, are written from right to left (RTL). Bidirectional text arises when segments of different directionality appear within the same line or paragraph—for instance, an Arabic sentence containing an English phrase, or a Hebrew document quoting a Latin-derived term. Proper handling of bidirectional text requires algorithms to determine the correct visual order of characters and to manage interactions between runs of opposite directions.

Directionality and Character Categories

The Unicode Standard assigns each character an implicit bidirectional category, or bidi class. Major categories include:

  • Strong LTR: Characters that always appear left-to-right, such as most Latin, Cyrillic, and Greek letters.
  • Strong RTL: Characters that always appear right-to-left, such as Arabic, Hebrew, and Syriac letters.
  • Weak: Characters whose display direction depends on surrounding strong characters, such as digits, mathematical symbols, and punctuation.
  • Neutral: Characters that take the direction of the adjacent strong text, for example spaces, most parentheses, and some punctuation.
  • Explicit formatting characters: Control characters like the Left-to-Right Mark (LRM), Right-to-Left Mark (RLM), Left-to-Right Embedding (LRE), and Right-to-Left Embedding (RLE) that override the default direction.

Unicode Bidirectional Algorithm

The Unicode Bidirectional Algorithm (UAX #9) is the standard for rendering bidirectional text. It specifies a multi-stage process to convert a sequence of character codes (logical order) into a visually ordered representation (display order). Key steps include:

  1. Resolving implicit direction – Assigning an initial directional type to each character based on its bidi class.
  2. Resolving explicit overrides – Processing explicit formatting characters (e.g., LRE, RLE, LRO, RLO, PDF) to set directional embeddings or overrides.
  3. Resolving weak and neutral types – Resolving the direction of weak and neutral characters based on surrounding strong characters, using rules that prevent spurious changes.
  4. Resolving implicit levels – Assigning each character a numeric level (0 for LTR, 1 for RTL, 2 for LTR nesting, etc.), ensuring proper nesting.
  5. Reordering – Visually reordering the characters from their logical order to the correct display order, typically using a “base direction” (paragraph direction) that can be LTR or RTL.

The algorithm ensures that numbers, punctuation, and embedded runs of the opposite direction are displayed in a human-readable manner.

Implementation Challenges

Practical implementation of bidirectional text presents several complexities:

  • Mirroring: Some characters (e.g., parentheses, brackets, angle brackets) have mirrored glyphs in RTL contexts. The algorithm must substitute the appropriate mirrored form (e.g., '(' becomes ')' in RTL runs).
  • Line breaking and alignment: Breaking a line containing bidi text must preserve the relative order of runs. Justification may require special handling to avoid breaking inside a bidirectional sequence.
  • Cursor placement and selection: Text selection and insertion point movement must correctly map between visual and logical positions. In a bidirectional string, a single cursor position may correspond to two logical insertion points (a “visual cursor”).
  • Keyboard input: Typing requires real-time reordering and display of characters as they are entered, with the correct direction for each new character.
  • Data storage: Text is typically stored in logical order (the order in which characters are typed or written) rather than visual order, which simplifies processing but necessitates the algorithm for display.

Usage in Software

Bidirectional text support is essential in many domains:

  • Operating systems: Windows, macOS, and Linux provide system-level bidi support via libraries like Uniscribe, Core Text, and Pango, respectively.
  • Web browsers: The World Wide Web Consortium (W3C) specifies bidi behavior in HTML and CSS (e.g., the dir attribute and unicode-bidi property). JavaScript also includes methods for bidi handling.
  • Text editors and word processors: Programs like Microsoft Word, Google Docs, and LibreOffice implement the Unicode Bidirectional Algorithm for editing mixed-script documents.
  • Electronic design automation (EDA) and other technical fields: Tooltips, menus, and UI strings in multilingual environments must correctly handle bidi text.
  • Programming languages: Many languages (e.g., Java, Python, C#) provide string classes that treat directional characters transparently, leaving rendering to the UI framework.

Limitations and Extensions

While the Unicode Bidirectional Algorithm handles most common scenarios, edge cases remain:

  • Nested embeddings: Deeply nested embeddings can become complex; implementations typically limit nesting depth (e.g., to 125 levels in Unicode).
  • Bi-directional parentheses: Mixing parentheses from different scripts can cause ambiguities, especially when paired with whitespace.
  • Line-initial or line-final punctuation: Neutral punctuation at line breaks may be reordered unexpectedly, requiring careful application of rules.
  • Arabic script ligatures and shaping: Arabic requires additional shaping (cursive joining) that interacts with bidi reordering; the algorithm does not handle shaping, which must be applied separately.

Related Standards and Terminology

  • Logical order: The sequence in which characters are typed or stored.
  • Visual order: The arrangement of characters as they appear on screen.
  • Base direction: The primary direction of a paragraph (LTR or RTL), often derived from the first strong character or explicitly set.
  • Embedding: A nested directional run created by explicit formatting characters or markup.
  • Override: An explicit override that forces all characters in a range to have a specific direction, ignoring their implicit types.

Bidirectional text is a fundamental aspect of globalized computing, enabling coexistence of diverse writing systems in a single document or interface. The Unicode Bidirectional Algorithm remains the cornerstone of bidi support across platforms and applications.

Comments (0)

U

No comments yet. Be the first to comment!

You May Be Interested In

Related Articles