Rendering consumer
Let readers annotate content displayed by your own viewer.
Your plugin has its own view renderer, like Panels+, and you want to let readers annotate what it displays. This means that:
- Your viewer renders the book content.
- You tell Ink Away how your viewer maps that content to the original book pages.
- Ink Away draws and saves the annotations. You can also subscribe to its events.
What do you need?
Throughout this example, we'll be using these three objects:
| Object | Where it comes from |
|---|---|
ui | The reader UI, normally your plugin's self.ui. |
viewer_widget | The actual widget your plugin shows through KOReader. It is not a built-in field that every plugin has. |
surface | A table you create to describe your viewer's page and geometry. |
Example:
-- YourPlugin/main.lua -- illustrative mock, NOT an installable KOReader plugin.
-- Rendering consumer: handles both before-ready and after-ready registration.
-- A plugin may also be an event-only consumer; the roles are independent.
-- Your own viewer setup supplies ui, viewer_widget and a valid surface. Rendering and
-- surface geometry are deliberately omitted; see Plugin-integration.md.
local YourPlugin = {}
-- MOCK signature: your plugin creates its own viewer and surface.
-- viewer_widget is that actual KOReader viewer object, not a standard plugin field.
function YourPlugin:init(ui, viewer_widget, surface)
self.ui, self.viewer_widget, self.surface = ui, viewer_widget, surface
self.closed, self.annotation_open = false, false
self.status = "Waiting for Ink Away"
if ui.inkAwayIntegrationAPI then
-- Also works if Ink Away happened to finish first.
self:configureInkAway(ui.inkAwayIntegrationAPI)
else
-- Queue this consumer instance; do not replace another plugin's queue entries.
ui.inkAwayPendingIntegrations = ui.inkAwayPendingIntegrations or {}
ui.inkAwayPendingIntegrations[self] = true
end
end
function YourPlugin:configureInkAway(ink)
if self.closed or self.cleanup then return end
if ink.integration_api_version ~= 1 or ink.document_surface_version ~= 1
or type(ink.installPluginIntegration) ~= "function" then
self.status = "This Ink Away version does not support the integration"
return
end
-- Ink Away passes itself here; ui["ink-away"] may not exist yet.
-- One call installs a provider plus callbacks scoped to this surface.
self.cleanup = ink:installPluginIntegration(self.viewer_widget, self.surface, {
on_open = function(event)
self.annotation_open = true
self.status = "Annotating"
self.annotation_document = event.document
-- YourPlugin can disable its own navigation controls here.
end,
on_close = function(event)
self.last_annotation_document = event.document
self.annotation_open = false
self.annotation_document = nil
self.status = "Previewing"
-- Restore YourPlugin's controls; event.document identifies the book.
-- This event does not confirm that saving to disk succeeded.
end,
on_error = function(err)
self.last_error = tostring(err)
self.status = "Annotation integration error"
-- Display or log the error using YourPlugin's own UI.
end,
})
self.ink = ink
self.status = "Previewing"
end
-- Example viewer paint hook. The viewer must already be in the window stack
-- when annotation starts. Its background uses absolute screen coordinates.
function YourPlugin:paintTo(bb)
self:paintBackground(bb)
if not self.closed and self.ink and self.ui.inkAwayIntegrationAPI == self.ink then
self.ink:paintDocumentSurface(self.surface, bb)
end
end
-- Example handler for YourPlugin's "Annotate" button/menu action.
function YourPlugin:onAnnotate()
if self.closed or not self.ink or self.ui.inkAwayIntegrationAPI ~= self.ink then
self.status = "Ink Away is not ready"
return
end
-- Requests opening; on_open above confirms that the editor actually opened.
self.ink:onInkAwayAnnotate()
end
-- Called BEFORE destroying the viewer_widget or its surface resources.
function YourPlugin:onCloseWidget()
if self.closed then return end
self.closed = true
-- Closing before readiness must cancel setup, not leave a stale viewer_widget queued.
local pending = self.ui.inkAwayPendingIntegrations
if pending and pending[self] == true then
pending[self] = nil
if next(pending) == nil then self.ui.inkAwayPendingIntegrations = nil end
end
if self.cleanup then
-- Saves/closes this surface's editor, unregisters, and unsubscribes.
-- Its close callback remains connected until release completes.
self.cleanup()
end
self.cleanup, self.ink = nil, nil
self.annotation_open = false
self.status = "Closed"
end
function YourPlugin:paintBackground(_bb)
-- MOCK: YourPlugin's existing renderer paints its content here.
-- surface:snapshot(bb) must use background-only painting, not paintTo(bb),
-- otherwise the ink overlay would be included recursively in its snapshot.
end
return YourPluginBreaking down the previous example, step by step:
1. Describe what your viewer displays
A surface is the table that tells Ink Away what your viewer is displaying. External surfaces support fixed-page documents, like PDFs and comic archives.
You need version = 1, document = ui.document, and these seven methods.
They describe the original book pages, even when your viewer only shows a crop:
| Method | What you provide |
|---|---|
pages() | Visible native page numbers, such as {7}. |
key() | A string that changes when the page, crop, zoom, pan, or rotation changes. |
toPage(x, y) | Screen point → page, px, py, zoom, angle, or nil. |
toScreen(page, px, py) | Page point → x, y, zoom, angle, or nil. |
snapshot(bb) | Paint the background into the supplied screen-sized buffer, without Ink Away ink. |
repaint() | Request a repaint of your viewer. |
clip() | Content rectangle {x, y, w, h} in absolute screen pixels. |
For example, this surface describes one page with a uniform scale and no rotation.
layout contains the page number, screen position, content width/height, and positive
zoom. Keep it updated when your viewer changes.
paintBackground(bb) is your viewer's own rendering function, without the ink overlay.
Replace that call with the function your viewer actually uses.
local UIManager = require("ui/uimanager")
local layout = {page = 7, x = 40, y = 80, w = 520, h = 640, zoom = 1}
local surface = {version = 1, document = ui.document}
function surface:pages() return {layout.page} end
function surface:key()
return table.concat({layout.page, layout.x, layout.y,
layout.w, layout.h, layout.zoom}, ":")
end
function surface:toPage(x, y)
return layout.page, (x - layout.x) / layout.zoom,
(y - layout.y) / layout.zoom, layout.zoom, 0
end
function surface:toScreen(page, px, py)
if page ~= layout.page then return nil end
return layout.x + px * layout.zoom,
layout.y + py * layout.zoom, layout.zoom, 0
end
function surface:snapshot(bb)
viewer_widget:paintBackground(bb) -- your renderer, without the ink overlay
end
function surface:repaint()
UIManager:setDirty(viewer_widget, "ui")
end
function surface:clip()
return {x = layout.x, y = layout.y, w = layout.w, h = layout.h}
endtoPage() and toScreen() need to agree: one maps screen positions to book page
coordinates, and the other maps them back. zoom is positive, and angle is clockwise
rotation in radians.
Do not reject points just because they are outside clip(). Ink Away handles
clipping separately. For more details, see
the full surface contract.
2. Connect when your viewer is ready
Call self:connectInkAway(self.ui, viewer_widget, surface) from your own viewer
setup, after creating the surface:
function YourPlugin:connectInkAway(ui, viewer_widget, surface)
self.ui, self.viewer_widget, self.surface = ui, viewer_widget, surface
self.closed = false
if ui.inkAwayIntegrationAPI then
self:configureInkAway(ui.inkAwayIntegrationAPI)
else
ui.inkAwayPendingIntegrations = ui.inkAwayPendingIntegrations or {}
ui.inkAwayPendingIntegrations[self] = true
end
endconnectInkAway is a helper name used in this example. Call it from your own viewer
setup; it is not a KOReader lifecycle signature.
If Ink Away is already ready, your plugin configures the integration immediately.
If it loads later, it removes your queue entry and calls configureInkAway(ink).
You do not need to control plugin loading order. Your viewer must be on KOReader's
window stack when annotation opens.
3. Register your surface
function YourPlugin:configureInkAway(ink)
if self.closed or self.cleanup then return end
if ink.integration_api_version ~= 1
or ink.document_surface_version ~= 1 then return end
self.cleanup = ink:installPluginIntegration(self.viewer_widget, self.surface, {
on_open = function() self.annotation_open = true end,
on_close = function() self.annotation_open = false end,
on_error = function(err) self.last_error = tostring(err) end,
})
self.ink = ink
endinstallPluginIntegration() installs your rendering provider and the callbacks for
this surface in one call. Keep the returned cleanup function: you'll call it when
your viewer closes.
These callbacks belong to your surface. If you also want notifications for all annotation sessions on this reader, use the subscriptions from the event-only example.
4. Paint saved ink and open annotation
Your viewer still renders its own content. After painting that background in your paint handler, tell Ink Away to draw the saved annotations on top:
if self.ink and self.ui.inkAwayIntegrationAPI == self.ink then
self.ink:paintDocumentSurface(self.surface, bb)
endTo open annotation from your own button or menu, call self.ink:onInkAwayAnnotate()
when the same ready-instance check succeeds. This requests opening the editor;
on_open tells you when it actually opened.
surface:snapshot(bb) must only paint your background. Do not call the combined
background-and-ink paint handler from it, or the snapshot can recursively include
saved ink.
5. Disconnect before destroying the viewer
function YourPlugin:onCloseWidget()
if self.closed then return end
self.closed = true
local pending = self.ui.inkAwayPendingIntegrations
if pending then pending[self] = nil end
if self.cleanup then self.cleanup() end
self.cleanup, self.ink = nil, nil
endRemoving the queue entry cancels setup if Ink Away has not loaded yet. Calling
cleanup() releases your surface's active editor, then removes the provider and
listeners. Keep your viewer and surface usable until that function finishes.
If your plugin replaces viewers when turning pages, see the handoff rules under optional surface callbacks before adding that behavior.