# Integration: text on the page

> rangeRects, caretRect, caretAt, wordAt, blockAt and wordRangeAt: where a range of the Markdown is painted on the laid-out pages, and which offset, word or block lies under a point, on justified, right-to-left, vertical and composed pages

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

## In short

This page is for developers. It shows how to find where some words of the text are drawn on a page, as boxes you can paint over. It also shows the other way round: which letter or word is under the point someone clicked. Both work on pages set right to left, in vertical columns and in turned frames. The numbers are positions in the Markdown text, so they match what the system stores.

**From a range of the Markdown to boxes on the page, and from a point on the page back to the Markdown.**

These calls read a laid-out document (a `VDTDocument` from `buildDocument`
or from the [layout worker](https://postext.dev/en/docs/configuration-programmatic-usage.md)) and
place text with the same code the canvas, HTML and PDF renderers paint it
with: justified spaces, hyphens added at line ends, bidirectional lines,
right-to-left and vertical pages, drop caps, opener band titles, composed
frames turned or scaled, comic balloons.

Two units run through all of them:

- **Offsets** are UTF-16 indices into the chapter's Markdown file, front matter counted, as `sourceAt` and every `sourceStart` of the VDT count them. A range is `{ start, end }`, end excluded.
- **Coordinates** are page px on the sheet, from its top-left corner (the sheet includes the bleed when the page has cut lines). Multiply by the scale you painted the page at.

## Where a range lies: `rangeRects`

```js
import { buildDocument, renderPage, rangeRects } from 'postext';

const markdown = '# The apiary\n\nThe keeper walked the hives at dawn, lifting each lid with care.';
const doc = buildDocument({ markdown }, config);

const start = markdown.indexOf('the hives');
const pieces = rangeRects(doc, { start, end: start + 'the hives at dawn'.length });
// [{ pageIndex: 0, blockId: 'b2', rect: { x, y, width, height }, quad: [p0, p1, p2, p3],
//    baseline: [left, right], upright: true, fontSize: 13.3 }, …]
```

A range gives one piece per run of a line: a range that breaks across two
lines gives two pieces, across a page break two pieces on two pages. Each
piece has:

| Field | What it is |
| --- | --- |
| `pageIndex` | The page it is on. |
| `blockId` | The block it is in (a paragraph, a heading, a table cell's block, a balloon). |
| `rect` | Its axis-aligned bounds on the sheet: enough for a highlight on an upright page. |
| `quad` | Its four corners in the line's own order (top-left, top-right, bottom-right, bottom-left as the line runs). On a vertical page or in a turned frame it is not a rectangle of the sheet: draw the polygon. |
| `baseline` | The baseline under the piece, from its start to its end. An underline follows it. |
| `upright` | Set when `rect` is the piece exactly. |
| `fontSize` | The text's em in px, for marks that set text of their own. |

To highlight a selection on a page painted with `renderPage`:

```js
const scale = 2;
const canvas = renderPage(doc.pages[0], doc, { scale });
const ctx = canvas.getContext('2d');
ctx.setTransform(scale, 0, 0, scale, 0, 0);
ctx.fillStyle = 'rgb(47 111 214 / 0.3)';
for (const piece of pieces.filter((p) => p.pageIndex === 0)) {
  ctx.beginPath();
  piece.quad.forEach((p, i) => (i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y)));
  ctx.fill();
}
```

> **Runnable example: Postext · the word under a click** — Click a word on a page painted with renderPageToCanvas: wordAt gives its Markdown range and rangeRects highlights it. ([source](https://github.com/drnachio/postext/tree/main/docs/examples/geometry-highlight))

Callouts, figures and other blocks that are not running text give no pieces;
a range that covers them gives the pieces of the text around them. Text
hidden under an opener band (the heading the band repeats) is left out, and
the band's title gives the pieces instead.

## A caret: `caretRect`

`caretRect(doc, offset)` gives the line of a caret at a character boundary:
`{ pageIndex, blockId, from, to }`, from the top of the text line to its
foot, in the line's frame. On a vertical page the caret lies across the
column. It returns `null` for an offset that falls in no painted text.

## What is under a point: `caretAt`, `wordAt`, `blockAt`

```js
canvas.addEventListener('click', (e) => {
  const r = canvas.getBoundingClientRect();
  const x = (e.clientX - r.left) * (doc.pages[0].width / r.width);
  const y = (e.clientY - r.top) * (doc.pages[0].height / r.height);

  const hit = caretAt(doc, 0, x, y);
  // { offset: 57, pageIndex: 0, blockId: 'b2' }, or null off the text
  const word = wordAt(doc, 0, x, y, markdown);
  // { start: 52, end: 59, pageIndex: 0, blockId: 'b2' }
  const block = blockAt(doc, 0, x, y);
  // { start: 14, end: 83, pageIndex: 0, blockId: 'b2' }: the paragraph's whole range
});
```

- `caretAt` gives the character boundary nearest the point, like a click in a text editor. It finds the nearest line of the nearest block, so a click in the gutter between two lines still lands on one of them. On a composed page the frame painted on top wins.
- `wordAt` widens that offset to the word around it, with `Intl.Segmenter` in the document's language (Chinese and Japanese words included), and leaves out Markdown emphasis marks at the word's edges. It takes the Markdown, or a function `(chapter) => markdown` for text a story carried in from another chapter.
- `blockAt` gives the block and its source range: a paragraph to comment on as a whole.
- `wordRangeAt(markdown, offset, { locale })` does the word step on the Markdown alone, without a page.

## Chapters and books

A [book](https://postext.dev/en/docs/sandbox-books-saving.md) is laid out one chapter at a time,
and a page can carry text that a story brought in from another chapter
(`VDTBlock.sourceChapter`). The options say which Markdown the offsets count
in:

```js
rangeRects(doc, range, { chapter: 3, ownChapter: 2 });
caretAt(doc, page, x, y, { ownChapter: 2 }); // → { offset, chapter: 3 } on carried text
```

- `ownChapter` is the position of the chapter `doc` lays out. A `chapter` equal to it, or none, means the document's own file.
- `pages: { from, to }` limits the search to a run of pages, for a document stitched from several chapters.

`caretAt` and `wordAt` set `chapter` in their answer when the text under the
point belongs to another chapter.

## Writing modes and frames

Nothing changes in the calls. On a right-to-left page the quad runs from
right to left, so `quad[0]` is the right end of the piece. On a vertical
page (`layout.writingMode: 'vertical-rl'`) a piece is a column segment: its
quad runs top to bottom and its "top" is its right side, so an underline
along `quad[0]`–`quad[1]` is the sideline Chinese and Japanese use. In a
composed frame turned by 30° the quad is turned by 30°.

## The pieces underneath

`postext/geometry` exports what these calls are built from, for a host that
draws its own caret or hit-tests its own way: `placeLineSegments` (each
segment of a line where it is painted), `sourceToPlainIndex` and
`plainRangeInLine` (Markdown offsets against the painted text),
`blockFrameMatrix` and `intoBlockSpace` (a composed frame's matrix),
`bandLineBoxes` (an opener band's title), `sourceHitAt`. The Sandbox's own
canvas preview uses them.

## Examples in the Cookbook

### Text on the page

- [Nº 212 Select on the page, read the Markdown range and text](https://postext.dev/en/cookbook/selection-as-markdown-range.md)
- [Nº 215 Notes in the margin, with a popover from page coordinates](https://postext.dev/en/cookbook/margin-notes-with-popovers.md)
- [Nº 219 Search results drawn on the pages](https://postext.dev/en/cookbook/search-results-on-pages.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)
- [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º 224 A reader's notebook: notes on what they select](https://postext.dev/en/cookbook/reader-notebook.md)
- [Nº 223 A reader's highlighter: a passage and a colour](https://postext.dev/en/cookbook/reader-highlighter.md)
