跳到主要内容
食谱编号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行代码

简单来说

一份地方史简报的两页,作为普通图片显示,上面画着编辑的修改:替换的词语、删去的词、高亮的人名、边注和插入的文字。页面下方是这些修改的编号列表。

成品一览

一份 Harwick 地方史简报的校样报告,不用查看器:一篇讲已停运电车线路的文章的两页,每页是一张 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 以两倍尺寸绘制页面,图片保留这张位图;任何页面图片都一样可用,比如 postext images 生成的 PNG 或页面缓存里的图片。annotationsSvg 把一页的标记写成以页面 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上的食谱文件夹 ↗ (在新标签页中打开)

变化

#把每页合成一张 PNG,用于电子邮件

很多邮件客户端不绘制内嵌 SVG;用 paintAnnotations(和 annotationsSvg 一起导入)把标记画在页面的 canvas 上,只发送一张图片。

-  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' });

致谢

文本
原创文字, CC BY 4.0
字体
Newsreader (SIL OFL 1.1) · Archivo (SIL OFL 1.1)