[doc] Editor core: document model, exact undo, .pictor format, text layout, display list, tiny-skia render, bitmap tools, SVG import, PNG/JPG/SVG/PDF export; worker doc.open and doc.import_svg

This commit is contained in:
2026-11-17 10:07:12 +01:00
parent e46cbe3547
commit 17db54fdbf
26 changed files with 6589 additions and 7 deletions
+58 -1
View File
@@ -63,6 +63,61 @@ XObjects, inline images, clipping, shadings, Type 3 fonts), and any gap leaves t
(`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
@@ -75,7 +130,7 @@ and decompressed), and a raw byte grep.
```
Error kinds: `needs_password`, `wrong_password`, `damaged`, `unsupported`, `too_large`, `codec_missing`, `invalid`,
`io`, `exists`, `not_open`, `cannot_reach_target`, `engine_missing`; the client adds `timed_out`,
`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 |
@@ -98,6 +153,8 @@ Error kinds: `needs_password`, `wrong_password`, `damaged`, `unsupported`, `too_
| `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` |