mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-24 00:46:29 +00:00
Merge upstream main d07cbbe9
This commit is contained in:
@@ -23,38 +23,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
freebsd:
|
||||
name: FreeBSD 14 (build + test)
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 300
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
# The Japanese font tests run against craft-fonts (an optional build input; they skip without it).
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
path: craft-fonts
|
||||
- name: Build and test in a FreeBSD VM
|
||||
uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9
|
||||
with:
|
||||
release: "14.3"
|
||||
usesh: true
|
||||
cpu: 4
|
||||
mem: 12288
|
||||
# Nothing to bring back; copying the target dir out of the VM fails on large builds.
|
||||
copyback: false
|
||||
prepare: |
|
||||
pkg install -y git curl pkgconf cmake python3 libxkbcommon wayland libX11 libXcursor libXrandr libXi libxcb mesa-libs vulkan-loader gtk3 fontconfig freetype2 alsa-lib
|
||||
curl -fsSL -o /tmp/rustup-init \
|
||||
https://static.rust-lang.org/rustup/archive/1.28.2/x86_64-unknown-freebsd/rustup-init
|
||||
test "$(sha256 -q /tmp/rustup-init)" = "4d9fef2e40731489f3186c61a0f178d54d79864fe3d34791edf5b17f70074956"
|
||||
chmod 700 /tmp/rustup-init
|
||||
/tmp/rustup-init -y --profile minimal
|
||||
run: |
|
||||
. "$HOME/.cargo/env"
|
||||
# The checkout is synced in under another owner; let tests that call git read it.
|
||||
git config --global --add safe.directory "*"
|
||||
rustc --version
|
||||
CARGO_PROFILE_DEV_DEBUG=0 cargo build --workspace --all-targets --locked
|
||||
CRAFT_FONTS_DIR="$PWD/craft-fonts" CARGO_PROFILE_DEV_DEBUG=0 cargo test --workspace --locked
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/freebsd.yml@main
|
||||
with:
|
||||
craft-fonts-ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
|
||||
@@ -19,54 +19,5 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
ACTIONLINT_VERSION: 1.7.12
|
||||
ACTIONLINT_SHA256: 8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- name: Install tools
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends shellcheck desktop-file-utils appstream libxml2-utils python3-yaml
|
||||
archive="$RUNNER_TEMP/actionlint.tar.gz"
|
||||
curl -fsSL -o "$archive" \
|
||||
"https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz"
|
||||
printf '%s %s\n' "$ACTIONLINT_SHA256" "$archive" | sha256sum -c -
|
||||
tar -xzf "$archive" -C "$RUNNER_TEMP" actionlint
|
||||
- name: actionlint
|
||||
run: |
|
||||
"$RUNNER_TEMP/actionlint" -color
|
||||
- name: shellcheck
|
||||
run: shellcheck packaging/*.sh packaging/*/*.sh
|
||||
- name: PowerShell
|
||||
shell: pwsh
|
||||
run: |
|
||||
foreach ($f in Get-ChildItem packaging/windows/*.ps1) {
|
||||
$errors = $null
|
||||
[System.Management.Automation.Language.Parser]::ParseFile($f.FullName, [ref]$null, [ref]$errors) | Out-Null
|
||||
if ($errors) { $errors | ForEach-Object { Write-Output "::error file=$($f.Name)::$_" }; exit 1 }
|
||||
Write-Output "ok $($f.Name)"
|
||||
}
|
||||
- name: XML, desktop entry, AppStream, Flatpak manifest
|
||||
run: |
|
||||
xmllint --noout packaging/windows/designcraft.wxs packaging/macos/*.plist* packaging/linux/*.xml*
|
||||
# Windows Installer needs Icon ids ending in .ico or .exe, or shortcuts show a blank icon.
|
||||
if grep -oE '<Icon Id="[^"]*"' packaging/windows/designcraft.wxs | grep -vE '\.(ico|exe)"$'; then
|
||||
echo "::error::Icon Id must end in .ico or .exe"; exit 1
|
||||
fi
|
||||
desktop-file-validate packaging/linux/ai.storyteller.designcraft.desktop
|
||||
sed -e 's/@VERSION@/0.0.0/' -e 's/@DATE@/2026-01-01/' packaging/linux/ai.storyteller.designcraft.metainfo.xml.in >"$RUNNER_TEMP/ai.storyteller.designcraft.metainfo.xml"
|
||||
appstreamcli validate --no-net --explain "$RUNNER_TEMP/ai.storyteller.designcraft.metainfo.xml"
|
||||
python3 - <<'EOF'
|
||||
import yaml
|
||||
src = yaml.safe_load(open("packaging/linux/flatpak/ai.storyteller.designcraft.yml"))
|
||||
bundle = yaml.safe_load(open("packaging/linux/flatpak/ai.storyteller.designcraft.bundle.yml"))
|
||||
for m in (src, bundle):
|
||||
assert m["id"] == "ai.storyteller.designcraft" and m["command"] == "designcraft", m["id"]
|
||||
for key in ("runtime", "runtime-version", "sdk", "finish-args"):
|
||||
assert src[key] == bundle[key], f"Flatpak manifests disagree on {key}: {src[key]} vs {bundle[key]}"
|
||||
print("Flatpak manifests ok")
|
||||
EOF
|
||||
- name: FreeBSD package layout (dry run)
|
||||
run: bash packaging/freebsd/package.sh --dry-run
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/packaging-lint.yml@main
|
||||
|
||||
+23
-419
@@ -1,12 +1,3 @@
|
||||
# Release pipeline: every push to `release` builds signed installers for every platform and
|
||||
# creates or updates the draft GitHub Release "DesignCraft v<version>". See docs/releasing.md.
|
||||
#
|
||||
# The signing jobs (macOS, Windows) and the draft-release job run in the `release` environment
|
||||
# (restricted to the `release` branch), which holds the signing secrets. Every secret is
|
||||
# optional: missing ones produce unsigned artifacts and a warning, never a failed build. The jobs
|
||||
# that sign nothing (version, Linux, Flatpak, FreeBSD, web) get no environment and no secrets,
|
||||
# so a `workflow_dispatch` on another branch dry-runs them; the signing jobs are refused there,
|
||||
# which skips the draft release.
|
||||
name: Release
|
||||
|
||||
on:
|
||||
@@ -20,420 +11,33 @@ on:
|
||||
default: ""
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
contents: write
|
||||
|
||||
concurrency:
|
||||
group: release-${{ github.ref }}
|
||||
# Never cancel a run mid-notarization or mid-upload; a newer push waits its turn.
|
||||
cancel-in-progress: false
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
CARGO_INCREMENTAL: 0
|
||||
RUST_BACKTRACE: 1
|
||||
DESIGNCRAFT_BUILD_SHA: ${{ github.sha }}
|
||||
INPUT_VERSION: ${{ inputs.version }}
|
||||
|
||||
jobs:
|
||||
version:
|
||||
name: version
|
||||
runs-on: ubuntu-latest
|
||||
# No `environment: release`: no secrets needed, and branch dry runs need this job.
|
||||
outputs:
|
||||
version: ${{ steps.v.outputs.version }}
|
||||
date: ${{ steps.v.outputs.date }}
|
||||
prerelease: ${{ steps.v.outputs.prerelease }}
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- id: v
|
||||
name: Resolve version
|
||||
run: |
|
||||
. packaging/env.sh
|
||||
v="${INPUT_VERSION#v}"
|
||||
v="${v:-$VERSION}"
|
||||
if ! printf '%s' "$v" | grep -Eq '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$'; then
|
||||
echo "::error::'$v' is not a version like 1.2.3 or 1.2.3-rc.1"
|
||||
exit 1
|
||||
fi
|
||||
case "$v" in *-*) pre=true ;; *) pre=false ;; esac
|
||||
{
|
||||
echo "version=$v"
|
||||
echo "date=$(date -u +%Y-%m-%d)"
|
||||
echo "prerelease=$pre"
|
||||
} >>"$GITHUB_OUTPUT"
|
||||
echo "### DesignCraft v$v (\`${GITHUB_SHA::9}\`)" >>"$GITHUB_STEP_SUMMARY"
|
||||
|
||||
macos:
|
||||
name: macOS universal
|
||||
needs: version
|
||||
runs-on: macos-15-xlarge # M2 Pro larger runner; use only in release.yml (actions_macos_xl budget caps spend)
|
||||
environment: release
|
||||
env:
|
||||
DESIGNCRAFT_VERSION: ${{ needs.version.outputs.version }}
|
||||
DESIGNCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
# Optional font input (docs: storytold/craft-fonts docs/integration.md); releases always embed it.
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
path: craft-fonts
|
||||
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
|
||||
with:
|
||||
targets: aarch64-apple-darwin, x86_64-apple-darwin
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: release-macos-universal
|
||||
- name: Apply version override
|
||||
if: inputs.version != ''
|
||||
run: cargo xtask version set "$DESIGNCRAFT_VERSION"
|
||||
- name: Import signing certificate
|
||||
env:
|
||||
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
||||
APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
|
||||
KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }}
|
||||
run: packaging/macos/import-cert.sh
|
||||
- name: Build, sign, notarize
|
||||
env:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
|
||||
CRAFT_FONTS_REQUIRED: "1"
|
||||
APPLE_ID: ${{ secrets.APPLE_ID }}
|
||||
APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
|
||||
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
|
||||
run: packaging/macos/package.sh --arch universal
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-macos
|
||||
path: dist/release/*
|
||||
if-no-files-found: error
|
||||
- name: Remove temporary keychain
|
||||
if: always()
|
||||
run: |
|
||||
if [ -n "${MACOS_KEYCHAIN:-}" ]; then security delete-keychain "$MACOS_KEYCHAIN" || true; fi
|
||||
|
||||
windows:
|
||||
name: Windows ${{ matrix.arch }}
|
||||
needs: version
|
||||
runs-on: windows-latest
|
||||
environment: release
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- { arch: x64, target: x86_64-pc-windows-msvc }
|
||||
- { arch: x86, target: i686-pc-windows-msvc }
|
||||
# Windows on ARM: cross-compiled on the x64 runner, so signing and WiX run as above.
|
||||
# .github/workflows/windows-arm64.yml installs and runs it on ARM64 hardware.
|
||||
- { arch: arm64, target: aarch64-pc-windows-msvc }
|
||||
env:
|
||||
DESIGNCRAFT_VERSION: ${{ needs.version.outputs.version }}
|
||||
DESIGNCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
# Optional font input (docs: storytold/craft-fonts docs/integration.md); releases always embed it.
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
path: craft-fonts
|
||||
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
|
||||
with:
|
||||
targets: ${{ matrix.target }}
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: release-windows-${{ matrix.arch }}
|
||||
- name: Apply version override
|
||||
if: inputs.version != ''
|
||||
shell: bash
|
||||
run: cargo xtask version set "$DESIGNCRAFT_VERSION"
|
||||
- name: Install WiX
|
||||
shell: pwsh
|
||||
run: dotnet tool install --global wix --version 5.0.2
|
||||
- name: Build, sign, package
|
||||
shell: pwsh
|
||||
env:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
|
||||
CRAFT_FONTS_REQUIRED: "1"
|
||||
WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }}
|
||||
WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }}
|
||||
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
|
||||
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
|
||||
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
|
||||
AZURE_SIGNING_ENDPOINT: ${{ secrets.AZURE_SIGNING_ENDPOINT }}
|
||||
AZURE_SIGNING_ACCOUNT: ${{ secrets.AZURE_SIGNING_ACCOUNT }}
|
||||
AZURE_CERT_PROFILE: ${{ secrets.AZURE_CERT_PROFILE }}
|
||||
run: ./packaging/windows/package.ps1 -Arch ${{ matrix.arch }}
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-windows-${{ matrix.arch }}
|
||||
path: dist/release/*
|
||||
if-no-files-found: error
|
||||
|
||||
linux:
|
||||
name: Linux ${{ matrix.arch }}
|
||||
needs: version
|
||||
# The oldest GitHub-hosted image, so the binaries only need glibc >= 2.35 (Ubuntu 22.04+,
|
||||
# Debian 12+, Fedora 36+, RHEL 10). See docs/releasing.md.
|
||||
runs-on: ${{ matrix.runner }}
|
||||
# No `environment: release`: this job signs nothing, so it gets no release secrets.
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- arch: x86_64
|
||||
runner: ubuntu-22.04
|
||||
deb: amd64
|
||||
nfpm_sha256: 3f1cf344bd0b57373ca55636a78c08b0491f7293d609a456a9ac3b0b150fda97
|
||||
- arch: aarch64
|
||||
runner: ubuntu-22.04-arm
|
||||
deb: arm64
|
||||
nfpm_sha256: 27419eb382695a7942be8ad52259f3ec1854fad001b3ae4baed34ce39a223b97
|
||||
env:
|
||||
DESIGNCRAFT_VERSION: ${{ needs.version.outputs.version }}
|
||||
DESIGNCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
|
||||
NFPM_VERSION: 2.47.0
|
||||
NFPM_SHA256: ${{ matrix.nfpm_sha256 }}
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
# Optional font input (docs: storytold/craft-fonts docs/integration.md); releases always embed it.
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
path: craft-fonts
|
||||
- name: Install build and packaging tools
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev \
|
||||
desktop-file-utils appstream python3-yaml zsync
|
||||
curl -fsSL -o "$RUNNER_TEMP/nfpm.deb" \
|
||||
"https://github.com/goreleaser/nfpm/releases/download/v${NFPM_VERSION}/nfpm_${NFPM_VERSION}_${{ matrix.deb }}.deb"
|
||||
printf '%s %s\n' "$NFPM_SHA256" "$RUNNER_TEMP/nfpm.deb" | sha256sum -c -
|
||||
sudo dpkg -i "$RUNNER_TEMP/nfpm.deb"
|
||||
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: release-linux-${{ matrix.arch }}
|
||||
- name: Apply version override
|
||||
if: inputs.version != ''
|
||||
run: cargo xtask version set "$DESIGNCRAFT_VERSION"
|
||||
- name: Build and package (AppImage, deb, rpm, tar.gz)
|
||||
env:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
|
||||
CRAFT_FONTS_REQUIRED: "1"
|
||||
run: packaging/linux/package.sh
|
||||
- name: Check the packages
|
||||
run: |
|
||||
dpkg-deb --info dist/release/*.deb
|
||||
dpkg-deb --contents dist/release/*.deb | grep -E 'usr/bin/designcraft$|metainfo|applications'
|
||||
ldd target/release/designcraft
|
||||
python3 -c "import yaml; m = yaml.safe_load(open('packaging/linux/flatpak/ai.storyteller.designcraft.yml')); assert m['id'] == 'ai.storyteller.designcraft'"
|
||||
for appimage in dist/release/*.AppImage; do
|
||||
chmod +x "$appimage"
|
||||
APPIMAGE_EXTRACT_AND_RUN=1 "$appimage" --version
|
||||
# Update information is embedded, and the .zsync for delta updates sits beside it.
|
||||
"$appimage" --appimage-updateinformation | grep -F 'gh-releases-zsync|'
|
||||
test -s "$appimage.zsync"
|
||||
done
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-linux-${{ matrix.arch }}
|
||||
path: dist/release/*
|
||||
if-no-files-found: error
|
||||
|
||||
flatpak:
|
||||
name: Flatpak ${{ matrix.arch }}
|
||||
needs: [version, linux]
|
||||
# Repackages the Linux job's tarball (the same binaries as the AppImage/deb/rpm); no Rust
|
||||
# build here. 24.04 for a newer flatpak-builder (1.4) than 22.04 ships.
|
||||
runs-on: ${{ matrix.runner }}
|
||||
# No `environment: release`: this job signs nothing, so it gets no release secrets.
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- { arch: x86_64, runner: ubuntu-24.04 }
|
||||
- { arch: aarch64, runner: ubuntu-24.04-arm }
|
||||
env:
|
||||
DESIGNCRAFT_VERSION: ${{ needs.version.outputs.version }}
|
||||
DESIGNCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: release-linux-${{ matrix.arch }}
|
||||
path: dist/linux
|
||||
- name: Install flatpak-builder
|
||||
run: |
|
||||
sudo apt-get update
|
||||
# librsvg2-common: the SVG pixbuf loader. flatpak-builder runs the host's
|
||||
# `appstreamcli compose`, which renders the scalable icon and fails without it.
|
||||
sudo apt-get install -y --no-install-recommends flatpak flatpak-builder librsvg2-common
|
||||
# Ubuntu 24.04's AppArmor blocks the unprivileged user namespaces bubblewrap needs.
|
||||
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
|
||||
- name: Build the Flatpak bundle
|
||||
run: packaging/linux/flatpak-bundle.sh "dist/linux/designcraft-$DESIGNCRAFT_VERSION-linux-${{ matrix.arch }}.tar.gz"
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-flatpak-${{ matrix.arch }}
|
||||
path: dist/release/*.flatpak
|
||||
if-no-files-found: error
|
||||
|
||||
freebsd:
|
||||
name: FreeBSD x86_64
|
||||
needs: version
|
||||
# GitHub has no FreeBSD runners: build in a FreeBSD 14.3 VM, the same image and packages as
|
||||
# .github/workflows/freebsd.yml.
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 180
|
||||
# No `environment: release`: this job signs nothing, so it gets no release secrets.
|
||||
env:
|
||||
DESIGNCRAFT_VERSION: ${{ needs.version.outputs.version }}
|
||||
DESIGNCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
# Optional font input (docs: storytold/craft-fonts docs/integration.md); releases always embed it.
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
path: craft-fonts
|
||||
- name: Build and package in a FreeBSD VM (tar.gz)
|
||||
# vmactions/freebsd-vm v1.5.9, pinned by commit (it runs our build).
|
||||
uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9
|
||||
with:
|
||||
release: "14.3"
|
||||
usesh: true
|
||||
cpu: 4
|
||||
mem: 12288
|
||||
# Passed into the VM by name; never templated into the script below.
|
||||
envs: "DESIGNCRAFT_VERSION DESIGNCRAFT_BUILD_DATE DESIGNCRAFT_BUILD_SHA INPUT_VERSION"
|
||||
# The FreeBSD CI packages, plus bash for the packaging script.
|
||||
prepare: |
|
||||
pkg install -y bash git curl pkgconf cmake python3 libxkbcommon wayland libX11 libXcursor libXrandr libXi libxcb mesa-libs vulkan-loader gtk3 fontconfig freetype2 alsa-lib
|
||||
curl -fsSL -o /tmp/rustup-init \
|
||||
https://static.rust-lang.org/rustup/archive/1.28.2/x86_64-unknown-freebsd/rustup-init
|
||||
test "$(sha256 -q /tmp/rustup-init)" = "4d9fef2e40731489f3186c61a0f178d54d79864fe3d34791edf5b17f70074956"
|
||||
chmod 700 /tmp/rustup-init
|
||||
/tmp/rustup-init -y --profile minimal --default-toolchain stable
|
||||
run: |
|
||||
set -e
|
||||
. "$HOME/.cargo/env"
|
||||
git config --global --add safe.directory "$PWD"
|
||||
# Build outside the checkout so only dist/ is copied back out of the VM (copying the
|
||||
# target dir back fails on large builds). 4 jobs: more runs the VM out of memory.
|
||||
export CARGO_TARGET_DIR=/var/tmp/designcraft-target CARGO_BUILD_JOBS=4
|
||||
if [ -n "$INPUT_VERSION" ]; then cargo xtask version set "$DESIGNCRAFT_VERSION"; fi
|
||||
# craft-fonts was checked out next to the sources and synced into the VM with them.
|
||||
export CRAFT_FONTS_DIR="$PWD/craft-fonts" CRAFT_FONTS_REQUIRED=1
|
||||
bash packaging/freebsd/package.sh
|
||||
tar -tzf dist/release/designcraft-*-freebsd-x86_64.tar.gz \
|
||||
| grep -E '/bin/designcraft$|/bin/designcraft-cli$|/share/applications/ai\.storyteller\.designcraft\.desktop$|/share/doc/designcraft/LICENSE-MIT$'
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-freebsd-x86_64
|
||||
path: dist/release/*
|
||||
if-no-files-found: error
|
||||
|
||||
web:
|
||||
name: Web (wasm)
|
||||
needs: version
|
||||
runs-on: ubuntu-latest
|
||||
# No `environment: release`: this job signs nothing, so it gets no release secrets.
|
||||
env:
|
||||
DESIGNCRAFT_VERSION: ${{ needs.version.outputs.version }}
|
||||
DESIGNCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
|
||||
TRUNK_VERSION: 0.21.14
|
||||
TRUNK_SHA256: f2b4680cd239693a646a2795e4633c625328d7b2a044fbe749fa3a2fe9e7036b
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
# Optional font input (docs: storytold/craft-fonts docs/integration.md); releases always embed it.
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
path: craft-fonts
|
||||
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: release-web
|
||||
- name: Install trunk
|
||||
run: |
|
||||
archive="$RUNNER_TEMP/trunk.tar.gz"
|
||||
curl -fsSL -o "$archive" \
|
||||
"https://github.com/trunk-rs/trunk/releases/download/v${TRUNK_VERSION}/trunk-x86_64-unknown-linux-gnu.tar.gz"
|
||||
printf '%s %s\n' "$TRUNK_SHA256" "$archive" | sha256sum -c -
|
||||
tar -xzf "$archive" -C "$HOME/.cargo/bin" trunk
|
||||
trunk --version
|
||||
- name: Apply version override
|
||||
if: inputs.version != ''
|
||||
run: cargo xtask version set "$DESIGNCRAFT_VERSION"
|
||||
- name: Build and zip
|
||||
env:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
|
||||
CRAFT_FONTS_REQUIRED: "1"
|
||||
run: packaging/web/package.sh
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: release-web
|
||||
path: dist/release/*
|
||||
if-no-files-found: error
|
||||
|
||||
release:
|
||||
name: Draft GitHub Release
|
||||
needs: [version, macos, windows, linux, flatpak, freebsd, web]
|
||||
runs-on: ubuntu-latest
|
||||
environment: release
|
||||
permissions:
|
||||
contents: write
|
||||
env:
|
||||
VERSION: ${{ needs.version.outputs.version }}
|
||||
PRERELEASE: ${{ needs.version.outputs.prerelease }}
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
GH_REPO: ${{ github.repository }}
|
||||
steps:
|
||||
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
pattern: release-*
|
||||
path: artifacts
|
||||
merge-multiple: true
|
||||
- name: Checksums
|
||||
working-directory: artifacts
|
||||
run: |
|
||||
sha256sum -- * | tee SHA256SUMS.txt
|
||||
- name: Create or update the draft release
|
||||
working-directory: artifacts
|
||||
run: |
|
||||
tag="v$VERSION"
|
||||
title="DesignCraft v$VERSION"
|
||||
pre=()
|
||||
if [ "$PRERELEASE" = true ]; then pre=(--prerelease); fi
|
||||
if draft="$(gh release view "$tag" --json isDraft --jq .isDraft 2>/dev/null)"; then
|
||||
if [ "$draft" != "true" ]; then
|
||||
echo "::error::Release $tag is already published. Bump the version (cargo xtask version set) before pushing to release."
|
||||
exit 1
|
||||
fi
|
||||
echo "Updating draft $tag: replacing its assets"
|
||||
gh release view "$tag" --json assets --jq '.assets[].name' | while read -r asset; do
|
||||
gh release delete-asset "$tag" "$asset" --yes
|
||||
done
|
||||
gh release edit "$tag" --draft --title "$title" --target "$GITHUB_SHA" --prerelease="$PRERELEASE"
|
||||
gh release upload "$tag" --clobber -- *
|
||||
else
|
||||
echo "Creating draft $tag"
|
||||
gh release create "$tag" --draft --title "$title" --target "$GITHUB_SHA" --generate-notes "${pre[@]}" -- *
|
||||
fi
|
||||
{
|
||||
echo "Draft release **$title** at \`${GITHUB_SHA::9}\`:"
|
||||
echo
|
||||
sed 's/^/ /' SHA256SUMS.txt
|
||||
} >>"$GITHUB_STEP_SUMMARY"
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
|
||||
with:
|
||||
app-name: designcraft
|
||||
app-title: DesignCraft
|
||||
craft-fonts-ref: abb83316d96aa59c1cf64784289e378fe9fa5695
|
||||
version: ${{ inputs.version }}
|
||||
secrets:
|
||||
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
||||
APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
|
||||
KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }}
|
||||
APPLE_ID: ${{ secrets.APPLE_ID }}
|
||||
APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
|
||||
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
|
||||
WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }}
|
||||
WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }}
|
||||
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
|
||||
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
|
||||
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
|
||||
AZURE_SIGNING_ENDPOINT: ${{ secrets.AZURE_SIGNING_ENDPOINT }}
|
||||
AZURE_SIGNING_ACCOUNT: ${{ secrets.AZURE_SIGNING_ACCOUNT }}
|
||||
AZURE_CERT_PROFILE: ${{ secrets.AZURE_CERT_PROFILE }}
|
||||
|
||||
@@ -26,70 +26,10 @@ concurrency:
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
CARGO_INCREMENTAL: 0
|
||||
|
||||
jobs:
|
||||
package:
|
||||
name: package (cross-compiled on x64)
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 90
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
|
||||
with:
|
||||
targets: aarch64-pc-windows-msvc
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: windows-arm64-package
|
||||
- name: Install WiX
|
||||
shell: pwsh
|
||||
run: dotnet tool install --global wix --version 5.0.2
|
||||
- name: Build and package (unsigned)
|
||||
shell: pwsh
|
||||
env:
|
||||
DIST: ${{ github.workspace }}\dist\arm64
|
||||
run: ./packaging/windows/package.ps1 -Arch arm64
|
||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
||||
with:
|
||||
name: designcraft-windows-arm64
|
||||
path: dist/arm64/*
|
||||
if-no-files-found: error
|
||||
retention-days: 7
|
||||
|
||||
install:
|
||||
name: install and run on ARM64
|
||||
needs: package
|
||||
runs-on: windows-11-arm
|
||||
timeout-minutes: 30
|
||||
steps:
|
||||
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
|
||||
with:
|
||||
name: designcraft-windows-arm64
|
||||
path: dist
|
||||
- name: Install the MSI, check it, run it, uninstall it
|
||||
shell: pwsh
|
||||
run: |
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$msi = (Get-ChildItem dist/*.msi | Select-Object -First 1).FullName
|
||||
$p = Start-Process msiexec.exe -ArgumentList '/i', "`"$msi`"", '/qn', '/l*v', 'install.log' -Wait -PassThru
|
||||
if ($p.ExitCode -ne 0) { Get-Content install.log -Tail 60; throw "msiexec /i exited $($p.ExitCode)" }
|
||||
$dir = Join-Path $env:ProgramFiles 'DesignCraft'
|
||||
foreach ($exe in 'designcraft.exe', 'designcraft-cli.exe') {
|
||||
$path = Join-Path $dir $exe
|
||||
if (-not (Test-Path $path)) { throw "$exe not installed in $dir" }
|
||||
$bytes = [System.IO.File]::ReadAllBytes($path)
|
||||
$machine = [BitConverter]::ToUInt16($bytes, [BitConverter]::ToInt32($bytes, 0x3C) + 4)
|
||||
if ($machine -ne 0xAA64) { throw "$exe is for machine 0x$('{0:X}' -f $machine), not ARM64" }
|
||||
Write-Output "ok $path (ARM64)"
|
||||
}
|
||||
# The installed CLI runs natively on ARM64.
|
||||
$cli = Join-Path $dir 'designcraft-cli.exe'
|
||||
$out = & $cli --version 2>&1 | Out-String
|
||||
if ($LASTEXITCODE -ne 0) { Write-Output $out; throw "designcraft-cli --version exited $LASTEXITCODE" }
|
||||
Write-Output "ok designcraft-cli --version: $($out.Trim())"
|
||||
$p = Start-Process msiexec.exe -ArgumentList '/x', "`"$msi`"", '/qn' -Wait -PassThru
|
||||
if ($p.ExitCode -ne 0) { throw "msiexec /x exited $($p.ExitCode)" }
|
||||
if (Test-Path (Join-Path $dir 'designcraft.exe')) { throw 'uninstall left designcraft.exe behind' }
|
||||
Write-Output 'ok uninstalled'
|
||||
windows-arm64:
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/windows-arm64.yml@main
|
||||
with:
|
||||
app-name: designcraft
|
||||
app-dir: DesignCraft
|
||||
|
||||
@@ -38,7 +38,7 @@ People trust DesignCraft with their layouts; a crash loses their work. **This ou
|
||||
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; delete your target dir when done (disk).
|
||||
|
||||
## Roadmap
|
||||
`ROADMAP.md` (committed) tracks status, milestones and estimates. Update it whenever a milestone task lands.
|
||||
`ROADMAP.md` (committed) is the one-page summary: stage, headline numbers, dimensions, languages, progress log. It follows craftrules' [`standards/progress-docs.md`](https://github.com/storytold/craftrules/blob/main/standards/progress-docs.md). Details: `docs/target-app-parity.md` (assessment), `docs/gaps.md` (ranked work list: pick from the top), `docs/roadmap.md` (milestones, current focus), `docs/parity-checklist.md` (presence checklist, `cargo xtask parity`), and the typography, file-format, UI, hardware and localization parity docs in `docs/`. When work lands, update the affected docs and their status lines, delete or shrink the closed gap, and add a progress-log line to ROADMAP.md.
|
||||
|
||||
## Contributor credits (About window)
|
||||
|
||||
|
||||
@@ -38,4 +38,4 @@ People trust DesignCraft with their layouts; a crash loses their work. **This ou
|
||||
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; delete your target dir when done (disk).
|
||||
|
||||
## Roadmap
|
||||
`ROADMAP.md` (committed) tracks status, milestones and estimates. Update it whenever a milestone task lands.
|
||||
`ROADMAP.md` (committed) is the one-page summary: stage, headline numbers, dimensions, languages, progress log. It follows craftrules' [`standards/progress-docs.md`](https://github.com/storytold/craftrules/blob/main/standards/progress-docs.md). Details: `docs/target-app-parity.md` (assessment), `docs/gaps.md` (ranked work list: pick from the top), `docs/roadmap.md` (milestones, current focus), `docs/parity-checklist.md` (presence checklist, `cargo xtask parity`), and the typography, file-format, UI, hardware and localization parity docs in `docs/`. When work lands, update the affected docs and their status lines, delete or shrink the closed gap, and add a progress-log line to ROADMAP.md.
|
||||
|
||||
Generated
+70
-38
@@ -796,6 +796,23 @@ dependencies = [
|
||||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-print"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=d991bf6792f8ade99c51358bf3c1d902c8cd96b5#d991bf6792f8ade99c51358bf3c1d902c8cd96b5"
|
||||
dependencies = [
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=5c874d51eeb3de70e735c8ce87a59a7dedd47675#5c874d51eeb3de70e735c8ce87a59a7dedd47675"
|
||||
dependencies = [
|
||||
"egui",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.2"
|
||||
@@ -870,8 +887,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"arboard",
|
||||
"designcraft-engine",
|
||||
"designcraft-ui-egui",
|
||||
"eframe",
|
||||
@@ -887,7 +905,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-cli"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -903,7 +921,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-color"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"kurbo",
|
||||
"moxcms",
|
||||
@@ -915,7 +933,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-compose"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-doc",
|
||||
@@ -928,11 +946,12 @@ dependencies = [
|
||||
"regex",
|
||||
"unicode-bidi",
|
||||
"unicode-bidi-mirroring",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-doc"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-geom",
|
||||
@@ -944,9 +963,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-engine"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"caseless",
|
||||
"craft-print",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
"designcraft-doc",
|
||||
@@ -971,13 +991,14 @@ dependencies = [
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"unicode-segmentation",
|
||||
"wasm-bindgen-test",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-epub"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-doc",
|
||||
@@ -986,7 +1007,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-fonts"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"harfrust 0.13.3",
|
||||
"kurbo",
|
||||
@@ -998,7 +1019,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-format"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-doc",
|
||||
"serde",
|
||||
@@ -1009,7 +1030,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-geom"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"flo_curves",
|
||||
"kurbo",
|
||||
@@ -1022,7 +1043,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-idml"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1035,7 +1056,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-images"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-fonts",
|
||||
@@ -1046,7 +1067,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-mcp"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-engine",
|
||||
"designcraft-geom",
|
||||
@@ -1059,7 +1080,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-pdf"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1081,7 +1102,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-render"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1099,7 +1120,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-textimport"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-doc",
|
||||
"quick-xml 0.38.4",
|
||||
@@ -1108,7 +1129,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-tools"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -1120,8 +1141,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-ui-egui"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"craft-ui",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
"designcraft-doc",
|
||||
@@ -1137,15 +1159,17 @@ dependencies = [
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"toml",
|
||||
"unicode-bidi",
|
||||
"unicode-bidi-mirroring",
|
||||
"unicode-segmentation",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "designcraft-web"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"designcraft-engine",
|
||||
"designcraft-format",
|
||||
@@ -1462,7 +1486,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1983,13 +2007,13 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "hayro"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4caa128ab87fd48ffb7490617cf93f77f606820dffbc9fd9ef6ab0ed077f56d"
|
||||
source = "git+https://github.com/storytold/pdfcraft?rev=b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9#b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"hayro-interpret",
|
||||
"image",
|
||||
"kurbo",
|
||||
"log",
|
||||
"pic-scale",
|
||||
"vello_cpu 0.0.8",
|
||||
]
|
||||
@@ -2013,8 +2037,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "hayro-interpret"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2613d0406898995042d0794c4245b2f2fba1246490c8ac593769bba6551129d"
|
||||
source = "git+https://github.com/storytold/pdfcraft?rev=b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9#b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"hayro-cmap",
|
||||
@@ -2024,7 +2047,7 @@ dependencies = [
|
||||
"phf",
|
||||
"rustc-hash 2.1.3",
|
||||
"siphasher",
|
||||
"skrifa 0.42.1",
|
||||
"skrifa 0.47.0",
|
||||
"smallvec",
|
||||
"yoke",
|
||||
]
|
||||
@@ -2032,8 +2055,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "hayro-jbig2"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69374b3668dd45aeb3d3145cda68f2c7b4f223aaa2511e67d076f1c7d741388d"
|
||||
source = "git+https://github.com/storytold/pdfcraft?rev=b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9#b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9"
|
||||
dependencies = [
|
||||
"fearless_simd",
|
||||
"hayro-ccitt",
|
||||
@@ -2057,8 +2079,7 @@ checksum = "885c5ef0654933139a9b9546fc2c69e18d37f38aa2520f079092b1be18f1fcaa"
|
||||
[[package]]
|
||||
name = "hayro-syntax"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0edeafd70aa2db743de8ede8637d07ec87db05efe69cde371d03f1b185fcef27"
|
||||
source = "git+https://github.com/storytold/pdfcraft?rev=b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9#b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"hayro-ccitt",
|
||||
@@ -2067,6 +2088,7 @@ dependencies = [
|
||||
"memchr",
|
||||
"rustc-hash 2.1.3",
|
||||
"smallvec",
|
||||
"stringprep",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
@@ -2636,7 +2658,7 @@ dependencies = [
|
||||
"once_cell",
|
||||
"png",
|
||||
"thiserror 2.0.21",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2719,7 +2741,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]]
|
||||
@@ -3828,7 +3850,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.12.1",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4178,6 +4200,17 @@ dependencies = [
|
||||
"float-cmp",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stringprep"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1"
|
||||
dependencies = [
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
"unicode-properties",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "subsetter"
|
||||
version = "0.2.6"
|
||||
@@ -4243,7 +4276,7 @@ dependencies = [
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix 1.1.5",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4478,7 +4511,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e"
|
||||
dependencies = [
|
||||
"memoffset",
|
||||
"tempfile",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5215,7 +5248,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.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5496,8 +5529,7 @@ checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||
[[package]]
|
||||
name = "winit"
|
||||
version = "0.30.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6755fa58a9f8350bd1e472d4c3fcc25f824ec358933bba33306d0b63df5978d"
|
||||
source = "git+https://github.com/skylartaylor/winit.git?rev=3d51077fd1a966f84df9ef287839dc84fd5b4ca7#3d51077fd1a966f84df9ef287839dc84fd5b4ca7"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"android-activity",
|
||||
@@ -5665,7 +5697,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
+14
-2
@@ -4,13 +4,14 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/designcraft", "apps/designcraft-cli", "xtask"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.5.0"
|
||||
version = "0.8.0"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.90"
|
||||
rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/designcraft"
|
||||
|
||||
[workspace.dependencies]
|
||||
craft-print = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "d991bf6792f8ade99c51358bf3c1d902c8cd96b5" }
|
||||
# internal
|
||||
designcraft-geom = { path = "crates/geom" }
|
||||
designcraft-color = { path = "crates/color" }
|
||||
@@ -67,6 +68,8 @@ quick-xml = "0.38"
|
||||
zip = { version = "3", default-features = false, features = ["deflate"] }
|
||||
|
||||
# ui
|
||||
# Shared frontend components; keep this dependency above the engine boundary.
|
||||
craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5c874d51eeb3de70e735c8ce87a59a7dedd47675", features = ["serde"] }
|
||||
egui = "0.36"
|
||||
egui_extras = { version = "0.36", default-features = false, features = ["svg"] }
|
||||
eframe = { version = "0.36", default-features = false }
|
||||
@@ -94,3 +97,12 @@ opt-level = 3
|
||||
lto = "thin"
|
||||
codegen-units = 16
|
||||
debug = 0
|
||||
|
||||
# Opt-in cancelable macOS termination on the pinned winit 0.30 line.
|
||||
[patch.crates-io]
|
||||
winit = { git = "https://github.com/skylartaylor/winit.git", rev = "3d51077fd1a966f84df9ef287839dc84fd5b4ca7" }
|
||||
# The hayro crates from pdfcraft's vendor/ directory, with the patches its README lists.
|
||||
hayro = { git = "https://github.com/storytold/pdfcraft", rev = "b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9" }
|
||||
hayro-interpret = { git = "https://github.com/storytold/pdfcraft", rev = "b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9" }
|
||||
hayro-syntax = { git = "https://github.com/storytold/pdfcraft", rev = "b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9" }
|
||||
hayro-jbig2 = { git = "https://github.com/storytold/pdfcraft", rev = "b5467d93a0ffaa5e5e6658bccdf8655a3ba689c9" }
|
||||
|
||||
@@ -32,12 +32,37 @@ list (author, source, license) is in ASSETS.md.
|
||||
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
- Portuguese hyphenation patterns, embedded through the hypher crate (feature "portuguese"):
|
||||
hyph-pt.tex 1.4 from hyph-utf8, https://github.com/hyphenation/tex-hyphen.
|
||||
Copyright (C) 1987, 1994, 1996, 2015 Pedro J. de Rezende, 1996, 2015 J. Joao Dias Almeida,
|
||||
2024 Leonardo Araujo and Aline Benevides. BSD 3-clause licence:
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted
|
||||
provided that the following conditions are met: * Redistributions of source code must retain the
|
||||
above copyright notice, this list of conditions and the following disclaimer. * Redistributions in
|
||||
binary form must reproduce the above copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided with the distribution. * Neither
|
||||
the name of the University of Campinas, of the University of Minho nor the names of its
|
||||
contributors may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
EVENT SHALL PEDRO J. DE REZENDE OR J.JOAO DIAS ALMEIDA BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
- Japanese fonts (craft-fonts): builds made with craft-fonts (CRAFT_FONTS_DIR; all official
|
||||
releases) embed the fonts listed in
|
||||
https://github.com/storytold/craft-fonts/blob/main/ATTRIBUTION.md (BIZ UDPGothic, BIZ UDMincho,
|
||||
Shippori Mincho), under the SIL Open Font License 1.1. Their licence texts ship with the
|
||||
packages as OFL-<family>.txt. Builds without CRAFT_FONTS_DIR do not include them.
|
||||
- All UI icons are drawn in code (crates/ui-egui/src/icons.rs) and are original work, MIT OR Apache-2.0.
|
||||
- hayro PDF crates (placed PDFs): hayro, hayro-interpret, hayro-syntax and hayro-jbig2
|
||||
(Apache-2.0 OR MIT) are pdfcraft's patched copies, fetched by git from its vendor/ directory at
|
||||
the commit that Cargo.toml pins (https://github.com/storytold/pdfcraft, MIT OR Apache-2.0).
|
||||
hayro-interpret embeds PDFium's Foxit standard-14 fonts under PDFium's BSD-style license
|
||||
(vendor/hayro-interpret/assets/LICENSE_FOXIT) and a CMYK profile under CC0-1.0.
|
||||
|
||||
Rust dependencies (vello_cpu, kurbo, skrifa, harfrust, image, egui, eframe, …) are permissively
|
||||
licensed (MIT/Apache-2.0/BSD/ISC/Zlib) and fetched through crates.io.
|
||||
|
||||
@@ -98,12 +98,15 @@ by its own renderer. Try it yourself with `File → New → Sample Document`, or
|
||||
|
||||
## Quick start
|
||||
|
||||
The egui frontend requires Rust 1.95 or newer (including its shared `craft-ui` components).
|
||||
|
||||
```sh
|
||||
cargo run --release -p designcraft # desktop app (start screen)
|
||||
cargo run --release -p designcraft -- --sample # open the sample magazine
|
||||
cargo run --release -p designcraft -- --sample --control 7979 # + JSON control channel
|
||||
cargo run --release -p designcraft-cli -- run --sample --all-pages out/ # headless: render every page to PNG
|
||||
cargo run --release -p designcraft-cli -- commands # list every command
|
||||
cargo run --release -p designcraft-cli -- script --help # command script syntax (docs/agents.md)
|
||||
cargo xtask ci # fmt, clippy, tests, assets, layering, wasm
|
||||
```
|
||||
|
||||
@@ -183,7 +186,7 @@ egui frontend is a separate crate, so the UI can be swapped without touching the
|
||||
| L6 | `ui-egui` (InDesign-style UI, control channel) |
|
||||
| L7 | `apps/designcraft`, `apps/designcraft-cli`, `apps/designcraft-web` |
|
||||
|
||||
- **Status and milestones:** [ROADMAP.md](ROADMAP.md)
|
||||
- **Status and milestones:** [ROADMAP.md](ROADMAP.md) (stage: pre-alpha); how close to InDesign: [`docs/target-app-parity.md`](docs/target-app-parity.md); known gaps: [`docs/gaps.md`](docs/gaps.md)
|
||||
- **Contributor and agent rules** (clean-room, asset policy, quality gates): [`AGENTS.md`](AGENTS.md)
|
||||
- **Bundled assets:** every one is listed with its licence in [`ASSETS.md`](ASSETS.md)
|
||||
- **App icon and colour:** a calico cat in a polka-dot scarf on DesignCraft green `#7bb51c`; see [`assets/app-icon/`](assets/app-icon/README.md)
|
||||
@@ -221,6 +224,8 @@ Installers and executables are code-signed.
|
||||
| Fedora/RHEL/openSUSE | `designcraft-<ver>-linux-x86_64.rpm` | `designcraft-<ver>-linux-aarch64.rpm` | |
|
||||
| Tarball | `designcraft-<ver>-linux-x86_64.tar.gz` | `designcraft-<ver>-linux-aarch64.tar.gz` | Unpack anywhere |
|
||||
|
||||
RISC-V (riscv64): `designcraft-<ver>-linux-riscv64.tar.gz` only, cross-compiled; needs glibc 2.39+ (Ubuntu 24.04 or newer).
|
||||
|
||||
### FreeBSD
|
||||
|
||||
| Build | File |
|
||||
|
||||
+223
-222
@@ -1,229 +1,230 @@
|
||||
# DesignCraft roadmap
|
||||
|
||||
- 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.
|
||||
**Stage: pre-alpha** · next: alpha, ~5–7 h away; beta ~28 points and ~420–750 h beyond that (the core-workflow gate, not the percentage: ready for real work is already ~47%)
|
||||
|
||||
- AR1.1: Kashida justification tests verify space fallback without Tatweel fonts using an isolated font database, while retaining elongation assertions when fonts support it.
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (text threads by hand; the alpha gate waits on Primary Text Frame page fixes) · **Target:** Adobe InDesign 2026
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (parent canvas refresh) · **Target:** Adobe InDesign 2026
|
||||
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (bounded control click/drag input) · **Target:** Adobe InDesign 2026
|
||||
|
||||
- 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.
|
||||
- CJK1: IDML composite fonts now retain character mappings and metrics; explicit aki, tsume, jidori, em alignment/leading, named/custom kinsoku boundaries, hanging punctuation, tate-chu-yoko offsets and kenten symbols flow through composition and IDML export. Automatic kerning no longer imports inactive numeric values as enormous manual spacing. Composite definitions are editable through commands with undo/cache invalidation. Mojikumi definitions are retained and unsupported spacing-table / push-in-push-out policies are reported by preflight. Full InDesign CJK composition parity, frame-grid layout and detailed ruby remain unfinished. Warichu sets the selected run in smaller lines inside the line after the line is broken.
|
||||
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.
|
||||
|
||||
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.
|
||||
## Headline numbers
|
||||
|
||||
## Status (2026-10-01)
|
||||
|
||||
**Working today**
|
||||
- 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.
|
||||
- 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.
|
||||
- PDF export (krilla): real selectable text with embedded font subsets, DeviceCMYK/RGB + spot Separations, bleed boxes, crop/bleed marks + page info, pages or spreads, PDF/A-2b (PDF/X-4 output intent pending) — `file.exportPdf`, File › Export PDF…, `designcraft-cli run --export out.pdf`. File › Export PDF… now opens an "Export PDF" options dialog (General / Compression / Marks and Bleeds / Advanced) and remembers the last-used settings across exports and sessions.
|
||||
- IDML interchange (`designcraft-idml`): export and import of swatches, styles, fonts, preferences, parent spreads, spreads/pages, frames, groups, images (embedded or linked), formatted threaded stories — opens in InDesign 2026 and round-trips InDesign-exported files (`file.exportIdml`, `file.openIdml`, File → Export IDML…, CLI `--in x.idml` / `--export x.idml`).
|
||||
- Tables (M8): tables anchored in stories (header/footer/body rows, merged cells, per-edge strokes, fills + alternating fills, insets, vertical justification, at-least/exact row heights); composed into the text column (columns scale to fit, rows break across columns/frames with repeating headers/footers, overset); rendered, exported to PDF (real text) and IDML (export + import); Table menu, Table panel, Create Table dialog, caret/typing/Tab navigation and cell selection in cells; `table.*` commands.
|
||||
- Hyperlinks (text or frames → URL / e-mail / page) and bookmarks, exported as PDF link annotations and outline.
|
||||
- EPUB 3 (reflowable) export: stories in reading order, CSS from paragraph/character styles, images, navigation (`file.exportEpub`, CLI `--export x.epub`).
|
||||
- Data Merge (one linked CSV, TSV, text, or Excel file; text, image, QR, and hyperlink placeholders; preview; a new document with one record per page or records tiled on a single page), spell checking (public-domain Moby list + document dictionary, suggestions), Step and Repeat (count or grid), snippets, object styles, Numbering & Section Options.
|
||||
- Footnotes: Type › Insert Footnote (caret moves into the note), Document Footnote Options (numbering style incl. symbols, start/restart per page/spread/section, prefix/suffix, reference position/character style, paragraph style, separator, spacing, first baseline, rule above); notes composed at the bottom of the referencing column with the body text making room; edited in place on the canvas; rendered, exported to PDF (real text) and IDML (InDesign's structure, verified opening in InDesign 2026) and imported from IDML; `footnote.*` commands.
|
||||
- Paragraph Rules: Rule Above / Rule Below (on, weight, colour, tint, column or text width, offset, left/right indent) set in Type › Paragraph Rules…, the Paragraph panel menu and Paragraph Style Options › Paragraph Rules; inherited style values shown, and `type.para` / `style.paragraph.edit` change only the rule fields named.
|
||||
- Cross-references and text anchors: Insert Cross-Reference (paragraph by style, or text anchor), 11 formats with InDesign's building blocks (full/partial paragraph, paragraph text/number, page number, anchor name, chapter, file name) plus user formats; resolved live at composition — never out of date, no Update step; unresolved destinations show `??`; PDF link annotations to the destination page; IDML export/import as InDesign cross-reference sources, hyperlinks, text destinations and formats (verified in InDesign 2026); `xref.*` / `anchor.*` commands.
|
||||
- Index: page references (topic from the selection or up to 4 levels, sort keys; current page, to end of story, next n paragraphs, suppressed, See / See also), Generate / Update Index (section headings, nested or run-in, page ranges merged, Index Title / Section Head / Level 1–4 styles); IDML export/import as InDesign topics, page references and topic cross-references (verified in InDesign 2026); `index.*` commands.
|
||||
- Text clipboard keeps formatting (character/paragraph formats, footnotes, cross-references, index markers, tables) and falls back to plain text when the system clipboard changed; Paste without Formatting (⇧⌘V); Change Case (UPPERCASE, lowercase, Title Case, Sentence case — formatting kept); Type › Insert Special Character / White Space / Break Character submenus.
|
||||
- Anchored objects: items flow in the text inline (on the baseline, with Y offset; lines grow to fit) or above the line (left/center/right, space before/after); paste copied items into text to anchor them; Anchored Object Options and Release (back onto the page where shown); rendered, exported to PDF and to/from IDML (InDesign anchored object settings; verified in InDesign 2026); copy/paste and threading carry them; `anchored.*` commands.
|
||||
- Ruler guides: drag out of the rulers (page guides, or spread guides on the pasteboard), drag to move, drop on a ruler to delete; Layout › Create Guides (rows/columns with gutters, fit to margins or page); Delete All Guides on Spread; `guide.*` commands.
|
||||
- Ruler origin and zero point: Preferences › Units & Increments › Origin (Spread, Page or Spine; `document.preferences {rulerOrigin}`, IDML `RulerOrigin`); drag from the ruler corner to place the zero point (crosshair lines while dragging), double-click the corner to reset, right-click it to lock, Cmd-drag (Ctrl) to make a horizontal and a vertical guide; one zero point per document at the same place on every spread (and page, for a page origin), saved with the document and as IDML `ZeroPoint`; rulers and the X/Y fields (Control, Properties, Info, `transform.set`) measure from the origin plus the zero point; moving it is not an undo step; `view.zeroPoint` / `view.lockZeroPoint`.
|
||||
- Links panel: every placed graphic with status (OK / Modified / Missing / Embedded, checked against the file on disk), page and effective PPI (low resolution flagged); Relink…, Go To, Update, Embed; `links.*` commands.
|
||||
- Missing fonts: highlighted pink on screen (never in output), listed by Preflight; Type › Find/Replace Font (fonts in the document, missing first; replace in text, cells, footnotes and styles); `font.list` / `font.replace`.
|
||||
- Missing glyphs, as in InDesign: a character the applied font lacks is drawn as that font's missing-glyph box (its .notdef; a drawn box when the font's is empty), on screen and in PDF (as a path, so PDF/A and PDF/UA exports take it), and Preflight lists the characters by font. Preferences › Composition › Draw Missing Glyphs from Fallback Fonts (`document.preferences {glyphFallback}`, stored with the document) draws them from other fonts instead; it is off in new documents and IDML imports, on in documents saved before it existed. A missing font's substitute always uses fallback fonts.
|
||||
- Damage-region repaint: after an edit the canvas re-renders only the frames whose composed text (or the items that) changed and patches its texture — pixel-identical to a full render; typing in a frame repaints in under a millisecond instead of a full-viewport render (22 ms measured on the sample).
|
||||
- Crash recovery: unsaved documents are written to the recovery folder every 30 s and reopened (unsaved, remembering their files) after a crash; File › Revert and Save a Copy…; `file.recovery.*`, `file.revert`, `file.saveACopy`.
|
||||
- Graphics backend chosen before the window exists (DirectX 12 on Windows, Vulkan on Linux, Metal on macOS, OpenGL as the fallback; `WGPU_BACKEND` overrides) with a start-up fallback: a backend whose start never showed a frame is skipped at the next start and the status bar says so (`gpu.json`). Found on an AMD hybrid-graphics laptop whose Vulkan driver faulted at the first present.
|
||||
- Edit › Paste Into (copied objects become a frame's content, clipped by it; rendered, PDF and IDML both ways — verified in InDesign 2026); Polygon Settings (sides, star inset; double-click the Polygon tool).
|
||||
- Color panel (fill/stroke proxy, CMYK/RGB/Lab sliders with channel ramps, tint slider for swatches, spectrum ramp, Add to Swatches) and Color Picker (double-click the proxy); mixed colours are unnamed (not listed in Swatches until added); `object.color`, `swatch.addToSwatches`, `swatch.addUnnamed`.
|
||||
- Place text files: Word (.docx: styles by name with their attributes, bold/italic/underline/size/font, footnotes, tables, tabs, breaks), RTF and plain text, into the insertion point, the selected frame or a new frame; autoflow adds pages and threaded frames until the text fits; Remove Styles option (`designcraft-textimport`).
|
||||
- Gradient Feather (⇧G tool, `object.gradientFeather`): objects fade along a linear or radial opacity gradient, on screen and as a soft mask in PDF.
|
||||
- Script Label (`object.label`, `object.findByLabel`), alt text (`object.altText`), Isolate Blending / Knockout Group (`object.transparencyGroup`; on screen, PDF isolates), Table ▸ Sort (`table.sortRows`, numeric-aware, header/footer fixed).
|
||||
- File ▸ Export Text (`file.exportText`): a story as Text Only or RTF (fonts, styles, alignment, indents, tables as tab-separated rows); Export EPUB now asks for a path in the UI.
|
||||
- Tagged PDF (`file.exportPdf {tagged}`, on by default from the UI): structure tree with stories as paragraphs in reading order, figures carrying their alt text, parent-page items and printer's marks as artifacts.
|
||||
- Global Light (`object.globalLight`, `globalLight` on drop/inner shadows; Use Global Light in the Effects panel) and Generate Static Caption (`object.caption`: template over name, path, alt text, label, effective ppi, dimensions, format; any side, offset, style).
|
||||
- Control panel scale X/Y, rotation and shear fields (`transform.info`, absolute `transform.set {scaleX, scaleY, rotation, shear}` about the reference point) and Preferences › Transformations are Totals for nested objects and placed graphics.
|
||||
- Variable fonts: each named instance is a style (Font menu, shaping with feature variations, metrics, outlines) and embeds at its axis settings in PDF; free axis sliders still to come.
|
||||
- Object ▸ Select (first/next above, next/last below, container, content, previous/next in group), full Object ▸ Convert Shape (`object.convertShape`), Table ▸ Split Cell Horizontally/Vertically.
|
||||
- Attributes panel (`object.attributes`: Overprint Fill/Stroke/Gap, Nonprinting), with Overprint Gap in Overprint Preview and IDML.
|
||||
- Page Numbering View (Preferences: section or absolute): page labels everywhere follow it; Go to Page takes section names, numbers or `+n`.
|
||||
- Preferences › Advanced Type (document `advancedType`: superscript/subscript size and position; IDML TextPreference); subscripts now drop 33.3% like InDesign.
|
||||
- Preferences › Composition › Highlight: keep violations, H&J violations (three shades), custom tracking/kerning, substituted fonts.
|
||||
- File ▸ Export HTML (`file.exportHtml`): one self-contained page in reading order, styles as CSS, images inline, alt text from Object Export Options (also in EPUB).
|
||||
- Swatches panel colour groups (`swatch.newColorGroup`, `moveToGroup`, `ungroupColorGroup`, `renameColorGroup`; folders and a row context menu; IDML ColorGroup).
|
||||
- Object ▸ Generate QR Code (`object.qrCode`: web link, text, SMS, email, business card; vector modules as a compound path, into a frame or re-encoded in place).
|
||||
- Conditional text (`condition.new/options/delete/apply/list`, Conditional Text panel): hidden conditions drop their text from layout and exports, indicators underline on screen, IDML Condition/AppliedConditions.
|
||||
- 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 give pages with transparency a DeviceCMYK or DeviceRGB page group, so viewers and RIPs blend in that space. On screen, spreads with transparency in a CMYK blend space are shown through the working CMYK profile (their colours 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.
|
||||
- Ink Manager (`ink.list`, `ink.options`; Swatches panel menu): All Spots to Process, per-ink conversion and ink aliases in PDF output; IDML ConvertToProcess/AliasInkName.
|
||||
- Appearance of Black complete: Printing/Exporting rich black for RGB output (PNG), Overprint [Black] Swatch at 100% (document, IDML OverprintBlack).
|
||||
- Hyperlinks and Bookmarks panels (Window ▸ Interactive): new from the selection, edit (`hyperlink.edit`), go to source (`hyperlink.goToSource`), rename bookmarks (`bookmark.rename`), go to page, delete.
|
||||
- Nested line styles (`nestedLineStyles` paragraph attribute, Paragraph Style Options › Drop Caps and Nested Styles, IDML AllNestedLineStyles): the first lines restyle until the line breaks settle.
|
||||
- User dictionary hyphenation exceptions (`hyphenation.addException/removeException/list`, Edit ▸ Spelling ▸ User Dictionary): `ex~am~ple` breaks only at `~`, a plain word never hyphenates.
|
||||
- Frame Fitting Options (`object.fittingOptions`, Object ▸ Fitting ▸ Frame Fitting Options…): Auto-Fit refits on resize, fitting, Align From reference point, crop amounts; IDML FrameFittingOption.
|
||||
- Paragraph borders (per-side weights, colour, tint, offsets) and shading offsets, drawn per column part of split paragraphs (also when overset); Paragraph panel controls; IDML ParagraphBorder*/ParagraphShading*Offset.
|
||||
- Font menu: search, favourites (stars, Show Favorites Only; `favoriteFonts` preference) and each family previewed in its own face. Western (and other-script) families come first, then Japanese, Simplified Chinese, Traditional Chinese and Korean ones, each group alphabetical and set off by a separator; the group comes from the font's own data (`meta` languages, family names in a CJK language, OS/2 code pages, else what it covers), read once (installed fonts by the font scan, without loading them). CJK families show their native names, the English name on hover; Preferences › Type › Show Font Names in English (`showFontNamesInEnglish`) shows English names throughout. Search matches either name; documents store the English name. Every family list (Character panel, Control bar, Properties, style dialogs, Find Font, Glyphs) follows this order.
|
||||
- Application preferences (engine `Prefs`) now persist across launches (desktop: prefs.json beside ui.json).
|
||||
- View ▸ Fit Selection in Window (⌥⌘=) and Find/Change ▸ Object (`find.objects`, `find.changeObjects`: by fill, stroke, weight, opacity, kind, layer, label).
|
||||
- Custom workspaces (`window.newWorkspace/deleteWorkspace/resetWorkspace`; workspace switcher shows the current one): bars and panel arrangement saved by name.
|
||||
- Word/RTF Import Options (File ▸ Place with Import Options…, `place.styles`, `file.place {styleMap, styleConflicts}`): map imported styles onto the document's, use existing / redefine / auto-rename on conflicts, or remove formatting.
|
||||
- Custom anchored objects (`anchored.insert/options {position: custom}`): placed relative to the anchor, frame, column, page margins or page edge with reference points and offsets, no space in the text, keep within column; IDML Anchored position.
|
||||
- Variable font axes: Character panel sliders; any axis values (`fontStyle: "Bold {wght:650}"`) make that instance for layout, screen and PDF.
|
||||
- Image Import Options › Crop to for placed PDFs (`file.place {pdfCrop: crop|trim|bleed|art|media}`, Place PDF dialog): the frame shows that page box.
|
||||
- Stroke styles: striped (thick-thin… and custom bands), wavy and straight-hash strokes now draw on screen and in PDF; named custom stripe/dash/dot styles (`strokeStyle.new/delete/list`, Stroke panel Type list; IDML Striped/Dashed/DottedStrokeStyle).
|
||||
- Object ▸ Primary Text Frame (`object.primaryTextFrame`): any text frame's story can become (or stop being) the primary story Smart Text Reflow follows.
|
||||
- Column rules (Text Frame Options › Column Rules, `object.textFrameOptions {columnRule, columnRuleWeight, columnRuleColor, columnRuleTint, columnRuleOffset, columnRuleTopInset, columnRuleBottomInset}`, reported by `document.inspect`): a bar centred in each gutter of a multi-column frame, inside the insets and between the columns of right-to-left and vertical frames too, broken around paragraphs that span columns, with weight, colour, tint, horizontal offset and top/bottom insets; on screen, in PDF (tagged as an artifact) and in IDML (`ColumnRule…` attributes of `TextFramePreference`, also in object styles). The dialog shows the frame's options and changes only the fields edited. Rule stroke types and overprint aren't there yet.
|
||||
- Place a page of an IDML or DesignCraft document (`file.place {layoutPage}`): its objects, stories, styles and swatches as one group (INDD files are not readable).
|
||||
- Articles panel (`article.new/add/remove/options/delete/list`): reading order and content of EPUB and HTML exports.
|
||||
- Object ▸ Clipping Path (`object.clippingPath {type: alpha|edges, threshold, tolerance}`): traces the placed image and makes the outline the frame.
|
||||
- Gridify: arrow keys while dragging a frame or shape tool add columns/rows (`frame.grid`), one undo step.
|
||||
- Gap tool (U, `gap.move`): drag the space between objects (or an object and the page edge) and both sides resize.
|
||||
- XML basics (`xml.newTag/deleteTag/tag/mapStyle/structure`, `file.exportXml`, `file.importXml`; Tags panel with structure; View ▸ Show Tagged Frames): tagged frames export in reading order, mapped paragraph styles become elements, imported elements flow into frames with matching tags. Inline text tagging (`xml.tagText`, an `xmlTag` character attribute, exported as nested elements) and tag markers (View ▸ Structure ▸ Show Tag Markers) too. DTDs (`xml.loadDtd`, `xml.validate`, `xml.deleteDtd`; Tags panel): loading one adds its elements as tags and names the root; validation checks content models (sequences, choices, `?*+`, mixed content, EMPTY/ANY, parameter entities) and required attributes, and lists each problem with its element path.
|
||||
- Adjust Layout / Layout Adjustment (`adjustLayout` on Document Setup and Margins and Columns): objects follow each page's margin box to its new size and position.
|
||||
- Content Collector and Placer tools (B; `conveyor.collect/place/list/clear`): copies of objects (with stories, styles, images) ride a conveyor between pages and documents; Alt-click keeps them on it.
|
||||
- Linked stories (Edit ▸ Place and Link, `story.placeAndLink/links/updateLink/unlink`): child stories copy a parent, show out of date in the Links panel and update.
|
||||
- Edit ▸ Menus (`window.hideMenuItem`): hide menu items; menus with hidden items end in Show All Menu Items.
|
||||
- View ▸ Separations Preview (`view.separations`): one process plate as ink density, or areas over an ink limit in red (a GCR preview of the rendered view; spot plates and the flattener preview aren't there).
|
||||
- Separations Preview plates now come from each object's own colour (CMYK as authored; RGB and images through the colour settings, images via a cached 17³ table), so pure black shows only on the black plate.
|
||||
- Language (Character panel ▸ Language, Properties ▸ Character, `type.char {language}`): text in other languages isn't hyphenated or spell-checked with the English dictionaries, typographer's quotes follow the language (German „…“, French and Russian «…», Swiss, Spanish, Dutch, Nordic, Japanese and Traditional Chinese 「…」, Simplified Chinese “…”), the font's localized forms (OpenType `locl`: Turkish i, Bulgarian Cyrillic, Japanese / Chinese / Korean glyph variants) follow it too, and so does the fallback for CJK characters the font lacks (with Draw Missing Glyphs from Fallback Fonts on): Japanese, Simplified Chinese, Traditional Chinese and Korean each try their own installed system fonts (Japanese starts with the craft-fonts Japanese faces, Mincho first, when built with them; Hangul, kana and Bopomofo pick their language's fonts whatever the language). Language names from IDML keep their spelling; locale codes as InDesign writes them for some languages (`de_DE_2006`, `nl_NL_2005`; any `ll`, `ll_CC`, `ll-CC`) are their language and region for all of the above, and Chinese is also recognised in its common spellings ("Simplified Chinese", "Chinese (Traditional)", `zh_CN`, `zh-Hant`). Spanish is hyphenated with Javier Bezos's Liang patterns (`hyph-es.tex` 5.0, MIT) through the `hypher` crate; hyphenation for other languages isn't bundled yet.
|
||||
- Books (`book.new/open/add/remove/list/styleSource/paginate/syncStyles/exportPdf`, Book panel): `.dcbook` files, continuous page numbering, style sync from a style source, one merged PDF.
|
||||
- Track Changes (`changes.track/list/acceptAll/rejectAll`, Type ▸ Track Changes): typing is marked added (highlighted on screen), deletions are kept but hidden from layout and exports until accepted or rejected. Per-change accept/reject and authors aren't there yet.
|
||||
- Track Changes: per-change Accept / Reject (`changes.accept`, `changes.reject`) and a Track Changes panel listing every change.
|
||||
- Move table rows and columns (`table.moveRow`, `table.moveColumn`, Table panel buttons; with whole rows or columns selected, drag them with the Type tool — `table.dropCells`).
|
||||
- Distribute Rows Evenly (`table.distributeRows`, Table menu and panel): selected rows receive equal fixed heights preserving their composed total height, with undo/redo. Row-spanning merges and rows outside the composed frames are rejected; shrinking tall cells can overset their text.
|
||||
- Object States (`states.create/show/rename/release/list`, Object States panel): multi-state objects show one state on screen and in PDF.
|
||||
- Buttons (`button.set/clear/list`, Buttons and Forms panel): go to page / next / previous / first / last / URL as PDF link annotations. Form fields too (`form.set/clear/list`: text fields, check boxes, combo and list boxes, signature fields; required, multiline, defaults) exported as AcroForm widgets through an incremental update (not in PDF/X).
|
||||
- Power Zoom (Hand tool: press and hold still for half a second, or Alt-press): zoom out to the spread, aim the red view rectangle, release to zoom back in there.
|
||||
- Graphic cells (`table.placeGraphic`, `table.textCell`, Table › Convert Cell Type, Table panel): images in table cells, fitted or filled, clipped to the cell on screen and in PDF.
|
||||
- Live Distribute (Space while dragging a selection handle; `transform.resize {distribute}`): objects keep their size and spread with the bounds.
|
||||
- Liquid Layout (`liquid.pageRule/object`, `guide.liquid`, `layout.createAlternate`, Liquid Layout panel): scale / re-center / guide-based / object-based page rules applied on page resize and Document Setup; dashed liquid guides; alternate layouts as resized page copies in a named section. Text in an alternate layout stays linked to the original stories (Links panel shows them out of date; `story.updateLink`).
|
||||
- Split Window / New Window (`window.split`, `window.newWindow`, Window ▸ Arrange): two side-by-side views of the document with their own zoom, scroll and render cache. New Window puts the second view in its own OS window (an egui viewport; a floating window on the web).
|
||||
- Transparency flattener (`file.exportPdf {flatten: high|medium|low|ppi}`, Edit ▸ Transparency Flattener Presets): each object involving transparency becomes an opaque image of its area over what's beneath it, so everything else stays vector (spreads with transparent parent items are rasterised whole).
|
||||
- Video and sound (place .mp4/.mov/.webm/.mp3/.wav…, `media.options/get`, Media panel): media frames show a poster or placeholder on screen, in print and in PDF, and export as HTML5 `<video>`/`<audio>` in EPUB. Interactive PDF export (`file.exportPdf {media: true}`, File ▸ Export PDF (Interactive)) embeds each file and plays it in a Screen annotation through a rendition action (controls, loop, play on page load). No in-app playback.
|
||||
- Right-to-left text (World-Ready basics): right-to-left runs are shaped right to left, lines are reordered by the Unicode Bidirectional Algorithm (matched against the reference implementation), Paragraph Direction (`type.para {direction}`, Paragraph panel, IDML ParagraphDirection, EPUB `dir`). Digits (`digits`: Default, Arabic, Hindi, Farsi, Native by language; Character panel for right-to-left text; IDML DigitsType) draw 0–9 in another script. Kashidas (`kashidas` paragraph attribute, on by default): justified Arabic lines stretch at one join per word (after seen/sad when possible, never inside lam–alef) with a drawn tatweel before word spaces widen. Carets follow the drawing: the caret before a right-to-left glyph sits at its right edge, arrow keys move as drawn (by words in the line's direction), clicks find the nearest drawn caret position and selections highlight the selected glyphs. Binding: Right to Left (Document Setup, `layout.documentSetup {binding}`, IDML PageBinding) makes page 1 a left page and lays spreads out right to left.
|
||||
- Interface language (Edit ▸ Interface Language, `app.language`): German, French, Spanish, Italian, Japanese, Simplified Chinese and Arabic menu titles, common menu items and panel names (our own translations). Common dialog, panel and tooltip labels are also localized, with English fallback for untranslated strings. Arabic translations cover every key in the shared interface translation table, including menu commands and utility labels. The Type menu's Story Direction, Horizontal/Vertical, Tate-Chu-Yoko, Ruby… and Kenten are translated too. Arabic labels use Unicode bidi run ordering and contextual shaping; welcome and new-document form layouts follow RTL direction independently of document binding. CJK and Arabic glyphs come from [craft-fonts](https://github.com/storytold/craft-fonts) when built with `CRAFT_FONTS_DIR` (all releases): BIZ UDPGothic and Noto Sans CJK SC for the UI (Chinese forms first in the Chinese interface), Noto Sans Arabic, and Mincho for Japanese documents (the web build embeds BIZ UDPGothic only); otherwise from system fonts. Ukrainian covers all 1,430 existing catalog keys, including publishing dialogs and built-in names, with persisted selection, grammatical count captions, English fallback and bundled Cyrillic UI glyph coverage.
|
||||
- Page transitions (`page.transition/transitions`, Page Transitions panel): twelve transition types with speed and direction per spread, written into exported PDFs as `/Trans` entries through an incremental update.
|
||||
- PDF/X-4 export (`file.exportPdf {standard: "x4"}`): an output intent with our own Generic CMYK ICC profile (lut16 tables generated from the parametric press model), GTS_PDFXVersion in Info and XMP, `pdf:Trapped` in XMP as in Info, RGB images without a profile tagged sRGB, no image interpolation (none in PDF/A either), and built-in PDF/X-4 checks reported as warnings. qpdf finds no errors in the result.
|
||||
- EPS place: placed at its (high-resolution) bounding box and shown, printed and exported through its TIFF preview, or a placeholder when it has none. PostScript itself isn't interpreted.
|
||||
- Preferences: Dictionary (user dictionary; `spelling.setWords/words`), Spelling (Dynamic Spelling: red squiggles on the canvas, also Edit ▸ Spelling), Autocorrect (word list, applied as you type), Notes, Track Changes, Story Editor Display and File Handling (recovery interval) sections.
|
||||
- Colour management exposed: Edit ▸ Color Settings (`color.settings`: RGB/CMYK working spaces, intent, black-point compensation; `color.loadProfile` for ICC files; `color.convert` through the working spaces, with gamut checks) and View ▸ Proof Setup / Proof Colors (press, sRGB, legacy Mac RGB, colour-blindness simulations, simulate paper) on the canvas.
|
||||
- Keyboard: Tab / Shift+Tab hide and show all panels / all but Tools (`window.hidePanels`, `window.hidePanelsExceptTools`); Cmd+F6 / Cmd+Shift+F6 cycle documents; panel fields are reached with Tab and Escape returns to the layout.
|
||||
- Smart guides and snapping (View ▸ Snap to Guides / Snap to Document Grid / Smart Guides, `view.snapPreferences` and Preferences ▸ Guides & Pasteboard): moving, drawing, resizing, rotating and pen/anchor drags snap in order to the document and baseline grids, ruler guides, margins and columns, other objects' edges and centres (parent-page items too, by visible stroked bounds), equal spacing, matching dimensions and matching rotation, with guides, gap marks and measurements drawn on the canvas. Shift and Cmd constrain the gesture.
|
||||
- View ▸ Rotate Spread (`view.rotateSpread`, `layout.rotateSpreadView`): the spread in view turns in quarter turns on screen (stored with the document; output is unaffected). The canvas maps each spread through its own transform, so tools, snapping, resize handles, guides, overlays and rendering all follow the turned spread.
|
||||
- Vertical type (Vertical Type Tool, Type ▸ Story Direction `type.storyDirection`, IDML StoryOrientation): the direction belongs to the story, so every frame of a thread is vertical or horizontal together and a frame threaded onto a story takes its direction (`object.textFrameOptions {vertical}` sets the frames' stories); lines run top to bottom and follow each other right to left; the arrow keys follow them (Down and Up along the line, Left and Right to the next and previous line, keeping the position along it); CJK characters stay upright and other text turns, on screen and in PDF; CJK line breaking with basic kinsoku (also in horizontal text); Korean breaks at spaces, word by word (Hangul in any language, hanja in Korean text), or between syllables when the paragraph asks for it (`type.para {koreanCharBreaks}`, Paragraph Style Options ▸ Justification; not written to IDML). Upright text is shaped top to bottom: punctuation and brackets take their vertical forms (OpenType `vert`; `vkrn` and `vpal` through OpenType features), glyphs advance by their vertical metrics (`vmtx`) and hang from their vertical origins (`VORG`) on the centre of the ideographic em box (`BASE`, else OS/2), which the first line's ascent follows; turned text keeps its horizontal forms. Tate-chu-yoko (Type ▸ Tate-Chu-Yoko, `type.tateChuYoko`, IDML `Tatechuyoko`) sets a run such as "12" side by side within one em of the line. Ruby (Type ▸ Ruby…, `type.ruby`, IDML RubyString; group ruby spread 1-2-1 or centred) and kenten emphasis dots (`type.kenten`, IDML KentenKind) are set over their text — to its right in vertical frames — without changing line breaks. Warichu and ruby options (per-character ruby, positions, overhang) aren't there yet.
|
||||
- Math expressions (Type ▸ Insert Math Expression…, `math.insert`, `math.svg`): a LaTeX subset (fractions, scripts, roots, big operators with limits, Greek, relations and operators with math spacing, `\left…\right`, `\text`) typeset by our own engine into SVG, placed inline at the cursor or as a frame; the LaTeX is kept as alt text and can be edited.
|
||||
- Object Layer Options for placed PDFs (`object.pdfLayers`, `object.layerOptions`, Object ▸ Object Layer Options…): the file's optional-content layers listed and shown or hidden per frame on screen; in exported PDFs such graphics go out as 300 ppi images of their visible layers.
|
||||
- Effects: Inner Glow (edge or centre), Bevel and Emboss (inner bevel lit from the light angle), Satin (`object.innerGlow`, `object.bevel`, `object.satin`); PDF export now keeps soft effects: objects with shadows, glows, feathers, bevels or satin go out as 300 ppi transparent images of their appearance (threaded text frames excepted).
|
||||
- Directional Feather (`object.directionalFeather {widths: [top, left, bottom, right]}`): each side fades over its own width.
|
||||
- View ▸ Flattener Preview (`view.flattenerPreview`): objects involving transparency highlighted in red; knockout groups export as images (PDF output has no knockout groups).
|
||||
- Live captions (`object.caption {live: true}`, Object ▸ Captions ▸ Generate Live Caption): the caption text is re-filled from the source object's metadata after every edit (same undo step).
|
||||
- Style Export Tagging (`style.exportTag`, Paragraph Style Options ▸ Export Tagging): paragraph and character styles choose their HTML element and class in EPUB/HTML.
|
||||
- Tagged Text: export (`file.exportText {format: "tagged"}`) and place (detected by its header; ASCII, UTF-8 or UTF-16) with paragraph and character styles, font, typeface and size, and `<0x…>` characters.
|
||||
- Tagged PDF: one structure element per paragraph (H1–H6 and BlockQuote from Export Tagging, else P) in reading order, frame fills, strokes, rules and tables as artifacts, figures with alt text.
|
||||
- EPUB: cover image from the first page (`file.exportEpub {cover}`), and Fixed Layout export (`file.exportFixedEpub`, File ▸ Export EPUB (Fixed Layout)…): pre-paginated pages as 144 ppi images with their text kept for search and read-aloud.
|
||||
- Interactive PDF options (`file.exportPdf {fullScreen, bookmarksPanel, pageLayout, view, advanceSeconds}`, File ▸ Export PDF (Interactive)…): page mode, layout, opening view and page timing in the catalog and pages.
|
||||
- Object Export Options (`object.exportOptions`): tagged-PDF artifact, EPUB/HTML rasterize (the object as an image), alignment and page break before, alt text.
|
||||
- Scripts panel (Window ▸ Utilities ▸ Scripts, `script.run`): saved command scripts (with `$N.path` references to earlier results) run as one undo step; edit, add, load and delete them in the panel. Scripts use DesignCraft's command language, not ExtendScript.
|
||||
- Drag and drop text editing: drag selected text to move it (Alt copies) with its formatting (`text.release`).
|
||||
- Image Import Options: place any page of a multi-page PDF (`file.place {pdfPage}`; the app asks which page), shown on screen and embedded as that page in PDF export.
|
||||
- Define Lists: named numbered lists that continue across stories in page order (`list.define`, paragraph `listName` / `startAt`), round-tripped through IDML; `null` in `type.para` / `type.char` now removes an override.
|
||||
- View › Overprint Preview (⌥⇧⌘Y): overprinting fills, strokes and 100% [Black] text mix with what's beneath on screen.
|
||||
- Type on a Path (⇧T tool, `type.onPath`): a story along any line, curve or shape on screen and in PDF; Type on a Path Options (start, flip, baseline/ascender/descender/centre) and Delete Type from Path. Round-trips through IDML as TextPath; on-path caret drawing is still to come.
|
||||
- Preferences › Appearance of Black: 100% K on screen accurately (dark grey) or as rich black (`window.richBlack`); the printing/export side is still to come.
|
||||
- Spreads of up to 10 pages: Add to Previous Spread and Allow Spread to Shuffle (Pages panel; `layout.pages.toSpread`, `layout.spreadShuffle`); inserted pages go around kept spreads.
|
||||
- Page tool (⇧P) and `layout.pageSize`: pages of their own size (or a preset); the spine stays put and objects keep their place on their page.
|
||||
- File › Print (⌘P): printer, copies, page range, spreads, marks, bleed; printed as PDF through the system spooler (`file.print`, `file.printers`, `dryRun` for agents). Shortcuts now act like choosing the menu item (dialogs open), and UI commands win shortcut ties (⌘N opens New Document).
|
||||
- Nested styles (through/up to N sentences, words, characters, letters, digits, tabs, breaks, spaces or given characters) and GREP styles (regex → character style), edited in Paragraph Style Options and composed under local formatting; `ui.dialog.open` for agents. Nested line styles are still to come; nested and GREP styles round-trip through IDML.
|
||||
- Table and cell styles (round-trip through IDML): cell styles (fill, insets, vertical justification, strokes, paragraph style) and table styles (region cell styles, border, alternating rows, spacing); apply from the Table panel, edits re-apply to every user; `style.cell.*`, `style.table.*`.
|
||||
- Style groups: paragraph and character styles shown in group folders, Move to Group (new, existing, none); `style.group` renames every use (styles, stories, table cells, footnotes, object styles).
|
||||
- Scrubby zoom: drag the Zoom tool left or right to zoom continuously around the press point.
|
||||
- Guides belong to the active layer: hidden with it (or with its Show Guides off) and locked with it.
|
||||
- Pencil tool (N): freehand strokes simplified to smooth paths (Alt closes); Smooth and Erase tools re-fit or remove the stretch you drag along (`path.smooth`, `path.erase`).
|
||||
- Edit › Keyboard Shortcuts: every command, click and press the new keys, conflicts shown, per-command and global reset; `window.setShortcut`.
|
||||
- Primary Text Frame (New Document) and Smart Text Reflow: pages are added while the primary story oversets and empty ones at the end removed, in the same undo step (Preferences › Type).
|
||||
- Glyphs panel: every character of any font (rendered by our renderer), search by character or U+code, click to insert, recently used.
|
||||
- UI scaling (Preferences › Interface, 50–200%; `window.uiScale`); the window title steps aside when the bar is crowded.
|
||||
- Underline / Strikethrough Options: weight, offset, colour and tint (Character panel; screen, PDF and IDML).
|
||||
- Relink to Folder and Relink File Extension (`links.relinkFolder`; missing links found by name).
|
||||
- File › Package (⌥⇧⌘P): the document relinked to a Links folder, the placed files, the font files that draw its text in a Document Fonts folder (the fonts it names and any fallback fonts that draw characters those lack; except fonts whose licence restricts it, OS/2 `fsType`), an IDML copy, an optional PDF and a report (fonts, fallback fonts, links, preflight, instructions); Copy Links To (`links.copyTo`).
|
||||
- Document fonts: opening a `.designcraft` or `.idml` file loads the fonts in the `Document Fonts` folder beside it (as Package writes it), ahead of installed fonts with the same name, so a packaged document opens with its fonts on another machine. Damaged, oversized (over 256 MB per file, 512 MB in all) and surplus (over 200) font files are skipped and listed in `file.open`'s `warnings`; `font.list` gives each font's `source`. Document fonts belong to their document, as in InDesign: other open documents don't see them (each document with a same-named font draws with its own), and closing the document (or reverting it, or a book chapter's export ending) makes them unavailable. Their files stay in memory until DesignCraft quits, so reopening a document doesn't read or load them again; on the web there is no folder to read.
|
||||
- Load Swatches / Save Swatches for Exchange (.ase: CMYK, RGB, Lab, Gray; spot or process) from the Swatches panel menu; `swatch.load` / `swatch.save`.
|
||||
- Pathfinder (Object › Pathfinder and a Pathfinder panel): Add, Subtract, Intersect, Exclude Overlap, Minus Back; curves stay curves (flo_curves); `object.pathfinder`.
|
||||
- Make / Release Compound Path (⌘8; nested paths become holes), Create Outlines (⇧⌘O; one path per text colour), Scissors tool (C) with `path.split`.
|
||||
- Transform Again / Individually / Sequence Again (⌥⌘3; a moved copy repeats like Step and Repeat), Clear Transformations; Layers: Merge, Delete Unused, Hide/Lock Others, Show/Unlock All; Break Link to Style (paragraph and character).
|
||||
- Floating panels: tear a panel off the dock (its float button, or drag its header away) into a movable, resizable window; Dock puts it back; positions persist; `window.floatPanel` / `window.dockPanel`.
|
||||
- Place SVG (vector in PDF export via krilla-svg, resvg on screen, text in the bundled fonts), Photoshop (.psd/.psb composite in its colour mode — Bitmap, Grayscale, Duotone, Indexed, RGB, CMYK, Lab, 8/16-bit — with merged transparency; extra alpha and spot channels don't change the image), BMP, and Illustrator files saved with PDF compatibility (.ai). New `designcraft-images` crate (L2) for sniffing, sizes and decoding. EPS is still missing.
|
||||
- View › Display Performance: Fast (grey boxes, no effects, ⌥⇧⌘Z), Typical (72 ppi proxies, ⌥⌘Z), High Quality (⌥⌘H, our default — the renderer is fast enough).
|
||||
- OpenType menu (Character panel): discretionary ligatures, fractions, ordinal, swash, titling, contextual alternates, slashed zero, the four figure styles and stylistic sets 1–20; `type.openType`; mapped to IDML's OTF attributes.
|
||||
- Add Anchor Point (=), Delete Anchor Point (-) and Convert Direction Point (⇧C) tools (click toggles smooth/corner, drag pulls out handles), with `path.addAnchor`, `path.deleteAnchor`, `path.convertAnchor`.
|
||||
- Gradients: the Gradient Swatch tool (G) drags a gradient's start and end across objects (Shift: 45°); the Gradient panel sets type, angle, reverse and edits stops on a ramp (drag, add, drag off to remove, location, colour). Edited gradients are unnamed swatches; `object.gradient`; the vector round-trips through IDML.
|
||||
- Document Setup (⌥⌘P): intent, number of pages, start page # (an even start begins with a left page), facing pages, size presets and orientation, bleed and slug per edge; `layout.documentSetup {}` reports the setup.
|
||||
- Stroke panel: cap, join, miter limit, alignment, type, Start/End arrowheads (12 kinds, drawn on screen and in PDF; the path is shortened under pointed heads) and gap colour under dashes and dots.
|
||||
- Preferences (⌘K: General, Type, Units & Increments, Grids, Guides & Pasteboard, Display Performance) backed by `prefs.set` (application) and `document.preferences` (units, increments, grids — undoable, saved with the document; right-click a ruler to pick its units, the horizontal and vertical rulers each have their own); scaling applies to content and scales stroke weights (Include Stroke Weight), and X/Y/W/H measure the stroke's outer edge (Dimensions Include Stroke Weight). Quick Apply (⌘Return) finds paragraph, character and object styles as well as commands.
|
||||
- Parent item overrides: Cmd+Shift-click a parent item on a page (or Override All Parent Page Items, ⌥⇧⌘L) makes an editable local copy that hides the parent's; Remove All Local Overrides and Detach All Objects from Parent in the Pages panel menu.
|
||||
- Place PDF: placed PDF pages are sized by their crop box, drawn on screen through hayro (with mip levels) and embedded in exported PDFs as vector form XObjects (krilla), `<PDF>` in IDML.
|
||||
- Menus expose the engine's features (TOC, numbering & sections, text variables, hyperlinks, cross-references, footnotes, step and repeat, spelling, fitting/content, effects, Data Merge, Preflight, EPUB); any menu command with parameters gets a dialog generated from its parameter documentation.
|
||||
- Table of contents (generate/update, dot leaders) and text variables (running headers, last page number, chapter number, file name, dates, custom) resolved per page.
|
||||
- Soft effects: blurred drop shadow, inner shadow, outer glow, basic feather.
|
||||
- ~200 commands, all reachable through the JSON control channel; headless CLI rendering to PNG.
|
||||
- MCP server (`designcraft-cli mcp [--connect PORT]`, docs/mcp.md): headless engine or the running app; commands, batch, document/story inspection, page renders as images, window screenshots, pointer/keyboard/dialog input.
|
||||
- Web build (`apps/designcraft-web`, trunk): the same UI on WebGPU with a WebGL2 fallback; open/place via the browser file picker or drag-and-drop, save/export as downloads.
|
||||
|
||||
**Next (in order):** books · New Window / split view · Ink Manager and Separations Preview · XML structure, tags and export · buttons and forms · PDF/X-4 output intent (needs krilla support) · EPS place.
|
||||
|
||||
## How far from full parity (estimate, 2026-10-04)
|
||||
|
||||
**Breadth: ~99% weighted** (P0 core 99%, P1 100%, P2 97%) over the 275 features of the InDesign catalogue, scored
|
||||
row by row in [docs/parity.md](docs/parity.md) (`cargo xtask parity` recomputes it). Many features scored done still
|
||||
lack some of InDesign's options or dialog details, so **overall parity including depth is about 83%**.
|
||||
|
||||
**Remaining work: about 170 wall-clock hours of a single Claude Opus 5.5 agent** (±30%), or roughly 60–85 hours with
|
||||
four agents in parallel on separate crates:
|
||||
|
||||
| Work | Estimate |
|
||||
|---|---|
|
||||
| Open P0 (2: PDF/X-4 validation against a certified checker, PostScript interpretation for EPS) | 8 h |
|
||||
| Open P1 (none partial) | 0 h |
|
||||
| Open P2 (3 partial: localization breadth, INDD pages (not readable clean-room), in-app media playback) | 14 h |
|
||||
| Depth and pixel fidelity of every dialog, panel and menu against InDesign 2026 | 90 h |
|
||||
| Performance (incremental composition, GPU raster) and interchange hardening (IDML/PDF corpus) | 55 h |
|
||||
|
||||
Basis: since the first estimate this effort landed about 45 arcs (from parent overrides, Preferences, Stroke panel,
|
||||
gradients and Pathfinder to table/cell styles, nested/GREP styles and Print) in roughly 14 hours on a heavily loaded
|
||||
machine — about 1–2 hours per catalogue row.
|
||||
|
||||
## Agents: CLI and MCP
|
||||
|
||||
Every command is reachable from `designcraft-cli` (`run`, `script` with `$N.path` result references, `app` for the
|
||||
running window, `describe`, `commands`), from MCP (`designcraft-cli mcp`, tools including `execute` and `batch` with the
|
||||
same references), and from the app's JSON control channel. See [docs/agents.md](docs/agents.md).
|
||||
|
||||
## Milestones
|
||||
|
||||
| # | Milestone | State |
|
||||
| | Value | Kind |
|
||||
|---|---|---|
|
||||
| M0 | Skeleton + vertical slice | ✅ |
|
||||
| M1 | Selection, transform, layers, pages, MCP | in progress |
|
||||
| M2 | Type I (Type tool, threading, Character/Paragraph, composer) | in progress |
|
||||
| M3 | Styles (nested/GREP, bullets, keeps, span columns) | started |
|
||||
| M4 | Color & effects | started |
|
||||
| M5 | Graphics & links | started |
|
||||
| M6 | Files & export (native, IDML, PDF, PNG/JPEG) | in progress (IDML ✅, PDF export) |
|
||||
| M7 | Long documents (sections, TOC, index, footnotes, books) | in progress (sections, TOC, text variables/running heads, hyperlinks, bookmarks, footnotes, cross-references, index) |
|
||||
| M8 | Tables | ✅ core (table/cell styles, rotation, diagonal lines pending) |
|
||||
| M9 | Performance (MT composition, tiles) | in progress (glyph path cache, bounded RTL label layout cache, culling, parallel compose 6.7× faster, perf harness) |
|
||||
| M10 | Find/Change, spelling, Preflight | in progress (Find/Change ✅, spelling ✅, Preflight) |
|
||||
| M11 | Layout power features (liquid/alternate layouts, data merge) | started (data merge phase 1, step & repeat, snippets) |
|
||||
| M12 | Interactive & digital (EPUB, HTML, interactive PDF) | started (EPUB, PDF links/bookmarks) |
|
||||
| M13 | Automation (scripts, batch) | |
|
||||
| M14 | 1.0 polish & packaging | |
|
||||
| **Feature breadth** | **~80%** (69% of InDesign 21.6's in-scope menu items matched; 99% of our 275-row checklist) | measured bounds, estimate between |
|
||||
| **Ready for real work** | **~47%** (range 42–52%) | estimated: weighted sum over the dimensions below |
|
||||
| Mainstream practitioner | **~42%** (weekly core 60.7% × 0.88 interaction × 0.92 stability × 0.85 file exchange) | estimated |
|
||||
| Essentials user | **~54%** (core features 72% × 0.90 launch × 0.88 discoverability × 0.95 files received) | estimated |
|
||||
| Remaining to **beta** | **420–750 Opus 5.5 agent-hours** (~120–200 wall-clock hours with 4–5 agents) | estimated |
|
||||
| Remaining to **full parity** | **1,150–1,950 Opus 5.5 agent-hours** | estimated |
|
||||
|
||||
| Audience | Ready | Opus 5.5 agent-hours to ~95% | Work that dominates |
|
||||
|---|---|---|---|
|
||||
| Full target (ready for real work) | ~47% | 1,150–1,950 (~70% parallel) | depth across all areas, scripting compatibility, localization, typography fidelity, INDD/IDML |
|
||||
| Mainstream practitioner | ~42% | 500–850 (~65% parallel) | INDD import and IDML corpus, composition fidelity, weekly-area depth, PDF/X-1a and printing |
|
||||
| Essentials user | ~54% | 135–250 (~50% parallel) | start-up robustness, context menus and copy/paste, threading, fonts, Windows printing |
|
||||
|
||||
**Why pre-alpha:** it fails one of InDesign's core workflows, the alpha gate in
|
||||
[docs/roadmap.md](docs/roadmap.md#alpha-gate). Text threads by hand (click a port, then click or
|
||||
draw a frame; thread and unthread commands), but Primary Text Frame autoflow adds pages users
|
||||
can't delete (#165). Everything else in the gate works end to end on macOS (document setup,
|
||||
styles, images, object editing, PDF export, save and reopen), and ready for real work (~47%) is
|
||||
above the alpha bar, so the Primary Text Frame page fixes are the whole distance to alpha.
|
||||
Beyond that, beta needs INDD opening, IDML proven on real files, composition compared with
|
||||
InDesign's, and basic editing fixed on Windows and Linux. Details:
|
||||
[docs/target-app-parity.md](docs/target-app-parity.md).
|
||||
|
||||
## By dimension
|
||||
|
||||
| Dimension | Ready | Hours | Doc |
|
||||
|---|---|---|---|
|
||||
| Features (depth) | 55% | 250–400 | [target-app-parity.md](docs/target-app-parity.md) |
|
||||
| Typography and composition fidelity | 45% | 120–200 | [typography-parity.md](docs/typography-parity.md) |
|
||||
| File formats | 40% | 150–250 + 20–60 (INDD) | [file-format-parity.md](docs/file-format-parity.md) |
|
||||
| UI/UX fidelity | 45% | 120–180 | [ui-parity.md](docs/ui-parity.md) |
|
||||
| Stability | 45% | 60–100 | [gaps.md](docs/gaps.md) |
|
||||
| Performance | 65% | 40–80 | [gaps.md](docs/gaps.md) |
|
||||
| Localization | 45% | 120–180 | [localization-parity.md](docs/localization-parity.md) |
|
||||
| Platforms | 55% | 40–80 | [hardware-parity.md](docs/hardware-parity.md) |
|
||||
| Hardware | 45% | 20–40 | [hardware-parity.md](docs/hardware-parity.md) |
|
||||
| Ecosystem (scripting, plug-ins) | 15% | 150–250 | [gaps.md](docs/gaps.md) |
|
||||
| AI features | 15% | 60–120 | [gaps.md](docs/gaps.md) |
|
||||
|
||||
All "Ready" figures are estimates; the weights and evidence are in target-app-parity.md.
|
||||
|
||||
## Features
|
||||
|
||||
Breadth from [docs/parity-checklist.md](docs/parity-checklist.md) (`cargo xtask parity`,
|
||||
measured); Ready estimated.
|
||||
|
||||
| Area | Breadth | Ready | Hours |
|
||||
|---|---|---|---|
|
||||
| Application shell & workspace | 99% | 55% | 40–60 |
|
||||
| Documents, pages, spreads | 100% | 65% | 20–35 |
|
||||
| Layers | 100% | 75% | 5–10 |
|
||||
| Frames, shapes & paths | 100% | 60% | 25–40 |
|
||||
| Transform | 100% | 70% | 10–20 |
|
||||
| Fill, stroke, colour | 100% | 60% | 20–35 |
|
||||
| Effects & transparency | 100% | 50% | 20–30 |
|
||||
| Placing & links | 93% | 55% | 20–35 |
|
||||
| Type & text frames | 99% | 45% | 40–60 |
|
||||
| Typography | 100% | 45% | see typography-parity.md |
|
||||
| Styles | 100% | 60% | 15–25 |
|
||||
| Tables | 100% | 55% | 25–40 |
|
||||
| Long documents | 100% | 45% | 30–50 |
|
||||
| Interactivity & digital | 100% | 40% | 30–50 |
|
||||
| Output & production | 92% | 55% | 40–70 |
|
||||
| XML & automation | 100% | 35% | see Ecosystem |
|
||||
| View & navigation | 100% | 65% | 10–20 |
|
||||
| Undo, history, saving | 100% | 70% | 5–10 |
|
||||
| Accessibility | 100% | 50% | 15–25 |
|
||||
|
||||
## Languages
|
||||
|
||||
Interface strings translated, of 1,724 measured. Detail: [docs/localization-parity.md](docs/localization-parity.md).
|
||||
|
||||
| Language | Code | Status | Translated |
|
||||
|---|---|---|---|
|
||||
| English | en | full | 100% |
|
||||
| Simplified Chinese | zh | partial (no canvas IME) | 83% |
|
||||
| Spanish | es | partial | 83% |
|
||||
| Hindi | hi | none | 0% |
|
||||
| Arabic | ar | partial (RTL UI and composition) | 85% |
|
||||
| French | fr | partial | 83% |
|
||||
| Portuguese (Brazil) | pt-br | partial | 61% |
|
||||
| Indonesian | id | none | 0% |
|
||||
| Japanese | ja | partial (no canvas IME) | 95% |
|
||||
| German | de | partial | 83% |
|
||||
| Korean | ko | none | 0% |
|
||||
| Vietnamese | vi | none | 0% |
|
||||
|
||||
Also shipped: Italian (85%), Ukrainian (83%).
|
||||
|
||||
## Upcoming
|
||||
|
||||
Ranked; detail and estimates in [docs/roadmap.md](docs/roadmap.md), the full list in
|
||||
[docs/gaps.md](docs/gaps.md).
|
||||
|
||||
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. Open INDD through a clean-room converter, owner decision on #114 (20–60 h)
|
||||
5. IDML real-file corpus and fixes (60–100 h)
|
||||
6. Composition oracle against InDesign; optical kerning; overset rule (80–140 h)
|
||||
7. Start on any GPU; find every installed font (25–50 h)
|
||||
|
||||
## Documents
|
||||
|
||||
[target-app-parity.md](docs/target-app-parity.md) (assessment) · [gaps.md](docs/gaps.md) (work
|
||||
list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.md](docs/parity-checklist.md)
|
||||
(presence checklist) · [typography-parity.md](docs/typography-parity.md) ·
|
||||
[file-format-parity.md](docs/file-format-parity.md) · [ui-parity.md](docs/ui-parity.md) ·
|
||||
[hardware-parity.md](docs/hardware-parity.md) · [localization-parity.md](docs/localization-parity.md) ·
|
||||
[architecture.md](docs/architecture.md) · agents: [agents.md](docs/agents.md), [mcp.md](docs/mcp.md),
|
||||
[control-protocol.md](docs/control-protocol.md) · CJK: [cjk.md](docs/cjk.md),
|
||||
[cjk-typography.md](docs/cjk-typography.md) · Arabic: [arabic-typography.md](docs/arabic-typography.md)
|
||||
|
||||
## Progress log
|
||||
|
||||
- **2026-10-13** · Switching between document and parent pages refreshes the canvas immediately, preventing black parent pages and stale document objects when the zoom stays the same. Ordinary edits retain partial repainting. No readiness percentage change.
|
||||
|
||||
Newest first.
|
||||
|
||||
- **2026-10-14** · Rulers measure from a Spread, Page or Spine origin plus a document zero point: drag it from the ruler corner, double-click to reset, right-click to lock, Cmd-drag for a horizontal and a vertical guide; saved with the document and as IDML `RulerOrigin` / `ZeroPoint`. No readiness percentage change.
|
||||
- **2026-10-13** · Object › Fitting has InDesign's shortcuts: Fill Frame Proportionally ⌘⌥⇧C, Fit Content Proportionally ⌘⌥⇧E, Fit Frame to Content ⌘⌥C, Fit Content to Frame ⌘⌥E, Center Content ⌘⇧E. No readiness percentage change.
|
||||
- **2026-10-13** · Gradients between process colours blend as inks, as InDesign shows them: magenta to cyan passes through C 50 M 50 (a light lilac) on screen instead of the darker RGB mix, and a midpoint between CMYK or grey stops no longer turns the PDF shading RGB. No readiness percentage change.
|
||||
- **2026-10-14** · The Swatches panel has InDesign's header: a Fill/Stroke proxy, Formatting Affects Container/Text and the Tint of the colour in front (a tint alone keeps each object's own swatch). Clicking a swatch applies it to the fill or stroke in front (of the text, when formatting affects text), and the bottom row has New Color Group, New Swatch and Delete Swatch. No readiness percentage change.
|
||||
- **2026-10-14** · With the Page tool, the Control panel shows the page as InDesign's does: W/H from a reference point, page-size preset (US Business Card added), orientation, Liquid Page Rule (now with Controlled by Parent) and Show Parent Page Overlay, which draws the parent page's size over a resized page. No readiness percentage change.
|
||||
- **2026-10-14** · On an object-based page the Page tool shows a selected object's liquid pins and resize locks as InDesign does: a click pins a side to the page edge or locks the width or height, with InDesign's tooltips. No readiness percentage change.
|
||||
- **2026-10-14** · The Liquid Layout panel follows InDesign's: Liquid Page Rule with the page number, Content › Auto-Fit, and Object Constraints › Resize with Page and Pin for the selected object on an object-based page; Create Alternate Layout moved to the Layout menu. No readiness percentage change.
|
||||
- **2026-10-14** · With the Page tool, the Control panel shows the page as InDesign's does: W/H from a reference point, page-size preset (US Business Card added), orientation, Liquid Page Rule (now with Controlled by Parent) and Show Parent Page Overlay, which draws the parent page's size over a resized page. No readiness percentage change.
|
||||
- **2026-10-14** · With the Page tool, the Control panel shows the page as InDesign's does: W/H from a reference point, page-size preset (US Business Card added), orientation, Liquid Page Rule and Show Parent Page Overlay, which draws the parent page's size over a resized page. No readiness percentage change.
|
||||
- **2026-10-14** · The Page tool works as InDesign's: a click selects a page and shows its handles and liquid page rule; dragging a handle previews the rule and springs back, Alt-drag resizes the page from the opposite edge, Shift keeps proportions, with a W/H readout. No readiness percentage change.
|
||||
- **2026-10-14** · With the Page tool, a guide dragged from a ruler is a liquid guide (dashed), as in InDesign; guide-based pages stretch the objects it crosses. No readiness percentage change.
|
||||
- **2026-10-14** · The Pages panel menu follows InDesign's order: Insert Pages…, Move Pages…, Duplicate Spread (and at End of Document), Delete Page/Spread, New Parent…, Apply Parent to Pages, Override All Parent Page Items, Parent Pages, Create Alternate Layout…, Numbering & Section Options…, Allow Selected Spread to Shuffle. No readiness percentage change.
|
||||
- **2026-10-14** · Pages panel: Alt-drag copies a page, a page dropped on Create new page is duplicated at the end, Edit page size offers the presets and Custom Page Size…; duplicated pages keep their own size, rule and guides; thumbnails of pages of their own size stay in their rows. No readiness percentage change.
|
||||
- **2026-10-12** · Text threads by hand: click a selected text frame's in or out port to load
|
||||
the text cursor, then click a frame to thread it, click empty space for a column-wide frame or
|
||||
drag one (it snaps like frame drawing); Shift-click autoflows, Alt-click stays loaded on the
|
||||
frame just added; click the linked frame or double-click a port to break the thread.
|
||||
`text.thread`, `text.unthread`, `text.removeFromThread` with undo; locked frames and layers
|
||||
refuse threads. The alpha gate now waits only on the Primary Text Frame page fixes (#165). No
|
||||
readiness percentage change.
|
||||
- **2026-10-13** · Layout ▸ Table of Contents opens a real dialog: title and title style, Include Paragraph Styles and Other Styles lists with Add and Remove, and per style its entry style, page number position (after, before, none), separator and level. Replace Existing regenerates the current table of contents in place; otherwise OK makes a new frame on the page in view. IDML export writes the definition as a TOC style that import reads back, so Update works after a round trip. No readiness percentage change.
|
||||
- **2026-10-13** · Control-channel clicks and drags validate positive counts and the complete pending-event budget before queuing input (#732). Oversized requests leave pending gestures intact, preventing unbounded click-count allocation. No readiness percentage change.
|
||||
- **2026-10-12** · macOS Dock Quit uses the same unsaved-document prompts as window and menu Quit. Repeated requests keep the current answers; Cancel retains every tab and unsaved recovery checkpoint, and completed Save/Don't Save answers exit through the ordinary event loop. No readiness percentage change.
|
||||
- **2026-10-12** · Orderly native exit writes the latest recovery state after finalizing current quit approvals; incomplete or stale plans keep their documents, and Undo to the saved state clears stale recovery. Unavailable or failed recovery is reported. Cancelable macOS Dock Quit remains open (#544). No readiness percentage change.
|
||||
- **2026-10-12** · Recovered documents keep a checkpoint until saved, closed or reverted, including a second crash before autosave. Restarted document IDs and explicit recovery folders preserve other snapshots; failed republishing retains the original. No readiness percentage change.
|
||||
- **2026-10-12** · Book numbering and style updates prepare every chapter before saving. Only closed native chapters are changed; imported IDML remains a read-only style/PDF source. If a later disk write fails, the error names the completed chapters; the batch is not a whole-book transaction. No readiness percentage change.
|
||||
- **2026-10-12** · Desktop write callbacks and CLI raster exports use the atomic writer too; open readers retain the original bytes, while private Unix modes and symlink targets survive replacement. Permanent tests cover both actual callers. No readiness percentage change.
|
||||
- **2026-10-12** · Save As, library edits and book collection edits persist prospective values before changing live state; failed writes preserve the document and existing collections. Save a Copy rebases data-source paths for its destination without changing the open document. No readiness percentage change.
|
||||
- **2026-10-12** · The Pen tool continues an open path from either end point, as in InDesign: the cursor shows it over an end point, a click there carries on drawing that path, a drag first reshapes the end point's direction lines, and the other end closes it. Object › Paths › Reverse Path (`path.reverse`); `path.setAnchor`; `path.appendAnchor` and `path.close` take a subpath. No readiness percentage change.
|
||||
- **2026-10-12** · Editing type on a path: the Type tool clicks, drags and double-clicks along the curve (the character under the pointer, not one on the straightened line), a click on the letters themselves hits the frame, and the caret and highlight bend with the path. No readiness percentage change.
|
||||
- **2026-10-12** · Type on a path has start and end brackets, as in InDesign: selected path type shows them across the path, and dragging either with the Selection or Direct Selection tool moves where the text starts or ends (text past the end bracket is overset). `type.pathOptions {end}`; IDML reads and writes EndBracket. No readiness percentage change.
|
||||
- **2026-10-12** · Window close and application/menu Quit ask about unsaved documents; later Cancel keeps every tab open, including earlier Don't Save choices. Approvals are rechecked after edits. Cancelable macOS Dock Quit remains open (#544). No readiness percentage change.
|
||||
- **2026-10-12** · Each running app keeps recovery data in its own locked folder; current folder and lock ownership are checked before writes and cleanup. Failed isolation disables recovery with a warning instead of sharing document IDs. No readiness percentage change.
|
||||
- **2026-10-12** · Native engine saves and exports replace files atomically; failed writes retain the original, private staging preserves Unix permissions, and valid long filenames work. Desktop Services and CLI raster exports remain separate follow-ups. No readiness percentage change.
|
||||
- **2026-10-12** · Hyphenation Zone applies to the Single-line Composer only, as in InDesign. With the default Paragraph Composer, ragged text hyphenates where InDesign does (aper-natem), giving the same line breaks. No readiness percentage change.
|
||||
- **2026-10-12** · The Tools panel's Fill/Stroke keys work: D (default fill and stroke), X (fill or stroke in front), Shift+X (swap per object, tints included) and / (Apply None), as `color.*` commands; swap and default are one undo step (`object.swapFillStroke`, `object.defaultFillStroke`); the Color panel follows the proxy's fill/stroke choice. No readiness percentage change.
|
||||
- **2026-10-11** · Corner Options keep curved sides curved and round every corner point, including where an arc meets a line (a Pathfinder quarter circle with rounded corners stayed a triangle). No readiness percentage change.
|
||||
- **2026-10-11** · `story.replaceRange`, `story.setText`, notes, footnotes, endnotes, type on a path and table cells read `\r` and `\r\n` as paragraph returns, as `frame.create` and `text.insert` already did. No readiness percentage change.
|
||||
- **2026-10-11** · The View menu follows InDesign's: proofing first, then zoom, Rotate Spread, Screen Mode, Display Performance, rulers, Extras, Grids & Guides and Structure; the view items that sat at the end of the Type menu moved there. No readiness percentage change.
|
||||
- **2026-10-12** · Underlines and strikethroughs are drawn as one bar per stretch of text on screen, as in PDF export: a bar per letter left faint seams between the letters. No readiness percentage change.
|
||||
- **2026-10-13** · Placed PDFs are drawn through pdfcraft's patched hayro crates: calculator functions whose `idiv` divides by zero or overflows, chains of stitching functions a hundred thousand deep, and mesh shadings with points far off the page, ten thousand overlapping triangles or forty thousand patches no longer panic, overflow the stack, run for over a minute or use more than 2 GiB of memory. A page with a `/UserUnit`, and its trim, bleed and art boxes, are sized in points. No readiness percentage change.
|
||||
- **2026-10-12** · Properties › Transform shows rotation, shear and X/Y scale, and Rotate 90° and Flip Horizontal/Vertical buttons, as InDesign's does. No readiness percentage change.
|
||||
- **2026-10-12** · Edit › Step and Repeat has its InDesign dialog: Repeat (Count, or Rows and Columns with Create as a grid), Offset (Vertical, Horizontal, in ruler units) and Preview, which shows the copies while the dialog is open; Cancel takes them away and OK makes them as one undo step. No readiness percentage change.
|
||||
- **2026-10-12** · Double-clicking the Fill or Stroke box in the Tools panel opens the Color Picker with the selection's colour, as in InDesign (it opened the Swatches panel). No readiness percentage change.
|
||||
- **2026-10-12** · The Effects panel shows the level its settings apply to ("Group: Color Dodge 100%") and, as in InDesign, Isolate Blending and Knockout Group for a selected group. No readiness percentage change.
|
||||
- **2026-10-11** · Properties gains InDesign's Pathfinder section (Add, Subtract, Intersect, Exclude Overlap, Minus Back) with several objects selected; section titles and icon buttons have accessible names. No readiness percentage change.
|
||||
- **2026-10-12** · The Eyedropper samples a placed image: clicking the photo with text (or objects) selected fills them with the image's colour under the pointer, as an unnamed colour (`color.sample`), as in InDesign. Images on locked layers can be sampled. No readiness percentage change.
|
||||
- **2026-10-12** · Words in capitals hyphenate like capitalized words (FERRUM COM-MODI), as in InDesign; Hyphenate Capitalized Words off keeps them whole. No readiness percentage change.
|
||||
- **2026-10-11** · `transform.move {copy}`, Edit › Duplicate and Step and Repeat return the copies' ids, so a script can go on with them. No readiness percentage change.
|
||||
- **2026-10-11** · `object.fill` and `object.stroke` refuse a swatch name the document doesn't have (it painted nothing) and suggest the name meant (`Paper` → `[Paper]`). No readiness percentage change.
|
||||
- **2026-10-11 (printing):** `file.printers` and `file.print` use craft-libs/craft-print for CUPS and Windows. DesignCraft retains PDF export, page selection, spreads, marks and bleed; dry runs create no temporary PDF or print job. Windows selects matching or default driver paper and shrinks oversized sheets without enlarging smaller ones; queue duplex defaults are preserved. Headless tests exercise exported bytes, job settings, dry runs and refusals; shared native Windows CI passes, including paused-queue submission and cleanup. PostScript, production print controls and physical output validation remain open. No readiness percentage change.
|
||||
|
||||
- **2026-10-10 (UI.4–UI.5):** Panel tabs can move into other groups, split the dock, float, resize and return to their saved locations through shared docking transactions. Layouts persist in custom workspaces; UI and control commands share the same validated action path. Shared pane geometry and widget-state theme application retain all five palettes and local fonts. Split panes send keyboard input only to the focused canvas and scope contextual toolbar IDs per pane. No readiness percentage change.
|
||||
- **2026-10-10** · Version 0.6.0; releases add a Linux riscv64 tar.gz (cross-compiled, CLI
|
||||
smoke-tested under QEMU).
|
||||
- **2026-10-10** · Readiness per audience: full ~47% (method aligned with the standard, was
|
||||
42%), mainstream practitioner ~42%, essentials user ~54%, each with hours to ~95%.
|
||||
- **2026-10-10** · Stage re-normalized to **pre-alpha** under craftrules' core-workflow gate: text
|
||||
can't be threaded by hand (no out-port click, no thread command).
|
||||
- **2026-10-10** · Full re-measure against InDesign 2026 21.6 (menu dump, bundle formats and
|
||||
localizations, 124 issues): breadth ~80%, ready ~42% (down from the ~83% "including depth" of
|
||||
2026-10-04, on user evidence and the INDD gap); progress docs restructured to the craftrules
|
||||
standard (`docs/parity.md` → `docs/parity-checklist.md`; new target-app-parity, gaps, roadmap,
|
||||
architecture, typography, file-format, UI, hardware and localization docs). Landed today:
|
||||
split columns (#295), PDF export options dialog (#196), column rules (#274), Paragraph Rules
|
||||
dialog (#263), Character Style Options (#144), PDF/X-4 RGB handling (#223), Spanish
|
||||
hyphenation (#222), Japanese (#272), Italian (#218) and Ukrainian (#236) interfaces, relocated packages relink (#244), graphics backend chosen
|
||||
before the window (#297), IDML table styles (#288), warichu (#280), list numbering from IDML
|
||||
(#298), security hardening (#153, #155), many text-composition fixes (#282–#293).
|
||||
- **2026-10-09** · Version 0.5.0; many user reports triaged into issues (#160–#265).
|
||||
- **2026-10-08** · Version 0.4.0.
|
||||
- **2026-10-07** · Community PRs: data merge, Brazilian Portuguese, PDF/X-4 flags, Office fonts
|
||||
on macOS; CJK 1.x (Korean line breaking, fallback fonts in Package); PrintCraft renamed PdfCraft.
|
||||
- **2026-10-06** · Snapping to parent items, Windows packaging, ARM64 workflow, control-channel
|
||||
hardening.
|
||||
- **2026-10-05** · FreeBSD build; caret-boundary fixes.
|
||||
- **2026-10-04** · Never-crash pass across importers and the engine; rotate spread view; kashida
|
||||
in styles; previous estimate: breadth 99%, "~83% including depth", ~170 h remaining.
|
||||
- **2026-10-03** · Release 0.1.1; separations, XML tagging, track changes, PDF/X-4 output
|
||||
intent, split window, buttons, menu customization.
|
||||
- **2026-10-02** · Hyperlinks/Bookmarks panels, conditional text, tagged PDF, lists across
|
||||
stories, style groups, Glyphs panel, High Contrast theme.
|
||||
- **2026-10-01** · First commit; skeleton, IDML, PDF, tables, footnotes, cross-references, index,
|
||||
spelling and parallel composition on the first day.
|
||||
|
||||
## Revision history
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-10-12 | minor | Manual text threading landed; alpha distance 15–25 h → 5–7 h (Primary Text Frame page fixes, #165) |
|
||||
| 2026-10-10 | minor | Full number 42% → 47% (method aligned with the standard: plain weighted sum, no new evidence); readiness table with hours per audience |
|
||||
| 2026-10-10 | minor | Headline adds mainstream practitioner (~42%) and essentials user (~54%); method in docs/target-app-parity.md |
|
||||
| 2026-10-10 | minor | Stage alpha → pre-alpha under the new core-workflow gate: manual text threading is missing; alpha gate table added to docs/roadmap.md |
|
||||
| 2026-10-10 | major | Restructured to the progress-docs standard; full re-measure; the "Working today" inventory moved to docs/target-app-parity.md, milestones to docs/roadmap.md, the CLI/MCP section left to docs/agents.md (which already covered it) |
|
||||
| 2026-10-04 | major | Parity estimate: breadth 99%, depth ~83%, ~170 h |
|
||||
| 2026-10-01 | major | First roadmap: status, milestones |
|
||||
|
||||
@@ -38,6 +38,37 @@ 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]\n designcraft-cli perf [--pages N] [--runs N] [--strict]\n designcraft-cli bench FILE [--runs N]\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]
|
||||
|
||||
Runs a command script from FILE (stdin when FILE is `-` or missing), headless or, with
|
||||
--connect PORT, in the running app (designcraft --control PORT).
|
||||
|
||||
Script text, one step per line:
|
||||
command.id {"param": 1} also `command.id={json}`, or `command.id` alone
|
||||
{"command": "command.id", "params": {...}} JSON lines, or a JSON array of them
|
||||
# comment, // comment skipped, like blank lines
|
||||
UTF-8 with or without a byte order mark; LF or CRLF line endings.
|
||||
|
||||
References to earlier results (in string parameters):
|
||||
"$N.path" the value at `path` in the result of step N. N is the 0-based step index:
|
||||
comments and blank lines are not steps. `$last` is the previous step.
|
||||
Paths go through result fields and array indices: "$1.story", "$3.ids.0".
|
||||
"${N.path}" inside a longer string, replaced by the value's text: "Frame ${0.id}".
|
||||
|
||||
Text parameters: "\n" starts a new paragraph, "\u2028" is a forced line break, "\t" a tab.
|
||||
Most commands ignore parameters they do not know instead of rejecting them: check names
|
||||
with `designcraft-cli describe COMMAND`.
|
||||
|
||||
Options:
|
||||
--in FILE / --sample open a document (.designcraft or .idml) or the sample first
|
||||
--save OUT save as .designcraft after the script succeeds
|
||||
--export OUT export after the script succeeds (.pdf, .idml, .epub, .png, .jpg); repeatable
|
||||
--keep-going run every step, recording failures as {"error": ...} results
|
||||
|
||||
Prints {"completed": n, "results": [...]}, plus failedIndex / failedCommand / error and a
|
||||
non-zero exit when a step fails."#;
|
||||
|
||||
/// The links line under the usage block.
|
||||
fn usage_footer() -> String {
|
||||
format!(
|
||||
@@ -281,7 +312,7 @@ fn export(s: &mut Session, out: &str, page: usize, scale: f64) -> Result<(), Str
|
||||
.render_page(&st.doc, &s.cache, page, scale, true, &designcraft_render::RenderOptions { printing_only: true, ..Default::default() })
|
||||
.ok_or("no such page")?;
|
||||
let bytes = if out.ends_with(".jpg") || out.ends_with(".jpeg") { img.to_jpeg(90) } else { img.to_png() };
|
||||
std::fs::write(out, bytes).map_err(|e| format!("{out}: {e}"))?;
|
||||
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(())
|
||||
}
|
||||
@@ -335,6 +366,10 @@ fn script(args: &[String]) -> Result<(), String> {
|
||||
"--save" => save = Some(val()?),
|
||||
"--export" => export.push(val()?),
|
||||
"--keep-going" => keep_going = true,
|
||||
"--help" | "-h" => {
|
||||
outln!("{SCRIPT_HELP}");
|
||||
return Ok(());
|
||||
}
|
||||
f if !f.starts_with("--") && file.is_none() => file = Some(f.to_string()),
|
||||
other => return Err(format!("unknown script option `{other}`")),
|
||||
}
|
||||
|
||||
@@ -87,6 +87,21 @@ fn help_goes_to_stdout_and_unknown_args_to_stderr() {
|
||||
assert!(String::from_utf8_lossy(&out.stderr).starts_with("usage: designcraft-cli"));
|
||||
}
|
||||
|
||||
/// `script --help` was "unknown script option `--help`"; the line syntax was only in
|
||||
/// `describe script.run` (#654).
|
||||
#[test]
|
||||
fn script_help_documents_the_syntax() {
|
||||
for flag in ["--help", "-h"] {
|
||||
let out = cli().args(["script", flag]).output().unwrap();
|
||||
let stdout = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(out.status.success(), "{flag}: {:?} {}", out.status, String::from_utf8_lossy(&out.stderr));
|
||||
assert!(stdout.starts_with("usage: designcraft-cli script"), "{flag}: {stdout:?}");
|
||||
for needle in ["0-based", "$1.story", "$3.ids.0", "\\u2028", "ignore"] {
|
||||
assert!(stdout.contains(needle), "{flag}: no {needle:?} in {stdout:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `designcraft-cli commands | head -1` panicked with "failed printing to
|
||||
/// stdout: Broken pipe (os error 32)" and exit status 101.
|
||||
#[test]
|
||||
@@ -122,3 +137,35 @@ fn run_export_options_come_before_their_export() {
|
||||
assert!(err.contains("wrote") && std::path::Path::new(a).exists() && std::path::Path::new(b).exists(), "{err}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn raster_export_preserves_an_existing_reader_and_private_symlink_target() {
|
||||
use std::io::Read;
|
||||
use std::os::unix::fs::{PermissionsExt, symlink};
|
||||
struct Fixture(std::path::PathBuf);
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
|
||||
let dir = std::env::temp_dir().join(format!("designcraft-cli-atomic-{}-{stamp}", std::process::id()));
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let fixture = Fixture(dir);
|
||||
let target = fixture.0.join(format!("{}.png", "a".repeat(243)));
|
||||
let old = b"previous private export";
|
||||
std::fs::write(&target, old).unwrap();
|
||||
std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
let alias = fixture.0.join("linked.png");
|
||||
symlink(target.file_name().unwrap(), &alias).unwrap();
|
||||
let mut reader = std::fs::File::open(&target).unwrap();
|
||||
let out = cli().args(["run", "--sample", "--scale", "0.1", "--export", alias.to_str().unwrap()]).output().unwrap();
|
||||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
let mut observed = Vec::new();
|
||||
reader.read_to_end(&mut observed).unwrap();
|
||||
assert!(observed == old, "CLI export changed bytes under an existing reader");
|
||||
assert!(std::fs::read(&target).unwrap().starts_with(b"\x89PNG\r\n\x1a\n"));
|
||||
assert_eq!(std::fs::metadata(&target).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
assert!(std::fs::symlink_metadata(alias).unwrap().file_type().is_symlink());
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ name = "designcraft-web"
|
||||
description = "DesignCraft in the browser (eframe web runner, WebGPU with WebGL2 fallback)"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
publish = false
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
name = "designcraft"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
publish = false
|
||||
default-run = "designcraft"
|
||||
@@ -24,10 +25,13 @@ winresource = "0.1"
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
rfd = { workspace = true }
|
||||
arboard = { workspace = true }
|
||||
|
||||
[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
|
||||
# The same winit eframe uses: to pick the X11 backend (file drag-and-drop) in main.rs.
|
||||
winit = { version = "0.30", default-features = false, features = ["x11"] }
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
# The same winit as eframe; only the native termination opt-in is used.
|
||||
winit = { version = "0.30", default-features = false }
|
||||
muda = { version = "0.21", default-features = false }
|
||||
|
||||
@@ -367,7 +367,9 @@ mod tests {
|
||||
// out of the set once it failed.
|
||||
write(&path, &record(Some(Backend::Gl), &[])).expect("write");
|
||||
let s = Startup::begin_unforced(Some(path.clone()), V).expect("candidates");
|
||||
if candidates().len() > 1 {
|
||||
// This synthetic prior start records OpenGL even on Metal-only macOS.
|
||||
// Switching depends on the chosen backend, not the candidate count.
|
||||
if s.backend != Backend::Gl {
|
||||
assert_eq!(s.switched_from, Some(Backend::Gl));
|
||||
let line = s.status_line().expect("status");
|
||||
assert!(line.contains("OpenGL") && line.contains(s.backend.name()) && line.contains("WGPU_BACKEND"), "{line}");
|
||||
|
||||
+202
-17
@@ -16,6 +16,7 @@ mod gpu;
|
||||
mod logging;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod native_menu;
|
||||
mod profile;
|
||||
|
||||
use designcraft_engine::Session;
|
||||
use designcraft_ui_egui::{DesignApp, Services};
|
||||
@@ -63,22 +64,31 @@ impl eframe::App for App {
|
||||
if let Some(s) = self.startup.as_mut() {
|
||||
s.presented();
|
||||
}
|
||||
if let Err(e) = preserve_documents_on_exit(&mut self.app) {
|
||||
log::error!("Couldn't preserve documents on exit: {e}");
|
||||
eprintln!("designcraft: couldn't preserve documents on exit: {e}");
|
||||
}
|
||||
save_prefs(&self.app);
|
||||
}
|
||||
}
|
||||
|
||||
/// The per-user settings directory: `ui.json`, `prefs.json`, `gpu.json` and `logs/` live here.
|
||||
fn config_dir() -> Option<std::path::PathBuf> {
|
||||
if cfg!(target_os = "macos") {
|
||||
std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join("Library/Application Support/DesignCraft"))
|
||||
} else if cfg!(windows) {
|
||||
std::env::var_os("APPDATA").map(|a| std::path::PathBuf::from(a).join("DesignCraft"))
|
||||
} else {
|
||||
std::env::var_os("XDG_CONFIG_HOME")
|
||||
.map(std::path::PathBuf::from)
|
||||
.or_else(|| std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join(".config")))
|
||||
.map(|c| c.join("designcraft"))
|
||||
/// Orderly OS termination can bypass egui's cancelable close event. Finish only a complete,
|
||||
/// current quit plan; otherwise preserve every live document, including edits after the timer.
|
||||
fn preserve_documents_on_exit(app: &mut DesignApp) -> Result<(), String> {
|
||||
app.finish_confirmed_quit()?;
|
||||
if app.session.documents().is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
if app.session.recovery_dir.is_none() {
|
||||
return if app.session.documents().iter().any(|d| d.is_dirty()) {
|
||||
Err("Recovery is unavailable; unsaved documents could not be preserved on exit".into())
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
}
|
||||
// Also discard stale recovery for clean documents (for example, Undo back to the saved
|
||||
// state after the last timer tick). Never choose a fallback recovery path here.
|
||||
app.run("file.recovery.save", serde_json::json!({})).map(|_| ())
|
||||
}
|
||||
|
||||
/// `DESIGNCRAFT_NO_PREFS`: read and write nothing in the settings directory (tests, demos).
|
||||
@@ -87,7 +97,7 @@ fn no_prefs() -> bool {
|
||||
}
|
||||
|
||||
fn prefs_path() -> Option<std::path::PathBuf> {
|
||||
config_dir().map(|b| b.join("ui.json"))
|
||||
profile::config_dir().map(|b| b.join("ui.json"))
|
||||
}
|
||||
|
||||
fn load_prefs(app: &mut DesignApp) {
|
||||
@@ -96,7 +106,7 @@ fn load_prefs(app: &mut DesignApp) {
|
||||
}
|
||||
if let Some(p) = prefs_path()
|
||||
&& let Ok(bytes) = std::fs::read(&p)
|
||||
&& let Ok(ui) = serde_json::from_slice::<designcraft_ui_egui::UiState>(&bytes)
|
||||
&& let Ok(ui) = designcraft_ui_egui::UiState::from_preferences_json(&bytes)
|
||||
{
|
||||
app.ui = ui;
|
||||
}
|
||||
@@ -199,7 +209,8 @@ fn services() -> Services {
|
||||
})),
|
||||
pick_save: Some(Box::new(|name: &str| rfd::FileDialog::new().set_file_name(name).save_file().map(|p| p.to_string_lossy().to_string()))),
|
||||
read: Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string()))),
|
||||
write: Some(Box::new(|p: &str, b: &[u8]| std::fs::write(p, b).map_err(|e| e.to_string()))),
|
||||
write: Some(Box::new(|p: &str, b: &[u8]| designcraft_engine::atomic::write_atomic(std::path::Path::new(p), b).map_err(|e| e.to_string()))),
|
||||
clipboard_text: Some(Box::new(|| arboard::Clipboard::new().and_then(|mut c| c.get_text()).ok())),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@@ -249,7 +260,7 @@ fn main() -> eframe::Result {
|
||||
}
|
||||
// The log file lives under the settings directory; opened after the arguments, so `--version`
|
||||
// leaves no file behind. Records logged until now are written to it first.
|
||||
if let (Some(logger), Some(dir)) = (logger, config_dir()) {
|
||||
if let (Some(logger), Some(dir)) = (logger, profile::config_dir()) {
|
||||
match logger.attach_dir(&dir.join("logs")) {
|
||||
Ok(path) => log::info!("DesignCraft {}, log file {}", env!("CARGO_PKG_VERSION"), path.display()),
|
||||
// Standard error only by now (`attach_dir` gave up on the file); unlike `eprintln!`, never panics.
|
||||
@@ -273,7 +284,7 @@ fn main() -> eframe::Result {
|
||||
}
|
||||
// The graphics backend, decided before the window exists: a driver that faults takes the
|
||||
// process down before any Rust code can catch it (see `gpu`).
|
||||
let gpu = gpu::Startup::begin(if no_prefs() { None } else { config_dir().map(|d| d.join(gpu::FILE)) }, env!("CARGO_PKG_VERSION"));
|
||||
let gpu = gpu::Startup::begin(if no_prefs() { None } else { profile::config_dir().map(|d| d.join(gpu::FILE)) }, env!("CARGO_PKG_VERSION"));
|
||||
if let Some(s) = &gpu
|
||||
&& let eframe::egui_wgpu::WgpuSetup::CreateNew(create) = &mut options.wgpu_options.wgpu_setup
|
||||
{
|
||||
@@ -289,6 +300,15 @@ fn main() -> eframe::Result {
|
||||
b.with_x11();
|
||||
}));
|
||||
}
|
||||
// AppKit Dock Quit otherwise terminates before egui can cancel the close. Route it through
|
||||
// ordinary CloseRequested events, preserving the same transactional Save/Don't Save flow.
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
options.event_loop_builder = Some(Box::new(|builder| {
|
||||
use winit::platform::macos::EventLoopBuilderExtMacOS;
|
||||
builder.with_close_requested_on_terminate(true);
|
||||
}));
|
||||
}
|
||||
eframe::run_native(
|
||||
"DesignCraft",
|
||||
options,
|
||||
@@ -304,7 +324,7 @@ fn main() -> eframe::Result {
|
||||
}
|
||||
let mut session = Session::new();
|
||||
// Crash recovery: reopen what a previous run left unsaved, then keep it current.
|
||||
session.recovery_dir = designcraft_engine::recovery::default_dir();
|
||||
session.recovery_dir = profile::recovery_dir();
|
||||
let recovered = session.execute("file.recovery.open", &serde_json::json!({})).ok();
|
||||
let mut app = DesignApp::new(session, services());
|
||||
if let Some(line) = gpu.as_ref().and_then(gpu::Startup::status_line) {
|
||||
@@ -366,3 +386,168 @@ mod tests {
|
||||
assert!(documents.iter().any(|filter| filter.extensions.contains(&"designcraft")));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod atomic_writer_tests {
|
||||
#[test]
|
||||
fn desktop_write_preserves_an_existing_reader_and_private_symlink_target() {
|
||||
use std::io::Read;
|
||||
use std::os::unix::fs::{PermissionsExt, symlink};
|
||||
struct Fixture(std::path::PathBuf);
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
|
||||
let dir = std::env::temp_dir().join(format!("designcraft-desktop-atomic-{}-{stamp}", std::process::id()));
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let fixture = Fixture(dir);
|
||||
let target = fixture.0.join(format!("{}.png", "a".repeat(243)));
|
||||
let old = b"previous private export";
|
||||
std::fs::write(&target, old).unwrap();
|
||||
std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
let alias = fixture.0.join("linked.png");
|
||||
symlink(target.file_name().unwrap(), &alias).unwrap();
|
||||
let mut reader = std::fs::File::open(&target).unwrap();
|
||||
let mut write = super::services().write.unwrap();
|
||||
write(alias.to_str().unwrap(), b"complete new export").unwrap();
|
||||
let mut observed = Vec::new();
|
||||
reader.read_to_end(&mut observed).unwrap();
|
||||
assert!(observed == old, "desktop export changed bytes under an existing reader");
|
||||
assert_eq!(std::fs::read(&target).unwrap(), b"complete new export");
|
||||
assert_eq!(std::fs::metadata(&target).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
assert!(std::fs::symlink_metadata(alias).unwrap().file_type().is_symlink());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod exit_recovery_tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn dirty_app(label: &str) -> (DesignApp, std::path::PathBuf) {
|
||||
let mut app = DesignApp::new(Session::new(), Services::default());
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
app.run("frame.create", json!({"rect": [20, 20, 120, 120]})).unwrap();
|
||||
let uid = app.session.active().unwrap().uid;
|
||||
let dir = std::env::temp_dir().join(format!("designcraft-exit-{label}-{}-{uid}", std::process::id()));
|
||||
assert!(!dir.exists());
|
||||
app.session.recovery_dir = Some(dir.clone());
|
||||
(app, dir)
|
||||
}
|
||||
|
||||
fn close_frame(app: &mut DesignApp, ctx: &egui::Context, close: bool) {
|
||||
let mut raw = egui::RawInput::default();
|
||||
if close {
|
||||
raw.viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close);
|
||||
}
|
||||
let mut out = ctx.run_ui(raw, |ui| app.logic(ui.ctx()));
|
||||
out.textures_delta.clear();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exit_recovery_preserves_latest_dirty_document_without_mutating_it() {
|
||||
let (mut app, dir) = dirty_app("latest");
|
||||
app.run("file.recovery.save", json!({})).unwrap();
|
||||
app.run("frame.create", json!({"rect": [200, 200, 300, 300]})).unwrap();
|
||||
let d = app.session.active().unwrap();
|
||||
let (uid, revision, saved_revision, doc, selection, undo, redo) =
|
||||
(d.uid, d.revision, d.saved_revision, d.doc.clone(), d.selection.items.clone(), d.history.undo.len(), d.history.redo.len());
|
||||
preserve_documents_on_exit(&mut app).unwrap();
|
||||
let current = app.session.active().unwrap();
|
||||
assert_eq!(
|
||||
(current.uid, current.revision, current.saved_revision, ¤t.selection.items, current.history.undo.len(), current.history.redo.len()),
|
||||
(uid, revision, saved_revision, &selection, undo, redo)
|
||||
);
|
||||
assert!(std::sync::Arc::ptr_eq(¤t.doc, &doc));
|
||||
assert!(current.is_dirty());
|
||||
let recovered = designcraft_engine::cmd::from_bytes(&std::fs::read(dir.join(format!("{uid}.designcraft"))).unwrap()).unwrap();
|
||||
assert_eq!(serde_json::to_value(&recovered).unwrap(), serde_json::to_value(doc.as_ref()).unwrap());
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exit_recovery_removes_stale_data_after_undo_to_saved_state() {
|
||||
let (mut app, dir) = dirty_app("clean");
|
||||
let uid = app.session.active().unwrap().uid;
|
||||
app.run("file.recovery.save", json!({})).unwrap();
|
||||
let entry = dir.join(format!("{uid}.designcraft"));
|
||||
assert!(entry.exists());
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
assert!(!app.session.active().unwrap().is_dirty());
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
preserve_documents_on_exit(&mut app).unwrap();
|
||||
assert!(!entry.exists(), "clean exit must not resurrect the pre-Undo edits");
|
||||
assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &doc));
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exit_recovery_reports_unavailable_or_failed_storage_without_changing_documents() {
|
||||
let (mut app, dir) = dirty_app("failure");
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
app.session.recovery_dir = None;
|
||||
assert!(preserve_documents_on_exit(&mut app).unwrap_err().contains("unavailable"));
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let blocked = dir.join("not-a-directory");
|
||||
std::fs::write(&blocked, b"preserve this file").unwrap();
|
||||
app.session.recovery_dir = Some(blocked.clone());
|
||||
assert!(preserve_documents_on_exit(&mut app).is_err());
|
||||
assert_eq!(std::fs::read(&blocked).unwrap(), b"preserve this file");
|
||||
assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &doc));
|
||||
assert!(app.session.active().unwrap().is_dirty());
|
||||
assert_eq!(app.session.documents().len(), 1);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exit_recovery_respects_complete_incomplete_stale_and_new_document_quit_plans() {
|
||||
for case in ["complete", "incomplete", "stale", "new"] {
|
||||
let (mut app, dir) = dirty_app(case);
|
||||
if case == "incomplete" {
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
app.run("frame.create", json!({"rect": [20, 20, 120, 120]})).unwrap();
|
||||
}
|
||||
app.run("file.recovery.save", json!({})).unwrap();
|
||||
let first_uid = app.session.documents()[0].uid;
|
||||
let ctx = egui::Context::default();
|
||||
close_frame(&mut app, &ctx, true);
|
||||
app.ui.dialog.as_mut().unwrap().fields.insert("discard".into(), json!(true));
|
||||
designcraft_ui_egui::dialogs::confirm(&mut app).unwrap();
|
||||
close_frame(&mut app, &ctx, false);
|
||||
if case == "stale" {
|
||||
app.run("frame.create", json!({"rect": [200, 200, 300, 300]})).unwrap();
|
||||
} else if case == "new" {
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
app.run("frame.create", json!({"rect": [20, 20, 120, 120]})).unwrap();
|
||||
}
|
||||
let active = app.session.active().unwrap().uid;
|
||||
let before: Vec<_> = app
|
||||
.session
|
||||
.documents()
|
||||
.iter()
|
||||
.map(|d| (d.uid, d.revision, d.doc.clone(), d.selection.items.clone(), d.history.undo.len(), d.history.redo.len()))
|
||||
.collect();
|
||||
preserve_documents_on_exit(&mut app).unwrap();
|
||||
if case == "complete" {
|
||||
assert!(app.session.documents().is_empty());
|
||||
assert!(!dir.join(format!("{first_uid}.designcraft")).exists(), "accepted Don't Save must not be recovered");
|
||||
} else {
|
||||
assert_eq!(app.session.documents().len(), before.len(), "{case}");
|
||||
assert_eq!(app.session.active().unwrap().uid, active, "{case}");
|
||||
for (d, (uid, revision, doc, selection, undo, redo)) in app.session.documents().iter().zip(before) {
|
||||
assert_eq!(
|
||||
(d.uid, d.revision, &d.selection.items, d.history.undo.len(), d.history.redo.len()),
|
||||
(uid, revision, &selection, undo, redo),
|
||||
"{case}"
|
||||
);
|
||||
assert!(std::sync::Arc::ptr_eq(&d.doc, &doc), "{case}");
|
||||
let recovered = designcraft_engine::cmd::from_bytes(&std::fs::read(dir.join(format!("{uid}.designcraft"))).unwrap()).unwrap();
|
||||
assert_eq!(serde_json::to_value(&recovered).unwrap(), serde_json::to_value(doc.as_ref()).unwrap(), "{case}");
|
||||
}
|
||||
}
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ impl NativeMenu {
|
||||
// Our own About splash (with the community links) instead of the stock panel.
|
||||
let about = MenuItem::with_id("dc-about", "About DesignCraft", true, None);
|
||||
let discord = MenuItem::with_id("dc-discord", "Join the ArtCraft Discord…", true, None);
|
||||
// Not the predefined Quit: it terminates the app at once, past the unsaved-changes prompt.
|
||||
let quit = MenuItem::with_id("dc-quit", designcraft_ui_egui::i18n::tr(&app.ui.language, "Quit DesignCraft"), true, accel("Cmd+Q"));
|
||||
let _ = app_menu.append_items(&[
|
||||
&about,
|
||||
&discord,
|
||||
@@ -50,10 +52,11 @@ impl NativeMenu {
|
||||
&PredefinedMenuItem::hide_others(None),
|
||||
&PredefinedMenuItem::show_all(None),
|
||||
&PredefinedMenuItem::separator(),
|
||||
&PredefinedMenuItem::quit(None),
|
||||
&quit,
|
||||
]);
|
||||
items.insert("dc-about".into(), ("help.about".into(), serde_json::Value::Null, Handle::Plain(about)));
|
||||
items.insert("dc-discord".into(), ("help.discord".into(), serde_json::Value::Null, Handle::Plain(discord)));
|
||||
items.insert("dc-quit".into(), ("app.quit".into(), serde_json::Value::Null, Handle::Plain(quit)));
|
||||
let _ = menu.append(&app_menu);
|
||||
for (title, entries) in menus::menu_tree() {
|
||||
let sub = Submenu::new(designcraft_ui_egui::i18n::tr(&app.ui.language, title), true);
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
//! Native settings and recovery paths, including isolated automation profiles.
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn config_dir() -> Option<PathBuf> {
|
||||
config_dir_with(std::env::consts::OS, |key| std::env::var_os(key))
|
||||
}
|
||||
|
||||
fn config_dir_with(os: &str, env: impl Fn(&str) -> Option<OsString>) -> Option<PathBuf> {
|
||||
if let Some(root) = env("DESIGNCRAFT_CONFIG_DIR") {
|
||||
// An explicitly empty override must not fall back to the user's profile.
|
||||
return (!root.is_empty()).then(|| PathBuf::from(root));
|
||||
}
|
||||
match os {
|
||||
"macos" => env("HOME").map(|p| PathBuf::from(p).join("Library/Application Support/DesignCraft")),
|
||||
"windows" => env("APPDATA").map(|p| PathBuf::from(p).join("DesignCraft")),
|
||||
_ => {
|
||||
env("XDG_CONFIG_HOME").map(PathBuf::from).or_else(|| env("HOME").map(|p| PathBuf::from(p).join(".config"))).map(|p| p.join("designcraft"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recovery_dir() -> Option<PathBuf> {
|
||||
recovery_dir_with(std::env::var_os("DESIGNCRAFT_CONFIG_DIR"), designcraft_engine::recovery::default_dir).and_then(own_run)
|
||||
}
|
||||
|
||||
/// This run's folder in the recovery folder `root`. A second running DesignCraft has its own, so
|
||||
/// neither writes over the other's documents nor takes them for a crash's.
|
||||
fn own_run(root: PathBuf) -> Option<PathBuf> {
|
||||
match designcraft_engine::recovery::run_dir(&root) {
|
||||
Ok(dir) => Some(dir),
|
||||
Err(e) => {
|
||||
log::warn!("recovery is unavailable because this run cannot be isolated: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn recovery_dir_with(root: Option<OsString>, default: impl FnOnce() -> Option<PathBuf>) -> Option<PathBuf> {
|
||||
match root {
|
||||
Some(root) => (!root.is_empty()).then(|| PathBuf::from(root).join("Recovery")),
|
||||
None => default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn environment(key: &str) -> Option<OsString> {
|
||||
match key {
|
||||
"HOME" => Some("/user".into()),
|
||||
"APPDATA" => Some("/roaming".into()),
|
||||
"XDG_CONFIG_HOME" => Some("/xdg-config".into()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn isolated_profile_overrides_every_platform_and_recovery() {
|
||||
for os in ["macos", "windows", "linux"] {
|
||||
let dir = config_dir_with(os, |key| if key == "DESIGNCRAFT_CONFIG_DIR" { Some("/isolated".into()) } else { environment(key) });
|
||||
assert_eq!(dir, Some(PathBuf::from("/isolated")));
|
||||
}
|
||||
assert_eq!(recovery_dir_with(Some("/isolated".into()), || Some("/user/recovery".into())), Some(PathBuf::from("/isolated/Recovery")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_override_never_reads_or_writes_the_default_profile() {
|
||||
assert_eq!(config_dir_with("macos", |key| { if key == "DESIGNCRAFT_CONFIG_DIR" { Some(OsString::new()) } else { environment(key) } }), None);
|
||||
assert_eq!(recovery_dir_with(Some(OsString::new()), || Some("/user/recovery".into())), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unset_override_preserves_existing_platform_paths() {
|
||||
assert_eq!(config_dir_with("macos", environment), Some("/user/Library/Application Support/DesignCraft".into()));
|
||||
assert_eq!(config_dir_with("windows", environment), Some("/roaming/DesignCraft".into()));
|
||||
assert_eq!(config_dir_with("linux", environment), Some("/xdg-config/designcraft".into()));
|
||||
assert_eq!(config_dir_with("linux", |key| if key == "HOME" { environment(key) } else { None }), Some("/user/.config/designcraft".into()));
|
||||
assert_eq!(recovery_dir_with(None, || Some("/existing/recovery".into())), Some("/existing/recovery".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_run_has_its_own_recovery_folder_when_one_can_be_made() {
|
||||
let dir = std::env::temp_dir().join(format!("designcraft-profile-run-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let root = dir.join("Recovery");
|
||||
assert_eq!(own_run(root.clone()).as_deref().and_then(|dir| dir.parent()), Some(root.as_path()));
|
||||
// Failure must not fall back to the shared root.
|
||||
let blocked = dir.join("blocked");
|
||||
std::fs::write(&blocked, "a file where the folder should be").unwrap();
|
||||
assert_eq!(own_run(blocked.join("Recovery")), None, "isolation failure must not select the shared root");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_exhausted_run_namespace_never_falls_back_to_shared_recovery() {
|
||||
let root = std::env::temp_dir().join(format!("designcraft-profile-exhausted-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
for n in 0..64 {
|
||||
std::fs::create_dir(root.join(format!("run-{}-{n}", std::process::id()))).unwrap();
|
||||
}
|
||||
assert_eq!(own_run(root.clone()), None, "a shared root would restore inter-process UID collisions");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
}
|
||||
@@ -103,10 +103,33 @@ impl Gradient {
|
||||
}
|
||||
out
|
||||
}
|
||||
/// Stops for a renderer that blends between them in RGB: the expanded stops, plus samples
|
||||
/// through each span that isn't RGB at both ends, so process colours keep blending as inks.
|
||||
pub fn display_stops(&self) -> Vec<(f32, Color, f32)> {
|
||||
const STEPS: usize = 16;
|
||||
let stops = self.expanded_stops();
|
||||
let mut out: Vec<(f32, Color, f32)> = Vec::with_capacity(stops.len() * STEPS);
|
||||
for (i, s) in stops.iter().enumerate() {
|
||||
out.push(*s);
|
||||
let Some(n) = stops.get(i + 1) else { continue };
|
||||
if matches!((s.1, n.1), (Color::Rgb { .. }, Color::Rgb { .. })) || n.0 - s.0 <= 1e-4 {
|
||||
continue;
|
||||
}
|
||||
for j in 1..STEPS {
|
||||
let t = s.0 + (n.0 - s.0) * j as f32 / STEPS as f32;
|
||||
let (c, a) = self.sample(t);
|
||||
out.push((t, c, a));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
pub fn reverse(&mut self) {
|
||||
// A midpoint belongs to the segment after its stop, so it moves with that segment and mirrors.
|
||||
let mids: Vec<_> = self.stops.iter().rev().skip(1).map(|s| 1.0 - s.midpoint).collect();
|
||||
self.stops.reverse();
|
||||
for s in &mut self.stops {
|
||||
for (s, m) in self.stops.iter_mut().zip(mids.into_iter().chain([0.5])) {
|
||||
s.offset = 1.0 - s.offset;
|
||||
s.midpoint = m;
|
||||
}
|
||||
}
|
||||
pub fn sort(&mut self) {
|
||||
@@ -196,6 +219,25 @@ mod tests {
|
||||
assert!((mid - 0.5).abs() < 1e-5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn process_stops_blend_as_inks() {
|
||||
// LYH Studio episode 6: magenta to cyan is C 50 M 50 halfway (a light lilac), not the RGB mix.
|
||||
let mut g = Gradient::default();
|
||||
g.stops[0].color = Color::cmyk(0.0, 1.0, 0.0, 0.0);
|
||||
g.stops[1].color = Color::cmyk(1.0, 0.0, 0.0, 0.0);
|
||||
assert_eq!(g.sample(0.5).0, Color::cmyk(0.5, 0.5, 0.0, 0.0));
|
||||
let d = g.display_stops();
|
||||
assert_eq!(d.len(), 17, "sampled through the span");
|
||||
assert!(d.iter().any(|(o, c, _)| (*o - 0.5).abs() < 1e-6 && *c == Color::cmyk(0.5, 0.5, 0.0, 0.0)));
|
||||
// A midpoint stays an ink too; RGB stops aren't sampled.
|
||||
g.stops[0].midpoint = 0.25;
|
||||
assert!(matches!(g.expanded_stops()[1].1, Color::Cmyk { .. }));
|
||||
assert_eq!(Gradient::default().display_stops().len(), 2);
|
||||
// Grey and CMYK meet as inks; RGB with anything else blends in RGB.
|
||||
assert_eq!(Color::gray(1.0).lerp(&Color::cmyk(1.0, 0.0, 0.0, 0.0), 0.5), Color::cmyk(0.5, 0.0, 0.0, 0.5));
|
||||
assert!(matches!(Color::rgb(1.0, 0.0, 0.0).lerp(&Color::cmyk(1.0, 0.0, 0.0, 0.0), 0.5), Color::Rgb { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn midpoint_shifts() {
|
||||
let mut g = Gradient::default();
|
||||
@@ -219,8 +261,24 @@ mod tests {
|
||||
#[test]
|
||||
fn reverse_stops() {
|
||||
let mut g = Gradient::default();
|
||||
g.stops[0].midpoint = 0.25;
|
||||
g.reverse();
|
||||
assert_eq!(g.stops[0].color.to_hex(), "#000000");
|
||||
assert_eq!(g.stops[0].offset, 0.0);
|
||||
// The midpoint moves to its reversed segment and mirrors.
|
||||
assert!((g.sample(0.75).0.to_rgb()[0] - 0.5).abs() < 1e-5);
|
||||
assert_eq!(g.stops.iter().map(|s| s.midpoint).collect::<Vec<_>>(), [0.75, 0.5]);
|
||||
|
||||
// With several stops, the reversed gradient is the original mirrored.
|
||||
let mut g = Gradient::default();
|
||||
g.stops[0].midpoint = 0.3;
|
||||
g.stops.insert(1, GradientStop { offset: 0.4, color: Color::rgb(1.0, 0.0, 0.0), opacity: 0.5, midpoint: 0.8 });
|
||||
let original = g.clone();
|
||||
g.reverse();
|
||||
for t in [0.1, 0.3, 0.5, 0.7, 0.9] {
|
||||
let ((c, o), (want_c, want_o)) = (g.sample(t), original.sample(1.0 - t));
|
||||
assert!(c.to_rgb().iter().zip(want_c.to_rgb()).all(|(a, b)| (a - b).abs() < 1e-5), "colour at {t}");
|
||||
assert!((o - want_o).abs() < 1e-5, "opacity at {t}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-1
@@ -169,8 +169,17 @@ impl Color {
|
||||
let [r, g, b] = self.to_rgb();
|
||||
Color::rgb(1.0 - r, 1.0 - g, 1.0 - b)
|
||||
}
|
||||
/// Linear interpolation in display RGB.
|
||||
/// The colour `t` of the way to `other`. Process inks blend as inks (CMYK with CMYK or grey,
|
||||
/// grey with grey), as InDesign blends a gradient's stops; anything else blends in RGB.
|
||||
pub fn lerp(&self, other: &Color, t: f32) -> Color {
|
||||
let l = |a: f32, b: f32| a + (b - a) * t;
|
||||
match (*self, *other) {
|
||||
(Color::Cmyk { c, m, y, k }, Color::Cmyk { c: c2, m: m2, y: y2, k: k2 }) => return Color::cmyk(l(c, c2), l(m, m2), l(y, y2), l(k, k2)),
|
||||
(Color::Gray { k }, Color::Gray { k: k2 }) => return Color::gray(l(k, k2)),
|
||||
(Color::Gray { k }, Color::Cmyk { .. }) => return Color::cmyk(0.0, 0.0, 0.0, k).lerp(other, t),
|
||||
(Color::Cmyk { .. }, Color::Gray { k }) => return self.lerp(&Color::cmyk(0.0, 0.0, 0.0, k), t),
|
||||
_ => {}
|
||||
}
|
||||
let a = self.to_rgb();
|
||||
let b = other.to_rgb();
|
||||
Color::rgb(a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t)
|
||||
|
||||
@@ -10,6 +10,7 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
unicode-bidi = { workspace = true }
|
||||
unicode-segmentation = { workspace = true }
|
||||
unicode-bidi-mirroring = { workspace = true }
|
||||
designcraft-geom = { workspace = true }
|
||||
designcraft-color = { workspace = true }
|
||||
@@ -18,7 +19,7 @@ designcraft-fonts = { workspace = true }
|
||||
log = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
miniz_oxide = "0.8"
|
||||
hypher = { version = "0.1.8", default-features = false, features = ["alloc", "spanish"] }
|
||||
hypher = { version = "0.1.8", default-features = false, features = ["alloc", "portuguese", "spanish"] }
|
||||
|
||||
[dev-dependencies]
|
||||
designcraft-fonts = { workspace = true, features = ["testing"] }
|
||||
|
||||
+21
-11
@@ -13,7 +13,8 @@ use designcraft_doc::{Document, Item, Story, StoryId, Styles, WrapMode};
|
||||
use crate::vars::RunningIndex;
|
||||
use crate::{ComposeOptions, ComposedStory};
|
||||
|
||||
type Key = (StoryId, Option<String>);
|
||||
/// A story, composed for one absolute page (parent-page items) or for its own frames (`None`).
|
||||
type Key = (StoryId, Option<usize>);
|
||||
|
||||
struct Entry {
|
||||
sig: Vec<usize>,
|
||||
@@ -55,11 +56,23 @@ impl Cache {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// The composed story, from cache or freshly composed.
|
||||
pub fn get(&self, doc: &Document, sid: StoryId, page_name: Option<&str>) -> Arc<ComposedStory> {
|
||||
/// The composed story, from cache or freshly composed. `page` is the absolute page a
|
||||
/// parent-page item is shown on (its page number markers and variables are that page's), or
|
||||
/// `None` for a story's own frames. Pages are identified by index, not name: names repeat when
|
||||
/// sections restart their numbering.
|
||||
pub fn get(&self, doc: &Document, sid: StoryId, page: Option<usize>) -> Arc<ComposedStory> {
|
||||
let Some(story) = doc.stories.get(&sid) else { return Arc::new(ComposedStory { story: sid, ..Default::default() }) };
|
||||
let (mut sig, keep_items) = signature(doc, story);
|
||||
let key = (sid, page_name.map(str::to_string));
|
||||
let key = (sid, page);
|
||||
// The key holds the page's index, which a renumbering doesn't change: its name (section
|
||||
// start number, style, prefix) is part of the signature.
|
||||
let page_name = page.map(|p| doc.page_name(p));
|
||||
if let Some(name) = &page_name {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
name.hash(&mut h);
|
||||
sig.push(h.finish() as usize);
|
||||
}
|
||||
let with_vars = crate::vars::has_vars(story);
|
||||
let running = with_vars && crate::vars::has_running(doc, story);
|
||||
if with_vars {
|
||||
@@ -125,23 +138,20 @@ impl Cache {
|
||||
return e.out.clone();
|
||||
}
|
||||
}
|
||||
// Parent-page items are composed per page; variables need that page's index.
|
||||
let page = match page_name {
|
||||
Some(n) if with_vars => (0..doc.page_count()).find(|&i| doc.page_name(i) == n),
|
||||
_ => None,
|
||||
};
|
||||
// Parent-page items are composed per page: markers show its name, variables need its index.
|
||||
let page = page.filter(|_| with_vars);
|
||||
let running = running.then(|| self.running_index(doc));
|
||||
let out = Arc::new(crate::compose_story(
|
||||
doc,
|
||||
sid,
|
||||
&ComposeOptions { page_name: page_name.map(str::to_string), page, running, label: None, xrefs: with_xrefs.then(|| self.xref_index(doc)) },
|
||||
&ComposeOptions { page_name, page, running, label: None, xrefs: with_xrefs.then(|| self.xref_index(doc)) },
|
||||
));
|
||||
let mut g = self.map.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let stamp = g.1;
|
||||
if g.0.len() > 4096 {
|
||||
g.0.retain(|_, e| stamp - e.stamp < 64);
|
||||
}
|
||||
let replaced = g.0.insert(key.clone(), Entry { sig, _keep: (story.clone(), doc.styles.clone(), keep_items), out: out.clone(), stamp });
|
||||
let replaced = g.0.insert(key, Entry { sig, _keep: (story.clone(), doc.styles.clone(), keep_items), out: out.clone(), stamp });
|
||||
drop(g);
|
||||
if key.1.is_none()
|
||||
&& let Some(old) = replaced
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Hyphenation. English uses the bundled dictionary and patterns below. Other languages use the
|
||||
//! Liang patterns compiled into `hypher`, one cargo feature per language: Spanish is Javier Bezos's
|
||||
//! `hyph-es.tex` 5.0 from hyph-utf8, MIT (see NOTICE).
|
||||
//! `hyph-es.tex` 5.0 from hyph-utf8, MIT; Portuguese is `hyph-pt.tex` 1.4 from hyph-utf8 (Pedro J.
|
||||
//! de Rezende, J. Joao Dias Almeida, Leonardo Araujo, Aline Benevides), BSD-3-Clause (see NOTICE).
|
||||
//!
|
||||
//! English lookup order for each word:
|
||||
//! 1. the **dictionary** ([`Dictionary::en_us`]) — the public-domain Moby Hyphenator II word list
|
||||
@@ -232,9 +233,8 @@ pub fn hyphen_points_in(word: &str, lim: &Limits, lang: Lang) -> Vec<usize> {
|
||||
if letters < lim.min_word.max(2) {
|
||||
return false;
|
||||
}
|
||||
if letters > 1 && run.iter().filter(|c| c.is_alphabetic()).all(|c| c.is_uppercase()) {
|
||||
return false;
|
||||
}
|
||||
// Words in capitals (FERRUM COMMODI) hyphenate like capitalized words, as InDesign's: Hyphenate
|
||||
// Capitalized Words off keeps both, acronyms included, whole.
|
||||
if !lim.capitalized && run[0].is_uppercase() {
|
||||
return false;
|
||||
}
|
||||
@@ -332,7 +332,10 @@ mod tests {
|
||||
#[test]
|
||||
fn case_apostrophes_and_punctuation() {
|
||||
assert_eq!(h("Typography"), "Ty-pog-ra-phy");
|
||||
assert_eq!(h("TYPOGRAPHY"), "TYPOGRAPHY");
|
||||
assert_eq!(h("TYPOGRAPHY"), "TY-POG-RA-PHY");
|
||||
assert_eq!(h("COMMODI"), "COM-MO-DI");
|
||||
let whole = Limits { capitalized: false, ..Limits::default() };
|
||||
assert_eq!(hyphenate_word("TYPOGRAPHY", &whole), "TYPOGRAPHY", "Hyphenate Capitalized Words off");
|
||||
assert_eq!(h("typography,"), "ty-pog-ra-phy,");
|
||||
assert_eq!(h("“typography”"), "“ty-pog-ra-phy”");
|
||||
assert_eq!(h("typography's"), "ty-pog-ra-phy's");
|
||||
@@ -389,6 +392,25 @@ mod tests {
|
||||
assert!(Lang::for_language("French").is_none(), "only languages built in");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn portuguese_uses_the_hyph_pt_patterns() {
|
||||
let pt = Lang::for_language("Portuguese: Brazilian").unwrap();
|
||||
assert_eq!(pt, Lang::for_language("Portuguese").unwrap());
|
||||
assert_eq!(pt, Lang::for_language("pt_BR").unwrap());
|
||||
assert_ne!(pt, Lang::for_language("Spanish").unwrap());
|
||||
// hyph-pt's own minimum: three letters after the last break.
|
||||
let lim = Limits { before_last: 3, ..Limits::default() };
|
||||
let h = |w: &str| {
|
||||
let pts = hyphen_points_in(w, &lim, pt);
|
||||
w.chars().enumerate().map(|(i, c)| if pts.contains(&i) { format!("-{c}") } else { c.to_string() }).collect::<String>()
|
||||
};
|
||||
assert_eq!(h("matemática"), "ma-te-má-tica");
|
||||
assert_eq!(h("orientação"), "ori-en-ta-ção");
|
||||
assert_eq!(h("Universidade"), "Uni-ver-si-dade");
|
||||
assert_eq!(h("experiência"), "ex-pe-ri-ên-cia");
|
||||
assert_eq!(h("pós-graduação"), "pós-gra-du-a-ção", "each part of a compound");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn heuristic_fallback() {
|
||||
let c: Vec<char> = "happen".chars().collect();
|
||||
|
||||
+625
-100
File diff suppressed because it is too large
Load Diff
@@ -197,10 +197,15 @@ pub fn find_note(cs: &ComposedStory, id: u64) -> Option<(usize, &PlacedNote)> {
|
||||
|
||||
/// Footnote text hit at frame-inner point `p`: (footnote id, byte in the footnote's story).
|
||||
pub fn hit_note(cs: &ComposedStory, fi: usize, p: Point) -> Option<(u64, usize)> {
|
||||
hit_note_with(cs, fi, p, crate::hit)
|
||||
}
|
||||
|
||||
/// [`hit_note`] with the hit test for the note's text (see [`crate::hit_cell_with`]).
|
||||
pub fn hit_note_with(cs: &ComposedStory, fi: usize, p: Point, hit: fn(&ComposedStory, usize, Point) -> Option<usize>) -> Option<(u64, usize)> {
|
||||
let ft = cs.frames.get(fi)?;
|
||||
let n = ft.notes.iter().find(|n| n.rect.contains(p))?;
|
||||
let local = Point::new(p.x - n.origin.x, p.y - n.origin.y);
|
||||
Some((n.id, crate::hit(&n.text, 0, local).unwrap_or(0)))
|
||||
Some((n.id, hit(&n.text, 0, local).unwrap_or(0)))
|
||||
}
|
||||
|
||||
/// Caret geometry for byte `pos` of a footnote's story, in frame inner space:
|
||||
|
||||
@@ -22,12 +22,15 @@ fn is_event(u: &NestedUntil, c: char, prev: Option<char>, next: Option<char>) ->
|
||||
NestedUntil::ForcedLineBreak => c == designcraft_doc::FORCED_LINE_BREAK,
|
||||
NestedUntil::EmSpace => c == '\u{2003}',
|
||||
NestedUntil::EnSpace => c == '\u{2002}',
|
||||
// Counted by position, not by character (see `overlays`).
|
||||
NestedUntil::Dropcap => false,
|
||||
NestedUntil::Chars(s) => s.contains(c),
|
||||
}
|
||||
}
|
||||
|
||||
/// Character-style ranges for the paragraph `text[range]`, in application order (later wins).
|
||||
pub(crate) fn overlays(text: &str, range: Range<usize>, nested: &[NestedStyle], grep: &[GrepStyle]) -> Vec<(Range<usize>, String)> {
|
||||
/// The paragraph's drop cap ends at `drop_end` (`range.start` without one).
|
||||
pub(crate) fn overlays(text: &str, range: Range<usize>, nested: &[NestedStyle], grep: &[GrepStyle], drop_end: usize) -> Vec<(Range<usize>, String)> {
|
||||
let mut out = Vec::new();
|
||||
let para = &text[range.clone()];
|
||||
// The paragraph's own end mark isn't styled.
|
||||
@@ -37,17 +40,22 @@ pub(crate) fn overlays(text: &str, range: Range<usize>, nested: &[NestedStyle],
|
||||
if pos >= end {
|
||||
break;
|
||||
}
|
||||
let mut count = 0;
|
||||
let mut stop = end;
|
||||
let chars: Vec<(usize, char)> = text[pos..end].char_indices().map(|(i, c)| (pos + i, c)).collect();
|
||||
for (k, &(i, c)) in chars.iter().enumerate() {
|
||||
let prev = if k > 0 { Some(chars[k - 1].1) } else { None };
|
||||
let next = chars.get(k + 1).map(|x| x.1);
|
||||
if is_event(&ns.until, c, prev, next) {
|
||||
count += 1;
|
||||
if count >= ns.count.max(1) {
|
||||
stop = if ns.through { i + c.len_utf8() } else { i };
|
||||
break;
|
||||
if ns.until == NestedUntil::Dropcap {
|
||||
// Through the drop cap runs to its end; up to it (it starts the paragraph) is nothing.
|
||||
stop = if ns.through { drop_end.max(pos).min(end) } else { pos };
|
||||
} else {
|
||||
let mut count = 0;
|
||||
let chars: Vec<(usize, char)> = text[pos..end].char_indices().map(|(i, c)| (pos + i, c)).collect();
|
||||
for (k, &(i, c)) in chars.iter().enumerate() {
|
||||
let prev = if k > 0 { Some(chars[k - 1].1) } else { None };
|
||||
let next = chars.get(k + 1).map(|x| x.1);
|
||||
if is_event(&ns.until, c, prev, next) {
|
||||
count += 1;
|
||||
if count >= ns.count.max(1) {
|
||||
stop = if ns.through { i + c.len_utf8() } else { i };
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,14 +92,21 @@ mod tests {
|
||||
let text = "Q: what is 42? A drop of 7 words.\n".to_string();
|
||||
let n = |style: &str, through: bool, count: u32, until: NestedUntil| NestedStyle { style: style.into(), through, count, until };
|
||||
// Through the first ':' then up to the second word end.
|
||||
let o = overlays(&text, 0..text.len(), &[n("Lead", true, 1, NestedUntil::Chars(":".into())), n("Next", false, 2, NestedUntil::Words)], &[]);
|
||||
let o =
|
||||
overlays(&text, 0..text.len(), &[n("Lead", true, 1, NestedUntil::Chars(":".into())), n("Next", false, 2, NestedUntil::Words)], &[], 0);
|
||||
assert_eq!(&text[o[0].0.clone()], "Q:");
|
||||
assert_eq!(&text[o[1].0.clone()], " what is", "up to the end of the second word");
|
||||
// Digits by GREP.
|
||||
let o = overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "Num".into(), pattern: r"\d+".into() }]);
|
||||
let o = overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "Num".into(), pattern: r"\d+".into() }], 0);
|
||||
let got: Vec<&str> = o.iter().map(|(r, _)| &text[r.clone()]).collect();
|
||||
assert_eq!(got, ["42", "7"]);
|
||||
// Through the drop cap (its first 2 bytes here); up to it is nothing; without one, nothing.
|
||||
let d = |through: bool| n("Cap", through, 1, NestedUntil::Dropcap);
|
||||
let o = overlays(&text, 0..text.len(), &[d(true), n("Next", true, 1, NestedUntil::Words)], &[], 2);
|
||||
assert_eq!((&text[o[0].0.clone()], &text[o[1].0.clone()]), ("Q:", " what "));
|
||||
assert_eq!(&text[overlays(&text, 0..text.len(), &[d(false), n("Next", true, 1, NestedUntil::Words)], &[], 2)[0].0.clone()], "Q: ");
|
||||
assert!(overlays(&text, 0..text.len(), &[d(true)], &[], 0).is_empty());
|
||||
// A bad pattern is ignored.
|
||||
assert!(overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "X".into(), pattern: "(".into() }]).is_empty());
|
||||
assert!(overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "X".into(), pattern: "(".into() }], 0).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,14 @@ pub struct Glyph {
|
||||
/// Baseline shift (positive = up), including super/subscript.
|
||||
pub shift: f64,
|
||||
pub ascent: f64,
|
||||
/// The font's typographic ascender at the point size (vertical scale doesn't apply): the Ascent
|
||||
/// first baseline offset.
|
||||
pub typo_ascent: f64,
|
||||
pub descent: f64,
|
||||
/// The leading this character asks for (absolute, or auto = size × auto %).
|
||||
pub leading: f64,
|
||||
/// Cap height and x height, scaled like `ascent`: the Cap Height and x Height first baseline
|
||||
/// offsets.
|
||||
pub cap: f64,
|
||||
pub xh: f64,
|
||||
pub size: f64,
|
||||
@@ -64,6 +69,9 @@ pub struct Glyph {
|
||||
/// Upright in a vertical frame: shaped top to bottom (`adv` is its vertical advance) and hung
|
||||
/// from its vertical origin on the line's centre (see [`crate::PlacedGlyph::vertical_xf`]).
|
||||
pub upright: bool,
|
||||
/// Part of the bullet or number the paragraph's list adds: not the line's own text, so it
|
||||
/// doesn't raise the Ascent first baseline.
|
||||
pub list_label: bool,
|
||||
}
|
||||
|
||||
impl Glyph {
|
||||
@@ -185,7 +193,8 @@ impl StyleTable<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve and shape one paragraph.
|
||||
/// Resolve and shape one paragraph. Its drop cap ends at byte `drop.0` (`range.start` without
|
||||
/// one) and takes the character style `drop.1` ("" = none).
|
||||
pub(crate) fn shape_para(
|
||||
db: &ScopedFonts<'_>,
|
||||
styles: &Styles,
|
||||
@@ -199,13 +208,37 @@ pub(crate) fn shape_para(
|
||||
nested: &[designcraft_doc::NestedStyle],
|
||||
grep: &[designcraft_doc::GrepStyle],
|
||||
lines: &[(std::ops::Range<usize>, String)],
|
||||
drop: (usize, &str),
|
||||
) -> ShapedPara {
|
||||
let _ = pi;
|
||||
let mut glyphs = Vec::with_capacity(range.len());
|
||||
// Nested line styles lie under nested and GREP styles (later overlays win).
|
||||
// Nested line styles lie under nested and GREP styles, and the drop cap's style over them all
|
||||
// (later overlays win).
|
||||
let mut overlays: Vec<(std::ops::Range<usize>, String)> = lines.to_vec();
|
||||
// The drop cap's style is the nested styles' leading "through 1 Dropcap" entry (the form IDML
|
||||
// stores it in). Set (`[None]` included), it restyles that entry; a real one stands in for a
|
||||
// missing entry. Either way the nested styles after it start after the drop cap.
|
||||
let set = Some(drop.1).filter(|s| !s.is_empty());
|
||||
let cap_style = set.filter(|s| *s != designcraft_doc::NO_CHAR_STYLE);
|
||||
let with_cap: Vec<designcraft_doc::NestedStyle>;
|
||||
let nested = match set {
|
||||
Some(s) if nested.first().is_some_and(designcraft_doc::NestedStyle::is_drop_cap) => {
|
||||
with_cap = std::iter::once(designcraft_doc::NestedStyle::drop_cap(s)).chain(nested.iter().skip(1).cloned()).collect();
|
||||
&with_cap[..]
|
||||
}
|
||||
Some(s) if cap_style.is_some() && !nested.is_empty() => {
|
||||
with_cap = std::iter::once(designcraft_doc::NestedStyle::drop_cap(s)).chain(nested.iter().cloned()).collect();
|
||||
&with_cap[..]
|
||||
}
|
||||
_ => nested,
|
||||
};
|
||||
if !nested.is_empty() || !grep.is_empty() {
|
||||
overlays.extend(crate::overlay::overlays(&story.text, range.clone(), nested, grep));
|
||||
overlays.extend(crate::overlay::overlays(&story.text, range.clone(), nested, grep, drop.0));
|
||||
}
|
||||
if let Some(s) = cap_style
|
||||
&& drop.0 > range.start
|
||||
{
|
||||
overlays.push((range.start..drop.0, s.to_string()));
|
||||
}
|
||||
// Each run, cut where nested / GREP styles start and end.
|
||||
let mut segments: Vec<(usize, usize, Option<&str>, &designcraft_doc::CharFormat)> = Vec::new();
|
||||
@@ -273,7 +306,7 @@ pub(crate) fn shape_para(
|
||||
}
|
||||
let mut g = control_glyph(&face, &props, auto_leading, style, a + i, c);
|
||||
g.ch = HIDDEN;
|
||||
(g.ascent, g.descent, g.leading, g.cap, g.xh) = (0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
(g.ascent, g.typo_ascent, g.descent, g.leading, g.cap, g.xh) = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
glyphs.push(g);
|
||||
}
|
||||
continue;
|
||||
@@ -597,10 +630,18 @@ fn shape_run_raw(
|
||||
Some(y) => {
|
||||
g.adv = o.w;
|
||||
g.ascent = g.ascent.max(o.h + y);
|
||||
g.typo_ascent = g.typo_ascent.max(o.h + y);
|
||||
g.cap = g.cap.max(o.h + y);
|
||||
g.xh = g.xh.max(o.h + y);
|
||||
g.descent = g.descent.max(-y);
|
||||
}
|
||||
// Pushes its line down by its height and spacing.
|
||||
None => g.ascent += o.h + o.space,
|
||||
None => {
|
||||
g.ascent += o.h + o.space;
|
||||
g.typo_ascent += o.h + o.space;
|
||||
g.cap += o.h + o.space;
|
||||
g.xh += o.h + o.space;
|
||||
}
|
||||
}
|
||||
if auto || o.y_offset.is_none() {
|
||||
g.leading = g.leading.max(g.ascent + g.descent);
|
||||
@@ -771,6 +812,7 @@ fn control_glyph(face: &Arc<FontFace>, p: &CharProps, auto_leading: TypeEnv, sty
|
||||
sy: k * p.v_scale,
|
||||
shift,
|
||||
ascent,
|
||||
typo_ascent: face.typo_ascent * k,
|
||||
descent,
|
||||
leading,
|
||||
cap,
|
||||
@@ -796,6 +838,7 @@ fn control_glyph(face: &Arc<FontFace>, p: &CharProps, auto_leading: TypeEnv, sty
|
||||
shaping_rtl: false,
|
||||
lang: designcraft_doc::language_tag(&p.language),
|
||||
upright: false,
|
||||
list_label: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -889,6 +932,7 @@ fn shape_segment(
|
||||
sy: k * p.v_scale,
|
||||
shift,
|
||||
ascent,
|
||||
typo_ascent: face.typo_ascent * k,
|
||||
descent,
|
||||
leading,
|
||||
cap,
|
||||
@@ -918,6 +962,7 @@ fn shape_segment(
|
||||
shaping_rtl: sg.rtl,
|
||||
lang,
|
||||
upright: up,
|
||||
list_label: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,6 +442,7 @@ fn emit(
|
||||
spacing: 1.0,
|
||||
hj: 0,
|
||||
keep_violation: false,
|
||||
drop_cap: None,
|
||||
});
|
||||
ft.tables.push(TableFrag {
|
||||
table: table.id,
|
||||
@@ -602,6 +603,17 @@ fn fragment_strokes(
|
||||
|
||||
/// The cell under `p` (frame inner space) in frame `fi`: (table, row, col, story byte in the cell).
|
||||
pub fn hit_cell(cs: &ComposedStory, fi: usize, p: Point) -> Option<(u64, usize, usize, usize)> {
|
||||
hit_cell_with(cs, fi, p, crate::hit)
|
||||
}
|
||||
|
||||
/// [`hit_cell`] with the hit test for the cell's text: [`crate::hit`] (caret position) or
|
||||
/// [`crate::hit_char`] (character under the point).
|
||||
pub fn hit_cell_with(
|
||||
cs: &ComposedStory,
|
||||
fi: usize,
|
||||
p: Point,
|
||||
hit: fn(&ComposedStory, usize, Point) -> Option<usize>,
|
||||
) -> Option<(u64, usize, usize, usize)> {
|
||||
let ft = cs.frames.get(fi)?;
|
||||
for t in &ft.tables {
|
||||
if !t.rect.contains(p) {
|
||||
@@ -610,7 +622,7 @@ pub fn hit_cell(cs: &ComposedStory, fi: usize, p: Point) -> Option<(u64, usize,
|
||||
for c in &t.cells {
|
||||
if c.rect.contains(p) {
|
||||
let local = Point::new(p.x - c.origin.x, p.y - c.origin.y);
|
||||
let b = crate::hit(&c.text, 0, local).unwrap_or(0);
|
||||
let b = hit(&c.text, 0, local).unwrap_or(0);
|
||||
return Some((t.table, c.row, c.col, b));
|
||||
}
|
||||
}
|
||||
|
||||
+1295
-6
File diff suppressed because it is too large
Load Diff
@@ -19,15 +19,17 @@ pub fn has_xrefs(st: &Story) -> bool {
|
||||
}
|
||||
|
||||
impl XrefIndex {
|
||||
/// `get` composes a story (normally through the cache). Stories that themselves hold
|
||||
/// cross-references are composed without resolving them (their anchors' pages hardly move).
|
||||
/// `get` composes a story (normally through the cache). Stories that hold cross-references or
|
||||
/// running headers are composed without resolving them (their anchors' pages hardly move):
|
||||
/// through the cache they would build this index again, directly or by way of the running-header index.
|
||||
pub fn build(doc: &Document, get: &dyn Fn(StoryId) -> Arc<ComposedStory>) -> XrefIndex {
|
||||
let mut pages = HashMap::new();
|
||||
for (sid, st) in &doc.stories {
|
||||
if st.anchors.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let cs = if has_xrefs(st) { Arc::new(crate::compose_story(doc, *sid, &Default::default())) } else { get(*sid) };
|
||||
let indexed = has_xrefs(st) || crate::vars::has_running(doc, st);
|
||||
let cs = if indexed { Arc::new(crate::compose_story(doc, *sid, &Default::default())) } else { get(*sid) };
|
||||
for a in &st.anchors {
|
||||
let Some(pos) = st.anchor_pos(a.id) else { continue };
|
||||
if let Some((page, ..)) = crate::vars::place(doc, &cs, pos) {
|
||||
|
||||
@@ -0,0 +1,411 @@
|
||||
//! Placed files (assets): one asset per distinct file, the assets a document uses, and the
|
||||
//! compaction a save applies (duplicates merged, unused assets left out).
|
||||
//!
|
||||
//! An asset holds what is shared by every placement of a file (bytes, link, type, the PDF page
|
||||
//! shown); each placement's frame data (transform, crop, fitting) lives on its [`Graphic`].
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||
use std::hash::BuildHasher;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::story::Story;
|
||||
use crate::{Asset, AssetId, Content, Document, Item};
|
||||
|
||||
/// Nesting limit for groups, anchored objects, table cells and notes. A document nested deeper
|
||||
/// is treated as using every asset, so nothing it references is merged away or pruned.
|
||||
const MAX_DEPTH: usize = 64;
|
||||
|
||||
impl Asset {
|
||||
/// Whether `other` holds the same file: the same type, page, link and bytes. Files at
|
||||
/// different paths stay apart even with identical bytes, since each can be updated or
|
||||
/// relinked on its own. Assets without bytes (a link that couldn't be read) are the same only
|
||||
/// when they link to the same path.
|
||||
pub fn same_file(&self, other: &Asset) -> bool {
|
||||
if self.mime != other.mime || self.page != other.page {
|
||||
return false;
|
||||
}
|
||||
if self.link != other.link {
|
||||
return false;
|
||||
}
|
||||
match (self.data.is_empty(), other.data.is_empty()) {
|
||||
(false, false) => self.same_bytes(other),
|
||||
(true, true) => self.link.is_some(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn same_bytes(&self, other: &Asset) -> bool {
|
||||
Arc::ptr_eq(&self.data, &other.data) || (self.data.len() == other.data.len() && *self.data == *other.data)
|
||||
}
|
||||
}
|
||||
|
||||
/// The asset in `assets` holding the same file as `asset`.
|
||||
pub fn find_same_asset(assets: &BTreeMap<AssetId, Arc<Asset>>, asset: &Asset) -> Option<AssetId> {
|
||||
assets.values().find(|a| a.same_file(asset)).map(|a| a.id)
|
||||
}
|
||||
|
||||
/// Add `asset` to `assets` unless they already hold the same file; returns the id placements
|
||||
/// should reference. A new asset with the bytes of an existing one shares its buffer.
|
||||
pub fn insert_asset(assets: &mut BTreeMap<AssetId, Arc<Asset>>, mut asset: Asset) -> AssetId {
|
||||
if let Some(id) = find_same_asset(assets, &asset) {
|
||||
return id;
|
||||
}
|
||||
if !asset.data.is_empty()
|
||||
&& let Some(a) = assets.values().find(|a| a.same_bytes(&asset))
|
||||
{
|
||||
asset.data = Arc::clone(&a.data);
|
||||
}
|
||||
let id = asset.id;
|
||||
assets.insert(id, Arc::new(asset));
|
||||
id
|
||||
}
|
||||
|
||||
/// Calls `f` with every asset reference in `it` and its children; false when nesting exceeded
|
||||
/// [`MAX_DEPTH`] (some references weren't visited).
|
||||
fn item_refs(it: &Item, f: &mut impl FnMut(AssetId), depth: usize) -> bool {
|
||||
if depth > MAX_DEPTH {
|
||||
return false;
|
||||
}
|
||||
if let Content::Graphic(g) = &it.content {
|
||||
f(g.asset);
|
||||
}
|
||||
if let Some(p) = it.media.as_ref().and_then(|m| m.poster) {
|
||||
f(p);
|
||||
}
|
||||
it.children().iter().all(|c| item_refs(c, f, depth + 1))
|
||||
}
|
||||
|
||||
/// Asset references in a story's anchored objects, graphic cells and notes (recursively).
|
||||
fn story_refs(st: &Story, f: &mut impl FnMut(AssetId), depth: usize) -> bool {
|
||||
if depth > MAX_DEPTH {
|
||||
return false;
|
||||
}
|
||||
let mut complete = st.objects.iter().all(|o| item_refs(&o.item, f, depth + 1));
|
||||
for t in st.tables.values() {
|
||||
for c in &t.cells {
|
||||
if let Some(g) = &c.graphic {
|
||||
f(g.asset);
|
||||
}
|
||||
complete &= story_refs(&c.text, f, depth + 1);
|
||||
}
|
||||
}
|
||||
for n in st.notes.iter().chain(&st.endnotes) {
|
||||
complete &= story_refs(&n.text, f, depth + 1);
|
||||
}
|
||||
complete
|
||||
}
|
||||
|
||||
/// Every asset reference in a story (its anchored objects, graphic cells and notes).
|
||||
pub fn story_asset_refs(st: &Story) -> Vec<AssetId> {
|
||||
let mut out = Vec::new();
|
||||
story_refs(st, &mut |a| out.push(a), 0);
|
||||
out
|
||||
}
|
||||
|
||||
/// Point a story's references to a key of `map` at its value.
|
||||
pub fn remap_story_assets(st: &mut Story, map: &HashMap<AssetId, AssetId>) {
|
||||
if !map.is_empty() {
|
||||
remap_story(st, map, 0);
|
||||
}
|
||||
}
|
||||
|
||||
fn item_uses(it: &Item, map: &HashMap<AssetId, AssetId>) -> bool {
|
||||
let mut hit = false;
|
||||
item_refs(it, &mut |a| hit |= map.contains_key(&a), 0);
|
||||
hit
|
||||
}
|
||||
|
||||
fn story_uses(st: &Story, map: &HashMap<AssetId, AssetId>) -> bool {
|
||||
let mut hit = false;
|
||||
story_refs(st, &mut |a| hit |= map.contains_key(&a), 0);
|
||||
hit
|
||||
}
|
||||
|
||||
fn remap_id(id: &mut AssetId, map: &HashMap<AssetId, AssetId>) {
|
||||
if let Some(n) = map.get(id) {
|
||||
*id = *n;
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrites the references in `it`, copying only the shared items that change.
|
||||
fn remap_item(it: &mut Arc<Item>, map: &HashMap<AssetId, AssetId>, depth: usize) {
|
||||
if depth > MAX_DEPTH || !item_uses(it, map) {
|
||||
return;
|
||||
}
|
||||
remap_item_in(Arc::make_mut(it), map, depth);
|
||||
}
|
||||
|
||||
fn remap_item_in(it: &mut Item, map: &HashMap<AssetId, AssetId>, depth: usize) {
|
||||
if let Content::Graphic(g) = &mut it.content {
|
||||
remap_id(&mut g.asset, map);
|
||||
}
|
||||
if let Some(p) = it.media.as_mut().and_then(|m| m.poster.as_mut()) {
|
||||
remap_id(p, map);
|
||||
}
|
||||
if let Some(kids) = it.children_mut() {
|
||||
for k in kids {
|
||||
remap_item(k, map, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remap_story(st: &mut Story, map: &HashMap<AssetId, AssetId>, depth: usize) {
|
||||
if depth > MAX_DEPTH {
|
||||
return;
|
||||
}
|
||||
for o in &mut st.objects {
|
||||
if item_uses(&o.item, map) {
|
||||
remap_item_in(&mut Arc::make_mut(o).item, map, depth + 1);
|
||||
}
|
||||
}
|
||||
for t in st.tables.values_mut() {
|
||||
if !t.cells.iter().any(|c| c.graphic.as_ref().is_some_and(|g| map.contains_key(&g.asset)) || story_uses(&c.text, map)) {
|
||||
continue;
|
||||
}
|
||||
for c in &mut Arc::make_mut(t).cells {
|
||||
if let Some(g) = &mut c.graphic {
|
||||
remap_id(&mut g.asset, map);
|
||||
}
|
||||
remap_story(&mut c.text, map, depth + 1);
|
||||
}
|
||||
}
|
||||
for n in st.notes.iter_mut().chain(st.endnotes.iter_mut()) {
|
||||
if story_uses(&n.text, map) {
|
||||
remap_story(&mut Arc::make_mut(n).text, map, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Document {
|
||||
/// Add a placed file, reusing the asset of an identical file already in the document;
|
||||
/// returns the id to reference.
|
||||
pub fn add_asset(&mut self, asset: Asset) -> AssetId {
|
||||
insert_asset(&mut self.assets, asset)
|
||||
}
|
||||
|
||||
/// Copy asset `id` of `src` into this document (or reuse the identical asset it already
|
||||
/// has); returns the id to reference here, `None` when `src` has no such asset.
|
||||
pub fn adopt_asset(&mut self, src: &Document, id: AssetId) -> Option<AssetId> {
|
||||
let a = src.assets.get(&id)?;
|
||||
if let Some(same) = find_same_asset(&self.assets, a) {
|
||||
return Some(same);
|
||||
}
|
||||
let fresh = AssetId(self.alloc());
|
||||
Some(self.add_asset(Asset { id: fresh, ..(**a).clone() }))
|
||||
}
|
||||
|
||||
/// Calls `f` with every asset reference: graphics on document and parent spreads (in
|
||||
/// groups and multi-state objects too), media posters, anchored and inline objects, graphic
|
||||
/// table cells and notes. False when nesting exceeded the depth limit.
|
||||
fn asset_refs(&self, f: &mut impl FnMut(AssetId)) -> bool {
|
||||
let mut complete = true;
|
||||
for sp in self.spreads.iter().chain(&self.parents) {
|
||||
for it in &sp.items {
|
||||
complete &= item_refs(it, f, 0);
|
||||
}
|
||||
}
|
||||
for st in self.stories.values() {
|
||||
complete &= story_refs(st, f, 0);
|
||||
}
|
||||
complete
|
||||
}
|
||||
|
||||
/// The assets something in the document references. When the document nests deeper than
|
||||
/// the traversal limit, every asset counts as used.
|
||||
pub fn used_assets(&self) -> BTreeSet<AssetId> {
|
||||
let mut used = BTreeSet::new();
|
||||
if !self.asset_refs(&mut |a| {
|
||||
used.insert(a);
|
||||
}) {
|
||||
return self.assets.keys().copied().collect();
|
||||
}
|
||||
used
|
||||
}
|
||||
|
||||
/// Point every reference to a key of `map` at its value.
|
||||
pub fn remap_assets(&mut self, map: &HashMap<AssetId, AssetId>) {
|
||||
if map.is_empty() {
|
||||
return;
|
||||
}
|
||||
for sp in self.spreads.iter_mut().chain(self.parents.iter_mut()) {
|
||||
if sp.items.iter().any(|it| item_uses(it, map)) {
|
||||
for it in &mut Arc::make_mut(sp).items {
|
||||
remap_item(it, map, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
for st in self.stories.values_mut() {
|
||||
if story_uses(st, map) {
|
||||
remap_story(Arc::make_mut(st), map, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge assets holding the same file into the first of them (lowest id), rewriting the
|
||||
/// references; returns how many assets were removed.
|
||||
pub fn merge_duplicate_assets(&mut self) -> usize {
|
||||
if !self.asset_refs(&mut |_| {}) {
|
||||
return 0;
|
||||
}
|
||||
let fingerprint = std::collections::hash_map::RandomState::new();
|
||||
let mut hashes = HashMap::new();
|
||||
let mut reps: HashMap<_, Vec<&Arc<Asset>>> = HashMap::new();
|
||||
let mut map = HashMap::new();
|
||||
for a in self.assets.values() {
|
||||
// Empty, unlinked assets never compare equal. Don't build a quadratic bucket
|
||||
// of them, and hash a shared immutable buffer only once per compaction.
|
||||
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 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)) {
|
||||
Some(r) => {
|
||||
map.insert(a.id, r.id);
|
||||
}
|
||||
None => bucket.push(a),
|
||||
}
|
||||
}
|
||||
self.remap_assets(&map);
|
||||
self.assets.retain(|id, _| !map.contains_key(id));
|
||||
map.len()
|
||||
}
|
||||
|
||||
/// Merge duplicate assets and drop the ones nothing references (what a saved file holds).
|
||||
pub fn compact_assets(&mut self) {
|
||||
self.merge_duplicate_assets();
|
||||
let used = self.used_assets();
|
||||
self.assets.retain(|id, _| used.contains(id));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Graphic, ItemId, LayerId, Shape, StoryId};
|
||||
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(), link: None, data: Arc::new(data.to_vec()), pixels: None, page: 0 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equal_size_assets_keep_distinct_contents_and_merge_exact_copies() {
|
||||
let mut d = Document::new(&Default::default());
|
||||
for id in 1..=256u64 {
|
||||
let mut bytes = vec![42; 1024];
|
||||
bytes[1016..].copy_from_slice(&id.to_le_bytes());
|
||||
d.assets.insert(AssetId(id), Arc::new(asset(id, &bytes)));
|
||||
d.assets.insert(AssetId(id + 256), Arc::new(asset(id + 256, &bytes)));
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compaction_preserves_asset_identity_and_missing_data_rules() {
|
||||
let mut d = Document::new(&Default::default());
|
||||
let mut cases = vec![asset(1, b"same"), asset(2, b"same"), asset(3, b"same"), asset(4, b"same")];
|
||||
cases[1].mime = "application/pdf".into();
|
||||
cases[2].page = 1;
|
||||
cases[3].link = Some("/linked.png".into());
|
||||
cases.extend([asset(5, b""), asset(6, b""), asset(7, b""), asset(8, b"")]);
|
||||
cases[6].link = Some("/missing.png".into());
|
||||
cases[7].link = Some("/missing.png".into());
|
||||
for a in cases {
|
||||
d.assets.insert(a.id, Arc::new(a));
|
||||
}
|
||||
assert_eq!(d.merge_duplicate_assets(), 1);
|
||||
assert_eq!(d.assets.keys().copied().collect::<Vec<_>>(), (1..=7).map(AssetId).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[ignore = "manual bounded timing comparison; no timing assertion"]
|
||||
fn equal_size_assets_timing() {
|
||||
for count in [500, 1000, 2000] {
|
||||
let mut d = Document::new(&Default::default());
|
||||
for id in 1..=count as u64 {
|
||||
let mut bytes = vec![42; 8192];
|
||||
bytes[8184..].copy_from_slice(&id.to_le_bytes());
|
||||
d.assets.insert(AssetId(id), Arc::new(asset(id, &bytes)));
|
||||
}
|
||||
let start = std::time::Instant::now();
|
||||
assert_eq!(d.merge_duplicate_assets(), 0);
|
||||
eprintln!("{count} distinct 8 KiB assets: {:?}", start.elapsed());
|
||||
}
|
||||
}
|
||||
|
||||
fn graphic_item(id: u64, asset: u64, crop: f64) -> Item {
|
||||
let mut it = Item::new(ItemId(id), LayerId(1), Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)));
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: AssetId(asset),
|
||||
size: (10.0, 10.0),
|
||||
xf: Affine::translate((crop, 0.0)),
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [crop; 4],
|
||||
});
|
||||
it
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identical_files_share_one_asset() {
|
||||
let mut d = Document::new(&Default::default());
|
||||
let a = d.add_asset(asset(1, b"same"));
|
||||
let b = d.add_asset(asset(2, b"same"));
|
||||
let c = d.add_asset(asset(3, b"diff"));
|
||||
let p = d.add_asset(Asset { page: 1, ..asset(4, b"same") });
|
||||
assert_eq!(a, b);
|
||||
assert_ne!(a, c);
|
||||
assert_ne!(a, p);
|
||||
assert_eq!(d.assets.len(), 3);
|
||||
// Unreadable links match by path only.
|
||||
let m1 = d.add_asset(Asset { link: Some("/m/1.png".into()), ..asset(5, b"") });
|
||||
let m2 = d.add_asset(Asset { link: Some("/m/2.png".into()), ..asset(6, b"") });
|
||||
let m3 = d.add_asset(asset(7, b""));
|
||||
let m4 = d.add_asset(asset(8, b""));
|
||||
assert_ne!(m1, m2);
|
||||
assert_ne!(m3, m4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_at_different_paths_stay_separate_assets() {
|
||||
let mut d = Document::new(&Default::default());
|
||||
let linked = |id, path: &str| Asset { link: Some(path.into()), ..asset(id, b"same") };
|
||||
let embedded = d.add_asset(asset(1, b"same"));
|
||||
let a = d.add_asset(linked(2, "/p/a.png"));
|
||||
let b = d.add_asset(linked(3, "/q/b.png"));
|
||||
assert_eq!(d.add_asset(linked(4, "/p/a.png")), a, "the same path placed again");
|
||||
assert_eq!(d.assets.len(), 3);
|
||||
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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compaction_merges_duplicates_and_drops_unused_assets() {
|
||||
let mut d = Document::new(&Default::default());
|
||||
for (id, data) in [(1, b"one"), (2, b"one"), (3, b"two"), (4, b"old")] {
|
||||
d.assets.insert(AssetId(id), Arc::new(asset(id, data)));
|
||||
}
|
||||
let sp = Arc::make_mut(d.spreads.first_mut().unwrap());
|
||||
sp.items.push(Arc::new(graphic_item(10, 1, 1.0)));
|
||||
sp.items.push(Arc::new(graphic_item(11, 2, 2.0)));
|
||||
// A graphic cell and an anchored object in a story.
|
||||
let mut st = Story::new(StoryId(20));
|
||||
st.objects.push(Arc::new(crate::AnchoredObject::new(graphic_item(12, 3, 3.0), Default::default())));
|
||||
d.stories.insert(StoryId(20), Arc::new(st));
|
||||
assert_eq!(d.used_assets(), [1, 2, 3].map(AssetId).into_iter().collect());
|
||||
|
||||
d.compact_assets();
|
||||
assert_eq!(d.assets.keys().copied().collect::<Vec<_>>(), vec![AssetId(1), AssetId(3)]);
|
||||
let crops: Vec<(AssetId, f64)> = d.spreads[0].items.iter().filter_map(|i| i.graphic().map(|g| (g.asset, g.crop[0]))).collect();
|
||||
assert_eq!(crops, vec![(AssetId(1), 1.0), (AssetId(1), 2.0)]);
|
||||
}
|
||||
}
|
||||
+30
-2
@@ -431,6 +431,8 @@ pub enum NestedUntil {
|
||||
ForcedLineBreak,
|
||||
EmSpace,
|
||||
EnSpace,
|
||||
/// The paragraph's drop cap characters.
|
||||
Dropcap,
|
||||
/// Any of these characters.
|
||||
Chars(String),
|
||||
}
|
||||
@@ -467,6 +469,18 @@ impl Default for NestedStyle {
|
||||
}
|
||||
}
|
||||
|
||||
impl NestedStyle {
|
||||
/// The drop cap's character style as a nested style: `style` through 1 drop cap.
|
||||
pub fn drop_cap(style: &str) -> Self {
|
||||
NestedStyle { style: style.into(), through: true, count: 1, until: NestedUntil::Dropcap }
|
||||
}
|
||||
|
||||
/// Is this the form a drop cap's character style takes at the start of a nested style list?
|
||||
pub fn is_drop_cap(&self) -> bool {
|
||||
self.until == NestedUntil::Dropcap && self.through && self.count == 1
|
||||
}
|
||||
}
|
||||
|
||||
/// A character style applied to every match of a regular expression in the paragraph.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", default)]
|
||||
@@ -530,12 +544,16 @@ pub struct TabStop {
|
||||
pub align_on: String,
|
||||
}
|
||||
|
||||
/// The colour of a paragraph rule that follows the paragraph's text colour.
|
||||
pub const TEXT_COLOR: &str = "Text Color";
|
||||
|
||||
/// A paragraph rule (Rule Above / Rule Below).
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct Rule {
|
||||
pub on: bool,
|
||||
pub weight: f64,
|
||||
/// A swatch, or [`TEXT_COLOR`].
|
||||
pub color: String,
|
||||
pub tint: f32,
|
||||
/// Width: column (true) or text (false).
|
||||
@@ -547,7 +565,7 @@ pub struct Rule {
|
||||
|
||||
impl Default for Rule {
|
||||
fn default() -> Self {
|
||||
Rule { on: false, weight: 1.0, color: "[Black]".into(), tint: 1.0, column_width: true, offset: 0.0, left_indent: 0.0, right_indent: 0.0 }
|
||||
Rule { on: false, weight: 1.0, color: TEXT_COLOR.into(), tint: 1.0, column_width: true, offset: 0.0, left_indent: 0.0, right_indent: 0.0 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,8 +827,17 @@ attr_set! {
|
||||
last_line_indent: f64 = 0.0,
|
||||
space_before: f64 = 0.0,
|
||||
space_after: f64 = 0.0,
|
||||
/// Drop cap: the paragraph's first `drop_cap_chars` characters (code points, never
|
||||
/// splitting a base from its combining marks) set `drop_cap_lines` lines tall, in the
|
||||
/// character style `drop_cap_style` ("" or `[None]`: the text's own formatting).
|
||||
drop_cap_lines: u32 = 0,
|
||||
drop_cap_chars: u32 = 0,
|
||||
drop_cap_style: String = String::new(),
|
||||
/// Align Left Edge: the drop cap's ink starts at the indent (its first letter's left side
|
||||
/// bearing is taken off), and the lines beside it start where its advance ends.
|
||||
drop_cap_align_left: bool = true,
|
||||
/// Scale for Descenders (kept and round-tripped; doesn't change the layout).
|
||||
drop_cap_scale_descenders: bool = false,
|
||||
/// Nested styles, in order from the paragraph start.
|
||||
nested_styles: Vec<NestedStyle> = Vec::new(),
|
||||
/// GREP styles: a character style for every match of a pattern (applied after nested).
|
||||
@@ -855,7 +882,8 @@ attr_set! {
|
||||
// Keeps
|
||||
keep_with_next: u32 = 0,
|
||||
keep_lines_together: bool = false,
|
||||
keep_all_lines: bool = true,
|
||||
/// All lines in paragraph (true) or at start/end of paragraph (false).
|
||||
keep_all_lines: bool = false,
|
||||
keep_first: u32 = 2,
|
||||
keep_last: u32 = 2,
|
||||
start_paragraph: StartParagraph = StartParagraph::Anywhere,
|
||||
|
||||
@@ -226,6 +226,7 @@ impl Document {
|
||||
liquid: Default::default(),
|
||||
transition: None,
|
||||
view_rotation: 0,
|
||||
show_parent_overlay: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+236
-64
@@ -268,87 +268,229 @@ impl Document {
|
||||
Ok((id, sid))
|
||||
}
|
||||
|
||||
/// Thread `from` → `to`: `to` joins `from`'s story right after `from`. If `to` held its own text,
|
||||
/// that text is appended to the story (as a new paragraph) and its old story is removed.
|
||||
/// Thread `from` → `to`: `to` joins `from`'s story right after `from` (between `from` and the
|
||||
/// frame that followed it, if any). If `to` held its own text, that text is appended to the
|
||||
/// story (as new paragraphs) and its old story is removed; frames that followed `to` come
|
||||
/// along, frames before it keep an empty story of their own. An empty (unassigned) `from` or
|
||||
/// `to` becomes a text frame. `from`'s story keeps its id.
|
||||
pub fn thread(&mut self, from: ItemId, to: ItemId) -> Result<()> {
|
||||
if from == to {
|
||||
return Err(DocError::Invalid("cannot thread a frame to itself".into()));
|
||||
}
|
||||
let fs = self.item(from).and_then(|i| i.text_frame()).map(|t| t.story).ok_or(DocError::NoItem(from))?;
|
||||
// Convert unassigned/graphic-less frames into text frames.
|
||||
let ts = match self.item(to).ok_or(DocError::NoItem(to))?.content.clone() {
|
||||
Content::Text(t) => Some(t.story),
|
||||
Content::Unassigned => None,
|
||||
_ => return Err(DocError::Invalid("can only thread to text or empty frames".into())),
|
||||
self.check_thread_links(from, to, false)?;
|
||||
let ts = self.frame_story(to)?;
|
||||
let fs = match self.frame_story(from)? {
|
||||
Some(s) => s,
|
||||
None => self.give_empty_story(from)?,
|
||||
};
|
||||
if ts == Some(fs) {
|
||||
return Err(DocError::Invalid("frames are already in the same story".into()));
|
||||
}
|
||||
if let Some(ts) = ts {
|
||||
let other = self.stories.remove(&ts).ok_or(DocError::NoStory(ts))?;
|
||||
let other = Arc::unwrap_or_clone(other);
|
||||
// Frames of the other story after `to` follow along; frames before it are left as empty new stories.
|
||||
let pos_to = other.frames.iter().position(|f| *f == to).unwrap_or(0);
|
||||
let before: Vec<ItemId> = other.frames[..pos_to].to_vec();
|
||||
let after: Vec<ItemId> = other.frames[pos_to..].to_vec();
|
||||
{
|
||||
let st = self.story_mut(fs).ok_or(DocError::NoStory(fs))?;
|
||||
if !other.text.is_empty() {
|
||||
st.text.push('\n');
|
||||
st.text.push_str(&other.text);
|
||||
st.paras.extend(other.paras.iter().cloned());
|
||||
// Char runs: '\n' takes the last run's format, then the other story's runs.
|
||||
if let Some(last) = st.chars.last_mut() {
|
||||
last.len += 1;
|
||||
}
|
||||
st.chars.extend(other.chars.iter().filter(|r| r.len > 0).cloned());
|
||||
st.tables.extend(other.tables.iter().map(|(k, v)| (*k, v.clone())));
|
||||
for n in &other.notes {
|
||||
let id = st.next_note_id();
|
||||
st.notes.push(Arc::new(crate::Footnote { id, text: n.text.clone() }));
|
||||
}
|
||||
st.anchors.extend(other.anchors.iter().cloned());
|
||||
st.xrefs.extend(other.xrefs.iter().cloned());
|
||||
st.index_refs.extend(other.index_refs.iter().cloned());
|
||||
st.objects.extend(other.objects.iter().cloned());
|
||||
st.rev += 1;
|
||||
}
|
||||
let at = st.frames.iter().position(|f| *f == from).map_or(st.frames.len(), |p| p + 1);
|
||||
for (k, f) in after.iter().enumerate() {
|
||||
st.frames.insert(at + k, *f);
|
||||
}
|
||||
}
|
||||
for f in &after {
|
||||
if let Some(t) = self.item_mut(*f).and_then(Item::text_frame_mut) {
|
||||
t.story = fs;
|
||||
}
|
||||
}
|
||||
if !before.is_empty() {
|
||||
let nid = StoryId(self.alloc());
|
||||
let mut ns = Story::new(nid);
|
||||
ns.frames = before.clone();
|
||||
ns.vertical = other.vertical;
|
||||
ns.direction = other.direction;
|
||||
for f in &before {
|
||||
if let Some(t) = self.item_mut(*f).and_then(Item::text_frame_mut) {
|
||||
t.story = nid;
|
||||
}
|
||||
}
|
||||
self.stories.insert(nid, Arc::new(ns));
|
||||
}
|
||||
} else {
|
||||
let Some(ts) = ts else {
|
||||
let st = self.story_mut(fs).ok_or(DocError::NoStory(fs))?;
|
||||
let at = st.frames.iter().position(|f| *f == from).map_or(st.frames.len(), |p| p + 1);
|
||||
st.frames.insert(at, to);
|
||||
st.rev += 1;
|
||||
let it = self.item_mut(to).ok_or(DocError::NoItem(to))?;
|
||||
it.content = Content::Text(TextFrame { story: fs, options: TextFrameOptions::default() });
|
||||
}
|
||||
if let Some(st) = self.story_mut(fs) {
|
||||
return Ok(());
|
||||
};
|
||||
let mut other = Arc::unwrap_or_clone(self.stories.remove(&ts).ok_or(DocError::NoStory(ts))?);
|
||||
// Frames of the other story from `to` on follow along; frames before it are left as an empty new story.
|
||||
let pos_to = other.frames.iter().position(|f| *f == to).unwrap_or(0);
|
||||
let after = other.frames.split_off(pos_to);
|
||||
let before = std::mem::take(&mut other.frames);
|
||||
let (vertical, direction) = (other.vertical, other.direction);
|
||||
let offset;
|
||||
{
|
||||
let st = self.story_mut(fs).ok_or(DocError::NoStory(fs))?;
|
||||
offset = if st.text.is_empty() { 0 } else { st.text.len().saturating_add(1) };
|
||||
// The story keeps its own direction, even when it was empty and takes the other's text.
|
||||
let own = (st.vertical, st.direction);
|
||||
st.append_story(other);
|
||||
(st.vertical, st.direction) = own;
|
||||
let at = st.frames.iter().position(|f| *f == from).map_or(st.frames.len(), |p| p + 1);
|
||||
for (k, f) in after.iter().enumerate() {
|
||||
st.frames.insert(at + k, *f);
|
||||
}
|
||||
st.rev += 1;
|
||||
}
|
||||
self.remap_thread_story_refs(ts, fs, offset);
|
||||
for f in &after {
|
||||
if let Some(t) = self.item_mut(*f).and_then(Item::text_frame_mut) {
|
||||
t.story = fs;
|
||||
}
|
||||
}
|
||||
if !before.is_empty() {
|
||||
let nid = StoryId(self.alloc());
|
||||
let mut ns = Story::new(nid);
|
||||
ns.frames = before.clone();
|
||||
ns.vertical = vertical;
|
||||
ns.direction = direction;
|
||||
for f in &before {
|
||||
if let Some(t) = self.item_mut(*f).and_then(Item::text_frame_mut) {
|
||||
t.story = nid;
|
||||
}
|
||||
}
|
||||
self.stories.insert(nid, Arc::new(ns));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [`Document::thread`], except that an empty `from` alone in its story, threaded in front of
|
||||
/// the first frame of `to`'s story, joins that story: the story with the text keeps its id
|
||||
/// (and whatever refers to it).
|
||||
pub fn thread_keeping_story(&mut self, from: ItemId, to: ItemId) -> Result<()> {
|
||||
let fs = self.frame_story(from)?;
|
||||
let from_empty = fs.is_none_or(|fs| self.story(fs).is_some_and(|s| s.text.is_empty() && s.frames == [from]));
|
||||
let ts = self.frame_story(to)?;
|
||||
let to_first = ts.and_then(|ts| self.story(ts)).is_some_and(|s| s.frames.first() == Some(&to));
|
||||
let (Some(ts), true, true) = (ts, from_empty, to_first && from != to) else { return self.thread(from, to) };
|
||||
self.check_thread_links(from, to, true)?;
|
||||
if let Some(fs) = fs {
|
||||
self.stories.remove(&fs);
|
||||
self.remap_thread_story_refs(fs, ts, 0);
|
||||
}
|
||||
let st = self.story_mut(ts).ok_or(DocError::NoStory(ts))?;
|
||||
st.frames.insert(0, from);
|
||||
st.rev += 1;
|
||||
let it = self.item_mut(from).ok_or(DocError::NoItem(from))?;
|
||||
match it.text_frame_mut() {
|
||||
Some(t) => t.story = ts,
|
||||
None => it.content = Content::Text(TextFrame { story: ts, options: TextFrameOptions::default() }),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Story-external text addresses move with the incoming text when threading merges stories.
|
||||
/// Linked children follow the surviving whole story, including for an empty removed parent;
|
||||
/// their copied content stays unchanged until an explicit Update Link.
|
||||
fn remap_thread_story_refs(&mut self, from: StoryId, to: StoryId, offset: usize) {
|
||||
// The child's old snapshot is not a snapshot of the newly merged parent. Force it
|
||||
// out of date even when the old and surviving revision numbers happen to coincide.
|
||||
let stale_rev = self.story(to).map_or(0, |st| st.rev.wrapping_sub(1));
|
||||
for st in self.stories.values_mut() {
|
||||
if st.link.is_some_and(|(parent, _)| parent == from) {
|
||||
Arc::make_mut(st).link = Some((to, stale_rev));
|
||||
}
|
||||
}
|
||||
for link in &mut self.hyperlinks {
|
||||
if let crate::HyperlinkSource::Text { story, start, end } = &mut link.source
|
||||
&& *story == from
|
||||
{
|
||||
*story = to;
|
||||
*start = start.saturating_add(offset);
|
||||
*end = end.saturating_add(offset);
|
||||
}
|
||||
}
|
||||
for placeholder in &mut self.data_merge.placeholders {
|
||||
if let crate::PlaceholderAnchor::Text { story, start, end } = &mut placeholder.anchor
|
||||
&& *story == from
|
||||
{
|
||||
*story = to;
|
||||
*start = start.saturating_add(offset);
|
||||
*end = end.saturating_add(offset);
|
||||
}
|
||||
}
|
||||
if self.settings.primary_story == Some(from) {
|
||||
self.settings.primary_story = Some(to);
|
||||
}
|
||||
}
|
||||
|
||||
/// A linked child is replaced wholesale on update. Only empty, unlinked frame
|
||||
/// extensions can join it without losing a link or newly appended body text.
|
||||
fn check_thread_links(&self, from: ItemId, to: ItemId, keep_to: bool) -> Result<()> {
|
||||
let fs = self.frame_story(from)?;
|
||||
let ts = self.frame_story(to)?;
|
||||
let first = fs.and_then(|id| self.story(id));
|
||||
let second = ts.and_then(|id| self.story(id));
|
||||
let incompatible = if keep_to {
|
||||
first.is_some_and(|st| st.link.is_some()) || second.is_some_and(|st| st.link.is_some_and(|(parent, _)| Some(parent) == fs))
|
||||
} else {
|
||||
second.is_some_and(|st| st.link.is_some())
|
||||
|| first.is_some_and(|st| {
|
||||
st.link.is_some()
|
||||
&& second.is_some_and(|incoming| !incoming.text.is_empty() || st.link.is_some_and(|(parent, _)| parent == incoming.id))
|
||||
})
|
||||
};
|
||||
if incompatible {
|
||||
return Err(DocError::Invalid("unlink the linked story before merging its text".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Can a user thread `from` → `to`? Both are text frames or empty frames (not lines), on
|
||||
/// document spreads or on the same parent spread, not in one story already, and no frame
|
||||
/// threads into `to` yet. [`Document::thread`] itself is less strict (import and autoflow).
|
||||
pub fn check_thread(&self, from: ItemId, to: ItemId) -> Result<()> {
|
||||
if from == to {
|
||||
return Err(DocError::Invalid("cannot thread a frame to itself".into()));
|
||||
}
|
||||
for id in [from, to] {
|
||||
let it = self.item(id).ok_or(DocError::NoItem(id))?;
|
||||
if !matches!(it.content, Content::Text(_) | Content::Unassigned) || it.shape == Shape::GraphicLine {
|
||||
return Err(DocError::Invalid(format!("{id} is not a text frame or an empty frame")));
|
||||
}
|
||||
if it.locked || self.layer(it.layer).is_some_and(|l| l.locked) {
|
||||
return Err(DocError::Invalid(format!("{id} is locked or on a locked layer")));
|
||||
}
|
||||
}
|
||||
let (Some(fl), Some(tl)) = (self.find(from), self.find(to)) else { return Err(DocError::NoItem(to)) };
|
||||
let same_place = match (fl.spread, tl.spread) {
|
||||
(SpreadRef::Doc(_), SpreadRef::Doc(_)) => true,
|
||||
(a, b) => a == b,
|
||||
};
|
||||
if !same_place {
|
||||
return Err(DocError::Invalid("frames on a parent spread thread only to frames on the same parent".into()));
|
||||
}
|
||||
let story = |id: ItemId| self.item(id).and_then(|i| i.text_frame()).map(|t| t.story);
|
||||
if story(from).is_some() && story(from) == story(to) {
|
||||
return Err(DocError::Invalid("the frames are already in the same thread".into()));
|
||||
}
|
||||
// Generated text is rebuilt from its source data. Merging body text into it would
|
||||
// silently discard that text on the next update (endnotes update after every edit).
|
||||
let generated = |sid| {
|
||||
self.endnote_story == Some(sid)
|
||||
|| self.toc.as_ref().is_some_and(|t| t.story == sid)
|
||||
|| self.index.as_ref().is_some_and(|i| i.story == sid)
|
||||
};
|
||||
let (fs, ts) = (story(from), story(to));
|
||||
if fs.is_some_and(generated) && ts.and_then(|sid| self.story(sid)).is_some_and(|st| !st.text.is_empty()) {
|
||||
return Err(DocError::Invalid("cannot merge body text into a generated story".into()));
|
||||
}
|
||||
if ts.is_some_and(generated) && fs.and_then(|sid| self.story(sid)).is_some_and(|st| !st.text.is_empty() || st.frames != [from]) {
|
||||
return Err(DocError::Invalid("a generated story can only take a new empty first frame".into()));
|
||||
}
|
||||
let keep_to = fs.is_none_or(|sid| self.story(sid).is_some_and(|st| st.text.is_empty() && st.frames == [from]))
|
||||
&& ts.and_then(|sid| self.story(sid)).is_some_and(|st| st.frames.first() == Some(&to));
|
||||
self.check_thread_links(from, to, keep_to)?;
|
||||
if self.prev_frame(to).is_some() {
|
||||
return Err(DocError::Invalid(format!("text already flows into {to} from another frame: break that thread first")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The story of a frame that can take part in a thread: `Some` for a text frame, `None` for
|
||||
/// an empty (unassigned) frame.
|
||||
fn frame_story(&self, id: ItemId) -> Result<Option<StoryId>> {
|
||||
match &self.item(id).ok_or(DocError::NoItem(id))?.content {
|
||||
Content::Text(t) => Ok(Some(t.story)),
|
||||
Content::Unassigned => Ok(None),
|
||||
_ => Err(DocError::Invalid("can only thread text frames or empty frames".into())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Make an empty frame a text frame with a new empty story.
|
||||
fn give_empty_story(&mut self, id: ItemId) -> Result<StoryId> {
|
||||
let sid = StoryId(self.alloc());
|
||||
let mut st = Story::new(sid);
|
||||
st.frames.push(id);
|
||||
let it = self.item_mut(id).ok_or(DocError::NoItem(id))?;
|
||||
it.content = Content::Text(TextFrame { story: sid, options: TextFrameOptions::default() });
|
||||
self.stories.insert(sid, Arc::new(st));
|
||||
Ok(sid)
|
||||
}
|
||||
|
||||
/// Break the thread after `frame`: the following frames become a new empty story (the text
|
||||
/// stays with the first part and may become overset).
|
||||
pub fn unthread_after(&mut self, frame: ItemId) -> Result<()> {
|
||||
@@ -377,6 +519,30 @@ impl Document {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Take `frame` out of its thread: the frames before and after it join up and keep the text,
|
||||
/// `frame` stays on the page with a new empty story.
|
||||
pub fn remove_from_thread(&mut self, frame: ItemId) -> Result<()> {
|
||||
let sid = self.item(frame).and_then(|i| i.text_frame()).map(|t| t.story).ok_or(DocError::NoItem(frame))?;
|
||||
let (vertical, direction) = {
|
||||
let st = self.story_mut(sid).ok_or(DocError::NoStory(sid))?;
|
||||
if st.frames.len() < 2 {
|
||||
return Err(DocError::Invalid(format!("frame {frame} is not threaded to another frame")));
|
||||
}
|
||||
st.frames.retain(|f| *f != frame);
|
||||
st.rev += 1;
|
||||
(st.vertical, st.direction)
|
||||
};
|
||||
let nid = StoryId(self.alloc());
|
||||
let mut ns = Story::new(nid);
|
||||
ns.frames = vec![frame];
|
||||
ns.vertical = vertical;
|
||||
ns.direction = direction;
|
||||
self.stories.insert(nid, Arc::new(ns));
|
||||
let t = self.item_mut(frame).and_then(Item::text_frame_mut).ok_or(DocError::NoItem(frame))?;
|
||||
t.story = nid;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Next / previous frame in a thread.
|
||||
pub fn next_frame(&self, frame: ItemId) -> Option<ItemId> {
|
||||
let st = self.story(self.item(frame)?.text_frame()?.story)?;
|
||||
@@ -484,5 +650,11 @@ pub fn item_hit(it: &Item, p: Point, tol: f64) -> bool {
|
||||
if area && it.path.is_closed() && designcraft_geom::hit::fill_contains(&bp, designcraft_geom::FillRule::NonZero, inner) {
|
||||
return true;
|
||||
}
|
||||
designcraft_geom::hit::stroke_contains(&bp, it.stroke.weight.max(1.0), tol, inner)
|
||||
// Type on a path: the letters stand over the path, so a click on them hits it too.
|
||||
let reach = if it.text_frame().is_some_and(|t| t.options.path.is_some()) { PATH_TYPE_REACH } else { 0.0 };
|
||||
designcraft_geom::hit::stroke_contains(&bp, it.stroke.weight.max(1.0), tol + reach, inner)
|
||||
}
|
||||
|
||||
/// How far from its path type on a path is hit (points): about a cap height of 12–18 pt type,
|
||||
/// either side (the type may sit on or under the path).
|
||||
const PATH_TYPE_REACH: f64 = 12.0;
|
||||
|
||||
@@ -21,3 +21,24 @@ id_type!(SpreadId, "sp");
|
||||
id_type!(PageId, "p");
|
||||
id_type!(LayerId, "l");
|
||||
id_type!(AssetId, "a");
|
||||
|
||||
/// Fresh story-local id. Keep monotonically increasing ids normally; a loaded MAX id must
|
||||
/// not make the next insertion overwrite it. In that case choose the first unused gap.
|
||||
pub(crate) fn next_local_id(ids: impl Iterator<Item = u64> + Clone) -> u64 {
|
||||
if let Some(next) = ids.clone().max().unwrap_or(0).checked_add(1) {
|
||||
return next;
|
||||
}
|
||||
let mut used: Vec<_> = ids.collect();
|
||||
used.sort_unstable();
|
||||
let mut next = 1u64;
|
||||
for id in used {
|
||||
if id > next {
|
||||
break;
|
||||
}
|
||||
if id == next {
|
||||
// An allocated Vec cannot contain every u64 id, so this always finds a gap.
|
||||
next = next.saturating_add(1);
|
||||
}
|
||||
}
|
||||
next
|
||||
}
|
||||
|
||||
+55
-5
@@ -281,6 +281,10 @@ pub enum FirstBaseline {
|
||||
Leading,
|
||||
XHeight,
|
||||
Fixed,
|
||||
/// The top of the font's em box: its `BASE` em box; else, for a CJK font, its OS/2 typo
|
||||
/// ascender when the typo ascender and descender span one em; else half an em above half the
|
||||
/// cap height.
|
||||
EmboxHeight,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -321,7 +325,7 @@ pub struct TextFrameOptions {
|
||||
pub first_baseline_min: f64,
|
||||
pub ignore_wrap: bool,
|
||||
pub auto_size: AutoSize,
|
||||
/// Reference point (0..9) that stays fixed when auto-sizing.
|
||||
/// Reference point (0..9, row-major from the top left) that stays fixed when auto-sizing.
|
||||
pub auto_size_ref: u8,
|
||||
pub column_rule: bool,
|
||||
pub column_rule_weight: f64,
|
||||
@@ -345,8 +349,12 @@ pub struct TextFrameOptions {
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", default)]
|
||||
pub struct PathType {
|
||||
/// Where the text starts, as a distance along the path.
|
||||
/// Where the text starts, as a distance along the path (the start bracket).
|
||||
pub start: f64,
|
||||
/// Where the text ends, as a distance along the path (the end bracket); `None` is the path's
|
||||
/// end. Text past it is overset.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub end: Option<f64>,
|
||||
/// Run the other way (the text sits on the other side).
|
||||
pub flip: bool,
|
||||
pub align: PathAlign,
|
||||
@@ -378,7 +386,7 @@ impl Default for TextFrameOptions {
|
||||
first_baseline_min: 0.0,
|
||||
ignore_wrap: false,
|
||||
auto_size: AutoSize::Off,
|
||||
auto_size_ref: 1,
|
||||
auto_size_ref: 4,
|
||||
column_rule: false,
|
||||
column_rule_weight: 1.0,
|
||||
column_rule_color: designcraft_color::swatch::BLACK.into(),
|
||||
@@ -1152,13 +1160,29 @@ impl Item {
|
||||
b.inflate(w, w)
|
||||
}
|
||||
|
||||
/// The frame's text area (inner space) after inset.
|
||||
/// The text frame's text area (inner space): inside the part of a visible stroke that lies
|
||||
/// within the frame, then the inset, on each edge.
|
||||
pub fn text_area(&self) -> Rect {
|
||||
let r = self.inner_bounds();
|
||||
let inset = self.text_frame().map(|t| t.options.inset).unwrap_or([0.0; 4]);
|
||||
let s = self.stroke_inside();
|
||||
let inset = self.text_frame().map(|t| t.options.inset).unwrap_or([0.0; 4]).map(|v| v + s);
|
||||
Rect::new(r.x0 + inset[1], r.y0 + inset[0], (r.x1 - inset[3]).max(r.x0 + inset[1]), (r.y1 - inset[2]).max(r.y0 + inset[0]))
|
||||
}
|
||||
|
||||
/// How far a visible stroke reaches inside the path: half its weight centred, all of it inside,
|
||||
/// none outside.
|
||||
fn stroke_inside(&self) -> f64 {
|
||||
let w = self.stroke.weight;
|
||||
if self.stroke.is_none() || !w.is_finite() {
|
||||
return 0.0;
|
||||
}
|
||||
match self.stroke.align {
|
||||
StrokeAlign::Center => w / 2.0,
|
||||
StrokeAlign::Inside => w,
|
||||
StrokeAlign::Outside => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Spread-space centre.
|
||||
pub fn center(&self) -> Point {
|
||||
self.bounds().center()
|
||||
@@ -1203,6 +1227,32 @@ mod tests {
|
||||
assert_eq!(it.visible_bounds(), Rect::new(8.0, 18.0, 112.0, 72.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_area_starts_inside_the_stroke() {
|
||||
let mut it = Item::new(ItemId(1), LayerId(1), Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 50.0)));
|
||||
it.content = Content::Text(TextFrame { story: StoryId(2), options: TextFrameOptions { inset: [5.0, 6.0, 7.0, 8.0], ..Default::default() } });
|
||||
let unstroked = Rect::new(6.0, 5.0, 92.0, 43.0);
|
||||
let stroked = |align: StrokeAlign, swatch: &str| {
|
||||
let mut it = it.clone();
|
||||
it.stroke = Stroke { weight: 4.0, align, swatch: swatch.into(), ..Stroke::default() };
|
||||
it.text_area()
|
||||
};
|
||||
let black = designcraft_color::swatch::BLACK;
|
||||
// The part of the stroke inside the frame, on every edge, then the inset.
|
||||
assert_eq!(stroked(StrokeAlign::Center, black), Rect::new(8.0, 7.0, 90.0, 41.0));
|
||||
assert_eq!(stroked(StrokeAlign::Inside, black), Rect::new(10.0, 9.0, 88.0, 39.0));
|
||||
assert_eq!(stroked(StrokeAlign::Outside, black), unstroked);
|
||||
// A stroke without a colour doesn't count.
|
||||
assert_eq!(stroked(StrokeAlign::Inside, designcraft_color::swatch::NONE), unstroked);
|
||||
// Nor does a weight that isn't a number.
|
||||
it.stroke = Stroke { weight: f64::NAN, ..Stroke::default() };
|
||||
assert_eq!(it.text_area(), unstroked);
|
||||
// A stroke wider than the frame leaves no room for text.
|
||||
it.stroke = Stroke { weight: 200.0, align: StrokeAlign::Inside, ..Stroke::default() };
|
||||
let a = it.text_area();
|
||||
assert!(a.width() == 0.0 && a.height() == 0.0, "{a:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_bounds_union_children() {
|
||||
let a = Item::new(ItemId(1), LayerId(1), Shape::Rectangle, shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)));
|
||||
|
||||
+40
-7
@@ -12,6 +12,7 @@
|
||||
pub mod anchored;
|
||||
pub mod arabic;
|
||||
pub mod arrow;
|
||||
pub mod assets;
|
||||
pub mod attrs;
|
||||
pub mod build;
|
||||
pub mod cjk;
|
||||
@@ -36,6 +37,7 @@ use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use anchored::{AnchorPosition, AnchoredObject, OBJECT_MARK};
|
||||
pub use assets::{find_same_asset, insert_asset, remap_story_assets, story_asset_refs};
|
||||
pub use attrs::*;
|
||||
pub use datamerge::{
|
||||
DataField, DataFieldKind, DataMerge, DataSource, Delimiter, Fingerprint, MergeOptions, Placeholder, PlaceholderAnchor, PlaceholderRole,
|
||||
@@ -155,7 +157,7 @@ impl Default for DocumentGrid {
|
||||
}
|
||||
|
||||
/// A named numbered list.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", default)]
|
||||
pub struct NumberedList {
|
||||
pub name: String,
|
||||
@@ -163,12 +165,6 @@ pub struct NumberedList {
|
||||
pub continue_across_stories: bool,
|
||||
}
|
||||
|
||||
impl Default for NumberedList {
|
||||
fn default() -> Self {
|
||||
NumberedList { name: String::new(), continue_across_stories: true }
|
||||
}
|
||||
}
|
||||
|
||||
/// A conditional-text condition: its indicator colour (screen only) and whether text with it shows.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -481,6 +477,16 @@ pub struct Asset {
|
||||
pub page: u32,
|
||||
}
|
||||
|
||||
impl Asset {
|
||||
/// A raster image (PNG, JPEG, GIF, WebP, TIFF, BMP, PSD), as opposed to a PDF, EPS or SVG.
|
||||
pub fn is_raster(&self) -> bool {
|
||||
matches!(
|
||||
self.mime.as_str(),
|
||||
"image/png" | "image/jpeg" | "image/gif" | "image/webp" | "image/tiff" | "image/bmp" | "image/vnd.adobe.photoshop"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_zero(v: &u32) -> bool {
|
||||
*v == 0
|
||||
}
|
||||
@@ -603,6 +609,30 @@ pub struct TocEntry {
|
||||
/// 1-based nesting level.
|
||||
#[serde(default = "one")]
|
||||
pub level: u8,
|
||||
/// Paragraph style of this style's entries; `None` is the generated "TOC Level n" style.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub entry_style: Option<String>,
|
||||
#[serde(default)]
|
||||
pub page_number: TocPageNumber,
|
||||
/// Text between the entry and its page number; `None` is a tab.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub separator: Option<String>,
|
||||
}
|
||||
|
||||
impl TocEntry {
|
||||
pub fn new(style: impl Into<String>, level: u8) -> Self {
|
||||
Self { style: style.into(), level, entry_style: None, page_number: TocPageNumber::After, separator: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// Where an entry's page number goes.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum TocPageNumber {
|
||||
#[default]
|
||||
After,
|
||||
Before,
|
||||
None,
|
||||
}
|
||||
|
||||
fn one() -> u8 {
|
||||
@@ -617,6 +647,9 @@ pub struct Toc {
|
||||
pub entries: Vec<TocEntry>,
|
||||
#[serde(default)]
|
||||
pub page_numbers: bool,
|
||||
/// Paragraph style of the title; `None` is the generated "TOC Title" style.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub title_style: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
|
||||
@@ -178,7 +178,7 @@ impl Story {
|
||||
}
|
||||
|
||||
pub(crate) fn next_note_id(&self) -> u64 {
|
||||
self.notes.iter().map(|n| n.id).max().unwrap_or(0) + 1
|
||||
crate::ids::next_local_id(self.notes.iter().map(|n| n.id))
|
||||
}
|
||||
|
||||
/// Insert empty footnotes for `n` new reference characters before which `k` references sit.
|
||||
|
||||
+23
-1
@@ -114,6 +114,9 @@ pub struct Page {
|
||||
/// Liquid page rule: how objects follow when the page changes size.
|
||||
#[serde(default, skip_serializing_if = "LiquidRule::is_off")]
|
||||
pub liquid: LiquidRule,
|
||||
/// Page tool › Show Parent Page Overlay: the parent page's size drawn over this page.
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
pub show_parent_overlay: bool,
|
||||
/// Window › Interactive › Page Transitions (the spread's first page holds the spread's).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub transition: Option<PageTransition>,
|
||||
@@ -208,6 +211,8 @@ pub enum LiquidRule {
|
||||
GuideBased,
|
||||
/// Each object's pins and resize settings decide (`Item::liquid`).
|
||||
ObjectBased,
|
||||
/// The rule of the page's parent page (InDesign's "Controlled by Parent"; Off without one).
|
||||
ControlledByParent,
|
||||
}
|
||||
|
||||
impl LiquidRule {
|
||||
@@ -246,7 +251,8 @@ impl Page {
|
||||
|
||||
/// Split `area` into `n` equal columns separated by `gutter`.
|
||||
pub fn column_rects(area: Rect, n: u32, gutter: f64) -> Vec<Rect> {
|
||||
let n = n.max(1) as usize;
|
||||
// The public allocator also receives native/imported options, outside command validation.
|
||||
let n = n.clamp(1, 216) as usize;
|
||||
let w = ((area.width() - gutter * (n as f64 - 1.0)) / n as f64).max(0.0);
|
||||
(0..n).map(|i| Rect::new(area.x0 + i as f64 * (w + gutter), area.y0, area.x0 + i as f64 * (w + gutter) + w, area.y1)).collect()
|
||||
}
|
||||
@@ -367,6 +373,7 @@ mod tests {
|
||||
liquid: Default::default(),
|
||||
transition: None,
|
||||
view_rotation: 0,
|
||||
show_parent_overlay: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,3 +410,18 @@ mod tests {
|
||||
assert_eq!(s.page_at_x(5000.0), Some(1));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod column_allocation_safety_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn column_allocation_caps_page_count_and_preserves_geometry() {
|
||||
let area = Rect::new(0., 0., 300., 100.);
|
||||
// Fail safely at a small boundary before exercising hostile counts.
|
||||
assert_eq!(column_rects(area, 217, 0.).len(), 216);
|
||||
assert_eq!(column_rects(area, u32::MAX, 0.).len(), 216);
|
||||
assert_eq!(column_rects(area, 0, 12.), vec![area]);
|
||||
assert_eq!(column_rects(area, 2, 12.), vec![Rect::new(0., 0., 144., 100.), Rect::new(156., 0., 300., 100.)]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +80,9 @@ pub struct Selection {
|
||||
/// Key object for Align.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub key: Option<ItemId>,
|
||||
/// The page selected with the Page tool (absolute index).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub page: Option<usize>,
|
||||
}
|
||||
|
||||
impl Selection {
|
||||
|
||||
@@ -126,6 +126,66 @@ impl Story {
|
||||
self.rev += 1;
|
||||
end
|
||||
}
|
||||
|
||||
/// Append the whole of `other` as new paragraphs after this story's text (threading two
|
||||
/// stories merges them this way). Its formatting, notes, endnotes, editorial notes, markers,
|
||||
/// anchored objects and tables come along; footnotes, endnotes, editorial notes and tables get
|
||||
/// ids that are free in this story. An empty story simply takes `other`'s content. Frames stay.
|
||||
pub fn append_story(&mut self, mut other: Story) {
|
||||
if other.text.is_empty() {
|
||||
return;
|
||||
}
|
||||
if self.text.is_empty() {
|
||||
let (id, frames, rev, link) = (self.id, std::mem::take(&mut self.frames), self.rev, self.link.take());
|
||||
*self = Story { id, frames, rev: rev.saturating_add(1), link, ..other };
|
||||
return;
|
||||
}
|
||||
if !other.tables.is_empty() {
|
||||
let mut ids = std::collections::HashMap::new();
|
||||
let mut next = crate::ids::next_local_id(self.tables.keys().copied());
|
||||
for (old, t) in std::mem::take(&mut other.tables) {
|
||||
let new = next;
|
||||
let mut t = Arc::unwrap_or_clone(t);
|
||||
t.id = new;
|
||||
ids.insert(old, new);
|
||||
self.tables.insert(new, Arc::new(t));
|
||||
// Continue through free ids without rescanning the table map for its maximum.
|
||||
// On exhaustion wrap to gaps; an allocated map cannot occupy every u64 id.
|
||||
next = next.checked_add(1).unwrap_or(1);
|
||||
while self.tables.contains_key(&next) {
|
||||
next = next.checked_add(1).unwrap_or(1);
|
||||
}
|
||||
}
|
||||
for p in &mut other.paras {
|
||||
p.table = p.table.and_then(|id| ids.get(&id).copied());
|
||||
}
|
||||
}
|
||||
self.text.push('\n');
|
||||
self.text.push_str(&other.text);
|
||||
// The paragraph end takes the last run's format.
|
||||
if let Some(last) = self.chars.last_mut() {
|
||||
last.len += 1;
|
||||
}
|
||||
self.chars.extend(other.chars.into_iter().filter(|r| r.len > 0));
|
||||
self.paras.extend(other.paras);
|
||||
for n in other.notes {
|
||||
let id = self.next_note_id();
|
||||
self.notes.push(Arc::new(crate::notes::Footnote { id, text: n.text.clone() }));
|
||||
}
|
||||
for n in other.endnotes {
|
||||
let id = crate::ids::next_local_id(self.endnotes.iter().map(|x| x.id));
|
||||
self.endnotes.push(Arc::new(crate::notes::Footnote { id, text: n.text.clone() }));
|
||||
}
|
||||
for n in other.editorial {
|
||||
let id = crate::ids::next_local_id(self.editorial.iter().map(|x| x.id));
|
||||
self.editorial.push(Arc::new(crate::endnotes::EditorialNote { id, ..(*n).clone() }));
|
||||
}
|
||||
self.anchors.extend(other.anchors);
|
||||
self.xrefs.extend(other.xrefs);
|
||||
self.index_refs.extend(other.index_refs);
|
||||
self.objects.extend(other.objects);
|
||||
self.rev = self.rev.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -159,4 +219,52 @@ mod tests {
|
||||
assert_eq!(part.notes.len(), 1);
|
||||
assert!(a.extract(2..8).notes.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_tables_preserves_sparse_and_wrapping_ids() {
|
||||
for existing in [vec![1, 3, u64::MAX], vec![1, 3, u64::MAX - 1], vec![1, 3, 8]] {
|
||||
let story = |id, ids: &[u64]| {
|
||||
let mut s = Story::with_text(StoryId(id), &vec![crate::TABLE_ANCHOR.to_string(); ids.len()].join("\n"), ParaFormat::default());
|
||||
for (p, &tid) in s.paras.iter_mut().zip(ids) {
|
||||
p.table = Some(tid);
|
||||
let mut t = crate::table::Table::new(tid, 1, 1, 0, 0, 100.0);
|
||||
t.cells[0].text = Story::with_text(StoryId(0), &format!("{id}:{tid}"), ParaFormat::default());
|
||||
s.tables.insert(tid, Arc::new(t));
|
||||
}
|
||||
s
|
||||
};
|
||||
let mut a = story(1, &existing);
|
||||
let b = story(2, &[1, 2, 3]);
|
||||
a.check().unwrap();
|
||||
b.check().unwrap();
|
||||
a.append_story(b);
|
||||
a.check().unwrap();
|
||||
assert_eq!(a.tables.len(), 6);
|
||||
for (p, tid) in a.paras.iter().take(3).zip(&existing) {
|
||||
assert_eq!(p.table, Some(*tid));
|
||||
assert_eq!(a.tables[tid].cells[0].text.text, format!("1:{tid}"));
|
||||
}
|
||||
for (p, tid) in a.paras.iter().skip(3).zip(1..=3) {
|
||||
assert_eq!(a.tables[&p.table.unwrap()].cells[0].text.text, format!("2:{tid}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn append_story_with_maximal_ids_does_not_overflow() {
|
||||
let mut a = Story::with_text(StoryId(1), "Alpha", ParaFormat::default());
|
||||
a.tables.insert(u64::MAX, Arc::new(crate::table::Table::new(u64::MAX, 1, 1, 0, 0, 100.0)));
|
||||
let note = |id| Arc::new(crate::notes::Footnote { id, text: Story::with_text(StoryId(0), "n", ParaFormat::default()) });
|
||||
a.endnotes.push(note(u64::MAX));
|
||||
a.notes.push(note(u64::MAX));
|
||||
a.rev = u64::MAX;
|
||||
let mut b = Story::with_text(StoryId(2), "Beta", ParaFormat::default());
|
||||
b.tables.insert(1, Arc::new(crate::table::Table::new(1, 1, 1, 0, 0, 100.0)));
|
||||
b.endnotes.push(note(1));
|
||||
b.notes.push(note(1));
|
||||
a.append_story(b);
|
||||
assert_eq!(a.text, "Alpha\nBeta");
|
||||
assert_eq!(a.endnotes.len(), 2);
|
||||
assert_eq!(a.notes.len(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -692,8 +692,8 @@ impl Styles {
|
||||
self.object.iter().find(|s| s.name == name)
|
||||
}
|
||||
|
||||
/// The paragraph style chain from root to `name` (cycle-safe).
|
||||
fn para_chain(&self, name: &str) -> Vec<&ParagraphStyle> {
|
||||
/// The paragraph style chain from root to `name` (cycle-safe): resolution starts at its first.
|
||||
pub fn para_chain(&self, name: &str) -> Vec<&ParagraphStyle> {
|
||||
let mut chain = Vec::new();
|
||||
let mut cur = self.para(name);
|
||||
while let Some(s) = cur {
|
||||
|
||||
+210
-3
@@ -444,6 +444,25 @@ impl Table {
|
||||
v
|
||||
}
|
||||
|
||||
/// Owning cells touched by the range, once each in row-major order. Bounds are clipped
|
||||
/// to the grid; resolving covered positions does not expand the selection rectangle.
|
||||
pub fn selected_owners(&self, range: CellRange) -> Vec<(usize, usize)> {
|
||||
let (nr, nc) = (self.nrows(), self.ncols());
|
||||
if range.r0 >= nr || range.c0 >= nc || range.r0 > range.r1 || range.c0 > range.c1 {
|
||||
return Vec::new();
|
||||
}
|
||||
let owners = self.owners();
|
||||
let mut selected = std::collections::BTreeSet::new();
|
||||
for r in range.r0..=range.r1.min(nr - 1) {
|
||||
for c in range.c0..=range.c1.min(nc - 1) {
|
||||
if let Some(&owner) = owners.get(r * nc + c) {
|
||||
selected.insert(owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
selected.into_iter().collect()
|
||||
}
|
||||
|
||||
/// The owning (top-left) cell address of grid position (r, c).
|
||||
pub fn owner(&self, r: usize, c: usize) -> (usize, usize) {
|
||||
self.owners().get(r * self.ncols() + c).copied().unwrap_or((r, c))
|
||||
@@ -695,6 +714,79 @@ impl Table {
|
||||
self.set_regions(®ions);
|
||||
}
|
||||
|
||||
/// Split each original owner touched by `range` once. Remember merged-cell cuts in the
|
||||
/// original grid: a neighboring split may refine that grid before we reach the owner.
|
||||
pub fn split_cells(&mut self, range: CellRange, horizontal: bool) -> Result<(), String> {
|
||||
if range.r0 > range.r1 || range.c0 > range.c1 || range.r1 >= self.nrows() || range.c1 >= self.ncols() {
|
||||
return Err("cell range out of bounds".into());
|
||||
}
|
||||
let owners = self.owners();
|
||||
let mut selected = std::collections::BTreeSet::new();
|
||||
for r in range.r0..=range.r1 {
|
||||
for c in range.c0..=range.c1 {
|
||||
let owner = owners.get(r * self.ncols() + c).ok_or("missing cell owner")?;
|
||||
selected.insert(*owner);
|
||||
}
|
||||
}
|
||||
let mut targets = Vec::new();
|
||||
for (r, c) in selected {
|
||||
let cell = self.cell(r, c).ok_or("missing split cell")?;
|
||||
let span = if horizontal { cell.row_span } else { cell.col_span } as usize;
|
||||
let start = if horizontal { r } else { c };
|
||||
let cut = if span > 1 { Some(start.checked_add(span.div_ceil(2)).ok_or("invalid split boundary")?) } else { None };
|
||||
targets.push((r, c, cut));
|
||||
}
|
||||
for i in 0..targets.len() {
|
||||
let (r, c, cut) = *targets.get(i).ok_or("missing split target")?;
|
||||
let cell = self.cell(r, c).ok_or("missing split cell")?;
|
||||
let (rs, cs) = (cell.row_span as usize, cell.col_span as usize);
|
||||
let mut blank = cell.blank_like();
|
||||
// Splitting creates an empty half, not a duplicate of graphic-cell content.
|
||||
blank.graphic = None;
|
||||
let before = if horizontal { self.nrows() } else { self.ncols() };
|
||||
if let Some(cut) = cut {
|
||||
let (start, span) = if horizontal { (r, rs) } else { (c, cs) };
|
||||
let first = cut.checked_sub(start).filter(|n| *n > 0 && *n < span).ok_or("invalid split boundary")?;
|
||||
let mut regions: Vec<_> = self.regions().into_iter().filter(|&(rr, cc, _, _)| (rr, cc) != (r, c)).collect();
|
||||
if horizontal {
|
||||
regions.push((r, c, first, cs));
|
||||
regions.push((cut, c, rs - first, cs));
|
||||
} else {
|
||||
regions.push((r, c, rs, first));
|
||||
regions.push((r, cut, rs, cs - first));
|
||||
}
|
||||
self.set_regions(®ions);
|
||||
} else {
|
||||
// An originally single-track cell may now span two tracks inserted for its
|
||||
// neighbor. split_cell divides that span instead of inserting another track.
|
||||
self.split_cell(r, c, horizontal);
|
||||
}
|
||||
let first = self.cell(r, c).ok_or("missing split half")?;
|
||||
let second = if horizontal { (r + first.row_span as usize, c) } else { (r, c + first.col_span as usize) };
|
||||
let second = self.cell_mut(second.0, second.1).ok_or("missing split half")?;
|
||||
blank.row_span = second.row_span;
|
||||
blank.col_span = second.col_span;
|
||||
*second = blank;
|
||||
let after = if horizontal { self.nrows() } else { self.ncols() };
|
||||
if after > before {
|
||||
let at = if horizontal { r + 1 } else { c + 1 };
|
||||
let added = after - before;
|
||||
for (row, col, cut) in targets.iter_mut().skip(i + 1) {
|
||||
let axis = if horizontal { row } else { col };
|
||||
if *axis >= at {
|
||||
*axis += added;
|
||||
}
|
||||
if let Some(cut) = cut
|
||||
&& *cut >= at
|
||||
{
|
||||
*cut += added;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Split the cell covering (r, c) in two: `horizontal` stacks the halves (Split Cell
|
||||
/// Horizontally), otherwise side by side. A merged cell splits its span; a single cell gets a
|
||||
/// new row (column) that the cells beside it span, the row height (column width) shared.
|
||||
@@ -719,10 +811,11 @@ impl Table {
|
||||
return;
|
||||
}
|
||||
if horizontal {
|
||||
let h = self.rows[or].height;
|
||||
let row = self.rows[or].clone();
|
||||
self.insert_rows(or + 1, 1);
|
||||
self.rows[or].height = h / 2.0;
|
||||
self.rows[or + 1].height = h / 2.0;
|
||||
self.rows[or].height = row.height / 2.0;
|
||||
// Both halves keep the split row's height mode and header/body/footer role.
|
||||
self.rows[or + 1] = Row { height: row.height / 2.0, ..row };
|
||||
// insert_rows doesn't grow regions that end at the split row; everything else on it
|
||||
// spans the new row now.
|
||||
let mut regions: Vec<_> = self.regions();
|
||||
@@ -1062,6 +1155,35 @@ mod tests {
|
||||
Story::with_text(StoryId(1), t, ParaFormat::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_owners_include_covered_cells_without_expanding_range() {
|
||||
for column in [true, false] {
|
||||
let mut t = Table::new(1, 2, 2, 0, 0, 120.0);
|
||||
let range = if column { CellRange::new(0, 1, 1, 1) } else { CellRange::new(1, 0, 1, 1) };
|
||||
assert_eq!(t.selected_owners(range), if column { vec![(0, 1), (1, 1)] } else { vec![(1, 0), (1, 1)] });
|
||||
t.merge(if column { CellRange::new(0, 0, 0, 1) } else { CellRange::new(0, 0, 1, 0) }).unwrap();
|
||||
assert_eq!(t.selected_owners(range), vec![(0, 0), (1, 1)]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_owners_are_bounded_sorted_and_unique() {
|
||||
let mut t = Table::new(1, 3, 3, 0, 0, 180.0);
|
||||
t.merge(CellRange::new(0, 0, 1, 1)).unwrap();
|
||||
assert_eq!(t.selected_owners(CellRange::new(1, 1, usize::MAX, usize::MAX)), vec![(0, 0), (1, 2), (2, 1), (2, 2)]);
|
||||
assert_eq!(t.selected_owners(CellRange::new(0, 0, 1, 1)), vec![(0, 0)], "covered positions do not duplicate an owner");
|
||||
assert!(t.selected_owners(CellRange::new(3, 0, usize::MAX, usize::MAX)).is_empty());
|
||||
assert!(t.selected_owners(CellRange::new(0, 3, usize::MAX, usize::MAX)).is_empty());
|
||||
assert!(t.selected_owners(CellRange { r0: 1, c0: 0, r1: 0, c1: 1 }).is_empty());
|
||||
t.rows.clear();
|
||||
t.cells.clear();
|
||||
assert!(t.selected_owners(CellRange::cell(0, 0)).is_empty());
|
||||
|
||||
let mut right = Table::new(2, 2, 2, 0, 0, 120.0);
|
||||
right.merge(CellRange::new(0, 1, 1, 1)).unwrap();
|
||||
assert_eq!(right.selected_owners(CellRange::new(1, 0, 1, 1)), vec![(0, 1), (1, 0)], "owner order, not encounter order");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_table_gets_own_paragraph() {
|
||||
let mut s = story("before after");
|
||||
@@ -1255,4 +1377,89 @@ mod split_tests {
|
||||
assert_eq!(m.nrows(), 4);
|
||||
assert_eq!((m.cell(0, 0).unwrap().row_span, m.cell(2, 0).unwrap().row_span), (2, 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_range_keeps_original_merged_cut_when_neighbor_tracks_are_refined() {
|
||||
for horizontal in [true, false] {
|
||||
let mut t = Table::new(1, 4, 4, 0, 0, 280.0);
|
||||
for (i, size) in [40.0, 60.0, 80.0, 100.0].into_iter().enumerate() {
|
||||
t.rows[i].mode = RowHeightMode::Exactly;
|
||||
t.rows[i].height = if horizontal { size } else { 70.0 };
|
||||
t.columns[i].width = if horizontal { 70.0 } else { size };
|
||||
}
|
||||
for r in 0..4 {
|
||||
for c in 0..4 {
|
||||
let cell = t.cell_mut(r, c).unwrap();
|
||||
cell.text.insert(0, &format!("{r},{c}"));
|
||||
cell.style = format!("style-{r}-{c}");
|
||||
cell.insets = [r as f64 + c as f64 + 1.0; 4];
|
||||
}
|
||||
}
|
||||
let (merged, selected, owner, lower, neighbor, moved_neighbor) = if horizontal {
|
||||
(CellRange::new(0, 1, 2, 1), CellRange::new(0, 0, 2, 1), (0, 1), (4, 1), (3, 3), (6, 3))
|
||||
} else {
|
||||
(CellRange::new(1, 0, 1, 2), CellRange::new(0, 0, 1, 2), (1, 0), (1, 4), (3, 3), (3, 6))
|
||||
};
|
||||
t.merge(merged).unwrap();
|
||||
let original = t.clone();
|
||||
t.split_cells(selected, horizontal).unwrap();
|
||||
t.check().unwrap();
|
||||
assert_eq!((t.nrows(), t.ncols()), if horizontal { (7, 4) } else { (4, 7) });
|
||||
let sizes: Vec<_> = if horizontal { t.rows.iter().map(|r| r.height).collect() } else { t.columns.iter().map(|c| c.width).collect() };
|
||||
assert_eq!(sizes, [20.0, 20.0, 30.0, 30.0, 40.0, 40.0, 100.0]);
|
||||
let first = t.cell(owner.0, owner.1).unwrap();
|
||||
let second = t.cell(lower.0, lower.1).unwrap();
|
||||
// The original three-track merge divides after its second original track,
|
||||
// regardless of which of those tracks a neighboring selected cell subdivides.
|
||||
assert_eq!(if horizontal { first.row_span } else { first.col_span }, 4);
|
||||
assert_eq!(if horizontal { second.row_span } else { second.col_span }, 2);
|
||||
assert_eq!(first.text, original.cell(owner.0, owner.1).unwrap().text);
|
||||
assert!(second.text.is_empty());
|
||||
assert_eq!(second.style, first.style);
|
||||
assert_eq!(second.insets, first.insets);
|
||||
assert_eq!(t.cell(moved_neighbor.0, moved_neighbor.1), original.cell(neighbor.0, neighbor.1));
|
||||
let owners = t.owners();
|
||||
assert_eq!(owners.iter().copied().collect::<std::collections::BTreeSet<_>>().len(), 18, "four original selected owners split once each");
|
||||
let mut before_text: Vec<_> = original.cells.iter().filter(|c| !c.text.is_empty()).map(|c| c.text.text.clone()).collect();
|
||||
let mut after_text: Vec<_> = t.cells.iter().filter(|c| !c.text.is_empty()).map(|c| c.text.text.clone()).collect();
|
||||
before_text.sort();
|
||||
after_text.sort();
|
||||
assert_eq!(after_text, before_text, "no original text is copied or lost");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_range_resolves_covered_positions_without_expanding_selection() {
|
||||
for horizontal in [true, false] {
|
||||
let mut t = Table::new(1, 3, 3, 0, 0, 180.0);
|
||||
t.merge(CellRange::new(0, 0, 1, 1)).unwrap();
|
||||
let selected = if horizontal { CellRange::new(1, 1, 1, 2) } else { CellRange::new(1, 1, 2, 1) };
|
||||
t.split_cells(selected, horizontal).unwrap();
|
||||
t.check().unwrap();
|
||||
let owners = t.owners();
|
||||
assert_eq!(owners.iter().copied().collect::<std::collections::BTreeSet<_>>().len(), 8, "only the two touched owners split");
|
||||
assert_eq!((t.nrows(), t.ncols()), if horizontal { (4, 3) } else { (3, 4) });
|
||||
assert_eq!(t.owner(0, 0), (0, 0));
|
||||
assert_eq!(if horizontal { t.owner(1, 0) } else { t.owner(0, 1) }, if horizontal { (1, 0) } else { (0, 1) });
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_header_row_keeps_its_height_mode_and_role() {
|
||||
let mut t = Table::new(1, 1, 2, 1, 1, 180.0);
|
||||
t.rows[0].height = 60.0;
|
||||
t.rows[0].mode = RowHeightMode::Exactly;
|
||||
let body = t.rows[1].clone();
|
||||
let footer = t.rows[2].clone();
|
||||
t.split_cells(CellRange::new(0, 0, 0, 1), true).unwrap();
|
||||
t.check().unwrap();
|
||||
assert_eq!(t.header_rows(), 2);
|
||||
for row in &t.rows[..2] {
|
||||
assert_eq!(row.height, 30.0);
|
||||
assert_eq!(row.mode, RowHeightMode::Exactly);
|
||||
assert_eq!(row.kind, RowKind::Header);
|
||||
}
|
||||
assert_eq!(t.rows[2], body);
|
||||
assert_eq!(t.rows[3], footer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,126 @@ fn thread_to_empty_frame_converts_it() {
|
||||
assert!(d.thread(a, a).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn threading_merges_tables_notes_and_endnotes() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let (a, sa) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 100.0, 100.0), lid, "first", ParaFormat::default()).unwrap();
|
||||
let (b, sb) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "second", ParaFormat::default()).unwrap();
|
||||
for (sid, note) in [(sa, "Note A"), (sb, "Note B")] {
|
||||
let st = d.story_mut(sid).unwrap();
|
||||
st.insert_note(3, note, ParaFormat::default());
|
||||
// Both stories hold a table with id 1.
|
||||
let n = st.len();
|
||||
st.insert_table(n, crate::table::Table::new(1, 2, 2, 0, 0, 80.0));
|
||||
}
|
||||
let st = d.story_mut(sb).unwrap();
|
||||
st.insert_endnote(2, "End B", ParaFormat::default());
|
||||
st.insert(1, &crate::NOTE_MARK.to_string());
|
||||
d.thread(a, b).unwrap();
|
||||
d.check().unwrap();
|
||||
let st = d.story(sa).unwrap();
|
||||
assert!(d.story(sb).is_none());
|
||||
assert_eq!(st.tables.len(), 2);
|
||||
assert_eq!(st.notes.len(), 2);
|
||||
assert_eq!(st.notes[1].text.text, "Note B");
|
||||
assert_eq!(st.endnotes.len(), 1);
|
||||
assert_eq!(st.endnotes[0].text.text, "End B");
|
||||
assert_eq!(st.editorial.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_frame_threaded_before_a_story_joins_it_at_the_front() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let (a, sa) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 100.0, 100.0), lid, "", ParaFormat::default()).unwrap();
|
||||
let (b, sb) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "Hello", ParaFormat::default()).unwrap();
|
||||
d.thread_keeping_story(a, b).unwrap();
|
||||
d.check().unwrap();
|
||||
assert!(d.story(sa).is_none());
|
||||
let st = d.story(sb).unwrap();
|
||||
assert_eq!(st.frames, vec![a, b]);
|
||||
assert_eq!(st.text, "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_story_takes_the_threaded_text_without_a_blank_paragraph() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let (a, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 100.0, 100.0), lid, "", ParaFormat::default()).unwrap();
|
||||
let (a2, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 110.0, 100.0, 200.0), lid, "", ParaFormat::default()).unwrap();
|
||||
d.thread(a, a2).unwrap();
|
||||
let (b, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "Hello", ParaFormat::default()).unwrap();
|
||||
d.thread(a2, b).unwrap();
|
||||
d.check().unwrap();
|
||||
let st = d.story(d.item(a).unwrap().text_frame().unwrap().story).unwrap();
|
||||
assert_eq!(st.frames, vec![a, a2, b]);
|
||||
assert_eq!(st.text, "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn threading_from_an_empty_frame_makes_it_a_text_frame() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let id = ItemId(d.alloc());
|
||||
let it = Item::new(id, lid, Shape::Rectangle, designcraft_geom::shapes::rectangle(Rect::new(0.0, 200.0, 50.0, 300.0)));
|
||||
d.insert_item(SpreadRef::Doc(0), it, None).unwrap();
|
||||
let (b, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "Hello", ParaFormat::default()).unwrap();
|
||||
d.thread(id, b).unwrap();
|
||||
d.check().unwrap();
|
||||
let st = d.story(d.item(id).unwrap().text_frame().unwrap().story).unwrap();
|
||||
assert_eq!(st.frames, vec![id, b]);
|
||||
assert_eq!(st.text, "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_frame_from_its_thread_keeps_the_text_in_the_others() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let (a, sa) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 100.0, 100.0), lid, "words", ParaFormat::default()).unwrap();
|
||||
let (b, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "", ParaFormat::default()).unwrap();
|
||||
let (c, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(210.0, 10.0, 300.0, 100.0), lid, "", ParaFormat::default()).unwrap();
|
||||
d.thread(a, b).unwrap();
|
||||
d.thread(b, c).unwrap();
|
||||
d.remove_from_thread(b).unwrap();
|
||||
d.check().unwrap();
|
||||
assert_eq!(d.story(sa).unwrap().frames, vec![a, c]);
|
||||
assert_eq!(d.story(sa).unwrap().text, "words");
|
||||
let own = d.item(b).unwrap().text_frame().unwrap().story;
|
||||
assert_ne!(own, sa);
|
||||
assert!(d.story(own).unwrap().text.is_empty());
|
||||
// A frame alone in its story has no thread to leave.
|
||||
assert!(d.remove_from_thread(b).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frame_removed_from_a_vertical_thread_stays_vertical() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let (a, sa) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 100.0, 100.0), lid, "縦書き", ParaFormat::default()).unwrap();
|
||||
let (b, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "", ParaFormat::default()).unwrap();
|
||||
d.story_mut(sa).unwrap().vertical = true;
|
||||
d.thread(a, b).unwrap();
|
||||
d.remove_from_thread(b).unwrap();
|
||||
d.check().unwrap();
|
||||
assert!(d.frame_vertical(d.item(a).unwrap()));
|
||||
assert!(d.frame_vertical(d.item(b).unwrap()), "the removed frame keeps the story's direction");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frame_removed_from_a_right_to_left_thread_keeps_its_column_order() {
|
||||
let mut d = doc();
|
||||
let lid = d.default_layer();
|
||||
let (a, sa) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 100.0, 100.0), lid, "نص", ParaFormat::default()).unwrap();
|
||||
let (b, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(110.0, 10.0, 200.0, 100.0), lid, "", ParaFormat::default()).unwrap();
|
||||
d.story_mut(sa).unwrap().direction = crate::TextDirection::RightToLeft;
|
||||
d.thread(a, b).unwrap();
|
||||
d.remove_from_thread(b).unwrap();
|
||||
d.check().unwrap();
|
||||
let own = d.item(b).unwrap().text_frame().unwrap().story;
|
||||
assert_eq!(d.story(own).unwrap().direction, crate::TextDirection::RightToLeft);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frames_keep_their_story_direction_through_threading() {
|
||||
let mut d = doc();
|
||||
|
||||
@@ -9,6 +9,7 @@ description = "DesignCraft engine: session, documents, history, selection, comma
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-print = { workspace = true }
|
||||
designcraft-images = { workspace = true }
|
||||
designcraft-geom = { workspace = true }
|
||||
designcraft-color = { workspace = true }
|
||||
@@ -29,6 +30,7 @@ log = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
caseless = { workspace = true }
|
||||
unicode-segmentation = { workspace = true }
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
tempfile = { workspace = true }
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
//! Crash-safe file replacement: how documents, exports and other user files reach the disk.
|
||||
//!
|
||||
//! [`write_atomic`] never writes the destination in place. It writes a uniquely named temporary
|
||||
//! file in the same folder, flushes it (`sync_all`), renames it over the destination and then
|
||||
//! flushes the folder (Unix) so the rename survives a power loss. When any step before the rename
|
||||
//! fails, the temporary file is removed and the destination keeps its old bytes.
|
||||
//!
|
||||
//! - A replaced file keeps its permissions (Unix mode).
|
||||
//! - A symbolic link keeps pointing at its file: the file it names is replaced.
|
||||
//! - Windows: antivirus scanners and the search indexer briefly hold files open, which makes the
|
||||
//! replacing rename fail with `PermissionDenied`; the rename is retried with a short backoff.
|
||||
//! - A failed rename (cross-device, unwritable folder) is returned as an error; there is no
|
||||
//! fallback to writing in place.
|
||||
//! - Flushing the parent folder after replacement is best effort. A successful save confirms
|
||||
//! that the new file is in place; it cannot guarantee survival of a subsequent power loss if
|
||||
//! the file system rejects that folder flush.
|
||||
//!
|
||||
//! The web has no file system, so this module is not built for `wasm32`.
|
||||
|
||||
use std::fs::File;
|
||||
#[cfg(unix)]
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{self, Write as _};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// How many times a replacing rename is tried before its error is returned.
|
||||
const RENAME_TRIES: u32 = if cfg!(windows) { 5 } else { 1 };
|
||||
/// The pause before the first rename retry; it doubles up to [`RENAME_MAX_DELAY_MS`].
|
||||
const RENAME_FIRST_DELAY_MS: u64 = 10;
|
||||
const RENAME_MAX_DELAY_MS: u64 = 200;
|
||||
/// Symbolic links followed before giving up (a loop, or an absurd chain).
|
||||
const MAX_LINKS: usize = 40;
|
||||
|
||||
/// Atomically replace (or create) `path` with `bytes`. See the module docs.
|
||||
pub fn write_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> {
|
||||
write_atomic_with(path, |f| f.write_all(bytes))
|
||||
}
|
||||
|
||||
/// [`write_atomic`] for a command's `path` parameter: the error names the path.
|
||||
pub(crate) fn write_file(path: &str, bytes: &[u8]) -> crate::Result<()> {
|
||||
write_atomic(Path::new(path), bytes).map_err(|e| crate::EngineError::Other(format!("{path}: {e}")))
|
||||
}
|
||||
|
||||
/// Atomically replace (or create) `path` with what `fill` writes into the temporary file. When
|
||||
/// `fill`, the flush or the rename fails, the temporary file is removed, `path` is untouched and
|
||||
/// the error is returned.
|
||||
pub fn write_atomic_with(path: &Path, fill: impl FnOnce(&mut File) -> io::Result<()>) -> io::Result<()> {
|
||||
let target = resolve_links(path)?;
|
||||
target.file_name().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, format!("{}: not a file path", path.display())))?;
|
||||
let dir = match target.parent() {
|
||||
Some(d) if !d.as_os_str().is_empty() => d.to_path_buf(),
|
||||
_ => PathBuf::from("."),
|
||||
};
|
||||
let existing = std::fs::metadata(&target).ok();
|
||||
if existing.is_some() {
|
||||
check_writable(&target)?;
|
||||
}
|
||||
let mut tmp = create_temp(&dir, existing.as_ref())?;
|
||||
if let Some(existing) = existing {
|
||||
// Restore the exact mode before any document bytes are written, and flush it along with
|
||||
// the content. The caller's umask may have restricted the initial temporary-file mode.
|
||||
keep_permissions(tmp.as_file(), &existing)?;
|
||||
}
|
||||
fill(tmp.as_file_mut())?;
|
||||
tmp.as_file().sync_all()?;
|
||||
// Closing the file before renaming also allows replacement on Windows. TempPath removes the
|
||||
// staging file on every error; NamedTempFile does the same if the writer unwinds.
|
||||
let tmp = tmp.into_temp_path();
|
||||
retry_rename(RENAME_TRIES, || std::fs::rename(&tmp, &target))?;
|
||||
// The new bytes are in place. A failed folder flush can't be undone and must not report the
|
||||
// save as failed.
|
||||
let _ = sync_dir(&dir);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create a fresh temporary file beside the target (the same folder, so the rename stays on one
|
||||
/// volume). A short randomized name supports long destination names and skips existing entries.
|
||||
fn create_temp(dir: &Path, existing: Option<&std::fs::Metadata>) -> io::Result<tempfile::NamedTempFile> {
|
||||
let mut builder = tempfile::Builder::new();
|
||||
builder.prefix(".designcraft-save-").suffix(".tmp");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
// Private replacements are private from creation. New files retain fs::write's normal
|
||||
// 0666 mode restricted by the caller's umask.
|
||||
let mode = existing.map_or(0o666, |metadata| metadata.permissions().mode());
|
||||
builder.permissions(std::fs::Permissions::from_mode(mode));
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = existing;
|
||||
builder.tempfile_in(dir)
|
||||
}
|
||||
|
||||
/// The file a chain of symbolic links ends at (`path` itself when it is not a link). A dangling
|
||||
/// link resolves to the missing file it names, which is then created.
|
||||
fn resolve_links(path: &Path) -> io::Result<PathBuf> {
|
||||
let mut p = path.to_path_buf();
|
||||
for _ in 0..MAX_LINKS {
|
||||
match std::fs::symlink_metadata(&p) {
|
||||
Ok(m) if m.file_type().is_symlink() => {
|
||||
let to = std::fs::read_link(&p)?;
|
||||
p = match p.parent() {
|
||||
Some(dir) if to.is_relative() => dir.join(to),
|
||||
_ => to,
|
||||
};
|
||||
}
|
||||
_ => return Ok(p),
|
||||
}
|
||||
}
|
||||
Err(io::Error::new(io::ErrorKind::InvalidInput, format!("{}: too many levels of symbolic links", path.display())))
|
||||
}
|
||||
|
||||
/// A rename replaces a file the user can't write (Unix only needs the folder to be writable);
|
||||
/// refuse, as writing it in place would. Opening for writing without truncating changes nothing.
|
||||
#[cfg(unix)]
|
||||
fn check_writable(target: &Path) -> io::Result<()> {
|
||||
OpenOptions::new().write(true).open(target).map(drop)
|
||||
}
|
||||
|
||||
/// Windows refuses to rename over a read-only file by itself.
|
||||
#[cfg(not(unix))]
|
||||
fn check_writable(_: &Path) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The replacement keeps the replaced file's mode (Unix; Windows files keep only a read-only flag,
|
||||
/// and a read-only file can't be replaced).
|
||||
#[cfg(unix)]
|
||||
fn keep_permissions(tmp: &File, replaced: &std::fs::Metadata) -> io::Result<()> {
|
||||
tmp.set_permissions(replaced.permissions())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn keep_permissions(_: &File, _: &std::fs::Metadata) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run `rename` up to `tries` times, pausing with a doubling delay while it fails with
|
||||
/// `PermissionDenied` (a scanner holding the file on Windows). Other errors return at once.
|
||||
fn retry_rename(tries: u32, mut rename: impl FnMut() -> io::Result<()>) -> io::Result<()> {
|
||||
let mut delay = RENAME_FIRST_DELAY_MS;
|
||||
let mut left = tries.max(1);
|
||||
loop {
|
||||
left = left.saturating_sub(1);
|
||||
match rename() {
|
||||
Err(e) if e.kind() == io::ErrorKind::PermissionDenied && left > 0 => {
|
||||
std::thread::sleep(std::time::Duration::from_millis(delay));
|
||||
delay = delay.saturating_mul(2).min(RENAME_MAX_DELAY_MS);
|
||||
}
|
||||
r => return r,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Flush a folder's entries (Unix). Rust's portable file API cannot open a directory on Windows;
|
||||
/// this does not establish power-loss durability for its directory entries.
|
||||
#[cfg(unix)]
|
||||
fn sync_dir(dir: &Path) -> io::Result<()> {
|
||||
File::open(dir)?.sync_all()
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn sync_dir(_: &Path) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn dir(name: &str) -> PathBuf {
|
||||
let d = std::env::temp_dir().join(format!("dc-atomic-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
/// The names in `d`, sorted.
|
||||
fn names(d: &Path) -> Vec<String> {
|
||||
let mut v: Vec<String> = std::fs::read_dir(d).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect();
|
||||
v.sort();
|
||||
v
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replaces_and_creates_without_leftovers() {
|
||||
let d = dir("replace");
|
||||
let path = d.join("doc.designcraft");
|
||||
write_atomic(&path, b"first").unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"first");
|
||||
write_atomic(&path, b"second, longer").unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"second, longer");
|
||||
write_atomic(&path, b"3").unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"3");
|
||||
assert_eq!(names(&d), ["doc.designcraft"], "no temporary file is left");
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_write_leaves_the_original_and_no_temporary_file() {
|
||||
let d = dir("fail");
|
||||
let path = d.join("doc.designcraft");
|
||||
std::fs::write(&path, b"original").unwrap();
|
||||
let r = write_atomic_with(&path, |f| {
|
||||
f.write_all(b"half a docu")?;
|
||||
Err(io::Error::other("disk full"))
|
||||
});
|
||||
assert_eq!(r.unwrap_err().to_string(), "disk full");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"original");
|
||||
assert_eq!(names(&d), ["doc.designcraft"], "the temporary file is removed");
|
||||
// A folder that doesn't exist: an error, nothing written.
|
||||
assert!(write_atomic(&d.join("missing").join("x.designcraft"), b"x").is_err());
|
||||
assert_eq!(names(&d), ["doc.designcraft"]);
|
||||
// A folder in the way: an error, the folder stays and nothing is left beside it.
|
||||
std::fs::create_dir(d.join("folder")).unwrap();
|
||||
assert!(write_atomic(&d.join("folder"), b"x").is_err());
|
||||
assert_eq!(names(&d), ["doc.designcraft", "folder"]);
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_destination_name_does_not_lengthen_the_temporary_name() {
|
||||
let d = dir("long-name");
|
||||
let name = format!("{}.designcraft", "a".repeat(235));
|
||||
let path = d.join(&name);
|
||||
std::fs::write(&path, b"old").unwrap();
|
||||
write_atomic(&path, b"new").unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"new");
|
||||
assert_eq!(names(&d), [name]);
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn private_permissions_are_preserved_while_the_file_is_written() {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
let d = dir("private-staging");
|
||||
let path = d.join("private.designcraft");
|
||||
std::fs::write(&path, b"old").unwrap();
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
write_atomic_with(&path, |file| {
|
||||
assert_eq!(file.metadata()?.permissions().mode() & 0o777, 0o600);
|
||||
file.write_all(b"private document")?;
|
||||
assert_eq!(file.metadata()?.permissions().mode() & 0o777, 0o600);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"private document");
|
||||
assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
assert_eq!(names(&d), ["private.designcraft"]);
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unwinding_writer_removes_its_partial_temporary_file() {
|
||||
let d = dir("unwinding-writer");
|
||||
let path = d.join("private.designcraft");
|
||||
std::fs::write(&path, b"old").unwrap();
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
write_atomic_with(&path, |file| {
|
||||
file.write_all(b"partial private document")?;
|
||||
panic!("synthetic writer panic");
|
||||
})
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"old");
|
||||
assert_eq!(names(&d), ["private.designcraft"]);
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_rename_is_retried_only_while_access_is_denied() {
|
||||
let mut calls = 0;
|
||||
let r = retry_rename(3, || {
|
||||
calls += 1;
|
||||
if calls < 3 { Err(io::Error::from(io::ErrorKind::PermissionDenied)) } else { Ok(()) }
|
||||
});
|
||||
assert!(r.is_ok() && calls == 3);
|
||||
calls = 0;
|
||||
let r = retry_rename(3, || {
|
||||
calls += 1;
|
||||
Err(io::Error::from(io::ErrorKind::PermissionDenied))
|
||||
});
|
||||
assert_eq!(r.map_err(|e| e.kind()), Err(io::ErrorKind::PermissionDenied));
|
||||
assert_eq!(calls, 3, "gives up after the last try");
|
||||
calls = 0;
|
||||
let r = retry_rename(3, || {
|
||||
calls += 1;
|
||||
Err(io::Error::from(io::ErrorKind::CrossesDevices))
|
||||
});
|
||||
assert!(r.is_err() && calls == 1, "other errors are not retried");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn permissions_are_kept_and_unwritable_files_are_not_replaced() {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
let d = dir("perms");
|
||||
let path = d.join("private.designcraft");
|
||||
std::fs::write(&path, b"old").unwrap();
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
write_atomic(&path, b"new").unwrap();
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"new");
|
||||
assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
// A read-only file stays as it is (root may write it anyway).
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o400)).unwrap();
|
||||
if OpenOptions::new().write(true).open(&path).is_err() {
|
||||
assert!(write_atomic(&path, b"newer").is_err());
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"new");
|
||||
assert_eq!(names(&d), ["private.designcraft"]);
|
||||
}
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
/// A folder the user can't write: the save fails and the original stays.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn an_unwritable_folder_fails_without_touching_the_file() {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
let d = dir("ro-dir");
|
||||
let path = d.join("doc.designcraft");
|
||||
std::fs::write(&path, b"original").unwrap();
|
||||
std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o555)).unwrap();
|
||||
// Root writes anyway; the check only means something for an ordinary user.
|
||||
let probe = d.join("probe");
|
||||
let unwritable = std::fs::write(&probe, b"").is_err();
|
||||
if unwritable {
|
||||
assert!(write_atomic(&path, b"new").is_err());
|
||||
assert_eq!(std::fs::read(&path).unwrap(), b"original");
|
||||
}
|
||||
std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let _ = std::fs::remove_file(&probe);
|
||||
assert_eq!(names(&d), ["doc.designcraft"]);
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_symbolic_link_keeps_pointing_at_the_replaced_file() {
|
||||
let d = dir("link");
|
||||
std::fs::create_dir(d.join("real")).unwrap();
|
||||
let target = d.join("real").join("doc.designcraft");
|
||||
std::fs::write(&target, b"old").unwrap();
|
||||
let link = d.join("link.designcraft");
|
||||
std::os::unix::fs::symlink("real/doc.designcraft", &link).unwrap();
|
||||
write_atomic(&link, b"new").unwrap();
|
||||
assert!(std::fs::symlink_metadata(&link).unwrap().file_type().is_symlink());
|
||||
assert_eq!(std::fs::read(&target).unwrap(), b"new");
|
||||
assert_eq!(names(&d), ["link.designcraft", "real"]);
|
||||
assert_eq!(names(&d.join("real")), ["doc.designcraft"]);
|
||||
// A loop of links is an error, not a hang.
|
||||
std::os::unix::fs::symlink("b", d.join("a")).unwrap();
|
||||
std::os::unix::fs::symlink("a", d.join("b")).unwrap();
|
||||
assert!(write_atomic(&d.join("a"), b"x").is_err());
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
}
|
||||
+178
-56
@@ -28,7 +28,7 @@ fn no_book() -> EngineError {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn save(b: &Book) -> Result<()> {
|
||||
let bytes = serde_json::to_vec_pretty(b).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
std::fs::write(&b.path, bytes).map_err(|e| EngineError::Other(format!("{}: {e}", b.path)))
|
||||
crate::atomic::write_file(&b.path, &bytes)
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@@ -47,20 +47,176 @@ fn load(path: &str) -> Result<designcraft_doc::Document> {
|
||||
if path.to_lowercase().ends_with(".idml") {
|
||||
return designcraft_idml::import_idml(&bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")));
|
||||
}
|
||||
super::file::from_bytes(&bytes)
|
||||
super::file::from_bytes(&bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))
|
||||
}
|
||||
|
||||
fn store(path: &str, d: &designcraft_doc::Document) -> Result<()> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
return std::fs::write(path, super::file::to_bytes(d)).map_err(|e| EngineError::Other(format!("{path}: {e}")));
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let _ = (path, d);
|
||||
Err(EngineError::Other("books need a file system".into()))
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const MAX_BATCH_CHAPTERS: usize = 4096;
|
||||
|
||||
/// A batch keeps only bounded path records in memory. Chapter snapshots live in a private
|
||||
/// temporary directory; only one document and serialized blob are live while staging.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
struct ChapterBatch {
|
||||
directory: tempfile::TempDir,
|
||||
chapters: Vec<(String, tempfile::TempPath)>,
|
||||
command: &'static str,
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
impl ChapterBatch {
|
||||
fn new(chapters: usize, command: &'static str) -> Result<Self> {
|
||||
if chapters > MAX_BATCH_CHAPTERS {
|
||||
return Err(bad(command, format!("a book update supports at most {MAX_BATCH_CHAPTERS} chapters")));
|
||||
}
|
||||
let directory = tempfile::Builder::new()
|
||||
.prefix(".designcraft-book-")
|
||||
.tempdir()
|
||||
.map_err(|e| EngineError::Other(format!("{command}: cannot stage chapter changes: {e}")))?;
|
||||
Ok(Self { directory, chapters: Vec::with_capacity(chapters), command })
|
||||
}
|
||||
|
||||
fn stage(&mut self, path: &str, doc: &designcraft_doc::Document) -> Result<()> {
|
||||
use std::io::Write;
|
||||
|
||||
// Propagate serialization errors; an empty fallback must never replace a chapter.
|
||||
let bytes = designcraft_format::save(doc).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
let mut file = tempfile::Builder::new()
|
||||
.prefix("chapter-")
|
||||
.tempfile_in(self.directory.path())
|
||||
.map_err(|e| EngineError::Other(format!("{path}: cannot stage changes: {e}")))?;
|
||||
file.write_all(&bytes).map_err(|e| EngineError::Other(format!("{path}: cannot stage changes: {e}")))?;
|
||||
self.chapters.push((path.to_string(), file.into_temp_path()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn commit(self) -> Result<()> {
|
||||
self.commit_with(write_staged_chapter)
|
||||
}
|
||||
|
||||
fn commit_with(self, mut write: impl FnMut(&std::path::Path, &std::path::Path) -> std::io::Result<()>) -> Result<()> {
|
||||
let mut completed = Vec::with_capacity(self.chapters.len());
|
||||
for (path, staged) in self.chapters {
|
||||
if let Err(e) = write(std::path::Path::new(&path), &staged) {
|
||||
// Each file is atomic; the whole book is not. Keep the successful prefix visible
|
||||
// rather than pretending that an error undid earlier chapter replacements.
|
||||
return Err(EngineError::Other(format!(
|
||||
"{}: failed to save chapter {path}: {e}; completed chapter paths: {completed:?}",
|
||||
self.command
|
||||
)));
|
||||
}
|
||||
completed.push(path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn write_staged_chapter(path: &std::path::Path, staged: &std::path::Path) -> std::io::Result<()> {
|
||||
let mut input = std::fs::File::open(staged)?;
|
||||
crate::atomic::write_atomic_with(path, |file| std::io::copy(&mut input, file).map(|_| ()))
|
||||
}
|
||||
|
||||
/// Batch commands only write closed native chapters. IDML is an imported source, and an open
|
||||
/// chapter's model (including unsaved edits) cannot be reconciled by a disk-only book update.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn writable_chapter(s: &Session, path: &str, command: &'static str) -> Result<()> {
|
||||
use std::path::Path;
|
||||
|
||||
if Path::new(path).extension().and_then(|ext| ext.to_str()).is_some_and(|ext| ext.eq_ignore_ascii_case("idml")) {
|
||||
return Err(bad(command, format!("cannot change imported IDML chapter {path}; convert it to a native DesignCraft document first")));
|
||||
}
|
||||
let target = std::fs::canonicalize(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
if s.docs.iter().any(|doc| doc.path.as_ref().is_some_and(|open| std::fs::canonicalize(open).is_ok_and(|open| open == target))) {
|
||||
return Err(bad(command, format!("chapter {path} is open; close it before updating the book")));
|
||||
}
|
||||
if !std::fs::metadata(&target).is_ok_and(|metadata| metadata.is_file()) {
|
||||
return Err(bad(command, format!("chapter {path} is not a regular file")));
|
||||
}
|
||||
// Check both the file and its resolved parent without truncating it. Replacement needs a
|
||||
// writable directory even when an in-place write would be permitted.
|
||||
std::fs::OpenOptions::new().write(true).open(&target).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
let parent = target.parent().ok_or_else(|| bad(command, format!("chapter {path} has no parent folder")))?;
|
||||
tempfile::Builder::new().prefix(".designcraft-book-check-").tempfile_in(parent).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn paginate(s: &Session) -> Result<Value> {
|
||||
let b = s.book.as_ref().ok_or_else(no_book)?;
|
||||
if b.documents.is_empty() {
|
||||
return Ok(json!({"pages": 0}));
|
||||
}
|
||||
let mut batch = ChapterBatch::new(b.documents.len(), "book.paginate")?;
|
||||
let mut next = 1u32;
|
||||
for path in &b.documents {
|
||||
writable_chapter(s, path, "book.paginate")?;
|
||||
let mut d = load(path)?;
|
||||
start_at(&mut d, next);
|
||||
let pages = u32::try_from(d.page_count()).map_err(|_| bad("book.paginate", "too many pages for continuous numbering"))?;
|
||||
next = next.checked_add(pages).ok_or_else(|| bad("book.paginate", "too many pages for continuous numbering"))?;
|
||||
batch.stage(path, &d)?;
|
||||
}
|
||||
batch.commit()?;
|
||||
Ok(json!({"pages": next - 1}))
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn sync_styles(s: &Session) -> Result<Value> {
|
||||
let b = s.book.as_ref().ok_or_else(no_book)?;
|
||||
let mut batch = ChapterBatch::new(b.documents.len(), "book.syncStyles")?;
|
||||
let src_path = b.documents.get(b.style_source).ok_or_else(|| bad("book.syncStyles", "the book is empty"))?;
|
||||
let src = load(src_path)?;
|
||||
let styles = src.styles.clone();
|
||||
let swatches = src.swatches.clone();
|
||||
drop(src);
|
||||
let mut n = 0;
|
||||
for (i, path) in b.documents.iter().enumerate() {
|
||||
if i == b.style_source {
|
||||
continue;
|
||||
}
|
||||
writable_chapter(s, path, "book.syncStyles")?;
|
||||
let mut d = load(path)?;
|
||||
{
|
||||
let st = d.styles_mut();
|
||||
for ps in &styles.paragraph {
|
||||
st.paragraph.retain(|x| x.name != ps.name);
|
||||
st.paragraph.push(ps.clone());
|
||||
}
|
||||
for cs in &styles.character {
|
||||
st.character.retain(|x| x.name != cs.name);
|
||||
st.character.push(cs.clone());
|
||||
}
|
||||
}
|
||||
for sw in &swatches {
|
||||
match d.swatches.iter_mut().find(|w| w.name == sw.name) {
|
||||
Some(w) if !w.locked => *w = sw.clone(),
|
||||
Some(_) => {}
|
||||
None => d.swatches.push(sw.clone()),
|
||||
}
|
||||
}
|
||||
batch.stage(path, &d)?;
|
||||
n += 1;
|
||||
}
|
||||
batch.commit()?;
|
||||
Ok(json!({"synced": n}))
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn paginate(_: &Session) -> Result<Value> {
|
||||
Err(EngineError::Other("books need a file system".into()))
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn sync_styles(_: &Session) -> Result<Value> {
|
||||
Err(EngineError::Other("books need a file system".into()))
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
#[path = "book/safety_tests.rs"]
|
||||
mod safety_tests;
|
||||
|
||||
/// Make page numbering of `d` start at `n` (the first section).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn start_at(d: &mut designcraft_doc::Document, n: u32) {
|
||||
if !d.sections.iter().any(|x| x.start == 0) {
|
||||
d.sections.insert(
|
||||
@@ -108,15 +264,16 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(noundo "book.add", "Add Document", [], None, "{path, at?: index}", has_book, |s, p| {
|
||||
let path = str_param(p, "path").ok_or_else(|| bad("book.add", "`path` required"))?.to_string();
|
||||
load(&path)?;
|
||||
let b = s.book.as_mut().ok_or_else(no_book)?;
|
||||
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()));
|
||||
b.documents.insert(at, path);
|
||||
save(b)?;
|
||||
save(&b)?;
|
||||
s.book = Some(b);
|
||||
ok()
|
||||
}),
|
||||
cmd!(noundo "book.remove", "Remove Document", [], None, "{index}", has_book, |s, p| {
|
||||
let i = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad("book.remove", "`index` required"))? as usize;
|
||||
let b = s.book.as_mut().ok_or_else(no_book)?;
|
||||
let mut b = s.book.as_ref().ok_or_else(no_book)?.clone();
|
||||
if i >= b.documents.len() {
|
||||
return Err(bad("book.remove", format!("no document {i}")));
|
||||
}
|
||||
@@ -124,17 +281,19 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
if b.style_source >= b.documents.len() {
|
||||
b.style_source = 0;
|
||||
}
|
||||
save(b)?;
|
||||
save(&b)?;
|
||||
s.book = Some(b);
|
||||
ok()
|
||||
}),
|
||||
cmd!(noundo "book.styleSource", "Style Source", [], None, "{index}", has_book, |s, p| {
|
||||
let i = p.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
let b = s.book.as_mut().ok_or_else(no_book)?;
|
||||
let mut b = s.book.as_ref().ok_or_else(no_book)?.clone();
|
||||
if i >= b.documents.len() {
|
||||
return Err(bad("book.styleSource", format!("no document {i}")));
|
||||
}
|
||||
b.style_source = i;
|
||||
save(b)?;
|
||||
save(&b)?;
|
||||
s.book = Some(b);
|
||||
ok()
|
||||
}),
|
||||
cmd!(query "book.list", "Book", [], None, "{} → {path, styleSource, documents: [{path, pages, firstPage}]}", has_book, |s, _| {
|
||||
@@ -149,48 +308,10 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
Ok(json!({"path": b.path, "styleSource": b.style_source, "documents": docs}))
|
||||
}),
|
||||
cmd!(noundo "book.paginate", "Update Numbering", [], None, "{} — each document starts numbering where the previous one ended (files are saved)", has_book, |s, _| {
|
||||
let b = s.book.clone().ok_or_else(no_book)?;
|
||||
let mut next = 1u32;
|
||||
for p in &b.documents {
|
||||
let mut d = load(p)?;
|
||||
start_at(&mut d, next);
|
||||
next += d.page_count() as u32;
|
||||
store(p, &d)?;
|
||||
}
|
||||
Ok(json!({"pages": next - 1}))
|
||||
paginate(s)
|
||||
}),
|
||||
cmd!(noundo "book.syncStyles", "Synchronize Book", [], None, "{} — paragraph and character styles and swatches of the style source go into every document (by name)", has_book, |s, _| {
|
||||
let b = s.book.clone().ok_or_else(no_book)?;
|
||||
let src_path = b.documents.get(b.style_source).ok_or_else(|| bad("book.syncStyles", "the book is empty"))?;
|
||||
let src = load(src_path)?;
|
||||
let mut n = 0;
|
||||
for (i, p) in b.documents.iter().enumerate() {
|
||||
if i == b.style_source {
|
||||
continue;
|
||||
}
|
||||
let mut d = load(p)?;
|
||||
{
|
||||
let st = d.styles_mut();
|
||||
for ps in &src.styles.paragraph {
|
||||
st.paragraph.retain(|x| x.name != ps.name);
|
||||
st.paragraph.push(ps.clone());
|
||||
}
|
||||
for cs in &src.styles.character {
|
||||
st.character.retain(|x| x.name != cs.name);
|
||||
st.character.push(cs.clone());
|
||||
}
|
||||
}
|
||||
for sw in &src.swatches {
|
||||
match d.swatches.iter_mut().find(|w| w.name == sw.name) {
|
||||
Some(w) if !w.locked => *w = sw.clone(),
|
||||
Some(_) => {}
|
||||
None => d.swatches.push(sw.clone()),
|
||||
}
|
||||
}
|
||||
store(p, &d)?;
|
||||
n += 1;
|
||||
}
|
||||
Ok(json!({"synced": n}))
|
||||
sync_styles(s)
|
||||
}),
|
||||
cmd!(noundo "book.exportPdf", "Export Book to PDF…", [], None, "{path?} — every document in order as one PDF → {path, bytes, pages} (no path: {base64, …})", has_book, |s, p| {
|
||||
let b = s.book.clone().ok_or_else(no_book)?;
|
||||
@@ -210,7 +331,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, &bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, &bytes)?;
|
||||
Ok(json!({"path": path, "bytes": bytes.len(), "pages": pages}))
|
||||
}
|
||||
None => Ok(json!({"base64": super::file::base64_encode(&bytes), "bytes": bytes.len(), "pages": pages})),
|
||||
@@ -235,6 +356,7 @@ mod tests {
|
||||
s.execute("file.new", &json!({"pages": pages})).unwrap();
|
||||
s.execute("style.paragraph.create", &json!({"name": style, "chars": {"size": 20}})).unwrap();
|
||||
s.execute("file.saveAs", &json!({"path": p(name)})).unwrap();
|
||||
s.execute("file.close", &json!({})).unwrap();
|
||||
}
|
||||
s.execute("book.new", &json!({"path": p("book.dcbook")})).unwrap();
|
||||
s.execute("book.add", &json!({"path": p("a.designcraft")})).unwrap();
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
use super::{ChapterBatch, MAX_BATCH_CHAPTERS, write_staged_chapter};
|
||||
use crate::Session;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn a_commit_failure_reports_the_completed_prefix_and_cleans_staging() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let first = dir.path().join("first.designcraft");
|
||||
let second = dir.path().join("second.designcraft");
|
||||
let third = dir.path().join("third.designcraft");
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let old = (*s.doc().unwrap().doc).clone();
|
||||
let mut changed = old.clone();
|
||||
changed.sections.first_mut().unwrap().start_number = Some(9);
|
||||
let old_bytes = designcraft_format::save(&old).unwrap();
|
||||
for path in [&first, &second, &third] {
|
||||
std::fs::write(path, &old_bytes).unwrap();
|
||||
}
|
||||
let mut batch = ChapterBatch::new(3, "book.paginate").unwrap();
|
||||
for path in [&first, &second, &third] {
|
||||
batch.stage(path.to_str().unwrap(), &changed).unwrap();
|
||||
}
|
||||
let staging = batch.directory.path().to_path_buf();
|
||||
let mut calls = 0;
|
||||
let error = batch
|
||||
.commit_with(|path, staged| {
|
||||
calls += 1;
|
||||
if path == second { Err(std::io::Error::other("synthetic disk full")) } else { write_staged_chapter(path, staged) }
|
||||
})
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert_eq!(calls, 2);
|
||||
assert!(error.contains("synthetic disk full"), "{error}");
|
||||
assert!(error.contains(second.to_str().unwrap()), "{error}");
|
||||
let completed = error.split("completed chapter paths:").nth(1).unwrap();
|
||||
assert!(completed.contains(first.to_str().unwrap()), "{error}");
|
||||
assert!(!completed.contains(second.to_str().unwrap()) && !completed.contains(third.to_str().unwrap()), "{error}");
|
||||
assert_eq!(designcraft_format::load(&std::fs::read(first).unwrap()).unwrap().sections.first().unwrap().start_number, Some(9));
|
||||
assert_eq!(std::fs::read(second).unwrap(), old_bytes);
|
||||
assert_eq!(std::fs::read(third).unwrap(), old_bytes);
|
||||
assert!(!staging.exists(), "temporary chapter snapshots must be removed on a partial failure");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chapter_record_limit_is_checked_before_staging_allocation() {
|
||||
let error = match ChapterBatch::new(MAX_BATCH_CHAPTERS + 1, "book.paginate") {
|
||||
Err(e) => e,
|
||||
Ok(_) => panic!("oversized book batch accepted"),
|
||||
};
|
||||
assert!(error.to_string().contains("4096"));
|
||||
}
|
||||
@@ -75,6 +75,11 @@ fn renumber(dst: &mut designcraft_doc::Document, src: &designcraft_doc::Document
|
||||
let new_id = ItemId(dst.alloc());
|
||||
maps.items.insert(old, new_id);
|
||||
it.id = new_id;
|
||||
if let Some(m) = it.media.as_mut()
|
||||
&& let Some(p) = m.poster
|
||||
{
|
||||
m.poster = dst.adopt_asset(src, p).or(Some(p));
|
||||
}
|
||||
match &mut it.content {
|
||||
Content::Text(tf) => {
|
||||
let new_sid = if let Some(n) = maps.stories.get(&tf.story) {
|
||||
@@ -83,6 +88,15 @@ fn renumber(dst: &mut designcraft_doc::Document, src: &designcraft_doc::Document
|
||||
let n = StoryId(dst.alloc());
|
||||
let mut st = src.story(tf.story).cloned().unwrap_or_else(|| Story::new(n));
|
||||
st.id = n;
|
||||
let mut assets = HashMap::new();
|
||||
for a in designcraft_doc::story_asset_refs(&st) {
|
||||
if let Some(na) = dst.adopt_asset(src, a)
|
||||
&& na != a
|
||||
{
|
||||
assets.insert(a, na);
|
||||
}
|
||||
}
|
||||
designcraft_doc::remap_story_assets(&mut st, &assets);
|
||||
st.frames.clear();
|
||||
dst.stories.insert(n, Arc::new(st));
|
||||
maps.stories.insert(tf.story, n);
|
||||
@@ -94,8 +108,8 @@ fn renumber(dst: &mut designcraft_doc::Document, src: &designcraft_doc::Document
|
||||
}
|
||||
}
|
||||
Content::Graphic(g) => {
|
||||
if let Some(a) = src.assets.get(&g.asset) {
|
||||
dst.assets.entry(g.asset).or_insert_with(|| a.clone());
|
||||
if let Some(a) = dst.adopt_asset(src, g.asset) {
|
||||
g.asset = a;
|
||||
}
|
||||
}
|
||||
Content::Group { items } => {
|
||||
@@ -355,21 +369,18 @@ fn place_image(
|
||||
};
|
||||
let (nw, nh) = (f64::from(pw), f64::from(ph));
|
||||
let mode = fitting_of(&options.fitting);
|
||||
let aid = AssetId(doc.alloc());
|
||||
let fresh = AssetId(doc.alloc());
|
||||
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| field.to_string());
|
||||
let link = options.link_images.then(|| path.to_string_lossy().into_owned());
|
||||
doc.assets.insert(
|
||||
aid,
|
||||
Arc::new(Asset {
|
||||
id: aid,
|
||||
name,
|
||||
mime: designcraft_render::image_mime(&bytes).to_string(),
|
||||
link,
|
||||
data: Arc::new(bytes),
|
||||
pixels: Some((pw, ph)),
|
||||
page: 0,
|
||||
}),
|
||||
);
|
||||
let aid = doc.add_asset(Asset {
|
||||
id: fresh,
|
||||
name,
|
||||
mime: designcraft_render::image_mime(&bytes).to_string(),
|
||||
link,
|
||||
data: Arc::new(bytes),
|
||||
pixels: Some((pw, ph)),
|
||||
page: 0,
|
||||
});
|
||||
let mut g = Graphic { asset: aid, size: (nw, nh), xf: Affine::translate((inner.x0, inner.y0)), auto_fit: mode, fit_align: 0, crop: [0.0; 4] };
|
||||
if let Some(xf) = g.fitted(inner, mode) {
|
||||
g.xf = xf;
|
||||
|
||||
@@ -367,10 +367,16 @@ fn sniff(name: &str, text: &str) -> Delimiter {
|
||||
if ext == "csv" {
|
||||
return Delimiter::Comma;
|
||||
}
|
||||
let header = first_physical_line(text);
|
||||
if header.contains('\t') {
|
||||
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 header.contains(';') && !header.contains(',') {
|
||||
} else if eligible(&semicolon) && !eligible(&comma) {
|
||||
Delimiter::Semicolon
|
||||
} else {
|
||||
Delimiter::Comma
|
||||
@@ -381,8 +387,66 @@ pub fn extension(name: &str) -> String {
|
||||
Path::new(name).extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase()
|
||||
}
|
||||
|
||||
fn first_physical_line(text: &str) -> &str {
|
||||
text.split('\n').next().unwrap_or("").trim_end_matches('\r')
|
||||
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.
|
||||
@@ -452,6 +516,7 @@ fn parse_delimited(s: &str, delim: char) -> Result<Vec<RawRow>, String> {
|
||||
let mut row: Vec<String> = Vec::new();
|
||||
let mut field = String::new();
|
||||
let mut quoted = false;
|
||||
let mut field_start = true;
|
||||
let mut line = 1usize;
|
||||
let mut row_line = 1usize;
|
||||
let mut data_rows = 0usize;
|
||||
@@ -501,21 +566,25 @@ fn parse_delimited(s: &str, delim: char) -> Result<Vec<RawRow>, String> {
|
||||
};
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '\n' {
|
||||
if c == '\n' || (c == '\r' && chars.peek() != Some(&'\n')) {
|
||||
line = line.saturating_add(1);
|
||||
}
|
||||
if quoted {
|
||||
match c {
|
||||
'"' if chars.peek() == Some(&'"') => {
|
||||
field.push('"');
|
||||
if !discarding {
|
||||
field.push('"');
|
||||
}
|
||||
chars.next();
|
||||
}
|
||||
'"' => quoted = false,
|
||||
_ => field.push(c),
|
||||
_ if !discarding => field.push(c),
|
||||
_ => {}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if c == delim {
|
||||
field_start = true;
|
||||
if discarding {
|
||||
field.clear();
|
||||
continue;
|
||||
@@ -531,14 +600,25 @@ fn parse_delimited(s: &str, delim: char) -> Result<Vec<RawRow>, String> {
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
'"' if field.is_empty() && !discarding => quoted = true,
|
||||
'\r' => {}
|
||||
'\n' => {
|
||||
'"' 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 discarding => {}
|
||||
_ => field.push(c),
|
||||
}
|
||||
}
|
||||
if !field.is_empty() || !row.is_empty() || discarding {
|
||||
|
||||
@@ -85,6 +85,282 @@ fn csv_parsing() {
|
||||
assert_eq!(quoted[1][0], "Ada\nGrace");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_sniffs_quoted_logical_headers() {
|
||||
for (body, delimiter, names) in [
|
||||
("\"Last, First\";City\nAda;London\n", Delimiter::Semicolon, ["Last, First", "City"]),
|
||||
("\"Name\tLabel\",City\nAda,London\n", Delimiter::Comma, ["Name\tLabel", "City"]),
|
||||
("\"Name\nLabel\";City\nAda;London\n", Delimiter::Semicolon, ["Name\nLabel", "City"]),
|
||||
("\r\n \r\nName;City\r\nAda;London\r\n", Delimiter::Semicolon, ["Name", "City"]),
|
||||
("\"Name \"\"label,\"\"\";City\nAda;London\n", Delimiter::Semicolon, ["Name \"label,\"", "City"]),
|
||||
] {
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
let result = exec(&mut s, "data.source.select", json!({"bytes": crate::cmd::base64_encode(body.as_bytes()), "name": "people.txt"}));
|
||||
assert_eq!(result["records"], 1, "{body:?}");
|
||||
let source = &s.documents()[0].doc.data_merge.sources[0];
|
||||
assert_eq!(source.delimiter, delimiter, "{body:?}");
|
||||
assert_eq!(source.fields.iter().map(|f| f.name.as_str()).collect::<Vec<_>>(), names, "{body:?}");
|
||||
assert_eq!(source.rows, vec![vec!["Ada", "London"]], "{body:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_sniff_quote_state_belongs_to_the_candidate_delimiter() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
for (body, delimiter, names) in [
|
||||
(format!("Name,\"preferred\tCity\"{ending}Ada\tLondon{ending}"), Delimiter::Tab, ["Name,\"preferred", "City\""]),
|
||||
(format!("\"Last, First\";City{ending}Ada;London{ending}"), Delimiter::Semicolon, ["Last, First", "City"]),
|
||||
(format!("\"Name\tLabel\",City{ending}Ada,London{ending}"), Delimiter::Comma, ["Name\tLabel", "City"]),
|
||||
(format!("\"\";\"\"{ending} \t{ending}\"Last, First\";City{ending}Ada;London{ending}"), Delimiter::Semicolon, ["Last, First", "City"]),
|
||||
] {
|
||||
let automatic = parse::table_from_bytes(body.as_bytes(), "people.txt", None).unwrap();
|
||||
let explicit = parse::table_from_bytes(body.as_bytes(), "people.txt", Some(delimiter)).unwrap();
|
||||
let fields: Vec<_> = names.into_iter().map(|name| DataField { name: name.into(), kind: DataFieldKind::Text }).collect();
|
||||
assert_eq!(automatic.delimiter, delimiter, "{body:?}");
|
||||
assert_eq!(automatic.fields, fields, "{body:?}");
|
||||
assert_eq!(automatic.rows, vec![vec!["Ada", "London"]], "{body:?}");
|
||||
assert!(automatic.warnings.is_empty(), "{body:?}: {:?}", automatic.warnings);
|
||||
assert_eq!(automatic, explicit, "{body:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_sniff_prefers_coherent_quoted_field_boundaries() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
for (header, first, second, delimiter, names, rows, warnings) in [
|
||||
(
|
||||
"\"Name\";\"Last, First\"",
|
||||
"\"Ada\";\"Lovelace, Countess\"",
|
||||
"\"Grace\";\"Hopper, Admiral\"",
|
||||
Delimiter::Semicolon,
|
||||
vec!["Name", "Last, First"],
|
||||
vec![vec!["Ada", "Lovelace, Countess"], vec!["Grace", "Hopper, Admiral"]],
|
||||
vec![],
|
||||
),
|
||||
(
|
||||
"\"Name\",\"Region\tCity\"",
|
||||
"\"Ada\",\"London\"",
|
||||
"\"Grace\",\"Paris\"",
|
||||
Delimiter::Comma,
|
||||
vec!["Name", "Region\tCity"],
|
||||
vec![vec!["Ada", "London"], vec!["Grace", "Paris"]],
|
||||
vec![],
|
||||
),
|
||||
(
|
||||
"\"Name\";\"City\";\"Last, \"\"First\"\"\";\"Title\"",
|
||||
"\"Ada\";\"London\";\"Lovelace, \"\"Countess\"\"\";\"Analyst\"",
|
||||
"\"Grace\";\"Paris\";\"Hopper, \"\"Admiral\"\"\";\"Engineer\";\"ignored\"",
|
||||
Delimiter::Semicolon,
|
||||
vec!["Name", "City", "Last, \"First\"", "Title"],
|
||||
vec![vec!["Ada", "London", "Lovelace, \"Countess\"", "Analyst"], vec!["Grace", "Paris", "Hopper, \"Admiral\"", "Engineer"]],
|
||||
vec!["Row 3 has cells past the header. Those cells were ignored."],
|
||||
),
|
||||
(
|
||||
"\"Name\",\"Role\",\"Region\t\"\"City\"\"\",\"Title\"",
|
||||
"\"Ada\",\"Analyst\",\"London\t\"\"West End\"\"\",\"Countess\"",
|
||||
"\"Grace\",\"Engineer\",\"Paris\t\"\"Centre\"\"\",\"Admiral\",\"ignored\"",
|
||||
Delimiter::Comma,
|
||||
vec!["Name", "Role", "Region\t\"City\"", "Title"],
|
||||
vec![vec!["Ada", "Analyst", "London\t\"West End\"", "Countess"], vec!["Grace", "Engineer", "Paris\t\"Centre\"", "Admiral"]],
|
||||
vec!["Row 3 has cells past the header. Those cells were ignored."],
|
||||
),
|
||||
] {
|
||||
let body = format!("{header}{ending}{first}{ending}{second}{ending}");
|
||||
let automatic = parse::table_from_bytes(body.as_bytes(), "people.txt", None).unwrap();
|
||||
let explicit = parse::table_from_bytes(body.as_bytes(), "people.txt", Some(delimiter)).unwrap();
|
||||
let fields: Vec<_> = names.into_iter().map(|name| DataField { name: name.into(), kind: DataFieldKind::Text }).collect();
|
||||
assert_eq!(automatic.delimiter, delimiter, "{body:?}");
|
||||
assert_eq!(automatic.fields, fields, "{body:?}");
|
||||
assert_eq!(automatic.rows, rows, "{body:?}");
|
||||
assert_eq!(automatic.warnings, warnings, "{body:?}");
|
||||
assert_eq!(automatic, explicit, "{body:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_sniff_retains_priority_and_permissive_fallback() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
for (header, row, delimiter, names, cells) in [
|
||||
// Equally coherent candidates retain Tab, then Comma, then Semicolon priority.
|
||||
("Name,Title\tCity", "Ada,Analyst\tLondon\tignored", Delimiter::Tab, ["Name,Title", "City"], ["Ada,Analyst", "London"]),
|
||||
("Name;Title,City", "Ada;Analyst,London,ignored", Delimiter::Comma, ["Name;Title", "City"], ["Ada;Analyst", "London"]),
|
||||
// No coherent candidate separates fields here. Keep parsing the trailing text
|
||||
// after a quoted field exactly as before, using the same delimiter priority.
|
||||
(
|
||||
"\"Name\"suffix,Title\tCity",
|
||||
"\"Ada\"suffix,Analyst\tLondon\tignored",
|
||||
Delimiter::Tab,
|
||||
["Namesuffix,Title", "City"],
|
||||
["Adasuffix,Analyst", "London"],
|
||||
),
|
||||
(
|
||||
"\"Name\"suffix;Title,City",
|
||||
"\"Ada\"suffix;Analyst,London,ignored",
|
||||
Delimiter::Comma,
|
||||
["Namesuffix;Title", "City"],
|
||||
["Adasuffix;Analyst", "London"],
|
||||
),
|
||||
("\"Name\"suffix;City", "\"Ada\"suffix;London;ignored", Delimiter::Semicolon, ["Namesuffix", "City"], ["Adasuffix", "London"]),
|
||||
] {
|
||||
let body = format!("{header}{ending}{row}{ending}");
|
||||
let automatic = parse::table_from_bytes(body.as_bytes(), "people.txt", None).unwrap();
|
||||
let explicit = parse::table_from_bytes(body.as_bytes(), "people.txt", Some(delimiter)).unwrap();
|
||||
let fields: Vec<_> = names.into_iter().map(|name| DataField { name: name.into(), kind: DataFieldKind::Text }).collect();
|
||||
assert_eq!(automatic.delimiter, delimiter, "{body:?}");
|
||||
assert_eq!(automatic.fields, fields, "{body:?}");
|
||||
assert_eq!(automatic.rows, vec![cells.to_vec()], "{body:?}");
|
||||
assert_eq!(automatic.warnings, vec!["Row 2 has cells past the header. Those cells were ignored."], "{body:?}");
|
||||
assert_eq!(automatic, explicit, "{body:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_sniff_allows_post_quote_whitespace() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
for space in [" ", "\u{00A0}", "\u{2003}"] {
|
||||
for (header, separator, delimiter, names) in [
|
||||
(format!("\"Name\"{space};\"Last, First\""), ';', Delimiter::Semicolon, ["Name", "Last, First"]),
|
||||
(format!("\"Name\"{space},\"Region\tCity\"{space}"), ',', Delimiter::Comma, ["Name", "Region\tCity"]),
|
||||
(format!("\"Name\"{space}\t\"Last, First; City\"{space}"), '\t', Delimiter::Tab, ["Name", "Last, First; City"]),
|
||||
] {
|
||||
let body = format!(
|
||||
"{header}{ending}\"Ada\"{space}{separator}\"London\"{space}{ending}\"Grace\"{space}{separator}\"Paris\"{space}{separator}ignored{ending}"
|
||||
);
|
||||
let automatic = parse::table_from_bytes(body.as_bytes(), "people.txt", None).unwrap();
|
||||
let explicit = parse::table_from_bytes(body.as_bytes(), "people.txt", Some(delimiter)).unwrap();
|
||||
let fields: Vec<_> = names.into_iter().map(|name| DataField { name: name.into(), kind: DataFieldKind::Text }).collect();
|
||||
assert_eq!(automatic.delimiter, delimiter, "{body:?}");
|
||||
assert_eq!(automatic.fields, fields, "{body:?}");
|
||||
// Header classification trims; record values must keep their whitespace.
|
||||
assert_eq!(
|
||||
automatic.rows,
|
||||
vec![vec![format!("Ada{space}"), format!("London{space}")], vec![format!("Grace{space}"), format!("Paris{space}")]],
|
||||
"{body:?}"
|
||||
);
|
||||
assert_eq!(automatic.warnings, vec!["Row 3 has cells past the header. Those cells were ignored."], "{body:?}");
|
||||
assert_eq!(automatic, explicit, "{body:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_sniff_post_quote_whitespace_keeps_closed_field_state() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
for space in [" ", "\u{00A0}", "\u{2003}"] {
|
||||
for suffix in ["suffix", "\"suffix\""] {
|
||||
let body = format!("\"Name\"{space}{suffix};\"Last, First\"{ending}Ada,London,ignored{ending}");
|
||||
// Both separated formats have incompatible post-quote suffixes. Keep
|
||||
// the permissive Comma fallback, including literal quotes in the name.
|
||||
let automatic = parse::table_from_bytes(body.as_bytes(), "people.txt", None).unwrap();
|
||||
let explicit = parse::table_from_bytes(body.as_bytes(), "people.txt", Some(Delimiter::Comma)).unwrap();
|
||||
assert_eq!(automatic.delimiter, Delimiter::Comma, "{body:?}");
|
||||
assert_eq!(
|
||||
automatic.fields,
|
||||
vec![
|
||||
DataField { name: format!("Name{space}{suffix};\"Last"), kind: DataFieldKind::Text },
|
||||
DataField { name: "First\"".into(), kind: DataFieldKind::Text },
|
||||
],
|
||||
"{body:?}"
|
||||
);
|
||||
assert_eq!(automatic.rows, vec![vec!["Ada", "London"]], "{body:?}");
|
||||
assert_eq!(automatic.warnings, vec!["Row 2 has cells past the header. Those cells were ignored."], "{body:?}");
|
||||
assert_eq!(automatic, explicit, "{body:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_literal_quote_header_merges_the_detected_column() {
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "", None);
|
||||
let body = "Name,\"preferred\tCity\"\nAda\tLondon\nGrace\tParis\n";
|
||||
let selected = exec(&mut s, "data.source.select", json!({"bytes": crate::cmd::base64_encode(body.as_bytes()), "name": "people.txt"}));
|
||||
assert_eq!(selected["records"], 2);
|
||||
let source = &s.documents()[0].doc.data_merge.sources[0];
|
||||
assert_eq!(source.delimiter, Delimiter::Tab);
|
||||
assert_eq!(source.rows, vec![vec!["Ada", "London"], vec!["Grace", "Paris"]]);
|
||||
exec(&mut s, "data.placeholder.add", json!({"field": "City\""}));
|
||||
let merged = exec(&mut s, "data.merge", json!({}));
|
||||
assert_eq!(merged["records"], 2);
|
||||
assert_eq!(merged["pages"], 2);
|
||||
assert_eq!(texts(&s), vec!["London", "Paris"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_preserves_quoted_newlines_and_counts_record_lines() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
let body = format!("Name,City{ending}\"Ada{ending}Lovelace\",London{ending}Grace,Paris,extra{ending}");
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
let result = select_csv(&mut s, &body);
|
||||
assert_eq!(result["records"], 2, "{ending:?}");
|
||||
let source = &s.documents()[0].doc.data_merge.sources[0];
|
||||
assert_eq!(source.rows, vec![vec![format!("Ada{ending}Lovelace"), "London".into()], vec!["Grace".into(), "Paris".into()]]);
|
||||
assert_eq!(source.warnings.len(), 1);
|
||||
assert!(source.warnings[0].contains("Row 4"), "{:?}", source.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_quoted_header_and_cr_records_merge_independently() {
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "", None);
|
||||
let body = "\u{FEFF}\"Last, First\";City\rAda;London\rGrace;Paris\r";
|
||||
exec(&mut s, "data.source.select", json!({"bytes": crate::cmd::base64_encode(body.as_bytes()), "name": "people.txt"}));
|
||||
exec(&mut s, "data.placeholder.add", json!({"field": "Last, First"}));
|
||||
let template = s.documents()[0].doc.clone();
|
||||
let result = exec(&mut s, "data.merge", json!({}));
|
||||
assert_eq!(result["records"], 2);
|
||||
assert_eq!(result["pages"], 2);
|
||||
assert_eq!(texts(&s), vec!["Ada", "Grace"]);
|
||||
assert_eq!(serde_json::to_value(&s.documents()[0].doc).unwrap(), serde_json::to_value(&template).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_discarded_quoted_fields_keep_records_and_warning_lines() {
|
||||
for ending in ["\n", "\r\n", "\r"] {
|
||||
for (discarded, following_line) in [(format!("\"Address \"\"line 1\"\"{ending}Address line 2\""), 4), ("literal\"quote".into(), 3)] {
|
||||
let mut s = Session::new();
|
||||
new_doc(&mut s, 1);
|
||||
text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "", None);
|
||||
let body = format!("Name{ending}Ada,,,{discarded}{ending}Grace,extra{ending}");
|
||||
let selected = select_csv(&mut s, &body);
|
||||
assert_eq!(selected["records"], 2, "{body:?}");
|
||||
let source = &s.documents()[0].doc.data_merge.sources[0];
|
||||
assert_eq!(source.rows, vec![vec!["Ada"], vec!["Grace"]], "{body:?}");
|
||||
assert_eq!(source.warnings.len(), 2, "{:?}", source.warnings);
|
||||
assert!(source.warnings[0].contains("Row 2"), "{:?}", source.warnings);
|
||||
assert!(source.warnings[1].contains(&format!("Row {following_line}")), "{:?}", source.warnings);
|
||||
exec(&mut s, "data.placeholder.add", json!({"field": "Name"}));
|
||||
let merged = exec(&mut s, "data.merge", json!({}));
|
||||
assert_eq!(merged["records"], 2);
|
||||
assert_eq!(merged["pages"], 2);
|
||||
assert_eq!(texts(&s), vec!["Ada", "Grace"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_source_delimiter_override_and_known_extensions_win() {
|
||||
let body = "Name;City\nAda;London\n";
|
||||
let csv = parse::table_from_bytes(body.as_bytes(), "people.csv", None).unwrap();
|
||||
assert_eq!(csv.delimiter, Delimiter::Comma);
|
||||
assert_eq!(csv.rows, vec![vec!["Ada;London"]]);
|
||||
let explicit = parse::table_from_bytes(body.as_bytes(), "people.csv", Some(Delimiter::Semicolon)).unwrap();
|
||||
assert_eq!(explicit.rows, vec![vec!["Ada", "London"]]);
|
||||
let tab = parse::table_from_bytes(b"Name,Title\tCity\nAda,Analyst\tLondon", "people.tsv", None).unwrap();
|
||||
assert_eq!(tab.delimiter, Delimiter::Tab);
|
||||
assert_eq!(tab.rows, vec![vec!["Ada,Analyst", "London"]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_sniff_padding_skips_and_warnings() {
|
||||
let dir = scratch("sniff");
|
||||
|
||||
+287
-13
@@ -20,13 +20,6 @@ fn has_clip(s: &Session) -> std::result::Result<(), String> {
|
||||
if s.clipboard.is_some() || text { Ok(()) } else { Err("clipboard is empty".into()) }
|
||||
}
|
||||
|
||||
fn has_clip_or_text(s: &Session) -> std::result::Result<(), String> {
|
||||
if s.active().is_some_and(|d| d.selection.text.is_some()) {
|
||||
return Ok(());
|
||||
}
|
||||
has_clip(s)
|
||||
}
|
||||
|
||||
fn has_selection_or_text(s: &Session) -> std::result::Result<(), String> {
|
||||
if s.active().and_then(|d| d.selection.text).is_some_and(|t| !t.is_caret()) {
|
||||
return Ok(());
|
||||
@@ -74,7 +67,7 @@ fn paste_text(s: &mut Session, text: Option<String>, formatted: bool) -> Result<
|
||||
}
|
||||
}
|
||||
let use_clip = formatted && clip.as_ref().is_some_and(|(_, p)| text.as_ref().is_none_or(|x| x.replace("\r\n", "\n") == *p));
|
||||
// Footnotes, cross-references and tables only paste into story text, not cells or footnotes.
|
||||
// Footnotes, endnotes, editorial notes, cross-references and tables cannot be pasted into cells or footnotes.
|
||||
if use_clip && let Some((slice, _)) = clip {
|
||||
let slice = if t.cell.is_some() { strip_objects(&slice) } else { (*slice).clone() };
|
||||
return s.edit(|d, sel| {
|
||||
@@ -88,12 +81,84 @@ fn paste_text(s: &mut Session, text: Option<String>, formatted: bool) -> Result<
|
||||
});
|
||||
}
|
||||
let text = text.or_else(|| clip.map(|(_, p)| p)).ok_or_else(|| super::bad("edit.paste", "clipboard is empty"))?;
|
||||
s.execute("text.insert", &json!({"text": text.replace("\r\n", "\n").replace('\r', "\n"), "raw": true}))
|
||||
let text = outside_text(&text)?;
|
||||
s.execute("text.insert", &json!({"text": text, "raw": true}))
|
||||
}
|
||||
|
||||
/// Text from the system clipboard as story text: Unix line ends, and none of the private-use
|
||||
/// characters (U+E000–U+E1FF) that stories use as markers for breaks, footnotes, tables and
|
||||
/// variables, since a stray one would be read as a marker without its payload.
|
||||
fn outside_text(text: &str) -> Result<String> {
|
||||
let text: String = text.replace("\r\n", "\n").replace('\r', "\n").chars().filter(|c| !('\u{E000}'..='\u{E1FF}').contains(c)).collect();
|
||||
if text.is_empty() { Err(super::bad("edit.paste", "the clipboard holds no text")) } else { Ok(text) }
|
||||
}
|
||||
|
||||
/// Pasted text with no insertion point: a new text frame holding it, as wide as the first column
|
||||
/// of the page's margin area and as tall as the text (at most the margin area's height), centred
|
||||
/// on `at` (spread coordinates) or else on the margin area. The frame ends up selected. With
|
||||
/// `slice` (the text copied here), the frame's story is that formatted copy.
|
||||
fn paste_text_frame(s: &mut Session, p: &Value, text: &str, slice: Option<&designcraft_doc::Story>) -> Result<Value> {
|
||||
use designcraft_geom::{Point, Rect, shapes};
|
||||
const LIMIT: f64 = 1.0e5;
|
||||
let sr = super::spread_param(p, "spread")?;
|
||||
let at = super::point_param(p, "at")
|
||||
.filter(|a| a.x.is_finite() && a.y.is_finite())
|
||||
.map(|a| Point::new(a.x.clamp(-LIMIT, LIMIT), a.y.clamp(-LIMIT, LIMIT)));
|
||||
let lid = s.doc()?.active_layer;
|
||||
let cache = s.cache.clone();
|
||||
let text = if slice.is_some() { String::new() } else { outside_text(text)? };
|
||||
s.edit(|d, sel| {
|
||||
let sp = d.spread(sr).ok_or_else(|| super::bad("edit.paste", "no such spread"))?;
|
||||
let page = at.and_then(|a| sp.page_at_x(a.x)).and_then(|i| sp.pages.get(i)).or_else(|| sp.pages.first());
|
||||
let page = page.ok_or_else(|| super::bad("edit.paste", "the spread has no pages"))?;
|
||||
let margins = page.margin_rect();
|
||||
let column = page.column_rects().first().copied().unwrap_or(margins);
|
||||
let w = column.width().max(12.0);
|
||||
let max_h = margins.height().max(12.0);
|
||||
let c = at.unwrap_or(Point::new(column.center().x, margins.center().y));
|
||||
let rect = |h: f64| Rect::new(c.x - w / 2.0, c.y - h / 2.0, c.x + w / 2.0, c.y + h / 2.0);
|
||||
let para = match slice.and_then(|sl| sl.paras.first()) {
|
||||
Some(pf) => designcraft_doc::ParaFormat { table: None, ..pf.clone() },
|
||||
None => designcraft_doc::ParaFormat { style: d.styles.default_paragraph.clone(), ..Default::default() },
|
||||
};
|
||||
let (id, sid) = d.add_text_frame(sr, rect(max_h), lid, &text, para)?;
|
||||
if let Some(sl) = slice {
|
||||
d.story_mut(sid).ok_or(designcraft_doc::DocError::NoStory(sid))?.insert_story(0, sl);
|
||||
}
|
||||
// Shrink to the composed text unless it already fills the frame.
|
||||
let composed = cache.get(d, sid, None);
|
||||
if !composed.is_overset()
|
||||
&& let Some(ft) = composed.frame(id)
|
||||
{
|
||||
let inset = d.item(id).and_then(|i| i.text_frame()).map_or(0.0, |tf| tf.options.inset[0] + tf.options.inset[2]);
|
||||
let h = (ft.content_height + inset + 0.5).clamp(12.0, max_h);
|
||||
if h.is_finite()
|
||||
&& h < max_h
|
||||
&& let Some(it) = d.item_mut(id)
|
||||
{
|
||||
it.path = shapes::rectangle(rect(h));
|
||||
// Keep the full height if the last line no longer fits.
|
||||
if cache.get(d, sid, None).is_overset()
|
||||
&& let Some(it) = d.item_mut(id)
|
||||
{
|
||||
it.path = shapes::rectangle(rect(max_h));
|
||||
}
|
||||
}
|
||||
}
|
||||
*sel = Selection::items(vec![id]);
|
||||
Ok(json!({"id": id.0, "story": sid.0}))
|
||||
})
|
||||
}
|
||||
|
||||
fn strip_objects(st: &designcraft_doc::Story) -> designcraft_doc::Story {
|
||||
let mut out = st.clone();
|
||||
let marks = [designcraft_doc::FOOTNOTE_REF, designcraft_doc::XREF_MARK, designcraft_doc::TABLE_ANCHOR];
|
||||
let marks = [
|
||||
designcraft_doc::FOOTNOTE_REF,
|
||||
designcraft_doc::ENDNOTE_REF,
|
||||
designcraft_doc::NOTE_MARK,
|
||||
designcraft_doc::XREF_MARK,
|
||||
designcraft_doc::TABLE_ANCHOR,
|
||||
];
|
||||
while let Some(i) = out.text.find(marks) {
|
||||
let n = out.text[i..].chars().next().map_or(1, char::len_utf8);
|
||||
out.delete(i..i + n);
|
||||
@@ -112,6 +177,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
st.doc = doc;
|
||||
st.selection = e.selection;
|
||||
st.revision += 1;
|
||||
// Pending typing overrides belong to the current edit state, not a restored snapshot.
|
||||
s.typing_format = None;
|
||||
Ok(json!({"undone": e.label}))
|
||||
}),
|
||||
cmd!(noundo "edit.redo", "Redo", ["Edit"], Some("Cmd+Shift+Z"), "{}", can_redo, |s, _| {
|
||||
@@ -123,6 +190,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
st.doc = doc;
|
||||
st.selection = e.selection;
|
||||
st.revision += 1;
|
||||
s.typing_format = None;
|
||||
Ok(json!({"redone": e.label}))
|
||||
}),
|
||||
cmd!(noundo "selection.set", "Select", [], None, "{ids: [id], add?: bool, content?: bool}", has_doc, |s, p| {
|
||||
@@ -231,11 +299,18 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Paste",
|
||||
["Edit"],
|
||||
Some("Cmd+V"),
|
||||
"{inPlace?: bool, text?: the system clipboard's text (pastes it unless it is what was copied here)}",
|
||||
has_clip_or_text,
|
||||
"{inPlace?: bool, text?: the system clipboard's text (pastes it unless it is what was copied here; with no insertion point it becomes a new text frame), spread?, at?: [x, y] (spread coords: the new text frame's centre)}",
|
||||
// The system clipboard may hold text from another app, so Paste is always available.
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let text = super::str_param(p, "text").filter(|t| !t.is_empty());
|
||||
if s.doc()?.selection.text.is_some() {
|
||||
return paste_text(s, super::str_param(p, "text").map(str::to_string), true);
|
||||
return paste_text(s, text.map(str::to_string), true);
|
||||
}
|
||||
if let Some(text) = text {
|
||||
// What our own text copy put on the clipboard keeps its formatting.
|
||||
let slice = s.text_clipboard.clone().filter(|(_, plain)| text.replace("\r\n", "\n") == *plain).map(|(slice, _)| slice);
|
||||
return paste_text_frame(s, p, text, slice.as_deref());
|
||||
}
|
||||
let Some(clip) = s.clipboard.clone() else {
|
||||
return Err(super::bad("edit.paste", "clipboard is empty"));
|
||||
@@ -320,6 +395,87 @@ mod text_clipboard_tests {
|
||||
s.doc().unwrap().doc.item(ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paste_text_with_no_insertion_point_makes_a_text_frame() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let items = |s: &Session| s.doc().unwrap().doc.spreads[0].items.len();
|
||||
// Text from another app, nothing copied here: a new, selected text frame.
|
||||
let r = s.execute("edit.paste", &json!({"text": "From elsewhere\r\nSecond line"})).unwrap();
|
||||
let id = ItemId(r["id"].as_u64().unwrap());
|
||||
let d = s.doc().unwrap();
|
||||
assert_eq!(d.selection.items, vec![id]);
|
||||
assert!(d.selection.text.is_none(), "selected as an object");
|
||||
let it = d.doc.item(id).unwrap();
|
||||
assert_eq!(d.doc.story(it.text_frame().unwrap().story).unwrap().text, "From elsewhere\nSecond line");
|
||||
let page = &d.doc.spreads[0].pages[0];
|
||||
let (b, m) = (it.bounds(), page.margin_rect());
|
||||
assert!((b.width() - page.column_rects()[0].width()).abs() < 1e-6, "{b:?}");
|
||||
assert!(b.height() < m.height() / 2.0, "fits two lines: {b:?}");
|
||||
assert!((b.center().y - m.center().y).abs() < 1e-6, "{b:?}");
|
||||
assert!(!s.cache.get(&d.doc, it.text_frame().unwrap().story, None).is_overset());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(items(&s), 0, "one undo removes it");
|
||||
// Centred on `at`; hostile numbers are clamped.
|
||||
let r = s.execute("edit.paste", &json!({"text": "Here", "at": [200.0, 300.0]})).unwrap();
|
||||
let b = s.doc().unwrap().doc.item(ItemId(r["id"].as_u64().unwrap())).unwrap().bounds();
|
||||
assert!((b.center().x - 200.0).abs() < 1e-6 && (b.center().y - 300.0).abs() < 1e-6, "{b:?}");
|
||||
assert!(s.execute("edit.paste", &json!({"text": "Far", "at": [1e300, -1e300], "spread": 99})).is_err());
|
||||
s.execute("edit.paste", &json!({"text": "Far", "at": [1e300, -1e300]})).unwrap();
|
||||
// Copied objects paste when no text is given.
|
||||
s.execute("edit.deselectAll", &json!({})).unwrap();
|
||||
let rect = s.execute("frame.create", &json!({"rect": [72, 72, 200, 160]})).unwrap()["id"].clone();
|
||||
s.execute("selection.set", &json!({"ids": [rect]})).unwrap();
|
||||
s.execute("edit.copy", &json!({})).unwrap();
|
||||
let n = items(&s);
|
||||
let r = s.execute("edit.paste", &json!({})).unwrap();
|
||||
assert_eq!(r["ids"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(items(&s), n + 1);
|
||||
assert!(s.doc().unwrap().doc.item(ItemId(r["ids"][0].as_u64().unwrap())).unwrap().text_frame().is_none());
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outside_text_never_pastes_markers() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
// Private-use characters from another app would read as footnote, table or variable markers.
|
||||
let dirty = "a\u{E00A}b\u{E009}c\u{E1FF}d\u{E200}";
|
||||
let r = s.execute("edit.paste", &json!({"text": dirty})).unwrap();
|
||||
let st = s.doc().unwrap().doc.item(ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story;
|
||||
assert_eq!(s.doc().unwrap().doc.story(st).unwrap().text, "abcd\u{E200}");
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
let b = frame(&mut s, "Target");
|
||||
s.execute("text.select", &json!({"story": b.0, "anchor": 6, "focus": 6})).unwrap();
|
||||
s.execute("edit.paste", &json!({"text": dirty})).unwrap();
|
||||
s.execute("edit.pasteWithoutFormatting", &json!({"text": dirty})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.story(b).unwrap().text, "Targetabcd\u{E200}abcd\u{E200}");
|
||||
assert!(s.execute("edit.paste", &json!({"text": "\u{E00A}"})).is_err());
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copied_text_pasted_with_no_insertion_point_keeps_its_formatting() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let a = frame(&mut s, "Bold words\nSecond");
|
||||
s.execute("text.select", &json!({"story": a.0, "anchor": 0, "focus": 4})).unwrap();
|
||||
s.execute("type.bold", &json!({})).unwrap();
|
||||
s.execute("text.select", &json!({"story": a.0, "anchor": 11, "focus": 17})).unwrap();
|
||||
s.execute("type.para", &json!({"attrs": {"align": "center"}})).unwrap();
|
||||
s.execute("text.select", &json!({"story": a.0, "anchor": 0, "focus": 17})).unwrap();
|
||||
let copied = s.execute("edit.copy", &json!({})).unwrap()["text"].as_str().unwrap().to_string();
|
||||
s.execute("edit.deselectAll", &json!({})).unwrap();
|
||||
let r = s.execute("edit.paste", &json!({"text": copied})).unwrap();
|
||||
let d = s.doc().unwrap();
|
||||
let st = d.doc.story(d.doc.item(ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story).unwrap();
|
||||
assert_eq!(st.text, "Bold words\nSecond");
|
||||
assert_eq!(st.char_format_at(2).over.font_style.as_deref(), Some("Bold"));
|
||||
assert_eq!(st.char_format_at(7).over.font_style, None);
|
||||
assert_eq!(st.paras[1], d.doc.story(a).unwrap().paras[1]);
|
||||
d.doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_copy_paste_and_plain_paste() {
|
||||
let mut s = Session::new();
|
||||
@@ -359,6 +515,124 @@ mod text_clipboard_tests {
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
fn note_clipboard_session() -> (Session, designcraft_doc::StoryId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let source = frame(&mut s, "aéz");
|
||||
s.execute("text.select", &json!({"story": source.0, "anchor": 1, "focus": 1})).unwrap();
|
||||
s.execute("endnote.insert", &json!({"text": "Citation A"})).unwrap();
|
||||
s.execute("note.new", &json!({"text": "Memo A", "author": "Ed"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.story(source).unwrap().text, "a\u{E00F}\u{E010}éz");
|
||||
s.execute("text.select", &json!({"story": source.0, "anchor": 0, "focus": 10})).unwrap();
|
||||
s.execute("type.char", &json!({"attrs": {"fontStyle": "Bold"}})).unwrap();
|
||||
assert_eq!(s.execute("edit.copy", &json!({})).unwrap(), json!({"text": "aéz"}));
|
||||
(s, source)
|
||||
}
|
||||
|
||||
fn assert_child_note_paste(
|
||||
s: &mut Session,
|
||||
source: designcraft_doc::StoryId,
|
||||
parent: designcraft_doc::StoryId,
|
||||
cell: designcraft_doc::CellAddr,
|
||||
endpoints: (usize, usize),
|
||||
) {
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let selected = s.doc().unwrap().selection.text.unwrap();
|
||||
assert_eq!((selected.story, selected.cell, selected.anchor, selected.focus), (parent, Some(cell), endpoints.0, endpoints.1));
|
||||
assert_eq!(selected.frame, None);
|
||||
let before_child = before.text_story(parent, Some(cell)).unwrap();
|
||||
assert_eq!(before_child.text, "L----R");
|
||||
let clipboard = s.text_clipboard.clone();
|
||||
let history = s.execute("document.history", &json!({})).unwrap();
|
||||
assert_eq!(history["redo"], json!([]));
|
||||
|
||||
let result = s.execute("edit.paste", &json!({})).unwrap();
|
||||
let after = s.doc().unwrap().doc.clone();
|
||||
let child = after.text_story(parent, Some(cell)).unwrap();
|
||||
assert_eq!(child.text, "LaézR", "root-only note markers must not enter child text");
|
||||
assert_eq!(child.len(), 6);
|
||||
assert_eq!(result, json!({"pos": 5, "formatted": true}));
|
||||
assert_eq!(s.doc().unwrap().selection, Selection::text(designcraft_doc::TextSel { anchor: 5, focus: 5, ..selected }));
|
||||
assert!(child.endnotes.is_empty() && child.editorial.is_empty());
|
||||
for pos in [1, 2, 4] {
|
||||
let (_, format) = child.runs().find(|(range, _)| range.contains(&pos)).unwrap();
|
||||
assert_eq!(format.over.font_style.as_deref(), Some("Bold"));
|
||||
}
|
||||
// Inspect actual run coverage: char_format_at at a run boundary means the preceding run.
|
||||
for pos in [0, 5] {
|
||||
let (_, old_format) = before_child.runs().find(|(range, _)| range.contains(&pos)).unwrap();
|
||||
let (_, new_format) = child.runs().find(|(range, _)| range.contains(&pos)).unwrap();
|
||||
assert_eq!(new_format, old_format);
|
||||
}
|
||||
assert_eq!(after.story(parent).unwrap().text, before.story(parent).unwrap().text);
|
||||
assert_eq!(after.story(parent).unwrap().chars, before.story(parent).unwrap().chars);
|
||||
assert_eq!(after.story(source), before.story(source));
|
||||
assert_eq!(s.text_clipboard, clipboard);
|
||||
|
||||
let mut pasted_history = history.clone();
|
||||
pasted_history["undo"].as_array_mut().unwrap().push(json!("Paste"));
|
||||
assert_eq!(s.execute("document.history", &json!({})).unwrap(), pasted_history);
|
||||
assert_eq!(s.execute("edit.undo", &json!({})).unwrap(), json!({"undone": "Paste"}));
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
let mut undone_history = history;
|
||||
undone_history["redo"] = json!(["Paste"]);
|
||||
assert_eq!(s.execute("document.history", &json!({})).unwrap(), undone_history);
|
||||
// This filter does not change which selection Undo restores.
|
||||
assert_eq!(s.execute("edit.redo", &json!({})).unwrap(), json!({"redone": "Paste"}));
|
||||
assert_eq!(s.doc().unwrap().doc, after);
|
||||
assert_eq!(s.execute("document.history", &json!({})).unwrap(), pasted_history);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_child_paste_filters_endnotes_and_editorial_in_cells() {
|
||||
let (mut s, source) = note_clipboard_session();
|
||||
let parent = frame(&mut s, "PZ");
|
||||
s.execute("text.select", &json!({"story": parent.0, "anchor": 1, "focus": 1})).unwrap();
|
||||
let table_id = s.execute("table.insert", &json!({"rows": 1, "cols": 2})).unwrap()["table"].as_u64().unwrap();
|
||||
s.execute("text.insert", &json!({"text": "L----R"})).unwrap();
|
||||
s.execute("table.nextCell", &json!({})).unwrap();
|
||||
s.execute("text.insert", &json!({"text": "NEIGHBOR"})).unwrap();
|
||||
s.execute("table.prevCell", &json!({})).unwrap();
|
||||
s.execute("text.move", &json!({"dir": "storyStart"})).unwrap();
|
||||
s.execute("text.move", &json!({"dir": "right"})).unwrap();
|
||||
for _ in 0..4 {
|
||||
s.execute("text.move", &json!({"dir": "right", "extend": true})).unwrap();
|
||||
}
|
||||
let before_table = s.doc().unwrap().doc.story(parent).unwrap().tables[&table_id].clone();
|
||||
let cell = designcraft_doc::CellAddr { table: table_id, row: 0, col: 0 };
|
||||
assert_child_note_paste(&mut s, source, parent, cell, (1, 5));
|
||||
let table = &s.doc().unwrap().doc.story(parent).unwrap().tables[&table_id];
|
||||
assert_eq!(table.cell(0, 1).unwrap().text.text, "NEIGHBOR");
|
||||
let mut expected_table = (*before_table).clone();
|
||||
expected_table.cells[0].text = table.cells[0].text.clone();
|
||||
assert_eq!(table.as_ref(), &expected_table);
|
||||
|
||||
// The same clipboard must still retain both markers when pasted into an ordinary root story.
|
||||
let root = frame(&mut s, "L----R");
|
||||
s.execute("text.select", &json!({"story": root.0, "anchor": 1, "focus": 5})).unwrap();
|
||||
assert_eq!(s.execute("edit.paste", &json!({})).unwrap(), json!({"pos": 11, "formatted": true}));
|
||||
assert_eq!(s.doc().unwrap().doc.story(root).unwrap().text, "La\u{E00F}\u{E010}ézR");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formatted_child_paste_filters_endnotes_and_editorial_in_footnotes() {
|
||||
let (mut s, source) = note_clipboard_session();
|
||||
let parent = frame(&mut s, "PZ");
|
||||
let neighbor = s.execute("footnote.insert", &json!({"text": "NEIGHBOR"})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("text.select", &json!({"story": parent.0, "anchor": 1, "focus": 1})).unwrap();
|
||||
let target = s.execute("footnote.insert", &json!({"text": "L----R"})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("text.move", &json!({"dir": "left"})).unwrap();
|
||||
for _ in 0..4 {
|
||||
s.execute("text.move", &json!({"dir": "left", "extend": true})).unwrap();
|
||||
}
|
||||
let before = s.doc().unwrap().doc.story(parent).unwrap().clone();
|
||||
assert_child_note_paste(&mut s, source, parent, designcraft_doc::CellAddr::footnote(target), (5, 1));
|
||||
let after = s.doc().unwrap().doc.story(parent).unwrap();
|
||||
assert_eq!(after.note(neighbor).unwrap().text.text, "NEIGHBOR");
|
||||
assert_eq!(after.note(neighbor), before.note(neighbor));
|
||||
assert_eq!(after.notes.iter().map(|note| note.id).collect::<Vec<_>>(), before.notes.iter().map(|note| note.id).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn change_case_keeps_runs() {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -16,7 +16,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
if ts.cell.is_some() {
|
||||
return Err(bad("note.new", "notes go in story text"));
|
||||
}
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
let author = str_param(p, "author").unwrap_or("").to_string();
|
||||
let at = ts.anchor.max(ts.focus);
|
||||
s.edit(|d, sel| {
|
||||
@@ -36,7 +36,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
}),
|
||||
cmd!("note.edit", "Edit Note", [], None, "{story, id, text?, author?}", has_doc, |s, p| {
|
||||
let (sid, id) = target(p)?;
|
||||
let (text, author) = (str_param(p, "text").map(str::to_string), str_param(p, "author").map(str::to_string));
|
||||
let (text, author) = (str_param(p, "text").map(|_| super::text_param(p, "text")), str_param(p, "author").map(str::to_string));
|
||||
s.edit(|d, _| {
|
||||
let st = d.story_mut(sid).ok_or_else(|| bad("note.edit", "no such story"))?;
|
||||
let n = st.editorial.iter_mut().find(|n| n.id == id).ok_or_else(|| bad("note.edit", format!("no note {id}")))?;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use designcraft_doc::{ParaFormat, SpreadRef, StoryId};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{CommandSpec, bad, cmd, has_doc, has_text, ok, str_param};
|
||||
use super::{CommandSpec, bad, cmd, has_doc, has_text, ok};
|
||||
use crate::{Result, Session};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -20,7 +20,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!("endnote.edit", "Edit Endnote", [], None, "{story, id, text}", has_doc, |s, p| {
|
||||
let (sid, id) = target(p)?;
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
s.edit(|d, _| {
|
||||
let style = d.endnote_options.para_style.clone();
|
||||
let st = d.story_mut(sid).ok_or_else(|| bad("endnote.edit", "no such story"))?;
|
||||
@@ -103,7 +103,7 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if st.doc.endnote_story == Some(ts.story) {
|
||||
return Err(bad(ID, "the endnote frame lists the endnotes"));
|
||||
}
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
let at = ts.anchor.max(ts.focus);
|
||||
s.edit(|d, sel| {
|
||||
// The endnote frame: a text frame in the margins of a new last page.
|
||||
|
||||
@@ -44,7 +44,7 @@ fn print_booklet(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, &r.bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, &r.bytes)?;
|
||||
Ok(json!({"path": path, "bytes": r.bytes.len(), "sheets": r.pages, "warnings": r.warnings}))
|
||||
}
|
||||
None => Ok(json!({"base64": super::file::base64_encode(&r.bytes), "bytes": r.bytes.len(), "sheets": r.pages, "warnings": r.warnings})),
|
||||
@@ -59,7 +59,7 @@ fn export_html(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, text.as_bytes()).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, text.as_bytes())?;
|
||||
Ok(json!({"path": path, "bytes": text.len()}))
|
||||
}
|
||||
None => Ok(json!({"bytes": text.len(), "text": text})),
|
||||
@@ -91,7 +91,7 @@ fn export_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match path {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, text.as_bytes()).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, text.as_bytes())?;
|
||||
Ok(json!({"path": path, "bytes": text.len()}))
|
||||
}
|
||||
None => Ok(json!({"bytes": text.len(), "text": text})),
|
||||
@@ -160,7 +160,7 @@ fn export_epub(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match p.get("path").and_then(Value::as_str) {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, &bytes).map_err(|e| crate::EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, &bytes)?;
|
||||
Ok(serde_json::json!({"path": path, "bytes": bytes.len()}))
|
||||
}
|
||||
None => Ok(serde_json::json!({"base64": super::file::base64_encode(&bytes), "bytes": bytes.len()})),
|
||||
@@ -598,7 +598,7 @@ fn export_pdf(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn write_file(path: &str, bytes: &[u8]) -> Result<()> {
|
||||
std::fs::write(path, bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))
|
||||
crate::atomic::write_file(path, bytes)
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn write_file(path: &str, _bytes: &[u8]) -> Result<()> {
|
||||
|
||||
+112
-42
@@ -18,16 +18,18 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(noundo "file.newSample", "Sample Document", ["Help"], None, "{} — a multi-page magazine sample", always, file_sample),
|
||||
cmd!(query "file.presets", "Document Presets", [], None, "{}", always, |_, _| Ok(serde_json::to_value(PRESETS).unwrap_or_default())),
|
||||
cmd!(noundo "file.open", "Open…", ["File"], Some("Cmd+O"),
|
||||
"{path} — .designcraft or .idml; the fonts in a `Document Fonts` folder beside it load first → {index, documentFonts: faces loaded, warnings: font files skipped}",
|
||||
"{path} — .designcraft or .idml; the fonts in a `Document Fonts` folder beside it load first → {index, documentFonts: faces loaded, warnings: font files skipped (and, for .idml, package parts missing)}",
|
||||
always, file_open),
|
||||
cmd!(noundo "file.openBytes", "Open Bytes", [], None, "{name, base64} — DesignCraft JSON or an IDML package", always, file_open_bytes),
|
||||
cmd!(noundo "file.save", "Save", ["File"], Some("Cmd+S"), "{path?}", has_doc, file_save),
|
||||
cmd!(noundo "file.saveAs", "Save As…", ["File"], Some("Cmd+Shift+S"), "{path}", 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", has_doc, |s, p| {
|
||||
let path = str_param(p, "path").ok_or_else(|| bad("file.saveACopy", "missing `path`"))?.to_string();
|
||||
let bytes = to_bytes(&s.doc()?.doc);
|
||||
let mut copy = (*s.doc()?.doc).clone();
|
||||
super::datamerge::refresh_relative_paths(&mut copy, std::path::Path::new(&path));
|
||||
let bytes = to_bytes(©);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(&path, &bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(&path, &bytes)?;
|
||||
Ok(json!({"path": path, "bytes": bytes.len()}))
|
||||
}),
|
||||
cmd!(noundo "file.revert", "Revert", ["File"], None, "{} — back to the last saved version (not undoable)", can_revert, file_revert),
|
||||
@@ -37,7 +39,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
}),
|
||||
cmd!(noundo "file.recovery.save", "Save Recovery Data", [], None, "{dir?} — write every unsaved document to the recovery folder (the app does this on a timer)", always, |s, p| {
|
||||
let dir = recovery_dir(s, p)?;
|
||||
Ok(json!({"saved": crate::recovery::save(s, &dir)?}))
|
||||
Ok(json!({"saved": crate::recovery::save_tracked(s, &dir)?}))
|
||||
}),
|
||||
cmd!(noundo "file.recovery.open", "Recover Documents", [], None, "{dir?} — reopen documents left in the recovery folder (unsaved)", always, |s, p| {
|
||||
let dir = recovery_dir(s, p)?;
|
||||
@@ -237,24 +239,26 @@ fn file_save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.map(str::to_string)
|
||||
.or_else(|| st.path.clone())
|
||||
.ok_or_else(|| bad("file.save", "missing `path` (document has never been saved)"))?;
|
||||
{
|
||||
let mut d = (*st.doc).clone();
|
||||
super::datamerge::refresh_relative_paths(&mut d, std::path::Path::new(&path));
|
||||
if d.data_merge != st.doc.data_merge {
|
||||
st.doc = Arc::new(d);
|
||||
st.revision = st.revision.saturating_add(1);
|
||||
}
|
||||
}
|
||||
let bytes = to_bytes(&st.doc);
|
||||
// Prepare paths for the destination without changing the live document until persistence succeeds.
|
||||
let mut saved = (*st.doc).clone();
|
||||
super::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 bytes = to_bytes(&saved);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(&path, &bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(&path, &bytes)?;
|
||||
if paths_changed {
|
||||
st.doc = saved;
|
||||
st.revision = st.revision.saturating_add(1);
|
||||
}
|
||||
st.path = Some(path.clone());
|
||||
st.saved_revision = st.revision;
|
||||
st.saved_doc = st.doc.clone();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(dir) = &s.recovery_dir {
|
||||
let uid = s.doc()?.uid;
|
||||
crate::recovery::discard(dir, uid);
|
||||
{
|
||||
let recovery_dir = s.recovery_dir.clone();
|
||||
let index = s.active_index().ok_or(EngineError::NoDocument)?;
|
||||
crate::recovery::discard_document(s, index, recovery_dir.as_deref());
|
||||
}
|
||||
Ok(json!({"path": path, "bytes": bytes.len()}))
|
||||
}
|
||||
@@ -313,8 +317,9 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.map(|(w, h)| (w.round().max(1.0) as u32, h.round().max(1.0) as u32))
|
||||
.ok_or_else(|| bad("file.place", "can't read that PDF page"))?,
|
||||
};
|
||||
// 72 ppi by default unless the file says otherwise; scale so it fits the page when huge.
|
||||
let (nw, nh) = eps_size.unwrap_or((pw as f64, ph as f64));
|
||||
// 72 ppi by default unless the file says otherwise; scale so it fits the page when huge. PDF pages
|
||||
// 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 frame shows that box of the PDF page.
|
||||
let crop_box = match str_param(p, "pdfCrop") {
|
||||
Some(k) if designcraft_render::is_pdf(&bytes) => {
|
||||
@@ -330,15 +335,15 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let st = s.active()?;
|
||||
st.selection.items.iter().copied().find(|i| st.doc.item(*i).is_some_and(|it| matches!(it.content, Content::Unassigned | Content::Graphic(_))))
|
||||
});
|
||||
let spread = super::spread_param(p, "spread");
|
||||
let spread = super::spread_param(p, "file.place")?;
|
||||
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);
|
||||
let want_w = p.get("width").and_then(Value::as_f64);
|
||||
s.edit(|d, sel| {
|
||||
let aid = AssetId(d.alloc());
|
||||
let fresh = AssetId(d.alloc());
|
||||
let mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
d.assets.insert(aid, Arc::new(Asset { page: pdf_page, id: aid, name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)) }));
|
||||
let aid = d.add_asset(Asset { page: pdf_page, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: Some((pw, ph)) });
|
||||
// The width the whole page (or image) gets; the height follows the proportions.
|
||||
let w = match want_w {
|
||||
Some(w) => w,
|
||||
@@ -350,14 +355,14 @@ fn file_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = if let Some(fid) = target_frame {
|
||||
let it = d.item_mut(fid).ok_or(designcraft_doc::DocError::NoItem(fid))?;
|
||||
let r = it.inner_bounds();
|
||||
// Fill frame proportionally.
|
||||
// Fill the frame proportionally once. Auto-Fit stays off, so resizing the frame crops.
|
||||
let k = (r.width() / nw).max(r.height() / nh);
|
||||
let (gw, gh) = (nw * k, nh * k);
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: aid,
|
||||
size: (nw, nh),
|
||||
xf: Affine::translate((r.x0 + (r.width() - gw) / 2.0, r.y0 + (r.height() - gh) / 2.0)) * Affine::scale(k),
|
||||
auto_fit: designcraft_doc::Fitting::FillProportionally,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [0.0; 4],
|
||||
});
|
||||
@@ -440,23 +445,20 @@ pub(crate) fn read_source(p: &Value) -> Result<(Vec<u8>, String, Option<String>)
|
||||
fn place_load(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (bytes, name, link) = read_source(p)?;
|
||||
let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad("place.load", "unsupported or corrupt image"))?;
|
||||
let natural = designcraft_render::natural_size(&bytes, 0).unwrap_or((pw as f64, ph as f64));
|
||||
let aid = s.edit(|d, _| {
|
||||
let aid = AssetId(d.alloc());
|
||||
let fresh = AssetId(d.alloc());
|
||||
let mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
d.assets.insert(
|
||||
aid,
|
||||
Arc::new(Asset { page: 0, id: aid, name: name.clone(), mime, link: link.clone(), data: Arc::new(bytes), pixels: Some((pw, ph)) }),
|
||||
);
|
||||
Ok(aid)
|
||||
Ok(d.add_asset(Asset { page: 0, id: fresh, name: name.clone(), mime, link: link.clone(), data: Arc::new(bytes), pixels: Some((pw, ph)) }))
|
||||
})?;
|
||||
s.loaded = Some((aid, (pw as f64, ph as f64)));
|
||||
s.loaded = Some((aid, natural));
|
||||
s.set_tool("placeGun");
|
||||
Ok(json!({"asset": aid.0, "name": name, "width": pw, "height": ph}))
|
||||
}
|
||||
|
||||
fn place_drop(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (aid, (nw, nh)) = s.loaded.ok_or_else(|| bad("place.drop", "nothing loaded in the place cursor"))?;
|
||||
let sr = super::spread_param(p, "spread");
|
||||
let sr = super::spread_param(p, "place.drop")?;
|
||||
let lid = s.doc()?.active_layer;
|
||||
let frame = super::id_param(p, "frame");
|
||||
let rect = super::rect_param(p, "rect");
|
||||
@@ -471,7 +473,7 @@ fn place_drop(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
asset: aid,
|
||||
size: (nw, nh),
|
||||
xf: Affine::translate((r.x0 + (r.width() - nw * k) / 2.0, r.y0 + (r.height() - nh * k) / 2.0)) * Affine::scale(k),
|
||||
auto_fit: designcraft_doc::Fitting::FillProportionally,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [0.0; 4],
|
||||
});
|
||||
@@ -520,7 +522,7 @@ fn snippet_export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, &bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, &bytes)?;
|
||||
Ok(json!({"path": path, "bytes": bytes.len(), "items": d.spreads.first().map(|s| s.items.len()).unwrap_or(0)}))
|
||||
}
|
||||
None => Ok(json!({"base64": base64_encode(&bytes), "bytes": bytes.len()})),
|
||||
@@ -548,7 +550,7 @@ fn place_layout_page(s: &mut Session, p: &Value, name: &str, bytes: &[u8]) -> Re
|
||||
if ids.is_empty() {
|
||||
return Err(bad(ID, format!("page {page} has no objects")));
|
||||
}
|
||||
let to = super::spread_param(p, "spread");
|
||||
let to = super::spread_param(p, ID)?;
|
||||
let off = match (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64)) {
|
||||
(Some(x), Some(y)) => designcraft_geom::Vec2::new(x - pr.x0, y - pr.y0),
|
||||
_ => designcraft_geom::Vec2::new(-pr.x0, -pr.y0),
|
||||
@@ -584,7 +586,7 @@ pub(crate) fn snippet_place(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if ids.is_empty() {
|
||||
return Err(bad("snippet.place", "the snippet is empty"));
|
||||
}
|
||||
let sr = super::spread_param(p, "spread");
|
||||
let sr = super::spread_param(p, "snippet.place")?;
|
||||
let bounds = ids.iter().filter_map(|i| src.item(*i).map(|it| it.bounds())).reduce(|a, b| a.union(b)).unwrap_or(Rect::ZERO);
|
||||
let off = match (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64)) {
|
||||
(Some(x), Some(y)) => designcraft_geom::Vec2::new(x - bounds.x0, y - bounds.y0),
|
||||
@@ -645,15 +647,14 @@ fn file_revert(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
// As opened again: the fonts beside it now (unchanged files are shared, not read again).
|
||||
let (fonts, _, _) = load_document_fonts(&mut d, &path);
|
||||
let uid = s.doc()?.uid;
|
||||
let st = s.doc_mut()?;
|
||||
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;
|
||||
fresh.revision = st.revision + 1;
|
||||
*st = fresh;
|
||||
if let Some(dir) = &s.recovery_dir {
|
||||
crate::recovery::discard(dir, uid);
|
||||
}
|
||||
fresh.revision = s.doc()?.revision + 1;
|
||||
crate::recovery::discard_document(s, index, recovery_dir.as_deref());
|
||||
*s.doc_mut()? = fresh;
|
||||
Ok(json!({"path": path}))
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@@ -689,6 +690,24 @@ mod pdf_crop_tests {
|
||||
assert!((trim.width() - 612.0).abs() < 1e-3 && (trim.height() - 792.0).abs() < 1e-3, "{trim:?}");
|
||||
assert!(t.execute("file.place", &json!({"base64": b64, "name": "a.pdf", "pdfCrop": "nope"})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placed_pdf_keeps_its_exact_page_size() {
|
||||
// A 100.4 x 50.4 pt page placed 100.4 pt wide is 50.4 pt tall, not stretched to whole points.
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 100.4, "height": 50.4, "pages": 1})).unwrap();
|
||||
let b64 = s.execute("file.exportPdf", &json!({})).unwrap()["base64"].as_str().unwrap().to_string();
|
||||
let mut t = Session::new();
|
||||
t.execute("file.new", &json!({})).unwrap();
|
||||
let r = t.execute("file.place", &json!({"base64": b64, "name": "a.pdf", "width": 100.4, "x": 0, "y": 0})).unwrap();
|
||||
let id = designcraft_doc::ItemId(r["id"].as_u64().unwrap());
|
||||
let d = &t.doc().unwrap().doc;
|
||||
let it = d.item(id).unwrap();
|
||||
let b = it.bounds();
|
||||
assert!((b.width() - 100.4).abs() < 1e-3 && (b.height() - 50.4).abs() < 1e-3, "{b:?}");
|
||||
let designcraft_doc::Content::Graphic(g) = &it.content else { panic!("not a graphic") };
|
||||
assert!((g.size.0 - 100.4).abs() < 1e-3 && (g.size.1 - 50.4).abs() < 1e-3, "{:?}", g.size);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -719,6 +738,37 @@ mod layout_place_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod save_tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// Saving over an existing document replaces it whole and leaves nothing beside it.
|
||||
#[test]
|
||||
fn save_replaces_the_existing_file_without_leftovers() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-save-atomic-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("doc.designcraft");
|
||||
std::fs::write(&path, vec![b'x'; 1 << 20]).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text", "text": "Saved whole", "caret": false})).unwrap();
|
||||
s.execute("file.save", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
s.execute("file.saveACopy", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let saved = super::from_bytes(&std::fs::read(&path).unwrap()).unwrap();
|
||||
assert!(saved.stories.values().any(|st| st.text == "Saved whole"));
|
||||
let names: Vec<_> = std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().file_name()).collect();
|
||||
assert_eq!(names, ["doc.designcraft"], "no temporary file is left");
|
||||
// A folder that doesn't exist: an error naming the path.
|
||||
let missing = dir.join("missing").join("doc.designcraft");
|
||||
let e = s.execute("file.save", &json!({"path": missing.to_string_lossy()})).unwrap_err();
|
||||
assert!(e.to_string().contains("missing"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod document_fonts_tests {
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -939,4 +989,24 @@ mod document_fonts_tests {
|
||||
assert!(glyph_faces(&s).iter().all(|(_, f)| f.family == FAMILY));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bold_and_italic_link_the_document_fonts_styles() {
|
||||
const FAMILY: &str = "DocFont Linked";
|
||||
let dir = temp_dir("linked");
|
||||
std::fs::write(dir.join(DOCUMENT_FONTS_FOLDER).join("linked.ttf"), font_with(FAMILY, &['H', 'e', 'l', 'o']).unwrap()).unwrap();
|
||||
let path = dir.join("Linked.designcraft");
|
||||
write_document(&path, FAMILY);
|
||||
|
||||
let mut s = Session::new();
|
||||
open(&mut s, &path);
|
||||
let sid = *s.doc().unwrap().doc.stories.keys().next().unwrap();
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 0, "focus": 0})).unwrap();
|
||||
// The family is the document's alone: its Regular is the style without bold or italic.
|
||||
let r = s.execute("type.bold", &json!({"on": false})).unwrap();
|
||||
assert_eq!(r["fontStyle"], "Regular", "{r}");
|
||||
let r = s.execute("type.italic", &json!({"on": false})).unwrap();
|
||||
assert_eq!(r["fontStyle"], "Regular", "{r}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
+881
-41
File diff suppressed because it is too large
Load Diff
@@ -21,14 +21,14 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"New Guide",
|
||||
[],
|
||||
None,
|
||||
"{orientation: horizontal|vertical, position (spread coordinate), spread?, page? (index in the spread; default: the page under `at` or the position), at? (the other coordinate), spreadGuide?: bool} → {page, index}; {orientation: both, point: [x, y], …} adds a horizontal guide through y and a vertical guide through x as one undo step → {guides: [{page, index}, {page, index}]}",
|
||||
"{orientation: horizontal|vertical, position (spread coordinate), spread?, page? (index in the spread; default: the page under `at` or the position), at? (the other coordinate), spreadGuide?: bool, liquid?: bool (a liquid guide, as the Page tool draws from the rulers)} → {page, index}; {orientation: both, point: [x, y], …} adds a horizontal guide through y and a vertical guide through x as one undo step → {guides: [{page, index}, {page, index}]}",
|
||||
has_doc,
|
||||
add
|
||||
),
|
||||
cmd!("guide.move", "Move Guide", [], None, "{spread?, page, index, position}", has_doc, move_guide),
|
||||
cmd!("guide.delete", "Delete Guide", [], None, "{spread?, page, index}", has_doc, delete),
|
||||
cmd!("guide.deleteAll", "Delete All Guides on Spread", ["View", "Grids & Guides"], None, "{spread?}", has_doc, |s, p| {
|
||||
let r = guide_spread(p, "guide.deleteAll")?;
|
||||
let r = super::spread_param(p, "guide.deleteAll")?;
|
||||
s.edit(|d, _| {
|
||||
let sp = d.spread_mut(r).ok_or_else(|| bad("guide.deleteAll", "no such spread"))?;
|
||||
let mut n = 0;
|
||||
@@ -41,7 +41,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
}),
|
||||
cmd!(query "guide.list", "Guides", [], None, "{spread?} → [{page, index, orientation, position, spread}]", has_doc, |s, p| {
|
||||
let d = &s.doc()?.doc;
|
||||
let sp = d.spread(guide_spread(p, "guide.list")?).ok_or_else(|| bad("guide.list", "no such spread"))?;
|
||||
let sp = d.spread(super::spread_param(p, "guide.list")?).ok_or_else(|| bad("guide.list", "no such spread"))?;
|
||||
let mut out = Vec::new();
|
||||
for (pi, pg) in sp.pages.iter().enumerate() {
|
||||
for (i, g) in pg.guides.iter().enumerate() {
|
||||
@@ -71,23 +71,6 @@ fn optional_usize(p: &Value, key: &str, command: &str) -> Result<Option<usize>>
|
||||
p.get(key).map(|value| checked_usize(value, key, command)).transpose()
|
||||
}
|
||||
|
||||
/// Numeric spread shorthand or the serialized `SpreadRef` form, without lossy/defaulting casts.
|
||||
fn guide_spread(p: &Value, command: &str) -> Result<SpreadRef> {
|
||||
let Some(value) = p.get("spread") else { return Ok(SpreadRef::Doc(0)) };
|
||||
if value.is_number() {
|
||||
return Ok(SpreadRef::Doc(checked_usize(value, "spread", command)?));
|
||||
}
|
||||
let object = value.as_object().ok_or_else(|| bad(command, "`spread` must be a non-negative integer or a spread reference"))?;
|
||||
let kind = object.get("kind").and_then(Value::as_str).ok_or_else(|| bad(command, "`spread.kind` must be `doc` or `parent`"))?;
|
||||
let index = object.get("index").ok_or_else(|| bad(command, "missing `spread.index`"))?;
|
||||
let index = checked_usize(index, "spread.index", command)?;
|
||||
match kind {
|
||||
"doc" => Ok(SpreadRef::Doc(index)),
|
||||
"parent" => Ok(SpreadRef::Parent(index)),
|
||||
_ => Err(bad(command, "`spread.kind` must be `doc` or `parent`")),
|
||||
}
|
||||
}
|
||||
|
||||
fn finite_number(p: &Value, key: &str, command: &str) -> Result<Option<f64>> {
|
||||
let Some(value) = p.get(key) else { return Ok(None) };
|
||||
let number = value.as_f64().ok_or_else(|| bad(command, format!("`{key}` must be a finite number")))?;
|
||||
@@ -111,8 +94,9 @@ fn page_at(d: &Document, r: SpreadRef, x: f64) -> usize {
|
||||
}
|
||||
|
||||
fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let r = guide_spread(p, "guide.add")?;
|
||||
let r = super::spread_param(p, "guide.add")?;
|
||||
let spread_guide = p.get("spreadGuide").and_then(Value::as_bool).unwrap_or(false);
|
||||
let liquid = p.get("liquid").and_then(Value::as_bool).unwrap_or(false);
|
||||
let page = optional_usize(p, "page", "guide.add")?;
|
||||
// (orientation, position, x used to find the page)
|
||||
let guides: Vec<(Orientation, f64, f64)> = if str_param(p, "orientation") == Some("both") {
|
||||
@@ -149,7 +133,14 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut out = Vec::new();
|
||||
for (o, pos, pi) in placed {
|
||||
let pg = sp.pages.get_mut(pi).ok_or_else(|| bad("guide.add", "no such page"))?;
|
||||
pg.guides.push(Guide { orientation: o, position: pos, spread: spread_guide, locked: false, layer: Some(layer), liquid: false });
|
||||
pg.guides.push(Guide {
|
||||
orientation: o,
|
||||
position: pos,
|
||||
spread: spread_guide,
|
||||
locked: false,
|
||||
layer: Some(layer),
|
||||
liquid: liquid && !spread_guide,
|
||||
});
|
||||
out.push(json!({"page": pi, "index": pg.guides.len() - 1}));
|
||||
}
|
||||
match <[Value; 1]>::try_from(out) {
|
||||
@@ -162,7 +153,7 @@ fn add(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn guide_ref(p: &Value, cmd: &str) -> Result<(SpreadRef, usize, usize)> {
|
||||
let pi = optional_usize(p, "page", cmd)?.ok_or_else(|| bad(cmd, "missing page"))?;
|
||||
let i = optional_usize(p, "index", cmd)?.ok_or_else(|| bad(cmd, "missing index"))?;
|
||||
Ok((guide_spread(p, cmd)?, pi, i))
|
||||
Ok((super::spread_param(p, cmd)?, pi, i))
|
||||
}
|
||||
|
||||
/// Commands must use the same guide/layer protection as pointer editing.
|
||||
@@ -260,7 +251,7 @@ fn create_guides(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cg = guide_gutter(p, "columnGutter")?;
|
||||
let to_page = str_param(p, "fitTo") == Some("page");
|
||||
let remove = p.get("removeExisting").and_then(Value::as_bool).unwrap_or(false);
|
||||
let r = guide_spread(p, "layout.createGuides")?;
|
||||
let r = super::spread_param(p, "layout.createGuides")?;
|
||||
let only = optional_usize(p, "page", "layout.createGuides")?;
|
||||
let page_count = {
|
||||
let sp = s.doc()?.doc.spread(r).ok_or_else(|| bad("layout.createGuides", "no such spread"))?;
|
||||
|
||||
@@ -13,7 +13,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"{path?, embedImages?: true} — writes an IDML package to `path`, or returns {base64} without a path",
|
||||
has_doc, export_idml),
|
||||
cmd!(noundo "file.openIdml", "Open IDML", [], None,
|
||||
"{path | base64, name?} — opens an IDML package as a new document (linked images are read next to the file or from its Links/ folder; the fonts in a `Document Fonts` folder beside it load first) → {index, documentFonts, warnings}",
|
||||
"{path | base64, name?} — opens an IDML package as a new document (linked images are read next to the file or from its Links/ folder; the fonts in a `Document Fonts` folder beside it load first) → {index, documentFonts, warnings: package parts missing, font files skipped}",
|
||||
always, open_idml),
|
||||
]
|
||||
}
|
||||
@@ -25,7 +25,7 @@ fn export_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, &bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, &bytes)?;
|
||||
Ok(json!({"path": path, "bytes": bytes.len()}))
|
||||
}
|
||||
None => Ok(json!({"base64": base64_encode(&bytes), "bytes": bytes.len()})),
|
||||
@@ -34,6 +34,11 @@ fn export_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
/// Import IDML bytes; `dir` is the folder the package came from (for relative link lookup).
|
||||
pub fn import(bytes: &[u8], dir: Option<&std::path::Path>) -> Result<Document> {
|
||||
import_report(bytes, dir).map(|(document, _)| document)
|
||||
}
|
||||
|
||||
/// [`import`], with the problems that did not stop it (parts missing from the package, …).
|
||||
pub fn import_report(bytes: &[u8], dir: Option<&std::path::Path>) -> Result<(Document, Vec<String>)> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let resolved = std::cell::RefCell::new(std::collections::HashMap::new());
|
||||
let read = |link: &str| -> Option<Vec<u8>> {
|
||||
@@ -49,7 +54,8 @@ pub fn import(bytes: &[u8], dir: Option<&std::path::Path>) -> Result<Document> {
|
||||
None
|
||||
}
|
||||
};
|
||||
let document = designcraft_idml::import_idml_with(bytes, &read).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let designcraft_idml::Imported { document, warnings } =
|
||||
designcraft_idml::import_idml_report(bytes, &read).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let document = {
|
||||
let mut document = document;
|
||||
@@ -62,7 +68,7 @@ pub fn import(bytes: &[u8], dir: Option<&std::path::Path>) -> Result<Document> {
|
||||
resolve_packaged_links(&mut document, dir);
|
||||
document
|
||||
};
|
||||
Ok(document)
|
||||
Ok((document, warnings))
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -142,14 +148,15 @@ pub(crate) fn open_idml(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
} else {
|
||||
return Err(bad("file.openIdml", "missing `path` or `base64`"));
|
||||
};
|
||||
let mut d = import(&bytes, dir.as_deref())?;
|
||||
let (mut d, mut warnings) = import_report(&bytes, dir.as_deref())?;
|
||||
if let Some(n) = name {
|
||||
d.title = n;
|
||||
}
|
||||
let (fonts, faces, warnings) = match str_param(p, "path").filter(|_| str_param(p, "base64").is_none()) {
|
||||
let (fonts, faces, font_warnings) = match str_param(p, "path").filter(|_| str_param(p, "base64").is_none()) {
|
||||
Some(path) => super::file::load_document_fonts(&mut d, path),
|
||||
None => (None, 0, Vec::new()),
|
||||
};
|
||||
warnings.extend(font_warnings);
|
||||
// Never save over the .idml with the native format: the document starts unsaved.
|
||||
let mut st = DocState::new(d, None);
|
||||
st.fonts = fonts;
|
||||
|
||||
+158
-19
@@ -135,21 +135,63 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
ok()
|
||||
})
|
||||
}),
|
||||
cmd!("layout.pages.duplicateSpread", "Duplicate Spread", ["Layout", "Pages"], None, "{spread}", has_doc, |s, p| {
|
||||
let si = p.get("spread").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
s.edit(|d, _| {
|
||||
let sp = d.spreads.get(si).cloned().ok_or_else(|| bad("layout.pages.duplicateSpread", "no such spread"))?;
|
||||
let after = d.first_page_of_spread(si) + sp.pages.len() - 1;
|
||||
let parent = sp.pages.first().and_then(|p| p.parent);
|
||||
d.insert_pages(Some(after), sp.pages.len(), parent)?;
|
||||
// Copy items onto the new spread(s) at the same positions.
|
||||
let src = d.clone();
|
||||
let ids: Vec<_> = sp.items.iter().map(|i| i.id).collect();
|
||||
let (nsi, _) = d.page_loc(after + 1).ok_or_else(|| bad("layout.pages.duplicateSpread", "insert failed"))?;
|
||||
super::object::duplicate_from(d, &src, &ids, designcraft_doc::SpreadRef::Doc(nsi), designcraft_geom::Vec2::ZERO)?;
|
||||
ok()
|
||||
})
|
||||
}),
|
||||
cmd!(
|
||||
"layout.pages.duplicateSpread",
|
||||
"Duplicate Spread",
|
||||
["Layout", "Pages"],
|
||||
None,
|
||||
"{spread, atEnd?: bool (Duplicate Spread at End of Document; the Pages panel's New Page button takes a dropped page there), to?: page index the copied pages move to, in the same undo step (the Pages panel's Alt-drag)} → {page: the copy's first page (0-based)} — the spread's pages (size, margins, columns, guides, liquid rule) and items",
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let si = p.get("spread").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
let at_end = p.get("atEnd").and_then(Value::as_bool).unwrap_or(false);
|
||||
let to = p.get("to").and_then(Value::as_u64).map(|t| t as usize);
|
||||
s.edit(|d, _| {
|
||||
let sp = d.spreads.get(si).cloned().ok_or_else(|| bad("layout.pages.duplicateSpread", "no such spread"))?;
|
||||
// The copies land at `to`..`to + n` of the document with them in it.
|
||||
if to.is_some_and(|t| t > d.page_count()) {
|
||||
return Err(bad("layout.pages.duplicateSpread", "no such page"));
|
||||
}
|
||||
let after = if at_end { d.page_count() - 1 } else { d.first_page_of_spread(si) + sp.pages.len() - 1 };
|
||||
let parent = sp.pages.first().and_then(|p| p.parent);
|
||||
d.insert_pages(Some(after), sp.pages.len(), parent)?;
|
||||
// The copies are the pages they copy: size, margins, columns, guides, liquid rule.
|
||||
for (k, src) in sp.pages.iter().enumerate() {
|
||||
let Some((nsi, npi)) = d.page_loc(after + 1 + k) else { continue };
|
||||
let nsp = std::sync::Arc::make_mut(&mut d.spreads[nsi]);
|
||||
let pg = &mut nsp.pages[npi];
|
||||
let dx = pg.x - src.x;
|
||||
(pg.width, pg.height, pg.margins, pg.columns, pg.liquid) =
|
||||
(src.width, src.height, src.margins, src.columns.clone(), src.liquid);
|
||||
pg.guides = src.guides.clone();
|
||||
for g in pg.guides.iter_mut().filter(|g| g.orientation == designcraft_doc::Orientation::Vertical) {
|
||||
g.position += dx;
|
||||
}
|
||||
nsp.relayout();
|
||||
}
|
||||
// Copy items onto the new spread(s) at the same positions.
|
||||
let src = d.clone();
|
||||
let ids: Vec<_> = sp.items.iter().map(|i| i.id).collect();
|
||||
let (nsi, _) = d.page_loc(after + 1).ok_or_else(|| bad("layout.pages.duplicateSpread", "insert failed"))?;
|
||||
super::object::duplicate_from(d, &src, &ids, designcraft_doc::SpreadRef::Doc(nsi), designcraft_geom::Vec2::ZERO)?;
|
||||
let first = after + 1;
|
||||
let Some(to) = to else { return Ok(json!({"page": first})) };
|
||||
// Moving a page shifts the ones between; order the moves so each copy keeps
|
||||
// its place among the others.
|
||||
let n = sp.pages.len();
|
||||
if first < to {
|
||||
for k in (0..n).rev() {
|
||||
d.move_page(first + k, to + k)?;
|
||||
}
|
||||
} else {
|
||||
for k in 0..n {
|
||||
d.move_page(first + k, to + k)?;
|
||||
}
|
||||
}
|
||||
Ok(json!({"page": to}))
|
||||
})
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"layout.pages.applyParent",
|
||||
"Apply Parent to Pages…",
|
||||
@@ -376,10 +418,48 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Page Size",
|
||||
[],
|
||||
None,
|
||||
"{pages: [1-based page numbers], width?, height?, preset?: \"A4\"|\"Letter\"|…} — pages of their own size (Page tool); objects on the pages keep their place on them",
|
||||
"{pages: [1-based page numbers], width?, height?, preset?: \"A4\"|\"Letter\"|…, anchor?: 0–8 (reference point that stays put, row-major from the top-left; default 0)} — pages of their own size (Page tool); objects keep their place relative to the anchor, or follow the page's liquid rule",
|
||||
has_doc,
|
||||
page_size
|
||||
),
|
||||
cmd!(
|
||||
"layout.pageOptions",
|
||||
"Page Options",
|
||||
[],
|
||||
None,
|
||||
"{pages: [1-based], showParentOverlay?: bool} — Page tool options (Show Parent Page Overlay: the parent page's size drawn over the page)",
|
||||
has_doc,
|
||||
page_options
|
||||
),
|
||||
cmd!(
|
||||
noundo "layout.selectPage",
|
||||
"Select Page",
|
||||
[],
|
||||
None,
|
||||
"{page: 1-based | null, ids?: [item ids] (objects selected with it: the Page tool on an object-based page)} — the page the Page tool selects (its handles, the Control panel)",
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let n = s.doc()?.doc.page_count();
|
||||
let page = match p.get("page") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(v.as_u64().filter(|v| *v >= 1 && *v as usize <= n).ok_or_else(|| bad("layout.selectPage", format!("`page`: 1–{n}")))? as usize - 1),
|
||||
};
|
||||
let ids: Vec<designcraft_doc::ItemId> = {
|
||||
let d = &s.doc()?.doc;
|
||||
p.get("ids")
|
||||
.and_then(Value::as_array)
|
||||
.map(|a| a.iter().filter_map(Value::as_u64).map(designcraft_doc::ItemId).filter(|id| d.item(*id).is_some()).collect())
|
||||
.unwrap_or_default()
|
||||
};
|
||||
let st = s.doc_mut()?;
|
||||
if page.is_some() {
|
||||
st.selection = designcraft_doc::Selection::items(ids);
|
||||
}
|
||||
st.selection.page = page;
|
||||
st.revision += 1;
|
||||
Ok(json!({"page": page.map(|p| p + 1)}))
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"layout.section",
|
||||
"Numbering & Section Options…",
|
||||
@@ -640,8 +720,32 @@ fn page_size(s: &mut crate::Session, p: &Value) -> Result<Value> {
|
||||
w = Some(pr.width);
|
||||
h = Some(pr.height);
|
||||
}
|
||||
let anchor = match p.get("anchor") {
|
||||
None | Some(Value::Null) => 0,
|
||||
Some(v) => v.as_u64().filter(|a| *a <= 8).ok_or_else(|| bad("layout.pageSize", "`anchor`: 0–8"))? as usize,
|
||||
};
|
||||
s.edit(|d, _| {
|
||||
resize_pages(d, &pages, w, h);
|
||||
resize_pages_from(d, &pages, w, h, anchor);
|
||||
Ok(json!({"pages": pages.len()}))
|
||||
})
|
||||
}
|
||||
|
||||
fn page_options(s: &mut crate::Session, p: &Value) -> Result<Value> {
|
||||
let pages: Vec<usize> =
|
||||
p.get("pages").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).map(|n| n as usize).collect()).unwrap_or_default();
|
||||
let n = s.doc()?.doc.page_count();
|
||||
if pages.is_empty() || pages.iter().any(|x| *x == 0 || *x > n) {
|
||||
return Err(bad("layout.pageOptions", "give `pages` (1-based)"));
|
||||
}
|
||||
let overlay = p.get("showParentOverlay").and_then(Value::as_bool);
|
||||
s.edit(|d, _| {
|
||||
for page in &pages {
|
||||
let Some((si, pi)) = d.page_loc(page - 1) else { continue };
|
||||
let pg = &mut std::sync::Arc::make_mut(&mut d.spreads[si]).pages[pi];
|
||||
if let Some(v) = overlay {
|
||||
pg.show_parent_overlay = v;
|
||||
}
|
||||
}
|
||||
Ok(json!({"pages": pages.len()}))
|
||||
})
|
||||
}
|
||||
@@ -649,14 +753,35 @@ fn page_size(s: &mut crate::Session, p: &Value) -> Result<Value> {
|
||||
/// Give `pages` (1-based) their own size. Objects stay on their page; pages with a liquid rule
|
||||
/// re-flow their objects by it.
|
||||
pub(crate) fn resize_pages(d: &mut designcraft_doc::Document, pages: &[usize], w: Option<f64>, h: Option<f64>) {
|
||||
resize_pages_from(d, pages, w, h, 0);
|
||||
}
|
||||
|
||||
/// [`resize_pages`] keeping reference point `anchor` (0–8, row-major from the top-left) in place:
|
||||
/// the page grows away from it, so objects keep their place relative to it.
|
||||
pub(crate) fn resize_pages_from(d: &mut designcraft_doc::Document, pages: &[usize], w: Option<f64>, h: Option<f64>, anchor: usize) {
|
||||
let (fx, fy) = ((anchor % 3) as f64 / 2.0, (anchor / 3).min(2) as f64 / 2.0);
|
||||
{
|
||||
for page in pages {
|
||||
let Some((si, pi)) = d.page_loc(page - 1) else { continue };
|
||||
let sp = std::sync::Arc::make_mut(&mut d.spreads[si]);
|
||||
let old: Vec<f64> = sp.pages.iter().map(|x| x.x).collect();
|
||||
let old_bounds = sp.pages[pi].bounds();
|
||||
// The page's own objects (outer pages take the pasteboard beside them).
|
||||
let last = sp.pages.len() - 1;
|
||||
let (lo, hi) = (if pi == 0 { f64::NEG_INFINITY } else { old_bounds.x0 }, if pi == last { f64::INFINITY } else { old_bounds.x1 });
|
||||
let own: Vec<usize> = sp
|
||||
.items
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, it)| {
|
||||
let c = it.bounds().center().x;
|
||||
c >= lo && c < hi
|
||||
})
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
let pg = &mut sp.pages[pi];
|
||||
let dw = w.map_or(0.0, |w| w.max(1.0) - pg.width);
|
||||
let dh = h.map_or(0.0, |h| h.max(1.0) - pg.height);
|
||||
if let Some(w) = w {
|
||||
pg.width = w.max(1.0);
|
||||
}
|
||||
@@ -693,8 +818,22 @@ pub(crate) fn resize_pages(d: &mut designcraft_doc::Document, pages: &[usize], w
|
||||
}
|
||||
}
|
||||
}
|
||||
// Liquid rules: objects were carried along with the page's left edge.
|
||||
let moved = old_bounds + designcraft_geom::Vec2::new(new[pi] - old[pi], 0.0);
|
||||
// Growing away from the anchor: the page's objects and guides keep their place
|
||||
// relative to it.
|
||||
let shift = designcraft_geom::Vec2::new(fx * dw, fy * dh);
|
||||
if shift != designcraft_geom::Vec2::ZERO {
|
||||
for i in &own {
|
||||
if let Some(it) = sp.items.get_mut(*i) {
|
||||
let it = std::sync::Arc::make_mut(it);
|
||||
it.xf = designcraft_geom::Affine::translate(shift) * it.xf;
|
||||
}
|
||||
}
|
||||
for g in &mut sp.pages[pi].guides {
|
||||
g.position += if g.orientation == designcraft_doc::Orientation::Vertical { shift.x } else { shift.y };
|
||||
}
|
||||
}
|
||||
// Liquid rules: objects were carried along with the page's left edge (and the anchor).
|
||||
let moved = old_bounds + designcraft_geom::Vec2::new(new[pi] - old[pi], 0.0) + shift;
|
||||
super::liquid::apply(d, designcraft_doc::SpreadRef::Doc(si), pi, moved);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl Library {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(p) = &self.path {
|
||||
let bytes = serde_json::to_vec_pretty(self).map_err(|e| crate::EngineError::Other(e.to_string()))?;
|
||||
std::fs::write(p, bytes).map_err(|e| crate::EngineError::Other(format!("{p}: {e}")))?;
|
||||
crate::atomic::write_file(p, &bytes)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -86,10 +86,12 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
let n = st.selection.items.len();
|
||||
(str_param(p, "name").map(str::to_string).unwrap_or_else(|| if n > 1 { format!("{n} objects") } else { kind.clone() }), str_param(p, "description").unwrap_or("").to_string())
|
||||
};
|
||||
let lib = s.library.as_mut().ok_or_else(no_library)?;
|
||||
let mut lib = s.library.as_ref().ok_or_else(no_library)?.clone();
|
||||
let index = lib.items.len();
|
||||
lib.items.push(LibraryItem { name, description, snippet: super::file::base64_encode(&snip) });
|
||||
lib.save()?;
|
||||
Ok(json!({"index": lib.items.len() - 1}))
|
||||
s.library = Some(lib);
|
||||
Ok(json!({"index": index}))
|
||||
}),
|
||||
cmd!(
|
||||
"library.place",
|
||||
@@ -107,13 +109,14 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
}
|
||||
),
|
||||
cmd!(noundo "library.remove", "Delete Item", [], None, "{index}", has_library, |s, p| {
|
||||
let lib = s.library.as_mut().ok_or_else(no_library)?;
|
||||
let mut lib = s.library.as_ref().ok_or_else(no_library)?.clone();
|
||||
let i = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad("library.remove", "`index` required"))? as usize;
|
||||
if i >= lib.items.len() {
|
||||
return Err(bad("library.remove", format!("no item {i}")));
|
||||
}
|
||||
lib.items.remove(i);
|
||||
lib.save()?;
|
||||
s.library = Some(lib);
|
||||
ok()
|
||||
}),
|
||||
cmd!(noundo "conveyor.collect", "Collect", [], None, "{ids? (default: the selection)} — each object onto the Content Collector conveyor → {count}", has_selection, |s, p| {
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
|s, p| {
|
||||
let parent = StoryId(p.get("story").and_then(Value::as_u64).ok_or_else(|| bad("story.placeAndLink", "missing story"))?);
|
||||
let rect = rect_param(p, "rect").ok_or_else(|| bad("story.placeAndLink", "missing rect"))?;
|
||||
let sr = spread_param(p, "spread");
|
||||
let sr = spread_param(p, "story.placeAndLink")?;
|
||||
let layer = s.doc()?.active_layer;
|
||||
s.edit(|d, sel| {
|
||||
if d.story(parent).is_none() {
|
||||
|
||||
@@ -112,8 +112,10 @@ pub fn status(a: &designcraft_doc::Asset) -> &'static str {
|
||||
}
|
||||
|
||||
fn list(d: &Document) -> Vec<Value> {
|
||||
let used = d.used_assets();
|
||||
d.assets
|
||||
.values()
|
||||
.filter(|a| used.contains(&a.id))
|
||||
.map(|a| {
|
||||
let uses: Vec<Value> = uses(d, a.id)
|
||||
.into_iter()
|
||||
@@ -311,3 +313,118 @@ mod relink_folder_tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shared_asset_tests {
|
||||
use super::*;
|
||||
|
||||
fn png(w: u32, h: u32) -> Vec<u8> {
|
||||
designcraft_render::Rendered { width: w, height: h, pixels: vec![200u8; (w * h * 4) as usize] }.to_png()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placements_of_one_file_share_an_asset_and_saves_drop_replaced_ones() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let a = super::super::file::base64_encode(&png(10, 10));
|
||||
s.execute("file.place", &json!({"base64": a, "name": "a.png", "x": 72, "y": 72})).unwrap();
|
||||
s.execute("edit.deselectAll", &json!({})).unwrap();
|
||||
s.execute("file.place", &json!({"base64": a, "name": "a.png", "x": 200, "y": 72})).unwrap();
|
||||
let l = s.execute("links.list", &json!({})).unwrap();
|
||||
assert_eq!(l.as_array().unwrap().len(), 1);
|
||||
assert_eq!(l[0]["uses"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(s.doc().unwrap().doc.assets.len(), 1);
|
||||
|
||||
// A new image into a frame: the old asset stays in memory (undo) but not in the file.
|
||||
let f = s.execute("frame.create", &json!({"rect": [100, 300, 200, 400]})).unwrap()["id"].as_u64().unwrap();
|
||||
let b = super::super::file::base64_encode(&png(20, 10));
|
||||
let c = super::super::file::base64_encode(&png(30, 10));
|
||||
s.execute("file.place", &json!({"base64": b, "name": "b.png", "frame": f})).unwrap();
|
||||
s.execute("file.place", &json!({"base64": c, "name": "c.png", "frame": f})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert_eq!(d.assets.len(), 3);
|
||||
assert_eq!(s.execute("links.list", &json!({})).unwrap().as_array().unwrap().len(), 2);
|
||||
let back = designcraft_format::load(&designcraft_format::save(&s.doc().unwrap().doc).unwrap()).unwrap();
|
||||
let mut names: Vec<&str> = back.assets.values().map(|a| a.name.as_str()).collect();
|
||||
names.sort();
|
||||
assert_eq!(names, ["a.png", "c.png"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_with_identical_bytes_at_different_paths_keep_their_own_links() {
|
||||
let dir = std::env::temp_dir().join(format!("dc-same-bytes-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let (a, b) = (dir.join("a.png"), dir.join("b.png"));
|
||||
std::fs::write(&a, png(10, 10)).unwrap();
|
||||
std::fs::write(&b, png(10, 10)).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
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();
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().doc.assets.len(), 2);
|
||||
let rows = |s: &mut Session| -> Vec<(String, Value)> {
|
||||
let l = s.execute("links.list", &json!({})).unwrap();
|
||||
l.as_array().unwrap().iter().map(|r| (r["path"].as_str().unwrap().to_string(), r["pixels"].clone())).collect()
|
||||
};
|
||||
let paths = |r: &[(String, Value)]| r.iter().map(|(p, _)| p.clone()).collect::<Vec<_>>();
|
||||
let before = rows(&mut s);
|
||||
assert_eq!(paths(&before), [a.to_string_lossy(), b.to_string_lossy()]);
|
||||
|
||||
// b.png changes on disk: updating it leaves a.png's placement alone.
|
||||
std::fs::write(&b, png(20, 10)).unwrap();
|
||||
assert_eq!(s.execute("links.update", &json!({})).unwrap()["updated"], 1);
|
||||
let after = rows(&mut s);
|
||||
assert_eq!(paths(&after), paths(&before));
|
||||
assert_eq!((after[0].1.clone(), after[1].1.clone()), (json!([10, 10]), json!([20, 10])));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pasted_graphics_reuse_the_identical_asset_of_the_target() {
|
||||
use designcraft_doc::build::NewDocument;
|
||||
use designcraft_doc::{Asset, Graphic, Item, ItemId, Shape, SpreadRef};
|
||||
let asset = |id: u64, data: Vec<u8>| Asset {
|
||||
id: AssetId(id),
|
||||
name: "p.png".into(),
|
||||
mime: "image/png".into(),
|
||||
link: None,
|
||||
data: Arc::new(data),
|
||||
pixels: None,
|
||||
page: 0,
|
||||
};
|
||||
let mut src = Document::new(&NewDocument::default());
|
||||
let mut dst = Document::new(&NewDocument::default());
|
||||
let (pa, pb) = (png(4, 4), png(8, 4));
|
||||
// The source's asset id is taken in the target by a different file.
|
||||
let sid = AssetId(src.alloc());
|
||||
src.assets.insert(sid, Arc::new(asset(sid.0, pa.clone())));
|
||||
dst.assets.insert(sid, Arc::new(asset(sid.0, pb)));
|
||||
dst.alloc();
|
||||
let same = AssetId(dst.alloc());
|
||||
assert_ne!(same, sid);
|
||||
dst.assets.insert(same, Arc::new(asset(same.0, pa)));
|
||||
let iid = ItemId(src.alloc());
|
||||
let mut it = Item::new(
|
||||
iid,
|
||||
src.default_layer(),
|
||||
Shape::Rectangle,
|
||||
designcraft_geom::shapes::rectangle(designcraft_geom::Rect::new(0.0, 0.0, 9.0, 9.0)),
|
||||
);
|
||||
it.content = Content::Graphic(Graphic {
|
||||
asset: sid,
|
||||
size: (4.0, 4.0),
|
||||
xf: designcraft_geom::Affine::IDENTITY,
|
||||
auto_fit: Default::default(),
|
||||
fit_align: 4,
|
||||
crop: [1.0; 4],
|
||||
});
|
||||
src.insert_item(SpreadRef::Doc(0), it, None).unwrap();
|
||||
let ids = super::super::object::duplicate_from(&mut dst, &src, &[iid], SpreadRef::Doc(0), designcraft_geom::Vec2::ZERO).unwrap();
|
||||
let g = dst.item(ids[0]).and_then(|i| i.graphic()).unwrap();
|
||||
assert_eq!((g.asset, g.crop), (same, [1.0; 4]));
|
||||
assert_eq!(dst.assets.len(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ fn axis(lo: f64, hi: f64, o: (f64, f64), n: (f64, f64), pin_lo: bool, pin_hi: bo
|
||||
fn target(rule: LiquidRule, b: Rect, old: Rect, new: Rect, liquid: Option<ObjectLiquid>, vguides: &[f64], hguides: &[f64]) -> Rect {
|
||||
let (ox, oy, nx, ny) = ((old.x0, old.x1), (old.y0, old.y1), (new.x0, new.x1), (new.y0, new.y1));
|
||||
match rule {
|
||||
LiquidRule::Off => b,
|
||||
LiquidRule::Off | LiquidRule::ControlledByParent => b,
|
||||
LiquidRule::ReCenter => b + (new.center() - old.center()),
|
||||
LiquidRule::Scale => {
|
||||
let k = (new.width() / old.width()).min(new.height() / old.height());
|
||||
@@ -63,7 +63,7 @@ fn target(rule: LiquidRule, b: Rect, old: Rect, new: Rect, liquid: Option<Object
|
||||
pub(crate) fn apply(d: &mut Document, sr: SpreadRef, pi: usize, old: Rect) {
|
||||
let Some(sp) = d.spread(sr) else { return };
|
||||
let Some(pg) = sp.pages.get(pi) else { return };
|
||||
let (rule, new) = (pg.liquid, pg.bounds());
|
||||
let (rule, new) = (effective_rule(d, sr, pi), pg.bounds());
|
||||
if rule == LiquidRule::Off || old == new || old.width() < 1e-6 || old.height() < 1e-6 {
|
||||
return;
|
||||
}
|
||||
@@ -108,8 +108,24 @@ pub(crate) fn apply(d: &mut Document, sr: SpreadRef, pi: usize, old: Rect) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The rule page `pi` of spread `sr` follows: its own, or its parent page's when it's controlled
|
||||
/// by the parent (Off when that's unset too).
|
||||
pub(crate) fn effective_rule(d: &Document, sr: SpreadRef, pi: usize) -> LiquidRule {
|
||||
let Some(pg) = d.spread(sr).and_then(|sp| sp.pages.get(pi)) else { return LiquidRule::Off };
|
||||
if pg.liquid != LiquidRule::ControlledByParent {
|
||||
return pg.liquid;
|
||||
}
|
||||
let SpreadRef::Doc(si) = sr else { return LiquidRule::Off };
|
||||
let abs = d.first_page_of_spread(si) + pi;
|
||||
match d.parent_page_for(abs).and_then(|(ppi, ppg)| d.parents.get(ppi)?.pages.get(ppg).map(|p| p.liquid)) {
|
||||
Some(LiquidRule::ControlledByParent) | None => LiquidRule::Off,
|
||||
Some(r) => r,
|
||||
}
|
||||
}
|
||||
|
||||
fn rule_param(p: &Value, id: &str) -> Result<LiquidRule> {
|
||||
serde_json::from_value(p.get("rule").cloned().unwrap_or(json!("off"))).map_err(|_| bad(id, "`rule`: off|scale|reCenter|guideBased|objectBased"))
|
||||
serde_json::from_value(p.get("rule").cloned().unwrap_or(json!("off")))
|
||||
.map_err(|_| bad(id, "`rule`: off|scale|reCenter|guideBased|objectBased|controlledByParent"))
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -119,7 +135,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Liquid Page Rule",
|
||||
["Layout", "Liquid Layout"],
|
||||
None,
|
||||
"{rule: off|scale|reCenter|guideBased|objectBased, pages?: [1-based] (default: all)}",
|
||||
"{rule: off|scale|reCenter|guideBased|objectBased|controlledByParent (the parent page's rule), pages?: [1-based] (default: all)}",
|
||||
has_doc,
|
||||
|s, p| {
|
||||
let rule = rule_param(p, "liquid.pageRule")?;
|
||||
@@ -168,7 +184,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
}
|
||||
),
|
||||
cmd!("guide.liquid", "Convert to Liquid Guide", [], None, "{spread?, page, index, on?: bool (default true)}", has_doc, |s, p| {
|
||||
let sr = super::spread_param(p, "spread");
|
||||
let sr = super::spread_param(p, "guide.liquid")?;
|
||||
let pi = p.get("page").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
let gi = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad("guide.liquid", "`index` required"))? as usize;
|
||||
let on = p.get("on").and_then(Value::as_bool).unwrap_or(true);
|
||||
|
||||
@@ -13,11 +13,16 @@ 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 aid = AssetId(d.alloc());
|
||||
d.assets.insert(
|
||||
aid,
|
||||
Arc::new(Asset { page: 0, id: aid, name: "math.svg".into(), mime: "image/svg+xml".into(), link: None, data: Arc::new(svg), pixels: None }),
|
||||
);
|
||||
let fresh = AssetId(d.alloc());
|
||||
let aid = d.add_asset(Asset {
|
||||
page: 0,
|
||||
id: fresh,
|
||||
name: "math.svg".into(),
|
||||
mime: "image/svg+xml".into(),
|
||||
link: None,
|
||||
data: Arc::new(svg),
|
||||
pixels: None,
|
||||
});
|
||||
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));
|
||||
@@ -65,7 +70,7 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
});
|
||||
}
|
||||
let text = s.doc()?.selection.text.filter(|t| t.cell.is_none());
|
||||
let spread = super::spread_param(p, "spread");
|
||||
let spread = super::spread_param(p, "math.insert")?;
|
||||
let (px, py) = (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64));
|
||||
s.edit(|d, sel| {
|
||||
if let Some(t) = text {
|
||||
|
||||
@@ -20,13 +20,13 @@ pub(crate) fn place_media(s: &mut Session, p: &Value, name: String, mime: &str,
|
||||
let st = s.active()?;
|
||||
st.selection.items.iter().copied().find(|i| st.doc.item(*i).is_some_and(|it| matches!(it.content, Content::Unassigned | Content::Graphic(_))))
|
||||
});
|
||||
let spread = super::spread_param(p, "spread");
|
||||
let spread = super::spread_param(p, "file.place")?;
|
||||
let lid = s.doc()?.active_layer;
|
||||
let (px, py) = (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64));
|
||||
let mime = mime.to_string();
|
||||
s.edit(|d, sel| {
|
||||
let aid = AssetId(d.alloc());
|
||||
d.assets.insert(aid, Arc::new(Asset { page: 0, id: aid, name, mime, link, data: Arc::new(bytes), pixels: None }));
|
||||
let fresh = AssetId(d.alloc());
|
||||
let aid = d.add_asset(Asset { page: 0, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: None });
|
||||
let id = match target {
|
||||
Some(fid) => {
|
||||
let it = d.item_mut(fid).ok_or(designcraft_doc::DocError::NoItem(fid))?;
|
||||
@@ -90,10 +90,9 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
s.edit(|d, _| {
|
||||
let poster_id = match poster {
|
||||
Some(Some((bytes, name, link, px))) => {
|
||||
let aid = AssetId(d.alloc());
|
||||
let fresh = AssetId(d.alloc());
|
||||
let mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
d.assets.insert(aid, Arc::new(Asset { page: 0, id: aid, name, mime, link, data: Arc::new(bytes), pixels: Some(px) }));
|
||||
Some(Some(aid))
|
||||
Some(Some(d.add_asset(Asset { page: 0, id: fresh, name, mime, link, data: Arc::new(bytes), pixels: Some(px) })))
|
||||
}
|
||||
Some(None) => Some(None),
|
||||
None => None,
|
||||
|
||||
@@ -15,7 +15,8 @@ mod editorial;
|
||||
mod endnotes;
|
||||
mod export;
|
||||
mod file;
|
||||
mod find;
|
||||
pub(crate) mod find;
|
||||
pub use find::same_find_domain;
|
||||
mod fonts;
|
||||
mod guides;
|
||||
mod index;
|
||||
@@ -51,6 +52,7 @@ mod strokes;
|
||||
mod style;
|
||||
pub mod table;
|
||||
pub mod text;
|
||||
mod thread;
|
||||
mod toc;
|
||||
mod transitions;
|
||||
mod variables;
|
||||
@@ -185,6 +187,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
v.extend(package::specs());
|
||||
v.extend(print::specs());
|
||||
v.extend(text::specs());
|
||||
v.extend(thread::specs());
|
||||
v.extend(style::specs());
|
||||
v.extend(table::specs());
|
||||
v.extend(layout::specs());
|
||||
@@ -256,12 +259,23 @@ pub(crate) fn rect_param(p: &Value, key: &str) -> Option<Rect> {
|
||||
let f = |i: usize| a.get(i).and_then(Value::as_f64);
|
||||
Some(Rect::new(f(0)?, f(1)?, f(2)?, f(3)?))
|
||||
}
|
||||
/// `{"kind":"doc","index":0}`, `0` or absent (= spread 0).
|
||||
pub(crate) fn spread_param(p: &Value, key: &str) -> SpreadRef {
|
||||
match p.get(key) {
|
||||
Some(Value::Number(n)) => SpreadRef::Doc(n.as_u64().unwrap_or(0) as usize),
|
||||
Some(v) => serde_json::from_value(v.clone()).unwrap_or(SpreadRef::Doc(0)),
|
||||
None => SpreadRef::Doc(0),
|
||||
/// The `spread` param: a body spread index (`0`), a spread reference (`{"kind":"doc"|"parent","index":0}`)
|
||||
/// or absent (= spread 0). Anything else is an error: read as spread 0, a script aiming at a parent
|
||||
/// spread got its items on the first page without a word.
|
||||
pub(crate) fn spread_param(p: &Value, command: &str) -> Result<SpreadRef> {
|
||||
let index = |v: &Value, key: &str| {
|
||||
v.as_u64().and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad(command, format!("`{key}` must be a non-negative integer")))
|
||||
};
|
||||
let Some(value) = p.get("spread") else { return Ok(SpreadRef::Doc(0)) };
|
||||
if value.is_number() {
|
||||
return Ok(SpreadRef::Doc(index(value, "spread")?));
|
||||
}
|
||||
let object = value.as_object().ok_or_else(|| bad(command, "`spread` must be a non-negative integer or {kind: doc|parent, index}"))?;
|
||||
let i = index(object.get("index").ok_or_else(|| bad(command, "missing `spread.index`"))?, "spread.index")?;
|
||||
match object.get("kind").and_then(Value::as_str) {
|
||||
Some("doc") => Ok(SpreadRef::Doc(i)),
|
||||
Some("parent") => Ok(SpreadRef::Parent(i)),
|
||||
_ => Err(bad(command, "`spread.kind` must be `doc` or `parent`")),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use designcraft_doc::{CellAddr, FootnoteOptions, ParaFormat, StoryId, TextSel};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{CommandSpec, bad, cmd, has_doc, str_param};
|
||||
use super::{CommandSpec, bad, cmd, has_doc};
|
||||
use crate::{Result, Session};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
@@ -22,7 +22,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Document Footnote Options…",
|
||||
["Type"],
|
||||
None,
|
||||
"{style?: arabic|upperRoman|lowerRoman|upperLetters|lowerLetters|symbols, startAt?, restart?: never|page|spread|section, prefix?, suffix?, affixIn?: none|reference|text|both, refPosition?: superscript|subscript|normal|otSuperscript, refCharStyle?, paraStyle?, separator?, spaceBefore?, spaceBetween?, firstBaseline?, firstBaselineMin?, spanColumns?, rule?: {on, weight, color, tint, width, offset, leftIndent}} → the options (no params: just read them)",
|
||||
"{style?: arabic|upperRoman|lowerRoman|upperLetters|lowerLetters|symbols, startAt?, restart?: never|page|spread|section, prefix?, suffix?, affixIn?: none|reference|text|both, refPosition?: superscript|subscript|normal|otSuperscript, refCharStyle?, paraStyle?, separator?, spaceBefore?, spaceBetween?, firstBaseline?: ascent|capHeight|leading|xHeight|emboxHeight|fixed, firstBaselineMin?, spanColumns?, rule?: {on, weight, color, tint, width, offset, leftIndent}} → the options (no params: just read them)",
|
||||
has_doc,
|
||||
options
|
||||
),
|
||||
@@ -58,7 +58,7 @@ fn in_note(s: &Session) -> std::result::Result<(), String> {
|
||||
}
|
||||
|
||||
fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
s.edit(|d, sel| {
|
||||
let t = sel.text.ok_or_else(|| bad("footnote.insert", "no insertion point"))?;
|
||||
let para = ParaFormat { style: d.footnote_options.para_style.clone(), ..Default::default() };
|
||||
@@ -130,7 +130,7 @@ fn list(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn set_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let sid = story_id(p, "footnote.setText")?;
|
||||
let id = note_id(p, "footnote.setText")?;
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
s.edit(|d, _| {
|
||||
let st = d.story_mut(sid).ok_or(designcraft_doc::DocError::NoStory(sid))?;
|
||||
let n = st.note_mut(id).ok_or_else(|| bad("footnote.setText", format!("no footnote {id}")))?;
|
||||
|
||||
+702
-28
@@ -14,6 +14,17 @@ use serde_json::{Value, json};
|
||||
use super::{CommandSpec, bad, bool_or, cmd, f64_or, has_doc, has_selection, ok, point_param, rect_param, spread_param, str_param, targets};
|
||||
use crate::{Result, Session};
|
||||
|
||||
/// `name` must be a swatch of the document: an unknown name would paint nothing. The error names
|
||||
/// the swatch it was probably meant to be (`Paper` → `[Paper]`, a different case).
|
||||
fn known_swatch(doc: &designcraft_doc::Document, name: &str, cmd: &str, key: &str) -> Result<()> {
|
||||
if doc.swatch(name).is_some() {
|
||||
return Ok(());
|
||||
}
|
||||
let bare = |n: &str| n.trim().trim_start_matches('[').trim_end_matches(']').to_lowercase();
|
||||
let hint = doc.swatches.iter().find(|s| bare(&s.name) == bare(name)).map(|s| format!(" (did you mean `{}`?)", s.name)).unwrap_or_default();
|
||||
Err(bad(cmd, format!("{key}: no swatch named `{name}`{hint}; create one with `swatch.create` and use its returned name")))
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
@@ -21,12 +32,20 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Create Frame",
|
||||
[],
|
||||
None,
|
||||
"{spread?, rect: [x0,y0,x1,y1] (spread coords), shape?: rectangle|ellipse|polygon, content?: graphic|text|unassigned, sides?: 6, text?: string, caret?: bool, vertical?: bool (text: a new vertical story)}",
|
||||
"{spread?: index | {kind: doc|parent, index} (a parent spread: index into `parents` of document.inspect), rect: [x0,y0,x1,y1] (spread coords), shape?: rectangle|ellipse|polygon, content?: graphic|text|unassigned, sides?: 6, text?: string, caret?: bool, vertical?: bool (text: a new vertical story)}",
|
||||
has_doc,
|
||||
frame_create
|
||||
),
|
||||
cmd!("line.create", "Create Line", [], None, "{spread?, a: [x,y], b: [x,y]}", has_doc, line_create),
|
||||
cmd!("transform.move", "Move", ["Object", "Transform"], None, "{dx, dy, copy?: bool, ids?, toSpread?}", has_selection, transform_move),
|
||||
cmd!(
|
||||
"transform.move",
|
||||
"Move",
|
||||
["Object", "Transform"],
|
||||
None,
|
||||
"{dx, dy, copy?: bool, ids?, toSpread?} → {moved, ids: the objects moved (with copy, the copies)}",
|
||||
has_selection,
|
||||
transform_move
|
||||
),
|
||||
cmd!(
|
||||
"transform.resize",
|
||||
"Resize",
|
||||
@@ -160,11 +179,53 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
)
|
||||
}
|
||||
),
|
||||
cmd!("object.fill", "Fill", [], None, "{swatch, tint?: 0..1, ids?}", has_selection, |s, p| {
|
||||
let sw = str_param(p, "swatch").ok_or_else(|| bad("object.fill", "missing swatch"))?.to_string();
|
||||
let tint = f64_or(p, "tint", 1.0) as f32;
|
||||
set_flag(s, p, move |i| i.fill = Fill { swatch: sw.clone(), tint, ..i.fill.clone() }, false)
|
||||
}),
|
||||
cmd!(
|
||||
"object.fill",
|
||||
"Fill",
|
||||
[],
|
||||
None,
|
||||
"{swatch? (a swatch's name, e.g. [Paper]), tint?: 0..1, ids?} — at least one; a tint alone keeps each object's own swatch (the Swatches panel's Tint field)",
|
||||
has_selection,
|
||||
|s, p| {
|
||||
let sw = str_param(p, "swatch").map(str::to_string);
|
||||
let tint = match p.get("tint") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(v.as_f64().filter(|t| (0.0..=1.0).contains(t)).ok_or_else(|| bad("object.fill", "`tint` is 0..1"))? as f32),
|
||||
};
|
||||
match &sw {
|
||||
Some(sw) => known_swatch(&s.doc()?.doc, sw, "object.fill", "swatch")?,
|
||||
None if tint.is_none() => return Err(bad("object.fill", "missing swatch")),
|
||||
None => {}
|
||||
}
|
||||
set_flag(
|
||||
s,
|
||||
p,
|
||||
move |i| match &sw {
|
||||
Some(sw) => i.fill = Fill { swatch: sw.clone(), tint: tint.unwrap_or(1.0), ..i.fill.clone() },
|
||||
None => i.fill.tint = tint.unwrap_or(i.fill.tint),
|
||||
},
|
||||
false,
|
||||
)
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"object.swapFillStroke",
|
||||
"Swap Fill and Stroke",
|
||||
[],
|
||||
None,
|
||||
"{ids?} — each object's fill and stroke colours (and tints) trade places; locked objects stay as they are",
|
||||
has_editable_selection,
|
||||
|s, p| edit_unlocked(s, p, swap_fill_stroke)
|
||||
),
|
||||
cmd!(
|
||||
"object.defaultFillStroke",
|
||||
"Default Fill and Stroke",
|
||||
[],
|
||||
None,
|
||||
"{ids?} — no fill and a black stroke; locked objects stay as they are",
|
||||
has_editable_selection,
|
||||
|s, p| edit_unlocked(s, p, default_fill_stroke)
|
||||
),
|
||||
cmd!(
|
||||
"object.stroke",
|
||||
"Stroke",
|
||||
@@ -173,11 +234,21 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"{swatch?, tint?, weight?, align?: center|inside|outside, type?: {kind:…}, cap?: butt|round|projecting, join?: miter|round|bevel, miterLimit?, start?, end?: none|simple|simpleWide|triangle|triangleWide|barbed|curved|circle|circleSolid|square|squareSolid|bar, gapSwatch?, gapTint?, gapOverprint?: bool, ids?}",
|
||||
has_selection,
|
||||
|s, p| {
|
||||
for key in ["swatch", "gapSwatch"] {
|
||||
if let Some(name) = p.get(key).and_then(Value::as_str) {
|
||||
known_swatch(&s.doc()?.doc, name, "object.stroke", key)?;
|
||||
}
|
||||
}
|
||||
let p2 = p.clone();
|
||||
// A tint alone (the Swatches panel's Tint field) recolours strokes, it doesn't add one.
|
||||
let tint_only = p.as_object().is_some_and(|o| o.contains_key("tint") && o.keys().all(|k| k == "tint" || k == "ids"));
|
||||
set_flag(
|
||||
s,
|
||||
p,
|
||||
move |i| {
|
||||
if tint_only && i.stroke.is_none() {
|
||||
return;
|
||||
}
|
||||
let mut st = if i.stroke.is_none() && p2.get("swatch").is_none() { Stroke::default() } else { i.stroke.clone() };
|
||||
if let Some(v) = p2.get("swatch").and_then(Value::as_str) {
|
||||
st.swatch = v.into();
|
||||
@@ -489,7 +560,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Text Frame Options…",
|
||||
["Object"],
|
||||
Some("Cmd+B"),
|
||||
"{columns?, gutter?, inset?: number|[t,l,b,r] (null keeps that side), verticalJustification?: top|center|bottom|justify, firstBaseline?, autoSize?, ignoreWrap?, balanceColumns?, columnRule?: bool, columnRuleWeight? (pt), columnRuleColor? (swatch name), columnRuleTint? (0..1), columnRuleOffset? (pt, horizontal), columnRuleTopInset? (pt), columnRuleBottomInset? (pt), vertical?: bool (sets the story direction of the frames' stories, as Type ▸ Story Direction), ids?}",
|
||||
"{columns?, gutter?, inset?: number|[t,l,b,r] (null keeps that side), verticalJustification?: top|center|bottom|justify, firstBaseline?: ascent|capHeight|leading|xHeight|emboxHeight|fixed, autoSize?, ignoreWrap?, balanceColumns?, columnRule?: bool, columnRuleWeight? (pt), columnRuleColor? (swatch name), columnRuleTint? (0..1), columnRuleOffset? (pt, horizontal), columnRuleTopInset? (pt), columnRuleBottomInset? (pt), vertical?: bool (sets the story direction of the frames' stories, as Type ▸ Story Direction), ids?}",
|
||||
has_selection,
|
||||
text_frame_options
|
||||
),
|
||||
@@ -565,13 +636,39 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Step and Repeat…",
|
||||
["Edit"],
|
||||
Some("Cmd+Alt+U"),
|
||||
"{count?: 1..1000, dx?, dy?, rows?, columns?, ids?} — copies of the selection (or ids) offset by (dx, dy); with rows/columns, a grid of at most 1000 cells, and at most 1000 item copies including group contents",
|
||||
"{count?: 1..1000, dx?, dy?, rows?, columns?, ids?} — copies of the selection (or ids) offset by (dx, dy); with rows/columns, a grid of at most 1000 cells, and at most 1000 item copies including group contents → {created, ids: the copies}",
|
||||
has_selection,
|
||||
step_and_repeat
|
||||
),
|
||||
cmd!("path.create", "Create Path", [], None, "{spread?, anchors: [{p:[x,y], in?:[x,y], out?:[x,y]}], closed?: bool}", has_doc, path_create),
|
||||
cmd!("path.appendAnchor", "Add Anchor", [], None, "{id, anchor: {p, in?, out?}} (spread coords)", has_doc, path_append),
|
||||
cmd!("path.close", "Close Path", ["Object", "Paths"], None, "{id}", has_doc, path_close),
|
||||
cmd!(
|
||||
"path.appendAnchor",
|
||||
"Add Anchor",
|
||||
[],
|
||||
None,
|
||||
"{id, anchor: {p, in?, out?} (spread coords), subpath?: index (default the last)} — after the subpath's last anchor",
|
||||
has_doc,
|
||||
path_append
|
||||
),
|
||||
cmd!(
|
||||
"path.setAnchor",
|
||||
"Set Anchor",
|
||||
[],
|
||||
None,
|
||||
"{id, subpath?: index (default the last), anchor: index|\"first\"|\"last\", p?, in?, out? (spread coords)} — moves an anchor or its direction lines; with both lines given and collinear it becomes smooth",
|
||||
has_doc,
|
||||
path_set_anchor
|
||||
),
|
||||
cmd!(
|
||||
"path.reverse",
|
||||
"Reverse Path",
|
||||
["Object", "Paths"],
|
||||
None,
|
||||
"{ids?, subpath?: index (only that subpath of the first target)} — the anchors run the other way (the end point becomes the start)",
|
||||
has_selection,
|
||||
path_reverse
|
||||
),
|
||||
cmd!("path.close", "Close Path", ["Object", "Paths"], None, "{id, subpath?: index (default every subpath)}", has_doc, path_close),
|
||||
cmd!("path.moveAnchors", "Move Anchors", [], None, "{id, anchors: [[subpath, index]], dx, dy, handle?: in|out}", has_doc, path_move_anchors),
|
||||
cmd!(
|
||||
"object.align",
|
||||
@@ -803,7 +900,7 @@ fn frame_create(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let rect = rect_param(p, "rect").ok_or_else(|| bad("frame.create", "missing rect"))?;
|
||||
let shape = str_param(p, "shape").unwrap_or("rectangle");
|
||||
let content = str_param(p, "content").unwrap_or("graphic");
|
||||
let sr = spread_param(p, "spread");
|
||||
let sr = spread_param(p, "frame.create")?;
|
||||
let lid = s.doc()?.active_layer;
|
||||
let text = super::text_param(p, "text");
|
||||
let caret = bool_or(p, "caret", content == "text");
|
||||
@@ -866,7 +963,7 @@ pub(crate) fn polygon_in(r: Rect, sides: u32, inset: f64) -> designcraft_geom::P
|
||||
fn line_create(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let a = point_param(p, "a").ok_or_else(|| bad("line.create", "missing a"))?;
|
||||
let b = point_param(p, "b").ok_or_else(|| bad("line.create", "missing b"))?;
|
||||
let sr = spread_param(p, "spread");
|
||||
let sr = spread_param(p, "line.create")?;
|
||||
let lid = s.doc()?.active_layer;
|
||||
s.edit(|d, sel| {
|
||||
let id = ItemId(d.alloc());
|
||||
@@ -900,6 +997,11 @@ pub fn duplicate_from(dst: &mut Document, src: &Document, ids: &[ItemId], to: Sp
|
||||
fn renumber(dst: &mut Document, src: &Document, it: &mut Item, stories: &mut HashMap<StoryId, StoryId>) {
|
||||
it.id = ItemId(dst.alloc());
|
||||
let my_id = it.id;
|
||||
if let Some(m) = it.media.as_mut()
|
||||
&& let Some(p) = m.poster
|
||||
{
|
||||
m.poster = dst.adopt_asset(src, p).or(Some(p));
|
||||
}
|
||||
match &mut it.content {
|
||||
Content::Text(tf) => {
|
||||
let new_sid = match stories.get(&tf.story) {
|
||||
@@ -908,6 +1010,15 @@ fn renumber(dst: &mut Document, src: &Document, it: &mut Item, stories: &mut Has
|
||||
let n = StoryId(dst.alloc());
|
||||
let mut st = src.story(tf.story).cloned().unwrap_or_else(|| Story::new(n));
|
||||
st.id = n;
|
||||
let mut assets = HashMap::new();
|
||||
for a in designcraft_doc::story_asset_refs(&st) {
|
||||
if let Some(na) = dst.adopt_asset(src, a)
|
||||
&& na != a
|
||||
{
|
||||
assets.insert(a, na);
|
||||
}
|
||||
}
|
||||
designcraft_doc::remap_story_assets(&mut st, &assets);
|
||||
st.frames.clear();
|
||||
dst.stories.insert(n, Arc::new(st));
|
||||
stories.insert(tf.story, n);
|
||||
@@ -920,10 +1031,8 @@ fn renumber(dst: &mut Document, src: &Document, it: &mut Item, stories: &mut Has
|
||||
}
|
||||
}
|
||||
Content::Graphic(g) => {
|
||||
if let Some(a) = src.assets.get(&g.asset)
|
||||
&& !dst.assets.contains_key(&g.asset)
|
||||
{
|
||||
dst.assets.insert(g.asset, a.clone());
|
||||
if let Some(a) = dst.adopt_asset(src, g.asset) {
|
||||
g.asset = a;
|
||||
}
|
||||
}
|
||||
Content::Group { items } => {
|
||||
@@ -969,7 +1078,7 @@ fn transform_move(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
it.xf = Affine::translate((dx, dy)) * it.xf;
|
||||
}
|
||||
Ok(json!({"moved": ids.len()}))
|
||||
Ok(json!({"moved": ids.len(), "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()}))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1272,6 +1381,43 @@ fn arrange(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = targets(s, p)?;
|
||||
let to = str_param(p, "to").unwrap_or("front").to_string();
|
||||
s.edit(|d, _| {
|
||||
if matches!(to.as_str(), "forward" | "backward") {
|
||||
let selected: HashSet<ItemId> = ids.iter().copied().collect();
|
||||
let mut positions: Vec<_> = selected
|
||||
.iter()
|
||||
.filter_map(|id| {
|
||||
let loc = d.find(*id)?;
|
||||
match loc.path.as_slice() {
|
||||
[i] => Some((loc.spread, *i)),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
// Move the leading selected items first, independently of selection/click order.
|
||||
// Swapping only with unselected siblings leaves the other selected indices valid.
|
||||
positions.sort_by_key(|(_, i)| *i);
|
||||
let forward = to == "forward";
|
||||
if forward {
|
||||
positions.reverse();
|
||||
}
|
||||
for (spread, i) in positions {
|
||||
let Some(sp) = d.spread_mut(spread) else { continue };
|
||||
let Some(it) = sp.items.get(i) else { continue };
|
||||
let layer = it.layer;
|
||||
let sibling = if forward {
|
||||
sp.items.iter().enumerate().skip(i.saturating_add(1)).find(|(_, it)| it.layer == layer)
|
||||
} else {
|
||||
sp.items.iter().enumerate().take(i).rev().find(|(_, it)| it.layer == layer)
|
||||
};
|
||||
if let Some((j, it)) = sibling
|
||||
&& !selected.contains(&it.id)
|
||||
{
|
||||
// Keep every other layer's slots and relative stacking order untouched.
|
||||
sp.items.swap(i, j);
|
||||
}
|
||||
}
|
||||
return ok();
|
||||
}
|
||||
for id in &ids {
|
||||
let Some(loc) = d.find(*id) else { continue };
|
||||
if loc.path.len() != 1 {
|
||||
@@ -1284,7 +1430,6 @@ fn arrange(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let j = match to.as_str() {
|
||||
"front" => n - 1,
|
||||
"back" => 0,
|
||||
"forward" => (i + 1).min(n - 1),
|
||||
_ => i.saturating_sub(1),
|
||||
};
|
||||
sp.items.insert(j, it);
|
||||
@@ -1358,6 +1503,60 @@ fn set_flag(s: &mut Session, p: &Value, f: impl Fn(&mut Item), deselect: bool) -
|
||||
})
|
||||
}
|
||||
|
||||
/// An object a colour command may change: neither it nor its layer is locked.
|
||||
fn unlocked(d: &Document, it: &Item) -> bool {
|
||||
!it.locked && d.layer(it.layer).is_none_or(|l| !l.locked)
|
||||
}
|
||||
|
||||
fn has_editable_selection(s: &Session) -> std::result::Result<(), String> {
|
||||
has_selection(s)?;
|
||||
let st = s.active().ok_or_else(|| super::NOTHING_SELECTED.to_string())?;
|
||||
if st.selection.items.iter().any(|id| st.doc.item(*id).is_some_and(|it| unlocked(&st.doc, it))) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("the selected objects are locked".into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `f` on each unlocked target in one edit (one undo step).
|
||||
fn edit_unlocked(s: &mut Session, p: &Value, f: impl Fn(&mut Item)) -> Result<Value> {
|
||||
let ids = targets(s, p)?;
|
||||
s.edit(|d, _| {
|
||||
let mut changed = 0;
|
||||
for id in &ids {
|
||||
if d.item(*id).is_some_and(|it| unlocked(d, it))
|
||||
&& let Some(it) = d.item_mut(*id)
|
||||
{
|
||||
f(it);
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
Ok(json!({"changed": changed}))
|
||||
})
|
||||
}
|
||||
|
||||
/// A stroke that gets a colour needs a weight to show it.
|
||||
fn visible_weight(st: &mut Stroke) {
|
||||
if st.swatch != designcraft_color::swatch::NONE && st.weight <= 0.0 {
|
||||
st.weight = Stroke::default().weight;
|
||||
}
|
||||
}
|
||||
|
||||
fn swap_fill_stroke(it: &mut Item) {
|
||||
let fill = std::mem::take(&mut it.fill);
|
||||
it.fill = Fill { swatch: std::mem::take(&mut it.stroke.swatch), tint: it.stroke.tint, ..fill.clone() };
|
||||
it.stroke.swatch = fill.swatch;
|
||||
it.stroke.tint = fill.tint;
|
||||
visible_weight(&mut it.stroke);
|
||||
}
|
||||
|
||||
fn default_fill_stroke(it: &mut Item) {
|
||||
it.fill = Fill { swatch: designcraft_color::swatch::NONE.into(), tint: 1.0, ..it.fill.clone() };
|
||||
it.stroke.swatch = designcraft_color::swatch::BLACK.into();
|
||||
it.stroke.tint = 1.0;
|
||||
visible_weight(&mut it.stroke);
|
||||
}
|
||||
|
||||
fn all_flag(s: &mut Session, f: impl Fn(&mut Item)) -> Result<Value> {
|
||||
s.edit(|d, _| {
|
||||
for i in 0..d.spreads.len() {
|
||||
@@ -1546,7 +1745,7 @@ fn gap_move(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.and_then(Value::as_array)
|
||||
.and_then(|a| Some(Point::new(a.first()?.as_f64()?, a.get(1)?.as_f64()?)))
|
||||
.ok_or_else(|| bad(ID, "missing at"))?;
|
||||
let sr = spread_param(p, "spread");
|
||||
let sr = spread_param(p, ID)?;
|
||||
let (vertical, a, z, before, after) = find_gap(&s.doc()?.doc, sr, at).ok_or_else(|| bad(ID, "no gap there"))?;
|
||||
// `delta` along the gap's axis, or a drag (dx, dy) of which the axis part counts.
|
||||
let delta =
|
||||
@@ -1841,6 +2040,10 @@ fn distribute(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
for ((id, r), t) in items.iter().zip(targets) {
|
||||
let delta = t - lo(r);
|
||||
if let Some(it) = d.item_mut(*id) {
|
||||
// Keep locked items in the spacing calculation, as align does for its reference.
|
||||
if it.locked {
|
||||
continue;
|
||||
}
|
||||
let v = if horizontal { Vec2::new(delta, 0.0) } else { Vec2::new(0.0, delta) };
|
||||
it.xf = Affine::translate(v) * it.xf;
|
||||
}
|
||||
@@ -1873,7 +2076,7 @@ fn path_create(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad("path.create", "a path needs at least two anchors"));
|
||||
}
|
||||
let closed = bool_or(p, "closed", false);
|
||||
let sr = spread_param(p, "spread");
|
||||
let sr = spread_param(p, "path.create")?;
|
||||
let lid = s.doc()?.active_layer;
|
||||
s.edit(|d, sel| {
|
||||
let id = ItemId(d.alloc());
|
||||
@@ -1889,11 +2092,20 @@ fn path_create(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn path_append(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = super::id_param(p, "id").ok_or_else(|| bad("path.appendAnchor", "missing id"))?;
|
||||
let a = p.get("anchor").and_then(anchor_from).ok_or_else(|| bad("path.appendAnchor", "missing anchor"))?;
|
||||
let subpath = p.get("subpath").and_then(Value::as_u64);
|
||||
s.edit(|d, _| {
|
||||
let loc = d.find(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
let inv = (d.parent_xf(&loc) * d.item_at(&loc).map(|i| i.xf).unwrap_or_default()).inverse();
|
||||
let it = d.item_mut(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
let sp = it.path.subpaths.last_mut().ok_or_else(|| bad("path.appendAnchor", "empty path"))?;
|
||||
let sp = match subpath {
|
||||
Some(i) => {
|
||||
usize::try_from(i).ok().and_then(|i| it.path.subpaths.get_mut(i)).ok_or_else(|| bad("path.appendAnchor", "no such subpath"))?
|
||||
}
|
||||
None => it.path.subpaths.last_mut().ok_or_else(|| bad("path.appendAnchor", "empty path"))?,
|
||||
};
|
||||
if sp.closed {
|
||||
return Err(bad("path.appendAnchor", "the subpath is closed"));
|
||||
}
|
||||
sp.anchors.push(a.transform(inv));
|
||||
if it.shape == Shape::GraphicLine {
|
||||
it.shape = Shape::Path;
|
||||
@@ -1902,12 +2114,103 @@ fn path_append(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
})
|
||||
}
|
||||
|
||||
fn path_set_anchor(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const ID: &str = "path.setAnchor";
|
||||
let id = super::id_param(p, "id").ok_or_else(|| bad(ID, "missing id"))?;
|
||||
let pt = |k: &str| -> Option<Point> {
|
||||
let a = p.get(k)?.as_array()?;
|
||||
let q = Point::new(a.first()?.as_f64()?, a.get(1)?.as_f64()?);
|
||||
(q.x.is_finite() && q.y.is_finite()).then_some(q)
|
||||
};
|
||||
let (new_p, new_in, new_out) = (pt("p"), pt("in"), pt("out"));
|
||||
if new_p.is_none() && new_in.is_none() && new_out.is_none() {
|
||||
return Err(bad(ID, "give p, in or out"));
|
||||
}
|
||||
let subpath = p.get("subpath").and_then(Value::as_u64);
|
||||
let which = p.get("anchor").cloned().unwrap_or(Value::Null);
|
||||
s.edit(|d, _| {
|
||||
let loc = d.find(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
let inv = (d.parent_xf(&loc) * d.item_at(&loc).map(|i| i.xf).unwrap_or_default()).inverse();
|
||||
let it = d.item_mut(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
if it.locked {
|
||||
return Err(bad(ID, "the path is locked"));
|
||||
}
|
||||
let sp = match subpath {
|
||||
Some(i) => usize::try_from(i).ok().and_then(|i| it.path.subpaths.get_mut(i)),
|
||||
None => it.path.subpaths.last_mut(),
|
||||
}
|
||||
.ok_or_else(|| bad(ID, "no such subpath"))?;
|
||||
let n = sp.anchors.len();
|
||||
let ai = match &which {
|
||||
Value::String(w) if w == "first" => 0,
|
||||
Value::String(w) if w == "last" => n.saturating_sub(1),
|
||||
v => v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or_else(|| bad(ID, "anchor: an index, \"first\" or \"last\""))?,
|
||||
};
|
||||
let a = sp.anchors.get_mut(ai).ok_or_else(|| bad(ID, "no such anchor"))?;
|
||||
if let Some(q) = new_p {
|
||||
let v = inv * q - a.p;
|
||||
a.p += v;
|
||||
a.h_in += v;
|
||||
a.h_out += v;
|
||||
}
|
||||
if let Some(q) = new_in {
|
||||
a.h_in = inv * q;
|
||||
}
|
||||
if let Some(q) = new_out {
|
||||
a.h_out = inv * q;
|
||||
}
|
||||
if new_in.is_some() && new_out.is_some() {
|
||||
let (i, o) = (a.h_in - a.p, a.h_out - a.p);
|
||||
let collinear = (i.x * o.y - i.y * o.x).abs() <= 1e-6 * i.hypot().max(1.0) * o.hypot().max(1.0) && i.dot(o) <= 0.0;
|
||||
a.kind = if collinear { designcraft_geom::AnchorKind::Smooth } else { designcraft_geom::AnchorKind::Corner };
|
||||
}
|
||||
ok()
|
||||
})
|
||||
}
|
||||
|
||||
fn path_reverse(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = targets(s, p)?;
|
||||
let only = p.get("subpath").and_then(Value::as_u64).map(|i| usize::try_from(i).unwrap_or(usize::MAX));
|
||||
if let (Some(i), Some(id)) = (only, ids.first())
|
||||
&& s.doc()?.doc.item(*id).is_none_or(|it| it.path.subpaths.get(i).is_none())
|
||||
{
|
||||
return Err(bad("path.reverse", "no such subpath"));
|
||||
}
|
||||
let ids = if only.is_some() { ids.into_iter().take(1).collect() } else { ids };
|
||||
s.edit(|d, _| {
|
||||
let mut reversed = 0;
|
||||
for id in &ids {
|
||||
let Some(it) = d.item_mut(*id) else { continue };
|
||||
if it.locked || it.is_group() {
|
||||
continue;
|
||||
}
|
||||
for (i, sp) in it.path.subpaths.iter_mut().enumerate() {
|
||||
if only.is_some_and(|o| o != i) {
|
||||
continue;
|
||||
}
|
||||
sp.anchors.reverse();
|
||||
for a in &mut sp.anchors {
|
||||
std::mem::swap(&mut a.h_in, &mut a.h_out);
|
||||
}
|
||||
}
|
||||
reversed += 1;
|
||||
}
|
||||
Ok(json!({"reversed": reversed}))
|
||||
})
|
||||
}
|
||||
|
||||
fn path_close(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = super::id_param(p, "id").ok_or_else(|| bad("path.close", "missing id"))?;
|
||||
let only = p.get("subpath").and_then(Value::as_u64).map(|i| usize::try_from(i).unwrap_or(usize::MAX));
|
||||
s.edit(|d, _| {
|
||||
let it = d.item_mut(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
for sp in &mut it.path.subpaths {
|
||||
sp.closed = true;
|
||||
if only.is_some_and(|i| i >= it.path.subpaths.len()) {
|
||||
return Err(bad("path.close", "no such subpath"));
|
||||
}
|
||||
for (i, sp) in it.path.subpaths.iter_mut().enumerate() {
|
||||
if only.is_none_or(|o| o == i) {
|
||||
sp.closed = true;
|
||||
}
|
||||
}
|
||||
if it.shape == Shape::GraphicLine {
|
||||
it.shape = Shape::Path;
|
||||
@@ -2070,14 +2373,195 @@ fn step_and_repeat(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
*sel = Selection::items(all.clone());
|
||||
Ok(json!({"created": all.len() - ids.len()}))
|
||||
let copies: Vec<u64> = all.iter().skip(ids.len()).map(|i| i.0).collect();
|
||||
Ok(json!({"created": copies.len(), "ids": copies}))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod arrange_tests {
|
||||
use super::*;
|
||||
|
||||
fn fixture<const N: usize>(layers: [usize; N]) -> (Session, [ItemId; N]) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 1, "facingPages": false})).unwrap();
|
||||
let first = s.doc().unwrap().doc.default_layer().0;
|
||||
let second = s.execute("layer.new", &json!({"name": "Other layer"})).unwrap()["id"].as_u64().unwrap();
|
||||
let layer_ids = [first, second];
|
||||
let mut ids = [ItemId(0); N];
|
||||
for (id, layer) in ids.iter_mut().zip(layers) {
|
||||
// Overlap objects within each layer; separate layers spatially for hit testing.
|
||||
let x = if layer == 0 { 0 } else { 200 };
|
||||
*id = ItemId(s.execute("frame.create", &json!({"rect": [x, 0, x + 100, 100]})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("object.setLayer", &json!({"ids": [id.0], "layer": layer_ids[layer]})).unwrap();
|
||||
}
|
||||
(s, ids)
|
||||
}
|
||||
|
||||
fn stack(s: &Session) -> Vec<ItemId> {
|
||||
s.doc().unwrap().doc.spreads[0].items.iter().map(|it| it.id).collect()
|
||||
}
|
||||
|
||||
fn snapshot(s: &Session) -> Value {
|
||||
let st = s.doc().unwrap();
|
||||
json!({"document": &*st.doc, "selection": &st.selection})
|
||||
}
|
||||
|
||||
fn assert_items_unchanged(before: &Document, after: &Document) {
|
||||
assert_eq!(serde_json::to_value(&before.layers).unwrap(), serde_json::to_value(&after.layers).unwrap());
|
||||
for it in &before.spreads[0].items {
|
||||
assert_eq!(serde_json::to_value(it).unwrap(), serde_json::to_value(after.item(it.id).unwrap()).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrange_steps_past_other_layers_change_hit_order_and_undo() {
|
||||
for direction in ["forward", "backward"] {
|
||||
let (mut s, [a, x, y, b]) = fixture([0, 1, 1, 0]);
|
||||
let selected = if direction == "forward" { a } else { b };
|
||||
s.execute("selection.set", &json!({"ids": [selected.0]})).unwrap();
|
||||
let before = snapshot(&s);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let undo_len = s.doc().unwrap().history.undo.len();
|
||||
let hit = |s: &Session| s.doc().unwrap().doc.hit_item(0, Point::new(50.0, 50.0), 0.0);
|
||||
assert_eq!(hit(&s), Some(b));
|
||||
|
||||
s.execute("object.arrange", &json!({"to": direction})).unwrap();
|
||||
assert_eq!(stack(&s), [b, x, y, a], "{direction}");
|
||||
assert_eq!(hit(&s), Some(a), "the visible topmost object must change");
|
||||
assert_items_unchanged(&doc, &s.doc().unwrap().doc);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len + 1);
|
||||
assert_eq!(s.doc().unwrap().selection.items, [selected]);
|
||||
let after = snapshot(&s);
|
||||
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(snapshot(&s), before);
|
||||
assert_eq!(hit(&s), Some(b));
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(snapshot(&s), after);
|
||||
assert_eq!(hit(&s), Some(a));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrange_multiple_selection_preserves_order_independent_of_input_order() {
|
||||
// Layer 0: a, b, c, d. Layer 1: x, y. Arrays name the original stack indices.
|
||||
let cases = [
|
||||
("forward", [0, 2], [4, 1, 0, 3, 2, 5]), // a,b cross c as a block
|
||||
("backward", [2, 4], [2, 1, 4, 3, 0, 5]), // b,c cross a as a block
|
||||
("forward", [0, 4], [2, 1, 0, 3, 5, 4]), // separate selected blocks
|
||||
("backward", [2, 5], [2, 1, 0, 3, 5, 4]),
|
||||
("forward", [4, 5], [0, 1, 2, 3, 4, 5]), // front block is already at its endpoint
|
||||
("backward", [0, 2], [0, 1, 2, 3, 4, 5]), // back block is already at its endpoint
|
||||
];
|
||||
for (direction, selected, expected) in cases {
|
||||
for reverse in [false, true] {
|
||||
let (mut s, ids) = fixture([0, 1, 0, 1, 0, 0]);
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let mut selected = selected.map(|i| ids[i].0).to_vec();
|
||||
if reverse {
|
||||
selected.reverse();
|
||||
}
|
||||
// Explicit command targets may contain duplicates: an item still moves once.
|
||||
selected.push(selected[0]);
|
||||
s.execute("object.arrange", &json!({"to": direction, "ids": selected})).unwrap();
|
||||
assert_eq!(stack(&s), expected.map(|i| ids[i]), "{direction}, reverse={reverse}");
|
||||
assert_items_unchanged(&doc, &s.doc().unwrap().doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrange_same_layer_steps_stop_at_layer_endpoints() {
|
||||
let cases = [
|
||||
("forward", vec![1], [0, 2, 1, 3, 4]),
|
||||
("backward", vec![3], [0, 1, 3, 2, 4]),
|
||||
("forward", vec![3], [0, 1, 2, 3, 4]),
|
||||
("backward", vec![1], [0, 1, 2, 3, 4]),
|
||||
("forward", vec![3, 1, 2], [0, 1, 2, 3, 4]),
|
||||
("backward", vec![3, 1, 2], [0, 1, 2, 3, 4]),
|
||||
];
|
||||
for (direction, selected, expected) in cases {
|
||||
let (mut s, ids) = fixture([1, 0, 0, 0, 1]);
|
||||
let selected: Vec<_> = selected.into_iter().map(|i| ids[i].0).collect();
|
||||
s.execute("object.arrange", &json!({"to": direction, "ids": selected})).unwrap();
|
||||
assert_eq!(stack(&s), expected.map(|i| ids[i]));
|
||||
}
|
||||
for direction in ["forward", "backward"] {
|
||||
let (mut s, [x, a, y]) = fixture([1, 0, 1]);
|
||||
s.execute("object.arrange", &json!({"to": direction, "ids": [a.0]})).unwrap();
|
||||
assert_eq!(stack(&s), [x, a, y], "only object on its layer");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrange_selection_on_two_layers_moves_each_layer_independently() {
|
||||
for direction in ["forward", "backward"] {
|
||||
let (mut s, [a, x, b, y]) = fixture([0, 1, 0, 1]);
|
||||
let selected = if direction == "forward" { [x.0, a.0] } else { [b.0, y.0] };
|
||||
s.execute("object.arrange", &json!({"to": direction, "ids": selected})).unwrap();
|
||||
assert_eq!(stack(&s), [b, y, a, x]);
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
assert_eq!(doc.hit_item(0, Point::new(50.0, 50.0), 0.0), Some(a));
|
||||
assert_eq!(doc.hit_item(0, Point::new(250.0, 50.0), 0.0), Some(x));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrange_menu_steps_use_same_layer_order() {
|
||||
for command in ["object.bringForward", "object.sendBackward"] {
|
||||
let (mut s, [a, x, b]) = fixture([0, 1, 0]);
|
||||
let selected = if command == "object.bringForward" { a } else { b };
|
||||
s.execute("selection.set", &json!({"ids": [selected.0]})).unwrap();
|
||||
s.execute(command, &json!({})).unwrap();
|
||||
assert_eq!(stack(&s), [b, x, a]);
|
||||
assert_eq!(s.doc().unwrap().doc.hit_item(0, Point::new(50.0, 50.0), 0.0), Some(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod step_repeat_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn swap_and_default_fill_stroke_are_one_undo_step_per_object() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = |v: Value| ItemId(v["id"].as_u64().unwrap());
|
||||
let a = id(s.execute("frame.create", &json!({"rect": [0, 0, 10, 10]})).unwrap());
|
||||
let b = id(s.execute("frame.create", &json!({"rect": [20, 0, 30, 10]})).unwrap());
|
||||
let locked = id(s.execute("frame.create", &json!({"rect": [40, 0, 50, 10]})).unwrap());
|
||||
s.execute("object.fill", &json!({"ids": [a.0], "swatch": "[Paper]", "tint": 0.5})).unwrap();
|
||||
s.execute("object.stroke", &json!({"ids": [a.0], "swatch": "[Black]", "weight": 2})).unwrap();
|
||||
s.execute("object.fill", &json!({"ids": [b.0, locked.0], "swatch": "[Black]"})).unwrap();
|
||||
s.execute("object.stroke", &json!({"ids": [b.0, locked.0], "swatch": "[None]"})).unwrap();
|
||||
s.execute("object.lock", &json!({"ids": [locked.0]})).unwrap();
|
||||
s.execute("selection.set", &json!({"ids": [a.0, b.0, locked.0]})).unwrap();
|
||||
let colors = |s: &Session, i: ItemId| {
|
||||
let it = s.doc().unwrap().doc.item(i).unwrap().clone();
|
||||
(it.fill.swatch, it.fill.tint, it.stroke.swatch, it.stroke.tint)
|
||||
};
|
||||
let before = [colors(&s, a), colors(&s, b), colors(&s, locked)];
|
||||
|
||||
assert_eq!(s.execute("object.swapFillStroke", &json!({})).unwrap()["changed"], 2);
|
||||
assert_eq!(colors(&s, a), ("[Black]".into(), 1.0, "[Paper]".into(), 0.5));
|
||||
assert_eq!(colors(&s, b), ("[None]".into(), 1.0, "[Black]".into(), 1.0));
|
||||
assert_eq!(colors(&s, locked), before[2], "a locked object keeps its colours");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!([colors(&s, a), colors(&s, b), colors(&s, locked)], before, "one undo restores both");
|
||||
|
||||
s.execute("object.defaultFillStroke", &json!({})).unwrap();
|
||||
for i in [a, b] {
|
||||
assert_eq!(colors(&s, i), ("[None]".into(), 1.0, "[Black]".into(), 1.0));
|
||||
}
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!([colors(&s, a), colors(&s, b), colors(&s, locked)], before);
|
||||
|
||||
s.execute("selection.set", &json!({"ids": [locked.0]})).unwrap();
|
||||
assert!(s.execute("object.swapFillStroke", &json!({})).is_err(), "nothing unlocked to change");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_repeat_validates_counts_and_offsets() {
|
||||
assert_eq!(step_repeat_offsets(&json!({"count": MAX_STEP_REPEAT_COPIES})).unwrap().len(), MAX_STEP_REPEAT_COPIES as usize);
|
||||
@@ -2142,7 +2626,7 @@ mod scale_tests {
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [100, 100, 200, 150]})).unwrap();
|
||||
let id = ItemId(r["id"].as_u64().unwrap());
|
||||
s.execute("object.stroke", &json!({"weight": 4, "swatch": "Black"})).unwrap();
|
||||
s.execute("object.stroke", &json!({"weight": 4, "swatch": "[Black]"})).unwrap();
|
||||
let weight = |s: &Session| s.doc().unwrap().doc.item(id).unwrap().stroke.weight;
|
||||
let bounds = |s: &Session| s.doc().unwrap().doc.item(id).unwrap().bounds();
|
||||
s.execute("transform.scale", &json!({"sx": 2.0})).unwrap();
|
||||
@@ -2454,8 +2938,8 @@ mod fitting_tests {
|
||||
s.execute("object.fittingOptions", &json!({"ids": [f], "fitting": "fitProportionally", "align": 0})).unwrap();
|
||||
let c = content(&s);
|
||||
assert!((c.x0 - 100.0).abs() < 1e-6 && (c.y0 - 100.0).abs() < 1e-6 && (c.width() - 200.0).abs() < 1e-6, "{c:?}");
|
||||
// Crop 10 pt on every side: the content spreads past the frame.
|
||||
s.execute("object.fittingOptions", &json!({"ids": [f], "crop": 10, "align": 4})).unwrap();
|
||||
// Crop 10 pt on every side and fit again: the content spreads past the frame.
|
||||
s.execute("object.fittingOptions", &json!({"ids": [f], "fitting": "fitProportionally", "crop": 10, "align": 4})).unwrap();
|
||||
let c = content(&s);
|
||||
assert!((c.width() - 220.0).abs() < 1e-6, "{c:?}");
|
||||
// Auto-fit: resizing the frame refits the content.
|
||||
@@ -2557,6 +3041,18 @@ mod line_end_tests {
|
||||
s.execute("text.insert", &json!({"text": "\rFour", "raw": true})).unwrap();
|
||||
assert_eq!(story(&s).text, "One\nTwo\nThree\nFour");
|
||||
assert_eq!(story(&s).paras.len(), 4);
|
||||
// Editing a range or the whole story (scripts written for InDesign end paragraphs with \r).
|
||||
s.execute("story.replaceRange", &json!({"story": sid.0, "start": 0, "end": 3, "text": "Zero\rOne"})).unwrap();
|
||||
assert_eq!((story(&s).text.as_str(), story(&s).paras.len()), ("Zero\nOne\nTwo\nThree\nFour", 5));
|
||||
s.execute("story.setText", &json!({"story": sid.0, "text": "A\r\nB\rC"})).unwrap();
|
||||
assert_eq!((story(&s).text.as_str(), story(&s).paras.len()), ("A\nB\nC", 3));
|
||||
// Notes, made and edited.
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 1})).unwrap();
|
||||
let n = s.execute("note.new", &json!({"text": "One\rTwo"})).unwrap();
|
||||
let note = |s: &mut Session| s.execute("note.list", &json!({})).unwrap()[0]["text"].clone();
|
||||
assert_eq!(note(&mut s), "One\nTwo");
|
||||
s.execute("note.edit", &json!({"story": n["story"], "id": n["id"], "text": "Three\r\nFour"})).unwrap();
|
||||
assert_eq!(note(&mut s), "Three\nFour");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2705,3 +3201,181 @@ mod blend_mode_tests {
|
||||
assert_eq!(it.opacity, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod copy_ids_tests {
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn copies_report_their_ids() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let a = s.execute("frame.create", &json!({"rect": [0, 0, 50, 50]})).unwrap()["id"].as_u64().unwrap();
|
||||
let ids = |r: &serde_json::Value| r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect::<Vec<_>>();
|
||||
let selected = |s: &crate::Session| s.doc().unwrap().selection.items.iter().map(|i| i.0).collect::<Vec<_>>();
|
||||
// Moving reports what moved; copying reports the copies, which are selected.
|
||||
assert_eq!(ids(&s.execute("transform.move", &json!({"dx": 5, "dy": 0, "ids": [a]})).unwrap()), [a]);
|
||||
let r = s.execute("transform.move", &json!({"dx": 100, "dy": 0, "copy": true, "ids": [a]})).unwrap();
|
||||
let copy = ids(&r);
|
||||
assert!(copy.len() == 1 && copy[0] != a && s.doc().unwrap().doc.item(designcraft_doc::ItemId(copy[0])).is_some(), "{r}");
|
||||
assert_eq!(selected(&s), copy);
|
||||
// Edit › Duplicate.
|
||||
let r = s.execute("edit.duplicate", &json!({})).unwrap();
|
||||
let dup = ids(&r);
|
||||
assert!(dup.len() == 1 && dup[0] != copy[0], "{r}");
|
||||
assert_eq!(selected(&s), dup);
|
||||
// Step and Repeat: every copy.
|
||||
let r = s.execute("edit.stepAndRepeat", &json!({"count": 3, "dx": 0, "dy": 60, "ids": [a]})).unwrap();
|
||||
assert_eq!((r["created"].as_u64(), ids(&r).len()), (Some(3), 3), "{r}");
|
||||
assert!(ids(&r).iter().all(|i| s.doc().unwrap().doc.item(designcraft_doc::ItemId(*i)).is_some()));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod swatch_name_tests {
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn fills_and_strokes_must_name_a_swatch_and_change_nothing_otherwise() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [0, 0, 100, 100]})).unwrap()["id"].clone();
|
||||
for (cmd, params, hint) in [
|
||||
("object.fill", json!({"swatch": "Paper", "ids": [id]}), Some("[Paper]")),
|
||||
("object.fill", json!({"swatch": "[black]", "ids": [id]}), Some("[Black]")),
|
||||
("object.fill", json!({"swatch": "Missing Ink", "ids": [id]}), None),
|
||||
("object.stroke", json!({"swatch": "#ff0000", "weight": 4, "ids": [id]}), None),
|
||||
("object.stroke", json!({"gapSwatch": "paper", "ids": [id]}), Some("[Paper]")),
|
||||
] {
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let e = s.execute(cmd, ¶ms).unwrap_err().to_string();
|
||||
assert!(e.contains("no swatch named") && e.contains("swatch.create"), "{cmd}: {e}");
|
||||
if let Some(h) = hint {
|
||||
assert!(e.contains(&format!("did you mean `{h}`")), "{cmd}: {e}");
|
||||
}
|
||||
assert!(std::sync::Arc::ptr_eq(&before, &s.doc().unwrap().doc), "{cmd}: nothing changed");
|
||||
}
|
||||
// Real names, [None] included, still work.
|
||||
for sw in ["[Paper]", "[None]", "C=100 M=0 Y=0 K=0"] {
|
||||
s.execute("object.fill", &json!({"swatch": sw, "ids": [id]})).unwrap();
|
||||
s.execute("object.stroke", &json!({"swatch": sw, "ids": [id]})).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod path_edit_tests {
|
||||
use designcraft_doc::ItemId;
|
||||
use designcraft_geom::Point;
|
||||
use serde_json::json;
|
||||
|
||||
fn session_with_path() -> (crate::Session, u64) {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s
|
||||
.execute(
|
||||
"path.create",
|
||||
&json!({"anchors": [{"p": [100, 100]}, {"p": [200, 100], "in": [170, 80], "out": [230, 120]}, {"p": [300, 200]}]}),
|
||||
)
|
||||
.unwrap()["id"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
fn anchors(s: &crate::Session, id: u64) -> Vec<designcraft_geom::Anchor> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let it = d.item(ItemId(id)).unwrap();
|
||||
let xf = d.parent_xf(&d.find(ItemId(id)).unwrap()) * it.xf;
|
||||
it.path.subpaths[0].anchors.iter().map(|a| a.transform(xf)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_path_swaps_ends_and_direction_lines() {
|
||||
let (mut s, id) = session_with_path();
|
||||
s.execute("path.reverse", &json!({"ids": [id]})).unwrap();
|
||||
let a = anchors(&s, id);
|
||||
assert_eq!(a.iter().map(|a| a.p).collect::<Vec<_>>(), [Point::new(300.0, 200.0), Point::new(200.0, 100.0), Point::new(100.0, 100.0)]);
|
||||
assert_eq!((a[1].h_in, a[1].h_out), (Point::new(230.0, 120.0), Point::new(170.0, 80.0)));
|
||||
assert!(s.execute("path.reverse", &json!({"ids": [id], "subpath": 3})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anchors_go_on_the_named_open_subpath_and_set_anchor_reshapes_one() {
|
||||
let (mut s, id) = session_with_path();
|
||||
s.execute("path.appendAnchor", &json!({"id": id, "subpath": 0, "anchor": {"p": [400, 250]}})).unwrap();
|
||||
assert_eq!(anchors(&s, id).last().unwrap().p, Point::new(400.0, 250.0));
|
||||
assert!(s.execute("path.appendAnchor", &json!({"id": id, "subpath": 1, "anchor": {"p": [0, 0]}})).is_err());
|
||||
// Direction lines on the last anchor: collinear and opposite make it smooth.
|
||||
s.execute("path.setAnchor", &json!({"id": id, "anchor": "last", "in": [380, 230], "out": [420, 270]})).unwrap();
|
||||
let last = *anchors(&s, id).last().unwrap();
|
||||
assert_eq!((last.h_in, last.h_out, last.kind), (Point::new(380.0, 230.0), Point::new(420.0, 270.0), designcraft_geom::AnchorKind::Smooth));
|
||||
s.execute("path.setAnchor", &json!({"id": id, "anchor": 0, "p": [90, 110]})).unwrap();
|
||||
assert_eq!(anchors(&s, id)[0].p, Point::new(90.0, 110.0));
|
||||
assert!(s.execute("path.setAnchor", &json!({"id": id, "anchor": 9, "p": [0, 0]})).is_err());
|
||||
assert!(s.execute("path.setAnchor", &json!({"id": id, "anchor": "last"})).is_err(), "nothing to set");
|
||||
// Closing one subpath; then it takes no more anchors.
|
||||
s.execute("path.close", &json!({"id": id, "subpath": 0})).unwrap();
|
||||
assert!(s.execute("path.appendAnchor", &json!({"id": id, "anchor": {"p": [0, 0]}})).is_err());
|
||||
assert!(s.execute("path.close", &json!({"id": id, "subpath": 4})).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod spread_target_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn creating_on_a_parent_spread_and_rejecting_unknown_spreads() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 1, "facingPages": false})).unwrap();
|
||||
let parent = json!({"kind": "parent", "index": 0});
|
||||
let r = s.execute("frame.create", &json!({"spread": parent, "rect": [0, 0, 100, 100]})).unwrap();
|
||||
let id = ItemId(r["id"].as_u64().unwrap());
|
||||
assert_eq!(s.doc().unwrap().doc.find(id).unwrap().spread, SpreadRef::Parent(0));
|
||||
s.execute("line.create", &json!({"spread": parent, "a": [0, 0], "b": [50, 50]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.parents[0].items.len(), 2);
|
||||
|
||||
// Each of these used to land on the first body page.
|
||||
for bad in
|
||||
[json!("A-Parent"), json!("0"), json!(-1), json!(1.5), json!({"kind": "master", "index": 0}), json!({"kind": "parent"}), json!([0])]
|
||||
{
|
||||
assert!(s.execute("frame.create", &json!({"spread": bad, "rect": [0, 0, 100, 100]})).is_err(), "frame.create spread {bad}");
|
||||
assert!(s.execute("line.create", &json!({"spread": bad, "a": [0, 0], "b": [50, 50]})).is_err(), "line.create spread {bad}");
|
||||
}
|
||||
assert!(s.doc().unwrap().doc.spreads[0].items.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod fill_tint_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_tint_alone_keeps_each_objects_own_swatch() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let a = s.execute("frame.create", &json!({"rect": [36, 36, 100, 100]})).unwrap()["id"].as_u64().unwrap();
|
||||
let b = s.execute("frame.create", &json!({"rect": [136, 36, 200, 100]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("object.fill", &json!({"ids": [a], "swatch": "C=100 M=0 Y=0 K=0"})).unwrap();
|
||||
s.execute("object.fill", &json!({"ids": [b], "swatch": "C=0 M=100 Y=0 K=0"})).unwrap();
|
||||
s.execute("object.fill", &json!({"ids": [a, b], "tint": 0.4})).unwrap();
|
||||
let fill_of = |s: &Session, id: u64| s.doc().unwrap().doc.item(ItemId(id)).unwrap().fill.clone();
|
||||
assert_eq!((fill_of(&s, a).swatch.as_str(), fill_of(&s, a).tint), ("C=100 M=0 Y=0 K=0", 0.4));
|
||||
assert_eq!((fill_of(&s, b).swatch.as_str(), fill_of(&s, b).tint), ("C=0 M=100 Y=0 K=0", 0.4));
|
||||
// A swatch without a tint is at 100 %, as before.
|
||||
s.execute("object.fill", &json!({"ids": [a], "swatch": "[Black]"})).unwrap();
|
||||
assert_eq!(fill_of(&s, a).tint, 1.0);
|
||||
// Neither, or a tint out of range: refused.
|
||||
assert!(s.execute("object.fill", &json!({"ids": [a]})).is_err());
|
||||
assert!(s.execute("object.fill", &json!({"ids": [a], "tint": 2})).is_err());
|
||||
assert!(s.execute("object.fill", &json!({"ids": [a], "tint": "x"})).is_err());
|
||||
// A stroke tint alone: stroked objects take it, an object without a stroke keeps none.
|
||||
s.execute("object.stroke", &json!({"ids": [a], "swatch": "C=100 M=0 Y=0 K=0", "weight": 2})).unwrap();
|
||||
s.execute("object.stroke", &json!({"ids": [b], "swatch": "[None]"})).unwrap();
|
||||
s.execute("object.stroke", &json!({"ids": [a, b], "tint": 0.5})).unwrap();
|
||||
let stroke_of = |s: &Session, id: u64| s.doc().unwrap().doc.item(ItemId(id)).unwrap().stroke.clone();
|
||||
assert_eq!((stroke_of(&s, a).swatch.as_str(), stroke_of(&s, a).tint, stroke_of(&s, a).weight), ("C=100 M=0 Y=0 K=0", 0.5, 2.0));
|
||||
assert!(stroke_of(&s, b).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,13 +285,13 @@ fn write_links(d: &Document, root: &OutputRoot, dir: &Path, only: Option<&[Asset
|
||||
let mut out = d.clone();
|
||||
let mut taken = Vec::new();
|
||||
let mut written = Vec::new();
|
||||
let mut ids: Vec<AssetId> = d.assets.keys().copied().collect();
|
||||
ids.sort_by_key(|i| i.0);
|
||||
// Only files something places; the saved document leaves the others out.
|
||||
let ids = d.used_assets();
|
||||
for id in ids {
|
||||
if only.is_some_and(|o| !o.contains(&id)) {
|
||||
continue;
|
||||
}
|
||||
let a = &d.assets[&id];
|
||||
let Some(a) = d.assets.get(&id) else { continue };
|
||||
let name = link_name(a.link.as_deref().unwrap_or(&a.name), id, &mut taken);
|
||||
let path = dir.join(&name);
|
||||
root.write(&path, a.data.as_slice())?;
|
||||
|
||||
@@ -23,7 +23,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Type on a Path Options…",
|
||||
["Type", "Type on a Path"],
|
||||
None,
|
||||
"{id?, start?, flip?, align?: baseline|ascender|descender|center, delete?: true (Delete Type from Path)}",
|
||||
"{id?, start?, end?: distance along the path or null (the path's end), flip?, align?: baseline|ascender|descender|center, delete?: true (Delete Type from Path)} — start and end are the brackets: start ≤ end, both on the path",
|
||||
has_doc,
|
||||
path_options
|
||||
),
|
||||
@@ -36,7 +36,7 @@ fn align_param(p: &Value) -> Result<Option<PathAlign>> {
|
||||
|
||||
fn on_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let id = super::id_param(p, "id").ok_or_else(|| bad("type.onPath", "missing id"))?;
|
||||
let text = super::str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
let mut pt = PathType {
|
||||
start: p.get("start").and_then(Value::as_f64).unwrap_or(0.0).max(0.0),
|
||||
flip: p.get("flip").and_then(Value::as_bool).unwrap_or(false),
|
||||
@@ -86,6 +86,7 @@ fn path_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let p = p.clone();
|
||||
s.edit(|d, sel| {
|
||||
let it = d.item_mut(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
let len = designcraft_geom::warp::PathWarp::new(&it.path.to_bezpath(), false).length();
|
||||
let Content::Text(tf) = &mut it.content else { return Err(bad("type.pathOptions", "not type on a path")) };
|
||||
let Some(pt) = tf.options.path.as_mut() else { return Err(bad("type.pathOptions", "not type on a path")) };
|
||||
if delete {
|
||||
@@ -95,8 +96,26 @@ fn path_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
sel.text = None;
|
||||
return Ok(json!({"deleted": true}));
|
||||
}
|
||||
if let Some(v) = p.get("start").and_then(Value::as_f64) {
|
||||
pt.start = v.max(0.0);
|
||||
if let Some(v) = p.get("start").and_then(Value::as_f64).filter(|v| v.is_finite()) {
|
||||
pt.start = v.max(0.0).min(len);
|
||||
}
|
||||
match p.get("end") {
|
||||
Some(Value::Null) => pt.end = None,
|
||||
Some(v) => {
|
||||
let e = v.as_f64().filter(|v| v.is_finite()).ok_or_else(|| bad("type.pathOptions", "end: a distance or null"))?;
|
||||
pt.end = (e < len - 0.01).then_some(e.max(0.0));
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
// The brackets can't cross: the one being moved stops at the other.
|
||||
if let Some(e) = pt.end
|
||||
&& pt.start > e
|
||||
{
|
||||
if p.get("end").is_some() {
|
||||
pt.end = Some(pt.start);
|
||||
} else {
|
||||
pt.start = e;
|
||||
}
|
||||
}
|
||||
if let Some(v) = p.get("flip").and_then(Value::as_bool) {
|
||||
pt.flip = v;
|
||||
@@ -104,7 +123,7 @@ fn path_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(a) = align {
|
||||
pt.align = a;
|
||||
}
|
||||
Ok(json!({"start": pt.start, "flip": pt.flip, "align": pt.align}))
|
||||
Ok(json!({"start": pt.start, "end": pt.end, "flip": pt.flip, "align": pt.align}))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -160,4 +179,93 @@ mod tests {
|
||||
let pdf = designcraft_pdf::export_pdf(&d, &s2.cache, &Default::default()).unwrap();
|
||||
assert!(pdf.len() > 1000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_brackets_set_where_the_text_starts_and_ends() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let id = s.execute("line.create", &json!({"a": [100, 300], "b": [500, 300]})).unwrap()["id"].as_u64().unwrap();
|
||||
let r = s.execute("type.onPath", &json!({"id": id, "text": "Sixteen letters!"})).unwrap();
|
||||
let sid = designcraft_doc::StoryId(r["story"].as_u64().unwrap());
|
||||
let overset = |s: &Session| designcraft_compose::compose_story(&s.doc().unwrap().doc, sid, &Default::default()).overset_at.is_some();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
assert!(!overset(&s));
|
||||
// The end bracket close to the start: the text no longer fits, and is overset.
|
||||
let r = s.execute("type.pathOptions", &json!({"id": id, "start": 50, "end": 70})).unwrap();
|
||||
assert_eq!((r["start"].as_f64(), r["end"].as_f64()), (Some(50.0), Some(70.0)));
|
||||
assert!(overset(&s));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "one undo step");
|
||||
// Brackets can't cross: the start stops at the end, the end at the start.
|
||||
let r = s.execute("type.pathOptions", &json!({"id": id, "start": 90})).unwrap();
|
||||
assert_eq!(r["start"].as_f64(), Some(70.0));
|
||||
let r = s.execute("type.pathOptions", &json!({"id": id, "start": 10, "end": 5})).unwrap();
|
||||
assert_eq!((r["start"].as_f64(), r["end"].as_f64()), (Some(10.0), Some(10.0)));
|
||||
// Past the path, or null: the path's end again; the start stays on the path.
|
||||
let r = s.execute("type.pathOptions", &json!({"id": id, "end": 1000})).unwrap();
|
||||
assert!(r["end"].is_null());
|
||||
let r = s.execute("type.pathOptions", &json!({"id": id, "end": 200})).unwrap();
|
||||
assert_eq!(r["end"].as_f64(), Some(200.0));
|
||||
let r = s.execute("type.pathOptions", &json!({"id": id, "end": null, "start": 5000})).unwrap();
|
||||
assert_eq!((r["start"].as_f64(), r["end"].is_null()), (Some(400.0), true));
|
||||
assert!(s.execute("type.pathOptions", &json!({"id": id, "end": "far"})).is_err());
|
||||
// Saved with the document.
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let back = crate::cmd::file_from(&crate::cmd::file_bytes(&d)).unwrap();
|
||||
let path = |d: &designcraft_doc::Document| d.item(designcraft_doc::ItemId(id)).unwrap().text_frame().unwrap().options.path.clone();
|
||||
assert_eq!(path(&back), path(&d));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_type_tool_clicks_and_selects_along_a_curved_path() {
|
||||
for flip in [false, true] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
// An arch: up from (100, 400) over (300, 200) and down to (500, 400).
|
||||
let id = s
|
||||
.execute("path.create", &json!({"anchors": [{"p": [100, 400], "out": [100, 250]}, {"p": [300, 200], "in": [200, 200], "out": [400, 200]}, {"p": [500, 400], "in": [500, 250]}]}))
|
||||
.unwrap()["id"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
let r = s.execute("type.onPath", &json!({"id": id, "text": "AAAA BBBB CCCC DDDD EEEE FFFF GGGG", "flip": flip})).unwrap();
|
||||
let sid = designcraft_doc::StoryId(r["story"].as_u64().unwrap());
|
||||
// Where each glyph sits on the path, from the composed line.
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let it = d.item(designcraft_doc::ItemId(id)).unwrap();
|
||||
let cs = designcraft_compose::compose_story(&d, sid, &Default::default());
|
||||
let ft = &cs.frames[0];
|
||||
let map = designcraft_compose::PathTextMap::new(it, ft).unwrap();
|
||||
let l = &ft.lines[0];
|
||||
let glyph = |byte: usize| l.glyphs.iter().find(|g| g.byte == byte).unwrap();
|
||||
let on_path = |byte: usize| {
|
||||
let g = glyph(byte);
|
||||
// A little outside the path, as a click near the letter would be.
|
||||
map.to_path(designcraft_geom::Point::new(g.x + g.adv * 0.3, l.baseline - 4.0))
|
||||
};
|
||||
// A click by the first C (byte 10), on the curve: the caret goes there.
|
||||
let p = on_path(10);
|
||||
let r = s.execute("text.placeCaret", &json!({"frame": id, "point": [p.x, p.y]})).unwrap();
|
||||
assert_eq!(r["pos"], 10, "flip={flip}: {r}");
|
||||
// Dragging to the first E (byte 20) selects C to D.
|
||||
let q = on_path(20);
|
||||
s.execute("text.extendTo", &json!({"frame": id, "point": [q.x, q.y]})).unwrap();
|
||||
let t = s.doc().unwrap().selection.text.unwrap();
|
||||
assert_eq!(t.range(), 10..20, "flip={flip}");
|
||||
// A double-click on a word (its first click places the caret) selects it.
|
||||
let w = on_path(16);
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 0})).unwrap();
|
||||
s.execute("text.selectWord", &json!({"frame": id, "point": [w.x, w.y]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.text.unwrap().range(), 15..19, "flip={flip}");
|
||||
// A click on a letter, above the path, hits the frame (the type tool's hit test).
|
||||
let above = map.to_path(designcraft_geom::Point::new(glyph(10).x, l.baseline - l.ascent * 0.7));
|
||||
let sp = s.doc().unwrap().doc.clone();
|
||||
assert_eq!(
|
||||
sp.hit_item(0, sp.parent_xf(&sp.find(designcraft_doc::ItemId(id)).unwrap()) * above, 1.0),
|
||||
Some(designcraft_doc::ItemId(id)),
|
||||
"flip={flip}"
|
||||
);
|
||||
// The map runs both ways along the path.
|
||||
let back = map.to_line(map.to_path(designcraft_geom::Point::new(123.0, l.baseline)));
|
||||
assert!((back.x - 123.0).abs() < 0.01, "{back:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,19 +124,25 @@ fn fit(d: &Document, sid: StoryId) -> (Option<usize>, usize, usize) {
|
||||
}
|
||||
|
||||
/// Thread new frames on new pages (margin rectangles) until the story fits. Returns pages added.
|
||||
pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize) -> Result<usize> {
|
||||
/// A story on a parent spread doesn't autoflow: its frames thread only on that parent. Errors
|
||||
/// name `cmd`, the command that autoflows.
|
||||
pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize, cmd: &str) -> Result<usize> {
|
||||
let last = d.story(sid).and_then(|s| s.frames.last().copied()).ok_or_else(|| bad(cmd, "the story has no frame"))?;
|
||||
if !matches!(d.find(last).map(|l| l.spread), Some(SpreadRef::Doc(_))) {
|
||||
return Err(bad(cmd, "autoflow works only from a frame on a document page"));
|
||||
}
|
||||
let mut added = 0;
|
||||
loop {
|
||||
let (overset, held, full) = fit(d, sid);
|
||||
let Some(at) = overset else { return Ok(added) };
|
||||
if added >= max_pages {
|
||||
return Err(bad("file.place", format!("autoflow stopped after {max_pages} pages")));
|
||||
return Err(bad(cmd, format!("autoflow stopped after {max_pages} pages")));
|
||||
}
|
||||
let len = d.story(sid).map_or(0, |s| s.len());
|
||||
// Frames still needed at the rate the full ones are filled (at least one); with no full
|
||||
// frame to go by yet, one more page.
|
||||
let want = held.checked_div(full).map_or(1, |per_frame| len.saturating_sub(at).div_ceil(per_frame.max(1))).clamp(1, max_pages - added);
|
||||
let last = *d.story(sid).and_then(|s| s.frames.last()).ok_or_else(|| bad("file.place", "the story has no frame"))?;
|
||||
let last = *d.story(sid).and_then(|s| s.frames.last()).ok_or_else(|| bad(cmd, "the story has no frame"))?;
|
||||
let mut page = d.page_of_item(last).unwrap_or(d.page_count().saturating_sub(1));
|
||||
let parent = d.page_loc(page).and_then(|(si, pi)| d.spreads[si].pages[pi].parent);
|
||||
d.insert_pages(Some(page), want, parent)?;
|
||||
@@ -144,7 +150,7 @@ pub(crate) fn autoflow(d: &mut Document, sid: StoryId, max_pages: usize) -> Resu
|
||||
let mut prev = last;
|
||||
for _ in 0..want {
|
||||
page += 1;
|
||||
let (si, pi) = d.page_loc(page).ok_or_else(|| bad("file.place", "page insert failed"))?;
|
||||
let (si, pi) = d.page_loc(page).ok_or_else(|| bad(cmd, "page insert failed"))?;
|
||||
let r = d.spreads[si].pages[pi].margin_rect();
|
||||
let (fid, _) = d.add_text_frame(SpreadRef::Doc(si), r, layer, "", ParaFormat::default())?;
|
||||
d.thread(prev, fid)?;
|
||||
@@ -230,7 +236,7 @@ pub(super) fn place_text(s: &mut Session, p: &Value, name: &str, bytes: &[u8]) -
|
||||
*sel = designcraft_doc::Selection::items(vec![fid]);
|
||||
sid
|
||||
};
|
||||
let pages = if autoflow_on { autoflow(d, sid, 2000)? } else { 0 };
|
||||
let pages = if autoflow_on { autoflow(d, sid, 2000, "file.place")? } else { 0 };
|
||||
let overset = fit(d, sid).0.is_some();
|
||||
Ok(json!({"story": sid.0, "pagesAdded": pages, "overset": overset, "warnings": warnings}))
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use designcraft_compose::ComposedStory;
|
||||
use designcraft_doc::{Content, Document, Intent, Item, SpreadRef};
|
||||
use designcraft_fonts::FaceRef;
|
||||
use designcraft_geom::Affine;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -29,10 +30,12 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"{minPpi?: 150} → issues [{severity, kind, message, item?, page?}]", has_doc, run)]
|
||||
}
|
||||
|
||||
fn page_of(d: &Document, sr: SpreadRef, it: &Item) -> Option<usize> {
|
||||
fn page_of(d: &Document, sr: SpreadRef, it: &Item, parent: Affine) -> Option<usize> {
|
||||
let SpreadRef::Doc(si) = sr else { return None };
|
||||
let sp = d.spreads.get(si)?;
|
||||
Some(d.first_page_of_spread(si) + sp.page_at_x(it.bounds().center().x).unwrap_or(0))
|
||||
// Item bounds include its own transform; ancestors bring the frame into spread space.
|
||||
let bounds = parent.transform_rect_bbox(it.bounds());
|
||||
Some(d.first_page_of_spread(si) + sp.page_at_x(bounds.center().x).unwrap_or(0))
|
||||
}
|
||||
|
||||
/// Characters a missing-glyph report names before "and N more".
|
||||
@@ -85,7 +88,7 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {
|
||||
missing_glyphs(&cs, &story.text, 0, &mut missing);
|
||||
if cs.is_overset() {
|
||||
let last = story.frames.last().copied();
|
||||
let page = last.and_then(|f| d.find(f).zip(d.item(f))).and_then(|(loc, it)| page_of(d, loc.spread, it));
|
||||
let page = last.and_then(|f| d.find(f).zip(d.item(f))).and_then(|(loc, it)| page_of(d, loc.spread, it, Affine::IDENTITY));
|
||||
let n = story.text[cs.overset_at.unwrap_or(0).min(story.len())..].chars().count();
|
||||
out.push(Issue { severity: "error", kind: "overset", message: format!("Overset text: {n} characters"), item: last.map(|i| i.0), page });
|
||||
}
|
||||
@@ -161,48 +164,52 @@ pub fn check(s: &Session, min_ppi: f64) -> Vec<Issue> {
|
||||
page: None,
|
||||
});
|
||||
}
|
||||
// Items.
|
||||
// Carry the ancestor transform in the same order as rendering, retaining pre-order.
|
||||
for (si, sp) in d.spreads.iter().enumerate() {
|
||||
for top in &sp.items {
|
||||
top.walk(&mut |it| {
|
||||
let page = page_of(d, SpreadRef::Doc(si), it);
|
||||
match &it.content {
|
||||
Content::Graphic(g) => match d.assets.get(&g.asset) {
|
||||
None => {
|
||||
out.push(Issue { severity: "error", kind: "missingLink", message: "Missing graphic".into(), item: Some(it.id.0), page })
|
||||
}
|
||||
Some(a) if a.data.is_empty() => out.push(Issue {
|
||||
severity: "error",
|
||||
kind: "missingLink",
|
||||
message: format!("Missing link: {}", a.link.as_deref().unwrap_or(&a.name)),
|
||||
item: Some(it.id.0),
|
||||
page,
|
||||
}),
|
||||
Some(a) => {
|
||||
// Effective ppi: pixels per inch at the placed size. Placed PDF and
|
||||
// SVG pages export as vectors; their `pixels` is only the preview's.
|
||||
let vector = designcraft_images::is_pdf(&a.data) || designcraft_images::is_svg(&a.data);
|
||||
if let Some((pw, _)) = a.pixels.filter(|_| !vector) {
|
||||
let placed_w = (g.xf * it.xf).as_coeffs()[0].hypot((g.xf * it.xf).as_coeffs()[1]) * g.size.0;
|
||||
let ppi = pw as f64 / (placed_w / 72.0).max(1e-6);
|
||||
if d.settings.intent == Intent::Print && ppi < min_ppi {
|
||||
out.push(Issue {
|
||||
severity: "warning",
|
||||
kind: "lowResolution",
|
||||
message: format!("{}: effective {ppi:.0} ppi (< {min_ppi:.0})", a.name),
|
||||
item: Some(it.id.0),
|
||||
page,
|
||||
});
|
||||
}
|
||||
let mut pending: Vec<_> = sp.items.iter().rev().map(|it| (it.as_ref(), Affine::IDENTITY)).collect();
|
||||
while let Some((it, parent)) = pending.pop() {
|
||||
let xf = parent * it.xf;
|
||||
pending.extend(it.children().iter().rev().map(|child| (child.as_ref(), xf)));
|
||||
let page = page_of(d, SpreadRef::Doc(si), it, parent);
|
||||
match &it.content {
|
||||
Content::Graphic(g) => match d.assets.get(&g.asset) {
|
||||
None => out.push(Issue { severity: "error", kind: "missingLink", message: "Missing graphic".into(), item: Some(it.id.0), page }),
|
||||
Some(a) if a.data.is_empty() => out.push(Issue {
|
||||
severity: "error",
|
||||
kind: "missingLink",
|
||||
message: format!("Missing link: {}", a.link.as_deref().unwrap_or(&a.name)),
|
||||
item: Some(it.id.0),
|
||||
page,
|
||||
}),
|
||||
Some(a) => {
|
||||
// Placed PDF and SVG pages export as vectors; `pixels` is only the preview's.
|
||||
let vector = designcraft_images::is_pdf(&a.data) || designcraft_images::is_svg(&a.data);
|
||||
if let Some((pw, ph)) = a.pixels.filter(|_| !vector) {
|
||||
// Measure each image axis in points, not its rotated bounding box or
|
||||
// cropped frame. Either axis can fall below the minimum resolution.
|
||||
let [xx, yx, xy, yy, _, _] = (xf * g.xf).as_coeffs();
|
||||
let placed_w = xx.hypot(yx) * g.size.0;
|
||||
let placed_h = xy.hypot(yy) * g.size.1;
|
||||
let ppi_x = pw as f64 / (placed_w / 72.0).max(1e-6);
|
||||
let ppi_y = ph as f64 / (placed_h / 72.0).max(1e-6);
|
||||
let ppi = ppi_x.min(ppi_y);
|
||||
if d.settings.intent == Intent::Print && ppi < min_ppi {
|
||||
out.push(Issue {
|
||||
severity: "warning",
|
||||
kind: "lowResolution",
|
||||
message: format!("{}: effective {ppi:.0} ppi (< {min_ppi:.0})", a.name),
|
||||
item: Some(it.id.0),
|
||||
page,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
Content::Text(tf) if d.story(tf.story).is_some_and(|s| s.is_empty()) => {
|
||||
out.push(Issue { severity: "warning", kind: "emptyText", message: "Empty text frame".into(), item: Some(it.id.0), page });
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Content::Text(tf) if d.story(tf.story).is_some_and(|s| s.is_empty()) => {
|
||||
out.push(Issue { severity: "warning", kind: "emptyText", message: "Empty text frame".into(), item: Some(it.id.0), page });
|
||||
}
|
||||
});
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
// RGB swatches in use in a print document.
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
//! File › Print: the pages go to a printer through the system print spooler (`lpr`), as PDF.
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
use std::io::Write;
|
||||
//! File › Print: the pages go to a printer through the system print spooler (craft-print), as PDF.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -20,91 +17,60 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Print…",
|
||||
["File"],
|
||||
Some("Cmd+P"),
|
||||
"{printer?, copies?: 1, pages?: \"1-3,5\", spreads?, marks?, bleed?, dryRun?: bool (only return the spool command)} → {printer, copies, pages, command}",
|
||||
"{printer?, copies?: 1, pages?: \"1-3,5\", spreads?, marks?, bleed?, dryRun?: bool (export and preview submission without printing)} → {printer, copies, pages, command, bytes}",
|
||||
has_doc,
|
||||
print
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn printers() -> Value {
|
||||
let run = |args: &[&str]| {
|
||||
std::process::Command::new("lpstat").args(args).output().ok().map(|o| String::from_utf8_lossy(&o.stdout).to_string()).unwrap_or_default()
|
||||
};
|
||||
let names: Vec<String> = run(&["-e"]).lines().map(str::trim).filter(|l| !l.is_empty()).map(str::to_string).collect();
|
||||
// "system default destination: NAME"
|
||||
let default = run(&["-d"]).rsplit(':').next().map(str::trim).filter(|s| !s.is_empty() && !s.contains("no system default")).map(str::to_string);
|
||||
let printers = craft_print::printers();
|
||||
let default = printers.iter().find(|p| p.default).map(|p| p.name.clone());
|
||||
let names: Vec<_> = printers.into_iter().map(|p| p.name).collect();
|
||||
json!({"printers": names, "default": default})
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn printers() -> Value {
|
||||
json!({"printers": [], "default": null})
|
||||
}
|
||||
|
||||
fn print(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let d = &s.doc()?.doc;
|
||||
let opts = super::export::options(p, d.page_count())?;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
// Even a dry run cannot construct a native spool command in the browser.
|
||||
let _ = opts;
|
||||
super::export::options(p, s.doc()?.doc.page_count())?;
|
||||
Err(bad("file.print", "printing isn't available on the web: export a PDF and print it"))
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let copies = p.get("copies").and_then(Value::as_u64).unwrap_or(1).clamp(1, 999);
|
||||
let printer = str_param(p, "printer").map(str::to_string);
|
||||
let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false);
|
||||
let pdf = designcraft_pdf::export_pdf(d, &s.cache, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let mut file = secure_print_file()?;
|
||||
let file_path = file.path().to_path_buf();
|
||||
let mut cmd: Vec<String> = vec!["lpr".into()];
|
||||
if let Some(pr) = &printer {
|
||||
cmd.extend(["-P".into(), pr.clone()]);
|
||||
}
|
||||
if copies > 1 {
|
||||
cmd.extend(["-#".into(), copies.to_string()]);
|
||||
}
|
||||
cmd.extend(["-T".into(), d.title.clone(), file_path.to_string_lossy().to_string()]);
|
||||
let pages = opts.pages.as_ref().map_or(d.page_count(), |v| v.len());
|
||||
let out = json!({"printer": printer, "copies": copies, "pages": pages, "command": cmd, "bytes": pdf.len()});
|
||||
if dry {
|
||||
return Ok(out);
|
||||
}
|
||||
file.as_file_mut().write_all(&pdf).map_err(|e| EngineError::Other(format!("{}: {e}", file.path().display())))?;
|
||||
file.as_file_mut().flush().map_err(|e| EngineError::Other(format!("{}: {e}", file.path().display())))?;
|
||||
let st = std::process::Command::new(&cmd[0]).args(&cmd[1..]).output().map_err(|e| bad("file.print", format!("lpr: {e}")))?;
|
||||
if !st.status.success() {
|
||||
return Err(bad("file.print", String::from_utf8_lossy(&st.stderr).trim().to_string()));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
print_with(s, p, craft_print::submit)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn secure_print_file() -> Result<tempfile::NamedTempFile> {
|
||||
let file = tempfile::Builder::new()
|
||||
.prefix("designcraft-print-")
|
||||
.suffix(".pdf")
|
||||
.tempfile()
|
||||
.map_err(|e| EngineError::Other(format!("temporary print file: {e}")))?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
file.as_file()
|
||||
.set_permissions(std::fs::Permissions::from_mode(0o600))
|
||||
.map_err(|e| EngineError::Other(format!("{}: {e}", file.path().display())))?;
|
||||
fn print_with(
|
||||
s: &mut Session,
|
||||
p: &Value,
|
||||
submit: impl FnOnce(&[u8], &craft_print::Job) -> std::result::Result<String, craft_print::Error>,
|
||||
) -> Result<Value> {
|
||||
let d = &s.doc()?.doc;
|
||||
let opts = super::export::options(p, d.page_count())?;
|
||||
let copies = p.get("copies").and_then(Value::as_u64).unwrap_or(1).clamp(1, 999) as u32;
|
||||
let job = craft_print::Job {
|
||||
printer: str_param(p, "printer").map(str::to_string),
|
||||
copies,
|
||||
scaling: craft_print::Scaling::ShrinkToFit,
|
||||
title: d.title.clone(),
|
||||
..craft_print::Job::default()
|
||||
};
|
||||
let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false);
|
||||
let pdf = designcraft_pdf::export_pdf(d, &s.cache, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let command = craft_print::command_preview(&job).map_err(|e| bad("file.print", e.to_string()))?;
|
||||
let pages = opts.pages.as_ref().map_or(d.page_count(), |v| v.len());
|
||||
let out = json!({"printer": job.printer, "copies": copies, "pages": pages, "command": command, "bytes": pdf.len()});
|
||||
if !dry {
|
||||
submit(&pdf, &job).map_err(|e| bad("file.print", e.to_string()))?;
|
||||
}
|
||||
Ok(file)
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn print_builds_the_spool_command() {
|
||||
@@ -112,37 +78,45 @@ mod tests {
|
||||
s.execute("file.new", &json!({"pages": 4})).unwrap();
|
||||
let r = s.execute("file.print", &json!({"printer": "Office", "copies": 2, "pages": "2-3", "dryRun": true})).unwrap();
|
||||
assert_eq!(r["pages"], 2);
|
||||
let cmd: Vec<&str> = r["command"].as_array().unwrap().iter().filter_map(|v| v.as_str()).collect();
|
||||
assert_eq!(&cmd[..5], ["lpr", "-P", "Office", "-#", "2"]);
|
||||
assert!(cmd.last().unwrap().ends_with(".pdf"));
|
||||
assert!(!std::path::Path::new(cmd.last().unwrap()).exists());
|
||||
let command: Vec<&str> = r["command"].as_array().unwrap().iter().filter_map(Value::as_str).collect();
|
||||
if craft_print::backend() == Some(craft_print::Backend::Cups) {
|
||||
assert_eq!(&command[..5], ["lp", "-d", "Office", "-n", "2"]);
|
||||
assert!(command.contains(&"print-scaling=auto-fit"));
|
||||
assert!(!command.iter().any(|a| a.starts_with("sides=")));
|
||||
} else {
|
||||
assert_eq!(command[0], "powershell");
|
||||
}
|
||||
assert!(r["bytes"].as_u64().unwrap() > 500);
|
||||
assert!(s.execute("file.print", &json!({"pages": "9", "dryRun": true})).is_err());
|
||||
assert!(s.execute("file.printers", &json!({})).unwrap()["printers"].is_array());
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn print_uses_distinct_ephemeral_files() {
|
||||
let first = super::secure_print_file().unwrap();
|
||||
let second = super::secure_print_file().unwrap();
|
||||
let first_path = first.path().to_path_buf();
|
||||
let second_path = second.path().to_path_buf();
|
||||
assert_ne!(first_path, second_path);
|
||||
drop((first, second));
|
||||
assert!(!first_path.exists());
|
||||
assert!(!second_path.exists());
|
||||
}
|
||||
|
||||
#[cfg(all(not(target_arch = "wasm32"), unix))]
|
||||
#[test]
|
||||
fn print_file_is_private_and_removed_on_drop() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let file = super::secure_print_file().unwrap();
|
||||
let path = file.path().to_path_buf();
|
||||
assert_eq!(file.as_file().metadata().unwrap().permissions().mode() & 0o777, 0o600);
|
||||
drop(file);
|
||||
assert!(!path.exists());
|
||||
fn print_submits_the_selected_export_and_reports_refusal() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 4})).unwrap();
|
||||
let params = json!({"printer": "Office (east)", "copies": 2, "pages": "2-3", "marks": true, "bleed": true});
|
||||
let expected = {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let options = super::super::export::options(¶ms, d.page_count()).unwrap();
|
||||
designcraft_pdf::export_pdf(d, &s.cache, &options).unwrap()
|
||||
};
|
||||
let r = print_with(&mut s, ¶ms, |pdf, job| {
|
||||
// The production export options and full bytes reach the shared spooler boundary.
|
||||
assert_eq!(pdf, expected);
|
||||
assert!(pdf.starts_with(b"%PDF-"));
|
||||
assert_eq!(job.printer.as_deref(), Some("Office (east)"));
|
||||
assert_eq!(job.copies, 2);
|
||||
assert_eq!(job.duplex, None);
|
||||
assert_eq!(job.scaling, craft_print::Scaling::ShrinkToFit);
|
||||
assert!(!job.title.is_empty());
|
||||
Ok("request id is Office-1".into())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(r["pages"], 2);
|
||||
let refusal = print_with(&mut s, ¶ms, |_, _| Err(craft_print::Error::Spool("printer offline".into()))).unwrap_err();
|
||||
assert!(refusal.to_string().contains("printer offline"));
|
||||
print_with(&mut s, &json!({"dryRun": true}), |_, _| panic!("dry runs must not spool")).unwrap();
|
||||
assert!(print_with(&mut s, &json!({"pages": "9"}), |_, _| panic!("invalid ranges must not spool")).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ fn qr_code(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let text = encode(p).map_err(|e| bad(ID, e))?;
|
||||
let color = str_param(p, "color").unwrap_or(designcraft_color::swatch::BLACK).to_string();
|
||||
let rect = rect_param(p, "rect");
|
||||
let sr = spread_param(p, "spread");
|
||||
let sr = spread_param(p, ID)?;
|
||||
let st = s.doc()?;
|
||||
let lid = st.active_layer;
|
||||
let sel = st.selection.items.clone();
|
||||
|
||||
+574
-44
@@ -105,7 +105,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"New Character Style…",
|
||||
[],
|
||||
None,
|
||||
"{name, basedOn?: style | \"[None]\", chars?: {…}} — the style sets only `chars` → {name} (made unique)",
|
||||
"{name, basedOn?: style | \"[None]\", chars?: {…}, fromSelection?: bool (default false)} — if true, capture the first selected character's style/local overrides (caret: typing format), then overlay chars (null unsets); only explicit fill names are validated; paragraph formatting stays inherited; no text target: chars only → {name} (made unique)",
|
||||
has_doc,
|
||||
create_char
|
||||
),
|
||||
@@ -180,10 +180,19 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Apply Color",
|
||||
[],
|
||||
None,
|
||||
"{color: \"#rrggbb\"|{c,m,y,k}(0..100)|[r,g,b](0..255), target?: fill|stroke, ids?} — an unnamed colour (not in the Swatches panel until Add to Swatches) on the selection",
|
||||
super::has_selection,
|
||||
"{color: \"#rrggbb\"|{c,m,y,k}(0..100)|[r,g,b](0..255), target?: fill|stroke, text?: bool, ids?} — an unnamed colour (not in the Swatches panel until Add to Swatches) on the selection; `text`: on the selected text, or the text of the selected frames (Formatting Affects Text)",
|
||||
has_text_or_selection,
|
||||
apply_color
|
||||
),
|
||||
cmd!(
|
||||
"color.sample",
|
||||
"Sample Color (Eyedropper)",
|
||||
[],
|
||||
None,
|
||||
"{at: [x,y] (spread coords), spread?, target?: fill|stroke, ids?} → {color: \"#rrggbb\", swatch, from: image id} — the Eyedropper on a placed image: the image's colour at the point (an unnamed colour) fills the selected text, else the selected objects",
|
||||
has_text_or_selection,
|
||||
sample_color
|
||||
),
|
||||
cmd!(
|
||||
"swatch.addToSwatches",
|
||||
"Add to Swatches",
|
||||
@@ -444,6 +453,7 @@ fn create_para(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
chars = full;
|
||||
}
|
||||
}
|
||||
super::text::validate_char_fill(&s.doc()?.doc, &chars, "style.paragraph.create")?;
|
||||
let based_on = str_param(p, "basedOn").map(str::to_string);
|
||||
let next = str_param(p, "nextStyle").map(str::to_string);
|
||||
s.edit(|d, _| {
|
||||
@@ -470,6 +480,7 @@ fn CharProps_to_attrs(p: &designcraft_doc::CharProps) -> CharAttrs {
|
||||
fn edit_para(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = str_param(p, "name").ok_or_else(|| bad("style.paragraph.edit", "missing name"))?.to_string();
|
||||
let chars: CharAttrs = attrs(p.get("chars"), |a: &mut CharAttrs, k, v| a.set_json(k, v))?;
|
||||
super::text::validate_char_fill(&s.doc()?.doc, &chars, "style.paragraph.edit")?;
|
||||
let rename = str_param(p, "rename").map(str::to_string);
|
||||
let based = p.get("basedOn").cloned();
|
||||
s.edit(|d, _| {
|
||||
@@ -620,7 +631,26 @@ fn delete_para(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
fn create_char(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let base = str_param(p, "name").unwrap_or("Character Style 1").to_string();
|
||||
let chars: CharAttrs = attrs(p.get("chars"), |a: &mut CharAttrs, k, v| a.set_json(k, v))?;
|
||||
let mut chars: CharAttrs = attrs(p.get("chars"), |a: &mut CharAttrs, k, v| a.set_json(k, v))?;
|
||||
// Validate caller-supplied fills; captured formatting may retain a deleted swatch name.
|
||||
super::text::validate_char_fill(&s.doc()?.doc, &chars, "style.character.create")?;
|
||||
if p.get("fromSelection").and_then(Value::as_bool).unwrap_or(false)
|
||||
&& let Some(target) = super::text::format_targets_pub(s).first()
|
||||
{
|
||||
let st = s.doc()?;
|
||||
// Match paragraph fromSelection's first-character/caret policy, but keep paragraph
|
||||
// attributes inherited instead of freezing the fully resolved character properties.
|
||||
let format = super::text::effective_char_format(s, target)?;
|
||||
let mut captured = st.doc.styles.char_style_attrs(&format.style);
|
||||
captured.merge(&format.over);
|
||||
// Replay the explicit fields so null clears a captured value rather than looking absent.
|
||||
if let Some(overrides) = p.get("chars").and_then(Value::as_object) {
|
||||
for (key, value) in overrides {
|
||||
captured.set_json(key, value).map_err(|e| bad("style", e))?;
|
||||
}
|
||||
}
|
||||
chars = captured;
|
||||
}
|
||||
// Based on [None] is based on nothing.
|
||||
let based_on = str_param(p, "basedOn").filter(|b| *b != designcraft_doc::NO_CHAR_STYLE).map(str::to_string);
|
||||
s.edit(|d, _| {
|
||||
@@ -641,6 +671,7 @@ fn edit_char(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad("style.character.edit", "[None] can't be edited"));
|
||||
}
|
||||
let chars: CharAttrs = attrs(p.get("chars"), |a: &mut CharAttrs, k, v| a.set_json(k, v))?;
|
||||
super::text::validate_char_fill(&s.doc()?.doc, &chars, "style.character.edit")?;
|
||||
// `null` unsets an attribute (the style no longer sets it).
|
||||
let unset: Vec<String> = p
|
||||
.get("chars")
|
||||
@@ -716,13 +747,18 @@ pub fn parse_color(v: &Value) -> Option<designcraft_color::Color> {
|
||||
use designcraft_color::Color;
|
||||
match v {
|
||||
Value::String(s) => Color::from_hex(s),
|
||||
// One scale per colour, not per component: {c: 0.7, y: 12.2} is C 0.7 %, Y 12.2 %. Only a colour whose
|
||||
// components are all within 0..1 is read as fractions (the older form).
|
||||
Value::Array(a) if a.len() >= 3 => {
|
||||
let f = |i: usize| a[i].as_f64().map(|x| if x > 1.0 { x / 255.0 } else { x } as f32);
|
||||
Some(Color::rgb(f(0)?, f(1)?, f(2)?))
|
||||
let v = [a[0].as_f64()?, a[1].as_f64()?, a[2].as_f64()?];
|
||||
let scale = if v.iter().any(|&x| x > 1.0) { 255.0 } else { 1.0 };
|
||||
Some(Color::rgb((v[0] / scale) as f32, (v[1] / scale) as f32, (v[2] / scale) as f32))
|
||||
}
|
||||
Value::Object(o) => {
|
||||
let g = |k: &str| o.get(k).and_then(Value::as_f64).map(|x| if x > 1.0 { x / 100.0 } else { x } as f32);
|
||||
Some(Color::cmyk(g("c")?, g("m")?, g("y")?, g("k")?))
|
||||
let g = |k: &str| o.get(k).and_then(Value::as_f64);
|
||||
let v = [g("c")?, g("m")?, g("y")?, g("k")?];
|
||||
let scale = if v.iter().any(|&x| x > 1.0) { 100.0 } else { 1.0 };
|
||||
Some(Color::cmyk((v[0] / scale) as f32, (v[1] / scale) as f32, (v[2] / scale) as f32, (v[3] / scale) as f32))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
@@ -835,7 +871,11 @@ fn save_swatches(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let bytes = designcraft_color::ase::write(&chosen);
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
std::fs::write(path, &bytes).map_err(|e| bad("swatch.save", format!("{path}: {e}")))?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let written = crate::atomic::write_atomic(std::path::Path::new(path), &bytes);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let written = std::fs::write(path, &bytes);
|
||||
written.map_err(|e| bad("swatch.save", format!("{path}: {e}")))?;
|
||||
Ok(json!({"path": path, "bytes": bytes.len()}))
|
||||
}
|
||||
None => Ok(json!({"base64": super::file::base64_encode(&bytes)})),
|
||||
@@ -942,41 +982,146 @@ fn apply_gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn apply_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let color = p.get("color").and_then(parse_color).ok_or_else(|| bad("object.color", "missing or bad color"))?;
|
||||
let stroke = str_param(p, "target") == Some("stroke");
|
||||
let ids = super::targets(s, p)?;
|
||||
s.edit(|d, _| {
|
||||
// Reuse a swatch holding exactly this colour, else add an unnamed one.
|
||||
let existing = d
|
||||
.swatches
|
||||
.iter()
|
||||
.find(|w| matches!(&w.value, SwatchValue::Color { color: c, color_type: designcraft_color::ColorType::Process } if *c == color))
|
||||
.map(|w| w.name.clone());
|
||||
let name = match existing {
|
||||
Some(n) => n,
|
||||
None => {
|
||||
let name = Styles::unique_name(|n| d.swatch(n).is_some(), &value_name(color));
|
||||
d.swatches.push(Swatch {
|
||||
name: name.clone(),
|
||||
value: SwatchValue::Color { color, color_type: designcraft_color::ColorType::Process },
|
||||
locked: false,
|
||||
named: false,
|
||||
hidden: true,
|
||||
});
|
||||
name
|
||||
}
|
||||
};
|
||||
for id in &ids {
|
||||
if let Some(it) = d.item_mut(*id) {
|
||||
if stroke {
|
||||
it.stroke.swatch = name.clone();
|
||||
it.stroke.tint = 1.0;
|
||||
} else {
|
||||
it.fill.swatch = name.clone();
|
||||
it.fill.tint = 1.0;
|
||||
}
|
||||
}
|
||||
let named = p.get("ids").is_some() || p.get("id").is_some();
|
||||
let text = !named
|
||||
&& (p.get("text").and_then(Value::as_bool).unwrap_or(false) || s.doc()?.selection.text.is_some() || s.doc()?.selection.cells.is_some());
|
||||
let (ids, texts) = if text { (vec![], super::text::format_targets_pub(s)) } else { (super::targets(s, p)?, vec![]) };
|
||||
if text && texts.is_empty() {
|
||||
return Err(bad("object.color", "select the text to colour"));
|
||||
}
|
||||
// A caret: the colour is for the next typing (once the swatch exists).
|
||||
let caret = matches!(texts.as_slice(), [t] if t.range.is_empty());
|
||||
let r = s.edit(|d, _| {
|
||||
let name = color_swatch(d, color);
|
||||
color_items(d, &ids, &name, stroke);
|
||||
if !caret {
|
||||
let a = if stroke {
|
||||
CharAttrs { stroke: Some(name.clone()), stroke_tint: Some(1.0), ..Default::default() }
|
||||
} else {
|
||||
CharAttrs { fill: Some(name.clone()), fill_tint: Some(1.0), ..Default::default() }
|
||||
};
|
||||
super::text::apply_char_attrs(d, &texts, &a, &[]);
|
||||
}
|
||||
Ok(json!({"swatch": name}))
|
||||
})
|
||||
})?;
|
||||
if caret && let Some(name) = r.get("swatch").cloned() {
|
||||
let attrs = if stroke { json!({"stroke": name, "strokeTint": 1.0}) } else { json!({"fill": name, "fillTint": 1.0}) };
|
||||
super::text::format_chars(s, &attrs)?;
|
||||
}
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
/// A process swatch holding exactly `color`, else a new unnamed one (hidden from the Swatches
|
||||
/// panel until Add to Swatches).
|
||||
fn color_swatch(d: &mut designcraft_doc::Document, color: designcraft_color::Color) -> String {
|
||||
let existing = d
|
||||
.swatches
|
||||
.iter()
|
||||
.find(|w| matches!(&w.value, SwatchValue::Color { color: c, color_type: designcraft_color::ColorType::Process } if *c == color))
|
||||
.map(|w| w.name.clone());
|
||||
if let Some(n) = existing {
|
||||
return n;
|
||||
}
|
||||
let name = Styles::unique_name(|n| d.swatch(n).is_some(), &value_name(color));
|
||||
d.swatches.push(Swatch {
|
||||
name: name.clone(),
|
||||
value: SwatchValue::Color { color, color_type: designcraft_color::ColorType::Process },
|
||||
locked: false,
|
||||
named: false,
|
||||
hidden: true,
|
||||
});
|
||||
name
|
||||
}
|
||||
|
||||
fn color_items(d: &mut designcraft_doc::Document, ids: &[designcraft_doc::ItemId], name: &str, stroke: bool) {
|
||||
for id in ids {
|
||||
if let Some(it) = d.item_mut(*id) {
|
||||
if stroke {
|
||||
it.stroke.swatch = name.to_string();
|
||||
it.stroke.tint = 1.0;
|
||||
} else {
|
||||
it.fill.swatch = name.to_string();
|
||||
it.fill.tint = 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn has_text_or_selection(s: &Session) -> std::result::Result<(), String> {
|
||||
has_doc(s)?;
|
||||
if s.active().is_some_and(|d| d.selection.text.is_some() || d.selection.cells.is_some()) { Ok(()) } else { super::has_selection(s) }
|
||||
}
|
||||
|
||||
/// The Eyedropper on a placed image (InDesign samples the pixel under the pointer): that colour,
|
||||
/// as an unnamed colour, fills the selected text (characters), or the selected objects.
|
||||
fn sample_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const ID: &str = "color.sample";
|
||||
let at = super::point_param(p, "at").ok_or_else(|| bad(ID, "missing at"))?;
|
||||
let sr = super::spread_param(p, "spread")?;
|
||||
let st = s.doc()?;
|
||||
let (from, color) = image_color_at(&st.doc, sr, at).ok_or_else(|| bad(ID, "no placed image under the point"))?;
|
||||
let hex = color.to_hex();
|
||||
let mut params = p.clone();
|
||||
params["color"] = json!(hex);
|
||||
let mut result = apply_color(s, ¶ms)?;
|
||||
result["color"] = json!(hex);
|
||||
result["from"] = json!(from.0);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// The colour of the placed raster image at `at` on spread `sr`: the frontmost visible image whose
|
||||
/// frame shows that point (locked ones too, as sampling changes nothing). (image frame, colour);
|
||||
/// None over no image, a transparent pixel, or a graphic that is not a raster (PDF, EPS).
|
||||
pub(crate) fn image_color_at(
|
||||
d: &designcraft_doc::Document,
|
||||
sr: designcraft_doc::SpreadRef,
|
||||
at: designcraft_geom::Point,
|
||||
) -> Option<(designcraft_doc::ItemId, designcraft_color::Color)> {
|
||||
let sp = d.spread(sr)?;
|
||||
let mut items: Vec<&designcraft_doc::Item> = sp.items.iter().map(|i| &**i).collect();
|
||||
items.sort_by_key(|i| d.layer_rank(i.layer));
|
||||
items
|
||||
.into_iter()
|
||||
.rev()
|
||||
.filter(|it| d.layer(it.layer).is_none_or(|l| l.visible))
|
||||
.find_map(|it| sample_item(d, it, designcraft_geom::Affine::IDENTITY, at, 0))
|
||||
}
|
||||
|
||||
fn sample_item(
|
||||
d: &designcraft_doc::Document,
|
||||
it: &designcraft_doc::Item,
|
||||
outer: designcraft_geom::Affine,
|
||||
at: designcraft_geom::Point,
|
||||
depth: usize,
|
||||
) -> Option<(designcraft_doc::ItemId, designcraft_color::Color)> {
|
||||
use designcraft_doc::Content;
|
||||
if it.hidden || depth > 64 {
|
||||
return None;
|
||||
}
|
||||
let xf = outer * it.xf;
|
||||
match &it.content {
|
||||
Content::Group { items } => items.iter().rev().find_map(|c| sample_item(d, c, xf, at, depth + 1)),
|
||||
Content::Graphic(g) => {
|
||||
let inner = xf.inverse() * at;
|
||||
if !designcraft_geom::hit::fill_contains(&it.path.to_bezpath(), designcraft_geom::FillRule::NonZero, inner) {
|
||||
return None;
|
||||
}
|
||||
let gp = g.xf.inverse() * inner;
|
||||
let (w, h) = g.size;
|
||||
// Written so that NaN (a degenerate transform) is outside too.
|
||||
if !(w > 0.0 && h > 0.0 && gp.x >= 0.0 && gp.y >= 0.0 && gp.x < w && gp.y < h) {
|
||||
return None;
|
||||
}
|
||||
let asset = d.assets.get(&g.asset).filter(|a| a.is_raster())?;
|
||||
let img = designcraft_images::decode_rgba(&asset.data)?;
|
||||
let (pw, ph) = img.dimensions();
|
||||
let px = ((gp.x / w * f64::from(pw)) as u32).min(pw.checked_sub(1)?);
|
||||
let py = ((gp.y / h * f64::from(ph)) as u32).min(ph.checked_sub(1)?);
|
||||
let [r, gr, b, a] = img.get_pixel_checked(px, py)?.0;
|
||||
(a > 0).then(|| (it.id, designcraft_color::Color::rgb8(r, gr, b)))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn add_to_swatches(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
@@ -990,7 +1135,7 @@ fn add_to_swatches(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
};
|
||||
let rename = str_param(p, "name").map(str::to_string);
|
||||
s.edit(|d, _| {
|
||||
let result = s.edit(|d, _| {
|
||||
let w = d.swatches.iter_mut().find(|w| w.name == name).ok_or_else(|| bad("swatch.addToSwatches", format!("no swatch `{name}`")))?;
|
||||
if w.locked {
|
||||
return Err(bad("swatch.addToSwatches", "special swatches are already listed"));
|
||||
@@ -1017,8 +1162,84 @@ fn add_to_swatches(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
rename_in(std::sync::Arc::make_mut(top), &name, &new);
|
||||
}
|
||||
}
|
||||
for st in d.stories.values_mut() {
|
||||
let mut copy = (**st).clone();
|
||||
if rename_in_story(&mut copy, &name, &new, 0)? {
|
||||
*st = std::sync::Arc::new(copy);
|
||||
}
|
||||
}
|
||||
Ok(json!({"name": new}))
|
||||
})
|
||||
})?;
|
||||
if let Some(new) = result.get("name").and_then(Value::as_str) {
|
||||
super::text::rename_typing_swatch(s, &name, new);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Point text colours (character fill, stroke, underline and strikethrough; paragraph shading,
|
||||
/// border and rules; table cells, borders and alternating fills) at a renamed swatch, in a story
|
||||
/// and the texts inside it (table cells, footnotes, endnotes). Returns whether anything changed.
|
||||
fn rename_in_story(st: &mut designcraft_doc::Story, from: &str, to: &str, depth: usize) -> Result<bool> {
|
||||
// Refuse the whole edit instead of leaving deeper references pointing at a removed name.
|
||||
if depth > 32 {
|
||||
return Err(bad("swatch.addToSwatches", "story nesting exceeds 32 levels"));
|
||||
}
|
||||
fn fix(s: &mut String, from: &str, to: &str) -> bool {
|
||||
let hit = s == from;
|
||||
if hit {
|
||||
*s = to.to_string();
|
||||
}
|
||||
hit
|
||||
}
|
||||
let mut n = 0;
|
||||
let mut f = |s: &mut String| n += usize::from(fix(s, from, to));
|
||||
for r in &mut st.chars {
|
||||
let o = &mut r.format.over;
|
||||
for c in [&mut o.fill, &mut o.stroke, &mut o.underline_color, &mut o.strikethrough_color].into_iter().flatten() {
|
||||
f(c);
|
||||
}
|
||||
}
|
||||
for pf in &mut st.paras {
|
||||
let a = &mut pf.para;
|
||||
for c in [&mut a.shading_color, &mut a.border_color].into_iter().flatten() {
|
||||
f(c);
|
||||
}
|
||||
for r in [&mut a.rule_above, &mut a.rule_below].into_iter().flatten() {
|
||||
f(&mut r.color);
|
||||
}
|
||||
}
|
||||
let mut nested = false;
|
||||
for t in st.tables.values_mut() {
|
||||
let t = std::sync::Arc::make_mut(t);
|
||||
let o = &mut t.options;
|
||||
f(&mut o.border.color);
|
||||
for b in o.borders.iter_mut().flatten() {
|
||||
f(&mut b.color);
|
||||
}
|
||||
for a in [&mut o.alt_rows, &mut o.alt_cols].into_iter().flatten() {
|
||||
f(&mut a.first_color);
|
||||
f(&mut a.next_color);
|
||||
}
|
||||
for k in [&mut o.row_strokes, &mut o.column_strokes].into_iter().flatten() {
|
||||
f(&mut k.first_stroke.color);
|
||||
f(&mut k.next_stroke.color);
|
||||
}
|
||||
for c in &mut t.cells {
|
||||
f(&mut c.fill);
|
||||
for k in &mut c.strokes {
|
||||
f(&mut k.color);
|
||||
}
|
||||
nested |= rename_in_story(&mut c.text, from, to, depth + 1)?;
|
||||
}
|
||||
}
|
||||
for note in st.notes.iter_mut().chain(st.endnotes.iter_mut()) {
|
||||
nested |= rename_in_story(&mut std::sync::Arc::make_mut(note).text, from, to, depth + 1)?;
|
||||
}
|
||||
let changed = n > 0 || nested;
|
||||
if changed {
|
||||
st.rev = st.rev.wrapping_add(1);
|
||||
}
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// Point fills and strokes of an item (and its children) at a renamed swatch.
|
||||
@@ -1040,6 +1261,168 @@ fn rename_in(it: &mut designcraft_doc::Item, from: &str, to: &str) {
|
||||
mod color_tests {
|
||||
use super::*;
|
||||
|
||||
fn fill_session() -> Session {
|
||||
let mut session = Session::new();
|
||||
session.execute("file.new", &json!({})).unwrap();
|
||||
let frame = session.execute("frame.create", &json!({"rect": [0, 0, 200, 100], "content": "text", "text": "Hi"})).unwrap();
|
||||
session.execute("text.select", &json!({"story": frame["story"], "anchor": 0, "focus": 2})).unwrap();
|
||||
session.execute("style.paragraph.create", &json!({"name": "Existing"})).unwrap();
|
||||
session.execute("style.character.create", &json!({"name": "Existing"})).unwrap();
|
||||
session.execute("type.char", &json!({"size": 18})).unwrap();
|
||||
session.execute("edit.undo", &json!({})).unwrap();
|
||||
session
|
||||
}
|
||||
|
||||
fn assert_fill_rejected(session: &mut Session, command: &str, params: Value) {
|
||||
let before = session.doc().unwrap().clone();
|
||||
let journal = session.journal.clone();
|
||||
let fill = params
|
||||
.pointer("/chars/fill")
|
||||
.or_else(|| params.pointer("/attrs/fill"))
|
||||
.or_else(|| params.get("fill"))
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("Missing Ink");
|
||||
|
||||
let result = session.execute(command, ¶ms);
|
||||
|
||||
match result {
|
||||
Err(crate::EngineError::BadParams { cmd, msg }) => {
|
||||
assert_eq!(cmd, command);
|
||||
assert!(msg.contains(fill), "{msg}");
|
||||
assert!(msg.contains("swatch.create") && msg.contains("returned name"), "{msg}");
|
||||
}
|
||||
other => panic!("expected BadParams for {command}, got {other:?}"),
|
||||
}
|
||||
let after = session.doc().unwrap();
|
||||
assert!(std::sync::Arc::ptr_eq(&before.doc, &after.doc));
|
||||
assert_eq!(before.selection, after.selection);
|
||||
assert_eq!(before.revision, after.revision);
|
||||
assert_eq!(format!("{:?}", before.history), format!("{:?}", after.history));
|
||||
assert_eq!(journal, session.journal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_rejects_unknown_flat_and_nested_names_when_formatting() {
|
||||
for fill in ["Missing Ink", "#ff0000", "[black]", " [Black]"] {
|
||||
for params in [json!({"fill": fill, "size": 24}), json!({"attrs": {"fill": fill, "size": 24}})] {
|
||||
let mut session = fill_session();
|
||||
assert_fill_rejected(&mut session, "type.char", params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_rejects_unknown_names_when_creating_or_editing_styles() {
|
||||
for command in ["style.paragraph.create", "style.paragraph.edit", "style.character.create", "style.character.edit"] {
|
||||
for fill in ["Missing Ink", "#ff0000"] {
|
||||
let mut session = fill_session();
|
||||
session.execute("style.paragraph.apply", &json!({"name": "Existing"})).unwrap();
|
||||
session.execute("style.character.apply", &json!({"name": "Existing"})).unwrap();
|
||||
assert_fill_rejected(&mut session, command, json!({"name": "Existing", "rename": "Renamed", "chars": {"fill": fill, "size": 24}}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_rejects_inherited_unknown_name_when_creating_from_selection() {
|
||||
let mut session = fill_session();
|
||||
session
|
||||
.edit(|document, selection| {
|
||||
let selected = selection.text.unwrap();
|
||||
document.story_mut(selected.story).unwrap().format_chars(selected.range(), |format| format.over.fill = Some("Missing Ink".into()));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_fill_rejected(&mut session, "style.paragraph.create", json!({"name": "Inherited", "fromSelection": true}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_accepts_valid_override_when_selection_has_unknown_name() {
|
||||
let mut session = fill_session();
|
||||
session
|
||||
.edit(|document, selection| {
|
||||
let selected = selection.text.unwrap();
|
||||
document.story_mut(selected.story).unwrap().format_chars(selected.range(), |format| format.over.fill = Some("Missing Ink".into()));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
session.execute("style.paragraph.create", &json!({"name": "Override", "fromSelection": true, "chars": {"fill": "[Paper]"}})).unwrap();
|
||||
|
||||
assert_eq!(session.doc().unwrap().doc.styles.para("Override").unwrap().chars.fill.as_deref(), Some("[Paper]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_rejects_unknown_name_when_changing_text_even_without_output() {
|
||||
for (find, change) in [("Hi", "Bye"), ("Hi", ""), ("Absent", "Bye")] {
|
||||
for fill in ["Missing Ink", "#ff0000"] {
|
||||
let mut session = fill_session();
|
||||
assert_fill_rejected(&mut session, "find.change", json!({"find": find, "change": change, "attrs": {"fill": fill}}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_preserves_exact_registered_names_when_used_at_all_boundaries() {
|
||||
for fill in ["Brand Ink", "#ff0000", "[None]", "[Black]"] {
|
||||
let mut session = fill_session();
|
||||
if !fill.starts_with('[') {
|
||||
let created = session.execute("swatch.create", &json!({"name": fill, "color": "#108080"})).unwrap();
|
||||
assert_eq!(created["name"], fill);
|
||||
}
|
||||
for params in [json!({"fill": fill}), json!({"attrs": {"fill": fill}})] {
|
||||
session.execute("type.char", ¶ms).unwrap();
|
||||
assert_eq!(session.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], fill);
|
||||
}
|
||||
for kind in ["paragraph", "character"] {
|
||||
session.execute(&format!("style.{kind}.create"), &json!({"name": "Colored", "chars": {"fill": fill}})).unwrap();
|
||||
session.execute(&format!("style.{kind}.edit"), &json!({"name": "Existing", "chars": {"fill": fill}})).unwrap();
|
||||
let document = &session.doc().unwrap().doc;
|
||||
for name in ["Colored", "Existing"] {
|
||||
let attrs = if kind == "paragraph" {
|
||||
&document.styles.para(name).unwrap().chars
|
||||
} else {
|
||||
&document.styles.char_style(name).unwrap().chars
|
||||
};
|
||||
assert_eq!(attrs.fill.as_deref(), Some(fill));
|
||||
}
|
||||
}
|
||||
assert_eq!(session.execute("find.change", &json!({"find": "Hi", "change": "Bye", "attrs": {"fill": fill}})).unwrap()["count"], 1);
|
||||
let selected = session.doc().unwrap().selection.text.unwrap();
|
||||
let story = session.doc().unwrap().doc.story(selected.story).unwrap();
|
||||
assert_eq!(story.text, "Bye");
|
||||
assert_eq!(story.format_after(0).over.fill.as_deref(), Some(fill));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_fill_accepts_omitted_and_null_attributes_when_used_at_all_boundaries() {
|
||||
for chars in [json!({"size": 18}), json!({"fill": null})] {
|
||||
let mut session = fill_session();
|
||||
session.execute("type.char", &json!({"attrs": chars})).unwrap();
|
||||
for kind in ["paragraph", "character"] {
|
||||
session.execute(&format!("style.{kind}.create"), &json!({"name": "Nullable", "chars": chars})).unwrap();
|
||||
session.execute(&format!("style.{kind}.edit"), &json!({"name": "Existing", "chars": chars})).unwrap();
|
||||
}
|
||||
assert_eq!(session.execute("find.change", &json!({"find": "Hi", "change": "Bye", "attrs": chars})).unwrap()["count"], 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn percent_components_at_most_one_stay_percent() {
|
||||
use designcraft_color::Color;
|
||||
// {"c": 0.7, ..., "y": 12.2} is a pale cream (C 0.7 %, Y 12.2 %), not C 70 %.
|
||||
let Some(Color::Cmyk { c, m, y, k }) = parse_color(&json!({"c": 0.7, "m": 0, "y": 12.2, "k": 0})) else { panic!("not CMYK") };
|
||||
assert!((c - 0.007).abs() < 1e-6 && m == 0.0 && (y - 0.122).abs() < 1e-6 && k == 0.0, "got {c} {m} {y} {k}");
|
||||
// Fractions (every component within 0..1) keep working.
|
||||
let Some(Color::Cmyk { c, k, .. }) = parse_color(&json!({"c": 0.5, "m": 0, "y": 0, "k": 1})) else { panic!("not CMYK") };
|
||||
assert!((c - 0.5).abs() < 1e-6 && (k - 1.0).abs() < 1e-6);
|
||||
// RGB arrays follow the same rule: [1, 128, 255] is 0..255.
|
||||
let Some(Color::Rgb { r, g, b }) = parse_color(&json!([1, 128, 255])) else { panic!("not RGB") };
|
||||
assert!((r - 1.0 / 255.0).abs() < 1e-6 && (g - 128.0 / 255.0).abs() < 1e-6 && (b - 1.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn style_groups_rename_every_use() {
|
||||
let mut s = Session::new();
|
||||
@@ -1170,6 +1553,40 @@ mod color_tests {
|
||||
s.execute("object.color", &json!({"color": {"c": 10, "m": 20, "y": 30, "k": 0}})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().fill.swatch, "Sand");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_to_swatches_renames_the_colour_in_text_cells_and_notes() {
|
||||
use std::sync::Arc;
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 400, 300], "content": "text", "text": "Hello world"})).unwrap();
|
||||
let sid = designcraft_doc::StoryId(r["story"].as_u64().unwrap());
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 11})).unwrap();
|
||||
s.execute("footnote.insert", &json!({"text": "note"})).unwrap();
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 0})).unwrap();
|
||||
s.execute("table.insert", &json!({"rows": 1, "cols": 1})).unwrap();
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 1, "focus": 4})).unwrap();
|
||||
let old = s.execute("object.color", &json!({"color": {"c": 10, "m": 20, "y": 30, "k": 0}})).unwrap()["swatch"].as_str().unwrap().to_string();
|
||||
// The same unnamed colour on a cell, the cell's text and the footnote's text.
|
||||
s.edit(|d, _| {
|
||||
let st = d.story_mut(sid).unwrap();
|
||||
let t = Arc::make_mut(st.tables.values_mut().next().unwrap());
|
||||
t.cells[0].fill = old.clone();
|
||||
t.cells[0].text.chars[0].format.over.fill = Some(old.clone());
|
||||
Arc::make_mut(&mut st.notes[0]).text.chars[0].format.over.stroke = Some(old.clone());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("swatch.addToSwatches", &json!({"swatch": old, "name": "Sand"})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let st = d.story(sid).unwrap();
|
||||
assert_eq!(st.char_format_at(2).over.fill.as_deref(), Some("Sand"));
|
||||
let cell = &st.tables.values().next().unwrap().cells[0];
|
||||
assert_eq!(cell.fill, "Sand");
|
||||
assert_eq!(cell.text.chars[0].format.over.fill.as_deref(), Some("Sand"));
|
||||
assert_eq!(st.notes[0].text.chars[0].format.over.stroke.as_deref(), Some("Sand"));
|
||||
d.check().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1318,3 +1735,116 @@ mod cjk_tests {
|
||||
assert!(s.execute("style.compositeFont.set", &definition(-1.0)).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sample_color_tests {
|
||||
use designcraft_color::{Color, SwatchValue};
|
||||
use designcraft_doc::ItemId;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// A 2×1 image, red then blue, placed 144 pt wide at (72, 72): each pixel is 72 pt.
|
||||
fn session_with_image() -> (Session, u64) {
|
||||
let png = designcraft_render::Rendered { width: 2, height: 1, pixels: vec![255, 0, 0, 255, 0, 0, 255, 255] }.to_png();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let b64 = super::super::file::base64_encode(&png);
|
||||
let r = s.execute("file.place", &json!({"base64": b64, "name": "rb.png", "x": 72, "y": 72, "width": 144})).unwrap();
|
||||
(s, r["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn swatch_color(s: &Session, name: &str) -> Color {
|
||||
match &s.doc().unwrap().doc.swatch(name).unwrap().value {
|
||||
SwatchValue::Color { color, .. } => *color,
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_eyedropper_colours_selected_text_with_the_image_under_it() {
|
||||
let (mut s, image) = session_with_image();
|
||||
let f = s.execute("frame.create", &json!({"rect": [300, 300, 500, 360], "content": "text", "text": "Elevate."})).unwrap();
|
||||
let fid = ItemId(f["id"].as_u64().unwrap());
|
||||
let story = s.doc().unwrap().doc.item(fid).unwrap().text_frame().unwrap().story;
|
||||
s.execute("text.select", &json!({"story": story.0, "anchor": 0, "focus": 7})).unwrap();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let r = s.execute("color.sample", &json!({"at": [180, 100]})).unwrap();
|
||||
assert_eq!((r["color"].as_str(), r["from"].as_u64()), (Some("#0000ff"), Some(image)), "{r}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "one undo step");
|
||||
let name = r["swatch"].as_str().unwrap().to_string();
|
||||
assert_eq!(swatch_color(&s, &name), Color::rgb8(0, 0, 255));
|
||||
assert!(s.doc().unwrap().doc.swatch(&name).unwrap().hidden, "an unnamed colour");
|
||||
let st = s.doc().unwrap().doc.story(story).unwrap().clone();
|
||||
assert_eq!(st.format_after(0).over.fill.as_deref(), Some(name.as_str()));
|
||||
assert_eq!(st.format_after(7).over.fill, None, "the full stop keeps its colour");
|
||||
// The frame itself is not coloured.
|
||||
assert!(s.doc().unwrap().doc.item(fid).unwrap().fill.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_eyedropper_fills_selected_objects_and_samples_locked_images() {
|
||||
let (mut s, _) = session_with_image();
|
||||
let lid = s.doc().unwrap().doc.layers[0].id.0;
|
||||
s.execute("layer.set", &json!({"id": lid, "locked": true})).unwrap();
|
||||
s.execute("layer.new", &json!({"name": "TEXT"})).unwrap();
|
||||
let rect = s.execute("frame.create", &json!({"rect": [300, 300, 400, 400]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("selection.set", &json!({"ids": [rect]})).unwrap();
|
||||
let r = s.execute("color.sample", &json!({"at": [100, 100], "target": "stroke"})).unwrap();
|
||||
assert_eq!(r["color"], "#ff0000", "{r}");
|
||||
let it = s.doc().unwrap().doc.item(ItemId(rect)).unwrap().clone();
|
||||
assert_eq!(swatch_color(&s, &it.stroke.swatch), Color::rgb8(255, 0, 0));
|
||||
// Sampling the same colour again reuses its swatch.
|
||||
let again = s.execute("color.sample", &json!({"at": [90, 90]})).unwrap();
|
||||
assert_eq!(again["swatch"], r["swatch"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nothing_to_sample_is_refused_without_an_edit() {
|
||||
let (mut s, _) = session_with_image();
|
||||
let f = s.execute("frame.create", &json!({"rect": [300, 300, 500, 360], "content": "text", "text": "Hi"})).unwrap();
|
||||
let fid = ItemId(f["id"].as_u64().unwrap());
|
||||
let story = s.doc().unwrap().doc.item(fid).unwrap().text_frame().unwrap().story;
|
||||
s.execute("text.select", &json!({"story": story.0, "anchor": 1, "focus": 1})).unwrap();
|
||||
let sampled = s.execute("color.sample", &json!({"at": [100, 100]})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], sampled["swatch"]);
|
||||
assert_eq!(s.doc().unwrap().doc.story(story).unwrap().text, "Hi", "caret color waits for typing");
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
s.execute("text.select", &json!({"story": story.0, "anchor": 0, "focus": 2})).unwrap();
|
||||
let none = s.execute("color.sample", &json!({"at": [600, 600]})).unwrap_err().to_string();
|
||||
assert!(none.contains("no placed image"), "{none}");
|
||||
// Just past the image's right edge.
|
||||
assert!(s.execute("color.sample", &json!({"at": [216.5, 100]})).is_err());
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampling_and_explicit_colour_use_the_same_text_application_path() {
|
||||
for command in ["color.sample", "object.color"] {
|
||||
let (mut s, _) = session_with_image();
|
||||
let f = s.execute("frame.create", &json!({"rect": [300, 300, 500, 360], "content": "text"})).unwrap();
|
||||
let story = designcraft_doc::StoryId(f["story"].as_u64().unwrap());
|
||||
s.execute("text.select", &json!({"story": story.0, "anchor": 0})).unwrap();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let r = s.execute(command, &json!({"at": [100, 100], "color": "#ff0000"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "{command}");
|
||||
s.execute("text.insert", &json!({"text": "Hi"})).unwrap();
|
||||
let text = s.doc().unwrap().doc.story(story).unwrap();
|
||||
assert_eq!(text.format_after(0).over.fill.as_deref(), r["swatch"].as_str(), "empty-story typing: {command}");
|
||||
// Explicit ids override text targeting, including with a text selection active.
|
||||
let fid = ItemId(f["id"].as_u64().unwrap());
|
||||
s.execute("text.select", &json!({"story": story.0, "anchor": 0, "focus": 2})).unwrap();
|
||||
let r = s.execute(command, &json!({"at": [180, 100], "color": "#0000ff", "id": fid.0})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.item(fid).unwrap().fill.swatch, r["swatch"].as_str().unwrap());
|
||||
let text = s.doc().unwrap().doc.story(story).unwrap();
|
||||
assert_ne!(text.format_after(0).over.fill.as_deref(), r["swatch"].as_str(), "text unchanged: {command}");
|
||||
let r = s.execute(command, &json!({"at": [180, 100], "color": "#0000ff", "target": "stroke"})).unwrap();
|
||||
let text = s.doc().unwrap().doc.story(story).unwrap();
|
||||
assert_eq!(text.format_after(0).over.stroke.as_deref(), r["swatch"].as_str(), "selected text stroke: {command}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "text_color_tests.rs"]
|
||||
mod text_color_tests;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! whole table).
|
||||
|
||||
use designcraft_compose as compose;
|
||||
use designcraft_doc::story::floor_char_boundary;
|
||||
use designcraft_doc::{
|
||||
AltFills, CellAddr, CellRange, CellStroke, Document, RowHeightMode, Selection, StoryId, StrokeType, Table, TableSel, TextSel,
|
||||
VerticalJustification,
|
||||
@@ -140,11 +141,9 @@ fn table_specs() -> Vec<CommandSpec> {
|
||||
|s, p| {
|
||||
let g = target(s, p, "table.textCell")?;
|
||||
edit_table(s, &g, "table.textCell", |t| {
|
||||
for r in g.range.r0..=g.range.r1 {
|
||||
for c in g.range.c0..=g.range.c1 {
|
||||
if let Some(cell) = t.cell_mut(r, c) {
|
||||
cell.graphic = None;
|
||||
}
|
||||
for (r, c) in t.selected_owners(g.range) {
|
||||
if let Some(cell) = t.cell_mut(r, c) {
|
||||
cell.graphic = None;
|
||||
}
|
||||
}
|
||||
ok()
|
||||
@@ -178,7 +177,7 @@ fn table_specs() -> Vec<CommandSpec> {
|
||||
"Split Cell Horizontally",
|
||||
["Table"],
|
||||
None,
|
||||
"{} — the target cell becomes two, one above the other",
|
||||
"{} — each target cell becomes two, one above the other",
|
||||
in_table,
|
||||
|s, p| split(s, p, true)
|
||||
),
|
||||
@@ -187,7 +186,7 @@ fn table_specs() -> Vec<CommandSpec> {
|
||||
"Split Cell Vertically",
|
||||
["Table"],
|
||||
None,
|
||||
"{} — the target cell becomes two, side by side",
|
||||
"{} — each target cell becomes two, side by side",
|
||||
in_table,
|
||||
|s, p| split(s, p, false)
|
||||
),
|
||||
@@ -351,13 +350,17 @@ fn clamp_selection(d: &Document, sel: &mut Selection) {
|
||||
match story.and_then(|x| x.tables.get(&c.table)) {
|
||||
Some(tb) => {
|
||||
let (r, col) = tb.owner(c.row.min(tb.nrows() - 1), c.col.min(tb.ncols() - 1));
|
||||
let len = tb.cell(r, col).map_or(0, |x| x.text.len());
|
||||
sel.text =
|
||||
Some(TextSel { anchor: t.anchor.min(len), focus: t.focus.min(len), cell: Some(CellAddr { table: c.table, row: r, col }), ..t });
|
||||
let text = tb.cell(r, col).map_or("", |x| x.text.text.as_str());
|
||||
sel.text = Some(TextSel {
|
||||
anchor: floor_char_boundary(text, t.anchor),
|
||||
focus: floor_char_boundary(text, t.focus),
|
||||
cell: Some(CellAddr { table: c.table, row: r, col }),
|
||||
..t
|
||||
});
|
||||
}
|
||||
None => {
|
||||
let len = story.map_or(0, |x| x.len());
|
||||
sel.text = Some(TextSel { anchor: t.anchor.min(len), focus: t.anchor.min(len), cell: None, ..t });
|
||||
let pos = story.map_or(0, |x| floor_char_boundary(&x.text, t.anchor));
|
||||
sel.text = Some(TextSel { anchor: pos, focus: pos, cell: None, ..t });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -589,7 +592,7 @@ fn merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn split(s: &mut Session, p: &Value, horizontal: bool) -> Result<Value> {
|
||||
let g = target(s, p, "table.split")?;
|
||||
edit_table(s, &g, "table.split", |t| {
|
||||
t.split_cell(g.range.r0, g.range.c0, horizontal);
|
||||
t.split_cells(g.range, horizontal).map_err(|e| bad("table.split", e))?;
|
||||
Ok(json!({"rows": t.nrows(), "cols": t.ncols()}))
|
||||
})
|
||||
}
|
||||
@@ -645,10 +648,9 @@ fn set_cell(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(o) => return Err(bad("table.setCell", format!("unknown vj `{o}`"))),
|
||||
None => None,
|
||||
};
|
||||
let text = str_param(p, "text").map(str::to_string);
|
||||
let text = str_param(p, "text").map(|_| super::text_param(p, "text"));
|
||||
let stroke = p.get("stroke").cloned();
|
||||
edit_table(s, &g, "table.setCell", |t| {
|
||||
let owners = t.owners();
|
||||
let nc = t.ncols();
|
||||
let nr = t.nrows();
|
||||
let rtl = t.options.direction == designcraft_doc::TextDirection::RightToLeft;
|
||||
@@ -656,50 +658,45 @@ fn set_cell(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Local edits outrank previously imported edges, including explicit zero/None.
|
||||
let priority = if stroke.is_some() { next_cell_stroke_priority(t) } else { 0 };
|
||||
let rg = g.range;
|
||||
for r in rg.r0..=rg.r1 {
|
||||
for c in rg.c0..=rg.c1 {
|
||||
if owners[r * nc + c] != (r, c) {
|
||||
continue;
|
||||
}
|
||||
let Some(cell) = t.cell_mut(r, c) else { continue };
|
||||
if let Some(f) = &fill {
|
||||
cell.fill = f.clone();
|
||||
}
|
||||
if let Some(ti) = tint {
|
||||
cell.fill_tint = ti;
|
||||
}
|
||||
if let Some(i) = insets {
|
||||
cell.insets = i;
|
||||
}
|
||||
if let Some(v) = vj {
|
||||
cell.vj = v;
|
||||
}
|
||||
if let Some(tx) = &text {
|
||||
let len = cell.text.len();
|
||||
cell.text.replace(0..len, tx);
|
||||
}
|
||||
if let Some(sv) = &stroke {
|
||||
let edges = str_param(sv, "edges").unwrap_or("all");
|
||||
let at_bottom = r.saturating_add(cell.row_span.max(1) as usize);
|
||||
let at_right = c.saturating_add(cell.col_span.max(1) as usize);
|
||||
let perimeter = [r == 0, if rtl { at_right == nc } else { c == 0 }, at_bottom == nr, if rtl { c == 0 } else { at_right == nc }];
|
||||
for (i, on) in [
|
||||
("top", r == rg.r0),
|
||||
("left", if rtl { at_right.saturating_sub(1) == rg.c1 } else { c == rg.c0 }),
|
||||
("bottom", at_bottom.saturating_sub(1) == rg.r1),
|
||||
("right", if rtl { c == rg.c0 } else { at_right.saturating_sub(1) == rg.c1 }),
|
||||
]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, (name, outer))| (i, edges == "all" || edges == *name || (edges == "outer" && *outer) || (edges == "inner" && !*outer)))
|
||||
{
|
||||
if on {
|
||||
let base = if perimeter[i] && !cell.border_overrides[i] { &borders[i] } else { &cell.strokes[i] };
|
||||
cell.strokes[i] = parse_stroke(sv, base.clone());
|
||||
cell.stroke_defined[i] = true;
|
||||
cell.stroke_priorities[i] = priority;
|
||||
cell.border_overrides[i] = true;
|
||||
}
|
||||
for (r, c) in t.selected_owners(rg) {
|
||||
let Some(cell) = t.cell_mut(r, c) else { continue };
|
||||
if let Some(f) = &fill {
|
||||
cell.fill = f.clone();
|
||||
}
|
||||
if let Some(ti) = tint {
|
||||
cell.fill_tint = ti;
|
||||
}
|
||||
if let Some(i) = insets {
|
||||
cell.insets = i;
|
||||
}
|
||||
if let Some(v) = vj {
|
||||
cell.vj = v;
|
||||
}
|
||||
if let Some(tx) = &text {
|
||||
let len = cell.text.len();
|
||||
cell.text.replace(0..len, tx);
|
||||
}
|
||||
if let Some(sv) = &stroke {
|
||||
let edges = str_param(sv, "edges").unwrap_or("all");
|
||||
let at_bottom = r.saturating_add(cell.row_span.max(1) as usize);
|
||||
let at_right = c.saturating_add(cell.col_span.max(1) as usize);
|
||||
let perimeter = [r == 0, if rtl { at_right == nc } else { c == 0 }, at_bottom == nr, if rtl { c == 0 } else { at_right == nc }];
|
||||
for (i, on) in [
|
||||
("top", r == rg.r0),
|
||||
("left", if rtl { at_right.saturating_sub(1) == rg.c1 } else { c == rg.c0 }),
|
||||
("bottom", at_bottom.saturating_sub(1) == rg.r1),
|
||||
("right", if rtl { c == rg.c0 } else { at_right.saturating_sub(1) == rg.c1 }),
|
||||
]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, (name, outer))| (i, edges == "all" || edges == *name || (edges == "outer" && *outer) || (edges == "inner" && !*outer)))
|
||||
{
|
||||
if on {
|
||||
let base = if perimeter[i] && !cell.border_overrides[i] { &borders[i] } else { &cell.strokes[i] };
|
||||
cell.strokes[i] = parse_stroke(sv, base.clone());
|
||||
cell.stroke_defined[i] = true;
|
||||
cell.stroke_priorities[i] = priority;
|
||||
cell.border_overrides[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -707,6 +704,7 @@ fn set_cell(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Keep both copies of unmerged shared boundaries consistent. Left/right name
|
||||
// physical sides, also in RTL tables.
|
||||
if let Some(sv) = &stroke {
|
||||
let owners = t.owners();
|
||||
let edges = str_param(sv, "edges").unwrap_or("all");
|
||||
let selected = |side: &str| edges == "all" || edges == "outer" || edges == side;
|
||||
if selected("top")
|
||||
@@ -834,7 +832,7 @@ fn distribute_rows(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
for key in ["row", "story", "table"] {
|
||||
if let Some(v) = p.get(key)
|
||||
&& !v.as_u64().is_some_and(|n| usize::try_from(n).is_ok())
|
||||
&& v.as_u64().is_none_or(|n| usize::try_from(n).is_err())
|
||||
{
|
||||
return Err(bad(ID, format!("{key} must be a non-negative integer")));
|
||||
}
|
||||
@@ -931,8 +929,9 @@ fn options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(v) = p.get("altCols") {
|
||||
o.alt_cols = parse_alt(v);
|
||||
}
|
||||
let h = p.get("headerRows").and_then(Value::as_u64).map(|v| v as usize);
|
||||
let f = p.get("footerRows").and_then(Value::as_u64).map(|v| v as usize);
|
||||
// At most 100 of each, as in `table.insert`: the rows the table lacks are added below.
|
||||
let h = p.get("headerRows").and_then(Value::as_u64).map(|v| v.min(100) as usize);
|
||||
let f = p.get("footerRows").and_then(Value::as_u64).map(|v| v.min(100) as usize);
|
||||
if h.is_some() || f.is_some() {
|
||||
let (h, f) = (h.unwrap_or(t.header_rows()), f.unwrap_or(t.footer_rows()));
|
||||
// Add rows when there aren't enough for the requested header/footer plus one body row.
|
||||
@@ -1095,7 +1094,10 @@ fn cell_style_create(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cell = d
|
||||
.story(g.story)
|
||||
.and_then(|st| st.tables.get(&g.table))
|
||||
.and_then(|t| t.cell(g.range.r0, g.range.c0))
|
||||
.and_then(|t| {
|
||||
let (r, c) = t.owner(g.range.r0, g.range.c0);
|
||||
t.cell(r, c)
|
||||
})
|
||||
.ok_or_else(|| bad("style.cell.create", "no cell"))?;
|
||||
cs.fill = Some(cell.fill.clone());
|
||||
cs.fill_tint = Some(cell.fill_tint);
|
||||
@@ -1122,17 +1124,11 @@ fn cell_style_apply(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cs = designcraft_doc::resolve_cell_style(styles, &name);
|
||||
let g = target(s, p, "style.cell.apply")?;
|
||||
edit_table(s, &g, "style.cell.apply", |t| {
|
||||
let owners = t.owners();
|
||||
let nc = t.ncols();
|
||||
let mut n = 0;
|
||||
for r in g.range.r0..=g.range.r1 {
|
||||
for c in g.range.c0..=g.range.c1 {
|
||||
if owners[r * nc + c] == (r, c)
|
||||
&& let Some(cell) = t.cell_mut(r, c)
|
||||
{
|
||||
cs.apply_to(cell);
|
||||
n += 1;
|
||||
}
|
||||
for (r, c) in t.selected_owners(g.range) {
|
||||
if let Some(cell) = t.cell_mut(r, c) {
|
||||
cs.apply_to(cell);
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
Ok(json!({"cells": n}))
|
||||
@@ -1353,7 +1349,7 @@ fn drop_cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
&& ts.story == sid
|
||||
&& ts.table == tid
|
||||
&& tid2 == tid
|
||||
&& ts.range.contains(fr, fc)
|
||||
&& s.doc()?.doc.story(sid).and_then(|st| st.tables.get(&tid)).is_some_and(|t| t.selected_owners(ts.range).binary_search(&(fr, fc)).is_ok())
|
||||
{
|
||||
let (nr, nc) = {
|
||||
let t = s.doc()?.doc.story(sid).and_then(|st| st.tables.get(&tid)).ok_or_else(|| bad(ID, "no table"))?;
|
||||
@@ -1402,20 +1398,10 @@ fn place_graphic(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (pw, ph) = designcraft_render::image_size(&bytes).ok_or_else(|| bad(ID, "unsupported or corrupt image"))?;
|
||||
let fill = p.get("fit").and_then(Value::as_str) == Some("fill");
|
||||
s.edit(|d, sel| {
|
||||
let aid = designcraft_doc::AssetId(d.alloc());
|
||||
let fresh = designcraft_doc::AssetId(d.alloc());
|
||||
let mime = designcraft_render::image_mime(&bytes).to_string();
|
||||
d.assets.insert(
|
||||
aid,
|
||||
std::sync::Arc::new(designcraft_doc::Asset {
|
||||
page: 0,
|
||||
id: aid,
|
||||
name,
|
||||
mime,
|
||||
link,
|
||||
data: std::sync::Arc::new(bytes),
|
||||
pixels: Some((pw, ph)),
|
||||
}),
|
||||
);
|
||||
let aid =
|
||||
d.add_asset(designcraft_doc::Asset { page: 0, id: fresh, name, mime, link, data: std::sync::Arc::new(bytes), pixels: Some((pw, ph)) });
|
||||
let st = d.story_mut(g.story).ok_or_else(|| bad(ID, "no story"))?;
|
||||
let t = st.table_mut(g.table).ok_or_else(|| bad(ID, "no table"))?;
|
||||
let (r, c) = t.owner(g.range.r0, g.range.c0);
|
||||
@@ -1690,6 +1676,19 @@ mod sort_tests {
|
||||
s.execute("table.moveColumn", &json!({"from": 1, "to": 0})).unwrap();
|
||||
assert_eq!((cell(&s, 2, 0), cell(&s, 2, 1)), ("x".to_string(), "a".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_options_limits_header_and_footer_rows() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 700], "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();
|
||||
// The limit `table.insert` has: the table used to grow by as many rows as were asked for.
|
||||
s.execute("table.options", &json!({"headerRows": 5000, "footerRows": 5000})).unwrap();
|
||||
let t = table(&s);
|
||||
assert_eq!((t.header_rows(), t.footer_rows(), t.nrows()), (100, 100, 201));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+736
-60
@@ -22,14 +22,26 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
release
|
||||
),
|
||||
cmd!(noundo "text.extendTo", "Extend Text Selection", [], None, "{frame, point}", has_doc, |s, p| place(s, p, true)),
|
||||
cmd!(noundo "text.selectWord", "Select Word", [], None, "{frame, point}", has_doc, |s, p| {
|
||||
place(s, p, false)?;
|
||||
let st = s.doc_mut()?;
|
||||
let Some(t) = st.selection.text else { return ok() };
|
||||
let text = &st.doc.text_story(t.story, t.cell).map(|x| x.text.clone()).unwrap_or_default();
|
||||
let (a, b) = word_bounds(text, t.focus);
|
||||
st.selection.text = Some(TextSel { anchor: a, focus: b, ..t });
|
||||
ok()
|
||||
cmd!(noundo "text.selectWord", "Select Word", [], None, "{frame, point} — the word (or run of spaces) under the point", has_doc, |s, p| {
|
||||
select_at(s, p, "word")
|
||||
}),
|
||||
cmd!(
|
||||
noundo "text.selectAt",
|
||||
"Select Text Unit",
|
||||
[],
|
||||
None,
|
||||
"{frame, point, unit: word|line|paragraph|story} — what a double, triple, quadruple or quintuple click selects: the word (or run of spaces) under the point, its line, its paragraph with the paragraph return, or the whole story",
|
||||
has_doc,
|
||||
|s, p| select_at(s, p, str_param(p, "unit").unwrap_or("word"))
|
||||
),
|
||||
cmd!(noundo "text.selectSentence", "Select Sentence", [], None, "{frame, point} — the sentence under the point", has_doc, |s, p| {
|
||||
select_around(s, p, "text.selectSentence", sentence_bounds)
|
||||
}),
|
||||
cmd!(noundo "text.selectParagraph", "Select Paragraph", [], None, "{frame, point} — the paragraph under the point, without its paragraph return", has_doc, |s, p| {
|
||||
select_around(s, p, "text.selectParagraph", para_bounds)
|
||||
}),
|
||||
cmd!(noundo "text.selectStory", "Select Story", [], None, "{frame, point} — all the text of the story under the point, threaded frames included", has_doc, |s, p| {
|
||||
select_around(s, p, "text.selectStory", |t, _| (0, t.len()))
|
||||
}),
|
||||
cmd!(noundo "text.select", "Select Text", [], None, "{story, anchor, focus} (UTF-8 byte offsets into the story text, as find.find reports them: á or — counts 2 or 3)", has_doc, |s, p| {
|
||||
let sid = StoryId(p.get("story").and_then(Value::as_u64).unwrap_or(0));
|
||||
@@ -39,6 +51,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
let f = floor_char_boundary(text, p.get("focus").and_then(Value::as_u64).map(|v| v as usize).unwrap_or(a));
|
||||
st.selection = Selection::text(TextSel { story: sid, anchor: a, focus: f, frame: None, cell: None });
|
||||
st.revision += 1;
|
||||
s.typing_format = None;
|
||||
ok()
|
||||
}),
|
||||
cmd!("text.insert", "Type", [], None, "{text, raw?: bool (no typographer's quotes)} — replaces the selected text", has_text, insert),
|
||||
@@ -76,7 +89,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!("story.setText", "Set Story Text", [], None, "{story, text} — replace a story's whole text", has_doc, |s, p| {
|
||||
let sid = StoryId(p.get("story").and_then(Value::as_u64).ok_or_else(|| bad("story.setText", "missing story"))?);
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
s.edit(|d, sel| {
|
||||
d.set_story_text(sid, &text)?;
|
||||
if let Some(t) = sel.text.as_mut().filter(|t| t.story == sid) {
|
||||
@@ -97,7 +110,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
let sid = StoryId(p.get("story").and_then(Value::as_u64).ok_or_else(|| bad("story.replaceRange", "missing story"))?);
|
||||
let a = p.get("start").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
let b = p.get("end").and_then(Value::as_u64).map(|v| v as usize).unwrap_or(a);
|
||||
let text = str_param(p, "text").unwrap_or("").to_string();
|
||||
let text = super::text_param(p, "text");
|
||||
s.edit(|d, sel| {
|
||||
let st = d.story_mut(sid).ok_or(designcraft_doc::DocError::NoStory(sid))?;
|
||||
let (a, b) = (a.min(st.len()), b.min(st.len()).max(a.min(st.len())));
|
||||
@@ -135,7 +148,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Paragraph Formatting",
|
||||
[],
|
||||
None,
|
||||
"{attrs: {align?, leftIndent?, firstLineIndent?, spaceBefore?, spaceAfter?, dropCapLines?, hyphenate?, composer?, tabs?, ruleAbove?/ruleBelow?: {on?, weight?, color?, tint?, columnWidth?, offset?, leftIndent?, rightIndent?} (only the named rule fields change), …}}",
|
||||
"{attrs: {align?, leftIndent?, firstLineIndent?, spaceBefore?, spaceAfter?, dropCapLines?, dropCapChars?, dropCapStyle?, dropCapAlignLeft?, dropCapScaleDescenders?, hyphenate?, composer?, tabs?, ruleAbove?/ruleBelow?: {on?, weight?, color?, tint?, columnWidth?, offset?, leftIndent?, rightIndent?} (only the named rule fields change), …}}",
|
||||
has_text_or_frames,
|
||||
|s, p| format_paras(s, p.get("attrs").unwrap_or(p))
|
||||
),
|
||||
@@ -152,8 +165,33 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
s,
|
||||
&json!({"align": "leftJustified"})
|
||||
)),
|
||||
cmd!("type.bold", "Bold", [], Some("Cmd+Shift+B"), "{}", has_text_or_frames, |s, _| format_chars(s, &json!({"fontStyle": "Bold"}))),
|
||||
cmd!("type.italic", "Italic", [], Some("Cmd+Shift+I"), "{}", has_text_or_frames, |s, _| format_chars(s, &json!({"fontStyle": "Italic"}))),
|
||||
cmd!(
|
||||
"type.bold",
|
||||
"Bold",
|
||||
[],
|
||||
Some("Cmd+Shift+B"),
|
||||
"{on?: bool} — the family's bold style, keeping italic (style linking); toggles when `on` is left out",
|
||||
has_text_or_frames,
|
||||
|s, p| link_style(s, p, true)
|
||||
),
|
||||
cmd!(
|
||||
"type.italic",
|
||||
"Italic",
|
||||
[],
|
||||
Some("Cmd+Shift+I"),
|
||||
"{on?: bool} — the family's italic style, keeping the weight (style linking); toggles when `on` is left out",
|
||||
has_text_or_frames,
|
||||
|s, p| link_style(s, p, false)
|
||||
),
|
||||
cmd!(
|
||||
"type.swapFillStroke",
|
||||
"Swap Text Fill and Stroke",
|
||||
[],
|
||||
None,
|
||||
"{} — swap each selected character's resolved fill/stroke and tints; at a caret, swap the next typing's colors",
|
||||
has_text_or_frames,
|
||||
swap_fill_stroke
|
||||
),
|
||||
cmd!("type.underline", "Underline", [], Some("Cmd+Shift+U"), "{}", has_text_or_frames, |s, _| format_chars(s, &json!({"underline": true}))),
|
||||
cmd!("type.allCaps", "All Caps", [], Some("Cmd+Shift+K"), "{}", has_text_or_frames, |s, _| format_chars(
|
||||
s,
|
||||
@@ -253,7 +291,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
has_text_or_frames,
|
||||
break_link
|
||||
),
|
||||
cmd!(query "type.selectionAttrs", "Selection Attributes", [], None, "{} → resolved character/paragraph attributes at the text selection", has_doc, selection_attrs),
|
||||
cmd!(query "type.selectionAttrs", "Selection Attributes", [], None, "{} → resolved character/paragraph attributes at the text selection, and its start as `selectedText` (one line, up to 60 characters)", has_doc, selection_attrs),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -271,6 +309,17 @@ pub(crate) fn story_of(s: &Session, p: &Value) -> Option<StoryId> {
|
||||
|
||||
/// Story byte at spread point `pt` in text frame `frame` (in a table cell's story when the point is in a cell).
|
||||
fn hit_byte(s: &Session, frame: ItemId, pt: Point) -> Option<(StoryId, usize, Option<CellAddr>)> {
|
||||
hit_byte_with(s, frame, pt, compose::hit)
|
||||
}
|
||||
|
||||
/// [`hit_byte`] with the hit test: [`compose::hit`] (caret position) or [`compose::hit_char`]
|
||||
/// (the character under the point).
|
||||
fn hit_byte_with(
|
||||
s: &Session,
|
||||
frame: ItemId,
|
||||
pt: Point,
|
||||
hit: fn(&compose::ComposedStory, usize, Point) -> Option<usize>,
|
||||
) -> Option<(StoryId, usize, Option<CellAddr>)> {
|
||||
let st = s.active()?;
|
||||
let loc = st.doc.find(frame)?;
|
||||
let it = st.doc.item_at(&loc)?;
|
||||
@@ -279,16 +328,95 @@ fn hit_byte(s: &Session, frame: ItemId, pt: Point) -> Option<(StoryId, usize, Op
|
||||
let inner = xf.inverse() * pt;
|
||||
let cs = s.cache.get(&st.doc, sid, None);
|
||||
let fi = cs.frames.iter().position(|f| f.frame == frame)?;
|
||||
if let Some((table, row, col, b)) = compose::hit_cell(&cs, fi, inner) {
|
||||
// Map path-text hits along the curve before choosing the character unit.
|
||||
let inner = cs.frames.get(fi).and_then(|ft| compose::PathTextMap::new(it, ft)).map_or(inner, |m| m.to_line(inner));
|
||||
if let Some((table, row, col, b)) = compose::hit_cell_with(&cs, fi, inner, hit) {
|
||||
return Some((sid, b, Some(CellAddr { table, row, col })));
|
||||
}
|
||||
if let Some((id, b)) = compose::hit_note(&cs, fi, inner) {
|
||||
if let Some((id, b)) = compose::hit_note_with(&cs, fi, inner, hit) {
|
||||
return Some((sid, b, Some(CellAddr::footnote(id))));
|
||||
}
|
||||
let b = compose::hit(&cs, fi, inner).unwrap_or(0);
|
||||
let b = hit(&cs, fi, inner).unwrap_or(0);
|
||||
Some((sid, b, None))
|
||||
}
|
||||
|
||||
/// Select the word, line, paragraph or story at `point` in `frame` (multiple clicks).
|
||||
fn select_at(s: &mut Session, p: &Value, unit: &str) -> Result<Value> {
|
||||
const ID: &str = "text.selectAt";
|
||||
let frame = id_param(p, "frame").ok_or_else(|| bad(ID, "missing frame"))?;
|
||||
let pt = point_param(p, "point").ok_or_else(|| bad(ID, "missing point"))?;
|
||||
let (sid, b, cell) = hit_byte_with(s, frame, pt, compose::hit_char).ok_or_else(|| bad(ID, "not a text frame"))?;
|
||||
let st = s.doc()?;
|
||||
let story = st.doc.text_story(sid, cell).ok_or(designcraft_doc::DocError::NoStory(sid))?;
|
||||
let text = story.text.as_str();
|
||||
let b = floor_char_boundary(text, b);
|
||||
let (a, e) = match unit {
|
||||
"word" => word_or_spaces_at(text, b),
|
||||
"line" => {
|
||||
let cs = s.cache.get(&st.doc, sid, None);
|
||||
line_at(&text_composition(cs, cell), b).unwrap_or((b, b))
|
||||
}
|
||||
// The paragraph with its return (the last paragraph has none).
|
||||
"paragraph" => story.para_ranges().get(story.para_at(b)).map_or((b, b), |r| (r.start, (r.end + 1).min(text.len()))),
|
||||
"story" => (0, text.len()),
|
||||
other => return Err(bad(ID, format!("unknown unit `{other}` (word, line, paragraph or story)"))),
|
||||
};
|
||||
s.text_drag = false;
|
||||
s.typing_format = None;
|
||||
let st = s.doc_mut()?;
|
||||
st.selection = Selection::text(TextSel { story: sid, anchor: a, focus: e, frame: Some(frame), cell });
|
||||
// Explicit reselection invalidates the Find scope even when the range is unchanged.
|
||||
st.revision = st.revision.wrapping_add(1);
|
||||
Ok(json!({"story": sid.0, "anchor": a, "focus": e}))
|
||||
}
|
||||
|
||||
/// The word at byte `i`; on spaces the run of spaces; on anything else that one character
|
||||
/// (grapheme cluster). At the end of a paragraph, what ends it. Grapheme clusters are never split:
|
||||
/// combining marks, viramas, variation selectors and emoji ZWJ sequences stay with their base.
|
||||
fn word_or_spaces_at(s: &str, i: usize) -> (usize, usize) {
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
let i = floor_char_boundary(s, i.min(s.len()));
|
||||
// Clusters never span a paragraph return, so the paragraph is enough.
|
||||
let ps = s[..i].rfind('\n').map_or(0, |k| k + 1);
|
||||
let pe = s[i..].find('\n').map_or(s.len(), |k| i + k);
|
||||
let g: Vec<(usize, &str)> = s[ps..pe].grapheme_indices(true).collect();
|
||||
let Some(k) = g.iter().rposition(|(o, _)| ps + o <= i) else { return (i, i) };
|
||||
let kind = |c: &str| match c.chars().next() {
|
||||
Some(c) if is_word(c) => 1,
|
||||
Some(c) if c.is_whitespace() => 2,
|
||||
_ => 0,
|
||||
};
|
||||
let span = |a: usize, e: usize| g.get(a).zip(g.get(e)).map_or((i, i), |((oa, _), (oe, ge))| (ps + oa, ps + oe + ge.len()));
|
||||
let kd = g.get(k).map_or(0, |(_, c)| kind(c));
|
||||
if kd == 0 {
|
||||
return span(k, k);
|
||||
}
|
||||
let mut a = k;
|
||||
while a > 0 && g.get(a - 1).is_some_and(|(_, c)| kind(c) == kd) {
|
||||
a -= 1;
|
||||
}
|
||||
let mut e = k;
|
||||
while g.get(e + 1).is_some_and(|(_, c)| kind(c) == kd) {
|
||||
e += 1;
|
||||
}
|
||||
span(a, e)
|
||||
}
|
||||
|
||||
/// The composition whose lines hold a story's text: the story's own, a table cell's or a footnote's.
|
||||
fn text_composition(cs: std::sync::Arc<compose::ComposedStory>, cell: Option<CellAddr>) -> std::sync::Arc<compose::ComposedStory> {
|
||||
match cell {
|
||||
Some(c) if c.footnote_id().is_some() => compose::find_note(&cs, c.row as u64).map(|(_, n)| n.text.clone()).unwrap_or(cs),
|
||||
Some(c) => compose::find_cell(&cs, c.table, c.row, c.col).map(|(_, _, pc)| pc.text.clone()).unwrap_or(cs),
|
||||
None => cs,
|
||||
}
|
||||
}
|
||||
|
||||
/// The composed line holding the character at byte `pos`.
|
||||
fn line_at(cs: &compose::ComposedStory, pos: usize) -> Option<(usize, usize)> {
|
||||
let lines = || cs.frames.iter().flat_map(|f| f.lines.iter());
|
||||
lines().find(|l| l.range.contains(&pos)).map(|l| (l.range.start, l.range.end)).or_else(|| line_of(cs, pos))
|
||||
}
|
||||
|
||||
fn place(s: &mut Session, p: &Value, extend: bool) -> Result<Value> {
|
||||
let frame = id_param(p, "frame").ok_or_else(|| bad("text.placeCaret", "missing frame"))?;
|
||||
let pt = point_param(p, "point").unwrap_or(Point::ZERO);
|
||||
@@ -307,6 +435,7 @@ fn place(s: &mut Session, p: &Value, extend: bool) -> Result<Value> {
|
||||
} else if s.text_drag {
|
||||
return Ok(json!({"story": sid.0, "dragging": true}));
|
||||
}
|
||||
s.typing_format = None;
|
||||
let st = s.doc_mut()?;
|
||||
// Dragging from one cell into another selects cells.
|
||||
if extend
|
||||
@@ -374,7 +503,8 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let text = if s.prefs.typographers_quotes && !raw { smart_quotes(s, &text) } else { text };
|
||||
let tracking = s.doc()?.doc.settings.track_changes;
|
||||
let autocorrect = (s.prefs.autocorrect && !tracking && !raw).then(|| s.prefs.autocorrect_list.clone());
|
||||
s.edit(|d, sel| {
|
||||
let typing = typing_format(s).cloned();
|
||||
let result = s.edit(|d, sel| {
|
||||
let t = sel.text.ok_or_else(|| bad("text.insert", "no insertion point"))?;
|
||||
let st = d.text_story_mut(t.story, t.cell).ok_or(designcraft_doc::DocError::NoStory(t.story))?;
|
||||
let r = t.range();
|
||||
@@ -434,6 +564,9 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Track Changes: the replaced text is marked deleted, the typing inserted after it.
|
||||
let at = super::changes::mark_deleted(st, r.clone());
|
||||
let mut fmt = st.char_format_at(at).clone();
|
||||
if let Some(f) = &typing {
|
||||
apply_typing_format(&mut fmt, f);
|
||||
}
|
||||
fmt.over.change = Some(designcraft_doc::ChangeMark::Inserted);
|
||||
st.insert_with(at, &text, fmt);
|
||||
let pos = at + text.len() - trail;
|
||||
@@ -441,10 +574,19 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Ok(json!({"pos": pos}));
|
||||
}
|
||||
st.replace(r.clone(), &text);
|
||||
if let Some(f) = &typing
|
||||
&& !text.is_empty()
|
||||
{
|
||||
st.format_chars(r.start..r.start + text.len(), |fmt| apply_typing_format(fmt, f));
|
||||
}
|
||||
let pos = r.start + text.len() - trail;
|
||||
sel.text = Some(TextSel { anchor: pos, focus: pos, ..t });
|
||||
Ok(json!({"pos": pos}))
|
||||
})
|
||||
});
|
||||
if result.is_ok() {
|
||||
s.typing_format = None;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn smart_quotes(s: &Session, text: &str) -> String {
|
||||
@@ -621,12 +763,62 @@ pub fn prev_word(s: &str, i: usize) -> usize {
|
||||
}
|
||||
0
|
||||
}
|
||||
fn word_bounds(s: &str, i: usize) -> (usize, usize) {
|
||||
let a = s[..i.min(s.len())].char_indices().rev().take_while(|(_, c)| is_word(*c)).last().map(|(k, _)| k).unwrap_or(i);
|
||||
let b = s[i.min(s.len())..].char_indices().find(|(_, c)| !is_word(*c)).map(|(k, _)| i + k).unwrap_or(s.len());
|
||||
/// Selects `bounds(text, byte)` around the byte under `point` in `frame`. Unlike a caret
|
||||
/// placement, a press inside the current selection is no start of a text drag here: it is the
|
||||
/// third or later click of a row, which lands in the word or sentence just selected.
|
||||
fn select_around(s: &mut Session, p: &Value, id: &str, bounds: fn(&str, usize) -> (usize, usize)) -> Result<Value> {
|
||||
let frame = id_param(p, "frame").ok_or_else(|| bad(id, "missing frame"))?;
|
||||
let pt = point_param(p, "point").unwrap_or(Point::ZERO);
|
||||
let (sid, b, cell) = hit_byte(s, frame, pt).ok_or_else(|| bad(id, "not a text frame"))?;
|
||||
let st = s.doc_mut()?;
|
||||
let text = st.doc.text_story(sid, cell).map(|x| x.text.clone()).unwrap_or_default();
|
||||
let (anchor, focus) = bounds(&text, floor_char_boundary(&text, b));
|
||||
st.selection = Selection::text(TextSel { story: sid, anchor, focus, frame: Some(frame), cell });
|
||||
st.revision += 1;
|
||||
Ok(json!({"story": sid.0, "anchor": anchor, "focus": focus}))
|
||||
}
|
||||
|
||||
/// The paragraph around byte `i`, without its `\n`.
|
||||
fn para_bounds(s: &str, i: usize) -> (usize, usize) {
|
||||
let i = i.min(s.len());
|
||||
let a = s.get(..i).and_then(|t| t.rfind('\n')).map_or(0, |k| k + 1);
|
||||
let b = s.get(i..).and_then(|t| t.find('\n')).map_or(s.len(), |k| i + k);
|
||||
(a, b)
|
||||
}
|
||||
|
||||
/// The sentence around byte `i`, within its paragraph: it ends after `.`, `!`, `?` or `…` (and
|
||||
/// any closing quotes or brackets) followed by a space or the paragraph end, or after a CJK full
|
||||
/// stop, and takes the spaces after it, as deleting a sentence should leave none behind.
|
||||
fn sentence_bounds(s: &str, i: usize) -> (usize, usize) {
|
||||
let (pa, pb) = para_bounds(s, i);
|
||||
let para = s.get(pa..pb).unwrap_or("");
|
||||
let mut start = 0;
|
||||
let mut it = para.char_indices().peekable();
|
||||
while let Some((_, c)) = it.next() {
|
||||
let cjk = matches!(c, '。' | '!' | '?');
|
||||
if !cjk && !matches!(c, '.' | '!' | '?' | '…') {
|
||||
continue;
|
||||
}
|
||||
while it.peek().is_some_and(|&(_, c)| {
|
||||
matches!(c, '.' | '!' | '?' | '…' | '。' | '!' | '?' | '"' | '\'' | '”' | '’' | '»' | '›' | ')' | ']' | '」' | '』' | ')')
|
||||
}) {
|
||||
it.next();
|
||||
}
|
||||
if !cjk && it.peek().is_some_and(|&(_, c)| !c.is_whitespace()) {
|
||||
continue;
|
||||
}
|
||||
while it.peek().is_some_and(|&(_, c)| c.is_whitespace()) {
|
||||
it.next();
|
||||
}
|
||||
let end = it.peek().map_or(para.len(), |&(k, _)| k);
|
||||
if pa + end > i || end == para.len() {
|
||||
return (pa + start, pa + end);
|
||||
}
|
||||
start = end;
|
||||
}
|
||||
(pa + start, pb)
|
||||
}
|
||||
|
||||
/// Type ▸ Story Direction.
|
||||
fn story_direction(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let vertical = p.get("vertical").and_then(Value::as_bool).ok_or_else(|| bad("type.storyDirection", "missing vertical"))?;
|
||||
@@ -665,13 +857,8 @@ fn move_caret(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let t = st.selection.text.ok_or_else(|| bad("text.move", "no caret"))?;
|
||||
let story = st.doc.text_story(t.story, t.cell).ok_or(designcraft_doc::DocError::NoStory(t.story))?;
|
||||
let text = story.text.clone();
|
||||
let cs = s.cache.get(&st.doc, t.story, None);
|
||||
// In a cell, lines come from the cell's own composition.
|
||||
let cs = match t.cell {
|
||||
Some(c) if c.footnote_id().is_some() => compose::find_note(&cs, c.row as u64).map(|(_, n)| n.text.clone()).unwrap_or(cs),
|
||||
Some(c) => compose::find_cell(&cs, c.table, c.row, c.col).map(|(_, _, pc)| pc.text.clone()).unwrap_or(cs),
|
||||
None => cs,
|
||||
};
|
||||
let cs = text_composition(s.cache.get(&st.doc, t.story, None), t.cell);
|
||||
let pos = t.focus.min(text.len());
|
||||
// Vertical lines run top to bottom and follow each other right to left: Down and Up move along
|
||||
// the line (Right and Left of horizontal text), Left and Right to the next and previous line.
|
||||
@@ -712,6 +899,7 @@ fn move_caret(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"storyEnd" => text.len(),
|
||||
other => return Err(bad("text.move", format!("unknown dir `{other}`"))),
|
||||
};
|
||||
s.typing_format = None;
|
||||
let stm = s.doc_mut()?;
|
||||
stm.selection.text = Some(if extend { TextSel { focus: new, ..t } } else { TextSel { anchor: new, focus: new, ..t } });
|
||||
stm.revision += 1;
|
||||
@@ -756,14 +944,9 @@ pub(crate) fn format_targets(s: &Session) -> Vec<Target> {
|
||||
&& let Some(t) = st.doc.story(ts.story).and_then(|x| x.tables.get(&ts.table))
|
||||
{
|
||||
let mut v = Vec::new();
|
||||
let owners = t.owners();
|
||||
for r in ts.range.r0..=ts.range.r1.min(t.nrows().saturating_sub(1)) {
|
||||
for c in ts.range.c0..=ts.range.c1.min(t.ncols().saturating_sub(1)) {
|
||||
if owners[r * t.ncols() + c] == (r, c) {
|
||||
let len = t.cell(r, c).map_or(0, |x| x.text.len());
|
||||
v.push(Target { story: ts.story, cell: Some(CellAddr { table: ts.table, row: r, col: c }), range: 0..len });
|
||||
}
|
||||
}
|
||||
for (r, c) in t.selected_owners(ts.range) {
|
||||
let len = t.cell(r, c).map_or(0, |x| x.text.len());
|
||||
v.push(Target { story: ts.story, cell: Some(CellAddr { table: ts.table, row: r, col: c }), range: 0..len });
|
||||
}
|
||||
return v;
|
||||
}
|
||||
@@ -782,6 +965,23 @@ pub(crate) fn format_targets(s: &Session) -> Vec<Target> {
|
||||
v
|
||||
}
|
||||
|
||||
/// Character formatting chosen at a bare caret in text: applied to what is typed there next.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct TypingFormat {
|
||||
doc: u64,
|
||||
at: TextSel,
|
||||
attrs: CharAttrs,
|
||||
/// Overrides removed (`null` in `type.char`).
|
||||
cleared: Vec<String>,
|
||||
}
|
||||
|
||||
/// The typing format for the active document's caret, if it was chosen at this caret.
|
||||
fn typing_format(s: &Session) -> Option<&TypingFormat> {
|
||||
let st = s.active()?;
|
||||
let t = st.selection.text?;
|
||||
s.typing_format.as_ref().filter(|f| f.doc == st.uid && f.at.story == t.story && f.at.cell == t.cell && f.at.range() == t.range())
|
||||
}
|
||||
|
||||
fn warichu_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let has_opts = ["lines", "size", "lineSpacing", "align", "charsBeforeBreak", "charsAfterBreak"].iter().any(|k| p.get(k).is_some());
|
||||
let on = match p.get("on").and_then(Value::as_bool) {
|
||||
@@ -819,6 +1019,18 @@ fn warichu_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
format_chars(s, &Value::Object(body))
|
||||
}
|
||||
|
||||
pub(crate) fn validate_char_fill(document: &designcraft_doc::Document, attrs: &CharAttrs, command_id: &str) -> Result<()> {
|
||||
if let Some(fill) = &attrs.fill
|
||||
&& document.swatch(fill).is_none()
|
||||
{
|
||||
return Err(bad(
|
||||
command_id,
|
||||
format!("unknown character fill `{fill}`; create a swatch with `swatch.create` and use its returned name as `fill`"),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn format_chars(s: &mut Session, attrs: &Value) -> Result<Value> {
|
||||
let mut a = CharAttrs::default();
|
||||
if let Some(o) = attrs.as_object() {
|
||||
@@ -826,34 +1038,53 @@ pub(crate) fn format_chars(s: &mut Session, attrs: &Value) -> Result<Value> {
|
||||
a.set_json(k, v).map_err(|e| bad("type.char", e))?;
|
||||
}
|
||||
}
|
||||
validate_char_fill(&s.doc()?.doc, &a, "type.char")?;
|
||||
let cleared: Vec<String> = attrs.as_object().map(|o| o.iter().filter(|(_, v)| v.is_null()).map(|(k, _)| k.clone()).collect()).unwrap_or_default();
|
||||
let targets = format_targets(s);
|
||||
// A caret in text: the format is for the next typing.
|
||||
if let [t] = targets.as_slice()
|
||||
&& t.range.is_empty()
|
||||
&& let Some(at) = s.doc()?.selection.text
|
||||
&& s.doc()?.doc.text_story(t.story, t.cell).is_some_and(|st| !st.is_empty())
|
||||
{
|
||||
let doc = s.doc()?.uid;
|
||||
let mut f = typing_format(s).cloned().unwrap_or(TypingFormat { doc, at, attrs: CharAttrs::default(), cleared: vec![] });
|
||||
f.attrs.merge(&a);
|
||||
f.cleared.retain(|k| attrs.get(k).is_none_or(|v| v.is_null()) && !cleared.contains(k));
|
||||
f.cleared.extend(cleared);
|
||||
s.typing_format = Some(f);
|
||||
return ok();
|
||||
}
|
||||
s.edit(|d, _| {
|
||||
for t in &targets {
|
||||
let r = &t.range;
|
||||
let Some(st) = d.text_story_mut(t.story, t.cell) else { continue };
|
||||
if r.is_empty() {
|
||||
// Caret: change the typing format (the empty run at the caret).
|
||||
let pos = r.start;
|
||||
if st.is_empty() {
|
||||
st.chars[0].format.over.merge(&a);
|
||||
st.rev += 1;
|
||||
} else {
|
||||
let _ = pos;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
st.format_chars(r.clone(), |f| {
|
||||
f.over.merge(&a);
|
||||
for k in &cleared {
|
||||
let _ = f.over.set_json(k, &Value::Null);
|
||||
}
|
||||
});
|
||||
}
|
||||
apply_char_attrs(d, &targets, &a, &cleared);
|
||||
ok()
|
||||
})
|
||||
}
|
||||
|
||||
/// Character attributes `a` (and the overrides `cleared` removed) over each target's text.
|
||||
pub(crate) fn apply_char_attrs(d: &mut designcraft_doc::Document, targets: &[Target], a: &CharAttrs, cleared: &[String]) {
|
||||
for t in targets {
|
||||
let r = &t.range;
|
||||
let Some(st) = d.text_story_mut(t.story, t.cell) else { continue };
|
||||
if r.is_empty() {
|
||||
// An empty story: its one (empty) run is the format typing takes.
|
||||
if st.is_empty()
|
||||
&& let Some(run) = st.chars.first_mut()
|
||||
{
|
||||
run.format.over.merge(a);
|
||||
st.rev += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
st.format_chars(r.clone(), |f| {
|
||||
f.over.merge(a);
|
||||
for k in cleared {
|
||||
let _ = f.over.set_json(k, &Value::Null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn format_paras(s: &mut Session, attrs: &Value) -> Result<Value> {
|
||||
let empty = serde_json::Map::new();
|
||||
let obj = attrs.as_object().unwrap_or(&empty);
|
||||
@@ -957,6 +1188,17 @@ fn open_type(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"features": list, "figures": otf::figures(&list)}))
|
||||
}
|
||||
|
||||
/// How much of the selected text `type.selectionAttrs` reports.
|
||||
const SELECTED_TEXT_CHARS: usize = 60;
|
||||
|
||||
fn selected_text_preview(chars: impl Iterator<Item = char>) -> String {
|
||||
chars
|
||||
.map(|c| if c.is_control() || c == designcraft_doc::OBJECT_MARK { ' ' } else { c })
|
||||
.skip_while(|c| c.is_whitespace())
|
||||
.take(SELECTED_TEXT_CHARS)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn selection_attrs(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
let st = s.doc()?;
|
||||
let targets = format_targets(s);
|
||||
@@ -965,10 +1207,176 @@ fn selection_attrs(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
let pi = story.para_at(r.start);
|
||||
let pf = &story.paras[pi];
|
||||
let (pp, base) = st.doc.styles.resolve_para(pf);
|
||||
let cf = if r.is_empty() { story.char_format_at(r.start) } else { story.format_after(r.start) };
|
||||
let cp = st.doc.styles.resolve_char(&base, cf);
|
||||
let cf = effective_char_format(s, &Target { story: sid, cell, range: r.clone() })?;
|
||||
let cp = st.doc.styles.resolve_char(&base, &cf);
|
||||
let selected = selected_text_preview(story.text.get(r.start..r.end).unwrap_or_default().chars());
|
||||
Ok(json!({"story": sid.0, "paragraphStyle": pf.style, "characterStyle": cf.style, "para": pp, "chars": cp,
|
||||
"paraOverrides": pf.para.count(), "charOverrides": cf.over.count()}))
|
||||
"paraOverrides": pf.para.count(), "charOverrides": cf.over.count(), "selectedText": selected.trim()}))
|
||||
}
|
||||
|
||||
/// The first target character, with any pending caret overrides. Keep character-style
|
||||
/// inheritance separate from paragraph defaults for style creation from the selection.
|
||||
pub(crate) fn effective_char_format(s: &Session, target: &Target) -> Result<designcraft_doc::CharFormat> {
|
||||
let story = s.doc()?.doc.text_story(target.story, target.cell).ok_or(designcraft_doc::DocError::NoStory(target.story))?;
|
||||
let mut format = if target.range.is_empty() { story.char_format_at(target.range.start) } else { story.format_after(target.range.start) }.clone();
|
||||
if let Some(pending) = typing_format(s)
|
||||
&& target.story == pending.at.story
|
||||
&& target.cell == pending.at.cell
|
||||
&& target.range == pending.at.range()
|
||||
{
|
||||
apply_typing_format(&mut format, pending);
|
||||
}
|
||||
Ok(format)
|
||||
}
|
||||
|
||||
/// A swatch promoted to a named color keeps any not-yet-inserted text pointing at it.
|
||||
pub(crate) fn rename_typing_swatch(s: &mut Session, from: &str, to: &str) {
|
||||
let Some(uid) = s.active().map(|st| st.uid) else { return };
|
||||
if let Some(pending) = &mut s.typing_format
|
||||
&& pending.doc == uid
|
||||
{
|
||||
for name in [&mut pending.attrs.fill, &mut pending.attrs.stroke, &mut pending.attrs.underline_color, &mut pending.attrs.strikethrough_color]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
if name == from {
|
||||
*name = to.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn swapped_colors(cp: &designcraft_doc::CharProps) -> CharAttrs {
|
||||
CharAttrs {
|
||||
fill: Some(cp.stroke.clone()),
|
||||
fill_tint: Some(cp.stroke_tint),
|
||||
stroke: Some(cp.fill.clone()),
|
||||
stroke_tint: Some(cp.fill_tint),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve each paragraph/run before editing: a mixed selection must not take the first run's colors.
|
||||
fn swap_fill_stroke(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
const ID: &str = "type.swapFillStroke";
|
||||
let targets = format_targets(s);
|
||||
if let [target] = targets.as_slice()
|
||||
&& target.range.is_empty()
|
||||
&& s.doc()?.selection.text.is_some()
|
||||
{
|
||||
let st = s.doc()?;
|
||||
let story = st.doc.text_story(target.story, target.cell).ok_or(designcraft_doc::DocError::NoStory(target.story))?;
|
||||
let pf = story.paras.get(story.para_at(target.range.start)).ok_or_else(|| bad(ID, "missing paragraph"))?;
|
||||
let (_, base) = st.doc.styles.resolve_para(pf);
|
||||
let cp = st.doc.styles.resolve_char(&base, &effective_char_format(s, target)?);
|
||||
return format_chars(s, &json!({"fill": cp.stroke, "fillTint": cp.stroke_tint, "stroke": cp.fill, "strokeTint": cp.fill_tint}));
|
||||
}
|
||||
let mut edits = Vec::new();
|
||||
let d = &s.doc()?.doc;
|
||||
for target in targets {
|
||||
let story = d.text_story(target.story, target.cell).ok_or(designcraft_doc::DocError::NoStory(target.story))?;
|
||||
if story.is_empty() {
|
||||
let pf = story.paras.first().ok_or_else(|| bad(ID, "missing paragraph"))?;
|
||||
let (_, base) = d.styles.resolve_para(pf);
|
||||
let attrs = swapped_colors(&d.styles.resolve_char(&base, story.char_format_at(0)));
|
||||
validate_char_fill(d, &attrs, ID)?;
|
||||
edits.push((target, attrs));
|
||||
continue;
|
||||
}
|
||||
let mut runs = story.runs().peekable();
|
||||
for (pi, para) in story.para_ranges().into_iter().enumerate() {
|
||||
let start = para.start.max(target.range.start);
|
||||
let end = para.end.saturating_add(1).min(story.len()).min(target.range.end);
|
||||
if start >= end {
|
||||
continue;
|
||||
}
|
||||
let pf = story.paras.get(pi).ok_or_else(|| bad(ID, "missing paragraph"))?;
|
||||
let (_, base) = d.styles.resolve_para(pf);
|
||||
while let Some((range, format)) = runs.peek() {
|
||||
if range.end <= start {
|
||||
runs.next();
|
||||
continue;
|
||||
}
|
||||
if range.start >= end {
|
||||
break;
|
||||
}
|
||||
let attrs = swapped_colors(&d.styles.resolve_char(&base, format));
|
||||
validate_char_fill(d, &attrs, ID)?;
|
||||
edits.push((Target { range: start.max(range.start)..end.min(range.end), ..target.clone() }, attrs));
|
||||
if range.end > end {
|
||||
break;
|
||||
}
|
||||
runs.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
s.edit(|d, _| {
|
||||
for (target, attrs) in &edits {
|
||||
apply_char_attrs(d, std::slice::from_ref(target), attrs, &[]);
|
||||
}
|
||||
ok()
|
||||
})
|
||||
}
|
||||
|
||||
fn apply_typing_format(cf: &mut designcraft_doc::CharFormat, f: &TypingFormat) {
|
||||
cf.over.merge(&f.attrs);
|
||||
for k in &f.cleared {
|
||||
let _ = cf.over.set_json(k, &Value::Null);
|
||||
}
|
||||
}
|
||||
|
||||
/// Bold (`bold`) or italic from the font family (style linking), per run of the selection: on or
|
||||
/// off as asked, else the opposite of the first selected character.
|
||||
fn link_style(s: &mut Session, p: &Value, bold: bool) -> Result<Value> {
|
||||
let id = if bold { "type.bold" } else { "type.italic" };
|
||||
let cur = selection_attrs(s, &json!({}))?;
|
||||
let family = cur["chars"]["fontFamily"].as_str().unwrap_or_default().to_string();
|
||||
let style = cur["chars"]["fontStyle"].as_str().unwrap_or_default().to_string();
|
||||
// The document's own fonts count too.
|
||||
let db = designcraft_fonts::FontDb::global().scoped(s.doc()?.doc.font_scope);
|
||||
let (weight, italic) = db.traits_of(&family, &style);
|
||||
let on = p.get("on").and_then(Value::as_bool).unwrap_or(if bold { weight < 650.0 } else { !italic });
|
||||
let (want_bold, want_italic) = if bold { (Some(on), None) } else { (None, Some(on)) };
|
||||
let targets = format_targets(s);
|
||||
// A caret: the linked style of the format there, for the next typing.
|
||||
if let [t] = targets.as_slice()
|
||||
&& t.range.is_empty()
|
||||
{
|
||||
let linked = db.linked_style(&family, &style, want_bold, want_italic).ok_or_else(|| bad(id, format!("{family} has no style for that")))?;
|
||||
format_chars(s, &json!({"fontStyle": linked}))?;
|
||||
return Ok(json!({"on": on, "fontStyle": linked}));
|
||||
}
|
||||
let mut changed = 0;
|
||||
let r = s.edit(|d, _| {
|
||||
let styles = d.styles.clone();
|
||||
for t in &targets {
|
||||
let Some(st) = d.text_story_mut(t.story, t.cell) else { continue };
|
||||
for (pi, pr) in st.para_ranges().into_iter().enumerate() {
|
||||
let (a, b) = (pr.start.max(t.range.start), pr.end.min(t.range.end));
|
||||
if a >= b {
|
||||
continue;
|
||||
}
|
||||
let Some(pf) = st.paras.get(pi) else { continue };
|
||||
let (_, pc) = styles.resolve_para(pf);
|
||||
st.format_chars(a..b, |f| {
|
||||
let cp = styles.resolve_char(&pc, f);
|
||||
if let Some(linked) = db.linked_style(&cp.font_family, &cp.font_style, want_bold, want_italic)
|
||||
&& linked != cp.font_style
|
||||
{
|
||||
f.over.font_style = Some(linked);
|
||||
changed += 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// No undo step for nothing.
|
||||
if changed == 0 {
|
||||
return Err(bad(id, "nothing to change"));
|
||||
}
|
||||
ok()
|
||||
});
|
||||
r?;
|
||||
Ok(json!({"on": on, "changed": changed}))
|
||||
}
|
||||
|
||||
/// Our own filler text (not Adobe's).
|
||||
@@ -1141,6 +1549,37 @@ mod open_type_tests {
|
||||
s.execute("text.move", &json!({"dir": "right"})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_text_preview_stops_at_its_character_budget() {
|
||||
let read = std::cell::Cell::new(0usize);
|
||||
let prefix = ['\t', '\u{2003}', designcraft_doc::OBJECT_MARK];
|
||||
let chars = prefix.into_iter().chain(std::iter::repeat_n('字', 100_000)).inspect(|_| read.set(read.get() + 1));
|
||||
let preview = super::selected_text_preview(chars);
|
||||
assert_eq!(preview, "字".repeat(super::SELECTED_TEXT_CHARS));
|
||||
assert_eq!(read.get(), prefix.len() + super::SELECTED_TEXT_CHARS, "the rest of a large selection is never copied or scanned");
|
||||
assert_eq!(super::selected_text_preview("A\nB".chars()), "A B");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selection_attrs_report_the_selected_text() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s
|
||||
.execute(
|
||||
"frame.create",
|
||||
&json!({"rect": [72, 72, 300, 200], "content": "text", "text": "Spring issue
|
||||
What's new in typography this season, and why it matters to every designer"}),
|
||||
)
|
||||
.unwrap();
|
||||
s.execute("text.select", &json!({"story": r["story"], "anchor": 0, "focus": 13})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["selectedText"], "Spring issue", "on one line, trimmed");
|
||||
s.execute("text.select", &json!({"story": r["story"], "anchor": 13, "focus": 200})).unwrap();
|
||||
let long = s.execute("type.selectionAttrs", &json!({})).unwrap()["selectedText"].as_str().unwrap().to_string();
|
||||
assert!(long.starts_with("What's new") && (55..=60).contains(&long.chars().count()), "the line break skipped, capped: {long}");
|
||||
s.execute("text.select", &json!({"story": r["story"], "anchor": 3, "focus": 3})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["selectedText"], "", "a caret selects nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_type_toggles_reach_the_text() {
|
||||
let mut s = Session::new();
|
||||
@@ -1239,6 +1678,202 @@ mod nested_style_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod multi_click_tests {
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
#[test]
|
||||
fn moving_the_caret_drops_the_pending_typing_format() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 200], "content": "text", "text": "Hello world"})).unwrap();
|
||||
let (fid, sid) = (r["id"].as_u64().unwrap(), r["story"].as_u64().unwrap());
|
||||
let size_at = |s: &Session, b: usize| s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().char_format_at(b).over.size;
|
||||
let pending = |s: &mut Session| {
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 5})).unwrap();
|
||||
s.execute("type.char", &json!({"attrs": {"size": 30}})).unwrap();
|
||||
};
|
||||
// Away and back by arrow keys, by a click, by selecting and by double-clicking: typing at the
|
||||
// same caret is in the text's own format.
|
||||
let moves: Vec<Box<dyn Fn(&mut Session)>> = vec![
|
||||
Box::new(|s| {
|
||||
s.execute("text.move", &json!({"dir": "right"})).unwrap();
|
||||
s.execute("text.move", &json!({"dir": "left"})).unwrap();
|
||||
}),
|
||||
Box::new(|s| {
|
||||
s.execute("text.placeCaret", &json!({"frame": fid, "point": [80, 80]})).unwrap();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 5})).unwrap();
|
||||
}),
|
||||
Box::new(|s| {
|
||||
s.execute("text.selectAt", &json!({"frame": fid, "point": [80, 80], "unit": "story"})).unwrap();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 5})).unwrap();
|
||||
}),
|
||||
];
|
||||
for m in &moves {
|
||||
pending(&mut s);
|
||||
m(&mut s);
|
||||
s.execute("text.insert", &json!({"text": "x"})).unwrap();
|
||||
assert_eq!(size_at(&s, 6), None);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
// Without a move it applies.
|
||||
pending(&mut s);
|
||||
s.execute("text.insert", &json!({"text": "x"})).unwrap();
|
||||
assert_eq!(size_at(&s, 6), Some(30.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_selection_keeps_grapheme_clusters_whole() {
|
||||
use super::word_or_spaces_at as word;
|
||||
// Decomposed "café": the combining acute belongs to the word, wherever the click lands.
|
||||
let s = "un cafe\u{301} noir";
|
||||
let cafe = (3, 3 + "cafe\u{301}".len());
|
||||
assert_eq!(word(s, 4), cafe);
|
||||
assert_eq!(word(s, 7), cafe, "on the combining mark");
|
||||
// Devanagari with a virama (U+094D) and a dependent vowel sign.
|
||||
let s = "\u{928}\u{92E}\u{938}\u{94D}\u{924}\u{947} x";
|
||||
let w = "\u{928}\u{92E}\u{938}\u{94D}\u{924}\u{947}".len();
|
||||
assert_eq!(word(s, 0), (0, w));
|
||||
assert_eq!(word(s, 9), (0, w), "on the virama");
|
||||
// An emoji ZWJ sequence and a variation selector stay one character.
|
||||
let s = "a \u{1F469}\u{200D}\u{1F4BB} b \u{263A}\u{FE0F}";
|
||||
let e = (2, 2 + "\u{1F469}\u{200D}\u{1F4BB}".len());
|
||||
assert_eq!(word(s, 2), e);
|
||||
assert_eq!(word(s, 6), e, "on the joiner");
|
||||
assert_eq!(word(s, s.len() - 1), (s.len() - 6, s.len()), "at the end: the last character whole");
|
||||
// Empty paragraphs select nothing.
|
||||
assert_eq!(word("a\n\nb", 2), (2, 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clicks_select_words_spaces_lines_paragraphs_and_the_story() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let text = "Hello brave world.\nSecond paragraph here.";
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 200], "content": "text", "text": text})).unwrap();
|
||||
let (fid, sid) = (r["id"].as_u64().unwrap(), r["story"].as_u64().unwrap());
|
||||
// Spread point over the right half of the glyph at byte `b`.
|
||||
let at = |s: &Session, b: usize| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let cs = designcraft_compose::compose_story(d, designcraft_doc::StoryId(sid), &Default::default());
|
||||
let (l, g) = cs.frames[0].lines.iter().find_map(|l| l.glyphs.iter().find(|g| g.byte == b && g.len > 0).map(|g| (l, g))).unwrap();
|
||||
json!([g.x + g.adv * 0.75, l.baseline - 3.0])
|
||||
};
|
||||
let mut select = |unit: &str, b: usize| {
|
||||
let p = at(&s, b);
|
||||
let r: Value = s.execute("text.selectAt", &json!({"frame": fid, "point": p, "unit": unit})).unwrap();
|
||||
(r["anchor"].as_u64().unwrap() as usize, r["focus"].as_u64().unwrap() as usize)
|
||||
};
|
||||
assert_eq!(select("word", 1), (0, 5), "inside a word");
|
||||
assert_eq!(select("word", 4), (0, 5), "its last letter");
|
||||
assert_eq!(select("word", 5), (5, 7), "the run of spaces after it");
|
||||
assert_eq!(select("word", 18), (18, 19), "punctuation alone");
|
||||
assert_eq!(select("line", 9), (0, 19));
|
||||
assert_eq!(select("paragraph", 9), (0, 20), "with its return");
|
||||
assert_eq!(select("paragraph", 22), (20, text.len()));
|
||||
assert_eq!(select("story", 9), (0, text.len()));
|
||||
// A second click inside the selected word selects again rather than starting a text drag.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 7, "focus": 12})).unwrap();
|
||||
let p = at(&s, 9);
|
||||
s.execute("text.selectAt", &json!({"frame": fid, "point": p, "unit": "line"})).unwrap();
|
||||
let t = s.doc().unwrap().selection.text.unwrap();
|
||||
assert_eq!((t.anchor, t.focus), (0, 19));
|
||||
s.execute("text.release", &json!({"frame": fid, "point": p, "moved": false})).unwrap();
|
||||
let t = s.doc().unwrap().selection.text.unwrap();
|
||||
assert_eq!((t.anchor, t.focus), (0, 19), "releasing keeps it");
|
||||
// The older word command agrees.
|
||||
let p = at(&s, 8);
|
||||
s.execute("text.selectWord", &json!({"frame": fid, "point": p})).unwrap();
|
||||
let t = s.doc().unwrap().selection.text.unwrap();
|
||||
assert_eq!((t.anchor, t.focus), (7, 12));
|
||||
assert!(s.execute("text.selectAt", &json!({"frame": fid, "point": p, "unit": "sentence"})).is_err());
|
||||
assert!(s.execute("text.selectAt", &json!({"frame": fid, "unit": "word"})).is_err(), "no point");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod style_link_tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
#[test]
|
||||
fn bold_and_italic_follow_the_family_and_toggle() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 500, 200], "content": "text", "text": "plain words here"})).unwrap();
|
||||
let sid = r["story"].as_u64().unwrap();
|
||||
let style_at = |s: &mut Session, b: usize| {
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": b, "focus": b + 1})).unwrap();
|
||||
s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fontStyle"].as_str().unwrap().to_string()
|
||||
};
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 5})).unwrap();
|
||||
s.execute("type.italic", &json!({})).unwrap();
|
||||
assert_eq!(style_at(&mut s, 0), "Italic");
|
||||
// Bold over italic and plain text: each run takes its own linked style.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 11})).unwrap();
|
||||
s.execute("type.bold", &json!({})).unwrap();
|
||||
assert_eq!(style_at(&mut s, 0), "Bold Italic");
|
||||
assert_eq!(style_at(&mut s, 7), "Bold");
|
||||
// Again: off, back to each run's regular weight.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 11})).unwrap();
|
||||
s.execute("type.bold", &json!({})).unwrap();
|
||||
assert_eq!(style_at(&mut s, 0), "Italic");
|
||||
assert_eq!(style_at(&mut s, 7), "Regular");
|
||||
// Nothing to change: an error and no undo step.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 6, "focus": 11})).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
assert!(s.execute("type.bold", &json!({"on": false})).is_err());
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps);
|
||||
// At a caret the style is for what is typed next.
|
||||
let end = "plain words here".len();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": end, "focus": end})).unwrap();
|
||||
s.execute("type.bold", &json!({})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fontStyle"], "Bold");
|
||||
s.execute("text.insert", &json!({"text": " now"})).unwrap();
|
||||
assert_eq!(style_at(&mut s, end + 1), "Bold");
|
||||
assert_eq!(style_at(&mut s, end - 1), "Regular", "text before the caret is unchanged");
|
||||
// Moving the caret forgets it.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 2, "focus": 2})).unwrap();
|
||||
s.execute("type.char", &json!({"attrs": {"size": 30}})).unwrap();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 3, "focus": 3})).unwrap();
|
||||
s.execute("text.insert", &json!({"text": "x"})).unwrap();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 3, "focus": 4})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["size"], 12.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod text_color_tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
#[test]
|
||||
fn unnamed_colours_go_to_text_when_asked() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 300, 200], "content": "text", "text": "colour me", "caret": false})).unwrap();
|
||||
let (fid, sid) = (r["id"].as_u64().unwrap(), r["story"].as_u64().unwrap());
|
||||
s.execute("selection.set", &json!({"ids": [fid]})).unwrap();
|
||||
let fill_of_frame = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(fid)).unwrap().fill.swatch.clone();
|
||||
let before = fill_of_frame(&s);
|
||||
let r = s.execute("object.color", &json!({"color": "#ff0000", "text": true})).unwrap();
|
||||
let red = r["swatch"].as_str().unwrap().to_string();
|
||||
assert_eq!(fill_of_frame(&s), before, "the frame keeps its fill");
|
||||
let story = s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().clone();
|
||||
assert_eq!(story.char_format_at(3).over.fill.as_deref(), Some(red.as_str()));
|
||||
// A text selection takes it without asking.
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 3})).unwrap();
|
||||
let r = s.execute("object.color", &json!({"color": "#00ff00", "target": "stroke"})).unwrap();
|
||||
let story = s.doc().unwrap().doc.story(designcraft_doc::StoryId(sid)).unwrap().clone();
|
||||
assert_eq!(story.format_after(0).over.stroke.as_deref(), r["swatch"].as_str());
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), 3, "the frame, then one undo step per colour");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod drag_text_tests {
|
||||
use serde_json::json;
|
||||
@@ -1883,3 +2518,44 @@ mod story_query_contract_tests {
|
||||
assert_eq!(s.execute("story.get", &json!({})).unwrap()["text"], "");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sentence_selection_tests {
|
||||
use super::{para_bounds, sentence_bounds};
|
||||
|
||||
fn sentence(s: &str, at: &str) -> String {
|
||||
let i = s.find(at).unwrap();
|
||||
let (a, b) = sentence_bounds(s, i);
|
||||
s[a..b].to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sentences_end_at_a_stop_before_a_space_or_the_paragraph_end() {
|
||||
let s = "It costs 3.5 euros. \u{201C}Really?\u{201D} (Twice!) Yes\u{2026} fine.\nNext one. Last";
|
||||
assert_eq!(sentence(s, "costs"), "It costs 3.5 euros. ");
|
||||
assert_eq!(sentence(s, "Really"), "\u{201C}Really?\u{201D} ");
|
||||
assert_eq!(sentence(s, "Twice"), "(Twice!) ");
|
||||
assert_eq!(sentence(s, "Yes"), "Yes\u{2026} ");
|
||||
assert_eq!(sentence(s, "fine"), "fine.");
|
||||
assert_eq!(sentence(s, "one"), "Next one. ");
|
||||
assert_eq!(sentence(s, "Last"), "Last");
|
||||
// The caret at the very end of a paragraph is in its last sentence.
|
||||
let end = s.find('\n').unwrap();
|
||||
assert_eq!(sentence_bounds(s, end), (s.find("fine").unwrap(), end));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cjk_full_stops_end_sentences_without_a_space() {
|
||||
let s = "これは文です。次の文!最後";
|
||||
assert_eq!(sentence(s, "次"), "次の文!");
|
||||
assert_eq!(sentence(s, "最後"), "最後");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraphs_stop_at_returns() {
|
||||
let s = "One.\nTwo two.\n";
|
||||
assert_eq!(para_bounds(s, 6), (5, 13));
|
||||
assert_eq!(para_bounds(s, 0), (0, 4));
|
||||
assert_eq!(para_bounds(s, s.len()), (s.len(), s.len()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
//! Cross-command caret color and character-style regressions.
|
||||
use crate::Session;
|
||||
use designcraft_doc::StoryId;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
fn setup(text: &str) -> (Session, u64) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let f = s.execute("frame.create", &json!({"rect": [36,36,300,200], "content":"text", "text":text})).unwrap();
|
||||
let sid = f["story"].as_u64().unwrap();
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":text.len()})).unwrap();
|
||||
(s, sid)
|
||||
}
|
||||
fn attrs(s: &mut Session) -> Value {
|
||||
s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"].clone()
|
||||
}
|
||||
#[test]
|
||||
fn integration_color_character_style_captures_pending_and_null() {
|
||||
let (mut s, sid) = setup("hello");
|
||||
s.execute("type.char", &json!({"size":30,"tracking":40,"fill":"[Paper]"})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name":"Pending","fromSelection":true,"chars":{"tracking":null}})).unwrap();
|
||||
let a = &s.doc().unwrap().doc.styles.char_style("Pending").unwrap().chars;
|
||||
assert_eq!(a.size, Some(30.0));
|
||||
assert_eq!(a.fill.as_deref(), Some("[Paper]"));
|
||||
assert_eq!(a.tracking, None);
|
||||
assert_eq!(s.doc().unwrap().doc.story(StoryId(sid)).unwrap().text, "hello");
|
||||
}
|
||||
#[test]
|
||||
fn integration_color_pending_follows_promoted_swatch() {
|
||||
for target in ["fill", "stroke"] {
|
||||
let (mut s, sid) = setup("hello");
|
||||
let old = s.execute("object.color", &json!({"color":"#123456","target":target})).unwrap()["swatch"].clone();
|
||||
s.execute("swatch.addToSwatches", &json!({"swatch":old,"name":"Brand"})).unwrap();
|
||||
assert_eq!(attrs(&mut s)[target], "Brand");
|
||||
s.execute("text.insert", &json!({"text":"X"})).unwrap();
|
||||
let st = s.doc().unwrap().doc.story(StoryId(sid)).unwrap();
|
||||
let f = &st.format_after(5).over;
|
||||
assert_eq!(if target == "fill" { f.fill.as_deref() } else { f.stroke.as_deref() }, Some("Brand"));
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn integration_color_undo_discards_missing_pending_swatch() {
|
||||
let (mut s, sid) = setup("hello");
|
||||
let old = s.execute("object.color", &json!({"color":"#123456"})).unwrap()["swatch"].as_str().unwrap().to_string();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.swatch(&old).is_none());
|
||||
assert_ne!(attrs(&mut s)["fill"], old);
|
||||
s.execute("text.insert", &json!({"text":"X"})).unwrap();
|
||||
assert_ne!(s.doc().unwrap().doc.story(StoryId(sid)).unwrap().format_after(5).over.fill.as_deref(), Some(old.as_str()));
|
||||
}
|
||||
#[test]
|
||||
fn integration_color_empty_story_uses_one_undo_step() {
|
||||
let (mut s, sid) = setup("");
|
||||
let before = serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap();
|
||||
let n = s.doc().unwrap().history.undo.len();
|
||||
s.execute("object.color", &json!({"color":"#123456"})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), n + 1);
|
||||
let colored = serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap(), before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap(), colored);
|
||||
s.execute("text.insert", &json!({"text":"X"})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.story(StoryId(sid)).unwrap().format_after(0).over.fill.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integration_color_swap_resolves_each_paragraph_and_keeps_unselected_text() {
|
||||
let (mut s, sid) = setup("ab\ncd");
|
||||
for (name, fill, stroke, ft, st, start, end) in
|
||||
[("First", "[Paper]", "[Black]", 0.2, 0.8, 0, 2), ("Second", "[Black]", "[Paper]", 0.4, 0.6, 3, 5)]
|
||||
{
|
||||
s.execute("style.paragraph.create", &json!({"name":name,"chars":{"fill":fill,"stroke":stroke,"fillTint":ft,"strokeTint":st}})).unwrap();
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":start,"focus":end})).unwrap();
|
||||
s.execute("style.paragraph.apply", &json!({"name":name})).unwrap();
|
||||
}
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":1,"focus":4})).unwrap();
|
||||
let before = serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap();
|
||||
let n = s.doc().unwrap().history.undo.len();
|
||||
s.execute("type.swapFillStroke", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), n + 1);
|
||||
for (pos, fill, stroke, ft, st) in [
|
||||
(0, "[Paper]", "[Black]", 0.2, 0.8),
|
||||
(1, "[Black]", "[Paper]", 0.8, 0.2),
|
||||
(2, "[Black]", "[Paper]", 0.8, 0.2),
|
||||
(3, "[Paper]", "[Black]", 0.6, 0.4),
|
||||
(4, "[Black]", "[Paper]", 0.4, 0.6),
|
||||
] {
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":pos,"focus":pos+1})).unwrap();
|
||||
let a = attrs(&mut s);
|
||||
assert_eq!(a["fill"], fill);
|
||||
assert_eq!(a["stroke"], stroke);
|
||||
assert!((a["fillTint"].as_f64().unwrap() - ft).abs() < 1e-6);
|
||||
assert!((a["strokeTint"].as_f64().unwrap() - st).abs() < 1e-6);
|
||||
}
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integration_color_swap_caret_applies_only_to_next_typing() {
|
||||
let (mut s, sid) = setup("hello");
|
||||
s.execute("type.char", &json!({"fill":"[Paper]","stroke":"[Black]","fillTint":0.2,"strokeTint":0.8})).unwrap();
|
||||
let before = serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap();
|
||||
s.execute("type.swapFillStroke", &json!({})).unwrap();
|
||||
assert_eq!(serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap(), before);
|
||||
let a = attrs(&mut s);
|
||||
assert_eq!(a["fill"], "[Black]");
|
||||
assert_eq!(a["stroke"], "[Paper]");
|
||||
s.execute("text.insert", &json!({"text":"X"})).unwrap();
|
||||
let st = s.doc().unwrap().doc.story(StoryId(sid)).unwrap();
|
||||
assert_eq!(st.text, "helloX");
|
||||
assert_eq!(st.format_after(0).over.fill, None);
|
||||
assert_eq!(st.format_after(5).over.stroke.as_deref(), Some("[Paper]"));
|
||||
assert_eq!(st.format_after(5).over.fill_tint, Some(0.8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integration_color_swap_selected_empty_cells_keeps_each_format() {
|
||||
let (mut s, sid) = setup("hello");
|
||||
let result = s.execute("table.insert", &json!({"rows":1,"cols":2})).unwrap();
|
||||
let tid = result["table"].as_u64().unwrap();
|
||||
s.edit(|d, _| {
|
||||
let table = std::sync::Arc::make_mut(d.story_mut(StoryId(sid)).unwrap().tables.get_mut(&tid).unwrap());
|
||||
for (c, fill, stroke) in [(0, "[Paper]", "[Black]"), (1, "[Black]", "[Paper]")] {
|
||||
let attrs = &mut table.cell_mut(0, c).unwrap().text.chars[0].format.over;
|
||||
attrs.fill = Some(fill.into());
|
||||
attrs.stroke = Some(stroke.into());
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("table.select", &json!({"story":sid,"table":tid,"what":"table"})).unwrap();
|
||||
let before = serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap();
|
||||
s.execute("type.swapFillStroke", &json!({})).unwrap();
|
||||
let table = &s.doc().unwrap().doc.story(StoryId(sid)).unwrap().tables[&tid];
|
||||
assert_eq!(table.cell(0, 0).unwrap().text.chars[0].format.over.fill.as_deref(), Some("[Black]"));
|
||||
assert_eq!(table.cell(0, 1).unwrap().text.chars[0].format.over.fill.as_deref(), Some("[Paper]"));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integration_color_failed_promotion_preserves_pending_and_history() {
|
||||
let (mut s, _) = setup("hello");
|
||||
s.execute("swatch.create", &json!({"name":"Brand","color":"#654321"})).unwrap();
|
||||
let old = s.execute("object.color", &json!({"color":"#123456"})).unwrap()["swatch"].clone();
|
||||
let before = s.doc().unwrap().clone();
|
||||
let pending = attrs(&mut s);
|
||||
assert!(s.execute("swatch.addToSwatches", &json!({"swatch":old,"name":"Brand"})).is_err());
|
||||
assert_eq!(attrs(&mut s), pending);
|
||||
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().doc, &before.doc));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), before.history.undo.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integration_color_promote_undo_redo_never_reuses_a_missing_pending_name() {
|
||||
let (mut s, _) = setup("hello");
|
||||
let old = s.execute("object.color", &json!({"color":"#123456"})).unwrap()["swatch"].clone();
|
||||
s.execute("swatch.addToSwatches", &json!({"swatch":old,"name":"Brand"})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let fill = attrs(&mut s)["fill"].as_str().unwrap().to_string();
|
||||
assert!(s.doc().unwrap().doc.swatch(&fill).is_some());
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
let fill = attrs(&mut s)["fill"].as_str().unwrap().to_string();
|
||||
assert!(s.doc().unwrap().doc.swatch(&fill).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integration_color_swap_validates_new_fill_before_any_edit() {
|
||||
let (mut s, sid) = setup("hello");
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":0,"focus":5})).unwrap();
|
||||
s.execute("type.char", &json!({"fill":"[Paper]","stroke":"Missing Ink"})).unwrap();
|
||||
let before = s.doc().unwrap().clone();
|
||||
let journal = s.journal.clone();
|
||||
let error = s.execute("type.swapFillStroke", &json!({})).unwrap_err().to_string();
|
||||
assert!(error.contains("Missing Ink"), "{error}");
|
||||
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().doc, &before.doc));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), before.history.undo.len());
|
||||
assert_eq!(s.journal, journal);
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
//! Threading text frames: thread a frame after another (their stories merge), break a thread,
|
||||
//! take a frame out of its thread. The Selection tool's loaded text cursor runs these.
|
||||
|
||||
use designcraft_doc::{Document, ItemId, ParaFormat, Selection, StoryId};
|
||||
use designcraft_geom::Rect;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{CommandSpec, bad, bool_or, cmd, has_doc, id_param, rect_param, spread_param, str_param};
|
||||
use crate::{Result, Session};
|
||||
|
||||
/// Pages Shift-click autoflow may add before it gives up.
|
||||
const AUTOFLOW_MAX_PAGES: usize = 2000;
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"text.thread",
|
||||
"Thread Text Frames",
|
||||
[],
|
||||
None,
|
||||
"{from?, to?, rect?: [x0,y0,x1,y1] (spread coords), spread?, autoflow?: bool} — threads frame `to` right after frame `from` (between `from` and the frame that followed it, if any). `to` is an empty frame or a text frame nothing threads into yet; its text joins the end of the story. Give `rect` in place of `from` or `to` to thread a new text frame made there on `spread`: `{from, rect}` adds it after `from`, `{rect, to}` in front of `to`. autoflow: then add pages with threaded frames until the story fits. Selects the frame that joined the thread. → {story, from, to, overset, pagesAdded}",
|
||||
has_doc,
|
||||
thread
|
||||
),
|
||||
cmd!(
|
||||
"text.unthread",
|
||||
"Break Thread",
|
||||
[],
|
||||
None,
|
||||
"{frame?, side?: after|before} — breaks the thread after (or before) `frame` (default: the selected text frame). The frames from the break on stay threaded to each other but become empty; the text stays in the story, overset if the earlier frames can't hold it",
|
||||
has_doc,
|
||||
unthread
|
||||
),
|
||||
cmd!(
|
||||
"text.removeFromThread",
|
||||
"Remove Frame from Thread",
|
||||
[],
|
||||
None,
|
||||
"{frame?} — takes `frame` (default: the selected text frame) out of its thread: the frames before and after it join up and keep the text, `frame` stays empty",
|
||||
has_doc,
|
||||
remove_from_thread
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/// `frame` param, or the first selected text frame.
|
||||
fn frame_param(s: &Session, p: &Value, cmd: &str) -> Result<ItemId> {
|
||||
let st = s.doc()?;
|
||||
let id = id_param(p, "frame")
|
||||
.or_else(|| st.selection.items.iter().copied().find(|i| st.doc.item(*i).is_some_and(|it| it.is_text_frame())))
|
||||
.ok_or_else(|| bad(cmd, "no text frame given or selected"))?;
|
||||
if !st.doc.item(id).is_some_and(|it| it.is_text_frame()) {
|
||||
return Err(bad(cmd, format!("{id} is not a text frame")));
|
||||
}
|
||||
check_editable(&st.doc, id, cmd)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn check_editable(d: &Document, id: ItemId, cmd: &str) -> Result<()> {
|
||||
let it = d.item(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
|
||||
if it.locked || d.layer(it.layer).is_some_and(|l| l.locked) {
|
||||
return Err(bad(cmd, format!("{id} is locked or on a locked layer")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A rect param made safe for a frame: finite, ordered, at least half a point each way.
|
||||
fn frame_rect(p: &Value) -> Result<Option<Rect>> {
|
||||
let Some(r) = rect_param(p, "rect") else { return Ok(None) };
|
||||
if ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) {
|
||||
return Err(bad("text.thread", "rect must be finite"));
|
||||
}
|
||||
let (x0, x1) = (r.x0.min(r.x1), r.x0.max(r.x1));
|
||||
let (y0, y1) = (r.y0.min(r.y1), r.y0.max(r.y1));
|
||||
Ok(Some(Rect::new(x0, y0, x1.max(x0 + 0.5), y1.max(y0 + 0.5))))
|
||||
}
|
||||
|
||||
fn thread(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (from, to, rect) = (id_param(p, "from"), id_param(p, "to"), frame_rect(p)?);
|
||||
let sr = spread_param(p, "spread")?;
|
||||
let autoflow = bool_or(p, "autoflow", false);
|
||||
let layer = s.doc()?.active_layer;
|
||||
s.edit(|d, sel| {
|
||||
let mut new_frame = || -> Result<ItemId> {
|
||||
let r = rect.ok_or_else(|| bad("text.thread", "give two of from, to and rect"))?;
|
||||
if d.layer(layer).is_none_or(|l| l.locked || !l.visible) {
|
||||
return Err(bad("text.thread", "the active layer is locked or hidden"));
|
||||
}
|
||||
let para = ParaFormat { style: d.styles.default_paragraph.clone(), ..Default::default() };
|
||||
Ok(d.add_text_frame(sr, r, layer, "", para)?.0)
|
||||
};
|
||||
let (from, to, joined) = match (from, to) {
|
||||
(Some(f), Some(t)) if rect.is_none() => (f, t, t),
|
||||
(Some(f), None) => {
|
||||
let t = new_frame()?;
|
||||
(f, t, t)
|
||||
}
|
||||
(None, Some(t)) => {
|
||||
let f = new_frame()?;
|
||||
(f, t, f)
|
||||
}
|
||||
_ => return Err(bad("text.thread", "give two of from, to and rect")),
|
||||
};
|
||||
d.check_thread(from, to)?;
|
||||
d.thread_keeping_story(from, to)?;
|
||||
let sid = d.item(from).and_then(|i| i.text_frame()).map(|t| t.story).ok_or_else(|| bad("text.thread", "threading lost the story"))?;
|
||||
let pages = if autoflow { super::place_text_autoflow(d, sid, AUTOFLOW_MAX_PAGES, "text.thread")? } else { 0 };
|
||||
*sel = Selection::items(vec![joined]);
|
||||
Ok(json!({"story": sid.0, "from": from.0, "to": to.0, "overset": overset(d, sid), "pagesAdded": pages}))
|
||||
})
|
||||
}
|
||||
|
||||
fn overset(d: &Document, sid: StoryId) -> bool {
|
||||
designcraft_compose::compose_story(d, sid, &Default::default()).overset_at.is_some()
|
||||
}
|
||||
|
||||
fn unthread(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "text.unthread";
|
||||
let frame = frame_param(s, p, CMD)?;
|
||||
let d0 = &s.doc()?.doc;
|
||||
let at = match str_param(p, "side").unwrap_or("after") {
|
||||
"after" => d0.next_frame(frame).map(|_| frame).ok_or_else(|| bad(CMD, format!("no frame follows {frame} in its thread")))?,
|
||||
"before" => d0.prev_frame(frame).ok_or_else(|| bad(CMD, format!("no frame comes before {frame} in its thread")))?,
|
||||
other => return Err(bad(CMD, format!("unknown side `{other}` (after, before)"))),
|
||||
};
|
||||
check_editable(d0, at, CMD)?;
|
||||
if let Some(next) = d0.next_frame(at) {
|
||||
check_editable(d0, next, CMD)?;
|
||||
}
|
||||
s.edit(|d, _| {
|
||||
d.unthread_after(at)?;
|
||||
let sid = d.item(at).and_then(|i| i.text_frame()).map(|t| t.story).ok_or_else(|| bad(CMD, "no story"))?;
|
||||
Ok(json!({"story": sid.0, "overset": overset(d, sid)}))
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_from_thread(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let frame = frame_param(s, p, "text.removeFromThread")?;
|
||||
let d = &s.doc()?.doc;
|
||||
for neighbour in [d.prev_frame(frame), d.next_frame(frame)].into_iter().flatten() {
|
||||
check_editable(d, neighbour, "text.removeFromThread")?;
|
||||
}
|
||||
s.edit(|d, _| {
|
||||
d.remove_from_thread(frame)?;
|
||||
Ok(json!({}))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 2})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn text_frame(s: &mut Session, rect: [f64; 4], text: &str) -> (u64, u64) {
|
||||
let r = s.execute("frame.create", &json!({"rect": rect, "content": "text", "text": text, "caret": false})).unwrap();
|
||||
(r["id"].as_u64().unwrap(), r["story"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn story(s: &mut Session, frame: u64) -> serde_json::Value {
|
||||
s.execute("story.get", &json!({"frame": frame})).unwrap()
|
||||
}
|
||||
|
||||
fn frames(s: &mut Session, frame: u64) -> Vec<u64> {
|
||||
story(s, frame)["frames"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect()
|
||||
}
|
||||
|
||||
const LONG: &str = "Threaded text keeps flowing from one frame into the next one. ";
|
||||
|
||||
#[test]
|
||||
fn thread_into_an_empty_frame_and_undo() {
|
||||
let mut s = session();
|
||||
let (a, sid) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], &LONG.repeat(10));
|
||||
let b = s.execute("frame.create", &json!({"rect": [72, 300, 200, 400], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
|
||||
assert!(!story(&mut s, a)["overset"].is_null(), "the first frame is overset");
|
||||
let r = s.execute("text.thread", &json!({"from": a, "to": b})).unwrap();
|
||||
assert_eq!(r["story"].as_u64(), Some(sid));
|
||||
assert_eq!(frames(&mut s, a), [a, b]);
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert!(d.item(designcraft_doc::ItemId(b)).unwrap().is_text_frame());
|
||||
assert_eq!(s.doc().unwrap().selection.items, [designcraft_doc::ItemId(b)]);
|
||||
// The text now reaches the second frame.
|
||||
let cs = designcraft_compose::compose_story(&s.doc().unwrap().doc, designcraft_doc::StoryId(sid), &Default::default());
|
||||
assert!(cs.frames.iter().any(|f| f.frame.0 == b && !f.range.is_empty()), "text flows into the new frame");
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a]);
|
||||
assert!(!s.doc().unwrap().doc.item(designcraft_doc::ItemId(b)).unwrap().is_text_frame());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thread_into_a_text_frame_merges_its_story() {
|
||||
let mut s = session();
|
||||
let (a, sa) = text_frame(&mut s, [72.0, 72.0, 200.0, 200.0], "First");
|
||||
let (b, sb) = text_frame(&mut s, [72.0, 300.0, 200.0, 400.0], "Second");
|
||||
s.execute("text.thread", &json!({"from": a, "to": b})).unwrap();
|
||||
let st = story(&mut s, b);
|
||||
assert_eq!(st["id"].as_u64(), Some(sa));
|
||||
assert_eq!(st["text"], "First\nSecond");
|
||||
assert!(s.doc().unwrap().doc.story(designcraft_doc::StoryId(sb)).is_none());
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thread_into_the_middle_of_a_thread() {
|
||||
let mut s = session();
|
||||
let (a, _) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], &LONG.repeat(8));
|
||||
let (c, _) = text_frame(&mut s, [72.0, 500.0, 200.0, 600.0], "");
|
||||
s.execute("text.thread", &json!({"from": a, "to": c})).unwrap();
|
||||
let (b, _) = text_frame(&mut s, [72.0, 300.0, 200.0, 400.0], "");
|
||||
s.execute("text.thread", &json!({"from": a, "to": b})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, b, c]);
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_threads_are_errors_and_change_nothing() {
|
||||
let mut s = session();
|
||||
let (a, _) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], "a");
|
||||
let (b, _) = text_frame(&mut s, [72.0, 300.0, 200.0, 400.0], "b");
|
||||
let (x, _) = text_frame(&mut s, [300.0, 72.0, 400.0, 120.0], "x");
|
||||
let (y, _) = text_frame(&mut s, [300.0, 300.0, 400.0, 400.0], "y");
|
||||
let g = s.execute("line.create", &json!({"a": [300, 500], "b": [400, 600]})).unwrap()["id"].as_u64().unwrap();
|
||||
let group = {
|
||||
let p = s.execute("frame.create", &json!({"rect": [500, 500, 550, 550], "content": "unassigned"})).unwrap()["id"].clone();
|
||||
let q = s.execute("frame.create", &json!({"rect": [560, 500, 600, 550], "content": "unassigned"})).unwrap()["id"].clone();
|
||||
s.execute("selection.set", &json!({"ids": [p, q]})).unwrap();
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
s.doc().unwrap().selection.items[0].0
|
||||
};
|
||||
s.execute("text.thread", &json!({"from": a, "to": b})).unwrap();
|
||||
s.execute("text.thread", &json!({"from": x, "to": y})).unwrap();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
for p in [
|
||||
json!({"from": a, "to": a}),
|
||||
json!({"from": a, "to": g}),
|
||||
json!({"from": g, "to": a}),
|
||||
json!({"from": a, "to": group}),
|
||||
// Earlier in the same thread.
|
||||
json!({"from": b, "to": a}),
|
||||
// Text already flows into y from x.
|
||||
json!({"from": a, "to": y}),
|
||||
json!({"from": a, "to": 999_999}),
|
||||
json!({"from": a}),
|
||||
json!({"from": a, "to": b, "rect": [0, 0, 10, 10]}),
|
||||
json!({"rect": [0, 0, 10, 10]}),
|
||||
json!({"from": a, "rect": [0, 0, 10, 10], "spread": 99}),
|
||||
json!({"from": "a", "to": [1]}),
|
||||
json!([1, 2]),
|
||||
] {
|
||||
assert!(s.execute("text.thread", &p).is_err(), "{p}");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn locked_frames_and_layers_refuse_threads() {
|
||||
let mut s = session();
|
||||
let (a, _) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], &LONG.repeat(8));
|
||||
let (b, _) = text_frame(&mut s, [72.0, 300.0, 200.0, 400.0], "");
|
||||
let layer = s.doc().unwrap().active_layer.0;
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
s.execute("object.lock", &json!({"ids": [b]})).unwrap();
|
||||
assert!(s.execute("text.thread", &json!({"from": a, "to": b})).is_err(), "locked frame");
|
||||
assert!(s.execute("text.thread", &json!({"from": b, "to": a})).is_err(), "locked frame");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
for (set, rect) in [(json!({"locked": true}), false), (json!({"locked": true}), true), (json!({"visible": false}), true)] {
|
||||
let mut p = json!({"id": layer});
|
||||
p.as_object_mut().unwrap().extend(set.as_object().unwrap().clone());
|
||||
s.execute("layer.set", &p).unwrap();
|
||||
let t = if rect { json!({"from": a, "rect": [300, 300, 400, 400]}) } else { json!({"from": a, "to": b}) };
|
||||
assert!(s.execute("text.thread", &t).is_err(), "{p} {t}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
assert_eq!(frames(&mut s, a), [a]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thread_a_new_frame_after_or_in_front() {
|
||||
let mut s = session();
|
||||
let (a, sa) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], &LONG.repeat(8));
|
||||
let r = s.execute("text.thread", &json!({"from": a, "rect": [72, 300, 200, 400]})).unwrap();
|
||||
let n = r["to"].as_u64().unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, n]);
|
||||
assert_eq!(s.doc().unwrap().selection.items, [designcraft_doc::ItemId(n)]);
|
||||
let b = s.doc().unwrap().doc.item(designcraft_doc::ItemId(n)).unwrap().bounds();
|
||||
assert_eq!(b, Rect::new(72.0, 300.0, 200.0, 400.0));
|
||||
// In front of the first frame: the story keeps its id and text.
|
||||
let r = s.execute("text.thread", &json!({"rect": [300, 72, 400, 120], "to": a})).unwrap();
|
||||
let m = r["from"].as_u64().unwrap();
|
||||
assert_eq!(r["story"].as_u64(), Some(sa));
|
||||
assert_eq!(frames(&mut s, a), [m, a, n]);
|
||||
assert_eq!(story(&mut s, a)["text"], LONG.repeat(8));
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
// One undo step each.
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, n]);
|
||||
}
|
||||
|
||||
use designcraft_geom::Rect;
|
||||
|
||||
#[test]
|
||||
fn thread_with_autoflow_adds_pages_until_the_text_fits() {
|
||||
let mut s = session();
|
||||
let (a, _) = text_frame(&mut s, [72.0, 72.0, 300.0, 200.0], &LONG.repeat(300));
|
||||
let r = s.execute("text.thread", &json!({"from": a, "rect": [72, 300, 300, 700], "autoflow": true})).unwrap();
|
||||
assert_eq!(r["overset"], false);
|
||||
assert!(r["pagesAdded"].as_u64().unwrap() >= 1, "{r}");
|
||||
assert!(frames(&mut s, a).len() >= 3);
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
// One undo removes the frames and the pages.
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a]);
|
||||
assert_eq!(s.doc().unwrap().doc.page_count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn autoflow_from_a_parent_frame_is_an_error() {
|
||||
let mut s = session();
|
||||
let parent = json!({"kind": "parent", "index": 0});
|
||||
let r = s.execute(
|
||||
"frame.create",
|
||||
&json!({"rect": [72, 72, 300, 200], "content": "text", "text": LONG.repeat(300), "caret": false, "spread": parent}),
|
||||
);
|
||||
let a = r.unwrap()["id"].as_u64().unwrap();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let doc = s.doc().unwrap().doc.clone();
|
||||
let e = s.execute("text.thread", &json!({"from": a, "rect": [72, 300, 300, 700], "spread": parent, "autoflow": true})).unwrap_err();
|
||||
assert!(e.to_string().contains("text.thread"), "{e}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
assert_eq!(*s.doc().unwrap().doc, *doc);
|
||||
assert_eq!(s.doc().unwrap().doc.page_count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unthread_after_and_before() {
|
||||
let mut s = session();
|
||||
let text = LONG.repeat(12);
|
||||
let (a, sa) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], &text);
|
||||
let (b, _) = text_frame(&mut s, [72.0, 200.0, 200.0, 250.0], "");
|
||||
let (c, _) = text_frame(&mut s, [72.0, 300.0, 200.0, 350.0], "");
|
||||
s.execute("text.thread", &json!({"from": a, "to": b})).unwrap();
|
||||
s.execute("text.thread", &json!({"from": b, "to": c})).unwrap();
|
||||
// Break after the first frame: b and c stay threaded to each other, empty.
|
||||
let r = s.execute("text.unthread", &json!({"frame": a})).unwrap();
|
||||
assert_eq!(r["story"].as_u64(), Some(sa));
|
||||
assert_eq!(r["overset"], true);
|
||||
assert_eq!(frames(&mut s, a), [a]);
|
||||
assert_eq!(story(&mut s, a)["text"], text);
|
||||
assert_eq!(frames(&mut s, b), [b, c]);
|
||||
assert_eq!(story(&mut s, b)["text"], "");
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, b, c]);
|
||||
// Break before the last frame, with the frame selected.
|
||||
s.execute("selection.set", &json!({"ids": [c]})).unwrap();
|
||||
s.execute("text.unthread", &json!({"side": "before"})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, b]);
|
||||
assert_eq!(frames(&mut s, c), [c]);
|
||||
// Nothing to break.
|
||||
assert!(s.execute("text.unthread", &json!({"frame": b})).is_err());
|
||||
assert!(s.execute("text.unthread", &json!({"frame": a, "side": "before"})).is_err());
|
||||
assert!(s.execute("text.unthread", &json!({"frame": a, "side": "sideways"})).is_err());
|
||||
assert!(s.execute("text.unthread", &json!({"frame": 999_999})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_or_removing_a_threaded_frame_keeps_the_text_flowing() {
|
||||
let mut s = session();
|
||||
let text = LONG.repeat(12);
|
||||
let (a, _) = text_frame(&mut s, [72.0, 72.0, 200.0, 120.0], &text);
|
||||
let (b, _) = text_frame(&mut s, [72.0, 200.0, 200.0, 250.0], "");
|
||||
let (c, _) = text_frame(&mut s, [72.0, 300.0, 200.0, 350.0], "");
|
||||
s.execute("text.thread", &json!({"from": a, "to": b})).unwrap();
|
||||
s.execute("text.thread", &json!({"from": b, "to": c})).unwrap();
|
||||
s.execute("text.removeFromThread", &json!({"frame": b})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, c]);
|
||||
assert_eq!(story(&mut s, a)["text"], text);
|
||||
assert_eq!(story(&mut s, b)["text"], "");
|
||||
assert!(s.execute("text.removeFromThread", &json!({"frame": b})).is_err());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.clear", &json!({"ids": [b]})).unwrap();
|
||||
assert_eq!(frames(&mut s, a), [a, c]);
|
||||
assert_eq!(story(&mut s, a)["text"], text);
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
}
|
||||
+199
-21
@@ -1,9 +1,9 @@
|
||||
//! Layout → Table of Contents: list the paragraphs of chosen styles with their page names, as a
|
||||
//! story with "TOC Title" / "TOC Level n" styles (right tab with dot leader). Update Table of
|
||||
//! Contents regenerates the same story after edits.
|
||||
//! story with "TOC Title" / "TOC Level n" styles (right tab with dot leader) or the entry styles
|
||||
//! chosen per style. Update Table of Contents regenerates the same story after edits.
|
||||
|
||||
use designcraft_compose::{ComposeOptions, caret, compose_story};
|
||||
use designcraft_doc::{Align, Document, ParaAttrs, ParaFormat, ParagraphStyle, SpreadRef, StoryId, TabAlign, TabStop, Toc, TocEntry};
|
||||
use designcraft_doc::{Align, Document, ParaAttrs, ParaFormat, ParagraphStyle, SpreadRef, StoryId, TabAlign, TabStop, Toc, TocEntry, TocPageNumber};
|
||||
use designcraft_geom::Rect;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -17,7 +17,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Table of Contents…",
|
||||
["Layout"],
|
||||
None,
|
||||
"{entries: [{style, level?: 1}] | styles: [names], title?: \"Contents\", pageNumbers?: true, page?: 1-based page for a new frame (its margins), rect?: [x0,y0,x1,y1] (spread coords)} → {story, entries}",
|
||||
"{entries: [{style, level?: 1 (1-9), entryStyle?: paragraph style of its entries (default \"TOC Level n\"), pageNumber?: \"after\"|\"before\"|\"none\", separator?: text between entry and number (default a tab)}] | styles: [names] (levels 1, 2, …), title?: \"Contents\", titleStyle?: paragraph style of the title (default \"TOC Title\"), pageNumbers?: true, replace?: true (regenerate the existing table of contents in place; false makes a new frame), page?: 1-based page for a new frame (its margins), rect?: [x0,y0,x1,y1] (spread coords)} → {story, entries}",
|
||||
has_doc,
|
||||
generate
|
||||
),
|
||||
@@ -44,20 +44,66 @@ fn has_toc(s: &Session) -> std::result::Result<(), String> {
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_ENTRIES: usize = 64;
|
||||
const MAX_NAME: usize = 256;
|
||||
const MAX_TITLE: usize = 1024;
|
||||
const MAX_SEPARATOR: usize = 64;
|
||||
|
||||
fn entries_param(p: &Value) -> Result<Vec<TocEntry>> {
|
||||
if let Some(v) = p.get("entries") {
|
||||
return serde_json::from_value(v.clone()).map_err(|e| bad("toc.generate", e.to_string()));
|
||||
let mut entries: Vec<TocEntry> = if let Some(v) = p.get("entries") {
|
||||
if v.as_array().is_some_and(|a| a.len() > MAX_ENTRIES) {
|
||||
return Err(bad("toc.generate", format!("at most {MAX_ENTRIES} entries")));
|
||||
}
|
||||
serde_json::from_value(v.clone()).map_err(|e| bad("toc.generate", e.to_string()))?
|
||||
} else if let Some(a) = p.get("styles").and_then(Value::as_array) {
|
||||
if a.len() > MAX_ENTRIES {
|
||||
return Err(bad("toc.generate", format!("at most {MAX_ENTRIES} styles")));
|
||||
}
|
||||
a.iter().enumerate().filter_map(|(i, v)| v.as_str().map(|s| TocEntry::new(s, (i + 1).min(9) as u8))).collect()
|
||||
} else {
|
||||
return Err(bad("toc.generate", "give `entries` or `styles`"));
|
||||
};
|
||||
for e in &mut entries {
|
||||
if !(1..=9).contains(&e.level) {
|
||||
return Err(bad("toc.generate", format!("level {} of `{}` is not 1–9", e.level, e.style)));
|
||||
}
|
||||
if e.style.chars().count() > MAX_NAME || e.entry_style.as_ref().is_some_and(|n| n.chars().count() > MAX_NAME) {
|
||||
return Err(bad("toc.generate", format!("style names are at most {MAX_NAME} characters")));
|
||||
}
|
||||
if let Some(sep) = &mut e.separator {
|
||||
if sep.chars().count() > MAX_SEPARATOR {
|
||||
return Err(bad("toc.generate", format!("the separator is at most {MAX_SEPARATOR} characters")));
|
||||
}
|
||||
*sep = one_line(sep);
|
||||
}
|
||||
// The defaults are stored as `None`, so a definition reads the same however it was made.
|
||||
if e.entry_style.as_deref() == Some("") {
|
||||
e.entry_style = None;
|
||||
}
|
||||
if e.separator.as_deref() == Some("\t") {
|
||||
e.separator = None;
|
||||
}
|
||||
}
|
||||
if let Some(a) = p.get("styles").and_then(Value::as_array) {
|
||||
return Ok(a.iter().enumerate().filter_map(|(i, v)| v.as_str().map(|s| TocEntry { style: s.into(), level: (i + 1).min(9) as u8 })).collect());
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
/// Paragraph breaks in a title or separator would shift the paragraph styles after them.
|
||||
fn one_line(s: &str) -> String {
|
||||
s.chars().map(|c| if matches!(c, '\n' | '\r' | '\u{2029}') { ' ' } else { c }).collect()
|
||||
}
|
||||
|
||||
fn para_style_exists(d: &Document, name: &str) -> Result<()> {
|
||||
match d.styles.para(name) {
|
||||
Some(_) => Ok(()),
|
||||
None => Err(bad("toc.generate", format!("no paragraph style `{name}`"))),
|
||||
}
|
||||
Err(bad("toc.generate", "give `entries` or `styles`"))
|
||||
}
|
||||
|
||||
pub struct Found {
|
||||
pub level: u8,
|
||||
pub text: String,
|
||||
pub page: String,
|
||||
entry: usize,
|
||||
key: (usize, i64, i64),
|
||||
}
|
||||
|
||||
@@ -70,7 +116,7 @@ pub fn collect(d: &Document, entries: &[TocEntry], skip: Option<StoryId>) -> Vec
|
||||
}
|
||||
let cs = compose_story(d, *sid, &ComposeOptions::default());
|
||||
for (pi, r) in st.para_ranges().into_iter().enumerate() {
|
||||
let Some(e) = entries.iter().find(|e| e.style == st.paras[pi].style) else { continue };
|
||||
let Some((ei, e)) = entries.iter().enumerate().find(|(_, e)| e.style == st.paras[pi].style) else { continue };
|
||||
let text: String = st.text[r.clone()]
|
||||
.chars()
|
||||
.filter(|c| !('\u{E000}'..='\u{E1FF}').contains(c))
|
||||
@@ -95,6 +141,7 @@ pub fn collect(d: &Document, entries: &[TocEntry], skip: Option<StoryId>) -> Vec
|
||||
out.push(Found {
|
||||
level: e.level,
|
||||
text,
|
||||
entry: ei,
|
||||
page: d.page_name(abs),
|
||||
key: (abs, ((b.y0 + baseline) * 10.0) as i64, ((b.x0 + x) * 10.0) as i64),
|
||||
});
|
||||
@@ -146,22 +193,35 @@ fn fill(d: &mut Document, toc: &Toc, width: Option<f64>) -> Result<usize> {
|
||||
d.story(toc.story).and_then(|st| st.frames.first()).and_then(|f| d.item(*f)).map(|i| i.text_area().width()).unwrap_or(300.0)
|
||||
});
|
||||
ensure_styles(d, levels, width);
|
||||
for name in toc.entries.iter().filter_map(|e| e.entry_style.as_deref()).chain(toc.title_style.as_deref()) {
|
||||
para_style_exists(d, name)?;
|
||||
}
|
||||
let mut text = String::new();
|
||||
let mut styles = Vec::new();
|
||||
if !toc.title.is_empty() {
|
||||
text.push_str(&toc.title);
|
||||
styles.push("TOC Title".to_string());
|
||||
styles.push(toc.title_style.clone().unwrap_or_else(|| "TOC Title".into()));
|
||||
}
|
||||
for f in &found {
|
||||
let Some(e) = toc.entries.get(f.entry) else { continue };
|
||||
if !text.is_empty() {
|
||||
text.push('\n');
|
||||
}
|
||||
text.push_str(&f.text);
|
||||
if toc.page_numbers {
|
||||
text.push('\t');
|
||||
text.push_str(&f.page);
|
||||
let sep = e.separator.as_deref().unwrap_or("\t");
|
||||
match e.page_number {
|
||||
TocPageNumber::After if toc.page_numbers => {
|
||||
text.push_str(&f.text);
|
||||
text.push_str(sep);
|
||||
text.push_str(&f.page);
|
||||
}
|
||||
TocPageNumber::Before if toc.page_numbers => {
|
||||
text.push_str(&f.page);
|
||||
text.push_str(sep);
|
||||
text.push_str(&f.text);
|
||||
}
|
||||
_ => text.push_str(&f.text),
|
||||
}
|
||||
styles.push(format!("TOC Level {}", f.level.max(1)));
|
||||
styles.push(e.entry_style.clone().unwrap_or_else(|| format!("TOC Level {}", f.level.max(1))));
|
||||
}
|
||||
let st = d.story_mut(toc.story).ok_or(designcraft_doc::DocError::NoStory(toc.story))?;
|
||||
let len = st.len();
|
||||
@@ -179,13 +239,29 @@ fn generate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if entries.is_empty() {
|
||||
return Err(bad("toc.generate", "no entries"));
|
||||
}
|
||||
let title = str_param(p, "title").unwrap_or("Contents").to_string();
|
||||
let title = str_param(p, "title").unwrap_or("Contents");
|
||||
if title.chars().count() > MAX_TITLE {
|
||||
return Err(bad("toc.generate", format!("the title is at most {MAX_TITLE} characters")));
|
||||
}
|
||||
let title = one_line(title);
|
||||
let title_style = str_param(p, "titleStyle").filter(|n| !n.is_empty()).map(str::to_string);
|
||||
if title_style.as_ref().is_some_and(|n| n.chars().count() > MAX_NAME) {
|
||||
return Err(bad("toc.generate", format!("style names are at most {MAX_NAME} characters")));
|
||||
}
|
||||
let page_numbers = p.get("pageNumbers").and_then(Value::as_bool).unwrap_or(true);
|
||||
let replace = p.get("replace").and_then(Value::as_bool).unwrap_or(true);
|
||||
let rect = super::rect_param(p, "rect");
|
||||
let page = p.get("page").and_then(Value::as_u64).unwrap_or(1).max(1) as usize - 1;
|
||||
if rect.is_some_and(|r| ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) || r.width() <= 0.0 || r.height() <= 0.0) {
|
||||
return Err(bad("toc.generate", "`rect` must be finite with a positive size"));
|
||||
}
|
||||
let page = p.get("page").and_then(Value::as_u64).map_or(Some(0), |n| usize::try_from(n.max(1) - 1).ok());
|
||||
let page = page.ok_or_else(|| bad("toc.generate", "no such page"))?;
|
||||
s.edit(|d, sel| {
|
||||
// Reuse the existing TOC story when there is one; otherwise make a frame.
|
||||
let existing = d.toc.as_ref().map(|t| t.story).filter(|sid| d.story(*sid).is_some());
|
||||
for e in &entries {
|
||||
para_style_exists(d, &e.style)?;
|
||||
}
|
||||
// Replace: regenerate the existing TOC story in place; otherwise make a frame.
|
||||
let existing = d.toc.as_ref().map(|t| t.story).filter(|sid| replace && d.story(*sid).is_some());
|
||||
let (story, width) = match existing {
|
||||
Some(sid) => (sid, None),
|
||||
None => {
|
||||
@@ -198,7 +274,7 @@ fn generate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
(sid, w)
|
||||
}
|
||||
};
|
||||
let toc = Toc { story, title: title.clone(), entries: entries.clone(), page_numbers };
|
||||
let toc = Toc { story, title: title.clone(), entries: entries.clone(), page_numbers, title_style: title_style.clone() };
|
||||
let n = fill(d, &toc, width)?;
|
||||
d.toc = Some(toc);
|
||||
Ok(json!({"story": story.0, "entries": n}))
|
||||
@@ -237,4 +313,106 @@ mod tests {
|
||||
assert!(text.contains("Thoughts on the Grid"), "{text}");
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
fn sample_with_toc_page() -> (Session, usize) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.newSample", &json!({})).unwrap();
|
||||
let pages = s.doc().unwrap().doc.page_count();
|
||||
s.execute("layout.pages.insert", &json!({"count": 1})).unwrap();
|
||||
(s, pages + 1)
|
||||
}
|
||||
|
||||
fn toc_lines(s: &Session) -> Vec<(String, String)> {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let st = d.story(d.toc.as_ref().unwrap().story).unwrap();
|
||||
st.para_ranges().into_iter().zip(&st.paras).map(|(r, f)| (st.text[r].trim_end_matches('\n').to_string(), f.style.clone())).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn toc_entry_style_page_number_position_and_separator_survive_update() {
|
||||
let (mut s, page) = sample_with_toc_page();
|
||||
let basic = designcraft_doc::story::BASIC_PARAGRAPH;
|
||||
s.execute(
|
||||
"toc.generate",
|
||||
&json!({"page": page, "title": "In This Issue", "titleStyle": basic, "entries": [
|
||||
{"style": "Cover Title", "level": 1, "pageNumber": "before", "separator": " — "},
|
||||
{"style": "Headline", "level": 2, "entryStyle": basic, "pageNumber": "none"},
|
||||
]}),
|
||||
)
|
||||
.unwrap();
|
||||
let check = |s: &Session, headline: &str| {
|
||||
let lines = toc_lines(s);
|
||||
assert_eq!(lines[0], ("In This Issue".to_string(), basic.to_string()));
|
||||
let cover = lines.iter().find(|(t, _)| t.contains("The Quiet")).unwrap();
|
||||
assert!(cover.0.starts_with("1 — The Quiet"), "{cover:?}");
|
||||
assert_eq!(cover.1, "TOC Level 1");
|
||||
let head = lines.iter().find(|(t, _)| t.contains("on the Grid")).unwrap();
|
||||
assert!(head.0.starts_with(headline) && !head.0.contains('\t'), "{head:?}");
|
||||
assert_eq!(head.1, basic);
|
||||
};
|
||||
check(&s, "Notes on the Grid");
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let hid = d.stories.iter().find(|(_, st)| st.text.starts_with("Notes on the Grid")).map(|(id, _)| *id).unwrap();
|
||||
s.execute("story.replaceRange", &json!({"story": hid.0, "start": 0, "end": 5, "text": "Thoughts"})).unwrap();
|
||||
s.execute("toc.update", &json!({})).unwrap();
|
||||
check(&s, "Thoughts on the Grid");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_regenerates_the_toc_in_place_and_off_makes_a_new_frame() {
|
||||
let (mut s, page) = sample_with_toc_page();
|
||||
let stories = |s: &Session| s.doc().unwrap().doc.stories.len();
|
||||
let first = s.execute("toc.generate", &json!({"styles": ["Headline"], "page": page})).unwrap()["story"].clone();
|
||||
let n = stories(&s);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let r = s.execute("toc.generate", &json!({"styles": ["Headline"], "title": "Inside", "page": page, "replace": true})).unwrap();
|
||||
assert_eq!(r["story"], first);
|
||||
assert_eq!(stories(&s), n, "one table of contents");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "one undo step");
|
||||
assert_eq!(toc_lines(&s)[0].0, "Inside");
|
||||
let r = s.execute("toc.generate", &json!({"styles": ["Headline"], "page": page, "replace": false})).unwrap();
|
||||
assert_ne!(r["story"], first);
|
||||
assert_eq!(stories(&s), n + 1);
|
||||
assert_eq!(s.doc().unwrap().doc.toc.as_ref().unwrap().story.0, r["story"].as_u64().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_toc_params_are_errors_that_leave_the_document_alone() {
|
||||
let (mut s, page) = sample_with_toc_page();
|
||||
let long = "x".repeat(5000);
|
||||
let many: Vec<Value> = (0..65).map(|_| json!({"style": "Headline"})).collect();
|
||||
for p in [
|
||||
json!({"entries": [{"style": "Headline", "level": 0}]}),
|
||||
json!({"entries": [{"style": "Headline", "level": 10}]}),
|
||||
json!({"entries": [{"style": "Headline", "level": 300}]}),
|
||||
json!({"entries": [{"style": "Headline", "level": -1}]}),
|
||||
json!({"entries": [{"style": "Headline", "pageNumber": "sideways"}]}),
|
||||
json!({"entries": [{"style": "No Such Style"}]}),
|
||||
json!({"entries": [{"style": "Headline", "entryStyle": "No Such Style"}]}),
|
||||
json!({"entries": [{"style": "Headline", "separator": long}]}),
|
||||
json!({"entries": [{"style": long}]}),
|
||||
json!({"entries": many}),
|
||||
json!({"entries": "Headline"}),
|
||||
json!({"entries": []}),
|
||||
json!({"styles": ["Headline"], "titleStyle": "No Such Style"}),
|
||||
json!({"styles": ["Headline"], "title": long}),
|
||||
json!({"styles": ["Headline"], "rect": [100, 100, 50, 50]}),
|
||||
json!({"styles": ["Headline"], "page": u64::MAX}),
|
||||
json!([1, 2]),
|
||||
] {
|
||||
let rev = s.doc().unwrap().revision;
|
||||
assert!(s.execute("toc.generate", &merge(&p, page)).is_err(), "{p}");
|
||||
assert_eq!(s.doc().unwrap().revision, rev, "{p}");
|
||||
assert!(s.doc().unwrap().doc.toc.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
/// `p` with the TOC page added when it is an object and has none.
|
||||
fn merge(p: &Value, page: usize) -> Value {
|
||||
let mut p = p.clone();
|
||||
if let Some(o) = p.as_object_mut() {
|
||||
o.entry("page").or_insert(json!(page));
|
||||
}
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,63 @@ fn define(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A parent-page page-number marker follows Numbering & Section Options: changing a section's
|
||||
/// start number renames its pages without changing their index or the number of sections, and
|
||||
/// the marker composed for a page shows the new name.
|
||||
#[test]
|
||||
fn parent_page_number_follows_section_renumbering() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 3, "facingPages": false})).unwrap();
|
||||
let marker = designcraft_doc::story::PAGE_NUMBER.to_string();
|
||||
let r = s
|
||||
.execute("frame.create", &json!({"spread": {"kind": "parent", "index": 0}, "rect": [72, 30, 500, 60], "content": "text", "text": marker}))
|
||||
.unwrap();
|
||||
let sid = s.doc().unwrap().doc.item(designcraft_doc::ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story;
|
||||
assert_eq!(s.doc().unwrap().doc.story(sid).unwrap().text, marker);
|
||||
let shown = |s: &Session, page: usize| -> usize {
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let cs = s.cache.get(&d, sid, Some(page));
|
||||
cs.frames.iter().flat_map(|f| f.lines.iter()).map(|l| l.glyphs.iter().filter(|g| g.visible && g.adv > 0.0).count()).sum()
|
||||
};
|
||||
assert_eq!(shown(&s, 2), 1, "page 3");
|
||||
s.execute("layout.section", &json!({"page": 1, "startNumber": 100})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.page_name(2), "102");
|
||||
assert_eq!(shown(&s, 2), 3, "page 102 after renumbering");
|
||||
}
|
||||
|
||||
/// Two sections that both number from 1 repeat page names ("1, 2, 1, 2"). A parent-page running
|
||||
/// header is composed for the page it is shown on, not the first page with that name.
|
||||
#[test]
|
||||
fn running_header_on_parent_page_with_repeated_page_numbers() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 4, "facingPages": false})).unwrap();
|
||||
s.execute("layout.section", &json!({"page": 3, "startNumber": 1})).unwrap();
|
||||
let names: Vec<String> = (0..4).map(|i| s.doc().unwrap().doc.page_name(i)).collect();
|
||||
assert_eq!(names, ["1", "2", "1", "2"]);
|
||||
s.execute("style.paragraph.create", &json!({"name": "Heading"})).ok();
|
||||
// The only heading is on the third page (the second section's page 1).
|
||||
let sp = s.doc().unwrap().doc.page_loc(2).unwrap().0;
|
||||
let r = s.execute("frame.create", &json!({"spread": sp, "rect": [72, 200, 400, 260], "content": "text", "text": "Epsilon"})).unwrap();
|
||||
let sid = s.doc().unwrap().doc.item(designcraft_doc::ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story;
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 0, "focus": 1})).unwrap();
|
||||
s.execute("style.paragraph.apply", &json!({"name": "Heading"})).unwrap();
|
||||
s.execute("variables.define", &json!({"name": "Chapter Head", "type": "runningHeader", "style": "Heading"})).unwrap();
|
||||
let r = s
|
||||
.execute("frame.create", &json!({"spread": {"kind": "parent", "index": 0}, "rect": [72, 30, 500, 60], "content": "text", "text": ""}))
|
||||
.unwrap();
|
||||
let sid = s.doc().unwrap().doc.item(designcraft_doc::ItemId(r["id"].as_u64().unwrap())).unwrap().text_frame().unwrap().story;
|
||||
s.execute("text.select", &json!({"story": sid.0, "anchor": 0, "focus": 0})).unwrap();
|
||||
s.execute("variables.insert", &json!({"name": "Chapter Head"})).unwrap();
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let shown = |page: usize| -> usize {
|
||||
let cs = s.cache.get(&d, sid, Some(page));
|
||||
cs.frames.iter().flat_map(|f| f.lines.iter()).map(|l| l.glyphs.iter().filter(|g| g.visible && g.adv > 0.0).count()).sum()
|
||||
};
|
||||
assert_eq!(shown(0), 0, "no heading on or before the first page");
|
||||
assert_eq!(shown(2), "Epsilon".len(), "the third page (named 1) shows its own heading");
|
||||
assert_eq!(shown(3), "Epsilon".len(), "the fourth page (named 2) carries it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn running_header_on_parent_page_follows_headings() {
|
||||
let mut s = Session::new();
|
||||
@@ -158,7 +215,7 @@ mod tests {
|
||||
assert!(matches!(d.find(fid).unwrap().spread, designcraft_doc::SpreadRef::Parent(0)));
|
||||
// Visible glyphs of the header composed for each page: "Alpha · 3" vs "Epsilon · 3".
|
||||
let shown = |page: usize| -> usize {
|
||||
let cs = s.cache.get(&d, sid, Some(&d.page_name(page)));
|
||||
let cs = s.cache.get(&d, sid, Some(page));
|
||||
cs.frames.iter().flat_map(|f| f.lines.iter()).map(|l| l.glyphs.iter().filter(|g| g.visible && g.adv > 0.0).count()).sum()
|
||||
};
|
||||
let (p1, p2, p3) = (shown(0), shown(1), shown(2));
|
||||
|
||||
@@ -305,7 +305,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
match str_param(p, "path") {
|
||||
Some(path) => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
std::fs::write(path, text.as_bytes()).map_err(|e| crate::EngineError::Other(format!("{path}: {e}")))?;
|
||||
crate::atomic::write_file(path, text.as_bytes())?;
|
||||
Ok(json!({"path": path, "bytes": text.len()}))
|
||||
}
|
||||
None => Ok(json!({"text": text, "bytes": text.len()})),
|
||||
|
||||
@@ -214,4 +214,23 @@ mod tests {
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.execute("xref.list", &json!({})).unwrap()[0]["text"], "(p. 3)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cross_reference_into_a_story_with_a_running_header_composes() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages": 1, "facingPages": false})).unwrap();
|
||||
let head = s.execute("frame.create", &json!({"rect": [72, 72, 400, 120], "content": "text", "text": "Chapter"})).unwrap()["story"].clone();
|
||||
let body = s.execute("frame.create", &json!({"rect": [72, 200, 400, 300], "content": "text", "text": "See"})).unwrap()["story"].clone();
|
||||
s.execute("text.select", &json!({"story": head, "anchor": 0, "focus": 0})).unwrap();
|
||||
s.execute("variables.insert", &json!({"name": "Running Header"})).unwrap();
|
||||
s.execute("text.select", &json!({"story": body, "anchor": 3, "focus": 3})).unwrap();
|
||||
s.execute("xref.insert", &json!({"story": head, "para": 0})).unwrap();
|
||||
// The cross-reference index and the running-header index used to build each other without end.
|
||||
assert_eq!(s.execute("xref.list", &json!({})).unwrap()[0]["text"], "\u{201C}Chapter\u{201D} on page 1");
|
||||
// The header shows the body's paragraph in front of its own text.
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let cs = s.cache.get(&d, StoryId(head.as_u64().unwrap()), None);
|
||||
let glyphs: usize = cs.frames[0].lines.iter().map(|l| l.glyphs.iter().filter(|g| g.visible && g.adv > 0.0).count()).sum();
|
||||
assert_eq!(glyphs, "SeeChapter".len());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub mod atomic;
|
||||
pub mod cmd;
|
||||
pub mod dtd;
|
||||
pub mod guard;
|
||||
@@ -91,6 +93,9 @@ pub struct DocState {
|
||||
/// Editing parent spreads (Pages panel double-click on a parent).
|
||||
pub editing_parents: bool,
|
||||
pub uid: u64,
|
||||
/// Recovery entries this state actually opened or published, for repeated-open and cleanup.
|
||||
/// Bounded, deduplicated checkpoint addresses; never inferred from uid alone.
|
||||
pub(crate) recovery_entries: Vec<(std::path::PathBuf, u64)>,
|
||||
/// Unfilled document while data-merge preview is showing a record. Not saved.
|
||||
pub preview_stash: Option<Arc<Document>>,
|
||||
/// 1-based record currently previewed.
|
||||
@@ -129,6 +134,7 @@ impl DocState {
|
||||
interaction: None,
|
||||
editing_parents: false,
|
||||
uid: NEXT_UID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
|
||||
recovery_entries: Vec::new(),
|
||||
preview_stash: None,
|
||||
preview_record: None,
|
||||
fonts: None,
|
||||
@@ -177,6 +183,8 @@ pub struct Prefs {
|
||||
pub rich_black_output: bool,
|
||||
/// Font menu favourites (family names).
|
||||
pub favorite_fonts: Vec<String>,
|
||||
/// Fonts picked in the font menus, most recent first (shown at the menus' top).
|
||||
pub recent_fonts: Vec<String>,
|
||||
/// Preferences › Type › Show Font Names in English: the font menus show CJK families by their
|
||||
/// English names instead of their native ones. Documents store the English name either way.
|
||||
pub show_font_names_in_english: bool,
|
||||
@@ -233,6 +241,7 @@ impl Default for Prefs {
|
||||
highlight_substituted_fonts: true,
|
||||
rich_black_output: false,
|
||||
favorite_fonts: Vec::new(),
|
||||
recent_fonts: Vec::new(),
|
||||
show_font_names_in_english: false,
|
||||
smart_text_reflow: true,
|
||||
autocorrect: false,
|
||||
@@ -251,6 +260,7 @@ pub struct Session {
|
||||
pub cache: Arc<Cache>,
|
||||
pub(crate) tool: Box<dyn designcraft_tools::Tool>,
|
||||
pub journal: Vec<(String, Value)>,
|
||||
pub(crate) find_context: Option<cmd::find::FindContext>,
|
||||
pub clipboard: Option<Arc<Document>>,
|
||||
/// Crash-recovery folder (the app sets it; saving or closing a document removes its entry).
|
||||
pub recovery_dir: Option<std::path::PathBuf>,
|
||||
@@ -267,6 +277,8 @@ pub struct Session {
|
||||
pub(crate) transforms: (Vec<(String, Value)>, Vec<designcraft_doc::ItemId>, bool),
|
||||
/// Drag and drop text editing: the press landed in the selected text.
|
||||
pub(crate) text_drag: bool,
|
||||
/// Character formatting chosen at a bare caret, for the text typed there next.
|
||||
pub(crate) typing_format: Option<cmd::text::TypingFormat>,
|
||||
/// The open Object Library (File › New / Open Library).
|
||||
pub library: Option<cmd::library::Library>,
|
||||
/// The open book (File › Open Book).
|
||||
@@ -291,6 +303,7 @@ impl Session {
|
||||
cache: Arc::new(Cache::new()),
|
||||
tool: designcraft_tools::create("selection"),
|
||||
journal: vec![],
|
||||
find_context: None,
|
||||
clipboard: None,
|
||||
recovery_dir: None,
|
||||
text_clipboard: None,
|
||||
@@ -298,6 +311,7 @@ impl Session {
|
||||
loaded: None,
|
||||
transforms: Default::default(),
|
||||
text_drag: false,
|
||||
typing_format: None,
|
||||
library: None,
|
||||
conveyor: Vec::new(),
|
||||
book: None,
|
||||
@@ -349,13 +363,26 @@ impl Session {
|
||||
pub fn doc_mut(&mut self) -> Result<&mut DocState> {
|
||||
self.active_mut().ok_or(EngineError::NoDocument)
|
||||
}
|
||||
/// Release the transient Find/Change domain when its caller starts or closes a search.
|
||||
pub fn reset_find_scope(&mut self) {
|
||||
self.find_context = None;
|
||||
}
|
||||
|
||||
fn discard_stale_find_scope(&mut self) {
|
||||
if self.find_context.as_ref().is_some_and(|c| self.active().is_none_or(|st| !c.is_current(st))) {
|
||||
self.reset_find_scope();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_active(&mut self, i: usize) {
|
||||
if i < self.docs.len() && self.active != Some(i) {
|
||||
self.reset_find_scope();
|
||||
self.stop_data_preview();
|
||||
self.active = Some(i);
|
||||
}
|
||||
}
|
||||
pub fn add_document(&mut self, d: DocState) -> usize {
|
||||
self.reset_find_scope();
|
||||
self.stop_data_preview();
|
||||
self.docs.push(d);
|
||||
self.active = Some(self.docs.len() - 1);
|
||||
@@ -374,10 +401,12 @@ impl Session {
|
||||
self.stop_data_preview();
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let (Some(dir), Some(d)) = (&self.recovery_dir, self.docs.get(i)) {
|
||||
recovery::discard(dir, d.uid);
|
||||
{
|
||||
let recovery_dir = self.recovery_dir.clone();
|
||||
recovery::discard_document(self, i, recovery_dir.as_deref());
|
||||
}
|
||||
if i < self.docs.len() {
|
||||
self.reset_find_scope();
|
||||
self.docs.remove(i);
|
||||
self.active = if self.docs.is_empty() { None } else { Some(i.min(self.docs.len() - 1)) };
|
||||
}
|
||||
@@ -408,6 +437,8 @@ impl Session {
|
||||
if id != "data.preview" && (spec.undoable || spec.journal) {
|
||||
self.stop_data_preview();
|
||||
}
|
||||
// Preview restoration can replace the document before a Find handler resolves bounds.
|
||||
self.discard_stale_find_scope();
|
||||
let before = self.active().map(|d| (d.uid, d.doc.clone()));
|
||||
let r = (spec.run)(self, params)?;
|
||||
// Undo, redo or deleting layers can take the active layer away: new objects would land on
|
||||
@@ -445,6 +476,7 @@ impl Session {
|
||||
if spec.journal {
|
||||
self.journal.push((id.to_string(), params.clone()));
|
||||
}
|
||||
self.discard_stale_find_scope();
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
@@ -465,7 +497,7 @@ impl Session {
|
||||
}
|
||||
let mut d = (*st.doc).clone();
|
||||
if overset {
|
||||
let _ = cmd::place_text_autoflow(&mut d, sid, 500);
|
||||
let _ = cmd::place_text_autoflow(&mut d, sid, 500, "Smart Text Reflow");
|
||||
} else {
|
||||
// Drop empty trailing frames on the last pages (one frame, nothing else there).
|
||||
while let Some(story) = d.story(sid) {
|
||||
@@ -532,6 +564,7 @@ impl Session {
|
||||
st.doc = Arc::new(doc);
|
||||
st.selection = sel;
|
||||
st.revision += 1;
|
||||
self.reset_find_scope();
|
||||
Ok(r)
|
||||
}
|
||||
}
|
||||
|
||||
+1162
-35
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,8 @@
|
||||
//!
|
||||
//! **Script text** ([`parse`]): a JSON array of steps, JSON lines (`{"command": …, "params": …}`),
|
||||
//! or compact lines `command.id {json}` / `command.id={json}` / `command.id`; blank lines and
|
||||
//! lines starting with `#` or `//` are ignored.
|
||||
//! lines starting with `#` or `//` are ignored. A leading byte order mark and CRLF line endings are
|
||||
//! accepted.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -22,7 +23,9 @@ pub struct Step {
|
||||
|
||||
/// Parse script text (see the module docs).
|
||||
pub fn parse(text: &str) -> Result<Vec<Step>, String> {
|
||||
let t = text.trim();
|
||||
// Windows tools save "UTF-8 with BOM"; `trim` keeps U+FEFF, which then became part of the
|
||||
// first command id.
|
||||
let t = text.trim_start_matches('\u{feff}').trim();
|
||||
if t.starts_with('[') {
|
||||
let v: Value = serde_json::from_str(t).map_err(|e| format!("script: {e}"))?;
|
||||
return v
|
||||
@@ -174,6 +177,17 @@ mod tests {
|
||||
assert!(parse("frame.create {bad").unwrap_err().contains("line 1"));
|
||||
}
|
||||
|
||||
/// Windows editors and PowerShell 5.1 save "UTF-8 with BOM" with CRLF line endings; the BOM
|
||||
/// became part of the first command id (#654).
|
||||
#[test]
|
||||
fn a_leading_bom_and_crlf_parse_like_plain_text() {
|
||||
let plain = "file.new {\"width\": 100}\nframe.create {\"rect\": [0, 0, 10, 10]}\n";
|
||||
let windows = format!("\u{feff}{}", plain.replace('\n', "\r\n"));
|
||||
assert_eq!(parse(&windows).unwrap(), parse(plain).unwrap());
|
||||
let array = r#"[{"command": "file.new"}]"#;
|
||||
assert_eq!(parse(&format!("\u{feff}{array}")).unwrap(), parse(array).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_references() {
|
||||
let results = vec![json!({"id": 8, "story": 9, "ids": [4, 5]}), json!({"name": "A"})];
|
||||
|
||||
+317
-1
@@ -284,7 +284,15 @@ fn place_gun_click_drag_and_into_frame() {
|
||||
let f = s.execute("frame.create", &json!({"rect": [300, 300, 400, 400], "content": "graphic"})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("place.load", &json!({"base64": b64})).unwrap();
|
||||
s.execute("place.drop", &json!({"frame": f})).unwrap();
|
||||
assert!(matches!(s.doc().unwrap().doc.item(designcraft_doc::ItemId(f)).unwrap().content, designcraft_doc::Content::Graphic(_)));
|
||||
let graphic_xf = |s: &Session| match &s.doc().unwrap().doc.item(designcraft_doc::ItemId(f)).unwrap().content {
|
||||
designcraft_doc::Content::Graphic(g) => g.xf,
|
||||
_ => panic!("not a graphic"),
|
||||
};
|
||||
let placed = graphic_xf(&s);
|
||||
// Dragging the top edge down crops the top only: the image stays where it is.
|
||||
s.execute("selection.set", &json!({"ids": [f]})).unwrap();
|
||||
s.execute("transform.resize", &json!({"from": [300, 300, 400, 400], "to": [300, 340, 400, 400]})).unwrap();
|
||||
assert_eq!(graphic_xf(&s), placed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -561,3 +569,311 @@ fn commands_take_non_object_params_without_panicking() {
|
||||
assert!(r.is_ok(), "{p}: {r:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_tool_selects_a_page_and_its_handles_preview_or_resize_it() {
|
||||
use designcraft_tools::{CanvasLayout, Mods, PointerEvent, PointerKind};
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 612, "height": 792, "facingPages": false})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [400, 100, 500, 200]})).unwrap()["id"].as_u64().unwrap();
|
||||
let frame = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds();
|
||||
let width = |s: &Session| s.doc().unwrap().doc.spreads[0].pages[0].width;
|
||||
// The page on the canvas (a single page is centred on the spine).
|
||||
let page = |s: &Session| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
CanvasLayout::new(d, false).page_rect(d, 0).unwrap()
|
||||
};
|
||||
s.set_tool("page");
|
||||
let v = ViewInfo::at_zoom(1.0);
|
||||
let ev = |kind, x: f64, y: f64, alt: bool| PointerEvent { mods: Mods { alt, ..Default::default() }, ..PointerEvent::new(kind, x, y) };
|
||||
let drag = |s: &mut Session, from: (f64, f64), by: f64, alt: bool, check: &dyn Fn(&Session)| {
|
||||
s.pointer(&ev(PointerKind::Down, from.0, from.1, alt), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Drag, from.0 + by, from.1, alt), v).unwrap();
|
||||
check(s);
|
||||
s.pointer(&ev(PointerKind::Up, from.0 + by, from.1, alt), v).unwrap();
|
||||
};
|
||||
// A click selects the page.
|
||||
let c = page(&s).center();
|
||||
s.pointer(&ev(PointerKind::Down, c.x, c.y, false), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Up, c.x, c.y, false), v).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.page, Some(0));
|
||||
// A plain drag of the right handle previews the page's liquid rule and springs back.
|
||||
s.execute("liquid.pageRule", &json!({"rule": "scale"})).unwrap();
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let r = page(&s);
|
||||
drag(&mut s, (r.x1, r.center().y), 612.0, false, &|s| {
|
||||
assert_eq!(width(s), 1224.0, "previewed");
|
||||
// Scale keeps proportions: only wider, so the frame is centred on the wider page.
|
||||
assert_eq!(frame(s).x0, 706.0, "the Scale rule in the preview: {:?}", frame(s));
|
||||
});
|
||||
assert_eq!((width(&s), frame(&s)), (612.0, designcraft_geom::Rect::new(400.0, 100.0, 500.0, 200.0)), "sprung back");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "nothing to undo");
|
||||
// Alt-drag resizes; with the rule off the frame stays where it is.
|
||||
s.execute("liquid.pageRule", &json!({"rule": "off"})).unwrap();
|
||||
let r = page(&s);
|
||||
drag(&mut s, (r.x1, r.center().y), 100.0, true, &|_| {});
|
||||
assert_eq!(width(&s), 712.0);
|
||||
assert_eq!(frame(&s).x0, 400.0);
|
||||
// The left handle grows the page to the left: the frame keeps its place by the right edge.
|
||||
let r = page(&s);
|
||||
drag(&mut s, (r.x0, r.center().y), -100.0, true, &|_| {});
|
||||
let pg = s.doc().unwrap().doc.spreads[0].pages[0].clone();
|
||||
assert_eq!(pg.width, 812.0);
|
||||
assert_eq!(frame(&s).x0 - pg.x, 500.0, "100 pt further from the page's left edge");
|
||||
// A click on the pasteboard deselects the page.
|
||||
let r = page(&s);
|
||||
s.pointer(&ev(PointerKind::Down, r.x0 - 200.0, r.center().y, false), v).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.page, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_size_keeps_objects_by_its_anchor() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400, "facingPages": false})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [100, 100, 200, 200]})).unwrap()["id"].as_u64().unwrap();
|
||||
let frame = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds();
|
||||
// Bottom-right stays put: 100 wider and 50 taller moves the frame 100 right and 50 down.
|
||||
s.execute("layout.pageSize", &json!({"pages": [1], "width": 500, "height": 450, "anchor": 8})).unwrap();
|
||||
assert_eq!(frame(&s), designcraft_geom::Rect::new(200.0, 150.0, 300.0, 250.0));
|
||||
// The centre: half of it.
|
||||
s.execute("layout.pageSize", &json!({"pages": [1], "width": 700, "height": 450, "anchor": 4})).unwrap();
|
||||
assert_eq!(frame(&s).x0, 300.0);
|
||||
assert!(s.execute("layout.pageSize", &json!({"pages": [1], "width": 700, "anchor": 9})).is_err());
|
||||
assert!(s.execute("layout.selectPage", &json!({"page": 2})).is_err());
|
||||
assert_eq!(s.execute("layout.selectPage", &json!({"page": 1})).unwrap()["page"], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn controlled_by_parent_takes_the_parent_pages_rule() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 400, "facingPages": false})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [100, 100, 200, 200]})).unwrap()["id"].as_u64().unwrap();
|
||||
let frame = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds();
|
||||
// The page follows its parent; the parent page re-centres.
|
||||
s.execute("liquid.pageRule", &json!({"rule": "controlledByParent", "pages": [1]})).unwrap();
|
||||
s.edit(|d, _| {
|
||||
std::sync::Arc::make_mut(&mut d.parents[0]).pages[0].liquid = designcraft_doc::LiquidRule::ReCenter;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("layout.pageSize", &json!({"pages": [1], "width": 600})).unwrap();
|
||||
assert_eq!(frame(&s).x0, 200.0, "re-centred: {:?}", frame(&s));
|
||||
// Without a parent (or a parent that's also controlled by its parent) nothing moves.
|
||||
s.execute("layout.pages.applyParent", &json!({"pages": [0], "parent": null})).unwrap();
|
||||
s.execute("layout.pageSize", &json!({"pages": [1], "width": 800})).unwrap();
|
||||
assert_eq!(frame(&s).x0, 200.0);
|
||||
// Show Parent Page Overlay is a page option.
|
||||
s.execute("layout.pageOptions", &json!({"pages": [1], "showParentOverlay": true})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.spreads[0].pages[0].show_parent_overlay);
|
||||
assert!(s.execute("layout.pageOptions", &json!({"pages": [2], "showParentOverlay": true})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_tool_pins_and_locks_objects_on_object_based_pages() {
|
||||
use designcraft_tools::{CanvasLayout, Mods, PointerEvent, PointerKind};
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 612, "height": 792, "facingPages": false})).unwrap();
|
||||
let id = s.execute("frame.create", &json!({"rect": [256, 72, 356, 132]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("liquid.pageRule", &json!({"rule": "objectBased"})).unwrap();
|
||||
s.execute("edit.deselectAll", &json!({})).unwrap();
|
||||
s.set_tool("page");
|
||||
let v = ViewInfo::at_zoom(1.0);
|
||||
let ev =
|
||||
|kind, p: designcraft_geom::Point, alt: bool| PointerEvent { mods: Mods { alt, ..Default::default() }, ..PointerEvent::new(kind, p.x, p.y) };
|
||||
let canvas = |s: &Session| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let l = CanvasLayout::new(d, false);
|
||||
(
|
||||
l.page_rect(d, 0).unwrap(),
|
||||
l.xf(designcraft_doc::SpreadRef::Doc(0)).transform_rect_bbox(d.item(designcraft_doc::ItemId(id)).unwrap().bounds()),
|
||||
)
|
||||
};
|
||||
let click = |s: &mut Session, p: designcraft_geom::Point| {
|
||||
s.pointer(&ev(PointerKind::Down, p, false), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Up, p, false), v).unwrap();
|
||||
};
|
||||
let liquid = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().liquid;
|
||||
// A click on the object selects it with its page; its pins and locks show.
|
||||
let (_, o) = canvas(&s);
|
||||
click(&mut s, o.center());
|
||||
let sel = s.doc().unwrap().selection.clone();
|
||||
assert_eq!((sel.page, sel.items.clone()), (Some(0), vec![designcraft_doc::ItemId(id)]));
|
||||
assert!(
|
||||
s.overlays(v)
|
||||
.iter()
|
||||
.any(|o| matches!(o, designcraft_tools::Overlay::LiquidPins { pins: [false, false, false, false], resize: [true, true], .. }))
|
||||
);
|
||||
// The right pin (14 px outside the right edge) and the width lock (10 px right of the centre).
|
||||
click(&mut s, designcraft_geom::Point::new(o.x1 + 14.0, o.center().y));
|
||||
click(&mut s, designcraft_geom::Point::new(o.center().x + 10.0, o.center().y));
|
||||
let l = liquid(&s).unwrap();
|
||||
assert!(l.pin_right && !l.resize_width && l.resize_height && !l.pin_left, "{l:?}");
|
||||
// Alt-drag the page 100 pt wider: the pinned, width-locked frame keeps its size by the right edge.
|
||||
let (p, _) = canvas(&s);
|
||||
let right = designcraft_geom::Point::new(p.x1, p.center().y);
|
||||
s.pointer(&ev(PointerKind::Down, right, true), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Drag, right + designcraft_geom::Vec2::new(100.0, 0.0), true), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Up, right + designcraft_geom::Vec2::new(100.0, 0.0), true), v).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let (pg, b) = (&d.spreads[0].pages[0], d.item(designcraft_doc::ItemId(id)).unwrap().bounds());
|
||||
assert_eq!(pg.width, 712.0);
|
||||
assert!((b.width() - 100.0).abs() < 1e-6 && (pg.x + pg.width - b.x1 - 256.0).abs() < 1e-6, "{b:?}");
|
||||
// Hovering a pin shows its tooltip.
|
||||
let (_, o) = canvas(&s);
|
||||
s.pointer(&ev(PointerKind::Move, designcraft_geom::Point::new(o.x1 + 14.0, o.center().y), false), v).unwrap();
|
||||
assert!(s.overlays(v).iter().any(|o| matches!(o, designcraft_tools::Overlay::Tip { text, .. } if text.starts_with("Object is pinned"))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicating_a_page_keeps_its_own_size_rule_guides_and_objects() {
|
||||
// Chris Cicchino's Page Tool video (2:06): the letterhead page is duplicated, then resized.
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 252, "height": 144, "facingPages": false})).unwrap();
|
||||
s.execute("layout.pages.insert", &json!({"count": 1})).unwrap();
|
||||
s.execute("layout.pageSize", &json!({"pages": [2], "width": 612, "height": 792})).unwrap();
|
||||
s.execute("liquid.pageRule", &json!({"rule": "scale", "pages": [2]})).unwrap();
|
||||
s.execute("guide.add", &json!({"spread": 1, "page": 0, "orientation": "vertical", "position": 300})).unwrap();
|
||||
s.execute("frame.create", &json!({"spread": 1, "rect": [100, 100, 200, 200]})).unwrap();
|
||||
let r = s.execute("layout.pages.duplicateSpread", &json!({"spread": 1})).unwrap();
|
||||
assert_eq!(r["page"], 2);
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let (si, pi) = d.page_loc(2).unwrap();
|
||||
let pg = &d.spreads[si].pages[pi];
|
||||
assert_eq!((pg.width, pg.height, pg.liquid), (612.0, 792.0, designcraft_doc::LiquidRule::Scale));
|
||||
assert_eq!(pg.guides.len(), 1);
|
||||
assert_eq!(d.spreads[si].items.len(), 1);
|
||||
// At the end of the document (the New Page button): the business card, not the last page's size.
|
||||
let r = s.execute("layout.pages.duplicateSpread", &json!({"spread": 0, "atEnd": true})).unwrap();
|
||||
assert_eq!(r["page"], 3);
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let (si, pi) = d.page_loc(3).unwrap();
|
||||
assert_eq!((d.spreads[si].pages[pi].width, d.spreads[si].pages[pi].height), (252.0, 144.0));
|
||||
}
|
||||
|
||||
/// Load the text from a frame's out port with the Selection tool and click on the pasteboard:
|
||||
/// a new frame is threaded there and the overset text flows into it.
|
||||
#[test]
|
||||
fn threading_with_the_loaded_text_cursor_on_the_sample() {
|
||||
use designcraft_tools::{PointerEvent, PointerKind};
|
||||
let mut s = Session::new();
|
||||
s.execute("file.newSample", &json!({})).unwrap();
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let (sid, first) = d.stories.values().find(|st| st.frames.len() >= 2).map(|st| (st.id, st.frames[0])).expect("a threaded story");
|
||||
// Break the thread so the first frame is overset, and select it.
|
||||
s.execute("text.unthread", &json!({"frame": first.0})).unwrap();
|
||||
assert!(s.cache.get(&s.doc().unwrap().doc, sid, None).is_overset());
|
||||
s.execute("selection.set", &json!({"ids": [first.0]})).unwrap();
|
||||
s.set_tool("selection");
|
||||
let v = ViewInfo::at_zoom(1.0);
|
||||
let layout = s.layout();
|
||||
let port = designcraft_tools::thread::port_center(&s.doc().unwrap().doc, &layout, first, true, v.zoom).unwrap();
|
||||
let ev = |k, p: designcraft_geom::Point| PointerEvent::new(k, p.x, p.y);
|
||||
s.pointer(&ev(PointerKind::Down, port), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Up, port), v).unwrap();
|
||||
assert_eq!(s.cursor(port + designcraft_geom::Vec2::new(-2000.0, 0.0), Default::default(), v), designcraft_tools::Cursor::LoadedText);
|
||||
// On the pasteboard left of the first spread.
|
||||
let b = layout.slots[0].bounds;
|
||||
let at = designcraft_geom::Point::new(b.x0 - 300.0, b.y0 + 100.0);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
s.pointer(&ev(PointerKind::Down, at), v).unwrap();
|
||||
s.pointer(&ev(PointerKind::Up, at), v).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(st.history.undo.len(), undo + 1, "one undo step");
|
||||
let frames = st.doc.story(sid).unwrap().frames.clone();
|
||||
assert_eq!(frames.len(), 2);
|
||||
let new = frames[1];
|
||||
assert_eq!(st.selection.items, [new]);
|
||||
let cs = s.cache.get(&s.doc().unwrap().doc, sid, None);
|
||||
assert!(cs.frames.iter().any(|f| f.frame == new && !f.range.is_empty()), "the text flows into the new frame");
|
||||
let nb = s.doc().unwrap().doc.item(new).unwrap().bounds();
|
||||
assert!((nb.y0 - layout.to_spread(designcraft_doc::SpreadRef::Doc(0), at).y).abs() < 1e-6, "{nb:?}");
|
||||
}
|
||||
|
||||
/// Drag a selection handle of item 0 on spread 0 from the edge point `at` by `by` (canvas units).
|
||||
fn drag_handle(s: &mut Session, at: designcraft_geom::Point, by: designcraft_geom::Vec2, v: ViewInfo) {
|
||||
use designcraft_tools::{PointerEvent, PointerKind};
|
||||
let p = s.layout().to_canvas(designcraft_doc::SpreadRef::Doc(0), at);
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, p.x, p.y), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, p.x + by.x / 2.0, p.y + by.y / 2.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, p.x + by.x, p.y + by.y), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, p.x + by.x, p.y + by.y), v).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_frame_resize_snaps_to_the_margins() {
|
||||
use designcraft_geom::{Point, Vec2};
|
||||
for zoom in [1.0, 0.5] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.newSample", &json!({})).unwrap();
|
||||
let margin = s.doc().unwrap().doc.spreads[0].pages[0].margin_rect();
|
||||
let (x0, y0) = (margin.x0 + 40.0, margin.y0 + 40.0);
|
||||
s.execute("frame.create", &json!({"rect": [x0, y0, x0 + 200.0, y0 + 200.0], "content": "text"})).unwrap();
|
||||
s.set_tool("selection");
|
||||
let v = ViewInfo::at_zoom(zoom);
|
||||
let b0 = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();
|
||||
// Right handle to 1.5 pt short of the right margin.
|
||||
drag_handle(&mut s, Point::new(b0.x1, b0.center().y), Vec2::new(margin.x1 - 1.5 - b0.x1, 0.0), v);
|
||||
let b = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();
|
||||
assert!((b.x1 - margin.x1).abs() < 1e-6, "zoom {zoom}: right edge {} not on the margin {}", b.x1, margin.x1);
|
||||
// Bottom handle to 1.5 pt past the bottom margin.
|
||||
drag_handle(&mut s, Point::new(b.center().x, b.y1), Vec2::new(0.0, margin.y1 + 1.5 - b.y1), v);
|
||||
let b = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();
|
||||
assert!((b.y1 - margin.y1).abs() < 1e-6, "zoom {zoom}: bottom edge {} not on the margin {}", b.y1, margin.y1);
|
||||
// Top-left corner handle onto the top and left margins.
|
||||
drag_handle(&mut s, Point::new(b.x0, b.y0), Vec2::new(margin.x0 + 1.0 - b.x0, margin.y0 - 1.0 - b.y0), v);
|
||||
let b = s.doc().unwrap().doc.spreads[0].items.last().unwrap().bounds();
|
||||
assert!((b.x0 - margin.x0).abs() < 1e-6 && (b.y0 - margin.y0).abs() < 1e-6, "zoom {zoom}: corner {b:?} not on the margins {margin:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_tool_frames_snap_to_the_margins() {
|
||||
use designcraft_geom::Point;
|
||||
use designcraft_tools::{PointerEvent, PointerKind};
|
||||
let mut s = session();
|
||||
let margin = s.doc().unwrap().doc.spreads[0].pages[0].margin_rect();
|
||||
s.set_tool("type");
|
||||
let v = ViewInfo::at_zoom(1.0);
|
||||
let c = |s: &Session, x: f64, y: f64| s.layout().to_canvas(designcraft_doc::SpreadRef::Doc(0), Point::new(x, y));
|
||||
// From 1.5 pt off the top-left margin corner to 1.5 pt off the right margin.
|
||||
let a = c(&s, margin.x0 + 1.5, margin.y0 + 1.5);
|
||||
let b = c(&s, margin.x1 - 1.5, margin.y0 + 101.5);
|
||||
let before = s.doc().unwrap().doc.spreads[0].items.len();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, a.x, a.y), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, (a.x + b.x) / 2.0, b.y), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, b.x, b.y), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, b.x, b.y), v).unwrap();
|
||||
let items = &s.doc().unwrap().doc.spreads[0].items;
|
||||
assert_eq!(items.len(), before + 1, "a frame was drawn");
|
||||
let r = items.last().unwrap().bounds();
|
||||
assert!((r.x0 - margin.x0).abs() < 1e-6 && (r.y0 - margin.y0).abs() < 1e-6, "press point {r:?} vs {margin:?}");
|
||||
assert!((r.x1 - margin.x1).abs() < 1e-6, "right edge {} vs {}", r.x1, margin.x1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicating_a_page_to_an_index_is_one_undo_step() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"facingPages": false})).unwrap();
|
||||
s.execute("layout.pages.insert", &json!({"count": 2})).unwrap();
|
||||
for (page, width) in [(1, 300), (2, 400), (3, 500)] {
|
||||
s.execute("layout.pageSize", &json!({"pages": [page], "width": width, "height": 600})).unwrap();
|
||||
}
|
||||
let widths = |s: &Session| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
(0..d.page_count()).map(|i| d.page_loc(i).map(|(si, pi)| d.spreads[si].pages[pi].width)).collect::<Option<Vec<_>>>().unwrap()
|
||||
};
|
||||
// Backwards: the last page copied to the front.
|
||||
let r = s.execute("layout.pages.duplicateSpread", &json!({"spread": 2, "to": 0})).unwrap();
|
||||
assert_eq!(r["page"], 0);
|
||||
assert_eq!(widths(&s), [500.0, 300.0, 400.0, 500.0]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(widths(&s), [300.0, 400.0, 500.0]);
|
||||
// Forwards: the first page copied to the end.
|
||||
s.execute("layout.pages.duplicateSpread", &json!({"spread": 0, "to": 3})).unwrap();
|
||||
assert_eq!(widths(&s), [300.0, 400.0, 500.0, 300.0]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(widths(&s), [300.0, 400.0, 500.0]);
|
||||
// Past the end: refused, nothing changes.
|
||||
assert!(s.execute("layout.pages.duplicateSpread", &json!({"spread": 0, "to": 4})).is_err());
|
||||
assert_eq!(widths(&s), [300.0, 400.0, 500.0]);
|
||||
}
|
||||
|
||||
@@ -91,6 +91,91 @@ fn magazine_round_trips_through_idml() {
|
||||
assert_eq!(items(&again.spreads).len(), items(&back.spreads).len());
|
||||
}
|
||||
|
||||
/// A one-page IDML package: two threaded frames side by side, 19 lines each (10 pt on 12 pt),
|
||||
/// paragraphs of `lines` forced-break lines in style `Body`, which carries `body_attrs`.
|
||||
fn keep_lines_idml(body_attrs: &str, lines: &[usize]) -> Vec<u8> {
|
||||
use std::io::Write;
|
||||
let paras: String = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(p, &n)| {
|
||||
let text = (0..n).map(|k| format!("P{p} line {k}")).collect::<Vec<_>>().join("\u{2028}");
|
||||
format!(
|
||||
r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Body"><CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>{text}</Content>{}</CharacterStyleRange></ParagraphStyleRange>"#,
|
||||
if p + 1 < lines.len() { "<Br/>" } else { "" }
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let frame = |me: &str, x: f64, prev: &str, next: &str| {
|
||||
let pts: String = [(x, 36.0), (x, 276.0), (x + 180.0, 276.0), (x + 180.0, 36.0)]
|
||||
.iter()
|
||||
.map(|(x, y)| format!(r#"<PathPointType Anchor="{x} {y}" LeftDirection="{x} {y}" RightDirection="{x} {y}"/>"#))
|
||||
.collect();
|
||||
format!(
|
||||
r#"<TextFrame Self="{me}" ParentStory="s1" PreviousTextFrame="{prev}" NextTextFrame="{next}" ItemTransform="1 0 0 1 0 0"><Properties><PathGeometry><GeometryPathType PathOpen="false"><PathPointArray>{pts}</PathPointArray></GeometryPathType></PathGeometry></Properties></TextFrame>"#
|
||||
)
|
||||
};
|
||||
let designmap = format!(
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="16.0" Self="d">
|
||||
<DocumentPreference PageWidth="450" PageHeight="312" FacingPages="false"/>
|
||||
<RootCharacterStyleGroup Self="rc"><CharacterStyle Self="CharacterStyle/$ID/[No character style]" Name="$ID/[No character style]"/></RootCharacterStyleGroup>
|
||||
<RootParagraphStyleGroup Self="rp">
|
||||
<ParagraphStyle Self="ParagraphStyle/$ID/[No paragraph style]" Name="$ID/[No paragraph style]" PointSize="12"/>
|
||||
<ParagraphStyle Self="ParagraphStyle/Body" Name="Body" PointSize="10" {body_attrs}><Properties><BasedOn type="object">ParagraphStyle/$ID/[No paragraph style]</BasedOn><Leading type="unit">12</Leading></Properties></ParagraphStyle>
|
||||
</RootParagraphStyleGroup>
|
||||
<Story Self="s1">{paras}</Story>
|
||||
<Spread Self="sp1" ItemTransform="1 0 0 1 0 0"><Page Self="p1" GeometricBounds="0 0 312 450" ItemTransform="1 0 0 1 0 0"/>{}{}</Spread>
|
||||
</Document>"#,
|
||||
frame("t1", 36.0, "n", "t2"),
|
||||
frame("t2", 234.0, "t1", "n")
|
||||
);
|
||||
let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
w.start_file("mimetype", stored).unwrap();
|
||||
w.write_all(b"application/vnd.adobe.indesign-idml-package").unwrap();
|
||||
w.start_file("designmap.xml", zip::write::SimpleFileOptions::default()).unwrap();
|
||||
w.write_all(designmap.as_bytes()).unwrap();
|
||||
w.finish().unwrap().into_inner()
|
||||
}
|
||||
|
||||
/// Lines of paragraph `para` in each frame of the story.
|
||||
fn para_lines_per_frame(d: &Document, para: usize) -> Vec<usize> {
|
||||
let sid = *d.stories.keys().next().unwrap();
|
||||
let cs = designcraft_compose::compose_story(d, sid, &Default::default());
|
||||
cs.frames.iter().map(|f| f.lines.iter().filter(|l| l.para == para).count()).collect()
|
||||
}
|
||||
|
||||
/// Keep Lines Together without a mode in the IDML: InDesign's default, at start/end of paragraph.
|
||||
/// A 10-line paragraph starting 6 lines above the first frame's bottom splits 6/4.
|
||||
#[test]
|
||||
fn idml_keep_lines_without_mode_split_at_start_and_end() {
|
||||
let keep = r#"KeepLinesTogether="true" KeepFirstLines="2" KeepLastLines="2""#;
|
||||
let d = designcraft_idml::import_idml(&keep_lines_idml(keep, &[13, 10, 2])).unwrap();
|
||||
assert_eq!(para_lines_per_frame(&d, 0), [13, 0]);
|
||||
assert_eq!(para_lines_per_frame(&d, 1), [6, 4]);
|
||||
// All lines in paragraph, written explicitly: the paragraph moves whole.
|
||||
let d = designcraft_idml::import_idml(&keep_lines_idml(&format!(r#"{keep} KeepAllLinesTogether="true""#), &[13, 10, 2])).unwrap();
|
||||
assert_eq!(para_lines_per_frame(&d, 1), [0, 10]);
|
||||
}
|
||||
|
||||
/// A document saved in format 1 with Keep Lines Together and no mode still keeps all lines
|
||||
/// together after loading; saved now, it keeps lines at start/end of paragraph.
|
||||
#[test]
|
||||
fn format_1_keep_lines_without_mode_lays_out_all_lines() {
|
||||
use std::io::Write;
|
||||
let d = designcraft_idml::import_idml(&keep_lines_idml(r#"KeepLinesTogether="true""#, &[13, 10, 2])).unwrap();
|
||||
let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
w.start_file("meta.json", zip::write::SimpleFileOptions::default()).unwrap();
|
||||
w.write_all(br#"{"format": "designcraft", "version": 1}"#).unwrap();
|
||||
w.start_file("document.json", zip::write::SimpleFileOptions::default()).unwrap();
|
||||
w.write_all(&serde_json::to_vec(&d).unwrap()).unwrap();
|
||||
let v1 = designcraft_format::load(&w.finish().unwrap().into_inner()).unwrap();
|
||||
assert_eq!(para_lines_per_frame(&v1, 1), [0, 10]);
|
||||
let now = designcraft_format::load(&designcraft_format::save(&d).unwrap()).unwrap();
|
||||
assert_eq!(para_lines_per_frame(&now, 1), [6, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idml_commands() {
|
||||
let mut s = Session::new();
|
||||
@@ -251,3 +336,60 @@ fn named_lists_round_trip_through_idml() {
|
||||
assert_eq!(st.paras[0].para.list_name.as_deref(), Some("Steps"));
|
||||
assert_eq!(st.paras[0].para.start_at, Some(Some(5)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_para_sets_a_drop_cap_that_survives_idml() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Initial", "chars": {"fontStyle": "Bold"}})).unwrap();
|
||||
let text = "Once upon a time there was a paragraph long enough to run beside its drop cap for a few lines and then some more.";
|
||||
let r = s.execute("frame.create", &json!({"rect": [72, 72, 300, 400], "content": "text", "text": text})).unwrap();
|
||||
let sid = r["story"].as_u64().unwrap();
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": 3, "focus": 3})).unwrap();
|
||||
s.execute("type.para", &json!({"attrs": {"dropCapLines": 3, "dropCapChars": 1, "dropCapStyle": "Initial", "dropCapAlignLeft": false}})).unwrap();
|
||||
let d = s.doc().unwrap().doc.clone();
|
||||
let cs = designcraft_compose::compose_story(&d, designcraft_doc::StoryId(sid), &Default::default());
|
||||
let lines = &cs.frames[0].lines;
|
||||
let dc = &lines[0].glyphs[0];
|
||||
assert_eq!(dc.byte, 0);
|
||||
assert!((lines[0].baseline + dc.y - lines[2].baseline).abs() < 1e-6, "on line 3's baseline");
|
||||
assert!(lines[0].drop_cap.is_some_and(|b| b.end == 1));
|
||||
let p = &d.story(designcraft_doc::StoryId(sid)).unwrap().paras[0].para;
|
||||
assert_eq!((p.drop_cap_lines, p.drop_cap_chars, p.drop_cap_style.as_deref()), (Some(3), Some(1), Some("Initial")));
|
||||
let back = designcraft_idml::import_idml(&designcraft_idml::export_idml(&d)).unwrap();
|
||||
let p = &back.stories.values().find(|st| st.text.starts_with("Once")).unwrap().paras[0].para;
|
||||
assert_eq!((p.drop_cap_lines, p.drop_cap_chars, p.drop_cap_align_left), (Some(3), Some(1), Some(false)));
|
||||
// The drop cap's character style goes through IDML as a leading Dropcap nested style.
|
||||
assert_eq!(p.drop_cap_style.as_deref(), Some("Initial"));
|
||||
assert_eq!(p.nested_styles, Some(vec![designcraft_doc::NestedStyle::drop_cap("Initial")]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_of_contents_definition_round_trips_through_idml_and_updates() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.newSample", &json!({})).unwrap();
|
||||
let page = s.doc().unwrap().doc.page_count() + 1;
|
||||
s.execute("layout.pages.insert", &json!({"count": 1})).unwrap();
|
||||
let basic = designcraft_doc::story::BASIC_PARAGRAPH;
|
||||
s.execute(
|
||||
"toc.generate",
|
||||
&json!({"page": page, "title": "Inside", "titleStyle": basic, "entries": [
|
||||
{"style": "Cover Title", "level": 1, "pageNumber": "before", "separator": " ^ "},
|
||||
{"style": "Headline", "level": 2, "entryStyle": basic, "pageNumber": "none"},
|
||||
]}),
|
||||
)
|
||||
.unwrap();
|
||||
let toc_text = |s: &Session| {
|
||||
let d = &s.doc().unwrap().doc;
|
||||
d.story(d.toc.as_ref().unwrap().story).unwrap().text.clone()
|
||||
};
|
||||
let before = s.doc().unwrap().doc.toc.clone().unwrap();
|
||||
let text = toc_text(&s);
|
||||
let b64 = s.execute("file.exportIdml", &json!({})).unwrap()["base64"].as_str().unwrap().to_string();
|
||||
s.execute("file.openIdml", &json!({"base64": b64, "name": "toc.idml"})).unwrap();
|
||||
let after = s.doc().unwrap().doc.toc.clone().expect("the TOC definition is imported");
|
||||
assert_eq!((&after.title, &after.title_style, &after.entries), (&before.title, &before.title_style, &before.entries));
|
||||
assert_eq!(toc_text(&s), text);
|
||||
s.execute("toc.update", &json!({})).unwrap();
|
||||
assert_eq!(toc_text(&s), text);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,321 @@ fn check(s: &Session) {
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_text_replacement_floors_utf8_selection_and_allows_quote_typing() {
|
||||
for (original, (a, b), replacement, (after_a, after_b), quoted) in [
|
||||
("a", (1, 1), "é", (0, 0), Some("“é")), // Exact caret/quote baseline.
|
||||
("abc", (3, 3), "🙂", (0, 0), Some("“🙂")),
|
||||
("ab", (1, 2), "éX", (0, 2), None),
|
||||
("ab", (2, 1), "éX", (2, 0), None),
|
||||
("abcdef", (3, 5), "🙂éX", (0, 4), None),
|
||||
("abcdef", (5, 3), "🙂éX", (4, 0), None),
|
||||
("abc", (1, 2), "🙂", (0, 0), None), // Both endpoints floor to the same boundary.
|
||||
("abcdef", (2, 6), "é🙂", (2, 6), None), // Valid Unicode endpoints stay fixed.
|
||||
("abcdef", (1, 4), "ABCDEF", (1, 4), None),
|
||||
("abcdef", (4, 1), "ABCDEF", (4, 1), None),
|
||||
("abcdef", (6, 6), "é", (2, 2), None), // Shortening clamps to EOF.
|
||||
("abc", (1, 3), "", (0, 0), None),
|
||||
] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let frame = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text", "text": ""})).unwrap();
|
||||
let sid = frame["story"].as_u64().unwrap();
|
||||
let fid = frame["id"].as_u64().unwrap();
|
||||
let tid = s.execute("table.insert", &json!({"rows": 1, "cols": 1})).unwrap()["table"].as_u64().unwrap();
|
||||
s.execute("text.insert", &json!({"text": original, "raw": true})).unwrap();
|
||||
// Real commands position the child selection; text.select addresses parent stories.
|
||||
for _ in a..original.len() {
|
||||
s.execute("text.move", &json!({"dir": "left"})).unwrap();
|
||||
}
|
||||
let (dir, steps) = if b >= a { ("right", b - a) } else { ("left", a - b) };
|
||||
for _ in 0..steps {
|
||||
s.execute("text.move", &json!({"dir": dir, "extend": true})).unwrap();
|
||||
}
|
||||
let mut expected = s.doc().unwrap().selection.clone();
|
||||
let selected = expected.text.as_mut().unwrap();
|
||||
assert_eq!((selected.anchor, selected.focus), (a, b));
|
||||
assert_eq!(selected.frame, Some(doc::ItemId(fid)));
|
||||
assert_eq!(selected.cell, Some(doc::CellAddr { table: tid, row: 0, col: 0 }));
|
||||
(selected.anchor, selected.focus) = (after_a, after_b);
|
||||
let parent = s.doc().unwrap().doc.story(doc::StoryId(sid)).unwrap().text.clone();
|
||||
s.execute("table.setCell", &json!({"text": replacement})).unwrap();
|
||||
assert_eq!(table_for(&s, sid, tid).cell(0, 0).unwrap().text.text, replacement);
|
||||
assert_eq!(s.doc().unwrap().selection, expected, "{original}: {a}->{b}, replacement {replacement}");
|
||||
assert!(replacement.is_char_boundary(after_a) && replacement.is_char_boundary(after_b));
|
||||
assert_eq!(s.doc().unwrap().doc.story(doc::StoryId(sid)).unwrap().text, parent);
|
||||
if let Some(quoted) = quoted {
|
||||
assert!(s.prefs.typographers_quotes);
|
||||
s.execute("text.insert", &json!({"text": "\""})).unwrap();
|
||||
assert_eq!(table_for(&s, sid, tid).cell(0, 0).unwrap().text.text, quoted);
|
||||
let selected = expected.text.as_mut().unwrap();
|
||||
(selected.anchor, selected.focus) = (3, 3); // Opening curly quote is three bytes.
|
||||
assert_eq!(s.doc().unwrap().selection, expected);
|
||||
}
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_edit_after_anchor_removal_floors_parent_caret() {
|
||||
for (original, (a, b), replacement, pos, quoted, typed_pos) in [("ab", (1, 2), "é", 0, "“é\n", 3), ("abcde", (5, 1), "é🙂", 2, "é”🙂\n", 5)]
|
||||
{
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let other = s.execute("frame.create", &json!({"rect": [320, 36, 580, 200], "content": "text"})).unwrap();
|
||||
let other_sid = other["story"].as_u64().unwrap();
|
||||
let other_tid = s.execute("table.insert", &json!({"rows": 1, "cols": 1})).unwrap()["table"].as_u64().unwrap();
|
||||
s.execute("text.insert", &json!({"text": "keep", "raw": true})).unwrap();
|
||||
let mut expected_other = table_for(&s, other_sid, other_tid).clone();
|
||||
expected_other.cell_mut(0, 0).unwrap().insets = [7.0; 4];
|
||||
|
||||
let frame = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap();
|
||||
let sid = frame["story"].as_u64().unwrap();
|
||||
let tid = s.execute("table.insert", &json!({"rows": 1, "cols": 1})).unwrap()["table"].as_u64().unwrap();
|
||||
s.execute("text.insert", &json!({"text": original, "raw": true})).unwrap();
|
||||
for _ in a..original.len() {
|
||||
s.execute("text.move", &json!({"dir": "left"})).unwrap();
|
||||
}
|
||||
let (dir, steps) = if b >= a { ("right", b - a) } else { ("left", a - b) };
|
||||
for _ in 0..steps {
|
||||
s.execute("text.move", &json!({"dir": dir, "extend": true})).unwrap();
|
||||
}
|
||||
let mut expected = s.doc().unwrap().selection.clone();
|
||||
let selected = expected.text.unwrap();
|
||||
assert_eq!((selected.anchor, selected.focus), (a, b));
|
||||
assert_eq!(selected.frame, Some(doc::ItemId(frame["id"].as_u64().unwrap())));
|
||||
assert_eq!(selected.cell, Some(doc::CellAddr { table: tid, row: 0, col: 0 }));
|
||||
// Explicit parent replacement removes its anchor, leaving the child address stale.
|
||||
assert_eq!(
|
||||
s.execute("find.change", &json!({"story": sid, "find": doc::TABLE_ANCHOR.to_string(), "change": replacement})).unwrap()["count"],
|
||||
1
|
||||
);
|
||||
let parent = s.doc().unwrap().doc.story(doc::StoryId(sid)).unwrap();
|
||||
assert_eq!(parent.text, format!("{replacement}\n"));
|
||||
assert!(!parent.tables.contains_key(&tid));
|
||||
assert_eq!(s.doc().unwrap().selection, expected);
|
||||
assert!(!parent.text.is_char_boundary(selected.anchor));
|
||||
// An explicit edit to the surviving table reaches the disappeared-table fallback.
|
||||
s.execute("table.setCell", &json!({"story": other_sid, "table": other_tid, "insets": 7})).unwrap();
|
||||
let selected = expected.text.as_mut().unwrap();
|
||||
(selected.anchor, selected.focus, selected.cell) = (pos, pos, None); // Both floor the OLD anchor.
|
||||
assert_eq!(s.doc().unwrap().selection, expected);
|
||||
assert_eq!(s.doc().unwrap().doc.story(doc::StoryId(sid)).unwrap().text, format!("{replacement}\n"));
|
||||
assert_eq!(table_for(&s, other_sid, other_tid), &expected_other);
|
||||
s.execute("text.insert", &json!({"text": "\""})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.story(doc::StoryId(sid)).unwrap().text, quoted);
|
||||
let selected = expected.text.as_mut().unwrap();
|
||||
(selected.anchor, selected.focus) = (typed_pos, typed_pos);
|
||||
assert_eq!(s.doc().unwrap().selection, expected);
|
||||
assert_eq!(table_for(&s, other_sid, other_tid), &expected_other);
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
|
||||
fn selected_merged_table(column: bool) -> (Session, u64, u64, u64) {
|
||||
let (mut s, sid, fid) = session_with_frame();
|
||||
let tid = s.execute("table.insert", &json!({"rows": 2, "cols": 2})).unwrap()["table"].as_u64().unwrap();
|
||||
for (i, text) in ["A", "B", "C", "D"].iter().enumerate() {
|
||||
s.execute("text.insert", &json!({"text": text})).unwrap();
|
||||
if i < 3 {
|
||||
s.execute("table.nextCell", &json!({})).unwrap();
|
||||
}
|
||||
}
|
||||
let merge = if column { json!({"rows": [0, 0], "cols": [0, 1]}) } else { json!({"rows": [0, 1], "cols": [0, 0]}) };
|
||||
s.execute("table.select", &merge).unwrap();
|
||||
s.execute("table.merge", &json!({})).unwrap();
|
||||
s.execute("table.select", &json!({"rows": [1, 1], "cols": [1, 1]})).unwrap();
|
||||
s.execute(if column { "table.selectColumn" } else { "table.selectRow" }, &json!({})).unwrap();
|
||||
(s, sid, tid, fid)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_merged_owners_receive_text_formatting_and_character_styles() {
|
||||
for column in [true, false] {
|
||||
let (mut s, sid, tid, _) = selected_merged_table(column);
|
||||
let selected = s.doc().unwrap().selection.clone();
|
||||
assert_eq!(selected.cells.unwrap().range, if column { doc::CellRange::new(0, 1, 1, 1) } else { doc::CellRange::new(1, 0, 1, 1) });
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
let neighbor = if column { (1, 0) } else { (0, 1) };
|
||||
s.execute("type.char", &json!({"attrs": {"size": 23}})).unwrap();
|
||||
for (r, c) in [(0, 0), (1, 1)] {
|
||||
let cell = table_for(&s, sid, tid).cell(r, c).unwrap();
|
||||
assert!(cell.text.runs().all(|(_, f)| f.over.size == Some(23.0)), "all runs of owner {r},{c}");
|
||||
assert_eq!(cell.text.text, before.cell(r, c).unwrap().text.text);
|
||||
}
|
||||
assert_eq!(table_for(&s, sid, tid).cell(neighbor.0, neighbor.1), before.cell(neighbor.0, neighbor.1));
|
||||
assert_eq!(s.doc().unwrap().selection, selected);
|
||||
s.execute("style.character.create", &json!({"name": "Selected owners", "chars": {"tracking": 37}})).unwrap();
|
||||
s.execute("style.character.apply", &json!({"name": "Selected owners"})).unwrap();
|
||||
for (r, c) in [(0, 0), (1, 1)] {
|
||||
assert!(table_for(&s, sid, tid).cell(r, c).unwrap().text.runs().all(|(_, f)| f.style == "Selected owners"));
|
||||
}
|
||||
let after = table_for(&s, sid, tid);
|
||||
assert_eq!(after.cell(neighbor.0, neighbor.1), before.cell(neighbor.0, neighbor.1));
|
||||
assert_eq!(after.rows, before.rows);
|
||||
assert_eq!(after.columns, before.columns);
|
||||
assert_eq!(s.doc().unwrap().selection, selected, "formatting preserves the original grid rectangle");
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_merged_owners_receive_cell_options_and_unique_style_counts() {
|
||||
for column in [true, false] {
|
||||
let (mut s, sid, tid, _) = selected_merged_table(column);
|
||||
let range = s.doc().unwrap().selection.cells.unwrap().range;
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
let neighbor = if column { (1, 0) } else { (0, 1) };
|
||||
let covered = if column { (0, 1) } else { (1, 0) };
|
||||
s.execute("table.setCell", &json!({"fill": "[Black]", "insets": 7, "text": "REPLACED"})).unwrap();
|
||||
for (r, c) in [(0, 0), (1, 1)] {
|
||||
let cell = table_for(&s, sid, tid).cell(r, c).unwrap();
|
||||
assert_eq!(serde_json::to_value(&cell.fill).unwrap(), json!("[Black]"));
|
||||
assert_eq!(cell.insets, [7.0; 4]);
|
||||
assert_eq!(cell.text.text, "REPLACED");
|
||||
}
|
||||
s.execute("style.cell.create", &json!({"name": "Selected cell owners", "insets": 9})).unwrap();
|
||||
assert_eq!(s.execute("style.cell.apply", &json!({"name": "Selected cell owners"})).unwrap()["cells"], 2);
|
||||
let after = table_for(&s, sid, tid);
|
||||
for (r, c) in [(0, 0), (1, 1)] {
|
||||
assert_eq!(after.cell(r, c).unwrap().style, "Selected cell owners");
|
||||
assert_eq!(after.cell(r, c).unwrap().insets, [9.0; 4]);
|
||||
}
|
||||
assert_eq!(after.cell(neighbor.0, neighbor.1), before.cell(neighbor.0, neighbor.1));
|
||||
assert_eq!(after.cell(covered.0, covered.1), before.cell(covered.0, covered.1));
|
||||
assert_eq!(after.rows, before.rows);
|
||||
assert_eq!(after.columns, before.columns);
|
||||
assert_eq!(s.doc().unwrap().selection.cells.unwrap().range, range);
|
||||
let merged = if column { json!({"rows": [0, 0], "cols": [0, 1]}) } else { json!({"rows": [0, 1], "cols": [0, 0]}) };
|
||||
s.execute("table.select", &merged).unwrap();
|
||||
assert_eq!(s.execute("style.cell.apply", &json!({"name": "Selected cell owners"})).unwrap()["cells"], 1);
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn covered_owner_strokes_keep_literal_edge_masks_and_neighbors() {
|
||||
for rtl in [false, true] {
|
||||
for outer in [false, true] {
|
||||
let (mut s, sid, tid, _) = selected_merged_table(true);
|
||||
if rtl {
|
||||
s.execute("table.options", &json!({"direction": "rightToLeft"})).unwrap();
|
||||
}
|
||||
s.execute("table.select", &json!({"rows": [0, 0], "cols": [1, 1]})).unwrap();
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
s.execute("table.setCell", &json!({"stroke": {"weight": 3, "edges": if outer { "outer" } else { "inner" }}})).unwrap();
|
||||
let after = table_for(&s, sid, tid);
|
||||
let mask = if rtl { [true, true, true, false] } else { [true, false, true, true] };
|
||||
let expected = mask.map(|edge| if outer { edge } else { !edge });
|
||||
let owner = after.cell(0, 0).unwrap();
|
||||
assert_eq!(owner.border_overrides, expected);
|
||||
assert_eq!(owner.stroke_defined, expected);
|
||||
for (i, on) in expected.into_iter().enumerate() {
|
||||
if on {
|
||||
assert_eq!(owner.strokes[i].weight, 3.0);
|
||||
assert_eq!(owner.stroke_priorities[i], 1);
|
||||
} else {
|
||||
assert_eq!(owner.strokes[i], before.cell(0, 0).unwrap().strokes[i]);
|
||||
}
|
||||
}
|
||||
for (r, c) in [(0, 1), (1, 0), (1, 1)] {
|
||||
assert_eq!(after.cell(r, c), before.cell(r, c), "strict merged/covered mirror guard preserves {r},{c}");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().selection.cells.unwrap().range, doc::CellRange::cell(0, 1));
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cell_style_sampling_resolves_covered_point_but_retains_upper_left_policy() {
|
||||
let (mut s, sid, tid, _) = selected_merged_table(true);
|
||||
s.execute("table.setCell", &json!({"row": 0, "col": 0, "insets": 7})).unwrap();
|
||||
s.execute("table.select", &json!({"rows": [0, 0], "cols": [1, 1]})).unwrap();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
s.execute("style.cell.create", &json!({"name": "Covered", "fromSelection": true})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.styles.cell.iter().find(|c| c.name == "Covered").unwrap().insets, Some([7.0; 4]));
|
||||
assert_eq!(table_for(&s, sid, tid), &before);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
|
||||
let (mut s, sid, _) = session_with_frame();
|
||||
let tid = s.execute("table.insert", &json!({"rows": 2, "cols": 2})).unwrap()["table"].as_u64().unwrap();
|
||||
s.execute("table.select", &json!({"rows": [0, 1], "cols": [1, 1]})).unwrap();
|
||||
s.execute("table.merge", &json!({})).unwrap();
|
||||
for (row, col, insets, size) in [(0, 1, 7, 17), (1, 0, 11, 29)] {
|
||||
s.execute("table.setCell", &json!({"row": row, "col": col, "text": "Text", "insets": insets})).unwrap();
|
||||
s.execute("table.select", &json!({"rows": [row, row], "cols": [col, col]})).unwrap();
|
||||
s.execute("type.char", &json!({"attrs": {"size": size}})).unwrap();
|
||||
}
|
||||
s.execute("table.selectRow", &json!({})).unwrap();
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
// Row-major owners are [(0,1),(1,0)], while the rectangle starts at (1,0).
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["size"], 17.0);
|
||||
s.execute("style.cell.create", &json!({"name": "Upper left", "fromSelection": true})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.styles.cell.iter().find(|c| c.name == "Upper left").unwrap().insets, Some([11.0; 4]));
|
||||
assert_eq!(table_for(&s, sid, tid), &before);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_merged_owners_convert_graphics_back_to_text() {
|
||||
let png = designcraft_render::Rendered { width: 1, height: 1, pixels: vec![255, 0, 0, 255] }.to_png();
|
||||
let image = crate::cmd::base64_encode(&png);
|
||||
for column in [true, false] {
|
||||
let (mut s, sid, tid, _) = selected_merged_table(column);
|
||||
let neighbor = if column { (1, 0) } else { (0, 1) };
|
||||
let covered = if column { (0, 1) } else { (1, 0) };
|
||||
for (r, c) in [covered, (1, 1), neighbor] {
|
||||
let placed = s.execute("table.placeGraphic", &json!({"row": r, "col": c, "base64": image, "name": "cell.png"})).unwrap();
|
||||
if (r, c) == covered {
|
||||
assert_eq!((placed["row"].as_u64(), placed["col"].as_u64()), (Some(0), Some(0)));
|
||||
}
|
||||
}
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
let selected = s.doc().unwrap().selection.cells.unwrap();
|
||||
s.execute("table.textCell", &json!({})).unwrap();
|
||||
let after = table_for(&s, sid, tid);
|
||||
for (r, c) in [(0, 0), (1, 1)] {
|
||||
assert!(before.cell(r, c).unwrap().graphic.is_some());
|
||||
assert!(after.cell(r, c).unwrap().graphic.is_none());
|
||||
assert_eq!(after.cell(r, c).unwrap().text, before.cell(r, c).unwrap().text);
|
||||
}
|
||||
assert_eq!(after.cell(neighbor.0, neighbor.1), before.cell(neighbor.0, neighbor.1));
|
||||
assert_eq!(after.cell(covered.0, covered.1), before.cell(covered.0, covered.1));
|
||||
assert_eq!(s.doc().unwrap().selection.cells, Some(selected));
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn covered_selected_owner_drag_keeps_merged_move_restrictions_atomic() {
|
||||
for column in [true, false] {
|
||||
let (mut s, sid, _, fid) = selected_merged_table(column);
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let revision = s.doc().unwrap().revision;
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let redo = s.doc().unwrap().history.redo.len();
|
||||
let cs = s.cache.get(&before, doc::StoryId(sid), None);
|
||||
let cells = &cs.frames[0].tables[0].cells;
|
||||
let point = |row, col| {
|
||||
let cell = cells.iter().find(|c| c.row == row && c.col == col).unwrap();
|
||||
before.text_xf(before.item(doc::ItemId(fid)).unwrap()) * cell.rect.center()
|
||||
};
|
||||
let from = point(0, 0);
|
||||
let to = if column { point(1, 0) } else { point(0, 1) };
|
||||
let err = s.execute("table.dropCells", &json!({"frame": fid, "from": [from.x, from.y], "to": [to.x, to.y]})).unwrap_err();
|
||||
assert!(err.to_string().contains(if column { "unmerge cells that span columns" } else { "unmerge cells that span rows" }), "{err}");
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(st.doc, before);
|
||||
assert_eq!(st.selection, selection);
|
||||
assert_eq!(st.revision, revision);
|
||||
assert_eq!((st.history.undo.len(), st.history.redo.len()), (undo, redo));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_type_in_cells_and_tab() {
|
||||
let (mut s, sid, _) = session_with_frame();
|
||||
@@ -399,3 +714,125 @@ fn editing_beside_a_merged_cell_only_changes_the_selected_edge_segment() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_selected_row_or_column_splits_both_cells() {
|
||||
for horizontal in [true, false] {
|
||||
let (mut s, sid, _) = session_with_frame();
|
||||
let tid = s.execute("table.insert", &json!({"rows": 2, "cols": 2})).unwrap()["table"].as_u64().unwrap();
|
||||
let params = if horizontal { json!({"rows": [0, 0], "cols": [0, 1]}) } else { json!({"rows": [0, 1], "cols": [0, 0]}) };
|
||||
s.execute("table.select", ¶ms).unwrap();
|
||||
let command = if horizontal { "table.splitHorizontally" } else { "table.splitVertically" };
|
||||
s.execute(command, &json!({})).unwrap();
|
||||
let table = table_for(&s, sid, tid);
|
||||
assert_eq!((table.nrows(), table.ncols()), if horizontal { (3, 2) } else { (2, 3) });
|
||||
for r in 0..2 {
|
||||
for c in 0..2 {
|
||||
assert_eq!(table.owner(r, c), (r, c), "both selected cells have independent halves");
|
||||
}
|
||||
}
|
||||
check(&s);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_selected_block_and_merged_cells_preserves_geometry_and_history() {
|
||||
for horizontal in [true, false] {
|
||||
for merged in [false, true] {
|
||||
let (mut s, sid, _) = session_with_frame();
|
||||
let tid = s.execute("table.insert", &json!({"rows": 4, "cols": 4, "width": 240})).unwrap()["table"].as_u64().unwrap();
|
||||
s.execute("table.setRowHeight", &json!({"table": tid, "height": 60, "mode": "exactly"})).unwrap();
|
||||
s.execute("style.cell.create", &json!({"name": "Neighbor", "fill": "[Paper]", "insets": 3})).unwrap();
|
||||
s.execute("style.cell.create", &json!({"name": "Chosen", "fill": "[Black]", "tint": 0.15, "insets": 2})).unwrap();
|
||||
s.execute("style.cell.apply", &json!({"table": tid, "name": "Neighbor"})).unwrap();
|
||||
for r in 0..4 {
|
||||
for c in 0..4 {
|
||||
s.execute("table.setCell", &json!({"row": r, "col": c, "text": format!("{r},{c}")})).unwrap();
|
||||
}
|
||||
}
|
||||
let range = if !merged {
|
||||
doc::CellRange::new(1, 1, 2, 2)
|
||||
} else if horizontal {
|
||||
doc::CellRange::new(0, 0, 2, 1)
|
||||
} else {
|
||||
doc::CellRange::new(0, 0, 1, 2)
|
||||
};
|
||||
let selection = json!({"rows": [range.r0, range.r1], "cols": [range.c0, range.c1]});
|
||||
s.execute("table.select", &selection).unwrap();
|
||||
s.execute("style.cell.apply", &json!({"name": "Chosen"})).unwrap();
|
||||
if merged {
|
||||
let merge = if horizontal { json!({"rows": [0, 2], "cols": [1, 1]}) } else { json!({"rows": [1, 1], "cols": [0, 2]}) };
|
||||
s.execute("table.merge", &merge).unwrap();
|
||||
s.execute("table.select", &selection).unwrap();
|
||||
}
|
||||
let before_doc = s.doc().unwrap().doc.clone();
|
||||
let before_selection = s.doc().unwrap().selection.clone();
|
||||
let before = table_for(&s, sid, tid).clone();
|
||||
let before_layout = s.cache.get(&before_doc, doc::StoryId(sid), None);
|
||||
let before_fragment = before_layout.frames.iter().flat_map(|f| &f.tables).find(|t| t.table == tid).unwrap();
|
||||
let undo_len = s.doc().unwrap().history.undo.len();
|
||||
let command = if horizontal { "table.splitHorizontally" } else { "table.splitVertically" };
|
||||
s.execute(command, &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len + 1, "one split command creates one undo entry");
|
||||
let after_doc = s.doc().unwrap().doc.clone();
|
||||
let after_selection = s.doc().unwrap().selection.clone();
|
||||
let after = table_for(&s, sid, tid).clone();
|
||||
let after_layout = s.cache.get(&after_doc, doc::StoryId(sid), None);
|
||||
let after_fragment = after_layout.frames.iter().flat_map(|f| &f.tables).find(|t| t.table == tid).unwrap();
|
||||
assert_eq!(after_fragment.rect, before_fragment.rect, "splitting does not resize the table");
|
||||
assert_eq!(after_fragment.cells.len(), before_fragment.cells.len() + 4, "four original owners split once");
|
||||
for placed in &before_fragment.cells {
|
||||
let original = before.cell(placed.row, placed.col).unwrap();
|
||||
let matches: Vec<_> = after_fragment.cells.iter().filter(|c| c.source == placed.source).collect();
|
||||
assert_eq!(matches.len(), 1, "original cell text survives once: {}", placed.source);
|
||||
let surviving = matches[0];
|
||||
let cell = after.cell(surviving.row, surviving.col).unwrap();
|
||||
let mut normalized = cell.clone();
|
||||
normalized.row_span = original.row_span;
|
||||
normalized.col_span = original.col_span;
|
||||
assert_eq!(&normalized, original, "existing cell text and formatting are preserved");
|
||||
if !range.contains(placed.row, placed.col) {
|
||||
assert_eq!(surviving.rect, placed.rect, "unselected neighbor geometry is unchanged");
|
||||
continue;
|
||||
}
|
||||
let second_addr = if horizontal {
|
||||
(surviving.row + cell.row_span as usize, surviving.col)
|
||||
} else {
|
||||
(surviving.row, surviving.col + cell.col_span as usize)
|
||||
};
|
||||
let second = after.cell(second_addr.0, second_addr.1).unwrap();
|
||||
assert_eq!(after.owner(second_addr.0, second_addr.1), second_addr);
|
||||
assert!(second.text.is_empty());
|
||||
assert_eq!(second.style, original.style);
|
||||
assert_eq!(second.insets, original.insets);
|
||||
assert_eq!(second.fill, original.fill);
|
||||
assert_eq!(second.fill_tint, original.fill_tint);
|
||||
let second_rect = after_fragment.cell(second_addr.0, second_addr.1).unwrap().rect;
|
||||
assert_eq!(surviving.rect.union(second_rect), placed.rect, "the halves exactly cover the original cell");
|
||||
if horizontal {
|
||||
assert_eq!(surviving.rect.y1, second_rect.y0);
|
||||
} else {
|
||||
assert_eq!(surviving.rect.x1, second_rect.x0);
|
||||
}
|
||||
}
|
||||
let text = s.doc().unwrap().selection.text.unwrap();
|
||||
let address = text.cell.unwrap();
|
||||
assert_eq!(after.owner(address.row, address.col), (address.row, address.col));
|
||||
assert!(text.focus <= after.cell(address.row, address.col).unwrap().text.len());
|
||||
let cells = s.doc().unwrap().selection.cells.unwrap();
|
||||
assert!(cells.range.r1 < after.nrows() && cells.range.c1 < after.ncols());
|
||||
check(&s);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().doc, &before_doc));
|
||||
assert_eq!(s.doc().unwrap().selection, before_selection);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().doc, &after_doc));
|
||||
assert_eq!(s.doc().unwrap().selection, after_selection);
|
||||
let bytes = crate::cmd::file_bytes(&s.doc().unwrap().doc);
|
||||
let reopened = crate::cmd::file_from(&bytes).unwrap();
|
||||
reopened.check().unwrap();
|
||||
assert_eq!(reopened.story(doc::StoryId(sid)).unwrap().tables.get(&tid).unwrap().as_ref(), &after);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
use std::path::Path;
|
||||
fn dir(name: &str) -> tempfile::TempDir {
|
||||
tempfile::Builder::new().prefix(&format!("dc-book-safety-{name}-")).tempdir().unwrap()
|
||||
}
|
||||
fn document(start: u32, custom: bool) -> designcraft_doc::Document {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"pages":2})).unwrap();
|
||||
let mut d = (*s.doc().unwrap().doc).clone();
|
||||
d.sections.first_mut().unwrap().start_number = Some(start);
|
||||
if custom {
|
||||
let mut style = d.styles.paragraph.get(1).unwrap().clone();
|
||||
style.name = "Synthetic Book Style".into();
|
||||
d.styles_mut().paragraph.push(style);
|
||||
}
|
||||
d
|
||||
}
|
||||
fn native(p: &Path, start: u32, custom: bool) {
|
||||
std::fs::write(p, designcraft_format::save(&document(start, custom)).unwrap()).unwrap();
|
||||
}
|
||||
fn idml(p: &Path, start: u32) {
|
||||
std::fs::write(p, designcraft_idml::export_idml(&document(start, false))).unwrap();
|
||||
assert!(designcraft_idml::import_idml(&std::fs::read(p).unwrap()).is_ok());
|
||||
}
|
||||
fn book(d: &Path, chapters: &[&Path]) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("book.new", &json!({"path":d.join("book.dcbook").to_string_lossy()})).unwrap();
|
||||
for p in chapters {
|
||||
s.execute("book.add", &json!({"path":p.to_string_lossy()})).unwrap();
|
||||
}
|
||||
s
|
||||
}
|
||||
#[test]
|
||||
fn late_missing_chapter_does_not_partially_paginate_earlier_chapters() {
|
||||
let d = dir("late-missing");
|
||||
let first = d.path().join("first.designcraft");
|
||||
let later = d.path().join("later.designcraft");
|
||||
native(&first, 9, false);
|
||||
native(&later, 1, false);
|
||||
let mut s = book(d.path(), &[&first, &later]);
|
||||
let before = std::fs::read(&first).unwrap();
|
||||
std::fs::remove_file(later).unwrap();
|
||||
let result = s.execute("book.paginate", &json!({}));
|
||||
assert!(result.is_err());
|
||||
let after = std::fs::read(&first).unwrap();
|
||||
assert!(
|
||||
before == after,
|
||||
"book.paginate changed the first chapter before error: {result:?}; new first number {:?}",
|
||||
designcraft_format::load(&after).unwrap().sections.first().unwrap().start_number
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn late_corrupt_chapter_does_not_partially_sync_earlier_chapters() {
|
||||
let d = dir("late-corrupt");
|
||||
let source = d.path().join("source.designcraft");
|
||||
let first = d.path().join("first.designcraft");
|
||||
let later = d.path().join("later.designcraft");
|
||||
native(&source, 1, true);
|
||||
native(&first, 1, false);
|
||||
native(&later, 1, false);
|
||||
let mut s = book(d.path(), &[&source, &first, &later]);
|
||||
let before = std::fs::read(&first).unwrap();
|
||||
std::fs::write(later, b"not a document").unwrap();
|
||||
let result = s.execute("book.syncStyles", &json!({}));
|
||||
assert!(result.is_err());
|
||||
let after = std::fs::read(&first).unwrap();
|
||||
assert!(
|
||||
before == after,
|
||||
"book.syncStyles changed the first target before error: {result:?}; copied custom style {}",
|
||||
designcraft_format::load(&after).unwrap().styles.paragraph.iter().any(|s| s.name == "Synthetic Book Style")
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn paginate_does_not_replace_imported_idml_with_native_format() {
|
||||
let d = dir("idml-paginate");
|
||||
let chapter = d.path().join("chapter.idml");
|
||||
idml(&chapter, 9);
|
||||
let mut s = book(d.path(), &[&chapter]);
|
||||
let before = std::fs::read(&chapter).unwrap();
|
||||
let result = s.execute("book.paginate", &json!({}));
|
||||
let after = std::fs::read(&chapter).unwrap();
|
||||
assert!(
|
||||
before == after,
|
||||
"book.paginate rewrote imported IDML: {result:?}; IDML readable after {}, native readable after {}",
|
||||
designcraft_idml::import_idml(&after).is_ok(),
|
||||
designcraft_format::load(&after).is_ok()
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn style_sync_does_not_replace_imported_idml_with_native_format() {
|
||||
let d = dir("idml-sync");
|
||||
let source = d.path().join("source.designcraft");
|
||||
let chapter = d.path().join("chapter.idml");
|
||||
native(&source, 1, true);
|
||||
idml(&chapter, 1);
|
||||
let mut s = book(d.path(), &[&source, &chapter]);
|
||||
let before = std::fs::read(&chapter).unwrap();
|
||||
let result = s.execute("book.syncStyles", &json!({}));
|
||||
let after = std::fs::read(&chapter).unwrap();
|
||||
assert!(
|
||||
before == after,
|
||||
"book.syncStyles rewrote imported IDML: {result:?}; IDML readable after {}, native readable after {}",
|
||||
designcraft_idml::import_idml(&after).is_ok(),
|
||||
designcraft_format::load(&after).is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_open_unsaved_chapter_blocks_batch_writes_before_any_files_change() {
|
||||
let d = dir("open-unsaved");
|
||||
let first = d.path().join("first.designcraft");
|
||||
let later = d.path().join("later.designcraft");
|
||||
native(&first, 9, false);
|
||||
native(&later, 5, false);
|
||||
let mut s = book(d.path(), &[&first, &later]);
|
||||
s.execute("file.open", &json!({"path":later.to_string_lossy()})).unwrap();
|
||||
s.execute("frame.create", &json!({"rect":[36,36,200,100],"content":"text","text":"Unsaved chapter edit"})).unwrap();
|
||||
let first_before = std::fs::read(&first).unwrap();
|
||||
let later_before = std::fs::read(&later).unwrap();
|
||||
let revision = s.doc().unwrap().revision;
|
||||
let result = s.execute("book.paginate", &json!({}));
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"book.paginate accepted an open dirty chapter: {result:?}; first changed {}, open backing file changed {}",
|
||||
std::fs::read(&first).unwrap() != first_before,
|
||||
std::fs::read(&later).unwrap() != later_before
|
||||
);
|
||||
assert_eq!(std::fs::read(&first).unwrap(), first_before);
|
||||
assert_eq!(std::fs::read(&later).unwrap(), later_before);
|
||||
assert_eq!(s.doc().unwrap().revision, revision);
|
||||
assert!(s.doc().unwrap().is_dirty());
|
||||
let e = result.unwrap_err().to_string();
|
||||
assert!(e.contains("later.designcraft") && e.contains("close"), "{e}");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn an_alias_of_an_open_chapter_blocks_all_batch_writes() {
|
||||
let d = dir("open-alias");
|
||||
let first = d.path().join("first.designcraft");
|
||||
let real = d.path().join("real.designcraft");
|
||||
let alias = d.path().join("alias.designcraft");
|
||||
native(&first, 9, false);
|
||||
native(&real, 5, false);
|
||||
std::os::unix::fs::symlink("real.designcraft", &alias).unwrap();
|
||||
let mut s = book(d.path(), &[&first, &alias]);
|
||||
s.execute("file.open", &json!({"path":real.to_string_lossy()})).unwrap();
|
||||
let first_before = std::fs::read(&first).unwrap();
|
||||
let real_before = std::fs::read(&real).unwrap();
|
||||
let result = s.execute("book.paginate", &json!({}));
|
||||
assert!(result.is_err(), "book.paginate accepted alias of open chapter: {result:?}");
|
||||
assert_eq!(std::fs::read(&first).unwrap(), first_before);
|
||||
assert_eq!(std::fs::read(&real).unwrap(), real_before);
|
||||
let e = result.unwrap_err().to_string();
|
||||
assert!(e.contains("alias.designcraft") && e.contains("close"), "{e}");
|
||||
assert!(std::fs::symlink_metadata(alias).unwrap().file_type().is_symlink());
|
||||
}
|
||||
#[test]
|
||||
fn imported_idml_remains_a_read_only_style_source() {
|
||||
let d = dir("idml-style-source");
|
||||
let source = d.path().join("source.idml");
|
||||
let target = d.path().join("target.designcraft");
|
||||
std::fs::write(&source, designcraft_idml::export_idml(&document(1, true))).unwrap();
|
||||
native(&target, 1, false);
|
||||
let mut s = book(d.path(), &[&source, &target]);
|
||||
let before = std::fs::read(&source).unwrap();
|
||||
let result = s.execute("book.syncStyles", &json!({})).unwrap();
|
||||
assert_eq!(result["synced"], 1);
|
||||
assert_eq!(std::fs::read(&source).unwrap(), before);
|
||||
assert!(designcraft_idml::import_idml(&before).is_ok());
|
||||
let target = designcraft_format::load(&std::fs::read(target).unwrap()).unwrap();
|
||||
assert!(target.styles.paragraph.iter().any(|s| s.name == "Synthetic Book Style"));
|
||||
}
|
||||
#[test]
|
||||
fn imported_idml_still_exports_to_book_pdf_without_source_changes() {
|
||||
let d = dir("idml-pdf");
|
||||
let chapter = d.path().join("chapter.idml");
|
||||
idml(&chapter, 1);
|
||||
let mut s = book(d.path(), &[&chapter]);
|
||||
let before = std::fs::read(&chapter).unwrap();
|
||||
let result = s.execute("book.exportPdf", &json!({})).unwrap();
|
||||
assert_eq!(result["pages"], 2);
|
||||
assert!(result["base64"].as_str().is_some_and(|s| !s.is_empty()));
|
||||
assert_eq!(std::fs::read(chapter).unwrap(), before);
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
//! Character Styles panel's existing `fromSelection` command contract.
|
||||
use designcraft_doc::{CharAttrs, StoryId};
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
fn story(text: &str) -> (Session, u64) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let frame = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text", "text": text})).unwrap();
|
||||
(s, frame["story"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn select(s: &mut Session, sid: u64, anchor: usize, focus: usize) {
|
||||
s.execute("text.select", &json!({"story": sid, "anchor": anchor, "focus": focus})).unwrap();
|
||||
}
|
||||
|
||||
fn chars(s: &Session, name: &str) -> CharAttrs {
|
||||
s.doc().unwrap().doc.styles.char_style(name).unwrap().chars.clone()
|
||||
}
|
||||
|
||||
fn document(s: &Session) -> Value {
|
||||
serde_json::to_value(s.doc().unwrap().doc.as_ref()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_overlays_style_chain_local_and_explicit_attrs_but_inherits_paragraph() {
|
||||
let (mut s, sid) = story("alpha\nbeta");
|
||||
s.execute("style.paragraph.create", &json!({"name": "Source paragraph", "chars": {"size": 18}})).unwrap();
|
||||
s.execute("style.paragraph.create", &json!({"name": "Destination", "chars": {"size": 30, "underline": true}})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Base", "chars": {"underline": true, "tracking": 10, "strikethrough": true}})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Emphasis", "basedOn": "Base", "chars": {"fontStyle": "Italic"}})).unwrap();
|
||||
select(&mut s, sid, 0, 5);
|
||||
s.execute("style.paragraph.apply", &json!({"name": "Source paragraph"})).unwrap();
|
||||
s.execute("style.character.apply", &json!({"name": "Emphasis"})).unwrap();
|
||||
s.execute("type.char", &json!({"tracking": 20, "underline": false})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Captured", "fromSelection": true, "chars": {"tracking": 7}})).unwrap();
|
||||
assert_eq!(
|
||||
chars(&s, "Captured"),
|
||||
CharAttrs { font_style: Some("Italic".into()), underline: Some(false), tracking: Some(7.0), strikethrough: Some(true), ..Default::default() }
|
||||
);
|
||||
select(&mut s, sid, 6, 10);
|
||||
s.execute("style.paragraph.apply", &json!({"name": "Destination"})).unwrap();
|
||||
s.execute("style.character.apply", &json!({"name": "Captured"})).unwrap();
|
||||
let resolved = s.execute("type.selectionAttrs", &json!({})).unwrap();
|
||||
assert_eq!(resolved["chars"]["fontStyle"], "Italic");
|
||||
assert_eq!(resolved["chars"]["strikethrough"], true);
|
||||
assert_eq!(resolved["chars"]["underline"], false);
|
||||
assert_eq!(resolved["chars"]["tracking"], 7.0);
|
||||
assert_eq!(resolved["chars"]["size"], 30.0);
|
||||
s.execute("style.paragraph.edit", &json!({"name": "Destination", "chars": {"size": 34}})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["size"], 34.0);
|
||||
// The captured attributes are a snapshot, independent of future source-style edits.
|
||||
s.execute("style.character.edit", &json!({"name": "Emphasis", "chars": {"fontStyle": "Bold"}})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fontStyle"], "Italic");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_reproducer_is_one_undo_step_and_survives_native_reopen() {
|
||||
let (mut s, sid) = story("alpha beta");
|
||||
select(&mut s, sid, 0, 5);
|
||||
s.execute("type.char", &json!({"underline": true})).unwrap();
|
||||
let before = document(&s);
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let undo_len = s.doc().unwrap().history.undo.len();
|
||||
let source = serde_json::to_value(s.doc().unwrap().doc.story(StoryId(sid)).unwrap()).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Captured", "fromSelection": true})).unwrap();
|
||||
assert_eq!(chars(&s, "Captured"), CharAttrs { underline: Some(true), ..Default::default() });
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len + 1);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
assert_eq!(serde_json::to_value(s.doc().unwrap().doc.story(StoryId(sid)).unwrap()).unwrap(), source);
|
||||
let after = document(&s);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(document(&s), before);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(document(&s), after);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
|
||||
// Both commands use the native ZIP format, without a filesystem fixture.
|
||||
let saved = s.execute("file.serialize", &json!({})).unwrap();
|
||||
let mut reopened = Session::new();
|
||||
reopened.execute("file.openBytes", &json!({"name": "capture.designcraft", "base64": saved["base64"]})).unwrap();
|
||||
assert_eq!(chars(&reopened, "Captured"), chars(&s, "Captured"));
|
||||
select(&mut reopened, sid, 6, 10);
|
||||
assert_eq!(reopened.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["underline"], false);
|
||||
let before_apply = document(&reopened);
|
||||
reopened.execute("style.character.apply", &json!({"name": "Captured"})).unwrap();
|
||||
assert_eq!(reopened.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["underline"], true);
|
||||
assert_eq!(reopened.doc().unwrap().doc.story(StoryId(sid)).unwrap().text, "alpha beta");
|
||||
let after_apply = document(&reopened);
|
||||
reopened.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(document(&reopened), before_apply);
|
||||
reopened.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(document(&reopened), after_apply);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_matches_paragraph_selection_and_caret_boundary_policy() {
|
||||
let (mut s, sid) = story("éX");
|
||||
select(&mut s, sid, 0, 2);
|
||||
s.execute("type.char", &json!({"underline": true})).unwrap();
|
||||
select(&mut s, sid, 2, 3);
|
||||
s.execute("type.char", &json!({"underline": false})).unwrap();
|
||||
// A selected character takes the run on the right; a caret takes the typing run on the left.
|
||||
// Reversed and mixed selections start at the lowest byte offset, just like paragraph capture.
|
||||
for (anchor, focus, underlined) in [(3, 2, false), (2, 2, true), (3, 0, true)] {
|
||||
select(&mut s, sid, anchor, focus);
|
||||
let p = s.execute("style.paragraph.create", &json!({"name": "Paragraph", "fromSelection": true})).unwrap();
|
||||
let c = s.execute("style.character.create", &json!({"name": "Character", "fromSelection": true})).unwrap();
|
||||
let p = s.doc().unwrap().doc.styles.para(p["name"].as_str().unwrap()).unwrap();
|
||||
let c = chars(&s, c["name"].as_str().unwrap());
|
||||
assert_eq!(c.underline, Some(underlined));
|
||||
assert_eq!(c.underline, p.chars.underline);
|
||||
assert_eq!(c.size, None, "unlike paragraph capture, character capture must not freeze paragraph defaults");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_is_opt_in_and_no_text_uses_only_explicit_chars() {
|
||||
let (mut s, sid) = story("alpha");
|
||||
select(&mut s, sid, 0, 5);
|
||||
s.execute("type.char", &json!({"underline": true})).unwrap();
|
||||
for options in [json!({"name": "Plain"}), json!({"name": "Plain", "fromSelection": false})] {
|
||||
let created = s.execute("style.character.create", &options).unwrap();
|
||||
assert!(chars(&s, created["name"].as_str().unwrap()).is_empty());
|
||||
}
|
||||
assert!(s.doc().unwrap().doc.styles.char_style("Plain 2").is_some());
|
||||
s.execute("selection.set", &json!({"ids": []})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "No target", "fromSelection": true, "basedOn": "Plain", "chars": {"underline": false}}))
|
||||
.unwrap();
|
||||
assert_eq!(chars(&s, "No target"), CharAttrs { underline: Some(false), ..Default::default() });
|
||||
assert_eq!(s.doc().unwrap().doc.styles.char_style("No target").unwrap().based_on.as_deref(), Some("Plain"));
|
||||
let (mut empty, empty_sid) = story("");
|
||||
select(&mut empty, empty_sid, 0, 0);
|
||||
empty.execute("style.character.create", &json!({"name": "Empty", "fromSelection": true})).unwrap();
|
||||
assert!(chars(&empty, "Empty").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_retains_deleted_fill_and_reuses_it_after_native_reopen() {
|
||||
for named_source in [false, true] {
|
||||
let (mut s, sid) = story("alpha beta gamma");
|
||||
s.execute("swatch.create", &json!({"name": "Ink", "color": "#ff0000"})).unwrap();
|
||||
select(&mut s, sid, 0, 5);
|
||||
if named_source {
|
||||
s.execute("style.character.create", &json!({"name": "Base", "chars": {"fill": "Ink"}})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Source", "basedOn": "Base"})).unwrap();
|
||||
s.execute("style.character.apply", &json!({"name": "Source"})).unwrap();
|
||||
} else {
|
||||
s.execute("type.char", &json!({"fill": "Ink"})).unwrap();
|
||||
}
|
||||
s.execute("swatch.delete", &json!({"name": "Ink"})).unwrap();
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
let before = document(&s);
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let undo_len = s.doc().unwrap().history.undo.len();
|
||||
s.execute("style.character.create", &json!({"name": "Captured", "fromSelection": true})).unwrap();
|
||||
assert_eq!(chars(&s, "Captured"), CharAttrs { fill: Some("Ink".into()), ..Default::default() });
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len + 1);
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
let after = document(&s);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(document(&s), before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(document(&s), after);
|
||||
// Non-fill overrides do not turn an existing captured reference into explicit input.
|
||||
s.execute("style.character.create", &json!({"name": "Tweaked", "fromSelection": true, "chars": {"underline": false, "tracking": 0}}))
|
||||
.unwrap();
|
||||
assert_eq!(chars(&s, "Tweaked"), CharAttrs { fill: Some("Ink".into()), underline: Some(false), tracking: Some(0.0), ..Default::default() });
|
||||
s.execute("style.character.create", &json!({"name": "Paper", "fromSelection": true, "chars": {"fill": "[Paper]"}})).unwrap();
|
||||
assert_eq!(chars(&s, "Paper"), CharAttrs { fill: Some("[Paper]".into()), ..Default::default() });
|
||||
s.execute("style.character.create", &json!({"name": "Cleared fill", "fromSelection": true, "chars": {"fill": null}})).unwrap();
|
||||
assert!(chars(&s, "Cleared fill").is_empty());
|
||||
select(&mut s, sid, 6, 10);
|
||||
s.execute("style.character.apply", &json!({"name": "Captured"})).unwrap();
|
||||
assert_eq!(s.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], "Ink");
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
|
||||
let saved = s.execute("file.serialize", &json!({})).unwrap();
|
||||
let mut reopened = Session::new();
|
||||
reopened.execute("file.openBytes", &json!({"name": "deleted-fill.designcraft", "base64": saved["base64"]})).unwrap();
|
||||
reopened.doc().unwrap().doc.check().unwrap();
|
||||
for name in ["Captured", "Tweaked", "Paper", "Cleared fill"] {
|
||||
assert_eq!(chars(&reopened, name), chars(&s, name));
|
||||
}
|
||||
select(&mut reopened, sid, 6, 10);
|
||||
assert_eq!(reopened.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], "Ink");
|
||||
select(&mut reopened, sid, 11, 16);
|
||||
assert_eq!(reopened.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], "[Black]");
|
||||
reopened.execute("style.character.apply", &json!({"name": "Captured"})).unwrap();
|
||||
assert_eq!(reopened.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], "Ink");
|
||||
reopened.execute("style.character.apply", &json!({"name": "Cleared fill"})).unwrap();
|
||||
assert_eq!(reopened.execute("type.selectionAttrs", &json!({})).unwrap()["chars"]["fill"], "[Black]");
|
||||
assert_eq!(reopened.doc().unwrap().doc.story(StoryId(sid)).unwrap().text, "alpha beta gamma");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_rejects_explicit_missing_fills_without_losing_redo() {
|
||||
let (mut s, sid) = story("alpha");
|
||||
s.execute("swatch.create", &json!({"name": "Ink", "color": "#ff0000"})).unwrap();
|
||||
select(&mut s, sid, 0, 5);
|
||||
s.execute("type.char", &json!({"fill": "Ink"})).unwrap();
|
||||
s.execute("swatch.delete", &json!({"name": "Ink"})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Redo sentinel"})).unwrap();
|
||||
let redo_doc = s.doc().unwrap().doc.clone();
|
||||
let redo_selection = s.doc().unwrap().selection.clone();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let before = s.doc().unwrap().clone();
|
||||
let journal = s.journal.clone();
|
||||
for (capture, fill) in [(true, "Missing"), (true, "Ink"), (false, "Missing")] {
|
||||
let error = s.execute("style.character.create", &json!({"name": "Invalid", "fromSelection": capture, "chars": {"fill": fill}})).unwrap_err();
|
||||
assert!(error.to_string().contains(fill));
|
||||
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().doc, &before.doc));
|
||||
assert_eq!(s.doc().unwrap().selection, before.selection);
|
||||
assert_eq!(s.doc().unwrap().revision, before.revision);
|
||||
assert_eq!(format!("{:?}", s.doc().unwrap().history), format!("{:?}", before.history));
|
||||
assert_eq!(s.journal, journal);
|
||||
}
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().doc, &redo_doc));
|
||||
assert_eq!(s.doc().unwrap().selection, redo_selection);
|
||||
assert!(s.doc().unwrap().doc.styles.char_style("Redo sentinel").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_capture_explicit_null_restores_live_paragraph_inheritance() {
|
||||
for named_source in [false, true] {
|
||||
let (mut s, sid) = story("alpha\nbeta");
|
||||
select(&mut s, sid, 0, 5);
|
||||
let source = json!({"tracking": 20, "underline": true, "underlineWeight": 1.5, "fontStyle": "Italic"});
|
||||
if named_source {
|
||||
s.execute("style.character.create", &json!({"name": "Base", "chars": source})).unwrap();
|
||||
s.execute("style.character.create", &json!({"name": "Source", "basedOn": "Base"})).unwrap();
|
||||
s.execute("style.character.apply", &json!({"name": "Source"})).unwrap();
|
||||
} else {
|
||||
s.execute("type.char", &source).unwrap();
|
||||
}
|
||||
// Omitting chars still captures values, including a nullable attribute's numeric value.
|
||||
s.execute("style.character.create", &json!({"name": "Unchanged", "fromSelection": true})).unwrap();
|
||||
assert_eq!(chars(&s, "Unchanged").tracking, Some(20.0));
|
||||
assert_eq!(chars(&s, "Unchanged").underline_weight, Some(Some(1.5)));
|
||||
let before = document(&s);
|
||||
let undo_len = s.doc().unwrap().history.undo.len();
|
||||
s.execute(
|
||||
"style.character.create",
|
||||
&json!({"name": "Cleared", "fromSelection": true,
|
||||
"chars": {"tracking": null, "underlineWeight": null, "underline": false, "baselineShift": 0}}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
chars(&s, "Cleared"),
|
||||
CharAttrs { font_style: Some("Italic".into()), underline: Some(false), baseline_shift: Some(0.0), ..Default::default() }
|
||||
);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len + 1);
|
||||
let after = document(&s);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(document(&s), before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(document(&s), after);
|
||||
s.execute("style.paragraph.create", &json!({"name": "Destination", "chars": {"tracking": 30, "underlineWeight": 2}})).unwrap();
|
||||
select(&mut s, sid, 6, 10);
|
||||
s.execute("style.paragraph.apply", &json!({"name": "Destination"})).unwrap();
|
||||
s.execute("style.character.apply", &json!({"name": "Cleared"})).unwrap();
|
||||
let effective = s.execute("type.selectionAttrs", &json!({})).unwrap();
|
||||
assert_eq!(effective["chars"]["tracking"], 30.0);
|
||||
assert_eq!(effective["chars"]["underlineWeight"], 2.0);
|
||||
assert_eq!(effective["chars"]["underline"], false);
|
||||
s.execute("style.paragraph.edit", &json!({"name": "Destination", "chars": {"tracking": 40, "underlineWeight": 3}})).unwrap();
|
||||
let effective = s.execute("type.selectionAttrs", &json!({})).unwrap();
|
||||
assert_eq!(effective["chars"]["tracking"], 40.0);
|
||||
assert_eq!(effective["chars"]["underlineWeight"], 3.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
fn library_failure(command: &str) {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let folder = temp.path().join("library");
|
||||
std::fs::create_dir(&folder).unwrap();
|
||||
let file = folder.join("items.dclib");
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("frame.create", &json!({"rect": [0, 0, 50, 50]})).unwrap();
|
||||
s.execute("library.new", &json!({"path": file})).unwrap();
|
||||
s.execute("library.add", &json!({"name": "Keep"})).unwrap();
|
||||
let before = serde_json::to_value(&s.library).unwrap();
|
||||
let path = s.library.as_ref().unwrap().path.clone();
|
||||
let journal = s.journal.clone();
|
||||
let original_bytes = std::fs::read(&file).unwrap();
|
||||
let kept = temp.path().join("kept-library");
|
||||
std::fs::rename(&folder, &kept).unwrap();
|
||||
|
||||
assert!(s.execute(command, &json!({"name": "New", "index": 0})).is_err());
|
||||
|
||||
assert_eq!(before, serde_json::to_value(&s.library).unwrap(), "a failed library write must retain its original items");
|
||||
assert_eq!(path, s.library.as_ref().unwrap().path);
|
||||
assert_eq!(journal, s.journal);
|
||||
assert_eq!(original_bytes, std::fs::read(kept.join("items.dclib")).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_library_add_keeps_existing_items() {
|
||||
library_failure("library.add");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_library_remove_keeps_existing_items() {
|
||||
library_failure("library.remove");
|
||||
}
|
||||
|
||||
fn book_failure(command: &str) {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let mut s = Session::new();
|
||||
let mut chapters = Vec::new();
|
||||
for i in 0..3 {
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let path = temp.path().join(format!("chapter-{i}.designcraft"));
|
||||
s.execute("file.saveAs", &json!({"path": path})).unwrap();
|
||||
chapters.push(path);
|
||||
}
|
||||
let folder = temp.path().join("book");
|
||||
std::fs::create_dir(&folder).unwrap();
|
||||
let file = folder.join("chapters.dcbook");
|
||||
s.execute("book.new", &json!({"path": file})).unwrap();
|
||||
s.execute("book.add", &json!({"path": chapters[0]})).unwrap();
|
||||
s.execute("book.add", &json!({"path": chapters[1]})).unwrap();
|
||||
s.execute("book.styleSource", &json!({"index": 1})).unwrap();
|
||||
let before = serde_json::to_value(&s.book).unwrap();
|
||||
let path = s.book.as_ref().unwrap().path.clone();
|
||||
let journal = s.journal.clone();
|
||||
let original_bytes = std::fs::read(&file).unwrap();
|
||||
let kept = temp.path().join("kept-book");
|
||||
std::fs::rename(&folder, &kept).unwrap();
|
||||
let params = if command == "book.add" { json!({"path": chapters[2]}) } else { json!({"index": 0}) };
|
||||
|
||||
assert!(s.execute(command, ¶ms).is_err());
|
||||
|
||||
assert_eq!(before, serde_json::to_value(&s.book).unwrap(), "a failed book write must retain its original chapter list and style source");
|
||||
assert_eq!(path, s.book.as_ref().unwrap().path);
|
||||
assert_eq!(journal, s.journal);
|
||||
assert_eq!(original_bytes, std::fs::read(kept.join("chapters.dcbook")).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_book_add_keeps_existing_chapters() {
|
||||
book_failure("book.add");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_book_remove_keeps_existing_chapters() {
|
||||
book_failure("book.remove");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_book_style_source_keeps_existing_source() {
|
||||
book_failure("book.styleSource");
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
//! Distribution preserves explicit item locks without changing reference/spacing calculations.
|
||||
use std::sync::Arc;
|
||||
|
||||
use designcraft_doc::ItemId;
|
||||
use designcraft_engine::Session;
|
||||
use designcraft_geom::Affine;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const STARTS: [f64; 4] = [10.0, 70.0, 150.0, 310.0];
|
||||
const SIZES: [f64; 4] = [20.0, 30.0, 40.0, 50.0];
|
||||
// An original, synthetic 1x1 RGBA PNG; used by both a locked and an unlocked frame.
|
||||
const PIXEL: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGPYYqPxHwAE2AIYBVnQbwAAAABJRU5ErkJggg==";
|
||||
|
||||
fn fixture(horizontal: bool, locks: [bool; 4]) -> (Session, Vec<u64>) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let mut ids = Vec::new();
|
||||
for (i, (&start, &size)) in STARTS.iter().zip(&SIZES).enumerate() {
|
||||
let cross = 40.0 + i as f64 * 35.0;
|
||||
let rect = if horizontal { [start, cross, start + size, cross + 25.0] } else { [cross, start, cross + 25.0, start + size] };
|
||||
let graphic = i == 1 || i == 2;
|
||||
let created =
|
||||
s.execute("frame.create", &json!({"rect": rect, "content": if graphic {"unassigned"} else {"text"}, "text": "Keep this text"})).unwrap();
|
||||
let id = created["id"].as_u64().unwrap();
|
||||
ids.push(id);
|
||||
if graphic {
|
||||
s.execute("file.place", &json!({"frame": id, "base64": PIXEL, "name": "pixel.png"})).unwrap();
|
||||
}
|
||||
}
|
||||
// An unselected control must retain all geometry and content too.
|
||||
s.execute("frame.create", &json!({"rect": [400, 400, 450, 450], "content": "text", "text": "Outside selection"})).unwrap();
|
||||
s.execute("object.fill", &json!({"ids": ids, "swatch": "[Black]", "tint": 0.5})).unwrap();
|
||||
let locked: Vec<u64> = ids.iter().zip(locks).filter_map(|(&id, lock)| lock.then_some(id)).collect();
|
||||
if !locked.is_empty() {
|
||||
s.execute("object.lock", &json!({"ids": locked})).unwrap();
|
||||
}
|
||||
// Layers selection may include locked items. Deliberately avoid geometric order.
|
||||
s.execute("selection.set", &json!({"ids": [ids[3], ids[1], ids[0], ids[2]]})).unwrap();
|
||||
assert!(!s.doc().unwrap().doc.assets.is_empty());
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
(s, ids)
|
||||
}
|
||||
|
||||
fn modes() -> [(Value, [f64; 4]); 3] {
|
||||
[
|
||||
// Endpoint centers 20 and 335 give a center step of 105.
|
||||
(json!({"by": "centers"}), [10.0, 110.0, 210.0, 310.0]),
|
||||
// Outer span 350 minus total widths 140 gives three gaps of 70.
|
||||
(json!({"by": "spacing"}), [10.0, 100.0, 200.0, 310.0]),
|
||||
// An explicit spacing value selects fixed-gap mode without requiring `by`.
|
||||
(json!({"spacing": 15}), [10.0, 45.0, 90.0, 145.0]),
|
||||
]
|
||||
}
|
||||
|
||||
fn check_distribution(horizontal: bool, mut params: Value, positions: [f64; 4], locks: [bool; 4], explicit_ids: bool) {
|
||||
let (mut s, ids) = fixture(horizontal, locks);
|
||||
params["axis"] = json!(if horizontal { "horizontal" } else { "vertical" });
|
||||
if explicit_ids {
|
||||
params["ids"] = json!([ids[2], ids[0], ids[3], ids[1]]);
|
||||
}
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
let selection = s.doc().unwrap().selection.clone();
|
||||
let undo_len = s.doc().unwrap().history.undo.len();
|
||||
let mut expected = (*before).clone();
|
||||
for (i, &id) in ids.iter().enumerate() {
|
||||
if !locks[i] {
|
||||
let delta = positions[i] - STARTS[i];
|
||||
let translation = if horizontal { (delta, 0.0) } else { (0.0, delta) };
|
||||
let item = expected.item_mut(ItemId(id)).unwrap();
|
||||
item.xf = Affine::translate(translation) * item.xf;
|
||||
}
|
||||
}
|
||||
s.execute("object.distribute", ¶ms).unwrap();
|
||||
let after = s.doc().unwrap().doc.clone();
|
||||
for (i, &id) in ids.iter().enumerate() {
|
||||
let item = after.item(ItemId(id)).unwrap();
|
||||
let b = item.bounds();
|
||||
let start = if horizontal { b.x0 } else { b.y0 };
|
||||
assert_eq!(start, if locks[i] { STARTS[i] } else { positions[i] }, "item {i}: {params}, locks={locks:?}");
|
||||
if locks[i] {
|
||||
assert_eq!(item, before.item(ItemId(id)).unwrap(), "locked item's transform, path, content and flags are unchanged");
|
||||
}
|
||||
}
|
||||
// Only the specified translations may change: all paths, sizes, other-axis coordinates,
|
||||
// paint, content, stories, layer membership, item order and embedded asset bytes are exact.
|
||||
assert_eq!(*after, expected, "{params}, locks={locks:?}");
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
after.check().unwrap();
|
||||
if expected != *before {
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo_len + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &before));
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &after));
|
||||
assert_eq!(s.doc().unwrap().selection, selection);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distribute_preserves_locked_items_and_moves_unlocked_controls() {
|
||||
for horizontal in [true, false] {
|
||||
for (params, positions) in modes() {
|
||||
// Locked interior object still participates in the numeric reference calculation.
|
||||
check_distribution(horizontal, params.clone(), positions, [false, true, false, false], false);
|
||||
// Locked endpoints and an interior object; only the unlocked interior control moves.
|
||||
check_distribution(horizontal, params, positions, [true, true, false, true], true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distribute_unlocked_spacing_modes_are_unchanged() {
|
||||
for horizontal in [true, false] {
|
||||
for (params, positions) in modes() {
|
||||
check_distribution(horizontal, params, positions, [false; 4], true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distribute_all_locked_leaves_document_and_selection_unchanged() {
|
||||
for horizontal in [true, false] {
|
||||
for (params, positions) in modes() {
|
||||
check_distribution(horizontal, params, positions, [true; 4], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
//! Effective raster resolution follows image, frame and ancestor transforms in spread space.
|
||||
use std::sync::Arc;
|
||||
|
||||
use designcraft_doc::{Content, Intent, ItemId};
|
||||
use designcraft_engine::{Session, cmd::base64_encode};
|
||||
use designcraft_geom::Affine;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
fn place(s: &mut Session, name: &str, bytes: &[u8]) -> ItemId {
|
||||
s.execute("selection.set", &json!({"ids": []})).unwrap();
|
||||
let placed = s.execute("file.place", &json!({"base64": base64_encode(bytes), "name": name, "x": 0, "y": 0, "width": 144})).unwrap();
|
||||
ItemId(placed["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn raster() -> (Session, ItemId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 1000, "height": 1000, "facingPages": false})).unwrap();
|
||||
// 600 × 300 pixels at 144 × 72 points: 300 ppi in both directions.
|
||||
let png = designcraft_render::Rendered { width: 600, height: 300, pixels: vec![200; 600 * 300 * 4] }.to_png();
|
||||
let id = place(&mut s, "photo.png", &png);
|
||||
assert_eq!(s.doc().unwrap().doc.item(id).unwrap().inner_bounds().size(), designcraft_geom::Size::new(144.0, 72.0));
|
||||
(s, id)
|
||||
}
|
||||
|
||||
fn facing_raster() -> (Session, ItemId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 612, "height": 792, "pages": 3, "facingPages": true})).unwrap();
|
||||
// Page 0 is the cover; spread 1 contains absolute pages 1 (left) and 2 (right).
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert_eq!(d.spreads.len(), 2);
|
||||
assert_eq!(d.spreads[0].pages.len(), 1);
|
||||
assert_eq!(d.spreads[1].pages.len(), 2);
|
||||
assert_eq!(d.first_page_of_spread(1), 1);
|
||||
assert_eq!(d.spreads[1].pages[0].x, 0.0);
|
||||
assert_eq!(d.spreads[1].pages[1].x, 612.0);
|
||||
let png = designcraft_render::Rendered { width: 600, height: 300, pixels: vec![200; 600 * 300 * 4] }.to_png();
|
||||
let placed = s
|
||||
.execute(
|
||||
"file.place",
|
||||
&json!({
|
||||
"base64": base64_encode(&png), "name": "photo.png", "spread": 1,
|
||||
"x": 0, "y": 0, "width": 144
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
let id = ItemId(placed["id"].as_u64().unwrap());
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert_eq!(d.find(id).unwrap().spread, designcraft_doc::SpreadRef::Doc(1));
|
||||
assert_eq!(d.item(id).unwrap().inner_bounds().size(), designcraft_geom::Size::new(144.0, 72.0));
|
||||
(s, id)
|
||||
}
|
||||
|
||||
fn transform(s: &mut Session, id: ItemId, frame: Affine, image: Affine) {
|
||||
s.edit(|d, _| {
|
||||
let it = d.item_mut(id).unwrap();
|
||||
it.xf = frame;
|
||||
let Content::Graphic(g) = &mut it.content else { panic!("graphic fixture") };
|
||||
g.xf = image * g.xf;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn group(s: &mut Session, child: ItemId, xf: Affine) -> ItemId {
|
||||
let spread = s.doc().unwrap().doc.find(child).unwrap().spread;
|
||||
let marker = s.execute("frame.create", &json!({"spread": spread, "rect": [0, 0, 1, 1]})).unwrap()["id"].clone();
|
||||
let group = s.execute("object.group", &json!({"ids": [child.0, marker]})).unwrap();
|
||||
let id = ItemId(group["id"].as_u64().unwrap());
|
||||
s.edit(|d, _| {
|
||||
d.item_mut(id).unwrap().xf = xf;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
fn resolutions(s: &mut Session, params: Value) -> Vec<Value> {
|
||||
let before = s.doc().unwrap();
|
||||
let doc = before.doc.clone();
|
||||
let selection = before.selection.clone();
|
||||
let state = (before.revision, before.history.undo.len(), before.history.redo.len(), s.journal.len());
|
||||
let result = s.execute("preflight.run", ¶ms).unwrap();
|
||||
let after = s.doc().unwrap();
|
||||
assert!(Arc::ptr_eq(&doc, &after.doc), "preflight must leave the document and assets untouched");
|
||||
assert_eq!(selection, after.selection, "preflight must leave the selection untouched");
|
||||
assert_eq!(state, (after.revision, after.history.undo.len(), after.history.redo.len(), s.journal.len()));
|
||||
result["issues"].as_array().unwrap().iter().filter(|i| i["kind"] == "lowResolution").cloned().collect()
|
||||
}
|
||||
|
||||
fn warning(s: &mut Session, id: ItemId, params: Value, ppi: u32, threshold: u32) {
|
||||
warning_on_page(s, id, params, ppi, threshold, 0);
|
||||
}
|
||||
|
||||
fn warning_on_page(s: &mut Session, id: ItemId, params: Value, ppi: u32, threshold: u32, page: usize) {
|
||||
assert_eq!(
|
||||
resolutions(s, params),
|
||||
vec![json!({
|
||||
"severity": "warning", "kind": "lowResolution", "item": id.0, "page": page,
|
||||
"message": format!("photo.png: effective {ppi} ppi (< {threshold})")
|
||||
})]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn either_raster_axis_can_fail_after_image_or_frame_scaling() {
|
||||
for scale in [Affine::scale_non_uniform(4.0, 1.0), Affine::scale_non_uniform(1.0, 4.0)] {
|
||||
for image_only in [false, true] {
|
||||
let (mut s, id) = raster();
|
||||
let (frame, image) = if image_only { (Affine::IDENTITY, scale) } else { (scale, Affine::IDENTITY) };
|
||||
transform(&mut s, id, frame, image);
|
||||
// One axis remains 300 ppi, the other is 75 ppi: report the worse axis once.
|
||||
warning(&mut s, id, json!({}), 75, 150);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_groups_accumulate_scale_and_keep_image_frame_parent_order() {
|
||||
let (mut s, id) = raster();
|
||||
let inner = group(&mut s, id, Affine::scale(2.0));
|
||||
group(&mut s, inner, Affine::scale(2.0));
|
||||
warning(&mut s, id, json!({}), 75, 150);
|
||||
|
||||
let (mut s, id) = raster();
|
||||
// Double image X, then turn the frame 90 degrees, then triple spread X.
|
||||
// The image axes become 288 and 216 pt, giving 150 and 100 ppi respectively.
|
||||
transform(&mut s, id, Affine::new([0.0, 1.0, -1.0, 0.0, 400.0, 20.0]), Affine::scale_non_uniform(2.0, 1.0));
|
||||
group(&mut s, id, Affine::scale_non_uniform(3.0, 1.0));
|
||||
warning(&mut s, id, json!({}), 100, 150);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translated_scaled_nested_raster_reports_right_facing_page() {
|
||||
let (mut s, id) = facing_raster();
|
||||
let inner = group(&mut s, id, Affine::scale(2.0));
|
||||
group(&mut s, inner, Affine::translate((640.0, 0.0)) * Affine::scale(2.0));
|
||||
// Local frame center x=72 is on the left page. Ancestors move its bounds to
|
||||
// x=640..1216 on the right page, while scaling 300 ppi down to 75 ppi.
|
||||
warning_on_page(&mut s, id, json!({}), 75, 150, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translating_group_changes_warning_page_without_changing_resolution() {
|
||||
let (mut s, id) = facing_raster();
|
||||
// A nonidentity frame transform also detects applying the frame transform twice.
|
||||
transform(&mut s, id, Affine::translate((-240.0, 0.0)), Affine::IDENTITY);
|
||||
warning_on_page(&mut s, id, json!({"minPpi": 301}), 300, 301, 1);
|
||||
group(&mut s, id, Affine::translate((880.0, 0.0)));
|
||||
// Translation moves the center to x=712, but does not change either image axis.
|
||||
assert!(resolutions(&mut s, json!({})).is_empty());
|
||||
warning_on_page(&mut s, id, json!({"minPpi": 301}), 300, 301, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotation_translation_and_reflection_do_not_change_resolution() {
|
||||
for xf in [
|
||||
Affine::translate((250.0, 300.0)) * Affine::rotate(std::f64::consts::FRAC_PI_4),
|
||||
Affine::translate((250.0, 300.0)) * Affine::scale_non_uniform(-1.0, 1.0),
|
||||
] {
|
||||
let (mut s, id) = raster();
|
||||
transform(&mut s, id, xf, Affine::IDENTITY);
|
||||
// Measuring the rotated bounding box instead would incorrectly warn here.
|
||||
assert!(resolutions(&mut s, json!({"minPpi": 250})).is_empty());
|
||||
warning(&mut s, id, json!({"minPpi": 301}), 300, 301);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cropping_keeps_resolution_and_fitting_changes_it_with_content_scale() {
|
||||
let (mut s, id) = raster();
|
||||
s.execute("transform.resize", &json!({"ids": [id.0], "from": [0, 0, 144, 72], "to": [0, 0, 36, 18]})).unwrap();
|
||||
warning(&mut s, id, json!({"minPpi": 301}), 300, 301);
|
||||
s.execute("transform.resize", &json!({"ids": [id.0], "from": [0, 0, 36, 18], "to": [0, 0, 144, 216]})).unwrap();
|
||||
warning(&mut s, id, json!({"minPpi": 301}), 300, 301);
|
||||
|
||||
s.execute("object.fit", &json!({"ids": [id.0], "mode": "fitProportionally"})).unwrap();
|
||||
warning(&mut s, id, json!({"minPpi": 301}), 300, 301);
|
||||
// Stretching just Y to 216 pt leaves X at 300 ppi but reduces Y to 100 ppi.
|
||||
s.execute("object.fit", &json!({"ids": [id.0], "mode": "fitContentToFrame"})).unwrap();
|
||||
warning(&mut s, id, json!({}), 100, 150);
|
||||
// Proportional fill is 432 × 216 pt: 100 ppi in both directions.
|
||||
s.execute("object.fit", &json!({"ids": [id.0], "mode": "fillProportionally"})).unwrap();
|
||||
warning(&mut s, id, json!({}), 100, 150);
|
||||
s.execute("object.fit", &json!({"ids": [id.0], "mode": "centerContent"})).unwrap();
|
||||
warning(&mut s, id, json!({}), 100, 150);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn threshold_is_strict_and_resolution_remains_print_only() {
|
||||
let (mut s, id) = raster();
|
||||
transform(&mut s, id, Affine::scale_non_uniform(1.0, 2.0), Affine::IDENTITY);
|
||||
assert!(resolutions(&mut s, json!({})).is_empty(), "150 ppi meets the default threshold");
|
||||
assert!(resolutions(&mut s, json!({"minPpi": 149})).is_empty());
|
||||
warning(&mut s, id, json!({"minPpi": 151}), 150, 151);
|
||||
for intent in [Intent::Web, Intent::Mobile] {
|
||||
s.edit(|d, _| {
|
||||
d.settings.intent = intent;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert!(resolutions(&mut s, json!({"minPpi": 151})).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transformed_vector_previews_are_excluded_beside_rasters() {
|
||||
let (mut s, id) = raster();
|
||||
let svg = br#"<svg xmlns="http://www.w3.org/2000/svg" width="144" height="72"><rect width="144" height="72" fill="red"/></svg>"#;
|
||||
let vector = place(&mut s, "vector.svg", svg);
|
||||
let parent = s.execute("object.group", &json!({"ids": [id.0, vector.0]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.edit(|d, _| {
|
||||
d.item_mut(ItemId(parent)).unwrap().xf = Affine::scale_non_uniform(1.0, 4.0);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
warning(&mut s, id, json!({}), 75, 150);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
//! Primary text frames retain all columns supported by the new-document command.
|
||||
use designcraft_doc::Document;
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
fn assert_primary_columns(doc: &Document, expected: u32) {
|
||||
let mut frames = 0;
|
||||
for story in doc.stories.values() {
|
||||
for spec in designcraft_compose::frame_specs(doc, story.id) {
|
||||
assert_eq!(spec.opts.columns, expected);
|
||||
let columns = spec.columns();
|
||||
assert_eq!(columns.len(), expected as usize);
|
||||
assert!(columns.iter().all(|r| r.width() > 0. && [r.x0, r.x1, r.y0, r.y1].into_iter().all(f64::is_finite)));
|
||||
frames += 1;
|
||||
}
|
||||
}
|
||||
assert!(frames > 0, "the primary text frame must exist");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn primary_columns_survive_composition_native_reopen_and_idml_round_trip() {
|
||||
for columns in [41, 216] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"columns":columns,"gutter":0,"primaryTextFrame":true})).unwrap();
|
||||
assert_primary_columns(&s.doc().unwrap().doc, columns);
|
||||
let bytes = s.execute("file.serialize", &json!({})).unwrap();
|
||||
let mut reopened = Session::new();
|
||||
reopened.execute("file.openBytes", &json!({"name":"primary-columns.designcraft","base64":bytes["base64"]})).unwrap();
|
||||
assert_primary_columns(&reopened.doc().unwrap().doc, columns);
|
||||
let idml = designcraft_idml::export_idml(&reopened.doc().unwrap().doc);
|
||||
let imported = designcraft_idml::import_idml(&idml).unwrap();
|
||||
assert_primary_columns(&imported, columns);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
fn linked_document(root: &Path) -> (Session, std::path::PathBuf) {
|
||||
let source = root.join("source/contacts.csv");
|
||||
std::fs::create_dir_all(source.parent().unwrap()).unwrap();
|
||||
std::fs::write(&source, "Name\nAda\n").unwrap();
|
||||
let original = root.join("original/template.designcraft");
|
||||
std::fs::create_dir_all(original.parent().unwrap()).unwrap();
|
||||
let mut session = Session::new();
|
||||
session.execute("file.new", &json!({})).unwrap();
|
||||
session.execute("data.source.select", &json!({"path": source})).unwrap();
|
||||
session.execute("file.saveAs", &json!({"path": original})).unwrap();
|
||||
(session, original)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_save_as_preserves_document_saved_state_history_and_original_file() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let (mut session, original) = linked_document(temp.path());
|
||||
session.execute("frame.create", &json!({"rect": [0, 0, 50, 50]})).unwrap();
|
||||
session.execute("edit.undo", &json!({})).unwrap();
|
||||
let before = session.doc().unwrap().clone();
|
||||
let journal = session.journal.clone();
|
||||
let original_bytes = std::fs::read(&original).unwrap();
|
||||
let destination = temp.path().join("missing/elsewhere/template.designcraft");
|
||||
|
||||
assert!(session.execute("file.saveAs", &json!({"path": destination})).is_err());
|
||||
|
||||
let after = session.doc().unwrap();
|
||||
assert!(Arc::ptr_eq(&before.doc, &after.doc), "failed Save As must not change data-source paths");
|
||||
assert!(Arc::ptr_eq(&before.saved_doc, &after.saved_doc));
|
||||
assert_eq!(before.path, after.path);
|
||||
assert_eq!((before.revision, before.saved_revision, before.is_dirty()), (after.revision, after.saved_revision, after.is_dirty()));
|
||||
assert_eq!(before.doc.data_merge, after.doc.data_merge);
|
||||
assert_eq!(before.history.undo.len(), after.history.undo.len());
|
||||
assert_eq!(before.history.redo.len(), after.history.redo.len());
|
||||
assert_eq!(serde_json::to_value(&before.selection).unwrap(), serde_json::to_value(&after.selection).unwrap());
|
||||
assert_eq!(journal, session.journal);
|
||||
assert_eq!(original_bytes, std::fs::read(original).unwrap());
|
||||
assert!(!destination.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_a_copy_rebases_its_data_source_without_changing_the_open_document() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let (mut session, original) = linked_document(temp.path());
|
||||
let before = session.doc().unwrap().clone();
|
||||
let original_bytes = std::fs::read(&original).unwrap();
|
||||
let destination = temp.path().join("copies/nested/template.designcraft");
|
||||
std::fs::create_dir_all(destination.parent().unwrap()).unwrap();
|
||||
|
||||
session.execute("file.saveACopy", &json!({"path": destination})).unwrap();
|
||||
|
||||
let copy = designcraft_format::load(&std::fs::read(destination).unwrap()).unwrap();
|
||||
assert_eq!(copy.data_merge.sources[0].relative_path.as_deref(), Some("../../source/contacts.csv"));
|
||||
let after = session.doc().unwrap();
|
||||
assert!(Arc::ptr_eq(&before.doc, &after.doc));
|
||||
assert!(Arc::ptr_eq(&before.saved_doc, &after.saved_doc));
|
||||
assert_eq!(before.path, after.path);
|
||||
assert_eq!((before.revision, before.saved_revision, before.is_dirty()), (after.revision, after.saved_revision, after.is_dirty()));
|
||||
assert_eq!(original_bytes, std::fs::read(original).unwrap());
|
||||
}
|
||||
@@ -163,3 +163,42 @@ fn decoration_offsets_are_centers_for_explicit_and_automatic_rules() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An underline is one bar per stretch of text: a bar per glyph left lighter seams between the
|
||||
/// letters where the anti-aliased edges met (at fractional positions).
|
||||
#[test]
|
||||
fn an_underline_has_no_seams_between_letters() {
|
||||
let mut d = Document::new(&NewDocument { width: 400.0, height: 120.0, facing_pages: false, ..Default::default() });
|
||||
let chars = CharAttrs {
|
||||
font_family: Some("Source Sans 3".into()),
|
||||
size: Some(48.0),
|
||||
fill: Some("[Black]".into()),
|
||||
underline: Some(true),
|
||||
underline_weight: Some(Some(6.0)),
|
||||
..Default::default()
|
||||
};
|
||||
d.add_text_frame(SpreadRef::Doc(0), Rect::new(10.0, 10.0, 390.0, 110.0), d.default_layer(), "FERRUM", ParaFormat { chars, ..Default::default() })
|
||||
.unwrap();
|
||||
let mut renderer = Renderer::new();
|
||||
renderer.threads = 0;
|
||||
let placed = [Placed { spread: SpreadRef::Doc(0), xf: Affine::IDENTITY }];
|
||||
let options = RenderOptions { paper: false, background: Some([255; 4]), greek_below_px: 0.0, ..Default::default() };
|
||||
// A fractional scale puts the letters' edges between pixels.
|
||||
let (w, h) = (543u32, 163u32);
|
||||
let img = renderer.render(&d, &Cache::new(), &placed, w, h, Affine::scale(1.3579), &options);
|
||||
let px = |x: u32, y: u32| {
|
||||
let i = ((y * w + x) * 4) as usize;
|
||||
[img.pixels[i], img.pixels[i + 1], img.pixels[i + 2], img.pixels[i + 3]]
|
||||
};
|
||||
// The underline's middle row: the row with the longest run of dark pixels ([Black] shows as
|
||||
// about 52 grey), then nothing lighter than its darkest pixel along it.
|
||||
let row = (0..h).max_by_key(|&y| (0..w).filter(|&x| px(x, y)[0] < 110).count()).unwrap();
|
||||
let dark: Vec<u32> = (0..w).filter(|&x| px(x, row)[0] < 110).collect();
|
||||
let (x0, x1) = (dark[0], *dark.last().unwrap());
|
||||
assert!(x1 - x0 > 150, "the underline spans the word: {x0}..{x1}");
|
||||
// The bar's own anti-aliased ends aside.
|
||||
let inside = x0 + 2..=x1 - 2;
|
||||
let ink = inside.clone().map(|x| px(x, row)[0]).min().unwrap();
|
||||
let light: Vec<(u32, u8)> = inside.map(|x| (x, px(x, row)[0])).filter(|&(_, v)| v > ink + 12).collect();
|
||||
assert!(light.is_empty(), "seams in the underline at {light:?}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
use designcraft_doc::{ParaFormat, Story, StoryId};
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::{Value, json};
|
||||
use std::sync::Arc;
|
||||
fn setup() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s
|
||||
}
|
||||
fn frame(s: &mut Session, text: &str) -> (u64, u64) {
|
||||
let r = s.execute("frame.create", &json!({"rect":[72,72,300,300],"content":"text","text":text,"caret":false})).unwrap();
|
||||
(r["id"].as_u64().unwrap(), r["story"].as_u64().unwrap())
|
||||
}
|
||||
fn snapshot(s: &Session) -> Value {
|
||||
let d = s.doc().unwrap();
|
||||
json!({"doc":d.doc,"selection":d.selection,"revision":d.revision,"dirty":d.is_dirty(),"undo":d.history.undo.len(),"redo":d.history.redo.len(),"journal":s.journal})
|
||||
}
|
||||
#[test]
|
||||
fn rejected_thread_is_atomic_with_new_frame_and_redo() {
|
||||
let mut s = setup();
|
||||
let (a, _) = frame(&mut s, "hello");
|
||||
frame(&mut s, "undo");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let before = snapshot(&s);
|
||||
assert!(s.execute("text.thread", &json!({"from":999999,"rect":[0,0,100,100]})).is_err());
|
||||
assert_eq!(snapshot(&s), before);
|
||||
assert!(s.execute("text.thread", &json!({"from":a,"rect":[0,0,100,100],"spread":999})).is_err());
|
||||
assert_eq!(snapshot(&s), before);
|
||||
}
|
||||
#[test]
|
||||
fn locked_frame_cannot_be_unthreaded() {
|
||||
let mut s = setup();
|
||||
let (a, _) = frame(&mut s, "hello");
|
||||
let (b, _) = frame(&mut s, "");
|
||||
s.execute("text.thread", &json!({"from":a,"to":b})).unwrap();
|
||||
s.execute("object.lock", &json!({"ids":[a]})).unwrap();
|
||||
let before = snapshot(&s);
|
||||
let r = s.execute("text.unthread", &json!({"frame":a}));
|
||||
assert!(r.is_err(), "unthread accepted locked frame: {r:?}");
|
||||
assert_eq!(snapshot(&s), before);
|
||||
}
|
||||
#[test]
|
||||
fn locked_frame_cannot_be_removed_from_thread() {
|
||||
let mut s = setup();
|
||||
let (a, _) = frame(&mut s, "hello");
|
||||
let (b, _) = frame(&mut s, "");
|
||||
s.execute("text.thread", &json!({"from":a,"to":b})).unwrap();
|
||||
s.execute("object.lock", &json!({"ids":[a]})).unwrap();
|
||||
let before = snapshot(&s);
|
||||
let r = s.execute("text.removeFromThread", &json!({"frame":a}));
|
||||
assert!(r.is_err(), "remove accepted locked frame: {r:?}");
|
||||
assert_eq!(snapshot(&s), before);
|
||||
}
|
||||
#[test]
|
||||
fn rejected_selection_preserves_pending_typing() {
|
||||
let mut s = setup();
|
||||
let (_, sid) = frame(&mut s, "hello");
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":3})).unwrap();
|
||||
s.execute("type.char", &json!({"attrs":{"size":30}})).unwrap();
|
||||
let before = s.execute("type.selectionAttrs", &json!({})).unwrap();
|
||||
assert!(s.execute("text.select", &json!({"story":999999,"anchor":0})).is_err());
|
||||
let after = s.execute("type.selectionAttrs", &json!({})).unwrap();
|
||||
assert_eq!(after, before, "rejected selection reset pending typing");
|
||||
}
|
||||
#[test]
|
||||
fn append_max_id_preserves_both_tables() {
|
||||
use designcraft_doc::table::Table;
|
||||
let mut a = Story::with_text(StoryId(1), &designcraft_doc::TABLE_ANCHOR.to_string(), ParaFormat::default());
|
||||
let mut b = Story::with_text(StoryId(2), &designcraft_doc::TABLE_ANCHOR.to_string(), ParaFormat::default());
|
||||
let mut ta = Table::new(u64::MAX, 1, 1, 0, 0, 100.);
|
||||
ta.cells[0].text = Story::with_text(StoryId(0), "ORIGINAL", ParaFormat::default());
|
||||
a.tables.insert(u64::MAX, Arc::new(ta));
|
||||
let mut tb = Table::new(1, 1, 1, 0, 0, 100.);
|
||||
tb.cells[0].text = Story::with_text(StoryId(0), "INCOMING", ParaFormat::default());
|
||||
b.tables.insert(1, Arc::new(tb));
|
||||
a.paras[0].table = Some(u64::MAX);
|
||||
b.paras[0].table = Some(1);
|
||||
a.check().unwrap();
|
||||
b.check().unwrap();
|
||||
a.append_story(b);
|
||||
let contents: Vec<_> = a.tables.values().map(|t| t.cells[0].text.text.clone()).collect();
|
||||
assert!(contents.contains(&"ORIGINAL".into()) && contents.contains(&"INCOMING".into()), "table loss: {contents:?}");
|
||||
}
|
||||
#[test]
|
||||
fn idml_dropcap_detail_preserves_inherited_other_bit() {
|
||||
use designcraft_doc::{Document, ParaAttrs, ParagraphStyle, build::NewDocument};
|
||||
let mut d = Document::new(&NewDocument::default());
|
||||
let parent = ParagraphStyle {
|
||||
name: "Parent".into(),
|
||||
based_on: None,
|
||||
next_style: None,
|
||||
para: ParaAttrs { drop_cap_align_left: Some(false), drop_cap_scale_descenders: Some(true), ..Default::default() },
|
||||
chars: Default::default(),
|
||||
shortcut: String::new(),
|
||||
};
|
||||
let child = ParagraphStyle {
|
||||
name: "Child".into(),
|
||||
based_on: Some("Parent".into()),
|
||||
next_style: None,
|
||||
para: ParaAttrs { drop_cap_scale_descenders: Some(false), ..Default::default() },
|
||||
chars: Default::default(),
|
||||
shortcut: String::new(),
|
||||
};
|
||||
d.styles_mut().paragraph.extend([parent, child]);
|
||||
let before = d.styles.resolve_para_style("Child").0;
|
||||
let bytes = designcraft_idml::export_idml(&d);
|
||||
let back = designcraft_idml::import_idml(&bytes).unwrap();
|
||||
let after = back.styles.resolve_para_style("Child").0;
|
||||
assert_eq!(
|
||||
(after.drop_cap_align_left, after.drop_cap_scale_descenders),
|
||||
(before.drop_cap_align_left, before.drop_cap_scale_descenders),
|
||||
"IDML export resets inherited align-left bit"
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn threading_preserves_incoming_text_hyperlink() {
|
||||
let mut s = setup();
|
||||
let (a, sa) = frame(&mut s, "First");
|
||||
let (b, sb) = frame(&mut s, "Second");
|
||||
s.execute("text.select", &json!({"story":sb,"anchor":0,"focus":6})).unwrap();
|
||||
s.execute("hyperlink.create", &json!({"url":"https://example.com"})).unwrap();
|
||||
s.execute("text.thread", &json!({"from":a,"to":b})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let back = designcraft_format::load(&designcraft_format::save(d).unwrap()).unwrap();
|
||||
match &back.hyperlinks[0].source {
|
||||
designcraft_doc::HyperlinkSource::Text { story, start, end } => {
|
||||
assert_eq!(story.0, sa, "hyperlink points to deleted incoming story");
|
||||
assert_eq!((*start, *end), (6, 12));
|
||||
}
|
||||
_ => panic!("wrong source"),
|
||||
};
|
||||
}
|
||||
#[test]
|
||||
fn thread_cannot_destroy_body_by_joining_generated_endnotes() {
|
||||
let mut s = setup();
|
||||
let (body, sid) = frame(&mut s, "KEEP THIS BODY");
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":14})).unwrap();
|
||||
let e = s.execute("endnote.insert", &json!({"text":"a note"})).unwrap();
|
||||
let ef = e["frame"].as_u64().unwrap();
|
||||
let before = snapshot(&s);
|
||||
let r = s.execute("text.thread", &json!({"from":ef,"to":body}));
|
||||
assert!(
|
||||
r.is_err(),
|
||||
"thread into generated endnotes accepted: {r:?}; stories: {:?}",
|
||||
s.doc().unwrap().doc.stories.values().map(|s| &s.text).collect::<Vec<_>>()
|
||||
);
|
||||
assert_eq!(snapshot(&s), before);
|
||||
}
|
||||
#[test]
|
||||
fn generated_endnotes_accept_empty_extensions_but_reject_reverse_body_merge() {
|
||||
let mut s = setup();
|
||||
let (body, sid) = frame(&mut s, "Body");
|
||||
s.execute("text.select", &json!({"story":sid,"anchor":4})).unwrap();
|
||||
let r = s.execute("endnote.insert", &json!({"text":"note"})).unwrap();
|
||||
let ef = r["frame"].as_u64().unwrap();
|
||||
let esid = s.doc().unwrap().doc.endnote_story.unwrap();
|
||||
let before = snapshot(&s);
|
||||
assert!(s.execute("text.thread", &json!({"from":body,"to":ef})).is_err());
|
||||
assert_eq!(snapshot(&s), before);
|
||||
s.execute("text.thread", &json!({"from":ef,"rect":[72,72,300,300]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.endnote_story, Some(esid));
|
||||
s.execute("text.thread", &json!({"to":ef,"rect":[72,72,300,300]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.endnote_story, Some(esid));
|
||||
assert_eq!(s.doc().unwrap().doc.story(esid).unwrap().frames.len(), 3);
|
||||
assert!(s.doc().unwrap().doc.story(StoryId(sid)).unwrap().text.starts_with("Body"));
|
||||
s.doc().unwrap().doc.check().unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn threading_moves_data_placeholders_and_primary_story_with_text() {
|
||||
use designcraft_doc::{Placeholder, PlaceholderAnchor, PlaceholderRole};
|
||||
let mut s = setup();
|
||||
let (a, sa) = frame(&mut s, "First");
|
||||
let (b, sb) = frame(&mut s, "<<name>>");
|
||||
s.edit(|d, _| {
|
||||
d.settings.primary_story = Some(StoryId(sb));
|
||||
d.data_merge.placeholders.push(Placeholder {
|
||||
id: 123,
|
||||
source_id: 0,
|
||||
field: "name".into(),
|
||||
role: PlaceholderRole::Text,
|
||||
anchor: PlaceholderAnchor::Text { story: StoryId(sb), start: 0, end: 8 },
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let before = s.doc().unwrap().doc.clone();
|
||||
s.execute("text.thread", &json!({"from":a,"to":b})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert_eq!(d.settings.primary_story, Some(StoryId(sa)));
|
||||
assert_eq!(d.data_merge.placeholders.len(), 1);
|
||||
assert_eq!(d.data_merge.placeholders[0].anchor, PlaceholderAnchor::Text { story: StoryId(sa), start: 6, end: 14 });
|
||||
let after = d.clone();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc, before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc, after);
|
||||
}
|
||||
#[test]
|
||||
fn append_max_ids_preserves_addressable_footnotes_and_endnotes() {
|
||||
let mut a = Story::with_text(StoryId(1), "A", ParaFormat::default());
|
||||
let mut b = Story::with_text(StoryId(2), "B", ParaFormat::default());
|
||||
a.insert_note(1, "original footnote", ParaFormat::default());
|
||||
b.insert_note(1, "incoming footnote", ParaFormat::default());
|
||||
Arc::make_mut(&mut a.notes[0]).id = u64::MAX;
|
||||
a.insert_endnote(1, "original endnote", ParaFormat::default());
|
||||
b.insert_endnote(1, "incoming endnote", ParaFormat::default());
|
||||
Arc::make_mut(&mut a.endnotes[0]).id = u64::MAX;
|
||||
a.check().unwrap();
|
||||
b.check().unwrap();
|
||||
a.append_story(b);
|
||||
a.check().unwrap();
|
||||
assert_ne!(a.notes[0].id, a.notes[1].id);
|
||||
assert_ne!(a.endnotes[0].id, a.endnotes[1].id);
|
||||
assert_eq!(a.notes[0].text.text, "original footnote");
|
||||
assert_eq!(a.endnotes[1].text.text, "incoming endnote");
|
||||
}
|
||||
#[test]
|
||||
fn locked_layer_and_neighbour_reject_unthread_atomically() {
|
||||
for lock_layer in [false, true] {
|
||||
let mut s = setup();
|
||||
let (a, _) = frame(&mut s, "A");
|
||||
let (b, _) = frame(&mut s, "B");
|
||||
s.execute("text.thread", &json!({"from":a,"to":b})).unwrap();
|
||||
if lock_layer {
|
||||
let lid = s.doc().unwrap().active_layer.0;
|
||||
s.execute("layer.set", &json!({"id":lid,"locked":true})).unwrap();
|
||||
} else {
|
||||
s.execute("object.lock", &json!({"ids":[b]})).unwrap();
|
||||
}
|
||||
let before = snapshot(&s);
|
||||
assert!(s.execute("text.unthread", &json!({"frame":a})).is_err());
|
||||
assert_eq!(snapshot(&s), before);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn idml_partial_dropcap_bits_resolve_both_style_and_paragraph_inheritance() {
|
||||
use designcraft_doc::{Document, ParaAttrs, ParagraphStyle, SpreadRef, build::NewDocument};
|
||||
use designcraft_geom::Rect;
|
||||
for parent_align in [false, true] {
|
||||
for parent_scale in [false, true] {
|
||||
for local_align in [false, true] {
|
||||
let mut d = Document::new(&NewDocument::default());
|
||||
d.styles_mut().paragraph.push(ParagraphStyle {
|
||||
name: "Parent".into(),
|
||||
based_on: None,
|
||||
next_style: None,
|
||||
para: ParaAttrs { drop_cap_align_left: Some(parent_align), drop_cap_scale_descenders: Some(parent_scale), ..Default::default() },
|
||||
chars: Default::default(),
|
||||
shortcut: String::new(),
|
||||
});
|
||||
let layer = d.default_layer();
|
||||
let (_, sid) = d
|
||||
.add_text_frame(
|
||||
SpreadRef::Doc(0),
|
||||
Rect::new(10., 10., 200., 200.),
|
||||
layer,
|
||||
"Body",
|
||||
ParaFormat {
|
||||
style: "Parent".into(),
|
||||
para: ParaAttrs { drop_cap_align_left: Some(local_align), ..Default::default() },
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let bytes = designcraft_idml::export_idml(&d);
|
||||
let back = designcraft_idml::import_idml(&bytes).unwrap();
|
||||
let st = back.stories.values().find(|s| s.text == "Body").unwrap();
|
||||
let actual = back.styles.resolve_para(&st.paras[0]).0;
|
||||
let expected = d.styles.resolve_para(&d.story(sid).unwrap().paras[0]).0;
|
||||
assert_eq!(
|
||||
(actual.drop_cap_align_left, actual.drop_cap_scale_descenders),
|
||||
(expected.drop_cap_align_left, expected.drop_cap_scale_descenders)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user