# 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

- HTML version: https://postext.dev/en/docs/integration-marks
- Last updated: 2026-12-04
- Reading time: 8 min
- Other languages: [es](https://postext.dev/es/docs/integration-marks.md), [ca](https://postext.dev/ca/docs/integration-marks.md), [de](https://postext.dev/de/docs/integration-marks.md), [el](https://postext.dev/el/docs/integration-marks.md), [fr](https://postext.dev/fr/docs/integration-marks.md), [it](https://postext.dev/it/docs/integration-marks.md), [pt](https://postext.dev/pt/docs/integration-marks.md), [ru](https://postext.dev/ru/docs/integration-marks.md), [zh](https://postext.dev/zh/docs/integration-marks.md), [ja](https://postext.dev/ja/docs/integration-marks.md), [ar](https://postext.dev/ar/docs/integration-marks.md)

## 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.

```js
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

| Kind | Drawn as | Uses |
| --- | --- | --- |
| `highlight` | A translucent fill over each piece of the range (35 %, `highlightOpacity`). | `color` |
| `underline` | A line under the baseline; on a vertical page, a sideline on the right of the column. | `color` |
| `strike` | A line through the middle of the letters. | `color` |
| `replace` | The strike, and `text` written over the struck words on a paper-coloured backdrop. | `text`, `color` |
| `insert` | A caret under the line at `range.start`, and `text` over it. | `text`, `color` |
| `note` | The range tinted, and a flag in the margin with `label`, joined by a dashed leader. | `label`, `color` |
| `box` | An 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:

```js
{
  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`

```js
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
```

> **Runnable example: Postext · proofreading marks over a page image** — Lay highlight, replacement, strike, insertion, underline and note marks over a page image as SVG, and find the mark under a click. ([source](https://github.com/drnachio/postext/tree/main/docs/examples/marks-svg))

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`

```js
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](https://postext.dev/en/docs/integration-anchors.md)). The
[viewer](https://postext.dev/en/docs/integration-viewer.md) does this for you on `setDocument` and
`setAnnotations`.

## Examples in the Cookbook

### Annotation marks

- [Nº 213 Highlights from a list of ranges](https://postext.dev/en/cookbook/highlights-from-ranges.md)
- [Nº 214 Proofreading marks: strike, replace and insert](https://postext.dev/en/cookbook/proofreading-marks-on-pages.md)
- [Nº 215 Notes in the margin, with a popover from page coordinates](https://postext.dev/en/cookbook/margin-notes-with-popovers.md)
- [Nº 220 Marks as SVG over page images, for a report](https://postext.dev/en/cookbook/marks-as-svg-over-pages.md)
- [Nº 224 A reader's notebook: notes on what they select](https://postext.dev/en/cookbook/reader-notebook.md)
- [Nº 218 Applying accepted edits and carrying the other notes](https://postext.dev/en/cookbook/applying-accepted-edits.md)
- [Nº 219 Search results drawn on the pages](https://postext.dev/en/cookbook/search-results-on-pages.md)
- [Nº 223 A reader's highlighter: a passage and a colour](https://postext.dev/en/cookbook/reader-highlighter.md)
- [Nº 222 The same marks on right-to-left and vertical pages](https://postext.dev/en/cookbook/marks-on-rtl-and-vertical-pages.md)
- [Nº 217 Anchors through a revision: moved, ambiguous, orphaned](https://postext.dev/en/cookbook/anchors-through-a-revision.md)
- [Nº 216 Click on the page to find the source](https://postext.dev/en/cookbook/click-to-markdown-source.md)
- [Nº 221 Your own canvas: renderPage, rangeRects and caretAt](https://postext.dev/en/cookbook/own-canvas-page-view.md)
