خطKhatt

Arabic calligraphy composed as real typography, then used to constrain what a generative image model is allowed to draw.

khatt — Arabic for line, script, or calligraphy.

Why I built this

Working on projects around weddings and celebrations, I kept seeing the same request: a couple's names set in Arabic calligraphy, to be used as a monogram on an invitation or a backdrop. In Latin script a generative model will do that readily. In Arabic it will not — it returns strokes with the texture of script and none of the letters.

That gap is what this is about. I built it to learn where it comes from and whether it can be closed by constraining the model rather than by asking it more nicely.

A ninth-century Kufic Qur'an folio with angular, horizontally extended letterforms in brown ink on vellum.
كوفي Kufic
8th–9th c. Angular, geometric, horizontally stretched.
A Qur'an manuscript page by Ibn al-Bawwab in fine Naskh script with gold illumination.
نسخ Naskh
c. 1000 CE. Legible, even, made for copying books.
An album folio showing the Arabic alphabet written in Thuluth script.
ثلث Thuluth
Tall verticals, deep curves. The monumental hand.
A Diwani calligraphic composition where dense cursive text is massed into a single sweeping crescent shape.
ديواني Diwani
19th c. Text massed into a single composed shape.

Historical plates, public domain. Sources listed at the foot of the page.

The craft is composition

Arabic script is cursive and contextual. A letter changes shape depending on whether it stands alone or sits at the start, middle or end of a word, and letters join to their neighbours. A calligrapher works within those rules and arranges the result into a shape — the Diwani plate above is one sentence massed into a crescent.

That is the part generative models get wrong. Asked for Arabic calligraphy, they produce the texture of script without the script: strokes that look correct at a glance and resolve into nothing.

Two panels side by side: a historical Naskh manuscript page, and a square of model-generated strokes that resemble Arabic but form no real letters.
Figure 1. Left, a page set by a calligrapher who knew the rules. Right, unconstrained model output: the stroke weight and rhythm are plausible, the letterforms are not letters. Panel (b) was generated for this page as an illustration of the failure mode.

Eight stages, one generative

Two names, from typed input to an ornamented piece. Everything up to the last step is deterministic: the names are mapped into Arabic script, set in a chosen face, composed, and fitted to a silhouette picked in advance. Only then does a model run, and what it receives is the contour of the actual words rather than a prompt describing them. Its job is ornament, not spelling.

input stage
1/10

input

Ameer & Samar, typed as given

Arabic chat alphabet, where digits stand in for letters with no Latin equivalent: 3 is ع, 7 is ح. Before anything can be set, this has to be resolved into real script.

latin input

script mapping stage
2/10

script mapping

أمير و سمر

Mapped to Arabic and joined with و, not an ampersand. A Latin mark inside a right-to-left run reorders unpredictably under the bidi algorithm.

rtl, bidi-safe

typeface stage
3/10

typeface

Set in Aref Ruqaa

The tool ships five Arabic faces. Four are book faces built for reading. Aref Ruqaa has calligraphic weight distribution and a long descending ع — the one that survives being ornamented.

5 faces compared

composition stage
4/10

composition

Stacked and optically centred

Centred on real ink bounds via getBBox(), not on the text baseline. Arabic baselines sit off centre because of ascenders and descenders, so baseline-centring looks wrong.

svg, vector

target shape stage
5/10

target shape

A tulip silhouette, chosen first

The outline is picked before generation, not drawn around the result. The composition has to resolve into this shape, which makes it a constraint on layout rather than a frame added afterwards.

constraint, not frame

training set stage
6/10

training set

1,268 Thuluth image/caption pairs

Each image cleaned and normalised, captioned to a fixed trigger phrase, and deduplicated by MD5 hash so no plate is learned twice. A second Kufi set alongside it. A LoRA trained on this and pinned to a version hash, so any output can be traced back to the exact weights that produced it.

1,268 pairs

control image stage
7/10

control image

Inverted to white on black

