Skip to main content
Recipe number 105

Cookbook · Chapter 2 · Type & text

An Arabic novel set right to left, bound on the right

The opening of an Arabic novel on a 14 × 21 cm page: locale 'ar' sets the text right to left, binds the book on the right and prints ١٢ for its folios.

On this page
Output
Canvas · PDF
Postext
Tested with Postext 1.65.0
Needs ≥ 1.25.0 · postext-pdf ≥ 1.25.0
Licence
Updated 10 Oct 2026
Code MIT · Text CC BY 4.0

pp. ١٠–١١ · 1–2 of 7

  • Trim 140 × 210 mm
  • 1 column
  • Markazi Text 13.5/22
  • Noto Kufi Arabic
  • Reem Kufi
  • 7 pages
  • Level
  • Postext 1.57.1
  • Laid out in 94 ms
  • 161 lines of code

In short

The first pages of a novel written in Arabic, laid out as an Arab publisher would print it: the lines run from right to left, the book opens from what a Latin reader calls the back, and the page numbers use Arabic digits.

What you'll build

The opening of حروف من رصاص (Letters of Lead), a novel about a grandson who reopens his grandfather's print shop in a Cairo alley, set as an Arab trade paperback: 14 × 21 cm, Markazi Text at 13.5 pt on a 22 pt line, justified, with the first-line indent on the right. The excerpt starts on page 10 with a part page in the blue of the shop's door, a lattice of eight-pointed stars drawn in code, and the part's name in Reem Kufi at 64 pt. The chapter opener faces it on the left-hand page, numbered in words, الفصل الأول. Folios print ١٠, ١١, ١٢; the book title runs over the right-hand pages and the chapter over the left. A trade paperback novel sets the same kind of book left to right.

This recipe answers

  • How do I set an Arabic book right to left and bind it on the right?

The short answer

script.js · lines 35–50in full code
const page = {
  width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
  // left and right are the flow's sides: in a right-to-left book left is the spine side,
  // so these margins put 20 mm at the spine and 16 mm outside on both pages of a spread.
  margins: { top: mm(23), bottom: mm(22), left: mm(20), right: mm(OUTER), mirror: true },
  // binding: 'auto' (unset) is 'right' for a right-to-left locale: page 1 is a left page.
};
const bodyText = {
  fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  // justify ends every line on both sides; the engine lengthens joined letters (kashida,
  // on by default for Arabic) after it opens the spaces a quarter. Never hyphenated.
  textAlign: 'justify', optimalLineBreaking: true,
  firstLineIndent: em(1.5), indentAfterHeading: false, // the indent is on the right
  avoidWidows: true, avoidOrphans: true, avoidRunts: true,
};

The locale sets the direction, the binding and the digits

Ingredients

Type
Markazi Text, Reem Kufi, Noto Kufi Arabic (SIL OFL 1.1)
Assets
None: every picture is drawn in code

Method

#1 · One locale, three decisions

The code is the short answer above. locale: 'ar' is all the configuration says, and the engine takes three things from it: the text runs right to left (direction: 'auto'), the book is bound on the right (page.binding: 'auto'), and every number the engine writes uses the Arabic-Indic digits (numerals: 'auto'), the folios included (Starting an Arabic book). Margins and alignments keep the words left and right, but in a right-to-left book they mean the sides of the flow: margins.left is the spine side, and the first-line indent stands on the right. Justified lines open their spaces first and then lengthen joined letters with the tatweel, the kashida of Arabic books; the text is never hyphenated (Justification and kashida).

Page 12: justified lines widened by kashida, the indent on the right, the folio ١٢ at the outer edge.

#2 · A chapter number in words

script.js · lines 54–75in full code
const SINK = 58; // mm: where the text starts on an opener
const centred = (y, size) => ({ anchor: { to: 'container', edge: 'top' },
  offset: { y: mm(y) }, ...(size && { size }) });
const opener = {
  enabled: true, minHeight: mm(SINK),
  slot: { elements: [
    { kind: 'image', id: 'star', resourceId: 'star', placement: centred(8, { width: mm(9) }) },
    // {number} is the level's numbering template below: الفصل الأول, الفصل الثاني…
    { kind: 'text', id: 'kicker', content: '{number}', fontFamily: LABEL, fontSize: pt(10),
      fontWeight: 600, color: col('door'), align: 'center', placement: centred(22) },
    { kind: 'text', id: 'title', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(30),
      fontWeight: 600, lineHeight: 1.3, color: col('ink'), align: 'center', overflow: 'wrap',
      placement: centred(29, { width: 'fill' }) },
  ] },
};
const chapter = {
  level: 1, fontFamily: DISPLAY, fontWeight: 600, marginTop: pt(0), marginBottom: pt(0),
  // Each chapter opens a page, on either side of the spine.
  breakBefore: { enabled: true, parity: 'any' },
  // A masculine ordinal in words: {1:ordinal-feminine} would give الأولى, الثانية…
  numberingTemplate: 'الفصل {1:ordinal}', advancedDesign: opener,
};

