本文へスキップ
レシピ番号222

レシピ集 · 第13章 · CMSへの組み込み

右から左のページと縦組みのページに同じ印

右から左に組むアラビア語のページと縦組みの日本語のページに、同じ書き方で印を付ける。layoutAnnotations が各ページの行に沿わせる。

このページの内容
レベル
上級
Postext
Postext 1.103.1で検証済み
1.103.0以上が必要
ライセンス
2026年12月4日更新
コードMIT · 本文CC BY 4.0
右から左に組んだアラビア語のページと縦組みの日本語のページに付けた同じ四つの印。それぞれの断片と、その進む向きが一覧になっている。
右から左に組んだアラビア語のページと縦組みの日本語のページに付けた同じ四つの印。それぞれの断片と、その進む向きが一覧になっている。
  • 英語の見本:日本語版はまだありません
  • 仕上がり120 × 170 mm
  • 1段
  • Noto Naskh Arabic 12.5/21
  • Noto Serif JP
  • 2ページ
  • レベル
  • Postext 1.103.1
  • 組版時間43 ms
  • コード142行

かんたんな説明

アラビア語のページと日本語のページを並べ、どちらにも同じ四つの直しを入れます。強調、消した語、差し替えた語、メモです。各ページの下のリストは、印がいくつの片に分かれ、どちらへ向かうかを示します。

できあがり

2ページを並べ、それぞれページビューアーに載せます。製本工房についてのアラビア語のページは Noto Naskh Arabic で右から左に、時計の修理職人についての文庫判の日本語のページは Noto Serif JP で縦組みにします。どちらのページにも、校閲ツールが残すのと同じ四つの印があります。強調、削除、差し替え、番号付きのメモです。アラビア語のページでは、差し替えの文字列が消した語の上に右から左へ読めるように置かれます。日本語のページでは、消し線が行の中央を縦に通り、差し替えの文字列はその横に縦に立ちます。各ページの下のリストは、印ごとの片の数と、最初の片の向きを示します。

このレシピが答える質問

  • 右から左に書くページや縦組みのページで、テキストに印を付けるには?

手短な答え

script.js · 132〜145行コード全体で見る
// 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);

材料

種類
Noto Naskh Arabic, Noto Serif JP (SIL OFL 1.1)
素材
なし:画像はすべてコードで描画

作り方

#1 · ページごとに書字方向を決める

script.js · 39〜65行コード全体で見る
// '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) },
  ] },
};

日本語のページが設定そのもので、writingMode: 'vertical-rl' を持ちます。アラビア語のページはそれを広げ、言語と判型と書体を変えます。locale: 'ar' で本文は右から左に組まれます。この後の印のコードは、どちらのページに描いているかを一度も尋ねません。

#2 · 両方のページの印を同じ形で書く

script.js · 94〜105行コード全体で見る
// 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, ['七十八歳', '一つずつ', ['丸一日', '二日'], '洗浄液']);

印は Markdown の範囲なので、アラビア語と日本語のリストは、それぞれ四つの語句から同じ関数で作れます。差し替えの文字列はそのページの言語で書き、フォントはその文字も読み込みます。読み込まないと、ブラウザーは代替フォントで表示してしまいます。

#3 · 印を配置し、各ページの上に描く

コードは上の手短な答えです。layoutAnnotations はページ自身の行から各印の位置を決めます。縦組みのページでは消し線が行の中央を通り、行末をまたぐ強調は二つの片になり、メモの旗は地の側へ行きます。ビューアーはページを描き、そのレイヤーは用紙 px 単位のコンテキストを受け取ります。形と同じ単位なので、paintAnnotations はそのまま描けます。各種類の説明は注釈のマークの文書にあります。

#4 · 文字の進む向きを読む

script.js · 149〜163行コード全体で見る
// 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 は行の区間ごとに一つの片を返し、その quad は行の順序を保ちます。quad[0] から quad[1] へは、アラビア語のページでは左へ、日本語のページでは下へ向かいます。独自の下線やコメントのピンを描くサイトは、言語から推し量るのではなく、ここから向きを取ります。

レシピの全体

// ═══ 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,

組み立てたscript.jsはそのまま動きます。任意のページのモジュールスクリプトに貼り付けるか、レシピをCodePenで開いてください。 GitHub上のレシピのフォルダー ↗ (新しいタブで開きます)

