Skip to main content

Chapter 24.3 · Part III · In practice

Integration: annotation marks

layoutAnnotations lays highlights, underlines, struck words, replacements between the lines, insertion carets, margin notes and boxes over the laid-out pages; paintAnnotations, annotationsSvg and annotationAt paint them and find them under a point

Updated 2026-12-048 minIn chapter 24 Integrationenescadeelfritptruzhjaar

In short

This page is for developers. It shows how to draw proofreading marks on the pages: highlights, underlines, crossed-out words, new words written above the line, a mark where text goes in, notes in the margin and boxes. The marks follow the lines, even on pages set right to left or in vertical columns. They can be drawn on a canvas or as an SVG picture placed over a page image. A click on a mark tells which one it is.

Seven kinds of mark, laid out in the text's own frame, painted on a canvas or written as SVG.

layoutAnnotations turns the host's annotations into drawing primitives on the sheet. It knows the lines, so a strike crosses the letters at the height of their middle, a replacement is written in the leading above the struck words, and a note's flag goes to the nearest margin with a dashed leader to its words.

import { layoutAnnotations, paintAnnotations } from 'postext/annotate';
 
const shapes = layoutAnnotations(doc, [
  { id: 'c1', kind: 'highlight', range: { start: 41, end: 57 } },
  { id: 'c2', kind: 'replace', range: { start: 87, end: 92 }, text: 'young bees' },
  { id: 'c3', kind: 'note', range: { start: 110, end: 115 }, label: '3' },
]);
paintAnnotations(ctx, shapes, { pageIndex: 0, scale: 2 });

#The kinds

KindDrawn asUses
highlightA translucent fill over each piece of the range (35 %, highlightOpacity).color
underlineA line under the baseline; on a vertical page, a sideline on the right of the column.color
strikeA line through the middle of the letters.color
replaceThe strike, and text written over the struck words on a paper-coloured backdrop.text, color
insertA caret under the line at range.start, and text over it.text, color
noteThe range tinted, and a flag in the margin with label, joined by a dashed leader.label, color
boxAn outline round each piece.color

An annotation is { id, kind, range, text?, label?, color?, chapter? }. The id is the host's, and comes back from annotationAt. The default colours (DEFAULT_ANNOTATION_COLORS) are each at least 3:1 against white paper and against dark paper (#1e1e1e). A host that colours marks by author or by state passes color.

How the marks set their own text:

  • Replacement and insertion text is sized from the marked text (0.62 of its em) and set in fontFamily (default system-ui, sans-serif). Two replacements on one line are pushed apart along it so they never overlap.
  • On a right-to-left page the text reads the right way round. On a vertical page it stands upright, one character at a time down the column.
  • Note flags stack along the margin without overlapping, and stay on the page. A label too long for the margin is cut short with an ellipsis; the full label stays on the annotation for the host to show elsewhere. noteSide: 'start' | 'end' picks the margin instead of the nearest one.
  • measure(text, font) replaces the browser's canvas measurement, for Node or a host with its own font metrics. paper is the colour behind labels (default white).

#The shapes

Each annotation gives one AnnotationShape per page it reaches:

{
  id: 'c2', kind: 'replace', pageIndex: 0, color: '#d6322b',
  primitives: [ /* polygon | line | text, in painting order */ ],
  hitAreas:   [ /* polygons on the sheet */ ],
  bounds:     { x, y, width, height },
}

The primitives are in page px on the sheet. A host can paint them with its own drawing code (WebGL, a PDF writer), or use one of the two painters.

#On a canvas: paintAnnotations

paintAnnotations(ctx, shapes, { pageIndex, scale }) paints the shapes of one page on a 2D context whose origin is the page's top-left corner. Paint the page first (renderPage, renderPageToCanvas), then the marks. The context is left as it was found.

#As SVG over an image: annotationsSvg

import { annotationsSvg } from 'postext/annotate';
 
const svg = annotationsSvg(shapes, doc.pages[0], { pageIndex: 0 });
overlay.innerHTML = svg; // over an <img> of the page, same box
Postext · proofreading marks over a page image
import { buildDocument, renderPage } from 'https://esm.sh/postext';
import { layoutAnnotations, annotationsSvg, annotationAt } from 'https://esm.sh/postext/annotate';
 
