Skip to main content
Recipe number 222

Cookbook · Chapter 13 · CMS integration

The same marks on right-to-left and vertical pages

One way of writing marks for an Arabic page set right to left and a Japanese page set in vertical columns: layoutAnnotations follows each page's lines.

On this page
Postext
Tested with Postext 1.103.1
Needs ≥ 1.103.0
Licence
Updated 4 Dec 2026
Code MIT · Text CC BY 4.0
The same four marks on an Arabic page set right to left and a Japanese page set vertically, each listed with its pieces and the direction they run.
The same four marks on an Arabic page set right to left and a Japanese page set vertically, each listed with its pieces and the direction they run.
  • Trim 120 × 170 mm
  • 1 column
  • Noto Naskh Arabic 12.5/21
  • Noto Serif JP
  • 2 pages
  • Level
  • Postext 1.103.1
  • Laid out in 43 ms
  • 142 lines of code

In short

An Arabic page and a Japanese page shown side by side, each with the same four corrections: a highlight, a crossed-out word, a replaced word and a note. Under each page, a list says how many pieces each mark has and which way it runs.

What you'll build

Two pages side by side, each in a page viewer: an Arabic page about a bookbinder's workshop, set right to left in Noto Naskh Arabic, and a Japanese bunko page about a watch repairer, set in vertical columns in Noto Serif JP. Each carries the same four marks a review tool would leave: a highlight, a deletion, a replacement and a numbered note. On the Arabic page the replacement reads right to left above the struck words; on the Japanese page the strike runs down the middle of the column and the replacement stands upright beside it. Under each page, a list gives each mark's pieces and the way the first one runs.

This recipe answers

  • How do I mark text on right-to-left and vertical pages?

The short answer

script.js · lines 132–145in full code
// layoutAnnotations follows the page's lines: a strike crosses a vertical column down its
// middle, a replacement's text reads right to left on the Arabic page and stands upright on
// the Japanese one, and a note's flag goes to the nearer margin, the foot of a vertical page.
const show = (id, doc, marks, fontFamily) => {
  const shapes = layoutAnnotations(doc, marks, { fontFamily, paper: palette.paper });
  createViewer(document.getElementById(id), {
    document: doc, zoom: 'fit-page', gap: 16, selectable: false, labels: labels.viewer,
    // A layer paints on the page in sheet px, the unit the shapes are in.
    layers: [{ id: 'marks', paint: (ctx, { pageIndex }) =>
      paintAnnotations(ctx, shapes, { pageIndex }) }],
  });
};
show('arabic', arabicDoc, arabicMarks, NASKH);
show('japanese', japaneseDoc, japaneseMarks, MINCHO);

The marks laid out on each page, painted in a layer of the viewer

Ingredients

Type
Noto Naskh Arabic, Noto Serif JP (SIL OFL 1.1)
Assets
None: every picture is drawn in code

Method

#1 · Give each page its writing mode

script.js · lines 39–65in full code
// 'ja' with 'vertical-rl' sets each line top to bottom and the lines from right to left; 'ar'
// sets the text right to left. Nothing in the marks' code changes with either.
const config = { // the Japanese page
  locale: 'ja', // written out, never LANG (gotcha: ja-locale-tag)
  colorPalette, header: none, footer: none,
  page: { sizePreset: 'custom', width: mm(105), height: mm(148), dpi: 200, // 文庫判
    backgroundColor: col('paper'),
    margins: { top: mm(15), bottom: mm(12), left: mm(11), right: mm(11) } },
  layout: { layoutType: 'single', writingMode: 'vertical-rl' },
  bodyText: { fontFamily: MINCHO, fontSize: pt(9.5), lineHeight: pt(16.5), ...ink,
    textAlign: 'justify', firstLineIndent: em(1), indentAfterHeading: true },
  headings: { fontFamily: MINCHO, fontWeight: 600, color: col('vermilion'), levels: [
    { level: 1, fontSize: pt(13), lineSpan: 3, indent: em(2) }, // 3行取り, two characters down
  ] },
};
const arabic = { ...config, // the Arabic page: its own language, trim, face and mode
  locale: 'ar', // written out, never LANG (gotcha: arabic-locale-tag)
  page: { ...config.page, width: mm(120), height: mm(170), dpi: 150,
    margins: { top: mm(18), bottom: mm(18), left: mm(16), right: mm(16) } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: NASKH, fontSize: pt(12.5), lineHeight: pt(21), ...ink,
    textAlign: 'justify', firstLineIndent: em(1.5), indentAfterHeading: false,
    avoidWidows: true, avoidOrphans: true },
  headings: { fontFamily: NASKH, fontWeight: 700, color: col('accent'), levels: [
    { level: 1, fontSize: pt(18), lineHeight: pt(28), marginTop: pt(0), marginBottom: pt(14) },
  ] },
};