This is what the model actually receives: the contour of the real words inside the chosen silhouette. Not a prompt describing Arabic — the shape of it.

controlnet input

Two rejected placement candidates side by side: one too small, leaving most of the tulip empty, and one too large, with strokes breaking outside the outline.
8/10

fit test

Two terms pulling against each other

Filling the silhouette is rewarded; any stroke falling outside it is subtracted. Both failures are real: too small and the shape stays empty, too large and the strokes break out of it. The lowest overflow of any candidate belongs to the one that fills almost nothing, so neither term can be optimised on its own.

fill rewarded · overflow penalised

Four candidate placements of the same Arabic composition inside the tulip silhouette, each on a measurement grid with bars for percentage filled and percentage overflowing. The second scores highest and is marked selected.
Figure 3. Four candidates under the two competing terms. Teal is stroke inside the silhouette, which the score rewards; ochre is stroke outside it, which the score subtracts. Candidate (i) has the lowest overflow of all four at 1% and still loses, because it fills only 11% of the shape — the two terms cannot be satisfied separately.
The selected placement shown as three regions: the area inside the tulip not occupied by text in ochre, the letterforms in teal, and everything outside the outline in pale grey.
9/10

illumination space

What is left for the ornament

Subtracting the letterforms from the silhouette leaves the region the generative step is allowed to work in — 78% of the shape here. The letterforms and everything beyond the outline are held, so the model is given a space to fill rather than a picture to reinterpret.

letterforms held

illumination stage
10/10

illumination

Ornament worked around the letterforms

The only generative step. Guidance is kept low at 3 with 28 inference steps; pushed higher, the model asserts its own idea of the letterforms and the conditioning stops holding.

guidance 3 · 28 steps

Stages 1–5 and 7–9 are deterministic and reproducible. Stage 6 is a sample of the training set rather than a step applied to these names. Stage 10 is the generative step, shown below at four ornament densities.

The composition with dense floral arabesque ornament.
floral
The composition with geometric Kufic-derived ornament.
geometric
The composition with sparse ornament, letterforms dominant.
sparse
The composition with fine scrollwork ornament.
scrollwork

The same composition at four ornament densities. The letterforms and the silhouette are held fixed; only the fill changes.

What's underneath

A browser tool, written in plain JavaScript and SVG with no framework and no build step, plus a small Node server that proxies to Replicate so the API token stays out of the browser. The generation step runs against a LoRA trained on a curated Arabic calligraphy set, pinned to an explicit version so output stays comparable over time.

Layout
Text placed inside a shape, along its perimeter, or repeated and clipped to it. Output is SVG.
Shapes
Circle, ellipse, rectangle, rounded rectangle, freeform polygon, arbitrary SVG path.
Type
Amiri, Noto Naskh Arabic, Scheherazade New, Aref Ruqaa, Lateef.
Server
Node, raw http. Single generation, generic transform, and a chained multi-step pipeline.
Training set
1,268 Thuluth image/caption pairs: cleaned and normalised, captioned to a fixed trigger phrase, deduplicated by MD5 hash. A second Kufi set alongside it.
Model
A custom LoRA trained on Replicate, pinned by version hash. Guidance 3, 28 steps. Conditioning strength and seed swept 4 × 3 per configuration.
Size
About 1,700 lines of JavaScript across six modules.

Guidance is kept low deliberately. Pushed higher, the model asserts its own idea of the letterforms and the structural conditioning stops holding.

Scope

A research tool, built to test one question: whether a generative model can be constrained to respect a script it renders badly on its own.

  • Glyph shaping is delegated to the platform's text engine; the layout layer sits above it, with a defined interface for a dedicated shaper.
  • Placement is scored geometrically, so a result can be reproduced and compared rather than judged by eye.
  • The model is pinned to an explicit version, which keeps output comparable as the pipeline changes.

The transferable part is the approach: treat the model as a renderer working inside a structure you control, rather than as something you ask for text and hope. That generalises to any script a model handles badly.