mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-28 01:11:23 +01:00
Merge main into fix/docx-hide-proofing-errors
This commit is contained in:
commit
23ebdc7089
215 files changed
+28457
-1228
No files matched your search
@@ -0,0 +1,188 @@
|
||||
# This file is machine-generated by `gh actions-lock`.
|
||||
# Do not edit by hand; run `gh actions-lock` to update.
|
||||
# Docs: https://gh.io/actions-lockfile
|
||||
version: 'v0.0.3'
|
||||
workflows:
|
||||
'.github/workflows/ai-core.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/atomic.yml':
|
||||
- 'actions/checkout@v4.4.0'
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/audio.yml':
|
||||
- 'actions/cache@v6.1.0'
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/cms.yml':
|
||||
- 'actions/checkout@v4.4.0'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
'.github/workflows/crw.yml':
|
||||
- 'actions/cache@v6.1.0'
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/dialogs.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/doc.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/docpath.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/ecproj-format.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/font-store.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/formats.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'swatinem/rust-cache@v2.9.3'
|
||||
'.github/workflows/house-rules.yml': []
|
||||
'.github/workflows/i18n.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/jpeg.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/keymap.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/lockfile.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/media.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/menu.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
- 'swatinem/rust-cache@v2.9.3'
|
||||
'.github/workflows/pcraft-format.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/pdf.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'actions/setup-python@v7.1.0'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
- 'step-security/taiki-e-install-action@v2.87.32'
|
||||
'.github/workflows/print.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/proof.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/sonarcloud.yml': []
|
||||
'.github/workflows/ui.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/update.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/vectorcraft-format.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/video.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/web-api.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/taiki-e-install-action@v2.87.32'
|
||||
'.github/workflows/web-host.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
- 'step-security/taiki-e-install-action@v2.87.32'
|
||||
'.github/workflows/window.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/workspace.yml': []
|
||||
dependencies:
|
||||
'actions/cache@v6.1.0':
|
||||
ref: 'v6.1.0'
|
||||
commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9'
|
||||
owner_id: 44036562
|
||||
repo_id: 215566462
|
||||
'actions/checkout@v4.4.0':
|
||||
ref: 'v4.4.0'
|
||||
commit: 'sha1-11d5960a326750d5838078e36cf38b85af677262'
|
||||
owner_id: 44036562
|
||||
repo_id: 197814629
|
||||
'actions/checkout@v7.0.1':
|
||||
ref: 'v7.0.1'
|
||||
commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1'
|
||||
owner_id: 44036562
|
||||
repo_id: 197814629
|
||||
'actions/setup-python@v7.1.0':
|
||||
ref: 'v7.1.0'
|
||||
commit: 'sha1-6d4fd22b20465ccc0f7354476588e8df7c9ca0e0'
|
||||
owner_id: 44036562
|
||||
repo_id: 192625525
|
||||
'dtolnay/rust-toolchain@stable':
|
||||
ref: 'stable'
|
||||
commit: 'sha1-b725eda525362e7cf34af9a7243cac1e3f01a7ec'
|
||||
owner_id: 1940490
|
||||
repo_id: 260749683
|
||||
'step-security/dtolnay-rust-toolchain@v1.0.0':
|
||||
ref: 'v1.0.0'
|
||||
commit: 'sha1-8dd607977d06adf4ed49b190381e74c17a9dd25f'
|
||||
owner_id: 88700172
|
||||
repo_id: 1249865941
|
||||
'step-security/harden-runner@v2.22.4':
|
||||
ref: 'v2.22.4'
|
||||
commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a'
|
||||
owner_id: 88700172
|
||||
repo_id: 422287306
|
||||
'step-security/rust-cache@v2.9.2':
|
||||
ref: 'v2.9.2'
|
||||
commit: 'sha1-8871c8ba063416da7c32246932ea6ae08700453e'
|
||||
owner_id: 88700172
|
||||
repo_id: 822636713
|
||||
'step-security/taiki-e-install-action@v2.87.32':
|
||||
ref: 'v2.87.32'
|
||||
commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8'
|
||||
owner_id: 88700172
|
||||
repo_id: 1382753260
|
||||
'swatinem/rust-cache@v2.9.3':
|
||||
ref: 'v2.9.3'
|
||||
commit: 'sha1-d1527c8f67ef9af357290475d4af4b0fab8f12e0'
|
||||
owner_id: 580492
|
||||
repo_id: 298565987
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
@@ -50,7 +50,7 @@ jobs:
|
||||
name: Atomic WASM
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v4.4.0
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
|
||||
+16
-16
@@ -25,18 +25,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-lint
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -69,15 +69,15 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
# The workspace's rust-version.
|
||||
- run: rustup toolchain install 1.95.0 --profile minimal
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-msrv
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -88,16 +88,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-features
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -113,20 +113,20 @@ jobs:
|
||||
runs-on: ubuntu-26.04
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-test
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: ${{ runner.temp }}/media-fixtures/opus/vectors
|
||||
# The archive's SHA-256 (scripts/fetch-opus-vectors.sh checks it).
|
||||
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
timeout-minutes: 20
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v4.4.0
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: clippy
|
||||
|
||||
+13
-13
@@ -29,17 +29,17 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-crw-${{ matrix.os }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -53,17 +53,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-crw-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -75,21 +75,21 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-crw-samples
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- name: Cache the CC0 sample files
|
||||
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||
uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: ${{ runner.temp }}/crw-samples
|
||||
key: crw-samples-${{ hashFiles('crates/crw/tests/samples.json') }}
|
||||
|
||||
@@ -23,15 +23,15 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
# The token isn't needed after checkout; cargo runs build scripts and proc macros from
|
||||
# dependencies, so don't leave it in .git/config for that third-party code to read.
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -72,17 +72,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
|
||||
- uses: Swatinem/rust-cache@v2.9.3
|
||||
with:
|
||||
key: ${{ matrix.crate }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -25,18 +25,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-jpeg
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -27,24 +27,24 @@ jobs:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Checkout test fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
path: .test-inputs/craft-fonts
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-keymap-linux
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -71,23 +71,23 @@ jobs:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Checkout test fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
path: .test-inputs/craft-fonts
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-keymap-${{ matrix.os }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -98,17 +98,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-keymap-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
name: Lockfile
|
||||
|
||||
# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin
|
||||
# no longer verifies upstream, so a moved tag cannot slip a different commit into a run.
|
||||
# Docs: https://gh.io/actions-lockfile
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
|
||||
jobs:
|
||||
lockfile:
|
||||
name: Lockfile is in sync
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Install gh-actions-lock
|
||||
run: gh extension install github/gh-actions-lock --pin v0.4.1
|
||||
- name: Verify the lockfile
|
||||
run: gh actions-lock --verify
|
||||
@@ -25,18 +25,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-media-lint
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -56,16 +56,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-media-test
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -21,14 +21,14 @@ jobs:
|
||||
name: Menu (Linux)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: rustfmt, clippy
|
||||
targets: aarch64-apple-darwin, wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
|
||||
- uses: Swatinem/rust-cache@v2.9.3
|
||||
with:
|
||||
key: craft-menu-linux
|
||||
- run: cargo fmt -p craft-menu --check
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
components: rustfmt, clippy
|
||||
|
||||
+20
-20
@@ -31,19 +31,19 @@ jobs:
|
||||
RUSTFLAGS: --cfg craft_pdf_unicode_fixtures
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-pdf
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- uses: actions/setup-python@v7
|
||||
- uses: actions/setup-python@v7.1.0
|
||||
with:
|
||||
python-version: "3.12"
|
||||
- name: Provision licensed PDF test fonts
|
||||
@@ -61,14 +61,14 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-pdf-backends
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -81,19 +81,19 @@ jobs:
|
||||
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-pdf-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- uses: actions/setup-python@v7
|
||||
- uses: actions/setup-python@v7.1.0
|
||||
with:
|
||||
python-version: "3.12"
|
||||
- name: Provision licensed PDF test fonts
|
||||
@@ -111,7 +111,7 @@ jobs:
|
||||
- name: Install the matching WASM test runner
|
||||
# A prebuilt, checksum-verified binary instead of building it from source on every run.
|
||||
# A version the pinned action does not list yet falls back to `cargo install --locked`.
|
||||
uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
|
||||
uses: step-security/taiki-e-install-action@v2.87.32
|
||||
with:
|
||||
tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }}
|
||||
fallback: cargo-install
|
||||
@@ -122,19 +122,19 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-plate-test
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- uses: actions/setup-python@v7
|
||||
- uses: actions/setup-python@v7.1.0
|
||||
with:
|
||||
python-version: "3.12"
|
||||
- name: Provision licensed PDF test fonts
|
||||
|
||||
@@ -24,11 +24,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -25,27 +25,27 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
# The Language data (craft-i18n `language-data/`), embedded by crates/proof/build.rs.
|
||||
# Pinned to storytold/craft-i18n#114's head (#109's SCOWL dictionaries + judgment/judgement,
|
||||
# CP2.1); TODO(CP2.1): move to #114's merge commit once it merges.
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-i18n
|
||||
ref: c239f934ace90b7939afaceb23b472ceeafd6b8a
|
||||
path: craft-i18n
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-proof
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -30,21 +30,21 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- name: Install software Vulkan renderer
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y mesa-vulkan-drivers libvulkan1
|
||||
echo 'WGPU_BACKEND=vulkan' >> "$GITHUB_ENV"
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-ui
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -59,15 +59,15 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-ui-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -28,15 +28,15 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
# The token isn't needed after checkout; cargo runs build scripts and proc macros from
|
||||
# dependencies, so don't leave it in .git/config for that third-party code to read.
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -22,18 +22,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-video-lint
|
||||
- run: cargo fmt -p craft-video --check
|
||||
@@ -60,16 +60,16 @@ jobs:
|
||||
name: Video tests (${{ matrix.os }})
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-video-${{ matrix.os }}
|
||||
- name: Install ffmpeg (Linux)
|
||||
|
||||
@@ -26,13 +26,13 @@ jobs:
|
||||
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
@@ -46,7 +46,7 @@ jobs:
|
||||
- name: Install the matching WASM test runner
|
||||
# A prebuilt, checksum-verified binary instead of building it from source on every run.
|
||||
# A version the pinned action does not list yet falls back to `cargo install --locked`.
|
||||
uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
|
||||
uses: step-security/taiki-e-install-action@v2.87.32
|
||||
with:
|
||||
tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }}
|
||||
fallback: cargo-install
|
||||
|
||||
@@ -27,18 +27,18 @@ jobs:
|
||||
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-web-host
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -55,7 +55,7 @@ jobs:
|
||||
- name: Install the matching WASM test runner
|
||||
# A prebuilt, checksum-verified binary instead of building it from source on every run.
|
||||
# A version the pinned action does not list yet falls back to `cargo install --locked`.
|
||||
uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
|
||||
uses: step-security/taiki-e-install-action@v2.87.32
|
||||
with:
|
||||
tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }}
|
||||
fallback: cargo-install
|
||||
|
||||
@@ -28,15 +28,15 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
# The token isn't needed after checkout; cargo runs build scripts and proc macros from
|
||||
# dependencies, so don't leave it in .git/config for that third-party code to read.
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
Generated
+116
-1
@@ -1846,17 +1846,22 @@ dependencies = [
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"avif-serialize",
|
||||
"craft-heif",
|
||||
"craft-jpeg",
|
||||
"craft-metadata",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
"hayro-jpeg2000",
|
||||
"image",
|
||||
"image-webp",
|
||||
"jpeg-encoder 0.6.1",
|
||||
"jxl-oxide",
|
||||
"png 0.18.1",
|
||||
"proptest",
|
||||
"rav1e",
|
||||
"ravif",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1885,7 +1890,9 @@ dependencies = [
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"jpeg-decoder",
|
||||
"jpeg-encoder 0.6.1",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
"zune-jpeg",
|
||||
]
|
||||
@@ -2178,6 +2185,7 @@ dependencies = [
|
||||
"bytemuck",
|
||||
"chrono",
|
||||
"craft-stream-codecs",
|
||||
"craft-svg",
|
||||
"flate2",
|
||||
"hayro",
|
||||
"hayro-cmap",
|
||||
@@ -2185,6 +2193,7 @@ dependencies = [
|
||||
"hayro-syntax",
|
||||
"js-sys",
|
||||
"krilla",
|
||||
"krilla-svg",
|
||||
"kurbo 0.13.1",
|
||||
"md-5 0.11.0",
|
||||
"proptest",
|
||||
@@ -2194,6 +2203,7 @@ dependencies = [
|
||||
"stringprep",
|
||||
"thiserror 2.0.21",
|
||||
"ttf-parser",
|
||||
"usvg 0.47.0",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-test",
|
||||
"zeroize",
|
||||
@@ -2359,6 +2369,18 @@ dependencies = [
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ps"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-eps",
|
||||
"kurbo 0.13.1",
|
||||
"miniz_oxide 0.8.9",
|
||||
"pdf-writer",
|
||||
"skrifa 0.47.0",
|
||||
"weezl 0.1.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
@@ -2565,6 +2587,14 @@ dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-texture"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tga"
|
||||
version = "0.1.0"
|
||||
@@ -2805,6 +2835,7 @@ name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -4038,6 +4069,20 @@ dependencies = [
|
||||
"roxmltree 0.20.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fontdb"
|
||||
version = "0.23.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "457e789b3d1202543297a350643cf459f836cade38934e7a4cf6a39e7cde2905"
|
||||
dependencies = [
|
||||
"fontconfig-parser",
|
||||
"log",
|
||||
"memmap2",
|
||||
"slotmap",
|
||||
"tinyvec",
|
||||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fontdb"
|
||||
version = "0.24.0"
|
||||
@@ -5326,6 +5371,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-decoder"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00810f1d8b74be64b13dbf3db89ac67740615d6c891f0e7b6179326533011a07"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-encoder"
|
||||
version = "0.6.1"
|
||||
@@ -5624,6 +5678,22 @@ dependencies = [
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "krilla-svg"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1237d7c37b16ca9fbc2e72dde13a10d321f9a44aceee35c062bc0a43bfc7ce16"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"fontdb 0.23.0",
|
||||
"krilla",
|
||||
"resvg 0.47.0",
|
||||
"skrifa 0.42.1",
|
||||
"smallvec",
|
||||
"tiny-skia 0.12.0",
|
||||
"usvg 0.47.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kurbo"
|
||||
version = "0.11.3"
|
||||
@@ -7831,6 +7901,23 @@ dependencies = [
|
||||
"usvg 0.45.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "resvg"
|
||||
version = "0.47.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9be183ad6a216aa96f33e4c8033b0988b8b3ea6fd2359d19af5bac4643fd8e81"
|
||||
dependencies = [
|
||||
"gif",
|
||||
"image-webp",
|
||||
"log",
|
||||
"pico-args",
|
||||
"rgb",
|
||||
"svgtypes 0.16.2",
|
||||
"tiny-skia 0.12.0",
|
||||
"usvg 0.47.0",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "resvg"
|
||||
version = "0.48.1"
|
||||
@@ -9877,6 +9964,34 @@ dependencies = [
|
||||
"xmlwriter",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "usvg"
|
||||
version = "0.47.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d46cf96c5f498d36b7a9693bc6a7075c0bb9303189d61b2249b0dc3d309c07de"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"fontdb 0.23.0",
|
||||
"imagesize 0.14.0",
|
||||
"kurbo 0.13.1",
|
||||
"log",
|
||||
"pico-args",
|
||||
"roxmltree 0.21.2",
|
||||
"rustybuzz",
|
||||
"simplecss",
|
||||
"siphasher",
|
||||
"strict-num",
|
||||
"svgtypes 0.16.2",
|
||||
"tiny-skia-path 0.12.0",
|
||||
"ttf-parser",
|
||||
"unicode-bidi",
|
||||
"unicode-script",
|
||||
"unicode-vo",
|
||||
"xmlwriter",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "usvg"
|
||||
version = "0.48.1"
|
||||
@@ -9885,7 +10000,7 @@ checksum = "977d0a4abdef933f424a99fe09f95576e089b90aebc6f016a3bc813762493e91"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"data-url",
|
||||
"fontdb",
|
||||
"fontdb 0.24.0",
|
||||
"harfrust 0.12.0",
|
||||
"imagesize 0.15.0",
|
||||
"kurbo 0.13.1",
|
||||
|
||||
+7
-1
@@ -20,8 +20,13 @@ craft-affinity = { path = "crates/affinity" }
|
||||
craft-ai-core = { path = "crates/ai-core" }
|
||||
craft-anicraft-format = { path = "crates/anicraft-format" }
|
||||
craft-pdf = { path = "crates/pdf", version = "=0.1.0" }
|
||||
craft-ps = { path = "crates/ps", version = "=0.1.0" }
|
||||
craft-pdn = { path = "crates/pdn" }
|
||||
craft-plate-test = { path = "crates/plate-test" }
|
||||
pdf-writer = "0.15"
|
||||
miniz_oxide = "0.8"
|
||||
weezl = "0.1"
|
||||
skrifa = "0.47"
|
||||
krilla = { version = "=0.8.2", default-features = false, features = ["raster-images"] }
|
||||
hayro = { version = "=0.7.1", default-features = false }
|
||||
hayro-interpret = { version = "=0.7.0", default-features = false, features = ["embed-fonts", "embed-cmaps"] }
|
||||
@@ -86,10 +91,10 @@ craft-keymap = { path = "crates/keymap" }
|
||||
craft-assuan = { path = "crates/assuan" }
|
||||
craft-kra = { path = "crates/kra" }
|
||||
craft-audio = { path = "crates/audio" }
|
||||
# No default features, so craft-audio's `encode` builds craft-media without `isobmff`.
|
||||
craft-latex = { path = "crates/latex" }
|
||||
craft-markdown = { path = "crates/markdown" }
|
||||
craft-math = { path = "crates/math" }
|
||||
# No default features: craft-audio turns on the craft-media modules each of its features needs.
|
||||
craft-media = { path = "crates/media", default-features = false }
|
||||
craft-materialx = { path = "crates/materialx" }
|
||||
craft-media-testkit = { path = "crates/media-testkit" }
|
||||
@@ -134,6 +139,7 @@ craft-stencilcraft-document = { path = "crates/stencilcraft-document" }
|
||||
craft-svg-paths = { path = "crates/svg-paths" }
|
||||
craft-stream-codecs = { path = "crates/stream-codecs" }
|
||||
craft-text = { path = "crates/text" }
|
||||
craft-texture = { path = "crates/texture" }
|
||||
craft-tga = { path = "crates/tga" }
|
||||
craft-timeline = { path = "crates/timeline" }
|
||||
craft-tiff = { path = "crates/tiff" }
|
||||
|
||||
@@ -254,9 +254,10 @@ applications retain their own document-font selection and licensing policy.
|
||||
linesweeper
|
||||
|
||||
linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in
|
||||
vendor/linesweeper with its upstream license files and one local patch. Its manifest names Joe Neeman
|
||||
as author, and its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See
|
||||
vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for provenance, the local patch
|
||||
vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs and
|
||||
docs/sweep.typ. Its manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018
|
||||
Raph Levien". See vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for
|
||||
provenance, the local patches
|
||||
and the files left out.
|
||||
|
||||
Platform lifecycle source provenance
|
||||
|
||||
@@ -231,9 +231,10 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
sockets: session, requests of method calls with back-references, responses, ids, address
|
||||
templates, credentials and the judgement of HTTP answers, over any HTTP implementation. Bounded
|
||||
on hostile answers; what a program asks and makes of the answers stays in it.
|
||||
- [`craft-jpeg`](crates/jpeg/README.md): clean-room decoder for 12-bit and arithmetic-coded
|
||||
JPEG (T.81 SOF0/SOF1 at 12 bits, SOF9, SOF10), from PhotoCraft, and a frame probe for routing.
|
||||
Metadata stays in each app.
|
||||
- [`craft-jpeg`](crates/jpeg/README.md): clean-room JPEG decoder for 12-bit and arithmetic-coded
|
||||
frames (T.81 SOF0/SOF1 at 12 bits, SOF9, SOF10), band decoding, and an optimized 8-bit baseline
|
||||
encoder, from PhotoCraft; includes a frame probe for routing. Metadata interpretation stays in
|
||||
each app.
|
||||
- [`craft-raster-codecs`](crates/raster-codecs/README.md): small raster formats, one module each,
|
||||
from PhotoCraft: DDS (BC1 to BC3, read and write), DICOM, FITS (read and write), IFF/ILBM (read
|
||||
and write), PCX, PNM/PAM/PFM (read and write) and XPM, into one flat image type. Bounded and
|
||||
@@ -347,6 +348,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
- [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds
|
||||
hyphenation points, with a bundled US English dictionary and patterns and optional hypher patterns
|
||||
for 47 more languages (one cargo feature each).
|
||||
- [`craft-texture`](crates/texture/README.md): linear float image planes, gray or RGBA, with
|
||||
nearest and bilinear sampling that repeats or clamps, resizing and display conversion. Sizes are
|
||||
bounded by `Limits`. From MaterialCraft.
|
||||
- [`craft-ui`](crates/ui/README.md): SVG catalogs, icon buttons, tabs, pane controls, transactional docking, print settings and theme helpers for egui frontends;
|
||||
assets, themes, localization and command handling stay in each app. Requires Rust 1.95 or later.
|
||||
- [`craft-vcard`](crates/vcard/README.md): vCard contacts, from MailCraft: vCard 2.1, 3.0 and 4.0 read
|
||||
|
||||
@@ -23,6 +23,7 @@ decode-native = ["aac-decode", "alac-decode", "opus-decode", "ac3-decode", "mpa-
|
||||
# The symphonia fallback for the formats with no native decoder yet.
|
||||
symphonia = ["decode-native", "dep:symphonia"]
|
||||
# Every encoder: the lossless file writers and the AAC, ALAC, AC-3/E-AC-3, MP3 and Opus encoders.
|
||||
# All but Opus are also file formats of `encode` and `FileEncoder`.
|
||||
encode = ["lossless-encode", "aac-encode", "alac-encode", "ac3-encode", "eac3-encode", "mp3-encode", "opus-encode"]
|
||||
# Encode WAV/BWF/RF64, AIFF/AIFF-C and FLAC files.
|
||||
lossless-encode = ["pcm-encode", "flac-encode"]
|
||||
@@ -32,16 +33,18 @@ pcm-encode = []
|
||||
flac-encode = ["craft-media/bitstream"]
|
||||
# Codec modules on their own, for packet-level use.
|
||||
aac-decode = ["craft-media/bitstream"]
|
||||
aac-encode = ["craft-media/bitstream"]
|
||||
# The AAC encoder (`aac::Encoder`), and AAC in M4A through `encode` and `FileEncoder`.
|
||||
aac-encode = ["craft-media/bitstream", "craft-media/isobmff"]
|
||||
opus-decode = []
|
||||
opus-encode = []
|
||||
ac3-decode = ["craft-media/bitstream"]
|
||||
mpa-decode = ["craft-media/bitstream"]
|
||||
# MPEG-1/2 Layer III (MP3) decoder (`mp3::Decoder`).
|
||||
mp3-decode = ["mpa-decode"]
|
||||
# ALAC packet decoder and encoder (`alac`), ported from Apple's reference implementation.
|
||||
# ALAC packet decoder and encoder (`alac`), ported from Apple's reference implementation. The
|
||||
# encoder also writes ALAC in M4A through `encode` and `FileEncoder`.
|
||||
alac-decode = []
|
||||
alac-encode = []
|
||||
alac-encode = ["craft-media/isobmff"]
|
||||
ac3-encode = ["craft-media/bitstream"]
|
||||
eac3-encode = ["craft-media/bitstream"]
|
||||
# MPEG-1/2 Layer III (MP3) encoder (`mp3`).
|
||||
|
||||
+105
-20
@@ -29,19 +29,19 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou
|
||||
| `decode` | `decode`, `probe`, `Decoder`, `tracks`, `TrackDecoder`; every format in [Format support](#format-support) | `decode-native`, `symphonia` |
|
||||
| `decode-native` | the same, native formats only (others are `Unsupported`); `wav_pcm`, `aiff_pcm` (a file's integer PCM bit for bit, as `RawPcm`) | the six decoders below, craft-media `isobmff`/`matroska`/`ogg`, `log` |
|
||||
| `symphonia` | the fallback for formats with no native decoder; turning it on is the same as `decode` | `decode-native`, symphonia |
|
||||
| `encode` | every encoder: the WAV, AIFF and FLAC file writers, and the AAC, ALAC, AC-3/E-AC-3, MP3 and Opus encoders | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode` |
|
||||
| `encode` | every encoder, and every format of `encode` and `FileEncoder` | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode` |
|
||||
| `lossless-encode` | WAV, AIFF and FLAC writing | `pcm-encode`, `flac-encode` |
|
||||
| `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header`; `pcm_wav_file`, `pcm_wav_header`, `pcm_aiff_file`, `pcm_aiff_header` (minimal files around integer PCM bytes, for essence embedded in AAF / OMF) | none |
|
||||
| `flac-encode` | `encode`, `FileEncoder` for FLAC; `flac::Encoder`, the packet encoder for callers that mux FLAC themselves | craft-media `bitstream` |
|
||||
| `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams) | craft-media `bitstream` |
|
||||
| `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams); with `aac-encode`, `encode`, `FileEncoder` for AAC in M4A | craft-media `bitstream`; `aac-encode` also `isobmff` |
|
||||
| `alac-decode` | `alac::Decoder` | none |
|
||||
| `alac-encode` | `alac::Encoder` | none |
|
||||
| `alac-encode` | `alac::Encoder`; `encode`, `FileEncoder` for ALAC in M4A | craft-media `isobmff` |
|
||||
| `opus-decode`, `opus-encode` | `opus::Decoder`/`StreamDecoder`, `opus::Encoder` | none |
|
||||
| `ac3-decode` | `ac3::Decoder`, `ac3::ProgrammeDecoder` | craft-media `bitstream` |
|
||||
| `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder` | craft-media `bitstream` |
|
||||
| `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder`; `encode`, `FileEncoder` for AC-3 and E-AC-3 streams | craft-media `bitstream` |
|
||||
| `mpa-decode` | `mpa::Decoder` | craft-media `bitstream` |
|
||||
| `mp3-decode` | `mp3::Decoder`, `mp3::InfoTag` | `mpa-decode` |
|
||||
| `mp3-encode` | `mp3::Encoder` | craft-media `bitstream` |
|
||||
| `mp3-encode` | `mp3::Encoder`; `encode`, `FileEncoder` for MP3 | craft-media `bitstream` |
|
||||
| `peaks` | `peaks::Peaks` | none |
|
||||
| `rayon` | parallel AAC encoding (with `aac-encode`) | `rayon` |
|
||||
| `serde` | `Serialize`/`Deserialize` on the shared types | `serde` |
|
||||
@@ -230,17 +230,86 @@ let wav = encode(&surround, &EncodeOptions::default().with_layout(ChannelLayout:
|
||||
let mut enc = FileEncoder::new(std::fs::File::create("take.wav")?, &EncodeOptions::default(), 48_000, 2)?;
|
||||
enc.write(&buf.channels, buf.frames())?; // one slice (or Vec) per channel
|
||||
enc.finish()?;
|
||||
|
||||
// The coded formats go the same way: AAC in M4A at 192 kbit/s, 5.1 as E-AC-3, 16-bit ALAC.
|
||||
let m4a = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(192_000))?;
|
||||
let eac3 = encode(&surround, &EncodeOptions::new(EncodeFormat::Eac3).with_layout(ChannelLayout::SURROUND_5_1_SIDE))?;
|
||||
let alac = encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16))?;
|
||||
```
|
||||
|
||||
`EncodeOptions::default()` is 24-bit WAV. `with_bwf` adds a BWF `bext` chunk and
|
||||
`with_force_rf64` writes RF64 below 4 GiB; both apply to WAV only. `encode_format_for("aiff")`
|
||||
maps an extension to an `EncodeFormat`, and `ENCODE_EXTENSIONS` lists the extensions the enabled
|
||||
features can write. `EncodeFormat::ALL` lists the formats; `extension()` is the extension a new
|
||||
file gets and `mime_type()` its media type. `write` refuses a block without one slice per channel, or with a slice
|
||||
shorter than `frames`, and writes nothing. After a write fails for another reason, every later
|
||||
maps an extension to an `EncodeFormat` (`"m4a"` to AAC; `"alac"` names ALAC), and `ENCODE_EXTENSIONS` lists the extensions the enabled
|
||||
features can write. `EncodeFormat::ALL` lists the formats and `is_built()` says whether this build
|
||||
writes one; `extension()` is the extension a new file gets and `mime_type()` its media type.
|
||||
`write` refuses a block without one slice per channel, or with a slice shorter than `frames`, and
|
||||
writes nothing. After a write fails for another reason, every later
|
||||
`write` and `finish` returns an error.
|
||||
|
||||
### Encode AAC
|
||||
| `EncodeFormat` | File | Audio | Options used |
|
||||
|---|---|---|---|
|
||||
| `Wav`, `Aiff`, `Flac` | WAV / BWF / RF64, AIFF / AIFF-C, FLAC | any rate; FLAC up to 8 channels | `bit_depth`, `dither`, `layout` (WAV, FLAC), `bwf`, `force_rf64` (WAV) |
|
||||
| `Alac` | M4A | 1–8 channels, any rate | `bit_depth` (16, 24, 32), `dither`, `layout` |
|
||||
| `Aac` | M4A | AAC-LC, 1–8 channels, the 13 AAC rates (7350–96000 Hz) | `rate_control`, `layout` |
|
||||
| `Mp3` | `.mp3` stream | mono or stereo; 32, 44.1, 48 kHz or 16, 22.05, 24 kHz | `rate_control` (bit rate), `layout` |
|
||||
| `Ac3`, `Eac3` | `.ac3` / `.eac3` stream | up to 5.1 (E-AC-3: 7.1); 32, 44.1, 48 kHz | `rate_control` (bit rate), `layout` |
|
||||
|
||||
`with_bitrate(bits_per_second)` sets a constant bit rate for all channels together. A rate the
|
||||
stream cannot carry is moved to the nearest one it can: MP3 and AC-3 use the highest rate of their
|
||||
tables not above it, AAC and E-AC-3 keep it inside their range (AAC from 6 kbit/s or a quarter of
|
||||
the sample rate per channel, whichever is more: from about half of that down the encoder codes
|
||||
silence). `with_quality(0.0..=1.0)` is AAC's
|
||||
constant quality (VBR); the other lossy formats return `Unsupported` for it. Without either, AAC
|
||||
is written at 64 kbit/s per channel, MP3 at 96 kbit/s per channel, and AC-3 and E-AC-3 at
|
||||
96 kbit/s per full-bandwidth channel (the LFE not counted).
|
||||
|
||||
The audio is in WAV channel order for every format; `layout` (the usual layout for the channel
|
||||
count when unset) names the speakers, and the channels are reordered into the codec's own order.
|
||||
Each coded format has a fixed set of layouts, and audio with other speakers is `Unsupported`
|
||||
rather than written to the wrong ones:
|
||||
|
||||
- AAC: C; L R; L R C; L R C Cb; L R C Ls Rs; 5.1; 6.1; and for 8 channels L R C LFE Lb Rb Lc Rc
|
||||
(a front pair left and right of centre, mask `0xFF`). ALAC: the same, with 6.1's back centre last.
|
||||
WAV 7.1 (backs and sides) is not that 7.1, and quad (the usual 4-channel layout) has no centre:
|
||||
both need another format.
|
||||
- AC-3 and E-AC-3: the layout picks the coding mode (1/0 to 3/2, with or without LFE; E-AC-3 also
|
||||
7.1 with backs and sides).
|
||||
- A surround pair is a surround pair: side and back surrounds are interchangeable where the format
|
||||
has one pair.
|
||||
|
||||
Before a file is created, the options and the format say what will happen:
|
||||
|
||||
```rust
|
||||
let opts = EncodeOptions::new(EncodeFormat::Ac3).with_layout(info.layout).with_bitrate(200_000);
|
||||
opts.check(info.sample_rate, info.channels)?; // the error FileEncoder::new would return, nothing written
|
||||
let bps = opts.bitrate(info.sample_rate, info.channels)?; // Some(192_000): the rate actually used; None when lossless or by quality
|
||||
|
||||
EncodeFormat::Ac3.sample_rates(); // Some(&[32_000, 44_100, 48_000]); None when any rate will do
|
||||
EncodeFormat::Ac3.max_channels(); // 6
|
||||
EncodeFormat::Ac3.fixed_layouts(3); // L R C, L R Cb, L R LFE; None for WAV, AIFF and FLAC (any layout)
|
||||
EncodeFormat::Ac3.supports_layout(&info.layout); // one of those, sides and backs standing for each other
|
||||
EncodeFormat::Ac3.supports_quality(); // false: AAC only
|
||||
```
|
||||
|
||||
`FileEncoder::bitrate()` gives the rate of a file being written. The `EncodeFormat` queries answer
|
||||
for a format whose feature is off too; `check` and `EncodeOptions::bitrate` (there with any
|
||||
encoder feature) return `Unsupported` for it.
|
||||
|
||||
The layout `decode` reports (`AudioInfo::layout`) can be passed to `with_layout` as it is. When a
|
||||
coded format refuses it (2.1 or quad into AAC, a single channel tagged as left, channels with no
|
||||
speaker assignment), the choices are another format, or audio rearranged into one of the layouts
|
||||
above with that layout set; leaving `layout` unset declares the usual layout for the count. This
|
||||
crate's decoder returns 7- and 8-channel AAC without a speaker assignment, in AAC order, so such a
|
||||
file needs that step before it is written again.
|
||||
|
||||
An M4A's edit list skips the AAC encoder's priming (1024 frames) and ends at the last frame
|
||||
written, so the file decodes to the length that went in. An MP3, AC-3 or E-AC-3 stream has no
|
||||
place for that (see [Limits](#limits)). For a container of your own, use the codec modules below.
|
||||
|
||||
### Encode AAC packets
|
||||
|
||||
For an `.m4a` file, use `EncodeFormat::Aac` above. `aac::Encoder` is the packet encoder behind it,
|
||||
for muxing into a container of your own:
|
||||
|
||||
```rust
|
||||
use craft_audio::AudioError;
|
||||
@@ -343,14 +412,14 @@ match craft_audio::decode(&bytes, None) {
|
||||
| WAV, BWF, RF64/BW64 | native | native |
|
||||
| AIFF, AIFF-C | native | native |
|
||||
| FLAC | symphonia | native |
|
||||
| AAC-LC | native | native |
|
||||
| AAC-LC | native | native; M4A through `encode` / `FileEncoder`, packets through `aac::Encoder` |
|
||||
| HE-AAC v1 | native (SBR) | — |
|
||||
| HE-AAC v2 | native, without parametric stereo (see [Limits](#limits)) | — |
|
||||
| ALAC | native | native, packets (`alac::Encoder`; no file writer) |
|
||||
| ALAC | native | native; M4A through `encode` / `FileEncoder`, packets through `alac::Encoder` |
|
||||
| MP1, MP2 (MPEG audio Layer I/II) | native | — |
|
||||
| MP3 (Layer III) | native | native, frames (`mp3::Encoder`; the frames in a row are an `.mp3` file) |
|
||||
| Opus | native | native |
|
||||
| AC-3, E-AC-3 | native | native packet encoder |
|
||||
| MP3 (Layer III) | native | native; `.mp3` through `encode` / `FileEncoder`, frames through `mp3::Encoder` |
|
||||
| Opus | native | native, packets (`opus::Encoder`; no file writer) |
|
||||
| AC-3, E-AC-3 | native | native; `.ac3` / `.eac3` through `encode` / `FileEncoder`, packets through `ac3::encoder::Encoder` |
|
||||
| Vorbis, ADPCM, A-law, µ-law | symphonia | — |
|
||||
|
||||
| Container | Demuxer | Timing |
|
||||
@@ -410,10 +479,26 @@ match craft_audio::decode(&bytes, None) {
|
||||
- `TrackDecoder` returns `Unsupported` for FLAC and Vorbis; `decode` and `Decoder` read
|
||||
those through symphonia. ALAC in CAF still goes through symphonia (craft-media has no CAF
|
||||
demuxer).
|
||||
- ALAC: `EncodeFormat` has no ALAC entry; `alac::Encoder` writes packets and the configuration,
|
||||
and the caller muxes them (MP4 `alac` box, Matroska `A_ALAC`). craft-media's MP4 muxer writes no
|
||||
`chan` box, so an MP4 of more than two channels carries no explicit layout; decoders then use the
|
||||
standard ALAC layout for the count.
|
||||
- ALAC: craft-media's MP4 muxer writes no `chan` box, so an M4A of more than two channels (from
|
||||
`EncodeFormat::Alac` or muxed from `alac::Encoder` packets) carries no explicit layout; decoders
|
||||
then use the standard ALAC layout for the count, which is the one written.
|
||||
- `EncodeFormat::Mp3`, `Ac3` and `Eac3` write raw streams, which cannot signal the codec's delay or
|
||||
the length: the file decodes with 1057 (MP3) or 256 (AC-3, E-AC-3) frames of delay in front and
|
||||
padded to whole frames at the end. MP3 gets no Xing / Info frame. `EncodeFormat::Aac` and `Alac`
|
||||
(M4A) decode to the length written.
|
||||
- An AAC M4A's length is in the edit list and the last packet's duration. This crate, Apple's
|
||||
decoders and ffmpeg 9 end the audio there; ffmpeg 8 skips the priming but plays the last
|
||||
packet's padding (under 1024 frames of near silence).
|
||||
- `EncodeFormat::Aac` and `Alac` write the `moov` box after the audio (no fast start), and no
|
||||
metadata. Raw ADTS AAC is not a file format of `encode`; `aac::EncoderConfig::with_adts` gives
|
||||
the frames.
|
||||
- This crate's file reader plays a raw E-AC-3 7.1 stream as its 5.1 independent substream.
|
||||
- `EncodeFormat::Aac` with 8 channels is AAC channel configuration 7 (C, Lc Rc, L R, Ls Rs, LFE),
|
||||
which Apple's decoders read as written. ffmpeg assumes such a stream is a mislabelled 7.1 with
|
||||
backs and sides and moves the channels accordingly, unless run with `-strict 1`.
|
||||
- A file with no audio: MP3, AC-3 and E-AC-3 are then zero bytes, which nothing can probe. An
|
||||
empty AAC M4A (a track with no samples) is read by ffmpeg; this crate's native reader passes it
|
||||
on, so `decode-native` alone refuses it and symphonia reads only some as zero frames.
|
||||
- ALAC 32-bit streams: ffmpeg does not decode their uncompressed (escaped) stereo frames, which
|
||||
Apple's encoder and this one write for audio that does not compress; Apple's decoder and this
|
||||
one do.
|
||||
@@ -481,7 +566,7 @@ finite count as silence and the rest are clamped to ±4, so no input makes an in
|
||||
decoder's 7680-bit input buffer.
|
||||
- Not yet: block switching (short blocks; transients pre-echo), `scfsi` and `preflag`, intensity
|
||||
stereo, a tonality-aware model, VBR/ABR, the Xing/Info and LAME gapless tags (the delay is not
|
||||
signalled), CRC, free format, and an `EncodeFormat` entry for `encode`/`FileEncoder`.
|
||||
signalled), CRC and free format.
|
||||
|
||||
### AC-3 and E-AC-3 encoding
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Write a tone as WAV and FLAC, whole and streamed, and as AAC packets.
|
||||
//! Write a tone as WAV, FLAC and AAC in M4A, whole and streamed, and as AAC packets.
|
||||
//!
|
||||
//! `cargo run -p craft-audio --example encode --features lossless-encode,aac-encode -- out`
|
||||
|
||||
@@ -28,7 +28,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
enc.finish()?;
|
||||
|
||||
// AAC packets for an MP4 or Matroska muxer, with the configuration its sample entry needs.
|
||||
// AAC in M4A at 160 kbit/s: the same call, with a bit rate in place of a bit depth.
|
||||
let m4a = encode(&stereo, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(160_000))?;
|
||||
std::fs::write(dir.join("tone.m4a"), m4a)?;
|
||||
|
||||
// AAC packets for a muxer of your own, with the configuration its sample entry needs.
|
||||
let mut aac = craft_audio::aac::Encoder::new(craft_audio::aac::EncoderConfig::cbr(rate as u32, 2, 128_000))?;
|
||||
let mut units = aac.encode(&[&tone, &tone]);
|
||||
units.extend(aac.flush());
|
||||
|
||||
@@ -162,6 +162,16 @@ const HISTORY: usize = 2048;
|
||||
/// Bits per channel in a silent frame, rounded up: the smallest frame the encoder writes.
|
||||
const MIN_FRAME_BITS_PER_CHANNEL: u64 = 48;
|
||||
|
||||
/// The lowest and highest constant bit rate (bits per second) [`Encoder::new`] accepts for a
|
||||
/// stream without ADTS headers: at least 1 kbit/s per channel and a silent frame, at most the
|
||||
/// 6144 bits per channel and frame of the decoder buffer.
|
||||
pub(crate) fn cbr_range(sample_rate: u32, channels: usize) -> (u32, u32) {
|
||||
let (rate, channels) = (u64::from(sample_rate), channels as u64);
|
||||
let silent = (MIN_FRAME_BITS_PER_CHANNEL * channels * rate).div_ceil(FRAME_LEN as u64);
|
||||
let most = 6144 * channels * rate / FRAME_LEN as u64;
|
||||
(u32::try_from(silent.max(1000 * channels)).unwrap_or(u32::MAX), u32::try_from(most).unwrap_or(u32::MAX))
|
||||
}
|
||||
|
||||
/// AAC-LC encoder.
|
||||
pub struct Encoder {
|
||||
cfg: EncoderConfig,
|
||||
|
||||
@@ -64,6 +64,8 @@ pub use config::{AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, co
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub use decoder::Decoder;
|
||||
#[cfg(feature = "aac-encode")]
|
||||
pub(crate) use encoder::cbr_range;
|
||||
#[cfg(feature = "aac-encode")]
|
||||
pub use encoder::{BitrateMode, Encoder, EncoderConfig, EncoderStats};
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub use latm::{LatmConfig, latm_config, latm_split};
|
||||
|
||||
@@ -61,7 +61,7 @@ pub enum Error {
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Data rate in kb/s by frmsizecod / 2 (Table 5.18).
|
||||
const BITRATES: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640];
|
||||
pub(crate) const BITRATES: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640];
|
||||
|
||||
/// Full-bandwidth channels per acmod (Table 5.8).
|
||||
const NFCHANS: [usize; 8] = [2, 1, 2, 3, 3, 4, 4, 5];
|
||||
|
||||
@@ -283,6 +283,40 @@ pub(crate) fn flac_assumed_mask(channels: usize) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// The surround speaker a layout may name in place of `speaker`: formats with one surround pair
|
||||
/// do not tell side from back, so a pair of either kind feeds it.
|
||||
fn surround_twin(speaker: Speaker) -> Option<Speaker> {
|
||||
Some(match speaker {
|
||||
Speaker::SideLeft => Speaker::BackLeft,
|
||||
Speaker::SideRight => Speaker::BackRight,
|
||||
Speaker::BackLeft => Speaker::SideLeft,
|
||||
Speaker::BackRight => Speaker::SideRight,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// How audio with `layout` feeds a format whose layout for the channel count is `mask`: for each
|
||||
/// channel the format takes, the channel of the audio that feeds it. `order` is the format's
|
||||
/// channel order over `mask` (for each of its channels, the WAV-order channel of `mask`; `None`
|
||||
/// when it takes WAV order). `None` when `layout` is not the format's layout: it is unordered, or
|
||||
/// has a speaker the format has not.
|
||||
pub(crate) fn channel_map(mask: u32, order: Option<&[usize]>, layout: &ChannelLayout) -> Option<Vec<usize>> {
|
||||
let speakers = ChannelLayout::from_mask(mask).speakers();
|
||||
if speakers.len() != usize::from(layout.channels()) {
|
||||
return None;
|
||||
}
|
||||
let mut map = Vec::with_capacity(speakers.len());
|
||||
for j in 0..speakers.len() {
|
||||
let speaker = *speakers.get(order.and_then(|o| o.get(j).copied()).unwrap_or(j))?;
|
||||
let at = layout.index_of(speaker).or_else(|| surround_twin(speaker).and_then(|t| layout.index_of(t)))?;
|
||||
if map.contains(&at) {
|
||||
return None;
|
||||
}
|
||||
map.push(at);
|
||||
}
|
||||
Some(map)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+231
-13
@@ -16,9 +16,10 @@
|
||||
//! - [`TrackDecoder`] (any decoder feature): one track's packets from any demuxer to planar
|
||||
//! `f32` in WAV channel order.
|
||||
//! - [`encode`], [`FileEncoder`]: WAV/BWF/RF64 and AIFF/AIFF-C (feature `pcm-encode`, which also
|
||||
//! gives [`wav_float_header`]) and FLAC (feature `flac-encode`), whole-buffer or incremental into
|
||||
//! any `Write + Seek`. Feature `lossless-encode` turns on both; feature `encode` turns on
|
||||
//! `lossless-encode` and every codec's encoder.
|
||||
//! gives [`wav_float_header`]), FLAC (feature `flac-encode`), AAC-LC and ALAC in M4A
|
||||
//! (`aac-encode`, `alac-encode`) and MP3, AC-3 and E-AC-3 streams (`mp3-encode`, `ac3-encode`,
|
||||
//! `eac3-encode`), whole-buffer or incremental into any `Write + Seek`. Feature
|
||||
//! `lossless-encode` turns on WAV, AIFF and FLAC; feature `encode` turns on every format and encoder.
|
||||
//! - [`wav_pcm`], [`aiff_pcm`] (feature `decode-native`) and [`pcm_wav_file`], [`pcm_wav_header`],
|
||||
//! [`pcm_aiff_file`], [`pcm_aiff_header`] (feature `pcm-encode`): the integer PCM of a WAV or AIFF
|
||||
//! file bit for bit, and minimal files around such PCM, for essence embedded in other containers.
|
||||
@@ -54,7 +55,15 @@ mod read;
|
||||
mod track;
|
||||
#[cfg(any(feature = "decode-native", feature = "pcm-encode"))]
|
||||
mod wav;
|
||||
#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))]
|
||||
#[cfg(any(
|
||||
feature = "pcm-encode",
|
||||
feature = "flac-encode",
|
||||
feature = "aac-encode",
|
||||
feature = "alac-encode",
|
||||
feature = "mp3-encode",
|
||||
feature = "ac3-encode",
|
||||
feature = "eac3-encode"
|
||||
))]
|
||||
mod write;
|
||||
|
||||
/// The FLAC packet encoder (RFC 9639), for a caller that muxes FLAC itself (into MP4, Matroska or
|
||||
@@ -100,7 +109,15 @@ pub use read::{RawPcm, aiff_pcm, wav_pcm};
|
||||
pub use track::{LOOKAHEAD_PACKETS, TrackDecoder};
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
pub use write::wav_float_header;
|
||||
#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))]
|
||||
#[cfg(any(
|
||||
feature = "pcm-encode",
|
||||
feature = "flac-encode",
|
||||
feature = "aac-encode",
|
||||
feature = "alac-encode",
|
||||
feature = "mp3-encode",
|
||||
feature = "ac3-encode",
|
||||
feature = "eac3-encode"
|
||||
))]
|
||||
pub use write::{ENCODE_EXTENSIONS, FileEncoder, encode, encode_format_for};
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
pub use write::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header};
|
||||
@@ -215,7 +232,8 @@ impl From<opus::encoder::Error> for AudioError {
|
||||
}
|
||||
|
||||
/// The file formats [`encode`] writes. Each needs its feature: WAV and AIFF `pcm-encode`, FLAC
|
||||
/// `flac-encode`; asking for one that is not built returns [`AudioError::Unsupported`].
|
||||
/// `flac-encode`, AAC `aac-encode`, ALAC `alac-encode`, MP3 `mp3-encode`, AC-3 `ac3-encode` and
|
||||
/// E-AC-3 `eac3-encode`; asking for one that is not built returns [`AudioError::Unsupported`].
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
@@ -226,20 +244,46 @@ pub enum EncodeFormat {
|
||||
Aiff,
|
||||
/// FLAC (integer depths).
|
||||
Flac,
|
||||
/// AAC-LC in an M4A (MP4) file, 1–8 channels at one of the 13 AAC sample rates. The edit list
|
||||
/// skips the encoder's priming and ends at the last frame, so the file decodes to the length
|
||||
/// that was written.
|
||||
Aac,
|
||||
/// Apple Lossless in an M4A (MP4) file (integer depths), 1–8 channels.
|
||||
Alac,
|
||||
/// An MP3 stream: mono or stereo at 32, 44.1 or 48 kHz, or 16, 22.05 or 24 kHz.
|
||||
Mp3,
|
||||
/// An AC-3 stream: up to 5.1 at 32, 44.1 or 48 kHz.
|
||||
Ac3,
|
||||
/// An E-AC-3 stream: up to 7.1 at 32, 44.1 or 48 kHz.
|
||||
Eac3,
|
||||
}
|
||||
|
||||
impl EncodeFormat {
|
||||
/// Every format, in the order menus list them. Whether [`encode`] writes one depends on its
|
||||
/// feature.
|
||||
pub const ALL: &[EncodeFormat] = &[EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac];
|
||||
/// feature ([`EncodeFormat::is_built`]).
|
||||
pub const ALL: &[EncodeFormat] = &[
|
||||
EncodeFormat::Wav,
|
||||
EncodeFormat::Aiff,
|
||||
EncodeFormat::Flac,
|
||||
EncodeFormat::Alac,
|
||||
EncodeFormat::Aac,
|
||||
EncodeFormat::Mp3,
|
||||
EncodeFormat::Ac3,
|
||||
EncodeFormat::Eac3,
|
||||
];
|
||||
|
||||
/// The extension a new file of this format gets, lower case and without the dot: `"wav"`,
|
||||
/// `"aif"`, `"flac"`. [`encode_format_for`] maps it back.
|
||||
/// `"aif"`, `"flac"`, `"m4a"` (AAC and ALAC), `"mp3"`, `"ac3"`, `"eac3"`.
|
||||
/// [`encode_format_for`] maps it back, `"m4a"` to AAC.
|
||||
pub fn extension(self) -> &'static str {
|
||||
match self {
|
||||
EncodeFormat::Wav => "wav",
|
||||
EncodeFormat::Aiff => "aif",
|
||||
EncodeFormat::Flac => "flac",
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => "m4a",
|
||||
EncodeFormat::Mp3 => "mp3",
|
||||
EncodeFormat::Ac3 => "ac3",
|
||||
EncodeFormat::Eac3 => "eac3",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,6 +293,10 @@ impl EncodeFormat {
|
||||
EncodeFormat::Wav => "audio/wav",
|
||||
EncodeFormat::Aiff => "audio/aiff",
|
||||
EncodeFormat::Flac => "audio/flac",
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => "audio/mp4",
|
||||
EncodeFormat::Mp3 => "audio/mpeg",
|
||||
EncodeFormat::Ac3 => "audio/ac3",
|
||||
EncodeFormat::Eac3 => "audio/eac3",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,8 +306,154 @@ impl EncodeFormat {
|
||||
EncodeFormat::Wav => Container::Wav,
|
||||
EncodeFormat::Aiff => Container::Aiff,
|
||||
EncodeFormat::Flac => Container::Flac,
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => Container::Mp4,
|
||||
EncodeFormat::Mp3 => Container::MpegAudio,
|
||||
EncodeFormat::Ac3 | EncodeFormat::Eac3 => Container::Ac3,
|
||||
}
|
||||
}
|
||||
|
||||
/// The codec of the audio this format writes.
|
||||
pub fn codec(self) -> CodecId {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => CodecId::Pcm,
|
||||
EncodeFormat::Flac => CodecId::Flac,
|
||||
EncodeFormat::Aac => CodecId::Aac,
|
||||
EncodeFormat::Alac => CodecId::Alac,
|
||||
EncodeFormat::Mp3 => CodecId::Mp3,
|
||||
EncodeFormat::Ac3 => CodecId::Ac3,
|
||||
EncodeFormat::Eac3 => CodecId::Eac3,
|
||||
}
|
||||
}
|
||||
|
||||
/// The feature that builds this format's writer.
|
||||
pub fn feature(self) -> &'static str {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => "pcm-encode",
|
||||
EncodeFormat::Flac => "flac-encode",
|
||||
EncodeFormat::Aac => "aac-encode",
|
||||
EncodeFormat::Alac => "alac-encode",
|
||||
EncodeFormat::Mp3 => "mp3-encode",
|
||||
EncodeFormat::Ac3 => "ac3-encode",
|
||||
EncodeFormat::Eac3 => "eac3-encode",
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether [`encode`] and [`FileEncoder`] write this format in this build (its
|
||||
/// [`feature`](Self::feature) is on): what to filter [`EncodeFormat::ALL`] by for a menu.
|
||||
pub fn is_built(self) -> bool {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => cfg!(feature = "pcm-encode"),
|
||||
EncodeFormat::Flac => cfg!(feature = "flac-encode"),
|
||||
EncodeFormat::Aac => cfg!(feature = "aac-encode"),
|
||||
EncodeFormat::Alac => cfg!(feature = "alac-encode"),
|
||||
EncodeFormat::Mp3 => cfg!(feature = "mp3-encode"),
|
||||
EncodeFormat::Ac3 => cfg!(feature = "ac3-encode"),
|
||||
EncodeFormat::Eac3 => cfg!(feature = "eac3-encode"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The sample rates the format takes, ascending, or `None` when it has no fixed list (WAV,
|
||||
/// AIFF, FLAC and ALAC take any rate their headers can hold).
|
||||
pub fn sample_rates(self) -> Option<&'static [u32]> {
|
||||
match self {
|
||||
EncodeFormat::Aac => Some(&[7350, 8000, 11_025, 12_000, 16_000, 22_050, 24_000, 32_000, 44_100, 48_000, 64_000, 88_200, 96_000]),
|
||||
EncodeFormat::Mp3 => Some(&[16_000, 22_050, 24_000, 32_000, 44_100, 48_000]),
|
||||
EncodeFormat::Ac3 | EncodeFormat::Eac3 => Some(&[32_000, 44_100, 48_000]),
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac | EncodeFormat::Alac => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The most channels a file of this format holds. A WAV's frame size is a 16-bit field, so it
|
||||
/// holds 65535 / bytes per sample: 32767 at 16 bits (the count given here), 21845 at 24 and
|
||||
/// 16383 at 32; [`EncodeOptions::check`] answers for the depth chosen.
|
||||
pub fn max_channels(self) -> u16 {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => 32_767,
|
||||
EncodeFormat::Flac | EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Eac3 => 8,
|
||||
EncodeFormat::Ac3 => 6,
|
||||
EncodeFormat::Mp3 => 2,
|
||||
}
|
||||
}
|
||||
|
||||
/// The speaker layouts of a format that has a fixed set of them, as WAV masks, for a channel
|
||||
/// count: one each for AAC, ALAC and MP3, one per coding mode for AC-3 and E-AC-3. Empty for a
|
||||
/// count the format has not, and for WAV, AIFF and FLAC, which have no such set.
|
||||
pub(crate) fn layout_masks(self, channels: u16) -> &'static [u32] {
|
||||
let per_count: &[&[u32]] = match self {
|
||||
// Channel configurations 1–6, the 6.1 program config element, and configuration 7
|
||||
// (7.1 with a front pair left and right of centre).
|
||||
EncodeFormat::Aac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x607], &[0x60F], &[0x70F], &[0xFF]],
|
||||
// Apple's layout tags for 1–8 channels, surrounds as back speakers.
|
||||
EncodeFormat::Alac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x37], &[0x3F], &[0x13F], &[0xFF]],
|
||||
EncodeFormat::Mp3 => &[&[0x4], &[0x3]],
|
||||
// The coding modes 1/0 to 3/2 with and without an LFE, by channel count.
|
||||
EncodeFormat::Ac3 => &[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F]],
|
||||
// The same, and 7.1 with backs and sides.
|
||||
EncodeFormat::Eac3 => {
|
||||
&[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F], &[], &[0x63F]]
|
||||
}
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => &[],
|
||||
};
|
||||
usize::from(channels).checked_sub(1).and_then(|i| per_count.get(i)).copied().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The speaker layouts a format with a fixed set of them has for `channels` channels: one for
|
||||
/// AAC, ALAC and MP3, one per coding mode for AC-3 and E-AC-3; empty when the format cannot
|
||||
/// hold that many channels. `None` for WAV, AIFF and FLAC, which take any layout. Audio in
|
||||
/// another layout has to be rearranged into one of these before it is written.
|
||||
///
|
||||
/// ```
|
||||
/// use craft_audio::{ChannelLayout, EncodeFormat};
|
||||
///
|
||||
/// assert_eq!(EncodeFormat::Aac.fixed_layouts(6), Some(vec![ChannelLayout::SURROUND_5_1_SIDE]));
|
||||
/// assert_eq!(EncodeFormat::Mp3.fixed_layouts(6), Some(vec![]));
|
||||
/// assert_eq!(EncodeFormat::Wav.fixed_layouts(6), None);
|
||||
/// ```
|
||||
pub fn fixed_layouts(self, channels: u16) -> Option<Vec<ChannelLayout>> {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => None,
|
||||
_ => Some(self.layout_masks(channels).iter().map(|&mask| ChannelLayout::from_mask(mask)).collect()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the format holds audio with `layout`: for WAV, AIFF and FLAC any layout of up to
|
||||
/// [`max_channels`](Self::max_channels) channels, for the others one of
|
||||
/// [`fixed_layouts`](Self::fixed_layouts), where side and back surrounds stand for each other
|
||||
/// in a layout with one surround pair.
|
||||
pub fn supports_layout(self, layout: &ChannelLayout) -> bool {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => (1..=self.max_channels()).contains(&layout.channels()),
|
||||
_ => self.layout_masks(layout.channels()).iter().any(|&mask| layout::channel_map(mask, None, layout).is_some()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the format takes [`RateControl::Quality`]: AAC. The other lossy formats are
|
||||
/// written at a constant bit rate only.
|
||||
pub fn supports_quality(self) -> bool {
|
||||
self == EncodeFormat::Aac
|
||||
}
|
||||
|
||||
/// Whether the format loses audio: AAC, MP3, AC-3 and E-AC-3. These take
|
||||
/// [`EncodeOptions::rate_control`] and ignore the bit depth and dither.
|
||||
pub fn is_lossy(self) -> bool {
|
||||
matches!(self, EncodeFormat::Aac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3)
|
||||
}
|
||||
}
|
||||
|
||||
/// How a lossy format spends bits ([`EncodeOptions::rate_control`]).
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum RateControl {
|
||||
/// A constant bit rate in bits per second, for all channels together. A format with a fixed
|
||||
/// set of rates (MP3, AC-3) uses the highest one not above it; a rate outside what the
|
||||
/// stream can carry is moved to the nearest one inside. AAC is written at 6 kbit/s or a
|
||||
/// quarter of the sample rate per channel at least: from about half of that down its encoder
|
||||
/// codes silence.
|
||||
Bitrate(u32),
|
||||
/// A constant quality from 0.0 (smallest) to 1.0 (best), at whatever bit rate each frame
|
||||
/// needs. AAC only; values outside the range are clamped, and NaN is an error.
|
||||
Quality(f32),
|
||||
}
|
||||
|
||||
/// Stored sample representation.
|
||||
@@ -418,9 +612,9 @@ pub enum BitDepth {
|
||||
pub struct EncodeOptions {
|
||||
/// Output file format.
|
||||
pub format: EncodeFormat,
|
||||
/// Sample depth to write.
|
||||
/// Sample depth to write (ignored by the lossy formats).
|
||||
pub bit_depth: BitDepth,
|
||||
/// Add TPDF dither when quantizing to an integer depth.
|
||||
/// Add TPDF dither when quantizing to an integer depth (ignored by the lossy formats).
|
||||
pub dither: bool,
|
||||
/// Write a BWF `bext` chunk (WAV only; ignored for other formats).
|
||||
pub bwf: Option<BwfInfo>,
|
||||
@@ -431,15 +625,27 @@ pub struct EncodeOptions {
|
||||
/// the count (they read 5 and 6 channels with side surrounds and 7 as 6.1 with sides, so the
|
||||
/// usual layouts for those counts get one; see the README's limits); AIFF has a fixed channel
|
||||
/// order and ignores it. It must have as many channels as the audio.
|
||||
///
|
||||
/// AAC, ALAC, MP3, AC-3 and E-AC-3 store channels in their own order, so here the layout says
|
||||
/// which speaker each channel of the audio (in WAV order) is, and the channels are reordered
|
||||
/// to match. Each of these formats has a fixed set of layouts (AAC and ALAC one per channel
|
||||
/// count, AC-3 one per coding mode); a layout outside it is [`AudioError::Unsupported`]. Side
|
||||
/// and back surrounds are taken as the same pair where the format has only one.
|
||||
pub layout: Option<ChannelLayout>,
|
||||
/// Write WAV in the RF64 layout even below 4 GiB (WAV only; ignored for other formats).
|
||||
pub force_rf64: bool,
|
||||
/// The bit rate or quality of a lossy format (ignored by the others). `None` is a constant
|
||||
/// 64 kbit/s per channel for AAC, 96 kbit/s per channel for MP3, and 96 kbit/s per
|
||||
/// full-bandwidth channel (the LFE not counted) for AC-3 and E-AC-3, moved to the nearest rate
|
||||
/// the stream can carry.
|
||||
pub rate_control: Option<RateControl>,
|
||||
}
|
||||
|
||||
impl EncodeOptions {
|
||||
/// Options for `format`: 24-bit, no dither, no BWF chunk, the usual layout.
|
||||
/// Options for `format`: 24-bit, no dither, no BWF chunk, the usual layout, the format's
|
||||
/// default bit rate.
|
||||
pub fn new(format: EncodeFormat) -> Self {
|
||||
Self { format, bit_depth: BitDepth::Int24, dither: false, bwf: None, layout: None, force_rf64: false }
|
||||
Self { format, bit_depth: BitDepth::Int24, dither: false, bwf: None, layout: None, force_rf64: false, rate_control: None }
|
||||
}
|
||||
|
||||
/// Sets `format`.
|
||||
@@ -477,6 +683,18 @@ impl EncodeOptions {
|
||||
self.force_rf64 = force_rf64;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets `rate_control` to a constant bit rate in bits per second ([`RateControl::Bitrate`]).
|
||||
pub fn with_bitrate(mut self, bits_per_second: u32) -> Self {
|
||||
self.rate_control = Some(RateControl::Bitrate(bits_per_second));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets `rate_control` to a constant quality from 0.0 to 1.0 ([`RateControl::Quality`]).
|
||||
pub fn with_quality(mut self, quality: f32) -> Self {
|
||||
self.rate_control = Some(RateControl::Quality(quality));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EncodeOptions {
|
||||
|
||||
@@ -0,0 +1,506 @@
|
||||
//! The coded formats of [`FileEncoder`](crate::FileEncoder): AAC-LC and ALAC in M4A (through
|
||||
//! craft-media's MP4 muxer) and MP3, AC-3 and E-AC-3 as raw streams. [`Writer`] maps the caller's
|
||||
//! WAV-order channels onto the encoder's order, drives the packet encoder, and for M4A records the
|
||||
//! encoder's priming and the exact length in the edit list.
|
||||
|
||||
// Each codec is its own feature, and so are the two sinks: with only some of them built, the
|
||||
// other halves of the shared code are unused.
|
||||
#![cfg_attr(
|
||||
not(all(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode")),
|
||||
allow(dead_code, unused_imports, unused_variables)
|
||||
)]
|
||||
|
||||
use super::failed_write_error;
|
||||
use super::pcm::validate_layout;
|
||||
use crate::layout::channel_map;
|
||||
use crate::{AudioBuffer, AudioError, ChannelLayout, EncodeFormat, EncodeOptions, RateControl, Result};
|
||||
use std::io::{Cursor, Seek, Write};
|
||||
|
||||
/// Frames handed to the encoder at a time, so its buffers and the packets waiting to be written
|
||||
/// stay small however long a block is. Also the frames of an ALAC packet.
|
||||
const CHUNK: usize = 4096;
|
||||
|
||||
/// Bits per second per channel when no rate is asked for: AAC.
|
||||
const AAC_DEFAULT_BPS_PER_CHANNEL: u32 = 64_000;
|
||||
/// Kilobits per second per channel when no rate is asked for: MP3, and AC-3 and E-AC-3 (per
|
||||
/// full-bandwidth channel).
|
||||
const DEFAULT_KBPS_PER_CHANNEL: u32 = 96;
|
||||
|
||||
/// The speakers of `mask` as labels: `"L R C LFE Ls Rs"`.
|
||||
fn labels(mask: u32) -> String {
|
||||
ChannelLayout::from_mask(mask).speakers().iter().map(|s| s.label()).collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
/// [`channel_map`] for a format with one layout per channel count ([`EncodeFormat::layout_masks`]),
|
||||
/// or why the audio does not fit.
|
||||
fn fixed_map(format: EncodeFormat, name: &str, order: Option<&[usize]>, layout: &ChannelLayout) -> Result<Vec<usize>> {
|
||||
let Some(&mask) = format.layout_masks(layout.channels()).first() else {
|
||||
return Err(AudioError::Unsupported(format!("{name} cannot hold {} channels", layout.channels())));
|
||||
};
|
||||
channel_map(mask, order, layout).ok_or_else(|| {
|
||||
AudioError::Unsupported(format!("{name} holds {} channels as {}; the audio is {}", layout.channels(), labels(mask), given(layout)))
|
||||
})
|
||||
}
|
||||
|
||||
/// The speakers of the caller's layout, for an error message.
|
||||
fn given(layout: &ChannelLayout) -> String {
|
||||
layout.mask().map_or_else(|| "without speaker assignment".into(), labels)
|
||||
}
|
||||
|
||||
/// The encoder's channel order over AAC's layout for the count: [`crate::aac::aac_order`], and for 8 channels
|
||||
/// configuration 7's C, Lc Rc, L R, Ls Rs, LFE over the WAV order L R C LFE Ls Rs Lc Rc.
|
||||
#[cfg(feature = "aac-encode")]
|
||||
fn aac_order(channels: usize) -> Option<&'static [usize]> {
|
||||
if channels == 8 { Some(&[2, 6, 7, 0, 1, 4, 5, 3]) } else { crate::aac::aac_order(channels) }
|
||||
}
|
||||
|
||||
/// The lowest AAC bit rate the writer uses, per channel: 6 kbit/s, and a quarter of the sample
|
||||
/// rate (256 bits a frame). The encoder accepts less, and from about half of this down codes
|
||||
/// silence; a file of silence is no answer to a low bit rate (or to kbit/s given as bit/s).
|
||||
#[cfg(feature = "aac-encode")]
|
||||
fn aac_floor_per_channel(sample_rate: u32) -> u32 {
|
||||
(sample_rate / 4).max(6000)
|
||||
}
|
||||
|
||||
/// The AAC rate control for `rate`: a bit rate is kept within what the stream can carry, a
|
||||
/// quality of 0–1 spans the encoder's VBR range.
|
||||
#[cfg(feature = "aac-encode")]
|
||||
fn aac_mode(rate: Option<RateControl>, sample_rate: u32, channels: usize) -> crate::aac::BitrateMode {
|
||||
use crate::aac::BitrateMode;
|
||||
let (lo, hi) = crate::aac::cbr_range(sample_rate, channels);
|
||||
let lo = lo.max(aac_floor_per_channel(sample_rate).saturating_mul(channels as u32)).min(hi.max(lo));
|
||||
// An empty range (a rate or channel count the encoder refuses anyway) leaves the rate alone.
|
||||
let fit = |bps: u32| if lo <= hi { bps.clamp(lo, hi) } else { bps };
|
||||
match rate {
|
||||
None => BitrateMode::Cbr(fit(AAC_DEFAULT_BPS_PER_CHANNEL.saturating_mul(channels as u32))),
|
||||
Some(RateControl::Bitrate(bps)) => BitrateMode::Cbr(fit(bps)),
|
||||
// NaN stays NaN, which the encoder refuses.
|
||||
Some(RateControl::Quality(q)) => BitrateMode::Vbr(1.0 + 4.0 * q.clamp(0.0, 1.0)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The bit rate in kbit/s for a codec with constant bit rates only.
|
||||
fn constant_kbps(name: &str, rate: Option<RateControl>, default: u32) -> Result<u32> {
|
||||
match rate {
|
||||
None => Ok(default),
|
||||
Some(RateControl::Bitrate(bps)) => Ok(bps / 1000),
|
||||
Some(RateControl::Quality(_)) => {
|
||||
Err(AudioError::Unsupported(format!("{name} is encoded at a constant bit rate; set a bit rate, not a quality")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The AC-3 or E-AC-3 settings for audio with `layout`, and the channel map onto them: the coding
|
||||
/// mode whose speakers the layout names, at the nearest bit rate the stream can carry.
|
||||
#[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
fn ac3_config(
|
||||
codec: crate::ac3::encoder::Codec,
|
||||
sample_rate: u32,
|
||||
layout: &ChannelLayout,
|
||||
rate: Option<RateControl>,
|
||||
) -> Result<(crate::ac3::encoder::Config, Vec<usize>)> {
|
||||
use crate::ac3::encoder::{Codec, CodingMode, Config};
|
||||
use CodingMode::*;
|
||||
let name = if codec == Codec::Ac3 { "AC-3" } else { "E-AC-3" };
|
||||
let modes = [Mono, Stereo, ThreeFront, StereoSurround, ThreeFrontSurround, StereoTwoSurround, ThreeFrontTwoSurround, Surround71];
|
||||
let found = modes.iter().flat_map(|&mode| [false, true].map(|lfe| (mode, lfe))).find_map(|(mode, lfe)| {
|
||||
// 7.1 is an E-AC-3 programme with an LFE; the encoder has no other.
|
||||
if mode == Surround71 && (codec != Codec::Eac3 || !lfe) {
|
||||
return None;
|
||||
}
|
||||
let config = Config { codec, sample_rate, bitrate_kbps: 0, mode, lfe };
|
||||
let map = channel_map(config.channel_layout().mask()?, None, layout)?;
|
||||
Some((config, map))
|
||||
});
|
||||
let Some((mut config, map)) = found else {
|
||||
return Err(AudioError::Unsupported(format!("{name} has no coding mode for {} channels that are {}", layout.channels(), given(layout))));
|
||||
};
|
||||
let full = config.mode.channels() as u32;
|
||||
let want = constant_kbps(name, rate, DEFAULT_KBPS_PER_CHANNEL * full)?;
|
||||
// What `Config::validate` asks of the rate: enough for the channels, and for AC-3 an entry of
|
||||
// the frame size table, for E-AC-3 a syncframe its 11-bit word count can hold.
|
||||
let least = if config.mode == Surround71 { 384 } else { 32 * full };
|
||||
config.bitrate_kbps = match codec {
|
||||
Codec::Ac3 => {
|
||||
let floor = crate::ac3::BITRATES.iter().copied().find(|&r| r >= least).unwrap_or(least);
|
||||
crate::ac3::BITRATES.iter().rev().copied().find(|&r| r <= want).filter(|&r| r >= floor).unwrap_or(floor)
|
||||
}
|
||||
Codec::Eac3 => {
|
||||
let most = if config.mode == Surround71 { 1024 } else { (2048 * u64::from(sample_rate) / 96_000).min(1024) as u32 };
|
||||
want.clamp(least, most.max(least))
|
||||
}
|
||||
};
|
||||
Ok((config, map))
|
||||
}
|
||||
|
||||
/// One encoded packet and the frames it decodes to.
|
||||
struct Packet {
|
||||
data: Vec<u8>,
|
||||
frames: u32,
|
||||
}
|
||||
|
||||
/// A packet encoder of any of the codecs, taking planar `f32` in its own channel order.
|
||||
enum Codec {
|
||||
#[cfg(feature = "aac-encode")]
|
||||
Aac(Box<crate::aac::Encoder>),
|
||||
#[cfg(feature = "alac-encode")]
|
||||
Alac {
|
||||
enc: Box<crate::alac::Encoder>,
|
||||
quantizers: Vec<super::pcm::Quantizer>,
|
||||
/// Quantized samples of the packet being filled, per channel.
|
||||
pending: Vec<Vec<i32>>,
|
||||
},
|
||||
#[cfg(feature = "mp3-encode")]
|
||||
Mp3(Box<crate::mp3::Encoder>),
|
||||
#[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Ac3 {
|
||||
enc: Box<crate::ac3::encoder::Encoder>,
|
||||
/// The block with every sample finite and within ±1, as the encoder requires.
|
||||
planes: Vec<Vec<f32>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Codec {
|
||||
/// Encodes `planes` (one slice per encoder channel, equally long, at most [`CHUNK`] frames)
|
||||
/// and appends the packets completed.
|
||||
fn push(&mut self, planes: &[&[f32]], out: &mut Vec<Packet>) -> Result<()> {
|
||||
match self {
|
||||
#[cfg(feature = "aac-encode")]
|
||||
Codec::Aac(enc) => out.extend(enc.encode(planes).into_iter().map(|data| Packet { data, frames: crate::aac::FRAME_LEN as u32 })),
|
||||
#[cfg(feature = "alac-encode")]
|
||||
Codec::Alac { enc, quantizers, pending } => {
|
||||
let frames = planes.first().map_or(0, |p| p.len());
|
||||
let mut at = 0;
|
||||
while at < frames {
|
||||
let filled = pending.first().map_or(0, Vec::len);
|
||||
let end = frames.min(at + CHUNK.saturating_sub(filled).max(1));
|
||||
for ((dst, q), src) in pending.iter_mut().zip(quantizers.iter_mut()).zip(planes) {
|
||||
dst.extend(src.get(at..end).unwrap_or_default().iter().map(|&s| q.quantize(s)));
|
||||
}
|
||||
at = end;
|
||||
if pending.first().is_some_and(|p| p.len() >= CHUNK) {
|
||||
alac_packet(enc, pending, out)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "mp3-encode")]
|
||||
Codec::Mp3(enc) => {
|
||||
let data = enc.encode(planes);
|
||||
if !data.is_empty() {
|
||||
out.push(Packet { data, frames: 0 });
|
||||
}
|
||||
}
|
||||
#[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Codec::Ac3 { enc, planes: clean } => {
|
||||
for (dst, src) in clean.iter_mut().zip(planes) {
|
||||
dst.clear();
|
||||
dst.extend(src.iter().map(|&s| if s.is_nan() { 0.0 } else { s.clamp(-1.0, 1.0) }));
|
||||
}
|
||||
let refs: Vec<&[f32]> = clean.iter().map(Vec::as_slice).collect();
|
||||
out.extend(enc.push(&refs)?.into_iter().map(|p| Packet { data: p.data, frames: p.samples }));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encodes what the encoder still holds and appends the last packets.
|
||||
fn finish(&mut self, out: &mut Vec<Packet>) -> Result<()> {
|
||||
match self {
|
||||
#[cfg(feature = "aac-encode")]
|
||||
Codec::Aac(enc) => out.extend(enc.flush().into_iter().map(|data| Packet { data, frames: crate::aac::FRAME_LEN as u32 })),
|
||||
#[cfg(feature = "alac-encode")]
|
||||
Codec::Alac { enc, pending, .. } => {
|
||||
if pending.first().is_some_and(|p| !p.is_empty()) {
|
||||
alac_packet(enc, pending, out)?;
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "mp3-encode")]
|
||||
Codec::Mp3(enc) => {
|
||||
let data = enc.flush();
|
||||
if !data.is_empty() {
|
||||
out.push(Packet { data, frames: 0 });
|
||||
}
|
||||
}
|
||||
#[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Codec::Ac3 { enc, .. } => out.extend(enc.finish()?.packets.into_iter().map(|p| Packet { data: p.data, frames: p.samples })),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decoded frames before the first frame of the audio.
|
||||
fn priming(&self) -> u32 {
|
||||
match self {
|
||||
#[cfg(feature = "aac-encode")]
|
||||
Codec::Aac(enc) => enc.priming_samples(),
|
||||
#[allow(unreachable_patterns)]
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// The MP4 sample entry of the stream as encoded so far (`bytes` in packets of `frames`
|
||||
/// frames in all): the configuration a decoder needs, with the average bit rate.
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
fn sample_entry(&self, sample_rate: u32, channels: u32, bytes: u64, frames: u64) -> Option<craft_media::isobmff::SampleEntry> {
|
||||
use craft_media::isobmff::{CodecConfig, FourCc, SampleEntry};
|
||||
match self {
|
||||
#[cfg(feature = "aac-encode")]
|
||||
Codec::Aac(enc) => {
|
||||
let mut entry = SampleEntry::aac(enc.audio_specific_config(), channels, sample_rate);
|
||||
if let CodecConfig::Aac(config) = &mut entry.codec
|
||||
&& frames > 0
|
||||
{
|
||||
let bps = u128::from(bytes) * 8 * u128::from(sample_rate) / u128::from(frames);
|
||||
config.avg_bitrate = u32::try_from(bps).unwrap_or(u32::MAX);
|
||||
}
|
||||
Some(entry)
|
||||
}
|
||||
#[cfg(feature = "alac-encode")]
|
||||
Codec::Alac { enc, .. } => {
|
||||
let bits = u16::from(enc.encoder_config().bit_depth);
|
||||
Some(SampleEntry::audio(FourCc(*b"alac"), CodecConfig::Alac { cookie: enc.cookie() }, channels, f64::from(sample_rate), bits))
|
||||
}
|
||||
#[allow(unreachable_patterns)]
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes the pending samples as one ALAC packet and empties them.
|
||||
#[cfg(feature = "alac-encode")]
|
||||
fn alac_packet(enc: &mut crate::alac::Encoder, pending: &mut [Vec<i32>], out: &mut Vec<Packet>) -> Result<()> {
|
||||
let refs: Vec<&[i32]> = pending.iter().map(Vec::as_slice).collect();
|
||||
let frames = refs.first().map_or(0, |p| p.len()) as u32;
|
||||
let data = enc.encode(&refs)?;
|
||||
pending.iter_mut().for_each(Vec::clear);
|
||||
out.push(Packet { data, frames });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A muxer's error as this crate's: a failed write stays an I/O error.
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
fn mux_error(e: craft_media::isobmff::Error) -> AudioError {
|
||||
match e {
|
||||
craft_media::isobmff::Error::Io(e) => AudioError::Io(e),
|
||||
e => AudioError::Encode(format!("M4A: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// An M4A file being written: one audio track whose samples are the packets.
|
||||
///
|
||||
/// The last packet is held back until the length is known: its duration is cut to the audio's
|
||||
/// last frame, so the sample durations add up to the priming plus the audio, and the edit that
|
||||
/// skips the priming ends where the audio does. The movie timescale is the sample rate, which
|
||||
/// keeps that edit exact.
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
struct M4a<W: Write + Seek> {
|
||||
mux: craft_media::isobmff::Mp4Writer<W>,
|
||||
/// The track, added with the first packet: a file with no audio gets a track without an edit.
|
||||
track: Option<usize>,
|
||||
entry: craft_media::isobmff::SampleEntry,
|
||||
sample_rate: u32,
|
||||
channels: u32,
|
||||
priming: u32,
|
||||
held: Option<Packet>,
|
||||
/// Frames and bytes of the packets written, the held one not counted.
|
||||
frames: u64,
|
||||
bytes: u64,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
impl<W: Write + Seek> M4a<W> {
|
||||
fn new(out: W, codec: &Codec, sample_rate: u32, channels: u32) -> Result<Self> {
|
||||
use craft_media::isobmff::{Brand, Mp4Writer, WriterOptions};
|
||||
let entry = codec.sample_entry(sample_rate, channels, 0, 0).ok_or_else(|| AudioError::Unsupported("M4A holds AAC and ALAC".into()))?;
|
||||
let mut options = WriterOptions::new(Brand::Mp4);
|
||||
options.movie_timescale = sample_rate;
|
||||
let mux = Mp4Writer::new(out, options).map_err(mux_error)?;
|
||||
Ok(M4a { mux, track: None, entry, sample_rate, channels, priming: codec.priming(), held: None, frames: 0, bytes: 0 })
|
||||
}
|
||||
|
||||
fn track(&mut self, has_audio: bool) -> Result<usize> {
|
||||
if let Some(track) = self.track {
|
||||
return Ok(track);
|
||||
}
|
||||
let mut config = craft_media::isobmff::TrackConfig::new(self.entry.clone(), self.sample_rate);
|
||||
if has_audio && self.priming > 0 {
|
||||
config.media_start = Some(i64::from(self.priming));
|
||||
// AAC's overlap: a packet decodes right only after the one before it. Apple's
|
||||
// decoders take the priming from the edit only when the track says so.
|
||||
config.roll_distance = Some(-1);
|
||||
}
|
||||
let track = self.mux.add_track(config).map_err(mux_error)?;
|
||||
self.track = Some(track);
|
||||
Ok(track)
|
||||
}
|
||||
|
||||
fn sample(&mut self, packet: &Packet, duration: u32) -> Result<()> {
|
||||
let track = self.track(true)?;
|
||||
let sample = craft_media::isobmff::WriteSample { data: &packet.data, duration, composition_offset: 0, is_sync: true };
|
||||
self.mux.write_sample(track, sample).map_err(mux_error)?;
|
||||
self.frames += u64::from(duration);
|
||||
self.bytes += packet.data.len() as u64;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write(&mut self, packet: Packet) -> Result<()> {
|
||||
if let Some(held) = self.held.replace(packet) {
|
||||
self.sample(&held, held.frames)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes the last packet, cut to the end of `audio_frames` frames of audio, and the `moov`.
|
||||
fn finish(mut self, codec: &Codec, audio_frames: u64) -> Result<W> {
|
||||
if let Some(held) = self.held.take() {
|
||||
let rest = (u64::from(self.priming) + audio_frames).saturating_sub(self.frames);
|
||||
self.sample(&held, rest.min(u64::from(held.frames)) as u32)?;
|
||||
}
|
||||
let track = self.track(false)?;
|
||||
// The configuration is complete only now: the bit rate, and ALAC's largest packet.
|
||||
if let Some(entry) = codec.sample_entry(self.sample_rate, self.channels, self.bytes, self.frames) {
|
||||
self.mux.set_sample_entry(track, entry).map_err(mux_error)?;
|
||||
}
|
||||
self.mux.finish().map_err(mux_error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the packets go.
|
||||
enum Sink<W: Write + Seek> {
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
M4a(Box<M4a<W>>),
|
||||
/// The packets in a row are the file.
|
||||
Raw(W),
|
||||
}
|
||||
|
||||
/// A file of one of the coded formats written block by block.
|
||||
pub(crate) struct Writer<W: Write + Seek> {
|
||||
codec: Codec,
|
||||
sink: Sink<W>,
|
||||
/// For each encoder channel, the caller's channel that feeds it.
|
||||
map: Vec<usize>,
|
||||
/// The constant bit rate the encoder was set to, bits per second; `None` for ALAC and for
|
||||
/// AAC at a constant quality.
|
||||
bitrate: Option<u32>,
|
||||
frames: u64,
|
||||
packets: Vec<Packet>,
|
||||
/// Set when a write has failed: the encoder and the sink no longer agree, so the writer
|
||||
/// refuses every later call.
|
||||
failed: bool,
|
||||
}
|
||||
|
||||
impl<W: Write + Seek> Writer<W> {
|
||||
pub(crate) fn new(out: W, opts: &EncodeOptions, sample_rate: u32, channels: usize) -> Result<Self> {
|
||||
validate_layout(channels, sample_rate)?;
|
||||
let layout = opts.layout.unwrap_or_else(|| ChannelLayout::default_for(channels as u16));
|
||||
let (codec, map, bitrate) = match opts.format {
|
||||
#[cfg(feature = "aac-encode")]
|
||||
EncodeFormat::Aac => {
|
||||
let map = fixed_map(EncodeFormat::Aac, "AAC", aac_order(channels), &layout)?;
|
||||
let mode = aac_mode(opts.rate_control, sample_rate, channels);
|
||||
let config = crate::aac::EncoderConfig { mode, ..crate::aac::EncoderConfig::cbr(sample_rate, channels, 0) };
|
||||
let bitrate = match mode {
|
||||
crate::aac::BitrateMode::Cbr(bps) => Some(bps),
|
||||
crate::aac::BitrateMode::Vbr(_) => None,
|
||||
};
|
||||
(Codec::Aac(Box::new(crate::aac::Encoder::new(config)?)), map, bitrate)
|
||||
}
|
||||
#[cfg(feature = "alac-encode")]
|
||||
EncodeFormat::Alac => {
|
||||
let count = u8::try_from(channels).unwrap_or(u8::MAX);
|
||||
let map = fixed_map(EncodeFormat::Alac, "ALAC", crate::alac::alac_order(count), &layout)?;
|
||||
let bits = match opts.bit_depth {
|
||||
crate::BitDepth::Int16 => 16,
|
||||
crate::BitDepth::Int24 => 24,
|
||||
crate::BitDepth::Int32 => 32,
|
||||
crate::BitDepth::Float32 => return Err(AudioError::Unsupported("ALAC cannot store 32-bit float samples".into())),
|
||||
};
|
||||
let enc = crate::alac::Encoder::new(crate::alac::EncoderConfig::new(sample_rate, count, bits).with_frame_length(CHUNK as u32))?;
|
||||
let quantizers = super::pcm::Quantizer::per_channel(u32::from(bits), opts.dither, channels);
|
||||
(Codec::Alac { enc: Box::new(enc), quantizers, pending: vec![Vec::new(); channels] }, map, None)
|
||||
}
|
||||
#[cfg(feature = "mp3-encode")]
|
||||
EncodeFormat::Mp3 => {
|
||||
let map = fixed_map(EncodeFormat::Mp3, "MP3", None, &layout)?;
|
||||
let want = constant_kbps("MP3", opts.rate_control, DEFAULT_KBPS_PER_CHANNEL * channels as u32)?;
|
||||
// A sample rate MP3 has not is the encoder's to refuse.
|
||||
let kbps = crate::mp3::nearest_bitrate(sample_rate, want).unwrap_or(want);
|
||||
let enc = crate::mp3::Encoder::new(crate::mp3::Config::cbr(sample_rate, channels, kbps))?;
|
||||
(Codec::Mp3(Box::new(enc)), map, Some(kbps.saturating_mul(1000)))
|
||||
}
|
||||
#[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
EncodeFormat::Ac3 | EncodeFormat::Eac3 => {
|
||||
let codec = if opts.format == EncodeFormat::Ac3 { crate::ac3::encoder::Codec::Ac3 } else { crate::ac3::encoder::Codec::Eac3 };
|
||||
let (config, map) = ac3_config(codec, sample_rate, &layout, opts.rate_control)?;
|
||||
let enc = crate::ac3::encoder::Encoder::new(config)?;
|
||||
(Codec::Ac3 { enc: Box::new(enc), planes: vec![Vec::new(); channels] }, map, Some(config.bitrate_kbps.saturating_mul(1000)))
|
||||
}
|
||||
#[allow(unreachable_patterns)]
|
||||
f => return Err(AudioError::Unsupported(format!("writing {f:?}"))),
|
||||
};
|
||||
let sink = match opts.format {
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => Sink::M4a(Box::new(M4a::new(out, &codec, sample_rate, channels as u32)?)),
|
||||
_ => Sink::Raw(out),
|
||||
};
|
||||
Ok(Writer { codec, sink, map, bitrate, frames: 0, packets: Vec::new(), failed: false })
|
||||
}
|
||||
|
||||
pub(crate) fn bitrate(&self) -> Option<u32> {
|
||||
self.bitrate
|
||||
}
|
||||
|
||||
/// Hands the packets waiting to the sink.
|
||||
fn drain(&mut self) -> Result<()> {
|
||||
for packet in self.packets.drain(..) {
|
||||
match &mut self.sink {
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
Sink::M4a(m4a) => m4a.write(packet)?,
|
||||
Sink::Raw(out) => out.write_all(&packet.data)?,
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Appends `frames` frames of `block`, which holds every channel at least that long.
|
||||
pub(crate) fn write<C: AsRef<[f32]>>(&mut self, block: &[C], frames: usize) -> Result<()> {
|
||||
if self.failed {
|
||||
return Err(failed_write_error());
|
||||
}
|
||||
for at in (0..frames).step_by(CHUNK) {
|
||||
let end = frames.min(at + CHUNK);
|
||||
let planes: Vec<&[f32]> = self.map.iter().map(|&c| block.get(c).and_then(|p| p.as_ref().get(at..end)).unwrap_or_default()).collect();
|
||||
let written = self.codec.push(&planes, &mut self.packets).and_then(|()| self.drain());
|
||||
if let Err(e) = written {
|
||||
self.failed = true;
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
self.frames = self.frames.saturating_add(frames as u64);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn finish(mut self) -> Result<W> {
|
||||
if self.failed {
|
||||
return Err(failed_write_error());
|
||||
}
|
||||
self.codec.finish(&mut self.packets)?;
|
||||
self.drain()?;
|
||||
match self.sink {
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode"))]
|
||||
Sink::M4a(m4a) => m4a.finish(&self.codec, self.frames),
|
||||
Sink::Raw(mut out) => {
|
||||
out.flush()?;
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result<Vec<u8>> {
|
||||
let mut w = Writer::new(Cursor::new(Vec::new()), opts, buf.sample_rate, buf.channels.len())?;
|
||||
w.write(&buf.channels, buf.frames())?;
|
||||
Ok(w.finish()?.into_inner())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,167 @@
|
||||
use super::*;
|
||||
|
||||
use crate::Speaker;
|
||||
|
||||
fn map(mask: u32, order: Option<&[usize]>, layout: u32) -> Option<Vec<usize>> {
|
||||
channel_map(mask, order, &ChannelLayout::from_mask(layout))
|
||||
}
|
||||
|
||||
/// Channels are matched by speaker, not by position: a layout that lists the same speakers in
|
||||
/// another WAV order is reordered, and one with other speakers is refused.
|
||||
#[test]
|
||||
fn channels_are_mapped_by_speaker() {
|
||||
// L R C LFE Ls Rs onto itself, and onto an encoder order C L R Ls Rs LFE.
|
||||
assert_eq!(map(0x60F, None, 0x60F), Some(vec![0, 1, 2, 3, 4, 5]));
|
||||
assert_eq!(map(0x60F, Some(&[2, 0, 1, 4, 5, 3]), 0x60F), Some(vec![2, 0, 1, 4, 5, 3]));
|
||||
// Back surrounds feed a format that has one surround pair, and the other way round.
|
||||
assert_eq!(map(0x60F, None, 0x3F), Some(vec![0, 1, 2, 3, 4, 5]));
|
||||
assert_eq!(map(0x3F, None, 0x60F), Some(vec![0, 1, 2, 3, 4, 5]));
|
||||
// 6.1 with the back centre after the pair (L R C LFE Lb Rb Cb) onto L R C LFE Cb Ls Rs.
|
||||
assert_eq!(map(0x70F, None, 0x13F), Some(vec![0, 1, 2, 3, 6, 4, 5]));
|
||||
// Quad has no centre; 7.1 with sides and backs has no front pair left and right of centre.
|
||||
assert_eq!(map(0x107, None, 0x33), None);
|
||||
assert_eq!(map(0xFF, None, 0x63F), None);
|
||||
// With both pairs in the format, one pair cannot feed both.
|
||||
assert_eq!(map(0x63F, None, 0xFF), None);
|
||||
assert_eq!(channel_map(0x3, None, &ChannelLayout::unordered(2)), None);
|
||||
assert_eq!(map(0x3, None, 0x7), None);
|
||||
}
|
||||
|
||||
#[cfg(feature = "aac-encode")]
|
||||
#[test]
|
||||
fn every_aac_channel_count_has_a_layout_and_an_order() {
|
||||
for channels in 1..=8 {
|
||||
let mask = EncodeFormat::Aac.layout_masks(channels as u16)[0];
|
||||
assert_eq!(mask.count_ones() as usize, channels);
|
||||
let mut order = map(mask, aac_order(channels), mask).unwrap();
|
||||
order.sort_unstable();
|
||||
assert_eq!(order, (0..channels).collect::<Vec<_>>());
|
||||
}
|
||||
assert!(EncodeFormat::Aac.layout_masks(9).is_empty() && EncodeFormat::Aac.layout_masks(0).is_empty());
|
||||
// The layouts this crate's decoder reports for what the encoder writes (`track::aac_layout`),
|
||||
// and configuration 7's speakers for 8 channels.
|
||||
let masks: Vec<u32> = (1..=8).map(|channels| EncodeFormat::Aac.layout_masks(channels)[0]).collect();
|
||||
assert_eq!(masks, [0x4, 0x3, 0x7, 0x107, 0x607, 0x60F, 0x70F, 0xFF]);
|
||||
assert_eq!(
|
||||
(EncodeFormat::Mp3.layout_masks(1), EncodeFormat::Mp3.layout_masks(2), EncodeFormat::Mp3.layout_masks(3)),
|
||||
(&[0x4][..], &[0x3][..], &[][..])
|
||||
);
|
||||
// Configuration 7: C, Lc Rc, L R, Ls Rs, LFE.
|
||||
let wide: Vec<Speaker> = ChannelLayout::from_mask(0xFF).speakers();
|
||||
let named: Vec<Speaker> = aac_order(8).unwrap().iter().map(|&c| wide[c]).collect();
|
||||
use Speaker::*;
|
||||
assert_eq!(named, [FrontCenter, FrontLeftOfCenter, FrontRightOfCenter, FrontLeft, FrontRight, BackLeft, BackRight, Lfe]);
|
||||
}
|
||||
|
||||
/// The range the writer keeps a bit rate in is the one the encoder accepts.
|
||||
#[cfg(feature = "aac-encode")]
|
||||
#[test]
|
||||
fn the_aac_bit_rate_range_is_the_encoders() {
|
||||
use crate::aac::{Encoder, EncoderConfig, SAMPLE_RATES};
|
||||
for &rate in &SAMPLE_RATES {
|
||||
for channels in 1..=8 {
|
||||
let (lo, hi) = crate::aac::cbr_range(rate, channels);
|
||||
assert!(lo <= hi, "{rate} Hz, {channels} channels");
|
||||
let accepts = |bps| Encoder::new(EncoderConfig::cbr(rate, channels, bps)).is_ok();
|
||||
assert!(accepts(lo) && accepts(hi), "{rate} Hz, {channels} channels: {lo}..={hi}");
|
||||
assert!(!accepts(lo - 1) && !accepts(hi + 1), "{rate} Hz, {channels} channels: {lo}..={hi}");
|
||||
for want in [None, Some(RateControl::Bitrate(0)), Some(RateControl::Bitrate(u32::MAX))] {
|
||||
let mode = aac_mode(want, rate, channels);
|
||||
assert!(
|
||||
Encoder::new(EncoderConfig { mode, ..EncoderConfig::cbr(rate, channels, 0) }).is_ok(),
|
||||
"{rate} Hz, {channels} channels, {want:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every layout the AC-3 encoders have is found from its speakers, at a bit rate the encoder
|
||||
/// accepts whatever rate was asked for.
|
||||
#[cfg(all(feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
#[test]
|
||||
fn ac3_settings_follow_the_layout_and_validate() {
|
||||
use crate::ac3::encoder::{Codec, CodingMode};
|
||||
let layouts = [
|
||||
(0x4, CodingMode::Mono, false),
|
||||
(0x3, CodingMode::Stereo, false),
|
||||
(0xB, CodingMode::Stereo, true),
|
||||
(0x7, CodingMode::ThreeFront, false),
|
||||
(0x103, CodingMode::StereoSurround, false),
|
||||
(0x107, CodingMode::ThreeFrontSurround, false),
|
||||
(0x33, CodingMode::StereoTwoSurround, false),
|
||||
(0x603, CodingMode::StereoTwoSurround, false),
|
||||
(0x37, CodingMode::ThreeFrontTwoSurround, false),
|
||||
(0x3F, CodingMode::ThreeFrontTwoSurround, true),
|
||||
(0x60F, CodingMode::ThreeFrontTwoSurround, true),
|
||||
];
|
||||
for codec in [Codec::Ac3, Codec::Eac3] {
|
||||
for sample_rate in [32_000, 44_100, 48_000] {
|
||||
for (mask, mode, lfe) in layouts {
|
||||
for want in [None, Some(RateControl::Bitrate(0)), Some(RateControl::Bitrate(200_000)), Some(RateControl::Bitrate(u32::MAX))] {
|
||||
let layout = ChannelLayout::from_mask(mask);
|
||||
let (config, map) = ac3_config(codec, sample_rate, &layout, want).unwrap();
|
||||
assert_eq!((config.mode, config.lfe), (mode, lfe), "{mask:#x}");
|
||||
assert_eq!(map, (0..usize::from(layout.channels())).collect::<Vec<_>>());
|
||||
assert_eq!(config.validate(), Ok(()), "{codec:?} {sample_rate} {mask:#x} {want:?}: {} kbit/s", config.bitrate_kbps);
|
||||
}
|
||||
}
|
||||
let seven_one = ac3_config(codec, sample_rate, &ChannelLayout::SURROUND_7_1, Some(RateControl::Bitrate(u32::MAX)));
|
||||
match codec {
|
||||
Codec::Ac3 => assert!(matches!(seven_one, Err(AudioError::Unsupported(_)))),
|
||||
Codec::Eac3 => assert_eq!(seven_one.unwrap().0.validate(), Ok(())),
|
||||
}
|
||||
}
|
||||
}
|
||||
// The highest table rate not above the one asked for; the default is 96 kbit/s per channel.
|
||||
let rate = |codec, mask, want| ac3_config(codec, 48_000, &ChannelLayout::from_mask(mask), want).unwrap().0.bitrate_kbps;
|
||||
assert_eq!(rate(Codec::Ac3, 0x3, Some(RateControl::Bitrate(200_000))), 192);
|
||||
assert_eq!(rate(Codec::Ac3, 0x3, None), 192);
|
||||
assert_eq!(rate(Codec::Ac3, 0x60F, None), 448);
|
||||
assert_eq!(rate(Codec::Ac3, 0x60F, Some(RateControl::Bitrate(1))), 160);
|
||||
assert_eq!(rate(Codec::Eac3, 0x3, Some(RateControl::Bitrate(200_500))), 200);
|
||||
assert!(matches!(ac3_config(Codec::Ac3, 48_000, &ChannelLayout::STEREO, Some(RateControl::Quality(0.5))), Err(AudioError::Unsupported(_))));
|
||||
assert!(matches!(ac3_config(Codec::Ac3, 48_000, &ChannelLayout::unordered(2), None), Err(AudioError::Unsupported(_))));
|
||||
}
|
||||
|
||||
/// The layouts `EncodeFormat` lists are the encoders' own: ALAC's layout per channel count, and
|
||||
/// the speakers of every AC-3 and E-AC-3 coding mode the encoder has.
|
||||
#[cfg(all(feature = "alac-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
#[test]
|
||||
fn the_listed_layouts_are_the_encoders() {
|
||||
use crate::ac3::encoder::{Codec, CodingMode::*, Config};
|
||||
for channels in 0..=10u16 {
|
||||
let alac = crate::alac::channel_mask(channels as u8);
|
||||
assert_eq!(EncodeFormat::Alac.layout_masks(channels), if alac == 0 { &[][..] } else { std::slice::from_ref(&alac) }, "ALAC, {channels}");
|
||||
for (format, codec) in [(EncodeFormat::Ac3, Codec::Ac3), (EncodeFormat::Eac3, Codec::Eac3)] {
|
||||
let modes = [Mono, Stereo, ThreeFront, StereoSurround, ThreeFrontSurround, StereoTwoSurround, ThreeFrontTwoSurround, Surround71];
|
||||
let mut encoder: Vec<u32> = modes
|
||||
.iter()
|
||||
.flat_map(|&mode| [false, true].map(|lfe| Config { codec, sample_rate: 48_000, bitrate_kbps: 640, mode, lfe }))
|
||||
.filter(|config| config.validate().is_ok() && config.channels() == usize::from(channels))
|
||||
.filter_map(|config| config.channel_layout().mask())
|
||||
.collect();
|
||||
let mut listed = format.layout_masks(channels).to_vec();
|
||||
encoder.sort_unstable();
|
||||
listed.sort_unstable();
|
||||
assert_eq!(listed, encoder, "{format:?}, {channels} channels");
|
||||
for &mask in &listed {
|
||||
let layout = ChannelLayout::from_mask(mask);
|
||||
assert_eq!(ac3_config(codec, 48_000, &layout, None).unwrap().0.channel_layout(), layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The sample rates `EncodeFormat` lists are the encoders'.
|
||||
#[cfg(all(feature = "aac-encode", feature = "mp3-encode"))]
|
||||
#[test]
|
||||
fn the_listed_sample_rates_are_the_encoders() {
|
||||
let sorted = |rates: &[u32]| {
|
||||
let mut rates = rates.to_vec();
|
||||
rates.sort_unstable();
|
||||
rates
|
||||
};
|
||||
assert_eq!(EncodeFormat::Aac.sample_rates().unwrap(), sorted(&crate::aac::SAMPLE_RATES));
|
||||
assert_eq!(EncodeFormat::Mp3.sample_rates().unwrap(), sorted(&crate::mp3::SAMPLE_RATES));
|
||||
}
|
||||
+139
-43
@@ -1,8 +1,11 @@
|
||||
//! Encoding: WAV/BWF/RF64 and AIFF/AIFF-C writers (`pcm-encode`) and the FLAC encoder
|
||||
//! (`flac-encode`), whole-buffer and streaming.
|
||||
//! Encoding: WAV/BWF/RF64 and AIFF/AIFF-C writers (`pcm-encode`), the FLAC encoder
|
||||
//! (`flac-encode`) and the coded formats (AAC and ALAC in M4A, MP3, AC-3 and E-AC-3 streams, each
|
||||
//! with its codec's feature), whole-buffer and streaming.
|
||||
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
mod aiff;
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
mod coded;
|
||||
#[cfg(feature = "flac-encode")]
|
||||
pub(crate) mod flac;
|
||||
mod pcm;
|
||||
@@ -16,9 +19,14 @@ use crate::{AudioBuffer, AudioError, ChannelLayout, EncodeFormat, EncodeOptions,
|
||||
pub use raw::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header};
|
||||
use std::io::{Seek, Write};
|
||||
|
||||
/// Encode `buf` as WAV/BWF, AIFF/AIFF-C or FLAC. Integer depths clamp samples to [-1, 1]; float
|
||||
/// Encode `buf` as the file `opts.format` names. Integer depths clamp samples to [-1, 1]; float
|
||||
/// keeps values above full scale. `opts.layout`, when set, must have `buf`'s channel count.
|
||||
///
|
||||
/// The lossy formats take `opts.rate_control`. AAC and ALAC are written as M4A, which records the
|
||||
/// encoder's priming and the exact length, so the file decodes to `buf`'s frames. MP3, AC-3 and
|
||||
/// E-AC-3 are raw streams, which have no place for that: they decode with the codec's delay in
|
||||
/// front and padded to whole frames.
|
||||
///
|
||||
/// ```
|
||||
/// # #[cfg(feature = "pcm-encode")] {
|
||||
/// use craft_audio::{AudioBuffer, BitDepth, EncodeFormat, EncodeOptions, encode};
|
||||
@@ -32,6 +40,7 @@ use std::io::{Seek, Write};
|
||||
pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result<Vec<u8>> {
|
||||
check_built(opts.format)?;
|
||||
// Checked for every format; WAV and FLAC record it.
|
||||
#[cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(unused_variables))]
|
||||
let mask = opts.layout.map(|l| wav_mask(&l, buf.channels.len())).transpose()?;
|
||||
match opts.format {
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
@@ -40,6 +49,8 @@ pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result<Vec<u8>> {
|
||||
EncodeFormat::Aiff => aiff::encode(buf, opts.bit_depth, opts.dither),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
EncodeFormat::Flac => flac::encode(buf, opts.bit_depth, opts.dither, mask),
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 => coded::encode(buf, opts),
|
||||
#[allow(unreachable_patterns)]
|
||||
f => Err(AudioError::Unsupported(format!("writing {f:?}"))),
|
||||
}
|
||||
@@ -47,31 +58,48 @@ pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result<Vec<u8>> {
|
||||
|
||||
/// Refuses a format whose writer is not built, before anything else about the request is checked.
|
||||
fn check_built(format: EncodeFormat) -> Result<()> {
|
||||
let (built, feature) =
|
||||
if format == EncodeFormat::Flac { (cfg!(feature = "flac-encode"), "flac-encode") } else { (cfg!(feature = "pcm-encode"), "pcm-encode") };
|
||||
if built { Ok(()) } else { Err(AudioError::Unsupported(format!("writing {format:?} needs the `{feature}` feature"))) }
|
||||
if format.is_built() { Ok(()) } else { Err(AudioError::Unsupported(format!("writing {format:?} needs the `{}` feature", format.feature()))) }
|
||||
}
|
||||
|
||||
/// The file extensions of the formats [`encode`] can write (those whose features are on).
|
||||
#[cfg(all(feature = "pcm-encode", feature = "flac-encode"))]
|
||||
pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "flac"];
|
||||
/// The file extensions of the formats [`encode`] can write (those whose features are on).
|
||||
#[cfg(all(feature = "pcm-encode", not(feature = "flac-encode")))]
|
||||
pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc"];
|
||||
/// The file extensions of the formats [`encode`] can write (those whose features are on).
|
||||
#[cfg(all(feature = "flac-encode", not(feature = "pcm-encode")))]
|
||||
pub const ENCODE_EXTENSIONS: &[&str] = &["flac"];
|
||||
/// Every extension [`ENCODE_EXTENSIONS`] can list, and how many of them the features built give.
|
||||
const fn extensions() -> ([&'static str; 12], usize) {
|
||||
let mut all = [""; 12];
|
||||
let mut n = 0;
|
||||
macro_rules! add {
|
||||
($built:expr, $($extension:literal),+) => {
|
||||
if $built {
|
||||
$(
|
||||
all[n] = $extension;
|
||||
n += 1;
|
||||
)+
|
||||
}
|
||||
};
|
||||
}
|
||||
add!(cfg!(feature = "pcm-encode"), "wav", "wave", "bwf", "aif", "aiff", "aifc");
|
||||
add!(cfg!(feature = "flac-encode"), "flac");
|
||||
add!(cfg!(any(feature = "aac-encode", feature = "alac-encode")), "m4a");
|
||||
add!(cfg!(feature = "mp3-encode"), "mp3");
|
||||
add!(cfg!(feature = "ac3-encode"), "ac3");
|
||||
add!(cfg!(feature = "eac3-encode"), "eac3", "ec3");
|
||||
(all, n)
|
||||
}
|
||||
|
||||
/// The format [`encode`] writes for a file extension or format name (`wav`, `aiff`, `flac`,
|
||||
/// case-insensitive, with or without the dot), or `None` when it cannot write it. `aifc` is
|
||||
/// AIFF: float AIFF is written as AIFF-C.
|
||||
const EXTENSIONS: ([&str; 12], usize) = extensions();
|
||||
|
||||
/// The file extensions of the formats [`encode`] can write (those whose features are on).
|
||||
pub const ENCODE_EXTENSIONS: &[&str] = EXTENSIONS.0.split_at(EXTENSIONS.1).0;
|
||||
|
||||
/// The format [`encode`] writes for a file extension or format name (`wav`, `aiff`, `flac`, `m4a`,
|
||||
/// `mp3`, `ac3`, `eac3`, case-insensitive, with or without the dot), or `None` when it cannot
|
||||
/// write it. `aifc` is AIFF: float AIFF is written as AIFF-C. `m4a` is AAC when the AAC encoder
|
||||
/// is built and ALAC otherwise; `alac` always names ALAC.
|
||||
///
|
||||
/// ```
|
||||
/// # #[cfg(feature = "pcm-encode")] {
|
||||
/// use craft_audio::{EncodeFormat, encode_format_for};
|
||||
///
|
||||
/// assert_eq!(encode_format_for(".AIFF"), Some(EncodeFormat::Aiff));
|
||||
/// assert_eq!(encode_format_for("mp3"), None);
|
||||
/// assert_eq!(encode_format_for("ogg"), None);
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn encode_format_for(name: &str) -> Option<EncodeFormat> {
|
||||
@@ -82,6 +110,18 @@ pub fn encode_format_for(name: &str) -> Option<EncodeFormat> {
|
||||
"aif" | "aiff" | "aifc" => Some(EncodeFormat::Aiff),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
"flac" => Some(EncodeFormat::Flac),
|
||||
#[cfg(feature = "aac-encode")]
|
||||
"m4a" => Some(EncodeFormat::Aac),
|
||||
#[cfg(all(feature = "alac-encode", not(feature = "aac-encode")))]
|
||||
"m4a" => Some(EncodeFormat::Alac),
|
||||
#[cfg(feature = "alac-encode")]
|
||||
"alac" => Some(EncodeFormat::Alac),
|
||||
#[cfg(feature = "mp3-encode")]
|
||||
"mp3" => Some(EncodeFormat::Mp3),
|
||||
#[cfg(feature = "ac3-encode")]
|
||||
"ac3" => Some(EncodeFormat::Ac3),
|
||||
#[cfg(feature = "eac3-encode")]
|
||||
"eac3" | "ec3" => Some(EncodeFormat::Eac3),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -102,11 +142,11 @@ fn failed_write_error() -> AudioError {
|
||||
AudioError::Encode("the stream encoder already failed on an earlier write; its output is incomplete".into())
|
||||
}
|
||||
|
||||
/// Writes WAV/BWF (RF64 above 4 GiB), AIFF / AIFF-C `fl32` or FLAC block by block, producing
|
||||
/// the same bytes [`encode`] would for the whole buffer: a file of any length never
|
||||
/// has to fit in memory. The file starts at the sink's position, and nothing may follow that
|
||||
/// position (a new file, or one truncated there): the encoder cannot truncate a `Write + Seek`,
|
||||
/// so the bytes of a longer file already there would stay after the new one.
|
||||
/// Writes a file of any [`EncodeFormat`] block by block, producing the same bytes [`encode`]
|
||||
/// would for the whole buffer: a file of any length never has to fit in memory. The file starts
|
||||
/// at the sink's position, and nothing may follow that position (a new file, or one truncated
|
||||
/// there): the encoder cannot truncate a `Write + Seek`, so the bytes of a longer file already
|
||||
/// there would stay after the new one.
|
||||
///
|
||||
/// ```
|
||||
/// # #[cfg(feature = "flac-encode")] {
|
||||
@@ -125,30 +165,27 @@ fn failed_write_error() -> AudioError {
|
||||
/// ```
|
||||
pub struct FileEncoder<W: Write + Seek> {
|
||||
writer: Writer<W>,
|
||||
format: EncodeFormat,
|
||||
channels: usize,
|
||||
}
|
||||
|
||||
impl<W: Write + Seek> std::fmt::Debug for FileEncoder<W> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let format = match &self.writer {
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
Writer::Wav(_) => EncodeFormat::Wav,
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
Writer::Aiff(_) => EncodeFormat::Aiff,
|
||||
#[cfg(feature = "flac-encode")]
|
||||
Writer::Flac(_) => EncodeFormat::Flac,
|
||||
};
|
||||
f.debug_struct("FileEncoder").field("format", &format).finish_non_exhaustive()
|
||||
f.debug_struct("FileEncoder").field("format", &self.format).finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
enum Writer<W> {
|
||||
// The writers differ in size with the features built; the coded one is boxed for its encoder.
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum Writer<W: Write + Seek> {
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
Wav(wav::Writer<W>),
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
Aiff(aiff::Writer<W>),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
Flac(flac::Writer<W>),
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Coded(Box<coded::Writer<W>>),
|
||||
}
|
||||
|
||||
impl<W: Write + Seek> FileEncoder<W> {
|
||||
@@ -156,7 +193,10 @@ impl<W: Write + Seek> FileEncoder<W> {
|
||||
/// `opts.layout`, when set, must have `channels` channels.
|
||||
pub fn new(out: W, opts: &EncodeOptions, sample_rate: u32, channels: u16) -> Result<Self> {
|
||||
check_built(opts.format)?;
|
||||
let (depth, dither, channels) = (opts.bit_depth, opts.dither, usize::from(channels));
|
||||
let channels = usize::from(channels);
|
||||
#[cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(unused_variables))]
|
||||
let (depth, dither) = (opts.bit_depth, opts.dither);
|
||||
#[cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(unused_variables))]
|
||||
let mask = opts.layout.map(|l| wav_mask(&l, channels)).transpose()?;
|
||||
let writer = match opts.format {
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
@@ -172,24 +212,45 @@ impl<W: Write + Seek> FileEncoder<W> {
|
||||
EncodeFormat::Aiff => Writer::Aiff(aiff::Writer::new(out, channels, sample_rate, depth, dither)?),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
EncodeFormat::Flac => Writer::Flac(flac::Writer::new(out, channels, sample_rate, depth, dither, mask)?),
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 => {
|
||||
Writer::Coded(Box::new(coded::Writer::new(out, opts, sample_rate, channels)?))
|
||||
}
|
||||
#[allow(unreachable_patterns)]
|
||||
f => return Err(AudioError::Unsupported(format!("writing {f:?}"))),
|
||||
};
|
||||
Ok(FileEncoder { writer, channels })
|
||||
Ok(FileEncoder { writer, format: opts.format, channels })
|
||||
}
|
||||
|
||||
/// The constant bit rate the file is written at, in bits per second for all channels
|
||||
/// together: the rate asked for or the format's default, moved to one the stream
|
||||
/// can carry as [`RateControl::Bitrate`](crate::RateControl::Bitrate) describes. `None` for the lossless
|
||||
/// formats and for AAC at a constant quality.
|
||||
pub fn bitrate(&self) -> Option<u32> {
|
||||
match &self.writer {
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Writer::Coded(w) => w.bitrate(),
|
||||
#[allow(unreachable_patterns)]
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Fails, without writing anything, when `frames` more frames would not fit the format (an
|
||||
/// AIFF holds 4 GiB, a FLAC 2^36 - 1 frames).
|
||||
/// AIFF holds 4 GiB, a FLAC 2^36 - 1 frames). The coded formats have no limit a file reaches.
|
||||
pub fn check_room(&self, frames: u64) -> Result<()> {
|
||||
match &self.writer {
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
Writer::Wav(w) => w.check_room(frames),
|
||||
Writer::Wav(w) => w.check_room(frames).map(drop),
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
Writer::Aiff(w) => w.check_room(frames),
|
||||
Writer::Aiff(w) => w.check_room(frames).map(drop),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
Writer::Flac(w) => w.check_room(frames),
|
||||
Writer::Flac(w) => w.check_room(frames).map(drop),
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Writer::Coded(_) => {
|
||||
let _ = frames;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
.map(drop)
|
||||
}
|
||||
|
||||
/// Appends the first `frames` frames of `block`, one slice per channel (`&[Vec<f32>]`,
|
||||
@@ -212,10 +273,13 @@ impl<W: Write + Seek> FileEncoder<W> {
|
||||
Writer::Aiff(w) => w.write(block, frames),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
Writer::Flac(w) => w.write(block, frames),
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Writer::Coded(w) => w.write(block, frames),
|
||||
}
|
||||
}
|
||||
|
||||
/// Completes the headers and hands back `out`, positioned at the end of the file.
|
||||
/// Encodes what is still buffered, completes the headers and hands back `out`, positioned at
|
||||
/// the end of the file.
|
||||
pub fn finish(self) -> Result<W> {
|
||||
match self.writer {
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
@@ -224,10 +288,41 @@ impl<W: Write + Seek> FileEncoder<W> {
|
||||
Writer::Aiff(w) => w.finish(),
|
||||
#[cfg(feature = "flac-encode")]
|
||||
Writer::Flac(w) => w.finish(),
|
||||
#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))]
|
||||
Writer::Coded(w) => w.finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodeOptions {
|
||||
/// Whether a file of `channels` channels at `sample_rate` can be written with these options:
|
||||
/// `Ok`, or the error [`FileEncoder::new`] and [`encode`] would return, with nothing written.
|
||||
/// For choosing a format before a file is created; [`EncodeFormat::sample_rates`],
|
||||
/// [`EncodeFormat::max_channels`] and [`EncodeFormat::supports_layout`] say what a format takes.
|
||||
///
|
||||
/// ```
|
||||
/// # #[cfg(feature = "mp3-encode")] {
|
||||
/// use craft_audio::{EncodeFormat, EncodeOptions};
|
||||
///
|
||||
/// let mp3 = EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(200_000);
|
||||
/// assert!(mp3.check(48_000, 2).is_ok());
|
||||
/// assert!(mp3.check(96_000, 2).is_err()); // not an MP3 sample rate
|
||||
/// assert_eq!(mp3.bitrate(48_000, 2).unwrap(), Some(192_000)); // the table rate below 200 kbit/s
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn check(&self, sample_rate: u32, channels: u16) -> Result<()> {
|
||||
self.bitrate(sample_rate, channels).map(drop)
|
||||
}
|
||||
|
||||
/// The constant bit rate such a file would be written at ([`FileEncoder::bitrate`]), or the
|
||||
/// error [`EncodeOptions::check`] returns.
|
||||
pub fn bitrate(&self, sample_rate: u32, channels: u16) -> Result<Option<u32>> {
|
||||
// The encoder a file would get, on a sink that is thrown away: what it accepts and the
|
||||
// rate it settles on are then the writer's by construction.
|
||||
Ok(FileEncoder::new(std::io::Cursor::new(Vec::new()), self, sample_rate, channels)?.bitrate())
|
||||
}
|
||||
}
|
||||
|
||||
/// The header of a 32-bit float WAV holding `frames` frames (little-endian samples follow it). Its
|
||||
/// length doesn't depend on `frames`, so a recorder can rewrite it in place as the file grows; it
|
||||
/// switches to RF64 above 4 GiB.
|
||||
@@ -252,7 +347,7 @@ pub fn wav_float_header(sample_rate: u32, channels: u16, frames: u64) -> Result<
|
||||
|
||||
// With only one of `pcm-encode` and `flac-encode`, the other's formats are refused, not silently
|
||||
// written as something else.
|
||||
#[cfg(all(test, not(all(feature = "pcm-encode", feature = "flac-encode"))))]
|
||||
#[cfg(all(test, any(feature = "pcm-encode", feature = "flac-encode"), not(all(feature = "pcm-encode", feature = "flac-encode"))))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -265,6 +360,7 @@ mod tests {
|
||||
let enc = FileEncoder::new(std::io::Cursor::new(Vec::new()), &opts, 48_000, 1);
|
||||
assert!(matches!(enc, Err(AudioError::Unsupported(_))));
|
||||
assert_eq!(encode_format_for(name), None);
|
||||
assert!(!format.is_built() && EncodeFormat::ALL.iter().any(|f| f.is_built()));
|
||||
// Also with a layout it could not use.
|
||||
let opts = opts.with_layout(ChannelLayout::STEREO);
|
||||
assert!(matches!(encode(&buf, &opts), Err(AudioError::Unsupported(_))));
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
//! PCM helpers shared by the WAV, AIFF and FLAC writers: quantization (with optional TPDF
|
||||
//! dither), interleaving and layout checks.
|
||||
|
||||
// The coded formats use the layout check, and of those only ALAC the quantizer.
|
||||
#![cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(dead_code))]
|
||||
|
||||
use crate::{AudioBuffer, AudioError, Result};
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
use {crate::BitDepth, std::io::Write};
|
||||
|
||||
@@ -62,11 +62,21 @@ fn every_decode_extension_probes_and_decodes() {
|
||||
fn every_encode_format_has_an_extension_it_is_written_and_read_under() {
|
||||
for &format in EncodeFormat::ALL {
|
||||
let ext = format.extension();
|
||||
assert_eq!(craft_audio::encode_format_for(ext), Some(format), "{ext}");
|
||||
assert!(craft_audio::ENCODE_EXTENSIONS.contains(&ext), "{ext}");
|
||||
// The extension names the format, or for `m4a`, which two formats share, AAC before ALAC.
|
||||
// A format whose feature is off has no name.
|
||||
match craft_audio::encode_format_for(ext) {
|
||||
Some(named) => {
|
||||
let shares_m4a = |f| matches!(f, EncodeFormat::Aac | EncodeFormat::Alac);
|
||||
assert!(named == format || (shares_m4a(named) && shares_m4a(format)), "{ext}");
|
||||
assert!(named.is_built(), "{ext}");
|
||||
assert!(craft_audio::ENCODE_EXTENSIONS.contains(&ext), "{ext}");
|
||||
}
|
||||
None => assert!(!matches!(format, EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac), "{ext}"),
|
||||
}
|
||||
assert!(DECODE_EXTENSIONS.contains(&ext), "{ext}: what is written reads back");
|
||||
assert!(format.mime_type().starts_with("audio/"), "{format:?}");
|
||||
}
|
||||
assert_eq!(EncodeFormat::ALL.iter().map(|f| f.extension()).collect::<Vec<_>>(), ["wav", "aif", "flac"]);
|
||||
let extensions: Vec<_> = EncodeFormat::ALL.iter().map(|f| f.extension()).collect();
|
||||
assert_eq!(extensions, ["wav", "aif", "flac", "m4a", "m4a", "mp3", "ac3", "eac3"]);
|
||||
assert_eq!(EncodeFormat::Aiff.mime_type(), "audio/aiff");
|
||||
}
|
||||
@@ -14,7 +14,7 @@ fn only_formats_the_encoder_writes_are_named() {
|
||||
for name in craft_audio::ENCODE_EXTENSIONS {
|
||||
assert!(encode_format_for(name).is_some(), "{name}");
|
||||
}
|
||||
for other in ["mp3", "ogg", "m4a", "xyz", ""] {
|
||||
for other in ["ogg", "opus", "aac", "mp4", "xyz", ""] {
|
||||
assert_eq!(encode_format_for(other), None, "{other}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,571 @@
|
||||
//! The coded formats of `encode` and `FileEncoder` (AAC and ALAC in M4A; MP3, AC-3 and E-AC-3
|
||||
//! streams), read back with this crate's decoder and, where ffmpeg is installed, with ffmpeg.
|
||||
|
||||
use std::io::{Cursor, Seek, SeekFrom, Write};
|
||||
use std::process::Command;
|
||||
|
||||
use craft_audio::{
|
||||
AudioBuffer, AudioError, BitDepth, ChannelLayout, CodecId, Container, ENCODE_EXTENSIONS, EncodeFormat, EncodeOptions, FileEncoder, RateControl,
|
||||
decode, encode, encode_format_for,
|
||||
};
|
||||
|
||||
const CODED: [EncodeFormat; 5] = [EncodeFormat::Aac, EncodeFormat::Alac, EncodeFormat::Mp3, EncodeFormat::Ac3, EncodeFormat::Eac3];
|
||||
|
||||
/// `channels` channels of `frames` frames, each channel a tone of its own frequency, on the
|
||||
/// 16-bit grid (so a lossless 16-bit format returns it exactly).
|
||||
fn tones(sample_rate: u32, channels: usize, frames: usize) -> AudioBuffer {
|
||||
let channel = |c: usize| {
|
||||
let step = (60.0 + 17.0 * c as f32) * std::f32::consts::TAU / sample_rate as f32;
|
||||
(0..frames).map(|i| ((i as f32 * step).sin() * 0.4 * 32768.0).round() / 32768.0).collect()
|
||||
};
|
||||
AudioBuffer { sample_rate, channels: (0..channels).map(channel).collect() }
|
||||
}
|
||||
|
||||
/// Signal-to-noise ratio in dB of `got` against `want`, over the frames both have.
|
||||
fn snr(want: &[f32], got: &[f32]) -> f64 {
|
||||
let (signal, noise) =
|
||||
want.iter().zip(got).fold((0f64, 0f64), |(s, n), (&w, &g)| (s + f64::from(w).powi(2), n + (f64::from(w) - f64::from(g)).powi(2)));
|
||||
10.0 * (signal / noise.max(1e-30)).log10()
|
||||
}
|
||||
|
||||
fn is_unsupported<T: std::fmt::Debug>(r: Result<T, AudioError>) -> bool {
|
||||
matches!(r, Err(AudioError::Unsupported(_)))
|
||||
}
|
||||
|
||||
/// An AAC file decodes to the frames that were written, in place: the edit list skips the
|
||||
/// encoder's priming and cuts the padding of the last packet.
|
||||
#[test]
|
||||
fn aac_decodes_to_the_frames_written() {
|
||||
for frames in [1, 1000, 1024, 1025, 5000, 48_017] {
|
||||
let buf = tones(48_000, 2, frames);
|
||||
let file = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac)).unwrap();
|
||||
let (info, out) = decode(&file, Some("m4a")).unwrap();
|
||||
assert_eq!((info.container, info.codec, info.sample_rate, info.damaged), (Container::Mp4, CodecId::Aac, 48_000, 0));
|
||||
assert_eq!((info.frames, out.frames(), info.layout), (frames as u64, frames, ChannelLayout::STEREO), "{frames} frames");
|
||||
if frames >= 5000 {
|
||||
for (want, got) in buf.channels.iter().zip(&out.channels) {
|
||||
assert!(snr(want, got) > 30.0, "{frames} frames: {} dB", snr(want, got));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The M4A track's durations add up to the priming and the audio, and its one edit skips the
|
||||
/// priming and lasts as long as the audio, in a movie timescale that is the sample rate.
|
||||
#[test]
|
||||
fn aac_timing_is_in_the_sample_table_and_the_edit_list() {
|
||||
use craft_audio::craft_media::isobmff;
|
||||
let file = encode(&tones(44_100, 1, 3000), &EncodeOptions::new(EncodeFormat::Aac)).unwrap();
|
||||
let mp4 = isobmff::open(file.as_slice()).unwrap();
|
||||
let track = &mp4.tracks[0];
|
||||
assert_eq!((mp4.timescale, track.timescale), (44_100, 44_100));
|
||||
assert_eq!(track.samples.iter().map(|s| u64::from(s.duration)).sum::<u64>(), 1024 + 3000);
|
||||
assert_eq!(track.edits.len(), 1);
|
||||
assert_eq!((track.edits[0].media_time, track.edits[0].segment_duration), (1024, 3000));
|
||||
// Every packet is in a `roll` group of distance -1, which Apple's decoders need to apply the edit.
|
||||
let groups = &track.sample_groups;
|
||||
assert_eq!((groups.len(), groups[0].grouping_type), (1, isobmff::FourCc(*b"roll")));
|
||||
assert_eq!((groups[0].runs.as_slice(), &groups[0].descriptions), ([(track.samples.len() as u32, 1)].as_slice(), &vec![vec![0xFF, 0xFF]]));
|
||||
}
|
||||
|
||||
/// The sample entry is completed when the file is: AAC's average bit rate, and the largest packet
|
||||
/// and average bit rate in ALAC's cookie.
|
||||
#[test]
|
||||
fn the_sample_entry_describes_the_finished_stream() {
|
||||
use craft_audio::craft_media::isobmff::{self, CodecConfig};
|
||||
let buf = tones(48_000, 2, 48_000);
|
||||
let file = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(96_000)).unwrap();
|
||||
let mp4 = isobmff::open(file.as_slice()).unwrap();
|
||||
let CodecConfig::Aac(aac) = &mp4.tracks[0].entries[0].codec else { panic!("not AAC") };
|
||||
assert!((90_000..100_000).contains(&aac.avg_bitrate), "{} bit/s", aac.avg_bitrate);
|
||||
|
||||
let file = encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16)).unwrap();
|
||||
let mp4 = isobmff::open(file.as_slice()).unwrap();
|
||||
let CodecConfig::Alac { cookie } = &mp4.tracks[0].entries[0].codec else { panic!("not ALAC") };
|
||||
let config = craft_audio::alac::SpecificConfig::parse(cookie).unwrap();
|
||||
let largest = mp4.tracks[0].samples.iter().map(|s| s.size).max().unwrap();
|
||||
assert_eq!((config.max_frame_bytes, config.bit_depth, config.frame_length), (largest, 16, 4096));
|
||||
assert!(config.avg_bit_rate > 0);
|
||||
}
|
||||
|
||||
/// Dither reaches ALAC's quantizer: a level below one 16-bit step is silence without it.
|
||||
#[test]
|
||||
fn alac_dithers_when_asked() {
|
||||
let buf = AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.3 / 32768.0; 4096]] };
|
||||
let opts = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16);
|
||||
let (_, plain) = decode(&encode(&buf, &opts).unwrap(), Some("m4a")).unwrap();
|
||||
let (_, dithered) = decode(&encode(&buf, &opts.with_dither(true)).unwrap(), Some("m4a")).unwrap();
|
||||
assert!(plain.channels[0].iter().all(|&s| s == 0.0));
|
||||
assert!(dithered.channels[0].iter().any(|&s| s != 0.0));
|
||||
}
|
||||
|
||||
/// ALAC returns the quantized samples exactly, for every channel count, with each channel where
|
||||
/// its speaker is.
|
||||
#[test]
|
||||
fn alac_is_lossless_in_every_layout() {
|
||||
for channels in 1..=8u8 {
|
||||
let layout = ChannelLayout::from_mask(craft_audio::alac::channel_mask(channels));
|
||||
let buf = tones(44_100, usize::from(channels), 10_000);
|
||||
let opts = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16).with_layout(layout);
|
||||
let file = encode(&buf, &opts).unwrap();
|
||||
let (info, out) = decode(&file, Some("m4a")).unwrap();
|
||||
assert_eq!((info.container, info.codec, info.frames, info.layout), (Container::Mp4, CodecId::Alac, 10_000, layout), "{channels} channels");
|
||||
assert_eq!(out, buf, "{channels} channels");
|
||||
}
|
||||
// 24 bits, and a length that is no whole number of packets.
|
||||
let buf = tones(96_000, 2, 4096 * 2 + 1);
|
||||
let (_, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Alac)).unwrap(), Some("m4a")).unwrap();
|
||||
assert_eq!(out, buf);
|
||||
}
|
||||
|
||||
/// Channels go to the encoder by speaker. ALAC's 6.1 has the back centre last and AAC's has it
|
||||
/// before the surround pair; 5.1 with side surrounds is the format's one 5.1.
|
||||
#[test]
|
||||
fn channels_reach_their_speakers() {
|
||||
// WAV 6.1 with a back pair (L R C LFE Lb Rb Cb), the usual 7-channel layout, into ALAC: the same.
|
||||
let buf = tones(48_000, 7, 8000);
|
||||
let (info, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16)).unwrap(), Some("m4a")).unwrap();
|
||||
assert_eq!(info.layout, ChannelLayout::from_mask(0x13F));
|
||||
assert_eq!(out, buf);
|
||||
// 5.1 with side surrounds decodes from ALAC as 5.1 with back surrounds, channel for channel.
|
||||
let buf = tones(48_000, 6, 8000);
|
||||
let opts = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16).with_layout(ChannelLayout::SURROUND_5_1_SIDE);
|
||||
assert_eq!(decode(&encode(&buf, &opts).unwrap(), Some("m4a")).unwrap().1, buf);
|
||||
|
||||
// AAC: the decoded channel each input channel comes back as. 5.1 decodes in WAV order; the
|
||||
// 6.1 and 7.1 streams have no speaker assignment and decode in AAC order (C L R Ls Rs Cs LFE
|
||||
// and C Lc Rc L R Ls Rs LFE).
|
||||
let wide = ChannelLayout::from_mask(0xFF);
|
||||
let cases: [(usize, Option<ChannelLayout>, &[usize]); 3] =
|
||||
[(6, None, &[0, 1, 2, 3, 4, 5]), (7, None, &[2, 0, 1, 4, 5, 6, 3]), (8, Some(wide), &[2, 6, 7, 0, 1, 4, 5, 3])];
|
||||
for (channels, layout, decoded_from) in cases {
|
||||
let buf = tones(48_000, channels, 48_000);
|
||||
let mut opts = EncodeOptions::new(EncodeFormat::Aac);
|
||||
opts.layout = layout;
|
||||
let (info, out) = decode(&encode(&buf, &opts).unwrap(), Some("m4a")).unwrap();
|
||||
assert_eq!((usize::from(info.channels), out.frames()), (channels, 48_000));
|
||||
for (decoded, &input) in decoded_from.iter().enumerate() {
|
||||
let db = snr(&buf.channels[input], &out.channels[decoded]);
|
||||
assert!(db > 20.0, "{channels} channels: decoded {decoded} is not input {input} ({db} dB)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// MP3, AC-3 and E-AC-3 are raw streams: the audio comes back behind the codec's delay.
|
||||
#[test]
|
||||
fn raw_streams_decode_behind_the_codec_delay() {
|
||||
let buf = tones(48_000, 2, 48_000);
|
||||
for (format, codec, container, delay, floor) in [
|
||||
(EncodeFormat::Mp3, CodecId::Mp3, Container::MpegAudio, craft_audio::mp3::DELAY as usize, 25.0),
|
||||
(EncodeFormat::Ac3, CodecId::Ac3, Container::Ac3, 256, 15.0),
|
||||
(EncodeFormat::Eac3, CodecId::Eac3, Container::Ac3, 256, 15.0),
|
||||
] {
|
||||
let file = encode(&buf, &EncodeOptions::new(format)).unwrap();
|
||||
let (info, out) = decode(&file, Some(format.extension())).unwrap();
|
||||
assert_eq!((info.codec, info.container, info.sample_rate, info.layout, info.damaged), (codec, container, 48_000, ChannelLayout::STEREO, 0));
|
||||
assert_eq!((format.codec(), format.container()), (codec, container));
|
||||
assert!(out.frames() >= 48_000 + delay, "{format:?}: {} frames", out.frames());
|
||||
for (want, got) in buf.channels.iter().zip(&out.channels) {
|
||||
let db = snr(want, &got[delay..]);
|
||||
assert!(db > floor, "{format:?}: {db} dB");
|
||||
}
|
||||
}
|
||||
// 5.1 with back surrounds is AC-3's 3/2 mode with an LFE, channel for channel.
|
||||
let buf = tones(48_000, 6, 48_000);
|
||||
let (info, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Ac3)).unwrap(), Some("ac3")).unwrap();
|
||||
assert_eq!(info.layout, ChannelLayout::SURROUND_5_1_SIDE);
|
||||
for (c, (want, got)) in buf.channels.iter().zip(&out.channels).enumerate() {
|
||||
// The encoder low-pass filters the LFE, which shifts its phase: it is compared by level.
|
||||
if c == 3 {
|
||||
let rms = |x: &[f32]| (x.iter().map(|&s| f64::from(s).powi(2)).sum::<f64>() / x.len() as f64).sqrt();
|
||||
let level = rms(&got[4800..]) / rms(want);
|
||||
assert!((0.5..1.5).contains(&level), "LFE at {level} of its level");
|
||||
} else {
|
||||
assert!(snr(want, &got[256..]) > 12.0, "channel {c}: {} dB", snr(want, &got[256..]));
|
||||
}
|
||||
}
|
||||
// 7.1 is an E-AC-3 programme.
|
||||
let buf = tones(48_000, 8, 4800);
|
||||
assert!(is_unsupported(encode(&buf, &EncodeOptions::new(EncodeFormat::Ac3))));
|
||||
// (Read back by ffmpeg in `ffmpeg_reads_the_files`: this crate's file reader plays the 5.1
|
||||
// independent substream of a raw 7.1 stream.)
|
||||
assert!(!encode(&buf, &EncodeOptions::new(EncodeFormat::Eac3)).unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// Blocks of any sizes give the bytes `encode` gives.
|
||||
#[test]
|
||||
fn streaming_writes_the_bytes_of_encode() {
|
||||
let buf = tones(44_100, 2, 30_000);
|
||||
for format in CODED {
|
||||
let opts = EncodeOptions::new(format);
|
||||
let whole = encode(&buf, &opts).unwrap();
|
||||
let mut enc = FileEncoder::new(Cursor::new(Vec::new()), &opts, 44_100, 2).unwrap();
|
||||
assert!(format!("{enc:?}").contains(&format!("{format:?}")));
|
||||
let mut at = 0;
|
||||
for n in [1, 4095, 4097, 0, 9000, 12_807] {
|
||||
let block: Vec<&[f32]> = buf.channels.iter().map(|c| &c[at..]).collect();
|
||||
enc.check_room(n as u64).unwrap();
|
||||
enc.write(&block, n).unwrap();
|
||||
at += n;
|
||||
}
|
||||
assert_eq!(at, buf.frames());
|
||||
assert!(enc.finish().unwrap().into_inner() == whole, "{format:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A file starts at the sink's position.
|
||||
#[test]
|
||||
fn a_file_starts_at_the_sinks_position() {
|
||||
let buf = tones(48_000, 1, 6000);
|
||||
for format in CODED {
|
||||
let opts = EncodeOptions::new(format);
|
||||
let mut sink = Cursor::new(b"junk".to_vec());
|
||||
sink.seek(SeekFrom::End(0)).unwrap();
|
||||
let mut enc = FileEncoder::new(sink, &opts, 48_000, 1).unwrap();
|
||||
enc.write(&buf.channels, 6000).unwrap();
|
||||
let mut sink = enc.finish().unwrap();
|
||||
let end = sink.stream_position().unwrap();
|
||||
let bytes = sink.into_inner();
|
||||
assert_eq!(end, bytes.len() as u64, "{format:?}");
|
||||
assert!(bytes[4..] == encode(&buf, &opts).unwrap(), "{format:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A file with no audio is still a file of its format.
|
||||
#[test]
|
||||
fn no_audio_gives_an_empty_file() {
|
||||
let buf = AudioBuffer::new(48_000, 2, 0);
|
||||
for format in CODED {
|
||||
let file = encode(&buf, &EncodeOptions::new(format)).unwrap();
|
||||
if format.container() == Container::Mp4 {
|
||||
let mp4 = craft_audio::craft_media::isobmff::open(file.as_slice()).unwrap();
|
||||
assert_eq!((mp4.tracks.len(), mp4.tracks[0].samples.len(), mp4.tracks[0].edits.len()), (1, 0, 0), "{format:?}");
|
||||
} else {
|
||||
assert!(file.is_empty(), "{format:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The bit rate asked for is the one written, moved to the nearest the format has.
|
||||
#[test]
|
||||
fn the_bit_rate_is_followed() {
|
||||
let buf = tones(48_000, 2, 48_000);
|
||||
let size = |opts: &EncodeOptions| encode(&buf, opts).unwrap().len();
|
||||
// MP3 at 48 kHz: 24 bytes per frame and kbit/s, 43 frames for a second and the delay.
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3)), 43 * 192 * 3);
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(130_000)), 43 * 128 * 3);
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(1)), 43 * 32 * 3);
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(u32::MAX)), 43 * 320 * 3);
|
||||
// AC-3: 32 packets of 1536 frames, 4 bytes per packet and kbit/s.
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Ac3)), 32 * 192 * 4);
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Ac3).with_bitrate(100_000)), 32 * 96 * 4);
|
||||
assert_eq!(size(&EncodeOptions::new(EncodeFormat::Eac3).with_bitrate(100_000)), 32 * 100 * 4);
|
||||
// AAC: a second of audio at about the rate asked for, around the 128 kbit/s default.
|
||||
let (low, default, high) = (
|
||||
size(&EncodeOptions::new(EncodeFormat::Aac).with_bitrate(64_000)),
|
||||
size(&EncodeOptions::new(EncodeFormat::Aac)),
|
||||
size(&EncodeOptions::new(EncodeFormat::Aac).with_bitrate(256_000)),
|
||||
);
|
||||
assert!((7000..10_000).contains(&low) && (15_000..18_500).contains(&default) && (30_000..35_000).contains(&high), "{low} {default} {high}");
|
||||
// A rate no stream can carry is moved into range, not refused.
|
||||
encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(u32::MAX)).unwrap();
|
||||
// The lowest rate still codes the audio: the encoder accepts rates that leave only silence
|
||||
// (128 for 128 kbit/s is one), and the writer does not go that low.
|
||||
for (sample_rate, channels) in [(8000, 1), (16_000, 2), (44_100, 2), (48_000, 6), (96_000, 2)] {
|
||||
let buf = tones(sample_rate, channels, sample_rate as usize);
|
||||
let (_, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(128)).unwrap(), Some("m4a")).unwrap();
|
||||
let rms = |x: &[f32]| (x.iter().map(|&s| f64::from(s).powi(2)).sum::<f64>() / x.len() as f64).sqrt();
|
||||
let level = rms(&out.channels[0]) / rms(&buf.channels[0]);
|
||||
assert!(level > 0.5, "{sample_rate} Hz, {channels} channels: {level} of the level");
|
||||
}
|
||||
// Quality is AAC's; ALAC has no rate to control.
|
||||
let (small, large) =
|
||||
(size(&EncodeOptions::new(EncodeFormat::Aac).with_quality(0.0)), size(&EncodeOptions::new(EncodeFormat::Aac).with_quality(1.0)));
|
||||
assert!(small < large, "{small} {large}");
|
||||
assert!(matches!(encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_quality(f32::NAN)), Err(AudioError::Encode(_))));
|
||||
for format in [EncodeFormat::Mp3, EncodeFormat::Ac3, EncodeFormat::Eac3] {
|
||||
assert!(is_unsupported(encode(&buf, &EncodeOptions::new(format).with_quality(0.5))), "{format:?}");
|
||||
}
|
||||
let alac = EncodeOptions::new(EncodeFormat::Alac);
|
||||
assert_eq!(size(&alac), size(&alac.clone().with_bitrate(64_000)));
|
||||
assert_eq!(alac.rate_control, None);
|
||||
assert_eq!(alac.with_quality(0.5).rate_control, Some(RateControl::Quality(0.5)));
|
||||
}
|
||||
|
||||
/// What a format cannot hold is refused before anything is written.
|
||||
#[test]
|
||||
fn what_a_format_cannot_hold_is_refused() {
|
||||
let refused = |format, sample_rate, channels: u16, layout: Option<ChannelLayout>| {
|
||||
let mut opts = EncodeOptions::new(format);
|
||||
opts.layout = layout;
|
||||
let mut sink = Cursor::new(Vec::new());
|
||||
let r = FileEncoder::new(&mut sink, &opts, sample_rate, channels).map(drop);
|
||||
assert!(r.is_ok() || sink.into_inner().is_empty(), "{format:?}: bytes written for a refused file");
|
||||
assert_eq!(r.is_err(), encode(&AudioBuffer::new(sample_rate, channels, 100), &opts).is_err());
|
||||
r
|
||||
};
|
||||
// Quad has no centre, which AAC's and ALAC's four channels have.
|
||||
assert!(is_unsupported(refused(EncodeFormat::Aac, 48_000, 4, None)));
|
||||
assert!(is_unsupported(refused(EncodeFormat::Alac, 48_000, 4, None)));
|
||||
refused(EncodeFormat::Aac, 48_000, 4, Some(ChannelLayout::from_mask(0x107))).unwrap();
|
||||
// WAV 7.1 (backs and sides) is not the 7.1 of AAC and ALAC (a front pair left and right of centre).
|
||||
assert!(is_unsupported(refused(EncodeFormat::Aac, 48_000, 8, None)));
|
||||
assert!(is_unsupported(refused(EncodeFormat::Alac, 48_000, 8, Some(ChannelLayout::SURROUND_7_1))));
|
||||
assert!(is_unsupported(refused(EncodeFormat::Aac, 48_000, 2, Some(ChannelLayout::unordered(2)))));
|
||||
for format in CODED {
|
||||
assert!(is_unsupported(refused(format, 48_000, 9, None)), "{format:?}");
|
||||
assert!(matches!(refused(format, 48_000, 2, Some(ChannelLayout::MONO)), Err(AudioError::Encode(_))), "{format:?}");
|
||||
assert!(matches!(refused(format, 0, 2, None), Err(AudioError::Encode(_))), "{format:?}");
|
||||
assert!(matches!(refused(format, 48_000, 0, None), Err(AudioError::Encode(_))), "{format:?}");
|
||||
}
|
||||
assert!(is_unsupported(refused(EncodeFormat::Mp3, 48_000, 3, None)));
|
||||
assert!(is_unsupported(refused(EncodeFormat::Ac3, 48_000, 7, None)));
|
||||
// Sample rates the codec has not.
|
||||
for format in [EncodeFormat::Aac, EncodeFormat::Mp3, EncodeFormat::Ac3, EncodeFormat::Eac3] {
|
||||
assert!(matches!(refused(format, 44_000, 2, None), Err(AudioError::Encode(_))), "{format:?}");
|
||||
}
|
||||
refused(EncodeFormat::Alac, 44_000, 2, None).unwrap();
|
||||
let float = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Float32);
|
||||
assert!(is_unsupported(encode(&AudioBuffer::new(48_000, 2, 100), &float)));
|
||||
}
|
||||
|
||||
/// Samples no encoder should see: NaN is silence, and values past full scale do not fail a
|
||||
/// write (AC-3, whose encoder refuses them, gets them clamped).
|
||||
#[test]
|
||||
fn non_finite_and_loud_samples_are_written() {
|
||||
let mut buf = tones(48_000, 2, 6000);
|
||||
buf.channels[0][100] = f32::NAN;
|
||||
buf.channels[0][200] = f32::INFINITY;
|
||||
buf.channels[1][300] = -7.5;
|
||||
for format in CODED {
|
||||
let file = encode(&buf, &EncodeOptions::new(format)).unwrap();
|
||||
let (_, out) = decode(&file, Some(format.extension())).unwrap();
|
||||
assert!(out.channels.iter().flatten().all(|s| s.is_finite()), "{format:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A sink that fails once `room` bytes are written.
|
||||
struct Full {
|
||||
inner: Cursor<Vec<u8>>,
|
||||
room: usize,
|
||||
}
|
||||
|
||||
impl Write for Full {
|
||||
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
|
||||
if self.inner.get_ref().len() + data.len() > self.room {
|
||||
return Err(std::io::Error::other("disk full"));
|
||||
}
|
||||
self.inner.write(data)
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Seek for Full {
|
||||
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
|
||||
self.inner.seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
/// After a failed write the encoder is unusable: later writes and `finish` fail too.
|
||||
#[test]
|
||||
fn a_failed_write_ends_the_encoder() {
|
||||
let buf = tones(48_000, 2, 48_000);
|
||||
for format in CODED {
|
||||
let mut enc = FileEncoder::new(Full { inner: Cursor::new(Vec::new()), room: 2000 }, &EncodeOptions::new(format), 48_000, 2).unwrap();
|
||||
assert!(matches!(enc.write(&buf.channels, 48_000), Err(AudioError::Io(_))), "{format:?}");
|
||||
assert!(matches!(enc.write(&buf.channels, 1), Err(AudioError::Encode(_))), "{format:?}");
|
||||
assert!(matches!(enc.finish(), Err(AudioError::Encode(_))), "{format:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_formats_are_named() {
|
||||
assert_eq!(encode_format_for("m4a"), Some(EncodeFormat::Aac));
|
||||
assert_eq!(encode_format_for(".ALAC"), Some(EncodeFormat::Alac));
|
||||
assert_eq!(encode_format_for("mp3"), Some(EncodeFormat::Mp3));
|
||||
assert_eq!(encode_format_for("ac3"), Some(EncodeFormat::Ac3));
|
||||
assert_eq!(encode_format_for("eac3"), Some(EncodeFormat::Eac3));
|
||||
assert_eq!(encode_format_for("ec3"), Some(EncodeFormat::Eac3));
|
||||
for &format in EncodeFormat::ALL {
|
||||
assert!(ENCODE_EXTENSIONS.contains(&format.extension()), "{format:?}");
|
||||
let named = encode_format_for(format.extension()).unwrap();
|
||||
assert_eq!((named.extension(), named.mime_type()), (format.extension(), format.mime_type()));
|
||||
assert_eq!(format.is_lossy(), !matches!(format.codec(), CodecId::Pcm | CodecId::Flac | CodecId::Alac));
|
||||
}
|
||||
for name in ENCODE_EXTENSIONS {
|
||||
assert!(encode_format_for(name).is_some(), "{name}");
|
||||
}
|
||||
assert_eq!(ENCODE_EXTENSIONS.len(), 12);
|
||||
for format in CODED {
|
||||
assert!(EncodeFormat::ALL.contains(&format) && format.is_built());
|
||||
}
|
||||
let features: Vec<_> = EncodeFormat::ALL.iter().map(|f| f.feature()).collect();
|
||||
assert_eq!(features, ["pcm-encode", "pcm-encode", "flac-encode", "alac-encode", "aac-encode", "mp3-encode", "ac3-encode", "eac3-encode"]);
|
||||
}
|
||||
|
||||
/// ffmpeg's decode of `file` as interleaved `f32`, split into `channels` channels.
|
||||
fn ffmpeg_decode(ff: &std::path::Path, file: &[u8], name: &str, channels: usize) -> Vec<Vec<f32>> {
|
||||
let path = crate::testkit::fixtures_dir("file_formats").join(name);
|
||||
std::fs::write(&path, file).unwrap();
|
||||
let out = Command::new(ff).args(["-v", "error", "-i"]).arg(&path).args(["-f", "f32le", "-"]).output().unwrap();
|
||||
assert!(out.status.success() && out.stderr.is_empty(), "{name}: {}", String::from_utf8_lossy(&out.stderr));
|
||||
let mut planar = vec![Vec::new(); channels];
|
||||
for (i, s) in out.stdout.as_chunks::<4>().0.iter().enumerate() {
|
||||
planar[i % channels].push(f32::from_le_bytes(*s));
|
||||
}
|
||||
planar
|
||||
}
|
||||
|
||||
/// ffmpeg reads every format: the M4A files without their priming (it applies the edit list) and,
|
||||
/// from ffmpeg 9, at the length written; ALAC exactly; the raw streams behind their delay.
|
||||
#[test]
|
||||
fn ffmpeg_reads_the_files() {
|
||||
let ff = require_ffmpeg!();
|
||||
for (channels, layout) in [(1, None), (2, None), (6, None), (8, Some(ChannelLayout::from_mask(0xFF)))] {
|
||||
let buf = tones(48_000, channels, 20_000);
|
||||
let mut opts = EncodeOptions::new(EncodeFormat::Aac);
|
||||
opts.layout = layout;
|
||||
let out = ffmpeg_decode(&ff, &encode(&buf, &opts).unwrap(), &format!("aac_{channels}.m4a"), channels);
|
||||
// The priming is skipped by every ffmpeg (the channels below line up). The padding of the
|
||||
// last packet is cut by ffmpeg 9; ffmpeg 8 plays it, to the end of the 20th packet.
|
||||
assert!([20_000, 20_480].contains(&out[0].len()), "AAC, {channels} channels: {} frames", out[0].len());
|
||||
if channels <= 6 {
|
||||
// ffmpeg also returns 5.1 in WAV order.
|
||||
for (c, (want, got)) in buf.channels.iter().zip(&out).enumerate() {
|
||||
assert!(snr(want, got) > 20.0, "AAC, {channels} channels, channel {c}: {} dB", snr(want, got));
|
||||
}
|
||||
}
|
||||
let opts = opts.with_format(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16);
|
||||
let opts = if channels == 6 { opts.with_layout(ChannelLayout::SURROUND_5_1) } else { opts };
|
||||
let out = ffmpeg_decode(&ff, &encode(&buf, &opts).unwrap(), &format!("alac_{channels}.m4a"), channels);
|
||||
assert!(out == buf.channels, "ALAC, {channels} channels");
|
||||
}
|
||||
let buf = tones(44_100, 2, 20_000);
|
||||
for (format, delay, floor) in
|
||||
[(EncodeFormat::Mp3, craft_audio::mp3::DELAY as usize, 25.0), (EncodeFormat::Ac3, 256, 15.0), (EncodeFormat::Eac3, 256, 15.0)]
|
||||
{
|
||||
let name = format!("stream.{}", format.extension());
|
||||
let out = ffmpeg_decode(&ff, &encode(&buf, &EncodeOptions::new(format)).unwrap(), &name, 2);
|
||||
for (want, got) in buf.channels.iter().zip(&out) {
|
||||
assert!(
|
||||
got.len() >= 20_000 + delay && snr(want, &got[delay..]) > floor,
|
||||
"{format:?}: {} frames, {} dB",
|
||||
got.len(),
|
||||
snr(want, &got[delay..])
|
||||
);
|
||||
}
|
||||
}
|
||||
// E-AC-3 7.1: the front channels, which the 5.1 downmix leaves as they are.
|
||||
let buf = tones(48_000, 8, 20_000);
|
||||
let out = ffmpeg_decode(&ff, &encode(&buf, &EncodeOptions::new(EncodeFormat::Eac3)).unwrap(), "stream_7_1.eac3", 8);
|
||||
assert!(out[7].len() >= 20_256);
|
||||
for c in 0..3 {
|
||||
assert!(snr(&buf.channels[c], &out[c][256..]) > 12.0, "E-AC-3 7.1, channel {c}: {} dB", snr(&buf.channels[c], &out[c][256..]));
|
||||
}
|
||||
}
|
||||
|
||||
/// Apple's decoder (macOS `afinfo`; skipped elsewhere) takes the priming and the length of an AAC
|
||||
/// file from the edit list, which it does only for a track with a `roll` sample group.
|
||||
#[test]
|
||||
fn apple_reads_the_aac_priming_and_length() {
|
||||
let Some(afinfo) = crate::testkit::oracle::find_tool("afinfo") else { return };
|
||||
for (frames, remainder) in [(20_000, 480), (1, 1023), (4096, 0)] {
|
||||
let path = crate::testkit::fixtures_dir("file_formats").join(format!("apple_{frames}.m4a"));
|
||||
std::fs::write(&path, encode(&tones(48_000, 2, frames), &EncodeOptions::new(EncodeFormat::Aac)).unwrap()).unwrap();
|
||||
let out = Command::new(&afinfo).arg(&path).output().unwrap();
|
||||
let text = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(text.contains(&format!("{frames} valid frames + 1024 priming + {remainder} remainder")), "{frames} frames: {text}");
|
||||
}
|
||||
}
|
||||
|
||||
/// What `EncodeFormat` says a format takes is what the writer accepts: for every format, a file
|
||||
/// can be begun exactly when the sample rate, the channel count and the layout are listed.
|
||||
#[test]
|
||||
fn the_format_queries_agree_with_the_writer() {
|
||||
// Every rate a format lists, each with its neighbours, and some that none lists.
|
||||
let mut rates: Vec<u32> = EncodeFormat::ALL.iter().filter_map(|f| f.sample_rates()).flatten().flat_map(|&r| [r - 1, r, r + 1]).collect();
|
||||
rates.extend([44_000, 176_400, 192_000]);
|
||||
rates.sort_unstable();
|
||||
rates.dedup();
|
||||
let masks = [
|
||||
0x4, 0x1, 0x3, 0xC, 0x600, 0x7, 0xB, 0x103, 0x33, 0x107, 0x603, 0xF, 0x10B, 0x37, 0x607, 0x10F, 0x60B, 0x3F, 0x60F, 0x13F, 0x70F, 0x63F, 0xFF,
|
||||
];
|
||||
let layouts = masks.iter().map(|&mask| ChannelLayout::from_mask(mask)).chain((1..=9).map(ChannelLayout::unordered));
|
||||
for layout in layouts {
|
||||
for &format in EncodeFormat::ALL {
|
||||
// 24 bits is a depth every format has.
|
||||
let opts = EncodeOptions::new(format).with_layout(layout);
|
||||
for &sample_rate in &rates {
|
||||
let listed = format.sample_rates().is_none_or(|r| r.contains(&sample_rate))
|
||||
&& layout.channels() <= format.max_channels()
|
||||
&& format.supports_layout(&layout);
|
||||
let checked = opts.check(sample_rate, layout.channels());
|
||||
assert_eq!(checked.is_ok(), listed, "{format:?}, {sample_rate} Hz, {layout:?}: {checked:?}");
|
||||
let begun = FileEncoder::new(Cursor::new(Vec::new()), &opts, sample_rate, layout.channels());
|
||||
assert_eq!(begun.is_ok(), listed, "{format:?}, {sample_rate} Hz, {layout:?}");
|
||||
}
|
||||
if let Some(fixed) = format.fixed_layouts(layout.channels()) {
|
||||
assert!(fixed.iter().all(|l| format.supports_layout(l) && l.channels() == layout.channels()));
|
||||
assert_eq!(format.supports_layout(&layout), fixed.iter().any(|l| same_speakers(l, &layout)), "{format:?}, {layout:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
for &format in EncodeFormat::ALL {
|
||||
assert!(!format.supports_layout(&ChannelLayout::unordered(0)));
|
||||
assert_eq!(format.supports_quality(), EncodeOptions::new(format).with_quality(0.5).check(48_000, 2).is_ok() && format.is_lossy());
|
||||
assert_eq!(format.fixed_layouts(2).is_none(), matches!(format, EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac));
|
||||
}
|
||||
// The most channels is the most: one more is refused, by the layouts of a format that has
|
||||
// a fixed set and by the writer of one that has not.
|
||||
let most: Vec<u16> = EncodeFormat::ALL.iter().map(|f| f.max_channels()).collect();
|
||||
assert_eq!(most, [32_767, 32_767, 8, 8, 8, 2, 6, 8]);
|
||||
for &format in EncodeFormat::ALL {
|
||||
let most = format.max_channels();
|
||||
match format.fixed_layouts(most) {
|
||||
Some(layouts) => assert!(!layouts.is_empty() && format.fixed_layouts(most + 1).unwrap().is_empty(), "{format:?}"),
|
||||
None => {
|
||||
let opts = EncodeOptions::new(format).with_bit_depth(BitDepth::Int16);
|
||||
assert!(opts.check(48_000, most).is_ok() && opts.check(48_000, most + 1).is_err(), "{format:?}");
|
||||
assert!(format.supports_layout(&ChannelLayout::unordered(most)) && !format.supports_layout(&ChannelLayout::unordered(most + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(EncodeFormat::Ac3.fixed_layouts(3).unwrap().len(), 3);
|
||||
assert!(EncodeFormat::Aac.fixed_layouts(9).unwrap().is_empty());
|
||||
}
|
||||
|
||||
/// Whether two layouts name the same speakers, a surround pair being either sides or backs.
|
||||
fn same_speakers(a: &ChannelLayout, b: &ChannelLayout) -> bool {
|
||||
let one_pair = |mask: u32| if mask & 0x630 == 0x30 { mask & !0x30 | 0x600 } else { mask };
|
||||
matches!((a.mask(), b.mask()), (Some(a), Some(b)) if one_pair(a) == one_pair(b))
|
||||
}
|
||||
|
||||
/// `bitrate` is the rate the file is written at, for the options and for the encoder they give.
|
||||
#[test]
|
||||
fn the_bit_rate_used_can_be_asked_for() {
|
||||
let rate = |opts: EncodeOptions, sample_rate, channels| {
|
||||
let asked = opts.bitrate(sample_rate, channels).unwrap();
|
||||
assert_eq!(FileEncoder::new(Cursor::new(Vec::new()), &opts, sample_rate, channels).unwrap().bitrate(), asked);
|
||||
asked
|
||||
};
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Mp3), 48_000, 2), Some(192_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(130_000), 48_000, 2), Some(128_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(u32::MAX), 22_050, 1), Some(160_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Ac3).with_bitrate(191_999), 48_000, 2), Some(160_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Ac3), 48_000, 6), Some(448_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Eac3).with_bitrate(100_999), 48_000, 2), Some(100_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac), 48_000, 2), Some(128_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac).with_bitrate(128), 48_000, 2), Some(24_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac).with_bitrate(u32::MAX), 8000, 1), Some(48_000));
|
||||
assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac).with_quality(0.5), 48_000, 2), None);
|
||||
for format in [EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac, EncodeFormat::Alac] {
|
||||
assert_eq!(rate(EncodeOptions::new(format).with_bitrate(96_000), 48_000, 2), None, "{format:?}");
|
||||
}
|
||||
// The size of a second of MP3 is the rate reported.
|
||||
let buf = tones(48_000, 2, 48_000);
|
||||
let opts = EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(130_000);
|
||||
assert_eq!(encode(&buf, &opts).unwrap().len() as u32, 43 * 1152 / 8 * opts.bitrate(48_000, 2).unwrap().unwrap() / 48_000);
|
||||
assert!(is_unsupported(EncodeOptions::new(EncodeFormat::Mp3).bitrate(48_000, 6)));
|
||||
}
|
||||
@@ -12,6 +12,7 @@ mod ac3_encoder_oracle;
|
||||
mod ac3_oracle;
|
||||
mod alac;
|
||||
mod concealment;
|
||||
mod file_formats;
|
||||
mod mp3_decoder_oracle;
|
||||
mod mp3_encoder_oracle;
|
||||
mod mpa_oracle;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::{Read, Seek};
|
||||
use std::io::{Cursor, Read, Seek, Write};
|
||||
|
||||
pub use craft_document_bundle::Asset;
|
||||
use craft_document_bundle::Spec;
|
||||
@@ -14,9 +14,10 @@ use serde::{Serialize, de::DeserializeOwned};
|
||||
use serde_json::Value;
|
||||
|
||||
pub const MIME: &str = "application/vnd.designcraft+zip";
|
||||
pub const VERSION: u32 = 4;
|
||||
pub const VERSION: u32 = 5;
|
||||
pub const EXTENSION: &str = "designcraft";
|
||||
pub const PREVIEW: &str = "preview.png";
|
||||
pub const PROXIES_DIRECTORY: &str = "proxies";
|
||||
pub const MAX_PREVIEW_BYTES: u64 = 4 << 20;
|
||||
|
||||
const BUNDLE: Spec<'static> = Spec {
|
||||
@@ -26,6 +27,7 @@ const BUNDLE: Spec<'static> = Spec {
|
||||
document_entry: "document.json",
|
||||
preview_entry: PREVIEW,
|
||||
assets_directory: "assets",
|
||||
proxies_directory: PROXIES_DIRECTORY,
|
||||
format_name: "designcraft",
|
||||
legacy_assets_field: Some("assetData"),
|
||||
};
|
||||
@@ -57,23 +59,53 @@ impl From<craft_document_bundle::Error> for Error {
|
||||
pub struct Loaded<T> {
|
||||
pub document: T,
|
||||
pub assets: Vec<(String, Vec<u8>)>,
|
||||
/// Proxy previews (JPEG or PNG bytes) of linked assets, keyed by asset id.
|
||||
pub proxies: Vec<(String, Vec<u8>)>,
|
||||
}
|
||||
|
||||
/// The reader capability required by `document`, inferred from its neutral JSON schema and
|
||||
/// embedded assets. Traversal beyond 64 levels conservatively requires version 4.
|
||||
/// embedded assets. Traversal beyond 64 levels conservatively requires version 4. A linked
|
||||
/// asset (`/assets/<id>/storage == "linked"`) requires version 5.
|
||||
pub fn required_version(document: &Value, assets: &[Asset<'_>]) -> u32 {
|
||||
if has_linked_asset(document) {
|
||||
return 5;
|
||||
}
|
||||
if has_version_four_feature(document) {
|
||||
return 4;
|
||||
}
|
||||
if assets.iter().any(|asset| craft_eps::is_eps(asset.data)) { 3 } else { 2 }
|
||||
}
|
||||
|
||||
/// Serialize a document and its external assets as a `.designcraft` ZIP bundle.
|
||||
pub fn save<T: Serialize>(document: &T, generator: &str, preview: Option<&[u8]>, assets: &[Asset<'_>]) -> Result<Vec<u8>, Error> {
|
||||
/// Serialize a document, its embedded assets and the proxy previews of its linked assets as a
|
||||
/// `.designcraft` ZIP bundle. Linked assets have a record in the document and no `assets` entry.
|
||||
pub fn save<T: Serialize>(
|
||||
document: &T,
|
||||
generator: &str,
|
||||
preview: Option<&[u8]>,
|
||||
assets: &[Asset<'_>],
|
||||
proxies: &[Asset<'_>],
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let mut out = Cursor::new(Vec::new());
|
||||
write_to(&mut out, document, generator, preview, assets, proxies)?;
|
||||
Ok(out.into_inner())
|
||||
}
|
||||
|
||||
/// Like [`save`], streaming the ZIP into `out`. A writer that returns an error (to cancel a
|
||||
/// long save, say) stops the write and the error is returned as [`Error::Io`].
|
||||
pub fn write_to<T: Serialize, W: Write + Seek>(
|
||||
out: W,
|
||||
document: &T,
|
||||
generator: &str,
|
||||
preview: Option<&[u8]>,
|
||||
assets: &[Asset<'_>],
|
||||
proxies: &[Asset<'_>],
|
||||
) -> Result<(), Error> {
|
||||
let value = serde_json::to_value(document).map_err(|error| Error::Io(error.to_string()))?;
|
||||
let embedded: Vec<Asset<'_>> = assets.iter().filter(|asset| !is_linked(&value, asset.id)).copied().collect();
|
||||
let assets = embedded.as_slice();
|
||||
let version = required_version(&value, assets);
|
||||
let json = serde_json::to_vec_pretty(&value).map_err(|error| Error::Io(error.to_string()))?;
|
||||
craft_document_bundle::write(&json, version, generator, preview, assets, BUNDLE).map_err(Into::into)
|
||||
craft_document_bundle::write_to(out, &json, version, generator, preview, assets, proxies, BUNDLE).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Read a `.designcraft` ZIP or legacy single-file JSON, applying all versioned schema migrations
|
||||
@@ -82,7 +114,7 @@ pub fn load<T: DeserializeOwned>(bytes: &[u8]) -> Result<Loaded<T>, Error> {
|
||||
let mut package = craft_document_bundle::read(bytes, VERSION, BUNDLE)?;
|
||||
upgrade_legacy_json(&mut package.document, package.version);
|
||||
let document = serde_json::from_value(package.document).map_err(|error| Error::NotOurs(error.to_string()))?;
|
||||
Ok(Loaded { document, assets: package.assets })
|
||||
Ok(Loaded { document, assets: package.assets, proxies: package.proxies })
|
||||
}
|
||||
|
||||
/// Whether `bytes` are a native DesignCraft ZIP package.
|
||||
@@ -100,6 +132,17 @@ pub fn read_preview<R: Read + Seek>(reader: R) -> Option<Vec<u8>> {
|
||||
craft_document_bundle::read_preview_from(reader, PREVIEW)
|
||||
}
|
||||
|
||||
fn is_linked(document: &Value, id: &str) -> bool {
|
||||
document.get("assets").and_then(|assets| assets.get(id)).and_then(|asset| asset.get("storage")).and_then(Value::as_str) == Some("linked")
|
||||
}
|
||||
|
||||
fn has_linked_asset(document: &Value) -> bool {
|
||||
document
|
||||
.get("assets")
|
||||
.and_then(Value::as_object)
|
||||
.is_some_and(|assets| assets.values().any(|asset| asset.get("storage").and_then(Value::as_str) == Some("linked")))
|
||||
}
|
||||
|
||||
fn has_version_four_feature(document: &Value) -> bool {
|
||||
let mut stack = vec![(document, 0usize)];
|
||||
let mut visited = 0usize;
|
||||
|
||||
@@ -7,7 +7,7 @@ fn native_bundle_roundtrips_document_assets_and_preview() -> Result<(), Box<dyn
|
||||
let image = [1u8, 2, 3, 4];
|
||||
let assets = [Asset { id: "7", mime: "image/png", data: &image }];
|
||||
let preview = b"preview png";
|
||||
let bytes = save(&document, "DesignCraft test", Some(preview), &assets)?;
|
||||
let bytes = save(&document, "DesignCraft test", Some(preview), &assets, &[])?;
|
||||
|
||||
assert!(is_designcraft(&bytes));
|
||||
assert!(starts_designcraft(&bytes[..bytes.len().min(80)]));
|
||||
@@ -21,7 +21,7 @@ fn native_bundle_roundtrips_document_assets_and_preview() -> Result<(), Box<dyn
|
||||
#[test]
|
||||
fn preview_reader_rejects_an_entry_over_its_declared_read_budget() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let oversized = vec![0u8; usize::try_from(MAX_PREVIEW_BYTES + 1)?];
|
||||
let bytes = save(&json!({}), "DesignCraft test", Some(&oversized), &[])?;
|
||||
let bytes = save(&json!({}), "DesignCraft test", Some(&oversized), &[], &[])?;
|
||||
assert_eq!(read_preview(std::io::Cursor::new(bytes)), None);
|
||||
Ok(())
|
||||
}
|
||||
@@ -65,8 +65,105 @@ fn legacy_json_extracts_assets_and_applies_v1_migrations() -> Result<(), Box<dyn
|
||||
|
||||
#[test]
|
||||
fn newer_metadata_precedes_malformed_document_json() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let bytes = craft_document_bundle::write(b"{", VERSION + 1, "future", None, &[], BUNDLE)?;
|
||||
let bytes = craft_document_bundle::write(b"{", VERSION + 1, "future", None, &[], &[], BUNDLE)?;
|
||||
assert!(matches!(load::<Value>(&bytes), Err(Error::TooNew(version)) if version == u64::from(VERSION + 1)));
|
||||
assert!(matches!(load::<Value>(b"not a package"), Err(Error::NotOurs(_))));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn linked_doc() -> Value {
|
||||
json!({"assets":{"7":{"storage":"linked","mime":"image/png"},"8":{"mime":"image/png"}}})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedded_documents_keep_their_version() {
|
||||
assert_eq!(required_version(&json!({"assets":{"7":{"storage":"embedded"},"8":{}}}), &[]), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_linked_asset_requires_version_five_and_old_readers_refuse_it() -> Result<(), Box<dyn std::error::Error>> {
|
||||
assert_eq!(required_version(&linked_doc(), &[]), 5);
|
||||
let bytes = save(&linked_doc(), "t", None, &[], &[])?;
|
||||
assert_eq!(craft_document_bundle::read(&bytes, 4, BUNDLE), Err(craft_document_bundle::Error::TooNew(5)));
|
||||
let loaded = load::<Value>(&bytes)?;
|
||||
assert_eq!(loaded.document, linked_doc());
|
||||
assert!(loaded.assets.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proxies_round_trip_and_unknown_or_oversized_ones_are_skipped() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let big = vec![0u8; usize::try_from(craft_document_bundle::PROXY_LIMIT)? + 1];
|
||||
let proxies = [
|
||||
Asset { id: "7", mime: "image/jpeg", data: b"jpeg" },
|
||||
Asset { id: "8", mime: "image/png", data: &big },
|
||||
Asset { id: "99", mime: "image/png", data: b"unknown" },
|
||||
];
|
||||
let bytes = save(&linked_doc(), "t", None, &[], &proxies)?;
|
||||
assert_eq!(load::<Value>(&bytes)?.proxies, [("7".to_owned(), b"jpeg".to_vec())]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proxy_paths_must_be_exactly_proxies_id_ext() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let proxies = [
|
||||
Asset { id: "../x", mime: "image/png", data: b"a" },
|
||||
Asset { id: "a/b", mime: "image/png", data: b"b" },
|
||||
Asset { id: "7/8", mime: "image/png", data: b"c" },
|
||||
];
|
||||
let bytes = save(&linked_doc(), "t", None, &[], &proxies)?;
|
||||
assert!(load::<Value>(&bytes)?.proxies.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_to_stops_when_the_writer_fails() {
|
||||
struct Failing {
|
||||
inner: Cursor<Vec<u8>>,
|
||||
budget: usize,
|
||||
failed: bool,
|
||||
calls_after_failure: std::rc::Rc<std::cell::Cell<u32>>,
|
||||
}
|
||||
impl Write for Failing {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
if self.failed {
|
||||
self.calls_after_failure.set(self.calls_after_failure.get() + 1);
|
||||
}
|
||||
if self.budget < buf.len() {
|
||||
self.failed = true;
|
||||
return Err(std::io::Error::other("cancelled"));
|
||||
}
|
||||
self.budget -= buf.len();
|
||||
self.inner.write(buf)
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
if self.failed {
|
||||
self.calls_after_failure.set(self.calls_after_failure.get() + 1);
|
||||
}
|
||||
self.inner.flush()
|
||||
}
|
||||
}
|
||||
impl Seek for Failing {
|
||||
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
|
||||
if self.failed {
|
||||
self.calls_after_failure.set(self.calls_after_failure.get() + 1);
|
||||
}
|
||||
self.inner.seek(pos)
|
||||
}
|
||||
}
|
||||
let data = vec![1u8; 4096];
|
||||
let assets = [Asset { id: "8", mime: "image/png", data: &data }];
|
||||
let calls = std::rc::Rc::new(std::cell::Cell::new(0));
|
||||
let out = Failing { inner: Cursor::new(Vec::new()), budget: 1024, failed: false, calls_after_failure: calls.clone() };
|
||||
let result = write_to(out, &linked_doc(), "t", None, &assets, &[]);
|
||||
assert_eq!(calls.get(), 0, "no write, flush or seek reaches the writer after it failed");
|
||||
assert!(matches!(result, Err(Error::Io(message)) if message.contains("cancelled")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_supplied_for_a_linked_asset_are_not_written() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let assets = [Asset { id: "7", mime: "image/png", data: b"linked bytes" }, Asset { id: "8", mime: "image/png", data: b"kept" }];
|
||||
let bytes = save(&linked_doc(), "t", None, &assets, &[])?;
|
||||
assert_eq!(load::<Value>(&bytes)?.assets, [("8".to_owned(), b"kept".to_vec())]);
|
||||
Ok(())
|
||||
}
|
||||
@@ -138,7 +138,9 @@ impl Fib {
|
||||
}
|
||||
let w_ident = u16le(0);
|
||||
if w_ident == 0xA5DC {
|
||||
return Err(DocbinError::NotWord("Word 6.0/95 format, only Word 97-2003 is supported".into()));
|
||||
// `read` sends Word 6.0/95 streams to `word6` before this FIB is parsed. This keeps any
|
||||
// other caller from reading one with the Word 97 layout, which it doesn't have.
|
||||
return Err(DocbinError::NotWord("a Word 6.0/95 file doesn't have the Word 97 FIB layout".into()));
|
||||
}
|
||||
if w_ident != W_IDENT {
|
||||
return Err(DocbinError::NotWord(format!("magic 0x{w_ident:04X}, expected 0x{W_IDENT:04X}")));
|
||||
|
||||
@@ -29,6 +29,19 @@ pub(crate) struct Bins {
|
||||
sorted: bool,
|
||||
/// Page number (× 512 = byte offset in the WordDocument stream).
|
||||
pns: Vec<u32>,
|
||||
/// Runs read ahead of time instead of pages (Word 6.0/95, whose FKPs and sprms are another
|
||||
/// layout, translated by [`crate::word6`]): sorted by `lo`, not overlapping.
|
||||
translated: Option<Vec<TranslatedRun>>,
|
||||
}
|
||||
|
||||
/// One run of a [`Bins::translated`] table: the FCs `[lo, hi)` it covers, the paragraph style
|
||||
/// (for PAPX runs) and a Word 97 grpprl.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct TranslatedRun {
|
||||
pub(crate) lo: u32,
|
||||
pub(crate) hi: u32,
|
||||
pub(crate) istd: u16,
|
||||
pub(crate) grpprl: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Bins {
|
||||
@@ -37,7 +50,7 @@ impl Bins {
|
||||
let fc = fc as usize;
|
||||
let lcb = lcb as usize;
|
||||
if lcb < 4 || !(lcb - 4).is_multiple_of(8) {
|
||||
return Ok(Bins { fcs: Vec::new(), sorted: true, pns: Vec::new() });
|
||||
return Ok(Bins { fcs: Vec::new(), sorted: true, pns: Vec::new(), translated: None });
|
||||
}
|
||||
let end = fc.checked_add(lcb).ok_or_else(|| DocbinError::Malformed("bin table overflow".into()))?;
|
||||
let plc = table.get(fc..end).ok_or_else(|| DocbinError::Malformed("bin table outside the Table stream".into()))?;
|
||||
@@ -56,7 +69,31 @@ impl Bins {
|
||||
pns.push(at(n + 1 + i).ok_or_else(|| DocbinError::Malformed("truncated bin aPn".into()))? & 0x003F_FFFF);
|
||||
}
|
||||
let sorted = fcs.windows(2).all(|w| w.first() <= w.get(1));
|
||||
Ok(Bins { fcs, sorted, pns })
|
||||
Ok(Bins { fcs, sorted, pns, translated: None })
|
||||
}
|
||||
|
||||
/// A table of runs read ahead of time ([`TranslatedRun`]). Runs are sorted by start; one that
|
||||
/// overlaps an earlier one (a corrupt file) or covers nothing is dropped.
|
||||
pub(crate) fn translated(mut runs: Vec<TranslatedRun>) -> Bins {
|
||||
runs.sort_by_key(|r| r.lo);
|
||||
let mut kept: Vec<TranslatedRun> = Vec::with_capacity(runs.len());
|
||||
for r in runs {
|
||||
if r.lo < r.hi && kept.last().is_none_or(|last| r.lo >= last.hi) {
|
||||
kept.push(r);
|
||||
}
|
||||
}
|
||||
Bins { fcs: Vec::new(), sorted: true, pns: Vec::new(), translated: Some(kept) }
|
||||
}
|
||||
|
||||
/// For a translated table: the run covering `fc`, or the gap `[lo, hi)` between runs that
|
||||
/// holds it.
|
||||
fn translated_at(runs: &[TranslatedRun], fc: u32) -> Result<&TranslatedRun, (u32, u32)> {
|
||||
let i = runs.partition_point(|r| r.lo <= fc);
|
||||
let prev = i.checked_sub(1).and_then(|i| runs.get(i));
|
||||
match prev {
|
||||
Some(r) if fc < r.hi => Ok(r),
|
||||
_ => Err((prev.map_or(0, |r| r.hi), runs.get(i).map_or(u32::MAX, |r| r.lo))),
|
||||
}
|
||||
}
|
||||
|
||||
/// The page containing `fc`, as a slice of the WordDocument stream, and the FC range
|
||||
@@ -77,13 +114,19 @@ impl Bins {
|
||||
|
||||
/// The grpprl of the Chpx covering `fc` ([MS-DOC] ChpxFkp: rgfc u32s, rgb offsets ×2,
|
||||
/// crun in the last byte).
|
||||
pub(crate) fn chpx<'w>(&self, word: &'w [u8], fc: u32) -> &'w [u8] {
|
||||
pub(crate) fn chpx<'w>(&'w self, word: &'w [u8], fc: u32) -> &'w [u8] {
|
||||
self.chpx_run(word, fc).grpprl
|
||||
}
|
||||
|
||||
/// [`Bins::chpx`] with the FC range over which the same grpprl is the answer, so a walk
|
||||
/// over consecutive characters looks it up once per run, not once per character.
|
||||
pub(crate) fn chpx_run<'w>(&self, word: &'w [u8], fc: u32) -> ChpxRun<'w> {
|
||||
pub(crate) fn chpx_run<'w>(&'w self, word: &'w [u8], fc: u32) -> ChpxRun<'w> {
|
||||
if let Some(runs) = &self.translated {
|
||||
return match Self::translated_at(runs, fc) {
|
||||
Ok(r) => ChpxRun { grpprl: &r.grpprl, lo: r.lo, hi: r.hi },
|
||||
Err((lo, hi)) => ChpxRun { grpprl: &[], lo, hi },
|
||||
};
|
||||
}
|
||||
let none = ChpxRun { grpprl: &[], lo: 1, hi: 0 };
|
||||
let Some((page, page_lo, page_hi)) = self.page(word, fc) else { return none };
|
||||
let Some(&crun) = page.get(511) else { return none };
|
||||
@@ -113,7 +156,13 @@ impl Bins {
|
||||
/// ([MS-DOC] PapxFkp: rgfc u32s — each the FC *following* a paragraph mark —, rgbx BX
|
||||
/// entries (1 offset byte + 12 PHE bytes), crun in the last byte; the PAPX itself is a
|
||||
/// PapxInFkp with the cb/cb' size quirk). Query with the FC of the character after the mark.
|
||||
pub(crate) fn papx<'w>(&self, word: &'w [u8], fc: u32) -> (u16, &'w [u8]) {
|
||||
pub(crate) fn papx<'w>(&'w self, word: &'w [u8], fc: u32) -> (u16, &'w [u8]) {
|
||||
if let Some(runs) = &self.translated {
|
||||
return match Self::translated_at(runs, fc) {
|
||||
Ok(r) => (r.istd, &r.grpprl),
|
||||
Err(_) => (0, &[]),
|
||||
};
|
||||
}
|
||||
let Some((page, _, _)) = self.page(word, fc) else { return (0, &[]) };
|
||||
let Some(&crun) = page.get(511) else { return (0, &[]) };
|
||||
// BX is 13 bytes; cap crun at what fits before byte 511.
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
//! like the other Craft parsers, is lenient by design (unknown structures are skipped, bad numbers
|
||||
//! fall back to defaults) and bounded (stream sizes, piece counts and property counts are
|
||||
//! capped), so hostile input yields an error or a best-effort document, never a panic.
|
||||
//!
|
||||
//! Word 6.0/95 files, which that specification does not cover, are read by `word6` from their
|
||||
//! observed layout: their text, formatting and tables so far.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
// The code came from an app whose lints allow this.
|
||||
#![allow(clippy::type_complexity)]
|
||||
@@ -29,6 +32,7 @@ mod sections;
|
||||
mod sprm;
|
||||
mod stsh;
|
||||
mod table;
|
||||
mod word6;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -220,6 +224,9 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) ->
|
||||
if word.len() as u64 > MAX_STREAM {
|
||||
return Err(DocbinError::Limit(format!("WordDocument stream is {} bytes", word.len())));
|
||||
}
|
||||
if word6::is_word6(&word) {
|
||||
return read_word6(&comp, &word, max_paragraphs);
|
||||
}
|
||||
let enc = fib::peek_encryption(&word);
|
||||
// The FIB names the Table stream (0Table or 1Table); fall back to whichever exists,
|
||||
// since files written by other writers sometimes carry the wrong bit.
|
||||
@@ -431,6 +438,55 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) ->
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
/// Read a Word 6.0/95 file ([`word6`]): its main text, with its character and paragraph
|
||||
/// formatting and table rows translated to Word 97 sprms, through the same walk as a Word 97 file.
|
||||
/// Its paragraph styles are read too; character styles and the other stories are not yet, and a
|
||||
/// section mark reads as a page break.
|
||||
fn read_word6(comp: &craft_cfb::CompoundFile<'_>, word: &[u8], max_paragraphs: usize) -> Result<Document, DocbinError> {
|
||||
let fib = word6::Fib6::parse(word)?;
|
||||
let content = word6::content(word, &fib)?;
|
||||
let mut doc = Document::new();
|
||||
// Word opens these files in compatibility mode 11 too.
|
||||
doc.settings.compat_mode = 11;
|
||||
if let Some(core) = metadata::read(comp) {
|
||||
doc.core = core;
|
||||
}
|
||||
doc.body.clear();
|
||||
// The paragraph styles, built as for Word 97 files from their translated formatting.
|
||||
doc.styles = Arc::new(fmt::stylesheet(&content.styles, &content.fonts, stsh::StandardFonts::default()));
|
||||
let sheet: StyleSheet = StyleSheet::clone(&doc.styles);
|
||||
// As for Word 97 files: the text read can't exceed twice the stream, nor the absolute ceiling.
|
||||
let budget = std::cell::Cell::new((word.len() as u64).saturating_mul(2).min(MAX_TOTAL_CHARS));
|
||||
let work = Work::new(MAX_WORK);
|
||||
let paragraphs = ParaBudget { left: std::cell::Cell::new(max_paragraphs), exceeded: std::cell::Cell::new(false) };
|
||||
let mut pics = media::Pictures::for_input(word.len());
|
||||
let ctx = WalkCtx {
|
||||
word,
|
||||
data: &[],
|
||||
pieces: &content.pieces,
|
||||
chpx_bins: &content.chpx,
|
||||
papx_bins: &content.papx,
|
||||
fonts: &content.fonts,
|
||||
raw_styles: &content.styles,
|
||||
sheet: &sheet,
|
||||
notes: &[],
|
||||
marks: &[],
|
||||
drawn: &[],
|
||||
sect_ends: &[],
|
||||
budget: &budget,
|
||||
paragraphs: ¶graphs,
|
||||
work: &work,
|
||||
};
|
||||
let paras = walk(&ctx, 0, fib.ccp_text, &mut pics);
|
||||
if paragraphs.exceeded.get() {
|
||||
return Err(paragraph_limit(max_paragraphs));
|
||||
}
|
||||
doc.body = table::assemble(paras);
|
||||
doc.ensure_nonempty();
|
||||
work.log();
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
fn paragraph_limit(max_paragraphs: usize) -> DocbinError {
|
||||
DocbinError::Limit(format!("the document has more than {max_paragraphs} paragraphs and table cells"))
|
||||
}
|
||||
@@ -648,6 +704,20 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic
|
||||
// querying at the mark's own FC finds that paragraph's properties.
|
||||
let fc_mark = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
let (istd, papx) = ctx.papx_bins.papx(ctx.word, fc_mark);
|
||||
// The Prm of the piece holding the mark adds its paragraph sprms after the PAPX's
|
||||
// ([MS-DOC] §2.4.6.1, step 5), as Word 6/95's fast saves record a paragraph change.
|
||||
let extra = prm_paragraph_sprms(ctx.pieces.piece_prm(cp), ctx.work);
|
||||
let combined;
|
||||
let papx: &[u8] = if extra.is_empty() {
|
||||
papx
|
||||
} else {
|
||||
// Re-framed from its Prls, so that a pad byte at its end can't swallow the
|
||||
// first appended opcode.
|
||||
let mut v: Vec<u8> = sprm::iter(papx).flat_map(|p| p.op.to_le_bytes().into_iter().chain(p.operand.iter().copied())).collect();
|
||||
v.extend_from_slice(&extra);
|
||||
combined = v;
|
||||
&combined
|
||||
};
|
||||
if let Some(st) = ctx.raw_styles.get(istd as usize).filter(|s| !s.name.is_empty()) {
|
||||
pb.props.style = Some(fmt::style_id(&st.name));
|
||||
}
|
||||
@@ -991,13 +1061,42 @@ struct FmtState<'a> {
|
||||
/// paragraph style stay the same, so a Prm grpprl (up to 64 KB) is interpreted once per
|
||||
/// formatting change, not once per character (#3692).
|
||||
struct FmtMemo<'a> {
|
||||
/// The CHPX grpprl, compared by address: it is a slice of the WordDocument stream.
|
||||
/// The CHPX grpprl, compared by address: a slice of the WordDocument stream, or of a Word 6.0/95
|
||||
/// file's translated runs, both of which stay put for the walk.
|
||||
chpx: &'a [u8],
|
||||
prm: u16,
|
||||
style: Option<String>,
|
||||
props: CharProps,
|
||||
}
|
||||
|
||||
/// The paragraph and table sprms (sgc 1 and 5) of a piece's Prm, as Prls to append to a
|
||||
/// paragraph's PAPX: a Prm0's single sprm, or those of the Prm1's grpprl. A Prm1 grpprl is
|
||||
/// charged to `work`, as for [`apply_prm`].
|
||||
fn prm_paragraph_sprms(prm: PrmRef<'_>, work: &Work) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
match prm {
|
||||
PrmRef::Sprm0 { isprm, val } => {
|
||||
if let Some(op) = sprm::from_prm0(isprm)
|
||||
&& matches!(Prl { op, operand: &[] }.sgc(), 1 | 5)
|
||||
{
|
||||
out.extend_from_slice(&op.to_le_bytes());
|
||||
out.push(val);
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl(Some(g)) => {
|
||||
if !work.charge(g.len() as u64, Cost::Formatting) {
|
||||
return out;
|
||||
}
|
||||
for prl in sprm::iter(g).filter(|p| matches!(p.sgc(), 1 | 5)) {
|
||||
out.extend_from_slice(&prl.op.to_le_bytes());
|
||||
out.extend_from_slice(prl.operand);
|
||||
}
|
||||
}
|
||||
PrmRef::None | PrmRef::Grpprl(None) => {}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Apply a piece's Prm (Prm0 short sprm or Prm1 grpprl) to character properties. A Prm1
|
||||
/// grpprl is charged to `work`; once that is spent the grpprl is skipped.
|
||||
fn apply_prm(props: &mut CharProps, prm: PrmRef<'_>, fonts: &[String], base: &CharProps, work: &Work) {
|
||||
|
||||
+161
-17
@@ -1,20 +1,71 @@
|
||||
//! The piece table ([MS-DOC] `Clx`/`Pcdt`/`PlcPcd`): where every character position lives in
|
||||
//! the WordDocument stream, and how it is encoded.
|
||||
//!
|
||||
//! Word 6.0/95 files ([`crate::word6`]) keep the same `Clx` shape, but every piece's text is at a
|
||||
//! plain byte offset: 8-bit text in the code page of its piece's or run's font (or the document's),
|
||||
//! or UTF-16 in a file with the FIB's fExtChar flag.
|
||||
|
||||
use craft_codepage::cp1252;
|
||||
use craft_codepage::{code_page_byte, cp1252, is_byte_code_page};
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
/// Most pieces any real file has; more means a corrupt `PlcPcd`.
|
||||
const MAX_PIECES: usize = 1_000_000;
|
||||
|
||||
/// One piece: text at FcCompressed `fc` (30-bit offset + compression bit).
|
||||
/// How a piece's characters are stored.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum Text {
|
||||
/// UTF-16LE at `fc`.
|
||||
Utf16,
|
||||
/// Windows-1252, one byte per character at `fc / 2` (Word 97 FcCompressed).
|
||||
Compressed,
|
||||
/// One byte per character at `fc` itself, in the code page [`PieceTable::code_page_at`] gives
|
||||
/// that byte (Word 6.0/95).
|
||||
Bytes,
|
||||
}
|
||||
|
||||
/// One piece: its text at `fc` (for Word 97, the 30-bit offset of FcCompressed).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Pcd {
|
||||
fc: u32,
|
||||
compressed: bool,
|
||||
/// `Prm`: either a single sprm (Prm0) or an index into the Clx's Prc grpprls (Prm1).
|
||||
text: Text,
|
||||
/// `Prm`: either a single sprm (Prm0) or an index into the Clx's Prc grpprls (Prm1). In a Word
|
||||
/// 6.0/95 table the Prcs hold translated grpprls ([`PieceTable::parse_word6`]), and a Prm0's
|
||||
/// isprm is already the opcode [MS-DOC]'s isprm table maps.
|
||||
prm: u16,
|
||||
/// For a Word 6.0/95 [`Text::Bytes`] piece whose Prm names a font: that font's code page,
|
||||
/// which outranks the runs' ([`PieceTable::code_page_at`]).
|
||||
code_page: Option<u16>,
|
||||
}
|
||||
|
||||
impl Pcd {
|
||||
/// The byte offset of the character `step / 2` characters into the piece. `step` is twice
|
||||
/// the CP offset, so one formula serves all three encodings.
|
||||
fn fc_at(&self, step: u32) -> Option<u32> {
|
||||
match self.text {
|
||||
Text::Utf16 => self.fc.checked_add(step),
|
||||
Text::Compressed => self.fc.checked_add(step)?.checked_div(2),
|
||||
Text::Bytes => self.fc.checked_add(step / 2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The code page of the 8-bit text in the byte range `[lo, hi)` of the WordDocument stream.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) struct CodePageRun {
|
||||
pub(crate) lo: u32,
|
||||
pub(crate) hi: u32,
|
||||
pub(crate) code_page: u16,
|
||||
}
|
||||
|
||||
/// How a Word 6.0/95 file's text is stored and decoded ([`crate::word6`]).
|
||||
pub(crate) struct Word6Text {
|
||||
/// UTF-16 rather than 8-bit (the FIB's fExtChar flag).
|
||||
pub(crate) wide: bool,
|
||||
/// The code page of 8-bit text that no piece or run gives one for.
|
||||
pub(crate) default_code_page: u16,
|
||||
/// The code page of each byte range, from its run's font (sorted by `lo`, not overlapping).
|
||||
pub(crate) code_pages: Vec<CodePageRun>,
|
||||
}
|
||||
|
||||
/// The parsed piece table plus the grpprls of the Prcs that Prm1 references.
|
||||
@@ -23,11 +74,58 @@ pub(crate) struct PieceTable {
|
||||
cps: Vec<u32>,
|
||||
pieces: Vec<Pcd>,
|
||||
prc_grpprls: Vec<Vec<u8>>,
|
||||
/// For [`Text::Bytes`] pieces: the code page of each byte range, sorted by `lo`, not
|
||||
/// overlapping. Bytes outside every range are in `default_code_page`.
|
||||
code_pages: Vec<CodePageRun>,
|
||||
default_code_page: u16,
|
||||
}
|
||||
|
||||
impl PieceTable {
|
||||
/// Parse the `Clx` at `table[fc .. fc+lcb]`.
|
||||
pub(crate) fn parse(table: &[u8], fc: u32, lcb: u32) -> Result<PieceTable, DocbinError> {
|
||||
Self::parse_clx(table, fc, lcb, None)
|
||||
}
|
||||
|
||||
/// Parse a Word 6.0/95 `Clx` at `word[fc .. fc+lcb]`: the Word 97 shape, but every piece's text
|
||||
/// is at its plain byte offset. `piece_code_page` gives the code page a piece's raw Prm
|
||||
/// imposes (with the Clx's raw Prc grpprls to look Prm1 indices up in), if any; then each Prc
|
||||
/// grpprl is replaced by `translate_prc`'s Word 97 sprms, which the walk applies to the piece.
|
||||
pub(crate) fn parse_word6(
|
||||
word: &[u8],
|
||||
fc: u32,
|
||||
lcb: u32,
|
||||
text: Word6Text,
|
||||
piece_code_page: impl Fn(u16, &[Vec<u8>]) -> Option<u16>,
|
||||
translate_prc: impl Fn(&[u8]) -> Vec<u8>,
|
||||
) -> Result<PieceTable, DocbinError> {
|
||||
let mut t = Self::parse_clx(word, fc, lcb, Some(text.wide))?;
|
||||
if !text.wide {
|
||||
for p in &mut t.pieces {
|
||||
p.code_page = piece_code_page(p.prm, &t.prc_grpprls);
|
||||
}
|
||||
}
|
||||
t.prc_grpprls = t.prc_grpprls.iter().map(|g| translate_prc(g)).collect();
|
||||
t.code_pages = text.code_pages;
|
||||
t.default_code_page = text.default_code_page;
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
/// One Word 6.0/95 piece: `ccp` characters from byte `fc` on. This covers a file that was not
|
||||
/// fast-saved, which has no `Clx`.
|
||||
pub(crate) fn contiguous_word6(fc: u32, ccp: u32, text: Word6Text) -> PieceTable {
|
||||
let kind = if text.wide { Text::Utf16 } else { Text::Bytes };
|
||||
PieceTable {
|
||||
cps: vec![0, ccp],
|
||||
pieces: vec![Pcd { fc, text: kind, prm: 0, code_page: None }],
|
||||
prc_grpprls: Vec::new(),
|
||||
code_pages: text.code_pages,
|
||||
default_code_page: text.default_code_page,
|
||||
}
|
||||
}
|
||||
|
||||
/// `word6`: `None` for Word 97 and later, `Some(wide)` for a Word 6.0/95 file (whose raw Prms
|
||||
/// and Prcs are kept for [`PieceTable::parse_word6`]).
|
||||
fn parse_clx(table: &[u8], fc: u32, lcb: u32, word6: Option<bool>) -> Result<PieceTable, DocbinError> {
|
||||
let fc = fc as usize;
|
||||
let lcb = lcb as usize;
|
||||
if lcb == 0 {
|
||||
@@ -95,9 +193,26 @@ impl PieceTable {
|
||||
return Err(DocbinError::Malformed("truncated aPcd".into()));
|
||||
};
|
||||
let prm = plc.get(at + 4..at + 6).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0);
|
||||
pieces.push(Pcd { fc: fc_raw & 0x3FFF_FFFF, compressed: fc_raw & 0x4000_0000 != 0, prm });
|
||||
pieces.push(match word6 {
|
||||
// A plain byte offset, with no compression bit: 8-bit text, or UTF-16 in a file with
|
||||
// fExtChar. The raw Prm is kept for `parse_word6`.
|
||||
Some(wide) => Pcd { fc: fc_raw, text: if wide { Text::Utf16 } else { Text::Bytes }, prm, code_page: None },
|
||||
None => {
|
||||
let text = if fc_raw & 0x4000_0000 != 0 { Text::Compressed } else { Text::Utf16 };
|
||||
Pcd { fc: fc_raw & 0x3FFF_FFFF, text, prm, code_page: None }
|
||||
}
|
||||
});
|
||||
}
|
||||
Ok(PieceTable { cps, pieces, prc_grpprls, code_pages: Vec::new(), default_code_page: 1252 })
|
||||
}
|
||||
|
||||
/// The code page of the 8-bit text byte at `fc` (in [`Text::Bytes`] pieces).
|
||||
fn code_page_at(&self, fc: u32) -> u16 {
|
||||
let i = self.code_pages.partition_point(|r| r.lo <= fc);
|
||||
match i.checked_sub(1).and_then(|i| self.code_pages.get(i)) {
|
||||
Some(r) if fc < r.hi => r.code_page,
|
||||
_ => self.default_code_page,
|
||||
}
|
||||
Ok(PieceTable { cps, pieces, prc_grpprls })
|
||||
}
|
||||
|
||||
/// The Prls this piece's `Prm` adds. Bit 0 (`fComplex`) discriminates: a Prm0
|
||||
@@ -133,8 +248,7 @@ impl PieceTable {
|
||||
let p = self.piece_of(cp)?;
|
||||
let i = self.cps.partition_point(|&c| c <= cp).checked_sub(1)?;
|
||||
let cp0 = *self.cps.get(i)?;
|
||||
let step = cp.checked_sub(cp0)?.checked_mul(2)?;
|
||||
if p.compressed { p.fc.checked_add(step)?.checked_div(2) } else { p.fc.checked_add(step) }
|
||||
p.fc_at(cp.checked_sub(cp0)?.checked_mul(2)?)
|
||||
}
|
||||
|
||||
/// The piece holding `cp`, with the CP range it covers: for every CP in that range the
|
||||
@@ -167,13 +281,24 @@ impl PieceTable {
|
||||
let (Some(p), Some(&cp0)) = (self.pieces.get(i), self.cps.get(i)) else { return Err(u32::MAX) };
|
||||
let Some(off) = cp.checked_sub(cp0) else { return Err(next) };
|
||||
let off = off as usize;
|
||||
if p.compressed {
|
||||
// 8-bit, Windows-1252, one byte per character, stored at fc/2.
|
||||
let at = ((p.fc / 2) as usize).checked_add(off);
|
||||
return match at.and_then(|a| word.get(a)) {
|
||||
Some(&b) => Ok((cp1252(b), 1)),
|
||||
None => Err(next),
|
||||
};
|
||||
match p.text {
|
||||
Text::Compressed => {
|
||||
// 8-bit, Windows-1252, one byte per character, stored at fc/2.
|
||||
let at = ((p.fc / 2) as usize).checked_add(off);
|
||||
return match at.and_then(|a| word.get(a)) {
|
||||
Some(&b) => Ok((cp1252(b), 1)),
|
||||
None => Err(next),
|
||||
};
|
||||
}
|
||||
Text::Bytes => {
|
||||
// 8-bit, one byte per character at fc, in its run's code page.
|
||||
let at = (p.fc as usize).checked_add(off);
|
||||
return match at.and_then(|a| Some((u32::try_from(a).ok()?, *word.get(a)?))) {
|
||||
Some((a, b)) => Ok((byte_char(p.code_page.unwrap_or_else(|| self.code_page_at(a)), b), 1)),
|
||||
None => Err(next),
|
||||
};
|
||||
}
|
||||
Text::Utf16 => {}
|
||||
}
|
||||
// UTF-16LE, one 16-bit unit per character, stored at fc.
|
||||
let unit = |k: usize| -> Option<u16> {
|
||||
@@ -207,8 +332,7 @@ impl PieceSpan<'_> {
|
||||
|
||||
/// [`PieceTable::fc_of_cp`] for a CP inside the span.
|
||||
pub(crate) fn fc_at(&self, cp: u32) -> Option<u32> {
|
||||
let step = cp.checked_sub(self.cp_lo)?.checked_mul(2)?;
|
||||
if self.pcd.compressed { self.pcd.fc.checked_add(step)?.checked_div(2) } else { self.pcd.fc.checked_add(step) }
|
||||
self.pcd.fc_at(cp.checked_sub(self.cp_lo)?.checked_mul(2)?)
|
||||
}
|
||||
|
||||
/// The raw Prm of the piece: two CPs with the same value get the same [`PrmRef`].
|
||||
@@ -228,3 +352,23 @@ pub(crate) enum PrmRef<'a> {
|
||||
/// Prm1: a grpprl from a Prc (absent if out of range).
|
||||
Grpprl(Option<&'a [u8]>),
|
||||
}
|
||||
|
||||
/// Windows' code page number for symbol fonts (CP_SYMBOL).
|
||||
pub(crate) const SYMBOL_CODE_PAGE: u16 = 42;
|
||||
|
||||
/// One byte of 8-bit text in `code_page`. The East Asian code pages store a character as a lead
|
||||
/// and a trail byte, which one CP per byte can't represent, so their bytes above ASCII read as
|
||||
/// U+FFFD rather than as unrelated Windows-1252 letters.
|
||||
fn byte_char(code_page: u16, b: u8) -> char {
|
||||
if code_page == SYMBOL_CODE_PAGE {
|
||||
// Each character from the space up to its private-use code point, as Word reads a document
|
||||
// marked as Symbol; control characters (paragraph marks, fields) stay themselves.
|
||||
if b < 0x20 { char::from(b) } else { char::from_u32(0xF000 + u32::from(b)).unwrap_or('\u{FFFD}') }
|
||||
} else if is_byte_code_page(code_page) {
|
||||
code_page_byte(code_page, b)
|
||||
} else if b.is_ascii() {
|
||||
char::from(b)
|
||||
} else {
|
||||
'\u{FFFD}'
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use super::read;
|
||||
mod drawing;
|
||||
mod encrypted;
|
||||
mod metadata;
|
||||
mod word6;
|
||||
|
||||
/// Offset in the WordDocument stream where test text pieces are placed (past the FIB).
|
||||
const TEXT_FC: u32 = 0x600;
|
||||
@@ -176,12 +177,15 @@ fn wrong_magic_rejected() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_6_95_rejected() {
|
||||
let mut fib = fib_bytes(&FibSpec::default());
|
||||
fn word_6_95_magic_goes_to_the_word6_reader() {
|
||||
// A Word 6.0/95 signature is no longer refused: the stream is read as that version (here an
|
||||
// empty document, since a Word 97 FIB holds no Word 6 text length). The flags are cleared:
|
||||
// the Word 97 default sets fExtChar, which the Word 6 reader refuses.
|
||||
let mut fib = fib_bytes(&FibSpec { flags: 0, ..FibSpec::default() });
|
||||
fib[0] = 0xDC;
|
||||
fib[1] = 0xA5;
|
||||
let f = cfb_file(&[("WordDocument", &fib)]);
|
||||
assert!(matches!(read(&f), Err(super::DocbinError::NotWord(m)) if m.contains("6.0/95")));
|
||||
assert!(read(&f).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -882,8 +886,6 @@ fn corpus_files_open() {
|
||||
let bytes = std::fs::read(&p).expect("read");
|
||||
let doc = match super::read(&bytes) {
|
||||
Ok(d) => d,
|
||||
// Word 6/95 files are a documented, intentional rejection.
|
||||
Err(super::DocbinError::NotWord(m)) if m.contains("6.0/95") => continue,
|
||||
Err(e) => panic!("{}: {e}", p.display()),
|
||||
};
|
||||
assert!(!doc.styles.styles.is_empty(), "{}: no styles", p.display());
|
||||
@@ -2106,6 +2108,29 @@ fn one_section(text: &'static str, sepx: Vec<u8>, papx_entries: Vec<(u32, u16, V
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn piece_prm_paragraph_formatting_applies_to_the_marks_paragraph() {
|
||||
// [MS-DOC] §2.4.6.1 step 5: the Prm of the piece holding the paragraph mark adds its paragraph
|
||||
// sprms after the PAPX's. Prm1 -> a Prc with sprmPDxaLeft80 (1440 twips) and sprmPJc80
|
||||
// (centred); Prm0 isprm 0x05 (sprmPJc) with operand 2 (right).
|
||||
let prcs = vec![grpprl(&[(0x840F, &1440i16.to_le_bytes()), (0x2403, &[0x01])])];
|
||||
let para = |prm: u16| {
|
||||
let spec = RichSpec {
|
||||
text: "Text\r",
|
||||
styles: vec![(1, 0x0FFF, "Normal", Vec::new(), Vec::new())],
|
||||
fonts: vec!["Times New Roman"],
|
||||
chpx_runs: vec![(0, Vec::new())],
|
||||
papx_entries: vec![(4, 0, grpprl(&[(0x840F, &360i16.to_le_bytes())]))],
|
||||
};
|
||||
let doc = read(&rich_doc_with(&spec, &RichExtra { prcs: prcs.clone(), prm, ..Default::default() })).expect("opens");
|
||||
let p = para0(&doc);
|
||||
(p.props.indent_left, p.props.align)
|
||||
};
|
||||
assert_eq!(para(0x0000), (Some(18.0), None), "no Prm: the PAPX's indent");
|
||||
assert_eq!(para(0x0001), (Some(72.0), Some(craft_wordmodel::props::Align::Center)), "Prm1: the Prc's sprms override the PAPX's");
|
||||
assert_eq!(para(0x020A), (Some(18.0), Some(craft_wordmodel::props::Align::Right)), "Prm0: sprmPJc right");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn piece_prm_character_formatting_applied() {
|
||||
// #458. Bit 0 of a Prm is fComplex ([MS-DOC] §2.9.215/§2.9.216): Prm0 = isprm in bits
|
||||
|
||||
@@ -0,0 +1,747 @@
|
||||
//! Word 6.0/95 fixtures, built in code. No public specification describes this version; these
|
||||
//! build the layout observed in real files and described in `crate::word6`.
|
||||
|
||||
use proptest::prelude::*;
|
||||
|
||||
use super::{body_texts, cfb_file};
|
||||
use crate::{DocbinError, read};
|
||||
|
||||
/// Where the text starts: right after the FIB, as in every observed file.
|
||||
const TEXT_AT: u32 = 0x300;
|
||||
|
||||
/// One stylesheet entry as Word 6/95 stores it.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
struct Std<'a> {
|
||||
/// The built-in style id, or 0x0FFE for a user-defined style.
|
||||
sti: u16,
|
||||
/// 1 paragraph, 2 character.
|
||||
sgc: u16,
|
||||
/// The base style's index, or 0x0FFF.
|
||||
base: u16,
|
||||
name: &'a str,
|
||||
/// Paragraph sprms (paragraph styles only).
|
||||
papx: &'a [u8],
|
||||
/// Character sprms.
|
||||
chpx: &'a [u8],
|
||||
}
|
||||
|
||||
/// The stylesheet bytes for `styles`: the 14-byte header (count, fixed-part size 8, …), then per
|
||||
/// style a size and the entry, or 0 for an empty slot.
|
||||
fn stylesheet(styles: &[Option<Std>]) -> Vec<u8> {
|
||||
let mut v = vec![14u8, 0];
|
||||
for w in [styles.len() as u16, 8, 1, 0x4B, 0x0F, 0, 0] {
|
||||
v.extend_from_slice(&w.to_le_bytes());
|
||||
}
|
||||
for (istd, st) in styles.iter().enumerate() {
|
||||
let Some(st) = st else {
|
||||
v.extend_from_slice(&[0, 0]);
|
||||
continue;
|
||||
};
|
||||
let cupx: u16 = if st.sgc == 1 { 2 } else { 1 };
|
||||
let mut e = Vec::new();
|
||||
for w in [st.sti, st.sgc | st.base << 4, cupx, 0] {
|
||||
e.extend_from_slice(&w.to_le_bytes());
|
||||
}
|
||||
e.push(st.name.len() as u8);
|
||||
e.extend_from_slice(st.name.as_bytes());
|
||||
e.push(0);
|
||||
let upx = |e: &mut Vec<u8>, bytes: &[u8]| {
|
||||
if e.len() % 2 == 1 {
|
||||
e.push(0);
|
||||
}
|
||||
e.extend_from_slice(&(bytes.len() as u16).to_le_bytes());
|
||||
e.extend_from_slice(bytes);
|
||||
};
|
||||
if st.sgc == 1 {
|
||||
let mut papx = (istd as u16).to_le_bytes().to_vec();
|
||||
papx.extend_from_slice(st.papx);
|
||||
upx(&mut e, &papx);
|
||||
}
|
||||
upx(&mut e, st.chpx);
|
||||
v.extend_from_slice(&(e.len() as u16).to_le_bytes());
|
||||
v.extend_from_slice(&e);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// What a test Word 6.0/95 file holds. Offsets in `runs` and `pieces` are into `text`.
|
||||
#[derive(Clone, Default)]
|
||||
struct Doc6<'a> {
|
||||
nfib: u16,
|
||||
flags: u16,
|
||||
/// The stored 8-bit text.
|
||||
text: &'a [u8],
|
||||
/// (charset, name) per font index.
|
||||
fonts: &'a [(u8, &'a str)],
|
||||
/// The font table's entries as raw bytes instead of `fonts` (the size word is added).
|
||||
raw_fonts: Option<&'a [u8]>,
|
||||
/// Contiguous character runs (lo, hi, grpprl), all in one FKP page.
|
||||
runs: &'a [(u32, u32, &'a [u8])],
|
||||
/// Contiguous paragraph runs (lo, hi, grpprl), style 0, all in one FKP page. Each covers the
|
||||
/// paragraph whose mark lies in it.
|
||||
paras: &'a [(u32, u32, &'a [u8])],
|
||||
/// For a fast-saved file: (length, offset) per piece, in CP order.
|
||||
pieces: Option<&'a [(u32, u32)]>,
|
||||
/// Each piece's Prm (0 when absent or shorter than `pieces`).
|
||||
prms: &'a [u16],
|
||||
/// The Clx's Prc grpprls, which a Prm1 indexes.
|
||||
prcs: &'a [&'a [u8]],
|
||||
/// The FIB's character set word at 0x14.
|
||||
chse: u16,
|
||||
/// The style of each paragraph run (0, Normal, when absent or shorter than `paras`).
|
||||
para_styles: &'a [u16],
|
||||
/// The stylesheet: one entry per style index, `None` for an empty slot.
|
||||
styles: &'a [Option<Std<'a>>],
|
||||
/// The main text's length in characters, when it isn't the stored bytes' (16-bit text).
|
||||
ccp: Option<u32>,
|
||||
}
|
||||
|
||||
fn put16(v: &mut [u8], at: usize, x: u16) {
|
||||
v[at..at + 2].copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
|
||||
fn put32(v: &mut [u8], at: usize, x: u32) {
|
||||
v[at..at + 4].copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
|
||||
/// Append one FKP page for `runs` and its bin table, and return the bin table's (fc, lcb). A
|
||||
/// character page has a 1-byte entry per run and CHPXs (byte count + grpprl); a paragraph page
|
||||
/// has 7-byte entries and PAPXs (word count + style 0 + grpprl padded to whole words).
|
||||
fn fkp(w: &mut Vec<u8>, runs: &[(u32, u32, &[u8])], papx: bool, istds: &[u16]) -> (u32, u32) {
|
||||
let (Some(first), Some(last)) = (runs.first(), runs.last()) else { return (0, 0) };
|
||||
w.resize(w.len().div_ceil(512) * 512, 0);
|
||||
let pn = (w.len() / 512) as u16;
|
||||
let crun = runs.len();
|
||||
let entry = if papx { 7 } else { 1 };
|
||||
let mut page = [0u8; 512];
|
||||
for (k, (lo, _, _)) in runs.iter().enumerate() {
|
||||
put32(&mut page, 4 * k, TEXT_AT + lo);
|
||||
}
|
||||
put32(&mut page, 4 * crun, TEXT_AT + last.1);
|
||||
// Records from the end of the page down, each at an even offset.
|
||||
let mut top = 511usize;
|
||||
for (k, (_, _, g)) in runs.iter().enumerate() {
|
||||
let body: Vec<u8> = if papx {
|
||||
let mut b = istds.get(k).copied().unwrap_or(0).to_le_bytes().to_vec();
|
||||
b.extend_from_slice(g);
|
||||
if b.len() % 2 == 1 {
|
||||
b.push(0);
|
||||
}
|
||||
b
|
||||
} else if g.is_empty() {
|
||||
continue;
|
||||
} else {
|
||||
g.to_vec()
|
||||
};
|
||||
let at = (top - 1 - body.len()) & !1;
|
||||
page[at] = if papx { (body.len() / 2) as u8 } else { body.len() as u8 };
|
||||
page[at + 1..at + 1 + body.len()].copy_from_slice(&body);
|
||||
page[4 * (crun + 1) + entry * k] = (at / 2) as u8;
|
||||
top = at;
|
||||
}
|
||||
page[511] = crun as u8;
|
||||
w.extend_from_slice(&page);
|
||||
let mut bte = Vec::new();
|
||||
bte.extend_from_slice(&(TEXT_AT + first.0).to_le_bytes());
|
||||
bte.extend_from_slice(&(TEXT_AT + last.1).to_le_bytes());
|
||||
bte.extend_from_slice(&pn.to_le_bytes());
|
||||
let at = w.len() as u32;
|
||||
w.extend_from_slice(&bte);
|
||||
(at, bte.len() as u32)
|
||||
}
|
||||
|
||||
/// The WordDocument stream: FIB, text, then the FKP pages, font table and piece table.
|
||||
fn word_stream(d: &Doc6) -> Vec<u8> {
|
||||
let mut w = vec![0u8; TEXT_AT as usize];
|
||||
w.extend_from_slice(d.text);
|
||||
let ccp = match (d.ccp, d.pieces) {
|
||||
(Some(ccp), _) => ccp,
|
||||
(None, pieces) => match pieces {
|
||||
Some(p) => p.iter().map(|(len, _)| len).sum(),
|
||||
None => d.text.len() as u32,
|
||||
},
|
||||
};
|
||||
let mut pairs: Vec<(usize, u32, u32)> = Vec::new();
|
||||
for (pair, runs, papx) in [(12, d.runs, false), (13, d.paras, true)] {
|
||||
let (fc, lcb) = fkp(&mut w, runs, papx, d.para_styles);
|
||||
if lcb > 0 {
|
||||
pairs.push((pair, fc, lcb));
|
||||
}
|
||||
}
|
||||
if !d.styles.is_empty() {
|
||||
let st = stylesheet(d.styles);
|
||||
pairs.push((1, w.len() as u32, st.len() as u32));
|
||||
w.extend_from_slice(&st);
|
||||
}
|
||||
if !d.fonts.is_empty() || d.raw_fonts.is_some() {
|
||||
let mut ffn = vec![0u8, 0];
|
||||
ffn.extend_from_slice(d.raw_fonts.unwrap_or_default());
|
||||
for (charset, name) in d.fonts {
|
||||
let mut e = vec![0u8, 0x16, 0x90, 0x01, *charset, 0x00];
|
||||
e.extend_from_slice(name.as_bytes());
|
||||
e.push(0);
|
||||
e[0] = (e.len() - 1) as u8;
|
||||
ffn.extend_from_slice(&e);
|
||||
}
|
||||
let size = ffn.len() as u16;
|
||||
put16(&mut ffn, 0, size);
|
||||
pairs.push((15, w.len() as u32, ffn.len() as u32));
|
||||
w.extend_from_slice(&ffn);
|
||||
}
|
||||
if let Some(pieces) = d.pieces {
|
||||
let mut plc = Vec::new();
|
||||
let mut cp = 0u32;
|
||||
plc.extend_from_slice(&cp.to_le_bytes());
|
||||
for (len, _) in pieces {
|
||||
cp += len;
|
||||
plc.extend_from_slice(&cp.to_le_bytes());
|
||||
}
|
||||
for (i, (_, off)) in pieces.iter().enumerate() {
|
||||
plc.extend_from_slice(&[0, 0]);
|
||||
plc.extend_from_slice(&(TEXT_AT + off).to_le_bytes());
|
||||
plc.extend_from_slice(&d.prms.get(i).copied().unwrap_or(0).to_le_bytes());
|
||||
}
|
||||
let mut clx = Vec::new();
|
||||
for g in d.prcs {
|
||||
clx.push(0x01);
|
||||
clx.extend_from_slice(&(g.len() as u16).to_le_bytes());
|
||||
clx.extend_from_slice(g);
|
||||
}
|
||||
clx.push(0x02);
|
||||
clx.extend_from_slice(&(plc.len() as u32).to_le_bytes());
|
||||
clx.extend_from_slice(&plc);
|
||||
pairs.push((33, w.len() as u32, clx.len() as u32));
|
||||
w.extend_from_slice(&clx);
|
||||
}
|
||||
put16(&mut w, 0x00, 0xA5DC);
|
||||
put16(&mut w, 0x02, if d.nfib == 0 { 0x0065 } else { d.nfib });
|
||||
put16(&mut w, 0x06, 0x0409);
|
||||
put16(&mut w, 0x0A, d.flags | if d.pieces.is_some() { 0x0004 } else { 0 });
|
||||
put16(&mut w, 0x0C, 0x0065);
|
||||
put16(&mut w, 0x14, d.chse);
|
||||
put32(&mut w, 0x18, TEXT_AT);
|
||||
put32(&mut w, 0x1C, TEXT_AT + d.text.len() as u32);
|
||||
let len = w.len() as u32;
|
||||
put32(&mut w, 0x20, len);
|
||||
put32(&mut w, 0x34, ccp);
|
||||
for (i, fc, lcb) in pairs {
|
||||
put32(&mut w, 0x58 + i * 8, fc);
|
||||
put32(&mut w, 0x58 + i * 8 + 4, lcb);
|
||||
}
|
||||
w
|
||||
}
|
||||
|
||||
/// A Word 6.0/95 file holding `d`: the WordDocument stream only, as the real ones have no Table
|
||||
/// stream.
|
||||
fn word6_file(d: &Doc6) -> Vec<u8> {
|
||||
cfb_file(&[("WordDocument", &word_stream(d))])
|
||||
}
|
||||
|
||||
fn texts(d: &Doc6) -> Vec<String> {
|
||||
body_texts(&read(&word6_file(d)).expect("a Word 6.0/95 file opens"))
|
||||
}
|
||||
|
||||
/// Fonts 0 (Western) and 1 (Cyrillic, charset 204), as the Russian files have.
|
||||
const FONTS: &[(u8, &str)] = &[(0, "Times New Roman"), (204, "Times New Roman Cyr")];
|
||||
|
||||
#[test]
|
||||
fn word_6_and_95_files_open_as_paragraphs() {
|
||||
for nfib in [0x0065, 0x0068] {
|
||||
let d = Doc6 { nfib, text: b"Hello\rWorld\r", ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Hello", "World"], "nFib 0x{nfib:04X}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_saved_text_follows_the_piece_table() {
|
||||
// Stored out of order; the pieces' fcs are plain byte offsets (Word 97 would halve them).
|
||||
let d = Doc6 { text: b"World\rHello\r", pieces: Some(&[(6, 6), (6, 0)]), ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Hello", "World"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_saved_file_without_a_piece_table_is_malformed() {
|
||||
let d = Doc6 { flags: 0x0004, text: b"Hello\r", ..Default::default() };
|
||||
assert!(matches!(read(&word6_file(&d)), Err(DocbinError::Malformed(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_runs_font_charset_chooses_its_code_page() {
|
||||
// "Da " in font 0, then 0xC4 0xE0 ("Да" in Windows-1251) in the Cyrillic font 1.
|
||||
let text = b"Da \xC4\xE0\r";
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[]), (3, 5, &[0x5D, 0x01, 0x00]), (5, 6, &[])];
|
||||
let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Da Да"]);
|
||||
}
|
||||
|
||||
/// Text in an East Asian code page (a Japanese font, charset 128: Shift-JIS) is refused, not
|
||||
/// read a byte at a time into replacement characters (review of wordcraft#6345).
|
||||
#[test]
|
||||
fn east_asian_text_is_refused_not_lost() {
|
||||
let fonts: &[(u8, &str)] = &[(0, "Times New Roman"), (128, "MS Mincho")];
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x01, 0x00]), (2, 3, &[])];
|
||||
let d = Doc6 { text: b"\x93\xfa\r", fonts, runs, ..Default::default() };
|
||||
assert!(matches!(read(&word6_file(&d)), Err(DocbinError::NotWord(e)) if e.contains("East Asian")));
|
||||
// The same file in a Western font still opens.
|
||||
let d = Doc6 { text: b"\x93\xfa\r", fonts, runs: &[], ..Default::default() };
|
||||
assert_eq!(texts(&d).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_font_sprm_is_found_after_other_sprms() {
|
||||
// As in real files: bold-like toggles, a size, a picture anchor's length-prefixed sprm.
|
||||
let text = b"\xC4\xE0\r";
|
||||
for g in [&[0x55, 0x81, 0x63, 0x18, 0x00, 0x5D, 0x01, 0x00][..], &[0x75, 0x01, 0x44, 0x04, 0, 0, 0, 0, 0x5D, 0x01, 0x00][..]] {
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, g), (2, 3, &[])];
|
||||
let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да"], "grpprl {g:02X?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_last_font_sprm_of_a_run_chooses_its_code_page() {
|
||||
// Font 0, then font 1 (Cyrillic): the later sprm is the one the formatting applies.
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x00, 0x00, 0x5D, 0x01, 0x00]), (2, 3, &[])];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_sprm_leaves_font_0() {
|
||||
// 0x99 has no known operand length, so the rest can't be framed: font 0 (Windows-1252).
|
||||
let text = b"\xC4\xE0\r";
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x99, 0x5D, 0x01, 0x00]), (2, 3, &[])];
|
||||
let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Äà"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_toggle_sprms_before_the_font_are_framed() {
|
||||
// 0x57 (strike, a one-byte toggle in [MS-DOC]'s Prm0 isprm table) before the font sprm: the
|
||||
// font must still be found, or the Cyrillic text reads as Windows-1252.
|
||||
let text = b"\xC4\xE0\r";
|
||||
for op in [0x57, 0x58, 0x59, 0x5C] {
|
||||
let g = [op, 0x01, 0x5D, 0x01, 0x00];
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, &g), (2, 3, &[])];
|
||||
let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да"], "opcode 0x{op:02X}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_font_entry_too_short_for_its_charset_ends_the_font_table() {
|
||||
// Entry 0 says it is 3 bytes long, which can't hold the charset at offset 4. The bytes after
|
||||
// it (the next entry, whose flags byte is 0xCC) must not be read as font 0's charset.
|
||||
let raw: &[u8] = &[0x02, 0x16, 0x90, 0x05, 0xCC, 0x90, 0x01, 0xCC, 0x00, b'X', 0];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", raw_fonts: Some(raw), runs: &[(0, 3, &[])], ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Äà"], "font 0 has no charset: Windows-1252");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bin_table_naming_one_page_many_times_reads_it_once() {
|
||||
// A hostile bin table: 1,000,000 entries, all naming one page of 50 Cyrillic runs. The text still
|
||||
// reads right; that the page is read once is pinned in `word6::tests`.
|
||||
let text: Vec<u8> = std::iter::repeat_n(0xC4u8, 50).chain(*b"\r").collect();
|
||||
let g: &[u8] = &[0x5D, 0x01, 0x00];
|
||||
let runs: Vec<(u32, u32, &[u8])> = (0..50).map(|i| (i, i + 1, g)).chain([(50, 51, &[][..])]).collect();
|
||||
let mut w = word_stream(&Doc6 { text: &text, fonts: FONTS, runs: &runs, ..Default::default() });
|
||||
let u32_at = |w: &[u8], at: usize| u32::from_le_bytes([w[at], w[at + 1], w[at + 2], w[at + 3]]);
|
||||
let bte_fc = u32_at(&w, 0x58 + 12 * 8) as usize;
|
||||
let pn = u16::from_le_bytes([w[bte_fc + 8], w[bte_fc + 9]]);
|
||||
let n = 1_000_000usize;
|
||||
let mut bte = Vec::with_capacity(4 * (n + 1) + 2 * n);
|
||||
bte.extend_from_slice(&TEXT_AT.to_le_bytes());
|
||||
for _ in 0..n {
|
||||
bte.extend_from_slice(&(TEXT_AT + 51).to_le_bytes());
|
||||
}
|
||||
for _ in 0..n {
|
||||
bte.extend_from_slice(&pn.to_le_bytes());
|
||||
}
|
||||
let at = w.len() as u32;
|
||||
w.extend_from_slice(&bte);
|
||||
put32(&mut w, 0x58 + 12 * 8, at);
|
||||
put32(&mut w, 0x58 + 12 * 8 + 4, bte.len() as u32);
|
||||
let doc = read(&cfb_file(&[("WordDocument", &w)])).expect("opens");
|
||||
assert_eq!(body_texts(&doc), ["Д".repeat(50)]);
|
||||
}
|
||||
|
||||
/// `s` as UTF-16LE bytes.
|
||||
fn utf16(s: &str) -> Vec<u8> {
|
||||
s.encode_utf16().flat_map(u16::to_le_bytes).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_with_extended_characters_holds_utf16_text() {
|
||||
// fExtChar (0x1000): Word reads the text as 16-bit units.
|
||||
let text = utf16("Да ok\r");
|
||||
let d = Doc6 { flags: 0x1000, text: &text, ccp: Some(6), ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да ok"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utf16_text_stops_at_the_end_of_its_bytes() {
|
||||
// Word reads no more 16-bit characters than the text's byte range (0x18..0x1C) holds, even
|
||||
// when the character count says more.
|
||||
let text = utf16("Hi\r");
|
||||
let d = Doc6 { flags: 0x1000, text: &text, ccp: Some(50), ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Hi"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_stops_at_the_end_of_its_bytes() {
|
||||
// The same for 8-bit text: Word read 19 characters of a file whose count said 40. The font
|
||||
// table after the text must not be read as body text.
|
||||
let d = Doc6 { text: b"Hi\r", ccp: Some(50), fonts: FONTS, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Hi"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_saved_utf16_pieces_are_at_plain_byte_offsets() {
|
||||
let text = utf16("World\rHello\r");
|
||||
let d = Doc6 { flags: 0x1000, text: &text, pieces: Some(&[(6, 12), (6, 0)]), ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Hello", "World"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_macintosh_document_reads_as_mac_roman() {
|
||||
// FIB 0x14 = 0x0100: byte 0x8E is "é" (Mac Roman), not "Ž" (Windows-1252), even in a run whose
|
||||
// font is Cyrillic.
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x01, 0x00]), (2, 3, &[])];
|
||||
let d = Doc6 { chse: 0x0100, text: b"H\x8E\r", fonts: FONTS, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Hé"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symbol_document_maps_characters_to_private_use() {
|
||||
// FIB 0x14 = 0x0002: every character from the space up is U+F000 + byte; marks stay marks.
|
||||
let d = Doc6 { chse: 0x0002, text: b"Hi\x8E\rA\r", ..Default::default() };
|
||||
assert_eq!(texts(&d), ["\u{F048}\u{F069}\u{F08E}", "\u{F041}"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn another_character_set_leaves_only_ascii_readable() {
|
||||
let d = Doc6 { chse: 0x0001, text: b"H\x8Ei\r", ..Default::default() };
|
||||
assert_eq!(texts(&d), ["H\u{FFFD}i"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_prm_on_the_marks_piece_formats_its_paragraph() {
|
||||
// As Word 95 fast-saves a 1" left indent on the first paragraph: the piece holding its mark
|
||||
// has Prm1 -> Prc `11 a0 05` (left indent 1440), and its PAPX says nothing. Word shows 72 pt;
|
||||
// the second paragraph keeps none.
|
||||
let text = b"First\rSecond\r";
|
||||
let pieces: &[(u32, u32)] = &[(5, 0), (1, 5), (7, 6)];
|
||||
let d = Doc6 { text, pieces: Some(pieces), prms: &[0, 0x0001], prcs: &[&[0x11, 0xA0, 0x05]], ..Default::default() };
|
||||
let doc = read(&word6_file(&d)).expect("opens");
|
||||
let paras: Vec<_> = doc.body.iter().filter_map(|b| b.as_para()).collect();
|
||||
assert_eq!(paras.iter().map(|p| p.text.as_str()).collect::<Vec<_>>(), ["First", "Second"]);
|
||||
assert_eq!((paras[0].props.indent_left, paras[1].props.indent_left), (Some(72.0), None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pieces_prm_formats_its_text() {
|
||||
// As Word 95 fast-saves formatting: piece 0 has Prm1 -> a Prc setting font 1 and bold, which
|
||||
// its character run doesn't. Piece 0 shows in font 1, bold (and decodes in its code page);
|
||||
// piece 1 keeps the run's plain formatting.
|
||||
let text = b"\xC4\xE0\xC4\xE0\r";
|
||||
let d =
|
||||
Doc6 { text, fonts: FONTS, pieces: Some(&[(2, 0), (3, 2)]), prms: &[0x0001], prcs: &[&[0x5D, 0x01, 0x00, 0x55, 0x01]], ..Default::default() };
|
||||
let doc = read(&word6_file(&d)).expect("opens");
|
||||
let p = first_para(&doc);
|
||||
assert_eq!(p.text, "ДаÄà");
|
||||
let first = &p.runs.first().expect("a run").props;
|
||||
assert_eq!((first.font.as_deref(), first.bold), (Some("Times New Roman Cyr"), Some(true)));
|
||||
let last = &p.runs.last().expect("a run").props;
|
||||
assert_eq!((last.font.as_deref(), last.bold), (None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_font_in_a_pieces_prm_chooses_its_code_page() {
|
||||
// As Word 95 fast-saves a font change: piece 0 ("\xC4\xE0") has Prm1 -> Prc 0 = sprm 0x5D font 1
|
||||
// (Cyrillic), while its character run names no font. Piece 1 keeps the run's font 0.
|
||||
let text = b"\xC4\xE0\xC4\xE0\r";
|
||||
let d = Doc6 { text, fonts: FONTS, pieces: Some(&[(2, 0), (3, 2)]), prms: &[0x0001], prcs: &[&[0x5D, 0x01, 0x00]], ..Default::default() };
|
||||
assert_eq!(texts(&d), ["ДаÄà"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_prm_index_past_the_prcs_is_ignored() {
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, pieces: Some(&[(3, 0)]), prms: &[0x0011], prcs: &[&[0x5D, 0x01, 0x00]], ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Äà"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn password_protected_files_are_refused() {
|
||||
let d = Doc6 { flags: 0x0100, text: b"secret\r", ..Default::default() };
|
||||
assert!(matches!(read(&word6_file(&d)), Err(DocbinError::Encryption(_))));
|
||||
}
|
||||
|
||||
/// The first paragraph of `doc`'s body.
|
||||
fn first_para(doc: &craft_wordmodel::Document) -> &craft_wordmodel::para::Paragraph {
|
||||
doc.body.iter().find_map(|b| b.as_para()).expect("a paragraph")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn character_formatting_is_read() {
|
||||
// Bold on, italic as the opposite of the (unread, so plain) style, 14 pt, red, single
|
||||
// underline, font 2: the Word 6 opcodes as observed.
|
||||
let g: &[u8] = &[0x55, 0x01, 0x56, 0x81, 0x63, 0x1C, 0x00, 0x62, 0x06, 0x5E, 0x01, 0x5D, 0x02, 0x00];
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, g), (2, 3, &[])];
|
||||
let fonts = &[(0, "Times New Roman"), (2, "Symbol"), (0, "Arial")];
|
||||
let doc = read(&word6_file(&Doc6 { text: b"Hi\r", fonts, runs, ..Default::default() })).expect("opens");
|
||||
let p = first_para(&doc);
|
||||
let c = &p.runs.first().expect("a run").props;
|
||||
assert_eq!((c.bold, c.italic, c.size), (Some(true), Some(true), Some(14.0)));
|
||||
assert_eq!(c.color, Some(craft_wordmodel::props::TextColor::Rgb(craft_wordmodel::props::Rgb(0xFF, 0, 0))));
|
||||
assert_eq!(c.underline, Some(craft_wordmodel::props::Underline::Single));
|
||||
assert_eq!(c.font.as_deref(), Some("Arial"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unseen_operand_values_are_left_unread() {
|
||||
// Underline 3 and "multiple" line spacing were never seen in a real file, so they aren't
|
||||
// guessed at.
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[0x5E, 0x03])];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 3, &[0x14, 0xF0, 0x00, 0x01, 0x00])];
|
||||
let doc = read(&word6_file(&Doc6 { text: b"Hi\r", runs, paras, ..Default::default() })).expect("opens");
|
||||
let p = first_para(&doc);
|
||||
assert_eq!(p.runs.first().and_then(|r| r.props.underline), None);
|
||||
assert_eq!(p.props.line_spacing, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unseen_alignment_value_is_left_unread() {
|
||||
// Only 0-3 were seen; 4 would otherwise become "distribute" and 0xFF "left".
|
||||
for jc in [0x04u8, 0xFF] {
|
||||
let g = [0x05, jc];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 3, &g)];
|
||||
let doc = read(&word6_file(&Doc6 { text: b"Hi\r", paras, ..Default::default() })).expect("opens");
|
||||
assert_eq!(first_para(&doc).props.align, None, "jc {jc:#04x}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_formatting_is_read() {
|
||||
// Justified, 0.5" left and 0.25" right indents, a 0.5" hanging first line, at least 27 pt.
|
||||
let g: &[u8] = &[0x05, 0x03, 0x11, 0xD0, 0x02, 0x10, 0x68, 0x01, 0x13, 0x30, 0xFD, 0x14, 0x1C, 0x02, 0x00, 0x00];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 3, g), (3, 6, &[0x05, 0x01])];
|
||||
let doc = read(&word6_file(&Doc6 { text: b"Hi\rHo\r", paras, ..Default::default() })).expect("opens");
|
||||
let ps: Vec<_> = doc.body.iter().filter_map(|b| b.as_para()).collect();
|
||||
let p = &ps[0].props;
|
||||
assert_eq!(p.align, Some(craft_wordmodel::props::Align::Justify));
|
||||
assert_eq!((p.indent_left, p.indent_right, p.indent_first), (Some(36.0), Some(18.0), Some(-36.0)));
|
||||
assert_eq!(p.line_spacing, Some(craft_wordmodel::props::LineSpacing::AtLeast(27.0)));
|
||||
assert_eq!(ps[1].props.align, Some(craft_wordmodel::props::Align::Center));
|
||||
}
|
||||
|
||||
/// A Word 6 table definition sprm (0xBE): the cell edges, then a 10-byte descriptor per cell
|
||||
/// whose first word holds the merge flags.
|
||||
fn def_table(edges: &[i16], rgfs: &[u16]) -> Vec<u8> {
|
||||
let mut rest = vec![rgfs.len() as u8];
|
||||
for e in edges {
|
||||
rest.extend_from_slice(&e.to_le_bytes());
|
||||
}
|
||||
for r in rgfs {
|
||||
rest.extend_from_slice(&r.to_le_bytes());
|
||||
rest.extend_from_slice(&[0; 8]);
|
||||
}
|
||||
let mut v = vec![0xBE];
|
||||
v.extend_from_slice(&((rest.len() + 1) as u16).to_le_bytes());
|
||||
v.extend_from_slice(&rest);
|
||||
v
|
||||
}
|
||||
|
||||
/// The rows of the first table in `doc`'s body, each as its cells' texts.
|
||||
fn table_rows(doc: &craft_wordmodel::Document) -> Vec<Vec<String>> {
|
||||
let t = doc.body.iter().find_map(|b| match b.as_ref() {
|
||||
craft_wordmodel::Block::Table(t) => Some(t),
|
||||
_ => None,
|
||||
});
|
||||
let t = t.expect("a table");
|
||||
t.rows
|
||||
.iter()
|
||||
.map(|r| r.cells.iter().map(|c| c.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.clone()).collect::<Vec<_>>().join("|")).collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_rows_and_merged_cells_are_read() {
|
||||
// Row 1: "A", "B". Row 2: "C" merged across both cells. In-table marks carry 0x18, the row
|
||||
// ends 0x18, 0x19 and the table definition.
|
||||
let text = b"A\x07B\x07\x07C\x07\x07\x07after\r";
|
||||
let mut row1 = vec![0x18, 1, 0x19, 1];
|
||||
row1.extend(def_table(&[0, 1440, 2880], &[0, 0]));
|
||||
let mut row2 = vec![0x18, 1, 0x19, 1];
|
||||
row2.extend(def_table(&[0, 1440, 2880], &[0x0001, 0x0002]));
|
||||
let cell: &[u8] = &[0x18, 1];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 2, cell), (2, 4, cell), (4, 5, &row1), (5, 7, cell), (7, 8, cell), (8, 9, &row2), (9, 15, &[])];
|
||||
let doc = read(&word6_file(&Doc6 { text, paras, ..Default::default() })).expect("opens");
|
||||
assert_eq!(table_rows(&doc), [vec!["A", "B"], vec!["C"]]);
|
||||
assert_eq!(body_texts(&doc), ["after"], "the paragraph after the table stays outside it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn several_merges_in_one_row_each_keep_their_cells() {
|
||||
// Five cells merged as [A B] [C] [D E]: the second merge must not be shifted by the first.
|
||||
let text = b"A\x07B\x07C\x07D\x07E\x07\x07";
|
||||
let mut row = vec![0x18, 1, 0x19, 1];
|
||||
row.extend(def_table(&[0, 100, 200, 300, 400, 500], &[0x0001, 0x0002, 0x0000, 0x0001, 0x0002]));
|
||||
let cell: &[u8] = &[0x18, 1];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 2, cell), (2, 4, cell), (4, 6, cell), (6, 8, cell), (8, 10, cell), (10, 11, &row)];
|
||||
let doc = read(&word6_file(&Doc6 { text, paras, ..Default::default() })).expect("opens");
|
||||
assert_eq!(table_rows(&doc), [vec!["A|B", "C", "D|E"]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_field_shows_its_cached_result() {
|
||||
let d = Doc6 { text: b"Date \x13DATE \x1412/12/97\x15\r", ..Default::default() };
|
||||
let text = read(&word6_file(&d)).expect("opens").plain_text(craft_wordmodel::StoryRef::Body);
|
||||
assert!(text.contains("Date 12/12/97"), "{text:?}");
|
||||
assert!(!text.contains("DATE"), "{text:?}");
|
||||
}
|
||||
|
||||
/// A fixture's paragraph styles: Normal, an empty slot, then "Mine" (user-defined, centred, bold, on
|
||||
/// Normal) and "Heading" stored under a local name with sti 1.
|
||||
const STYLES: &[Option<Std>] = &[
|
||||
Some(Std { sti: 0, sgc: 1, base: 0x0FFF, name: "Standard", papx: &[], chpx: &[0x61, 0x09, 0x04] }),
|
||||
None,
|
||||
Some(Std { sti: 0x0FFE, sgc: 1, base: 0, name: "Mine", papx: &[0x05, 0x01], chpx: &[0x55, 0x01] }),
|
||||
Some(Std { sti: 1, sgc: 1, base: 0, name: "Überschrift 1", papx: &[0x08, 0x01], chpx: &[0x55, 0x81, 0x63, 0x20, 0x00] }),
|
||||
];
|
||||
|
||||
/// Each paragraph's style name, through the document's style sheet.
|
||||
fn style_names(doc: &craft_wordmodel::Document) -> Vec<String> {
|
||||
doc.body
|
||||
.iter()
|
||||
.filter_map(|b| b.as_para())
|
||||
.map(|p| p.props.style.as_deref().and_then(|id| doc.styles.get(id)).map_or(String::new(), |s| s.name.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraphs_take_their_styles() {
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 2, &[]), (2, 4, &[]), (4, 6, &[])];
|
||||
let d = Doc6 { text: b"A\rB\rC\r", paras, para_styles: &[0, 2, 3], styles: STYLES, ..Default::default() };
|
||||
let doc = read(&word6_file(&d)).expect("opens");
|
||||
// A built-in style goes by its built-in name, whatever the file stores (as in Word).
|
||||
assert_eq!(style_names(&doc), ["Normal", "Mine", "Heading 1"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn styles_carry_their_formatting_and_base() {
|
||||
let d = Doc6 { text: b"A\r", styles: STYLES, ..Default::default() };
|
||||
let doc = read(&word6_file(&d)).expect("opens");
|
||||
let mine = doc.styles.styles.iter().find(|s| s.name == "Mine").expect("Mine");
|
||||
assert_eq!(mine.para.align, Some(craft_wordmodel::props::Align::Center));
|
||||
assert_eq!(mine.chr.bold, Some(true));
|
||||
assert_eq!(mine.based_on.as_deref().and_then(|id| doc.styles.get(id)).map(|s| s.name.as_str()), Some("Normal"));
|
||||
let heading = doc.styles.styles.iter().find(|s| s.name == "Heading 1").expect("Heading 1");
|
||||
assert_eq!((heading.para.keep_next, heading.chr.size), (Some(true), Some(16.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bulleted_styles_frame_past_their_numbering() {
|
||||
// 0x0D (1 byte) and 0x0C (a count byte and its bytes) before the indents, as in the corpus.
|
||||
let mut papx = vec![0x05, 0x03, 0x0D, 0x0B, 0x11, 0x1C, 0x01, 0x13, 0xE4, 0xFE, 0x0C, 4, 0xFF, 0x01, 0x00, 0x08];
|
||||
papx.extend_from_slice(&[0x0E, 0x01, 0x2C, 0x00, 0x1D, 0x60, 0x1A, 0xF8, 0xFF, 0x25, 0x02, 0x10, 0x68, 0x01]);
|
||||
let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", papx: &papx, ..Default::default() })];
|
||||
let doc = read(&word6_file(&Doc6 { text: b"A\r", styles, ..Default::default() })).expect("opens");
|
||||
let normal = doc.styles.get("Normal").expect("Normal");
|
||||
assert_eq!((normal.para.indent_left, normal.para.indent_first, normal.para.indent_right), (Some(14.2), Some(-14.2), Some(18.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_runs_language_or_its_styles_chooses_the_code_page_without_a_font() {
|
||||
// No font named anywhere: the style's language decides (Russian -> Windows-1251), as in Word.
|
||||
let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x19, 0x04], ..Default::default() })];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, styles, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да"]);
|
||||
// The run's own language outranks the style's.
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[0x61, 0x09, 0x04])];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, styles, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Äà"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_0_is_not_a_fallback() {
|
||||
// Font 0 is Cyrillic, but nothing names it and the style's language is English: Word reads
|
||||
// Windows-1252.
|
||||
let fonts = &[(204, "Times New Roman Cyr"), (0, "Arial")];
|
||||
let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x09, 0x04], ..Default::default() })];
|
||||
// A character run that names no font (where the reader used to take font 0).
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[])];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts, styles, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Äà"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_font_named_in_the_style_chooses_the_code_page() {
|
||||
// The base style names the Cyrillic font 1; the run and its own style name none.
|
||||
let styles = &[
|
||||
Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x5D, 0x01, 0x00], ..Default::default() }),
|
||||
Some(Std { sti: 0x0FFE, sgc: 1, base: 0, name: "Child", ..Default::default() }),
|
||||
];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 3, &[])];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, styles, paras, para_styles: &[1], ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ansi_font_leaves_the_code_page_to_the_language() {
|
||||
// Arial (ANSI) named on the run, the style's language Russian: Word reads Cyrillic.
|
||||
let fonts = &[(0, "Times New Roman"), (0, "Arial")];
|
||||
let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x19, 0x04], ..Default::default() })];
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x01, 0x00]), (2, 3, &[])];
|
||||
let d = Doc6 { text: b"\xC4\xE0\r", fonts, styles, runs, ..Default::default() };
|
||||
assert_eq!(texts(&d), ["Да"]);
|
||||
}
|
||||
|
||||
/// A fixture with every structure read: runs, fonts and a piece table.
|
||||
fn full_stream() -> Vec<u8> {
|
||||
let runs: &[(u32, u32, &[u8])] = &[(0, 6, &[0x55, 0x81]), (6, 9, &[0x5D, 0x01, 0x00]), (9, 12, &[])];
|
||||
let styles = &[
|
||||
Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x09, 0x04], ..Default::default() }),
|
||||
None,
|
||||
Some(Std { sti: 0x0FFE, sgc: 1, base: 0, name: "Mine", papx: &[0x05, 0x01], chpx: &[0x55, 0x01] }),
|
||||
];
|
||||
let paras: &[(u32, u32, &[u8])] = &[(0, 6, &[]), (6, 12, &[])];
|
||||
word_stream(&Doc6 {
|
||||
text: b"Hello\r\xC4\xE0\rx\r\r",
|
||||
fonts: FONTS,
|
||||
runs,
|
||||
paras,
|
||||
para_styles: &[2, 0],
|
||||
styles,
|
||||
pieces: Some(&[(6, 0), (6, 6)]),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_full_fixture_reads() {
|
||||
let f = cfb_file(&[("WordDocument", &full_stream())]);
|
||||
let doc = read(&f).expect("opens");
|
||||
assert_eq!(body_texts(&doc), ["Hello", "Да", "x", ""]);
|
||||
assert_eq!(style_names(&doc)[0], "Mine");
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(256))]
|
||||
|
||||
#[test]
|
||||
fn corrupt_word6_streams_never_panic(edits in proptest::collection::vec((0..0x700usize, any::<u8>()), 1..16)) {
|
||||
let mut w = full_stream();
|
||||
for (at, b) in edits {
|
||||
if let Some(x) = w.get_mut(at) {
|
||||
*x = b;
|
||||
}
|
||||
}
|
||||
let _ = read(&cfb_file(&[("WordDocument", &w)]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_word6_streams_never_panic(n in 0..0x700usize) {
|
||||
let w = full_stream();
|
||||
let _ = read(&cfb_file(&[("WordDocument", &w[..n.min(w.len())])]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,842 @@
|
||||
//! Word 6.0 and Word 95 `.doc` files (`wIdent` 0xA5DC, `nFib` 0x0065 and 0x0068).
|
||||
//!
|
||||
//! No public specification describes this version: [MS-DOC] covers Word 97 and later. Everything
|
||||
//! here is observed, from the layout of real files checked against what Word shows when it opens
|
||||
//! them (its text through COM, its formatting in the `.docx` Word saves from them), plus the Word
|
||||
//! 97 structures in [MS-DOC] that these files visibly share. Nothing else is assumed.
|
||||
//!
|
||||
//! What is read: the main text, its paragraphs and fields, the code page of each run, the
|
||||
//! character and paragraph formatting listed below, table rows, and the paragraph styles (with
|
||||
//! their formatting). Character styles, sections, headers and footers, notes, pictures, borders,
|
||||
//! shading and tabs are not read yet.
|
||||
//!
|
||||
//! The layout as observed:
|
||||
//!
|
||||
//! - A compound file with a `WordDocument` stream and **no Table stream**: the structures that
|
||||
//! Word 97 keeps in `0Table`/`1Table` live in the WordDocument stream itself.
|
||||
//! - The FIB is one fixed block. The flags at 0x0A have the Word 97 meaning for bit 2 (fast-saved,
|
||||
//! set exactly in the files that carry a piece table) and, as far as can be told, bit 8
|
||||
//! (encrypted). The text starts at the u32 at 0x18, and the main text's length in characters is
|
||||
//! the u32 at 0x34.
|
||||
//! - From 0x58, (u32 fc, u32 lcb) pairs, in the order of Word 97's `FibRgFcLcb97`. Index 12 is the
|
||||
//! character bin table, 13 the paragraph bin table, 15 the font table and 33 the piece table.
|
||||
//! - Text is one byte per character, in the code page of the font of its run. The FIB's language
|
||||
//! does not choose it: files marked English hold Russian and Czech text that Word shows as such.
|
||||
//! - Both bin tables hold 2-byte page numbers (Word 97: 4 bytes). The FKP pages have the Word 97
|
||||
//! layout, except that a paragraph page's entries are 7 bytes (an offset byte and 6 more; Word
|
||||
//! 97: 13), and a PAPX is a count of 2-byte words followed by exactly that many words: the
|
||||
//! 2-byte style index, then the grpprl (padded with one 0 byte to whole words).
|
||||
//! - Sprms have **1-byte opcodes**. Those with a 1-byte operand match the opcodes [MS-DOC]'s
|
||||
//! `Prm0` table lists for the same properties (bold 0x55, colour 0x62, in-table 0x18…); the
|
||||
//! others were identified against Word. See [`chp`] and [`pap`].
|
||||
//! - A font table entry is a size byte (the entry's length − 1), a flags byte, a 2-byte weight, the
|
||||
//! charset byte, one more byte, then the name in 8-bit characters.
|
||||
//! - The stylesheet (pair 1) has the Word 97 shape with smaller parts: a u16 header size (14),
|
||||
//! then a header of that many bytes whose first two words are the style count and the size of
|
||||
//! a style's fixed part (8, Word 97: 10);
|
||||
//! then per style a u16 size (0: an empty slot) and that many bytes: the fixed part (sti, then
|
||||
//! kind and base style, then UPX count and next style, then a size), the name (a length byte,
|
||||
//! 8-bit characters, a 0), and the UPXs, each a u16 size and its bytes, padded to even. A
|
||||
//! paragraph style's first UPX is its style index and paragraph sprms, the second its character
|
||||
//! sprms; a character style has one, its character sprms.
|
||||
|
||||
use craft_codepage::charset_code_page;
|
||||
|
||||
use crate::DocbinError;
|
||||
use crate::fkp::{Bins, TranslatedRun};
|
||||
use crate::piece::{CodePageRun, PieceTable, SYMBOL_CODE_PAGE, Word6Text};
|
||||
use crate::stsh::RawStyle;
|
||||
|
||||
/// `wIdent` of a Word 6.0/95 file.
|
||||
const W_IDENT: u16 = 0xA5DC;
|
||||
const OFF_FLAGS: usize = 0x0A;
|
||||
/// Fast-saved: the text is in pieces listed by a piece table.
|
||||
const F_COMPLEX: u16 = 0x0004;
|
||||
/// Password-protected (Word 97's bit; no unprotected file sets it).
|
||||
const F_ENCRYPTED: u16 = 0x0100;
|
||||
/// Word 97's `fExtChar` bit. Word reads a Word 6.0/95 file that sets it as UTF-16 text (each
|
||||
/// piece's fc a plain byte offset), and so does this reader.
|
||||
const F_EXT_CHAR: u16 = 0x1000;
|
||||
/// The document's character set (u16): 0 Windows, 0x0100 Macintosh (all 8-bit text is Mac Roman,
|
||||
/// whatever the fonts say), 0x0002 Symbol (every byte a private-use character). Word reads the
|
||||
/// bytes above ASCII of any other value as U+FFFD.
|
||||
const OFF_CHSE: usize = 0x14;
|
||||
const OFF_FC_MAC: usize = 0x1C;
|
||||
/// Bin table entries read, as for Word 97 (`fkp::MAX_BINS`).
|
||||
const MAX_BINS: usize = 100_000;
|
||||
/// FKP runs read (character or paragraph): far more than any real file's, and few enough to stay small.
|
||||
const MAX_RUNS: usize = 1_000_000;
|
||||
const OFF_FC_MIN: usize = 0x18;
|
||||
const OFF_CCP_TEXT: usize = 0x34;
|
||||
const OFF_PAIRS: usize = 0x58;
|
||||
/// Pairs read: up to and including the piece table's.
|
||||
const PAIRS: usize = 34;
|
||||
const PAIR_STSH: usize = 1;
|
||||
const PAIR_BTE_CHPX: usize = 12;
|
||||
const PAIR_BTE_PAPX: usize = 13;
|
||||
const PAIR_FONTS: usize = 15;
|
||||
const PAIR_CLX: usize = 33;
|
||||
/// More characters than Word's own documents get near: the FIB is corrupt (as for Word 97).
|
||||
const MAX_CCP: u32 = 100_000_000;
|
||||
/// The font table's entries: more than any real file has.
|
||||
const MAX_FONTS: usize = 0x1_0000;
|
||||
/// FKP page size in bytes.
|
||||
const PAGE: usize = 512;
|
||||
/// Size of a paragraph FKP entry (offset byte + 6).
|
||||
const PAPX_BX: usize = 7;
|
||||
/// Most cells a row's table definition is read for (the model's own cap is 63).
|
||||
const MAX_CELLS: usize = 63;
|
||||
/// Styles read: style indexes stop below 0x0FFF ("no style").
|
||||
const MAX_STYLES: usize = 0x0FFE;
|
||||
/// Base-style links followed when a style inherits a font or language.
|
||||
const MAX_STYLE_DEPTH: usize = 16;
|
||||
|
||||
/// Character sprms read, by Word 6.0/95 opcode. Each one was matched against Word's own reading of
|
||||
/// the corpus files (the `.docx` it saves from them).
|
||||
mod chp {
|
||||
/// Bold, a toggle (0 off, 1 on, 0x80 as the style, 0x81 the opposite of the style).
|
||||
pub(super) const BOLD: u8 = 0x55;
|
||||
/// Italic, a toggle.
|
||||
pub(super) const ITALIC: u8 = 0x56;
|
||||
/// Small capitals, a toggle.
|
||||
pub(super) const SMALL_CAPS: u8 = 0x5A;
|
||||
/// All capitals, a toggle.
|
||||
pub(super) const CAPS: u8 = 0x5B;
|
||||
/// The font: a 2-byte index into the font table.
|
||||
pub(super) const FONT: u8 = 0x5D;
|
||||
/// Underline: 0 none, 1 single (the only values seen).
|
||||
pub(super) const UNDERLINE: u8 = 0x5E;
|
||||
/// Colour: an index into Word's 16-colour palette, as Word 97's `Ico`.
|
||||
pub(super) const COLOR: u8 = 0x62;
|
||||
/// Size in half-points (2 bytes).
|
||||
pub(super) const SIZE: u8 = 0x63;
|
||||
/// The language (2 bytes, a Windows LCID): with no non-ANSI font, it gives the code page.
|
||||
pub(super) const LID: u8 = 0x61;
|
||||
}
|
||||
|
||||
/// Paragraph and table sprms read, by Word 6.0/95 opcode, matched like [`chp`]'s.
|
||||
mod pap {
|
||||
/// Alignment: 0 left, 1 centred, 2 right, 3 justified.
|
||||
pub(super) const JC: u8 = 0x05;
|
||||
/// Keep with the next paragraph (1 byte; on the heading styles H1-H6, which Word shows with
|
||||
/// keepNext; [MS-DOC]'s isprm table has sprmPFKeepFollow at 0x08).
|
||||
pub(super) const KEEP_NEXT: u8 = 0x08;
|
||||
/// Right indent in twips (signed, 2 bytes).
|
||||
pub(super) const RIGHT: u8 = 0x10;
|
||||
/// Left indent in twips.
|
||||
pub(super) const LEFT: u8 = 0x11;
|
||||
/// First-line indent in twips, relative to the left one (negative: hanging).
|
||||
pub(super) const FIRST: u8 = 0x13;
|
||||
/// Line spacing: the 4-byte Word 97 `LSPD` (dyaLine, fMultLinespace).
|
||||
pub(super) const LINE: u8 = 0x14;
|
||||
/// The paragraph is in a table.
|
||||
pub(super) const IN_TABLE: u8 = 0x18;
|
||||
/// The mark ends a table row.
|
||||
pub(super) const TTP: u8 = 0x19;
|
||||
/// Row alignment (2 bytes): 1 centred.
|
||||
pub(super) const ROW_JC: u8 = 0xB6;
|
||||
/// Row height in twips (signed, 2 bytes): positive at least, negative exactly.
|
||||
pub(super) const ROW_HEIGHT: u8 = 0xBD;
|
||||
/// The row's cells: u16 count, cell count, (count + 1) i16 cell edges, then a 10-byte
|
||||
/// descriptor per cell whose first word's bit 0 starts a horizontal merge and bit 1 continues
|
||||
/// it.
|
||||
pub(super) const DEF_TABLE: u8 = 0xBE;
|
||||
}
|
||||
|
||||
/// Word 97 opcodes the translations produce ([MS-DOC] §2.6).
|
||||
mod w97 {
|
||||
pub(super) const C_BOLD: u16 = 0x0835;
|
||||
pub(super) const C_ITALIC: u16 = 0x0836;
|
||||
pub(super) const C_SMALL_CAPS: u16 = 0x083A;
|
||||
pub(super) const C_CAPS: u16 = 0x083B;
|
||||
pub(super) const C_KUL: u16 = 0x2A3E;
|
||||
pub(super) const C_ICO: u16 = 0x2A42;
|
||||
pub(super) const C_HPS: u16 = 0x4A43;
|
||||
pub(super) const C_FTC0: u16 = 0x4A4F;
|
||||
pub(super) const P_JC80: u16 = 0x2403;
|
||||
pub(super) const P_KEEP_FOLLOW: u16 = 0x2406;
|
||||
pub(super) const P_DXA_RIGHT80: u16 = 0x840E;
|
||||
pub(super) const P_DXA_LEFT80: u16 = 0x840F;
|
||||
pub(super) const P_DXA_LEFT1_80: u16 = 0x8411;
|
||||
pub(super) const P_DYA_LINE: u16 = 0x6412;
|
||||
pub(super) const P_IN_TABLE: u16 = 0x2416;
|
||||
pub(super) const P_TTP: u16 = 0x2417;
|
||||
pub(super) const T_JC: u16 = 0x5400;
|
||||
pub(super) const T_ROW_HEIGHT: u16 = 0x9407;
|
||||
pub(super) const T_DEF_TABLE: u16 = 0xD608;
|
||||
pub(super) const T_MERGE: u16 = 0x5624;
|
||||
}
|
||||
|
||||
/// Is this WordDocument stream a Word 6.0/95 one?
|
||||
pub(crate) fn is_word6(word: &[u8]) -> bool {
|
||||
word.get(0..2) == Some(&W_IDENT.to_le_bytes()[..])
|
||||
}
|
||||
|
||||
/// The fields of a Word 6.0/95 FIB that are read.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Fib6 {
|
||||
complex: bool,
|
||||
/// Text is UTF-16 (fExtChar).
|
||||
wide: bool,
|
||||
chse: u16,
|
||||
fc_min: u32,
|
||||
fc_mac: u32,
|
||||
/// Characters in the main text.
|
||||
pub(crate) ccp_text: u32,
|
||||
pairs: [(u32, u32); PAIRS],
|
||||
}
|
||||
|
||||
impl Fib6 {
|
||||
pub(crate) fn parse(word: &[u8]) -> Result<Fib6, DocbinError> {
|
||||
let u16_at = |at: usize| word.get(at..at + 2).map(|b| u16::from_le_bytes([b[0], b[1]]));
|
||||
let u32_at = |at: usize| word.get(at..at + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
|
||||
if word.len() < OFF_PAIRS + PAIRS * 8 {
|
||||
return Err(DocbinError::NotWord(format!("Word 6.0/95 WordDocument stream is {} bytes", word.len())));
|
||||
}
|
||||
let flags = u16_at(OFF_FLAGS).unwrap_or(0);
|
||||
if flags & F_ENCRYPTED != 0 {
|
||||
return Err(DocbinError::Encryption("password-protected Word 6.0/95 files can't be opened".into()));
|
||||
}
|
||||
let ccp_text = u32_at(OFF_CCP_TEXT).unwrap_or(0);
|
||||
if ccp_text > MAX_CCP {
|
||||
return Err(DocbinError::Limit(format!("document claims {ccp_text} characters")));
|
||||
}
|
||||
let mut pairs = [(0, 0); PAIRS];
|
||||
for (i, p) in pairs.iter_mut().enumerate() {
|
||||
let at = OFF_PAIRS + i * 8;
|
||||
*p = (u32_at(at).unwrap_or(0), u32_at(at + 4).unwrap_or(0));
|
||||
}
|
||||
Ok(Fib6 {
|
||||
complex: flags & F_COMPLEX != 0,
|
||||
wide: flags & F_EXT_CHAR != 0,
|
||||
chse: u16_at(OFF_CHSE).unwrap_or(0),
|
||||
fc_min: u32_at(OFF_FC_MIN).unwrap_or(0),
|
||||
fc_mac: u32_at(OFF_FC_MAC).unwrap_or(0),
|
||||
ccp_text,
|
||||
pairs,
|
||||
})
|
||||
}
|
||||
|
||||
/// The (fc, lcb) pair at index `i`, if it points at anything.
|
||||
fn pair(&self, i: usize) -> Option<(u32, u32)> {
|
||||
self.pairs.get(i).copied().filter(|&(_, lcb)| lcb > 0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything the walk needs from a Word 6.0/95 file besides its text stream.
|
||||
pub(crate) struct Content {
|
||||
pub(crate) pieces: PieceTable,
|
||||
/// Character formatting, translated to Word 97 grpprls.
|
||||
pub(crate) chpx: Bins,
|
||||
/// Paragraph formatting and table rows, translated to Word 97 grpprls.
|
||||
pub(crate) papx: Bins,
|
||||
/// Font names by index (the walk resolves translated font sprms against them).
|
||||
pub(crate) fonts: Vec<String>,
|
||||
/// The styles by index, their formatting translated to Word 97 grpprls.
|
||||
pub(crate) styles: Vec<RawStyle>,
|
||||
}
|
||||
|
||||
/// Read the text, formatting and fonts of a Word 6.0/95 file.
|
||||
///
|
||||
/// 8-bit text decodes, as Word does, in the code page of: the document's character set when it
|
||||
/// isn't Windows ([`OFF_CHSE`]); else the font a piece's Prm names (as Word 95 writes when a fast
|
||||
/// save changes a font) when its charset has a code page; else, the same for the font of the
|
||||
/// character run, or the one its paragraph style (or a base of it) names; else the language of
|
||||
/// the run, or of its paragraph style; else Windows-1252. A font is only one named somewhere:
|
||||
/// font 0 is not a fallback, and a font with the ANSI charset leaves the choice to the language.
|
||||
pub(crate) fn content(word: &[u8], fib: &Fib6) -> Result<Content, DocbinError> {
|
||||
let fonts = fib.pair(PAIR_FONTS).map(|(fc, lcb)| font_table(word, fc, lcb)).unwrap_or_default();
|
||||
let chp_runs = fib.pair(PAIR_BTE_CHPX).map(|(fc, lcb)| fkp_runs(word, fc, lcb, false)).unwrap_or_default();
|
||||
let pap_runs = fib.pair(PAIR_BTE_PAPX).map(|(fc, lcb)| fkp_runs(word, fc, lcb, true)).unwrap_or_default();
|
||||
let (styles, facts) = fib.pair(PAIR_STSH).map(|(fc, lcb)| stylesheet(word, fc, lcb)).unwrap_or_default();
|
||||
let windows = fib.chse == 0;
|
||||
let default_code_page = match fib.chse {
|
||||
// Text that no run covers is formatted by the Normal style.
|
||||
0 => text_code_page(&fonts, &facts, 0, None, None),
|
||||
0x0100 => MAC_ROMAN,
|
||||
0x0002 => SYMBOL_CODE_PAGE,
|
||||
// Not a code page: bytes above ASCII read as U+FFFD.
|
||||
_ => 0,
|
||||
};
|
||||
let code_pages = if windows { code_page_runs(&chp_runs, &pap_runs, &facts, &fonts, default_code_page) } else { Vec::new() };
|
||||
// shortcut: East Asian (lead and trail byte) text is refused, as all Word 6.0/95 files were
|
||||
// before; read it a character at a time when the piece walk can decode byte pairs.
|
||||
let east_asian = || DocbinError::NotWord("Word 6.0/95 text in an East Asian code page is not read yet".into());
|
||||
if craft_codepage::is_multibyte_code_page(default_code_page) || code_pages.iter().any(|r| craft_codepage::is_multibyte_code_page(r.code_page)) {
|
||||
return Err(east_asian());
|
||||
}
|
||||
let multibyte_piece = std::cell::Cell::new(false);
|
||||
let text = Word6Text { wide: fib.wide, default_code_page, code_pages };
|
||||
let pieces = if fib.complex {
|
||||
let (fc, lcb) = fib.pair(PAIR_CLX).ok_or_else(|| DocbinError::Malformed("the fast-saved file has no piece table".into()))?;
|
||||
// A Prm1's Prc may name the piece's font (sprm 0x5D); a Prm0 can't, its operand being one
|
||||
// byte.
|
||||
let piece_code_page = |prm: u16, prcs: &[Vec<u8>]| -> Option<u16> {
|
||||
if !windows || prm & 1 == 0 {
|
||||
return None;
|
||||
}
|
||||
let cp = last_font(prcs.get(usize::from(prm >> 1))?).and_then(|f| font_code_page(&fonts, f));
|
||||
if cp.is_some_and(craft_codepage::is_multibyte_code_page) {
|
||||
multibyte_piece.set(true);
|
||||
}
|
||||
cp
|
||||
};
|
||||
// The Prcs then carry Word 97 sprms, as the CHPXs and PAPXs do: the walk applies their
|
||||
// character sprms to the piece's text and their paragraph sprms to the paragraph whose mark
|
||||
// the piece holds.
|
||||
let table = PieceTable::parse_word6(word, fc, lcb, text, piece_code_page, translate_prc)?;
|
||||
if multibyte_piece.get() {
|
||||
return Err(east_asian());
|
||||
}
|
||||
table
|
||||
} else {
|
||||
// Word reads no more characters than the text's byte range (fcMin..fcMac) holds, one byte
|
||||
// each or two for UTF-16, whatever the character count says.
|
||||
let unit = if fib.wide { 2 } else { 1 };
|
||||
let ccp = fib.ccp_text.min(fib.fc_mac.saturating_sub(fib.fc_min) / unit);
|
||||
PieceTable::contiguous_word6(fib.fc_min, ccp, text)
|
||||
};
|
||||
let chpx = Bins::translated(chp_runs.iter().map(|r| TranslatedRun { lo: r.lo, hi: r.hi, istd: 0, grpprl: translate_chp(r.grpprl) }).collect());
|
||||
let papx =
|
||||
Bins::translated(pap_runs.iter().map(|r| TranslatedRun { lo: r.lo, hi: r.hi, istd: r.istd, grpprl: translate_pap(r.grpprl) }).collect());
|
||||
Ok(Content { pieces, chpx, papx, fonts: fonts.into_iter().map(|f| f.name).collect(), styles })
|
||||
}
|
||||
|
||||
/// Mac OS Roman, the 8-bit text of a Macintosh Word document.
|
||||
const MAC_ROMAN: u16 = craft_codepage::MAC_ROMAN_CODE_PAGE;
|
||||
|
||||
/// The code page of font `font`'s charset, if it has one (not for ANSI, default or symbol
|
||||
/// charsets, whose text Word decodes by its language).
|
||||
fn font_code_page(fonts: &[Font], font: u16) -> Option<u16> {
|
||||
fonts.get(usize::from(font)).and_then(|f| charset_code_page(i32::from(f.charset)))
|
||||
}
|
||||
|
||||
/// Windows' ANSI code page for the language `lid` (an LCID), which Word uses for 8-bit text in a
|
||||
/// font without a charset of its own. Russian (1251) and English (1252) were checked against Word;
|
||||
/// the rest is the standard Windows assignment.
|
||||
fn lid_code_page(lid: u16) -> u16 {
|
||||
match lid & 0x03FF {
|
||||
// Serbian in Cyrillic (0x0C1A, 0x1C1A); Croatian and Serbian in Latin otherwise.
|
||||
0x1A => match lid {
|
||||
0x0C1A | 0x1C1A => 1251,
|
||||
_ => 1250,
|
||||
},
|
||||
0x02 | 0x19 | 0x22 | 0x23 | 0x2F | 0x3F | 0x40 | 0x44 | 0x50 => 1251,
|
||||
0x05 | 0x0E | 0x15 | 0x18 | 0x1B | 0x1C | 0x24 => 1250,
|
||||
0x08 => 1253,
|
||||
0x1F => 1254,
|
||||
0x0D => 1255,
|
||||
0x01 | 0x20 | 0x29 => 1256,
|
||||
0x25..=0x27 => 1257,
|
||||
0x2A => 1258,
|
||||
0x1E => 874,
|
||||
0x11 => 932,
|
||||
0x12 => 949,
|
||||
0x04 => match lid {
|
||||
0x0804 | 0x1004 => 936,
|
||||
_ => 950,
|
||||
},
|
||||
_ => 1252,
|
||||
}
|
||||
}
|
||||
|
||||
/// A style's own font and language sprms and its base, for inheritance ([`StyleFacts::resolve`]).
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
struct StyleFacts {
|
||||
base: u16,
|
||||
font: Option<u16>,
|
||||
lid: Option<u16>,
|
||||
}
|
||||
|
||||
impl StyleFacts {
|
||||
/// Style `istd`'s font and language, each from the style itself or the nearest base that has
|
||||
/// one.
|
||||
fn resolve(facts: &[StyleFacts], istd: u16) -> (Option<u16>, Option<u16>) {
|
||||
let (mut font, mut lid) = (None, None);
|
||||
let mut at = istd;
|
||||
for _ in 0..MAX_STYLE_DEPTH {
|
||||
let Some(f) = facts.get(usize::from(at)) else { break };
|
||||
font = font.or(f.font);
|
||||
lid = lid.or(f.lid);
|
||||
if font.is_some() && lid.is_some() {
|
||||
break;
|
||||
}
|
||||
at = f.base;
|
||||
}
|
||||
(font, lid)
|
||||
}
|
||||
}
|
||||
|
||||
/// The styles of the stylesheet at `word[fc..fc+lcb]`, by index (an empty slot is a default
|
||||
/// style, which keeps the indexes aligned), with their formatting translated, and each style's
|
||||
/// own font and language. Reading stops at the first entry that runs past the table.
|
||||
fn stylesheet(word: &[u8], fc: u32, lcb: u32) -> (Vec<RawStyle>, Vec<StyleFacts>) {
|
||||
let Some(st) = (fc as usize).checked_add(lcb as usize).and_then(|end| word.get(fc as usize..end)) else { return Default::default() };
|
||||
let u16_at = |b: &[u8], at: usize| b.get(at..at + 2).map(|x| u16::from_le_bytes([x[0], x[1]]));
|
||||
let Some(cb_header) = u16_at(st, 0).map(usize::from) else { return Default::default() };
|
||||
let (Some(cstd), Some(cb_base)) = (u16_at(st, 2), u16_at(st, 4)) else { return Default::default() };
|
||||
let cb_base = usize::from(cb_base);
|
||||
let (mut styles, mut facts) = (Vec::new(), Vec::new());
|
||||
let mut at = 2 + cb_header;
|
||||
for _ in 0..usize::from(cstd).min(MAX_STYLES) {
|
||||
let Some(cb) = u16_at(st, at).map(usize::from) else { break };
|
||||
let Some(std) = st.get(at + 2..at + 2 + cb) else { break };
|
||||
at += 2 + cb;
|
||||
let (style, fact) = style(std, cb_base).unwrap_or_else(|| (RawStyle::default(), StyleFacts { base: 0x0FFF, ..Default::default() }));
|
||||
styles.push(style);
|
||||
facts.push(fact);
|
||||
}
|
||||
(styles, facts)
|
||||
}
|
||||
|
||||
/// The names of the built-in styles by sti: the "Index within built-in styles" of the reserved
|
||||
/// style names in [MS-OE376] (Part 4 §2.7.3.9), which [MS-DOC]'s StdfBase says sti is. 0x0FFE
|
||||
/// (user-defined) and anything past the table keep the name stored in the file.
|
||||
const BUILT_IN_NAMES: [&str; 107] = [
|
||||
"Normal",
|
||||
"heading 1",
|
||||
"heading 2",
|
||||
"heading 3",
|
||||
"heading 4",
|
||||
"heading 5",
|
||||
"heading 6",
|
||||
"heading 7",
|
||||
"heading 8",
|
||||
"heading 9",
|
||||
"index 1",
|
||||
"index 2",
|
||||
"index 3",
|
||||
"index 4",
|
||||
"index 5",
|
||||
"index 6",
|
||||
"index 7",
|
||||
"index 8",
|
||||
"index 9",
|
||||
"toc 1",
|
||||
"toc 2",
|
||||
"toc 3",
|
||||
"toc 4",
|
||||
"toc 5",
|
||||
"toc 6",
|
||||
"toc 7",
|
||||
"toc 8",
|
||||
"toc 9",
|
||||
"Normal Indent",
|
||||
"footnote text",
|
||||
"annotation text",
|
||||
"header",
|
||||
"footer",
|
||||
"index heading",
|
||||
"caption",
|
||||
"table of figures",
|
||||
"envelope address",
|
||||
"envelope return",
|
||||
"footnote reference",
|
||||
"annotation reference",
|
||||
"line number",
|
||||
"page number",
|
||||
"endnote reference",
|
||||
"endnote text",
|
||||
"table of authorities",
|
||||
"macro",
|
||||
"toa heading",
|
||||
"List",
|
||||
"List Bullet",
|
||||
"List Number",
|
||||
"List 2",
|
||||
"List 3",
|
||||
"List 4",
|
||||
"List 5",
|
||||
"List Bullet 2",
|
||||
"List Bullet 3",
|
||||
"List Bullet 4",
|
||||
"List Bullet 5",
|
||||
"List Number 2",
|
||||
"List Number 3",
|
||||
"List Number 4",
|
||||
"List Number 5",
|
||||
"Title",
|
||||
"Closing",
|
||||
"Signature",
|
||||
"Default Paragraph Font",
|
||||
"Body Text",
|
||||
"Body Text Indent",
|
||||
"List Continue",
|
||||
"List Continue 2",
|
||||
"List Continue 3",
|
||||
"List Continue 4",
|
||||
"List Continue 5",
|
||||
"Message Header",
|
||||
"Subtitle",
|
||||
"Salutation",
|
||||
"Date",
|
||||
"Body Text First Indent",
|
||||
"Body Text First Indent 2",
|
||||
"Note Heading",
|
||||
"Body Text 2",
|
||||
"Body Text 3",
|
||||
"Body Text Indent 2",
|
||||
"Body Text Indent 3",
|
||||
"Block Text",
|
||||
"Hyperlink",
|
||||
"FollowedHyperlink",
|
||||
"Strong",
|
||||
"Emphasis",
|
||||
"Document Map",
|
||||
"Plain Text",
|
||||
"E-mail Signature",
|
||||
"HTML Top of Form",
|
||||
"HTML Bottom of Form",
|
||||
"Normal (Web)",
|
||||
"HTML Acronym",
|
||||
"HTML Address",
|
||||
"HTML Cite",
|
||||
"HTML Code",
|
||||
"HTML Definition",
|
||||
"HTML Keyboard",
|
||||
"HTML Preformatted",
|
||||
"HTML Sample",
|
||||
"HTML Typewriter",
|
||||
"HTML Variable",
|
||||
"Normal Table",
|
||||
"annotation subject",
|
||||
];
|
||||
|
||||
/// One style entry (`None` for an empty slot or one too short for its fixed part and name).
|
||||
fn style(std: &[u8], cb_base: usize) -> Option<(RawStyle, StyleFacts)> {
|
||||
let u16_at = |at: usize| std.get(at..at + 2).map(|x| u16::from_le_bytes([x[0], x[1]]));
|
||||
if cb_base < 8 {
|
||||
return None;
|
||||
}
|
||||
let (sti, kind_base, upx_next) = (u16_at(0)? & 0x0FFF, u16_at(2)?, u16_at(4)?);
|
||||
let (stk, istd_base, cupx) = ((kind_base & 0x000F) as u8, kind_base >> 4, usize::from(upx_next & 0x000F));
|
||||
let len = usize::from(*std.get(cb_base)?);
|
||||
// A built-in style goes by its built-in name, as in Word, whatever name the file stores
|
||||
// ("Default" or "Обычный" for Normal). Other names are 8-bit, as Word shows them
|
||||
// (Windows-1252: names it couldn't store read back with '?').
|
||||
let stored = std.get(cb_base + 1..cb_base + 1 + len)?;
|
||||
let name: String = match BUILT_IN_NAMES.get(usize::from(sti)) {
|
||||
Some(built_in) => (*built_in).to_owned(),
|
||||
None => stored.iter().map(|&b| craft_codepage::cp1252(b)).collect(),
|
||||
};
|
||||
let mut upxs: Vec<&[u8]> = Vec::new();
|
||||
let mut j = cb_base + 1 + len + 1;
|
||||
for _ in 0..cupx.min(3) {
|
||||
j += j % 2;
|
||||
let Some(n) = u16_at(j).map(usize::from) else { break };
|
||||
let Some(upx) = std.get(j + 2..j + 2 + n) else { break };
|
||||
upxs.push(upx);
|
||||
j += 2 + n;
|
||||
}
|
||||
// A paragraph style: style index + paragraph sprms, then character sprms. A character style:
|
||||
// character sprms.
|
||||
let (papx, chpx) = match stk {
|
||||
1 => (upxs.first().and_then(|u| u.get(2..)).unwrap_or_default(), upxs.get(1).copied().unwrap_or_default()),
|
||||
_ => (&[][..], upxs.first().copied().unwrap_or_default()),
|
||||
};
|
||||
let fact = StyleFacts { base: istd_base, font: last_font(chpx), lid: last_lid(chpx) };
|
||||
Some((RawStyle { stk, istd_base, name, papx: translate_pap(papx), chpx: translate_chp(chpx) }, fact))
|
||||
}
|
||||
|
||||
/// One font table entry.
|
||||
struct Font {
|
||||
name: String,
|
||||
charset: u8,
|
||||
}
|
||||
|
||||
/// The fonts of the font table at `word[fc..fc+lcb]`, by index. A truncated entry ends the table.
|
||||
fn font_table(word: &[u8], fc: u32, lcb: u32) -> Vec<Font> {
|
||||
let Some(table) = (fc as usize).checked_add(lcb as usize).and_then(|end| word.get(fc as usize..end)) else { return Vec::new() };
|
||||
// The table starts with its own size, which bounds the entries along with `lcb`.
|
||||
let size = table.get(0..2).map_or(0, |b| usize::from(u16::from_le_bytes([b[0], b[1]])));
|
||||
let table = table.get(..size.min(table.len())).unwrap_or_default();
|
||||
let mut fonts = Vec::new();
|
||||
let mut at = 2usize;
|
||||
while let Some(&cb_m1) = table.get(at) {
|
||||
let next = at + usize::from(cb_m1) + 1;
|
||||
// The entry must lie inside the table and hold its charset (one too short is corrupt).
|
||||
let Some(entry) = table.get(at..next) else { break };
|
||||
let (Some(&charset), Some(name)) = (entry.get(4), entry.get(6..)) else { break };
|
||||
if fonts.len() >= MAX_FONTS {
|
||||
break;
|
||||
}
|
||||
// An 8-bit name; font names are ASCII in every file seen, other bytes read as Latin-1.
|
||||
let name = name.split(|&b| b == 0).next().unwrap_or_default().iter().map(|&b| char::from(b)).collect();
|
||||
fonts.push(Font { name, charset });
|
||||
at = next;
|
||||
}
|
||||
fonts
|
||||
}
|
||||
|
||||
/// One FKP run as stored: its FC range, the paragraph style (PAPX runs) and its Word 6 grpprl.
|
||||
struct Run<'w> {
|
||||
lo: u32,
|
||||
hi: u32,
|
||||
istd: u16,
|
||||
grpprl: &'w [u8],
|
||||
}
|
||||
|
||||
/// The page numbers of a bin table with `n` entries ((n+1) u32 FCs, then n u16 page numbers),
|
||||
/// each once, however many entries name it: a page's runs carry their own FCs, so reading it
|
||||
/// again would only repeat them. The entries read are capped as for Word 97.
|
||||
fn distinct_pages(plc: &[u8], n: usize) -> Vec<usize> {
|
||||
let mut pns: Vec<usize> = (0..n.min(MAX_BINS))
|
||||
.filter_map(|i| plc.get(4 * (n + 1) + 2 * i..4 * (n + 1) + 2 * i + 2).map(|b| usize::from(u16::from_le_bytes([b[0], b[1]]))))
|
||||
.collect();
|
||||
pns.sort_unstable();
|
||||
pns.dedup();
|
||||
pns
|
||||
}
|
||||
|
||||
/// Every run of the FKP pages a bin table at `word[fc..fc+lcb]` lists, each page read once and
|
||||
/// at most [`MAX_RUNS`] runs in all. Unreadable pages are skipped.
|
||||
fn fkp_runs(word: &[u8], fc: u32, lcb: u32, papx: bool) -> Vec<Run<'_>> {
|
||||
let Some(plc) = (fc as usize).checked_add(lcb as usize).and_then(|end| word.get(fc as usize..end)) else { return Vec::new() };
|
||||
let Some(n) = plc.len().checked_sub(4).filter(|r| r % 6 == 0).map(|r| r / 6) else { return Vec::new() };
|
||||
let entry = if papx { PAPX_BX } else { 1 };
|
||||
let mut runs = Vec::new();
|
||||
for pn in distinct_pages(plc, n) {
|
||||
if runs.len() >= MAX_RUNS {
|
||||
break;
|
||||
}
|
||||
let Some(page) = pn.checked_mul(PAGE).and_then(|at| word.get(at..at.checked_add(PAGE)?)) else { continue };
|
||||
let crun = usize::from(page.get(PAGE - 1).copied().unwrap_or(0));
|
||||
// The FCs and the entries must fit before the count byte.
|
||||
if 4 * (crun + 1) + entry * crun >= PAGE {
|
||||
continue;
|
||||
}
|
||||
let fc_at = |k: usize| page.get(4 * k..4 * k + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
|
||||
for k in 0..crun {
|
||||
let (Some(lo), Some(hi)) = (fc_at(k), fc_at(k + 1)) else { break };
|
||||
let off = usize::from(page.get(4 * (crun + 1) + entry * k).copied().unwrap_or(0)) * 2;
|
||||
let (istd, grpprl) = match (off, papx) {
|
||||
(0, _) => (0, &[][..]),
|
||||
// A CHPX: a byte count, then the grpprl.
|
||||
(_, false) => (0, page.get(off).and_then(|&cb| page.get(off + 1..off + 1 + usize::from(cb))).unwrap_or_default()),
|
||||
// A PAPX: a count of 2-byte words, then the style index and the grpprl.
|
||||
(_, true) => {
|
||||
let body = page.get(off).and_then(|&cw| page.get(off + 1..off + 1 + 2 * usize::from(cw))).unwrap_or_default();
|
||||
let istd = body.get(0..2).map_or(0, |b| u16::from_le_bytes([b[0], b[1]]));
|
||||
(istd, body.get(2..).unwrap_or_default())
|
||||
}
|
||||
};
|
||||
runs.push(Run { lo, hi, istd, grpprl });
|
||||
}
|
||||
}
|
||||
runs
|
||||
}
|
||||
|
||||
/// The sprms of a Word 6.0/95 grpprl as (opcode, operand). Reading stops at an opcode whose
|
||||
/// operand length is not known, since the rest can't be framed, and at a lone trailing 0 (the
|
||||
/// padding of a PAPX to whole words).
|
||||
fn sprms(grpprl: &[u8]) -> impl Iterator<Item = (u8, &[u8])> {
|
||||
let mut rest = grpprl;
|
||||
std::iter::from_fn(move || {
|
||||
let (&op, after) = rest.split_first()?;
|
||||
let len = match op {
|
||||
// 0x55-0x5C are the character toggles of [MS-DOC]'s Prm0 isprm table (bold, italic,
|
||||
// strike, outline, shadow, small caps, caps, hidden), all one byte; 0x55, 0x56, 0x5A
|
||||
// and 0x5B are also seen in real files.
|
||||
// 0x0D, 0x0E, 0x1D, 0x25 and 0x2C are one-byte paragraph sprms of [MS-DOC]'s isprm table
|
||||
// too (numbering level, no line numbers, frame anchoring, wrapping, no hyphenation).
|
||||
0x05 | 0x08 | 0x0D | 0x0E | 0x18 | 0x19 | 0x1D | 0x25 | 0x2C | 0x33 | 0x55..=0x5C | 0x5E | 0x62 | 0x68 | 0x75 | 0xB7 | 0xB9 => 1,
|
||||
// 0x1A/0x1B: a frame's position; 0x6B: kerning in half-points (H1 has 0x0024; Word
|
||||
// shows kern=36).
|
||||
0x10 | 0x11 | 0x13 | 0x15 | 0x16 | 0x1A | 0x1B | 0x26 | 0x27 | 0x28 | 0x29 | 0x2F | 0x50 | 0x5D | 0x60 | 0x61 | 0x63 | 0x6B | 0xB6
|
||||
| 0xB8 | 0xBD | 0xC0 => 2,
|
||||
0x14 => 4,
|
||||
// A count byte, then that many bytes (0x0C: a bulleted style's numbering, 52 bytes).
|
||||
0x0C | 0x0F | 0x44 | 0xBF => 1 + usize::from(*after.first()?),
|
||||
// A 2-byte count, then one byte fewer than it says (as Word 97's sprmTDefTable).
|
||||
0xBE => 2 + usize::from(u16::from_le_bytes([*after.first()?, *after.get(1)?])).saturating_sub(1),
|
||||
_ => return None,
|
||||
};
|
||||
let operand = after.get(..len)?;
|
||||
rest = after.get(len..).unwrap_or_default();
|
||||
Some((op, operand))
|
||||
})
|
||||
}
|
||||
|
||||
/// Append a Word 97 Prl: opcode, then operand.
|
||||
fn prl(out: &mut Vec<u8>, op: u16, operand: &[u8]) {
|
||||
out.extend_from_slice(&op.to_le_bytes());
|
||||
out.extend_from_slice(operand);
|
||||
}
|
||||
|
||||
/// A Word 6.0/95 CHPX grpprl as the Word 97 sprms it stands for. Sprms not read are left out.
|
||||
fn translate_chp(grpprl: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
for (op, v) in sprms(grpprl) {
|
||||
match op {
|
||||
chp::BOLD => prl(&mut out, w97::C_BOLD, v),
|
||||
chp::ITALIC => prl(&mut out, w97::C_ITALIC, v),
|
||||
chp::SMALL_CAPS => prl(&mut out, w97::C_SMALL_CAPS, v),
|
||||
chp::CAPS => prl(&mut out, w97::C_CAPS, v),
|
||||
// Only none and single were seen; any other value is left unread.
|
||||
chp::UNDERLINE if matches!(v, [0] | [1]) => prl(&mut out, w97::C_KUL, v),
|
||||
chp::COLOR => prl(&mut out, w97::C_ICO, v),
|
||||
chp::SIZE => prl(&mut out, w97::C_HPS, v),
|
||||
chp::FONT => prl(&mut out, w97::C_FTC0, v),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A Word 6.0/95 Prc grpprl as the Word 97 sprms it stands for: character and paragraph sprms
|
||||
/// alike (Word 95 records a paragraph change before a fast save as a Prc on the mark's piece).
|
||||
fn translate_prc(grpprl: &[u8]) -> Vec<u8> {
|
||||
[translate_chp(grpprl), translate_pap(grpprl)].concat()
|
||||
}
|
||||
|
||||
/// A Word 6.0/95 PAPX grpprl as the Word 97 sprms it stands for, table rows included. Sprms not
|
||||
/// read are left out.
|
||||
fn translate_pap(grpprl: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
for (op, v) in sprms(grpprl) {
|
||||
match op {
|
||||
// Only 0-3 were seen: another value is left unread.
|
||||
pap::JC if matches!(v, [0..=3]) => prl(&mut out, w97::P_JC80, v),
|
||||
pap::KEEP_NEXT => prl(&mut out, w97::P_KEEP_FOLLOW, v),
|
||||
pap::RIGHT => prl(&mut out, w97::P_DXA_RIGHT80, v),
|
||||
pap::LEFT => prl(&mut out, w97::P_DXA_LEFT80, v),
|
||||
pap::FIRST => prl(&mut out, w97::P_DXA_LEFT1_80, v),
|
||||
// Only "at least" spacing was seen (a positive height, not a multiple): the other
|
||||
// forms are left unread.
|
||||
pap::LINE => {
|
||||
if let [l0, l1, 0, 0] = v
|
||||
&& i16::from_le_bytes([*l0, *l1]) > 0
|
||||
{
|
||||
prl(&mut out, w97::P_DYA_LINE, v);
|
||||
}
|
||||
}
|
||||
pap::IN_TABLE => prl(&mut out, w97::P_IN_TABLE, v),
|
||||
pap::TTP => prl(&mut out, w97::P_TTP, v),
|
||||
pap::ROW_JC => prl(&mut out, w97::T_JC, v),
|
||||
pap::ROW_HEIGHT => prl(&mut out, w97::T_ROW_HEIGHT, v),
|
||||
pap::DEF_TABLE => def_table(&mut out, v),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A Word 6.0/95 table definition as a Word 97 `sprmTDefTable` holding the cell edges (the only
|
||||
/// part the table reader uses) and one `sprmTMerge` per horizontal merge.
|
||||
fn def_table(out: &mut Vec<u8>, v: &[u8]) {
|
||||
let Some(&itc) = v.get(2) else { return };
|
||||
let itc = usize::from(itc).min(MAX_CELLS);
|
||||
let Some(edges) = v.get(3..3 + 2 * (itc + 1)) else { return };
|
||||
// Word 97 operand: cb (u16: the bytes after it, plus one), the cell count, the edges.
|
||||
let cb = (1 + edges.len() + 1) as u16;
|
||||
out.extend_from_slice(&w97::T_DEF_TABLE.to_le_bytes());
|
||||
out.extend_from_slice(&cb.to_le_bytes());
|
||||
out.push(itc as u8);
|
||||
out.extend_from_slice(edges);
|
||||
let cells = v.get(3 + edges.len()..).unwrap_or_default();
|
||||
let mut merges: Vec<(usize, usize)> = Vec::new();
|
||||
let mut start: Option<usize> = None;
|
||||
for i in 0..=itc {
|
||||
let rgf = cells.get(10 * i..10 * i + 2).map_or(0, |b| u16::from_le_bytes([b[0], b[1]]));
|
||||
let continues = i < itc && rgf & 0x0002 != 0 && rgf & 0x0001 == 0;
|
||||
if continues {
|
||||
continue;
|
||||
}
|
||||
// A merge runs from its first cell through every cell that continues it.
|
||||
if let Some(a) = start.take()
|
||||
&& i > a + 1
|
||||
{
|
||||
merges.push((a, i));
|
||||
}
|
||||
if i < itc && rgf & 0x0001 != 0 {
|
||||
start = Some(i);
|
||||
}
|
||||
}
|
||||
// Right to left: the table reader folds each merge's cells into its first one as it goes, so
|
||||
// a merge further right must be applied before one to its left moves its cells.
|
||||
for (a, b) in merges.into_iter().rev() {
|
||||
prl(out, w97::T_MERGE, &[a as u8, b as u8]);
|
||||
}
|
||||
}
|
||||
|
||||
/// The code page of text in paragraph style `istd` whose run names `run_font` and `run_lid`: the
|
||||
/// run's font, else the style's (nearest in its base chain), when that font's charset has a code
|
||||
/// page; else the run's language, else the style's; else Windows-1252.
|
||||
fn text_code_page(fonts: &[Font], facts: &[StyleFacts], istd: u16, run_font: Option<u16>, run_lid: Option<u16>) -> u16 {
|
||||
let (style_font, style_lid) = StyleFacts::resolve(facts, istd);
|
||||
run_font.or(style_font).and_then(|f| font_code_page(fonts, f)).or_else(|| run_lid.or(style_lid).map(lid_code_page)).unwrap_or(1252)
|
||||
}
|
||||
|
||||
/// The language a grpprl names (sprm 0x61), if any: the last one.
|
||||
fn last_lid(grpprl: &[u8]) -> Option<u16> {
|
||||
sprms(grpprl).filter(|(op, _)| *op == chp::LID).filter_map(|(_, v)| Some(u16::from_le_bytes([*v.first()?, *v.get(1)?]))).last()
|
||||
}
|
||||
|
||||
/// The byte ranges whose code page is not the document default `default`, cut at every character and paragraph run
|
||||
/// boundary, each stretch in [`text_code_page`]'s. Sorted by start and not overlapping.
|
||||
fn code_page_runs(chp: &[Run], pap: &[Run], facts: &[StyleFacts], fonts: &[Font], default: u16) -> Vec<CodePageRun> {
|
||||
let sorted = |runs: &[Run]| -> Vec<(u32, u32, usize)> {
|
||||
let mut v: Vec<(u32, u32, usize)> = runs.iter().enumerate().filter(|(_, r)| r.lo < r.hi).map(|(i, r)| (r.lo, r.hi, i)).collect();
|
||||
v.sort_unstable();
|
||||
v
|
||||
};
|
||||
let (chp_sorted, pap_sorted) = (sorted(chp), sorted(pap));
|
||||
let covering = |v: &[(u32, u32, usize)], fc: u32| -> Option<usize> {
|
||||
let k = v.partition_point(|&(lo, _, _)| lo <= fc).checked_sub(1)?;
|
||||
v.get(k).filter(|&&(_, hi, _)| fc < hi).map(|&(_, _, i)| i)
|
||||
};
|
||||
let mut cuts: Vec<u32> = chp_sorted.iter().chain(pap_sorted.iter()).flat_map(|&(lo, hi, _)| [lo, hi]).collect();
|
||||
cuts.sort_unstable();
|
||||
cuts.dedup();
|
||||
let mut out: Vec<CodePageRun> = Vec::new();
|
||||
for w in cuts.windows(2) {
|
||||
let (&lo, &hi) = (w.first().unwrap_or(&0), w.get(1).unwrap_or(&0));
|
||||
let run = covering(&chp_sorted, lo).and_then(|i| chp.get(i));
|
||||
let para = covering(&pap_sorted, lo).and_then(|i| pap.get(i));
|
||||
if run.is_none() && para.is_none() {
|
||||
continue;
|
||||
}
|
||||
let run_font = run.and_then(|r| last_font(r.grpprl));
|
||||
let run_lid = run.and_then(|r| last_lid(r.grpprl));
|
||||
let code_page = text_code_page(fonts, facts, para.map_or(0, |p| p.istd), run_font, run_lid);
|
||||
if code_page == default {
|
||||
continue;
|
||||
}
|
||||
match out.last_mut() {
|
||||
Some(last) if last.hi == lo && last.code_page == code_page => last.hi = hi,
|
||||
_ => out.push(CodePageRun { lo, hi, code_page }),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The font a CHPX or Prc grpprl names (sprm 0x5D), if any: the last one, as a later sprm
|
||||
/// overrides an earlier one when the formatting is applied (a font before an opcode of unknown
|
||||
/// length stands).
|
||||
fn last_font(grpprl: &[u8]) -> Option<u16> {
|
||||
sprms(grpprl).filter(|(op, _)| *op == chp::FONT).filter_map(|(_, v)| Some(u16::from_le_bytes([*v.first()?, *v.get(1)?]))).last()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn a_page_named_by_many_bin_entries_is_read_once() {
|
||||
// A hostile bin table: a million entries all naming page 7, and one naming page 3.
|
||||
let n = 1_000_001usize;
|
||||
let mut plc = vec![0u8; 4 * (n + 1)];
|
||||
for i in 0..n {
|
||||
plc.extend_from_slice(&(if i == 5 { 3u16 } else { 7u16 }).to_le_bytes());
|
||||
}
|
||||
assert_eq!(super::distinct_pages(&plc, n), [3, 7]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bin_entries_past_the_cap_are_not_read() {
|
||||
let n = super::MAX_BINS + 1;
|
||||
let mut plc = vec![0u8; 4 * (n + 1)];
|
||||
for i in 0..n {
|
||||
plc.extend_from_slice(&(if i == n - 1 { 9u16 } else { 7u16 }).to_le_bytes());
|
||||
}
|
||||
assert_eq!(super::distinct_pages(&plc, n), [7]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//! Paths long enough that a per-level recursive descent would exhaust the stack. Parsing is
|
||||
//! iterative — `resolve` and `classify` reach `parse_share` at a fixed depth — so these come
|
||||
//! back; a refusal is fine, a crash is not.
|
||||
|
||||
use craft_docpath::{classify, resolve};
|
||||
|
||||
/// Half a megabyte of components under a UNC share: one component per level for a parser that
|
||||
/// recursed on each separator.
|
||||
#[test]
|
||||
fn a_half_megabyte_unc_path_comes_back() {
|
||||
let mut path = String::from(r"\\?\UNC\host\share");
|
||||
for _ in 0..(1 << 18) {
|
||||
path.push_str(r"\d");
|
||||
}
|
||||
let _ = resolve(&path, None);
|
||||
let _ = classify(&path);
|
||||
}
|
||||
|
||||
/// Sixteen thousand nested verbatim prefixes: each level was a candidate recursive call when the
|
||||
/// UNC prefixes were first reviewed.
|
||||
#[test]
|
||||
fn nested_verbatim_prefixes_come_back() {
|
||||
let mut path = String::new();
|
||||
for _ in 0..(1 << 14) {
|
||||
path.push_str(r"\\?\UNC\");
|
||||
}
|
||||
let _ = resolve(&path, None);
|
||||
let _ = classify(&path);
|
||||
}
|
||||
@@ -6,15 +6,17 @@
|
||||
//! bounded reads and extraction of legacy base64 asset maps. Entry names and MIME type are
|
||||
//! supplied by the format using the bundle.
|
||||
|
||||
use std::io::{Read, Seek};
|
||||
use std::io::{Cursor, Read, Seek, Write};
|
||||
|
||||
use craft_zip::{Archive, Compression, Limits, Writer};
|
||||
use craft_zip::{Archive, Limits};
|
||||
use serde_json::Value;
|
||||
|
||||
const MAX_ENTRIES: u64 = 100_000;
|
||||
const JSON_LIMIT: u64 = 256 << 20;
|
||||
const ASSET_LIMIT: u64 = 1 << 30;
|
||||
const EXPANSION_LIMIT: u64 = 4 << 30;
|
||||
/// Largest proxy preview read from a bundle; larger entries are skipped.
|
||||
pub const PROXY_LIMIT: u64 = 1 << 20;
|
||||
|
||||
/// Per-entry and total-expansion limits for an untrusted document bundle.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -46,6 +48,8 @@ pub struct Spec<'a> {
|
||||
pub preview_entry: &'a str,
|
||||
/// Directory prefix for embedded assets, without a trailing slash.
|
||||
pub assets_directory: &'a str,
|
||||
/// Directory prefix for proxy previews of linked assets, without a trailing slash.
|
||||
pub proxies_directory: &'a str,
|
||||
/// Metadata value written in the `format` field.
|
||||
pub format_name: &'a str,
|
||||
/// Old single-file JSON field containing base64 assets keyed by decimal id.
|
||||
@@ -72,6 +76,8 @@ pub struct Package {
|
||||
pub version: u64,
|
||||
/// Embedded asset bytes keyed by decimal asset id.
|
||||
pub assets: Vec<(String, Vec<u8>)>,
|
||||
/// Proxy preview bytes (JPEG or PNG) keyed by decimal asset id, for ids the document lists.
|
||||
pub proxies: Vec<(String, Vec<u8>)>,
|
||||
}
|
||||
|
||||
/// Why a document bundle could not be read or written.
|
||||
@@ -116,24 +122,104 @@ pub fn starts_with_mimetype(prefix: &[u8], spec: Spec<'_>) -> bool {
|
||||
craft_zip::stored_mimetype(prefix).is_some_and(|mime| mime == spec.mimetype.as_bytes())
|
||||
}
|
||||
|
||||
/// Write a ZIP document bundle containing the serialized document, metadata, preview and assets.
|
||||
pub fn write(document: &[u8], version: u32, generator: &str, preview: Option<&[u8]>, assets: &[Asset<'_>], spec: Spec<'_>) -> Result<Vec<u8>, Error> {
|
||||
let mut writer = Writer::new();
|
||||
writer.add(spec.mimetype_entry, spec.mimetype.as_bytes(), Compression::Stored).map_err(|e| Error::Write(e.to_string()))?;
|
||||
let metadata = serde_json::json!({"format":spec.format_name, "version":version, "generator":generator});
|
||||
let metadata = serde_json::to_vec(&metadata).map_err(|e| Error::Write(e.to_string()))?;
|
||||
writer.add(spec.metadata_entry, &metadata, Compression::Deflated).map_err(|e| Error::Write(e.to_string()))?;
|
||||
if let Some(preview) = preview {
|
||||
writer.add(spec.preview_entry, preview, Compression::Stored).map_err(|e| Error::Write(e.to_string()))?;
|
||||
/// Refuses every write and seek after the first failure of the wrapped writer. Dropping a
|
||||
/// `ZipWriter` after an error finalizes the archive, which must not touch a cancelled target.
|
||||
struct Latch<W> {
|
||||
inner: W,
|
||||
failed: bool,
|
||||
}
|
||||
|
||||
impl<W> Latch<W> {
|
||||
fn latched() -> std::io::Error {
|
||||
std::io::Error::other("writer already failed")
|
||||
}
|
||||
writer.add(spec.document_entry, document, Compression::Deflated).map_err(|e| Error::Write(e.to_string()))?;
|
||||
}
|
||||
|
||||
impl<W: Write> Write for Latch<W> {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
if self.failed {
|
||||
return Err(Self::latched());
|
||||
}
|
||||
self.inner.write(buf).inspect_err(|_| self.failed = true)
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
if self.failed {
|
||||
return Err(Self::latched());
|
||||
}
|
||||
self.inner.flush().inspect_err(|_| self.failed = true)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Seek> Seek for Latch<W> {
|
||||
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
|
||||
if self.failed {
|
||||
return Err(Self::latched());
|
||||
}
|
||||
self.inner.seek(pos).inspect_err(|_| self.failed = true)
|
||||
}
|
||||
}
|
||||
|
||||
fn write_err(e: impl std::fmt::Display) -> Error {
|
||||
Error::Write(e.to_string())
|
||||
}
|
||||
|
||||
/// Write a ZIP document bundle containing the serialized document, metadata, preview, assets and
|
||||
/// proxy previews.
|
||||
pub fn write(
|
||||
document: &[u8],
|
||||
version: u32,
|
||||
generator: &str,
|
||||
preview: Option<&[u8]>,
|
||||
assets: &[Asset<'_>],
|
||||
proxies: &[Asset<'_>],
|
||||
spec: Spec<'_>,
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let mut out = Cursor::new(Vec::new());
|
||||
write_to(&mut out, document, version, generator, preview, assets, proxies, spec)?;
|
||||
Ok(out.into_inner())
|
||||
}
|
||||
|
||||
/// Like [`write`], streaming the archive into `out`. A writer that fails (for example to cancel
|
||||
/// a long save) stops the write at the next member or buffer and the error is returned.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn write_to<W: Write + Seek>(
|
||||
out: W,
|
||||
document: &[u8],
|
||||
version: u32,
|
||||
generator: &str,
|
||||
preview: Option<&[u8]>,
|
||||
assets: &[Asset<'_>],
|
||||
proxies: &[Asset<'_>],
|
||||
spec: Spec<'_>,
|
||||
) -> Result<(), Error> {
|
||||
use zip::CompressionMethod::{Deflated, Stored};
|
||||
let mut zip = zip::ZipWriter::new(Latch { inner: out, failed: false });
|
||||
let mut add = |name: &str, data: &[u8], method: zip::CompressionMethod| -> Result<(), Error> {
|
||||
let options = zip::write::SimpleFileOptions::default()
|
||||
.compression_method(method)
|
||||
.compression_level(if method == Deflated { Some(6) } else { None })
|
||||
.last_modified_time(zip::DateTime::default())
|
||||
.large_file(u64::try_from(data.len()).unwrap_or(u64::MAX) >= u64::from(u32::MAX) / 2);
|
||||
zip.start_file(name, options).map_err(|e| Error::Write(format!("{name}: {e}")))?;
|
||||
zip.write_all(data).map_err(|e| Error::Write(format!("{name}: {e}")))
|
||||
};
|
||||
add(spec.mimetype_entry, spec.mimetype.as_bytes(), Stored)?;
|
||||
let metadata = serde_json::json!({"format":spec.format_name, "version":version, "generator":generator});
|
||||
add(spec.metadata_entry, &serde_json::to_vec(&metadata).map_err(write_err)?, Deflated)?;
|
||||
if let Some(preview) = preview {
|
||||
add(spec.preview_entry, preview, Stored)?;
|
||||
}
|
||||
add(spec.document_entry, document, Deflated)?;
|
||||
for asset in assets.iter().filter(|asset| !asset.data.is_empty()) {
|
||||
let path = format!("{}/{}.{}", spec.assets_directory, asset.id, extension(asset.mime));
|
||||
let compression =
|
||||
if matches!(asset.mime, "image/png" | "image/jpeg" | "image/gif" | "image/webp") { Compression::Stored } else { Compression::Deflated };
|
||||
writer.add(&path, asset.data, compression).map_err(|e| Error::Write(e.to_string()))?;
|
||||
let method = if matches!(asset.mime, "image/png" | "image/jpeg" | "image/gif" | "image/webp") { Stored } else { Deflated };
|
||||
add(&path, asset.data, method)?;
|
||||
}
|
||||
writer.finish().map_err(|e| Error::Write(e.to_string()))
|
||||
for proxy in proxies.iter().filter(|proxy| !proxy.data.is_empty()) {
|
||||
let ext = if proxy.mime == "image/jpeg" { "jpg" } else { "png" };
|
||||
add(&format!("{}/{}.{ext}", spec.proxies_directory, proxy.id), proxy.data, Stored)?;
|
||||
}
|
||||
zip.finish().map(drop).map_err(write_err)
|
||||
}
|
||||
|
||||
/// Read a ZIP bundle or legacy JSON document within bounded member sizes.
|
||||
@@ -186,7 +272,40 @@ pub fn read_with_limits(bytes: &[u8], supported_version: u32, spec: Spec<'_>, li
|
||||
embedded.push((id.to_owned(), data));
|
||||
}
|
||||
}
|
||||
Ok(Package { document, version, assets: embedded })
|
||||
let proxies = read_proxies(&mut archive, &wanted, spec);
|
||||
Ok(Package { document, version, assets: embedded, proxies })
|
||||
}
|
||||
|
||||
/// Proxy previews for known asset ids. Only `<proxies>/<id>.jpg|png` is accepted; entries over
|
||||
/// [`PROXY_LIMIT`] or that fail to read are skipped.
|
||||
fn read_proxies(archive: &mut Archive<'_>, wanted: &std::collections::HashSet<String>, spec: Spec<'_>) -> Vec<(String, Vec<u8>)> {
|
||||
let prefix = format!("{}/", spec.proxies_directory);
|
||||
let mut proxies = Vec::new();
|
||||
for path in archive.names().map(|name| name.into_owned()).collect::<Vec<_>>() {
|
||||
let Some(member) = path.strip_prefix(&prefix) else { continue };
|
||||
let Some((id, ext)) = member.split_once('.') else { continue };
|
||||
if !matches!(ext, "jpg" | "png") || !wanted.contains(id) || id.parse::<u64>().is_err() {
|
||||
continue;
|
||||
}
|
||||
if archive.declared_size(&path).is_none_or(|size| size > PROXY_LIMIT) {
|
||||
continue;
|
||||
}
|
||||
let Some(size) = archive.declared_size(&path) else { continue };
|
||||
if archive.budget().remaining() < size {
|
||||
break;
|
||||
}
|
||||
match archive.read(&path, PROXY_LIMIT) {
|
||||
Ok(Some(data)) => proxies.push((id.to_owned(), data)),
|
||||
Ok(None) => {}
|
||||
// A failed read may have decompressed up to `size` bytes without being charged.
|
||||
Err(_) => {
|
||||
if archive.budget_mut().charge(size).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
proxies
|
||||
}
|
||||
|
||||
/// Read a preview with a fixed maximum size without decompressing the full bundle.
|
||||
@@ -235,7 +354,7 @@ fn read_legacy(bytes: &[u8], spec: Spec<'_>) -> Result<Package, Error> {
|
||||
object.remove(field);
|
||||
}
|
||||
}
|
||||
Ok(Package { document, version: 1, assets })
|
||||
Ok(Package { document, version: 1, assets, proxies: Vec::new() })
|
||||
}
|
||||
|
||||
fn decode_base64(input: &str) -> Vec<u8> {
|
||||
@@ -271,6 +390,7 @@ mod tests {
|
||||
document_entry: "document.json",
|
||||
preview_entry: "preview.png",
|
||||
assets_directory: "assets",
|
||||
proxies_directory: "proxies",
|
||||
format_name: "test",
|
||||
legacy_assets_field: Some("assetData"),
|
||||
};
|
||||
@@ -278,8 +398,8 @@ mod tests {
|
||||
#[test]
|
||||
fn writes_and_reads_bundle_members() {
|
||||
let payload = [1u8, 2, 3];
|
||||
let bytes =
|
||||
write(br#"{"assets":{"7":{}}}"#, 4, "Test 0.1", Some(b"png"), &[Asset { id: "7", mime: "image/png", data: &payload }], SPEC).unwrap();
|
||||
let bytes = write(br#"{"assets":{"7":{}}}"#, 4, "Test 0.1", Some(b"png"), &[Asset { id: "7", mime: "image/png", data: &payload }], &[], SPEC)
|
||||
.unwrap();
|
||||
assert!(has_document(&bytes, SPEC));
|
||||
assert!(starts_with_mimetype(&bytes, SPEC));
|
||||
let loaded = read(&bytes, 4, SPEC).unwrap();
|
||||
@@ -298,7 +418,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn refuses_newer_versions() {
|
||||
let bytes = write(b"{}", 9, "future", None, &[], SPEC).unwrap();
|
||||
let bytes = write(b"{}", 9, "future", None, &[], &[], SPEC).unwrap();
|
||||
assert_eq!(read(&bytes, 4, SPEC), Err(Error::TooNew(9)));
|
||||
}
|
||||
|
||||
@@ -307,11 +427,31 @@ mod tests {
|
||||
let bytes = vec![7u8; 128];
|
||||
let document = br#"{"assets":{"7":{}}}"#;
|
||||
let limits = ReadLimits { json_bytes: 1 << 20, compressed_asset_bytes: 8, expansion_bytes: 1 << 20 };
|
||||
let stored = write(document, 4, "test", None, &[Asset { id: "7", mime: "image/png", data: &bytes }], SPEC).unwrap();
|
||||
let stored = write(document, 4, "test", None, &[Asset { id: "7", mime: "image/png", data: &bytes }], &[], SPEC).unwrap();
|
||||
let package = read_with_limits(&stored, 4, SPEC, limits).unwrap();
|
||||
assert_eq!(package.assets, [("7".to_owned(), bytes.clone())]);
|
||||
|
||||
let compressed = write(document, 4, "test", None, &[Asset { id: "7", mime: "application/octet-stream", data: &bytes }], SPEC).unwrap();
|
||||
let compressed = write(document, 4, "test", None, &[Asset { id: "7", mime: "application/octet-stream", data: &bytes }], &[], SPEC).unwrap();
|
||||
assert!(read_with_limits(&compressed, 4, SPEC, limits).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_proxy_reads_are_charged_and_stop_reading_when_the_budget_is_spent() {
|
||||
let pad = " ".repeat(40_000);
|
||||
let document = format!(r#"{{"assets":{{"1":{{}},"2":{{}},"3":{{}},"4":{{}}}},"pad":"{pad}"}}"#);
|
||||
let blobs: Vec<Vec<u8>> = (1u8..=4).map(|n| vec![n.wrapping_mul(50); 1000]).collect();
|
||||
let ids = ["1", "2", "3", "4"];
|
||||
let proxies: Vec<Asset<'_>> = ids.iter().zip(&blobs).map(|(id, data)| Asset { id, mime: "image/png", data }).collect();
|
||||
let mut bytes = write(document.as_bytes(), 4, "t", None, &[], &proxies, SPEC).unwrap();
|
||||
// Corrupt proxies 1 to 3 so their reads fail their checksum.
|
||||
for blob in &blobs[..3] {
|
||||
let at = bytes.windows(blob.len()).position(|w| w == blob.as_slice()).unwrap();
|
||||
bytes[at + 500] ^= 0xFF;
|
||||
}
|
||||
let source = bytes.len() as u64;
|
||||
let all = read(&bytes, 4, SPEC).unwrap();
|
||||
assert_eq!(all.proxies.len(), 1);
|
||||
let tight = ReadLimits { json_bytes: 1 << 20, compressed_asset_bytes: 1 << 20, expansion_bytes: 40_100 + 2_500 - source };
|
||||
assert!(read_with_limits(&bytes, 4, SPEC, tight).unwrap().proxies.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,10 @@ pub(crate) struct Reader<'p> {
|
||||
pub theme_fills: shape_style::ThemeFills,
|
||||
/// The fill of the group being read (what its members' `a:grpFill` stands for).
|
||||
pub group_fill: Option<craft_wordmodel::fill::Fill>,
|
||||
/// The package has no default table style (no styles part, or none marked `w:default`):
|
||||
/// Word then gives tables no cell margins rather than its Normal Table's 0.08". Set by
|
||||
/// `read_styles`, which runs before any story with tables is read.
|
||||
pub no_default_table_style: bool,
|
||||
}
|
||||
|
||||
/// The package path that internal relationship `id` points at, if it has type `kind` (an `rt`
|
||||
@@ -193,6 +197,7 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize,
|
||||
theme_fills: Default::default(),
|
||||
group_fill: None,
|
||||
chunks,
|
||||
no_default_table_style: true,
|
||||
};
|
||||
r.doc.missing_pictures.main = main.clone();
|
||||
r.pc.major_font = r.doc.settings.major_font.clone();
|
||||
@@ -210,6 +215,8 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize,
|
||||
let fallback = match t {
|
||||
rt::STYLES => "word/styles.xml",
|
||||
rt::NUMBERING => "word/numbering.xml",
|
||||
rt::FOOTNOTES => "word/footnotes.xml",
|
||||
rt::ENDNOTES => "word/endnotes.xml",
|
||||
_ => return None,
|
||||
};
|
||||
pkg.get(fallback).map(|_| fallback.to_string())
|
||||
|
||||
@@ -389,15 +389,22 @@ impl PropCtx {
|
||||
}
|
||||
"w:numPr" => {
|
||||
let num = k.child_val("w:numId").and_then(u32_of);
|
||||
let ilvl = k.child_val("w:ilvl").and_then(u32_of);
|
||||
let level = ilvl.unwrap_or(0).min(8) as u8;
|
||||
match (num, ilvl) {
|
||||
(Some(num), _) => p.numbering = Some(NumRef { num, level }),
|
||||
// A level without a list: the list comes from the based-on style, as in
|
||||
// a Heading 3 that only says "level 3" of Heading 2's list. Resolved once
|
||||
// the styles are read (`resolve_list_levels`).
|
||||
(None, Some(_)) => p.numbering = Some(NumRef { num: LEVEL_ONLY, level }),
|
||||
(None, None) => {}
|
||||
let ilvl_node = k.child("w:ilvl");
|
||||
let ilvl =
|
||||
ilvl_node.and_then(|node| node.attr("w:val")).and_then(int).filter(|level| (0..=8).contains(level)).map(|level| level as u32);
|
||||
// A missing level means level zero. A present but malformed/out-of-range
|
||||
// level invalidates this numbering reference instead of silently becoming
|
||||
// level zero; the latter can turn hostile input into a real list.
|
||||
if ilvl_node.is_none() || ilvl.is_some() {
|
||||
let level = ilvl.unwrap_or(0) as u8;
|
||||
match (num, ilvl) {
|
||||
(Some(num), _) => p.numbering = Some(NumRef { num, level }),
|
||||
// A level without a list: the list comes from the based-on style, as in
|
||||
// a Heading 3 that only says "level 3" of Heading 2's list. Resolved once
|
||||
// the styles are read (`resolve_list_levels`).
|
||||
(None, Some(_)) => p.numbering = Some(NumRef { num: LEVEL_ONLY, level }),
|
||||
(None, None) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
"w:pBdr" => p.borders = Some(self.borders(k)),
|
||||
@@ -817,20 +824,21 @@ pub fn sectpr(pc: &PropCtx, e: &El) -> (SectionProps, Vec<HfRef>) {
|
||||
s.footnote_columns = k.attr("w:val").and_then(u32_of).filter(|v| (1..=45).contains(v));
|
||||
}
|
||||
"w:lnNumType" => {
|
||||
let mut l = LineNumbering::default();
|
||||
if let Some(v) = k.attr("w:countBy").and_then(u32_of) {
|
||||
l.count_by = v.clamp(1, 100);
|
||||
// Word leaves line numbering off unless `w:countBy` gives an interval above 0,
|
||||
// whatever `w:start`, `w:distance` and `w:restart` say (#6209, observed in Word).
|
||||
if let Some(count_by) = k.attr("w:countBy").and_then(u32_of).filter(|&v| v > 0) {
|
||||
let mut l = LineNumbering { count_by: count_by.clamp(1, 100), ..LineNumbering::default() };
|
||||
if let Some(v) = k.attr("w:start").and_then(int) {
|
||||
l.start = (v.clamp(0, 32_767) + 1) as u32;
|
||||
}
|
||||
l.distance = tw(k, "w:distance").unwrap_or(0.0).max(0.0);
|
||||
l.restart = match k.attr("w:restart") {
|
||||
Some("newSection") => LineNumberRestart::Section,
|
||||
Some("continuous") => LineNumberRestart::Continuous,
|
||||
_ => LineNumberRestart::Page,
|
||||
};
|
||||
s.line_numbers = Some(l);
|
||||
}
|
||||
if let Some(v) = k.attr("w:start").and_then(int) {
|
||||
l.start = (v.clamp(0, 32_767) + 1) as u32;
|
||||
}
|
||||
l.distance = tw(k, "w:distance").unwrap_or(0.0).max(0.0);
|
||||
l.restart = match k.attr("w:restart") {
|
||||
Some("newSection") => LineNumberRestart::Section,
|
||||
Some("continuous") => LineNumberRestart::Continuous,
|
||||
_ => LineNumberRestart::Page,
|
||||
};
|
||||
s.line_numbers = Some(l);
|
||||
}
|
||||
"w:vAlign" => {
|
||||
s.valign = match k.attr("w:val") {
|
||||
|
||||
@@ -113,6 +113,8 @@ pub struct StoryCtx {
|
||||
pub story_depth: usize,
|
||||
/// Reading a header or footer (not a text box inside one): where Word keeps its watermark.
|
||||
pub in_hf: bool,
|
||||
/// Reading a table cell's paragraphs (not a text box inside one).
|
||||
in_cell: bool,
|
||||
}
|
||||
|
||||
/// Paragraph content under construction.
|
||||
|
||||
@@ -276,6 +276,10 @@ impl Reader<'_> {
|
||||
if sc.fields.iter().any(|f| f.phase == Phase::Instr) && para.plain_text().is_empty() {
|
||||
para.mark.hidden = Some(true);
|
||||
}
|
||||
// A cell's paragraphs are marked once their table is read: its style counts.
|
||||
if !sc.in_cell {
|
||||
self.doc.styles.mark_runs_as_word_draws_them(&mut para, None);
|
||||
}
|
||||
para.normalize();
|
||||
para
|
||||
}
|
||||
@@ -314,15 +318,32 @@ impl Reader<'_> {
|
||||
|
||||
// ---- tables ----
|
||||
|
||||
/// Word gives a table no cell margins when the file has no default table style and neither the
|
||||
/// table nor its style sets them; saving may write a default Normal Table, so the zeros are
|
||||
/// kept as the table's own. shortcut: they stay direct formatting (a table style applied later
|
||||
/// keeps them), until the model knows the file's default table style. A `w:tblCellMar` that sets
|
||||
/// only some sides keeps them: only the sides it omits become 0.
|
||||
fn no_default_margins(&self, props: &mut craft_wordmodel::props::TableProps) {
|
||||
if self.no_default_table_style
|
||||
&& props.cell_margins.is_none()
|
||||
&& props.style.as_deref().and_then(|id| self.doc.styles.table_style(id)).is_none_or(|s| s.parts.cell_margins.is_none())
|
||||
{
|
||||
props.cell_margins = Some([0.0; 4]);
|
||||
}
|
||||
}
|
||||
|
||||
fn read_table(&mut self, sc: &mut StoryCtx, t: &El, rels: &Rels, depth: usize) -> Option<Table> {
|
||||
let mut table = Table::default();
|
||||
if let Some(p) = t.child("w:tblPr") {
|
||||
table.props = self.pc.tblpr(p);
|
||||
if let Some(ch) = p.child("w:tblPrChange") {
|
||||
let old = ch.child("w:tblPr").map(|o| self.pc.tblpr(o)).unwrap_or_default();
|
||||
let mut old = ch.child("w:tblPr").map(|o| self.pc.tblpr(o)).unwrap_or_default();
|
||||
// Rejecting the change brings back these properties: with the same margins.
|
||||
self.no_default_margins(&mut old);
|
||||
table.props.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, ch), old);
|
||||
}
|
||||
}
|
||||
self.no_default_margins(&mut table.props);
|
||||
if let Some(g) = t.child("w:tblGrid") {
|
||||
table.grid = g
|
||||
.children("w:gridCol")
|
||||
@@ -355,8 +376,10 @@ impl Reader<'_> {
|
||||
continue;
|
||||
}
|
||||
let mut blocks = Blocks::new();
|
||||
let outer = std::mem::replace(&mut sc.in_cell, true);
|
||||
self.read_blocks(sc, tc, rels, &mut blocks, depth + 4);
|
||||
self.flush_pending(sc, &mut blocks);
|
||||
sc.in_cell = outer;
|
||||
row.cells.push(Cell { props, blocks, controls: cell_controls });
|
||||
}
|
||||
if !row.cells.is_empty() {
|
||||
@@ -370,6 +393,20 @@ impl Reader<'_> {
|
||||
let cols = table.cols().clamp(1, MAX_COLS);
|
||||
table.grid = vec![72.0; cols];
|
||||
}
|
||||
// The cells' paragraphs, with the run formatting the table style gives each cell (a
|
||||
// table inside has marked its own).
|
||||
let chrs = self.doc.styles.table_cells_chr(&table);
|
||||
for (ri, row) in table.rows.iter_mut().enumerate() {
|
||||
for (ci, cell) in row.cells.iter_mut().enumerate() {
|
||||
let chr = chrs.as_ref().and_then(|c| c.get(ri)?.get(ci));
|
||||
for b in &mut cell.blocks {
|
||||
if let Block::Para(p) = Arc::make_mut(b) {
|
||||
self.doc.styles.mark_runs_as_word_draws_them(p, chr);
|
||||
p.normalize();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(table)
|
||||
}
|
||||
}
|
||||
+103
-15
@@ -1,10 +1,22 @@
|
||||
use super::{
|
||||
AbstractNum, DocxError, El, Level, LevelSuffix, Num, NumFormat, PartFormat, Reader, Style, StyleKind, StyleSheet, TablePart, TableStyleParts,
|
||||
VAlign, flag, int, props, tw, u32_of,
|
||||
AbstractNum, DocxError, El, Level, LevelSuffix, Num, NumFormat, PartFormat, Reader, RevisionKind, Style, StyleKind, StyleSheet, TablePart,
|
||||
TableStyleParts, VAlign, flag, int, props, tw, u32_of,
|
||||
};
|
||||
use crate::units::tw_i16;
|
||||
use craft_wordmodel::props::PropChange;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn numbering_level_index(value: Option<&str>) -> Option<usize> {
|
||||
match value {
|
||||
None => Some(0),
|
||||
Some(value) => int(value).filter(|level| (0..=8).contains(level)).map(|level| level as usize),
|
||||
}
|
||||
}
|
||||
|
||||
fn style_is_default(style: &El) -> bool {
|
||||
style.attr("w:default").is_some_and(|value| !matches!(value.trim(), "0" | "false" | "off" | "False" | "FALSE"))
|
||||
}
|
||||
|
||||
/// Built-in style names Word stores in lower case, and how the UI shows them.
|
||||
pub(crate) const LOWER_NAMES: &[(&str, &str)] = &[
|
||||
("caption", "Caption"),
|
||||
@@ -52,6 +64,20 @@ fn builtin_heading_level(name: &str) -> Option<u8> {
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
/// Whether a style's kept (last) definition is a default table style.
|
||||
fn has_default_table_style(&self, root: &El) -> bool {
|
||||
let mut defaults: Vec<String> = Vec::new();
|
||||
for s in root.children("w:style").take(10_000) {
|
||||
let Some(raw_id) = s.attr("w:styleId").filter(|i| !i.is_empty()) else { continue };
|
||||
let id = self.pc.style_id(raw_id);
|
||||
defaults.retain(|d| *d != id);
|
||||
if s.attr("w:type") == Some("table") && style_is_default(s) {
|
||||
defaults.push(id);
|
||||
}
|
||||
}
|
||||
!defaults.is_empty()
|
||||
}
|
||||
|
||||
pub(super) fn read_styles(&mut self, path: &str) -> Result<(), DocxError> {
|
||||
/// Largest `w:latentStyles` kept (Word's has ~380 exceptions, ~40 KB).
|
||||
const MAX_LATENT_STYLES_XML: usize = 1 << 20;
|
||||
@@ -59,9 +85,7 @@ impl Reader<'_> {
|
||||
// The default paragraph style must be called `Normal` in our model.
|
||||
let has_normal = root.children("w:style").any(|s| s.attr("w:styleId") == Some("Normal"));
|
||||
if !has_normal
|
||||
&& let Some(def) = root
|
||||
.children("w:style")
|
||||
.find(|s| s.attr("w:type") == Some("paragraph") && s.attr("w:default").is_some_and(|v| v == "1" || v == "true"))
|
||||
&& let Some(def) = root.children("w:style").find(|s| s.attr("w:type") == Some("paragraph") && style_is_default(s))
|
||||
&& let Some(id) = def.attr("w:styleId")
|
||||
{
|
||||
self.pc.alias.insert(id.to_string(), "Normal".into());
|
||||
@@ -85,6 +109,7 @@ impl Reader<'_> {
|
||||
}
|
||||
}
|
||||
let mut sheet = StyleSheet::empty();
|
||||
let mut normal_is_default = None;
|
||||
sheet.default_para_present = root.child("w:docDefaults").and_then(|dd| dd.child("w:pPrDefault")).is_some();
|
||||
if let Some(dd) = root.child("w:docDefaults") {
|
||||
if let Some(r) = dd.child("w:rPrDefault").and_then(|d| d.child("w:rPr")) {
|
||||
@@ -112,6 +137,13 @@ impl Reader<'_> {
|
||||
"numbering" => StyleKind::Numbering,
|
||||
_ => StyleKind::Paragraph,
|
||||
};
|
||||
if kind == StyleKind::Paragraph && id == "Normal" {
|
||||
// Word treats the built-in Normal style as the paragraph default when the file
|
||||
// doesn't nominate another style. Keep that fact for an explicitly declared
|
||||
// Normal; the gallery later synthesizes a visually identical Normal when the
|
||||
// file did not declare one, and that synthetic style is not source provenance.
|
||||
normal_is_default = Some(s.attr("w:default").is_none());
|
||||
}
|
||||
let name = s.child_val("w:name").map(ui_style_name).unwrap_or_else(|| id.clone());
|
||||
let r = |n: &str| s.child_val(n).filter(|v| !v.is_empty()).map(|v| self.pc.style_id(v));
|
||||
let mut st = Style {
|
||||
@@ -127,7 +159,7 @@ impl Reader<'_> {
|
||||
builtin: !s.attr("w:customStyle").is_some_and(|v| matches!(v.trim(), "1" | "true" | "on")),
|
||||
locked: flag(s, "w:locked").unwrap_or(false),
|
||||
// Only where the writer couldn't tell from the id (it flags the English ids).
|
||||
default: s.attr("w:default").is_some_and(|v| v == "1" || v == "true")
|
||||
default: style_is_default(s)
|
||||
&& !matches!(
|
||||
(kind, id.as_str()),
|
||||
(StyleKind::Paragraph, "Normal")
|
||||
@@ -137,26 +169,50 @@ impl Reader<'_> {
|
||||
),
|
||||
..Default::default()
|
||||
};
|
||||
let mut old_para = None;
|
||||
let mut old_chr = None;
|
||||
if let Some(p) = s.child("w:pPr") {
|
||||
st.para = self.pc.ppr(p).0;
|
||||
if let Some(ch) = p.child("w:pPrChange") {
|
||||
let old = ch.child("w:pPr").map(|o| self.pc.ppr(o).0.formatting()).unwrap_or_default();
|
||||
old_para = Some((self.revision(RevisionKind::Format, ch), old));
|
||||
}
|
||||
}
|
||||
// Word gives its built-in heading styles their outline level whether or not the file
|
||||
// writes `w:outlineLvl` (some producers leave it out): "heading 2" is level 2 in the
|
||||
// Navigation pane and the table of contents, whatever its id (#4696).
|
||||
if kind == StyleKind::Paragraph
|
||||
&& st.builtin
|
||||
&& st.para.outline_level.is_none()
|
||||
&& let Some(level) = s.child_val("w:name").and_then(builtin_heading_level)
|
||||
{
|
||||
st.para.outline_level = Some(level);
|
||||
if st.para.outline_level.is_none() {
|
||||
st.para.outline_level = Some(level);
|
||||
}
|
||||
if let Some((_, old)) = &mut old_para
|
||||
&& old.outline_level.is_none()
|
||||
{
|
||||
old.outline_level = Some(level);
|
||||
}
|
||||
}
|
||||
if let Some(rp) = s.child("w:rPr") {
|
||||
st.chr = self.pc.rpr(rp);
|
||||
if let Some(ch) = rp.child("w:rPrChange") {
|
||||
let old = ch.child("w:rPr").map(|o| self.pc.rpr(o).formatting()).unwrap_or_default();
|
||||
old_chr = Some((self.revision(RevisionKind::Format, ch), old));
|
||||
}
|
||||
}
|
||||
if kind == StyleKind::Table {
|
||||
let parts = self.table_style_parts(s, &mut st);
|
||||
st.table = (parts != TableStyleParts::default()).then_some(parts);
|
||||
}
|
||||
// CT_Style has no `stylePrChange`. Keep its two valid revision carriers separate: a
|
||||
// foreign document may attribute paragraph and run changes to different authors.
|
||||
if let Some((rev, old)) = old_para {
|
||||
st.para_change = PropChange::boxed(rev, old);
|
||||
}
|
||||
if let Some((rev, old)) = old_chr {
|
||||
st.chr_change = PropChange::boxed(rev, old);
|
||||
}
|
||||
// A style defined twice (some generators write their own Heading 1 after a
|
||||
// placeholder one) is the last definition, whole: Word drops the earlier one rather
|
||||
// than merging the two (observed with synthetic documents).
|
||||
@@ -165,6 +221,7 @@ impl Reader<'_> {
|
||||
None => sheet.styles.push(st),
|
||||
}
|
||||
}
|
||||
self.no_default_table_style = !self.has_default_table_style(&root);
|
||||
// Styles that give only a list level take the list from the style they are based on
|
||||
// (Heading 3: level 3 of Heading 2's outline list).
|
||||
let fixed: Vec<Option<craft_wordmodel::props::NumRef>> = sheet
|
||||
@@ -181,6 +238,30 @@ impl Reader<'_> {
|
||||
for (s, n) in sheet.styles.iter_mut().zip(fixed) {
|
||||
s.para.numbering = n;
|
||||
}
|
||||
if !sheet.styles.iter().any(|style| style.kind == StyleKind::Paragraph && style.default)
|
||||
&& normal_is_default == Some(true)
|
||||
&& let Some(normal) = sheet.styles.iter_mut().find(|style| style.kind == StyleKind::Paragraph && style.id == "Normal")
|
||||
{
|
||||
normal.default = true;
|
||||
}
|
||||
let old_fixed: Vec<Option<Option<craft_wordmodel::props::NumRef>>> = sheet
|
||||
.styles
|
||||
.iter()
|
||||
.map(|s| {
|
||||
s.para_change.as_ref().map(|ch| match ch.old.numbering {
|
||||
Some(n) if n.num == props::LEVEL_ONLY => {
|
||||
let base = s.based_on.as_deref().and_then(|b| props::chain_list(&sheet, b));
|
||||
base.map(|num| craft_wordmodel::props::NumRef { num, level: n.level })
|
||||
}
|
||||
other => other,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
for (s, n) in sheet.styles.iter_mut().zip(old_fixed) {
|
||||
if let (Some(ch), Some(n)) = (&mut s.para_change, n) {
|
||||
ch.old.numbering = n;
|
||||
}
|
||||
}
|
||||
if sheet.default_para.numbering.is_some_and(|n| n.num == props::LEVEL_ONLY) {
|
||||
sheet.default_para.numbering = None;
|
||||
}
|
||||
@@ -250,11 +331,11 @@ impl Reader<'_> {
|
||||
for a in root.children("w:abstractNum").take(10_000) {
|
||||
let Some(id) = a.attr("w:abstractNumId").and_then(u32_of) else { continue };
|
||||
let mut levels: Vec<Level> = (0..9).map(default_level).collect();
|
||||
let mut explicit_levels = [false; 9];
|
||||
for l in a.children("w:lvl") {
|
||||
let i = l.attr("w:ilvl").and_then(u32_of).unwrap_or(0) as usize;
|
||||
if let Some(slot) = levels.get_mut(i) {
|
||||
*slot = self.level(l, i);
|
||||
}
|
||||
let Some(i) = numbering_level_index(l.attr("w:ilvl")) else { continue };
|
||||
levels[i] = self.level(l, i);
|
||||
explicit_levels[i] = true;
|
||||
}
|
||||
if let Some(link) = a.child_val("w:numStyleLink") {
|
||||
links.push((id, link.to_string()));
|
||||
@@ -264,6 +345,7 @@ impl Reader<'_> {
|
||||
id,
|
||||
name: a.child_val("w:name").map(str::to_string),
|
||||
levels,
|
||||
explicit_levels: Some(explicit_levels),
|
||||
nsid: a.child_val("w:nsid").filter(|v| hex(v)).map(str::to_ascii_uppercase),
|
||||
tmpl: a.child_val("w:tmpl").filter(|v| hex(v)).map(str::to_ascii_uppercase),
|
||||
multi_level_type: a
|
||||
@@ -278,7 +360,9 @@ impl Reader<'_> {
|
||||
let (Some(id), Some(abs)) = (n.attr("w:numId").and_then(u32_of), n.child_val("w:abstractNumId").and_then(u32_of)) else { continue };
|
||||
let (mut start_overrides, mut level_overrides) = (Vec::new(), Vec::new());
|
||||
for o in n.children("w:lvlOverride").take(9) {
|
||||
let lvl = o.attr("w:ilvl").and_then(u32_of).unwrap_or(0).min(8) as u8;
|
||||
let Some(raw_level) = o.attr("w:ilvl") else { continue };
|
||||
let Some(lvl) = numbering_level_index(Some(raw_level)) else { continue };
|
||||
let lvl = lvl as u8;
|
||||
if let Some(s) = o.child_val("w:startOverride").and_then(u32_of) {
|
||||
start_overrides.push((lvl, s));
|
||||
} else if let Some(s) = o.child("w:lvl").and_then(|l| l.child_val("w:start")).and_then(u32_of) {
|
||||
@@ -295,9 +379,13 @@ impl Reader<'_> {
|
||||
for (aid, style) in links {
|
||||
let num = self.doc.styles.get(&self.pc.style_id(&style)).and_then(|s| s.para.numbering).map(|n| n.num);
|
||||
let src = num.and_then(|n| nums.iter().find(|x: &&Num| x.id == n)).map(|x| x.abstract_id);
|
||||
let levels = src.filter(|s| *s != aid).and_then(|s| abstracts.iter().find(|a: &&AbstractNum| a.id == s)).map(|a| a.levels.clone());
|
||||
if let (Some(levels), Some(a)) = (levels, abstracts.iter_mut().find(|a| a.id == aid)) {
|
||||
let source = src
|
||||
.filter(|s| *s != aid)
|
||||
.and_then(|s| abstracts.iter().find(|a: &&AbstractNum| a.id == s))
|
||||
.map(|a| (a.levels.clone(), a.explicit_levels));
|
||||
if let (Some((levels, explicit_levels)), Some(a)) = (source, abstracts.iter_mut().find(|a| a.id == aid)) {
|
||||
a.levels = levels;
|
||||
a.explicit_levels = explicit_levels;
|
||||
}
|
||||
}
|
||||
Arc::make_mut(&mut self.doc.numbering).abstracts = abstracts;
|
||||
|
||||
@@ -56,8 +56,12 @@ pub fn measure(s: &str, per_pt: f32) -> Option<f32> {
|
||||
"emu" => v / 12_700.0,
|
||||
_ => return None,
|
||||
};
|
||||
let pt = pt as f32;
|
||||
pt.is_finite().then_some(pt.clamp(-MAX_LEN_PT, MAX_LEN_PT))
|
||||
// Clamp while still in f64: a large but finite OOXML measurement can overflow f32 before
|
||||
// the output limit is applied, even though its bounded point value is representable.
|
||||
if pt.is_nan() {
|
||||
return None;
|
||||
}
|
||||
Some(pt.clamp(-f64::from(MAX_LEN_PT), f64::from(MAX_LEN_PT)) as f32)
|
||||
}
|
||||
|
||||
/// `w:line` under `lineRule="auto"`: the multiple of single line spacing, in 240ths (240 =
|
||||
@@ -196,6 +200,8 @@ mod tests {
|
||||
assert_eq!(measure("nan", 20.0), None);
|
||||
assert_eq!(measure("1e400", 20.0), None);
|
||||
assert_eq!(measure("99999999999999", 20.0), Some(MAX_LEN_PT));
|
||||
assert_eq!(measure("1e300pt", 20.0), Some(MAX_LEN_PT));
|
||||
assert_eq!(measure("-1e300pt", 20.0), Some(-MAX_LEN_PT));
|
||||
assert_eq!(measure("x", 20.0), None);
|
||||
// `w:line` auto: the multiple in 240ths without a unit, through twips with one (#2329).
|
||||
assert_eq!(line_multiple("240"), Some(1.0));
|
||||
|
||||
@@ -87,7 +87,7 @@ impl Writer<'_> {
|
||||
}
|
||||
rels.add(rt::GLOSSARY, &file(part), false);
|
||||
push_part(entries, overrides, part, w.into_bytes(), GLOSSARY_CONTENT_TYPE, prels);
|
||||
push_part(entries, overrides, styles, styles_xml(doc), &format!("{CT_WML}styles+xml"), PartRels::default());
|
||||
push_part(entries, overrides, styles, styles_xml(self), &format!("{CT_WML}styles+xml"), PartRels::default());
|
||||
if lists {
|
||||
push_part(entries, overrides, numbering, numbering_xml(doc), &format!("{CT_WML}numbering+xml"), PartRels::default());
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result<Vec<(String,
|
||||
write_comments(doc, &comment_paras, &ns_refs, &mut wr, &mut entries, &mut overrides, &mut rels);
|
||||
|
||||
rels.add(rt::STYLES, "styles.xml", false);
|
||||
push_part(&mut entries, &mut overrides, "word/styles.xml", styles_xml(doc), &format!("{CT_WML}styles+xml"), PartRels::default());
|
||||
push_part(&mut entries, &mut overrides, "word/styles.xml", styles_xml(&mut wr), &format!("{CT_WML}styles+xml"), PartRels::default());
|
||||
if !doc.numbering.nums.is_empty() || !doc.numbering.abstracts.is_empty() {
|
||||
rels.add(rt::NUMBERING, "numbering.xml", false);
|
||||
push_part(&mut entries, &mut overrides, "word/numbering.xml", numbering_xml(doc), &format!("{CT_WML}numbering+xml"), PartRels::default());
|
||||
|
||||
@@ -294,7 +294,7 @@ impl Writer<'_> {
|
||||
}
|
||||
|
||||
/// `w:id`, `w:author` and `w:date` of a tracked change by revision `idx`.
|
||||
pub(super) fn change_attrs(&mut self, idx: u32) -> ChangeAttrs {
|
||||
pub(crate) fn change_attrs(&mut self, idx: u32) -> ChangeAttrs {
|
||||
let (id, author, date) = self.rev_attrs(idx);
|
||||
let mut a = vec![("w:id", id), ("w:author", author)];
|
||||
if !date.is_empty() {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use super::{Document, Level, LevelSuffix, ParaProps, Style, StyleKind, TablePart, VAlign, W, props, xml};
|
||||
use super::{Level, LevelSuffix, ParaProps, Style, StyleKind, TablePart, VAlign, W, Writer, props, xml};
|
||||
use crate::write::props::open_change;
|
||||
use craft_wordmodel::Document;
|
||||
|
||||
/// Built-in names Word keeps in lower case in styles.xml.
|
||||
fn file_style_name(name: &str) -> String {
|
||||
@@ -16,7 +18,8 @@ fn file_style_name(name: &str) -> String {
|
||||
name.to_string()
|
||||
}
|
||||
|
||||
pub(super) fn styles_xml(doc: &Document) -> Vec<u8> {
|
||||
pub(super) fn styles_xml(wr: &mut Writer<'_>) -> Vec<u8> {
|
||||
let doc = wr.doc;
|
||||
let s = &doc.styles;
|
||||
let mut w = W::new();
|
||||
let ns = xml::body_ns();
|
||||
@@ -50,13 +53,15 @@ pub(super) fn styles_xml(doc: &Document) -> Vec<u8> {
|
||||
// Kinds whose default style the file named (`w:default`); the English ids stand in otherwise.
|
||||
let flagged: Vec<StyleKind> = s.styles.iter().filter(|st| st.default).map(|st| st.kind).collect();
|
||||
for st in &s.styles {
|
||||
style_xml(&mut w, st, &flagged);
|
||||
style_xml(wr, &mut w, st, &flagged);
|
||||
}
|
||||
w.close("w:styles");
|
||||
w.into_bytes()
|
||||
}
|
||||
|
||||
fn style_xml(w: &mut W, st: &Style, flagged: &[StyleKind]) {
|
||||
/// One `w:style` element. A tracked change of its paragraph/run properties is nested in the
|
||||
/// corresponding property element as `w:pPrChange` / `w:rPrChange`.
|
||||
fn style_xml(wr: &mut Writer<'_>, w: &mut W, st: &Style, flagged: &[StyleKind]) {
|
||||
let ty = match st.kind {
|
||||
StyleKind::Paragraph => "paragraph",
|
||||
StyleKind::Character => "character",
|
||||
@@ -70,9 +75,14 @@ fn style_xml(w: &mut W, st: &Style, flagged: &[StyleKind]) {
|
||||
| (StyleKind::Table, "TableNormal")
|
||||
| (StyleKind::Numbering, "NoList")
|
||||
);
|
||||
let is_default = st.default || (english_default && !flagged.contains(&st.kind));
|
||||
let normal_is_explicitly_off = st.kind == StyleKind::Paragraph && st.id == "Normal" && !st.default && flagged.contains(&StyleKind::Paragraph);
|
||||
let is_default = st.default || (english_default && !flagged.contains(&st.kind) && !normal_is_explicitly_off);
|
||||
let mut a = vec![("w:type", ty), ("w:styleId", st.id.as_str())];
|
||||
if is_default {
|
||||
if normal_is_explicitly_off {
|
||||
// Preserve a model where Normal is explicitly not default. Without this, the fallback
|
||||
// `english_default` marker makes the reader infer a change on round trip.
|
||||
a.push(("w:default", "0"));
|
||||
} else if is_default {
|
||||
a.push(("w:default", "1"));
|
||||
}
|
||||
if !st.builtin {
|
||||
@@ -102,16 +112,7 @@ fn style_xml(w: &mut W, st: &Style, flagged: &[StyleKind]) {
|
||||
if st.locked {
|
||||
w.empty("w:locked", &[]);
|
||||
}
|
||||
if !st.para.is_empty() && st.kind != StyleKind::Character {
|
||||
w.open("w:pPr", &[]);
|
||||
props::ppr_inner(w, &st.para, false, None);
|
||||
w.close("w:pPr");
|
||||
}
|
||||
if props::has_rpr(&st.chr) && st.kind != StyleKind::Numbering {
|
||||
w.open("w:rPr", &[]);
|
||||
props::rpr_inner(w, &st.chr);
|
||||
w.close("w:rPr");
|
||||
}
|
||||
style_property_xml(wr, w, st);
|
||||
if let Some(t) = &st.table {
|
||||
if t.borders.is_some() || t.cell_margins.is_some() || t.band_size.is_some() || t.col_band_size.is_some() {
|
||||
w.open("w:tblPr", &[]);
|
||||
@@ -179,6 +180,37 @@ fn style_xml(w: &mut W, st: &Style, flagged: &[StyleKind]) {
|
||||
w.close("w:style");
|
||||
}
|
||||
|
||||
fn style_property_xml(wr: &mut Writer<'_>, w: &mut W, st: &Style) {
|
||||
let para_change = st.para_change.as_deref().filter(|ch| ch.old.formatting() != st.para.formatting());
|
||||
if (!st.para.is_empty() || para_change.is_some()) && st.kind != StyleKind::Character {
|
||||
w.open("w:pPr", &[]);
|
||||
props::ppr_inner(w, &st.para, false, None);
|
||||
if let Some(ch) = para_change {
|
||||
let a = wr.change_attrs(ch.rev);
|
||||
open_change(w, "w:pPrChange", &a);
|
||||
w.open("w:pPr", &[]);
|
||||
props::ppr_inner(w, &ch.old, false, None);
|
||||
w.close("w:pPr");
|
||||
w.close("w:pPrChange");
|
||||
}
|
||||
w.close("w:pPr");
|
||||
}
|
||||
let run_change = st.chr_change.as_deref().filter(|ch| ch.old.formatting() != st.chr.formatting());
|
||||
if (props::has_rpr(&st.chr) || run_change.is_some()) && st.kind != StyleKind::Numbering {
|
||||
w.open("w:rPr", &[]);
|
||||
props::rpr_inner(w, &st.chr);
|
||||
if let Some(ch) = run_change {
|
||||
let a = wr.change_attrs(ch.rev);
|
||||
open_change(w, "w:rPrChange", &a);
|
||||
w.open("w:rPr", &[]);
|
||||
props::rpr_inner(w, &ch.old);
|
||||
w.close("w:rPr");
|
||||
w.close("w:rPrChange");
|
||||
}
|
||||
w.close("w:rPr");
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn numbering_xml(doc: &Document) -> Vec<u8> {
|
||||
let mut w = W::new();
|
||||
let ns = xml::body_ns();
|
||||
|
||||
@@ -175,3 +175,155 @@ fn rtl_tables_and_sections_round_trip() {
|
||||
assert_eq!(again, vec![true, false]);
|
||||
assert!(r.last_section.rtl && r.last_section.columns.count == 2);
|
||||
}
|
||||
|
||||
/// A package whose body is `body` and whose styles part holds `styles`.
|
||||
fn docx_with_styles(body: &str, styles: &str) -> Vec<u8> {
|
||||
let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#;
|
||||
let root = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>"#;
|
||||
let rels = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rIdS" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#;
|
||||
let doc = format!(r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document {ns}><w:body>{body}</w:body></w:document>"#);
|
||||
let st = format!(r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles {ns}>{styles}</w:styles>"#);
|
||||
zip(&[
|
||||
("_rels/.rels", root.as_bytes()),
|
||||
("word/_rels/document.xml.rels", rels.as_bytes()),
|
||||
("word/document.xml", doc.as_bytes()),
|
||||
("word/styles.xml", st.as_bytes()),
|
||||
])
|
||||
}
|
||||
|
||||
/// Thai text draws in the complex-script formatting only where Word draws it so (measured in Word
|
||||
/// for Windows; run defaults Angsana New 22 pt, complex script Tahoma 11 pt bold). Without
|
||||
/// `w:cs` or `w:rtl` on the run, its paragraph style or the run defaults, Thai text measures as
|
||||
/// Angsana New 22 pt, whatever its language, `w:hint` or character style.
|
||||
#[test]
|
||||
fn thai_text_takes_the_complex_script_formatting_only_in_a_complex_script_run() {
|
||||
let thai = "ทดสอบ ภาษาไทย";
|
||||
let defaults = |extra: &str| {
|
||||
format!(
|
||||
r#"<w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="Angsana New" w:hAnsi="Angsana New" w:cs="Tahoma"/><w:bCs/><w:sz w:val="44"/><w:szCs w:val="22"/>{extra}</w:rPr></w:rPrDefault></w:docDefaults>
|
||||
<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/></w:style>
|
||||
<w:style w:type="paragraph" w:styleId="CsPara"><w:name w:val="CsPara"/><w:basedOn w:val="Normal"/><w:rPr><w:cs/></w:rPr></w:style>
|
||||
<w:style w:type="character" w:styleId="CsChar"><w:name w:val="CsChar"/><w:rPr><w:cs/></w:rPr></w:style>
|
||||
<w:style w:type="character" w:styleId="CsOff"><w:name w:val="CsOff"/><w:rPr><w:cs w:val="0"/></w:rPr></w:style>
|
||||
<w:style w:type="character" w:styleId="RtlChar"><w:name w:val="RtlChar"/><w:rPr><w:rtl/></w:rPr></w:style>
|
||||
<w:style w:type="table" w:styleId="CsTable"><w:name w:val="CsTable"/><w:rPr><w:cs/></w:rPr></w:style>
|
||||
<w:style w:type="table" w:styleId="SizeTable"><w:name w:val="SizeTable"/><w:rPr><w:sz w:val="30"/><w:szCs w:val="16"/></w:rPr></w:style>"#
|
||||
)
|
||||
};
|
||||
let latin = ("Angsana New", 22.0, false);
|
||||
let complex = ("Tahoma", 11.0, true);
|
||||
let run = |rpr: &str| format!(r#"<w:p><w:r><w:rPr>{rpr}</w:rPr><w:t xml:space="preserve">{thai}</w:t></w:r></w:p>"#);
|
||||
let styled = |p: &str, rpr: &str| {
|
||||
format!(r#"<w:p><w:pPr><w:pStyle w:val="{p}"/></w:pPr><w:r><w:rPr>{rpr}</w:rPr><w:t xml:space="preserve">{thai}</w:t></w:r></w:p>"#)
|
||||
};
|
||||
let in_table = |style: &str| {
|
||||
format!(
|
||||
r#"<w:tbl><w:tblPr><w:tblStyle w:val="{style}"/></w:tblPr><w:tblGrid><w:gridCol w:w="6000"/></w:tblGrid><w:tr><w:tc>{}</w:tc></w:tr></w:tbl>"#,
|
||||
run("")
|
||||
)
|
||||
};
|
||||
let cs_off = r#"<w:rStyle w:val="CsOff"/>"#;
|
||||
let cases: Vec<(&str, String, String, (&str, f32, bool))> = vec![
|
||||
("plain", defaults(""), run(""), latin),
|
||||
("bidi language", defaults(""), run(r#"<w:lang w:val="th-TH" w:bidi="th-TH"/>"#), latin),
|
||||
("hint", defaults(""), run(r#"<w:rFonts w:hint="cs"/>"#), latin),
|
||||
("character style", defaults(""), run(r#"<w:rStyle w:val="CsChar"/>"#), latin),
|
||||
("character style rtl", defaults(""), run(r#"<w:rStyle w:val="RtlChar"/>"#), latin),
|
||||
("run cs", defaults(""), run("<w:cs/>"), complex),
|
||||
("run rtl", defaults(""), run("<w:rtl/>"), complex),
|
||||
("paragraph style", defaults(""), styled("CsPara", ""), complex),
|
||||
("paragraph style, run cs off", defaults(""), styled("CsPara", r#"<w:cs w:val="0"/>"#), latin),
|
||||
("run defaults", defaults("<w:cs/>"), run(""), complex),
|
||||
// A character style's `w:cs w:val="0"` doesn't turn the paragraph style's or the run
|
||||
// defaults' off either.
|
||||
("paragraph style, character style cs off", defaults(""), styled("CsPara", cs_off), complex),
|
||||
("run defaults, character style cs off", defaults("<w:cs/>"), run(cs_off), complex),
|
||||
// A table style's `w:cs` counts, and its sizes are the run's.
|
||||
("table style", defaults(""), in_table("CsTable"), complex),
|
||||
("table style sizes", defaults(""), in_table("SizeTable"), ("Angsana New", 15.0, false)),
|
||||
];
|
||||
let check = |d: &Document, name: &str, want: (&str, f32, bool), when: &str| {
|
||||
let (p, table) = match &*d.body[0] {
|
||||
craft_wordmodel::Block::Table(t) => {
|
||||
(t.rows[0].cells[0].blocks[0].as_para().unwrap(), d.styles.table_cells_chr(t).map(|c| c[0][0].clone()))
|
||||
}
|
||||
b => (b.as_para().unwrap(), None),
|
||||
};
|
||||
let rc = d.styles.resolve_char_in(p.props.style.as_deref(), table.as_ref(), &p.runs[0].props);
|
||||
for ch in p.text.chars() {
|
||||
let drawn = if rc.uses_complex(ch) { rc.complex() } else { rc.clone() };
|
||||
assert_eq!((drawn.font.as_str(), drawn.size, drawn.bold), want, "{name} {when}: {ch:?}");
|
||||
}
|
||||
let c = &p.runs[0].props;
|
||||
assert_eq!((c.font_cs.as_deref(), c.size_cs, c.bold_cs), (None, None, None), "{name} {when}: nothing is pinned");
|
||||
};
|
||||
for (name, styles, body, want) in &cases {
|
||||
let d = craft_docx::read(&docx_with_styles(body, styles)).unwrap();
|
||||
check(&d, name, *want, "read");
|
||||
let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
|
||||
check(&again, name, *want, "after a save");
|
||||
}
|
||||
// Latin text and right-to-left text are left as they were.
|
||||
let body = r#"<w:p><w:r><w:t>Latin only</w:t></w:r></w:p><w:p><w:r><w:t>سلام</w:t></w:r></w:p>"#;
|
||||
let d = craft_docx::read(&docx_with_styles(body, &defaults(""))).unwrap();
|
||||
for p in paras(&d) {
|
||||
assert_eq!(p.runs[0].props.cs, None, "{}", p.text);
|
||||
}
|
||||
// Latin text follows the same rule (measured): a character style's `w:cs` or `w:rtl` leaves it in the
|
||||
// run's own formatting, a character style's `w:cs w:val="0"` under a paragraph style's
|
||||
// `w:cs` in the complex-script formatting.
|
||||
let latin_in =
|
||||
|p: &str, rpr: &str| format!(r#"<w:p><w:pPr><w:pStyle w:val="{p}"/></w:pPr><w:r><w:rPr>{rpr}</w:rPr><w:t>Latin</w:t></w:r></w:p>"#);
|
||||
let body =
|
||||
latin_in("Normal", r#"<w:rStyle w:val="CsChar"/>"#) + &latin_in("CsPara", cs_off) + &latin_in("Normal", r#"<w:rStyle w:val="RtlChar"/>"#);
|
||||
let d = craft_docx::read(&docx_with_styles(&body, &defaults(""))).unwrap();
|
||||
let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
|
||||
for (d, when) in [(&d, "read"), (&again, "after a save")] {
|
||||
let drawn: Vec<_> = paras(d)
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let rc = d.styles.resolve_char(p.props.style.as_deref(), &p.runs[0].props);
|
||||
let c = if rc.uses_complex('L') { rc.complex() } else { rc };
|
||||
(c.font, c.size)
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(drawn, [("Angsana New".to_string(), 22.0), ("Tahoma".to_string(), 11.0), ("Angsana New".to_string(), 22.0)], "{when}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Measured in Word for Windows: Thai text as a field's result follows the same rule as ordinary
|
||||
/// text, also when the reader keeps the field as one object with its result.
|
||||
#[test]
|
||||
fn a_fields_thai_result_takes_the_complex_script_formatting_only_in_a_complex_script_run() {
|
||||
let styles = r#"<w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="Angsana New" w:hAnsi="Angsana New" w:cs="Tahoma"/><w:sz w:val="20"/><w:szCs w:val="40"/></w:rPr></w:rPrDefault></w:docDefaults>
|
||||
<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/></w:style>"#;
|
||||
let field = |rpr: &str| {
|
||||
let r = |inner: &str| format!("<w:r><w:rPr>{rpr}</w:rPr>{inner}</w:r>");
|
||||
format!(
|
||||
"<w:p>{}{}{}{}{}</w:p>",
|
||||
r(r#"<w:fldChar w:fldCharType="begin"/>"#),
|
||||
r(r#"<w:instrText xml:space="preserve"> QUOTE "x" </w:instrText>"#),
|
||||
r(r#"<w:fldChar w:fldCharType="separate"/>"#),
|
||||
r("<w:t>ทดสอบ</w:t>"),
|
||||
r(r#"<w:fldChar w:fldCharType="end"/>"#)
|
||||
)
|
||||
};
|
||||
let drawn = |d: &Document| {
|
||||
let p = paras(d)[0];
|
||||
let at = p.text.find(craft_wordmodel::para::OBJ).expect("the field is one object");
|
||||
let run = p.runs.iter().scan(0, |end, r| {
|
||||
let start = *end;
|
||||
*end += r.len;
|
||||
Some((start, r))
|
||||
});
|
||||
let (_, run) = run.into_iter().find(|(start, r)| *start <= at && at < start + r.len).unwrap();
|
||||
let rc = d.styles.resolve_char(p.props.style.as_deref(), &run.props);
|
||||
if rc.uses_complex('ท') { (rc.font_cs.clone(), rc.size_cs) } else { (rc.font.clone(), rc.size) }
|
||||
};
|
||||
for (what, rpr, want) in [("nothing", "", ("Angsana New", 10.0)), ("w:cs", "<w:cs/>", ("Tahoma", 20.0))] {
|
||||
let d = craft_docx::read(&docx_with_styles(&field(rpr), styles)).unwrap();
|
||||
assert_eq!(drawn(&d), (want.0.to_string(), want.1), "a run with {what}: read");
|
||||
let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
|
||||
assert_eq!(drawn(&again), (want.0.to_string(), want.1), "a run with {what}: after a save");
|
||||
}
|
||||
}
|
||||
@@ -28,3 +28,73 @@ fn partial_margin_stays_partial() {
|
||||
assert!(!margins.contains("<w:bottom"));
|
||||
assert!(!margins.contains("<w:right"));
|
||||
}
|
||||
|
||||
/// A package with this body and, if given, this styles part.
|
||||
fn package(body: &str, styles: Option<&str>) -> Vec<u8> {
|
||||
const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
|
||||
let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let mut add = |name: &str, xml: &str| {
|
||||
zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
zip.write_all(xml.as_bytes()).unwrap();
|
||||
};
|
||||
add("word/document.xml", &format!(r#"<w:document xmlns:w="{W}"><w:body>{body}</w:body></w:document>"#));
|
||||
if let Some(styles) = styles {
|
||||
add("word/styles.xml", &format!(r#"<w:styles xmlns:w="{W}">{styles}</w:styles>"#));
|
||||
add(
|
||||
"word/_rels/document.xml.rels",
|
||||
r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>"#,
|
||||
);
|
||||
}
|
||||
zip.finish().unwrap().into_inner()
|
||||
}
|
||||
|
||||
/// Without a default table style (no styles part, or none marked `w:default`) Word gives a table
|
||||
/// no cell margins: a cell that sets only its right margin has none on its other sides.
|
||||
#[test]
|
||||
fn no_default_table_style_means_no_cell_margins() {
|
||||
let body = r#"<w:tbl><w:tblPr><w:tblW w:w="6000" w:type="dxa"/></w:tblPr><w:tblGrid><w:gridCol w:w="3000"/></w:tblGrid><w:tr><w:tc><w:tcPr><w:tcMar><w:right w:w="108" w:type="dxa"/></w:tcMar></w:tcPr><w:p/></w:tc></w:tr></w:tbl>"#;
|
||||
let normal_table = r#"<w:style w:type="table" w:default="1" w:styleId="TableNormal"><w:name w:val="Normal Table"/><w:tblPr><w:tblCellMar><w:left w:w="108" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style>"#;
|
||||
let margins = |styles: Option<&str>| {
|
||||
let doc = craft_docx::read(&package(body, styles)).unwrap();
|
||||
let table = doc.body[0].as_table().unwrap();
|
||||
(table.props.cell_margins, table.rows[0].cells[0].props.resolved_margins(table.props.cell_margins.unwrap_or([0.0, 5.4, 0.0, 5.4])))
|
||||
};
|
||||
assert_eq!(margins(None), (Some([0.0; 4]), [0.0, 0.0, 0.0, 5.4]));
|
||||
assert_eq!(margins(Some(r#"<w:docDefaults/>"#)), (Some([0.0; 4]), [0.0, 0.0, 0.0, 5.4]));
|
||||
// With Word's usual Normal Table the table keeps no margins of its own (the defaults apply).
|
||||
assert_eq!(margins(Some(normal_table)).0, None);
|
||||
// A style defined twice is its last definition: a default one redefined as not default leaves none.
|
||||
let redefined = format!(r#"{normal_table}<w:style w:type="table" w:styleId="TableNormal"><w:name w:val="Normal Table"/></w:style>"#);
|
||||
assert_eq!(margins(Some(&redefined)).0, Some([0.0; 4]));
|
||||
// A custom default table style marked `on` is a default too, also after saving and reopening.
|
||||
let plain = r#"<w:style w:type="table" w:default="on" w:customStyle="1" w:styleId="Plain"><w:name w:val="Plain"/></w:style>"#;
|
||||
let doc = craft_docx::read(&package(body, Some(plain))).unwrap();
|
||||
assert_eq!(doc.body[0].as_table().unwrap().props.cell_margins, None);
|
||||
let reopened = craft_docx::read(&craft_docx::write(&doc).unwrap()).unwrap();
|
||||
assert_eq!(reopened.body[0].as_table().unwrap().props.cell_margins, None);
|
||||
// A table that sets only some sides keeps them; the sides it omits are 0.
|
||||
let partial = body.replace("<w:tblPr>", r#"<w:tblPr><w:tblCellMar><w:left w:w="144" w:type="dxa"/></w:tblCellMar>"#);
|
||||
let doc = craft_docx::read(&package(&partial, None)).unwrap();
|
||||
assert_eq!(doc.body[0].as_table().unwrap().props.cell_margins, Some([0.0, 7.2, 0.0, 0.0]));
|
||||
// Rejecting a tracked change to the table's properties brings back the same zero margins.
|
||||
let tracked = body.replace(
|
||||
"</w:tblPr>",
|
||||
r#"<w:tblPrChange w:id="1" w:author="A" w:date="2026-11-13T00:00:00Z"><w:tblPr><w:tblW w:w="5000" w:type="dxa"/></w:tblPr></w:tblPrChange></w:tblPr>"#,
|
||||
);
|
||||
let doc = craft_docx::read(&package(&tracked, None)).unwrap();
|
||||
let change = doc.body[0].as_table().unwrap().props.fmt_change.as_deref().expect("a tracked table change");
|
||||
assert_eq!(change.old.cell_margins, Some([0.0; 4]));
|
||||
// A table whose own style gives margins keeps them.
|
||||
let styled = body.replace("<w:tblPr>", r#"<w:tblPr><w:tblStyle w:val="Boxed"/>"#);
|
||||
let boxed = r#"<w:style w:type="table" w:styleId="Boxed"><w:name w:val="Boxed"/><w:tblPr><w:tblCellMar><w:left w:w="144" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style>"#;
|
||||
let doc = craft_docx::read(&package(&styled, Some(boxed))).unwrap();
|
||||
assert_eq!(doc.body[0].as_table().unwrap().props.cell_margins, None);
|
||||
// The zeros survive saving, which writes a default Normal Table.
|
||||
let doc = craft_docx::read(&package(body, None)).unwrap();
|
||||
let bytes = craft_docx::write(&doc).unwrap();
|
||||
let mut xml = String::new();
|
||||
zip::ZipArchive::new(Cursor::new(bytes.clone())).unwrap().by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap();
|
||||
assert!(xml.contains("<w:tblCellMar>"), "{xml}");
|
||||
let reopened = craft_docx::read(&bytes).unwrap();
|
||||
assert_eq!(reopened.body[0].as_table().unwrap().props.cell_margins, Some([0.0; 4]));
|
||||
}
|
||||
@@ -17,6 +17,10 @@ fn note(tag: &str, mark: &str, comment: u32, text: &str) -> String {
|
||||
}
|
||||
|
||||
fn read_doc() -> Document {
|
||||
read_doc_with_note_relationships(true)
|
||||
}
|
||||
|
||||
fn read_doc_with_note_relationships(with_note_relationships: bool) -> Document {
|
||||
let doc = format!(
|
||||
r#"<w:document {W_NS}><w:body><w:p><w:r><w:t>Body</w:t></w:r><w:r><w:footnoteReference w:id="1"/></w:r><w:r><w:endnoteReference w:id="1"/></w:r></w:p></w:body></w:document>"#
|
||||
);
|
||||
@@ -25,10 +29,15 @@ fn read_doc() -> Document {
|
||||
let comments = format!(
|
||||
r#"<w:comments {W_NS}><w:comment w:id="7" w:author="Ann" w:initials="A"><w:p><w:r><w:t>On the footnote</w:t></w:r></w:p></w:comment><w:comment w:id="8" w:author="Bob" w:initials="B"><w:p><w:r><w:t>On the endnote</w:t></w:r></w:p></w:comment></w:comments>"#
|
||||
);
|
||||
let doc_rels = if with_note_relationships {
|
||||
DOC_RELS
|
||||
} else {
|
||||
r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml"/></Relationships>"#
|
||||
};
|
||||
let bytes = zip(&[
|
||||
("_rels/.rels", ROOT_RELS.as_bytes()),
|
||||
("word/document.xml", doc.as_bytes()),
|
||||
("word/_rels/document.xml.rels", DOC_RELS.as_bytes()),
|
||||
("word/_rels/document.xml.rels", doc_rels.as_bytes()),
|
||||
("word/footnotes.xml", footnotes.as_bytes()),
|
||||
("word/endnotes.xml", endnotes.as_bytes()),
|
||||
("word/comments.xml", comments.as_bytes()),
|
||||
@@ -36,6 +45,26 @@ fn read_doc() -> Document {
|
||||
craft_docx::read(&bytes).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conventional_note_parts_are_read_without_relationships() {
|
||||
let d = read_doc_with_note_relationships(false);
|
||||
let body = d.body[0].as_para().unwrap();
|
||||
let refs: Vec<(NoteKind, u32)> = body
|
||||
.objects
|
||||
.iter()
|
||||
.filter_map(|o| match o {
|
||||
InlineObject::NoteRef { kind, id, .. } => Some((*kind, *id)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(refs.iter().map(|(kind, _)| *kind).collect::<Vec<_>>(), [NoteKind::Footnote, NoteKind::Endnote]);
|
||||
for ((kind, id), wanted) in refs.into_iter().zip(["Foot.", "End."]) {
|
||||
let part = d.parts.get(&id).unwrap_or_else(|| panic!("missing {kind:?} part {id}"));
|
||||
let text = part.blocks[0].as_para().unwrap().plain_text();
|
||||
assert!(text.contains(wanted), "{kind:?}: {text:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// comments.xml has to be read before the notes: a note's `w:commentRangeStart` resolves its id
|
||||
/// against the comments already read, and was dropped when the notes came first.
|
||||
#[test]
|
||||
|
||||
@@ -320,6 +320,34 @@ fn list_identities_survive_and_a_copied_definition_gets_none() {
|
||||
assert_eq!(n.matches("1F2E3D4C").count(), 1, "{n}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_numbering_levels_are_skipped_instead_of_becoming_level_zero() {
|
||||
let numbering = r#"<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1."/></w:lvl><w:lvl w:ilvl="-1"><w:numFmt w:val="bullet"/><w:lvlText w:val="bad"/></w:lvl><w:lvl w:ilvl="9"><w:numFmt w:val="bullet"/><w:lvlText w:val="bad"/></w:lvl><w:lvl w:ilvl="999999999999999999999"><w:numFmt w:val="bullet"/><w:lvlText w:val="bad"/></w:lvl></w:abstractNum><w:num w:numId="1"><w:abstractNumId w:val="0"/><w:lvlOverride w:ilvl="-1"><w:startOverride w:val="42"/></w:lvlOverride><w:lvlOverride w:ilvl="9"><w:startOverride w:val="43"/></w:lvlOverride><w:lvlOverride><w:startOverride w:val="44"/></w:lvlOverride></w:num></w:numbering>"#;
|
||||
let body = r#"<w:p><w:pPr><w:numPr><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>default level</w:t></w:r></w:p><w:p><w:pPr><w:numPr><w:ilvl w:val="2"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>valid level</w:t></w:r></w:p><w:p><w:pPr><w:numPr><w:ilvl w:val="-1"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>negative level</w:t></w:r></w:p><w:p><w:pPr><w:numPr><w:ilvl w:val="9"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>out of range</w:t></w:r></w:p><w:p><w:pPr><w:numPr><w:ilvl w:val="999999999999999999999"/><w:numId w:val="1"/></w:numPr></w:pPr><w:r><w:t>overflow</w:t></w:r></w:p>"#;
|
||||
let bytes = package(body, &[("rId1", &format!("{REL}/numbering"), "numbering.xml")], &[], &[("word/numbering.xml", numbering)]);
|
||||
let doc = craft_docx::read(&bytes).unwrap();
|
||||
let paragraphs: Vec<_> = doc
|
||||
.body
|
||||
.iter()
|
||||
.filter_map(|block| match &**block {
|
||||
craft_wordmodel::Block::Para(p) => Some(p),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(paragraphs.len(), 5);
|
||||
assert!(doc.numbering.level_is_defined(1, 0));
|
||||
assert!(!doc.numbering.level_is_defined(1, 2), "effective defaults remain distinguishable from source definitions");
|
||||
assert!(doc.numbering.level(1, 2).is_some(), "Word's effective default level remains available");
|
||||
assert_eq!(paragraphs[0].props.numbering.map(|n| (n.num, n.level)), Some((1, 0)), "an omitted level defaults to zero");
|
||||
assert_eq!(paragraphs[1].props.numbering.map(|n| (n.num, n.level)), Some((1, 2)), "a valid level is preserved");
|
||||
for p in ¶graphs[2..] {
|
||||
assert_eq!(p.props.numbering, None, "malformed explicit level is not treated as a real list: {}", p.text);
|
||||
}
|
||||
let level_zero = doc.numbering.level(1, 0).expect("the valid level zero");
|
||||
assert_eq!(level_zero.format, craft_wordmodel::section::NumFormat::Decimal);
|
||||
assert_eq!(level_zero.start, 1, "invalid level overrides do not affect level zero");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extended_properties_are_kept_without_words_counts_and_personal_ones_follow_the_privacy_setting() {
|
||||
let app = r#"<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>42</TotalTime><Pages>107</Pages><Words>40000</Words><Characters>250000</Characters><Application>Microsoft Office Word</Application><DocSecurity>0</DocSecurity><Lines>2000</Lines><Paragraphs>1700</Paragraphs><ScaleCrop>false</ScaleCrop><Manager>M</Manager><Company>Firma</Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>290000</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinkBase>https://example.com/</HyperlinkBase><HyperlinksChanged>false</HyperlinksChanged><AppVersion>16.0000</AppVersion></Properties>"#;
|
||||
|
||||
@@ -269,6 +269,7 @@ fn styles_and_lists_round_trip() {
|
||||
locked: true,
|
||||
default: false,
|
||||
table: None,
|
||||
..Default::default()
|
||||
});
|
||||
Arc::make_mut(&mut d.styles).upsert(Style {
|
||||
id: "MyStyleChar".into(),
|
||||
|
||||
@@ -3,12 +3,20 @@
|
||||
//! `<w:pStyle w:val="Footer"/>` without defining Footer.
|
||||
use craft_wordmodel::props::LineSpacing;
|
||||
use craft_wordmodel::{Block, Document, PartKind};
|
||||
use std::io::Write;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
|
||||
const R: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
|
||||
|
||||
fn package(normal_id: &str) -> Vec<u8> {
|
||||
package_with_default(normal_id, true)
|
||||
}
|
||||
|
||||
fn package_with_default(normal_id: &str, is_default: bool) -> Vec<u8> {
|
||||
package_with_body_default(normal_id, is_default, false)
|
||||
}
|
||||
|
||||
fn package_with_body_default(normal_id: &str, is_default: bool, has_body_default: bool) -> Vec<u8> {
|
||||
let root = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>"#;
|
||||
let doc = format!(
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="{W}" xmlns:r="{R}"><w:body><w:p><w:pPr><w:pStyle w:val="Footer"/></w:pPr><w:r><w:t>body</w:t></w:r></w:p><w:p><w:r><w:t>normal</w:t></w:r></w:p><w:sectPr><w:footerReference w:type="default" r:id="rId2"/></w:sectPr></w:body></w:document>"#
|
||||
@@ -16,8 +24,11 @@ fn package(normal_id: &str) -> Vec<u8> {
|
||||
let footer = format!(
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:ftr xmlns:w="{W}"><w:p><w:pPr><w:pStyle w:val="Footer"/><w:jc w:val="center"/></w:pPr><w:r><w:t>1</w:t></w:r></w:p></w:ftr>"#
|
||||
);
|
||||
let default_attr = if is_default { r#" w:default="1""# } else { "" };
|
||||
let body_style =
|
||||
if has_body_default { r#"<w:style w:type="paragraph" w:default="on" w:styleId="Body"><w:name w:val="Body"/></w:style>"# } else { "" };
|
||||
let styles = format!(
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="{W}"><w:docDefaults><w:rPrDefault><w:rPr><w:sz w:val="24"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:spacing w:after="200"/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph" w:default="1" w:styleId="{normal_id}"><w:name w:val="Normal"/><w:pPr><w:spacing w:before="20" w:after="40" w:line="259" w:lineRule="auto"/></w:pPr><w:rPr><w:sz w:val="26"/></w:rPr></w:style></w:styles>"#
|
||||
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="{W}"><w:docDefaults><w:rPrDefault><w:rPr><w:sz w:val="24"/></w:rPr></w:rPrDefault><w:pPrDefault><w:pPr><w:spacing w:after="200"/></w:pPr></w:pPrDefault></w:docDefaults><w:style w:type="paragraph"{default_attr} w:styleId="{normal_id}"><w:name w:val="Normal"/><w:pPr><w:spacing w:before="20" w:after="40" w:line="259" w:lineRule="auto"/></w:pPr><w:rPr><w:sz w:val="26"/></w:rPr></w:style>{body_style}</w:styles>"#
|
||||
);
|
||||
let rels = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/></Relationships>"#;
|
||||
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
@@ -34,6 +45,27 @@ fn package(normal_id: &str) -> Vec<u8> {
|
||||
zw.finish().unwrap().into_inner()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_explicit_normal_style_is_the_default_even_without_w_default() {
|
||||
let doc = craft_docx::read(&package_with_default("Normal", false)).unwrap();
|
||||
let normal = doc.styles.get("Normal").expect("the explicit Normal style");
|
||||
assert!(normal.default, "Word treats the explicitly declared built-in Normal style as the default");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_explicit_body_default_on_prevents_normal_inference() {
|
||||
let doc = craft_docx::read(&package_with_body_default("Normal", false, true)).unwrap();
|
||||
assert!(doc.styles.styles.iter().any(|style| style.id == "Body" && style.default));
|
||||
assert!(!doc.styles.styles.iter().any(|style| style.id == "Normal" && style.default));
|
||||
let saved = craft_docx::write(&doc).unwrap();
|
||||
let mut archive = zip::ZipArchive::new(std::io::Cursor::new(saved)).unwrap();
|
||||
let mut file = archive.by_name("word/styles.xml").unwrap();
|
||||
let mut styles = String::new();
|
||||
file.read_to_string(&mut styles).unwrap();
|
||||
assert!(styles.contains(r#"w:styleId="Normal" w:default="0""#) || styles.contains(r#"w:default="0" w:styleId="Normal""#));
|
||||
assert!(styles.contains(r#"w:styleId="Body" w:default="1""#) || styles.contains(r#"w:default="1" w:styleId="Body""#));
|
||||
}
|
||||
|
||||
fn footer_para(d: &Document) -> &craft_wordmodel::Paragraph {
|
||||
let part = d.parts.values().find(|p| p.kind == PartKind::Footer).expect("a footer");
|
||||
match part.blocks.first().map(|b| &**b) {
|
||||
|
||||
@@ -11,7 +11,9 @@ allocation failure inside the decoder. On native applications, call `use_own_wor
|
||||
startup and call `serve_if_asked()` before normal argument handling. Subsequent `decode` and
|
||||
`encode_*` calls then run in a child process with a 30-minute timeout; a crash or stack overflow
|
||||
is contained to that child. The worker has no operating-system memory limit yet. Without a
|
||||
configured worker, conversions run in the current process.
|
||||
configured worker, conversions are refused rather than run in the current process: call
|
||||
`allow_in_process()` only for input that is known to be trusted (tests, or bytes this process
|
||||
produced itself). Coordinates, once a worker is configured but cannot start, also fail closed.
|
||||
|
||||
The bridge is not built for `wasm32` or FreeBSD/aarch64. Those targets retain the public API and
|
||||
return a clear unsupported-build error. The `acadrust` 0.6.6 dependency is used unmodified under
|
||||
|
||||
+11
-1
@@ -21,6 +21,11 @@
|
||||
//! These bound the input- and output-size variants. A small file that declares a huge internal
|
||||
//! table or section can still make the dependency allocate before any size is known, so the app
|
||||
//! runs both directions in a child process ([`worker`]): what that kills is the child.
|
||||
//!
|
||||
//! Isolation is not optional for untrusted input. With no worker configured the conversion is
|
||||
//! refused rather than run here, and a worker that cannot start is an error, so a missing worker
|
||||
//! never turns into decoding a hostile file in the caller's process. [`worker::allow_in_process`]
|
||||
//! is the explicit exception, for input the caller knows is trusted.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
@@ -65,7 +70,10 @@ fn mb(n: u64) -> String {
|
||||
#[cfg(not(no_dwg_bridge))]
|
||||
pub mod worker;
|
||||
#[cfg(not(no_dwg_bridge))]
|
||||
pub use worker::{WORKER_ARG, crash_worker_for_test, decode, encode_as, encode_kept_noted, encode_noted, serve_if_asked, use_own_worker, use_worker};
|
||||
pub use worker::{
|
||||
WORKER_ARG, allow_in_process, crash_worker_for_test, decode, encode_as, encode_kept_noted, encode_noted, serve_if_asked, use_own_worker,
|
||||
use_worker,
|
||||
};
|
||||
#[cfg(not(no_dwg_bridge))]
|
||||
mod proxy_transport;
|
||||
|
||||
@@ -1569,6 +1577,8 @@ pub fn serve_if_asked() -> Option<i32> {
|
||||
#[cfg(no_dwg_bridge)]
|
||||
pub fn use_own_worker() {}
|
||||
#[cfg(no_dwg_bridge)]
|
||||
pub fn allow_in_process() {}
|
||||
#[cfg(no_dwg_bridge)]
|
||||
pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec<u8>, Vec<String>), String> {
|
||||
dwg_to_dxf_noted(bytes, limits)
|
||||
}
|
||||
|
||||
+60
-13
@@ -6,9 +6,11 @@
|
||||
//! that reads one request from standard input and writes one reply to standard output. A file that
|
||||
//! kills the worker then costs that one open or save, and every other open drawing stays safe.
|
||||
//!
|
||||
//! Without a worker (libraries, tests, a build that can't start one) [`decode`] and [`encode_as`]
|
||||
//! run in the calling process, as they always did. There's no memory limit on the worker yet: that
|
||||
//! takes per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate.
|
||||
//! Without a worker [`decode`] and [`encode_as`] refuse rather than run the conversion in the
|
||||
//! calling process, because the third-party code is the part that can end a process.
|
||||
//! [`allow_in_process`] is the explicit exception, for input the caller knows is trusted (tests, or
|
||||
//! bytes this process produced itself). There's no memory limit on the worker yet: that takes
|
||||
//! per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate.
|
||||
//!
|
||||
//! A reply comes from a process that just handled a possibly hostile file, so it is parsed as
|
||||
//! untrusted input.
|
||||
@@ -18,6 +20,7 @@ use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{RecvTimeoutError, channel};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -38,6 +41,9 @@ const MAX_NOTES_BYTES: u64 = 64 << 20;
|
||||
const MAX_DWG_REPLY: u64 = 2 << 30;
|
||||
/// Most bytes one request holds: the largest input, whichever the direction, and its header.
|
||||
const MAX_REQUEST: u64 = (2 << 30) + 256;
|
||||
/// What a conversion says when it has no worker and is not allowed to run here.
|
||||
const NO_WORKER: &str = "DWG: no worker is configured, so this file was not converted; \
|
||||
call use_worker or use_own_worker at startup, or allow_in_process for input that is trusted";
|
||||
|
||||
struct Worker {
|
||||
program: PathBuf,
|
||||
@@ -45,6 +51,8 @@ struct Worker {
|
||||
}
|
||||
|
||||
static WORKER: Mutex<Option<Worker>> = Mutex::new(None);
|
||||
/// Whether this process may run the conversion itself, set only by [`allow_in_process`].
|
||||
static IN_PROCESS: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Start `program args` as the worker from now on. `program` must call [`serve_if_asked`].
|
||||
pub fn use_worker(program: PathBuf, args: Vec<OsString>) {
|
||||
@@ -62,12 +70,27 @@ pub fn use_own_worker() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Let [`decode`] and [`encode_as`] run the conversion in this process when no worker is set.
|
||||
///
|
||||
/// This opts out of the isolation the crate exists for: the third-party code allocates from sizes
|
||||
/// a hostile file declares, and a failed allocation ends the process. Call it for input the caller
|
||||
/// knows is trusted (tests, or bytes this process produced), never for a file that arrived from
|
||||
/// outside.
|
||||
pub fn allow_in_process() {
|
||||
IN_PROCESS.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn in_process_allowed() -> bool {
|
||||
IN_PROCESS.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Serve one request and return the exit code if this process was started as a worker.
|
||||
pub fn serve_if_asked() -> Option<i32> {
|
||||
(std::env::args_os().nth(1)? == WORKER_ARG).then(|| serve(std::io::stdin().lock(), std::io::stdout().lock()))
|
||||
}
|
||||
|
||||
/// [`super::dwg_to_dxf_noted`] in the worker, when there is one.
|
||||
/// [`super::dwg_to_dxf_noted`] in the worker. With no worker configured this is refused unless
|
||||
/// [`allow_in_process`] was called; a configured worker that cannot start is an error.
|
||||
pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec<u8>, Vec<String>), String> {
|
||||
// Before starting a process, and so the same limit holds with or without one.
|
||||
if bytes.len() > limits.max_dwg_bytes {
|
||||
@@ -76,17 +99,19 @@ pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec<u8>, Vec<String>), St
|
||||
let reply_limit = (limits.max_dxf_bytes as u64).saturating_add(MAX_NOTES_BYTES);
|
||||
match call(&request(DECODE, limits, ""), bytes, reply_limit, "reading this file") {
|
||||
Some(reply) => reply?,
|
||||
None => super::dwg_to_dxf_noted(bytes, limits),
|
||||
None if in_process_allowed() => super::dwg_to_dxf_noted(bytes, limits),
|
||||
None => Err(NO_WORKER.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// [`super::dxf_to_dwg_as`] in the worker, when there is one.
|
||||
/// [`super::dxf_to_dwg_as`] in the worker. Refused without a worker unless [`allow_in_process`]
|
||||
/// was called.
|
||||
pub fn encode_as(dxf: &[u8], version: &str) -> Result<Vec<u8>, String> {
|
||||
encode_noted(dxf, version).map(|(dwg, _)| dwg)
|
||||
}
|
||||
|
||||
/// [`super::dxf_to_dwg_noted`] in the worker, when there is one. What the writer left out comes
|
||||
/// back as notes of `TYPE<tab>count`.
|
||||
/// [`super::dxf_to_dwg_noted`] in the worker. What the writer left out comes back as notes of
|
||||
/// `TYPE<tab>count`.
|
||||
pub fn encode_noted(dxf: &[u8], version: &str) -> Result<(Vec<u8>, super::Unwritten), String> {
|
||||
encode_kept_noted(dxf, version, None)
|
||||
}
|
||||
@@ -103,7 +128,8 @@ pub fn encode_kept_noted(dxf: &[u8], version: &str, kept: Option<&str>) -> Resul
|
||||
push_str(&mut header, kept.unwrap_or(""));
|
||||
match call(&header, dxf, MAX_DWG_REPLY, "writing this file") {
|
||||
Some(reply) => reply?.map(|(dwg, notes)| (dwg, unwritten_from(¬es))),
|
||||
None => encode_here(dxf, version, kept),
|
||||
None if in_process_allowed() => encode_here(dxf, version, kept),
|
||||
None => Err(NO_WORKER.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,8 +173,9 @@ fn push_str(r: &mut Vec<u8>, text: &str) {
|
||||
|
||||
type Reply = Result<(Vec<u8>, Vec<String>), String>;
|
||||
|
||||
/// Run one request in a worker. `None`: none could be started, so the caller does the work.
|
||||
/// `Err`: the worker went wrong, and `doing` says at what, for the user.
|
||||
/// Run one request in a worker. `None`: no worker is configured, so the caller decides what to do
|
||||
/// (convert here only with [`allow_in_process`], or refuse). `Err`: a configured worker could not
|
||||
/// start or went wrong, and `doing` says at what, for the user.
|
||||
fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option<Result<Reply, String>> {
|
||||
let mut command = {
|
||||
let worker = WORKER.lock().ok()?;
|
||||
@@ -167,14 +194,15 @@ fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option<
|
||||
let mut child = match command.spawn() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
// A worker is configured but cannot run: never fall back to decoding here.
|
||||
log::warn!("DWG worker: can't start it: {e}");
|
||||
return None;
|
||||
return Some(Err(format!("the DWG worker could not start ({e}); nothing else is affected")));
|
||||
}
|
||||
};
|
||||
let (Some(mut to_child), Some(from_child)) = (child.stdin.take(), child.stdout.take()) else {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return None;
|
||||
return Some(Err("the DWG worker could not start (no pipes); nothing else is affected".to_string()));
|
||||
};
|
||||
let received = std::thread::scope(|scope| {
|
||||
// Written beside the read: a worker that replies before it has read everything, or dies,
|
||||
@@ -367,6 +395,25 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// A conversion is refused rather than run here, and a worker that cannot start is an error:
|
||||
/// neither case may turn into decoding a (possibly hostile) file in this process.
|
||||
#[test]
|
||||
fn a_conversion_is_refused_without_a_worker_and_never_falls_back_to_this_process() {
|
||||
// No worker is configured and nothing opted in.
|
||||
assert_eq!(decode(b"AC1032", Limits::DEFAULT).unwrap_err(), NO_WORKER);
|
||||
assert_eq!(encode_as(b"not a dxf", "AC1032").unwrap_err(), NO_WORKER);
|
||||
// Trusted input may run here, and the reader still reports a file it cannot read.
|
||||
allow_in_process();
|
||||
assert!(in_process_allowed());
|
||||
assert!(decode(b"AC1032", Limits::DEFAULT).is_err());
|
||||
assert!(encode_as(b"not a dxf", "AC1032").is_err());
|
||||
// A configured worker that cannot start is an error even after the opt-in: the conversion
|
||||
// never runs here instead.
|
||||
use_worker(PathBuf::from("/nonexistent/craft-dwg-worker"), Vec::new());
|
||||
assert!(decode(b"AC1032", Limits::DEFAULT).unwrap_err().contains("could not start"));
|
||||
assert!(encode_as(b"not a dxf", "AC1032").unwrap_err().contains("could not start"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serving_reports_a_decode_failure_as_a_reply() {
|
||||
let mut request = self::request(DECODE, Limits::DEFAULT, "");
|
||||
|
||||
@@ -8,8 +8,10 @@ nothing.
|
||||
|---|---|
|
||||
| `sniff`, `is_eps` | EPS (binary header, or `%!PS-Adobe-… EPSF-…`), plain PostScript or PDF, and whether it is an Illustrator file: PostScript-based (`%AI…` comments, or Illustrator as `%%Creator`) or PDF-based (the `AIPrivateData` / `AIPDFPrivateData` keys near the start or end of the PDF, a hint only) |
|
||||
| `sections` | The parts behind the binary header ("DOS EPS"): the PostScript, the TIFF preview, the Windows metafile preview. A header that points outside the file is an error |
|
||||
| `postscript_section` | The PostScript program alone; a damaged preview does not prevent a reader from using a valid program section |
|
||||
| `wrap` | PostScript and its previews behind a binary header, as `sections` reads them back |
|
||||
| `Comments::read` | `%%BoundingBox` and `%%HiResBoundingBox` (the first in the header; `(atend)` taken from the trailer, outside embedded documents), `%%Pages`, `%%LanguageLevel`, `%%Creator`, `%%Title` |
|
||||
| `Comments::read_with_header_limit` | The same comments with a caller-selected header scan limit capped at 256 KiB; `read` defaults to 64 KiB |
|
||||
| `epsi` | The device-independent preview (`%%BeginPreview`, 1, 2, 4 or 8 bits) as an 8-bit grey bitmap |
|
||||
| `ascii85::encode`, `ascii85::decode` | The ASCII85 data encoding; lines never start with `%`, decoding is bounded |
|
||||
|
||||
|
||||
+26
-10
@@ -27,6 +27,27 @@ pub struct Sections<'a> {
|
||||
pub metafile: Option<&'a [u8]>,
|
||||
}
|
||||
|
||||
fn word(bytes: &[u8], at: usize) -> Result<usize, Error> {
|
||||
let end = at.checked_add(4).ok_or(Error::OutsideFile)?;
|
||||
let b = bytes.get(at..end).and_then(|b| <[u8; 4]>::try_from(b).ok()).ok_or(Error::OutsideFile)?;
|
||||
usize::try_from(u32::from_le_bytes(b)).map_err(|_| Error::OutsideFile)
|
||||
}
|
||||
|
||||
fn part(bytes: &[u8], at: usize) -> Result<&[u8], Error> {
|
||||
let len_at = at.checked_add(4).ok_or(Error::OutsideFile)?;
|
||||
let (start, len) = (word(bytes, at)?, word(bytes, len_at)?);
|
||||
start.checked_add(len).and_then(|end| bytes.get(start..end)).ok_or(Error::OutsideFile)
|
||||
}
|
||||
|
||||
/// The PostScript program in an EPS, ignoring preview ranges in its binary header.
|
||||
///
|
||||
/// This is useful to readers that execute only PostScript and historically accepted a valid
|
||||
/// program section even when a separate preview range was damaged. Use [`sections`] when every
|
||||
/// declared section must be validated.
|
||||
pub fn postscript_section(bytes: &[u8]) -> Result<&[u8], Error> {
|
||||
if bytes.starts_with(&DOS_MAGIC) { part(bytes, 4) } else { Ok(bytes) }
|
||||
}
|
||||
|
||||
/// The sections of an EPS file: its PostScript and previews behind a binary header, or the whole
|
||||
/// file as PostScript. A header that points outside the file is an error, whichever section it
|
||||
/// is: such a file is cut short or not an EPS.
|
||||
@@ -34,17 +55,12 @@ pub fn sections(bytes: &[u8]) -> Result<Sections<'_>, Error> {
|
||||
if !bytes.starts_with(&DOS_MAGIC) {
|
||||
return Ok(Sections { postscript: bytes, tiff: None, metafile: None });
|
||||
}
|
||||
let word = |at: usize| -> Result<usize, Error> {
|
||||
let b = bytes.get(at..at.saturating_add(4)).and_then(|b| <[u8; 4]>::try_from(b).ok()).ok_or(Error::OutsideFile)?;
|
||||
usize::try_from(u32::from_le_bytes(b)).map_err(|_| Error::OutsideFile)
|
||||
};
|
||||
let part = |at: usize| -> Result<&[u8], Error> {
|
||||
let (start, len) = (word(at)?, word(at + 4)?);
|
||||
start.checked_add(len).and_then(|end| bytes.get(start..end)).ok_or(Error::OutsideFile)
|
||||
};
|
||||
// A preview with no offset or no length is absent.
|
||||
let preview = |at: usize| -> Result<Option<&[u8]>, Error> { if word(at)? == 0 || word(at + 4)? == 0 { Ok(None) } else { part(at).map(Some) } };
|
||||
Ok(Sections { postscript: part(4)?, metafile: preview(12)?, tiff: preview(20)? })
|
||||
let preview = |at: usize| -> Result<Option<&[u8]>, Error> {
|
||||
let len_at = at.checked_add(4).ok_or(Error::OutsideFile)?;
|
||||
if word(bytes, at)? == 0 || word(bytes, len_at)? == 0 { Ok(None) } else { part(bytes, at).map(Some) }
|
||||
};
|
||||
Ok(Sections { postscript: postscript_section(bytes)?, metafile: preview(12)?, tiff: preview(20)? })
|
||||
}
|
||||
|
||||
/// `postscript` behind a binary header, followed by its previews. With neither preview the
|
||||
|
||||
+13
-1
@@ -2,6 +2,8 @@
|
||||
|
||||
/// Bytes of a file's start read for its header comments.
|
||||
pub const MAX_HEADER: usize = 64 << 10;
|
||||
/// Largest header scan available for readers preserving a wider historical bound.
|
||||
pub const MAX_EXTENDED_HEADER: usize = 256 << 10;
|
||||
/// Bytes of a file's end read for comments its header defers there with `(atend)`.
|
||||
pub const MAX_TRAILER: usize = 64 << 10;
|
||||
|
||||
@@ -34,7 +36,17 @@ impl Comments {
|
||||
/// A bounding box the header defers with `(atend)` is the last one of the trailer, outside
|
||||
/// any document the file embeds.
|
||||
pub fn read(ps: &[u8]) -> Self {
|
||||
let head = String::from_utf8_lossy(ps.get(..MAX_HEADER).unwrap_or(ps));
|
||||
Self::read_with_header_limit(ps, MAX_HEADER)
|
||||
}
|
||||
|
||||
/// Read comments with a caller-selected scan limit for the header.
|
||||
///
|
||||
/// The limit is capped at [`MAX_EXTENDED_HEADER`], and the trailer remains capped at
|
||||
/// [`MAX_TRAILER`]. This lets readers preserve a previously supported wider scan without
|
||||
/// making header or trailer work unbounded.
|
||||
pub fn read_with_header_limit(ps: &[u8], header_limit: usize) -> Self {
|
||||
let header_limit = header_limit.min(MAX_EXTENDED_HEADER).min(ps.len());
|
||||
let head = String::from_utf8_lossy(&ps[..header_limit]);
|
||||
let mut c = Comments::default();
|
||||
let (mut box_atend, mut hires_atend) = (false, false);
|
||||
// Lines may end in `\r` alone (classic Mac files).
|
||||
|
||||
@@ -25,8 +25,8 @@ pub mod postscript;
|
||||
mod preview;
|
||||
mod rectangles;
|
||||
|
||||
pub use container::{DOS_HEADER, DOS_MAGIC, Sections, sections, wrap};
|
||||
pub use dsc::{Comments, MAX_HEADER, MAX_TRAILER};
|
||||
pub use container::{DOS_HEADER, DOS_MAGIC, Sections, postscript_section, sections, wrap};
|
||||
pub use dsc::{Comments, MAX_EXTENDED_HEADER, MAX_HEADER, MAX_TRAILER};
|
||||
pub use embedded::{EmbeddedFontProgram, embedded_font_programs};
|
||||
pub use preview::{Bitmap, MAX_EPSI, epsi};
|
||||
pub use rectangles::{MAX_SIMPLE_RECTANGLE_SAVE_DEPTH, MAX_SIMPLE_RECTANGLE_TOKENS, MAX_SIMPLE_RECTANGLES, Rectangle, simple_rectangles};
|
||||
|
||||
@@ -29,6 +29,24 @@ fn sections_of_plain_and_binary_files() {
|
||||
assert_eq!(sections(&file), Ok(Sections { postscript: EPS, tiff: Some(b"TIFF"), metafile: Some(b"WMF") }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postscript_section_survives_a_damaged_preview_range() {
|
||||
let ps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 10 10\n";
|
||||
let mut bytes = wrap(ps, Some(b"tiff"), None).unwrap();
|
||||
bytes[20..24].copy_from_slice(&u32::MAX.to_le_bytes());
|
||||
bytes[24..28].copy_from_slice(&1u32.to_le_bytes());
|
||||
assert_eq!(postscript_section(&bytes), Ok(ps.as_slice()));
|
||||
assert_eq!(sections(&bytes), Err(Error::OutsideFile));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extended_comment_scan_keeps_its_upper_bound() {
|
||||
let mut ps = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec();
|
||||
ps.resize(ps.len() + MAX_EXTENDED_HEADER + 1, b' ');
|
||||
ps.extend_from_slice(b"\n%%BoundingBox: 0 0 20 10\n%%EndComments\n");
|
||||
assert_eq!(Comments::read_with_header_limit(&ps, usize::MAX).page_box(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_header_that_points_outside_the_file_is_refused() {
|
||||
let file = dos(EPS, b"WMF", b"TIFF");
|
||||
|
||||
@@ -12,8 +12,10 @@ use crate::{Error, corrupt};
|
||||
const MAX_META: u64 = 1 << 20;
|
||||
/// Largest file [`write_archive`] writes (the canvas and the images together).
|
||||
const MAX_FILE: u64 = 2 * 1024 * 1024 * 1024;
|
||||
/// Most entries an archive may declare; checked before the archive is indexed.
|
||||
pub const MAX_ENTRIES: u64 = 1_000_000;
|
||||
/// Most entries an archive may declare; checked before the archive is indexed. The archive holds one
|
||||
/// entry per image, so 100 000 leaves room for far more than a `.fig` file carries, and a header
|
||||
/// count can no longer make the index alone cost a hundred megabytes.
|
||||
pub const MAX_ENTRIES: u64 = 100_000;
|
||||
|
||||
/// What one open may read from `images/`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
|
||||
@@ -194,6 +194,45 @@ pub fn animation(width: u32, height: u32, frames: &[RgbaFrame<'_>], options: Opt
|
||||
writer.finish()
|
||||
}
|
||||
|
||||
/// Encode a single pre-quantized indexed-color frame as a GIF89a image.
|
||||
///
|
||||
/// `palette` holds 1 to 256 RGB triplets; `indices` is `width * height` row-major palette indices.
|
||||
/// When `interlaced` is true, rows are reordered into GIF's 4-pass interlaced order before writing.
|
||||
pub fn encode_indexed(
|
||||
width: u32,
|
||||
height: u32,
|
||||
palette: &[[u8; 3]],
|
||||
indices: &[u8],
|
||||
transparent: Option<u8>,
|
||||
interlaced: bool,
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let (w, h) = dimensions(width, height)?;
|
||||
let expected = usize::from(w) * usize::from(h);
|
||||
if indices.len() != expected {
|
||||
return Err(Error::FrameLength { index: 0, actual: indices.len(), expected });
|
||||
}
|
||||
if palette.is_empty() || palette.len() > 256 {
|
||||
return Err(Error::Encode("GIF palette must hold 1 to 256 entries".into()));
|
||||
}
|
||||
let flat_palette: Vec<u8> = palette.iter().flat_map(|c| *c).collect();
|
||||
let buffer = if interlaced { std::borrow::Cow::Owned(interlace(indices, usize::from(w))) } else { std::borrow::Cow::Borrowed(indices) };
|
||||
let mut enc = gif::Encoder::new(Vec::new(), w, h, &flat_palette).map_err(Error::from)?;
|
||||
let frame = gif::Frame { width: w, height: h, buffer, transparent, interlaced, ..gif::Frame::default() };
|
||||
enc.write_frame(&frame).map_err(Error::from)?;
|
||||
enc.into_inner().map_err(Error::from)
|
||||
}
|
||||
|
||||
/// The rows of a `width`-wide image in GIF's interlaced order: every 8th row from 0, every 8th
|
||||
/// from 4, every 4th from 2, then every 2nd from 1.
|
||||
fn interlace(px: &[u8], width: usize) -> Vec<u8> {
|
||||
let rows: Vec<&[u8]> = px.chunks(width.max(1)).collect();
|
||||
[(0, 8), (4, 8), (2, 4), (1, 2)]
|
||||
.into_iter()
|
||||
.flat_map(|(start, step)| rows.iter().skip(start).step_by(step))
|
||||
.flat_map(|r| r.iter().copied())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
@@ -288,4 +327,17 @@ mod tests {
|
||||
assert_eq!(decoded.frames[0].rgba, red);
|
||||
assert_eq!(decoded.frames[1].rgba, blue);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_indexed_validates_and_writes_gif() {
|
||||
let palette = [[255, 0, 0], [0, 255, 0]];
|
||||
let indices = [0u8, 1, 1, 0, 0, 1, 1, 0, 0];
|
||||
let bytes = super::encode_indexed(3, 3, &palette, &indices, Some(1), true).unwrap();
|
||||
let info = crate::scan(&bytes, 4).unwrap();
|
||||
assert_eq!((info.width, info.height), (3, 3));
|
||||
assert_eq!(info.frames.len(), 1);
|
||||
assert_eq!(info.frames[0].transparent, Some(1));
|
||||
assert!(super::encode_indexed(2, 2, &[], &[0; 4], None, false).is_err());
|
||||
assert!(super::encode_indexed(2, 2, &palette, &[0; 3], None, false).is_err());
|
||||
}
|
||||
}
|
||||
+43
-6
@@ -11,7 +11,9 @@
|
||||
//!
|
||||
//! Input is treated as hostile: the header length and the picture size are capped by [`Limits`],
|
||||
//! and every scan line is validated before the float picture is allocated, so a short file cannot
|
||||
//! ask for a large allocation.
|
||||
//! ask for a large allocation. Whatever the limits, no buffer is requested past the target's
|
||||
//! largest allocation (`isize::MAX` bytes), so raised limits on a 32-bit target such as `wasm32`
|
||||
//! give [`Error::LimitExceeded`] instead of a panic.
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -42,6 +44,7 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
#[non_exhaustive]
|
||||
pub struct Limits {
|
||||
/// Most pixels (width × height) a picture may have; each takes 12 bytes decoded. Default 2^26.
|
||||
/// A picture over the target's largest allocation is refused at any setting.
|
||||
pub max_pixels: u64,
|
||||
/// Longest header read, in bytes (real ones are a few hundred). Default 64 KiB.
|
||||
pub max_header: usize,
|
||||
@@ -62,6 +65,27 @@ pub struct Picture {
|
||||
pub rgb: Vec<f32>,
|
||||
}
|
||||
|
||||
/// The largest allocation in bytes that a `Vec` or `String` can make on the target.
|
||||
const MAX_ALLOC_BYTES: u64 = isize::MAX as u64;
|
||||
|
||||
/// Bytes taken by `count` elements of `element_bytes` each, or `None` when the count overflows `u64`.
|
||||
fn checked_byte_len(count: u64, element_bytes: usize) -> Option<u64> {
|
||||
count.checked_mul(element_bytes as u64)
|
||||
}
|
||||
|
||||
/// `count` as a length when `count` elements of `element_bytes` each fit in `max_alloc_bytes`.
|
||||
fn alloc_len(count: u64, element_bytes: usize, max_alloc_bytes: u64) -> Option<usize> {
|
||||
checked_byte_len(count, element_bytes).filter(|&bytes| bytes <= max_alloc_bytes).and_then(|_| usize::try_from(count).ok())
|
||||
}
|
||||
|
||||
/// Bytes of `String::from_utf8_lossy(bytes)`: each invalid sequence becomes one U+FFFD (3 bytes).
|
||||
fn lossy_len(bytes: &[u8]) -> Option<u64> {
|
||||
bytes.utf8_chunks().try_fold(0u64, |n, chunk| {
|
||||
let invalid = if chunk.invalid().is_empty() { 0 } else { '\u{FFFD}'.len_utf8() };
|
||||
n.checked_add(chunk.valid().len() as u64)?.checked_add(invalid as u64)
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `bytes` starts like a Radiance picture.
|
||||
pub fn sniff(bytes: &[u8]) -> bool {
|
||||
bytes.starts_with(b"#?RADIANCE") || bytes.starts_with(b"#?RGBE")
|
||||
@@ -158,6 +182,11 @@ fn scanline(b: &[u8], pos: &mut usize, line: &mut [[u8; 4]]) -> Result<()> {
|
||||
|
||||
/// Decodes a Radiance picture to linear float RGB, top row first.
|
||||
pub fn decode(b: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
decode_within(b, limits, MAX_ALLOC_BYTES)
|
||||
}
|
||||
|
||||
/// [`decode`] with `max_alloc_bytes` as the largest allocation the target allows.
|
||||
fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result<Picture> {
|
||||
if !sniff(b) {
|
||||
return Err(Error::NotHdr);
|
||||
}
|
||||
@@ -166,7 +195,11 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
let line = |pos: &mut usize| -> Result<String> {
|
||||
let rest = b.get(*pos..).ok_or(Error::Malformed("truncated header"))?;
|
||||
let end = rest.iter().take(limits.max_header).position(|&c| c == b'\n').ok_or(Error::Malformed("truncated header"))?;
|
||||
let s = String::from_utf8_lossy(rest.get(..end).unwrap_or_default()).trim_end_matches('\r').to_string();
|
||||
let raw = rest.get(..end).unwrap_or_default();
|
||||
if lossy_len(raw).is_none_or(|n| n > max_alloc_bytes) {
|
||||
return Err(Error::LimitExceeded("header length"));
|
||||
}
|
||||
let s = String::from_utf8_lossy(raw).trim_end_matches('\r').to_string();
|
||||
*pos += end + 1;
|
||||
Ok(s)
|
||||
};
|
||||
@@ -188,8 +221,9 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
}
|
||||
let res = line(&mut pos)?;
|
||||
let bad = Error::Malformed("bad resolution line");
|
||||
let t: Vec<&str> = res.split_whitespace().collect();
|
||||
let &[a1, n1, a2, n2] = t.as_slice().first_chunk::<4>().ok_or(bad.clone())?;
|
||||
// Tokens after the fourth are ignored, so they are not collected.
|
||||
let mut t = res.split_whitespace();
|
||||
let (Some(a1), Some(n1), Some(a2), Some(n2)) = (t.next(), t.next(), t.next(), t.next()) else { return Err(bad) };
|
||||
let ((x1, p1), (x2, p2)) = (axis(a1).ok_or(bad.clone())?, axis(a2).ok_or(bad.clone())?);
|
||||
if x1 == x2 {
|
||||
return Err(Error::Malformed("resolution line names one axis twice"));
|
||||
@@ -205,6 +239,10 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
if pixels > limits.max_pixels {
|
||||
return Err(Error::LimitExceeded("pixel count"));
|
||||
}
|
||||
// One scan line of RGBE pixels and the float picture, each within the target's largest allocation.
|
||||
let scan_len = alloc_len(u64::from(if x1 { height } else { width }), size_of::<[u8; 4]>(), max_alloc_bytes);
|
||||
let rgb_len = pixels.checked_mul(3).and_then(|floats| alloc_len(floats, size_of::<f32>(), max_alloc_bytes));
|
||||
let (Some(scan_len), Some(total)) = (scan_len, rgb_len) else { return Err(Error::LimitExceeded("pixel count")) };
|
||||
// A scan line takes at least a few bits of file, so a header cannot ask for more than the data has.
|
||||
if (b.len().saturating_sub(pos) as u64).saturating_mul(4096) < pixels {
|
||||
return Err(Error::Malformed("truncated pixel data"));
|
||||
@@ -212,12 +250,11 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result<Picture> {
|
||||
// Validate every encoded scan line before allocating the float picture: a short file could
|
||||
// otherwise ask for the whole limit of zeroed pixels and only then fail on its first line.
|
||||
let pixel_data = pos;
|
||||
let mut scan = vec![[0u8; 4]; n2];
|
||||
let mut scan = vec![[0u8; 4]; scan_len];
|
||||
for _ in 0..n1 {
|
||||
scanline(b, &mut pos, &mut scan)?;
|
||||
}
|
||||
|
||||
let total = w.checked_mul(h).and_then(|n| n.checked_mul(3)).ok_or(Error::LimitExceeded("pixel count"))?;
|
||||
let mut rgb = vec![0f32; total];
|
||||
pos = pixel_data;
|
||||
for i in 0..n1 {
|
||||
|
||||
@@ -142,3 +142,57 @@ fn limits_are_checked_before_allocating() {
|
||||
assert_eq!(decode(&long, &Limits { max_header: 8, ..Limits::default() }), Err(Error::Malformed("truncated header")));
|
||||
assert_eq!((Limits::default().max_pixels, Limits::default().max_header), (1 << 26, 64 << 10));
|
||||
}
|
||||
|
||||
/// `isize::MAX` on a 32-bit target such as `wasm32`.
|
||||
const BOUND_32: u64 = i32::MAX as u64;
|
||||
|
||||
#[test]
|
||||
fn buffer_lengths_respect_target_isize_bound() {
|
||||
// A scan line of RGBE pixels, 4 bytes each.
|
||||
assert_eq!(alloc_len(536_870_911, 4, BOUND_32), Some(536_870_911));
|
||||
assert_eq!(alloc_len(536_870_912, 4, BOUND_32), None);
|
||||
assert_eq!(alloc_len(u64::from(u32::MAX), 4, BOUND_32), None);
|
||||
// The float picture: 3 floats of 4 bytes a pixel.
|
||||
assert_eq!(alloc_len(178_956_970 * 3, 4, BOUND_32), Some(178_956_970 * 3));
|
||||
assert_eq!(alloc_len(178_956_971 * 3, 4, BOUND_32), None);
|
||||
// The default pixel limit fits a 32-bit target.
|
||||
assert_eq!(alloc_len(Limits::default().max_pixels * 3, 4, BOUND_32), Some(3 << 26));
|
||||
assert_eq!(checked_byte_len(u64::MAX, 4), None);
|
||||
assert_eq!(alloc_len(u64::MAX, 1, MAX_ALLOC_BYTES), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raised_pixel_limit_stops_at_the_target_bound() {
|
||||
let open = Limits { max_pixels: u64::MAX, ..Limits::default() };
|
||||
// 2 × 2 needs an 8-byte scan line and a 48-byte float picture.
|
||||
let ok = file("-Y 2 +X 2", &[0; 16]);
|
||||
assert!(decode_within(&ok, &open, 48).is_ok());
|
||||
assert_eq!(decode_within(&ok, &open, 47), Err(Error::LimitExceeded("pixel count")));
|
||||
// A 1 × 536,870,912 picture is past the 32-bit bound in both buffers; refused before
|
||||
// anything is allocated, ahead of the scan-line validation.
|
||||
let wide = file("-Y 1 +X 536870912", &[0; 131_072]);
|
||||
assert_eq!(decode_within(&wide, &open, BOUND_32), Err(Error::LimitExceeded("pixel count")));
|
||||
// 178,956,971 one-pixel scan lines fit the scan buffer but not the float picture.
|
||||
let tall = file("-Y 178956971 +X 1", &[0; 43_692]);
|
||||
assert_eq!(decode_within(&tall, &open, BOUND_32), Err(Error::LimitExceeded("pixel count")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_lines_are_measured_after_lossy_conversion() {
|
||||
// `#` and ten bytes that are not UTF-8: 11 bytes in the file, 31 as text.
|
||||
let mut f = b"#?RADIANCE\n#".to_vec();
|
||||
f.extend_from_slice(&[0xff; 10]);
|
||||
f.extend_from_slice(b"\n\n-Y 1 +X 1\n\0\0\0\0");
|
||||
assert_eq!(lossy_len(&f[11..22]), Some(31));
|
||||
assert!(decode_within(&f, &Limits::default(), 31).is_ok());
|
||||
assert_eq!(decode_within(&f, &Limits::default(), 30), Err(Error::LimitExceeded("header length")));
|
||||
assert_eq!(lossy_len(b"ok \xe2\x82"), Some(6));
|
||||
assert_eq!(lossy_len(b""), Some(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolution_tokens_after_the_fourth_are_ignored() {
|
||||
let f = dec(&file("-Y 1 +X 1 a b c d e f", &[0; 4])).unwrap();
|
||||
assert_eq!((f.width, f.height), (1, 1));
|
||||
assert_eq!(dec(&file("-Y 1 +X", &[0; 4])), Err(Error::Malformed("bad resolution line")));
|
||||
}
|
||||
+12
-1
@@ -39,7 +39,10 @@ pub mod value;
|
||||
|
||||
pub use craft_xml::{El, Local, Node};
|
||||
pub use package::{Document, Limits, Package, write_package};
|
||||
pub use part::{CONTAINER_XML, Props, designmap_xml, parse_part, part_root, part_xml, prop, with_props, xml_limits};
|
||||
pub use part::{
|
||||
CONTAINER_XML, Props, designmap_include, designmap_root, designmap_xml, designmap_xml_with_pi, parse_part, part_root, part_xml, prop, with_props,
|
||||
xml_limits,
|
||||
};
|
||||
|
||||
/// The IDML package mimetype (content of the first, stored `mimetype` entry).
|
||||
pub const MIMETYPE: &str = "application/vnd.adobe.indesign-idml-package";
|
||||
@@ -48,6 +51,14 @@ pub const DOM_VERSION: &str = "16.0";
|
||||
/// The namespace of the `idPkg` prefix.
|
||||
pub const PACKAGING_NS: &str = "http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging";
|
||||
|
||||
/// Writes the bounded XMP metadata packet used by an IDML package. Dates are already normalized
|
||||
/// ISO-8601 values supplied by the caller; this function owns the XML vocabulary and envelope.
|
||||
pub fn xmp_metadata(create_date: &str, modify_date: &str) -> String {
|
||||
format!(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <rdf:Description rdf:about=\"\" xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\">\n <xmp:CreateDate>{create_date}</xmp:CreateDate>\n <xmp:ModifyDate>{modify_date}</xmp:ModifyDate>\n </rdf:Description>\n </rdf:RDF>\n</x:xmpmeta>\n"
|
||||
)
|
||||
}
|
||||
|
||||
/// Why a package was not read or not written.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -0,0 +1,525 @@
|
||||
//! The `Document` tree: root values and included designmap nodes in source order.
|
||||
|
||||
use craft_xml::El;
|
||||
|
||||
use super::{AlternatingFillValues, AlternatingStrokeValues, Spread, Story, StrokeValues, Values};
|
||||
|
||||
/// One IDML table-of-contents style entry before document style names are resolved.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TocStyleEntryValues {
|
||||
pub style: Option<String>,
|
||||
pub level: Option<u8>,
|
||||
pub page_number_position: Option<String>,
|
||||
pub separator: Option<String>,
|
||||
pub entry_style: Option<String>,
|
||||
pub page_number_style: Option<String>,
|
||||
pub separator_style: Option<String>,
|
||||
pub sort: Option<bool>,
|
||||
}
|
||||
|
||||
/// An IDML table-of-contents style and its entry grammar.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TocStyleValues {
|
||||
pub self_id: Option<String>,
|
||||
pub title: Option<String>,
|
||||
pub title_style: Option<String>,
|
||||
pub create_bookmarks: Option<bool>,
|
||||
pub entries: Vec<TocStyleEntryValues>,
|
||||
}
|
||||
|
||||
/// Date-based IDML text variable kinds.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TextDateKind {
|
||||
Creation,
|
||||
Modification,
|
||||
Output,
|
||||
}
|
||||
|
||||
/// Semantically classified `TextVariable` definitions before caller model mapping.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum TextVariableKind {
|
||||
CustomText { text: String },
|
||||
LastPageNumber { section: bool },
|
||||
ChapterNumber,
|
||||
FileName { extension: bool },
|
||||
Date { kind: TextDateKind, format: String },
|
||||
RunningHeader { style: String, character: bool, last_on_page: bool },
|
||||
CrossReference { page_number: bool },
|
||||
Imported { variable_type: String, settings_element: String, settings: Vec<(String, String)> },
|
||||
}
|
||||
|
||||
/// Parsed values from an IDML `TextVariable` definition.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TextVariableValues {
|
||||
pub self_id: Option<String>,
|
||||
pub name: String,
|
||||
pub before: String,
|
||||
pub after: String,
|
||||
pub kind: TextVariableKind,
|
||||
}
|
||||
|
||||
/// A recognized IDML cross-reference format building block.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum CrossReferenceBuildingBlock {
|
||||
CustomText(String),
|
||||
FullParagraph,
|
||||
ParagraphText,
|
||||
ParagraphNumber,
|
||||
PageNumber,
|
||||
BookmarkName,
|
||||
ChapterNumber,
|
||||
FileName,
|
||||
PartialParagraph { delimiter: String, include_delimiter: bool },
|
||||
}
|
||||
|
||||
/// Parsed IDML cross-reference format definition.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct CrossReferenceFormatValues {
|
||||
pub self_id: Option<String>,
|
||||
pub name: String,
|
||||
pub blocks: Vec<CrossReferenceBuildingBlock>,
|
||||
}
|
||||
|
||||
impl CrossReferenceFormatValues {
|
||||
/// Reads a `CrossReferenceFormat` and recognizes its building-block grammar.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
let mut result =
|
||||
Self { self_id: values.self_id().map(str::to_owned), name: values.get("Name").unwrap_or("Format").to_owned(), blocks: Vec::new() };
|
||||
for block in el.elements().filter(|child| child.local() == "BuildingBlock") {
|
||||
let values = Values::of(block);
|
||||
let text = |key: &str| values.get(key).filter(|value| *value != "$ID/").unwrap_or("").to_owned();
|
||||
let parsed = match values.get("BlockType").unwrap_or("") {
|
||||
"CustomStringBuildingBlock" => Some(CrossReferenceBuildingBlock::CustomText(text("CustomText"))),
|
||||
"FullParagraphBuildingBlock" => Some(CrossReferenceBuildingBlock::FullParagraph),
|
||||
"ParagraphTextBuildingBlock" => Some(CrossReferenceBuildingBlock::ParagraphText),
|
||||
"ParagraphNumberBuildingBlock" => Some(CrossReferenceBuildingBlock::ParagraphNumber),
|
||||
"PageNumberBuildingBlock" => Some(CrossReferenceBuildingBlock::PageNumber),
|
||||
"BookmarkNameBuildingBlock" => Some(CrossReferenceBuildingBlock::BookmarkName),
|
||||
"ChapterNumberBuildingBlock" => Some(CrossReferenceBuildingBlock::ChapterNumber),
|
||||
"FileNameBuildingBlock" => Some(CrossReferenceBuildingBlock::FileName),
|
||||
"PartialParagraphBuildingBlock" => Some(CrossReferenceBuildingBlock::PartialParagraph {
|
||||
delimiter: text("AppliedDelimiter"),
|
||||
include_delimiter: values.get("IncludeDelimiter") == Some("true"),
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(parsed) = parsed {
|
||||
result.blocks.push(parsed);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Builds an IDML cross-reference format from DesignCraft's stored token definition.
|
||||
pub fn to_el(self_id: &str, name: &str, definition: &str) -> El {
|
||||
let mut root = El::new("CrossReferenceFormat").with_attr("Self", self_id).with_attr("Name", name).with_attr("AppliedCharacterStyle", "n");
|
||||
let mut index = 0;
|
||||
let mut block = |root: &mut El, kind: &str, text: &str, delimiter: &str, include: bool| {
|
||||
root.push(
|
||||
El::new("BuildingBlock")
|
||||
.with_attr("Self", format!("{self_id}BuildingBlock{index}"))
|
||||
.with_attr("BlockType", kind)
|
||||
.with_attr("AppliedCharacterStyle", "n")
|
||||
.with_attr("CustomText", if text.is_empty() { "$ID/" } else { text })
|
||||
.with_attr("AppliedDelimiter", if delimiter.is_empty() { "$ID/" } else { delimiter })
|
||||
.with_attr("IncludeDelimiter", if include { "true" } else { "false" }),
|
||||
);
|
||||
index += 1;
|
||||
};
|
||||
let mut rest = definition;
|
||||
while let Some(start) = rest.find('<') {
|
||||
let Some(end) = rest[start..].find('>') else { break };
|
||||
if start > 0 {
|
||||
block(&mut root, "CustomStringBuildingBlock", &rest[..start], "", false);
|
||||
}
|
||||
let tag = rest[start + 1..start + end].trim().trim_end_matches('/').trim();
|
||||
let attr =
|
||||
|key: &str| tag.find(&format!("{key}=\"")).and_then(|position| tag[position + key.len() + 2..].split('"').next()).unwrap_or("");
|
||||
match tag.split_whitespace().next().unwrap_or("") {
|
||||
"fullPara" => block(&mut root, "FullParagraphBuildingBlock", "", "", false),
|
||||
"paraText" => block(&mut root, "ParagraphTextBuildingBlock", "", "", false),
|
||||
"paraNum" => block(&mut root, "ParagraphNumberBuildingBlock", "", "", false),
|
||||
"pageNum" => block(&mut root, "PageNumberBuildingBlock", "", "", false),
|
||||
"txtAnchrName" => block(&mut root, "BookmarkNameBuildingBlock", "", "", false),
|
||||
"chapNum" => block(&mut root, "ChapterNumberBuildingBlock", "", "", false),
|
||||
"fileName" => block(&mut root, "FileNameBuildingBlock", "", "", false),
|
||||
"partialPara" => block(&mut root, "PartialParagraphBuildingBlock", "", attr("delim"), attr("includeDelim") == "true"),
|
||||
_ => block(&mut root, "CustomStringBuildingBlock", &rest[start..start + end + 1], "", false),
|
||||
}
|
||||
rest = &rest[start + end + 1..];
|
||||
}
|
||||
if !rest.is_empty() {
|
||||
block(&mut root, "CustomStringBuildingBlock", rest, "", false);
|
||||
}
|
||||
root
|
||||
}
|
||||
}
|
||||
|
||||
/// Parsed EPUB document-splitting preference.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct EpubSplitValues {
|
||||
pub break_document: bool,
|
||||
pub paragraph_style: Option<String>,
|
||||
}
|
||||
|
||||
/// Parsed IDML condition metadata.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ConditionValues {
|
||||
pub self_id: Option<String>,
|
||||
pub name: Option<String>,
|
||||
pub indicator_rgb: Option<[u8; 3]>,
|
||||
pub visible: bool,
|
||||
}
|
||||
|
||||
impl ConditionValues {
|
||||
/// Reads condition fields and its optional three-channel indicator color.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
let rgb: Vec<u8> = values
|
||||
.get("IndicatorColor")
|
||||
.unwrap_or("")
|
||||
.split_whitespace()
|
||||
.filter_map(|value| value.parse::<f64>().ok())
|
||||
.map(|value| value.clamp(0.0, 255.0) as u8)
|
||||
.collect();
|
||||
Self {
|
||||
self_id: values.self_id().map(str::to_owned),
|
||||
name: values.get("Name").map(str::to_owned),
|
||||
indicator_rgb: (rgb.len() == 3).then(|| [rgb[0], rgb[1], rgb[2]]),
|
||||
visible: values.get("Visible") != Some("false"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parsed IDML numbering-list metadata.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct NumberingListValues {
|
||||
pub name: String,
|
||||
pub continue_across_stories: bool,
|
||||
}
|
||||
|
||||
/// Parsed cell-style assignments that are independent of DesignCraft's style resolution.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct CellStyleValues {
|
||||
pub self_id: Option<String>,
|
||||
pub name: Option<String>,
|
||||
pub based_on: Option<String>,
|
||||
pub fill: Option<String>,
|
||||
pub fill_tint: Option<f64>,
|
||||
pub insets: [Option<f64>; 4],
|
||||
pub vertical_justification: Option<String>,
|
||||
pub first_baseline: Option<String>,
|
||||
pub first_baseline_min: Option<f64>,
|
||||
pub paragraph_style: Option<String>,
|
||||
pub edge_strokes: [StrokeValues; 4],
|
||||
}
|
||||
|
||||
impl CellStyleValues {
|
||||
/// Reads the cell-style value assignments and supports the older inset property names.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
let inset = |modern, legacy| values.num(modern).or_else(|| values.num(legacy)).map(|v| v.max(0.0));
|
||||
Self {
|
||||
self_id: values.self_id().map(str::to_owned),
|
||||
name: values.name().map(str::to_owned),
|
||||
based_on: values.prop("BasedOn"),
|
||||
fill: values.prop("FillColor"),
|
||||
fill_tint: values.num("FillTint"),
|
||||
insets: [
|
||||
inset("TextTopInset", "TopInset"),
|
||||
inset("TextLeftInset", "LeftInset"),
|
||||
inset("TextBottomInset", "BottomInset"),
|
||||
inset("TextRightInset", "RightInset"),
|
||||
],
|
||||
vertical_justification: values.get("VerticalJustification").map(str::to_owned),
|
||||
first_baseline: values.get("FirstBaselineOffset").map(str::to_owned),
|
||||
first_baseline_min: values.num("MinimumFirstBaselineOffset").filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 8640.0)),
|
||||
paragraph_style: values.get("AppliedParagraphStyle").map(str::to_owned),
|
||||
edge_strokes: ["TopEdge", "LeftEdge", "BottomEdge", "RightEdge"].map(|edge| values.stroke_values(edge)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parsed table-style assignments that are independent of DesignCraft's style resolution.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TableStyleValues {
|
||||
pub name: Option<String>,
|
||||
pub based_on: Option<String>,
|
||||
pub header_cell_style: Option<String>,
|
||||
pub body_cell_style: Option<String>,
|
||||
pub footer_cell_style: Option<String>,
|
||||
pub left_column_cell_style: Option<String>,
|
||||
pub right_column_cell_style: Option<String>,
|
||||
pub header_same_as_body: Option<bool>,
|
||||
pub footer_same_as_body: Option<bool>,
|
||||
pub left_column_same_as_body: Option<bool>,
|
||||
pub right_column_same_as_body: Option<bool>,
|
||||
pub space_before: Option<f64>,
|
||||
pub space_after: Option<f64>,
|
||||
pub border_strokes: [StrokeValues; 4],
|
||||
pub row_fills: AlternatingFillValues,
|
||||
pub column_fills: AlternatingFillValues,
|
||||
pub row_strokes: AlternatingStrokeValues,
|
||||
pub column_strokes: AlternatingStrokeValues,
|
||||
}
|
||||
|
||||
impl TableStyleValues {
|
||||
/// Reads table-style regions, inheritance, spacing, and region reuse values.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self {
|
||||
name: values.name().map(str::to_owned),
|
||||
based_on: values.prop("BasedOn"),
|
||||
header_cell_style: values.get("HeaderRegionCellStyle").map(str::to_owned),
|
||||
body_cell_style: values.get("BodyRegionCellStyle").map(str::to_owned),
|
||||
footer_cell_style: values.get("FooterRegionCellStyle").map(str::to_owned),
|
||||
left_column_cell_style: values.get("LeftColumnRegionCellStyle").map(str::to_owned),
|
||||
right_column_cell_style: values.get("RightColumnRegionCellStyle").map(str::to_owned),
|
||||
header_same_as_body: values.boolean("HeaderRegionSameAsBodyRegion"),
|
||||
footer_same_as_body: values.boolean("FooterRegionSameAsBodyRegion"),
|
||||
left_column_same_as_body: values.boolean("LeftColumnRegionSameAsBodyRegion"),
|
||||
right_column_same_as_body: values.boolean("RightColumnRegionSameAsBodyRegion"),
|
||||
space_before: values.num("SpaceBefore"),
|
||||
space_after: values.num("SpaceAfter"),
|
||||
border_strokes: ["TopBorder", "LeftBorder", "BottomBorder", "RightBorder"].map(|border| values.stroke_values(border)),
|
||||
row_fills: values.alternating_fill_values("Row"),
|
||||
column_fills: values.alternating_fill_values("Column"),
|
||||
row_strokes: values.alternating_stroke_values("Row"),
|
||||
column_strokes: values.alternating_stroke_values("Column"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NumberingListValues {
|
||||
/// Reads a `NumberingList` element.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self {
|
||||
name: values.get("Name").unwrap_or("").to_owned(),
|
||||
continue_across_stories: values.boolean("ContinueNumbersAcrossStories") == Some(true),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EpubSplitValues {
|
||||
/// Reads an `EPubExportPreference` element.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self { break_document: values.boolean("BreakDocument") == Some(true), paragraph_style: values.get("ParagraphStyleName").map(str::to_owned) }
|
||||
}
|
||||
}
|
||||
|
||||
impl TextVariableValues {
|
||||
/// Reads a `TextVariable` and its preference record.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
let preference = el.elements().find(|child| child.name.ends_with("Preference"));
|
||||
let attr = |key: &str| preference.and_then(|child| child.attr(key)).unwrap_or("");
|
||||
let variable_type = values.get("VariableType").unwrap_or("");
|
||||
let date = |kind| TextVariableKind::Date { kind, format: if attr("Format").is_empty() { "MM/dd/yy".into() } else { attr("Format").into() } };
|
||||
let kind = match variable_type {
|
||||
"CustomTextType" => TextVariableKind::CustomText { text: attr("Contents").into() },
|
||||
"LastPageNumberType" => TextVariableKind::LastPageNumber { section: attr("Scope") == "SectionScope" },
|
||||
"ChapterNumberType" => TextVariableKind::ChapterNumber,
|
||||
"FileNameType" if attr("IncludePath") != "true" => TextVariableKind::FileName { extension: attr("IncludeExtension") == "true" },
|
||||
"CreationDateType" => date(TextDateKind::Creation),
|
||||
"ModificationDateType" => date(TextDateKind::Modification),
|
||||
"OutputDateType" => date(TextDateKind::Output),
|
||||
"MatchParagraphStyleType" | "MatchCharacterStyleType" => {
|
||||
let character = variable_type == "MatchCharacterStyleType";
|
||||
let style = attr(if character { "AppliedCharacterStyle" } else { "AppliedParagraphStyle" });
|
||||
if style.is_empty() {
|
||||
TextVariableKind::Imported {
|
||||
variable_type: variable_type.into(),
|
||||
settings_element: preference.map(|child| child.name.clone()).unwrap_or_default(),
|
||||
settings: preference.map(|child| child.attrs.clone()).unwrap_or_default(),
|
||||
}
|
||||
} else {
|
||||
TextVariableKind::RunningHeader { style: style.into(), character, last_on_page: attr("SearchStrategy") == "LastOnPage" }
|
||||
}
|
||||
}
|
||||
_ if variable_type.starts_with("Xref") => TextVariableKind::CrossReference { page_number: variable_type == "XrefPageNumberType" },
|
||||
_ => TextVariableKind::Imported {
|
||||
variable_type: variable_type.into(),
|
||||
settings_element: preference.map(|child| child.name.clone()).unwrap_or_default(),
|
||||
settings: preference.map(|child| child.attrs.clone()).unwrap_or_default(),
|
||||
},
|
||||
};
|
||||
let text = |key: &str| preference.and_then(|child| child.attr(key)).or_else(|| values.get(key)).unwrap_or("").to_owned();
|
||||
Self {
|
||||
self_id: values.self_id().map(str::to_owned),
|
||||
name: values.get("Name").unwrap_or("").to_owned(),
|
||||
before: text("TextBefore"),
|
||||
after: text("TextAfter"),
|
||||
kind,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TocStyleValues {
|
||||
/// Reads a `TOCStyle` element and its bounded `TOCStyleEntry` children.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
let mut result = Self {
|
||||
self_id: values.self_id().map(str::to_owned),
|
||||
title: values.get("Title").map(str::to_owned),
|
||||
title_style: values.get("TitleStyle").map(str::to_owned),
|
||||
create_bookmarks: values.get("CreateBookmarks").or_else(|| values.get("CreatePdfBookmarks")).map(|value| value == "true"),
|
||||
entries: Vec::new(),
|
||||
};
|
||||
result.entries.extend(el.elements().filter(|entry| entry.local() == "TOCStyleEntry").take(64).map(|entry| {
|
||||
let values = Values::of(entry);
|
||||
TocStyleEntryValues {
|
||||
style: values.get("Name").map(str::to_owned),
|
||||
level: values.get("Level").and_then(|value| value.trim().parse().ok()),
|
||||
page_number_position: values.get("PageNumberPosition").map(str::to_owned),
|
||||
separator: values.get("Separator").map(str::to_owned),
|
||||
entry_style: values.prop("FormatStyle"),
|
||||
page_number_style: values.prop("PageNumberStyle"),
|
||||
separator_style: values.prop("SeparatorStyle"),
|
||||
sort: values.get("Sort").map(|value| value.trim() == "true"),
|
||||
}
|
||||
}));
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
/// The top-level IDML elements consumed by document importers.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TopLevelKind {
|
||||
DocumentPreference,
|
||||
TransparencyPreference,
|
||||
TextPreference,
|
||||
MarginPreference,
|
||||
ViewPreference,
|
||||
GridPreference,
|
||||
PasteboardPreference,
|
||||
CompositeFont,
|
||||
KinsokuTable,
|
||||
MojikumiTable,
|
||||
FootnoteOption,
|
||||
EpubExportPreference,
|
||||
Layer,
|
||||
CrossReferenceFormat,
|
||||
Index,
|
||||
TextVariable,
|
||||
Story,
|
||||
Hyperlink,
|
||||
HyperlinkUrlDestination,
|
||||
HyperlinkPageDestination,
|
||||
HyperlinkPageItemSource,
|
||||
HyperlinkOtherDestination,
|
||||
MasterSpread,
|
||||
Spread,
|
||||
Section,
|
||||
TocStyle,
|
||||
Condition,
|
||||
NumberingList,
|
||||
StyleGroup,
|
||||
Other,
|
||||
}
|
||||
|
||||
/// One included node in `designmap.xml`, classified by the shared IDML grammar.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TopLevelNode {
|
||||
pub kind: TopLevelKind,
|
||||
data: TopLevelData,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum TopLevelData {
|
||||
Story(Story),
|
||||
Spread(Spread),
|
||||
Raw(El),
|
||||
}
|
||||
|
||||
impl TopLevelNode {
|
||||
fn from_el(el: El) -> TopLevelNode {
|
||||
let kind = top_level_kind(el.local());
|
||||
let data = match kind {
|
||||
TopLevelKind::Story => TopLevelData::Story(Story::from_el(&el)),
|
||||
TopLevelKind::Spread | TopLevelKind::MasterSpread => TopLevelData::Spread(Spread::from_el(&el)),
|
||||
_ => TopLevelData::Raw(el),
|
||||
};
|
||||
TopLevelNode { kind, data }
|
||||
}
|
||||
|
||||
/// The typed story or spread tree when this is one of those parts.
|
||||
pub fn story(&self) -> Option<&Story> {
|
||||
if let TopLevelData::Story(story) = &self.data { Some(story) } else { None }
|
||||
}
|
||||
|
||||
pub fn spread(&self) -> Option<&Spread> {
|
||||
if let TopLevelData::Spread(spread) = &self.data { Some(spread) } else { None }
|
||||
}
|
||||
|
||||
/// The original element, reconstructed from the typed story/spread model where applicable.
|
||||
pub fn to_el(&self) -> El {
|
||||
match &self.data {
|
||||
TopLevelData::Story(story) => story.to_el(),
|
||||
TopLevelData::Spread(spread) => spread.to_el(),
|
||||
TopLevelData::Raw(el) => el.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn values(&self) -> Values {
|
||||
Values::of(&self.to_el())
|
||||
}
|
||||
}
|
||||
|
||||
/// A neutral view of an IDML package's designmap and included top-level parts.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ModelDocument {
|
||||
pub root: Values,
|
||||
pub top: Vec<TopLevelNode>,
|
||||
pub warnings: Vec<String>,
|
||||
pub metadata: Option<String>,
|
||||
}
|
||||
|
||||
impl ModelDocument {
|
||||
/// Converts the shared bounded package reader's parsed document into typed document nodes.
|
||||
pub fn from_package(document: crate::Document) -> ModelDocument {
|
||||
ModelDocument {
|
||||
root: Values::of(&document.root),
|
||||
top: document.top.into_iter().map(TopLevelNode::from_el).collect(),
|
||||
warnings: document.warnings,
|
||||
metadata: document.metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn top_level_kind(name: &str) -> TopLevelKind {
|
||||
match name {
|
||||
"DocumentPreference" => TopLevelKind::DocumentPreference,
|
||||
"TransparencyPreference" => TopLevelKind::TransparencyPreference,
|
||||
"TextPreference" => TopLevelKind::TextPreference,
|
||||
"MarginPreference" => TopLevelKind::MarginPreference,
|
||||
"ViewPreference" => TopLevelKind::ViewPreference,
|
||||
"GridPreference" => TopLevelKind::GridPreference,
|
||||
"PasteboardPreference" => TopLevelKind::PasteboardPreference,
|
||||
"CompositeFont" => TopLevelKind::CompositeFont,
|
||||
"KinsokuTable" => TopLevelKind::KinsokuTable,
|
||||
"MojikumiTable" => TopLevelKind::MojikumiTable,
|
||||
"FootnoteOption" => TopLevelKind::FootnoteOption,
|
||||
"EPubExportPreference" => TopLevelKind::EpubExportPreference,
|
||||
"Layer" => TopLevelKind::Layer,
|
||||
"CrossReferenceFormat" => TopLevelKind::CrossReferenceFormat,
|
||||
"Index" => TopLevelKind::Index,
|
||||
"TextVariable" => TopLevelKind::TextVariable,
|
||||
"Story" => TopLevelKind::Story,
|
||||
"Hyperlink" => TopLevelKind::Hyperlink,
|
||||
"HyperlinkURLDestination" => TopLevelKind::HyperlinkUrlDestination,
|
||||
"HyperlinkPageDestination" => TopLevelKind::HyperlinkPageDestination,
|
||||
"HyperlinkPageItemSource" => TopLevelKind::HyperlinkPageItemSource,
|
||||
"MasterSpread" => TopLevelKind::MasterSpread,
|
||||
"Spread" => TopLevelKind::Spread,
|
||||
"Section" => TopLevelKind::Section,
|
||||
"TOCStyle" => TopLevelKind::TocStyle,
|
||||
"Condition" => TopLevelKind::Condition,
|
||||
"NumberingList" => TopLevelKind::NumberingList,
|
||||
name if name.starts_with("Root") && name.ends_with("StyleGroup") => TopLevelKind::StyleGroup,
|
||||
name if name.starts_with("Hyperlink") && name.ends_with("Destination") => TopLevelKind::HyperlinkOtherDestination,
|
||||
_ => TopLevelKind::Other,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,404 @@
|
||||
//! The IDML transparency/effect settings found on page items and object styles.
|
||||
|
||||
use craft_xml::{El, Local, Node};
|
||||
|
||||
use super::Values;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct DropShadowValues {
|
||||
pub mode: Option<String>,
|
||||
pub x_offset: Option<f64>,
|
||||
pub y_offset: Option<f64>,
|
||||
pub color: Option<String>,
|
||||
pub opacity: Option<f64>,
|
||||
pub size: Option<f64>,
|
||||
pub spread: Option<f64>,
|
||||
pub blend_mode: Option<String>,
|
||||
}
|
||||
|
||||
impl DropShadowValues {
|
||||
pub fn to_el(&self) -> El {
|
||||
let mut element = El::new("DropShadowSetting");
|
||||
if let Some(value) = &self.mode {
|
||||
element.set_attr("Mode", value);
|
||||
}
|
||||
for (key, value) in
|
||||
[("XOffset", self.x_offset), ("YOffset", self.y_offset), ("Size", self.size), ("Spread", self.spread), ("Opacity", self.opacity)]
|
||||
{
|
||||
if let Some(value) = value {
|
||||
element.set_attr(key, crate::value::num(value));
|
||||
}
|
||||
}
|
||||
if let Some(value) = &self.blend_mode {
|
||||
element.set_attr("BlendMode", value);
|
||||
}
|
||||
if let Some(value) = &self.color {
|
||||
element.set_attr("EffectColor", value);
|
||||
}
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct FeatherValues {
|
||||
pub mode: Option<String>,
|
||||
pub width: Option<f64>,
|
||||
pub choke_amount: Option<f64>,
|
||||
pub corner_type: Option<String>,
|
||||
}
|
||||
|
||||
impl FeatherValues {
|
||||
pub fn to_el(&self) -> El {
|
||||
let mut element = El::new("FeatherSetting");
|
||||
if let Some(value) = &self.mode {
|
||||
element.set_attr("Mode", value);
|
||||
}
|
||||
for (key, value) in [("Width", self.width), ("ChokeAmount", self.choke_amount)] {
|
||||
if let Some(value) = value {
|
||||
element.set_attr(key, crate::value::num(value));
|
||||
}
|
||||
}
|
||||
if let Some(value) = &self.corner_type {
|
||||
element.set_attr("CornerType", value);
|
||||
}
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct OpacityStopValues {
|
||||
pub location: Option<f64>,
|
||||
pub opacity: Option<f64>,
|
||||
}
|
||||
|
||||
impl OpacityStopValues {
|
||||
pub fn to_el(&self, self_id: &str, midpoint: bool) -> El {
|
||||
let mut element = El::new("OpacityGradientStop").with_attr("Self", self_id);
|
||||
if let Some(value) = self.opacity {
|
||||
element.set_attr("Opacity", crate::value::num(value));
|
||||
}
|
||||
if let Some(value) = self.location {
|
||||
element.set_attr("Location", crate::value::num(value));
|
||||
}
|
||||
if midpoint {
|
||||
element.set_attr("Midpoint", "50");
|
||||
}
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct GradientFeatherValues {
|
||||
pub applied: bool,
|
||||
pub radial: bool,
|
||||
pub angle: Option<f64>,
|
||||
pub length: Option<f64>,
|
||||
pub start: Option<[f64; 2]>,
|
||||
pub stops: Vec<OpacityStopValues>,
|
||||
}
|
||||
|
||||
impl GradientFeatherValues {
|
||||
pub fn to_el(&self, id_prefix: &str) -> El {
|
||||
let mut element =
|
||||
El::new("GradientFeatherSetting").with_attr("Applied", "true").with_attr("Type", if self.radial { "Radial" } else { "Linear" });
|
||||
if let Some(value) = self.angle {
|
||||
element.set_attr("Angle", crate::value::num(value));
|
||||
}
|
||||
if let Some(value) = self.length {
|
||||
element.set_attr("Length", crate::value::num(value));
|
||||
}
|
||||
if let Some([x, y]) = self.start {
|
||||
element.set_attr("GradientStart", format!("{} {}", crate::value::num(x), crate::value::num(y)));
|
||||
}
|
||||
for (index, stop) in self.stops.iter().enumerate() {
|
||||
let stop_id = format!("{id_prefix}TransparencySetting1GradientFeatherSetting1OpacityGradientStop{index}");
|
||||
element.push(stop.to_el(&stop_id, index > 0));
|
||||
}
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct DirectionalFeatherValues {
|
||||
pub applied: bool,
|
||||
pub widths: [Option<f64>; 4],
|
||||
}
|
||||
|
||||
impl DirectionalFeatherValues {
|
||||
pub fn to_el(&self) -> El {
|
||||
let mut element = El::new("DirectionalFeatherSetting").with_attr("Applied", "true");
|
||||
for (key, value) in
|
||||
[("LeftWidth", self.widths[1]), ("RightWidth", self.widths[3]), ("TopWidth", self.widths[0]), ("BottomWidth", self.widths[2])]
|
||||
{
|
||||
if let Some(value) = value {
|
||||
element.set_attr(key, crate::value::num(value));
|
||||
}
|
||||
}
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
/// A typed view of an IDML `TransparencySetting` and its effect elements.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TransparencySetting {
|
||||
pub blend_mode: Option<String>,
|
||||
pub opacity: Option<f64>,
|
||||
pub drop_shadow: Option<DropShadowValues>,
|
||||
pub feather: Option<FeatherValues>,
|
||||
pub gradient_feather: Option<GradientFeatherValues>,
|
||||
pub directional_feather: Option<DirectionalFeatherValues>,
|
||||
}
|
||||
|
||||
impl TransparencySetting {
|
||||
pub fn to_el(&self, id_prefix: &str) -> El {
|
||||
let mut element = El::new("TransparencySetting");
|
||||
if self.blend_mode.is_some() || self.opacity.is_some() {
|
||||
let mut blending = El::new("BlendingSetting");
|
||||
if let Some(value) = &self.blend_mode {
|
||||
blending.set_attr("BlendMode", value);
|
||||
}
|
||||
if let Some(value) = self.opacity {
|
||||
blending.set_attr("Opacity", crate::value::num(value));
|
||||
}
|
||||
element.push(blending);
|
||||
}
|
||||
if let Some(effect) = &self.drop_shadow {
|
||||
element.push(effect.to_el());
|
||||
}
|
||||
if let Some(effect) = &self.feather {
|
||||
element.push(effect.to_el());
|
||||
}
|
||||
if let Some(effect) = &self.directional_feather {
|
||||
element.push(effect.to_el());
|
||||
}
|
||||
if let Some(effect) = &self.gradient_feather {
|
||||
element.push(effect.to_el(id_prefix));
|
||||
}
|
||||
element
|
||||
}
|
||||
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let child = |name: &str| el.child(Local(name));
|
||||
let blending = child("BlendingSetting").map(Values::of);
|
||||
let drop_shadow = child("DropShadowSetting").map(|el| {
|
||||
let values = Values::of(el);
|
||||
DropShadowValues {
|
||||
mode: values.get("Mode").map(str::to_owned),
|
||||
x_offset: values.num("XOffset"),
|
||||
y_offset: values.num("YOffset"),
|
||||
color: values.get("EffectColor").map(str::to_owned),
|
||||
opacity: values.num("Opacity"),
|
||||
size: values.num("Size"),
|
||||
spread: values.num("Spread"),
|
||||
blend_mode: values.get("BlendMode").map(str::to_owned),
|
||||
}
|
||||
});
|
||||
let feather = child("FeatherSetting").map(|el| {
|
||||
let values = Values::of(el);
|
||||
FeatherValues {
|
||||
mode: values.get("Mode").map(str::to_owned),
|
||||
width: values.num("Width"),
|
||||
choke_amount: values.num("ChokeAmount"),
|
||||
corner_type: values.get("CornerType").map(str::to_owned),
|
||||
}
|
||||
});
|
||||
let gradient_feather = child("GradientFeatherSetting").map(|el| {
|
||||
let values = Values::of(el);
|
||||
let start = values
|
||||
.prop("GradientStart")
|
||||
.and_then(|value| value.split_whitespace().map(|part| part.parse::<f64>().ok().filter(|n| n.is_finite())).collect::<Option<Vec<_>>>())
|
||||
.and_then(|parts| <[f64; 2]>::try_from(parts).ok());
|
||||
GradientFeatherValues {
|
||||
applied: values.boolean("Applied") == Some(true),
|
||||
radial: values.get("Type") == Some("Radial"),
|
||||
angle: values.num("Angle"),
|
||||
length: values.num("Length"),
|
||||
start,
|
||||
stops: el
|
||||
.elements()
|
||||
.filter(|stop| stop.local() == "OpacityGradientStop")
|
||||
.map(|stop| {
|
||||
let values = Values::of(stop);
|
||||
OpacityStopValues { location: values.num("Location"), opacity: values.num("Opacity") }
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
});
|
||||
let directional_feather = child("DirectionalFeatherSetting").map(|el| {
|
||||
let values = Values::of(el);
|
||||
DirectionalFeatherValues {
|
||||
applied: values.boolean("Applied") == Some(true),
|
||||
widths: [values.num("TopWidth"), values.num("LeftWidth"), values.num("BottomWidth"), values.num("RightWidth")],
|
||||
}
|
||||
});
|
||||
Self {
|
||||
blend_mode: blending.as_ref().and_then(|values| values.get("BlendMode").map(str::to_owned)),
|
||||
opacity: blending.as_ref().and_then(|values| values.num("Opacity")),
|
||||
drop_shadow,
|
||||
feather,
|
||||
gradient_feather,
|
||||
directional_feather,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolved object-style chain used to inherit item-level IDML settings.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct ObjectStyleStack {
|
||||
chain: Vec<El>,
|
||||
}
|
||||
|
||||
impl ObjectStyleStack {
|
||||
/// Resolves a style reference and its `BasedOn` chain (nearest first, cycle-safe, max 16).
|
||||
pub fn from_styles(styles: &[El], reference: Option<&str>, include_none: bool) -> Self {
|
||||
let find = |reference: &str| {
|
||||
styles.iter().find(|style| Values::of(style).self_id() == Some(reference)).or_else(|| {
|
||||
let normalized = if reference.starts_with("ObjectStyle/") {
|
||||
reference.to_owned()
|
||||
} else {
|
||||
format!("ObjectStyle/{}", crate::value::escape_id(reference.trim()))
|
||||
};
|
||||
styles.iter().find(|style| Values::of(style).self_id() == Some(normalized.as_str()))
|
||||
})
|
||||
};
|
||||
let mut chain = Vec::new();
|
||||
let mut next = reference.map(str::to_owned);
|
||||
while let Some(id) = next {
|
||||
let Some(style) = find(&id) else { break };
|
||||
let self_id = Values::of(style).self_id().map(str::to_owned);
|
||||
if chain.len() >= 16 || self_id.as_deref().is_some_and(|id| chain.iter().any(|entry: &El| Values::of(entry).self_id() == Some(id))) {
|
||||
break;
|
||||
}
|
||||
chain.push(style.clone());
|
||||
next = Values::of(style).prop("BasedOn").map(|base| {
|
||||
let base = base.trim();
|
||||
if base.starts_with("ObjectStyle/") { base.to_owned() } else { format!("ObjectStyle/{}", crate::value::escape_id(base)) }
|
||||
});
|
||||
}
|
||||
if include_none
|
||||
&& !chain.iter().any(|style| Values::of(style).self_id() == Some("ObjectStyle/$ID/[None]"))
|
||||
&& let Some(none) = find("ObjectStyle/$ID/[None]")
|
||||
{
|
||||
chain.push(none.clone());
|
||||
}
|
||||
Self { chain }
|
||||
}
|
||||
|
||||
/// The resolved style elements, nearest first.
|
||||
pub fn styles(&self) -> &[El] {
|
||||
&self.chain
|
||||
}
|
||||
|
||||
/// Merges an item's child over the corresponding elements in the resolved style chain.
|
||||
pub fn merged_child(&self, item: &El, name: &str) -> Option<El> {
|
||||
let category = match name {
|
||||
"TransparencySetting" => Some("ObjectStyleObjectEffectsCategorySettings"),
|
||||
"StrokeTransparencySetting" => Some("ObjectStyleStrokeEffectsCategorySettings"),
|
||||
"FillTransparencySetting" => Some("ObjectStyleFillEffectsCategorySettings"),
|
||||
"ContentTransparencySetting" => Some("ObjectStyleContentEffectsCategorySettings"),
|
||||
_ => None,
|
||||
};
|
||||
let enabled = |effect: &str| {
|
||||
let (Some(category), Some(flag)) = (category, effect_flag(effect)) else { return true };
|
||||
self.chain.iter().find_map(|style| style.child(Local(category)).and_then(|c| Values::of(c).get(flag).map(str::to_owned))).as_deref()
|
||||
!= Some("false")
|
||||
};
|
||||
let mut result = None;
|
||||
for style in self.chain.iter().rev() {
|
||||
let Some(child) = style.child(Local(name)) else { continue };
|
||||
let mut child = child.clone();
|
||||
child.children.retain(|node| !matches!(node, Node::El(element) if !enabled(element.local())));
|
||||
match &mut result {
|
||||
Some(current) => overlay(current, &child, 0),
|
||||
None => result = Some(child),
|
||||
}
|
||||
}
|
||||
if let Some(child) = item.child(Local(name)) {
|
||||
match &mut result {
|
||||
Some(current) => overlay(current, child, 0),
|
||||
None => result = Some(child.clone()),
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Returns the drop-shadow and feather records enabled by the style's effect category.
|
||||
pub fn enabled_shadow_feather(&self) -> Option<El> {
|
||||
let category = self.chain.iter().find_map(|style| style.child(Local("ObjectStyleObjectEffectsCategorySettings")));
|
||||
let setting = |name: &str| self.chain.iter().find_map(|style| style.child(Local("TransparencySetting"))?.child(Local(name)).cloned());
|
||||
let mut result = El::new("TransparencySetting");
|
||||
let mut any = false;
|
||||
for (name, flag) in [("DropShadowSetting", "EnableDropShadow"), ("FeatherSetting", "EnableFeather")] {
|
||||
let element = setting(name);
|
||||
let on = match category {
|
||||
Some(category) => Values::of(category).get(flag) == Some("true"),
|
||||
None => element.as_ref().and_then(|element| Values::of(element).get("Mode").map(str::to_owned)).is_some_and(|mode| mode != "None"),
|
||||
};
|
||||
any |= on;
|
||||
if let (true, Some(element)) = (on, element) {
|
||||
result.push(element);
|
||||
}
|
||||
}
|
||||
any.then_some(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn effect_flag(effect: &str) -> Option<&'static str> {
|
||||
Some(match effect {
|
||||
"BlendingSetting" => "EnableTransparency",
|
||||
"DropShadowSetting" => "EnableDropShadow",
|
||||
"FeatherSetting" => "EnableFeather",
|
||||
"InnerShadowSetting" => "EnableInnerShadow",
|
||||
"OuterGlowSetting" => "EnableOuterGlow",
|
||||
"InnerGlowSetting" => "EnableInnerGlow",
|
||||
"BevelAndEmbossSetting" => "EnableBevelEmboss",
|
||||
"SatinSetting" => "EnableSatin",
|
||||
"DirectionalFeatherSetting" => "EnableDirectionalFeather",
|
||||
"GradientFeatherSetting" => "EnableGradientFeather",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn overlay(base: &mut El, top: &El, depth: usize) {
|
||||
if depth > 32 {
|
||||
*base = top.clone();
|
||||
return;
|
||||
}
|
||||
for (key, value) in &top.attrs {
|
||||
base.set_attr(key, value);
|
||||
if let Some(Node::El(properties)) = base.children.iter_mut().find(|node| matches!(node, Node::El(el) if el.local() == "Properties")) {
|
||||
properties.children.retain(|node| !matches!(node, Node::El(el) if el.local() == key));
|
||||
}
|
||||
}
|
||||
if let Some(properties) = top.child(Local("Properties")) {
|
||||
let names: Vec<&str> = properties.elements().map(El::local).collect();
|
||||
base.attrs.retain(|(key, _)| !names.contains(&key.as_str()));
|
||||
}
|
||||
let has_text = top.children.iter().any(|node| matches!(node, Node::Text(text) | Node::CData(text) if !text.trim().is_empty()));
|
||||
if has_text {
|
||||
base.children.retain(|node| !matches!(node, Node::Text(_) | Node::CData(_)));
|
||||
}
|
||||
let mut seen: std::collections::HashMap<&str, usize> = std::collections::HashMap::new();
|
||||
for node in &top.children {
|
||||
match node {
|
||||
Node::El(child) => {
|
||||
let index = seen.entry(child.local()).or_insert(0);
|
||||
let slot = base
|
||||
.children
|
||||
.iter_mut()
|
||||
.filter_map(|node| match node {
|
||||
Node::El(el) if el.local() == child.local() => Some(el),
|
||||
_ => None,
|
||||
})
|
||||
.nth(*index);
|
||||
*index += 1;
|
||||
match slot {
|
||||
Some(slot) => overlay(slot, child, depth + 1),
|
||||
None => base.children.push(node.clone()),
|
||||
}
|
||||
}
|
||||
Node::Text(_) | Node::CData(_) if has_text => base.children.push(node.clone()),
|
||||
Node::Text(_) | Node::CData(_) | Node::Comment(_) | Node::Pi { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,19 @@ use craft_xml::{El, Local};
|
||||
|
||||
use crate::value::{nums, point_str};
|
||||
|
||||
/// Why a bounded path geometry was refused.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum PathGeometryError {
|
||||
#[error("too many points")]
|
||||
TooManyPoints,
|
||||
#[error("point without an anchor")]
|
||||
MissingAnchor,
|
||||
#[error("malformed point")]
|
||||
MalformedPoint,
|
||||
#[error("non-finite point")]
|
||||
NonFinitePoint,
|
||||
}
|
||||
|
||||
/// Most points one geometry keeps by default; points past it are dropped.
|
||||
pub const MAX_PATH_POINTS: usize = 1_000_000;
|
||||
|
||||
@@ -84,6 +97,45 @@ impl PathGeometry {
|
||||
PathGeometry { paths }
|
||||
}
|
||||
|
||||
/// Parse a path geometry without silently discarding malformed points, keeping at most
|
||||
/// `max` point elements. Missing direction handles default to the anchor.
|
||||
pub fn try_from_el_capped(pg: &El, max: usize) -> Result<PathGeometry, PathGeometryError> {
|
||||
let point = |value: Option<&str>, required: bool| -> Result<Option<[f64; 2]>, PathGeometryError> {
|
||||
let Some(value) = value else {
|
||||
return if required { Err(PathGeometryError::MissingAnchor) } else { Ok(None) };
|
||||
};
|
||||
let mut nums = value.split_whitespace();
|
||||
let Some(x) = nums.next().and_then(|n| n.parse::<f64>().ok()) else { return Err(PathGeometryError::MalformedPoint) };
|
||||
let Some(y) = nums.next().and_then(|n| n.parse::<f64>().ok()) else { return Err(PathGeometryError::MalformedPoint) };
|
||||
if nums.next().is_some() {
|
||||
return Err(PathGeometryError::MalformedPoint);
|
||||
}
|
||||
if !(x.is_finite() && y.is_finite()) {
|
||||
return Err(PathGeometryError::NonFinitePoint);
|
||||
}
|
||||
Ok(Some([x, y]))
|
||||
};
|
||||
let mut count = 0usize;
|
||||
let mut paths = Vec::new();
|
||||
for geometry in pg.children(Local("GeometryPathType")) {
|
||||
let mut points = Vec::new();
|
||||
if let Some(array) = geometry.child(Local("PathPointArray")) {
|
||||
for el in array.children(Local("PathPointType")) {
|
||||
count = count.saturating_add(1);
|
||||
if count > max {
|
||||
return Err(PathGeometryError::TooManyPoints);
|
||||
}
|
||||
let anchor = point(el.attr("Anchor"), true)?.ok_or(PathGeometryError::MissingAnchor)?;
|
||||
let left = point(el.attr("LeftDirection"), false)?.unwrap_or(anchor);
|
||||
let right = point(el.attr("RightDirection"), false)?.unwrap_or(anchor);
|
||||
points.push(PathPoint { anchor, left, right });
|
||||
}
|
||||
}
|
||||
paths.push(SubPath { open: geometry.attr("PathOpen") == Some("true"), points });
|
||||
}
|
||||
Ok(PathGeometry { paths })
|
||||
}
|
||||
|
||||
/// The `PathGeometry` element.
|
||||
pub fn to_el(&self) -> El {
|
||||
let mut pg = El::new("PathGeometry");
|
||||
|
||||
@@ -12,20 +12,44 @@
|
||||
//! Nodes are built from the elements [`crate::Package::document`] gives ([`Spread::from_el`],
|
||||
//! [`spreads`]) and written back with `to_el`, then [`crate::part_xml`].
|
||||
|
||||
mod document;
|
||||
mod effects;
|
||||
mod geometry;
|
||||
mod preferences;
|
||||
mod resources;
|
||||
mod spread;
|
||||
mod story;
|
||||
|
||||
pub use geometry::{MAX_PATH_POINTS, PathGeometry, PathPoint, SubPath};
|
||||
pub use resources::{
|
||||
ColorGroup, ColorSpace, GradientStop, StrokeStyle, StrokeStyleKind, Style, StyleGroup, StyleKind, StyleNode, Swatch, SwatchKind, color_groups,
|
||||
named, preference, stroke_styles, style_groups, swatches,
|
||||
};
|
||||
pub use spread::{Graphic, GraphicChild, GraphicKind, Guide, ItemChild, ItemKind, Link, Page, PageChild, PageItem, Spread, SpreadChild, spreads};
|
||||
pub use story::{Ace, CONTAINERS, Cell, Container, ContentPart, Range, Story, Table, TableChild, TextNode, stories};
|
||||
|
||||
use craft_xml::{El, Local, Node};
|
||||
pub use document::{
|
||||
CellStyleValues, ConditionValues, CrossReferenceBuildingBlock, CrossReferenceFormatValues, EpubSplitValues, ModelDocument, NumberingListValues,
|
||||
TableStyleValues, TextDateKind, TextVariableKind, TextVariableValues, TocStyleEntryValues, TocStyleValues, TopLevelKind, TopLevelNode,
|
||||
};
|
||||
pub use effects::{
|
||||
DirectionalFeatherValues, DropShadowValues, FeatherValues, GradientFeatherValues, ObjectStyleStack, OpacityStopValues, TransparencySetting,
|
||||
};
|
||||
pub use geometry::{MAX_PATH_POINTS, PathGeometry, PathGeometryError, PathPoint, SubPath};
|
||||
pub use preferences::{DocumentPreferences, FootnotePreferences, GridPreferences, TextFramePreferences};
|
||||
pub use resources::{
|
||||
CharacterAppearanceValues, CharacterSpecialValues, CharacterTypographyValues, CjkCharacterValues, CjkParagraphValues, ColorDefinition,
|
||||
ColorGroup, ColorGroupDefinition, ColorGroupMember, ColorSpace, CompositeFontDefinition, CompositeFontEntryDefinition, EpubExportTagMap,
|
||||
FontDefinition, FontFamilyDefinition, FontsPart, GradientDefinition, GradientStop, GradientStopDefinition, GradientStopValue, GraphicPart,
|
||||
GrepStyleValue, Ink, InkDefinition, MojikumiRow, NestedLineStyleValue, NestedStyleValue, NestedStyleValues, ObjectStyleValues,
|
||||
OpenTypeFeatureValues, ParagraphAlignmentValues, ParagraphBoxValues, ParagraphFlowValues, ParagraphListValues, ParagraphSpanValues, RubyValues,
|
||||
StrokeDefinition, StrokeStyle, StrokeStyleKind, Style, StyleGroup, StyleGroupWriter, StyleKind, StyleNode, Swatch, SwatchKind, TabStopValues,
|
||||
TextDecorationValues, TintDefinition, color_groups, inks, named, preference, stroke_styles, style_groups, swatches,
|
||||
};
|
||||
pub use spread::{
|
||||
BaselineFrameGridOption, BaselineFrameGridRelative, ClippingPathValues, FrameFittingOption, Graphic, GraphicChild, GraphicKind,
|
||||
GraphicLayerOptions, Guide, ItemChild, ItemKind, Link, MarginPreference, Page, PageChild, PageItem, PdfAttribute, Spread, SpreadChild, TextPath,
|
||||
TextWrapPreference, spreads,
|
||||
};
|
||||
pub use story::{
|
||||
Ace, AnchoredObjectSetting, AnchoredObjectValues, CONTAINERS, Cell, CellOptions, Container, ContainerKind, ContentPart, CrossReferenceSource,
|
||||
IndexCrossReferenceValues, IndexDocument, IndexReference, IndexTopicPath, IndexValues, PageReference, Range, SavedCrossReference,
|
||||
SavedCrossReferencePart, Story, StoryAnchor, StoryPreference, Table, TableChild, TableColumn, TableLayout, TableRow, TextNode, TrackedChangeKind,
|
||||
stories,
|
||||
};
|
||||
|
||||
use crate::value::{matrix, nums};
|
||||
|
||||
@@ -43,6 +67,157 @@ pub struct Values {
|
||||
pub properties: Vec<El>,
|
||||
}
|
||||
|
||||
/// Raw IDML stroke values before the caller maps swatches and stroke styles into its model.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct StrokeValues {
|
||||
pub weight: Option<f64>,
|
||||
pub color: Option<String>,
|
||||
pub tint: Option<f64>,
|
||||
pub kind: Option<String>,
|
||||
pub alignment: Option<String>,
|
||||
pub cap: Option<String>,
|
||||
pub join: Option<String>,
|
||||
pub miter_limit: Option<f64>,
|
||||
pub start: Option<String>,
|
||||
pub end: Option<String>,
|
||||
pub gap_color: Option<String>,
|
||||
pub gap_tint: Option<f64>,
|
||||
pub overprint: Option<bool>,
|
||||
pub gap_overprint: Option<bool>,
|
||||
}
|
||||
|
||||
impl StrokeValues {
|
||||
/// Reads stroke properties from elements in precedence order (item first, then nearer styles).
|
||||
pub fn from_elements(elements: &[&El]) -> StrokeValues {
|
||||
let mut result = StrokeValues::default();
|
||||
for el in elements {
|
||||
let values = Values::of(el);
|
||||
let num_attr = |key: &str| values.get(key).and_then(|value| value.parse::<f64>().ok()).filter(|value| value.is_finite());
|
||||
let bool_attr = |key: &str| match values.get(key) {
|
||||
Some("true") => Some(true),
|
||||
Some("false") => Some(false),
|
||||
_ => None,
|
||||
};
|
||||
if result.weight.is_none() {
|
||||
result.weight = num_attr("StrokeWeight");
|
||||
}
|
||||
if result.color.is_none() {
|
||||
result.color = values.get("StrokeColor").map(str::to_owned);
|
||||
}
|
||||
if result.tint.is_none() {
|
||||
result.tint = num_attr("StrokeTint");
|
||||
}
|
||||
if result.kind.is_none() {
|
||||
result.kind = values.get("StrokeType").map(str::to_owned);
|
||||
}
|
||||
if result.alignment.is_none() {
|
||||
result.alignment = values.get("StrokeAlignment").map(str::to_owned);
|
||||
}
|
||||
if result.cap.is_none() {
|
||||
result.cap = values.get("EndCap").map(str::to_owned);
|
||||
}
|
||||
if result.join.is_none() {
|
||||
result.join = values.get("EndJoin").map(str::to_owned);
|
||||
}
|
||||
if result.miter_limit.is_none() {
|
||||
result.miter_limit = num_attr("MiterLimit");
|
||||
}
|
||||
if result.start.is_none() {
|
||||
result.start = values.get("LeftLineEnd").map(str::to_owned);
|
||||
}
|
||||
if result.end.is_none() {
|
||||
result.end = values.get("RightLineEnd").map(str::to_owned);
|
||||
}
|
||||
if result.gap_color.is_none() {
|
||||
result.gap_color = values.get("GapColor").map(str::to_owned);
|
||||
}
|
||||
if result.gap_tint.is_none() {
|
||||
result.gap_tint = num_attr("GapTint");
|
||||
}
|
||||
if result.overprint.is_none() {
|
||||
result.overprint = bool_attr("OverprintStroke");
|
||||
}
|
||||
if result.gap_overprint.is_none() {
|
||||
result.gap_overprint = bool_attr("OverprintGap");
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw fill properties before a caller resolves swatch names to its own colour model.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct FillValues {
|
||||
pub color: Option<String>,
|
||||
pub tint: Option<f64>,
|
||||
pub overprint: Option<bool>,
|
||||
}
|
||||
|
||||
impl FillValues {
|
||||
/// Reads fill attributes in precedence order (item first, then nearer styles).
|
||||
pub fn from_elements(elements: &[&El]) -> FillValues {
|
||||
let mut result = FillValues::default();
|
||||
for el in elements {
|
||||
let values = Values::of(el);
|
||||
if result.color.is_none() {
|
||||
result.color = values.get("FillColor").map(str::to_owned);
|
||||
}
|
||||
if result.tint.is_none() {
|
||||
result.tint = values.get("FillTint").and_then(|value| value.parse::<f64>().ok());
|
||||
}
|
||||
if result.overprint.is_none() {
|
||||
result.overprint = match values.get("OverprintFill") {
|
||||
Some("true") => Some(true),
|
||||
Some("false") => Some(false),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct AlternatingFillValues {
|
||||
pub first_count: Option<f64>,
|
||||
pub first_color: Option<String>,
|
||||
pub first_tint: Option<f64>,
|
||||
pub next_count: Option<f64>,
|
||||
pub next_color: Option<String>,
|
||||
pub next_tint: Option<f64>,
|
||||
pub skip_first: Option<f64>,
|
||||
pub skip_last: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct AlternatingStrokeValues {
|
||||
pub first_count: Option<f64>,
|
||||
pub next_count: Option<f64>,
|
||||
pub first: StrokeValues,
|
||||
pub next: StrokeValues,
|
||||
pub skip_first: Option<f64>,
|
||||
pub skip_last: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TableOptions {
|
||||
pub direction: Option<String>,
|
||||
pub header_rows: Option<f64>,
|
||||
pub footer_rows: Option<f64>,
|
||||
pub applied_style: Option<String>,
|
||||
pub space_before: Option<f64>,
|
||||
pub space_after: Option<f64>,
|
||||
pub repeat_header: bool,
|
||||
pub repeat_footer: bool,
|
||||
pub borders: [StrokeValues; 4],
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct CornerValues {
|
||||
pub option: Option<String>,
|
||||
pub radius: Option<f64>,
|
||||
}
|
||||
|
||||
impl Values {
|
||||
/// The attributes and `<Properties>` of `el`.
|
||||
pub fn of(el: &El) -> Values {
|
||||
@@ -104,6 +279,13 @@ impl Values {
|
||||
self.prop(key).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite())
|
||||
}
|
||||
|
||||
/// A two-number value (for properties such as `GradientFillStart`), bounded to three tokens.
|
||||
pub fn pair(&self, key: &str) -> Option<[f64; 2]> {
|
||||
let source = self.prop(key)?;
|
||||
let values: Vec<f64> = source.split_whitespace().take(3).filter_map(|v| v.parse::<f64>().ok()).collect();
|
||||
<[f64; 2]>::try_from(values).ok().filter(|v| v.iter().all(|n| n.is_finite()))
|
||||
}
|
||||
|
||||
/// `true` or `false`; anything else counts as absent.
|
||||
pub fn boolean(&self, key: &str) -> Option<bool> {
|
||||
match self.prop(key)?.trim() {
|
||||
@@ -154,6 +336,77 @@ impl Values {
|
||||
}
|
||||
el
|
||||
}
|
||||
|
||||
/// Read an IDML stroke attribute family such as `TopEdgeStrokeWeight` or
|
||||
/// `StartRowStrokeColor`.
|
||||
pub fn stroke_values(&self, prefix: &str) -> StrokeValues {
|
||||
StrokeValues {
|
||||
weight: self.num(&format!("{prefix}StrokeWeight")).map(|v| v.max(0.0)),
|
||||
color: self.value(&format!("{prefix}StrokeColor")),
|
||||
tint: self.num(&format!("{prefix}StrokeTint")),
|
||||
kind: self.value(&format!("{prefix}StrokeType")),
|
||||
gap_color: self.value(&format!("{prefix}StrokeGapColor")),
|
||||
gap_tint: self.num(&format!("{prefix}StrokeGapTint")),
|
||||
..StrokeValues::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alternating_fill_values(&self, kind: &str) -> AlternatingFillValues {
|
||||
let num = |key: &str| self.num(key).map(|v| v.max(0.0));
|
||||
AlternatingFillValues {
|
||||
first_count: num(&format!("Start{kind}FillCount")),
|
||||
first_color: self.value(&format!("Start{kind}FillColor")),
|
||||
first_tint: self.num(&format!("Start{kind}FillTint")),
|
||||
next_count: num(&format!("End{kind}FillCount")),
|
||||
next_color: self.value(&format!("End{kind}FillColor")),
|
||||
next_tint: self.num(&format!("End{kind}FillTint")),
|
||||
skip_first: num(&format!("SkipFirstAlternatingFill{kind}s")),
|
||||
skip_last: num(&format!("SkipLastAlternatingFill{kind}s")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alternating_stroke_values(&self, kind: &str) -> AlternatingStrokeValues {
|
||||
let num = |key: &str| self.num(key).map(|v| v.max(0.0));
|
||||
let mut next = self.stroke_values(&format!("End{kind}"));
|
||||
// IDML writes this one table-column value as LineStyle rather than StrokeType.
|
||||
if kind == "Column" {
|
||||
next.kind = self.value("EndColumnLineStyle").or(next.kind);
|
||||
}
|
||||
AlternatingStrokeValues {
|
||||
first_count: num(&format!("Start{kind}StrokeCount")),
|
||||
next_count: num(&format!("End{kind}StrokeCount")),
|
||||
first: self.stroke_values(&format!("Start{kind}")),
|
||||
next,
|
||||
skip_first: num(&format!("SkipFirstAlternatingStroke{kind}s")),
|
||||
skip_last: num(&format!("SkipLastAlternatingStroke{kind}s")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn table_options(&self) -> TableOptions {
|
||||
TableOptions {
|
||||
direction: self.value("TableDirection"),
|
||||
header_rows: self.num("HeaderRowCount"),
|
||||
footer_rows: self.num("FooterRowCount"),
|
||||
applied_style: self.value("AppliedTableStyle"),
|
||||
space_before: self.num("SpaceBefore"),
|
||||
space_after: self.num("SpaceAfter"),
|
||||
repeat_header: self.get("HeaderBehavior") != Some("RepeatOnce"),
|
||||
repeat_footer: self.get("FooterBehavior") != Some("RepeatOnce"),
|
||||
borders: [
|
||||
self.stroke_values("TopBorder"),
|
||||
self.stroke_values("LeftBorder"),
|
||||
self.stroke_values("BottomBorder"),
|
||||
self.stroke_values("RightBorder"),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn corner_values(&self, corner: &str) -> CornerValues {
|
||||
CornerValues {
|
||||
option: self.value(&format!("{corner}CornerOption")).or_else(|| self.value("CornerOption")),
|
||||
radius: self.num(&format!("{corner}CornerRadius")).or_else(|| self.num("CornerRadius")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `el` without its indentation: the whitespace-only text between the children of an element
|
||||
|
||||
@@ -0,0 +1,420 @@
|
||||
//! Typed views of IDML's reusable text-frame preference grammar.
|
||||
|
||||
use craft_xml::El;
|
||||
|
||||
use super::Values;
|
||||
|
||||
/// Parsed values from IDML `DocumentPreference` elements.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct DocumentPreferences {
|
||||
pub overprint_black: Option<bool>,
|
||||
pub page_width: Option<f64>,
|
||||
pub page_height: Option<f64>,
|
||||
pub facing_pages: Option<bool>,
|
||||
pub page_binding: Option<String>,
|
||||
pub bleed: [Option<f64>; 4],
|
||||
pub slug: [Option<f64>; 4],
|
||||
pub intent: Option<String>,
|
||||
pub primary_text_frame: Option<bool>,
|
||||
pub column_guides_locked: Option<bool>,
|
||||
}
|
||||
|
||||
impl DocumentPreferences {
|
||||
/// Reads the document-level page, bleed, slug, and intent values.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self {
|
||||
overprint_black: values.get("OverprintBlack").map(|value| value != "false"),
|
||||
page_width: values.num("PageWidth"),
|
||||
page_height: values.num("PageHeight"),
|
||||
facing_pages: values.boolean("FacingPages"),
|
||||
page_binding: values.prop("PageBinding"),
|
||||
bleed: [
|
||||
values.num("DocumentBleedTopOffset"),
|
||||
values.num("DocumentBleedBottomOffset"),
|
||||
values.num("DocumentBleedInsideOrLeftOffset"),
|
||||
values.num("DocumentBleedOutsideOrRightOffset"),
|
||||
],
|
||||
slug: [
|
||||
values.num("SlugTopOffset"),
|
||||
values.num("SlugBottomOffset"),
|
||||
values.num("SlugInsideOrLeftOffset"),
|
||||
values.num("SlugRightOrOutsideOffset"),
|
||||
],
|
||||
intent: values.prop("Intent"),
|
||||
primary_text_frame: values.boolean("CreatePrimaryTextFrame"),
|
||||
column_guides_locked: values.boolean("ColumnGuideLocked"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Full canonical attributes for an exported IDML `DocumentPreference`.
|
||||
pub fn to_export_attrs(&self) -> Vec<(String, String)> {
|
||||
let mut attrs = Vec::new();
|
||||
for (key, value) in [("PageHeight", self.page_height), ("PageWidth", self.page_width)] {
|
||||
if let Some(value) = value {
|
||||
attrs.push((key.into(), crate::value::num(value)));
|
||||
}
|
||||
}
|
||||
attrs.push(("PagesPerDocument".into(), "1".into()));
|
||||
if let Some(value) = self.facing_pages {
|
||||
attrs.push(("FacingPages".into(), if value { "true" } else { "false" }.into()));
|
||||
}
|
||||
for (key, value) in [
|
||||
("DocumentBleedTopOffset", self.bleed[0]),
|
||||
("DocumentBleedBottomOffset", self.bleed[1]),
|
||||
("DocumentBleedInsideOrLeftOffset", self.bleed[2]),
|
||||
("DocumentBleedOutsideOrRightOffset", self.bleed[3]),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
attrs.push((key.into(), crate::value::num(value)));
|
||||
}
|
||||
}
|
||||
if self.bleed.iter().all(Option::is_some) {
|
||||
attrs.push((
|
||||
"DocumentBleedUniformSize".into(),
|
||||
bool_string(self.bleed.iter().flatten().all(|value| *value == self.bleed[0].unwrap_or_default())),
|
||||
));
|
||||
}
|
||||
for (key, value) in [
|
||||
("SlugTopOffset", self.slug[0]),
|
||||
("SlugBottomOffset", self.slug[1]),
|
||||
("SlugInsideOrLeftOffset", self.slug[2]),
|
||||
("SlugRightOrOutsideOffset", self.slug[3]),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
attrs.push((key.into(), crate::value::num(value)));
|
||||
}
|
||||
}
|
||||
if self.slug.iter().all(Option::is_some) {
|
||||
attrs.push((
|
||||
"DocumentSlugUniformSize".into(),
|
||||
bool_string(self.slug.iter().flatten().all(|value| *value == self.slug[0].unwrap_or_default())),
|
||||
));
|
||||
}
|
||||
attrs.push(("PreserveLayoutWhenShuffling".into(), "true".into()));
|
||||
attrs.push(("AllowPageShuffle".into(), "true".into()));
|
||||
if let Some(value) = self.overprint_black {
|
||||
attrs.push(("OverprintBlack".into(), bool_string(value)));
|
||||
}
|
||||
if let Some(value) = &self.page_binding {
|
||||
attrs.push(("PageBinding".into(), value.clone()));
|
||||
}
|
||||
attrs.push(("ColumnDirection".into(), "Horizontal".into()));
|
||||
if let Some(value) = &self.intent {
|
||||
attrs.push(("Intent".into(), value.clone()));
|
||||
}
|
||||
if let Some(value) = self.primary_text_frame {
|
||||
attrs.push(("CreatePrimaryTextFrame".into(), bool_string(value)));
|
||||
}
|
||||
if let Some(value) = self.column_guides_locked {
|
||||
attrs.push(("ColumnGuideLocked".into(), bool_string(value)));
|
||||
}
|
||||
if let Some(value) = self.primary_text_frame {
|
||||
attrs.push(("MasterTextFrame".into(), bool_string(value)));
|
||||
}
|
||||
attrs.push(("SnippetImportUsesOriginalLocation".into(), "false".into()));
|
||||
attrs
|
||||
}
|
||||
}
|
||||
|
||||
/// Parsed document and baseline-grid preference values.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct GridPreferences {
|
||||
pub baseline_start: Option<f64>,
|
||||
pub baseline_division: Option<f64>,
|
||||
pub baseline_view_threshold: Option<f64>,
|
||||
pub baseline_relative_option: Option<String>,
|
||||
pub horizontal_division: Option<f64>,
|
||||
pub vertical_division: Option<f64>,
|
||||
pub horizontal_subdivision: Option<f64>,
|
||||
pub vertical_subdivision: Option<f64>,
|
||||
pub grids_in_back: Option<bool>,
|
||||
}
|
||||
|
||||
/// Parsed values from IDML `FootnoteOption` elements.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct FootnotePreferences {
|
||||
pub start_at: Option<f64>,
|
||||
pub prefix: Option<String>,
|
||||
pub suffix: Option<String>,
|
||||
pub paragraph_style: Option<String>,
|
||||
pub marker_style: Option<String>,
|
||||
pub separator: Option<String>,
|
||||
pub space_between: Option<f64>,
|
||||
pub space_before: Option<f64>,
|
||||
pub first_baseline: Option<String>,
|
||||
pub first_baseline_minimum: Option<f64>,
|
||||
pub span_columns: Option<bool>,
|
||||
pub rule_on: Option<bool>,
|
||||
pub rule_weight: Option<f64>,
|
||||
pub rule_tint: Option<f64>,
|
||||
pub rule_left_indent: Option<f64>,
|
||||
pub rule_width: Option<f64>,
|
||||
pub rule_offset: Option<f64>,
|
||||
pub rule_color: Option<String>,
|
||||
pub numbering_style: Option<String>,
|
||||
pub restart_numbering: Option<String>,
|
||||
pub show_prefix_suffix: Option<String>,
|
||||
pub marker_positioning: Option<String>,
|
||||
}
|
||||
|
||||
impl FootnotePreferences {
|
||||
/// Reads footnote preference attributes and properties from IDML.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self {
|
||||
start_at: values.num("StartAt"),
|
||||
prefix: values.get("Prefix").map(str::to_owned),
|
||||
suffix: values.get("Suffix").map(str::to_owned),
|
||||
paragraph_style: values.get("FootnoteTextStyle").map(str::to_owned),
|
||||
marker_style: values.get("FootnoteMarkerStyle").map(str::to_owned),
|
||||
separator: values.get("SeparatorText").map(str::to_owned),
|
||||
space_between: values.num("SpaceBetween"),
|
||||
space_before: values.num("Spacer"),
|
||||
first_baseline: values.prop("FootnoteFirstBaselineOffset"),
|
||||
first_baseline_minimum: values.num("FootnoteMinimumFirstBaselineOffset"),
|
||||
span_columns: values.boolean("EnableStraddling"),
|
||||
rule_on: values.boolean("RuleOn"),
|
||||
rule_weight: values.num("RuleLineWeight"),
|
||||
rule_tint: values.num("RuleTint"),
|
||||
rule_left_indent: values.num("RuleLeftIndent"),
|
||||
rule_width: values.num("RuleWidth"),
|
||||
rule_offset: values.num("RuleOffset"),
|
||||
rule_color: values.prop("RuleColor"),
|
||||
numbering_style: values.prop("FootnoteNumberingStyle"),
|
||||
restart_numbering: values.prop("RestartNumbering"),
|
||||
show_prefix_suffix: values.prop("ShowPrefixSuffix"),
|
||||
marker_positioning: values.prop("MarkerPositioning"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes one canonical IDML `FootnoteOption` using caller-resolved style, colour, and enum
|
||||
/// references.
|
||||
pub fn to_el(&self) -> El {
|
||||
let mut el = El::new("FootnoteOption")
|
||||
.with_attr("StartAt", crate::value::num(self.start_at.unwrap_or(1.0)))
|
||||
.with_attr("Prefix", self.prefix.as_deref().unwrap_or(""))
|
||||
.with_attr("Suffix", self.suffix.as_deref().unwrap_or(""))
|
||||
.with_attr("FootnoteTextStyle", self.paragraph_style.as_deref().unwrap_or("$ID/NormalParagraphStyle"))
|
||||
.with_attr("FootnoteMarkerStyle", self.marker_style.as_deref().unwrap_or("$ID/[None]"))
|
||||
.with_attr("SeparatorText", self.separator.as_deref().unwrap_or(""))
|
||||
.with_attr("SpaceBetween", crate::value::num(self.space_between.unwrap_or(0.0)))
|
||||
.with_attr("Spacer", crate::value::num(self.space_before.unwrap_or(0.0)))
|
||||
.with_attr("FootnoteFirstBaselineOffset", self.first_baseline.as_deref().unwrap_or("Ascent"))
|
||||
.with_attr("FootnoteMinimumFirstBaselineOffset", crate::value::num(self.first_baseline_minimum.unwrap_or(0.0)))
|
||||
.with_attr("EnableStraddling", bool_text(self.span_columns.unwrap_or(false)))
|
||||
.with_attr("RuleOn", bool_text(self.rule_on.unwrap_or(true)))
|
||||
.with_attr("RuleLineWeight", crate::value::num(self.rule_weight.unwrap_or(1.0)))
|
||||
.with_attr("RuleTint", crate::value::num(self.rule_tint.unwrap_or(1.0)))
|
||||
.with_attr("RuleLeftIndent", crate::value::num(self.rule_left_indent.unwrap_or(0.0)))
|
||||
.with_attr("RuleWidth", crate::value::num(self.rule_width.unwrap_or(72.0)))
|
||||
.with_attr("RuleOffset", crate::value::num(self.rule_offset.unwrap_or(0.0)));
|
||||
let mut properties = El::new("Properties");
|
||||
for (name, value) in [
|
||||
("FootnoteNumberingStyle", self.numbering_style.as_deref().unwrap_or("Arabic")),
|
||||
("RestartNumbering", self.restart_numbering.as_deref().unwrap_or("DontRestart")),
|
||||
("ShowPrefixSuffix", self.show_prefix_suffix.as_deref().unwrap_or("NoPrefixSuffix")),
|
||||
("MarkerPositioning", self.marker_positioning.as_deref().unwrap_or("Superscript")),
|
||||
] {
|
||||
properties.push(El::new(name).with_attr("type", "enumeration").with_text(value));
|
||||
}
|
||||
properties.push(El::new("RuleColor").with_attr("type", "object").with_text(self.rule_color.as_deref().unwrap_or("Swatch/Black")));
|
||||
el.push(properties);
|
||||
el
|
||||
}
|
||||
}
|
||||
|
||||
impl GridPreferences {
|
||||
/// Reads baseline and document-grid values.
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self {
|
||||
baseline_start: values.num("BaselineStart"),
|
||||
baseline_division: values.num("BaselineDivision"),
|
||||
baseline_view_threshold: values.num("BaselineViewThreshold"),
|
||||
baseline_relative_option: values.prop("BaselineGridRelativeOption"),
|
||||
horizontal_division: values.num("HorizontalGridlineDivision"),
|
||||
vertical_division: values.num("VerticalGridlineDivision"),
|
||||
horizontal_subdivision: values.num("HorizontalGridSubdivision"),
|
||||
vertical_subdivision: values.num("VerticalGridSubdivision"),
|
||||
grids_in_back: values.boolean("GridsInBack"),
|
||||
}
|
||||
}
|
||||
|
||||
/// IDML attributes represented by this grid preference record.
|
||||
pub fn to_attrs(&self) -> Vec<(String, String)> {
|
||||
let mut attrs = Vec::new();
|
||||
for (key, value) in [
|
||||
("HorizontalGridlineDivision", self.horizontal_division),
|
||||
("VerticalGridlineDivision", self.vertical_division),
|
||||
("HorizontalGridSubdivision", self.horizontal_subdivision),
|
||||
("VerticalGridSubdivision", self.vertical_subdivision),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
attrs.push((key.into(), crate::value::num(value)));
|
||||
}
|
||||
}
|
||||
if let Some(value) = self.grids_in_back {
|
||||
attrs.push(("GridsInBack".into(), if value { "true" } else { "false" }.into()));
|
||||
}
|
||||
for (key, value) in [
|
||||
("BaselineStart", self.baseline_start),
|
||||
("BaselineDivision", self.baseline_division),
|
||||
("BaselineViewThreshold", self.baseline_view_threshold),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
attrs.push((key.into(), crate::value::num(value)));
|
||||
}
|
||||
}
|
||||
if let Some(value) = self.baseline_relative_option.as_deref() {
|
||||
attrs.push(("BaselineGridRelativeOption".into(), value.into()));
|
||||
}
|
||||
attrs
|
||||
}
|
||||
}
|
||||
|
||||
fn bool_string(value: bool) -> String {
|
||||
if value { "true" } else { "false" }.into()
|
||||
}
|
||||
|
||||
/// Parsed `TextFramePreference` values in nearest-to-farthest precedence order.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TextFramePreferences {
|
||||
pub column_count: Option<f64>,
|
||||
pub column_gutter: Option<f64>,
|
||||
pub fixed_column_width: Option<f64>,
|
||||
pub fixed_width: bool,
|
||||
pub flexible_width: bool,
|
||||
pub balance_columns: bool,
|
||||
pub inset: Option<[f64; 4]>,
|
||||
pub vertical_justification: Option<String>,
|
||||
pub vertical_threshold: Option<f64>,
|
||||
pub first_baseline: Option<String>,
|
||||
pub minimum_first_baseline: Option<f64>,
|
||||
pub ignore_wrap: bool,
|
||||
pub auto_sizing_type: Option<String>,
|
||||
pub auto_sizing_reference: Option<String>,
|
||||
pub use_minimum_height: bool,
|
||||
pub minimum_height: Option<f64>,
|
||||
pub use_minimum_width: bool,
|
||||
pub minimum_width: Option<f64>,
|
||||
pub no_line_breaks: bool,
|
||||
pub column_rule_override: bool,
|
||||
pub column_rule_width: Option<f64>,
|
||||
pub column_rule_tint: Option<f64>,
|
||||
pub column_rule_offset: Option<f64>,
|
||||
pub column_rule_top_inset: Option<f64>,
|
||||
pub column_rule_bottom_inset: Option<f64>,
|
||||
}
|
||||
|
||||
impl TextFramePreferences {
|
||||
/// Reads preference levels nearest first. A malformed explicit value still masks an inherited
|
||||
/// one, matching IDML's item-over-object-style precedence.
|
||||
pub fn from_elements(elements: &[&El]) -> Self {
|
||||
let prop = |key: &str| elements.iter().find_map(|el| Values::of(el).prop(key));
|
||||
let num = |key: &str| prop(key).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite());
|
||||
let boolean = |key: &str| matches!(prop(key).as_deref().map(str::trim), Some("true"));
|
||||
Self {
|
||||
column_count: num("TextColumnCount"),
|
||||
column_gutter: num("TextColumnGutter"),
|
||||
fixed_column_width: num("TextColumnFixedWidth"),
|
||||
fixed_width: boolean("UseFixedColumnWidth"),
|
||||
flexible_width: boolean("UseFlexibleColumnWidth"),
|
||||
balance_columns: boolean("VerticalBalanceColumns"),
|
||||
inset: inset(elements),
|
||||
vertical_justification: prop("VerticalJustification"),
|
||||
vertical_threshold: num("VerticalThreshold"),
|
||||
first_baseline: prop("FirstBaselineOffset"),
|
||||
minimum_first_baseline: num("MinimumFirstBaselineOffset"),
|
||||
ignore_wrap: boolean("IgnoreWrap"),
|
||||
auto_sizing_type: prop("AutoSizingType"),
|
||||
auto_sizing_reference: prop("AutoSizingReferencePoint"),
|
||||
use_minimum_height: boolean("UseMinimumHeightForAutoSizing"),
|
||||
minimum_height: num("MinimumHeightForAutoSizing"),
|
||||
use_minimum_width: boolean("UseMinimumWidthForAutoSizing"),
|
||||
minimum_width: num("MinimumWidthForAutoSizing"),
|
||||
no_line_breaks: boolean("UseNoLineBreaksForAutoSizing"),
|
||||
column_rule_override: boolean("ColumnRuleOverride"),
|
||||
column_rule_width: num("ColumnRuleStrokeWidth"),
|
||||
column_rule_tint: num("ColumnRuleStrokeTint"),
|
||||
column_rule_offset: num("ColumnRuleOffset"),
|
||||
column_rule_top_inset: num("ColumnRuleTopInset"),
|
||||
column_rule_bottom_inset: num("ColumnRuleBottomInset"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes one IDML `TextFramePreference`, including its inset list and column-rule
|
||||
/// properties. Enumeration strings are supplied by the caller because they are model policy.
|
||||
pub fn to_el(&self, column_rule_color: &str) -> El {
|
||||
use crate::value::num;
|
||||
|
||||
let mut inset = El::new("InsetSpacing").with_attr("type", "list");
|
||||
for value in self.inset.unwrap_or([0.0; 4]) {
|
||||
inset.push(El::new("ListItem").with_attr("type", "unit").with_text(num(value)));
|
||||
}
|
||||
let el = El::new("TextFramePreference")
|
||||
.with_attr("TextColumnCount", num(self.column_count.unwrap_or(1.0).max(1.0)))
|
||||
.with_attr("TextColumnGutter", num(self.column_gutter.unwrap_or(0.0)))
|
||||
.with_attr("UseFixedColumnWidth", bool_text(self.fixed_width))
|
||||
.with_attr("UseFlexibleColumnWidth", bool_text(self.flexible_width))
|
||||
.with_attr("VerticalBalanceColumns", bool_text(self.balance_columns))
|
||||
.with_attr("FirstBaselineOffset", self.first_baseline.as_deref().unwrap_or("Ascent"))
|
||||
.with_attr("MinimumFirstBaselineOffset", num(self.minimum_first_baseline.unwrap_or(0.0)))
|
||||
.with_attr("VerticalJustification", self.vertical_justification.as_deref().unwrap_or("TopAlign"))
|
||||
.with_attr("VerticalThreshold", num(self.vertical_threshold.unwrap_or(0.0)))
|
||||
.with_attr("IgnoreWrap", bool_text(self.ignore_wrap))
|
||||
.with_attr("AutoSizingType", self.auto_sizing_type.as_deref().unwrap_or("Off"))
|
||||
.with_attr("AutoSizingReferencePoint", self.auto_sizing_reference.as_deref().unwrap_or("CenterPoint"))
|
||||
.with_attr("UseMinimumHeightForAutoSizing", bool_text(self.use_minimum_height))
|
||||
.with_attr("MinimumHeightForAutoSizing", num(self.minimum_height.unwrap_or(0.0)))
|
||||
.with_attr("UseMinimumWidthForAutoSizing", bool_text(self.use_minimum_width))
|
||||
.with_attr("MinimumWidthForAutoSizing", num(self.minimum_width.unwrap_or(0.0)))
|
||||
.with_attr("UseNoLineBreaksForAutoSizing", bool_text(self.no_line_breaks));
|
||||
let el = if let Some(width) = self.fixed_column_width.filter(|width| *width > 0.0) {
|
||||
el.with_attr("TextColumnFixedWidth", num(width))
|
||||
} else {
|
||||
el
|
||||
};
|
||||
let el = el
|
||||
.with_attr("ColumnRuleOverride", bool_text(self.column_rule_override))
|
||||
.with_attr("ColumnRuleStrokeWidth", num(self.column_rule_width.unwrap_or(0.0)))
|
||||
.with_attr("ColumnRuleStrokeColor", column_rule_color)
|
||||
.with_attr("ColumnRuleStrokeTint", num(self.column_rule_tint.unwrap_or(0.0)))
|
||||
.with_attr("ColumnRuleOffset", num(self.column_rule_offset.unwrap_or(0.0)))
|
||||
.with_attr("ColumnRuleTopInset", num(self.column_rule_top_inset.unwrap_or(0.0)))
|
||||
.with_attr("ColumnRuleBottomInset", num(self.column_rule_bottom_inset.unwrap_or(0.0)))
|
||||
.with_attr("ColumnRuleInsetChainOverride", bool_text(self.column_rule_top_inset == self.column_rule_bottom_inset));
|
||||
el.with_child(El::new("Properties").with_child(inset))
|
||||
}
|
||||
}
|
||||
|
||||
fn bool_text(value: bool) -> &'static str {
|
||||
if value { "true" } else { "false" }
|
||||
}
|
||||
|
||||
fn inset(elements: &[&El]) -> Option<[f64; 4]> {
|
||||
let element = elements.iter().find(|el| Values::of(el).prop("InsetSpacing").is_some())?;
|
||||
let values = Values::of(element);
|
||||
let number = |s: &str| s.trim().parse::<f64>().ok().filter(|v| v.is_finite());
|
||||
let Some(property) = values.prop_el("InsetSpacing") else {
|
||||
return values.get("InsetSpacing").and_then(number).map(|v| [v; 4]);
|
||||
};
|
||||
let items: Vec<_> = property.elements().collect();
|
||||
if property.attr("type") == Some("list") || items.iter().any(|item| item.local() == "ListItem") {
|
||||
if items.len() != 4 {
|
||||
return None;
|
||||
}
|
||||
let mut result = [0.0; 4];
|
||||
for (slot, item) in result.iter_mut().zip(items) {
|
||||
if item.local() != "ListItem" || item.elements().next().is_some() {
|
||||
return None;
|
||||
}
|
||||
*slot = number(&item.deep_text())?;
|
||||
}
|
||||
Some(result)
|
||||
} else if items.is_empty() {
|
||||
number(&property.deep_text()).map(|v| [v; 4])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -17,6 +17,60 @@ fn with_children(mut el: El, children: impl IntoIterator<Item = El>) -> El {
|
||||
el
|
||||
}
|
||||
|
||||
/// Raw clip-path settings attached to a placed graphic, before the caller maps type and
|
||||
/// coordinates into its scene model.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct ClippingPathValues {
|
||||
pub clipping_type: Option<String>,
|
||||
pub geometry: PathGeometry,
|
||||
pub invert: Option<bool>,
|
||||
pub include_inside_edges: Option<bool>,
|
||||
pub restrict_to_frame: Option<bool>,
|
||||
pub use_high_resolution: Option<bool>,
|
||||
pub threshold: Option<f64>,
|
||||
pub tolerance: Option<f64>,
|
||||
pub inset: Option<f64>,
|
||||
pub source_name: Option<String>,
|
||||
pub index: Option<f64>,
|
||||
}
|
||||
|
||||
impl ClippingPathValues {
|
||||
/// Reads a graphic's `ClippingPathSettings` and saved geometry, with the caller's point cap.
|
||||
pub fn from_graphic(graphic: &El, max_points: usize) -> Option<Self> {
|
||||
let settings = graphic.find("ClippingPathSettings")?;
|
||||
let settings_values = Values::of(settings);
|
||||
let graphic_values = Values::of(graphic);
|
||||
let clipping_type = settings_values.get("ClippingType").map(str::to_owned);
|
||||
let invert = settings_values.boolean("InvertPath");
|
||||
let include_inside_edges = settings_values.boolean("IncludeInsideEdges");
|
||||
let restrict_to_frame = settings_values.boolean("RestrictToFrame");
|
||||
let use_high_resolution = settings_values.boolean("UseHighResolutionImage");
|
||||
let threshold = settings_values.num("Threshold");
|
||||
let tolerance = settings_values.num("Tolerance");
|
||||
let inset = settings_values.num("InsetFrame");
|
||||
let source_name = settings_values.get("AppliedPathName").map(str::to_owned);
|
||||
let index = settings_values.num("Index");
|
||||
let geometry = settings_values
|
||||
.prop_el("ClippingPathGeometry")
|
||||
.or_else(|| settings.find("ClippingPathGeometry"))
|
||||
.or_else(|| graphic_values.prop_el("ClippingPathGeometry"))?;
|
||||
let geometry = PathGeometry::from_el_capped(geometry, max_points);
|
||||
Some(Self {
|
||||
clipping_type,
|
||||
geometry,
|
||||
invert,
|
||||
include_inside_edges,
|
||||
restrict_to_frame,
|
||||
use_high_resolution,
|
||||
threshold,
|
||||
tolerance,
|
||||
inset,
|
||||
source_name,
|
||||
index,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A spread (`Spread`) or a parent spread (`MasterSpread`).
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Spread {
|
||||
@@ -86,6 +140,52 @@ impl Spread {
|
||||
pub fn parent_name(&self) -> Option<(&str, &str)> {
|
||||
Some((self.values.get("NamePrefix")?, self.values.get("BaseName")?))
|
||||
}
|
||||
|
||||
/// Parent spread's prefix, using the prefix of `Name` or `A` when absent.
|
||||
pub fn parent_prefix(&self) -> String {
|
||||
self.values.get("NamePrefix").map(str::to_string).unwrap_or_else(|| self.name().split('-').next().unwrap_or("A").to_string())
|
||||
}
|
||||
|
||||
/// Parent spread's base name, using the suffix of `Name` or `Parent` when absent.
|
||||
pub fn base_name(&self) -> String {
|
||||
self.values
|
||||
.get("BaseName")
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| self.name().split_once('-').map(|(_, b)| b.to_string()).unwrap_or_else(|| self.name().to_string()))
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
self.values.name().unwrap_or("Parent")
|
||||
}
|
||||
|
||||
/// Whether master page items are shown.
|
||||
pub fn show_master_items(&self) -> bool {
|
||||
self.values.boolean("ShowMasterItems") != Some(false)
|
||||
}
|
||||
|
||||
/// Whether page shuffle is allowed.
|
||||
pub fn allow_shuffle(&self) -> bool {
|
||||
self.values.boolean("AllowPageShuffle") != Some(false)
|
||||
}
|
||||
|
||||
/// Exact quarter-turn view rotation encoded by the spread transform, clockwise on screen.
|
||||
pub fn view_rotation(&self) -> u8 {
|
||||
if self.master {
|
||||
return 0;
|
||||
}
|
||||
let [a, b, c, d, _, _] = self.values.item_transform();
|
||||
let is = |x: f64, y: f64| (x - y).abs() < 1e-6;
|
||||
let quarter = |m: [f64; 4]| is(a, m[0]) && is(b, m[1]) && is(c, m[2]) && is(d, m[3]);
|
||||
if quarter([0.0, 1.0, -1.0, 0.0]) {
|
||||
1
|
||||
} else if quarter([-1.0, 0.0, 0.0, -1.0]) {
|
||||
2
|
||||
} else if quarter([0.0, -1.0, 1.0, 0.0]) {
|
||||
3
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The spreads and parent spreads of a document, in its order.
|
||||
@@ -100,6 +200,44 @@ pub struct Page {
|
||||
pub children: Vec<PageChild>,
|
||||
}
|
||||
|
||||
/// Parsed layout values of an IDML `MarginPreference`.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MarginPreference {
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
impl MarginPreference {
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
Self { values: Values::of(el) }
|
||||
}
|
||||
pub fn top(&self) -> Option<f64> {
|
||||
self.values.num("Top")
|
||||
}
|
||||
pub fn left(&self) -> Option<f64> {
|
||||
self.values.num("Left")
|
||||
}
|
||||
pub fn bottom(&self) -> Option<f64> {
|
||||
self.values.num("Bottom")
|
||||
}
|
||||
pub fn right(&self) -> Option<f64> {
|
||||
self.values.num("Right")
|
||||
}
|
||||
pub fn column_count(&self) -> Option<f64> {
|
||||
self.values.num("ColumnCount")
|
||||
}
|
||||
pub fn column_gutter(&self) -> Option<f64> {
|
||||
self.values.num("ColumnGutter")
|
||||
}
|
||||
|
||||
/// Parse at most `limit` whitespace-separated `ColumnsPositions` values; malformed or
|
||||
/// non-finite values invalidate the list.
|
||||
pub fn column_positions(&self, limit: usize) -> Option<Vec<f64>> {
|
||||
let source = self.values.get("ColumnsPositions")?;
|
||||
let values: Option<Vec<f64>> = source.split_whitespace().take(limit).map(|v| v.parse::<f64>().ok().filter(|v| v.is_finite())).collect();
|
||||
values.filter(|values| !values.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
/// A child of a page, in document order.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum PageChild {
|
||||
@@ -142,6 +280,20 @@ impl Page {
|
||||
self.values.get("AppliedMaster").filter(|m| *m != "n")
|
||||
}
|
||||
|
||||
pub fn self_id(&self) -> Option<&str> {
|
||||
self.values.self_id()
|
||||
}
|
||||
|
||||
/// `ItemTransform`; the identity when absent or malformed.
|
||||
pub fn item_transform(&self) -> Matrix {
|
||||
self.values.item_transform()
|
||||
}
|
||||
|
||||
/// `LayoutRule`, defaulting to `Off`.
|
||||
pub fn layout_rule(&self) -> &str {
|
||||
self.values.get("LayoutRule").unwrap_or("Off")
|
||||
}
|
||||
|
||||
/// `OverrideList`: the `Self`s of the parent items overridden on this page.
|
||||
pub fn override_list(&self) -> Vec<&str> {
|
||||
self.values.get("OverrideList").map(|l| l.split_whitespace().collect()).unwrap_or_default()
|
||||
@@ -155,6 +307,10 @@ impl Page {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn margin_settings(&self) -> Option<MarginPreference> {
|
||||
self.margin_preference().map(|values| MarginPreference { values })
|
||||
}
|
||||
|
||||
pub fn guides(&self) -> impl DoubleEndedIterator<Item = &Guide> {
|
||||
self.children.iter().filter_map(|c| if let PageChild::Guide(g) = c { Some(g) } else { None })
|
||||
}
|
||||
@@ -188,6 +344,16 @@ impl Guide {
|
||||
pub fn liquid(&self) -> bool {
|
||||
self.values.get("GuideType") == Some("Liquid")
|
||||
}
|
||||
|
||||
/// `Locked` is true.
|
||||
pub fn locked(&self) -> bool {
|
||||
self.values.boolean("Locked") == Some(true)
|
||||
}
|
||||
|
||||
/// `ItemLayer`: the identifier of the layer the guide belongs to.
|
||||
pub fn item_layer(&self) -> Option<&str> {
|
||||
self.values.item_layer()
|
||||
}
|
||||
}
|
||||
|
||||
/// What a page item is.
|
||||
@@ -297,6 +463,61 @@ impl PageItem {
|
||||
self.values.prop_el("PathGeometry").map(PathGeometry::from_el).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn self_id(&self) -> Option<&str> {
|
||||
self.values.self_id()
|
||||
}
|
||||
|
||||
pub fn item_layer(&self) -> Option<&str> {
|
||||
self.values.item_layer()
|
||||
}
|
||||
|
||||
pub fn item_transform(&self) -> Matrix {
|
||||
self.values.item_transform()
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&str> {
|
||||
self.values.name()
|
||||
}
|
||||
|
||||
pub fn locked(&self) -> bool {
|
||||
self.values.locked()
|
||||
}
|
||||
|
||||
pub fn hidden(&self) -> bool {
|
||||
!self.values.visible()
|
||||
}
|
||||
|
||||
pub fn nonprinting(&self) -> bool {
|
||||
self.values.boolean("Nonprinting") == Some(true)
|
||||
}
|
||||
|
||||
pub fn gradient_fill_angle(&self) -> Option<f64> {
|
||||
self.values.num("GradientFillAngle")
|
||||
}
|
||||
pub fn gradient_fill_start(&self) -> Option<[f64; 2]> {
|
||||
self.values.pair("GradientFillStart")
|
||||
}
|
||||
pub fn gradient_fill_length(&self) -> Option<f64> {
|
||||
self.values.num("GradientFillLength")
|
||||
}
|
||||
pub fn gradient_stroke_angle(&self) -> Option<f64> {
|
||||
self.values.num("GradientStrokeAngle")
|
||||
}
|
||||
pub fn gradient_stroke_start(&self) -> Option<[f64; 2]> {
|
||||
self.values.pair("GradientStrokeStart")
|
||||
}
|
||||
pub fn gradient_stroke_length(&self) -> Option<f64> {
|
||||
self.values.num("GradientStrokeLength")
|
||||
}
|
||||
|
||||
pub fn text_path(&self) -> Option<TextPath> {
|
||||
self.other("TextPath").map(|el| TextPath { values: Values::of(el) })
|
||||
}
|
||||
|
||||
pub fn text_wrap(&self) -> Option<TextWrapPreference> {
|
||||
self.other("TextWrapPreference").map(TextWrapPreference::from_el)
|
||||
}
|
||||
|
||||
/// Replaces the outline.
|
||||
pub fn set_geometry(&mut self, g: &PathGeometry) {
|
||||
self.values.set_prop_el("PathGeometry", Some(g.to_el()));
|
||||
@@ -346,6 +567,169 @@ impl PageItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parsed IDML `TextWrapPreference` values, including the nested offset and contour elements.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TextWrapPreference {
|
||||
values: Values,
|
||||
contour_type: Option<String>,
|
||||
offsets: Option<[f64; 4]>,
|
||||
}
|
||||
|
||||
/// Parsed IDML frame fitting options.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct FrameFittingOption {
|
||||
pub crop: [Option<f64>; 4],
|
||||
pub alignment: Option<String>,
|
||||
pub auto_fit: bool,
|
||||
pub fitting_on_empty_frame: Option<String>,
|
||||
}
|
||||
|
||||
impl FrameFittingOption {
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
Self {
|
||||
crop: [values.num("TopCrop"), values.num("LeftCrop"), values.num("BottomCrop"), values.num("RightCrop")],
|
||||
alignment: values.get("FittingAlignment").map(str::to_owned),
|
||||
auto_fit: values.boolean("AutoFit") == Some(true),
|
||||
fitting_on_empty_frame: values.get("FittingOnEmptyFrame").map(str::to_owned),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// IDML baseline-grid position relative to a page, frame, or inset.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum BaselineFrameGridRelative {
|
||||
TopMargin,
|
||||
TopOfFrame,
|
||||
TopInset,
|
||||
#[default]
|
||||
TopOfPage,
|
||||
}
|
||||
|
||||
/// Parsed IDML custom baseline-grid options on an item or object style.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct BaselineFrameGridOption {
|
||||
pub use_custom: bool,
|
||||
pub start: f64,
|
||||
pub increment: f64,
|
||||
pub relative_to: BaselineFrameGridRelative,
|
||||
}
|
||||
|
||||
impl BaselineFrameGridOption {
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let values = Values::of(el);
|
||||
let relative = values.get("BaselineFrameGridRelativeOption").unwrap_or("");
|
||||
let relative = relative.strip_suffix("OfBaselineFrameGridRelativeOption").unwrap_or(relative);
|
||||
let relative_to = match relative {
|
||||
"TopOfMargin" | "TopMargin" => BaselineFrameGridRelative::TopMargin,
|
||||
"TopOfFrame" => BaselineFrameGridRelative::TopOfFrame,
|
||||
"TopOfInset" | "TopInset" => BaselineFrameGridRelative::TopInset,
|
||||
_ => BaselineFrameGridRelative::TopOfPage,
|
||||
};
|
||||
Self {
|
||||
use_custom: values.boolean("UseCustomBaselineFrameGrid") == Some(true),
|
||||
start: values.num("StartingOffsetForBaselineFrameGrid").unwrap_or(0.0),
|
||||
increment: values.num("BaselineFrameGridIncrement").unwrap_or(12.0),
|
||||
relative_to,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TextWrapPreference {
|
||||
pub fn from_el(el: &El) -> Self {
|
||||
let contour_type = el.child(Local("ContourOption")).and_then(|c| Values::of(c).get("ContourType").map(str::to_string));
|
||||
let offsets = el.child(Local("Properties")).and_then(|p| p.child(Local("TextWrapOffset"))).map(|o| {
|
||||
let values = Values::of(o);
|
||||
[
|
||||
values.num("Top").unwrap_or(0.0),
|
||||
values.num("Left").unwrap_or(0.0),
|
||||
values.num("Bottom").unwrap_or(0.0),
|
||||
values.num("Right").unwrap_or(0.0),
|
||||
]
|
||||
});
|
||||
Self { values: Values::of(el), contour_type, offsets }
|
||||
}
|
||||
|
||||
pub fn mode(&self) -> Option<&str> {
|
||||
self.values.get("TextWrapMode")
|
||||
}
|
||||
pub fn inverse(&self) -> bool {
|
||||
self.values.boolean("Inverse") == Some(true)
|
||||
}
|
||||
pub fn side(&self) -> Option<&str> {
|
||||
self.values.get("TextWrapSide")
|
||||
}
|
||||
pub fn contour_type(&self) -> Option<&str> {
|
||||
self.contour_type.as_deref()
|
||||
}
|
||||
pub fn offsets(&self) -> Option<[f64; 4]> {
|
||||
self.offsets
|
||||
}
|
||||
|
||||
/// Writes the IDML preference structure around caller-supplied enum values and path geometry.
|
||||
pub fn to_el(mode: &str, inverse: bool, side: &str, contour: &str, offsets: [f64; 4], paths: &[PathGeometry]) -> El {
|
||||
use crate::value::num;
|
||||
|
||||
let mut properties = El::new("Properties");
|
||||
for path in paths {
|
||||
properties.push(path.to_el());
|
||||
}
|
||||
properties.push(
|
||||
El::new("TextWrapOffset")
|
||||
.with_attr("Top", num(offsets[0]))
|
||||
.with_attr("Left", num(offsets[1]))
|
||||
.with_attr("Bottom", num(offsets[2]))
|
||||
.with_attr("Right", num(offsets[3])),
|
||||
);
|
||||
El::new("TextWrapPreference")
|
||||
.with_attr("Inverse", if inverse { "true" } else { "false" })
|
||||
.with_attr("ApplyToMasterPageOnly", "false")
|
||||
.with_attr("TextWrapSide", side)
|
||||
.with_attr("TextWrapMode", mode)
|
||||
.with_child(properties)
|
||||
.with_child(El::new("ContourOption").with_attr("ContourType", contour).with_attr("IncludeInsideEdges", "false"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Type-on-a-path settings.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TextPath {
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
impl TextPath {
|
||||
/// Writes IDML type-on-a-path settings for a frame and its story.
|
||||
pub fn to_el(frame_self: &str, parent_story: &str, alignment: &str, flipped: bool, start: f64, end: f64) -> El {
|
||||
El::new("TextPath")
|
||||
.with_attr("Self", format!("{frame_self}tp"))
|
||||
.with_attr("ParentStory", parent_story)
|
||||
.with_attr("PreviousTextFrame", "n")
|
||||
.with_attr("NextTextFrame", "n")
|
||||
.with_attr("PathEffect", "RainbowPathEffect")
|
||||
.with_attr("PathAlignment", "CenterPathAlignment")
|
||||
.with_attr("TextAlignment", alignment)
|
||||
.with_attr("FlipPathEffect", if flipped { "Flipped" } else { "NotFlipped" })
|
||||
.with_attr("StartBracket", crate::value::num(start))
|
||||
.with_attr("EndBracket", crate::value::num(end))
|
||||
}
|
||||
|
||||
pub fn parent_story(&self) -> Option<&str> {
|
||||
self.values.get("ParentStory").filter(|s| *s != "n")
|
||||
}
|
||||
pub fn start_bracket(&self) -> Option<f64> {
|
||||
self.values.num("StartBracket")
|
||||
}
|
||||
pub fn end_bracket(&self) -> Option<f64> {
|
||||
self.values.num("EndBracket")
|
||||
}
|
||||
pub fn flipped(&self) -> bool {
|
||||
self.values.get("FlipPathEffect") == Some("Flipped")
|
||||
}
|
||||
pub fn alignment(&self) -> Option<&str> {
|
||||
self.values.get("TextAlignment")
|
||||
}
|
||||
}
|
||||
|
||||
/// What a placed graphic is.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum GraphicKind {
|
||||
@@ -420,6 +804,119 @@ impl Link {
|
||||
}
|
||||
}
|
||||
|
||||
/// A placed graphic's layer choices, independent of the document model that stores them.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct GraphicLayerOptions {
|
||||
pub attributes: Vec<(String, String)>,
|
||||
pub layers: Vec<Vec<(String, String)>>,
|
||||
/// Parent indices in pre-order. Missing entries and invalid parents are treated as top-level.
|
||||
pub parents: Vec<Option<usize>>,
|
||||
}
|
||||
|
||||
impl GraphicLayerOptions {
|
||||
/// Reads a `GraphicLayerOption` subtree while preserving layer order and hierarchy.
|
||||
pub fn from_el(option: &El) -> Self {
|
||||
let attributes = option.attrs.iter().filter(|(key, _)| key != "Self").cloned().collect();
|
||||
let mut roots: Vec<_> = option.elements().collect();
|
||||
roots.reverse();
|
||||
let mut pending: Vec<_> = roots.into_iter().map(|element| (element, None)).collect();
|
||||
let mut layers = Vec::new();
|
||||
let mut parents = Vec::new();
|
||||
while let Some((element, parent)) = pending.pop() {
|
||||
let layer_parent = if element.local() == "GraphicLayer" {
|
||||
let index = layers.len();
|
||||
layers.push(element.attrs.iter().filter(|(key, _)| key != "Self").cloned().collect());
|
||||
parents.push(parent);
|
||||
Some(index)
|
||||
} else {
|
||||
parent
|
||||
};
|
||||
let mut children: Vec<_> = element.elements().collect();
|
||||
children.reverse();
|
||||
pending.extend(children.into_iter().map(|child| (child, layer_parent)));
|
||||
}
|
||||
Self { attributes, layers, parents }
|
||||
}
|
||||
|
||||
/// Serializes this layer tree, generating fresh IDs for the option and each layer.
|
||||
pub fn to_el(&self, self_id: &str, mut fresh: impl FnMut() -> String) -> El {
|
||||
let mut option = El::new("GraphicLayerOption").with_attr("Self", self_id);
|
||||
for (key, value) in &self.attributes {
|
||||
if valid_layer_attribute(key) {
|
||||
option.set_attr(key, value);
|
||||
}
|
||||
}
|
||||
// A parent must still be open in the pre-order walk. IDML layer groups have a depth cap.
|
||||
let mut open = Vec::new();
|
||||
let mut parents = Vec::with_capacity(self.layers.len());
|
||||
for index in 0..self.layers.len() {
|
||||
let parent = self.parents.get(index).copied().flatten();
|
||||
if let Some(at) = parent.and_then(|parent| open.iter().position(|id| *id == parent)).filter(|at| *at < 239) {
|
||||
open.truncate(at + 1);
|
||||
parents.push(parent);
|
||||
} else {
|
||||
open.clear();
|
||||
parents.push(None);
|
||||
}
|
||||
open.push(index);
|
||||
}
|
||||
let mut children: Vec<Vec<El>> = (0..self.layers.len()).map(|_| Vec::new()).collect();
|
||||
let mut roots = Vec::new();
|
||||
for (index, attributes) in self.layers.iter().enumerate().rev() {
|
||||
let mut layer = El::new("GraphicLayer").with_attr("Self", fresh());
|
||||
for (key, value) in attributes {
|
||||
if valid_layer_attribute(key) {
|
||||
layer.set_attr(key, value);
|
||||
}
|
||||
}
|
||||
if let Some(nested) = children.get_mut(index) {
|
||||
for child in std::mem::take(nested).into_iter().rev() {
|
||||
layer.push(child);
|
||||
}
|
||||
}
|
||||
if let Some(nested) = parents.get(index).copied().flatten().and_then(|parent| children.get_mut(parent)) {
|
||||
nested.push(layer);
|
||||
} else {
|
||||
roots.push(layer);
|
||||
}
|
||||
}
|
||||
for layer in roots.into_iter().rev() {
|
||||
option.push(layer);
|
||||
}
|
||||
option
|
||||
}
|
||||
}
|
||||
|
||||
fn valid_layer_attribute(key: &str) -> bool {
|
||||
!key.is_empty()
|
||||
&& key != "Self"
|
||||
&& key != "xmlns"
|
||||
&& key
|
||||
.bytes()
|
||||
.enumerate()
|
||||
.all(|(index, byte)| byte.is_ascii_alphabetic() || byte == b'_' || (index > 0 && (byte.is_ascii_digit() || matches!(byte, b'.' | b'-'))))
|
||||
}
|
||||
|
||||
/// A placed PDF's import options.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct PdfAttribute {
|
||||
values: Values,
|
||||
}
|
||||
|
||||
impl PdfAttribute {
|
||||
pub fn page_index(&self) -> u32 {
|
||||
self.values
|
||||
.get("PageNumber")
|
||||
.and_then(|value| value.trim().parse::<u64>().ok())
|
||||
.and_then(|page| u32::try_from(page.saturating_sub(1)).ok())
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn crop(&self) -> Option<&str> {
|
||||
self.values.get("PDFCrop")
|
||||
}
|
||||
}
|
||||
|
||||
impl Graphic {
|
||||
pub fn from_el(el: &El) -> Option<Graphic> {
|
||||
let kind = GraphicKind::of(el.local())?;
|
||||
@@ -449,6 +946,83 @@ impl Graphic {
|
||||
self.children.iter().find_map(|c| if let GraphicChild::Link(l) = c { Some(l) } else { None })
|
||||
}
|
||||
|
||||
pub fn self_id(&self) -> Option<&str> {
|
||||
self.values.self_id()
|
||||
}
|
||||
|
||||
pub fn applied_object_style(&self) -> Option<&str> {
|
||||
self.values.get("AppliedObjectStyle")
|
||||
}
|
||||
|
||||
pub fn item_transform(&self) -> Matrix {
|
||||
self.values.item_transform()
|
||||
}
|
||||
|
||||
/// GraphicBounds in IDML's `Left`, `Top`, `Right`, `Bottom` order.
|
||||
pub fn bounds(&self) -> [f64; 4] {
|
||||
self.values
|
||||
.prop_el("GraphicBounds")
|
||||
.map(Values::of)
|
||||
.map(|values| ["Left", "Top", "Right", "Bottom"].map(|key| values.num(key).unwrap_or(0.0)))
|
||||
.unwrap_or([0.0; 4])
|
||||
}
|
||||
|
||||
/// Two finite values from InDesign's `ActualPpi` field.
|
||||
pub fn actual_ppi(&self) -> Option<[f64; 2]> {
|
||||
let source = self.values.get("ActualPpi")?;
|
||||
let numbers: Vec<f64> = source.split_whitespace().take(3).filter_map(|value| value.parse().ok()).collect();
|
||||
<[f64; 2]>::try_from(numbers).ok().filter(|values| values.iter().all(|value| value.is_finite()))
|
||||
}
|
||||
|
||||
pub fn fill_color(&self) -> Option<&str> {
|
||||
self.values.get("FillColor")
|
||||
}
|
||||
|
||||
pub fn fill_tint(&self) -> Option<f64> {
|
||||
self.values.num("FillTint")
|
||||
}
|
||||
|
||||
/// The MIME fallback when a linked graphic's bytes cannot be sniffed.
|
||||
pub fn fallback_mime(&self) -> &'static str {
|
||||
let format = self.link().and_then(Link::format).unwrap_or("");
|
||||
match format {
|
||||
value if value.contains("EPS") || self.kind == GraphicKind::EPS => "application/postscript",
|
||||
value if value.contains("JPEG") => "image/jpeg",
|
||||
value if value.contains("TIFF") => "image/tiff",
|
||||
value if value.contains("PDF") || self.kind == GraphicKind::PDF => "application/pdf",
|
||||
value if value.contains("GIF") => "image/gif",
|
||||
_ => "image/png",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layer_options(&self) -> Option<GraphicLayerOptions> {
|
||||
self.children
|
||||
.iter()
|
||||
.find_map(|child| match child {
|
||||
GraphicChild::Other(el) if el.local() == "GraphicLayerOption" => Some(el),
|
||||
GraphicChild::Other(el) => el.find("GraphicLayerOption"),
|
||||
GraphicChild::Link(link) => {
|
||||
link.rest.iter().find_map(|el| if el.local() == "GraphicLayerOption" { Some(el) } else { el.find("GraphicLayerOption") })
|
||||
}
|
||||
})
|
||||
.or_else(|| self.values.prop_el("GraphicLayerOption"))
|
||||
.map(GraphicLayerOptions::from_el)
|
||||
}
|
||||
|
||||
pub fn pdf_attribute(&self) -> Option<PdfAttribute> {
|
||||
self.children
|
||||
.iter()
|
||||
.find_map(|child| match child {
|
||||
GraphicChild::Other(el) if el.local() == "PDFAttribute" => Some(el),
|
||||
GraphicChild::Other(el) => el.find("PDFAttribute"),
|
||||
GraphicChild::Link(link) => {
|
||||
link.rest.iter().find_map(|el| if el.local() == "PDFAttribute" { Some(el) } else { el.find("PDFAttribute") })
|
||||
}
|
||||
})
|
||||
.or_else(|| self.values.prop_el("PDFAttribute"))
|
||||
.map(|el| PdfAttribute { values: Values::of(el) })
|
||||
}
|
||||
|
||||
/// `Properties/GraphicBounds`: `top left bottom right` in the graphic's inner coordinates.
|
||||
pub fn graphic_bounds(&self) -> Option<[f64; 4]> {
|
||||
let b = self.values.prop_el("GraphicBounds")?;
|
||||
|
||||
@@ -2,9 +2,116 @@
|
||||
//! anchored page items, hyperlink and change containers).
|
||||
|
||||
use craft_xml::{El, Local, Node};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
use super::{PageItem, Values, tidy};
|
||||
|
||||
/// Resolved IDML anchored-object options from an item and its inherited style levels.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct AnchoredObjectValues {
|
||||
pub position: String,
|
||||
pub anchor_point: String,
|
||||
pub horizontal_alignment: String,
|
||||
pub horizontal_reference: String,
|
||||
pub vertical_alignment: String,
|
||||
pub vertical_reference: String,
|
||||
pub pin_position: bool,
|
||||
pub spine_relative: bool,
|
||||
pub space_above: f64,
|
||||
pub x_offset: f64,
|
||||
pub y_offset: f64,
|
||||
}
|
||||
|
||||
/// An IDML `AnchoredObjectSetting` element.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct AnchoredObjectSetting {
|
||||
attributes: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl AnchoredObjectSetting {
|
||||
pub fn inline_position(y_offset: f64) -> Self {
|
||||
Self { attributes: vec![("AnchoredPosition".into(), "InlinePosition".into()), ("AnchorYoffset".into(), crate::value::num(y_offset))] }
|
||||
}
|
||||
|
||||
pub fn above_line(horizontal_alignment: &str, space_above: f64, y_offset: f64) -> Self {
|
||||
Self {
|
||||
attributes: vec![
|
||||
("AnchoredPosition".into(), "AboveLine".into()),
|
||||
("HorizontalAlignment".into(), horizontal_alignment.into()),
|
||||
("AnchorSpaceAbove".into(), crate::value::num(space_above)),
|
||||
("AnchorYoffset".into(), crate::value::num(y_offset)),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn anchored(
|
||||
anchor_point: &str,
|
||||
horizontal_reference: &str,
|
||||
vertical_reference: &str,
|
||||
horizontal_alignment: &str,
|
||||
vertical_alignment: &str,
|
||||
x_offset: f64,
|
||||
y_offset: f64,
|
||||
pin_position: bool,
|
||||
spine_relative: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
attributes: vec![
|
||||
("AnchoredPosition".into(), "Anchored".into()),
|
||||
("AnchorPoint".into(), anchor_point.into()),
|
||||
("HorizontalReferencePoint".into(), horizontal_reference.into()),
|
||||
("VerticalReferencePoint".into(), vertical_reference.into()),
|
||||
("HorizontalAlignment".into(), horizontal_alignment.into()),
|
||||
("VerticalAlignment".into(), vertical_alignment.into()),
|
||||
("AnchorXoffset".into(), crate::value::num(x_offset)),
|
||||
("AnchorYoffset".into(), crate::value::num(y_offset)),
|
||||
("PinPosition".into(), bool_string(pin_position)),
|
||||
("SpineRelative".into(), bool_string(spine_relative)),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_el(&self) -> El {
|
||||
let mut element = El::new("AnchoredObjectSetting");
|
||||
for (key, value) in &self.attributes {
|
||||
element.set_attr(key, value);
|
||||
}
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
fn bool_string(value: bool) -> String {
|
||||
if value { "true" } else { "false" }.into()
|
||||
}
|
||||
|
||||
impl AnchoredObjectValues {
|
||||
/// Resolves attributes item-first from `AnchoredObjectSetting` children.
|
||||
pub fn from_elements(elements: &[&El]) -> Self {
|
||||
let get = |key: &str, default: &str| {
|
||||
elements
|
||||
.iter()
|
||||
.filter_map(|element| element.child(Local("AnchoredObjectSetting")))
|
||||
.find_map(|setting| Values::of(setting).get(key).map(str::to_owned))
|
||||
.unwrap_or_else(|| default.to_owned())
|
||||
};
|
||||
let number = |key: &str| get(key, "0").parse::<f64>().ok().filter(|value| value.is_finite()).unwrap_or(0.0);
|
||||
Self {
|
||||
position: get("AnchoredPosition", "InlinePosition"),
|
||||
anchor_point: get("AnchorPoint", "BottomRightAnchor"),
|
||||
horizontal_alignment: get("HorizontalAlignment", "LeftAlign"),
|
||||
horizontal_reference: get("HorizontalReferencePoint", "TextFrame"),
|
||||
vertical_alignment: get("VerticalAlignment", "BottomAlign"),
|
||||
vertical_reference: get("VerticalReferencePoint", "LineBaseline"),
|
||||
pin_position: get("PinPosition", "true") == "true",
|
||||
spine_relative: get("SpineRelative", "false") == "true",
|
||||
space_above: number("AnchorSpaceAbove"),
|
||||
x_offset: number("AnchorXoffset"),
|
||||
y_offset: number("AnchorYoffset"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Story`.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Story {
|
||||
@@ -12,6 +119,26 @@ pub struct Story {
|
||||
pub children: Vec<TextNode>,
|
||||
}
|
||||
|
||||
/// The IDML `StoryPreference` element written for a story.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct StoryPreference {
|
||||
pub optical_margin_alignment: bool,
|
||||
pub optical_margin_size: f64,
|
||||
pub vertical: bool,
|
||||
pub right_to_left: bool,
|
||||
}
|
||||
|
||||
impl StoryPreference {
|
||||
pub fn to_el(self) -> El {
|
||||
El::new("StoryPreference")
|
||||
.with_attr("OpticalMarginAlignment", self.optical_margin_alignment)
|
||||
.with_attr("OpticalMarginSize", self.optical_margin_size)
|
||||
.with_attr("FrameType", "TextFrameType")
|
||||
.with_attr("StoryOrientation", if self.vertical { "Vertical" } else { "Horizontal" })
|
||||
.with_attr("StoryDirection", if self.right_to_left { "RightToLeftDirection" } else { "LeftToRightDirection" })
|
||||
}
|
||||
}
|
||||
|
||||
/// One node of a story's text, in document order. IDML nests freely (an `XMLElement` or a
|
||||
/// `HyperlinkTextSource` can hold paragraph ranges or content), so every container holds these.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -24,10 +151,19 @@ pub enum TextNode {
|
||||
Content(Vec<ContentPart>),
|
||||
/// `Br`: the end of a paragraph.
|
||||
Break,
|
||||
/// A processing instruction at a structural story position. The parent serializer preserves
|
||||
/// it in place; `to_el` represents it as a one-instruction `Content` fragment.
|
||||
ProcessingInstruction {
|
||||
target: String,
|
||||
data: String,
|
||||
},
|
||||
/// Text inside another element ([`CONTAINERS`]): a hyperlink's source, a tracked change, an
|
||||
/// XML element, a cross-reference's source, a footnote, a note.
|
||||
Container(Container),
|
||||
Table(Table),
|
||||
Anchor(StoryAnchor),
|
||||
PageReference(PageReference),
|
||||
TextVariable(Values),
|
||||
/// A page item anchored in the text.
|
||||
Item(PageItem),
|
||||
/// Anything else (`StoryPreference`, `HyperlinkTextDestination`, `TextVariableInstance`, …),
|
||||
@@ -35,6 +171,264 @@ pub enum TextNode {
|
||||
Other(El),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StoryAnchor {
|
||||
pub name: String,
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
impl StoryAnchor {
|
||||
/// Builds the IDML `Self` value for a hyperlink text destination.
|
||||
pub fn destination_self(id: u64) -> String {
|
||||
format!("HyperlinkTextDestination/a{id}")
|
||||
}
|
||||
|
||||
/// Writes an IDML `HyperlinkTextDestination` from a DesignCraft-independent anchor identity.
|
||||
pub fn destination_el(id: u64, name: &str) -> El {
|
||||
El::new("HyperlinkTextDestination")
|
||||
.with_attr("Self", Self::destination_self(id))
|
||||
.with_attr("Name", name)
|
||||
.with_attr("Hidden", "false")
|
||||
.with_attr("DestinationUniqueKey", id & 0xFFFF_FFFF)
|
||||
}
|
||||
|
||||
pub fn self_id(&self) -> Option<&str> {
|
||||
self.values.self_id()
|
||||
}
|
||||
pub fn label(&self) -> Option<&str> {
|
||||
self.values.name()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct PageReference {
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
/// Nested index topics and their See / See also references for one IDML `Index`.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct IndexValues {
|
||||
pub topics: BTreeMap<Vec<String>, String>,
|
||||
pub cross_references: Vec<IndexCrossReferenceValues>,
|
||||
}
|
||||
|
||||
/// An index cross-reference attached to a topic path.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct IndexCrossReferenceValues {
|
||||
pub from: Vec<String>,
|
||||
pub reference_type: String,
|
||||
pub target: String,
|
||||
}
|
||||
|
||||
/// A topic path and its sort keys, as they are saved on nested IDML `Topic` elements.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct IndexTopicPath {
|
||||
pub path: Vec<String>,
|
||||
pub sort: Vec<String>,
|
||||
}
|
||||
|
||||
/// An IDML index cross-reference as stored under an index topic.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct IndexReference {
|
||||
pub from: IndexTopicPath,
|
||||
pub referenced_topic: Option<String>,
|
||||
pub reference_type: Option<String>,
|
||||
}
|
||||
|
||||
/// Parsed IDML index topics and cross-references.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct IndexDocument {
|
||||
pub topics: HashMap<String, IndexTopicPath>,
|
||||
pub cross_references: Vec<IndexReference>,
|
||||
}
|
||||
|
||||
impl IndexDocument {
|
||||
pub fn from_el(element: &El) -> Self {
|
||||
let mut result = Self::default();
|
||||
let mut stack: Vec<(&El, IndexTopicPath)> =
|
||||
element.elements().filter(|child| child.local() == "Topic").map(|topic| (topic, IndexTopicPath::default())).collect();
|
||||
while let Some((topic, prefix)) = stack.pop() {
|
||||
let name = topic.attr("Name").unwrap_or_default().to_string();
|
||||
let mut current = prefix.clone();
|
||||
current.path.push(name);
|
||||
if let Some(key) = topic.attr("SortOrder").filter(|key| !key.is_empty()) {
|
||||
current.sort.resize(prefix.path.len(), String::new());
|
||||
current.sort.push(key.to_string());
|
||||
}
|
||||
if let Some(self_id) = topic.attr("Self") {
|
||||
result.topics.insert(self_id.to_string(), current.clone());
|
||||
}
|
||||
for child in topic.elements() {
|
||||
if child.local() == "CrossReference" {
|
||||
result.cross_references.push(IndexReference {
|
||||
from: current.clone(),
|
||||
referenced_topic: child.attr("ReferencedTopic").map(str::to_string),
|
||||
reference_type: child.attr("CrossReferenceType").map(str::to_string),
|
||||
});
|
||||
} else if child.local() == "Topic" {
|
||||
stack.push((child, current.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl IndexValues {
|
||||
/// Builds the IDML `Self` value for a topic path.
|
||||
pub fn topic_self(path: &[String]) -> String {
|
||||
let mut id = String::from("ix");
|
||||
for part in path {
|
||||
id.push_str("Topicn");
|
||||
id.push_str(&crate::value::escape_id(part));
|
||||
}
|
||||
id
|
||||
}
|
||||
|
||||
/// Writes the nested IDML index grammar, assigning a fresh ID to each cross-reference.
|
||||
pub fn to_el(&self, mut fresh: impl FnMut() -> String) -> El {
|
||||
let mut topics = self.topics.clone();
|
||||
for reference in &self.cross_references {
|
||||
topics.entry(vec![reference.target.clone()]).or_default();
|
||||
}
|
||||
|
||||
fn build(
|
||||
topics: &BTreeMap<Vec<String>, String>,
|
||||
references: &[IndexCrossReferenceValues],
|
||||
prefix: &[String],
|
||||
fresh: &mut impl FnMut() -> String,
|
||||
) -> Vec<El> {
|
||||
let mut out = Vec::new();
|
||||
for (path, sort_order) in topics.iter().filter(|(path, _)| path.len() == prefix.len() + 1 && path.starts_with(prefix)) {
|
||||
let mut element = El::new("Topic")
|
||||
.with_attr("Self", IndexValues::topic_self(path))
|
||||
.with_attr("SortOrder", sort_order)
|
||||
.with_attr("Name", path.last().map_or("", String::as_str));
|
||||
for reference in references.iter().filter(|reference| reference.from == *path) {
|
||||
element.push(
|
||||
El::new("CrossReference")
|
||||
.with_attr("Self", fresh())
|
||||
.with_attr("ReferencedTopic", IndexValues::topic_self(std::slice::from_ref(&reference.target)))
|
||||
.with_attr("CrossReferenceType", &reference.reference_type)
|
||||
.with_attr("CustomTypeString", ""),
|
||||
);
|
||||
}
|
||||
element.children.extend(build(topics, references, path, fresh).into_iter().map(Node::El));
|
||||
out.push(element);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
let mut element = El::new("Index").with_attr("Self", "ix");
|
||||
element.children = build(&topics, &self.cross_references, &[], &mut fresh).into_iter().map(Node::El).collect();
|
||||
element
|
||||
}
|
||||
}
|
||||
|
||||
/// Text saved inside an IDML `CrossReferenceSource` before the application regenerates it from
|
||||
/// the cross-reference format. Page-number variables are separate segments so callers can retain
|
||||
/// the last displayed page while mapping to their own text model.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct SavedCrossReference {
|
||||
pub self_id: Option<String>,
|
||||
pub applied_format: Option<String>,
|
||||
pub parts: Vec<SavedCrossReferencePart>,
|
||||
/// The first directly nested character range, which supplies the marker's formatting.
|
||||
pub character_range: Option<Range>,
|
||||
}
|
||||
|
||||
/// The IDML cross-reference source wrapper around its saved character range.
|
||||
pub struct CrossReferenceSource;
|
||||
|
||||
impl CrossReferenceSource {
|
||||
pub fn to_el(self_id: &str, applied_format: &str, name: &str, character_range: El) -> El {
|
||||
El::new("CrossReferenceSource")
|
||||
.with_attr("Self", self_id)
|
||||
.with_attr("AppliedFormat", applied_format)
|
||||
.with_attr("Name", name)
|
||||
.with_attr("Hidden", "false")
|
||||
.with_child(character_range)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum SavedCrossReferencePart {
|
||||
Text(String),
|
||||
PageNumber { result_text: Option<String> },
|
||||
}
|
||||
|
||||
impl SavedCrossReference {
|
||||
/// Reads the bounded saved-text grammar. `page_variables` contains IDs whose variable type is
|
||||
/// the cross-reference page number; when the document omits variable definitions, InDesign's
|
||||
/// conventional `TV XRefPageNumber` suffix is recognized.
|
||||
pub fn from_el(el: &El, page_variables: &[String]) -> SavedCrossReference {
|
||||
let mut parsed = SavedCrossReference {
|
||||
self_id: el.attr("Self").map(str::to_string),
|
||||
applied_format: el.attr("AppliedFormat").map(str::to_string),
|
||||
parts: Vec::new(),
|
||||
character_range: el.find(Local("CharacterStyleRange")).and_then(|range| match TextNode::from_el(range) {
|
||||
TextNode::Character(range) => Some(range),
|
||||
_ => None,
|
||||
}),
|
||||
};
|
||||
saved_xref_parts(el, page_variables, &mut parsed.parts, 0);
|
||||
parsed
|
||||
}
|
||||
|
||||
pub fn from_container(container: &Container, page_variables: &[String]) -> SavedCrossReference {
|
||||
Self::from_el(&container.to_el(), page_variables)
|
||||
}
|
||||
}
|
||||
|
||||
fn saved_xref_parts(el: &El, page_variables: &[String], parts: &mut Vec<SavedCrossReferencePart>, depth: usize) {
|
||||
if depth > 16 {
|
||||
return;
|
||||
}
|
||||
for child in el.elements() {
|
||||
match child.local() {
|
||||
"Content" => parts.push(SavedCrossReferencePart::Text(child.deep_text())),
|
||||
"TextVariableInstance" => {
|
||||
let id = child.attr("AssociatedTextVariable").unwrap_or_default();
|
||||
let page_number =
|
||||
page_variables.iter().any(|variable| variable == id) || (page_variables.is_empty() && id.ends_with("TV XRefPageNumber"));
|
||||
if page_number {
|
||||
parts.push(SavedCrossReferencePart::PageNumber { result_text: child.attr("ResultText").map(str::to_string) });
|
||||
} else {
|
||||
parts.push(SavedCrossReferencePart::Text(child.attr("ResultText").unwrap_or_default().to_string()));
|
||||
}
|
||||
}
|
||||
"Change" if child.attr("ChangeType") == Some("DeletedText") => {}
|
||||
"CharacterStyleRange" | "Change" => saved_xref_parts(child, page_variables, parts, depth + 1),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PageReference {
|
||||
pub fn referenced_topic(&self) -> Option<&str> {
|
||||
self.values.get("ReferencedTopic")
|
||||
}
|
||||
pub fn reference_type(&self) -> Option<&str> {
|
||||
self.values.get("PageReferenceType")
|
||||
}
|
||||
pub fn limit(&self) -> Option<f64> {
|
||||
self.values.num("PageReferenceLimit")
|
||||
}
|
||||
|
||||
/// Writes a page-reference record from the shared index model's type and target.
|
||||
pub fn to_el(self_id: &str, reference_type: &str, topic: &str, id: usize, limit: Option<u32>) -> El {
|
||||
let mut el = El::new("PageReference")
|
||||
.with_attr("Self", self_id)
|
||||
.with_attr("PageReferenceType", reference_type)
|
||||
.with_attr("ReferencedTopic", topic)
|
||||
.with_attr("Id", id);
|
||||
if let Some(limit) = limit {
|
||||
el.set_attr("PageReferenceLimit", limit);
|
||||
}
|
||||
el
|
||||
}
|
||||
}
|
||||
|
||||
/// The elements read as [`TextNode::Container`].
|
||||
pub const CONTAINERS: [&str; 7] = ["HyperlinkTextSource", "Change", "XMLElement", "CrossReferenceSource", "Footnote", "Note", "EndnoteRange"];
|
||||
|
||||
@@ -54,6 +448,54 @@ pub struct Container {
|
||||
pub children: Vec<TextNode>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ContainerKind {
|
||||
HyperlinkTextSource,
|
||||
Change,
|
||||
XMLElement,
|
||||
CrossReferenceSource,
|
||||
Footnote,
|
||||
Note,
|
||||
EndnoteRange,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TrackedChangeKind {
|
||||
Deleted,
|
||||
Inserted,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Container {
|
||||
pub fn to_el(&self) -> El {
|
||||
with_children(self.values.element(&self.name), &self.children)
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> ContainerKind {
|
||||
match self.name.as_str() {
|
||||
"HyperlinkTextSource" => ContainerKind::HyperlinkTextSource,
|
||||
"Change" => ContainerKind::Change,
|
||||
"XMLElement" => ContainerKind::XMLElement,
|
||||
"CrossReferenceSource" => ContainerKind::CrossReferenceSource,
|
||||
"Footnote" => ContainerKind::Footnote,
|
||||
"Note" => ContainerKind::Note,
|
||||
_ => ContainerKind::EndnoteRange,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn self_id(&self) -> Option<&str> {
|
||||
self.values.self_id()
|
||||
}
|
||||
|
||||
pub fn change_kind(&self) -> Option<TrackedChangeKind> {
|
||||
(self.kind() == ContainerKind::Change).then(|| match self.values.get("ChangeType") {
|
||||
Some("DeletedText") => TrackedChangeKind::Deleted,
|
||||
Some("InsertedText" | "MovedText") => TrackedChangeKind::Inserted,
|
||||
_ => TrackedChangeKind::Other,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A piece of a `Content` element.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ContentPart {
|
||||
@@ -85,6 +527,11 @@ pub enum Ace {
|
||||
}
|
||||
|
||||
impl Ace {
|
||||
/// Parses an IDML ACE instruction's decimal code.
|
||||
pub fn from_decimal(value: &str) -> Option<Ace> {
|
||||
value.trim().parse::<u32>().ok().map(Ace::from_code)
|
||||
}
|
||||
|
||||
pub fn from_code(code: u32) -> Ace {
|
||||
match code {
|
||||
4 => Ace::FootnoteNumber,
|
||||
@@ -115,6 +562,63 @@ pub struct Table {
|
||||
pub children: Vec<TableChild>,
|
||||
}
|
||||
|
||||
/// The row and column grid of an IDML table, read without allocating cell models.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TableLayout {
|
||||
pub rows: Vec<TableRow>,
|
||||
pub columns: Vec<TableColumn>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TableRow {
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TableColumn {
|
||||
pub values: Values,
|
||||
}
|
||||
|
||||
impl TableLayout {
|
||||
pub fn from_el(el: &El) -> TableLayout {
|
||||
Self::from_table(&Table::from_el(el))
|
||||
}
|
||||
|
||||
pub fn from_table(table: &Table) -> TableLayout {
|
||||
let mut layout = TableLayout::default();
|
||||
for child in &table.children {
|
||||
if let TableChild::Other(element) = child {
|
||||
match element.local() {
|
||||
"Row" => layout.rows.push(TableRow { values: Values::of(element) }),
|
||||
"Column" => layout.columns.push(TableColumn { values: Values::of(element) }),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
layout
|
||||
}
|
||||
}
|
||||
|
||||
impl TableRow {
|
||||
pub fn auto_grow(&self) -> Option<bool> {
|
||||
self.values.boolean("AutoGrow")
|
||||
}
|
||||
|
||||
pub fn minimum_height(&self) -> Option<f64> {
|
||||
self.values.num("MinimumHeight")
|
||||
}
|
||||
|
||||
pub fn single_row_height(&self) -> Option<f64> {
|
||||
self.values.num("SingleRowHeight")
|
||||
}
|
||||
}
|
||||
|
||||
impl TableColumn {
|
||||
pub fn single_column_width(&self) -> Option<f64> {
|
||||
self.values.num("SingleColumnWidth")
|
||||
}
|
||||
}
|
||||
|
||||
/// A child of a table, in document order.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum TableChild {
|
||||
@@ -130,13 +634,34 @@ pub struct Cell {
|
||||
pub children: Vec<TextNode>,
|
||||
}
|
||||
|
||||
/// Format-level options on an IDML table cell, before DesignCraft maps them into its model.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct CellOptions {
|
||||
pub fill_color: Option<String>,
|
||||
pub fill_tint: Option<f64>,
|
||||
pub insets: [Option<f64>; 4],
|
||||
pub vertical_justification: Option<String>,
|
||||
pub first_baseline_offset: Option<String>,
|
||||
pub minimum_first_baseline_offset: Option<f64>,
|
||||
pub rotation_angle: Option<f64>,
|
||||
pub edge_stroke_priorities: [i32; 4],
|
||||
pub edge_strokes: [super::StrokeValues; 4],
|
||||
}
|
||||
|
||||
/// The children of `el` other than `<Properties>`, as text nodes.
|
||||
fn nodes(el: &El) -> Vec<TextNode> {
|
||||
el.elements().filter(|e| !e.is(Local("Properties"))).map(TextNode::from_el).collect()
|
||||
el.children
|
||||
.iter()
|
||||
.filter_map(|node| match node {
|
||||
Node::El(e) if !e.is(Local("Properties")) => Some(TextNode::from_el(e)),
|
||||
Node::Pi { target, data } => Some(TextNode::ProcessingInstruction { target: target.clone(), data: data.clone() }),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn with_children(mut el: El, children: &[TextNode]) -> El {
|
||||
el.children.extend(children.iter().map(|c| Node::El(c.to_el())));
|
||||
el.children.extend(children.iter().map(TextNode::to_node));
|
||||
el
|
||||
}
|
||||
|
||||
@@ -148,6 +673,11 @@ impl TextNode {
|
||||
"Content" => TextNode::Content(content_parts(el)),
|
||||
"Br" => TextNode::Break,
|
||||
"Table" => TextNode::Table(Table::from_el(el)),
|
||||
"HyperlinkTextDestination" | "ParagraphDestination" => {
|
||||
TextNode::Anchor(StoryAnchor { name: el.local().to_string(), values: Values::of(el) })
|
||||
}
|
||||
"PageReference" => TextNode::PageReference(PageReference { values: Values::of(el) }),
|
||||
"TextVariableInstance" => TextNode::TextVariable(Values::of(el)),
|
||||
n if CONTAINERS.contains(&n) => TextNode::Container(Container { name: n.to_string(), values: Values::of(el), children: nodes(el) }),
|
||||
_ => PageItem::from_el(el).map_or_else(|| TextNode::Other(tidy(el)), TextNode::Item),
|
||||
}
|
||||
@@ -170,13 +700,30 @@ impl TextNode {
|
||||
c
|
||||
}
|
||||
TextNode::Break => El::new("Br"),
|
||||
TextNode::ProcessingInstruction { target, data } => {
|
||||
let mut content = El::new("Content");
|
||||
content.children.push(Node::Pi { target: target.clone(), data: data.clone() });
|
||||
content
|
||||
}
|
||||
TextNode::Container(c) => with_children(c.values.element(&c.name), &c.children),
|
||||
TextNode::Table(t) => t.to_el(),
|
||||
TextNode::Anchor(a) => a.values.element(&a.name),
|
||||
TextNode::PageReference(r) => r.values.element("PageReference"),
|
||||
TextNode::TextVariable(v) => v.element("TextVariableInstance"),
|
||||
TextNode::Item(i) => i.to_el(),
|
||||
TextNode::Other(e) => e.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes this node in its parent position, preserving processing instructions as XML
|
||||
/// instructions rather than wrapping them in an element.
|
||||
pub fn to_node(&self) -> Node {
|
||||
match self {
|
||||
TextNode::ProcessingInstruction { target, data } => Node::Pi { target: target.clone(), data: data.clone() },
|
||||
_ => Node::El(self.to_el()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The nodes this one holds; none for content, breaks, page items and others.
|
||||
pub fn children(&self) -> &[TextNode] {
|
||||
match self {
|
||||
@@ -226,6 +773,26 @@ impl Story {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `StoryDirection` selects right-to-left order.
|
||||
pub fn right_to_left(&self) -> bool {
|
||||
self.preference().and_then(|values| values.get("StoryDirection").map(str::to_string)).as_deref() == Some("RightToLeftDirection")
|
||||
}
|
||||
|
||||
/// Whether `StoryOrientation` selects vertical composition.
|
||||
pub fn vertical(&self) -> bool {
|
||||
self.preference().and_then(|values| values.get("StoryOrientation").map(str::to_string)).as_deref() == Some("Vertical")
|
||||
}
|
||||
|
||||
/// Whether optical margin alignment is enabled; false when absent.
|
||||
pub fn optical_margin_enabled(&self) -> bool {
|
||||
self.preference().and_then(|values| values.boolean("OpticalMarginAlignment")) == Some(true)
|
||||
}
|
||||
|
||||
/// The `OpticalMarginSize`, if finite.
|
||||
pub fn optical_margin_size(&self) -> Option<f64> {
|
||||
self.preference().and_then(|values| values.num("OpticalMarginSize"))
|
||||
}
|
||||
|
||||
/// The story's paragraph ranges in reading order, those inside containers included, but not
|
||||
/// those of its tables, footnotes or notes (which are text of their own).
|
||||
pub fn paragraphs(&self) -> Vec<&Range> {
|
||||
@@ -275,6 +842,11 @@ fn text(nodes: &[TextNode], ace: &dyn Fn(Ace) -> Option<char>) -> String {
|
||||
}
|
||||
}
|
||||
TextNode::Break => out.push('\n'),
|
||||
TextNode::ProcessingInstruction { target, data } if target == "ACE" => {
|
||||
if let Ok(code) = data.trim().parse::<u32>() {
|
||||
out.extend(ace(Ace::from_code(code)));
|
||||
}
|
||||
}
|
||||
TextNode::Paragraph(r) | TextNode::Character(r) => stack.extend(r.children.iter().rev()),
|
||||
TextNode::Container(c) if inline_container(&c.name) => stack.extend(c.children.iter().rev()),
|
||||
_ => {}
|
||||
@@ -284,6 +856,11 @@ fn text(nodes: &[TextNode], ace: &dyn Fn(Ace) -> Option<char>) -> String {
|
||||
}
|
||||
|
||||
impl Range {
|
||||
/// `ParagraphBreakType`, except `Anywhere` which means no forced break.
|
||||
pub fn paragraph_break_type(&self) -> Option<&str> {
|
||||
self.values.get("ParagraphBreakType").filter(|kind| *kind != "Anywhere")
|
||||
}
|
||||
|
||||
/// `AppliedParagraphStyle` of a paragraph range.
|
||||
pub fn paragraph_style(&self) -> Option<&str> {
|
||||
self.values.get("AppliedParagraphStyle")
|
||||
@@ -378,6 +955,14 @@ impl Table {
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
pub fn from_el(el: &El) -> Cell {
|
||||
Cell { values: Values::of(el), children: nodes(el) }
|
||||
}
|
||||
|
||||
pub fn to_el(&self) -> El {
|
||||
with_children(self.values.element("Cell"), &self.children)
|
||||
}
|
||||
|
||||
/// `Name`: `column:row` of the cell's top left (`2:0`).
|
||||
pub fn position(&self) -> Option<(u32, u32)> {
|
||||
let (c, r) = self.values.name()?.split_once(':')?;
|
||||
@@ -390,6 +975,27 @@ impl Cell {
|
||||
(n("ColumnSpan"), n("RowSpan"))
|
||||
}
|
||||
|
||||
pub fn options(&self) -> CellOptions {
|
||||
let inset = |current: &str, legacy: &str| self.values.num(current).or_else(|| self.values.num(legacy));
|
||||
let priority = |side: &str| self.values.prop(&format!("{side}StrokePriority")).and_then(|v| v.trim().parse().ok()).unwrap_or(0);
|
||||
CellOptions {
|
||||
fill_color: self.values.value("FillColor"),
|
||||
fill_tint: self.values.num("FillTint"),
|
||||
insets: [
|
||||
inset("TextTopInset", "TopInset"),
|
||||
inset("TextLeftInset", "LeftInset"),
|
||||
inset("TextBottomInset", "BottomInset"),
|
||||
inset("TextRightInset", "RightInset"),
|
||||
],
|
||||
vertical_justification: self.values.value("VerticalJustification"),
|
||||
first_baseline_offset: self.values.value("FirstBaselineOffset"),
|
||||
minimum_first_baseline_offset: self.values.num("MinimumFirstBaselineOffset"),
|
||||
rotation_angle: self.values.num("RotationAngle"),
|
||||
edge_stroke_priorities: [priority("TopEdge"), priority("LeftEdge"), priority("BottomEdge"), priority("RightEdge")],
|
||||
edge_strokes: ["TopEdge", "LeftEdge", "BottomEdge", "RightEdge"].map(|side| self.values.stroke_values(side)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn paragraphs(&self) -> Vec<&Range> {
|
||||
paragraphs(&self.children)
|
||||
}
|
||||
|
||||
+27
-3
@@ -22,6 +22,25 @@ pub const fn xml_limits() -> craft_xml::Limits {
|
||||
/// `META-INF/container.xml` of a package.
|
||||
pub const CONTAINER_XML: &str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n\t<rootfiles>\n\t\t<rootfile full-path=\"designmap.xml\" media-type=\"text/xml\"/>\n\t</rootfiles>\n</container>\n";
|
||||
|
||||
/// The standard root element for `designmap.xml`, including its required namespace and core
|
||||
/// document identity fields. The caller supplies DesignCraft-independent values; children are
|
||||
/// appended after construction.
|
||||
pub fn designmap_root(self_id: &str, story_list: &str, name: &str, zero_point: &str, active_layer: &str) -> El {
|
||||
El::new("Document")
|
||||
.with_attr("xmlns:idPkg", PACKAGING_NS)
|
||||
.with_attr("DOMVersion", DOM_VERSION)
|
||||
.with_attr("Self", self_id)
|
||||
.with_attr("StoryList", story_list)
|
||||
.with_attr("Name", name)
|
||||
.with_attr("ZeroPoint", zero_point)
|
||||
.with_attr("ActiveLayer", active_layer)
|
||||
}
|
||||
|
||||
/// An `idPkg` reference in `designmap.xml` to a part stored at `src`.
|
||||
pub fn designmap_include(kind: &str, src: &str) -> El {
|
||||
El::new(format!("idPkg:{kind}")).with_attr("src", src)
|
||||
}
|
||||
|
||||
/// Parses a part into its root element with [`xml_limits`].
|
||||
///
|
||||
/// Names are kept as written (`idPkg:Story`), text is kept exactly (it is story content), CDATA
|
||||
@@ -112,10 +131,15 @@ pub fn part_xml(root: &El) -> Vec<u8> {
|
||||
/// `designmap.xml` as bytes: [`part_xml`] with the `<?aid …?>` instruction that tells InDesign
|
||||
/// what the file is.
|
||||
pub fn designmap_xml(root: &El) -> Vec<u8> {
|
||||
let pi = format!("<?aid style=\"50\" type=\"document\" readerVersion=\"6.0\" featureSet=\"257\" product=\"{}(1)\" ?>", DOM_VERSION);
|
||||
designmap_xml_with_pi(root, &pi)
|
||||
}
|
||||
|
||||
/// A designmap part with the caller's `<?aid …?>` metadata instruction.
|
||||
pub fn designmap_xml_with_pi(root: &El, pi: &str) -> Vec<u8> {
|
||||
let mut s = String::from(DECL_STANDALONE);
|
||||
s.push_str("<?aid style=\"50\" type=\"document\" readerVersion=\"6.0\" featureSet=\"257\" product=\"");
|
||||
s.push_str(DOM_VERSION);
|
||||
s.push_str("(1)\" ?>\n");
|
||||
s.push_str(pi);
|
||||
s.push('\n');
|
||||
root.write_xml_pretty(&mut s, "\t");
|
||||
s.into_bytes()
|
||||
}
|
||||
@@ -30,7 +30,15 @@ pub fn nums(s: &str) -> Vec<f64> {
|
||||
/// An `ItemTransform`: the six coefficients `a b c d tx ty`, or `None` when the value is not
|
||||
/// six finite numbers.
|
||||
pub fn matrix(s: &str) -> Option<[f64; 6]> {
|
||||
<[f64; 6]>::try_from(nums(s)).ok()
|
||||
let mut tokens = s.split_whitespace();
|
||||
let mut matrix = [0.0_f64; 6];
|
||||
for value in &mut matrix {
|
||||
*value = tokens.next()?.parse().ok()?;
|
||||
if !value.is_finite() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
tokens.next().is_none().then_some(matrix)
|
||||
}
|
||||
|
||||
/// A transform as an `ItemTransform` value.
|
||||
|
||||
@@ -266,3 +266,17 @@ fn deeply_nested_groups_are_read_and_walked() {
|
||||
let again = Spread::from_el(&parse_part(&part_xml(&s.to_el())).unwrap());
|
||||
assert_eq!(again, s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_variable_decoration_uses_preference_and_falls_back_to_legacy_root_values() {
|
||||
let preferred = parse_part(
|
||||
br#"<TextVariable Name="Last Page Number" VariableType="LastPageNumberType" TextBefore="legacy " TextAfter=" legacy"><PageNumberVariablePreference TextBefore="of " TextAfter=" pages" Scope="SectionScope"/></TextVariable>"#,
|
||||
)
|
||||
.unwrap();
|
||||
let variable = craft_idml::model::TextVariableValues::from_el(&preferred);
|
||||
assert_eq!((variable.before.as_str(), variable.after.as_str()), ("of ", " pages"));
|
||||
|
||||
let legacy = parse_part(br#"<TextVariable Name="Last Page Number" TextBefore="of " TextAfter=" pages"/>"#).unwrap();
|
||||
let variable = craft_idml::model::TextVariableValues::from_el(&legacy);
|
||||
assert_eq!((variable.before.as_str(), variable.after.as_str()), ("of ", " pages"));
|
||||
}
|
||||
Loaded 100 of 215 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user