The Japanese page is the config, with writingMode: 'vertical-rl'; the Arabic one spreads it and changes the language, the trim and the face. locale: 'ar' sets the text right to left. The marks' code below never asks which page it is drawing on.

#2 · Write the marks the same way for both

script.js · lines 94–105in full code
// A mark is a range of the Markdown. `find` gives the range of a phrase that occurs once.
const find = (text, words) => ({ start: text.indexOf(words),
  end: text.indexOf(words) + words.length });
const marksOn = (text, [lit, cut, [was, now], noted]) => [
  { id: 'h', kind: 'highlight', range: find(text, lit) },
  { id: 's', kind: 'strike', range: find(text, cut) },
  { id: 'r', kind: 'replace', range: find(text, was), text: now },
  { id: 'n', kind: 'note', range: find(text, noted), label: '1' },
];
const arabicMarks = marksOn(arabicText,
  ['أربعين سنة', 'صغيرة', ['خشب الجوز', 'خشب الزان'], 'بورق ياباني رقيق']);
const japaneseMarks = marksOn(markdown, ['七十八歳', '一つずつ', ['丸一日', '二日'], '洗浄液']);

A mark is a range of the Markdown, so the Arabic and the Japanese lists are built by the same function from four phrases each. The replacement's text is in the page's own language, and the fonts load the characters of that text too, or the browser would set it in a fallback face.

#3 · Lay the marks out and paint them over each page

The code is the short answer above. layoutAnnotations places each mark from the page's own lines: on the vertical page a strike crosses the column down its middle, a highlight broken by a column end gives two pieces, and the note's flag goes to the foot. The viewer paints the page; its layer gets a context in sheet px, the unit the shapes are in, so paintAnnotations draws them as they are. The annotation marks docs give every kind.

#4 · Read which way the text runs

script.js · lines 149–163in full code
// A quad runs in the line's own order, quad[0] to quad[1]: leftwards on the Arabic page,
// downwards on the Japanese one. A range broken across two lines or columns gives two pieces.
const way = ([a, b]) => (Math.abs(b.x - a.x) > Math.abs(b.y - a.y)
  ? (b.x < a.x ? '←' : '→') : (b.y > a.y ? '↓' : '↑'));
const list = (id, doc, text, marks) => document.getElementById(id).replaceChildren(
  ...marks.map((m) => {
    const pieces = rangeRects(doc, m.range);
    const [li, words] = [document.createElement('li'), document.createElement('bdi')];
    words.textContent = text.slice(m.range.start, m.range.end); // its own direction
    li.append(`${labels.kinds[m.id]} · `, words, ` · ${pieces.length} `
      + `${labels.pieces[pieces.length === 1 ? 0 : 1]} ${way(pieces[0].quad)}`);
    return li;
  }));
list('arabic-pieces', arabicDoc, arabicText, arabicMarks);
list('japanese-pieces', japaneseDoc, markdown, japaneseMarks);

rangeRects gives a piece for each run of a line, and its quad keeps the line's order: quad[0] to quad[1] runs leftwards on the Arabic page and downwards on the Japanese one. A host that draws its own underline or comment pin takes the direction from there instead of guessing it from the language.

The whole recipe

