169 lines
14 KiB
Markdown
169 lines
14 KiB
Markdown
# Formats and the worker protocol
|
||
|
||
What Pictor's core reads and writes, which library does each PDF operation, and how the app talks to
|
||
`pictor-worker`. Version 1 (Day 1, core v0.1.0). The `.pictor` document format arrives on Day 3 and gets its own
|
||
section here.
|
||
|
||
## Images (crates/raster, crates/meta)
|
||
|
||
| Format | Read | Write | Metadata strip |
|
||
| :-- | :-- | :-- | :-- |
|
||
| PNG | yes | yes | container-level (eXIf, tEXt, zTXt, iTXt, tIME dropped); pixels identical |
|
||
| JPEG | yes, EXIF orientation applied | yes (quality 88 default; alpha onto white) | container-level (APP1 Exif/XMP, APP13 IPTC, COM, other APPn dropped; JFIF, ICC, Adobe kept); orientation ≠ 1 kept as a 36-byte EXIF; pixels identical |
|
||
| WebP | yes | lossless only (the image crate's encoder) | EXIF and XMP chunks dropped, VP8X flags fixed |
|
||
| GIF | yes (first frame for conversion) | single frame | block-level (comments, XMP dropped; every frame and the loop kept) |
|
||
| TIFF | yes | yes | re-encoded losslessly (uncompressed; ICC and orientation are lost: a known gap); multi-page refused |
|
||
| BMP | yes | yes | (no metadata) |
|
||
| ICO | yes | 256/48/32/16 px | — |
|
||
| HEIC/HEIF | Windows only, through WIC with the codecs Windows has; never bundled | no | convert to JPG first |
|
||
| AVIF | recognised; decoding needs the AV1 extension (reported as missing) | no | — |
|
||
|
||
Formats are recognised by their content (magic bytes), never by the extension. Caps (`pictor_raster::Limits`):
|
||
128 million pixels, 512 MiB input, 1 GiB decoded; the pixel count is checked from the header before decoding.
|
||
|
||
HEIC needs two Store extensions: "HEIF Image Extensions" (free, `9PMMSR1CGPWG`) and an HEVC decoder ("HEVC Video
|
||
Extensions", `9NMZLZ57R3T7`, US$0.99; checked 2026-11-16). `heif_support()` says which one is missing.
|
||
|
||
## PDF (crates/pdf)
|
||
|
||
Coordinates between the app and the core are in **view space**: points (1/72 inch) from the top-left corner of the
|
||
page as displayed (the crop box, `/Rotate` applied). `geometry::PageFrame` converts to PDF user space.
|
||
|
||
| Operation | Library | Why |
|
||
| :-- | :-- | :-- |
|
||
| open, page sizes, render, text, search, outline | pdfium | what Chrome and Edge use: a file Edge opens, Pictor opens |
|
||
| reorder, rotate, delete, insert blank, extract, split | lopdf, in place | keeps the form, bookmarks and metadata; deleted pages and their annotations and fields really leave the file (pdfium's save keeps unreferenced objects) |
|
||
| merge | pdfium page import | handles inherited resources across files; merged forms keep their look but are no longer fillable; bookmarks are not carried over |
|
||
| images → PDF | lopdf | a JPEG is embedded byte for byte (no re-encoding); others are stored losslessly with an alpha mask |
|
||
| PDF → PNG/JPG | pdfium | rendering |
|
||
| form fill | lopdf (`/V`, `/AS` with each checkbox's own on-state) + `NeedAppearances`, then pdfium builds the appearances while rendering with the form | the appearance comes from the same engine as Edge's, Czech letters included; viewers that ignore the flag show the value too |
|
||
| annotations: highlight, underline, strike, note, free text, ink, rectangle, ellipse, line/arrow | lopdf, each with its own appearance stream | looks the same in every viewer; free text uses Helvetica with WinAnsi plus the Central European letters through `/Differences` |
|
||
| visual signature (stamp) | lopdf: the image goes into the page content, the old content wrapped in `q … Q` | it is part of the page, not a movable annotation |
|
||
| flatten | pdfium `FPDFPage_Flatten`, then lopdf drops the form and orphans | |
|
||
| password on | lopdf, AES-256 (V5/R6), no permission restrictions | pdfium cannot write encryption; qpdf was not needed |
|
||
| password off | lopdf decrypting load + plain save | |
|
||
| true redaction | pdfium render + lopdf rebuild (below) | |
|
||
| compress presets (Day 2) | lopdf: images capped at the largest page side × the preset's dpi and re-encoded as JPEG (E-mail 120 → 96 → 72 dpi until ≤ 5 MB, Web 150, Print 300); Flate everywhere; object streams | text, fonts and vectors untouched, so text stays extractable; fonts are not subset (gap) |
|
||
| PDF to Word (Day 2) | pdfium characters (box, size, weight, slant) → lines, segments, tables, paragraphs, headings; images at their place; docx-rs writes the .docx | "Layout is approximate"; spaces come from the gaps between letters (measured against the line's own letter spacing), never from pdfium's invented whitespace, so PDFs without space glyphs keep whole words; glyphs without a Unicode mapping stay U+FFFD; scanned pages are reported, and OCR text from Fundamentum's Lector can be passed in |
|
||
|
||
### True redaction
|
||
|
||
Each redacted page is rendered by pdfium at 200 dpi (annotations and form fields included), the boxes are painted
|
||
black into the pixels, and the page is rebuilt as that one JPEG image (same object number, displayed size kept,
|
||
`/Rotate 0`). The page's old annotations go, fields that lived only there go, resources inherited from the page
|
||
tree are pushed down to the pages that use them, and the document loses `/Info`, every `/Metadata` and
|
||
`/PieceInfo`, the struct tree (its ActualText can hold text), bookmarks, attachments, document JavaScript and
|
||
non-destination open actions. Hidden layers (`/OCProperties`) go when no remaining page uses them; otherwise they
|
||
stay and the result carries a warning naming those pages. Then every unreferenced object is pruned and the file is
|
||
written fresh (no incremental history). The other pages' objects are written back byte for byte.
|
||
|
||
Cost: the text of a redacted page is no longer selectable or searchable. A content-stream rewrite (cutting out only
|
||
what is under the box) was not used: it needs a full content-stream interpreter (fonts and glyph positions, form
|
||
XObjects, inline images, clipping, shadings, Type 3 fonts), and any gap leaves text behind. The tests
|
||
(`crates/pdf/tests/redaction.rs`) check pdfium text extraction, a lopdf scan of every string, name and stream (raw
|
||
and decompressed), and a raw byte grep.
|
||
|
||
## The .pictor document (crates/doc), format 1
|
||
|
||
A `.pictor` file is a zip:
|
||
|
||
| Entry | What |
|
||
| :-- | :-- |
|
||
| `document.json` | the document (below), compressed |
|
||
| `assets/<id>.png` | the pixels of each bitmap layer and placed picture, RGBA, stored (PNG is already compressed) |
|
||
| `fonts/<sha256>.ttf` / `.otf` | the fonts the document uses whose licence allows embedding (OS/2 `fsType` installable or editable) |
|
||
|
||
`document.json` (every length in points, 1/72 in; a document made from an image has 1 pt = 1 px):
|
||
|
||
```
|
||
{ "format": 1, "next_id": 12,
|
||
"pages": [ { "id": 1, "width": 595.276, "height": 841.89, "margins": [t, r, b, l], "guides": [{"vertical": true, "at": 297.6}],
|
||
"background": [255, 255, 255, 255] | null,
|
||
"layers": [ { "id": 2, "name": "Layer 1", "visible": true, "locked": false, "opacity": 1,
|
||
"kind": "vector", "objects": [ … ] }
|
||
| { …, "kind": "bitmap", "asset": "b3", "x": 0, "y": 0, "w": 800, "h": 600 }
|
||
| { …, "kind": "group", "layers": [ … ] } ] } ],
|
||
"assets": [ { "id": "b3", "width": 800, "height": 600 } ],
|
||
"fonts": [ { "family": "Georgia", "weight": 700, "italic": false, "file": "fonts/<sha256>.ttf" } ] }
|
||
```
|
||
|
||
An object is `{ "id", "name", "transform": [a, b, c, d, e, f], "opacity", "visible", "locked", "fill", "stroke",
|
||
"shape" }` with `shape.type` one of `rect {w, h, radius}`, `ellipse {w, h}`, `line {x1, y1, x2, y2, arrow_start,
|
||
arrow_end}`, `path {d}` (absolute or relative M L H V C Q Z; no arcs), `text {w, text, style}` (style: `family,
|
||
weight, italic, size, color, align` left / center / right / justify, `line_height` as a multiple of the size,
|
||
`letter_spacing` in points), `image {asset, w, h, crop {x, y, w, h}}` (the whole picture's place inside the frame)
|
||
or `group {objects}`. A paint is `{"type": "solid", "color": [r, g, b, a]}`, `linear {x1, y1, x2, y2, stops}` or
|
||
`radial {cx, cy, r, stops}` with coordinates as fractions of the shape's box; a stroke is `{paint, width, dash, cap,
|
||
join}`. Colours are sRGB with straight alpha.
|
||
|
||
**Reading is strict** (`format::read`, `Limits`): at most 4096 entries; every entry name relative, without `..`,
|
||
`\`, `:` or empty parts, no directories or links (refused even when unused); entries are read by known name, never
|
||
extracted; `document.json` at most 64 MB and any entry 256 MB, the whole 1 GB, and no entry over 1 MB may expand
|
||
more than 1000×; the declared size must be the real one; `format` is read first and a newer one is refused
|
||
("made by a newer Pictor"); ids unique and below `next_id`, numbers finite, pages 1–14 400 pt, opacities 0–1, paths
|
||
parsed, nesting at most 32 deep; every picture at most 100 MP (400 MP together), decoded with its declared size as
|
||
the limit, and every asset a layer or object refers to must be present. A font must parse.
|
||
|
||
**Undo** is an operation log (`ops::Op`, `ops::History`): insert / remove / set a page, layer or object, replace an
|
||
object, patch an asset's pixels, put an asset, a batch. Applying an operation returns its exact inverse.
|
||
|
||
**Rendering** goes through one display list (`display::build`): fills, strokes, images (with a clip), glyph runs
|
||
(glyph outlines in font units, defined once) and opacity groups. The core paints it with tiny-skia; the app's
|
||
canvas painter follows the same rules; the PDF and SVG exports walk it too.
|
||
|
||
**PDF export** (`export::pdf`): pdf-writer; vectors as paths, gradients as shading patterns (a gradient's stops
|
||
share one alpha, their average), opacity as ExtGState alpha or an isolated transparency group, pictures as
|
||
FlateDecode RGB with an SMask, text as Type 0 fonts (Identity-H, CIDs are the subset's glyph ids, CIDToGIDMap
|
||
Identity) with a ToUnicode map and widths, so the layout's positions are kept with TJ adjustments; a font whose
|
||
`fsType` is restricted or bitmap-only is refused with its name and the reason; fonts that forbid subsetting are
|
||
embedded whole. Each page's MediaBox is its exact size.
|
||
|
||
## The worker protocol (crates/worker), version 1
|
||
|
||
`pictor-worker.exe` reads one JSON object per line on stdin and answers one per line on stdout. Its first line is
|
||
`{"hello": "pictor-worker", "protocol": 1, "version": "0.1.0"}`; the client refuses another protocol.
|
||
|
||
```
|
||
→ {"id": 7, "op": "pdf.render", "args": {"doc": 1, "page": 0, "width": 1200}}
|
||
← {"id": 7, "ok": {"png": "<base64>", "width": 1200, "height": 1553}}
|
||
← {"id": 7, "err": {"kind": "needs_password", "message": "This PDF is password-protected. Enter its password to open it."}}
|
||
```
|
||
|
||
Error kinds: `needs_password`, `wrong_password`, `damaged`, `unsupported`, `too_large`, `codec_missing`, `invalid`,
|
||
`io`, `exists`, `not_open`, `newer` (a .pictor from a newer Pictor), `cannot_reach_target`, `engine_missing`; the client adds `timed_out`,
|
||
`out_of_memory`, `crashed` and `spawn`. `message` is a plain sentence for the user.
|
||
|
||
| Operation | Arguments | Result |
|
||
| :-- | :-- | :-- |
|
||
| `pdf.open` | `path`, `password?` | `doc` (a session id), `info` (pages with sizes and labels, outline, `has_form`) |
|
||
| `pdf.close` | `doc` | — |
|
||
| `pdf.render` | `doc`, `page`, `width` (px) | `png` (base64), `width`, `height` |
|
||
| `pdf.text` | `doc` | `pages`: text per page |
|
||
| `pdf.search` | `doc`, `query` | `hits`: `{page, rects}` in view space (max 2000) |
|
||
| `pdf.form` | `doc` | `fields`: name, kind, value, options, page, rect, read_only, multiline |
|
||
| `pdf.save` | `doc`, `edits` (see `pictor_pdf::Edits`), `output`, `mode` (`new` / `overwrite`) | `path`, `bytes`, `bak?`, `warnings` |
|
||
| `pdf.extract` | `doc`, `pages`, `output` | `path` |
|
||
| `pdf.compress` | `doc`, `preset` (`email` ≤ 5 MB / `web` / `print`), `output` | `path`, `before`, `after`, `images_changed`, `warnings` |
|
||
| `pdf.to_word` | `doc`, `output` (.docx), `ocr?` ({page: text} for scanned pages) | `path`, `pages`, `scanned_pages` |
|
||
| `pdf.page_png` | `doc`, `page`, `dpi`, `output` | `path` (a page for OCR) |
|
||
| `pdf.to_images` | `doc`, `pages`, `dpi`, `format`, `output` (with `{n}`) | `paths` |
|
||
| `pdf.merge` | `inputs` [{`path`, `password?`}], `output` | `path` |
|
||
| `pdf.split` | `path`, `password?`, `ranges` ("1-3, 5, 8-"), `output` (with `{n}`) | `paths` |
|
||
| `pdf.from_images` | `inputs`, `output`, `fit` (`image`/`a4`/`letter`), `margin` | `path` |
|
||
| `image.probe` | `path` | `format`, `width`, `height`, `has_alpha`, `frames`, `bytes` |
|
||
| `image.thumb` | `path`, `max` | `png`, original `width`, `height` |
|
||
| `image.convert` | `input`, `output`, `format`, `quality?`, `crop?`, `rotate?`, `resize?`, `preset?`, `target_bytes?`, `overwrite?` | `path`, `bytes`, `format`, `width`, `height` |
|
||
| `doc.open` | `input`, `output` | `path`, `pages`, `format` (a clean .pictor of the worker's making at `output`) |
|
||
| `doc.import_svg` | `input`, `output` | `path`, `width`, `height` (the SVG as a one-page .pictor; text as outlines) |
|
||
| `image.heif_support` | — | `support` |
|
||
| `meta.read` | `path` | `report` (items by group), `groups` (with labels) |
|
||
| `meta.strip` | `input`, `output`, `overwrite?` | `path`, `removed` |
|
||
| `qr.render` | `payload`, `level`, `style`, `logo?` (a path) | `png`, `svg`, `level_used`, `size`, `text` |
|
||
|
||
Saving: `mode: new` refuses to replace a file (the app picks `<name>-edited.pdf`); `overwrite` writes
|
||
`<file>.bak` first, then replaces the file in one rename (`crates/save`).
|
||
|
||
Caps: files over 1 GB are refused; the client runs the worker in a Job Object (2 GB by default, killed with the
|
||
app) and gives each request a time cap; a worker that hangs or dies is replaced on the next request (open documents
|
||
must then be opened again).
|