The level's numberingTemplate writes الفصل and the number as a masculine ordinal, so the second chapter is الفصل الثاني with no change to the Markdown; {1:ordinal-feminine} gives الأولى, الثانية for a feminine noun such as الليلة (Ordinal and cardinal words). The opener prints it with {number} above the title. Arabic openers are usually centred, so align: 'center' avoids any question of which side the design starts on.

#3 · Running heads stay on their pages

script.js · lines 79–96in full code
// Running heads and folios keep their physical sides in a right-to-left book. The right page
// is an even page here and is read first: the book's title; the left page, the chapter's.
const label = { fontFamily: LABEL, fontSize: pt(8), color: col('muted'), pages: 'body' };
const at = (edge, x, y = 12) => ({ anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(y) } });
const folio = { ...label, fontWeight: 600, color: col('ink') };
const header = { elements: [
  { kind: 'text', id: 'r-folio', content: '{pageNumber}', parity: 'even', ...folio,
    align: 'right', placement: at('top-right', -OUTER) },
  { kind: 'text', id: 'r-title', content: '{title}', parity: 'even', ...label, align: 'right',
    placement: at('top-right', -(OUTER + 9)) },
  { kind: 'text', id: 'l-chapter', content: '{chapterTitle}', parity: 'odd', ...label,
    align: 'left', placement: at('top-left', OUTER + 9) },
  { kind: 'text', id: 'l-folio', content: '{pageNumber}', parity: 'odd', ...folio,
    align: 'left', placement: at('top-left', OUTER) },
] };
// The openers carry no head: their folio drops to the foot, centred.
const footer = { elements: [{ kind: 'text', id: 'drop-folio', content: '{pageNumber}', ...folio,
  pages: 'opener', align: 'center', placement: at('bottom', 0, -11) }] };

Body text follows the flow, but running heads and folios keep their physical sides. Page 12 is an even page and a right-hand page in this book, so its folio goes top right with the book's title beside it; page 13 is on the left and carries the chapter. Read a spread from the right, as an Arab reader does, and the book's title comes first. showBook lays the pages out mirrored, page 11 to the left of page 10, so the screen shows the spreads as the book opens.

#4 · A part page drawn in code

script.js · lines 100–120in full code
// # الدرب {style="plate" kicker="…" line="…"}: a heading that paints a whole page.
const onPage = (y) => ({ anchor: { to: 'page', edge: 'top' }, offset: { y: mm(y) } });
const plate = {
  id: 'plate', span: 'page', numbered: false, runningChapter: false, toc: false,
  breakBefore: { enabled: true, parity: 'any' }, // (gotcha: style-inherits-break)
  header: { elements: [] }, footer: { elements: [] },
  advancedDesign: { enabled: true, slot: { elements: [
    // The drawing carries 3 mm of bleed a side: a box of the trim would fit it by its width
    // and leave a strip of paper at the head and the foot.
    { kind: 'image', id: 'field', resourceId: 'lattice', placement: {
      anchor: { to: 'page', edge: 'top-left' }, offset: { x: mm(-3), y: mm(-3) },
      size: { width: mm(TRIM.width + 6), height: mm(TRIM.height + 6) } } },
    { kind: 'text', id: 'part', content: '{attr.kicker}', fontFamily: LABEL, fontSize: pt(11),
      fontWeight: 600, color: col('brass'), align: 'center', placement: onPage(76) },
    { kind: 'text', id: 'name', content: '{titleText}', fontFamily: DISPLAY, fontSize: pt(64),
      fontWeight: 600, lineHeight: 1.2, color: col('paper'), align: 'center',
      placement: onPage(86) },
    { kind: 'text', id: 'line', content: '{attr.line}', fontFamily: TEXT, fontSize: pt(13),
      color: col('paper'), align: 'center', placement: onPage(124) },
  ] } },
};

The part page is a first-level heading, # الدرب {style="plate" …}, whose style paints the whole sheet and clears its head and folio. The lattice is an SVG generated in the pen from two squares turned 45°, the khātam star of Islamic geometric ornament, in the palette's blue and brass, so the PDF keeps it as vector paths. numbered: false keeps the part page out of the chapter count, and the opener that follows is الفصل الأول.

The whole recipe

Sandbox
// ═══ Postext Cookbook · Nº 105 · An Arabic novel set right to left, bound on the right ═══
// https://postext.dev/en/cookbook/arabic-novel-right-bound
// Code: MIT · Text: original Arabic prose (CC BY 4.0) · Pattern: drawn in code
// Fonts: Markazi Text, Reem Kufi, Noto Kufi Arabic (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocument, withLoadedFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
import { renderToPdf, decompressWoff2 } from 'https://esm.sh/postext-pdf';

const LANG = 'en'; // @lang: the language of the frame; the novel is Arabic in both editions
const RECIPE = 'arabic-novel-right-bound';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// The blue of the press's door, the brass of its key, ink on a cream book paper.
// #region palette: the door's blue, the key's brass, every colour linked by id
const palette = {
  ink: '#1f1c19', // text: a warm near-black
  door: '#2b4766', // the accent: the part page's field, the openers' kickers
  brass: '#b08d4a', // the second accent: the medallion's line and the openers' star
  lattice: '#3d5d82', // the pattern's lines, a step lighter than the field
  rule: '#cbc3b5', // hairlines
  muted: '#6b645b', // running heads, folios, the colophon
  paper: '#fbf8f1', // a cream book paper
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = Object.entries({ ...palette, 'main-color': palette.door })
  .map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } }));
