| --- |
| license: cc-by-4.0 |
| language: |
| - ar |
| task_categories: |
| - text-to-image |
| tags: |
| - arabic |
| - typography |
| - text-rendering |
| - internationalization |
| - evaluation |
| - bidi |
| - benchmark |
| - rtl |
| - arabic-nlp |
| - reproducible |
| - harfbuzz |
| - pillow |
| - shaping |
| - arabic-reshaper |
| pretty_name: ArShape |
| size_categories: |
| - n<1K |
| configs: |
| - config_name: results |
| data_files: |
| - split: test |
| path: results.jsonl |
| --- |
| |
| # ArShape — when does the standard Arabic rendering recipe break Arabic? |
|
|
| **Author:** Syamjith NK |
| **Write-up:** [The Arabic fix everyone recommends is now the thing breaking your Arabic](https://syamjithnk.com/arabic-text-rendering) |
| **Companion dataset:** [ArNum-TTS](https://huggingface.co/datasets/syamjithnk/arnum-tts) — the same failure mode in speech. |
|
|
| ## The finding |
|
|
| `arabic_reshaper` + `python-bidi` before drawing is the standard advice for rendering |
| Arabic in Python. On any renderer that already performs complex-text layout it |
| **corrupts the output**, because the shaping and the bidi reorder both happen twice. |
|
|
| The word is "welcome": |
|
|
| | | rendered as | |
| |---|---| |
| | correct (no preprocessing) | مرحبا بكم | |
| | after reshaper + bidi | مكب ابحرم | |
|
|
| Each string sits alone in its own table cell, because putting them either side of an |
| arrow on one line lets the bidi algorithm reorder them - which, on a page about Arabic |
| being silently reordered, is exactly the wrong mistake to make. |
|
|
| Pillow 12 links Raqm/HarfBuzz, so it already does this work. The recipe was correct |
| when it was written; text stacks caught up and the advice did not. |
|
|
| ## Measured |
|
|
| 3 Arabic fonts × 5 strings × 4 rendering paths, scored on shape similarity against a |
| verified-correct reference: |
|
|
| | rendering path | identical | recognisable | broken | |
| |---|---|---|---| |
| | modern engine, text as-is | **15** | 0 | 0 | |
| | modern engine + reshaper + bidi | 0 | 1 | **14** | |
| | no shaping engine, text as-is | 0 | 0 | **15** | |
| | no shaping engine + reshaper + bidi | 0 | 9 | 6 | |
|
|
| The last row is the useful nuance: on a renderer without shaping the recipe is a |
| **partial** rescue. Plain Arabic recovers, but digits, embedded Latin and diacritics |
| still fail — positioning combining marks and resolving bidirectional runs needs real |
| shaping regardless of what you pre-process. |
|
|
| ## Font coverage trap |
|
|
| Checked against the font `cmap` tables, not by eye: **SF Arabic and Geeza Pro contain |
| no Latin letters or digits at all.** `في عام 2026` renders the year as `.notdef` boxes |
| while looking entirely correct to anyone not reading the number. IBM Plex Sans Arabic |
| has full coverage. |
|
|
| ## The practical version |
|
|
| - Check whether your renderer does complex-text layout — in Pillow, |
| `PIL.features.check("raqm")`. |
| - If it does: pass Arabic through untouched. Do not reshape. |
| - If it does not: reshape, and test digits, Latin and diacritics separately, because |
| those will still be wrong. |
| - Check the font actually contains every character you are about to draw. |
|
|
| ## Honesty about method |
|
|
| Two errors, both caught only by looking at the output rather than the score, and both |
| recorded because they are instructive: |
|
|
| 1. **The premise was backwards.** I was confident reshaping was required, so I made it |
| the reference the others were scored against — which meant the correct renderings |
| scored worst. |
| 2. **The metric compared position as well as shape**, so a visually identical render |
| scored zero because two layout engines place glyphs a few pixels apart. Fixed by |
| normalising to the ink bounding box, then reporting three bands instead of |
| pass/fail — which is what exposed the partial-rescue result above. |
|
|
| A benchmark inherits the assumptions of whoever wrote it. The only reliable way to find |
| them is to look at the output. |
|
|
| ## Reproduce |
|
|
| ```sh |
| python render_bench.py |
| ``` |
|
|
| Renders every combination to `out/` and writes `results.jsonl`. Images are not shipped |
| — they regenerate in seconds and the fonts are system fonts. |
|
|
| <!-- series-block --> |
| ## Does Arabic Survive the Pipeline? |
|
|
| This is one of three reproducible benchmarks, one per stage of a real production pipeline. |
| Each measures an Arabic failure that looks correct to anyone who does not read Arabic — which |
| is exactly why it ships. |
|
|
| - [ArNum-TTS](https://huggingface.co/datasets/syamjithnk/arnum-tts) — do numbers survive speech synthesis? |
| - [ArShape](https://huggingface.co/datasets/syamjithnk/arshape) — does the standard reshaping recipe survive rendering? **→ you are here** |
| - [ArPDF](https://huggingface.co/datasets/syamjithnk/arpdf) — does Arabic survive a PDF round trip? |
|
|
| All three are CC BY 4.0 and ship the scorer, the raw per-item results, and an explicit |
| statement of what the measurement does *not* establish. |
| <!-- series-block --> |
|
|