// ═══ Postext Cookbook · Nº 222 · The same marks on right-to-left and vertical pages ═════
// https://postext.dev/en/cookbook/marks-on-rtl-and-vertical-pages
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Noto Naskh Arabic, Noto Serif JP (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// An Arabic page set right to left and a Japanese page set in vertical columns, each in
// postext-viewer, each with the same four marks: a highlight, a deletion, a replacement and a
// note. The host writes the marks the same way for both, as ranges of the Markdown; the
// engine turns them to follow each page's lines, and a list under each page says how.
import {
  buildDocument, withLoadedFonts, renderPageToCanvas, loadVerticalAlternates, rangeRects,
} from 'https://esm.sh/postext';
import { layoutAnnotations, paintAnnotations } from 'https://esm.sh/postext/annotate';
import { createViewer } from 'https://esm.sh/postext-viewer';

const LANG = 'en'; // @lang: the language of the labels; both pages are the same in each edition
const RECIPE = 'marks-on-rtl-and-vertical-pages';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: ink on a warm paper, one accent per page, every colour linked by id
const palette = {
  ink: '#1e1b18', // text
  accent: '#2b4766', // the Arabic title's blue
  vermilion: '#a8392b', // 朱: the Japanese title
  paper: '#fbf8f1', // a cream book paper
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
  ...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion
const [NASKH, MINCHO] = ['Noto Naskh Arabic', 'Noto Serif JP'];
const ink = { color: col('ink'), boldColor: col('ink'), italicColor: col('ink'),
  referenceColor: col('ink') };
const none = { elements: [] }; // one page each: no running heads

// #region pages: writingMode turns the Japanese lines; the locale sets the Arabic direction
// 'ja' with 'vertical-rl' sets each line top to bottom and the lines from right to left; 'ar'
// sets the text right to left. Nothing in the marks' code changes with either.
const config = { // the Japanese page
  locale: 'ja', // written out, never LANG (gotcha: ja-locale-tag)
  colorPalette, header: none, footer: none,
  page: { sizePreset: 'custom', width: mm(105), height: mm(148), dpi: 200, // 文庫判
    backgroundColor: col('paper'),
    margins: { top: mm(15), bottom: mm(12), left: mm(11), right: mm(11) } },
  layout: { layoutType: 'single', writingMode: 'vertical-rl' },
  bodyText: { fontFamily: MINCHO, fontSize: pt(9.5), lineHeight: pt(16.5), ...ink,

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

#Let the viewer lay out the marks

The viewer's annotations option does the same layout and paint, and also names each mark for screen readers and reports it on annotationclick.

-  const shapes = layoutAnnotations(doc, marks, { fontFamily, paper: palette.paper });
   createViewer(document.getElementById(id), {
     document: doc, zoom: 'fit-page', gap: 16, selectable: false, labels: labels.viewer,
-    // A layer paints on the page in sheet px, the unit the shapes are in.
-    layers: [{ id: 'marks', paint: (ctx, { pageIndex }) =>
-      paintAnnotations(ctx, shapes, { pageIndex }) }],
+    annotations: marks, marks: { fontFamily, paper: palette.paper }, // named for readers too
   });

#Set a whole vertical novel

A bunko novel set vertically, with furigana sets a Japanese book on the same page, with ruby, a character grid and running heads down the fore-edge.

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

Tag a Japanese text 'ja', never zh-Hans or LANG

A recipe's editions are en and es, but a Japanese sample is Japanese in both: `locale: LANG` would tag it English or Spanish, and a Chinese tag would set it by the Chinese rules (Kaiming punctuation, small kana free to start a line, 图 for 図, Chinese glyph forms in the PDF). From postext 1.30 the tag is also the language the text is shaped in (measured, painted and embedded), so any tag but 'ja' gives other glyph forms than the Japanese ones (the Chinese ones under a Chinese tag) and other line breaks. Write 'ja': it picks the Japan region (JLReq line breaking and punctuation, sesame emphasis marks, furigana spacing, 図 and 表 labels) and turns hyphenation off. The lint fails a text with kana under a zh or ko tag. Japanese line breaking (kinsoku) →

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

Chinese faces load by slices, through the cjk block

Fontsource serves a Chinese, Japanese or Korean family as about a hundred files per weight, each covering a range of characters. loadFonts fetches only the latin file, so on screen the Han characters come from a system face and measure wrong, and fontsourceProvider hands the PDF that latin file, which prints them as empty boxes. List the cjk kit block, call loadCjkFonts(FONTS, markdown) after loadFonts (once per voice, with the text it sets, when the book uses several CJK faces) and give renderToPdf fontProvider: cjkPdfProvider: both take the files that hold the text's characters. Chinese, Japanese and Korean fonts →

Credits

Text
Original prose, CC BY 4.0
Fonts
Noto Naskh Arabic (SIL OFL 1.1) · Noto Serif JP (SIL OFL 1.1)