// #endregion
const [TEXT, DISPLAY, LABEL] = ['Markazi Text', 'Reem Kufi', 'Noto Kufi Arabic'];
const [BODY, LEAD] = [13.5, 22]; // pt: Naskh sets small, so 13.5 pt on 1.63 × the size
const TRIM = { width: 140, height: 210 }; // mm: 14 × 21 cm, the Arab trade novel
const OUTER = 16; // mm: the outer margin, where the folios stand

// #region answer: the locale sets the direction, the binding and the digits
const page = {
  width: mm(TRIM.width), height: mm(TRIM.height), dpi: 150, backgroundColor: col('paper'),
  // left and right are the flow's sides: in a right-to-left book left is the spine side,
  // so these margins put 20 mm at the spine and 16 mm outside on both pages of a spread.
  margins: { top: mm(23), bottom: mm(22), left: mm(20), right: mm(OUTER), mirror: true },
  // binding: 'auto' (unset) is 'right' for a right-to-left locale: page 1 is a left page.
};
const bodyText = {
  fontFamily: TEXT, fontSize: pt(BODY), lineHeight: pt(LEAD), color: col('ink'),
  boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
  // justify ends every line on both sides; the engine lengthens joined letters (kashida,
  // on by default for Arabic) after it opens the spaces a quarter. Never hyphenated.
  textAlign: 'justify', optimalLineBreaking: true,
  firstLineIndent: em(1.5), indentAfterHeading: false, // the indent is on the right

The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)

Variations

A Maghreb edition prints 1, 2, 3; locale: 'ar-MA' does it by itself, and the Maghreb edition shows the whole book.

-  locale: 'ar', // written out, never LANG (gotcha: arabic-locale-tag)
+  locale: 'ar', numerals: 'latn',

#Justify with spaces alone

Some publishers keep the tatweel out of running text: the spaces take the whole difference.

   textAlign: 'justify', optimalLineBreaking: true,
+  kashida: 'none',

Pitfalls

Pitfall

Tag an Arabic book 'ar', not with LANG

A recipe's editions are en and es, but an Arabic sample is Arabic in both: `locale: LANG` would set it left to right, bind it on the left, number its pages 1 2 3 and caption its figures Figure or Figura. From postext 1.30 the tag is also the language the text is shaped in (measured, painted and embedded), so under an English or Spanish tag a face such as Markazi Text does not draw the Arabic forms of its guillemets and stops, and the lines break elsewhere. Write the tag yourself: 'ar' (Arabic-Indic digits, the Mashriq convention), a region such as 'ar-EG' or 'ar-SA', or 'ar-MA', 'ar-DZ' or 'ar-TN' for a Maghreb edition with European digits. A Latin page that only quotes Arabic keeps its own locale. Right-to-left text →

Pitfall

Arabic faces need their arabic file, through the arabic block

Fontsource serves Amiri, Noto Naskh Arabic or Scheherazade New as one file per subset, and the Arabic letters are in the arabic file. loadFonts fetches only latin (and latin-ext), so on screen the Arabic comes from a system face and measures wrong, and fontsourceProvider hands the PDF that latin file, which prints empty boxes. List the arabic kit block, call loadArabicFonts(FONTS, markdown) after loadFonts, with every weight the pages set in Arabic listed in FONTS, and give renderToPdf fontProvider: arabicPdfProvider. Arabic fonts →

Pitfall

A right-bound book shows its spreads with showBook

In a book bound on the right (Arabic, Hebrew or Persian text, vertical Chinese, or page.binding 'right') page 1 is still the recto, but it lies on the left of the spine, and the pairs read [3 | 2]. showPages lays every book out left-bound; showBook from the book block (the cjk block carries the same function) reads doc.binding and mirrors the pairs. capture.hero still names a spread in reading order, [verso, recto]: [2, 3]. Books bound on the right →

Pitfall

A heading style inherits its level's page break

A headingStyles entry takes every field it leaves out from its heading level, breakBefore included. A contents page or a colophon styled on an H1 after a :::pagebreak inherits parity 'odd' and lands behind a blank page. Give such a style breakBefore: { enabled: false }. Heading styles →

  • A colophon in English or Spanish at the end of an Arabic book needs {dir=ltr} on its block: without it the Latin line is aligned and punctuated as a right-to-left paragraph, with its full stop on the left.

Credits

Text
  • The opening of a novel, حروف من رصاص (Letters of Lead), written in Arabic for the recipe; its author and its people are invented · Postext Cookbook · original
Fonts
Markazi Text (SIL OFL 1.1) · Reem Kufi (SIL OFL 1.1) · Noto Kufi Arabic (SIL OFL 1.1)
SandboxPDF