Skip to content

Read and write a page's viewports without loading the page - #115

Merged
bobsingor merged 1 commit into
embedpdf/mainfrom
embedpdf/feature/raw-viewports
Nov 8, 2026
Merged

bobsingor merged 1 commit into
embedpdf/mainfrom
embedpdf/feature/raw-viewports

Conversation

@bobsingor

Copy link
Copy Markdown
Contributor

A page's measurement viewports (/VP) could only be read and written through a loaded FPDF_PAGE, and loading a page parses its whole content stream. The engine reads every page's viewports, so opening a 1,000-page document parsed 1,000 pages to read one dictionary entry each, and "apply this scale to all pages" parsed every page again to write it.

Now the viewport functions have raw twins, the way annotations have EPDFPage_GetAnnotRaw and friends. They take a document and a page index, and they never parse the page:

  • EPDFPage_CountViewportsRaw(doc, page_index) reads the size of /VP from the page dictionary. Nothing else is built, so a pass over every page skips the pages without viewports for the price of a dictionary lookup.
  • EPDFPage_GetViewportRaw and EPDFPage_AddViewportRaw return an EPDF_VIEWPORT hosted by a page built from its dictionary and never parsed, as EPDFPage_GetAnnotRaw builds one. Every EPDFViewport_*, EPDFMeasure_* and EPDFNumberFormat_* accessor works on it unchanged.
  • EPDFPage_RemoveViewportRaw removes an entry.
  • EPDFViewport_Close releases a raw viewport's host and every handle obtained through it (its measure and formats). The document's measure registry keeps the open hosts, so one left open is freed with the document. A viewport of a loaded page still belongs to its page; closing one does nothing.

Each raw function opens the document's view scope, so in a layer it reads the page in the layer, and a write promotes exactly the page, as through a loaded page. Raw handles and page handles share their generations (both key them by the page's object number), so a removal through either makes the other side's handles stale.

The four page functions and their twins share one body each, taking a CPDF_Page: a loaded page or the raw host. The page functions behave as before.

There is no raw FindViewport: the engine picks the viewport under a point itself.

The engine side moves viewports onto the page layout and reads them through these functions (plan: measurement viewports on the layout). Pin this there after release.

Tests

  • New embedder tests:
    • RawViewportsReadWhatTheLoadedPageReads: count, bbox, name and measure through a raw handle match the loaded page; bad page and entry indexes; closing a loaded page's viewport does nothing; a raw handle left open is freed with the document.
    • RawViewportWritesShareGenerationsWithLoadedPages: raw adds show on the loaded page; a raw removal makes raw and loaded handles stale, and a loaded removal makes raw handles stale.
    • RawLayerReadsPromoteNothingAndWritesOnlyPage: in a layer, raw reads promote nothing, a raw add promotes exactly the page, an earlier raw handle follows the page into the layer, and the delta reopens with both viewports.
    • BadArgumentsAndRejectedWrites covers the raw functions and EPDFViewport_Close(NULL).
  • epdf_c_api_test.c lists the five new exports.
  • Layer mode: the three new tests pass, with no layer-only failures, and are recorded as writers (5, 3, 5). In RawViewportsReadWhatTheLoadedPageReads the document read through raw handles leaves 0 objects; the 5 are the document Base() builds.
  • On macOS:
    • Embedder suite: 1,776 pass and 12 fail. They're the 12 that Draw a signature field's box #114 lists as failing without its change: progressive render, text render, RenderHelloWorldWithFlags and CaptionAppearanceGolden.
    • Unit tests weren't run; nothing they cover changed.
  • Linux CI hasn't run yet.

A page's measurement viewports (/VP) could only be reached through a
loaded FPDF_PAGE, and loading a page parses its whole content stream.
The engine reads every page's viewports, so a 1,000-page document parsed
1,000 pages to read one dictionary entry each.

The viewport functions now have raw twins, as annotations have
EPDFPage_GetAnnotRaw: EPDFPage_CountViewportsRaw, GetViewportRaw,
AddViewportRaw and RemoveViewportRaw take a document and a page index and
never parse the page. A raw viewport is hosted by a page built from its
dictionary, and every viewport, measure and number-format accessor works
on it unchanged. EPDFViewport_Close releases the host and every handle
obtained through it; one left open is freed with the document. Raw and
page handles share their generations, so a removal through either makes
the other side's handles stale. Each page function and its raw twin share
one body.
@bobsingor
bobsingor merged commit 6907dbb into embedpdf/main Nov 8, 2026
1 check passed
bobsingor added a commit to embedpdf/embed-pdf-viewer that referenced this pull request Nov 8, 2026
The release has the raw viewport functions (embedpdf/runtime#115) and the
upstream sync to 6688c576f (Nov 6 2026). The bindings generated from it
are the ones already committed.
lucapivato pushed a commit to iceteagroup/embed-pdf-viewer that referenced this pull request Nov 8, 2026
The fork branch embedpdf/feature/raw-viewports (embedpdf/runtime#115)
adds EPDFPage_CountViewportsRaw, GetViewportRaw, AddViewportRaw,
RemoveViewportRaw and EPDFViewport_Close. The runtime pin moves once that
release is out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant