diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d8eabea47..29b2c0c98 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -28,53 +28,9 @@ jobs: contents: read pull-requests: read uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main - git-sources: - name: Git dependencies come from storytold - runs-on: ubuntu-latest - permissions: - contents: read - steps: - - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - name: Git dependencies come from storytold - run: | - set -euo pipefail - test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; } - # Source replacement, patches and path overrides in cargo config never show up - # in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key. - # Cargo still reads the legacy .cargo/config. - python3 - <<'PY' - import os, sys, tomllib - for path in (".cargo/config", ".cargo/config.toml"): - if not os.path.exists(path): - continue - config = tomllib.load(open(path, "rb")) - replaced = [name for name, source in config.get("source", {}).items() - if isinstance(source, dict) and "replace-with" in source] - if replaced or "patch" in config or "paths" in config: - print(f"{path} replaces, patches or overrides a source") - sys.exit(1) - PY - # Fails when Cargo.lock is out of date with the manifests. - cargo metadata --locked --format-version 1 > /dev/null - python3 - <<'PY' - import sys, tomllib - lock = tomllib.load(open("Cargo.lock", "rb")) - bad = sorted({p["source"] for p in lock.get("package", []) - if p.get("source", "").startswith("git+") - and not p["source"].startswith("git+https://github.com/storytold/")}) - if bad: - print("Git dependencies outside github.com/storytold:", *bad, sep="\n") - sys.exit(1) - PY release: - needs: [git-sources, credits] + needs: credits permissions: contents: write packages: write diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index e78fe1ea3..c5731a0ae 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -18,4 +18,6 @@ jobs: sonarcloud: # Tracked at @main so org-wide CI improvements propagate without PR churn. uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main + with: + fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 secrets: inherit diff --git a/AGENTS.md b/AGENTS.md index d2eebf491..fff6f71a0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -110,3 +110,7 @@ People trust DesignCraft with their layouts; a crash loses their work. **This ou - Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it. New workspace crates include `workspace = true` under `[lints]` in `Cargo.toml`. Production crate roots declare the crate-level lint attributes before modules; crate-level `//!` documentation may precede them. + +## Shared libraries + +Before adding or changing file-format, codec, protocol, or reusable UI code, check `storytold/craft-libs` and its open PRs first; reuse or extend the shared library rather than duplicating it locally, keep app-specific policy and model mapping in the app, and preserve behavior, security/resource limits, attribution, and regression coverage during cutovers. diff --git a/Cargo.lock b/Cargo.lock index 0367fb6bd..7f96b25db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,6 +116,17 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aes" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fc36d7440d015b45468ba08a21e80e056ebbd014c611c6298bb7503e04bd1" +dependencies = [ + "cipher", + "cpubits", + "cpufeatures 0.3.1", +] + [[package]] name = "ahash" version = "0.8.12" @@ -150,13 +161,28 @@ version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" +[[package]] +name = "alloc-no-stdlib" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a" + [[package]] name = "alloc-stdlib" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e76a019e91224d279006ff972f1e984179a6e9feb050adba6ce8274aef23195" dependencies = [ - "alloc-no-stdlib", + "alloc-no-stdlib 2.0.4", +] + +[[package]] +name = "alloc-stdlib" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1" +dependencies = [ + "alloc-no-stdlib 3.0.0", ] [[package]] @@ -230,7 +256,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.61.2", + "windows-sys 0.52.0", "wl-clipboard-rs", "x11rb", ] @@ -536,6 +562,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1" + [[package]] name = "bitflags" version = "1.3.2" @@ -557,6 +589,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "block2" version = "0.5.1" @@ -618,9 +659,9 @@ version = "8.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cc91aac060a7a1e25823bdccbfb6af1875b88f17c6daac97894eed8207166b3" dependencies = [ - "alloc-no-stdlib", - "alloc-stdlib", - "brotli-decompressor", + "alloc-no-stdlib 2.0.4", + "alloc-stdlib 0.2.4", + "brotli-decompressor 5.0.3", ] [[package]] @@ -629,8 +670,18 @@ version = "5.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a32acac15fe1967bc3986b2a6347dffc965602354ea6f450ad07e8bfd253583" dependencies = [ - "alloc-no-stdlib", - "alloc-stdlib", + "alloc-no-stdlib 2.0.4", + "alloc-stdlib 0.2.4", +] + +[[package]] +name = "brotli-decompressor" +version = "6.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75c74c9c51b9140663e196b5fb88bc94c334d7a497c8311cb759d8557874747f" +dependencies = [ + "alloc-no-stdlib 3.0.0", + "alloc-stdlib 0.3.0", ] [[package]] @@ -794,6 +845,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "crypto-common 0.2.2", + "inout", +] + [[package]] name = "clipboard-win" version = "5.4.3" @@ -812,6 +873,12 @@ dependencies = [ "cc", ] +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + [[package]] name = "cmpv2" version = "0.2.0" @@ -830,7 +897,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b77c319abfd5219629c45c34c89ba945ed3c5e49fcde9d16b6c3885f118a730" dependencies = [ - "const-oid", + "const-oid 0.9.6", "der", "spki", "x509-cert", @@ -906,6 +973,12 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + [[package]] name = "core-foundation" version = "0.9.4" @@ -983,6 +1056,12 @@ dependencies = [ "libm", ] +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -1009,14 +1088,50 @@ dependencies = [ "rustix 1.1.5", ] +[[package]] +name = "craft-cfb" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" + [[package]] name = "craft-cms" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ + "md-5", "rayon", ] +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701" + +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18" + +[[package]] +name = "craft-csv" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=387c64892b30fdad491b263cc3d9eac660d88dcc#387c64892b30fdad491b263cc3d9eac660d88dcc" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-designcraft-format" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b1b4602a71d19e09ad33f7752a10d6598cf39b2b#b1b4602a71d19e09ad33f7752a10d6598cf39b2b" +dependencies = [ + "craft-document-bundle", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b1b4602a71d19e09ad33f7752a10d6598cf39b2b)", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-dialogs" version = "0.1.0" @@ -1035,6 +1150,108 @@ name = "craft-docpath" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +[[package]] +name = "craft-document-bundle" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b1b4602a71d19e09ad33f7752a10d6598cf39b2b#b1b4602a71d19e09ad33f7752a10d6598cf39b2b" +dependencies = [ + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b1b4602a71d19e09ad33f7752a10d6598cf39b2b)", + "serde_json", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-docx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "base64 0.23.1", + "chrono", + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "craft-math", + "craft-offcrypto", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "craft-wordmodel", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "log", + "quick-xml 0.41.0", + "thiserror 2.0.21", + "unicode-bidi", + "zeroize", +] + +[[package]] +name = "craft-drawingml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119#2b9a325f57624804ed423aded16695eddb815119" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119)", + "kurbo", + "log", + "serde", +] + +[[package]] +name = "craft-drawingml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "kurbo", + "log", + "serde", +] + +[[package]] +name = "craft-drawingml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "kurbo", + "log", + "serde", +] + +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b1b4602a71d19e09ad33f7752a10d6598cf39b2b#b1b4602a71d19e09ad33f7752a10d6598cf39b2b" + +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#6d0b33c0cd63ae4250c180a04eb4a4d66ab81448" + +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" + +[[package]] +name = "craft-epub" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#6d0b33c0cd63ae4250c180a04eb4a4d66ab81448" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-file-formats" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "serde", + "serde_json", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -1063,6 +1280,156 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-idml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2#899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-idml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" +dependencies = [ + "craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", + "craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701" +dependencies = [ + "craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", + "craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f#cc23ffd4d26f3074808e2187d332564817624f8f" +dependencies = [ + "craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-indd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47bf5650b0d503b97bfb5e841746c4957897d11f#47bf5650b0d503b97bfb5e841746c4957897d11f" +dependencies = [ + "indd", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f#cc23ffd4d26f3074808e2187d332564817624f8f" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-keymap" version = "0.1.0" @@ -1075,23 +1442,145 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-math" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "serde", +] + +[[package]] +name = "craft-metadata" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" +dependencies = [ + "brotli-decompressor 6.3.1", + "chrono", + "craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", + "quick-xml 0.41.0", + "serde", +] + +[[package]] +name = "craft-metadata" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701" +dependencies = [ + "brotli-decompressor 6.3.1", + "chrono", + "craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", + "quick-xml 0.41.0", + "serde", +] + +[[package]] +name = "craft-mime" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18" +dependencies = [ + "chrono", + "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18)", + "encoding_rs", +] + +[[package]] +name = "craft-offcrypto" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "aes", + "base64 0.23.1", + "craft-cfb", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "getrandom 0.3.4", + "hmac", + "sha1 0.11.0", + "sha2 0.11.1", + "thiserror 2.0.21", + "zeroize", +] + +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119#2b9a325f57624804ed423aded16695eddb815119" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119)", + "log", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484)", + "log", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "log", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "log", + "thiserror 2.0.21", +] + [[package]] name = "craft-pdf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "bytemuck", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", "hayro", "hayro-interpret", "hayro-syntax", "krilla", + "krilla-svg", + "thiserror 2.0.21", + "usvg 0.47.0", +] + +[[package]] +name = "craft-pptx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f6cab1980)", + "kurbo", + "log", + "serde", + "serde_json", "thiserror 2.0.21", ] [[package]] name = "craft-prepress" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "craft-cms", "craft-pdf", @@ -1114,6 +1603,93 @@ dependencies = [ "winsafe", ] +[[package]] +name = "craft-psd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=d15bf1f35b17b759870f9e5111b9a5d243f64888#d15bf1f35b17b759870f9e5111b9a5d243f64888" +dependencies = [ + "encoding_rs", + "flate2", + "rayon", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-rtf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701" +dependencies = [ + "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", + "encoding_rs", +] + +[[package]] +name = "craft-sheet" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119#2b9a325f57624804ed423aded16695eddb815119" + +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" +dependencies = [ + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", + "flate2", + "kurbo", + "roxmltree 0.21.2", + "thiserror 2.0.21", + "usvg 0.48.1", + "vectorcraft-geom", + "vectorcraft-pathops", +] + +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f#cc23ffd4d26f3074808e2187d332564817624f8f" +dependencies = [ + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", + "flate2", + "kurbo", + "resvg 0.48.1", + "roxmltree 0.21.2", + "thiserror 2.0.21", + "usvg 0.48.1", +] + +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558)", + "flate2", + "kurbo", + "roxmltree 0.21.2", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1138,7 +1714,7 @@ dependencies = [ [[package]] name = "craft-update" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b" +source = "git+https://github.com/storytold/craft-libs?rev=e98299546f5c5c3c22388d16507f8b5e61a5f280#e98299546f5c5c3c22388d16507f8b5e61a5f280" dependencies = [ "chrono", "dunce", @@ -1152,7 +1728,7 @@ dependencies = [ "semver", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "tar", "tempfile", "ureq", @@ -1181,6 +1757,195 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-wordmodel" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "base64 0.23.1", + "craft-math", + "log", + "serde", + "serde_json", + "thiserror 2.0.21", + "unicode-segmentation", +] + +[[package]] +name = "craft-xlsx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119#2b9a325f57624804ed423aded16695eddb815119" +dependencies = [ + "craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119)", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119)", + "craft-sheet", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119#2b9a325f57624804ed423aded16695eddb815119" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2#899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#6d0b33c0cd63ae4250c180a04eb4a4d66ab81448" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f#cc23ffd4d26f3074808e2187d332564817624f8f" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=2b9a325f57624804ed423aded16695eddb815119#2b9a325f57624804ed423aded16695eddb815119" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=7ad4765ee55496d0ccdb7659ad7a6375c7bbb484#7ad4765ee55496d0ccdb7659ad7a6375c7bbb484" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2#899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b1b4602a71d19e09ad33f7752a10d6598cf39b2b#b1b4602a71d19e09ad33f7752a10d6598cf39b2b" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#6d0b33c0cd63ae4250c180a04eb4a4d66ab81448" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=eed3c28cf016723d004d6f2c79c61424c15d4558#eed3c28cf016723d004d6f2c79c61424c15d4558" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=f6cab1980#f6cab1980bbe422cc00c4b19d9f0f9ba337e1316" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "crc32fast" version = "1.5.3" @@ -1252,6 +2017,24 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", +] + +[[package]] +name = "ctutils" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +dependencies = [ + "cmov", +] + [[package]] name = "cursor-icon" version = "1.2.0" @@ -1270,7 +2053,7 @@ version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" dependencies = [ - "const-oid", + "const-oid 0.9.6", "der_derive", "flagset", "pem-rfc7468", @@ -1307,7 +2090,7 @@ dependencies = [ [[package]] name = "designcraft" -version = "0.20.1" +version = "0.21.0" dependencies = [ "arboard", "craft-update", @@ -1332,8 +2115,10 @@ dependencies = [ [[package]] name = "designcraft-cli" -version = "0.20.1" +version = "0.21.0" dependencies = [ + "craft-file-formats", + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1349,7 +2134,7 @@ dependencies = [ [[package]] name = "designcraft-color" -version = "0.20.1" +version = "0.21.0" dependencies = [ "craft-cms", "kurbo", @@ -1361,7 +2146,7 @@ dependencies = [ [[package]] name = "designcraft-compose" -version = "0.20.1" +version = "0.21.0" dependencies = [ "designcraft-color", "designcraft-doc", @@ -1381,7 +2166,7 @@ dependencies = [ [[package]] name = "designcraft-doc" -version = "0.20.1" +version = "0.21.0" dependencies = [ "chrono", "designcraft-color", @@ -1394,13 +2179,20 @@ dependencies = [ [[package]] name = "designcraft-engine" -version = "0.20.1" +version = "0.21.0" dependencies = [ "caseless", "craft-atomic", + "craft-csv", "craft-docpath", + "craft-file-formats", + "craft-indd", + "craft-mime", "craft-prepress", "craft-print", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1418,13 +2210,12 @@ dependencies = [ "hayro-cmap", "hayro-interpret", "hayro-syntax", - "indd", "kurbo", "log", - "quick-xml 0.41.0", "same-file", "serde", "serde_json", + "sha2 0.10.9", "tempfile", "thiserror 2.0.21", "unicode-segmentation", @@ -1434,9 +2225,10 @@ dependencies = [ [[package]] name = "designcraft-epub" -version = "0.20.1" +version = "0.21.0" dependencies = [ - "chrono", + "craft-epub", + "craft-mime", "designcraft-color", "designcraft-doc", "zip 3.0.0", @@ -1444,7 +2236,7 @@ dependencies = [ [[package]] name = "designcraft-fonts" -version = "0.20.1" +version = "0.21.0" dependencies = [ "core-text", "harfrust 0.13.3", @@ -1457,19 +2249,17 @@ dependencies = [ [[package]] name = "designcraft-format" -version = "0.20.1" +version = "0.21.0" dependencies = [ + "craft-designcraft-format", "designcraft-doc", - "designcraft-images", - "serde", "serde_json", - "thiserror 2.0.21", "zip 3.0.0", ] [[package]] name = "designcraft-geom" -version = "0.20.1" +version = "0.21.0" dependencies = [ "flo_curves", "kurbo", @@ -1482,24 +2272,32 @@ dependencies = [ [[package]] name = "designcraft-idml" -version = "0.20.1" +version = "0.21.0" dependencies = [ + "craft-file-formats", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2)", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2)", "designcraft-color", "designcraft-compose", "designcraft-doc", "designcraft-geom", "designcraft-images", "log", - "quick-xml 0.41.0", "thiserror 2.0.21", "zip 3.0.0", ] [[package]] name = "designcraft-images" -version = "0.20.1" +version = "0.21.0" dependencies = [ "craft-cms", + "craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)", + "craft-file-formats", + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", + "craft-psd", + "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", "designcraft-color", "designcraft-fonts", "flate2", @@ -1507,17 +2305,15 @@ dependencies = [ "jpeg-decoder", "log", "quick-xml 0.41.0", - "resvg", "skrifa 0.47.0", "tiff", - "zune-core", - "zune-jpeg", ] [[package]] name = "designcraft-mcp" -version = "0.20.1" +version = "0.21.0" dependencies = [ + "craft-file-formats", "designcraft-engine", "designcraft-geom", "designcraft-render", @@ -1529,10 +2325,11 @@ dependencies = [ [[package]] name = "designcraft-pdf" -version = "0.20.1" +version = "0.21.0" dependencies = [ "chrono", "craft-cms", + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", "craft-pdf", "craft-prepress", "designcraft-color", @@ -1544,20 +2341,22 @@ dependencies = [ "flate2", "hayro-cmap", "hayro-interpret", - "hayro-syntax", "image", - "krilla-svg", "kurbo", "log", + "skrifa 0.47.0", "thiserror 2.0.21", "tiff", ] [[package]] name = "designcraft-pptx" -version = "0.20.1" +version = "0.21.0" dependencies = [ "chrono", + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", + "craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)", + "craft-pptx", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -1573,9 +2372,10 @@ dependencies = [ [[package]] name = "designcraft-render" -version = "0.20.1" +version = "0.21.0" dependencies = [ "bytemuck", + "craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)", "craft-pdf", "designcraft-color", "designcraft-compose", @@ -1593,16 +2393,21 @@ dependencies = [ [[package]] name = "designcraft-textimport" -version = "0.20.1" +version = "0.21.0" dependencies = [ + "craft-docx", + "craft-file-formats", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2)", + "craft-rtf", + "craft-wordmodel", + "craft-xlsx", "designcraft-doc", - "quick-xml 0.41.0", "zip 3.0.0", ] [[package]] name = "designcraft-tools" -version = "0.20.1" +version = "0.21.0" dependencies = [ "designcraft-color", "designcraft-compose", @@ -1614,9 +2419,10 @@ dependencies = [ [[package]] name = "designcraft-ui-egui" -version = "0.20.1" +version = "0.21.0" dependencies = [ "craft-dialogs", + "craft-file-formats", "craft-keymap", "craft-prepress", "craft-ui", @@ -1648,7 +2454,7 @@ dependencies = [ [[package]] name = "designcraft-web" -version = "0.20.1" +version = "0.21.0" dependencies = [ "designcraft-engine", "designcraft-format", @@ -1684,8 +2490,20 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid 0.10.3", + "crypto-common 0.2.2", + "ctutils", ] [[package]] @@ -2026,7 +2844,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2064,6 +2882,22 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "exr" +version = "1.74.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "multiversion", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "fancy-regex" version = "0.19.2" @@ -2265,6 +3099,17 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "fontdb" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2660c5e9157bf76d2db1294e4a9feba604ef610819a3b591088d0d8392a3290f" +dependencies = [ + "log", + "slotmap", + "tinyvec", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -2430,9 +3275,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi 5.3.0", "wasip2", + "wasm-bindgen", ] [[package]] @@ -2693,7 +3540,7 @@ dependencies = [ [[package]] name = "hayro" version = "0.7.1" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "bytemuck", "hayro-interpret", @@ -2713,7 +3560,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-cmap" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "brotli", "hayro-postscript", @@ -2722,7 +3569,7 @@ dependencies = [ [[package]] name = "hayro-interpret" version = "0.7.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "bitflags 2.13.2", "hayro-cmap", @@ -2740,7 +3587,7 @@ dependencies = [ [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "fearless_simd", "hayro-ccitt", @@ -2758,12 +3605,12 @@ dependencies = [ [[package]] name = "hayro-postscript" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" [[package]] name = "hayro-syntax" version = "0.7.2" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "flate2", "hayro-ccitt", @@ -2811,6 +3658,15 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest 0.11.3", +] + [[package]] name = "http" version = "1.5.0" @@ -2850,6 +3706,15 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "typenum", +] + [[package]] name = "hyper" version = "1.12.0" @@ -3154,6 +4019,7 @@ dependencies = [ "moxcms", "num-traits", "png", + "qoi", "tiff", "zune-core", "zune-jpeg", @@ -3175,6 +4041,12 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e54e57b4c48b40f7aec75635392b12b3421fa26fe8b4332e63138ed278459c" +[[package]] +name = "imagesize" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" + [[package]] name = "indd" version = "0.1.0" @@ -3190,6 +4062,15 @@ dependencies = [ "hashbrown 0.17.1", ] +[[package]] +name = "inout" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9" +dependencies = [ + "hybrid-array", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -3313,6 +4194,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00810f1d8b74be64b13dbf3db89ac67740615d6c891f0e7b6179326533011a07" +[[package]] +name = "jpeg-encoder" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f" + [[package]] name = "js-sys" version = "0.3.106" @@ -3364,7 +4251,7 @@ dependencies = [ [[package]] name = "krilla" version = "0.8.2" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" dependencies = [ "base64 0.22.1", "bumpalo", @@ -3373,7 +4260,7 @@ dependencies = [ "gif", "hayro-write", "image-webp", - "imagesize", + "imagesize 0.14.0", "indexmap", "pdf-writer", "png", @@ -3396,13 +4283,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1237d7c37b16ca9fbc2e72dde13a10d321f9a44aceee35c062bc0a43bfc7ce16" dependencies = [ "flate2", - "fontdb", + "fontdb 0.23.0", "krilla", - "resvg", + "resvg 0.47.0", "skrifa 0.42.1", "smallvec", "tiny-skia 0.12.0", - "usvg", + "usvg 0.47.0", ] [[package]] @@ -3424,6 +4311,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + [[package]] name = "libc" version = "0.2.191" @@ -3525,6 +4418,16 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + [[package]] name = "memchr" version = "2.8.3" @@ -3624,6 +4527,28 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multiversion" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076" +dependencies = [ + "multiversion-macros", + "target-features", +] + +[[package]] +name = "multiversion-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "target-features", +] + [[package]] name = "multiversion_no_op" version = "1.0.0" @@ -3731,7 +4656,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -4156,7 +5081,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4619,6 +5544,15 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "qrcodegen" version = "1.8.0" @@ -4709,7 +5643,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4979,7 +5913,25 @@ dependencies = [ "rgb", "svgtypes", "tiny-skia 0.12.0", - "usvg", + "usvg 0.47.0", + "zune-jpeg", +] + +[[package]] +name = "resvg" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67e3803f97b999e80cbf7c6ecdd07a8102204d92e1633cf48783720c521196bd" +dependencies = [ + "bytemuck", + "gif", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes", + "tiny-skia 0.12.0", + "usvg 0.48.1", "zune-jpeg", ] @@ -5098,7 +6050,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -5157,7 +6109,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -5406,7 +6358,18 @@ checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" dependencies = [ "cfg-if", "cpufeatures 0.2.17", - "digest", + "digest 0.10.7", +] + +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -5417,7 +6380,18 @@ checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", "cpufeatures 0.2.17", - "digest", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", ] [[package]] @@ -5471,12 +6445,12 @@ checksum = "da90c3d9af638898a5fdc347479b18f950bb0dde11ecf2244f94cd28135da53b" dependencies = [ "aws-lc-rs", "base64 0.22.1", - "const-oid", + "const-oid 0.9.6", "der", - "digest", + "digest 0.10.7", "pem", "rand_core 0.10.1", - "sha2", + "sha2 0.10.9", "signature", "sigstore-types", "spki", @@ -5545,7 +6519,7 @@ dependencies = [ "chrono", "cmpv2", "cms", - "const-oid", + "const-oid 0.9.6", "der", "hex", "rand", @@ -5584,7 +6558,7 @@ dependencies = [ "base64 0.22.1", "chrono", "cms", - "const-oid", + "const-oid 0.9.6", "hex", "pem", "rustls-pki-types", @@ -5974,6 +6948,12 @@ dependencies = [ "xattr", ] +[[package]] +name = "target-features" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" + [[package]] name = "tempfile" version = "3.27.0" @@ -5981,10 +6961,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix 1.1.5", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -6539,8 +7519,8 @@ dependencies = [ "base64 0.22.1", "data-url", "flate2", - "fontdb", - "imagesize", + "fontdb 0.23.0", + "imagesize 0.14.0", "kurbo", "log", "pico-args", @@ -6558,6 +7538,33 @@ dependencies = [ "xmlwriter", ] +[[package]] +name = "usvg" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "977d0a4abdef933f424a99fe09f95576e089b90aebc6f016a3bc813762493e91" +dependencies = [ + "base64 0.23.1", + "data-url", + "fontdb 0.24.0", + "harfrust 0.12.0", + "imagesize 0.15.0", + "kurbo", + "log", + "pico-args", + "roxmltree 0.21.2", + "simplecss", + "siphasher", + "skrifa 0.44.0", + "strict-num", + "svgtypes", + "tiny-skia-path 0.12.0", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + [[package]] name = "utf16_iter" version = "1.0.5" @@ -6587,6 +7594,27 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "vectorcraft-geom" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "vectorcraft-pathops" +version = "0.18.0" +source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" +dependencies = [ + "kurbo", + "linesweeper", + "thiserror 2.0.21", + "vectorcraft-geom", +] + [[package]] name = "vello_common" version = "0.0.8" @@ -7196,7 +8224,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -7608,9 +8636,9 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1301e935010a701ae5f8655edc0ad17c44bad3ac5ce8c39185f75453b720ae94" dependencies = [ - "const-oid", + "const-oid 0.9.6", "der", - "sha1", + "sha1 0.10.7", "signature", "spki", "tls_codec", @@ -7688,7 +8716,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" [[package]] name = "xtask" -version = "0.20.1" +version = "0.21.0" dependencies = [ "serde_json", ] @@ -7986,6 +9014,15 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + [[package]] name = "zune-jpeg" version = "0.5.15" @@ -7995,6 +9032,15 @@ dependencies = [ "zune-core", ] +[[package]] +name = "zune-jpegxl" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4" +dependencies = [ + "zune-core", +] + [[package]] name = "zvariant" version = "5.15.1" diff --git a/Cargo.toml b/Cargo.toml index d90986f91..512ba8145 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,21 +4,41 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/designcraft", "apps/designcraft-cli", "xtask"] [workspace.package] -version = "0.20.1" +version = "0.21.0" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" repository = "https://github.com/storytold/designcraft" [workspace.dependencies] +# Shared RFC 4648 encoding; document decoding policies remain local. +craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" } # Shared reader and writer; publish the immutable revision before this consumer. -craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false } +craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.1.0", default-features = false } +craft-designcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "b1b4602a71d19e09ad33f7752a10d6598cf39b2b", version = "=0.1.0" } +craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } +craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2", version = "=0.1.0" } +craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } +craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "cc23ffd4d26f3074808e2187d332564817624f8f", version = "=0.1.0", features = ["tree-render", "tree-text"] } +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "899e396b9e2710fcdf1ad9b6249d6c456b7ca9a2", version = "=0.1.0" } +craft-xml-dtd = { package = "craft-xml", git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.1.0" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "bfea7d9800a9f14e496209f9df30e9ef45655701", version = "=0.1.0" } +craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "2b9a325f57624804ed423aded16695eddb815119", version = "=0.1.0" } +craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "7ad4765ee55496d0ccdb7659ad7a6375c7bbb484", version = "=0.1.0" } +craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "7ad4765ee55496d0ccdb7659ad7a6375c7bbb484", version = "=0.1.0" } +craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "387c64892b30fdad491b263cc3d9eac660d88dcc", version = "=0.1.0" } +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "bfea7d9800a9f14e496209f9df30e9ef45655701", version = "=0.1.0" } +craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "d15bf1f35b17b759870f9e5111b9a5d243f64888", version = "=0.1.0" } +craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "eed3c28cf016723d004d6f2c79c61424c15d4558", version = "=0.1.0" } +craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" } +craft-pptx = { git = "https://github.com/storytold/craft-libs", rev = "f6cab1980", version = "=0.1.0" } # Shared keyboard bindings: chords, overrides, conflicts, egui dispatch, the Keyboard Shortcuts dialog. craft-keymap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui", "ui"] } # Separations, ink coverage and the ink manager; same revision as craft-pdf and the krilla patch. -craft-prepress = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["flatten"] } +craft-prepress = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.1.0", features = ["flatten"] } # craft-update: app-specific pretend versions apply to startup and manual checks. -craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] } +craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "e98299546f5c5c3c22388d16507f8b5e61a5f280", features = ["egui"] } # internal designcraft-geom = { path = "crates/geom" } designcraft-color = { path = "crates/color" } @@ -66,25 +86,15 @@ vello_cpu = "0.2" vello_common = { version = "0.2", default-features = false } # Decoded image buffers become pixmap pixels without a copy; the version vello_common uses. bytemuck = { version = "1.25", features = ["extern_crate_alloc"] } -image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } +image = { version = "0.25", default-features = false } # CMYK TIFF samples for PDF export (`image` decodes them to RGB); the version `image` uses. tiff = "0.11" -# SVG (same usvg as krilla-svg). -resvg = { version = "0.47", default-features = false, features = ["text"] } -krilla-svg = "=0.8.1" # Boolean operations on Bézier paths (Pathfinder). flo_curves = "0.8" # QR codes (Object › Generate QR Code); MIT. qrcodegen = "1.8" quick-xml = "0.41" zip = { version = "3", default-features = false, features = ["deflate"] } -# INDD/INDT → IDML conversion for File › Open: storytold/indd-utils, pinned by rev as every other -# git dependency here is. It parses untrusted INDD files, and `branch = "main"` meant any -# `cargo update` -- Dependabot runs them -- pulled whatever was on that branch into the parser -# without review (#1599). -# (MIT OR Apache-2.0, no dependencies, builds for wasm). -indd = { git = "https://github.com/storytold/indd-utils", rev = "7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" } - # Which paths stored in documents may be followed (craft-libs). craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } # Atomic file replacement for saves and exports (craft-libs). @@ -93,7 +103,7 @@ craft-atomic = { version = "0.1.0", git = "https://github.com/storytold/craft-li craft-print = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } # ICC colour management: working spaces, transforms, black point compensation (craft-libs). -craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } +craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813" } # ui # Shared frontend components; keep this dependency above the engine boundary. @@ -143,16 +153,16 @@ debug = 0 # Opt-in cancelable macOS termination on the pinned winit 0.30 line. [patch.crates-io] -krilla = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.8.2" } +krilla = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.8.2" } winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } # The hayro crates from craft-libs' vendor/ directory (pdfcraft's, plus the plate mode and decode # limits craft-pdf and craft-prepress need); same revision as craft-pdf. -hayro = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.1" } -hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.0" } -hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.2" } -hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.3.0" } -hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0" } -hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0" } +hayro = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.7.1" } +hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.7.0" } +hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.7.2" } +hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.3.0" } +hayro-cmap = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.1.0" } +hayro-postscript = { git = "https://github.com/storytold/craft-libs", rev = "a5ac3aa05cf3ec013961a85599ddd9e0b5c03813", version = "=0.1.0" } ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } diff --git a/ROADMAP.md b/ROADMAP.md index ac73acaac..25b4c967a 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ - 2026-10-23: placed-artwork layer options survive IDML and native round trips per placement, independently of shared assets. Unsupported visibility overrides are explicit import/preflight/PDF warnings; format 4 prevents older native readers from silently discarding them. Rendering parity is still open; see [graphic-layer-options.md](docs/graphic-layer-options.md). - FID1: PDF export normalizes mixed emitted gradient color spaces to RGB with an explicit warning; homogeneous device-space gradients retain their components. Linear/radial, midpoint, archival and print policies have synthetic regression coverage. -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-11-06 · **Change:** minor (Pages panel parents and alternate layouts; File ▸ Export Layouts and Versions; placed graphic layer-option preservation and explicit rendering warnings; File ▸ Open Recent; .indd and .indt open through indd-utils; text threads by hand; the alpha gate waits on Primary Text Frame page fixes; Japanese print, import and table controls localized; Live corners (#186); Optical kerning from outlines (#181); window and menu quit confirmation; failed writes preserve live save and collection state; spring-loaded Zoom navigation; orderly native exit recovery; Pages panel parents; cancelable macOS Dock Quit; text stroke behind fill; Based on Parent; object styles carry effects; table anchors keep their own paragraphs; grouped HTML/EPUB reading order; Links source paths; XLSX cached cell values retain numeric precision and text; automation roots refuse superscript COM/LPT names and a device name before a colon) · **Target:** Adobe InDesign 2026 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-11-15 · **Change:** minor (obsolete CI gap removed; tracked-change selection mapping; Pages panel parents and alternate layouts; File ▸ Export Layouts and Versions; placed graphic layer-option preservation and explicit rendering warnings; File ▸ Open Recent; .indd and .indt open through indd-utils; text threads by hand; the alpha gate waits on Primary Text Frame page fixes; Japanese print, import and table controls localized; Live corners (#186); Optical kerning from outlines (#181); window and menu quit confirmation; failed writes preserve live save and collection state; spring-loaded Zoom navigation; orderly native exit recovery; Pages panel parents; cancelable macOS Dock Quit; text stroke behind fill; Based on Parent; object styles carry effects; table anchors keep their own paragraphs; grouped HTML/EPUB reading order; Links source paths; XLSX cached cell values retain numeric precision and text; automation roots refuse superscript COM/LPT names and a device name before a colon) · **Target:** Adobe InDesign 2026 - AR1.1: Kashida justification tests verify space fallback without Tatweel fonts using an isolated font database, while retaining elongation assertions when fonts support it. - AR1: Arabic IDML controls now retain character direction, kashida switches, diacritic offsets/forms, paragraph policy values and independent story/table directions. Shaping preserves joining context and script/language selection; paragraph-level bidi, contextual digit conversion, mark-safe elongation, RTL columns/tables and physical column insertion have regression coverage. Vendor-specific justification and diacritic presets remain explicit preflight warnings; see docs/arabic-typography.md. @@ -12,6 +12,7 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, open (documented JSON format + IDML), scriptable by agents (MCP), and available on the web. +- **2026-11-14** · Files placed from disk are linked instead of embedded: the document keeps a 1024 px proxy and a content hash, opens without reading the originals, and shows link status OK, Modified or Missing, with Update, Relink, Embed and Unembed (native format 5; old embedded documents migrate on save). Several images load into one place cursor, a grid or a contact sheet; see [linked-artwork.md](docs/linked-artwork.md) and [multi-image-placement.md](docs/multi-image-placement.md). No readiness percentage change. - **2026-11-06** · Automation roots refuse `COM¹` to `LPT³` and a device name before a colon (`COM1:`, `NUL:x`). A batch-export layout of that name is written with a leading `_` on every platform. No readiness percentage change. - **2026-10-30** · Document Fonts lookup treats a bare document filename's empty parent as the current directory (#2014), preserving case-insensitive discovery and automation read-root checks. No readiness percentage change. - **2026-10-27** · Automatic recovery writes off the interface thread, straight to disk: on a @@ -64,7 +65,7 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, ope - Document model: facing/non-facing spreads, parent pages with page-number markers, sections, layers, frames (text / graphic / unassigned), threaded stories, paragraph/character/object styles with based-on, swatches (process/spot/tints/gradients), corner options, text wrap, drop shadow, opacity/blend. - Text engine: Knuth–Plass paragraph composer and single-line composer, dictionary hyphenation (public-domain Moby list + our own trained Liang patterns), justification with word/letter/glyph-scaling ranges, keep options (keep with next, keep lines together, widows/orphans), balance ragged lines, optical margin alignment, hyphenation zone/limit, columns, threading across frames and spreads, tabs, bullets/numbering, rules, shading, baseline grid, first-baseline options, vertical justification, wrap exclusions, overset detection, caret/hit testing. - FID2: IDML underline/strikethrough Text Color resets survive style inheritance and roundtrip. Raster and PDF paint underlines behind glyphs, retain strikethrough above, and center rules on their offsets; synthetic overlap tests cover both backends. -- Rendering: vello_cpu (SIMD, multithreaded) — pages, items, images with mip levels, gradients, composed text. +- Rendering: vello_cpu (SIMD, multithreaded) — pages, items, images with mip levels (only the levels drawn are cached; the canvas decodes on background threads and shows a light box or another cached level meanwhile), gradients, composed text. - UI (egui): application bar + menus, Control panel (object and text modes), Tools panel with flyouts, document tabs, rulers, pasteboard, guides (margins, columns, bleed, baseline grid), frame edges, selection handles, text ports and threads, Properties / Pages / Layers dock, Swatches, Styles, Character, Paragraph, Stroke, Text Wrap panels, New Document and Preferences dialogs, Quick Apply (⌘Return), 4 brightness themes. - Pen tool, Direct Selection anchor/handle editing, rotate from corners, Rotate/Scale/Shear tools, Eyedropper, snapping & smart guides, Align/Distribute, Find/Change (text + GREP), Story Editor, Paragraph Style Options, Effects panel, hidden characters, native macOS menu bar, measured InDesign 2026 look (Medium Dark): Properties sections per selection state, 21 pt spinner fields with arithmetic, Contextual Task Bar, inverse (black) text selection, Pages panel with drag-and-drop reorder / parent apply, Layers panel with per-object rows. - Tools: Selection (click, marquee, move, Alt-duplicate, resize handles), Direct Selection, Type (draw frame, click caret, select, type), Rectangle/Ellipse/Polygon (+ frame variants), Line, Hand, Zoom. @@ -110,7 +111,7 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, ope - Endnotes (`endnote.insert/edit/delete/list/options`, Type ▸ Insert Endnote): references number through the document, the endnote frame on a new last page lists them under a heading; IDML keeps the listed text only. - Editorial notes (`note.new/edit/delete/convertToText/list`, Notes panel): anchored in text, flagged on screen, never printed or exported. - File ▸ Print Booklet (`file.printBooklet`): saddle-stitch or 2-up consecutive printer spreads as PDF, padded with blanks, with a gap between pages. -- Edit ▸ Transparency Blend Space (`edit.transparencyBlendSpace`; RGB for web/mobile documents; IDML TransparencyPreference) — exported PDFs blend in that space: CMYK print exports write their transparency groups and page groups in DeviceCMYK (PDF/X-4 also carries the CMYK working space as its `GTS_PDFX` output intent); RGB exports give pages with transparency a DeviceRGB page group. On screen, spreads with transparency in a CMYK blend space are shown through the working CMYK profile (their colors keep to its gamut, as when composited in CMYK). +- Edit ▸ Transparency Blend Space (`edit.transparencyBlendSpace`; RGB for web/mobile documents; IDML TransparencyPreference) — exported PDFs blend in that space: CMYK print exports write their transparency groups and page groups in DeviceCMYK (PDF/X-4 also carries the CMYK working space as its `GTS_PDFX` output intent; plain print, PDF/X-4, booklet and CMYK-containing RGB exports attach the working CMYK profile as `/DefaultCMYK`); RGB exports give pages with transparency a DeviceRGB page group. On screen, spreads with transparency in a CMYK blend space are shown through the working CMYK profile (their colors keep to its gamut, as when composited in CMYK). - High Contrast interface theme (fifth Interface Color Theme: black chrome, white text, yellow selection). - Place Excel workbooks (.xlsx): the first worksheet's used range becomes a table (shared/inline strings, numbers, booleans). - Object Library (`library.new/open/add/place/remove/list/json/close`, Library panel): `.dclib` files of snippets that keep stories, styles, swatches and images. @@ -375,11 +376,10 @@ Ranked; detail and estimates in [docs/roadmap.md](docs/roadmap.md), the full lis 1. **Alpha gate:** Primary Text Frame page fixes: autoflow adds pages users can't delete (#165) (5–7 h) 2. Basic editing on every platform: copy/paste, context menus, caret formatting (30–50 h) -3. CI running `cargo xtask ci` on pull requests (4–8 h) -4. INDD real-file corpus: measure what `.indd` files keep through indd-utils (10–30 h) -5. IDML real-file corpus and fixes (60–100 h) -6. Composition oracle against InDesign; optical kerning tuning; overset rule (70–125 h) -7. Start on any GPU; find every installed font (25–50 h) +3. INDD real-file corpus: measure what `.indd` files keep through indd-utils (10–30 h) +4. IDML real-file corpus and fixes (60–100 h) +5. Composition oracle against InDesign; optical kerning tuning; overset rule (70–125 h) +6. Start on any GPU; find every installed font (25–50 h) ## Documents @@ -395,6 +395,15 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m ## Progress log - **2026-11-13** · A placed Photoshop file comes in at its own resolution (ResolutionInfo), as other rasters do since #1991: a 300 ppi PSD was placed four times too large at 72 ppi. No readiness percentage change. +- **2026-11-15** · Removed the obsolete gap saying pull requests have no test workflow: [CI](.github/workflows/ci.yml) runs the shared quality gate on every pull request and push to main (#1352). No readiness percentage change. +- **2026-11-15** · Accepting tracked deletions or rejecting insertions follows body-text selections through the removed text, including resolving all changes, and preserves independent cell and footnote insertion points. No readiness percentage change. +- **2026-11-15** · Format follow-ups to the craft-libs cutover (craft-libs#778; ported from #2734 by Dylan M. Taylor): IDML export keeps CDATA image contents and table spans such as `inf` reach the table edge; DOCX plain-text import leaves out deleted tracked changes and expands inline objects; an XLSX number cell that isn't a number shows as written; unmarked CMYK JPEGs keep the shared decoder's ink polarity; PPTX export writes exact opacity; PDF export repins craft-pdf and craft-prepress to craft-libs main. No readiness percentage change. +- **2026-11-14** · `object.stroke` refuses a tint or gap tint outside 0 to 1, a weight outside 0 to 1000 pt, a miter limit outside 1 to 500, a gap overprint that isn't true or false, and an alignment, cap, join or arrowhead name it doesn't have, instead of clamping the numbers and skipping the names. The Stroke panel's Weight and Miter Limit fields keep their values in those ranges. No readiness percentage change. +- **2026-11-14** · Photo books no longer freeze the canvas (#1411; based on #2051 by texjer). The image cache keeps only the mip level each image is drawn at, for every format and display quality: a TIFF, PNG, PSD or CMYK JPEG used to keep its full decode too (48 MB for a 12 MP photo), so after about a dozen photos each edit evicted and decoded them again on the render thread. The canvas no longer waits for decodes: an image not cached at its size shows a light box or another cached level and decodes on one of three background threads (within the same in-flight memory limit as the decodes ahead of a render), and finished decodes are drawn in batches, at most one canvas render every 250 ms. Zooming into an image decodes it at most twice. Thumbnails and exports still wait for every image. No readiness percentage change. +- **2026-11-14** · IDML export keeps each endnote reference as its number in superscript text, as the endnote frame lists it, instead of dropping it; the endnotes are still written as plain text, without IDML's endnote elements (#2753). No readiness percentage change. +- **2026-11-14** · Object › Text Frame Options shows the inset spacing (one Inset for a non-rectangular frame), the column width and gutter and the paragraph spacing limit in the document's ruler units, as the Properties panel does, instead of always in picas, and a bare number typed in any of its measure fields is in those units (#2739). No readiness percentage change. +- **2026-11-12** · A render decodes the placed images in view that are not cached yet on several threads before it draws, instead of one after another while drawing. Page thumbnails, image exports and the first canvas frame of a page with six 24 MP photos take about 1.2 s instead of 2.8 s. No readiness percentage change. +- **2026-11-13** · PDF exports outside PDF/X-1a and PDF/A-2b attach the CMYK working space's ICC profile as `/DefaultCMYK` (`[/ICCBased …]`) on every page, inherited `/Pages` resource dict, Form XObject, SMask form, tiling pattern, and Type3 font resource scope (#2743). Plain print exports and booklets embed one shared Flate-compressed `/N 4` profile stream without adding `/OutputIntents` or `/Trapped`; PDF/X-4 reuses its catalog `/DestOutputProfile`; RGB-blend documents attach `/DefaultCMYK` when they draw in `DeviceCMYK` and skip it for pure RGB. Viewers that honor `/DefaultCMYK` instead of `/OutputIntents` (including Apple Quartz and MuPDF) now render CMYK colors consistently with the working space across both plain and PDF/X-4 exports. No readiness percentage change. - **2026-11-12** · Properties › Text Frame shows the frame's inset spacing (Top, Bottom, Left and Right with a link that makes all four the same; one Inset for a non-rectangular frame) and four Vertical Justification buttons (Top, Center, Bottom, Justify), and the section also shows with the Type tool in a frame, where it changes the frame that shows the insertion point. No readiness percentage change. - **2026-11-12** · An edit of a long threaded story composes the paragraphs it can change and copies the other lines from the composition before the edit; every typed character composed the whole story. In a 240-paragraph story a typed character composes 3 paragraphs (240 before). An edit that moves lines into other frames composes on until a paragraph starts in the same frame and place as before, or to the story's end, and stories with footnotes, tables, anchored objects, balanced columns or keep options that move lines are composed whole. No readiness percentage change. - **2026-11-12** · Composing a story with endnotes, anchored objects or cross-references counts their marks in one pass over the story, instead of counting again from the story start for every paragraph. No readiness percentage change. @@ -406,6 +415,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.m - **2026-11-12** · The status bar's preflight count and the Preflight panel take their issues from a check on a background worker, which checks the newest state of the document and shows the issues last found meanwhile. Moving or resizing a frame and typing no longer wait for a check of the whole document on every drag step and keystroke. No readiness percentage change. - **2026-11-12** · Object › Text Frame Options… (Cmd+B) opens with the Type tool in a frame, so inset spacing and vertical justification can be changed while typing; it changes the frame that shows the insertion point, or every frame that holds part of the selected text (#2396). No readiness percentage change. +- **2026-11-05** · Document tabs that overflow the bar scroll (trackpad, wheel, arrows), keep the active tab in view and list every open document in a menu. No readiness percentage change. - **2026-11-04** · Striped strokes retain out-and-back cubic segments whose second control point is away from the coincident endpoints and first control point. A geometry regression checks the offset extent and both stripe bands. No readiness percentage change. - **2026-11-04** · Esperanto is a text language (Character › Language, `type.char {language: "Esperanto"}`, IDML `$ID/Esperanto`): tag `eo`, English-style quotes; not hyphenated yet (#1201). No readiness percentage change. @@ -430,6 +440,9 @@ Newest first. - **2026-10-30** · Import warnings about a story have a Show button: it closes the warnings (the status line keeps them) and selects the story's first frame, fitted in the window. `file.open` returns `warningItems`, the frame each IDML or InDesign warning is about (or null), matching the converter's INDD story numbers (`story 30892`, written to IDML as `u78ac`) and IDML `Self` names to the imported stories. No readiness percentage change. - **2026-10-30** · Parent pages: a running-header variable shows its name in angle brackets on the parent itself (it was blank), as InDesign shows it; on the document pages, and in text with no page, it shows its text as before. No readiness percentage change. - **2026-10-30** · Without facing pages, a page of a multi-page spread shows the parent page in its place, as in InDesign: the second page of a two-page spread shows the parent's second page. Every single-sided page showed the parent's first page, so an InDesign book set up without facing pages but laid out in two-page spreads repeated the left parent page's items (a black heading bar) on its right pages and lost the right parent page's. No readiness percentage change. +- **2026-10-30** · Dropping images from the file manager works as in InDesign: images dropped together load the place cursor in the order they came, and one image dropped off a frame that takes it loads the place cursor instead of being placed at once (on a frame it still fills it). Documents and text files dropped keep opening and flowing as before. Ported from #1307 and #1666; see [multi-image placement](docs/multi-image-placement.md). No readiness percentage change. +- **2026-10-30** · File › Place Contact Sheet… puts every image loaded in the place cursor on new pages, in a grid of equal frames (`place.contactSheet`), as one undo step. Ported from #1392; see [multi-image placement](docs/multi-image-placement.md). No readiness percentage change. +- **2026-10-30** · Placing several images works as in InDesign. File › Place takes several files, and they load the place cursor in the order chosen: it shows the first one's thumbnail with how many are loaded ("(6)"), each click places the next, arrow keys while dragging lay out a grid (`place.grid`), and Escape unloads the rest (`place.cancel`). Switching, opening or closing a document unloads it too, back to the Selection tool. A drag with the cursor draws the frame at the graphic's proportions, with its scale. Ported from #1307, #1392 and #1432 (multi-image placement, cursor thumbnail and count); see [multi-image placement](docs/multi-image-placement.md). No readiness percentage change. - **2026-10-30** · macOS: a file dragged in from the Finder fills the frame it is dropped on. winit sends no pointer events during such a drag, so the drop asks the system where the pointer is (`Services::pointer`, from `NSEvent.mouseLocation`), at any UI scale. No readiness percentage change. - **2026-10-30** · File > Package opens a dialog, as the reference app's: a Summary of fonts, links and images and inks, pages for each, a Printing Instructions step, and a PDF made with the options of a preset (#1514, #1515). - **2026-10-30** · IDML import reads TOC separators and entry styles written with the `$ID/` prefix or as `Properties` children, and the TOC separator notation (`^t`, `^y`, `^m`, `^>`) is shared by the dialog and IDML, with a special-character menu on the separator field (#1249, #1255). @@ -443,6 +456,7 @@ Newest first. - **2026-10-26** · Inner Shadow, Inner Glow (from the edges) and Bevel and Emboss on a frame holding an image follow what the image paints: a transparent part is outside the object, so the effect lines the image's opaque edge, not the frame (#1622). Satin and a centred Inner Glow still follow the frame. No readiness percentage change. - **2026-10-25** · Duplicate Layer (`layer.duplicate`; the layer's context menu in the Layers panel, or a layer dropped on Create New Layer) adds a copy named after the layer with " copy" added ("Art copy" for Art) above the layer, with its settings, a copy of every object on it and copies of its ruler guides, on document and parent pages, in one undo step. Copied frames stay threaded to the copies of frames on the same layer and are not threaded to frames on other layers. A copied override of a parent item overrides that item's copy, and pages hide the copy where they hide the parent item. Hyperlinks on copied objects and text get copies that go to the same destinations. No readiness percentage change. - **2026-10-24** · Column rules run from the top line's ascent to the bottom line's descent, as InDesign draws them, instead of from the top to the bottom of the columns, on screen, in PDF and in PowerPoint export. The top and bottom insets shorten the rule from those lines. Existing documents draw their column rules at the new length. No readiness percentage change. +- **2026-10-24** · Table › Cell Options › Diagonal Lines… (`table.setCell` `diagonal`, `table.getCellDiagonal`): as in InDesign, a cell can carry a diagonal line from the top left corner to the bottom right, from the top right to the bottom left, or both, with the weight, type, color, tint, gap color and gap tint that cell edges take, drawn in front of the cell's contents or behind them. The lines draw on screen and in PDF and are saved in the native format. No readiness percentage change. - **2026-10-20** · Edit › Keyboard Shortcuts is craft-keymap's shared dialog: commands grouped by menu with Show and Filter, recording that asks before taking a key from another command, undo and redo, named shortcut sets (edits of Default become Custom; an imported set becomes a set of its own), an on-screen US keyboard, InDesign Show Set files reviewed before they apply, JSON export and an HTML summary. ui.json keeps the `shortcuts` map earlier versions read, for the active set. No readiness percentage change. - **2026-10-19** · Import InDesign Set… runs on craft-keymap's importer: a set's Window menu panel items (Layers F7, Pages F12, Swatches F5) and Tools items set the panel and tool shortcuts, all of an item's Default keys apply, and the report adds numeric keypad keys read as main keys and likely commands for unmatched items (`window.importShortcuts`). On InDesign's Windows defaults it matches 366 of 1,629 items (was 233) and changes 23 commands. No readiness percentage change. - **2026-10-19** · Keyboard shortcuts run on the shared craft-keymap crate. Panels take shortcuts, with InDesign's defaults where the key is free (Swatches F5, Color F6, Layers F7, Info F8, Stroke F10, Paragraph Styles F11, Character Styles Shift+F11, Pages F12, Align Shift+F7); tool keys are editable in Edit › Keyboard Shortcuts and `window.setShortcut`, and tools sharing a key take turns (B). Saved shortcuts load unchanged. No readiness percentage change. @@ -469,6 +483,7 @@ Newest first. - **2026-10-23** · `designcraft-cli mcp` takes `--automation-read-root` and `--automation-write-root` more than once, as the suite's other apps do (#1552): a path inside any root of its kind passes, and the roots' common parent, `..` or a link out of them and the other kind's roots stay refused. No readiness percentage change. - **2026-10-23** · Layout › Go Back and Go Forward (Cmd+Page Up / Cmd+Page Down, Ctrl on Windows and Linux) retrace the jumps between pages (Go to Page, the page and spread commands, the Pages panel) as InDesign's do, returning to where the view was on each page at the current zoom; a new jump after going back drops the pages ahead, scrolling adds no entry, and pages deleted since are skipped. No readiness percentage change. - **2026-10-24** · The Story Editor has InDesign's vertical depth ruler between the style column and the text, and View › Story Editor › Show/Hide Depth Ruler (`view.storyEditorDepthRuler`) shows or hides it in the Story Editor, remembered with the preferences. The ruler is marked in the document's vertical ruler units with how deep the story's text fills its frames, counted through its columns and threaded frames (lines set side by side, in the sub-columns of a split paragraph or on both sides of a wrap object, count once), and is shaded down to the overset point, where a red line now crosses the editor. No readiness percentage change. +- **2026-10-24** · View › Story Editor › Show/Hide Style Name Column (`view.storyEditorStyleColumn`) shows or hides the Story Editor's column of paragraph style names, as in InDesign, and dragging the bar between the column and the text makes the column wider or narrower (40 to 400 points; the command's `width` sets it too). Both settings are remembered with the preferences, so the editor opens with the same column the next time. No readiness percentage change. - **2026-10-24** · Split Note (`note.split {story, id, at}`, the Notes panel's Split Note button): as in InDesign, a note splits in two at the insertion point in its text. DesignCraft moves the text after it into a new note by the same author, anchored right after the first. No readiness percentage change. - **2026-10-24** · Type › Notes › Previous Note and Next Note (`note.previous`, `note.next`): as in InDesign, they move the insertion point from note to note in the story, putting the caret after the note's anchor. Each item is unavailable when no note lies in its direction in the story, or when the insertion point is in a table cell or a footnote. No readiness percentage change. - **2026-10-24** · Type › Notes › Convert to Note (`note.convertFromText {author?}`): as in InDesign, the selected text leaves the story and becomes the text of a new note, whose anchor takes its place; Convert Note to Text puts it back. DesignCraft refuses a selection in a table cell, or one that holds a note, footnote, endnote, anchored object, index marker, text anchor, cross-reference or table. No readiness percentage change. @@ -525,6 +540,7 @@ Newest first. - **2026-10-16** · Lock Guides and Lock Column Guides are commands in View ▸ Grids & Guides (`view.lockGuides`, ⌥⌘;, and `view.lockColumnGuides`), so the control channel and MCP reach them. Lock Column Guides is an undo step, is saved with the document, on in new ones, and read from and written to IDML's `ColumnGuideLocked`, which export always wrote as locked (#1096). No readiness percentage change. - **2026-10-16** · IDML: a page keeps column guides that were dragged off centre (InDesign's Custom columns). They were divided evenly on import, so the gutter sprang back to the middle; the imported positions now place the columns, with the canvas guides, snapping and text placement following them, and they are written back on export. Layout › Margins and Columns re-divides them evenly when the count or gutter changes, as InDesign's does. No readiness percentage change. - **2026-10-16** · IDML: custom column positions are checked before they are used (#1096). A set must give two edges per column, ascending from the left margin to the right one; any other (a wrong count, unsorted or non-numeric values, a list longer than 216 columns allow, or positions left behind by a margin or page-size change) is the even split, in the canvas, on import and on export, instead of columns drawn outside the margins. No readiness percentage change. +- **2026-10-21** · Layer Options has InDesign's Lock Guides: the layer's ruler guides can't be moved or deleted while its objects stay editable (`layer.set {lockGuides}`), and IDML reads and writes the layer's `LockGuides` (it was always written as false). The options are arranged as InDesign's: Show Layer and Show Guides, Lock Layer and Lock Guides, Print Layer. Found in the Learn Skills Daily course, chapter 32. No readiness percentage change. - **2026-10-20** · Smart guides: an edge being drawn or resized snaps to the page's centre line, as in InDesign, so a frame drawn from the top margin to the middle of the page stops there instead of at the nearest baseline grid line. No readiness percentage change. - **2026-10-21** · An empty graphic frame's X joins its corners, so a frame slanted with Direct Selection keeps its X corner to corner, as InDesign draws it; it used to stay across the old rectangle. Found in Zero2Graphic's *How to Page Layout design in Adobe InDesign 2022 CC*. No readiness percentage change. - **2026-10-21** · Paragraph Border and Paragraph Shading link their four sides as InDesign's: the stroke weights, offsets and corners have the "Make all settings the same" link, on when the four are the same, so a side typed in sets all four; with the link off a side is set alone (a border with only a thick left side). Found in the Learn Skills Daily course, chapter 40. No readiness percentage change. @@ -868,6 +884,7 @@ Newest first. | Date | Change | Summary | |---|---|---| +| 2026-11-15 | minor | Track Changes resolution maps body selections through removed text and preserves nested text positions | | 2026-10-12 | minor | Manual text threading landed; alpha distance 15–25 h → 5–7 h (Primary Text Frame page fixes, #165) | | 2026-10-11 | minor | Localized Japanese print, import and table controls; readiness estimates unchanged | | 2026-10-10 | minor | Full number 42% → 47% (plain weighted sum, no new evidence); readiness table with hours per audience | @@ -885,3 +902,5 @@ The shared craft-installer UI preserves this revision's install options and prov - **2026-10-15** · Windows recovery folders validate the identity of the live locked file without reading it through a second handle. Missing or replaced locks disable writes and cleanup; regression tests cover retained exclusive locking and replacement-file protection. No readiness percentage change. - PDF reader infrastructure: pinned shared parsing/rasterization, transformed page-box geometry and explicit failures for known incomplete layered-PDF exports. Application ownership and parity targets are unchanged. + +- Format reuse: HTML data URIs and file serialization use craft-mime Base64 encoding; existing decoder policies and document budgets remain unchanged. diff --git a/apps/designcraft-cli/Cargo.toml b/apps/designcraft-cli/Cargo.toml index ab2464147..d214b9d84 100644 --- a/apps/designcraft-cli/Cargo.toml +++ b/apps/designcraft-cli/Cargo.toml @@ -22,5 +22,7 @@ designcraft-compose = { workspace = true } designcraft-format = { workspace = true } designcraft-pdf = { workspace = true } designcraft-mcp = { workspace = true } +craft-file-formats = { workspace = true } +craft-image-codecs = { workspace = true } image = { workspace = true } serde_json = { workspace = true } diff --git a/apps/designcraft-cli/src/main.rs b/apps/designcraft-cli/src/main.rs index 79997e0ae..848929d53 100644 --- a/apps/designcraft-cli/src/main.rs +++ b/apps/designcraft-cli/src/main.rs @@ -42,7 +42,7 @@ use designcraft_engine::Session; use serde_json::{Value, json}; /// The usage block. Shared by `--help` (stdout, success) and an unknown command (stderr, failure). -const USAGE: &str = "usage: designcraft-cli run [--in FILE | --sample] [--cmd ID[=JSON]]... [--page N] [--scale S] [--pdf-options JSON] [--export OUT] [--all-pages DIR]\n (--page, --scale and --pdf-options apply to the exports that follow them)\n designcraft-cli commands [FILTER]\n designcraft-cli describe COMMAND\n designcraft-cli script [FILE|-] [--in FILE | --sample] [--connect PORT] [--save OUT] [--export OUT] [--keep-going]\n designcraft-cli app [--port PORT] COMMAND [JSON] | --method METHOD [JSON]\n designcraft-cli mcp [--connect PORT] [--sample] [--automation-read-root DIR]... [--automation-write-root DIR]...\n the roots confine every file the agent's commands read (open, placed images, data sources, libraries) and\n write (save, export, package) to those folders, links followed. Each may be given more than once; a kind\n left out grants none of its access.\n They run the in-process server, so they conflict with --connect (a running app is not confined by them).\n designcraft-cli perf [--pages N] [--runs N] [--strict]\n designcraft-cli bench FILE [--runs N]\n designcraft-cli validate-pdf FILE.pdf [--standard x4|x1a] # built-in PDF/X checks (including visible unembedded fonts; the standard defaults to the file's own)\n designcraft-cli thumbnail FILE.designcraft OUT.png [SIZE] # the saved first-page preview, at most SIZE px\n designcraft-cli links\n designcraft-cli --version"; +const USAGE: &str = "usage: designcraft-cli run [--in FILE | --sample] [--cmd ID[=JSON]]... [--page N] [--scale S] [--pdf-options JSON] [--export OUT] [--all-pages DIR]\n (--page, --scale and --pdf-options apply to the exports that follow them)\n designcraft-cli commands [FILTER]\n designcraft-cli describe COMMAND\n designcraft-cli script [FILE|-] [--in FILE | --sample] [--connect PORT] [--save OUT] [--export OUT] [--keep-going]\n designcraft-cli app [--port PORT] COMMAND [JSON] | --method METHOD [JSON]\n designcraft-cli mcp [--connect PORT] [--sample] [--automation-read-root DIR]... [--automation-write-root DIR]...\n the roots confine every file the agent's commands read (open, placed images, data sources, libraries) and\n write (save, export, package) to those folders, links followed. Each may be given more than once; a kind\n left out grants none of its access.\n They run the in-process server, so they conflict with --connect (a running app is not confined by them).\n designcraft-cli perf [--pages N] [--frames N] [--chars N] [--images N] [--runs N] [--strict]\n designcraft-cli bench FILE [--runs N]\n designcraft-cli validate-pdf FILE.pdf [--standard x4|x1a] # built-in PDF/X checks (including visible unembedded fonts; the standard defaults to the file's own)\n designcraft-cli thumbnail FILE.designcraft OUT.png [SIZE] # the saved first-page preview, at most SIZE px\n designcraft-cli links\n designcraft-cli --version"; /// `script --help`: the script syntax, otherwise only in `describe script.run` and docs/agents.md. const SCRIPT_HELP: &str = r#"usage: designcraft-cli script [FILE|-] [--in FILE | --sample] [--connect PORT] [--save OUT] [--export OUT] [--keep-going] @@ -319,16 +319,17 @@ fn names_the_same_file(input: &std::path::Path, out: &std::path::Path) -> bool { /// as it is; it is decoded anyway, so a broken one is an error rather than an unreadable thumbnail. fn fit_png(png: Vec, size: Option) -> Result, String> { // The preview comes from the file: it may claim any size. - let mut limits = image::Limits::default(); - limits.max_image_width = Some(4096); - limits.max_image_height = Some(4096); - let mut reader = image::ImageReader::with_format(std::io::Cursor::new(&png), image::ImageFormat::Png); - reader.limits(limits); - let img = reader.decode().map_err(|e| e.to_string())?; - let Some(size) = size.filter(|s| img.width().max(img.height()) > *s) else { return Ok(png) }; - let mut out = std::io::Cursor::new(Vec::new()); - img.resize(size, size, image::imageops::FilterType::Triangle).write_to(&mut out, image::ImageFormat::Png).map_err(|e| e.to_string())?; - Ok(out.into_inner()) + let info = craft_image_codecs::inspect_raster(&png).map_err(|e| e.to_string())?.ok_or("invalid PNG preview")?; + if info.width > 4096 || info.height > 4096 { + return Err("preview is larger than 4096 pixels".into()); + } + let decoded = craft_image_codecs::decode(&png).map_err(|e| e.to_string())?; + let Some(size) = size.filter(|s| decoded.width().max(decoded.height()) > *s) else { return Ok(png) }; + let rgba = image::RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8()).ok_or("invalid PNG pixel data")?; + let resized = image::DynamicImage::ImageRgba8(rgba).resize(size, size, image::imageops::FilterType::Triangle).to_rgba8(); + let encoded = craft_image_codecs::Image::from_u8(resized.width(), resized.height(), craft_image_codecs::ChannelLayout::Rgba, resized.into_raw()) + .map_err(|e| e.to_string())?; + craft_image_codecs::encode(&encoded, craft_image_codecs::Format::Png, &Default::default()).map_err(|e| e.to_string()) } /// `mcp` (headless, in-process engine) or `mcp --connect PORT|HOST:PORT` (drive a running app @@ -597,8 +598,8 @@ fn export(s: &mut Session, out: &str, page: usize, scale: f64) -> Result<(), Str let img = r .render_page(&st.doc, &s.cache, page, scale, true, &designcraft_render::RenderOptions { printing_only: true, ..Default::default() }) .ok_or("no such page")?; - let lower = out.to_lowercase(); - let bytes = if lower.ends_with(".jpg") || lower.ends_with(".jpeg") { img.to_jpeg(90) } else { img.to_png() }; + let jpeg = craft_file_formats::detect(out, &[]) == Some(craft_file_formats::Format::Jpeg); + let bytes = if jpeg { img.to_jpeg(90) } else { img.to_png() }; designcraft_engine::atomic::write_atomic(std::path::Path::new(out), &bytes).map_err(|e| format!("{out}: {e}"))?; eprintln!("wrote {out} ({}×{}, {:.1} ms, {} glyphs)", img.width, img.height, t.elapsed().as_secs_f64() * 1000.0, r.stats.glyphs); Ok(()) @@ -645,7 +646,7 @@ fn script(args: &[String]) -> Result<(), String> { match a.as_str() { "--in" => { let p = val()?; - let cmd = if p.to_lowercase().ends_with(".idml") { "file.openIdml" } else { "file.open" }; + let cmd = if craft_file_formats::detect(&p, &[]) == Some(craft_file_formats::Format::Idml) { "file.openIdml" } else { "file.open" }; setup.push((cmd.into(), json!({"path": p}))); } "--sample" => setup.push(("file.newSample".into(), json!({}))), @@ -691,11 +692,15 @@ fn script(args: &[String]) -> Result<(), String> { let report = if keep_going { // Run every step; failures become {"error": …} results. let mut results: Vec = Vec::new(); - for st in &steps { + let mut failed = None; + for (i, st) in steps.iter().enumerate() { let r = designcraft_engine::script::resolve(&st.params, &results).and_then(|p| exec(b, &st.command, p)); - results.push(r.unwrap_or_else(|e| json!({"error": e, "command": st.command}))); + results.push(r.unwrap_or_else(|e| { + failed.get_or_insert_with(|| (i, st.command.clone(), e.clone())); + json!({"error": e, "command": st.command}) + })); } - designcraft_engine::script::Report { results, failed: None } + designcraft_engine::script::Report { results, failed } } else { designcraft_engine::script::run(&steps, |id, p| exec(b, id, p)) }; diff --git a/apps/designcraft-cli/src/perf.rs b/apps/designcraft-cli/src/perf.rs index 3a3afe931..d67fffa4b 100644 --- a/apps/designcraft-cli/src/perf.rs +++ b/apps/designcraft-cli/src/perf.rs @@ -97,6 +97,9 @@ fn row(rows: &mut Vec, name: impl Into, ms: f64, budget: Option Result, String> { + if doc.spreads.is_empty() { + return Err("cannot benchmark a document with no spreads".into()); + } let mut rows = vec![]; let cores = std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get); let chars: usize = doc.stories.values().map(|s| s.text.len()).sum(); diff --git a/apps/designcraft-cli/src/stress.rs b/apps/designcraft-cli/src/stress.rs index c9d5b3015..ce3e7ed7d 100644 --- a/apps/designcraft-cli/src/stress.rs +++ b/apps/designcraft-cli/src/stress.rs @@ -169,7 +169,7 @@ pub fn synthetic(spec: &Spec) -> Result { id, name: format!("art-{k}.png"), mime: "image/png".into(), - data: Arc::new(data), + data: designcraft_doc::AssetData::embedded(data), pixels: Some((aw, ah)), ..Default::default() }), diff --git a/apps/designcraft-cli/tests/it/args.rs b/apps/designcraft-cli/tests/it/args.rs new file mode 100644 index 000000000..a854f2aba --- /dev/null +++ b/apps/designcraft-cli/tests/it/args.rs @@ -0,0 +1,395 @@ +//! What the command line does with its arguments, pinned. +//! +//! Every assertion here was measured, not intended: these tests say what the parser does today, +//! including where that is odd (an unknown option taken as a filter, a `--opt=value` form that +//! only the automation roots have). They exist so that moving to a derived parser — the work +//! photocraft#7071 lists, which this parser needs too — changes the surface where that is the +//! point and nowhere else. +//! +//! They assert the exit code, which stream the output went to, and that an error names the +//! argument it is about. They do not assert wording, so a message can be improved without +//! editing a test, except in the few places where the wording *is* the behaviour and the test +//! says so. +//! +//! `tests/cli.rs` holds what the subcommands then do: exports, scripts, the MCP server. + +use std::process::{Command, Stdio}; + +/// `designcraft-cli` with stdin closed, so a subcommand that reads a script does not wait. +fn cli() -> Command { + let mut c = Command::new(env!("CARGO_BIN_EXE_designcraft-cli")); + c.stdin(Stdio::null()); + c +} + +/// What one invocation did. +struct Ran { + code: Option, + out: String, + err: String, +} + +fn run(args: &[&str]) -> Ran { + let out = cli().args(args).output().unwrap_or_else(|e| panic!("{args:?}: {e}")); + Ran { code: out.status.code(), out: String::from_utf8_lossy(&out.stdout).into_owned(), err: String::from_utf8_lossy(&out.stderr).into_owned() } +} + +impl Ran { + /// Succeeded, said nothing on stderr; gives back what it printed. + fn succeeded(self, what: &str) -> String { + assert_eq!(self.code, Some(0), "{what}: {:?}", self.err); + assert_eq!(self.err, "", "{what} wrote to stderr"); + self.out + } + + /// Failed with the usage exit code, said nothing on stdout; gives back the complaint. + fn failed(self, what: &str) -> String { + assert_eq!(self.code, Some(1), "{what}: expected a usage failure, got {:?} / {:?}", self.out, self.err); + assert_eq!(self.out, "", "{what} wrote to stdout"); + self.err + } +} + +/// A port with nothing listening on it, so `--connect` and `--port` fail the same way every time. +fn closed_port() -> String { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + drop(listener); + port.to_string() +} + +/// A directory of this test's own, removed when the test ends. +fn temp_dir(name: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("dc-args-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +/// Every subcommand, in the order the usage block lists them. +const SUBCOMMANDS: [&str; 10] = ["run", "commands", "describe", "script", "app", "mcp", "perf", "bench", "validate-pdf", "links"]; + +#[test] +fn with_no_arguments_it_prints_the_usage_block_and_fails() { + let err = run(&[]).failed("no arguments"); + assert!(err.starts_with("usage: designcraft-cli run"), "{err:?}"); + // The usage block names every subcommand, which is how ` --help` finds its lines. + for sub in SUBCOMMANDS { + assert!(err.contains(&format!("designcraft-cli {sub}")), "usage names {sub}: {err:?}"); + } + // An empty argument is a command nobody has, not a request for help. + run(&[""]).failed("an empty argument"); +} + +#[test] +fn help_goes_to_stdout_and_succeeds_in_all_three_spellings() { + let long = run(&["--help"]).succeeded("--help"); + assert!(long.starts_with("usage: designcraft-cli run"), "{long:?}"); + // The community links are under the usage block, for someone who has nowhere else to ask. + assert!(long.contains("Community:") && long.contains("discord"), "{long:?}"); + for spelling in ["-h", "help"] { + assert_eq!(run(&[spelling]).succeeded(spelling), long, "{spelling} prints the same help"); + } +} + +#[test] +fn an_unknown_command_fails_with_the_same_text_on_stderr() { + let usage = run(&["--help"]).succeeded("--help"); + for unknown in ["bogus", "-x", "--nope", "Run"] { + let err = run(&[unknown]).failed(unknown); + assert_eq!(err, usage, "{unknown}: the same usage, on stderr"); + // Asking for help about a command nobody has is still that command: ` --help` + // answered with the whole usage block on stdout and succeeded, which skipped the + // unknown-command failure entirely (noted in review). + for spelling in ["--help", "-h"] { + let err = run(&[unknown, spelling]).failed(&format!("{unknown} {spelling}")); + assert_eq!(err, usage, "{unknown} {spelling}: the same usage, on stderr"); + } + } + // A real subcommand still answers about itself, and only about itself. + for sub in SUBCOMMANDS.iter().filter(|s| **s != "script") { + let text = run(&[sub, "--help"]).succeeded(sub); + assert!(text.contains(&format!("designcraft-cli {sub}")), "{sub} --help describes {sub}: {text:?}"); + assert_ne!(text, usage, "{sub} --help is about {sub}, not the whole usage block"); + } +} + +#[test] +fn the_version_is_the_crate_version() { + let expected = format!("designcraft-cli {}\n", env!("CARGO_PKG_VERSION")); + for spelling in ["--version", "-V", "version"] { + assert_eq!(run(&[spelling]).succeeded(spelling), expected, "{spelling}"); + } +} + +/// ` --help` asks about that subcommand. It answers from the usage block, on stdout, and +/// nothing else happens: no export is written, no port is dialled, no measurement is taken, and a +/// file named earlier in the same line is not opened or complained about (photocraft#423). +#[test] +fn every_subcommand_answers_help_without_running_anything() { + let dir = temp_dir("help"); + let png = dir.join("ran.png"); + let dead = closed_port(); + let cases: [(&str, Vec<&str>); 10] = [ + ("run", vec!["run", "--sample", "--export", png.to_str().unwrap()]), + ("commands", vec!["commands"]), + ("describe", vec!["describe", "file.open"]), + ("script", vec!["script", "no-such-script.dcs"]), + ("app", vec!["app", "--port", &dead, "document.list"]), + ("mcp", vec!["mcp", "--connect", &dead]), + ("perf", vec!["perf", "--pages", "1", "--runs", "1"]), + ("bench", vec!["bench", "no-such-document.designcraft"]), + ("validate-pdf", vec!["validate-pdf", "no-such-file.pdf"]), + ("links", vec!["links"]), + ]; + for (sub, args) in cases { + for flag in ["--help", "-h"] { + let mut args = args.clone(); + args.push(flag); + let what = format!("{sub} {flag}"); + let out = run(&args).succeeded(&what); + assert!(out.contains(&format!("designcraft-cli {sub}")), "{what} names it: {out:?}"); + assert!(!out.contains("unknown option"), "{what}: {out:?}"); + // None of the work the rest of the line asks for was done. + assert!(!png.exists(), "{what} exported a file"); + assert!(!out.contains("cannot connect") && !out.contains("render threads"), "{what}: {out:?}"); + // Either operating system's wording, so this catches a file the help path opened on + // Windows as well as on unix. + assert!(!out.contains("No such file") && !out.contains("cannot find the file"), "{what}: {out:?}"); + } + } + // `script`'s own help is longer than one usage line: it documents the script syntax, which is + // otherwise only in `describe script.run` and the docs. + let out = run(&["script", "--help"]).succeeded("script --help"); + assert!(out.contains("Script text, one step per line"), "{out:?}"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// The subcommands that have options refuse an option they do not know, and name it. +#[test] +fn an_unknown_option_is_refused_by_name() { + let cases: [Vec<&str>; 5] = [ + vec!["run", "--nope"], + vec!["script", "-", "--nope"], + vec!["mcp", "--nope"], + vec!["perf", "--nope"], + vec!["bench", "f.designcraft", "--nope"], + ]; + for args in cases { + let err = run(&args).failed(&format!("{args:?}")); + assert!(err.contains("--nope"), "{args:?}: {err:?}"); + assert!(err.contains("unknown"), "{args:?}: {err:?}"); + } +} + +/// The subcommands whose argument is a word, not an option, take an unknown option as that word: +/// `commands --nope` filters the command list by `--nope` and finds nothing. Each of these would +/// be an error under the rule photocraft#7071 settles on; today they are not, and a change of +/// mind shows up here. +#[test] +fn the_subcommands_that_take_a_word_read_an_unknown_option_as_that_word() { + // A filter that matches no command: an empty list, successfully. + assert_eq!(run(&["commands", "--nope"]).succeeded("commands --nope").trim(), "[]"); + // A command id nobody has. + let err = run(&["describe", "--nope"]).failed("describe --nope"); + assert!(err.contains("no command `--nope`"), "{err:?}"); + // A path nobody has. `--nope` has to fail the way an ordinary missing path fails, because + // that is what shows it reached the file system as a path instead of being read as an + // option. Asserting only that the complaint names `--nope` does not show that: a parser that + // rejected unknown options would name it too (noted in review). + // + // So the two are compared with the name taken out. That holds the shape of the failure + // without asserting the operating system's wording for a missing file -- Windows says "The + // system cannot find the file specified" -- which an earlier round of review rightly took + // out of this test. `failed` already checks the exit code and that stdout stayed empty. + let as_option = run(&["validate-pdf", "--nope"]).failed("validate-pdf --nope"); + let as_path = run(&["validate-pdf", "nope.pdf"]).failed("validate-pdf nope.pdf"); + assert!(as_option.contains("--nope"), "{as_option:?}"); + assert_eq!( + as_option.replace("--nope", ""), + as_path.replace("nope.pdf", ""), + "`--nope` must fail as a missing path does, not as an option" + ); + // A method name for the running app, so the error is about reaching the app. + let dead = closed_port(); + let err = run(&["app", "--port", &dead, "--nope"]).failed("app --nope"); + assert!(err.contains("cannot connect"), "{err:?}"); + // And `links`, which takes nothing, ignores what it is given. + let out = run(&["links", "--nope"]).succeeded("links --nope"); + assert_eq!(out, run(&["links"]).succeeded("links")); +} + +/// There is no `--opt=value` form. The two automation roots are the exception: they have one, +/// because a folder whose name starts with `-` has no other way in there. +#[test] +fn only_the_automation_roots_have_an_opt_equals_value_form() { + let cases: [(&str, Vec<&str>); 3] = [ + ("--scale=2", vec!["run", "--scale=2", "--sample"]), + ("--pages=2", vec!["perf", "--pages=2"]), + ("--save=out.designcraft", vec!["script", "-", "--save=out.designcraft"]), + ]; + for (token, args) in cases { + let err = run(&args).failed(&format!("{args:?}")); + assert!(err.contains("unknown"), "{args:?}: {err:?}"); + // The whole token is named, `=value` and all, so it is clear what was not understood. + assert!(err.contains(token), "{args:?}: {err:?}"); + } + // The roots read the value after `=`: an empty one is refused as a folder, which is the + // parser having taken it rather than ignoring the form. + let err = run(&["mcp", "--automation-read-root="]).failed("empty root"); + assert!(err.contains("--automation-read-root") && err.contains("folder"), "{err:?}"); + // A root given more than once adds another folder, in either spelling, rather than the last + // one winning or the repeat being refused: the roots are repeatable (`...` in the usage), and + // `mcp_automation_roots_take_several_folders` covers what the extra folder grants. This PR + // first pinned the older behaviour, where a second root was an error; upstream has made them + // repeatable since, so what is pinned here is today's. + // + // Checked by what fails rather than by letting the server start: the stray token after the + // roots is the error, which shows both roots were taken. + for second in [vec!["--automation-read-root=b"], vec!["--automation-read-root", "b"]] { + let mut args = vec!["mcp", "--automation-read-root=a"]; + args.extend(second.iter().copied()); + args.push("stray"); + let err = run(&args).failed(&format!("{second:?}")); + assert!(err.contains("stray"), "the stray token is what fails: {err:?}"); + assert!(!err.contains("twice"), "a repeated root is not refused any more: {err:?}"); + } +} + +/// An option that needs a value and has none says which option, and nothing runs. +#[test] +fn a_missing_value_names_its_option() { + let dir = temp_dir("missing"); + let png = dir.join("out.png"); + let cases: [(&str, Vec<&str>); 9] = [ + ("--export", vec!["run", "--sample", "--export"]), + ("--in", vec!["run", "--in"]), + ("--all-pages", vec!["run", "--sample", "--all-pages"]), + // Before an export, too: the order of the options is nothing to judge until this one has + // a value, so it is not reported as an export it is not in front of. + ("--scale", vec!["run", "--sample", "--scale", "--export", png.to_str().unwrap()]), + ("--page", vec!["run", "--sample", "--page"]), + ("--save", vec!["script", "-", "--save"]), + ("--connect", vec!["script", "-", "--connect"]), + ("--port", vec!["app", "--port"]), + ("--pages", vec!["perf", "--pages"]), + ]; + for (option, args) in cases { + let err = run(&args).failed(&format!("{args:?}")); + assert!(err.contains(option), "{args:?} names {option}: {err:?}"); + assert!(err.contains("needs"), "{args:?}: {err:?}"); + } + assert!(!png.exists(), "nothing is exported"); + + // A missing value after an export is found before that export runs: the line writes nothing, + // as a malformed line always did. + let first = dir.join("first.png"); + let line = ["run", "--sample", "--export", first.to_str().unwrap(), "--scale", "--export", png.to_str().unwrap()]; + let err = run(&line).failed("a missing value after an export"); + assert!(err.contains("--scale needs a value"), "{err:?}"); + assert!(!first.exists(), "the export before it was not written"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// `--help` wins wherever it appears after the subcommand, even where an option would take it as +/// its value: `run --in -h` prints `run`'s usage and opens nothing. +#[test] +fn help_wins_even_where_an_option_would_take_it_as_a_value() { + for flag in ["-h", "--help"] { + let out = run(&["run", "--in", flag]).succeeded(&format!("run --in {flag}")); + assert!(out.contains("designcraft-cli run"), "{out:?}"); + } + // And each subcommand's usage lists every option it takes. + let perf = run(&["perf", "--help"]).succeeded("perf --help"); + for option in ["--pages", "--frames", "--chars", "--images", "--runs", "--strict"] { + assert!(perf.contains(option), "perf --help lists {option}: {perf:?}"); + } + let validate = run(&["validate-pdf", "--help"]).succeeded("validate-pdf --help"); + assert!(validate.contains("--standard"), "{validate:?}"); +} + +/// An option that takes no value does not swallow the token after it: that token is read as the +/// next argument, and refused if it is not one. +#[test] +fn a_flag_that_takes_no_value_leaves_the_next_token_alone() { + let err = run(&["run", "--sample", "extra", "--export", "out.png"]).failed("run --sample extra"); + assert!(err.contains("extra"), "{err:?}"); + let err = run(&["perf", "--strict", "3"]).failed("perf --strict 3"); + assert!(err.contains('3'), "{err:?}"); +} + +/// A value flag given twice keeps the last value. (The automation roots are the exception, above.) +#[test] +fn a_repeated_value_flag_keeps_the_last_value() { + let dir = temp_dir("repeat"); + let at = |args: &[&str], name: &str| -> Vec { + let path = dir.join(name); + let mut full: Vec<&str> = vec!["run", "--sample"]; + full.extend_from_slice(args); + let out = path.to_str().unwrap(); + full.extend(["--export", out]); + let ran = run(&full); + assert_eq!(ran.code, Some(0), "{full:?}: {:?}", ran.err); + std::fs::read(&path).unwrap() + }; + // `--scale`: the size of the image says which value was used. + let one = at(&[], "1.png"); + let three = at(&["--scale", "3"], "3.png"); + assert_eq!(at(&["--scale", "2", "--scale", "3"], "23.png"), three, "the last --scale won"); + assert_ne!(three, one, "and --scale 3 is not --scale 1"); + // `--page`: which page was exported says it. + let second = at(&["--page", "1"], "p1.png"); + assert_ne!(second, one, "page 1 of the sample is not page 0"); + assert_eq!(at(&["--page", "1", "--page", "0"], "p10.png"), one, "the last --page won"); + // `--connect` and `--port`: the address the error names says it. + let (a, b) = (closed_port(), closed_port()); + for args in [vec!["mcp", "--connect", &a, "--connect", &b], vec!["app", "--port", &a, "--port", &b, "document.list"]] { + let err = run(&args).failed(&format!("{args:?}")); + assert!(err.contains(&format!("127.0.0.1:{b}")), "{args:?} dialled the last port: {err:?}"); + } + let _ = std::fs::remove_dir_all(&dir); +} + +/// The flags that are meant to repeat each take effect, in order. +#[test] +fn the_repeatable_flags_all_take_effect() { + let dir = temp_dir("each"); + let (a, b) = (dir.join("a.png"), dir.join("b.png")); + let ran = run(&[ + "run", + "--sample", + "--cmd", + "document.list", + "--cmd", + "document.list", + "--export", + a.to_str().unwrap(), + "--export", + b.to_str().unwrap(), + ]); + assert_eq!(ran.code, Some(0), "{:?}", ran.err); + // One result line per `--cmd`, in the order given. + let results: Vec<&str> = ran.out.lines().collect(); + assert_eq!(results.len(), 2, "{results:?}"); + assert_eq!(results[0], results[1]); + // And one file per `--export`. + assert_eq!(std::fs::read(&a).unwrap(), std::fs::read(&b).unwrap()); + assert_eq!(ran.err.lines().filter(|l| l.starts_with("wrote ")).count(), 2, "{:?}", ran.err); + let _ = std::fs::remove_dir_all(&dir); +} + +/// A subcommand given nothing to do does nothing, quietly or with its own usage line. `run` with +/// no arguments is the one that says nothing at all: there is no document to act on and nothing +/// was asked of it. +#[test] +fn a_subcommand_with_nothing_to_do_says_so_or_says_nothing() { + assert_eq!(run(&["run"]).succeeded("run"), ""); + // `script` with no file reads stdin, which is closed here: an empty script, run. + let out = run(&["script"]).succeeded("script"); + assert!(out.contains("\"completed\": 0"), "{out:?}"); + // The two that need an argument print the usage line for themselves. + for sub in ["describe", "bench", "validate-pdf"] { + let err = run(&[sub]).failed(sub); + assert!(err.contains(&format!("designcraft-cli {sub}")), "{sub}: {err:?}"); + } +} diff --git a/apps/designcraft-cli/tests/it/cli.rs b/apps/designcraft-cli/tests/it/cli.rs index b45c1d449..f12732578 100644 --- a/apps/designcraft-cli/tests/it/cli.rs +++ b/apps/designcraft-cli/tests/it/cli.rs @@ -7,6 +7,31 @@ fn cli() -> Command { Command::new(env!("CARGO_BIN_EXE_designcraft-cli")) } +#[test] +fn bench_without_spreads_reports_an_error_instead_of_panicking() { + struct Fixture(std::path::PathBuf); + impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } + } + let path = std::env::temp_dir().join(format!("dc-cli-empty-bench-{}.designcraft", std::process::id())); + assert!(std::fs::symlink_metadata(&path).is_err_and(|error| error.kind() == std::io::ErrorKind::NotFound)); + let fixture = Fixture(path); + let mut document = designcraft_doc::Document::new(&Default::default()); + document.spreads.clear(); + document.check().unwrap(); + let bytes = designcraft_format::save(&document).unwrap(); + assert!(designcraft_format::load(&bytes).unwrap().spreads.is_empty()); + std::fs::write(&fixture.0, bytes).unwrap(); + + let out = cli().args(["bench", fixture.0.to_str().unwrap(), "--runs", "1"]).output().unwrap(); + + let err = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(1), "{err}"); + assert!(err.contains("cannot benchmark a document with no spreads"), "{err}"); +} + #[test] fn script_from_stdin_with_references_saves_and_exports() { let dir = std::env::temp_dir().join(format!("dc-cli-{}", std::process::id())); @@ -151,11 +176,49 @@ fn script_export_of_the_document_in_the_running_app_saves_a_copy() { #[test] fn failing_script_exits_nonzero_with_the_step() { let mut child = cli().args(["script", "-"]).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap(); - child.stdin.take().unwrap().write_all(b"document.inspect\nno.such.command\n").unwrap(); + child.stdin.take().unwrap().write_all(b"document.inspect\nno.such.command\ndocument.inspect\n").unwrap(); let out = child.wait_with_output().unwrap(); assert!(!out.status.success()); let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); assert_eq!(v["failedIndex"], 1); + assert_eq!(v["completed"], 1); +} + +#[test] +fn keep_going_reports_the_first_failure_and_does_not_write_outputs() { + struct Outputs([std::path::PathBuf; 2]); + impl Drop for Outputs { + fn drop(&mut self) { + for path in &self.0 { + let _ = std::fs::remove_file(path); + } + } + } + let root = std::env::temp_dir(); + let name = format!("dc-cli-keep-going-{}", std::process::id()); + let paths = [root.join(format!("{name}.designcraft")), root.join(format!("{name}.png"))]; + assert!(paths.iter().all(|path| !path.exists())); + let outputs = Outputs(paths); + + let mut child = cli() + .args(["script", "-", "--keep-going", "--save", outputs.0[0].to_str().unwrap(), "--export", outputs.0[1].to_str().unwrap()]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + child.stdin.take().unwrap().write_all(b"no.such.command\ndocument.inspect\nno.other.command\n").unwrap(); + let out = child.wait_with_output().unwrap(); + + assert!(!out.status.success()); + let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + assert_eq!(v["completed"], 3); + assert_eq!(v["failedIndex"], 0); + assert_eq!(v["failedCommand"], "no.such.command"); + assert!(v["results"][0]["error"].is_string()); + assert!(v["results"][1].is_object() && v["results"][1]["error"].is_null()); + assert!(v["results"][2]["error"].is_string()); + assert!(outputs.0.iter().all(|path| !path.exists()), "--save and --export require a successful script"); } #[test] diff --git a/apps/designcraft-cli/tests/it/main.rs b/apps/designcraft-cli/tests/it/main.rs index 26710c101..e8761f4e1 100644 --- a/apps/designcraft-cli/tests/it/main.rs +++ b/apps/designcraft-cli/tests/it/main.rs @@ -1 +1,2 @@ +mod args; mod cli; diff --git a/apps/designcraft-web/src/browser_policy.rs b/apps/designcraft-web/src/browser_policy.rs index 524df6cc5..ca82c71ca 100644 --- a/apps/designcraft-web/src/browser_policy.rs +++ b/apps/designcraft-web/src/browser_policy.rs @@ -1,4 +1,4 @@ -//! Pure browser startup and download metadata policies. +//! Browser startup, download metadata, and atomic import policies. /// Query flags are case-sensitive decoded keys. Values are ignored: `?sample=false` /// still enables sample startup, and repeated keys have the same presence semantics. @@ -65,3 +65,40 @@ mod tests { } } } + +/// Read a drop in order, publishing nothing unless every file can be read. +pub(crate) async fn read_batch(files: impl IntoIterator, mut read: F) -> Result, E> +where + F: FnMut(T) -> Fut, + Fut: std::future::Future>, +{ + let mut result = Vec::new(); + for file in files { + result.push(read(file).await?); + } + Ok(result) +} + +#[cfg(test)] +mod batch_tests { + #[test] + fn failed_read_aborts_the_whole_batch() { + use std::{ + future::Future, + task::{Context, Poll, Waker}, + }; + let mut seen = Vec::new(); + let result = { + let future = super::read_batch([1, 2, 3], |n| { + seen.push(n); + std::future::ready(if n == 2 { Err("read failed") } else { Ok(n) }) + }); + let mut future = std::pin::pin!(future); + future.as_mut().poll(&mut Context::from_waker(Waker::noop())) + }; + assert_eq!(result, Poll::Ready(Err("read failed"))); + assert_eq!(seen, [1, 2]); + let mut success = std::pin::pin!(super::read_batch([3, 1], |n| std::future::ready(Ok::<_, ()>(n)))); + assert_eq!(success.as_mut().poll(&mut Context::from_waker(Waker::noop())), Poll::Ready(Ok(vec![3, 1]))); + } +} diff --git a/apps/designcraft-web/src/web.rs b/apps/designcraft-web/src/web.rs index 19bc3a122..4d4c936c1 100644 --- a/apps/designcraft-web/src/web.rs +++ b/apps/designcraft-web/src/web.rs @@ -76,7 +76,7 @@ pub fn start() { if has_query_flag(&query(), "sample") { let _ = app.run("file.newSample", serde_json::json!({})); } - Ok(Box::new(WebShell { app, inbox, graphics: creator_graphics })) + Ok(Box::new(WebShell { app, inbox, read_errors: Default::default(), graphics: creator_graphics })) }), ) .await; @@ -344,12 +344,18 @@ fn query() -> String { struct WebShell { app: DesignApp, inbox: Inbox, + /// Why a dropped file couldn't be read, for the status bar (the read finishes outside a frame). + read_errors: std::sync::Arc>>, graphics: GraphicsState, } impl eframe::App for WebShell { fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { let graphics = self.graphics.clone(); + let errors = std::mem::take(&mut *self.read_errors.lock().unwrap_or_else(|e| e.into_inner())); + if let Some(error) = errors.into_iter().last() { + self.app.status(error); + } let _ = graphics.run_if_healthy(|| self.app.logic(ctx)); } @@ -357,18 +363,34 @@ impl eframe::App for WebShell { let _ = self.graphics.run_if_healthy(|| { // Dropped files belong to this host iteration's raw input, not to stale logic input: // read them here once, before the batch is saved as pending for a hidden pass. - for f in std::mem::take(&mut raw.dropped_files) { + // Files dropped together arrive together, in their order, so several images load the + // place cursor as one batch. + let dropped = std::mem::take(&mut raw.dropped_files); + if !dropped.is_empty() { let request = self.app.import_request("drop"); let inbox = self.inbox.clone(); + let read_errors = self.read_errors.clone(); let ctx = ctx.clone(); wasm_bindgen_futures::spawn_local(async move { - let name = f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into()); - match f.bytes_async().await { - Ok(bytes) => { - inbox.lock().unwrap_or_else(|e| e.into_inner()).push(ImportedFile { request, name, bytes }); + let files = crate::browser_policy::read_batch(dropped, |f| { + let request = request.clone(); + async move { + let name = f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into()); + let bytes = f.bytes_async().await.map_err(|e| format!("couldn't read dropped file {name}: {e}"))?; + Ok::<_, String>(ImportedFile { request, name, bytes }) + } + }) + .await; + match files { + Ok(files) => { + inbox.lock().unwrap_or_else(|e| e.into_inner()).extend(files); + ctx.request_repaint(); + } + Err(error) => { + log::error!("{error}"); + read_errors.lock().unwrap_or_else(|e| e.into_inner()).push(error); ctx.request_repaint(); } - Err(e) => log::error!("couldn't read dropped file {name}: {e}"), } }); } @@ -402,11 +424,18 @@ fn services(inbox: Inbox, ctx: egui::Context) -> Services { rfd::AsyncFileDialog::new().add_filter("DesignCraft, IDML or InDesign", DOC_EXTS) }; wasm_bindgen_futures::spawn_local(async move { - let Some(file) = dialog.pick_file().await else { - return; + // Place takes several files, loaded into the place cursor in the order chosen. + let handles = if request.purpose == "place" { + dialog.pick_files().await.unwrap_or_default() + } else { + dialog.pick_file().await.into_iter().collect() }; - let bytes = file.read().await; - inbox.lock().unwrap_or_else(|e| e.into_inner()).push(ImportedFile { request, name: file.file_name(), bytes }); + let mut files = Vec::with_capacity(handles.len()); + for file in handles { + let bytes = file.read().await; + files.push(ImportedFile { request: request.clone(), name: file.file_name(), bytes }); + } + inbox.lock().unwrap_or_else(|e| e.into_inner()).extend(files); ctx.request_repaint(); }); })), diff --git a/apps/designcraft/src/native_menu.rs b/apps/designcraft/src/native_menu.rs index ba92e0031..65c3945c1 100644 --- a/apps/designcraft/src/native_menu.rs +++ b/apps/designcraft/src/native_menu.rs @@ -41,9 +41,10 @@ struct RecentMenu { separator: Option, } -/// Accelerator for "Cmd+Shift+]" (modifier-less shortcuts stay in the app so typing works). +/// Accelerator for "Cmd+Shift+]". Shortcuts the bar would never see, such as modifier-less ones +/// or Option on a character key, stay in the app (`menus::native_key_equivalent`). fn accel(sc: &str) -> Option { - if !(sc.contains("Cmd") || sc.contains("Ctrl") || sc.contains("Alt")) { + if !menus::native_key_equivalent(sc) { return None; } Accelerator::from_str(&sc.replace("Cmd", "CMD").replace("Alt", "ALT").replace("Shift", "SHIFT").replace("Ctrl", "CTRL")).ok() diff --git a/apps/designcraft/src/pointer.rs b/apps/designcraft/src/pointer.rs index 14bf9599a..3725cfbaa 100644 --- a/apps/designcraft/src/pointer.rs +++ b/apps/designcraft/src/pointer.rs @@ -11,6 +11,33 @@ pub fn screen_position() -> Option { // AppKit's origin is the bottom left of the first screen, the one with the menu bar. let height = NSScreen::screens(mtm).firstObject()?.frame().size.height; let at = NSEvent::mouseLocation(); - let (x, y) = (at.x, height - at.y); + from_appkit(at.x, at.y, height) +} + +/// A point in AppKit's global coordinates (origin at the bottom left of the main display, the one +/// with the menu bar, y up) in winit's (origin at its top left, y down), given that display's +/// height. Only the main display's height: winit flips window positions the same way +/// (`flip_window_screen_coordinates`, against `CGMainDisplayID`), whatever display the window is +/// on, so a display left of or above the main one gives negative coordinates in both. +fn from_appkit(x: f64, y: f64, main_height: f64) -> Option { + let y = main_height - y; (x.is_finite() && y.is_finite()).then(|| egui::pos2(x as f32, y as f32)) } + +#[cfg(test)] +mod tests { + use super::from_appkit; + + #[test] + fn appkit_points_flip_against_the_main_display_only() { + // The main display is 1000 points high: its top left is (0, 0), its bottom left (0, 1000). + assert_eq!(from_appkit(0.0, 1000.0, 1000.0), Some(egui::pos2(0.0, 0.0))); + assert_eq!(from_appkit(200.0, 100.0, 1000.0), Some(egui::pos2(200.0, 900.0))); + // A second display above the main one, and one to its left: negative, as winit reports a + // window there. + assert_eq!(from_appkit(300.0, 1200.0, 1000.0), Some(egui::pos2(300.0, -200.0))); + assert_eq!(from_appkit(-800.0, 500.0, 1000.0), Some(egui::pos2(-800.0, 500.0))); + assert_eq!(from_appkit(f64::NAN, 0.0, 1000.0), None); + assert_eq!(from_appkit(0.0, 0.0, f64::INFINITY), None); + } +} diff --git a/contributors/people.toml b/contributors/people.toml index 617091888..edae2a69a 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -91,3 +91,6 @@ display_name = "Jonathan Cardoso" [people.adikshan11] real_name = "Adithya Shankaran" sync_github_name = true + +[people.gmrmad1] +real_name = "Madhav Agarwal" diff --git a/crates/compose/src/breaker.rs b/crates/compose/src/breaker.rs index 46fb3bdff..bd9351119 100644 --- a/crates/compose/src/breaker.rs +++ b/crates/compose/src/breaker.rs @@ -53,8 +53,12 @@ pub struct Spacing { pub optical_size: Option, /// Korean text breaks between syllables instead of at spaces. pub korean_char_breaks: bool, - /// 0: minimum adjustment; 1: push in; 2: push out first; 3: push out only. + /// JIS X 4051 禁則処理 type: 0: minimum adjustment (調整量優先); 1: push in first (追い込み + /// 優先, the default); 2: push out first (追い出し優先); 3: push out only (追い出しのみ). pub kinsoku_priority: u8, + /// The paragraph has CJK text, so the 禁則 priority biases its breaks. A Latin paragraph's + /// breaks are left alone by the (default) push-in-first policy. + pub kinsoku_cjk: bool, } impl Default for Spacing { @@ -77,19 +81,25 @@ impl Default for Spacing { optical: false, optical_size: None, korean_char_breaks: false, - kinsoku_priority: 0, + kinsoku_priority: 1, + kinsoku_cjk: false, } } } impl Spacing { + /// Push out only: the 追い込み (compression that would pull the next character in) is + /// unavailable, so every line end is pushed out (and no line is compressed at its end). + pub(crate) fn push_out_only(&self) -> bool { + self.kinsoku_cjk && self.kinsoku_priority == 3 + } /// Per-tier (word, letter, glyph) stretch and shrink of glyph `g` as a box. fn box_elastic(&self, g: &Glyph) -> ([f64; 3], [f64; 3]) { if g.locked_advance { return ([0.0; 3], [0.0; 3]); } let moji_stretch = if self.justify { g.moji.stretch } else { 0.0 }; - let moji_shrink = if self.kinsoku_priority == 3 { 0.0 } else { g.moji.shrink }; + let moji_shrink = if self.push_out_only() { 0.0 } else { g.moji.shrink }; if !self.justify { return ([0.0; 3], [moji_shrink, 0.0, 0.0]); } @@ -276,13 +286,18 @@ fn kinsoku_allows(a: char, b: char) -> bool { !NO_START.contains(b) && !NO_END.contains(a) } +/// Hangul: Jamo, compatibility Jamo, the syllables, and Jamo Extended-A/B. Shared with the +/// paragraph's CJK gate so the two classifications cannot drift apart. +pub(crate) fn is_hangul(c: char) -> bool { + matches!(c as u32, 0x1100..=0x11FF | 0x3130..=0x318F | 0xA960..=0xA97F | 0xAC00..=0xD7FF) +} + /// Hangul, and Han ideographs (hanja) in Korean text: Korean is set word by word (KLREQ), so /// these letters break like Western ones, at spaces. fn korean_letter(g: &Glyph) -> bool { - let c = g.ch as u32; - let hangul = matches!(c, 0x1100..=0x11FF | 0x3130..=0x318F | 0xA960..=0xA97F | 0xAC00..=0xD7FF); - let han = matches!(c, 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0x20000..=0x3FFFF); - hangul || (han && g.lang.is_some_and(|t| designcraft_doc::language_subtag(t) == "ko")) + let c = g.ch; + let han = matches!(c as u32, 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0x20000..=0x3FFFF); + is_hangul(c) || (han && g.lang.is_some_and(|t| designcraft_doc::language_subtag(t) == "ko")) } /// Is the place between glyphs `a` and `b` one where CJK text may break (next to an ideograph, kana @@ -413,7 +428,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut p = Item::penalty(0.0, -INF, false); p.hang = prev_hang; if let Some(prev) = i.checked_sub(1).and_then(|j| glyphs.get(j)) { - p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, !sp.push_out_only()); } it.push(p); ig.push(i + 1); @@ -427,7 +442,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut gl = Item::glue(w, st, sh); gl.hang = prev_hang; if let Some(prev) = i.checked_sub(1).and_then(|j| glyphs.get(j)) { - gl.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, sp.kinsoku_priority != 3); + gl.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, !sp.push_out_only()); } gl.over = sp.space_overflow(g); last_space = it.len(); @@ -440,7 +455,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut p = Item::penalty(0.0, 0.0, false); p.hang = prev_hang; if let Some(prev) = i.checked_sub(1).and_then(|j| glyphs.get(j)) { - p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(prev, sp.justify, !sp.push_out_only()); } last_space = it.len(); it.push(p); @@ -462,7 +477,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let (st, sh) = sp.box_elastic(g); let mut bx = Item::boxed(g.adv, st, sh); bx.hang = hang_left(g, sp) - g.moji.start; - bx.start_elastic = crate::mojikumi::start_elastic(g, sp.justify, sp.kinsoku_priority != 3); + bx.start_elastic = crate::mojikumi::start_elastic(g, sp.justify, !sp.push_out_only()); it.push(bx); ig.push(i); let next_is_space = glyphs.get(i + 1).is_none_or(|n| n.is_space()); @@ -470,12 +485,12 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S if matches!(g.ch, '-' | '\u{2010}') { let mut p = Item::penalty(0.0, 50.0, true); p.hang = right_hang(g, sp); - p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); push_inword(&mut it, &mut ig, p, i + 1, sp); } else if matches!(g.ch, '\u{2013}' | '\u{2014}' | '/') || g.break_after.unwrap_or_else(|| cjk_break(g, &glyphs[i + 1], sp)) { let mut p = Item::penalty(0.0, 0.0, false); p.hang = right_hang(g, sp); - p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); push_inword(&mut it, &mut ig, p, i + 1, sp); } else if hyph_after[i] { let hw = hyphen_width(g, &mut hy_cache); @@ -497,7 +512,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S let mut p = Item::penalty(0.0, -INF, false); p.hang = if n > 0 { right_hang(&glyphs[n - 1], sp) } else { 0.0 }; if let Some(g) = glyphs.last() { - p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3); + p.edge_elastic = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only()); } it.push(p); ig.push(n); @@ -638,11 +653,19 @@ fn hyphen_allowed(it: Item, n: Node, sp: &Spacing, widths: &[f64], items: &[Item } /// Fitness class and cumulative penalty for a candidate line break. -fn break_demerits(n: Node, r: f64, pp: f64, flagged: bool, priority: u8) -> (usize, f64) { - let preference = match priority { - 1 if r > 0.0 => 4.0, - 2 if r < 0.0 => 4.0, - _ => 1.0, +fn break_demerits(n: Node, r: f64, pp: f64, flagged: bool, priority: u8, cjk: bool) -> (usize, f64) { + // 禁則 priority (JIS X 4051): push-in-first rates breaks whose line would have to be + // stretched (r > 0: the next character is pushed out) worse than ones it can reach by + // compressing (追い込み); push-out-first the other way. Adjustment-amount mode and a + // paragraph without CJK text rate every candidate by its adjustment alone. + let preference = if !cjk { + 1.0 + } else { + match priority { + 1 if r > 0.0 => 4.0, + 2 if r < 0.0 => 4.0, + _ => 1.0, + } }; let bad = preference * 100.0 * r.abs().powi(3); let lp = 10.0 + bad; @@ -770,7 +793,7 @@ fn kp_pass(input: PassInput<'_>) -> Option> { if flagged && !hyphen_allowed(it, n, sp, &sw, items, target) { continue; } - let (fit, total) = break_demerits(n, r, pp, flagged, sp.kinsoku_priority); + let (fit, total) = break_demerits(n, r, pp, flagged, sp.kinsoku_priority, sp.kinsoku_cjk); let hs = if flagged { (n.hyphens as usize + 1).min(states - 1) } else { 0 }; let slot = &mut best[fit * states + hs]; if slot.is_none_or(|(bd, _)| total < bd) { @@ -925,7 +948,7 @@ pub fn greedy( while start < n { let w = width(line).max(1.0); let mut x = -hang_left(&glyphs[start], sp) + glyphs[start].moji.start; - let mut shrink = crate::mojikumi::start_elastic(&glyphs[start], sp.justify, sp.kinsoku_priority != 3)[1]; + let mut shrink = crate::mojikumi::start_elastic(&glyphs[start], sp.justify, !sp.push_out_only())[1]; let mut last_natural: Option<(usize, bool)> = None; let mut last_ok: Option<(usize, bool)> = None; // (break position = end glyph, hyphen) let mut last_space: Option<(usize, f64)> = None; // (break position, line width there) @@ -952,9 +975,10 @@ pub fn greedy( } let hang_r = right_hang(g, sp); let adv = if g.ch == '\t' { tab(line, x, i) } else { g.adv }; - let end_shrink = crate::mojikumi::end_elastic(g, sp.justify, sp.kinsoku_priority != 3)[1]; + let end_shrink = crate::mojikumi::end_elastic(g, sp.justify, !sp.push_out_only())[1]; let available = shrink + sp.box_elastic(g).1.iter().sum::() + end_shrink; - if sp.kinsoku_priority == 2 + if sp.kinsoku_cjk + && sp.kinsoku_priority == 2 && x + adv - hang_r > w + FIT_EPS && let Some((end, hyphen)) = last_natural { diff --git a/crates/compose/src/hyphen.rs b/crates/compose/src/hyphen.rs index 3d2be41f9..a709995f4 100644 --- a/crates/compose/src/hyphen.rs +++ b/crates/compose/src/hyphen.rs @@ -224,6 +224,135 @@ pub fn word_breaks_in(word: &str, lang: Lang) -> (Vec, Source) { (out, src) } +/// Whether a whitespace-delimited token is one no language's hyphenation should break. +/// +/// Korean attaches particles straight onto Latin words ("News와", "PDF로"), and notices are full +/// of addresses. With the paragraph language left at the default English -- which is what an +/// ordinary Korean document has -- a word run inside such a token was hyphenated as English, +/// giving `New-` / `s와` and `library.exam-` / `ple.kr` (#1688). +/// +/// A token is kept whole when it carries Hangul, kana or Han, or when it reads as an address: a +/// URL with a scheme, an e-mail address, a dotted domain, a `/`-rooted path or a Windows drive +/// path. Breaking at a hyphen the author typed is still allowed; this only stops hyphens being +/// invented inside the token. +pub fn keep_whole(token: &str) -> bool { + let cjk = |c: char| { + matches!(c as u32, + 0xAC00..=0xD7A3 // Hangul syllables + | 0x1100..=0x11FF // Hangul Jamo + | 0x3130..=0x318F // Hangul compatibility Jamo + | 0xA960..=0xA97F // Hangul Jamo Extended-A + | 0xD7B0..=0xD7FF // Hangul Jamo Extended-B + | 0x3040..=0x30FF // kana + | 0xFF66..=0xFF9F // halfwidth katakana + | 0x4E00..=0x9FFF // CJK unified ideographs + | 0x3400..=0x4DBF + | 0xF900..=0xFAFF + | 0x20000..=0x3FFFF) // supplementary ideographs + }; + if token.chars().any(cjk) { + return true; + } + // Prose punctuation around a token is not part of the address inside it: `(https://...)` was + // not recognised because the bracket came first, and `library.example.kr,` because the comma + // ended the last label. Only this classification ignores it; the text keeps + // every character it had. + let wrapper = |c: char| { + matches!(c, '(' | ')' | '[' | ']' | '{' | '}' | '"' | '\'' | ',' | ';' | ':' | '!' | '?' | '\u{201C}' | '\u{201D}' | '\u{2018}' | '\u{2019}') + }; + // Full stops count with the wrapper characters at the end, so `(library.example.kr).` loses + // both its closing bracket and the sentence's full stop. + let token = token.trim_start_matches(wrapper).trim_end_matches(|c: char| wrapper(c) || c == '.'); + let lower = token.to_ascii_lowercase(); + if lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("ftp://") || lower.starts_with("www.") { + return true; + } + // An e-mail address: one `@` with something either side, no space (tokens have none). + if let Some((user, host)) = token.split_once('@') { + return !user.is_empty() && host.contains('.'); + } + // A path, rooted or with a drive letter. + if token.starts_with('/') || token.starts_with('\\') { + return true; + } + let mut cs = token.chars(); + if let (Some(a), Some(b), Some(c)) = (cs.next(), cs.next(), cs.next()) + && a.is_ascii_alphabetic() + && b == ':' + && (c == '/' || c == '\\') + { + return true; + } + // A domain: dot-separated labels up to the first `/`, whose last label is a short run of + // letters -- "example.kr", "library.example.kr". Ordinary prose ("end. Next") never reaches + // here, because a token holds no space. + // Judged as written, not lower-cased: a domain's last label is lower-case letters, so `Mr.Smith` + // is not one and stays a word to hyphenate. + let host = token.split('/').next().unwrap_or(""); + let labels: Vec<&str> = host.trim_end_matches('.').split('.').collect(); + labels.len() >= 2 + && labels.iter().all(|l| !l.is_empty() && l.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')) + && labels.last().is_some_and(|tld| tld.len() <= 6 && tld.chars().all(|c| c.is_ascii_lowercase())) +} + +/// Whether a paragraph is set in Korean, so automatic hyphenation does not apply to it. +/// +/// Korean is set word by word and is not hyphenated (KLREQ). A paragraph with at least as many +/// Hangul syllables as Latin letters is treated as Korean however its language is set, since an +/// ordinary Korean document leaves the paragraph language at the default English (#1688). +pub fn mostly_hangul(text: &str) -> bool { + mostly_hangul_chars(text.chars()) +} + +/// [`mostly_hangul`] over characters, for judging the text that is actually set: hidden text has +/// no glyphs, and counting it let hidden Korean turn hyphenation off for the visible English +/// around it, and hidden Latin keep it on for visible Korean. +pub fn mostly_hangul_chars(chars: impl Iterator) -> bool { + let (mut hangul, mut latin) = (0usize, 0usize); + for c in chars { + match c as u32 { + _ if crate::breaker::is_hangul(c) => hangul += 1, + _ if c.is_ascii_alphabetic() => latin += 1, + _ => {} + } + } + hangul > 0 && hangul >= latin +} + +/// [`mostly_hangul`] over a paragraph's set glyphs, given as `(source byte, character, +/// generated)` so it can be tested without a font. +/// +/// One vote per set character, not per glyph. Shaping makes a cluster several glyphs over one +/// character and each repeats that character, so a syllable shaped into three glyphs voted three +/// times -- and since the test is `hangul >= latin`, that can turn hyphenation off for a +/// paragraph that is mostly Latin. Consecutive glyphs on one source byte are one character. +/// +/// Generated glyphs do not vote: a list bullet or number, or a footnote label, is not the +/// paragraph's own text, and `prepend_label` gives it the paragraph's *first* byte -- so counting +/// it would also swallow the real first character in the deduplication. +/// +/// **Approximation.** A ligature is one glyph over several characters, so those characters vote +/// once between them and Latin is under-counted. That can change the answer: 16 Hangul with +/// `office hyphenation` is 17 Latin letters, and a font that shapes `fi` counts at most 16, so +/// the paragraph classifies as Korean and loses its hyphens. Fixing it needs +/// the visible source characters each cluster stands for; `byte..byte + len` is not that, because +/// the glyph after a hidden run spans it -- measured, every byte of the paragraph came out +/// covered -- which would revive the hidden-text bug this function was changed to avoid. +pub fn korean_paragraph(glyphs: impl Iterator) -> bool { + let mut chars: Vec = Vec::new(); + let mut last: Option = None; + for (byte, ch, generated) in glyphs { + if generated { + continue; + } + if last != Some(byte) { + chars.push(ch); + } + last = Some(byte); + } + mostly_hangul_chars(chars.into_iter()) +} + /// Allowed hyphenation points in `word` as char indices (a hyphen goes before that char), with /// the paragraph limits applied to each letter run. pub fn hyphen_points(word: &str, lim: &Limits) -> Vec { @@ -359,6 +488,65 @@ fn heuristic_points(c: &[char]) -> Vec { #[cfg(test)] mod tests { + /// A token no language's hyphenation should break: Korean particles on Latin words, and the + /// addresses a notice is full of (#1688). + #[test] + fn a_token_with_hangul_or_an_address_is_kept_whole() { + for t in [ + "News\u{C640}", // News와 + "PDF\u{B85C}", // PDF로 + "\u{C77C}\u{BCF8}\u{C5B4}", // 일본어 + "https://example.kr/page", + "www.example.kr", + "library.example.kr", + "mail@example.kr", + "/usr/share/fonts", + "C:\\Users\\Hong", + "example.kr", + ] { + assert!(keep_whole(t), "{t} should be kept whole"); + } + // Ordinary words, including ones with a hyphen or a full stop, are still hyphenated. + for t in ["hyphenation", "well-known", "sentence.", "Mr.", "re-examine", "3.5"] { + assert!(!keep_whole(t), "{t} should not be protected"); + } + + // An address wrapped in prose punctuation is the same address. + for t in [ + "library.example.kr,", + "(https://example.kr/page)", + "\u{201C}www.example.kr\u{201D}", + "mail@example.kr;", + "example.kr.", + // A bracket and then the sentence's full stop. + "(library.example.kr).", + "[https://example.kr]", + ] { + assert!(keep_whole(t), "{t} should be kept whole"); + } + + // Halfwidth katakana and a supplementary ideograph are CJK too. + for t in ["News\u{FF76}", "\u{20B9F}\u{C640}", "x\u{FF9F}"] { + assert!(keep_whole(t), "{t:?} should be kept whole"); + } + + // A domain's last label is lower-case letters as written: a name run into the next word + // after a full stop is not one, and its words still hyphenate. + for t in ["Mr.Smith", "Section.Introduction", "End.Next"] { + assert!(!keep_whole(t), "{t} is not an address"); + } + } + + /// A Korean paragraph gets no automatic hyphens whatever its language is set to (#1688). + #[test] + fn a_paragraph_that_is_mostly_hangul_is_not_hyphenated() { + assert!(mostly_hangul("\u{C774}\u{BC88} \u{D1B5}\u{C9C0}\u{BB38}\u{C740} News\u{C640} \u{AD00}\u{B828}")); + assert!(!mostly_hangul("An English paragraph about hyphenation and composition")); + // A Latin paragraph with a Korean word in it is still hyphenated as Latin. + assert!(!mostly_hangul("The Korean word \u{D55C}\u{AD6D} appears once in this English sentence")); + assert!(!mostly_hangul("")); + } + use super::*; fn h(w: &str) -> String { @@ -720,3 +908,75 @@ mod tests { assert!(t.elapsed().as_secs() < 5, "{:?}", t.elapsed()); } } + +#[cfg(test)] +mod korean_paragraph_tests { + use super::korean_paragraph; + + /// `(byte, ch, generated)` per glyph, which is all the classification reads -- so these cases + /// need no font and state the counting rules directly. + fn k(glyphs: &[(usize, char, bool)]) -> bool { + korean_paragraph(glyphs.iter().copied()) + } + + /// One character per source byte, however many glyphs shaping made of it. A syllable shaped + /// into three glyphs voted three times, which could classify a mostly-Latin paragraph as + /// Korean. + #[test] + fn glyphs_of_one_cluster_vote_once() { + // Two Hangul characters, the first shaped into three glyphs, against three Latin letters. + let clustered = [ + (0, '\u{AC00}', false), + (0, '\u{AC00}', false), + (0, '\u{AC00}', false), + (3, '\u{B098}', false), + (6, 'a', false), + (7, 'b', false), + (8, 'c', false), + ]; + assert!(!k(&clustered), "two Hangul against three Latin is not Korean"); + // Counting each glyph instead gives four Hangul against three, which flips it -- so the + // deduplication is what decides this case. + let per_glyph: Vec = clustered.iter().map(|(_, c, _)| *c).collect(); + assert!(super::mostly_hangul_chars(per_glyph.into_iter()), "per-glyph counting would call it Korean"); + } + + /// A generated label is not the paragraph's own text, and `prepend_label` gives it the + /// paragraph's *first* byte -- so counting it also swallowed the real first character in the + /// deduplication. + #[test] + fn a_generated_label_neither_votes_nor_hides_the_first_character() { + // A footnote label at byte 0, then two Hangul characters (the first also at byte 0) and + // two Latin letters. Two against two is Korean, since the test is `hangul >= latin` -- + // and losing one Hangul character to the label makes it one against two, which is not. + let labelled = [(0, '1', true), (0, '\u{AC00}', false), (3, '\u{B098}', false), (6, 'a', false), (7, 'b', false)]; + assert!(k(&labelled), "two Hangul against two Latin is Korean"); + // Without the exclusion the label takes byte 0, the Hangul character there is deduplicated + // away, and one Hangul against two Latin is no longer Korean. + let counted: Vec = { + let mut out = Vec::new(); + let mut last = None; + for (b, c, _) in labelled { + if last != Some(b) { + out.push(c); + } + last = Some(b); + } + out + }; + assert!(!super::mostly_hangul_chars(counted.into_iter()), "counting the label loses the first character"); + } + + /// The remaining approximation, pinned so it is visible rather than surprising: a ligature is + /// one glyph over several characters, so Latin is under-counted and this paragraph classifies + /// as Korean although it has more Latin letters. + #[test] + fn a_ligature_under_counts_latin_which_is_a_known_approximation() { + // Two Hangul characters against three Latin letters shaped as one `ffi` glyph. + let ligated = [(0, '\u{AC00}', false), (3, '\u{B098}', false), (6, '\u{FB03}', false)]; + assert!(k(&ligated), "the ligature counts as one character, so this reads as Korean"); + // Spelled out, the same text is not Korean -- which is the error the approximation makes. + let spelled = [(0, '\u{AC00}', false), (3, '\u{B098}', false), (6, 'f', false), (7, 'f', false), (8, 'i', false)]; + assert!(!k(&spelled), "the same characters unligated are not Korean"); + } +} diff --git a/crates/compose/src/kenten.rs b/crates/compose/src/kenten.rs index 1053c26b8..69994aa7a 100644 --- a/crates/compose/src/kenten.rs +++ b/crates/compose/src/kenten.rs @@ -123,6 +123,7 @@ fn shape_mark(face: FaceRef, text: &str, m: &KentenMark, b: &PlacedGlyph) -> Vec let ch = text.get(sg.cluster..).and_then(|t| t.chars().next()).unwrap_or(' '); let adv = f64::from(sg.x_advance) * sx; out.push(PlacedGlyph { + list_label: false, rendered_char: ch, face, gid: sg.gid, diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 2942fb962..17ad2f76b 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -165,6 +165,10 @@ pub struct PlacedGlyph { /// How far after its pen position the glyph is drawn (CJK aki, ruby parent spacing): its /// advance starts at `x - dx`. pub dx: f64, + /// Part of the bullet or number the paragraph's list adds (see [`shape::Glyph::list_label`]). + /// It carries the first character's offset and no length, so anything selecting glyphs by + /// byte range has to leave it out or it reaches text outside the range. + pub list_label: bool, } impl PlacedGlyph { @@ -1000,6 +1004,13 @@ pub struct FrameSpec { } impl FrameSpec { + /// The baseline grid when the text area can be counted in its steps: a step so small, or a + /// first line so far off, that the count is more than an f64 holds is no grid (as for the lines). + fn countable_grid(&self) -> Option<(f64, f64)> { + self.grid + .filter(|&(g_start, inc)| inc > 0.0 && inc.is_finite() && [self.area.y0, self.area.y1].iter().all(|y| ((y - g_start) / inc).is_finite())) + } + pub fn columns(&self) -> Vec { let a = self.area; // Primary frames inherit the document limit (216), beyond the interactive frame editor's 40. @@ -1633,6 +1644,8 @@ struct NestedLineStyleInput<'a, 'b, 'font> { range: Range, props: &'a designcraft_doc::ParaProps, para_chars: &'a designcraft_doc::CharProps, + /// The paragraph has CJK text, so its 禁則 priority applies. + cjk: bool, drop_end: usize, base_leading: f64, base_cap: f64, @@ -1658,6 +1671,7 @@ fn apply_nested_line_styles(input: NestedLineStyleInput<'_, '_, '_>) { range: prange, props: pp, para_chars: base_chars, + cjk, drop_end, base_leading, base_cap, @@ -1680,11 +1694,14 @@ fn apply_nested_line_styles(input: NestedLineStyleInput<'_, '_, '_>) { let cols_here = &cols[cur.fi]; let col = span_rect(cols_here, cols_here[cur.col.min(cols_here.len() - 1)], pp.span_columns); let col = SplitCfg::of(pp).map_or(col, |c| c.sub_rect(col, 0)); - let spacing = spacing_for(pp, base_chars.size, story); + let spacing = spacing_for(pp, base_chars.size, story, cjk); let mut lines: Vec<(std::ops::Range, String)> = Vec::new(); for _ in 0..3 { // The same drop cap, spacing, hyphenation and breaker as the layout below. let mut gl = sp.glyphs.clone(); + // Classified before the drop cap is split off: `split_drop_cap` takes those glyphs out + // of the vector, and the drop cap's character still votes (#1688). + let korean = hyphen::korean_paragraph(gl.iter().map(|g| (g.byte, g.ch, g.list_label || g.generated_text.is_some()))); let dc = split_drop_cap(db, &mut gl, drop_end, pp.drop_cap_lines, base_leading, base_cap, drop_grid, drop_align, drop_tabs); let width = |j: usize| { let ind = match &dc { @@ -1696,7 +1713,7 @@ fn apply_nested_line_styles(input: NestedLineStyleInput<'_, '_, '_>) { }; apply_desired_spacing(&mut gl, pp); mojikumi::apply(&mut gl, &doc.styles, &pp.mojikumi); - let mut hy = hyphenation_points(&story.text, &gl, pp, hyph_exceptions, &foreign_ranges(doc, story, prange.clone(), base_chars)); + let mut hy = hyphenation_points(&story.text, &gl, pp, hyph_exceptions, &foreign_ranges(doc, story, prange.clone(), base_chars), korean); suppress_hyphens(&mut hy, &gl, suppressed_hyphens); let breaks = if pp.composer.single_line() || gl.iter().any(|g| g.ch == '\t') || gl.len() > 4000 { let tab = |j: usize, x: f64, i: usize| { @@ -2788,6 +2805,10 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { }, &mut table, ); + // The paragraph's script decides whether its 禁則 priority applies. Classify the shaped + // glyphs before split_drop_cap removes the drop cap and prepend_label adds the list + // label: a CJK drop cap is part of the paragraph, a generated label is not. + let cjk = paragraph_is_cjk(&sp.glyphs); // An empty paragraph has no glyphs of its own, so its only addressable character is its // hidden paragraph mark (the `\n` just past `prange.end`). A leading/size override applied // there (e.g. selecting the pilcrow to adjust line height) still needs to set the line's @@ -2808,7 +2829,7 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { let face = db.face(&base_chars.font_family, &base_chars.font_style); face.cap_height * base_chars.size / face.upem * base_chars.v_scale }; - let drop_grid = cur_frame.and_then(|f| f.grid).filter(|_| pp.grid_align == GridAlign::AllLines).map(|g| g.1); + let drop_grid = cur_frame.and_then(FrameSpec::countable_grid).filter(|_| pp.grid_align == GridAlign::AllLines).map(|g| g.1); // Align Left Edge (left-to-right paragraphs). let drop_align = pp.drop_cap_align_left && pp.direction == designcraft_doc::TextDirection::LeftToRight; // Tab stops for the drop cap, with the start-side indent (the right one, right to left). @@ -2820,6 +2841,7 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { range: prange.clone(), props: pp, para_chars: base_chars, + cjk, drop_end, base_leading, base_cap, @@ -2842,6 +2864,8 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { g.and_then(|g| table.styles.get(g.style as usize)).map_or((base_chars.fill.clone(), base_chars.fill_tint), |s| (s.fill.clone(), s.fill_tint)) }; let (above, below) = (text_color(sp.glyphs.iter().find(|g| g.len > 0)), text_color(sp.glyphs.iter().rev().find(|g| g.len > 0))); + // Classified before the drop cap is split off (see the same call in the measuring loop above). + let korean = hyphen::korean_paragraph(sp.glyphs.iter().map(|g| (g.byte, g.ch, g.list_label || g.generated_text.is_some()))); let drop_cap = split_drop_cap( db, &mut sp.glyphs, @@ -2931,10 +2955,11 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { ); apply_desired_spacing(&mut glyphs, pp); mojikumi::apply(&mut glyphs, &doc.styles, &pp.mojikumi); - let mut hyph_after = hyphenation_points(&story.text, &glyphs, pp, hyph_exceptions, &foreign_ranges(doc, story, prange.clone(), base_chars)); + let mut hyph_after = + hyphenation_points(&story.text, &glyphs, pp, hyph_exceptions, &foreign_ranges(doc, story, prange.clone(), base_chars), korean); suppress_hyphens(&mut hyph_after, &glyphs, suppressed_hyphens); let base_size = base_chars.size; - let spacing = spacing_for(pp, base_size, story); + let spacing = spacing_for(pp, base_size, story, cjk); // An empty paragraph takes its height from its paragraph mark's character format. let mark_chars = if glyphs.is_empty() { doc.styles.resolve_char(base_chars, story.char_format_of(prange.start)) } else { base_chars.clone() }; let mark_leading = match mark_chars.leading { @@ -3935,8 +3960,9 @@ fn optical_for(pp: &ParaProps, story: &Story) -> (bool, Option) { } } -/// Breaker parameters from the paragraph's settings. -fn spacing_for(pp: &ParaProps, base_size: f64, story: &Story) -> Spacing { +/// Breaker parameters from the paragraph's settings; `cjk` says the paragraph has CJK text (so +/// its 禁則 priority applies). +fn spacing_for(pp: &ParaProps, base_size: f64, story: &Story, cjk: bool) -> Spacing { let (optical, optical_size) = optical_for(pp, story); Spacing { justify: pp.align.is_justified(), @@ -3958,15 +3984,51 @@ fn spacing_for(pp: &ParaProps, base_size: f64, story: &Story) -> Spacing { optical, optical_size, korean_char_breaks: pp.korean_char_breaks, + kinsoku_cjk: cjk, + // JIS X 4051 禁則処理: the empty value is InDesign's default and the UI's "Push In First", + // and 調整量優先 (adjustment amount) is the breaker's minimum-adjustment mode 0. kinsoku_priority: match pp.kinsoku_type.as_str() { - "KinsokuPushInFirst" => 1, + "" | "KinsokuPushInFirst" => 1, "KinsokuPushOutFirst" => 2, "KinsokuPushOutOnly" => 3, + "KinsokuPrioritizeAdjustmentAmount" => 0, _ => 0, }, } } +/// Whether the paragraph's own shaped glyphs contain CJK text, the gate for its 禁則 priority. +/// Called before `split_drop_cap` and `prepend_label`, so a drop cap counts and generated list +/// and story labels do not. A text variable or page number keeps its marker in [`Glyph::ch`], so +/// its replacement text is what the paragraph shows; inspect that too. +fn paragraph_is_cjk(glyphs: &[Glyph]) -> bool { + glyphs.iter().any(|g| is_cjk_char(g.ch) || g.generated_text.as_deref().is_some_and(|text| text.chars().any(is_cjk_char))) +} + +/// Is `c` CJK text (the scripts the 禁則 line-start/line-end rules are about): ideographs, kana, +/// CJK punctuation and full/half-width forms, bopomofo, Hangul. +fn is_cjk_char(c: char) -> bool { + // Hangul comes from the breaker's classification (Jamo including Extended-A/B, syllables), + // so the two sets agree. Bopomofo is 0x3100..=0x312F; 0x3130..=0x318F is Hangul. + breaker::is_hangul(c) + || matches!( + c as u32, + 0x2E80..=0x2FFF + | 0x3000..=0x30FF + | 0x3100..=0x312F + | 0x3190..=0x33FF + | 0x3400..=0x4DBF + | 0x4E00..=0x9FFF + | 0xA000..=0xA4CF + | 0xF900..=0xFAFF + | 0xFE30..=0xFE4F + | 0xFF00..=0xFFEF + // Extension B and later (U+20000..): the same range as the breaker's Han check, + // so the kinsoku settings apply to a paragraph of supplementary-plane text alone. + | 0x20000..=0x3FFFF + ) +} + /// Desired word spacing, letter spacing and glyph scaling apply to every line (any alignment). fn apply_desired_spacing(glyphs: &mut [Glyph], pp: &ParaProps) { for i in 0..glyphs.len() { @@ -5219,7 +5281,7 @@ fn layout_line( let mut offset = 0.0; // Kashidas: in justified Arabic, the joins of words take the extra length first. let kashidas = if justify_this { allocate_kashidas(&line, &mut add, pp, seg, &mut extra) } else { Vec::new() }; - if (justify_this || squeeze_last) && (extra >= 0.0 || sp.kinsoku_priority != 3) { + if (justify_this || squeeze_last) && (extra >= 0.0 || !sp.push_out_only()) { extra = mojikumi::distribute(line.get_mut(seg..).unwrap_or_default(), extra, add.get_mut(seg..).unwrap_or_default()); } if (justify_this || squeeze_last) && !spaces.is_empty() { @@ -5422,6 +5484,12 @@ fn tab_leader(tab: &Glyph, leader: &str, x: f64, w: f64, origin: f64, out: &mut while at + pw <= end + 0.01 && n < 2000 { for &(gid, adv) in &glyphs { out.push(PlacedGlyph { + // The leader fills a tab, so it belongs to whatever that tab belongs to. Saying + // `false` for the tab of a list label made the leader claim to be the + // paragraph's own text while carrying the paragraph-start byte, so a PDF link + // starting at the first text character swept the leader positions into its + // rectangle (noted in review). + list_label: tab.list_label, rendered_char: '\t', face: tab.face, gid, @@ -5586,6 +5654,7 @@ fn place(g: &Glyph, x: f64) -> PlacedGlyph { len: g.len, generated_text: g.generated_text.clone(), visible, + list_label: g.list_label, upright: g.upright, tcy: g.tcy, rtl: false, @@ -5913,17 +5982,31 @@ fn hyphenation_points( pp: &ParaProps, exceptions: &HashMap>, foreign: &[(Range, Option)], + korean: bool, ) -> Vec { let mut out = vec![false; glyphs.len()]; if !pp.hyphenate { return out; } + // Korean is set word by word and is not hyphenated (KLREQ). An ordinary Korean document + // leaves the paragraph language at the default English, so the paragraph's own text decides + // rather than its language setting (#1688). + // + // Decided by the caller, from the paragraph's glyphs *before* its drop cap is split off: + // `split_drop_cap` takes those glyphs out of the vector, so classifying here lost the drop + // cap's character -- 15 Hangul against 15 Latin became 14 against 15, and the paragraph + // hyphenated its English. See `hyphen::korean_paragraph`. + if korean { + return out; + } let lim = hyphen::Limits { min_word: pp.hyph_min_word as usize, after_first: pp.hyph_after_first as usize, before_last: pp.hyph_before_last as usize, capitalized: pp.hyph_capitalized, }; + // The token the last word run sat in, and whether it is kept whole (see below). + let mut token: Option<(usize, usize, bool)> = None; let mut i = 0; while i < glyphs.len() { if !glyphs[i].is_letter() || glyphs[i].len == 0 { @@ -5944,6 +6027,24 @@ fn hyphenation_points( let (a, b) = (glyphs[i].byte, glyphs[j - 1].byte + glyphs[j - 1].len); if !discretionary && b > a && b <= text.len() && text.is_char_boundary(a) && text.is_char_boundary(b) { let word = &text[a..b]; + // The whitespace-delimited token the word sits in: a Latin run with a Korean particle + // on it ("News와") or inside an address is not a word to hyphenate (#1688). Found once + // per token and reused for the word runs inside it: unspaced CJK text is one token + // however many Latin words it holds, and scanning it again for each was quadratic. + let keep = match token { + Some((start, end, keep)) if start <= a && b <= end => keep, + _ => { + let start = text[..a].rfind(char::is_whitespace).map_or(0, |i| i + text[i..].chars().next().map_or(1, char::len_utf8)); + let end = text[b..].find(char::is_whitespace).map_or(text.len(), |i| b + i); + let keep = hyphen::keep_whole(&text[start..end]); + token = Some((start, end, keep)); + keep + } + }; + if keep { + i = j.max(i + 1); + continue; + } // Do not hyphenate the paragraph's last word unless allowed. let is_last_word = !pp.hyph_last_word && glyphs[j..].iter().all(|g| !g.is_letter()); let lang = foreign.iter().find(|(r, _)| r.contains(&a)).map_or(Some(hyphen::Lang::English), |(_, l)| *l); @@ -6253,7 +6354,7 @@ fn vertical_justify( } }; let center = f.opts.vertical_justification == VerticalJustification::Center; - let step = f.grid.map(|(_, inc)| inc).filter(|inc| *inc > 0.0 && inc.is_finite()).filter(|_| match f.opts.vertical_justification { + let step = f.countable_grid().map(|(_, inc)| inc).filter(|_| match f.opts.vertical_justification { VerticalJustification::Justify => idx.iter().zip(&rows).any(|(&i, &k)| k == n && on_grid(i)), _ => idx.iter().any(|&i| on_grid(i)), }); diff --git a/crates/compose/src/ruby.rs b/crates/compose/src/ruby.rs index 47c48a728..47b7c21c1 100644 --- a/crates/compose/src/ruby.rs +++ b/crates/compose/src/ruby.rs @@ -648,6 +648,7 @@ pub(crate) fn annotate( let style = ruby_style(styles, &st, &spec, shaped.size, missing); for (r, off) in shaped.glyphs.iter().zip(offsets) { extra.push(PlacedGlyph { + list_label: false, rendered_char: r.ch, face: shaped.face, gid: r.gid, diff --git a/crates/compose/src/table.rs b/crates/compose/src/table.rs index 0190aad05..341a116b6 100644 --- a/crates/compose/src/table.rs +++ b/crates/compose/src/table.rs @@ -34,6 +34,10 @@ pub struct PlacedCell { pub repeated: bool, /// Graphic cell content (drawn at `clip`'s top left, clipped to it). pub graphic: Option, + /// The cell's diagonal lines (Cell Options › Diagonal Lines), corner to corner of `rect`. + pub diagonals: Vec, + /// Whether the diagonals are drawn over the cell's contents (otherwise under them). + pub diagonals_in_front: bool, } /// One cell edge (or the table border) to stroke. @@ -91,6 +95,10 @@ impl TableFrag { c.rect = c.rect + v; c.clip = c.clip + v; c.origin += v; + for s in &mut c.diagonals { + s.a += v; + s.b += v; + } } for r in &mut self.rows { r.1 += dy; @@ -474,6 +482,14 @@ fn emit( overset, repeated, graphic: cell.graphic.clone(), + diagonals: cell + .diagonal + .segments(rect) + .into_iter() + .filter(|_| cell.diagonal.stroke.is_visible()) + .map(|(a, b)| StrokeSeg { a, b, stroke: cell.diagonal.stroke.clone() }) + .collect(), + diagonals_in_front: cell.diagonal.in_front, }); } } diff --git a/crates/compose/src/table/border_tests.rs b/crates/compose/src/table/border_tests.rs index 5a28d7274..336b0803d 100644 --- a/crates/compose/src/table/border_tests.rs +++ b/crates/compose/src/table/border_tests.rs @@ -31,6 +31,42 @@ fn explicit_cell_edges_override_table_border_without_changing_native_defaults() assert_eq!(right.stroke.weight, 2.0, "unoverridden right edge keeps the table border"); } +#[test] +fn diagonal_lines_cross_the_placed_cell_corner_to_corner() { + let mut table = Table::new(77, 2, 2, 0, 0, 100.0); + let stroke = CellStroke { weight: 3.0, ..Default::default() }; + let cell = table.cell_mut(0, 0).unwrap(); + cell.col_span = 2; + cell.diagonal = designcraft_doc::CellDiagonal { lines: designcraft_doc::DiagonalLines::Crossed, stroke: stroke.clone(), in_front: false }; + table.cell_mut(1, 0).unwrap().diagonal.lines = designcraft_doc::DiagonalLines::TopLeft; + table.cell_mut(1, 0).unwrap().diagonal.stroke.color = designcraft_color::swatch::NONE.into(); + let composed = compose_table(table); + let fragment = &composed.frames[0].tables[0]; + let merged = fragment.cells.iter().find(|c| (c.row, c.col) == (0, 0)).unwrap(); + let r = merged.rect; + let ends: Vec<_> = merged.diagonals.iter().map(|s| (s.a, s.b)).collect(); + assert_eq!(ends, [(Point::new(r.x0, r.y0), Point::new(r.x1, r.y1)), (Point::new(r.x1, r.y0), Point::new(r.x0, r.y1))], "across the merged cell"); + assert!(merged.diagonals.iter().all(|s| s.stroke == stroke) && !merged.diagonals_in_front); + assert!(fragment.cells.iter().filter(|c| c.row == 1).all(|c| c.diagonals.is_empty()), "none, or an invisible stroke, draws nothing"); + assert!(fragment.strokes.iter().all(|s| s.a.x == s.b.x || s.a.y == s.b.y), "the edges stay edges"); +} + +#[test] +fn diagonal_lines_move_with_their_cell_in_a_bottom_justified_frame() { + let mut table = Table::new(77, 1, 1, 0, 0, 100.0); + table.cell_mut(0, 0).unwrap().diagonal.lines = designcraft_doc::DiagonalLines::TopLeft; + let mut doc = Document::new(&NewDocument::default()); + let layer = doc.default_layer(); + let (fid, sid) = doc.add_text_frame(SpreadRef::Doc(0), Rect::new(0.0, 0.0, 200.0, 300.0), layer, "", ParaFormat::default()).unwrap(); + doc.item_mut(fid).unwrap().text_frame_mut().unwrap().options.vertical_justification = VerticalJustification::Bottom; + doc.story_mut(sid).unwrap().insert_table(0, table); + let composed = crate::compose_story(&doc, sid, &ComposeOptions::default()); + let cell = &composed.frames[0].tables[0].cells[0]; + assert!(cell.rect.y0 > 100.0, "the table sits at the bottom: {:?}", cell.rect); + let s = &cell.diagonals[0]; + assert_eq!((s.a, s.b), (Point::new(cell.rect.x0, cell.rect.y0), Point::new(cell.rect.x1, cell.rect.y1))); +} + #[test] fn merged_cell_keeps_explicit_border_suppression() { let mut table = Table::new(77, 2, 2, 0, 0, 100.0); diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index e28b1265a..f8cb4f3ce 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -1054,7 +1054,7 @@ fn an_alpha_channel_contour_stops_at_the_clipping_path() { height: 200, outer: rect(75.0, 150.0), holes: PathData::default(), - source: std::sync::Weak::new(), + source: designcraft_doc::SourceKey(0), serial: designcraft_doc::AlphaOutline::next_serial(), })); let cs = compose_story(&d, sid, &ComposeOptions::default()); @@ -1499,6 +1499,137 @@ fn hyphen_limit_is_a_hard_constraint() { } } +/// Each paragraph's own text decides, not the story's: a Korean document's English paragraphs +/// still hyphenate, and a mostly-English story's Korean paragraphs still do not (noted in review +/// of #1688). +#[test] +fn hyphenation_follows_each_paragraph_not_the_story() { + let rect = Rect::new(0.0, 0.0, 90.0, 100_000.0); + let para = ParaAttrs { align: Some(Align::LeftJustified), hyphenate: Some(true), ..Default::default() }; + // 이번 통지문은 아래와 같습니다 / an English paragraph that wants hyphenation + let korean = "\u{C774}\u{BC88} \u{D1B5}\u{C9C0}\u{BB38}\u{C740} \u{C544}\u{B798}\u{C640} \u{AC19}\u{C2B5}\u{B2C8}\u{B2E4}"; + let english = "This notice about hyphenation and composition continues for a while in English"; + let hyphens = |text: &str| { + let (d, sid, _) = doc_with(text, rect, para.clone()); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + let lines = all_lines(&cs); + // Which paragraph each hyphenated line belongs to: the Korean one is first. + lines.iter().filter(|l| l.hyphenated).count() + }; + // The English paragraph in a Korean document still hyphenates: the story is mostly Hangul, + // but that paragraph is not. + let mixed = format!("{korean}\n{english}"); + assert!(hyphens(&mixed) > 0, "the English paragraph of a Korean document still hyphenates"); + // And the Korean paragraph alone invents none. + assert_eq!(hyphens(korean), 0, "a Korean paragraph invents no hyphens"); +} + +/// The paragraph's *glyphs* decide, not its source text: hidden conditional text is not set, so +/// it does not get a vote. Reading the source let hidden Korean turn hyphenation off for the +/// visible English around it (noted in review of #1688). +#[test] +fn hidden_text_does_not_decide_whether_a_paragraph_hyphenates() { + let rect = Rect::new(0.0, 0.0, 90.0, 100_000.0); + let para = ParaAttrs { align: Some(Align::LeftJustified), hyphenate: Some(true), ..Default::default() }; + // 이번 통지문은 아래와 같습니다 — repeated, so the Hangul outweighs the English after it + // (`mostly_hangul` wants at least as many Hangul characters as ASCII letters). + let korean = "\u{C774}\u{BC88} \u{D1B5}\u{C9C0}\u{BB38}\u{C740} \u{C544}\u{B798}\u{C640} \u{AC19}\u{C2B5}\u{B2C8}\u{B2E4} ".repeat(6); + let english = "This notice about hyphenation and composition continues for a while in English"; + let text = format!("{korean}{english}"); + + let hyphens = |hide: bool| { + let (mut d, sid, _) = doc_with(&text, rect, para.clone()); + d.conditions.push(designcraft_doc::Condition { name: "Draft".into(), color: [255, 0, 0], visible: !hide }); + let story = d.story_mut(sid).expect("the story is there"); + let chars = designcraft_doc::CharAttrs { conditions: Some(vec!["Draft".into()]), ..Default::default() }; + story.chars = vec![ + designcraft_doc::CharRun { len: korean.len(), format: designcraft_doc::CharFormat { over: chars, ..Default::default() } }, + designcraft_doc::CharRun { len: english.len(), format: designcraft_doc::CharFormat::default() }, + ]; + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs).iter().filter(|l| l.hyphenated).count() + }; + + // Shown, the Hangul outweighs the English and the paragraph is not hyphenated. + assert_eq!(hyphens(false), 0, "a visibly Korean paragraph invents no hyphens"); + // Hidden, the only text set is English, and it hyphenates like any English paragraph. + assert!(hyphens(true) > 0, "hidden Korean does not turn hyphenation off for the visible English"); +} + +/// `split_drop_cap` takes the drop cap's glyphs out of the vector, so classifying the paragraph +/// after it ran lost that character's vote: with the Hangul and Latin counts level, dropping one +/// Hangul character made the paragraph read as Latin and its English words took automatic hyphens +/// (noted in review). The classification is made before the split. +#[test] +fn a_drop_cap_still_counts_when_a_paragraph_is_judged_korean() { + let rect = Rect::new(0.0, 0.0, 90.0, 100_000.0); + // English long enough that it really does hyphenate in a 90 pt column -- the run + // `hidden_text_does_not_decide_whether_a_paragraph_hyphenates` uses for the same reason. A + // short run leaves no hyphens whichever way the paragraph is classified, so the test would + // pass without testing anything (measured: it did). + let english = "This notice about hyphenation and composition continues for a while in English"; + let letters = english.chars().filter(char::is_ascii_alphabetic).count(); + // One Hangul syllable per Latin letter, so the counts are level -- which `hangul >= latin` + // calls Korean, and one character either way decides it. + let korean: String = "\u{AC00}\u{B098}\u{B2E4}\u{B77C}\u{B9C8}".chars().cycle().take(letters).collect(); + assert_eq!(korean.chars().count(), letters, "the two counts are level at {letters}"); + let text = format!("{korean} {english}"); + + let hyphens = |para: ParaAttrs| { + let (d, sid, _) = doc_with(&text, rect, para); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs).iter().filter(|l| l.hyphenated).count() + }; + let base = ParaAttrs { align: Some(Align::LeftJustified), hyphenate: Some(true), ..Default::default() }; + + // Level counts read as Korean, so nothing hyphenates. + assert_eq!(hyphens(base.clone()), 0, "level counts read as Korean"); + // The paragraph opens on a Hangul character, so that character is the drop cap. It is still + // set, just larger, so the answer must not change. + let with_cap = ParaAttrs { drop_cap_lines: Some(2), drop_cap_chars: Some(1), ..base.clone() }; + assert_eq!(hyphens(with_cap), 0, "a drop cap does not turn the paragraph Latin"); + + // And the fixture really is poised on the boundary: one Latin letter more and the English + // hyphenates, so the assertions above are not passing for want of any hyphen at all. + let tipped = format!("{korean} {english}x"); + let (d, sid, _) = doc_with(&tipped, rect, base); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + assert!(all_lines(&cs).iter().any(|l| l.hyphenated), "one more Latin letter tips it and hyphens appear"); +} + +/// A Korean notice leaves its paragraphs at the default English language, so English +/// hyphenation broke the Latin words Korean particles attach to and the addresses such a document +/// is full of: `New-` / `s와`, `library.exam-` / `ple.kr` (#1688). The lines may now be looser -- +/// that is the cost of not hyphenating, and what #1691 is about -- but no hyphen is invented +/// inside those tokens. +#[test] +fn korean_text_and_addresses_are_not_hyphenated() { + // Narrow enough that every line has to break somewhere. + let rect = Rect::new(0.0, 0.0, 90.0, 100_000.0); + let para = ParaAttrs { align: Some(Align::LeftJustified), hyphenate: Some(true), ..Default::default() }; + let hyphens_in = |text: &str| { + let (d, sid, _) = doc_with(text, rect, para.clone()); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + all_lines(&cs).iter().filter(|l| l.hyphenated).count() + }; + + // 이번 통지문은 News와 관련된 library.example.kr 주소를 포함합니다 + let korean = "\u{C774}\u{BC88} \u{D1B5}\u{C9C0}\u{BB38}\u{C740} News\u{C640} \u{AD00}\u{B828}\u{B41C} library.example.kr \u{C8FC}\u{C18C}\u{B97C} \u{D3EC}\u{D568}\u{D569}\u{B2C8}\u{B2E4}"; + assert_eq!(hyphens_in(korean), 0, "a Korean paragraph invents no hyphens"); + + // An English paragraph holding the same address: the prose still hyphenates, the address does + // not, so the protection is about the token and not about turning hyphenation off. + let english = "This notice about hyphenation and composition mentions library.example.kr somewhere inside a long sentence"; + assert!(hyphens_in(english) > 0, "English prose is still hyphenated"); + let (d, sid, _) = doc_with(english, rect, para.clone()); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + for l in all_lines(&cs).iter().filter(|l| l.hyphenated) { + let upto = &english[..l.range.end.min(english.len())]; + let token = upto.rsplit(char::is_whitespace).next().unwrap_or(""); + assert!(!"library.example.kr".starts_with(token) || token.is_empty(), "a hyphen was invented inside the address: {token:?}"); + } +} + #[test] fn hyphenation_zone_limits_ragged_hyphens() { let text = CORPUS.join(" "); @@ -3000,6 +3131,19 @@ fn vertical_justification_with_an_extreme_grid_increment_stays_finite() { assert!((b - natural).abs() < 1e-6, "Top, grid away from the line: baseline {b}, natural {natural}"); } } + + // A grid whose first line is too far off to count the text area in its 0.7 pt steps: Bottom + // takes the room as it does without a grid. + let mut area = Rect::ZERO; + let (lines, _) = grid_doc("One", ParaAttrs { grid_align: Some(GridAlign::AllLines), ..Default::default() }, |d, sid| { + let spec = frame_specs(d, sid).remove(0); + area = spec.area; + let opts = &mut d.item_mut(spec.id).unwrap().text_frame_mut().unwrap().options; + opts.baseline_grid = Some(designcraft_doc::FrameBaselineGrid::new(-1.7e308, 0.7)); + opts.vertical_justification = Bottom; + }); + let b = lines[0].baseline; + assert!((b - area.y1).abs() < 1e-6, "Bottom with a far-off grid: baseline {b}, text area {area:?}"); } /// In a shaped frame that narrows and widens again, Align to Baseline Grid takes the grid step @@ -5195,6 +5339,22 @@ fn drop_cap_on_a_baseline_grid_reaches_down_by_grid_steps() { assert!((lines[2].baseline - cap_of(dc) - (lines[0].baseline - cap_of(body))).abs() < 0.01); } +/// A grid the lines can't count in steps (its first line too far off for a 0.7 pt step) is no grid +/// for the drop cap either: it reaches down to line 3 at the 12 pt leading the lines are set at. +#[test] +fn drop_cap_skips_a_grid_the_lines_skip() { + let (d, sid, _) = drop_doc(LOREM, ParaAttrs { grid_align: Some(designcraft_doc::GridAlign::AllLines), ..drop_cap(3, 1) }); + let mut specs = frame_specs(&d, sid); + specs[0].grid = Some((-1.7e308, 0.7)); + let cs = compose(&d, d.story(sid).unwrap(), &specs, &ComposeOptions::default()); + let lines = all_lines(&cs); + assert!((lines[2].baseline - lines[0].baseline - 24.0).abs() < 1e-6, "{} {}", lines[0].baseline, lines[2].baseline); + let dc = &lines[0].glyphs[0]; + assert!((lines[0].baseline + dc.y - lines[2].baseline).abs() < 1e-6, "drop cap baseline {} below line 1", dc.y); + let b = lines[0].drop_cap.unwrap(); + assert!((b.baseline - lines[2].baseline).abs() < 1e-6, "{}", b.baseline); +} + /// A line with a larger auto leading doesn't move the drop cap: it reaches down `lines − 1` /// leadings of the text after it, not to where line N is set. #[test] @@ -6976,6 +7136,224 @@ fn mojikumi_kinsoku_push_in_and_push_out_select_different_legal_breaks() { } } +/// A paragraph that can compress: the Mojikumi table gives its ideographs shrinkable space, so +/// a line ending before a 禁則 boundary can be pulled in (追い込み) or pushed out (追い出し). +fn kinsoku_push_pull_doc(kind: Option<&str>) -> (Document, StoryId) { + kinsoku_push_pull_doc_text("甲乙丙丁戊己庚辛", kind) +} + +/// Same paragraph with a different body: used for the supplementary-plane ideographs, whose +/// CJK classification must match the breaker's Han set. +fn kinsoku_push_pull_doc_text(text: &str, kind: Option<&str>) -> (Document, StoryId) { + use designcraft_doc::cjk::{MojikumiAki, MojikumiTable}; + let (mut d, sid, _) = doc_with( + text, + Rect::new(0.0, 0.0, 45.0, 300.0), + ParaAttrs { + composer: Some(Composer::SingleLine), + mojikumi: Some("MojikumiTable/Compress".into()), + kinsoku_type: kind.map(str::to_string), + ..Default::default() + }, + ); + Arc::make_mut(&mut d.styles).mojikumi_tables.push(MojikumiTable { + name: "Compress".into(), + based_on: "SimpChineseDefault".into(), + overrides: vec![MojikumiAki { + target_class: 12, + side_class: 12, + after: true, + minimum: 0.0, + desired: 1.0, + maximum: 2.0, + priority: 1, + ..Default::default() + }], + }); + (d, sid) +} + +fn first_line_end(cs: &ComposedStory) -> (usize, f64) { + let line = all_lines(cs)[0]; + (line.range.end, line.end_x) +} + +#[test] +fn default_kinsoku_type_composes_as_push_in_first() { + // The empty value is InDesign's default (and what the UI's "Push In First" stores): it must + // behave as push-in-first, not as the minimum-adjustment mode. + let (d, sid) = kinsoku_push_pull_doc(None); + let default = compose_story(&d, sid, &ComposeOptions::default()); + let (d, sid) = kinsoku_push_pull_doc(Some("KinsokuPushInFirst")); + let push_in = compose_story(&d, sid, &ComposeOptions::default()); + let (d, sid) = kinsoku_push_pull_doc(Some("KinsokuPushOutFirst")); + let push_out = compose_story(&d, sid, &ComposeOptions::default()); + assert_eq!(first_line_end(&default).0, first_line_end(&push_in).0); + assert!(first_line_end(&default).0 > first_line_end(&push_out).0, "{:?} vs {:?}", first_line_end(&default), first_line_end(&push_out)); +} + +#[test] +fn push_out_only_leaves_the_line_end_uncompressed() { + // Push-out-only turns the 追い込み compression off: the line cannot be squeezed to reach a + // break, so it ends earlier (and never past the measure). + let (d, sid) = kinsoku_push_pull_doc(None); + let push_in = compose_story(&d, sid, &ComposeOptions::default()); + let (d, sid) = kinsoku_push_pull_doc(Some("KinsokuPushOutOnly")); + let push_out_only = compose_story(&d, sid, &ComposeOptions::default()); + assert!(first_line_end(&push_out_only).0 <= first_line_end(&push_in).0); + for cs in [&push_in, &push_out_only] { + for line in all_lines(cs) { + assert!(line.end_x <= line.x1 + 0.01, "line end {} past the measure {}", line.end_x, line.x1); + } + } + // Push-out-only's first line is not compressed to the measure: it ends short of it. + let (_, short) = first_line_end(&push_out_only); + let line = all_lines(&push_out_only)[0]; + assert!(line.x1 - short > 0.5, "no compression: end {short} vs measure {}", line.x1); +} + +#[test] +fn supplementary_ideograph_paragraph_keeps_the_kinsoku_gate() { + // Extension B 以降の漢字 (U+20000..) は breaker では Han として扱われる。段落全体の CJK 判定 + // (is_cjk_char) がそこを落とすと、補助面だけの段落で禁則の指定が効かなくなる。 + // 補助面だけの本文でも、追い込みと追い出しで行末が変わる (指定が効いている) こと。 + let first_end = |text: &str, kind: Option<&str>| { + let (d, sid) = kinsoku_push_pull_doc_text(text, kind); + first_line_end(&compose_story(&d, sid, &ComposeOptions::default())).0 + }; + let bmp = "甲乙丙丁戊己庚辛"; + let sup = "\u{20000}\u{20001}\u{20002}\u{20003}\u{20004}\u{20005}\u{20006}\u{20007}"; + // 対照: BMP の漢字だけの段落では、追い込みと追い出しで行末が変わる + assert_ne!(first_end(bmp, None), first_end(bmp, Some("KinsokuPushOutOnly")), "BMP の対照で禁則の指定が効いていない"); + // 補助面だけの段落でも同じように効く (判定から漏れると、どちらも同じ行末になる) + assert_ne!(first_end(sup, None), first_end(sup, Some("KinsokuPushOutOnly")), "補助面だけの段落で禁則の指定が効いていない"); + // 判定そのもの (Extension B の最初と最後、Ext G の終わり) + assert!(is_cjk_char('\u{20000}')); + assert!(is_cjk_char('\u{2A6DF}')); + assert!(is_cjk_char('\u{3FFFF}')); + assert!(!is_cjk_char('A')); +} + +#[test] +fn hangul_jamo_extended_blocks_count_as_cjk() { + // `is_cjk_char` takes Hangul from the breaker's classification, so the syllables and the + // Jamo Extended-A/B blocks count as well: a Hangul-only paragraph keeps its 禁則 priority. + for c in ['\u{A960}', '\u{A97F}', '\u{D7B0}', '\u{D7FF}'] { + assert!(is_cjk_char(c), "U+{:04X} is Hangul", c as u32); + } + assert!(is_cjk_char('\u{3105}'), "bopomofo is still CJK"); +} + +/// Shape `text` as a one-paragraph story with the default font and styles: the glyph list the +/// composition classifies before `split_drop_cap` and `prepend_label`. +fn shape_for_script_test(text: &str) -> Vec { + shape_for_script_test_with(text, &SubstCtx::default()) +} + +/// Same, with text variables resolved through `sub`. +fn shape_for_script_test_with(text: &str, sub: &SubstCtx) -> Vec { + let db = designcraft_fonts::FontDb::global().scoped(0); + let mut styles = Vec::new(); + let mut missing = std::collections::HashMap::new(); + let mut table = shape::StyleTable { styles: &mut styles, missing: &mut missing }; + let mut story = Story::new(StoryId(0)); + story.insert(0, text); + let doc_styles = designcraft_doc::Styles::default(); + shape::shape_para( + shape::ShapeInput { + db: &db, + styles: &doc_styles, + story: &story, + range: 0..text.len(), + para_chars: &designcraft_doc::CharProps::default(), + auto_leading: shape::TypeEnv { auto_leading: 1.2, adv: Default::default(), glyph_fallback: true }, + sub, + nested: &[], + grep: &[], + lines: &[], + drop_cap: (0, ""), + }, + &mut table, + ) + .glyphs +} + +#[test] +fn paragraph_script_counts_a_drop_cap_but_not_a_generated_label() { + // The 禁則 gate reads the paragraph's own shaped glyphs. The drop cap has not been split off + // yet, so a CJK one still makes the paragraph CJK. + assert!(paragraph_is_cjk(&shape_for_script_test("漢abc"))); + // A generated list label is prepended after the gate, so it is not part of the paragraph. + let mut glyphs = shape_for_script_test("abc"); + assert!(!paragraph_is_cjk(&glyphs)); + let db = designcraft_fonts::FontDb::global().scoped(0); + let mut styles = Vec::new(); + let mut missing = std::collections::HashMap::new(); + let mut table = shape::StyleTable { styles: &mut styles, missing: &mut missing }; + let n = prepend_label( + &db, + &mut glyphs, + "第1章", + 0, + &designcraft_doc::CharProps::default(), + shape::TypeEnv { auto_leading: 1.2, adv: Default::default(), glyph_fallback: true }, + &mut table, + ); + assert!(n > 0); + // The label's glyphs are CJK, so a gate computed after prepend_label would flip the answer. + assert!(glyphs.iter().take(n).any(|g| is_cjk_char(g.ch))); +} + +#[test] +fn paragraph_script_reads_a_text_variable_replacement() { + // A text variable's glyphs keep the source marker in `ch`; the paragraph shows the replacement, + // so a CJK replacement makes it CJK text even though no source character is. + let marker = designcraft_doc::vars::var_char(0).expect("variable 0"); + let text = format!("{marker}abc"); + let sub = SubstCtx { vars: Arc::new(vec!["漢字".into()]), ..Default::default() }; + let glyphs = shape_for_script_test_with(&text, &sub); + assert!(!glyphs.iter().any(|g| is_cjk_char(g.ch)), "the source is only the marker and Latin text"); + assert!(glyphs.iter().any(|g| g.generated_text.as_deref().is_some_and(|t| t.chars().any(is_cjk_char)))); + assert!(paragraph_is_cjk(&glyphs), "the CJK replacement is the paragraph's text"); +} + +#[test] +fn kinsoku_priority_modes_map_to_breaker_policies() { + let mut pp = ParaProps::default(); + let story = designcraft_doc::Story::new(designcraft_doc::StoryId::default()); + for (kind, priority) in [ + ("", 1u8), + ("KinsokuPushInFirst", 1), + ("KinsokuPushOutFirst", 2), + ("KinsokuPushOutOnly", 3), + ("KinsokuPrioritizeAdjustmentAmount", 0), + ("KinsokuSomethingElse", 0), + ] { + pp.kinsoku_type = kind.to_string(); + let sp = spacing_for(&pp, 12.0, &story, true); + assert_eq!(sp.kinsoku_priority, priority, "{kind}"); + assert!(sp.kinsoku_cjk, "{kind}"); + assert!(!spacing_for(&pp, 12.0, &story, false).kinsoku_cjk, "{kind}: Latin text is not biased"); + } +} + +#[test] +fn latin_paragraphs_are_not_biased_by_the_kinsoku_priority() { + // A justified Latin paragraph composes the same with push-in-first, push-out-first and + // minimum adjustment: the policy is about CJK 禁則, and a Latin paragraph has none. + let compose_with = |kind: &str| { + let (mut d, sid, _) = + doc_with(LOREM, Rect::new(0.0, 0.0, 200.0, 500.0), ParaAttrs { align: Some(Align::FullyJustified), ..Default::default() }); + d.story_mut(sid).unwrap().paras[0].para.kinsoku_type = Some(kind.to_string()); + compose_story(&d, sid, &ComposeOptions::default()) + }; + let ends = |cs: &ComposedStory| all_lines(cs).iter().map(|l| l.range.end).collect::>(); + let push_in = ends(&compose_with("")); + assert_eq!(ends(&compose_with("KinsokuPushInFirst")), push_in); + assert_eq!(ends(&compose_with("KinsokuPushOutFirst")), push_in); + assert_eq!(ends(&compose_with("KinsokuPrioritizeAdjustmentAmount")), push_in); +} + #[test] fn mojikumi_priority_and_elastic_line_start_are_used_in_placement() { use designcraft_doc::cjk::{MojikumiAki, MojikumiTable}; diff --git a/crates/doc/src/anchored.rs b/crates/doc/src/anchored.rs index 524e2bfc5..f5c4b856c 100644 --- a/crates/doc/src/anchored.rs +++ b/crates/doc/src/anchored.rs @@ -749,7 +749,7 @@ mod tests { id, name: name.into(), mime: "application/pdf".into(), - data: Arc::new(format!("%PDF-1.7 {name}").into_bytes()), + data: crate::AssetData::embedded(format!("%PDF-1.7 {name}").into_bytes()), ..Default::default() }; let (mut a, host) = doc(); diff --git a/crates/doc/src/asset_data.rs b/crates/doc/src/asset_data.rs new file mode 100644 index 000000000..a4584c19c --- /dev/null +++ b/crates/doc/src/asset_data.rs @@ -0,0 +1,411 @@ +//! The bytes of a placed file ([`crate::Asset::data`]): held in memory (embedded) or read from +//! the linked file when needed. Caches of decoded images key by the value's [`SourceKey`], +//! which outlives the bytes: a decoded image stays usable after the encoded bytes are dropped. +//! +//! Linked bytes are read by the [`Loader`] the engine installs ([`install_loader`]; this crate +//! does no file I/O) and kept in a process-wide store of at most [`STORE_BUDGET`] bytes, least +//! recently used first out. A file over [`MAX_LINKED_BYTES`] is never read. + +use std::fmt; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; + +use crate::datamerge::Fingerprint; + +mod store; + +pub use store::{Loaded, Loader, MAX_LINKED_BYTES, STORE_BUDGET, install_loader}; + +/// Names one version of an asset's bytes. Every [`AssetData`] made from new bytes gets a key no +/// other value in this process has had; clones share it. Keys are never reused, so a cache +/// entry under a key can't be mistaken for different bytes allocated later. Counting starts at +/// 1: `SourceKey(0)` names no bytes. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct SourceKey(pub u64); + +impl SourceKey { + /// A key no other value has had. + pub fn fresh() -> Self { + static NEXT: AtomicU64 = AtomicU64::new(1); + SourceKey(NEXT.fetch_add(1, Ordering::Relaxed)) + } +} + +/// Why an asset's bytes can't be had. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Unavailable { + #[error("the linked file is missing")] + Missing, + #[error("the linked file has changed")] + Modified, + #[error("the linked file is too large to load")] + TooLarge, + #[error("no loader for linked files")] + NoLoader, +} + +/// Where a linked asset's bytes are, and what they must be: the [`crate::Asset`] link record +/// ([`LinkedSource::of`]) with the document it is resolved against. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct LinkedSource { + /// The file's absolute path ([`crate::Asset::link`]), tried first. + pub path: String, + /// The path relative to the document's folder ([`crate::Asset::link_relative`]), tried when + /// the absolute path doesn't hold the bytes. + pub relative: Option, + /// The document file `relative` is resolved against. + pub document: Option, + /// The size of the bytes. + pub len: u64, + /// Size and modification time of the file when it was known to hold the bytes: a file that + /// still has them is read without hashing. + pub fingerprint: Option, + /// The bytes' content hash ([`crate::Asset::link_hash`], `sha256:` and hex digits), checked + /// when the file's fingerprint differs. + pub hash: Option, +} + +impl LinkedSource { + /// The source `asset`'s link record names, resolved against the document file at + /// `document`. `None` without a link, or without the size its fingerprint records. + pub fn of(asset: &crate::Asset, document: Option<&Path>) -> Option { + let fingerprint = asset.link_fingerprint?; + Some(LinkedSource { + path: asset.link.clone()?, + relative: asset.link_relative.clone(), + document: document.map(Path::to_path_buf), + len: fingerprint.size, + fingerprint: Some(fingerprint), + hash: asset.link_hash.clone(), + }) + } +} + +/// A linked source, with the fingerprint a load last found the file at (hash-checked), so later +/// loads of a file whose time changed but whose bytes didn't skip the hash. +struct Linked { + source: LinkedSource, + trusted: Mutex>, +} + +impl Linked { + fn trusted(&self) -> Option { + *self.trusted.lock().unwrap_or_else(PoisonError::into_inner) + } + + fn trust(&self, fingerprint: Fingerprint) { + *self.trusted.lock().unwrap_or_else(PoisonError::into_inner) = Some(fingerprint); + } +} + +#[derive(Clone)] +enum Repr { + Embedded(Arc>), + Linked(Arc), +} + +/// An asset's bytes: embedded in the document, or linked (read from the file on demand). +/// Cloning is cheap and keeps the [`SourceKey`]. +#[derive(Clone)] +pub struct AssetData { + key: SourceKey, + repr: Repr, +} + +impl AssetData { + /// Embedded bytes, under a new key. + pub fn embedded(bytes: impl Into>>) -> Self { + AssetData { key: SourceKey::fresh(), repr: Repr::Embedded(bytes.into()) } + } + + /// Embedded bytes under `key`, which the caller made ([`SourceKey::fresh`]) for exactly these + /// bytes: bytes derived again from the same input (a layer variant of the same file) keep + /// the key their decodes are cached under. Different bytes under one key would draw stale + /// pixels. + pub fn embedded_under(key: SourceKey, bytes: impl Into>>) -> Self { + AssetData { key, repr: Repr::Embedded(bytes.into()) } + } + + /// The bytes of the linked file `source` names, under a new key. + pub fn linked(source: LinkedSource) -> Self { + AssetData { key: SourceKey::fresh(), repr: Repr::Linked(Arc::new(Linked { source, trusted: Mutex::new(None) })) } + } + + /// The linked file `source` names, under `key`: embedded bytes found identical to the file + /// (hash-checked by the caller) become a link without decoding them again. As with + /// [`AssetData::embedded_under`], other bytes under one key would draw stale pixels. + pub fn linked_under(key: SourceKey, source: LinkedSource) -> Self { + AssetData { key, repr: Repr::Linked(Arc::new(Linked { source, trusted: Mutex::new(None) })) } + } + + /// This value pointing at `source` (the file moved, the document was saved elsewhere, the + /// fingerprint was brought up to date). It keeps the key when both name the same bytes + /// (same size and content hash), so nothing decoded is decoded again. + pub fn with_source(&self, source: LinkedSource) -> Self { + let same = self.linked_source().is_some_and(|old| old.len == source.len && old.hash.is_some() && old.hash == source.hash); + let mut new = AssetData::linked(source); + if same { + new.key = self.key; + } + new + } + + /// The bytes, when they are embedded. + pub fn embedded_bytes(&self) -> Option<&Arc>> { + match &self.repr { + Repr::Embedded(b) => Some(b), + Repr::Linked(_) => None, + } + } + + /// Where linked bytes are read from. + pub fn linked_source(&self) -> Option<&LinkedSource> { + match &self.repr { + Repr::Embedded(_) => None, + Repr::Linked(l) => Some(&l.source), + } + } + + /// The bytes: embedded ones, or the linked file's (read once by the installed [`Loader`], + /// and kept while the store's budget allows). Concurrent loads of one value read it once. + pub fn load(&self) -> Result>, Unavailable> { + match &self.repr { + Repr::Embedded(b) => Ok(Arc::clone(b)), + Repr::Linked(l) => store::global().load(self.key, l, store::installed().as_deref()), + } + } + + /// The bytes for output: [`Self::load`], or for a linked file that has changed the file's + /// current bytes, flagged `true`, which are never cached. Linked bytes already in memory are + /// used only while the file still holds them ([`Loader::holds`]): output goes by the file as + /// it is now. + pub fn load_for_output(&self) -> Result<(Arc>, bool), Unavailable> { + if let Repr::Linked(l) = &self.repr + && let Some(kept) = store::global().resident(self.key) + && let Some(loader) = store::installed() + { + return match loader.holds(&l.source, l.trusted(), &kept) { + Ok(found) => { + if let Some(fingerprint) = found { + l.trust(fingerprint); + } + Ok((kept, false)) + } + Err(Unavailable::Modified) => loader.load_current(&l.source).map(|b| (Arc::new(b), true)), + Err(e) => Err(e), + }; + } + match self.load() { + Ok(b) => Ok((b, false)), + Err(Unavailable::Modified) => { + let (Some(source), Some(loader)) = (self.linked_source(), store::installed()) else { return Err(Unavailable::Modified) }; + loader.load_current(source).map(|b| (Arc::new(b), true)) + } + Err(e) => Err(e), + } + } + + /// The bytes without reading anything: embedded ones, or linked ones the store holds. + pub fn resident(&self) -> Option>> { + match &self.repr { + Repr::Embedded(b) => Some(Arc::clone(b)), + Repr::Linked(_) => store::global().resident(self.key), + } + } + + /// Keep `bytes`, just read from the linked file and checked by the caller to be the ones its + /// source names, in the store, so the first [`AssetData::load`] doesn't read the file again. + /// Embedded values, bytes of another size and bytes over the store's budget are ignored. + pub fn keep_resident(&self, bytes: Arc>) { + if let Repr::Linked(l) = &self.repr + && bytes.len() as u64 == l.source.len + { + store::global().keep(self.key, bytes); + } + } + + /// Drop the store's copy of linked bytes (they are read again when next loaded). + pub fn evict(&self) { + if self.is_linked() { + store::global().evict(self.key); + } + } + + pub fn key(&self) -> SourceKey { + self.key + } + + /// The size of the bytes. + pub fn len(&self) -> u64 { + match &self.repr { + Repr::Embedded(b) => b.len() as u64, + Repr::Linked(l) => l.source.len, + } + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn is_linked(&self) -> bool { + matches!(self.repr, Repr::Linked(_)) + } + + /// A handle that doesn't keep embedded bytes alive, and upgrades to this value (same key) + /// while anything else does. + pub fn downgrade(&self) -> WeakAssetData { + let repr = match &self.repr { + Repr::Embedded(b) => WeakRepr::Embedded(Arc::downgrade(b)), + Repr::Linked(l) => WeakRepr::Linked(Arc::clone(l)), + }; + WeakAssetData { key: self.key, repr } + } +} + +/// Advances when linked files may have come back or changed ([`links_changed`]). +static LINK_GENERATION: AtomicU64 = AtomicU64::new(0); + +/// Say that linked files may have changed on disk (a relink, an update, a status check): caches +/// that remembered a linked file as unavailable ask for it again. +pub fn links_changed() { + LINK_GENERATION.fetch_add(1, Ordering::AcqRel); +} + +/// The count [`links_changed`] advances. +pub fn link_generation() -> u64 { + LINK_GENERATION.load(Ordering::Acquire) +} + +#[derive(Clone)] +enum WeakRepr { + Embedded(Weak>), + Linked(Arc), +} + +/// [`AssetData::downgrade`]. +#[derive(Clone)] +pub struct WeakAssetData { + key: SourceKey, + repr: WeakRepr, +} + +impl WeakAssetData { + /// The value, while its embedded bytes are held elsewhere. + pub fn upgrade(&self) -> Option { + let repr = match &self.repr { + WeakRepr::Embedded(w) => Repr::Embedded(w.upgrade()?), + WeakRepr::Linked(l) => Repr::Linked(Arc::clone(l)), + }; + Some(AssetData { key: self.key, repr }) + } + + pub fn key(&self) -> SourceKey { + self.key + } +} + +impl fmt::Debug for WeakAssetData { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("WeakAssetData").field("key", &self.key.0).finish() + } +} + +/// New embedded bytes ([`AssetData::embedded`]). +impl From> for AssetData { + fn from(bytes: Vec) -> Self { + AssetData::embedded(bytes) + } +} + +impl Default for AssetData { + fn default() -> Self { + AssetData::embedded(Vec::new()) + } +} + +/// Embedded bytes compare by content; linked ones are equal only as the same version. +impl PartialEq for AssetData { + fn eq(&self, other: &Self) -> bool { + match (&self.repr, &other.repr) { + (Repr::Embedded(a), Repr::Embedded(b)) => a == b, + (Repr::Linked(a), Repr::Linked(b)) => self.key == other.key && a.source.len == b.source.len, + _ => false, + } + } +} + +impl fmt::Debug for AssetData { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let kind = if self.is_linked() { "Linked" } else { "Embedded" }; + f.debug_struct(kind).field("key", &self.key.0).field("len", &self.len()).finish() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + pub(super) fn source(path: &str, len: u64) -> LinkedSource { + LinkedSource { path: path.into(), relative: None, document: None, len, fingerprint: None, hash: None } + } + + #[test] + fn clones_keep_the_key_and_new_bytes_get_a_new_one() { + let a = AssetData::embedded(vec![1, 2, 3]); + let b = a.clone(); + assert_eq!(a.key(), b.key()); + let c = AssetData::embedded(vec![1, 2, 3]); + assert_ne!(a.key(), c.key()); + assert_eq!(a, c, "embedded bytes compare by content"); + let shared = AssetData::embedded(Arc::clone(a.embedded_bytes().unwrap())); + assert_ne!(a.key(), shared.key(), "wrapping bytes again names a new version"); + assert_eq!(a.load().unwrap().as_slice(), [1, 2, 3]); + assert_eq!(a.len(), 3); + let weak = a.downgrade(); + assert_eq!(weak.upgrade().map(|d| d.key()), Some(a.key())); + drop((a, b)); + assert!(weak.upgrade().is_some(), "`shared` still holds the bytes"); + drop(shared); + assert!(weak.upgrade().is_none()); + } + + #[test] + fn linked_assets_keep_their_key_only_for_the_same_bytes() { + let mut s = source("/nowhere/no-loader.png", 10); + let l = AssetData::linked(s.clone()); + assert!(l.is_linked()); + assert_eq!(l.len(), 10); + assert!(l.embedded_bytes().is_none() && l.resident().is_none()); + assert!(AssetData::default().is_empty()); + assert_ne!(l.with_source(s.clone()).key(), l.key(), "no hash: nothing says the bytes are the same"); + s.hash = Some("sha256:00".into()); + let hashed = AssetData::linked(s.clone()); + s.path = "/elsewhere/no-loader.png".into(); + let moved = hashed.with_source(s.clone()); + assert_eq!((moved.key(), moved.linked_source().map(|s| s.path.as_str())), (hashed.key(), Some("/elsewhere/no-loader.png"))); + s.hash = Some("sha256:01".into()); + assert_ne!(hashed.with_source(s).key(), hashed.key(), "other bytes, another key"); + let weak = hashed.downgrade(); + drop(hashed); + assert!(weak.upgrade().is_some(), "a linked value holds no bytes to let go of"); + } + + #[test] + fn a_link_record_names_its_source() { + let fingerprint = Fingerprint { size: 7, mtime: 9 }; + let asset = crate::Asset { + link: Some("/a/b.png".into()), + link_relative: Some("b.png".into()), + link_fingerprint: Some(fingerprint), + link_hash: Some("sha256:ab".into()), + ..Default::default() + }; + let s = LinkedSource::of(&asset, Some(Path::new("/a/doc.dcx"))).unwrap(); + assert_eq!( + (s.len, s.fingerprint, s.relative.as_deref(), s.document.as_deref()), + (7, Some(fingerprint), Some("b.png"), Some(Path::new("/a/doc.dcx"))) + ); + assert!(LinkedSource::of(&crate::Asset { link_fingerprint: None, ..asset }, None).is_none(), "the size comes from the fingerprint"); + } +} diff --git a/crates/doc/src/asset_data/store.rs b/crates/doc/src/asset_data/store.rs new file mode 100644 index 000000000..787289a38 --- /dev/null +++ b/crates/doc/src/asset_data/store.rs @@ -0,0 +1,370 @@ +//! The process-wide store of linked files' bytes ([`super::AssetData::load`]). + +use std::collections::HashMap; +use std::sync::{Arc, Condvar, LazyLock, Mutex, MutexGuard, PoisonError, RwLock}; + +use super::{Linked, LinkedSource, SourceKey, Unavailable}; +use crate::datamerge::Fingerprint; + +/// Linked bytes kept in memory at most, over all files. A file larger than this is read for the +/// one load that asks for it and not kept. +pub const STORE_BUDGET: u64 = 512 << 20; + +/// The largest linked file read: a larger one is [`Unavailable::TooLarge`]. +pub const MAX_LINKED_BYTES: u64 = 1 << 30; + +/// A linked file's bytes as a [`Loader`] read them. +pub struct Loaded { + pub bytes: Vec, + /// The fingerprint the file had, when its bytes were found to be the expected ones by their + /// hash: later loads trust that fingerprint as they trust [`LinkedSource::fingerprint`]. + pub fingerprint: Option, +} + +/// Reads linked files (the engine's, [`install_loader`]). +pub trait Loader: Send + Sync { + /// The bytes of the file `source` names, if it holds the expected ones: its size is + /// `source.len`, and its fingerprint is `source.fingerprint` or `trusted`, or its content + /// hash is `source.hash`. Other bytes are [`Unavailable::Modified`]; no file is + /// [`Unavailable::Missing`]; one over [`MAX_LINKED_BYTES`] is [`Unavailable::TooLarge`]. + fn load(&self, source: &LinkedSource, trusted: Option) -> Result; + + /// The bytes the file `source` names has now, whatever they are (for output that uses the + /// current file of a modified link). Never cached. + fn load_current(&self, _source: &LinkedSource) -> Result, Unavailable> { + Err(Unavailable::NoLoader) + } + + /// Whether the file `source` names still holds `kept`, the bytes read from it earlier: by its + /// fingerprint as [`Self::load`] goes by it, else by its content hash, or for a source without + /// a hash by its bytes. → the file's fingerprint when it was read to tell, else `None`; + /// [`Unavailable::Modified`] when the files there hold other bytes. A loader that can't look + /// takes the kept bytes as the file's. + fn holds(&self, _source: &LinkedSource, _trusted: Option, _kept: &[u8]) -> Result, Unavailable> { + Ok(None) + } +} + +static LOADER: RwLock>> = RwLock::new(None); + +/// Read linked files with `loader` from now on (in the whole process). +pub fn install_loader(loader: Arc) { + *LOADER.write().unwrap_or_else(PoisonError::into_inner) = Some(loader); +} + +pub(super) fn installed() -> Option> { + LOADER.read().unwrap_or_else(PoisonError::into_inner).clone() +} + +pub(super) fn global() -> &'static Store { + static STORE: LazyLock = LazyLock::new(|| Store::new(STORE_BUDGET, MAX_LINKED_BYTES)); + &STORE +} + +type Outcome = Result>, Unavailable>; + +/// One load in progress, which the other loads of the same bytes wait for. +#[derive(Default)] +struct Flight { + outcome: Mutex>, + landed: Condvar, +} + +impl Flight { + fn wait(&self) -> Outcome { + let mut g = self.outcome.lock().unwrap_or_else(PoisonError::into_inner); + loop { + if let Some(outcome) = g.as_ref() { + return outcome.clone(); + } + g = self.landed.wait(g).unwrap_or_else(PoisonError::into_inner); + } + } +} + +struct Kept { + bytes: Arc>, + stamp: u64, +} + +#[derive(Default)] +struct Inner { + kept: HashMap, + flights: HashMap>, + bytes: u64, + clock: u64, +} + +impl Inner { + /// The bytes kept under `key`, marked used. + fn touch(&mut self, key: SourceKey) -> Option>> { + self.clock += 1; + let clock = self.clock; + self.kept.get_mut(&key).map(|k| { + k.stamp = clock; + Arc::clone(&k.bytes) + }) + } + + /// Keep `bytes` under `key`, dropping the least recently used others beyond `budget`. + fn keep(&mut self, key: SourceKey, bytes: Arc>, budget: u64) { + self.clock += 1; + let n = bytes.len() as u64; + if let Some(old) = self.kept.insert(key, Kept { bytes, stamp: self.clock }) { + self.bytes = self.bytes.saturating_sub(old.bytes.len() as u64); + } + self.bytes = self.bytes.saturating_add(n); + while self.bytes > budget { + let Some(oldest) = self.kept.iter().filter(|(k, _)| **k != key).min_by_key(|(_, k)| k.stamp).map(|(k, _)| *k) else { break }; + if let Some(old) = self.kept.remove(&oldest) { + self.bytes = self.bytes.saturating_sub(old.bytes.len() as u64); + } + } + } +} + +/// A single-flight, least-recently-used store of loaded bytes by [`SourceKey`]. +pub(super) struct Store { + inner: Mutex, + budget: u64, + cap: u64, +} + +/// Lands a flight when dropped: with its outcome, or as missing when the loader panicked, so +/// the loads waiting for it don't wait forever. +struct Landing<'a> { + store: &'a Store, + key: SourceKey, + flight: Arc, + outcome: Option, +} + +impl Drop for Landing<'_> { + fn drop(&mut self) { + let outcome = self.outcome.take().unwrap_or(Err(Unavailable::Missing)); + { + let mut g = self.store.lock(); + g.flights.remove(&self.key); + if let Ok(bytes) = &outcome + && bytes.len() as u64 <= self.store.budget + { + g.keep(self.key, Arc::clone(bytes), self.store.budget); + } + } + *self.flight.outcome.lock().unwrap_or_else(PoisonError::into_inner) = Some(outcome); + self.flight.landed.notify_all(); + } +} + +impl Store { + pub(super) fn new(budget: u64, cap: u64) -> Self { + Store { inner: Mutex::new(Inner::default()), budget, cap } + } + + fn lock(&self) -> MutexGuard<'_, Inner> { + self.inner.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// The bytes of `linked`, kept under `key`: from the store, from a load of them in progress, + /// or read by `loader`. + pub(super) fn load(&self, key: SourceKey, linked: &Linked, loader: Option<&dyn Loader>) -> Outcome { + if linked.source.len > self.cap { + return Err(Unavailable::TooLarge); + } + let flight = { + let mut g = self.lock(); + if let Some(bytes) = g.touch(key) { + return Ok(bytes); + } + if let Some(f) = g.flights.get(&key).cloned() { + drop(g); + return f.wait(); + } + let Some(loader) = loader else { return Err(Unavailable::NoLoader) }; + let f = Arc::new(Flight::default()); + g.flights.insert(key, Arc::clone(&f)); + (f, loader) + }; + let (flight, loader) = flight; + let mut landing = Landing { store: self, key, flight, outcome: None }; + // Read outside the lock: other bytes load and are found meanwhile. + let outcome = match loader.load(&linked.source, linked.trusted()) { + Ok(l) if l.bytes.len() as u64 > self.cap => Err(Unavailable::TooLarge), + Ok(l) => { + if let Some(fingerprint) = l.fingerprint { + linked.trust(fingerprint); + } + Ok(Arc::new(l.bytes)) + } + Err(e) => Err(e), + }; + landing.outcome = Some(outcome.clone()); + drop(landing); + outcome + } + + /// The bytes kept under `key`, without loading them. + pub(super) fn resident(&self, key: SourceKey) -> Option>> { + self.lock().touch(key) + } + + /// Keep `bytes` under `key` as if a load had read them (not when over the budget). + pub(super) fn keep(&self, key: SourceKey, bytes: Arc>) { + if bytes.len() as u64 <= self.budget { + self.lock().keep(key, bytes, self.budget); + } + } + + pub(super) fn evict(&self, key: SourceKey) { + let mut g = self.lock(); + if let Some(old) = g.kept.remove(&key) { + g.bytes = g.bytes.saturating_sub(old.bytes.len() as u64); + } + } + + #[cfg(test)] + fn kept_bytes(&self) -> u64 { + self.lock().bytes + } +} + +#[cfg(test)] +mod tests { + use super::super::tests::source; + use super::super::{AssetData, Repr}; + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + + /// Serves `len` bytes of `fill` for every path, counting its reads; slow, so loads overlap. + #[derive(Default)] + struct Counting { + reads: AtomicUsize, + delay_ms: u64, + fail: Option, + trusted: Mutex>>, + } + + impl Loader for Counting { + fn load(&self, source: &LinkedSource, trusted: Option) -> Result { + self.reads.fetch_add(1, Ordering::SeqCst); + self.trusted.lock().unwrap().push(trusted); + std::thread::sleep(std::time::Duration::from_millis(self.delay_ms)); + if let Some(e) = self.fail { + return Err(e); + } + let fill = source.path.bytes().last().unwrap_or(0); + Ok(Loaded { bytes: vec![fill; source.len as usize], fingerprint: Some(Fingerprint { size: source.len, mtime: 5 }) }) + } + } + + fn linked(data: &AssetData) -> &Linked { + match &data.repr { + Repr::Linked(l) => l, + Repr::Embedded(_) => panic!("not linked"), + } + } + + fn load(store: &Store, data: &AssetData, loader: &dyn Loader) -> Outcome { + store.load(data.key(), linked(data), Some(loader)) + } + + #[test] + fn linked_bytes_are_read_once_and_kept() { + let store = Store::new(100, 1000); + let loader = Counting::default(); + let a = AssetData::linked(source("a", 10)); + assert_eq!(load(&store, &a, &loader).unwrap().as_slice(), [b'a'; 10]); + assert_eq!(load(&store, &a.clone(), &loader).unwrap().len(), 10); + assert_eq!(loader.reads.load(Ordering::SeqCst), 1); + assert_eq!(store.resident(a.key()).map(|b| b.len()), Some(10)); + assert_eq!(*loader.trusted.lock().unwrap(), [None]); + store.evict(a.key()); + load(&store, &a, &loader).unwrap(); + assert_eq!(loader.reads.load(Ordering::SeqCst), 2, "evicted bytes are read again"); + assert_eq!( + loader.trusted.lock().unwrap().last(), + Some(&Some(Fingerprint { size: 10, mtime: 5 })), + "a hash-checked fingerprint is trusted next time" + ); + assert_eq!(AssetData::linked(source("x", 1)).load(), Err(Unavailable::NoLoader), "no loader installed in these tests"); + } + + #[test] + fn the_least_recently_used_bytes_go_past_the_budget() { + let store = Store::new(100, 1000); + let loader = Counting::default(); + let [a, b, c] = ["a", "b", "c"].map(|p| AssetData::linked(source(p, 40))); + load(&store, &a, &loader).unwrap(); + load(&store, &b, &loader).unwrap(); + load(&store, &a, &loader).unwrap(); + load(&store, &c, &loader).unwrap(); + assert!(store.resident(b.key()).is_none(), "b was used least recently"); + assert!(store.resident(a.key()).is_some() && store.resident(c.key()).is_some()); + assert_eq!(store.kept_bytes(), 80); + } + + #[test] + fn a_file_over_the_budget_is_returned_but_not_kept_and_one_over_the_cap_is_not_read() { + let store = Store::new(100, 1000); + let loader = Counting::default(); + let small = AssetData::linked(source("s", 50)); + load(&store, &small, &loader).unwrap(); + let big = AssetData::linked(source("b", 200)); + assert_eq!(load(&store, &big, &loader).unwrap().len(), 200); + assert!(store.resident(big.key()).is_none()); + assert!(store.resident(small.key()).is_some(), "an unkept file evicts nothing"); + let huge = AssetData::linked(source("h", 1001)); + assert_eq!(load(&store, &huge, &loader), Err(Unavailable::TooLarge)); + assert_eq!(loader.reads.load(Ordering::SeqCst), 2); + } + + #[test] + fn concurrent_loads_of_the_same_bytes_read_once() { + let store = Store::new(1000, 1000); + let loader = Counting { delay_ms: 50, ..Default::default() }; + let a = AssetData::linked(source("a", 10)); + let got: Vec = std::thread::scope(|s| { + let handles: Vec<_> = (0..8).map(|_| s.spawn(|| load(&store, &a, &loader))).collect(); + handles.into_iter().map(|h| h.join().unwrap()).collect() + }); + assert!(got.iter().all(|g| g.as_ref().is_ok_and(|b| b.len() == 10))); + assert_eq!(loader.reads.load(Ordering::SeqCst), 1); + } + + #[test] + fn a_failed_load_is_shared_and_tried_again_later() { + let store = Store::new(1000, 1000); + let loader = Counting { delay_ms: 30, fail: Some(Unavailable::Modified), ..Default::default() }; + let a = AssetData::linked(source("a", 10)); + let got: Vec = std::thread::scope(|s| { + let handles: Vec<_> = (0..4).map(|_| s.spawn(|| load(&store, &a, &loader))).collect(); + handles.into_iter().map(|h| h.join().unwrap()).collect() + }); + assert!(got.iter().all(|g| *g == Err(Unavailable::Modified))); + assert!(store.resident(a.key()).is_none(), "nothing adopted"); + let reads = loader.reads.load(Ordering::SeqCst); + load(&store, &a, &loader).unwrap_err(); + assert_eq!(loader.reads.load(Ordering::SeqCst), reads + 1, "failures aren't remembered here"); + } + + /// A loader that panics lands its flight: the loads waiting for it return. + #[test] + fn a_panicking_loader_does_not_hang_the_waiters() { + struct Panics; + impl Loader for Panics { + fn load(&self, _: &LinkedSource, _: Option) -> Result { + std::thread::sleep(std::time::Duration::from_millis(30)); + panic!("loader fault"); + } + } + let store = Store::new(1000, 1000); + let a = AssetData::linked(source("a", 10)); + std::thread::scope(|s| { + let first = s.spawn(|| load(&store, &a, &Panics)); + std::thread::sleep(std::time::Duration::from_millis(5)); + let waiter = s.spawn(|| load(&store, &a, &Panics)); + assert!(first.join().is_err()); + assert!(waiter.join().is_ok_and(|r| r.is_err())); + }); + } +} diff --git a/crates/doc/src/assets.rs b/crates/doc/src/assets.rs index 0d100cee7..18f07326f 100644 --- a/crates/doc/src/assets.rs +++ b/crates/doc/src/assets.rs @@ -108,8 +108,23 @@ impl Asset { } } + /// The same bytes: the same version, or equal embedded bytes. Linked bytes aren't read here. fn same_bytes(&self, other: &Asset) -> bool { - Arc::ptr_eq(&self.data, &other.data) || (self.data.len() == other.data.len() && *self.data == *other.data) + if self.data.key() == other.data.key() { + return true; + } + match (self.data.embedded_bytes(), other.data.embedded_bytes()) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b) || (a.len() == b.len() && **a == **b), + _ => false, + } + } + + /// A hash that is equal for assets with [`Asset::same_bytes`]. + fn bytes_hash(&self, state: &std::collections::hash_map::RandomState) -> u64 { + match self.data.embedded_bytes() { + Some(b) => state.hash_one(b.as_slice()), + None => state.hash_one(self.data.key()), + } } } @@ -132,7 +147,7 @@ pub fn insert_asset(assets: &mut BTreeMap>, mut asset: Asset if !asset.data.is_empty() && let Some(a) = assets.values().find(|a| a.same_bytes(&asset)) { - asset.data = Arc::clone(&a.data); + asset.data = a.data.clone(); } let id = asset.id; assets.insert(id, Arc::new(asset)); @@ -165,7 +180,7 @@ impl AssetIndex { assets.insert(id, Arc::new(asset)); return id; } - let hash = self.fingerprint.hash_one(asset.data.as_slice()); + let hash = asset.bytes_hash(&self.fingerprint); let key = (hash, asset.mime.clone(), asset.page, asset.link.clone()); if let Some(id) = self.files.get(&key).and_then(|ids| ids.iter().copied().find(|id| assets.get(id).is_some_and(|a| a.same_file(&asset)))) { return id; @@ -173,7 +188,7 @@ impl AssetIndex { if !asset.data.is_empty() { let bucket = self.bytes.entry(hash).or_default(); match bucket.iter().find_map(|id| assets.get(id).filter(|a| a.same_bytes(&asset))) { - Some(a) => asset.data = Arc::clone(&a.data), + Some(a) => asset.data = a.data.clone(), None => bucket.push(asset.id), } } @@ -381,7 +396,7 @@ impl Document { if a.data.is_empty() && a.link.is_none() { continue; } - let hash = *hashes.entry(Arc::as_ptr(&a.data)).or_insert_with(|| fingerprint.hash_one(a.data.as_slice())); + let hash = *hashes.entry(a.data.key()).or_insert_with(|| a.bytes_hash(&fingerprint)); let bucket = reps.entry((hash, a.mime.as_str(), a.page, a.link.as_deref())).or_default(); // A fingerprint only selects candidates; equality still checks the actual file. match bucket.iter().find(|r| r.same_file(a)) { @@ -411,7 +426,13 @@ mod tests { use designcraft_geom::{Affine, Rect, shapes}; fn asset(id: u64, data: &[u8]) -> Asset { - Asset { id: AssetId(id), name: "p.png".into(), mime: "image/png".into(), data: Arc::new(data.to_vec()), ..Default::default() } + Asset { + id: AssetId(id), + name: "p.png".into(), + mime: "image/png".into(), + data: crate::AssetData::embedded(data.to_vec()), + ..Default::default() + } } #[test] @@ -426,7 +447,7 @@ mod tests { assert_eq!(d.merge_duplicate_assets(), 256); assert_eq!(d.assets.len(), 256); for id in 1..=256u64 { - assert_eq!(d.assets[&AssetId(id)].data[1016..], id.to_le_bytes()); + assert_eq!(d.assets[&AssetId(id)].data.embedded_bytes().unwrap()[1016..], id.to_le_bytes()); } } @@ -547,7 +568,10 @@ mod tests { assert_eq!(s, i); } assert_eq!(scanned.keys().collect::>(), indexed.keys().collect::>()); - assert!(Arc::ptr_eq(&indexed[&AssetId(5)].data, &indexed[&AssetId(6)].data), "identical bytes share one buffer"); + assert!( + Arc::ptr_eq(indexed[&AssetId(5)].data.embedded_bytes().unwrap(), indexed[&AssetId(6)].data.embedded_bytes().unwrap()), + "identical bytes share one buffer" + ); } /// Many empty unlinked placements (graphics with neither bytes nor a link) insert without @@ -581,7 +605,10 @@ mod tests { assert!(embedded != a && a != b && embedded != b); assert_eq!(d.assets[&embedded].link, None); assert_eq!(d.assets[&b].link.as_deref(), Some("/q/b.png")); - assert!(Arc::ptr_eq(&d.assets[&a].data, &d.assets[&b].data), "identical bytes share one buffer"); + assert!( + Arc::ptr_eq(d.assets[&a].data.embedded_bytes().unwrap(), d.assets[&b].data.embedded_bytes().unwrap()), + "identical bytes share one buffer" + ); } #[test] diff --git a/crates/doc/src/attrs/para_attrs.rs b/crates/doc/src/attrs/para_attrs.rs index 0077c8e12..b4f356b10 100644 --- a/crates/doc/src/attrs/para_attrs.rs +++ b/crates/doc/src/attrs/para_attrs.rs @@ -113,7 +113,10 @@ attr_set! { kinsoku: Option = None => "deserialize_explicit_value", /// Preserved spacing-table reference; compatibility audit reports unsupported composition. mojikumi: String = String::new(), - /// Preserved priority policy; the current breaker does not implement these priorities. + /// JIS X 4051 禁則処理 type, applied by both composers to CJK text: empty (the default) + /// and `KinsokuPushInFirst` compose as push-in-first, `KinsokuPushOutFirst`, + /// `KinsokuPushOutOnly` and `KinsokuPrioritizeAdjustmentAmount` (minimum adjustment) as + /// their names say. kinsoku_type: String = String::new(), kinsoku_hang: crate::cjk::KinsokuHang = crate::cjk::KinsokuHang::None, bunri_kinshi: bool = false, diff --git a/crates/doc/src/build.rs b/crates/doc/src/build.rs index 3fc1062a7..1ee4ea1c4 100644 --- a/crates/doc/src/build.rs +++ b/crates/doc/src/build.rs @@ -176,6 +176,7 @@ impl Document { locked: false, printable: true, show_guides: true, + lock_guides: false, suppress_wrap_when_hidden: false, }); // A-Parent. diff --git a/crates/doc/src/edit.rs b/crates/doc/src/edit.rs index cadc3d419..628fc8ee6 100644 --- a/crates/doc/src/edit.rs +++ b/crates/doc/src/edit.rs @@ -28,6 +28,22 @@ pub struct ItemLoc { pub path: ItemPath, } +/// A gap between adjacent items (or between an item and the page edge) on a spread. +#[derive(Clone, Debug, PartialEq)] +pub struct Gap { + /// True for a vertical gap (between left and right neighbours, moving along X); + /// false for a horizontal gap (between top and bottom neighbours, moving along Y). + pub vertical: bool, + /// Leading edge of the gap along its movement axis. + pub a: f64, + /// Trailing edge of the gap along its movement axis. + pub z: f64, + /// Items ending at `a` that resize when the gap moves. + pub before: Vec, + /// Items starting at `z` that resize when the gap moves. + pub after: Vec, +} + impl ItemLoc { /// Top-level index in the spread. pub fn top(&self) -> usize { @@ -648,6 +664,38 @@ impl Document { .map(|it| it.id) } + /// The gap at `p` (spread space) on spread `sr`: between left and right neighbours (vertical + /// gap moving along X), or above and below (horizontal gap moving along Y). + pub fn find_gap(&self, sr: SpreadRef, p: Point) -> Option { + let sp = self.spread(sr)?; + let page = sp.page_at_x(p.x).and_then(|i| sp.pages.get(i)).map(|pg| pg.bounds())?; + let items: Vec<(ItemId, Rect)> = sp + .items + .iter() + .filter(|it| !it.hidden && !it.locked && self.layer(it.layer).is_none_or(|l| !l.locked && l.visible)) + .map(|it| (it.id, it.bounds())) + .collect(); + if items.iter().any(|(_, b)| b.contains(p)) { + return None; + } + // Between left and right neighbours (a vertical gap), or above and below (horizontal). + let side = |vertical: bool| { + let (pos, lo, hi) = if vertical { (p.x, page.x0, page.x1) } else { (p.y, page.y0, page.y1) }; + let across = |b: &Rect| if vertical { b.y0 <= p.y && p.y <= b.y1 } else { b.x0 <= p.x && p.x <= b.x1 }; + let end = |b: &Rect| if vertical { b.x1 } else { b.y1 }; + let start = |b: &Rect| if vertical { b.x0 } else { b.y0 }; + let a = items.iter().filter(|(_, b)| across(b) && end(b) <= pos).map(|(_, b)| end(b)).fold(lo, f64::max); + let z = items.iter().filter(|(_, b)| across(b) && start(b) >= pos).map(|(_, b)| start(b)).fold(hi, f64::min); + let before: Vec = items.iter().filter(|(_, b)| across(b) && (end(b) - a).abs() < 0.5).map(|(i, _)| *i).collect(); + let after: Vec = items.iter().filter(|(_, b)| across(b) && (start(b) - z).abs() < 0.5).map(|(i, _)| *i).collect(); + (!before.is_empty() || !after.is_empty()).then_some(Gap { vertical, a, z, before, after }) + }; + match (side(true), side(false)) { + (Some(v), Some(h)) => Some(if v.z - v.a <= h.z - h.a { v } else { h }), + (v, h) => v.or(h), + } + } + /// The first item on a document or parent spread whose geometry isn't finite /// ([`Item::geometry_is_finite`]). pub fn non_finite_item(&self) -> Option { diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index f4c8a25b6..338f122f4 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -12,6 +12,7 @@ pub mod anchored; pub mod arabic; pub mod arrow; +pub mod asset_data; pub mod assets; pub mod attrs; pub mod build; @@ -44,6 +45,7 @@ use std::collections::BTreeMap; use std::sync::Arc; pub use anchored::{AnchorPosition, AnchoredObject, OBJECT_MARK}; +pub use asset_data::{AssetData, LinkedSource, SourceKey, Unavailable, WeakAssetData}; pub use assets::{AssetIndex, find_same_asset, insert_asset, remap_story_assets, story_asset_refs}; pub use attrs::*; pub use datamerge::{ @@ -52,7 +54,7 @@ pub use datamerge::{ }; pub use designcraft_color as color; pub use designcraft_geom as geom; -pub use edit::{ItemLoc, ItemPath, SpreadRef, hit_descendant, item_hit as edit_hit}; +pub use edit::{Gap, ItemLoc, ItemPath, SpreadRef, hit_descendant, item_hit as edit_hit}; pub use endnotes::{ENDNOTE_REF, EditorialNote, EndnoteOptions, NOTE_MARK}; pub use file_info::FileInfo; pub use flex::{FlexAlign, FlexDirection, FlexJustify, FlexLayout, FlexSize}; @@ -575,6 +577,9 @@ pub struct Layer { pub printable: bool, #[serde(default = "yes")] pub show_guides: bool, + /// Lock Guides: the layer's ruler guides can't be moved or deleted. + #[serde(default)] + pub lock_guides: bool, #[serde(default)] pub suppress_wrap_when_hidden: bool, } @@ -620,8 +625,20 @@ pub struct Asset { /// clears them. #[serde(default, skip_serializing_if = "Option::is_none")] pub link_fingerprint: Option, + /// `link` relative to the document's folder, for finding the file after both moved. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub link_relative: Option, + /// The linked file's content hash (`sha256:` and hex digits). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub link_hash: Option, + /// Whether a save writes the bytes into the document or only the link to them. + #[serde(default, skip_serializing_if = "Storage::is_embedded")] + pub storage: Storage, + /// A small preview drawn while the full file can't be read. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub proxy: Option, #[serde(skip)] - pub data: Arc>, + pub data: AssetData, /// Pixel size for raster images. #[serde(default, skip_serializing_if = "Option::is_none")] pub pixels: Option<(u32, u32)>, @@ -639,7 +656,42 @@ pub struct Asset { pub pdf_box: Option<[f64; 4]>, } +/// An asset's bytes for output ([`Asset::output_bytes`]) and what to tell the user about them. +pub struct OutputBytes { + pub bytes: std::sync::Arc>, + /// A warning to report: the file on disk replaced a changed original, or the proxy stands in + /// for a missing one. + pub warning: Option, + /// The bytes are the asset's preview, not the original (a missing or unreadable link). + pub proxied: bool, +} + impl Asset { + /// The bytes an export uses: the original (a changed linked file's current bytes, with a + /// warning), or for a missing or unreadable one its proxy, with a warning. `Err` when + /// there is neither. + pub fn output_bytes(&self) -> std::result::Result { + match self.data.load_for_output() { + Ok((bytes, false)) => Ok(OutputBytes { bytes, warning: None, proxied: false }), + Ok((bytes, true)) => Ok(OutputBytes { + bytes, + warning: Some(format!("the linked file `{}` has changed; the file on disk is used", self.name)), + proxied: false, + }), + Err(e) => { + let proxy = self.proxy.as_ref().and_then(|p| p.data.load().ok()); + match proxy { + Some(bytes) => Ok(OutputBytes { + bytes, + warning: Some(format!("the linked file `{}` is unavailable ({e}); its preview is used", self.name)), + proxied: true, + }), + None => Err(e), + } + } + } + } + /// The part of the page a placed PDF shows, when [`Asset::pdf_box`] holds a usable box. pub fn shown_box(&self) -> Option<[f64; 4]> { self.shown(self.pdf_box?) @@ -660,6 +712,40 @@ impl Asset { } } +/// Where a save keeps an asset's bytes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum Storage { + /// In the document. + #[default] + Embedded, + /// In the linked file only. + Linked, +} + +impl Storage { + fn is_embedded(&self) -> bool { + *self == Storage::Embedded + } +} + +/// A linked asset's preview image. Its bytes are stored next to the JSON, like asset bytes. +#[derive(Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Proxy { + pub mime: String, + pub pixels: (u32, u32), + /// The preview's encoded bytes, under a key of their own (decoded images are cached by it). + #[serde(skip)] + pub data: AssetData, +} + +impl std::fmt::Debug for Proxy { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Proxy").field("mime", &self.mime).field("pixels", &self.pixels).field("len", &self.data.len()).finish() + } +} + /// Image Import Options › Crop to: which box of a PDF page a placed PDF shows. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -717,9 +803,8 @@ pub struct AlphaOutline { /// The boundaries of transparent holes those areas enclose (wound the other way). pub holes: designcraft_geom::PathData, /// The image data it was traced from: a relink or link update replaces the asset's data - /// under the same id, and the outline is traced again. (Weak, so it doesn't keep old image - /// data alive, and its address can't be reused by new data while it's held.) - pub source: std::sync::Weak>, + /// under the same id, and the outline is traced again. + pub source: SourceKey, /// A number no other outline traced in this process has ([`AlphaOutline::next_serial`]): /// composition caches compare it to tell a new outline from the one they used. pub serial: u64, @@ -733,8 +818,8 @@ impl AlphaOutline { } /// Traced from `data`, the asset's current image data. - pub fn traced_from(&self, data: &Arc>) -> bool { - std::ptr::eq(self.source.as_ptr(), Arc::as_ptr(data)) + pub fn traced_from(&self, data: &AssetData) -> bool { + self.source == data.key() } } @@ -743,7 +828,7 @@ impl PartialEq for AlphaOutline { (self.asset, self.threshold, self.width, self.height) == (other.asset, other.threshold, other.width, other.height) && self.outer == other.outer && self.holes == other.holes - && self.source.ptr_eq(&other.source) + && self.source == other.source } } @@ -873,6 +958,15 @@ pub enum HyperlinkDest { Anchor(u64), } +/// Whether a URL may be used as an external link. Only web and mail schemes are allowed: a +/// `file:`, `javascript:` or UNC link can make a recipient's PDF viewer contact an attacker. +pub fn is_safe_link_url(url: &str) -> bool { + let Some((scheme, rest)) = url.split_once(':') else { return false }; + !rest.is_empty() + && !url.chars().any(|c| c.is_control() || c.is_whitespace()) + && matches!(scheme.to_ascii_lowercase().as_str(), "http" | "https" | "mailto") +} + impl HyperlinkDest { /// The name a hyperlink is given when none is supplied. pub fn label(&self) -> String { diff --git a/crates/doc/src/page.rs b/crates/doc/src/page.rs index adac7ac0d..d6e9b1302 100644 --- a/crates/doc/src/page.rs +++ b/crates/doc/src/page.rs @@ -153,9 +153,9 @@ impl Guide { pub fn visible_in(&self, d: &crate::Document) -> bool { self.layer.is_none_or(|l| d.layer(l).is_none_or(|l| l.visible && l.show_guides)) } - /// Can be dragged: neither it nor its layer is locked. + /// Can be dragged: neither it nor its layer is locked, nor are the layer's guides. pub fn editable_in(&self, d: &crate::Document) -> bool { - !self.locked && self.layer.is_none_or(|l| d.layer(l).is_none_or(|l| !l.locked && l.visible && l.show_guides)) + !self.locked && self.layer.is_none_or(|l| d.layer(l).is_none_or(|l| !l.locked && !l.lock_guides && l.visible && l.show_guides)) } } diff --git a/crates/doc/src/swatch_refs.rs b/crates/doc/src/swatch_refs.rs index b3a885f89..2c9af65b8 100644 --- a/crates/doc/src/swatch_refs.rs +++ b/crates/doc/src/swatch_refs.rs @@ -174,6 +174,7 @@ impl Names<'_> { for s in &mut c.strokes { self.cell_stroke(s); } + self.cell_stroke(&mut c.diagonal.stroke); } } /// One story's own text, objects and tables (cell and note text are visited separately). @@ -239,8 +240,8 @@ impl Document { /// Point every reference to swatch `from` at `to`: tints based on it, colour groups, inks, /// styles, page items (fills, strokes, gaps, effects, column rules, the swatch an image is /// coloured with), text, kenten and ruby colours, rules, shading, borders and border gaps, - /// tables (with stroke gaps and cell images), footnotes, gradient stops, endnotes and the - /// footnote rule. The swatch itself is renamed by the caller. + /// tables (with stroke gaps, diagonal lines and cell images), footnotes, gradient stops, + /// endnotes and the footnote rule. The swatch itself is renamed by the caller. pub fn rename_swatch_refs(&mut self, from: &str, to: &str) { if from == to { return; diff --git a/crates/doc/src/table.rs b/crates/doc/src/table.rs index 4c726b743..1926cf1df 100644 --- a/crates/doc/src/table.rs +++ b/crates/doc/src/table.rs @@ -22,7 +22,7 @@ mod grid; mod merge; mod story; -pub use cell::{AltFills, AltStrokes, Cell, CellStroke, Column, Row, RowHeightMode, RowKind, TableOptions}; +pub use cell::{AltFills, AltStrokes, Cell, CellDiagonal, CellStroke, Column, DiagonalLines, Row, RowHeightMode, RowKind, TableOptions}; pub use grid::CellRange; /// Most rows a table holds, as InDesign allows. @@ -366,4 +366,27 @@ mod tests { assert_eq!(back, explicit); assert_eq!(back.blank_like().border_overrides, explicit.border_overrides); } + + #[test] + fn cell_diagonal_lines_roundtrip_and_run_corner_to_corner() { + let plain = serde_json::to_value(Cell::default()).unwrap(); + assert!(plain.get("diagonal").is_none(), "a cell without diagonal lines saves none"); + let diagonal = CellDiagonal { lines: DiagonalLines::Crossed, stroke: CellStroke { weight: 2.0, ..Default::default() }, in_front: false }; + let cell = Cell { diagonal: diagonal.clone(), ..Cell::default() }; + let back: Cell = serde_json::from_str(&serde_json::to_string(&cell).unwrap()).unwrap(); + assert_eq!(back.diagonal, diagonal); + assert_eq!(back.blank_like().diagonal, diagonal, "new cells take the cell's formatting"); + let r = designcraft_geom::Rect::new(10.0, 20.0, 110.0, 70.0); + let p = designcraft_geom::Point::new; + let down = (p(10.0, 20.0), p(110.0, 70.0)); + let up = (p(110.0, 20.0), p(10.0, 70.0)); + for (lines, want) in [ + (DiagonalLines::None, vec![]), + (DiagonalLines::TopLeft, vec![down]), + (DiagonalLines::TopRight, vec![up]), + (DiagonalLines::Crossed, vec![down, up]), + ] { + assert_eq!(CellDiagonal { lines, ..Default::default() }.segments(r), want, "{lines:?}"); + } + } } diff --git a/crates/doc/src/table/cell.rs b/crates/doc/src/table/cell.rs index ac022a7a8..628892af6 100644 --- a/crates/doc/src/table/cell.rs +++ b/crates/doc/src/table/cell.rs @@ -74,6 +74,55 @@ impl Default for CellStroke { } } +/// Cell Options › Diagonal Lines: the diagonals drawn across a cell. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum DiagonalLines { + #[default] + None, + /// From the top left corner to the bottom right corner. + TopLeft, + /// From the top right corner to the bottom left corner. + TopRight, + /// Both diagonals. + Crossed, +} + +/// A cell's diagonal lines, their stroke, and whether they are drawn in front of the cell's +/// contents (Draw › Diagonal In Front) or behind them (Content In Front). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct CellDiagonal { + pub lines: DiagonalLines, + pub stroke: CellStroke, + pub in_front: bool, +} + +impl Default for CellDiagonal { + fn default() -> Self { + CellDiagonal { lines: DiagonalLines::None, stroke: CellStroke::default(), in_front: true } + } +} + +impl CellDiagonal { + pub fn is_default(&self) -> bool { + *self == CellDiagonal::default() + } + + /// The diagonals' ends in the cell rectangle `r`, as (start, end) pairs. + pub fn segments(&self, r: designcraft_geom::Rect) -> Vec<(designcraft_geom::Point, designcraft_geom::Point)> { + use designcraft_geom::Point; + let down = (Point::new(r.x0, r.y0), Point::new(r.x1, r.y1)); + let up = (Point::new(r.x1, r.y0), Point::new(r.x0, r.y1)); + match self.lines { + DiagonalLines::None => vec![], + DiagonalLines::TopLeft => vec![down], + DiagonalLines::TopRight => vec![up], + DiagonalLines::Crossed => vec![down, up], + } + } +} + impl CellStroke { pub fn none() -> Self { CellStroke { weight: 0.0, ..Default::default() } @@ -207,6 +256,9 @@ pub struct Cell { /// content box, origin at the box's top left). #[serde(default, skip_serializing_if = "Option::is_none")] pub graphic: Option, + /// Cell Options › Diagonal Lines. + #[serde(default, skip_serializing_if = "CellDiagonal::is_default")] + pub diagonal: CellDiagonal, } impl Default for Cell { @@ -228,6 +280,7 @@ impl Default for Cell { stroke_defined: [false; 4], style: String::new(), graphic: None, + diagonal: CellDiagonal::default(), } } } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 6b6a43a87..f3dce660f 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -13,6 +13,7 @@ workspace = true testing = [] [dependencies] +craft-mime = { workspace = true } craft-print = { workspace = true } craft-docpath = { workspace = true } designcraft-images = { workspace = true } @@ -30,14 +31,20 @@ designcraft-pdf = { workspace = true } craft-prepress = { workspace = true } designcraft-tools = { workspace = true } designcraft-idml = { workspace = true } -indd = { workspace = true } +craft-file-formats = { workspace = true } +craft-csv = { workspace = true } +craft-indd = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } log = { workspace = true } -quick-xml = { workspace = true } +craft-xml = { workspace = true } +craft-xml-dtd = { workspace = true } +craft-svg = { workspace = true } caseless = { workspace = true } unicode-segmentation = { workspace = true } +# Content hashes of linked files (`asset_io::content_hash`). +sha2 = "0.10" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] craft-atomic = { workspace = true } diff --git a/crates/engine/src/asset_io.rs b/crates/engine/src/asset_io.rs new file mode 100644 index 000000000..1f3b917e5 --- /dev/null +++ b/crates/engine/src/asset_io.rs @@ -0,0 +1,322 @@ +//! Reading linked files' bytes for the document model ([`designcraft_doc::asset_data`]): the +//! [`Loader`] every [`crate::Session`] installs. Files are read through [`crate::io`] (the +//! automation roots of the loading thread apply) and only ever yield the bytes the link record +//! names: other bytes at the path are [`Unavailable::Modified`] and are never handed out. +//! +//! The web build installs no loader: its linked assets are [`Unavailable::NoLoader`]. + +use std::path::{Path, PathBuf}; + +use designcraft_doc::asset_data::{Loaded, Loader, MAX_LINKED_BYTES}; +use designcraft_doc::{Fingerprint, LinkedSource, Unavailable}; + +/// Read linked files with [`FileLoader`] in this process (on the web: nothing to install). +pub fn install() { + #[cfg(not(target_arch = "wasm32"))] + designcraft_doc::asset_data::install_loader(std::sync::Arc::new(FileLoader)); +} + +/// The engine's [`Loader`]: [`read_linked`]. +pub struct FileLoader; + +impl Loader for FileLoader { + fn load(&self, source: &LinkedSource, trusted: Option) -> Result { + read_linked(source, trusted) + } + + fn load_current(&self, source: &LinkedSource) -> Result, Unavailable> { + read_current(source) + } + + fn holds(&self, source: &LinkedSource, trusted: Option, kept: &[u8]) -> Result, Unavailable> { + still_holds(source, trusted, kept) + } +} + +/// Whether a candidate file ([`candidates`]) still holds `kept`, the bytes read for `source` +/// earlier ([`Loader::holds`]): one of `source.len` bytes with the recorded or `trusted` +/// fingerprint (nothing is read), else one whose content hash is `source.hash`, or without a +/// hash whose bytes are `kept`. → the fingerprint of a file that was read to tell. +pub fn still_holds(source: &LinkedSource, trusted: Option, kept: &[u8]) -> Result, Unavailable> { + let mut seen = false; + for path in candidates(source) { + let Some(meta) = crate::io::file_metadata(&path) else { continue }; + seen = true; + let found = fingerprint_from(&meta); + if found.size != source.len { + continue; + } + if has_time(&found) && (Some(found) == source.fingerprint || Some(found) == trusted) { + return Ok(None); + } + let Ok(bytes) = crate::io::read_capped(&path, source.len) else { continue }; + let same = match &source.hash { + Some(hash) => *hash == content_hash(&bytes), + None => bytes == kept, + }; + if same { + return Ok(has_time(&found).then_some(found)); + } + } + Err(if seen { Unavailable::Modified } else { Unavailable::Missing }) +} + +/// The bytes of the first candidate file that exists, whatever they are (no fingerprint or hash +/// check): what an export uses for a link whose file has changed. +pub fn read_current(source: &LinkedSource) -> Result, Unavailable> { + for path in candidates(source) { + let Some(meta) = crate::io::file_metadata(&path) else { continue }; + if meta.len() > MAX_LINKED_BYTES { + return Err(Unavailable::TooLarge); + } + if let Ok(bytes) = crate::io::read_capped(&path, MAX_LINKED_BYTES) { + return Ok(bytes); + } + } + Err(Unavailable::Missing) +} + +/// `sha256:` and the hex digits of the SHA-256 of `bytes` ([`designcraft_doc::Asset::link_hash`]). +pub fn content_hash(bytes: &[u8]) -> String { + use sha2::Digest; + use std::fmt::Write; + let digest = sha2::Sha256::digest(bytes); + let mut out = String::with_capacity(7 + 64); + out.push_str("sha256:"); + for b in digest { + let _ = write!(out, "{b:02x}"); + } + out +} + +/// Size and modification time (whole seconds) from a file's metadata, as link records store them. +pub fn fingerprint_from(meta: &crate::io::Metadata) -> Fingerprint { + let mtime = meta + .modified() + .ok() + .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()) + .map(|d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX)) + .unwrap_or(0); + Fingerprint { size: meta.len(), mtime } +} + +/// Whether `f` has a modification time to tell the file by: [`fingerprint_from`] gives 0 where +/// the file system reports none, and two such files of one size would look the same. +pub fn has_time(f: &Fingerprint) -> bool { + f.mtime != 0 +} + +/// The files that may hold `source`'s bytes, in the order they are tried: the absolute path, +/// then the relative one resolved against the document. Paths [`crate::io::document_path`] +/// refuses are left out. +pub fn candidates(source: &LinkedSource) -> Vec { + let document = source.document.as_deref(); + let mut out: Vec = crate::io::document_path(&source.path, document).into_iter().collect(); + if let (Some(relative), Some(document)) = (source.relative.as_deref(), document) + && let Ok(p) = crate::io::document_path(relative, Some(document)) + && !out.contains(&p) + { + out.push(p); + } + out +} + +/// Why one candidate file doesn't serve. +enum Miss { + Absent, + Modified, + TooLarge, +} + +/// The bytes of the first candidate file ([`candidates`]) that holds `source`'s bytes: one of +/// `source.len` bytes whose fingerprint is `source.fingerprint` or `trusted`, or whose content +/// hash is `source.hash`. A source that records neither fingerprint nor hash is taken by its +/// size. The fingerprint of a file accepted by its hash is returned with the bytes. +pub fn read_linked(source: &LinkedSource, trusted: Option) -> Result { + if source.len > MAX_LINKED_BYTES { + return Err(Unavailable::TooLarge); + } + let mut worst = Miss::Absent; + for path in candidates(source) { + match read_candidate(&path, source, trusted) { + Ok(loaded) => return Ok(loaded), + Err(Miss::Absent) => {} + Err(miss @ Miss::TooLarge) => worst = miss, + Err(Miss::Modified) => { + if !matches!(worst, Miss::TooLarge) { + worst = Miss::Modified; + } + } + } + } + Err(match worst { + Miss::Absent => Unavailable::Missing, + Miss::Modified => Unavailable::Modified, + Miss::TooLarge => Unavailable::TooLarge, + }) +} + +fn read_candidate(path: &Path, source: &LinkedSource, trusted: Option) -> Result { + let meta = crate::io::file_metadata(path).ok_or(Miss::Absent)?; + let found = fingerprint_from(&meta); + if found.size > MAX_LINKED_BYTES { + return Err(Miss::TooLarge); + } + if found.size != source.len { + return Err(Miss::Modified); + } + let known = has_time(&found) && (Some(found) == source.fingerprint || Some(found) == trusted); + if !known && source.hash.is_none() && source.fingerprint.is_some() { + // Nothing to tell the bytes by. + return Err(Miss::Modified); + } + // At most one byte past the size: a file that grew since is caught without reading it all. + let bytes = crate::io::read_capped(path, source.len).map_err(|_| Miss::Absent)?; + if bytes.len() as u64 != source.len { + return Err(Miss::Modified); + } + if known { + return Ok(Loaded { bytes, fingerprint: None }); + } + match &source.hash { + Some(hash) if *hash != content_hash(&bytes) => Err(Miss::Modified), + Some(_) => Ok(Loaded { bytes, fingerprint: Some(found) }), + None => Ok(Loaded { bytes, fingerprint: None }), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use designcraft_doc::AssetData; + + struct Dir(PathBuf); + + impl Dir { + fn new(name: &str) -> Self { + let dir = std::env::temp_dir().join(format!("dc-asset-io-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + Dir(dir) + } + } + + impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + + /// The link record of the file at `path` as it is now. + fn source_of(path: &Path) -> LinkedSource { + let bytes = std::fs::read(path).unwrap(); + LinkedSource { + path: path.to_string_lossy().into_owned(), + relative: None, + document: None, + len: bytes.len() as u64, + fingerprint: Some(fingerprint_from(&std::fs::metadata(path).unwrap())), + hash: Some(content_hash(&bytes)), + } + } + + /// Set the file's modification time `secs` seconds into the past. + fn age(path: &Path, secs: u64) { + let file = std::fs::OpenOptions::new().write(true).open(path).unwrap(); + file.set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(secs)).unwrap(); + } + + #[test] + fn content_hashes_are_sha256_hex() { + assert_eq!(content_hash(b"abc"), "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); + } + + #[test] + fn a_linked_file_is_read_when_it_holds_the_recorded_bytes() { + let dir = Dir::new("read"); + let path = dir.0.join("a.png"); + std::fs::write(&path, b"original").unwrap(); + let source = source_of(&path); + assert_eq!(read_linked(&source, None).ok().map(|l| (l.bytes, l.fingerprint)), Some((b"original".to_vec(), None))); + // Same bytes, another time: accepted by the hash, and the new fingerprint returned. + age(&path, 1000); + let now = fingerprint_from(&std::fs::metadata(&path).unwrap()); + let loaded = read_linked(&source, None).ok().unwrap(); + assert_eq!((loaded.bytes.as_slice(), loaded.fingerprint), (b"original".as_slice(), Some(now))); + assert!(read_linked(&source, Some(now)).is_ok_and(|l| l.fingerprint.is_none()), "a trusted fingerprint needs no hash"); + let unverifiable = LinkedSource { hash: None, ..source.clone() }; + assert_eq!(read_linked(&unverifiable, None).err(), Some(Unavailable::Modified), "nothing to tell the bytes by"); + } + + #[test] + fn other_bytes_at_the_path_are_modified_and_a_missing_file_is_missing() { + let dir = Dir::new("modified"); + let path = dir.0.join("a.png"); + std::fs::write(&path, b"original").unwrap(); + let source = source_of(&path); + std::fs::write(&path, b"changed!").unwrap(); + age(&path, 1000); + assert_eq!(read_linked(&source, None).err(), Some(Unavailable::Modified), "same size, other bytes"); + std::fs::write(&path, b"longer than before").unwrap(); + assert_eq!(read_linked(&source, None).err(), Some(Unavailable::Modified), "another size"); + std::fs::remove_file(&path).unwrap(); + assert_eq!(read_linked(&source, None).err(), Some(Unavailable::Missing)); + let too_large = LinkedSource { len: MAX_LINKED_BYTES + 1, ..source }; + assert_eq!(read_linked(&too_large, None).err(), Some(Unavailable::TooLarge)); + } + + #[test] + fn the_relative_path_serves_when_the_absolute_one_does_not() { + let dir = Dir::new("relative"); + std::fs::create_dir_all(dir.0.join("moved/links")).unwrap(); + let away = dir.0.join("moved/links/a.png"); + std::fs::write(&away, b"original").unwrap(); + let mut source = source_of(&away); + source.path = dir.0.join("gone/links/a.png").to_string_lossy().into_owned(); + assert_eq!(read_linked(&source, None).err(), Some(Unavailable::Missing)); + source.relative = Some("links/a.png".into()); + source.document = Some(dir.0.join("moved/doc.dcx")); + assert_eq!(read_linked(&source, None).ok().map(|l| l.bytes), Some(b"original".to_vec())); + // A changed file at the absolute path doesn't hide the one that holds the bytes. + std::fs::create_dir_all(dir.0.join("gone/links")).unwrap(); + std::fs::write(dir.0.join("gone/links/a.png"), b"other!!!").unwrap(); + age(&dir.0.join("gone/links/a.png"), 1000); + assert_eq!(read_linked(&source, None).ok().map(|l| l.bytes), Some(b"original".to_vec())); + } + + /// A time of 0 is what a file system without times gives: it can't tell two files apart. + #[test] + fn a_file_without_a_time_is_told_by_its_hash() { + let dir = Dir::new("no-time"); + let path = dir.0.join("a.png"); + let at_epoch = |p: &Path| std::fs::OpenOptions::new().write(true).open(p).unwrap().set_modified(std::time::UNIX_EPOCH).unwrap(); + std::fs::write(&path, b"original").unwrap(); + at_epoch(&path); + let source = source_of(&path); + assert_eq!(source.fingerprint.map(|f| f.mtime), Some(0)); + std::fs::write(&path, b"changed!").unwrap(); + at_epoch(&path); + assert_eq!(read_linked(&source, None).err(), Some(Unavailable::Modified)); + } + + /// Through the installed loader and the store: a file changed on disk is never adopted. + #[test] + fn asset_data_loads_through_the_installed_loader() { + install(); + let dir = Dir::new("installed"); + let path = dir.0.join("a.png"); + std::fs::write(&path, b"original").unwrap(); + let data = AssetData::linked(source_of(&path)); + let changed = AssetData::linked(source_of(&path)); + assert_eq!(data.load().ok().map(|b| b.to_vec()), Some(b"original".to_vec())); + assert!(data.resident().is_some()); + std::fs::write(&path, b"changed!").unwrap(); + age(&path, 1000); + assert_eq!(data.load().ok().map(|b| b.to_vec()), Some(b"original".to_vec()), "kept bytes are the recorded ones"); + assert_eq!(changed.load().err(), Some(Unavailable::Modified)); + assert!(changed.resident().is_none(), "nothing adopted"); + std::fs::remove_file(&path).unwrap(); + data.evict(); + assert_eq!(data.load().err(), Some(Unavailable::Missing)); + } +} diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 74768e86b..803814d7e 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -34,6 +34,7 @@ mod layout; pub mod library; mod linked; mod links; +pub mod native_links; pub use links::{LinkListChecks, list_document_links, preview_document_links, remember_link_files}; mod liquid; mod lists; @@ -201,6 +202,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(library::specs()); v.extend(lists::specs()); v.extend(linked::specs()); + v.extend(native_links::specs()); v.extend(mathexpr::specs()); v.extend(media::specs()); v.extend(pdflayers::specs()); @@ -254,6 +256,11 @@ pub fn find_command(id: &str) -> Option<&'static CommandSpec> { pub(crate) fn bad(cmd: &str, msg: impl Into) -> EngineError { EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } } + +/// The bytes of `asset`, or why `cmd` can't have them. +pub(crate) fn asset_bytes(cmd: &str, asset: &designcraft_doc::Asset) -> Result>> { + asset.data.load().map_err(|e| bad(cmd, format!("{}: {e}", asset.name))) +} /// `p` with `key` set to `v`. Parameters come from callers (control channel, MCP, scripts) and /// may not be an object: `p[key] = v` panics on an array or a string, so start from `{}` then. pub(crate) fn with_param(p: &Value, key: &str, v: Value) -> Value { diff --git a/crates/engine/src/cmd/alpha_wrap.rs b/crates/engine/src/cmd/alpha_wrap.rs index ea9801f79..de092273d 100644 --- a/crates/engine/src/cmd/alpha_wrap.rs +++ b/crates/engine/src/cmd/alpha_wrap.rs @@ -6,7 +6,7 @@ use std::collections::BTreeSet; use std::sync::Arc; -use designcraft_doc::{AlphaOutline, AssetId, ContourType, Document, Item, WrapMode}; +use designcraft_doc::{AlphaOutline, AssetData, AssetId, ContourType, Document, Item, WrapMode}; use designcraft_geom::{BezPath, PathData, Point}; /// Images larger than this many pixels aren't traced (their contour follows the frame). @@ -57,7 +57,15 @@ pub(crate) fn sync(doc: &mut Document) { let Some(a) = doc.assets.get(&asset).filter(|a| a.is_raster()) else { continue }; // An image with no outline (unreadable, too large) is remembered as an empty // one, so later edits don't decode it again; the contour is then the frame's. - let o = trace(&a.data, asset, threshold).unwrap_or_else(|| no_outline(&a.data, asset, threshold)); + // A linked image's original is not read here (a document opens without reading its + // links): its preview is traced, scaled to the original's size; with no preview, the + // image has no outline yet and is traced once its bytes are resident. + let o = if a.data.is_linked() && a.data.resident().is_none() { + let Some(proxy) = a.proxy.as_ref().and_then(|p| Some((p.data.load().ok()?, a.pixels?))) else { continue }; + trace_bytes(&proxy.0, a.data.key(), Some(proxy.1), asset, threshold).unwrap_or_else(|| no_outline(&a.data, asset, threshold)) + } else { + trace(&a.data, asset, threshold).unwrap_or_else(|| no_outline(&a.data, asset, threshold)) + }; doc.alpha_outlines.push(Arc::new(o)); } // `Document::alpha_outline` finds an outline by binary search. @@ -68,7 +76,7 @@ pub(crate) fn sync(doc: &mut Document) { /// The empty outline standing for an image `trace` found none in: zero-size, which composition /// takes as no outline, and dropped like a traced one when the asset's data changes. -fn no_outline(data: &Arc>, asset: AssetId, threshold: u8) -> AlphaOutline { +fn no_outline(data: &AssetData, asset: AssetId, threshold: u8) -> AlphaOutline { AlphaOutline { asset, threshold, @@ -76,19 +84,25 @@ fn no_outline(data: &Arc>, asset: AssetId, threshold: u8) -> AlphaOutlin height: 0, outer: Default::default(), holes: Default::default(), - source: Arc::downgrade(data), + source: data.key(), serial: AlphaOutline::next_serial(), } } /// The outline of the pixels of `data` more opaque than `threshold`: an image with no /// transparency gives its rectangle. None for an image that can't be read or is too large. -pub(crate) fn trace(data: &Arc>, asset: AssetId, threshold: u8) -> Option { - let (w, h) = designcraft_images::pixel_size(data)?; +pub(crate) fn trace(data: &AssetData, asset: AssetId, threshold: u8) -> Option { + trace_bytes(&data.load().ok()?, data.key(), None, asset, threshold) +} + +/// [`trace`] of encoded `bytes` standing for the data under `source`. With `size`, the outline +/// is scaled to an image of that size (a preview traced for the original it stands for). +fn trace_bytes(bytes: &[u8], source: designcraft_doc::SourceKey, size: Option<(u32, u32)>, asset: AssetId, threshold: u8) -> Option { + let (w, h) = designcraft_images::pixel_size(bytes)?; if w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_PIXELS { return None; } - let img = designcraft_images::decode_rgba(data)?; + let img = designcraft_images::decode_rgba(bytes)?; if img.width() != w || img.height() != h { return None; } @@ -107,7 +121,8 @@ pub(crate) fn trace(data: &Arc>, asset: AssetId, threshold: u8) -> Optio outer.iter().chain(holes).map(|r| r.len() + 1).sum::() <= designcraft_compose::MAX_CONTOUR_POINTS }; if let Some((outer, holes)) = rings(&mask, cols as usize, rows as usize).filter(|(o, h)| fits(o, h)) { - let (sx, sy) = (f64::from(w) / f64::from(cols), f64::from(h) / f64::from(rows)); + let (tw, th) = size.filter(|&(a, b)| a > 0 && b > 0).unwrap_or((w, h)); + let (sx, sy) = (f64::from(tw) / f64::from(cols), f64::from(th) / f64::from(rows)); let path = |rings: &[Vec<(i32, i32)>]| { let mut p = BezPath::new(); for ring in rings { @@ -121,9 +136,8 @@ pub(crate) fn trace(data: &Arc>, asset: AssetId, threshold: u8) -> Optio } PathData::from_bezpath(&p) }; - let source = Arc::downgrade(data); let serial = AlphaOutline::next_serial(); - return Some(AlphaOutline { asset, threshold, width: w, height: h, outer: path(&outer), holes: path(&holes), source, serial }); + return Some(AlphaOutline { asset, threshold, width: tw, height: th, outer: path(&outer), holes: path(&holes), source, serial }); } finer = Some((mask, cols, rows)); grid /= 2; @@ -273,7 +287,7 @@ mod tests { #[test] fn a_fragmented_outline_is_traced_coarser_until_composition_can_follow_it() { let dots = png_sized(256, |x, y| x % 4 == 0 && y % 4 == 0); - let o = trace(&Arc::new(dots), AssetId(1), 25).unwrap(); + let o = trace(&AssetData::embedded(dots), AssetId(1), 25).unwrap(); let elements = o.outer.to_bezpath().elements().len() + o.holes.to_bezpath().elements().len(); assert!(elements > 0 && elements <= designcraft_compose::MAX_CONTOUR_POINTS, "{elements} path elements"); } @@ -296,14 +310,14 @@ mod tests { fn traces_outer_boundaries_and_holes_apart() { // A ring: a square with a square hole. let data = png(|x, y| (8..56).contains(&x) && (8..56).contains(&y) && !((24..40).contains(&x) && (24..40).contains(&y))); - let o = trace(&Arc::new(data), AssetId(1), 25).unwrap(); + let o = trace(&AssetData::embedded(data), AssetId(1), 25).unwrap(); assert_eq!((o.width, o.height), (64, 64)); assert_eq!((o.outer.subpaths.len(), o.holes.subpaths.len()), (1, 1)); let (a, b) = (o.outer.bounds().unwrap(), o.holes.bounds().unwrap()); assert_eq!((a.x0, a.y0, a.x1, a.y1), (8.0, 8.0, 56.0, 56.0)); assert_eq!((b.x0, b.y0, b.x1, b.y1), (24.0, 24.0, 40.0, 40.0)); // An opaque image's outline is its rectangle. - let o = trace(&Arc::new(png(|_, _| true)), AssetId(1), 25).unwrap(); + let o = trace(&AssetData::embedded(png(|_, _| true)), AssetId(1), 25).unwrap(); let a = o.outer.bounds().unwrap(); assert_eq!((a.x0, a.y0, a.x1, a.y1, o.holes.subpaths.len()), (0.0, 0.0, 64.0, 64.0, 0)); } @@ -432,7 +446,7 @@ mod tests { else { panic!("a graphic") }; - let narrower = Arc::new(png(|x, _| (24..40).contains(&x))); + let narrower = AssetData::embedded(png(|x, _| (24..40).contains(&x))); s.edit(|d, _| { let a = d.assets.get(&g.asset).unwrap(); let mut a = (**a).clone(); @@ -496,7 +510,7 @@ mod tests { else { panic!("a graphic") }; - let right = Arc::new(png(|x, _| x >= 32)); + let right = AssetData::embedded(png(|x, _| x >= 32)); s.edit(|d, _| { let mut a = (**d.assets.get(&g.asset).unwrap()).clone(); a.data = right.clone(); diff --git a/crates/engine/src/cmd/anchored.rs b/crates/engine/src/cmd/anchored.rs index d55cdf753..bc80b2e1c 100644 --- a/crates/engine/src/cmd/anchored.rs +++ b/crates/engine/src/cmd/anchored.rs @@ -544,7 +544,7 @@ mod text_frame_tests { let d = &s.doc().unwrap().doc; d.check().unwrap(); let mut files: Vec<_> = - d.anchored_items().iter().filter_map(|(_, i)| i.graphic()).map(|g| d.assets.get(&g.asset).map(|a| a.data.clone())).collect(); + d.anchored_items().iter().filter_map(|(_, i)| i.graphic()).map(|g| d.assets.get(&g.asset).map(|a| a.data.load().unwrap())).collect(); files.sort(); files } diff --git a/crates/engine/src/cmd/book.rs b/crates/engine/src/cmd/book.rs index 29f95741d..c24aeae1c 100644 --- a/crates/engine/src/cmd/book.rs +++ b/crates/engine/src/cmd/book.rs @@ -59,7 +59,7 @@ fn load(path: &str) -> Result { let _ = path; vec![] }; - if path.to_lowercase().ends_with(".idml") { + if craft_file_formats::detect(path, &bytes) == Some(craft_file_formats::Format::Idml) { return super::interchange::import_idml_confined(&bytes, Some(std::path::Path::new(path))) .map_err(|e| EngineError::Other(format!("{path}: {e}"))); } @@ -132,7 +132,11 @@ impl ChapterBatch { fn writable_chapter(s: &Session, path: &str, command: &'static str) -> Result<()> { use std::path::Path; - if Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|ext| ext.eq_ignore_ascii_case("idml")) { + if Path::new(path) + .extension() + .and_then(std::ffi::OsStr::to_str) + .is_some_and(|ext| craft_file_formats::from_extension(ext) == Some(craft_file_formats::Format::Idml)) + { return Err(bad(command, format!("cannot change imported IDML chapter {path}; convert it to a native DesignCraft document first"))); } let target = crate::io::canonicalize(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?; @@ -307,7 +311,12 @@ pub fn specs() -> Vec { load(&path)?; let mut b = s.book.as_ref().ok_or_else(no_book)?.clone(); let at = p.get("at").and_then(Value::as_u64).map_or(b.documents.len(), |i| (i as usize).min(b.documents.len())); + // Keep a valid existing source attached to its chapter when inserting before it. + let shift_source = b.style_source < b.documents.len() && at <= b.style_source; b.documents.insert(at, path); + if shift_source { + b.style_source += 1; + } save(&b)?; s.book = Some(b); ok() @@ -319,6 +328,9 @@ pub fn specs() -> Vec { return Err(bad("book.remove", format!("no document {i}"))); } b.documents.remove(i); + if i < b.style_source { + b.style_source -= 1; + } if b.style_source >= b.documents.len() { b.style_source = 0; } @@ -462,4 +474,75 @@ mod tests { // As the one-style-at-a-time loop left them: each name once, at its last position. assert_eq!(kept, [("b", 2), ("a", 3), ("c", 4)]); } + + #[cfg(not(target_arch = "wasm32"))] + fn open_book_fixture(path: &std::path::Path, documents: &[&str], style_source: usize) -> Session { + let book = super::Book { + path: path.to_str().unwrap().to_string(), + documents: documents.iter().map(|path| path.to_string()).collect(), + style_source, + }; + super::save(&book).unwrap(); + let mut s = Session::new(); + s.execute("book.open", &json!({"path": book.path})).unwrap(); + s + } + + #[cfg(not(target_arch = "wasm32"))] + fn assert_book_style_source(s: &Session, path: &std::path::Path, index: usize, source: Option<&str>) { + let mut reopened = Session::new(); + reopened.execute("book.open", &json!({"path": path.to_str().unwrap()})).unwrap(); + for book in [s.book.as_ref().unwrap(), reopened.book.as_ref().unwrap()] { + assert_eq!(book.style_source, index, "{book:?}"); + assert_eq!(book.documents.get(index).map(String::as_str), source, "{book:?}"); + } + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn adding_a_chapter_keeps_the_existing_style_source() { + let dir = tempfile::TempDir::new().unwrap(); + let path = dir.path().join("book.dcbook"); + let inserted = dir.path().join("inserted.designcraft"); + let inserted = inserted.to_str().unwrap(); + let doc = designcraft_doc::Document::new(&Default::default()); + std::fs::write(inserted, designcraft_format::save(&doc).unwrap()).unwrap(); + let chapters = ["a.designcraft", "b.designcraft", "c.designcraft"]; + for (documents, source, at, expected_index, expected_source) in [ + (&chapters[..], 1, 0, 2, Some("b.designcraft")), + (&chapters[..], 1, 1, 2, Some("b.designcraft")), + (&chapters[..], 1, 2, 1, Some("b.designcraft")), + (&chapters[..], 1, 3, 1, Some("b.designcraft")), + (&[][..], 0, 0, 0, Some(inserted)), + (&chapters[..], 3, 0, 3, Some("c.designcraft")), + (&chapters[..], usize::MAX, 0, usize::MAX, None), + ] { + let mut s = open_book_fixture(&path, documents, source); + s.execute("book.add", &json!({"path": inserted, "at": at})).unwrap(); + assert_book_style_source(&s, &path, expected_index, expected_source); + } + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn removing_a_chapter_keeps_a_surviving_style_source() { + let dir = tempfile::TempDir::new().unwrap(); + let path = dir.path().join("book.dcbook"); + let chapters = ["a.designcraft", "b.designcraft", "c.designcraft"]; + for (documents, source, removed, expected_index, expected_source) in [ + (&chapters[..], 1, 0, 0, Some("b.designcraft")), + (&chapters[..], 2, 0, 1, Some("c.designcraft")), + (&chapters[..], 1, 2, 1, Some("b.designcraft")), + (&chapters[..], 0, 2, 0, Some("a.designcraft")), + (&chapters[..], 1, 1, 1, Some("c.designcraft")), + (&chapters[..], 2, 2, 0, Some("a.designcraft")), + (&chapters[..1], 0, 0, 0, None), + (&chapters[..], 3, 0, 0, Some("b.designcraft")), + (&chapters[..], usize::MAX, 0, 0, Some("b.designcraft")), + ] { + let mut s = open_book_fixture(&path, documents, source); + s.execute("book.remove", &json!({"index": removed})).unwrap(); + assert_book_style_source(&s, &path, expected_index, expected_source); + } + } } diff --git a/crates/engine/src/cmd/buttons.rs b/crates/engine/src/cmd/buttons.rs index 5a644503d..9c5174440 100644 --- a/crates/engine/src/cmd/buttons.rs +++ b/crates/engine/src/cmd/buttons.rs @@ -18,7 +18,13 @@ fn parse(p: &Value) -> Result> { "lastPage" => ButtonAction::GoToLastPage, "nextPage" => ButtonAction::GoToNextPage, "previousPage" => ButtonAction::GoToPreviousPage, - "url" => ButtonAction::GoToUrl { url: p.get("url").and_then(Value::as_str).ok_or_else(|| bad("button.set", "`url` required"))?.to_string() }, + "url" => { + let url = p.get("url").and_then(Value::as_str).ok_or_else(|| bad("button.set", "`url` required"))?; + if !designcraft_doc::is_safe_link_url(url) { + return Err(bad("button.set", "`url` must start with http://, https:// or mailto:")); + } + ButtonAction::GoToUrl { url: url.to_string() } + } o => return Err(bad("button.set", format!("unknown action `{o}`"))), })) } @@ -179,4 +185,16 @@ mod tests { assert_eq!(designcraft_render::pdf_page_count(&bytes), Some(3), "the update still reads"); assert!(s.execute("button.list", &json!({})).unwrap().as_array().unwrap().is_empty()); } + + #[test] + fn buttons_refuse_non_web_schemes() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 1})).unwrap(); + let id = s.execute("frame.create", &json!({"rect": [72, 72, 200, 120]})).unwrap()["id"].clone(); + for bad in ["javascript:alert(1)", "file:///etc/passwd", "\\\\attacker\\share"] { + let e = s.execute("button.set", &json!({"ids": [id.clone()], "action": "url", "url": bad})).expect_err("refused").to_string(); + assert!(e.contains("http://"), "{bad}: {e}"); + } + s.execute("button.set", &json!({"ids": [id], "action": "url", "url": "https://example.com"})).unwrap(); + } } diff --git a/crates/engine/src/cmd/captions.rs b/crates/engine/src/cmd/captions.rs index c2fbbc6ee..b00dac1be 100644 --- a/crates/engine/src/cmd/captions.rs +++ b/crates/engine/src/cmd/captions.rs @@ -112,6 +112,19 @@ fn caption(s: &mut Session, p: &Value) -> Result { }) } +/// Whether any item on the document spreads, inside groups included, is a live caption: the +/// items [`refresh_live`] visits. +pub(crate) fn has_live(d: &Document) -> bool { + let mut found = false; + for it in d.spreads.iter().flat_map(|sp| &sp.items) { + it.walk(&mut |i| found |= i.live_caption.is_some()); + if found { + return true; + } + } + false +} + /// Bring live captions up to date with their source objects (the text frame's story is /// replaced when its text changed; a caption whose source is gone keeps its last text). pub(crate) fn refresh_live(d: &mut Document) -> bool { @@ -136,6 +149,17 @@ pub(crate) fn refresh_live(d: &mut Document) -> bool { changed } +/// Keep a text selection inside its story after `refresh_live` replaced the story's text. +pub(crate) fn clamp_text_selection(d: &Document, sel: &mut Selection) { + if let Some(ts) = sel.text.as_mut() + && ts.cell.is_none() + && let Some(st) = d.story(ts.story) + { + ts.anchor = designcraft_doc::floor_char_boundary(&st.text, ts.anchor); + ts.focus = designcraft_doc::floor_char_boundary(&st.text, ts.focus); + } +} + #[cfg(test)] mod tests { use serde_json::json; @@ -178,4 +202,107 @@ mod tests { s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(text(&s), "Photo: First", "one undo step for both"); } + + #[test] + fn live_caption_follows_a_committed_gesture() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = designcraft_doc::ItemId(r["ids"][0].as_u64().unwrap()); + let text = |s: &Session| { + let d = &s.doc().unwrap().doc; + d.stories[&d.item(cid).unwrap().text_frame().unwrap().story].text.clone() + }; + s.begin_interaction("Alt Text").unwrap(); + s.preview("object.altText", json!({"text": "Second", "ids": [a]})).unwrap(); + let revision = s.doc().unwrap().revision; + s.commit_interaction().unwrap(); + assert_eq!(text(&s), "Photo: Second", "updated when the gesture commits"); + assert_ne!(s.doc().unwrap().revision, revision, "caches keyed by the revision draw the new caption"); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(text(&s), "Photo: First", "one undo step for both"); + } + + #[test] + fn auto_sized_live_caption_fits_a_committed_gesture() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = r["ids"][0].as_u64().unwrap(); + s.execute("object.textFrameOptions", &json!({"ids": [cid], "autoSize": "heightOnly"})).unwrap(); + let height = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(cid)).unwrap().bounds().height(); + let short = height(&s); + let long = "A harbour at dawn, with the fishing boats coming in one after another past the lighthouse and the long stone pier"; + s.begin_interaction("Alt Text").unwrap(); + s.preview("object.altText", json!({"text": long, "ids": [a]})).unwrap(); + s.commit_interaction().unwrap(); + assert!(height(&s) > short + 5.0, "the caption frame grows with its text: {short} -> {}", height(&s)); + } + + #[test] + fn grouped_live_caption_follows_its_source() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + let b = s.execute("frame.create", &json!({"rect": [300, 72, 400, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = designcraft_doc::ItemId(r["ids"][0].as_u64().unwrap()); + // The only live caption sits inside a group, not at the top of the spread. + s.execute("object.group", &json!({"ids": [cid.0, b]})).unwrap(); + let text = |s: &Session| { + let d = &s.doc().unwrap().doc; + d.stories[&d.item(cid).unwrap().text_frame().unwrap().story].text.clone() + }; + s.execute("object.altText", &json!({"text": "Second", "ids": [a]})).unwrap(); + assert_eq!(text(&s), "Photo: Second", "updated by a command"); + s.begin_interaction("Alt Text").unwrap(); + s.preview("object.altText", json!({"text": "Third", "ids": [a]})).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(text(&s), "Photo: Third", "updated when the gesture commits"); + } + + /// A session with a live caption reading "Photo: First" and the caret at its end. + fn caret_at_end_of_live_caption() -> (Session, designcraft_doc::StoryId) { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = designcraft_doc::ItemId(r["ids"][0].as_u64().unwrap()); + let sid = s.doc().unwrap().doc.item(cid).unwrap().text_frame().unwrap().story; + let len = "Photo: First".len(); + s.execute("text.select", &json!({"story": sid.0, "anchor": len, "focus": len})).unwrap(); + (s, sid) + } + + fn assert_caret_inside(s: &Session, sid: designcraft_doc::StoryId) { + let st = s.doc().unwrap(); + let len = st.doc.stories[&sid].text.len(); + let ts = st.selection.text.unwrap(); + assert!(ts.anchor <= len && ts.focus <= len, "caret {}..{} past the end of {len} bytes", ts.anchor, ts.focus); + assert!(s.ime_text_context().is_some(), "the caret still describes its text"); + } + + #[test] + fn caret_stays_inside_a_live_caption_that_discards_typing() { + let (mut s, sid) = caret_at_end_of_live_caption(); + s.execute("text.insert", &json!({"text": " and more"})).unwrap(); + assert_eq!(s.doc().unwrap().doc.stories[&sid].text, "Photo: First"); + assert_caret_inside(&s, sid); + } + + #[test] + fn caret_stays_inside_a_live_caption_after_a_committed_gesture() { + let (mut s, sid) = caret_at_end_of_live_caption(); + s.begin_interaction("Typing").unwrap(); + s.preview("text.insert", json!({"text": " and more"})).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(s.doc().unwrap().doc.stories[&sid].text, "Photo: First"); + assert_caret_inside(&s, sid); + } } diff --git a/crates/engine/src/cmd/changes.rs b/crates/engine/src/cmd/changes.rs index 328bcf186..1ed5b8716 100644 --- a/crates/engine/src/cmd/changes.rs +++ b/crates/engine/src/cmd/changes.rs @@ -1,6 +1,6 @@ //! Type › Track Changes: record edits as inserted / deleted text; accept or reject them. -use designcraft_doc::{ChangeMark, Document, Story, StoryId}; +use designcraft_doc::{ChangeMark, Document, Selection, Story, StoryId}; use serde_json::{Value, json}; use super::{CommandSpec, cmd, has_doc}; @@ -46,7 +46,7 @@ fn ranges(st: &Story, mark: ChangeMark) -> Vec> { } /// Accept (or reject) every change in `sid`. -fn resolve(d: &mut Document, sid: StoryId, accept: bool) -> usize { +fn resolve(d: &mut Document, sel: &mut Selection, sid: StoryId, accept: bool) -> usize { let (remove, keep) = if accept { (ChangeMark::Deleted, ChangeMark::Inserted) } else { (ChangeMark::Inserted, ChangeMark::Deleted) }; let (removed, keep_count) = { let st = match d.story(sid) { @@ -68,6 +68,7 @@ fn resolve(d: &mut Document, sid: StoryId, accept: bool) -> usize { }); } for r in removed { + super::notes::follow_delete(sel, sid, r.clone()); d.adjust_text_ranges(sid, r, 0); } n @@ -96,10 +97,12 @@ fn one(s: &mut Session, p: &Value, accept: bool) -> Result { } } if delete { + super::notes::follow_delete(sel, sid, r.clone()); d.adjust_text_ranges(sid, r.clone(), 0); } if let Some(t) = &mut sel.text && t.story == sid + && t.cell.is_none() { let len = d.story(sid).map_or(0, |x| x.len()); t.anchor = t.anchor.min(len); @@ -163,10 +166,11 @@ fn all(s: &mut Session, p: &Value, accept: bool) -> Result { let ids: Vec = d.stories.keys().copied().filter(|id| only.is_none_or(|o| o == *id)).collect(); let mut n = 0; for id in ids { - n += resolve(d, id, accept); + n += resolve(d, sel, id, accept); } - // The caret may now be past the end. + // A body-text edit must not clamp a cell or note caret against the body length. if let Some(t) = &mut sel.text + && t.cell.is_none() && let Some(st) = d.story(t.story) { t.anchor = t.anchor.min(st.len()); @@ -220,4 +224,91 @@ mod tests { assert_eq!(text(&s), "The dog sat"); assert!(s.execute("changes.list", &json!({})).unwrap().as_array().unwrap().is_empty()); } + + /// Removing a tracked run follows the selected text by UTF-8 byte offsets, including undo. + #[test] + fn resolving_a_removed_change_follows_the_text_selection() { + use designcraft_doc::ChangeMark; + + for (command, mark) in [ + ("changes.accept", ChangeMark::Deleted), + ("changes.reject", ChangeMark::Inserted), + ("changes.acceptAll", ChangeMark::Deleted), + ("changes.rejectAll", ChangeMark::Inserted), + ] { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let sid = s.execute("frame.create", &json!({"rect": [72, 72, 400, 200], "content": "text", "text": "ab猫cdéfg"})).unwrap()["story"] + .as_u64() + .unwrap(); + s.edit(|d, _| { + d.story_mut(designcraft_doc::StoryId(sid)).unwrap().format_chars(2..5, |f| f.over.change = Some(mark)); + Ok(()) + }) + .unwrap(); + s.execute("text.select", &json!({"story": sid, "anchor": 9, "focus": 5})).unwrap(); + let params = if command.ends_with("All") { json!({"story": sid}) } else { json!({"story": sid, "start": 2}) }; + s.execute(command, ¶ms).unwrap(); + assert_eq!(s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().text, "abcdéfg", "{command}"); + let selected = s.doc().unwrap().selection.text.unwrap(); + assert_eq!((selected.anchor, selected.focus), (6, 2), "{command}"); + s.execute("edit.undo", &json!({})).unwrap(); + let selected = s.doc().unwrap().selection.text.unwrap(); + assert_eq!((selected.anchor, selected.focus), (9, 5), "undo {command}"); + s.execute("edit.redo", &json!({})).unwrap(); + let selected = s.doc().unwrap().selection.text.unwrap(); + assert_eq!((selected.anchor, selected.focus), (6, 2), "redo {command}"); + } + } + + /// All-change resolution removes disjoint runs from the end and maps each one once. + #[test] + fn resolving_all_changes_follows_multiple_removed_runs() { + use designcraft_doc::ChangeMark; + + for (command, mark) in [("changes.acceptAll", ChangeMark::Deleted), ("changes.rejectAll", ChangeMark::Inserted)] { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let sid = s.execute("frame.create", &json!({"rect": [72, 72, 400, 200], "content": "text", "text": "a猫bécδd"})).unwrap()["story"] + .as_u64() + .unwrap(); + s.edit(|d, _| { + let story = d.story_mut(designcraft_doc::StoryId(sid)).unwrap(); + story.format_chars(1..4, |f| f.over.change = Some(mark)); + story.format_chars(5..7, |f| f.over.change = Some(mark)); + Ok(()) + }) + .unwrap(); + s.execute("text.select", &json!({"story": sid, "anchor": 10, "focus": 4})).unwrap(); + s.execute(command, &json!({"story": sid})).unwrap(); + assert_eq!(s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().text, "abcδd", "{command}"); + let selected = s.doc().unwrap().selection.text.unwrap(); + assert_eq!((selected.anchor, selected.focus), (5, 1), "{command}"); + } + } + + /// A body-story change leaves the independent footnote insertion point alone. + #[test] + fn resolving_body_changes_keeps_a_footnote_caret() { + for command in ["changes.accept", "changes.acceptAll"] { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let sid = s.execute("frame.create", &json!({"rect": [72, 72, 400, 200], "content": "text", "text": "abcd"})).unwrap()["story"] + .as_u64() + .unwrap(); + s.execute("text.select", &json!({"story": sid, "anchor": 4, "focus": 4})).unwrap(); + s.execute("footnote.insert", &json!({"text": "Footnote text beyond body"})).unwrap(); + let before = s.doc().unwrap().selection.text.unwrap(); + s.edit(|d, _| { + d.story_mut(designcraft_doc::StoryId(sid)) + .unwrap() + .format_chars(0..1, |f| f.over.change = Some(designcraft_doc::ChangeMark::Deleted)); + Ok(()) + }) + .unwrap(); + let params = if command.ends_with("All") { json!({"story": sid}) } else { json!({"story": sid, "start": 0}) }; + s.execute(command, ¶ms).unwrap(); + assert_eq!(s.doc().unwrap().selection.text.unwrap(), before, "{command}"); + } + } } diff --git a/crates/engine/src/cmd/color_theme.rs b/crates/engine/src/cmd/color_theme.rs index c406c72da..eb84bbae1 100644 --- a/crates/engine/src/cmd/color_theme.rs +++ b/crates/engine/src/cmd/color_theme.rs @@ -37,11 +37,12 @@ fn extract(s: &mut Session, p: &Value) -> Result { }; let graphic = st.doc.item(id).and_then(|it| it.graphic()).ok_or_else(|| bad(ID, "select one placed raster image"))?; let asset = st.doc.assets.get(&graphic.asset).filter(|a| a.is_raster()).ok_or_else(|| bad(ID, "a raster image is required"))?; - let (width, height) = designcraft_images::pixel_size(&asset.data).ok_or_else(|| bad(ID, "cannot read image dimensions"))?; + let bytes = crate::cmd::asset_bytes(ID, asset)?; + let (width, height) = designcraft_images::pixel_size(&bytes).ok_or_else(|| bad(ID, "cannot read image dimensions"))?; if width == 0 || height == 0 || u64::from(width) * u64::from(height) > 16_000_000 { return Err(bad(ID, "the image must contain between 1 and 16 million pixels")); } - let img = designcraft_images::decode_rgba(&asset.data).ok_or_else(|| bad(ID, "cannot decode this image"))?; + let img = designcraft_images::decode_rgba(&bytes).ok_or_else(|| bad(ID, "cannot decode this image"))?; // A regular 128 × 128 grid bounds work and includes both axes even for very thin images. let (nx, ny) = (width.min(128), height.min(128)); let mut samples = Vec::with_capacity((nx * ny) as usize); diff --git a/crates/engine/src/cmd/datamerge.rs b/crates/engine/src/cmd/datamerge.rs index d34558423..fe565d679 100644 --- a/crates/engine/src/cmd/datamerge.rs +++ b/crates/engine/src/cmd/datamerge.rs @@ -19,7 +19,8 @@ use serde_json::{Value, json}; use super::{CommandSpec, bad, cmd, has_doc, id_param, ok, str_param}; use crate::{DocState, Result, Session}; use fill::{FillReport, ImageCache, Maps, MissingImage, Sources, copy_item, fill_row, identity_map}; -use parse::{Table, TileInput, extension, relative_between, select_records, status_of, table_from_bytes, table_from_grid, table_from_objects, tile}; +pub(crate) use parse::relative_between; +use parse::{Table, TileInput, select_records, status_of, table_from_bytes, table_from_grid, table_from_objects, tile}; #[cfg(not(target_arch = "wasm32"))] pub(crate) use parse::fingerprint_from; @@ -146,8 +147,27 @@ fn fields_cmd(s: &mut Session, p: &Value) -> Result { let Some(src) = st.doc.data_merge.sources.first() else { return Ok(json!([])); }; - let placeholders = st.doc.data_merge.placeholders.clone(); - Ok(fields_json(&src.fields, |name| placeholders.iter().filter(|ph| ph.field == name).count())) + let doc = st.preview_stash.as_deref().unwrap_or(st.doc.as_ref()); + let placeholders = &doc.data_merge.placeholders; + let mut marker_counts = std::collections::HashMap::::new(); + for (story_id, story) in &doc.stories { + // The generated endnote frame repeats note text already counted in its owning story. + if Some(*story_id) == doc.endnote_story { + continue; + } + for (start, end, name) in fill::find_markers(&story.text) { + // A bound placeholder already accounts for its marker in the usage count. + let covered = placeholders.iter().any(|ph| { + matches!((&ph.role, &ph.anchor), (PlaceholderRole::Text | PlaceholderRole::Image, PlaceholderAnchor::Text { story, start: a, end: b }) + if story == story_id && start < *b && end > *a) + }); + if !covered { + *marker_counts.entry(name).or_default() += 1; + } + } + fill::count_nested_markers(story, &mut marker_counts, 0); + } + Ok(fields_json(&src.fields, |name| placeholders.iter().filter(|ph| ph.field == name).count() + marker_counts.get(name).copied().unwrap_or(0))) } fn placeholder_add(s: &mut Session, p: &Value) -> Result { @@ -827,11 +847,10 @@ fn take_file(p: &Value) -> std::result::Result<(Vec, String, Option) } fn parse_file(bytes: &[u8], name: &str, delimiter: Option, sheet: Option<&str>) -> std::result::Result { - let ext = extension(name); - if ext == "xls" || ext == "xlsm" { - return Err(format!("{ext} workbooks are not supported")); + if craft_file_formats::detect(name, &[]) == Some(craft_file_formats::Format::LegacyExcel) { + return Err("legacy Excel .xls/.xlsm workbooks are not supported".into()); } - if ext == "xlsx" { + if craft_file_formats::detect(name, bytes) == Some(craft_file_formats::Format::Xlsx) { let grid = designcraft_textimport::xlsx_records(bytes, sheet).map_err(|e| e.to_string())?; return table_from_grid(grid, Delimiter::Comma); } diff --git a/crates/engine/src/cmd/datamerge/fill.rs b/crates/engine/src/cmd/datamerge/fill.rs index 9d94da7d9..b7d465320 100644 --- a/crates/engine/src/cmd/datamerge/fill.rs +++ b/crates/engine/src/cmd/datamerge/fill.rs @@ -181,7 +181,7 @@ pub struct ImageCache { #[derive(Default)] struct ImageCacheInner { - files: std::collections::HashMap>>, + files: std::collections::HashMap, bytes: usize, } @@ -191,14 +191,16 @@ const MAX_CACHED_IMAGE_BYTES: usize = 256 << 20; impl ImageCache { /// The bytes of `path`, reading it once per merge. `read` is called only on a miss. - fn get_or_read(&self, path: &Path, read: impl FnOnce() -> Option>) -> Option>> { + fn get_or_read(&self, path: &Path, read: impl FnOnce() -> Option>) -> Option { if let Some(found) = self.inner.borrow().files.get(path) { return Some(found.clone()); } - let bytes = std::sync::Arc::new(read()?); + let bytes = read()?; + let len = bytes.len(); + let bytes = designcraft_doc::AssetData::embedded(bytes); let mut inner = self.inner.borrow_mut(); - if inner.bytes.saturating_add(bytes.len()) <= MAX_CACHED_IMAGE_BYTES { - inner.bytes = inner.bytes.saturating_add(bytes.len()); + if inner.bytes.saturating_add(len) <= MAX_CACHED_IMAGE_BYTES { + inner.bytes = inner.bytes.saturating_add(len); inner.files.insert(path.to_path_buf(), bytes.clone()); } Some(bytes) @@ -415,6 +417,26 @@ fn fill_inner(st: &mut Story, fields: &[DataField], row: &[String], depth: usize } } +/// Count markers in nested text to match the merge's depth limit. +pub(super) fn count_nested_markers(st: &Story, counts: &mut HashMap, depth: usize) { + if depth >= MAX_NESTED_DEPTH { + return; + } + let mut count = |inner: &Story| { + for (_, _, name) in find_markers(&inner.text) { + *counts.entry(name).or_default() += 1; + } + count_nested_markers(inner, counts, depth + 1); + }; + for table in st.tables.values() { + for cell in &table.cells { + count(&cell.text); + } + } + for note in st.notes.iter().chain(st.endnotes.iter()) { + count(¬e.text); + } +} /// Replace the markers of known fields in `st`'s own text with the record's values. fn fill_own(st: &mut Story, fields: &[DataField], row: &[String]) { let found: Vec<(usize, usize, String)> = @@ -424,7 +446,7 @@ fn fill_own(st: &mut Story, fields: &[DataField], row: &[String]) { } } -fn find_markers(text: &str) -> Vec<(usize, usize, String)> { +pub(super) fn find_markers(text: &str) -> Vec<(usize, usize, String)> { let mut out = Vec::new(); let mut i = 0; // The next `>>` at or after the `<<` being looked at: found once, kept until passed, so the @@ -525,7 +547,7 @@ fn create_link(doc: &mut designcraft_doc::Document, anchor: PlaceholderAnchor, d /// An image file named by a cell: where it was found, its bytes, its pixel size and its natural size. struct LoadedImage { path: PathBuf, - bytes: Arc>, + bytes: designcraft_doc::AssetData, pixels: (u32, u32), natural: (f64, f64), } @@ -547,7 +569,8 @@ fn load_image(cell: &str, sources: &Sources) -> Option { })?; // A file that does not decode leaves the search going, so a valid image at a later // candidate is still found. - let (pixels, natural) = designcraft_render::image_size(&bytes).zip(designcraft_render::natural_size(&bytes, 0))?; + let raw = bytes.embedded_bytes()?; + let (pixels, natural) = designcraft_render::image_size(raw).zip(designcraft_render::natural_size(raw, 0))?; Some(LoadedImage { path: p.clone(), bytes, pixels, natural }) }) } @@ -565,7 +588,7 @@ fn add_image_asset(doc: &mut designcraft_doc::Document, image: LoadedImage, fiel doc.add_asset(Asset { id: fresh, name, - mime: designcraft_render::image_mime(&image.bytes).to_string(), + mime: designcraft_render::image_mime(image.bytes.embedded_bytes().map_or(&[][..], |b| b.as_slice())).to_string(), link, // The cached buffer is shared with every other record naming this file, rather than // copied per record (#1595). diff --git a/crates/engine/src/cmd/datamerge/parse.rs b/crates/engine/src/cmd/datamerge/parse.rs index ca3a61b27..435188f99 100644 --- a/crates/engine/src/cmd/datamerge/parse.rs +++ b/crates/engine/src/cmd/datamerge/parse.rs @@ -366,102 +366,20 @@ pub fn status_of(path: &Path, previous: Option) -> SourceStatus { } fn decode_utf8(bytes: &[u8], name: &str) -> Result { - let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes); - let mut text = String::from_utf8(bytes.to_vec()).map_err(|_| format!("{name} is not UTF-8"))?; - if text.starts_with('\u{FEFF}') { - text.replace_range(..'\u{FEFF}'.len_utf8(), ""); - } - Ok(text) + craft_csv::decode_utf8(bytes).map_err(|_| format!("{name} is not UTF-8")) } fn sniff(name: &str, text: &str) -> Delimiter { - let ext = extension(name); - if ext == "tsv" || ext == "tab" { - return Delimiter::Tab; + match craft_file_formats::detect(name, &[]) { + Some(craft_file_formats::Format::Tsv) => return Delimiter::Tab, + Some(craft_file_formats::Format::Csv) => return Delimiter::Comma, + _ => {} } - if ext == "csv" { - return Delimiter::Comma; + match craft_csv::sniff_delimiter(text, '"') { + b'\t' => Delimiter::Tab, + b';' => Delimiter::Semicolon, + _ => Delimiter::Comma, } - let tab = header_delimiter_candidate(text, '\t'); - let semicolon = header_delimiter_candidate(text, ';'); - let comma = header_delimiter_candidate(text, ','); - // Prefer formats with coherent quoted-field boundaries. If none separates fields, - // retain the legacy permissive choice; this is detection, not record validation. - let prefer_coherent = [&tab, &semicolon, &comma].iter().any(|c| c.found && c.coherent); - let eligible = |c: &HeaderDelimiterCandidate| c.found && (!prefer_coherent || c.coherent); - if eligible(&tab) { - Delimiter::Tab - } else if eligible(&semicolon) && !eligible(&comma) { - Delimiter::Semicolon - } else { - Delimiter::Comma - } -} - -pub fn extension(name: &str) -> String { - Path::new(name).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase() -} - -struct HeaderDelimiterCandidate { - found: bool, - coherent: bool, -} - -/// A candidate separator outside quotes in its first nonempty logical record. -/// Only that candidate starts a new field. Quotes in an unquoted field stay literal; -/// Non-whitespace text after a quoted field closes lowers coherence, not validity. -fn header_delimiter_candidate(text: &str, delimiter: char) -> HeaderDelimiterCandidate { - let mut found = false; - let mut coherent = true; - let mut quoted = false; - let mut after_quote = false; - let mut field_start = true; - let mut nonempty = false; - let mut chars = text.chars().peekable(); - while let Some(c) = chars.next() { - if quoted { - if c == '"' { - if chars.peek() == Some(&'"') { - chars.next(); - nonempty = true; - } else { - quoted = false; - after_quote = true; - } - } else if !c.is_whitespace() { - nonempty = true; - } - continue; - } - match c { - '"' if field_start => { - quoted = true; - field_start = false; - } - separator if separator == delimiter => { - found = true; - field_start = true; - after_quote = false; - } - '\r' | '\n' => { - if nonempty { - break; - } - found = false; - coherent = true; - field_start = true; - after_quote = false; - } - _ => { - // Whitespace may follow a closing quote, but does not start a new field - // or allow a later non-whitespace suffix to regain coherence. - coherent &= !after_quote || c.is_whitespace(); - nonempty |= !c.is_whitespace(); - field_start = false; - } - } - } - HeaderDelimiterCandidate { found, coherent: coherent && !quoted } } /// Header cell: trim, drop one leading apostrophe, then `@` is image and `#` is qr. @@ -523,121 +441,59 @@ fn table_from_raw(raw: &[RawRow]) -> Result { Ok(Table { fields, rows, warnings, delimiter: Delimiter::Comma }) } -/// Split `s` on `delim`. Quoted fields keep commas, doubled quotes, and newlines. -/// More than 200 header columns, or the 100,001st non-empty data row, is an error and -/// the oversized row is not kept. +/// Parse delimited text with the shared CSV grammar, then apply DesignCraft's table limits and row policy. fn parse_delimited(s: &str, delim: char) -> Result, String> { - let mut rows: Vec = Vec::new(); - let mut row: Vec = Vec::new(); - let mut field = String::new(); - let mut quoted = false; - let mut field_start = true; + let mut rows = Vec::new(); let mut line = 1usize; - let mut row_line = 1usize; - let mut data_rows = 0usize; - let mut header_width: Option = None; - let mut discarding = false; - let mut chars = s.chars().peekable(); - - let finish = |row: &mut Vec, - field: &mut String, - rows: &mut Vec, - data_rows: &mut usize, - header_width: &mut Option, - row_line: usize, - discarding: &mut bool| - -> Result<(), String> { - if !*discarding { - row.push(std::mem::take(field)); - } else { - field.clear(); - } - *discarding = false; - let empty = row.iter().all(|c| c.trim().is_empty()); - if empty { - row.clear(); - return Ok(()); - } - match *header_width { - None => { - if row.len() > DATA_MERGE_MAX_COLS { - return Err(format!("the file has {} columns; the limit is {DATA_MERGE_MAX_COLS}", row.len())); - } - *header_width = Some(row.len()); - rows.push(RawRow { line: row_line, cells: std::mem::take(row) }); - } - Some(width) => { - *data_rows = data_rows.saturating_add(1); - if *data_rows > DATA_MERGE_MAX_ROWS { - return Err(format!("the file has more than {DATA_MERGE_MAX_ROWS} data rows")); - } - if row.len() > width.saturating_add(1) { - row.truncate(width.saturating_add(1)); - } - rows.push(RawRow { line: row_line, cells: std::mem::take(row) }); - } - } - Ok(()) - }; - - while let Some(c) = chars.next() { - if c == '\n' || (c == '\r' && chars.peek() != Some(&'\n')) { - line = line.saturating_add(1); - } - if quoted { - match c { - '"' if chars.peek() == Some(&'"') => { - if !discarding { - field.push('"'); + let mut error = None; + craft_csv::parse_records(s, delim, '"', DATA_MERGE_MAX_COLS.saturating_add(1), |cells| { + let row_line = line; + let inside_newlines = cells + .iter() + .map(|cell| { + let mut count = 0usize; + let mut previous_cr = false; + for c in cell.chars() { + if c == '\r' { + count = count.saturating_add(1); + previous_cr = true; + } else if c == '\n' { + if !previous_cr { + count = count.saturating_add(1); + } + previous_cr = false; + } else { + previous_cr = false; } - chars.next(); } - '"' => quoted = false, - _ if !discarding => field.push(c), - _ => {} - } - continue; + count + }) + .sum::(); + line = line.saturating_add(inside_newlines).saturating_add(1); + if cells.iter().all(|cell| cell.trim().is_empty()) { + return true; } - if c == delim { - field_start = true; - if discarding { - field.clear(); - continue; - } - row.push(std::mem::take(&mut field)); - if header_width.is_none() && row.len() > DATA_MERGE_MAX_COLS { - return Err(format!("the file has {} columns; the limit is {DATA_MERGE_MAX_COLS}", row.len())); - } - if header_width.is_some_and(|w| row.len() > w.saturating_add(1)) { - row.pop(); - discarding = true; - } - continue; + if rows.is_empty() && cells.len() > DATA_MERGE_MAX_COLS { + error = Some(format!("the file has {} columns; the limit is {DATA_MERGE_MAX_COLS}", cells.len())); + return false; } - match c { - '"' if field_start => { - quoted = true; - field_start = false; - } - '\r' | '\n' => { - if c == '\r' && chars.peek() == Some(&'\n') { - chars.next(); - line = line.saturating_add(1); - } - finish(&mut row, &mut field, &mut rows, &mut data_rows, &mut header_width, row_line, &mut discarding)?; - row_line = line; - field_start = true; - } - _ => { - field_start = false; - if !discarding { - field.push(c); - } - } + if !rows.is_empty() && rows.len() > DATA_MERGE_MAX_ROWS { + error = Some(format!("the file has more than {DATA_MERGE_MAX_ROWS} data rows")); + return false; } + rows.push(RawRow { line: row_line, cells }); + true + }); + if let Some(error) = error { + return Err(error); } - if !field.is_empty() || !row.is_empty() || discarding { - finish(&mut row, &mut field, &mut rows, &mut data_rows, &mut header_width, row_line, &mut discarding)?; + let Some(width) = rows.first().map(|row| row.cells.len()) else { + return Ok(rows); + }; + for row in rows.iter_mut().skip(1) { + if row.cells.len() > width.saturating_add(1) { + row.cells.truncate(width.saturating_add(1)); + } } Ok(rows) } diff --git a/crates/engine/src/cmd/datamerge/tests.rs b/crates/engine/src/cmd/datamerge/tests.rs index e631b69ea..3515d665e 100644 --- a/crates/engine/src/cmd/datamerge/tests.rs +++ b/crates/engine/src/cmd/datamerge/tests.rs @@ -1654,8 +1654,10 @@ fn a_field_converted_into_a_table_is_filled_in_its_cell() { exec(&mut s, "text.select", json!({"story": made["story"], "anchor": 0, "focus": 13})); exec(&mut s, "table.convertFromText", json!({"columnSeparator": "tab"})); assert_eq!(cell_texts(&s), ["Name", "<>"]); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1); exec(&mut s, "data.preview", json!({"record": 1})); assert_eq!(cell_texts(&s), ["Name", "Ada"], "the field is filled in its cell"); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1, "the count reads the unfilled template while previewing"); } /// A table `Who⇥Where` with the caret in its second cell, and a source with `Name` and `City`. @@ -1745,6 +1747,7 @@ fn a_field_in_an_endnote_is_filled() { s.documents()[s.active_index().unwrap()].doc.stories.values().flat_map(|st| st.endnotes.iter().map(|e| e.text.text.clone())).collect() }; assert_eq!(endnotes(&s), ["From <>"]); + assert_eq!(exec(&mut s, "data.fields", json!({}))[1]["uses"], 1); exec(&mut s, "data.preview", json!({"record": 1})); assert_eq!(endnotes(&s), ["From London"]); } @@ -1799,3 +1802,50 @@ fn select_records_stops_expanding_once_the_limit_is_met_but_still_checks_every_r assert!(parse::select_records(10, "range", 1, "1-10,5-11", Some(2)).is_err()); assert!(parse::select_records(10, "range", 1, "1-2,0", Some(2)).is_err()); } + +#[test] +fn idml_reopen_relink_counts_existing_text_field() { + let dir = scratch("idml-field-count"); + let csv = dir.join("people.csv"); + let idml = dir.join("template.idml"); + write(&csv, "Name\nAda\n"); + let mut s = Session::new(); + new_doc(&mut s, 1); + text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "", None); + exec(&mut s, "data.source.select", json!({"path": csv})); + exec(&mut s, "data.placeholder.add", json!({"field": "Name"})); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1); + exec(&mut s, "file.exportIdml", json!({"path": idml})); + let mut reopened = Session::new(); + exec(&mut reopened, "file.openIdml", json!({"path": idml})); + exec(&mut reopened, "data.source.select", json!({"path": csv})); + let uses = exec(&mut reopened, "data.fields", json!({}))[0]["uses"].clone(); + exec(&mut reopened, "data.merge", json!({})); + assert!(texts(&reopened).contains(&"Ada".to_string())); + assert_eq!(uses, 1); +} + +#[test] +fn fields_count_unbound_markers_without_double_counting_bound_placeholders() { + let mut s = Session::new(); + new_doc(&mut s, 1); + select_csv(&mut s, "Name\nAda\n"); + text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "<> <>", None); + text_frame(&mut s, [36.0, 100.0, 240.0, 150.0], "", None); + exec(&mut s, "data.placeholder.add", json!({"field": "Name"})); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 3); + assert_eq!(exec(&mut s, "data.fields", json!({"csv": "Name\nAda\n"}))[0]["uses"], 0); +} + +#[test] +fn hyperlink_over_marker_does_not_hide_its_field_usage() { + let mut s = Session::new(); + new_doc(&mut s, 1); + select_csv(&mut s, "Name,Link\nAda,https://example.com\n"); + let made = text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "<>", None); + exec(&mut s, "text.select", json!({"story": made["story"], "anchor": 0, "focus": 8})); + exec(&mut s, "data.placeholder.add", json!({"field": "Link", "role": "hyperlink"})); + let fields = exec(&mut s, "data.fields", json!({})); + assert_eq!(fields[0]["uses"], 1); + assert_eq!(fields[1]["uses"], 1); +} diff --git a/crates/engine/src/cmd/export.rs b/crates/engine/src/cmd/export.rs index 270fcfc4c..d65af9272 100644 --- a/crates/engine/src/cmd/export.rs +++ b/crates/engine/src/cmd/export.rs @@ -22,3 +22,6 @@ pub use registry::specs; #[cfg(test)] mod text_tests; + +#[cfg(test)] +mod linked_tests; diff --git a/crates/engine/src/cmd/export/flatten.rs b/crates/engine/src/cmd/export/flatten.rs index 21fec1c5e..730f28b4a 100644 --- a/crates/engine/src/cmd/export/flatten.rs +++ b/crates/engine/src/cmd/export/flatten.rs @@ -31,7 +31,7 @@ pub(super) fn flatten(d: &designcraft_doc::Document, cache: &designcraft_compose id: aid, name: format!("flattened-{}.png", aid.0), mime: "image/png".into(), - data: std::sync::Arc::new(img.to_png()), + data: designcraft_doc::AssetData::embedded(img.to_png()), pixels: Some((w, h)), ..Default::default() }), @@ -310,7 +310,7 @@ fn rasterize_where_with_links( id: aid, name: format!("effects-{}.png", id.0), mime: "image/png".into(), - data: std::sync::Arc::new(img.to_png()), + data: designcraft_doc::AssetData::embedded(img.to_png()), pixels: Some((w, h)), ..Default::default() }), @@ -426,7 +426,7 @@ pub(super) fn rasterize_layered(d: &designcraft_doc::Document) -> Result<(Option .filter(|it| it.has_pdf_layer_override()) .filter_map(|it| { let g = it.graphic()?; - let a = d.assets.get(&g.asset).filter(|a| designcraft_render::is_pdf(&a.data))?; + let a = d.assets.get(&g.asset).filter(|a| a.output_bytes().is_ok_and(|o| designcraft_render::is_pdf(&o.bytes)))?; Some((it.id, g.clone(), a.clone(), it.pdf_hidden_layers.clone())) }) .collect(); @@ -442,7 +442,8 @@ pub(super) fn rasterize_layered(d: &designcraft_doc::Document) -> Result<(Option let [l, t, r, b] = shown.unwrap_or([0.0, 0.0, 1.0, 1.0]); let page_side = (g.size.0 / (r - l)).max(g.size.1 / (b - t)); let side = (page_side * 300.0 / 72.0).clamp(64.0, 8000.0) as u32; - let png = designcraft_render::pdf_page_png_options_checked(&a.data, a.page as usize, side, Some(&hidden), shown) + let bytes = a.output_bytes().map_err(|e| EngineError::Other(format!("{}: {e}", a.name)))?.bytes; + let png = designcraft_render::pdf_page_png_options_checked(&bytes, a.page as usize, side, Some(&hidden), shown) .map_err(|e| EngineError::Other(format!("{}: {e}", a.name)))?; let px = designcraft_render::image_size(&png); let aid = designcraft_doc::AssetId(out.alloc()); @@ -452,7 +453,7 @@ pub(super) fn rasterize_layered(d: &designcraft_doc::Document) -> Result<(Option id: aid, name: format!("{}.png", a.name), mime: "image/png".into(), - data: std::sync::Arc::new(png), + data: designcraft_doc::AssetData::embedded(png), pixels: px, ..Default::default() }), @@ -540,7 +541,7 @@ mod placed_layer_tests { let aid = d.item(id).unwrap().graphic().unwrap().asset; let asset = Arc::make_mut(d.assets.get_mut(&aid).unwrap()); asset.mime = "image/png".into(); - asset.data = Arc::new(designcraft_render::Rendered { width: 1, height: 1, pixels: vec![0, 0, 0, 0] }.to_png()); + asset.data = designcraft_doc::AssetData::embedded(designcraft_render::Rendered { width: 1, height: 1, pixels: vec![0, 0, 0, 0] }.to_png()); let (copy, count) = super::rasterize_layered(d).unwrap(); assert_eq!(count, 0); assert!(copy.is_none()); diff --git a/crates/engine/src/cmd/export/formats.rs b/crates/engine/src/cmd/export/formats.rs index 4474d8ae7..2f9d9f88e 100644 --- a/crates/engine/src/cmd/export/formats.rs +++ b/crates/engine/src/cmd/export/formats.rs @@ -25,7 +25,8 @@ pub(super) fn print_booklet(s: &mut Session, p: &Value) -> Result { } } -/// HTML and reflowable EPUB embed the EPS preview; keep the session's source bytes intact. +/// HTML and reflowable EPUB read embedded bytes only: every used linked asset gets its output +/// bytes embedded, and EPS its preview. The session's own assets stay as they are. fn eps_previews(d: &designcraft_doc::Document) -> (Cow<'_, designcraft_doc::Document>, Vec) { // Match the exporter's reading order (including Article exclusions), not every asset // in the document: unused or hidden graphics must not prevent an export. @@ -48,17 +49,40 @@ fn eps_previews(d: &designcraft_doc::Document) -> (Cow<'_, designcraft_doc::Docu 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 Some(a) = d.assets.get(&id) else { continue }; + let linked = a.data.is_linked(); + let got = match a.output_bytes() { + Ok(got) => got, + Err(e) => { + if linked { + warnings.push(format!("{}: the linked file is unavailable ({e}); graphic omitted", a.name)); + } + continue; + } }; - let png = designcraft_images::eps_png(&a.data); - if png.is_none() { - warnings.push(format!("{}: unusable EPS preview; graphic omitted", a.name)); + let bytes = got.bytes; + let eps = designcraft_images::is_eps(&bytes); + // Embedded bytes the writers read as they are; a linked asset's bytes are read here + // (the writers only see embedded data), and EPS is replaced by its preview. + if !eps && !linked { + continue; } + warnings.extend(got.warning); + let data = if eps { + let png = designcraft_images::eps_png(&bytes); + if png.is_none() { + warnings.push(format!("{}: unusable EPS preview; graphic omitted", a.name)); + } + png.unwrap_or_default() + } else { + bytes.as_ref().clone() + }; 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()); + if eps { + a.mime = "image/png".into(); + } + a.data = designcraft_doc::AssetData::embedded(data); } } (out, warnings) @@ -136,7 +160,7 @@ pub(super) fn export_text(s: &mut Session, p: &Value) -> Result { Some(f @ ("rtf" | "tagged")) => f, Some("txt") | Some("text") => "txt", Some(f) => return Err(bad(ID, format!("unknown format `{f}` (txt, rtf, tagged)"))), - None if path.is_some_and(|x| x.to_ascii_lowercase().ends_with(".rtf")) => "rtf", + None if path.is_some_and(|x| craft_file_formats::detect(x, &[]) == Some(craft_file_formats::Format::Rtf)) => "rtf", None => "txt", }; let text = match format { @@ -414,6 +438,36 @@ mod pptx_tests { assert!(s.execute("file.exportPptx", &json!({"pages": [9]})).is_err()); } + #[test] + fn cell_diagonal_lines_export_as_cell_diagonal_borders() { + 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": 2, "cols": 2})).unwrap(); + for (row, col, lines) in [(0, 0, "topLeft"), (0, 1, "topRight"), (1, 0, "crossed")] { + s.execute("table.setCell", &json!({"row": row, "col": col, "diagonal": {"lines": lines, "weight": 3}})).unwrap(); + } + let r = s.execute("file.exportPptx", &json!({})).unwrap(); + let x = read(&mut unzip(r["base64"].as_str().unwrap()), "ppt/slides/slide1.xml"); + let cells: Vec<&str> = x.split("").filter(|c| c.contains(" = cells.iter().map(|c| has(c)).collect(); + assert_eq!(got, [(true, false), (false, true), (true, true), (false, false)], "{x}"); + // The diagonals follow the four edges and come before the cell's fill, in the schema's order. + let c = cells[2]; + let at = |tag: &str| c.find(tag).unwrap_or(usize::MAX); + assert!(at(" at(" Self { + let dir = std::env::temp_dir().join(format!("dc-linked-output-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + Dir(dir) + } +} + +impl Drop for Dir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +/// A `w` × `h` PNG; transparent in its left half when `cut`. +fn png(w: usize, h: usize, cut: bool) -> Vec { + let mut pixels = Vec::new(); + for _ in 0..h { + for x in 0..w { + pixels.extend([200, 40, 40, if cut && x < w / 2 { 0 } else { 255 }]); + } + } + designcraft_render::Rendered { width: w as u32, height: h as u32, pixels }.to_png() +} + +/// A session with `bytes` placed, then made a linked asset to `file` (holding `bytes`) with a +/// proxy of them → the asset. +fn linked_session(file: &Path, bytes: &[u8]) -> (Session, AssetId) { + std::fs::write(file, bytes).unwrap(); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + s.execute("file.place", &json!({"base64": crate::cmd::base64_encode(bytes), "name": "art.png", "x": 10, "y": 10, "width": 100})).unwrap(); + let id = *s.doc().unwrap().doc.assets.keys().next().unwrap(); + let proxy = designcraft_render::proxy::make_proxy(bytes, 0).unwrap(); + let st = s.doc_mut().unwrap(); + let a = Arc::make_mut(Arc::make_mut(&mut st.doc).assets.get_mut(&id).unwrap()); + a.link = Some(file.to_string_lossy().into_owned()); + a.link_fingerprint = Some(fingerprint_from(&std::fs::metadata(file).unwrap())); + a.link_hash = Some(content_hash(bytes)); + a.storage = Storage::Linked; + a.data = AssetData::linked(LinkedSource::of(a, None).unwrap()); + a.proxy = Some(Proxy { mime: proxy.mime.into(), pixels: proxy.pixels, data: AssetData::embedded(proxy.data) }); + (s, id) +} + +fn warnings(r: &Value) -> String { + r["warnings"].to_string() +} + +fn pdf_text(r: &Value) -> String { + String::from_utf8_lossy(&crate::cmd::base64_decode(r["base64"].as_str().unwrap())).into_owned() +} + +#[test] +fn a_missing_link_is_a_preflight_error_and_exports_its_proxy() { + let dir = Dir::new("missing"); + let file = dir.0.join("art.png"); + let (mut s, _) = linked_session(&file, &png(40, 20, false)); + std::fs::remove_file(&file).unwrap(); + let issues = s.execute("preflight.run", &json!({})).unwrap(); + assert!(issues.to_string().contains("missingLink"), "{issues}"); + let r = s.execute("file.exportPdf", &json!({})).unwrap(); + assert!(warnings(&r).contains("art.png") && warnings(&r).contains("preview"), "{}", warnings(&r)); + assert!(pdf_text(&r).contains("/Subtype /Image") || pdf_text(&r).contains("/Subtype/Image"), "the proxy is in the PDF"); +} + +#[test] +fn a_modified_link_is_a_preflight_warning_and_exports_the_file_on_disk() { + let dir = Dir::new("modified"); + let file = dir.0.join("art.png"); + let (mut s, _) = linked_session(&file, &png(40, 20, false)); + std::fs::write(&file, png(77, 33, false)).unwrap(); + let issues = s.execute("preflight.run", &json!({})).unwrap(); + assert!(issues.to_string().contains("modifiedLink"), "{issues}"); + let r = s.execute("file.exportPdf", &json!({})).unwrap(); + assert!(warnings(&r).contains("file on disk"), "{}", warnings(&r)); + let text = pdf_text(&r); + assert!(text.contains("/Width 77") || text.contains("/Width%2077") || text.contains("/Width\n77"), "the new file's pixels are in the PDF"); +} + +#[test] +fn a_package_copies_a_linked_file_from_disk_and_links_to_the_copy() { + let dir = Dir::new("package"); + let file = dir.0.join("art.png"); + let bytes = png(40, 20, false); + let (mut s, id) = linked_session(&file, &bytes); + let out = dir.0.join("out"); + s.execute("file.package", &json!({"dir": out.to_string_lossy(), "pdf": false, "idml": false})).unwrap(); + let copy = out.join("Links/art.png"); + assert_eq!(std::fs::read(©).unwrap(), bytes); + let doc = std::fs::read_dir(&out) + .unwrap() + .filter_map(Result::ok) + .map(|e| e.path()) + .find(|p| p.extension().is_some_and(|e| e == "designcraft")) + .unwrap(); + std::fs::remove_file(&file).unwrap(); + let mut t = Session::new(); + t.execute("file.open", &json!({"path": doc.to_string_lossy()})).unwrap(); + let a = &t.doc().unwrap().doc.assets[&id]; + assert_eq!((a.storage, a.link_relative.as_deref()), (Storage::Linked, Some("Links/art.png"))); + assert_eq!(a.data.load().unwrap().as_slice(), bytes.as_slice()); +} + +#[test] +fn opening_a_document_with_a_linked_alpha_wrap_image_reads_no_original() { + let dir = Dir::new("alpha"); + let file = dir.0.join("art.png"); + let (mut s, _) = linked_session(&file, &png(64, 64, true)); + let image = s.doc().unwrap().doc.spreads[0].items[0].id.0; + s.execute("object.textWrap", &json!({"ids": [image], "mode": "contour", "contour": "alphaChannel"})).unwrap(); + let path = dir.0.join("doc.designcraft"); + s.execute("file.save", &json!({"path": path.to_string_lossy()})).unwrap(); + let mut t = Session::new(); + t.execute("file.open", &json!({"path": path.to_string_lossy()})).unwrap(); + let d = &t.doc().unwrap().doc; + let a = d.assets.values().next().unwrap(); + assert!(a.data.is_linked() && a.data.resident().is_none(), "no original was read"); + let o = d.alpha_outlines.first().expect("the outline is traced from the proxy"); + assert_eq!((o.width, o.height), (64, 64), "scaled to the original's size"); +} + +#[test] +fn epub_and_html_carry_a_linked_graphic_and_a_modified_one_from_disk() { + let dir = Dir::new("epub"); + let file = dir.0.join("art.png"); + let (mut s, _) = linked_session(&file, &png(40, 20, false)); + let html = s.execute("file.exportHtml", &json!({})).unwrap(); + assert!(html["text"].as_str().unwrap().contains(" = z.file_names().map(str::to_string).collect(); + let image = names.iter().find(|n| n.starts_with("OEBPS/images/")).unwrap_or_else(|| panic!("no image in {names:?}")).clone(); + // A modified link exports the file on disk, with a warning. + std::fs::write(&file, png(77, 33, false)).unwrap(); + let r = s.execute("file.exportEpub", &json!({})).unwrap(); + assert!(warnings(&r).contains("file on disk"), "{}", warnings(&r)); + let mut z = zip::ZipArchive::new(std::io::Cursor::new(crate::cmd::base64_decode(r["base64"].as_str().unwrap()))).unwrap(); + let mut bytes = Vec::new(); + std::io::Read::read_to_end(&mut z.by_name(&image).unwrap(), &mut bytes).unwrap(); + assert_eq!(bytes, std::fs::read(&file).unwrap()); +} + +#[test] +fn a_package_copies_a_modified_link_from_disk_with_a_warning() { + let dir = Dir::new("package-modified"); + let file = dir.0.join("art.png"); + let (mut s, _) = linked_session(&file, &png(40, 20, false)); + let changed = png(77, 33, false); + std::fs::write(&file, &changed).unwrap(); + let out = dir.0.join("out"); + let r = s.execute("file.package", &json!({"dir": out.to_string_lossy(), "pdf": false, "idml": false})).unwrap(); + assert_eq!(std::fs::read(out.join("Links/art.png")).unwrap(), changed); + assert!(r.to_string().contains("file on disk"), "{r}"); +} diff --git a/crates/engine/src/cmd/export/pdf.rs b/crates/engine/src/cmd/export/pdf.rs index a11c4e22a..09a9b9a1e 100644 --- a/crates/engine/src/cmd/export/pdf.rs +++ b/crates/engine/src/cmd/export/pdf.rs @@ -712,4 +712,37 @@ mod overset_tests { let r = s.execute("file.exportPdf", &json!({"pages": [1], "spreads": true})).unwrap(); assert!(about(&r).is_empty(), "the cover's spread holds no overset frame: {r}"); } + + /// Plain print exports attach `/DefaultCMYK` without `/OutputIntents`, PDF/X-4 keeps both + /// cleanly, and PDF/X-1a omits `/DefaultCMYK`. + #[test] + fn export_pdf_command_attaches_default_cmyk_according_to_standard() { + let mut s = Session::new(); + s.execute("file.new", &json!({"intent": "print"})).unwrap(); + s.execute("frame.create", &json!({"rect": [36, 36, 200, 200]})).unwrap(); + s.execute("swatch.create", &json!({"name": "Slate", "color": {"c": 42.5, "m": 17.5, "y": 16.0, "k": 7.1}})).unwrap(); + s.execute("object.fill", &json!({"swatch": "Slate"})).unwrap(); + + let export = |s: &mut Session, extra: serde_json::Value| { + let res = s.execute("file.exportPdf", &extra).unwrap(); + let raw = crate::cmd::base64_decode(res["base64"].as_str().unwrap()); + (raw, res["warnings"].clone()) + }; + + let (plain, plain_w) = export(&mut s, json!({"tagged": true})); + assert_eq!(plain_w, json!([]), "plain export warnings: {plain_w}"); + let plain_text = String::from_utf8_lossy(&plain); + assert!(plain_text.contains("/DefaultCMYK ["), "plain export must carry /DefaultCMYK"); + assert!(!plain_text.contains("/OutputIntents"), "plain export must not carry /OutputIntents"); + + let (x4, x4_w) = export(&mut s, json!({"standard": "x4"})); + assert_eq!(x4_w, json!([]), "PDF/X-4 warnings: {x4_w}"); + let x4_text = String::from_utf8_lossy(&x4); + assert!(x4_text.contains("/DefaultCMYK ["), "PDF/X-4 export must carry /DefaultCMYK"); + assert!(x4_text.contains("/OutputIntents"), "PDF/X-4 export must keep /OutputIntents"); + + let (x1a, _) = export(&mut s, json!({"standard": "x1a"})); + let x1a_text = String::from_utf8_lossy(&x1a); + assert!(!x1a_text.contains("/DefaultCMYK"), "PDF/X-1a must not carry ICCBased DefaultCMYK"); + } } diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 44aaa85b7..b8429f860 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -21,7 +21,7 @@ mod snippets; pub use bytes::{DOCUMENT_SNIFF_BYTES, base64_decode, base64_encode, from_bytes, is_document, to_bytes, with_document_extension}; pub(crate) use open::load_document_fonts; pub use open::{RecentPreview, recent_preview}; -pub(crate) use place::{crop_pdf, read_source, validate_placement}; +pub(crate) use place::{crop_pdf, crop_pdf_from, document_of, prepare_placed, read_source, validate_placement}; pub use registry::specs; pub(crate) use snippets::{snippet_bytes, snippet_place}; diff --git a/crates/engine/src/cmd/file/bytes.rs b/crates/engine/src/cmd/file/bytes.rs index 5fd6d4e0b..028937364 100644 --- a/crates/engine/src/cmd/file/bytes.rs +++ b/crates/engine/src/cmd/file/bytes.rs @@ -21,7 +21,7 @@ fn save_bytes(d: &Document, preview: Option<&[u8]>) -> Result> { pub fn with_document_extension(path: &str) -> String { let p = std::path::Path::new(path); let ext = p.extension().and_then(std::ffi::OsStr::to_str); - if p.file_name().is_none() || ext.is_some_and(|e| e.eq_ignore_ascii_case(designcraft_format::EXTENSION)) { + if p.file_name().is_none() || ext.is_some_and(|e| craft_file_formats::from_extension(e) == Some(craft_file_formats::Format::DesignCraft)) { return path.to_string(); } format!("{path}.{}", designcraft_format::EXTENSION) @@ -32,7 +32,7 @@ pub fn with_document_extension(path: &str) -> String { /// that kept it would name the file `Brochure.designcraft.designcraft`. A bare `.designcraft` stays. pub(super) fn without_native_extension(name: &str) -> &str { match name.rsplit_once('.') { - Some((stem, ext)) if !stem.is_empty() && ext.eq_ignore_ascii_case(designcraft_format::EXTENSION) => stem, + Some((stem, ext)) if !stem.is_empty() && craft_file_formats::from_extension(ext) == Some(craft_file_formats::Format::DesignCraft) => stem, _ => name, } } @@ -41,7 +41,10 @@ pub(super) fn without_native_extension(name: &str) -> &str { /// The first [`DOCUMENT_SNIFF_BYTES`] of a file are enough for packages that start with their /// `mimetype` entry, as both formats write them. pub fn is_document(bytes: &[u8]) -> bool { - designcraft_idml::is_idml(bytes) || designcraft_format::starts_designcraft(bytes) || designcraft_format::is_designcraft(bytes) + matches!( + craft_file_formats::detect("", bytes), + Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson | craft_file_formats::Format::Idml) + ) } /// How much of a file [`is_document`] needs to recognise a package by its `mimetype` entry. @@ -80,19 +83,7 @@ pub(super) fn load_bytes(b: &[u8]) -> Result<(Document, Vec)> { Ok((d, warnings)) } -const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -pub fn base64_encode(data: &[u8]) -> String { - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for c in data.chunks(3) { - let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32; - out.push(B64[(n >> 18) as usize & 63] as char); - out.push(B64[(n >> 12) as usize & 63] as char); - out.push(if c.len() > 1 { B64[(n >> 6) as usize & 63] as char } else { '=' }); - out.push(if c.len() > 2 { B64[n as usize & 63] as char } else { '=' }); - } - out -} +pub use craft_mime::transfer::base64 as base64_encode; pub fn base64_decode(s: &str) -> Vec { let mut out = Vec::with_capacity(s.len() * 3 / 4); @@ -178,3 +169,20 @@ mod extension_tests { let _ = std::fs::remove_dir_all(&dir); } } + +#[cfg(test)] +mod shared_base64_tests { + use super::{base64_decode, base64_encode}; + + #[test] + fn serialization_encoding_preserves_all_bytes_and_padding() { + let bytes: Vec = (0..=255).collect(); + for n in 0..=bytes.len() { + let encoded = base64_encode(&bytes[..n]); + assert_eq!(encoded.len(), n.div_ceil(3) * 4); + assert!(!encoded.contains(['\r', '\n'])); + assert_eq!(base64_decode(&encoded), bytes[..n]); + } + assert_eq!(base64_encode(&[0xfb, 0xff, 0xff]), "+///"); + } +} diff --git a/crates/engine/src/cmd/file/eps_place_tests.rs b/crates/engine/src/cmd/file/eps_place_tests.rs index 2501a31d3..8a78422a3 100644 --- a/crates/engine/src/cmd/file/eps_place_tests.rs +++ b/crates/engine/src/cmd/file/eps_place_tests.rs @@ -167,8 +167,8 @@ fn eps_idml_roundtrip_preserves_source_and_resource_type() { 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()); + assert_eq!(a.data.load().unwrap().as_slice(), bytes); + assert!(designcraft_render::decode_pixmap(&a.data.load().unwrap()).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(); @@ -186,7 +186,7 @@ fn eps_idml_roundtrip_preserves_source_and_resource_type() { .unwrap(); let a = resolved.assets.values().next().unwrap(); assert_eq!(a.mime, "application/postscript"); - assert_eq!(a.data.as_slice(), bytes); + assert_eq!(a.data.load().unwrap().as_slice(), bytes); } } @@ -197,7 +197,7 @@ fn eps_place_cursor_retains_source_and_uses_bounding_box_size() { 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].data.load().unwrap().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(); diff --git a/crates/engine/src/cmd/file/open.rs b/crates/engine/src/cmd/file/open.rs index 529ec2d96..a2f5c709c 100644 --- a/crates/engine/src/cmd/file/open.rs +++ b/crates/engine/src/cmd/file/open.rs @@ -84,24 +84,33 @@ fn open_path(s: &mut Session, p: &Value) -> Result { let bytes = crate::io::read_file(path).map_err(EngineError::Other)?; // The content decides first: a file's extension may be missing or wrong. The extension // then routes files whose content isn't recognised to the reader that explains why. - let ext = std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default().to_ascii_lowercase(); - let native = designcraft_format::is_designcraft(&bytes) || bytes.trim_ascii_start().starts_with(b"{"); - if designcraft_idml::is_idml(&bytes) || (!native && (crate::cmd::interchange::is_indd(path, &bytes) || ext == "idml")) { + let format = craft_file_formats::detect(path, &bytes); + let native = matches!( + craft_file_formats::detect("", &bytes), + Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson) + ); + let legacy = (format == Some(craft_file_formats::Format::Indd) && !native).then(|| load_bytes(&bytes).ok()).flatten(); + if format == Some(craft_file_formats::Format::Idml) || (!native && format == Some(craft_file_formats::Format::Indd) && legacy.is_none()) { return crate::cmd::interchange::open_idml(s, p); } - if !native && ext != designcraft_format::EXTENSION { + if !native && legacy.is_none() { return Err(bad("file.open", format!("{path}: not a DesignCraft or IDML file"))); } - let (mut d, mut warnings) = load_bytes(&bytes)?; + let (mut d, mut warnings) = match legacy { + Some(loaded) => loaded, + None => load_bytes(&bytes)?, + }; crate::cmd::interchange::resolve_packaged_links(&mut d, Some(std::path::Path::new(path))); crate::cmd::datamerge::resolve_sources_on_open(&mut d, Some(std::path::Path::new(path))); + let links = crate::cmd::native_links::resolve(&mut d, Some(std::path::Path::new(path)), true); let (fonts, faces, font_warnings) = load_document_fonts(&mut d, path); warnings.extend(font_warnings); warnings.extend(crate::cmd::style::grep_style_warnings(&d)); let mut st = DocState::new(d, Some(path.to_string())); st.fonts = fonts; + let checks = crate::cmd::native_links::start(&mut st, links); let i = s.add_document(st); - Ok(json!({"index": i, "documentFonts": faces, "warnings": warnings})) + Ok(json!({"index": i, "documentFonts": faces, "warnings": warnings, "linkChecks": checks})) } #[cfg(target_arch = "wasm32")] { @@ -156,17 +165,29 @@ pub(super) fn file_open_bytes(s: &mut Session, p: &Value) -> Result { let b = base64_decode(str_param(p, "base64").unwrap_or("")); // IDML packages are recognised by their stored `mimetype` first entry, INDD files by their // signature (or their name). - if designcraft_idml::is_idml(&b) || crate::cmd::interchange::is_indd(str_param(p, "name").unwrap_or_default(), &b) { + let name = str_param(p, "name").unwrap_or_default(); + let format = craft_file_formats::detect(name, &b); + let native = + matches!(craft_file_formats::detect("", &b), Some(craft_file_formats::Format::DesignCraft | craft_file_formats::Format::DesignCraftJson)); + let legacy = (format == Some(craft_file_formats::Format::Indd) && !native).then(|| load_bytes(&b).ok()).flatten(); + if format == Some(craft_file_formats::Format::Idml) || (!native && format == Some(craft_file_formats::Format::Indd) && legacy.is_none()) { return crate::cmd::interchange::open_idml(s, p); } - let (mut d, warnings) = load_bytes(&b)?; + let (mut d, warnings) = match legacy { + Some(loaded) => loaded, + None => load_bytes(&b)?, + }; crate::cmd::datamerge::resolve_sources_on_open(&mut d, None); + // No file to resolve relative links against, and nothing moved to links. + let links = crate::cmd::native_links::resolve(&mut d, None, false); if let Some(n) = str_param(p, "name") { // Titled as an IDML file opened from bytes is: without its extension. d.title = without_native_extension(n).to_string(); } - let i = s.add_document(DocState::new(d, None)); - Ok(json!({"index": i, "warnings": warnings})) + let mut st = DocState::new(d, None); + let checks = crate::cmd::native_links::start(&mut st, links); + let i = s.add_document(st); + Ok(json!({"index": i, "warnings": warnings, "linkChecks": checks})) } #[cfg(test)] @@ -507,7 +528,17 @@ mod open_bytes_tests { maker.execute("file.new", &json!({})).unwrap(); let idml = designcraft_idml::export_idml(&maker.doc().unwrap().doc); let base64 = super::super::bytes::base64_encode(&idml); - let legacy = super::super::bytes::base64_encode(&serde_json::to_vec(&maker.doc().unwrap().doc).unwrap()); + let legacy_bytes = serde_json::to_vec(&maker.doc().unwrap().doc).unwrap(); + let legacy = super::super::bytes::base64_encode(&legacy_bytes); + #[cfg(not(target_arch = "wasm32"))] + { + let legacy_path = std::env::temp_dir().join(format!("dc-legacy-indd-{}.indd", std::process::id())); + std::fs::write(&legacy_path, &legacy_bytes).unwrap(); + let mut by_path = Session::new(); + by_path.execute("file.open", &json!({"path": legacy_path})).unwrap(); + assert_eq!(by_path.documents().len(), 1, "legacy JSON must win over an InDesign extension"); + let _ = std::fs::remove_file(&legacy_path); + } for name in ["Brochure.indd", "Brochure.INDT"] { let mut s = Session::new(); s.execute("file.openBytes", &json!({"name": name, "base64": legacy})).unwrap(); diff --git a/crates/engine/src/cmd/file/place.rs b/crates/engine/src/cmd/file/place.rs index e0d285702..7a8a37b95 100644 --- a/crates/engine/src/cmd/file/place.rs +++ b/crates/engine/src/cmd/file/place.rs @@ -4,9 +4,10 @@ use super::EngineError; use super::bytes::base64_encode; use super::bytes::{base64_decode, from_bytes}; use super::{ - Affine, Arc, Asset, AssetId, Content, Graphic, Item, ItemId, PdfCrop, Rect, Result, Selection, Session, Shape, Value, bad, json, ok, shapes, - str_param, + Affine, Asset, AssetId, Content, Graphic, Item, ItemId, PdfCrop, Rect, Result, Selection, Session, Shape, Value, bad, json, ok, shapes, str_param, }; +use crate::cmd::links::{FileLink, give_placed}; +use crate::cmd::native_links::Pending; pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { // The file's size and time before it is read (a linked text file remembers them). @@ -32,15 +33,23 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { return Err(bad("file.place", "the frame is locked")); } // A page of another layout (IDML or DesignCraft): its objects, as one group. - let lname = name.to_lowercase(); - if lname.ends_with(".idml") || lname.ends_with(".designcraft") { - return place_layout_page(s, p, &name, &bytes); + let format = craft_file_formats::detect(&name, &bytes); + if matches!(format, Some(craft_file_formats::Format::Idml | craft_file_formats::Format::DesignCraft)) { + return place_layout_page(s, p, &bytes, format); } // Keep the EPS source for link comparison; rendering uses its preview at the bounding box size. let eps_size = eps_size(&bytes, "file.place")?; + // A file placed from a path links to it unless `embed`. + let embed = p.get("embed").and_then(Value::as_bool) == Some(true); + let document = document_of(s); // Video and sound: a media frame (Window › Interactive › Media). if let Some(mime) = designcraft_doc::media_mime(&name) { - return crate::cmd::media::place_media(s, p, name, mime, bytes, link); + let file = link.as_deref().map(|l| FileLink::of(std::path::Path::new(l), before)); + let mut asset = Asset { name, mime: mime.to_string(), ..Default::default() }; + let pending = give_placed(&mut asset, bytes, file, embed, document.as_deref()); + let r = crate::cmd::media::place_media(s, p, asset)?; + prepare_placed(s, pending); + return Ok(r); } // Text files (plain, Word, RTF) flow into frames. if designcraft_textimport::is_text_file(&name) { @@ -57,11 +66,11 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { }) .transpose()?; // Image Import Options: which page of a PDF (1-based). - if designcraft_render::is_pdf(&bytes) { + if format == Some(craft_file_formats::Format::Pdf) { designcraft_render::check_pdf_input_size(bytes.len()).map_err(|e| bad("file.place", e.to_string()))?; } let pdf_page = match p.get("pdfPage").and_then(Value::as_u64) { - Some(n) if designcraft_render::is_pdf(&bytes) => { + Some(n) if format == Some(craft_file_formats::Format::Pdf) => { let count = designcraft_render::pdf_page_count(&bytes).unwrap_or(1); let invalid_page = || bad("file.place", format!("the PDF has {count} page(s)")); let one_based = usize::try_from(n).map_err(|_| invalid_page())?; @@ -83,35 +92,31 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { // and SVGs keep their exact size: rounding it to whole points would stretch them unevenly. let (nw, nh) = eps_size.or_else(|| designcraft_render::natural_size(&bytes, pdf_page as usize)).unwrap_or((pw as f64, ph as f64)); // Image Import Options › Crop to: the graphic spans that box of the PDF page. - let is_pdf = designcraft_render::is_pdf(&bytes); + let is_pdf = format == Some(craft_file_formats::Format::Pdf); let pdf_crop = match str_param(p, "pdfCrop") { Some(k) if is_pdf => PdfCrop::from_name(k) .ok_or_else(|| bad("file.place", format!("unknown pdfCrop `{k}` (crop, art, trim, bleed, media, contentVisible, contentAll)")))?, _ => PdfCrop::Crop, }; let mime = designcraft_render::image_mime(&bytes).to_string(); - let mut asset = Asset { - page: pdf_page, - name, - mime, - link, - link_fingerprint: crate::cmd::links::loaded_fingerprint(before, &bytes), - data: Arc::new(bytes), - pixels: Some((pw, ph)), - pdf_crop, - ..Default::default() - }; + let file = link.as_deref().map(|l| FileLink::of(std::path::Path::new(l), before)); + let mut asset = Asset { page: pdf_page, name, mime, pixels: Some((pw, ph)), pdf_crop, ..Default::default() }; // The graphic's natural size: the box in points (the whole page when the box can't be found, // and then the document says so). - let (bw, bh) = match crop_pdf(&mut asset) { + let cropped = crop_pdf_from(&mut asset, &bytes); + let pending = give_placed(&mut asset, bytes, file, embed, document.as_deref()); + let (bw, bh) = match cropped { Some(size) => size, None => { asset.pdf_crop = PdfCrop::Crop; (nw, nh) } }; + // A file joining the place cursor's queue goes after the ones already there, whatever is selected. + let load_cursor_param = p.get("loadCursor").and_then(Value::as_bool) == Some(true); + let append = p.get("append").and_then(Value::as_bool) == Some(true); let target_frame = crate::cmd::id_param(p, "frame").or_else(|| { - if ["x", "y", "spread", "width"].iter().any(|key| p.get(*key).is_some_and(|value| !value.is_null())) { + if (load_cursor_param && append) || ["x", "y", "spread", "width"].iter().any(|key| p.get(*key).is_some_and(|value| !value.is_null())) { return None; } let st = s.active()?; @@ -125,10 +130,12 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { let lid = s.doc()?.active_layer; let px = p.get("x").and_then(Value::as_f64); let py = p.get("y").and_then(Value::as_f64); - if target_frame.is_none() && px.is_none() && py.is_none() && p.get("loadCursor").and_then(Value::as_bool) == Some(true) { - return load_cursor(s, asset, (bw, bh)); + if target_frame.is_none() && px.is_none() && py.is_none() && load_cursor_param { + let r = load_cursor(s, asset, (bw, bh), append)?; + prepare_placed(s, pending); + return Ok(r); } - s.edit(|d, sel| { + let r = s.edit(|d, sel| { let fresh = AssetId(d.alloc()); let aid = d.add_asset(Asset { id: fresh, ..asset }); // The width the whole page (or image) gets; the height follows the proportions. @@ -191,32 +198,77 @@ pub(super) fn file_place(s: &mut Session, p: &Value) -> Result { }; *sel = Selection::items(vec![id]); Ok(json!({"id": id.0, "asset": aid.0})) - }) + })?; + prepare_placed(s, pending); + Ok(r) +} + +/// Have the hash and proxy of graphics just placed into the active document made in the +/// background ([`crate::cmd::native_links::prepare`]). +pub(crate) fn prepare_placed(s: &mut Session, pending: impl IntoIterator) { + if let Ok(st) = s.doc_mut() { + crate::cmd::native_links::prepare(st, pending.into_iter().collect()); + } } /// Sets `a.pdf_box` to where its `pdf_crop` box sits on its page and returns the box's size in /// points. The crop box is the whole page and has none; nor has a page that can't be read, which -/// keeps its crop choice (a missing link relinked later gets its box then). +/// keeps its crop choice (a missing link relinked later gets its box then). Only a PDF with +/// another box chosen is read. pub(crate) fn crop_pdf(a: &mut Asset) -> Option<(f64, f64)> { a.pdf_box = None; - if a.pdf_crop == PdfCrop::Crop || !designcraft_render::is_pdf(&a.data) { + if !crops_pdf(a) { return None; } - let (frac, size) = designcraft_render::pdf_crop_box(&a.data, a.page as usize, a.pdf_crop)?; + let bytes = a.data.load().ok()?; + crop_pdf_from(a, &bytes) +} + +/// [`crop_pdf`] with `a`'s bytes, `bytes`, in hand. +pub(crate) fn crop_pdf_from(a: &mut Asset, bytes: &[u8]) -> Option<(f64, f64)> { + a.pdf_box = None; + if !crops_pdf(a) || !designcraft_render::is_pdf(bytes) { + return None; + } + let (frac, size) = designcraft_render::pdf_crop_box(bytes, a.page as usize, a.pdf_crop)?; a.pdf_box = Some(frac); Some(size) } -pub(crate) fn read_source(p: &Value) -> Result<(Vec, String, Option)> { - read_linked_source(p).map(|(bytes, name, link, _)| (bytes, name, link)) +/// Whether `a` is a PDF placed with a box other than its crop box, which only its bytes locate. +fn crops_pdf(a: &Asset) -> bool { + a.pdf_crop != PdfCrop::Crop && a.mime == "application/pdf" } -/// [`read_source`], with the size and time to keep with a linked file's bytes -/// ([`crate::cmd::links::loaded_fingerprint`]): the file's from before it is read, not those of a -/// later version that replaces it while a network or syncing source is being loaded. -fn read_linked_source(p: &Value) -> Result<(Vec, String, Option, Option)> { +/// A file to place: its bytes and name, and its link record when it was read from a path. +pub(crate) struct Source { + pub bytes: Vec, + pub name: String, + file: Option, + /// `embed: true`: the bytes are kept in the document instead of linking the file. + embed: bool, +} + +impl Source { + /// Give `a` these bytes: linked to the file they were read from once the returned work is + /// done, else embedded ([`give_placed`]). `a`'s `mime` and `page` are set. + pub(crate) fn give(self, a: &mut Asset, document: Option<&std::path::Path>) -> Option { + give_placed(a, self.bytes, self.file, self.embed, document) + } +} + +/// The file of the document open in `s`, which relative links are recorded against. +pub(crate) fn document_of(s: &Session) -> Option { + s.doc().ok()?.path.as_deref().map(std::path::PathBuf::from) +} + +/// The file `p` names: `{path, embed?}` or `{base64, name?}`. Its size and time are taken before +/// it is read, so a later version that replaces it while a network or syncing source is being +/// loaded isn't vouched for ([`crate::cmd::links::loaded_fingerprint`]). +pub(crate) fn read_source(p: &Value) -> Result { + let embed = p.get("embed").and_then(Value::as_bool) == Some(true); if let Some(b) = str_param(p, "base64") { - return Ok((base64_decode(b), str_param(p, "name").unwrap_or("image").to_string(), None, None)); + return Ok(Source { bytes: base64_decode(b), name: str_param(p, "name").unwrap_or("image").to_string(), file: None, embed }); } let path = str_param(p, "path").ok_or_else(|| bad("place", "missing `path` or `base64`"))?; let before = crate::cmd::datamerge::fingerprint_from_path(path); @@ -225,8 +277,8 @@ fn read_linked_source(p: &Value) -> Result<(Vec, String, Option, Opt #[cfg(target_arch = "wasm32")] let b: Vec = vec![]; let name = std::path::Path::new(path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.into()); - let loaded = crate::cmd::links::loaded_fingerprint(before, &b); - Ok((b, name, Some(path.to_string()), loaded)) + let file = Some(FileLink::of(std::path::Path::new(path), before)); + Ok(Source { bytes: b, name, file, embed }) } fn eps_size(bytes: &[u8], command: &str) -> Result> { @@ -254,35 +306,126 @@ pub(crate) fn validate_placement(size: (f64, f64), frame: Rect, xf: Affine, comm Ok(()) } +/// Decode and validate the complete batch before replacing the placement cursor. pub(super) fn place_load(s: &mut Session, p: &Value) -> Result { - let (bytes, name, link, link_fingerprint) = read_linked_source(p)?; - let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad("place.load", "unsupported or corrupt image"))?; - 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, link_fingerprint, data: Arc::new(bytes), pixels: Some((pw, ph)), ..Default::default() }; - load_cursor(s, asset, natural) + let append = p.get("append").and_then(Value::as_bool) == Some(true); + let embed = p.get("embed").and_then(Value::as_bool) == Some(true); + let document = document_of(s); + if p.get("files").is_none() { + let source = read_source(p)?; + let bytes = &source.bytes; + let (pw, ph) = designcraft_render::image_size(bytes).ok_or_else(|| bad("place.load", "unsupported or corrupt image"))?; + let natural = eps_size(bytes, "place.load")?.or_else(|| designcraft_render::natural_size(bytes, 0)).unwrap_or((pw as f64, ph as f64)); + let mut asset = Asset { + name: source.name.clone(), + mime: designcraft_render::image_mime(bytes).to_string(), + pixels: Some((pw, ph)), + ..Default::default() + }; + let pending = source.give(&mut asset, document.as_deref()); + let r = load_cursor(s, asset, natural, append)?; + prepare_placed(s, pending); + return Ok(r); + } + let sources = p.get("files").and_then(Value::as_array).ok_or_else(|| bad("place.load", "files must be an array"))?; + if sources.is_empty() || sources.len() > MAX_LOADED_IMAGES { + return Err(bad("place.load", format!("select between 1 and {MAX_LOADED_IMAGES} images"))); + } + check_cursor_room(s, sources.len(), append, "place.load")?; + let mut images = Vec::with_capacity(sources.len()); + let mut pending = Vec::new(); + for source in sources { + let mut source = read_source(source)?; + source.embed |= embed; + let bytes = &source.bytes; + let (pw, ph) = + designcraft_render::image_size(bytes).ok_or_else(|| bad("place.load", format!("unsupported or corrupt image: {}", source.name)))?; + let size = eps_size(bytes, "place.load")?.or_else(|| designcraft_render::natural_size(bytes, 0)).unwrap_or((pw as f64, ph as f64)); + validate_placement(size, Rect::new(0.0, 0.0, size.0, size.1), Affine::IDENTITY, "place.load")?; + let mut asset = Asset { + name: source.name.clone(), + mime: designcraft_render::image_mime(bytes).to_string(), + pixels: Some((pw, ph)), + ..Default::default() + }; + pending.extend(source.give(&mut asset, document.as_deref())); + images.push((asset, size)); + } + let queue = s.edit(|d, _| { + let mut queue = std::collections::VecDeque::with_capacity(images.len()); + for (mut asset, size) in images { + asset.id = AssetId(d.alloc()); + queue.push_back(crate::LoadedGraphic { asset: d.add_asset(asset), size }); + } + Ok(queue) + })?; + prepare_placed(s, pending); + if append && s.loaded.is_some() { + s.loaded_queue.extend(queue); + } else { + s.loaded_queue = queue; + s.loaded = s.loaded_queue.pop_front(); + } + s.set_tool("placeGun"); + Ok(json!({"remaining": loaded_count(s), "loaded": true})) } -/// Adds `asset` to the document (with a fresh id) and loads it into the place cursor. -fn load_cursor(s: &mut Session, mut asset: Asset, size: (f64, f64)) -> Result { +/// Most images the place cursor holds, and so most one `place.load` takes. +const MAX_LOADED_IMAGES: usize = 1000; + +/// Whether `incoming` images fit in the place cursor: appended, they go after the ones it holds. +fn check_cursor_room(s: &Session, incoming: usize, append: bool, command: &str) -> Result<()> { + let held = if append { loaded_count(s) } else { 0 }; + if held.saturating_add(incoming) > MAX_LOADED_IMAGES { + return Err(bad(command, format!("the place cursor holds at most {MAX_LOADED_IMAGES} images, and it has {held}"))); + } + Ok(()) +} + +/// How many images the place cursor holds: the current one and those queued after it. +fn loaded_count(s: &Session) -> usize { + s.loaded_queue.len().saturating_add(usize::from(s.loaded.is_some())) +} + +/// Add a decoded asset with a fresh id, replacing the previous placement queue (or, with +/// `append` and an image already loaded, queued after it). +fn load_cursor(s: &mut Session, mut asset: Asset, size: (f64, f64), append: bool) -> Result { + check_cursor_room(s, 1, append, "place.load")?; let name = asset.name.clone(); let (pw, ph) = asset.pixels.unwrap_or_default(); let aid = s.edit(|d, _| { asset.id = AssetId(d.alloc()); Ok(d.add_asset(asset)) })?; - s.loaded = Some(crate::LoadedGraphic { asset: aid, size }); + let image = crate::LoadedGraphic { asset: aid, size }; + if append && s.loaded.is_some() { + s.loaded_queue.push_back(image); + } else { + s.loaded_queue.clear(); + s.loaded = Some(image); + } s.set_tool("placeGun"); Ok(json!({"asset": aid.0, "name": name, "width": pw, "height": ph, "loaded": true})) } -pub(super) fn place_drop(s: &mut Session, p: &Value) -> Result { - let crate::LoadedGraphic { asset: aid, size: (nw, nh) } = s.loaded.ok_or_else(|| bad("place.drop", "nothing loaded in the place cursor"))?; - // Undo can remove the loaded graphic's asset. - if !s.doc()?.doc.assets.contains_key(&aid) { - s.loaded = None; - return Err(bad("place.drop", "the loaded graphic is no longer in the document")); +/// Cancel every pending image if Undo removed an asset used by the cursor. +fn validate_place_cursor(s: &mut Session, command: &str) -> Result<()> { + // An empty cursor is its own error, and leaves the tool as it is. + if s.loaded.is_none() { + return Err(bad(command, "nothing loaded in the place cursor")); } + let valid = s.loaded.iter().chain(s.loaded_queue.iter()).all(|image| s.doc().is_ok_and(|d| d.doc.assets.contains_key(&image.asset))); + if !valid { + s.unload_place_cursor(); + return Err(bad(command, "the loaded graphic is no longer in the document")); + } + Ok(()) +} + +/// Place the current image and advance the cursor only after success. +pub(super) fn place_drop(s: &mut Session, p: &Value) -> Result { + validate_place_cursor(s, "place.drop")?; + let crate::LoadedGraphic { asset: aid, size: (nw, nh) } = s.loaded.ok_or_else(|| bad("place.drop", "nothing loaded in the place cursor"))?; let sr = crate::cmd::spread_param(p, "place.drop")?; let lid = s.doc()?.active_layer; let frame = crate::cmd::id_param(p, "frame"); @@ -340,16 +483,189 @@ pub(super) fn place_drop(s: &mut Session, p: &Value) -> Result { *sel = Selection::items(vec![id]); Ok(json!({"id": id.0})) })?; - s.loaded = None; - s.set_tool("selection"); + advance_place_cursor(s); Ok(r) } +/// Advance in file order, returning to selection when the queue is exhausted. +fn advance_place_cursor(s: &mut Session) { + s.loaded = s.loaded_queue.pop_front(); + if s.loaded.is_none() { + s.set_tool("selection"); + } +} + +/// Drop the current loaded image without placing it and move to the next; the last one ends placing. +pub(super) fn place_skip(s: &mut Session, _p: &Value) -> Result { + validate_place_cursor(s, "place.skip")?; + advance_place_cursor(s); + Ok(json!({"remaining": loaded_count(s)})) +} + +/// Rotate the loaded images: `dir` 1 makes the next one current (the current one goes to the back), +/// -1 the previous one (the last goes to the front). +pub(super) fn place_cycle(s: &mut Session, p: &Value) -> Result { + const ID: &str = "place.cycle"; + let dir = p.get("dir").and_then(Value::as_i64).filter(|d| matches!(d, 1 | -1)).ok_or_else(|| bad(ID, "dir must be 1 or -1"))?; + validate_place_cursor(s, ID)?; + if let Some(current) = s.loaded { + if dir == 1 { + s.loaded_queue.push_back(current); + s.loaded = s.loaded_queue.pop_front(); + } else if let Some(last) = s.loaded_queue.pop_back() { + s.loaded_queue.push_front(current); + s.loaded = Some(last); + } + } + Ok(json!({"remaining": loaded_count(s)})) +} + +/// Validate bounded grid dimensions and a finite nonnegative gutter. +fn grid_dimensions(p: &Value, command: &str, default_columns: usize, default_rows: usize) -> Result<(usize, usize, f64)> { + let dimension = |key, default| -> Result { + match p.get(key) { + None => Ok(default), + Some(v) => v + .as_u64() + .filter(|n| (1..=100).contains(n)) + .and_then(|n| usize::try_from(n).ok()) + .ok_or_else(|| bad(command, "rows and columns must be integers between 1 and 100")), + } + }; + let gutter = crate::cmd::f64_or(p, "gutter", 6.0); + if !gutter.is_finite() || gutter < 0.0 { + return Err(bad(command, "gutter must be finite and nonnegative")); + } + Ok((dimension("columns", default_columns)?, dimension("rows", default_rows)?, gutter)) +} + +/// Calculate row-major cells, rejecting gutters that leave no usable space. +fn grid_cells(rect: Rect, columns: usize, rows: usize, gutter: f64, command: &str) -> Result> { + let (columns, rows) = (columns.max(1), rows.max(1)); + let w = (rect.width() - gutter * columns.saturating_sub(1) as f64) / columns as f64; + let h = (rect.height() - gutter * rows.saturating_sub(1) as f64) / rows as f64; + if ![rect.x0, rect.y0, rect.x1, rect.y1, w, h].iter().all(|v| v.is_finite()) || w <= 0.0 || h <= 0.0 { + return Err(bad(command, "the grid area is too small for its rows, columns and gutter")); + } + Ok((0..rows) + .flat_map(|row| { + (0..columns).map(move |column| { + let x = rect.x0 + column as f64 * (w + gutter); + let y = rect.y0 + row as f64 * (h + gutter); + Rect::new(x, y, x + w, y + h) + }) + }) + .collect()) +} + +/// Insert a proportionally fitted image into one grid cell. +fn insert_grid_image( + d: &mut designcraft_doc::Document, + spread: designcraft_doc::SpreadRef, + layer: designcraft_doc::LayerId, + image: crate::LoadedGraphic, + rect: Rect, + command: &str, +) -> Result { + let crate::LoadedGraphic { asset, size: (nw, nh) } = image; + if !d.assets.contains_key(&asset) { + return Err(bad(command, "a loaded image is no longer in the document")); + } + let k = (rect.width() / nw).min(rect.height() / nh); + let xf = Affine::translate((rect.x0 + (rect.width() - nw * k) / 2.0, rect.y0 + (rect.height() - nh * k) / 2.0)) * Affine::scale(k); + validate_placement((nw, nh), rect, xf, command)?; + let id = ItemId(d.alloc()); + let mut item = Item::new(id, layer, Shape::Rectangle, shapes::rectangle(rect)); + item.object_style = d.styles.default_graphic_frame.clone(); + if let Some(effects) = d.styles.object_effects(&item.object_style) { + item.effects = effects.clone(); + } + item.content = Content::Graphic(Graphic { + layer_options: None, + asset, + size: (nw, nh), + xf, + auto_fit: designcraft_doc::Fitting::FitProportionally, + fit_align: 4, + crop: [0.0; 4], + clip: None, + wrap: Default::default(), + fill: Default::default(), + }); + d.insert_item(spread, item, None)?; + Ok(id) +} + +/// Place a row-major grid as one undoable edit. +pub(super) fn place_grid(s: &mut Session, p: &Value) -> Result { + const ID: &str = "place.grid"; + validate_place_cursor(s, ID)?; + let (columns, rows, gutter) = grid_dimensions(p, ID, 1, 1)?; + let rect = crate::cmd::rect_param(p, "rect").ok_or_else(|| bad(ID, "missing rect"))?; + let cells = grid_cells(rect, columns, rows, gutter, ID)?; + let images: Vec<_> = s.loaded.iter().chain(s.loaded_queue.iter()).copied().take(cells.len()).collect(); + if images.is_empty() { + return Err(bad(ID, "nothing loaded in the place cursor")); + } + let layer = s.doc()?.active_layer; + let spread = crate::cmd::spread_param(p, ID)?; + let ids = s.edit(|d, sel| { + let mut ids = Vec::with_capacity(images.len()); + for (image, rect) in images.iter().zip(cells) { + ids.push(insert_grid_image(d, spread, layer, *image, rect, ID)?); + } + *sel = Selection::items(ids.clone()); + Ok(ids) + })?; + for _ in &ids { + advance_place_cursor(s); + } + Ok(json!({"ids": ids.iter().map(|id| id.0).collect::>(), "remaining": loaded_count(s)})) +} + +/// Place all queued images on new pages as one undoable edit. +pub(super) fn place_contact_sheet(s: &mut Session, p: &Value) -> Result { + const ID: &str = "place.contactSheet"; + validate_place_cursor(s, ID)?; + let (columns, rows, gutter) = grid_dimensions(p, ID, 3, 4)?; + let margin = crate::cmd::f64_or(p, "margin", 36.0); + if !margin.is_finite() || margin < 0.0 { + return Err(bad(ID, "invalid margin")); + } + let images: Vec<_> = s.loaded.iter().chain(s.loaded_queue.iter()).copied().collect(); + if images.is_empty() { + return Err(bad(ID, "load images first using File > Place")); + } + let layer = s.doc()?.active_layer; + let per_page = columns.saturating_mul(rows).max(1); + let count = images.len().div_ceil(per_page); + let ids = s.edit(|d, sel| { + let after = d.page_count().saturating_sub(1); + d.insert_pages(Some(after), count, None)?; + let mut ids = Vec::with_capacity(images.len()); + for (page_offset, chunk) in images.chunks(per_page).enumerate() { + let index = after.saturating_add(1).saturating_add(page_offset); + let (si, pi) = d.page_loc(index).ok_or_else(|| bad(ID, "missing page"))?; + let b = d.spreads.get(si).and_then(|sp| sp.pages.get(pi)).ok_or_else(|| bad(ID, "missing page"))?.bounds(); + let cells = grid_cells(Rect::new(b.x0 + margin, b.y0 + margin, b.x1 - margin, b.y1 - margin), columns, rows, gutter, ID)?; + for (image, rect) in chunk.iter().zip(cells) { + ids.push(insert_grid_image(d, designcraft_doc::SpreadRef::Doc(si), layer, *image, rect, ID)?); + } + } + *sel = Selection::items(ids.clone()); + Ok(ids) + })?; + for _ in &ids { + advance_place_cursor(s); + } + Ok(json!({"pages": count, "images": ids.len()})) +} + /// File › Place of an IDML or DesignCraft document: the objects of one page (`layoutPage`, /// 1-based) grouped, top-left at x/y (default: the source position on the first page). -fn place_layout_page(s: &mut Session, p: &Value, name: &str, bytes: &[u8]) -> Result { +fn place_layout_page(s: &mut Session, p: &Value, bytes: &[u8], format: Option) -> Result { const ID: &str = "file.place"; - let src = if name.to_lowercase().ends_with(".idml") { + let src = if format == Some(craft_file_formats::Format::Idml) { crate::cmd::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)? @@ -414,6 +730,64 @@ pub(super) mod image_place_tests { Ok(file) } + /// With nothing in the place cursor, placing says so and leaves the tool as it is (the + /// cursor's graphic gone from the document is another error). + #[test] + fn an_empty_place_cursor_says_so_and_keeps_the_tool() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + s.set_tool("rectangleFrame"); + for (cmd, p) in + [("place.drop", json!({"x": 10, "y": 10})), ("place.grid", json!({"rect": [0, 0, 100, 100]})), ("place.contactSheet", json!({}))] + { + let e = s.execute(cmd, &p).unwrap_err().to_string(); + assert!(e.contains("nothing loaded"), "{cmd}: {e}"); + assert_eq!(s.tool_id(), "rectangleFrame", "{cmd}"); + } + } + + fn test_png(shade: u8) -> String { + let png = designcraft_render::Rendered { width: 4, height: 4, pixels: vec![shade; 4 * 4 * 4] }.to_png(); + crate::cmd::base64_encode(&png) + } + + fn loaded_names(s: &Session) -> Vec { + let Ok(st) = s.doc() else { return Vec::new() }; + let d = &st.doc; + s.loaded.iter().chain(s.loaded_queue.iter()).map(|g| d.assets[&g.asset].name.clone()).collect() + } + + #[test] + fn skip_drops_the_current_image_and_the_last_one_ends_placing() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let files: Vec<_> = (0..3).map(|i| json!({"base64": test_png(i as u8), "name": format!("{i}.png")})).collect(); + s.execute("place.load", &json!({"files": files})).unwrap(); + assert_eq!(s.execute("place.skip", &json!({})).unwrap()["remaining"], 2); + assert_eq!(loaded_names(&s), ["1.png", "2.png"]); + s.execute("place.skip", &json!({})).unwrap(); + assert_eq!(s.tool_id(), "placeGun"); + s.execute("place.skip", &json!({})).unwrap(); + assert!(s.loaded.is_none()); + assert_eq!(s.tool_id(), "selection"); + assert!(s.execute("place.skip", &json!({})).is_err()); + } + + #[test] + fn cycle_rotates_the_loaded_images_both_ways() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let files: Vec<_> = (0..3).map(|i| json!({"base64": test_png(i as u8), "name": format!("{i}.png")})).collect(); + s.execute("place.load", &json!({"files": files})).unwrap(); + s.execute("place.cycle", &json!({"dir": 1})).unwrap(); + assert_eq!(loaded_names(&s), ["1.png", "2.png", "0.png"]); + s.execute("place.cycle", &json!({"dir": -1})).unwrap(); + s.execute("place.cycle", &json!({"dir": -1})).unwrap(); + assert_eq!(loaded_names(&s), ["2.png", "0.png", "1.png"]); + assert!(s.execute("place.cycle", &json!({"dir": 2})).is_err()); + assert!(s.execute("place.cycle", &json!({})).is_err()); + } + #[test] fn consecutive_path_placements_create_distinct_graphics() { let images = [image_file_with_color(30).unwrap(), image_file_with_color(31).unwrap(), image_file_with_color(32).unwrap()]; @@ -479,9 +853,9 @@ pub(super) mod image_place_tests { }; // Written just now: another write in the same second would keep its time, so the time - // can't vouch for the bytes later and isn't kept. + // can't vouch for the bytes later: only the size is kept, with a time no file has. let asset = load(&mut session); - assert_eq!(asset.link_fingerprint, None); + assert_eq!(asset.link_fingerprint.map(|f| f.mtime), Some(crate::cmd::links::UNVERIFIED_MTIME)); assert_eq!(asset.link.as_deref(), image.path().to_str()); // A file that has been as it is for a while is remembered by its size and time. @@ -1013,3 +1387,80 @@ mod nested_frame_tests { assert_eq!(d.spreads[0].items.len(), 1, "no new object"); } } + +#[cfg(test)] +mod cursor_cap_tests { + use serde_json::{Value, json}; + + use super::MAX_LOADED_IMAGES; + use crate::Session; + + fn png() -> String { + let png = designcraft_render::Rendered { width: 4, height: 4, pixels: [200, 90, 30, 255].repeat(16) }.to_png(); + super::super::base64_encode(&png) + } + + /// A session whose place cursor holds `held` images, loaded without decoding each of them. + fn holding(held: usize) -> Session { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + s.execute("place.load", &json!({"base64": png(), "name": "first.png"})).unwrap(); + let first = s.loaded.unwrap(); + s.loaded_queue = std::iter::repeat_n(first, held - 1).collect(); + s + } + + fn count(s: &Session) -> usize { + super::loaded_count(s) + } + + /// Every way of adding to the cursor stops at the cap, and a refused add changes nothing. + #[test] + fn appending_stops_at_the_cap_on_the_whole_cursor() { + let image = png(); + let single = json!({"base64": image, "name": "a.png", "append": true}); + let placed = json!({"base64": image, "name": "a.png", "append": true, "loadCursor": true}); + let two = json!({"files": [{"base64": image, "name": "a.png"}, {"base64": image, "name": "b.png"}], "append": true}); + for (command, params) in [("place.load", &single), ("file.place", &placed), ("place.load", &two)] { + let mut s = holding(MAX_LOADED_IMAGES); + let assets = s.doc().unwrap().doc.assets.len(); + assert!(s.execute(command, params).is_err(), "{command} {params}"); + assert_eq!(count(&s), MAX_LOADED_IMAGES, "{command} {params}"); + assert_eq!(s.doc().unwrap().doc.assets.len(), assets, "{command} {params}"); + } + // One over, as a batch. + let mut s = holding(MAX_LOADED_IMAGES - 1); + assert!(s.execute("place.load", &two).is_err()); + assert_eq!(count(&s), MAX_LOADED_IMAGES - 1); + } + + /// The last places up to the cap load, and an unreadable batch reports the cap before decoding. + #[test] + fn appending_up_to_the_cap_still_loads() { + let image = png(); + for params in + [json!({"base64": image, "name": "a.png", "append": true}), json!({"files": [{"base64": image, "name": "a.png"}], "append": true})] + { + let mut s = holding(MAX_LOADED_IMAGES - 1); + s.execute("place.load", ¶ms).unwrap(); + assert_eq!(count(&s), MAX_LOADED_IMAGES, "{params}"); + } + let mut s = holding(MAX_LOADED_IMAGES); + let error = + s.execute("place.load", &json!({"files": [{"base64": "not an image", "name": "a.png"}], "append": true})).unwrap_err().to_string(); + assert!(error.contains(&MAX_LOADED_IMAGES.to_string()), "{error}"); + } + + /// Without `append` the new images replace the cursor, so what it holds doesn't count. + #[test] + fn replacing_a_full_cursor_is_not_capped() { + let image = png(); + let mut s = holding(MAX_LOADED_IMAGES); + let result: Value = s.execute("place.load", &json!({"base64": image, "name": "a.png"})).unwrap(); + assert_eq!(result["loaded"], true); + assert_eq!(count(&s), 1); + let mut s = holding(MAX_LOADED_IMAGES); + s.execute("place.load", &json!({"files": [{"base64": image, "name": "a.png"}]})).unwrap(); + assert_eq!(count(&s), 1); + } +} diff --git a/crates/engine/src/cmd/file/registry.rs b/crates/engine/src/cmd/file/registry.rs index df8709f10..f59144fe6 100644 --- a/crates/engine/src/cmd/file/registry.rs +++ b/crates/engine/src/cmd/file/registry.rs @@ -1,10 +1,8 @@ -#[cfg(not(target_arch = "wasm32"))] -use super::EngineError; -use super::bytes::{base64_encode, to_bytes, to_bytes_with_preview, with_document_extension}; +use super::bytes::{base64_encode, to_bytes}; use super::new::{file_new, file_sample, save_preset}; use super::open::{file_open, file_open_bytes, file_open_recent, has_recent, recent_entry}; -use super::place::{file_place, place_drop, place_load, read_source}; -use super::save::{FolderAccess, can_revert, file_revert, file_save, recovery_dir}; +use super::place::{file_place, place_contact_sheet, place_cycle, place_drop, place_grid, place_load, place_skip, read_source}; +use super::save::{FolderAccess, can_revert, file_revert, file_save, file_save_a_copy, recovery_dir}; use super::snippets::{snippet_export, snippet_place}; use super::{CommandSpec, PRESETS, Value, always, bad, cmd, has_doc, json, ok, str_param}; @@ -52,16 +50,7 @@ pub fn specs() -> Vec { }), cmd!(noundo "file.save", "Save", ["File"], Some("Cmd+S"), "{path?} — a `path` without the .designcraft extension gets it → {path: the file written, bytes}", has_doc, file_save), cmd!(noundo "file.saveAs", "Save As…", ["File"], Some("Cmd+Shift+S"), "{path} — a path without the .designcraft extension gets it → {path: the file written, bytes}", has_doc, file_save), - cmd!(noundo "file.saveACopy", "Save a Copy…", ["File"], None, "{path} — writes the document without changing which file it is or its unsaved state; a path without the .designcraft extension gets it → {path: the file written, bytes}", has_doc, |s, p| { - let path = str_param(p, "path").ok_or_else(|| bad("file.saveACopy", "missing `path`"))?; - let path = with_document_extension(path); - let mut copy = (*s.doc()?.doc).clone(); - crate::cmd::datamerge::refresh_relative_paths(&mut copy, std::path::Path::new(&path)); - let bytes = to_bytes_with_preview(©, &s.cache)?; - #[cfg(not(target_arch = "wasm32"))] - crate::io::write_file(&path, &bytes).map_err(EngineError::Other)?; - Ok(json!({"path": path, "bytes": bytes.len()})) - }), + cmd!(noundo "file.saveACopy", "Save a Copy…", ["File"], None, "{path, embedLinks?: copy every linked file in (fails when one is missing or changed)} — writes the document without changing which file it is or its unsaved state; a path without the .designcraft extension gets it → {path: the file written, bytes, warnings}", has_doc, file_save_a_copy), cmd!(noundo "file.revert", "Revert", ["File"], None, "{} — back to the last saved version (not undoable)", can_revert, file_revert), cmd!(query "file.recovery.list", "Recovery Data", [], None, "{dir?} → [{uid, path, title, saved}] unsaved documents left by a crash (default: the session's recovery folder)", always, |s, p| { let dir = recovery_dir(s, p, FolderAccess::Read)?; @@ -90,12 +79,15 @@ pub fn specs() -> Vec { let dir = recovery_dir(s, p, FolderAccess::ReadWrite)?; Ok(json!({"opened": crate::recovery::open(s, &dir)?})) }), - cmd!(query "file.serialize", "Serialize", [], None, "{} → {base64, bytes} the .designcraft file", has_doc, |s, _| { + cmd!(query "file.serialize", "Serialize", [], None, "{} → {base64, bytes, warnings} the .designcraft file (on the web, linked files stay links, with a warning)", has_doc, |s, _| { let st = s.doc()?; // Preview fill is session state. Serialize the unfilled template. let doc = st.preview_stash.as_deref().unwrap_or(st.doc.as_ref()); + let proxied = crate::cmd::native_links::with_proxies(doc, || false); + let doc = proxied.as_ref().unwrap_or(doc); let bytes = to_bytes(doc)?; - Ok(json!({"base64": base64_encode(&bytes), "bytes": bytes.len()})) + let warnings = crate::cmd::native_links::web_save_warnings(doc); + Ok(json!({"base64": base64_encode(&bytes), "bytes": bytes.len(), "warnings": warnings})) }), cmd!(noundo "file.close", "Close", ["File"], Some("Cmd+W"), "{index?}", has_doc, |s, p| { let i = p.get("index").and_then(Value::as_u64).map(|v| v as usize).or(s.active_index()).unwrap_or(0); @@ -111,8 +103,8 @@ pub fn specs() -> Vec { cmd!(query "place.styles", "Import Options: Styles", [], None, "{path | base64, name} → {paragraph: [name], character: [name], conflicts: [name]} — the styles a Word/RTF file brings (for styleMap / styleConflicts)", has_doc, |s, p| { - let (bytes, name, _) = read_source(p)?; - let imp = designcraft_textimport::import(&name, &bytes).map_err(|e| bad("place.styles", e.to_string()))?; + let source = read_source(p)?; + let imp = designcraft_textimport::import(&source.name, &source.bytes).map_err(|e| bad("place.styles", e.to_string()))?; let st = &s.doc()?.doc.styles; let conflicts: Vec<&str> = imp .para_styles @@ -137,17 +129,43 @@ pub fn specs() -> Vec { "Load Place Cursor", [], None, - "{path | base64, name?} — load a graphic into the place cursor (then click/drag with the placeGun tool)", + "{path | base64, name?, files?: [{path | base64, name?, embed?}], append? (keep the images already loaded and queue these after them), embed? (keep every image's bytes in the document; default: images read from a path link to their files)} — load up to 1000 images into the place cursor, in order (then click/drag with the placeGun tool)", has_doc, place_load ), cmd!("place.drop", "Place Loaded Graphic", [], None, "{spread?, x, y, rect?: [x0,y0,x1,y1], frame?: id}", has_doc, place_drop), + cmd!( + "place.grid", + "Place Image Grid", + [], + None, + "{spread?, rect: [x0,y0,x1,y1], columns?: 1, rows?: 1, gutter?: 6} — place queued images in row order", + has_doc, + place_grid + ), + cmd!( + "place.contactSheet", + "Place Contact Sheet", + ["File"], + None, + "{columns?: 3, rows?: 4, gutter?: 6, margin?: 36} — place all loaded images on new pages", + has_doc, + place_contact_sheet + ), + cmd!(noundo "place.skip", "Skip Loaded Image", [], None, "{} — drops the current loaded image without placing it and loads the next; the last one ends placing", has_doc, place_skip), + cmd!(noundo "place.cycle", "Cycle Loaded Images", [], None, "{dir: 1|-1} — makes the next (1) or previous (-1) loaded image the current one", has_doc, place_cycle), + cmd!(noundo "place.cancel", "Cancel Image Placement", [], None, "{} — clears every loaded image", has_doc, |s, _| { + s.loaded = None; + s.loaded_queue.clear(); + s.set_tool("selection"); + ok() + }), cmd!( "file.place", "Place…", ["File"], Some("Cmd+D"), - "{path?|base64?, name?, frame?: id (place into), spread?, x?, y?, width?: finite number > 0 (null: absent), height? (video/audio), pdfPage?: n (1-based, Image Import Options), pdfCrop?: crop|art|trim|bleed|media|contentVisible|contentAll (the box the graphic shows; `width` is the whole page's), loadCursor? (with no frame to fill and no x/y, an image loads into the place cursor, as File › Place), layoutPage?: n (IDML / .designcraft: that page's objects as a group), page?, rect?: [x0,y0,x1,y1] (text: a new frame there), autoflow? (threads one frame per page column from the first frame on, adding pages until the text fits), removeStyles?, styleMap?: {imported name: document style}, styleConflicts?: useExisting|redefine|autoRename, link? (keep the file linked in the Links panel; default: the linkTextFiles preference)} — places an image into `frame` when given; otherwise any explicit x/y/spread/width creates a new frame; otherwise uses the selected empty or graphic frame, or creates a new frame; text files (.txt, .docx, .rtf, .md) and Excel workbooks (.xlsx, as a table) go into the insertion point, the selected frame or a new frame on `page`/`rect`", + "{path?|base64?, name?, embed? (keep the file's bytes in the document; default: a graphic placed from a path links to its file, base64 bytes and the web always embed), frame?: id (place into), spread?, x?, y?, width?: finite number > 0 (null: absent), height? (video/audio), pdfPage?: n (1-based, Image Import Options), pdfCrop?: crop|art|trim|bleed|media|contentVisible|contentAll (the box the graphic shows; `width` is the whole page's), loadCursor? (with no frame to fill and no x/y, an image loads into the place cursor, as File › Place), append? (with loadCursor: queue the image after those already loaded instead of replacing them), layoutPage?: n (IDML / .designcraft: that page's objects as a group), page?, rect?: [x0,y0,x1,y1] (text: a new frame there), autoflow? (threads one frame per page column from the first frame on, adding pages until the text fits), removeStyles?, styleMap?: {imported name: document style}, styleConflicts?: useExisting|redefine|autoRename, link? (keep the file linked in the Links panel; default: the linkTextFiles preference)} — places an image into `frame` when given; otherwise any explicit x/y/spread/width creates a new frame; otherwise uses the selected empty or graphic frame, or creates a new frame; text files (.txt, .docx, .rtf, .md) and Excel workbooks (.xlsx, as a table) go into the insertion point, the selected frame or a new frame on `page`/`rect`", has_doc, file_place ), diff --git a/crates/engine/src/cmd/file/save.rs b/crates/engine/src/cmd/file/save.rs index d94a02ab9..7d5b1ba19 100644 --- a/crates/engine/src/cmd/file/save.rs +++ b/crates/engine/src/cmd/file/save.rs @@ -19,16 +19,28 @@ pub(super) fn file_save(s: &mut Session, p: &Value) -> Result { // Before anything about the document changes: a refused save leaves it as it was. crate::file_access::check_write(&path).map_err(EngineError::Other)?; // Prepare paths for the destination without changing the live document until persistence succeeds. - let mut saved = (*st.doc).clone(); - crate::cmd::datamerge::refresh_relative_paths(&mut saved, std::path::Path::new(&path)); - let paths_changed = saved.data_merge != st.doc.data_merge; - let saved = if paths_changed { Arc::new(saved) } else { st.doc.clone() }; + let destination = std::path::Path::new(&path); + let from = st.path.as_deref().map(std::path::Path::new); + let linked = crate::cmd::native_links::for_destination(&st.doc, from, destination); + let links_changed = linked.is_some(); + let mut saved = linked.unwrap_or_else(|| (*st.doc).clone()); + crate::cmd::datamerge::refresh_relative_paths(&mut saved, destination); + // Proxies the background job hasn't made yet are made here, on this thread: the save is + // synchronous. The document keeps them. + let proxied = crate::cmd::native_links::with_proxies(&saved, || false); + let proxies_made = proxied.is_some(); + let saved = proxied.unwrap_or(saved); + let changed = links_changed || proxies_made || saved.data_merge != st.doc.data_merge; + let saved = if changed { Arc::new(saved) } else { st.doc.clone() }; + let warnings = crate::cmd::native_links::web_save_warnings(&saved); let bytes = to_bytes_with_preview(&saved, &s.cache)?; #[cfg(not(target_arch = "wasm32"))] crate::io::write_file(&path, &bytes).map_err(EngineError::Other)?; let st = s.doc_mut()?; - if paths_changed { - st.doc = saved; + // Images saved as links let go of their bytes; linked files resolve against the new file. + let doc = crate::cmd::native_links::after_save(&saved, destination).map_or(saved, Arc::new); + if !Arc::ptr_eq(&doc, &st.doc) { + st.doc = doc; st.bump_revision(); } st.path = Some(path.clone()); @@ -41,7 +53,35 @@ pub(super) fn file_save(s: &mut Session, p: &Value) -> Result { crate::recovery::discard_document(s, index, recovery_dir.as_deref()); s.prefs.note_recent_file(&path); } - Ok(json!({"path": path, "bytes": bytes.len()})) + Ok(json!({"path": path, "bytes": bytes.len(), "warnings": warnings})) +} + +/// `file.saveACopy`: the document written to `path` without changing which file it is or its +/// unsaved state. With `embedLinks`, every linked file is copied in: a document that opens +/// anywhere. +pub(super) fn file_save_a_copy(s: &mut Session, p: &Value) -> Result { + let path = str_param(p, "path").ok_or_else(|| bad("file.saveACopy", "missing `path`"))?; + let path = with_document_extension(path); + let embed = match p.get("embedLinks") { + None | Some(Value::Null) => false, + Some(v) => v.as_bool().ok_or_else(|| bad("file.saveACopy", "`embedLinks` must be true or false"))?, + }; + let st = s.doc()?; + let destination = std::path::Path::new(&path); + let from = st.path.as_deref().map(std::path::Path::new); + let mut copy = crate::cmd::native_links::for_destination(&st.doc, from, destination).unwrap_or_else(|| (*st.doc).clone()); + crate::cmd::datamerge::refresh_relative_paths(&mut copy, destination); + if embed { + crate::cmd::native_links::embed_linked(&mut copy, "file.saveACopy")?; + } + if let Some(proxied) = crate::cmd::native_links::with_proxies(©, || false) { + copy = proxied; + } + let warnings = crate::cmd::native_links::web_save_warnings(©); + let bytes = to_bytes_with_preview(©, &s.cache)?; + #[cfg(not(target_arch = "wasm32"))] + crate::io::write_file(&path, &bytes).map_err(EngineError::Other)?; + Ok(json!({"path": path, "bytes": bytes.len(), "warnings": warnings})) } /// Which access a recovery command needs to the folder it is given. @@ -89,20 +129,23 @@ pub(super) fn file_revert(s: &mut Session, _: &Value) -> Result { let path = s.doc()?.path.clone().ok_or_else(|| bad("file.revert", "the document has never been saved"))?; #[cfg(not(target_arch = "wasm32"))] { - let mut d = if path.to_ascii_lowercase().ends_with(".idml") { - let bytes = crate::io::read_file(&path).map_err(EngineError::Other)?; + let bytes = crate::io::read_file(&path).map_err(EngineError::Other)?; + let mut d = if craft_file_formats::detect(&path, &bytes) == Some(craft_file_formats::Format::Idml) { crate::cmd::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)?)? + from_bytes(&bytes)? }; - // As opened again: the fonts beside it now (unchanged files are shared, not read again). + // As opened again: the fonts beside it now (unchanged files are shared, not read again), + // its linked files found again. let (fonts, _, _) = load_document_fonts(&mut d, &path); + let links = crate::cmd::native_links::resolve(&mut d, Some(std::path::Path::new(&path)), true); let uid = s.doc()?.uid; let recovery_dir = s.recovery_dir.clone(); let index = s.active_index().ok_or(EngineError::NoDocument)?; let mut fresh = DocState::new(d, Some(path.clone())); fresh.fonts = fonts; fresh.uid = uid; + crate::cmd::native_links::start(&mut fresh, links); // A new state has a revision no state had before, so caches don't show the old document. crate::recovery::discard_document(s, index, recovery_dir.as_deref()); *s.doc_mut()? = fresh; diff --git a/crates/engine/src/cmd/file/snippets.rs b/crates/engine/src/cmd/file/snippets.rs index 5ff1f3795..21d986dce 100644 --- a/crates/engine/src/cmd/file/snippets.rs +++ b/crates/engine/src/cmd/file/snippets.rs @@ -23,8 +23,7 @@ pub(super) fn snippet_export(s: &mut Session, p: &Value) -> Result { } pub(crate) fn snippet_place(s: &mut Session, p: &Value) -> Result { - let (bytes, _, _) = read_source(p)?; - let src = from_bytes(&bytes)?; + let src = from_bytes(&read_source(p)?.bytes)?; let ids: Vec = src.spreads.first().map(|sp| sp.items.iter().map(|i| i.id).collect()).unwrap_or_default(); if ids.is_empty() { return Err(bad("snippet.place", "the snippet is empty")); diff --git a/crates/engine/src/cmd/guides.rs b/crates/engine/src/cmd/guides.rs index 18826fb18..24ccf1696 100644 --- a/crates/engine/src/cmd/guides.rs +++ b/crates/engine/src/cmd/guides.rs @@ -54,19 +54,25 @@ pub fn specs() -> Vec { has_doc, delete ), - cmd!("guide.deleteAll", "Delete All Guides on Spread", ["View", "Grids & Guides"], None, "{spread?}", has_doc, |s, p| { - let r = super::spread_param(p, "guide.deleteAll")?; - s.edit(|d, sel| { - let sp = d.spread_mut(r).ok_or_else(|| bad("guide.deleteAll", "no such spread"))?; - let mut n = 0; - for pg in &mut sp.pages { - n += pg.guides.len(); - pg.guides.clear(); - } - sel.guides.retain(|g| g.spread != r); - Ok(json!({"deleted": n})) - }) - }), + cmd!( + "guide.deleteAll", + "Delete All Guides on Spread", + ["View", "Grids & Guides"], + None, + "{spread?} — a locked guide, and the guides of a layer that is locked, hidden or locks its guides, stay → {deleted}", + has_doc, + |s, p| { + let r = super::spread_param(p, "guide.deleteAll")?; + s.edit(|d, sel| { + let keep = kept_guides(d, r).ok_or_else(|| bad("guide.deleteAll", "no such spread"))?; + let sp = d.spread_mut(r).ok_or_else(|| bad("guide.deleteAll", "no such spread"))?; + let n: usize = sp.pages.iter_mut().zip(&keep).map(|(pg, keep)| drop_editable(&mut pg.guides, keep)).sum(); + // The guides kept can't be selected: the spread's selected guides are gone. + sel.guides.retain(|g| g.spread != r); + Ok(json!({"deleted": n})) + }) + } + ), cmd!( "view.lockColumnGuides", "Lock Column Guides", @@ -112,7 +118,7 @@ pub fn specs() -> Vec { "Select All Guides", ["Edit"], Some("Cmd+Alt+G"), - "{spread?} — every guide on the spread that is not locked or on a locked or hidden layer → {selected: n}", + "{spread?} — every guide on the spread that is not locked or on a layer that is locked, hidden or locks its guides → {selected: n}", has_doc, select_all ), @@ -566,6 +572,7 @@ fn create_guides(s: &mut Session, p: &Value) -> Result { .ok_or_else(|| bad("layout.createGuides", format!("a single command may create at most {MAX_GUIDES_CREATED} guides")))?; let layer = s.doc()?.active_layer; s.edit(|d, sel| { + let keep = kept_guides(d, r).ok_or_else(|| bad("layout.createGuides", "no such spread"))?; if remove { // The cleared pages' guide addresses name the new guides now. sel.guides.retain(|g| g.spread != r || only.is_some_and(|o| o != g.page)); @@ -577,7 +584,7 @@ fn create_guides(s: &mut Session, p: &Value) -> Result { continue; } if remove { - pg.guides.clear(); + drop_editable(&mut pg.guides, keep.get(pi).map_or(&[][..], Vec::as_slice)); } let area: Rect = if to_page { pg.bounds() } else { pg.margin_rect() }; for y in grid_positions(area.y0, area.y1, rows, rg)? { @@ -607,6 +614,20 @@ fn create_guides(s: &mut Session, p: &Value) -> Result { }) } +/// Per page of spread `r`, the guides that can't be edited (locked, or on a layer that is locked, +/// hidden or locks its guides): what removing guides leaves. `None` without the spread. +fn kept_guides(d: &designcraft_doc::Document, r: designcraft_doc::SpreadRef) -> Option>> { + Some(d.spread(r)?.pages.iter().map(|pg| pg.guides.iter().map(|g| !g.editable_in(d)).collect()).collect()) +} + +/// Remove the guides `keep` doesn't mark (the editable ones); returns how many went. +fn drop_editable(guides: &mut Vec, keep: &[bool]) -> usize { + let before = guides.len(); + let mut keep = keep.iter(); + guides.retain(|_| keep.next().copied().unwrap_or(false)); + before - guides.len() +} + #[cfg(test)] mod tests { use super::*; @@ -953,6 +974,53 @@ mod layer_tests { assert_eq!(s.execute("guide.list", &json!({})).unwrap()[0]["position"], 200.0); } + /// Layer Options › Lock Guides: the layer's guides stay where they are (moving and deleting + /// them is refused) while its objects stay editable. + #[test] + fn lock_guides_locks_only_the_layers_guides() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let layer = s.execute("layer.new", &json!({"name": "Grid"})).unwrap()["id"].as_u64().unwrap(); + s.execute("layer.activate", &json!({"id": layer})).unwrap(); + s.execute("guide.add", &json!({"orientation": "vertical", "position": 100})).unwrap(); + let frame = s.execute("frame.create", &json!({"rect": [36, 36, 200, 200]})).unwrap()["id"].as_u64().unwrap(); + s.execute("layer.set", &json!({"id": layer, "lockGuides": true})).unwrap(); + let d = s.doc().unwrap().doc.clone(); + let g = d.spreads[0].pages[0].guides[0].clone(); + assert!(g.visible_in(&d) && !g.editable_in(&d)); + for command in ["guide.move", "guide.delete"] { + assert!(s.execute(command, &json!({"page": 0, "index": 0, "position": 200})).is_err(), "{command} moved a locked guide"); + } + assert_eq!(s.execute("guide.list", &json!({})).unwrap()[0]["position"], 100.0); + // The layer's objects aren't locked. + s.execute("transform.move", &json!({"ids": [frame], "dx": 10, "dy": 0})).unwrap(); + s.execute("layer.set", &json!({"id": layer, "lockGuides": false})).unwrap(); + s.execute("guide.move", &json!({"page": 0, "index": 0, "position": 200})).unwrap(); + assert_eq!(s.execute("guide.list", &json!({})).unwrap()[0]["position"], 200.0); + } + + #[test] + fn delete_all_and_remove_existing_keep_locked_guides() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let grid = s.execute("layer.new", &json!({"name": "Grid"})).unwrap()["id"].as_u64().unwrap(); + s.execute("layer.activate", &json!({"id": grid})).unwrap(); + s.execute("guide.add", &json!({"orientation": "vertical", "position": 100})).unwrap(); + let free = s.execute("layer.new", &json!({"name": "Free"})).unwrap()["id"].as_u64().unwrap(); + s.execute("layer.activate", &json!({"id": free})).unwrap(); + s.execute("guide.add", &json!({"orientation": "vertical", "position": 200})).unwrap(); + s.execute("layer.set", &json!({"id": grid, "lockGuides": true})).unwrap(); + let positions = |s: &mut Session| { + s.execute("guide.list", &json!({})).unwrap().as_array().unwrap().iter().map(|g| g["position"].as_f64().unwrap()).collect::>() + }; + assert_eq!(s.execute("guide.deleteAll", &json!({})).unwrap()["deleted"], 1); + assert_eq!(positions(&mut s), [100.0], "the locked layer's guide stays"); + s.execute("guide.add", &json!({"orientation": "vertical", "position": 200})).unwrap(); + s.execute("layout.createGuides", &json!({"columns": 2, "removeExisting": true, "page": 0})).unwrap(); + let after = positions(&mut s); + assert!(after.contains(&100.0) && !after.contains(&200.0), "{after:?}"); + } + #[test] fn guides_follow_their_layer() { let mut s = Session::new(); diff --git a/crates/engine/src/cmd/interactive.rs b/crates/engine/src/cmd/interactive.rs index 9f340e24d..75347b549 100644 --- a/crates/engine/src/cmd/interactive.rs +++ b/crates/engine/src/cmd/interactive.rs @@ -101,6 +101,9 @@ pub fn specs() -> Vec { /// and kept the old destination (noted in review). fn dest_param(id: &str, d: &designcraft_doc::Document, p: &Value) -> Result> { if let Some(u) = str_param(p, "url") { + if !designcraft_doc::is_safe_link_url(u) { + return Err(bad(id, "`url` must start with http://, https:// or mailto:")); + } return Ok(Some(HyperlinkDest::Url(u.to_string()))); } if let Some(e) = str_param(p, "email") { @@ -195,6 +198,19 @@ mod panel_tests { s.execute("bookmark.rename", &json!({"index": 0, "name": "Back"})).unwrap(); assert_eq!(s.execute("bookmark.list", &json!({})).unwrap()[0]["name"], "Back"); } + + #[test] + fn hyperlinks_refuse_non_web_schemes() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 1})).unwrap(); + let id = s.execute("frame.create", &json!({"rect": [72, 72, 200, 120]})).unwrap()["id"].clone(); + for bad in ["javascript:alert(1)", "file:///etc/passwd", "data:text/html,hi", "\\\\attacker\\share"] { + let e = s.execute("hyperlink.create", &json!({"ids": [id.clone()], "url": bad})).expect_err("refused").to_string(); + assert!(e.contains("http://"), "{bad}: {e}"); + } + s.execute("hyperlink.create", &json!({"ids": [id.clone()], "url": "mailto:info@example.com"})).unwrap(); + s.execute("hyperlink.create", &json!({"ids": [id], "url": "https://example.com"})).unwrap(); + } } #[cfg(test)] diff --git a/crates/engine/src/cmd/interchange.rs b/crates/engine/src/cmd/interchange.rs index e84c28103..940404aa9 100644 --- a/crates/engine/src/cmd/interchange.rs +++ b/crates/engine/src/cmd/interchange.rs @@ -49,7 +49,8 @@ fn export_idml(s: &mut Session, p: &Value) -> Result { /// replaced with another format) is not one, so the package report does not name it. pub(crate) fn has_applicable_pdf_layer_overrides(doc: &Document) -> bool { doc.placed_items().into_iter().any(|it| { - it.has_pdf_layer_override() && it.graphic().and_then(|g| doc.assets.get(&g.asset)).is_some_and(|a| designcraft_render::is_pdf(&a.data)) + it.has_pdf_layer_override() + && it.graphic().and_then(|g| doc.assets.get(&g.asset)).is_some_and(|a| a.data.load().is_ok_and(|b| designcraft_render::is_pdf(&b))) }) } @@ -66,7 +67,8 @@ fn with_pdf_layer_variants(doc: &Document, embed: bool) -> Result Result) { + let Some(bytes) = asset.data.embedded_bytes().cloned() else { return }; + let Some(path) = asset.link.clone() else { return }; + let preview = asset.proxy.take().and_then(|p| preview_proxy(&p)); + let (whole, fallback) = match preview { + Some(p) if shows_whole(asset, &bytes, &p) => (Some(p), None), + p => (None, p), + }; + // The fingerprint kept by the read (none for a file just written: its hash tells its bytes). + let file = super::links::FileLink::of(std::path::Path::new(&path), asset.link_fingerprint); + super::links::link_to_file(asset, bytes, file, package, whole); + if asset.storage == designcraft_doc::Storage::Linked && asset.proxy.is_none() { + asset.proxy = fallback; + } +} + +/// A document's stored preview of a linked graphic as a proxy: checked to decode, and made again +/// within the proxy limits ([`designcraft_render::proxy`]) when it is bigger or in another +/// format (a larger one wouldn't be kept by the saved document). `None` when it doesn't decode. +#[cfg(not(target_arch = "wasm32"))] +fn preview_proxy(preview: &designcraft_doc::Proxy) -> Option { + use designcraft_render::proxy::{MAX_BYTES, MAX_SIDE_PX, make_proxy}; + let bytes = preview.data.embedded_bytes()?; + let mime = designcraft_render::image_mime(bytes); + let fits = bytes.len() <= MAX_BYTES + && matches!(mime, "image/jpeg" | "image/png") + && designcraft_render::image_size(bytes).is_some_and(|(w, h)| w.max(h) <= MAX_SIDE_PX); + if fits && let Some(pm) = designcraft_render::decode_pixmap(bytes) { + let pixels = (u32::from(pm.width()), u32::from(pm.height())); + return Some(designcraft_doc::Proxy { mime: mime.into(), pixels, data: preview.data.clone() }); + } + let p = make_proxy(bytes, 0)?; + Some(designcraft_doc::Proxy { mime: p.mime.into(), pixels: p.pixels, data: designcraft_doc::AssetData::embedded(p.data) }) +} + +/// Whether `preview` shows the whole of `asset`'s image (`bytes`, its file's), as a proxy must: +/// a raster whose preview has the image's proportions. A PDF's preview shows its crop, which may +/// not be the page the proxy stands for, and other files' proportions aren't known here. +#[cfg(not(target_arch = "wasm32"))] +fn shows_whole(asset: &designcraft_doc::Asset, bytes: &[u8], preview: &designcraft_doc::Proxy) -> bool { + if designcraft_render::is_pdf(bytes) || asset.mime == "application/pdf" { + return false; + } + let Some((w, h)) = designcraft_render::image_size(bytes) else { return false }; + let (pw, ph) = preview.pixels; + if w == 0 || h == 0 || pw == 0 || ph == 0 { + return false; + } + // Proportions equal but for the preview's rounding to whole pixels. + let (ratio, preview_ratio) = (f64::from(w) / f64::from(h), f64::from(pw) / f64::from(ph)); + (preview_ratio / ratio - 1.0).abs() <= 1.0 / f64::from(pw) + 1.0 / f64::from(ph) + 0.01 +} + /// Placed PDFs read from IDML name their crop (`PDFCrop`): find where each box sits on its page, /// which the IDML reader can't (it doesn't parse PDFs). The box spans the graphic's /// `GraphicBounds`; a graphic without bounds takes the box's size. Bounding Box crops interpret @@ -351,6 +420,10 @@ fn is_graphic(bytes: &[u8]) -> bool { 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() { + // Linked storage finds its files itself (`native_links::resolve`). + if asset.storage == designcraft_doc::Storage::Linked { + continue; + } let Some(link) = asset.link.as_deref() else { continue }; if link.len() > MAX_LINK_PATH_BYTES { continue; @@ -422,7 +495,7 @@ pub(crate) fn open_idml(s: &mut Session, p: &Value) -> Result { let mut previews = std::collections::HashMap::new(); let bytes = if is_indd(&file, &bytes) { // What the converter can't read still comes over as IDML (#21). - let conversion = indd::convert(&bytes, &file).map_err(|e| { + let conversion = craft_indd::convert(&bytes, &file).map_err(|e| { EngineError::Other(format!( "can't convert the InDesign file: {e}. In InDesign, choose File ▸ Export… ▸ InDesign Markup (IDML) and open the .idml in DesignCraft." )) @@ -474,19 +547,17 @@ fn warning_item( /// the bytes are neither a recognized DesignCraft document nor IDML. The converter explains /// unrecognized data with an InDesign extension. pub(crate) fn is_indd(name: &str, bytes: &[u8]) -> bool { - bytes.starts_with(&indd::header::SIGNATURE) - || (has_indd_extension(name) && !super::file::is_document(bytes) && !bytes.trim_ascii_start().starts_with(b"{")) -} - -pub(crate) fn has_indd_extension(name: &str) -> bool { - let n = name.to_ascii_lowercase(); - n.ends_with(".indd") || n.ends_with(".indt") + craft_file_formats::detect(name, bytes) == Some(craft_file_formats::Format::Indd) && !super::file::is_document(bytes) } /// `name` without a trailing .idml, .indd or .indt (any case). fn without_document_extension(name: &str) -> &str { match name.rsplit_once('.') { - Some((stem, ext)) if ["idml", "indd", "indt"].iter().any(|e| ext.eq_ignore_ascii_case(e)) => stem, + Some((stem, ext)) + if matches!(craft_file_formats::from_extension(ext), Some(craft_file_formats::Format::Idml | craft_file_formats::Format::Indd)) => + { + stem + } _ => name, } } @@ -543,7 +614,7 @@ mod tests { 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"); + assert_eq!(*asset.data.resident().unwrap(), 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"); let expected = super::super::datamerge::fingerprint_from(&std::fs::metadata(&path).unwrap()); @@ -563,7 +634,7 @@ mod tests { 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.data.resident().unwrap(), png); assert_eq!(asset.link.as_deref(), std::path::absolute(&path).unwrap().to_str()); assert_eq!(super::super::links::status_cached(asset, None, &mut Default::default()), "ok"); std::fs::remove_dir_all(dir).unwrap(); @@ -579,12 +650,95 @@ mod tests { 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.data.resident().unwrap(), png); assert_eq!(asset.link.as_deref(), std::path::absolute(&path).unwrap().to_str()); assert_eq!(super::super::links::status_cached(asset, None, &mut Default::default()), "ok"); std::fs::remove_dir_all(dir).unwrap(); } + #[test] + fn a_link_that_resolves_imports_as_a_linked_asset() { + let dir = std::env::temp_dir().join(format!("dc-idml-linked-import-{}", std::process::id())); + let path = dir.join("Links").join("logo.png"); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + let (idml, png) = linked_idml("/original-computer/project/logo.png", false).unwrap(); + std::fs::write(&path, &png).unwrap(); + + let doc = import(&idml, Some(&dir.join("x.idml"))).unwrap(); + let asset = doc.assets.values().next().unwrap(); + assert_eq!(asset.storage, designcraft_doc::Storage::Linked); + assert!(asset.data.embedded_bytes().is_none(), "no embedded original"); + assert!(asset.data.is_linked() && asset.data.resident().is_some(), "the bytes read are resident for the first draw"); + assert_eq!(asset.link_hash.as_deref(), Some(crate::asset_io::content_hash(&png).as_str())); + assert_eq!(asset.link_relative.as_deref(), Some("Links/logo.png")); + assert!(asset.proxy.is_some(), "a proxy made from the file"); + assert_eq!(asset.data.load().ok().map(|b| (*b).clone()), Some(png)); + std::fs::remove_dir_all(dir).unwrap(); + } + + /// A document's stored preview stands in for a linked file only when it decodes and shows + /// the whole image, within the proxy limits (made again when bigger); otherwise the proxy is + /// made from the file. + #[test] + fn a_stored_preview_is_the_proxy_only_when_it_decodes_and_shows_the_whole_image() { + use designcraft_render::proxy::{MAX_BYTES, MAX_SIDE_PX}; + let dir = std::env::temp_dir().join(format!("dc-idml-previews-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("photo.png"); + let png = |width: u32, height: u32| designcraft_render::Rendered { width, height, pixels: vec![255; (width * height * 4) as usize] }.to_png(); + let original = png(400, 200); + std::fs::write(&path, &original).unwrap(); + let linked_with = |preview: Vec| { + let mut a = designcraft_doc::Asset { + name: "photo.png".into(), + mime: "image/png".into(), + link: Some(path.to_str().unwrap().into()), + data: designcraft_doc::AssetData::embedded(original.clone()), + pixels: Some((400, 200)), + proxy: Some(designcraft_doc::Proxy { mime: "image/png".into(), pixels: (1, 1), data: designcraft_doc::AssetData::embedded(preview) }), + ..Default::default() + }; + link_read_file(&mut a, None); + assert_eq!(a.storage, designcraft_doc::Storage::Linked); + let proxy = a.proxy.expect("a proxy"); + let bytes = proxy.data.embedded_bytes().expect("embedded").clone(); + let pm = designcraft_render::decode_pixmap(&bytes).expect("the proxy decodes"); + assert_eq!((u32::from(pm.width()), u32::from(pm.height())), proxy.pixels); + assert!(bytes.len() <= MAX_BYTES && proxy.pixels.0.max(proxy.pixels.1) <= MAX_SIDE_PX); + (proxy.pixels, bytes) + }; + let small = png(100, 50); + assert_eq!(linked_with(small.clone()), ((100, 50), Arc::new(small)), "kept as it is"); + assert_eq!(linked_with(png(2048, 1024)).0, (1024, 512), "made again within the limits"); + assert_eq!(linked_with(png(100, 100)).0, (400, 200), "a crop of the image: made from the file"); + assert_eq!(linked_with(b"not an image".to_vec()).0, (400, 200), "undecodable: made from the file"); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn a_link_that_does_not_resolve_keeps_its_path_for_relink() { + let (idml, _) = linked_idml("/original-computer/project/logo.png", false).unwrap(); + let doc = import(&idml, None).unwrap(); + let asset = doc.assets.values().next().unwrap(); + assert_eq!(asset.storage, designcraft_doc::Storage::Embedded); + assert_eq!(asset.link.as_deref(), Some("/original-computer/project/logo.png")); + assert!(asset.data.embedded_bytes().is_some_and(|b| b.is_empty()) && asset.link_hash.is_none()); + } + + #[test] + fn a_graphic_embedded_in_the_idml_stays_embedded_beside_its_file() { + let dir = std::env::temp_dir().join(format!("dc-idml-embedded-stays-{}", std::process::id())); + let path = dir.join("logo.png"); + std::fs::create_dir_all(&dir).unwrap(); + let (idml, png) = linked_idml(path.to_str().unwrap(), true).unwrap(); + std::fs::write(&path, &png).unwrap(); + let doc = import(&idml, Some(&dir.join("x.idml"))).unwrap(); + let asset = doc.assets.values().next().unwrap(); + assert_eq!(asset.storage, designcraft_doc::Storage::Embedded); + assert_eq!(asset.data.embedded_bytes().map(|b| (**b).clone()), Some(png)); + std::fs::remove_dir_all(dir).unwrap(); + } + #[test] fn embedded_link_resolution_preserves_originals_and_image_bytes() { let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); @@ -600,7 +754,7 @@ mod tests { 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!(**asset.data.embedded_bytes().unwrap(), png); assert_eq!(super::super::links::status_cached(asset, None, &mut Default::default()), "modified"); std::fs::remove_file(&original).unwrap(); @@ -608,7 +762,7 @@ mod tests { 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!(**asset.data.embedded_bytes().unwrap(), png, "rebinding must not replace embedded pixels"); assert_eq!(super::super::links::status_cached(asset, None, &mut Default::default()), "modified"); std::fs::remove_file(©).unwrap(); @@ -617,7 +771,7 @@ mod tests { 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!(**asset.data.embedded_bytes().unwrap(), png); assert_eq!(super::super::links::status_cached(asset, None, &mut Default::default()), "missing"); } let oversized = "a/".repeat(MAX_LINK_PATH_BYTES); @@ -627,7 +781,7 @@ mod tests { 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); + assert_eq!(**asset.data.embedded_bytes().unwrap(), png); std::fs::remove_dir_all(dir).unwrap(); } @@ -674,12 +828,12 @@ mod tests { 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!((&*asset.data.resident().unwrap(), asset.link.as_deref()), (&png, away.to_str())); assert_eq!(super::super::links::status_cached(asset, None, &mut Default::default()), "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); + assert_eq!(*doc.assets.values().next().unwrap().data.resident().unwrap(), png); std::fs::remove_dir_all(base).unwrap(); } @@ -687,7 +841,7 @@ mod tests { /// name) go to the converter, and what it can't read is an open error that adds no document. #[test] fn indd_files_go_through_the_converter_and_its_errors_are_open_errors() { - let mut signed = indd::header::SIGNATURE.to_vec(); + let mut signed = craft_indd::SIGNATURE.to_vec(); signed.resize(4096, 0); let cases = [(signed, "Brochure"), (vec![0; 4096], "Brochure.indd"), (b"not an InDesign file".to_vec(), "Template.INDT")]; let mut s = Session::new(); @@ -779,14 +933,14 @@ mod nested_layer_tests { for (id, expected) in ids.into_iter().zip([states(false, true), states(true, true)]) { let item = copy.placed_items().into_iter().find(|i| i.id == id).unwrap(); let asset = ©.assets[&item.graphic().unwrap().asset]; - assert_eq!(designcraft_render::pdf_layers(&asset.data), expected, "{location}: placement {id}"); + assert_eq!(designcraft_render::pdf_layers(&asset.data.load().unwrap()), expected, "{location}: placement {id}"); assert_ne!(asset.id, original_asset); assert!(asset.link.is_none()); assert!(!item.pdf_layer_override && item.pdf_hidden_layers.is_empty()); } assert_eq!(copy.item(control).unwrap().graphic().unwrap().asset, original_asset); assert!(Arc::ptr_eq(©.assets[&original_asset], &source.assets[&original_asset])); - assert_eq!(designcraft_render::pdf_layers(&source.assets[&original_asset].data), states(true, false)); + assert_eq!(designcraft_render::pdf_layers(&source.assets[&original_asset].data.load().unwrap()), states(true, false)); assert!(source.placed_items().into_iter().filter(|i| ids.contains(&i.id)).all(|i| i.has_pdf_layer_override())); } @@ -796,8 +950,12 @@ mod nested_layer_tests { let result = session.execute("file.exportIdml", &json!({})).unwrap(); let bytes = base64_decode(result["base64"].as_str().unwrap()); let imported = designcraft_idml::import_idml(&bytes).unwrap(); - let mut roundtrip: Vec<_> = - imported.assets.values().filter(|a| designcraft_render::is_pdf(&a.data)).map(|a| designcraft_render::pdf_layers(&a.data)).collect(); + let mut roundtrip: Vec<_> = imported + .assets + .values() + .filter(|a| designcraft_render::is_pdf(&a.data.load().unwrap())) + .map(|a| designcraft_render::pdf_layers(&a.data.load().unwrap())) + .collect(); let mut expected = vec![states(true, false), states(false, true), states(true, true)]; roundtrip.sort(); expected.sort(); @@ -881,8 +1039,12 @@ mod nested_layer_tests { doc.check().unwrap(); let result = s.execute("file.exportIdml", &json!({})).unwrap(); let imported = designcraft_idml::import_idml(&base64_decode(result["base64"].as_str().unwrap())).unwrap(); - let mut layers: Vec<_> = - imported.assets.values().filter(|a| designcraft_render::is_pdf(&a.data)).map(|a| designcraft_render::pdf_layers(&a.data)).collect(); + let mut layers: Vec<_> = imported + .assets + .values() + .filter(|a| designcraft_render::is_pdf(&a.data.load().unwrap())) + .map(|a| designcraft_render::pdf_layers(&a.data.load().unwrap())) + .collect(); layers.sort(); assert_eq!(layers, vec![states(false, true), states(true, false)]); } diff --git a/crates/engine/src/cmd/layout/layers.rs b/crates/engine/src/cmd/layout/layers.rs index 6601e11e0..8e4a92c4b 100644 --- a/crates/engine/src/cmd/layout/layers.rs +++ b/crates/engine/src/cmd/layout/layers.rs @@ -29,7 +29,7 @@ pub(super) fn set_layer(s: &mut Session, p: &Value) -> Result { Some(v) => v.as_bool().map(Some).ok_or_else(|| bad(ID, format!("`{k}` is true or false"))), }; let (visible, locked, printable) = (flag("visible")?, flag("locked")?, flag("printable")?); - let (guides, wrap) = (flag("showGuides")?, flag("suppressWrapWhenHidden")?); + let (guides, lock_guides, wrap) = (flag("showGuides")?, flag("lockGuides")?, flag("suppressWrapWhenHidden")?); s.edit(|d, _| { if let Some(n) = &name && d.layers.iter().any(|l| l.id != id && l.name == *n) @@ -48,6 +48,7 @@ pub(super) fn set_layer(s: &mut Session, p: &Value) -> Result { (&mut l.locked, locked), (&mut l.printable, printable), (&mut l.show_guides, guides), + (&mut l.lock_guides, lock_guides), (&mut l.suppress_wrap_when_hidden, wrap), ] { if let Some(v) = v { diff --git a/crates/engine/src/cmd/layout/registry.rs b/crates/engine/src/cmd/layout/registry.rs index f120c80f6..d79d829b8 100644 --- a/crates/engine/src/cmd/layout/registry.rs +++ b/crates/engine/src/cmd/layout/registry.rs @@ -14,7 +14,7 @@ pub fn specs() -> Vec { "Insert Pages…", ["Layout", "Pages"], Some("Cmd+Shift+P"), - "{count?: 1 (1 to 9999), after?: 0-based page index (default: the last page; past it: at the end), before?: 0-based page index (instead of `after`; 0: at the start), parent?: id|\"A-Parent\"|\"A\"|null}", + "{count?: 1 (1 to 9999), after?: 0-based page index (default: the last page; past it: at the end), before?: 0-based page index (instead of `after`; 0: at the start), parent?: id|\"A-Parent\"|\"A\"|null (default: the parent of the page they follow, or of the first page)}", has_doc, |s, p| { const ID: &str = "layout.pages.insert"; @@ -50,7 +50,13 @@ pub fn specs() -> Vec { (_, Some(b)) => b.checked_sub(1), (a, None) => Some(a.unwrap_or(n.saturating_sub(1))), }; - let parent = parent_ref(ID, &st.doc, p.get("parent"))?.unwrap_or_else(|| st.doc.parents.first().map(|x| x.id)); + // No parent given: the new pages continue the page they follow (the first page when + // they go at the start), [None] included (#2788); in a document without pages, the + // first parent. + let parent = parent_ref(ID, &st.doc, p.get("parent"))?.unwrap_or_else(|| match st.doc.page(after.unwrap_or(0)) { + Some(pg) => pg.parent, + None => st.doc.parents.first().map(|x| x.id), + }); s.edit(|d, _| { let ids = d.insert_pages(after, count, parent)?; Ok(json!({"pages": ids.len(), "total": d.page_count()})) @@ -705,6 +711,7 @@ pub fn specs() -> Vec { locked: false, printable: true, show_guides: true, + lock_guides: false, suppress_wrap_when_hidden: false, }, ); @@ -727,7 +734,7 @@ pub fn specs() -> Vec { "Layer Options…", [], None, - "{id, name?, color?: [r,g,b] | layer colour name (\"Light Blue\", \"Red\", …), visible?, locked?, printable?, showGuides?, suppressWrapWhenHidden?} — names are unique and not empty", + "{id, name?, color?: [r,g,b] | layer colour name (\"Light Blue\", \"Red\", …), visible?, locked?, printable?, showGuides?, lockGuides?, suppressWrapWhenHidden?} — names are unique and not empty", has_doc, set_layer ), diff --git a/crates/engine/src/cmd/links.rs b/crates/engine/src/cmd/links.rs index 22e31cf64..f048ba98b 100644 --- a/crates/engine/src/cmd/links.rs +++ b/crates/engine/src/cmd/links.rs @@ -1,9 +1,14 @@ //! Window › Links: placed graphics with their status, effective resolution and uses; relink, //! update, embed, go to. //! -//! Every placed graphic keeps its bytes in the document; a linked graphic also remembers its -//! file. Its status compares that file with the copy in the document: missing, modified (the -//! file's bytes differ) or OK. +//! A graphic placed from a file links to it ([`Storage::Linked`]): the document keeps the file's +//! path (absolute, and relative to the document), size, time and content hash, and a proxy +//! image; the bytes are read from the file when needed. Its status is OK while a file at one of +//! those paths holds the recorded bytes (same size and time, or the same hash), Modified when +//! files are there with other bytes, and Missing otherwise. Embed copies the bytes into the +//! document and keeps the link record, so Unembed can link to the file again. An embedded copy +//! from a document saved before linked storage (a link without a hash) is compared with its file +//! byte for byte. //! //! 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. @@ -16,7 +21,9 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use designcraft_compose::Cache; -use designcraft_doc::{AssetId, Content, Document, Fingerprint, Item, ItemId, Selection, Story, StoryId, TextFileLink}; +use designcraft_doc::{ + Asset, AssetData, AssetId, Content, Document, Fingerprint, Item, ItemId, LinkedSource, Selection, Storage, Story, StoryId, TextFileLink, +}; use designcraft_geom::Affine; use serde_json::{Value, json}; @@ -25,10 +32,10 @@ use crate::{EngineError, Result, Session}; pub fn specs() -> Vec { vec![ - 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, _| { + cmd!(query "links.list", "Links", [], None, "{} → [{asset, name, path, storage: linked|embedded (where a save keeps the bytes), status: ok|modified|missing|embedded|checking (a linked file still being hashed), 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.link_checks); - let links = s.doc().map(|d| list(&d.doc, &s.cache, d.path.as_deref().map(Path::new), Some(&mut checks), None).unwrap_or_default()); + let links = s.doc().map(|d| list(&d.doc, &s.cache, d.path.as_deref().map(Path::new), Some(&mut checks), None, Some(&d.links)).unwrap_or_default()); s.link_checks = checks; Ok(Value::Array(links?)) }), @@ -64,22 +71,18 @@ pub fn specs() -> Vec { "Embed Link", ["Window", "Links"], None, - "{asset | story} — keep only the copy in the document (a text file: unlink the story)", + "{asset | story} — copy a linked graphic's file into the document (its link is kept for Unembed); fails when the file is missing or has changed (update or relink it first). A text file: unlink the story", has_doc, - |s, p| { - if let Some(sid) = story_param(p) { - return s.edit(|d, _| { - d.story_mut(sid).ok_or_else(|| bad("links.embed", "no such story"))?.file_link = None; - Ok(Value::Null) - }); - } - let aid = asset_param(p, "links.embed")?; - s.edit(|d, _| { - let a = d.assets.get_mut(&aid).ok_or_else(|| bad("links.embed", "no such graphic"))?; - Arc::make_mut(a).set_link(None, None); - Ok(Value::Null) - }) - } + embed + ), + cmd!( + "links.unembed", + "Unembed Link…", + ["Window", "Links"], + None, + "{asset, dir?} — link an embedded graphic to a file instead of keeping it in the document: its original file when that still holds the same bytes, else a file written into `dir` (an existing one with the same bytes is reused) → {path}. Not on the web", + can_unembed, + unembed ), cmd!(noundo "links.goTo", "Go To Link", ["Window", "Links"], None, "{asset | story} — selects the frames showing the graphic or the linked text", has_doc, |s, p| { let st = s.doc_mut()?; @@ -145,6 +148,12 @@ 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"))] { + if a.storage == Storage::Linked { + return match LinkedSource::of(a, document) { + Some(source) => !crate::asset_io::candidates(&source).iter().any(crate::io::is_file), + None => true, + }; + } !crate::io::document_path(link, document).is_ok_and(crate::io::exists) } #[cfg(target_arch = "wasm32")] @@ -157,10 +166,10 @@ pub fn link_missing(a: &designcraft_doc::Asset, document: Option<&Path>) -> bool /// Linked images checked this session: (asset, the identity of its bytes, the file its link /// resolves to) → (the file's size and time when it was read, whether it matched the bytes). The /// resolved file, not the link text, is the key: a relative link names another file once the -/// document is saved elsewhere. The bytes' identity (their `Arc`'s address, as the render cache -/// keys them) changes with every relink, update or undo, so a remembered answer can never -/// describe other bytes. -pub(crate) type ImageChecks = std::collections::HashMap<(AssetId, usize, std::path::PathBuf), (Fingerprint, bool)>; +/// document is saved elsewhere. The bytes' [`designcraft_doc::SourceKey`] (as the render cache +/// keys them) changes with every relink or update and is never reused, so a remembered answer +/// can never describe other bytes. +pub(crate) type ImageChecks = std::collections::HashMap<(AssetId, designcraft_doc::SourceKey, std::path::PathBuf), (Fingerprint, bool)>; /// The size and time to keep with `bytes` just read from a linked file, `before` being the /// file's from before the read. Kept only when they can vouch for the bytes later: the size is @@ -171,6 +180,216 @@ pub(crate) fn loaded_fingerprint(before: Option, bytes: &[u8]) -> O before.filter(|f| f.size == bytes.len() as u64 && settled(f)) } +/// The modification time kept with a linked file whose time couldn't vouch for its bytes when +/// they were read ([`loaded_fingerprint`]): no file has it, so the file is told by its hash until +/// a check finds it holding the bytes and keeps its real time ([`remember_link_files`]). +pub(crate) const UNVERIFIED_MTIME: i64 = i64::MIN; + +/// The file bytes for a graphic were just read from. +pub(crate) struct FileLink { + /// The file's absolute path. + path: String, + /// Its size and time from before the read. + before: Option, +} + +impl FileLink { + pub(crate) fn of(path: &Path, before: Option) -> Self { + let path = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf()).to_string_lossy().into_owned(); + FileLink { path, before } + } +} + +/// The proxy a linked graphic is drawn with while its file can't be read: none for video and +/// sound, or a file that doesn't decode. +pub(super) fn proxy_of(a: &Asset, bytes: &[u8]) -> Option { + if a.mime.starts_with("video/") || a.mime.starts_with("audio/") { + return None; + } + let p = designcraft_render::proxy::make_proxy(bytes, a.page)?; + Some(designcraft_doc::Proxy { mime: p.mime.into(), pixels: p.pixels, data: AssetData::embedded(p.data) }) +} + +/// Give `a` the bytes of a graphic being placed, relinked or updated (its `mime` and `page` set). +/// Bytes read from a file (`file`) link to it ([`link_to_file`]), unless `embed` or on the web, +/// where they are embedded with the link record kept. Other bytes (pasted, dropped, base64) are +/// embedded without a link. +pub(crate) fn give_bytes(a: &mut Asset, bytes: impl Into>>, file: Option, embed: bool, document: Option<&Path>) { + let bytes: Arc> = bytes.into(); + a.proxy = None; + let Some(file) = file else { + a.set_link(None, None); + a.link_relative = None; + a.link_hash = None; + a.storage = Storage::Embedded; + a.data = AssetData::embedded(bytes); + return; + }; + if embed || cfg!(target_arch = "wasm32") { + record_file(a, &bytes, file, document); + a.link_hash = Some(crate::asset_io::content_hash(&bytes)); + a.storage = Storage::Embedded; + a.data = AssetData::embedded(bytes); + return; + } + link_to_file(a, bytes, file, document, None); +} + +/// [`give_bytes`] for a graphic being placed, leaving out the work that grows with the file: the +/// graphic links to its file at once, with the file's size and time as its fingerprint and the +/// bytes just read kept in memory for drawing, and its content hash and proxy are made in the +/// background ([`super::native_links::prepare`] the returned work once the asset is in the +/// document; [`add_prepared`] adds them). A link without a hash is complete: its status goes by +/// size and time until the hash is in. Placing many press-size images would otherwise decode and +/// hash each one before the app responds again. Embedded placements, bytes without a file and +/// the web (no threads) are [`give_bytes`]. +#[must_use = "the hash and proxy are made only once the returned work is queued"] +pub(crate) fn give_placed( + a: &mut Asset, + bytes: impl Into>>, + file: Option, + embed: bool, + document: Option<&Path>, +) -> Option { + let bytes: Arc> = bytes.into(); + let file = match file { + Some(file) if !embed && cfg!(not(target_arch = "wasm32")) => file, + file => { + give_bytes(a, bytes, file, embed, document); + return None; + } + }; + record_file(a, &bytes, file, document); + a.link_hash = None; + link_recorded(a, Arc::clone(&bytes), designcraft_doc::SourceKey::fresh(), document, |_, _| None); + Some(super::native_links::Pending::of(a, bytes)) +} + +/// Link `a` to `file`, whose bytes (`bytes`) were just read: the file's record (paths, size and +/// time, content hash) and a proxy to draw while the file can't be read (`preview` when there is +/// one, else made from the bytes). The bytes stay in memory for the first draw. +pub(crate) fn link_to_file(a: &mut Asset, bytes: Arc>, file: FileLink, document: Option<&Path>, preview: Option) { + record_file(a, &bytes, file, document); + a.link_hash = Some(crate::asset_io::content_hash(&bytes)); + link_recorded(a, bytes, designcraft_doc::SourceKey::fresh(), document, |a, bytes| preview.or_else(|| proxy_of(a, bytes))); +} + +/// Add to a graphic placed with [`give_placed`] (or opened with a link that has no hash) its +/// content `hash` and `proxy`, made from the bytes it has under its current key: the hash when it +/// has none, the proxy when it is linked and has none. Its storage stays as it is (a graphic +/// embedded since with Links › Embed stays embedded). → whether `a` changed. +pub(super) fn add_prepared(a: &mut Asset, hash: &str, proxy: Option<&designcraft_doc::Proxy>) -> bool { + let mut changed = false; + if a.link_hash.is_none() { + a.link_hash = Some(hash.to_owned()); + // The same bytes: the key stays, so nothing decoded from them is decoded again. + if let Some(mut source) = a.data.linked_source().cloned() { + source.hash = Some(hash.to_owned()); + a.data = AssetData::linked_under(a.data.key(), source); + } + changed = true; + } + if a.storage == Storage::Linked + && a.proxy.is_none() + && let Some(proxy) = proxy + { + a.proxy = Some(proxy.clone()); + changed = true; + } + changed +} + +/// `a`, whose link record is complete, as a link to its file under `key`: drawn with `proxy`'s +/// image while the file can't be read, and `bytes` (just read from the file) kept in memory for +/// the first draw. Embedded under `key` when the record doesn't name a file. +fn link_recorded( + a: &mut Asset, + bytes: Arc>, + key: designcraft_doc::SourceKey, + document: Option<&Path>, + proxy: impl FnOnce(&Asset, &[u8]) -> Option, +) { + a.link_fingerprint = Some(a.link_fingerprint.unwrap_or(Fingerprint { size: bytes.len() as u64, mtime: UNVERIFIED_MTIME })); + let Some(source) = LinkedSource::of(a, document) else { + a.storage = Storage::Embedded; + a.proxy = None; + a.data = AssetData::embedded_under(key, bytes); + return; + }; + a.storage = Storage::Linked; + a.proxy = proxy(a, &bytes); + a.data = AssetData::linked_under(key, source); + a.data.keep_resident(bytes); +} + +/// Keep with `a` the record of `file`, which `bytes` were read from: its absolute path, its path +/// relative to the `document` file and the size and time that vouch for the bytes +/// ([`loaded_fingerprint`]). The caller sets the content hash. +fn record_file(a: &mut Asset, bytes: &[u8], file: FileLink, document: Option<&Path>) { + a.link_fingerprint = loaded_fingerprint(file.before, bytes); + a.link_relative = document.and_then(|d| super::native_links::relative_to(d, Path::new(&file.path))); + a.link = Some(file.path); +} + +/// Does `bytes` (a file's) hold `a`'s bytes: its recorded hash, or, without one, its bytes? +#[cfg(not(target_arch = "wasm32"))] +fn holds(a: &Asset, bytes: &[u8]) -> bool { + match &a.link_hash { + Some(hash) => *hash == crate::asset_io::content_hash(bytes), + None => a.data.resident().is_some_and(|d| *d == bytes), + } +} + +/// Status of a linked graphic: OK when a file at its absolute or relative path has the recorded +/// size and time (nothing is read), or the recorded size and the recorded bytes (hashed once per +/// new time, `checks`); Modified when files are there and none holds them; Missing when none is. +fn linked_status(a: &Asset, document: Option<&Path>, checks: &mut ImageChecks) -> (&'static str, Option) { + let Some(source) = LinkedSource::of(a, document) else { return ("missing", None) }; + #[cfg(not(target_arch = "wasm32"))] + { + let mut status = "missing"; + for path in crate::asset_io::candidates(&source) { + let Some(meta) = crate::io::file_metadata(&path) else { continue }; + let now = crate::asset_io::fingerprint_from(&meta); + if Some(now) == source.fingerprint && settled(&now) { + return ("ok", None); + } + status = "modified"; + if now.size != source.len { + continue; + } + let key = (a.id, a.data.key(), path.clone()); + let same = match checks.get(&key) { + Some((at, same)) if *at == now => *same, + _ => { + let Ok(bytes) = crate::io::read_capped(&path, source.len) else { continue }; + let same = bytes.len() as u64 == source.len && holds(a, &bytes); + if settled(&now) { + checks.insert(key, (now, same)); + } + same + } + }; + if same { + return ("ok", settled(&now).then_some(now)); + } + } + (status, None) + } + #[cfg(target_arch = "wasm32")] + { + let _ = (source, checks); + ("missing", None) + } +} + +/// Whether a linked image's files are there and none holds its bytes ([`linked_status`] +/// "modified"): Preflight's check, which reads a file only as the Links panel does, once per new +/// size and time (`checks`). +pub(crate) fn link_modified(a: &Asset, document: Option<&Path>, checks: &mut LinkListChecks) -> bool { + a.storage == Storage::Linked && linked_status(a, document, &mut checks.images).0 == "modified" +} + /// Is the linked file still the one the asset's bytes were loaded from, going by the size and /// time kept then ([`designcraft_doc::Asset::link_fingerprint`], [`loaded_fingerprint`])? Nothing /// is read when it is, also on the first check after a document is opened, which matters on a @@ -178,7 +397,7 @@ pub(crate) fn loaded_fingerprint(before: Option, bytes: &[u8]) -> O /// new time or no fingerprint leave it to the byte comparison. #[cfg(not(target_arch = "wasm32"))] fn loaded_unchanged(a: &designcraft_doc::Asset, now: Fingerprint) -> bool { - a.link_fingerprint.is_some_and(|loaded| loaded.size == a.data.len() as u64 && loaded == now && settled(&now)) + a.link_fingerprint.is_some_and(|loaded| loaded.size == a.data.len() && loaded == now && settled(&now)) } /// Link status of a placed graphic (`ok`, `modified`, `missing`, `embedded`), reading the file @@ -186,14 +405,55 @@ fn loaded_unchanged(a: &designcraft_doc::Asset, now: Fingerprint) -> bool { /// ([`loaded_unchanged`]) nor the one remembered from the last read (`checks`): the Links panel /// polls, and a document of press-size images would otherwise be read whole on every poll. A file /// changed in the last two seconds is never remembered (see [`settled`]). +#[cfg(test)] pub(crate) fn status_cached(a: &designcraft_doc::Asset, document: Option<&Path>, checks: &mut ImageChecks) -> &'static str { - image_status(a, document, checks).0 + image_status(a, document, checks, None).0 +} + +/// Whether the automation read roots let this session read a file at one of `a`'s link paths +/// (always without roots in force, and then nothing is asked of the file system). +fn may_read_link(a: &Asset, document: Option<&Path>) -> bool { + #[cfg(not(target_arch = "wasm32"))] + { + LinkedSource::of(a, document).is_some_and(|s| crate::asset_io::candidates(&s).iter().any(|p| crate::file_access::check_read(p).is_ok())) + } + #[cfg(target_arch = "wasm32")] + { + let _ = (a, document); + true + } +} + +/// Whether `a`'s bytes are the document's own copy, with no file to compare them with: embedded +/// with no link, or with the link record Embed keeps (it has a hash; a copy saved before linked +/// storage has none, and is compared with its file). +fn embedded_copy(a: &Asset) -> bool { + a.storage == Storage::Embedded && (a.link.is_none() || a.link_hash.is_some()) } /// [`status_cached`], and the file's size and time when a read found the asset's bytes in it /// although the fingerprint kept with the asset says nothing or something else: what -/// [`remember_link_files`] keeps, so the file isn't read again in another session. -fn image_status(a: &designcraft_doc::Asset, document: Option<&Path>, checks: &mut ImageChecks) -> (&'static str, Option) { +/// [`remember_link_files`] keeps, so the file isn't read again in another session. While the +/// background check the open of the document started (`open`) still has the asset's file to look +/// at, it is `checking`; after that the file is looked at as it is now, which may have changed +/// since. +fn image_status( + a: &designcraft_doc::Asset, + document: Option<&Path>, + checks: &mut ImageChecks, + open: Option<&super::native_links::OpenLinks>, +) -> (&'static str, Option) { + if embedded_copy(a) { + return ("embedded", None); + } + if a.storage == Storage::Linked { + if open.is_some_and(|o| o.checking() && o.status(a) == Some(super::native_links::LinkStatus::Checking)) { + // A file this session may not read (outside the automation read roots) is missing, + // whatever the background check finds. + return (if may_read_link(a, document) { "checking" } else { "missing" }, None); + } + return linked_status(a, document, checks); + } let Some(link) = &a.link else { return ("embedded", None) }; // Nothing was read from the file (a package's link that wasn't followed): missing, as in // Preflight. @@ -213,17 +473,17 @@ fn image_status(a: &designcraft_doc::Asset, document: Option<&Path>, checks: &mu if loaded_unchanged(a, now) { return ("ok", None); } - if now.size != a.data.len() as u64 { + if now.size != a.data.len() { return ("modified", None); } - let key = (a.id, Arc::as_ptr(&a.data) as usize, path.clone()); + let key = (a.id, a.data.key(), path.clone()); if let Some((at, same)) = checks.get(&key) && *at == now { return if *same { ("ok", Some(now)) } else { ("modified", None) }; } - let same = match crate::io::read_capped(&path, a.data.len() as u64) { - Ok(b) => b == *a.data, + let same = match crate::io::read_capped(&path, a.data.len()) { + Ok(b) => a.data.load().is_ok_and(|d| b == *d), Err(_) => return ("missing", None), }; // The time from before the read: a change during it is seen next time. @@ -253,9 +513,13 @@ pub(crate) fn checkable_size(len: usize) -> bool { /// while the file was changed in the last two: another change in that second keeps the time. A /// time in the future (clock skew, a file copied from another machine) counts as settled, or such /// a file would be read on every poll; a later save gives it a time unlike the remembered one. +/// A file without a time ([`crate::asset_io::has_time`]) never is. pub(crate) fn settled(f: &Fingerprint) -> bool { #[cfg(not(target_arch = "wasm32"))] { + if !crate::asset_io::has_time(f) { + return false; + } let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX)); f.mtime > now || now.saturating_sub(f.mtime) >= 2 } @@ -414,7 +678,7 @@ impl LinkListChecks { /// what [`list_document_links`] finds on a worker thread, where a share that doesn't answer /// can't stall it. pub fn preview_document_links(d: &Document, cache: &Cache, document: Option<&Path>) -> Vec { - list(d, cache, document, None, None).unwrap_or_default() + list(d, cache, document, None, None, None).unwrap_or_default() } /// The rows of `links.list` for a document outside a [`Session`], so a native UI can check the @@ -428,7 +692,7 @@ pub fn list_document_links( checks: &mut LinkListChecks, stop: &AtomicBool, ) -> Option> { - list(d, cache, document, Some(checks), Some(stop)) + list(d, cache, document, Some(checks), Some(stop), None) } /// Keep, with each linked image in `files`, the size and time its file had when a check read it @@ -443,16 +707,25 @@ pub fn remember_link_files(s: &mut Session, doc: &Arc, files: &[(Asset if !Arc::ptr_eq(&st.doc, doc) || st.interaction.is_some() { return 0; } - let stale = |a: &designcraft_doc::Asset, f: &Fingerprint| a.link.is_some() && a.data.len() as u64 == f.size && a.link_fingerprint != Some(*f); + let stale = |a: &designcraft_doc::Asset, f: &Fingerprint| a.link.is_some() && a.data.len() == f.size && a.link_fingerprint != Some(*f); let fresh: Vec<(AssetId, Fingerprint)> = files.iter().filter(|(id, f)| st.doc.assets.get(id).is_some_and(|a| stale(a, f))).copied().collect(); if fresh.is_empty() { return 0; } let saved = !st.is_dirty(); + let document = st.path.clone(); let d = Arc::make_mut(&mut st.doc); for (id, f) in &fresh { if let Some(a) = d.assets.get_mut(id) { - Arc::make_mut(a).link_fingerprint = Some(*f); + let a = Arc::make_mut(a); + a.link_fingerprint = Some(*f); + // Linked bytes are read by that time too (the same bytes, found in the file: the key + // stays, also for a link with no hash yet). + if a.data.is_linked() + && let Some(source) = LinkedSource::of(a, document.as_deref().map(Path::new)) + { + a.data = AssetData::linked_under(a.data.key(), source); + } } } if saved { @@ -465,7 +738,14 @@ pub fn remember_link_files(s: &mut Session, doc: &Arc, files: &[(Asset /// `files` is what was read this session (a poll reads nothing unchanged); without it no file is /// looked at and a status that needs one is `checking` ([`preview_document_links`]). `None` when /// `stop` was set before the last link was reached. -fn list(d: &Document, cache: &Cache, document: Option<&Path>, files: Option<&mut LinkListChecks>, stop: Option<&AtomicBool>) -> Option> { +fn list( + d: &Document, + cache: &Cache, + document: Option<&Path>, + files: Option<&mut LinkListChecks>, + stop: Option<&AtomicBool>, + open: Option<&super::native_links::OpenLinks>, +) -> Option> { let stopped = || stop.is_some_and(|stop| stop.load(Ordering::Relaxed)); let (mut checks, mut images, mut verified) = match files { Some(files) => { @@ -519,17 +799,21 @@ fn list(d: &Document, cache: &Cache, document: Option<&Path>, files: Option<&mut let why = a.link.as_deref().and_then(|l| refused(l, document)).map(|r| r.to_string()); let status = match images.as_deref_mut() { Some(images) => { - let (status, file) = image_status(a, document, images); + let (status, file) = image_status(a, document, images, open); if let (Some(verified), Some(file)) = (verified.as_deref_mut(), file) { verified.push((a.id, file)); } status } - None if a.link.is_none() => "embedded", + None if embedded_copy(a) => "embedded", None if a.data.is_empty() || why.is_some() => "missing", - None => "checking", + None => open.and_then(|o| o.status(a)).map_or("checking", |s| s.as_str()), }; - Some(json!({"asset": a.id.0, "name": a.name, "path": a.link, "status": status, "refused": why, "pixels": a.pixels, "pdfCrop": pdf_crop, "uses": uses})) + let storage = match a.storage { + Storage::Linked => "linked", + Storage::Embedded => "embedded", + }; + Some(json!({"asset": a.id.0, "name": a.name, "path": a.link, "storage": storage, "status": status, "refused": why, "pixels": a.pixels, "pdfCrop": pdf_crop, "uses": uses})) }) .chain(texts) .collect() @@ -604,14 +888,12 @@ fn reload(path: impl AsRef) -> Result<(Vec, (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> { +/// The replacement for `a`: bytes read from the file at `read`, linked to it (its absolute path, +/// and its path relative to the `document` file) with a new hash and proxy. +fn relinked_asset(a: &designcraft_doc::Asset, read: &Path, document: Option<&Path>) -> Result> { // The size and time from before the read: a change during it is seen by the next check. let before = super::datamerge::fingerprint_of(read); let (bytes, px) = reload(read)?; - let loaded = loaded_fingerprint(before, &bytes); 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) @@ -622,16 +904,26 @@ fn relinked_asset(a: &designcraft_doc::Asset, read: &Path, link: &str) -> Result }; 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.set_link(Some(link.to_string()), loaded); + a.name = file_name(&read.to_string_lossy()); // A placed PDF keeps its crop choice; its box is found on the new file. - super::file::crop_pdf(&mut a); + super::file::crop_pdf_from(&mut a, &bytes); + let file = FileLink::of(read, before); + give_bytes(&mut a, bytes, Some(file), false, document); Ok(Arc::new(a)) } +/// The file Update reads `a`'s bytes from: the first of a linked graphic's paths that holds a +/// file, or where the link of an embedded copy resolves. +fn update_source(a: &Asset, document: Option<&Path>) -> Option { + if a.storage == Storage::Linked { + let source = LinkedSource::of(a, document)?; + return crate::asset_io::candidates(&source).into_iter().find(|p| crate::io::is_file(p)); + } + crate::io::document_path(a.link.as_deref()?, document).ok() +} + /// `links.relink`: a placed graphic or linked text file takes its bytes from `path` from now on. fn relink(s: &mut Session, p: &Value) -> Result { let path = str_param(p, "path").ok_or_else(|| bad("links.relink", "missing path"))?.to_string(); @@ -644,15 +936,17 @@ fn relink(s: &mut Session, p: &Value) -> Result { }); } let aid = asset_param(p, "links.relink")?; - let a = s.doc()?.doc.assets.get(&aid).ok_or_else(|| bad("links.relink", "no such graphic"))?; + let st = s.doc()?; + let a = st.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)?; + let fresh = relinked_asset(a, Path::new(&path), st.path.as_deref().map(Path::new))?; let r = s.edit(|d, _| { let name = fresh.name.clone(); d.assets.insert(aid, fresh); Ok(json!({"name": name})) }); s.link_checks.images.clear(); + designcraft_doc::asset_data::links_changed(); r } @@ -679,7 +973,9 @@ fn relink_folder(s: &mut Session, p: &Value) -> Result { 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_cached(a, document, &mut images) != "missing") { + if only.as_ref().is_some_and(|o| !o.contains(&a.id.0)) + || (missing_only && image_status(a, document, &mut images, Some(&st.links)).0 != "missing") + { continue; } let old = std::path::PathBuf::from(a.link.clone().unwrap_or_else(|| a.name.clone())); @@ -716,7 +1012,7 @@ fn relink_folder(s: &mut Session, p: &Value) -> Result { let mut fresh = Vec::new(); for (aid, path) in todo { 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())?)); + fresh.push((aid, relinked_asset(a, &path, document)?)); } let n = fresh.len(); if n > 0 { @@ -727,16 +1023,178 @@ fn relink_folder(s: &mut Session, p: &Value) -> Result { Ok(()) })?; images.clear(); + designcraft_doc::asset_data::links_changed(); } s.link_checks.images = images; Ok(json!({"relinked": n, "notFound": not_found})) } +/// Unembed reads and writes files: not on the web. +fn can_unembed(s: &Session) -> std::result::Result<(), String> { + has_doc(s)?; + if cfg!(target_arch = "wasm32") { + return Err("files can't be linked on the web".into()); + } + Ok(()) +} + +/// `links.embed`: the graphic's bytes go into the document, checked against the link record. +fn embed(s: &mut Session, p: &Value) -> Result { + const ID: &str = "links.embed"; + if let Some(sid) = story_param(p) { + return s.edit(|d, _| { + d.story_mut(sid).ok_or_else(|| bad(ID, "no such story"))?.file_link = None; + Ok(Value::Null) + }); + } + let aid = asset_param(p, ID)?; + let st = s.doc()?; + let document = st.path.as_deref().map(Path::new); + let a = st.doc.assets.get(&aid).ok_or_else(|| bad(ID, "no such graphic"))?; + if a.storage == Storage::Linked { + if cfg!(target_arch = "wasm32") { + return Err(bad(ID, format!("{}: linked files can't be read on the web", a.name))); + } + // Checked even when the bytes are in memory: what is embedded is what the file holds. + match linked_status(a, document, &mut ImageChecks::default()).0 { + "missing" => return Err(bad(ID, format!("{}: the linked file is missing; relink it, then embed it", a.name))), + "modified" => return Err(bad(ID, format!("{}: the linked file has changed; update or relink it, then embed it", a.name))), + _ => {} + } + } + let bytes = a.data.load().map_err(|e| bad(ID, format!("{}: {e}", a.name)))?; + // A link without a hash would be taken for a copy saved before linked storage. + let hash = a.link_hash.clone().or_else(|| a.link.is_some().then(|| crate::asset_io::content_hash(&bytes))); + let key = a.data.key(); + s.edit(|d, _| { + let a = Arc::make_mut(d.assets.get_mut(&aid).ok_or_else(|| bad(ID, "no such graphic"))?); + // The same bytes: what was decoded from them stays usable. + a.data = AssetData::embedded_under(key, bytes); + a.storage = Storage::Embedded; + a.proxy = None; + a.link_hash = hash; + Ok(Value::Null) + })?; + Ok(Value::Null) +} + +/// The file in `folder` named like the original (or `name` with an extension for `mime`) that +/// holds `bytes`: an existing one with exactly these bytes, else a new one written there. +#[cfg(not(target_arch = "wasm32"))] +fn write_into(folder: &Path, name: &str, mime: &str, bytes: &[u8]) -> Result { + const ID: &str = "links.unembed"; + if !crate::io::is_dir(folder) { + return Err(bad(ID, format!("{}: not a folder", folder.display()))); + } + let file = Path::new(name).file_name().map(|n| n.to_string_lossy().into_owned()).filter(|n| !n.is_empty()).unwrap_or_else(|| "graphic".into()); + let mut file = std::path::PathBuf::from(file); + if file.extension().is_none() { + let ext = match mime { + "image/jpeg" => "jpg", + "image/gif" => "gif", + "image/tiff" => "tif", + "image/webp" => "webp", + "image/svg+xml" => "svg", + "application/pdf" => "pdf", + "application/postscript" => "eps", + "image/png" => "png", + _ => "bin", + }; + file.set_extension(ext); + } + let stem = file.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(); + let ext = file.extension().map(|e| e.to_string_lossy().into_owned()).unwrap_or_default(); + for n in 0..1000u32 { + let candidate = if n == 0 { folder.join(&file) } else { folder.join(format!("{stem}-{n}.{ext}")) }; + if !crate::io::exists(&candidate) { + crate::io::write_file(&candidate, bytes).map_err(EngineError::Other)?; + return Ok(candidate); + } + if file_holds(&candidate, bytes) { + return Ok(candidate); + } + } + Err(bad(ID, format!("{}: no free file name for {}", folder.display(), file.display()))) +} + +/// Does the regular file at `path` hold exactly `bytes`? +#[cfg(not(target_arch = "wasm32"))] +fn file_holds(path: &Path, bytes: &[u8]) -> bool { + crate::io::file_metadata(path).is_some_and(|m| m.len() == bytes.len() as u64) + && crate::io::read_capped(path, bytes.len() as u64).is_ok_and(|b| b == bytes) +} + +/// `links.unembed`: an embedded graphic links to its original file, or to a copy written into `dir`. +fn unembed(s: &mut Session, p: &Value) -> Result { + const ID: &str = "links.unembed"; + can_unembed(s).map_err(|e| bad(ID, e))?; + let aid = asset_param(p, ID)?; + let st = s.doc()?; + let document = st.path.clone(); + let document = document.as_deref().map(Path::new); + let a = st.doc.assets.get(&aid).ok_or_else(|| bad(ID, "no such graphic"))?; + if a.storage == Storage::Linked { + return Err(bad(ID, format!("{} is already linked", a.name))); + } + let bytes = a + .data + .embedded_bytes() + .cloned() + .filter(|b| !b.is_empty()) + .ok_or_else(|| bad(ID, format!("{}: the document holds no bytes for it", a.name)))?; + #[cfg(not(target_arch = "wasm32"))] + { + let original = a.link.as_ref().and_then(|link| { + let source = LinkedSource { + path: link.clone(), + relative: a.link_relative.clone(), + document: document.map(Path::to_path_buf), + len: bytes.len() as u64, + fingerprint: None, + hash: None, + }; + crate::asset_io::candidates(&source).into_iter().find(|p| file_holds(p, &bytes)) + }); + let target = match (original, str_param(p, "dir")) { + (Some(original), _) => original, + (None, Some(dir)) => { + let dir = Path::new(dir); + crate::file_access::check_write(dir).map_err(EngineError::Other)?; + write_into(dir, &a.name, &a.mime, &bytes)? + } + (None, None) => return Err(bad(ID, format!("{}: its original file is missing or has changed; give `dir` to write it there", a.name))), + }; + let before = super::datamerge::fingerprint_of(&target); + let file = FileLink::of(&target, before); + let key = a.data.key(); + let mut linked = (**a).clone(); + give_bytes(&mut linked, Arc::clone(&bytes), Some(file), false, document); + // The same bytes: what was decoded from them stays usable. + if let Some(source) = linked.data.linked_source().cloned() { + linked.data = AssetData::linked_under(key, source); + linked.data.keep_resident(bytes); + } + let path = linked.link.clone(); + let r = s.edit(|d, _| { + d.assets.insert(aid, Arc::new(linked)); + Ok(json!({"path": path})) + }); + s.link_checks.images.clear(); + designcraft_doc::asset_data::links_changed(); + r + } + #[cfg(target_arch = "wasm32")] + { + let _ = (document, bytes); + Err(bad(ID, "files can't be linked on the web")) + } +} + /// `links.update`: re-read the modified links (one, or every one) from where they resolve. fn update(s: &mut Session, p: &Value) -> Result { - let (d, document) = { + let (d, document, open) = { let st = s.doc()?; - (st.doc.clone(), st.path.clone()) + (st.doc.clone(), st.path.clone(), st.links.clone()) }; let document = document.as_deref().map(Path::new); let LinkListChecks { text: checks, images, .. } = &mut s.link_checks; @@ -754,13 +1212,14 @@ fn update(s: &mut Session, p: &Value) -> Result { }) .collect(); let mut fresh = Vec::new(); - for a in d.assets.values().filter(|a| all || only_asset == Some(a.id.0)).filter(|a| status_cached(a, document, images) == "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)?)); + for a in + d.assets.values().filter(|a| all || only_asset == Some(a.id.0)).filter(|a| image_status(a, document, images, Some(&open)).0 == "modified") + { + if let Some(at) = update_source(a, document) { + fresh.push((a.id, relinked_asset(a, &at, document)?)); } } + drop(open); let mut fresh_texts = Vec::new(); for (sid, path, at) in texts { let (bytes, before) = read_text(&at)?; @@ -781,6 +1240,7 @@ fn update(s: &mut Session, p: &Value) -> Result { Ok(json!({"updated": n, "warnings": warnings})) }); s.link_checks.images.clear(); + designcraft_doc::asset_data::links_changed(); r } @@ -806,6 +1266,7 @@ mod relink_folder_tests { std::fs::write(&path, r#""#).unwrap(); s.doc_mut().unwrap().selection = Default::default(); s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap(); + crate::cmd::native_links::wait(&mut s); std::fs::rename(&path, dir.join("new").join(name)).unwrap(); } (s, dir) @@ -873,6 +1334,7 @@ mod relink_folder_tests { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": orig.to_string_lossy()})).unwrap(); + crate::cmd::native_links::wait(&mut s); // The file moves away: relink from the new folder. std::fs::rename(&orig, dir.join("new").join("logo.svg")).unwrap(); let r = s.execute("links.relinkFolder", &json!({"dir": dir.join("new").to_string_lossy()})).unwrap(); @@ -936,6 +1398,7 @@ mod shared_asset_tests { for (path, x) in [(&a, 72), (&b, 200)] { s.execute("edit.deselectAll", &json!({})).unwrap(); s.execute("file.place", &json!({"path": path.to_string_lossy(), "x": x, "y": 72})).unwrap(); + crate::cmd::native_links::wait(&mut s); } assert_eq!(s.doc().unwrap().doc.assets.len(), 2); let rows = |s: &mut Session| -> Vec<(String, Value)> { @@ -963,7 +1426,7 @@ mod shared_asset_tests { id: AssetId(id), name: "p.png".into(), mime: "image/png".into(), - data: Arc::new(data), + data: designcraft_doc::AssetData::embedded(data), ..Default::default() }; let mut src = Document::new(&NewDocument::default()); @@ -1018,7 +1481,7 @@ mod shared_asset_tests { name: "p.png".into(), mime: "image/png".into(), link: None, - data: Arc::new(png(8, 4)), + data: designcraft_doc::AssetData::embedded(png(8, 4)), pixels: None, ..Default::default() }), @@ -1095,7 +1558,6 @@ mod text_link_tests { /// A small Word file: a Title paragraph, then Body paragraphs (`body` is WordprocessingML). fn docx(body: &str) -> Vec { - use std::io::Write; const W: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; let styles = format!( r#" @@ -1105,13 +1567,7 @@ mod text_link_tests { r#"Head {body}"# ); - let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let o = zip::write::SimpleFileOptions::default(); - for (n, c) in [("word/document.xml", doc), ("word/styles.xml", styles)] { - w.start_file(n, o).unwrap(); - w.write_all(c.as_bytes()).unwrap(); - } - w.finish().unwrap().into_inner() + crate::cmd::place_text::word_package(&doc, &styles) } fn text_rows(s: &mut Session) -> Vec { @@ -1248,6 +1704,7 @@ mod text_link_tests { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": path.to_string_lossy(), "link": true})).unwrap(); + crate::cmd::native_links::wait(&mut s); let status = |s: &mut Session| text_rows(s)[0]["status"].as_str().unwrap().to_string(); // Size and time as placed: not read. @@ -1329,6 +1786,7 @@ mod text_link_tests { let sid = fr["story"].as_u64().unwrap(); s.execute("text.select", &json!({"story": sid, "anchor": 6, "focus": 6})).unwrap(); let r = s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap(); + crate::cmd::native_links::wait(&mut s); assert!(r["warnings"][0].as_str().unwrap().contains("not linked"), "{r}"); assert!(s.doc().unwrap().doc.story(StoryId(sid)).unwrap().file_link.is_none()); let _ = std::fs::remove_dir_all(&dir); diff --git a/crates/engine/src/cmd/links/tests.rs b/crates/engine/src/cmd/links/tests.rs index 733891dbf..8de21fb14 100644 --- a/crates/engine/src/cmd/links/tests.rs +++ b/crates/engine/src/cmd/links/tests.rs @@ -33,6 +33,7 @@ fn unchanged_linked_images_are_not_read_again() { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": path.to_string_lossy(), "link": true})).unwrap(); + crate::cmd::native_links::wait(&mut s); let status = |s: &mut Session| { let l = s.execute("links.list", &json!({})).unwrap(); l.as_array().unwrap().iter().find(|r| r.get("asset").is_some()).unwrap()["status"].as_str().unwrap().to_string() @@ -101,7 +102,7 @@ fn a_linked_image_at_its_loaded_size_and_time_is_not_read() { id: AssetId(1), link: Some(path.to_string_lossy().into_owned()), link_fingerprint, - data: Arc::new(b"data".to_vec()), + data: designcraft_doc::AssetData::embedded(b"data".to_vec()), ..Default::default() }; let status = |a: &designcraft_doc::Asset| status_cached(a, None, &mut ImageChecks::default()); @@ -111,7 +112,7 @@ fn a_linked_image_at_its_loaded_size_and_time_is_not_read() { assert_eq!(status(&asset(None)), "modified"); assert_eq!(status(&asset(Some(Fingerprint { mtime: loaded.mtime - 60, ..loaded }))), "modified", "a new time alone is compared"); // Bytes replaced without their fingerprint: it is of another size, so it isn't theirs. - let swapped = designcraft_doc::Asset { data: Arc::new(b"other bytes".to_vec()), ..asset(Some(loaded)) }; + let swapped = designcraft_doc::Asset { data: designcraft_doc::AssetData::embedded(b"other bytes".to_vec()), ..asset(Some(loaded)) }; assert_eq!(status(&swapped), "modified"); // A time too recent to trust: another write in the same second would keep it. set_age(&path, 0); @@ -140,6 +141,7 @@ fn a_graphic_rewritten_with_the_same_bytes_stays_ok() { 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()); + crate::cmd::native_links::wait(&mut s); rewrite(&image, 60); let a = s.doc().unwrap().doc.assets[&aid].clone(); assert!(a.link_fingerprint.is_some()); @@ -181,14 +183,18 @@ fn preview_rows_look_at_no_file() { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); let place = |s: &mut Session, path: &Path, x: u32| { - AssetId(s.execute("file.place", &json!({"path": path.to_string_lossy(), "x": x, "y": 72})).unwrap()["asset"].as_u64().unwrap()) + let id = AssetId(s.execute("file.place", &json!({"path": path.to_string_lossy(), "x": x, "y": 72})).unwrap()["asset"].as_u64().unwrap()); + crate::cmd::native_links::wait(s); + id }; let ids = [place(&mut s, &linked, 72), place(&mut s, &embedded, 144), place(&mut s, &gone, 216)]; s.execute("links.embed", &json!({"asset": ids[1].0})).unwrap(); std::fs::remove_file(&gone).unwrap(); let st = s.doc().unwrap(); assert!(st.doc.assets[&ids[0]].link_fingerprint.is_some(), "a placed file is remembered by its size and time"); - assert!(st.doc.assets[&ids[1]].link_fingerprint.is_none(), "embed forgets the file"); + let copy = &st.doc.assets[&ids[1]]; + assert_eq!(copy.storage, designcraft_doc::Storage::Embedded); + assert!(copy.link.is_some() && copy.link_hash.is_some(), "embed keeps the link record for Unembed"); let statuses = |rows: &[Value]| -> Vec { ids.iter().map(|id| rows.iter().find(|r| r["asset"] == id.0).unwrap()["status"].as_str().unwrap().to_string()).collect() }; @@ -243,7 +249,9 @@ fn a_file_written_just_now_is_not_vouched_for_by_its_time() { 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.doc().unwrap().doc.assets[&aid].link_fingerprint, None); + crate::cmd::native_links::wait(&mut s); + let kept = s.doc().unwrap().doc.assets[&aid].link_fingerprint.unwrap(); + assert_eq!((kept.size, kept.mtime), (image.len() as u64, UNVERIFIED_MTIME), "the time can't vouch for the bytes"); let mut other = image.clone(); if let Some(last) = other.last_mut() { *last ^= 0xff; @@ -268,6 +276,7 @@ fn a_stopped_check_looks_at_no_further_file() { let path = dir.join(name); std::fs::write(&path, png(x, 10)).unwrap(); s.execute("file.place", &json!({"path": path.to_string_lossy(), "x": x, "y": 72})).unwrap(); + crate::cmd::native_links::wait(&mut s); } let st = s.doc().unwrap(); let mut checks = LinkListChecks::default(); @@ -297,6 +306,7 @@ fn a_file_found_unchanged_by_its_bytes_is_remembered_with_the_document() { 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()); + crate::cmd::native_links::wait(&mut s); s.execute("file.saveAs", &json!({"path": dir.join("doc.designcraft").to_string_lossy()})).unwrap(); assert!(!s.doc().unwrap().is_dirty()); set_age(&path, 60); @@ -424,7 +434,7 @@ fn eps_web_exports_convert_tiff_and_skip_bad_used_assets() { 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()); + Arc::make_mut(d.assets.get_mut(&aid).unwrap()).data = designcraft_doc::AssetData::embedded(b"%!PS-Adobe-3.0 EPSF-3.0\n%%EOF\n".to_vec()); Ok(()) }) .unwrap(); @@ -458,13 +468,14 @@ fn eps_status_tracks_source_bytes_across_save_update_relink_and_embed() { 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()); + crate::cmd::native_links::wait(&mut s); 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); + assert_eq!(reopened.assets[&aid].data.load().unwrap().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"); @@ -472,7 +483,7 @@ fn eps_status_tracks_source_bytes_across_save_update_relink_and_embed() { 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); + assert_eq!(s.doc().unwrap().doc.assets[&aid].data.load().unwrap().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"); @@ -489,8 +500,8 @@ fn eps_status_tracks_source_bytes_across_save_update_relink_and_embed() { 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_cached(&embedded.assets[&aid], None, &mut Default::default()), "embedded"); - assert_eq!(embedded.assets[&aid].data.as_slice(), changed); - assert!(designcraft_render::decode_pixmap(&embedded.assets[&aid].data).is_some()); + assert_eq!(embedded.assets[&aid].data.load().unwrap().as_slice(), changed); + assert!(designcraft_render::decode_pixmap(&embedded.assets[&aid].data.load().unwrap()).is_some()); let _ = std::fs::remove_dir_all(dir); } @@ -504,11 +515,16 @@ fn eps_legacy_preview_document_is_upgraded_by_update() { 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()); + crate::cmd::native_links::wait(&mut s); let proxy = designcraft_images::eps_proxy(source).unwrap().0; + // A document saved before linked storage: the preview embedded, a link without a hash. s.edit(|d, _| { let a = Arc::make_mut(d.assets.get_mut(&aid).unwrap()); - a.data = Arc::new(proxy); + a.data = designcraft_doc::AssetData::embedded(proxy); a.mime = "image/png".into(); + a.storage = designcraft_doc::Storage::Embedded; + a.link_hash = None; + a.proxy = None; Ok(()) }) .unwrap(); @@ -517,9 +533,9 @@ fn eps_legacy_preview_document_is_upgraded_by_update() { 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); + assert_eq!(s.doc().unwrap().doc.assets[&aid].data.load().unwrap().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"); + assert!(designcraft_render::decode_pixmap(&s.doc().unwrap().doc.assets[&aid].data.load().unwrap()).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); @@ -542,7 +558,7 @@ fn eps_source_exports_preview_to_pdf_html_and_epub_without_editing_document() { // 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.data = designcraft_doc::AssetData::embedded(source.to_vec()); a.mime = "application/postscript".into(); Ok(()) }) @@ -573,7 +589,7 @@ fn eps_source_exports_preview_to_pdf_html_and_epub_without_editing_document() { 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); + assert_eq!(before.assets[&aid].data.load().unwrap().as_slice(), source); } #[test] @@ -588,6 +604,7 @@ fn eps_relink_update_and_folder_relink_retain_source() { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": original.to_string_lossy()})).unwrap(); + crate::cmd::native_links::wait(&mut s); 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); @@ -596,7 +613,7 @@ fn eps_relink_update_and_folder_relink_retain_source() { 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()); + assert_eq!(a.data.load().unwrap().as_slice(), eps(30).as_bytes()); let moved = dir.join("new").join("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); @@ -616,12 +633,13 @@ fn pdf_relink_and_update_preserve_selected_page_size() { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": original.to_string_lossy(), "pdfPage": 2})).unwrap(); + crate::cmd::native_links::wait(&mut s); 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()); + assert!(designcraft_render::render_pdf_page(&a.data.load().unwrap(), 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))); @@ -646,6 +664,7 @@ fn pdf_reload_rejects_missing_page_without_edit() { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": original.to_string_lossy(), "pdfPage": 2})).unwrap(); + crate::cmd::native_links::wait(&mut s); let aid = s.doc().unwrap().doc.assets.keys().next().copied().unwrap(); if command == "links.update" { std::fs::write(&original, &short).unwrap(); @@ -667,7 +686,7 @@ fn pdf_reload_rejects_missing_page_without_edit() { 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()); + assert!(designcraft_render::render_pdf_page(&st.doc.assets[&aid].data.load().unwrap(), 1, 64).is_some()); } let _ = std::fs::remove_dir_all(dir); } @@ -683,6 +702,7 @@ fn link_status_relink_update_embed() { let mut s = Session::new(); s.execute("file.new", &json!({})).unwrap(); s.execute("file.place", &json!({"path": a.to_string_lossy(), "x": 72, "y": 72, "width": 72})).unwrap(); + crate::cmd::native_links::wait(&mut s); let l = s.execute("links.list", &json!({})).unwrap(); assert_eq!(l[0]["status"], "ok"); assert_eq!(l[0]["uses"][0]["ppi"], 10.0, "10 px over 1 inch"); @@ -707,3 +727,272 @@ fn link_status_relink_update_embed() { assert_eq!(s.execute("links.list", &json!({})).unwrap()[0]["status"], "embedded"); let _ = std::fs::remove_dir_all(&dir); } + +/// A temporary folder, removed when dropped. +#[cfg(not(target_arch = "wasm32"))] +struct TempDir(std::path::PathBuf); + +#[cfg(not(target_arch = "wasm32"))] +impl TempDir { + fn new(name: &str) -> Self { + let dir = std::env::temp_dir().join(format!("dc-links-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + TempDir(dir) + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for TempDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +/// Two valid PNGs of one byte length with other pixels (a uniform fill compresses to the same size). +fn same_size_pngs() -> (Vec, Vec) { + let a = designcraft_render::Rendered { width: 30, height: 20, pixels: vec![200u8; 30 * 20 * 4] }.to_png(); + let b = designcraft_render::Rendered { width: 30, height: 20, pixels: vec![100u8; 30 * 20 * 4] }.to_png(); + assert_eq!(a.len(), b.len()); + (a, b) +} + +/// Place a file and let the background job link it ([`crate::cmd::native_links::prepare`]). +fn placed(s: &mut Session, params: Value) -> AssetId { + let id = AssetId(s.execute("file.place", ¶ms).unwrap()["asset"].as_u64().unwrap()); + crate::cmd::native_links::wait(s); + id +} + +fn asset_of(s: &Session, id: AssetId) -> Arc { + Arc::clone(&s.doc().unwrap().doc.assets[&id]) +} + +fn row(s: &mut Session, id: AssetId) -> Value { + s.execute("links.list", &json!({})).unwrap().as_array().unwrap().iter().find(|r| r["asset"] == id.0).unwrap().clone() +} + +/// The format version a saved bundle declares. +#[cfg(not(target_arch = "wasm32"))] +fn bundle_version(bytes: &[u8]) -> (u64, Vec) { + use std::io::Read; + let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let names: Vec = zip.file_names().map(str::to_string).collect(); + let mut version = 0; + for name in names.iter().filter(|n| n.ends_with(".json")) { + let mut text = String::new(); + zip.by_name(name).unwrap().read_to_string(&mut text).unwrap(); + if let Ok(v) = serde_json::from_str::(&text) + && v.get("format").is_some() + { + version = v["version"].as_u64().unwrap(); + } + } + (version, names.into_iter().filter(|n| n.starts_with("assets/")).collect()) +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_file_placed_from_a_path_links_to_it_and_embed_true_embeds_it() { + use designcraft_doc::Storage; + let dir = TempDir::new("place"); + let path = dir.0.join("art.png"); + let (bytes, _) = same_size_pngs(); + std::fs::write(&path, &bytes).unwrap(); + set_age(&path, 60); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + + let id = placed(&mut s, json!({"path": path.to_string_lossy()})); + let a = asset_of(&s, id); + assert_eq!(a.storage, Storage::Linked); + assert!(a.data.is_linked() && a.data.embedded_bytes().is_none(), "no embedded copy"); + assert_eq!(a.link.as_deref(), path.to_str()); + assert_eq!(a.link_hash.as_deref(), Some(crate::asset_io::content_hash(&bytes).as_str())); + assert!(a.proxy.as_ref().is_some_and(|p| p.pixels == (30, 20))); + assert_eq!(a.data.resident().as_deref(), Some(&bytes), "the bytes read for placing are kept for the first draw"); + let r = row(&mut s, id); + assert_eq!((r["storage"].as_str(), r["status"].as_str()), (Some("linked"), Some("ok"))); + let (version, stored) = bundle_version(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()); + assert_eq!((version, stored), (5, vec![]), "linked: format 5 and no copy of the file"); + + let e = placed(&mut s, json!({"path": path.to_string_lossy(), "embed": true, "x": 200})); + let a = asset_of(&s, e); + assert_eq!(a.storage, Storage::Embedded); + assert_eq!(a.data.embedded_bytes().map(|b| b.as_slice()), Some(bytes.as_slice())); + assert!(a.link.is_some() && a.link_hash.is_some(), "the link record stays for Unembed"); + let r = row(&mut s, e); + assert_eq!((r["storage"].as_str(), r["status"].as_str()), (Some("embedded"), Some("embedded"))); +} + +/// Placing a large image links it at once and returns before its proxy and hash are made. A save +/// in between writes the link without a hash, with a proxy made for it then; the copy reopens as +/// that link, and its open hashes the file. The background job adds the hash and proxy to the +/// live document under the key its bytes were drawn with. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn placing_leaves_the_proxy_and_hash_to_the_background() { + use designcraft_doc::Storage; + let dir = TempDir::new("deferred"); + let path = dir.0.join("big.png"); + let bytes = png(1600, 1200); + std::fs::write(&path, &bytes).unwrap(); + set_age(&path, 60); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let id = AssetId(s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap()["asset"].as_u64().unwrap()); + let a = asset_of(&s, id); + assert!(a.proxy.is_none() && a.link_hash.is_none(), "no proxy or hash is made while placing"); + assert_eq!((a.storage, a.link.as_deref()), (Storage::Linked, path.to_str())); + assert_eq!(a.data.resident().as_deref(), Some(&bytes), "the bytes read for placing are kept for drawing"); + assert!(s.doc().unwrap().links.checking(), "the background job is running"); + let key = a.data.key(); + + let early = dir.0.join("early.designcraft"); + s.execute("file.saveACopy", &json!({"path": early.to_string_lossy()})).unwrap(); + let mut t = Session::new(); + t.execute("file.open", &json!({"path": early.to_string_lossy()})).unwrap(); + crate::cmd::native_links::wait(&mut t); + let c = t.doc().unwrap().doc.assets.values().next().cloned().unwrap(); + assert_eq!(c.storage, Storage::Linked); + assert!(c.proxy.is_some(), "the save made the proxy"); + assert_eq!(c.data.load().unwrap().as_slice(), bytes.as_slice()); + let hash = crate::asset_io::content_hash(&bytes); + assert_eq!(c.link_hash.as_deref(), Some(hash.as_str()), "the open hashed the file that matched by size and time"); + + crate::cmd::native_links::wait(&mut s); + let a = asset_of(&s, id); + assert_eq!((a.storage, a.link_hash.as_deref()), (Storage::Linked, Some(hash.as_str()))); + assert_eq!(a.data.key(), key, "still under the key its decodes are cached by"); + assert!(a.proxy.as_ref().is_some_and(|p| p.pixels == (1024, 768)), "the proxy is made in the background"); + assert!(!s.doc().unwrap().links.checking()); +} + +/// Cmd-S right after placing a large photo writes a link and a proxy, not the photo. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_save_right_after_placing_writes_no_copy_of_the_file() { + let dir = TempDir::new("save-at-once"); + let path = dir.0.join("photo.png"); + // Opaque noise doesn't compress: the PNG is about as large as its pixels. + let mut seed = 0x2545_f491_u32; + let pixels = (0..800 * 600 * 4) + .map(|i| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } + }) + .collect(); + let bytes = designcraft_render::Rendered { width: 800, height: 600, pixels }.to_png(); + std::fs::write(&path, &bytes).unwrap(); + set_age(&path, 60); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + s.execute("file.place", &json!({"path": path.to_string_lossy()})).unwrap(); + let doc = dir.0.join("doc.designcraft"); + s.execute("file.save", &json!({"path": doc.to_string_lossy()})).unwrap(); + let saved = std::fs::metadata(&doc).unwrap().len(); + assert!(saved < bytes.len() as u64, "saved {saved} bytes for a {} byte photo", bytes.len()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_modified_link_is_updated_undoably() { + let dir = TempDir::new("update"); + let path = dir.0.join("art.png"); + let (first, second) = same_size_pngs(); + std::fs::write(&path, &first).unwrap(); + set_age(&path, 60); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let id = placed(&mut s, json!({"path": path.to_string_lossy()})); + let key = asset_of(&s, id).data.key(); + + std::fs::write(&path, &second).unwrap(); + set_age(&path, 30); + assert_eq!(row(&mut s, id)["status"], "modified"); + assert_eq!(asset_of(&s, id).data.load().unwrap().as_slice(), first, "changed bytes are never adopted"); + assert_eq!(s.execute("links.update", &json!({"asset": id.0})).unwrap()["updated"], 1); + let a = asset_of(&s, id); + assert_eq!(row(&mut s, id)["status"], "ok"); + assert_ne!(a.data.key(), key); + assert_eq!(a.link_hash.as_deref(), Some(crate::asset_io::content_hash(&second).as_str())); + assert_eq!(a.data.load().unwrap().as_slice(), second); + + s.execute("edit.undo", &json!({})).unwrap(); + let a = asset_of(&s, id); + assert_eq!((a.data.key(), a.link_hash.clone()), (key, Some(crate::asset_io::content_hash(&first)))); + assert_eq!(row(&mut s, id)["status"], "modified"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn embed_refuses_a_missing_link_and_unembed_links_to_the_original_or_into_a_folder() { + use designcraft_doc::Storage; + let dir = TempDir::new("embed"); + let path = dir.0.join("art.png"); + let (first, second) = same_size_pngs(); + std::fs::write(&path, &first).unwrap(); + set_age(&path, 60); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let id = placed(&mut s, json!({"path": path.to_string_lossy()})); + + std::fs::remove_file(&path).unwrap(); + let before = s.doc().unwrap().doc.clone(); + let err = s.execute("links.embed", &json!({"asset": id.0})).unwrap_err().to_string(); + assert!(err.contains("missing"), "{err}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &before)); + + std::fs::write(&path, &first).unwrap(); + s.execute("links.embed", &json!({"asset": id.0})).unwrap(); + let a = asset_of(&s, id); + assert_eq!((a.storage, a.data.embedded_bytes().map(|b| b.to_vec())), (Storage::Embedded, Some(first.clone()))); + assert_eq!(row(&mut s, id)["status"], "embedded"); + + // The original still holds the bytes: linked to it again. + let r = s.execute("links.unembed", &json!({"asset": id.0})).unwrap(); + assert_eq!(r["path"].as_str(), path.to_str()); + assert_eq!(asset_of(&s, id).storage, Storage::Linked); + assert_eq!(row(&mut s, id)["status"], "ok"); + + // Changed since: no original, so the bytes are written into `dir` (and found there next time). + s.execute("links.embed", &json!({"asset": id.0})).unwrap(); + std::fs::write(&path, &second).unwrap(); + assert!(s.execute("links.unembed", &json!({"asset": id.0})).is_err(), "no original and no folder"); + let out = dir.0.join("out"); + std::fs::create_dir_all(&out).unwrap(); + let r = s.execute("links.unembed", &json!({"asset": id.0, "dir": out.to_string_lossy()})).unwrap(); + let written = out.join("art.png"); + assert_eq!(r["path"].as_str(), written.to_str()); + assert_eq!(std::fs::read(&written).unwrap(), first); + let r = row(&mut s, id); + assert_eq!((r["storage"].as_str(), r["status"].as_str()), (Some("linked"), Some("ok"))); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(asset_of(&s, id).storage, Storage::Embedded); + s.execute("links.unembed", &json!({"asset": id.0, "dir": out.to_string_lossy()})).unwrap(); + assert_eq!(std::fs::read_dir(&out).unwrap().count(), 1, "the file with the same bytes is reused"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn every_image_the_place_cursor_loads_is_linked() { + let dir = TempDir::new("cursor"); + let files: Vec<_> = (0..3u32) + .map(|i| { + let path = dir.0.join(format!("{i}.png")); + std::fs::write(&path, png(10 + i, 10)).unwrap(); + json!({"path": path.to_string_lossy()}) + }) + .collect(); + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + s.execute("place.load", &json!({"files": files})).unwrap(); + crate::cmd::native_links::wait(&mut s); + s.execute("place.contactSheet", &json!({})).unwrap(); + let d = &s.doc().unwrap().doc; + let used = d.used_assets(); + assert_eq!(used.len(), 3); + assert!(used.iter().all(|id| d.assets[id].storage == designcraft_doc::Storage::Linked && d.assets[id].data.is_linked())); +} diff --git a/crates/engine/src/cmd/load_styles.rs b/crates/engine/src/cmd/load_styles.rs index 762f7b1fc..154306048 100644 --- a/crates/engine/src/cmd/load_styles.rs +++ b/crates/engine/src/cmd/load_styles.rs @@ -85,7 +85,7 @@ fn source(p: &Value, id: &str) -> Result { (Some(path), None) => (crate::io::read_file(path).map_err(|e| bad(id, e))?, path.to_string()), _ => return Err(bad(id, "give `path` or `base64`")), }; - if name.to_lowercase().ends_with(".idml") { + if craft_file_formats::detect(&name, &bytes) == Some(craft_file_formats::Format::Idml) { designcraft_idml::import_idml(&bytes).map_err(|e| bad(id, e.to_string())) } else { super::file::from_bytes(&bytes).map_err(|e| bad(id, e.to_string())) diff --git a/crates/engine/src/cmd/mathexpr.rs b/crates/engine/src/cmd/mathexpr.rs index 0c8a120c3..44171e098 100644 --- a/crates/engine/src/cmd/mathexpr.rs +++ b/crates/engine/src/cmd/mathexpr.rs @@ -2,8 +2,6 @@ //! text insertion point (an anchored object) or as a frame. The LaTeX is kept as the object's //! alt text, so it can be edited and exports accessibly. -use std::sync::Arc; - use designcraft_doc::{Asset, AssetId, Content, Graphic, Item, ItemId, Shape}; use designcraft_geom::{Affine, Rect, shapes}; use serde_json::{Value, json}; @@ -14,7 +12,13 @@ use crate::{Result, Session}; fn math_item(d: &mut designcraft_doc::Document, latex: &str, size: f64, lid: designcraft_doc::LayerId, at: (f64, f64)) -> Item { let (svg, (w, h)) = crate::math::to_svg(latex, size); let fresh = AssetId(d.alloc()); - let aid = d.add_asset(Asset { id: fresh, name: "math.svg".into(), mime: "image/svg+xml".into(), data: Arc::new(svg), ..Default::default() }); + let aid = d.add_asset(Asset { + id: fresh, + name: "math.svg".into(), + mime: "image/svg+xml".into(), + data: designcraft_doc::AssetData::embedded(svg), + ..Default::default() + }); let id = ItemId(d.alloc()); let r = Rect::new(at.0, at.1, at.0 + w, at.1 + h); let mut it = Item::new(id, lid, Shape::Rectangle, shapes::rectangle(r)); diff --git a/crates/engine/src/cmd/media.rs b/crates/engine/src/cmd/media.rs index eab1027f6..deb4abf38 100644 --- a/crates/engine/src/cmd/media.rs +++ b/crates/engine/src/cmd/media.rs @@ -1,8 +1,6 @@ //! Window › Interactive › Media: placed video and sound (shown as a poster or a placeholder, //! exported as `