mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-02 14:47:05 -06:00
Merge remote-tracking branch 'upstream/main' into 1180-import-shortcuts
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
version: 2
|
||||
updates:
|
||||
# Maintain Rust / Cargo dependencies
|
||||
- package-ecosystem: "cargo"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
||||
time: "06:00"
|
||||
timezone: "America/New_York"
|
||||
# Consolidate updates into a single PR rather than one PR per crate
|
||||
groups:
|
||||
cargo-dependencies:
|
||||
patterns:
|
||||
- "*"
|
||||
commit-message:
|
||||
prefix: "chore(deps)"
|
||||
|
||||
# Maintain GitHub Actions workflows
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
||||
time: "06:00"
|
||||
timezone: "America/New_York"
|
||||
groups:
|
||||
actions:
|
||||
patterns:
|
||||
- "*"
|
||||
commit-message:
|
||||
prefix: "ci(deps)"
|
||||
@@ -4,6 +4,7 @@ plan/
|
||||
/dist
|
||||
/corpus
|
||||
/log
|
||||
/outputs/
|
||||
.DS_Store
|
||||
/.claude/
|
||||
/target-*/
|
||||
|
||||
Generated
+29
-21
@@ -824,6 +824,11 @@ dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-docpath"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=82a24b006eefa036b43c09a67fb9a3fbd17ffe63#82a24b006eefa036b43c09a67fb9a3fbd17ffe63"
|
||||
|
||||
[[package]]
|
||||
name = "craft-pdf"
|
||||
version = "0.1.0"
|
||||
@@ -924,7 +929,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"arboard",
|
||||
"designcraft-engine",
|
||||
@@ -946,7 +951,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-cli"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -962,7 +967,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-color"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"craft-cms",
|
||||
"kurbo",
|
||||
@@ -974,7 +979,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-compose"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-doc",
|
||||
@@ -994,7 +999,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-doc"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-geom",
|
||||
@@ -1006,10 +1011,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-engine"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"caseless",
|
||||
"craft-atomic",
|
||||
"craft-docpath",
|
||||
"craft-print",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1044,7 +1050,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-epub"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-doc",
|
||||
@@ -1053,7 +1059,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-fonts"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"core-text",
|
||||
"harfrust 0.13.3",
|
||||
@@ -1066,9 +1072,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-format"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-doc",
|
||||
"designcraft-images",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1077,7 +1084,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-geom"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"flo_curves",
|
||||
"kurbo",
|
||||
@@ -1090,12 +1097,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-idml"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
"designcraft-doc",
|
||||
"designcraft-geom",
|
||||
"designcraft-images",
|
||||
"log",
|
||||
"quick-xml",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1104,7 +1112,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-images"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"craft-cms",
|
||||
"designcraft-color",
|
||||
@@ -1119,7 +1127,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-mcp"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-engine",
|
||||
"designcraft-geom",
|
||||
@@ -1132,7 +1140,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-pdf"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"craft-pdf",
|
||||
"designcraft-color",
|
||||
@@ -1154,7 +1162,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-pptx"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1171,7 +1179,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-render"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1189,7 +1197,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-textimport"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-doc",
|
||||
"quick-xml",
|
||||
@@ -1198,7 +1206,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-tools"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1210,7 +1218,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-ui-egui"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"craft-ui",
|
||||
"designcraft-color",
|
||||
@@ -1237,7 +1245,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-web"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"designcraft-engine",
|
||||
"designcraft-format",
|
||||
@@ -5788,7 +5796,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
+3
-1
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/designcraft", "apps/designcraft-cli", "xtask"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
@@ -73,6 +73,8 @@ zip = { version = "3", default-features = false, features = ["deflate"] }
|
||||
# (MIT OR Apache-2.0, no dependencies, builds for wasm).
|
||||
indd = { git = "https://github.com/storytold/indd-utils", branch = "main" }
|
||||
|
||||
# Which paths stored in documents may be followed (craft-libs).
|
||||
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "82a24b006eefa036b43c09a67fb9a3fbd17ffe63" }
|
||||
# Atomic file replacement for saves and exports (craft-libs).
|
||||
craft-atomic = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e9546e191dd62f2bc1d61eca45dbdc2f1b3c1d8e" }
|
||||
# File › Print through the system print spooler (craft-libs).
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
<h1 align="center">DesignCraft</h1>
|
||||
|
||||
<p align="center">
|
||||
<b>Page layout and publishing; an open-source, clean-room reimplementation of Adobe InDesign, rebuilt in pure Rust.</b>
|
||||
<b>Page layout and publishing; an open-source, modern desktop publishing and page layout studio, built in pure Rust.</b>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
A fast, open-source, clean-room take on the Adobe InDesign workflow. It runs natively on macOS,
|
||||
A fast, open-source desktop publishing and page layout workflow. It runs natively on macOS,
|
||||
Windows and Linux, and in the browser via WebAssembly.<br>
|
||||
<i>By the ArtCraft team.</i>
|
||||
</p>
|
||||
@@ -78,9 +78,9 @@ by its own renderer. Try it yourself with `File → New → Sample Document`, or
|
||||
|
||||
## Why DesignCraft
|
||||
|
||||
- **Familiar.** InDesign's layout, tools, menus, panels and shortcuts: spreads and parent pages,
|
||||
frames and threaded stories, the Control panel, paragraph and character styles, swatches, text
|
||||
wrap and more. If you know InDesign, you already know how to use it.
|
||||
- **Familiar.** A familiar layout, tools, menus, panels and shortcuts: spreads and parent pages,
|
||||
master pages, text frames, threaded text, paragraph and character styles, tables, text wrap and
|
||||
more. Everything is right where you expect it.
|
||||
- **Beautiful type.** A Knuth–Plass paragraph composer (plus single-line), dictionary hyphenation
|
||||
(the public-domain Moby word list plus our own trained patterns), word, letter and glyph-scaling
|
||||
justification, keeps, optical margin alignment, columns, baseline grid, tabs, rules and shading.
|
||||
@@ -88,7 +88,7 @@ by its own renderer. Try it yourself with `File → New → Sample Document`, or
|
||||
- **Fast.** Multithreaded SIMD rendering (vello_cpu), copy-on-write documents with O(1) undo
|
||||
snapshots, and cached composition.
|
||||
- **Open.** A documented native format, IDML import and export, INDD import (through
|
||||
[indd-utils](https://github.com/storytold/indd-utils)), editable PowerPoint export, and PNG, PDF
|
||||
[indd-utils](https://github.com/storytold/indd-utils)), editable .pptx export, and PNG, PDF
|
||||
(PDF/X-4, PDF/A-2b) and EPUB export.
|
||||
|
||||
No subscription, no licence server, no telemetry.
|
||||
@@ -247,10 +247,10 @@ egui frontend is a separate crate, so the UI can be swapped without touching the
|
||||
| L3 | `render` (vello_cpu) |
|
||||
| L4 | `tools` (pointer events → commands + overlays) |
|
||||
| L5 | `engine` (session, history, command registry) |
|
||||
| L6 | `ui-egui` (InDesign-style UI, control channel) |
|
||||
| L6 | `ui-egui` (publishing UI, control channel) |
|
||||
| L7 | `apps/designcraft`, `apps/designcraft-cli`, `apps/designcraft-web` |
|
||||
|
||||
- **Status and milestones:** [ROADMAP.md](ROADMAP.md) (stage: pre-alpha); how close to InDesign: [`docs/target-app-parity.md`](docs/target-app-parity.md); known gaps: [`docs/gaps.md`](docs/gaps.md)
|
||||
- **Status and milestones:** [ROADMAP.md](ROADMAP.md) (stage: pre-alpha); feature assessment: [`docs/target-app-parity.md`](docs/target-app-parity.md); known gaps: [`docs/gaps.md`](docs/gaps.md)
|
||||
- **Contributor and agent rules** (clean-room, asset policy, quality gates): [`AGENTS.md`](AGENTS.md)
|
||||
- **Bundled assets:** every one is listed with its licence in [`ASSETS.md`](ASSETS.md)
|
||||
- **App icon and colour:** a calico cat in a polka-dot scarf on DesignCraft green `#7bb51c`; see [`assets/app-icon/`](assets/app-icon/README.md)
|
||||
@@ -373,8 +373,6 @@ ArtCraft Team and are not covered by this license. They may be used only unmodif
|
||||
part of this repository and DesignCraft, under [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt).
|
||||
Forks and modified versions must remove them.
|
||||
|
||||
<sub>Adobe, Photoshop, Illustrator, Premiere Pro, Lightroom, Acrobat, After Effects and InDesign are trademarks or registered trademarks of Adobe Inc. in the United States and/or other countries. DesignCraft is an independent, open-source project and is not affiliated with, sponsored by or endorsed by Adobe Inc.; these names are used only to describe the workflows it is compatible with.</sub>
|
||||
|
||||
<br>
|
||||
|
||||
<p align="center">
|
||||
|
||||
+8
-1
@@ -186,7 +186,7 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, ope
|
||||
## How far from full parity (estimate, 2026-10-04)
|
||||
|
||||
**Breadth: ~99% weighted** (P0 core 99%, P1 100%, P2 97%) over the 275 features of the InDesign catalogue, scored
|
||||
row by row in [docs/parity.md](docs/parity.md) (`cargo xtask parity` recomputes it). Many features scored done still
|
||||
row by row in [docs/parity-checklist.md](docs/parity-checklist.md) (`cargo xtask parity` recomputes it). Many features scored done still
|
||||
lack some of InDesign's options or dialog details, so **overall parity including depth is about 83%**.
|
||||
|
||||
**Remaining work: about 170 wall-clock hours of a single Claude Opus 5.5 agent** (±30%), or roughly 60–85 hours with
|
||||
@@ -349,6 +349,13 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m
|
||||
Newest first.
|
||||
|
||||
- **2026-10-16** · Edit › Keyboard Shortcuts imports a set saved by InDesign's Show Set (.txt, Windows or Mac key names, UTF-8 or UTF-16): matched commands take its Default shortcuts or lose theirs, other commands give up keys it takes, and a report lists what it changed and the items it could not match (`window.importShortcuts`, #1180). Tool keys stay fixed. No readiness percentage change.
|
||||
- **2026-10-16** · Type › Reload Fonts (`font.reload`): fonts installed, edited or removed while DesignCraft runs are read again without a restart, and the open documents recompose with them (#328, #547). An edited font file is noticed by its length and modification time; bundled, added and document fonts stay as they are. InDesign does this by itself, so noticing changes automatically is still open (gap 6). No readiness percentage change.
|
||||
- **2026-10-16** · Parents can be deleted from their Pages-panel context menu or `layout.parents.delete {id}`. Applied-page and based-on references are cleared, local override copies survive, and Undo restores the parent. Addresses the deletion portion of #956; no readiness percentage change.
|
||||
- **2026-10-17** · File › Package writes the document's and the IDML's links relative to the package (`Links/logo.png`, `file:Links/logo.png`), so a package holds no path from the machine that made it; opening a document resolves them to the files beside it. Links › Copy Link(s) To still links by full path. (#340). No readiness percentage change.
|
||||
- **2026-10-18** · View › Screen Mode: Bleed and Slug do what they say. Both were selectable from the toolbar, the View menu and `view.screenMode` but drew the canvas exactly as Normal did, guides and all; they are preview modes now, and what each page shows reaches the bleed box or the slug box instead of stopping at the trim. The slug guide is drawn as well, in the Slug colour from Preferences › Guides & Pasteboard, which nothing had read. No readiness percentage change.
|
||||
- **2026-10-17** · Object strokes reject malformed types and unknown type fields before changing the document; command help lists every stroke kind and its required fields. No readiness percentage change.
|
||||
- **2026-10-17** · Docs: documented a persistent source CLI install with Cargo, MCP-client PATH setup and bare-name server registration in `docs/mcp.md`. No readiness percentage change.
|
||||
|
||||
- **2026-10-16** · Threading: the loaded text cursor threads into a text frame inside a group (it hit the group and showed it could not thread); a plain click still selects the group (#1075). Gap 27 closed. No readiness percentage change.
|
||||
- **2026-10-16** · Greek text hyphenates. The patterns were never compiled in, so Greek set unhyphenated however narrow the column; `"Greek"`, `"Greek: Greece"`, `el` and `el-GR` all resolve now and ανθρωπος breaks αν-θρω-πος. They are the monotonic patterns (hyph-el-monoton, Dimitrios Filippou, under MIT), which is what polytonic text hyphenates by too. Five of InDesign's ~40 hyphenation languages. No readiness percentage change.
|
||||
- **2026-10-16** · A swatch applied to a placed greyscale or bitmap image colours it, as in InDesign: IDML's `<Image FillColor>` is read and written, greyscale pixels become a tint of the swatch on opaque white and a bitmap's white lets the frame show through, on screen and in PDF (spot swatches print on their plate). Colour images keep their colours. Applying the swatch from the Swatches panel comes next. No readiness percentage change.
|
||||
|
||||
@@ -62,6 +62,7 @@ References to earlier results (in string parameters):
|
||||
"$$" a literal `$` anywhere: "$$1" passes `$1` (a GREP group), "$${0.id}" passes `${0.id}`.
|
||||
|
||||
Text parameters: "\n" starts a new paragraph, "\u2028" is a forced line break, "\t" a tab.
|
||||
"\u000b" (Word's line break, Chr(11)) is read as a forced line break too.
|
||||
Most commands ignore parameters they do not know instead of rejecting them: check names
|
||||
with `designcraft-cli describe COMMAND`.
|
||||
|
||||
@@ -330,7 +331,10 @@ fn run(args: &[String]) -> Result<(), String> {
|
||||
}
|
||||
}
|
||||
"--page" => page = val()?.parse().map_err(|_| "bad --page")?,
|
||||
"--scale" => scale = val()?.parse().map_err(|_| "bad --scale")?,
|
||||
"--scale" => {
|
||||
// 0, a negative value and NaN made a 1×1 image; infinity, "no such page".
|
||||
scale = val()?.parse().ok().filter(|s: &f64| s.is_finite() && *s > 0.0).ok_or("bad --scale: expected a positive number")?;
|
||||
}
|
||||
"--pdf-options" => {
|
||||
// The export adds its path to them.
|
||||
pdf_opts = match serde_json::from_str(&val()?).map_err(|e| format!("--pdf-options: {e}"))? {
|
||||
|
||||
@@ -252,6 +252,22 @@ fn run_export_options_come_before_their_export() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// `--scale 0`, a negative value or `nan` wrote a 1×1 PNG and exited 0; `inf` said "no such page".
|
||||
#[test]
|
||||
fn run_scale_that_is_not_finite_and_positive_is_an_error() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-cli-scale-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let png = dir.join("a.png");
|
||||
for scale in ["nan", "0", "-2", "inf", "-inf"] {
|
||||
let out = cli().args(["run", "--sample", "--scale", scale, "--export", png.to_str().unwrap()]).output().unwrap();
|
||||
let err = String::from_utf8_lossy(&out.stderr);
|
||||
assert!(!out.status.success(), "--scale {scale}: {err}");
|
||||
assert!(err.contains("bad --scale"), "--scale {scale}: {err}");
|
||||
}
|
||||
assert!(!png.exists(), "nothing is exported when the scale is wrong");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// `--pdf-options` must be a JSON object; any other JSON value used to panic when the PDF path was set on it.
|
||||
#[test]
|
||||
fn pdf_options_that_are_not_an_object_are_an_error_not_a_panic() {
|
||||
|
||||
@@ -138,13 +138,18 @@ pub fn choose(candidates: &[Backend], previous: &Record, version: &str, skipped:
|
||||
let first = *candidates.first()?;
|
||||
let same_version = previous.version == version;
|
||||
let mut failed: Vec<Backend> = if same_version { previous.failed.clone() } else { Vec::new() };
|
||||
// A stored entry that is no candidate here came from another machine and says nothing. The
|
||||
// backend the previous start was still on is added after this and stays, candidate or not:
|
||||
// the instance can load a backend that is no candidate — everywhere but Windows OpenGL rides
|
||||
// along beside the chosen one (`instance_backends`) — and a start that one took down must
|
||||
// keep it out of the next instance.
|
||||
failed.retain(|b| candidates.contains(b));
|
||||
let unfinished = previous.trying.filter(|_| same_version);
|
||||
if let Some(b) = unfinished
|
||||
&& !failed.contains(&b)
|
||||
{
|
||||
failed.push(b);
|
||||
}
|
||||
failed.retain(|b| candidates.contains(b));
|
||||
let pick = |failed: &[Backend]| candidates.iter().copied().find(|b| !failed.contains(b) && !skipped.contains(b));
|
||||
let backend = match pick(&failed) {
|
||||
Some(b) => b,
|
||||
@@ -387,6 +392,23 @@ mod tests {
|
||||
assert_eq!(c.record.failed, &[Backend::Vulkan]);
|
||||
}
|
||||
|
||||
/// The backend the previous start was still on stays in `failed` even where it is no
|
||||
/// candidate: on macOS Metal is the only candidate, but the instance loads OpenGL beside it,
|
||||
/// so a start OpenGL took down has to keep it out of the next one. Only the stored list is
|
||||
/// filtered to this platform's candidates — those entries came from another machine.
|
||||
#[test]
|
||||
fn the_backend_a_start_was_on_counts_as_failed_even_where_it_is_no_candidate() {
|
||||
const MAC: &[Backend] = &[Backend::Metal];
|
||||
let c = choose(MAC, &record(Some(Backend::Gl), &[]), V, &[]).unwrap();
|
||||
assert_eq!(c.backend, Backend::Metal);
|
||||
assert_eq!(c.record.failed, &[Backend::Gl], "OpenGL ran here, so it failed here");
|
||||
assert_eq!(c.switched_from, Some(Backend::Gl));
|
||||
assert!(!instance_backends(c.backend, &c.record.failed).contains(wgpu::Backends::GL));
|
||||
// Stored on another machine, never run here: dropped, and OpenGL may ride along again.
|
||||
let c = choose(MAC, &record(None, &[Backend::Gl]), V, &[]).unwrap();
|
||||
assert_eq!(c.record.failed, &[]);
|
||||
}
|
||||
|
||||
/// A restart that found no adapter of DirectX 12 names the backend in its skip list: that start
|
||||
/// takes the next candidate, with or without a record, and the record doesn't keep it as failed
|
||||
/// (a later launch tries it again).
|
||||
|
||||
@@ -192,6 +192,10 @@ fn open_filters(purpose: &str) -> &'static [OpenFilter] {
|
||||
"book" => &[OpenFilter { name: "Book", extensions: &["dcbook"] }],
|
||||
"xml" => &[OpenFilter { name: "XML", extensions: &["xml"] }],
|
||||
"library" => &[OpenFilter { name: "Object Library", extensions: &["dclib"] }],
|
||||
"relink" => &[OpenFilter {
|
||||
name: "Graphics",
|
||||
extensions: &["png", "jpg", "jpeg", "gif", "webp", "tif", "tiff", "bmp", "psd", "svg", "pdf", "ai", "eps"],
|
||||
}],
|
||||
"dataMerge" => &[OpenFilter { name: "Data source (CSV, TSV, text, Excel)", extensions: &["csv", "tsv", "tab", "txt", "xlsx"] }],
|
||||
"place" => &[
|
||||
OpenFilter {
|
||||
@@ -553,6 +557,16 @@ fn main() -> std::process::ExitCode {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn relink_open_dialog_lists_graphics() {
|
||||
let filters = super::open_filters("relink");
|
||||
let exts: Vec<&str> = filters.iter().flat_map(|filter| filter.extensions.iter().copied()).collect();
|
||||
for ext in ["png", "jpg", "tif", "psd", "svg", "pdf", "ai", "eps"] {
|
||||
assert!(exts.contains(&ext), "{ext} is missing from the relink dialog: {exts:?}");
|
||||
}
|
||||
assert!(!exts.contains(&"designcraft") && !exts.contains(&"idml"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_port_values_that_are_not_ports_are_ignored() {
|
||||
assert_eq!(super::control_port_from("--control", Some("7979".into())), Some(7979));
|
||||
|
||||
@@ -65,3 +65,6 @@ sync_github_name = false
|
||||
real_name = "Marco Arnulfo"
|
||||
display_name = "Marco Arnulfo"
|
||||
sync_github_name = false
|
||||
|
||||
[people.4waymedia]
|
||||
real_name = "Paul Gemignani"
|
||||
|
||||
+35
-3
@@ -98,9 +98,11 @@ pub fn read(bytes: &[u8]) -> Result<Vec<Swatch>, AseError> {
|
||||
}
|
||||
}
|
||||
let color = match &model[..] {
|
||||
b"CMYK" => Color::Cmyk { c: b.f32()?, m: b.f32()?, y: b.f32()?, k: b.f32()? },
|
||||
b"RGB " => Color::Rgb { r: b.f32()?, g: b.f32()?, b: b.f32()? },
|
||||
b"Gray" => Color::Gray { k: 1.0 - b.f32()? },
|
||||
b"CMYK" => {
|
||||
Color::Cmyk { c: b.f32()?.clamp(0.0, 1.0), m: b.f32()?.clamp(0.0, 1.0), y: b.f32()?.clamp(0.0, 1.0), k: b.f32()?.clamp(0.0, 1.0) }
|
||||
}
|
||||
b"RGB " => Color::Rgb { r: b.f32()?.clamp(0.0, 1.0), g: b.f32()?.clamp(0.0, 1.0), b: b.f32()?.clamp(0.0, 1.0) },
|
||||
b"Gray" => Color::Gray { k: (1.0 - b.f32()?).clamp(0.0, 1.0) },
|
||||
b"LAB " => {
|
||||
let (l, a, bb) = (b.f32()?, b.f32()?, b.f32()?);
|
||||
let [r, g, bl] = cms::lab::lab_to_srgb(cms::Lab { l: l * 100.0, a, b: bb });
|
||||
@@ -231,6 +233,36 @@ mod tests {
|
||||
assert_eq!(read(&write(&sw[1..])).map(|w| w.len()), Ok(MAX_COLORS), "up to the limit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_components_are_clamped() {
|
||||
let mut f = Vec::new();
|
||||
f.extend(b"ASEF\x00\x01\x00\x00");
|
||||
f.extend(3u32.to_be_bytes());
|
||||
let cases: [(&[u8; 4], &[f32]); 3] = [(b"RGB ", &[5.0, -2.0, 0.5]), (b"CMYK", &[2.0, -1.0, 0.5, 9.0]), (b"Gray", &[-1.0])];
|
||||
for (model, vals) in cases {
|
||||
let mut c = Vec::new();
|
||||
c.extend(2u16.to_be_bytes());
|
||||
c.extend([0, b'X', 0, 0]);
|
||||
c.extend(model);
|
||||
for v in vals {
|
||||
c.extend(v.to_bits().to_be_bytes());
|
||||
}
|
||||
c.extend(2u16.to_be_bytes());
|
||||
f.extend(COLOR.to_be_bytes());
|
||||
f.extend((c.len() as u32).to_be_bytes());
|
||||
f.extend(&c);
|
||||
}
|
||||
let colors: Vec<Color> = read(&f)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter_map(|s| match s.value {
|
||||
SwatchValue::Color { color, .. } => Some(color),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(colors, [Color::Rgb { r: 1.0, g: 0.0, b: 0.5 }, Color::Cmyk { c: 1.0, m: 0.0, y: 0.5, k: 1.0 }, Color::Gray { k: 1.0 },]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_finite_components_are_skipped() {
|
||||
let sw = |name: &str, color: Color| Swatch {
|
||||
|
||||
@@ -563,7 +563,11 @@ fn overlong_words(items: &[Item], ig: &[usize], glyphs: &[Glyph], measure: f64)
|
||||
let marks = out.get_or_insert_with(|| vec![false; glyphs.len()]);
|
||||
for i in a..b {
|
||||
let (Some(g), Some(next)) = (glyphs.get(i), glyphs.get(i + 1)) else { continue };
|
||||
// Glyphs of one source character (an automatic page number's digits, a
|
||||
// multi-glyph cluster) stay together, as InDesign keeps them.
|
||||
let one_char = next.byte == g.byte && (next.len == 0 || next.len == g.len);
|
||||
let ok = !g.no_break
|
||||
&& !one_char
|
||||
&& g.break_after != Some(false)
|
||||
&& next.ch != SOFT_HYPHEN
|
||||
&& !is_forced(next.ch)
|
||||
|
||||
@@ -1716,7 +1716,8 @@ fn compose_with_db(doc: &Document, story: &Story, frames: &[FrameSpec], opts: &C
|
||||
drop_box = Some(DropCapBox {
|
||||
end: dc.end,
|
||||
baseline: baseline + dc.drop,
|
||||
rect: Rect::new(at, baseline - asc, at + dc.width, baseline + dc.drop + desc),
|
||||
// From its line's top, or its own top when it rises above that line.
|
||||
rect: Rect::new(at, (baseline - asc).min(baseline + dc.drop - dc.cap), at + dc.width, baseline + dc.drop + desc),
|
||||
});
|
||||
}
|
||||
ruby::annotate(db, &mut styles_tab, &story.text, &mut placed, doc.settings.glyph_fallback, f.vertical);
|
||||
@@ -3764,11 +3765,15 @@ struct DropCap {
|
||||
/// Its baseline below the first line's: `lines − 1` leadings of the text after it (grid steps
|
||||
/// on a baseline grid), wherever line `lines` is set.
|
||||
drop: f64,
|
||||
/// Its cap height as set (how far its top sits above its baseline).
|
||||
cap: f64,
|
||||
}
|
||||
|
||||
/// Take the drop cap (the glyphs before byte `end`) off the front of a paragraph's glyphs and scale
|
||||
/// it so its cap height reaches from the first line's cap height down to line `lines`' baseline,
|
||||
/// measured in the leading and cap height of the text after it. A one-line drop cap keeps its own
|
||||
/// measured in the leading and cap height of the text after it: the size of a cap set at that text's
|
||||
/// size. As in InDesign, a cap set larger (or smaller) is scaled by its size over that text's,
|
||||
/// keeping its baseline, so it rises above the first line (or falls short of it). A one-line drop cap keeps its own
|
||||
/// size when that is smaller. On a baseline grid of increment `grid`, lines are that many grid
|
||||
/// steps apart. With `align_left` its ink starts at the indent (see [`DropCap::lsb`]). Tabs inside
|
||||
/// it advance to the stops of `tabs` (stops and the start-side indent; in right-to-left paragraphs
|
||||
@@ -3796,14 +3801,19 @@ fn split_drop_cap(
|
||||
let mut after: Vec<Glyph> = glyphs.drain(..spaces).collect();
|
||||
let end = after.last().map_or(end, |g| g.byte.saturating_add(g.len).max(end));
|
||||
let body = glyphs.iter().find(|g| g.len > 0 && g.cap > 0.0);
|
||||
let (lead, cap) = body.map_or((base_leading, base_cap), |g| (g.leading, g.cap));
|
||||
let (lead, cap, body_size) = body.map_or((base_leading, base_cap, None), |g| (g.leading, g.cap, Some(g.size)));
|
||||
let lead = match grid.map(|inc| (lead / inc - 1e-6).ceil().max(1.0) * inc) {
|
||||
Some(pitch) if pitch.is_finite() && pitch > 0.0 => pitch,
|
||||
_ => lead,
|
||||
};
|
||||
let drop = (lines - 1) as f64 * lead;
|
||||
let own = dc.iter().map(|g| g.cap).fold(0.0, f64::max);
|
||||
let k = if own > 0.0 { (drop + cap) / own } else { 1.0 };
|
||||
// The cap's tallest glyph sets its height; its point size over the body's scales the fit.
|
||||
let (own, own_size) = dc.iter().fold((0.0, 0.0), |(c, s), g| if g.cap > c { (g.cap, g.size) } else { (c, s) });
|
||||
let ratio = match body_size {
|
||||
Some(b) if b > 0.0 && own_size > 0.0 && (own_size / b).is_finite() => own_size / b,
|
||||
_ => 1.0,
|
||||
};
|
||||
let k = if own > 0.0 { (drop + cap) / own * ratio } else { 1.0 };
|
||||
let k = if lines == 1 { k.min(1.0) } else { k };
|
||||
let k = if k.is_finite() && k > 0.0 { k.min(1000.0) } else { 1.0 };
|
||||
for g in dc.iter_mut().chain(after.iter_mut()) {
|
||||
@@ -3844,7 +3854,8 @@ fn split_drop_cap(
|
||||
}
|
||||
let width = dc.iter().map(|g| g.adv).sum();
|
||||
dc.append(&mut after);
|
||||
Some(DropCap { glyphs: dc, end, lines, width, drop, lsb })
|
||||
let cap = dc.iter().map(|g| g.cap).fold(0.0, f64::max);
|
||||
Some(DropCap { glyphs: dc, end, lines, width, drop, lsb, cap })
|
||||
}
|
||||
|
||||
impl DropCap {
|
||||
|
||||
@@ -125,7 +125,7 @@ fn compose_cell(doc: &Document, cell: &Cell, w: f64, opts: &ComposeOptions, left
|
||||
let spec = FrameSpec {
|
||||
id: ItemId(0),
|
||||
area: Rect::new(0.0, 0.0, w.max(1.0), 1.0e6),
|
||||
opts: TextFrameOptions::default(),
|
||||
opts: TextFrameOptions { first_baseline: cell.first_baseline, first_baseline_min: cell.first_baseline_min, ..TextFrameOptions::default() },
|
||||
vertical: false,
|
||||
exclusions: vec![],
|
||||
outline: None,
|
||||
@@ -835,6 +835,23 @@ mod height_tests {
|
||||
AnchoredObject::new(designcraft_doc::Item::new(ItemId(0), LayerId(0), designcraft_doc::Shape::Rectangle, path), position)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_first_baseline_option_sets_the_first_line_and_the_row_height() {
|
||||
// Cell Options › Text › First Baseline: Cap Height sets the first line by the cap height,
|
||||
// not the typographic ascent, so a row sized for one line of caps keeps its height.
|
||||
let ascent = text_cell("Hd", 4.0);
|
||||
let mut cap = ascent.clone();
|
||||
cap.first_baseline = designcraft_doc::FirstBaseline::CapHeight;
|
||||
let (a, c) = (measure(&ascent, 92.0), measure(&cap, 92.0));
|
||||
let (la, lc) = (&a.text.frames[0].lines[0], &c.text.frames[0].lines[0]);
|
||||
assert!(lc.baseline < la.baseline, "cap height {} should sit above ascent {}", lc.baseline, la.baseline);
|
||||
assert!(c.content_h < a.content_h);
|
||||
let mut fixed = ascent.clone();
|
||||
fixed.first_baseline = designcraft_doc::FirstBaseline::Fixed;
|
||||
fixed.first_baseline_min = 6.0;
|
||||
assert!((measure(&fixed, 92.0).text.frames[0].lines[0].baseline - 6.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_bottom_inset_contains_descenders_without_growing_every_row() {
|
||||
let cell = text_cell("gyp", 8.5);
|
||||
|
||||
@@ -474,6 +474,23 @@ fn jump_object_keeps_grid_aligned_lines_on_the_grid() {
|
||||
assert!((l.baseline - want).abs() < 1e-6, "{} vs {want}", l.baseline);
|
||||
}
|
||||
|
||||
/// An automatic page number wider than its frame stays on one line and hangs out, as in InDesign
|
||||
/// ("136" in a frame made for two digits); breaking it put "13" over "4".
|
||||
#[test]
|
||||
fn page_number_wider_than_its_frame_stays_whole() {
|
||||
let text = designcraft_doc::story::PAGE_NUMBER.to_string();
|
||||
let (d, sid, _) = doc_with(&text, Rect::new(0.0, 0.0, 10.0, 40.0), ParaAttrs::default());
|
||||
let mut specs = frame_specs(&d, sid);
|
||||
specs[0].page_name = Some("136".into());
|
||||
let cs = compose(&d, d.story(sid).unwrap(), &specs, &ComposeOptions::default());
|
||||
let lines = all_lines(&cs);
|
||||
assert_eq!(lines.len(), 1, "{lines:?}");
|
||||
assert!(!cs.is_overset());
|
||||
// A word of ordinary letters that doesn't fit still breaks where it must.
|
||||
let (d, sid, _) = doc_with("Wordwiderthanframe", Rect::new(0.0, 0.0, 30.0, 200.0), ParaAttrs::default());
|
||||
assert!(all_lines(&compose_story(&d, sid, &ComposeOptions::default())).len() > 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_wrap_geometry_terminates() {
|
||||
let (d, sid) = wrap_doc(ParaAttrs { grid_align: Some(GridAlign::AllLines), ..Default::default() }, &[]);
|
||||
@@ -3646,6 +3663,37 @@ fn right_to_left_drop_cap_sets_its_tabs_from_the_right_edge() {
|
||||
assert!(right <= 260.0 + 1e-6 && right > 255.0, "text ends at {right}");
|
||||
}
|
||||
|
||||
/// The cap's point size scales it, as in InDesign: at the body's size (10 pt) it fills its lines
|
||||
/// exactly; set larger or smaller it is scaled by its size over the body's, keeping its baseline,
|
||||
/// so it rises above the first line (or falls short of it). The body's lines don't move, and the
|
||||
/// cap's box (highlight, caret) reaches the top of a cap that rises above its line.
|
||||
#[test]
|
||||
fn drop_cap_scales_with_its_point_size() {
|
||||
let set = |size: f64| {
|
||||
let (mut d, sid, _) = drop_doc(&[LOREM; 2].join(" "), drop_cap(3, 1));
|
||||
d.story_mut(sid).unwrap().format_chars(0..1, |f| f.over.size = Some(size));
|
||||
let cs = compose_story(&d, sid, &ComposeOptions::default());
|
||||
all_lines(&cs).into_iter().cloned().collect::<Vec<Line>>()
|
||||
};
|
||||
let base = set(10.0);
|
||||
let (b0, b2) = (base[0].baseline, base[2].baseline);
|
||||
let body = base[0].glyphs.iter().find(|g| g.byte == 1).unwrap();
|
||||
let fitted = cap_of(&base[0].glyphs[0]);
|
||||
assert!((b2 - fitted - (b0 - cap_of(body))).abs() < 0.01, "at the body's size the cap fills its lines");
|
||||
for (size, scale) in [(15.0, 1.5), (7.5, 0.75)] {
|
||||
let l = set(size);
|
||||
let dc = &l[0].glyphs[0];
|
||||
for (a, b) in l.iter().zip(&base) {
|
||||
assert!((a.baseline - b.baseline).abs() < 1e-6, "{size} pt: the lines stay where they were");
|
||||
}
|
||||
assert!((l[0].baseline + dc.y - b2).abs() < 1e-6, "{size} pt: the cap sits on line 3's baseline");
|
||||
assert!((cap_of(dc) - fitted * scale).abs() < 0.01, "{size} pt: cap height {} vs {}", cap_of(dc), fitted * scale);
|
||||
let top = (l[0].baseline - l[0].ascent).min(b2 - cap_of(dc));
|
||||
assert!((l[0].drop_cap.unwrap().rect.y0 - top).abs() < 1e-6, "{size} pt: the box starts at the line's or the cap's top");
|
||||
}
|
||||
assert!(b2 - fitted * 1.5 < b0 - base[0].ascent, "a 15 pt cap rises above the first line");
|
||||
}
|
||||
|
||||
/// A rule in Text Color takes the colour of the paragraph's text: the first character's for the
|
||||
/// rule above, the last character's for the rule below. A swatch colour is used as is.
|
||||
#[test]
|
||||
|
||||
+48
-1
@@ -2,7 +2,7 @@
|
||||
//! (insert/delete/move pages, repagination into spreads, parents).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
|
||||
use designcraft_geom::{Affine, Unit, units};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -258,6 +258,53 @@ impl Document {
|
||||
id
|
||||
}
|
||||
|
||||
/// Delete a parent spread and unapply it wherever it is used. Local overrides survive as
|
||||
/// ordinary items; the deleted parent's own items leave their text threads normally.
|
||||
pub fn delete_parent(&mut self, id: SpreadId) -> Result<()> {
|
||||
let index = self.parent_index(id).ok_or_else(|| DocError::Invalid(format!("no parent {id}")))?;
|
||||
let parent = self.parents.get(index).ok_or_else(|| DocError::Invalid(format!("no parent {id}")))?;
|
||||
let roots: Vec<_> = parent.items.iter().map(|item| item.id).collect();
|
||||
let mut deleted = HashSet::new();
|
||||
for item in &parent.items {
|
||||
item.walk(&mut |item| {
|
||||
deleted.insert(item.id);
|
||||
});
|
||||
}
|
||||
for item in roots {
|
||||
self.remove_item(item)?;
|
||||
}
|
||||
self.parents.remove(index);
|
||||
|
||||
let mut detached = Vec::new();
|
||||
for spread in self.spreads.iter_mut().chain(self.parents.iter_mut()) {
|
||||
let spread = Arc::make_mut(spread);
|
||||
if let Some(parent) = &mut spread.parent
|
||||
&& parent.based_on == Some(id)
|
||||
{
|
||||
parent.based_on = None;
|
||||
}
|
||||
for page in &mut spread.pages {
|
||||
if page.parent == Some(id) {
|
||||
page.parent = None;
|
||||
}
|
||||
page.overridden.retain(|item| !deleted.contains(item));
|
||||
}
|
||||
for item in &spread.items {
|
||||
item.walk(&mut |item| {
|
||||
if item.overrides.is_some_and(|source| deleted.contains(&source)) {
|
||||
detached.push(item.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
for item in detached {
|
||||
if let Some(item) = self.item_mut(item) {
|
||||
item.overrides = None;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// List the pages of each facing parent spread in binding order: left then right, or right
|
||||
/// then left when bound right to left. Positions stay, so the parent items keep their pages.
|
||||
pub fn order_parent_pages(&mut self) {
|
||||
|
||||
+18
-5
@@ -301,17 +301,27 @@ impl Spread {
|
||||
pub fn bounds(&self) -> Rect {
|
||||
self.pages.iter().map(Page::bounds).reduce(|a, b| a.union(b)).unwrap_or(Rect::ZERO)
|
||||
}
|
||||
/// The bleed guide: one rectangle around the spread. `bleed` is the document's top, bottom,
|
||||
/// The bleed guide: one rectangle around the spread. None without pages or without any bleed.
|
||||
pub fn bleed_rect(&self, bleed: [f64; 4]) -> Option<Rect> {
|
||||
self.outset_rect(bleed)
|
||||
}
|
||||
/// The slug guide. Slug offsets are measured from the page edges, like the bleed, so a slug
|
||||
/// narrower than the bleed sits inside it, as in InDesign. None without pages or without any
|
||||
/// slug.
|
||||
pub fn slug_rect(&self, slug: [f64; 4]) -> Option<Rect> {
|
||||
self.outset_rect(slug)
|
||||
}
|
||||
/// The spread's bounds grown by a document outset (the bleed or the slug): top, bottom,
|
||||
/// inside, outside; the inside value applies only at an outer edge on the spine side (a lone
|
||||
/// left page's right edge). The outer edges are the leftmost and rightmost pages, whichever
|
||||
/// binding ordered the list. None without pages or without any bleed.
|
||||
pub fn bleed_rect(&self, bleed: [f64; 4]) -> Option<Rect> {
|
||||
/// binding ordered the list.
|
||||
pub fn outset_rect(&self, outset: [f64; 4]) -> Option<Rect> {
|
||||
let (l, r) = self.outer_page_indices()?;
|
||||
let (first, last) = (self.pages.get(l)?, self.pages.get(r)?);
|
||||
if !bleed.iter().any(|v| *v > 0.0) {
|
||||
if !outset.iter().any(|v| *v > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let [top, bottom, inside, outside] = bleed;
|
||||
let [top, bottom, inside, outside] = outset;
|
||||
let l = if first.side == PageSide::Left { outside } else { inside };
|
||||
let r = if matches!(last.side, PageSide::Right | PageSide::Single) { outside } else { inside };
|
||||
let b = self.bounds();
|
||||
@@ -464,6 +474,9 @@ mod tests {
|
||||
let s = Spread { id: SpreadId(1), pages, items: vec![], parent: None, allow_shuffle: true };
|
||||
assert_eq!(s.bleed_rect(bleed), Some(Rect::new(-4.0, -1.0, 1228.0, 794.0)));
|
||||
assert_eq!(s.bleed_rect([0.0; 4]), None);
|
||||
// The slug is measured the same way, from the page edges.
|
||||
assert_eq!(s.slug_rect(bleed), s.bleed_rect(bleed));
|
||||
assert_eq!(s.slug_rect([0.0; 4]), None);
|
||||
}
|
||||
let lone_left = Spread { id: SpreadId(1), pages: vec![page(PageSide::Left, 0.0)], items: vec![], parent: None, allow_shuffle: true };
|
||||
assert_eq!(lone_left.bleed_rect(bleed), Some(Rect::new(-4.0, -1.0, 615.0, 794.0)));
|
||||
|
||||
@@ -454,6 +454,11 @@ pub struct CellStyle {
|
||||
pub inset_overrides: [Option<f64>; 4],
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub vj: Option<crate::item::VerticalJustification>,
|
||||
/// Cell Options › Text › First Baseline, and its minimum.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub first_baseline: Option<crate::item::FirstBaseline>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub first_baseline_min: Option<f64>,
|
||||
/// All four edges.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub stroke: Option<crate::table::CellStroke>,
|
||||
@@ -529,6 +534,12 @@ impl CellStyle {
|
||||
if let Some(v) = self.vj {
|
||||
cell.vj = v;
|
||||
}
|
||||
if let Some(v) = self.first_baseline {
|
||||
cell.first_baseline = v;
|
||||
}
|
||||
if let Some(v) = self.first_baseline_min {
|
||||
cell.first_baseline_min = v;
|
||||
}
|
||||
if let Some(v) = &self.stroke {
|
||||
cell.strokes = [v.clone(), v.clone(), v.clone(), v.clone()];
|
||||
cell.border_overrides = [true; 4];
|
||||
@@ -698,6 +709,12 @@ pub fn resolve_cell_style(cells: &[CellStyle], name: &str) -> CellStyle {
|
||||
if style.vj.is_some() {
|
||||
out.vj = style.vj;
|
||||
}
|
||||
if style.first_baseline.is_some() {
|
||||
out.first_baseline = style.first_baseline;
|
||||
}
|
||||
if style.first_baseline_min.is_some() {
|
||||
out.first_baseline_min = style.first_baseline_min;
|
||||
}
|
||||
if let Some(stroke) = &style.stroke {
|
||||
out.strokes = std::array::from_fn(|_| CellStrokeAttrs::from_stroke(stroke));
|
||||
}
|
||||
|
||||
+31
-2
@@ -13,7 +13,7 @@ use std::sync::Arc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::ids::StoryId;
|
||||
use crate::item::{StrokeType, VerticalJustification};
|
||||
use crate::item::{FirstBaseline, StrokeType, VerticalJustification};
|
||||
use crate::story::{CharFormat, ParaFormat, Story, TABLE_ANCHOR};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -159,6 +159,12 @@ fn full() -> f32 {
|
||||
fn no_fill() -> String {
|
||||
designcraft_color::swatch::NONE.into()
|
||||
}
|
||||
fn is_ascent(f: &FirstBaseline) -> bool {
|
||||
*f == FirstBaseline::Ascent
|
||||
}
|
||||
fn is_zero_min(v: &f64) -> bool {
|
||||
*v == 0.0
|
||||
}
|
||||
fn default_insets() -> [f64; 4] {
|
||||
[4.0; 4]
|
||||
}
|
||||
@@ -181,6 +187,12 @@ pub struct Cell {
|
||||
pub insets: [f64; 4],
|
||||
#[serde(default)]
|
||||
pub vj: VerticalJustification,
|
||||
/// Cell Options › Text › First Baseline: how far the first line's baseline sits below the
|
||||
/// top inset (IDML `FirstBaselineOffset`), and its minimum (`MinimumFirstBaselineOffset`).
|
||||
#[serde(default, skip_serializing_if = "is_ascent")]
|
||||
pub first_baseline: FirstBaseline,
|
||||
#[serde(default, skip_serializing_if = "is_zero_min")]
|
||||
pub first_baseline_min: f64,
|
||||
/// Text rotation in degrees (only 0 is composed for now).
|
||||
#[serde(default)]
|
||||
pub rotation: f64,
|
||||
@@ -216,6 +228,8 @@ impl Default for Cell {
|
||||
fill_tint: 1.0,
|
||||
insets: default_insets(),
|
||||
vj: VerticalJustification::Top,
|
||||
first_baseline: FirstBaseline::Ascent,
|
||||
first_baseline_min: 0.0,
|
||||
rotation: 0.0,
|
||||
strokes: Default::default(),
|
||||
border_overrides: [false; 4],
|
||||
@@ -897,7 +911,8 @@ impl Table {
|
||||
}
|
||||
|
||||
/// Move row `from` to position `to` (indices before the move). Refused when merged cells span
|
||||
/// rows (the move would cut them).
|
||||
/// rows (the move would cut them) or when the move would take a row across a header/footer
|
||||
/// boundary (`from` and `to` must be the same kind of row).
|
||||
pub fn move_row(&mut self, from: usize, to: usize) -> Result<(), String> {
|
||||
let (nr, nc) = (self.nrows(), self.ncols());
|
||||
if from >= nr || to >= nr {
|
||||
@@ -906,6 +921,9 @@ impl Table {
|
||||
if self.regions().iter().any(|r| r.2 > 1) {
|
||||
return Err("unmerge cells that span rows first".into());
|
||||
}
|
||||
if self.rows[from].kind != self.rows[to].kind {
|
||||
return Err("header and footer rows must stay first and last".into());
|
||||
}
|
||||
let row = self.rows.remove(from);
|
||||
self.rows.insert(to, row);
|
||||
let cells: Vec<Cell> = self.cells.drain(from * nc..(from + 1) * nc).collect();
|
||||
@@ -1255,6 +1273,17 @@ mod tests {
|
||||
Story::with_text(StoryId(1), t, ParaFormat::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_row_keeps_header_and_footer_rows_in_place() {
|
||||
let mut t = Table::new(1, 3, 2, 1, 1, 120.0);
|
||||
for (from, to) in [(0, 2), (0, 4), (2, 0), (2, 4), (4, 2), (4, 0)] {
|
||||
assert!(t.move_row(from, to).is_err(), "move_row({from}, {to}) should be refused");
|
||||
t.check().unwrap();
|
||||
}
|
||||
t.move_row(1, 3).unwrap();
|
||||
t.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_owners_include_covered_cells_without_expanding_range() {
|
||||
for column in [true, false] {
|
||||
|
||||
@@ -10,6 +10,7 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-print = { workspace = true }
|
||||
craft-docpath = { workspace = true }
|
||||
designcraft-images = { workspace = true }
|
||||
designcraft-geom = { workspace = true }
|
||||
designcraft-color = { workspace = true }
|
||||
|
||||
@@ -36,6 +36,19 @@ fn save(_: &Book) -> Result<()> {
|
||||
Err(EngineError::Other("books need a file system".into()))
|
||||
}
|
||||
|
||||
/// A chapter's file, resolved against the book's file. A chapter on another computer (other than
|
||||
/// the book's own network share) or a device is not opened ([`crate::io::document_path`]).
|
||||
fn chapter(b: &Book, path: &str) -> Result<String> {
|
||||
crate::io::document_path(path, Some(std::path::Path::new(&b.path)))
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
.map_err(|why| EngineError::Other(format!("{path}: {why}")))
|
||||
}
|
||||
|
||||
/// Every chapter's file ([`chapter`]), in order.
|
||||
fn chapters(b: &Book) -> Result<Vec<String>> {
|
||||
b.documents.iter().map(|path| chapter(b, path)).collect()
|
||||
}
|
||||
|
||||
fn load(path: &str) -> Result<designcraft_doc::Document> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let bytes = crate::io::read_file(path).map_err(EngineError::Other)?;
|
||||
@@ -45,7 +58,8 @@ fn load(path: &str) -> Result<designcraft_doc::Document> {
|
||||
vec![]
|
||||
};
|
||||
if path.to_lowercase().ends_with(".idml") {
|
||||
return super::interchange::import_idml_confined(&bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")));
|
||||
return super::interchange::import_idml_confined(&bytes, Some(std::path::Path::new(path)))
|
||||
.map_err(|e| EngineError::Other(format!("{path}: {e}")));
|
||||
}
|
||||
super::file::from_bytes(&bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))
|
||||
}
|
||||
@@ -144,7 +158,7 @@ fn paginate(s: &Session) -> Result<Value> {
|
||||
let mut next = 1u32;
|
||||
let mut chapter = 0u32;
|
||||
let mut chapters = Vec::with_capacity(b.documents.len());
|
||||
for path in &b.documents {
|
||||
for path in &self::chapters(b)? {
|
||||
writable_chapter(s, path, "book.paginate")?;
|
||||
let mut d = load(path)?;
|
||||
start_at(&mut d, next);
|
||||
@@ -167,13 +181,14 @@ fn paginate(s: &Session) -> Result<Value> {
|
||||
fn sync_styles(s: &Session) -> Result<Value> {
|
||||
let b = s.book.as_ref().ok_or_else(no_book)?;
|
||||
let mut batch = ChapterBatch::new(b.documents.len(), "book.syncStyles")?;
|
||||
let src_path = b.documents.get(b.style_source).ok_or_else(|| bad("book.syncStyles", "the book is empty"))?;
|
||||
let paths = chapters(b)?;
|
||||
let src_path = paths.get(b.style_source).ok_or_else(|| bad("book.syncStyles", "the book is empty"))?;
|
||||
let src = load(src_path)?;
|
||||
let styles = src.styles.clone();
|
||||
let swatches = src.swatches.clone();
|
||||
drop(src);
|
||||
let mut n = 0;
|
||||
for (i, path) in b.documents.iter().enumerate() {
|
||||
for (i, path) in paths.iter().enumerate() {
|
||||
if i == b.style_source {
|
||||
continue;
|
||||
}
|
||||
@@ -304,7 +319,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
let mut first = 1usize;
|
||||
let mut docs = Vec::new();
|
||||
for p in &b.documents {
|
||||
let pages = load(p).map(|d| d.page_count()).unwrap_or(0);
|
||||
let pages = chapter(b, p).and_then(|p| load(&p)).map(|d| d.page_count()).unwrap_or(0);
|
||||
docs.push(json!({"path": p, "pages": pages, "firstPage": first}));
|
||||
first = first.saturating_add(pages);
|
||||
}
|
||||
@@ -319,7 +334,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(noundo "book.exportPdf", "Export Book to PDF…", [], None, "{path?} — every document in order as one PDF → {path, bytes, pages} (no path: {base64, …})", has_book, |s, p| {
|
||||
let b = s.book.clone().ok_or_else(no_book)?;
|
||||
let mut parts = Vec::new();
|
||||
for path in &b.documents {
|
||||
for path in &chapters(&b)? {
|
||||
let mut d = load(path)?;
|
||||
// The chapter's own fonts, for this export (skipped font files are logged).
|
||||
let _fonts = super::file::load_document_fonts(&mut d, path).0;
|
||||
|
||||
@@ -354,7 +354,7 @@ fn place_image(
|
||||
report.warnings.push(format!("Record {record}, field {field}: missing image {cell}."));
|
||||
return;
|
||||
};
|
||||
let Some((pw, ph)) = designcraft_render::image_size(&bytes) else {
|
||||
let Some(((pw, ph), (nw, nh))) = designcraft_render::image_size(&bytes).zip(designcraft_render::natural_size(&bytes, 0)) else {
|
||||
if let Some(it) = doc.item_mut(item) {
|
||||
it.content = Content::Unassigned;
|
||||
}
|
||||
@@ -367,22 +367,9 @@ fn place_image(
|
||||
report.missing.push(MissingImage { record, field: field.to_string(), path: cell.to_string() });
|
||||
return;
|
||||
};
|
||||
let (nw, nh) = (f64::from(pw), f64::from(ph));
|
||||
let mode = fitting_of(&options.fitting);
|
||||
let fresh = AssetId(doc.alloc());
|
||||
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| field.to_string());
|
||||
let link = options.link_images.then(|| path.to_string_lossy().into_owned());
|
||||
let aid = doc.add_asset(Asset {
|
||||
id: fresh,
|
||||
name,
|
||||
mime: designcraft_render::image_mime(&bytes).to_string(),
|
||||
link,
|
||||
data: Arc::new(bytes),
|
||||
pixels: Some((pw, ph)),
|
||||
..Default::default()
|
||||
});
|
||||
let mut g = Graphic {
|
||||
asset: aid,
|
||||
asset: AssetId(0),
|
||||
size: (nw, nh),
|
||||
xf: Affine::translate((inner.x0, inner.y0)),
|
||||
auto_fit: mode,
|
||||
@@ -401,6 +388,27 @@ fn place_image(
|
||||
let m = bb.center();
|
||||
g.xf = Affine::translate((c.x - m.x, c.y - m.y)) * g.xf;
|
||||
}
|
||||
// Reject geometry that cannot be represented before an asset is allocated for it.
|
||||
if super::super::file::validate_placement((nw, nh), inner, g.xf, "data.merge").is_err() {
|
||||
if let Some(it) = doc.item_mut(item) {
|
||||
it.content = Content::Unassigned;
|
||||
}
|
||||
report.missing.push(MissingImage { record, field: field.to_string(), path: cell.to_string() });
|
||||
report.warnings.push(format!("Record {record}, field {field}: image {cell} exceeds finite geometry limits."));
|
||||
return;
|
||||
}
|
||||
let fresh = AssetId(doc.alloc());
|
||||
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| field.to_string());
|
||||
let link = options.link_images.then(|| path.to_string_lossy().into_owned());
|
||||
g.asset = doc.add_asset(Asset {
|
||||
id: fresh,
|
||||
name,
|
||||
mime: designcraft_render::image_mime(&bytes).to_string(),
|
||||
link,
|
||||
data: Arc::new(bytes),
|
||||
pixels: Some((pw, ph)),
|
||||
..Default::default()
|
||||
});
|
||||
if let Some(it) = doc.item_mut(item) {
|
||||
it.content = Content::Graphic(g);
|
||||
}
|
||||
@@ -489,3 +497,21 @@ mod link_tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod image_path_tests {
|
||||
use std::path::Path;
|
||||
|
||||
use super::image_candidates;
|
||||
|
||||
#[test]
|
||||
fn image_cells_resolve_beside_the_data_and_never_to_other_computers() {
|
||||
let (data, doc) = (Path::new("/work/data"), Path::new("/work/doc"));
|
||||
for cell in [r"\\192.0.2.1\share\x.png", "file://192.0.2.1/s/x.png", "//192.0.2.1/s/x.png", "https://example.com/x.png"] {
|
||||
assert!(image_candidates(cell, Some(data), Some(doc)).is_empty(), "{cell}");
|
||||
}
|
||||
let found = image_candidates("img/x.png", Some(data), Some(doc));
|
||||
assert_eq!(found, [data.join("img").join("x.png"), doc.join("img").join("x.png")]);
|
||||
assert!(image_candidates("img/x.png", None, None).is_empty(), "not beside the working directory");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,11 +98,20 @@ fn source_select(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Where a data source's file is, resolved against the document's file (`document`); `None` when
|
||||
/// it has none or the path may not be followed ([`crate::io::document_path`]).
|
||||
fn source_file(src: &DataSource, document: Option<&Path>) -> Option<PathBuf> {
|
||||
crate::io::document_path(src.path.as_deref()?, document).ok()
|
||||
}
|
||||
|
||||
fn source_update(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
let (path, name, delimiter, sheet) = {
|
||||
let src = s.doc()?.doc.data_merge.sources.first().ok_or_else(|| bad("data.source.update", "select a data source"))?;
|
||||
let st = s.doc()?;
|
||||
let src = st.doc.data_merge.sources.first().ok_or_else(|| bad("data.source.update", "select a data source"))?;
|
||||
let path = src.path.clone().ok_or_else(|| bad("data.source.update", "this source has no file to update"))?;
|
||||
(path, src.name.clone(), src.delimiter, src.sheet.clone())
|
||||
let at =
|
||||
crate::io::document_path(&path, st.path.as_deref().map(Path::new)).map_err(|why| bad("data.source.update", format!("{path}: {why}")))?;
|
||||
(at.to_string_lossy().into_owned(), src.name.clone(), src.delimiter, src.sheet.clone())
|
||||
};
|
||||
let bytes = read_path(&path).map_err(|e| bad("data.source.update", e))?;
|
||||
let table = parse_file(&bytes, &name, Some(delimiter), sheet.as_deref()).map_err(|e| bad("data.source.update", e))?;
|
||||
@@ -199,7 +208,7 @@ fn preview_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let options = doc.data_merge.options.clone();
|
||||
let maps = identity_map(&doc);
|
||||
let row = source.rows.get((record as usize).saturating_sub(1)).cloned().unwrap_or_default();
|
||||
let data_dir = source.path.as_deref().and_then(parent_dir);
|
||||
let data_dir = source_file(&source, st.path.as_deref().map(Path::new)).as_deref().and_then(Path::parent).map(Path::to_path_buf);
|
||||
let doc_dir = st.path.as_deref().and_then(parent_dir);
|
||||
fill_row(&mut doc, &source.fields, &row, record as u32, &options, &maps, data_dir.as_deref(), doc_dir.as_deref());
|
||||
st.doc = Arc::new(doc);
|
||||
@@ -218,7 +227,7 @@ fn merge_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (table, dir) = load_inline(p).map_err(|e| bad("data.merge", e))?;
|
||||
(table, Vec::new(), dir, None)
|
||||
} else {
|
||||
let (table, warnings, dir, status) = linked_table(&st.doc).map_err(|e| bad("data.merge", e))?;
|
||||
let (table, warnings, dir, status) = linked_table(&st.doc, st.path.as_deref().map(Path::new)).map_err(|e| bad("data.merge", e))?;
|
||||
(table, warnings, dir, Some(status))
|
||||
};
|
||||
let mut options = st.doc.data_merge.options.clone();
|
||||
@@ -624,28 +633,24 @@ pub(crate) fn refresh_relative_paths(doc: &mut Document, doc_path: &Path) {
|
||||
}
|
||||
|
||||
/// On open, compare the absolute path with its fingerprint. A missing absolute path is tried
|
||||
/// again as `relative_path` beside the document.
|
||||
/// again as `relative_path` beside the document. Paths that may not be followed ([`crate::io::document_path`]) are missing.
|
||||
pub(crate) fn resolve_sources_on_open(doc: &mut Document, doc_path: Option<&Path>) {
|
||||
for src in &mut doc.data_merge.sources {
|
||||
let abs_exists = src.path.as_deref().is_some_and(crate::io::is_file);
|
||||
if abs_exists {
|
||||
if let Some(path) = src.path.clone() {
|
||||
src.status = status_of(Path::new(&path), src.fingerprint);
|
||||
}
|
||||
if let Some(path) = source_file(src, doc_path).filter(|p| crate::io::is_file(p)) {
|
||||
src.status = status_of(&path, src.fingerprint);
|
||||
continue;
|
||||
}
|
||||
if let (Some(doc_path), Some(rel)) = (doc_path, src.relative_path.clone())
|
||||
&& let Some(dir) = doc_path.parent()
|
||||
&& let Ok(candidate) = crate::io::document_path(&rel, Some(doc_path))
|
||||
&& crate::io::is_file(&candidate)
|
||||
{
|
||||
let candidate = dir.join(&rel);
|
||||
if crate::io::is_file(&candidate) {
|
||||
let saved = candidate.to_string_lossy().into_owned();
|
||||
src.fingerprint = fingerprint_of(Path::new(&saved));
|
||||
src.status = SourceStatus::Ok;
|
||||
src.relative_path = Some(relative_between(dir, Path::new(&saved)));
|
||||
src.path = Some(saved);
|
||||
continue;
|
||||
}
|
||||
let saved = candidate.to_string_lossy().into_owned();
|
||||
src.fingerprint = fingerprint_of(Path::new(&saved));
|
||||
src.status = SourceStatus::Ok;
|
||||
src.relative_path = Some(relative_between(dir, Path::new(&saved)));
|
||||
src.path = Some(saved);
|
||||
continue;
|
||||
}
|
||||
if src.path.is_some() {
|
||||
src.status = SourceStatus::Missing;
|
||||
@@ -680,11 +685,17 @@ fn read_source(p: &Value) -> std::result::Result<(DataSource, Value), String> {
|
||||
Ok((source, report))
|
||||
}
|
||||
|
||||
fn linked_table(doc: &Document) -> std::result::Result<(Table, Vec<String>, Option<PathBuf>, SourceStatus), String> {
|
||||
fn linked_table(doc: &Document, document: Option<&Path>) -> std::result::Result<(Table, Vec<String>, Option<PathBuf>, SourceStatus), String> {
|
||||
let src = doc.data_merge.sources.first().ok_or_else(|| "select a data source or pass csv or rows".to_string())?;
|
||||
let mut warnings = Vec::new();
|
||||
let status = if let Some(path) = &src.path {
|
||||
let status = status_of(Path::new(path), src.fingerprint);
|
||||
let status = match crate::io::document_path(path, document) {
|
||||
Ok(path) => status_of(&path, src.fingerprint),
|
||||
Err(why) => {
|
||||
warnings.push(format!("The data file is not read ({why}). This merge used the cached rows."));
|
||||
return Ok((cached_table(src), warnings, None, SourceStatus::Missing));
|
||||
}
|
||||
};
|
||||
match status {
|
||||
SourceStatus::Modified => warnings.push("The data file changed since it was read. This merge used the cached rows.".into()),
|
||||
SourceStatus::Missing => warnings.push("The data file is missing. This merge used the cached rows.".into()),
|
||||
@@ -694,9 +705,12 @@ fn linked_table(doc: &Document) -> std::result::Result<(Table, Vec<String>, Opti
|
||||
} else {
|
||||
src.status
|
||||
};
|
||||
let data_dir = src.path.as_deref().and_then(parent_dir);
|
||||
let table = Table { fields: src.fields.clone(), rows: src.rows.clone(), warnings: src.warnings.clone(), delimiter: src.delimiter };
|
||||
Ok((table, warnings, data_dir, status))
|
||||
let data_dir = source_file(src, document).as_deref().and_then(Path::parent).map(Path::to_path_buf);
|
||||
Ok((cached_table(src), warnings, data_dir, status))
|
||||
}
|
||||
|
||||
fn cached_table(src: &DataSource) -> Table {
|
||||
Table { fields: src.fields.clone(), rows: src.rows.clone(), warnings: src.warnings.clone(), delimiter: src.delimiter }
|
||||
}
|
||||
|
||||
fn set_source_status(doc: &mut Document, status: SourceStatus) -> bool {
|
||||
|
||||
@@ -277,32 +277,23 @@ fn next_slot(col: i32, row: i32, rows_first: bool, fits_col: &impl Fn(i32) -> bo
|
||||
(0, 0, true)
|
||||
}
|
||||
|
||||
/// Files to try for an image cell. `http` and `https` are not candidates. Relative paths are
|
||||
/// not resolved against the process working directory.
|
||||
/// Files to try for an image cell: an absolute path as it is, a relative one beside the data
|
||||
/// file, then beside the document. Relative paths are not resolved against the process working
|
||||
/// directory, and paths that may not be followed ([`crate::io::document_path`]: URLs, other
|
||||
/// computers, devices) are not candidates.
|
||||
pub fn image_candidates(cell: &str, data_dir: Option<&Path>, doc_dir: Option<&Path>) -> Vec<PathBuf> {
|
||||
let cell = cell.trim();
|
||||
if cell.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let lower = cell.to_ascii_lowercase();
|
||||
if lower.starts_with("http://") || lower.starts_with("https://") {
|
||||
return Vec::new();
|
||||
}
|
||||
let path = Path::new(cell);
|
||||
let mut out = Vec::new();
|
||||
let mut push = |p: PathBuf| {
|
||||
if !out.iter().any(|e| e == &p) {
|
||||
let found = std::iter::once(crate::io::document_path(cell, None))
|
||||
.chain(data_dir.map(|dir| crate::io::folder_path(cell, dir)))
|
||||
.chain(doc_dir.map(|dir| crate::io::folder_path(cell, dir)));
|
||||
let mut out: Vec<PathBuf> = Vec::new();
|
||||
for p in found.flatten() {
|
||||
if !out.contains(&p) {
|
||||
out.push(p);
|
||||
}
|
||||
};
|
||||
if path.is_absolute() {
|
||||
push(path.to_path_buf());
|
||||
}
|
||||
if let Some(dir) = data_dir {
|
||||
push(dir.join(cell));
|
||||
}
|
||||
if let Some(dir) = doc_dir {
|
||||
push(dir.join(cell));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use designcraft_pdf::{Marks, PdfOptions, Standard};
|
||||
use serde_json::{Value, json};
|
||||
use std::{borrow::Cow, sync::Arc};
|
||||
|
||||
use super::{CommandSpec, bad, cmd, has_doc, str_param};
|
||||
use crate::{EngineError, Result, Session};
|
||||
@@ -23,7 +24,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"{path?, type?: saddleStitch|twoUpConsecutive, spaceBetween? (pt)} — printer spreads as PDF (pages imposed in booklet order) → {path, bytes, sheets} (no path: {base64, …})",
|
||||
has_doc, print_booklet),
|
||||
cmd!(noundo "file.exportHtml", "Export HTML…", ["File"], None,
|
||||
"{path?, title?, language?} — one self-contained page (styles inline, images embedded), stories and graphics in reading order → {path, bytes} (no path: {text, bytes})",
|
||||
"{path?, title?, language?} — one self-contained page (styles inline, images embedded), stories and graphics in reading order → {path, bytes, warnings} (no path: {text, bytes, warnings})",
|
||||
has_doc, export_html),
|
||||
cmd!(noundo "file.exportPptx", "Export PowerPoint…", ["File"], None,
|
||||
"{path?, pages?: \"1-3,5\" | [1,3] (1-based positions; default all), lineBreaks?: keep|reflow (keep: DesignCraft's lines, spacing and leading; reflow: one box per frame, PowerPoint sets the lines), rasterize?: bool (default true: objects PowerPoint can't show — placed PDF/EPS, type on a path, some effects — go in as 300 ppi images), embedFonts?: bool (default true: the TrueType fonts whose licences allow it), title?, author?} — every page as a slide of editable shapes, pictures, text boxes and tables → {path, bytes, slides, warnings} (no path: {base64, …})",
|
||||
@@ -55,6 +56,39 @@ fn print_booklet(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
|
||||
/// HTML and reflowable EPUB embed the EPS preview; keep the session's source bytes intact.
|
||||
fn eps_previews(d: &designcraft_doc::Document) -> (Cow<'_, designcraft_doc::Document>, Vec<String>) {
|
||||
// Match the exporter's reading order (including Article exclusions), not every asset
|
||||
// in the document: unused or hidden graphics must not prevent an export.
|
||||
let mut used = std::collections::BTreeSet::new();
|
||||
for block in designcraft_epub::reading_order(d) {
|
||||
let designcraft_epub::Block::Image(id) = block else { continue };
|
||||
let Some(it) = d.item(id) else { continue };
|
||||
let designcraft_doc::Content::Graphic(g) = &it.content else { continue };
|
||||
used.insert(g.asset);
|
||||
if d.assets.get(&g.asset).is_some_and(|a| designcraft_doc::media_kind(&a.mime).is_some())
|
||||
&& let Some(poster) = it.media.as_ref().and_then(|m| m.poster)
|
||||
{
|
||||
used.insert(poster);
|
||||
}
|
||||
}
|
||||
let mut out = Cow::Borrowed(d);
|
||||
let mut warnings = Vec::new();
|
||||
for id in used {
|
||||
let Some(a) = d.assets.get(&id).filter(|a| designcraft_images::is_eps(&a.data)) else { continue };
|
||||
let png = designcraft_images::eps_png(&a.data);
|
||||
if png.is_none() {
|
||||
warnings.push(format!("{}: unusable EPS preview; graphic omitted", a.name));
|
||||
}
|
||||
if let Some(a) = out.to_mut().assets.get_mut(&id) {
|
||||
let a = Arc::make_mut(a);
|
||||
a.mime = "image/png".into();
|
||||
a.data = Arc::new(png.unwrap_or_default());
|
||||
}
|
||||
}
|
||||
(out, warnings)
|
||||
}
|
||||
|
||||
fn export_html(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let st = s.doc()?;
|
||||
let opts = designcraft_epub::HtmlOptions {
|
||||
@@ -62,14 +96,15 @@ fn export_html(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
language: str_param(p, "language").map(str::to_string),
|
||||
generic_families: generic_families(&st.doc),
|
||||
};
|
||||
let text = designcraft_epub::export_html(&st.doc, &opts);
|
||||
let (doc, warnings) = eps_previews(&st.doc);
|
||||
let text = designcraft_epub::export_html(&doc, &opts);
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
crate::io::write_file(path, text.as_bytes()).map_err(EngineError::Other)?;
|
||||
Ok(json!({"path": path, "bytes": text.len()}))
|
||||
Ok(json!({"path": path, "bytes": text.len(), "warnings": warnings}))
|
||||
}
|
||||
None => Ok(json!({"bytes": text.len(), "text": text})),
|
||||
None => Ok(json!({"bytes": text.len(), "text": text, "warnings": warnings})),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,9 +300,11 @@ fn export_epub(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None => Ok(serde_json::json!({"base64": super::base64_encode(&bytes), "bytes": bytes.len(), "pages": pages.len()})),
|
||||
};
|
||||
}
|
||||
let bytes = designcraft_epub::export_epub(epub_doc, &opts).map_err(|e| crate::EngineError::Other(e.to_string()))?;
|
||||
let (epub_doc, eps_warnings) = eps_previews(epub_doc);
|
||||
warnings.extend(eps_warnings);
|
||||
let bytes = designcraft_epub::export_epub(&epub_doc, &opts).map_err(|e| crate::EngineError::Other(e.to_string()))?;
|
||||
warnings.extend(
|
||||
designcraft_epub::images_without_alt(epub_doc)
|
||||
designcraft_epub::images_without_alt(&epub_doc)
|
||||
.into_iter()
|
||||
.map(|name| format!("{name}: no alt text (Object Export Options); exported as decorative (alt=\"\")")),
|
||||
);
|
||||
|
||||
@@ -478,7 +478,7 @@ fn open_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad("file.open", format!("{path}: not a DesignCraft or IDML file")));
|
||||
}
|
||||
let (mut d, mut warnings) = load_bytes(&bytes)?;
|
||||
super::interchange::resolve_packaged_links(&mut d, std::path::Path::new(path).parent());
|
||||
super::interchange::resolve_packaged_links(&mut d, Some(std::path::Path::new(path)));
|
||||
super::datamerge::resolve_sources_on_open(&mut d, Some(std::path::Path::new(path)));
|
||||
let (fonts, faces, font_warnings) = load_document_fonts(&mut d, path);
|
||||
warnings.extend(font_warnings);
|
||||
@@ -608,13 +608,8 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if lname.ends_with(".idml") || lname.ends_with(".designcraft") {
|
||||
return place_layout_page(s, p, &name, &bytes);
|
||||
}
|
||||
// EPS: shown and printed through its preview (or a placeholder) at its bounding box size.
|
||||
let (bytes, eps_size) = if designcraft_images::is_eps(&bytes) {
|
||||
let (proxy, size) = designcraft_images::eps_proxy(&bytes).ok_or_else(|| bad("file.place", "the EPS has no bounding box"))?;
|
||||
(proxy, Some(size))
|
||||
} else {
|
||||
(bytes, None)
|
||||
};
|
||||
// Keep the EPS source for link comparison; rendering uses its preview at the bounding box size.
|
||||
let eps_size = eps_size(&bytes, "file.place")?;
|
||||
// Video and sound: a media frame (Window › Interactive › Media).
|
||||
if let Some(mime) = designcraft_doc::media_mime(&name) {
|
||||
return super::media::place_media(s, p, name, mime, bytes, link);
|
||||
@@ -705,10 +700,12 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Fill the frame proportionally once. Auto-Fit stays off, so resizing the frame crops.
|
||||
let k = (r.width() / bw).max(r.height() / bh);
|
||||
let (gw, gh) = (bw * k, bh * k);
|
||||
let xf = Affine::translate((r.x0 + (r.width() - gw) / 2.0, r.y0 + (r.height() - gh) / 2.0)) * Affine::scale(k);
|
||||
validate_placement((bw, bh), r, xf, "file.place")?;
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: aid,
|
||||
size: (bw, bh),
|
||||
xf: Affine::translate((r.x0 + (r.width() - gw) / 2.0, r.y0 + (r.height() - gh) / 2.0)) * Affine::scale(k),
|
||||
xf,
|
||||
clip: None,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
@@ -724,6 +721,8 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = ItemId(d.alloc());
|
||||
let k = w / nw;
|
||||
let r = super::finite_rect("file.place", Rect::new(x, y, x + bw * k, y + bh * k))?;
|
||||
let xf = Affine::translate((x, y)) * Affine::scale(k);
|
||||
validate_placement((bw, bh), r, xf, "file.place")?;
|
||||
let mut it = Item::new(id, lid, Shape::Rectangle, shapes::rectangle(r));
|
||||
it.object_style = d.styles.default_graphic_frame.clone();
|
||||
if let Some(e) = d.styles.object_effects(&it.object_style) {
|
||||
@@ -732,7 +731,7 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: aid,
|
||||
size: (bw, bh),
|
||||
xf: Affine::translate((x, y)) * Affine::scale(k),
|
||||
xf,
|
||||
clip: None,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
@@ -811,10 +810,35 @@ pub(crate) fn read_source(p: &Value) -> Result<(Vec<u8>, String, Option<String>)
|
||||
Ok((b, name, Some(path.to_string())))
|
||||
}
|
||||
|
||||
fn eps_size(bytes: &[u8], command: &str) -> Result<Option<(f64, f64)>> {
|
||||
if !designcraft_images::is_eps(bytes) {
|
||||
return Ok(None);
|
||||
}
|
||||
designcraft_images::eps_size(bytes)
|
||||
.map(Some)
|
||||
.ok_or_else(|| bad(command, "the EPS has a missing or invalid bounding box (dimensions must be 0.001–20,000 points)"))
|
||||
}
|
||||
|
||||
/// Fitting can overflow even with finite inputs. Never persist NaN/Inf as JSON nulls.
|
||||
pub(crate) fn validate_placement(size: (f64, f64), frame: Rect, xf: Affine, command: &str) -> Result<()> {
|
||||
let valid_rect = |r: Rect| [r.x0, r.y0, r.x1, r.y1, r.width(), r.height()].iter().all(|n| n.is_finite()) && r.width() >= 0.0 && r.height() >= 0.0;
|
||||
if !size.0.is_finite()
|
||||
|| !size.1.is_finite()
|
||||
|| size.0 <= 0.0
|
||||
|| size.1 <= 0.0
|
||||
|| !valid_rect(frame)
|
||||
|| !xf.as_coeffs().iter().all(|n| n.is_finite())
|
||||
|| !valid_rect(xf.transform_rect_bbox(Rect::new(0.0, 0.0, size.0, size.1)))
|
||||
{
|
||||
return Err(bad(command, "image placement exceeds finite geometry limits"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn place_load(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (bytes, name, link) = read_source(p)?;
|
||||
let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad("place.load", "unsupported or corrupt image"))?;
|
||||
let natural = designcraft_render::natural_size(&bytes, 0).unwrap_or((pw as f64, ph as f64));
|
||||
let natural = eps_size(&bytes, "place.load")?.or_else(|| designcraft_render::natural_size(&bytes, 0)).unwrap_or((pw as f64, ph as f64));
|
||||
let mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
let asset = Asset { name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)), ..Default::default() };
|
||||
load_cursor(s, asset, natural)
|
||||
@@ -845,55 +869,53 @@ fn place_drop(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let frame = super::id_param(p, "frame");
|
||||
let rect = super::rect_param(p, "rect");
|
||||
let (x, y) = (super::f64_or(p, "x", 0.0), super::f64_or(p, "y", 0.0));
|
||||
let d = &s.doc()?.doc;
|
||||
let (frame_rect, xf, auto_fit) = if let Some(fid) = frame {
|
||||
let r = d.item(fid).ok_or(designcraft_doc::DocError::NoItem(fid))?.inner_bounds();
|
||||
let k = (r.width() / nw).max(r.height() / nh);
|
||||
let xf = Affine::translate((r.x0 + (r.width() - nw * k) / 2.0, r.y0 + (r.height() - nh * k) / 2.0)) * Affine::scale(k);
|
||||
// Fill the frame proportionally once. Auto-Fit stays off, so resizing the frame crops.
|
||||
(r, xf, Default::default())
|
||||
} else {
|
||||
if d.spread(sr).is_none() {
|
||||
return Err(bad("place.drop", "no such spread"));
|
||||
}
|
||||
// Drag: fit proportionally; click: actual size at the point.
|
||||
let (r, k) = match rect {
|
||||
Some(r) if r.width() > 2.0 && r.height() > 2.0 => {
|
||||
let k = (r.width() / nw).min(r.height() / nh);
|
||||
(Rect::new(r.x0, r.y0, r.x0 + nw * k, r.y0 + nh * k), k)
|
||||
}
|
||||
_ => (Rect::new(x, y, x + nw, y + nh), 1.0),
|
||||
};
|
||||
(r, Affine::translate((r.x0, r.y0)) * Affine::scale(k), Default::default())
|
||||
};
|
||||
validate_placement((nw, nh), frame_rect, xf, "place.drop")?;
|
||||
let r = s.edit(|d, sel| {
|
||||
let id = match frame {
|
||||
Some(fid) => {
|
||||
let it = d.item_mut(fid).ok_or(designcraft_doc::DocError::NoItem(fid))?;
|
||||
let r = it.inner_bounds();
|
||||
let k = (r.width() / nw).max(r.height() / nh);
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: aid,
|
||||
size: (nw, nh),
|
||||
xf: Affine::translate((r.x0 + (r.width() - nw * k) / 2.0, r.y0 + (r.height() - nh * k) / 2.0)) * Affine::scale(k),
|
||||
clip: None,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [0.0; 4],
|
||||
wrap: Default::default(),
|
||||
fill: Default::default(),
|
||||
});
|
||||
fid
|
||||
}
|
||||
None => {
|
||||
// Drag: fit proportionally into the dragged rect; click: actual size at the point.
|
||||
let (frame_r, k) = match rect {
|
||||
Some(r) if r.width() > 2.0 && r.height() > 2.0 => {
|
||||
let k = (r.width() / nw).min(r.height() / nh);
|
||||
(Rect::new(r.x0, r.y0, r.x0 + nw * k, r.y0 + nh * k), k)
|
||||
}
|
||||
_ => (Rect::new(x, y, x + nw, y + nh), 1.0),
|
||||
};
|
||||
let frame_r = super::finite_rect("place.drop", frame_r)?;
|
||||
let id = ItemId(d.alloc());
|
||||
let mut it = Item::new(id, lid, Shape::Rectangle, shapes::rectangle(frame_r));
|
||||
it.object_style = d.styles.default_graphic_frame.clone();
|
||||
if let Some(e) = d.styles.object_effects(&it.object_style) {
|
||||
it.effects = e.clone();
|
||||
}
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: aid,
|
||||
size: (nw, nh),
|
||||
xf: Affine::translate((frame_r.x0, frame_r.y0)) * Affine::scale(k),
|
||||
clip: None,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [0.0; 4],
|
||||
wrap: Default::default(),
|
||||
fill: Default::default(),
|
||||
});
|
||||
d.insert_item(sr, it, None)?;
|
||||
id
|
||||
let graphic = Content::Graphic(Graphic {
|
||||
asset: aid,
|
||||
size: (nw, nh),
|
||||
xf,
|
||||
clip: None,
|
||||
auto_fit,
|
||||
fit_align: 4,
|
||||
crop: [0.0; 4],
|
||||
wrap: Default::default(),
|
||||
fill: Default::default(),
|
||||
});
|
||||
let id = if let Some(fid) = frame {
|
||||
d.item_mut(fid).ok_or(designcraft_doc::DocError::NoItem(fid))?.content = graphic;
|
||||
fid
|
||||
} else {
|
||||
let id = ItemId(d.alloc());
|
||||
let mut it = Item::new(id, lid, Shape::Rectangle, shapes::rectangle(frame_rect));
|
||||
it.object_style = d.styles.default_graphic_frame.clone();
|
||||
if let Some(e) = d.styles.object_effects(&it.object_style) {
|
||||
it.effects = e.clone();
|
||||
}
|
||||
it.content = graphic;
|
||||
d.insert_item(sr, it, None)?;
|
||||
id
|
||||
};
|
||||
*sel = Selection::items(vec![id]);
|
||||
Ok(json!({"id": id.0}))
|
||||
@@ -926,7 +948,7 @@ fn snippet_export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn place_layout_page(s: &mut Session, p: &Value, name: &str, bytes: &[u8]) -> Result<Value> {
|
||||
const ID: &str = "file.place";
|
||||
let src = if name.to_lowercase().ends_with(".idml") {
|
||||
super::interchange::import_idml_confined(bytes).map_err(|e| bad(ID, e.to_string()))?
|
||||
super::interchange::import_idml_confined(bytes, str_param(p, "path").map(std::path::Path::new)).map_err(|e| bad(ID, e.to_string()))?
|
||||
} else {
|
||||
from_bytes(bytes)?
|
||||
};
|
||||
@@ -1051,7 +1073,7 @@ fn file_revert(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
{
|
||||
let mut d = if path.to_ascii_lowercase().ends_with(".idml") {
|
||||
let bytes = crate::io::read_file(&path).map_err(EngineError::Other)?;
|
||||
super::interchange::import_idml_confined(&bytes).map_err(|e| EngineError::Other(e.to_string()))?
|
||||
super::interchange::import_idml_confined(&bytes, Some(std::path::Path::new(&path))).map_err(|e| EngineError::Other(e.to_string()))?
|
||||
} else {
|
||||
from_bytes(&crate::io::read_file(&path).map_err(EngineError::Other)?)?
|
||||
};
|
||||
@@ -1438,6 +1460,9 @@ mod new_document_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod eps_place_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod pdf_crop_tests {
|
||||
use serde_json::json;
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
//! Placing EPS artwork: the source is kept and only the preview is rasterised.
|
||||
//!
|
||||
//! These helpers write real files, so they live in their own `_tests.rs` file: the
|
||||
//! file-system guard in `tests_file_guard.rs` checks `src` files but exempts test ones.
|
||||
|
||||
use super::*;
|
||||
|
||||
pub(crate) fn tiff_eps() -> Vec<u8> {
|
||||
let mut tiff = b"II".to_vec();
|
||||
tiff.extend(42u16.to_le_bytes());
|
||||
tiff.extend(8u32.to_le_bytes());
|
||||
tiff.extend(10u16.to_le_bytes());
|
||||
for (tag, ty, count, value) in [
|
||||
(256u16, 4u16, 1u32, 2u32),
|
||||
(257, 4, 1, 2),
|
||||
(258, 3, 3, 134),
|
||||
(259, 3, 1, 1),
|
||||
(262, 3, 1, 2),
|
||||
(273, 4, 1, 140),
|
||||
(277, 3, 1, 3),
|
||||
(278, 4, 1, 2),
|
||||
(279, 4, 1, 12),
|
||||
(284, 3, 1, 1),
|
||||
] {
|
||||
tiff.extend(tag.to_le_bytes());
|
||||
tiff.extend(ty.to_le_bytes());
|
||||
tiff.extend(count.to_le_bytes());
|
||||
tiff.extend(value.to_le_bytes());
|
||||
}
|
||||
tiff.extend(0u32.to_le_bytes());
|
||||
for _ in 0..3 {
|
||||
tiff.extend(8u16.to_le_bytes());
|
||||
}
|
||||
tiff.extend([255, 0, 0].repeat(4));
|
||||
let ps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EOF\n";
|
||||
let mut eps = vec![0xC5, 0xD0, 0xD3, 0xC6];
|
||||
for n in [30usize, ps.len(), 0, 0, 30 + ps.len(), tiff.len()] {
|
||||
eps.extend((n as u32).to_le_bytes());
|
||||
}
|
||||
eps.extend([255, 255]);
|
||||
eps.extend(ps);
|
||||
eps.extend(tiff);
|
||||
eps
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_degenerate_bounds_are_rejected_without_edits() {
|
||||
for bounds in ["0 0 1e-320 1e-320", "0 0 1e308 1e308", "0 0 inf 10"] {
|
||||
let source = format!("%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: {bounds}\n%%EOF\n");
|
||||
for command in ["file.place", "place.load"] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let revision = s.doc().unwrap().revision;
|
||||
assert!(
|
||||
s.execute(command, &json!({"base64": base64_encode(source.as_bytes()), "name": "bad.eps", "width": 100})).is_err(),
|
||||
"{command}: {bounds}"
|
||||
);
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
assert_eq!(s.doc().unwrap().revision, revision);
|
||||
assert!(s.loaded.is_none());
|
||||
designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_fitting_overflow_is_rejected_before_document_edits() {
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 0.01 0.01\n%%EOF\n";
|
||||
let params = json!({"base64": base64_encode(source), "name": "small.eps"});
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
assert!(s.execute("file.place", &json!({"base64": base64_encode(source), "name": "small.eps", "width": 1e308})).is_err());
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
s.execute("place.load", ¶ms).unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
assert!(s.execute("place.drop", &json!({"rect": [0, 0, 1e308, 1e308]})).is_err());
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
s.execute("place.drop", &json!({"rect": [72, 72, 172, 122]})).unwrap();
|
||||
designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_table_and_data_merge_reject_fitting_overflow() {
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 0.01 0.01\n%%EOF\n";
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect":[72,72,372,272],"content":"text"})).unwrap();
|
||||
s.execute("text.select", &json!({"story":r["story"],"anchor":0,"focus":0})).unwrap();
|
||||
s.execute("table.insert", &json!({"rows":1,"cols":1,"width":1e308})).unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
assert!(s.execute("table.placeGraphic", &json!({"base64":base64_encode(source),"name":"small.eps","fit":"fill"})).is_err());
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
designcraft_format::load(&designcraft_format::save(&before).unwrap()).unwrap();
|
||||
|
||||
let path = std::env::temp_dir().join(format!("dc-eps-fit-{}.eps", std::process::id()));
|
||||
std::fs::write(&path, source).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("data.source.select", &json!({"bytes":base64_encode(format!("@Photo\n{}\n",path.display()).as_bytes()),"name":"source.csv"})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect":[0,0,1e308,100],"content":"graphic"})).unwrap();
|
||||
s.execute("data.placeholder.add", &json!({"field":"Photo","item":r["id"]})).unwrap();
|
||||
let result = s.execute("data.merge", &json!({"fitting":"fillProportionally"})).unwrap();
|
||||
assert_eq!(result["missingImages"].as_array().unwrap().len(), 1);
|
||||
assert!(s.doc().unwrap().doc.assets.is_empty());
|
||||
designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_table_and_data_merge_use_bounding_box_points() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-eps-routes-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
for (index, bytes) in [b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EOF\n".to_vec(), tiff_eps()].into_iter().enumerate() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72,72,372,272], "content":"text"})).unwrap();
|
||||
s.execute("text.select", &json!({"story": r["story"], "anchor":0, "focus":0})).unwrap();
|
||||
s.execute("table.insert", &json!({"rows":1,"cols":1})).unwrap();
|
||||
s.execute("table.placeGraphic", &json!({"base64": base64_encode(&bytes), "name":"logo.eps"})).unwrap();
|
||||
let table = s.doc().unwrap().doc.stories.values().flat_map(|st| st.tables.values()).next().unwrap();
|
||||
assert_eq!(table.cells[0].graphic.as_ref().unwrap().size, (100.0, 50.0), "table {index}");
|
||||
let path = dir.join(format!("logo-{index}.eps"));
|
||||
std::fs::write(&path, &bytes).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("data.source.select", &json!({"bytes": base64_encode(format!("@Photo\n{}\n", path.display()).as_bytes()), "name":"source.csv"}))
|
||||
.unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect":[72,72,372,272],"content":"graphic"})).unwrap();
|
||||
s.execute("data.placeholder.add", &json!({"field":"Photo","item":r["id"]})).unwrap();
|
||||
let result = s.execute("data.merge", &json!({"fitting":"none"})).unwrap();
|
||||
assert!(result["missingImages"].as_array().unwrap().is_empty());
|
||||
let g = s
|
||||
.doc()
|
||||
.unwrap()
|
||||
.doc
|
||||
.spreads
|
||||
.iter()
|
||||
.flat_map(|sp| &sp.items)
|
||||
.find_map(|it| if let Content::Graphic(g) = &it.content { Some(g) } else { None })
|
||||
.unwrap();
|
||||
assert_eq!(g.size, (100.0, 50.0), "merge {index}");
|
||||
designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_idml_roundtrip_preserves_source_and_resource_type() {
|
||||
use std::io::Read;
|
||||
for bytes in [b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 50\n%%EOF\n".to_vec(), tiff_eps()] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("file.place", &json!({"base64":base64_encode(&bytes),"name":"logo.eps"})).unwrap();
|
||||
let source = s.doc().unwrap().doc.clone();
|
||||
let idml = designcraft_idml::export_idml(&source);
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(&idml)).unwrap();
|
||||
let names: Vec<_> = z.file_names().filter(|n| n.starts_with("Spreads/")).map(str::to_string).collect();
|
||||
let mut xml = String::new();
|
||||
for name in names {
|
||||
z.by_name(&name).unwrap().read_to_string(&mut xml).unwrap();
|
||||
}
|
||||
assert!(xml.contains("<EPS "), "EPS resource element missing");
|
||||
assert!(xml.contains("Encapsulated PostScript (EPS)"));
|
||||
assert!(!xml.contains("Portable Network Graphics (PNG)"));
|
||||
let restored = designcraft_idml::import_idml(&idml).unwrap();
|
||||
let a = restored.assets.values().next().unwrap();
|
||||
assert_eq!(a.mime, "application/postscript");
|
||||
assert_eq!(a.data.as_slice(), bytes);
|
||||
assert!(designcraft_render::decode_pixmap(&a.data).is_some());
|
||||
// A linked-only EPS keeps its type even when the external file is unavailable;
|
||||
// when the resolver supplies it, sniff the raw source rather than PNG metadata.
|
||||
let mut linked = (*source).clone();
|
||||
let a = Arc::make_mut(linked.assets.values_mut().next().unwrap());
|
||||
a.link = Some("/synthetic/logo.eps".into());
|
||||
let idml = designcraft_idml::export_idml_with(&linked, &designcraft_idml::ExportOptions { embed_images: false });
|
||||
let missing = designcraft_idml::import_idml_with(&idml, &|_| None).unwrap();
|
||||
let a = missing.assets.values().next().unwrap();
|
||||
assert_eq!(a.mime, "application/postscript");
|
||||
assert!(a.data.is_empty());
|
||||
let resolved = designcraft_idml::import_idml_with(&idml, &|path| {
|
||||
assert_eq!(path, "/synthetic/logo.eps");
|
||||
Some(bytes.clone())
|
||||
})
|
||||
.unwrap();
|
||||
let a = resolved.assets.values().next().unwrap();
|
||||
assert_eq!(a.mime, "application/postscript");
|
||||
assert_eq!(a.data.as_slice(), bytes);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_place_cursor_retains_source_and_uses_bounding_box_size() {
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%HiResBoundingBox: 0 0 100.5 50.25\n%%EOF\n";
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let result = s.execute("place.load", &json!({"base64": base64_encode(source), "name": "logo.eps"})).unwrap();
|
||||
let aid = AssetId(result["asset"].as_u64().unwrap());
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].data.as_slice(), source);
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].mime, "application/postscript");
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].pixels, Some((201, 101)));
|
||||
let result = s.execute("place.drop", &json!({"x": 72, "y": 72})).unwrap();
|
||||
let it = s.doc().unwrap().doc.item(ItemId(result["id"].as_u64().unwrap())).unwrap();
|
||||
let Content::Graphic(g) = &it.content else { panic!("expected a graphic") };
|
||||
assert_eq!(g.size, (100.5, 50.25));
|
||||
assert_eq!((it.bounds().width(), it.bounds().height()), g.size);
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
for invalid in ["%!PS-Adobe-3.0 EPSF-3.0\nno bounding box\n", "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 inf 10\n"] {
|
||||
let corrupt = json!({"base64": base64_encode(invalid.as_bytes()), "name": "bad.eps"});
|
||||
assert!(s.execute("place.load", &corrupt).is_err());
|
||||
assert!(s.execute("file.place", &corrupt).is_err());
|
||||
}
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Type › Find/Replace Font: the fonts a document uses (missing ones flagged) and replacing one
|
||||
//! font with another everywhere — local formatting and style definitions.
|
||||
//! font with another everywhere — local formatting and style definitions. Type › Reload Fonts:
|
||||
//! the fonts installed, edited or removed since they were read.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
@@ -8,7 +9,7 @@ use designcraft_doc::{CharAttrs, Document, Story};
|
||||
use designcraft_fonts::FontSource;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{CommandSpec, bad, cmd, has_doc, str_param};
|
||||
use super::{CommandSpec, always, bad, cmd, has_doc, str_param};
|
||||
use crate::{Result, Session};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -25,9 +26,30 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
has_doc,
|
||||
replace
|
||||
),
|
||||
cmd!(
|
||||
noundo "font.reload",
|
||||
"Reload Fonts",
|
||||
["Type"],
|
||||
None,
|
||||
"{} — scans the font folders again and reads again the installed font files changed or removed since they were read, then recomposes the open documents → {cataloged, changed: [path]}",
|
||||
always,
|
||||
reload
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// Fonts installed, edited or removed while the app runs (#328, #547): the font database reads
|
||||
/// them again, then every open document is composed again with what is installed now.
|
||||
fn reload(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let reloaded = designcraft_fonts::FontDb::global().reload_fonts();
|
||||
// A new cache: render jobs still holding the old one can't bring back the old fonts' layout.
|
||||
s.cache = Arc::new(designcraft_compose::Cache::new());
|
||||
for st in &mut s.docs {
|
||||
st.bump_revision();
|
||||
}
|
||||
Ok(json!({"cataloged": reloaded.cataloged, "changed": reloaded.changed}))
|
||||
}
|
||||
|
||||
/// Every story's text, including table cells and footnotes.
|
||||
pub(super) fn for_each_story(d: &Document, f: &mut dyn FnMut(&Story)) {
|
||||
let mut pending: Vec<&Story> = d.stories.values().map(AsRef::as_ref).collect();
|
||||
@@ -233,6 +255,22 @@ fn replace(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn reload_fonts_recomposes_open_documents_without_editing_them() {
|
||||
let mut s = Session::new();
|
||||
let r = s.execute("font.reload", &json!({})).unwrap();
|
||||
assert!(r["cataloged"].is_u64() && r["changed"].is_array(), "{r}");
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("frame.create", &json!({"rect": [72, 72, 400, 300], "content": "text", "text": "Some text here."})).unwrap();
|
||||
let (revision, undo) = (s.doc().unwrap().revision, s.doc().unwrap().history.undo.len());
|
||||
let cache = Arc::clone(&s.cache);
|
||||
s.execute("font.reload", &json!({})).unwrap();
|
||||
assert!(!Arc::ptr_eq(&cache, &s.cache), "the layout composed with the old fonts is let go");
|
||||
let st = s.doc().unwrap();
|
||||
assert_ne!(st.revision, revision, "the canvas draws the document again");
|
||||
assert_eq!(st.history.undo.len(), undo, "reloading fonts isn't an edit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replacement_resolves_inherited_family_and_style_and_rejects_missing_targets() {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
//! Interchange formats: IDML (InDesign Markup Language) import and export, and INDD/INDT
|
||||
//! import (converted to IDML in memory by indd-utils).
|
||||
//!
|
||||
//! A package's linked files are read from where the link points on this computer, then from the
|
||||
//! link's trailing names beside the package (where a package moved from its author's computer
|
||||
//! keeps them). Links to other computers and devices are not followed
|
||||
//! ([`crate::io::document_path`]). Only regular files that hold an image or a graphic format are
|
||||
//! read, and only up to [`MAX_LINKED_FILE_BYTES`].
|
||||
|
||||
use designcraft_doc::Document;
|
||||
use serde_json::{Value, json};
|
||||
@@ -33,28 +39,40 @@ fn export_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
|
||||
/// [`designcraft_idml::import_idml`] with the linked images read through [`crate::io`], so the
|
||||
/// automation read root applies to the paths the package names, and its PDF crops resolved.
|
||||
pub(crate) fn import_idml_confined(bytes: &[u8]) -> designcraft_idml::Result<Document> {
|
||||
let mut d = designcraft_idml::import_idml_with(bytes, &|path| crate::io::read_file(path).ok())?;
|
||||
/// [`designcraft_idml::import_idml`] with the linked images read through [`crate::io`] from
|
||||
/// beside `package` (the package's file, if it has one), so the automation read root applies to
|
||||
/// the paths the package names, and its PDF crops resolved.
|
||||
pub(crate) fn import_idml_confined(bytes: &[u8], package: Option<&std::path::Path>) -> designcraft_idml::Result<Document> {
|
||||
let read = |link: &str| -> Option<Vec<u8>> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
read_packaged_link(link, package).map(|(_, data)| data)
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let _ = (link, package);
|
||||
None
|
||||
}
|
||||
};
|
||||
let mut d = designcraft_idml::import_idml_with(bytes, &read)?;
|
||||
resolve_pdf_crops(&mut d);
|
||||
Ok(d)
|
||||
}
|
||||
|
||||
/// Import IDML bytes; `dir` is the folder the package came from (for relative link lookup).
|
||||
pub fn import(bytes: &[u8], dir: Option<&std::path::Path>) -> Result<Document> {
|
||||
import_report(bytes, dir).map(|(document, _)| document)
|
||||
/// Import IDML bytes; `package` is the package's file (its folder is where links are looked for).
|
||||
pub fn import(bytes: &[u8], package: Option<&std::path::Path>) -> Result<Document> {
|
||||
import_report(bytes, package).map(|(document, _)| document)
|
||||
}
|
||||
|
||||
/// [`import`], with the problems that did not stop it (parts missing from the package, …).
|
||||
pub fn import_report(bytes: &[u8], dir: Option<&std::path::Path>) -> Result<(Document, Vec<String>)> {
|
||||
import_report_with_previews(bytes, dir, &std::collections::HashMap::new())
|
||||
pub fn import_report(bytes: &[u8], package: Option<&std::path::Path>) -> Result<(Document, Vec<String>)> {
|
||||
import_report_with_previews(bytes, package, &std::collections::HashMap::new())
|
||||
}
|
||||
|
||||
/// [`import_report`], showing `previews` (graphic `Self` → image) for linked files that can't be read.
|
||||
fn import_report_with_previews(
|
||||
bytes: &[u8],
|
||||
dir: Option<&std::path::Path>,
|
||||
package: Option<&std::path::Path>,
|
||||
previews: &std::collections::HashMap<String, std::sync::Arc<Vec<u8>>>,
|
||||
) -> Result<(Document, Vec<String>)> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -62,13 +80,13 @@ fn import_report_with_previews(
|
||||
let read = |link: &str| -> Option<Vec<u8>> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let (path, data) = read_packaged_link(link, dir)?;
|
||||
let (path, data) = read_packaged_link(link, package)?;
|
||||
resolved.borrow_mut().insert(link.to_string(), path);
|
||||
Some(data)
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let _ = (link, &dir);
|
||||
let _ = (link, &package);
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -84,7 +102,7 @@ fn import_report_with_previews(
|
||||
std::sync::Arc::make_mut(asset).link = Some(path);
|
||||
}
|
||||
}
|
||||
resolve_packaged_links(&mut document, dir);
|
||||
resolve_packaged_links(&mut document, package);
|
||||
document
|
||||
};
|
||||
Ok((document, warnings))
|
||||
@@ -173,77 +191,107 @@ const MAX_LINK_PATH_BYTES: usize = 128 * 1024;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const MAX_LINK_SUFFIX_PARTS: usize = 8;
|
||||
|
||||
/// Largest linked file read when a package is opened (bytes); bigger files stay missing.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn packaged_link_candidates(link: &str, dir: Option<&std::path::Path>) -> Vec<std::path::PathBuf> {
|
||||
pub(crate) const MAX_LINKED_FILE_BYTES: u64 = 256 * 1024 * 1024;
|
||||
|
||||
/// The files to try for `link` in the package at `package`: the link itself, then its trailing
|
||||
/// names beside the package (without a package file, only an absolute link). Each is a path
|
||||
/// [`crate::io::document_path`] allows.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn packaged_link_candidates(link: &str, package: Option<&std::path::Path>) -> Vec<std::path::PathBuf> {
|
||||
// Enough for a maximum-length Windows path even with four-byte UTF-8 characters.
|
||||
// Bound splitting/allocation for link strings supplied by imported documents.
|
||||
if link.len() > MAX_LINK_PATH_BYTES {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut candidates = vec![std::path::PathBuf::from(link)];
|
||||
if let Some(dir) = dir {
|
||||
// IDML may retain Windows paths even when the package is opened on another platform.
|
||||
let parts: Vec<&str> = link.split(['/', '\\']).filter(|part| !part.is_empty()).collect();
|
||||
let safe_part = |part: &&str| *part != "." && *part != ".." && !part.contains(':');
|
||||
if !link.starts_with(['/', '\\']) && parts.iter().all(safe_part) {
|
||||
candidates.push(dir.join(parts.iter().collect::<std::path::PathBuf>()));
|
||||
let mut candidates: Vec<std::path::PathBuf> = crate::io::document_path(link, package).into_iter().collect();
|
||||
let Some(package) = package else { return candidates };
|
||||
// Names relative to the package, resolved beside it.
|
||||
let mut beside = |names: &[&str]| {
|
||||
if let Ok(path) = crate::io::document_path(&names.join("/"), Some(package))
|
||||
&& !candidates.contains(&path)
|
||||
{
|
||||
candidates.push(path);
|
||||
}
|
||||
// Packagers can keep subfolders under Links (for example, Links/illustrations/logo.ai).
|
||||
// Preserve that suffix instead of flattening every resource to its basename.
|
||||
if let Some(index) = parts.iter().rposition(|part| part.eq_ignore_ascii_case("Links")) {
|
||||
let suffix = &parts[index + 1..];
|
||||
if !suffix.is_empty() && suffix.iter().all(safe_part) {
|
||||
candidates.push(dir.join("Links").join(suffix.iter().collect::<std::path::PathBuf>()));
|
||||
}
|
||||
}
|
||||
// A package moved off its author's machine keeps its own subfolders (`/images/logo.png`
|
||||
// beside the .idml as `images/logo.png`): try the link's trailing folders, longest first.
|
||||
let first = parts.len().saturating_sub(MAX_LINK_SUFFIX_PARTS);
|
||||
for start in first..parts.len().saturating_sub(1) {
|
||||
if let Some(suffix) = parts.get(start..).filter(|s| s.iter().all(safe_part)) {
|
||||
candidates.push(dir.join(suffix.iter().collect::<std::path::PathBuf>()));
|
||||
}
|
||||
}
|
||||
if let Some(name) = parts.last().filter(|part| safe_part(part)) {
|
||||
candidates.push(dir.join(name));
|
||||
candidates.push(dir.join("Links").join(name));
|
||||
};
|
||||
// IDML may retain Windows paths even when the package is opened on another platform.
|
||||
let parts: Vec<&str> = link.split(['/', '\\']).filter(|part| !part.is_empty()).collect();
|
||||
let safe_part = |part: &&str| *part != "." && *part != ".." && !part.contains(':');
|
||||
// Packagers can keep subfolders under Links (for example, Links/illustrations/logo.ai).
|
||||
// Preserve that suffix instead of flattening every resource to its basename.
|
||||
if let Some(index) = parts.iter().rposition(|part| part.eq_ignore_ascii_case("Links")) {
|
||||
let suffix = parts.get(index + 1..).unwrap_or_default();
|
||||
if !suffix.is_empty() && suffix.iter().all(safe_part) {
|
||||
beside(&[&["Links"][..], suffix].concat());
|
||||
}
|
||||
}
|
||||
// A package moved off its author's machine keeps its own subfolders (`/images/logo.png`
|
||||
// beside the .idml as `images/logo.png`): try the link's trailing folders, longest first.
|
||||
let first = parts.len().saturating_sub(MAX_LINK_SUFFIX_PARTS);
|
||||
for start in first..parts.len().saturating_sub(1) {
|
||||
if let Some(suffix) = parts.get(start..).filter(|s| s.iter().all(safe_part)) {
|
||||
beside(suffix);
|
||||
}
|
||||
}
|
||||
if let Some(name) = parts.last().filter(|part| safe_part(part)) {
|
||||
beside(&[name]);
|
||||
beside(&["Links", name]);
|
||||
}
|
||||
candidates
|
||||
}
|
||||
|
||||
/// Bytes an image or a graphic can be placed from (raster formats, PDF, SVG, PSD, EPS).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn is_graphic(bytes: &[u8]) -> bool {
|
||||
designcraft_images::mime(bytes) != "application/octet-stream" || designcraft_images::is_eps(bytes)
|
||||
}
|
||||
|
||||
/// Embedded images do not call the IDML resource loader. Rebind their missing links
|
||||
/// beside the opened document too, without replacing their embedded image bytes.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub(crate) fn resolve_packaged_links(document: &mut Document, dir: Option<&std::path::Path>) {
|
||||
let Some(dir) = dir else { return };
|
||||
pub(crate) fn resolve_packaged_links(document: &mut Document, package: Option<&std::path::Path>) {
|
||||
let Some(package) = package else { return };
|
||||
for asset in document.assets.values_mut() {
|
||||
let Some(link) = asset.link.as_deref() else { continue };
|
||||
if link.len() > MAX_LINK_PATH_BYTES || crate::io::is_file(link) {
|
||||
if link.len() > MAX_LINK_PATH_BYTES {
|
||||
continue;
|
||||
}
|
||||
let Some(path) = packaged_link_candidates(link, Some(dir)).into_iter().find(|p| crate::io::is_file(p)) else { continue };
|
||||
// Preserve ordinary Windows paths rather than canonicalizing to a verbatim URI.
|
||||
let path = std::path::absolute(&path).unwrap_or(path);
|
||||
if let Ok(path) = crate::io::document_path(link, Some(package))
|
||||
&& crate::io::is_file(&path)
|
||||
{
|
||||
// A link relative to the document (what File › Package writes, `Links/logo.png`) is
|
||||
// held as the file it names, as every other link is, so preflight and updates find it.
|
||||
if std::path::Path::new(link).is_relative() {
|
||||
std::sync::Arc::make_mut(asset).link = Some(path.to_string_lossy().into_owned());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let Some(path) = packaged_link_candidates(link, Some(package)).into_iter().find(|p| crate::io::is_file(p)) else { continue };
|
||||
std::sync::Arc::make_mut(asset).link = Some(path.to_string_lossy().into_owned());
|
||||
}
|
||||
}
|
||||
|
||||
/// The first candidate for `link` ([`packaged_link_candidates`]) that is a regular file no larger
|
||||
/// than [`MAX_LINKED_FILE_BYTES`] holding an image or a graphic, with its path.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn read_packaged_link(link: &str, dir: Option<&std::path::Path>) -> Option<(String, Vec<u8>)> {
|
||||
for path in packaged_link_candidates(link, dir) {
|
||||
if let Ok(bytes) = crate::io::read_file(&path) {
|
||||
// Keep ordinary Windows drive/UNC syntax for IDML URI export; canonicalize
|
||||
// would introduce a verbatim prefix that is not an IDML file URI.
|
||||
let path = std::path::absolute(&path).unwrap_or(path);
|
||||
return Some((path.to_string_lossy().into_owned(), bytes));
|
||||
fn read_packaged_link(link: &str, package: Option<&std::path::Path>) -> Option<(String, Vec<u8>)> {
|
||||
for path in packaged_link_candidates(link, package) {
|
||||
if !crate::io::is_file(&path) || !crate::io::metadata(&path).is_ok_and(|m| m.len() <= MAX_LINKED_FILE_BYTES) {
|
||||
continue;
|
||||
}
|
||||
// At most one byte over the limit, in case the file grew since.
|
||||
let Ok(bytes) = crate::io::read_capped(&path, MAX_LINKED_FILE_BYTES) else { continue };
|
||||
if bytes.len() as u64 > MAX_LINKED_FILE_BYTES || !is_graphic(&bytes) {
|
||||
continue;
|
||||
}
|
||||
return Some((path.to_string_lossy().into_owned(), bytes));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn open_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (bytes, dir, file, title) = if let Some(b) = str_param(p, "base64") {
|
||||
let (bytes, package, file, title) = if let Some(b) = str_param(p, "base64") {
|
||||
let name = str_param(p, "name").unwrap_or_default();
|
||||
(base64_decode(b), None, name.to_string(), without_document_extension(name).to_string())
|
||||
} else if let Some(path) = str_param(p, "path") {
|
||||
@@ -253,7 +301,7 @@ pub(crate) fn open_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let b: Vec<u8> = Vec::new();
|
||||
let pp = std::path::Path::new(path);
|
||||
let file = pp.file_name().map_or_else(|| path.to_string(), |n| n.to_string_lossy().to_string());
|
||||
(b, pp.parent().map(|d| d.to_path_buf()), file, pp.file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default())
|
||||
(b, Some(pp.to_path_buf()), file, pp.file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default())
|
||||
} else {
|
||||
return Err(bad("file.openIdml", "missing `path` or `base64`"));
|
||||
};
|
||||
@@ -280,7 +328,7 @@ pub(crate) fn open_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
} else {
|
||||
bytes
|
||||
};
|
||||
let (mut d, import_warnings) = import_report_with_previews(&bytes, dir.as_deref(), &previews)?;
|
||||
let (mut d, import_warnings) = import_report_with_previews(&bytes, package.as_deref(), &previews)?;
|
||||
warnings.extend(import_warnings);
|
||||
if !title.is_empty() {
|
||||
d.title = title;
|
||||
@@ -346,12 +394,12 @@ mod tests {
|
||||
std::fs::write(&path, &png).unwrap();
|
||||
std::fs::write(dir.join("logo.png"), b"a different file with the same basename").unwrap();
|
||||
|
||||
let doc = import(&idml, Some(&dir)).unwrap();
|
||||
let doc = import(&idml, Some(&dir.join("x.idml"))).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(*asset.data, png, "the packaged graphic must render even when its author-machine path no longer exists");
|
||||
let resolved = std::path::absolute(&path).unwrap();
|
||||
assert_eq!(asset.link.as_deref(), resolved.to_str(), "remember the resolved link so update and preflight use the packaged file");
|
||||
assert_eq!(super::super::links::status(asset), "ok");
|
||||
assert_eq!(super::super::links::status(asset, None), "ok");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -364,11 +412,11 @@ mod tests {
|
||||
let (idml, png) = linked_idml("/images/logo.png", false).unwrap();
|
||||
std::fs::write(&path, &png).unwrap();
|
||||
|
||||
let doc = import(&idml, Some(&dir)).unwrap();
|
||||
let doc = import(&idml, Some(&dir.join("x.idml"))).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(*asset.data, png);
|
||||
assert_eq!(asset.link.as_deref(), std::path::absolute(&path).unwrap().to_str());
|
||||
assert_eq!(super::super::links::status(asset), "ok");
|
||||
assert_eq!(super::super::links::status(asset, None), "ok");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -380,11 +428,11 @@ mod tests {
|
||||
let (idml, png) = linked_idml("/original-computer/project/logo.png", false).unwrap();
|
||||
std::fs::write(&path, &png).unwrap();
|
||||
|
||||
let doc = import(&idml, Some(&dir)).unwrap();
|
||||
let doc = import(&idml, Some(&dir.join("x.idml"))).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(*asset.data, png);
|
||||
assert_eq!(asset.link.as_deref(), std::path::absolute(&path).unwrap().to_str());
|
||||
assert_eq!(super::super::links::status(asset), "ok");
|
||||
assert_eq!(super::super::links::status(asset, None), "ok");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -400,39 +448,90 @@ mod tests {
|
||||
let (idml, png) = linked_idml(original.to_str().unwrap(), true).unwrap();
|
||||
std::fs::write(&original, b"modified original").unwrap();
|
||||
std::fs::write(©, &png).unwrap();
|
||||
let doc = import(&idml, Some(&package)).unwrap();
|
||||
let doc = import(&idml, Some(&package.join("x.idml"))).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(std::path::Path::new(asset.link.as_deref().unwrap()), original.as_path(), "an existing original stays authoritative");
|
||||
assert_eq!(*asset.data, png);
|
||||
assert_eq!(super::super::links::status(asset), "modified");
|
||||
assert_eq!(super::super::links::status(asset, None), "modified");
|
||||
|
||||
std::fs::remove_file(&original).unwrap();
|
||||
std::fs::write(©, b"modified packaged copy").unwrap();
|
||||
let doc = import(&idml, Some(&package)).unwrap();
|
||||
let doc = import(&idml, Some(&package.join("x.idml"))).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(asset.link.as_deref(), copy.to_str());
|
||||
assert_eq!(*asset.data, png, "rebinding must not replace embedded pixels");
|
||||
assert_eq!(super::super::links::status(asset), "modified");
|
||||
assert_eq!(super::super::links::status(asset, None), "modified");
|
||||
|
||||
std::fs::remove_file(©).unwrap();
|
||||
for folder in [Some(package.as_path()), None] {
|
||||
let packaged = package.join("x.idml");
|
||||
for folder in [Some(packaged.as_path()), None] {
|
||||
let doc = import(&idml, folder).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(std::path::Path::new(asset.link.as_deref().unwrap()), original.as_path(), "keep a missing link when there is no candidate");
|
||||
assert_eq!(*asset.data, png);
|
||||
assert_eq!(super::super::links::status(asset), "missing");
|
||||
assert_eq!(super::super::links::status(asset, None), "missing");
|
||||
}
|
||||
let oversized = "a/".repeat(MAX_LINK_PATH_BYTES);
|
||||
assert!(packaged_link_candidates(&oversized, Some(&package)).is_empty());
|
||||
assert!(packaged_link_candidates(&oversized, Some(&packaged)).is_empty());
|
||||
let mut doc = import(&idml, None).unwrap();
|
||||
Arc::make_mut(doc.assets.values_mut().next().unwrap()).link = Some(oversized.clone());
|
||||
resolve_packaged_links(&mut doc, Some(&package));
|
||||
resolve_packaged_links(&mut doc, Some(&packaged));
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!(asset.link.as_deref(), Some(oversized.as_str()));
|
||||
assert_eq!(*asset.data, png);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_local_graphic_files_are_read_from_links() {
|
||||
let base = std::env::temp_dir().join(format!("dc-idml-link-reach-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (dir, outside) = (base.join("package"), base.join("outside"));
|
||||
std::fs::create_dir_all(dir.join("Links")).unwrap();
|
||||
std::fs::create_dir_all(&outside).unwrap();
|
||||
let package = dir.join("x.idml");
|
||||
let png = designcraft_render::Rendered { width: 2, height: 2, pixels: vec![255; 16] }.to_png();
|
||||
std::fs::write(dir.join("Links").join("notes.png"), b"not an image").unwrap();
|
||||
std::fs::write(outside.join("notes.txt"), b"not an image").unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
std::os::unix::fs::symlink("/dev/zero", dir.join("Links").join("zero.png")).unwrap();
|
||||
std::os::unix::fs::symlink("/dev/zero", outside.join("zero.png")).unwrap();
|
||||
}
|
||||
let (text, zero) = (outside.join("notes.txt"), outside.join("zero.png"));
|
||||
let links = [
|
||||
r"\\192.0.2.1\share\x.png",
|
||||
"file://192.0.2.1/s/x.png",
|
||||
text.to_str().unwrap(),
|
||||
zero.to_str().unwrap(),
|
||||
"/dev/zero",
|
||||
"Links/zero.png",
|
||||
"Links/notes.png",
|
||||
];
|
||||
for link in links {
|
||||
let (idml, _) = linked_idml(link, false).unwrap();
|
||||
let (doc, seen) = crate::io::touched(|| import(&idml, Some(&package)).unwrap());
|
||||
// Beside the package, and the link itself where it is on this computer.
|
||||
assert!(seen.iter().all(|p| p.starts_with(&dir) || p == std::path::Path::new(link)), "{link}: {seen:?}");
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert!(asset.data.is_empty(), "{link}");
|
||||
assert_eq!(super::super::links::status(asset, None), "missing", "{link}");
|
||||
}
|
||||
// An image elsewhere on this computer is read, and so is one beside the package.
|
||||
let away = outside.join("logo.png");
|
||||
std::fs::write(&away, &png).unwrap();
|
||||
let (idml, _) = linked_idml(away.to_str().unwrap(), false).unwrap();
|
||||
let doc = import(&idml, Some(&package)).unwrap();
|
||||
let asset = doc.assets.values().next().unwrap();
|
||||
assert_eq!((&*asset.data, asset.link.as_deref()), (&png, away.to_str()));
|
||||
assert_eq!(super::super::links::status(asset, None), "ok");
|
||||
std::fs::write(dir.join("Links").join("logo.png"), &png).unwrap();
|
||||
let (idml, _) = linked_idml("Links/logo.png", false).unwrap();
|
||||
let doc = import(&idml, Some(&package)).unwrap();
|
||||
assert_eq!(*doc.assets.values().next().unwrap().data, png);
|
||||
std::fs::remove_dir_all(base).unwrap();
|
||||
}
|
||||
|
||||
/// INDD routing without an InDesign file: bytes with the INDD signature (or an .indd/.indt
|
||||
/// name) go to the converter, and what it can't read is an open error that adds no document.
|
||||
#[test]
|
||||
|
||||
@@ -257,6 +257,27 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
Ok(json!({"id": id.0, "prefix": prefix}))
|
||||
})
|
||||
}),
|
||||
cmd!(
|
||||
"layout.parents.delete",
|
||||
"Delete Parent",
|
||||
[],
|
||||
None,
|
||||
"{id: parent spread id} — unapply the parent and keep local overrides as ordinary items",
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let id = p.get("id").and_then(Value::as_u64).map(SpreadId).ok_or_else(|| bad("layout.parents.delete", "missing parent id"))?;
|
||||
s.edit(|d, sel| {
|
||||
d.delete_parent(id)?;
|
||||
*sel = Default::default();
|
||||
ok()
|
||||
})?;
|
||||
let st = s.doc_mut()?;
|
||||
if st.doc.parents.is_empty() {
|
||||
st.editing_parents = false;
|
||||
}
|
||||
ok()
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
noundo "layout.parents.edit",
|
||||
"Edit Parents",
|
||||
@@ -2030,3 +2051,7 @@ mod parent_base_tests {
|
||||
assert_eq!(d.parents[1].parent.as_ref().unwrap().based_on, Some(a));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "parent_delete_tests.rs"]
|
||||
mod parent_delete_tests;
|
||||
|
||||
+579
-83
@@ -7,10 +7,16 @@
|
||||
//!
|
||||
//! A linked text file (File › Place with Create Links) remembers its file's size and hash on the
|
||||
//! story instead; updating it imports the file again into the story.
|
||||
//!
|
||||
//! A link is followed only where [`crate::io::document_path`] allows it, resolved against the
|
||||
//! document's own file; a refused link is missing, with the reason in `links.list`.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use designcraft_doc::{AssetId, Content, Document, Fingerprint, ItemId, Selection, Story, StoryId, TextFileLink};
|
||||
use designcraft_compose::Cache;
|
||||
use designcraft_doc::{AssetId, Content, Document, Fingerprint, Item, ItemId, Selection, Story, StoryId, TextFileLink};
|
||||
use designcraft_geom::Affine;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{CommandSpec, bad, cmd, has_doc, str_param};
|
||||
@@ -18,10 +24,10 @@ use crate::{EngineError, Result, Session};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(query "links.list", "Links", [], None, "{} → [{asset, name, path, status: ok|modified|missing|embedded, pixels, pdfCrop (placed PDFs: the box shown), uses: [{id, page, ppi}]}], then linked text files: [{story, name, path, status (also tooLarge: unchanged size, too big to check), edited (the story was changed in the layout), uses: [{id, page}]}]", has_doc, |s, _| {
|
||||
cmd!(query "links.list", "Links", [], None, "{} → [{asset, name, path, status: ok|modified|missing|embedded, refused?: why a missing link isn't followed (a path on another computer, a device), pixels, pdfCrop (placed PDFs: the box shown), uses: [{id, page, ppi}]}], then linked text files: [{story, name, path, status (also tooLarge: unchanged size, too big to check), edited (the story was changed in the layout), uses: [{id, page}]}]", has_doc, |s, _| {
|
||||
// Taken out for the call so the document needn't be cloned on every poll.
|
||||
let mut checks = std::mem::take(&mut s.text_link_checks);
|
||||
let links = s.doc().map(|d| list(&d.doc, &mut checks));
|
||||
let links = s.doc().map(|d| list(&d.doc, &s.cache, d.path.as_deref().map(Path::new), &mut checks));
|
||||
s.text_link_checks = checks;
|
||||
Ok(Value::Array(links?))
|
||||
}),
|
||||
@@ -30,7 +36,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Relink…",
|
||||
["Window", "Links"],
|
||||
None,
|
||||
"{asset | story, path} — use another file for every placement of the graphic, or import another text file into the linked story",
|
||||
"{asset | story, path} — use another file for every placement of the graphic (keeps the selected PDF page; error if missing), or import another text file into the linked story",
|
||||
has_doc,
|
||||
relink
|
||||
),
|
||||
@@ -126,31 +132,47 @@ fn uses(d: &Document, aid: AssetId) -> Vec<ItemId> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Why a link stored in the document at `document` isn't followed, if it isn't.
|
||||
pub fn refused(link: &str, document: Option<&Path>) -> Option<crate::io::Refused> {
|
||||
crate::io::document_path(link, document).err()
|
||||
}
|
||||
|
||||
/// Whether an asset's linked file is gone ([`status`] "missing" without reading the file). An
|
||||
/// asset can hold data while its file is missing: the document's preview of it.
|
||||
pub fn link_missing(a: &designcraft_doc::Asset) -> bool {
|
||||
let Some(path) = &a.link else { return false };
|
||||
/// asset can hold data while its file is missing: the document's preview of it. `document` is
|
||||
/// the document's file, which relative links are resolved against.
|
||||
pub fn link_missing(a: &designcraft_doc::Asset, document: Option<&Path>) -> bool {
|
||||
let Some(link) = &a.link else { return false };
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
!crate::io::exists(path)
|
||||
crate::io::document_path(link, document).map_or(true, |path| !crate::io::exists(path))
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let _ = path;
|
||||
let _ = (link, document);
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Link status of an asset.
|
||||
pub fn status(a: &designcraft_doc::Asset) -> &'static str {
|
||||
let Some(path) = &a.link else { return "embedded" };
|
||||
/// Link status of an asset in the document at `document`. Only a regular file is compared, so a
|
||||
/// link to a device (`/dev/zero`) or a FIFO is missing rather than read.
|
||||
pub fn status(a: &designcraft_doc::Asset, document: Option<&Path>) -> &'static str {
|
||||
let Some(link) = &a.link else { return "embedded" };
|
||||
// Nothing was read from the file (a package's link that wasn't followed): missing, as in
|
||||
// Preflight.
|
||||
if a.data.is_empty() {
|
||||
return "missing";
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
match crate::io::metadata(path) {
|
||||
let Ok(path) = crate::io::document_path(link, document) else { return "missing" };
|
||||
if !crate::io::is_file(&path) {
|
||||
return "missing";
|
||||
}
|
||||
match crate::io::metadata(&path) {
|
||||
Err(_) => "missing",
|
||||
Ok(m) if m.len() as usize != a.data.len() => "modified",
|
||||
// Same size: compare the bytes (cheap for the sizes involved, and exact).
|
||||
Ok(_) => match crate::io::read_file(path) {
|
||||
Ok(_) => match crate::io::read_capped(&path, a.data.len() as u64) {
|
||||
Ok(b) if b == *a.data => "ok",
|
||||
Ok(_) => "modified",
|
||||
Err(_) => "missing",
|
||||
@@ -159,7 +181,7 @@ pub fn status(a: &designcraft_doc::Asset) -> &'static str {
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let _ = path;
|
||||
let _ = (link, document);
|
||||
"ok"
|
||||
}
|
||||
}
|
||||
@@ -196,14 +218,15 @@ pub(crate) type TextChecks = std::collections::HashMap<String, (Fingerprint, Str
|
||||
/// Link status of a linked text file: `tooLarge` when it is the recorded size but over
|
||||
/// [`MAX_TEXT_LINK_BYTES`], so its contents aren't checked. The file is read only when its size
|
||||
/// is the recorded one but its time isn't, and then once per new time (`checks`): a file saved
|
||||
/// or synced again without changes stays OK.
|
||||
pub(crate) fn text_status(l: &TextFileLink, checks: &mut TextChecks) -> &'static str {
|
||||
/// or synced again without changes stays OK. `document` is the document's file.
|
||||
pub(crate) fn text_status(l: &TextFileLink, checks: &mut TextChecks, document: Option<&Path>) -> &'static str {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
if !crate::io::is_file(&l.path) {
|
||||
let Ok(path) = crate::io::document_path(&l.path, document) else { return "missing" };
|
||||
if !crate::io::is_file(&path) {
|
||||
return "missing";
|
||||
}
|
||||
let now = match crate::io::metadata(&l.path) {
|
||||
let now = match crate::io::metadata(&path) {
|
||||
Err(_) => return "missing",
|
||||
Ok(m) => super::datamerge::fingerprint_from(&m),
|
||||
};
|
||||
@@ -216,7 +239,7 @@ pub(crate) fn text_status(l: &TextFileLink, checks: &mut TextChecks) -> &'static
|
||||
let hash = match checks.get(&l.path) {
|
||||
Some((at, hash)) if *at == now => hash.clone(),
|
||||
_ if now.size > MAX_TEXT_LINK_BYTES => return "tooLarge",
|
||||
_ => match read_text(&l.path) {
|
||||
_ => match read_text(&path) {
|
||||
Ok((b, at)) => {
|
||||
let hash = fnv_hex(&b);
|
||||
// The time from before the read: a change during it is seen next time.
|
||||
@@ -232,7 +255,7 @@ pub(crate) fn text_status(l: &TextFileLink, checks: &mut TextChecks) -> &'static
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let _ = (l, checks);
|
||||
let _ = (l, checks, document);
|
||||
"ok"
|
||||
}
|
||||
}
|
||||
@@ -241,40 +264,93 @@ fn file_name(path: &str) -> String {
|
||||
std::path::Path::new(path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.to_string())
|
||||
}
|
||||
|
||||
fn list(d: &Document, checks: &mut TextChecks) -> Vec<Value> {
|
||||
/// A graphic showing an asset: the item, its page, and the transform from its parent's space to
|
||||
/// the spread.
|
||||
struct Shown<'a> {
|
||||
item: &'a Item,
|
||||
page: Option<usize>,
|
||||
parent_xf: Affine,
|
||||
}
|
||||
|
||||
/// Where an asset shows: frames on spreads (see [`uses`]) and anchored or inline objects, whose
|
||||
/// page and position come from the frame their story lays them out in.
|
||||
fn shown<'a>(d: &'a Document, cache: &Cache, aid: AssetId) -> Vec<Shown<'a>> {
|
||||
fn walk<'a>(it: &'a Item, xf: Affine, aid: AssetId, page: Option<usize>, out: &mut Vec<Shown<'a>>) {
|
||||
if let Content::Graphic(g) = &it.content
|
||||
&& g.asset == aid
|
||||
{
|
||||
out.push(Shown { item: it, page, parent_xf: xf });
|
||||
}
|
||||
for c in it.children() {
|
||||
walk(c, xf * it.xf, aid, page, out);
|
||||
}
|
||||
}
|
||||
let mut out: Vec<Shown> = uses(d, aid)
|
||||
.into_iter()
|
||||
.filter_map(|id| {
|
||||
let loc = d.find(id)?;
|
||||
Some(Shown { item: d.item_at(&loc)?, page: d.page_of_item(id), parent_xf: d.parent_xf(&loc) })
|
||||
})
|
||||
.collect();
|
||||
for st in d.stories.values() {
|
||||
let mut composed = None;
|
||||
for (k, o) in st.objects.iter().enumerate() {
|
||||
let mut found = false;
|
||||
o.item.walk(&mut |i| found |= matches!(&i.content, Content::Graphic(g) if g.asset == aid));
|
||||
if !found {
|
||||
continue;
|
||||
}
|
||||
let cs = composed.get_or_insert_with(|| cache.get(d, st.id, None));
|
||||
let placed = cs.frames.iter().find_map(|ft| ft.objects.iter().find(|p| p.index == k).map(|p| (ft.frame, p.origin)));
|
||||
// Overset: no page; the resolution of the object as it would show.
|
||||
let (page, xf) = match placed {
|
||||
Some((fid, origin)) => {
|
||||
let frame_xf =
|
||||
d.find(fid).map_or(Affine::IDENTITY, |loc| d.parent_xf(&loc)) * d.item(fid).map_or(Affine::IDENTITY, |i| d.text_xf(i));
|
||||
(d.page_of_item(fid), frame_xf * Affine::translate(origin.to_vec2()))
|
||||
}
|
||||
None => (None, Affine::IDENTITY),
|
||||
};
|
||||
walk(&o.item, xf, aid, page, &mut out);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn list(d: &Document, cache: &Cache, document: Option<&Path>, checks: &mut TextChecks) -> Vec<Value> {
|
||||
let texts = d.stories.values().filter_map(|st| {
|
||||
let l = st.file_link.as_ref()?;
|
||||
let uses: Vec<Value> = st.frames.iter().map(|id| json!({"id": id.0, "page": d.page_of_item(*id).map(|p| d.page_name(p))})).collect();
|
||||
Some(json!({"story": st.id.0, "name": file_name(&l.path), "path": l.path, "status": text_status(l, checks), "edited": story_hash(st) != l.story_hash, "uses": uses}))
|
||||
Some(json!({"story": st.id.0, "name": file_name(&l.path), "path": l.path, "status": text_status(l, checks, document), "refused": refused(&l.path, document).map(|r| r.to_string()), "edited": story_hash(st) != l.story_hash, "uses": uses}))
|
||||
});
|
||||
let used = d.used_assets();
|
||||
d.assets
|
||||
.values()
|
||||
.filter(|a| used.contains(&a.id))
|
||||
.map(|a| {
|
||||
let uses: Vec<Value> = uses(d, a.id)
|
||||
let uses: Vec<Value> = shown(d, cache, a.id)
|
||||
.into_iter()
|
||||
.map(|id| {
|
||||
.map(|u| {
|
||||
let it = u.item;
|
||||
// Effective resolution: image pixels over the size it is shown at.
|
||||
let ppi = (|| {
|
||||
let loc = d.find(id)?;
|
||||
let it = d.item_at(&loc)?;
|
||||
let Content::Graphic(g) = &it.content else { return None };
|
||||
// Vector (placed PDF or SVG): no effective resolution.
|
||||
if a.mime == "application/pdf" || a.mime == "image/svg+xml" {
|
||||
return None;
|
||||
}
|
||||
let (pw, _) = a.pixels?;
|
||||
let xf = d.parent_xf(&loc) * it.xf * g.xf;
|
||||
let xf = u.parent_xf * it.xf * g.xf;
|
||||
let shown_w = xf.transform_rect_bbox(designcraft_geom::Rect::new(0.0, 0.0, g.size.0, g.size.1)).width();
|
||||
(shown_w > 0.0).then(|| (pw as f64 / (shown_w / 72.0)).round())
|
||||
})();
|
||||
let page = d.page_of_item(id).map(|p| d.page_name(p));
|
||||
json!({"id": id.0, "page": page, "ppi": ppi})
|
||||
let page = u.page.map(|p| d.page_name(p));
|
||||
json!({"id": it.id.0, "page": page, "ppi": ppi})
|
||||
})
|
||||
.collect();
|
||||
let pdf_crop = (a.mime == "application/pdf").then(|| a.pdf_crop.name());
|
||||
json!({"asset": a.id.0, "name": a.name, "path": a.link, "status": status(a), "pixels": a.pixels, "pdfCrop": pdf_crop, "uses": uses})
|
||||
let why = a.link.as_deref().and_then(|l| refused(l, document)).map(|r| r.to_string());
|
||||
json!({"asset": a.id.0, "name": a.name, "path": a.link, "status": status(a, document), "refused": why, "pixels": a.pixels, "pdfCrop": pdf_crop, "uses": uses})
|
||||
})
|
||||
.chain(texts)
|
||||
.collect()
|
||||
@@ -282,15 +358,16 @@ fn list(d: &Document, checks: &mut TextChecks) -> Vec<Value> {
|
||||
|
||||
/// A linked text file's bytes, with its size and time from before they were read.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn read_text(path: &str) -> Result<(Vec<u8>, Fingerprint)> {
|
||||
let err = |e: std::io::Error| EngineError::Other(format!("{path}: {e}"));
|
||||
fn read_text(path: impl AsRef<Path>) -> Result<(Vec<u8>, Fingerprint)> {
|
||||
let path = path.as_ref();
|
||||
let err = |e: std::io::Error| EngineError::Other(format!("{}: {e}", path.display()));
|
||||
// The roots' own message when they refuse the path.
|
||||
crate::file_access::check_read(path).map_err(EngineError::Other)?;
|
||||
let m = crate::io::metadata(path).map_err(err)?;
|
||||
if !crate::io::is_file(path) {
|
||||
return Err(EngineError::Other(format!("{path}: not a file")));
|
||||
return Err(EngineError::Other(format!("{}: not a file", path.display())));
|
||||
}
|
||||
let too_large = || EngineError::Other(format!("{path}: larger than {} MiB", MAX_TEXT_LINK_BYTES / (1024 * 1024)));
|
||||
let too_large = || EngineError::Other(format!("{}: larger than {} MiB", path.display(), MAX_TEXT_LINK_BYTES / (1024 * 1024)));
|
||||
if m.len() > MAX_TEXT_LINK_BYTES {
|
||||
return Err(too_large());
|
||||
}
|
||||
@@ -303,8 +380,8 @@ fn read_text(path: &str) -> Result<(Vec<u8>, Fingerprint)> {
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn read_text(path: &str) -> Result<(Vec<u8>, Fingerprint)> {
|
||||
Err(EngineError::Other(format!("{path}: files can't be read on the web")))
|
||||
fn read_text(path: impl AsRef<Path>) -> Result<(Vec<u8>, Fingerprint)> {
|
||||
Err(EngineError::Other(format!("{}: files can't be read on the web", path.as_ref().display())))
|
||||
}
|
||||
|
||||
/// Import a linked text file again; a warning says when edits made in the layout were replaced.
|
||||
@@ -318,15 +395,41 @@ fn reimport(d: &mut Document, sid: StoryId, path: &str, bytes: &[u8], before: Fi
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn reload(path: &str) -> Result<(Vec<u8>, (u32, u32))> {
|
||||
fn reload(path: impl AsRef<Path>) -> Result<(Vec<u8>, (u32, u32))> {
|
||||
let path = path.as_ref();
|
||||
let b = crate::io::read_file(path).map_err(EngineError::Other)?;
|
||||
let px = designcraft_render::image_size(&b).ok_or_else(|| bad("links", format!("{path}: unsupported or corrupt image")))?;
|
||||
let px = designcraft_render::image_size(&b).ok_or_else(|| bad("links", format!("{}: unsupported or corrupt image", path.display())))?;
|
||||
Ok((b, px))
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn reload(path: &str) -> Result<(Vec<u8>, (u32, u32))> {
|
||||
Err(EngineError::Other(format!("{path}: files can't be read on the web")))
|
||||
fn reload(path: impl AsRef<Path>) -> Result<(Vec<u8>, (u32, u32))> {
|
||||
Err(EngineError::Other(format!("{}: files can't be read on the web", path.as_ref().display())))
|
||||
}
|
||||
|
||||
/// The replacement for `a`: bytes read from `read`, with `link` recorded as the stored link.
|
||||
/// Relink passes the file the user chose as both. Update passes the path the stored link resolved
|
||||
/// to and the stored link itself, so refreshing a link never rewrites what the document holds.
|
||||
fn relinked_asset(a: &designcraft_doc::Asset, read: &Path, link: &str) -> Result<Arc<designcraft_doc::Asset>> {
|
||||
let (bytes, px) = reload(read)?;
|
||||
let page = if designcraft_render::is_pdf(&bytes) { a.page } else { 0 };
|
||||
let px = if page > 0 {
|
||||
designcraft_render::pdf_page_size(&bytes, page as usize)
|
||||
.map(|(w, h)| (w.round().max(1.0) as u32, h.round().max(1.0) as u32))
|
||||
.ok_or_else(|| bad("links", format!("{}: can't read PDF page {}", read.display(), u64::from(page) + 1)))?
|
||||
} else {
|
||||
px
|
||||
};
|
||||
let mut a = a.clone();
|
||||
a.mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
a.data = Arc::new(bytes);
|
||||
a.pixels = Some(px);
|
||||
a.page = page;
|
||||
a.name = Path::new(link).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| link.to_string());
|
||||
a.link = Some(link.to_string());
|
||||
// A placed PDF keeps its crop choice; its box is found on the new file.
|
||||
super::file::crop_pdf(&mut a);
|
||||
Ok(Arc::new(a))
|
||||
}
|
||||
|
||||
fn relink(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
@@ -340,18 +443,12 @@ fn relink(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
});
|
||||
}
|
||||
let aid = asset_param(p, "links.relink")?;
|
||||
let (bytes, px) = reload(&path)?;
|
||||
let name = std::path::Path::new(&path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.clone());
|
||||
let a = s.doc()?.doc.assets.get(&aid).ok_or_else(|| bad("links.relink", "no such graphic"))?;
|
||||
// Build the replacement before the edit: a file that cannot be read leaves the document as it was.
|
||||
let fresh = relinked_asset(a, Path::new(&path), &path)?;
|
||||
s.edit(|d, _| {
|
||||
let a = d.assets.get_mut(&aid).ok_or_else(|| bad("links.relink", "no such graphic"))?;
|
||||
let a = Arc::make_mut(a);
|
||||
a.mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
a.data = Arc::new(bytes.clone());
|
||||
a.pixels = Some(px);
|
||||
a.name = name.clone();
|
||||
a.link = Some(path.clone());
|
||||
// A placed PDF keeps its crop choice; its box is found on the new file.
|
||||
super::file::crop_pdf(a);
|
||||
let name = fresh.name.clone();
|
||||
d.assets.insert(aid, fresh);
|
||||
Ok(json!({"name": name}))
|
||||
})
|
||||
}
|
||||
@@ -363,16 +460,20 @@ fn relink_folder(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
crate::file_access::check_read(dir).map_err(EngineError::Other)?;
|
||||
}
|
||||
let ext = str_param(p, "extension").map(|e| e.trim_start_matches('.').to_string());
|
||||
if ext.as_ref().is_some_and(|e| e.contains(['/', '\\', '\0'])) {
|
||||
return Err(bad("links.relinkFolder", "extension must not contain path separators or NUL"));
|
||||
}
|
||||
if dir.is_none() && ext.is_none() {
|
||||
return Err(bad("links.relinkFolder", "give `dir` and/or `extension`"));
|
||||
}
|
||||
let missing_only = p.get("missingOnly").and_then(Value::as_bool).unwrap_or(dir.is_some() && ext.is_none());
|
||||
let only: Option<Vec<u64>> = p.get("assets").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).collect());
|
||||
let d = &s.doc()?.doc;
|
||||
let st = s.doc()?;
|
||||
let (d, document) = (&st.doc, st.path.as_deref().map(Path::new));
|
||||
let mut todo: Vec<(AssetId, std::path::PathBuf)> = Vec::new();
|
||||
let mut not_found = Vec::new();
|
||||
for a in d.assets.values() {
|
||||
if only.as_ref().is_some_and(|o| !o.contains(&a.id.0)) || (missing_only && status(a) != "missing") {
|
||||
if only.as_ref().is_some_and(|o| !o.contains(&a.id.0)) || (missing_only && status(a, document) != "missing") {
|
||||
continue;
|
||||
}
|
||||
let old = std::path::PathBuf::from(a.link.clone().unwrap_or_else(|| a.name.clone()));
|
||||
@@ -380,47 +481,70 @@ fn relink_folder(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(e) = &ext {
|
||||
name = std::path::Path::new(&name).with_extension(e).into_os_string();
|
||||
}
|
||||
let folder = dir.clone().or_else(|| old.parent().map(|p| p.to_path_buf())).unwrap_or_default();
|
||||
let cand = folder.join(&name);
|
||||
if crate::io::exists(&cand) {
|
||||
// The link's own folder only where the link may be followed; the name only as a plain
|
||||
// file name (not a device).
|
||||
let folder = match &dir {
|
||||
Some(dir) => Some(dir.clone()),
|
||||
None => a.link.as_deref().and_then(|l| crate::io::document_path(l, document).ok()).and_then(|p| p.parent().map(Path::to_path_buf)),
|
||||
};
|
||||
let plain_name = name.to_str().is_some_and(|n| !n.contains(['/', '\\']) && craft_docpath::is_local(n));
|
||||
let cand = folder.filter(|_| plain_name).map(|f| f.join(&name));
|
||||
if let Some(cand) = cand.filter(|c| crate::io::exists(c)) {
|
||||
todo.push((a.id, cand));
|
||||
} else {
|
||||
not_found.push(name.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
let mut n = 0;
|
||||
// Load every replacement before committing any of them: a bad file must not leave an
|
||||
// unrecorded partial edit. Successful batches commit once and have one undo step.
|
||||
let mut fresh = Vec::new();
|
||||
for (aid, path) in todo {
|
||||
relink(s, &json!({"asset": aid.0, "path": path.to_string_lossy()}))?;
|
||||
n += 1;
|
||||
let a = d.assets.get(&aid).ok_or_else(|| bad("links.relinkFolder", "no such graphic"))?;
|
||||
fresh.push((aid, relinked_asset(a, &path, &path.to_string_lossy())?));
|
||||
}
|
||||
let n = fresh.len();
|
||||
if n > 0 {
|
||||
s.edit(|d, _| {
|
||||
for (aid, a) in fresh {
|
||||
d.assets.insert(aid, a);
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
Ok(json!({"relinked": n, "notFound": not_found}))
|
||||
}
|
||||
|
||||
fn update(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let d = s.doc()?.doc.clone();
|
||||
let (d, document) = {
|
||||
let st = s.doc()?;
|
||||
(st.doc.clone(), st.path.clone())
|
||||
};
|
||||
let document = document.as_deref().map(Path::new);
|
||||
let checks = &mut s.text_link_checks;
|
||||
let (only_asset, only_story) = (p.get("asset").and_then(Value::as_u64), story_param(p));
|
||||
let all = only_asset.is_none() && only_story.is_none();
|
||||
let which: Vec<(AssetId, String)> = d
|
||||
.assets
|
||||
.values()
|
||||
.filter(|a| all || only_asset == Some(a.id.0))
|
||||
.filter(|a| status(a) == "modified")
|
||||
.filter_map(|a| a.link.clone().map(|l| (a.id, l)))
|
||||
.collect();
|
||||
let texts: Vec<(StoryId, String)> = d
|
||||
// A link craft-docpath refuses is missing, so it is never read or refreshed.
|
||||
let resolved = |link: &str| crate::io::document_path(link, document).ok();
|
||||
let texts: Vec<(StoryId, String, std::path::PathBuf)> = d
|
||||
.stories
|
||||
.values()
|
||||
.filter(|st| all || only_story == Some(st.id))
|
||||
.filter_map(|st| st.file_link.as_ref().filter(|l| text_status(l, checks) == "modified").map(|l| (st.id, l.path.clone())))
|
||||
.filter_map(|st| {
|
||||
let l = st.file_link.as_ref().filter(|l| text_status(l, checks, document) == "modified")?;
|
||||
Some((st.id, l.path.clone(), resolved(&l.path)?))
|
||||
})
|
||||
.collect();
|
||||
let mut fresh = Vec::new();
|
||||
for (aid, path) in which {
|
||||
fresh.push((aid, reload(&path)?));
|
||||
for a in d.assets.values().filter(|a| all || only_asset == Some(a.id.0)).filter(|a| status(a, document) == "modified") {
|
||||
// The bytes come from where the stored link resolves; the link itself stays as the
|
||||
// document stored it, so refreshing never rewrites a relative link to an absolute one.
|
||||
if let Some((link, at)) = a.link.as_deref().and_then(|l| resolved(l).map(|at| (l, at))) {
|
||||
fresh.push((a.id, relinked_asset(a, &at, link)?));
|
||||
}
|
||||
}
|
||||
let mut fresh_texts = Vec::new();
|
||||
for (sid, path) in texts {
|
||||
let (bytes, before) = read_text(&path)?;
|
||||
for (sid, path, at) in texts {
|
||||
let (bytes, before) = read_text(&at)?;
|
||||
fresh_texts.push((sid, path, bytes, before));
|
||||
}
|
||||
let n = fresh.len() + fresh_texts.len();
|
||||
@@ -428,13 +552,8 @@ fn update(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Ok(json!({"updated": 0}));
|
||||
}
|
||||
s.edit(|d, _| {
|
||||
for (aid, (bytes, px)) in &fresh {
|
||||
if let Some(a) = d.assets.get_mut(aid) {
|
||||
let a = Arc::make_mut(a);
|
||||
a.data = Arc::new(bytes.clone());
|
||||
a.pixels = Some(*px);
|
||||
super::file::crop_pdf(a);
|
||||
}
|
||||
for (aid, a) in fresh {
|
||||
d.assets.insert(aid, a);
|
||||
}
|
||||
let mut warnings = Vec::new();
|
||||
for (sid, path, bytes, before) in &fresh_texts {
|
||||
@@ -452,6 +571,315 @@ mod tests {
|
||||
designcraft_render::Rendered { width: w, height: h, pixels: vec![200u8; (w * h * 4) as usize] }.to_png()
|
||||
}
|
||||
|
||||
fn pdf(sizes: &[(u32, u32)]) -> Vec<u8> {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": sizes.len(), "facingPages": false})).unwrap();
|
||||
for (i, (width, height)) in sizes.iter().enumerate() {
|
||||
s.execute("layout.pageSize", &json!({"pages": [i + 1], "width": width, "height": height})).unwrap();
|
||||
}
|
||||
let result = s.execute("file.exportPdf", &json!({})).unwrap();
|
||||
super::super::file::base64_decode(result["base64"].as_str().unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anchored_image_lists_its_page_and_resolution() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 2, "facingPages": false})).unwrap();
|
||||
let r = s
|
||||
.execute("frame.create", &json!({"spread": 1, "rect": [72, 72, 400, 400], "content": "text", "text": "Before after and more words."}))
|
||||
.unwrap();
|
||||
let sid = s.doc().unwrap().doc.item(ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story;
|
||||
let placed = s
|
||||
.execute(
|
||||
"file.place",
|
||||
&json!({"base64": super::super::file::base64_encode(&png(10, 10)), "name": "dot.png", "x": 450, "y": 450, "width": 36}),
|
||||
)
|
||||
.unwrap();
|
||||
let id = placed["id"].as_u64().unwrap();
|
||||
let before = s.execute("links.list", &json!({})).unwrap();
|
||||
assert_eq!(before[0]["uses"][0]["ppi"], 20.0);
|
||||
s.execute("anchored.insert", &json!({"ids": [id], "story": sid.0, "pos": 7})).unwrap();
|
||||
let list = s.execute("links.list", &json!({})).unwrap();
|
||||
let uses = list[0]["uses"].as_array().unwrap();
|
||||
assert_eq!(uses.len(), 1, "anchored image must have one use: {list}");
|
||||
assert_eq!(uses[0]["page"], "2");
|
||||
assert_eq!(uses[0]["ppi"], 20.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_web_exports_convert_tiff_and_skip_bad_used_assets() {
|
||||
use std::io::Read;
|
||||
let bytes = super::super::file::eps_place_tests::tiff_eps();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let placed = s.execute("file.place", &json!({"base64":super::super::file::base64_encode(&bytes),"name":"logo.eps"})).unwrap();
|
||||
let aid = AssetId(placed["asset"].as_u64().unwrap());
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let html = s.execute("file.exportHtml", &json!({})).unwrap();
|
||||
let encoded = html["text"].as_str().unwrap().split("data:image/png;base64,").nth(1).unwrap().split('"').next().unwrap();
|
||||
let png = super::super::file::base64_decode(encoded);
|
||||
assert!(png.starts_with(b"\x89PNG"));
|
||||
assert_eq!(designcraft_render::image_size(&png), Some((2, 2)));
|
||||
assert_eq!(designcraft_render::decode_pixmap(&png).unwrap().data()[0], designcraft_render::decode_pixmap(&bytes).unwrap().data()[0]);
|
||||
let result = s.execute("file.exportEpub", &json!({})).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(super::super::file::base64_decode(result["base64"].as_str().unwrap()))).unwrap();
|
||||
let mut image = Vec::new();
|
||||
z.by_name(&format!("OEBPS/images/logo-{}.png", aid.0)).unwrap().read_to_end(&mut image).unwrap();
|
||||
assert_eq!(image, png);
|
||||
let mut opf = String::new();
|
||||
z.by_name("OEBPS/content.opf").unwrap().read_to_string(&mut opf).unwrap();
|
||||
assert!(opf.contains("media-type=\"image/png\""));
|
||||
assert!(!opf.contains("image/tiff"));
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
s.edit(|d, _| {
|
||||
Arc::make_mut(d.assets.get_mut(&aid).unwrap()).data = Arc::new(b"%!PS-Adobe-3.0 EPSF-3.0\n%%EOF\n".to_vec());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
for command in ["file.exportHtml", "file.exportEpub"] {
|
||||
let result = s.execute(command, &json!({})).unwrap();
|
||||
assert_eq!(result["warnings"].as_array().unwrap().len(), 1, "{command}");
|
||||
if command == "file.exportHtml" {
|
||||
assert!(!result["text"].as_str().unwrap().contains("<img "));
|
||||
}
|
||||
}
|
||||
assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));
|
||||
s.execute("edit.clear", &json!({})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.assets.contains_key(&aid));
|
||||
for command in ["file.exportHtml", "file.exportEpub"] {
|
||||
let result = s.execute(command, &json!({})).unwrap();
|
||||
assert!(result["warnings"].as_array().unwrap().is_empty(), "unused EPS: {command}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_status_tracks_source_bytes_across_save_update_relink_and_embed() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-eps-status-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("logo.eps");
|
||||
let first = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n0 0 0 setrgbcolor\n%%EOF\n";
|
||||
let changed = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n1 0 0 setrgbcolor\n%%EOF\n";
|
||||
assert_eq!(first.len(), changed.len());
|
||||
assert_eq!(designcraft_images::eps_proxy(first), designcraft_images::eps_proxy(changed), "same preview, different source");
|
||||
std::fs::write(&path, first).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let aid = AssetId(s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap()["asset"].as_u64().unwrap());
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
assert_eq!(s.execute("links.update", &json!({})).unwrap()["updated"], 0);
|
||||
assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &before), "unchanged EPS must not create an edit");
|
||||
let saved = designcraft_format::save(&before).unwrap();
|
||||
let reopened = designcraft_format::load(&saved).unwrap();
|
||||
assert_eq!(reopened.assets[&aid].data.as_slice(), first);
|
||||
let mut s = Session::new();
|
||||
s.add_document(crate::DocState::new(reopened, None));
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
std::fs::write(&path, changed).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "modified");
|
||||
assert_eq!(s.execute("links.update", &json!({})).unwrap()["updated"], 1);
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].data.as_slice(), changed);
|
||||
std::fs::remove_file(&path).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "missing");
|
||||
let replacement = dir.join("replacement.eps");
|
||||
std::fs::write(&replacement, changed).unwrap();
|
||||
s.execute("links.relink", &json!({"asset": aid.0, "path": replacement.to_string_lossy()})).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "missing");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
s.execute("links.embed", &json!({"asset": aid.0})).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "embedded");
|
||||
std::fs::remove_file(&replacement).unwrap();
|
||||
let embedded = designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
// The asset is embedded, so no link is followed and the document is irrelevant.
|
||||
assert_eq!(status(&embedded.assets[&aid], None), "embedded");
|
||||
assert_eq!(embedded.assets[&aid].data.as_slice(), changed);
|
||||
assert!(designcraft_render::decode_pixmap(&embedded.assets[&aid].data).is_some());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_legacy_preview_document_is_upgraded_by_update() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-eps-legacy-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("logo.eps");
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n%%EOF\n";
|
||||
std::fs::write(&path, source).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let aid = AssetId(s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap()["asset"].as_u64().unwrap());
|
||||
let proxy = designcraft_images::eps_proxy(source).unwrap().0;
|
||||
s.edit(|d, _| {
|
||||
let a = Arc::make_mut(d.assets.get_mut(&aid).unwrap());
|
||||
a.data = Arc::new(proxy);
|
||||
a.mime = "image/png".into();
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let old = designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.add_document(crate::DocState::new(old, None));
|
||||
assert_eq!(s.execute("links.update", &json!({})).unwrap()["updated"], 1);
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].data.as_slice(), source);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(designcraft_render::decode_pixmap(&s.doc().unwrap().doc.assets[&aid].data).is_some(), "old preview remains renderable");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_source_exports_preview_to_pdf_html_and_epub_without_editing_document() {
|
||||
use hayro_syntax::object::Name;
|
||||
use std::io::Read;
|
||||
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n%%EOF\n";
|
||||
let proxy = designcraft_images::eps_proxy(source).unwrap().0;
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let aid = AssetId(
|
||||
s.execute("file.place", &json!({"base64": super::super::file::base64_encode(source), "name": "logo.eps"})).unwrap()["asset"]
|
||||
.as_u64()
|
||||
.unwrap(),
|
||||
);
|
||||
// A document retaining the original EPS rather than the legacy preview-only asset.
|
||||
s.edit(|d, _| {
|
||||
let a = Arc::make_mut(d.assets.get_mut(&aid).unwrap());
|
||||
a.data = Arc::new(source.to_vec());
|
||||
a.mime = "application/postscript".into();
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let result = s.execute("file.exportPdf", &json!({})).unwrap();
|
||||
assert!(result["warnings"].as_array().unwrap().is_empty(), "{:?}", result["warnings"]);
|
||||
let pdf = hayro_syntax::Pdf::new(super::super::file::base64_decode(result["base64"].as_str().unwrap())).unwrap();
|
||||
assert_eq!(
|
||||
pdf.objects()
|
||||
.into_iter()
|
||||
.filter_map(|o| o.into_stream())
|
||||
.filter(|st| st.dict().get::<Name>("Subtype").is_some_and(|n| n.as_str() == "Image"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
let result = s.execute("file.exportHtml", &json!({})).unwrap();
|
||||
let encoded = result["text"].as_str().unwrap().split("data:image/png;base64,").nth(1).unwrap().split('"').next().unwrap();
|
||||
assert_eq!(super::super::file::base64_decode(encoded), proxy);
|
||||
let result = s.execute("file.exportEpub", &json!({})).unwrap();
|
||||
let bytes = super::super::file::base64_decode(result["base64"].as_str().unwrap());
|
||||
let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
|
||||
let mut image = Vec::new();
|
||||
archive.by_name(&format!("OEBPS/images/logo-{}.png", aid.0)).unwrap().read_to_end(&mut image).unwrap();
|
||||
assert_eq!(image, proxy);
|
||||
let mut opf = String::new();
|
||||
archive.by_name("OEBPS/content.opf").unwrap().read_to_string(&mut opf).unwrap();
|
||||
assert!(opf.contains("media-type=\"image/png\""));
|
||||
assert!(!opf.contains("application/postscript"));
|
||||
assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &before), "exports must leave the source document intact");
|
||||
assert_eq!(before.assets[&aid].data.as_slice(), source);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_relink_update_and_folder_relink_retain_source() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-links-eps-{}", std::process::id()));
|
||||
std::fs::create_dir_all(dir.join("new")).unwrap();
|
||||
let eps = |w: u32| format!("%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 {w} 10\n%%EOF\n");
|
||||
let original = dir.join("original.eps");
|
||||
let replacement = dir.join("replacement.eps");
|
||||
std::fs::write(&original, eps(10)).unwrap();
|
||||
std::fs::write(&replacement, eps(30)).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("file.place", &json!({"path": original.to_string_lossy()})).unwrap();
|
||||
let aid = s.doc().unwrap().doc.assets.keys().next().copied().unwrap();
|
||||
std::fs::write(&original, eps(20)).unwrap();
|
||||
assert_eq!(s.execute("links.update", &json!({})).unwrap()["updated"], 1);
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].pixels, Some((40, 20)));
|
||||
s.execute("links.relink", &json!({"asset": aid.0, "path": replacement.to_string_lossy()})).unwrap();
|
||||
let a = &s.doc().unwrap().doc.assets[&aid];
|
||||
assert_eq!(a.pixels, Some((60, 20)));
|
||||
assert_eq!(a.mime, "application/postscript");
|
||||
assert_eq!(a.data.as_slice(), eps(30).as_bytes());
|
||||
let moved = dir.join("new/replacement.eps");
|
||||
std::fs::rename(&replacement, &moved).unwrap();
|
||||
assert_eq!(s.execute("links.relinkFolder", &json!({"dir": dir.join("new").to_string_lossy()})).unwrap()["relinked"], 1);
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].link.as_deref(), moved.to_str());
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "ok");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_relink_and_update_preserve_selected_page_size() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-links-pdf-size-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let original = dir.join("original.pdf");
|
||||
let replacement = dir.join("replacement.pdf");
|
||||
std::fs::write(&original, pdf(&[(10, 10), (20, 30)])).unwrap();
|
||||
std::fs::write(&replacement, pdf(&[(40, 40), (50, 60)])).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("file.place", &json!({"path": original.to_string_lossy(), "pdfPage": 2})).unwrap();
|
||||
let aid = s.doc().unwrap().doc.assets.keys().next().copied().unwrap();
|
||||
s.execute("links.relink", &json!({"asset": aid.0, "path": replacement.to_string_lossy()})).unwrap();
|
||||
let a = &s.doc().unwrap().doc.assets[&aid];
|
||||
assert_eq!(a.page, 1);
|
||||
assert_eq!(a.pixels, Some((50, 60)));
|
||||
assert!(designcraft_render::render_pdf_page(&a.data, a.page as usize, 64).is_some());
|
||||
std::fs::write(&replacement, pdf(&[(70, 70), (80, 90)])).unwrap();
|
||||
assert_eq!(s.execute("links.update", &json!({})).unwrap()["updated"], 1);
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].pixels, Some((80, 90)));
|
||||
let raster = dir.join("replacement.png");
|
||||
std::fs::write(&raster, png(4, 5)).unwrap();
|
||||
s.execute("links.relink", &json!({"asset": aid.0, "path": raster.to_string_lossy()})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.assets[&aid].page, 0);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_reload_rejects_missing_page_without_edit() {
|
||||
let full = pdf(&[(10, 10), (20, 30)]);
|
||||
let short = pdf(&[(10, 10)]);
|
||||
let dir = std::env::temp_dir().join(format!("dc-links-pdf-page-{}", std::process::id()));
|
||||
std::fs::create_dir_all(dir.join("new")).unwrap();
|
||||
let original = dir.join("book.pdf");
|
||||
let replacement = dir.join("new/book.pdf");
|
||||
std::fs::write(&replacement, short.clone()).unwrap();
|
||||
for command in ["links.relink", "links.relinkFolder", "links.update"] {
|
||||
std::fs::write(&original, &full).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("file.place", &json!({"path": original.to_string_lossy(), "pdfPage": 2})).unwrap();
|
||||
let aid = s.doc().unwrap().doc.assets.keys().next().copied().unwrap();
|
||||
if command == "links.update" {
|
||||
std::fs::write(&original, &short).unwrap();
|
||||
}
|
||||
let st = s.doc().unwrap();
|
||||
let before = st.doc.clone();
|
||||
let revision = st.revision;
|
||||
let undo = st.history.undo.len();
|
||||
let journal = s.journal.len();
|
||||
let params = match command {
|
||||
"links.relinkFolder" => json!({"dir": dir.join("new").to_string_lossy(), "missingOnly": false}),
|
||||
"links.update" => json!({"asset": aid.0}),
|
||||
_ => json!({"asset": aid.0, "path": replacement.to_string_lossy()}),
|
||||
};
|
||||
let err = s.execute(command, ¶ms).unwrap_err();
|
||||
assert!(err.to_string().contains("PDF page 2"), "{command}: {err}");
|
||||
let st = s.doc().unwrap();
|
||||
assert!(Arc::ptr_eq(&st.doc, &before), "{command}");
|
||||
assert_eq!(st.revision, revision);
|
||||
assert_eq!(st.history.undo.len(), undo);
|
||||
assert_eq!(s.journal.len(), journal);
|
||||
assert!(designcraft_render::render_pdf_page(&st.doc.assets[&aid].data, 1, 64).is_some());
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_status_relink_update_embed() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-links-{}", std::process::id()));
|
||||
@@ -491,10 +919,78 @@ mod tests {
|
||||
|
||||
#[cfg(test)]
|
||||
mod relink_folder_tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn two_missing_links(case: &str) -> (Session, std::path::PathBuf) {
|
||||
let dir = std::env::temp_dir().join(format!("dc-relink-{case}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(dir.join("new")).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
for name in ["a.svg", "b.svg"] {
|
||||
let path = dir.join(name);
|
||||
std::fs::write(&path, r#"<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"/>"#).unwrap();
|
||||
s.doc_mut().unwrap().selection = Default::default();
|
||||
s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
std::fs::rename(&path, dir.join("new").join(name)).unwrap();
|
||||
}
|
||||
(s, dir)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_relink_failure_keeps_document_and_history() {
|
||||
let (mut s, dir) = two_missing_links("atomic");
|
||||
std::fs::write(dir.join("new/b.svg"), b"not an image").unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
let before = st.doc.clone();
|
||||
let revision = st.revision;
|
||||
let undo = st.history.undo.len();
|
||||
let journal = s.journal.len();
|
||||
let err = s.execute("links.relinkFolder", &json!({"dir": dir.join("new").to_string_lossy()})).unwrap_err();
|
||||
assert!(err.to_string().contains("unsupported or corrupt image"), "{err}");
|
||||
let st = s.doc().unwrap();
|
||||
assert!(Arc::ptr_eq(&st.doc, &before), "a failed batch must not leave its first link changed");
|
||||
assert_eq!(st.revision, revision);
|
||||
assert_eq!(st.history.undo.len(), undo);
|
||||
assert_eq!(s.journal.len(), journal);
|
||||
s.execute("frame.create", &json!({"rect": [0, 0, 20, 20]})).unwrap();
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_relink_is_one_undo_step() {
|
||||
let (mut s, dir) = two_missing_links("undo");
|
||||
let st = s.doc().unwrap();
|
||||
let before = st.doc.clone();
|
||||
let revision = st.revision;
|
||||
let undo = st.history.undo.len();
|
||||
let r = s.execute("links.relinkFolder", &json!({"dir": dir.join("new").to_string_lossy()})).unwrap();
|
||||
assert_eq!(r["relinked"], 2);
|
||||
let st = s.doc().unwrap();
|
||||
// `revision` is drawn from a process-wide counter, so a test running beside this one
|
||||
// can take the next value: that the batch commits *once* is what the single undo step is.
|
||||
assert_ne!(st.revision, revision, "the batch commits");
|
||||
assert_eq!(st.history.undo.len(), undo + 1, "exactly one undo step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &before));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relink_extension_rejects_path_separators_without_panicking() {
|
||||
let (mut s, dir) = two_missing_links("extension");
|
||||
for extension in ["png/other", "png\\other", "png\0other"] {
|
||||
// Call directly so the command guard cannot hide a panic in Path::with_extension.
|
||||
let err = super::relink_folder(&mut s, &json!({"extension": extension})).unwrap_err();
|
||||
assert!(matches!(err, crate::EngineError::BadParams { .. }), "{err}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relink_missing_links_to_a_folder_and_by_extension() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-relink-{}", std::process::id()));
|
||||
@@ -834,7 +1330,7 @@ mod text_link_tests {
|
||||
// Saved and opened again: the link and its options are kept.
|
||||
let back = designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
assert_eq!(back.story(sid).unwrap().file_link, story(&s, sid).file_link);
|
||||
assert_eq!(super::text_status(back.story(sid).unwrap().file_link.as_ref().unwrap(), &mut Default::default()), "ok");
|
||||
assert_eq!(super::text_status(back.story(sid).unwrap().file_link.as_ref().unwrap(), &mut Default::default(), None), "ok");
|
||||
|
||||
// The file moves: Missing; Relink to it imports it into the story.
|
||||
let moved = dir.join("moved").join("chapter.docx");
|
||||
@@ -973,7 +1469,7 @@ mod text_link_read_tests {
|
||||
|
||||
fn status_of(path: &std::path::Path, size: u64) -> &'static str {
|
||||
let l = TextFileLink { path: path.to_string_lossy().to_string(), size, hash: "0".into(), ..Default::default() };
|
||||
promptly(move || text_status(&l, &mut Default::default()))
|
||||
promptly(move || text_status(&l, &mut Default::default(), None))
|
||||
}
|
||||
|
||||
fn read(path: &std::path::Path) -> Result<usize, String> {
|
||||
|
||||
@@ -61,7 +61,9 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(id) = super::id_param(p, "id") {
|
||||
return s.edit(|d, _| {
|
||||
let old = d.item(id).ok_or_else(|| bad("math.insert", "no such object"))?;
|
||||
let at = old.bounds();
|
||||
// The path and graphic live in the item's inner space, which `xf` maps to the spread:
|
||||
// build the new ones at the old inner corner and keep `xf`.
|
||||
let at = old.path.bounds().ok_or_else(|| bad("math.insert", "equation has no geometry"))?;
|
||||
let it = math_item(d, &latex, size, lid, (at.x0, at.y0));
|
||||
let target = d.item_mut(id).ok_or_else(|| bad("math.insert", "no such object"))?;
|
||||
target.path = it.path;
|
||||
@@ -129,4 +131,46 @@ mod tests {
|
||||
let st = &s.doc().unwrap().doc.stories[&designcraft_doc::StoryId(f["story"].as_u64().unwrap())];
|
||||
assert_eq!(st.objects.len(), 1);
|
||||
}
|
||||
|
||||
/// The item's top-left corner in spread space (inner path corner through its transform).
|
||||
fn origin(s: &Session, id: designcraft_doc::ItemId) -> designcraft_geom::Point {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let it = d.item(id).unwrap();
|
||||
let r = it.path.bounds().unwrap();
|
||||
it.xf * designcraft_geom::Point::new(r.x0, r.y0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editing_a_transformed_equation_keeps_its_place() {
|
||||
for step in ["move", "rotate", "scale"] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("math.insert", &json!({"latex": r"\frac{a}{b}", "x": 100, "y": 100})).unwrap();
|
||||
let id = designcraft_doc::ItemId(r["id"].as_u64().unwrap());
|
||||
match step {
|
||||
"move" => s.execute("transform.move", &json!({"ids": [id.0], "dx": 50, "dy": 40})),
|
||||
"rotate" => s.execute("transform.rotate", &json!({"ids": [id.0], "angle": 30})),
|
||||
_ => s.execute("transform.scale", &json!({"ids": [id.0], "sx": 2.0, "sy": 1.5})),
|
||||
}
|
||||
.unwrap();
|
||||
let before = origin(&s, id);
|
||||
s.execute("math.insert", &json!({"id": id.0, "latex": r"\frac{a}{b}"})).unwrap();
|
||||
let after = origin(&s, id);
|
||||
assert!((before.x - after.x).abs() < 1e-6 && (before.y - after.y).abs() < 1e-6, "{step}: {before:?} -> {after:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editing_a_moved_equation_keeps_its_bounds() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("math.insert", &json!({"latex": "x", "x": 100, "y": 100})).unwrap();
|
||||
let id = designcraft_doc::ItemId(r["id"].as_u64().unwrap());
|
||||
s.execute("transform.move", &json!({"ids": [id.0], "dx": 50, "dy": 40})).unwrap();
|
||||
let before = s.doc().unwrap().doc.item(id).unwrap().bounds();
|
||||
s.execute("math.insert", &json!({"id": id.0, "latex": "x"})).unwrap();
|
||||
let after = s.doc().unwrap().doc.item(id).unwrap().bounds();
|
||||
assert_eq!((before.x0, before.y0), (after.x0, after.y0));
|
||||
assert_eq!((after.x0, after.y0), (150.0, 140.0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,9 +259,14 @@ pub(crate) fn str_param<'a>(p: &'a Value, key: &str) -> Option<&'a str> {
|
||||
}
|
||||
/// Plain text from a parameter, with CR LF and lone CR (classic Mac and InDesign line ends) as
|
||||
/// `\n`, the paragraph separator, as edit.paste and text import already do. Kept as they were,
|
||||
/// the CRs joined the paragraphs into one.
|
||||
/// the CRs joined the paragraphs into one. A vertical tab (Word's and PowerPoint's Shift+Enter,
|
||||
/// Chr(11)) is a forced line break: kept, it was drawn as a missing-glyph box.
|
||||
pub(crate) fn text_param(p: &Value, key: &str) -> String {
|
||||
str_param(p, key).unwrap_or("").replace("\r\n", "\n").replace('\r', "\n")
|
||||
str_param(p, key)
|
||||
.unwrap_or("")
|
||||
.replace("\r\n", "\n")
|
||||
.replace('\r', "\n")
|
||||
.replace('\u{B}', designcraft_doc::story::FORCED_LINE_BREAK.encode_utf8(&mut [0; 4]))
|
||||
}
|
||||
pub(crate) fn id_param(p: &Value, key: &str) -> Option<ItemId> {
|
||||
p.get(key).and_then(Value::as_u64).map(ItemId)
|
||||
|
||||
+114
-10
@@ -259,9 +259,28 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Stroke",
|
||||
[],
|
||||
None,
|
||||
"{swatch?, tint?, weight?, align?: center|inside|outside, type?: {kind:…}, cap?: butt|round|projecting, join?: miter|round|bevel, miterLimit?, start?, end?: none|simple|simpleWide|triangle|triangleWide|barbed|curved|circle|circleSolid|square|squareSolid|bar, gapSwatch?, gapTint?, gapOverprint?: bool, ids?}",
|
||||
"{swatch?, tint?, weight?, align?: center|inside|outside, type?: {kind: solid|dashed|dotted|thickThin|thinThick|thinThin|thickThick|thinThickThin|thickThinThick|wavy|hashed|stripes|style, pattern?: [dash, gap, …] (dashed: required, points), bands?: [[start, width], …] (stripes: required, fractions of stroke weight), name?: string (style: required, document stroke style name)} (other kinds take no extra fields; unknown type fields are errors), cap?: butt|round|projecting, join?: miter|round|bevel, miterLimit?, start?, end?: none|simple|simpleWide|triangle|triangleWide|barbed|curved|circle|circleSolid|square|squareSolid|bar, gapSwatch?, gapTint?, gapOverprint?: bool, ids?}",
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let kind: Option<designcraft_doc::StrokeType> = p
|
||||
.get("type")
|
||||
.map(|v| {
|
||||
if let Some(fields) = v.as_object() {
|
||||
let field = match v.get("kind").and_then(Value::as_str) {
|
||||
Some("dashed") => Some("pattern"),
|
||||
Some("stripes") => Some("bands"),
|
||||
Some("style") => Some("name"),
|
||||
_ => None,
|
||||
};
|
||||
for key in fields.keys() {
|
||||
if key != "kind" && Some(key.as_str()) != field {
|
||||
return Err(bad("object.stroke", format!("type: unknown field `{key}`")));
|
||||
}
|
||||
}
|
||||
}
|
||||
serde_json::from_value(v.clone()).map_err(|e| bad("object.stroke", format!("type: {e}")))
|
||||
})
|
||||
.transpose()?;
|
||||
for key in ["swatch", "gapSwatch"] {
|
||||
if let Some(name) = p.get(key).and_then(Value::as_str) {
|
||||
known_swatch(&s.doc()?.doc, name, "object.stroke", key)?;
|
||||
@@ -297,7 +316,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
st.swatch = v.into();
|
||||
}
|
||||
if let Some(v) = p2.get("tint").and_then(Value::as_f64) {
|
||||
st.tint = v as f32;
|
||||
st.tint = v.clamp(0.0, 1.0) as f32;
|
||||
}
|
||||
if let Some(v) = p2.get("weight").and_then(Value::as_f64) {
|
||||
st.weight = v.max(0.0);
|
||||
@@ -309,16 +328,18 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
st.gap_swatch = v.into();
|
||||
}
|
||||
if let Some(v) = p2.get("gapTint").and_then(Value::as_f64) {
|
||||
st.gap_tint = v as f32;
|
||||
st.gap_tint = v.clamp(0.0, 1.0) as f32;
|
||||
}
|
||||
if let Some(v) = p2.get("gapOverprint").and_then(Value::as_bool) {
|
||||
st.gap_overprint = v;
|
||||
}
|
||||
for (k, apply) in [("align", 0), ("type", 1), ("cap", 2), ("join", 3), ("start", 4), ("end", 5)] {
|
||||
if let Some(kind) = &kind {
|
||||
st.kind = kind.clone();
|
||||
}
|
||||
for (k, apply) in [("align", 0), ("cap", 2), ("join", 3), ("start", 4), ("end", 5)] {
|
||||
if let Some(v) = p2.get(k).cloned() {
|
||||
let _ = match apply {
|
||||
0 => serde_json::from_value(v).map(|x| st.align = x),
|
||||
1 => serde_json::from_value(v).map(|x| st.kind = x),
|
||||
2 => serde_json::from_value(v).map(|x| st.cap = x),
|
||||
3 => serde_json::from_value(v).map(|x| st.join = x),
|
||||
4 => serde_json::from_value(v).map(|x| st.start = x),
|
||||
@@ -1201,11 +1222,10 @@ fn transform_resize(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let to = rect_param(p, "to").ok_or_else(|| bad("transform.resize", "missing to"))?;
|
||||
let scale_content = bool_or(p, "content", false);
|
||||
let distribute = bool_or(p, "distribute", false);
|
||||
if from.width().abs() < 1e-9 || from.height().abs() < 1e-9 {
|
||||
return Err(bad("transform.resize", "degenerate source rect"));
|
||||
}
|
||||
let sx = to.width() / from.width();
|
||||
let sy = to.height() / from.height();
|
||||
// A flat source (a horizontal or vertical line) has no size to scale on its flat axis: that axis only moves.
|
||||
let ratio = |new: f64, old: f64| if old.abs() < 1e-9 { 1.0 } else { new / old };
|
||||
let sx = ratio(to.width(), from.width());
|
||||
let sy = ratio(to.height(), from.height());
|
||||
let sx = if sx.abs() < 1e-3 { 1e-3f64.copysign(sx) } else { sx };
|
||||
let sy = if sy.abs() < 1e-3 { 1e-3f64.copysign(sy) } else { sy };
|
||||
let m = Affine::translate((to.x0, to.y0)) * Affine::scale_non_uniform(sx, sy) * Affine::translate((-from.x0, -from.y0));
|
||||
@@ -4189,6 +4209,16 @@ mod fitting_tests {
|
||||
assert!(String::from_utf8_lossy(&bytes).contains("/Subtype/Image"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_flat_line_resizes_along_its_length() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("line.create", &json!({"a": [100, 100], "b": [200, 100]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("transform.resize", &json!({"ids": [id], "from": [100, 100, 200, 100], "to": [100, 100, 300, 100]})).unwrap();
|
||||
let r = s.doc().unwrap().doc.item(ItemId(id)).unwrap().bounds();
|
||||
assert!((r.x0 - 100.0).abs() < 1e-9 && (r.x1 - 300.0).abs() < 1e-9 && (r.y0 - 100.0).abs() < 1e-9 && r.height().abs() < 1e-9, "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_distribute_spreads_objects_without_resizing() {
|
||||
let mut s = Session::new();
|
||||
@@ -4369,6 +4399,22 @@ mod line_end_tests {
|
||||
s.execute("note.edit", &json!({"story": n["story"], "id": n["id"], "text": "Three\r\nFour"})).unwrap();
|
||||
assert_eq!(note(&mut s), "Three\nFour");
|
||||
}
|
||||
|
||||
/// "One\u{B}Two" kept the vertical tab, drawn as a missing-glyph box, where Word and
|
||||
/// PowerPoint text (Chr(11)) means a forced line break.
|
||||
#[test]
|
||||
fn vertical_tabs_are_forced_line_breaks() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [36, 36, 300, 300], "content": "text", "text": "One\u{B}Two"})).unwrap();
|
||||
let sid = designcraft_doc::StoryId(r["story"].as_u64().unwrap());
|
||||
let story = |s: &Session| s.doc().unwrap().doc.story(sid).unwrap().clone();
|
||||
assert_eq!((story(&s).text.as_str(), story(&s).paras.len()), ("One\u{2028}Two", 1));
|
||||
s.execute("text.insert", &json!({"text": "\u{B}Three", "raw": true})).unwrap();
|
||||
assert_eq!((story(&s).text.as_str(), story(&s).paras.len()), ("One\u{2028}Two\u{2028}Three", 1));
|
||||
s.execute("story.setText", &json!({"story": sid.0, "text": "A\u{B}B\rC"})).unwrap();
|
||||
assert_eq!((story(&s).text.as_str(), story(&s).paras.len()), ("A\u{2028}B\nC", 2));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -4799,6 +4845,64 @@ mod group_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod stroke_type_tests {
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn malformed_stroke_types_return_bad_params_without_editing() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [0, 0, 100, 100]})).unwrap()["id"].clone();
|
||||
for (kind, hint) in [
|
||||
(json!({"kind": "dashed", "dash": [8, 2.5]}), "dash"),
|
||||
(json!({"kind": "zigzag"}), "zigzag"),
|
||||
(json!({"kind": "solid", "dash": [8, 2.5]}), "dash"),
|
||||
(json!({"kind": "dashed", "pattern": [8, 2.5], "dash": [1, 2]}), "dash"),
|
||||
(json!({"kind": "dashed"}), "pattern"),
|
||||
(json!({"kind": "dashed", "pattern": "bad"}), "type"),
|
||||
(json!({"kind": "stripes", "bands": [[0, "bad"]]}), "type"),
|
||||
(json!({"kind": "style"}), "name"),
|
||||
(json!({}), "kind"),
|
||||
(json!("solid"), "type"),
|
||||
(json!(null), "type"),
|
||||
] {
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let error = s.execute("object.stroke", &json!({"ids": [id], "weight": 5, "type": kind})).unwrap_err();
|
||||
assert!(matches!(error, crate::EngineError::BadParams { .. }), "{error}");
|
||||
assert!(error.to_string().contains(hint), "{kind}: {error}");
|
||||
assert!(std::sync::Arc::ptr_eq(&before, &s.doc().unwrap().doc), "{kind}: nothing changed");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stroke_types_apply_all_supported_variants() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [0, 0, 100, 100]})).unwrap()["id"].as_u64().unwrap();
|
||||
for kind in [
|
||||
json!({"kind": "solid"}),
|
||||
json!({"kind": "dashed", "pattern": [8, 2.5]}),
|
||||
json!({"kind": "dotted"}),
|
||||
json!({"kind": "thickThin"}),
|
||||
json!({"kind": "thinThick"}),
|
||||
json!({"kind": "thinThin"}),
|
||||
json!({"kind": "thickThick"}),
|
||||
json!({"kind": "thinThickThin"}),
|
||||
json!({"kind": "thickThinThick"}),
|
||||
json!({"kind": "wavy"}),
|
||||
json!({"kind": "hashed"}),
|
||||
json!({"kind": "stripes", "bands": [[0, 0.3], [0.7, 0.3]]}),
|
||||
json!({"kind": "style", "name": "Rails"}),
|
||||
] {
|
||||
let result = s.execute("object.stroke", &json!({"ids": [id], "type": kind})).unwrap();
|
||||
assert_eq!(result["changed"], 1);
|
||||
let item = s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap();
|
||||
assert_eq!(item.stroke.kind, serde_json::from_value::<designcraft_doc::StrokeType>(kind).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod swatch_name_tests {
|
||||
use serde_json::json;
|
||||
|
||||
@@ -18,6 +18,9 @@ use serde_json::{Value, json};
|
||||
|
||||
const MAX_OUTPUT_COMPONENT_BYTES: usize = 200;
|
||||
|
||||
/// The folder of a package that holds the files its document places.
|
||||
const LINKS_FOLDER: &str = "Links";
|
||||
|
||||
fn path_error(path: &Path, message: impl std::fmt::Display) -> EngineError {
|
||||
EngineError::Other(format!("{}: {message}", path.display()))
|
||||
}
|
||||
@@ -282,7 +285,10 @@ fn unique_name(base: String, taken: &mut Vec<String>) -> String {
|
||||
}
|
||||
|
||||
/// Write each asset's bytes into `dir`; returns the relinked document and the paths written.
|
||||
fn write_links(d: &Document, root: &OutputRoot, dir: &Path, only: Option<&[AssetId]>) -> Result<(Document, Vec<PathBuf>)> {
|
||||
/// `relative_to` names the folder `dir` is, beside the saved document: the links then read
|
||||
/// `Links/logo.png`, so a package holds no path from the machine that made it (#340). `None` links
|
||||
/// the copies by their full paths (Links › Copy Link(s) To, which relinks the open document).
|
||||
fn write_links(d: &Document, root: &OutputRoot, dir: &Path, only: Option<&[AssetId]>, relative_to: Option<&str>) -> Result<(Document, Vec<PathBuf>)> {
|
||||
root.validate_directory(dir)?;
|
||||
let mut out = d.clone();
|
||||
let mut taken = Vec::new();
|
||||
@@ -303,7 +309,10 @@ fn write_links(d: &Document, root: &OutputRoot, dir: &Path, only: Option<&[Asset
|
||||
let path = dir.join(&name);
|
||||
root.write(&path, a.data.as_slice())?;
|
||||
let mut na = (**a).clone();
|
||||
na.link = Some(path.to_string_lossy().to_string());
|
||||
na.link = Some(match relative_to {
|
||||
Some(folder) => format!("{folder}/{name}"),
|
||||
None => path.to_string_lossy().to_string(),
|
||||
});
|
||||
out.assets.insert(id, Arc::new(na));
|
||||
written.push(path);
|
||||
}
|
||||
@@ -387,8 +396,8 @@ fn package(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let links = s.execute("links.list", &json!({}))?;
|
||||
let pre = s.execute("preflight.run", &json!({})).unwrap_or(Value::Null);
|
||||
let d = s.doc()?.doc.clone();
|
||||
let links_dir = root.child("Links")?;
|
||||
let (packed, mut files) = write_links(&d, &root, &links_dir, None)?;
|
||||
let links_dir = root.child(LINKS_FOLDER)?;
|
||||
let (packed, mut files) = write_links(&d, &root, &links_dir, None, Some(LINKS_FOLDER))?;
|
||||
let doc_path = package_output_path(&dir, &title, "designcraft")?;
|
||||
root.write(&doc_path, &super::to_bytes(&packed)?)?;
|
||||
files.insert(0, doc_path);
|
||||
@@ -463,7 +472,7 @@ fn copy_links(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let only: Option<Vec<AssetId>> = p.get("assets").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).map(AssetId).collect());
|
||||
let d = s.doc()?.doc.clone();
|
||||
let root = OutputRoot::new(dir.clone())?;
|
||||
let (relinked, written) = write_links(&d, &root, &dir, only.as_deref())?;
|
||||
let (relinked, written) = write_links(&d, &root, &dir, only.as_deref(), None)?;
|
||||
s.edit(|doc, _| {
|
||||
doc.assets = relinked.assets;
|
||||
Ok(())
|
||||
@@ -520,14 +529,17 @@ mod tests {
|
||||
std::io::Read::read_to_string(&mut f, &mut spreads).unwrap();
|
||||
}
|
||||
}
|
||||
let linked = dir.join("Links").join("my photo.png");
|
||||
let uri = format!("file:{}", linked.to_string_lossy().replace(' ', "%20"));
|
||||
// Relative to the package: no path from the machine that made it (#340).
|
||||
let uri = "file:Links/my%20photo.png";
|
||||
let folder = dir.file_name().unwrap().to_string_lossy().into_owned();
|
||||
assert!(!spreads.contains(&folder), "the package's own folder is not written: {spreads}");
|
||||
assert!(spreads.contains(&format!(r#"LinkResourceURI="{uri}""#)), "{spreads}");
|
||||
assert!(!spreads.contains("<Contents") && !spreads.contains(r#"StoredState="Embedded""#), "no image is embedded");
|
||||
let back = designcraft_idml::import_idml_with(&idml, &|_| None).unwrap();
|
||||
let asset = back.assets.values().next().unwrap();
|
||||
assert!(asset.data.is_empty(), "no image is embedded");
|
||||
assert_eq!(asset.link.as_deref(), linked.to_str());
|
||||
assert_eq!(asset.link.as_deref(), Some("Links/my photo.png"));
|
||||
assert!(dir.join("Links").join("my photo.png").exists());
|
||||
}
|
||||
|
||||
/// A link whose file is gone has no bytes to copy: the package leaves it out of Links/, the
|
||||
@@ -573,6 +585,29 @@ mod tests {
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// A package holds no path from the machine that made it: the document and the IDML link the
|
||||
/// copies in `Links/` by relative names, and they still open as linked after the move (#340).
|
||||
#[test]
|
||||
fn package_documents_hold_relative_links_only() {
|
||||
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
|
||||
let dir = std::env::temp_dir().join(format!("dc-package-relative-{}-{nonce}", std::process::id()));
|
||||
let folder = dir.file_name().unwrap().to_string_lossy().into_owned();
|
||||
let png = designcraft_render::Rendered { width: 4, height: 3, pixels: [30, 90, 180, 255].repeat(12) }.to_png();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"title": "RelativePackage", "width": 72, "height": 72})).unwrap();
|
||||
s.execute("file.place", &json!({"base64": super::super::base64_encode(&png), "name": "owned.png", "x": 5, "y": 5, "width": 16})).unwrap();
|
||||
s.execute("file.package", &json!({"dir": dir.to_string_lossy(), "idml": true, "pdf": false})).unwrap();
|
||||
let packed = super::super::from_bytes(&std::fs::read(dir.join("RelativePackage.designcraft")).unwrap()).unwrap();
|
||||
let link = packed.assets.values().next().unwrap().link.clone().unwrap();
|
||||
assert_eq!(link, "Links/owned.png");
|
||||
assert!(!link.contains(&folder));
|
||||
// Copy Link(s) To is unchanged: it relinks the open document by full path.
|
||||
s.execute("links.copyTo", &json!({"dir": dir.join("Copies").to_string_lossy()})).unwrap();
|
||||
let copied = s.doc().unwrap().doc.assets.values().next().unwrap().link.clone().unwrap();
|
||||
assert!(std::path::Path::new(&copied).is_absolute(), "{copied}");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_writes_document_links_and_report() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-package-{}", std::process::id()));
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
//! Regression coverage for deleting parent spreads through the command boundary.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use designcraft_doc::{ItemId, Selection, SpreadId, TextSel};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn frame(s: &mut Session, parent: usize, text: Option<&str>) -> ItemId {
|
||||
let mut p = json!({"spread": {"kind": "parent", "index": parent}, "rect": [36, 36, 200, 100]});
|
||||
if let Some(text) = text {
|
||||
p["content"] = json!("text");
|
||||
p["text"] = json!(text);
|
||||
}
|
||||
ItemId(s.execute("frame.create", &p).unwrap()["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_delete_rejects_invalid_ids_without_editing_or_adding_history() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("layout.parents.edit", &json!({"on": true})).unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let history = s.doc().unwrap().history.undo.len();
|
||||
for params in [json!({}), json!({"id": null}), json!({"id": "A"}), json!({"id": -1}), json!({"id": 999999})] {
|
||||
assert!(s.execute("layout.parents.delete", ¶ms).is_err(), "{params}");
|
||||
let st = s.doc().unwrap();
|
||||
assert!(Arc::ptr_eq(&st.doc, &before));
|
||||
assert_eq!(st.history.undo.len(), history);
|
||||
assert!(st.editing_parents);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_delete_preserves_local_copies_cleans_references_and_roundtrips_undo() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 3, "facingPages": false})).unwrap();
|
||||
let a = s.doc().unwrap().doc.parents[0].id;
|
||||
let text = frame(&mut s, 0, Some("Parent folio"));
|
||||
let story = s.doc().unwrap().doc.item(text).unwrap().text_frame().unwrap().story;
|
||||
let shape = frame(&mut s, 0, None);
|
||||
let group = ItemId(s.execute("object.group", &json!({"ids": [text.0, shape.0]})).unwrap()["id"].as_u64().unwrap());
|
||||
let local = ItemId(s.execute("layout.overrideParentItems", &json!({"page": 0})).unwrap()["ids"][0].as_u64().unwrap());
|
||||
let local_text = s.doc().unwrap().doc.item(local).unwrap().children().iter().find(|i| i.text_frame().is_some()).unwrap().id;
|
||||
let local_story = s.doc().unwrap().doc.item(local_text).unwrap().text_frame().unwrap().story;
|
||||
assert_ne!(story, local_story);
|
||||
|
||||
let b = SpreadId(s.execute("layout.parents.new", &json!({"prefix": "B"})).unwrap()["id"].as_u64().unwrap());
|
||||
let c = SpreadId(s.execute("layout.parents.new", &json!({"prefix": "C"})).unwrap()["id"].as_u64().unwrap());
|
||||
let inherited = frame(&mut s, 1, Some("Local parent edit"));
|
||||
s.edit(|d, _| {
|
||||
// Imported parents can base themselves on another parent and carry local overrides.
|
||||
let bp = Arc::make_mut(&mut d.parents[1]);
|
||||
bp.parent.as_mut().unwrap().based_on = Some(a);
|
||||
bp.pages[0].parent = Some(a);
|
||||
bp.pages[0].overridden.push(text);
|
||||
Arc::make_mut(&mut d.parents[2]).parent.as_mut().unwrap().based_on = Some(b);
|
||||
d.item_mut(inherited).unwrap().overrides = Some(text);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("layout.pages.applyParent", &json!({"pages": [1], "parent": "B"})).unwrap();
|
||||
let other_local = ItemId(s.execute("layout.overrideParentItems", &json!({"page": 1, "ids": [inherited.0]})).unwrap()["ids"][0].as_u64().unwrap());
|
||||
s.execute("layout.parents.edit", &json!({"on": true})).unwrap();
|
||||
s.doc_mut().unwrap().selection = Selection::text(TextSel::caret(story, 0));
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let history = s.doc().unwrap().history.undo.len();
|
||||
before.check().unwrap();
|
||||
|
||||
s.execute("layout.parents.delete", &json!({"id": a.0})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(st.history.undo.len(), history + 1);
|
||||
assert_eq!(st.selection, Selection::default());
|
||||
assert!(st.editing_parents, "other parents remain editable");
|
||||
let d = &st.doc;
|
||||
assert_eq!(d.parents.iter().map(|p| p.id).collect::<Vec<_>>(), [b, c]);
|
||||
assert_eq!(d.page(0).unwrap().parent, None);
|
||||
assert_eq!(d.page(1).unwrap().parent, Some(b));
|
||||
assert_eq!(d.page(2).unwrap().parent, None);
|
||||
assert_eq!(d.parents[0].parent.as_ref().unwrap().based_on, None);
|
||||
assert_eq!(d.parents[1].parent.as_ref().unwrap().based_on, Some(b));
|
||||
assert_eq!(d.parents[0].pages[0].parent, None);
|
||||
assert!(d.parents[0].pages[0].overridden.is_empty());
|
||||
assert!([group, text, shape].iter().all(|id| d.item(*id).is_none()));
|
||||
assert!(d.story(story).is_none(), "nested text frames leave no dangling story");
|
||||
assert_eq!(d.item(local).unwrap().overrides, None);
|
||||
assert_eq!(d.story(local_story).unwrap().text, "Parent folio");
|
||||
assert!(d.page(0).unwrap().overridden.is_empty());
|
||||
assert_eq!(d.item(inherited).unwrap().overrides, None);
|
||||
assert_eq!(d.item(other_local).unwrap().overrides, Some(inherited));
|
||||
assert_eq!(d.page(1).unwrap().overridden, [inherited]);
|
||||
d.check().unwrap();
|
||||
let after = d.clone();
|
||||
|
||||
s.execute("edit.undo", &Value::Null).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
s.execute("edit.redo", &Value::Null).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc, after);
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_delete_last_parent_returns_to_document_pages() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 2})).unwrap();
|
||||
let id = s.doc().unwrap().doc.parents[0].id;
|
||||
s.execute("layout.parents.edit", &json!({"on": true})).unwrap();
|
||||
frame(&mut s, 0, Some("Last parent"));
|
||||
let selection_before = s.doc().unwrap().selection.clone();
|
||||
assert!(selection_before.text.is_some(), "creating the text frame leaves a caret");
|
||||
s.execute("layout.parents.delete", &json!({"id": id.0})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
assert!(st.doc.parents.is_empty());
|
||||
assert!(!st.editing_parents);
|
||||
assert_eq!(st.selection, Selection::default());
|
||||
assert_eq!(st.doc.page_count(), 2);
|
||||
assert!(st.doc.spreads.iter().flat_map(|sp| &sp.pages).all(|page| page.parent.is_none()));
|
||||
s.execute("edit.undo", &Value::Null).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.parents[0].id, id);
|
||||
assert_eq!(s.doc().unwrap().selection, selection_before);
|
||||
s.execute("edit.redo", &Value::Null).unwrap();
|
||||
assert!(s.doc().unwrap().doc.parents.is_empty());
|
||||
assert!(!s.doc().unwrap().editing_parents);
|
||||
assert_eq!(s.doc().unwrap().selection, Selection::default());
|
||||
// A parent-free document still supports creating another parent.
|
||||
s.execute("layout.parents.new", &json!({"prefix": "B"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.parents.len(), 1);
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
@@ -236,7 +236,8 @@ fn same_rect(a: Rect, b: Rect) -> bool {
|
||||
/// on its page (when it sits in one), then one frame per column on each page it adds. With
|
||||
/// `primary` (Smart Text Reflow of the primary text frame), each added page gets one frame on the
|
||||
/// live area with the page's columns instead. Empty frames it made at the end of the thread are
|
||||
/// removed again, except the first on a page. Returns pages added.
|
||||
/// removed again, except the first on a page it didn't add; a page it added that holds only such
|
||||
/// a frame goes with it. Returns pages added.
|
||||
/// A story on a parent spread doesn't autoflow: its frames thread only on that parent. Errors
|
||||
/// name `cmd`, the command that autoflows.
|
||||
pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize, cmd: &str, primary: bool) -> Result<usize> {
|
||||
@@ -249,6 +250,7 @@ pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize, cmd: &s
|
||||
let vertical = story_vertical || primary;
|
||||
let mut added = 0;
|
||||
let mut made = Vec::new();
|
||||
let mut new_pages = Vec::new();
|
||||
let mut first = true;
|
||||
loop {
|
||||
let (overset, held, full) = fit(d, sid);
|
||||
@@ -278,7 +280,7 @@ pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize, cmd: &s
|
||||
let per_page = if vertical { 1 } else { d.page(page).map_or(1, |p| p.columns.count.max(1) as usize) };
|
||||
let pages = want.div_ceil(per_page).clamp(1, max_pages - added);
|
||||
let parent = d.page_loc(page).and_then(|(si, pi)| d.spreads[si].pages[pi].parent);
|
||||
d.insert_pages(Some(page), pages, parent)?;
|
||||
new_pages.extend(d.insert_pages(Some(page), pages, parent)?);
|
||||
for pg in page + 1..=page + pages {
|
||||
let (si, pi) = d.page_loc(pg).ok_or_else(|| bad(cmd, "page insert failed"))?;
|
||||
let columns = d.spreads[si].pages[pi].columns.clone();
|
||||
@@ -297,10 +299,24 @@ pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize, cmd: &s
|
||||
while let Some(&f) = made.last() {
|
||||
let frames = d.story(sid).map_or(&[][..], |s| s.frames.as_slice());
|
||||
let [.., before, tail] = frames else { break };
|
||||
if *tail != f || cs.frame(f).is_some_and(|ft| !ft.range.is_empty()) || d.page_of_item(*before) != d.page_of_item(f) {
|
||||
if *tail != f || cs.frame(f).is_some_and(|ft| !ft.range.is_empty()) {
|
||||
break;
|
||||
}
|
||||
d.remove_item(f)?;
|
||||
let page = d.page_of_item(f);
|
||||
if d.page_of_item(*before) != page {
|
||||
// The first frame on its page: the estimate added more pages than the text needs.
|
||||
// Drop the page too when autoflow added it and the frame is all it holds.
|
||||
let Some(pg) = page.filter(|pg| d.page(*pg).is_some_and(|p| new_pages.contains(&p.id))) else { break };
|
||||
let alone = d.spreads.iter().flat_map(|sp| sp.items.iter()).all(|it| it.id == f || d.page_of_item(it.id) != Some(pg));
|
||||
if !alone {
|
||||
break;
|
||||
}
|
||||
d.remove_item(f)?;
|
||||
d.delete_pages(&[pg])?;
|
||||
added = added.saturating_sub(1);
|
||||
} else {
|
||||
d.remove_item(f)?;
|
||||
}
|
||||
made.pop();
|
||||
}
|
||||
Ok(added)
|
||||
@@ -366,7 +382,14 @@ pub(super) fn place_text(
|
||||
let story = d.story_mut(t.story).ok_or(designcraft_doc::DocError::NoStory(t.story))?;
|
||||
let r = t.range();
|
||||
story.delete(r.clone());
|
||||
// An insertion point in an empty paragraph takes the first imported paragraph's style
|
||||
// (insert_story would keep the empty paragraph's own, losing the styleMap result).
|
||||
let p0 = story.para_at(r.start);
|
||||
let blank = story.para_ranges().get(p0).is_some_and(|x| x.is_empty());
|
||||
let end = story.insert_story(r.start, &imp.story);
|
||||
if blank && let (Some(first), Some(p)) = (imp.story.paras.first(), story.paras.get_mut(p0)) {
|
||||
*p = ParaFormat { table: p.table, ..first.clone() };
|
||||
}
|
||||
sel.text = Some(TextSel { anchor: end, focus: end, ..t });
|
||||
if link.is_some() {
|
||||
warnings.push("not linked: a linked text file fills a whole story, not an insertion point".into());
|
||||
@@ -686,6 +709,38 @@ mod reflow_tests {
|
||||
s.execute("text.delete", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.page_count(), 1);
|
||||
}
|
||||
|
||||
/// Placing text into the primary frame adds only the pages the text needs, also when a wrap
|
||||
/// leaves page 1 little room or the text ends at a page boundary (#1165).
|
||||
#[test]
|
||||
fn smart_text_reflow_place_adds_no_empty_pages() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-reflow-place-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
for (n, wrap, want) in [(280, true, 5), (250, false, 3)] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"preset": "Letter", "pages": 1, "primaryTextFrame": true})).unwrap();
|
||||
if wrap {
|
||||
let id = s.execute("frame.create", &json!({"rect": [36, 36, 576, 600]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("object.textWrap", &json!({"ids": [id], "mode": "boundingBox"})).unwrap();
|
||||
}
|
||||
let sid = s.doc().unwrap().doc.settings.primary_story.expect("primary story");
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 0, "focus": 0})).unwrap();
|
||||
let path = dir.join(format!("words-{n}.txt"));
|
||||
std::fs::write(&path, "Words keep coming and the page fills up quickly with them. ".repeat(n)).unwrap();
|
||||
s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
assert_eq!(d.page_count(), want, "{n} lines");
|
||||
let frames = &d.story(sid).unwrap().frames;
|
||||
assert_eq!(frames.len(), d.page_count(), "{n} lines");
|
||||
let cs = designcraft_compose::compose_story(&d, sid, &Default::default());
|
||||
assert!(cs.overset_at.is_none(), "{n} lines");
|
||||
for f in frames {
|
||||
assert!(cs.frame(*f).is_some_and(|ft| !ft.range.is_empty()), "{n} lines: empty frame {f:?}");
|
||||
}
|
||||
d.check().unwrap();
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -767,6 +822,20 @@ mod pdf_page_tests {
|
||||
assert_eq!(s.doc().unwrap().doc.styles.para("Body").unwrap().chars.size, Some(10.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_first_paragraph_into_empty_caret_keeps_mapped_style() {
|
||||
// Regression (#1208): placing into a new text frame (which leaves its caret selected) left the first paragraph unstyled.
|
||||
let b64 = crate::cmd::base64_encode(&word_file());
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("style.paragraph.create", &json!({"name": "Headline"})).unwrap();
|
||||
let fr = s.execute("frame.create", &json!({"rect": [72, 72, 300, 200], "content": "text"})).unwrap();
|
||||
let sid = fr["story"].as_u64().unwrap();
|
||||
s.execute("file.place", &json!({"base64": b64, "name": "a.docx", "frame": fr["id"], "styleMap": {"Title": "Headline"}})).unwrap();
|
||||
let st = s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().clone();
|
||||
assert_eq!(st.paras.iter().map(|p| p.style.as_str()).collect::<Vec<_>>(), ["Headline", "Body"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_normal_comes_in_as_its_own_style() {
|
||||
// Regression (#722): Normal's formatting (over Word's document defaults) was dropped.
|
||||
|
||||
@@ -116,6 +116,7 @@ fn overset_location(s: &Session, target: designcraft_doc::ItemId) -> Option<(des
|
||||
pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {
|
||||
let Some(st) = s.active() else { return vec![] };
|
||||
let d = &st.doc;
|
||||
let document = st.path.as_deref().map(std::path::Path::new);
|
||||
let mut out = Vec::new();
|
||||
// Stories: overset and fonts.
|
||||
let mut missing_fonts: Vec<String> = Vec::new();
|
||||
@@ -251,10 +252,13 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {
|
||||
match &it.content {
|
||||
Content::Graphic(g) => match d.assets.get(&g.asset) {
|
||||
None => out.push(Issue { severity: "error", kind: "missingLink", message: "Missing graphic".into(), item: Some(it.id.0), page }),
|
||||
Some(a) if a.data.is_empty() || super::links::link_missing(a) => out.push(Issue {
|
||||
Some(a) if a.data.is_empty() || super::links::link_missing(a, document) => out.push(Issue {
|
||||
severity: "error",
|
||||
kind: "missingLink",
|
||||
message: format!("Missing link: {}", a.link.as_deref().unwrap_or(&a.name)),
|
||||
message: match a.link.as_deref().and_then(|l| super::links::refused(l, document)) {
|
||||
Some(why) => format!("Missing link: {} ({why})", a.link.as_deref().unwrap_or(&a.name)),
|
||||
None => format!("Missing link: {}", a.link.as_deref().unwrap_or(&a.name)),
|
||||
},
|
||||
item: Some(it.id.0),
|
||||
page,
|
||||
}),
|
||||
|
||||
@@ -30,7 +30,7 @@ fn style_specs() -> Vec<CommandSpec> {
|
||||
"New Cell Style…",
|
||||
[],
|
||||
None,
|
||||
"{name, fromSelection?: bool (the target cell's look), fill?, tint?, insets?: n|[t,l,b,r], vj?: top|center|bottom|justify, stroke?: {weight?, color?, tint?, type?, gapColor?, gapTint?} (all four edges; values as table.setCell's stroke), paragraphStyle?, basedOn?: cell style name, story?, table?, rows?: [a, b] | row?, cols?: [a, b] | col? (the target cells; default: the selection)} → {name}",
|
||||
"{name, fromSelection?: bool (the target cell's look), fill?, tint?, insets?: n|[t,l,b,r], vj?: top|center|bottom|justify, firstBaseline?: ascent|capHeight|leading|xHeight|fixed|emboxHeight, firstBaselineMin?: pt, stroke?: {weight?, color?, tint?, type?, gapColor?, gapTint?} (all four edges; values as table.setCell's stroke), paragraphStyle?, basedOn?: cell style name, story?, table?, rows?: [a, b] | row?, cols?: [a, b] | col? (the target cells; default: the selection)} → {name}",
|
||||
has_doc,
|
||||
|s, p| cell_style_create(s, p)
|
||||
),
|
||||
@@ -297,7 +297,7 @@ fn table_specs() -> Vec<CommandSpec> {
|
||||
"Cell Options",
|
||||
[],
|
||||
None,
|
||||
"{fill?: swatch, tint?: 0..1, insets?: n | [t,l,b,r], vj? | verticalJustification?: top|center|bottom|justify, text?, stroke?: {weight?, color?: swatch, tint?: 0..1, type?: solid|dashed|dotted|thickThin|thinThick|thinThin|thickThick|thinThickThin|thickThinThick|wavy|hashed | {kind:…}, gapColor?: swatch, gapTint?: 0..1, edges?: name | [names] (all|outer|inner|top|left|bottom|right|innerHorizontal|innerVertical; default all)}, story?, table?, rows?: [a, b] | row?, cols?: [a, b] | col? (the target cells; default: the selection)} — top/left/bottom/right are the target range's outer sides, innerHorizontal/innerVertical the lines between its rows/columns",
|
||||
"{fill?: swatch, tint?: 0..1, insets?: n | [t,l,b,r], vj? | verticalJustification?: top|center|bottom|justify, firstBaseline?: ascent|capHeight|leading|xHeight|fixed|emboxHeight, firstBaselineMin?: pt, text?, stroke?: {weight?, color?: swatch, tint?: 0..1, type?: solid|dashed|dotted|thickThin|thinThick|thinThin|thickThick|thinThickThin|thickThinThick|wavy|hashed | {kind:…}, gapColor?: swatch, gapTint?: 0..1, edges?: name | [names] (all|outer|inner|top|left|bottom|right|innerHorizontal|innerVertical; default all)}, story?, table?, rows?: [a, b] | row?, cols?: [a, b] | col? (the target cells; default: the selection)} — top/left/bottom/right are the target range's outer sides, innerHorizontal/innerVertical the lines between its rows/columns",
|
||||
in_table,
|
||||
set_cell
|
||||
),
|
||||
@@ -932,6 +932,22 @@ fn drawn_side(t: &Table, owners: &[(usize, usize)], (r, c): (usize, usize), side
|
||||
drawn.or_else(|| t.cell(r, c).and_then(|cell| cell.strokes.get(side).cloned()))
|
||||
}
|
||||
|
||||
/// Cell Options › Text › First Baseline: `firstBaseline` (offset method) and `firstBaselineMin` (pt).
|
||||
fn first_baseline_params(p: &Value, id: &str) -> Result<(Option<designcraft_doc::FirstBaseline>, Option<f64>)> {
|
||||
let fb = match p.get("firstBaseline").filter(|v| !v.is_null()) {
|
||||
Some(v) => Some(serde_json::from_value(v.clone()).map_err(|e| bad(id, format!("firstBaseline: {e}")))?),
|
||||
None => None,
|
||||
};
|
||||
let min = match p.get("firstBaselineMin").filter(|v| !v.is_null()) {
|
||||
Some(v) => match v.as_f64().filter(|m| m.is_finite() && (0.0..=8640.0).contains(m)) {
|
||||
Some(m) => Some(m),
|
||||
None => return Err(bad(id, "firstBaselineMin: a number of points from 0 to 8640")),
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
Ok((fb, min))
|
||||
}
|
||||
|
||||
fn set_cell(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const ID: &str = "table.setCell";
|
||||
let g = target(s, p, ID)?;
|
||||
@@ -954,6 +970,7 @@ fn set_cell(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(o) => return Err(bad(ID, format!("unknown vj `{o}`"))),
|
||||
None => None,
|
||||
};
|
||||
let (first_baseline, first_baseline_min) = first_baseline_params(p, ID)?;
|
||||
let text = str_param(p, "text").map(|_| super::text_param(p, "text"));
|
||||
let stroke = match p.get("stroke").filter(|v| !v.is_null()) {
|
||||
Some(sv) => Some((StrokeEdit::parse(doc, sv, ID)?, EdgeSel::parse(sv.get("edges"), ID)?)),
|
||||
@@ -998,6 +1015,12 @@ fn set_cell(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(v) = vj {
|
||||
cell.vj = v;
|
||||
}
|
||||
if let Some(v) = first_baseline {
|
||||
cell.first_baseline = v;
|
||||
}
|
||||
if let Some(v) = first_baseline_min {
|
||||
cell.first_baseline_min = v;
|
||||
}
|
||||
if let Some(tx) = &text {
|
||||
let len = cell.text.len();
|
||||
cell.text.replace(0..len, tx);
|
||||
@@ -1525,7 +1548,7 @@ fn get(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.filter_map(|(r, c)| {
|
||||
let cell = t.cell(r, c)?;
|
||||
Some(json!({"row": r, "col": c, "rowSpan": cell.row_span, "colSpan": cell.col_span, "text": cell.text.text, "fill": cell.fill,
|
||||
"insets": cell.insets, "vj": cell.vj, "style": cell.style}))
|
||||
"insets": cell.insets, "vj": cell.vj, "firstBaseline": cell.first_baseline, "firstBaselineMin": cell.first_baseline_min, "style": cell.style}))
|
||||
})
|
||||
.collect();
|
||||
Ok(json!({
|
||||
@@ -1572,6 +1595,13 @@ fn cell_style_fields(cs: &mut designcraft_doc::CellStyle, p: &Value, stroke: Opt
|
||||
if let Some(v) = p.get("vj") {
|
||||
cs.vj = Some(serde_json::from_value(v.clone()).map_err(|e| bad(cmd, format!("vj: {e}")))?);
|
||||
}
|
||||
let (first_baseline, first_baseline_min) = first_baseline_params(p, cmd)?;
|
||||
if first_baseline.is_some() {
|
||||
cs.first_baseline = first_baseline;
|
||||
}
|
||||
if first_baseline_min.is_some() {
|
||||
cs.first_baseline_min = first_baseline_min;
|
||||
}
|
||||
if let Some(st) = stroke {
|
||||
cs.stroke = Some(st);
|
||||
cs.strokes = Default::default();
|
||||
@@ -1600,6 +1630,8 @@ fn cell_style_create(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
cs.fill_tint = Some(cell.fill_tint);
|
||||
cs.insets = Some(cell.insets);
|
||||
cs.vj = Some(cell.vj);
|
||||
cs.first_baseline = Some(cell.first_baseline);
|
||||
cs.first_baseline_min = Some(cell.first_baseline_min);
|
||||
cs.stroke = Some(cell.strokes[0].clone());
|
||||
cs.paragraph_style = cell.text.paras.first().map(|p| p.style.clone());
|
||||
}
|
||||
@@ -1787,6 +1819,28 @@ mod style_tests {
|
||||
|
||||
use crate::Session;
|
||||
|
||||
#[test]
|
||||
fn cell_first_baseline_is_a_cell_option_and_a_cell_style_attribute() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 400], "content": "text", "text": ""})).unwrap();
|
||||
let sid = r["story"].as_u64().unwrap();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();
|
||||
s.execute("table.insert", &json!({"rows": 2, "cols": 2})).unwrap();
|
||||
s.execute("table.setCell", &json!({"row": 0, "col": 0, "firstBaseline": "capHeight", "firstBaselineMin": 3})).unwrap();
|
||||
let got = s.execute("table.get", &json!({})).unwrap();
|
||||
let cell = got["cells"].as_array().unwrap().iter().find(|c| c["row"] == 0 && c["col"] == 0).unwrap().clone();
|
||||
assert_eq!((cell["firstBaseline"].as_str(), cell["firstBaselineMin"].as_f64()), (Some("capHeight"), Some(3.0)));
|
||||
assert!(s.execute("table.setCell", &json!({"row": 0, "col": 0, "firstBaseline": "middle"})).is_err());
|
||||
assert!(s.execute("table.setCell", &json!({"row": 0, "col": 0, "firstBaselineMin": -1})).is_err());
|
||||
s.execute("style.cell.create", &json!({"name": "Leading", "firstBaseline": "leading"})).unwrap();
|
||||
s.execute("style.cell.apply", &json!({"name": "Leading", "row": 1, "col": 1})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let t = d.story(designcraft_doc::StoryId(sid)).unwrap().tables.values().next().unwrap();
|
||||
assert_eq!(t.cell(1, 1).unwrap().first_baseline, designcraft_doc::FirstBaseline::Leading);
|
||||
assert_eq!(t.cell(1, 0).unwrap().first_baseline, designcraft_doc::FirstBaseline::Ascent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_and_table_styles_apply_and_follow_edits() {
|
||||
let mut s = Session::new();
|
||||
@@ -1897,7 +1951,20 @@ fn place_graphic(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let g = target(s, p, ID)?;
|
||||
let (bytes, name, link) = super::file::read_source(p)?;
|
||||
let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad(ID, "unsupported or corrupt image"))?;
|
||||
let (nw, nh) = designcraft_render::natural_size(&bytes, 0).ok_or_else(|| bad(ID, "invalid image dimensions"))?;
|
||||
let fill = p.get("fit").and_then(Value::as_str) == Some("fill");
|
||||
// Compute fitting before the edit: a small EPS can overflow in a huge cell.
|
||||
let t = s.doc()?.doc.story(g.story).and_then(|st| st.tables.get(&g.table)).ok_or_else(|| bad(ID, "no table"))?;
|
||||
let (r, c) = t.owner(g.range.r0, g.range.c0);
|
||||
let cell = t.cell(r, c).ok_or_else(|| bad(ID, "no cell"))?;
|
||||
let (rs, cs) = (cell.row_span.max(1) as usize, cell.col_span.max(1) as usize);
|
||||
let w: f64 = t.columns.get(c..c.saturating_add(cs).min(t.ncols())).ok_or_else(|| bad(ID, "invalid cell span"))?.iter().map(|x| x.width).sum();
|
||||
let h: f64 = t.rows.get(r..r.saturating_add(rs).min(t.nrows())).ok_or_else(|| bad(ID, "invalid cell span"))?.iter().map(|x| x.height).sum();
|
||||
let ins = cell.insets;
|
||||
let (bw, bh) = ((w - ins[1] - ins[3]).max(1.0), (h - ins[0] - ins[2]).max(1.0));
|
||||
let k = if fill { (bw / nw).max(bh / nh) } else { (bw / nw).min(bh / nh) };
|
||||
let xf = designcraft_geom::Affine::translate(((bw - nw * k) / 2.0, (bh - nh * k) / 2.0)) * designcraft_geom::Affine::scale(k);
|
||||
super::file::validate_placement((nw, nh), designcraft_geom::Rect::new(0.0, 0.0, bw, bh), xf, ID)?;
|
||||
s.edit(|d, sel| {
|
||||
let fresh = designcraft_doc::AssetId(d.alloc());
|
||||
let mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
@@ -1912,16 +1979,6 @@ fn place_graphic(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
});
|
||||
let st = d.story_mut(g.story).ok_or_else(|| bad(ID, "no story"))?;
|
||||
let t = st.table_mut(g.table).ok_or_else(|| bad(ID, "no table"))?;
|
||||
let (r, c) = t.owner(g.range.r0, g.range.c0);
|
||||
let cell = t.cell(r, c).ok_or_else(|| bad(ID, "no cell"))?;
|
||||
let (rs, cs) = (cell.row_span.max(1) as usize, cell.col_span.max(1) as usize);
|
||||
let w: f64 = t.columns[c..(c + cs).min(t.ncols())].iter().map(|x| x.width).sum();
|
||||
let h: f64 = t.rows[r..(r + rs).min(t.nrows())].iter().map(|x| x.height).sum();
|
||||
let ins = cell.insets;
|
||||
let (bw, bh) = ((w - ins[1] - ins[3]).max(1.0), (h - ins[0] - ins[2]).max(1.0));
|
||||
let (nw, nh) = (pw as f64, ph as f64);
|
||||
let k = if fill { (bw / nw).max(bh / nh) } else { (bw / nw).min(bh / nh) };
|
||||
let xf = designcraft_geom::Affine::translate(((bw - nw * k) / 2.0, (bh - nh * k) / 2.0)) * designcraft_geom::Affine::scale(k);
|
||||
let cell = t.cell_mut(r, c).ok_or_else(|| bad(ID, format!("no cell {r},{c}")))?;
|
||||
cell.graphic = Some(designcraft_doc::Graphic {
|
||||
asset: aid,
|
||||
@@ -2188,6 +2245,31 @@ mod sort_tests {
|
||||
assert_eq!((cell(&s, 2, 0), cell(&s, 2, 1)), ("x".to_string(), "a".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_row_cannot_move_a_header_row_out_of_place() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 400], "content": "text", "text": ""})).unwrap();
|
||||
s.execute("text.select", &json!({"story": r["story"], "anchor": 0, "focus": 0})).unwrap();
|
||||
s.execute("table.insert", &json!({"rows": 3, "cols": 2, "headerRows": 1})).unwrap();
|
||||
assert!(s.execute("table.moveRow", &json!({"from": 0, "to": 2})).is_err());
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
// Dragging the selected header row onto the last body row is refused too.
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let (sid, tid) = d.stories.values().find_map(|st| st.tables.keys().next().map(|k| (st.id, *k))).unwrap();
|
||||
let fid = d.stories[&sid].frames[0];
|
||||
let cs = s.cache.get(&d, sid, None);
|
||||
let cells = &cs.frames[0].tables[0].cells;
|
||||
let centre = |r: usize| {
|
||||
let c = cells.iter().find(|c| c.row == r && c.col == 0).unwrap().rect.center();
|
||||
d.text_xf(d.item(fid).unwrap()) * c
|
||||
};
|
||||
let (from, to) = (centre(0), centre(2));
|
||||
s.execute("table.select", &json!({"story": sid.0, "table": tid, "rows": [0, 0], "what": "row"})).unwrap();
|
||||
assert!(s.execute("table.dropCells", &json!({"frame": fid.0, "from": [from.x, from.y], "to": [to.x, to.y]})).is_err());
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_options_limits_header_and_footer_rows() {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -125,12 +125,20 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let sid = StoryId(p.get("story").and_then(Value::as_u64).ok_or_else(|| bad("story.replaceRange", "missing story"))?);
|
||||
let a = p.get("start").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
let b = p.get("end").and_then(Value::as_u64).map(|v| v as usize).unwrap_or(a);
|
||||
let offset = |v: u64| usize::try_from(v).map_err(|_| bad("story.replaceRange", format!("offset {v} is out of range")));
|
||||
let a = offset(p.get("start").and_then(Value::as_u64).unwrap_or(0))?;
|
||||
let b = match p.get("end").and_then(Value::as_u64) {
|
||||
Some(v) => offset(v)?,
|
||||
None => a,
|
||||
};
|
||||
let text = super::text_param(p, "text");
|
||||
s.edit(|d, sel| {
|
||||
let st = d.story_mut(sid).ok_or(designcraft_doc::DocError::NoStory(sid))?;
|
||||
let (a, b) = (a.min(st.len()), b.min(st.len()).max(a.min(st.len())));
|
||||
let n = st.len();
|
||||
if a > b || b > n || !st.text.is_char_boundary(a) || !st.text.is_char_boundary(b) {
|
||||
let msg = format!("range {a}..{b} must be UTF-8 byte offsets on character boundaries, start <= end; the story is {n} bytes");
|
||||
return Err(bad("story.replaceRange", msg));
|
||||
}
|
||||
st.replace(a..b, &text);
|
||||
let len = st.len();
|
||||
if let Some(t) = sel.text.as_mut().filter(|t| t.story == sid) {
|
||||
@@ -2292,9 +2300,14 @@ mod open_type_tests {
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [0, 0, 200, 100], "content": "text", "text": "é漢🙂x"})).unwrap();
|
||||
let sid = r["story"].clone();
|
||||
// Offsets inside 漢 (2..5) and 🙂 (5..9) snap back to the character starts: 漢 is replaced.
|
||||
s.execute("story.replaceRange", &json!({"story": sid, "start": 3, "end": 6, "text": "字"})).unwrap();
|
||||
let story = designcraft_doc::StoryId(sid.as_u64().unwrap());
|
||||
// Offsets inside 漢 (2..5) and 🙂 (5..9), past the end, or reversed are refused and change nothing.
|
||||
for (start, end) in [(3, 6), (2, 4), (0, 99), (5, 2)] {
|
||||
let r = s.execute("story.replaceRange", &json!({"story": sid, "start": start, "end": end, "text": "字"}));
|
||||
assert!(r.is_err_and(|e| e.to_string().contains("10 bytes")), "{start}..{end}");
|
||||
assert_eq!(s.doc().unwrap().doc.story(story).unwrap().text, "é漢🙂x");
|
||||
}
|
||||
s.execute("story.replaceRange", &json!({"story": sid, "start": 2, "end": 5, "text": "字"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.story(story).unwrap().text, "é字🙂x");
|
||||
// A caret before 🙂 (byte 5) would land inside it once é (2 bytes) is gone.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 5})).unwrap();
|
||||
|
||||
+57
-1
@@ -8,12 +8,58 @@
|
||||
//! it is ([`is_file`], [`is_dir`], [`exists`], [`metadata`]), and as the roots' own message where
|
||||
//! it asks for the bytes.
|
||||
|
||||
pub use craft_docpath::Refused;
|
||||
pub use std::fs::Metadata;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// The path to open for a path stored in a document (a link, a data source, a book chapter),
|
||||
/// resolved against `document`, the path of the file it came from, when there is one.
|
||||
///
|
||||
/// Paths to other computers (other than the document's own network share), devices and URLs are
|
||||
/// refused, and so is a result that isn't absolute: without a document, a relative path would
|
||||
/// depend on the working directory, and a Windows path names no file on other platforms. A
|
||||
/// refused path is never handed to the file system; callers treat it as a missing file.
|
||||
pub fn document_path(stored: &str, document: Option<&Path>) -> Result<PathBuf, Refused> {
|
||||
let document = document.map(|d| std::path::absolute(d).unwrap_or_else(|_| d.to_path_buf()));
|
||||
let path = craft_docpath::resolve(stored, document.as_deref())?;
|
||||
if path.is_absolute() { Ok(path) } else { Err(Refused::Invalid) }
|
||||
}
|
||||
|
||||
/// [`document_path`] for a path stored in a file inside `folder` (a data file's image names).
|
||||
pub fn folder_path(stored: &str, folder: &Path) -> Result<PathBuf, Refused> {
|
||||
// Resolved against a document in the folder: only the document's folder is used.
|
||||
document_path(stored, Some(&folder.join("-")))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
static TOUCHED: std::cell::RefCell<Option<Vec<PathBuf>>> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// Run `f` and list every path it handed to this module, in order (on this thread).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn touched<T>(f: impl FnOnce() -> T) -> (T, Vec<PathBuf>) {
|
||||
let previous = TOUCHED.with(|t| t.replace(Some(Vec::new())));
|
||||
let out = f();
|
||||
let seen = TOUCHED.with(|t| t.replace(previous)).unwrap_or_default();
|
||||
(out, seen)
|
||||
}
|
||||
|
||||
fn touch(path: &Path) {
|
||||
#[cfg(test)]
|
||||
TOUCHED.with(|t| {
|
||||
if let Some(seen) = t.borrow_mut().as_mut() {
|
||||
seen.push(path.to_path_buf());
|
||||
}
|
||||
});
|
||||
#[cfg(not(test))]
|
||||
let _ = path;
|
||||
}
|
||||
|
||||
/// The bytes of the file at `path`, inside the automation read root when there is one.
|
||||
pub fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>, String> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path)?;
|
||||
std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))
|
||||
}
|
||||
@@ -22,6 +68,7 @@ pub fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>, String> {
|
||||
/// failures. A refused path is a `PermissionDenied` error carrying the roots' message.
|
||||
pub fn read(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
std::fs::read(path)
|
||||
}
|
||||
@@ -31,6 +78,7 @@ pub fn read(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
|
||||
pub fn read_capped(path: impl AsRef<Path>, cap: u64) -> std::io::Result<Vec<u8>> {
|
||||
use std::io::Read;
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
let mut b = Vec::new();
|
||||
std::fs::File::open(path)?.take(cap.saturating_add(1)).read_to_end(&mut b)?;
|
||||
@@ -42,6 +90,7 @@ pub fn read_capped(path: impl AsRef<Path>, cap: u64) -> std::io::Result<Vec<u8>>
|
||||
/// roots' message.
|
||||
pub fn open(path: impl AsRef<Path>) -> std::io::Result<std::fs::File> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
std::fs::File::open(path)
|
||||
}
|
||||
@@ -49,6 +98,7 @@ pub fn open(path: impl AsRef<Path>) -> std::io::Result<std::fs::File> {
|
||||
/// The text of the file at `path`, inside the automation read root when there is one.
|
||||
pub fn read_to_string(path: impl AsRef<Path>) -> Result<String, String> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path)?;
|
||||
std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))
|
||||
}
|
||||
@@ -56,6 +106,7 @@ pub fn read_to_string(path: impl AsRef<Path>) -> Result<String, String> {
|
||||
/// Write `bytes` to the file at `path`, inside the automation write root when there is one.
|
||||
pub fn write_file(path: impl AsRef<Path>, bytes: &[u8]) -> Result<(), String> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_write(path)?;
|
||||
// Replaced atomically where there is a real file system (see `atomic`).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -71,6 +122,7 @@ pub fn write_file(path: impl AsRef<Path>, bytes: &[u8]) -> Result<(), String> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn replace_with(path: impl AsRef<Path>, staged: impl AsRef<Path>) -> std::io::Result<()> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_write(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
let mut input = std::fs::File::open(staged)?;
|
||||
crate::atomic::write_atomic_with(path, |file| std::io::copy(&mut input, file).map(|_| ()))
|
||||
@@ -80,6 +132,7 @@ pub fn replace_with(path: impl AsRef<Path>, staged: impl AsRef<Path>) -> std::io
|
||||
/// write root when there is one.
|
||||
pub fn writable(path: impl AsRef<Path>) -> std::io::Result<()> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_write(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
std::fs::OpenOptions::new().write(true).open(path).map(drop)
|
||||
}
|
||||
@@ -88,6 +141,7 @@ pub fn writable(path: impl AsRef<Path>) -> std::io::Result<()> {
|
||||
/// one.
|
||||
pub fn canonicalize(path: impl AsRef<Path>) -> std::io::Result<std::path::PathBuf> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
std::fs::canonicalize(path)
|
||||
}
|
||||
@@ -96,6 +150,7 @@ pub fn canonicalize(path: impl AsRef<Path>) -> std::io::Result<std::path::PathBu
|
||||
/// there is one. A refused path is a `PermissionDenied` error carrying the roots' message.
|
||||
pub fn metadata(path: impl AsRef<Path>) -> std::io::Result<Metadata> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
std::fs::metadata(path)
|
||||
}
|
||||
@@ -118,6 +173,7 @@ pub fn exists(path: impl AsRef<Path>) -> bool {
|
||||
/// The entries of the folder at `path`, inside the automation read root when there is one.
|
||||
pub fn read_dir(path: impl AsRef<Path>) -> std::io::Result<std::fs::ReadDir> {
|
||||
let path = path.as_ref();
|
||||
touch(path);
|
||||
crate::file_access::check_read(path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
std::fs::read_dir(path)
|
||||
}
|
||||
|
||||
@@ -744,6 +744,8 @@ mod tests_idml;
|
||||
#[cfg(test)]
|
||||
mod tests_ime;
|
||||
#[cfg(test)]
|
||||
mod tests_linked_paths;
|
||||
#[cfg(test)]
|
||||
mod tests_params;
|
||||
#[cfg(test)]
|
||||
mod tests_roots;
|
||||
|
||||
@@ -158,7 +158,7 @@ fn offenders(crates_dir: &Path) -> (Vec<String>, usize) {
|
||||
for path in files {
|
||||
let rel = path.strip_prefix(crates_dir).unwrap_or(&path).to_string_lossy().replace('\\', "/");
|
||||
// `crates/<name>/src/...` only: build scripts, benches and `tests/` are not shipped code.
|
||||
if !rel.split('/').nth(1).is_some_and(|d| d == "src") || is_test_file(&rel) || ALLOWED.iter().any(|(allowed, _)| *allowed == rel) {
|
||||
if rel.split('/').nth(1).is_none_or(|d| d != "src") || is_test_file(&rel) || ALLOWED.iter().any(|(allowed, _)| *allowed == rel) {
|
||||
continue;
|
||||
}
|
||||
let Ok(text) = std::fs::read_to_string(&path) else { continue };
|
||||
|
||||
@@ -575,3 +575,100 @@ fn table_of_contents_character_styles_and_sort_round_trip_through_idml() {
|
||||
let e = &after.entries[1];
|
||||
assert_eq!((e.page_number_style.as_deref(), e.separator_style.as_deref(), e.sort), (None, None, false));
|
||||
}
|
||||
|
||||
/// `text` with the value after every `open` (up to the next `close`) set to `value`, and how
|
||||
/// many values were replaced.
|
||||
fn replace_values(text: &str, open: &str, close: char, value: &str) -> (String, usize) {
|
||||
let mut rest = text;
|
||||
let mut new = String::new();
|
||||
let mut count = 0;
|
||||
while let Some(at) = rest.find(open) {
|
||||
let start = at + open.len();
|
||||
new.push_str(&rest[..start]);
|
||||
new.push_str(value);
|
||||
let end = rest[start..].find(close).map_or(rest.len(), |e| start + e);
|
||||
rest = &rest[end..];
|
||||
count += 1;
|
||||
}
|
||||
new.push_str(rest);
|
||||
(new, count)
|
||||
}
|
||||
|
||||
/// `package` with every `{name}="…"` attribute and `<{name}>…</{name}>` / `<{name} type="…">…`
|
||||
/// property element in its XML parts set to `value`, and how many values were replaced.
|
||||
fn with_attr_set(package: &[u8], name: &str, value: &str) -> (Vec<u8>, usize) {
|
||||
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(package)).unwrap();
|
||||
let mut out = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let mut count = 0;
|
||||
for i in 0..zip.len() {
|
||||
let mut f = zip.by_index(i).unwrap();
|
||||
let mut body = Vec::new();
|
||||
std::io::Read::read_to_end(&mut f, &mut body).unwrap();
|
||||
if f.name().ends_with(".xml")
|
||||
&& let Ok(text) = String::from_utf8(body.clone())
|
||||
{
|
||||
let (text, a) = replace_values(&text, &format!(" {name}=\""), '"', value);
|
||||
let (text, b) = replace_values(&text, &format!("<{name}>"), '<', value);
|
||||
let (text, c) = replace_values(&text, &format!("<{name} type=\"unit\">"), '<', value);
|
||||
let (text, d) = replace_values(&text, &format!("<{name} type=\"double\">"), '<', value);
|
||||
count += a + b + c + d;
|
||||
body = text.into_bytes();
|
||||
}
|
||||
out.start_file(f.name(), zip::write::SimpleFileOptions::default()).unwrap();
|
||||
std::io::Write::write_all(&mut out, &body).unwrap();
|
||||
}
|
||||
(out.finish().unwrap().into_inner(), count)
|
||||
}
|
||||
|
||||
/// Non-finite numbers in an IDML package never reach the model: the imported document saves
|
||||
/// and reopens. Every listed name occurs in the exported sample, so each case really rewrites
|
||||
/// values; names the sample lacks (feather `Width`, gradient start/length) go through `El::num`,
|
||||
/// covered in `xml.rs`.
|
||||
#[test]
|
||||
fn idml_non_finite_numbers_import_to_a_document_that_reopens() {
|
||||
let package = designcraft_idml::export_idml(&crate::sample::magazine());
|
||||
let attrs = [
|
||||
"StrokeWeight",
|
||||
"MiterLimit",
|
||||
"Angle",
|
||||
"FirstBaselineOffset",
|
||||
"MinimumFirstBaselineOffset",
|
||||
"SpaceBefore",
|
||||
"SpaceAfter",
|
||||
"LeftIndent",
|
||||
"RightIndent",
|
||||
"FirstLineIndent",
|
||||
"PointSize",
|
||||
"Tracking",
|
||||
"Leading",
|
||||
"FillTint",
|
||||
"StrokeTint",
|
||||
"GapTint",
|
||||
"Position",
|
||||
"BaselineShift",
|
||||
"Skew",
|
||||
];
|
||||
let mut failures = Vec::new();
|
||||
for attr in attrs {
|
||||
for value in ["NaN", "inf", "-inf"] {
|
||||
let (bytes, replaced) = with_attr_set(&package, attr, value);
|
||||
if replaced == 0 {
|
||||
failures.push(format!("{attr}: not present in the exported package"));
|
||||
continue;
|
||||
}
|
||||
let doc = match designcraft_idml::import_idml(&bytes) {
|
||||
Ok(doc) => doc,
|
||||
Err(e) => {
|
||||
failures.push(format!("{attr}={value}: import failed: {e}"));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
match designcraft_format::save(&doc).map(|saved| designcraft_format::load(&saved)) {
|
||||
Ok(Ok(_)) => {}
|
||||
Ok(Err(e)) => failures.push(format!("{attr}={value}: saved but load failed: {e}")),
|
||||
Err(e) => failures.push(format!("{attr}={value}: save failed: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(failures.is_empty(), "{failures:#?}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
//! Paths stored in documents (links, data sources, book chapters) are followed only where
|
||||
//! [`crate::io::document_path`] allows: never to another computer, and resolved beside the
|
||||
//! document. Every path the engine hands the file system is recorded by [`crate::io::touched`].
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
const UNC: &str = r"\\192.0.2.1\share\x.png";
|
||||
const FILE_URL: &str = "file://192.0.2.1/s/x.png";
|
||||
|
||||
fn png() -> Vec<u8> {
|
||||
designcraft_render::Rendered { width: 2, height: 2, pixels: vec![255; 16] }.to_png()
|
||||
}
|
||||
|
||||
fn folder(name: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("dc-linked-paths-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// A document saved as `doc` with one placed graphic per link, linked to it.
|
||||
fn document_with_links(doc: &Path, links: &[&str]) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
for (i, link) in links.iter().enumerate() {
|
||||
s.execute("file.place", &json!({"base64": crate::cmd::base64_encode(&png()), "name": format!("{i}.png"), "x": 72 * i, "y": 72})).unwrap();
|
||||
let link = link.to_string();
|
||||
s.edit(move |d, _| {
|
||||
let a = d.assets.values_mut().find(|a| a.link.is_none() && a.name == format!("{i}.png")).unwrap();
|
||||
std::sync::Arc::make_mut(a).link = Some(link);
|
||||
Ok(Value::Null)
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
s.doc_mut().unwrap().path = Some(doc.to_string_lossy().into_owned());
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn links_to_other_computers_never_reach_the_file_system() {
|
||||
let dir = folder("remote");
|
||||
let doc = dir.join("doc.designcraft");
|
||||
let mut s = document_with_links(&doc, &[UNC, FILE_URL]);
|
||||
let (rows, seen) = crate::io::touched(|| {
|
||||
let rows = s.execute("links.list", &json!({})).unwrap();
|
||||
let issues = s.execute("preflight.run", &json!({})).unwrap();
|
||||
s.execute("links.relinkFolder", &json!({"extension": "jpg"})).unwrap();
|
||||
s.execute("links.update", &json!({})).unwrap();
|
||||
(rows, issues)
|
||||
});
|
||||
assert!(seen.is_empty(), "{seen:?}");
|
||||
let (rows, issues) = rows;
|
||||
for row in rows.as_array().unwrap() {
|
||||
assert_eq!(row["status"], "missing", "{row}");
|
||||
assert_eq!(row["refused"], "the path is on another computer", "{row}");
|
||||
}
|
||||
assert_eq!(issues.to_string().matches("Missing link").count(), 2, "{issues}");
|
||||
|
||||
// Data sources and book chapters stored the same way.
|
||||
let mut d = s.doc().unwrap().doc.as_ref().clone();
|
||||
d.data_merge.sources.push(designcraft_doc::DataSource {
|
||||
id: 1,
|
||||
path: Some(UNC.replace("x.png", "data.csv")),
|
||||
relative_path: Some(FILE_URL.replace("x.png", "data.csv")),
|
||||
name: "data.csv".into(),
|
||||
delimiter: designcraft_doc::Delimiter::Comma,
|
||||
sheet: None,
|
||||
fields: vec![],
|
||||
rows: vec![],
|
||||
fingerprint: None,
|
||||
status: designcraft_doc::SourceStatus::Ok,
|
||||
warnings: vec![],
|
||||
});
|
||||
std::fs::write(&doc, designcraft_format::save(&d).unwrap()).unwrap();
|
||||
let mut opened = Session::new();
|
||||
let ((), seen) = crate::io::touched(|| {
|
||||
opened.execute("file.open", &json!({"path": doc.to_string_lossy()})).unwrap();
|
||||
opened.execute("links.list", &json!({})).unwrap();
|
||||
});
|
||||
// The document itself and the folders beside it, nothing else.
|
||||
assert!(seen.iter().all(|p| p.starts_with(&dir)), "{seen:?}");
|
||||
assert_eq!(opened.doc().unwrap().doc.data_merge.sources[0].status, designcraft_doc::SourceStatus::Missing);
|
||||
let book = dir.join("b.dcbook");
|
||||
std::fs::write(&book, json!({"documents": [UNC.replace("x.png", "c.designcraft"), FILE_URL.replace("x.png", "c.designcraft")]}).to_string())
|
||||
.unwrap();
|
||||
s.execute("book.open", &json!({"path": book.to_string_lossy()})).unwrap();
|
||||
let (list, seen) = crate::io::touched(|| s.execute("book.list", &json!({})).unwrap());
|
||||
assert!(seen.is_empty(), "{seen:?}");
|
||||
assert_eq!(list["documents"][0]["pages"], 0);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_relative_link_resolves_beside_the_document() {
|
||||
let dir = folder("relative");
|
||||
std::fs::write(dir.join("logo.png"), png()).unwrap();
|
||||
let mut s = document_with_links(&dir.join("doc.designcraft"), &["logo.png"]);
|
||||
let (rows, seen) = crate::io::touched(|| s.execute("links.list", &json!({})).unwrap());
|
||||
assert_eq!(rows[0]["status"], "ok", "{rows}");
|
||||
assert!(rows[0]["refused"].is_null());
|
||||
assert!(seen.iter().all(|p| p.starts_with(&dir)), "{seen:?}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
@@ -71,9 +71,10 @@ fn idml_links_are_read_through_the_roots() {
|
||||
// Linked, not embedded: opening the package has to read the file its link names.
|
||||
let idml = base64_idml(&mut s);
|
||||
let linked = |doc: &designcraft_doc::Document| doc.assets.values().filter(|a| !a.data.is_empty()).count();
|
||||
let unconfined = cmd::interchange::import_idml_confined(&idml).unwrap();
|
||||
let package = base.join("outside/x.idml");
|
||||
let unconfined = cmd::interchange::import_idml_confined(&idml, Some(&package)).unwrap();
|
||||
assert_eq!(linked(&unconfined), 1, "without roots the link is followed");
|
||||
let confined = confine(Some(&roots), || cmd::interchange::import_idml_confined(&idml)).unwrap();
|
||||
let confined = confine(Some(&roots), || cmd::interchange::import_idml_confined(&idml, Some(&package))).unwrap();
|
||||
assert_eq!(linked(&confined), 0, "with roots a link outside the read root is not read");
|
||||
// The same through the commands that open or place a package.
|
||||
let b64 = cmd::base64_encode(&idml);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
//! `object.stroke` keeps `tint` and `gapTint` within 0..1, so a saved document always reloads.
|
||||
use designcraft_doc::ItemId;
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
fn stroked(tint: f64, gap_tint: f64) -> (Session, ItemId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let created = s.execute("frame.create", &json!({"rect": [36, 36, 200, 200]})).unwrap();
|
||||
let id = ItemId(created["id"].as_u64().unwrap());
|
||||
s.execute("object.stroke", &json!({"ids": [id.0], "swatch": "[Black]", "tint": tint, "gapTint": gap_tint})).unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_stroke_tints_are_clamped_and_the_saved_file_reloads() {
|
||||
for (tint, gap, want_tint, want_gap) in [(1e300, 1e300, 1.0, 1.0), (2.0, -1.0, 1.0, 0.0), (-1e300, 0.5, 0.0, 0.5)] {
|
||||
let (s, id) = stroked(tint, gap);
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let st = &doc.item(id).unwrap().stroke;
|
||||
assert_eq!((st.tint, st.gap_tint), (want_tint, want_gap), "tint {tint}, gapTint {gap}");
|
||||
let bytes = designcraft_format::save(doc).unwrap();
|
||||
designcraft_format::load(&bytes).expect("reload what we just saved");
|
||||
}
|
||||
}
|
||||
@@ -370,6 +370,24 @@ impl FontFace {
|
||||
}
|
||||
}
|
||||
|
||||
/// A font file's length and modification time.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
type FileStamp = (u64, Option<std::time::SystemTime>);
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn file_stamp(path: &std::path::Path) -> Option<FileStamp> {
|
||||
std::fs::metadata(path).ok().map(|m| (m.len(), m.modified().ok()))
|
||||
}
|
||||
|
||||
/// What [`FontDb::reload_fonts`] did: the faces cataloged by the rescan, and the installed font
|
||||
/// files that changed or were removed since they were read (their faces are read again, from the
|
||||
/// new file, the next time they are looked up).
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
pub struct ReloadedFonts {
|
||||
pub cataloged: usize,
|
||||
pub changed: Vec<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[derive(Clone, Debug)]
|
||||
struct CatalogEntry {
|
||||
@@ -412,6 +430,10 @@ pub struct FontDb {
|
||||
outlines: Mutex<HashMap<(u32, u32), Arc<BezPath>>>,
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
catalog: RwLock<Vec<CatalogEntry>>,
|
||||
/// Each installed font file read into the shared fonts, with its (length, modification time)
|
||||
/// when it was read: [`FontDb::reload_fonts`] reads again the files that changed since.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
installed_files: Mutex<HashMap<std::path::PathBuf, FileStamp>>,
|
||||
/// The folders the system font scan reads (the platform's font folders for [`FontDb::global`]).
|
||||
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
|
||||
font_dirs: Vec<std::path::PathBuf>,
|
||||
@@ -1067,6 +1089,8 @@ impl FontDb {
|
||||
outlines: Mutex::new(HashMap::new()),
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
catalog: RwLock::new(Vec::new()),
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
installed_files: Mutex::new(HashMap::new()),
|
||||
font_dirs,
|
||||
font_files: Vec::new,
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -1221,6 +1245,56 @@ impl FontDb {
|
||||
count
|
||||
}
|
||||
|
||||
/// Fonts installed, changed or removed since they were read (#328): scan the font folders
|
||||
/// again (see [`FontDb::load_system_fonts`]), then let go of the shared faces whose installed
|
||||
/// file changed (length or modification time) or is gone, so the next lookup reads the file as
|
||||
/// it is now, or finds the font missing. What was composed with the old faces must be composed
|
||||
/// again. Faces are never freed (see [`FaceRef`]): each reload of a changed file keeps the old
|
||||
/// copy. Bundled, added and document fonts are left alone. Nothing to do on wasm.
|
||||
pub fn reload_fonts(&self) -> ReloadedFonts {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
// Rescan first: a lookup after the faces are dropped finds the new files.
|
||||
let cataloged = self.load_system_fonts();
|
||||
let changed: Vec<std::path::PathBuf> = {
|
||||
let mut files = self.installed_files.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut changed: Vec<_> = files.iter().filter(|(p, stamp)| file_stamp(p).as_ref() != Some(*stamp)).map(|(p, _)| p.clone()).collect();
|
||||
changed.sort();
|
||||
for p in &changed {
|
||||
files.remove(p);
|
||||
}
|
||||
changed
|
||||
};
|
||||
if !changed.is_empty() {
|
||||
let stale = |f: &FontFace| matches!(&f.source, FontSource::Installed(p) if changed.contains(p));
|
||||
let mut dropped = std::collections::HashSet::new();
|
||||
self.faces.write().unwrap_or_else(|e| e.into_inner()).retain(|f| {
|
||||
let keep = !stale(f);
|
||||
if !keep {
|
||||
dropped.insert(f.id);
|
||||
}
|
||||
keep
|
||||
});
|
||||
self.instances.write().unwrap_or_else(|e| e.into_inner()).retain(|(base, _), f| !dropped.contains(base) && !stale(f));
|
||||
self.outlines.lock().unwrap_or_else(|e| e.into_inner()).retain(|(face, _), _| !dropped.contains(face));
|
||||
log::debug!("reloading {} changed font files ({} faces)", changed.len(), dropped.len());
|
||||
}
|
||||
ReloadedFonts { cataloged, changed }
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
ReloadedFonts::default()
|
||||
}
|
||||
|
||||
/// Read an installed font file, noting its length and modification time as they were before
|
||||
/// the read (a change during it is then found by the next reload).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn read_installed(&self, path: &std::path::Path) -> Option<Vec<u8>> {
|
||||
let stamp = file_stamp(path)?;
|
||||
let data = std::fs::read(path).ok()?;
|
||||
self.installed_files.lock().unwrap_or_else(|e| e.into_inner()).insert(path.to_path_buf(), stamp);
|
||||
Some(data)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn scan_font_dirs(&self) -> usize {
|
||||
let mut found = Vec::new();
|
||||
@@ -1294,7 +1368,7 @@ impl FontDb {
|
||||
// The whole family arrives at once (see `insert_faces`).
|
||||
let mut faces = Vec::new();
|
||||
for p in paths {
|
||||
if let Ok(data) = std::fs::read(&p) {
|
||||
if let Some(data) = self.read_installed(&p) {
|
||||
faces.extend(self.new_faces(data, FontSource::Installed(p)));
|
||||
}
|
||||
}
|
||||
@@ -1499,7 +1573,7 @@ impl FontDb {
|
||||
if std::fs::metadata(&p).map(|m| m.len() > 40 << 20).unwrap_or(true) {
|
||||
continue;
|
||||
}
|
||||
let Ok(data) = std::fs::read(&p) else { continue };
|
||||
let Some(data) = self.read_installed(&p) else { continue };
|
||||
let hit =
|
||||
enumerate_faces(&data).iter().any(|(i, _, _, _)| skrifa::FontRef::from_index(&data, *i).is_ok_and(|f| f.charmap().map(c).is_some()));
|
||||
if hit && self.add_faces(data, FontSource::Installed(p)) > 0 && covered(self) {
|
||||
|
||||
+27
-1
@@ -20,7 +20,7 @@ mod vertical;
|
||||
pub use features::{FontFeatures, font_features};
|
||||
pub use fontdb::{
|
||||
DOCUMENT_FONTS_FOLDER, DocumentFonts, FALLBACK_FAMILY, FaceRef, FontDb, FontFace, FontSource, MAX_DOCUMENT_FONT_BYTES, MAX_DOCUMENT_FONT_FILES,
|
||||
MAX_DOCUMENT_FONTS_TOTAL, ScopedFonts, base_style, bundled, same_style, system_font_dirs,
|
||||
MAX_DOCUMENT_FONTS_TOTAL, ReloadedFonts, ScopedFonts, base_style, bundled, same_style, system_font_dirs,
|
||||
};
|
||||
pub use group::{FamilyInfo, FontGroup, sort_for_menu};
|
||||
pub use harfrust::Feature;
|
||||
@@ -197,6 +197,20 @@ pub fn shape_with_context(
|
||||
out
|
||||
}
|
||||
|
||||
/// `features` as a run in `script` takes them. Contextual alternates are part of how Arabic joins
|
||||
/// (connecting strokes, tatweel and letter variants): InDesign keeps them in Arabic text with
|
||||
/// Contextual Alternates off, so `calt` off is dropped there.
|
||||
fn script_features(features: &[Feature], script: unicode_script::Script) -> std::borrow::Cow<'_, [Feature]> {
|
||||
use unicode_script::Script;
|
||||
let calt = Tag::new(b"calt");
|
||||
let off = |f: &Feature| f.tag == calt && f.value == 0;
|
||||
if script == Script::Arabic && features.iter().any(off) {
|
||||
features.iter().filter(|f| !off(f)).copied().collect::<Vec<_>>().into()
|
||||
} else {
|
||||
features.into()
|
||||
}
|
||||
}
|
||||
|
||||
fn shape_dir(
|
||||
face: &FontFace,
|
||||
text: &str,
|
||||
@@ -207,6 +221,8 @@ fn shape_dir(
|
||||
context: ShapeContext<'_>,
|
||||
) -> Vec<ShapedGlyph> {
|
||||
let mut out = Vec::with_capacity(text.len());
|
||||
let features = script_features(features, script);
|
||||
let features = &features[..];
|
||||
let shaped = face.hb().map(|hb| {
|
||||
let shaper = face.shaper.shaper(&hb).instance(face.instance.as_ref()).build();
|
||||
let mut buf = UnicodeBuffer::new();
|
||||
@@ -334,6 +350,16 @@ pub fn first_glyph(face: &FontFace, chars: &[char]) -> u32 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn arabic_keeps_contextual_alternates() {
|
||||
use unicode_script::Script;
|
||||
let off = feature("-calt").unwrap();
|
||||
let liga = feature("-liga").unwrap();
|
||||
let tags = |s: Script| script_features(&[off, liga], s).iter().map(|f| (f.tag, f.value)).collect::<Vec<_>>();
|
||||
assert_eq!(tags(Script::Arabic), vec![(liga.tag, 0)]);
|
||||
assert_eq!(tags(Script::Latin), vec![(off.tag, 0), (liga.tag, 0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arabic_script_runs_do_not_inherit_the_hebrew_shaper() {
|
||||
use unicode_script::Script;
|
||||
|
||||
@@ -389,3 +389,53 @@ fn installed_and_loaded_fonts_are_found_by_postscript_name() {
|
||||
assert_eq!(db.postscript_face("Source Serif 4"), None);
|
||||
assert_eq!(db.postscript_face("NoSuchFont-Regular"), None);
|
||||
}
|
||||
|
||||
/// Write `font` to `path` with a modification time `secs` from now, so a rewrite of the same
|
||||
/// length still reads as changed however coarse the file system's clock.
|
||||
fn write_font(path: &Path, font: &[u8], secs: u64) {
|
||||
std::fs::write(path, font).unwrap();
|
||||
let file = std::fs::File::options().write(true).open(path).unwrap();
|
||||
file.set_modified(std::time::SystemTime::now() + std::time::Duration::from_secs(secs)).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reloading_reads_changed_font_files_again_and_keeps_the_others() {
|
||||
use crate::testing::font_with_glyph;
|
||||
let dir = font_dir("reload");
|
||||
let family = "DC Test Reloaded";
|
||||
let path = dir.join("Reloaded.ttf");
|
||||
write_font(&path, &font_with_glyph(family, &['a'], 'W').unwrap(), 10);
|
||||
let db = FontDb::with_font_dirs(vec![dir.clone()]);
|
||||
let before = db.face(family, "Regular");
|
||||
let wide = before.advance(before.glyph_for('a'));
|
||||
let kept = db.face(FAMILY, "Regular");
|
||||
// The font is edited and installed again under the same name (#328).
|
||||
write_font(&path, &font_with_glyph(family, &['a'], 'i').unwrap(), 20);
|
||||
assert_eq!(db.face(family, "Regular").id(), before.id(), "a lookup alone doesn't read the file again");
|
||||
let reloaded = db.reload_fonts();
|
||||
assert_eq!(reloaded.changed, std::slice::from_ref(&path));
|
||||
assert_eq!(reloaded.cataloged, 3);
|
||||
let after = db.face(family, "Regular");
|
||||
assert_ne!(after.id(), before.id());
|
||||
let narrow = after.advance(after.glyph_for('a'));
|
||||
assert!(narrow < wide, "the edited font sets 'a' as its i: {narrow} vs {wide}");
|
||||
assert_eq!(db.face(FAMILY, "Regular").id(), kept.id(), "unchanged files aren't read again");
|
||||
assert_eq!(db.reload_fonts().changed, Vec::<PathBuf>::new(), "nothing changed since");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reloading_lets_go_of_removed_fonts_and_finds_new_ones() {
|
||||
let dir = font_dir("reload-removed");
|
||||
let removed = dir.join("Removed.ttf");
|
||||
write_font(&removed, &crate::testing::font_with("DC Test Removed", &['a']).unwrap(), 10);
|
||||
let db = FontDb::with_font_dirs(vec![dir.clone()]);
|
||||
assert_eq!(db.face("DC Test Removed", "Regular").family, "DC Test Removed");
|
||||
std::fs::remove_file(&removed).unwrap();
|
||||
write_font(&dir.join("Added.ttf"), &crate::testing::font_with("DC Test Added Later", &['a']).unwrap(), 10);
|
||||
assert_eq!(db.reload_fonts().changed, [removed]);
|
||||
assert!(!db.has_family("DC Test Removed"));
|
||||
assert_eq!(db.face("DC Test Removed", "Regular").family, FALLBACK_FAMILY);
|
||||
assert!(db.has_family("DC Test Added Later"));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
designcraft-doc = { workspace = true }
|
||||
designcraft-images = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
|
||||
+55
-13
@@ -21,14 +21,16 @@ use designcraft_doc::{AssetId, Document, ParaAttrs, Story};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const MIME: &str = "application/vnd.designcraft+zip";
|
||||
pub const VERSION: u32 = 2;
|
||||
/// Latest reader capability. Version 2 pinned the Keep Lines Together and drop cap defaults;
|
||||
/// version 3 adds rendering of retained EPS source assets.
|
||||
pub const VERSION: u32 = 3;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FormatError {
|
||||
#[error("not a DesignCraft document: {0}")]
|
||||
NotOurs(String),
|
||||
#[error("document is from a newer DesignCraft (format {0}); please update")]
|
||||
TooNew(u32),
|
||||
TooNew(u64),
|
||||
#[error("{0}")]
|
||||
Io(String),
|
||||
/// The document holds something the file can't represent; nothing is written.
|
||||
@@ -44,6 +46,7 @@ fn ext_for(mime: &str) -> &'static str {
|
||||
"image/webp" => "webp",
|
||||
"image/tiff" => "tif",
|
||||
"application/pdf" => "pdf",
|
||||
"application/postscript" => "eps",
|
||||
"image/svg+xml" => "svg",
|
||||
_ => "bin",
|
||||
}
|
||||
@@ -81,7 +84,11 @@ fn save_with_large_entries_from(doc: &Document, preview: Option<&[u8]>, large_en
|
||||
z.start_file("mimetype", stored).map_err(io)?;
|
||||
z.write_all(MIME.as_bytes()).map_err(|e| FormatError::Io(e.to_string()))?;
|
||||
z.start_file("meta.json", deflate).map_err(io)?;
|
||||
let meta = json!({"format": "designcraft", "version": VERSION, "generator": concat!("DesignCraft ", env!("CARGO_PKG_VERSION"))});
|
||||
// Older readers would silently omit EPS artwork. Preview-only legacy assets and
|
||||
// documents without EPS stay at 2 so their existing readers keep working; writing 1
|
||||
// would make a reader re-run the version 2 paragraph upgrade over current defaults.
|
||||
let version = if doc.assets.values().any(|a| designcraft_images::is_eps(&a.data)) { VERSION } else { 2 };
|
||||
let meta = json!({"format": "designcraft", "version": version, "generator": concat!("DesignCraft ", env!("CARGO_PKG_VERSION"))});
|
||||
z.write_all(meta.to_string().as_bytes()).map_err(|e| FormatError::Io(e.to_string()))?;
|
||||
if let Some(png) = preview {
|
||||
z.start_file(PREVIEW, stored).map_err(io)?;
|
||||
@@ -193,8 +200,8 @@ fn load_zip(bytes: &[u8]) -> Result<Document, FormatError> {
|
||||
let version = read_entry(&mut z, "meta.json", MAX_JSON_BYTES, &mut left)?
|
||||
.and_then(|meta| serde_json::from_slice::<Value>(&meta).ok())
|
||||
.and_then(|m| m.get("version").and_then(Value::as_u64))
|
||||
.map_or(1, |v| u32::try_from(v).unwrap_or(u32::MAX));
|
||||
if version > VERSION {
|
||||
.unwrap_or(1);
|
||||
if version > u64::from(VERSION) {
|
||||
return Err(FormatError::TooNew(version));
|
||||
}
|
||||
let json = read_entry(&mut z, "document.json", MAX_JSON_BYTES, &mut left)?.ok_or_else(|| FormatError::NotOurs("missing document.json".into()))?;
|
||||
@@ -226,7 +233,7 @@ fn load_zip(bytes: &[u8]) -> Result<Document, FormatError> {
|
||||
Arc::make_mut(a).data = data;
|
||||
}
|
||||
}
|
||||
upgrade(&mut doc, version);
|
||||
upgrade(&mut doc, u32::try_from(version).unwrap_or(u32::MAX));
|
||||
doc.merge_duplicate_assets();
|
||||
doc.mark_differing_margins_local();
|
||||
doc.check().map_err(|e| FormatError::NotOurs(e.to_string()))?;
|
||||
@@ -433,6 +440,39 @@ mod tests {
|
||||
buf.into_inner()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_source_requires_new_reader_but_preview_documents_keep_the_older_one() {
|
||||
let mut d = Document::new(&NewDocument::default());
|
||||
let version = |d: &Document| {
|
||||
let bytes = save(d).unwrap();
|
||||
let mut z = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();
|
||||
let mut meta = String::new();
|
||||
z.by_name("meta.json").unwrap().read_to_string(&mut meta).unwrap();
|
||||
serde_json::from_str::<Value>(&meta).unwrap()["version"].as_u64().unwrap()
|
||||
};
|
||||
assert_eq!(version(&d), 2);
|
||||
let aid = AssetId(d.alloc());
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n%%EOF\n".to_vec();
|
||||
d.assets.insert(
|
||||
aid,
|
||||
Arc::new(Asset {
|
||||
id: aid,
|
||||
name: "logo.eps".into(),
|
||||
mime: "image/png".into(),
|
||||
link: None,
|
||||
data: Arc::new(source.clone()),
|
||||
pixels: Some((40, 20)),
|
||||
page: 0,
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
place(&mut d, aid, 0.0);
|
||||
assert_eq!(version(&d), 3, "sniff source even when legacy metadata says PNG");
|
||||
assert_eq!(load(&save(&d).unwrap()).unwrap().assets[&aid].data.as_slice(), source);
|
||||
Arc::make_mut(d.assets.get_mut(&aid).unwrap()).data = Arc::new(b"\x89PNG\r\n\x1a\npreview".to_vec());
|
||||
assert_eq!(version(&d), 2, "legacy EPS previews stay compatible");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn roundtrip_with_assets() {
|
||||
let mut d = Document::new(&NewDocument { pages: 3, ..Default::default() });
|
||||
@@ -773,14 +813,16 @@ mod tests {
|
||||
#[test]
|
||||
fn rejects_garbage_and_newer_versions() {
|
||||
assert!(load(b"nope").is_err());
|
||||
let mut buf = Cursor::new(Vec::new());
|
||||
{
|
||||
let mut z = zip::ZipWriter::new(&mut buf);
|
||||
z.start_file("meta.json", zip::write::SimpleFileOptions::default()).unwrap();
|
||||
z.write_all(br#"{"version": 99}"#).unwrap();
|
||||
z.finish().unwrap();
|
||||
for version in [99u64, u64::from(u32::MAX) + 2] {
|
||||
let mut buf = Cursor::new(Vec::new());
|
||||
{
|
||||
let mut z = zip::ZipWriter::new(&mut buf);
|
||||
z.start_file("meta.json", zip::write::SimpleFileOptions::default()).unwrap();
|
||||
z.write_all(json!({"version": version}).to_string().as_bytes()).unwrap();
|
||||
z.finish().unwrap();
|
||||
}
|
||||
assert!(matches!(load(&buf.into_inner()), Err(FormatError::TooNew(v)) if v == version));
|
||||
}
|
||||
assert!(matches!(load(&buf.into_inner()), Err(FormatError::TooNew(99))));
|
||||
}
|
||||
|
||||
fn r7a_nullable_document(local_clear: bool, style_clear: bool) -> Document {
|
||||
|
||||
@@ -12,6 +12,7 @@ workspace = true
|
||||
designcraft-geom = { workspace = true }
|
||||
designcraft-color = { workspace = true }
|
||||
designcraft-doc = { workspace = true }
|
||||
designcraft-images = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
|
||||
@@ -923,6 +923,12 @@ impl<'a> Ex<'a> {
|
||||
if let Some(vj) = cs.vj {
|
||||
el.set("VerticalJustification", names::vj_out(vj));
|
||||
}
|
||||
if let Some(fb) = cs.first_baseline {
|
||||
el.set("FirstBaselineOffset", names::first_baseline_out(fb));
|
||||
}
|
||||
if let Some(min) = cs.first_baseline_min {
|
||||
el.set("MinimumFirstBaselineOffset", num(min));
|
||||
}
|
||||
if let Some(st) = &cs.stroke {
|
||||
for side in ["TopEdge", "LeftEdge", "BottomEdge", "RightEdge"] {
|
||||
self.cell_stroke_attrs(&mut el, side, st);
|
||||
@@ -2083,9 +2089,17 @@ impl<'a> Ex<'a> {
|
||||
|
||||
fn image_el(&mut self, asset: &designcraft_doc::Asset, g: &designcraft_doc::Graphic) -> El {
|
||||
let is_pdf = asset.mime == "application/pdf";
|
||||
let mut el = El::new(if is_pdf { "PDF" } else { "Image" }).attr("Self", self.fresh());
|
||||
let is_eps = asset.mime == "application/postscript" || designcraft_images::is_eps(&asset.data);
|
||||
let mut el = El::new(if is_eps {
|
||||
"EPS"
|
||||
} else if is_pdf {
|
||||
"PDF"
|
||||
} else {
|
||||
"Image"
|
||||
})
|
||||
.attr("Self", self.fresh());
|
||||
// A PDF is vector: its page size in `pixels` isn't a resolution, and a cropped graphic
|
||||
// spans less than the page.
|
||||
// spans less than the page. An EPS's `pixels` is its preview raster, so it still counts.
|
||||
if let Some((pw, ph)) = asset.pixels.filter(|_| !is_pdf)
|
||||
&& g.size.0 > 0.0
|
||||
&& g.size.1 > 0.0
|
||||
@@ -2125,7 +2139,7 @@ impl<'a> Ex<'a> {
|
||||
.attr("AssetURL", "$ID/")
|
||||
.attr("AssetID", "$ID/")
|
||||
.attr("LinkResourceURI", uri)
|
||||
.attr("LinkResourceFormat", format!("$ID/{}", format_name(&asset.mime)))
|
||||
.attr("LinkResourceFormat", format!("$ID/{}", format_name(if is_eps { "application/postscript" } else { &asset.mime })))
|
||||
.attr("StoredState", if embed { "Embedded" } else { "Normal" })
|
||||
.attr("LinkClassID", "35906")
|
||||
.attr("LinkClientID", "257")
|
||||
@@ -2762,6 +2776,8 @@ impl<'a> Ex<'a> {
|
||||
.attr("BottomInset", num(cell.insets[2]))
|
||||
.attr("RightInset", num(cell.insets[3]))
|
||||
.attr("VerticalJustification", vj)
|
||||
.attr("FirstBaselineOffset", names::first_baseline_out(cell.first_baseline))
|
||||
.attr("MinimumFirstBaselineOffset", num(cell.first_baseline_min))
|
||||
.attr("RotationAngle", num(cell.rotation));
|
||||
for (i, side) in ["TopEdge", "LeftEdge", "BottomEdge", "RightEdge"].iter().enumerate() {
|
||||
// Do not turn absent native defaults into explicit shared-edge candidates.
|
||||
@@ -3027,6 +3043,7 @@ fn format_name(mime: &str) -> &'static str {
|
||||
"image/gif" => "GIF",
|
||||
"image/tiff" => "TIFF",
|
||||
"application/pdf" => "Adobe Portable Document Format (PDF)",
|
||||
"application/postscript" => "Encapsulated PostScript (EPS)",
|
||||
_ => "Portable Network Graphics (PNG)",
|
||||
}
|
||||
}
|
||||
|
||||
+50
-12
@@ -391,6 +391,12 @@ fn nums(s: &str) -> Vec<f64> {
|
||||
s.split_whitespace().filter_map(|v| v.parse().ok()).collect()
|
||||
}
|
||||
|
||||
/// Like [`nums`], but empty when any number is NaN or infinite.
|
||||
fn finite_nums(s: &str) -> Vec<f64> {
|
||||
let v = nums(s);
|
||||
if v.iter().all(|x| x.is_finite()) { v } else { Vec::new() }
|
||||
}
|
||||
|
||||
/// The viewBox size of an SVG whose root has a viewBox but no width/height (percentages count as
|
||||
/// none): InDesign places such a file at one viewBox unit to the point.
|
||||
fn svg_viewbox_size(data: &[u8]) -> Option<(f64, f64)> {
|
||||
@@ -936,12 +942,16 @@ impl<'r> Importer<'r> {
|
||||
// Custom stroke styles.
|
||||
for e in top.iter().filter(|e| matches!(e.local(), "StripedStrokeStyle" | "DashedStrokeStyle" | "DottedStrokeStyle")) {
|
||||
let (Some(id), Some(name)) = (e.get("Self"), e.get("Name")) else { continue };
|
||||
let nums = |k: &str| e.get(k).unwrap_or("").split_whitespace().filter_map(|x| x.parse::<f64>().ok()).collect::<Vec<_>>();
|
||||
let kind = match e.local() {
|
||||
"StripedStrokeStyle" => designcraft_doc::StrokeType::Stripes {
|
||||
bands: nums("StripeArray").as_chunks::<2>().0.iter().map(|c| (c[0] / 100.0, (c[1] - c[0]) / 100.0)).collect(),
|
||||
bands: finite_nums(e.get("StripeArray").unwrap_or(""))
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|c| (c[0] / 100.0, (c[1] - c[0]) / 100.0))
|
||||
.collect(),
|
||||
},
|
||||
"DashedStrokeStyle" => designcraft_doc::StrokeType::Dashed { pattern: nums("DashArray") },
|
||||
"DashedStrokeStyle" => designcraft_doc::StrokeType::Dashed { pattern: finite_nums(e.get("DashArray").unwrap_or("")) },
|
||||
_ => designcraft_doc::StrokeType::Dotted,
|
||||
};
|
||||
let _ = id;
|
||||
@@ -1189,6 +1199,8 @@ impl<'r> Importer<'r> {
|
||||
cs.inset_overrides = [None; 4];
|
||||
}
|
||||
cs.vj = e.get("VerticalJustification").map(names::vj_in);
|
||||
cs.first_baseline = e.get("FirstBaselineOffset").map(names::first_baseline_in);
|
||||
cs.first_baseline_min = e.num("MinimumFirstBaselineOffset").filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 8640.0));
|
||||
cs.strokes = ["TopEdge", "LeftEdge", "BottomEdge", "RightEdge"].map(|side| self.cell_stroke_attrs(e, side));
|
||||
if let Some(stroke) = uniform_cell_stroke(&cs.strokes) {
|
||||
cs.stroke = Some(stroke);
|
||||
@@ -1830,7 +1842,7 @@ impl<'r> Importer<'r> {
|
||||
for li in tl.find_all("ListItem") {
|
||||
let get = |k: &str| li.find(k).map(|x| x.text_content());
|
||||
tabs.push(TabStop {
|
||||
position: get("Position").and_then(|v| v.trim().parse().ok()).unwrap_or(0.0),
|
||||
position: get("Position").and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite()).unwrap_or(0.0),
|
||||
align: names::tab_align_in(get("Alignment").unwrap_or_default().trim()),
|
||||
leader: get("Leader").unwrap_or_default(),
|
||||
align_on: get("AlignmentCharacter").filter(|c| c != ".").unwrap_or_default(),
|
||||
@@ -2189,8 +2201,8 @@ impl<'r> Importer<'r> {
|
||||
if r >= nr || c >= nc {
|
||||
continue;
|
||||
}
|
||||
let rs = (ce.num("RowSpan").unwrap_or(1.0).max(1.0) as usize).min(nr - r);
|
||||
let cs = (ce.num("ColumnSpan").unwrap_or(1.0).max(1.0) as usize).min(nc - c);
|
||||
let rs = (span(ce, "RowSpan").max(1.0) as usize).min(nr - r);
|
||||
let cs = (span(ce, "ColumnSpan").max(1.0) as usize).min(nc - c);
|
||||
let mut cell = t.cell(r, c).cloned().unwrap_or_default();
|
||||
let cell_style = ce.get("AppliedCellStyle").map(|r| self.cell_style_ref(r)).unwrap_or_else(|| designcraft_doc::NO_CELL_STYLE.into());
|
||||
if cell_style != designcraft_doc::NO_CELL_STYLE {
|
||||
@@ -2213,6 +2225,12 @@ impl<'r> Importer<'r> {
|
||||
if let Some(vj) = ce.get("VerticalJustification") {
|
||||
cell.vj = names::vj_in(vj);
|
||||
}
|
||||
if let Some(fb) = ce.get("FirstBaselineOffset") {
|
||||
cell.first_baseline = names::first_baseline_in(fb);
|
||||
}
|
||||
if let Some(min) = ce.num("MinimumFirstBaselineOffset").filter(|v| v.is_finite()) {
|
||||
cell.first_baseline_min = min.clamp(0.0, 8640.0);
|
||||
}
|
||||
cell.rotation = ce.num("RotationAngle").unwrap_or(cell.rotation);
|
||||
for (i, side) in ["TopEdge", "LeftEdge", "BottomEdge", "RightEdge"].iter().enumerate() {
|
||||
let attrs = self.cell_stroke_attrs(ce, side);
|
||||
@@ -2745,7 +2763,7 @@ impl<'r> Importer<'r> {
|
||||
if let Some(c) = color {
|
||||
s.swatch = self.swatch_ref(&c);
|
||||
}
|
||||
if let Some(w) = g(self, "StrokeWeight").and_then(|v| v.parse().ok()) {
|
||||
if let Some(w) = g(self, "StrokeWeight").and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()) {
|
||||
s.weight = w;
|
||||
}
|
||||
if let Some(t) = g(self, "StrokeTint").and_then(|v| v.parse().ok()) {
|
||||
@@ -2763,7 +2781,7 @@ impl<'r> Importer<'r> {
|
||||
if let Some(v) = g(self, "EndJoin") {
|
||||
s.join = names::join_in(&v);
|
||||
}
|
||||
if let Some(v) = g(self, "MiterLimit").and_then(|v| v.parse().ok()) {
|
||||
if let Some(v) = g(self, "MiterLimit").and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()) {
|
||||
s.miter_limit = v;
|
||||
}
|
||||
if let Some(v) = g(self, "LeftLineEnd") {
|
||||
@@ -2829,7 +2847,7 @@ impl<'r> Importer<'r> {
|
||||
{
|
||||
it.fill.gradient_angle = Some(a);
|
||||
}
|
||||
let start: Option<Vec<f64>> = e.prop("GradientFillStart").map(|s| s.split_whitespace().filter_map(|v| v.parse().ok()).collect());
|
||||
let start: Option<Vec<f64>> = e.prop("GradientFillStart").map(|s| finite_nums(&s));
|
||||
if let (Some([x, y]), Some(len)) = (start.as_deref().and_then(|v| <[f64; 2]>::try_from(v).ok()), e.num("GradientFillLength"))
|
||||
&& len > 0.0
|
||||
{
|
||||
@@ -2847,7 +2865,7 @@ impl<'r> Importer<'r> {
|
||||
{
|
||||
it.stroke.gradient_angle = Some(a);
|
||||
}
|
||||
let start: Option<Vec<f64>> = e.prop("GradientStrokeStart").map(|s| s.split_whitespace().filter_map(|v| v.parse().ok()).collect());
|
||||
let start: Option<Vec<f64>> = e.prop("GradientStrokeStart").map(|s| finite_nums(&s));
|
||||
if let (Some([x, y]), Some(len)) = (start.as_deref().and_then(|v| <[f64; 2]>::try_from(v).ok()), e.num("GradientStrokeLength"))
|
||||
&& len > 0.0
|
||||
{
|
||||
@@ -3066,6 +3084,7 @@ impl<'r> Importer<'r> {
|
||||
let size = svg_viewbox_size(&data).unwrap_or(size);
|
||||
let (mime, px) = if preview.is_some() { (None, None) } else { sniff_image(&data) };
|
||||
let mime = mime.map(str::to_string).unwrap_or_else(|| match link.and_then(|k| k.get("LinkResourceFormat")).unwrap_or("") {
|
||||
f if f.contains("EPS") || g.local() == "EPS" => "application/postscript".into(),
|
||||
f if f.contains("JPEG") => "image/jpeg".into(),
|
||||
f if f.contains("TIFF") => "image/tiff".into(),
|
||||
f if f.contains("PDF") || g.local() == "PDF" => "application/pdf".into(),
|
||||
@@ -3300,6 +3319,11 @@ fn overlay(base: &mut El, top: &El, depth: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
/// A cell span count; an infinite span is kept so it clamps to the table edge.
|
||||
fn span(e: &El, k: &str) -> f64 {
|
||||
e.prop(k).and_then(|v| v.trim().parse::<f64>().ok()).unwrap_or(1.0)
|
||||
}
|
||||
|
||||
fn finite(v: Option<f64>) -> Option<f64> {
|
||||
v.filter(|v| v.is_finite())
|
||||
}
|
||||
@@ -3710,9 +3734,23 @@ fn wrap_path_of(pg: &El) -> std::result::Result<PathData, &'static str> {
|
||||
Ok(PathData::new(subs))
|
||||
}
|
||||
|
||||
/// IDML link URI → file system path (`file:/a%20b` → `/a b`, `file:///C:/x` → `C:/x`).
|
||||
/// IDML link URI → file system path (`file:/a%20b` → `/a b`, `file:///C:/x` → `C:/x`). A URI
|
||||
/// naming another computer keeps it as a network path (`file://host/s/x` → `//host/s/x`).
|
||||
pub(crate) fn uri_to_path(uri: &str) -> String {
|
||||
let rest = uri.strip_prefix("file://").or_else(|| uri.strip_prefix("file:")).unwrap_or(uri);
|
||||
let rest = match uri.strip_prefix("file://") {
|
||||
Some(rest) => {
|
||||
let host_end = rest.find(['/', '\\']).unwrap_or(rest.len());
|
||||
let host = rest.get(..host_end).unwrap_or_default();
|
||||
if host.is_empty() || host.eq_ignore_ascii_case("localhost") {
|
||||
rest.get(host_end..).unwrap_or(rest)
|
||||
} else if matches!(host.as_bytes(), [letter, b':'] if letter.is_ascii_alphabetic()) {
|
||||
rest
|
||||
} else {
|
||||
return format!("//{}", percent_decode(rest));
|
||||
}
|
||||
}
|
||||
None => uri.strip_prefix("file:").unwrap_or(uri),
|
||||
};
|
||||
// `file:///C:/…` → `/C:/…` → `C:/…`
|
||||
let rest = match rest.as_bytes() {
|
||||
[b'/', _, b':', _, ..] => rest.get(1..).unwrap_or(rest),
|
||||
|
||||
@@ -126,8 +126,11 @@ pub(crate) fn base64_decode(s: &str) -> Vec<u8> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Sniff an image's MIME type and pixel size from its header (PNG, JPEG, GIF, WebP, TIFF).
|
||||
/// Sniff an image's MIME type and pixel size (PNG, JPEG, GIF, WebP, TIFF, PDF, EPS).
|
||||
pub(crate) fn sniff_image(b: &[u8]) -> (Option<&'static str>, Option<(u32, u32)>) {
|
||||
if designcraft_images::is_eps(b) {
|
||||
return (Some("application/postscript"), designcraft_images::eps_pixel_size(b));
|
||||
}
|
||||
let be32 = |i: usize| b.get(i..i + 4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]]));
|
||||
if b.starts_with(b"\x89PNG\r\n\x1a\n") {
|
||||
return (Some("image/png"), be32(16).zip(be32(20)));
|
||||
|
||||
@@ -263,7 +263,7 @@ pub fn leading_out(l: Leading) -> (String, &'static str) {
|
||||
}
|
||||
}
|
||||
pub fn leading_in(s: &str) -> Option<Leading> {
|
||||
if s == "Auto" { Some(Leading::Auto) } else { s.trim().parse().ok().map(Leading::Points) }
|
||||
if s == "Auto" { Some(Leading::Auto) } else { s.trim().parse::<f64>().ok().filter(|v| v.is_finite()).map(Leading::Points) }
|
||||
}
|
||||
|
||||
pub fn stroke_align_out(a: StrokeAlign) -> &'static str {
|
||||
|
||||
@@ -1022,6 +1022,10 @@ fn round_trips_overridden_parent_items() {
|
||||
fn uri_and_base64_helpers() {
|
||||
assert_eq!(import::uri_to_path("file:/a%20b/c.png"), "/a b/c.png");
|
||||
assert_eq!(import::uri_to_path("file:///C:/x/y.jpg"), "C:/x/y.jpg");
|
||||
assert_eq!(import::uri_to_path("file://localhost/a/b.png"), "/a/b.png");
|
||||
assert_eq!(import::uri_to_path("file://C:/x/y.jpg"), "C:/x/y.jpg");
|
||||
// Another computer stays one: a network path, not a folder named after it.
|
||||
assert_eq!(import::uri_to_path("file://192.0.2.1/s/x%20y.png"), "//192.0.2.1/s/x y.png");
|
||||
assert_eq!(export::path_to_uri("/a b/c.png"), "file:/a%20b/c.png");
|
||||
let data: Vec<u8> = (0..=255u8).cycle().take(1000).collect();
|
||||
assert_eq!(base64_decode(&base64_encode(&data)), data);
|
||||
@@ -4189,3 +4193,44 @@ mod liquid_page_rules {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Cell Options › Text › First Baseline, set by a cell style that a table style applies to its body
|
||||
/// region (cells with `[None]` take it from there) and overridden on one cell.
|
||||
const FIRST_BASELINE_STYLES: &str = r#"<idPkg:Styles xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
|
||||
<RootCellStyleGroup>
|
||||
<CellStyle Self="CellStyle/$ID/[None]" Name="$ID/[None]"/>
|
||||
<CellStyle Self="cs-cap" Name="Cap" FirstBaselineOffset="CapHeight" MinimumFirstBaselineOffset="2" VerticalJustification="CenterAlign"/>
|
||||
<CellStyle Self="cs-cap-child" Name="CapChild" FillTint="50"><Properties><BasedOn type="object">cs-cap</BasedOn></Properties></CellStyle>
|
||||
</RootCellStyleGroup>
|
||||
<RootParagraphStyleGroup/>
|
||||
<RootTableStyleGroup>
|
||||
<TableStyle Self="ts" Name="Tbl" BodyRegionCellStyle="cs-cap-child"/>
|
||||
</RootTableStyleGroup></idPkg:Styles>"#;
|
||||
|
||||
const FIRST_BASELINE_STORY: &str = r#"<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
|
||||
<Story Self="s1"><ParagraphStyleRange><CharacterStyleRange><Table AppliedTableStyle="ts" HeaderRowCount="0">
|
||||
<Row Name="0" MinimumHeight="20"/>
|
||||
<Column Name="0" SingleColumnWidth="60"/><Column Name="1" SingleColumnWidth="60"/>
|
||||
<Cell Name="0:0" AppliedCellStyle="CellStyle/$ID/[None]"><ParagraphStyleRange><CharacterStyleRange><Content>Region</Content></CharacterStyleRange></ParagraphStyleRange></Cell>
|
||||
<Cell Name="1:0" AppliedCellStyle="CellStyle/$ID/[None]" FirstBaselineOffset="LeadingOffset"><ParagraphStyleRange><CharacterStyleRange><Content>Local</Content></CharacterStyleRange></ParagraphStyleRange></Cell>
|
||||
</Table></CharacterStyleRange></ParagraphStyleRange></Story></idPkg:Story>"#;
|
||||
|
||||
#[test]
|
||||
fn cell_first_baseline_comes_from_cell_styles_table_regions_and_cells_and_round_trips() {
|
||||
use designcraft_doc::FirstBaseline;
|
||||
let d = import_idml(&fixture_with_table_styles(FIRST_BASELINE_STYLES, FIRST_BASELINE_STORY)).unwrap();
|
||||
d.check().unwrap();
|
||||
let style = d.styles.cell.iter().find(|c| c.name == "Cap").unwrap();
|
||||
assert_eq!((style.first_baseline, style.first_baseline_min), (Some(FirstBaseline::CapHeight), Some(2.0)));
|
||||
let table = d.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
|
||||
let region = table.cell(0, 0).unwrap();
|
||||
assert_eq!((region.first_baseline, region.first_baseline_min), (FirstBaseline::CapHeight, 2.0), "inherited through BasedOn and the body region");
|
||||
assert_eq!(table.cell(0, 1).unwrap().first_baseline, FirstBaseline::Leading, "a cell's own attribute wins");
|
||||
let back = import_idml(&export_idml(&d)).unwrap();
|
||||
let t2 = back.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
|
||||
assert_eq!(t2.cell(0, 0).unwrap().first_baseline, FirstBaseline::CapHeight);
|
||||
assert_eq!(t2.cell(0, 0).unwrap().first_baseline_min, 2.0);
|
||||
assert_eq!(t2.cell(0, 1).unwrap().first_baseline, FirstBaseline::Leading);
|
||||
let s2 = back.styles.cell.iter().find(|c| c.name == "Cap").unwrap();
|
||||
assert_eq!(s2.first_baseline, Some(FirstBaseline::CapHeight));
|
||||
}
|
||||
|
||||
+10
-1
@@ -93,8 +93,9 @@ impl El {
|
||||
pub fn prop_el(&self, k: &str) -> Option<&El> {
|
||||
self.find("Properties").and_then(|p| p.find(k))
|
||||
}
|
||||
/// A finite numeric property; `NaN` and infinities count as absent.
|
||||
pub fn num(&self, k: &str) -> Option<f64> {
|
||||
self.prop(k).and_then(|v| v.trim().parse().ok())
|
||||
self.prop(k).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite())
|
||||
}
|
||||
pub fn boolean(&self, k: &str) -> Option<bool> {
|
||||
match self.prop(k)?.trim() {
|
||||
@@ -337,4 +338,12 @@ mod tests {
|
||||
assert_eq!(e.num("Leading"), Some(14.0));
|
||||
assert_eq!(e.prop("Missing"), None);
|
||||
}
|
||||
|
||||
/// `NaN` and infinities parse as `f64` but are not usable numbers.
|
||||
#[test]
|
||||
fn non_finite_numbers_count_as_absent() {
|
||||
let x = br#"<A W="NaN" X="inf"><Properties><Y type="unit">-inf</Y><Z>2.5</Z></Properties></A>"#;
|
||||
let e = parse(x).unwrap();
|
||||
assert_eq!((e.num("W"), e.num("X"), e.num("Y"), e.num("Z")), (None, None, None, Some(2.5)));
|
||||
}
|
||||
}
|
||||
|
||||
+135
-16
@@ -27,34 +27,122 @@ fn sections(bytes: &[u8]) -> (&[u8], Option<&[u8]>) {
|
||||
pub fn bounding_box(bytes: &[u8]) -> Option<[f64; 4]> {
|
||||
let (ps, _) = sections(bytes);
|
||||
let head = String::from_utf8_lossy(&ps[..ps.len().min(64 * 1024)]);
|
||||
let read = |key: &str| {
|
||||
// DSC allows (atend): scan a bounded trailer as well, without reading the whole program.
|
||||
let tail = String::from_utf8_lossy(&ps[ps.len().saturating_sub(64 * 1024)..]);
|
||||
let header_value = |key: &str| {
|
||||
// Lines may end in `\r` alone (classic Mac files).
|
||||
head.split(['\n', '\r']).find_map(|l| {
|
||||
let v: Vec<f64> = l.strip_prefix(key)?.split_whitespace().filter_map(|t| t.parse().ok()).collect();
|
||||
// Finite corners and a finite size (subtracting extreme corners can overflow to infinity).
|
||||
let [x0, y0, x1, y1] = <[f64; 4]>::try_from(v).ok()?;
|
||||
let finite = [x0, y0, x1, y1, x1 - x0, y1 - y0].iter().all(|c| c.is_finite());
|
||||
(finite && x1 > x0 && y1 > y0).then_some([x0, y0, x1, y1])
|
||||
})
|
||||
head.split(['\n', '\r'])
|
||||
.take_while(|l| {
|
||||
(l.starts_with('%') || l.trim().is_empty())
|
||||
&& ![
|
||||
"%%EndComments",
|
||||
"%%BeginDocument",
|
||||
"%%BeginProlog",
|
||||
"%%BeginSetup",
|
||||
"%%BeginData",
|
||||
"%%BeginBinary",
|
||||
"%%BeginResource",
|
||||
"%%Page:",
|
||||
]
|
||||
.iter()
|
||||
.any(|k| l.starts_with(k))
|
||||
})
|
||||
.find_map(|l| l.strip_prefix(key))
|
||||
};
|
||||
read("%%HiResBoundingBox:").or_else(|| read("%%BoundingBox:"))
|
||||
let hires = header_value("%%HiResBoundingBox:");
|
||||
let bbox = header_value("%%BoundingBox:");
|
||||
let at_end = |v: Option<&str>| v.is_some_and(|v| v.trim() == "(atend)");
|
||||
// An outer header box must not be replaced by a nested document's trailer comments.
|
||||
// Inspect the trailer only when the outer header delegates its bounds there.
|
||||
hires
|
||||
.and_then(parse_box)
|
||||
.or_else(|| (at_end(hires) || at_end(bbox)).then(|| trailer_box(&tail, "%%HiResBoundingBox:")).flatten())
|
||||
.or_else(|| bbox.and_then(parse_box))
|
||||
.or_else(|| at_end(bbox).then(|| trailer_box(&tail, "%%BoundingBox:")).flatten())
|
||||
}
|
||||
|
||||
fn parse_box(value: &str) -> Option<[f64; 4]> {
|
||||
let mut words = value.split_whitespace();
|
||||
let mut bb = [0.0f64; 4];
|
||||
for n in &mut bb {
|
||||
*n = words.next()?.parse().ok()?;
|
||||
}
|
||||
// Finite corners and a finite size (subtracting extreme corners can overflow to infinity).
|
||||
let finite = [bb[0], bb[1], bb[2], bb[3], bb[2] - bb[0], bb[3] - bb[1]].iter().all(|n| n.is_finite());
|
||||
(words.next().is_none() && finite && bb[2] > bb[0] && bb[3] > bb[1]).then_some(bb)
|
||||
}
|
||||
|
||||
fn trailer_box(tail: &str, key: &str) -> Option<[f64; 4]> {
|
||||
// Work backwards from the outer EOF so nesting remains known even when the bounded
|
||||
// window begins inside a child document. Opaque DSC data/resource blocks are skipped too.
|
||||
let mut nested = 0usize;
|
||||
for line in tail.split(['\n', '\r']).rev() {
|
||||
if ["%%EndDocument", "%%EndData", "%%EndBinary", "%%EndResource"].iter().any(|k| line.starts_with(k)) {
|
||||
nested = nested.checked_add(1)?;
|
||||
} else if ["%%BeginDocument", "%%BeginData", "%%BeginBinary", "%%BeginResource"].iter().any(|k| line.starts_with(k)) {
|
||||
nested = nested.checked_sub(1)?;
|
||||
} else if nested == 0
|
||||
&& let Some(bb) = line.strip_prefix(key).and_then(parse_box)
|
||||
{
|
||||
return Some(bb);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Natural EPS dimensions in points. Bound sizes before fitting or allocating a proxy:
|
||||
/// sub-thousandth-point artwork and dimensions over 20,000 points aren't usable layouts.
|
||||
pub fn eps_size(bytes: &[u8]) -> Option<(f64, f64)> {
|
||||
let bb = bounding_box(bytes)?;
|
||||
let (w, h) = (bb[2] - bb[0], bb[3] - bb[1]);
|
||||
((0.001..=20_000.0).contains(&w) && (0.001..=20_000.0).contains(&h)).then_some((w, h))
|
||||
}
|
||||
|
||||
fn preview(bytes: &[u8]) -> Option<(&[u8], (u32, u32))> {
|
||||
let (_, tiff) = sections(bytes);
|
||||
let tiff = tiff?;
|
||||
let (w, h) = crate::pixel_size(tiff)?;
|
||||
// Match the decoder's allocation budget, including conversion to RGBA.
|
||||
(w > 0 && h > 0 && u64::from(w) * u64::from(h) <= 256 * 1024 * 1024 / 4).then_some((tiff, (w, h)))
|
||||
}
|
||||
|
||||
/// Proxy pixel dimensions, without encoding the placeholder just to read its size.
|
||||
pub fn eps_pixel_size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
let (w, h) = eps_size(bytes)?;
|
||||
Some(
|
||||
preview(bytes)
|
||||
.map(|(_, px)| px)
|
||||
.unwrap_or_else(|| ((w * 2.0).round().clamp(2.0, 4000.0) as u32, (h * 2.0).round().clamp(2.0, 4000.0) as u32)),
|
||||
)
|
||||
}
|
||||
|
||||
/// A browser-readable EPS preview. Keep the original TIFF for print exports (CMYK inks),
|
||||
/// but HTML and EPUB need PNG rather than a TIFF data URI or a mislabeled .png file.
|
||||
pub fn eps_png(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
let (proxy, _) = eps_proxy(bytes)?;
|
||||
if proxy.starts_with(b"\x89PNG\r\n\x1a\n") {
|
||||
return Some(proxy);
|
||||
}
|
||||
let rgba = crate::decode_rgba(&proxy)?;
|
||||
let mut png = Vec::new();
|
||||
image::DynamicImage::ImageRgba8(rgba).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).ok()?;
|
||||
Some(png)
|
||||
}
|
||||
|
||||
/// The proxy for a placed EPS: (image bytes, its size in points). The TIFF preview when present,
|
||||
/// else a light grey placeholder with a cross, at 2 pixels per point.
|
||||
pub fn eps_proxy(bytes: &[u8]) -> Option<(Vec<u8>, (f64, f64))> {
|
||||
let bb = bounding_box(bytes)?;
|
||||
let (w, h) = (bb[2] - bb[0], bb[3] - bb[1]);
|
||||
if let (_, Some(tiff)) = sections(bytes)
|
||||
&& crate::pixel_size(tiff).is_some()
|
||||
{
|
||||
let (w, h) = eps_size(bytes)?;
|
||||
if let Some((tiff, _)) = preview(bytes) {
|
||||
return Some((tiff.to_vec(), (w, h)));
|
||||
}
|
||||
// Simple PostScript drawn here: InDesign's QR codes are coloured rectangles only.
|
||||
if let Some(png) = render_rectangles(sections(bytes).0, bb) {
|
||||
if let Some(bb) = bounding_box(bytes)
|
||||
&& let Some(png) = render_rectangles(sections(bytes).0, bb)
|
||||
{
|
||||
return Some((png, (w, h)));
|
||||
}
|
||||
let (pw, ph) = ((w * 2.0).round().clamp(2.0, 4000.0) as u32, (h * 2.0).round().clamp(2.0, 4000.0) as u32);
|
||||
let (pw, ph) = eps_pixel_size(bytes)?;
|
||||
let mut img = image::RgbaImage::from_pixel(pw, ph, image::Rgba([228, 228, 228, 255]));
|
||||
// Diagonals and a border mark it as a stand-in.
|
||||
let n = pw.max(ph);
|
||||
@@ -228,6 +316,37 @@ fn render_rectangles(ps: &[u8], bb: [f64; 4]) -> Option<Vec<u8>> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn eps_rejects_unsafe_bounds_and_accepts_trailer_bounds() {
|
||||
for bounds in ["0 0 1e-320 1e-320", "0 0 1e308 1e308", "0 0 inf 10"] {
|
||||
let eps = format!("%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: {bounds}\n%%EOF\n");
|
||||
assert!(eps_proxy(eps.as_bytes()).is_none(), "{bounds}");
|
||||
}
|
||||
let mut eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: (atend)\n".to_vec();
|
||||
eps.extend(vec![b' '; 70_000]);
|
||||
eps.extend(b"\n%%Trailer\n%%BoundingBox: 0 0 20 10\n%%EOF\n");
|
||||
assert_eq!(eps_proxy(&eps).unwrap().1, (20.0, 10.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_nested_document_bounds_do_not_override_outer_artwork() {
|
||||
for (deferred, padding, child_padding) in [(false, 0, 0), (false, 8_000, 0), (true, 8_000, 0), (true, 0, 8_000)] {
|
||||
let mut eps =
|
||||
format!("%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: {}\n%%EndComments\n", if deferred { "(atend)" } else { "0 0 100 50" }).into_bytes();
|
||||
eps.extend(b"% padding\n".repeat(padding));
|
||||
eps.extend(b"%%BeginDocument: child.eps\n%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: (atend)\n%%EndComments\n");
|
||||
eps.extend(b"% child padding\n".repeat(child_padding));
|
||||
eps.extend(b"%%Trailer\n%%BoundingBox: 0 0 10 20\n%%HiResBoundingBox: 0 0 10 20\n%%EOF\n%%EndDocument\n");
|
||||
if deferred {
|
||||
eps.extend(b"%%Trailer\n%%BoundingBox: 0 0 100 50\n");
|
||||
}
|
||||
eps.extend(b"%%EOF\n");
|
||||
assert_eq!(eps_size(&eps), Some((100.0, 50.0)), "deferred={deferred}, padding={padding}, child_padding={child_padding}");
|
||||
}
|
||||
let eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: (atend)\n%%HiResBoundingBox: (atend)\n%%EndComments\nnewpath\n%%Trailer\n%%BoundingBox: 0 0 101 51\n%%HiResBoundingBox: 0 0 100.5 50.25\n%%EOF\n";
|
||||
assert_eq!(eps_size(eps), Some((100.5, 50.25)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_rectangle_eps_is_drawn_like_a_qr_code() {
|
||||
// As InDesign writes QR codes: a procedure for rectfill, a colour, then modules.
|
||||
|
||||
@@ -13,7 +13,7 @@ mod eps;
|
||||
mod jpeg;
|
||||
mod psd;
|
||||
mod tone;
|
||||
pub use eps::{bounding_box as eps_bounding_box, eps_proxy, is_eps};
|
||||
pub use eps::{bounding_box as eps_bounding_box, eps_pixel_size, eps_png, eps_proxy, eps_size, is_eps};
|
||||
pub use tone::{Tone, tone};
|
||||
|
||||
/// CSS pixels (SVG user units) → points.
|
||||
@@ -37,6 +37,9 @@ pub fn is_svg(bytes: &[u8]) -> bool {
|
||||
|
||||
/// MIME type of a placed graphic.
|
||||
pub fn mime(bytes: &[u8]) -> &'static str {
|
||||
if is_eps(bytes) {
|
||||
return "application/postscript";
|
||||
}
|
||||
if is_pdf(bytes) {
|
||||
return "application/pdf";
|
||||
}
|
||||
@@ -72,7 +75,11 @@ fn svg_options() -> &'static usvg::Options<'static> {
|
||||
db.set_sans_serif_family("Source Sans 3");
|
||||
db.set_monospace_family("JetBrains Mono");
|
||||
let font_resolver = usvg::FontResolver { select_font: select_font(), select_fallback: select_fallback() };
|
||||
usvg::Options { fontdb: Arc::new(db), font_family: "Source Sans 3".into(), font_resolver, ..Default::default() }
|
||||
// Images inside an SVG come only from `data:` URLs: an `href` naming a file is not read
|
||||
// (it is left out, as usvg leaves out an image it can't load).
|
||||
let image_href_resolver =
|
||||
usvg::ImageHrefResolver { resolve_data: usvg::ImageHrefResolver::default_data_resolver(), resolve_string: Box::new(|_, _| None) };
|
||||
usvg::Options { fontdb: Arc::new(db), font_family: "Source Sans 3".into(), font_resolver, image_href_resolver, ..Default::default() }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -510,6 +517,39 @@ mod tests {
|
||||
assert!(!is_svg(b"\x89PNG"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_images_come_only_from_data_urls() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-svg-href-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("red.png");
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255])).save(&file).unwrap();
|
||||
let blue = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10'%3E%3Crect width='10' height='10' fill='blue'/%3E%3C/svg%3E";
|
||||
let svg = format!(
|
||||
r#"<svg xmlns="http://www.w3.org/2000/svg" width="30" height="10">
|
||||
<image x="0" y="0" width="10" height="10" href="{}"/>
|
||||
<image x="10" y="0" width="10" height="10" href="{blue}"/>
|
||||
<image x="20" y="0" width="10" height="10" href="/etc/hostname"/>
|
||||
</svg>"#,
|
||||
file.display()
|
||||
);
|
||||
// The image nodes usvg kept: only the data URL's (PDF export embeds every one).
|
||||
fn images(g: &usvg::Group) -> usize {
|
||||
g.children()
|
||||
.iter()
|
||||
.map(|n| match n {
|
||||
usvg::Node::Group(g) => images(g),
|
||||
usvg::Node::Image(_) => 1,
|
||||
_ => 0,
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
assert_eq!(images(svg_tree(svg.as_bytes()).unwrap().root()), 1, "files the SVG names are not read");
|
||||
let (px, w, _) = render_svg(svg.as_bytes(), 30).unwrap();
|
||||
let at = |x: usize, y: usize| px[(y * w as usize + x) * 4..][..4].to_vec();
|
||||
assert_eq!(at(15, 5), [0, 0, 255, 255], "a data URL is drawn");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The family of the font that sets each text glyph of `svg`, and the glyph's id.
|
||||
fn glyph_families(svg: &str) -> Vec<(String, u16)> {
|
||||
fn walk(g: &usvg::Group, tree: &usvg::Tree, out: &mut Vec<(String, u16)>) {
|
||||
|
||||
@@ -254,7 +254,7 @@ fn export_pdf_with_report_inner(doc: &Document, cache: &Cache, opts: &PdfOptions
|
||||
}
|
||||
if opts.standard == Standard::PdfX4 {
|
||||
// Output intent and PDF/X identification, then check the result.
|
||||
match crate::pdfx::make_pdfx4(&bytes, &title) {
|
||||
match crate::pdfx::make_pdfx4(&bytes, &title, created) {
|
||||
Some(b) => bytes = b,
|
||||
None => warnings.push("PDF/X-4: the output intent couldn't be added".into()),
|
||||
}
|
||||
@@ -1318,7 +1318,18 @@ impl Exporter<'_> {
|
||||
return i.clone();
|
||||
}
|
||||
let asset = self.doc.assets.get(&id)?.clone();
|
||||
let data = asset.data.clone();
|
||||
// Use the same EPS preview as the renderer, preserving CMYK TIFF inks below. The
|
||||
// content cache is keyed on the bytes actually converted, so equal previews share one.
|
||||
let data = if designcraft_images::is_eps(&asset.data) {
|
||||
let Some((proxy, _)) = designcraft_images::eps_proxy(&asset.data) else {
|
||||
self.warn(format!("image `{}` could not be decoded and was skipped", asset.name));
|
||||
self.images.insert(id, None);
|
||||
return None;
|
||||
};
|
||||
Arc::new(proxy)
|
||||
} else {
|
||||
asset.data.clone()
|
||||
};
|
||||
if let Some(converted) = self.images_by_content.get(&data).cloned() {
|
||||
if let Some(warning) = converted.warning {
|
||||
self.warn(format!("{}: {warning}", asset.name));
|
||||
@@ -2036,3 +2047,74 @@ mod unwind_tests {
|
||||
println!("all three export entry points recovered");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod eps_tests {
|
||||
use super::*;
|
||||
use hayro_syntax::object::Name;
|
||||
|
||||
#[test]
|
||||
fn eps_tiff_preview_keeps_cmyk_inks_in_pdf() {
|
||||
let pixels = vec![0u8, 0, 0, 255, 0, 255, 255, 0];
|
||||
let mut tiff = Vec::new();
|
||||
tiff::encoder::TiffEncoder::new(std::io::Cursor::new(&mut tiff))
|
||||
.unwrap()
|
||||
.write_image::<tiff::encoder::colortype::CMYK8>(2, 1, &pixels)
|
||||
.unwrap();
|
||||
let ps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 2 1\n%%EOF\n";
|
||||
let mut eps = vec![0xC5, 0xD0, 0xD3, 0xC6];
|
||||
for value in [30usize, ps.len(), 0, 0, 30 + ps.len(), tiff.len()] {
|
||||
eps.extend((value as u32).to_le_bytes());
|
||||
}
|
||||
eps.extend([0xFF, 0xFF]);
|
||||
eps.extend_from_slice(ps);
|
||||
eps.extend_from_slice(&tiff);
|
||||
assert_eq!(designcraft_images::eps_proxy(&eps).unwrap().0, tiff);
|
||||
let mut doc = Document::new(&designcraft_doc::build::NewDocument::default());
|
||||
let aid = AssetId(doc.alloc());
|
||||
doc.assets.insert(
|
||||
aid,
|
||||
Arc::new(designcraft_doc::Asset {
|
||||
id: aid,
|
||||
name: "cmyk.eps".into(),
|
||||
mime: "application/postscript".into(),
|
||||
link: None,
|
||||
data: Arc::new(eps),
|
||||
pixels: Some((2, 1)),
|
||||
page: 0,
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
let mut item = Item::new(
|
||||
designcraft_doc::ItemId(doc.alloc()),
|
||||
doc.default_layer(),
|
||||
designcraft_doc::Shape::Rectangle,
|
||||
designcraft_geom::shapes::rectangle(Rect::new(72.0, 72.0, 272.0, 172.0)),
|
||||
);
|
||||
item.content = Content::Graphic(designcraft_doc::Graphic {
|
||||
asset: aid,
|
||||
size: (2.0, 1.0),
|
||||
xf: Affine::translate((72.0, 72.0)) * Affine::scale(100.0),
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [0.0; 4],
|
||||
clip: None,
|
||||
wrap: Default::default(),
|
||||
fill: Default::default(),
|
||||
});
|
||||
doc.insert_item(designcraft_doc::SpreadRef::Doc(0), item, None).unwrap();
|
||||
for standard in [Standard::None, Standard::PdfX4] {
|
||||
let report = export_pdf_with_report(&doc, &Cache::new(), &PdfOptions { standard, ..Default::default() }).unwrap();
|
||||
assert!(report.warnings.is_empty(), "{:?}", report.warnings);
|
||||
let pdf = hayro_syntax::Pdf::new(report.bytes).unwrap();
|
||||
let images: Vec<_> = pdf
|
||||
.objects()
|
||||
.into_iter()
|
||||
.filter_map(|o| o.into_stream())
|
||||
.filter(|s| s.dict().get::<Name>("Subtype").is_some_and(|n| n.as_str() == "Image"))
|
||||
.map(|s| (s.dict().get::<Name>("ColorSpace").unwrap().as_str().to_string(), s.decoded().unwrap().into_owned()))
|
||||
.collect();
|
||||
assert_eq!(images, vec![("DeviceCMYK".to_string(), pixels.clone())], "{standard:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+20
-5
@@ -23,9 +23,11 @@ fn lit(s: &str) -> String {
|
||||
o
|
||||
}
|
||||
|
||||
/// Turn krilla's output into PDF/X-4. `None` when the file isn't the simple kind this
|
||||
/// understands (a classic cross-reference table and plain dictionaries).
|
||||
pub fn make_pdfx4(pdf: &[u8], title: &str) -> Option<Vec<u8>> {
|
||||
/// Turn krilla's output into PDF/X-4. `date` (Unix seconds, UTC) is the XMP metadata date; `None`
|
||||
/// (no clock, as on wasm) writes the epoch, because PDF/X-4 requires the entry. `None` back when
|
||||
/// the file isn't the simple kind this understands (a classic cross-reference table and plain
|
||||
/// dictionaries).
|
||||
pub fn make_pdfx4(pdf: &[u8], title: &str, date: Option<i64>) -> Option<Vec<u8>> {
|
||||
let t_at = rfind(pdf, b"trailer")?;
|
||||
let trailer = String::from_utf8_lossy(&pdf[t_at..]).to_string();
|
||||
let size = int_after(&trailer, "/Size")?;
|
||||
@@ -84,7 +86,8 @@ pub fn make_pdfx4(pdf: &[u8], title: &str) -> Option<Vec<u8>> {
|
||||
i
|
||||
}
|
||||
};
|
||||
// XMP: the PDF/X identification schema, and the trapping state that Info also states.
|
||||
// XMP: the PDF/X identification schema, the trapping state that Info also states, and the
|
||||
// metadata date and version ID that PDF/X-4 requires.
|
||||
if let Some(meta) = int_after(&cat, "/Metadata") {
|
||||
let r = object(pdf, meta)?;
|
||||
let body = &pdf[r];
|
||||
@@ -96,11 +99,17 @@ pub fn make_pdfx4(pdf: &[u8], title: &str) -> Option<Vec<u8>> {
|
||||
if dict.contains("/Filter") {
|
||||
return None;
|
||||
}
|
||||
let (y, mo, d, h, mi, se) = crate::export::civil(date.unwrap_or(0));
|
||||
let stamp = format!("{y:04}-{mo:02}-{d:02}T{h:02}:{mi:02}:{se:02}Z");
|
||||
let add = concat!(
|
||||
"<rdf:Description rdf:about=\"\" xmlns:pdfxid=\"http://www.npes.org/pdfx/ns/id/\"><pdfxid:GTS_PDFXVersion>PDF/X-4</pdfxid:GTS_PDFXVersion></rdf:Description>",
|
||||
"<rdf:Description rdf:about=\"\" xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"><pdf:Trapped>False</pdf:Trapped></rdf:Description>"
|
||||
);
|
||||
let xml = xml.replacen("</rdf:RDF>", &format!("{add}</rdf:RDF>"), 1);
|
||||
let dates = format!(
|
||||
"<rdf:Description rdf:about=\"\" xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\" xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\">\
|
||||
<xmp:MetadataDate>{stamp}</xmp:MetadataDate><xmpMM:VersionID>1</xmpMM:VersionID></rdf:Description>"
|
||||
);
|
||||
let xml = xml.replacen("</rdf:RDF>", &format!("{add}{dates}</rdf:RDF>"), 1);
|
||||
let dict = {
|
||||
let i = dict.find("/Length")?;
|
||||
let rest = &dict[i + 7..];
|
||||
@@ -216,6 +225,12 @@ pub fn check_pdfx4(pdf: &[u8]) -> Vec<String> {
|
||||
if !text.contains("pdfxid:GTS_PDFXVersion>PDF/X-4<") {
|
||||
issues.push("the XMP metadata doesn't name PDF/X-4".into());
|
||||
}
|
||||
if !text.contains("<xmp:MetadataDate>") {
|
||||
issues.push("the XMP metadata has no xmp:MetadataDate".into());
|
||||
}
|
||||
if !text.contains("<xmpMM:VersionID>") {
|
||||
issues.push("the XMP metadata has no xmpMM:VersionID".into());
|
||||
}
|
||||
if text.contains("/Interpolate true") {
|
||||
issues.push("an image asks for interpolation".into());
|
||||
}
|
||||
|
||||
@@ -163,6 +163,20 @@ fn spreads_and_ranges() {
|
||||
assert_eq!(export_pdf(&d, &Cache::new(), &PdfOptions { pages: Some(vec![9]), ..Default::default() }), Err(PdfError::BadPage(10)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdfx4_xmp_has_metadata_date_and_version_id() {
|
||||
let d = doc_with_text("Print me");
|
||||
let opts = PdfOptions { standard: Standard::PdfX4, created: Some(1_700_000_000), ..Default::default() };
|
||||
let r = export_pdf_with_report(&d, &Cache::new(), &opts).unwrap();
|
||||
let text = String::from_utf8_lossy(&r.bytes).to_string();
|
||||
assert!(text.contains("<xmp:MetadataDate>2023-11-14T22:13:20Z</xmp:MetadataDate>"), "metadata date");
|
||||
assert!(text.contains("<xmpMM:VersionID>1</xmpMM:VersionID>"), "version id");
|
||||
assert!(crate::check_pdfx4(&r.bytes).is_empty());
|
||||
// Without the two entries the check says so.
|
||||
let stripped = text.replace("xmp:MetadataDate", "xmp:Other").replace("xmpMM:VersionID", "xmpMM:Other");
|
||||
assert_eq!(crate::check_pdfx4(stripped.as_bytes()).iter().filter(|i| i.contains("xmp")).count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standards() {
|
||||
let d = doc_with_text("Archive me");
|
||||
|
||||
@@ -1140,6 +1140,9 @@ pub fn decode_pixmap(bytes: &[u8]) -> Option<Pixmap> {
|
||||
|
||||
/// [`decode_pixmap`] for page `page` of a PDF (other formats have one page).
|
||||
pub fn decode_pixmap_page(bytes: &[u8], page: u32) -> Option<Pixmap> {
|
||||
// Documents retain the EPS source; only display its TIFF preview or placeholder.
|
||||
let proxy = if designcraft_images::is_eps(bytes) { Some(designcraft_images::eps_proxy(bytes)?.0) } else { None };
|
||||
let bytes = proxy.as_deref().unwrap_or(bytes);
|
||||
if is_pdf(bytes) {
|
||||
return render_pdf_page(bytes, page as usize, 3000);
|
||||
}
|
||||
@@ -1200,6 +1203,9 @@ fn now() -> u64 {
|
||||
|
||||
/// Pixel size of an encoded image without decoding it fully.
|
||||
pub fn image_size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
if designcraft_images::is_eps(bytes) {
|
||||
return designcraft_images::eps_pixel_size(bytes);
|
||||
}
|
||||
if is_pdf(bytes) {
|
||||
// Placed PDFs are sized by their page (crop box) in points.
|
||||
return pdf_page_size(bytes, 0).map(|(w, h)| (w.round().max(1.0) as u32, h.round().max(1.0) as u32));
|
||||
@@ -1212,7 +1218,8 @@ pub fn image_size(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
}
|
||||
|
||||
/// Natural size of a placed graphic, unrounded: a PDF page (crop box) and an SVG in points, a raster
|
||||
/// in pixels. Placed graphics are drawn at this size; `image_size` rounds it to whole pixels for display.
|
||||
/// in pixels, an EPS its bounding box independently of preview pixels. Placed graphics are drawn at
|
||||
/// this size; `image_size` rounds it to whole pixels for display.
|
||||
pub fn natural_size(bytes: &[u8], page: usize) -> Option<(f64, f64)> {
|
||||
if is_pdf(bytes) {
|
||||
return pdf_page_size(bytes, page).map(|(w, h)| (w.max(1e-3), h.max(1e-3)));
|
||||
@@ -1468,6 +1475,34 @@ mod tests {
|
||||
use designcraft_doc::build::NewDocument;
|
||||
use designcraft_doc::{Fill, ParaFormat};
|
||||
|
||||
#[test]
|
||||
fn eps_source_decodes_like_its_preview() {
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 20 10\n%%EOF\n";
|
||||
let mut tiff = Vec::new();
|
||||
image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(3, 2, image::Rgba([255, 0, 0, 128])))
|
||||
.write_to(&mut std::io::Cursor::new(&mut tiff), image::ImageFormat::Tiff)
|
||||
.unwrap();
|
||||
let mut dos = vec![0xC5, 0xD0, 0xD3, 0xC6];
|
||||
for value in [30usize, source.len(), 0, 0, 30 + source.len(), tiff.len()] {
|
||||
dos.extend((value as u32).to_le_bytes());
|
||||
}
|
||||
dos.extend([0xFF, 0xFF]);
|
||||
dos.extend_from_slice(source);
|
||||
dos.extend_from_slice(&tiff);
|
||||
for source in [source.as_slice(), dos.as_slice()] {
|
||||
let proxy = designcraft_images::eps_proxy(source).unwrap().0;
|
||||
let actual = decode_pixmap(source).unwrap();
|
||||
let expected = decode_pixmap(&proxy).unwrap();
|
||||
assert_eq!((actual.width(), actual.height()), (expected.width(), expected.height()));
|
||||
assert_eq!(actual.data(), expected.data());
|
||||
assert_eq!(image_size(source), image_size(&proxy));
|
||||
assert_eq!(image_mime(source), "application/postscript");
|
||||
}
|
||||
let corrupt = b"%!PS-Adobe-3.0 EPSF-3.0\nno bounding box\n";
|
||||
assert!(decode_pixmap(corrupt).is_none());
|
||||
assert!(image_size(corrupt).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footnote_vector_objects_are_painted() {
|
||||
use designcraft_doc::{AnchorPosition, AnchoredObject, ItemId, Shape, SpreadRef};
|
||||
|
||||
@@ -48,7 +48,7 @@ impl Bounds {
|
||||
}
|
||||
let Some(clip) = self.clips.last() else { return };
|
||||
let r = r.intersect(*clip);
|
||||
if !(r.width() > 0.0 && r.height() > 0.0) || ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) {
|
||||
if !(r.width() > 0.0 && r.height() > 0.0 && [r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite())) {
|
||||
return;
|
||||
}
|
||||
self.out = Some(self.out.map_or(r, |o| o.union(r)));
|
||||
|
||||
@@ -180,7 +180,7 @@ fn run_attrs(rpr: Option<&El>) -> CharAttrs {
|
||||
if let Some(s) = r.on("strike") {
|
||||
a.strikethrough = Some(s);
|
||||
}
|
||||
if let Some(sz) = r.val("sz").and_then(|v| v.parse::<f64>().ok()) {
|
||||
if let Some(sz) = r.val("sz").and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()) {
|
||||
a.size = Some(sz / 2.0);
|
||||
}
|
||||
if let Some(f) = r.child("rFonts").and_then(|f| f.attr("ascii").or(f.attr("hAnsi"))) {
|
||||
@@ -203,7 +203,7 @@ fn run_attrs(rpr: Option<&El>) -> CharAttrs {
|
||||
fn para_attrs(ppr: Option<&El>) -> ParaAttrs {
|
||||
let mut a = ParaAttrs::default();
|
||||
let Some(p) = ppr else { return a };
|
||||
let tw = |v: Option<&str>| v.and_then(|v| v.parse::<f64>().ok()).map(|v| v / 20.0);
|
||||
let tw = |v: Option<&str>| v.and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()).map(|v| v / 20.0);
|
||||
if let Some(sp) = p.child("spacing") {
|
||||
a.space_before = tw(sp.attr("before"));
|
||||
a.space_after = tw(sp.attr("after"));
|
||||
@@ -680,6 +680,28 @@ mod tests {
|
||||
assert_eq!(st.text, "Date: 1 May 2026.\nLink");
|
||||
}
|
||||
|
||||
/// `str::parse::<f64>` accepts "NaN"/"inf"; sizes, spacing and indents must ignore them.
|
||||
#[test]
|
||||
fn non_finite_sizes_spacing_and_indents_are_ignored() {
|
||||
let document = format!(
|
||||
r#"<w:document {W}><w:body>
|
||||
<w:p><w:pPr><w:spacing w:before="NaN" w:after="inf"/><w:ind w:left="-inf" w:right="NaN" w:firstLine="-inf"/></w:pPr>
|
||||
<w:r><w:rPr><w:sz w:val="NaN"/></w:rPr><w:t>a</w:t></w:r><w:r><w:rPr><w:sz w:val="inf"/></w:rPr><w:t>b</w:t></w:r></w:p>
|
||||
<w:p><w:pPr><w:ind w:hanging="inf"/></w:pPr><w:r><w:t>c</w:t></w:r></w:p>
|
||||
</w:body></w:document>"#
|
||||
);
|
||||
let st = import(&docx(&document, "", "")).unwrap().story;
|
||||
let fin = |v: Option<f64>| v.is_none_or(f64::is_finite);
|
||||
for p in &st.paras {
|
||||
let p = &p.para;
|
||||
assert!(fin(p.space_before) && fin(p.space_after), "{p:?}");
|
||||
assert!(fin(p.left_indent) && fin(p.right_indent) && fin(p.first_line_indent), "{p:?}");
|
||||
}
|
||||
for at in [0, 1] {
|
||||
assert!(fin(st.format_after(at).over.size), "{:?}", st.format_after(at).over.size);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracked_changes_import_as_accepted_including_moves() {
|
||||
// Regression (#256): text in a tracked move destination (`w:moveTo`) vanished on import.
|
||||
|
||||
@@ -216,7 +216,10 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
|
||||
"ul" => restyle(&|s| s.underline = on),
|
||||
"ulnone" => restyle(&|s| s.underline = false),
|
||||
"strike" => restyle(&|s| s.strike = on),
|
||||
"fs" => restyle(&|s| s.size = param.map(|p| p as f64 / 2.0)),
|
||||
// Half-points; sizes outside the engine's 0.1–1296 pt range fall back to the default.
|
||||
"fs" => restyle(&|s| {
|
||||
s.size = param.map(|p| p as f64 / 2.0).filter(|pt| (0.1..=1296.0).contains(pt));
|
||||
}),
|
||||
"plain" => restyle(&|s| {
|
||||
s.bold = false;
|
||||
s.italic = false;
|
||||
@@ -297,6 +300,18 @@ Caf\'e9 \u8364? price\tab tab\line next\par
|
||||
assert!(!st.text.contains("Times"), "font table skipped");
|
||||
}
|
||||
|
||||
/// `\fs` sizes outside the engine's 0.1–1296 pt character size range are not stored.
|
||||
#[test]
|
||||
fn font_size_out_of_range_is_not_stored() {
|
||||
let i = import(br"{\rtf1\ansi\fs0 A\fs-8 B\fs999999999 C\fs24 D}").unwrap();
|
||||
let st = &i.story;
|
||||
for ch in ["A", "B", "C"] {
|
||||
let size = st.format_after(st.text.find(ch).unwrap()).over.size;
|
||||
assert!(size.is_none_or(|s| (0.1..=1296.0).contains(&s)), "{ch}: {size:?}");
|
||||
}
|
||||
assert_eq!(st.format_after(st.text.find('D').unwrap()).over.size, Some(12.0));
|
||||
}
|
||||
|
||||
/// A '-' right after a control word is text unless a digit follows it (then it is a negative
|
||||
/// parameter). It used to be swallowed.
|
||||
#[test]
|
||||
|
||||
@@ -170,7 +170,7 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
|
||||
}
|
||||
"cFont" => fmt.over.font_family = (!value.is_empty()).then(|| value.to_string()),
|
||||
"cTypeface" => fmt.over.font_style = (!value.is_empty()).then(|| value.to_string()),
|
||||
"cSize" => fmt.over.size = value.trim().parse().ok(),
|
||||
"cSize" => fmt.over.size = value.trim().parse().ok().filter(|s: &f64| s.is_finite()),
|
||||
"DefineParaStyle" | "DefineCharStyle" => {
|
||||
let nm = value.split('=').next().unwrap_or("").trim().to_string();
|
||||
let list = if name == "DefineParaStyle" { &mut para_styles } else { &mut char_styles };
|
||||
@@ -245,6 +245,17 @@ mod tests {
|
||||
assert_eq!(import("<ASCII-WIN>\r\n<ParaStyle:Body>A<0xD83D>B<0xDE00>C".as_bytes()).unwrap().story.text, "ABC");
|
||||
}
|
||||
|
||||
/// `f64::from_str` accepts "NaN" and "inf"; a non-finite size must not reach the story.
|
||||
#[test]
|
||||
fn non_finite_char_size_is_ignored() {
|
||||
for v in ["NaN", "inf", "infinity", "-inf"] {
|
||||
let src = format!("<ASCII-WIN>\r\n<ParaStyle:Body><cSize:{v}>big<cSize:>");
|
||||
let i = import(src.as_bytes()).unwrap();
|
||||
let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size;
|
||||
assert!(size.is_none_or(f64::is_finite), "{v}: size {size:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tagged_text_keeps_a_leading_empty_paragraph() {
|
||||
let doc = Document::new(&designcraft_doc::build::NewDocument::default());
|
||||
|
||||
@@ -35,11 +35,23 @@ fn first_sheet_target(wb: &str, rels: &str) -> Option<String> {
|
||||
Some(if target.starts_with("xl/") { target } else { format!("xl/{target}") })
|
||||
}
|
||||
|
||||
/// The text of a shared string (`<si>`) or inline string (`<is>`): its own `<t>` and the `<t>` of its
|
||||
/// `<r>` runs. Phonetic guides (`<rPh>`, `<phoneticPr>`) are the reading, not the cell text.
|
||||
fn string_item(si: &El) -> String {
|
||||
si.els()
|
||||
.map(|e| match e.name.as_str() {
|
||||
"t" => e.text(),
|
||||
"r" => e.els().filter(|t| t.name == "t").map(El::text).collect(),
|
||||
_ => String::new(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn value(c: &El, shared: &[String]) -> String {
|
||||
let v = || c.child("v").map(El::text).unwrap_or_default();
|
||||
match c.attr("t") {
|
||||
Some("s") => v().trim().parse::<usize>().ok().and_then(|i| shared.get(i).cloned()).unwrap_or_default(),
|
||||
Some("inlineStr") => c.child("is").map(El::text).unwrap_or_default(),
|
||||
Some("inlineStr") => c.child("is").map(string_item).unwrap_or_default(),
|
||||
// A cached formula string is text even when it looks like a number.
|
||||
Some("str") => v(),
|
||||
Some("b") => {
|
||||
@@ -90,7 +102,7 @@ pub fn records(bytes: &[u8], sheet: Option<&str>) -> Result<Vec<Vec<String>>, Im
|
||||
let shared: Vec<String> = part(&mut zip, "xl/sharedStrings.xml")?
|
||||
.map(|x| parse(&x))
|
||||
.transpose()?
|
||||
.map(|sst| sst.els().filter(|e| e.name == "si").map(El::text).collect())
|
||||
.map(|sst| sst.els().filter(|e| e.name == "si").map(string_item).collect())
|
||||
.unwrap_or_default();
|
||||
let mut cells: Vec<(usize, usize, String)> = Vec::new();
|
||||
if let Some(data) = sheet_xml.child("sheetData") {
|
||||
@@ -198,7 +210,7 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
|
||||
let shared: Vec<String> = part(&mut zip, "xl/sharedStrings.xml")?
|
||||
.map(|x| parse(&x))
|
||||
.transpose()?
|
||||
.map(|sst| sst.els().filter(|e| e.name == "si").map(El::text).collect())
|
||||
.map(|sst| sst.els().filter(|e| e.name == "si").map(string_item).collect())
|
||||
.unwrap_or_default();
|
||||
let mut cells: Vec<((usize, usize), String)> = Vec::new();
|
||||
if let Some(data) = sheet.child("sheetData") {
|
||||
@@ -355,6 +367,31 @@ mod tests {
|
||||
assert!(records(&book, Some("Missing")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phonetic_guide_text_is_not_cell_text() {
|
||||
let book = sheets(&[
|
||||
(
|
||||
"xl/workbook.xml",
|
||||
r#"<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="S" sheetId="1" r:id="rId1"/></sheets></workbook>"#,
|
||||
),
|
||||
("xl/_rels/workbook.xml.rels", r#"<Relationships><Relationship Id="rId1" Type="x" Target="worksheets/sheet1.xml"/></Relationships>"#),
|
||||
(
|
||||
"xl/sharedStrings.xml",
|
||||
r#"<sst><si><t>日本</t><rPh sb="0" eb="2"><t>ニホン</t></rPh><phoneticPr fontId="1"/></si><si><r><t>東</t></r><r><t>京</t></r><rPh sb="0" eb="2"><t>トウキョウ</t></rPh></si></sst>"#,
|
||||
),
|
||||
(
|
||||
"xl/worksheets/sheet1.xml",
|
||||
r#"<worksheet><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="inlineStr"><is><t>大阪</t><rPh sb="0" eb="2"><t>オオサカ</t></rPh><phoneticPr/></is></c></row></sheetData></worksheet>"#,
|
||||
),
|
||||
]);
|
||||
let want = ["日本", "東京", "大阪"].map(String::from).to_vec();
|
||||
assert_eq!(records(&book, None).unwrap(), vec![want.clone()]);
|
||||
let imp = import(&book).unwrap();
|
||||
let t = imp.story.tables.values().next().expect("a table");
|
||||
let got: Vec<String> = (0..3).map(|c| t.cell(0, c).unwrap().text.text.clone()).collect();
|
||||
assert_eq!(got, want);
|
||||
}
|
||||
|
||||
fn one_value(cell: &str) -> Vec<u8> {
|
||||
workbook_with(&format!(
|
||||
r#"<worksheet><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>Value</t></is></c></row><row r="2">{cell}</row></sheetData></worksheet>"#
|
||||
|
||||
@@ -32,7 +32,7 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
pub use catalog::{SPLIT_IN_TWO_COLUMNS, TOOL_GROUPS, ToolInfo, tool_for_shortcut, tool_info};
|
||||
pub use layout::CanvasLayout;
|
||||
pub use layout::{CanvasLayout, SpreadSlot};
|
||||
pub use nav::ZoomTool;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
||||
@@ -434,8 +434,8 @@ impl Tool for PageTool {
|
||||
p: d.cur,
|
||||
text: format!(
|
||||
"W: {}\nH: {}",
|
||||
designcraft_geom::format_measure(pg.width, cx.unit),
|
||||
designcraft_geom::format_measure(pg.height, cx.unit)
|
||||
designcraft_geom::format_measure(pg.width, cx.doc.settings.horizontal_units),
|
||||
designcraft_geom::format_measure(pg.height, cx.doc.settings.vertical_units)
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use designcraft_compose as compose;
|
||||
use designcraft_doc::{Content, Document, Item, SpreadRef};
|
||||
use designcraft_engine::doc::Selection;
|
||||
use designcraft_geom::{Affine, Point, Rect as DRect, Vec2 as DVec2};
|
||||
use designcraft_tools::{CanvasLayout, Cursor, Mods, Overlay, PointerEvent, PointerKind, ToolKey};
|
||||
use designcraft_tools::{CanvasLayout, Cursor, Mods, Overlay, PointerEvent, PointerKind, SpreadSlot, ToolKey};
|
||||
use egui::{Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -477,7 +477,10 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
layout.slots = layout.slots.get(cur).copied().into_iter().collect();
|
||||
}
|
||||
let painter = ui.painter_at(screen);
|
||||
let preview = matches!(app.ui.screen_mode, ScreenMode::Preview | ScreenMode::Presentation);
|
||||
// Preview, Bleed, Slug and Presentation are all preview modes: guides, frame edges and the
|
||||
// other non-printing marks go, and only the shown area of each page keeps its content. They
|
||||
// differ in how much of the page shows, which `shown_area` gives.
|
||||
let preview = app.ui.screen_mode != ScreenMode::Normal;
|
||||
let bg = if app.ui.screen_mode == ScreenMode::Presentation { Color32::BLACK } else { t.pasteboard };
|
||||
painter.rect_filled(screen, 0.0, bg);
|
||||
// Page shadow: a hard 1.5 pt black offset on the right and bottom (InDesign 2026).
|
||||
@@ -514,9 +517,9 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
}
|
||||
mesh.indices.extend([0, 1, 2, 0, 2, 3]);
|
||||
if preview {
|
||||
// Preview: only page areas (trim) show content.
|
||||
// Preview: only the shown area of each spread keeps its content.
|
||||
for slot in &layout.slots {
|
||||
let r = xf.rect(slot.bounds);
|
||||
let r = shown_area(app, &doc, &xf, slot);
|
||||
painter.with_clip_rect(r.intersect(screen)).add(egui::Shape::mesh(mesh.clone()));
|
||||
}
|
||||
} else {
|
||||
@@ -933,6 +936,20 @@ fn baseline_grid_lines(start: f64, increment: f64, height: f64) -> impl Iterator
|
||||
(0..MAX_BASELINES).map(move |n| first + n as f64 * step).take_while(move |y| *y < height)
|
||||
}
|
||||
|
||||
/// What a screen mode shows of a spread, in screen coordinates: the trim, or out to the bleed or
|
||||
/// the slug box. Slug shows every printing element, so the bleed as well — and a slug narrower
|
||||
/// than the bleed does not pull the edge back in.
|
||||
fn shown_area(app: &DesignApp, doc: &Document, xf: &Xf, slot: &SpreadSlot) -> Rect {
|
||||
let trim = xf.rect(slot.bounds);
|
||||
let boxes: &[[f64; 4]] = match app.ui.screen_mode {
|
||||
ScreenMode::Bleed => &[doc.settings.bleed],
|
||||
ScreenMode::Slug => &[doc.settings.bleed, doc.settings.slug],
|
||||
_ => &[],
|
||||
};
|
||||
let Some(sp) = doc.spread(slot.spread) else { return trim };
|
||||
boxes.iter().filter_map(|o| sp.outset_rect(*o)).map(|r| xf.rect(slot.xf.transform_rect_bbox(r))).fold(trim, |a, b| a.union(b))
|
||||
}
|
||||
|
||||
fn draw_guides(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout, t: &Tokens) {
|
||||
let s = &doc.settings;
|
||||
for slot in &layout.slots {
|
||||
@@ -941,11 +958,14 @@ fn draw_guides(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document
|
||||
let m = slot.xf;
|
||||
let pt = |p: Point| xf.to_screen(m * p);
|
||||
let rect = |r: DRect| xf.rect(m.transform_rect_bbox(r));
|
||||
// Bleed: one rectangle around the spread (inside bleed only applies at the spread's outer edges).
|
||||
if app.ui.guides
|
||||
&& let Some(br) = sp.bleed_rect(s.bleed)
|
||||
{
|
||||
painter.rect_stroke(rect(br), 0.0, Stroke::new(hair(painter), c32(s.bleed_color)), StrokeKind::Middle);
|
||||
// Bleed, then slug: one rectangle each around the spread (the inside offset only applies
|
||||
// at the spread's outer edges).
|
||||
if app.ui.guides {
|
||||
for (r, color) in [(sp.bleed_rect(s.bleed), s.bleed_color), (sp.slug_rect(s.slug), s.slug_color)] {
|
||||
if let Some(r) = r {
|
||||
painter.rect_stroke(rect(r), 0.0, Stroke::new(hair(painter), c32(color)), StrokeKind::Middle);
|
||||
}
|
||||
}
|
||||
}
|
||||
let pasteboard = m.inverse().transform_rect_bbox(layout.pasteboard);
|
||||
for (pi, p) in sp.pages.iter().enumerate() {
|
||||
@@ -3028,6 +3048,81 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bleed and Slug are preview modes: like Preview they hide the guides, and what the page
|
||||
/// shows widens to the bleed or the slug box. The slug guide is drawn in Normal, in its own
|
||||
/// colour, beside the bleed guide.
|
||||
#[test]
|
||||
fn bleed_and_slug_are_preview_modes_that_show_their_area() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", serde_json::json!({"bleed": 9, "slug": 18})).unwrap();
|
||||
let strokes = |app: &mut DesignApp, want: [u8; 3]| -> usize {
|
||||
let ctx = egui::Context::default();
|
||||
let mut shapes = Vec::new();
|
||||
for _ in 0..2 {
|
||||
let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1440.0, 900.0))), ..Default::default() };
|
||||
let mut out = ctx.run_ui(raw, |ui| show(app, ui));
|
||||
out.textures_delta.clear();
|
||||
shapes = out.shapes;
|
||||
}
|
||||
let want = c32(want);
|
||||
shapes.iter().filter(|c| matches!(&c.shape, egui::Shape::Rect(s) if s.stroke.color == want)).count()
|
||||
};
|
||||
let s = app.session.active().unwrap().doc.settings.clone();
|
||||
// Normal draws every guide, the slug's among them.
|
||||
app.ui.screen_mode = ScreenMode::Normal;
|
||||
assert!(strokes(&mut app, s.margin_color) > 0, "Normal draws the margin guide");
|
||||
assert!(strokes(&mut app, s.bleed_color) > 0, "Normal draws the bleed guide");
|
||||
assert!(strokes(&mut app, s.slug_color) > 0, "Normal draws the slug guide");
|
||||
// Every preview mode hides them.
|
||||
for mode in [ScreenMode::Preview, ScreenMode::Bleed, ScreenMode::Slug, ScreenMode::Presentation] {
|
||||
app.ui.screen_mode = mode;
|
||||
assert_eq!(strokes(&mut app, s.margin_color), 0, "{mode:?} hides the margin guide");
|
||||
assert_eq!(strokes(&mut app, s.slug_color), 0, "{mode:?} hides the slug guide");
|
||||
}
|
||||
}
|
||||
|
||||
/// Each preview mode's shown area: Preview stops at the trim, Bleed reaches the bleed box and
|
||||
/// Slug the slug box. A slug narrower than the bleed never pulls the edge back in.
|
||||
#[test]
|
||||
fn shown_area_widens_to_the_bleed_and_slug_boxes() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", serde_json::json!({"bleed": 9, "slug": 18})).unwrap();
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
let layout = CanvasLayout::new(&doc, false);
|
||||
let xf = Xf::new(Rect::from_min_size(Pos2::ZERO, vec2(1440.0, 900.0)), &View::default());
|
||||
let slot = &layout.slots[0];
|
||||
let area = |app: &mut DesignApp, mode| {
|
||||
app.ui.screen_mode = mode;
|
||||
shown_area(app, &doc, &xf, slot)
|
||||
};
|
||||
let trim = xf.rect(slot.bounds);
|
||||
assert_eq!(area(&mut app, ScreenMode::Preview), trim, "Preview stops at the trim");
|
||||
assert_eq!(area(&mut app, ScreenMode::Presentation), trim, "Presentation stops at the trim");
|
||||
let bleed = area(&mut app, ScreenMode::Bleed);
|
||||
let slug = area(&mut app, ScreenMode::Slug);
|
||||
assert!(bleed.contains_rect(trim) && bleed != trim, "Bleed reaches past the trim");
|
||||
assert!(slug.contains_rect(bleed) && slug != bleed, "Slug reaches past the bleed");
|
||||
// 9 pt of bleed and 18 pt of slug at this zoom, on every edge.
|
||||
let grew = |r: Rect, pt: f64| {
|
||||
let d = (pt * xf.zoom) as f32;
|
||||
(r.min - trim.min + vec2(d, d)).length() < 0.01 && (r.max - trim.max - vec2(d, d)).length() < 0.01
|
||||
};
|
||||
assert!(grew(bleed, 9.0), "Bleed grows by the bleed: {bleed:?}");
|
||||
assert!(grew(slug, 18.0), "Slug grows by the slug: {slug:?}");
|
||||
// A slug inside the bleed: Slug still shows every printing element, so the bleed too.
|
||||
app.run("file.new", serde_json::json!({"bleed": 18, "slug": 9})).unwrap();
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
app.ui.screen_mode = ScreenMode::Slug;
|
||||
assert!(grew(shown_area(&app, &doc, &xf, slot), 18.0), "the wider of the two boxes wins");
|
||||
// No bleed and no slug: nothing to widen to.
|
||||
app.run("file.new", serde_json::json!({})).unwrap();
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
for mode in [ScreenMode::Bleed, ScreenMode::Slug] {
|
||||
app.ui.screen_mode = mode;
|
||||
assert_eq!(shown_area(&app, &doc, &xf, slot), trim, "{mode:?} without a bleed or slug");
|
||||
}
|
||||
}
|
||||
|
||||
fn harness(app: DesignApp) -> Harness<'static, DesignApp> {
|
||||
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).build_ui_state(
|
||||
|ui, app: &mut DesignApp| {
|
||||
|
||||
@@ -1243,7 +1243,9 @@ pub fn open_document_setup(app: &mut DesignApp) {
|
||||
}
|
||||
// The margins as shown: OK applies them to every page only when they were edited.
|
||||
f["marginsShown"] = json!(SETUP_EDGES.map(|e| f[format!("margin{e}")].clone()));
|
||||
app.ui.dialog = Some(Dialog::new("documentSetup", f));
|
||||
let mut d = Dialog::new("documentSetup", f);
|
||||
d.unit = units;
|
||||
app.ui.dialog = Some(d);
|
||||
}
|
||||
|
||||
/// The edges of Document Setup's margins, bleed and slug (field suffixes).
|
||||
@@ -7083,6 +7085,17 @@ mod tests {
|
||||
assert!((doc.settings.page_width - 612.0).abs() < 1e-6 && doc.settings.bleed[0] == 0.0, "one undo step");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_setup_reads_a_bare_number_in_the_documents_units() {
|
||||
let mut app = document_setup_app("millimeters");
|
||||
setup_fields(&mut app, json!({"bleedTop": "5", "bleedBottom": "5", "bleedInside": "5", "bleedOutside": "5", "width": "210"}));
|
||||
confirm(&mut app).unwrap();
|
||||
let mm = designcraft_geom::units::PT_PER_MM;
|
||||
let settings = &app.session.active().unwrap().doc.settings;
|
||||
assert!(settings.bleed.iter().all(|b| (b - 5.0 * mm).abs() < 1e-6), "5 mm, not 5 picas: {:?}", settings.bleed);
|
||||
assert!((settings.page_width - 210.0 * mm).abs() < 1e-6, "{}", settings.page_width);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn document_setup_keeps_each_pages_margins_unless_they_are_edited() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
|
||||
@@ -68,7 +68,9 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
egui::Panel::right("dock").default_size(253.0).size_range(220.0..=420.0).resizable(true).frame(egui::Frame::NONE.fill(t.panel)).show(
|
||||
ui,
|
||||
|ui| {
|
||||
dock_header(ui, &t, "»");
|
||||
if dock_header(ui, &t, "»").clicked() {
|
||||
let _ = app.run("window.collapseToIcons", serde_json::json!({}));
|
||||
}
|
||||
// Tab strip (27 pt).
|
||||
let (strip, _) = ui.allocate_exact_size(vec2(ui.available_width(), 27.0), Sense::hover());
|
||||
ui.painter().rect_filled(strip, 0.0, t.tab_strip);
|
||||
@@ -127,7 +129,9 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
// (Essentials Classic) show their names.
|
||||
let groups = app.ui.icon_groups.clone();
|
||||
egui::Panel::right("icon_column").exact_size(icon_column_width(app)).resizable(false).frame(egui::Frame::NONE.fill(t.panel)).show(ui, |ui| {
|
||||
dock_header(ui, &t, "«");
|
||||
if dock_header(ui, &t, "«").clicked() {
|
||||
app.ui.dock_expanded = true;
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
match &groups {
|
||||
Some(groups) => {
|
||||
@@ -194,12 +198,13 @@ fn icon_clicked(app: &mut DesignApp, ui: &mut egui::Ui, id: &str, response: &egu
|
||||
crate::panel_docking::legacy_tab(app, ui, id, response);
|
||||
}
|
||||
|
||||
/// 11 pt dock header strip with a collapse chevron.
|
||||
fn dock_header(ui: &mut egui::Ui, t: &Tokens, chevron: &str) {
|
||||
let (r, _) = ui.allocate_exact_size(vec2(ui.available_width(), 11.0), Sense::hover());
|
||||
/// 11 pt dock header strip with a collapse chevron; clicking it reports a click on the response.
|
||||
fn dock_header(ui: &mut egui::Ui, t: &Tokens, chevron: &str) -> egui::Response {
|
||||
let (r, response) = ui.allocate_exact_size(vec2(ui.available_width(), 11.0), Sense::click());
|
||||
ui.painter().rect_filled(r, 0.0, t.tab_strip);
|
||||
ui.painter().text(egui::pos2(r.max.x - 8.0, r.center().y), egui::Align2::RIGHT_CENTER, chevron, egui::FontId::proportional(9.0), t.text_dim);
|
||||
ui.painter().line_segment([r.left_bottom(), r.right_bottom()], Stroke::new(1.0, t.border));
|
||||
response
|
||||
}
|
||||
|
||||
/// A panel opened from the icon column, shown as a floating flyout next to the dock.
|
||||
@@ -518,3 +523,50 @@ mod visual_regressions {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod chevron_tests {
|
||||
use super::*;
|
||||
use egui_kittest::Harness;
|
||||
|
||||
fn click(harness: &mut Harness<'_, DesignApp>, at: egui::Pos2) {
|
||||
harness.hover_at(at);
|
||||
harness.run_steps(1);
|
||||
harness.drag_at(at);
|
||||
harness.run_steps(1);
|
||||
harness.drop_at(at);
|
||||
harness.run_steps(3);
|
||||
}
|
||||
|
||||
/// The » / « chevron painted at the right end of a panel's 11 pt header.
|
||||
fn chevron(harness: &Harness<'_, DesignApp>, panel: &str) -> egui::Pos2 {
|
||||
let r = egui::containers::panel::PanelState::load(&harness.ctx, egui::Id::new(panel)).expect("the panel is laid out").outer_rect;
|
||||
egui::pos2(r.max.x - 10.0, r.min.y + 5.0)
|
||||
}
|
||||
|
||||
/// Issue #1203: the header chevrons were painted text; clicking them did nothing.
|
||||
#[test]
|
||||
fn the_dock_chevron_collapses_and_expands_the_dock() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), Default::default());
|
||||
app.ui.dock_expanded = true;
|
||||
let mut ready = false;
|
||||
let mut harness = Harness::builder().with_size(vec2(1280.0, 640.0)).build_ui_state(
|
||||
move |ui, app: &mut DesignApp| {
|
||||
if !ready {
|
||||
crate::theme::install_fonts(ui.ctx(), "en");
|
||||
ready = true;
|
||||
return;
|
||||
}
|
||||
show(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
harness.run_steps(4);
|
||||
let at = chevron(&harness, "dock");
|
||||
click(&mut harness, at);
|
||||
assert!(!harness.state().ui.dock_expanded, "» collapses the dock to icons");
|
||||
let at = chevron(&harness, "icon_column");
|
||||
click(&mut harness, at);
|
||||
assert!(harness.state().ui.dock_expanded, "« expands the dock again");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -580,6 +580,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[
|
||||
("Flattener Preview", "გაბრტყელების წინასწარი ნახვა"),
|
||||
("Float Panel", "მცურავი პანელი"),
|
||||
("Fonts in Document", "დოკუმენტის შრიფტები"),
|
||||
("Reload Fonts", "შრიფტების ხელახლა ჩატვირთვა"),
|
||||
("Footnotes", "სქოლიოები"),
|
||||
("Form Fields", "ფორმის ველები"),
|
||||
("Frame Fitting Options…", "ჩარჩოს ზომაზე მორგების პარამეტრები…"),
|
||||
|
||||
@@ -738,7 +738,9 @@ impl DesignApp {
|
||||
ViewInfo {
|
||||
zoom: self.view().map(|v| v.zoom).unwrap_or(1.0),
|
||||
snap: self.ui.snap_view(),
|
||||
unit: self.ui.units,
|
||||
// Tool readouts (sizes, smart-guide distances) measure in the document's ruler units,
|
||||
// as its Control and Properties fields do.
|
||||
unit: self.session.active().map_or(self.ui.units, |st| st.doc.settings.horizontal_units),
|
||||
stroke_in_front: self.ui.stroke_front,
|
||||
}
|
||||
}
|
||||
@@ -835,7 +837,7 @@ impl DesignApp {
|
||||
}
|
||||
}
|
||||
if r.is_ok()
|
||||
&& matches!(id, "font.replace" | "edit.undo" | "edit.redo")
|
||||
&& matches!(id, "font.replace" | "font.reload" | "edit.undo" | "edit.redo")
|
||||
&& let Some(dialog) = self.ui.dialog.as_mut().filter(|d| d.id == "findFont")
|
||||
{
|
||||
dialog.fields.remove("_fonts");
|
||||
@@ -951,7 +953,7 @@ impl DesignApp {
|
||||
}
|
||||
}
|
||||
// Release only when the request actually replaces Find, including a native PDF picker.
|
||||
if was_find && !self.ui.dialog.as_ref().is_some_and(|d| d.id == "findChange") {
|
||||
if was_find && self.ui.dialog.as_ref().is_none_or(|d| d.id != "findChange") {
|
||||
self.session.reset_find_scope();
|
||||
}
|
||||
}
|
||||
@@ -1442,7 +1444,7 @@ impl DesignApp {
|
||||
self.status(format!("{file}: {}", warnings.join("; ")));
|
||||
// Find Font, offered for the document's missing fonts, stays up: the status line
|
||||
// keeps the warnings.
|
||||
if !self.ui.dialog.as_ref().is_some_and(|d| d.id == "findFont") {
|
||||
if self.ui.dialog.as_ref().is_none_or(|d| d.id != "findFont") {
|
||||
self.ui.dialog =
|
||||
Some(dialogs::Dialog::new("alert", json!({"title": "Import Warnings", "file": file, "message": warnings.join("\n")})));
|
||||
}
|
||||
@@ -1848,6 +1850,17 @@ mod tests {
|
||||
assert!(!accurate.rich_black, "a saved choice is kept");
|
||||
}
|
||||
|
||||
/// Tool readouts measured in picas whatever the document's units (the UI's unit is never set):
|
||||
/// they follow the active document's ruler units.
|
||||
#[test]
|
||||
fn tool_readouts_measure_in_the_documents_units() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), Services::default());
|
||||
assert_eq!(app.view_info().unit, Unit::Picas, "no document: the UI's default");
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
app.run("document.preferences", json!({"horizontalUnits": "inches", "verticalUnits": "millimeters"})).unwrap();
|
||||
assert_eq!(app.view_info().unit, Unit::Inches);
|
||||
}
|
||||
|
||||
/// An app with one document that has an unsaved edit.
|
||||
fn app_with_unsaved_document() -> DesignApp {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), Services::default());
|
||||
|
||||
@@ -467,6 +467,7 @@ pub const MENUS: &[(&str, &[&str])] = &[
|
||||
"-",
|
||||
"cmd:type.createOutlines",
|
||||
"ui:app.findFontDialog",
|
||||
"cmd:font.reload",
|
||||
">Change Case",
|
||||
"cmd:type.changeCase|UPPERCASE|{\"case\": \"upper\"}",
|
||||
"cmd:type.changeCase|lowercase|{\"case\": \"lower\"}",
|
||||
@@ -5021,6 +5022,7 @@ mod tests {
|
||||
"-",
|
||||
"Create Outlines",
|
||||
"Find/Replace Font…",
|
||||
"Reload Fonts",
|
||||
"Change Case >",
|
||||
"-",
|
||||
"Type on a Path >",
|
||||
|
||||
@@ -360,9 +360,8 @@ fn cards(app: &DesignApp, tab: &str) -> Vec<Card> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `210 × 297 mm`, `51p0 × 66p0`.
|
||||
fn size_text(nd: &NewDocument) -> String {
|
||||
let u = nd.units;
|
||||
/// `210 × 297 mm`, `51p0 × 66p0`: the size of `nd` in the dialog's units `u`.
|
||||
fn size_text(nd: &NewDocument, u: Unit) -> String {
|
||||
let (w, h) = (format_measure(nd.width, u), format_measure(nd.height, u));
|
||||
let suffix = u.suffix();
|
||||
if suffix.starts_with(' ')
|
||||
@@ -391,7 +390,7 @@ fn paint_page(p: &egui::Painter, area: Rect, nd: &NewDocument, custom: bool, col
|
||||
}
|
||||
}
|
||||
|
||||
fn card(ui: &mut Ui, c: &Card, selected: bool) -> egui::Response {
|
||||
fn card(ui: &mut Ui, c: &Card, units: Unit, selected: bool) -> egui::Response {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let (r, resp) = ui.allocate_exact_size(CARD, Sense::click());
|
||||
if selected {
|
||||
@@ -406,7 +405,7 @@ fn card(ui: &mut Ui, c: &Card, selected: bool) -> egui::Response {
|
||||
ui.painter(),
|
||||
pos2(r.center().x, r.top() + 132.0),
|
||||
Align2::CENTER_CENTER,
|
||||
&size_text(&c.nd),
|
||||
&size_text(&c.nd, units),
|
||||
FontId::proportional(11.0),
|
||||
t.text_dim,
|
||||
);
|
||||
@@ -494,13 +493,14 @@ fn presets(app: &mut DesignApp, ui: &mut Ui, d: &mut Dialog) -> bool {
|
||||
});
|
||||
ui.add_space(10.0);
|
||||
let current = d.s("preset");
|
||||
let card_units = units(d);
|
||||
let mut create = false;
|
||||
let mut delete = None;
|
||||
let layout = if rtl { Layout::right_to_left(Align::TOP) } else { Layout::left_to_right(Align::TOP) }.with_main_wrap(true);
|
||||
ui.with_layout(layout, |ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(10.0, 10.0);
|
||||
for c in &list {
|
||||
let resp = card(ui, c, current == c.key);
|
||||
let resp = card(ui, c, card_units, current == c.key);
|
||||
if resp.clicked() || resp.double_clicked() {
|
||||
select(d, c);
|
||||
}
|
||||
@@ -1031,6 +1031,13 @@ mod tests {
|
||||
assert!(!doc.settings.facing_pages);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn card_sizes_follow_the_dialog_units() {
|
||||
let card = NewDocument::from_preset("Business Card").unwrap();
|
||||
assert_eq!(size_text(&card, Unit::Millimeters), "88.9 × 50.8 mm");
|
||||
assert_ne!(size_text(&card, Unit::Millimeters), size_text(&card, Unit::Picas));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn units_reformat_the_measures_and_linked_edges_follow() {
|
||||
// InDesign's Units list.
|
||||
|
||||
@@ -377,6 +377,17 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
if resp.dragged() {
|
||||
drag_ghost(ui, label, &t);
|
||||
}
|
||||
if let Some((_, id)) = row.parent {
|
||||
resp.context_menu(|ui| {
|
||||
let label = crate::i18n::tr(&app.ui.language, "Delete Parent");
|
||||
if ui.button(crate::rtl::widget(ui, label)).clicked() {
|
||||
if app.run("layout.parents.delete", json!({"id": id.0})).is_ok() {
|
||||
app.views.clear();
|
||||
}
|
||||
ui.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
if opts.parents_vertical {
|
||||
for row in &rows {
|
||||
@@ -778,6 +789,30 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::test_window;
|
||||
|
||||
#[test]
|
||||
fn parent_context_menu_deletes_the_clicked_id_even_with_a_shared_prefix() {
|
||||
for vertical in [true, false] {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"pages": 2, "facingPages": false})).unwrap();
|
||||
app.ui.pages_panel.parents_vertical = vertical;
|
||||
let original = app.session.doc().unwrap().doc.parents[0].id;
|
||||
let added = app.run("layout.parents.new", json!({"prefix": "A", "name": "Other"})).unwrap()["id"].as_u64().unwrap();
|
||||
let mut h = harness(app);
|
||||
// Target the actual parent row in either panel layout, not a menu helper.
|
||||
h.get_by_label("A-Other").click_secondary();
|
||||
h.run_steps(3);
|
||||
h.get_by_label("Delete Parent").click();
|
||||
h.run_steps(3);
|
||||
let doc = &h.state().session.doc().unwrap().doc;
|
||||
assert_eq!(doc.parents.len(), 1);
|
||||
assert_eq!(doc.parents[0].id, original);
|
||||
assert!(doc.parents.iter().all(|p| p.id.0 != added));
|
||||
assert_eq!(doc.page_count(), 2, "document pages are not deleted");
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
assert_eq!(h.state().session.doc().unwrap().doc.parents.len(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn double_clicking_a_parent_shows_that_parent() {
|
||||
let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
|
||||
@@ -1864,6 +1864,12 @@ pub fn stroke_panel(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
super::cell_stroke::panel(app, ui);
|
||||
return;
|
||||
}
|
||||
if super::colors_affect_text(app)
|
||||
&& let Some(a) = text_attrs(app)
|
||||
{
|
||||
text_stroke_panel(app, ui, &a["chars"]);
|
||||
return;
|
||||
}
|
||||
let Some(i) = sel_info(app) else {
|
||||
crate::rtl::label(ui, crate::i18n::tr(&app.ui.language, "Select an object."));
|
||||
return;
|
||||
@@ -1985,6 +1991,27 @@ pub fn stroke_panel(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Stroke panel while text is selected: the text's outline colour, weight and whether it sits behind the fill.
|
||||
fn text_stroke_panel(app: &mut DesignApp, ui: &mut egui::Ui, c: &Value) {
|
||||
let swatch = c["stroke"].as_str().unwrap_or(designcraft_color::swatch::NONE).to_string();
|
||||
egui::Grid::new("text_stroke_panel").num_columns(2).spacing(vec2(8.0, 6.0)).show(ui, |ui| {
|
||||
caption(ui, crate::i18n::tr(&app.ui.language, "Weight:"));
|
||||
if let Some(v) = number(ui, "tsw_panel", c["strokeWeight"].as_f64(), " pt", 60.0, 3) {
|
||||
let _ = app.run("type.char", json!({"attrs": {"strokeWeight": v}}));
|
||||
}
|
||||
ui.end_row();
|
||||
caption(ui, crate::i18n::tr(&app.ui.language, "Color:"));
|
||||
super::swatch_picker(app, ui, "tspick", Some(swatch), |app, n| {
|
||||
let _ = super::apply_swatch(app, true, &n, None);
|
||||
});
|
||||
ui.end_row();
|
||||
});
|
||||
let mut behind = c["strokeBehindFill"].as_bool().unwrap_or(false);
|
||||
if ui.checkbox(&mut behind, crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Stroke Behind Fill"))).changed() {
|
||||
let _ = app.run("type.char", json!({"attrs": {"strokeBehindFill": behind}}));
|
||||
}
|
||||
}
|
||||
|
||||
/// A short line ending in arrowhead `a` (pointing left for Start, right for End).
|
||||
fn arrow_preview(ui: &mut egui::Ui, a: Arrowhead, start: bool, color: egui::Color32) {
|
||||
use designcraft_geom::kurbo;
|
||||
@@ -2360,7 +2387,11 @@ pub fn links_panel(app: &mut DesignApp, ui: &mut egui::Ui) {
|
||||
"tooLarge" => ("Too large to check", egui::Color32::from_rgb(0xe8, 0xb3, 0x2c)),
|
||||
_ => ("OK", t.text_dim),
|
||||
};
|
||||
crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&app.ui.language, txt)).color(col).size(11.0));
|
||||
let status = crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&app.ui.language, txt)).color(col).size(11.0));
|
||||
// Why a link isn't followed (a path on another computer, a device).
|
||||
if let Some(why) = r["refused"].as_str() {
|
||||
status.on_hover_text(why);
|
||||
}
|
||||
let uses = r["uses"].as_array().cloned().unwrap_or_default();
|
||||
let info = match uses.first() {
|
||||
Some(u) => {
|
||||
@@ -2911,6 +2942,30 @@ mod tests {
|
||||
h
|
||||
}
|
||||
|
||||
/// With the caret in text, the Stroke panel offers the text's stroke options, not an empty panel.
|
||||
#[test]
|
||||
fn stroke_panel_shows_the_text_stroke_options_while_text_is_selected() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
app.run("frame.create", json!({"rect": [72, 72, 400, 400], "content": "text", "caret": true})).unwrap();
|
||||
let mut ready = false;
|
||||
let mut h = Harness::builder().with_size(vec2(320.0, 400.0)).build_ui_state(
|
||||
move |ui, app: &mut DesignApp| {
|
||||
if !ready {
|
||||
crate::theme::install_fonts(ui.ctx(), "");
|
||||
crate::theme::apply(ui.ctx(), &Tokens::for_brightness(Default::default()));
|
||||
ready = true;
|
||||
return;
|
||||
}
|
||||
stroke_panel(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(3);
|
||||
assert!(h.query_by_label("Select an object.").is_none());
|
||||
h.get_by_label("Stroke Behind Fill");
|
||||
}
|
||||
|
||||
/// As InDesign's: Edit Page takes the Page tool to the page in view, and Properties sets that
|
||||
/// page alone; Back returns to the document.
|
||||
#[test]
|
||||
|
||||
@@ -51,27 +51,26 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
|
||||
.show(ctx, |ui| {
|
||||
egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
|
||||
ui.horizontal_top(|ui| {
|
||||
// Style column.
|
||||
ui.vertical(|ui| {
|
||||
ui.set_width(130.0);
|
||||
for (i, r) in story.para_ranges().iter().enumerate() {
|
||||
let lines = story.text[r.clone()].len() / 60 + 1;
|
||||
let style = &story.paras[i].style;
|
||||
crate::rtl::label(ui, egui::RichText::new(crate::i18n::style_name(&app.ui.language, style)).size(11.0).color(t.text_dim));
|
||||
for _ in 1..lines {
|
||||
ui.label(egui::RichText::new(" ").size(11.0));
|
||||
}
|
||||
}
|
||||
});
|
||||
// Style column: the labels are painted after the text, at the rows the text really wraps to.
|
||||
let (column, _) = ui.allocate_exact_size(vec2(130.0, 0.0), egui::Sense::hover());
|
||||
ui.separator();
|
||||
let mut text = story.text.clone();
|
||||
let r = ui.add(
|
||||
egui::TextEdit::multiline(&mut text)
|
||||
.font(egui::FontId::proportional(app.ui.story_editor_size.clamp(8.0, 36.0)))
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(20)
|
||||
.frame(egui::Frame::NONE),
|
||||
);
|
||||
let out = egui::TextEdit::multiline(&mut text)
|
||||
.font(egui::FontId::proportional(app.ui.story_editor_size.clamp(8.0, 36.0)))
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(20)
|
||||
.frame(egui::Frame::NONE)
|
||||
.show(ui);
|
||||
let r = out.response;
|
||||
let starts = paragraph_start_offsets(out.galley.rows.iter().map(|row| (row.pos.y, row.ends_with_newline)));
|
||||
for (i, y) in starts.into_iter().enumerate() {
|
||||
let Some(para) = story.paras.get(i) else { break };
|
||||
let rect = egui::Rect::from_min_size(egui::pos2(column.min.x, out.galley_pos.y + y), vec2(130.0, 14.0));
|
||||
ui.scope_builder(egui::UiBuilder::new().max_rect(rect), |ui| {
|
||||
let name = crate::i18n::style_name(&app.ui.language, ¶.style);
|
||||
crate::rtl::label(ui, egui::RichText::new(name).size(11.0).color(t.text_dim));
|
||||
});
|
||||
}
|
||||
if r.changed() {
|
||||
// Minimal diff: common prefix and suffix.
|
||||
let old = &story.text;
|
||||
@@ -111,6 +110,21 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical offset of the first laid-out row of each paragraph, from `(row y, row ends with a newline)`
|
||||
/// in layout order. A paragraph that wraps spans several rows, so counting characters per line
|
||||
/// drifts from what the editor draws; the rows themselves are the truth.
|
||||
fn paragraph_start_offsets(rows: impl Iterator<Item = (f32, bool)>) -> Vec<f32> {
|
||||
let mut starts = Vec::new();
|
||||
let mut at_start = true;
|
||||
for (y, ends_with_newline) in rows {
|
||||
if at_start {
|
||||
starts.push(y);
|
||||
}
|
||||
at_start = ends_with_newline;
|
||||
}
|
||||
starts
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use egui_kittest::kittest::Queryable;
|
||||
@@ -123,6 +137,13 @@ mod tests {
|
||||
st.doc.stories.values().next().unwrap().text.clone()
|
||||
}
|
||||
|
||||
/// A wrapped paragraph takes several rows; the next paragraph's label goes to the row after them.
|
||||
#[test]
|
||||
fn labels_follow_wrapped_rows() {
|
||||
let rows = [(0.0, false), (20.0, false), (40.0, true), (60.0, true), (80.0, false)];
|
||||
assert_eq!(super::paragraph_start_offsets(rows.into_iter()), vec![0.0, 60.0, 80.0]);
|
||||
}
|
||||
|
||||
/// Story ids are counted per document: typing in the editor after switching documents edited
|
||||
/// the other document's story with the same id.
|
||||
#[test]
|
||||
|
||||
+10
-1
@@ -26,7 +26,7 @@ of step N's result: N is the 0-based step index (comments and blank lines are no
|
||||
step) and the path goes through result fields and array indices (`"$1.story"`, `"$3.ids.0"`); `"${N.path}"`
|
||||
interpolates inside longer strings. Any other string starting with `$` stays as written (`"$1-$2"`, `"$5 off"`), and
|
||||
`$$` writes a literal `$` anywhere (`"$$1"` passes `$1`, a GREP group). In text parameters `\n` starts a new paragraph and `\u2028` is a forced line
|
||||
break. Most commands ignore parameters they don't know instead of rejecting them, so check names with `describe`.
|
||||
break (`\u000b`, Word's line break, is read as one too). Most commands ignore parameters they don't know instead of rejecting them, so check names with `describe`.
|
||||
`designcraft-cli script --help` prints this reference.
|
||||
|
||||
```sh
|
||||
@@ -87,3 +87,12 @@ designcraft-cli app --method ui.screenshot '{"path": "/tmp/w.png"}' # any contro
|
||||
|
||||
`ui.menu.invoke {command}` without params acts like choosing the menu item: a command labelled "…" opens its
|
||||
dialog, which `ui.dialog.set` / `ui.dialog.confirm` fill in and close like a user would.
|
||||
|
||||
## Parent pages
|
||||
|
||||
In the Pages panel, right-click a parent row and choose **Delete Parent**. The equivalent
|
||||
command is `layout.parents.delete {id}`, using the parent spread's numeric ID. It deletes that
|
||||
parent's shared artwork and clears its assignments from document pages and other parents.
|
||||
Local override copies remain as ordinary editable objects; document pages and their own content
|
||||
remain. Undo restores the parent and its relationships in one step. Deleting the last parent
|
||||
returns the canvas to document pages. Parent navigation is a separate operation.
|
||||
|
||||
+4
-4
@@ -1,6 +1,6 @@
|
||||
# Where DesignCraft falls short of InDesign
|
||||
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-16 · **Change:** minor (gap 27 closed: the loaded text cursor threads into a frame inside a group (#1075); .indd and .indt open through indd-utils; gap 0 shrunk: text threads by hand; threading gaps added; Optical kerning from outlines (#181); gap 6: macOS lists fonts that font services and managers activate; shared print spooler; image clipping paths from IDML; Tabs panel listed as present (#343); book chapter update safety; gap 16: Based on Parent leftovers; bounded control click/drag input; swatches on placed greyscale and bitmap images) · **Target:** Adobe InDesign 2026 (21.6)
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-17 · **Change:** minor (gap 6 shrunk: Type › Reload Fonts reads fonts installed, edited or removed while running (#328); gap 3: Package links are relative, #340; gap 27 closed: the loaded text cursor threads into a frame inside a group (#1075); .indd and .indt open through indd-utils; gap 0 shrunk: text threads by hand; threading gaps added; Optical kerning from outlines (#181); gap 6: macOS lists fonts that font services and managers activate; shared print spooler; image clipping paths from IDML; Tabs panel listed as present (#343); book chapter update safety; gap 16: Based on Parent leftovers; bounded control click/drag input; swatches on placed greyscale and bitmap images) · **Target:** Adobe InDesign 2026 (21.6)
|
||||
|
||||
Every known shortfall against InDesign 2026, one entry each, ranked by how much it stops an
|
||||
InDesign professional from doing real work. This is the work list: agents pick from the top.
|
||||
@@ -57,8 +57,7 @@ Kind: **F** feature · **U** UI/UX · **T** typography · **FF** file format ·
|
||||
- **Missing:** a corpus test of real InDesign-authored IDML (via `storytold/photocraft-corpus` or
|
||||
a sibling) measuring what survives import → export → reopen in InDesign. Open bugs: frames not
|
||||
rendered without a warning (#193), `$ID/` font names reported missing (#338), no
|
||||
`PDFAttribute` on export (#339), Package writes build-machine absolute paths (#340), drag-drop
|
||||
open of IDML on the app icon (#211).
|
||||
`PDFAttribute` on export (#339), drag-drop open of IDML on the app icon (#211).
|
||||
- **Evidence:** importer coverage is broad (styles, tables, footnotes, xrefs, index, CJK, Arabic,
|
||||
verified in InDesign 2026 with synthetic files), but nothing is measured on real documents.
|
||||
- **Impact:** IDML is the main interchange format until INDD lands, and the only one that goes
|
||||
@@ -93,7 +92,8 @@ Kind: **F** feature · **U** UI/UX · **T** typography · **FF** file format ·
|
||||
|
||||
### 6. Installed fonts not always found (F)
|
||||
- **Missing:** fonts in user font folders used for text (#161, #110, #107), fonts that font
|
||||
services and managers activate on Windows, reload when fonts change on disk (#328).
|
||||
services and managers activate on Windows, noticing by itself when fonts change on disk (#328:
|
||||
Type › Reload Fonts, `font.reload`, reads them again on request since 2026-10-16).
|
||||
- **Evidence:** on macOS the scan reads the font files CoreText has available, so fonts that
|
||||
Adobe Fonts and font managers activate are listed (#261, #327, 2026-10-11); not yet confirmed
|
||||
by the reporters.
|
||||
|
||||
+51
-6
@@ -7,21 +7,64 @@ the result as PNG images. It speaks newline-delimited JSON-RPC 2.0 on stdin/stdo
|
||||
|
||||
## Register it
|
||||
|
||||
Build the CLI (`cargo build --release -p designcraft-cli`), then:
|
||||
### From source (development)
|
||||
|
||||
With Rust and Cargo installed, run this from the repository root:
|
||||
|
||||
```sh
|
||||
cargo install --path apps/designcraft-cli --locked
|
||||
```
|
||||
|
||||
Cargo copies the executable into its installation root's `bin` directory, normally
|
||||
`~/.cargo/bin` (Windows `%USERPROFILE%\.cargo\bin`). If you set `CARGO_INSTALL_ROOT`,
|
||||
Cargo's `install.root` configuration, or `CARGO_HOME`, use that installation's `bin`
|
||||
directory instead. Unlike a symlink into `target/release`, this copy survives
|
||||
`cargo clean`. Repeat the install command after source changes to update it.
|
||||
|
||||
Ensure that directory is on `PATH`. For a default install in a POSIX shell:
|
||||
|
||||
```sh
|
||||
export PATH="$HOME/.cargo/bin:$PATH"
|
||||
designcraft-cli --help
|
||||
```
|
||||
|
||||
Persist the PATH setting in your shell configuration (on Windows, add the directory
|
||||
to your user Path). The **MCP client process** must inherit that PATH too; restart
|
||||
the client after changing it. A GUI-launched client may not read shell configuration.
|
||||
If it still reports `ENOENT` or "executable not found", use the absolute path to the
|
||||
installed binary in its registration instead of the bare name.
|
||||
|
||||
### Headless and connected registration
|
||||
|
||||
After installation, register the CLI by name:
|
||||
|
||||
```sh
|
||||
# Headless: an in-process engine, no window
|
||||
claude mcp add designcraft -- /path/to/target/release/designcraft-cli mcp
|
||||
claude mcp add designcraft -- designcraft-cli mcp
|
||||
|
||||
# Drive the running desktop app (start it first with a control port)
|
||||
# Drive the running desktop app (install/start the desktop app separately)
|
||||
designcraft --control 7979
|
||||
claude mcp add designcraft-app -- /path/to/target/release/designcraft-cli mcp --connect 7979
|
||||
claude mcp add designcraft-app -- designcraft-cli mcp --connect 7979
|
||||
```
|
||||
|
||||
Other MCP clients use the same command line, e.g. in a JSON config:
|
||||
|
||||
```json
|
||||
{"mcpServers": {"designcraft": {"command": "/path/to/designcraft-cli", "args": ["mcp"]}}}
|
||||
{
|
||||
"mcpServers": {
|
||||
"designcraft": {
|
||||
"command": "designcraft-cli",
|
||||
"args": ["mcp"]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If the client cannot find the CLI on PATH, substitute the absolute installed path
|
||||
(not a path under `target/`), for example:
|
||||
|
||||
```sh
|
||||
claude mcp add designcraft -- /absolute/install/root/bin/designcraft-cli mcp
|
||||
```
|
||||
|
||||
### From an installed release
|
||||
@@ -137,7 +180,9 @@ Positions in a story's text are **UTF-8 byte offsets**, not character counts: `a
|
||||
`start` / `end` of `story.replaceRange` and of `find.find` matches, the `pos` that `text.placeCaret`, `text.insert`
|
||||
and `text.move` return, a story's `length` in `get_story` and `inspect_document`, and its `overset` position in
|
||||
`get_story`. A character outside ASCII takes 2 to 4 bytes (é 2, — and は 3), so the story `はただ商店` has length 15
|
||||
and its second character starts at 3. An offset inside a character snaps back to the start of that character.
|
||||
and its second character starts at 3. An offset inside a character snaps back to the start of that character, except
|
||||
in `story.replaceRange`: there an offset inside a character, an `end` past the end of the story or a `start` after `end`
|
||||
is an error that gives the story length in bytes, and the story is left unchanged.
|
||||
|
||||
## Text and image fitting
|
||||
|
||||
|
||||
@@ -286,7 +286,7 @@ It is evidence of breadth: each item exists. It is not a claim of InDesign-level
|
||||
- Ruler guides: drag out of the rulers (page guides, or spread guides on the pasteboard), drag to move, drop on a ruler to delete; Layout › Create Guides (rows/columns with gutters, fit to margins or page); Delete All Guides on Spread; `guide.*` commands.
|
||||
- Links panel: every placed graphic with status (OK / Modified / Missing / Embedded, checked against the file on disk), page and effective PPI (low resolution flagged); Relink…, Go To, Update, Embed; `links.*` commands.
|
||||
- Linked text files: `file.place {link: true}` or Preferences › File Handling › Create Links When Placing Text Files keeps a placed .docx/.rtf/.txt story linked to its file (path, size and hash, and the style map and conflict option used). The Links panel shows it OK / Modified / Missing; Update imports the file again into the same frames and thread with the saved style map, replacing edits made in the layout (the Links panel and menu ask first; the `links.update` command updates and returns a warning; undoable); Relink, Go To and Unlink. Saved in the native format; IDML exports the text unlinked.
|
||||
- Missing fonts: highlighted pink on screen (never in output), listed by Preflight; Type › Find/Replace Font (fonts in the document, missing first; replace in text, cells, footnotes and styles); `font.list` / `font.replace`.
|
||||
- Missing fonts: highlighted pink on screen (never in output), listed by Preflight; Type › Find/Replace Font (fonts in the document, missing first; replace in text, cells, footnotes and styles); `font.list` / `font.replace`. Type › Reload Fonts (`font.reload`) scans the font folders again and reads again the installed font files edited or removed since they were read, then recomposes the open documents: a font installed or edited while DesignCraft runs needs no restart (#328, #547). InDesign notices such changes by itself.
|
||||
- Missing glyphs, as in InDesign: a character the applied font lacks is drawn as that font's missing-glyph box (its .notdef; a drawn box when the font's is empty), on screen and in PDF (as a path, so PDF/A and PDF/UA exports take it), and Preflight lists the characters by font. Preferences › Composition › Draw Missing Glyphs from Fallback Fonts (`document.preferences {glyphFallback}`, stored with the document) draws them from other fonts instead; it is off in new documents and IDML imports, on in documents saved before it existed. A missing font's substitute always uses fallback fonts.
|
||||
- Damage-region repaint: after an edit the canvas re-renders only the frames whose composed text (or the items that) changed and patches its texture — pixel-identical to a full render; typing in a frame repaints in under a millisecond instead of a full-viewport render (22 ms measured on the sample).
|
||||
- Native engine saves, Save a Copy, snippets, books, libraries, swatches and engine/MCP exports use same-folder atomic replacement through craft-libs/craft-atomic, as do desktop Services writes and CLI raster exports. Failed writes before replacement retain the original, and already-open readers retain the old bytes. On Unix a new file gets the permissions ordinary file creation gives it (the umask applies) and a replaced file keeps its permissions. Parent-folder flushing is best effort, so this is not a power-loss durability guarantee.
|
||||
@@ -377,7 +377,7 @@ It is evidence of breadth: each item exists. It is not a claim of InDesign-level
|
||||
- Right-to-left text (World-Ready basics): right-to-left runs are shaped right to left, lines are reordered by the Unicode Bidirectional Algorithm (matched against the reference implementation), Paragraph Direction (`type.para {direction}`, Paragraph panel, IDML ParagraphDirection, EPUB `dir`). Digits (`digits`: Default, Arabic, Hindi, Farsi, Native by language; Character panel and Character Style Options; IDML DigitsType) draw 0–9 in another script. Kashidas (`kashidas` paragraph attribute, on by default; IDML Kashidas): justified Arabic lines stretch at one join per word (after seen/sad when possible, never inside lam–alef) with a drawn tatweel before word spaces widen. Carets follow the drawing: the caret before a right-to-left glyph sits at its right edge, arrow keys move as drawn (by words in the line's direction), clicks find the nearest drawn caret position and selections highlight the selected glyphs. Binding: Right to Left (Document Setup, `layout.documentSetup {binding}`, IDML PageBinding) makes page 1 a left page and lays spreads out right to left.
|
||||
- Interface language (Preferences ▸ General ▸ Interface Language, `app.language`; each language listed by its own name): German, French, Spanish, Italian, Japanese, Simplified Chinese and Arabic menu titles, common menu items and panel names (our own translations). Common dialog, panel and tooltip labels are also localized, with English fallback for untranslated strings. Arabic translations cover every key in the shared interface translation table, including menu commands and utility labels. The Type menu's Story Direction, Horizontal/Vertical, Tate-Chu-Yoko, Ruby… and Kenten are translated too. Arabic labels use Unicode bidi run ordering and contextual shaping; welcome and new-document form layouts follow RTL direction independently of document binding. CJK and Arabic glyphs come from [craft-fonts](https://github.com/storytold/craft-fonts) when built with `CRAFT_FONTS_DIR` (all releases): BIZ UDPGothic and Noto Sans CJK SC for the UI (Chinese forms first in the Chinese interface), Noto Sans Arabic, and Mincho for Japanese documents (the web build embeds BIZ UDPGothic only); otherwise from system fonts. Ukrainian covers all 1,430 existing catalog keys, including publishing dialogs and built-in names, with persisted selection, grammatical count captions, English fallback and bundled Cyrillic UI glyph coverage. Polish covers 1,717 catalog keys with the same category-first count captions and English fallback.
|
||||
- Page transitions (`page.transition/transitions`, Page Transitions panel): twelve transition types with speed and direction per spread, written into exported PDFs as `/Trans` entries through an incremental update.
|
||||
- PDF/X-4 export (`file.exportPdf {standard: "x4"}`): an output intent with the working CMYK space's profile (by default craft-cms's Coated FOGRA39 profile, identifier FOGRA39; Generic CMYK and Device CMYK embed our Generic CMYK profile, lut16 tables generated from the parametric press model), GTS_PDFXVersion in Info and XMP, `pdf:Trapped` in XMP as in Info, RGB images without a profile tagged sRGB, no image interpolation (none in PDF/A either), and built-in PDF/X-4 checks reported as warnings. qpdf finds no errors in the result.
|
||||
- PDF/X-4 export (`file.exportPdf {standard: "x4"}`): an output intent with the working CMYK space's profile (by default craft-cms's Coated FOGRA39 profile, identifier FOGRA39; Generic CMYK and Device CMYK embed our Generic CMYK profile, lut16 tables generated from the parametric press model), GTS_PDFXVersion in Info and XMP, `pdf:Trapped` in XMP as in Info, `xmp:MetadataDate` and `xmpMM:VersionID` in XMP, RGB images without a profile tagged sRGB, no image interpolation (none in PDF/A either), and built-in PDF/X-4 checks reported as warnings. qpdf finds no errors in the result.
|
||||
- EPS place: placed at its (high-resolution) bounding box and shown, printed and exported through its TIFF preview, or a placeholder when it has none. PostScript itself isn't interpreted.
|
||||
- Preferences: Dictionary (user dictionary; `spelling.setWords/words`), Spelling (Dynamic Spelling: red squiggles on the canvas, also Edit ▸ Spelling), Autocorrect (word list, applied as you type), Notes, Track Changes, Story Editor Display and File Handling (recovery interval) sections.
|
||||
- Colour management exposed (ICC through the craft-libs `craft-cms` crate): Edit ▸ Color Settings (`color.settings`: RGB/CMYK working spaces, default CMYK Coated FOGRA39 with CMYK swatches shown relative colorimetric with black-point compensation, Generic CMYK and Device CMYK still selectable, intent, black-point compensation; `color.loadProfile` for ICC files; `color.convert` through the working spaces, with gamut checks) and View ▸ Proof Setup / Proof Colors (press, sRGB, legacy Mac RGB, colour-blindness simulations, simulate paper) on the canvas. Placed images are shown through their embedded ICC profiles, and CMYK images through their own profile or the working CMYK space, with the settings' intent and black-point compensation.
|
||||
@@ -422,6 +422,7 @@ It is evidence of breadth: each item exists. It is not a claim of InDesign-level
|
||||
- UI scaling (Preferences › Interface, 50–200%; `window.uiScale`); the window title steps aside when the bar is crowded.
|
||||
- Underline / Strikethrough Options: weight, offset, colour and tint (Character panel; screen, PDF and IDML).
|
||||
- Relink to Folder and Relink File Extension (`links.relinkFolder`; missing links found by name).
|
||||
- Paths a document names (links, data sources, book chapters) are followed only on this computer or on the document's own network share, relative ones beside the document; a link to another computer or a device is Missing, with the reason in `links.list` (`refused`) and Preflight. An IDML or INDD package's linked files are read from where the link points on this computer, then from the link's trailing names beside the package, and only regular files up to 256 MB that hold an image or a graphic. Images inside an SVG come only from `data:` URLs.
|
||||
- File › Package (⌥⇧⌘P): the document relinked to a Links folder, the placed files, the font files that draw its text in a Document Fonts folder (the fonts it names and any fallback fonts that draw characters those lack; except fonts whose licence restricts it, OS/2 `fsType`), an IDML copy, an optional PDF and a report (fonts, fallback fonts, links, preflight, instructions); Copy Links To (`links.copyTo`).
|
||||
- Document fonts: opening a `.designcraft` or `.idml` file loads the fonts in the `Document Fonts` folder beside it (as Package writes it), ahead of installed fonts with the same name, so a packaged document opens with its fonts on another machine. Damaged, oversized (over 256 MB per file, 512 MB in all) and surplus (over 200) font files are skipped and listed in `file.open`'s `warnings`; `font.list` gives each font's `source`. Document fonts belong to their document, as in InDesign: other open documents don't see them (each document with a same-named font draws with its own), and closing the document (or reverting it, or a book chapter's export ending) makes them unavailable. Their files stay in memory until DesignCraft quits, so reopening a document doesn't read or load them again; on the web there is no folder to read.
|
||||
- Load Swatches / Save Swatches for Exchange (.ase: CMYK, RGB, Lab, Gray; spot or process) from the Swatches panel menu; `swatch.load` / `swatch.save`.
|
||||
@@ -438,7 +439,7 @@ It is evidence of breadth: each item exists. It is not a claim of InDesign-level
|
||||
- Gradients: the Gradient Swatch tool (G) drags a gradient's start and end across objects (Shift: 45°); the Gradient panel (InDesign's layout: thumbnail, Type, Location, Angle, Reverse) sets type, angle, reverse and edits stops and midpoints on a ramp (drag, add, drag off to remove, location; drop a swatch from the Swatches panel on a stop or the ramp; stops keep their CMYK), and follows the Fill/Stroke proxy. Edited gradients are unnamed swatches until the Swatches panel's New Swatch names one "New Gradient Swatch" (`swatch.new`, which also duplicates a named swatch); `swatch.create {gradient}` builds one from colour swatches; `object.gradient` (`target: stroke` for a stroke's gradient, drawn across the path's box on screen and in PDF); the vector round-trips through IDML (`GradientFill…`/`GradientStroke…`). A radial gradient stretches with the object's box as InDesign's does (an ellipse reaching the sides of a non-square frame, on screen and in PDF). Text takes a gradient swatch as its fill or stroke, laid across its frame's box as in InDesign (screen and PDF).
|
||||
- New Document (⌘N) as in InDesign: Recent, Saved, Print, Web and Mobile tabs of page cards (double-click creates) beside the preset's details: document name, size with arrows, units, orientation, pages, start page #, facing pages, primary text frame, columns, margins, bleed and slug per edge (linked or not); Save Document Preset (`file.savePreset`, `file.deletePreset`; saved presets are kept with the preferences, listed by `file.presets` and made by `file.new {preset: name}`); `file.new` takes `intent`, `units`, `startPage` and per-edge `bleed`/`slug`. Preview shows a document with the window's settings in the workspace while it is open; Create keeps it, Close removes it.
|
||||
- Document Setup (⌥⌘P), laid out like InDesign's: intent, number of pages, start page # (an even start begins with a left page), facing pages, binding, Page Size (presets keeping the orientation, width and height with arrows, orientation), margins for every page and parent page, bleed and slug per edge (each group linked or not), and Adjust Layout (objects follow the new page size and margins), all in one undo step; measures are read in the document's units, and a value that can't be read keeps the dialog open; `layout.documentSetup {}` reports the setup, margins included.
|
||||
- Stroke panel: cap, join, miter limit, alignment, type, Start/End arrowheads (12 kinds, drawn on screen and in PDF; the path is shortened under pointed heads) and gap colour under dashes and dots.
|
||||
- Stroke panel: cap, join, miter limit, alignment, type, Start/End arrowheads (12 kinds, drawn on screen and in PDF; the path is shortened under pointed heads) and gap colour under dashes and dots; with text selected (or text frames selected with Formatting Affects Text on) it shows the text's stroke weight, colour and Stroke Behind Fill.
|
||||
- Preferences (⌘K: General, Type, Units & Increments, Grids, Guides & Pasteboard, Display Performance) backed by `prefs.set` (application) and `document.preferences` (units, increments, grids — undoable, saved with the document; right-click a ruler to pick its units, the horizontal and vertical rulers each have their own); scaling applies to content and scales stroke weights (Include Stroke Weight), and X/Y/W/H measure the stroke's outer edge (Dimensions Include Stroke Weight). Quick Apply (⌘Return) finds paragraph, character and object styles as well as commands.
|
||||
- Parent item overrides: Cmd+Shift-click a parent item on a page (or Override All Parent Page Items, ⌥⇧⌘L) makes an editable local copy that hides the parent's; Remove All Local Overrides and Detach All Objects from Parent in the Pages panel menu.
|
||||
- Based on Parent (`layout.parents.options {parent, basedOn}`): a parent shows its base parent's items under its own, on its pages and on the parent itself; chains work, cycles are refused, and items from any parent in the chain can be overridden. IDML carries it as the parent pages' `AppliedMaster`; a cycle or missing base in a file is cleared with a warning on open. The Pages panel has no Parent Options dialog yet.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Typography and composition parity
|
||||
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (Optical kerning from outlines (#181); macOS lists fonts that font services and managers activate; Justification dialog for the selected paragraphs; anchored objects wrap text; Tabs panel listed as present (#343); character stroke behind fill) · **Target:** Adobe InDesign 2026 (21.6)
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-16 · **Change:** minor (Type › Reload Fonts reads fonts installed, edited or removed since start-up (#328, #547); Optical kerning from outlines (#181); macOS lists fonts that font services and managers activate; Justification dialog for the selected paragraphs; anchored objects wrap text; Tabs panel listed as present (#343); character stroke behind fill) · **Target:** Adobe InDesign 2026 (21.6)
|
||||
|
||||
Typesetting is what InDesign is bought for. A feature can exist and still set text differently
|
||||
from InDesign: different line breaks, different page count, overset where InDesign fits. This
|
||||
@@ -67,7 +67,7 @@ Fidelity: **unmeasured** unless stated.
|
||||
| CJK composition | mojikumi, kinsoku, aki, tsume, ruby, kenten, warichu, frame grid | most; mojikumi tables kept but not applied; ruby group-only; no frame grid | ~ | cjk.md, cjk-typography.md |
|
||||
| Arabic / Hebrew | kashida, digits, diacritic positioning, RTL stories and tables | ✓ | ~ | vendor justification presets unsupported; list bullets and multi-column RTL (#100) |
|
||||
| Missing glyphs and fonts | pink highlight, substitution | ✓ | ✓ | |
|
||||
| Installed fonts | every activated font | system font folders; on macOS also fonts that font managers and Adobe Fonts activate; user folders not always found | ~ | #161, #110, #107, #327, #261 |
|
||||
| Installed fonts | every activated font, picked up as installed or changed | system font folders; on macOS also fonts that font managers and Adobe Fonts activate; user folders not always found; fonts installed, edited or removed while running found by Type › Reload Fonts, not automatically | ~ | #161, #110, #107, #327, #261, #328 |
|
||||
| Composition highlights | H&J, keeps, custom tracking/kerning, substituted fonts | ✓ | ✓ | |
|
||||
|
||||
Measured from the table above (2026-10-10): 34 rows, 18 ✓, 16 ~, 0 ✗, scoring ✓ = 1 and ~ = 0.5:
|
||||
|
||||
Reference in New Issue
Block a user