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

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

ページ画像に重ねる SVG の注釈:報告書のために

layoutAnnotations と annotationsSvg で、校正者の印をビューアーなしでページ画像に重ねる。CMS がメールで送ったり印刷したりできる校正報告書。

このページの内容
レベル
中級
Postext
Postext 1.103.1で検証済み
1.103.0以上が必要
ライセンス
2026年12月4日更新
コードMIT · 本文CC BY 4.0
PNG のページ画像に SVG で印を重ねた校正レポート:差し替え、問い合わせ付きのハイライト、消した語、メモ、挿入。ページの下に一覧がある。
PNG のページ画像に SVG で印を重ねた校正レポート:差し替え、問い合わせ付きのハイライト、消した語、メモ、挿入。ページの下に一覧がある。
  • 英語の見本:日本語版はまだありません
  • 仕上がり125 × 180 mm
  • 1段
  • Newsreader 9.5/13.5
  • Archivo
  • 2ページ
  • レベル
  • Postext 1.103.1
  • 組版時間57 ms
  • コード139行

かんたんな説明

郷土史会報の2ページをただの画像として表示し、その上に編集者の直しを描きます。差し替えた語句、消した語、確認のための強調、欄外のメモ、挿入です。ページの下には同じ直しの番号付きリストがあります。

できあがり

Harwick 郷土史会報の校正報告書です。ビューアーは使いません。廃止された路面電車の路線についての記事の2ページを、それぞれ PNG として表示し、その上に校正者が残した印を重ねます。語句の差し替え、語の削除、確認のための人名の強調、車両番号への欄外メモ、そして挿入です。ページの下には番号付きのリストがあり、各印のページと対象の語を示します。コンテンツ管理システムは同じ HTML をサーバーで組み立ててメールで送るか、コメント付きの PDF として印刷します。

このレシピが答える質問

  • ビューアーを使わずに、報告書やメール用のページ画像に印を付けるには?

手短な答え

script.js · 138〜149行コード全体で見る
const shapes = layoutAnnotations(doc, marks, { fontFamily: 'Archivo' });
const sheets = doc.pages.map((page, pageIndex) => {
  const figure = document.createElement('figure');
  const image = new Image(); // a PNG, as a page cache or `postext images` would keep it
  image.src = renderPage(page, doc, { scale: 2 }).toDataURL('image/png');
  image.alt = t({ en: `Page ${pageIndex + 1}`, es: `Página ${pageIndex + 1}` });
  // The SVG has the page's size in page px and a viewBox: it scales with the image.
  figure.innerHTML = annotationsSvg(shapes, page, { pageIndex });
  figure.prepend(image);
  return figure;
});
document.getElementById('sheets').replaceChildren(...sheets);

材料

種類
Newsreader, Archivo (SIL OFL 1.1)
素材
なし:画像はすべてコードで描画

作り方

#1 · 本と同じページを組む

script.js · 31〜50行コード全体で見る
const opener = { enabled: true, minHeight: mm(34), slot: { elements: [
  { kind: 'text', id: 'kicker', content: '{title}', fontFamily: 'Archivo', fontSize: pt(7),
    fontWeight: 600, letterSpacing: pt(1.4), textTransform: 'uppercase', color: col('accent'),
    placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: mm(4) } } },
  { kind: 'text', id: 'title', content: '{titleText}', fontFamily: 'Newsreader', fontSize: pt(22),
    lineHeight: 1.1, fontWeight: 600, color: col('ink'), align: 'left', overflow: 'wrap',
    placement: { anchor: { to: '#kicker', edge: 'below' }, offset: { y: mm(2.5) },
      size: { width: mm(88) } } },
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1), color: col('accent'),
    placement: { anchor: { to: '#title', edge: 'below' }, offset: { y: mm(3) },
      size: { width: mm(10) } } },
] } };
const head = { kind: 'text', pages: 'body', fontFamily: 'Archivo', fontSize: pt(6.5),
  fontWeight: 500, letterSpacing: pt(1), textTransform: 'uppercase', color: col('muted') };
const header = { elements: [
  { ...head, id: 'title', content: '{title}', placement: { anchor: { to: 'page', edge: 'top-left' },
    offset: { x: mm(16), y: mm(10) } } },
  { ...head, id: 'folio', content: '{pageNumber}', fontWeight: 600, color: col('accent'),
    placement: { anchor: { to: 'page', edge: 'top-right' }, offset: { x: mm(-16), y: mm(10) } } },
] };

報告書は本物のページを見せるので、会報は自分のデザインを保ちます。会報の名前を柱見出しにした短い扉と、ノンブル付きの柱です。設定の中には印についての記述は何もありません。

#2 · 印を Markdown の範囲として保存する