const paragraph = 'The keeper walked the hives at dawn, lifting each lid with care and reading the frames for brood, honey and the queen. A hive that hums low in the cold keeps its cluster tight; one that roars at noon wants more room before the flow.';
const markdown = ['# The apiary', paragraph, paragraph].join('\n\n');
const config = {
  page: { width: { value: 120, unit: 'mm' }, height: { value: 120, unit: 'mm' } },
  bodyText: { fontFamily: 'EB Garamond', fontSize: { value: 12, unit: 'pt' }, lineHeight: { value: 18, unit: 'pt' } },
};
await Promise.all([document.fonts.load('16px "EB Garamond"'), document.fonts.load('bold 16px "Open Sans"')]);
 
const doc = buildDocument({ markdown }, config);
const page = doc.pages[0];
const at = (words) => ({ start: markdown.indexOf(words), end: markdown.indexOf(words) + words.length });
 
// A page image, as a server or the command line would make it.
document.getElementById('page').src = renderPage(page, doc, { scale: 2 }).toDataURL();
 
const annotations = [
  { id: 'a', kind: 'highlight', range: at('walked the hives') },
  { id: 'b', kind: 'replace', range: at('brood'), text: 'young bees' },
  { id: 'c', kind: 'strike', range: at('low in the cold ') },
  // An insertion marks a point: the end of “cluster tight”.
  { id: 'd', kind: 'insert', range: { start: at('cluster tight').end, end: at('cluster tight').end }, text: 'and warm' },
  { id: 'e', kind: 'underline', range: at('roars at noon') },
  { id: 'f', kind: 'note', range: at('queen'), label: '1' },
];
const shapes = layoutAnnotations(doc, annotations);
const overlay = document.getElementById('marks');
overlay.innerHTML = annotationsSvg(shapes, page, { pageIndex: 0 });
 
// Hit-testing in page px: the mark painted on top first.
overlay.addEventListener('click', (e) => {
  const r = overlay.getBoundingClientRect();
  const ids = annotationAt(shapes, 0, (e.clientX - r.left) * (page.width / r.width), (e.clientY - r.top) * (page.height / r.height));
  const a = annotations.find((x) => x.id === ids[0]);
  document.getElementById('status').textContent = a ? `${a.kind} on “${markdown.slice(a.range.start, a.range.end)}”${a.text ? ` → “${a.text}”` : ''}` : 'No mark there.';
});
index.html
<figure id="sheet">
  <img id="page" alt="The first page of the chapter">
  <div id="marks"></div>
</figure>
<p id="status">Click a mark.</p>
style.css
body {
  margin: 0;
  padding: 16px;
  font-family: system-ui, sans-serif;
  background: #ecebe6;
  color: #1d1d1f;
}
/* The SVG has the page's viewBox: it scales with the image under it. */
#sheet {
  position: relative;
  width: min(100%, 480px);
  margin: 0;
  box-shadow: 0 2px 10px rgb(0 0 0 / 0.15);
}
#page {
  display: block;
  width: 100%;
  background: #fff;
}
#marks,
#marks svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
#marks g {
  cursor: pointer;
}

Loads an interactive editor from codepen.io. The example imports the latest postext release from a CDN.

The SVG has the page's size and a viewBox in page px, so it scales with the image under it. Each mark is a <g data-annotation="c2" data-kind="replace">, which CSS can style and events can find. This is the way to mark up pages the system already has as images: the command line's postext images, a page cache, a server that renders a page for an e-mail.

#Which mark is under a point: annotationAt

import { annotationAt } from 'postext/annotate';
 
const ids = annotationAt(shapes, pageIndex, x, y); // ['c3', 'c1']: the one on top first

Each mark is found by its hit areas: the pieces of its range, its line with a few px of slack, its label, its flag. A point over two marks gives both, in the order they were painted, top first.

#Keeping shapes up to date

Shapes belong to one layout. When the document is laid out again (a new text, another font size), lay the annotations out again, after resolving their anchors against the new text (see Anchors and edits). The viewer does this for you on setDocument and setAnnotations.

#Examples in the Cookbook

Recipes that use what this page describes, each under the section it suits best. A recipe shows its pages beside the whole script.