When you type an Arabic word in a text editor and see it on screen — connected, diacriticized, flowing in the right direction — the text passes through several processing stages. This article explains what happens under the hood. Not just out of curiosity, but because understanding these stages explains why Arabic breaks in some applications and not others.
Stage 1: Storage — Everything Is Unicode
Arabic text is stored in memory using Unicode encoding (usually UTF-8). Every character has a fixed code point: ba is U+0628, ta is U+062A, fatha is U+064E. The order in memory is “logical” — following reading order, not screen position. The first letter of a word is stored first even though it appears on the right.
This means the file doesn’t contain “letters in different shapes” — it contains a single character (ba), and the device decides which form to display.
Stage 2: Direction — The BiDi Algorithm
Arabic text runs right-to-left, but numbers and English words within it run left-to-right. This coexistence is managed by the Unicode Bidirectional Algorithm (known as UAX#9).
How does it work? Every character in Unicode has a directional type. Arabic letters are classified as “strong RTL,” Latin letters as “strong LTR,” and digits and spaces as “neutral” or “weak.”
The algorithm takes the stored character sequence and reorders it visually: Arabic segments are flipped to appear from the right, Latin segments stay on the left. This all happens automatically — provided the engine knows the base paragraph direction (dir="rtl").
Without specifying the base direction, the algorithm guesses. And it can guess wrong — especially in paragraphs that start with a number or an English word.
Stage 3: Shaping — HarfBuzz
After determining display order, the shaping engine takes over. The most widely used one today is HarfBuzz — an open-source library used by Firefox, Chrome, Android, KOReader, and most modern applications.
What HarfBuzz does:
-
Selects the correct form for each letter based on position. Ba at the start of a word becomes “بـ,” in the middle “ـبـ,” and at the end “ـب.” These are called positional forms, stored in the font under OpenType features named
init,medi,fina, andisol. -
Applies ligatures. Certain letter sequences render as a single glyph. The most common is “la” (lam + alef = ﻻ). This ligature is mandatory — every Arabic font must include it. The responsible OpenType features:
rlig(required ligatures) andliga(optional ligatures). -
Positions diacritics. A fatha doesn’t sit in the same spot above ba as it does above ta — the font specifies the exact placement for each diacritic on each letter in each form. This is handled by the font’s GPOS tables.
If the font lacks these features, or the engine doesn’t support them, problems appear: disconnected letters, misplaced diacritics, missing ligatures.
Stage 4: Layout — Arranging Lines
After shaping, the text passes through the layout engine, which decides:
-
Where to break lines. In Arabic, breaks occur between words (at spaces). Words aren’t split arbitrarily — and if hyphenation does occur, each part must retain its correct form as if unsplit.
-
How to justify lines. Justified alignment in Arabic differs from Latin scripts. In Latin text, spaces between words are widened. In traditional Arabic typography, kashida (tatweel) is used — a horizontal stroke that extends a letter. But CSS doesn’t support automatic kashida, and there’s no web standard for it. So justified alignment in browsers works by expanding word spacing only — which is less elegant than what Arabic readers are accustomed to.
-
Line height. Because diacritics sit above and below letters, diacriticized Arabic text needs more line height than English. A value of 1.8 is comfortable; 1.5 is too tight for text with diacritics.
Why Does Performance Vary Between Engines?
| Engine | Shaping Library | Notes |
|---|---|---|
| Gecko (Firefox) | HarfBuzz | One of the earliest adopters, excellent performance |
| Blink (Chrome/Edge) | HarfBuzz | Adopted later, good now but had bugs previously |
| WebKit (Safari) | CoreText (Apple) | Apple’s proprietary engine, good for Arabic but issues with EPUB |
| MuPDF (KOReader) | HarfBuzz (since 2022) | Major improvement after adding BiDi support |
The differences aren’t in the algorithm (all follow Unicode) but in the details: how they handle letter joining across separate HTML elements, when they invoke OpenType features, and how they deal with incomplete fonts.
What Does This Mean for Ebooks?
An EPUB is a collection of HTML pages. The e-reader uses one of the engines listed above to render them. If you understand how the engine works, you understand why a book looks fine on Firefox but breaks on Apple Books.
More importantly: if you’re building ebooks, controlling these stages is what separates a well-formed book from a broken one:
- Direction:
dir="rtl"in HTML +page-progression-direction="rtl"in OPF - Shaping: an Arabic font with full OpenType features
- Layout: sufficient
line-height,letter-spacing: 0, logical CSS properties
Nassiq handles all of this automatically — from scanned PDF to a properly shaped and directed EPUB.