mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 18:04:48 +00:00
Preserve ODF lookahead and stacking normalization together
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
[lints]
|
||||
cargo.unused_workspace_dependencies = "allow"
|
||||
+4
-1
@@ -90,7 +90,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
|
||||
| `crates/psd/tests/fixtures/atn/*.atn` (seven synthetic action sets, encoded field by field; `crates/psd/tests/fixtures/atn/README.md`) | PhotoCraft contributors | [PhotoCraft `crates/psd/tests/fixtures/atn`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/psd/tests/fixtures/atn) | MIT OR Apache-2.0 |
|
||||
| `crates/lottie/tests/fixtures/` | EffectCraft contributors (original work: four hand-written Lottie JSON documents, written from the public Lottie format description; not exported from any Adobe product or third-party file) | [EffectCraft `crates/lottie/tests/fixtures`](https://github.com/storytold/effectcraft/tree/e18f48fb/crates/lottie/tests/fixtures) | MIT OR Apache-2.0 |
|
||||
| `crates/pkcs7/tests/data/` | PdfCraft contributors (original work: throw-away test keys and certificates made with OpenSSL 3 for these tests, as seven PKCS #12 files with the password `test` and two PEM certificates; they protect nothing; commands in `crates/pkcs7/tests/data/README.md`) | [PdfCraft `crates/sign/tests/data`](https://github.com/storytold/pdfcraft/tree/1d7a9f9f/crates/sign/tests/data) | MIT OR Apache-2.0 |
|
||||
| `crates/affinity/tests/support/corpus.tsv`, `crates/affinity/tests/support/affinity3-signatures.txt` (manifest and measured signatures only; the 51 documents are not bundled, `scripts/fetch-affinity-corpus.sh` downloads them outside the checkout) | The authors of each document: samuel-etver (vector-art), Seth A. Robinson (Patchy fixtures), Nick Beeuwsaert (AFDesignLoad), Jeremy Cantu (Branding), eviltwo (AssetStoreTemplate), Square (leakcanary) | each file's pinned upstream URL and SHA-256 in the manifest; list carried over from [VectorCraft `xtask/src/affinity_corpus.rs`](https://github.com/storytold/vectorcraft/blob/6e873b806c9d5b3f2b55f651df57bd97933b579a/xtask/src/affinity_corpus.rs) | CC0-1.0, MIT or Apache-2.0 (per file, in the manifest) |
|
||||
| `crates/affinity/tests/support/corpus.tsv`, `crates/affinity/tests/support/affinity3-signatures.txt` (manifest and measured signatures only; the 113 files, 90 of them documents, are not bundled, `scripts/fetch-affinity-corpus.sh` downloads them outside the checkout) | The authors of each document: samuel-etver (vector-art), satoshoe-dev (affinity-samples), Seth A. Robinson (Patchy fixtures), Nick Beeuwsaert (AFDesignLoad), Jeremy Cantu (Branding), eviltwo (AssetStoreTemplate), Square (leakcanary) | each file's pinned upstream URL and SHA-256 in the manifest; every name from [VectorCraft `xtask/src/affinity_corpus.rs`](https://github.com/storytold/vectorcraft/blob/bada8e17103db9917de44e40a1cead5a7fc71c3c/xtask/src/affinity_corpus.rs) and [PhotoCraft `xtask/affinity-corpus.sha256`](https://github.com/storytold/photocraft/blob/629751503165118d5fec5f70e64fe2d6592f7b53/xtask/affinity-corpus.sha256) | CC0-1.0, MIT or Apache-2.0 (per file, in the manifest) |
|
||||
| `crates/raw/docs/coverage/adobe-cameras.tsv` | Facts (maker, model, extension, profile, minimum Camera Raw version) parsed by LightCraft contributors from Adobe's published list | [Adobe Camera Raw supported cameras](https://helpx.adobe.com/camera-raw/desktop/dng-and-file-formats/camera-raw-plug-supported-cameras.html), Wayback capture 20261003133011; moved from [LightCraft `docs/raw`](https://github.com/storytold/lightcraft/tree/5ff567a4a2e067bef1b6bce152c11e7f727ccbaf/docs/raw) | Facts, not copyrightable expression; the file is MIT OR Apache-2.0 |
|
||||
| `crates/raw/docs/coverage/{coverage,coverage-sweep,evidence,usage-weights}.tsv`, `coverage.py` | LightCraft contributors | [LightCraft `docs/raw`](https://github.com/storytold/lightcraft/tree/5ff567a4a2e067bef1b6bce152c11e7f727ccbaf/docs/raw) | MIT OR Apache-2.0 |
|
||||
| `crates/raw/docs/raf-corpus.sha256` (hashes only; the files are not bundled) | The photographers who contributed each file to raw.pixls.us | [raw.pixls.us](https://raw.pixls.us/) | CC0-1.0 |
|
||||
@@ -104,6 +104,9 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
|
||||
`crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained.
|
||||
The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate.
|
||||
|
||||
## Source-timed MIDI compatibility
|
||||
|
||||
`crates/midi/src/source_timed.rs` is extracted from LoopCraft `crates/midi/src/lib.rs` at `884e67c24b9b68badf6a4a4f7d62492f3673d354` (MIT OR Apache-2.0). The recorded source history for the extracted file is Dylan M. Taylor (`0d150c1f`, `440bc7ed`, `9e50523d`); Git authorship is preserved. It includes the original hostile-file, SMPTE, timing and round-trip tests. UTF-8 truncation and event-budget suffix repairs are subsequent changes. The module is an application-independent source-timed model; it does not depend on LoopCraft.
|
||||
## Non-seeking WAV primitives
|
||||
|
||||
`crates/audio/src/wav_stream.rs` is extracted from AniCraft `crates/audio/src/wav.rs` at `d875a7feb250e6ab06aa28a37fb78a0699e88330` (MIT OR Apache-2.0). Tanish Manku is retained as the primary import author, from original commit `30c121d269a49d9abe54a3834ed4aa0201542535`. Odd-data padding accounting, shared API documentation and regression tests are subsequent Dylan M. Taylor changes.
|
||||
|
||||
Generated
+697
-18
File diff suppressed because it is too large
Load Diff
@@ -26,6 +26,7 @@ craft-plist = { path = "crates/plist" }
|
||||
craft-platform = { path = "crates/platform" }
|
||||
craft-aep = { path = "crates/aep" }
|
||||
craft-affinity = { path = "crates/affinity" }
|
||||
craft-ai = { path = "crates/ai" }
|
||||
craft-ai-core = { path = "crates/ai-core" }
|
||||
craft-anicraft-format = { path = "crates/anicraft-format" }
|
||||
craft-pdf = { path = "crates/pdf", version = "=0.1.0" }
|
||||
@@ -48,6 +49,7 @@ craft-gzip = { path = "crates/gzip" }
|
||||
craft-gridcraft-format = { path = "crates/gridcraft-format" }
|
||||
craft-aaf = { path = "crates/aaf" }
|
||||
craft-atomic = { path = "crates/atomic" }
|
||||
craft-xtask = { path = "crates/xtask" }
|
||||
craft-captions = { path = "crates/captions" }
|
||||
craft-cfb = { path = "crates/cfb" }
|
||||
craft-codepage = { path = "crates/codepage" }
|
||||
@@ -142,6 +144,7 @@ craft-pcraft-bundle = { path = "crates/pcraft-bundle" }
|
||||
craft-pkcs7 = { path = "crates/pkcs7" }
|
||||
craft-plotter = { path = "crates/plotter" }
|
||||
craft-prepress = { path = "crates/prepress" }
|
||||
craft-press-print = { path = "crates/press-print" }
|
||||
craft-print = { path = "crates/print" }
|
||||
craft-proof = { path = "crates/proof" }
|
||||
craft-raster-codecs = { path = "crates/raster-codecs" }
|
||||
@@ -163,6 +166,7 @@ craft-stencilcraft-format = { path = "crates/stencilcraft-format" }
|
||||
craft-stencilcraft-document = { path = "crates/stencilcraft-document" }
|
||||
craft-svg-paths = { path = "crates/svg-paths" }
|
||||
craft-stream-codecs = { path = "crates/stream-codecs" }
|
||||
craft-testkit = { path = "crates/testkit" }
|
||||
craft-text = { path = "crates/text" }
|
||||
craft-tcraft = { path = "crates/tcraft" }
|
||||
craft-texture = { path = "crates/texture" }
|
||||
@@ -210,6 +214,9 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] }
|
||||
unsafe_code = "forbid"
|
||||
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(craft_pdf_unicode_fixtures)"] }
|
||||
|
||||
[workspace.lints.cargo]
|
||||
unused_workspace_dependencies = "allow"
|
||||
|
||||
[workspace.lints.clippy]
|
||||
cognitive_complexity = "deny"
|
||||
too_many_arguments = "deny"
|
||||
|
||||
@@ -254,11 +254,10 @@ applications retain their own document-font selection and licensing policy.
|
||||
linesweeper
|
||||
|
||||
linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in
|
||||
vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs and
|
||||
docs/sweep.typ. Its manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018
|
||||
Raph Levien". See vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for
|
||||
provenance, the local patches
|
||||
and the files left out.
|
||||
vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs,
|
||||
src/curve/mod.rs, src/curve/line.rs and docs/sweep.typ. Its manifest names Joe Neeman as author, and
|
||||
its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See vendor/linesweeper/README.craft.md and
|
||||
vendor/linesweeper/SOURCE.json for provenance, the local patches and the files left out.
|
||||
|
||||
Platform lifecycle source provenance
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
- [`craft-assuan`](crates/assuan/README.md): the Assuan protocol and gpg-agent's operations (key lookup, signing a digest, decrypting a session key) over any `Read + Write` stream, with a bounded canonical S-expression reader. No sockets; everything the agent sends is capped.
|
||||
- [`craft-affinity`](crates/affinity/README.md): bounded reader for native Affinity documents (`.af`, `.afdesign`, `.afphoto`, `.afpub`): archive, object stream and a neutral document model with layers, artboards, vectors, text, tables, images and masks. Read only; mapping onto an app's document stays in the app.
|
||||
- [`craft-atomic`](crates/atomic/README.md): Crash-safe atomic file writing and replacement with same-directory staging, directory fsync, Apple `F_FULLFSYNC` fallback, transient lock backoff, and fault injection for the Craft suite.
|
||||
- [`craft-xtask`](crates/xtask/README.md): a library for the apps' `cargo xtask` binaries: the gate's step runner (steps in order, stop at the first failure, the ok, FAIL and skip summary, workspace tests that exclude the running xtask package), an optional slot lock held across processes and an optional per-step JSON report. Each app keeps its step list and its own policy. Planned for DesignCraft and LightCraft.
|
||||
- [`craft-captions`](crates/captions/README.md): caption and subtitle files behind one neutral
|
||||
model (cues with exact tick times): SubRip, WebVTT, Scenarist SCC, MacCaption MCC, EBU STL and
|
||||
TTML / IMSC1 / DFXP readers and writers, and CEA-608 / CEA-708 byte-pair coding. Carved from
|
||||
@@ -286,9 +287,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
stay in each app.
|
||||
- [`craft-mesh`](crates/mesh/README.md): 3D mesh file formats behind one neutral model (meshes
|
||||
of polygons with per-corner indices, materials, a node hierarchy): STL, OBJ with MTL, PLY,
|
||||
glTF 2.0 / GLB and 3MF, one feature each. Readers are bounded by `Limits`, never open a file themselves
|
||||
glTF 2.0 / GLB and FBX, one feature each. Readers are bounded by `Limits`, never open a file themselves
|
||||
and never panic. Carved from EffectCraft, SolidCraft, TextureCraft and MaterialCraft.
|
||||
glTF 2.0 / GLB, FBX and 3MF, one feature each. Readers are bounded by `Limits`, never open a file
|
||||
themselves and never panic. Carved from EffectCraft, SolidCraft, TextureCraft and MaterialCraft.
|
||||
- [`craft-midi`](crates/midi/README.md): Standard MIDI Files: a bounded reader for formats 0, 1 and
|
||||
2 and a format-1 writer, over a neutral model of notes and channel events in ticks at 960 per
|
||||
quarter note. From SoundCraft.
|
||||
@@ -326,6 +326,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
- [`craft-pptx`](crates/pptx/README.md): PresentationML, the model of a `.pptx` presentation:
|
||||
masters, layouts, slides, shapes, placeholders and their inheritance, transitions and animations,
|
||||
comments, sections, media and kept parts. From DeckCraft. DrawingML comes from `craft-drawingml`.
|
||||
- [`craft-press-print`](crates/press-print/README.md): press-accurate printing: a PDF page separated
|
||||
into plates by `craft-prepress`, composited into the printer's RGB profile and printed through
|
||||
`craft-print`'s RGB transport, application-managed.
|
||||
- [`craft-print`](crates/print/README.md): PDF submission to CUPS and Windows, printer discovery,
|
||||
duplex/copies/colour and driver preferences. PDF generation stays in each app.
|
||||
- [`craft-proof`](crates/proof/README.md): proofing per language (spelling, suggestions, grammar,
|
||||
@@ -364,6 +367,13 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
check for drawing a tree with resvg. Apps take usvg from here, not as their own dependency.
|
||||
- [`craft-svg-paths`](crates/svg-paths/src/lib.rs): the former name of `craft-svg`'s `paths`,
|
||||
`color` and `write`; re-exports them for existing users.
|
||||
- [`craft-testkit`](crates/testkit/README.md): test support for the apps' own tests. Its `guards` module
|
||||
reports source patterns that broke platforms before: muda's predefined Quit item, WGSL float literals
|
||||
out of range, and `std::time::Instant` in code the web build runs. Its `deadline` module fails a test
|
||||
whose body runs past a limit, or whose condition does not hold in time; a variable the caller names
|
||||
replaces each limit. Its `hostile` module runs a parser on every truncation and on seeded damaged
|
||||
copies of a valid input, and fails the test with the case that panicked or did not finish. Not
|
||||
published.
|
||||
- [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds
|
||||
hyphenation points, with a bundled US English dictionary and patterns and optional hypher patterns
|
||||
for 47 more languages (one cargo feature each).
|
||||
@@ -371,6 +381,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
nearest and bilinear sampling that repeats or clamps, resizing and display conversion. Sizes are
|
||||
bounded by `Limits`. From MaterialCraft.
|
||||
- [`craft-ui`](crates/ui/README.md): SVG catalogs, icon buttons, tabs, pane controls, transactional docking, print settings and theme helpers for egui frontends;
|
||||
- [`craft-ai`](crates/ai/README.md) *(spec)*: AI provider abstraction — BYO API keys (OpenAI-compatible) and one-click curated local models with capability probing, resumable checksummed downloads, and document segmentation that protects markup while translating prose;
|
||||
assets, themes, localization and command handling stay in each app. Requires Rust 1.95 or later.
|
||||
- [`craft-vcard`](crates/vcard/README.md): vCard contacts, from MailCraft: vCard 2.1, 3.0 and 4.0 read
|
||||
(legacy character sets, quoted-printable, labels and Apple label groups, `KEY` certificates and
|
||||
|
||||
+17
-10
@@ -115,11 +115,15 @@ text-frame fixture.
|
||||
against the limits below: grids of 16 million cells, grids and stories shared by many layers,
|
||||
200 000 paragraphs, a document embedded hundreds of times, compounds that fan out or contain
|
||||
themselves, lists of a million null slots linked many times.
|
||||
* `scripts/fetch-affinity-corpus.sh` fetches 51 public CC0/MIT/Apache-2.0 documents at pinned
|
||||
commits, SHA-256 verified against `tests/support/corpus.tsv` (which names each file's source and
|
||||
licence), with the upstream licence notices. Of these, 33 are current `.af`: four CC0 vector
|
||||
illustrations and 28 original Affinity 3.2.3 feature probes plus one separately identified
|
||||
derived regression input from [SethRobinson/Patchy](https://github.com/SethRobinson/Patchy)
|
||||
* `scripts/fetch-affinity-corpus.sh` fetches 113 files at pinned commits, SHA-256 verified
|
||||
against `tests/support/corpus.tsv` (which names each file's source and license): 90 public
|
||||
CC0/MIT/Apache-2.0 documents, the 21 PNG files published with the
|
||||
[satoshoe-dev/affinity-samples](https://github.com/satoshoe-dev/affinity-samples) documents, and
|
||||
two upstream license notices. The manifest holds every name from the PhotoCraft and VectorCraft
|
||||
corpus lists, so 21 files are fetched under two names. Of the documents, 55 are current `.af`:
|
||||
four CC0 vector illustrations (each under two names), 18 CC0 Affinity 3.1.0 samples from
|
||||
satoshoe-dev/affinity-samples, and 28 original Affinity 3.2.3 feature probes plus one separately
|
||||
identified derived regression input from [SethRobinson/Patchy](https://github.com/SethRobinson/Patchy)
|
||||
(MIT). Only document data and provenance notices were inspected, never that project's importer
|
||||
or authoring scripts. The files are never committed.
|
||||
|
||||
@@ -129,11 +133,14 @@ text-frame fixture.
|
||||
```
|
||||
|
||||
Without `AFFINITY_CORPUS` the corpus tests are skipped. `AFFINITY_SAMPLES` names any other
|
||||
folder of documents to parse.
|
||||
* `tests/it/real_files.rs` parses every file and freezes the modelled features of all 33 current
|
||||
`.af` files (`tests/support/affinity3-signatures.txt`: counts, fonts, artboards, warnings and a
|
||||
CRC of the whole model). Native raster tests compare decoded pixels with independent embedded
|
||||
images; text tests assert Unicode run boundaries, fallback fonts and feature retention.
|
||||
folder of documents to parse. A corpus folder fetched for an older manifest fails the tests
|
||||
until the script is run on it again.
|
||||
* `tests/it/real_files.rs` checks the shape of every manifest row (four fields, a SHA-256, a
|
||||
relative name listed once, a license and an `https` URL), parses every file and freezes the
|
||||
modeled features of all 55 `.af` files (`tests/support/affinity3-signatures.txt`: counts,
|
||||
fonts, artboards, warnings and a CRC of the whole model). Native raster tests compare decoded
|
||||
pixels with independent embedded images; text tests assert Unicode run boundaries, fallback
|
||||
fonts and feature retention.
|
||||
* `fuzz/` has `cargo-fuzz` targets for the whole reader (`container`) and the object stream
|
||||
(`stream`):
|
||||
|
||||
|
||||
@@ -22,8 +22,55 @@ fn files(dir: &Path, out: &mut Vec<PathBuf>) {
|
||||
}
|
||||
|
||||
fn samples() -> Option<PathBuf> {
|
||||
let pinned = corpus::pinned();
|
||||
std::env::var_os("AFFINITY_SAMPLES").map(PathBuf::from).or(pinned)
|
||||
folder(std::env::var_os("AFFINITY_SAMPLES"), corpus::pinned)
|
||||
}
|
||||
|
||||
/// The `AFFINITY_SAMPLES` folder when one is given, without reading the pinned corpus; otherwise
|
||||
/// the pinned corpus.
|
||||
fn folder(samples: Option<std::ffi::OsString>, pinned: impl FnOnce() -> Option<PathBuf>) -> Option<PathBuf> {
|
||||
samples.map(PathBuf::from).or_else(pinned)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_samples_folder_is_used_without_the_pinned_corpus() {
|
||||
assert_eq!(folder(Some("samples".into()), || panic!("the pinned corpus was read")), Some(PathBuf::from("samples")));
|
||||
assert_eq!(folder(None, || Some(PathBuf::from("pinned"))), Some(PathBuf::from("pinned")));
|
||||
assert_eq!(folder(None, || None), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pinned_manifest_is_well_formed() {
|
||||
let rows = corpus::rows(corpus::MANIFEST).unwrap();
|
||||
assert!(rows.iter().filter(|r| r.name.ends_with(".af")).count() >= 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_manifest_check_rejects_malformed_rows() {
|
||||
let digest = "0".repeat(64);
|
||||
let url = "https://example.com/a.af";
|
||||
let row = |name: &str| format!("{digest}\t{name}\tMIT\t{url}\n");
|
||||
// Two names may share a digest.
|
||||
assert!(corpus::rows(&(row("a.af") + &row("dir/b.af"))).is_ok());
|
||||
for (bad, why) in [
|
||||
(row("a.af") + &row("a.af"), "listed twice"),
|
||||
(format!("{digest}\ta.af\tMIT\n"), "four tab-separated fields"),
|
||||
(format!("{digest}\ta.af\tMIT\t{url}\textra\n"), "four tab-separated fields"),
|
||||
(format!("{}\ta.af\tMIT\t{url}\n", "0".repeat(63)), "invalid SHA-256"),
|
||||
(format!("{}\ta.af\tMIT\t{url}\n", "A".repeat(64)), "invalid SHA-256"),
|
||||
(row("../a.af"), "relative path"),
|
||||
(row("/a.af"), "relative path"),
|
||||
(row("dir//a.af"), "relative path"),
|
||||
(row("dir\\a.af"), "relative path"),
|
||||
(row("C:a.af"), "relative path"),
|
||||
(format!("{digest}\ta.af\t \t{url}\n"), "no license"),
|
||||
(format!("{digest}\ta.af\tMIT\thttp://example.com/a.af\n"), "https"),
|
||||
(format!("{digest}\ta.af\tMIT\thttps://\n"), "https"),
|
||||
] {
|
||||
match corpus::rows(&bad) {
|
||||
Ok(_) => panic!("accepted {bad:?}"),
|
||||
Err(e) => assert!(e.contains(why), "{bad:?}: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,3 +1,21 @@
|
||||
affinity-samples/01-layer-effects.af dpi=72;spreads=1;model_crc32=9a2e65d6;counts={"depth": 2, "fills": 13, "gradients": 0, "layers": 12, "nodes": 38, "segments": 51, "shapes": 13, "strokes": 0, "subpaths": 13, "text_chars": 166, "text_runs": 13, "texts": 13};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["blur effects (left out)", "layer effects of other kinds (left out) (2×)", "paragraph leading of an unknown kind (imported as automatic) (13×)", "unknown layers attached to other layers (imported without them) (2×)"]
|
||||
affinity-samples/02-adjustment-layers.af dpi=72;spreads=1;model_crc32=fc8b239f;counts={"depth": 2, "fills": 17, "gradients": 16, "layers": 8, "nodes": 42, "segments": 51, "shapes": 17, "strokes": 0, "subpaths": 17, "text_chars": 96, "text_runs": 9, "texts": 9, "unsupported": 8};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["adjustment layers (8×)", "paragraph leading of an unknown kind (imported as automatic) (9×)"]
|
||||
affinity-samples/03-brush-strokes.af dpi=72;spreads=1;model_crc32=a492c413;counts={"depth": 1, "fills": 1, "gradients": 0, "hidden": 4, "images": 2, "nodes": 14, "segments": 15, "shapes": 7, "strokes": 6, "subpaths": 7, "text_chars": 107, "text_runs": 5, "texts": 5};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (5×)"]
|
||||
affinity-samples/04-master-page.af dpi=300;spreads=3;model_crc32=ba260d8d;counts={"depth": 3, "fills": 1, "gradients": 0, "layers": 6, "master_instances": 3, "nodes": 30, "segments": 18, "shapes": 9, "strokes": 0, "subpaths": 9, "text_chars": 31, "text_runs": 3, "texts": 3, "unsupported": 9};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (3×)", "text fields such as page numbers (left out of the text)", "text whose story could not be read (master-page text, or an unknown layout) (9×)"]
|
||||
affinity-samples/05-symbols.af dpi=72;spreads=1;model_crc32=fe74faa6;counts={"depth": 2, "fills": 5, "gradients": 0, "groups": 1, "layers": 3, "nodes": 40, "segments": 129, "shapes": 32, "strokes": 1, "subpaths": 32, "text_chars": 179, "text_runs": 4, "texts": 4};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (4×)"]
|
||||
affinity-samples/06-special-shapes.af dpi=72;spreads=1;model_crc32=7e9a9eb3;counts={"depth": 1, "fills": 17, "gradients": 0, "nodes": 50, "segments": 268, "shapes": 33, "strokes": 16, "subpaths": 34, "text_chars": 182, "text_runs": 17, "texts": 17};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (17×)"]
|
||||
affinity-samples/07-transparency.af dpi=72;spreads=1;model_crc32=6595042f;counts={"depth": 1, "fills": 18, "gradients": 1, "nodes": 26, "opacity": 1, "segments": 57, "shapes": 19, "strokes": 0, "subpaths": 19, "text_chars": 151, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["bitmap and pattern fills (left empty)", "paragraph leading of an unknown kind (imported as automatic) (7×)", "transparency gradients (imported without them) (2×)"]
|
||||
affinity-samples/08-gradients.af dpi=72;spreads=1;model_crc32=5638db4d;counts={"depth": 1, "fills": 7, "gradients": 6, "nodes": 14, "segments": 27, "shapes": 7, "strokes": 0, "subpaths": 7, "text_chars": 97, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (7×)"]
|
||||
affinity-samples/09-corners-stars.af dpi=72;spreads=1;model_crc32=e3528065;counts={"depth": 2, "fills": 9, "gradients": 0, "layers": 8, "nodes": 26, "segments": 81, "shapes": 9, "strokes": 0, "subpaths": 9, "text_chars": 122, "text_runs": 9, "texts": 9};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (9×)"]
|
||||
affinity-samples/10-multiple-fills.af dpi=72;spreads=1;model_crc32=07d2e434;counts={"depth": 2, "fills": 7, "gradients": 3, "layers": 6, "nodes": 20, "segments": 21, "shapes": 7, "strokes": 3, "subpaths": 7, "text_chars": 140, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (7×)", "several fills or strokes on one object (only the active one is imported) (6×)"]
|
||||
affinity-samples/11-text.af dpi=72;spreads=1;model_crc32=de077247;counts={"depth": 2, "fills": 13, "gradients": 0, "groups": 1, "layers": 1, "nodes": 28, "segments": 198, "shapes": 13, "strokes": 0, "subpaths": 18, "text_chars": 452, "text_runs": 11, "texts": 11, "unsupported": 2};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)", "text fields such as page numbers (left out of the text) (2×)", "text frames with several columns (imported as one column)", "unknown layers (2×)"]
|
||||
affinity-samples/12-cmyk8.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"]
|
||||
affinity-samples/12-grey8.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"]
|
||||
affinity-samples/12-lab16.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"]
|
||||
affinity-samples/12-rgb16.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"]
|
||||
affinity-samples/12-rgb32.af dpi=72;spreads=1;model_crc32=a9aeb9ab;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"]
|
||||
affinity-samples/12-rgb8.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"]
|
||||
affinity-samples/13-blend-modes.af dpi=72;spreads=1;model_crc32=13f9c6d7;counts={"depth": 2, "fills": 13, "gradients": 6, "layers": 6, "nodes": 26, "segments": 45, "shapes": 13, "strokes": 0, "subpaths": 13, "text_chars": 169, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (7×)"]
|
||||
affinity3-mexican-guy.af dpi=300;spreads=1;model_crc32=32b7cf3f;counts={"depth": 8, "fills": 169, "gradients": 0, "groups": 23, "nodes": 283, "opacity": 1, "segments": 1491, "shapes": 260, "strokes": 114, "subpaths": 264};boards=[];fonts={};warnings=["blur effects (left out) (156×)"]
|
||||
affinity3-mexican-man.af dpi=300;spreads=1;model_crc32=c10daaea;counts={"depth": 7, "fills": 277, "gradients": 2, "groups": 29, "nodes": 903, "segments": 3225, "shapes": 874, "strokes": 540, "subpaths": 881};boards=[];fonts={};warnings=["blur effects (left out) (244×)"]
|
||||
affinity3-mexican-woman.af dpi=300;spreads=1;model_crc32=3bc13374;counts={"depth": 9, "fills": 527, "gradients": 0, "groups": 28, "nodes": 595, "segments": 3562, "shapes": 567, "strokes": 60, "subpaths": 570};boards=[];fonts={};warnings=["blur effects (left out) (278×)"]
|
||||
@@ -31,3 +49,7 @@ patchy-text-runs.af dpi=72;spreads=1;model_crc32=7820b881;counts={"depth": 1, "n
|
||||
patchy-transform.af dpi=72;spreads=1;model_crc32=8d56df84;counts={"depth": 1, "images": 1, "nodes": 1};boards=[];fonts={};warnings=[]
|
||||
patchy-vector-mask.af dpi=72;spreads=1;model_crc32=80ca946a;counts={"depth": 1, "fills": 2, "gradients": 0, "nodes": 2, "segments": 7, "shapes": 2, "strokes": 0, "subpaths": 2, "vector_masks": 1};boards=[];fonts={};warnings=["unknown layers attached to other layers (imported without them)"]
|
||||
patchy-vector.af dpi=72;spreads=1;model_crc32=3b16c7ed;counts={"depth": 1, "fills": 3, "gradients": 0, "nodes": 4, "segments": 17, "shapes": 4, "strokes": 2, "subpaths": 5};boards=[];fonts={};warnings=[]
|
||||
vector-art/mexican-guy.af dpi=300;spreads=1;model_crc32=32b7cf3f;counts={"depth": 8, "fills": 169, "gradients": 0, "groups": 23, "nodes": 283, "opacity": 1, "segments": 1491, "shapes": 260, "strokes": 114, "subpaths": 264};boards=[];fonts={};warnings=["blur effects (left out) (156×)"]
|
||||
vector-art/mexican-man.af dpi=300;spreads=1;model_crc32=c10daaea;counts={"depth": 7, "fills": 277, "gradients": 2, "groups": 29, "nodes": 903, "segments": 3225, "shapes": 874, "strokes": 540, "subpaths": 881};boards=[];fonts={};warnings=["blur effects (left out) (244×)"]
|
||||
vector-art/mexican-woman.af dpi=300;spreads=1;model_crc32=3bc13374;counts={"depth": 9, "fills": 527, "gradients": 0, "groups": 28, "nodes": 595, "segments": 3562, "shapes": 567, "strokes": 60, "subpaths": 570};boards=[];fonts={};warnings=["blur effects (left out) (278×)"]
|
||||
vector-art/playing-cards.af dpi=300;spreads=1;model_crc32=bc0c838d;counts={"depth": 5, "fills": 363, "gradients": 0, "groups": 758, "nodes": 1350, "segments": 4864, "shapes": 592, "strokes": 17, "subpaths": 656};boards=[];fonts={};warnings=["blur effects (left out) (309×)", "brush strokes (imported as plain strokes)"]
|
||||
|
||||
@@ -10,9 +10,56 @@ use sha2::{Digest, Sha256};
|
||||
/// `sha256 name licence url` rows.
|
||||
pub const MANIFEST: &str = include_str!("corpus.tsv");
|
||||
|
||||
/// One manifest row.
|
||||
pub struct Row<'a> {
|
||||
pub digest: &'a str,
|
||||
pub name: &'a str,
|
||||
pub license: &'a str,
|
||||
pub url: &'a str,
|
||||
}
|
||||
|
||||
/// The rows of `manifest`. A row has exactly four tab-separated fields: a lowercase hexadecimal
|
||||
/// SHA-256, a relative `/`-separated name without empty, `.` or `..` parts, a license and an
|
||||
/// `https://` URL. Each name is listed once; several names may share a digest.
|
||||
pub fn rows(manifest: &str) -> Result<Vec<Row<'_>>, String> {
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
let mut rows = Vec::new();
|
||||
for line in manifest.lines().filter(|l| !l.trim().is_empty()) {
|
||||
let cols: Vec<&str> = line.split('\t').collect();
|
||||
let &[digest, name, license, url] = cols.as_slice() else {
|
||||
return Err(format!("corpus row needs four tab-separated fields: {line}"));
|
||||
};
|
||||
if digest.len() != 64 || !digest.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) {
|
||||
return Err(format!("{name}: invalid SHA-256 {digest:?}"));
|
||||
}
|
||||
if name.starts_with('/')
|
||||
|| name.contains(['\\', ':'])
|
||||
|| name.chars().any(char::is_control)
|
||||
|| name.split('/').any(|part| part.is_empty() || part == "." || part == "..")
|
||||
{
|
||||
return Err(format!("{name:?}: the name must be a relative path inside the corpus folder"));
|
||||
}
|
||||
if license.trim().is_empty() {
|
||||
return Err(format!("{name}: no license"));
|
||||
}
|
||||
if !url.starts_with("https://") || url.len() == "https://".len() {
|
||||
return Err(format!("{name}: the URL must use https: {url:?}"));
|
||||
}
|
||||
if !seen.insert(name) {
|
||||
return Err(format!("{name}: listed twice"));
|
||||
}
|
||||
rows.push(Row { digest, name, license, url });
|
||||
}
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
fn manifest() -> Vec<Row<'static>> {
|
||||
rows(MANIFEST).unwrap_or_else(|e| panic!("malformed corpus.tsv: {e}"))
|
||||
}
|
||||
|
||||
/// The names of the pinned files.
|
||||
pub fn names() -> impl Iterator<Item = &'static str> {
|
||||
MANIFEST.lines().filter_map(|l| l.split('\t').nth(1))
|
||||
manifest().into_iter().map(|row| row.name)
|
||||
}
|
||||
|
||||
pub fn pinned() -> Option<PathBuf> {
|
||||
@@ -21,10 +68,7 @@ pub fn pinned() -> Option<PathBuf> {
|
||||
return None;
|
||||
};
|
||||
let mut count = 0;
|
||||
for line in MANIFEST.lines().filter(|l| !l.trim().is_empty()) {
|
||||
let mut cols = line.split('\t');
|
||||
let (Some(digest), Some(name)) = (cols.next(), cols.next()) else { panic!("malformed corpus row: {line}") };
|
||||
assert_eq!(digest.len(), 64, "invalid SHA256 for {name}");
|
||||
for Row { digest, name, .. } in manifest() {
|
||||
let bytes = std::fs::read(dir.join(name)).unwrap_or_else(|e| panic!("missing pinned Affinity file {name}: {e}"));
|
||||
let hex: String = Sha256::digest(&bytes).iter().map(|b| format!("{b:02x}")).collect();
|
||||
assert_eq!(hex, digest, "{name}: pinned corpus modified; run scripts/fetch-affinity-corpus.sh");
|
||||
|
||||
@@ -7,10 +7,59 @@ e56c35f876277b101790ff4384cfde103a093ed6c81790a2d62176de21da339f afdesignload-re
|
||||
08946c60c0da1c8aeea92d65acfb244721eeb3b480ed358609f4a48bbecfc5b9 afdesignload-shape_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/shape_test.afdesign
|
||||
7306ab4fcbdc225d816f32a0d2aca15ac887053f60170459ac947e70f4ef343d afdesignload-slice_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/slice_test.afdesign
|
||||
e5d04bac0235af9386aea251cdd6903ed7810d335fdb6194d0483ea30b14e24a afdesignload-test_path.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/test_path.afdesign
|
||||
cc8ca0db0d8fc545eb29153be900f153fa6b7b7eac068d05e54a502aba1264fe afdesignload/color.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/color.afdesign
|
||||
9a17f500d09fb3b1a70d24aabdc2882955ec3eac3700a474549e6100be3a8484 afdesignload/layer_mode.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/layer_mode.afdesign
|
||||
77ae60c02749224fe2c52691c951568138eaed5f8584ebc5da83715fae0234dd afdesignload/layer_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/layer_test.afdesign
|
||||
04944918ff8e29c5c1b466b7e43c232f7ab80db15fb934080bbcd7baaf093e22 afdesignload/margins.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/margins.afdesign
|
||||
35afa31b89f2b9970eb3701ee64e2a19476383637e8b07689334d6ffe556b593 afdesignload/raster_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/raster_test.afdesign
|
||||
e56c35f876277b101790ff4384cfde103a093ed6c81790a2d62176de21da339f afdesignload/revision_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/revision_test.afdesign
|
||||
08946c60c0da1c8aeea92d65acfb244721eeb3b480ed358609f4a48bbecfc5b9 afdesignload/shape_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/shape_test.afdesign
|
||||
7306ab4fcbdc225d816f32a0d2aca15ac887053f60170459ac947e70f4ef343d afdesignload/slice_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/slice_test.afdesign
|
||||
e5d04bac0235af9386aea251cdd6903ed7810d335fdb6194d0483ea30b14e24a afdesignload/test_path.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/test_path.afdesign
|
||||
5f5b98f0ab2121f75f24362792c00cd5b610767820eb754b5660668f478b0c86 affinity-samples/01-layer-effects.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/01-layer-effects.af
|
||||
33c1ad5022abc14e6136f4cb9fa0751c9778a0a564bc3abc2e0e6b051f7e00bb affinity-samples/01-layer-effects.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/01-layer-effects.png
|
||||
4d19e95abe3423692f5bdfd0ede5bf3aebdd4bf16c32eaa304cead91a7a2e9b7 affinity-samples/02-adjustment-layers.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/02-adjustment-layers.af
|
||||
726b7939e23a745f7578fa84188b56c2eb845cde28493499f1b18b4ede9b8a6e affinity-samples/02-adjustment-layers.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/02-adjustment-layers.png
|
||||
c6ca86439c89d83ba43c76e9d783865ac09ca0ff524d5b347caa77f473caac98 affinity-samples/03-brush-strokes.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/03-brush-strokes.af
|
||||
e57e36ffffdd88d3a62ebdb142435d7d3056286b22130f459c404a3ef529f61f affinity-samples/03-brush-strokes.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/03-brush-strokes.png
|
||||
e3bf04f7cc02e1d49ec79503f2b4044f385de1e890bd461013a5a6fca09b5223 affinity-samples/04-master-page.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page.af
|
||||
246279f00d1f08917409b34e60359bd3bba158bcf4e8d5847a806f575d8eb98f affinity-samples/04-master-page_1.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page_1.png
|
||||
0e023792ce89f724b1dfedb6360556348309e483c23e32b91d87afab5d64c9cc affinity-samples/04-master-page_2.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page_2.png
|
||||
f431b15027e20ab760c4d241aa6981130560409cb270bbc6d4dd9241d1883f0a affinity-samples/04-master-page_3.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page_3.png
|
||||
ef779b58a6721001ee961b42eacb96fb47dcaa35ccb7e6ccf2ec6f33106070b2 affinity-samples/05-symbols.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/05-symbols.af
|
||||
6675fc4a1563f61b7fcaa64978447e0fa6e1b0bb8073b4e63f958db1cf726fef affinity-samples/05-symbols.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/05-symbols.png
|
||||
9e463b4d1ca8e7a1b67b3c30351a28af8ed05b9cd11f3cea2159319c9974bc67 affinity-samples/06-special-shapes.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/06-special-shapes.af
|
||||
a897c607631acbea31c5a926af6ca20c0acb2744636003b8c9ef07861752d947 affinity-samples/06-special-shapes.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/06-special-shapes.png
|
||||
3aba0d83055f09da4dba465a3abf66c77d4362dc6060548e54db943029b3996e affinity-samples/07-transparency.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/07-transparency.af
|
||||
3b73945b7302627caf679a961053f30a9593bab7d709e27acb1df54dadc53968 affinity-samples/07-transparency.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/07-transparency.png
|
||||
4292c1571b6c8c19b80c530602ca21d5996df7b99221639a4cde76d8307ffbcb affinity-samples/08-gradients.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/08-gradients.af
|
||||
f6fb427c9715ea193e595d0c441b462224971fcda2aa2b4d8b71631d0f59f992 affinity-samples/08-gradients.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/08-gradients.png
|
||||
c913882b3bea7a4218aa88e96890181cdc08ce37f0859196a1ad1828be78b347 affinity-samples/09-corners-stars.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/09-corners-stars.af
|
||||
068bcc7599336a174d7ce87962da414ccf429d549484ccbe8f68f669779d84d0 affinity-samples/09-corners-stars.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/09-corners-stars.png
|
||||
f03e797d6dbf8a31eab9d0b9cf9a8bddcc8440c6003863f31d97fe23c195c4b2 affinity-samples/10-multiple-fills.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/10-multiple-fills.af
|
||||
e1951aa6d8acdbeccc281b6a20a03df9b257c0d739aa5f6aec27c105151fef3f affinity-samples/10-multiple-fills.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/10-multiple-fills.png
|
||||
6da8b59b40d0e082435c5a2004c50fa90e3c4f15c3a02763b8edf84b8b9f4506 affinity-samples/11-text.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/11-text.af
|
||||
bb2040cda017cb753d0594416ac0501a1049bc333a7cf2c1c832356e2bbe1a45 affinity-samples/11-text.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/11-text.png
|
||||
0c13cfe0f716902199775251b0983f28d60a64c1f79d3c6f496f2c019365bdb5 affinity-samples/12-cmyk8.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-cmyk8.af
|
||||
cfef257c50201968121332e8ae12a7fe2ecdcc3bce1faeb5e581751662098c05 affinity-samples/12-cmyk8.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-cmyk8.png
|
||||
03d4cfe5ce4b80833d83de89848e88639056e8984c049865c8f29b9ef41b40e5 affinity-samples/12-grey8.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-grey8.af
|
||||
855e4f08f21c60f1d2080c9533b98f99c7a635c5804abf8ec9d6ee1a29029cef affinity-samples/12-grey8.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-grey8.png
|
||||
9adc0d4242b5448ac84aef886bd7afc165e1959015125ce21bf402ddb65caba1 affinity-samples/12-lab16.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-lab16.af
|
||||
6c30cef8304fd08539d631988280a6b3bc8570db3f1425fa771d83c1591dfc8d affinity-samples/12-lab16.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-lab16.png
|
||||
26b47861275b2f83517e7d42ec051efc3cd22956440784b6df0bc016ecb0e008 affinity-samples/12-rgb16.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb16.af
|
||||
967ba0cb7d93496a7e454dcc37013c1b19089cc283c7e08f2dea7023ad330e80 affinity-samples/12-rgb16.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb16.png
|
||||
36e62b0de8488712c04831ba3ef466da68bf080ec3caf9fbe6646905d3fb87e8 affinity-samples/12-rgb32.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb32.af
|
||||
0ebcf7211447d64158c21c27eeb19b8737cb2cdcc708dcdf3b4fed5041b449e9 affinity-samples/12-rgb32.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb32.png
|
||||
4d210ce4724409fb6c9715046ad9efa0049442f8324a21174068a02c6df10632 affinity-samples/12-rgb8.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb8.af
|
||||
4e4dda966c337119165ba5d88cb3d4af10bf6507b9a69986d33060a8eeb37c21 affinity-samples/12-rgb8.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb8.png
|
||||
b84815dbb5e3a7f7842d860acb888f93dbd1f949bcb6a3bf858515b85ea3c3b7 affinity-samples/13-blend-modes.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/13-blend-modes.af
|
||||
a49d2fe6043002aef4f4c6b8cf64a04838d21cf29795324c02fb1772d5519a21 affinity-samples/13-blend-modes.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/13-blend-modes.png
|
||||
b73eceb73a45df1a424d60164b7e1b521b9f01f56fe86159e477b49c9cd90f78 affinity-samples/source/07-texture.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/source/07-texture.png
|
||||
c499cf3ca73cf7cd3e2e65bed048d1b8ecf03e7167b74d54f0ae6db31868cab8 affinity3-mexican-guy.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-guy.af
|
||||
730e3fae51d9044ada58f51c7b5f9327d99e8d62376453bc251d0d26cda47743 affinity3-mexican-man.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-man.af
|
||||
5f5fccc0bd8be2bb3b2a07461ec77a012a61eef1883485d7c80f72d4383231ce affinity3-mexican-woman.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-woman.af
|
||||
0a52bb923c949c566a4339b996bce1a67b683f61def1a73dfefb910effaa450d affinity3-playing-cards.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/playing-cards.af
|
||||
e1b121d7c3d02356baa5dea512f5aa1fe77249c28d7b227e29cab2bc67ec323a asset-store-template/AssetStore.aftemplate MIT https://raw.githubusercontent.com/eviltwo/AssetStoreTemplate/f671981ee89d15526285b3cc87b4393d3defe644/AssetStoreTemplate/AssetStore.aftemplate
|
||||
87a7bd93de16c404f6c6dd9cdebff96ec47832a3b342d2ce0af73d9a87e209e0 designer-beer.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/beer.afdesign
|
||||
09ba790c428e8e09dc0b9ddebb4f3db6daefaa2af1da492ee34e73fba1b8af69 designer-cactus.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/cactus.afdesign
|
||||
c3bcd0395b407493bc9d2877d424029b70f0e4c817a56e893f7ac74973df52e5 designer-car.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/car.afdesign
|
||||
@@ -19,6 +68,8 @@ c3bcd0395b407493bc9d2877d424029b70f0e4c817a56e893f7ac74973df52e5 designer-car.af
|
||||
e1b121d7c3d02356baa5dea512f5aa1fe77249c28d7b227e29cab2bc67ec323a eviltwo-AssetStore.aftemplate MIT https://raw.githubusercontent.com/eviltwo/AssetStoreTemplate/f671981ee89d15526285b3cc87b4393d3defe644/AssetStoreTemplate/AssetStore.aftemplate
|
||||
fbb196e918e302c16f8de9f2e6dffaef70ab3c317b32a9f44607fd4b2ead0206 jac21-SimpleLogo.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogo.afdesign
|
||||
9d874245936483de3ec04cca48b383028d33666801dff1814246a44e695e29b8 jac21-SimpleLogoBanner.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogoBanner.afdesign
|
||||
fbb196e918e302c16f8de9f2e6dffaef70ab3c317b32a9f44607fd4b2ead0206 jac21/SimpleLogo.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogo.afdesign
|
||||
9d874245936483de3ec04cca48b383028d33666801dff1814246a44e695e29b8 jac21/SimpleLogoBanner.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogoBanner.afdesign
|
||||
ec1072cb03fcd0ed7ac1dfc7e087f4920df545fe9c84a386a7dac2fd33fbbe1f leakcanary-vector_icon.afdesign Apache-2.0 https://raw.githubusercontent.com/square/leakcanary/00880a8fb66eb4046a0124bff8303a04874bc07e/docs/assets/vector_icon.afdesign
|
||||
bbc50c8c376e0e5980939be7df6769feed1a30289c7efc6391b204dfb15de88d patchy-LICENSE.txt MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/LICENSE
|
||||
150e423be0c5eb22f252a38c2de68ab3ccec7e20bfba166f7ddd622ac243d8f4 patchy-NOTICE.txt MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/NOTICE-THIRD-PARTY.md
|
||||
@@ -51,3 +102,12 @@ a5ccb62ee8d7e36d1a19a037aba989a5ba0232767e9f3305f7d9f7d4603291dd patchy-text-rot
|
||||
87e1dee23769932b7e414116233063a8d68726cbffdebb975b3cd0ef447c602a patchy-transform.af MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/test-fixtures/af/tiny-transform.af
|
||||
fa5dd80f44ae0712a9888c62953fd442a28119034d343db93b7892d5d2a61328 patchy-vector-mask.af MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/test-fixtures/af/tiny-vector-mask.af
|
||||
8ab1995ecbd9306ebcb7a4d2cd81701f34401e67f08b242f14dc3e3d04c94803 patchy-vector.af MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/test-fixtures/af/tiny-vector.af
|
||||
87a7bd93de16c404f6c6dd9cdebff96ec47832a3b342d2ce0af73d9a87e209e0 vector-art/beer.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/beer.afdesign
|
||||
09ba790c428e8e09dc0b9ddebb4f3db6daefaa2af1da492ee34e73fba1b8af69 vector-art/cactus.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/cactus.afdesign
|
||||
c3bcd0395b407493bc9d2877d424029b70f0e4c817a56e893f7ac74973df52e5 vector-art/car.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/car.afdesign
|
||||
112562bc91e7cccf003d34364430544208373c4fb4bb4ab9acdaf27cf0125047 vector-art/flowers.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/flowers.afdesign
|
||||
023531b8216f2d4da24ab715e3b313506712e50d5f5bf7da927f434def1d0ea2 vector-art/lion-track.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/lion-track.afdesign
|
||||
c499cf3ca73cf7cd3e2e65bed048d1b8ecf03e7167b74d54f0ae6db31868cab8 vector-art/mexican-guy.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-guy.af
|
||||
730e3fae51d9044ada58f51c7b5f9327d99e8d62376453bc251d0d26cda47743 vector-art/mexican-man.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-man.af
|
||||
5f5fccc0bd8be2bb3b2a07461ec77a012a61eef1883485d7c80f72d4383231ce vector-art/mexican-woman.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-woman.af
|
||||
0a52bb923c949c566a4339b996bce1a67b683f61def1a73dfefb910effaa450d vector-art/playing-cards.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/playing-cards.af
|
||||
|
||||
|
@@ -0,0 +1,32 @@
|
||||
[package]
|
||||
name = "craft-ai"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
description = "AI provider abstraction for the Craft suite — BYO API keys and one-click local models"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
dirs = "7"
|
||||
sysinfo = "0.39"
|
||||
ureq = { version = "3", features = ["json"] }
|
||||
sha2 = "0.11"
|
||||
candle-core = { version = "0.9", optional = true }
|
||||
candle-nn = { version = "0.9", optional = true }
|
||||
candle-transformers = { version = "0.9", optional = true }
|
||||
tokenizers = { version = "0.22", optional = true, default-features = false, features = ["onig"] }
|
||||
|
||||
[features]
|
||||
# Local GGUF inference on candle. Heavy dependency tree; apps opt in.
|
||||
local = ["dep:candle-core", "dep:candle-nn", "dep:candle-transformers", "dep:tokenizers"]
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
@@ -0,0 +1,145 @@
|
||||
# craft-ai — AI providers for the Craft suite
|
||||
|
||||
Shared AI abstraction for every Craft app: translation, rewriting, summaries,
|
||||
vision captions — one crate, one settings store, one model cache.
|
||||
|
||||
Status: **spec** — this file is the contract; implementation follows.
|
||||
|
||||
## The feature this powers
|
||||
|
||||
A per-app **AI tab** offering document-wide operations — the flagship being
|
||||
"translate this whole document" — backed by either:
|
||||
|
||||
1. **BYOT** — the user's own API token against an OpenAI-compatible endpoint
|
||||
(OpenAI, Azure, Ollama, LM Studio, any compatible proxy), or
|
||||
2. **Local model** — a one-click download of a curated open model, chosen for
|
||||
the machine it's running on.
|
||||
|
||||
The user experience is deliberately *informed*: every step reports what is
|
||||
happening, why this model was picked, what it will cost in disk/RAM, and that
|
||||
nothing leaves the machine in local mode.
|
||||
|
||||
## Modules
|
||||
|
||||
### `probe` — capability detection
|
||||
|
||||
Runs once, caches the result:
|
||||
|
||||
| Signal | Source |
|
||||
| --- | --- |
|
||||
| Total/free RAM | `sysinfo` or `std::alloc`-safe OS calls |
|
||||
| CPU threads + features | `std::thread`, `is_x86_feature_detected!` (AVX2 gates Q4 speed) |
|
||||
| GPU + VRAM | `wgpu` adapter info (apps already link wgpu — reuse, don't re-probe) |
|
||||
| Free disk at the model cache dir | `fs2`/`std::fs` |
|
||||
| Network reachability | optional HEAD to the model host, skipped offline |
|
||||
|
||||
Output: `MachineCaps { ram_gb, vram_gb, threads, avx2, disk_free_gb }`.
|
||||
|
||||
### `registry` — the curated model list
|
||||
|
||||
A small hand-maintained table (Rust consts, not runtime-fetched — supply
|
||||
chain stays auditable). Each entry: `id`, display name, HF repo + file,
|
||||
`sha256`, size, min RAM, recommended RAM, license id, quality tier,
|
||||
languages covered.
|
||||
|
||||
Curation rule — **permissive licenses only** (the suite is MIT OR Apache-2.0
|
||||
and commercial users run it):
|
||||
|
||||
- ✅ Qwen2.5-0.5B / 1.5B / 3B Instruct (Apache-2.0)
|
||||
- ✅ SmolLM2-1.7B (Apache-2.0)
|
||||
- ✅ Gemma-3 (Gemma Terms — allowed, flag in UI)
|
||||
- ✅ Helsinki-NLP opus-mt-* per-language pairs (CC-BY/MIT)
|
||||
- ❌ NLLB-200, M2M-100 — CC-BY-NC, excluded
|
||||
- ❌ Llama-* — custom license acceptable? default: exclude until reviewed
|
||||
|
||||
`registry::suggest(&caps)` picks the best fitting model: prefers 3B when
|
||||
VRAM ≥ 6 GB or RAM ≥ 16 GB, falls to 1.5B at 8 GB, 0.5B below. Always shows
|
||||
alternatives — the suggestion is a default, not a cage.
|
||||
|
||||
### `provider` — inference backends
|
||||
|
||||
```rust
|
||||
trait Provider {
|
||||
fn name(&self) -> &str;
|
||||
fn complete(&self, prompt: &str, opts: &GenOpts) -> Result<String, AiError>;
|
||||
fn test(&self) -> Result<ProviderInfo, AiError>;
|
||||
}
|
||||
```
|
||||
|
||||
- `OpenAiCompat` — POST `/v1/chat/completions`, configurable endpoint+token+
|
||||
model. Covers OpenAI, Ollama (`http://localhost:11434/v1`), LM Studio,
|
||||
Groq, OpenRouter… Token stored via the OS keyring (`keyring` crate) or a
|
||||
0600-permissioned config file — never in project files, never logged.
|
||||
- `LocalGguf` — GGUF inference. First implementation target: **candle**
|
||||
(pure Rust, Apache/MIT — keeps the no-C++ toolchain rule). `llama-cpp-2`
|
||||
stays a possible perf upgrade later, gated behind a feature flag.
|
||||
|
||||
### `fetch` — one-click download
|
||||
|
||||
- Resumable HTTP GET (range requests), streamed to `*.part` then renamed —
|
||||
a half-downloaded GGUF can never be mistaken for a valid one.
|
||||
- SHA-256 checked against the registry entry; mismatch = hard fail, delete.
|
||||
- Progress events (`Download { bytes_done, bytes_total, bytes_per_sec }`)
|
||||
feed the UI's live status line.
|
||||
- Cache dir is **shared across the suite**:
|
||||
`%LOCALAPPDATA%\artcraft\models` (Windows), `~/Library/Application
|
||||
Support/artcraft/models` (macOS), `~/.cache/artcraft/models` (Linux).
|
||||
|
||||
### `translate` — document segmentation
|
||||
|
||||
The model is the easy part; the hard part is *what not to touch*. The
|
||||
translator walks the document as segments, not raw text:
|
||||
|
||||
- **Protected**: LaTeX commands/envs/math (`\cite{…}`, `$…$`), code fences,
|
||||
placeholders (`{count}`), URLs, citation keys, markup tags.
|
||||
- **Translated**: prose runs, captions, headings, table cells.
|
||||
- Per-app adapters declare the grammar: TexCraft protects `\command{…}` and
|
||||
math mode; WordCraft protects field codes; MatCraft protects code.
|
||||
- Batch-friendly: segments → prompt with numbered items → parse numbered
|
||||
output; verify count matches, retry split halves on mismatch.
|
||||
- Deterministic options: temperature 0-ish, glossary hook (`HashMap<term,
|
||||
translation>`) applied post-hoc.
|
||||
|
||||
## The AI tab (per app, `ui-egui/src/ai.rs`)
|
||||
|
||||
First-open flow — the "smooth informed" sequence:
|
||||
|
||||
1. Two cards: **Use an API provider** / **Run on this PC**.
|
||||
2. BYOT card: endpoint, token, model fields + **Test connection** → green
|
||||
check with model list echo.
|
||||
3. Local card: **Detect this PC** → probe runs →
|
||||
`Suggested: Qwen2.5-1.5B-Instruct (Q4_K_M, 1.1 GB) — fits your 16 GB RAM;
|
||||
est. ~8 tok/s` with an "Other models ▸" disclosure showing the registry.
|
||||
4. **Download** → progress bar with MB/%/speed → `Verifying checksum…` →
|
||||
`Loading…` → `Ready — local, offline, nothing leaves this PC`.
|
||||
5. Then the operations: "Translate document to [language ▾]", "Summarize
|
||||
selection", custom prompt box. Status line narrates each step;
|
||||
expandable activity log for the curious.
|
||||
6. Language detected vs document language → gentle offer chip:
|
||||
`This document is French — translate to English?`
|
||||
|
||||
## Non-negotiables
|
||||
|
||||
- **Honesty**: the UI always says which provider is active, what model,
|
||||
and "cloud — your document text is sent to the endpoint" vs "local —
|
||||
nothing leaves this PC". No silent cloud calls; AI is strictly opt-in.
|
||||
- **No crash**: model load failures, corrupt GGUFs, OOM — all `Result`,
|
||||
all surfaced as messages.
|
||||
- **Rust only**: candle over llama.cpp bindings preserves the wasm story
|
||||
(local inference on wasm is future-possible with `candle` + WGPU backend).
|
||||
- **Licensing**: registry carries SPDX ids; the UI shows them; CC-BY-NC and
|
||||
unreviewed custom licenses are excluded by policy.
|
||||
- **Everything is a command**: `ai.translate_doc`, `ai.status`,
|
||||
`ai.models.list`, `ai.model.download`, `ai.provider.test` — reachable
|
||||
from the app's control channel and MCP, same as every Craft convention.
|
||||
|
||||
## Milestones
|
||||
|
||||
| # | Deliverable | Done when |
|
||||
| --- | --- | --- |
|
||||
| A1 | `probe` + `registry` + `fetch` | CLI-free test: probe reports real caps; `suggest` picks sanely per fixture; a tiny model downloads + sha256 verifies |
|
||||
| A2 | `OpenAiCompat` provider | `translate` a string through Ollama/OpenAI with mocked HTTP tests |
|
||||
| A3 | `LocalGguf` on candle | Qwen2.5-0.5B loads and answers on CPU in a test (model fixture fetched in-test, skipped without network) |
|
||||
| A4 | `translate` segmentation | LaTeX/code/placeholder round-trip tests — protected spans byte-identical |
|
||||
| A5 | AI tab in one pilot app (suggest TexCraft or WordCraft) | download → translate document end-to-end, live status UX |
|
||||
| A6 | Commands + MCP surface | `ai.*` commands reachable via control channel |
|
||||
@@ -0,0 +1,52 @@
|
||||
//! The crate's error type: everything fails with `Result`, never a panic.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// Every failure a caller can act on: the variant says what went wrong,
|
||||
/// the message says why.
|
||||
#[derive(Debug)]
|
||||
pub enum AiError {
|
||||
/// The HTTP layer failed (DNS, TLS, status code, body).
|
||||
Http(String),
|
||||
/// A downloaded file's SHA-256 did not match the expected digest.
|
||||
Checksum { expected: String, got: String },
|
||||
/// The local disk refused us (permissions, full, vanished mid-write).
|
||||
Io(std::io::Error),
|
||||
/// The provider returned a response we could not parse.
|
||||
BadResponse(String),
|
||||
/// The provider rejected the request (bad token, quota, model name).
|
||||
Rejected(String),
|
||||
/// No provider is configured — the UI should route to setup.
|
||||
NotConfigured,
|
||||
/// Local model inference failed (load, OOM, bad GGUF).
|
||||
Inference(String),
|
||||
/// The operation was cancelled by the user.
|
||||
Cancelled,
|
||||
/// The same cache entry is already being downloaded by another
|
||||
/// caller — only one may hold the `.part` file at a time.
|
||||
Busy,
|
||||
}
|
||||
|
||||
impl fmt::Display for AiError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Http(m) => write!(f, "network error: {m}"),
|
||||
Self::Checksum { expected, got } => write!(f, "checksum mismatch: expected {expected}, got {got}"),
|
||||
Self::Io(e) => write!(f, "disk error: {e}"),
|
||||
Self::BadResponse(m) => write!(f, "unreadable response: {m}"),
|
||||
Self::Rejected(m) => write!(f, "provider rejected the request: {m}"),
|
||||
Self::NotConfigured => write!(f, "no AI provider configured"),
|
||||
Self::Inference(m) => write!(f, "local model error: {m}"),
|
||||
Self::Cancelled => write!(f, "cancelled"),
|
||||
Self::Busy => write!(f, "a download of this model is already in progress"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for AiError {}
|
||||
|
||||
impl From<std::io::Error> for AiError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Self::Io(e)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
//! One-click model download: resumable, `.part`-suffixed while in flight
|
||||
//! (a half-downloaded GGUF is never mistaken for a valid one), SHA-256
|
||||
//! verified, then atomically renamed.
|
||||
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use sha2::Digest;
|
||||
|
||||
use crate::error::AiError;
|
||||
use crate::registry::ModelEntry;
|
||||
|
||||
/// Progress + lifecycle events, fed to the UI's status line.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Event {
|
||||
/// We know the total; download is starting or resuming.
|
||||
Started { total: u64, resumed_from: u64 },
|
||||
/// Bytes on disk so far. `bytes_per_sec` is a running average.
|
||||
Progress { done: u64, total: u64, bytes_per_sec: u64 },
|
||||
/// Body finished; hashing the file before we trust it.
|
||||
Verifying,
|
||||
/// Download is complete. `verified` is false when no reference digest
|
||||
/// existed anywhere (registry table and the host's ETag) — the file is
|
||||
/// kept but the UI should say "unverified" rather than pretend.
|
||||
Done { verified: bool },
|
||||
}
|
||||
|
||||
/// A handle to abort a download: `cancel` returns false → `AiError::Cancelled`.
|
||||
pub type Cancel<'a> = &'a dyn Fn() -> bool;
|
||||
|
||||
/// ureq's `timeout_recv_body` is a *total* budget, not a stall timer, so a
|
||||
/// single fixed cap would kill slow-but-alive downloads of multi-GB models.
|
||||
/// Instead the GET's body deadline scales with the registry's declared
|
||||
/// size: a floor rate of 256 KiB/s, between 5 min and 6 h. A dead
|
||||
/// connection can therefore never block the read loop indefinitely, and a
|
||||
/// retry still resumes from the `.part` file.
|
||||
const MIN_BODY_RATE: u64 = 256 * 1024;
|
||||
const BODY_BUDGET_MIN: Duration = Duration::from_secs(5 * 60);
|
||||
const BODY_BUDGET_MAX: Duration = Duration::from_secs(6 * 60 * 60);
|
||||
|
||||
fn body_agent(remaining_bytes: u64) -> ureq::Agent {
|
||||
let budget = Duration::from_secs(remaining_bytes / MIN_BODY_RATE).clamp(BODY_BUDGET_MIN, BODY_BUDGET_MAX);
|
||||
ureq::Agent::config_builder()
|
||||
.timeout_connect(Some(Duration::from_secs(15)))
|
||||
.timeout_send_request(Some(Duration::from_secs(30)))
|
||||
.timeout_recv_response(Some(Duration::from_secs(60)))
|
||||
.timeout_recv_body(Some(budget))
|
||||
.build()
|
||||
.into()
|
||||
}
|
||||
|
||||
/// OS-level lock guarding one cache entry against concurrent downloads;
|
||||
/// released automatically on close/process exit, so no stale lock files.
|
||||
fn lock_entry(part_path: &Path) -> Result<File, AiError> {
|
||||
let mut lock_name = part_path.as_os_str().to_os_string();
|
||||
lock_name.push(".lock");
|
||||
let lock = OpenOptions::new().write(true).create(true).truncate(false).open(PathBuf::from(lock_name))?;
|
||||
match lock.try_lock() {
|
||||
Ok(()) => Ok(lock),
|
||||
Err(std::fs::TryLockError::WouldBlock) => Err(AiError::Busy),
|
||||
Err(std::fs::TryLockError::Error(e)) => Err(AiError::Io(e)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Download `entry` into `cache_dir`, calling `on` with progress events.
|
||||
/// Resumes an existing `*.part` via HTTP Range. Returns the final path.
|
||||
pub fn download(entry: &ModelEntry, cache_dir: &Path, on: impl Fn(&Event), cancel: Option<Cancel<'_>>) -> Result<PathBuf, AiError> {
|
||||
if cancel.is_some_and(|c| !c()) {
|
||||
return Err(AiError::Cancelled);
|
||||
}
|
||||
fs::create_dir_all(cache_dir)?;
|
||||
let final_path = cache_dir.join(entry.file);
|
||||
if final_path.is_file() {
|
||||
// Already on disk — verify once, reuse if good.
|
||||
on(&Event::Verifying);
|
||||
if let Some(expected) = expected_sha256(entry)? {
|
||||
let got = sha256_file(&final_path)?;
|
||||
if got.eq_ignore_ascii_case(&expected) {
|
||||
on(&Event::Done { verified: true });
|
||||
return Ok(final_path);
|
||||
}
|
||||
return Err(AiError::Checksum { expected, got });
|
||||
}
|
||||
on(&Event::Done { verified: false });
|
||||
return Ok(final_path);
|
||||
}
|
||||
|
||||
let part_path = cache_dir.join(format!("{}.part", entry.file));
|
||||
// One downloader per cache entry: two concurrent calls would read the
|
||||
// same resume offset and append the same bytes twice, corrupting the
|
||||
// `.part`. The lock is held through the final rename.
|
||||
let _entry_lock = lock_entry(&part_path)?;
|
||||
let mut resumed_from = part_path.metadata().map_or(0, |m| m.len());
|
||||
|
||||
let expected = expected_sha256(entry)?;
|
||||
|
||||
let get = |range_from: u64| {
|
||||
// Budget for the *full* size, not the remainder: a 200 that
|
||||
// ignores Range means we re-download the whole model.
|
||||
let mut req = body_agent(entry.size_mb.saturating_mul(1 << 20)).get(entry.url);
|
||||
if range_from > 0 {
|
||||
req = req.header("Range", &format!("bytes={range_from}-"));
|
||||
}
|
||||
req.call()
|
||||
};
|
||||
let mut resp = match get(resumed_from) {
|
||||
Ok(r) => r,
|
||||
// A 416 means the server found our resume offset unsatisfiable —
|
||||
// the common cause is a `.part` that already holds the complete
|
||||
// file (cancelled between the last byte and the rename). If it
|
||||
// verifies, finish the job; otherwise restart from scratch.
|
||||
Err(ureq::Error::StatusCode(416)) if resumed_from > 0 => {
|
||||
if let Some(expected) = &expected
|
||||
&& sha256_file(&part_path).is_ok_and(|got| got.eq_ignore_ascii_case(expected))
|
||||
{
|
||||
on(&Event::Verifying);
|
||||
fs::rename(&part_path, &final_path)?;
|
||||
on(&Event::Done { verified: true });
|
||||
return Ok(final_path);
|
||||
}
|
||||
fs::remove_file(&part_path)?;
|
||||
resumed_from = 0;
|
||||
get(0).map_err(http_err)?
|
||||
}
|
||||
Err(e) => return Err(http_err(e)),
|
||||
};
|
||||
|
||||
// A 200 to a Range request means the server ignored resume — truncate.
|
||||
let honored_resume = resumed_from > 0 && resp.status() == 206;
|
||||
let total = content_length(&resp).map(|len| if honored_resume { len + resumed_from } else { len });
|
||||
|
||||
on(&Event::Started { total: total.unwrap_or(0), resumed_from: if honored_resume { resumed_from } else { 0 } });
|
||||
|
||||
let mut file = if honored_resume { OpenOptions::new().append(true).open(&part_path)? } else { File::create(&part_path)? };
|
||||
|
||||
let mut body = resp.body_mut().as_reader();
|
||||
let mut buf = vec![0u8; 256 * 1024];
|
||||
let mut done = if honored_resume { resumed_from } else { 0 };
|
||||
let started = Instant::now();
|
||||
loop {
|
||||
if cancel.is_some_and(|c| !c()) {
|
||||
return Err(AiError::Cancelled);
|
||||
}
|
||||
let n = match body.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => n,
|
||||
Err(e) => return Err(AiError::Io(e)),
|
||||
};
|
||||
file.write_all(&buf[..n])?;
|
||||
done += n as u64;
|
||||
let secs = started.elapsed().as_secs_f64().max(0.001);
|
||||
on(&Event::Progress { done, total: total.unwrap_or(0), bytes_per_sec: (done.saturating_sub(resumed_from) as f64 / secs) as u64 });
|
||||
}
|
||||
file.flush()?;
|
||||
drop(file);
|
||||
|
||||
// A short read means the connection dropped mid-body — keep the .part
|
||||
// for resume and report it as an HTTP failure, not a success.
|
||||
if let Some(t) = total
|
||||
&& done < t
|
||||
{
|
||||
return Err(AiError::Http(format!("connection dropped at {done}/{t} bytes — retry to resume")));
|
||||
}
|
||||
|
||||
on(&Event::Verifying);
|
||||
let verified = if let Some(expected) = expected {
|
||||
let got = sha256_file(&part_path)?;
|
||||
if !got.eq_ignore_ascii_case(&expected) {
|
||||
// A corrupt download is deleted — it can never silently pass.
|
||||
let _ = fs::remove_file(&part_path);
|
||||
return Err(AiError::Checksum { expected, got });
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
fs::rename(&part_path, &final_path)?;
|
||||
on(&Event::Done { verified });
|
||||
Ok(final_path)
|
||||
}
|
||||
|
||||
/// The digest we hold this file to: our registry hash if pinned, else the
|
||||
/// ETag of the resolved URL (Hugging Face serves the LFS sha256 there).
|
||||
fn expected_sha256(entry: &ModelEntry) -> Result<Option<String>, AiError> {
|
||||
if let Some(s) = entry.sha256 {
|
||||
return Ok(Some(s.to_string()));
|
||||
}
|
||||
// No redirects: HF serves the LFS sha256 in `x-linked-etag` on the
|
||||
// 302 itself; following it would return the CDN's own etag instead.
|
||||
let agent: ureq::Agent = ureq::Agent::config_builder()
|
||||
.max_redirects(0)
|
||||
.timeout_connect(Some(Duration::from_secs(15)))
|
||||
.timeout_recv_response(Some(Duration::from_secs(30)))
|
||||
.build()
|
||||
.into();
|
||||
match agent.head(entry.url).call() {
|
||||
Ok(resp) => Ok(etag(&resp)),
|
||||
// HEAD unsupported/unreachable → proceed without verification.
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn etag(resp: &ureq::http::Response<ureq::Body>) -> Option<String> {
|
||||
// Prefer the redirect target's etag (the LFS object), fall back to
|
||||
// the response's own. Quotes stripped; weak etags ("W/…") ignored.
|
||||
for name in ["x-linked-etag", "etag"] {
|
||||
if let Some(v) = resp.headers().get(name).and_then(|v| v.to_str().ok()) {
|
||||
let v = v.trim().trim_matches('"');
|
||||
if !v.starts_with("W/") && v.len() == 64 && v.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
return Some(v.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn content_length(resp: &ureq::http::Response<ureq::Body>) -> Option<u64> {
|
||||
resp.headers()
|
||||
.get("content-length")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|v| v.trim().parse().ok())
|
||||
// A redirect's own length is meaningless; only trust it after
|
||||
// ureq followed redirects (it does — so the final body length).
|
||||
.filter(|&n: &u64| n > 0)
|
||||
}
|
||||
|
||||
fn sha256_file(path: &Path) -> Result<String, AiError> {
|
||||
let mut file = File::open(path)?;
|
||||
let mut hasher = sha2::Sha256::new();
|
||||
let mut buf = vec![0u8; 256 * 1024];
|
||||
loop {
|
||||
let n = file.read(&mut buf)?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buf[..n]);
|
||||
}
|
||||
Ok(hasher.finalize().iter().map(|b| format!("{b:02x}")).collect())
|
||||
}
|
||||
|
||||
fn http_err(e: ureq::Error) -> AiError {
|
||||
match e {
|
||||
ureq::Error::StatusCode(code) => AiError::Rejected(format!("HTTP {code}")),
|
||||
other => AiError::Http(other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a model would land once downloaded.
|
||||
pub fn model_path(entry: &ModelEntry, cache_dir: &Path) -> PathBuf {
|
||||
cache_dir.join(entry.file)
|
||||
}
|
||||
|
||||
/// Is `entry` already fully downloaded?
|
||||
pub fn is_downloaded(entry: &ModelEntry, cache_dir: &Path) -> bool {
|
||||
cache_dir.join(entry.file).is_file()
|
||||
}
|
||||
|
||||
/// `tokenizer.json` beside the GGUF: `<file>.tokenizer.json`.
|
||||
pub fn tokenizer_path(entry: &ModelEntry, cache_dir: &Path) -> PathBuf {
|
||||
cache_dir.join(format!("{}.tokenizer.json", entry.file))
|
||||
}
|
||||
|
||||
/// The companion tokenizer — small (a few MB), so no resume loop, but the
|
||||
/// same `.part`-then-rename rule applies.
|
||||
pub fn download_tokenizer(entry: &ModelEntry, cache_dir: &Path) -> Result<PathBuf, AiError> {
|
||||
let dest = tokenizer_path(entry, cache_dir);
|
||||
if dest.is_file() {
|
||||
return Ok(dest);
|
||||
}
|
||||
let tmp = dest.with_extension("part");
|
||||
let mut resp = body_agent(32 * 1024 * 1024).get(entry.tokenizer_url).call().map_err(http_err)?;
|
||||
let bytes = resp.body_mut().read_to_vec().map_err(|e| AiError::Http(e.to_string()))?;
|
||||
fs::write(&tmp, bytes)?;
|
||||
fs::rename(&tmp, &dest)?;
|
||||
Ok(dest)
|
||||
}
|
||||
|
||||
/// Remove `entry`'s file (and any `.part` remnant). Missing is fine;
|
||||
/// other failures (permissions, locked) are surfaced to the caller.
|
||||
pub fn delete(entry: &ModelEntry, cache_dir: &Path) -> Result<(), AiError> {
|
||||
for p in [cache_dir.join(entry.file), cache_dir.join(format!("{}.part", entry.file))] {
|
||||
match fs::remove_file(&p) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(e) => return Err(AiError::Io(e)),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::registry::Arch;
|
||||
|
||||
fn entry() -> &'static ModelEntry {
|
||||
static E: ModelEntry = ModelEntry {
|
||||
id: "t",
|
||||
name: "t",
|
||||
url: "",
|
||||
file: "m.gguf",
|
||||
tokenizer_url: "",
|
||||
sha256: None,
|
||||
size_mb: 1,
|
||||
min_ram_gb: 0.0,
|
||||
rec_ram_gb: 0.0,
|
||||
license: "MIT",
|
||||
quality: crate::registry::Quality::Tiny,
|
||||
arch: Arch::Llama,
|
||||
langs: "",
|
||||
};
|
||||
&E
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn second_lock_on_same_entry_is_busy() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let part = dir.path().join("m.gguf.part");
|
||||
let _lock = lock_entry(&part).expect("first lock");
|
||||
assert!(matches!(lock_entry(&part), Err(AiError::Busy)));
|
||||
// Releasing lets the next caller in.
|
||||
drop(_lock);
|
||||
assert!(lock_entry(&part).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_reports_failures_beyond_not_found() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
// Absent paths are fine.
|
||||
delete(entry(), dir.path()).expect("missing ok");
|
||||
// A directory where the file would be fails removal on every OS.
|
||||
fs::create_dir(dir.path().join("m.gguf")).expect("mkdir");
|
||||
assert!(matches!(delete(entry(), dir.path()), Err(AiError::Io(_))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//! craft-ai — AI provider abstraction for the Craft suite.
|
||||
//!
|
||||
//! See `README.md` for the design: BYO API provider (OpenAI-compatible) +
|
||||
//! one-click local models with capability detection.
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
mod error;
|
||||
mod provider;
|
||||
mod registry;
|
||||
mod translate;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub mod fetch;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[cfg(feature = "local")]
|
||||
pub mod local;
|
||||
// `MachineCaps` is portable data the registry needs on every target; only
|
||||
// the OS probe itself is native-only.
|
||||
pub mod probe;
|
||||
|
||||
pub use error::AiError;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub use provider::OpenAiCompat;
|
||||
pub use provider::{GenOpts, Provider, ProviderInfo};
|
||||
pub use registry::{Arch, MODELS, ModelEntry, Quality, by_id, fits, suggest};
|
||||
pub use translate::{PdfGrammar, Segment, TranslateGrammar, is_protected_token, segment, translate_segments};
|
||||
@@ -0,0 +1,144 @@
|
||||
//! Local GGUF inference on candle (pure Rust — keeps the no-C++ rule and
|
||||
//! the door open for wasm later). CPU only for now; GPU comes from the
|
||||
//! app's wgpu adapter in a later milestone.
|
||||
//!
|
||||
//! Both supported architectures speak ChatML — one prompt template covers
|
||||
//! Qwen2.5 and SmolLM2.
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
use candle_core::quantized::gguf_file;
|
||||
use candle_core::{Device, IndexOp, Tensor};
|
||||
use candle_transformers::generation::LogitsProcessor;
|
||||
use candle_transformers::models::{quantized_llama, quantized_qwen2};
|
||||
use tokenizers::Tokenizer;
|
||||
|
||||
use crate::error::AiError;
|
||||
use crate::provider::{GenOpts, Provider, ProviderInfo};
|
||||
use crate::registry::{Arch, ModelEntry};
|
||||
|
||||
enum Weights {
|
||||
Qwen2(quantized_qwen2::ModelWeights),
|
||||
Llama(quantized_llama::ModelWeights),
|
||||
}
|
||||
|
||||
impl Weights {
|
||||
fn forward(&mut self, ids: &Tensor, pos: usize) -> Result<Tensor, candle_core::Error> {
|
||||
match self {
|
||||
Self::Qwen2(w) => w.forward(ids, pos),
|
||||
Self::Llama(w) => w.forward(ids, pos),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A downloaded GGUF loaded and ready. Construction is the expensive
|
||||
/// part; `complete` is then just a forward loop.
|
||||
pub struct LocalGguf {
|
||||
weights: Weights,
|
||||
tokenizer: Tokenizer,
|
||||
device: Device,
|
||||
eos: u32,
|
||||
entry: &'static ModelEntry,
|
||||
}
|
||||
|
||||
const SEED: u64 = 0xC0FFEE;
|
||||
|
||||
impl LocalGguf {
|
||||
/// Load the GGUF + tokenizer.json of `entry` from the model cache.
|
||||
/// All failures are `Inference` errors — a corrupt file, an OOM, a
|
||||
/// wrong-arch GGUF, all land in the status line, never in a panic.
|
||||
pub fn load(entry: &'static ModelEntry, gguf_path: &Path, tokenizer_path: &Path) -> Result<Self, AiError> {
|
||||
let device = Device::Cpu;
|
||||
let mut file = File::open(gguf_path).map_err(AiError::Io)?;
|
||||
// Malformed GGUF metadata (e.g. `general.alignment = 0`) makes
|
||||
// candle's parser panic on a division — hostile input must come
|
||||
// back as an error, not a crash.
|
||||
// Malformed GGUF metadata (e.g. `general.alignment = 0`, or a
|
||||
// zero `attention.head_count` reaching a division in the weight
|
||||
// constructors) makes candle panic — hostile input must come
|
||||
// back as an error, not a crash. The guard covers parsing *and*
|
||||
// weight construction.
|
||||
let weights = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<Weights, AiError> {
|
||||
let content = gguf_file::Content::read(&mut file).map_err(|e| AiError::Inference(format!("bad GGUF: {e}")))?;
|
||||
match entry.arch {
|
||||
Arch::Qwen2 => Ok(Weights::Qwen2(
|
||||
quantized_qwen2::ModelWeights::from_gguf(content, &mut file, &device)
|
||||
.map_err(|e| AiError::Inference(format!("cannot load {}: {e}", entry.file)))?,
|
||||
)),
|
||||
Arch::Llama => Ok(Weights::Llama(
|
||||
quantized_llama::ModelWeights::from_gguf(content, &mut file, &device)
|
||||
.map_err(|e| AiError::Inference(format!("cannot load {}: {e}", entry.file)))?,
|
||||
)),
|
||||
}
|
||||
}))
|
||||
.map_err(|_| AiError::Inference("GGUF loader panicked on malformed metadata".to_owned()))??;
|
||||
let tokenizer = Tokenizer::from_file(tokenizer_path).map_err(|e| AiError::Inference(format!("bad tokenizer.json: {e}")))?;
|
||||
let eos = tokenizer.token_to_id("<|im_end|>").ok_or_else(|| AiError::Inference("tokenizer has no EOS token".into()))?;
|
||||
Ok(Self { weights, tokenizer, device, eos, entry })
|
||||
}
|
||||
|
||||
/// ChatML wrap — Qwen2.5 and SmolLM2 both ship this template.
|
||||
fn chat(prompt: &str) -> String {
|
||||
format!(
|
||||
"<|im_start|>system\nYou are a precise translation engine. Follow the instructions exactly.<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n",
|
||||
)
|
||||
}
|
||||
|
||||
fn generate(&mut self, prompt: &str, opts: &GenOpts) -> Result<String, AiError> {
|
||||
let text = Self::chat(prompt);
|
||||
let encoding = self.tokenizer.encode(text, true).map_err(|e| AiError::Inference(format!("encode failed: {e}")))?;
|
||||
let mut tokens: Vec<u32> = encoding.get_ids().to_vec();
|
||||
|
||||
let temp = if opts.temperature <= 0.02 { None } else { Some(f64::from(opts.temperature)) };
|
||||
let mut sampler = LogitsProcessor::new(SEED, temp, None);
|
||||
|
||||
let mut pos = 0usize;
|
||||
let mut generated: Vec<u32> = Vec::with_capacity(opts.max_tokens.min(4096) as usize);
|
||||
let mut hit_eos = false;
|
||||
for _ in 0..opts.max_tokens {
|
||||
let input_len = tokens.len() - pos;
|
||||
let input =
|
||||
Tensor::new(&tokens[pos..], &self.device).and_then(|t| t.reshape((1, t.dims1()?))).map_err(|e| AiError::Inference(e.to_string()))?;
|
||||
let logits = self.weights.forward(&input, pos).map_err(|e| AiError::Inference(e.to_string()))?;
|
||||
pos += input_len;
|
||||
// `forward` already returns the last sequence position's
|
||||
// logits [batch, vocab] — index the batch row only.
|
||||
let logits = logits.i(0).map_err(|e| AiError::Inference(e.to_string()))?;
|
||||
let next = sampler.sample(&logits).map_err(|e| AiError::Inference(e.to_string()))?;
|
||||
if next == self.eos {
|
||||
hit_eos = true;
|
||||
break;
|
||||
}
|
||||
tokens.push(next);
|
||||
generated.push(next);
|
||||
}
|
||||
// No EOS inside the budget → the reply was cut mid-translation;
|
||||
// partial output would silently truncate the document.
|
||||
if !hit_eos {
|
||||
return Err(AiError::Inference(format!("generation hit max_tokens ({}) without EOS", opts.max_tokens)));
|
||||
}
|
||||
// Decode the generated ids through the tokenizer: byte-level BPE
|
||||
// vocab strings can't be concatenated (spaces/newlines/non-ASCII
|
||||
// bytes stay encoded, and UTF-8 can split across tokens).
|
||||
self.tokenizer.decode(&generated, true).map_err(|e| AiError::Inference(format!("decode failed: {e}")))
|
||||
}
|
||||
}
|
||||
|
||||
impl Provider for LocalGguf {
|
||||
fn name(&self) -> String {
|
||||
format!("local: {}", self.entry.name)
|
||||
}
|
||||
|
||||
fn complete(&mut self, prompt: &str, opts: &GenOpts) -> Result<String, AiError> {
|
||||
self.generate(prompt, opts).map(|s| s.trim().to_string())
|
||||
}
|
||||
|
||||
fn test(&mut self) -> Result<ProviderInfo, AiError> {
|
||||
Ok(ProviderInfo { label: format!("loaded: {}", self.entry.name), models: vec![self.entry.id.to_string()] })
|
||||
}
|
||||
|
||||
fn is_local(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//! Machine capability detection — runs once, cache the result.
|
||||
//!
|
||||
//! GPU/VRAM is deliberately *not* probed here: apps already link wgpu, so
|
||||
//! they read their own adapter info and fill `MachineCaps::vram_gb` after
|
||||
//! calling [`probe`]. Re-probing the GPU would double the surface for a
|
||||
//! bug that reports nonsense VRAM on hybrid laptops.
|
||||
|
||||
/// What this machine can comfortably run.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MachineCaps {
|
||||
/// Total physical RAM, in GiB.
|
||||
pub ram_gb: f32,
|
||||
/// Free space on the volume holding the model cache, in GiB.
|
||||
pub disk_free_gb: f32,
|
||||
/// Logical CPU threads.
|
||||
pub threads: usize,
|
||||
/// AVX2 detected — without it quantized GGUF inference crawls.
|
||||
pub avx2: bool,
|
||||
/// Dedicated VRAM in GiB, when the app supplied it from its wgpu adapter.
|
||||
/// `None` means "unknown or no discrete GPU" — never guessed.
|
||||
pub vram_gb: Option<f32>,
|
||||
}
|
||||
|
||||
impl MachineCaps {
|
||||
/// Rough throughput hint for a model of `size_mb`: tokens/sec on CPU.
|
||||
/// Deliberately conservative — an honest underestimate beats an
|
||||
/// optimistic one that makes the app look slow.
|
||||
pub fn est_tokens_per_sec(&self, size_mb: u64) -> f32 {
|
||||
let base = if self.avx2 { 6.0 } else { 1.2 };
|
||||
// ~0.5B params ≈ 350 MiB Q4; speed scales inversely with size.
|
||||
let scale = 350.0 / (size_mb.max(1) as f32);
|
||||
let thread_boost = (self.threads as f32 / 4.0).min(1.5);
|
||||
(base * scale * thread_boost).max(0.3)
|
||||
}
|
||||
}
|
||||
|
||||
/// Probe the real machine once. `cache_dir` is the future model-cache
|
||||
/// location — disk space is measured on *its* volume, which may not be
|
||||
/// the OS volume.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn probe(cache_dir: &std::path::Path) -> MachineCaps {
|
||||
let mut sys = sysinfo::System::new();
|
||||
sys.refresh_memory();
|
||||
let ram_gb = sys.total_memory() as f32 / (1 << 30) as f32;
|
||||
|
||||
let disk_free_gb = disk_free(cache_dir);
|
||||
|
||||
MachineCaps { ram_gb, disk_free_gb, threads: std::thread::available_parallelism().map_or(1, |n| n.get()), avx2: avx2(), vram_gb: None }
|
||||
}
|
||||
|
||||
#[cfg(all(not(target_arch = "wasm32"), any(target_arch = "x86_64", target_arch = "x86")))]
|
||||
fn avx2() -> bool {
|
||||
std::arch::is_x86_feature_detected!("avx2")
|
||||
}
|
||||
|
||||
#[cfg(all(not(target_arch = "wasm32"), not(any(target_arch = "x86_64", target_arch = "x86"))))]
|
||||
fn avx2() -> bool {
|
||||
// AArch64 NEON is mandatory, so the honest answer is "fast enough".
|
||||
true
|
||||
}
|
||||
|
||||
/// Free bytes on the volume containing `dir`, walking up to the nearest
|
||||
/// existing ancestor if `dir` doesn't exist yet (the common case).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn disk_free(dir: &std::path::Path) -> f32 {
|
||||
// Canonicalize the existing ancestor: a relative or symlinked
|
||||
// `cache_dir` must still match an absolute mount point.
|
||||
let probe: std::path::PathBuf =
|
||||
dir.ancestors().find(|p| p.exists()).and_then(|p| p.canonicalize().ok()).unwrap_or_else(|| std::path::PathBuf::from("."));
|
||||
let disks = sysinfo::Disks::new_with_refreshed_list();
|
||||
// The longest mount point that prefixes our dir owns it.
|
||||
let best = disks.iter().filter(|d| probe.starts_with(d.mount_point())).max_by_key(|d| d.mount_point().as_os_str().len());
|
||||
best.map_or(0.0, |d| d.available_space() as f32 / (1 << 30) as f32)
|
||||
}
|
||||
|
||||
/// The suite-shared model cache (`%LOCALAPPDATA%\artcraft\models` etc.).
|
||||
/// `None` on platforms without a cache dir — the caller should refuse
|
||||
/// local models rather than splashing files into the cwd.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn models_dir() -> Option<std::path::PathBuf> {
|
||||
dirs::cache_dir().map(|d| d.join("artcraft").join("models"))
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
//! Inference backends behind one trait. Apps hold a `Box<dyn Provider>`
|
||||
//! and never care whether the bytes went to OpenAI, Ollama or a local GGUF.
|
||||
|
||||
use crate::error::AiError;
|
||||
|
||||
/// Generation parameters — kept minimal on purpose; the UI exposes
|
||||
/// "translate", not a sampler playground.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GenOpts {
|
||||
/// Max tokens to generate.
|
||||
pub max_tokens: u32,
|
||||
/// Sampling temperature; translation wants ~0.
|
||||
pub temperature: f32,
|
||||
}
|
||||
|
||||
impl Default for GenOpts {
|
||||
fn default() -> Self {
|
||||
Self { max_tokens: 2048, temperature: 0.1 }
|
||||
}
|
||||
}
|
||||
|
||||
/// What `test()` learned about a provider — shown next to the green check.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ProviderInfo {
|
||||
/// Human label, e.g. "Ollama at http://localhost:11434".
|
||||
pub label: String,
|
||||
/// Model ids the endpoint advertised (may be empty if it doesn't say).
|
||||
pub models: Vec<String>,
|
||||
}
|
||||
|
||||
/// An AI backend the suite can use. `Send` so apps can move a loaded
|
||||
/// provider onto a worker thread for long generations.
|
||||
pub trait Provider: Send {
|
||||
/// Short name for the status line ("local: Qwen2.5 1.5B", "OpenAI-compatible").
|
||||
fn name(&self) -> String;
|
||||
/// One completion. Prompt construction is the caller's job — this
|
||||
/// trait is deliberately text-in/text-out so every consumer (translate,
|
||||
/// summarize, custom prompt) shares one path.
|
||||
fn complete(&mut self, prompt: &str, opts: &GenOpts) -> Result<String, AiError>;
|
||||
/// Can we reach it? Never sends document text — just a handshake.
|
||||
fn test(&mut self) -> Result<ProviderInfo, AiError>;
|
||||
/// True when document text stays on this machine.
|
||||
fn is_local(&self) -> bool;
|
||||
}
|
||||
|
||||
/// Any OpenAI-compatible `/v1/chat/completions` endpoint: OpenAI, Azure,
|
||||
/// Ollama (`http://localhost:11434/v1`), LM Studio, Groq, OpenRouter…
|
||||
/// The token is a plain string — storage (OS keyring, settings file) is
|
||||
/// the app's choice, and it is never logged or echoed into errors.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub struct OpenAiCompat {
|
||||
/// Base URL ending at the API root, e.g. `https://api.openai.com/v1`.
|
||||
pub endpoint: String,
|
||||
/// Bearer token; `None` for unauthenticated local endpoints (Ollama).
|
||||
pub token: Option<String>,
|
||||
/// Model id sent in every request.
|
||||
pub model: String,
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
impl OpenAiCompat {
|
||||
pub fn new(endpoint: impl Into<String>, token: Option<String>, model: impl Into<String>) -> Self {
|
||||
Self { endpoint: endpoint.into(), token, model: model.into() }
|
||||
}
|
||||
|
||||
fn url(&self, path: &str) -> String {
|
||||
format!("{}/{}", self.endpoint.trim_end_matches('/'), path)
|
||||
}
|
||||
|
||||
/// ureq sets no timeouts by default — an endpoint that accepts the
|
||||
/// socket and never replies would block the caller forever, so every
|
||||
/// request goes through an agent with finite deadlines. The response
|
||||
/// budgets are generous: a non-streamed chat completion only sends
|
||||
/// headers once generation is done, which can take minutes.
|
||||
fn agent() -> ureq::Agent {
|
||||
ureq::Agent::config_builder()
|
||||
.timeout_connect(Some(std::time::Duration::from_secs(15)))
|
||||
.timeout_send_request(Some(std::time::Duration::from_secs(30)))
|
||||
.timeout_send_body(Some(std::time::Duration::from_secs(60)))
|
||||
.timeout_recv_response(Some(std::time::Duration::from_secs(300)))
|
||||
.timeout_recv_body(Some(std::time::Duration::from_secs(300)))
|
||||
.build()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn auth(&self, req: ureq::RequestBuilder<ureq::typestate::WithBody>) -> ureq::RequestBuilder<ureq::typestate::WithBody> {
|
||||
match &self.token {
|
||||
Some(t) if !t.is_empty() => req.header("Authorization", format!("Bearer {t}")),
|
||||
_ => req,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
impl Provider for OpenAiCompat {
|
||||
fn name(&self) -> String {
|
||||
format!("{} ({})", self.model, self.endpoint)
|
||||
}
|
||||
|
||||
fn complete(&mut self, prompt: &str, opts: &GenOpts) -> Result<String, AiError> {
|
||||
if self.endpoint.trim().is_empty() {
|
||||
return Err(AiError::NotConfigured);
|
||||
}
|
||||
let body = serde_json::json!({
|
||||
"model": self.model,
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"temperature": opts.temperature,
|
||||
"max_tokens": opts.max_tokens,
|
||||
});
|
||||
let mut resp =
|
||||
self.auth(Self::agent().post(self.url("chat/completions"))).header("Content-Type", "application/json").send_json(body).map_err(|e| {
|
||||
match e {
|
||||
ureq::Error::StatusCode(401 | 403) => AiError::Rejected("authentication failed — check the API token".into()),
|
||||
ureq::Error::StatusCode(404) => AiError::Rejected("model or endpoint not found — check both fields".into()),
|
||||
ureq::Error::StatusCode(c) => AiError::Rejected(format!("HTTP {c}")),
|
||||
other => AiError::Http(other.to_string()),
|
||||
}
|
||||
})?;
|
||||
let v: serde_json::Value = resp.body_mut().read_json().map_err(|e| AiError::BadResponse(e.to_string()))?;
|
||||
// A completion cut off at max_tokens ("length") or aborted is not
|
||||
// a translation — returning it would silently truncate the doc.
|
||||
match v.pointer("/choices/0/finish_reason").and_then(serde_json::Value::as_str) {
|
||||
Some("stop") | Some("tool_calls") | None => {}
|
||||
Some(reason) => return Err(AiError::BadResponse(format!("incomplete completion (finish_reason={reason})"))),
|
||||
}
|
||||
v.pointer("/choices/0/message/content")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::to_string)
|
||||
.ok_or_else(|| AiError::BadResponse("no choices[0].message.content in response".into()))
|
||||
}
|
||||
|
||||
fn test(&mut self) -> Result<ProviderInfo, AiError> {
|
||||
if self.endpoint.trim().is_empty() {
|
||||
return Err(AiError::NotConfigured);
|
||||
}
|
||||
// GET /v1/models — a handshake only, no user text leaves the app.
|
||||
let mut req = Self::agent().get(self.url("models"));
|
||||
if let Some(t) = &self.token
|
||||
&& !t.is_empty()
|
||||
{
|
||||
req = req.header("Authorization", format!("Bearer {t}"));
|
||||
}
|
||||
let mut resp = req.call().map_err(|e| match e {
|
||||
ureq::Error::StatusCode(401 | 403) => AiError::Rejected("authentication failed — check the API token".into()),
|
||||
ureq::Error::StatusCode(c) => AiError::Rejected(format!("HTTP {c}")),
|
||||
other => AiError::Http(other.to_string()),
|
||||
})?;
|
||||
let v: serde_json::Value = resp.body_mut().read_json().map_err(|e| AiError::BadResponse(e.to_string()))?;
|
||||
let models = v
|
||||
.get("data")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.map(|arr| arr.iter().filter_map(|m| m.get("id").and_then(serde_json::Value::as_str).map(str::to_string)).collect())
|
||||
.unwrap_or_default();
|
||||
Ok(ProviderInfo { label: format!("reachable: {}", self.endpoint), models })
|
||||
}
|
||||
|
||||
fn is_local(&self) -> bool {
|
||||
// Local endpoints keep text on the machine; we can't verify a
|
||||
// remote URL's forwarding, so honesty means: loopback → local.
|
||||
// Parse the authority properly — a prefix match would call
|
||||
// `localhost.example.com` or `127.0.0.1.example.com` "local"
|
||||
// while document text leaves the machine.
|
||||
let e = self.endpoint.trim();
|
||||
let after_scheme = e.split_once("://").map_or(e, |(_, rest)| rest);
|
||||
let authority = after_scheme.split(['/', '?', '#']).next().unwrap_or("");
|
||||
let host_port = authority.rsplit('@').next().unwrap_or(""); // strip userinfo
|
||||
let host = if let Some(rest) = host_port.strip_prefix('[') {
|
||||
rest.split(']').next().unwrap_or("")
|
||||
} else {
|
||||
host_port.split(':').next().unwrap_or("")
|
||||
};
|
||||
host.eq_ignore_ascii_case("localhost") || host.parse::<std::net::IpAddr>().is_ok_and(|ip| ip.is_loopback())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn is_local_accepts_only_real_loopback() {
|
||||
let local = |ep: &str| OpenAiCompat::new(ep, None, "m").is_local();
|
||||
assert!(local("http://localhost:11434/v1"));
|
||||
assert!(local("https://localhost/v1"));
|
||||
assert!(local("http://127.0.0.1:8080/v1"));
|
||||
assert!(local("http://127.99.0.1/v1")); // whole 127/8 is loopback
|
||||
assert!(local("http://[::1]:11434/v1"));
|
||||
// Lookalike hostnames must NOT report local — document text would
|
||||
// leave the machine while the UI promises it stays.
|
||||
assert!(!local("https://localhost.example.com/v1"));
|
||||
assert!(!local("https://127.0.0.1.example.com/v1"));
|
||||
assert!(!local("https://localhost.evil.io/v1"));
|
||||
assert!(!local("https://api.openai.com/v1"));
|
||||
assert!(!local("https://example.com/localhost/v1"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
//! The curated model registry — a hand-maintained table, compiled in.
|
||||
//! Runtime-fetching this list would make our supply chain un-auditable.
|
||||
//!
|
||||
//! Curation rule: **permissive licenses only** (the suite is MIT OR
|
||||
//! Apache-2.0 and commercial users run it). CC-BY-NC models like NLLB-200
|
||||
//! are excluded by policy; their absence is deliberate.
|
||||
//!
|
||||
//! `sha256` may be `None`: Hugging Face serves the LFS digest in the
|
||||
//! `X-Linked-ETag` header of the resolve redirect, which [`crate::fetch`]
|
||||
//! uses as the expected checksum. We only hard-code a hash when we've
|
||||
//! verified it ourselves — never a guessed one.
|
||||
|
||||
use crate::probe::MachineCaps;
|
||||
|
||||
/// Model quality tier — higher is better output, bigger download.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum Quality {
|
||||
Tiny,
|
||||
Small,
|
||||
Medium,
|
||||
}
|
||||
|
||||
/// Which candle architecture loads the GGUF.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Arch {
|
||||
Qwen2,
|
||||
Llama,
|
||||
}
|
||||
|
||||
/// One downloadable model.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ModelEntry {
|
||||
/// Stable identifier stored in settings (`"qwen2.5-1.5b-q4km"`).
|
||||
pub id: &'static str,
|
||||
/// Display name for the picker.
|
||||
pub name: &'static str,
|
||||
/// Canonical download URL (Hugging Face `resolve` endpoint).
|
||||
pub url: &'static str,
|
||||
/// Filename inside the model cache.
|
||||
pub file: &'static str,
|
||||
/// Companion tokenizer (needed by local inference; the GGUF ships
|
||||
/// vocab only, not the chat-time tokenizer.json).
|
||||
pub tokenizer_url: &'static str,
|
||||
/// Verified SHA-256 of the GGUF, when we have one.
|
||||
pub sha256: Option<&'static str>,
|
||||
/// Download size, MiB — shown to the user before they commit.
|
||||
pub size_mb: u64,
|
||||
/// Below this RAM the model is refused outright.
|
||||
pub min_ram_gb: f32,
|
||||
/// Recommended RAM for comfortable use.
|
||||
pub rec_ram_gb: f32,
|
||||
/// SPDX license id, displayed in the UI.
|
||||
pub license: &'static str,
|
||||
pub quality: Quality,
|
||||
pub arch: Arch,
|
||||
/// Human-readable language coverage for the picker.
|
||||
pub langs: &'static str,
|
||||
}
|
||||
|
||||
/// The registry, smallest first.
|
||||
pub static MODELS: &[ModelEntry] = &[
|
||||
ModelEntry {
|
||||
id: "qwen2.5-0.5b-q4km",
|
||||
name: "Qwen2.5 0.5B Instruct (Q4_K_M)",
|
||||
url: "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf",
|
||||
file: "qwen2.5-0.5b-instruct-q4_k_m.gguf",
|
||||
tokenizer_url: "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct/resolve/main/tokenizer.json",
|
||||
sha256: None,
|
||||
size_mb: 389,
|
||||
min_ram_gb: 2.0,
|
||||
rec_ram_gb: 4.0,
|
||||
license: "Apache-2.0",
|
||||
quality: Quality::Tiny,
|
||||
arch: Arch::Qwen2,
|
||||
langs: "29+ languages incl. EN/ZH/FR/DE/ES/JA/KO/RU",
|
||||
},
|
||||
ModelEntry {
|
||||
id: "smollm2-1.7b-q4km",
|
||||
name: "SmolLM2 1.7B Instruct (Q4_K_M)",
|
||||
url: "https://huggingface.co/bartowski/SmolLM2-1.7B-Instruct-GGUF/resolve/main/SmolLM2-1.7B-Instruct-Q4_K_M.gguf",
|
||||
file: "SmolLM2-1.7B-Instruct-Q4_K_M.gguf",
|
||||
tokenizer_url: "https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct/resolve/main/tokenizer.json",
|
||||
sha256: None,
|
||||
size_mb: 1033,
|
||||
min_ram_gb: 4.0,
|
||||
rec_ram_gb: 8.0,
|
||||
license: "Apache-2.0",
|
||||
quality: Quality::Small,
|
||||
arch: Arch::Llama,
|
||||
langs: "primarily EN/FR/DE/ES/IT",
|
||||
},
|
||||
ModelEntry {
|
||||
id: "qwen2.5-1.5b-q4km",
|
||||
name: "Qwen2.5 1.5B Instruct (Q4_K_M)",
|
||||
url: "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf",
|
||||
file: "qwen2.5-1.5b-instruct-q4_k_m.gguf",
|
||||
tokenizer_url: "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct/resolve/main/tokenizer.json",
|
||||
sha256: None,
|
||||
size_mb: 986,
|
||||
min_ram_gb: 4.0,
|
||||
rec_ram_gb: 8.0,
|
||||
license: "Apache-2.0",
|
||||
quality: Quality::Small,
|
||||
arch: Arch::Qwen2,
|
||||
langs: "29+ languages incl. EN/ZH/FR/DE/ES/JA/KO/RU",
|
||||
},
|
||||
// NOTE: Qwen2.5-3B was removed — its `qwen-research` license is
|
||||
// non-commercial and breaks the permissive-only rule above. No
|
||||
// permissively-licensed Medium-tier candidate is currently curated.
|
||||
];
|
||||
|
||||
/// Look up a model by stable id.
|
||||
pub fn by_id(id: &str) -> Option<&'static ModelEntry> {
|
||||
MODELS.iter().find(|m| m.id == id)
|
||||
}
|
||||
|
||||
/// Whether `entry` can run at all on this machine.
|
||||
pub fn fits(caps: &MachineCaps, entry: &ModelEntry) -> bool {
|
||||
let ram_ok = caps.ram_gb >= entry.min_ram_gb;
|
||||
let disk_ok = caps.disk_free_gb >= entry.size_mb as f32 / 1024.0 * 1.3; // file + headroom
|
||||
ram_ok && disk_ok
|
||||
}
|
||||
|
||||
/// The default suggestion for `caps`: best quality that fits, preferring
|
||||
/// 3B on roomy machines, 1.5B mid-range, 0.5B on small ones. Returns
|
||||
/// `None` when nothing fits — the UI then says so honestly instead of
|
||||
/// offering a download that can't run.
|
||||
pub fn suggest(caps: &MachineCaps) -> Option<&'static ModelEntry> {
|
||||
let capable: Vec<&ModelEntry> = MODELS.iter().filter(|m| fits(caps, m)).collect();
|
||||
let best_allowed = if caps.vram_gb.is_some_and(|v| v >= 6.0) || caps.ram_gb >= 16.0 {
|
||||
Quality::Medium
|
||||
} else if caps.ram_gb >= 8.0 {
|
||||
Quality::Small
|
||||
} else {
|
||||
Quality::Tiny
|
||||
};
|
||||
capable.into_iter().filter(|m| m.quality <= best_allowed).max_by_key(|m| m.quality)
|
||||
}
|
||||
@@ -0,0 +1,525 @@
|
||||
//! Document translation as *segments*, never raw text: the model sees
|
||||
//! only translatable runs, and protected spans (URLs, placeholders,
|
||||
//! markup, numbers-only runs) come back byte-identical because we never
|
||||
//! sent them.
|
||||
//!
|
||||
//! Each app declares its grammar via [`TranslateGrammar`]; the default
|
||||
//! [`PdfGrammar`] suits extracted PDF text.
|
||||
|
||||
use crate::error::AiError;
|
||||
use crate::provider::{GenOpts, Provider};
|
||||
|
||||
/// A unit of text the translator decided on.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Segment {
|
||||
/// Sent to the model; replace with the translation.
|
||||
Text(String),
|
||||
/// Never sent; re-inserted verbatim.
|
||||
Protected(String),
|
||||
}
|
||||
|
||||
impl Segment {
|
||||
/// The source text, translated or not.
|
||||
pub fn source(&self) -> &str {
|
||||
match self {
|
||||
Self::Text(s) | Self::Protected(s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Which runs of a document's text must never be translated.
|
||||
pub trait TranslateGrammar {
|
||||
/// True when `token` (already delimited — a word, symbol, or
|
||||
/// whitespace-free run) is structural and must survive verbatim.
|
||||
fn is_protected(&self, token: &str) -> bool;
|
||||
}
|
||||
|
||||
/// Grammar for text extracted from a PDF: protects URLs, e-mail
|
||||
/// addresses, `{placeholders}`, `<markup>`, and runs with no letters
|
||||
/// (page furniture, digits, punctuation).
|
||||
pub struct PdfGrammar;
|
||||
|
||||
impl TranslateGrammar for PdfGrammar {
|
||||
fn is_protected(&self, token: &str) -> bool {
|
||||
is_protected_token(token)
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared protected-token test, useful to other apps' grammars.
|
||||
pub fn is_protected_token(token: &str) -> bool {
|
||||
let t = token.trim();
|
||||
if t.is_empty() {
|
||||
return true;
|
||||
}
|
||||
if !t.chars().any(|c| c.is_alphabetic()) {
|
||||
return true; // numbers, punctuation, symbols
|
||||
}
|
||||
let lower = t.to_ascii_lowercase();
|
||||
lower.starts_with("http://")
|
||||
|| lower.starts_with("https://")
|
||||
|| lower.starts_with("www.")
|
||||
|| lower.contains('@') && lower.contains('.') && !lower.contains(' ')
|
||||
|| (t.starts_with('{') && t.ends_with('}'))
|
||||
|| (t.starts_with('<') && t.ends_with('>'))
|
||||
}
|
||||
|
||||
/// Split `text` into segments on whitespace boundaries, grouping
|
||||
/// consecutive protected or translatable tokens. `<markup>` spans are
|
||||
/// carved out first — a tag with attributes contains whitespace, and a
|
||||
/// whole `<em>Hello</em>` run must keep "Hello" translatable while the
|
||||
/// tags themselves stay byte-identical.
|
||||
pub fn segment(text: &str, grammar: &dyn TranslateGrammar) -> Vec<Segment> {
|
||||
fn flush(out: &mut Vec<Segment>, buf: &mut String, protected: bool) {
|
||||
if buf.is_empty() {
|
||||
return;
|
||||
}
|
||||
let seg = if protected { Segment::Protected(std::mem::take(buf)) } else { Segment::Text(std::mem::take(buf)) };
|
||||
out.push(seg);
|
||||
}
|
||||
|
||||
let mut out: Vec<Segment> = Vec::new();
|
||||
let mut buf = String::new();
|
||||
let mut buf_protected = false;
|
||||
let mut started = false;
|
||||
|
||||
for (token, is_tag) in split_tokens(text) {
|
||||
let ws = token.chars().all(char::is_whitespace);
|
||||
// Whitespace glues to whatever the current run is; markup tags
|
||||
// are always protected.
|
||||
let this_protected = if ws { buf_protected } else { is_tag || grammar.is_protected(token) };
|
||||
if started && this_protected != buf_protected {
|
||||
flush(&mut out, &mut buf, buf_protected);
|
||||
}
|
||||
buf_protected = this_protected;
|
||||
started = true;
|
||||
buf.push_str(token);
|
||||
}
|
||||
flush(&mut out, &mut buf, buf_protected);
|
||||
out
|
||||
}
|
||||
|
||||
/// Tokenize into `(slice, is_tag)`: markup tags are whole `<…>` spans
|
||||
/// (attributes included), everything else alternates between maximal
|
||||
/// whitespace and non-whitespace runs.
|
||||
fn split_tokens(text: &str) -> Vec<(&str, bool)> {
|
||||
let mut out = Vec::new();
|
||||
let mut pos = 0usize;
|
||||
let bytes = text.as_bytes();
|
||||
while pos < bytes.len() {
|
||||
if bytes[pos] == b'<'
|
||||
&& let Some(tag_len) = tag_len(&text[pos..])
|
||||
{
|
||||
out.push((&text[pos..pos + tag_len], true));
|
||||
pos += tag_len;
|
||||
continue;
|
||||
}
|
||||
// Advance a non-tag run up to the next '<' that starts a tag;
|
||||
// bare '<' characters just stay inside the run.
|
||||
let mut scan = pos;
|
||||
let mut run_end = bytes.len();
|
||||
while let Some(lt) = bytes[scan..].iter().position(|&b| b == b'<') {
|
||||
let at = scan + lt;
|
||||
if tag_len(&text[at..]).is_some() {
|
||||
run_end = at;
|
||||
break;
|
||||
}
|
||||
scan = at + 1;
|
||||
}
|
||||
for piece in split_words_ws(&text[pos..run_end]) {
|
||||
out.push((piece, false));
|
||||
}
|
||||
pos = run_end;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Length of a `<…>` tag starting at `s[0]`, or `None` when `<` does not
|
||||
/// begin markup (bare `<`, `<3`, `< hello`). Tags start `</`, `<?`, `<!`
|
||||
/// or `<`+letter and close at the first `>` before any `<` or newline;
|
||||
/// the scan is capped so pathological input can't stall it.
|
||||
fn tag_len(s: &str) -> Option<usize> {
|
||||
const MAX_TAG: usize = 512;
|
||||
let second = s.as_bytes().get(1)?;
|
||||
let opener_ok = second.is_ascii_alphabetic() || matches!(second, b'/' | b'!' | b'?');
|
||||
if !opener_ok {
|
||||
return None;
|
||||
}
|
||||
s.char_indices().skip(2).take(MAX_TAG).find(|(_, c)| matches!(c, '>' | '<' | '\n')).and_then(|(i, c)| (c == '>').then_some(i + 1))
|
||||
}
|
||||
|
||||
/// Split into alternating word/whitespace runs (like `split_inclusive`
|
||||
/// but grouping every maximal run of whitespace or non-whitespace).
|
||||
fn split_words_ws(text: &str) -> Vec<&str> {
|
||||
let mut out = Vec::new();
|
||||
let mut start = 0usize;
|
||||
let mut prev_ws: Option<bool> = None;
|
||||
for (i, c) in text.char_indices() {
|
||||
let ws = c.is_whitespace();
|
||||
if let Some(p) = prev_ws
|
||||
&& p != ws
|
||||
{
|
||||
out.push(&text[start..i]);
|
||||
start = i;
|
||||
}
|
||||
prev_ws = Some(ws);
|
||||
}
|
||||
if start < text.len() {
|
||||
out.push(&text[start..]);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// How many segments one prompt carries — keeps responses snappy and
|
||||
/// makes a numbering slip cheap to retry.
|
||||
const BATCH: usize = 20;
|
||||
/// …and how many bytes one prompt carries. A document with no protected
|
||||
/// tokens is one `Text` segment of arbitrary length — without a size
|
||||
/// bound the request can exceed `max_tokens` and come back truncated
|
||||
/// (which providers must reject, not return as success).
|
||||
const BATCH_BYTES: usize = 4 * 1024;
|
||||
/// A single `Text` run larger than this is chopped into pieces before
|
||||
/// batching, so no one request is unbounded.
|
||||
const RUN_BYTES: usize = 3 * 1024;
|
||||
|
||||
/// A translatable piece of one segment: the whitespace-stripped `core`
|
||||
/// is sent to the model, `lead`/`trail` are restored verbatim around the
|
||||
/// translation so `Visitez` never glues to `https://a.io`.
|
||||
struct Run<'a> {
|
||||
seg: usize,
|
||||
lead: &'a str,
|
||||
core: &'a str,
|
||||
trail: &'a str,
|
||||
/// False for whitespace-only pieces: never sent to the model, but
|
||||
/// still reassembled into the segment.
|
||||
send: bool,
|
||||
}
|
||||
|
||||
/// `(leading ws, core, trailing ws)` of `s`, all on char boundaries.
|
||||
fn ws_edges(s: &str) -> (&str, &str, &str) {
|
||||
let lead = s.len() - s.trim_start().len();
|
||||
let tail = s.len() - s.trim_end().len();
|
||||
if lead + tail >= s.len() {
|
||||
return (s, "", ""); // all-whitespace run: nothing to translate
|
||||
}
|
||||
(&s[..lead], &s[lead..s.len() - tail], &s[s.len() - tail..])
|
||||
}
|
||||
|
||||
/// Chop `s` into ≤ `max`-byte pieces on whitespace boundaries (the last
|
||||
/// ws before the limit; a run with no ws is hard-cut at the limit). The
|
||||
/// boundary whitespace stays inside the pieces — nothing is lost.
|
||||
fn split_run(s: &str, max: usize) -> Vec<&str> {
|
||||
let mut out = Vec::new();
|
||||
let mut rest = s;
|
||||
while rest.len() > max {
|
||||
let mut ws_cut = None;
|
||||
let mut boundary = 0usize;
|
||||
for (i, c) in rest.char_indices() {
|
||||
if i >= max {
|
||||
break;
|
||||
}
|
||||
boundary = i + c.len_utf8();
|
||||
if c.is_whitespace() {
|
||||
ws_cut = Some(i); // cut *before* the ws — it leads the next piece
|
||||
}
|
||||
}
|
||||
let cut = match ws_cut.filter(|w| *w > max / 4) {
|
||||
Some(w) => w,
|
||||
None => boundary.max(1),
|
||||
};
|
||||
out.push(&rest[..cut]);
|
||||
rest = &rest[cut..];
|
||||
}
|
||||
if !rest.is_empty() {
|
||||
out.push(rest);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Translate every `Text` segment to `target_lang`, leaving `Protected`
|
||||
/// untouched. Batched with numbered items; a count mismatch retries the
|
||||
/// offending half rather than misaligning the whole document.
|
||||
pub fn translate_segments(provider: &mut dyn Provider, segs: &[Segment], target_lang: &str) -> Result<Vec<String>, AiError> {
|
||||
// Source whitespace never reaches the model: each Text segment is
|
||||
// split edge-to-edge, and oversized runs are chopped further.
|
||||
let mut runs: Vec<Run<'_>> = Vec::new();
|
||||
for (i, s) in segs.iter().enumerate() {
|
||||
let Segment::Text(src) = s else { continue };
|
||||
for piece in split_run(src, RUN_BYTES) {
|
||||
let (lead, core, trail) = ws_edges(piece);
|
||||
// Pure-whitespace pieces (possible after a long run is chopped)
|
||||
// keep their bytes in the output but skip the provider.
|
||||
runs.push(Run { seg: i, lead, core, trail, send: !core.is_empty() });
|
||||
}
|
||||
}
|
||||
let run_segs: std::collections::HashSet<usize> = runs.iter().map(|r| r.seg).collect();
|
||||
|
||||
let mut out: Vec<String> = segs.iter().map(|s| s.source().to_string()).collect();
|
||||
for &i in &run_segs {
|
||||
out[i].clear(); // rebuilt from translated runs below
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < runs.len() {
|
||||
// Whitespace-only pieces restore verbatim, in order, and never
|
||||
// reach the model.
|
||||
while i < runs.len() && !runs[i].send {
|
||||
let r = &runs[i];
|
||||
out[r.seg].push_str(r.lead);
|
||||
out[r.seg].push_str(r.trail);
|
||||
i += 1;
|
||||
}
|
||||
// A batch is bounded by item count *and* total prompt bytes.
|
||||
let mut bytes = 0usize;
|
||||
let start = i;
|
||||
while i < runs.len() && runs[i].send && i - start < BATCH && bytes + runs[i].core.len() <= BATCH_BYTES {
|
||||
bytes += runs[i].core.len();
|
||||
i += 1;
|
||||
}
|
||||
if i == start {
|
||||
if i < runs.len() {
|
||||
i += 1; // never spin on an oversized single item
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let chunk = &runs[start..i];
|
||||
let translated = translate_batch(provider, chunk, target_lang)?;
|
||||
for (run, t) in chunk.iter().zip(translated) {
|
||||
out[run.seg].push_str(run.lead);
|
||||
out[run.seg].push_str(&t);
|
||||
out[run.seg].push_str(run.trail);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// One numbered batch; splits recursively on count mismatch so a single
|
||||
/// malformed reply can't corrupt neighbors.
|
||||
fn translate_batch(provider: &mut dyn Provider, runs: &[Run<'_>], lang: &str) -> Result<Vec<String>, AiError> {
|
||||
if runs.len() == 1 {
|
||||
return Ok(vec![translate_one(provider, runs[0].core, lang)?]);
|
||||
}
|
||||
let prompt = numbered_prompt(&runs.iter().map(|r| r.core.to_string()).collect::<Vec<_>>(), lang);
|
||||
let reply = provider.complete(&prompt, &GenOpts::default())?;
|
||||
match parse_numbered(&reply, runs.len()) {
|
||||
Some(v) => Ok(v),
|
||||
None => {
|
||||
let mid = runs.len() / 2;
|
||||
let mut a = translate_batch(provider, &runs[..mid], lang)?;
|
||||
let b = translate_batch(provider, &runs[mid..], lang)?;
|
||||
a.extend(b);
|
||||
Ok(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_one(provider: &mut dyn Provider, source: &str, lang: &str) -> Result<String, AiError> {
|
||||
let prompt = format!("Translate the following text to {lang}. Reply with ONLY the translation, no quotes, no notes.\n\n{source}");
|
||||
Ok(provider.complete(&prompt, &GenOpts::default())?.trim().to_string())
|
||||
}
|
||||
|
||||
/// `"1. …"`-style numbered prompt — models follow it far more reliably
|
||||
/// than delimiter schemes.
|
||||
fn numbered_prompt(sources: &[String], lang: &str) -> String {
|
||||
let mut p = format!(
|
||||
"Translate each numbered item to {lang}. Keep the numbering. \
|
||||
Preserve all formatting, placeholders like {{name}}, URLs and markup verbatim. \
|
||||
Reply with ONLY the numbered translations.\n\n"
|
||||
);
|
||||
for (i, s) in sources.iter().enumerate() {
|
||||
p.push_str(&format!("{}. {}\n", i + 1, s.replace('\n', " ")));
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
/// Parse `"1. …\n2. …"` back into items; `None` when the reply is
|
||||
/// malformed — duplicates, out-of-range numbers, or a missing item all
|
||||
/// mean the numbering slipped, and the caller retries/splits rather
|
||||
/// than accepting corrupted text. Non-numbered lines after an item are
|
||||
/// continuations of it; non-numbered chatter before item 1 is rejected
|
||||
/// the same way, so nothing is silently dropped.
|
||||
fn parse_numbered(reply: &str, want: usize) -> Option<Vec<String>> {
|
||||
let mut out = vec![String::new(); want];
|
||||
let mut seen = vec![false; want];
|
||||
let mut current: Option<usize> = None;
|
||||
for line in reply.lines() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
match numbered_line(line) {
|
||||
Some(idx) if idx >= 1 && idx <= want => {
|
||||
if seen[idx - 1] {
|
||||
return None; // "1. un\n2. deux\n1. trois" overwrites — reject
|
||||
}
|
||||
seen[idx - 1] = true;
|
||||
let rest = line.split_once(['.', ')']).map_or("", |(_, r)| r).trim().to_string();
|
||||
out[idx - 1] = rest;
|
||||
current = Some(idx - 1);
|
||||
}
|
||||
Some(_) => return None, // numbered but out of range — malformed reply
|
||||
// A wrapped/multi-line item: keep the continuation, not
|
||||
// "1. Première phrase.\nSuite" dropping "Suite". Unnumbered
|
||||
// preamble is rejected — not our format.
|
||||
None => {
|
||||
let i = current?;
|
||||
out[i].push('\n');
|
||||
out[i].push_str(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
(seen.iter().all(|s| *s) && out.iter().all(|s| !s.is_empty())).then_some(out)
|
||||
}
|
||||
|
||||
/// `Some(idx)` when `line` opens as `"12."` / `"12)"` — a 1-3 digit
|
||||
/// number followed by `'.'` or `')'`. Anything else is `None`, so
|
||||
/// "Section 2. notes" is a continuation, not item 2.
|
||||
fn numbered_line(line: &str) -> Option<usize> {
|
||||
let line = line.trim_start();
|
||||
let digits = line.bytes().take_while(|b| b.is_ascii_digit()).count();
|
||||
if digits == 0 || digits > 3 || !matches!(line.as_bytes().get(digits), Some(b'.' | b')')) {
|
||||
return None;
|
||||
}
|
||||
line.get(..digits)?.parse().ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn split_words_ws_groups_runs() {
|
||||
assert_eq!(split_words_ws("a b\tc"), vec!["a", " ", "b", "\t", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_grammar_protects() {
|
||||
let g = PdfGrammar;
|
||||
assert!(g.is_protected("https://x.io/a"));
|
||||
assert!(g.is_protected("{count}"));
|
||||
assert!(g.is_protected("42.5"));
|
||||
assert!(!g.is_protected("Bonjour"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn segmentation_glues_whitespace_and_protects_spans() {
|
||||
let segs = segment("Visit https://a.io now {n} times", &PdfGrammar);
|
||||
// "Visit " text, protected url, " now " text, protected {n}, " times" text
|
||||
let texts: Vec<&Segment> = segs.iter().filter(|s| matches!(s, Segment::Text(_))).collect();
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.trim() == "https://a.io")));
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.trim() == "{n}")));
|
||||
assert!(texts.iter().any(|s| s.source().contains("Visit")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbered_parse_roundtrip() {
|
||||
let r = "1. un\n2. deux\n3. trois";
|
||||
assert_eq!(parse_numbered(r, 3), Some(vec!["un".into(), "deux".into(), "trois".into()]));
|
||||
assert_eq!(parse_numbered("1. un\n3. trois", 3), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbered_parse_rejects_duplicates_and_overflow() {
|
||||
assert_eq!(parse_numbered("1. un\n2. deux\n1. trois", 2), None);
|
||||
assert_eq!(parse_numbered("1. un\n2. deux\n3. trois", 2), None);
|
||||
assert_eq!(parse_numbered("0. zero\n1. un\n2. deux", 2), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbered_parse_keeps_continuation_lines() {
|
||||
let r = "1. Première phrase.\nSuite du paragraphe\n2. Deuxième élément.";
|
||||
assert_eq!(parse_numbered(r, 2), Some(vec!["Première phrase.\nSuite du paragraphe".into(), "Deuxième élément.".into()]));
|
||||
// Preamble chatter that isn't a numbered item is rejected, not
|
||||
// silently dropped.
|
||||
assert_eq!(parse_numbered("Sure, here you go:\n1. un\n2. deux", 2), None);
|
||||
// "Section 2. notes" looks numbered but isn't → continuation.
|
||||
let r2 = "1. un\nSection 2. notes\n2. deux";
|
||||
assert_eq!(parse_numbered(r2, 2), Some(vec!["un\nSection 2. notes".into(), "deux".into()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn markup_tags_split_from_their_text() {
|
||||
let segs = segment("<em>Hello</em> world", &PdfGrammar);
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p == "<em>")));
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.trim() == "</em>")));
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Text(t) if t.contains("Hello"))));
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Text(t) if t.contains("world"))));
|
||||
// Tags containing spaces stay whole and protected.
|
||||
let segs = segment("<a href=\"x\">link</a>", &PdfGrammar);
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p == "<a href=\"x\">")));
|
||||
// A bare '<' is text, not a tag opener — words around it stay
|
||||
// translatable (the lone '<' itself is protected as punctuation).
|
||||
let segs = segment("a < b and <3 hearts", &PdfGrammar);
|
||||
assert!(segs.iter().any(|s| matches!(s, Segment::Text(t) if t.contains("hearts"))));
|
||||
assert!(!segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.contains('<') && p.contains('>'))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ws_edges_and_split_run_preserve_every_byte() {
|
||||
assert_eq!(ws_edges(" héllo \n"), (" ", "héllo", " \n"));
|
||||
assert_eq!(ws_edges(" "), (" ", "", ""));
|
||||
let s = "alpha beta gamma delta epsilon";
|
||||
let pieces = split_run(s, 12);
|
||||
assert!(pieces.iter().all(|p| p.len() <= 12));
|
||||
assert_eq!(pieces.concat(), s); // boundary whitespace survives
|
||||
// A run with no whitespace is hard-cut at a char boundary.
|
||||
let long = "x".repeat(50);
|
||||
let pieces = split_run(&long, 20);
|
||||
assert!(pieces.iter().all(|p| p.len() <= 20));
|
||||
assert_eq!(pieces.concat(), long);
|
||||
}
|
||||
|
||||
/// A provider that echoes numbered replies — exercises the batching,
|
||||
/// whitespace restore and reassembly end to end without a network.
|
||||
struct Echo;
|
||||
|
||||
impl Provider for Echo {
|
||||
fn name(&self) -> String {
|
||||
"echo".into()
|
||||
}
|
||||
|
||||
fn complete(&mut self, prompt: &str, _opts: &GenOpts) -> Result<String, AiError> {
|
||||
if prompt.starts_with("Translate each numbered item") {
|
||||
// Parse our own prompt's numbering and echo each item.
|
||||
let body = prompt.split("\n\n").nth(1).unwrap_or("");
|
||||
let mut out = String::new();
|
||||
for line in body.lines() {
|
||||
out.push_str(line);
|
||||
out.push('\n');
|
||||
}
|
||||
return Ok(out);
|
||||
}
|
||||
Ok(prompt.rsplit("\n\n").next().unwrap_or(prompt).to_string())
|
||||
}
|
||||
|
||||
fn test(&mut self) -> Result<crate::provider::ProviderInfo, AiError> {
|
||||
Ok(crate::provider::ProviderInfo { label: "echo".into(), models: vec![] })
|
||||
}
|
||||
|
||||
fn is_local(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbered_parse_accepts_paren_markers() {
|
||||
// "1) …" replies contain sentence-ending periods inside items —
|
||||
// the leading delimiter must be picked before inspecting text.
|
||||
let r = "1) Première phrase.\nSuite.\n2) Deuxième élément.";
|
||||
assert_eq!(parse_numbered(r, 2), Some(vec!["Première phrase.\nSuite.".into(), "Deuxième élément.".into()]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn whitespace_only_pieces_survive_reconstruction() {
|
||||
let segs = vec![Segment::Text(format!("{}Hello", " ".repeat(RUN_BYTES)))];
|
||||
let mut echo = Echo;
|
||||
let out = translate_segments(&mut echo, &segs, "fr").unwrap_or_default();
|
||||
assert_eq!(out.concat(), format!("{}Hello", " ".repeat(RUN_BYTES)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translate_restores_whitespace_around_protected_spans() {
|
||||
let segs = segment("Visit https://a.io now", &PdfGrammar);
|
||||
let mut echo = Echo;
|
||||
let out = translate_segments(&mut echo, &segs, "fr").unwrap_or_default();
|
||||
assert_eq!(out.concat(), "Visit https://a.io now");
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,10 @@ qoa = ["qoa-decode", "qoa-encode"]
|
||||
qoa-decode = []
|
||||
qoa-encode = []
|
||||
opus-encode = []
|
||||
# GSM 06.10 full-rate speech: decoder and encoder in the `gsm` module, each on its own.
|
||||
gsm = ["gsm-decode", "gsm-encode"]
|
||||
gsm-decode = []
|
||||
gsm-encode = []
|
||||
ac3-decode = ["craft-media/bitstream"]
|
||||
mpa-decode = ["craft-media/bitstream"]
|
||||
# MPEG-1/2 Layer III (MP3) decoder (`mp3::Decoder`).
|
||||
@@ -58,6 +62,8 @@ ac3-encode = ["craft-media/bitstream"]
|
||||
eac3-encode = ["craft-media/bitstream"]
|
||||
# MPEG-1/2 Layer III (MP3) encoder (`mp3`).
|
||||
mp3-encode = ["craft-media/bitstream"]
|
||||
# Bluetooth Low Complexity Subband Coding (SBC) audio codec (`sbc`).
|
||||
sbc = []
|
||||
# Multi-resolution waveform overviews (`peaks`).
|
||||
peaks = []
|
||||
# Encode AAC channels in parallel (with `aac-encode`).
|
||||
@@ -92,10 +98,18 @@ required-features = ["decode", "encode"]
|
||||
name = "flac_decode"
|
||||
required-features = ["flac-decode", "flac-encode"]
|
||||
|
||||
[[test]]
|
||||
name = "gsm_oracle"
|
||||
required-features = ["gsm"]
|
||||
|
||||
[[test]]
|
||||
name = "qoa_oracle"
|
||||
required-features = ["qoa"]
|
||||
|
||||
[[test]]
|
||||
name = "sbc_oracle"
|
||||
required-features = ["sbc"]
|
||||
|
||||
[[example]]
|
||||
name = "info"
|
||||
required-features = ["decode"]
|
||||
|
||||
@@ -44,6 +44,7 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou
|
||||
| `mpa-decode` | `mpa::Decoder` | craft-media `bitstream` |
|
||||
| `mp3-decode` | `mp3::Decoder`, `mp3::InfoTag` | `mpa-decode` |
|
||||
| `mp3-encode` | `mp3::Encoder`; `encode`, `FileEncoder` for MP3 | craft-media `bitstream` |
|
||||
| `gsm-decode`, `gsm-encode` (`gsm`) | `gsm::Decoder`, `gsm::Encoder`, `gsm::decode`, `gsm::encode`: GSM 06.10 full-rate speech, bit-exact, raw `.gsm` and WAV (GSM 6.10) packings | none |
|
||||
| `peaks` | `peaks::Peaks` | none |
|
||||
| `rayon` | parallel AAC encoding (with `aac-encode`) | `rayon` |
|
||||
| `serde` | `Serialize`/`Deserialize` on the shared types | `serde` |
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -30,7 +30,8 @@
|
||||
//! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and
|
||||
//! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`]
|
||||
//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-decode`: the frame and file decoder;
|
||||
//! `flac-encode`: the packet encoder), [`qoa`] (`qoa-decode`, `qoa-encode`: whole QOA files).
|
||||
//! `flac-encode`: the packet encoder), [`qoa`] (`qoa-decode`, `qoa-encode`: whole QOA files),
|
||||
//! [`gsm`] (`gsm-decode`, `gsm-encode`: GSM 06.10 full-rate speech).
|
||||
//!
|
||||
//! The shared types below build with no features.
|
||||
|
||||
@@ -41,6 +42,8 @@ pub mod ac3;
|
||||
#[cfg(any(feature = "alac-decode", feature = "alac-encode"))]
|
||||
pub mod alac;
|
||||
mod buffer;
|
||||
#[cfg(any(feature = "gsm-decode", feature = "gsm-encode"))]
|
||||
pub mod gsm;
|
||||
mod layout;
|
||||
#[cfg(any(feature = "mp3-encode", feature = "mp3-decode"))]
|
||||
pub mod mp3;
|
||||
@@ -56,6 +59,8 @@ pub mod peaks;
|
||||
pub mod qoa;
|
||||
#[cfg(feature = "decode-native")]
|
||||
mod read;
|
||||
#[cfg(feature = "sbc")]
|
||||
pub mod sbc;
|
||||
#[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))]
|
||||
mod track;
|
||||
#[cfg(feature = "vorbis-encode")]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,138 @@
|
||||
//! GSM 06.10 against ffmpeg's decoder as the reference (oracle; skipped when ffmpeg is absent, a
|
||||
//! failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`): ffmpeg decodes what our encoder writes, in both
|
||||
//! packings, to exactly the samples our decoder returns, and so does arbitrary frame data.
|
||||
|
||||
use craft_audio::gsm::{Packing, decode, encode};
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
fn xorshift(seed: &mut u32) -> u32 {
|
||||
*seed ^= *seed << 13;
|
||||
*seed ^= *seed >> 17;
|
||||
*seed ^= *seed << 5;
|
||||
*seed
|
||||
}
|
||||
|
||||
fn signal(frames: usize) -> Vec<i16> {
|
||||
let mut seed = 0x9E37_79B9u32;
|
||||
(0..frames * 160)
|
||||
.map(|i| {
|
||||
let t = i as f64 / 8000.0;
|
||||
let f0 = 110.0 + 40.0 * (t * 2.0).sin();
|
||||
let voiced = (t * f0 * std::f64::consts::TAU).sin() * 7000.0 + (t * 4.0 * f0 * std::f64::consts::TAU).sin() * 2000.0;
|
||||
let noise = f64::from(xorshift(&mut seed) % 3000) - 1500.0;
|
||||
// Voiced stretches, noise bursts, silence and full-scale clipping.
|
||||
match (i / 1600) % 5 {
|
||||
0 | 1 => voiced as i16,
|
||||
2 => noise as i16,
|
||||
3 => 0,
|
||||
_ => (voiced * 6.0).clamp(-32768.0, 32767.0) as i16,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn wav49_file(blocks: &[u8], samples: usize) -> Vec<u8> {
|
||||
let mut fmt = Vec::new();
|
||||
fmt.extend_from_slice(&0x31u16.to_le_bytes()); // GSM 6.10
|
||||
fmt.extend_from_slice(&1u16.to_le_bytes());
|
||||
fmt.extend_from_slice(&8000u32.to_le_bytes());
|
||||
fmt.extend_from_slice(&1625u32.to_le_bytes());
|
||||
fmt.extend_from_slice(&65u16.to_le_bytes());
|
||||
fmt.extend_from_slice(&0u16.to_le_bytes());
|
||||
fmt.extend_from_slice(&2u16.to_le_bytes());
|
||||
fmt.extend_from_slice(&320u16.to_le_bytes());
|
||||
let mut body = b"WAVE".to_vec();
|
||||
body.extend_from_slice(b"fmt ");
|
||||
body.extend_from_slice(&(fmt.len() as u32).to_le_bytes());
|
||||
body.extend_from_slice(&fmt);
|
||||
body.extend_from_slice(b"fact");
|
||||
body.extend_from_slice(&4u32.to_le_bytes());
|
||||
body.extend_from_slice(&(samples as u32).to_le_bytes());
|
||||
body.extend_from_slice(b"data");
|
||||
body.extend_from_slice(&(blocks.len() as u32).to_le_bytes());
|
||||
body.extend_from_slice(blocks);
|
||||
let mut out = b"RIFF".to_vec();
|
||||
out.extend_from_slice(&(body.len() as u32).to_le_bytes());
|
||||
out.extend_from_slice(&body);
|
||||
out
|
||||
}
|
||||
|
||||
fn ffmpeg_decode(ffmpeg: &Path, file: &Path, forced_format: Option<&str>) -> Vec<i16> {
|
||||
let mut cmd = Command::new(ffmpeg);
|
||||
cmd.args(["-v", "error", "-y"]);
|
||||
if let Some(f) = forced_format {
|
||||
cmd.args(["-f", f]);
|
||||
}
|
||||
let out = cmd.arg("-i").arg(file).args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]).output().expect("run ffmpeg");
|
||||
assert!(out.status.success(), "ffmpeg failed on {}: {}", file.display(), String::from_utf8_lossy(&out.stderr));
|
||||
out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect()
|
||||
}
|
||||
|
||||
/// Frames whose long-term and RPE fields are arbitrary, over the log-area ratios of real speech.
|
||||
/// Random log-area ratios give filters that overflow at every sample, where the specification's
|
||||
/// saturating arithmetic and other decoders' differ; here every field of every sub-frame still
|
||||
/// takes all of its values, with the amplitude and gain kept where the signal stays in range.
|
||||
fn random_frames(n: usize, seed: &mut u32) -> Vec<u8> {
|
||||
let speech = encode(&signal(n), Packing::Raw);
|
||||
let mut out = Vec::new();
|
||||
for b in speech.as_chunks::<33>().0 {
|
||||
let mut frame = craft_audio::gsm::Frame::from_raw(b).expect("frame");
|
||||
for s in &mut frame.sub {
|
||||
s.lag = 40 + (xorshift(seed) % 81) as u8;
|
||||
s.gain = (xorshift(seed) % 3) as u8;
|
||||
s.grid = (xorshift(seed) & 3) as u8;
|
||||
s.xmax = (xorshift(seed) % 26) as u8;
|
||||
for p in &mut s.pulses {
|
||||
*p = (xorshift(seed) & 7) as u8;
|
||||
}
|
||||
}
|
||||
out.extend_from_slice(&frame.to_raw());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ffmpeg_decodes_our_raw_streams_to_the_same_samples() {
|
||||
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
|
||||
let dir = craft_media_testkit::fixture_subdir("gsm");
|
||||
for frames in [1usize, 2, 7, 50, 120] {
|
||||
let bytes = encode(&signal(frames), Packing::Raw);
|
||||
let file = dir.join(format!("ours_{frames}.gsm"));
|
||||
std::fs::write(&file, &bytes).expect("write fixture");
|
||||
let theirs = ffmpeg_decode(&ffmpeg, &file, Some("gsm"));
|
||||
let ours = decode(&bytes, Packing::Raw).expect("decode");
|
||||
assert_eq!(theirs.len(), ours.len(), "{frames} frames: sample count");
|
||||
assert!(theirs == ours, "{frames} frames: samples differ");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ffmpeg_decodes_our_wav49_streams_to_the_same_samples() {
|
||||
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
|
||||
let dir = craft_media_testkit::fixture_subdir("gsm");
|
||||
for frames in [2usize, 4, 20, 100] {
|
||||
let pcm = signal(frames);
|
||||
let bytes = encode(&pcm, Packing::Wav49);
|
||||
let file = dir.join(format!("ours_{frames}.wav"));
|
||||
std::fs::write(&file, wav49_file(&bytes, pcm.len())).expect("write fixture");
|
||||
let theirs = ffmpeg_decode(&ffmpeg, &file, None);
|
||||
let ours = decode(&bytes, Packing::Wav49).expect("decode");
|
||||
assert_eq!(theirs.len(), ours.len(), "{frames} frames: sample count");
|
||||
assert!(theirs == ours, "{frames} frames: samples differ");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ffmpeg_and_we_decode_arbitrary_frames_identically() {
|
||||
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return };
|
||||
let dir = craft_media_testkit::fixture_subdir("gsm");
|
||||
let mut seed = 0xC0FF_EE11u32;
|
||||
let bytes = random_frames(300, &mut seed);
|
||||
let file = dir.join("random.gsm");
|
||||
std::fs::write(&file, &bytes).expect("write fixture");
|
||||
let theirs = ffmpeg_decode(&ffmpeg, &file, Some("gsm"));
|
||||
let ours = decode(&bytes, Packing::Raw).expect("decode");
|
||||
assert_eq!(theirs.len(), ours.len());
|
||||
assert!(theirs == ours, "arbitrary frames decode differently");
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
#![cfg(feature = "sbc")]
|
||||
|
||||
use craft_audio::sbc::{
|
||||
AllocationMethod, BlockCount, ChannelMode, Decoder, Encoder, Header, SYNCWORD_MSBC, SYNCWORD_SBC, SamplingFrequency, Subbands, decode, encode,
|
||||
};
|
||||
use std::process::Command;
|
||||
|
||||
fn pearson_correlation(x: &[i16], y: &[i16], lag: usize) -> f64 {
|
||||
let n = x.len().min(y.len().saturating_sub(lag));
|
||||
if n == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
let x = &x[..n];
|
||||
let y = &y[lag..lag + n];
|
||||
let mean_x = x.iter().map(|&v| v as f64).sum::<f64>() / (n as f64);
|
||||
let mean_y = y.iter().map(|&v| v as f64).sum::<f64>() / (n as f64);
|
||||
let mut num = 0.0;
|
||||
let mut denom_x = 0.0;
|
||||
let mut denom_y = 0.0;
|
||||
for (&a, &b) in x.iter().zip(y) {
|
||||
let da = (a as f64) - mean_x;
|
||||
let db = (b as f64) - mean_y;
|
||||
num += da * db;
|
||||
denom_x += da * da;
|
||||
denom_y += db * db;
|
||||
}
|
||||
let denom = (denom_x * denom_y).sqrt();
|
||||
if denom > 0.0 { num / denom } else { 0.0 }
|
||||
}
|
||||
|
||||
fn generate_sine(sample_rate: u32, channels: usize, num_frames: usize) -> Vec<i16> {
|
||||
let mut pcm = Vec::with_capacity(num_frames * channels);
|
||||
for i in 0..num_frames {
|
||||
let t = (i as f64) / (sample_rate as f64);
|
||||
let s_l = (16000.0 * (2.0 * std::f64::consts::PI * 440.0 * t).sin()) as i16;
|
||||
pcm.push(s_l);
|
||||
if channels == 2 {
|
||||
let s_r = (16000.0 * (2.0 * std::f64::consts::PI * 880.0 * t).sin()) as i16;
|
||||
pcm.push(s_r);
|
||||
}
|
||||
}
|
||||
pcm
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_parse_and_frame_length() {
|
||||
// Standard SBC header: 0x9C, 44.1kHz, 16 blocks, Joint Stereo, Loudness, 8 subbands, bitpool 32
|
||||
let mut packet = vec![SYNCWORD_SBC, 0xBD, 32, 0x00];
|
||||
let header = Header::parse(&packet).expect("parse header");
|
||||
assert_eq!(header.sampling_frequency, SamplingFrequency::Freq44k1);
|
||||
assert_eq!(header.blocks, BlockCount::Blocks16);
|
||||
assert_eq!(header.channel_mode, ChannelMode::JointStereo);
|
||||
assert_eq!(header.allocation_method, AllocationMethod::Loudness);
|
||||
assert_eq!(header.subbands, Subbands::Subbands8);
|
||||
assert_eq!(header.bitpool, 32);
|
||||
assert_eq!(header.channels(), 2);
|
||||
assert_eq!(header.samples_per_frame(), 128);
|
||||
|
||||
// Frame length formula check
|
||||
let expected_len = 4 + (4 * 8 * 2) / 8 + (8usize + 16 * 32).div_ceil(8);
|
||||
assert_eq!(header.frame_length(), expected_len);
|
||||
assert_eq!(header.frame_length(), 77);
|
||||
|
||||
// mSBC header: 0xAD
|
||||
packet[0] = SYNCWORD_MSBC;
|
||||
let msbc_header = Header::parse(&packet).expect("parse msbc header");
|
||||
assert_eq!(msbc_header.frame_length(), 57);
|
||||
assert_eq!(msbc_header.sample_rate(), 16000);
|
||||
assert_eq!(msbc_header.channels(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_silence_roundtrip() {
|
||||
for &sample_rate in &[16_000u32, 32_000, 44_100, 48_000] {
|
||||
for &channels in &[1usize, 2] {
|
||||
for &subbands in &[4usize, 8] {
|
||||
for &blocks in &[4usize, 8, 12, 16] {
|
||||
let frame_samples = blocks * subbands * channels;
|
||||
let pcm = vec![0i16; frame_samples * 4];
|
||||
let mut encoder = Encoder::new(sample_rate, channels, blocks, subbands, 32, AllocationMethod::Loudness).expect("create encoder");
|
||||
|
||||
let mut sbc_bytes = Vec::new();
|
||||
encoder.encode(&pcm, &mut sbc_bytes).expect("encode silence");
|
||||
assert!(!sbc_bytes.is_empty());
|
||||
|
||||
let mut decoder = Decoder::new();
|
||||
let mut decoded = Vec::new();
|
||||
decoder.decode(&sbc_bytes, &mut decoded).expect("decode silence");
|
||||
|
||||
assert_eq!(decoded.len(), pcm.len());
|
||||
for &s in &decoded {
|
||||
assert!(s.abs() <= 16, "silence sample too large: {s}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sine_wave_roundtrip_fidelity() {
|
||||
let sample_rate = 44_100u32;
|
||||
let channels = 2usize;
|
||||
let num_frames = 128 * 20;
|
||||
let pcm = generate_sine(sample_rate, channels, num_frames);
|
||||
|
||||
let sbc_bytes = encode(&pcm, sample_rate, channels, 32).expect("encode sine");
|
||||
let (decoded, info) = decode(&sbc_bytes).expect("decode sine");
|
||||
|
||||
assert_eq!(info.sample_rate, sample_rate);
|
||||
assert_eq!(info.channels, channels);
|
||||
assert_eq!(decoded.len(), pcm.len());
|
||||
|
||||
// 8-subband filterbank total delay is 73 samples per channel
|
||||
let corr_l = pearson_correlation(&pcm[0..], &decoded[0..], 73 * 2);
|
||||
assert!(corr_l > 0.98, "left channel correlation too low: {corr_l}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_and_corrupt_inputs() {
|
||||
let mut decoder = Decoder::new();
|
||||
let mut dst = Vec::new();
|
||||
|
||||
// Empty packet
|
||||
assert!(decoder.decode_frame(&[], &mut dst).is_err());
|
||||
|
||||
// Too short header
|
||||
assert!(decoder.decode_frame(&[SYNCWORD_SBC, 0x00], &mut dst).is_err());
|
||||
|
||||
// Invalid syncword
|
||||
assert!(decoder.decode_frame(&[0x00, 0xBD, 32, 0x00], &mut dst).is_err());
|
||||
|
||||
// CRC mismatch
|
||||
let bad_crc_frame = vec![SYNCWORD_SBC, 0xBD, 32, 0x55, 0x00, 0x00, 0x00, 0x00];
|
||||
assert!(decoder.decode_frame(&bad_crc_frame, &mut dst).is_err());
|
||||
|
||||
// Bitpool capacity exceeded (Mono 4 subbands capacity is 64; 65 must fail)
|
||||
let bad_bitpool_frame = vec![SYNCWORD_SBC, 0x00, 65, 0x00];
|
||||
assert!(decoder.decode_frame(&bad_bitpool_frame, &mut dst).is_err());
|
||||
assert!(Encoder::new(16000, 1, 4, 4, 65, AllocationMethod::Loudness).is_err());
|
||||
|
||||
// Unsupported 15-block configuration (only 16kHz mono 8-subband 26-bitpool is valid mSBC)
|
||||
assert!(Encoder::new(44100, 2, 15, 8, 32, AllocationMethod::Loudness).is_err());
|
||||
|
||||
// Incomplete PCM frames rejected by encoder
|
||||
let mut enc = Encoder::new(44100, 2, 16, 8, 32, AllocationMethod::Loudness).expect("enc");
|
||||
let mut out = Vec::new();
|
||||
assert!(enc.encode(&[0i16; 100], &mut out).is_err());
|
||||
|
||||
// Valid mSBC encoding produces exact 57-byte mSBC frame
|
||||
let mut msbc_enc = Encoder::new(16000, 1, 15, 8, 26, AllocationMethod::Loudness).expect("msbc enc");
|
||||
let mut msbc_out = Vec::new();
|
||||
msbc_enc.encode(&vec![0i16; 120], &mut msbc_out).expect("encode msbc frame");
|
||||
assert_eq!(msbc_out.len(), 57);
|
||||
assert_eq!(msbc_out[0], SYNCWORD_MSBC);
|
||||
|
||||
// Hostile fuzzed input should never panic
|
||||
let mut seed = 0x1234_5678u32;
|
||||
for len in [10, 50, 100, 500] {
|
||||
let mut random_bytes = vec![0u8; len];
|
||||
for b in &mut random_bytes {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
*b = seed as u8;
|
||||
}
|
||||
let _ = decoder.decode(&random_bytes, &mut dst);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_ffmpeg_cross_verification() {
|
||||
let Some(ffmpeg) = craft_media_testkit::ffmpeg() else {
|
||||
return;
|
||||
};
|
||||
let dir = craft_media_testkit::fixture_subdir("sbc");
|
||||
|
||||
// 1. FFmpeg decodes our encoded SBC files
|
||||
for &(sample_rate, channels) in &[(16_000u32, 1usize), (44_100, 2), (48_000, 2)] {
|
||||
let pcm = generate_sine(sample_rate, channels, 128 * 20);
|
||||
let sbc_bytes = encode(&pcm, sample_rate, channels, 32).expect("encode sbc");
|
||||
|
||||
let sbc_file = dir.join(format!("ours_{sample_rate}_{channels}.sbc"));
|
||||
std::fs::write(&sbc_file, &sbc_bytes).expect("write sbc fixture");
|
||||
|
||||
let out = Command::new(&ffmpeg)
|
||||
.args(["-v", "error", "-y", "-i"])
|
||||
.arg(&sbc_file)
|
||||
.args(["-f", "s16le", "-acodec", "pcm_s16le", "-"])
|
||||
.output()
|
||||
.expect("run ffmpeg");
|
||||
|
||||
assert!(out.status.success(), "ffmpeg failed to decode our SBC: {}", String::from_utf8_lossy(&out.stderr));
|
||||
|
||||
let theirs: Vec<i16> = out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect();
|
||||
|
||||
assert!(!theirs.is_empty());
|
||||
let corr = pearson_correlation(&pcm, &theirs, 73 * channels);
|
||||
assert!(corr > 0.95, "FFmpeg decoded output correlation against input too low ({sample_rate} Hz, {channels} ch): {corr}");
|
||||
}
|
||||
|
||||
// 2. Our decoder decodes FFmpeg-encoded SBC files
|
||||
for &(sample_rate, channels) in &[(16_000u32, 1usize), (44_100, 2), (48_000, 2)] {
|
||||
let ff_sbc_file = dir.join(format!("ff_{sample_rate}_{channels}.sbc"));
|
||||
let ff_enc = Command::new(&ffmpeg)
|
||||
.args([
|
||||
"-v",
|
||||
"error",
|
||||
"-y",
|
||||
"-f",
|
||||
"lavfi",
|
||||
"-i",
|
||||
&format!("sine=frequency=440:duration=0.2:sample_rate={sample_rate}"),
|
||||
"-c:a",
|
||||
"sbc",
|
||||
"-ac",
|
||||
&channels.to_string(),
|
||||
])
|
||||
.arg(&ff_sbc_file)
|
||||
.output()
|
||||
.expect("ffmpeg encode sbc");
|
||||
|
||||
assert!(ff_enc.status.success(), "ffmpeg failed to encode SBC: {}", String::from_utf8_lossy(&ff_enc.stderr));
|
||||
|
||||
let ff_sbc_bytes = std::fs::read(&ff_sbc_file).expect("read ff sbc fixture");
|
||||
let (decoded, info) = decode(&ff_sbc_bytes).expect("decode ff sbc");
|
||||
assert_eq!(info.sample_rate, sample_rate);
|
||||
assert_eq!(info.channels, channels);
|
||||
assert!(!decoded.is_empty());
|
||||
|
||||
// Decode ff_sbc_file with ffmpeg as well to compare
|
||||
let ff_raw = Command::new(&ffmpeg)
|
||||
.args(["-v", "error", "-y", "-i"])
|
||||
.arg(&ff_sbc_file)
|
||||
.args(["-f", "s16le", "-acodec", "pcm_s16le", "-"])
|
||||
.output()
|
||||
.expect("ffmpeg decode raw");
|
||||
|
||||
let ff_pcm: Vec<i16> = ff_raw.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect();
|
||||
|
||||
let corr = pearson_correlation(&decoded, &ff_pcm, 0);
|
||||
assert!(corr > 0.99, "Our decoder output correlation against FFmpeg decoder output too low: {corr}");
|
||||
}
|
||||
|
||||
// 3. Our decoder decodes FFmpeg-encoded mSBC wideband speech
|
||||
let msbc_file = dir.join("ff_msbc.sbc");
|
||||
let ff_msbc_enc = Command::new(&ffmpeg)
|
||||
.args(["-v", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=440:duration=0.2:sample_rate=16000", "-c:a", "sbc", "-msbc", "1"])
|
||||
.arg(&msbc_file)
|
||||
.output()
|
||||
.expect("ffmpeg encode msbc");
|
||||
|
||||
assert!(ff_msbc_enc.status.success(), "ffmpeg failed to encode mSBC: {}", String::from_utf8_lossy(&ff_msbc_enc.stderr));
|
||||
|
||||
let msbc_bytes = std::fs::read(&msbc_file).expect("read msbc fixture");
|
||||
let (decoded, info) = decode(&msbc_bytes).expect("decode msbc");
|
||||
assert_eq!(info.sample_rate, 16000);
|
||||
assert_eq!(info.channels, 1);
|
||||
assert_eq!(info.blocks, 15);
|
||||
assert_eq!(info.subbands, 8);
|
||||
assert!(!decoded.is_empty());
|
||||
|
||||
let ff_msbc_raw = Command::new(&ffmpeg)
|
||||
.args(["-v", "error", "-y", "-i"])
|
||||
.arg(&msbc_file)
|
||||
.args(["-f", "s16le", "-acodec", "pcm_s16le", "-"])
|
||||
.output()
|
||||
.expect("ffmpeg decode msbc raw");
|
||||
|
||||
let ff_msbc_pcm: Vec<i16> = ff_msbc_raw.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect();
|
||||
|
||||
let corr = pearson_correlation(&decoded, &ff_msbc_pcm, 0);
|
||||
assert!(corr > 0.99, "mSBC decoded output correlation against FFmpeg decoder too low: {corr}");
|
||||
|
||||
// 4. FFmpeg decodes our encoded mSBC file
|
||||
let msbc_sine = generate_sine(16000, 1, 120 * 20);
|
||||
let mut our_msbc_enc = Encoder::new(16000, 1, 15, 8, 26, AllocationMethod::Loudness).expect("msbc enc");
|
||||
let mut our_msbc_bytes = Vec::new();
|
||||
our_msbc_enc.encode(&msbc_sine, &mut our_msbc_bytes).expect("encode msbc");
|
||||
|
||||
let our_msbc_file = dir.join("ours_msbc.sbc");
|
||||
std::fs::write(&our_msbc_file, &our_msbc_bytes).expect("write our msbc fixture");
|
||||
|
||||
let our_msbc_out = Command::new(&ffmpeg)
|
||||
.args(["-v", "error", "-y", "-i"])
|
||||
.arg(&our_msbc_file)
|
||||
.args(["-f", "s16le", "-acodec", "pcm_s16le", "-"])
|
||||
.output()
|
||||
.expect("ffmpeg decode our msbc");
|
||||
|
||||
assert!(our_msbc_out.status.success(), "ffmpeg failed to decode our mSBC: {}", String::from_utf8_lossy(&our_msbc_out.stderr));
|
||||
|
||||
let our_msbc_theirs: Vec<i16> = our_msbc_out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect();
|
||||
|
||||
assert!(!our_msbc_theirs.is_empty());
|
||||
let corr_msbc = pearson_correlation(&msbc_sine, &our_msbc_theirs, 73);
|
||||
assert!(corr_msbc > 0.95, "FFmpeg decoded output correlation against our mSBC input too low: {corr_msbc}");
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
//! JACOsub (`.jss`, `.js`): multi-feature scriptable subtitle format.
|
||||
//!
|
||||
//! ```text
|
||||
//! # JACOsub file
|
||||
//! #TimeRes 100
|
||||
//! 0:00:01.00 0:00:03.00 D0 First line\nsecond line
|
||||
//! ```
|
||||
//!
|
||||
//! Lines beginning with `#` are comments or header directives. Timed caption lines begin with
|
||||
//! `START STOP [DIRECTIVE] Text`, where timestamps are `H:MM:SS.FF` (or `@units`) relative to
|
||||
//! the declared `#TimeRes` (default 30). Inside the text, `\n` represents a newline.
|
||||
|
||||
use crate::text::{escape_markup, ms_to_tick, plain_text, tick_to_ms};
|
||||
use crate::{Cue, Document, Error, Result};
|
||||
|
||||
/// Parse a JACOsub timestamp `H:MM:SS.FF` or `@units` into milliseconds.
|
||||
pub(crate) fn parse_time(s: &str, time_res: i64) -> Option<i64> {
|
||||
let s = s.trim();
|
||||
if s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(units_str) = s.strip_prefix('@') {
|
||||
let units: i64 = units_str.trim().parse().ok()?;
|
||||
let time_res = if time_res > 0 { time_res } else { 30 };
|
||||
return Some(units.saturating_mul(1000) / time_res);
|
||||
}
|
||||
|
||||
let (h_str, rest) = s.split_once(':')?;
|
||||
let (m_str, rest) = rest.split_once(':')?;
|
||||
let (s_str, f_str) = rest.split_once('.')?;
|
||||
|
||||
let h: i64 = h_str.trim().parse().ok()?;
|
||||
let m: i64 = m_str.trim().parse().ok()?;
|
||||
let sec: i64 = s_str.trim().parse().ok()?;
|
||||
let frac: i64 = f_str.trim().parse().ok()?;
|
||||
|
||||
let time_res = if time_res > 0 { time_res } else { 30 };
|
||||
let frac_ms = frac.saturating_mul(1000) / time_res;
|
||||
|
||||
let total_secs = h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec);
|
||||
Some(total_secs.saturating_mul(1000).saturating_add(frac_ms))
|
||||
}
|
||||
|
||||
/// Format milliseconds into JACOsub timestamp string `H:MM:SS.cc` (at 100 centiseconds/sec).
|
||||
pub(crate) fn format_time(ms: i64) -> String {
|
||||
let ms = ms.max(0);
|
||||
let total_secs = ms / 1000;
|
||||
let centis = (ms % 1000) / 10;
|
||||
let hours = total_secs / 3600;
|
||||
let mins = (total_secs % 3600) / 60;
|
||||
let secs = total_secs % 60;
|
||||
|
||||
format!("{hours}:{mins:02}:{secs:02}.{centis:02}")
|
||||
}
|
||||
|
||||
/// Check if a line is a JACOsub timed caption line: `H:MM:SS.FF H:MM:SS.FF ...` or `@units @units ...`.
|
||||
pub(crate) fn is_timed_line(line: &str) -> bool {
|
||||
let line = line.trim();
|
||||
if line.starts_with('#') {
|
||||
return false;
|
||||
}
|
||||
let mut parts = line.split_whitespace();
|
||||
let Some(t0) = parts.next() else { return false };
|
||||
let Some(t1) = parts.next() else { return false };
|
||||
(t0.contains(':') && t0.contains('.') && t1.contains(':') && t1.contains('.')) || (t0.starts_with('@') && t1.starts_with('@'))
|
||||
}
|
||||
|
||||
pub fn parse(text: &str) -> Result<Document> {
|
||||
let mut doc = Document::default();
|
||||
let mut time_res: i64 = 30; // standard default
|
||||
let mut shift_ms: i64 = 0;
|
||||
let mut found = false;
|
||||
|
||||
for (n, line) in text.lines().enumerate() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(cmd) = line.strip_prefix('#') {
|
||||
let cmd_lower = cmd.to_ascii_lowercase();
|
||||
if cmd_lower.starts_with("timeres") || cmd_lower.starts_with('t') {
|
||||
let parts: Vec<&str> = cmd.split_whitespace().collect();
|
||||
if parts.len() >= 2
|
||||
&& let Ok(res) = parts[1].parse::<i64>()
|
||||
&& res > 0
|
||||
{
|
||||
time_res = res;
|
||||
}
|
||||
} else if cmd_lower.starts_with("shift") || cmd_lower.starts_with('s') {
|
||||
let parts: Vec<&str> = cmd.split_whitespace().collect();
|
||||
if parts.len() >= 2 {
|
||||
let s_val = parts[1].trim();
|
||||
let (neg, s_raw) = if let Some(stripped) = s_val.strip_prefix('-') {
|
||||
(true, stripped)
|
||||
} else if let Some(stripped) = s_val.strip_prefix('+') {
|
||||
(false, stripped)
|
||||
} else {
|
||||
(false, s_val)
|
||||
};
|
||||
if let Some(ms) = parse_time(s_raw, time_res) {
|
||||
shift_ms = if neg { -ms } else { ms };
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if !is_timed_line(line) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut parts = line.split_whitespace();
|
||||
let Some(start_str) = parts.next() else { continue };
|
||||
let Some(end_str) = parts.next() else { continue };
|
||||
|
||||
let start_ms = match parse_time(start_str, time_res) {
|
||||
Some(ms) => ms.saturating_add(shift_ms).max(0),
|
||||
None => {
|
||||
doc.warnings.push(format!("line {}: invalid start timestamp, skipped", n + 1));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let end_ms = match parse_time(end_str, time_res) {
|
||||
Some(ms) => ms.saturating_add(shift_ms).max(0),
|
||||
None => {
|
||||
doc.warnings.push(format!("line {}: invalid end timestamp, skipped", n + 1));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
found = true;
|
||||
|
||||
// Skip to after the end timestamp in the original line to preserve exact spaces
|
||||
let after_end_pos = match line.find(end_str) {
|
||||
Some(idx) => idx + end_str.len(),
|
||||
None => continue,
|
||||
};
|
||||
let remainder = &line[after_end_pos..];
|
||||
let trimmed_rem = remainder.trim_start();
|
||||
|
||||
// Check if there is a directive prefix: either [Directive] or a single non-space word starting with D
|
||||
let text_part = if let Some(stripped) = trimmed_rem.strip_prefix('[') {
|
||||
match stripped.find(']') {
|
||||
Some(end_bracket) => stripped[end_bracket + 1..].trim_start(),
|
||||
None => trimmed_rem,
|
||||
}
|
||||
} else if let Some(space_idx) = trimmed_rem.find(' ') {
|
||||
let first_token = &trimmed_rem[..space_idx];
|
||||
if first_token.starts_with('D') && first_token[1..].chars().all(|c| c.is_ascii_digit()) {
|
||||
trimmed_rem[space_idx + 1..].trim_start()
|
||||
} else {
|
||||
trimmed_rem
|
||||
}
|
||||
} else {
|
||||
trimmed_rem
|
||||
};
|
||||
|
||||
// Text parsing: `\n` represents newline
|
||||
let raw_lines: Vec<&str> = text_part.split(r"\n").map(str::trim).filter(|l| !l.is_empty()).collect();
|
||||
let lines: Vec<String> = raw_lines.into_iter().map(escape_markup).collect();
|
||||
|
||||
let start = ms_to_tick(start_ms);
|
||||
let end = ms_to_tick(end_ms);
|
||||
if end <= start || lines.is_empty() {
|
||||
doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1));
|
||||
continue;
|
||||
}
|
||||
|
||||
doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() });
|
||||
}
|
||||
|
||||
if !found && !text.trim().is_empty() {
|
||||
return Err(Error::NotFormat("JACOsub"));
|
||||
}
|
||||
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
pub fn write(doc: &Document) -> String {
|
||||
let mut out = String::new();
|
||||
out.push_str("# JACOsub file\n");
|
||||
out.push_str("#TimeRes 100\n\n");
|
||||
|
||||
for c in &doc.cues {
|
||||
let start_ms = tick_to_ms(c.start);
|
||||
let end_ms = tick_to_ms(c.end).max(start_ms + 10);
|
||||
let raw_lines = plain_text(&c.text);
|
||||
let lines: Vec<&str> = raw_lines.lines().map(str::trim).filter(|l| !l.is_empty()).collect();
|
||||
|
||||
if !lines.is_empty() {
|
||||
let text = lines.join(r"\n");
|
||||
out.push_str(&format!("{} {} D0 {}\n", format_time(start_ms), format_time(end_ms), text));
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_and_write_roundtrip() {
|
||||
let jss = "# JACOsub file\n#TimeRes 100\n\n0:00:01.00 0:00:03.00 D0 First line\\nsecond line\n0:00:04.50 0:00:06.00 D0 Second cue\n";
|
||||
let doc = parse(jss).expect("parse");
|
||||
assert_eq!(doc.cues.len(), 2);
|
||||
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
|
||||
assert_eq!(doc.cues[0].end, ms_to_tick(3000));
|
||||
assert_eq!(doc.cues[0].text, "First line\nsecond line");
|
||||
assert_eq!(doc.cues[1].start, ms_to_tick(4500));
|
||||
assert_eq!(doc.cues[1].end, ms_to_tick(6000));
|
||||
assert_eq!(doc.cues[1].text, "Second cue");
|
||||
|
||||
let written = write(&doc);
|
||||
assert_eq!(written, jss);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_bracketed_directive() {
|
||||
let jss = "#TimeRes 100\n0:00:01.00 0:00:03.00 [Style1] Bracketed style text\n";
|
||||
let doc = parse(jss).expect("parse");
|
||||
assert_eq!(doc.cues.len(), 1);
|
||||
assert_eq!(doc.cues[0].text, "Bracketed style text");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_shift_under_one_hour() {
|
||||
let jss = "#TimeRes 100\n#Shift -0:00:01.00\n0:00:02.00 0:00:04.00 D0 Shifted line\n";
|
||||
let doc = parse(jss).expect("parse");
|
||||
assert_eq!(doc.cues.len(), 1);
|
||||
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
|
||||
assert_eq!(doc.cues[0].end, ms_to_tick(3000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_jacosub() {
|
||||
assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err());
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,10 @@ struct ReadmeExamples;
|
||||
|
||||
pub mod ass;
|
||||
pub mod cea608;
|
||||
pub mod jacosub;
|
||||
pub mod mcc;
|
||||
pub mod pjs;
|
||||
pub mod realtext;
|
||||
pub mod scc;
|
||||
pub mod srt;
|
||||
pub mod stl;
|
||||
@@ -72,10 +75,28 @@ pub enum Format {
|
||||
Dfxp,
|
||||
/// Advanced SubStation Alpha (also reads SubStation Alpha).
|
||||
Ass,
|
||||
/// Phoenix Japanimation Society (PJS) subtitles.
|
||||
Pjs,
|
||||
/// RealText streaming timed text markup.
|
||||
RealText,
|
||||
/// JACOsub scriptable subtitles.
|
||||
JacoSub,
|
||||
}
|
||||
|
||||
impl Format {
|
||||
pub const ALL: [Format; 8] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp, Format::Ass];
|
||||
pub const ALL: [Format; 11] = [
|
||||
Format::Srt,
|
||||
Format::WebVtt,
|
||||
Format::Scc,
|
||||
Format::Mcc,
|
||||
Format::Stl,
|
||||
Format::Ttml,
|
||||
Format::Dfxp,
|
||||
Format::Ass,
|
||||
Format::Pjs,
|
||||
Format::RealText,
|
||||
Format::JacoSub,
|
||||
];
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Format::Srt => "SubRip (SRT)",
|
||||
@@ -86,6 +107,9 @@ impl Format {
|
||||
Format::Ttml => "TTML (IMSC1)",
|
||||
Format::Dfxp => "DFXP (TTML1)",
|
||||
Format::Ass => "Advanced SubStation Alpha (ASS)",
|
||||
Format::Pjs => "PJS",
|
||||
Format::RealText => "RealText",
|
||||
Format::JacoSub => "JACOsub",
|
||||
}
|
||||
}
|
||||
pub fn extension(self) -> &'static str {
|
||||
@@ -98,6 +122,9 @@ impl Format {
|
||||
Format::Ttml => "ttml",
|
||||
Format::Dfxp => "dfxp",
|
||||
Format::Ass => "ass",
|
||||
Format::Pjs => "pjs",
|
||||
Format::RealText => "rt",
|
||||
Format::JacoSub => "jss",
|
||||
}
|
||||
}
|
||||
pub fn from_name(s: &str) -> Option<Format> {
|
||||
@@ -110,6 +137,9 @@ impl Format {
|
||||
"ttml" | "imsc" | "imsc1" | "xml" => Some(Format::Ttml),
|
||||
"dfxp" => Some(Format::Dfxp),
|
||||
"ass" | "ssa" => Some(Format::Ass),
|
||||
"pjs" => Some(Format::Pjs),
|
||||
"rt" | "realtext" => Some(Format::RealText),
|
||||
"jss" | "js" | "jacosub" => Some(Format::JacoSub),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -260,6 +290,16 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option<Format> {
|
||||
if head.starts_with("[Script Info]") {
|
||||
return Some(Format::Ass);
|
||||
}
|
||||
if realtext::is_realtext(head) {
|
||||
return Some(Format::RealText);
|
||||
}
|
||||
let first_lines: Vec<&str> = head.lines().map(str::trim).filter(|l| !l.is_empty()).take(3).collect();
|
||||
if first_lines.first().is_some_and(|l| pjs::split_cue(l).is_some()) {
|
||||
return Some(Format::Pjs);
|
||||
}
|
||||
if first_lines.iter().any(|l| jacosub::is_timed_line(l)) || (head.starts_with('#') && (head.contains("TimeRes") || head.contains("#T"))) {
|
||||
return Some(Format::JacoSub);
|
||||
}
|
||||
let ext = ext.map(|e| e.trim_start_matches('.').to_ascii_lowercase());
|
||||
if (head.starts_with('<') && (head.contains("<tt ") || head.contains("<tt>") || head.contains(":tt ")))
|
||||
&& (head.contains("ns/ttml") || head.contains("ttaf1"))
|
||||
@@ -271,7 +311,7 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option<Format> {
|
||||
match ext.as_deref() {
|
||||
Some("srt") if has_arrow_timing || head.is_empty() => Some(Format::Srt),
|
||||
Some("vtt") if has_arrow_timing => Some(Format::WebVtt),
|
||||
Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa") => None,
|
||||
Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa" | "pjs" | "rt" | "jss" | "js") => None,
|
||||
_ if has_arrow_timing => Some(Format::Srt),
|
||||
_ => None,
|
||||
}
|
||||
@@ -306,6 +346,9 @@ fn parse_unbounded(bytes: &[u8], format: Format) -> Result<Document> {
|
||||
Format::Mcc => mcc::parse(&text),
|
||||
Format::Ttml | Format::Dfxp => ttml::parse(&text),
|
||||
Format::Ass => ass::parse(&text),
|
||||
Format::Pjs => pjs::parse(&text),
|
||||
Format::RealText => realtext::parse(&text),
|
||||
Format::JacoSub => jacosub::parse(&text),
|
||||
Format::Stl => stl::parse(bytes),
|
||||
}
|
||||
}
|
||||
@@ -344,6 +387,9 @@ pub fn write(doc: &Document, format: Format, opts: WriteOptions) -> Vec<u8> {
|
||||
Format::Ttml => ttml::write(doc, ttml::Flavor::Imsc1, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(),
|
||||
Format::Dfxp => ttml::write(doc, ttml::Flavor::Dfxp, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(),
|
||||
Format::Ass => ass::write(doc).into_bytes(),
|
||||
Format::Pjs => pjs::write(doc).into_bytes(),
|
||||
Format::RealText => realtext::write(doc).into_bytes(),
|
||||
Format::JacoSub => jacosub::write(doc).into_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
//! Phoenix Japanimation Society (PJS) subtitles (`.pjs`): times in tenths of a second.
|
||||
//!
|
||||
//! ```text
|
||||
//! 10,50,"First line"
|
||||
//! 60,120,"Second line|with break"
|
||||
//! ```
|
||||
//!
|
||||
//! Lines are comma-separated `start,end,"text"` with times in deciseconds (tenths of a second).
|
||||
//! Inside the quotes, `|` breaks a line.
|
||||
|
||||
use crate::text::{escape_markup, ms_to_tick, number, plain_lines, tick_to_ms};
|
||||
use crate::{Cue, Document, Error, Result};
|
||||
|
||||
/// `start,end,"text"` as its parts.
|
||||
pub(crate) fn split_cue(line: &str) -> Option<(i64, i64, &str)> {
|
||||
let (start_str, rest) = line.split_once(',')?;
|
||||
let (end_str, text_str) = rest.split_once(',')?;
|
||||
let start = number(start_str.trim())?;
|
||||
let end = number(end_str.trim())?;
|
||||
let text = text_str.trim();
|
||||
let text = if let Some(stripped) = text.strip_prefix('"').and_then(|t| t.strip_suffix('"')) { stripped } else { text };
|
||||
Some((start, end, text))
|
||||
}
|
||||
|
||||
/// Parse a PJS subtitle document.
|
||||
pub fn parse(text: &str) -> Result<Document> {
|
||||
let mut doc = Document::default();
|
||||
let mut found = false;
|
||||
for (n, line) in text.lines().enumerate() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some((start_ds, end_ds, raw)) = split_cue(line) else {
|
||||
doc.warnings.push(format!("line {}: not a PJS cue, skipped", n + 1));
|
||||
continue;
|
||||
};
|
||||
found = true;
|
||||
let mut lines = Vec::new();
|
||||
for l in raw.split('|') {
|
||||
let trimmed = l.trim();
|
||||
if !trimmed.is_empty() {
|
||||
let unquoted = trimmed.replace("\"\"", "\"");
|
||||
lines.push(escape_markup(&unquoted));
|
||||
}
|
||||
}
|
||||
let start = ms_to_tick(start_ds.saturating_mul(100));
|
||||
let end = ms_to_tick(end_ds.saturating_mul(100));
|
||||
if end <= start || lines.is_empty() {
|
||||
doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1));
|
||||
continue;
|
||||
}
|
||||
doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() });
|
||||
}
|
||||
if !found && !text.trim().is_empty() {
|
||||
return Err(Error::NotFormat("PJS"));
|
||||
}
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
/// Serialize a document into PJS format.
|
||||
pub fn write(doc: &Document) -> String {
|
||||
let mut out = String::new();
|
||||
let ds = |t| (tick_to_ms(t) + 50) / 100;
|
||||
for c in &doc.cues {
|
||||
let start = ds(c.start);
|
||||
let end = ds(c.end).max(start + 1);
|
||||
let lines: Vec<String> =
|
||||
plain_lines(&c.text).into_iter().map(|l| l.replace('"', "\"\"").replace('|', "|")).filter(|l| !l.is_empty()).collect();
|
||||
if !lines.is_empty() {
|
||||
out.push_str(&format!("{start},{end},\"{}\"\n", lines.join("|")));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_and_write_roundtrip() {
|
||||
let pjs = "10,50,\"First line\"\n60,120,\"Second line|with break\"\n130,150,\"Say \"\"Hi\"\"\"\n";
|
||||
let doc = parse(pjs).expect("parse");
|
||||
assert_eq!(doc.cues.len(), 3);
|
||||
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
|
||||
assert_eq!(doc.cues[0].end, ms_to_tick(5000));
|
||||
assert_eq!(doc.cues[0].text, "First line");
|
||||
assert_eq!(doc.cues[1].start, ms_to_tick(6000));
|
||||
assert_eq!(doc.cues[1].end, ms_to_tick(12000));
|
||||
assert_eq!(doc.cues[1].text, "Second line\nwith break");
|
||||
assert_eq!(doc.cues[2].text, "Say \"Hi\"");
|
||||
|
||||
let written = write(&doc);
|
||||
assert_eq!(written, pjs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_pjs() {
|
||||
assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
//! RealText (`.rt`): streaming HTML-like timed text markup.
|
||||
//!
|
||||
//! ```text
|
||||
//! <window type="generic" width="320" height="60" wordwrap="true">
|
||||
//! <time begin="00:01.00" end="00:03.00"/>First line<br/>second line
|
||||
//! </window>
|
||||
//! ```
|
||||
//!
|
||||
//! RealText documents encapsulate timed text inside a `<window>` container with `<time begin="…" [end="…"]/>`
|
||||
//! markers. Inside each caption block, `<br/>` and `<br>` represent line breaks.
|
||||
|
||||
use crate::text::{escape_markup, last_cue_end, ms_to_tick, plain_lines, plain_text, tick_to_ms};
|
||||
use crate::{Cue, Document, Error, Result};
|
||||
|
||||
/// Parse a RealText timestamp string into milliseconds.
|
||||
/// Supports `SS[.fff]`, `MM:SS[.fff]`, and `HH:MM:SS[.fff]`.
|
||||
pub(crate) fn parse_time(s: &str) -> Option<i64> {
|
||||
let s = s.trim();
|
||||
if s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let parts: Vec<&str> = s.split(':').collect();
|
||||
let (hours, mins, secs_str) = match parts.len() {
|
||||
1 => (0i64, 0i64, parts[0]),
|
||||
2 => (0i64, parts[0].parse().ok()?, parts[1]),
|
||||
3 => (parts[0].parse().ok()?, parts[1].parse().ok()?, parts[2]),
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let (secs, ms) = if let Some((sec_p, frac_p)) = secs_str.split_once('.') {
|
||||
let sec: i64 = sec_p.parse().ok()?;
|
||||
let frac_str = frac_p.trim_end();
|
||||
let ms = match frac_str.len() {
|
||||
0 => 0i64,
|
||||
1 => frac_str.parse::<i64>().ok()?.saturating_mul(100),
|
||||
2 => frac_str.parse::<i64>().ok()?.saturating_mul(10),
|
||||
3 => frac_str.parse::<i64>().ok()?,
|
||||
_ => {
|
||||
let first3 = frac_str.get(..3)?;
|
||||
first3.parse::<i64>().ok()?
|
||||
}
|
||||
};
|
||||
(sec, ms)
|
||||
} else {
|
||||
(secs_str.parse().ok()?, 0i64)
|
||||
};
|
||||
|
||||
let total_ms = hours.saturating_mul(3600).saturating_add(mins.saturating_mul(60)).saturating_add(secs).saturating_mul(1000).saturating_add(ms);
|
||||
|
||||
Some(total_ms)
|
||||
}
|
||||
|
||||
/// Format milliseconds into RealText timestamp string `MM:SS.ss` or `HH:MM:SS.ss`.
|
||||
pub(crate) fn format_time(ms: i64) -> String {
|
||||
let ms = ms.max(0);
|
||||
let total_secs = ms / 1000;
|
||||
let centis = (ms % 1000) / 10;
|
||||
let hours = total_secs / 3600;
|
||||
let mins = (total_secs % 3600) / 60;
|
||||
let secs = total_secs % 60;
|
||||
|
||||
if hours > 0 { format!("{hours:02}:{mins:02}:{secs:02}.{centis:02}") } else { format!("{mins:02}:{secs:02}.{centis:02}") }
|
||||
}
|
||||
|
||||
/// Extract attribute value from a tag (e.g., `begin="00:01.00"`).
|
||||
fn attr_value<'a>(tag: &'a str, attr_name: &str) -> Option<&'a str> {
|
||||
let mut rest = tag;
|
||||
while let Some(idx) = rest.find(attr_name) {
|
||||
let candidate = &rest[idx + attr_name.len()..];
|
||||
let trimmed = candidate.trim_start();
|
||||
if let Some(after_eq) = trimmed.strip_prefix('=') {
|
||||
let after_eq = after_eq.trim_start();
|
||||
let quote = after_eq.chars().next()?;
|
||||
if quote == '"' || quote == '\'' {
|
||||
let val_start = 1;
|
||||
let val_end = after_eq[1..].find(quote)?;
|
||||
return Some(&after_eq[val_start..1 + val_end]);
|
||||
}
|
||||
}
|
||||
rest = &rest[idx + attr_name.len()..];
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn find_case_insensitive(haystack: &str, needle: &str) -> Option<usize> {
|
||||
let needle_bytes = needle.as_bytes();
|
||||
if needle_bytes.is_empty() {
|
||||
return Some(0);
|
||||
}
|
||||
let haystack_bytes = haystack.as_bytes();
|
||||
if haystack_bytes.len() < needle_bytes.len() {
|
||||
return None;
|
||||
}
|
||||
haystack_bytes.windows(needle_bytes.len()).position(|window| window.iter().zip(needle_bytes).all(|(&h, &n)| h.eq_ignore_ascii_case(&n)))
|
||||
}
|
||||
|
||||
/// Check if text is a RealText file.
|
||||
pub(crate) fn is_realtext(text: &str) -> bool {
|
||||
let trimmed = text.trim_start();
|
||||
let lower_prefix: String = trimmed.chars().take(100).flat_map(|c| c.to_lowercase()).collect();
|
||||
if lower_prefix.starts_with("<window") {
|
||||
return true;
|
||||
}
|
||||
find_case_insensitive(text, "<window").is_some() && find_case_insensitive(text, "<time").is_some()
|
||||
}
|
||||
|
||||
pub fn parse(text: &str) -> Result<Document> {
|
||||
if !is_realtext(text) {
|
||||
return Err(Error::NotFormat("RealText"));
|
||||
}
|
||||
|
||||
let mut doc = Document::default();
|
||||
struct RawCue<'a> {
|
||||
begin: i64,
|
||||
end: Option<i64>,
|
||||
body: &'a str,
|
||||
}
|
||||
|
||||
let mut raw_cues: Vec<RawCue> = Vec::new();
|
||||
let mut search_pos = 0;
|
||||
|
||||
while let Some(time_idx) = find_case_insensitive(&text[search_pos..], "<time") {
|
||||
let actual_idx = search_pos + time_idx;
|
||||
let tag_end = match text[actual_idx..].find('>') {
|
||||
Some(end) => actual_idx + end + 1,
|
||||
None => break,
|
||||
};
|
||||
|
||||
let tag_str = &text[actual_idx..tag_end];
|
||||
let begin = attr_value(tag_str, "begin").and_then(parse_time);
|
||||
let end = attr_value(tag_str, "end").and_then(parse_time);
|
||||
|
||||
if let Some(begin_ms) = begin {
|
||||
// Text runs from tag_end to next '<time' or '</window>' or EOF
|
||||
let remaining = &text[tag_end..];
|
||||
let body_end =
|
||||
find_case_insensitive(remaining, "<time").or_else(|| find_case_insensitive(remaining, "</window>")).unwrap_or(remaining.len());
|
||||
|
||||
let body = &text[tag_end..tag_end + body_end];
|
||||
raw_cues.push(RawCue { begin: begin_ms, end, body });
|
||||
search_pos = tag_end;
|
||||
} else {
|
||||
search_pos = tag_end;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..raw_cues.len() {
|
||||
let rc = &raw_cues[i];
|
||||
let start = ms_to_tick(rc.begin);
|
||||
let end = if let Some(explicit_end) = rc.end {
|
||||
ms_to_tick(explicit_end)
|
||||
} else if i + 1 < raw_cues.len() {
|
||||
ms_to_tick(raw_cues[i + 1].begin)
|
||||
} else {
|
||||
last_cue_end(start)
|
||||
};
|
||||
|
||||
// Convert <br/> and <br> to newlines, decode markup to clean text
|
||||
let normalized = rc.body.replace("<br/>", "\n").replace("<br>", "\n").replace("<BR/>", "\n").replace("<BR>", "\n");
|
||||
let plain = plain_text(&normalized);
|
||||
let lines: Vec<&str> = plain.lines().map(str::trim).filter(|l| !l.is_empty()).collect();
|
||||
|
||||
if end <= start || lines.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let escaped_lines: Vec<String> = lines.into_iter().map(escape_markup).collect();
|
||||
doc.cues.push(Cue { start, end, text: escaped_lines.join("\n"), ..Default::default() });
|
||||
}
|
||||
|
||||
if doc.cues.is_empty() && !text.trim().is_empty() {
|
||||
return Err(Error::NotFormat("RealText"));
|
||||
}
|
||||
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
pub fn write(doc: &Document) -> String {
|
||||
let mut out = String::new();
|
||||
out.push_str("<window type=\"generic\" width=\"320\" height=\"60\" wordwrap=\"true\">\n");
|
||||
|
||||
for c in &doc.cues {
|
||||
let start_ms = tick_to_ms(c.start);
|
||||
let mut end_ms = tick_to_ms(c.end);
|
||||
if (end_ms / 10) <= (start_ms / 10) {
|
||||
end_ms = start_ms.saturating_add(10);
|
||||
}
|
||||
let lines: Vec<String> = plain_lines(&c.text).into_iter().map(|l| escape_markup(&l)).filter(|l| !l.is_empty()).collect();
|
||||
|
||||
if !lines.is_empty() {
|
||||
let body = lines.join("<br/>");
|
||||
out.push_str(&format!("<time begin=\"{}\" end=\"{}\"/>{}\n", format_time(start_ms), format_time(end_ms), body));
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str("</window>\n");
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_and_write_roundtrip() {
|
||||
let rt = "<window type=\"generic\" width=\"320\" height=\"60\" wordwrap=\"true\">\n<time begin=\"00:01.00\" end=\"00:03.00\"/>Hello world\n<time begin=\"00:04.50\" end=\"00:06.25\"/>Second cue<br/>with newline\n</window>\n";
|
||||
let doc = parse(rt).expect("parse");
|
||||
assert_eq!(doc.cues.len(), 2);
|
||||
assert_eq!(doc.cues[0].start, ms_to_tick(1000));
|
||||
assert_eq!(doc.cues[0].end, ms_to_tick(3000));
|
||||
assert_eq!(doc.cues[0].text, "Hello world");
|
||||
assert_eq!(doc.cues[1].start, ms_to_tick(4500));
|
||||
assert_eq!(doc.cues[1].end, ms_to_tick(6250));
|
||||
assert_eq!(doc.cues[1].text, "Second cue\nwith newline");
|
||||
|
||||
let written = write(&doc);
|
||||
let doc2 = parse(&written).expect("parse written");
|
||||
assert_eq!(doc2.cues.len(), 2);
|
||||
assert_eq!(doc2.cues[0].text, doc.cues[0].text);
|
||||
assert_eq!(doc2.cues[1].text, doc.cues[1].text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamps() {
|
||||
assert_eq!(parse_time("5"), Some(5000));
|
||||
assert_eq!(parse_time("5.5"), Some(5500));
|
||||
assert_eq!(parse_time("00:02.50"), Some(2500));
|
||||
assert_eq!(parse_time("01:02.500"), Some(62500));
|
||||
assert_eq!(parse_time("01:02:03.40"), Some(3723400));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn does_not_misidentify_embedded_window_in_pjs() {
|
||||
let pjs = "10,50,\"Use <window> here\"\n";
|
||||
assert!(!is_realtext(pjs));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_cue_quantization_roundtrip() {
|
||||
let mut doc = Document::default();
|
||||
doc.cues.push(Cue { start: ms_to_tick(1000), end: ms_to_tick(1001), text: "Short".into(), ..Default::default() });
|
||||
let written = write(&doc);
|
||||
let parsed = parse(&written).expect("parse short cue document");
|
||||
assert_eq!(parsed.cues.len(), 1);
|
||||
assert_eq!(parsed.cues[0].text, "Short");
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,52 @@ pub fn plain_text(s: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
use crate::TICKS_PER_MS;
|
||||
use crate::time::{TICKS_PER_SECOND, Tick};
|
||||
|
||||
/// How long the last cue of a format that gives only start times lasts: such a file says when a
|
||||
/// caption appears, and the next one ends it.
|
||||
pub(crate) const LAST_CUE: Tick = Tick(4 * TICKS_PER_SECOND);
|
||||
|
||||
/// `start + LAST_CUE`, not overflowing.
|
||||
pub(crate) fn last_cue_end(start: Tick) -> Tick {
|
||||
Tick(start.0.saturating_add(LAST_CUE.0))
|
||||
}
|
||||
|
||||
/// Milliseconds as a time.
|
||||
pub(crate) fn ms_to_tick(ms: i64) -> Tick {
|
||||
Tick(ms.saturating_mul(TICKS_PER_MS))
|
||||
}
|
||||
|
||||
/// A time in whole milliseconds, rounded to the nearest; negative times read as zero.
|
||||
pub(crate) fn tick_to_ms(t: Tick) -> i64 {
|
||||
let per = i128::from(TICKS_PER_MS);
|
||||
((i128::from(t.0.max(0)) + per / 2) / per) as i64
|
||||
}
|
||||
|
||||
/// A decimal number of at most nine digits (so that times built from it cannot overflow).
|
||||
pub(crate) fn number(s: &str) -> Option<i64> {
|
||||
if s.is_empty() || s.len() > 9 || !s.bytes().all(|b| b.is_ascii_digit()) { None } else { s.parse().ok() }
|
||||
}
|
||||
|
||||
/// Plain text as cue text: a literal `&` or `<` becomes an entity so that it is not read as markup.
|
||||
pub(crate) fn escape_markup(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'&' => out.push_str("&"),
|
||||
'<' => out.push_str("<"),
|
||||
_ => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The cue's text as plain lines (tags gone, entities decoded, empty lines dropped).
|
||||
pub(crate) fn plain_lines(text: &str) -> Vec<String> {
|
||||
text.lines().map(plain_text).map(|l| l.trim().to_string()).filter(|l| !l.is_empty()).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -2,6 +2,7 @@ mod ass;
|
||||
mod limits;
|
||||
mod literal_roundtrips;
|
||||
mod mcc_708_timing;
|
||||
mod pjs_realtext_jacosub;
|
||||
mod roundtrip;
|
||||
mod ttml_body_styles;
|
||||
mod ttml_body_timing;
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
//! Integration and FFmpeg oracle tests for PJS, RealText, and JACOsub legacy caption formats.
|
||||
|
||||
use craft_captions::{Cue, Document, Format, TICKS_PER_SECOND, Tick, WriteOptions, detect, parse, write};
|
||||
|
||||
fn ms(millis: i64) -> Tick {
|
||||
Tick(millis.saturating_mul(TICKS_PER_SECOND / 1000))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_legacy_formats() {
|
||||
let pjs = b"10,50,\"Hello world\"\n60,120,\"Line one|Line two\"\n";
|
||||
assert_eq!(detect(pjs, Some("pjs")), Some(Format::Pjs));
|
||||
assert_eq!(detect(pjs, None), Some(Format::Pjs));
|
||||
|
||||
let rt = b"<window type=\"generic\" width=\"320\" height=\"60\">\n<time begin=\"00:01.00\" end=\"00:03.00\"/>Hello\n</window>\n";
|
||||
assert_eq!(detect(rt, Some("rt")), Some(Format::RealText));
|
||||
assert_eq!(detect(rt, None), Some(Format::RealText));
|
||||
|
||||
let jss = b"# JACOsub file\n#TimeRes 100\n0:00:01.00 0:00:03.00 D0 Hello\n";
|
||||
assert_eq!(detect(jss, Some("jss")), Some(Format::JacoSub));
|
||||
assert_eq!(detect(jss, Some("js")), Some(Format::JacoSub));
|
||||
assert_eq!(detect(jss, None), Some(Format::JacoSub));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pjs_roundtrip_and_edge_cases() {
|
||||
let doc = Document {
|
||||
cues: vec![
|
||||
Cue { start: ms(1000), end: ms(4500), text: "First caption".into(), ..Default::default() },
|
||||
Cue { start: ms(6000), end: ms(10200), text: "Line 1\nLine 2".into(), ..Default::default() },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let bytes = write(&doc, Format::Pjs, WriteOptions::default());
|
||||
let parsed = parse(&bytes, Format::Pjs).expect("parse pjs");
|
||||
assert_eq!(parsed.cues.len(), 2);
|
||||
assert_eq!(parsed.cues[0].start, ms(1000));
|
||||
assert_eq!(parsed.cues[0].end, ms(4500));
|
||||
assert_eq!(parsed.cues[0].text, "First caption");
|
||||
assert_eq!(parsed.cues[1].start, ms(6000));
|
||||
assert_eq!(parsed.cues[1].end, ms(10200));
|
||||
assert_eq!(parsed.cues[1].text, "Line 1\nLine 2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_realtext_roundtrip_and_edge_cases() {
|
||||
let doc = Document {
|
||||
cues: vec![
|
||||
Cue { start: ms(2000), end: ms(5500), text: "Welcome to RealText".into(), ..Default::default() },
|
||||
Cue { start: ms(7000), end: ms(12000), text: "Multi-line\nRealText caption".into(), ..Default::default() },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let bytes = write(&doc, Format::RealText, WriteOptions::default());
|
||||
let parsed = parse(&bytes, Format::RealText).expect("parse realtext");
|
||||
assert_eq!(parsed.cues.len(), 2);
|
||||
assert_eq!(parsed.cues[0].start, ms(2000));
|
||||
assert_eq!(parsed.cues[0].end, ms(5500));
|
||||
assert_eq!(parsed.cues[0].text, "Welcome to RealText");
|
||||
assert_eq!(parsed.cues[1].start, ms(7000));
|
||||
assert_eq!(parsed.cues[1].end, ms(12000));
|
||||
assert_eq!(parsed.cues[1].text, "Multi-line\nRealText caption");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_jacosub_roundtrip_and_edge_cases() {
|
||||
let doc = Document {
|
||||
cues: vec![
|
||||
Cue { start: ms(1000), end: ms(4000), text: "First JACOsub cue".into(), ..Default::default() },
|
||||
Cue { start: ms(5500), end: ms(9200), text: "Line A\nLine B".into(), ..Default::default() },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let bytes = write(&doc, Format::JacoSub, WriteOptions::default());
|
||||
let parsed = parse(&bytes, Format::JacoSub).expect("parse jacosub");
|
||||
assert_eq!(parsed.cues.len(), 2);
|
||||
assert_eq!(parsed.cues[0].start, ms(1000));
|
||||
assert_eq!(parsed.cues[0].end, ms(4000));
|
||||
assert_eq!(parsed.cues[0].text, "First JACOsub cue");
|
||||
assert_eq!(parsed.cues[1].start, ms(5500));
|
||||
assert_eq!(parsed.cues[1].end, ms(9200));
|
||||
assert_eq!(parsed.cues[1].text, "Line A\nLine B");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hostile_inputs_never_panic() {
|
||||
for format in [Format::Pjs, Format::RealText, Format::JacoSub] {
|
||||
let _ = parse(b"", format);
|
||||
let _ = parse(b" \n\r\n\t ", format);
|
||||
let _ = parse(b"invalid text without any headers or timestamps\nfoo bar baz", format);
|
||||
let _ = parse(b"9999999999999999999999999999999999,99999999999999999999,\"overflow\"", format);
|
||||
let _ = parse(b"<window><time begin=\"invalid\" end=\"NaN\"/>unclosed", format);
|
||||
let _ = parse(b"#TimeRes 0\n@9999999999999999999999 @0 D0 test", format);
|
||||
|
||||
for len in [1, 5, 20, 100, 500] {
|
||||
let garbage = vec![0xFE; len];
|
||||
let _ = parse(&garbage, format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ffmpeg_oracle_pjs_realtext_jacosub() {
|
||||
let Some(ff) = craft_media_testkit::ffmpeg_or_skip("legacy text subtitles oracle") else { return };
|
||||
|
||||
let temp_dir = std::env::temp_dir().join("craft_captions_legacy_test");
|
||||
let _ = std::fs::create_dir_all(&temp_dir);
|
||||
|
||||
// 1. PJS Oracle: write PJS -> decode with FFmpeg -> compare cues
|
||||
let pjs_doc = Document {
|
||||
cues: vec![
|
||||
Cue { start: ms(1000), end: ms(4000), text: "PJS oracle cue 1".into(), ..Default::default() },
|
||||
Cue { start: ms(5000), end: ms(9000), text: "PJS cue 2 line 1\nPJS cue 2 line 2".into(), ..Default::default() },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
let pjs_path = temp_dir.join("test.pjs");
|
||||
std::fs::write(&pjs_path, write(&pjs_doc, Format::Pjs, WriteOptions::default())).expect("write pjs");
|
||||
|
||||
let ff_pjs_srt =
|
||||
std::process::Command::new(&ff).args(["-y", "-i", pjs_path.to_str().expect("path"), "-f", "srt", "-"]).output().expect("ffmpeg pjs").stdout;
|
||||
let ff_pjs_doc = parse(&ff_pjs_srt, Format::Srt).expect("parse ff pjs srt");
|
||||
assert_eq!(ff_pjs_doc.cues.len(), pjs_doc.cues.len());
|
||||
for i in 0..pjs_doc.cues.len() {
|
||||
assert_eq!(ff_pjs_doc.cues[i].start, pjs_doc.cues[i].start);
|
||||
assert_eq!(ff_pjs_doc.cues[i].end, pjs_doc.cues[i].end);
|
||||
assert_eq!(ff_pjs_doc.cues[i].text.trim(), pjs_doc.cues[i].text.trim());
|
||||
}
|
||||
|
||||
// 2. RealText Oracle: write RealText -> decode with FFmpeg -> compare cues
|
||||
let rt_doc = Document {
|
||||
cues: vec![
|
||||
Cue { start: ms(1000), end: ms(3000), text: "RealText oracle cue 1".into(), ..Default::default() },
|
||||
Cue { start: ms(4500), end: ms(8000), text: "RealText cue 2 line 1\nRealText cue 2 line 2".into(), ..Default::default() },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
let rt_path = temp_dir.join("test.rt");
|
||||
std::fs::write(&rt_path, write(&rt_doc, Format::RealText, WriteOptions::default())).expect("write rt");
|
||||
|
||||
let ff_rt_srt =
|
||||
std::process::Command::new(&ff).args(["-y", "-i", rt_path.to_str().expect("path"), "-f", "srt", "-"]).output().expect("ffmpeg rt").stdout;
|
||||
let ff_rt_doc = parse(&ff_rt_srt, Format::Srt).expect("parse ff rt srt");
|
||||
assert_eq!(ff_rt_doc.cues.len(), rt_doc.cues.len());
|
||||
for i in 0..rt_doc.cues.len() {
|
||||
assert_eq!(ff_rt_doc.cues[i].start, rt_doc.cues[i].start);
|
||||
assert_eq!(ff_rt_doc.cues[i].end, rt_doc.cues[i].end);
|
||||
assert_eq!(ff_rt_doc.cues[i].text.trim(), rt_doc.cues[i].text.trim());
|
||||
}
|
||||
|
||||
// 3. JACOsub Oracle: write JACOsub -> decode with FFmpeg -> compare cues
|
||||
let jss_doc = Document {
|
||||
cues: vec![
|
||||
Cue { start: ms(1000), end: ms(4000), text: "JACOsub oracle cue 1".into(), ..Default::default() },
|
||||
Cue { start: ms(5000), end: ms(9000), text: "JACOsub cue 2 line 1\nJACOsub cue 2 line 2".into(), ..Default::default() },
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
let jss_path = temp_dir.join("test.jss");
|
||||
std::fs::write(&jss_path, write(&jss_doc, Format::JacoSub, WriteOptions::default())).expect("write jss");
|
||||
|
||||
let ff_jss_srt =
|
||||
std::process::Command::new(&ff).args(["-y", "-i", jss_path.to_str().expect("path"), "-f", "srt", "-"]).output().expect("ffmpeg jss").stdout;
|
||||
let ff_jss_doc = parse(&ff_jss_srt, Format::Srt).expect("parse ff jss srt");
|
||||
assert_eq!(ff_jss_doc.cues.len(), jss_doc.cues.len());
|
||||
for i in 0..jss_doc.cues.len() {
|
||||
assert_eq!(ff_jss_doc.cues[i].start, jss_doc.cues[i].start);
|
||||
assert_eq!(ff_jss_doc.cues[i].end, jss_doc.cues[i].end);
|
||||
assert_eq!(ff_jss_doc.cues[i].text.trim(), jss_doc.cues[i].text.trim());
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,33 @@
|
||||
# craft-doc-binary
|
||||
|
||||
Reader for the Word 97-2003 binary file format (`.doc`, [MS-DOC]) into the `craft-wordmodel`
|
||||
document model. It is WordCraft's reader, on `craft-cfb` for the compound file and
|
||||
`craft-codepage` for 8-bit text.
|
||||
Reader and writer for the Word 97-2003 binary file format (`.doc`, [MS-DOC]) and the
|
||||
`craft-wordmodel` document model. It is WordCraft's reader and writer, on `craft-cfb` for the
|
||||
compound file and `craft-codepage` for 8-bit text.
|
||||
|
||||
```rust
|
||||
// Not a compound file: an error, never a panic.
|
||||
assert!(craft_doc_binary::read(b"not a .doc").is_err());
|
||||
assert!(!craft_doc_binary::is_encrypted(b"not a .doc"));
|
||||
|
||||
// A document written as a .doc reads back.
|
||||
let bytes = craft_doc_binary::write(&craft_wordmodel::Document::from_text("Hello")).unwrap();
|
||||
let doc = craft_doc_binary::read(&bytes).unwrap();
|
||||
assert_eq!(doc.body[0].as_para().map(|p| p.text.as_str()), Some("Hello"));
|
||||
```
|
||||
|
||||
- `read(bytes)`, `read_with_password(bytes, Option<&str>)`, `is_encrypted(bytes)`.
|
||||
- `read(bytes)`, `read_with_password(bytes, Option<&str>)`, `is_encrypted(bytes)`, `write(&document)`.
|
||||
- Text in pieces (8-bit and UTF-16), paragraph and character formatting, styles, lists,
|
||||
tables, sections, headers and footers, notes, comments, fields, bookmarks, pictures and
|
||||
OfficeArt shapes, document properties.
|
||||
- XOR obfuscation, RC4 and RC4 CryptoAPI password protection.
|
||||
- Tracked insertions and deletions, with their author and time.
|
||||
|
||||
There is no writer.
|
||||
`write` produces a Word 97-2003 file (nFib 0x00C1, a version 3 compound file): text, character
|
||||
and paragraph formatting, styles, fonts, lists, tables, sections with headers and footers,
|
||||
footnotes and endnotes, fields and hyperlinks, bookmarks, inline pictures, tracked insertions
|
||||
and deletions, and the document properties. Floating pictures are written in line, nested tables
|
||||
become paragraphs of their cell, and shapes, charts, text boxes and comments are left out, each
|
||||
kind logged once per save. It never writes a password-protected file.
|
||||
|
||||
## Limits
|
||||
|
||||
|
||||
@@ -110,8 +110,8 @@ pub(crate) fn ico(i: u8) -> Option<Rgb> {
|
||||
}
|
||||
|
||||
/// The model border style a `brcType` stands for ([MS-DOC] §2.9.22 BrcType; 0x02, thick, is in
|
||||
/// the BRC of Microsoft's Word 97-2007 specification). Types the model doesn't carry (the gap
|
||||
/// variants, 3-D, inset/outset) draw as single lines, as the DOCX reader maps them.
|
||||
/// the BRC of Microsoft's Word 97-2007 specification); 0x0B-0x13 are the thin/thick compound
|
||||
/// lines. Types the model doesn't carry (3-D, inset/outset, art borders) draw as single lines.
|
||||
fn brc_style(t: u8) -> BorderStyle {
|
||||
match t {
|
||||
0x00 => BorderStyle::None,
|
||||
@@ -121,6 +121,15 @@ fn brc_style(t: u8) -> BorderStyle {
|
||||
0x07 | 0x16 => BorderStyle::Dashed,
|
||||
0x08 | 0x09 => BorderStyle::DotDash,
|
||||
0x0A => BorderStyle::Triple,
|
||||
0x0B => BorderStyle::ThinThickSmallGap,
|
||||
0x0C => BorderStyle::ThickThinSmallGap,
|
||||
0x0D => BorderStyle::ThinThickThinSmallGap,
|
||||
0x0E => BorderStyle::ThinThickMediumGap,
|
||||
0x0F => BorderStyle::ThickThinMediumGap,
|
||||
0x10 => BorderStyle::ThinThickThinMediumGap,
|
||||
0x11 => BorderStyle::ThinThickLargeGap,
|
||||
0x12 => BorderStyle::ThickThinLargeGap,
|
||||
0x13 => BorderStyle::ThinThickThinLargeGap,
|
||||
0x14 | 0x15 => BorderStyle::Wave,
|
||||
_ => BorderStyle::Single,
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
//! Reader for the Word 97-2003 binary file format (`.doc`, [MS-DOC]), WordCraft's.
|
||||
//! Reader and writer for the Word 97-2003 binary file format (`.doc`, [MS-DOC]), WordCraft's.
|
||||
//!
|
||||
//! [`read`] turns the bytes of a `.doc` file into a [`craft_wordmodel::Document`]. There is no
|
||||
//! writer: saving always goes through the other formats. The reader is written from the public
|
||||
//! specification ([MS-DOC], Word (.doc) Binary File Format, Microsoft Open Specifications) and,
|
||||
//! like the other Craft parsers, is lenient by design (unknown structures are skipped, bad numbers
|
||||
//! fall back to defaults) and bounded (stream sizes, piece counts and property counts are
|
||||
//! capped), so hostile input yields an error or a best-effort document, never a panic.
|
||||
//! [`read`] turns the bytes of a `.doc` file into a [`craft_wordmodel::Document`], and [`write`]
|
||||
//! turns a document into a Word 97-2003 `.doc` file (the `write` module lists what it keeps).
|
||||
//! Both follow the public specification ([MS-DOC], Word (.doc) Binary File Format, Microsoft Open
|
||||
//! Specifications). The reader, like the other Craft parsers, is lenient by design (unknown
|
||||
//! structures are skipped, bad numbers fall back to defaults) and bounded (stream sizes, piece
|
||||
//! counts and property counts are capped), so hostile input yields an error or a best-effort
|
||||
//! document, never a panic.
|
||||
//!
|
||||
//! Word 6.0/95 files, which that specification does not cover, are read by `word6` from their
|
||||
//! observed layout: their text, formatting and tables so far.
|
||||
@@ -28,11 +29,15 @@ mod metadata;
|
||||
mod notes;
|
||||
mod officeart;
|
||||
mod piece;
|
||||
mod rmark;
|
||||
mod sections;
|
||||
mod sprm;
|
||||
mod stsh;
|
||||
mod table;
|
||||
mod word6;
|
||||
mod write;
|
||||
|
||||
pub use write::write;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -303,6 +308,7 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) ->
|
||||
// tight against aliasing — and never past the absolute ceiling.
|
||||
let budget = std::cell::Cell::new((word.len() as u64).saturating_mul(2).min(MAX_TOTAL_CHARS));
|
||||
let work = Work::new(MAX_WORK);
|
||||
let revs = rmark::Revisions::new(&table, &fib);
|
||||
let paragraphs = ParaBudget { left: std::cell::Cell::new(max_paragraphs), exceeded: std::cell::Cell::new(false) };
|
||||
|
||||
let ctx = WalkCtx {
|
||||
@@ -321,6 +327,7 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) ->
|
||||
budget: &budget,
|
||||
paragraphs: ¶graphs,
|
||||
work: &work,
|
||||
revs: &revs,
|
||||
};
|
||||
|
||||
// Drawings: text box stories become parts first, then the floating shapes and pictures
|
||||
@@ -432,6 +439,7 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) ->
|
||||
return Err(paragraph_limit(max_paragraphs));
|
||||
}
|
||||
doc.media.extend(pics.media);
|
||||
doc.revisions = revs.take();
|
||||
doc.body = table::assemble(paras);
|
||||
doc.ensure_nonempty();
|
||||
work.log();
|
||||
@@ -460,6 +468,7 @@ fn read_word6(comp: &craft_cfb::CompoundFile<'_>, word: &[u8], max_paragraphs: u
|
||||
let work = Work::new(MAX_WORK);
|
||||
let paragraphs = ParaBudget { left: std::cell::Cell::new(max_paragraphs), exceeded: std::cell::Cell::new(false) };
|
||||
let mut pics = media::Pictures::for_input(word.len());
|
||||
let revs = rmark::Revisions::none();
|
||||
let ctx = WalkCtx {
|
||||
word,
|
||||
data: &[],
|
||||
@@ -476,6 +485,7 @@ fn read_word6(comp: &craft_cfb::CompoundFile<'_>, word: &[u8], max_paragraphs: u
|
||||
budget: &budget,
|
||||
paragraphs: ¶graphs,
|
||||
work: &work,
|
||||
revs: &revs,
|
||||
};
|
||||
let paras = walk(&ctx, 0, fib.ccp_text, &mut pics);
|
||||
if paragraphs.exceeded.get() {
|
||||
@@ -574,6 +584,8 @@ struct WalkCtx<'a> {
|
||||
paragraphs: &'a ParaBudget,
|
||||
/// Repeated-work bytes left ([`MAX_WORK`]), and what they left out.
|
||||
work: &'a Work,
|
||||
/// Tracked insertions and deletions met so far.
|
||||
revs: &'a rmark::Revisions,
|
||||
}
|
||||
|
||||
/// The paragraph and cell marks still allowed in this document, and whether a walk ran into
|
||||
@@ -748,7 +760,10 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic
|
||||
pb.props.numbering = Some(NumRef { num, level: ilvl });
|
||||
}
|
||||
let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
pb.mark = char_props(ctx.chpx_bins.chpx(ctx.word, fc), ctx.fonts, &CharProps::default());
|
||||
let chpx = ctx.chpx_bins.chpx(ctx.word, fc);
|
||||
pb.mark = char_props(chpx, ctx.fonts, &CharProps::default());
|
||||
// A tracked paragraph mark: an inserted or deleted paragraph break.
|
||||
ctx.revs.mark(&mut pb.mark, chpx, ctx.pieces.piece_prm(cp));
|
||||
let done = std::mem::take(&mut pb);
|
||||
out.push(ParaOut {
|
||||
para: Paragraph {
|
||||
@@ -769,7 +784,7 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic
|
||||
Some(f) => {
|
||||
f.depth = f.depth.saturating_add(1);
|
||||
}
|
||||
None => field = Some(FieldBuild { depth: 1, instr: String::new(), sep: None }),
|
||||
None => field = Some(FieldBuild { depth: 1, instr: String::new(), sep: None, anchor: anchor_props(ctx, cp) }),
|
||||
}
|
||||
cp = cp.saturating_add(1);
|
||||
continue;
|
||||
@@ -799,13 +814,16 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic
|
||||
'\u{1}' => {
|
||||
// Inline picture: the CHPX of the anchor carries sprmCPicLocation.
|
||||
let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
let fc_pic = pic_location(ctx.chpx_bins.chpx(ctx.word, fc));
|
||||
let chpx = ctx.chpx_bins.chpx(ctx.word, fc);
|
||||
let fc_pic = pic_location(chpx);
|
||||
match fc_pic.and_then(|at| pics.get(ctx.data, at)) {
|
||||
Some(p) => {
|
||||
// Every anchor of a picture gets its own copy of the alt text; the
|
||||
// copies are charged to the work budget (#3709).
|
||||
let alt = if ctx.work.charge(p.alt.len() as u64, Cost::AltText) { p.alt.clone() } else { String::new() };
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
let mut props = CharProps::default();
|
||||
ctx.revs.mark(&mut props, chpx, ctx.pieces.piece_prm(cp));
|
||||
pb.push('\u{FFFC}', props);
|
||||
pb.objects.push(InlineObject::Image {
|
||||
media: p.key.clone(),
|
||||
w: p.w,
|
||||
@@ -829,7 +847,7 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic
|
||||
if !ctx.work.charge(object_cost(obj), Cost::FloatingObject) {
|
||||
continue;
|
||||
}
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.push('\u{FFFC}', anchor_props(ctx, cp));
|
||||
pb.objects.push(obj.clone());
|
||||
}
|
||||
}
|
||||
@@ -837,7 +855,7 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic
|
||||
// Footnote or endnote reference; custom symbols share the same character.
|
||||
let at = ctx.notes.partition_point(|(c, _, _)| *c < cp);
|
||||
if let Some((_, kind, id)) = ctx.notes.get(at).filter(|(c, _, _)| *c == cp) {
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.push('\u{FFFC}', anchor_props(ctx, cp));
|
||||
pb.objects.push(InlineObject::NoteRef { kind: *kind, id: *id, custom: String::new() });
|
||||
}
|
||||
}
|
||||
@@ -871,6 +889,8 @@ struct FieldBuild {
|
||||
depth: u32,
|
||||
instr: String,
|
||||
sep: Option<FieldResult>,
|
||||
/// The begin character's revision marks, for the field object's anchor.
|
||||
anchor: CharProps,
|
||||
}
|
||||
|
||||
/// A field's cached result: its text and, as runs, the formatting of that text. A run is added
|
||||
@@ -905,6 +925,15 @@ fn object_cost(obj: &InlineObject) -> u64 {
|
||||
}
|
||||
}
|
||||
|
||||
/// The properties of an object anchor at `cp`: only its revision marks, so a tracked
|
||||
/// insertion or deletion of a picture, note reference or field survives.
|
||||
fn anchor_props(ctx: &WalkCtx<'_>, cp: u32) -> CharProps {
|
||||
let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
let mut props = CharProps::default();
|
||||
ctx.revs.mark(&mut props, ctx.chpx_bins.chpx(ctx.word, fc), ctx.pieces.piece_prm(cp));
|
||||
props
|
||||
}
|
||||
|
||||
/// Emit a finished field: HYPERLINK keeps its result text as a linked run; every other
|
||||
/// field becomes one inline object with its cached result.
|
||||
///
|
||||
@@ -934,7 +963,7 @@ fn finish_field(pb: &mut ParaBuild, f: FieldBuild, work: &Work) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.push('\u{FFFC}', f.anchor);
|
||||
pb.objects.push(InlineObject::Field { instr, result: result.text, locked: false, dirty: false, code: false, form: String::new() });
|
||||
}
|
||||
|
||||
@@ -1012,7 +1041,9 @@ fn push_formatted<'a>(pb: &mut ParaBuild, cp: u32, c: char, ctx: &WalkCtx<'a>, f
|
||||
}
|
||||
};
|
||||
let mut props = char_props(grpprl, ctx.fonts, base);
|
||||
apply_prm(&mut props, ctx.pieces.piece_prm(cp), ctx.fonts, base, ctx.work);
|
||||
let piece_prm = ctx.pieces.piece_prm(cp);
|
||||
apply_prm(&mut props, piece_prm, ctx.fonts, base, ctx.work);
|
||||
ctx.revs.mark(&mut props, grpprl, piece_prm);
|
||||
st.memo = Some(FmtMemo { chpx: grpprl, prm, style: style.map(str::to_owned), props });
|
||||
st.generation += 1;
|
||||
}
|
||||
@@ -1083,7 +1114,7 @@ fn prm_paragraph_sprms(prm: PrmRef<'_>, work: &Work) -> Vec<u8> {
|
||||
out.push(val);
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl(Some(g)) => {
|
||||
PrmRef::Grpprl { grpprl: Some(g), .. } => {
|
||||
if !work.charge(g.len() as u64, Cost::Formatting) {
|
||||
return out;
|
||||
}
|
||||
@@ -1092,7 +1123,7 @@ fn prm_paragraph_sprms(prm: PrmRef<'_>, work: &Work) -> Vec<u8> {
|
||||
out.extend_from_slice(prl.operand);
|
||||
}
|
||||
}
|
||||
PrmRef::None | PrmRef::Grpprl(None) => {}
|
||||
PrmRef::None | PrmRef::Grpprl { grpprl: None, .. } => {}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -1111,7 +1142,7 @@ fn apply_prm(props: &mut CharProps, prm: PrmRef<'_>, fonts: &[String], base: &Ch
|
||||
}
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl(Some(g)) => {
|
||||
PrmRef::Grpprl { grpprl: Some(g), .. } => {
|
||||
if !work.charge(g.len() as u64, Cost::Formatting) {
|
||||
return;
|
||||
}
|
||||
@@ -1121,7 +1152,7 @@ fn apply_prm(props: &mut CharProps, prm: PrmRef<'_>, fonts: &[String], base: &Ch
|
||||
}
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl(None) => {}
|
||||
PrmRef::Grpprl { grpprl: None, .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,15 +35,19 @@ struct RawLvl {
|
||||
xst: Vec<u16>,
|
||||
}
|
||||
|
||||
/// A bullet level's character, with the 0xF000 flag bits stripped and the common
|
||||
/// Symbol/Wingdings glyphs mapped to their Unicode equivalents so every font renders them.
|
||||
/// A bullet level's character: a symbol-font character (0xF000 to 0xF0FF, the font's own code
|
||||
/// in the low byte) with the common Symbol/Wingdings glyphs mapped to their Unicode equivalents
|
||||
/// so every font renders them; any other character is itself.
|
||||
fn bullet_char(c: u16) -> char {
|
||||
match c & 0x0FFF {
|
||||
if !(0xF000..=0xF0FF).contains(&c) {
|
||||
return char::from_u32(u32::from(c)).unwrap_or('\u{2022}');
|
||||
}
|
||||
match c & 0x00FF {
|
||||
0xB7 => '\u{2022}', // Symbol bullet
|
||||
0xA7 => '\u{25AA}', // Wingdings small square
|
||||
0x6F => '\u{25CB}', // Symbol open circle
|
||||
0x77 | 0x78 => '\u{25CF}', // Wingdings filled circles
|
||||
other => char::from_u32(other as u32).unwrap_or('\u{2022}'),
|
||||
other => char::from_u32(u32::from(other)).unwrap_or('\u{2022}'),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +55,7 @@ fn bullet_char(c: u16) -> char {
|
||||
/// 0x00 to 0x27 run in the order of ST_NumberFormat (decimal, upperRoman … 0x16 decimalZero, 0x17
|
||||
/// bullet … 0x25 chineseCounting, 0x26 chineseLegalSimplified, 0x27 chineseCountingThousand);
|
||||
/// later codes are drawn as decimal.
|
||||
fn num_format(nfc: u8) -> NumFormat {
|
||||
pub(crate) fn num_format(nfc: u8) -> NumFormat {
|
||||
match nfc {
|
||||
0xFF => NumFormat::None,
|
||||
0x00..=0x27 => NumFormat::ALL.get(nfc as usize).copied().unwrap_or(NumFormat::Decimal),
|
||||
|
||||
@@ -15,6 +15,12 @@ fn u32_at(bytes: &[u8], offset: usize) -> Option<u32> {
|
||||
Some(u32::from_le_bytes(bytes.get(offset..offset.checked_add(4)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
/// [`parse`], for the writer's tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn parse_for_tests(bytes: &[u8]) -> Option<CoreProps> {
|
||||
parse(bytes)
|
||||
}
|
||||
|
||||
fn parse(bytes: &[u8]) -> Option<CoreProps> {
|
||||
if bytes.get(..2)? != [0xfe, 0xff] || bytes.get(28..44)? != SUMMARY_FMTID || u32_at(bytes, 24)? < 1 {
|
||||
return None;
|
||||
|
||||
@@ -93,6 +93,11 @@ pub(crate) fn parse_bookmarks(table: &[u8], fib: &crate::fib::Fib) -> Vec<(u32,
|
||||
/// (1-byte length and Windows-1252 chars). `cbExtra` bytes after each string are skipped.
|
||||
/// Names are kept to 40 characters, but the reader always advances by the stored length.
|
||||
fn sttb_strings(table: &[u8], fc: u32, lcb: u32) -> Option<Vec<String>> {
|
||||
sttb_strings_max(table, fc, lcb, 40)
|
||||
}
|
||||
|
||||
/// [`sttb_strings`] keeping up to `max` characters of each string.
|
||||
pub(crate) fn sttb_strings_max(table: &[u8], fc: u32, lcb: u32, max: usize) -> Option<Vec<String>> {
|
||||
let b = table.get(fc as usize..(fc as usize).saturating_add(lcb as usize))?;
|
||||
let extended = b.get(0..2)? == [0xFF, 0xFF];
|
||||
let head = if extended { 2 } else { 0 };
|
||||
@@ -125,9 +130,10 @@ fn sttb_strings(table: &[u8], fc: u32, lcb: u32) -> Option<Vec<String>> {
|
||||
}
|
||||
at += cb_extra;
|
||||
let s: String = if extended {
|
||||
chars.as_chunks::<2>().0.iter().take(40).map(|c| char::from_u32(u16::from_le_bytes(*c) as u32).unwrap_or('\u{FFFD}')).collect()
|
||||
// UTF-16: a surrogate pair is one character; a lone surrogate becomes U+FFFD.
|
||||
char::decode_utf16(chars.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c))).take(max).map(|c| c.unwrap_or('\u{FFFD}')).collect()
|
||||
} else {
|
||||
chars.iter().take(40).map(|&c| craft_codepage::cp1252(c)).collect()
|
||||
chars.iter().take(max).map(|&c| craft_codepage::cp1252(c)).collect()
|
||||
};
|
||||
out.push(s);
|
||||
}
|
||||
@@ -153,6 +159,20 @@ mod tests {
|
||||
b
|
||||
}
|
||||
|
||||
/// A character outside the BMP (a surrogate pair) stays one character; a lone surrogate
|
||||
/// is U+FFFD. The limit counts characters, not UTF-16 units.
|
||||
#[test]
|
||||
fn extended_strings_decode_surrogate_pairs() {
|
||||
let mut b = ext_sttb(&["Ada \u{1F600}"], &[]);
|
||||
let lone = [0x00u8, 0xD8];
|
||||
b[2] = 2;
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&lone);
|
||||
b.extend_from_slice(&u16::from(b'x').to_le_bytes());
|
||||
let got = super::sttb_strings_max(&b, 0, b.len() as u32, 5).unwrap();
|
||||
assert_eq!(got, vec!["Ada \u{1F600}".to_string(), "\u{FFFD}x".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_bookmark_name_does_not_desync_table() {
|
||||
let long = "A".repeat(50);
|
||||
|
||||
@@ -228,7 +228,8 @@ impl PieceTable {
|
||||
}
|
||||
PrmRef::Sprm0 { isprm, val: (p.prm >> 8) as u8 }
|
||||
} else {
|
||||
PrmRef::Grpprl(self.prc_grpprls.get((p.prm >> 1) as usize).map(Vec::as_slice))
|
||||
let index = p.prm >> 1;
|
||||
PrmRef::Grpprl { index, grpprl: self.prc_grpprls.get(usize::from(index)).map(Vec::as_slice) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,6 +343,7 @@ impl PieceSpan<'_> {
|
||||
}
|
||||
|
||||
/// What a piece's `Prm` refers to.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum PrmRef<'a> {
|
||||
None,
|
||||
/// Prm0: a short sprm index into the isprm table, with a 1-byte operand.
|
||||
@@ -349,8 +351,11 @@ pub(crate) enum PrmRef<'a> {
|
||||
isprm: u8,
|
||||
val: u8,
|
||||
},
|
||||
/// Prm1: a grpprl from a Prc (absent if out of range).
|
||||
Grpprl(Option<&'a [u8]>),
|
||||
/// Prm1: the Prc at `index` and its grpprl (absent if out of range).
|
||||
Grpprl {
|
||||
index: u16,
|
||||
grpprl: Option<&'a [u8]>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Windows' code page number for symbol fonts (CP_SYMBOL).
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
//! Tracked insertions and deletions: the revision-mark sprms of a run's CHPX (`sprmCFRMarkIns`
|
||||
//! and `sprmCFRMarkDel`, each with its author index into `SttbfRMark` and its `DTTM`)
|
||||
//! ([MS-DOC] §2.6.1, §2.9.290), as model revisions.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use craft_wordmodel::props::CharProps;
|
||||
use craft_wordmodel::{Revision, RevisionKind};
|
||||
|
||||
use crate::fib::Fib;
|
||||
use crate::piece::PrmRef;
|
||||
use crate::sprm;
|
||||
|
||||
const C_RMARK_DEL: u16 = 0x0800;
|
||||
const C_RMARK_INS: u16 = 0x0801;
|
||||
const C_IBST_RMARK: u16 = 0x4804;
|
||||
const C_DTTM_RMARK: u16 = 0x6805;
|
||||
const C_IBST_RMARK_DEL: u16 = 0x4863;
|
||||
const C_DTTM_RMARK_DEL: u16 = 0x6864;
|
||||
/// `fcSttbfRMark`: the revision authors.
|
||||
const STTBF_RMARK: usize = 51;
|
||||
/// Most distinct revisions (author, time and kind) one document gets.
|
||||
const MAX_REVISIONS: usize = 100_000;
|
||||
/// Longest author name kept.
|
||||
const MAX_AUTHOR: usize = 255;
|
||||
|
||||
/// The document's revisions, made as the walk meets their marks: runs with the same author,
|
||||
/// time and kind share one.
|
||||
pub(crate) struct Revisions {
|
||||
authors: Vec<String>,
|
||||
list: RefCell<Vec<Revision>>,
|
||||
index: RefCell<HashMap<(bool, u16, u32), u32>>,
|
||||
/// The marks of each Prc a piece's Prm1 names, by its index (at most 32,768 of them): a Prc
|
||||
/// grpprl of up to 64 KB is scanned once, not at every run, paragraph mark and object anchor
|
||||
/// of its pieces.
|
||||
prcs: RefCell<HashMap<u16, Marks>>,
|
||||
}
|
||||
|
||||
impl Revisions {
|
||||
/// No revision authors: a file whose revision marks are not read (Word 6.0/95, whose
|
||||
/// revision sprms differ). Marks are still never set, as none of its sprms are Word 97's.
|
||||
pub(crate) fn none() -> Self {
|
||||
Self { authors: Vec::new(), list: RefCell::default(), index: RefCell::default(), prcs: RefCell::default() }
|
||||
}
|
||||
|
||||
pub(crate) fn new(table: &[u8], fib: &Fib) -> Self {
|
||||
let authors = fib
|
||||
.pair(STTBF_RMARK)
|
||||
.filter(|(_, lcb)| *lcb > 0)
|
||||
.and_then(|(fc, lcb)| crate::notes::sttb_strings_max(table, fc, lcb, MAX_AUTHOR))
|
||||
.unwrap_or_default();
|
||||
Revisions { authors, list: RefCell::new(Vec::new()), index: RefCell::new(HashMap::new()), prcs: RefCell::new(HashMap::new()) }
|
||||
}
|
||||
|
||||
/// Set `props.ins` / `props.del` from the revision marks of a run: those in its CHPX
|
||||
/// `grpprl`, then those of its piece's `prm`, which apply after the CHPX's.
|
||||
pub(crate) fn mark(&self, props: &mut CharProps, grpprl: &[u8], prm: PrmRef<'_>) {
|
||||
let mut m = Marks::of(grpprl);
|
||||
match prm {
|
||||
PrmRef::Sprm0 { isprm, val } => {
|
||||
if let Some(op) = sprm::from_prm0(isprm) {
|
||||
m.apply(op, &[val]);
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl { index, grpprl: Some(g) } => {
|
||||
let prc = *self.prcs.borrow_mut().entry(index).or_insert_with(|| Marks::of(g));
|
||||
m = m.then(prc);
|
||||
}
|
||||
PrmRef::Grpprl { grpprl: None, .. } | PrmRef::None => {}
|
||||
}
|
||||
if m.ins == Some(true) {
|
||||
props.ins = self.id(true, m.ibst_ins.unwrap_or(0), m.dttm_ins.unwrap_or(0));
|
||||
}
|
||||
if m.del == Some(true) {
|
||||
props.del = self.id(false, m.ibst_del.unwrap_or(0), m.dttm_del.unwrap_or(0));
|
||||
}
|
||||
}
|
||||
|
||||
/// The index of the revision of this kind, author and time, made on first use.
|
||||
fn id(&self, insert: bool, ibst: u16, dttm: u32) -> Option<u32> {
|
||||
let key = (insert, ibst, dttm);
|
||||
if let Some(&i) = self.index.borrow().get(&key) {
|
||||
return Some(i);
|
||||
}
|
||||
let mut list = self.list.borrow_mut();
|
||||
if list.len() >= MAX_REVISIONS {
|
||||
return None;
|
||||
}
|
||||
let author = self.authors.get(usize::from(ibst)).cloned().unwrap_or_else(|| "Unknown".to_string());
|
||||
let kind = if insert { RevisionKind::Insert } else { RevisionKind::Delete };
|
||||
list.push(Revision::new(kind, author, dttm_iso(dttm)));
|
||||
let i = u32::try_from(list.len() - 1).ok()?;
|
||||
self.index.borrow_mut().insert(key, i);
|
||||
Some(i)
|
||||
}
|
||||
|
||||
pub(crate) fn take(self) -> Vec<Revision> {
|
||||
self.list.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// The revision-mark sprms seen so far for one run, a later one replacing an earlier one; each
|
||||
/// is `None` until one is seen.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct Marks {
|
||||
ins: Option<bool>,
|
||||
del: Option<bool>,
|
||||
ibst_ins: Option<u16>,
|
||||
dttm_ins: Option<u32>,
|
||||
ibst_del: Option<u16>,
|
||||
dttm_del: Option<u32>,
|
||||
}
|
||||
|
||||
impl Marks {
|
||||
/// The marks of one grpprl.
|
||||
fn of(grpprl: &[u8]) -> Self {
|
||||
let mut m = Marks::default();
|
||||
for prl in sprm::iter(grpprl) {
|
||||
m.apply(prl.op, prl.operand);
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
/// These marks with `later`'s applied after them, as if its sprms came next.
|
||||
fn then(self, later: Marks) -> Self {
|
||||
Marks {
|
||||
ins: later.ins.or(self.ins),
|
||||
del: later.del.or(self.del),
|
||||
ibst_ins: later.ibst_ins.or(self.ibst_ins),
|
||||
dttm_ins: later.dttm_ins.or(self.dttm_ins),
|
||||
ibst_del: later.ibst_del.or(self.ibst_del),
|
||||
dttm_del: later.dttm_del.or(self.dttm_del),
|
||||
}
|
||||
}
|
||||
|
||||
fn apply(&mut self, op: u16, operand: &[u8]) {
|
||||
let u16v = || operand.get(..2).map_or(0, |b| u16::from_le_bytes([b[0], b[1]]));
|
||||
let u32v = || operand.get(..4).map_or(0, |b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
|
||||
match op {
|
||||
C_RMARK_INS => self.ins = Some(operand.first().is_some_and(|&v| v == 1 || v == 0x81)),
|
||||
C_RMARK_DEL => self.del = Some(operand.first().is_some_and(|&v| v == 1 || v == 0x81)),
|
||||
C_IBST_RMARK => self.ibst_ins = Some(u16v()),
|
||||
C_DTTM_RMARK => self.dttm_ins = Some(u32v()),
|
||||
C_IBST_RMARK_DEL => self.ibst_del = Some(u16v()),
|
||||
C_DTTM_RMARK_DEL => self.dttm_del = Some(u32v()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A `DTTM` ([MS-DOC] §2.9.75) as an ISO 8601 UTC time; empty for 0 or an impossible date.
|
||||
pub(crate) fn dttm_iso(d: u32) -> String {
|
||||
if d == 0 {
|
||||
return String::new();
|
||||
}
|
||||
let minute = d & 0x3F;
|
||||
let hour = (d >> 6) & 0x1F;
|
||||
let day = (d >> 11) & 0x1F;
|
||||
let month = (d >> 16) & 0xF;
|
||||
let year = 1900 + ((d >> 20) & 0x1FF) as i32;
|
||||
match chrono::NaiveDate::from_ymd_opt(year, month, day).and_then(|date| date.and_hms_opt(hour, minute, 0)) {
|
||||
Some(t) => t.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn revisions() -> Revisions {
|
||||
Revisions {
|
||||
authors: vec!["Ada".into(), "Grace".into()],
|
||||
list: RefCell::new(Vec::new()),
|
||||
index: RefCell::new(HashMap::new()),
|
||||
prcs: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn marked(chpx: &[u8], prm: PrmRef<'_>) -> (CharProps, Vec<Revision>) {
|
||||
let r = revisions();
|
||||
let mut props = CharProps::default();
|
||||
r.mark(&mut props, chpx, prm);
|
||||
(props, r.take())
|
||||
}
|
||||
|
||||
const INS: [u8; 3] = [0x01, 0x08, 1];
|
||||
const NOT_INS: [u8; 3] = [0x01, 0x08, 0];
|
||||
/// sprmCFRMarkDel on, by the author at index 1.
|
||||
const DEL_BY_GRACE: [u8; 7] = [0x00, 0x08, 1, 0x63, 0x48, 1, 0];
|
||||
|
||||
#[test]
|
||||
fn a_piece_prm_marks_a_run() {
|
||||
// Prm1: a grpprl from the piece table's Prc.
|
||||
let (p, revs) = marked(&[], PrmRef::Grpprl { index: 0, grpprl: Some(&DEL_BY_GRACE) });
|
||||
assert_eq!(p.del, Some(0));
|
||||
assert_eq!((revs[0].kind, revs[0].author.as_str()), (RevisionKind::Delete, "Grace"));
|
||||
// Prm0: sprmCFRMarkIns by its isprm.
|
||||
let (p, revs) = marked(&[], PrmRef::Sprm0 { isprm: 0x42, val: 1 });
|
||||
assert_eq!(p.ins, Some(0));
|
||||
assert_eq!((revs[0].kind, revs[0].author.as_str()), (RevisionKind::Insert, "Ada"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_piece_prm_applies_after_the_chpx() {
|
||||
let (p, _) = marked(&INS, PrmRef::Grpprl { index: 0, grpprl: Some(&NOT_INS) });
|
||||
assert_eq!(p.ins, None, "the Prm turns the CHPX's insertion off");
|
||||
let (p, _) = marked(&NOT_INS, PrmRef::Sprm0 { isprm: 0x42, val: 1 });
|
||||
assert_eq!(p.ins, Some(0));
|
||||
let (p, _) = marked(&INS, PrmRef::None);
|
||||
assert_eq!(p.ins, Some(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_prcs_marks_apply_after_the_chpx_one_by_one_and_are_scanned_once() {
|
||||
let r = revisions();
|
||||
// The CHPX inserts, by Grace; the Prc only sets the time, so the author stays Grace's.
|
||||
let chpx = [INS.as_slice(), &[0x04, 0x48, 1, 0]].concat();
|
||||
let prc = [0x05u8, 0x68, 0x21, 0x2B, 0xC3, 0x07];
|
||||
let mut p = CharProps::default();
|
||||
r.mark(&mut p, &chpx, PrmRef::Grpprl { index: 3, grpprl: Some(&prc) });
|
||||
assert_eq!(p.ins, Some(0));
|
||||
// The marks of Prc 3 are kept: the same index finds them without its grpprl's bytes.
|
||||
let mut again = CharProps::default();
|
||||
r.mark(&mut again, &INS, PrmRef::Grpprl { index: 3, grpprl: Some(&[]) });
|
||||
assert_eq!(again.ins, Some(1), "Ada's insertion at the Prc's time");
|
||||
let revs = r.take();
|
||||
assert_eq!((revs[0].author.as_str(), revs[0].date.as_str()), ("Grace", "2024-03-05T12:33:00Z"));
|
||||
assert_eq!((revs[1].author.as_str(), revs[1].date.as_str()), ("Ada", "2024-03-05T12:33:00Z"));
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,8 @@ pub(crate) fn from_prm0(isprm: u8) -> Option<u16> {
|
||||
0x09 => 0x2407, // sprmPFPageBreakBefore
|
||||
0x33 => 0x2435, // sprmPWidowControl (low byte of the operand is the value)
|
||||
0x78 => 0x2640, // sprmPOutLvl
|
||||
0x41 => 0x0800, // sprmCFRMarkDel
|
||||
0x42 => 0x0801, // sprmCFRMarkIns
|
||||
0x4D => 0x2A0C, // sprmCHighlight
|
||||
0x55 => 0x0835, // sprmCFBold
|
||||
0x56 => 0x0836, // sprmCFItalic
|
||||
|
||||
@@ -2822,7 +2822,12 @@ fn a_long_hyperlink_over_a_long_result_is_one_linked_run() {
|
||||
// what the copies charge to the work budget, which a busy machine can't skew.
|
||||
let mut result = super::FieldResult::default();
|
||||
(0..n).for_each(|_| result.push('y', craft_wordmodel::props::CharProps::default()));
|
||||
let f = super::FieldBuild { depth: 1, instr: format!("HYPERLINK \"http://{target}\""), sep: Some(result) };
|
||||
let f = super::FieldBuild {
|
||||
depth: 1,
|
||||
instr: format!("HYPERLINK \"http://{target}\""),
|
||||
sep: Some(result),
|
||||
anchor: craft_wordmodel::props::CharProps::default(),
|
||||
};
|
||||
let work = super::Work::new(u64::MAX);
|
||||
super::finish_field(&mut super::ParaBuild::default(), f, &work);
|
||||
assert_eq!(u64::MAX - work.left.get(), ("http://".len() + n) as u64, "one copy of the target");
|
||||
@@ -2836,7 +2841,12 @@ fn hyperlink_target_copies_are_charged_to_the_work_budget() {
|
||||
let bold = CharProps { bold: Some(true), ..Default::default() };
|
||||
let mut result = super::FieldResult::default();
|
||||
(0..1000).for_each(|i| result.push('a', if i % 2 == 0 { bold.clone() } else { CharProps::default() }));
|
||||
let f = super::FieldBuild { depth: 1, instr: format!("HYPERLINK \"{}\"", "u".repeat(100)), sep: Some(result) };
|
||||
let f = super::FieldBuild {
|
||||
depth: 1,
|
||||
instr: format!("HYPERLINK \"{}\"", "u".repeat(100)),
|
||||
sep: Some(result),
|
||||
anchor: craft_wordmodel::props::CharProps::default(),
|
||||
};
|
||||
let mut pb = super::ParaBuild::default();
|
||||
let budget = super::Work::new(10_000);
|
||||
super::finish_field(&mut pb, f, &budget);
|
||||
@@ -2901,15 +2911,46 @@ fn prm_grpprls_past_the_work_budget_are_skipped() {
|
||||
let work = super::Work::new(g.len() as u64 + 1);
|
||||
let base = CharProps::default();
|
||||
let mut first = CharProps::default();
|
||||
super::apply_prm(&mut first, crate::piece::PrmRef::Grpprl(Some(&g)), &[], &base, &work);
|
||||
super::apply_prm(&mut first, crate::piece::PrmRef::Grpprl { index: 0, grpprl: Some(&g) }, &[], &base, &work);
|
||||
assert_eq!(first.bold, Some(true));
|
||||
let mut second = CharProps::default();
|
||||
super::apply_prm(&mut second, crate::piece::PrmRef::Grpprl(Some(&g)), &[], &base, &work);
|
||||
super::apply_prm(&mut second, crate::piece::PrmRef::Grpprl { index: 0, grpprl: Some(&g) }, &[], &base, &work);
|
||||
assert_eq!(second.bold, None);
|
||||
assert_eq!(work.left.get(), 1);
|
||||
assert_eq!(work.dropped(), [super::Cost::Formatting]);
|
||||
}
|
||||
|
||||
/// A piece's Prm grpprl (a Prc of up to 64 KB) was scanned for revision marks again at every
|
||||
/// paragraph mark and object anchor, which the per-formatting-change memo (#3692) doesn't
|
||||
/// cover. Each Prc's marks are now found once.
|
||||
#[test]
|
||||
fn a_large_prc_is_scanned_for_revision_marks_once() {
|
||||
// 21,844 bold sprms, then sprmCFRMarkIns: the Prc's marks are only known at its end.
|
||||
let mut prls = vec![(0x0835u16, &[0x01u8][..]); 21_844];
|
||||
prls.push((0x0801, &[0x01]));
|
||||
let prc = grpprl(&prls);
|
||||
// A field and a paragraph mark per paragraph, all in the piece with that Prc.
|
||||
let text: &'static str = Box::leak("\u{13}\u{15}\r".repeat(100_000).into_boxed_str());
|
||||
let spec = RichSpec {
|
||||
text,
|
||||
styles: vec![(1, 0x0FFF, "Normal", Vec::new(), Vec::new())],
|
||||
fonts: vec!["Times New Roman"],
|
||||
chpx_runs: vec![(0, Vec::new())],
|
||||
papx_entries: vec![(0, 0, Vec::new())],
|
||||
};
|
||||
let bytes = rich_doc_with(&spec, &RichExtra { prcs: vec![prc], prm: 0x0001, ..Default::default() });
|
||||
let t = std::time::Instant::now();
|
||||
let doc = read(&bytes).expect("opens");
|
||||
// Well under a second; scanning the Prc at every mark and anchor took 17 s.
|
||||
assert!(t.elapsed().as_secs() < 10, "opened in {:?}", t.elapsed());
|
||||
assert_eq!(doc.body.len(), 100_000);
|
||||
assert_eq!(doc.revisions.len(), 1);
|
||||
for p in doc.body.iter().filter_map(|b| b.as_para()) {
|
||||
assert_eq!(p.mark.ins, Some(0), "the paragraph mark is a tracked insertion");
|
||||
assert_eq!(p.runs.first().and_then(|r| r.props.ins), Some(0), "so is the field");
|
||||
}
|
||||
}
|
||||
|
||||
/// A field's cached result keeps one `CharProps` per formatting run, not one per character
|
||||
/// (365 bytes each: 4 million result characters took 1.4 GB).
|
||||
#[test]
|
||||
@@ -2941,7 +2982,7 @@ fn field_results_keep_their_text_and_runs() {
|
||||
{
|
||||
result.push(c, p);
|
||||
}
|
||||
let f = super::FieldBuild { depth: 1, instr: "HYPERLINK \"http://x\"".into(), sep: Some(result) };
|
||||
let f = super::FieldBuild { depth: 1, instr: "HYPERLINK \"http://x\"".into(), sep: Some(result), anchor: CharProps::default() };
|
||||
let mut pb = super::ParaBuild::default();
|
||||
super::finish_field(&mut pb, f, &super::Work::new(u64::MAX));
|
||||
assert_eq!(pb.text, "a\u{e9}\u{20ac}bc");
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
//! Formatted disk pages ([MS-DOC] §2.9.43 ChpxFkp, §2.9.177 PapxFkp) and the bin tables that
|
||||
//! index them (`PlcBteChpx`, `PlcBtePapx`), plus the PLC and STTB containers the Table stream
|
||||
//! is made of.
|
||||
|
||||
/// Bytes in a formatted disk page.
|
||||
const PAGE: usize = 512;
|
||||
/// Most runs a `ChpxFkp` holds ([MS-DOC]: crun MUST be at most 0x65).
|
||||
const MAX_CHPX_RUNS: usize = 0x65;
|
||||
/// Most paragraphs a `PapxFkp` holds ([MS-DOC]: crun MUST be at most 0x1D).
|
||||
const MAX_PAPX_RUNS: usize = 0x1D;
|
||||
/// Size of a `BxPap`: a word offset and a 12-byte `PHE` (left zero: Word recomputes heights).
|
||||
const BX: usize = 13;
|
||||
|
||||
/// One run of text sharing its character formatting: `[fc_start, fc_end)` in the WordDocument
|
||||
/// stream, with the CHPX grpprl (empty = no direct formatting).
|
||||
pub(crate) struct ChpxRun<'a> {
|
||||
pub(crate) fc_start: u32,
|
||||
pub(crate) fc_end: u32,
|
||||
pub(crate) grpprl: &'a [u8],
|
||||
}
|
||||
|
||||
/// One paragraph: `[fc_start, fc_end)` (its mark included), its style and PAPX grpprl.
|
||||
pub(crate) struct PapxRun<'a> {
|
||||
pub(crate) fc_start: u32,
|
||||
pub(crate) fc_end: u32,
|
||||
pub(crate) istd: u16,
|
||||
pub(crate) grpprl: &'a [u8],
|
||||
}
|
||||
|
||||
/// Pages ready for the WordDocument stream and the bin table (`aFc` + `aPn`) that indexes them.
|
||||
pub(crate) struct Paged {
|
||||
pub(crate) pages: Vec<[u8; PAGE]>,
|
||||
/// `aFc`: the first FC of each page, then the last page's end.
|
||||
fcs: Vec<u32>,
|
||||
}
|
||||
|
||||
impl Paged {
|
||||
/// The `PlcBte*` for these pages once they sit from page number `first_pn` on.
|
||||
pub(crate) fn plc_bte(&self, first_pn: u32) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(self.fcs.len() * 4 + self.pages.len() * 4);
|
||||
for fc in &self.fcs {
|
||||
v.extend_from_slice(&fc.to_le_bytes());
|
||||
}
|
||||
for i in 0..self.pages.len() {
|
||||
v.extend_from_slice(&first_pn.saturating_add(i as u32).to_le_bytes());
|
||||
}
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
/// A page being filled: the `rgfc` entries so far and the property bytes, which grow down from
|
||||
/// the end of the page (the last byte is `crun`).
|
||||
struct Page {
|
||||
buf: [u8; PAGE],
|
||||
fcs: Vec<u32>,
|
||||
/// Word offset (÷2) of each entry's properties (0 = none).
|
||||
offs: Vec<u8>,
|
||||
/// Lowest byte used by property data.
|
||||
low: usize,
|
||||
/// Property bytes already on the page and where they are, so equal ones are stored once.
|
||||
stored: Vec<(Vec<u8>, u8)>,
|
||||
}
|
||||
|
||||
impl Page {
|
||||
fn new() -> Page {
|
||||
Page { buf: [0; PAGE], fcs: Vec::new(), offs: Vec::new(), low: PAGE - 1, stored: Vec::new() }
|
||||
}
|
||||
|
||||
/// Bytes the header takes with `crun` entries of `entry` bytes each.
|
||||
fn header(crun: usize, entry: usize) -> usize {
|
||||
(crun + 1) * 4 + crun * entry
|
||||
}
|
||||
|
||||
/// Place `data` (already in its on-page form), returning its word offset, if it fits beside
|
||||
/// a header for `crun` entries.
|
||||
fn place(&mut self, data: &[u8], crun: usize, entry: usize) -> Option<u8> {
|
||||
if data.is_empty() {
|
||||
return (Page::header(crun, entry) <= self.low).then_some(0);
|
||||
}
|
||||
if let Some((_, off)) = self.stored.iter().find(|(d, _)| d == data) {
|
||||
return (Page::header(crun, entry) <= self.low).then_some(*off);
|
||||
}
|
||||
let at = self.low.checked_sub(data.len())? & !1;
|
||||
if at < Page::header(crun, entry) || at / 2 > u8::MAX as usize {
|
||||
return None;
|
||||
}
|
||||
self.buf.get_mut(at..at + data.len())?.copy_from_slice(data);
|
||||
self.low = at;
|
||||
let off = (at / 2) as u8;
|
||||
self.stored.push((data.to_vec(), off));
|
||||
Some(off)
|
||||
}
|
||||
|
||||
/// Write the header and `crun`; `entry` is the size of each `rgb`/`rgbx` element.
|
||||
fn finish(mut self, end_fc: u32, entry: usize) -> ([u8; PAGE], u32) {
|
||||
let crun = self.offs.len();
|
||||
let first = self.fcs.first().copied().unwrap_or(end_fc);
|
||||
self.fcs.push(end_fc);
|
||||
for (i, fc) in self.fcs.iter().enumerate() {
|
||||
if let Some(b) = self.buf.get_mut(i * 4..i * 4 + 4) {
|
||||
b.copy_from_slice(&fc.to_le_bytes());
|
||||
}
|
||||
}
|
||||
let rgb_at = (crun + 1) * 4;
|
||||
for (i, off) in self.offs.iter().enumerate() {
|
||||
if let Some(b) = self.buf.get_mut(rgb_at + i * entry) {
|
||||
*b = *off;
|
||||
}
|
||||
}
|
||||
self.buf[PAGE - 1] = crun as u8;
|
||||
(self.buf, first)
|
||||
}
|
||||
}
|
||||
|
||||
/// Page the character runs into `ChpxFkp`s: each run's `Chpx` is `cb` then the grpprl (at most
|
||||
/// 255 bytes; the caller truncates).
|
||||
pub(crate) fn chpx_pages(runs: &[ChpxRun<'_>]) -> Paged {
|
||||
let mut out = Paged { pages: Vec::new(), fcs: Vec::new() };
|
||||
let mut page = Page::new();
|
||||
for r in runs {
|
||||
let mut data = Vec::new();
|
||||
if !r.grpprl.is_empty() {
|
||||
data.push(r.grpprl.len().min(255) as u8);
|
||||
data.extend_from_slice(r.grpprl.get(..255).unwrap_or(r.grpprl));
|
||||
}
|
||||
let off = if page.offs.len() < MAX_CHPX_RUNS { page.place(&data, page.offs.len() + 1, 1) } else { None };
|
||||
let off = match off {
|
||||
Some(o) => o,
|
||||
None => {
|
||||
let (buf, first) = std::mem::replace(&mut page, Page::new()).finish(r.fc_start, 1);
|
||||
out.pages.push(buf);
|
||||
out.fcs.push(first);
|
||||
// A fresh page always has room for one Chpx of at most 256 bytes.
|
||||
page.place(&data, 1, 1).unwrap_or(0)
|
||||
}
|
||||
};
|
||||
page.fcs.push(r.fc_start);
|
||||
page.offs.push(off);
|
||||
}
|
||||
let end = runs.last().map_or(0, |r| r.fc_end);
|
||||
if !page.offs.is_empty() {
|
||||
let (buf, first) = page.finish(end, 1);
|
||||
out.pages.push(buf);
|
||||
out.fcs.push(first);
|
||||
}
|
||||
out.fcs.push(end);
|
||||
out
|
||||
}
|
||||
|
||||
/// A `PapxInFkp` ([MS-DOC] §2.9.175): the istd and grpprl with the size byte(s) in front: `cb`
|
||||
/// (2×cb−1 bytes follow) for an odd size, else 0 and `cb'` (2×cb' bytes follow).
|
||||
fn papx_in_fkp(istd: u16, grpprl: &[u8]) -> Vec<u8> {
|
||||
let mut body = istd.to_le_bytes().to_vec();
|
||||
body.extend_from_slice(grpprl);
|
||||
let mut v = Vec::with_capacity(body.len() + 2);
|
||||
if body.len() % 2 == 1 {
|
||||
v.push(body.len().div_ceil(2) as u8);
|
||||
} else {
|
||||
v.push(0);
|
||||
v.push((body.len() / 2) as u8);
|
||||
}
|
||||
v.extend_from_slice(&body);
|
||||
v
|
||||
}
|
||||
|
||||
/// Largest PAPX grpprl a page takes: one paragraph alone on a page must still fit.
|
||||
pub(crate) const MAX_PAPX_GRPPRL: usize = 480;
|
||||
|
||||
/// Page the paragraphs into `PapxFkp`s.
|
||||
pub(crate) fn papx_pages(paras: &[PapxRun<'_>]) -> Paged {
|
||||
let mut out = Paged { pages: Vec::new(), fcs: Vec::new() };
|
||||
let mut page = Page::new();
|
||||
for p in paras {
|
||||
let data = papx_in_fkp(p.istd, p.grpprl.get(..MAX_PAPX_GRPPRL).unwrap_or(p.grpprl));
|
||||
// Even an unformatted paragraph names its style, so every BX points at a PAPX.
|
||||
let off = if page.offs.len() < MAX_PAPX_RUNS { page.place(&data, page.offs.len() + 1, BX) } else { None };
|
||||
let off = match off {
|
||||
Some(o) => o,
|
||||
None => {
|
||||
let (buf, first) = std::mem::replace(&mut page, Page::new()).finish(p.fc_start, BX);
|
||||
out.pages.push(buf);
|
||||
out.fcs.push(first);
|
||||
page.place(&data, 1, BX).unwrap_or(0)
|
||||
}
|
||||
};
|
||||
page.fcs.push(p.fc_start);
|
||||
page.offs.push(off);
|
||||
}
|
||||
let end = paras.last().map_or(0, |p| p.fc_end);
|
||||
if !page.offs.is_empty() {
|
||||
let (buf, first) = page.finish(end, BX);
|
||||
out.pages.push(buf);
|
||||
out.fcs.push(first);
|
||||
}
|
||||
out.fcs.push(end);
|
||||
out
|
||||
}
|
||||
|
||||
/// A PLC ([MS-DOC] §2.2.2): `cps` (one more than the data elements), then the elements.
|
||||
pub(crate) fn plc(cps: &[u32], data: &[&[u8]]) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(cps.len() * 4 + data.iter().map(|d| d.len()).sum::<usize>());
|
||||
for cp in cps {
|
||||
v.extend_from_slice(&cp.to_le_bytes());
|
||||
}
|
||||
for d in data {
|
||||
v.extend_from_slice(d);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// An extended STTB ([MS-DOC] §2.2.4): `fExtend`, `cData`, `cbExtra`, then each string as a
|
||||
/// count of UTF-16 units and the units, followed by its `extra` bytes.
|
||||
pub(crate) fn sttb_ext(strings: &[(String, Vec<u8>)], cb_extra: u16) -> Vec<u8> {
|
||||
let mut v = vec![0xFF, 0xFF];
|
||||
v.extend_from_slice(&(strings.len().min(u16::MAX as usize) as u16).to_le_bytes());
|
||||
v.extend_from_slice(&cb_extra.to_le_bytes());
|
||||
for (s, extra) in strings.iter().take(u16::MAX as usize) {
|
||||
let units: Vec<u16> = s.encode_utf16().take(u16::MAX as usize).collect();
|
||||
v.extend_from_slice(&(units.len() as u16).to_le_bytes());
|
||||
for u in units {
|
||||
v.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
let mut e = extra.clone();
|
||||
e.resize(cb_extra as usize, 0);
|
||||
v.extend_from_slice(&e);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::fkp::Bins;
|
||||
|
||||
#[test]
|
||||
fn chpx_pages_read_back() {
|
||||
let bold = [0x35u8, 0x08, 1];
|
||||
let runs: Vec<ChpxRun> =
|
||||
(0..300u32).map(|i| ChpxRun { fc_start: 0x400 + i * 2, fc_end: 0x402 + i * 2, grpprl: if i % 2 == 0 { &bold } else { &[] } }).collect();
|
||||
let paged = chpx_pages(&runs);
|
||||
assert!(paged.pages.len() >= 3, "300 runs need several pages");
|
||||
let mut word = vec![0u8; 0x1000];
|
||||
for p in &paged.pages {
|
||||
word.extend_from_slice(p);
|
||||
}
|
||||
let bte = paged.plc_bte(8);
|
||||
let bins = Bins::parse(&bte, 0, bte.len() as u32).unwrap();
|
||||
for i in 0..300u32 {
|
||||
let g = bins.chpx(&word, 0x400 + i * 2);
|
||||
assert_eq!(g, if i % 2 == 0 { &bold[..] } else { &[][..] }, "run {i}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn papx_pages_read_back_odd_and_even_sizes() {
|
||||
let odd = [0x03u8, 0x24, 1]; // 2 + 3 = 5 bytes with the istd
|
||||
let even = [0x03u8, 0x24, 1, 0]; // padded to 6 by the istd
|
||||
let paras: Vec<PapxRun> = (0..70u32)
|
||||
.map(|i| PapxRun {
|
||||
fc_start: 0x400 + i * 10,
|
||||
fc_end: 0x40A + i * 10,
|
||||
istd: i as u16 % 3,
|
||||
grpprl: if i % 2 == 0 { &odd } else { &even[..3] },
|
||||
})
|
||||
.collect();
|
||||
let paged = papx_pages(¶s);
|
||||
assert!(paged.pages.len() >= 3);
|
||||
let mut word = vec![0u8; 0x1000];
|
||||
for p in &paged.pages {
|
||||
word.extend_from_slice(p);
|
||||
}
|
||||
let bte = paged.plc_bte(8);
|
||||
let bins = Bins::parse(&bte, 0, bte.len() as u32).unwrap();
|
||||
for i in 0..70u32 {
|
||||
let (istd, g) = bins.papx(&word, 0x400 + i * 10 + 9);
|
||||
assert_eq!(istd, i as u16 % 3);
|
||||
assert_eq!(g, &odd[..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,753 @@
|
||||
//! Model properties → grpprls of sprms ([MS-DOC] §2.6): character (CHPX), paragraph (PAPX) and
|
||||
//! section (SEPX) formatting. The opcodes are the ones the reader decodes (`fmt.rs`,
|
||||
//! `sections.rs`, `table.rs`), so a saved file reads back with the same properties.
|
||||
|
||||
use craft_wordmodel::Rgb;
|
||||
use craft_wordmodel::props::{
|
||||
Align, Border, BorderStyle, Borders, CharProps, Highlight, LineSpacing, ParaProps, TabAlign, TabLeader, TextColor, Underline, VertAlign,
|
||||
};
|
||||
use craft_wordmodel::section::{NumFormat, PageVAlign, SectionProps, SectionStart};
|
||||
|
||||
/// A grpprl being built: Prls appended one by one.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub(crate) struct Grpprl(pub(crate) Vec<u8>);
|
||||
|
||||
impl Grpprl {
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
|
||||
fn op(&mut self, op: u16) {
|
||||
self.0.extend_from_slice(&op.to_le_bytes());
|
||||
}
|
||||
|
||||
pub(crate) fn u8(&mut self, op: u16, v: u8) {
|
||||
self.op(op);
|
||||
self.0.push(v);
|
||||
}
|
||||
|
||||
pub(crate) fn u16(&mut self, op: u16, v: u16) {
|
||||
self.op(op);
|
||||
self.0.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
|
||||
pub(crate) fn i16(&mut self, op: u16, v: i16) {
|
||||
self.u16(op, v as u16);
|
||||
}
|
||||
|
||||
pub(crate) fn u32(&mut self, op: u16, v: u32) {
|
||||
self.op(op);
|
||||
self.0.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
|
||||
/// A Prl whose operand is written as given (its size implied by the opcode's spra, or a
|
||||
/// leading size byte the caller includes).
|
||||
pub(crate) fn raw(&mut self, op: u16, operand: &[u8]) {
|
||||
self.op(op);
|
||||
self.0.extend_from_slice(operand);
|
||||
}
|
||||
|
||||
/// A variable-length Prl (spra 6): a size byte, then the operand.
|
||||
pub(crate) fn var(&mut self, op: u16, operand: &[u8]) {
|
||||
self.op(op);
|
||||
self.0.push(u8::try_from(operand.len()).unwrap_or(u8::MAX));
|
||||
self.0.extend_from_slice(operand.get(..255).unwrap_or(operand));
|
||||
}
|
||||
|
||||
pub(crate) fn extend(&mut self, other: &Grpprl) {
|
||||
self.0.extend_from_slice(&other.0);
|
||||
}
|
||||
|
||||
/// Keep whole Prls up to `max` bytes: a grpprl that doesn't fit where it is stored (a CHPX
|
||||
/// holds 255 bytes) loses its last properties rather than the file its shape.
|
||||
pub(crate) fn truncate_to(&mut self, max: usize) {
|
||||
if self.0.len() <= max {
|
||||
return;
|
||||
}
|
||||
let mut keep = 0usize;
|
||||
for prl in crate::sprm::iter(&self.0) {
|
||||
let next = keep + 2 + prl.operand.len();
|
||||
if next > max {
|
||||
break;
|
||||
}
|
||||
keep = next;
|
||||
}
|
||||
self.0.truncate(keep);
|
||||
}
|
||||
}
|
||||
|
||||
// Character sprms ([MS-DOC] §2.6.1).
|
||||
pub(crate) const C_RMARK_DEL: u16 = 0x0800;
|
||||
pub(crate) const C_RMARK_INS: u16 = 0x0801;
|
||||
pub(crate) const C_IBST_RMARK: u16 = 0x4804;
|
||||
pub(crate) const C_DTTM_RMARK: u16 = 0x6805;
|
||||
pub(crate) const C_IBST_RMARK_DEL: u16 = 0x4863;
|
||||
pub(crate) const C_DTTM_RMARK_DEL: u16 = 0x6864;
|
||||
pub(crate) const C_ISTD: u16 = 0x4A30;
|
||||
const C_BOLD: u16 = 0x0835;
|
||||
const C_ITALIC: u16 = 0x0836;
|
||||
const C_STRIKE: u16 = 0x0837;
|
||||
const C_OUTLINE: u16 = 0x0838;
|
||||
const C_SHADOW: u16 = 0x0839;
|
||||
const C_SMALL_CAPS: u16 = 0x083A;
|
||||
const C_CAPS: u16 = 0x083B;
|
||||
const C_VANISH: u16 = 0x083C;
|
||||
const C_IMPRINT: u16 = 0x0854;
|
||||
pub(crate) const C_SPEC: u16 = 0x0855;
|
||||
const C_EMBOSS: u16 = 0x0858;
|
||||
const C_RTL: u16 = 0x085A;
|
||||
const C_BOLD_BI: u16 = 0x085C;
|
||||
const C_ITALIC_BI: u16 = 0x085D;
|
||||
const C_NO_PROOF: u16 = 0x0875;
|
||||
const C_COMPLEX: u16 = 0x0882;
|
||||
const C_HIGHLIGHT: u16 = 0x2A0C;
|
||||
const C_KUL: u16 = 0x2A3E;
|
||||
const C_ICO: u16 = 0x2A42;
|
||||
const C_ISS: u16 = 0x2A48;
|
||||
const C_DSTRIKE: u16 = 0x2A53;
|
||||
const C_HPS: u16 = 0x4A43;
|
||||
const C_HPS_POS: u16 = 0x4845;
|
||||
const C_HPS_KERN: u16 = 0x484B;
|
||||
const C_CHAR_SCALE: u16 = 0x4852;
|
||||
const C_LID_BI: u16 = 0x485F;
|
||||
const C_LID0_80: u16 = 0x486D;
|
||||
const C_LID1_80: u16 = 0x486E;
|
||||
const C_LID0: u16 = 0x4873;
|
||||
const C_LID1: u16 = 0x4874;
|
||||
const C_FTC0: u16 = 0x4A4F;
|
||||
const C_FTC1: u16 = 0x4A50;
|
||||
const C_FTC2: u16 = 0x4A51;
|
||||
const C_FTC_BI: u16 = 0x4A5E;
|
||||
const C_HPS_BI: u16 = 0x4A61;
|
||||
pub(crate) const C_PIC_LOCATION: u16 = 0x6A03;
|
||||
const C_CV: u16 = 0x6870;
|
||||
const C_CV_UL: u16 = 0x6877;
|
||||
const C_DXA_SPACE: u16 = 0x8840;
|
||||
const C_SHD: u16 = 0xCA71;
|
||||
|
||||
/// What [`chpx`] needs besides the properties: the font table, the style table, and the
|
||||
/// tracked-change authors.
|
||||
pub(crate) trait CharCtx {
|
||||
/// Index of `name` in the font table (added on first use).
|
||||
fn font(&mut self, name: &str) -> u16;
|
||||
/// The istd of a character style id; `None` when the stylesheet doesn't have it.
|
||||
fn char_style(&self, id: &str) -> Option<u16>;
|
||||
/// The author index and `DTTM` of revision `rev` (`Document::revisions`).
|
||||
fn revision(&mut self, rev: u32) -> Option<(u16, u32)>;
|
||||
}
|
||||
|
||||
/// The CHPX grpprl for `p`: only the properties it sets, in the order Word applies them (the
|
||||
/// style first, then toggles, then values). Inherited (`None`) properties are left out.
|
||||
pub(crate) fn chpx(p: &CharProps, ctx: &mut dyn CharCtx) -> Grpprl {
|
||||
let mut g = Grpprl::default();
|
||||
if let Some(istd) = p.style.as_deref().and_then(|s| ctx.char_style(s)) {
|
||||
g.u16(C_ISTD, istd);
|
||||
}
|
||||
// Tracked changes: the mark, the author and the time ([MS-DOC] sprmCFRMarkIns and friends).
|
||||
if let Some((ibst, dttm)) = p.ins.and_then(|r| ctx.revision(r)) {
|
||||
g.u8(C_RMARK_INS, 1);
|
||||
g.u16(C_IBST_RMARK, ibst);
|
||||
g.u32(C_DTTM_RMARK, dttm);
|
||||
}
|
||||
if let Some((ibst, dttm)) = p.del.and_then(|r| ctx.revision(r)) {
|
||||
g.u8(C_RMARK_DEL, 1);
|
||||
g.u16(C_IBST_RMARK_DEL, ibst);
|
||||
g.u32(C_DTTM_RMARK_DEL, dttm);
|
||||
}
|
||||
for (op, v) in [
|
||||
(C_BOLD, p.bold),
|
||||
(C_ITALIC, p.italic),
|
||||
(C_STRIKE, p.strike),
|
||||
(C_OUTLINE, p.outline),
|
||||
(C_SHADOW, p.shadow),
|
||||
(C_SMALL_CAPS, p.small_caps),
|
||||
(C_CAPS, p.caps),
|
||||
(C_VANISH, p.hidden),
|
||||
(C_IMPRINT, p.engrave),
|
||||
(C_EMBOSS, p.emboss),
|
||||
(C_RTL, p.rtl),
|
||||
(C_BOLD_BI, p.bold_cs),
|
||||
(C_ITALIC_BI, p.italic_cs),
|
||||
(C_NO_PROOF, p.no_proof),
|
||||
(C_COMPLEX, p.cs),
|
||||
] {
|
||||
if let Some(on) = v {
|
||||
g.u8(op, u8::from(on));
|
||||
}
|
||||
}
|
||||
if let Some(d) = p.double_strike {
|
||||
g.u8(C_DSTRIKE, u8::from(d));
|
||||
}
|
||||
if let Some(u) = p.underline {
|
||||
g.u8(C_KUL, kul(u));
|
||||
}
|
||||
if let Some(c) = p.underline_color {
|
||||
g.u32(C_CV_UL, colorref(c));
|
||||
}
|
||||
match p.color {
|
||||
Some(TextColor::Auto) => g.u8(C_ICO, 0),
|
||||
Some(TextColor::Rgb(c)) => {
|
||||
// The palette colour for Word 97, then the exact colour for every later version.
|
||||
g.u8(C_ICO, nearest_ico(c));
|
||||
g.u32(C_CV, colorref(c));
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
if let Some(h) = p.highlight {
|
||||
g.u8(C_HIGHLIGHT, highlight_ico(h));
|
||||
}
|
||||
if let Some(s) = p.size {
|
||||
g.u16(C_HPS, half_points(s));
|
||||
}
|
||||
if let Some(s) = p.size_cs {
|
||||
g.u16(C_HPS_BI, half_points(s));
|
||||
}
|
||||
if let Some(v) = p.vert_align {
|
||||
g.u8(
|
||||
C_ISS,
|
||||
match v {
|
||||
VertAlign::Baseline => 0,
|
||||
VertAlign::Superscript => 1,
|
||||
VertAlign::Subscript => 2,
|
||||
},
|
||||
);
|
||||
}
|
||||
if let Some(pos) = p.position {
|
||||
g.i16(C_HPS_POS, (pos * 2.0).round().clamp(-3168.0, 3168.0) as i16);
|
||||
}
|
||||
if let Some(sp) = p.spacing {
|
||||
g.i16(C_DXA_SPACE, twips(sp));
|
||||
}
|
||||
if let Some(sc) = p.scale {
|
||||
g.u16(C_CHAR_SCALE, sc.round().clamp(1.0, 600.0) as u16);
|
||||
}
|
||||
if let Some(k) = p.kern {
|
||||
g.u16(C_HPS_KERN, half_points(k.max(0.0)));
|
||||
}
|
||||
if let Some(f) = p.font.as_deref() {
|
||||
let i = ctx.font(f);
|
||||
g.u16(C_FTC0, i);
|
||||
// Text outside ASCII but not East Asian takes the third font; Word sets both together.
|
||||
g.u16(C_FTC2, i);
|
||||
}
|
||||
if let Some(f) = p.font_ea.as_deref() {
|
||||
let i = ctx.font(f);
|
||||
g.u16(C_FTC1, i);
|
||||
}
|
||||
if let Some(f) = p.font_cs.as_deref() {
|
||||
let i = ctx.font(f);
|
||||
g.u16(C_FTC_BI, i);
|
||||
}
|
||||
if let Some(lid) = p.lang.as_deref().and_then(lcid) {
|
||||
g.u16(C_LID0_80, lid);
|
||||
g.u16(C_LID0, lid);
|
||||
}
|
||||
if let Some(lid) = p.lang_ea.as_deref().and_then(lcid) {
|
||||
g.u16(C_LID1_80, lid);
|
||||
g.u16(C_LID1, lid);
|
||||
}
|
||||
if let Some(lid) = p.lang_bidi.as_deref().and_then(lcid) {
|
||||
g.u16(C_LID_BI, lid);
|
||||
}
|
||||
if let Some(c) = p.shading {
|
||||
g.var(C_SHD, &shd(Some(c)));
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
/// Points → half-points, as the size sprms store them.
|
||||
fn half_points(pt: f32) -> u16 {
|
||||
(pt * 2.0).round().clamp(2.0, 3276.0) as u16
|
||||
}
|
||||
|
||||
/// Points → twips, clamped to what an `XAS`/`YAS` holds.
|
||||
pub(crate) fn twips(pt: f32) -> i16 {
|
||||
if !pt.is_finite() {
|
||||
return 0;
|
||||
}
|
||||
(pt * 20.0).round().clamp(-31_680.0, 31_680.0) as i16
|
||||
}
|
||||
|
||||
/// A `COLORREF` (0x00BBGGRR, stored R, G, B, 0).
|
||||
pub(crate) fn colorref(c: Rgb) -> u32 {
|
||||
u32::from_le_bytes([c.0, c.1, c.2, 0])
|
||||
}
|
||||
|
||||
/// The automatic colour (`fAuto` in the high byte).
|
||||
pub(crate) const CV_AUTO: u32 = 0xFF00_0000;
|
||||
|
||||
/// A 10-byte `Shd` ([MS-DOC] §2.9.248): automatic foreground, `fill` as background, clear
|
||||
/// pattern; no fill is an all-automatic one.
|
||||
pub(crate) fn shd(fill: Option<Rgb>) -> [u8; 10] {
|
||||
let mut b = [0u8; 10];
|
||||
b[0..4].copy_from_slice(&CV_AUTO.to_le_bytes());
|
||||
let back = fill.map_or(CV_AUTO, colorref);
|
||||
b[4..8].copy_from_slice(&back.to_le_bytes());
|
||||
b
|
||||
}
|
||||
|
||||
/// Word's 16-colour palette ([MS-DOC] §2.9.135 Ico), index 1..=16.
|
||||
const ICO: [Rgb; 16] = [
|
||||
Rgb(0, 0, 0),
|
||||
Rgb(0, 0, 255),
|
||||
Rgb(0, 255, 255),
|
||||
Rgb(0, 255, 0),
|
||||
Rgb(255, 0, 255),
|
||||
Rgb(255, 0, 0),
|
||||
Rgb(255, 255, 0),
|
||||
Rgb(255, 255, 255),
|
||||
Rgb(0, 0, 128),
|
||||
Rgb(0, 128, 128),
|
||||
Rgb(0, 128, 0),
|
||||
Rgb(128, 0, 128),
|
||||
Rgb(128, 0, 0),
|
||||
Rgb(128, 128, 0),
|
||||
Rgb(128, 128, 128),
|
||||
Rgb(192, 192, 192),
|
||||
];
|
||||
|
||||
/// The palette entry nearest `c` (by squared distance), 1-based.
|
||||
pub(crate) fn nearest_ico(c: Rgb) -> u8 {
|
||||
let d = |p: &Rgb| {
|
||||
let dr = i32::from(p.0) - i32::from(c.0);
|
||||
let dg = i32::from(p.1) - i32::from(c.1);
|
||||
let db = i32::from(p.2) - i32::from(c.2);
|
||||
dr * dr + dg * dg + db * db
|
||||
};
|
||||
ICO.iter().enumerate().min_by_key(|(_, p)| d(p)).map_or(1, |(i, _)| i as u8 + 1)
|
||||
}
|
||||
|
||||
/// A highlight colour as the `Ico` `sprmCHighlight` takes.
|
||||
fn highlight_ico(h: Highlight) -> u8 {
|
||||
match h {
|
||||
Highlight::None => 0,
|
||||
Highlight::Black => 1,
|
||||
Highlight::Blue => 2,
|
||||
Highlight::Turquoise => 3,
|
||||
Highlight::BrightGreen => 4,
|
||||
Highlight::Pink => 5,
|
||||
Highlight::Red => 6,
|
||||
Highlight::Yellow => 7,
|
||||
Highlight::White => 8,
|
||||
Highlight::DarkBlue => 9,
|
||||
Highlight::Teal => 10,
|
||||
Highlight::Green => 11,
|
||||
Highlight::Violet => 12,
|
||||
Highlight::DarkRed => 13,
|
||||
Highlight::DarkYellow => 14,
|
||||
Highlight::Gray50 => 15,
|
||||
Highlight::Gray25 => 16,
|
||||
}
|
||||
}
|
||||
|
||||
/// An underline as a `Kul` ([MS-DOC] §2.9.133).
|
||||
fn kul(u: Underline) -> u8 {
|
||||
match u {
|
||||
Underline::None => 0x00,
|
||||
Underline::Single => 0x01,
|
||||
Underline::Words => 0x02,
|
||||
Underline::Double => 0x03,
|
||||
Underline::Dotted => 0x04,
|
||||
Underline::Thick => 0x06,
|
||||
Underline::Dash => 0x07,
|
||||
Underline::DotDash => 0x09,
|
||||
Underline::DotDotDash => 0x0A,
|
||||
Underline::Wave => 0x0B,
|
||||
Underline::DoubleWave => 0x2B,
|
||||
}
|
||||
}
|
||||
|
||||
/// The Windows language id (LCID) of a BCP 47 tag, for the common languages; `None` for a tag
|
||||
/// not in the table (the text keeps the style's language).
|
||||
pub(crate) fn lcid(tag: &str) -> Option<u16> {
|
||||
let t = tag.trim().to_ascii_lowercase().replace('_', "-");
|
||||
Some(match t.as_str() {
|
||||
"ar-sa" | "ar" => 0x0401,
|
||||
"bg-bg" | "bg" => 0x0402,
|
||||
"ca-es" | "ca" => 0x0403,
|
||||
"zh-tw" => 0x0404,
|
||||
"cs-cz" | "cs" => 0x0405,
|
||||
"da-dk" | "da" => 0x0406,
|
||||
"de-de" | "de" => 0x0407,
|
||||
"el-gr" | "el" => 0x0408,
|
||||
"en-us" | "en" => 0x0409,
|
||||
"es-es" | "es" => 0x0C0A,
|
||||
"fi-fi" | "fi" => 0x040B,
|
||||
"fr-fr" | "fr" => 0x040C,
|
||||
"he-il" | "he" => 0x040D,
|
||||
"hu-hu" | "hu" => 0x040E,
|
||||
"is-is" | "is" => 0x040F,
|
||||
"it-it" | "it" => 0x0410,
|
||||
"ja-jp" | "ja" => 0x0411,
|
||||
"ko-kr" | "ko" => 0x0412,
|
||||
"nl-nl" | "nl" => 0x0413,
|
||||
"nb-no" | "nb" | "no" => 0x0414,
|
||||
"pl-pl" | "pl" => 0x0415,
|
||||
"pt-br" => 0x0416,
|
||||
"ro-ro" | "ro" => 0x0418,
|
||||
"ru-ru" | "ru" => 0x0419,
|
||||
"hr-hr" | "hr" => 0x041A,
|
||||
"sk-sk" | "sk" => 0x041B,
|
||||
"sv-se" | "sv" => 0x041D,
|
||||
"th-th" | "th" => 0x041E,
|
||||
"tr-tr" | "tr" => 0x041F,
|
||||
"id-id" | "id" => 0x0421,
|
||||
"uk-ua" | "uk" => 0x0422,
|
||||
"sl-si" | "sl" => 0x0424,
|
||||
"et-ee" | "et" => 0x0425,
|
||||
"lv-lv" | "lv" => 0x0426,
|
||||
"lt-lt" | "lt" => 0x0427,
|
||||
"fa-ir" | "fa" => 0x0429,
|
||||
"vi-vn" | "vi" => 0x042A,
|
||||
"hi-in" | "hi" => 0x0439,
|
||||
"ne-np" | "ne" => 0x0461,
|
||||
"zh-cn" | "zh" => 0x0804,
|
||||
"de-ch" => 0x0807,
|
||||
"en-gb" => 0x0809,
|
||||
"es-mx" => 0x080A,
|
||||
"pt-pt" | "pt" => 0x0816,
|
||||
"sr-latn-rs" => 0x241A,
|
||||
"sr-cyrl-rs" | "sr" => 0x281A,
|
||||
"en-au" => 0x0C09,
|
||||
"en-ca" => 0x1009,
|
||||
"fr-ca" => 0x0C0C,
|
||||
"es-uy" => 0x380A,
|
||||
"es-419" => 0x580A,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
// Paragraph sprms ([MS-DOC] §2.6.2).
|
||||
const P_JC80: u16 = 0x2403;
|
||||
const P_JC: u16 = 0x2461;
|
||||
const P_KEEP: u16 = 0x2405;
|
||||
const P_KEEP_FOLLOW: u16 = 0x2406;
|
||||
const P_PAGE_BREAK_BEFORE: u16 = 0x2407;
|
||||
const P_NO_LINE_NUMB: u16 = 0x240C;
|
||||
const P_IN_TABLE: u16 = 0x2416;
|
||||
const P_TTP: u16 = 0x2417;
|
||||
const P_NO_AUTO_HYPH: u16 = 0x242A;
|
||||
const P_WIDOW: u16 = 0x2431;
|
||||
const P_BIDI: u16 = 0x2441;
|
||||
const P_OUTLVL: u16 = 0x2640;
|
||||
const P_CONTEXTUAL: u16 = 0x246D;
|
||||
const P_ILVL: u16 = 0x260A;
|
||||
const P_ILFO: u16 = 0x460B;
|
||||
const P_DXC_RIGHT: u16 = 0x4455;
|
||||
const P_DXC_LEFT: u16 = 0x4456;
|
||||
const P_DXC_FIRST: u16 = 0x4457;
|
||||
const P_DYA_LINE: u16 = 0x6412;
|
||||
const P_ITAP: u16 = 0x6649;
|
||||
const P_DXA_RIGHT80: u16 = 0x840E;
|
||||
const P_DXA_LEFT80: u16 = 0x840F;
|
||||
const P_DXA_FIRST80: u16 = 0x8411;
|
||||
const P_DXA_RIGHT: u16 = 0x845D;
|
||||
const P_DXA_LEFT: u16 = 0x845E;
|
||||
const P_DXA_FIRST: u16 = 0x8460;
|
||||
const P_DYA_BEFORE: u16 = 0xA413;
|
||||
const P_DYA_AFTER: u16 = 0xA414;
|
||||
const P_CHG_TABS_PAPX: u16 = 0xC60D;
|
||||
const P_SHD: u16 = 0xC64D;
|
||||
const P_BRC: [u16; 5] = [0xC64E, 0xC64F, 0xC650, 0xC651, 0xC652];
|
||||
const P_BRC80: [u16; 5] = [0x6424, 0x6425, 0x6426, 0x6427, 0x6428];
|
||||
|
||||
/// What [`papx`] needs besides the properties: the list (LFO) a model num id became.
|
||||
pub(crate) trait ParaCtx {
|
||||
/// The 1-based LFO index of model num `id` (0 = no list).
|
||||
fn lfo(&self, num: u32) -> u16;
|
||||
}
|
||||
|
||||
/// The PAPX grpprl for `p` (its style is the PAPX's istd, not a sprm).
|
||||
pub(crate) fn papx(p: &ParaProps, ctx: &dyn ParaCtx) -> Grpprl {
|
||||
let mut g = Grpprl::default();
|
||||
if let Some(a) = p.align {
|
||||
let jc = match a {
|
||||
Align::Left => 0,
|
||||
Align::Center => 1,
|
||||
Align::Right => 2,
|
||||
Align::Justify => 3,
|
||||
Align::Distribute => 4,
|
||||
};
|
||||
g.u8(P_JC80, jc);
|
||||
g.u8(P_JC, jc);
|
||||
}
|
||||
for (v, op80, op) in
|
||||
[(p.indent_left, P_DXA_LEFT80, P_DXA_LEFT), (p.indent_right, P_DXA_RIGHT80, P_DXA_RIGHT), (p.indent_first, P_DXA_FIRST80, P_DXA_FIRST)]
|
||||
{
|
||||
if let Some(v) = v {
|
||||
g.i16(op80, twips(v));
|
||||
g.i16(op, twips(v));
|
||||
}
|
||||
}
|
||||
for (v, op) in [(p.indent_left_chars, P_DXC_LEFT), (p.indent_right_chars, P_DXC_RIGHT), (p.indent_first_chars, P_DXC_FIRST)] {
|
||||
if let Some(v) = v.filter(|v| v.is_finite()) {
|
||||
g.i16(op, (v * 100.0).round().clamp(-31_680.0, 31_680.0) as i16);
|
||||
}
|
||||
}
|
||||
if let Some(v) = p.space_before {
|
||||
g.u16(P_DYA_BEFORE, twips(v.max(0.0)) as u16);
|
||||
}
|
||||
if let Some(v) = p.space_after {
|
||||
g.u16(P_DYA_AFTER, twips(v.max(0.0)) as u16);
|
||||
}
|
||||
if let Some(ls) = p.line_spacing {
|
||||
// LSPD: dyaLine, then fMultLinespace.
|
||||
let (line, mult): (i16, i16) = match ls {
|
||||
LineSpacing::Multiple(m) => ((m * 240.0).round().clamp(1.0, 31_680.0) as i16, 1),
|
||||
LineSpacing::AtLeast(pt) => (twips(pt.max(0.0)), 0),
|
||||
LineSpacing::Exactly(pt) => (twips(pt.max(0.05)).saturating_neg(), 0),
|
||||
};
|
||||
let mut op = line.to_le_bytes().to_vec();
|
||||
op.extend_from_slice(&mult.to_le_bytes());
|
||||
g.raw(P_DYA_LINE, &op);
|
||||
}
|
||||
for (op, v) in [
|
||||
(P_KEEP, p.keep_lines),
|
||||
(P_KEEP_FOLLOW, p.keep_next),
|
||||
(P_PAGE_BREAK_BEFORE, p.page_break_before),
|
||||
(P_WIDOW, p.widow_control),
|
||||
(P_CONTEXTUAL, p.contextual_spacing),
|
||||
(P_NO_AUTO_HYPH, p.suppress_hyphens),
|
||||
(P_NO_LINE_NUMB, p.suppress_line_numbers),
|
||||
(P_BIDI, p.bidi),
|
||||
] {
|
||||
if let Some(on) = v {
|
||||
g.u8(op, u8::from(on));
|
||||
}
|
||||
}
|
||||
if let Some(l) = p.outline_level {
|
||||
g.u8(P_OUTLVL, l.min(9));
|
||||
}
|
||||
if let Some(n) = p.numbering {
|
||||
g.u8(P_ILVL, n.level.min(8));
|
||||
g.u16(P_ILFO, if n.num == 0 { 0 } else { ctx.lfo(n.num) });
|
||||
}
|
||||
if let Some(c) = p.shading {
|
||||
g.var(P_SHD, &shd(Some(c)));
|
||||
}
|
||||
if let Some(b) = &p.borders {
|
||||
para_borders(&mut g, b);
|
||||
}
|
||||
if let Some(tabs) = &p.tabs {
|
||||
tab_stops(&mut g, tabs);
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
/// The paragraph's borders: the Word 97 form (palette colour) and the exact one, side by side.
|
||||
fn para_borders(g: &mut Grpprl, b: &Borders) {
|
||||
for (i, side) in [b.top, b.left, b.bottom, b.right, b.between].iter().enumerate() {
|
||||
let Some(side) = side else { continue };
|
||||
if let (Some(&op80), Some(&op)) = (P_BRC80.get(i), P_BRC.get(i)) {
|
||||
g.raw(op80, &brc80(side));
|
||||
g.var(op, &brc(side));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A border's `brcType` ([MS-DOC] §2.9.22).
|
||||
fn brc_type(s: BorderStyle) -> u8 {
|
||||
match s {
|
||||
BorderStyle::None => 0x00,
|
||||
BorderStyle::Single => 0x01,
|
||||
BorderStyle::Thick => 0x02,
|
||||
BorderStyle::Double => 0x03,
|
||||
BorderStyle::Dotted => 0x06,
|
||||
BorderStyle::Dashed => 0x07,
|
||||
BorderStyle::DotDash => 0x08,
|
||||
BorderStyle::Triple => 0x0A,
|
||||
BorderStyle::Wave => 0x14,
|
||||
BorderStyle::ThinThickSmallGap => 0x0B,
|
||||
BorderStyle::ThickThinSmallGap => 0x0C,
|
||||
BorderStyle::ThinThickThinSmallGap => 0x0D,
|
||||
BorderStyle::ThinThickMediumGap => 0x0E,
|
||||
BorderStyle::ThickThinMediumGap => 0x0F,
|
||||
BorderStyle::ThinThickThinMediumGap => 0x10,
|
||||
BorderStyle::ThinThickLargeGap => 0x11,
|
||||
BorderStyle::ThickThinLargeGap => 0x12,
|
||||
BorderStyle::ThinThickThinLargeGap => 0x13,
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Brc80` ([MS-DOC] §2.9.18): width in eighths of a point, type, palette colour, space.
|
||||
pub(crate) fn brc80(b: &Border) -> [u8; 4] {
|
||||
if b.style == BorderStyle::None {
|
||||
return [0; 4];
|
||||
}
|
||||
let dpt = (b.width * 8.0).round().clamp(2.0, 255.0) as u8;
|
||||
let ico = b.color.map_or(0, nearest_ico);
|
||||
let space = b.space.round().clamp(0.0, 31.0) as u8;
|
||||
[dpt, brc_type(b.style), ico, space]
|
||||
}
|
||||
|
||||
/// A `Brc` ([MS-DOC] §2.9.16): `COLORREF`, width in eighths of a point, type, space and flags.
|
||||
pub(crate) fn brc(b: &Border) -> [u8; 8] {
|
||||
if b.style == BorderStyle::None {
|
||||
return [0; 8];
|
||||
}
|
||||
let cv = b.color.map_or(0, colorref);
|
||||
let [c0, c1, c2, c3] = cv.to_le_bytes();
|
||||
let dpt = (b.width * 8.0).round().clamp(2.0, 255.0) as u8;
|
||||
let space = b.space.round().clamp(0.0, 31.0) as u8;
|
||||
[c0, c1, c2, c3, dpt, brc_type(b.style), space, 0]
|
||||
}
|
||||
|
||||
/// The paragraph's own tab stops as a `PChgTabsPapxOperand`: the cleared stops in `rgdxaDel`,
|
||||
/// the others (with their alignment and leader) in `rgdxaAdd`, each list ascending.
|
||||
fn tab_stops(g: &mut Grpprl, tabs: &[craft_wordmodel::props::TabStop]) {
|
||||
let mut del: Vec<i16> = tabs.iter().filter(|t| t.align == TabAlign::Clear).map(|t| twips(t.pos)).collect();
|
||||
let mut add: Vec<(i16, u8)> = tabs
|
||||
.iter()
|
||||
.filter(|t| t.align != TabAlign::Clear)
|
||||
.map(|t| {
|
||||
let jc = match t.align {
|
||||
TabAlign::Center => 1,
|
||||
TabAlign::Right => 2,
|
||||
TabAlign::Decimal => 3,
|
||||
TabAlign::Bar => 4,
|
||||
_ => 0,
|
||||
};
|
||||
let tlc = match t.leader {
|
||||
TabLeader::None => 0,
|
||||
TabLeader::Dot => 1,
|
||||
TabLeader::Hyphen => 2,
|
||||
TabLeader::Underscore => 3,
|
||||
TabLeader::MiddleDot => 5,
|
||||
};
|
||||
(twips(t.pos), jc | (tlc << 3))
|
||||
})
|
||||
.collect();
|
||||
del.sort_unstable();
|
||||
del.dedup();
|
||||
add.sort_by_key(|t| t.0);
|
||||
add.dedup_by_key(|t| t.0);
|
||||
// The operand's size byte holds 255: 64 stops each way is Word's own limit and fits.
|
||||
del.truncate(64);
|
||||
add.truncate(64);
|
||||
if del.is_empty() && add.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut op = vec![del.len() as u8];
|
||||
for d in &del {
|
||||
op.extend_from_slice(&d.to_le_bytes());
|
||||
}
|
||||
op.push(add.len() as u8);
|
||||
for (x, _) in &add {
|
||||
op.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
op.extend(add.iter().map(|(_, tbd)| *tbd));
|
||||
g.var(P_CHG_TABS_PAPX, &op);
|
||||
}
|
||||
|
||||
/// The table membership of a paragraph inside a table cell (`sprmPFInTable`, `sprmPItap`); the
|
||||
/// row-end mark adds `sprmPFTtp`.
|
||||
pub(crate) fn in_table(g: &mut Grpprl, row_end: bool) {
|
||||
g.u8(P_IN_TABLE, 1);
|
||||
if row_end {
|
||||
g.u8(P_TTP, 1);
|
||||
}
|
||||
g.u32(P_ITAP, 1);
|
||||
}
|
||||
|
||||
// Section sprms ([MS-DOC] §2.6.4).
|
||||
const S_BKC: u16 = 0x3009;
|
||||
const S_TITLE_PAGE: u16 = 0x300A;
|
||||
const S_CCOLUMNS: u16 = 0x500B;
|
||||
const S_DXA_COLUMNS: u16 = 0x900C;
|
||||
const S_NFC_PGN: u16 = 0x300E;
|
||||
const S_PGN_RESTART: u16 = 0x3011;
|
||||
const S_LNC: u16 = 0x3013;
|
||||
const S_NLNN_MOD: u16 = 0x5015;
|
||||
const S_DXA_LNN: u16 = 0x9016;
|
||||
const S_HDR_TOP: u16 = 0xB017;
|
||||
const S_HDR_BOTTOM: u16 = 0xB018;
|
||||
const S_VJC: u16 = 0x301A;
|
||||
const S_LNN_MIN: u16 = 0x501B;
|
||||
const S_PGN_START: u16 = 0x501C;
|
||||
const S_ORIENTATION: u16 = 0x301D;
|
||||
const S_XA_PAGE: u16 = 0xB01F;
|
||||
const S_YA_PAGE: u16 = 0xB020;
|
||||
const S_DXA_LEFT: u16 = 0xB021;
|
||||
const S_DXA_RIGHT: u16 = 0xB022;
|
||||
const S_DYA_TOP: u16 = 0x9023;
|
||||
const S_DYA_BOTTOM: u16 = 0x9024;
|
||||
const S_DZA_GUTTER: u16 = 0xB025;
|
||||
const S_EVENLY_SPACED: u16 = 0x3005;
|
||||
const S_BIDI: u16 = 0x3228;
|
||||
|
||||
/// The SEPX grpprl of a section: every page property, so the section reads the same whatever
|
||||
/// the reader's defaults.
|
||||
pub(crate) fn sepx(s: &SectionProps) -> Grpprl {
|
||||
let mut g = Grpprl::default();
|
||||
g.u8(
|
||||
S_BKC,
|
||||
match s.start {
|
||||
SectionStart::Continuous => 0,
|
||||
SectionStart::NextColumn => 1,
|
||||
SectionStart::NextPage => 2,
|
||||
SectionStart::EvenPage => 3,
|
||||
SectionStart::OddPage => 4,
|
||||
},
|
||||
);
|
||||
if s.title_page {
|
||||
g.u8(S_TITLE_PAGE, 1);
|
||||
}
|
||||
let cols = s.columns.count.clamp(1, 44);
|
||||
if cols > 1 {
|
||||
g.u16(S_CCOLUMNS, (cols - 1) as u16);
|
||||
g.u16(S_DXA_COLUMNS, twips(s.columns.space.max(0.0)) as u16);
|
||||
g.u8(S_EVENLY_SPACED, 1);
|
||||
}
|
||||
if let Some(start) = s.page_num_start {
|
||||
g.u8(S_PGN_RESTART, 1);
|
||||
g.u16(S_PGN_START, start.min(0x7FFF) as u16);
|
||||
}
|
||||
if s.page_num_format != NumFormat::Decimal {
|
||||
g.u8(S_NFC_PGN, nfc(s.page_num_format));
|
||||
}
|
||||
if let Some(ln) = &s.line_numbers {
|
||||
g.u8(
|
||||
S_LNC,
|
||||
match ln.restart {
|
||||
craft_wordmodel::section::LineNumberRestart::Page => 0,
|
||||
craft_wordmodel::section::LineNumberRestart::Section => 1,
|
||||
craft_wordmodel::section::LineNumberRestart::Continuous => 2,
|
||||
},
|
||||
);
|
||||
g.u16(S_NLNN_MOD, ln.count_by.clamp(1, 100) as u16);
|
||||
g.i16(S_DXA_LNN, twips(ln.distance.max(0.0)));
|
||||
g.u16(S_LNN_MIN, ln.start.saturating_sub(1).min(0x7FFF) as u16);
|
||||
}
|
||||
g.u8(S_ORIENTATION, if s.landscape { 2 } else { 1 });
|
||||
g.u16(S_XA_PAGE, twips(s.page_w.max(7.2)) as u16);
|
||||
g.u16(S_YA_PAGE, twips(s.page_h.max(7.2)) as u16);
|
||||
g.u16(S_DXA_LEFT, twips(s.margin_left.max(0.0)) as u16);
|
||||
g.u16(S_DXA_RIGHT, twips(s.margin_right.max(0.0)) as u16);
|
||||
g.i16(S_DYA_TOP, twips(s.margin_top));
|
||||
g.i16(S_DYA_BOTTOM, twips(s.margin_bottom));
|
||||
g.u16(S_DZA_GUTTER, twips(s.gutter.max(0.0)) as u16);
|
||||
g.u16(S_HDR_TOP, twips(s.header.max(0.0)) as u16);
|
||||
g.u16(S_HDR_BOTTOM, twips(s.footer.max(0.0)) as u16);
|
||||
let vjc = match s.valign {
|
||||
PageVAlign::Top => 0,
|
||||
PageVAlign::Center => 1,
|
||||
PageVAlign::Justify => 2,
|
||||
PageVAlign::Bottom => 3,
|
||||
};
|
||||
if vjc != 0 {
|
||||
g.u8(S_VJC, vjc);
|
||||
}
|
||||
if s.rtl {
|
||||
g.u8(S_BIDI, 1);
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
/// A number format as an `MSONFC` ([MS-OSHARED] §2.2.1.3): its place in the ST_NumberFormat
|
||||
/// order up to 0x27, decimal past it (the formats Word 97 never had), 0xFF for none.
|
||||
pub(crate) fn nfc(f: NumFormat) -> u8 {
|
||||
match f {
|
||||
NumFormat::None => 0xFF,
|
||||
f => NumFormat::ALL.iter().position(|x| *x == f).filter(|&i| i <= 0x27).map_or(0, |i| i as u8),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
//! Lists: `PlfLst` (one LSTF per abstract list, its nine LVLs after the array) and `PlfLfo`
|
||||
//! (one LFO per model num, with LFOLVL start and level overrides) ([MS-DOC] §2.9.147, §2.9.150).
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use craft_wordmodel::numbering::{Level, LevelSuffix, Numbering};
|
||||
use craft_wordmodel::props::{Align, ParaProps};
|
||||
use craft_wordmodel::section::NumFormat;
|
||||
|
||||
use super::grpprl::{self, CharCtx, ParaCtx};
|
||||
|
||||
/// Levels of a multi-level list.
|
||||
const LEVELS: usize = 9;
|
||||
/// Most lists written (the format counts LFOs from 1 to 0x07FE).
|
||||
const MAX_LISTS: usize = 0x07FE;
|
||||
|
||||
/// The written lists: `PlfLst` (with its LVLs appended) and how much of it `lcbPlfLst` covers,
|
||||
/// `PlfLfo`, and each model num id's LFO index.
|
||||
pub(crate) struct Lists {
|
||||
pub(crate) plf_lst: Vec<u8>,
|
||||
pub(crate) lcb_plf_lst: u32,
|
||||
pub(crate) plf_lfo: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Each model num's 1-based LFO index, in `Numbering::nums` order.
|
||||
pub(crate) fn lfo_indexes(n: &Numbering) -> HashMap<u32, u16> {
|
||||
n.nums.iter().take(MAX_LISTS).enumerate().map(|(i, num)| (num.id, (i + 1) as u16)).collect()
|
||||
}
|
||||
|
||||
/// The list id (`lsid`) of abstract list `i`: any unique value other than 0 and -1.
|
||||
fn lsid(i: usize) -> i32 {
|
||||
0x1000_0000 + i as i32
|
||||
}
|
||||
|
||||
pub(crate) fn write(n: &Numbering, ctx: &mut (impl CharCtx + ParaCtx)) -> Option<Lists> {
|
||||
let abstracts: Vec<_> = n.abstracts.iter().take(MAX_LISTS).collect();
|
||||
if abstracts.is_empty() || n.nums.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut lst = Vec::new();
|
||||
lst.extend_from_slice(&(abstracts.len() as i16).to_le_bytes());
|
||||
for (i, _) in abstracts.iter().enumerate() {
|
||||
// LSTF: lsid, tplc, rgistdPara[9] (none), flags (multi-level), grfhic.
|
||||
lst.extend_from_slice(&lsid(i).to_le_bytes());
|
||||
lst.extend_from_slice(&0u32.to_le_bytes());
|
||||
for _ in 0..LEVELS {
|
||||
lst.extend_from_slice(&0x0FFFu16.to_le_bytes());
|
||||
}
|
||||
lst.push(0);
|
||||
lst.push(0);
|
||||
}
|
||||
let lcb_plf_lst = lst.len() as u32;
|
||||
for a in &abstracts {
|
||||
for l in 0..LEVELS {
|
||||
let level = a.levels.get(l).cloned().unwrap_or_else(|| default_level(l));
|
||||
lst.extend_from_slice(&lvl(&level, ctx));
|
||||
}
|
||||
}
|
||||
|
||||
let nums: Vec<_> = n.nums.iter().take(MAX_LISTS).collect();
|
||||
let mut lfo = Vec::new();
|
||||
lfo.extend_from_slice(&(nums.len() as i32).to_le_bytes());
|
||||
let mut data = Vec::new();
|
||||
for num in &nums {
|
||||
let ai = abstracts.iter().position(|a| a.id == num.abstract_id).unwrap_or(0);
|
||||
// The overrides per level: a start value, a whole level, or both.
|
||||
let mut over: Vec<(u8, Option<u32>, Option<&Level>)> = Vec::new();
|
||||
for &(l, start) in &num.start_overrides {
|
||||
if usize::from(l) < LEVELS {
|
||||
over.push((l, Some(start), None));
|
||||
}
|
||||
}
|
||||
for (l, level) in &num.level_overrides {
|
||||
if usize::from(*l) >= LEVELS {
|
||||
continue;
|
||||
}
|
||||
match over.iter_mut().find(|o| o.0 == *l) {
|
||||
Some(o) => o.2 = Some(level),
|
||||
None => over.push((*l, None, Some(level))),
|
||||
}
|
||||
}
|
||||
over.sort_by_key(|o| o.0);
|
||||
// LFO: lsid, unused1, unused2, clfolvl, ibstFltAutoNum, grfhic, unused3.
|
||||
lfo.extend_from_slice(&lsid(ai).to_le_bytes());
|
||||
lfo.extend_from_slice(&[0u8; 8]);
|
||||
lfo.push(over.len() as u8);
|
||||
lfo.extend_from_slice(&[0u8; 3]);
|
||||
// LFOData: cp (unused, 0xFFFFFFFF), then the LFOLVLs.
|
||||
data.extend_from_slice(&u32::MAX.to_le_bytes());
|
||||
for (l, start, level) in over {
|
||||
let start_at = start.or(level.map(|lv| lv.start)).unwrap_or(1).min(0x7FFF);
|
||||
data.extend_from_slice(&(start_at as i32).to_le_bytes());
|
||||
let mut flags = l & 0x0F;
|
||||
if start.is_some() {
|
||||
flags |= 0x10; // fStartAt
|
||||
}
|
||||
if level.is_some() {
|
||||
flags |= 0x20; // fFormatting
|
||||
}
|
||||
data.push(flags);
|
||||
data.extend_from_slice(&[0u8; 3]);
|
||||
if let Some(level) = level {
|
||||
data.extend_from_slice(&lvl(level, ctx));
|
||||
}
|
||||
}
|
||||
}
|
||||
lfo.extend_from_slice(&data);
|
||||
Some(Lists { plf_lst: lst, lcb_plf_lst, plf_lfo: lfo })
|
||||
}
|
||||
|
||||
/// A level the model's list doesn't have: decimal, `%n.`, indented a quarter inch a level.
|
||||
fn default_level(l: usize) -> Level {
|
||||
Level { start: 1, format: NumFormat::Decimal, text: format!("%{}.", l + 1), indent: 18.0 * (l as f32 + 1.0), hanging: 18.0, ..Level::default() }
|
||||
}
|
||||
|
||||
/// One LVL: LVLF, grpprlPapx (the indents), grpprlChpx (the number's formatting), number text.
|
||||
fn lvl(level: &Level, ctx: &mut (impl CharCtx + ParaCtx)) -> Vec<u8> {
|
||||
// Number text: `%n` placeholders become the level index (n − 1), their 1-based positions
|
||||
// go in rgbxchNums.
|
||||
let mut xst: Vec<u16> = Vec::new();
|
||||
let mut nums: Vec<u8> = Vec::new();
|
||||
let mut chars = level.text.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '%'
|
||||
&& let Some(d) = chars.peek().and_then(|d| d.to_digit(10)).filter(|d| (1..=9).contains(d))
|
||||
{
|
||||
chars.next();
|
||||
xst.push((d - 1) as u16);
|
||||
if nums.len() < 9 {
|
||||
nums.push(xst.len() as u8);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let mut buf = [0u16; 2];
|
||||
xst.extend_from_slice(c.encode_utf16(&mut buf));
|
||||
}
|
||||
xst.truncate(64);
|
||||
nums.retain(|&p| usize::from(p) <= xst.len());
|
||||
|
||||
let mut papx = grpprl::papx(&ParaProps { indent_left: Some(level.indent), indent_first: Some(-level.hanging), ..Default::default() }, ctx);
|
||||
papx.truncate_to(255);
|
||||
let mut chpx = grpprl::chpx(&level.chr, ctx);
|
||||
chpx.truncate_to(255);
|
||||
|
||||
let mut v = Vec::with_capacity(28 + papx.len() + chpx.len() + 2 + xst.len() * 2);
|
||||
v.extend_from_slice(&(level.start.min(0x7FFF) as i32).to_le_bytes());
|
||||
v.push(grpprl::nfc(level.format));
|
||||
let jc: u8 = match level.align {
|
||||
Align::Center => 1,
|
||||
Align::Right => 2,
|
||||
_ => 0,
|
||||
};
|
||||
let mut flags = jc;
|
||||
if level.legal {
|
||||
flags |= 0x04;
|
||||
}
|
||||
if !level.restart {
|
||||
flags |= 0x08;
|
||||
}
|
||||
v.push(flags);
|
||||
let mut rgbxch = [0u8; 9];
|
||||
for (slot, p) in rgbxch.iter_mut().zip(&nums) {
|
||||
*slot = *p;
|
||||
}
|
||||
v.extend_from_slice(&rgbxch);
|
||||
v.push(match level.suffix {
|
||||
LevelSuffix::Tab => 0,
|
||||
LevelSuffix::Space => 1,
|
||||
LevelSuffix::Nothing => 2,
|
||||
});
|
||||
v.extend_from_slice(&0i32.to_le_bytes()); // dxaIndentSav
|
||||
v.extend_from_slice(&0u32.to_le_bytes()); // unused2
|
||||
v.push(chpx.len() as u8);
|
||||
v.push(papx.len() as u8);
|
||||
v.push(level.restart_after.map_or(0, |r| r.saturating_add(1)).min(LEVELS as u8));
|
||||
v.push(0); // grfhic
|
||||
v.extend_from_slice(&papx.0);
|
||||
v.extend_from_slice(&chpx.0);
|
||||
v.extend_from_slice(&(xst.len() as u16).to_le_bytes());
|
||||
for u in &xst {
|
||||
v.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
v
|
||||
}
|
||||
@@ -0,0 +1,463 @@
|
||||
//! The `.doc` writer: a [`Document`] as a Word 97-2003 binary file ([MS-DOC]).
|
||||
//!
|
||||
//! The file is what Word 97 itself writes, minus the parts the model has nothing for: a compound file
|
||||
//! with the WordDocument stream (the FIB, the text as one Unicode piece, the formatting pages and
|
||||
//! the section properties), the 1Table stream (stylesheet, fonts, piece table, document
|
||||
//! properties, the PLCs locating sections, headers, notes, fields and bookmarks, the lists), a
|
||||
//! Data stream with the pictures, and the summary information.
|
||||
//!
|
||||
//! Kept: text, character and paragraph formatting, styles, lists, tables (one level; nested
|
||||
//! tables become paragraphs of their cell), sections with headers and footers, footnotes and
|
||||
//! endnotes, fields and hyperlinks, bookmarks, inline pictures, tracked insertions and deletions,
|
||||
//! and the document properties. Floating pictures are written in line; shapes, charts, text
|
||||
//! boxes and comments are left out, each kind logged once per save.
|
||||
|
||||
mod fkp;
|
||||
mod grpprl;
|
||||
mod lists;
|
||||
mod picture;
|
||||
mod story;
|
||||
mod styles;
|
||||
mod summary;
|
||||
mod tables;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use craft_wordmodel::Document;
|
||||
use craft_wordmodel::para::{NoteKind, Paragraph};
|
||||
use craft_wordmodel::section::SectionProps;
|
||||
|
||||
use crate::DocbinError;
|
||||
use crate::fib::pair;
|
||||
use grpprl::{CharCtx, Grpprl};
|
||||
use story::{Lookup, PARA, Text};
|
||||
|
||||
/// Where the text starts in the WordDocument stream: right after the FIB (900 bytes).
|
||||
const TEXT_FC: u32 = 0x400;
|
||||
/// Bytes in the FIB this writer produces: FibBase, csw + FibRgW97, cslw + FibRgLw97,
|
||||
/// cbRgFcLcb + FibRgFcLcb97 (0x5D pairs), cswNew.
|
||||
const FIB_LEN: usize = 0x9A + 0x5D * 8 + 2;
|
||||
/// Most characters written: the WordDocument stream's FCs are 32-bit and each character takes
|
||||
/// two bytes; stay far inside.
|
||||
const MAX_CP: usize = 256 << 20;
|
||||
/// Size of a Dop97.
|
||||
const DOP97_LEN: usize = 500;
|
||||
|
||||
/// Write `doc` as a Word 97-2003 document.
|
||||
pub fn write(doc: &Document) -> Result<Vec<u8>, DocbinError> {
|
||||
let plan = styles::plan(&doc.styles);
|
||||
let normal = plan.istd.get("Normal").copied().unwrap_or(0);
|
||||
let lfos = lists::lfo_indexes(&doc.numbering);
|
||||
let mut look = Lookup::new(doc, plan.istd.clone(), normal, lfos);
|
||||
// Font 0 is the document's default font, as in every Word file; the stylesheet header names
|
||||
// it as the standard font.
|
||||
let default_font = doc.styles.default_chr.font.clone().unwrap_or_else(|| "Times New Roman".to_string());
|
||||
let f_ascii = look.font(&default_font);
|
||||
let f_ea = doc.styles.default_chr.font_ea.as_deref().map_or(f_ascii, |f| look.font(f));
|
||||
let f_cs = doc.styles.default_chr.font_cs.as_deref().map_or(f_ascii, |f| look.font(f));
|
||||
let stsh = styles::stsh(&doc.styles, &plan, &mut look, [f_ascii, f_ea, f_cs]);
|
||||
let lists = lists::write(&doc.numbering, &mut look);
|
||||
|
||||
let mut t = Text::new(look);
|
||||
// The main document; it ends with a plain paragraph (a table or a section break can't end it).
|
||||
t.blocks(&doc.body, true);
|
||||
t.close_fields();
|
||||
let ends_plain = doc.body.last().and_then(|b| b.as_para()).is_some_and(|p| p.section.is_none());
|
||||
if !ends_plain || t.paras.last().is_none_or(|p| p.end_cp != t.cp()) {
|
||||
t.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
}
|
||||
let ccp_text = t.cp();
|
||||
let mut sections: Vec<(u32, SectionProps)> = std::mem::take(&mut t.sections);
|
||||
sections.push((ccp_text, doc.last_section.clone()));
|
||||
|
||||
// Footnotes, then the header document, then endnotes, each a subdocument.
|
||||
let (ccp_ftn, ftn_txt) = notes(&mut t, doc, NoteKind::Footnote);
|
||||
let hdd_start = t.cp();
|
||||
let hdd_stories = headers(&mut t, doc, §ions);
|
||||
let ccp_hdd = t.cp() - hdd_start;
|
||||
let (ccp_edn, edn_txt) = notes(&mut t, doc, NoteKind::Endnote);
|
||||
if ccp_ftn + ccp_hdd + ccp_edn > 0 {
|
||||
// One more paragraph mark after the last subdocument ([MS-DOC] §2.8.35).
|
||||
t.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
}
|
||||
let ccp_all = t.cp();
|
||||
if t.units.len() > MAX_CP {
|
||||
return Err(DocbinError::Limit(format!("the document has {} characters, more than a .doc holds", t.units.len())));
|
||||
}
|
||||
log_dropped(&t.dropped);
|
||||
|
||||
// ---- WordDocument stream: FIB, text, formatting pages, section properties.
|
||||
let mut word = vec![0u8; TEXT_FC as usize];
|
||||
for u in &t.units {
|
||||
word.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
let fc = |cp: u32| TEXT_FC + cp * 2;
|
||||
pad_to(&mut word, 512);
|
||||
let runs = t.char_runs();
|
||||
let chpx_runs: Vec<fkp::ChpxRun> = runs.iter().map(|&(a, b, g)| fkp::ChpxRun { fc_start: fc(a), fc_end: fc(b), grpprl: g }).collect();
|
||||
let chpx = fkp::chpx_pages(&chpx_runs);
|
||||
let chpx_pn = (word.len() / 512) as u32;
|
||||
for p in &chpx.pages {
|
||||
word.extend_from_slice(p);
|
||||
}
|
||||
let mut start = 0u32;
|
||||
let papx_runs: Vec<fkp::PapxRun> = t
|
||||
.paras
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let r = fkp::PapxRun { fc_start: fc(start), fc_end: fc(p.end_cp), istd: p.istd, grpprl: p.grpprl.as_slice() };
|
||||
start = p.end_cp;
|
||||
r
|
||||
})
|
||||
.collect();
|
||||
let papx = fkp::papx_pages(&papx_runs);
|
||||
let papx_pn = (word.len() / 512) as u32;
|
||||
for p in &papx.pages {
|
||||
word.extend_from_slice(p);
|
||||
}
|
||||
let mut sepx_at = Vec::with_capacity(sections.len());
|
||||
for (_, s) in §ions {
|
||||
pad_to(&mut word, 2);
|
||||
sepx_at.push(word.len() as u32);
|
||||
let g = grpprl::sepx(s);
|
||||
word.extend_from_slice(&(g.len() as u16).to_le_bytes());
|
||||
word.extend_from_slice(&g.0);
|
||||
}
|
||||
let cb_mac = word.len() as u32;
|
||||
|
||||
// ---- Table stream.
|
||||
let mut tbl = TableStream::default();
|
||||
tbl.put(pair::STSHF, &stsh);
|
||||
if let Some(&v) = tbl.pairs.get(&pair::STSHF) {
|
||||
tbl.pairs.insert(0, v); // fcStshfOrig: the same stylesheet
|
||||
}
|
||||
// Notes: references in the main text and the stories in their subdocuments.
|
||||
for (kind, ref_pair, txt_pair, ccp, txt) in [
|
||||
(NoteKind::Footnote, pair::PLCF_FND_REF, pair::PLCF_FND_TXT, ccp_ftn, &ftn_txt),
|
||||
(NoteKind::Endnote, pair::PLCF_END_REF, pair::PLCF_END_TXT, ccp_edn, &edn_txt),
|
||||
] {
|
||||
if ccp == 0 {
|
||||
continue;
|
||||
}
|
||||
let refs: Vec<&story::NoteRefAt> = t.notes.iter().filter(|n| n.kind == kind).collect();
|
||||
let mut cps: Vec<u32> = refs.iter().map(|n| n.cp).collect();
|
||||
cps.push(ccp_text);
|
||||
let idx: Vec<[u8; 2]> = refs.iter().enumerate().map(|(i, n)| if n.auto { (i as u16 + 1).to_le_bytes() } else { [0, 0] }).collect();
|
||||
tbl.put(ref_pair, &fkp::plc(&cps, &idx.iter().map(|x| &x[..]).collect::<Vec<_>>()));
|
||||
let mut tcps = txt.clone();
|
||||
tcps.push(ccp - 1);
|
||||
tcps.push(ccp);
|
||||
tbl.put(txt_pair, &fkp::plc(&tcps, &[]));
|
||||
}
|
||||
// Sections.
|
||||
let mut sed_cps = vec![0u32];
|
||||
sed_cps.extend(sections.iter().map(|(end, _)| *end));
|
||||
let seds: Vec<[u8; 12]> = sepx_at
|
||||
.iter()
|
||||
.map(|at| {
|
||||
let mut s = [0u8; 12];
|
||||
s[2..6].copy_from_slice(&at.to_le_bytes());
|
||||
s[8..12].copy_from_slice(&u32::MAX.to_le_bytes());
|
||||
s
|
||||
})
|
||||
.collect();
|
||||
tbl.put(pair::PLCF_SED, &fkp::plc(&sed_cps, &seds.iter().map(|x| &x[..]).collect::<Vec<_>>()));
|
||||
if ccp_hdd > 0 {
|
||||
let mut cps = hdd_stories;
|
||||
cps.push(ccp_hdd - 1);
|
||||
cps.push(ccp_hdd);
|
||||
tbl.put(pair::PLCF_HDD, &fkp::plc(&cps, &[]));
|
||||
}
|
||||
tbl.put(pair::PLCF_BTE_CHPX, &chpx.plc_bte(chpx_pn));
|
||||
tbl.put(pair::PLCF_BTE_PAPX, &papx.plc_bte(papx_pn));
|
||||
// Fields, one PLC per subdocument with CPs relative to it.
|
||||
let ftn_start = ccp_text;
|
||||
let edn_start = hdd_start + ccp_hdd;
|
||||
for (i, a, len) in [(16usize, 0, ccp_text), (18, ftn_start, ccp_ftn), (17, hdd_start, ccp_hdd), (48, edn_start, ccp_edn)] {
|
||||
let f: Vec<&story::FieldChar> = t.fields.iter().filter(|f| f.cp >= a && f.cp < a + len).collect();
|
||||
if f.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut cps: Vec<u32> = f.iter().map(|f| f.cp - a).collect();
|
||||
cps.push(len);
|
||||
tbl.put(i, &fkp::plc(&cps, &f.iter().map(|f| &f.fld[..]).collect::<Vec<_>>()));
|
||||
}
|
||||
// Bookmarks in the main text (with an end): names, starts with the index of their end, ends.
|
||||
let mut marks: Vec<(String, u32, u32)> =
|
||||
t.bookmarks.iter().filter_map(|(n, a, b)| b.filter(|&b| b <= ccp_text && *a <= b).map(|b| (n.clone(), *a, b))).take(0x3FFF).collect();
|
||||
marks.sort_by_key(|m| m.1);
|
||||
if !marks.is_empty() {
|
||||
let mut ends: Vec<(u32, usize)> = marks.iter().enumerate().map(|(i, m)| (m.2, i)).collect();
|
||||
ends.sort();
|
||||
let mut ibkl = vec![0u16; marks.len()];
|
||||
for (pos, (_, i)) in ends.iter().enumerate() {
|
||||
if let Some(slot) = ibkl.get_mut(*i) {
|
||||
*slot = pos as u16;
|
||||
}
|
||||
}
|
||||
let names: Vec<(String, Vec<u8>)> = marks.iter().map(|m| (m.0.chars().take(40).collect(), Vec::new())).collect();
|
||||
tbl.put(pair::STTBF_BKMK, &fkp::sttb_ext(&names, 0));
|
||||
let mut cps: Vec<u32> = marks.iter().map(|m| m.1).collect();
|
||||
cps.push(ccp_text + 1);
|
||||
let fbkf: Vec<[u8; 4]> = ibkl
|
||||
.iter()
|
||||
.map(|i| {
|
||||
let mut b = [0u8; 4];
|
||||
b[0..2].copy_from_slice(&i.to_le_bytes());
|
||||
b
|
||||
})
|
||||
.collect();
|
||||
tbl.put(pair::PLCF_BKF, &fkp::plc(&cps, &fbkf.iter().map(|x| &x[..]).collect::<Vec<_>>()));
|
||||
let mut cps: Vec<u32> = ends.iter().map(|e| e.0).collect();
|
||||
cps.push(ccp_text + 1);
|
||||
tbl.put(pair::PLCF_BKL, &fkp::plc(&cps, &[]));
|
||||
}
|
||||
tbl.put(31, &dop(doc));
|
||||
// SttbfAssoc MUST be there: 18 strings (template, title, author…), all empty here.
|
||||
tbl.put(32, &fkp::sttb_ext(&vec![(String::new(), Vec::new()); 18], 0));
|
||||
// The piece table: one Unicode piece holding every character.
|
||||
let mut plc = fkp::plc(&[0, ccp_all], &[]);
|
||||
plc.extend_from_slice(&0u16.to_le_bytes());
|
||||
plc.extend_from_slice(&TEXT_FC.to_le_bytes());
|
||||
plc.extend_from_slice(&0u16.to_le_bytes());
|
||||
let mut clx = vec![0x02];
|
||||
clx.extend_from_slice(&(plc.len() as u32).to_le_bytes());
|
||||
clx.extend_from_slice(&plc);
|
||||
tbl.put(pair::CLX, &clx);
|
||||
if t.look.authors.len() > 1 {
|
||||
let authors: Vec<(String, Vec<u8>)> = t.look.authors.iter().map(|a| (a.clone(), Vec::new())).collect();
|
||||
tbl.put(51, &fkp::sttb_ext(&authors, 0));
|
||||
}
|
||||
if let Some(l) = &lists {
|
||||
pad_to(&mut tbl.bytes, 2);
|
||||
tbl.pairs.insert(pair::PLF_LST, (tbl.bytes.len() as u32, l.lcb_plf_lst));
|
||||
tbl.bytes.extend_from_slice(&l.plf_lst);
|
||||
tbl.put(pair::PLF_LFO, &l.plf_lfo);
|
||||
}
|
||||
// The font table last: every font the text and styles name is in it now.
|
||||
tbl.put(pair::STTBF_FFN, &styles::font_table(&t.look.fonts));
|
||||
|
||||
// ---- FIB.
|
||||
let has_pic = !t.pictures.data.is_empty();
|
||||
let fib = fib(cb_mac, [ccp_text, ccp_ftn, ccp_hdd, ccp_edn], &tbl.pairs, has_pic);
|
||||
if let Some(head) = word.get_mut(..FIB_LEN) {
|
||||
head.copy_from_slice(&fib);
|
||||
}
|
||||
|
||||
// ---- The compound file.
|
||||
let mut streams: Vec<(&str, Vec<u8>)> = vec![("WordDocument", word), ("1Table", tbl.bytes)];
|
||||
if has_pic {
|
||||
streams.push(("Data", std::mem::take(&mut t.pictures.data)));
|
||||
}
|
||||
streams.push(("\u{5}SummaryInformation", summary::stream(&doc.core)));
|
||||
compound(&streams)
|
||||
}
|
||||
|
||||
/// The Table stream being built, and the FIB pair (fc, lcb) of each structure in it.
|
||||
#[derive(Default)]
|
||||
struct TableStream {
|
||||
bytes: Vec<u8>,
|
||||
pairs: HashMap<usize, (u32, u32)>,
|
||||
}
|
||||
|
||||
impl TableStream {
|
||||
/// Append a structure (word-aligned) and record it as FIB pair `i`; nothing for an empty one.
|
||||
fn put(&mut self, i: usize, bytes: &[u8]) {
|
||||
if bytes.is_empty() {
|
||||
return;
|
||||
}
|
||||
pad_to(&mut self.bytes, 2);
|
||||
self.pairs.insert(i, (self.bytes.len() as u32, bytes.len() as u32));
|
||||
self.bytes.extend_from_slice(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the stories of the notes of `kind` the main text referred to, in reference order:
|
||||
/// the subdocument's length and each story's start CP (relative to it).
|
||||
fn notes(t: &mut Text<'_>, doc: &Document, kind: NoteKind) -> (u32, Vec<u32>) {
|
||||
let start = t.cp();
|
||||
let parts: Vec<u32> = t.notes.iter().filter(|n| n.kind == kind).map(|n| n.part).collect();
|
||||
if parts.is_empty() {
|
||||
return (0, Vec::new());
|
||||
}
|
||||
let mut starts = Vec::with_capacity(parts.len());
|
||||
for id in parts {
|
||||
starts.push(t.cp() - start);
|
||||
let blocks = doc.parts.get(&id).map(|p| p.blocks.as_slice()).unwrap_or_default();
|
||||
t.note_story(kind, id, blocks);
|
||||
t.close_fields();
|
||||
}
|
||||
// The guard paragraph mark after the last story.
|
||||
t.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
(t.cp() - start, starts)
|
||||
}
|
||||
|
||||
/// Write the header document: the six (empty) note separator stories, then six stories per
|
||||
/// section (even header, header, even footer, footer, first-page header, first-page footer),
|
||||
/// each ended by a guard paragraph mark. The story start CPs (relative); nothing is written
|
||||
/// when no section has a header or footer.
|
||||
fn headers(t: &mut Text<'_>, doc: &Document, sections: &[(u32, SectionProps)]) -> Vec<u32> {
|
||||
let any = sections.iter().any(|(_, s)| {
|
||||
[s.headers.default, s.headers.first, s.headers.even, s.footers.default, s.footers.first, s.footers.even].iter().any(Option::is_some)
|
||||
});
|
||||
if !any {
|
||||
return Vec::new();
|
||||
}
|
||||
let start = t.cp();
|
||||
let mut cps = vec![0u32; 6];
|
||||
for (_, s) in sections {
|
||||
for id in [s.headers.even, s.headers.default, s.footers.even, s.footers.default, s.headers.first, s.footers.first] {
|
||||
cps.push(t.cp() - start);
|
||||
let Some(part) = id.and_then(|id| doc.parts.get(&id)) else { continue };
|
||||
if part.blocks.is_empty() {
|
||||
t.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
} else {
|
||||
t.blocks(&part.blocks, false);
|
||||
}
|
||||
t.close_fields();
|
||||
t.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
}
|
||||
}
|
||||
// The final mark the last CP of Plcfhdd ends at ([MS-DOC] §2.8.22).
|
||||
t.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
cps
|
||||
}
|
||||
|
||||
/// Log, once per save, each kind of content the file leaves out.
|
||||
fn log_dropped(d: &story::Dropped) {
|
||||
for (n, what) in [
|
||||
(d.shapes, "shapes, charts and text boxes are left out"),
|
||||
(d.comments, "comments are left out"),
|
||||
(d.nested_tables, "tables inside table cells become paragraphs of their cell"),
|
||||
(d.floating_pictures, "floating pictures are placed in line with the text"),
|
||||
(d.pictures, "pictures in formats a .doc can't hold are left out"),
|
||||
(d.equations, "equations are written as their linear text"),
|
||||
] {
|
||||
if n > 0 {
|
||||
log::warn!(".doc save: {n} × {what}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pad_to(v: &mut Vec<u8>, align: usize) {
|
||||
while !v.len().is_multiple_of(align) {
|
||||
v.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// The FIB for nFib 0x00C1 ([MS-DOC] §2.5): `ccp` is text, footnotes, headers, endnotes.
|
||||
fn fib(cb_mac: u32, ccp: [u32; 4], pairs: &HashMap<usize, (u32, u32)>, has_pic: bool) -> Vec<u8> {
|
||||
let mut v = vec![0u8; FIB_LEN];
|
||||
let put16 = |v: &mut Vec<u8>, at: usize, x: u16| {
|
||||
if let Some(b) = v.get_mut(at..at + 2) {
|
||||
b.copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
};
|
||||
put16(&mut v, 0x00, 0xA5EC); // wIdent
|
||||
put16(&mut v, 0x02, 0x00C1); // nFib
|
||||
put16(&mut v, 0x06, 0x0409); // lid
|
||||
// fHasPic, fWhichTblStm (1Table), fExtChar.
|
||||
put16(&mut v, 0x0A, 0x0200 | 0x1000 | if has_pic { 0x0008 } else { 0 });
|
||||
put16(&mut v, 0x0C, 0x00BF); // nFibBack
|
||||
put16(&mut v, 0x20, 0x000E); // csw
|
||||
put16(&mut v, 0x22 + 13 * 2, 0x0409); // FibRgW97.lidFE
|
||||
put16(&mut v, 0x3E, 0x0016); // cslw
|
||||
let put32 = |v: &mut Vec<u8>, at: usize, x: u32| {
|
||||
if let Some(b) = v.get_mut(at..at + 4) {
|
||||
b.copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
};
|
||||
put32(&mut v, 0x40, cb_mac);
|
||||
put32(&mut v, 0x40 + 3 * 4, ccp[0]);
|
||||
put32(&mut v, 0x40 + 4 * 4, ccp[1]);
|
||||
put32(&mut v, 0x40 + 5 * 4, ccp[2]);
|
||||
put32(&mut v, 0x40 + 8 * 4, ccp[3]);
|
||||
put16(&mut v, 0x98, 0x005D); // cbRgFcLcb
|
||||
for (&i, &(fc, lcb)) in pairs {
|
||||
if i < 0x5D {
|
||||
put32(&mut v, 0x9A + i * 8, fc);
|
||||
put32(&mut v, 0x9A + i * 8 + 4, lcb);
|
||||
}
|
||||
}
|
||||
// cswNew = 0: the last two bytes stay zero.
|
||||
v
|
||||
}
|
||||
|
||||
/// A Dop97 ([MS-DOC] §2.7.4): footnotes at the bottom of the page, endnotes at the end of the
|
||||
/// document, both numbered from 1; the default tab stop, hyphenation zone and the settings the
|
||||
/// document carries; 100 % zoom.
|
||||
fn dop(doc: &Document) -> Vec<u8> {
|
||||
let s = &doc.settings;
|
||||
let mut v = vec![0u8; DOP97_LEN];
|
||||
// fFacingPages (different odd and even headers), fpc = 1 (bottom of page).
|
||||
v[0] = u8::from(s.even_odd_headers) | (1 << 5);
|
||||
v[2..4].copy_from_slice(&(1u16 << 2).to_le_bytes()); // rncFtn 0, nFtn 1
|
||||
let mut flags: u32 = 0;
|
||||
if s.auto_hyphenation {
|
||||
flags |= 1 << 12; // fAutoHyphen
|
||||
}
|
||||
if s.hyphenate_caps {
|
||||
flags |= 1 << 11; // fHyphCapitals
|
||||
}
|
||||
if s.track_changes {
|
||||
flags |= 1 << 15; // fRevMarking
|
||||
}
|
||||
if s.mirror_margins {
|
||||
flags |= 1 << 21; // fMirrorMargins
|
||||
}
|
||||
v[4..8].copy_from_slice(&flags.to_le_bytes());
|
||||
let tab = if s.default_tab > 0.0 { grpprl::twips(s.default_tab) } else { 720 };
|
||||
v[10..12].copy_from_slice(&tab.to_le_bytes()); // dxaTab
|
||||
let hot = if s.hyphenation_zone > 0.0 { grpprl::twips(s.hyphenation_zone) } else { 360 };
|
||||
v[14..16].copy_from_slice(&hot.to_le_bytes()); // dxaHotZ
|
||||
v[20..24].copy_from_slice(&dttm(&doc.core.created).to_le_bytes());
|
||||
v[24..28].copy_from_slice(&dttm(&doc.core.modified).to_le_bytes());
|
||||
v[32..34].copy_from_slice(&(doc.core.revision.clamp(1, u16::MAX as u32) as u16).to_le_bytes());
|
||||
v[52..54].copy_from_slice(&(1u16 << 2).to_le_bytes()); // rncEdn 0, nEdn 1
|
||||
v[54..56].copy_from_slice(&3u16.to_le_bytes()); // epc: end of document
|
||||
v[82..84].copy_from_slice(&((100u16 << 3) | (1 << 12)).to_le_bytes()); // pctWwdSaved, zkSaved
|
||||
v
|
||||
}
|
||||
|
||||
/// A `DTTM` ([MS-DOC] §2.9.75) from an ISO 8601 date; 0 when there is none.
|
||||
pub(crate) fn dttm(iso: &str) -> u32 {
|
||||
use chrono::{Datelike, Timelike};
|
||||
let Some(t) = parse_time(iso) else { return 0 };
|
||||
let year = t.year();
|
||||
if !(1900..=2411).contains(&year) {
|
||||
return 0;
|
||||
}
|
||||
(t.minute() & 0x3F)
|
||||
| ((t.hour() & 0x1F) << 6)
|
||||
| ((t.day() & 0x1F) << 11)
|
||||
| ((t.month() & 0xF) << 16)
|
||||
| ((((year - 1900) as u32) & 0x1FF) << 20)
|
||||
| ((t.weekday().num_days_from_sunday() & 7) << 29)
|
||||
}
|
||||
|
||||
/// An ISO 8601 date or date-time (UTC when it doesn't say).
|
||||
pub(crate) fn parse_time(iso: &str) -> Option<chrono::NaiveDateTime> {
|
||||
let s = iso.trim();
|
||||
if s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if let Ok(t) = chrono::DateTime::parse_from_rfc3339(s) {
|
||||
return Some(t.naive_utc());
|
||||
}
|
||||
for f in ["%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S%.f", "%Y-%m-%d %H:%M:%S"] {
|
||||
if let Ok(t) = chrono::NaiveDateTime::parse_from_str(s.trim_end_matches('Z'), f) {
|
||||
return Some(t);
|
||||
}
|
||||
}
|
||||
chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").ok().and_then(|d| d.and_hms_opt(0, 0, 0))
|
||||
}
|
||||
|
||||
/// The compound file holding `streams` at its root. Version 3 (512-byte sectors), the version
|
||||
/// Word 97-2003 writes; readers of that era expect it.
|
||||
fn compound(streams: &[(&str, Vec<u8>)]) -> Result<Vec<u8>, DocbinError> {
|
||||
let refs: Vec<(&str, &[u8])> = streams.iter().map(|(name, data)| (*name, data.as_slice())).collect();
|
||||
craft_cfb::write_streams(craft_cfb::Version::V3, &refs).map_err(|e| DocbinError::Container(e.to_string()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,211 @@
|
||||
//! Inline pictures in the Data stream: a `PICF` header ([MS-DOC] §2.9.193) followed by an
|
||||
//! `OfficeArtInlineSpContainer` ([MS-ODRAW]): the picture-frame shape, its properties (the
|
||||
//! picture's place in the BLIP store, crop, alt text) and the `OfficeArtFBSE` with the BLIP
|
||||
//! embedded in it. The character 0x01 in the text points here through `sprmCPicLocation`.
|
||||
|
||||
use sha1::Digest;
|
||||
|
||||
/// `PICF.cbHeader`.
|
||||
const CB_HEADER: u16 = 0x44;
|
||||
/// `MM_SHAPE`: the picture is the OfficeArt shape that follows the header.
|
||||
const MM_SHAPE: u16 = 0x0064;
|
||||
/// Largest picture written (bytes of its BLIP): Word's own limit for one picture is far above
|
||||
/// what the Data stream's 32-bit offsets allow many of.
|
||||
const MAX_PICTURE: usize = 64 << 20;
|
||||
/// All pictures together: `sprmCPicLocation` offsets are 32-bit, keep well inside them.
|
||||
const MAX_DATA: usize = 1 << 30;
|
||||
|
||||
/// OfficeArt record types and BLIP instances ([MS-ODRAW] §2.2).
|
||||
const SP_CONTAINER: u16 = 0xF004;
|
||||
const FSP: u16 = 0xF00A;
|
||||
const FOPT: u16 = 0xF00B;
|
||||
const FBSE: u16 = 0xF007;
|
||||
const BLIP_EMF: (u16, u16) = (0xF01A, 0x3D4);
|
||||
const BLIP_WMF: (u16, u16) = (0xF01B, 0x216);
|
||||
const BLIP_JPEG: (u16, u16) = (0xF01D, 0x46A);
|
||||
const BLIP_PNG: (u16, u16) = (0xF01E, 0x6E0);
|
||||
/// `msosptPictureFrame`.
|
||||
const SPT_PICTURE_FRAME: u16 = 75;
|
||||
|
||||
/// The Data stream being built.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Pictures {
|
||||
pub(crate) data: Vec<u8>,
|
||||
}
|
||||
|
||||
/// A picture in a form the BLIP store holds, with its `btWin32` type.
|
||||
enum Blip {
|
||||
Png(Vec<u8>),
|
||||
Jpeg(Vec<u8>),
|
||||
/// EMF (true) or WMF metafile bytes.
|
||||
Metafile(Vec<u8>, bool),
|
||||
}
|
||||
|
||||
impl Blip {
|
||||
/// Sniff `bytes`: PNG and JPEG go in as they are, metafiles compressed, other raster
|
||||
/// formats (GIF, BMP, TIFF, WebP) re-encoded as PNG.
|
||||
fn of(bytes: &[u8]) -> Option<Blip> {
|
||||
if bytes.starts_with(b"\x89PNG") {
|
||||
return Some(Blip::Png(bytes.to_vec()));
|
||||
}
|
||||
if bytes.starts_with(&[0xFF, 0xD8, 0xFF]) {
|
||||
return Some(Blip::Jpeg(bytes.to_vec()));
|
||||
}
|
||||
// EMF: an EMR_HEADER record (type 1) with " EMF" at offset 40.
|
||||
if bytes.get(0..4) == Some(&[1, 0, 0, 0]) && bytes.get(40..44) == Some(b" EMF") {
|
||||
return Some(Blip::Metafile(bytes.to_vec(), true));
|
||||
}
|
||||
// WMF: the placeable header's key, or a plain header (type 1 or 2, header size 9).
|
||||
if bytes.starts_with(&[0xD7, 0xCD, 0xC6, 0x9A]) || (matches!(bytes.get(0..4), Some([1 | 2, 0, 9, 0]))) {
|
||||
// The BLIP holds the metafile without the placeable header.
|
||||
let body = if bytes.starts_with(&[0xD7, 0xCD, 0xC6, 0x9A]) { bytes.get(22..)? } else { bytes };
|
||||
return Some(Blip::Metafile(body.to_vec(), false));
|
||||
}
|
||||
let img = image::load_from_memory(bytes).ok()?;
|
||||
let mut png = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
|
||||
Some(Blip::Png(png))
|
||||
}
|
||||
|
||||
/// `btWin32` ([MS-ODRAW] §2.4.1 MSOBLIPTYPE).
|
||||
fn bt(&self) -> u8 {
|
||||
match self {
|
||||
Blip::Png(_) => 6,
|
||||
Blip::Jpeg(_) => 5,
|
||||
Blip::Metafile(_, true) => 2,
|
||||
Blip::Metafile(_, false) => 3,
|
||||
}
|
||||
}
|
||||
|
||||
/// The `OfficeArtBlip*` record.
|
||||
fn record(&self, w_emu: i32, h_emu: i32) -> Vec<u8> {
|
||||
let (raw, (typ, inst)) = match self {
|
||||
Blip::Png(b) => (b, BLIP_PNG),
|
||||
Blip::Jpeg(b) => (b, BLIP_JPEG),
|
||||
Blip::Metafile(b, emf) => (b, if *emf { BLIP_EMF } else { BLIP_WMF }),
|
||||
};
|
||||
let mut body = uid(raw).to_vec();
|
||||
match self {
|
||||
Blip::Png(b) | Blip::Jpeg(b) => {
|
||||
body.push(0xFF); // tag
|
||||
body.extend_from_slice(b);
|
||||
}
|
||||
Blip::Metafile(b, _) => {
|
||||
let packed = miniz_oxide::deflate::compress_to_vec_zlib(b, 6);
|
||||
// OfficeArtMetafileHeader: cbSize, rcBounds, ptSize (EMUs), cbSave, compression
|
||||
// (0 = deflate), filter (0xFE = none).
|
||||
body.extend_from_slice(&(b.len() as u32).to_le_bytes());
|
||||
for v in [0i32, 0, w_emu / 12_700, h_emu / 12_700] {
|
||||
body.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
body.extend_from_slice(&w_emu.to_le_bytes());
|
||||
body.extend_from_slice(&h_emu.to_le_bytes());
|
||||
body.extend_from_slice(&(packed.len() as u32).to_le_bytes());
|
||||
body.push(0);
|
||||
body.push(0xFE);
|
||||
body.extend_from_slice(&packed);
|
||||
}
|
||||
}
|
||||
rec(0, inst, typ, &body)
|
||||
}
|
||||
}
|
||||
|
||||
/// A 16-byte id for a BLIP (the format wants an MD4; any digest identifies it).
|
||||
fn uid(bytes: &[u8]) -> [u8; 16] {
|
||||
let d = sha1::Sha1::digest(bytes);
|
||||
let mut u = [0u8; 16];
|
||||
u.copy_from_slice(d.get(..16).unwrap_or(&[0; 16]));
|
||||
u
|
||||
}
|
||||
|
||||
/// An OfficeArt record: header (version, instance, type, length) then the body.
|
||||
fn rec(ver: u16, inst: u16, typ: u16, body: &[u8]) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(8 + body.len());
|
||||
v.extend_from_slice(&((ver & 0xF) | (inst << 4)).to_le_bytes());
|
||||
v.extend_from_slice(&typ.to_le_bytes());
|
||||
v.extend_from_slice(&(body.len() as u32).to_le_bytes());
|
||||
v.extend_from_slice(body);
|
||||
v
|
||||
}
|
||||
|
||||
/// A 16.16 fixed-point fraction.
|
||||
fn fixed(f: f32) -> u32 {
|
||||
((f.clamp(0.0, 0.99) * 65_536.0).round() as i32) as u32
|
||||
}
|
||||
|
||||
impl Pictures {
|
||||
/// Add a picture shown `w` × `h` points with `crop` (left, top, right, bottom fractions):
|
||||
/// the offset of its `PICF` in the Data stream, `None` when the picture can't be stored.
|
||||
pub(crate) fn add(&mut self, bytes: &[u8], w: f32, h: f32, crop: [f32; 4], alt: &str) -> Option<u32> {
|
||||
if bytes.len() > MAX_PICTURE {
|
||||
return None;
|
||||
}
|
||||
let blip = Blip::of(bytes)?;
|
||||
let [cl, ct, cr, cb] = crop.map(|c| if c.is_finite() { c.clamp(0.0, 0.99) } else { 0.0 });
|
||||
// The goal size is the uncropped picture's: the crop takes its share off it.
|
||||
let gw = (w.max(1.0) / (1.0 - cl - cr).max(0.01)).min(1584.0);
|
||||
let gh = (h.max(1.0) / (1.0 - ct - cb).max(0.01)).min(1584.0);
|
||||
let emu = |pt: f32| (pt * 12_700.0).round() as i32;
|
||||
|
||||
let blip_rec = blip.record(emu(gw), emu(gh));
|
||||
let mut fbse = vec![blip.bt(), blip.bt()];
|
||||
fbse.extend_from_slice(&uid(bytes));
|
||||
fbse.extend_from_slice(&0x00FFu16.to_le_bytes()); // tag
|
||||
fbse.extend_from_slice(&(blip_rec.len() as u32).to_le_bytes());
|
||||
fbse.extend_from_slice(&1u32.to_le_bytes()); // cRef
|
||||
fbse.extend_from_slice(&0u32.to_le_bytes()); // foDelay: embedded
|
||||
fbse.extend_from_slice(&[0, 0, 0, 0]); // unused1, cbName, unused2, unused3
|
||||
fbse.extend_from_slice(&blip_rec);
|
||||
let fbse = rec(2, u16::from(blip.bt()), FBSE, &fbse);
|
||||
|
||||
// The shape: an FSP (spid, fHaveAnchor | fHaveSpt) and its properties.
|
||||
let mut fsp = 1025u32.to_le_bytes().to_vec();
|
||||
fsp.extend_from_slice(&0x0A00u32.to_le_bytes());
|
||||
let fsp = rec(2, SPT_PICTURE_FRAME, FSP, &fsp);
|
||||
let mut props: Vec<(u16, u32)> = Vec::new();
|
||||
for (pid, f) in [(0x0100u16, ct), (0x0101, cb), (0x0102, cl), (0x0103, cr)] {
|
||||
if f > 0.0 {
|
||||
props.push((pid, fixed(f)));
|
||||
}
|
||||
}
|
||||
props.push((0x4104, 1)); // pib, fBid: the first (only) store entry
|
||||
let alt_units: Vec<u16> = alt.encode_utf16().take(4095).chain(std::iter::once(0)).collect();
|
||||
let has_alt = alt_units.len() > 1;
|
||||
if has_alt {
|
||||
props.push((0x8381, (alt_units.len() * 2) as u32)); // wzDescription, complex
|
||||
}
|
||||
let mut fopt = Vec::new();
|
||||
for (pid, v) in &props {
|
||||
fopt.extend_from_slice(&pid.to_le_bytes());
|
||||
fopt.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
if has_alt {
|
||||
for u in &alt_units {
|
||||
fopt.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
}
|
||||
let fopt = rec(3, props.len() as u16, FOPT, &fopt);
|
||||
let mut sp = fsp;
|
||||
sp.extend_from_slice(&fopt);
|
||||
let sp = rec(0xF, 0, SP_CONTAINER, &sp);
|
||||
|
||||
let lcb = usize::from(CB_HEADER) + sp.len() + fbse.len();
|
||||
let at = self.data.len();
|
||||
if at.checked_add(lcb)? > MAX_DATA {
|
||||
return None;
|
||||
}
|
||||
let mut picf = vec![0u8; usize::from(CB_HEADER)];
|
||||
picf[0..4].copy_from_slice(&(lcb as u32).to_le_bytes());
|
||||
picf[4..6].copy_from_slice(&CB_HEADER.to_le_bytes());
|
||||
picf[6..8].copy_from_slice(&MM_SHAPE.to_le_bytes());
|
||||
// PICMID: dxaGoal, dyaGoal, mx, my (1000 = 100 %).
|
||||
picf[28..30].copy_from_slice(&super::grpprl::twips(gw).to_le_bytes());
|
||||
picf[30..32].copy_from_slice(&super::grpprl::twips(gh).to_le_bytes());
|
||||
picf[32..34].copy_from_slice(&1000u16.to_le_bytes());
|
||||
picf[34..36].copy_from_slice(&1000u16.to_le_bytes());
|
||||
self.data.extend_from_slice(&picf);
|
||||
self.data.extend_from_slice(&sp);
|
||||
self.data.extend_from_slice(&fbse);
|
||||
u32::try_from(at).ok()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,735 @@
|
||||
//! The text of every story, as Word's character stream: paragraphs and table cells end with
|
||||
//! their marks, fields become begin/separator/end characters, pictures and note references
|
||||
//! special characters, each character carrying its CHPX and each paragraph its PAPX
|
||||
//! ([MS-DOC] §2.4.1 Retrieving Text, §2.4.3 Table Text).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_wordmodel::para::{InlineObject, NoteKind, Paragraph};
|
||||
use craft_wordmodel::props::{CharProps, ParaProps};
|
||||
use craft_wordmodel::section::SectionProps;
|
||||
use craft_wordmodel::table::Table;
|
||||
use craft_wordmodel::{Block, Document};
|
||||
|
||||
use super::grpprl::{self, CharCtx, Grpprl, ParaCtx};
|
||||
use super::picture::Pictures;
|
||||
use super::tables;
|
||||
|
||||
/// Paragraph mark, cell or row end, section mark and the other characters Word gives meaning.
|
||||
pub(crate) const PARA: u16 = 0x0D;
|
||||
pub(crate) const CELL: u16 = 0x07;
|
||||
pub(crate) const SECTION: u16 = 0x0C;
|
||||
const FIELD_BEGIN: u16 = 0x13;
|
||||
const FIELD_SEP: u16 = 0x14;
|
||||
const FIELD_END: u16 = 0x15;
|
||||
const NOTE_REF: u16 = 0x02;
|
||||
const PICTURE: u16 = 0x01;
|
||||
|
||||
/// Deepest field nesting written; deeper fields keep their result text only.
|
||||
const MAX_FIELD_DEPTH: usize = 20;
|
||||
|
||||
/// One paragraph's mark: the CP after it, the paragraph's style and PAPX grpprl.
|
||||
pub(crate) struct ParaEnd {
|
||||
pub(crate) end_cp: u32,
|
||||
pub(crate) istd: u16,
|
||||
pub(crate) grpprl: Arc<Vec<u8>>,
|
||||
}
|
||||
|
||||
/// One field character ([MS-DOC] §2.9.84 Fld): its CP (absolute) and its two bytes.
|
||||
pub(crate) struct FieldChar {
|
||||
pub(crate) cp: u32,
|
||||
pub(crate) fld: [u8; 2],
|
||||
}
|
||||
|
||||
/// A note reference in the main text: its CP, kind, story (part id) and whether it is numbered
|
||||
/// automatically.
|
||||
pub(crate) struct NoteRefAt {
|
||||
pub(crate) cp: u32,
|
||||
pub(crate) kind: NoteKind,
|
||||
pub(crate) part: u32,
|
||||
pub(crate) auto: bool,
|
||||
}
|
||||
|
||||
/// What the writer needs to look up while building the text.
|
||||
pub(crate) struct Lookup<'d> {
|
||||
pub(crate) doc: &'d Document,
|
||||
/// Style id → istd.
|
||||
pub(crate) styles: HashMap<String, u16>,
|
||||
/// The istd of `Normal`.
|
||||
pub(crate) normal: u16,
|
||||
/// Model num id → 1-based LFO index.
|
||||
pub(crate) lfos: HashMap<u32, u16>,
|
||||
/// Font names in table order and their indexes.
|
||||
pub(crate) fonts: Vec<String>,
|
||||
font_index: HashMap<String, u16>,
|
||||
/// Revision authors (`SttbfRMark`, index 0 = "Unknown") and their indexes.
|
||||
pub(crate) authors: Vec<String>,
|
||||
}
|
||||
|
||||
impl<'d> Lookup<'d> {
|
||||
pub(crate) fn new(doc: &'d Document, styles: HashMap<String, u16>, normal: u16, lfos: HashMap<u32, u16>) -> Self {
|
||||
Lookup { doc, styles, normal, lfos, fonts: Vec::new(), font_index: HashMap::new(), authors: vec!["Unknown".to_string()] }
|
||||
}
|
||||
}
|
||||
|
||||
impl CharCtx for Lookup<'_> {
|
||||
fn font(&mut self, name: &str) -> u16 {
|
||||
let name = name.trim();
|
||||
if let Some(&i) = self.font_index.get(name) {
|
||||
return i;
|
||||
}
|
||||
// The font table keeps names Word can store (it counts them in 16-bit units).
|
||||
let i = u16::try_from(self.fonts.len()).unwrap_or(0).min(0x7FFF);
|
||||
if self.fonts.len() < 0x7FFF {
|
||||
self.fonts.push(name.chars().take(31).collect());
|
||||
self.font_index.insert(name.to_string(), i);
|
||||
}
|
||||
i
|
||||
}
|
||||
|
||||
fn char_style(&self, id: &str) -> Option<u16> {
|
||||
let st = self.doc.styles.get(id)?;
|
||||
(st.kind == craft_wordmodel::styles::StyleKind::Character).then(|| self.styles.get(id).copied()).flatten()
|
||||
}
|
||||
|
||||
fn revision(&mut self, rev: u32) -> Option<(u16, u32)> {
|
||||
let r = self.doc.revisions.get(rev as usize)?;
|
||||
let author = if r.author.trim().is_empty() { "Unknown" } else { r.author.trim() };
|
||||
let ibst = match self.authors.iter().position(|a| a == author) {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
self.authors.push(author.to_string());
|
||||
self.authors.len() - 1
|
||||
}
|
||||
};
|
||||
Some((u16::try_from(ibst).unwrap_or(0), super::dttm(&r.date)))
|
||||
}
|
||||
}
|
||||
|
||||
impl ParaCtx for Lookup<'_> {
|
||||
fn lfo(&self, num: u32) -> u16 {
|
||||
self.lfos.get(&num).copied().unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Grpprls are shared: a run or paragraph names one by its index.
|
||||
#[derive(Default)]
|
||||
struct Interned {
|
||||
all: Vec<Arc<Vec<u8>>>,
|
||||
index: HashMap<Arc<Vec<u8>>, usize>,
|
||||
}
|
||||
|
||||
impl Interned {
|
||||
fn get(&mut self, g: Vec<u8>) -> usize {
|
||||
if let Some(&i) = self.index.get(&g) {
|
||||
return i;
|
||||
}
|
||||
let g = Arc::new(g);
|
||||
self.all.push(g.clone());
|
||||
self.index.insert(g, self.all.len() - 1);
|
||||
self.all.len() - 1
|
||||
}
|
||||
}
|
||||
|
||||
/// Counts of content the binary format (as written here) leaves out, logged once per save.
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct Dropped {
|
||||
pub(crate) shapes: usize,
|
||||
pub(crate) comments: usize,
|
||||
pub(crate) nested_tables: usize,
|
||||
pub(crate) floating_pictures: usize,
|
||||
pub(crate) pictures: usize,
|
||||
pub(crate) equations: usize,
|
||||
}
|
||||
|
||||
/// The character stream of every story, with what each character and paragraph carries.
|
||||
pub(crate) struct Text<'d> {
|
||||
pub(crate) look: Lookup<'d>,
|
||||
pub(crate) units: Vec<u16>,
|
||||
/// Run starts: (CP, interned CHPX), each run up to the next start.
|
||||
runs: Vec<(u32, usize)>,
|
||||
chpx: Interned,
|
||||
/// CHPX → the same with `sprmCFSpec`, for field characters.
|
||||
spec: HashMap<usize, usize>,
|
||||
pub(crate) paras: Vec<ParaEnd>,
|
||||
/// Field characters of the subdocument being written, then of each finished one.
|
||||
pub(crate) fields: Vec<FieldChar>,
|
||||
/// Bookmarks: name, start CP, end CP (absolute), in the order they start.
|
||||
pub(crate) bookmarks: Vec<(String, u32, Option<u32>)>,
|
||||
pub(crate) notes: Vec<NoteRefAt>,
|
||||
/// Section ends in the main text: the CP after each section mark, and its properties.
|
||||
pub(crate) sections: Vec<(u32, SectionProps)>,
|
||||
pub(crate) pictures: Pictures,
|
||||
pub(crate) dropped: Dropped,
|
||||
/// Fields begun with a `FieldStart` and not yet ended (their nesting depth counts here).
|
||||
open_fields: usize,
|
||||
/// The note story being written: its own reference mark is its leading 0x02.
|
||||
note: Option<(NoteKind, u32)>,
|
||||
/// Inside a table cell: section marks can't end a cell's paragraph.
|
||||
in_cell: bool,
|
||||
}
|
||||
|
||||
impl<'d> Text<'d> {
|
||||
pub(crate) fn new(look: Lookup<'d>) -> Self {
|
||||
Text {
|
||||
look,
|
||||
units: Vec::new(),
|
||||
runs: Vec::new(),
|
||||
chpx: Interned::default(),
|
||||
spec: HashMap::new(),
|
||||
paras: Vec::new(),
|
||||
fields: Vec::new(),
|
||||
bookmarks: Vec::new(),
|
||||
notes: Vec::new(),
|
||||
sections: Vec::new(),
|
||||
pictures: Pictures::default(),
|
||||
dropped: Dropped::default(),
|
||||
open_fields: 0,
|
||||
note: None,
|
||||
in_cell: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The CP the next character gets.
|
||||
pub(crate) fn cp(&self) -> u32 {
|
||||
u32::try_from(self.units.len()).unwrap_or(u32::MAX)
|
||||
}
|
||||
|
||||
/// The character runs: (start CP, end CP, CHPX grpprl).
|
||||
pub(crate) fn char_runs(&self) -> Vec<(u32, u32, &[u8])> {
|
||||
let end = self.cp();
|
||||
self.runs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &(cp, g))| {
|
||||
let next = self.runs.get(i + 1).map_or(end, |r| r.0);
|
||||
(cp, next, self.chpx.all.get(g).map_or(&[][..], |v| v.as_slice()))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn emit(&mut self, unit: u16, chpx: usize) {
|
||||
let cp = self.cp();
|
||||
if self.runs.last().is_none_or(|&(_, g)| g != chpx) {
|
||||
self.runs.push((cp, chpx));
|
||||
}
|
||||
self.units.push(unit);
|
||||
}
|
||||
|
||||
/// The interned CHPX of `props`, plus `extra` sprms.
|
||||
fn chpx_of(&mut self, props: &CharProps, extra: Option<&Grpprl>) -> usize {
|
||||
let mut g = grpprl::chpx(props, &mut self.look);
|
||||
if let Some(e) = extra {
|
||||
g.extend(e);
|
||||
}
|
||||
g.truncate_to(255);
|
||||
self.chpx.get(g.0)
|
||||
}
|
||||
|
||||
fn text(&mut self, s: &str, chpx: usize) {
|
||||
for c in s.chars() {
|
||||
self.char(c, chpx);
|
||||
}
|
||||
}
|
||||
|
||||
/// One character of text, mapped to what Word stores for it.
|
||||
fn char(&mut self, c: char, chpx: usize) {
|
||||
let unit = match c {
|
||||
'\n' => 0x0B,
|
||||
'\t' => 0x09,
|
||||
'\u{C}' => 0x0C,
|
||||
'\u{E}' => 0x0E,
|
||||
'\u{2011}' => 0x1E,
|
||||
'\u{AD}' => 0x1F,
|
||||
'\r' | '\u{FFFC}' => return,
|
||||
c if (c as u32) < 0x20 => return,
|
||||
c => {
|
||||
let mut buf = [0u16; 2];
|
||||
for u in c.encode_utf16(&mut buf).iter() {
|
||||
self.emit(*u, chpx);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.emit(unit, chpx);
|
||||
}
|
||||
|
||||
/// A field character: the text's formatting with `sprmCFSpec` on, as every field character
|
||||
/// MUST have ([MS-DOC] §2.8.25).
|
||||
fn field_char(&mut self, ch: u16, second: u8, chpx: usize) {
|
||||
let spec = match self.spec.get(&chpx) {
|
||||
Some(&s) => s,
|
||||
None => {
|
||||
let mut g = Grpprl(self.chpx.all.get(chpx).map_or_else(Vec::new, |g| g.as_ref().clone()));
|
||||
g.truncate_to(252);
|
||||
g.u8(grpprl::C_SPEC, 1);
|
||||
let s = self.chpx.get(g.0);
|
||||
self.spec.insert(chpx, s);
|
||||
s
|
||||
}
|
||||
};
|
||||
self.fields.push(FieldChar { cp: self.cp(), fld: [ch as u8, second] });
|
||||
self.emit(ch, spec);
|
||||
}
|
||||
|
||||
/// A whole field: begin, its code, separator and result, end.
|
||||
fn field(&mut self, instr: &str, result: Option<&str>, locked: bool, dirty: bool, chpx: usize) {
|
||||
if self.open_fields >= MAX_FIELD_DEPTH {
|
||||
if let Some(r) = result {
|
||||
self.text(r, chpx);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.field_char(FIELD_BEGIN, flt(instr), chpx);
|
||||
self.text(&format!(" {} ", instr.trim()), chpx);
|
||||
if let Some(r) = result {
|
||||
self.field_char(FIELD_SEP, 0, chpx);
|
||||
self.text(r, chpx);
|
||||
}
|
||||
self.field_char(FIELD_END, end_flags(result.is_some(), locked, dirty, self.open_fields > 0), chpx);
|
||||
}
|
||||
|
||||
/// Write `blocks` as a story's paragraphs and tables.
|
||||
pub(crate) fn blocks(&mut self, blocks: &[Arc<Block>], top: bool) {
|
||||
for b in blocks {
|
||||
match &**b {
|
||||
Block::Para(p) => self.paragraph(p, top, None),
|
||||
Block::Table(t) => self.table(t),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a note's story, its reference mark first.
|
||||
pub(crate) fn note_story(&mut self, kind: NoteKind, part: u32, blocks: &[Arc<Block>]) {
|
||||
let own_mark = blocks
|
||||
.first()
|
||||
.and_then(|b| b.as_para())
|
||||
.is_some_and(|p| p.objects.iter().any(|o| matches!(o, InlineObject::NoteRef { kind: k, id, .. } if *k == kind && *id == part)));
|
||||
self.note = Some((kind, part));
|
||||
if !own_mark {
|
||||
let style = self.note_ref_style(kind);
|
||||
let chpx = self.special(&style);
|
||||
self.emit(NOTE_REF, chpx);
|
||||
}
|
||||
if blocks.is_empty() {
|
||||
self.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
}
|
||||
self.blocks(blocks, false);
|
||||
self.note = None;
|
||||
}
|
||||
|
||||
/// A character style for note references ("footnote reference"), when the document has it.
|
||||
fn note_ref_style(&self, kind: NoteKind) -> CharProps {
|
||||
let id = match kind {
|
||||
NoteKind::Footnote => "FootnoteReference",
|
||||
NoteKind::Endnote => "EndnoteReference",
|
||||
};
|
||||
let style = self.look.styles.contains_key(id).then(|| id.to_string());
|
||||
CharProps { style, ..Default::default() }
|
||||
}
|
||||
|
||||
/// The CHPX of a special character (a note reference, a picture): `sprmCFSpec` on.
|
||||
fn special(&mut self, props: &CharProps) -> usize {
|
||||
let mut extra = Grpprl::default();
|
||||
extra.u8(grpprl::C_SPEC, 1);
|
||||
self.chpx_of(props, Some(&extra))
|
||||
}
|
||||
|
||||
/// One paragraph: its text, then its mark (`mark` overrides the paragraph mark, for cells;
|
||||
/// a paragraph ending a main-text section takes a section mark).
|
||||
pub(crate) fn paragraph(&mut self, p: &Paragraph, top: bool, cell: Option<(u16, &Grpprl)>) {
|
||||
let mut objects = p.objects.iter();
|
||||
let mut link: Option<(String, usize)> = None;
|
||||
let mut at = 0usize;
|
||||
for run in &p.runs {
|
||||
let end = at.saturating_add(run.len).min(p.text.len());
|
||||
let Some(s) = p.text.get(at..end) else { break };
|
||||
at = end;
|
||||
if s.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// A run's link becomes a HYPERLINK field around the runs that share it.
|
||||
let mut props = run.props.clone();
|
||||
let target = props.link.take();
|
||||
if link.as_ref().map(|l| &l.0) != target.as_ref() {
|
||||
if let Some((_, chpx)) = link.take() {
|
||||
self.field_char(FIELD_END, end_flags(true, false, false, self.open_fields > 0), chpx);
|
||||
}
|
||||
if let Some(t) = &target {
|
||||
let chpx = self.chpx_of(&props, None);
|
||||
self.field_char(FIELD_BEGIN, FLT_HYPERLINK, chpx);
|
||||
self.text(&format!(" {} ", hyperlink_instr(t)), chpx);
|
||||
self.field_char(FIELD_SEP, 0, chpx);
|
||||
link = Some((t.clone(), chpx));
|
||||
}
|
||||
}
|
||||
let chpx = self.chpx_of(&props, None);
|
||||
for c in s.chars() {
|
||||
if c == '\u{FFFC}' {
|
||||
if let Some(o) = objects.next() {
|
||||
self.object(o, &props, chpx);
|
||||
}
|
||||
} else {
|
||||
self.char(c, chpx);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some((_, chpx)) = link.take() {
|
||||
self.field_char(FIELD_END, end_flags(true, false, false, self.open_fields > 0), chpx);
|
||||
}
|
||||
match cell {
|
||||
Some((mark, extra)) => self.mark(p, mark, extra),
|
||||
None => {
|
||||
let section = top && !self.in_cell && p.section.is_some();
|
||||
self.mark(p, if section { SECTION } else { PARA }, &Grpprl::default());
|
||||
if section && let Some(s) = &p.section {
|
||||
self.sections.push((self.cp(), (**s).clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The paragraph mark: its character formatting is the paragraph's mark formatting; the
|
||||
/// PAPX carries the style and paragraph properties (plus `extra`, for table cells).
|
||||
pub(crate) fn mark(&mut self, p: &Paragraph, mark: u16, extra: &Grpprl) {
|
||||
let chpx = self.chpx_of(&p.mark, None);
|
||||
self.emit(mark, chpx);
|
||||
let (istd, g) = self.papx_of(&p.props, extra);
|
||||
self.paras.push(ParaEnd { end_cp: self.cp(), istd, grpprl: g });
|
||||
}
|
||||
|
||||
pub(crate) fn papx_of(&mut self, props: &ParaProps, extra: &Grpprl) -> (u16, Arc<Vec<u8>>) {
|
||||
let istd = props.style.as_deref().and_then(|s| self.look.styles.get(s).copied()).unwrap_or(self.look.normal);
|
||||
let mut g = grpprl::papx(props, &self.look);
|
||||
g.extend(extra);
|
||||
g.truncate_to(super::fkp::MAX_PAPX_GRPPRL);
|
||||
(istd, Arc::new(g.0))
|
||||
}
|
||||
|
||||
fn object(&mut self, o: &InlineObject, props: &CharProps, chpx: usize) {
|
||||
match o {
|
||||
InlineObject::Field { instr, result, locked, dirty, .. } => self.field(instr, Some(result), *locked, *dirty, chpx),
|
||||
InlineObject::FieldStart { instr, locked, dirty } => {
|
||||
if self.open_fields >= MAX_FIELD_DEPTH {
|
||||
self.open_fields += 1;
|
||||
return;
|
||||
}
|
||||
self.field_char(FIELD_BEGIN, flt(instr), chpx);
|
||||
self.text(&format!(" {} ", instr.trim()), chpx);
|
||||
self.field_char(FIELD_SEP, 0, chpx);
|
||||
// The end's flags are decided now: they don't depend on the result.
|
||||
self.open_fields += 1;
|
||||
let _ = (locked, dirty);
|
||||
}
|
||||
InlineObject::FieldEnd => {
|
||||
if self.open_fields == 0 {
|
||||
return;
|
||||
}
|
||||
self.open_fields -= 1;
|
||||
if self.open_fields < MAX_FIELD_DEPTH {
|
||||
self.field_char(FIELD_END, end_flags(true, false, false, self.open_fields > 0), chpx);
|
||||
}
|
||||
}
|
||||
InlineObject::NoteRef { kind, id, custom } => {
|
||||
if self.note == Some((*kind, *id)) {
|
||||
let c = self.special(props);
|
||||
self.emit(NOTE_REF, c);
|
||||
} else if self.note.is_none() {
|
||||
let cp = self.cp();
|
||||
let c = self.special(props);
|
||||
self.emit(NOTE_REF, c);
|
||||
self.notes.push(NoteRefAt { cp, kind: *kind, part: *id, auto: custom.is_empty() });
|
||||
}
|
||||
}
|
||||
InlineObject::BookmarkStart { name } => {
|
||||
if !name.is_empty() && self.bookmarks.iter().all(|(n, _, _)| n != name) {
|
||||
self.bookmarks.push((name.clone(), self.cp(), None));
|
||||
}
|
||||
}
|
||||
InlineObject::BookmarkEnd { name } => {
|
||||
let cp = self.cp();
|
||||
if let Some(b) = self.bookmarks.iter_mut().find(|(n, _, e)| n == name && e.is_none()) {
|
||||
b.2 = Some(cp);
|
||||
}
|
||||
}
|
||||
InlineObject::Image { media, w, h, crop, float, alt, .. } => {
|
||||
if float.wrap != craft_wordmodel::para::Wrap::Inline {
|
||||
self.dropped.floating_pictures += 1;
|
||||
}
|
||||
let Some(bytes) = self.look.doc.media.get(media).cloned() else {
|
||||
self.dropped.pictures += 1;
|
||||
return;
|
||||
};
|
||||
match self.pictures.add(&bytes, *w, *h, *crop, alt) {
|
||||
Some(fc) => {
|
||||
let mut extra = Grpprl::default();
|
||||
extra.u8(grpprl::C_SPEC, 1);
|
||||
extra.u32(grpprl::C_PIC_LOCATION, fc);
|
||||
let c = self.chpx_of(props, Some(&extra));
|
||||
self.emit(PICTURE, c);
|
||||
}
|
||||
None => self.dropped.pictures += 1,
|
||||
}
|
||||
}
|
||||
InlineObject::Equation { linear, .. } => {
|
||||
self.dropped.equations += 1;
|
||||
self.text(linear, chpx);
|
||||
}
|
||||
InlineObject::Ruby { base, .. } => self.text(base, chpx),
|
||||
InlineObject::PositionalTab { .. } => self.emit(0x09, chpx),
|
||||
InlineObject::Opaque { text, .. } => self.text(text, chpx),
|
||||
InlineObject::CommentStart { .. } => self.dropped.comments += 1,
|
||||
InlineObject::Shape { .. } | InlineObject::Group { .. } | InlineObject::Graphic { .. } => self.dropped.shapes += 1,
|
||||
InlineObject::CommentEnd { .. }
|
||||
| InlineObject::ControlStart { .. }
|
||||
| InlineObject::ControlEnd
|
||||
| InlineObject::PermStart { .. }
|
||||
| InlineObject::PermEnd { .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// End fields left open at the end of a story, so every begin has its end; the ends get a
|
||||
/// paragraph of their own, as the story must end with a mark.
|
||||
pub(crate) fn close_fields(&mut self) {
|
||||
let mut wrote = false;
|
||||
while self.open_fields > 0 {
|
||||
self.open_fields -= 1;
|
||||
if self.open_fields < MAX_FIELD_DEPTH {
|
||||
let chpx = self.chpx.get(Vec::new());
|
||||
self.field_char(FIELD_END, end_flags(true, false, false, self.open_fields > 0), chpx);
|
||||
wrote = true;
|
||||
}
|
||||
}
|
||||
if wrote {
|
||||
self.mark(&Paragraph::new(), PARA, &Grpprl::default());
|
||||
}
|
||||
}
|
||||
|
||||
/// A table: each cell's paragraphs end with a cell mark, each row with a row-end mark that
|
||||
/// carries the row's table properties ([MS-DOC] §2.4.3).
|
||||
fn table(&mut self, t: &Table) {
|
||||
let was = self.in_cell;
|
||||
self.in_cell = true;
|
||||
let rows = t.rows.len();
|
||||
let style_borders = t.props.style.as_deref().and_then(|s| self.look.doc.styles.get(s)).and_then(|s| s.table.as_ref()).and_then(|p| p.borders);
|
||||
for (ri, row) in t.rows.iter().enumerate() {
|
||||
// A row has at most 63 cells: the text of any past them goes in the last one.
|
||||
let last = row.cells.len().min(tables::MAX_CELLS).saturating_sub(1);
|
||||
let mut in_table = Grpprl::default();
|
||||
grpprl::in_table(&mut in_table, false);
|
||||
for (ci, cell) in row.cells.iter().enumerate() {
|
||||
let mut paras: Vec<&Paragraph> = Vec::new();
|
||||
let mut owned: Vec<Paragraph> = Vec::new();
|
||||
flatten(&cell.blocks, &mut paras, &mut owned, &mut self.dropped.nested_tables);
|
||||
let all: Vec<&Paragraph> = if owned.is_empty() { paras } else { owned.iter().collect() };
|
||||
// Whether this cell's last paragraph ends the (possibly merged) cell.
|
||||
let ends_cell = ci < last || ci + 1 == row.cells.len();
|
||||
if all.is_empty() {
|
||||
let mark = if ends_cell { CELL } else { PARA };
|
||||
self.paragraph(&Paragraph::new(), false, Some((mark, &in_table)));
|
||||
continue;
|
||||
}
|
||||
for (i, p) in all.iter().enumerate() {
|
||||
let mark = if ends_cell && i + 1 == all.len() { CELL } else { PARA };
|
||||
self.paragraph(p, false, Some((mark, &in_table)));
|
||||
}
|
||||
}
|
||||
let tapx = tables::row_end(t, ri, rows, style_borders);
|
||||
let mut extra = Grpprl::default();
|
||||
grpprl::in_table(&mut extra, true);
|
||||
extra.extend(&tapx);
|
||||
self.mark(&Paragraph::new(), CELL, &extra);
|
||||
}
|
||||
self.in_cell = was;
|
||||
}
|
||||
}
|
||||
|
||||
/// A cell's paragraphs, nested tables flattened into their own paragraphs (the writer keeps one
|
||||
/// level of tables). When there is nothing to flatten `paras` borrows; else `owned` holds all.
|
||||
fn flatten<'a>(blocks: &'a [Arc<Block>], paras: &mut Vec<&'a Paragraph>, owned: &mut Vec<Paragraph>, nested: &mut usize) {
|
||||
if blocks.iter().all(|b| b.as_para().is_some()) {
|
||||
paras.extend(blocks.iter().filter_map(|b| b.as_para()));
|
||||
return;
|
||||
}
|
||||
*nested += 1;
|
||||
fn walk(blocks: &[Arc<Block>], out: &mut Vec<Paragraph>, depth: usize) {
|
||||
for b in blocks {
|
||||
match &**b {
|
||||
Block::Para(p) => out.push(p.clone()),
|
||||
Block::Table(t) if depth < 16 => {
|
||||
for row in &t.rows {
|
||||
for c in &row.cells {
|
||||
walk(&c.blocks, out, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Block::Table(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(blocks, owned, 0);
|
||||
}
|
||||
|
||||
/// `flt` of a HYPERLINK field.
|
||||
const FLT_HYPERLINK: u8 = 88;
|
||||
|
||||
/// The field type (`flt`, [MS-DOC] §2.9.87) of a field code, from its first word; 0x01 (an
|
||||
/// unknown keyword) for anything not in the table, which Word reparses from the code anyway.
|
||||
fn flt(instr: &str) -> u8 {
|
||||
let word = instr.split_whitespace().next().unwrap_or("").trim_start_matches('=').to_ascii_uppercase();
|
||||
if instr.trim_start().starts_with('=') {
|
||||
return 34;
|
||||
}
|
||||
match word.as_str() {
|
||||
"REF" => 3,
|
||||
"FTNREF" => 5,
|
||||
"SET" => 6,
|
||||
"IF" => 7,
|
||||
"INDEX" => 8,
|
||||
"TC" => 9,
|
||||
"STYLEREF" => 10,
|
||||
"SEQ" => 12,
|
||||
"TOC" => 13,
|
||||
"INFO" => 14,
|
||||
"TITLE" => 15,
|
||||
"SUBJECT" => 16,
|
||||
"AUTHOR" => 17,
|
||||
"KEYWORDS" => 18,
|
||||
"COMMENTS" => 19,
|
||||
"LASTSAVEDBY" => 20,
|
||||
"CREATEDATE" => 21,
|
||||
"SAVEDATE" => 22,
|
||||
"PRINTDATE" => 23,
|
||||
"REVNUM" => 24,
|
||||
"EDITTIME" => 25,
|
||||
"NUMPAGES" => 26,
|
||||
"NUMWORDS" => 27,
|
||||
"NUMCHARS" => 28,
|
||||
"FILENAME" => 29,
|
||||
"TEMPLATE" => 30,
|
||||
"DATE" => 31,
|
||||
"TIME" => 32,
|
||||
"PAGE" => 33,
|
||||
"QUOTE" => 35,
|
||||
"INCLUDE" => 36,
|
||||
"PAGEREF" => 37,
|
||||
"ASK" => 38,
|
||||
"FILLIN" => 39,
|
||||
"DATA" => 40,
|
||||
"NEXT" => 41,
|
||||
"NEXTIF" => 42,
|
||||
"SKIPIF" => 43,
|
||||
"MERGEREC" => 44,
|
||||
"DDE" => 45,
|
||||
"DDEAUTO" => 46,
|
||||
"GLOSSARY" => 47,
|
||||
"PRINT" => 48,
|
||||
"EQ" => 49,
|
||||
"GOTOBUTTON" => 50,
|
||||
"MACROBUTTON" => 51,
|
||||
"AUTONUMOUT" => 52,
|
||||
"AUTONUMLGL" => 53,
|
||||
"AUTONUM" => 54,
|
||||
"IMPORT" => 55,
|
||||
"LINK" => 56,
|
||||
"SYMBOL" => 57,
|
||||
"EMBED" => 58,
|
||||
"MERGEFIELD" => 59,
|
||||
"USERNAME" => 60,
|
||||
"USERINITIALS" => 61,
|
||||
"USERADDRESS" => 62,
|
||||
"BARCODE" => 63,
|
||||
"DOCVARIABLE" => 64,
|
||||
"SECTION" => 65,
|
||||
"SECTIONPAGES" => 66,
|
||||
"INCLUDEPICTURE" => 67,
|
||||
"INCLUDETEXT" => 68,
|
||||
"FILESIZE" => 69,
|
||||
"FORMTEXT" => 70,
|
||||
"FORMCHECKBOX" => 71,
|
||||
"NOTEREF" => 72,
|
||||
"TOA" => 73,
|
||||
"TA" => 74,
|
||||
"MERGESEQ" => 75,
|
||||
"AUTOTEXT" => 79,
|
||||
"COMPARE" => 80,
|
||||
"ADDIN" => 81,
|
||||
"FORMDROPDOWN" => 83,
|
||||
"ADVANCE" => 84,
|
||||
"DOCPROPERTY" => 85,
|
||||
"CONTROL" => 87,
|
||||
"HYPERLINK" => 88,
|
||||
"AUTOTEXTLIST" => 89,
|
||||
"LISTNUM" => 90,
|
||||
"HTMLCONTROL" => 91,
|
||||
"BIDIOUTLINE" => 92,
|
||||
"ADDRESSBLOCK" => 93,
|
||||
"GREETINGLINE" => 94,
|
||||
"SHAPE" => 95,
|
||||
"CITATION" => 96,
|
||||
"BIBLIOGRAPHY" => 97,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// The second byte of a field end ([MS-DOC] §2.9.84 grffldEnd): `fHasSep` when there is a
|
||||
/// result, `fLocked`, `fResultsDirty` and `fNested`.
|
||||
fn end_flags(has_sep: bool, locked: bool, dirty: bool, nested: bool) -> u8 {
|
||||
let mut f = 0u8;
|
||||
if dirty {
|
||||
f |= 0x04;
|
||||
}
|
||||
if locked {
|
||||
f |= 0x10;
|
||||
}
|
||||
if nested {
|
||||
f |= 0x40;
|
||||
}
|
||||
if has_sep {
|
||||
f |= 0x80;
|
||||
}
|
||||
f
|
||||
}
|
||||
|
||||
/// The field code of a link: `HYPERLINK "url"`, `HYPERLINK \l "bookmark"` for an internal
|
||||
/// one, or both for a link into another document's bookmark. A quote or backslash in an
|
||||
/// argument is escaped with a backslash, as field codes do.
|
||||
fn hyperlink_instr(target: &str) -> String {
|
||||
let q = |s: &str| s.replace('\\', "\\\\").replace('"', "\\\"");
|
||||
match target.split_once('#') {
|
||||
Some(("", frag)) => format!("HYPERLINK \\l \"{}\"", q(frag)),
|
||||
Some((url, frag)) if !frag.is_empty() && !url.contains("://") => format!("HYPERLINK \"{}\" \\l \"{}\"", q(url), q(frag)),
|
||||
_ => format!("HYPERLINK \"{}\"", q(target)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn field_types() {
|
||||
assert_eq!(flt("PAGE"), 33);
|
||||
assert_eq!(flt(" hyperlink \"x\""), 88);
|
||||
assert_eq!(flt("= 2 + 2"), 34);
|
||||
assert_eq!(flt("NOSUCHFIELD"), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hyperlink_codes() {
|
||||
assert_eq!(hyperlink_instr("https://example.org/a#b"), "HYPERLINK \"https://example.org/a#b\"");
|
||||
assert_eq!(hyperlink_instr("#Intro"), "HYPERLINK \\l \"Intro\"");
|
||||
assert_eq!(hyperlink_instr("other.doc#Part"), "HYPERLINK \"other.doc\" \\l \"Part\"");
|
||||
assert_eq!(craft_wordmodel::link::hyperlink_target(&hyperlink_instr("#Intro")).as_deref(), Some("#Intro"));
|
||||
assert_eq!(
|
||||
craft_wordmodel::link::hyperlink_target(&hyperlink_instr("https://example.org/?q=\"x\"")).as_deref(),
|
||||
Some("https://example.org/?q=\"x\"")
|
||||
);
|
||||
assert_eq!(
|
||||
craft_wordmodel::link::hyperlink_target(&hyperlink_instr(r"file:///C:\docs\report.doc")).as_deref(),
|
||||
Some(r"file:///C:\docs\report.doc")
|
||||
);
|
||||
assert_eq!(craft_wordmodel::link::hyperlink_target(&hyperlink_instr("#a\"b")).as_deref(), Some("#a\"b"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
//! The stylesheet (`STSH`, [MS-DOC] §2.9.271) and the font table (`SttbfFfn`, §2.9.286).
|
||||
//!
|
||||
//! Istds 0–14 are fixed: 0 Normal, 1–9 heading 1–9, 10 Default Paragraph Font, 11 Normal
|
||||
//! Table, 12 No List, 13–14 unused. Every other style follows from istd 15 in stylesheet
|
||||
//! order. A style Word has built in keeps its `sti` and the name Word gives it, so Word treats
|
||||
//! it as its own; the rest are user styles.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use craft_wordmodel::props::CharProps;
|
||||
use craft_wordmodel::styles::{Style, StyleKind, StyleSheet};
|
||||
|
||||
use super::grpprl::{self, CharCtx, Grpprl, ParaCtx};
|
||||
|
||||
/// Istds reserved for built-in styles.
|
||||
const FIXED: u16 = 15;
|
||||
/// `sti` of a user-defined style.
|
||||
const STI_USER: u16 = 0x0FFE;
|
||||
/// `istdBase` / `istdNext` meaning "none".
|
||||
const ISTD_NIL: u16 = 0x0FFF;
|
||||
/// Most styles written (the format allows 0x0FFE).
|
||||
const MAX_STYLES: usize = 4000;
|
||||
|
||||
/// Built-in styles: the name Word stores (lower-case where Word's is), and the `sti`.
|
||||
const BUILTIN: &[(&str, u16)] = &[
|
||||
("Normal", 0),
|
||||
("heading 1", 1),
|
||||
("heading 2", 2),
|
||||
("heading 3", 3),
|
||||
("heading 4", 4),
|
||||
("heading 5", 5),
|
||||
("heading 6", 6),
|
||||
("heading 7", 7),
|
||||
("heading 8", 8),
|
||||
("heading 9", 9),
|
||||
("toc 1", 19),
|
||||
("toc 2", 20),
|
||||
("toc 3", 21),
|
||||
("toc 4", 22),
|
||||
("toc 5", 23),
|
||||
("toc 6", 24),
|
||||
("toc 7", 25),
|
||||
("toc 8", 26),
|
||||
("toc 9", 27),
|
||||
("Normal Indent", 28),
|
||||
("footnote text", 29),
|
||||
("annotation text", 30),
|
||||
("header", 31),
|
||||
("footer", 32),
|
||||
("index heading", 33),
|
||||
("caption", 34),
|
||||
("table of figures", 35),
|
||||
("footnote reference", 38),
|
||||
("annotation reference", 39),
|
||||
("line number", 40),
|
||||
("page number", 41),
|
||||
("endnote reference", 42),
|
||||
("endnote text", 43),
|
||||
("List", 47),
|
||||
("List Bullet", 48),
|
||||
("List Number", 49),
|
||||
("Title", 62),
|
||||
("Signature", 64),
|
||||
("Default Paragraph Font", 65),
|
||||
("Body Text", 66),
|
||||
("Body Text Indent", 67),
|
||||
("List Continue", 68),
|
||||
("Subtitle", 74),
|
||||
("Salutation", 75),
|
||||
("Date", 76),
|
||||
("Hyperlink", 85),
|
||||
("FollowedHyperlink", 86),
|
||||
("Strong", 87),
|
||||
("Emphasis", 88),
|
||||
("Plain Text", 91),
|
||||
("Normal (Web)", 94),
|
||||
("Normal Table", 105),
|
||||
("No List", 107),
|
||||
("Balloon Text", 153),
|
||||
("Table Grid", 154),
|
||||
("Placeholder Text", 156),
|
||||
("No Spacing", 157),
|
||||
("List Paragraph", 179),
|
||||
("Quote", 180),
|
||||
("Intense Quote", 181),
|
||||
("Subtle Emphasis", 260),
|
||||
("Intense Emphasis", 261),
|
||||
("Subtle Reference", 262),
|
||||
("Intense Reference", 263),
|
||||
("Book Title", 264),
|
||||
("Bibliography", 265),
|
||||
("TOC Heading", 266),
|
||||
];
|
||||
|
||||
/// The built-in style a model style is, by its name or id (`Heading 1`, `Heading1` and
|
||||
/// `heading 1` are the same), with the name Word stores.
|
||||
fn builtin(st: &Style) -> Option<(&'static str, u16)> {
|
||||
let norm = |s: &str| s.chars().filter(|c| !c.is_whitespace()).collect::<String>().to_ascii_lowercase();
|
||||
let (name, id) = (norm(&st.name), norm(&st.id));
|
||||
// The docx reader's ids for a few styles differ from their names.
|
||||
let alias = |s: &str| match s {
|
||||
"tablenormal" => "normaltable".to_string(),
|
||||
"commenttext" => "annotationtext".to_string(),
|
||||
"commentreference" => "annotationreference".to_string(),
|
||||
other => other.to_string(),
|
||||
};
|
||||
BUILTIN
|
||||
.iter()
|
||||
.find(|(n, _)| {
|
||||
let n = norm(n);
|
||||
n == alias(&name) || n == alias(&id)
|
||||
})
|
||||
.copied()
|
||||
}
|
||||
|
||||
/// The stylesheet as written: each model style's istd.
|
||||
pub(crate) struct Plan {
|
||||
/// Model style id → istd.
|
||||
pub(crate) istd: HashMap<String, u16>,
|
||||
/// Istd → model style (index into the stylesheet), `None` for an empty slot.
|
||||
slots: Vec<Option<usize>>,
|
||||
/// Istd → the name and sti it is written with.
|
||||
names: Vec<(String, u16)>,
|
||||
}
|
||||
|
||||
/// Assign istds: the fixed built-ins to their slots, everything else after them.
|
||||
pub(crate) fn plan(sheet: &StyleSheet) -> Plan {
|
||||
let mut slots: Vec<Option<usize>> = vec![None; FIXED as usize];
|
||||
let mut names: Vec<(String, u16)> = vec![(String::new(), 0); FIXED as usize];
|
||||
let mut istd = HashMap::new();
|
||||
let mut taken = std::collections::HashSet::new();
|
||||
for (i, st) in sheet.styles.iter().enumerate().take(MAX_STYLES) {
|
||||
if st.id.is_empty() || istd.contains_key(&st.id) {
|
||||
continue;
|
||||
}
|
||||
let bi = builtin(st);
|
||||
// The fixed slot a built-in style takes, when its kind is the one Word gives it.
|
||||
let fixed = bi.and_then(|(_, sti)| match (sti, st.kind) {
|
||||
(0..=9, StyleKind::Paragraph) => Some(sti),
|
||||
(65, StyleKind::Character) => Some(10),
|
||||
(105, StyleKind::Table) => Some(11),
|
||||
(107, StyleKind::Numbering) => Some(12),
|
||||
_ => None,
|
||||
});
|
||||
let slot = match fixed {
|
||||
Some(s) if !taken.contains(&s) => s,
|
||||
_ => {
|
||||
slots.push(None);
|
||||
names.push((String::new(), 0));
|
||||
(slots.len() - 1) as u16
|
||||
}
|
||||
};
|
||||
taken.insert(slot);
|
||||
if let Some(s) = slots.get_mut(slot as usize) {
|
||||
*s = Some(i);
|
||||
}
|
||||
// A built-in name is Word's own spelling; its sti only where the kind matches.
|
||||
let (name, sti) = match bi {
|
||||
Some((n, sti)) if builtin_kind(sti) == st.kind => (n.to_string(), sti),
|
||||
_ => (st.name.clone(), STI_USER),
|
||||
};
|
||||
if let Some(n) = names.get_mut(slot as usize) {
|
||||
*n = (name, sti);
|
||||
}
|
||||
istd.insert(st.id.clone(), slot);
|
||||
}
|
||||
// Normal and Default Paragraph Font are always there.
|
||||
if slots.first().is_some_and(Option::is_none)
|
||||
&& let Some(n) = names.get_mut(0)
|
||||
{
|
||||
*n = ("Normal".into(), 0);
|
||||
}
|
||||
for (i, name, sti) in [(10usize, "Default Paragraph Font", 65u16), (11, "Normal Table", 105), (12, "No List", 107)] {
|
||||
if slots.get(i).is_some_and(Option::is_none)
|
||||
&& let Some(n) = names.get_mut(i)
|
||||
{
|
||||
*n = (name.into(), sti);
|
||||
}
|
||||
}
|
||||
Plan { istd, slots, names }
|
||||
}
|
||||
|
||||
/// The kind of style Word's built-in `sti` is.
|
||||
fn builtin_kind(sti: u16) -> StyleKind {
|
||||
match sti {
|
||||
38 | 39 | 40 | 41 | 42 | 65 | 85 | 86 | 87 | 88 | 156 | 260..=264 => StyleKind::Character,
|
||||
105 | 154 => StyleKind::Table,
|
||||
107 => StyleKind::Numbering,
|
||||
_ => StyleKind::Paragraph,
|
||||
}
|
||||
}
|
||||
|
||||
/// The STSH bytes. `default_font` is the font-table index of the document's default font
|
||||
/// (the standard font of the stylesheet header).
|
||||
pub(crate) fn stsh(sheet: &StyleSheet, plan: &Plan, ctx: &mut (impl CharCtx + ParaCtx), default_fonts: [u16; 3]) -> Vec<u8> {
|
||||
// LPStshi: cbStshi, then Stshi: Stshif (18 bytes), ftcBi, and the latent style data (an
|
||||
// empty LSD for every sti below stiMaxWhenSaved).
|
||||
let cstd = plan.slots.len().min(0x0FFE) as u16;
|
||||
let sti_max = plan.names.iter().map(|(_, sti)| *sti).filter(|&s| s != STI_USER).max().map_or(0x5B, |m| (m + 1).max(0x5B));
|
||||
let cb_stshi = 18 + 2 + 2 + 4 * usize::from(sti_max);
|
||||
let mut v = Vec::new();
|
||||
v.extend_from_slice(&(cb_stshi as u16).to_le_bytes());
|
||||
v.extend_from_slice(&cstd.to_le_bytes());
|
||||
v.extend_from_slice(&0x000Au16.to_le_bytes()); // cbSTDBaseInFile
|
||||
v.extend_from_slice(&1u16.to_le_bytes()); // fStdStylenamesWritten
|
||||
v.extend_from_slice(&sti_max.to_le_bytes()); // stiMaxWhenSaved
|
||||
v.extend_from_slice(&FIXED.to_le_bytes()); // istdMaxFixedWhenSaved
|
||||
v.extend_from_slice(&0u16.to_le_bytes()); // nVerBuiltInNamesWhenSaved
|
||||
for f in default_fonts {
|
||||
v.extend_from_slice(&f.to_le_bytes()); // rgftcStandardChpStsh
|
||||
}
|
||||
v.extend_from_slice(&default_fonts[2].to_le_bytes()); // ftcBi
|
||||
v.extend_from_slice(&4u16.to_le_bytes()); // StshiLsd.cbLSD
|
||||
v.extend(std::iter::repeat_n(0u8, 4 * usize::from(sti_max)));
|
||||
for (istd, slot) in plan.slots.iter().enumerate().take(cstd as usize) {
|
||||
let (name, sti) = plan.names.get(istd).cloned().unwrap_or_default();
|
||||
let std = match slot.and_then(|i| sheet.styles.get(i)) {
|
||||
Some(st) => std_bytes(st, &name, sti, sheet, plan, ctx, istd == 0),
|
||||
None if !name.is_empty() => empty_builtin(&name, sti, istd as u16),
|
||||
None => Vec::new(),
|
||||
};
|
||||
v.extend_from_slice(&(std.len() as u16).to_le_bytes());
|
||||
v.extend_from_slice(&std);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// A built-in style the document doesn't define (Normal, Default Paragraph Font).
|
||||
fn empty_builtin(name: &str, sti: u16, istd: u16) -> Vec<u8> {
|
||||
let own = istd.to_le_bytes().to_vec();
|
||||
let (stk, upx): (u16, Vec<Vec<u8>>) = match sti {
|
||||
65 => (2, vec![Vec::new()]),
|
||||
105 => (3, vec![Vec::new(), own, Vec::new()]),
|
||||
107 => (4, vec![own]),
|
||||
_ => (1, vec![own, Vec::new()]),
|
||||
};
|
||||
let next = if stk == 1 { istd } else { ISTD_NIL };
|
||||
assemble(sti, stk, ISTD_NIL, next, upx.len() as u16, 0, name, &upx)
|
||||
}
|
||||
|
||||
/// One STD: `StdfBase`, the name, then the UPXs for its kind.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn std_bytes(st: &Style, name: &str, sti: u16, sheet: &StyleSheet, plan: &Plan, ctx: &mut (impl CharCtx + ParaCtx), normal: bool) -> Vec<u8> {
|
||||
let base = st.based_on.as_deref().and_then(|b| plan.istd.get(b).copied()).unwrap_or(ISTD_NIL);
|
||||
let istd_self = plan.istd.get(&st.id).copied().unwrap_or(0);
|
||||
let next = st.next.as_deref().and_then(|b| plan.istd.get(b).copied()).unwrap_or(istd_self);
|
||||
// Document defaults have nowhere else to go in this format: Normal (and any other root
|
||||
// paragraph or character style) starts from them.
|
||||
let root = base == ISTD_NIL;
|
||||
let mut chr = st.chr.clone();
|
||||
let mut para = st.para.clone();
|
||||
if root && matches!(st.kind, StyleKind::Paragraph) {
|
||||
chr = overlay(&sheet.default_chr, &chr);
|
||||
para = craft_wordmodel::props::ParaProps { style: None, ..overlay_para(&sheet.default_para, ¶) };
|
||||
}
|
||||
if normal {
|
||||
chr.style = None;
|
||||
}
|
||||
para.style = None;
|
||||
para.numbering = para.numbering.filter(|n| n.num != 0);
|
||||
chr.style = None;
|
||||
let mut grfstd: u16 = 0;
|
||||
if st.hidden {
|
||||
grfstd |= 0x0002 | 0x0100; // fHidden, fSemiHidden
|
||||
}
|
||||
if st.locked {
|
||||
grfstd |= 0x0200;
|
||||
}
|
||||
if st.quick {
|
||||
grfstd |= 0x1000; // fQFormat
|
||||
}
|
||||
let (stk, cupx, upx): (u16, u16, Vec<Vec<u8>>) = match st.kind {
|
||||
StyleKind::Paragraph => {
|
||||
let mut papx = istd_self.to_le_bytes().to_vec();
|
||||
let mut g = grpprl::papx(¶, ctx);
|
||||
g.truncate_to(super::fkp::MAX_PAPX_GRPPRL);
|
||||
papx.extend_from_slice(&g.0);
|
||||
let mut c = grpprl::chpx(&chr, ctx);
|
||||
c.truncate_to(255);
|
||||
(1, 2, vec![papx, c.0])
|
||||
}
|
||||
StyleKind::Character => {
|
||||
let mut c = grpprl::chpx(&chr, ctx);
|
||||
c.truncate_to(255);
|
||||
(2, 1, vec![c.0])
|
||||
}
|
||||
// Table styles: their paragraph and character formatting; the conditional formats stay
|
||||
// with the model (tables carry their own borders and shading when written).
|
||||
StyleKind::Table => {
|
||||
let mut papx = istd_self.to_le_bytes().to_vec();
|
||||
papx.extend_from_slice(&grpprl::papx(¶, ctx).0);
|
||||
(3, 3, vec![Vec::new(), papx, grpprl::chpx(&chr, ctx).0])
|
||||
}
|
||||
StyleKind::Numbering => {
|
||||
let mut papx = istd_self.to_le_bytes().to_vec();
|
||||
papx.extend_from_slice(&Grpprl::default().0);
|
||||
(4, 1, vec![papx])
|
||||
}
|
||||
};
|
||||
assemble(sti, stk, base, next, cupx, grfstd, name, &upx)
|
||||
}
|
||||
|
||||
/// `StdfBase` + `xstzName` + `GrLPUpxSw` (each UPX padded to an even size).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn assemble(sti: u16, stk: u16, base: u16, next: u16, cupx: u16, grfstd: u16, name: &str, upx: &[Vec<u8>]) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
v.extend_from_slice(&(sti & 0x0FFF).to_le_bytes());
|
||||
v.extend_from_slice(&((stk & 0xF) | ((base & 0x0FFF) << 4)).to_le_bytes());
|
||||
v.extend_from_slice(&((cupx & 0xF) | ((next & 0x0FFF) << 4)).to_le_bytes());
|
||||
let bch_at = v.len();
|
||||
v.extend_from_slice(&0u16.to_le_bytes()); // bchUpe, filled below
|
||||
v.extend_from_slice(&grfstd.to_le_bytes());
|
||||
let units: Vec<u16> = name.encode_utf16().take(253).collect();
|
||||
v.extend_from_slice(&(units.len() as u16).to_le_bytes());
|
||||
for u in &units {
|
||||
v.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
v.extend_from_slice(&0u16.to_le_bytes());
|
||||
for u in upx {
|
||||
v.extend_from_slice(&(u.len() as u16).to_le_bytes());
|
||||
v.extend_from_slice(u);
|
||||
if u.len() % 2 == 1 {
|
||||
v.push(0);
|
||||
}
|
||||
}
|
||||
let len = v.len() as u16;
|
||||
if let Some(b) = v.get_mut(bch_at..bch_at + 2) {
|
||||
b.copy_from_slice(&len.to_le_bytes());
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// `over`'s character properties over `under`'s.
|
||||
fn overlay(under: &CharProps, over: &CharProps) -> CharProps {
|
||||
let mut out = under.clone();
|
||||
out.overlay(over);
|
||||
out
|
||||
}
|
||||
|
||||
fn overlay_para(under: &craft_wordmodel::props::ParaProps, over: &craft_wordmodel::props::ParaProps) -> craft_wordmodel::props::ParaProps {
|
||||
let mut out = under.clone();
|
||||
out.overlay(over);
|
||||
out
|
||||
}
|
||||
|
||||
/// The `SttbfFfn`: a non-extended STTB whose entries are `FFN`s (a byte count, then the FFN).
|
||||
pub(crate) fn font_table(names: &[String]) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
v.extend_from_slice(&(names.len().min(0x7FFF) as u16).to_le_bytes());
|
||||
v.extend_from_slice(&0u16.to_le_bytes()); // cbExtra
|
||||
for name in names.iter().take(0x7FFF) {
|
||||
let ffn = ffn(name);
|
||||
v.push(ffn.len() as u8);
|
||||
v.extend_from_slice(&ffn);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// One `FFN` ([MS-DOC] §2.9.82): pitch/TrueType/family bits, `wWeight`, `chs`,
|
||||
/// `ixchSzAlt`, `panose`, `fs`, then the name, NUL-terminated UTF-16.
|
||||
fn ffn(name: &str) -> Vec<u8> {
|
||||
let units: Vec<u16> = name.encode_utf16().take(31).collect();
|
||||
let mut v = Vec::with_capacity(40 + units.len() * 2 + 2);
|
||||
let symbol = matches!(name.to_ascii_lowercase().as_str(), "symbol" | "wingdings" | "wingdings 2" | "wingdings 3" | "webdings");
|
||||
let family: u8 = if symbol {
|
||||
5 // FF_DECORATIVE
|
||||
} else if name.to_ascii_lowercase().contains("mono") || name.eq_ignore_ascii_case("courier new") {
|
||||
3 // FF_MODERN
|
||||
} else if name.to_ascii_lowercase().contains("sans")
|
||||
|| ["arial", "calibri", "aptos", "verdana", "tahoma", "segoe ui"].contains(&name.to_ascii_lowercase().as_str())
|
||||
{
|
||||
2 // FF_SWISS
|
||||
} else {
|
||||
1 // FF_ROMAN
|
||||
};
|
||||
// prq (2 = variable pitch), fTrueType, ff.
|
||||
v.push(2 | 0x04 | (family << 4));
|
||||
v.extend_from_slice(&400u16.to_le_bytes()); // wWeight
|
||||
v.push(if symbol { 2 } else { 0 }); // chs: SYMBOL_CHARSET or ANSI_CHARSET
|
||||
v.push(0); // ixchSzAlt: no alternate name
|
||||
v.extend_from_slice(&[0u8; 10]); // panose
|
||||
v.extend_from_slice(&[0u8; 24]); // fs
|
||||
for u in &units {
|
||||
v.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
v.extend_from_slice(&0u16.to_le_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn font_table_reads_back() {
|
||||
let names = vec!["Times New Roman".to_string(), "Symbol".to_string(), "Noto Sans Ünï".to_string()];
|
||||
let t = font_table(&names);
|
||||
assert_eq!(crate::stsh::fonts(&t, 0, t.len() as u32), names);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
//! The `\u{5}SummaryInformation` stream ([MS-OLEPS] §2.21): title, subject, author, keywords,
|
||||
//! comments, last author, revision and the creation and save times.
|
||||
|
||||
use craft_wordmodel::CoreProps;
|
||||
|
||||
/// `FMTID_SummaryInformation` as stored (little-endian GUID fields).
|
||||
const SUMMARY_FMTID: [u8; 16] = [0xe0, 0x85, 0x9f, 0xf2, 0xf9, 0x4f, 0x68, 0x10, 0xab, 0x91, 0x08, 0x00, 0x2b, 0x27, 0xb3, 0xd9];
|
||||
const VT_I2: u32 = 2;
|
||||
const VT_LPSTR: u32 = 30;
|
||||
const VT_FILETIME: u32 = 64;
|
||||
/// Longest string property written (characters).
|
||||
const MAX_CHARS: usize = 32 << 10;
|
||||
|
||||
/// The text of `s` in code page 1252, when every character has a byte there.
|
||||
fn cp1252(s: &str) -> Option<Vec<u8>> {
|
||||
s.chars().map(|c| (0u8..=255).find(|&b| craft_codepage::cp1252(b) == c && (b >= 0x20 || c == '\t' || c == '\n' || c == '\r'))).collect()
|
||||
}
|
||||
|
||||
/// The stream's bytes. Strings are Windows-1252 when they all fit it, else UTF-16 (code page
|
||||
/// 1200).
|
||||
pub(crate) fn stream(core: &CoreProps) -> Vec<u8> {
|
||||
let strings: Vec<(u32, String)> =
|
||||
[(2u32, &core.title), (3, &core.subject), (4, &core.creator), (5, &core.keywords), (6, &core.description), (8, &core.last_modified_by)]
|
||||
.into_iter()
|
||||
.filter(|(_, s)| !s.is_empty())
|
||||
.map(|(id, s)| (id, s.chars().take(MAX_CHARS).collect()))
|
||||
.chain((core.revision > 0).then(|| (9u32, core.revision.to_string())))
|
||||
.collect();
|
||||
let ansi = strings.iter().all(|(_, s)| cp1252(s).is_some());
|
||||
let codepage: u16 = if ansi { 1252 } else { 1200 };
|
||||
|
||||
let mut props: Vec<(u32, Vec<u8>)> = Vec::new();
|
||||
let mut cp = VT_I2.to_le_bytes().to_vec();
|
||||
cp.extend_from_slice(&codepage.to_le_bytes());
|
||||
cp.extend_from_slice(&[0, 0]);
|
||||
props.push((1, cp));
|
||||
for (id, s) in &strings {
|
||||
let mut bytes = if ansi { cp1252(s).unwrap_or_default() } else { s.encode_utf16().flat_map(u16::to_le_bytes).collect() };
|
||||
bytes.extend_from_slice(if ansi { &[0][..] } else { &[0, 0][..] });
|
||||
let mut v = VT_LPSTR.to_le_bytes().to_vec();
|
||||
v.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
|
||||
v.extend_from_slice(&bytes);
|
||||
while !v.len().is_multiple_of(4) {
|
||||
v.push(0);
|
||||
}
|
||||
props.push((*id, v));
|
||||
}
|
||||
for (id, iso) in [(12u32, &core.created), (13, &core.modified)] {
|
||||
if let Some(ft) = filetime(iso) {
|
||||
let mut v = VT_FILETIME.to_le_bytes().to_vec();
|
||||
v.extend_from_slice(&(ft as u32).to_le_bytes());
|
||||
v.extend_from_slice(&((ft >> 32) as u32).to_le_bytes());
|
||||
props.push((id, v));
|
||||
}
|
||||
}
|
||||
|
||||
// PropertySet: size, count, (id, offset) pairs, then the values.
|
||||
let table = 8 + props.len() * 8;
|
||||
let mut set = Vec::new();
|
||||
let mut values = Vec::new();
|
||||
let mut index = Vec::new();
|
||||
for (id, v) in &props {
|
||||
index.extend_from_slice(&id.to_le_bytes());
|
||||
index.extend_from_slice(&((table + values.len()) as u32).to_le_bytes());
|
||||
values.extend_from_slice(v);
|
||||
}
|
||||
set.extend_from_slice(&((table + values.len()) as u32).to_le_bytes());
|
||||
set.extend_from_slice(&(props.len() as u32).to_le_bytes());
|
||||
set.extend_from_slice(&index);
|
||||
set.extend_from_slice(&values);
|
||||
|
||||
// PropertySetStream header: byte order, version, system id, CLSID, one set, its FMTID and
|
||||
// offset.
|
||||
let mut out = vec![0xFE, 0xFF, 0, 0];
|
||||
out.extend_from_slice(&0x0002_0006u32.to_le_bytes());
|
||||
out.extend_from_slice(&[0u8; 16]);
|
||||
out.extend_from_slice(&1u32.to_le_bytes());
|
||||
out.extend_from_slice(&SUMMARY_FMTID);
|
||||
out.extend_from_slice(&48u32.to_le_bytes());
|
||||
out.extend_from_slice(&set);
|
||||
out
|
||||
}
|
||||
|
||||
/// A FILETIME (100 ns since 1601) from an ISO 8601 date.
|
||||
fn filetime(iso: &str) -> Option<u64> {
|
||||
let t = super::parse_time(iso)?.and_utc().timestamp();
|
||||
let ft = (t.checked_add(11_644_473_600)?).checked_mul(10_000_000)?;
|
||||
u64::try_from(ft).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn summary_reads_back() {
|
||||
let core = craft_wordmodel::CoreProps {
|
||||
title: "Annual Report".into(),
|
||||
creator: "Zoë Writer".into(),
|
||||
keywords: "a, b".into(),
|
||||
revision: 3,
|
||||
created: "2024-05-06T07:08:09Z".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let read = crate::metadata::parse_for_tests(&super::stream(&core)).unwrap();
|
||||
assert_eq!(read.title, "Annual Report");
|
||||
assert_eq!(read.creator, "Zoë Writer");
|
||||
assert_eq!(read.keywords, "a, b");
|
||||
assert_eq!(read.revision, 3);
|
||||
assert_eq!(read.created, "2024-05-06T07:08:09Z");
|
||||
let unicode = craft_wordmodel::CoreProps { title: "Отчёт 报告".into(), ..Default::default() };
|
||||
assert_eq!(crate::metadata::parse_for_tests(&super::stream(&unicode)).unwrap().title, "Отчёт 报告");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
//! A table row's properties, carried by its row-end mark ([MS-DOC] §2.4.3, §2.6.3): the cell
|
||||
//! edges and per-cell borders (`sprmTDefTable`), the table's borders, alignment and indent, row
|
||||
//! height and flags, and the cells' shading, widths, vertical merges and alignment.
|
||||
|
||||
use craft_wordmodel::props::{Align, Border, Borders, HeightRule, VAlign, VMerge};
|
||||
use craft_wordmodel::table::Table;
|
||||
|
||||
use super::grpprl::{Grpprl, brc, brc80, shd, twips};
|
||||
|
||||
const T_JC90: u16 = 0x5400;
|
||||
const T_JC: u16 = 0x548A;
|
||||
const T_DXA_LEFT: u16 = 0x9601;
|
||||
const T_DXA_GAP_HALF: u16 = 0x9602;
|
||||
const T_CANT_SPLIT90: u16 = 0x3403;
|
||||
const T_TABLE_HEADER: u16 = 0x3404;
|
||||
const T_CANT_SPLIT: u16 = 0x3466;
|
||||
const T_TABLE_BORDERS80: u16 = 0xD605;
|
||||
const T_ROW_HEIGHT: u16 = 0x9407;
|
||||
const T_DEF_TABLE: u16 = 0xD608;
|
||||
const T_DEF_TABLE_SHD: [u16; 3] = [0xD612, 0xD616, 0xD60C];
|
||||
const T_TABLE_BORDERS: u16 = 0xD613;
|
||||
const T_TABLE_WIDTH: u16 = 0xF614;
|
||||
const T_VERT_MERGE: u16 = 0xD62B;
|
||||
const T_VERT_ALIGN: u16 = 0xD62C;
|
||||
const T_CELL_PADDING_DEFAULT: u16 = 0xD634;
|
||||
const T_CELL_WIDTH: u16 = 0xD635;
|
||||
|
||||
/// Most cells a row holds (`itcMac` MUST be at most 63).
|
||||
pub(crate) const MAX_CELLS: usize = 63;
|
||||
/// Word's default space left of a cell's text (5.4 pt), when the table doesn't say.
|
||||
const DEFAULT_GAP: f32 = 5.4;
|
||||
|
||||
/// The TAPX sprms of row `ri` of `t` (of `rows`), for its row-end mark; `style_borders` are
|
||||
/// the borders of the table's style, for a table without its own.
|
||||
pub(crate) fn row_end(t: &Table, ri: usize, rows: usize, style_borders: Option<Borders>) -> Grpprl {
|
||||
let mut g = Grpprl::default();
|
||||
let Some(row) = t.rows.get(ri) else { return g };
|
||||
let cells = &row.cells.get(..row.cells.len().min(MAX_CELLS)).unwrap_or(&[]);
|
||||
let n = cells.len();
|
||||
let tp = &t.props;
|
||||
|
||||
// Alignment, indent and the cell gap.
|
||||
let jc: u16 = match tp.align {
|
||||
Some(Align::Center) => 1,
|
||||
Some(Align::Right) => 2,
|
||||
_ => 0,
|
||||
};
|
||||
if jc != 0 {
|
||||
g.u16(T_JC90, jc);
|
||||
g.u16(T_JC, jc);
|
||||
}
|
||||
let gap = tp.cell_margins.map_or(DEFAULT_GAP, |m| m[1]).clamp(0.0, 100.0);
|
||||
g.i16(T_DXA_GAP_HALF, twips(gap));
|
||||
let indent = tp.indent.unwrap_or(0.0);
|
||||
g.i16(T_DXA_LEFT, twips(indent + gap));
|
||||
if let Some(m) = tp.cell_margins {
|
||||
// CSSAOperand: itcFirst, itcLim, grfbrc (top 1, left 2, bottom 4, right 8), ftsWidth
|
||||
// (3 = twips), wWidth.
|
||||
for (side, v) in [(1u8, m[0]), (2, m[1]), (4, m[2]), (8, m[3])] {
|
||||
let w = twips(v.max(0.0)) as u16;
|
||||
let mut op = vec![0, n.max(1) as u8, side, 3];
|
||||
op.extend_from_slice(&w.to_le_bytes());
|
||||
g.var(T_CELL_PADDING_DEFAULT, &op);
|
||||
}
|
||||
}
|
||||
if let Some(pct) = tp.width_pct {
|
||||
// ftsWidth 2: fiftieths of a percent.
|
||||
let mut op = vec![2u8];
|
||||
op.extend_from_slice(&((pct.clamp(0.0, 600.0) * 50.0).round() as u16).to_le_bytes());
|
||||
g.raw(T_TABLE_WIDTH, &op);
|
||||
} else if let Some(w) = tp.width {
|
||||
let mut op = vec![3u8];
|
||||
op.extend_from_slice(&(twips(w.max(0.0)) as u16).to_le_bytes());
|
||||
g.raw(T_TABLE_WIDTH, &op);
|
||||
}
|
||||
|
||||
// Row height and flags.
|
||||
if let Some(h) = row.props.height {
|
||||
let tw = twips(h.max(0.0));
|
||||
g.i16(T_ROW_HEIGHT, if row.props.height_rule == HeightRule::Exact { tw.saturating_neg() } else { tw });
|
||||
}
|
||||
if row.props.header {
|
||||
g.u8(T_TABLE_HEADER, 1);
|
||||
}
|
||||
if row.props.cant_split {
|
||||
g.u8(T_CANT_SPLIT90, 1);
|
||||
g.u8(T_CANT_SPLIT, 1);
|
||||
}
|
||||
|
||||
// The table's borders: its own, else its style's.
|
||||
let borders = tp.borders.or(style_borders).unwrap_or_default();
|
||||
let six = [borders.top, borders.left, borders.bottom, borders.right, borders.between, borders.inside_v];
|
||||
let mut b80 = Vec::with_capacity(24);
|
||||
let mut b = Vec::with_capacity(48);
|
||||
for side in &six {
|
||||
b80.extend_from_slice(&side.as_ref().map_or([0; 4], brc80));
|
||||
b.extend_from_slice(&side.as_ref().map_or([0; 8], brc));
|
||||
}
|
||||
g.var(T_TABLE_BORDERS80, &b80);
|
||||
g.var(T_TABLE_BORDERS, &b);
|
||||
|
||||
// Cell edges: each cell spans its grid columns (from the row's `grid_before`), else its own
|
||||
// preferred width; the edges start at the table's indent.
|
||||
let widths = cell_widths(t, ri);
|
||||
let mut edges: Vec<i16> = Vec::with_capacity(n + 1);
|
||||
let mut x = twips(indent);
|
||||
edges.push(x);
|
||||
for w in &widths {
|
||||
x = x.saturating_add(twips(*w).max(1));
|
||||
edges.push(x);
|
||||
}
|
||||
// TDefTableOperand: cb, itcMac, rgdxaCenter, rgTc80.
|
||||
let mut body = vec![n as u8];
|
||||
for e in &edges {
|
||||
body.extend_from_slice(&e.to_le_bytes());
|
||||
}
|
||||
for (ci, cell) in cells.iter().enumerate() {
|
||||
let mut grf: u16 = 0;
|
||||
match cell.props.vmerge {
|
||||
VMerge::Restart => grf |= 0x20 | 0x40,
|
||||
VMerge::Continue => grf |= 0x20,
|
||||
VMerge::None => {}
|
||||
}
|
||||
grf |= valign_bits(cell.props.valign) << 7;
|
||||
grf |= 3 << 9; // ftsWidth: twips
|
||||
if cell.props.no_wrap {
|
||||
grf |= 1 << 13;
|
||||
}
|
||||
body.extend_from_slice(&grf.to_le_bytes());
|
||||
body.extend_from_slice(&(twips(widths.get(ci).copied().unwrap_or(0.0)) as u16).to_le_bytes());
|
||||
let cb = cell_borders(&borders, cell.props.borders.as_ref(), ri, rows, ci, n);
|
||||
for side in [cb.top, cb.left, cb.bottom, cb.right] {
|
||||
body.extend_from_slice(&side.as_ref().map_or([0; 4], brc80));
|
||||
}
|
||||
}
|
||||
let cb = u16::try_from(body.len() + 1).unwrap_or(u16::MAX);
|
||||
let mut op = cb.to_le_bytes().to_vec();
|
||||
op.extend_from_slice(&body);
|
||||
g.raw(T_DEF_TABLE, &op);
|
||||
|
||||
// Cell shading, in three arrays of up to 22 cells each.
|
||||
if cells.iter().any(|c| c.props.shading.is_some()) || tp.shading.is_some() {
|
||||
for (k, op) in T_DEF_TABLE_SHD.iter().enumerate() {
|
||||
let chunk = cells.get(k * 22..((k + 1) * 22).min(n)).unwrap_or(&[]);
|
||||
if chunk.is_empty() {
|
||||
break;
|
||||
}
|
||||
let mut v = Vec::with_capacity(chunk.len() * 10);
|
||||
for c in chunk {
|
||||
v.extend_from_slice(&shd(c.props.shading.or(tp.shading)));
|
||||
}
|
||||
g.var(*op, &v);
|
||||
}
|
||||
}
|
||||
// Per-cell preferred widths, vertical merges and alignment, for the readers that take them
|
||||
// from sprms rather than from the TC80s.
|
||||
for (ci, cell) in cells.iter().enumerate() {
|
||||
let i = ci as u8;
|
||||
let mut op = vec![i, i + 1];
|
||||
if let Some(pct) = cell.props.width_pct {
|
||||
op.push(2);
|
||||
op.extend_from_slice(&((pct.clamp(0.0, 100.0) * 50.0).round() as u16).to_le_bytes());
|
||||
} else {
|
||||
op.push(3);
|
||||
op.extend_from_slice(&(twips(widths.get(ci).copied().unwrap_or(0.0)) as u16).to_le_bytes());
|
||||
}
|
||||
g.var(T_CELL_WIDTH, &op);
|
||||
match cell.props.vmerge {
|
||||
VMerge::Restart => g.var(T_VERT_MERGE, &[i, 3]),
|
||||
VMerge::Continue => g.var(T_VERT_MERGE, &[i, 1]),
|
||||
VMerge::None => {}
|
||||
}
|
||||
if let Some(v) = cell.props.valign
|
||||
&& v != VAlign::Top
|
||||
{
|
||||
g.var(T_VERT_ALIGN, &[i, i + 1, valign_bits(Some(v)) as u8]);
|
||||
}
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
fn valign_bits(v: Option<VAlign>) -> u16 {
|
||||
match v {
|
||||
Some(VAlign::Center) => 1,
|
||||
Some(VAlign::Bottom) => 2,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Each cell's width in points: the grid columns it spans, else its preferred width, else an
|
||||
/// equal share of a 6-inch row.
|
||||
fn cell_widths(t: &Table, ri: usize) -> Vec<f32> {
|
||||
let Some(row) = t.rows.get(ri) else { return Vec::new() };
|
||||
let n = row.cells.len().min(MAX_CELLS);
|
||||
let mut col = row.props.grid_before as usize;
|
||||
let mut out = Vec::with_capacity(n);
|
||||
for cell in row.cells.iter().take(n) {
|
||||
let span = (cell.props.span.max(1) as usize).min(MAX_CELLS);
|
||||
let from_grid: f32 = t.grid.get(col..col.saturating_add(span).min(t.grid.len())).map_or(0.0, |g| g.iter().sum());
|
||||
col = col.saturating_add(span);
|
||||
let w = if from_grid > 0.0 && from_grid.is_finite() {
|
||||
from_grid
|
||||
} else {
|
||||
cell.props.width.filter(|w| *w > 0.0 && w.is_finite()).unwrap_or(432.0 / n.max(1) as f32)
|
||||
};
|
||||
out.push(w.clamp(1.0, 1584.0));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The borders cell `ci` of `n` in row `ri` of `rows` shows: the table's outer borders on the
|
||||
/// table's edges, its inside ones between cells, the cell's own over both.
|
||||
fn cell_borders(table: &Borders, own: Option<&Borders>, ri: usize, rows: usize, ci: usize, n: usize) -> Borders {
|
||||
let pick = |own: Option<Border>, edge: bool, outer: Option<Border>, inner: Option<Border>| own.or(if edge { outer } else { inner });
|
||||
let own = own.copied().unwrap_or_default();
|
||||
Borders {
|
||||
top: pick(own.top, ri == 0, table.top, table.between),
|
||||
bottom: pick(own.bottom, ri + 1 == rows, table.bottom, table.between),
|
||||
left: pick(own.left, ci == 0, table.left, table.inside_v),
|
||||
right: pick(own.right, ci + 1 == n, table.right, table.inside_v),
|
||||
between: None,
|
||||
inside_v: None,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,707 @@
|
||||
//! Round trips: documents written as `.doc` and read back with the reader, which follows the
|
||||
//! same specification independently of the writer.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_wordmodel::para::{InlineObject, NoteKind, Paragraph};
|
||||
use craft_wordmodel::props::*;
|
||||
use craft_wordmodel::section::{Columns, SectionProps, SectionStart};
|
||||
use craft_wordmodel::table::{Cell, Row, Table};
|
||||
use craft_wordmodel::{Block, Document, PartKind, para_block};
|
||||
|
||||
use crate::{read, write};
|
||||
|
||||
fn doc_of(paras: Vec<Paragraph>) -> Document {
|
||||
let mut d = Document::new();
|
||||
d.body = paras.into_iter().map(para_block).collect();
|
||||
d
|
||||
}
|
||||
|
||||
fn round(d: &Document) -> Document {
|
||||
let bytes = write(d).expect("write");
|
||||
read(&bytes).expect("read back")
|
||||
}
|
||||
|
||||
fn texts(d: &Document) -> Vec<String> {
|
||||
d.body.iter().filter_map(|b| b.as_para().map(|p| p.plain_text())).collect()
|
||||
}
|
||||
|
||||
fn para(text: &str) -> Paragraph {
|
||||
Paragraph::with_text(text, CharProps::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_text_round_trips() {
|
||||
let d = doc_of(vec![
|
||||
para("Hello, world."),
|
||||
para(""),
|
||||
para("Ünïcødé — “quotes” 😀 𝄞 中文"),
|
||||
para("tab\there, line\nbreak, nb\u{2011}hyphen, soft\u{AD}hyphen"),
|
||||
]);
|
||||
let r = round(&d);
|
||||
assert_eq!(texts(&r), texts(&d));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_document_is_one_paragraph() {
|
||||
let r = round(&Document::new());
|
||||
assert_eq!(texts(&r), vec![String::new()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_formatting_round_trips() {
|
||||
let mut p = para("bold italic under red big font high sup");
|
||||
let fmt = |p: &mut Paragraph, a: usize, b: usize, f: &dyn Fn(&mut CharProps)| p.format(a, b, f).unwrap();
|
||||
fmt(&mut p, 0, 4, &|c| c.bold = Some(true));
|
||||
fmt(&mut p, 5, 11, &|c| c.italic = Some(true));
|
||||
fmt(&mut p, 12, 17, &|c| c.underline = Some(Underline::Double));
|
||||
fmt(&mut p, 18, 21, &|c| c.color = Some(TextColor::Rgb(craft_wordmodel::Rgb(0xC0, 0x10, 0x20))));
|
||||
fmt(&mut p, 22, 25, &|c| c.size = Some(18.5));
|
||||
fmt(&mut p, 26, 30, &|c| c.font = Some("Georgia".into()));
|
||||
fmt(&mut p, 31, 35, &|c| c.highlight = Some(Highlight::Yellow));
|
||||
fmt(&mut p, 36, 39, &|c| {
|
||||
c.strike = Some(true);
|
||||
c.small_caps = Some(true);
|
||||
});
|
||||
let r = round(&doc_of(vec![p.clone()]));
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
assert_eq!(q.plain_text(), p.plain_text());
|
||||
let at = |i: usize| q.props_at(i + 1).clone();
|
||||
assert_eq!(at(0).bold, Some(true));
|
||||
assert_eq!(at(5).italic, Some(true));
|
||||
assert_eq!(at(12).underline, Some(Underline::Double));
|
||||
assert_eq!(at(18).color, Some(TextColor::Rgb(craft_wordmodel::Rgb(0xC0, 0x10, 0x20))));
|
||||
assert_eq!(at(22).size, Some(18.5));
|
||||
assert_eq!(at(26).font.as_deref(), Some("Georgia"));
|
||||
assert_eq!(at(36).strike, Some(true));
|
||||
assert_eq!(at(36).small_caps, Some(true));
|
||||
assert_ne!(at(5).bold, Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_formatting_round_trips() {
|
||||
let mut p = para("Centred and indented");
|
||||
p.props.align = Some(Align::Center);
|
||||
p.props.indent_left = Some(36.0);
|
||||
p.props.indent_right = Some(18.0);
|
||||
p.props.indent_first = Some(-9.0);
|
||||
p.props.space_before = Some(12.0);
|
||||
p.props.space_after = Some(6.0);
|
||||
p.props.line_spacing = Some(LineSpacing::Multiple(1.5));
|
||||
p.props.keep_next = Some(true);
|
||||
p.props.page_break_before = Some(true);
|
||||
p.props.tabs = Some(vec![TabStop { pos: 72.0, align: TabAlign::Right, leader: TabLeader::Dot }]);
|
||||
p.props.borders = Some(Borders {
|
||||
bottom: Some(Border { style: BorderStyle::Double, width: 1.5, color: Some(craft_wordmodel::Rgb(0, 0, 255)), space: 1.0 }),
|
||||
..Default::default()
|
||||
});
|
||||
let mut exact = para("Exactly 14 pt");
|
||||
exact.props.line_spacing = Some(LineSpacing::Exactly(14.0));
|
||||
let r = round(&doc_of(vec![p.clone(), exact]));
|
||||
let q = &r.body[0].as_para().unwrap().props;
|
||||
assert_eq!(q.align, Some(Align::Center));
|
||||
assert_eq!(q.indent_left, Some(36.0));
|
||||
assert_eq!(q.indent_right, Some(18.0));
|
||||
assert_eq!(q.indent_first, Some(-9.0));
|
||||
assert_eq!(q.space_before, Some(12.0));
|
||||
assert_eq!(q.space_after, Some(6.0));
|
||||
assert_eq!(q.line_spacing, Some(LineSpacing::Multiple(1.5)));
|
||||
assert_eq!(q.keep_next, Some(true));
|
||||
assert_eq!(q.page_break_before, Some(true));
|
||||
assert_eq!(q.tabs, p.props.tabs);
|
||||
assert_eq!(q.borders.and_then(|b| b.bottom), p.props.borders.and_then(|b| b.bottom));
|
||||
assert_eq!(r.body[1].as_para().unwrap().props.line_spacing, Some(LineSpacing::Exactly(14.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn styles_round_trip() {
|
||||
let mut d = Document::new();
|
||||
let h1 = d.styles.styles.iter().find(|s| s.name == "Heading 1").map(|s| s.id.clone()).expect("a Heading 1 style");
|
||||
Arc::make_mut(&mut d.styles).styles.push(craft_wordmodel::styles::Style {
|
||||
id: "MyQuote".into(),
|
||||
name: "My Quote".into(),
|
||||
kind: craft_wordmodel::styles::StyleKind::Paragraph,
|
||||
based_on: Some("Normal".into()),
|
||||
chr: CharProps { italic: Some(true), ..Default::default() },
|
||||
para: ParaProps { indent_left: Some(24.0), ..Default::default() },
|
||||
..Default::default()
|
||||
});
|
||||
d.body = vec![para_block(para("Title text").styled(&h1)), para_block(para("Quoted").styled("MyQuote")), para_block(para("Body"))];
|
||||
let r = round(&d);
|
||||
assert_eq!(texts(&r), vec!["Title text", "Quoted", "Body"]);
|
||||
let style_of = |i: usize| r.body[i].as_para().unwrap().props.style.clone().and_then(|id| r.styles.get(&id).map(|s| s.name.clone()));
|
||||
assert_eq!(style_of(0).as_deref(), Some("Heading 1"));
|
||||
assert_eq!(style_of(1).as_deref(), Some("My Quote"));
|
||||
let q = r.styles.styles.iter().find(|s| s.name == "My Quote").unwrap();
|
||||
assert_eq!(q.chr.italic, Some(true));
|
||||
assert_eq!(q.para.indent_left, Some(24.0));
|
||||
// The heading keeps its outline level, the stylesheet its default font.
|
||||
let h = r.styles.styles.iter().find(|s| s.name == "Heading 1").unwrap();
|
||||
assert_eq!(h.para.outline_level, Some(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_paragraphs_page_their_formatting() {
|
||||
let paras: Vec<Paragraph> = (0..2000)
|
||||
.map(|i| {
|
||||
let mut p = para(&format!("Paragraph {i} with some text"));
|
||||
if i % 3 == 0 {
|
||||
p.format(0, 9, &|c| c.bold = Some(true)).unwrap();
|
||||
}
|
||||
if i % 5 == 0 {
|
||||
p.props.indent_left = Some(i as f32 % 100.0);
|
||||
}
|
||||
p
|
||||
})
|
||||
.collect();
|
||||
let d = doc_of(paras);
|
||||
let r = round(&d);
|
||||
assert_eq!(texts(&r), texts(&d));
|
||||
for i in [0usize, 3, 999, 1998] {
|
||||
let q = r.body[i].as_para().unwrap();
|
||||
assert_eq!(q.props_at(1).bold, if i % 3 == 0 { Some(true) } else { q.props_at(1).bold.filter(|_| false) }, "paragraph {i}");
|
||||
if i % 5 == 0 {
|
||||
assert_eq!(q.props.indent_left, Some(i as f32 % 100.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cell(text: &str) -> Cell {
|
||||
Cell { props: CellProps { span: 1, ..Default::default() }, blocks: vec![para_block(para(text))], controls: Default::default() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tables_round_trip() {
|
||||
let mut a = cell("A1");
|
||||
a.props.shading = Some(craft_wordmodel::Rgb(0xEE, 0xDD, 0x00));
|
||||
a.props.vmerge = VMerge::Restart;
|
||||
let mut b = cell("");
|
||||
b.props.vmerge = VMerge::Continue;
|
||||
let mut multi = cell("B1 first");
|
||||
multi.blocks.push(para_block(para("B1 second")));
|
||||
let t = Table {
|
||||
props: TableProps {
|
||||
borders: Some(Borders { top: Some(Border::single(0.5)), bottom: Some(Border::single(0.5)), ..Default::default() }),
|
||||
..Default::default()
|
||||
},
|
||||
grid: vec![100.0, 150.0, 80.0],
|
||||
rows: vec![
|
||||
Row { props: RowProps { header: true, ..Default::default() }, cells: vec![a, multi, cell("C1")], controls: Default::default() },
|
||||
Row {
|
||||
props: RowProps { height: Some(30.0), height_rule: HeightRule::Exact, ..Default::default() },
|
||||
cells: vec![b, cell("B2"), cell("C2")],
|
||||
controls: Default::default(),
|
||||
},
|
||||
],
|
||||
controls: Default::default(),
|
||||
};
|
||||
let mut d = Document::new();
|
||||
d.body = vec![para_block(para("Before")), Arc::new(Block::Table(t)), para_block(para("After"))];
|
||||
let r = round(&d);
|
||||
assert_eq!(r.body.len(), 3, "{:?}", r.body.iter().map(|b| b.as_para().map(|p| p.plain_text())).collect::<Vec<_>>());
|
||||
let t = r.body[1].as_table().expect("a table");
|
||||
assert_eq!(t.rows.len(), 2);
|
||||
assert_eq!(t.grid, vec![100.0, 150.0, 80.0]);
|
||||
let cell_text = |r: usize, c: usize| t.rows[r].cells[c].blocks.iter().filter_map(|b| b.as_para().map(|p| p.plain_text())).collect::<Vec<_>>();
|
||||
assert_eq!(cell_text(0, 0), vec!["A1"]);
|
||||
assert_eq!(cell_text(0, 1), vec!["B1 first", "B1 second"]);
|
||||
assert_eq!(cell_text(1, 2), vec!["C2"]);
|
||||
assert_eq!(t.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0xEE, 0xDD, 0x00)));
|
||||
assert_eq!(t.rows[0].cells[0].props.vmerge, VMerge::Restart);
|
||||
assert_eq!(t.rows[1].cells[0].props.vmerge, VMerge::Continue);
|
||||
assert!(t.rows[0].props.header);
|
||||
assert_eq!(t.rows[1].props.height, Some(30.0));
|
||||
assert_eq!(t.rows[1].props.height_rule, HeightRule::Exact);
|
||||
assert_eq!(texts(&r), vec!["Before", "After"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sections_headers_and_footers_round_trip() {
|
||||
let mut d = Document::new();
|
||||
let h1 = d.add_part(PartKind::Header, vec![para_block(para("First header"))]);
|
||||
let f1 = d.add_part(PartKind::Footer, vec![para_block(para("First footer"))]);
|
||||
let h2 = d.add_part(PartKind::Header, vec![para_block(para("Landscape header"))]);
|
||||
let mut first = para("Portrait section");
|
||||
let mut s1 = SectionProps::default();
|
||||
s1.headers.default = Some(h1);
|
||||
s1.footers.default = Some(f1);
|
||||
s1.margin_left = 54.0;
|
||||
first.section = Some(Box::new(s1));
|
||||
let mut last =
|
||||
SectionProps { landscape: true, start: SectionStart::NextPage, columns: Columns { count: 2, ..Default::default() }, ..Default::default() };
|
||||
(last.page_w, last.page_h) = (792.0, 612.0);
|
||||
last.headers.default = Some(h2);
|
||||
d.body = vec![para_block(first), para_block(para("Landscape section"))];
|
||||
d.last_section = last;
|
||||
let r = round(&d);
|
||||
assert_eq!(texts(&r), vec!["Portrait section", "Landscape section"]);
|
||||
let s = r.body[0].as_para().unwrap().section.as_ref().expect("the first section ends here");
|
||||
assert_eq!(s.margin_left, 54.0);
|
||||
assert!(!s.landscape);
|
||||
let part_text =
|
||||
|id: Option<u32>| id.and_then(|i| r.parts.get(&i)).and_then(|p| p.blocks.first()).and_then(|b| b.as_para()).map(|p| p.plain_text());
|
||||
assert_eq!(part_text(s.headers.default).as_deref(), Some("First header"));
|
||||
assert_eq!(part_text(s.footers.default).as_deref(), Some("First footer"));
|
||||
assert!(r.last_section.landscape);
|
||||
assert_eq!((r.last_section.page_w, r.last_section.page_h), (792.0, 612.0));
|
||||
assert_eq!(r.last_section.columns.count, 2);
|
||||
assert_eq!(part_text(r.last_section.headers.default).as_deref(), Some("Landscape header"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notes_round_trip() {
|
||||
let mut d = Document::new();
|
||||
let f = d.add_part(PartKind::Footnote, vec![para_block(para("The footnote."))]);
|
||||
let e = d.add_part(PartKind::Endnote, vec![para_block(para("The endnote."))]);
|
||||
let mut p = para("Text.");
|
||||
p.insert_object(5, InlineObject::NoteRef { kind: NoteKind::Footnote, id: f, custom: String::new() }, &CharProps::default()).unwrap();
|
||||
let end = p.text.len();
|
||||
p.insert_object(end, InlineObject::NoteRef { kind: NoteKind::Endnote, id: e, custom: String::new() }, &CharProps::default()).unwrap();
|
||||
d.body = vec![para_block(p), para_block(para("After."))];
|
||||
let r = round(&d);
|
||||
assert_eq!(texts(&r)[1], "After.");
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
let notes: Vec<(NoteKind, String)> = q
|
||||
.objects
|
||||
.iter()
|
||||
.filter_map(|o| match o {
|
||||
InlineObject::NoteRef { kind, id, .. } => {
|
||||
Some((*kind, r.parts.get(id).and_then(|p| p.blocks.first()).and_then(|b| b.as_para()).map(|p| p.plain_text()).unwrap_or_default()))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(notes, vec![(NoteKind::Footnote, "The footnote.".to_string()), (NoteKind::Endnote, "The endnote.".to_string())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_links_and_bookmarks_round_trip() {
|
||||
let mut p = para("Page , see Example and here.");
|
||||
p.insert_object(
|
||||
5,
|
||||
InlineObject::Field { instr: "PAGE".into(), result: "7".into(), locked: false, dirty: false, code: false, form: String::new() },
|
||||
&CharProps::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let at = p.text.find("Example").unwrap();
|
||||
p.format(at, at + 7, &|c| c.link = Some("https://example.org/a?b=1".into())).unwrap();
|
||||
let here = p.text.find("here").unwrap();
|
||||
p.insert_object(here + 4, InlineObject::BookmarkEnd { name: "Spot".into() }, &CharProps::default()).unwrap();
|
||||
p.insert_object(here, InlineObject::BookmarkStart { name: "Spot".into() }, &CharProps::default()).unwrap();
|
||||
let r = round(&doc_of(vec![p]));
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
assert!(q.objects.iter().any(|o| matches!(o, InlineObject::Field { instr, result, .. } if instr == "PAGE" && result == "7")), "{:?}", q.objects);
|
||||
let link_at = q.text.find("Example").unwrap();
|
||||
assert_eq!(q.props_at(link_at + 1).link.as_deref(), Some("https://example.org/a?b=1"));
|
||||
assert!(q.objects.iter().any(|o| matches!(o, InlineObject::BookmarkStart { name } if name == "Spot")));
|
||||
assert!(q.objects.iter().any(|o| matches!(o, InlineObject::BookmarkEnd { name } if name == "Spot")));
|
||||
}
|
||||
|
||||
/// A 2×1 PNG.
|
||||
fn png() -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_pixel(2, 1, image::Rgba([10, 200, 30, 255]));
|
||||
let mut out = Vec::new();
|
||||
image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_pictures_round_trip() {
|
||||
let mut d = Document::new();
|
||||
let bytes = png();
|
||||
d.media.insert("pic.png".into(), Arc::new(bytes.clone()));
|
||||
let mut p = para("Picture: .");
|
||||
p.insert_object(
|
||||
9,
|
||||
InlineObject::Image {
|
||||
media: "pic.png".into(),
|
||||
w: 144.0,
|
||||
h: 72.0,
|
||||
alt: "A green bar".into(),
|
||||
float: Default::default(),
|
||||
crop: [0.0; 4],
|
||||
ole: None,
|
||||
fx: Default::default(),
|
||||
},
|
||||
&CharProps::default(),
|
||||
)
|
||||
.unwrap();
|
||||
d.body = vec![para_block(p)];
|
||||
let r = round(&d);
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
let Some(InlineObject::Image { media, w, h, alt, .. }) = q.objects.first() else { panic!("no picture: {:?}", q.objects) };
|
||||
assert!((w - 144.0).abs() < 0.1 && (h - 72.0).abs() < 0.1, "{w}×{h}");
|
||||
assert_eq!(alt, "A green bar");
|
||||
assert_eq!(r.media.get(media).map(|m| m.as_slice()), Some(bytes.as_slice()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_properties_round_trip() {
|
||||
let mut d = Document::new();
|
||||
d.core.title = "Quarterly".into();
|
||||
d.core.creator = "A. Writer".into();
|
||||
let r = round(&d);
|
||||
assert_eq!(r.core.title, "Quarterly");
|
||||
assert_eq!(r.core.creator, "A. Writer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_round_trip() {
|
||||
use craft_wordmodel::numbering::{AbstractNum, Level, Num};
|
||||
use craft_wordmodel::section::NumFormat;
|
||||
let mut d = Document::new();
|
||||
let levels = vec![
|
||||
Level { start: 1, format: NumFormat::Decimal, text: "%1.".into(), indent: 36.0, hanging: 18.0, ..Level::default() },
|
||||
Level { start: 1, format: NumFormat::LowerLetter, text: "%1.%2)".into(), indent: 72.0, hanging: 18.0, ..Level::default() },
|
||||
Level { start: 1, format: NumFormat::Bullet, text: "\u{2022}".into(), indent: 108.0, hanging: 18.0, ..Level::default() },
|
||||
];
|
||||
let numbering = Arc::make_mut(&mut d.numbering);
|
||||
numbering.abstracts = vec![AbstractNum { id: 7, levels, ..Default::default() }];
|
||||
numbering.nums = vec![Num { id: 3, abstract_id: 7, start_overrides: vec![(0, 4)], ..Default::default() }];
|
||||
let item = |text: &str, level: u8| {
|
||||
let mut p = para(text);
|
||||
p.props.numbering = Some(NumRef { num: 3, level });
|
||||
para_block(p)
|
||||
};
|
||||
d.body = vec![item("One", 0), item("One a", 1), item("Dot", 2)];
|
||||
let r = round(&d);
|
||||
let num = |i: usize| r.body[i].as_para().unwrap().props.numbering.expect("numbered");
|
||||
assert_eq!((num(0).level, num(1).level, num(2).level), (0, 1, 2));
|
||||
let n = r.numbering.nums.iter().find(|n| n.id == num(0).num).expect("the list");
|
||||
assert_eq!(n.start_overrides, vec![(0, 4)]);
|
||||
let a = r.numbering.abstracts.iter().find(|a| a.id == n.abstract_id).expect("its levels");
|
||||
assert_eq!(a.levels[0].text, "%1.");
|
||||
assert_eq!(a.levels[1].text, "%1.%2)");
|
||||
assert_eq!(a.levels[1].format, NumFormat::LowerLetter);
|
||||
assert_eq!(a.levels[2].text, "\u{2022}");
|
||||
assert_eq!(a.levels[2].format, NumFormat::Bullet);
|
||||
assert_eq!((a.levels[1].indent, a.levels[1].hanging), (72.0, 18.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracked_changes_round_trip() {
|
||||
use craft_wordmodel::{Revision, RevisionKind};
|
||||
let mut d = Document::new();
|
||||
d.revisions = vec![
|
||||
Revision::new(RevisionKind::Insert, "Ada", "2025-03-04T05:06:00Z"),
|
||||
Revision::new(RevisionKind::Delete, "Grace", "2025-03-05T10:20:00Z"),
|
||||
];
|
||||
let mut p = para("kept added removed");
|
||||
p.format(5, 10, &|c| c.ins = Some(0)).unwrap();
|
||||
p.format(11, 18, &|c| c.del = Some(1)).unwrap();
|
||||
d.body = vec![para_block(p)];
|
||||
let r = round(&d);
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
assert_eq!(q.plain_text(), "kept added removed");
|
||||
let ins = q.props_at(6).ins.and_then(|i| r.revisions.get(i as usize)).expect("an insertion");
|
||||
assert_eq!((ins.kind, ins.author.as_str(), ins.date.as_str()), (RevisionKind::Insert, "Ada", "2025-03-04T05:06:00Z"));
|
||||
let del = q.props_at(12).del.and_then(|i| r.revisions.get(i as usize)).expect("a deletion");
|
||||
assert_eq!((del.kind, del.author.as_str(), del.date.as_str()), (RevisionKind::Delete, "Grace", "2025-03-05T10:20:00Z"));
|
||||
assert_eq!(q.props_at(1).ins, None);
|
||||
assert_eq!(q.mark.ins, None);
|
||||
}
|
||||
|
||||
/// An inserted or deleted paragraph break (the paragraph mark's revision) round-trips too.
|
||||
#[test]
|
||||
fn tracked_paragraph_marks_round_trip() {
|
||||
use craft_wordmodel::{Revision, RevisionKind};
|
||||
let mut d = Document::new();
|
||||
d.revisions = vec![
|
||||
Revision::new(RevisionKind::Insert, "Ada", "2025-03-04T05:06:00Z"),
|
||||
Revision::new(RevisionKind::Delete, "Grace", "2025-03-05T10:20:00Z"),
|
||||
];
|
||||
let (mut inserted, mut deleted) = (para("inserted break"), para("deleted break"));
|
||||
inserted.mark.ins = Some(0);
|
||||
deleted.mark.del = Some(1);
|
||||
d.body = vec![para_block(inserted), para_block(deleted), para_block(para("last"))];
|
||||
let r = round(&d);
|
||||
let mark = |i: usize| r.body[i].as_para().unwrap().mark.clone();
|
||||
let ins = mark(0).ins.and_then(|i| r.revisions.get(i as usize)).expect("an inserted mark");
|
||||
assert_eq!((ins.kind, ins.author.as_str()), (RevisionKind::Insert, "Ada"));
|
||||
let del = mark(1).del.and_then(|i| r.revisions.get(i as usize)).expect("a deleted mark");
|
||||
assert_eq!((del.kind, del.author.as_str()), (RevisionKind::Delete, "Grace"));
|
||||
assert_eq!((mark(2).ins, mark(2).del), (None, None));
|
||||
assert_eq!(r.body[0].as_para().unwrap().props_at(1).ins, None, "the text itself isn't marked");
|
||||
}
|
||||
|
||||
/// A tracked insertion or deletion of a picture, a note reference or a field round-trips: the
|
||||
/// reader takes each anchor's revision marks from its CHPX.
|
||||
#[test]
|
||||
fn tracked_object_anchors_round_trip() {
|
||||
use craft_wordmodel::{Revision, RevisionKind};
|
||||
let mut d = Document::new();
|
||||
d.revisions = vec![
|
||||
Revision::new(RevisionKind::Insert, "Ada", "2025-03-04T05:06:00Z"),
|
||||
Revision::new(RevisionKind::Delete, "Grace", "2025-03-05T10:20:00Z"),
|
||||
];
|
||||
d.media.insert("pic.png".into(), Arc::new(png()));
|
||||
let f = d.add_part(PartKind::Footnote, vec![para_block(para("The footnote."))]);
|
||||
let inserted = CharProps { ins: Some(0), ..Default::default() };
|
||||
let deleted = CharProps { del: Some(1), ..Default::default() };
|
||||
let mut p = para("a b c d");
|
||||
let picture = InlineObject::Image {
|
||||
media: "pic.png".into(),
|
||||
w: 20.0,
|
||||
h: 10.0,
|
||||
alt: String::new(),
|
||||
float: Default::default(),
|
||||
crop: [0.0; 4],
|
||||
ole: None,
|
||||
fx: Default::default(),
|
||||
};
|
||||
let page = InlineObject::Field { instr: "PAGE".into(), result: "1".into(), locked: false, dirty: false, code: false, form: String::new() };
|
||||
let note = InlineObject::NoteRef { kind: NoteKind::Footnote, id: f, custom: String::new() };
|
||||
// Inserted back to front so the offsets stay put: picture after "a", note after "b",
|
||||
// field after "c" and an untracked field after "d".
|
||||
p.insert_object(7, page.clone(), &CharProps::default()).unwrap();
|
||||
p.insert_object(5, page, &inserted).unwrap();
|
||||
p.insert_object(3, note, &deleted).unwrap();
|
||||
p.insert_object(1, picture, &inserted).unwrap();
|
||||
d.body = vec![para_block(p)];
|
||||
let r = round(&d);
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
let anchors: Vec<(&str, Option<RevisionKind>, Option<RevisionKind>)> = q
|
||||
.text
|
||||
.match_indices('\u{FFFC}')
|
||||
.zip(&q.objects)
|
||||
.map(|((at, _), o)| {
|
||||
let props = q.props_of_char(at);
|
||||
let kind = |rev: Option<u32>| rev.and_then(|i| r.revisions.get(i as usize)).map(|rev| rev.kind);
|
||||
let name = match o {
|
||||
InlineObject::Image { .. } => "picture",
|
||||
InlineObject::NoteRef { .. } => "note",
|
||||
InlineObject::Field { .. } => "field",
|
||||
_ => "other",
|
||||
};
|
||||
(name, kind(props.ins), kind(props.del))
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
anchors,
|
||||
vec![
|
||||
("picture", Some(RevisionKind::Insert), None),
|
||||
("note", None, Some(RevisionKind::Delete)),
|
||||
("field", Some(RevisionKind::Insert), None),
|
||||
("field", None, None),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "the reader pairs bookmark ends through FBKF.ibkl once craft-libs#1817 is in"]
|
||||
fn overlapping_bookmarks_keep_their_ends() {
|
||||
// Outer starts first and ends last; inner ends before it: the ends sort apart from the starts.
|
||||
let mut p = para("abcdef");
|
||||
p.insert_object(6, InlineObject::BookmarkEnd { name: "Outer".into() }, &CharProps::default()).unwrap();
|
||||
p.insert_object(4, InlineObject::BookmarkEnd { name: "Inner".into() }, &CharProps::default()).unwrap();
|
||||
p.insert_object(2, InlineObject::BookmarkStart { name: "Inner".into() }, &CharProps::default()).unwrap();
|
||||
p.insert_object(0, InlineObject::BookmarkStart { name: "Outer".into() }, &CharProps::default()).unwrap();
|
||||
let r = round(&doc_of(vec![p.clone()]));
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
let marks = |p: &Paragraph| -> Vec<String> {
|
||||
p.objects
|
||||
.iter()
|
||||
.filter_map(|o| match o {
|
||||
InlineObject::BookmarkStart { name } => Some(format!("+{name}")),
|
||||
InlineObject::BookmarkEnd { name } => Some(format!("-{name}")),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
assert_eq!(marks(q), marks(&p));
|
||||
assert_eq!(q.text, p.text);
|
||||
}
|
||||
|
||||
/// What the writer writes for a character's vertical position: superscript and subscript as
|
||||
/// `sprmCIss`, a raised or lowered position as `sprmCHpsPos`, character spacing as
|
||||
/// `sprmCDxaSpace`. The reader reads them once craft-libs#1799 is in.
|
||||
#[test]
|
||||
#[ignore = "the reader reads sprmCIss, sprmCHpsPos and sprmCDxaSpace once craft-libs#1799 is in"]
|
||||
fn position_and_spacing_round_trip() {
|
||||
let mut p = para("sup sub up sp");
|
||||
p.format(0, 3, &|c| c.vert_align = Some(VertAlign::Superscript)).unwrap();
|
||||
p.format(4, 7, &|c| c.vert_align = Some(VertAlign::Subscript)).unwrap();
|
||||
p.format(8, 10, &|c| c.position = Some(3.0)).unwrap();
|
||||
p.format(11, 13, &|c| c.spacing = Some(1.5)).unwrap();
|
||||
let r = round(&doc_of(vec![p]));
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
assert_eq!(q.props_at(1).vert_align, Some(VertAlign::Superscript));
|
||||
assert_eq!(q.props_at(5).vert_align, Some(VertAlign::Subscript));
|
||||
assert_eq!(q.props_at(9).position, Some(3.0));
|
||||
assert_ne!(q.props_at(9).vert_align, Some(VertAlign::Superscript));
|
||||
assert_eq!(q.props_at(12).spacing, Some(1.5));
|
||||
}
|
||||
|
||||
/// Double strikethrough, emboss and the double-wave underline. The reader reads them once
|
||||
/// craft-libs#1969 is fixed.
|
||||
#[test]
|
||||
#[ignore = "the reader reads sprmCFDStrike, sprmCFEmboss and Kul 0x2B once craft-libs#1969 is fixed"]
|
||||
fn character_effects_round_trip() {
|
||||
let mut p = para("dbl emb");
|
||||
p.format(0, 3, &|c| c.double_strike = Some(true)).unwrap();
|
||||
p.format(4, 7, &|c| {
|
||||
c.emboss = Some(true);
|
||||
c.underline = Some(Underline::DoubleWave);
|
||||
})
|
||||
.unwrap();
|
||||
let r = round(&doc_of(vec![p]));
|
||||
let q = r.body[0].as_para().unwrap();
|
||||
assert_eq!(q.props_at(1).double_strike, Some(true));
|
||||
assert_eq!(q.props_at(5).emboss, Some(true));
|
||||
assert_eq!(q.props_at(5).underline, Some(Underline::DoubleWave));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "the reader reads sprmCHighlight as an Ico once craft-libs#1854 is in"]
|
||||
fn every_highlight_colour_round_trips() {
|
||||
for h in Highlight::ALL {
|
||||
let mut p = para("x");
|
||||
p.format(0, 1, &|c| c.highlight = Some(h)).unwrap();
|
||||
let r = round(&doc_of(vec![p]));
|
||||
assert_eq!(r.body[0].as_para().unwrap().props_at(0).highlight, Some(h), "{h:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A section's column count survives a save.
|
||||
#[test]
|
||||
fn section_columns_round_trip() {
|
||||
let mut d = Document::new();
|
||||
d.last_section.columns = Columns { count: 3, space: 18.0, ..Default::default() };
|
||||
d.body = vec![para_block(para("Three columns"))];
|
||||
let r = round(&d);
|
||||
assert_eq!(r.last_section.columns.count, 3);
|
||||
}
|
||||
|
||||
/// The page-number start and format, the column spacing, and the paragraph's shading, widow
|
||||
/// control and contextual spacing, as the writer writes them. Each needs a reader change that is
|
||||
/// still open: page numbers craft-libs#1354, column spacing and widow control craft-libs#1799,
|
||||
/// shading craft-libs#1808, contextual spacing craft-libs#1969.
|
||||
#[test]
|
||||
#[ignore = "needs the reader changes in craft-libs#1354, #1799 and #1808, and craft-libs#1969"]
|
||||
fn page_numbering_and_paragraph_shading_round_trip() {
|
||||
let mut d = Document::new();
|
||||
d.last_section.page_num_start = Some(5);
|
||||
d.last_section.page_num_format = craft_wordmodel::section::NumFormat::LowerRoman;
|
||||
d.last_section.columns = Columns { count: 3, space: 18.0, ..Default::default() };
|
||||
let mut p = para("Shaded");
|
||||
p.props.shading = Some(craft_wordmodel::Rgb(0xDD, 0xEE, 0xFF));
|
||||
p.props.widow_control = Some(false);
|
||||
p.props.contextual_spacing = Some(true);
|
||||
d.body = vec![para_block(p)];
|
||||
let r = round(&d);
|
||||
assert_eq!(r.last_section.page_num_start, Some(5));
|
||||
assert_eq!(r.last_section.page_num_format, craft_wordmodel::section::NumFormat::LowerRoman);
|
||||
assert_eq!((r.last_section.columns.count, r.last_section.columns.space), (3, 18.0));
|
||||
let q = &r.body[0].as_para().unwrap().props;
|
||||
assert_eq!(q.shading, Some(craft_wordmodel::Rgb(0xDD, 0xEE, 0xFF)));
|
||||
assert_eq!(q.widow_control, Some(false));
|
||||
assert_eq!(q.contextual_spacing, Some(true));
|
||||
}
|
||||
|
||||
fn table_of(grid: Vec<f32>, rows: Vec<Vec<Cell>>) -> Table {
|
||||
Table {
|
||||
props: TableProps::default(),
|
||||
grid,
|
||||
rows: rows.into_iter().map(|cells| Row { props: RowProps::default(), cells, controls: Default::default() }).collect(),
|
||||
controls: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_tables_become_paragraphs_of_their_cell() {
|
||||
let inner = table_of(vec![50.0, 50.0], vec![vec![cell("i1"), cell("i2")]]);
|
||||
let mut outer_cell = cell("before");
|
||||
outer_cell.blocks.push(Arc::new(Block::Table(inner)));
|
||||
let mut d = Document::new();
|
||||
d.body = vec![Arc::new(Block::Table(table_of(vec![200.0], vec![vec![outer_cell]]))), para_block(para("end"))];
|
||||
let r = round(&d);
|
||||
let t = r.body[0].as_table().expect("the outer table");
|
||||
let texts: Vec<String> = t.rows[0].cells[0].blocks.iter().filter_map(|b| b.as_para().map(|p| p.plain_text())).collect();
|
||||
assert_eq!(texts, vec!["before", "i1", "i2"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_table_ending_the_document_gets_a_paragraph_after_it() {
|
||||
let mut d = Document::new();
|
||||
d.body = vec![Arc::new(Block::Table(table_of(vec![100.0], vec![vec![cell("only")]])))];
|
||||
let r = round(&d);
|
||||
assert!(r.body[0].as_table().is_some());
|
||||
assert_eq!(texts(&r), vec![String::new()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rows_wider_than_the_format_keep_their_first_cells() {
|
||||
let cells: Vec<Cell> = (0..80).map(|i| cell(&format!("c{i}"))).collect();
|
||||
let mut d = Document::new();
|
||||
d.body = vec![Arc::new(Block::Table(table_of(vec![10.0; 80], vec![cells]))), para_block(para("after"))];
|
||||
// Never a panic; the first 63 cells (the format's most) keep their text.
|
||||
let r = round(&d);
|
||||
let all: Vec<String> = r
|
||||
.body
|
||||
.iter()
|
||||
.flat_map(|b| match &**b {
|
||||
Block::Para(p) => vec![p.plain_text()],
|
||||
Block::Table(t) => {
|
||||
t.rows.iter().flat_map(|r| r.cells.iter()).flat_map(|c| c.blocks.iter()).filter_map(|b| b.as_para().map(|p| p.plain_text())).collect()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
for i in [0, 30, 62, 79] {
|
||||
assert!(all.iter().any(|t| t == &format!("c{i}")), "c{i} missing: {all:?}");
|
||||
}
|
||||
assert_eq!(texts(&r).last().map(String::as_str), Some("after"));
|
||||
}
|
||||
|
||||
proptest::proptest! {
|
||||
#![proptest_config(proptest::prelude::ProptestConfig::with_cases(64))]
|
||||
/// Any text, with any run boundaries and formatting, writes without panicking and reads back
|
||||
/// as the same text.
|
||||
#[test]
|
||||
fn any_text_round_trips(lines in proptest::collection::vec("\\PC{0,40}", 1..8), bold in proptest::collection::vec(proptest::prelude::any::<bool>(), 8)) {
|
||||
let paras: Vec<Paragraph> = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, l)| {
|
||||
let clean: String = l.chars().filter(|c| !c.is_control() && *c != '\u{FFFC}').collect();
|
||||
let mut p = para(&clean);
|
||||
if bold.get(i).copied().unwrap_or(false) && !clean.is_empty() {
|
||||
let end = clean.char_indices().nth(clean.chars().count() / 2).map_or(clean.len(), |(i, _)| i);
|
||||
p.format(0, end, &|c| c.bold = Some(true)).unwrap();
|
||||
}
|
||||
p
|
||||
})
|
||||
.collect();
|
||||
let d = doc_of(paras);
|
||||
let r = round(&d);
|
||||
proptest::prop_assert_eq!(texts(&r), texts(&d));
|
||||
}
|
||||
}
|
||||
|
||||
/// The thin/thick compound border lines (`brcType` 0x0B-0x13) keep their style through a save,
|
||||
/// rather than coming back as single lines.
|
||||
#[test]
|
||||
fn compound_border_lines_round_trip() {
|
||||
let styles = [
|
||||
BorderStyle::ThinThickSmallGap,
|
||||
BorderStyle::ThickThinSmallGap,
|
||||
BorderStyle::ThinThickThinSmallGap,
|
||||
BorderStyle::ThinThickMediumGap,
|
||||
BorderStyle::ThickThinMediumGap,
|
||||
BorderStyle::ThinThickThinMediumGap,
|
||||
BorderStyle::ThinThickLargeGap,
|
||||
BorderStyle::ThickThinLargeGap,
|
||||
BorderStyle::ThinThickThinLargeGap,
|
||||
];
|
||||
let mut d = Document::new();
|
||||
d.body = styles
|
||||
.iter()
|
||||
.map(|&style| {
|
||||
let mut p = para("Bordered");
|
||||
p.props.borders = Some(Borders { bottom: Some(Border { style, width: 3.0, color: None, space: 1.0 }), ..Default::default() });
|
||||
para_block(p)
|
||||
})
|
||||
.collect();
|
||||
let r = round(&d);
|
||||
let got: Vec<_> = r.body.iter().filter_map(|b| b.as_para()).filter_map(|p| p.props.borders.as_ref()?.bottom.map(|b| b.style)).collect();
|
||||
assert_eq!(got, styles);
|
||||
}
|
||||
@@ -236,6 +236,11 @@ impl Package {
|
||||
self.opc.size()
|
||||
}
|
||||
|
||||
/// The entries of the zip that could not be read and were left out, by name.
|
||||
pub fn skipped(&self) -> &[String] {
|
||||
self.opc.skipped()
|
||||
}
|
||||
|
||||
/// A package from parts already in memory (a Flat OPC file, a converted document), by part
|
||||
/// name without the leading `/`.
|
||||
pub fn from_parts(files: BTreeMap<String, Vec<u8>>) -> Package {
|
||||
|
||||
@@ -373,6 +373,7 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize,
|
||||
}
|
||||
let (left, bytes, graphic) = (r.chunks.left, r.chunks.bytes, r.chunks.graphic);
|
||||
let mut doc = r.doc;
|
||||
doc.skipped_parts.extend(pkg.skipped().iter().cloned());
|
||||
doc.ensure_nonempty();
|
||||
Ok((doc, left, bytes, graphic))
|
||||
}
|
||||
|
||||
@@ -129,7 +129,14 @@ fn placeholder(out: &mut Vec<GraphicItem>, space: &El, theme: &[Rgb], w: f32, h:
|
||||
}
|
||||
let Some(ty) = plot.els().find(|e| e.name.ends_with("Chart")) else { return };
|
||||
backdrop(out, space, theme, w, h);
|
||||
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill: None, stroke: Some(AXIS), stroke_width: 0.75 });
|
||||
out.push(GraphicItem::Shape {
|
||||
rect: [0.0, 0.0, w, h],
|
||||
kind: ShapeKind::Rectangle,
|
||||
fill: None,
|
||||
stroke: Some(AXIS),
|
||||
stroke_width: 0.75,
|
||||
alpha: 1.0,
|
||||
});
|
||||
let mut area = Rect { x: PAD, y: PAD, w: (w - 2.0 * PAD).max(1.0), h: (h - 2.0 * PAD).max(1.0) };
|
||||
if let Some(t) = title_of(chart, &read_series(ty, false, theme)) {
|
||||
area = title_block(out, &t, area);
|
||||
@@ -150,7 +157,14 @@ pub(crate) fn chartex_items(space: Option<&El>, w: f32, h: f32) -> Vec<GraphicIt
|
||||
if !(w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
|
||||
return out;
|
||||
}
|
||||
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill: None, stroke: Some(AXIS), stroke_width: 0.75 });
|
||||
out.push(GraphicItem::Shape {
|
||||
rect: [0.0, 0.0, w, h],
|
||||
kind: ShapeKind::Rectangle,
|
||||
fill: None,
|
||||
stroke: Some(AXIS),
|
||||
stroke_width: 0.75,
|
||||
alpha: 1.0,
|
||||
});
|
||||
let mut area = Rect { x: PAD, y: PAD, w: (w - 2.0 * PAD).max(1.0), h: (h - 2.0 * PAD).max(1.0) };
|
||||
let chart = space.and_then(|s| s.child("cx:chart"));
|
||||
let title = chart.and_then(|c| c.child("cx:title")).and_then(|t| {
|
||||
|
||||
@@ -69,7 +69,14 @@ pub(super) fn stroke(out: &mut Vec<GraphicItem>, segs: Vec<PathSeg>, color: Rgb,
|
||||
}
|
||||
|
||||
fn swatch(out: &mut Vec<GraphicItem>, x: f32, y: f32, color: Rgb) {
|
||||
out.push(GraphicItem::Shape { rect: [x, y, SWATCH, SWATCH], kind: ShapeKind::Rectangle, fill: Some(color), stroke: None, stroke_width: 0.0 });
|
||||
out.push(GraphicItem::Shape {
|
||||
rect: [x, y, SWATCH, SWATCH],
|
||||
kind: ShapeKind::Rectangle,
|
||||
fill: Some(color),
|
||||
stroke: None,
|
||||
stroke_width: 0.0,
|
||||
alpha: 1.0,
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn seg(x0: f32, y0: f32, x1: f32, y1: f32) -> Vec<PathSeg> {
|
||||
|
||||
@@ -24,7 +24,7 @@ pub(super) fn backdrop(out: &mut Vec<GraphicItem>, space: &El, theme: &[Rgb], w:
|
||||
let stroke = ln.and_then(|l| fill_colour(l, theme));
|
||||
let stroke_width = ln.and_then(|l| l.attr("w")).and_then(|v| measure(v, 12_700.0)).unwrap_or(0.75).clamp(0.0, 20.0);
|
||||
if fill.is_some() || stroke.is_some() {
|
||||
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill, stroke, stroke_width });
|
||||
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill, stroke, stroke_width, alpha: 1.0 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,7 +544,7 @@ fn markers(out: &mut Vec<GraphicItem>, pts: impl Iterator<Item = (f32, f32)>, co
|
||||
let size = s.marker_size.filter(|v| v.is_finite()).unwrap_or(MARKER).clamp(2.0, 72.0);
|
||||
let h = size / 2.0;
|
||||
let shape = |out: &mut Vec<GraphicItem>, rect: [f32; 4], kind: ShapeKind| {
|
||||
out.push(GraphicItem::Shape { rect, kind, fill: Some(colour), stroke: None, stroke_width: 0.0 });
|
||||
out.push(GraphicItem::Shape { rect, kind, fill: Some(colour), stroke: None, stroke_width: 0.0, alpha: 1.0 });
|
||||
};
|
||||
let mut strokes = Vec::new();
|
||||
for (x, y) in pts {
|
||||
|
||||
@@ -96,7 +96,7 @@ pub(super) fn radar(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
for (i, p) in ring.iter().enumerate() {
|
||||
if c.radar_marks && s.values.get(i).copied().flatten().is_some() {
|
||||
let rect = [p.0 as f32 - MARKER / 2.0, p.1 as f32 - MARKER / 2.0, MARKER, MARKER];
|
||||
out.push(GraphicItem::Shape { rect, kind: ShapeKind::Ellipse, fill: Some(col), stroke: None, stroke_width: 0.0 });
|
||||
out.push(GraphicItem::Shape { rect, kind: ShapeKind::Ellipse, fill: Some(col), stroke: None, stroke_width: 0.0, alpha: 1.0 });
|
||||
}
|
||||
if let Some(text) = label_of(s, i) {
|
||||
labels.push(DataLabel { x: p.0 as f32, y: p.1 as f32, text, inside: false, pos: LabelPos::Above, off: 0.0 });
|
||||
|
||||
@@ -63,7 +63,7 @@ impl Reader<'_> {
|
||||
let chunk = AltChunk { content_type: &ct, part: &part, bytes };
|
||||
// A WordprocessingML chunk is a package; one that isn't Word's fails to read (logged).
|
||||
let is_docx = bytes.starts_with(b"PK\x03\x04");
|
||||
let doc = if is_docx { self.read_nested(bytes, depth) } else { self.chunks.import.and_then(|f| f(&chunk)) };
|
||||
let doc = if is_docx { self.read_nested(bytes, depth, &part) } else { self.chunks.import.and_then(|f| f(&chunk)) };
|
||||
let Some(mut doc) = doc else {
|
||||
log::warn!("docx: alternative format chunk {part} ({ct}) is in a format WordCraft can't bring in");
|
||||
return;
|
||||
@@ -83,6 +83,8 @@ impl Reader<'_> {
|
||||
// The pictures the chunk's own reader dropped are the file's too (strict export, #6005),
|
||||
// each named by the chunk and its part in it (`word/afchunk1.docx > word/header1.xml`).
|
||||
// A chunk in another format brings none: those importers record nothing yet (#6007).
|
||||
let skipped = std::mem::take(&mut doc.skipped_parts);
|
||||
self.doc.skipped_parts.extend(skipped.into_iter().map(|name| format!("{part} > {name}")));
|
||||
let nested = std::mem::take(&mut doc.missing_pictures);
|
||||
let list_len = nested.list.len() as u64;
|
||||
for mut item in nested.list {
|
||||
@@ -103,7 +105,7 @@ impl Reader<'_> {
|
||||
|
||||
/// A WordprocessingML chunk: its document, read with the same importer and what is left of
|
||||
/// the budgets, its XML depth counting on from `depth`.
|
||||
fn read_nested(&mut self, bytes: &[u8], depth: usize) -> Option<Document> {
|
||||
fn read_nested(&mut self, bytes: &[u8], depth: usize, part: &str) -> Option<Document> {
|
||||
if self.chunks.nesting >= MAX_CHUNK_NESTING {
|
||||
log::warn!("docx: alternative format chunks nest deeper than {MAX_CHUNK_NESTING}; the inner ones are left out");
|
||||
return None;
|
||||
@@ -112,8 +114,14 @@ impl Reader<'_> {
|
||||
Package::open_limited(bytes, MAX_PART, self.chunks.bytes).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?;
|
||||
self.chunks.bytes = self.chunks.bytes.saturating_sub(pkg.size());
|
||||
let state = ChunkState { nesting: self.chunks.nesting + 1, depth: depth + 1, ..self.chunks };
|
||||
let (doc, left, bytes, graphic) =
|
||||
super::read_opened_in(&pkg, state).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?;
|
||||
let (doc, left, bytes, graphic) = match super::read_opened_in(&pkg, state) {
|
||||
Ok(read) => read,
|
||||
Err(e) => {
|
||||
log::warn!("docx: unreadable nested document chunk: {e}");
|
||||
self.doc.skipped_parts.extend(pkg.skipped().iter().map(|name| format!("{part} > {name}")));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
self.chunks.left = left;
|
||||
self.chunks.bytes = bytes;
|
||||
self.chunks.graphic = graphic;
|
||||
|
||||
@@ -7,7 +7,7 @@ use craft_wordmodel::props::Rgb;
|
||||
|
||||
use super::Reader;
|
||||
use super::chart::text_w;
|
||||
use super::drawing_color::{color, paint};
|
||||
use super::drawing_color::{color, color_alpha, paint};
|
||||
use crate::package::{Rel, Rels, rel_is, rt};
|
||||
use crate::units::{MAX_LEN_PT, int};
|
||||
use crate::xml::El;
|
||||
@@ -164,7 +164,7 @@ fn shape(sp: &El, xf: Xf, b: [f64; 4], theme: &[Rgb], fit: f32, image: &mut dyn
|
||||
},
|
||||
};
|
||||
match geo {
|
||||
Geo::Kind(kind) => out.push(GraphicItem::Shape { rect, kind, fill, stroke, stroke_width }),
|
||||
Geo::Kind(kind) => out.push(GraphicItem::Shape { rect, kind, fill, stroke, stroke_width, alpha: fill_opacity(sppr, theme) }),
|
||||
Geo::Poly(pts, closed) if !pts.is_empty() => {
|
||||
// Flip in the box, then turn about its centre (clockwise, y down).
|
||||
let (s, c) = rot.to_radians().sin_cos();
|
||||
@@ -215,6 +215,12 @@ fn fill_of(sp: &El, sppr: Option<&El>, theme: &[Rgb]) -> Option<Rgb> {
|
||||
}
|
||||
}
|
||||
|
||||
/// How opaque a shape's own solid fill is, 0 to 1.
|
||||
fn fill_opacity(sppr: Option<&El>, theme: &[Rgb]) -> f32 {
|
||||
let solid = sppr.and_then(|p| p.child("a:solidFill"));
|
||||
solid.and_then(|f| color_alpha(f, theme, None)).map_or(1.0, |(_, t)| 1.0 - t / 100.0)
|
||||
}
|
||||
|
||||
/// A shape's outline colour and width in points (`a:ln`, else the style's line at 0.75 pt).
|
||||
fn line_of(sp: &El, sppr: Option<&El>, theme: &[Rgb]) -> (Option<Rgb>, f32) {
|
||||
let ln = sppr.and_then(|p| p.child("a:ln"));
|
||||
|
||||
@@ -345,7 +345,10 @@ pub(crate) const PROTECTION_HASH_ATTRS: [&str; 16] = [
|
||||
/// kept; a hostile attribute value can't be longer than a hash needs.
|
||||
fn read_protection(k: &El, s: &mut craft_wordmodel::Settings) {
|
||||
let on = |v: &str| !matches!(v, "0" | "false" | "off");
|
||||
if !k.attr("w:enforcement").is_some_and(on) {
|
||||
// Word enforces protection when `w:enforcement` is missing ([MS-OI29500] §2.1.402 (c)), the
|
||||
// opposite of the standard's literal reading, so only an explicit off value unlocks the
|
||||
// document; a file whose generator omits the attribute keeps its restriction and its hash.
|
||||
if k.attr("w:enforcement").is_some_and(|v| !on(v)) {
|
||||
return;
|
||||
}
|
||||
let edit = k.attr("w:edit").filter(|e| matches!(*e, "readOnly" | "comments" | "trackedChanges" | "forms"));
|
||||
|
||||
@@ -156,6 +156,7 @@ impl Sh {
|
||||
/// keep the theme's background colour so adjacent nodes remain separated.
|
||||
fn recolor(&mut self, spec: &SmartArtSpec, i: usize) {
|
||||
let Some(c) = spec.items.get(i).and_then(|it| it.color) else { return };
|
||||
self.alpha = Some(u32::from(spec.items.get(i).and_then(|it| it.opacity).unwrap_or(100).min(100)) * 1000);
|
||||
for p in [&mut self.fill, &mut self.line] {
|
||||
if let Paint::Accent(_, tint) = *p {
|
||||
*p = Paint::Rgb(c, tint);
|
||||
@@ -788,7 +789,14 @@ pub fn data_xml(spec: &SmartArtSpec, drawing_rel: &str) -> String {
|
||||
Some(c) => {
|
||||
w.open("dgm:spPr", &[]);
|
||||
w.open("a:solidFill", &[]);
|
||||
w.empty("a:srgbClr", &[("val", &c.hex())]);
|
||||
match it.opacity {
|
||||
Some(o) => {
|
||||
w.open("a:srgbClr", &[("val", &c.hex())]);
|
||||
w.empty("a:alpha", &[("val", &(u32::from(o.min(100)) * 1000).to_string())]);
|
||||
w.close("a:srgbClr");
|
||||
}
|
||||
None => w.empty("a:srgbClr", &[("val", &c.hex())]),
|
||||
}
|
||||
w.close("a:solidFill");
|
||||
w.close("dgm:spPr");
|
||||
}
|
||||
@@ -1114,7 +1122,11 @@ pub(crate) fn spec_of(data: &El) -> Option<SmartArtSpec> {
|
||||
let text: String = p.child("dgm:t")?.child("a:p")?.children("a:r").filter_map(|r| r.child("a:t")).map(|t| t.text()).collect();
|
||||
let color =
|
||||
p.child("dgm:spPr").and_then(|s| s.child("a:solidFill")).and_then(|f| f.child("a:srgbClr")).and_then(|c| Rgb::parse(c.attr("val")?));
|
||||
Some(SmartArtItem { text, level: (*l)?, color })
|
||||
let opacity = color.and_then(|_| {
|
||||
let a = p.child("dgm:spPr")?.child("a:solidFill")?.child("a:srgbClr")?.child("a:alpha")?;
|
||||
u8::try_from((a.attr("val")?.parse::<u32>().ok()? / 1000).min(100)).ok()
|
||||
});
|
||||
Some(SmartArtItem { text, level: (*l)?, color, opacity })
|
||||
})
|
||||
.collect::<Option<Vec<_>>>()?;
|
||||
let spec = SmartArtSpec { layout, items, colors }.sanitized();
|
||||
@@ -1256,3 +1268,50 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod opacity_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn an_items_opacity_is_drawn_and_read_back() {
|
||||
let mut spec = SmartArtSpec::sample(SmartArtLayout::Venn);
|
||||
spec.items[0].color = Some(Rgb(0x12, 0x34, 0x56));
|
||||
spec.items[0].opacity = Some(12);
|
||||
let drawing = drawing_xml(&spec, 400.0, 250.0);
|
||||
assert!(drawing.contains("<a:alpha val=\"12000\""), "{drawing}");
|
||||
let items = smart_art_items(&spec, &[], 400.0, 250.0);
|
||||
let GraphicItem::Shape { alpha, .. } = &items[0] else { panic!("shape first") };
|
||||
assert!((alpha - 0.12).abs() < 0.01, "{alpha}");
|
||||
}
|
||||
#[test]
|
||||
fn an_omitted_opacity_on_a_coloured_item_is_solid_and_hostile_values_write_clamped() {
|
||||
let mut spec = SmartArtSpec::sample(SmartArtLayout::Venn);
|
||||
spec.items[0].color = Some(Rgb(0x12, 0x34, 0x56));
|
||||
let items = smart_art_items(&spec, &[], 400.0, 250.0);
|
||||
let GraphicItem::Shape { alpha, .. } = &items[0] else { panic!("shape first") };
|
||||
assert!((alpha - 1.0).abs() < 0.01, "{alpha}");
|
||||
spec.items[0].opacity = Some(250);
|
||||
let drawing = drawing_xml(&spec, 400.0, 250.0);
|
||||
assert!(!drawing.contains("val=\"250000\""), "{drawing}");
|
||||
assert!(drawing.contains("val=\"100000\""), "{drawing}");
|
||||
}
|
||||
#[test]
|
||||
fn opacity_of_0_50_and_100_survives_and_hostile_values_are_clamped() {
|
||||
for pct in [0u8, 50, 100] {
|
||||
let mut spec = SmartArtSpec::sample(SmartArtLayout::Venn);
|
||||
spec.items[0].color = Some(Rgb(1, 2, 3));
|
||||
spec.items[0].opacity = Some(pct);
|
||||
let items = smart_art_items(&spec, &[], 400.0, 250.0);
|
||||
let GraphicItem::Shape { alpha, .. } = &items[0] else { panic!("shape first") };
|
||||
assert!((alpha - f32::from(pct) / 100.0).abs() < 0.01, "{pct}: {alpha}");
|
||||
}
|
||||
let mut spec = SmartArtSpec::sample(SmartArtLayout::Venn);
|
||||
spec.items[0].color = Some(Rgb(1, 2, 3));
|
||||
spec.items[0].opacity = Some(250);
|
||||
spec.items[1].opacity = Some(40);
|
||||
spec.sanitize();
|
||||
assert_eq!(spec.items[0].opacity, Some(100), "above 100 is clamped");
|
||||
assert_eq!(spec.items[1].opacity, None, "no colour, no opacity");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
mod bidi;
|
||||
mod embed;
|
||||
mod font_table;
|
||||
mod glossary;
|
||||
mod settings_merge;
|
||||
pub(crate) use settings_merge::has_unowned as settings_has_unowned;
|
||||
|
||||
@@ -102,12 +102,13 @@ impl<'d> EmbedWriter<'d> {
|
||||
}
|
||||
|
||||
/// Write the font table kept from a file, with the fonts it embeds, relating it from `rels`.
|
||||
pub fn font_table(&mut self, rels: &mut PartRels) {
|
||||
/// Whether there was one to write.
|
||||
pub fn font_table(&mut self, rels: &mut PartRels) -> bool {
|
||||
let mut written = Vec::new();
|
||||
let Some(root) = crate::package::font_table_key(self.passthrough).map(str::to_string) else { return };
|
||||
if let Some(name) = self.copy_tree(&root, &mut HashMap::new(), &mut written) {
|
||||
rels.add(rt::FONT_TABLE, &relative(STORY_PART, &name), false);
|
||||
}
|
||||
let Some(root) = crate::package::font_table_key(self.passthrough).map(str::to_string) else { return false };
|
||||
let Some(name) = self.copy_tree(&root, &mut HashMap::new(), &mut written) else { return false };
|
||||
rels.add(rt::FONT_TABLE, &relative(STORY_PART, &name), false);
|
||||
true
|
||||
}
|
||||
|
||||
/// Reserve the names of the media files this writer writes.
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
//! A font table (`word/fontTable.xml`, ECMA-376 Part 1 §17.8) for a document that brings none of
|
||||
//! its own: one built in code, or read from another format. It names each font the document
|
||||
//! uses with its generic family and pitch, so a reader without the font draws a like one (a sans
|
||||
//! for Arial, not its own serif default). A table read from a file is written back as it was
|
||||
//! instead ([`super::embed::EmbedWriter::font_table`]).
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use craft_wordmodel::font_kind::GenericFamily;
|
||||
use craft_wordmodel::props::CharProps;
|
||||
use craft_wordmodel::symbol_fonts::is_symbol_font;
|
||||
use craft_wordmodel::walk::each_para;
|
||||
use craft_wordmodel::{Document, Paragraph};
|
||||
|
||||
use crate::xml::W;
|
||||
|
||||
/// Most fonts the table names (hostile input: a font per run).
|
||||
const MAX_FONTS: usize = 1000;
|
||||
/// Longest font name it keeps, bytes (as the reader, `MAX_FONT_NAME`).
|
||||
const MAX_NAME: usize = 256;
|
||||
|
||||
/// Add the fonts `c` names to `out`, up to [`MAX_FONTS`].
|
||||
fn take<'a>(c: &'a CharProps, out: &mut BTreeSet<&'a str>) {
|
||||
for f in [&c.font, &c.font_ea, &c.font_cs, &c.sym_font].into_iter().flatten() {
|
||||
let f = f.trim();
|
||||
if out.len() < MAX_FONTS && !f.is_empty() && f.len() <= MAX_NAME {
|
||||
out.insert(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The fonts `doc` names, sorted: the document defaults', its styles' and list levels', and
|
||||
/// every run's and paragraph mark's in every story.
|
||||
fn used_fonts(doc: &Document) -> BTreeSet<&str> {
|
||||
let mut out = BTreeSet::new();
|
||||
take(&doc.styles.default_chr, &mut out);
|
||||
for s in &doc.styles.styles {
|
||||
take(&s.chr, &mut out);
|
||||
}
|
||||
for a in &doc.numbering.abstracts {
|
||||
for l in &a.levels {
|
||||
take(&l.chr, &mut out);
|
||||
}
|
||||
}
|
||||
for n in &doc.numbering.nums {
|
||||
for (_, l) in &n.level_overrides {
|
||||
take(&l.chr, &mut out);
|
||||
}
|
||||
}
|
||||
let stories = std::iter::once(&doc.body).chain(doc.parts.values().map(|p| &p.blocks));
|
||||
for b in stories.flat_map(|blocks| blocks.iter()) {
|
||||
if out.len() >= MAX_FONTS {
|
||||
break;
|
||||
}
|
||||
each_para(b, 0, &mut |p: &Paragraph| {
|
||||
take(&p.mark, &mut out);
|
||||
for r in &p.runs {
|
||||
take(&r.props, &mut out);
|
||||
}
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `word/fontTable.xml` for `doc`, or `None` when it names no font.
|
||||
pub(super) fn font_table_xml(doc: &Document) -> Option<Vec<u8>> {
|
||||
let fonts = used_fonts(doc);
|
||||
if fonts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut w = W::new();
|
||||
w.open(
|
||||
"w:fonts",
|
||||
&[
|
||||
("xmlns:w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"),
|
||||
("xmlns:r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"),
|
||||
],
|
||||
);
|
||||
for name in fonts {
|
||||
w.open("w:font", &[("w:name", name)]);
|
||||
if is_symbol_font(name) {
|
||||
// Symbol, Wingdings…: the symbol character set, so a reader maps their codes as the
|
||||
// font's own and doesn't substitute a text face.
|
||||
w.val("w:charset", "02");
|
||||
w.val("w:family", "auto");
|
||||
w.val("w:pitch", "variable");
|
||||
} else {
|
||||
// A family the file's own table named roman stays roman (Word draws it in a serif).
|
||||
let family =
|
||||
if doc.settings.roman_fonts.iter().any(|r| r.eq_ignore_ascii_case(name)) { GenericFamily::Roman } else { GenericFamily::of(name) };
|
||||
w.val("w:family", family.name());
|
||||
w.val("w:pitch", if family.fixed_pitch() { "fixed" } else { "variable" });
|
||||
}
|
||||
w.close("w:font");
|
||||
}
|
||||
w.close("w:fonts");
|
||||
Some(w.into_bytes())
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use super::{
|
||||
BTreeMap, Blocks, CT_WML, Document, DocxError, Flavor, HashMap, MAX_VBA_RELATED, PartRels, VBA_PROJECT_PART, VBA_RELATED, W, Writer, embed,
|
||||
glossary, rt, settings_merge, watermark, xml,
|
||||
};
|
||||
use crate::package::FONT_TABLE_PART;
|
||||
|
||||
/// The parts of the package [`write_as`] zips, in order, `[Content_Types].xml` first: (part name
|
||||
/// without the leading `/`, bytes).
|
||||
@@ -163,7 +164,13 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result<Vec<(String,
|
||||
PartRels::default(),
|
||||
);
|
||||
|
||||
wr.embeds.font_table(&mut rels);
|
||||
// The file's own font table, else one naming the fonts the document uses.
|
||||
if !wr.embeds.font_table(&mut rels)
|
||||
&& let Some(table) = super::font_table::font_table_xml(doc)
|
||||
{
|
||||
rels.add(rt::FONT_TABLE, "fontTable.xml", false);
|
||||
push_part(&mut entries, &mut overrides, FONT_TABLE_PART, table, &format!("{CT_WML}fontTable+xml"), PartRels::default());
|
||||
}
|
||||
|
||||
// Media referenced by some drawing (orphan parts would make the package suspect).
|
||||
let mut media_types: BTreeMap<String, &str> = BTreeMap::new();
|
||||
@@ -223,6 +230,11 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result<Vec<(String,
|
||||
}
|
||||
}
|
||||
|
||||
// Bibliography sources: Word looks for their data store among the main part's relationships,
|
||||
// not the package's, and shows no sources when it is related from `_rels/.rels`.
|
||||
if !doc.sources.is_empty() {
|
||||
rels.add(rt::CUSTOM_XML, "../customXml/item1.xml", false);
|
||||
}
|
||||
// Main part goes first in the zip after content types.
|
||||
entries.insert(0, ("word/document.xml".into(), body));
|
||||
overrides.insert(0, ("/word/document.xml".into(), flavor.main_content_type().into()));
|
||||
@@ -242,7 +254,6 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result<Vec<(String,
|
||||
overrides.push(("/docProps/custom.xml".into(), "application/vnd.openxmlformats-officedocument.custom-properties+xml".into()));
|
||||
}
|
||||
if !doc.sources.is_empty() {
|
||||
root.add(rt::CUSTOM_XML, "customXml/item1.xml", false);
|
||||
let mut item_rels = PartRels::default();
|
||||
item_rels.add(rt::CUSTOM_XML_PROPS, "itemProps1.xml", false);
|
||||
push_part(
|
||||
|
||||
@@ -48,7 +48,7 @@ fn package_styled(body: &str, chunk: Option<(&str, &str, &[u8])>, styles: Option
|
||||
files.push(("[Content_Types].xml".into(), types.into_bytes()));
|
||||
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
for (name, bytes) in files {
|
||||
zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
zw.start_file(name, zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored)).unwrap();
|
||||
zw.write_all(&bytes).unwrap();
|
||||
}
|
||||
zw.finish().unwrap().into_inner()
|
||||
@@ -76,6 +76,19 @@ fn a_chunk_comes_in_through_the_importer_where_it_stands() {
|
||||
assert_eq!(body_lines(&read(&bytes).unwrap()), ["Before", "After"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreadable_nested_main_part_is_reported_with_its_chunk() {
|
||||
let mut inner = package(&p("Chunk"), None);
|
||||
let document = inner.windows(b"<w:document".len()).position(|w| w == b"<w:document").unwrap();
|
||||
inner[document] ^= 1; // A bad ZIP checksum makes the required main part unreadable.
|
||||
let outer = package(&format!("{}{}", p("Before"), CHUNK), Some(("docx", DOCX_MAIN, &inner)));
|
||||
|
||||
let d = read(&outer).unwrap();
|
||||
|
||||
assert_eq!(d.skipped_parts, vec!["word/afchunk.docx > word/document.xml".to_string()]);
|
||||
assert_eq!(body_lines(&d), ["Before"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bookmark_opened_before_a_chunk_at_the_start_of_the_body_is_kept() {
|
||||
let body = format!(r#"<w:bookmarkStart w:id="0" w:name="Here"/>{CHUNK}<w:bookmarkEnd w:id="0"/>{}"#, p("After"));
|
||||
@@ -444,3 +457,15 @@ fn a_chunk_imported_twice_counts_its_dropped_pictures_past_the_list_once() {
|
||||
assert_eq!(d.missing_pictures.list.len(), craft_wordmodel::MAX_MISSING_PICTURES);
|
||||
assert_eq!(d.missing_pictures.total, n as u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_part_a_chunk_could_not_read_is_listed_with_the_chunk() {
|
||||
let bytes = package(&format!("{}{CHUNK}", p("Before")), Some(("htm", "application/xhtml+xml", b"<p>x</p>")));
|
||||
let import = |_: &AltChunk| {
|
||||
let mut d = Document::from_text("Chunk");
|
||||
d.skipped_parts = vec!["word/media/image1.png".to_string()];
|
||||
Some(d)
|
||||
};
|
||||
let d = read_with(&bytes, &ReadOptions { chunks: Some(&import), ..Default::default() }).unwrap();
|
||||
assert_eq!(d.skipped_parts, vec!["word/afchunk.htm > word/media/image1.png".to_string()]);
|
||||
}
|
||||
|
||||
@@ -221,6 +221,8 @@ const PINNED_PARTS: &[&str] = &[
|
||||
"word/styles.xml",
|
||||
"word/settings.xml",
|
||||
"word/theme/theme1.xml",
|
||||
// The fonts the document uses, with their families: it brings no font table of its own.
|
||||
"word/fontTable.xml",
|
||||
"docProps/core.xml",
|
||||
"docProps/app.xml",
|
||||
];
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
//! A document that brings no font table of its own (one built in code, or read from another
|
||||
//! format) gets one naming the fonts it uses, with their generic family and pitch: a reader
|
||||
//! without Arial then draws a sans, not its own serif default.
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::Read;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_wordmodel::props::CharProps;
|
||||
use craft_wordmodel::table::Table;
|
||||
use craft_wordmodel::{Block, Document, Paragraph};
|
||||
|
||||
fn unzip(bytes: &[u8]) -> BTreeMap<String, String> {
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
|
||||
let mut out = BTreeMap::new();
|
||||
for i in 0..z.len() {
|
||||
let mut f = z.by_index(i).unwrap();
|
||||
let mut s = String::new();
|
||||
if f.read_to_string(&mut s).is_ok() {
|
||||
out.insert(f.name().unwrap().to_string(), s);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The `w:font` element for `name` in `table`.
|
||||
fn font<'a>(table: &'a str, name: &str) -> &'a str {
|
||||
let open = format!("<w:font w:name=\"{name}\">");
|
||||
let at = table.find(&open).unwrap_or_else(|| panic!("no {name} in {table}"));
|
||||
let rest = &table[at..];
|
||||
&rest[..rest.find("</w:font>").unwrap()]
|
||||
}
|
||||
|
||||
fn append(p: &mut Paragraph, s: &str, props: &CharProps) {
|
||||
let at = p.len();
|
||||
p.insert_text(at, s, props).unwrap();
|
||||
}
|
||||
|
||||
fn run(text: &str, font: &str) -> Paragraph {
|
||||
Paragraph::with_text(text, CharProps { font: Some(font.into()), ..Default::default() })
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_built_document_names_its_fonts_with_their_family_and_pitch() {
|
||||
let mut d = Document::new();
|
||||
let mut table = Table::new(1, 1, 200.0);
|
||||
if let Some(c) = table.rows.first_mut().and_then(|r| r.cells.first_mut()) {
|
||||
c.blocks = vec![Arc::new(Block::Para(run("cell", "Courier New")))];
|
||||
}
|
||||
let mut symbol = Paragraph::new();
|
||||
append(&mut symbol, "\u{F0B7}", &CharProps { sym_font: Some("Symbol".into()), ..Default::default() });
|
||||
let mut serif = run("serif", "Times New Roman");
|
||||
append(&mut serif, " east", &CharProps { font_ea: Some("MS Mincho".into()), ..Default::default() });
|
||||
d.body =
|
||||
vec![Arc::new(Block::Para(run("sans", "Arial"))), Arc::new(Block::Table(table)), Arc::new(Block::Para(serif)), Arc::new(Block::Para(symbol))];
|
||||
let files = unzip(&craft_docx::write(&d).unwrap());
|
||||
let t = files.get("word/fontTable.xml").unwrap_or_else(|| panic!("no font table in {:?}", files.keys()));
|
||||
assert!(font(t, "Arial").contains(r#"<w:family w:val="swiss"/><w:pitch w:val="variable"/>"#), "{t}");
|
||||
assert!(font(t, "Times New Roman").contains(r#"<w:family w:val="roman"/>"#), "{t}");
|
||||
assert!(font(t, "Courier New").contains(r#"<w:family w:val="modern"/><w:pitch w:val="fixed"/>"#), "a table cell's font: {t}");
|
||||
assert!(font(t, "MS Mincho").contains("<w:family "), "an East Asian font: {t}");
|
||||
assert!(font(t, "Symbol").contains(r#"<w:charset w:val="02"/>"#), "a symbol font has the symbol set: {t}");
|
||||
// The built-in styles' fonts are there too.
|
||||
let body = d.styles.default_chr.font.clone().unwrap();
|
||||
font(t, &body);
|
||||
assert_eq!(t.matches(r#"<w:font w:name="Arial">"#).count(), 1, "each font once");
|
||||
// Related from the document and typed.
|
||||
assert!(
|
||||
files["word/_rels/document.xml.rels"].contains(r#"relationships/fontTable" Target="fontTable.xml""#),
|
||||
"{}",
|
||||
files["word/_rels/document.xml.rels"]
|
||||
);
|
||||
assert!(files["[Content_Types].xml"].contains("wordprocessingml.fontTable+xml"));
|
||||
// The reader takes the families back: Times New Roman is a serif.
|
||||
let back = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
|
||||
assert!(back.settings.roman_fonts.iter().any(|f| f == "Times New Roman"), "{:?}", back.settings.roman_fonts);
|
||||
assert!(!back.settings.roman_fonts.iter().any(|f| f == "Arial"));
|
||||
// Saved again, the table read back is written as it was.
|
||||
let again = unzip(&craft_docx::write(&back).unwrap());
|
||||
assert_eq!(again.get("word/fontTable.xml"), Some(t));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_font_table_family_wins_over_the_name() {
|
||||
// A file's table classes "Fancy Face" as roman: the generated table, written when that one is
|
||||
// gone, keeps it a serif.
|
||||
let mut d = Document::new();
|
||||
d.body = vec![Arc::new(Block::Para(run("x", "Fancy Face")))];
|
||||
d.settings.roman_fonts = vec!["Fancy Face".into()];
|
||||
let files = unzip(&craft_docx::write(&d).unwrap());
|
||||
assert!(font(&files["word/fontTable.xml"], "Fancy Face").contains(r#"<w:family w:val="roman"/>"#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_font_counts_and_names_are_bounded() {
|
||||
let mut d = Document::new();
|
||||
let mut p = Paragraph::new();
|
||||
for i in 0..5000 {
|
||||
append(&mut p, "x", &CharProps { font: Some(format!("Face {i}")), ..Default::default() });
|
||||
}
|
||||
append(&mut p, "y", &CharProps { font: Some("N".repeat(10_000)), ..Default::default() });
|
||||
d.body = vec![Arc::new(Block::Para(p))];
|
||||
let files = unzip(&craft_docx::write(&d).unwrap());
|
||||
let t = &files["word/fontTable.xml"];
|
||||
assert!(t.matches("<w:font ").count() <= 1000, "{}", t.matches("<w:font ").count());
|
||||
assert!(!t.contains(&"N".repeat(300)));
|
||||
}
|
||||
@@ -18,6 +18,7 @@ mod east_asian_lang;
|
||||
mod east_asian_layout;
|
||||
mod emphasis_marks;
|
||||
mod fixtures;
|
||||
mod font_table;
|
||||
mod glossary;
|
||||
mod heading_levels;
|
||||
mod kashida;
|
||||
|
||||
@@ -87,6 +87,31 @@ fn a_malformed_main_part_is_an_error_not_a_truncated_document() {
|
||||
assert_eq!(d.plain_text(craft_wordmodel::StoryRef::Body), "a <- b\nlast", "well-formed text still reads whole");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_part_with_a_bad_checksum_is_left_out_and_reported() {
|
||||
let body = format!("<w:document {W_NS}><w:body><w:p><w:r><w:t>kept</w:t></w:r></w:p></w:body></w:document>");
|
||||
let build = |extra: &[u8]| {
|
||||
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
zw.start_file("_rels/.rels", stored).unwrap();
|
||||
zw.write_all(ROOT_RELS.as_bytes()).unwrap();
|
||||
zw.start_file("word/document.xml", stored).unwrap();
|
||||
zw.write_all(body.as_bytes()).unwrap();
|
||||
zw.start_file("word/media/image1.png", stored).unwrap();
|
||||
zw.write_all(extra).unwrap();
|
||||
zw.finish().unwrap().into_inner()
|
||||
};
|
||||
let sound = read(&build(b"PICTURE-BYTES-0123456789")).unwrap();
|
||||
assert!(sound.skipped_parts.is_empty(), "a sound file reports nothing");
|
||||
// The entry is stored as is, so one changed byte fails its checksum when it is read.
|
||||
let mut z = build(b"PICTURE-BYTES-0123456789");
|
||||
let at = z.windows(7).position(|w| w == b"PICTURE").unwrap();
|
||||
z[at] = b'p';
|
||||
let damaged = read(&z).unwrap();
|
||||
assert_eq!(damaged.plain_text(craft_wordmodel::StoryRef::Body), "kept", "the rest still opens");
|
||||
assert_eq!(damaged.skipped_parts, vec!["word/media/image1.png".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deep_nesting_is_rejected() {
|
||||
let mut x = format!("<w:document {W_NS}><w:body>");
|
||||
|
||||
@@ -1897,8 +1897,21 @@ fn flat_opc_round_trip_equals_docx() {
|
||||
assert!(text.starts_with("<?xml") && text.contains("<?mso-application progid=\"Word.Document\"?>"));
|
||||
assert!(text.contains("pkg:name=\"/word/document.xml\"") && text.contains("<pkg:binaryData>"));
|
||||
assert_eq!(craft_docx::sniff_xml(&xml), craft_docx::XmlKind::FlatOpc);
|
||||
let back = craft_docx::read_xml(&xml).expect("read flat");
|
||||
assert_eq!(back, rt(&d));
|
||||
let mut back = craft_docx::read_xml(&xml).expect("read flat");
|
||||
// `pkg:xmlData` inlines a part without its XML declaration, so a kept XML part (the font
|
||||
// table) comes back without one; the markup is the same.
|
||||
let mut want = rt(&d);
|
||||
for doc in [&mut back, &mut want] {
|
||||
for v in doc.passthrough.values_mut() {
|
||||
if let Some(rest) =
|
||||
v.strip_prefix(b"<?xml").and_then(|r| r.iter().position(|&b| b == b'>').map(|e| r[e + 1..].trim_ascii_start().to_vec()))
|
||||
{
|
||||
*v = std::sync::Arc::new(rest);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(back.passthrough.contains_key("word/fontTable.xml"));
|
||||
assert_eq!(back, want);
|
||||
assert_eq!(back.media.len(), 1);
|
||||
}
|
||||
|
||||
@@ -2382,6 +2395,10 @@ fn bibliography_sources_round_trip() {
|
||||
|
||||
assert_bibliography_parts(&bytes);
|
||||
assert_bibliography_sources_round_trip(&bytes);
|
||||
// Saved again after reading, the store keeps one relationship, from the main part.
|
||||
let again = craft_docx::write(&craft_docx::read(&bytes).expect("read")).expect("write again");
|
||||
assert_bibliography_parts(&again);
|
||||
assert_eq!(craft_docx::read(&again).expect("read again").sources.len(), d.sources.len());
|
||||
}
|
||||
|
||||
/// #4848: a corporate author saves as Word's `b:Corporate`, in place of a name list (a role holds
|
||||
@@ -2423,10 +2440,14 @@ fn assert_bibliography_parts(bytes: &[u8]) {
|
||||
assert!(zip.by_name("customXml/itemProps1.xml").is_ok());
|
||||
assert!(zip.by_name("customXml/_rels/item1.xml.rels").is_ok());
|
||||
|
||||
// Word finds the sources' data store only through the main part's relationships.
|
||||
let mut rels_xml = String::new();
|
||||
std::io::Read::read_to_string(&mut zip.by_name("_rels/.rels").unwrap(), &mut rels_xml).unwrap();
|
||||
assert!(rels_xml.contains("relationships/customXml"), "{rels_xml}");
|
||||
assert!(rels_xml.contains("Target=\"customXml/item1.xml\""), "{rels_xml}");
|
||||
std::io::Read::read_to_string(&mut zip.by_name("word/_rels/document.xml.rels").unwrap(), &mut rels_xml).unwrap();
|
||||
assert_eq!(rels_xml.matches("relationships/customXml\"").count(), 1, "{rels_xml}");
|
||||
assert!(rels_xml.contains("Target=\"../customXml/item1.xml\""), "{rels_xml}");
|
||||
let mut root_xml = String::new();
|
||||
std::io::Read::read_to_string(&mut zip.by_name("_rels/.rels").unwrap(), &mut root_xml).unwrap();
|
||||
assert!(!root_xml.contains("relationships/customXml\""), "{root_xml}");
|
||||
|
||||
let mut ct_xml = String::new();
|
||||
std::io::Read::read_to_string(&mut zip.by_name("[Content_Types].xml").unwrap(), &mut ct_xml).unwrap();
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//! `w:documentProtection` (ECMA-376 §17.15.1.29): Word enforces protection when
|
||||
//! `w:enforcement` is missing ([MS-OI29500] §2.1.402 c), so the reader must keep the
|
||||
//! restriction — and, on save, its hash — rather than unlocking the document. Synthetic
|
||||
//! fixture written for this test (the test carried over from WordCraft's reader, which
|
||||
//! craft-docx replaced).
|
||||
|
||||
use std::io::{Cursor, Write};
|
||||
|
||||
const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#;
|
||||
const RT: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/";
|
||||
|
||||
fn fixture(protection: &str) -> Vec<u8> {
|
||||
let mut z = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let parts: [(&str, String); 3] = [
|
||||
("word/document.xml", format!(r#"<w:document {W_NS}><w:body><w:p><w:r><w:t>Locked</w:t></w:r></w:p></w:body></w:document>"#)),
|
||||
(
|
||||
"word/_rels/document.xml.rels",
|
||||
format!(
|
||||
r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="{RT}settings" Target="settings.xml"/></Relationships>"#
|
||||
),
|
||||
),
|
||||
("word/settings.xml", format!(r#"<w:settings {W_NS}>{protection}</w:settings>"#)),
|
||||
];
|
||||
for (name, text) in parts {
|
||||
z.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
z.write_all(text.as_bytes()).unwrap();
|
||||
}
|
||||
z.finish().unwrap().into_inner()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protection_without_enforcement_is_enforced_like_word() {
|
||||
let no_enforcement = r#"<w:documentProtection w:edit="readOnly" w:cryptAlgorithmSid="4" w:cryptSpinCount="100000" w:hash="AAAA" w:salt="BBBB"/>"#;
|
||||
let read = craft_docx::read_with_password(&fixture(no_enforcement), None).unwrap();
|
||||
assert_eq!(read.settings.protection.as_deref(), Some("readOnly"), "Word enforces protection with no w:enforcement");
|
||||
assert!(read.settings.protect_hash.iter().any(|(k, _)| k == "w:hash"), "the password hash is kept");
|
||||
let again = craft_docx::read_with_password(&craft_docx::write(&read).unwrap(), None).unwrap();
|
||||
assert_eq!(again.settings.protection.as_deref(), Some("readOnly"), "saving keeps the protection");
|
||||
assert!(again.settings.protect_hash.iter().any(|(k, _)| k == "w:hash"), "saving keeps the hash");
|
||||
// Explicitly off stays off (ignored but persisted, as the standard says).
|
||||
let off = craft_docx::read_with_password(&fixture(r#"<w:documentProtection w:edit="readOnly" w:enforcement="0"/>"#), None).unwrap();
|
||||
assert!(off.settings.protection.is_none());
|
||||
}
|
||||
@@ -1115,6 +1115,9 @@ fn flowchart_and_star_shapes(c: &Ctx) -> Option<Geometry> {
|
||||
Some(g)
|
||||
}
|
||||
|
||||
/// The range presetShapeDefinitions gives the elbows' handles (`minX` / `maxX`, `minY` / `maxY`).
|
||||
const ADJ_LIMIT: f64 = 2_147_483_647.0;
|
||||
|
||||
fn callout_wave_and_connector_shapes(c: &Ctx) -> Option<Geometry> {
|
||||
let name = c.name;
|
||||
let w = c.w;
|
||||
@@ -1197,28 +1200,30 @@ fn callout_wave_and_connector_shapes(c: &Ctx) -> Option<Geometry> {
|
||||
}
|
||||
// The elbows of ECMA-376's presetShapeDefinitions: 3 has one middle leg, 4 adds a
|
||||
// horizontal one at `adj2`, 5 a third vertical one at `adj3`. 4 and 5 were drawn as 3.
|
||||
// Their handles take the spec's whole range (its `ahXY` min and max), so dragging one
|
||||
// never clamps a value an imported route uses.
|
||||
"bentConnector3" => {
|
||||
let x1 = w * a(0) / 1e5;
|
||||
let l = Pb::new().m(0.0, 0.0).l(x1, 0.0).l(x1, h).l(w, h).take();
|
||||
let mut g = Geometry { paths: vec![stroke_only(l)], text_rect: full, handles: vec![], sites: vec![Point::ZERO, Point::new(w, h)] };
|
||||
g.handles.push(Handle::new(0, Point::new(0.0, h / 2.0), Vec2::new(w / 1e5, 0.0), a(0), -1e6, 1e6));
|
||||
g.handles.push(Handle::new(0, Point::new(0.0, h / 2.0), Vec2::new(w / 1e5, 0.0), a(0), -ADJ_LIMIT, ADJ_LIMIT));
|
||||
g
|
||||
}
|
||||
"bentConnector4" => {
|
||||
let (x1, y2) = (w * a(0) / 1e5, h * a(1) / 1e5);
|
||||
let l = Pb::new().m(0.0, 0.0).l(x1, 0.0).l(x1, y2).l(w, y2).l(w, h).take();
|
||||
let mut g = Geometry { paths: vec![stroke_only(l)], text_rect: full, handles: vec![], sites: vec![Point::ZERO, Point::new(w, h)] };
|
||||
g.handles.push(Handle::new(0, Point::new(0.0, y2 / 2.0), Vec2::new(w / 1e5, 0.0), a(0), -1e6, 1e6));
|
||||
g.handles.push(Handle::new(1, Point::new((x1 + w) / 2.0, 0.0), Vec2::new(0.0, h / 1e5), a(1), -1e6, 1e6));
|
||||
g.handles.push(Handle::new(0, Point::new(0.0, y2 / 2.0), Vec2::new(w / 1e5, 0.0), a(0), -ADJ_LIMIT, ADJ_LIMIT));
|
||||
g.handles.push(Handle::new(1, Point::new((x1 + w) / 2.0, 0.0), Vec2::new(0.0, h / 1e5), a(1), -ADJ_LIMIT, ADJ_LIMIT));
|
||||
g
|
||||
}
|
||||
"bentConnector5" => {
|
||||
let (x1, y2, x3) = (w * a(0) / 1e5, h * a(1) / 1e5, w * a(2) / 1e5);
|
||||
let l = Pb::new().m(0.0, 0.0).l(x1, 0.0).l(x1, y2).l(x3, y2).l(x3, h).l(w, h).take();
|
||||
let mut g = Geometry { paths: vec![stroke_only(l)], text_rect: full, handles: vec![], sites: vec![Point::ZERO, Point::new(w, h)] };
|
||||
g.handles.push(Handle::new(0, Point::new(0.0, y2 / 2.0), Vec2::new(w / 1e5, 0.0), a(0), -1e6, 1e6));
|
||||
g.handles.push(Handle::new(1, Point::new((x1 + x3) / 2.0, 0.0), Vec2::new(0.0, h / 1e5), a(1), -1e6, 1e6));
|
||||
g.handles.push(Handle::new(2, Point::new(0.0, (y2 + h) / 2.0), Vec2::new(w / 1e5, 0.0), a(2), -1e6, 1e6));
|
||||
g.handles.push(Handle::new(0, Point::new(0.0, y2 / 2.0), Vec2::new(w / 1e5, 0.0), a(0), -ADJ_LIMIT, ADJ_LIMIT));
|
||||
g.handles.push(Handle::new(1, Point::new((x1 + x3) / 2.0, 0.0), Vec2::new(0.0, h / 1e5), a(1), -ADJ_LIMIT, ADJ_LIMIT));
|
||||
g.handles.push(Handle::new(2, Point::new(0.0, (y2 + h) / 2.0), Vec2::new(w / 1e5, 0.0), a(2), -ADJ_LIMIT, ADJ_LIMIT));
|
||||
g
|
||||
}
|
||||
// The curved connectors follow the same legs with cubic Béziers.
|
||||
|
||||
@@ -452,6 +452,17 @@ fn the_three_and_four_bend_elbows_follow_their_adjust_values() {
|
||||
let g = build("bentConnector5", 200.0, 100.0, &[25000.0, 75000.0, 60000.0]).unwrap();
|
||||
let pos: Vec<(f64, f64)> = g.handles.iter().map(|h| (h.pos.x, h.pos.y)).collect();
|
||||
assert_eq!(pos, [(50.0, 37.5), (85.0, 75.0), (120.0, 87.5)]);
|
||||
// A handle dragged far out keeps the value: the spec's range is ±2147483647, and PowerPoint's
|
||||
// routing gives a thin connector box values well beyond ±1000000 (a leg 50 pt beside a 1 pt
|
||||
// wide box is 5000000).
|
||||
for name in ["bentConnector3", "bentConnector4", "bentConnector5"] {
|
||||
let g = build(name, 200.0, 100.0, &[]).unwrap();
|
||||
for h in &g.handles {
|
||||
for v in [5_000_000.0, -5_000_000.0] {
|
||||
assert_eq!(h.value_at(h.origin + h.dir * v), v, "{name} handle {}", h.index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// curvedConnector2 is one cubic (l,t)…(r,b) through (wd2,t) and (r,hd2); 4 and 5 bend at the same
|
||||
|
||||
@@ -146,6 +146,53 @@ pub fn color_channels<'a>(names: impl IntoIterator<Item = &'a str>) -> Option<(C
|
||||
best.or_else(|| (count == 1).then(|| (ColorLayout::Y, vec![0])))
|
||||
}
|
||||
|
||||
/// The main picture of a file by the OpenEXR convention, the one compositing applications show
|
||||
/// when a file is imported: the channels without a layer prefix, `R`, `G`, `B` (and `A` when
|
||||
/// present), of the first part that has all three; else the first part's unprefixed `Y` (and
|
||||
/// `A`). Named layers never stand in for it: a file of named layers only (Blender's
|
||||
/// `ViewLayer.Combined.R`, a part that stores `beauty.R`) has no main picture, and its layers
|
||||
/// are reached by name, as in After Effects (EXtractoR) and Nuke (the `rgba` layer stays empty).
|
||||
///
|
||||
/// `parts` gives each part's channel names as stored, in file order. Returns the part's index
|
||||
/// and the channels' indices in that part, in layout order (R, G, B, A or Y, A). The names are
|
||||
/// matched exactly, as the specification spells them; the first of duplicate names counts.
|
||||
pub fn main_picture<'a, P, C>(parts: P) -> Option<(usize, ColorLayout, Vec<usize>)>
|
||||
where
|
||||
P: IntoIterator<Item = C>,
|
||||
C: IntoIterator<Item = &'a str>,
|
||||
{
|
||||
let mut luminance: Option<(usize, ColorLayout, Vec<usize>)> = None;
|
||||
for (part, names) in parts.into_iter().enumerate() {
|
||||
let (mut r, mut g, mut b, mut a, mut y) = (None, None, None, None, None);
|
||||
for (index, name) in names.into_iter().enumerate() {
|
||||
let slot = match name {
|
||||
"R" => &mut r,
|
||||
"G" => &mut g,
|
||||
"B" => &mut b,
|
||||
"A" => &mut a,
|
||||
"Y" => &mut y,
|
||||
_ => continue,
|
||||
};
|
||||
slot.get_or_insert(index);
|
||||
}
|
||||
if let (Some(r), Some(g), Some(b)) = (r, g, b) {
|
||||
return Some(match a {
|
||||
Some(a) => (part, ColorLayout::Rgba, vec![r, g, b, a]),
|
||||
None => (part, ColorLayout::Rgb, vec![r, g, b]),
|
||||
});
|
||||
}
|
||||
if luminance.is_none()
|
||||
&& let Some(y) = y
|
||||
{
|
||||
luminance = Some(match a {
|
||||
Some(a) => (part, ColorLayout::YA, vec![y, a]),
|
||||
None => (part, ColorLayout::Y, vec![y]),
|
||||
});
|
||||
}
|
||||
}
|
||||
luminance
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
@@ -235,4 +282,46 @@ mod tests {
|
||||
assert_eq!(pick(&[]), None);
|
||||
assert_eq!(pick(&["r", "g", "b"]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_main_picture_is_the_unprefixed_colour_of_the_first_part_that_has_it() {
|
||||
let main = |parts: &[&[&str]]| main_picture(parts.iter().map(|p| p.iter().copied()));
|
||||
// One part: the bare channels, whatever else is there, also before them alphabetically.
|
||||
assert_eq!(
|
||||
main(&[&["A", "B", "CryptoMaterial00.A", "CryptoMaterial00.B", "CryptoMaterial00.G", "CryptoMaterial00.R", "G", "R", "Z"]]),
|
||||
Some((0, ColorLayout::Rgba, vec![7, 6, 1, 0]))
|
||||
);
|
||||
assert_eq!(main(&[&["B", "G", "R", "diffuse.A", "diffuse.B", "diffuse.G", "diffuse.R"]]), Some((0, ColorLayout::Rgb, vec![2, 1, 0])));
|
||||
// Named layers only (Blender multilayer): no main picture, however good a layer looks.
|
||||
assert_eq!(
|
||||
main(&[&[
|
||||
"ViewLayer.AO.B",
|
||||
"ViewLayer.AO.G",
|
||||
"ViewLayer.AO.R",
|
||||
"ViewLayer.Combined.A",
|
||||
"ViewLayer.Combined.B",
|
||||
"ViewLayer.Combined.G",
|
||||
"ViewLayer.Combined.R"
|
||||
]]),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
main(&[&["ViewLayer.Combined.A", "ViewLayer.Combined.B", "ViewLayer.Combined.G", "ViewLayer.Combined.R"], &["ViewLayer.Depth.Z"]]),
|
||||
None
|
||||
);
|
||||
// Multi-part: the first part with bare R, G and B, not the first part.
|
||||
assert_eq!(main(&[&["Z"], &["A", "B", "G", "R"], &["B", "G", "R"]]), Some((1, ColorLayout::Rgba, vec![3, 2, 1, 0])));
|
||||
assert_eq!(main(&[&["B", "G", "R"], &["A", "B", "G", "R"]]), Some((0, ColorLayout::Rgb, vec![2, 1, 0])));
|
||||
// Colour wins over an earlier luminance part; luminance when there is no colour.
|
||||
assert_eq!(main(&[&["Y"], &["B", "G", "R"]]), Some((1, ColorLayout::Rgb, vec![2, 1, 0])));
|
||||
assert_eq!(main(&[&["Z"], &["A", "Y"], &["Y"]]), Some((1, ColorLayout::YA, vec![1, 0])));
|
||||
// Incomplete colour, other spellings and lone data channels are no picture.
|
||||
assert_eq!(main(&[&["G", "R"]]), None);
|
||||
assert_eq!(main(&[&["b", "g", "r"]]), None);
|
||||
assert_eq!(main(&[&["Z"]]), None);
|
||||
assert_eq!(main(&[&["A"]]), None);
|
||||
assert_eq!(main(&[]), None);
|
||||
// The first of duplicate names counts.
|
||||
assert_eq!(main(&[&["R", "G", "B", "R"]]), Some((0, ColorLayout::Rgb, vec![0, 1, 2])));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,8 +281,9 @@ pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result<MetaData> {
|
||||
return Err(Error::LimitExceeded(format!("{total} bytes across {} parts exceed {}", meta.headers.len(), limits.max_bytes)));
|
||||
}
|
||||
// A flat part's samples are allocated before its chunks are read, so the file must be able
|
||||
// to hold the flat parts' samples. DWA's run-length channels are zlib-packed on top and reach
|
||||
// about 132000:1, so DWA parts are counted at a quarter. Deep parts size their samples from
|
||||
// to hold the flat parts' samples. DWA's run-length channels are zlib-packed on top (RLE up to
|
||||
// 64:1, zlib up to about 1032:1, about 66000:1 together), so DWA parts are counted at a quarter
|
||||
// (with room to spare). Deep parts size their samples from
|
||||
// the file's own counts and are not counted here.
|
||||
let declared = flat.saturating_sub(dwa).saturating_add(dwa / 4);
|
||||
let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20);
|
||||
@@ -308,7 +309,7 @@ fn text_attributes(header: &Header) -> Vec<(String, String)> {
|
||||
out
|
||||
}
|
||||
|
||||
fn part_info(index: usize, header: &Header) -> PartInfo {
|
||||
pub(crate) fn part_info(index: usize, header: &Header) -> PartInfo {
|
||||
let (w, h) = window_size(header.layer_size);
|
||||
let display = &header.shared_attributes.display_window;
|
||||
let (dw, dh) = window_size(display.size);
|
||||
@@ -449,6 +450,7 @@ pub fn decode_display_part(
|
||||
header_limits.max_pixels = u64::MAX;
|
||||
header_limits.max_bytes = u64::MAX;
|
||||
let meta = read_meta(bytes, &header_limits)?;
|
||||
check_dwa_chunks(bytes, &meta)?;
|
||||
let info = meta.headers.get(index).map(|header| part_info(index, header)).ok_or_else(|| malformed(format!("part {index}")))?;
|
||||
if info.deep {
|
||||
return Err(Error::Unsupported("deep data (use the deep reader)".into()));
|
||||
@@ -756,6 +758,16 @@ fn tables_start(bytes: &[u8]) -> Result<u64> {
|
||||
Ok(cursor.position())
|
||||
}
|
||||
|
||||
/// Checks the chunks of the file's DWAA/DWAB parts before the `exr` crate decompresses them (see
|
||||
/// [`crate::dwa_guard`]: a few bytes of counters can make it allocate terabytes and abort).
|
||||
fn check_dwa_chunks(bytes: &[u8], meta: &MetaData) -> Result<()> {
|
||||
if !meta.headers.iter().any(|h| crate::dwa_guard::is_dwa(h.compression)) {
|
||||
return Ok(());
|
||||
}
|
||||
let tables = usize::try_from(tables_start(bytes)?).map_err(|_| malformed("header size"))?;
|
||||
crate::dwa_guard::check_file(bytes, meta, tables).map_err(malformed)
|
||||
}
|
||||
|
||||
/// Stable dense ordinal without materializing every tile. The library's block iterator
|
||||
/// internally allocates a Vec of all coordinates, which is unsuitable for cropped reads.
|
||||
pub(super) fn flat_block_ordinal(header: &Header, tile: TileCoordinates) -> Result<usize> {
|
||||
@@ -944,6 +956,7 @@ fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &m
|
||||
/// [`Error::Unsupported`]; the file's other parts may be deep.
|
||||
pub fn decode_part(bytes: &[u8], index: usize, keep: &dyn Fn(&str) -> bool, limits: &Limits) -> Result<Part> {
|
||||
let meta = read_meta(bytes, limits)?;
|
||||
check_dwa_chunks(bytes, &meta)?;
|
||||
let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?;
|
||||
if header.deep {
|
||||
return Err(Error::Unsupported("deep data (use the deep reader)".into()));
|
||||
|
||||
@@ -354,13 +354,13 @@ fn mixed_headers_with_deep_size(
|
||||
}
|
||||
|
||||
/// Sample `i` of a flat channel: `R` is 1, 2, 3, …, `G` 10 more and `B` 100 more, top row first.
|
||||
fn mixed_flat(channel: f32, i: usize) -> f32 {
|
||||
pub(super) fn mixed_flat(channel: f32, i: usize) -> f32 {
|
||||
channel + 1.0 + i as f32
|
||||
}
|
||||
|
||||
/// A mixed file with a 3 × 2 flat part, and its chunk offsets as `offsets[part][chunk]`. The
|
||||
/// tiled flat part's chunks hold the tiles at (0, 0) and (1, 0).
|
||||
fn mixed_file(deep_first: bool, tiled: bool) -> (Vec<u8>, [[usize; 2]; 2]) {
|
||||
pub(super) fn mixed_file(deep_first: bool, tiled: bool) -> (Vec<u8>, [[usize; 2]; 2]) {
|
||||
let (w, h) = (3usize, 2usize);
|
||||
let mut out = mixed_headers(deep_first, tiled, (w, h));
|
||||
let chunks: Vec<Vec<Vec<u8>>> = (0..2i32)
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
//! A check of DWAA/DWAB chunks before the `exr` crate decompresses them.
|
||||
//!
|
||||
//! A DWA payload starts with eleven little-endian `u64` counters, which the `exr` crate (1.74.3,
|
||||
//! `src/compression/dwa/chunk_header.rs`, `DwaHeader::parse`) reads in this order: version,
|
||||
//! unknown uncompressed size, unknown compressed size, AC compressed size, DC compressed size,
|
||||
//! RLE compressed size, RLE uncompressed size, RLE raw size, AC count, DC count, AC compression.
|
||||
//! The crate checks them only against `i64::MAX`, then inflates sections with these sizes as the
|
||||
//! size hint (`src/compression/dwa/section_stream.rs`: `inflate(section, expected)` with the
|
||||
//! unknown uncompressed size, `ac_count * 2` for deflated AC, `dc_count * 2`, and the RLE
|
||||
//! uncompressed size), and its inflater allocates the hint up front. A 100-byte chunk declaring
|
||||
//! 2^44 DC values makes it allocate 32 TiB: the process aborts, which `catch_unwind` can't stop.
|
||||
//!
|
||||
//! What a valid chunk declares is bounded by its block (`width × height` pixels of all the
|
||||
//! part's channels, `raw` bytes uncompressed):
|
||||
//! - the unknown (losslessly zipped) channels and the RLE channels are some of the block's
|
||||
//! channels stored planar, so their uncompressed sizes are at most `raw`;
|
||||
//! - the RLE token stream adds at most one count byte per 127 literal bytes (runs of three or
|
||||
//! more shrink), so it stays within `2 × raw`, a bound that leaves room for any encoder;
|
||||
//! - the lossy channels are cut into 8 × 8 DCT blocks over the block padded to multiples of 8:
|
||||
//! one DC value and at most 63 AC values per DCT block and channel.
|
||||
|
||||
use exr::block::chunk::{CompressedBlock, CompressedScanLineBlock, CompressedTileBlock, TileCoordinates};
|
||||
use exr::math::Vec2;
|
||||
use exr::meta::header::Header;
|
||||
use exr::prelude::Compression;
|
||||
|
||||
/// The counters' bytes at the start of a DWA payload.
|
||||
const COUNTERS: usize = 11 * 8;
|
||||
|
||||
/// Whether chunks of `compression` go through this check.
|
||||
pub(crate) fn is_dwa(compression: Compression) -> bool {
|
||||
matches!(compression, Compression::DWAA(_) | Compression::DWAB(_))
|
||||
}
|
||||
|
||||
/// What the check needs of a DWA part: the bytes of one pixel of all its channels, and how many
|
||||
/// channels it has. `None` when the pixel's bytes overflow.
|
||||
fn shape(header: &Header) -> Option<(usize, usize)> {
|
||||
let pixel_bytes = header.channels.list.iter().map(|c| c.sample_type.bytes_per_sample()).try_fold(0usize, usize::checked_add)?;
|
||||
Some((pixel_bytes, header.channels.list.len()))
|
||||
}
|
||||
|
||||
/// Checks the DWA payload `payload` of a block of `width × height` pixels of the part `header`
|
||||
/// describes: an error message when a counter declares more than a valid chunk of that block
|
||||
/// can hold. A payload as long as the block's raw bytes is stored uncompressed (the crate doesn't
|
||||
/// read counters then); one shorter than the counters is left to the crate, which refuses it.
|
||||
pub(crate) fn check_payload(header: &Header, size: (usize, usize), payload: &[u8]) -> Result<(), String> {
|
||||
if !is_dwa(header.compression) {
|
||||
return Ok(());
|
||||
}
|
||||
check_shaped(shape(header).ok_or("a DWA pixel size overflows")?, size, payload)
|
||||
}
|
||||
|
||||
/// [`check_payload`] with the part's [`shape`] worked out already.
|
||||
fn check_shaped((pixel_bytes, channels): (usize, usize), (width, height): (usize, usize), payload: &[u8]) -> Result<(), String> {
|
||||
let raw = pixel_bytes.checked_mul(width).and_then(|b| b.checked_mul(height)).ok_or("a DWA block size overflows")?;
|
||||
if payload.len() == raw {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(counters) = payload.get(..COUNTERS) else { return Ok(()) };
|
||||
let counter = |i: usize| counters.get(i * 8..i * 8 + 8).and_then(|b| <[u8; 8]>::try_from(b).ok()).map_or(u64::MAX, u64::from_le_bytes);
|
||||
let dct_blocks = width.div_ceil(8).checked_mul(height.div_ceil(8)).and_then(|n| n.checked_mul(channels)).ok_or("a DWA block size overflows")?;
|
||||
let raw = raw as u64;
|
||||
let bounds: [(usize, &str, u64); 5] = [
|
||||
(1, "unknown uncompressed size", raw),
|
||||
(6, "RLE uncompressed size", raw.saturating_mul(2)),
|
||||
(7, "RLE raw size", raw),
|
||||
(8, "AC count", (dct_blocks as u64).saturating_mul(63)),
|
||||
(9, "DC count", dct_blocks as u64),
|
||||
];
|
||||
for (i, name, most) in bounds {
|
||||
let value = counter(i);
|
||||
// (also keeps `count * 2` within a 32-bit usize: the bounds are block sizes, which fit)
|
||||
if value > most || usize::try_from(value.saturating_mul(2)).is_err() {
|
||||
return Err(format!("a DWA chunk declares a {name} of {value}, more than its {width} × {height} block can hold ({most})"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The pixel size of the block the `exr` crate decompresses a chunk of the part `header`
|
||||
/// describes into, from the chunk's coordinates: the crate's own placement
|
||||
/// (`Header::get_block_data_indices`, then `get_absolute_block_pixel_coordinates`), so a scan-line
|
||||
/// `y` the crate rounds into a block (it divides toward zero: rows just above the data window land
|
||||
/// in block 0) is sized as that block. An error where the crate can't place the chunk.
|
||||
fn block_size(header: &Header, block: &CompressedBlock) -> Result<(usize, usize), String> {
|
||||
if let CompressedBlock::Tile(tile) = block {
|
||||
// (the crate's level size asserts on levels of 64 and more; no valid file has 32)
|
||||
if tile.coordinates.level_index.0 >= 32 || tile.coordinates.level_index.1 >= 32 {
|
||||
return Err("a DWA tile of an impossible level".into());
|
||||
}
|
||||
}
|
||||
let tile = header.get_block_data_indices(block).map_err(|e| format!("a DWA chunk the decoder can't place: {e}"))?;
|
||||
let bounds = header.get_absolute_block_pixel_coordinates(tile).map_err(|e| format!("a DWA chunk the decoder can't place: {e}"))?;
|
||||
Ok((bounds.size.0, bounds.size.1))
|
||||
}
|
||||
|
||||
/// Checks every chunk the offset tables point at that is, or names, a flat DWA part (`tables` is
|
||||
/// where the offset tables start), before the `exr` crate's whole-image reader, which offers no
|
||||
/// hook between reading a chunk and decompressing it, gets the bytes. It fails closed: a chunk of
|
||||
/// a DWA part whose part number, coordinates, size or payload can't be read, or whose block can't
|
||||
/// be placed, is an error, not left to the crate.
|
||||
pub(crate) fn check_file(bytes: &[u8], meta: &exr::meta::MetaData, tables: usize) -> Result<(), String> {
|
||||
if !meta.headers.iter().any(|h| !h.deep && is_dwa(h.compression)) {
|
||||
return Ok(());
|
||||
}
|
||||
let multipart = meta.requirements.is_multilayer();
|
||||
let word = |at: usize| bytes.get(at..at.checked_add(4)?).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes);
|
||||
let cut = || "a DWA chunk cut off by the end of the file".to_string();
|
||||
// Per part: its shape when it is a flat DWA part (worked out once, not per chunk).
|
||||
let shapes: Vec<Option<(usize, usize)>> = meta
|
||||
.headers
|
||||
.iter()
|
||||
.map(|h| if !h.deep && is_dwa(h.compression) { shape(h).ok_or("a DWA pixel size overflows").map(Some) } else { Ok(None) })
|
||||
.collect::<Result<_, _>>()?;
|
||||
let mut table = tables;
|
||||
for (table_part, header) in meta.headers.iter().enumerate() {
|
||||
let entries =
|
||||
bytes.get(table..table.saturating_add(header.chunk_count.saturating_mul(8))).ok_or("the offset tables run past the end of the file")?;
|
||||
table = table.saturating_add(header.chunk_count.saturating_mul(8));
|
||||
for entry in entries.as_chunks::<8>().0 {
|
||||
let offset = usize::try_from(u64::from_le_bytes(*entry)).ok();
|
||||
// The part the crate decodes the chunk as: the one it names (multi-part files).
|
||||
let (part, mut at) = match (multipart, offset) {
|
||||
(false, Some(at)) => (Some(table_part), at),
|
||||
(true, Some(at)) => (word(at).and_then(|p| usize::try_from(p).ok()), at.saturating_add(4)),
|
||||
(_, None) => (None, 0),
|
||||
};
|
||||
let named = part.and_then(|p| shapes.get(p).copied().flatten().map(|s| (p, s)));
|
||||
let Some((part, shape)) = named else {
|
||||
// (not a DWA chunk, unless it sits in a DWA part's table and can't be read)
|
||||
if shapes.get(table_part).copied().flatten().is_some() && part.is_none_or(|p| p >= meta.headers.len()) {
|
||||
return Err(cut());
|
||||
}
|
||||
continue;
|
||||
};
|
||||
let header = meta.headers.get(part).ok_or_else(cut)?;
|
||||
let block = match header.blocks {
|
||||
exr::meta::BlockDescription::ScanLines => {
|
||||
let y = word(at).ok_or_else(cut)?;
|
||||
at = at.saturating_add(4);
|
||||
CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: Vec::new() })
|
||||
}
|
||||
exr::meta::BlockDescription::Tiles(_) => {
|
||||
let tile = [word(at), word(at.saturating_add(4)), word(at.saturating_add(8)), word(at.saturating_add(12))];
|
||||
at = at.saturating_add(16);
|
||||
let [Some(x), Some(y), Some(lx), Some(ly)] = tile else { return Err(cut()) };
|
||||
let index = |v: i32| usize::try_from(v).map_err(|_| "a DWA tile at a negative position".to_string());
|
||||
let coordinates = TileCoordinates { tile_index: Vec2(index(x)?, index(y)?), level_index: Vec2(index(lx)?, index(ly)?) };
|
||||
CompressedBlock::Tile(CompressedTileBlock { coordinates, compressed_pixels_le: Vec::new() })
|
||||
}
|
||||
};
|
||||
let size = block_size(header, &block)?;
|
||||
let len = word(at).ok_or_else(cut)?;
|
||||
let len = usize::try_from(len).map_err(|_| "a DWA chunk declaring a negative size".to_string())?;
|
||||
let start = at.saturating_add(4);
|
||||
let payload = bytes.get(start..start.saturating_add(len)).ok_or_else(cut)?;
|
||||
check_shaped(shape, size, payload)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
//! - [`decode`]: bounded part listing, flat decoding (scan lines and tiles, every compression the
|
||||
//! `exr` crate reads) and writing, on top of the `exr` crate.
|
||||
//! - [`deep`]: deep scan lines and tiles (NONE, RLE, ZIPS) and their flattening.
|
||||
//! - [`seek`]: any channels, display rows and reduced resolution of a file's flat parts, read from
|
||||
//! a seekable source and delivered band by band (or collected into planes).
|
||||
//! - [`stream`]: one flat scan-line part read block by block, for pictures larger than memory.
|
||||
//! - [`cryptomatte_samples`]: a file's Cryptomatte layers and their per-pixel (ID, coverage) ranks.
|
||||
|
||||
@@ -36,4 +38,10 @@ pub mod deep;
|
||||
#[cfg(all(test, feature = "decode"))]
|
||||
mod deep_tests;
|
||||
#[cfg(feature = "decode")]
|
||||
mod dwa_guard;
|
||||
#[cfg(feature = "decode")]
|
||||
pub mod seek;
|
||||
#[cfg(all(test, feature = "decode"))]
|
||||
mod seek_tests;
|
||||
#[cfg(feature = "decode")]
|
||||
pub mod stream;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -262,6 +262,9 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
}
|
||||
self.source.read_exact(bytes).map_err(invalid)?;
|
||||
}
|
||||
// (a DWA chunk's counters can make the crate allocate terabytes: see `crate::dwa_guard`)
|
||||
let lines = self.rows.min((self.info.height as usize).saturating_sub(row as usize));
|
||||
crate::dwa_guard::check_payload(header, (self.info.width as usize, lines), &packed).map_err(invalid)?;
|
||||
let chunk = Chunk {
|
||||
layer_index: 0,
|
||||
compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }),
|
||||
|
||||
@@ -18,11 +18,7 @@ pub(super) fn uses_kind(blocks: &[FBlock], k: Kind, depth: usize) -> bool {
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Document → Flow
|
||||
|
||||
/// Is this a monospace family name?
|
||||
pub fn is_mono(font: &str) -> bool {
|
||||
let f = font.to_ascii_lowercase();
|
||||
f.contains("courier") || f.contains("mono") || f.contains("consolas") || f.contains("menlo") || f.contains("code") || f == "monaco"
|
||||
}
|
||||
pub use craft_wordmodel::font_kind::is_mono;
|
||||
|
||||
fn is_code_style(doc: &Document, id: &str) -> bool {
|
||||
let names = |s: &str| {
|
||||
@@ -201,7 +197,7 @@ fn para_extra(doc: &Document, p: &craft_wordmodel::props::ParaProps) -> Option<B
|
||||
let outline_level = p.outline_level.or_else(|| style.as_ref().and_then(|_| doc.styles.resolve_para(p).outline_level));
|
||||
let x = craft_wordmodel::props::ParaProps {
|
||||
tabs,
|
||||
shading: p.shading,
|
||||
shading: doc.styles.resolve_para(p).shading,
|
||||
style,
|
||||
contextual_spacing: p.contextual_spacing,
|
||||
bidi: p.bidi.filter(|b| *b),
|
||||
@@ -553,6 +549,12 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec<Para> {
|
||||
if c == PAGE_BREAK {
|
||||
if !hidden {
|
||||
out.push_text(&std::mem::take(&mut buf), &f);
|
||||
let extra = out.extra.as_ref().and_then(|x| {
|
||||
let mut x = (**x).clone();
|
||||
x.drop_cap = None;
|
||||
x.drop_cap_distance = None;
|
||||
(x != craft_wordmodel::props::ParaProps::default()).then(|| Box::new(x))
|
||||
});
|
||||
let next = Para {
|
||||
kind,
|
||||
list: None,
|
||||
@@ -567,7 +569,7 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec<Para> {
|
||||
mark_ins: None,
|
||||
mark_del: None,
|
||||
fmt_change: None,
|
||||
extra: None,
|
||||
extra,
|
||||
mark: None,
|
||||
kept: None,
|
||||
wrap: None,
|
||||
@@ -765,9 +767,14 @@ fn graphic_group(
|
||||
.iter()
|
||||
.take(10_000)
|
||||
.filter_map(|item| match item {
|
||||
GraphicItem::Shape { rect, kind, fill, stroke, stroke_width } if finite(rect) => {
|
||||
GraphicItem::Shape { rect, kind, fill, stroke, stroke_width, alpha } if finite(rect) => {
|
||||
let look = BoxLook {
|
||||
fill: fill.map(craft_wordmodel::fill::Fill::solid).filter(|_| *kind != ShapeKind::Line),
|
||||
fill: fill
|
||||
.map(|color| craft_wordmodel::fill::Fill::Solid {
|
||||
color,
|
||||
transparency: (1.0 - if alpha.is_finite() { alpha.clamp(0.0, 1.0) } else { 1.0 }) * 100.0,
|
||||
})
|
||||
.filter(|_| *kind != ShapeKind::Line),
|
||||
line: stroke.filter(|_| stroke_width.is_finite() && *stroke_width > 0.0).map(|c| (c, stroke_width.min(12.0))),
|
||||
..plain.clone()
|
||||
};
|
||||
|
||||
@@ -99,3 +99,25 @@ fn pictures_are_sniffed_and_sized_from_their_headers() {
|
||||
let wide = make_img(PNG.to_vec(), Some(1000.0), Some(500.0), "").unwrap();
|
||||
assert_eq!((wide.w, wide.h), (468.0, 234.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_cap_not_repeated_after_page_break() {
|
||||
let mut doc = craft_wordmodel::Document::default();
|
||||
let mut para = craft_wordmodel::Paragraph::with_text("Before\x0cAfter", craft_wordmodel::props::CharProps::default());
|
||||
para.props.drop_cap = Some(3);
|
||||
para.props.drop_cap_distance = Some(72.0);
|
||||
para.props.shading = Some(craft_wordmodel::Rgb(255, 0, 0));
|
||||
doc.body = vec![std::sync::Arc::new(craft_wordmodel::Block::Para(para))];
|
||||
|
||||
let flow = craft_flow::from_doc_with(&doc, craft_flow::FlowOpts { paras: true, ..Default::default() });
|
||||
let paras: Vec<_> = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect();
|
||||
assert_eq!(paras.len(), 2);
|
||||
// First paragraph keeps drop cap and shading
|
||||
assert_eq!(paras[0].extra.as_ref().and_then(|x| x.drop_cap), Some(3));
|
||||
assert_eq!(paras[0].extra.as_ref().and_then(|x| x.drop_cap_distance), Some(72.0));
|
||||
assert_eq!(paras[0].extra.as_ref().and_then(|x| x.shading), Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
// Continuation clears drop cap, but preserves shading
|
||||
assert_eq!(paras[1].extra.as_ref().and_then(|x| x.drop_cap), None);
|
||||
assert_eq!(paras[1].extra.as_ref().and_then(|x| x.drop_cap_distance), None);
|
||||
assert_eq!(paras[1].extra.as_ref().and_then(|x| x.shading), Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
@@ -30,7 +30,9 @@ decode = ["dep:heic-rs", "dep:rayon"]
|
||||
# `read_metadata` is this reader.
|
||||
metadata = []
|
||||
# `craft_heif::testdata`: lossless synthetic HEIF files, for the tests of the crates above.
|
||||
testdata = []
|
||||
test-support = []
|
||||
# The former name of `test-support`, kept until every crate that enables it uses `test-support`.
|
||||
testdata = ["test-support"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -55,8 +55,10 @@ and a scan gathers at most 64 MiB in all. A build with
|
||||
`default-features = false, features = ["metadata"]` has no heic-rs dependency, and its
|
||||
`read_metadata` is `items::read`. With both features, a test checks that the two readers agree.
|
||||
|
||||
Feature `testdata` exposes `craft_heif::testdata`: lossless synthetic HEIF files (PCM-coded
|
||||
32 × 32 HEVC pictures, grids, every property above) for the tests of the crates above.
|
||||
Feature `test-support` exposes `craft_heif::testdata`: lossless synthetic HEIF files (PCM-coded
|
||||
32 × 32 HEVC pictures, grids, every property above) for the tests of the crates above. The
|
||||
feature's former name, `testdata`, still turns it on until every crate that enables it uses
|
||||
`test-support`.
|
||||
|
||||
## Why it is a separate, optional crate
|
||||
|
||||
|
||||
+11
-2
@@ -26,8 +26,9 @@ mod census;
|
||||
pub mod items;
|
||||
#[cfg(feature = "decode")]
|
||||
mod orient;
|
||||
/// Lossless synthetic HEIF files for tests, here and in the crates above (feature `testdata`).
|
||||
#[cfg(any(test, feature = "testdata"))]
|
||||
/// Lossless synthetic HEIF files for tests, here and in the crates above (feature `test-support`,
|
||||
/// or its former name `testdata`).
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
#[doc(hidden)]
|
||||
pub mod testdata;
|
||||
#[cfg(feature = "decode")]
|
||||
@@ -704,4 +705,12 @@ mod tests {
|
||||
assert!(Error::Limit("x".into()).to_string().contains("limit"));
|
||||
assert!(Error::Malformed("x".into()).to_string().contains("malformed"));
|
||||
}
|
||||
|
||||
/// A crate that enables the former feature name `testdata` gets `test-support`, and with it
|
||||
/// `craft_heif::testdata`. Without that, this test does not compile.
|
||||
#[test]
|
||||
#[cfg(feature = "testdata")]
|
||||
fn the_testdata_feature_turns_on_test_support() {
|
||||
const { assert!(cfg!(feature = "test-support"), "feature `testdata` must enable `test-support`") };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//! Synthetic HEIF files for tests (feature `testdata`): lossless, so every decoded sample is known.
|
||||
//! Synthetic HEIF files for tests (feature `test-support`, or its former name `testdata`): lossless,
|
||||
//! so every decoded sample is known.
|
||||
//!
|
||||
//! Each coded picture is one 32 × 32 coding tree unit stored as PCM samples (H.265 7.3.8.7):
|
||||
//! the bitstream carries the YCbCr values verbatim, so the decoder's HEVC stage reproduces them
|
||||
|
||||
@@ -21,7 +21,7 @@ use std::sync::Arc;
|
||||
|
||||
use craft_wordmodel::props::LineSpacing;
|
||||
use craft_wordmodel::props::VAlign;
|
||||
use craft_wordmodel::{Align, Document};
|
||||
use craft_wordmodel::{Align, Document, Rgb};
|
||||
|
||||
use craft_flow::{
|
||||
self as model, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, Meta, Para, Spacing, base64_encode, data_uri, make_img, mime_of,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::css::{apply_style, css_len_pt, strip_important};
|
||||
use super::import::{PAGE_FONT, PAGE_HEADING_SIZES, collapse_ws};
|
||||
use super::{
|
||||
Align, Arc, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, LineSpacing, ListInfo, Meta, Para, Spacing, VAlign, data_uri, make_img, model,
|
||||
Align, Arc, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, LineSpacing, ListInfo, Meta, Para, Rgb, Spacing, VAlign, data_uri, make_img, model,
|
||||
parse_color,
|
||||
};
|
||||
|
||||
@@ -31,6 +31,11 @@ struct El {
|
||||
name: String,
|
||||
kind: ElKind,
|
||||
fmt: Fmt,
|
||||
/// Block or container background declared on this element (e.g. div, p, pre, table, cell),
|
||||
/// kept separate from character shading (`fmt.background`) so child elements can explicitly
|
||||
/// inherit it (`background: inherit` / `background-color: inherit`) and table cells can
|
||||
/// resolve shading through transparent row groups or rows (#1786).
|
||||
block_background: Option<Rgb>,
|
||||
/// Margins, indents and line height its CSS states (blocks only; they don't inherit; on a
|
||||
/// page's spans, their margins).
|
||||
spacing: Spacing,
|
||||
@@ -54,6 +59,7 @@ struct TableB {
|
||||
|
||||
struct ElementStyle {
|
||||
fmt: Fmt,
|
||||
block_background: Option<Rgb>,
|
||||
align: Option<Align>,
|
||||
preserve: bool,
|
||||
page_break: bool,
|
||||
@@ -650,6 +656,16 @@ impl<'r> Builder<'r> {
|
||||
}
|
||||
let pb_before = self.stack.iter_mut().rev().take_while(|e| e.kind != ElKind::Cell).any(|e| std::mem::take(&mut e.page_break));
|
||||
p.page_break = pb_before || std::mem::take(&mut self.pending_page_break);
|
||||
let block_shading = self
|
||||
.stack
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|e| e.kind != ElKind::Cell && e.kind != ElKind::Table)
|
||||
.filter(|e| self.page || !matches!(e.name.as_str(), "body" | "html"))
|
||||
.find_map(|e| e.block_background);
|
||||
if let Some(bg) = block_shading {
|
||||
p.extra.get_or_insert_with(Default::default).shading = Some(bg);
|
||||
}
|
||||
self.para = Some(p);
|
||||
}
|
||||
self.para.get_or_insert_with(Para::default)
|
||||
@@ -700,6 +716,35 @@ impl<'r> Builder<'r> {
|
||||
&& prev.list.is_some()
|
||||
&& prev.list.map(|l| l.level) == level
|
||||
{
|
||||
let prev_bg = prev.extra.as_ref().and_then(|x| x.shading);
|
||||
let cont_bg = p.extra.as_ref().and_then(|x| x.shading);
|
||||
if prev_bg != cont_bg {
|
||||
if let Some(bg) = prev_bg {
|
||||
for inline in &mut prev.inlines {
|
||||
let fmt = match inline {
|
||||
Inline::Text(_, fmt) | Inline::Ruby { fmt, .. } => fmt,
|
||||
_ => continue,
|
||||
};
|
||||
if fmt.background.is_none() {
|
||||
fmt.background = Some(bg);
|
||||
}
|
||||
}
|
||||
if let Some(extra) = &mut prev.extra {
|
||||
extra.shading = None;
|
||||
}
|
||||
}
|
||||
if let Some(bg) = cont_bg {
|
||||
for inline in &mut p.inlines {
|
||||
let fmt = match inline {
|
||||
Inline::Text(_, fmt) | Inline::Ruby { fmt, .. } => fmt,
|
||||
_ => continue,
|
||||
};
|
||||
if fmt.background.is_none() {
|
||||
fmt.background = Some(bg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
prev.push_text("\n", &Fmt::default());
|
||||
prev.inlines.extend(p.inlines);
|
||||
return;
|
||||
@@ -933,6 +978,11 @@ impl<'r> Builder<'r> {
|
||||
{
|
||||
self.pop_to(k);
|
||||
}
|
||||
if matches!(name, "tbody" | "thead" | "tfoot")
|
||||
&& let Some(k) = self.find_open(|e| matches!(e.name.as_str(), "tbody" | "thead" | "tfoot"), |e| e.kind == ElKind::Table)
|
||||
{
|
||||
self.pop_to(k);
|
||||
}
|
||||
if is_table_part && !self.tables.is_empty() {
|
||||
if matches!(name, "td" | "th")
|
||||
&& let Some(k) = self.find_open(|e| e.kind == ElKind::Cell, |e| matches!(e.kind, ElKind::Table | ElKind::Tr))
|
||||
@@ -1049,11 +1099,34 @@ impl<'r> Builder<'r> {
|
||||
fmt.color.get_or_insert(craft_wordmodel::Rgb(0, 0, 0));
|
||||
fmt.size = PAGE_HEADING_SIZES.get(usize::from(n.clamp(1, 6)) - 1).copied();
|
||||
}
|
||||
apply_style(css, &mut fmt, &mut align, &mut preserve, &mut page_break, &mut page_break_after);
|
||||
if kind != ElKind::Inline {
|
||||
// CSS background does not inherit from inline ancestors to non-inline elements (#1786).
|
||||
// HTML bgcolor presentational attribute supplies the initial background, which can be
|
||||
// overridden or cleared by CSS declarations in apply_style.
|
||||
fmt.background = attr(attrs, "bgcolor").and_then(parse_color);
|
||||
}
|
||||
let parent_bg = self
|
||||
.stack
|
||||
.last()
|
||||
.and_then(|e| if !self.page && matches!(e.name.as_str(), "body" | "html") { None } else { e.fmt.background.or(e.block_background) });
|
||||
apply_style(css, &mut fmt, &mut align, &mut preserve, &mut page_break, &mut page_break_after, parent_bg);
|
||||
if kind == ElKind::Pre {
|
||||
preserve = true;
|
||||
}
|
||||
ElementStyle { fmt, align, preserve, page_break, page_break_after }
|
||||
// CSS background-color does not inherit to child text runs; container elements (div, p,
|
||||
// pre, table, row groups, cells, etc.) do not set character shading (#1786). Container
|
||||
// backgrounds are saved to `block_background` for paragraph shading on block containers,
|
||||
// explicit child inheritance (`background: inherit`), and table cell shading, while
|
||||
// character shading `fmt.background` is kept `None`.
|
||||
let block_background = if kind != ElKind::Inline { fmt.background.take() } else { None };
|
||||
let has_root_bg =
|
||||
block_background.is_some() || self.stack.iter().any(|e| matches!(e.name.as_str(), "body" | "html") && e.block_background.is_some());
|
||||
if !self.page && matches!(name, "body" | "html") && has_root_bg {
|
||||
// A pasted fragment drops the root background; its foreground was chosen for that
|
||||
// background, so drop it too rather than painting light text on the page default.
|
||||
fmt.color = None;
|
||||
}
|
||||
ElementStyle { fmt, block_background, align, preserve, page_break, page_break_after }
|
||||
}
|
||||
|
||||
pub(super) fn start(&mut self, name: &str, attrs: &[(String, String)], self_close: bool) {
|
||||
@@ -1089,7 +1162,7 @@ impl<'r> Builder<'r> {
|
||||
self.flush();
|
||||
}
|
||||
let css = self.css_of(name, attrs);
|
||||
let ElementStyle { mut fmt, align, preserve, page_break, page_break_after } = self.element_style(name, attrs, kind, &css);
|
||||
let ElementStyle { mut fmt, block_background, align, preserve, page_break, page_break_after } = self.element_style(name, attrs, kind, &css);
|
||||
match kind {
|
||||
ElKind::Table => self.tables.push(TableB { rows: Vec::new(), row: None, bordered: declares_border(attrs, &css, true) }),
|
||||
ElKind::Tr => {
|
||||
@@ -1115,7 +1188,20 @@ impl<'r> Builder<'r> {
|
||||
}
|
||||
let num = |k: &str| attr(attrs, k).and_then(|v| v.trim().parse::<u32>().ok()).map(|v| v.clamp(1, 63));
|
||||
let colspan = num("colspan").unwrap_or(1);
|
||||
let shading = attr(attrs, "bgcolor").and_then(parse_color).or(fmt.background);
|
||||
let shading = block_background.or_else(|| {
|
||||
for e in self.stack.iter().rev() {
|
||||
if !self.page && matches!(e.name.as_str(), "body" | "html") {
|
||||
continue;
|
||||
}
|
||||
if let Some(bg) = e.block_background {
|
||||
return Some(bg);
|
||||
}
|
||||
if e.kind == ElKind::Cell {
|
||||
break;
|
||||
}
|
||||
}
|
||||
None
|
||||
});
|
||||
let mut valign = attr(attrs, "valign").and_then(|v| match v.trim().to_ascii_lowercase().as_str() {
|
||||
"top" => Some(VAlign::Top),
|
||||
"middle" | "center" => Some(VAlign::Center),
|
||||
@@ -1153,7 +1239,19 @@ impl<'r> Builder<'r> {
|
||||
let spacing =
|
||||
if (kind != ElKind::Inline || self.page) && !matches!(name, "body" | "html") { block_spacing(&css) } else { Spacing::default() };
|
||||
let auto_margins = self.page && (name == "p" || matches!(kind, ElKind::Heading(_)));
|
||||
self.stack.push(El { name: name.to_string(), kind, fmt, spacing, auto_margins, align, preserve, page_break, page_break_after, used: false });
|
||||
self.stack.push(El {
|
||||
name: name.to_string(),
|
||||
kind,
|
||||
fmt,
|
||||
block_background,
|
||||
spacing,
|
||||
auto_margins,
|
||||
align,
|
||||
preserve,
|
||||
page_break,
|
||||
page_break_after,
|
||||
used: false,
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn end(&mut self, name: &str) {
|
||||
|
||||
+463
-4
@@ -1,6 +1,6 @@
|
||||
use super::builder::decls;
|
||||
use super::import::PAGE_FONT;
|
||||
use super::{Align, Fmt, model, parse_color};
|
||||
use super::{Align, Fmt, Rgb, model, parse_color};
|
||||
|
||||
/// A CSS length (or font-size keyword) in points; `None` when it is not one.
|
||||
pub fn css_len_pt(v: &str) -> Option<f32> {
|
||||
@@ -88,7 +88,398 @@ pub(super) fn css_unescape(s: &str) -> String {
|
||||
o
|
||||
}
|
||||
|
||||
/// Apply a `style` attribute to formatting / block properties.
|
||||
/// Maximum number of tokens parsed in a CSS background shorthand declaration to prevent DOS.
|
||||
const MAX_CSS_SHORTHAND_TOKENS: usize = 64;
|
||||
|
||||
/// Returns true if `c` is a CSS whitespace character according to CSS Syntax Level 3 § 4.2.
|
||||
///
|
||||
/// Only U+0020 (space), U+0009 (tab), U+000A (LF), U+000C (FF), and U+000D (CR) are CSS whitespace.
|
||||
/// Non-breaking space (U+00A0) and other Unicode whitespace characters are not CSS whitespace.
|
||||
fn is_css_whitespace(c: char) -> bool {
|
||||
matches!(c, ' ' | '\t' | '\n' | '\x0C' | '\r')
|
||||
}
|
||||
|
||||
/// Trims leading and trailing CSS whitespace from a string slice according to CSS Syntax Level 3 § 4.2.
|
||||
fn trim_css_whitespace(s: &str) -> &str {
|
||||
s.trim_matches(is_css_whitespace)
|
||||
}
|
||||
|
||||
/// Check whether a token is a valid CSS length or percentage value.
|
||||
///
|
||||
/// Returns `true` for `"0"` or numeric quantities ending in valid CSS length units
|
||||
/// (`px`, `pt`, `em`, `rem`, `%`, `in`, `cm`, `mm`, `vw`, `vh`, `vmin`, `vmax`, `pc`, `ch`, `ex`).
|
||||
fn is_length_or_percentage(s: &str) -> bool {
|
||||
let s = trim_css_whitespace(s);
|
||||
if s.parse::<f64>().is_ok_and(|x| x == 0.0) {
|
||||
return true;
|
||||
}
|
||||
let units = ["px", "pt", "em", "rem", "%", "in", "cm", "mm", "vw", "vh", "vmin", "vmax", "pc", "ch", "ex"];
|
||||
for unit in units {
|
||||
if let Some(num_str) = s.strip_suffix(unit)
|
||||
&& trim_css_whitespace(num_str).parse::<f32>().is_ok_and(|x| x.is_finite())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Represents a parsed CSS background layer containing its tokens and depth-zero slash count.
|
||||
struct BgLayer<'a> {
|
||||
/// Tokens belonging to this layer, trimmed of CSS whitespace.
|
||||
tokens: Vec<&'a str>,
|
||||
/// Number of slashes occurring at parenthesis depth 0 in this layer.
|
||||
depth_zero_slashes: usize,
|
||||
}
|
||||
|
||||
/// Push a sliced and trimmed token to the layer's token list, enforcing the shorthand token limit.
|
||||
fn push_shorthand_token<'a>(val: &'a str, start: &mut Option<usize>, end: usize, tokens: &mut Vec<&'a str>, total_tokens: &mut usize) -> Option<()> {
|
||||
if let Some(s) = start.take()
|
||||
&& let Some(token) = val.get(s..end)
|
||||
{
|
||||
let trimmed = trim_css_whitespace(token);
|
||||
if !trimmed.is_empty() {
|
||||
if *total_tokens >= MAX_CSS_SHORTHAND_TOKENS {
|
||||
return None;
|
||||
}
|
||||
tokens.push(trimmed);
|
||||
*total_tokens = total_tokens.saturating_add(1);
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Parse CSS background shorthand value into comma-separated layers and tokens.
|
||||
///
|
||||
/// Respects quotes and parenthesis depth. Rejects and returns `None` if the token
|
||||
/// limit `MAX_CSS_SHORTHAND_TOKENS` is exceeded (indicating hostile or truncated input),
|
||||
/// or if quotes or parentheses are unbalanced.
|
||||
fn parse_bg_layers(val: &str) -> Option<Vec<BgLayer<'_>>> {
|
||||
let mut layers = Vec::new();
|
||||
let mut current_tokens = Vec::new();
|
||||
let mut current_slashes = 0usize;
|
||||
let mut total_tokens = 0usize;
|
||||
|
||||
let mut start = None;
|
||||
let mut paren_depth = 0usize;
|
||||
let mut in_quote = None;
|
||||
let mut escaped = false;
|
||||
let mut in_raw_url = false;
|
||||
|
||||
let mut byte_idx = 0;
|
||||
while byte_idx < val.len() {
|
||||
let Some(c) = val[byte_idx..].chars().next() else { break };
|
||||
let char_len = c.len_utf8();
|
||||
let i = byte_idx;
|
||||
byte_idx += char_len;
|
||||
|
||||
if escaped {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
if c == '\\' {
|
||||
escaped = true;
|
||||
if start.is_none() {
|
||||
start = Some(i);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(q) = in_quote {
|
||||
if c == q {
|
||||
in_quote = None;
|
||||
}
|
||||
} else if c == '\'' || c == '"' {
|
||||
in_quote = Some(c);
|
||||
if start.is_none() {
|
||||
start = Some(i);
|
||||
}
|
||||
} else if !in_raw_url && val[i..].starts_with("/*") {
|
||||
// CSS comments outside strings and URLs are consumed before delimiter classification
|
||||
if paren_depth == 0 {
|
||||
push_shorthand_token(val, &mut start, i, &mut current_tokens, &mut total_tokens)?;
|
||||
}
|
||||
if let Some(end_offset) = val[i + 2..].find("*/") {
|
||||
byte_idx = i + 2 + end_offset + 2;
|
||||
} else {
|
||||
byte_idx = val.len();
|
||||
}
|
||||
continue;
|
||||
} else if c == '(' {
|
||||
if in_quote.is_none()
|
||||
&& let Some(s) = start
|
||||
&& val.get(s..i).is_some_and(|prefix| trim_css_whitespace(prefix).eq_ignore_ascii_case("url"))
|
||||
{
|
||||
in_raw_url = true;
|
||||
}
|
||||
paren_depth = paren_depth.saturating_add(1);
|
||||
if start.is_none() {
|
||||
start = Some(i);
|
||||
}
|
||||
} else if c == ')' {
|
||||
if paren_depth == 0 {
|
||||
return None;
|
||||
}
|
||||
paren_depth -= 1;
|
||||
if paren_depth == 0 {
|
||||
in_raw_url = false;
|
||||
}
|
||||
} else if paren_depth == 0 {
|
||||
if c == '/' {
|
||||
current_slashes = current_slashes.saturating_add(1);
|
||||
push_shorthand_token(val, &mut start, i, &mut current_tokens, &mut total_tokens)?;
|
||||
if total_tokens >= MAX_CSS_SHORTHAND_TOKENS {
|
||||
return None;
|
||||
}
|
||||
if let Some(slash_token) = val.get(i..i + 1) {
|
||||
current_tokens.push(slash_token);
|
||||
total_tokens = total_tokens.saturating_add(1);
|
||||
}
|
||||
} else if c == ',' {
|
||||
push_shorthand_token(val, &mut start, i, &mut current_tokens, &mut total_tokens)?;
|
||||
if current_tokens.is_empty() {
|
||||
return None;
|
||||
}
|
||||
layers.push(BgLayer { tokens: std::mem::take(&mut current_tokens), depth_zero_slashes: current_slashes });
|
||||
current_slashes = 0;
|
||||
} else if is_css_whitespace(c) {
|
||||
push_shorthand_token(val, &mut start, i, &mut current_tokens, &mut total_tokens)?;
|
||||
} else if start.is_none() {
|
||||
start = Some(i);
|
||||
}
|
||||
} else if start.is_none() {
|
||||
start = Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
if escaped || paren_depth != 0 || in_quote.is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
push_shorthand_token(val, &mut start, val.len(), &mut current_tokens, &mut total_tokens)?;
|
||||
|
||||
if current_tokens.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
layers.push(BgLayer { tokens: current_tokens, depth_zero_slashes: current_slashes });
|
||||
|
||||
Some(layers)
|
||||
}
|
||||
|
||||
fn is_valid_rgb_component(s: &str) -> bool {
|
||||
let s = trim_css_whitespace(s);
|
||||
if s.eq_ignore_ascii_case("none") {
|
||||
return true;
|
||||
}
|
||||
if let Some(p) = s.strip_suffix('%') {
|
||||
trim_css_whitespace(p).parse::<f32>().is_ok_and(|v| v.is_finite())
|
||||
} else {
|
||||
s.parse::<f32>().is_ok_and(|v| v.is_finite())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_valid_hue_component(s: &str) -> bool {
|
||||
let s = trim_css_whitespace(s);
|
||||
if s.eq_ignore_ascii_case("none") {
|
||||
return true;
|
||||
}
|
||||
let num_str = if let Some(deg) = s.strip_suffix("deg") {
|
||||
deg
|
||||
} else if let Some(rad) = s.strip_suffix("rad") {
|
||||
rad
|
||||
} else if let Some(turn) = s.strip_suffix("turn") {
|
||||
turn
|
||||
} else {
|
||||
s
|
||||
};
|
||||
trim_css_whitespace(num_str).parse::<f32>().is_ok_and(|v| v.is_finite())
|
||||
}
|
||||
|
||||
fn is_valid_zero_alpha(a: &str) -> bool {
|
||||
let a = trim_css_whitespace(a);
|
||||
if a.eq_ignore_ascii_case("none") || a == "0" || a == "0%" || a == "0.0" {
|
||||
return true;
|
||||
}
|
||||
if let Some(p) = a.strip_suffix('%') {
|
||||
trim_css_whitespace(p).parse::<f32>().is_ok_and(|v| v <= 0.0)
|
||||
} else {
|
||||
a.parse::<f32>().is_ok_and(|v| v <= 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if a CSS color expression specifies a fully transparent color (alpha = 0).
|
||||
fn is_zero_alpha_color(s: &str) -> bool {
|
||||
let s = trim_css_whitespace(s).to_ascii_lowercase();
|
||||
if s == "transparent" {
|
||||
return true;
|
||||
}
|
||||
if let Some(h) = s.strip_prefix('#') {
|
||||
if h.len() == 4 && h.ends_with('0') && h.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
return true;
|
||||
}
|
||||
if h.len() == 8 && h.ends_with("00") && h.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if let Some(inner) = s.strip_prefix("rgb(").or_else(|| s.strip_prefix("rgba(")) {
|
||||
let Some(inner) = inner.strip_suffix(')') else { return false };
|
||||
let (rgb_part, alpha_part) = inner.split_once('/').map_or((inner, None), |(a, b)| (a, Some(b)));
|
||||
let (parts, alpha_str) = match alpha_part {
|
||||
Some(a) => {
|
||||
let parts: Vec<&str> = if rgb_part.contains(',') { rgb_part.split(',').collect() } else { rgb_part.split_whitespace().collect() };
|
||||
(parts, a)
|
||||
}
|
||||
None => {
|
||||
let parts: Vec<&str> = if inner.contains(',') { inner.split(',').collect() } else { inner.split_whitespace().collect() };
|
||||
if parts.len() == 4 {
|
||||
(parts[..3].to_vec(), parts[3])
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
return parts.len() == 3 && parts.iter().all(|&p| is_valid_rgb_component(p)) && is_valid_zero_alpha(alpha_str);
|
||||
}
|
||||
if let Some(inner) = s.strip_prefix("hsl(").or_else(|| s.strip_prefix("hsla(")) {
|
||||
let Some(inner) = inner.strip_suffix(')') else { return false };
|
||||
let (hsl_part, alpha_part) = inner.split_once('/').map_or((inner, None), |(a, b)| (a, Some(b)));
|
||||
let (parts, alpha_str) = match alpha_part {
|
||||
Some(a) => {
|
||||
let parts: Vec<&str> = if hsl_part.contains(',') { hsl_part.split(',').collect() } else { hsl_part.split_whitespace().collect() };
|
||||
(parts, a)
|
||||
}
|
||||
None => {
|
||||
let parts: Vec<&str> = if inner.contains(',') { inner.split(',').collect() } else { inner.split_whitespace().collect() };
|
||||
if parts.len() == 4 {
|
||||
(parts[..3].to_vec(), parts[3])
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
return parts.len() == 3
|
||||
&& is_valid_hue_component(parts[0])
|
||||
&& is_valid_rgb_component(parts[1])
|
||||
&& is_valid_rgb_component(parts[2])
|
||||
&& is_valid_zero_alpha(alpha_str);
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns true if a lowercase token represents a recognized CSS image function.
|
||||
fn is_valid_image_function(ltok: &str) -> bool {
|
||||
if !ltok.ends_with(')') {
|
||||
return false;
|
||||
}
|
||||
ltok.starts_with("url(")
|
||||
|| ltok.starts_with("image(")
|
||||
|| ltok.starts_with("image-set(")
|
||||
|| ltok.starts_with("-webkit-image-set(")
|
||||
|| ltok.starts_with("cross-fade(")
|
||||
|| ltok.starts_with("element(")
|
||||
|| ltok.starts_with("paint(")
|
||||
|| ltok.starts_with("linear-gradient(")
|
||||
|| ltok.starts_with("radial-gradient(")
|
||||
|| ltok.starts_with("conic-gradient(")
|
||||
|| ltok.starts_with("repeating-linear-gradient(")
|
||||
|| ltok.starts_with("repeating-radial-gradient(")
|
||||
|| ltok.starts_with("repeating-conic-gradient(")
|
||||
|| ltok.starts_with("-webkit-linear-gradient(")
|
||||
|| ltok.starts_with("-webkit-radial-gradient(")
|
||||
|| ltok.starts_with("-webkit-gradient(")
|
||||
|| ltok.starts_with("-moz-linear-gradient(")
|
||||
|| ltok.starts_with("-moz-radial-gradient(")
|
||||
}
|
||||
|
||||
/// Returns true if a lowercase token represents a recognized CSS math or environment function.
|
||||
fn is_valid_math_or_value_function(ltok: &str) -> bool {
|
||||
if !ltok.ends_with(')') {
|
||||
return false;
|
||||
}
|
||||
ltok.starts_with("calc(")
|
||||
|| ltok.starts_with("min(")
|
||||
|| ltok.starts_with("max(")
|
||||
|| ltok.starts_with("clamp(")
|
||||
|| ltok.starts_with("var(")
|
||||
|| ltok.starts_with("env(")
|
||||
}
|
||||
|
||||
/// Check whether a token is a valid CSS background position component (`top`, `bottom`, `left`, `right`, `center`, length, percentage, or math function).
|
||||
fn is_valid_pos_token(s: &str) -> bool {
|
||||
let ls = s.to_ascii_lowercase();
|
||||
matches!(ls.as_str(), "top" | "bottom" | "left" | "right" | "center") || is_length_or_percentage(&ls) || is_valid_math_or_value_function(&ls)
|
||||
}
|
||||
|
||||
/// Check whether a token is a valid CSS background size component (`auto`, length, percentage, or math function).
|
||||
fn is_valid_size_component(s: &str) -> bool {
|
||||
let ls = s.to_ascii_lowercase();
|
||||
ls == "auto" || is_length_or_percentage(&ls) || is_valid_math_or_value_function(&ls)
|
||||
}
|
||||
|
||||
/// Check whether a token is a valid CSS background property token outside of position-and-size slash expressions.
|
||||
///
|
||||
/// Recognizes color names/hex codes, images, repeat styles, attachments, and box keywords.
|
||||
/// Standalone size keywords (`cover`, `contain`, `auto`) and slashes are rejected unless positioned in a valid slash layer.
|
||||
fn is_valid_non_slash_bg_token(tok: &str) -> bool {
|
||||
let ltok = tok.to_ascii_lowercase();
|
||||
if is_valid_image_function(<ok) || is_valid_math_or_value_function(<ok) {
|
||||
return true;
|
||||
}
|
||||
if is_zero_alpha_color(tok) || parse_color(tok).is_some() || ltok == "transparent" {
|
||||
return true;
|
||||
}
|
||||
match ltok.as_str() {
|
||||
"none" | "repeat" | "no-repeat" | "repeat-x" | "repeat-y" | "space" | "round" | "scroll" | "fixed" | "local" | "border-box"
|
||||
| "padding-box" | "content-box" | "top" | "bottom" | "left" | "right" | "center" => true,
|
||||
_ => is_length_or_percentage(<ok),
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate that a single CSS background shorthand layer satisfies the position-and-size grammar.
|
||||
///
|
||||
/// If depth-zero slashes are present, exactly one slash must separate a valid `<bg-position>` from a valid `<bg-size>`.
|
||||
/// Background size keywords `cover` and `contain` are rejected unless preceded by a valid position and slash (#1786).
|
||||
fn is_valid_bg_layer(layer: &BgLayer<'_>) -> bool {
|
||||
if layer.depth_zero_slashes > 1 {
|
||||
return false;
|
||||
}
|
||||
if layer.depth_zero_slashes == 0 {
|
||||
return layer.tokens.iter().all(|t| is_valid_non_slash_bg_token(t));
|
||||
}
|
||||
|
||||
let Some(slash_idx) = layer.tokens.iter().position(|&t| t == "/") else {
|
||||
return false;
|
||||
};
|
||||
let before_tokens = &layer.tokens[..slash_idx];
|
||||
let after_tokens = &layer.tokens[slash_idx + 1..];
|
||||
|
||||
let Some(&last_before) = before_tokens.last() else {
|
||||
return false;
|
||||
};
|
||||
if !is_valid_pos_token(last_before) {
|
||||
return false;
|
||||
}
|
||||
if !before_tokens[..before_tokens.len().saturating_sub(1)].iter().all(|t| is_valid_non_slash_bg_token(t)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if after_tokens.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let first_after = after_tokens[0];
|
||||
let remaining_after = if first_after.eq_ignore_ascii_case("cover") || first_after.eq_ignore_ascii_case("contain") {
|
||||
&after_tokens[1..]
|
||||
} else if is_valid_size_component(first_after) {
|
||||
if after_tokens.len() > 1 && is_valid_size_component(after_tokens[1]) { &after_tokens[2..] } else { &after_tokens[1..] }
|
||||
} else {
|
||||
return false;
|
||||
};
|
||||
|
||||
remaining_after.iter().all(|t| is_valid_non_slash_bg_token(t))
|
||||
}
|
||||
|
||||
/// Apply CSS `style` declarations to formatting and block properties, updating character and block attributes.
|
||||
pub(super) fn apply_style(
|
||||
css: &str,
|
||||
f: &mut Fmt,
|
||||
@@ -96,6 +487,7 @@ pub(super) fn apply_style(
|
||||
preserve: &mut bool,
|
||||
page_break: &mut bool,
|
||||
page_break_after: &mut bool,
|
||||
parent_bg: Option<Rgb>,
|
||||
) {
|
||||
for (k, v) in decls(css) {
|
||||
let lv = v.to_ascii_lowercase();
|
||||
@@ -119,11 +511,78 @@ pub(super) fn apply_style(
|
||||
f.color = Some(c);
|
||||
}
|
||||
}
|
||||
"background-color" | "background" => {
|
||||
if let Some(c) = v.split_whitespace().find_map(parse_color) {
|
||||
"background-color" => {
|
||||
let trimmed = trim_css_whitespace(v);
|
||||
let lv = trimmed.to_ascii_lowercase();
|
||||
if lv == "transparent" || lv == "none" || lv == "initial" || lv == "unset" || is_zero_alpha_color(trimmed) {
|
||||
f.background = None;
|
||||
} else if lv == "inherit" {
|
||||
f.background = parent_bg;
|
||||
} else if let Some(c) = parse_color(trimmed) {
|
||||
f.background = Some(c);
|
||||
}
|
||||
}
|
||||
"background" => {
|
||||
let Some(layers) = parse_bg_layers(v) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// CSS-wide keywords (inherit, initial, unset) must be the sole value of the declaration.
|
||||
if layers.len() == 1 && layers[0].tokens.len() == 1 {
|
||||
let first = layers[0].tokens[0].to_ascii_lowercase();
|
||||
match first.as_str() {
|
||||
"inherit" => {
|
||||
f.background = parent_bg;
|
||||
continue;
|
||||
}
|
||||
"initial" | "unset" | "none" | "transparent" => {
|
||||
f.background = None;
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if layers.iter().any(|l| l.tokens.iter().any(|t| matches!(t.to_ascii_lowercase().as_str(), "inherit" | "initial" | "unset"))) {
|
||||
// Invalid CSS: CSS-wide keywords cannot be combined with other values.
|
||||
continue;
|
||||
}
|
||||
|
||||
// In CSS background shorthand:
|
||||
// 1. Each layer may have at most one slash separating position and size.
|
||||
// 2. All layers must satisfy the grammar (e.g. `cover` requires position and slash).
|
||||
// 3. Only the final layer may specify a background color.
|
||||
if !layers.iter().all(is_valid_bg_layer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only the final layer can specify background-color. If any earlier layer has a color,
|
||||
// the entire declaration is invalid CSS.
|
||||
let earlier_has_color = layers[..layers.len().saturating_sub(1)]
|
||||
.iter()
|
||||
.any(|l| l.tokens.iter().any(|t| is_zero_alpha_color(t) || parse_color(t).is_some()));
|
||||
if earlier_has_color {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract background color from the final layer.
|
||||
let final_layer = &layers[layers.len().saturating_sub(1)];
|
||||
let color_tokens: Vec<_> =
|
||||
final_layer.tokens.iter().copied().filter(|t| is_zero_alpha_color(t) || parse_color(t).is_some()).collect();
|
||||
if color_tokens.len() > 1 {
|
||||
// Invalid CSS: multiple background colors in a single layer.
|
||||
continue;
|
||||
}
|
||||
if let Some(&tok) = color_tokens.first() {
|
||||
if is_zero_alpha_color(tok) {
|
||||
f.background = None;
|
||||
} else if let Some(c) = parse_color(tok) {
|
||||
f.background = Some(c);
|
||||
}
|
||||
} else {
|
||||
// A valid background shorthand without a color (such as background: url(...) or none repeat)
|
||||
// resets background-color to transparent.
|
||||
f.background = None;
|
||||
}
|
||||
}
|
||||
"font-size" => {
|
||||
if let Some(p) = css_len_pt(v).filter(|p| *p >= 1.0 && *p <= 1638.0) {
|
||||
f.size = Some(p);
|
||||
|
||||
@@ -183,18 +183,70 @@ pub fn inlines_html(inlines: &[Inline]) -> String {
|
||||
o
|
||||
}
|
||||
|
||||
/// Formatted code inline spans inside `<pre><code>`: preserves formatting tags while keeping
|
||||
/// raw whitespace semantics (spaces and tabs are preserved, not converted to ` ` or `<br>`).
|
||||
fn code_inlines_html(inlines: &[Inline]) -> String {
|
||||
let mut o = String::new();
|
||||
for i in inlines {
|
||||
match i {
|
||||
Inline::Text(t, f) => {
|
||||
let (open, close) = fmt_tags(f);
|
||||
o.push_str(&open);
|
||||
o.push_str(&esc(t));
|
||||
for c in close.iter().rev() {
|
||||
o.push_str(c);
|
||||
}
|
||||
}
|
||||
Inline::Ruby { base, ruby, fmt } => {
|
||||
let (open, close) = fmt_tags(fmt);
|
||||
o.push_str(&open);
|
||||
o.push_str(&format!("<ruby>{}<rt>{}</rt></ruby>", esc(base), esc(ruby)));
|
||||
for c in close.iter().rev() {
|
||||
o.push_str(c);
|
||||
}
|
||||
}
|
||||
Inline::Anchor(a) => o.push_str(&format!("<a id=\"{}\"></a>", esc(a))),
|
||||
Inline::Image(img) => {
|
||||
o.push_str(&format!(
|
||||
"<img src=\"data:{};base64,{}\" alt=\"{}\" width=\"{}\" height=\"{}\">",
|
||||
mime_of(&img.ext),
|
||||
base64_encode(&img.data),
|
||||
esc(&img.alt),
|
||||
(img.w / 0.75).round(),
|
||||
(img.h / 0.75).round()
|
||||
));
|
||||
}
|
||||
Inline::Field { result, fmt, .. } | Inline::Control { text: result, fmt, .. } => {
|
||||
let (open, close) = fmt_tags(fmt);
|
||||
o.push_str(&open);
|
||||
o.push_str(&esc(result));
|
||||
for c in close.iter().rev() {
|
||||
o.push_str(c);
|
||||
}
|
||||
}
|
||||
Inline::Figure(alt) | Inline::Chart(craft_flow::Chart { alt, .. }) => o.push_str(&format!("<img alt=\"{}\">", esc(alt))),
|
||||
Inline::Equation { linear, display } => o.push_str(&craft_math::mathml::linear_to_mathml(linear, *display)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
fn trim_num(v: f32) -> String {
|
||||
let s = format!("{:.2}", v);
|
||||
s.trim_end_matches('0').trim_end_matches('.').to_string()
|
||||
}
|
||||
|
||||
fn para_attrs(p: &Para) -> String {
|
||||
let mut css: Vec<&str> = Vec::new();
|
||||
let mut css: Vec<String> = Vec::new();
|
||||
if let Some(a) = align_css(p.align) {
|
||||
css.push(a);
|
||||
css.push(a.to_string());
|
||||
}
|
||||
if p.page_break {
|
||||
css.push("break-before:page;page-break-before:always");
|
||||
css.push("break-before:page;page-break-before:always".to_string());
|
||||
}
|
||||
if let Some(bg) = p.extra.as_ref().and_then(|x| x.shading) {
|
||||
css.push(format!("background-color:#{}", bg.hex()));
|
||||
}
|
||||
if css.is_empty() { String::new() } else { format!(" style=\"{}\"", css.join(";")) }
|
||||
}
|
||||
@@ -240,7 +292,11 @@ fn table_html(t: &FTable, out: &mut String, depth: usize) {
|
||||
css.push(format!("background-color:#{}", s.hex()));
|
||||
}
|
||||
let single = match c.blocks.as_slice() {
|
||||
[FBlock::Para(p)] if p.kind == Kind::Normal && p.list.is_none() && !p.page_break => Some(p),
|
||||
[FBlock::Para(p)]
|
||||
if p.kind == Kind::Normal && p.list.is_none() && !p.page_break && p.extra.as_ref().and_then(|x| x.shading).is_none() =>
|
||||
{
|
||||
Some(p)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if let Some(a) = single.and_then(|p| align_css(p.align)) {
|
||||
@@ -310,17 +366,20 @@ fn blocks_html(blocks: &[FBlock], out: &mut String, depth: usize) {
|
||||
out.push('\n');
|
||||
}
|
||||
FBlock::Para(p) if p.kind == Kind::Code => {
|
||||
out.push_str("<pre><code>");
|
||||
// One `<pre>` per run of code paragraphs whose attributes match: they share the
|
||||
// `<pre>`'s shading, so a different background starts a new block.
|
||||
let attrs = para_attrs(p);
|
||||
out.push_str(&format!("<pre{attrs}><code>"));
|
||||
let mut first = true;
|
||||
while let Some(FBlock::Para(q)) = blocks.get(i) {
|
||||
if q.kind != Kind::Code || q.list.is_some() {
|
||||
if q.kind != Kind::Code || q.list.is_some() || para_attrs(q) != attrs {
|
||||
break;
|
||||
}
|
||||
if !first {
|
||||
out.push('\n');
|
||||
}
|
||||
first = false;
|
||||
out.push_str(&esc(&q.text()));
|
||||
out.push_str(&code_inlines_html(&q.inlines));
|
||||
i += 1;
|
||||
}
|
||||
out.push_str("</code></pre>\n");
|
||||
@@ -357,14 +416,14 @@ fn close_list(stack: &mut Vec<(bool, bool)>, out: &mut String) {
|
||||
|
||||
/// A complete HTML document.
|
||||
pub fn export(doc: &Document) -> String {
|
||||
let flow = model::from_doc(doc);
|
||||
let flow = model::from_doc_with(doc, model::FlowOpts { paras: true, ..Default::default() });
|
||||
export_flow(&flow, doc.styles.default_chr.lang.as_deref().unwrap_or("en"))
|
||||
}
|
||||
|
||||
/// `doc`'s content as an HTML fragment (no head or style sheet): what a copy puts on the system
|
||||
/// clipboard beside its text, equations as MathML (#816).
|
||||
pub fn export_fragment(doc: &Document) -> String {
|
||||
let flow = model::from_doc(doc);
|
||||
let flow = model::from_doc_with(doc, model::FlowOpts { paras: true, ..Default::default() });
|
||||
let mut out = String::from("<meta charset=\"utf-8\">");
|
||||
blocks_html(&flow.blocks, &mut out, 0);
|
||||
out
|
||||
|
||||
@@ -767,3 +767,834 @@ fn hostile_style_sheets_never_panic() {
|
||||
let _ = parse_in(&format!("<style>{css}</style><p class='a é'>x</p><td style='width: 1e40px'>y</td>"), &|_| None, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure container elements (such as `div` and `p`) with background colors do not leak
|
||||
/// character shading onto their child text runs (#1786).
|
||||
#[test]
|
||||
fn container_background_color_does_not_leak_into_character_shading() {
|
||||
let html = r#"<div style="background-color:#1e1e1e;color:#d4d4d4"><span>code</span></div>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert!(!p.runs.is_empty(), "expected nonempty runs");
|
||||
assert_eq!(&p.text, "code");
|
||||
assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x1e, 0x1e, 0x1e)), "container div background is preserved as paragraph shading");
|
||||
for r in &p.runs {
|
||||
assert_eq!(r.props.shading, None, "character should have no shading from container div");
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that table background colors cascade down to cell shading through implicit or explicit
|
||||
/// row groups (`tbody`, `thead`, `tfoot`), but do not leak into cell character shading.
|
||||
#[test]
|
||||
fn table_background_inherits_through_row_groups() {
|
||||
let html = r#"<table style="background-color:#e8f0fa"><tbody><tr><td>cell1</td></tr></tbody></table>
|
||||
<table><tbody style="background-color:#d0e0f0"><tr><td>cell2</td></tr></tbody></table>
|
||||
<table><thead style="background-color:#c0d0e0"><tr><th>head</th></tr></thead></table>"#;
|
||||
let doc = import(html);
|
||||
let tables: Vec<_> = doc
|
||||
.body
|
||||
.iter()
|
||||
.filter_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(tables.len(), 3);
|
||||
for (t, want_hex) in tables.iter().zip(["e8f0fa", "d0e0f0", "c0d0e0"]) {
|
||||
let cell = &t.rows[0].cells[0];
|
||||
assert_eq!(
|
||||
cell.props.shading.map(|c| c.hex().to_ascii_lowercase()),
|
||||
Some(want_hex.to_string()),
|
||||
"cell should inherit shading from table or row group"
|
||||
);
|
||||
let p = &cell.blocks[0];
|
||||
let craft_wordmodel::Block::Para(p) = &**p else {
|
||||
panic!("expected table cell block to be a paragraph");
|
||||
};
|
||||
assert!(!p.runs.is_empty(), "expected nonempty runs");
|
||||
for r in &p.runs {
|
||||
assert_eq!(r.props.shading, None, "cell text should have no character shading");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that block-level container backgrounds (`p`, `pre`, `div`) are preserved as paragraph
|
||||
/// shading on paragraphs rather than character shading on individual runs (#1786).
|
||||
#[test]
|
||||
fn block_element_backgrounds_preserve_paragraph_shading() {
|
||||
let html = r#"<p style="background:#1e1e1e;color:white">text</p><pre style="background:#2a2a2a">code</pre><div style="background:#3a3a3a"><p>inner</p></div>"#;
|
||||
let doc = import(html);
|
||||
assert_eq!(doc.body.len(), 3);
|
||||
let craft_wordmodel::Block::Para(p1) = &*doc.body[0] else { panic!("expected Para") };
|
||||
let craft_wordmodel::Block::Para(p2) = &*doc.body[1] else { panic!("expected Para") };
|
||||
let craft_wordmodel::Block::Para(p3) = &*doc.body[2] else { panic!("expected Para") };
|
||||
assert_eq!(p1.props.shading, Some(craft_wordmodel::Rgb(0x1e, 0x1e, 0x1e)));
|
||||
assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x2a, 0x2a, 0x2a)));
|
||||
assert_eq!(p3.props.shading, Some(craft_wordmodel::Rgb(0x3a, 0x3a, 0x3a)));
|
||||
assert_eq!(&p1.text, "text");
|
||||
assert_eq!(&p2.text, "code");
|
||||
assert_eq!(&p3.text, "inner");
|
||||
assert!(!p1.runs.is_empty());
|
||||
assert!(!p2.runs.is_empty());
|
||||
assert!(!p3.runs.is_empty());
|
||||
for r in &p1.runs {
|
||||
assert_eq!(r.props.shading, None, "character should have no shading from paragraph background");
|
||||
}
|
||||
for r in &p2.runs {
|
||||
assert_eq!(r.props.shading, None, "character should have no shading from pre background");
|
||||
}
|
||||
for r in &p3.runs {
|
||||
assert_eq!(r.props.shading, None, "character should have no shading from container div background");
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that paragraph shading created by a container background survives an HTML round trip (#1786).
|
||||
#[test]
|
||||
fn paragraph_shading_survives_an_html_round_trip() {
|
||||
let html = r#"<div style="background-color:#1e1e1e"><p>code</p></div><pre style="background-color:#2a2a2a">pre</pre>"#;
|
||||
let doc = import(html);
|
||||
let out = export(&doc);
|
||||
assert!(out.contains("background-color:#1E1E1E"), "paragraph shading is exported: {out}");
|
||||
assert!(out.contains("background-color:#2A2A2A"), "code block shading is exported: {out}");
|
||||
|
||||
let back = import(&out);
|
||||
let craft_wordmodel::Block::Para(p1) = &*back.body[0] else { panic!("expected Para") };
|
||||
let craft_wordmodel::Block::Para(p2) = &*back.body[1] else { panic!("expected Para") };
|
||||
assert_eq!(p1.props.shading, Some(craft_wordmodel::Rgb(0x1e, 0x1e, 0x1e)));
|
||||
assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x2a, 0x2a, 0x2a)));
|
||||
}
|
||||
|
||||
/// Verify that paragraph shading inside a single-paragraph table cell survives an HTML round trip (#1786).
|
||||
#[test]
|
||||
fn table_cell_paragraph_shading_survives_an_html_round_trip() {
|
||||
let html = r#"<table><tr><td><p style="background-color:#123456">text</p></td></tr></table>"#;
|
||||
let doc = import(html);
|
||||
let out = export(&doc);
|
||||
assert!(out.contains("background-color:#123456"), "paragraph shading is exported: {out}");
|
||||
|
||||
let back = import(&out);
|
||||
let table = back
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.expect("expected table");
|
||||
let cell = &table.rows[0].cells[0];
|
||||
assert_eq!(cell.props.shading, None, "paragraph shading must not become cell shading");
|
||||
let craft_wordmodel::Block::Para(p) = &*cell.blocks[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
}
|
||||
|
||||
/// Verify that consecutive code paragraphs with different backgrounds export their own `<pre>`.
|
||||
#[test]
|
||||
fn consecutive_code_blocks_keep_their_own_shading() {
|
||||
let html = r#"<pre style="background-color:#111111">a</pre><pre style="background-color:#222222">b</pre>"#;
|
||||
let out = export(&import(html));
|
||||
assert!(out.contains("<pre style=\"background-color:#111111\"><code>a</code></pre>"), "{out}");
|
||||
assert!(out.contains("<pre style=\"background-color:#222222\"><code>b</code></pre>"), "{out}");
|
||||
}
|
||||
|
||||
/// Verify that paragraph shading inherited from a paragraph style is exported (#1786).
|
||||
#[test]
|
||||
fn paragraph_shading_from_a_style_survives_html_export() {
|
||||
let mut doc = Document::new();
|
||||
std::sync::Arc::make_mut(&mut doc.styles).styles.push(craft_wordmodel::Style {
|
||||
id: "Shaded".into(),
|
||||
name: "Shaded".into(),
|
||||
para: craft_wordmodel::props::ParaProps { shading: Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)), ..Default::default() },
|
||||
..Default::default()
|
||||
});
|
||||
let mut p = craft_wordmodel::Paragraph::with_text("text", craft_wordmodel::CharProps::default());
|
||||
p.props.style = Some("Shaded".into());
|
||||
doc.body = vec![craft_wordmodel::para_block(p)];
|
||||
|
||||
let out = export(&doc);
|
||||
assert!(out.contains("background-color:#123456"), "style shading is exported: {out}");
|
||||
}
|
||||
|
||||
/// A closing parenthesis without an opening one invalidates the declaration (#1786).
|
||||
#[test]
|
||||
fn background_shorthand_unbalanced_closing_paren_is_ignored() {
|
||||
let html = r#"<span style="background-color:#ff0000"><span style="background: ) none repeat">text</span></span>"#;
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0xff, 0x00, 0x00)), "invalid shorthand must not clear inherited shading");
|
||||
}
|
||||
|
||||
/// A shaded paragraph keeps its shading on both sides of an internal page break (#1786).
|
||||
#[test]
|
||||
fn paragraph_shading_survives_an_internal_page_break() {
|
||||
let mut p = craft_wordmodel::Paragraph::with_text("before\u{000C}after", craft_wordmodel::CharProps::default());
|
||||
p.props.shading = Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56));
|
||||
let mut doc = Document::new();
|
||||
doc.body = vec![craft_wordmodel::para_block(p)];
|
||||
let out = export(&doc);
|
||||
assert_eq!(out.matches("background-color:#123456").count(), 2, "{out}");
|
||||
}
|
||||
|
||||
/// Verify that CSS-escaped quote characters in background shorthand URLs remain part of the URL
|
||||
/// content and do not cause the shorthand declaration to be rejected.
|
||||
#[test]
|
||||
fn background_shorthand_escaped_quotes_in_url() {
|
||||
let doc = import(r#"<p><span style='background: #123456 url("a\"b.png")'>text</span></p>"#);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(&p.text, "text");
|
||||
assert!(!p.runs.is_empty());
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
}
|
||||
|
||||
/// Verify that valid multi-token CSS background shorthands without a color (such as `none repeat`)
|
||||
/// reset character shading, while invalid declarations are ignored.
|
||||
#[test]
|
||||
fn background_shorthand_without_color_resets_background() {
|
||||
let html = r#"<span style="background-color: #ff0000"><span style="background: none repeat">plain</span></span>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, None);
|
||||
|
||||
let html_invalid = r#"<span style="background-color: #ff0000"><span style="background: none invalid_xyz">shaded</span></span>"#;
|
||||
let doc_invalid = import(html_invalid);
|
||||
let p_invalid = match &*doc_invalid.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_invalid.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
/// Verify that CSS `background: inherit` keeps inherited character shading instead of removing it (#1786).
|
||||
#[test]
|
||||
fn background_inherit_retains_inline_shading() {
|
||||
let html = r#"<span style="background-color: #ff0000"><span style="background: inherit">shaded</span></span>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)), "background: inherit should retain inherited character shading");
|
||||
|
||||
// Also verify background-color: inherit retains shading.
|
||||
let html_color = r#"<span style="background-color: #ff0000"><span style="background-color: inherit">shaded</span></span>"#;
|
||||
let doc_color = import(html_color);
|
||||
let p_color = match &*doc_color.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(
|
||||
p_color.runs[0].props.shading,
|
||||
Some(craft_wordmodel::Rgb(255, 0, 0)),
|
||||
"background-color: inherit should retain inherited character shading"
|
||||
);
|
||||
|
||||
// CSS-wide keyword inherit mixed with other values is invalid and ignored.
|
||||
let html_mixed = r#"<span style="background-color: #00ff00"><span style="background: red inherit">green</span></span>"#;
|
||||
let doc_mixed = import(html_mixed);
|
||||
let p_mixed = match &*doc_mixed.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(
|
||||
p_mixed.runs[0].props.shading,
|
||||
Some(craft_wordmodel::Rgb(0, 255, 0)),
|
||||
"invalid shorthand mixing inherit with values should be ignored"
|
||||
);
|
||||
}
|
||||
|
||||
/// Verify that hostile background declarations with deeply nested slashes do not cause stack overflow (#1786).
|
||||
#[test]
|
||||
fn hostile_background_slashes_never_stack_overflow() {
|
||||
let slashes = "/".repeat(20_000);
|
||||
let html = format!(r#"<span style="background: {slashes}">hostile</span>"#);
|
||||
let doc = import(&html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, None);
|
||||
}
|
||||
|
||||
/// Verify that non-breaking space (U+00A0) is not treated as CSS whitespace when splitting tokens (#1786).
|
||||
#[test]
|
||||
fn background_whitespace_preserves_nbsp() {
|
||||
let html = "<span style=\"background-color: #ff0000\"><span style=\"background: none\u{00A0}repeat\">shaded</span></span>";
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(
|
||||
p.runs[0].props.shading,
|
||||
Some(craft_wordmodel::Rgb(255, 0, 0)),
|
||||
"non-breaking space is not CSS whitespace and should not clear shading"
|
||||
);
|
||||
}
|
||||
|
||||
/// Verify that background shorthands exceeding the token limit are rejected and unapplied (#1786).
|
||||
#[test]
|
||||
fn background_shorthand_truncated_rejects_unapplied() {
|
||||
let mut layers = String::new();
|
||||
for i in 0..64 {
|
||||
layers.push_str(&format!("url(img{i}.png), "));
|
||||
}
|
||||
layers.push_str("#00ff00");
|
||||
let html = format!(r#"<span style="background-color: #ff0000"><span style="background: {layers}">retained</span></span>"#);
|
||||
let doc = import(&html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(
|
||||
p.runs[0].props.shading,
|
||||
Some(craft_wordmodel::Rgb(255, 0, 0)),
|
||||
"truncated shorthand should be rejected and leave inherited background untouched"
|
||||
);
|
||||
}
|
||||
|
||||
/// Verify that shorthands with image-set or calc functions parse and preserve colors (#1786).
|
||||
#[test]
|
||||
fn background_shorthand_valid_functions_preserve_color() {
|
||||
let html_image_set = r#"<span style="background: #123456 image-set(url(a.png) 1x)">image-set</span>"#;
|
||||
let doc = import(html_image_set);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
|
||||
let html_calc = r#"<span style="background: #123456 url(a.png) calc(50% - 2px)">calc</span>"#;
|
||||
let doc_calc = import(html_calc);
|
||||
let p_calc = match &*doc_calc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_calc.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
}
|
||||
|
||||
/// Verify that multi-layer background shorthands validate slashes per layer and preserve final-layer color (#1786).
|
||||
#[test]
|
||||
fn background_shorthand_multi_layer_slashes_apply_color() {
|
||||
let html = r#"<span style="background: url(a) center/cover, url(b) center/contain #123456">multi-layer</span>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
|
||||
// Color in a non-final layer is invalid CSS and must be ignored.
|
||||
let html_invalid_color = r#"<span style="background-color: #ff0000"><span style="background: #123456, url(b)">invalid</span></span>"#;
|
||||
let doc_invalid_color = import(html_invalid_color);
|
||||
let p_invalid_color = match &*doc_invalid_color.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_invalid_color.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
// Multiple slashes in a single layer makes the declaration invalid.
|
||||
let html_multi_slash =
|
||||
r#"<span style="background-color: #ff0000"><span style="background: url(a) center/cover/extra, #123456">invalid</span></span>"#;
|
||||
let doc_multi_slash = import(html_multi_slash);
|
||||
let p_multi_slash = match &*doc_multi_slash.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_multi_slash.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
/// Verify that background shorthands with empty layers (leading, trailing, or double commas) are rejected (#1786).
|
||||
#[test]
|
||||
fn background_shorthand_rejects_empty_layers() {
|
||||
// Leading comma
|
||||
let html_leading = r#"<span style="background-color: #ff0000"><span style="background: , #00ff00">leading</span></span>"#;
|
||||
let doc_leading = import(html_leading);
|
||||
let p_leading = match &*doc_leading.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_leading.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
// Trailing comma
|
||||
let html_trailing = r#"<span style="background-color: #ff0000"><span style="background: transparent,">trailing</span></span>"#;
|
||||
let doc_trailing = import(html_trailing);
|
||||
let p_trailing = match &*doc_trailing.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_trailing.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
// Double comma
|
||||
let html_double = r#"<span style="background-color: #ff0000"><span style="background: #00ff00,,#0000ff">double</span></span>"#;
|
||||
let doc_double = import(html_double);
|
||||
let p_double = match &*doc_double.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_double.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
/// Verify that zero-alpha colors in background shorthands or declarations remain transparent (#1786).
|
||||
#[test]
|
||||
fn background_zero_alpha_colors_are_transparent() {
|
||||
for decl in ["rgb(0 0 0 / 0)", "rgba(0, 0, 0, 0)", "#0000", "#00000000", "#ffffff00", "hsla(120, 100%, 50%, 0)", "hsl(120 100% 50% / 0)"] {
|
||||
let html = format!(r#"<span style="background-color: #ff0000"><span style="background: {decl}">transparent</span></span>"#);
|
||||
let doc = import(&html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, None, "zero-alpha color `{decl}` must clear background rather than composite over white");
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that ancestor table shading is preserved beneath transparent table layers (#1786).
|
||||
#[test]
|
||||
fn transparent_table_layers_preserve_ancestor_table_shading() {
|
||||
let html = r#"<table style="background-color:#e8f0fa">
|
||||
<tbody style="background-color:transparent">
|
||||
<tr style="background:transparent">
|
||||
<td>transparent_layers</td>
|
||||
<td style="background-color:#123456">explicit_cell</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>"#;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
table.rows[0].cells[0].props.shading,
|
||||
Some(craft_wordmodel::Rgb(0xe8, 0xf0, 0xfa)),
|
||||
"table background must show through transparent tbody, tr, and td"
|
||||
);
|
||||
assert_eq!(
|
||||
table.rows[0].cells[1].props.shading,
|
||||
Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)),
|
||||
"cell with explicit background must override table background"
|
||||
);
|
||||
}
|
||||
|
||||
/// Verify that block-level container background is available to immediate inline children using explicit `inherit` (#1786).
|
||||
#[test]
|
||||
fn block_background_explicit_inherit() {
|
||||
// When the immediate parent <p> has no background, an inline span inheriting background gets transparent (None):
|
||||
let html_indirect = r#"<div style="background-color: #ff0000"><p><span style="background-color: inherit">text</span></p></div>"#;
|
||||
let doc_indirect = import(html_indirect);
|
||||
let p_indirect = match &*doc_indirect.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_indirect.runs[0].props.shading, None);
|
||||
|
||||
// When the span is an immediate child of the styled div, it inherits the div's computed background:
|
||||
let html_direct = r#"<div style="background-color: #ff0000"><span style="background-color: inherit">text</span></div>"#;
|
||||
let doc_direct = import(html_direct);
|
||||
let p_direct = match &*doc_direct.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_direct.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
let html_shorthand = r#"<div style="background: #00ff00"><span style="background: inherit">text</span></div>"#;
|
||||
let doc_shorthand = import(html_shorthand);
|
||||
let p_shorthand = match &*doc_shorthand.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_shorthand.runs[0].props.shading, Some(craft_wordmodel::Rgb(0, 255, 0)));
|
||||
}
|
||||
|
||||
/// Verify that CSS-escaped quotes inside background URLs do not close the quote or drop declaration colors (#1786).
|
||||
#[test]
|
||||
fn background_escaped_quote_url() {
|
||||
let html = r#"<span style='background: #123456 url("a\"b.png")'>text</span>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
|
||||
let html_single = r#"<span style="background: #654321 url('a\'b.png')">text</span>"#;
|
||||
let doc_single = import(html_single);
|
||||
let p_single = match &*doc_single.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_single.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x65, 0x43, 0x21)));
|
||||
}
|
||||
|
||||
/// Verify that invalid hex color declarations like `#ggg0` do not clear inherited shading (#1786).
|
||||
#[test]
|
||||
fn background_invalid_hex_retains_shading() {
|
||||
let html = r#"<span style="background-color: #ff0000"><span style="background-color: #ggg0">text</span></span>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
/// Verify that background size keywords (`cover`, `contain`) without preceding position and slash are invalid and retain shading (#1786).
|
||||
#[test]
|
||||
fn background_cover_without_slash_invalid() {
|
||||
let html_cover = r#"<span style="background-color: #ff0000"><span style="background: cover">text</span></span>"#;
|
||||
let doc_cover = import(html_cover);
|
||||
let p_cover = match &*doc_cover.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_cover.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
let html_contain = r#"<span style="background-color: #ff0000"><span style="background: contain">text</span></span>"#;
|
||||
let doc_contain = import(html_contain);
|
||||
let p_contain = match &*doc_contain.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_contain.runs[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
let html_valid = r#"<span style="background-color: #ff0000"><span style="background: center / cover">text</span></span>"#;
|
||||
let doc_valid = import(html_valid);
|
||||
let p_valid = match &*doc_valid.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p_valid.runs[0].props.shading, None);
|
||||
}
|
||||
|
||||
/// Verify that table backgrounds declared on thead or tbody are applied to cells (#1786).
|
||||
#[test]
|
||||
fn table_row_group_background_inheritance() {
|
||||
let html = r#"<table>
|
||||
<thead style="background-color: #00ff00">
|
||||
<tr><th>head</th></tr>
|
||||
</thead>
|
||||
<tbody style="background-color: #0000ff">
|
||||
<tr><td>body</td></tr>
|
||||
</tbody>
|
||||
</table>"#;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0, 255, 0)));
|
||||
assert_eq!(table.rows[1].cells[0].props.shading, Some(craft_wordmodel::Rgb(0, 0, 255)));
|
||||
}
|
||||
|
||||
/// Verify that background shorthand with slashes inside URL functions correctly identifies the position/size slash (#1786).
|
||||
#[test]
|
||||
fn background_url_with_slashes_and_position_size() {
|
||||
let html = r#"<span style="background: url(/img/bg.png) center/cover #123456">text</span>"#;
|
||||
let doc = import(html);
|
||||
let p = match &*doc.body[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
}
|
||||
|
||||
/// Verify that a table inside an inline element with background shading does not turn character shading into table/cell background (#1786).
|
||||
#[test]
|
||||
fn table_inside_inline_element_does_not_inherit_character_shading() {
|
||||
let html = r#"<a style="background: #ff0000"><table><tr><td>cell</td></tr></table></a>"#;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(table.rows[0].cells[0].props.shading, None);
|
||||
}
|
||||
|
||||
/// Verify that a child with `background: inherit` inside an unstyled cell in a shaded table does not inherit the table fallback (#1786).
|
||||
#[test]
|
||||
fn cell_inherit_uses_cell_background_not_table_fallback() {
|
||||
let html = r#"<table style="background: #ff0000"><tr><td><span style="background: inherit">text</span></td></tr></table>"#;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
let p = match &*table.rows[0].cells[0].blocks[0] {
|
||||
craft_wordmodel::Block::Para(p) => p,
|
||||
_ => panic!("expected paragraph"),
|
||||
};
|
||||
assert_eq!(p.runs[0].props.shading, None);
|
||||
}
|
||||
|
||||
/// Verify that CSS `background: transparent` overrides HTML `bgcolor` presentational attribute (#1786).
|
||||
#[test]
|
||||
fn bgcolor_overridden_by_css_transparent() {
|
||||
let html = r##"<table bgcolor="#ff0000" style="background: transparent"><tr><td>cell</td></tr></table>"##;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(table.rows[0].cells[0].props.shading, None);
|
||||
}
|
||||
|
||||
/// Verify that nested container shading is preserved when merging list continuation paragraphs (#1786).
|
||||
#[test]
|
||||
fn list_continuation_preserves_nested_container_shading() {
|
||||
let html = r#"<ul><li>before<div style="background:#111111;color:white">inside</div>after</li></ul>"#;
|
||||
let flow = parse(html);
|
||||
assert_eq!(flow.blocks.len(), 1);
|
||||
let FBlock::Para(p) = &flow.blocks[0] else { panic!("expected Para") };
|
||||
assert!(p.list.is_some(), "expected list item");
|
||||
let dark = Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11));
|
||||
let inside = p
|
||||
.inlines
|
||||
.iter()
|
||||
.find_map(|i| match i {
|
||||
Inline::Text(t, fmt) if t.contains("inside") => Some(fmt.background),
|
||||
_ => None,
|
||||
})
|
||||
.expect("inside text inline");
|
||||
assert_eq!(inside, dark, "nested container shading should be preserved on merged run");
|
||||
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p_doc) = &*doc.body[0] else { panic!("expected Para") };
|
||||
let mut offset = 0usize;
|
||||
let mut inside_shading = None;
|
||||
for r in &p_doc.runs {
|
||||
let text_slice = &p_doc.text[offset..offset + r.len];
|
||||
if text_slice.contains("inside") {
|
||||
inside_shading = Some(r.props.shading);
|
||||
} else if text_slice.contains("before") || text_slice.contains("after") {
|
||||
assert_eq!(r.props.shading, None);
|
||||
}
|
||||
offset += r.len;
|
||||
}
|
||||
assert_eq!(inside_shading, Some(dark));
|
||||
}
|
||||
|
||||
/// Verify that starting a new row group implicitly closes any preceding sibling row group so
|
||||
/// cell shading is not inherited from an omitted-end-tag preceding group (#1786).
|
||||
#[test]
|
||||
fn consecutive_row_groups_with_omitted_end_tag_do_not_leak_shading() {
|
||||
let html = r#"<table><tbody style="background:red"><tr><td>a</td></tr><tbody><tr><td>b</td></tr></table>"#;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(table.rows.len(), 2);
|
||||
assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
assert_eq!(table.rows[1].cells[0].props.shading, None, "second cell should not inherit shading from closed tbody");
|
||||
}
|
||||
|
||||
/// Verify that a list item with a shaded container followed by unshaded text preserves separate
|
||||
/// background regions so the trailing text does not inherit the container's background (#1786).
|
||||
#[test]
|
||||
fn shaded_container_first_in_list_item_preserves_unshaded_trailing_text() {
|
||||
let html = r#"<ul><li style="color:#111111"><div style="background:#111111;color:white">inside</div>after</li></ul>"#;
|
||||
let doc = import(html);
|
||||
assert_eq!(doc.body.len(), 1);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, None, "list paragraph as a whole should have no background when regions differ");
|
||||
let dark = Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11));
|
||||
let mut offset = 0usize;
|
||||
let mut inside_shading = None;
|
||||
let mut after_shading = None;
|
||||
for r in &p.runs {
|
||||
let text_slice = &p.text[offset..offset + r.len];
|
||||
if text_slice.contains("inside") {
|
||||
inside_shading = Some(r.props.shading);
|
||||
} else if text_slice.contains("after") {
|
||||
after_shading = Some(r.props.shading);
|
||||
}
|
||||
offset += r.len;
|
||||
}
|
||||
assert_eq!(inside_shading, Some(dark));
|
||||
assert_eq!(after_shading, Some(None), "trailing text should have no shading");
|
||||
}
|
||||
|
||||
/// Verify that container backgrounds behind tables are preserved on table cells (#1786).
|
||||
#[test]
|
||||
fn container_background_behind_table_preserved_on_cells() {
|
||||
let html = r#"<div style="background:#111111;color:white"><table><tr><td>text</td></tr></table></div>"#;
|
||||
let doc = import(html);
|
||||
let table = doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)));
|
||||
|
||||
// Nested table inside an unshaded cell stops at the cell boundary:
|
||||
let nested_html = r#"<div style="background:#111111"><table><tr><td><table><tr><td>nested</td></tr></table></td></tr></table></div>"#;
|
||||
let nested_doc = import(nested_html);
|
||||
let outer_table = nested_doc
|
||||
.body
|
||||
.iter()
|
||||
.find_map(|b| match &**b {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(outer_table.rows[0].cells[0].props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)));
|
||||
let inner_table = match &*outer_table.rows[0].cells[0].blocks[0] {
|
||||
craft_wordmodel::Block::Table(t) => t,
|
||||
_ => panic!("expected nested Table"),
|
||||
};
|
||||
assert_eq!(inner_table.rows[0].cells[0].props.shading, None, "nested table in unshaded cell must stop at cell boundary");
|
||||
}
|
||||
|
||||
/// Verify that page imports preserve root (body/html) backgrounds while pasted fragments do not (#1786).
|
||||
#[test]
|
||||
fn root_background_preserved_on_page_import_only() {
|
||||
let html = r#"<body style="background:#111111;color:white"><p>text</p></body>"#;
|
||||
let page_doc = import_page(html, &|_| None);
|
||||
let craft_wordmodel::Block::Para(p_page) = &*page_doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p_page.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)), "page import must preserve body background on paragraphs");
|
||||
assert_eq!(
|
||||
p_page.runs[0].props.color,
|
||||
Some(craft_wordmodel::TextColor::Rgb(craft_wordmodel::Rgb(0xff, 0xff, 0xff))),
|
||||
"page import keeps the root foreground with its background"
|
||||
);
|
||||
|
||||
let frag_doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p_frag) = &*frag_doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p_frag.props.shading, None, "fragment paste must not turn body background into paragraph shading");
|
||||
assert_eq!(p_frag.runs[0].props.color, None, "fragment paste drops the root foreground with its background");
|
||||
}
|
||||
|
||||
/// Verify that an inline child with `background: inherit` does not receive character shading from a pasted body background (#1786).
|
||||
#[test]
|
||||
fn pasted_body_background_not_inherited_by_inline_child() {
|
||||
let html = r#"<body style="background:#111111"><span style="background:inherit">text</span></body>"#;
|
||||
let frag_doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p_frag) = &*frag_doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p_frag.runs[0].props.shading, None, "inline child in pasted fragment must not inherit character shading from body background");
|
||||
|
||||
let page_doc = import_page(html, &|_| None);
|
||||
let craft_wordmodel::Block::Para(p_page) = &*page_doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(
|
||||
p_page.runs[0].props.shading,
|
||||
Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)),
|
||||
"inline child in page import inherits body background via inherit"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_background_on_html_and_foreground_on_body() {
|
||||
let html = r#"<html style="background:#111111"><body style="color:white"><p>text</p></body></html>"#;
|
||||
let frag_doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p_frag) = &*frag_doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p_frag.props.shading, None, "fragment paste drops html background");
|
||||
assert_eq!(p_frag.runs[0].props.color, None, "fragment paste drops body foreground when html supplied suppressed background");
|
||||
|
||||
let page_doc = import_page(html, &|_| None);
|
||||
let craft_wordmodel::Block::Para(p_page) = &*page_doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p_page.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)));
|
||||
assert_eq!(p_page.runs[0].props.color, Some(craft_wordmodel::TextColor::Rgb(craft_wordmodel::Rgb(0xff, 0xff, 0xff))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn background_unitless_zero_lengths() {
|
||||
let html = r#"<div style="background:#123456 0.0 0"><p>text</p></div>"#;
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x12, 0x34, 0x56)));
|
||||
|
||||
let html2 = r#"<div style="background:#654321 -0 +0.0"><p>text</p></div>"#;
|
||||
let doc2 = import(html2);
|
||||
let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x65, 0x43, 0x21)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn background_comments_skipped_outside_strings() {
|
||||
let html = r#"<div style="background:#111111 /* dark */;color:white"><p>text</p></div>"#;
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)));
|
||||
|
||||
let html2 = r#"<div style="background:#222222 /* comment / with / slashes */"><p>text</p></div>"#;
|
||||
let doc2 = import(html2);
|
||||
let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(0x22, 0x22, 0x22)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_color_does_not_clear_preceding_background() {
|
||||
let html = r#"<div style="background-color:red;background-color:rgba(bogus / 0)"><p>text</p></div>"#;
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
|
||||
let html2 = r#"<div style="background:red;background:rgba(bogus / 0)"><p>text</p></div>"#;
|
||||
let doc2 = import(html2);
|
||||
let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p2.props.shading, Some(craft_wordmodel::Rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_block_export_preserves_foreground_color() {
|
||||
let html = r#"<pre style="background:#000000;color:#ffffff"><code>const x = 1;</code></pre>"#;
|
||||
let doc = import_page(html, &|_| None);
|
||||
let exported = export(&doc);
|
||||
assert!(exported.contains("style=\"background-color:#000000\""), "exported pre must keep background: {}", exported);
|
||||
assert!(exported.contains("color:#FFFFFF"), "exported code inlines must keep foreground: {}", exported);
|
||||
assert!(exported.contains("const x = 1;"), "exported code text preserved: {}", exported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn background_comments_inside_functions() {
|
||||
let html = r#"<div style="background:linear-gradient(/* stops */ #111111,#222222) #111111;color:white"><p>text</p></div>"#;
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, Some(craft_wordmodel::Rgb(0x11, 0x11, 0x11)));
|
||||
assert_eq!(p.runs[0].props.color, Some(craft_wordmodel::TextColor::Rgb(craft_wordmodel::Rgb(0xff, 0xff, 0xff))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_alpha_colors_with_none_channels() {
|
||||
let html = r#"<div style="background-color:red;background-color:rgb(none none none / 0)"><p>text</p></div>"#;
|
||||
let doc = import(html);
|
||||
let craft_wordmodel::Block::Para(p) = &*doc.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p.props.shading, None);
|
||||
|
||||
let html2 = r#"<div style="background:red;background:rgb(none none none / 0)"><p>text</p></div>"#;
|
||||
let doc2 = import(html2);
|
||||
let craft_wordmodel::Block::Para(p2) = &*doc2.body[0] else { panic!("expected Para") };
|
||||
assert_eq!(p2.props.shading, None);
|
||||
}
|
||||
|
||||
@@ -353,8 +353,16 @@ Rec. 2020 RGB as 16-bit BT.2100 PQ, with measured content-light levels and the r
|
||||
* Adam7 PNG against progressive PNG and against the `image` crate as an oracle;
|
||||
* limit enforcement.
|
||||
|
||||
If `corpus/pngsuite/*.png` exists at the repo root, every file in it is compared against the
|
||||
`image` crate's decoder. Without it, that test is skipped.
|
||||
With the `corpus` feature, the PngSuite tests read `corpus/pngsuite/` at the repo root. They
|
||||
take the PNGs that `tests/it/common/pngsuite.tsv` lists (one `sha256<TAB>name` row per file),
|
||||
check each file's SHA-256 before decoding it, and compare it against the `image` crate's
|
||||
decoder. A listed PNG that is missing or changed fails the tests; a file the list does not name is
|
||||
not read. To fill the folder, download
|
||||
[PngSuite-2017jul19.tgz](http://www.schaik.com/pngsuite/PngSuite-2017jul19.tgz) from the
|
||||
[PngSuite page](http://www.schaik.com/pngsuite/), check that its SHA-256 is
|
||||
`0294b244c95a8342c01b00010cf34abdcabc7c6a34fd0fe1bd963917537bfdc8`, and extract it into
|
||||
`corpus/pngsuite/`. No PngSuite file is committed here; the list links the suite's
|
||||
[terms](http://www.schaik.com/pngsuite/PngSuite.LICENSE).
|
||||
|
||||
With the `corpus` feature, the deep EXR test reads `11.deep.exr`, `42.deep.exr`, `64.deep.exr`,
|
||||
`multivariate.deep.exr` and `objectid.deep.exr` from `corpus/exr/` at the repo root. These are the
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! image generators, comparison metrics.
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub mod pngsuite;
|
||||
pub mod tiffgen;
|
||||
|
||||
use craft_image_codecs::*;
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
//! The pinned PngSuite. `pngsuite.tsv` lists every file of the suite as a `sha256<TAB>name` row,
|
||||
//! with `#` comment lines; further tab-separated columns are allowed and ignored. The corpus tests
|
||||
//! read only the listed PNGs from `corpus/pngsuite/` at the repo root, and only after each file's
|
||||
//! SHA-256 matches its row. A file the list does not name is never read.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
/// The pinned list.
|
||||
pub const MANIFEST: &str = include_str!("pngsuite.tsv");
|
||||
|
||||
/// The largest file read from the corpus folder. The suite's largest file is under 5 KiB.
|
||||
pub const MAX_FILE_BYTES: u64 = 4 * 1024 * 1024;
|
||||
|
||||
/// `corpus/pngsuite` at the repo root.
|
||||
pub fn dir() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite")
|
||||
}
|
||||
|
||||
/// The rows of `manifest` as name to lowercase hex SHA-256. A row needs a 64-digit lowercase hex
|
||||
/// digest and a plain file name (ASCII letters, digits, `.`, `_` and `-`, not starting with `.`),
|
||||
/// and each name may appear once.
|
||||
pub fn parse(manifest: &str) -> Result<BTreeMap<&str, &str>, String> {
|
||||
let mut rows = BTreeMap::new();
|
||||
for (number, line) in manifest.lines().enumerate().map(|(i, l)| (i + 1, l)) {
|
||||
if line.is_empty() || line.starts_with('#') {
|
||||
continue;
|
||||
}
|
||||
let mut cols = line.split('\t');
|
||||
let (Some(digest), Some(name)) = (cols.next(), cols.next()) else {
|
||||
return Err(format!("line {number}: expected a SHA-256, a tab and a name"));
|
||||
};
|
||||
if digest.len() != 64 || !digest.bytes().all(|c| matches!(c, b'0'..=b'9' | b'a'..=b'f')) {
|
||||
return Err(format!("line {number}: invalid SHA-256 for {name:?}"));
|
||||
}
|
||||
if name.is_empty() || name.starts_with('.') || !name.bytes().all(|c| c.is_ascii_alphanumeric() || b"._-".contains(&c)) {
|
||||
return Err(format!("line {number}: invalid file name {name:?}"));
|
||||
}
|
||||
if rows.insert(name, digest).is_some() {
|
||||
return Err(format!("line {number}: {name} is listed twice"));
|
||||
}
|
||||
}
|
||||
if rows.is_empty() {
|
||||
return Err("the manifest lists no files".into());
|
||||
}
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
/// The lowercase hex SHA-256 of `bytes`.
|
||||
pub fn sha256_hex(bytes: &[u8]) -> String {
|
||||
Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
/// Reads a regular file of at most `limit` bytes. A symbolic link, a folder or a larger file is an
|
||||
/// error, and a file that grows during the read is cut off at `limit + 1` bytes and refused.
|
||||
fn read_file(path: &Path, limit: u64) -> Result<Vec<u8>, String> {
|
||||
let meta = std::fs::symlink_metadata(path).map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
if !meta.is_file() || meta.len() > limit {
|
||||
return Err(format!("{}: expected a regular file of at most {limit} bytes", path.display()));
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
std::fs::File::open(path).and_then(|f| f.take(limit + 1).read_to_end(&mut bytes)).map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
if bytes.len() as u64 > limit {
|
||||
return Err(format!("{}: exceeds {limit} bytes", path.display()));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Every `.png` that `manifest` lists, read from `dir` in name order with its bytes. A listed PNG
|
||||
/// that is missing, is not a regular file or does not match its SHA-256 is an error.
|
||||
pub fn pinned_pngs(dir: &Path, manifest: &str) -> Result<Vec<(String, Vec<u8>)>, String> {
|
||||
let rows = parse(manifest)?;
|
||||
let mut out = Vec::new();
|
||||
for (name, digest) in rows.into_iter().filter(|(name, _)| name.ends_with(".png")) {
|
||||
let bytes = read_file(&dir.join(name), MAX_FILE_BYTES)?;
|
||||
let got = sha256_hex(&bytes);
|
||||
if got != digest {
|
||||
return Err(format!("{name}: SHA-256 {got} does not match the pinned {digest}"));
|
||||
}
|
||||
out.push((name.to_string(), bytes));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The pinned PNGs of `corpus/pngsuite/`. A missing or changed file fails the calling test.
|
||||
pub fn pngs() -> Vec<(String, Vec<u8>)> {
|
||||
let dir = dir();
|
||||
pinned_pngs(&dir, MANIFEST).unwrap_or_else(|e| {
|
||||
panic!("{}: {e}; extract the archive named in tests/it/common/pngsuite.tsv into this folder (see the crate README)", dir.display())
|
||||
})
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user