script.js · 111〜134行コード全体で見る
// Ranges are offsets into the Markdown; `at` finds a phrase that occurs once in the text.
const at = (words) => {
  const start = markdown.indexOf(words);
  return { start, end: start + words.length };
};
const point = (words) => ({ start: at(words).end, end: at(words).end });
const marks = t({
  en: [
    { id: 'm1', kind: 'replace', range: at('two and a half'), text: 'three' },
    { id: 'm2', kind: 'highlight', range: at('Edith Pearce'), comment: 'Spelling: Pierce?' },
    { id: 'm3', kind: 'strike', range: at('early ') },
    { id: 'm4', kind: 'note', range: at('number 9'), label: '1',
      comment: 'The council minutes say car 12.' },
    { id: 'm5', kind: 'insert', range: point('Bridge Street'), text: 'and Mill Lane' },
  ],
  es: [
    { id: 'm1', kind: 'replace', range: at('cuatro kilómetros'), text: 'cinco' },
    { id: 'm2', kind: 'highlight', range: at('Edith Pearce'), comment: '¿Se escribe Pierce?' },
    { id: 'm3', kind: 'strike', range: at('primeros ') },
    { id: 'm4', kind: 'note', range: at('número 9'), label: '1',
      comment: 'Las actas del ayuntamiento dicen coche 12.' },
    { id: 'm5', kind: 'insert', range: point('calle del Puente'), text: 'y del Molino' },
  ],
});

印はそれぞれサイト側のレコードです。id、種類、Markdown の中の位置の範囲を持ち、差し替えや挿入には新しい文字列、メモにはラベルが付きます。挿入は一点を示すので、範囲の始まりと終わりは同じ位置です。comment フィールドはサイトのもので、layoutAnnotations は読まず、ページの下のリストがそれを表示します。

#3 · 各画像に同じ大きさの SVG を重ねる

コードは上の手短な答えです。renderPage はページを2倍の大きさで描き、画像はそのビットマップを保ちます。postext images で作った PNG やページのキャッシュなど、どんなページ画像でも同じように使えます。annotationsSvg は1ページ分の印を、ページ px 単位で viewBox 付きの SVG として書き出すので、両方が同じ箱を満たせば画像と一緒に拡大縮小します。各印は <g data-annotation="m4"> で、リストから指し示すことも、CSS でスタイルを付けることもできます。各種類の説明は注釈のマークの文書にあります。

レシピの全体

// ═══ Postext Cookbook · Nº 220 · Marks as SVG over page images ═════════════════════════
// https://postext.dev/en/cookbook/marks-as-svg-over-pages
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Newsreader, Archivo (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// A proof report with no viewer: each page of a local history bulletin as a plain image, the
// editor's marks laid over it as SVG, and the list of marks under the pages. A content system
// could send the same HTML in an e-mail or print it to a PDF of comments.
import { buildDocumentWithFonts, renderPage, renderPageToCanvas } from 'https://esm.sh/postext';
import { layoutAnnotations, annotationsSvg } from 'https://esm.sh/postext/annotate';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'marks-as-svg-over-pages';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#1d1f22', // text
  accent: '#2d5e57', // a tram-livery green: kicker, rule, folios
  muted: '#676c70', // running heads
};
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 LEAD = 13.5; // pt of body leading

// #region bulletin: a society bulletin's page, a short opener and a running head
const opener = { enabled: true, minHeight: mm(34), slot: { elements: [
  { kind: 'text', id: 'kicker', content: '{title}', fontFamily: 'Archivo', fontSize: pt(7),
    fontWeight: 600, letterSpacing: pt(1.4), textTransform: 'uppercase', color: col('accent'),
    placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: mm(4) } } },
  { kind: 'text', id: 'title', content: '{titleText}', fontFamily: 'Newsreader', fontSize: pt(22),
    lineHeight: 1.1, fontWeight: 600, color: col('ink'), align: 'left', overflow: 'wrap',
    placement: { anchor: { to: '#kicker', edge: 'below' }, offset: { y: mm(2.5) },
      size: { width: mm(88) } } },
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(1), color: col('accent'),
    placement: { anchor: { to: '#title', edge: 'below' }, offset: { y: mm(3) },
      size: { width: mm(10) } } },
] } };
const head = { kind: 'text', pages: 'body', fontFamily: 'Archivo', fontSize: pt(6.5),
  fontWeight: 500, letterSpacing: pt(1), textTransform: 'uppercase', color: col('muted') };
const header = { elements: [
  { ...head, id: 'title', content: '{title}', placement: { anchor: { to: 'page', edge: 'top-left' },
    offset: { x: mm(16), y: mm(10) } } },
  { ...head, id: 'folio', content: '{pageNumber}', fontWeight: 600, color: col('accent'),

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

アレンジ

#メール用に各ページを1枚の PNG にまとめる

多くのメールソフトは埋め込みの SVG を描きません。paintAnnotations(annotationsSvg と一緒に読み込む)でページの canvas に印を描き、1枚の画像として送ります。

-  image.src = renderPage(page, doc, { scale: 2 }).toDataURL('image/png');
+  const canvas = renderPage(page, doc, { scale: 2 });
+  const ctx = canvas.getContext('2d');
+  paintAnnotations(ctx, shapes, { pageIndex, scale: 2 });
+  image.src = canvas.toDataURL('image/png'); // the page and its marks in one bitmap
-  figure.innerHTML = annotationsSvg(shapes, page, { pageIndex });

#メモをすべて外側の余白に置く

メモの旗は初期設定では近いほうの余白に置かれます。noteSide: 'end' にすると、左から右へ読むこのページでは、すべて右の余白に置かれます。

-const shapes = layoutAnnotations(doc, marks, { fontFamily: 'Archivo' });
+const shapes = layoutAnnotations(doc, marks, { fontFamily: 'Archivo', noteSide: 'end' });

クレジット

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