Ink Away
Integration APIReference

The surface

The table that tells Ink Away where the book's page is on your viewer's screen.

A surface is a Lua table you write. You pass it to installPluginIntegration, and Ink Away uses it to place ink on your view and to save what is drawn there as ink on the book's own pages.

The rendering example shows a small working surface. This page is the full contract.

Surface contract

local surface = {
    version = 1,
    document = ui.document, -- the open document itself, not just the same file
}

Screen coordinates are absolute pixels. Page coordinates are the document page's own units.

Required methodAcceptsReturns or does
pages()NothingThe document pages shown, in display order.
key()NothingA string that changes whenever where a page sits on the screen changes. Ink Away caches the placed ink by it, so keep it exact and cheap.
toPage(x, y)Screen pointpage, px, py, zoom, angle, or nil.
toScreen(page, px, py)Page pointx, y, zoom, angle, or nil when that page is not shown.
clip()NothingThe page's part of the screen, {x, y, w, h}, without your controls or margins. Ink is drawn and painted only there.
snapshot(bb)Screen-sized bufferPaint your view into bb without the ink. Annotation mode shows it under the canvas.
repaint()NothingAsk for your view to be painted again.
  • zoom is screen pixels per page unit and must be positive. angle is clockwise, in radians. Both mappings must agree.
  • Keep the mappings valid outside clip(): clipping is separate from mapping.
  • Never call paintDocumentSurface from snapshot, or the ink is painted into its own background.
  • Never keep or free the buffer passed to snapshot.

Optional callbacks

A basic viewer can leave these out. Add the ones your viewer needs. Any you supply must be functions.

MethodWhat it doesWhen left out
begin()Annotation mode is opening on this surface. Hide your controls here.Nothing extra happens.
finish()Annotation mode has closed on it. Show your controls again. It must cope with your viewer already being closed.Nothing extra happens.
turn(dir, done, frame)Turn the page: 1 forward, -1 back. Call done(next_surface) once it has settled. Pass the new surface when the page's surface changed, or nothing to stay on this one. Call frame() after each animation step.Page turns stay on the page.
readingDirection()"ltr" or "rtl". On a right-to-left page, the toolbar's left arrow goes forward.The reader's setting.
setViewport(rect, change)The room annotation mode leaves beside its toolbar. Return true if your page moved. Keep change for later: see below.Your layout stays as it is.
regions()The parts of the page, each a surface of its own with its own clip(), such as the two halves of a spread with the gutter removed.The whole surface is painted as one.
mapPath(points, to_page, page) and isAffine()For a page one transform cannot map: map a flat {x1, y1, x2, y2, ...} path point by point (to_page true maps screen to page), and return false from isAffine(). A shape that crosses the fold is kept as a polygon.One transform maps everything.

Page turns

Ink Away saves what is in hand before asking you to turn, and drawing waits until you call done. Keep the old mappings working while the page moves. The surface you pass to done must belong to the same document. An invalid one is reported to your on_error, and annotation mode stays on the old surface.

Moving the page while annotating

Before you move or resize the page yourself, call the latest change with a function that does the move: change(function() ... end). Ink Away keeps what is in hand against the old layout, runs your function, then reloads against the new one. Don't call change from inside setViewport, and drop it in finish().

Known limits

  • Only fixed-page books (PDF, comics, DjVu). A surface cannot map reflowing books such as EPUB.
  • KOReader's own highlights aren't drawn on your page, and the eraser and highlight colour sheet don't reach them there.
  • While your page animates, annotation mode shows its picture and takes no input.

On this page