Inferensys

Glossary

Bidirectional Text Rendering

The ability of a software application to correctly display and format text that mixes both left-to-right scripts (like English) and right-to-left scripts (like Arabic or Hebrew) within the same paragraph.
Developer working on RAG retrieval system, document chunks visible on screen, technical workspace with code editor.
INTERNATIONALIZATION ENGINE

What is Bidirectional Text Rendering?

Bidirectional text rendering is the software capability to correctly display and format text that mixes left-to-right (LTR) scripts, like English, with right-to-left (RTL) scripts, like Arabic or Hebrew, within the same paragraph or user interface element.

Bidirectional text rendering is the algorithmic process defined by the Unicode Bidirectional Algorithm (UBA) that resolves the display order of characters with mixed directionality. When a sentence embeds an English quote inside an Arabic paragraph, the rendering engine must logically determine the visual placement of punctuation, numbers, and neutral characters based on their surrounding directional context, not their storage order in memory.

This capability is a core requirement of internationalization (i18n) and relies on libraries like ICU4C and HarfBuzz to handle complex script shaping. Failure to implement correct bidirectional rendering results in garbled text, misplaced punctuation, and broken user interfaces, making the application unusable for RTL markets.

BIDIRECTIONAL TEXT RENDERING

Core Characteristics of BiDi Rendering

Bidirectional text rendering is a complex software engineering discipline that ensures the correct visual display and logical ordering of mixed-script text. It relies on the Unicode Bidirectional Algorithm to resolve the inherent conflict between left-to-right and right-to-left character directionality.

01

The Unicode Bidirectional Algorithm (UBA)

The foundational specification that defines how text containing both left-to-right (LTR) and right-to-left (RTL) scripts is ordered for display. The UBA assigns a directional type to every character and uses a set of explicit rules to resolve ambiguities.

  • Implicit Ordering: Characters are assigned levels (0 for LTR, 1 for RTL) based on their inherent direction.
  • Explicit Overrides: Control characters like LRE (Left-to-Right Embedding) and RLE (Right-to-Left Embedding) force a specific direction.
  • Mirroring: Characters like parentheses () are automatically mirrored to )( when rendered in an RTL context to maintain logical syntax.
02

Logical vs. Visual Ordering

A critical distinction in BiDi rendering is the separation of storage order from display order. Text is always stored in logical order—the sequence in which characters are typed and processed by the system.

  • Logical Order: The underlying string in memory, e.g., Hello שלום.
  • Visual Order: The rendered output on screen, where the RTL segment is displayed right-to-left.
  • Caret Navigation: A key challenge is ensuring the text cursor moves in the logical order, not the visual order, to prevent disjointed editing experiences.
03

Directional Isolates and Embedding

Modern BiDi rendering uses directional isolates to cleanly separate text segments with different base directions, preventing the spill-over effect that plagued older embedding methods.

  • RLI/LRI/FSI: Right-to-Left Isolate, Left-to-Right Isolate, and First-Strong Isolate characters create a new directional context that is completely independent of the surrounding text.
  • LRE/RLE (Legacy): Older embedding characters that could cause the direction of one segment to influence adjacent punctuation, leading to the 'leaky' BiDi bug.
  • PDF (Pop Directional Format): A control character that terminates the effect of the last explicit embedding or override.
04

Handling Neutral Characters

Characters like spaces, punctuation, and numbers are directionally neutral. Their rendering direction is determined by the surrounding strongly-typed characters, making them a primary source of rendering bugs.

  • Weak Characters: Numbers and currency symbols have a weak directionality and can be influenced by adjacent text.
  • Neutral Characters: Spaces and most punctuation adopt the direction of the surrounding paragraph or embedding level.
  • Common Bug: A period at the end of a mixed-script sentence may render on the wrong side of the text if the surrounding directional context is not correctly resolved.
05

Paragraph-Level Direction

The base direction of a block of text is set at the paragraph level, providing the foundational context for the entire BiDi algorithm. This can be set explicitly or detected heuristically.

  • First-Strong Heuristic: The algorithm scans for the first character with a strong direction (e.g., an Arabic or Latin letter) and sets the paragraph's base direction accordingly.
  • Explicit Declaration: In HTML, the dir attribute (dir='rtl') or the CSS direction property overrides the heuristic and sets the base direction definitively.
  • Alignment: The base direction also controls the default text alignment—right-aligned for RTL paragraphs, left-aligned for LTR.
06

Mirroring and Glyph Substitution

Beyond character ordering, BiDi rendering requires glyph mirroring to ensure that the visual representation of a character matches its logical function in an RTL context.

  • Paired Punctuation: Characters like (, [, {, and « are replaced with their mirrored counterparts ), ], }, and » when resolved to an RTL level.
  • Glyph Shaping: For cursive scripts like Arabic, characters must also undergo contextual shaping, where a letter's form changes based on its position (isolated, initial, medial, final) within a word.
  • Combined Process: Mirroring and shaping happen after the BiDi levels are resolved, as part of the final layout and rendering pipeline.
BIDIRECTIONAL TEXT RENDERING

Frequently Asked Questions

Clear, technical answers to the most common questions about implementing and debugging bidirectional text rendering in modern software applications.

Bidirectional (bidi) text rendering is the process by which a software application correctly displays and formats text that mixes both left-to-right (LTR) scripts, such as English or Latin, and right-to-left (RTL) scripts, such as Arabic, Hebrew, or Persian, within the same paragraph. The core mechanism relies on the Unicode Bidirectional Algorithm (UBA) , defined in Unicode Standard Annex #9. The UBA assigns a directional type to every character—strong LTR, strong RTL, or neutral—and then resolves the embedding levels of each character based on an implicit set of rules. This determines the visual ordering of characters on the screen, which often differs from their logical storage order in memory. For example, in a sentence mixing English and Arabic, the logical first character might not be the visual first character. The algorithm handles nested embeddings, directional overrides, and the mirroring of glyphs like parentheses to ensure the text is human-readable.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.