アレンジ

#印の配置をビューアーに任せる

ビューアーの annotations オプションは同じ配置と描画を行い、さらに各印にスクリーンリーダー用の名前を付け、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
   });

#縦組みの小説を一冊組む

ルビ付きの縦組み文庫 は、同じ判型に日本語の本を組みます。ルビ、字詰めのグリッド、小口側の柱が付きます。

よくあるつまずき

つまずき

アラビア語の本のタグはLANGではなく'ar'にする

レシピの版はenとesですが、アラビア語のサンプルはどちらの版でもアラビア語です。`locale: LANG`では左から右に組まれ、左綴じになり、ページ番号は1 2 3となり、図のキャプションにはFigureやFiguraが付きます。postext 1.30以降、このタグはテキストを組む言語(計測、描画、埋め込みに使う言語)でもあるので、英語やスペイン語のタグでは、Markazi Textのようなフォントがギュメ(« »)や終止符をアラビア語の字形で描かず、改行の位置も変わります。タグは自分で書いてください。'ar'(アラビア・インド数字、マシュリク地域の慣習)、'ar-EG'や'ar-SA'のような地域付きのタグ、ヨーロッパ式数字を使うマグレブ版なら'ar-MA'、'ar-DZ'、'ar-TN'です。アラビア語を引用するだけのラテン文字のページは、自分のロケールのままにします。 右から左のテキスト →

つまずき

日本語のテキストのタグは'ja'。zh-HansやLANGにしない

レシピの版はenとesですが、日本語のサンプルはどちらの版でも日本語です。`locale: LANG`では英語やスペイン語とタグ付けされ、中国語のタグでは中国語の規則で組まれます(開明式の約物、小書きの仮名が行頭に来てもよい、図の代わりに图、PDFでは中国の字形)。postext 1.30以降、このタグはテキストを組む言語(計測、描画、埋め込みに使う言語)でもあるので、'ja'以外のタグでは日本語の字形ではなく別の字形(中国語のタグでは中国の字形)になり、改行の位置も変わります。'ja'と書いてください。日本の地域(JLReqの改行と約物、ゴマの圏点、ルビの間隔、図と表の表示名)が選ばれ、ハイフネーションが切られます。リントは、zhやkoのタグで仮名を含むテキストをエラーにします。 日本語の改行(禁則処理) →

つまずき

アラビア語の書体にはarabicブロックを通じてarabicファイルが必要

FontsourceはAmiri、Noto Naskh Arabic、Scheherazade Newをサブセットごとに1ファイルとして配信し、アラビア文字はarabicファイルに入っています。loadFontsが取得するのはlatin(とlatin-ext)だけなので、画面ではアラビア文字がシステムの書体で出て計測が狂い、fontsourceProviderはPDFにそのlatinファイルを渡すため、空の四角が印字されます。キットのarabicブロックを挙げ、loadFontsのあとにloadArabicFonts(FONTS, markdown)を呼び(ページでアラビア文字に使うすべてのウェイトをFONTSに挙げておきます)、renderToPdfにfontProvider: arabicPdfProviderを渡してください。 アラビア語のフォント →

つまずき

中国語の書体はcjkブロックを通じてスライス単位で読み込む

Fontsourceは中国語、日本語、韓国語のファミリーを、ウェイトごとに約100個のファイルとして配信し、各ファイルが文字の範囲を受け持ちます。loadFontsが取得するのはlatinファイルだけなので、画面では漢字がシステムの書体で出て計測が狂い、fontsourceProviderはPDFにそのlatinファイルを渡すため、漢字が空の四角で印字されます。キットのcjkブロックを挙げ、loadFontsのあとにloadCjkFonts(FONTS, markdown)を呼び(本で複数のCJK書体を使うときは、それぞれの書体で組むテキストを渡して書体ごとに1回)、renderToPdfにfontProvider: cjkPdfProviderを渡してください。どちらもテキストの文字を含むファイルを取得します。 中国語・日本語・韓国語のフォント →

クレジット

レシピ
Ignacio Ferro
本文
書き下ろしの文章, CC BY 4.0
フォント
Noto Naskh Arabic (SIL OFL 1.1) · Noto Serif JP (SIL OFL 1.1)