How LibreOffice Renders Arabic, Devanagari, and Thai
LibreOffice renders complex scripts such as Devanagari, Arabic, and Thai through a dedicated Complex Text Layout (CTL) subsystem powered by open-source text-shaping engines and typography standards. This article breaks down the underlying technical architecture, detailing how LibreOffice coordinates bidirectional text analysis, OpenType shaping via HarfBuzz, dictionary-based line breaking, and font rasterization to display linguistically accurate glyphs on screen.
The Complex Text Layout (CTL) Engine
Standard Western scripts (like Latin) generally use a one-to-one mapping between a character code and a visual glyph, placed sequentially from left to right. In contrast, complex scripts require contextual transformations, non-linear positioning, dynamic ligatures, or bidirectional reordering.
LibreOffice handles these scripts by routing them through its CTL pipeline rather than the standard simple text rendering path. When CTL support is enabled, the layout engine relies on the International Components for Unicode (ICU) library and the HarfBuzz text-shaping engine to process characters before they are drawn.
Text Shaping with HarfBuzz
At the core of LibreOffice’s complex script rendering is HarfBuzz, an industry-standard text shaping engine. HarfBuzz receives Unicode input strings alongside font metrics and applies font-specific layout tables to determine the correct sequence and exact coordinates of visual glyphs.
- Glyph Substitution (GSUB): Replaces basic character shapes with context-appropriate glyphs, ligatures, or decomposed components.
- Glyph Positioning (GPOS): Calculates precise two-dimensional offsets for diacritics, vowel signs, and overlapping elements relative to base characters.
Handling Arabic: Bidirectionality and Cursive Joining
Arabic rendering introduces two primary challenges: directionality and cursive connectivity.
- Bidirectional Algorithm (BiDi): LibreOffice uses ICU to implement the Unicode Bidirectional Algorithm. When mixed text (such as Arabic and English) is processed, the engine assigns directional levels to each character, reordering the logical storage order into visual order from right to left.
- Contextual Shaping: Arabic letters change shape depending on whether they appear in an isolated, initial, medial, or final position. HarfBuzz reads the font’s GSUB tables to substitute standard Unicode codepoints with the appropriate positional glyph variant.
- Mark Placement: Diacritics (tashkeel) are dynamically positioned above or below base consonants using GPOS anchor points to avoid collisions with neighboring letters.
Handling Devanagari: Conjuncts and Reordering
Devanagari and other Indic scripts rely on complex syllable structures consisting of consonants, implicit vowels, explicit vowel signs (matras), and viramas (halant).
- Reordering: Certain vowels, such as the short “I” matra (ि), are typed and stored after a consonant logically, but must be rendered to the left of the consonant cluster visually. The shaping engine handles this reordering automatically before glyph assignment.
- Conjunct Formation: When a virama sits between consonants, the engine merges them into half-forms or unique conjunct ligatures (e.g., क् + ष = क्ष).
- Stacking and Matra Positioning: Below-base and above-base signs are aligned using GPOS anchor points defined in the OpenType font, ensuring correct placement regardless of consonant cluster width.
Handling Thai: Stacking and Word Boundary Detection
Thai script rendering requires multi-level vertical alignment and specialized boundary parsing.
- Vertical Diacritic Stacking: Thai characters can stack up to three levels high (base consonant, vowel mark, and tone mark). If a consonant has an ascender, the tone mark must shift upward or to the side. LibreOffice uses OpenType rules to prevent diacritic overlapping.
- Dictionary-Based Line Breaking: Unlike Latin or Arabic scripts, Thai does not use spaces between words. To wrap lines correctly without breaking mid-word, LibreOffice uses ICU’s rule-based and dictionary-based break iterators. The engine checks text against a Thai word dictionary to insert soft line breaks at legitimate lexical boundaries.
Advanced Font Tech: Graphite Support
In addition to OpenType, LibreOffice supports SIL Graphite, a programmable smart-font system designed for minority and highly complex writing systems. Graphite allows font designers to embed complex shaping logic directly into the font file, which LibreOffice executes natively for scripts that exceed standard OpenType capabilities.
Rasterization and Output
Once HarfBuzz and ICU calculate the final glyph IDs, visual ordering, and coordinate placements, LibreOffice hands the data to its graphical backend (such as Skia, Cairo, or platform-native APIs like DirectWrite on Windows and CoreText on macOS). The rasterization layer pulls the vector outlines from FreeType or the system font engine and renders the shaped, positioned text onto the document canvas.