diff --git a/.github/workflows/house-rules.yml b/.github/workflows/house-rules.yml new file mode 100644 index 0000000000..de64ad3ee4 --- /dev/null +++ b/.github/workflows/house-rules.yml @@ -0,0 +1,28 @@ +name: House rules + +on: + push: + branches: [main] + paths: + - AGENTS.md + - CLAUDE.md + - .github/workflows/house-rules.yml + pull_request: + paths: + - AGENTS.md + - CLAUDE.md + - .github/workflows/house-rules.yml + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: house-rules-${{ github.ref }} + cancel-in-progress: true + +jobs: + house-rules: + uses: storytold/craft-common-actions/.github/workflows/house-rules.yml@main + with: + rust: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2951acfc3a..89d3b647b1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,7 +18,14 @@ concurrency: cancel-in-progress: false jobs: + # Regenerates contributors/contributors.json for the release commit; every build bundles it. + credits: + permissions: + contents: read + pull-requests: read + uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main release: + needs: credits # Only this job publishes the GitHub release, so only it gets write access. permissions: contents: write @@ -31,6 +38,7 @@ jobs: craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 web-craft-fonts: true version: ${{ inputs.version }} + contributors-artifact: ${{ needs.credits.outputs.artifact-name }} secrets: APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }} APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }} diff --git a/AGENTS.md b/AGENTS.md index 04fb60a525..702f630871 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,10 +1,62 @@ + +## House rules + +These apply in every Craft repository. A later rule may be stricter, not weaker. The source is `standards/house-rules-core.md` in `storytold/craft-contrib`. Propose changes there, not in a copy. + +1. **A person is accountable.** Whoever runs an agent answers for what it opens and for its review comments. +2. **Check, then claim.** Before writing code, read the issue comments, search pull requests for `#N` and the keywords, and check recent commits on `main`. Comment on the issue first. Release a claim you will not finish. +3. **Verify, don't guess.** Open a definition before using it. Confirm the cause in the code, even when someone names a location. +4. **Keep fixes narrow and tested.** Fix what the issue describes, where the bug is. Add a test that fails without the fix. +5. **Review the diff before opening a pull request.** Identifiers exist as used, the test fails without the fix, nothing unrelated changed, and the description is true. +6. **Look for a better pull request.** If another fixes the same thing, close yours when it is better or equal, and say why. +7. **Report only what ran.** Say what you tested and what you could not run. Without a local build or test, say so and rely on CI before marking ready. +8. **`main` changes through a pull request.** Do not push to it. Branch protection enforces this. There is no exception. +9. **Stop and ask only about** licensing outside the allowlist, publishing, releases, a new remote, secrets, spending money, destructive git, and anything else hard to undo. Otherwise keep working. `plan/` is a gitignored notebook, not rules. Without it, do not ask for it and do not stop. +10. **Keep secrets and personal details out** of the repository, logs and requests. +11. **CI stays shared.** Workflows call `storytold/craft-common-actions`. Do not change CI in a feature or fix pull request. If CI cannot cover a change, open an issue there. +12. **Guards say why.** A check that looks removable says what it prevents. Read that before changing it. +13. **Instructions must be readable by everyone.** No notes about one machine, and no links to files a reader cannot open. `craftrules` is retired. Do not link to it. +14. **Keep pull requests moving.** Concurrent pull requests are fine; unanswered actionable feedback is not. Respond in 7 days, a reminder at 14, closure eligible at 30, from when an actionable request waits on the author. Suggestions and unconfirmed bot findings do not start the clock. A fix, explanation or agreed plan resets it; unrelated activity does not. Waiting on a reviewer or maintainer pauses it and does not count against the author. A maintainer decides closure; it is never automatic. Reuse search results, do not poll, and stop at a rate limit. +15. **No agent attribution.** A pull request does not say an agent wrote it. A repository rule may not require that. + + +## Rust Craft repositories + +These apply to native Craft apps and to shared Rust libraries. Select them by repository type. A repository that merely contains some Rust does not take this block. Rules later in a repository's file may be stricter, but not weaker. The source is `standards/house-rules-rust.md` in [storytold/craft-contrib](https://github.com/storytold/craft-contrib). + +1. **Never crash.** Non-test code does not panic and has no `unsafe`, except where the repository file names an exception. Return errors, treat input as hostile, and bound recursion and allocations. New production crate roots copy the `deny` attribute from an existing crate: `clippy::unwrap_used`, `clippy::expect_used`, `clippy::panic`, `clippy::unimplemented`, `clippy::todo`, `clippy::unreachable`. Every crash fix comes with a test that panicked before the fix. +2. **Clean room.** Learn from observed behaviour and public specifications. Do not copy another product's code, assets, strings or artwork. Check a dependency's licence before adding it. +3. **Assets and fonts.** Every asset is original or suitably licensed, and is recorded in `ATTRIBUTION.md` in the same change. Font files live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), not in the app repository. +4. **Rust only for native Craft apps.** No handwritten JavaScript or TypeScript, and no Tauri, Electron or webview shell, in those apps. Shared tooling follows its documented technical scope and maintainer-approved exceptions. Craft PR Review's existing Tauri and TypeScript UI is one such exception. It does not relax this rule for native apps. Preserve any existing web build. +5. **Time goes through `chrono`.** Do not hand-roll calendar conversion. For dates beyond what `chrono` can represent, follow [standards/time.md](https://github.com/storytold/craft-contrib/blob/main/standards/time.md). +6. **Contributor credits.** Add or edit only your own person's entry in `contributors/people.toml`, and only after asking them. Never guess a name or hand-edit `contributors.json`. + +## Licence allowlist + +Dependencies and assets use SPDX identifiers. An expression is allowed when every identifier is in this list. A repository may name a shorter list. It may not add an identifier here by editing its own copy of these rules. + +- `ISC` +- `MIT` +- `BSD-2-Clause` +- `BSD-3-Clause` +- `Apache-2.0` +- `OFL-1.1` +- `Ubuntu-font-1.0` +- `Bitstream-Vera` +- `CC0-1.0` +- `CC-BY-4.0` +- `CC-BY-SA-4.0` +- `LicenseRef-Public-Domain` + +`LicenseRef-Storyteller-Trademark` is not in this list. It covers Storyteller brand marks only, where a repository already says so. + + # WordCraft — instructions for agents WordCraft is a clean-room, open-source, Rust-native word processor targeting Microsoft Word parity — and going further on speed, openness and agent control. It runs natively on macOS, Windows, Linux and BSD, and on the web via WASM. Siblings with the same conventions: `../photocraft` (Photoshop), `../vectorcraft` (Illustrator), `../filmcraft` (Premiere), `../lightcraft` (Lightroom), `../pdfcraft` (Acrobat), `../effectcraft` (After Effects), `../designcraft` (InDesign). ## Start every session here -1. Read `plan/STATUS.md` (current milestone, next task), then the task in `plan/execution-plan.md` and the relevant `plan/architecture.md` section. `plan/` is gitignored (local only); if it's missing, start from `ROADMAP.md`, `docs/gaps.md` and `docs/roadmap.md` (Current focus). -2. Follow the autonomous operation protocol (`plan/execution-plan.md` §5). Don't stop to ask unless a decision is genuinely the owner's (licensing, publishing, pushing to new remotes, secrets). +Read [`ROADMAP.md`](ROADMAP.md), [`docs/gaps.md`](docs/gaps.md) and [`docs/roadmap.md`](docs/roadmap.md). `plan/` is an optional gitignored notebook. Do not ask for it and do not stop when it is absent. ## Never crash People trust WordCraft with their writing; a crash loses their work. **This outranks feature work**: never ship a feature through a panic path, and fix a crash before building on top of it. @@ -28,7 +80,7 @@ People trust WordCraft with their writing; a crash loses their work. **This outr - Never read, disassemble or copy anything inside the Word bundle (file names/listings only). Never commit files produced by Word. Never copy Microsoft wording beyond feature names. - File formats come from public specs: ECMA-376 / ISO 29500 (OOXML), the published RTF spec, OASIS ODF 1.3. Never copy GPL/LGPL/AGPL code (LibreOffice, AbiWord, pandoc…). -## Architecture (see `plan/architecture.md`) +## Architecture | Layer | Crate | Job | |---|---|---| | L0 | `geom` | units, rects, measurement parsing | @@ -44,7 +96,6 @@ People trust WordCraft with their writing; a crash loses their work. **This outr - **Everything is a command.** User-visible behaviour = a `CommandSpec` in `crates/engine/src/cmd/*.rs` (id, label, ribbon location, shortcut, params doc, `enabled`, `run`) + tests. UI-only commands (`ui.*`) live in `crates/ui-egui/src/lib.rs`. The ribbon, shortcuts, command search, CLI, control channel and MCP all dispatch by id. Programmatic calls never open dialogs. - **Parity is measured:** `crates/engine/src/catalog.rs` lists Word's ribbon/menu features with command ids. `cargo xtask parity` writes `docs/parity-checklist.md`; a test enforces a floor that only rises. Implement missing ids to raise it. - **Layering** is enforced by `cargo xtask layers`. Nothing below L6 depends on egui/eframe/winit/rfd. The UI is thin and reads `Session` state; colours come from `theme::Tokens`. -- **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math). A crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic). - **Rust only** (no handwritten JS/TS). **Never break wasm** (`cargo xtask wasm`). ## Quality gates @@ -85,3 +136,5 @@ Progress docs: `ROADMAP.md` (stage, headline numbers, progress log), `docs/targe - Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here. - Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time. - Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it. + +New workspace crates include `workspace = true` under `[lints]` in `Cargo.toml`. Production crate roots declare the crate-level lint attributes before modules; crate-level `//!` documentation may precede them. diff --git a/CLAUDE.md b/CLAUDE.md index c3d28358b1..43c994c2d3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,71 +1 @@ -# WordCraft — instructions for agents - -WordCraft is a clean-room, open-source, Rust-native word processor targeting Microsoft Word parity — and going further on speed, openness and agent control. It runs natively on macOS, Windows, Linux and BSD, and on the web via WASM. Siblings with the same conventions: `../photocraft` (Photoshop), `../vectorcraft` (Illustrator), `../filmcraft` (Premiere), `../lightcraft` (Lightroom), `../pdfcraft` (Acrobat), `../effectcraft` (After Effects), `../designcraft` (InDesign). - -## Start every session here -1. Read `plan/STATUS.md` (current milestone, next task), then the task in `plan/execution-plan.md` and the relevant `plan/architecture.md` section. `plan/` is gitignored (local only); if it's missing, start from `ROADMAP.md`, `docs/gaps.md` and `docs/roadmap.md` (Current focus). -2. Follow the autonomous operation protocol (`plan/execution-plan.md` §5). Don't stop to ask unless a decision is genuinely the owner's (licensing, publishing, pushing to new remotes, secrets). - -## Never crash -People trust WordCraft with their writing; a crash loses their work. **This outranks feature work**: never ship a feature through a panic path, and fix a crash before building on top of it. -- **No panics in non-test code:** no `unwrap()`, `expect()`, `panic!`, `unreachable!`, `todo!`, `unimplemented!`; no `unsafe` (`unsafe_code = "forbid"`). -- **Errors are `Result`** through the crate's error type and `?`. An unfinished feature returns an error or is disabled; it never panics. -- **Input-derived numbers are hostile** (files, commands, MCP/control params): `get()` instead of `[i]`, slice strings only at char boundaries, checked/saturating arithmetic, clamp sizes, cap allocations, bound recursion. -- **Last-resort guard:** `Session::run` turns an escaped panic into an error and restores the document. It's a safety net, not a licence. -- **Prove it:** every crash fix lands with a regression test. `crates/engine/src/tests.rs::hostile_params_never_panic` runs every command with junk params — keep it green. -- Production crate roots carry `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`; new crates start with it and add `[lints]` with `workspace = true` to their `Cargo.toml`. Tests may `unwrap` (`clippy.toml`). - -## Assets: absolutely no Microsoft (or Adobe/Avid/Autodesk) iconography or images -**This rule is non-negotiable.** Breaking it is the worst mistake you can make in this repo. -- Never copy, trace, screenshot-crop or recreate icons, images, templates, themes, clip art, fonts, cursors, sounds or any other asset from Microsoft Word/Office or any Microsoft, Adobe, Avid or Autodesk product. Not even "temporarily". -- Every asset must be **original** (drawn in code — prefer this: `crates/ui-egui/src/icons.rs`), **public domain / CC0**, **OSI open source**, or **Creative Commons that allows redistribution**, or licensed open source by a contributor who made it. -- **Every asset file has a row in [`ATTRIBUTION.md`](ATTRIBUTION.md)** (author, source, licence). `cargo xtask assets` (part of `cargo xtask ci`) fails otherwise. Add the row in the same commit as the file. -- Fonts: new font files go to [`storytold/craft-fonts`](https://github.com/storytold/craft-fonts), never this repo; it's an optional build input (`CRAFT_FONTS_DIR`). Spelling word lists and hyphenation patterns live in [`storytold/craft-i18n`](https://github.com/storytold/craft-i18n) `language-data/`, never this repo; they are embedded from `CRAFT_I18N_DIR` (pinned by `craft-i18n.rev`, which CI checks out). Without it the app builds, but English spelling falls back to an installed Hunspell English or the built-in Moby word list (with no American/British variant table) and pattern hyphenation is off (Bulgarian, Turkish and Russian still hyphenate by rule), so proofing tests need it. Documents asking for proprietary fonts (Calibri, Aptos, Cambria…) are rendered with installed system fonts or metric-compatible open substitutes (`crates/fonts/src/word.rs`) — never bundle proprietary fonts. -- Templates, sample documents and themes are our own text and colours (`crates/engine/src/sample.rs`, `cmd/design.rs`). Demo images are public domain, with sources recorded. - -## Clean room -- Microsoft Word is installed on the dev machine and may be **observed** black-box: run it, use its UI with synthetic documents, take screenshots **of its window only** (by window id — the machine runs other work; never capture the whole desktop) stored only under `plan/word/screenshots/` (gitignored, never committed). -- Never read, disassemble or copy anything inside the Word bundle (file names/listings only). Never commit files produced by Word. Never copy Microsoft wording beyond feature names. -- File formats come from public specs: ECMA-376 / ISO 29500 (OOXML), the published RTF spec, OASIS ODF 1.3. Never copy GPL/LGPL/AGPL code (LibreOffice, AbiWord, pandoc…). - -## Architecture (see `plan/architecture.md`) -| Layer | Crate | Job | -|---|---|---| -| L0 | `geom` | units, rects, measurement parsing | -| L1 | `doc`, `fonts` | document model + edit primitives; font DB, shaping, Word font substitution | -| L2 | `layout`, `docx`, `formats` | line breaking, pagination, tables, headers/footers, hit testing; OOXML I/O; TXT/MD/HTML/RTF/ODT | -| L3 | `render`, `pdf` | vello_cpu rasteriser; krilla PDF | -| L4 | `engine` | Session, undo, **command registry**, Word feature catalog, samples, I/O dispatch | -| L5 | `mcp` | MCP server (headless or bridged to the app) | -| L6 | `ui-egui` | Word-style egui front end (swappable) | -| apps | `apps/wordcraft`, `apps/wordcraft-cli`, `apps/wordcraft-web` | binaries | -| external | `craft-proof` (craft-libs, used as `wordcraft-proof`, sits at L1) | proofing: spelling, grammar, hyphenation; its word lists and patterns are craft-i18n's `language-data/` (`CRAFT_I18N_DIR`) | - -- **Everything is a command.** User-visible behaviour = a `CommandSpec` in `crates/engine/src/cmd/*.rs` (id, label, ribbon location, shortcut, params doc, `enabled`, `run`) + tests. UI-only commands (`ui.*`) live in `crates/ui-egui/src/lib.rs`. The ribbon, shortcuts, command search, CLI, control channel and MCP all dispatch by id. Programmatic calls never open dialogs. -- **Parity is measured:** `crates/engine/src/catalog.rs` lists Word's ribbon/menu features with command ids. `cargo xtask parity` writes `docs/parity-checklist.md`; a test enforces a floor that only rises. Implement missing ids to raise it. -- **Layering** is enforced by `cargo xtask layers`. Nothing below L6 depends on egui/eframe/winit/rfd. The UI is thin and reads `Session` state; colours come from `theme::Tokens`. -- **Rust only** (no handwritten JS/TS). **Never break wasm** (`cargo xtask wasm`). - -## Quality gates -Before every commit: `cargo xtask ci` (fmt, clippy -D warnings, tests, assets, layers, wasm). Commit after every arc of work that builds, with a task id in the message (`M2.1: line spacing dialog`). - -## Typing behaves like Word -Typing, Enter, Backspace, Tab, lists, AutoCorrect and AutoFormat follow Word's observed behaviour in [`docs/typing-parity.md`](docs/typing-parity.md), pinned by `crates/engine/src/tests/typing.rs` (keys go through the same commands as the keyboard). Change behaviour, doc and tests together; a typing fix lands with a test there. Compare headlessly (`ui_shot`); don't drive GUIs with keystrokes on the shared machine. - -## Running and looking at the app -- `cargo run --release -p wordcraft -- --sample --control 7981` (sample document + control channel). -- Drive it with JSON lines on `127.0.0.1:7981`, each with the window's key from `~/.config/wordcraft/control-key.local-7981` (macOS: `~/Library/Application Support/WordCraft/`), e.g. `{"id":1,"key":"…","method":"engine.execute","params":{"command":"text.insert","params":{"text":"Hello"}}}` then `{"id":2,"key":"…","method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`; docs: `docs/control-protocol.md` (Keys). -- **Headless window screenshots** (no focus stealing, works with a locked screen): `cargo run --release -p wordcraft-ui-egui --example ui_shot -- script.jsonl` (see the example's header). -- **For UI work, look at the result** (screenshot, read the PNG) and compare with Word side by side. -- CLI: `wordcraft-cli convert in.docx out.pdf`, `wordcraft-cli run --template sample --cmd 'select.text={"text":"Studio"}' --cmd format.bold --save out.docx`, `wordcraft-cli commands`, `wordcraft-cli parity`. -- MCP: `wordcraft-cli mcp` (headless) or `wordcraft-cli mcp --connect 127.0.0.1:7981` (drives the running app). See `docs/mcp.md`. -- Shell gotcha: `mv`/`cp` may be aliased interactive — use `/bin/mv -f` / `/bin/cp -f`. -- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; delete your target dir when done (disk). - -## Roadmap -Progress docs: `ROADMAP.md` (stage, headline numbers, progress log), `docs/target-app-parity.md` (the assessment), `docs/gaps.md` (ranked work list), `docs/roadmap.md` (milestones, Current focus), `docs/architecture.md`, and the parity checklists `docs/file-format-parity.md`, `docs/layout-parity.md`, `docs/typing-parity.md`, `docs/ui-parity.md`, `docs/hardware-parity.md`, `docs/localization-parity.md`. When work lands, update the gap, the parity doc and the progress log, and bump each touched doc's status line and revision history. - -## CI workflows -- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here. -- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time. -- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it. +@AGENTS.md diff --git a/Cargo.lock b/Cargo.lock index e46eb7356f..91c94ce265 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1090,9 +1090,10 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=a3e0b5a9440d18f19422a9dca13516747b7d3181#a3e0b5a9440d18f19422a9dca13516747b7d3181" dependencies = [ "craft-winprint-gdi", + "log", "miniz_oxide 0.9.1", "roxmltree", "tempfile", @@ -1157,10 +1158,21 @@ dependencies = [ "zip 9.1.0", ] +[[package]] +name = "craft-web-api" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fd5e2feff6ea25bed371a99cc732b3968d621505#fd5e2feff6ea25bed371a99cc732b3968d621505" +dependencies = [ + "js-sys", + "serde_json", + "wasm-bindgen", + "wasm-bindgen-futures", +] + [[package]] name = "craft-web-host" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=2e2312ddaf21bc3d85cd3395d0955c7d239306f6#2e2312ddaf21bc3d85cd3395d0955c7d239306f6" +source = "git+https://github.com/storytold/craft-libs?rev=fd5e2feff6ea25bed371a99cc732b3968d621505#fd5e2feff6ea25bed371a99cc732b3968d621505" dependencies = [ "wasm-bindgen", "web-sys", @@ -6814,7 +6826,9 @@ dependencies = [ "open", "pollster 1.0.1", "rfd", + "serde", "serde_json", + "tempfile", "windows", "winit", "winresource", @@ -7095,6 +7109,7 @@ dependencies = [ name = "wordcraft-web" version = "0.15.1" dependencies = [ + "craft-web-api", "craft-web-host", "eframe", "egui", diff --git a/ROADMAP.md b/ROADMAP.md index d475139fff..39386c2d02 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,10 +1,20 @@ # WordCraft roadmap +> **Last reviewed:** 2026-10-31 · **Last updated:** 2026-10-31 · **Change:** minor (#3398: persistent screen paper colors; parity estimates unchanged); previously minor (#3693: additional Delete Previous Sentence command and shortcut; Word-parity estimates unchanged) +> **CLI path update:** 2026-11-04 · Options before a non-Unicode input filename no longer make the CLI reject that filename; text option values still require Unicode; `run --file` and `run --script` retain native paths. No ranked gap or readiness estimate changes. +> **Floating anchor row segments:** 2026-11-07 · A floating anchor after a manual page/column break counts visible content on the far side of the same wrapped row. Marker-only cover anchors retain their previous-page rule. Synthetic picture spacing, placement and history controls; no new native Word measurement. Readiness estimates unchanged. + > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-05 · **Change:** trivial (progress log: the status bar's count can be words, characters or characters without spaces, #2797); previously trivial (Linux display server setting in File › Options › Advanced, #2184); previously trivial (progress log: Linux Read Aloud through Speech Dispatcher, #4576); previously trivial (progress log: a click just outside a floating table by turned cell text, #4181; previously minor: progress log: Spanish proofing, #3360, #3553; previously trivial: progress log: Insert › Table's grid previews the hovered table on the page, #3931; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial (progress log: MHT quoted MIME parameters); previously trivial (progress log: CLI convert format options); previously trivial (MATH construction reuse, #3630); previously trivial (DOCX `w:adjustRightInd` is preserved through style resolution and ordered DOCX saves); previously trivial (per-page ink object lookups, #3614); previously trivial (progress log: ODT display settings removed); previously trivial (progress log: dialogs are modal except the five Word keeps modeless, and a press beside one shakes it, #3859); previously trivial (progress log: picture brightness, contrast and transparency, ODT element audit, ODT checked against 220 files from other applications, ODT display styles); previously trivial (progress log: the dialogs of the mirrored Arabic interface read right to left: label-and-field rows, tables (list columns, the Models table), tile galleries and the Add/Remove arrows follow the reading direction, through one `rtl::Table` helper that draws exactly as before in English; no ranked gap closed, readiness estimates unchanged); previously trivial (progress log: fonts that aren't installed are flagged in the font box and status bar); previously trivial (progress log: ODT sections inside text boxes); previously trivial (progress log: ODT definitions sharing a name, sections around tables, stacking order); previously trivial (progress log: ODT forms and indexes written as read); previously trivial (progress log: ODT table styles saved, WordCraft-origin documents checked in LibreOffice); previously trivial (progress log: ODT interpretation profile); previously trivial (progress log: ODT controls, custom shapes, in-line placement); previously trivial (progress log: ODT charts kept, frame outlines, header height); previously trivial (progress log: ODT header rows); previously trivial (progress log: ODT layout setting, padding, columns); previously trivial (progress log: ODT tables, header spacing, picture paragraphs); previously trivial (progress log: ODT round trips keep their page count); previously trivial (progress log: emphasis marks, SET fields, ODT page backgrounds and tables); previously trivial (progress log: ODT checked against LibreOffice's templates); previously trivial (progress log: ODT at ~90%); previously trivial (progress log: ODT read and written nearly whole); previously trivial (progress log: ODT text boxes); previously trivial (progress log: ODT layout settings); previously trivial (progress log: ODT character styles); previously trivial (progress log: ODT custom styles); previously trivial (progress log: ODT equations saved); previously trivial (progress log: ODT floating pictures); previously trivial (progress log: ODT paragraph borders); previously trivial (progress log: ODT heading numbering); previously trivial (progress log: ODT table borders); previously trivial (progress log: ODT heading styles); previously trivial (progress log: ODT tab stops); previously trivial (progress log: ODT character formatting); previously trivial (progress log: ODT lists); previously trivial (progress log: ODT comments); previously trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: Options › Language redesign, CP4.3); previously trivial (progress log: the missing-pack bar on a fresh install, the caret's typing language, and the spelling menu heading in the interface language, CP4.3); previously trivial (progress log: Language pack languages in every language list, CP4.3); previously trivial (progress log: craft-proof pin, French elision and Valencian, CP4.3); previously trivial (progress log: a long comment's card and balloon stop at ten lines, #3382); previously trivial (progress log: a pasted text box story is charged for the formatting it copies); previously trivial (progress log: a .doc table row keeps one override per cell); previously trivial (progress log: a .doc file's sections find their paragraphs by index); previously minor (Arabic interface language with mirrored UI, #1195); previously trivial (progress log: Replace and Remove Table of Contents with Word's title above a section break, #4041); previously trivial (progress log: a password-protected file named on the command line asks for its password after the startup document is replaced, #5299); previously trivial (progress log: language-packs-2 published and pinned, 75 Language packs); previously trivial (progress log: language-packs-1 published and pinned, CP4.4); previously trivial (progress log: Language packs, CP4);; previously trivial (DOCX table rows and cells inside selected alternate-content branches survive open and repeated saves (#5219); broader corpus qualification and readiness estimates unchanged); previously trivial (progress log: a list inside a Markdown block quote stays quoted); previously trivial (progress log: arrowheads keep their kind, width and length and are drawn at Word's sizes); previously trivial (progress log: the line between columns for selected text, #5178); previously trivial (progress log: .doc standard font, #5066); previously trivial (progress log: a table row doesn't split off a picture that doesn't fit, #4272); previously trivial (progress log: a middle or bottom cell of a row split across pages lines up on the first page); previously trivial (progress log: tab leader dots spaced and placed as in Word); previously trivial (progress log: a list level above counts its first number at the first deeper item); previously trivial (progress log: paragraph side borders lie outside the text, #5036); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-08 · **Change:** trivial (progress log: a turned inline picture whose file keeps no room for the turn keeps its line to its frame); previously trivial (progress log: the Arabic interface's Styles gallery reads from the right (#5566)); previously trivial (progress log: ODT pictures offered in several formats); previously trivial (progress log: ODT pictures offered in several formats); previously trivial (progress log: ODT pictures offered in several formats); previously trivial (progress log: ODT heading text of another size); previously trivial (Protected typing boundary lookup counts preceding objects once and indexes editable permission IDs (#3614); caret movement and enforcement policy are unchanged; previously trivial: progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial (progress log: a shape dropped just after a page break inside a paragraph stays on the new page, #4262); previously trivial (progress log: a float anchored on a later page of a long paragraph is drawn there, #4250); previously minor (progress log: `file.closeDocument` over the control channel and MCP bridge is refused while the document it names has unsaved changes; previously trivial (progress log: ODT element audit, ODT checked against 220 files from other applications, ODT display styles); previously trivial (progress log: Shape Format › Size in the interface unit, #4196); previously trivial (progress log: typed n-ary operators such as ∫ and ∑ wait for their limits, observed in Word for Windows on #4938 (#4917)); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: justified and distributed lines keep the width of the spaces before their first character); previously trivial (print helpers erase loaded plaintext inputs before driver work, #5710); previously trivial (Windows print submissions use isolated helpers so a cancelled driver cannot block a retry, #5710); previously trivial (print cancellation remains available without a document; sending-boundary limitation clarified, #5710); previously trivial (progress log: a print job waiting on its driver can be cancelled, and quitting doesn't wait for it, #5710); previously trivial (progress log: the Print panel's printer list no longer waits on printer drivers, #5710); previously trivial (Page drawing indexes text-box warp lookups per paragraph (#3614)); previously trivial (built-in citation source lookups, #4888; current-main progress records retained); previously trivial (progress log: Times and Courier take their Windows counterparts' line spacing; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial (progress log: the status bar's count can be words, characters or characters without spaces, #2797); previously trivial (Linux display server setting in File › Options › Advanced, #2184); previously trivial (progress log: Linux Read Aloud through Speech Dispatcher, #4576); previously trivial (progress log: a click just outside a floating table by turned cell text, #4181; previously minor: progress log: Spanish proofing, #3360, #3553; previously trivial: progress log: Insert › Table's grid previews the hovered table on the page, #3931; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial (progress log: MHT quoted MIME parameters); previously trivial (progress log: CLI convert format options); previously trivial (MATH construction reuse, #3630); previously trivial (DOCX `w:adjustRightInd` is preserved through style resolution and ordered DOCX saves); previously trivial (per-page ink object lookups, #3614); previously trivial (progress log: ODT display settings removed); previously trivial (progress log: dialogs are modal except the five Word keeps modeless, and a press beside one shakes it, #3859); previously trivial (progress log: picture brightness, contrast and transparency, ODT element audit, ODT checked against 220 files from other applications, ODT display styles); previously trivial (progress log: the dialogs of the mirrored Arabic interface read right to left: label-and-field rows, tables (list columns, the Models table), tile galleries and the Add/Remove arrows follow the reading direction, through one `rtl::Table` helper that draws exactly as before in English; no ranked gap closed, readiness estimates unchanged); previously trivial (progress log: fonts that aren't installed are flagged in the font box and status bar); previously trivial (progress log: ODT sections inside text boxes); previously trivial (progress log: ODT definitions sharing a name, sections around tables, stacking order); previously trivial (progress log: ODT forms and indexes written as read); previously trivial (progress log: ODT table styles saved, WordCraft-origin documents checked in LibreOffice); previously trivial (progress log: ODT interpretation profile); previously trivial (progress log: ODT controls, custom shapes, in-line placement); previously trivial (progress log: ODT charts kept, frame outlines, header height); previously trivial (progress log: ODT header rows); previously trivial (progress log: ODT layout setting, padding, columns); previously trivial (progress log: ODT tables, header spacing, picture paragraphs); previously trivial (progress log: ODT round trips keep their page count); previously trivial (progress log: emphasis marks, SET fields, ODT page backgrounds and tables); previously trivial (progress log: ODT checked against LibreOffice's templates); previously trivial (progress log: ODT at ~90%); previously trivial (progress log: ODT read and written nearly whole); previously trivial (progress log: ODT text boxes); previously trivial (progress log: ODT layout settings); previously trivial (progress log: ODT character styles); previously trivial (progress log: ODT custom styles); previously trivial (progress log: ODT equations saved); previously trivial (progress log: ODT floating pictures); previously trivial (progress log: ODT paragraph borders); previously trivial (progress log: ODT heading numbering); previously trivial (progress log: ODT table borders); previously trivial (progress log: ODT heading styles); previously trivial (progress log: ODT tab stops); previously trivial (progress log: ODT character formatting); previously trivial (progress log: ODT lists); previously trivial (progress log: ODT comments); previously trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: Options › Language redesign, CP4.3); previously trivial (progress log: the missing-pack bar on a fresh install, the caret's typing language, and the spelling menu heading in the interface language, CP4.3); previously trivial (progress log: Language pack languages in every language list, CP4.3); previously trivial (progress log: craft-proof pin, French elision and Valencian, CP4.3); previously trivial (progress log: a long comment's card and balloon stop at ten lines, #3382); previously trivial (progress log: a pasted text box story is charged for the formatting it copies); previously trivial (progress log: a .doc table row keeps one override per cell); previously trivial (progress log: a .doc file's sections find their paragraphs by index); previously minor (Arabic interface language with mirrored UI, #1195); previously trivial (progress log: Replace and Remove Table of Contents with Word's title above a section break, #4041); previously trivial (progress log: a password-protected file named on the command line asks for its password after the startup document is replaced, #5299); previously trivial (progress log: language-packs-2 published and pinned, 75 Language packs); previously trivial (progress log: language-packs-1 published and pinned, CP4.4); previously trivial (progress log: Language packs, CP4);; previously trivial (DOCX table rows and cells inside selected alternate-content branches survive open and repeated saves (#5219); broader corpus qualification and readiness estimates unchanged); previously trivial (progress log: a list inside a Markdown block quote stays quoted); previously trivial (progress log: arrowheads keep their kind, width and length and are drawn at Word's sizes); previously trivial (progress log: the line between columns for selected text, #5178); previously trivial (progress log: .doc standard font, #5066); previously trivial (progress log: a table row doesn't split off a picture that doesn't fit, #4272); previously trivial (progress log: a middle or bottom cell of a row split across pages lines up on the first page); previously trivial (progress log: tab leader dots spaced and placed as in Word); previously trivial (progress log: a list level above counts its first number at the first deeper item); previously trivial (progress log: paragraph side borders lie outside the text, #5036); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> +> **Recipient CSV update:** 2026-10-27 · Complete quoted header records choose the delimiter (#4249); detection and regressions follow the current mailings module split. Recipient limits and readiness estimates are unchanged. +> +> **Headered CSV prefix update:** 2026-10-31 · Unquoted whitespace-only prefix lines are skipped before choosing recipient headers (#4249); quoted whitespace headers, headerless records, the shared CSV reader and recipient limits remain unchanged. + +> **Page Setup update:** 2026-11-07 · Page Setup number boxes keep long input inside the field, including accepted margin values after focus leaves (#5650); accepted ranges and readiness estimates are unchanged. > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. > **Legacy form update:** 2026-10-22 · trivial: numeric XML references decode in field names and labels; readiness estimates unchanged. @@ -140,9 +150,44 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm ## Progress log +- 2026-11-08: Layout: a turned inline picture whose file keeps no room for the turn (no or a zero `wp:effectExtent`, as other tools write) keeps its line to its frame and overhangs it, cut at the line's top and bottom, as Word does: the frame as it is in Word 2007 layout, with width and height exchanged from 45° to 135° and 225° to 315° in Word 2013+ layout. Such a file saves the same way. Pictures with the room recorded (Word's own files) still take their rotated bounds. Readiness unchanged. +- 2026-11-08: XML text and CDATA read with their line ends normalised (XML 1.0 §2.11): CR LF and a lone CR are one line feed, ` ` stays a CR, so a line break inside `w:t` in a file saved with Windows line ends is one space, not two (#5897); readiness estimates unchanged. +- 2026-11-08: Layout: contextual spacing drops the space after a table cell's last paragraph when that paragraph is in the Normal style, the end-of-cell mark counting as a Normal paragraph next to it; a last paragraph in another style keeps its space after. The rule is inferred from docxide-pdf's notes and its `cell-end-*` fixtures (`cell-end-text-context`, `cell-end-empty-context`); a Word comparison that tells it apart from the neighbouring paragraph's style is still open. Readiness unchanged. +- 2026-11-06: Arabic interface: the Home tab's Styles gallery reads from the right, as the rest of the mirrored ribbon does (#5566). Normal is the rightmost tile, the next styles follow leftwards and the scroll arrows and More sit at the gallery's left end; left to right is unchanged. Readiness unchanged. +- 2026-11-05: ODT heading text of another size keeps it (gap 19): a smaller word in a heading came back at the heading's size in LibreOffice after a save. +- 2026-11-05: ODT table rows kept from breaking across pages stay so (gap 19): they came back from a save free to break. +- 2026-11-05: ODT: a watermark added, changed or removed, and Different Odd & Even Pages turned on or off, in a file read from ODT are saved. +- 2026-11-05: ODT font declarations are kept (gap 19): replacing them made LibreOffice pick other substitutes for fonts not installed, reflowing a document from 83 pages to 70 on a save with no edits. +- 2026-11-05: ODT pictures offered in several formats show and survive a save (gap 19): LibreOffice's letter templates lost theirs. +- 2026-11-07: An in-line picture lowered past its own height (`w:position`) lowers its line as far, as in Word; all 156 synthetic lines measured for #3984 match Word (#5796). Exact spacing keeps the line's baseline. +- 2026-11-03: Search types in place: a click on the title bar's Search box, or Alt+Q, takes the typing in that box, with the results under it, as in Word, instead of a second box in a dialog (#5398). +- 2026-10-31: List labels: a numbering level with format `none` and literal text, such as `RESPONSE:` with a one-inch hanging indent, shows that text as its label, as Word does; it used to show nothing. As in Word, a `none` level still counts and restarts the levels below it, its own number is empty (in legal numbering too), and an empty label still tabs the text to the indent (#4985). Readiness unchanged. +- 2026-10-31: Font menus draw their entries from a few shared textures written in place, as the Equation tab's pictures are (#1002), instead of a texture per font: on Windows (DirectX 12) browsing the list showed the names on black strips in light mode and white or red ones in dark mode (#5105). Readiness unchanged. +- 2026-11-02: A picture linked from its context menu shows its address on hover and opens it with Ctrl/⌘+click, in line or floating, as linked text does; DOCX files keep the link where Word does, on the drawing (`a:hlinkClick`), so links on pictures from Word open too (#5267). Readiness unchanged. +- 2026-11-01: DOCX tables keep rows and cells wrapped in `mc:AlternateContent`, selecting one supported Choice or Fallback with the existing reader policy (#5219). Original synthetic checks cover namespace aliases, unsupported requirements, nested row/cell content controls and two save/reopen cycles; broader corpus and native Word qualification remain open. + +- 2026-11-01: Shapes: a line's arrowheads are as long and as wide as Word draws them: 2, 3 or 5 times the line's width for small, medium and large (a line under 2 points as a 2-point one). Their kind, width and length are read from DOCX (DrawingML and VML) and saved back; before, every arrowhead was one size and saved as a medium triangle. +- 2026-11-07: DOCX: a VML text box keeps its text direction: `mso-layout-flow-alt:bottom-to-top` reads as Rotate all text 270° whatever `layout-flow` says, and otherwise `layout-flow:vertical` (or `vertical-ideographic`) as 90°, so the turned text box in a compatibility-mode document's header is drawn turned, as in Word. (#5193) +- 2026-11-06: Change Picture keeps the picture's width and takes the new picture's aspect ratio, dropping the old crop, instead of stretching the new picture into the old frame (a revised figure came out about 2% taller in a real manuscript) (#5761). Readiness estimates unchanged. +- 2026-11-06: Superscript and subscript text is set at 0.65 of its size rounded to the half point, as Word does (8 pt → 5, 11 → 7, 12 → 8, 24 → 15.5; measured in Word for Mac from digit advances with `cargo xtask word-compare`), not 0.65 exactly: lines with citation numbers and footnote references break where Word's do (the Arial superscript and author-line probes now match Word line for line). Readiness estimates unchanged. +- 2026-11-06: Line numbers take the document's "Line Number" style over its defaults, as in Word (Aptos 12 pt black in a manuscript whose defaults are Aptos 12 pt, measured in Word for Mac), instead of a fixed 10 pt grey. Readiness estimates unchanged. +- 2026-11-07: Newly opened secondary windows constrain their initial viewport dimensions to the monitor's usable area with margins for system panels, preventing new windows from exceeding screen width on high-DPI scaled displays (#5871). +- 2026-11-07: A floating anchor after a page break stays with text on the far side of the same wrapped row (Leaf #107); marker-only cover anchors retain their rule. Picture spacing, drop position, Undo/Redo and DOCX regressions. +- 2026-11-06: Windows print submissions run in separate helper processes (#5710). A helper acknowledges cancellation before it stops, so a driver stalled in a cancelled job cannot hold another job’s provider lock. A private temporary PDF and bounded settings messages cross the process boundary; normal shutdown reaps helpers and removes their inputs. The helper erases its plaintext inputs before driver work; a hard app crash during startup can still leave private temporary files. Jobs already sending still complete. Readiness estimates unchanged. +- 2026-11-06: File › Print: before sending starts, Cancel printing stops a waiting job, frees the Print page and keeps the settings; a successfully cancelled job is never sent. Cancellation remains available after the last document closes. Quitting cancels waiting jobs and joins jobs already sending (#5710). On Windows, winprint repeats the DEVMODE conversion after sending starts. A stall during that repeat prevents cancellation and can keep shutdown waiting, even before a native job exists. Readiness estimates unchanged. +- 2026-11-06: File › Print on Windows no longer waits on printer drivers to list printers (#5710). The list used to wait until each printer's driver said whether it prints on both sides, and a networked printer asleep in power save could keep it empty with Refresh unavailable; now discovery asks no driver (the Windows spooler still supplies the list), and printing still checks the job's options with the driver. The reporter's sleeping printer has not been retested yet. Readiness estimates unchanged. - 2026-11-04: CLI `convert` rejects a missing or repeated `--format` value before opening the input or writing output, matching the existing page/scale option checks. Readiness estimates unchanged. - 2026-11-04: MHT import keeps semicolons and escaped quotes inside quoted MIME parameters, so an unrelated parameter cannot supply a false multipart boundary and hide the document text. Readiness estimates unchanged. +- 2026-10-30: Equations: `\int`, `\sum` and the other n-ary operators typed with AutoCorrect wait for their limits. `\int` + Space, then `_0^1` and Space, gives ∫ with its limits and the caret in its operand, and `\int_0^1` typed in one go does the same, since any character that can't continue `\name` corrects it; this is observed in Word for Windows on #4938. Enter or an arrow key builds it without going into the operand, and `∫∫` puts the operand in the inner operator; those two are not observed in Word (#4917). Readiness unchanged. + +- 2026-11-04: RTF import skips the declared Unicode fallback count even when it exceeds eight fallback units, so longer ANSI alternatives do not leak into the document text. Skipping stays bounded by the available input and ends at a group boundary. Readiness estimates unchanged. +- 2026-11-05: Linux: a WordCraft started by an AppImage app no longer takes that app's file for its own (#5653). The AppImage runtime's `APPIMAGE`, `APPDIR`, `ARGV0` and `OWD` pass to whatever the app starts, so an installed WordCraft opened through `xdg-open` saw another program's `APPIMAGE`, and its self-update replaced that program's file. The app now starts over without those variables unless its executable is under `APPDIR`, so the update goes through the package or plain file it really is. Should starting over fail, the self-update is off in that window and Help › Check for Updates says why. Readiness estimates unchanged. +- 2026-11-05: A Word template (`.dotx`, `.dotm`, `.dot`) opened from the file manager, the command line or the Finder starts a new, untitled document based on it, as Word's default verb for a template does (#3433). It opened the template itself, so Save (and AutoSave) wrote over it. The new document is attached to the template and Save asks where to write; a protected template asks for its password and still starts a new document. File › Open and a file dropped on the window still open a template itself, to edit it. +- 2026-11-05: Arabic letters join across a change of formatting (gap 16, #4093). Each run was shaped on its own, so a letter at a run's edge took the form of a word's start or end: a coloured letter stood apart, and a word edited with Track Changes on read with a space in it (ال ذكاء). Each run is now shaped knowing the text shown on either side, past hidden or deleted text left out of the view, as for Syriac, Mongolian and the other joining scripts. Readiness estimates unchanged. +- 2026-11-05: Shapes, text boxes, groups and drawing canvases in Strict Open XML documents open (#3548). Strict puts them in its one drawing namespace, where Transitional documents use Word 2010's `wps`, `wpg` and `wpc`, and Word writes a Strict text box's text in `wne:txbxContent`; the reader knew neither, so a Strict file opened with its text and no shapes at all. They now read like their Transitional twins and are saved as Transitional. Charts and SmartArt in Strict namespaces are not mapped yet. - 2026-10-28: Ink replay, eraser picking, lasso selection and arrow-candidate discovery reuse the paragraph prefixes they have already scanned (#3614). Page item order, stroke geometry and saved data retain their behavior. This narrows the stability work; ink import fidelity, native pen input and broader corpus qualification remain open. +- 2026-10-29: Page drawing finds text-box warps without rescanning the paragraph for every box (#3614). Only requested offsets are indexed, and each paragraph walk stops after its last requested character. Original Linux discovery, complete display-output and CLI/MCP save/reopen controls; native Word comparison untested. Readiness estimates unchanged. +- 2026-10-28: Protected typing boundary lookup counts preceding objects once and indexes editable permission IDs (#3614); caret movement and enforcement policy are unchanged. Existing mutation and protection checks retain their costs; this does not close the broader performance gap. - 2026-11-04: OpenDocument files have no display settings any more (gap 19): File › Options › Advanced › OpenDocument files, `file.options` `odf`, `WORDCRAFT_ODF`, the LibreOffice style and custom values are gone. WordCraft makes one choice for each case the standard leaves open, the one the default made, and still follows what a file states ([docs/odf-interpretation.md](docs/odf-interpretation.md)). - 2026-10-31: Dialogs are modal, as in Word (#3859): while one is open, the ribbon, File, the page and the panes of its document window take no clicks, and typing and shortcuts (Ctrl+O…) do nothing until it closes; WordCraft's own Open/Save As window is modal too. A press beside a modal dialog shakes it a little, so it is clear why nothing behind it reacts (Word beeps); with animation effects off, its border flashes instead. Find and Replace, Symbol, Mark Index Entry, Cross-reference and Insert Merge Field stay modeless, as in Word for Mac 16.114.1 (checked on a newer build than the 16.113.4 the parity target names). @@ -189,13 +234,34 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm - 2026-10-31: ODT: paragraph spacing, indents and line spacing are read and written, and an ODT's own default spacing (none, single lines) replaces Word's 8 pt after and 1.15 lines, so LibreOffice documents keep their vertical rhythm on open and save (gap 19). Readiness unchanged. - 2026-11-01: PDF export, printing and page images show the body's SEQ, STYLEREF and REF fields with their current results, as Word fills them in when it prints or exports even with Update fields before printing off: a caption numbered 7 when it is the first shows 1 on paper, and a REF to a bookmark the document lacks prints Word's "Error! Reference source not found.". NUMWORDS, FILENAME, the document properties, formulas and locked fields keep their stored results there, as in Word; the editor and the saved document keep every stored result. - 2026-10-28: Print Markup HTML, Markdown and text copies discover comment anchors in one paragraph walk, so unrelated bookmark markers no longer cause repeated prefix scans (#3614). Existing quotes, replies, order and export limits are unchanged; original synthetic semantic and scaling regressions cover the traversal. +- 2026-11-04: Table Sort refuses malformed and nonexistent explicit columns before reordering rows (#5531). Omitted column 0, valid columns, header/order/language behavior and Undo/Redo are retained. Original Linux CLI/MCP controls and repeated native/DOCX reopens cover the command; native Word and desktop comparison remain untested. Readiness estimates unchanged. - 2026-10-30: Layout resource caps (#3867, #4385) and linear-time passes (#3866, #4392): repeated table-header rows are capped by item counts, a clipped group counting every nested member, the wrap cache by decoded pixels and the widths a linked text box chain lays out by their lines and items; PDF export, hit testing, tracked-change and clipped-table costs, and long endnote selections no longer grow quadratically. Synthetic regression tests only; no new Word comparison. - 2026-11-03: Proofing (CP4.3): craft-proof moves to craft-libs main with #589 and #590. French spelling suggestions offer the elided form first (`Jadore` → `J'adore`, before `J adore`), and Valencian text (`ca-ES-valencia`) is proofed with the `ca-valencia` pack instead of plain Catalan. Readiness unchanged. +- 2026-11-03: Security: PDF export leaves the document's author out of the PDF's properties when "Remove personal information from file properties on save" is on, as DOCX saving already does. Readiness unchanged. - 2026-11-03: A long comment no longer grows its card or balloon without limit (#3382). Its field in the Comments pane and in a selected balloon stops at ten lines and scrolls, keeping the caret in view; a balloon that isn't selected shows the first ten lines of the comment and of each reply, and clicking it opens the scrolling field. The buttons and the next comment stay in view. - 2026-11-03: Arabic dialogs: the dialogs of the mirrored interface now read right to left. Rows of a label and its field, lists of columns (Compare, AutoCorrect, Special Characters, Track Changes Options, the Columns widths), the tile galleries (chart types, SmartArt layouts) and the ribbon Paragraph group start at the right edge, through an `rtl::Table` helper that is an `egui::Grid` in English; the Add >> and << Remove arrows swap, the Columns preset captions and the Macros buttons no longer overlap, and the Recipient List grid and About › Models scroll instead of running past the left edge. Readiness unchanged. +- 2026-11-03: Shape Format › Size: the width and height fields show the interface unit (centimetres, inches…) instead of always points, and take a size typed in any unit, such as `3 cm` or `300 px`, applied once typed (Esc cancels), each side on its own, as Picture Format › Size does (#4196). In both, a field clicked and left with Enter or Tab without typing no longer resizes the object by the rounding of the size it shows (a 100 pt shape became 100.08 pt in inches). +- 2026-11-03: Security: Paste Special with picture files on the clipboard refuses anything that isn't a regular file (a pipe or device reports no length and can produce endless data), and `cargo xtask stats` no longer follows directory symlinks. Readiness unchanged. +- 2026-11-03: Size keeps the side it isn't asked to change (#5469): typing only the width of a straight line keeps its height of 0 instead of making it 1 pt; the 1–4000 pt limits apply only to the sides a `picture.size` call changes. Readiness unchanged. +- 2026-11-03: Find and Replace: a keyboard apostrophe also finds typographic single quotes, so smart-quoted typing stays searchable (#5388). Regex, wildcards, literal searches and numeric character codes remain exact. Original UTF-8 match ranges and replacement spelling are preserved; readiness unchanged. +- 2026-11-03: Security: XLSX import finds each sheet's worksheet part through an index of the workbook's relationships, built once, instead of searching every relationship for every sheet. Readiness unchanged. +- 2026-11-03: Layout: a row of a nested table marked Can't Split isn't split with its outer row any more. The outer row moves to the next page whole when that row comes first in it, as Word does (docxide-pdf `nested-first-row-*-cant1`), and otherwise splits above it (inferred, not yet checked in Word); on a fresh page a nested row taller than the room still splits rather than running into the margin. Readiness unchanged. +- 2026-11-03: Layout: no line break between a full stop and an opening parenthesis. UAX #14 allows one there, Word doesn't: `29.05.2023г.(оформление ЗУ)` and `2023.(Example detail)` now wrap whole at the space before, as in the docxide-pdf `dot-open-token*` Word references. Readiness unchanged. +- 2026-11-03: Layout: before Word 2013 layout a floating table that may not overlap another and is moved below it keeps its own left edge, as Word places it, also when its first cell has no left margin; the layout took a zero margin for Word 2013 layout and stepped the table in by its distance from the text. Readiness unchanged. +- 2026-11-03: Layout: an empty paragraph after a document's leading floating table takes no space before, as in Word's PDFs of docxide-pdf's `leading-empty-before-float` cases (beside the table); below a full-width one it loses it too, which is untested. Readiness unchanged. - 2026-11-03: Security: pasting text boxes charges each copied story for its formatting too (run, mark, paragraph and section properties), not only its text and objects, so a story with huge style or font names can't copy past the paste's 32 MiB text box budget. Readiness unchanged. - 2026-11-03: Security: `.doc` import keeps one width, shading, alignment and merge override per table cell, the last one winning as before, instead of a copy every time a paragraph's properties repeat the sprm, so a shared PAPX can't pile up copies on every paragraph mark. Readiness unchanged. - 2026-11-03: Security: `.doc` import attaches each section to the paragraph that ends it through an index of paragraph ends, instead of scanning every paragraph for every section, so a file with a thousand sections that end nowhere can't multiply the work over its paragraphs. Readiness unchanged. +- 2026-11-03: Layout: a line can break after a hyphen that a slash and a letter follow ("Dataprotection-" / "/Secrecyviolation"), as Word does ("fitness-/1589", with a digit after the slash, stays whole), where UAX #14 forbids a break before the slash; an auto-fit table column holding such a compound wraps it instead of widening and taking the width from its neighbours. (#3408) +- 2026-11-03: Layout: a section's closing paragraph whose mark is hidden and that holds only an in-line picture (or a note reference) is no longer laid out as an empty hidden mark. The picture keeps its room, so the next continuous section's text starts below it instead of being drawn over it. A paragraph with only floating objects and a hidden mark still takes no room. Not observed in Word. (#5288) +- 2026-11-03: Security: DOCX import looks relationships up by id through an index built once per part, the first relationship with an id still winning, instead of scanning every relationship for each hyperlink, picture or embedded part that names one. Readiness unchanged. +- 2026-11-03: Security: DOCX import no longer carries a file's part named like WordCraft's own bookkeeping (`wordcraft:unmodelled.roots` and the like), which a save read back as that bookkeeping, adding relationships the file chose. Readiness unchanged. +- 2026-11-03: Security: LaTeX import parses at most 63 cells of a `tabular` row (what a table row keeps), so a very wide row can no longer widen the multirow tracking that every later row walks. Readiness unchanged. +- 2026-11-03: Security: XLSX and ODS import (mail merge recipient lists) keep each cell's character count as text is added, instead of counting the whole cell again for every piece of text, so a full cell followed by many small text events no longer costs its length each time. Readiness unchanged. +- 2026-11-03: Security: exported hyperlinks (ODT, LaTeX, HTML, Markdown, RTF) holding a control character or noncharacter that the writer drops are written as plain text, so `file\u{85}:///…` can't pass the link check and come out as a `file:` link. Readiness unchanged. +- 2026-11-03: Security: pictures are measured, and checked for transparency for tight text wrapping, from a PNG's chunk headers, instead of through the decoder, whose header read inflates an embedded ICC profile of up to 512 MiB for every picture it only measures. Readiness unchanged. +- 2026-11-03: Security: saving an embedded object (a native document's kept markup included) collects the relationship ids its markup uses in a set and checks them against the writable ones in one pass, instead of scanning every id collected so far for each attribute. Readiness unchanged. +- 2026-11-03: Security: the renderer leaves out a drawing path reaching more than a million device pixels (64 times the largest canvas) or of no finite size, which the rasteriser would otherwise flatten into points by the billion before clipping it. Readiness unchanged. - 2026-11-03: Language packs: language-packs-2 is published and WordCraft pins its index: 75 languages, adding Danish, German (Germany, Austria, Switzerland), Esperanto, Estonian, Persian, Croatian, Hungarian, Norwegian (Bokmål, Nynorsk), Dutch (#4145, #2505) and Swedish (Sweden, Finland), which need craft-proof's flag-based compounding (storytold/craft-libs#497, pinned). English (Australia) is checked against SCOWL's en-AU list as well as en-GB's (storytold/craft-libs#496). Mongolian and Thai stay hidden. Readiness unchanged. - 2026-11-03: Language lists (CP4.3): a language installed from a Language pack (Bulgarian, say) can now be chosen. Set Proofing Language lists every built-in language and every installed or available pack's, each once and sorted by name; Review › Language and the status bar's menu add the installed packs' languages to the short list, with one way of working out the check mark, by tag. A pack's language is stored as a tag that finds the pack again (`sr-Latn-RS` is proofed by the Latin `sr-latn` pack, not the Cyrillic one; Valencian is Word's `ca-ES-valencia`). Every pack in language-packs-2 now has an English name (Belarusian used to show its own name, Беларуская). Readiness unchanged. - 2026-11-03: Language packs (CP4.3): on a fresh install (or after an upgrade, with the list cached for an older pin) the missing-pack bar still offers a document language that has no dictionary, "Turkish proofing isn't installed. Install": the person's click turns on downloading, fetches the list and installs the pack if it lists one (if not, the bar goes). With downloading already on, a missing or stale list is fetched once in the background. Choosing a language with only the caret counts before anything is typed, and the spelling context menu's heading (Possible spelling mistake) is in the interface language, not the text's. Closing the bar is remembered per document, per pack and, before the list is known, per language tag. Readiness unchanged. @@ -203,6 +269,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm - 2026-11-02: Language packs (CP4.4): craft-i18n's first pack release, language-packs-1, is published and WordCraft pins its index (sha256): 61 languages install from File › Options › Language, among them Lithuanian (#4707), Romanian (#3438), Catalan (#2895, #4947), French (#1325), Greek (#2561), Slovak (#3806), Serbian (#4968) and Latvian (#4438). Dutch (#4145, #2505), German and the other compounding languages wait on craft-proof engine support. Readiness unchanged. - 2026-11-01: Language packs (CP4.3–CP4.5): languages beyond English install their spelling dictionary and hyphenation as a pack from File › Options › Language, or from a bar a document in that language shows ("Polish proofing isn't installed. Install (4.6 MB)"), through the font store and its single download setting; About › Language data shows every licence; `proof.packs`, `proof.installPack` and `proof.removePack` for agents. Install works once language-packs-1 is published (until then with a test index); the default build then embeds English data only, and keeps today's carry-over data embedded until then (`cargo xtask language`). Lithuanian (#4707) and Romanian (#3438) are available as a Language pack once language-packs-1 is published; the Dutch, Catalan, French, Greek, Slovak, Serbian and Latvian packs (#4145, #2505, #2895, #4947, #1325, #2561, #3806, #4968, #4438) are defined and wait on craft-proof engine support (FLAG long/num, OCONV, ISO 8859-7, CIRCUMFIX, FULLSTRIP). Readiness unchanged. - 2026-11-01: DOCX tables keep rows and cells wrapped in `mc:AlternateContent`, selecting one supported Choice or Fallback with the existing reader policy (#5219). Original synthetic checks cover namespace aliases, unsupported requirements, nested row/cell content controls and two save/reopen cycles; broader corpus and native Word qualification remain open.- 2026-11-01: Shapes: a line's arrowheads are as long and as wide as Word draws them: 2, 3 or 5 times the line's width for small, medium and large (a line under 2 points as a 2-point one). Their kind, width and length are read from DOCX (DrawingML and VML) and saved back; before, every arrowhead was one size and saved as a medium triangle. +- 2026-11-02: Page break before applies to a paragraph opening a continuous section mid-page in paginated layout (#5287). The original Word for Mac 16.113 probe puts each heading on a new page. At the top of a later column in such a section, the break also starts a new page; that case remains unmeasured in Word. Readiness unchanged. - 2026-11-01: Columns with Line between, made for selected text (a section in the middle of a page), draw their line; the line runs as tall as the section's text on the page, not the whole page (#5178). Readiness unchanged. - 2026-10-31: `.doc` import: text that names no font takes the stylesheet's standard font, so a Word 365 `.doc` in Aptos opens in Aptos (or its substitute) instead of Times New Roman, the font table's first entry (#5066). Readiness unchanged. - 2026-10-31: Tables: a row that doesn't fit at the bottom of a page no longer splits off its picture. When a cell's inline picture (one line, which is never split) or first line runs past the room left on a page that already holds something, the row moves to the next page whole instead of leaving the other cells' text beside an empty cell, so a photo row at least 193 pt tall grown to 207 pt by its picture goes on in one piece (#4272). At the top of a page such a row still splits. The reporter's 4-page file is still 5 pages: there the row has less room than its minimum and already moves on whole (#1742); the extra page now comes from the pages above taking more room than in Word. @@ -214,6 +281,8 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm - 2026-10-31: Table of contents: with Word's table over entries in columns, where the title sits in a one-column section above a continuous break, Insert Table of Contents with new settings keeps the title above the break and the entries in their columns (the new title went under the break, into the columns), and Remove Table of Contents takes the title, the blank paragraph and its break with the entries, as Word does (observed in Word for Mac); a paragraph of another style above the break stays (#4041). - 2026-10-31: A table of contents writes its page numbers as `PAGEREF` fields to the headings' bookmarks, as Word does, so updating the fields in Word moves the numbers with the headings; they were plain text (#5097). - 2026-10-31: Keytips reach the Backstage: Alt, F badges its pages with Word's letters, and Alt, F, P (or V) opens Print (#4960). +- **2026-10-31 (#3398):** General Options offers persistent Document background / Soft green / Warm cream / Soft gray screen paper. The view.paperColor command reaches the same setting headlessly. Document page colors and pictures, saves, printing and exports retain their own styling; the screen raster/tile cache and page backing share the selected color. Read Mode None uses the preset; explicit Sepia/Inverse keep priority. Parity counts and readiness estimates unchanged. +- **2026-10-31 (#3693, editing extension):** Delete Previous Sentence is a headless registry command and Mod+Shift+Backspace shortcut, using existing sentence boundaries and deletion/undo/protection paths. Paragraph and cell boundaries are preserved. Additional WordCraft behavior; Word-parity counts and estimates unchanged. - 2026-10-29: Documents with hyperlinks or a table of contents print again on the desktop (#4734). The print job scales each page to the paper, and the spooler refused a page with link annotations ("flatten page annotations before using shrink-to-fit"); the PDF sent to the printer now omits link annotations, which print nothing; the link and table-of-contents text stays on the page. Exported PDFs and Save these pages as PDF (`file.printPdf`) keep the annotations. - 2026-10-28: Word count counts each Chinese and Japanese character as a word, as Word does, in the status bar, the Word Count dialog and NUMWORDS fields. Unspaced Chinese text used to count as one word per run of text; Korean, which is written with spaces, still counts by words. (#4547) - 2026-10-28: Aligned-copy review fixes keep the document axis locked through middle-button scrolling and let Ctrl/Command+Shift start on a secondary selected object. Plain Ctrl and Shift clicks keep their existing behavior (#4764). @@ -235,6 +304,9 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm - 2026-10-29: HTML import keeps preformatted lines with CR, CRLF or LF input newlines (#4810); numeric character references retain their separate meaning; readiness estimates unchanged. - 2026-10-27: File › Print: Return prints, as in Word, where ⌘P opens on Print as the default button; a field or list on the page keeps Return while it has the keyboard. - 2026-10-28: A window restored to a place no monitor covers now opens centred on the primary monitor. Before, a monitor replaced by one of the same size left the window off screen, and the app seemed to close as soon as it started. +- 2026-10-29: Built-in citation refresh indexes source tags and bibliography numbers for each operation (#4888). Existing ordering, duplicate-tag first matches and saved sources remain unchanged. +- 2026-10-25: Select Recipients detects comma, semicolon and tab separators across quoted multiline headers. Quoted punctuation and doubled quotes stay in their cells; detection stops at the first unquoted record boundary. Existing tie preferences and parsing behavior remain unchanged. + - 2026-10-22: View › Window › New Window opens a second window on the document (desktop). The windows are titled `Name:1` and `Name:2` and each keeps its own caret, scroll position, zoom and view; edits and Undo are the document's and show in both. Closing one of them asks nothing; the last one asks about unsaved changes, and quitting asks once per document. - 2026-10-22: One native window per open document on the desktop. File › New and Open (from the ribbon, File, shortcuts, the file manager, the command line or the control channel) open the document in a window of its own, focused and placed a step below and right of the last one; opening a file replaces a blank document nobody has used in its own window, and a file that is already open brings its window to the front. Each window keeps its own ribbon tab, File page, dialogs, panes, scroll position and composition. View › Window › Switch Windows lists the windows and brings the chosen one forward. File › Close and a window's close button close that window, asking about unsaved changes first; the last window stays open with no document. Quitting asks about each document with unsaved changes in its own window, and Cancel keeps everything open. The control channel's input and screenshots go to the window focused last. The web app keeps one window. - 2026-10-22: Recovery covers every open document. Each dirty document keeps its own copy, written on the same ten-second cadence whether it is shown or not; closing a document without saving deletes its copy, as saving does, and a clean exit deletes the copies of every document saved or closed since its last copy. File › Recover lists only copies no open document keeps, and Restore opens a copy as another document (or in place of an untouched blank one), so it never replaces work. After a crash with several unsaved documents, the next start lists all of them. @@ -243,6 +315,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm - 2026-10-22: Legacy form names and selected drop-down labels decode numeric XML references, including saved whitespace, without decoding escaped reference text twice. - 2026-10-25: Interface catalogs are still the shared storytold/craft-i18n ones embedded from `craft-i18n.rev` (`9205108c…`): 27 WordCraft languages with English fallback for missing strings, and the equation “Delete Around Caret in Equation” translation preserved. Native-speaker review remains human. - 2026-10-25: Input methods (egui 0.37): character- and byte-counted surrounding deletions, a fresh composition after body/equation replacement, rejection of queued stale replacements before mutation, transactional equation deletes and one undo group, with tracked and placeholder length mapping. Synthetic regressions document these paths; managed CI passed, and native IME acceptance (#155–#164) plus browser IME behavior remain open. +- 2026-10-25: Justified and distributed lines keep the width of the spaces before their first character, as Word does: Thai official letters indent paragraphs with a run of spaces, and their first line no longer moves right by the line's slack (86 pt in a measured case). A distributed line's text reaches the margin even when the line ends with the space it wrapped at. - 2026-10-25: Text Highlight Color shows the last colour as the button's bar in the ribbon and mini toolbar; after No Color it shows the pen without a misleading yellow bar, and the button removes highlighting (#4087). - 2026-10-23: cu20we: DrawingML flat, square and round line caps are imported, saved, displayed and exported to PDF; absent or unknown caps default to flat. VML end caps (including shapetype inheritance) are preserved through DOCX save. Ink and metafile stroke handling remain separate. Synthetic DOCX, raster and PDF regressions cover the change; no new native Word comparison. - 2026-10-23: cu20we: paragraph-relative box floats start above effective space before; header, footer, cell and text-box layout regressions pass. Framed paragraph positioning is unchanged; no new native Word comparison. @@ -294,11 +367,15 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target - 2026-10-20: Restrict Editing: read only and comments-only protection grey out every edit in the ribbon when no exception lets the author edit, and tracked-changes protection greys out Accept and Reject, instead of buttons that look live and refuse when clicked. - 2026-10-20: Restrict Editing: with Filling in forms enforced, the Developer tab's content-control buttons and Properties grey out in the ribbon, their tooltips saying why, as in Word; before, they looked live and only refused when clicked. - 2026-10-20: The page tip shown while dragging the vertical scrollbar stays on one line; it broke into two or three lines ("Page 10 of" / "40") as the page number grew during the drag (#3341). +- 2026-10-20: Layout: text in Apple's Times and Courier takes Times New Roman's and Courier New's single-line spacing, as in Word for Mac; in a corpus of 180 real manuscripts, the five set in Times no longer gain a page. - 2026-10-19: Restrict Editing: with Filling in forms enforced, inserting content controls, their Properties and Remove Content Control are refused, as in Word (it greys them out), so a form's fields stay as they are and only their content is filled in. - 2026-10-19: Content controls: text typed over a placeholder takes the control's own run properties (`w:sdtPr/w:rPr`, ECMA-376 §17.5.2.27), as in Word, and the placeholder style is recognised by its built-in name "Placeholder Text" whatever its localized id (German Word: `Platzhaltertext`), so entries in forms from localized Word no longer stay grey. - 2026-10-15: Find opens Navigation on Results; editing the query restores Results after manual Headings/Pages navigation. Typed and selected-text searches retain document contents (#2096). | Date | What landed | +| 2026-11-04 | The CLI opens a non-Unicode input filename even after options such as `--page 2` or `--format flatopc`; text option values still require Unicode; `run --file` and `run --script` retain native paths. | +| 2026-11-06 | Accept All and Reject All no longer restyle the paragraph after an empty one carrying a section break (#5667): a Compare that added a section, then Reject All, left `Body` in the removed paragraph's Normal style and indents. The empty paragraph now goes whole and its section joins the following one, as deleting the break does. | +| 2026-11-07 | Deleting a preceding empty paragraph keeps the following paragraph's style, outline and mark formatting rather than promoting body text to a heading (Leaf #152). Non-empty merges and section absorption retain their existing rules; synthetic model, command, DOCX and Undo/Redo coverage. | | 2026-11-04 | Pictures keep the brightness, contrast and transparency their file gives them (#1711, #1766): a "Washout" picture watermark (VML `gain`/`blacklevel`) and a washed-out letterhead picture behind the text (`a:lum`) are drawn faded on screen and in PDF instead of at full colour, with the pixel curve measured on Word's own PDF export; `a:alphaModFix` transparency too. Saved back as DrawingML (and as VML `gain`/`blacklevel` in Word 2003 XML, without the transparency). Picture Format › Corrections, Transparency and Color › Washout replace the file's values (other Color modes leave them alone), and Reset Picture discards them all, as in Word; the PDF embeds the corrected pixels once per picture | |---|---| | 2026-10-17 | Copies keep their formatting in programs that read RTF rather than HTML: Copy and Cut put RTF beside the text and HTML on the system clipboard on Windows, macOS and Wayland (an X11 copy stays text and HTML, since arboard owns the selection there; a copy of only pictures or shapes carries no HTML or RTF, so pasting it back keeps every detail), and Paste reads another program's RTF on macOS and X11 too, not only on Windows and Wayland | @@ -307,10 +384,13 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target | 2026-11-01 | Proofing moved to the shared libraries, with nothing users see changing: the code is `craft-proof` in storytold/craft-libs (with its history) and the spelling and hyphenation data (the Moby list, our English patterns and word lists, the German, Polish, Portuguese and Italian patterns, `ru-extra`) is craft-i18n's `language-data/`, embedded from `CRAFT_I18N_DIR` at the commit in `craft-i18n.rev`. Before/after `wordcraft-cli` runs on a proofing document give the same issues, suggestions and hyphenated PDF (CP1). | | 2026-10-28 | Fonts a document uses that aren't installed are no longer silent: the font box shows a warning mark (hover names the font drawn instead) and the status bar says how many are missing, listing them (the first 20, then how many more) with the faces drawn instead and Add Font…. The list is what layout actually drew, so hidden text, unused styles and orphaned stories don't count; substitutes that measure the same (Carlito for Calibri, Liberation for Arial) and Symbol/Wingdings bullets aren't flagged | | 2026-10-21 | Read Aloud on Linux speaks through Speech Dispatcher when neither `espeak-ng` nor `espeak` is installed (#4576). It talks to the server over its own socket (SSIP), so the text is never taken for a command and stopping cancels only WordCraft's own speech, not a screen reader's; each sentence goes in its language, region included, and a sentence a screen reader's announcement cuts off is said again after a short pause; if another program keeps stopping it, reading pauses on it with a message. With no speech program at all, Read Aloud names the package to install, and the `.deb` and `.rpm` packages recommend one. | +| 2026-10-31 | trivial | Preserve recipient columns after an unquoted whitespace-only prefix in a headered CSV (#4249); keep quoted headers, headerless whitespace records and shared CSV parsing. | +| 2026-10-31 | trivial | Integrate complete-record recipient CSV delimiter detection (#4249) with current main; preserve leading blank-line detection (#5042), original regressions and readiness estimates. | | 2026-10-28 | **Arabic diacritic editing (#4757):** Backspace removes the last stored Arabic combining mark, so its base letter can stay while the mark is replaced. Caret movement and forward Delete keep their cluster rules. Synthetic command, history, protection and native/DOCX fresh-reopen checks; no native Word comparison or readiness change. | | 2026-10-29 | Insert › Shapes shows the basic shapes as a grid of code-drawn icon tiles, each with an accessible name and tooltip, instead of a text list (#4349) | | 2026-11-03 | A password-protected file named on the command line (double-clicked in the file manager) asks for its password when the first blank document was replaced because File › Options chose a region; the window now follows that replacement at once, so the first frame no longer takes it for a document switch and drops the Password box (#5299) | | 2026-11-01 | Markdown export: a list inside a block quote stays in the quote (`> - item`), its continuation lines included; it was read as quoted list paragraphs but written without `>`, so the list moved out of the quote on every save | +| 2026-11-01 | Rulers: in a section of several text columns the horizontal ruler shades the space between each two columns like the margins (Print Layout, in the main text outside tables, with the selection in one section); dragging an edge of a space changes the spacing (equal columns stay equal, as in Word for Mac), one undo step per drag (#5197) | | 2026-10-29 | Window sizing falls back to the screen rectangle when the reported inner size is invalid, and an Equation symbol tooltip no longer repeats a missing glyph's name or code point | | 2026-10-29 | A NUL in a document title no longer reaches the native window title, which panicked on X11 (#3634): displayed titles and suggested file names drop NUL characters and fall back to the untitled name when empty; the stored title is unchanged | | 2026-10-28 | The Traditional Chinese interface draws hanzi to the Taiwan standard: on the desktop an installed Traditional Chinese font (PingFang TC on macOS, Microsoft JhengHei on Windows, Noto Sans CJK TC on Linux) comes before the embedded Noto Sans CJK SC, which drew 說, 這, 為, 戶 in mainland shapes; without one, and on the web, nothing changes. | @@ -320,6 +400,11 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target | 2026-10-14 | Insert › Date & Time and DATE, TIME and other date fields show the computer's local date, time and weekday, daylight saving included, as Word does (#1707); they showed UTC, so a user in New Zealand at noon on 31 October got 30 October 23:00. Timestamps saved in files (revisions, comments, document properties) stay UTC. Backstage's greeting follows the local hour too | | 2026-10-23 | File > Print: N-up physical sheets and left/right-bound booklets, shared preview/PDF geometry and padded copies (#641); printer jobs keep upstream’s link-annotation fix. | | 2026-10-17 | Password-protected Word files from Word 2007 (Standard encryption: AES-128/192/256, SHA-1) open with their password, also when named `.doc` or `.dot`; a wrong or missing password is reported as for newer files. Word 97-2003 files with an AES header, which the public spec doesn't define, are refused with a clear message (gap 9) | +| 2026-11-04 | Quitting over the control channel and MCP bridge is refused while any open document has unsaved changes — a programmatic call never prompts — and `{"force": true}` quits without saving. The Save Changes prompt and the refusal now share one predicate, so they cannot drift apart (#3274). | +| 2026-10-31 | A picture or shape anchored in a line of a long paragraph on a later page is drawn on that page, as in Word, instead of the page the paragraph starts on; one dropped on such a page stays where it was dropped (#4250) | +| 2026-10-31 | A picture or shape anchored in a line of a long paragraph on a later page is drawn on that page, as in Word, instead of the page the paragraph starts on; one dropped on such a page stays where it was dropped (#4250) | +| 2026-10-31 | A picture or shape dropped beside the first line after a page break inside a paragraph stays on the new page, as in Word, instead of going back to the page before the break (#4262) | +| 2026-11-07 | A brace or bracket shape in a `.doc` file is drawn without a fill unless the file gives it one, as in Word, instead of as a white box that hides the text behind it (#5943) | | 2026-10-27 | Connector rerouting indexes laid-out object positions once per batch, preserving page hints, first-match order and same-page glue; synthetic equivalence, geometry and scaling regressions (#3631); routing policy and readiness estimates unchanged | | 2026-10-28 | Markdown import bounds nested undefined reference-label work within the existing link allowance; literal source and normal reference matching are preserved (#4658). | | 2026-10-23 | A command that changes nothing is not an edit: it adds no undo step, marks no unsaved changes and forces no relayout, so the redo stack survives it and five hundred of them can no longer push the user's own edits out of the undo history. | @@ -366,6 +451,7 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target | 2026-10-19 | Centred numbered headings after a leading page break keep their number before the text (#2964). The label uses the alignment of the line it is drawn on; heading text positions and page count stay the same. | | 2026-10-17 | Edit Recipient List finds records (#1576): a Find in list box hides rows with no cell containing the text (any case) and counts the matches, and "Show only records that will be merged" hides the rows the filter leaves out; both only change the view. Rows are striped, and edits and removes go to the right record however the grid is narrowed | | 2026-10-17 | Recent documents can be taken off the list (#2716): hovering a row in File › Home/Open ▸ Recent shows a × that removes that document, a Clear all button under the list empties it, and the change is written to the preferences right away, so it survives a crash. `ui.recentRemove {"path"}` and `ui.recentClear` do the same for CLI, control and MCP | +| 2026-10-16 | MCP `insert_table` tool (#2242): a filled table in one call from `values`, with `header` (repeating first row) and `style`; `insert.spreadsheet` takes the same `values`/`header`/`style` and rejects ragged, oversized or non-array input | | 2026-10-17 | AccessKit focus guard (#2726): every tree update egui sends carries its focused node, so a frame that drops it falls back to the root instead of panicking in `accesskit_consumer` and closing the window on Windows | | 2026-10-17 | Equation arrays centre a row that has no `&` among rows that do, as Word does, so case equations like Word's "First Property of Radicals" no longer push the aligned row right (#2617). | | 2026-10-17 | Formatting revisions: a table, row, cell or section property change made while tracking now records as `w:tblPrChange`/`w:trPrChange`/`w:tcPrChange`/`w:sectPrChange` (structure stays out), so Reject All restores and Accept All keeps it; test tracked_table_changes_are_kept_as_formatting_changes | @@ -438,11 +524,15 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target | 2026-10-17 | Non-overlapping body floating tables clear header tables. Original native controls distinguish never-overlap from allowed overlap. | | 2026-10-14 | Comment text being typed is no longer lost or misplaced (#1348): an edit or reply the document refuses (for example read-only protection) keeps the typed text in its field and says why on the status line; drafts belong to their document, so after New or Open a comment with the same number shows its own text; Save, Export, Print, Close, New and Open write pending comment text first; if the document refuses it, Save, Export and Print carry on with the stored comments and show why on the status line (a refused draft is dropped when Save is the only change), while Close, New and Open show the Save Changes prompt (Don't Save drops text the document refuses); Escape discards what was typed; in a document protected as read only or for forms the comment fields can't be typed in | |---|---| +| 2026-10-12 | RTF reads and writes footnotes, endnotes and comments (author, initials, date, commented range), so they survive a round trip through RTF (#1053) | +| 2026-11-07 | Page Setup also bounds the unfocused margin display after a long numeric paste; the accepted value is retained (#5650). | +| 2026-11-05 | Page Setup's margin, paper-size and document-grid number boxes scroll long text inside the field instead of enlarging the dialog (#5650). Readiness estimates unchanged. | | 2026-10-29 | Print Markup discovers comment balloon anchors in one forward paragraph-object walk; anchor positions, filtering and the existing cap stay unchanged (#3614) | | 2026-10-26 | Spelling & Grammar opens a right-side Proofing task pane observed in Word for Windows: sentence context, directly clickable suggestion cards and ignore/dictionary actions. Current-story traversal and stale-target checks are retained. Right-click keeps its compact menu. | | 2026-10-22 | Documents that keep content in another format (`w:altChunk`: a web page, RTF, text or another Word document, as generators and mail-merge tools write them) show it where it stands, as Word does, instead of leaving it out; one such file in the docxide-pdf corpus opened as an empty page and now matches Word's 8 pages. `.mht` web archives open, and web pages open the way Word opens them (style sheets, paragraph margins, Times New Roman, single lines, automatic spacing). Insert › Text from File keeps the inserted file's pictures | | 2026-10-18 | Explicit New/Open keeps the requested document visible when startup recovery discovery finishes; older copies stay available. Recover uses the regular theme. See docs/recovery.md. | | 2026-10-15 | A floating picture or shape in a table cell set to align centre or bottom no longer lands half its height above its line (#1887); it starts at the anchor line | +| 2026-10-26 | View › Window across the native windows (#322): Arrange All, View Side by Side, Synchronous Scrolling | | 2026-10-18 | Picture Format › Size (#2573): width and height in the interface unit, and a size typed in any unit works, pixels too (`300 px` keeps the picture's proportions, #1901), applied once typed (Enter, Tab or a click elsewhere; Esc cancels) in one undo step. The group's launcher opens a Size dialog: height and width, scale of the original size (or of the size it opened with), Lock aspect ratio, the original size with its pixels, and Reset. `picture.size {"original": true, "scale"?}` sizes from the original; sizes past 4000 pt that keep the proportions shrink as a whole. | | 2026-10-16 | Save Changes gives assistive technology a named alert dialog with the loss warning and any save failure; visual layout and save policy unchanged (#1015). Native screen-reader verification remains pending. | | 2026-10-17 | `design.scaleFontSizes` (Design › Document Formatting): grow or shrink every style's font size at once, by points (`delta`) or percent, rounded to half points and clamped to 1–1638 pt; inherited sizes follow, runs are untouched, one undo step (#1475) | @@ -769,12 +859,23 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target Footer page-table height and body wrapping are corrected. Other pagination gaps remain open. +Quoted field escapes are interpreted like Word, and body `PAGE` fields use their active section number format in paragraphs and tables. Synthetic regressions cover fields and section layout. + Section page-numbering settings preserve `w:chapStyle` values across the full unsigned-byte range. MATH glyphs that reference the same construction now share its immutable variants and assembly data (#3630). Glyph mappings, values and the existing partial-record fallback stay unchanged. Repeated glyph IDs keep their last valid reference without retaining superseded constructions. Distinct overlapping constructions and assembly-only sharing remain separate. ## Revision history +> 2026-11-08 · Progress log: turned inline pictures without a recorded effect extent keep their line to the frame, as in Word. +> +> 2026-11-05 · Linux self-update ignores another program's AppImage variables (#5653); recorded in the progress log, readiness unchanged. +> 2026-11-05 · A template opened from the file manager starts a new document instead of being edited and saved over (#3433); recorded in the progress log, readiness unchanged. +> 2026-11-05 · Arabic letters join across a change of formatting and past hidden or deleted text (#4093); recorded in the progress log, readiness unchanged. +> 2026-11-05 · Shapes, text boxes, groups and canvases in Strict Open XML documents open (#3548); recorded in the progress log, readiness unchanged. +> 2026-11-07 · Progress log: VML text boxes keep their text direction (#5193). +> > 2026-11-03 · Arabic dialogs read right to left (rtl::Table); recorded in the progress log, readiness unchanged. +> 2026-11-03 · Before Word 2013 layout a floating table moved below another keeps its own left edge, also when its first cell has no left margin; readiness unchanged. > > 2026-10-30 · Technical synchronization: retain the Proofing pane and command/selection/undo contracts on main `3b7f7ed`; new-SHA validation pending, readiness unchanged. > @@ -786,18 +887,55 @@ MATH glyphs that reference the same construction now share its immutable variant | Date | Change | Summary | |---|---|---| +| 2026-11-08 | trivial | Progress log: XML line ends in text normalised (#5897) | +| 2026-11-08 | trivial | Progress log: contextual spacing drops the space after a cell's last Normal paragraph (#5450) | +| 2026-11-06 | trivial | Progress log: the Arabic interface's Styles gallery reads from the right (#5566) | +| 2026-11-07 | trivial | Page Setup also bounds the unfocused margin display after a long numeric paste; the accepted value is retained (#5650). | +| 2026-11-05 | trivial | Page Setup's margin, paper-size and document-grid number boxes scroll long text inside the field instead of enlarging the dialog (#5650). Readiness estimates unchanged. | +| 2026-11-07 | trivial | Progress log: An in-line picture lowered past its own height (`w:position`) lowers its line as far, as in Word; all 156 synthetic lines measured for #3984 match Word (#5796). | +| 2026-11-03 | trivial | Progress log: the title bar's Search box takes the typing in place (#5398) | +| 2026-10-31 | trivial | Progress log: a numFmt none list level shows its literal text (#4985); readiness unchanged | +| 2026-10-31 | trivial | Progress log: font menu entries are drawn from shared textures (#5105) | +| 2026-11-02 | trivial | Progress log: a linked picture shows and follows its link; DOCX `a:hlinkClick` (#5267) | +| 2026-11-02 | trivial | Progress log: alternative format chunks and MHT open | +| 2026-11-06 | trivial | Progress log: Change Picture keeps the width and takes the new picture's aspect ratio (#5761) | +| 2026-11-06 | trivial | Progress log: superscript and subscript text at 0.65 of its size rounded to the half point (Word for Mac) | +| 2026-11-06 | trivial | Progress log: line numbers take the document's "Line Number" style | +| 2026-11-07 | trivial | Charts: a series without its own colour takes the accent its `c:idx` picks, as Word does, not the one its place in the file picks (#5963) | +| 2026-11-07 | trivial | A floating anchor after a manual page/column break counts visible content on the far side of the same wrapped row. Marker-only cover anchors retain their previous-page rule. Synthetic picture spacing, placement and history controls; no new native Word measurement. Estimates unchanged. | +| 2026-11-06 | trivial | Print helpers remove loaded PDF/settings files before driver work, tolerate prior removal and report cleanup errors; a held-driver regression checks the files are already absent (#5710) | +| 2026-11-06 | trivial | Windows print submissions isolated per helper process; cancelled driver work cannot block a retry (#5710) | +| 2026-11-06 | trivial | Progress log: print cancellation before sending, including without a document, and the Windows repeated-conversion limitation (#5710) | +| 2026-11-06 | trivial | Progress log: the printer list no longer waits on printer drivers (#5710) | +| 2026-11-06 | trivial | Progress log: resolving away an empty paragraph with a section break keeps the next paragraph's properties, #5667; readiness estimates unchanged | | 2026-11-05 | trivial | PR #4545: eligible cell floats retain in-bounds offsets and touch the outer cell edge only on overflow, ignoring wrap distances for object placement. Front-of-text, turned-cell and outside-cell opt-outs remain. Synthetic geometry controls; native front/behind-text parity remains unqualified. | | 2026-11-04 | trivial | Progress log: CLI convert format options; readiness estimates unchanged | | 2026-11-04 | trivial | Progress log: MHT quoted MIME parameters; readiness estimates unchanged | +| 2026-10-30 | trivial | Progress log: typed n-ary operators such as ∫ and ∑ wait for their limits, and any character that cannot continue a math name corrects it, as observed in Word for Windows on #4938; Enter, arrow keys and nested operators are WordCraft's own choices (#4917); readiness unchanged | +| 2026-11-04 | trivial | CLI input paths keep their native filename when a value-taking option comes first; regression coverage includes conversion to Flat OPC and rejection of non-Unicode option values. | | 2026-11-05 | trivial | DOCX paragraph `w:adjustRightInd` is preserved with true, false and absent values; OOXML properties follow schema order. Grid layout remains open. | +| 2026-11-04 | trivial | Progress log: RTF Unicode fallback counts; readiness estimates unchanged | | 2026-10-28 | trivial | Per-page ink object lookups, #3614. Drawing order, geometry and saved data retain their behavior; no readiness estimate change | +| 2026-11-05 | trivial | Progress log: ODT heading text of another size (gap 19) | +| 2026-11-05 | trivial | Progress log: ODT table rows that don't break (gap 19) | +| 2026-11-05 | trivial | Progress log: a watermark or odd and even headers change to a file is saved | +| 2026-11-05 | trivial | Progress log: ODT font declarations kept (gap 19) | +| 2026-11-05 | trivial | Progress log: ODT pictures offered in several formats (gap 19) | | 2026-10-28 | trivial | Shared MATH construction records reuse variants and assembly data without changing glyph mappings or existing limits (#3630); readiness estimates unchanged | +| 2026-10-29 | trivial | Page drawing indexes text-box warp lookups per paragraph (#3614); requested offsets only, bounded prefix traversal, original Linux display and persistence controls; readiness estimates unchanged | +| 2026-10-31 | trivial | Progress log: a shape dropped just after a page break inside a paragraph stays on the new page (#4262) | +| 2026-10-31 | trivial | Progress log: a floating object anchored on a later page of a long paragraph is drawn there (#4250) | +| 2026-10-28 | trivial | Protected typing boundary lookup counts preceding objects once and indexes editable permission IDs (#3614); caret movement and enforcement policy are unchanged. | | 2026-11-04 | trivial | Progress log: OpenDocument display settings removed (gap 19) | | 2026-10-31 | trivial | Progress log: dialogs are modal except the five Word keeps modeless; a press beside one shakes it (#3859) | | 2026-11-04 | trivial | Progress log: picture brightness, contrast and transparency (#1711, #1766) | +| 2026-11-04 | minor | `app.quit` over the control channel and MCP bridge is refused while a document has unsaved changes, and `{"force": true}` quits without saving; the prompt and the refusal share one predicate (#3274). | +| 2026-11-04 | minor | `file.closeDocument` over the control channel and MCP bridge is refused while the document it names has unsaved changes, a save the browser's picker hasn't finished, or a comment still being typed; `{"force": true}` discards them. The prompt and the refusal share one predicate (#3274). | +| 2026-11-04 | minor | A programmatic `file.close` on a document shown in two windows closes the window alone, as the window's own close does: the document and its unsaved work stay open, so the control channel no longer refuses a close that loses nothing (#3274). | | 2026-11-04 | trivial | Progress log: ODT element audit (gap 19) | | 2026-11-04 | trivial | Progress log: ODT checked against 220 files from other applications (gap 19) | | 2026-11-04 | trivial | Progress log: OpenDocument files shown in Microsoft Word style or LibreOffice style (gap 19) | +| 2026-11-07 | trivial | Progress log: `.doc` brace and bracket shapes unfilled by default (#5943) | | 2026-11-04 | trivial | PR #4609: relative REF keeps context already displayed by the own or a needed ancestor label; six synthetic legal-template cases and separate-list context follow the Word for Windows measurements reported by @pakiat in PR discussion. Existing unknown-switch/cache boundaries and readiness estimates unchanged; no new native Word run. | | 2026-11-01 | trivial | Progress log: SEQ, STYLEREF and REF fields show fresh results in output | | 2026-10-29 | trivial | Progress log: Print Markup balloon anchors use one object walk per paragraph (#3614); readiness estimates unchanged | @@ -805,17 +943,24 @@ MATH glyphs that reference the same construction now share its immutable variant | 2026-11-03 | trivial | Progress log: Language pack languages in every language list (CP4.3) | | 2026-11-03 | trivial | Progress log: the missing-pack bar on a fresh install, the caret's typing language, and the spelling menu heading in the interface language (CP4.3) | | 2026-11-03 | trivial | Progress log: Options › Language and Proofing redesign (CP4.3) | +| 2026-11-03 | minor | Home › Paragraph Tools (#5043): nine selection- or document-scoped conversion and cleanup commands; one undo step per command, with tracked deletions preserved; readiness estimates unchanged | | 2026-10-30 | trivial | Progress log: layout resource caps — repeated table headers are bounded by item counts, the wrap cache by decoded pixels and linked text box layout by its lines and items; PDF export, hit testing, tracked changes, clipped tables and long endnote selections cost linear time (#3867, #3866) | | 2026-10-17 | trivial | Progress log: RTF on the system clipboard | | 2026-11-03 | trivial | Progress log: craft-proof pin, French elision suggestions and Valencian (CP4.3) | | 2026-11-03 | trivial | Progress log: a long comment's card and balloon stop at ten lines (#3382) | +| 2026-11-03 | trivial | Progress log: Shape Format › Size in the interface unit (#4196) | +| 2026-11-03 | trivial | Find smart-quoted text with a keyboard apostrophe (#5388); exact search modes and replacement spelling preserved. | +| 2026-11-03 | trivial | Progress log: a picture in a paragraph whose hidden mark closes a section keeps its room (#5288) | +| 2026-11-03 | trivial | Progress log: a nested table's Can't Split row moves on with its outer row | +| 2026-11-03 | trivial | Progress log: no line break between a full stop and an opening parenthesis | +| 2026-11-03 | trivial | Progress log: an empty paragraph after a document's leading floating table takes no space before | | 2026-11-03 | trivial | Progress log: Replace and Remove Table of Contents with Word's title above a section break (#4041) | | 2026-11-03 | trivial | Progress log: language-packs-2 published and pinned, 75 Language packs | | 2026-11-02 | trivial | Progress log: language-packs-1 published and pinned, 61 Language packs (CP4.4) | | 2026-11-01 | trivial | Progress log: Language packs install from File › Options › Language and the missing-pack bar once language-packs-1 is published (CP4) | | 2026-11-01 | trivial | Progress log: English spelling reads SCOWL 2026 (CP2) | +| 2026-11-02 | trivial | Honor page break before at a continuous section's mid-page top (#5287); its later-column case remains unmeasured in Word. Readiness unchanged. | | 2026-11-01 | trivial | Progress log: arrowheads keep their kind, width and length and are drawn at Word's sizes | -| 2026-11-02 | trivial | Progress log: alternative format chunks and MHT open | | 2026-11-01 | trivial | Progress log: columns for selected text draw their line between (#5178) | | 2026-10-31 | trivial | Progress log: a Word 365 .doc in Aptos opens in Aptos (#5066); readiness unchanged | | 2026-10-31 | trivial | Progress log: a table row doesn't split off a picture that doesn't fit (#4272) | @@ -825,9 +970,12 @@ MATH glyphs that reference the same construction now share its immutable variant | 2026-11-01 | trivial | Progress log: paragraph side borders lie outside the text (#5036) | | 2026-11-01 | trivial | Progress log: proofing moved to craft-proof (craft-libs) and its data to craft-i18n `language-data/` (CP1) | | 2026-11-01 | trivial | #3915: preserve guarded whole-line ordinary-tab alignment with current main. Existing controls retained; source checks only, primary qualification pending. | +| 2026-11-01 | trivial | #2437: Preserve finite paragraph-border flow extents and terminal-break keep preflight with current-main changes. Recursive terminal-successor lookahead remains outside scope. Source checks only; primary qualification pending. | | 2026-10-31 | trivial | Progress log: a paragraph naming an undefined DOCX style is formatted as Normal (#5077) | +| 2026-10-31 | minor | #3398: persistent screen-only green, cream and gray paper presets, with document backgrounds and export colors retained. Read Mode None follows the screen preset; Sepia/Inverse retain priority. | | 2026-10-31 | trivial | Progress log: TOC page numbers are PAGEREF fields (#5097) | | 2026-10-31 | trivial | Progress log: Backstage keytips, Alt, F, P prints (#4960) | +| 2026-10-31 | minor | #3693: Delete Previous Sentence command and keyboard dispatch, with sentence/paragraph/cell, selection, undo and tracking regressions. Additional WordCraft behavior; Word-parity estimates unchanged. | | 2026-10-30 | trivial | Progress log: only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them (#4734) | | 2026-10-29 | trivial | Progress log: documents with links print (#4734) | | 2026-10-31 | trivial | Synchronize the existing Proofing pane with main `eecbb8f`; preserve explicit-position lookup, implicit selection, navigation, undo and per-window state; fresh exact-SHA hosted validation pending, readiness unchanged | @@ -843,6 +991,7 @@ MATH glyphs that reference the same construction now share its immutable variant | 2026-10-28 | trivial | Note conversion walks reference offsets and formatting runs together, #4750; readiness estimates unchanged. | | 2026-10-29 | trivial | DOCX comment IDs use ordered paragraph lookup and one durable-ID reservation set; threading and ID rules unchanged | | 2026-10-28 | trivial | #4658: bound Markdown reference-label rejection work, preserving literal source and ordinary link matching | +| 2026-11-01 | trivial | Progress log: the ruler shows the spaces between text columns (#5197) | | 2026-10-29 | trivial | Progress log: Insert › Shapes icon grid (#4349) | | 2026-10-29 | trivial | HTML import keeps preformatted lines with CR, CRLF or LF input newlines (#4810); numeric character references retain their separate meaning; readiness estimates unchanged. | | 2026-10-29 | trivial | Progress log: window fallback geometry and equation symbol tooltips | @@ -854,12 +1003,16 @@ MATH glyphs that reference the same construction now share its immutable variant | 2026-10-27 | trivial | PR #4609: Numeric cross-references (#4533): preserve the numbered clause through field updates; adapted from @Alyosha-dot-shop's issue patch, with extra relative-context and cache guards. Synthetic checks only; readiness estimates unchanged. | | 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; exact selected spelling takes priority over overlapping grammar, with command/pane regressions and integration workflow paths; readiness estimates unchanged | | 2026-10-14 | trivial | Progress log: Insert Date and Time and DATE/TIME fields use local time (#1707) | +| 2026-10-27 | trivial | Preserve #4249 complete-record CSV delimiter detection and its regressions in the mailings modules; recipient commands, limits and refusal behavior unchanged. | +| 2026-10-26 | trivial | Integrate #4249 with current-main CSV helpers, preserving logical-record detection, existing recipient limits and refusal behavior. | +| 2026-10-25 | trivial | Recipient CSV delimiter detection reads the first complete record, including quoted line breaks and doubled quotes; separator ties and cell parsing remain unchanged. Original parser and CLI import/save/reopen checks; readiness estimates unchanged. | | 2026-10-22 | trivial | Progress log: one native window per open document | | 2026-10-22 | trivial | Progress log: every open document keeps its own recovery copy | | 2026-10-22 | trivial | Progress log: several open documents in the desktop window | | 2026-10-22 | trivial | Progress log: several open documents in one session | | 2026-10-22 | trivial | Decode numeric XML references once in legacy form names and drop-down labels | | 2026-10-25 | trivial | Progress log: egui 0.37 migration records the `craft-i18n.rev` catalog pin (9205108…; 27 languages) and the input-method handling; managed CI passed; native acceptance and native-speaker review remain open | +| 2026-10-25 | trivial | Progress log: justified and distributed lines keep the width of the spaces before their first character | | 2026-10-17 | trivial | Progress log: Standard-encrypted (AES) Word files open (gap 9) | | 2026-10-25 | trivial | Progress log: Text Highlight Color buttons show the remembered colour, or no colour bar after No Color (#4087) | | 2026-10-23 | minor | A command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it. | @@ -920,12 +1073,14 @@ MATH glyphs that reference the same construction now share its immutable variant | 2026-10-19 | trivial | A right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, starts the next line with its text instead of ending the line (a TOC entry's page number keeps its leader); a left tab still past the right indent there takes no room (#1840) | | 2026-10-19 | trivial | Progress log: a picture dragged near the bottom of a page lands there (#3169) | | 2026-10-19 | trivial | Progress log: a Thai run with Latin finds one installed font covering it all on Linux too (#1549) | +| 2026-10-20 | trivial | Progress log: Times and Courier take their Windows counterparts' line spacing | | 2026-10-19 | trivial | Progress log: forms protection keeps its content controls: no inserting, Properties or Remove Content Control | | 2026-10-19 | trivial | Progress log: text typed into a content control takes the control's formatting; localized placeholder styles recognised | | 2026-10-17 | trivial | Progress log: #2048 entry covers anchors pushed onto the next page | | 2026-10-17 | trivial | A line breaks after a hyphen before a digit, like Word (#2712) | | 2026-10-15 | trivial | Progress log: text above a float's anchor wraps around it, #2048 | | 2026-10-18 | trivial | Progress log: long navigation headings wrap within the pane instead of ending in an ellipsis (#3015) | +| 2026-10-26 | trivial | View › Window across the native windows (#322): View catalog 26/29 → 29/29 | | 2026-10-18 | trivial | Font list starts at its first row after clearing a no-match query; recent heading remains visible (#2068) | | 2026-10-18 | trivial | A word after a slash wraps whole, like Word (#2963) | | 2026-10-19 | trivial | Progress log: render threads reused while typing (#3027) | @@ -1235,9 +1390,13 @@ MATH glyphs that reference the same construction now share its immutable variant | 2026-10-29 | trivial | Progress log: corporate authors in bibliographies (#4848) | | 2026-10-29 | trivial | Progress log: Table Properties follows the selection (#4857) | | 2026-11-01 | trivial | Progress log: a list inside a Markdown block quote stays quoted | +| 2026-10-29 | trivial | Progress log: citation source and bibliography-number lookups (#4888) | | 2026-10-27 | trivial | Progress log: AutoCorrect Options with no document open (#4585) | | 2026-10-28 | trivial | Progress log: RTF metafile pictures are imported (#4259) | | 2026-10-28 | trivial | Progress log: Chinese and Japanese text counts a word per character (#4547) | | 2026-10-27 | trivial | 2026-10-27 · #4533 review: numeric REF updates lazily index body bookmarks once per update, retaining the first body bookmark for duplicate names. Unsupported switches and ordinary REF/PAGEREF behavior are unchanged; a synthetic 64-reference regression covers repeated updates and duplicate names. Estimates unchanged. | | 2026-10-27 | trivial | 2026-10-27 · #4533: numeric REF fields use paragraph list numbers for no/full/relative context, reusing bounded numbering. Shared context already embedded in a level label is removed; merged-away cells do not count. Unsupported separator/text/position switches and ambiguous contexts or delimiter-free joins retain cached results. Synthetic OOXML regressions; no native Word or real-document validation, estimates unchanged. | > 2026-10-29 · Port #3915 to split layout modules: ordinary LTR tab groups follow paragraph alignment. Positional tabs, lists, RTL and display math retain their alignment paths. Source port only; no test execution or new native measurements. + +> 2026-10-29 · Port #2437 to split layout modules: finite border extents and terminal-break keep preflight. Recursive keep lookahead remains outside scope. Source port only; no test execution or new native measurements. +| 2026-11-03 | trivial | Progress log: a line breaks after a hyphen before a slash (#3408) | diff --git a/apps/wordcraft-cli/src/main.rs b/apps/wordcraft-cli/src/main.rs index 669da7c377..3d4235786d 100644 --- a/apps/wordcraft-cli/src/main.rs +++ b/apps/wordcraft-cli/src/main.rs @@ -98,8 +98,8 @@ fn open(path: &Path, password: Option<&str>) -> Result { Ok(s) } -/// The commands that open a file, and which of their arguments not starting with `--` it is (as -/// `pos` counts). `run` opens the value of `--file`. +/// The commands that open a file, and which positional argument it is, excluding option values. +/// `run` opens the value of `--file`. const OPENS_FILE: &[(&str, usize)] = &[("convert", 0), ("info", 0), ("text", 0), ("inspect", 0), ("render", 0), ("zotero", 1)]; /// The arguments as text. Only the file the command opens may have a name that isn't valid Unicode, @@ -108,8 +108,14 @@ const OPENS_FILE: &[(&str, usize)] = &[("convert", 0), ("info", 0), ("text", 0), fn text_args>(raw: &[S]) -> Result<(Vec, Option), String> { let args: Vec = raw.iter().map(|a| a.as_ref().to_string_lossy().into_owned()).collect(); let cmd = args.first().map(String::as_str).unwrap_or(""); + let specs = match cmd { + "convert" => CONVERT_OPTIONS, + "render" => RENDER_OPTIONS, + "zotero" => ZOTERO_OPTIONS, + _ => PASSWORD_OPTIONS, + }; let file_at = match OPENS_FILE.iter().find(|(c, _)| *c == cmd) { - Some((_, n)) => args.iter().enumerate().skip(1).filter(|(_, a)| !a.starts_with("--")).nth(*n).map(|(i, _)| i), + Some((_, n)) => positional_indices(args.get(1..).unwrap_or_default(), specs).get(*n).map(|i| i + 1), None if cmd == "run" => args.iter().position(|a| a == "--file").map(|i| i + 1), None => None, }; @@ -145,8 +151,13 @@ fn password_from(args: &[String]) -> Result, String> { } /// The positional arguments, skipping every option's value so an option value is never read as a -/// path. Mirrors [`validate_options`], which has already checked the options are known. +/// path. Uses the same option specifications as [`validate_options`]. fn positionals<'a>(args: &'a [String], specs: &[OptionSpec]) -> Vec<&'a String> { + positional_indices(args, specs).into_iter().filter_map(|i| args.get(i)).collect() +} + +/// Keep the original indices too, so [`text_args`] can open a positional path by its native name. +fn positional_indices(args: &[String], specs: &[OptionSpec]) -> Vec { let mut out = Vec::new(); let mut i = 0; while let Some(arg) = args.get(i) { @@ -156,7 +167,7 @@ fn positionals<'a>(args: &'a [String], specs: &[OptionSpec]) -> Vec<&'a String> i += 2; } else { if !arg.starts_with("--") { - out.push(arg); + out.push(i); } i += 1; } diff --git a/apps/wordcraft-cli/src/tests.rs b/apps/wordcraft-cli/src/tests.rs index 209fb546e7..844dda5689 100644 --- a/apps/wordcraft-cli/src/tests.rs +++ b/apps/wordcraft-cli/src/tests.rs @@ -211,6 +211,10 @@ fn only_the_opened_file_may_be_other_than_unicode() { let ok = |raw: Vec| text_args(&raw).unwrap().1; assert_eq!(ok(vec!["zotero".into(), "refresh".into(), latin1()]), Some(PathBuf::from(latin1()))); assert_eq!(ok(vec!["render".into(), latin1(), "out.png".into(), "--page".into(), "2".into()]), Some(PathBuf::from(latin1()))); + assert_eq!(ok(vec!["render".into(), "--page".into(), "2".into(), latin1(), "out.png".into()]), Some(PathBuf::from(latin1()))); + assert_eq!(ok(vec!["convert".into(), "--format".into(), "flatopc".into(), latin1(), "out.xml".into()]), Some(PathBuf::from(latin1()))); + assert_eq!(ok(vec!["info".into(), "--password".into(), "secret".into(), latin1()]), Some(PathBuf::from(latin1()))); + assert_eq!(ok(vec!["zotero".into(), "--port".into(), "23119".into(), "refresh".into(), latin1()]), Some(PathBuf::from(latin1()))); assert_eq!(ok(vec!["run".into(), "--print".into(), "--file".into(), latin1()]), Some(PathBuf::from(latin1()))); assert_eq!(ok(vec!["info".into(), "in.docx".into()]), None); // Every command that opens a file reaches the open with it (no such file here), so @@ -228,6 +232,7 @@ fn only_the_opened_file_may_be_other_than_unicode() { for (raw, n) in [ (vec!["zotero".into(), latin1(), "in.docx".into()], 2), (vec!["render".into(), "in.docx".into(), latin1()], 3), + (vec!["render".into(), "--page".into(), latin1(), "in.docx".into(), "out.png".into()], 3), (vec!["run".into(), "--save".into(), latin1()], 3), (vec!["commands".into(), latin1()], 2), ] { diff --git a/apps/wordcraft-cli/tests/it/non_utf8_args.rs b/apps/wordcraft-cli/tests/it/non_utf8_args.rs index fef340285b..032a28b09f 100644 --- a/apps/wordcraft-cli/tests/it/non_utf8_args.rs +++ b/apps/wordcraft-cli/tests/it/non_utf8_args.rs @@ -53,6 +53,15 @@ fn the_file_a_command_opens_may_have_any_name() { let _ = std::fs::remove_dir_all(dir); } +#[test] +fn an_option_before_the_input_keeps_its_native_name() { + let (dir, file) = folder("option-first"); + let saved = dir.join("out.xml"); + ok(&cli(&["convert".into(), "--format".into(), "flatopc".into(), file, saved.clone().into()])); + assert!(std::fs::read_to_string(saved).unwrap().contains("Bonjour")); + let _ = std::fs::remove_dir_all(dir); +} + #[test] fn any_other_such_argument_is_an_error_naming_it() { let (dir, file) = folder("error"); diff --git a/apps/wordcraft-web/Cargo.toml b/apps/wordcraft-web/Cargo.toml index 971daf0cbd..8c42c988b1 100644 --- a/apps/wordcraft-web/Cargo.toml +++ b/apps/wordcraft-web/Cargo.toml @@ -17,7 +17,9 @@ path = "src/main.rs" wordcraft-engine = { workspace = true } wordcraft-ui-egui = { workspace = true } # The host messages (storytold/craft-libs): one contract and one bridge for the web builds. -craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "2e2312ddaf21bc3d85cd3395d0955c7d239306f6", version = "0.1.0" } +# The page command API (storytold/craft-libs#413): window.wordcraft over the control channel. +craft-web-api = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" } +craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" } egui = { workspace = true } log = { workspace = true } serde_json = { workspace = true } diff --git a/apps/wordcraft-web/src/api.rs b/apps/wordcraft-web/src/api.rs new file mode 100644 index 0000000000..e0dbfed200 --- /dev/null +++ b/apps/wordcraft-web/src/api.rs @@ -0,0 +1,61 @@ +//! `window.wordcraft`: the page command API (`craft-web-api`, storytold/craft-libs#413) over the +//! app's own control channel, the one the desktop control server drives (`docs/control-protocol.md`). +//! +//! The crate owns the JavaScript side (`request`, `execute`, `commands`, `docInspect`, `uiInspect`, +//! `screenshot`, as promises with a timeout). This module is WordCraft's part: a [`Request`] that +//! queues a [`ControlRequest`] for the app, which answers it on its next frame, and waits for the +//! reply. WordCraft's control request has no abandon state, so a call the page gave up on still runs +//! when the app reaches it, as it does for a desktop client that timed out. + +use std::rc::Rc; +use std::sync::mpsc::{Receiver, TryRecvError, channel}; + +use craft_web_api::methods::Methods; +use craft_web_api::page::{PageApi, Request}; +use serde_json::{Value, json}; +use wasm_bindgen_futures::JsFuture; +use wordcraft_ui_egui::ControlRequest; + +/// The app id in `window.`. +const APP: &str = "wordcraft"; + +/// How often a waiting call looks for the app's reply, in milliseconds (about a frame). +const POLL_MS: i32 = 16; + +/// Put `window.wordcraft` on the page and return the queue the app drains each frame (hand it to +/// `WordApp::with_control`). An existing `window.wordcraft` keeps the functions it already has. +pub fn install(ctx: egui::Context) -> Receiver { + let (queue, requests) = channel::(); + let request: Request = Rc::new(move |method, params| { + let (control, reply) = ControlRequest::new(method, params); + if queue.send(control).is_err() { + return Box::pin(async { json!({"ok": false, "error": "the app is not running"}) }); + } + ctx.request_repaint(); + Box::pin(wait(reply)) + }); + if let Err(e) = Rc::new(PageApi::new(APP, Methods::default(), request)).install() { + log::warn!("couldn't install window.{APP}: {e:?}"); + } + requests +} + +/// The app's reply, polled about once a frame. +async fn wait(reply: Receiver) -> Value { + loop { + match reply.try_recv() { + Ok(reply) => return reply, + Err(TryRecvError::Empty) => sleep(POLL_MS).await, + Err(TryRecvError::Disconnected) => return json!({"ok": false, "error": "the app dropped the request"}), + } + } +} + +async fn sleep(ms: i32) { + let promise = js_sys::Promise::new(&mut |resolve, _| { + if let Some(window) = web_sys::window() { + let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, ms); + } + }); + let _ = JsFuture::from(promise).await; +} diff --git a/apps/wordcraft-web/src/main.rs b/apps/wordcraft-web/src/main.rs index eb24ece139..285ce2e435 100644 --- a/apps/wordcraft-web/src/main.rs +++ b/apps/wordcraft-web/src/main.rs @@ -20,6 +20,8 @@ mod prefs; #[cfg(any(target_arch = "wasm32", test))] mod save_fallback; +#[cfg(target_arch = "wasm32")] +mod api; #[cfg(target_arch = "wasm32")] mod host; #[cfg(target_arch = "wasm32")] diff --git a/apps/wordcraft-web/src/web.rs b/apps/wordcraft-web/src/web.rs index a6bb8a8990..fb808decf8 100644 --- a/apps/wordcraft-web/src/web.rs +++ b/apps/wordcraft-web/src/web.rs @@ -76,7 +76,8 @@ pub fn start() { if let Some(bridge) = &app_bridge { bridge.connect(&mut services, inbox.clone(), cc.egui_ctx.clone()); } - let mut app = WordApp::new(session, services); + let control = crate::api::install(cc.egui_ctx.clone()); + let mut app = WordApp::new(session, services).with_control(control); // Pasted pictures wait apart from dropped files: the app inserts one only when the page is the edit target (#1349). app.services.pasted = Some(pasted); app.autosave = false; diff --git a/apps/wordcraft/Cargo.toml b/apps/wordcraft/Cargo.toml index f44ae42a57..0fd7a423db 100644 --- a/apps/wordcraft/Cargo.toml +++ b/apps/wordcraft/Cargo.toml @@ -22,6 +22,8 @@ wordcraft-control-key = { workspace = true } eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "clipboard", "wayland", "x11", "links"] } egui = { workspace = true } serde_json = { workspace = true } +serde = { workspace = true } +tempfile = "3" log = { workspace = true } chrono = { workspace = true } @@ -77,7 +79,9 @@ windows = { version = "0.62", features = ["UI_ViewManagement", "System_UserProfi clipboard-win = { version = "5.4", features = ["std"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } +# Pinned apart from craft-ui, which doesn't depend on it: `printer_queues`, the printer list that +# asks no driver (#5710, storytold/craft-libs#618), and `submit_cancellable` (storytold/craft-libs#622). +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "a3e0b5a9440d18f19422a9dca13516747b7d3181" } rfd = { workspace = true } # Waits for an `rfd::AsyncFileDialog` on a background thread (the dialogs don't block the UI, #94); # rfd itself already depends on it. diff --git a/apps/wordcraft/src/appimage_env.rs b/apps/wordcraft/src/appimage_env.rs new file mode 100644 index 0000000000..9772244621 --- /dev/null +++ b/apps/wordcraft/src/appimage_env.rs @@ -0,0 +1,139 @@ +//! An AppImage's environment inherited from another program (#5653). +//! +//! The AppImage runtime gives the program it starts `APPIMAGE` (the image's file), `APPDIR` (where +//! it is mounted), `ARGV0` and `OWD`, and whatever that program starts inherits them. So an AppImage +//! app that opens a document through `xdg-open` starts an installed WordCraft with *its* `APPIMAGE`, +//! and the self-update, which trusts that variable, replaces the other app's file with WordCraft. +//! +//! This copy is in the image only when its executable is under `APPDIR`. Otherwise the variables +//! are another program's, and the process starts over without them, before anything reads them. +//! (Removing them in place is `unsafe` under this edition, and `exec` keeps the process id, the +//! arguments and the standard streams.) + +use std::ffi::OsStr; +use std::io::Write; +use std::os::unix::process::CommandExt; +use std::path::Path; +use std::process::Command; + +/// What the AppImage runtime sets; the same list as the documented workaround on the issue. +const RUNTIME_VARS: [&str; 4] = ["APPIMAGE", "APPDIR", "ARGV0", "OWD"]; + +/// Whether the executable runs from the image mounted at `appdir`. Anything unclear counts as not: +/// the update then goes through the package or the plain file this copy really is, and at worst +/// says it can't, where the other answer would overwrite a file that isn't WordCraft's. +fn runs_from_image(exe: Option<&Path>, appdir: Option<&OsStr>) -> bool { + let (Some(exe), Some(appdir)) = (exe, appdir) else { return false }; + let appdir = Path::new(appdir); + if !appdir.is_absolute() { + return false; + } + let Ok(appdir) = appdir.canonicalize() else { return false }; + // No image mounts at the root, where every program on the machine would count as inside it. + appdir.parent().is_some() && exe.starts_with(&appdir) +} + +/// Starts this process over without another program's AppImage variables; returns only when there +/// are none to drop (`None`) or starting over failed. Call it first, before any other thread starts. +pub(crate) fn drop_inherited() -> Option { + let appimage = std::env::var_os("APPIMAGE")?; + let exe = std::env::current_exe().ok(); + if runs_from_image(exe.as_deref(), std::env::var_os("APPDIR").as_deref()) { + return None; + } + // The logger isn't up yet, and wouldn't survive `exec`. Unlike `eprintln!`, never panics. + let _ = writeln!(std::io::stderr(), "wordcraft: ignoring the AppImage {:?} inherited from another program", Path::new(&appimage)); + let mut args = std::env::args_os(); + // This file, whatever has happened to its path since it started. + let mut again = Command::new("/proc/self/exe"); + if let Some(arg0) = args.next() { + again.arg0(arg0); + } + again.args(args); + for var in RUNTIME_VARS { + again.env_remove(var); + } + Some(again.exec()) +} + +#[cfg(test)] +mod tests { + use super::runs_from_image; + use std::path::{Path, PathBuf}; + + /// A folder of its own for one test, removed when it ends. + struct Tmp(PathBuf); + + impl Tmp { + /// An empty folder named after `test` and this process, made afresh. + fn new(test: &str) -> Tmp { + let dir = std::env::temp_dir().join(format!("wordcraft-appimage-{test}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + Tmp(dir) + } + } + + impl Drop for Tmp { + /// Removes the folder and what the test left in it. + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + + /// A mount with the executable inside it, by real path (`/tmp` may be a link). + fn mount(root: &Path, name: &str) -> (PathBuf, PathBuf) { + let dir = root.join(name); + std::fs::create_dir_all(dir.join("usr/bin")).unwrap(); + let dir = dir.canonicalize().unwrap(); + let exe = dir.join("usr/bin/wordcraft"); + (dir, exe) + } + + #[test] + fn an_executable_in_the_mount_is_the_image() { + let tmp = Tmp::new("own"); + let (dir, exe) = mount(&tmp.0, ".mount_WordCr"); + assert!(runs_from_image(Some(&exe), Some(dir.as_os_str()))); + } + + #[test] + fn a_link_to_the_mount_is_the_same_mount() { + let tmp = Tmp::new("link"); + let (dir, exe) = mount(&tmp.0, ".mount_WordCr"); + let link = tmp.0.join("link"); + std::os::unix::fs::symlink(&dir, &link).unwrap(); + assert!(runs_from_image(Some(&exe), Some(link.as_os_str()))); + } + + #[test] + fn an_installed_executable_is_not_in_another_programs_mount() { + let tmp = Tmp::new("other"); + let (other, _) = mount(&tmp.0, ".mount_other"); + assert!(!runs_from_image(Some(Path::new("/usr/bin/wordcraft")), Some(other.as_os_str()))); + } + + /// Paths compare by component: one mount's name can start with another's. + #[test] + fn a_mount_whose_name_starts_the_same_is_another_mount() { + let tmp = Tmp::new("prefix"); + let (other, _) = mount(&tmp.0, ".mount_ab"); + let (_, exe) = mount(&tmp.0, ".mount_abc"); + assert!(!runs_from_image(Some(&exe), Some(other.as_os_str()))); + } + + #[test] + fn anything_unclear_is_not_the_image() { + let tmp = Tmp::new("unclear"); + let (dir, exe) = mount(&tmp.0, ".mount_WordCr"); + // No `APPDIR`, one that's gone (the other program has exited) or empty. + assert!(!runs_from_image(Some(&exe), None)); + assert!(!runs_from_image(Some(&exe), Some(tmp.0.join("gone").as_os_str()))); + assert!(!runs_from_image(Some(&exe), Some("".as_ref()))); + // The root, and a relative path that happens to lead here. + assert!(!runs_from_image(Some(&exe), Some("/".as_ref()))); + assert!(!runs_from_image(Some(&exe), Some(".".as_ref()))); + // No executable path at all. + assert!(!runs_from_image(None, Some(dir.as_os_str()))); + } +} diff --git a/apps/wordcraft/src/apple_events.rs b/apps/wordcraft/src/apple_events.rs index e79d923a9a..56a284c0cb 100644 --- a/apps/wordcraft/src/apple_events.rs +++ b/apps/wordcraft/src/apple_events.rs @@ -36,14 +36,15 @@ impl AppleEvents { /// Open the documents and act on the quit requests that arrived since the last frame. A document /// opens like File › Open: beside the open ones, in place of an untouched blank one, or, when it -/// is already open, by showing it. Quitting closes the window, which asks about every document +/// is already open, by showing it; a Word template starts a new document based on it, as Word's +/// default verb for one does (#3433). Quitting closes the window, which asks about every document /// with unsaved changes. pub fn poll(inbox: &Inbox, app: &mut WordApp, ctx: &egui::Context) { for e in inbox.drain() { match e { Event::Open(paths) => { for p in paths.iter().filter_map(|p| p.to_str()) { - match app.open_file(p, None) { + match app.open_from_platform(p, None) { // Leave Backstage, as opening from the Open dialog does. Ok(_) => app.ui.backstage = false, Err(e) => { diff --git a/apps/wordcraft/src/main.rs b/apps/wordcraft/src/main.rs index 333cc9a4b0..9f497ab7dd 100644 --- a/apps/wordcraft/src/main.rs +++ b/apps/wordcraft/src/main.rs @@ -10,6 +10,8 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod animations; +#[cfg(target_os = "linux")] +mod appimage_env; #[cfg(target_os = "macos")] mod apple_events; mod clipboard; @@ -67,6 +69,8 @@ impl eframe::App for App { if let Some((pos, size)) = self.1.and_then(|g| g.fit_on_monitor(&window)) { ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(size)); ctx.send_viewport_cmd(egui::ViewportCommand::OuterPosition(pos)); + let b = self.0.window_builder().clone().with_inner_size(size); + self.0.set_window_builder(b); } // The geometry saved for the next start is the active window's. let prev = self.0.ui.window; @@ -336,6 +340,7 @@ fn services(file_dialog: file_dialogs::Hook, ctx: &egui::Context) -> Services { file_dialog: Some(file_dialog), printers: Some(print::discover()), print_native: Some(print::submit()), + cancel_print: Some(print::cancel()), clipboard_picture: Some(Box::new(clipboard::picture)), make_default_app: default_apps::hook(), // Personal templates live in `Templates` in the settings folder (#2852), made on first save. @@ -420,14 +425,15 @@ fn without_launch_args(args: Vec) -> Vec { /// Open a file whose name isn't valid Unicode (#1440). `file.open` takes its path as text, so it /// gets the file's bytes under the lossy name, and the document then keeps the real path to save to. -/// The bytes are read as `file.open` reads a path: only a regular file, and at most 2 GB. +/// The bytes are read as `file.open` reads a path: only a regular file, and at most 2 GB. A Word +/// template starts a new document instead ([`WordApp::open_from_platform`]), which has no path. fn open_non_unicode(app: &mut WordApp, path: &std::path::Path) { let name = path.to_string_lossy(); let opened = wordcraft_engine::io::read_document(path) - .and_then(|bytes| app.run("file.open", serde_json::json!({"path": name, "data": wordcraft_engine::cmd::insert::base64_encode(&bytes)}))); + .and_then(|bytes| app.open_from_platform(&name, Some(&wordcraft_engine::cmd::insert::base64_encode(&bytes)))); match opened { // Not when it waits for a password: that open names the document itself. - Ok(r) if r.get("pending").is_none() => app.session.path = Some(path.into()), + Ok(r) if r.get("pending").is_none() && !wordcraft_ui_egui::documents::is_word_template(&name) => app.session.path = Some(path.into()), Ok(_) => {} Err(e) => { app.report_open_failure(&name, &e); @@ -439,14 +445,31 @@ fn open_non_unicode(app: &mut WordApp, path: &std::path::Path) { /// geometry, opens requested documents or the Start screen, starts the control server when /// asked, and runs the eframe event loop. fn main() -> eframe::Result { + #[cfg(windows)] + if let Some(code) = print::isolated::helper_mode() { + std::process::exit(code); + } // Windows: the update helper is a copy of this program started with `--finish-update`. #[cfg(windows)] if let Some(result) = craft_update::self_update::finish(&wordcraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::>()) { std::process::exit(i32::from(result.is_err())); } + // Linux: before anything reads another program's AppImage variables, or a thread starts (#5653). + #[cfg(target_os = "linux")] + let start_over_failed = appimage_env::drop_inherited(); + // Still carrying them, this process must never update itself: the update would replace the + // other program's file. + #[cfg(target_os = "linux")] + let updates_unsafe = start_over_failed.is_some(); + #[cfg(not(target_os = "linux"))] + let updates_unsafe = false; // First, so the panic hook and every start-up warning are recorded (`logging`). let logger = logging::install(); logging::install_panic_hook(); + #[cfg(target_os = "linux")] + if let Some(e) = start_over_failed { + log::error!("couldn't start again without another program's AppImage variables, so the self-update is off in this window: {e}"); + } let mut control_port: Option = std::env::var("WORDCRAFT_CONTROL_PORT").ok().and_then(|p| p.parse().ok()); let mut files = Vec::new(); let mut sample = false; @@ -561,6 +584,10 @@ fn main() -> eframe::Result { let mut app = WordApp::new(session, services(file_dialog, &cc.egui_ctx)); app.set_startup_update_check(true); load_prefs(&mut app); + // After the preferences, so the reason it gives is in the interface language. + if updates_unsafe { + app.disable_self_update(); + } load_user_dictionary(&mut app); // Language packs from the suite's font store; an Install click is the person's own. wordcraft_engine::packs::use_font_store(true); @@ -600,8 +627,9 @@ fn main() -> eframe::Result { if files.is_empty() && other_files.is_empty() && !sample && control_port.is_none() { app.start_screen(); } + // A Word template named here starts a new document based on it (#3433). for f in files { - if let Err(e) = app.run("file.open", serde_json::json!({"path": f})) { + if let Err(e) = app.open_from_platform(&f, None) { app.report_open_failure(&f, &e); } } diff --git a/apps/wordcraft/src/print.rs b/apps/wordcraft/src/print.rs index 5e8489b71a..9465c4485c 100644 --- a/apps/wordcraft/src/print.rs +++ b/apps/wordcraft/src/print.rs @@ -1,13 +1,21 @@ //! Desktop spooler adapter. Discovery and submission never run on the UI thread. +#[cfg(any(windows, test))] +pub(crate) mod isolated; + use wordcraft_ui_egui::printing::{Discover, Duplex, Printer, Settings, Submit}; /// Returns a discovery callback that enumerates spooler printers on a named worker thread and /// sends the list through a channel, keeping spooler calls off the UI thread. +/// +/// The Print panel needs only names and the default, so the list comes from +/// `craft_print::printer_queues`, which asks no printer driver anything: on Windows a networked +/// printer in power save can hold a driver's capability query for a long time, and the list must +/// not wait on it (#5710). Submission still checks the job's options with the driver. pub fn discover() -> Discover { Box::new(|| { let (sender, receiver) = std::sync::mpsc::channel(); let _ = std::thread::Builder::new().name("print-discovery".into()).spawn(move || { - let printers = craft_print::printers() + let printers = craft_print::printer_queues() .into_iter() .map(|printer| Printer { name: printer.name, label: printer.description, default: printer.default }) .collect(); @@ -17,22 +25,59 @@ pub fn discover() -> Discover { }) } +/// A submission worker and how to stop its job: `true` when it will never be sent, `false` when +/// it was already being sent. +struct Job { + worker: std::thread::JoinHandle<()>, + cancel: Box bool + Send>, + // Acknowledged helper cancellation permits joining it to reap the process and its input PDF. + isolated: bool, +} + // Keep submission workers reachable until normal app shutdown; dropping a JoinHandle would // let process exit end an in-process Windows job or the CUPS hand-off before the spooler has it. -static JOBS: std::sync::Mutex>> = std::sync::Mutex::new(Vec::new()); +static JOBS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); +fn jobs() -> std::sync::MutexGuard<'static, Vec> { + JOBS.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +/// Waits for the jobs already being sent, so the spooler has them before the process exits. A +/// job still waiting (on a sleeping printer's driver, say: #5710) is cancelled instead, so it is +/// never sent and quitting doesn't wait on the driver. pub fn finish_pending_jobs() { - let workers = std::mem::take(&mut *JOBS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)); - for worker in workers { - if worker.join().is_err() { + let pending = std::mem::take(&mut *jobs()); + // Cancel them all first: a job left waiting while another is joined could start sending. + let finishing: Vec<_> = pending + .into_iter() + .filter(|job| { + let cancelled = (job.cancel)(); + job.isolated || !cancelled + }) + .collect(); + for job in finishing { + if job.worker.join().is_err() { log::error!("the print worker stopped unexpectedly during shutdown"); } } } +/// The Print page's Cancel: stops the job being submitted (the Print page submits one at a +/// time, the latest) unless it has started reaching the spooler. `true` when it will never be +/// sent. A job that has finished sending answers `false`, so its receipt is still shown. +pub fn cancel() -> Box bool> { + Box::new(|| jobs().last().is_none_or(|job| (job.cancel)())) +} + +/// Submit off the UI thread, isolating Windows drivers while retaining CUPS token cancellation. pub fn submit() -> Submit { Box::new(|bytes, settings: Settings, collate, options| { let (sender, receiver) = std::sync::mpsc::channel(); + #[cfg(not(windows))] + let cancel = craft_print::Cancel::new(); + #[cfg(windows)] + let cancel = isolated::Submission::new(); + let token = cancel.clone(); let worker = std::thread::Builder::new().name("print-submit".into()).spawn(move || { let job = craft_print::Job { printer: Some(settings.printer), @@ -48,12 +93,16 @@ pub fn submit() -> Submit { title: "WordCraft document".into(), options, }; - let _ = sender.send(craft_print::submit(&bytes, &job).map_err(|error| error.to_string())); + #[cfg(not(windows))] + let sent = craft_print::submit_cancellable(&bytes, &job, &mut |_, _| {}, &token).map_err(|error| error.to_string()); + #[cfg(windows)] + let sent = token.submit(&bytes, &job); + let _ = sender.send(sent); }); if let Ok(worker) = worker { - let mut jobs = JOBS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - jobs.retain(|job| !job.is_finished()); - jobs.push(worker); + let mut jobs = jobs(); + jobs.retain(|job| !job.worker.is_finished()); + jobs.push(Job { worker, cancel: Box::new(move || cancel.cancel()), isolated: cfg!(windows) }); } receiver }) @@ -62,20 +111,67 @@ pub fn submit() -> Submit { #[cfg(test)] mod tests { use super::*; - #[test] - fn shutdown_waits_for_pending_submission_workers() { + /// A worker that waits until released, then reports that it finished. + fn worker() -> (std::thread::JoinHandle<()>, std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>) { let (release, wait) = std::sync::mpsc::channel(); let (completed, completion) = std::sync::mpsc::channel(); let worker = std::thread::spawn(move || { - wait.recv().unwrap(); - completed.send(()).unwrap(); + let _ = wait.recv(); + let _ = completed.send(()); }); - JOBS.lock().unwrap().push(worker); - let shutdown = std::thread::spawn(finish_pending_jobs); - assert!(completion.try_recv().is_err(), "the print worker is still pending"); - release.send(()).unwrap(); - shutdown.join().unwrap(); - completion.recv().unwrap(); - assert!(JOBS.lock().unwrap().is_empty(), "workers were joined before shutdown returned"); + (worker, release, completion) + } + + /// A cancel action that records when it was asked and answers `stops`. + fn asked(stops: bool) -> (Box bool + Send>, std::sync::Arc) { + let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let set = flag.clone(); + let cancel = Box::new(move || { + set.store(true, std::sync::atomic::Ordering::SeqCst); + stops + }); + (cancel, flag) + } + + /// Shutdown waits for a job already being sent, so the spooler has it before the process + /// exits, but not for one still waiting on a driver (a sleeping printer, #5710): that one is + /// cancelled, so it is never sent, and quitting doesn't hang on it. Every job is cancelled + /// before any is waited for, so a waiting job can't start sending meanwhile. The Print page's + /// Cancel asks only the latest job. One test, because JOBS is global. + #[test] + fn shutdown_waits_for_jobs_being_sent_and_cancels_the_rest() { + let (sending, release_sending, sent) = worker(); + let (stuck, release_stuck, _) = worker(); + let (stop_sending, sending_asked) = asked(false); + let (stop_stuck, cancelled) = asked(true); + // The job being sent comes first, so a one-at-a-time shutdown would join it before + // cancelling the waiting one. + *JOBS.lock().unwrap() = + vec![Job { worker: sending, cancel: stop_sending, isolated: false }, Job { worker: stuck, cancel: stop_stuck, isolated: false }]; + // Cancel from the Print page asks the latest job only. + assert!(cancel()(), "the latest job was still waiting: never sent"); + assert!(!sending_asked.load(std::sync::atomic::Ordering::SeqCst), "an earlier job isn't the one being submitted"); + cancelled.store(false, std::sync::atomic::Ordering::SeqCst); + let (finished, shutdown) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + finish_pending_jobs(); + let _ = finished.send(()); + }); + let wait = std::time::Duration::from_millis(200); + assert!(shutdown.recv_timeout(wait).is_err(), "shutdown waits for the job being sent"); + assert!(cancelled.load(std::sync::atomic::Ordering::SeqCst), "the waiting job was cancelled before the wait"); + release_sending.send(()).unwrap(); + assert!(shutdown.recv_timeout(std::time::Duration::from_secs(10)).is_ok(), "shutdown doesn't wait for the stuck job"); + sent.recv().unwrap(); + assert!(JOBS.lock().unwrap().is_empty(), "shutdown took every job"); + // A job that has already been sent answers false, so the Print page keeps its receipt. + let (done, release_done, finished) = worker(); + release_done.send(()).unwrap(); + finished.recv().unwrap(); + *JOBS.lock().unwrap() = vec![Job { worker: done, cancel: Box::new(|| false), isolated: false }]; + assert!(!cancel()(), "a sent job isn't reported as cancelled"); + JOBS.lock().unwrap().clear(); + // The stuck worker was left behind without blocking shutdown; let it end. + release_stuck.send(()).unwrap(); } } diff --git a/apps/wordcraft/src/print/isolated.rs b/apps/wordcraft/src/print/isolated.rs new file mode 100644 index 0000000000..b2463b5d92 --- /dev/null +++ b/apps/wordcraft/src/print/isolated.rs @@ -0,0 +1,592 @@ +//! One Windows driver submission per process: a cancelled driver cannot retain another +//! job's provider lock. The child acknowledges its Cancel token before it exits. + +use std::{ + io::{BufRead, BufReader, Read, Write}, + path::Path, + process::{Child, ChildStdin, Command, Stdio}, + sync::{Arc, Condvar, Mutex}, + time::Duration, +}; + +use serde::{Deserialize, Serialize}; + +#[cfg(windows)] +const FLAG: &str = "--wordcraft-print-worker"; +const PREFIX: &str = "WORDCRAFT_PRINT:"; +const MAX_MESSAGE: u64 = 64 * 1024; +const MAX_PDF: u64 = 512 * 1024 * 1024; +const CANCELLED: &str = "the print job was cancelled before anything was sent to the printer"; + +#[derive(Serialize, Deserialize)] +enum Duplex { + Off, + LongEdge, + ShortEdge, +} + +#[derive(Serialize, Deserialize)] +struct Request { + printer: Option, + copies: u32, + collate: bool, + duplex: Option, + grayscale: bool, + shrink: bool, + title: String, + options: Vec<(String, String)>, +} + +impl Request { + /// Preserve all spooler options in a platform-independent wire representation. + fn from_job(job: &craft_print::Job) -> Self { + Self { + printer: job.printer.clone(), + copies: job.copies, + collate: job.collate, + duplex: job.duplex.map(|d| match d { + craft_print::Duplex::Off => Duplex::Off, + craft_print::Duplex::LongEdge => Duplex::LongEdge, + craft_print::Duplex::ShortEdge => Duplex::ShortEdge, + }), + grayscale: job.grayscale, + shrink: job.scaling == craft_print::Scaling::ShrinkToFit, + title: job.title.clone(), + options: job.options.clone(), + } + } + + /// Restore the job without substituting printer defaults or changing copy semantics. + fn job(self) -> craft_print::Job { + craft_print::Job { + printer: self.printer, + copies: self.copies, + collate: self.collate, + duplex: self.duplex.map(|d| match d { + Duplex::Off => craft_print::Duplex::Off, + Duplex::LongEdge => craft_print::Duplex::LongEdge, + Duplex::ShortEdge => craft_print::Duplex::ShortEdge, + }), + grayscale: self.grayscale, + scaling: if self.shrink { craft_print::Scaling::ShrinkToFit } else { craft_print::Scaling::Exact }, + title: self.title, + options: self.options, + } + } +} + +#[derive(Serialize, Deserialize)] +enum Event { + Ready, + Cancelled, + Sending, + Done(Result), +} + +enum State { + Starting, + Waking(Arc>), + Running(ChildStdin), + Sending(ChildStdin), + Cancelled, + Finished, +} + +#[derive(Clone)] +pub(crate) struct Submission(Arc<(Mutex, Condvar)>); + +impl Submission { + /// Create one submission whose driver cannot run until the parent grants startup. + pub(crate) fn new() -> Self { + Self(Arc::new((Mutex::new(State::Starting), Condvar::new()))) + } + + /// Recover the lifecycle state after poisoning without invoking another driver call. + fn state(&self) -> std::sync::MutexGuard<'_, State> { + self.0.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner) + } + + /// Only an acknowledgement of the child's token can cancel a running helper. A timeout + /// is a refusal, never permission to kill a process that may already be sending. + pub(crate) fn cancel(&self) -> bool { + let mut state = self.state(); + match &mut *state { + State::Starting => { + *state = State::Cancelled; + return true; + } + State::Waking(child) => { + let _ = child.lock().unwrap_or_else(std::sync::PoisonError::into_inner).kill(); + *state = State::Cancelled; + return true; + } + State::Cancelled => return true, + State::Finished | State::Sending(_) => return false, + State::Running(input) => { + if input.write_all(b"cancel\n").and_then(|_| input.flush()).is_err() { + return false; + } + } + } + let (state, _) = self + .0 + .1 + .wait_timeout_while(state, Duration::from_secs(1), |s| matches!(s, State::Running(_))) + .unwrap_or_else(std::sync::PoisonError::into_inner); + matches!(*state, State::Cancelled) + } + + #[cfg(windows)] + /// Submit through this executable's private helper mode and await its final receipt. + pub(crate) fn submit(&self, pdf: &[u8], job: &craft_print::Job) -> Result { + self.submit_with(pdf, job, |directory| { + let mut command = Command::new(std::env::current_exe()?); + command.arg(FLAG).arg(directory); + Ok(command) + }) + } + + /// Publish a terminal state on every result, waking a cancellation caller awaiting an ack. + fn submit_with(&self, pdf: &[u8], job: &craft_print::Job, command: impl FnOnce(&Path) -> std::io::Result) -> Result { + let result = self.run(pdf, job, command); + let mut state = self.state(); + if !matches!(*state, State::Cancelled) { + *state = State::Finished; + } + self.0.1.notify_all(); + if matches!(*state, State::Cancelled) { Err(CANCELLED.into()) } else { result } + } + + /// Own the private inputs and child until receipt, acknowledged cancellation or failure. + fn run(&self, pdf: &[u8], job: &craft_print::Job, command: impl FnOnce(&Path) -> std::io::Result) -> Result { + if matches!(*self.state(), State::Cancelled) { + return Err(CANCELLED.into()); + } + if pdf.len() as u64 > MAX_PDF { + return Err("the print PDF exceeds the 512 MiB helper limit".into()); + } + let request = serde_json::to_vec(&Request::from_job(job)).map_err(error)?; + if request.len() as u64 > MAX_MESSAGE { + return Err("the print settings exceed the helper message limit".into()); + } + let directory = tempfile::tempdir().map_err(error)?; + std::fs::write(directory.path().join("job.json"), request).map_err(error)?; + std::fs::write(directory.path().join("input.pdf"), pdf).map_err(error)?; + let child = + command(directory.path()).map_err(error)?.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(error)?; + let child = Arc::new(Mutex::new(child)); + let outcome = (|| { + let (input, output) = { + let mut process = child.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + (process.stdin.take().ok_or("print helper has no input pipe")?, process.stdout.take().ok_or("print helper has no output pipe")?) + }; + { + let mut state = self.state(); + if matches!(*state, State::Cancelled) { + return Err(CANCELLED.into()); + } + *state = State::Waking(child.clone()); + } + let mut output = BufReader::new(output); + loop { + let message = line(&mut output)?.ok_or("the print helper stopped before it was ready")?; + let Some((_, json)) = message.split_once(PREFIX) else { continue }; + if !matches!(serde_json::from_str::(json).map_err(error)?, Event::Ready) { + return Err("the print helper did not acknowledge startup".into()); + } + break; + } + { + let mut state = self.state(); + if matches!(*state, State::Cancelled) { + return Err(CANCELLED.into()); + } + *state = State::Running(input); + if let State::Running(input) = &mut *state { + input.write_all(b"start\n").and_then(|_| input.flush()).map_err(error)?; + } + } + while let Some(line) = line(&mut output)? { + let Some((_, json)) = line.split_once(PREFIX) else { continue }; + match serde_json::from_str::(json).map_err(error)? { + Event::Ready => return Err("the print helper repeated its startup acknowledgement".into()), + Event::Cancelled => { + *self.state() = State::Cancelled; + self.0.1.notify_all(); + return Err(CANCELLED.into()); + } + Event::Sending => { + let mut state = self.state(); + let previous = std::mem::replace(&mut *state, State::Finished); + *state = match previous { + State::Running(input) | State::Sending(input) => State::Sending(input), + other => other, + }; + self.0.1.notify_all(); + } + Event::Done(result) => return result, + } + } + Err("the print helper stopped without a receipt".into()) + })(); + // A cancelled child has acknowledged that it cannot send. Other outcomes are terminal + // receipts or a broken protocol/pipe; reap the process and its private input files. + if outcome.is_err() { + let _ = child.lock().unwrap_or_else(std::sync::PoisonError::into_inner).kill(); + } + let status = child.lock().unwrap_or_else(std::sync::PoisonError::into_inner).wait().map_err(error)?; + if outcome.is_ok() && !status.success() { + return Err("the print helper failed after its receipt".into()); + } + outcome + } +} + +/// Keep helper transport failures distinct from a printer's own error message. +fn error(error: impl std::fmt::Display) -> String { + format!("print helper: {error}") +} + +/// Read at most one byte beyond the cap so oversized files fail without unbounded allocation. +fn bounded(mut input: impl Read, limit: u64) -> Result, String> { + let mut bytes = Vec::new(); + input.by_ref().take(limit.saturating_add(1)).read_to_end(&mut bytes).map_err(error)?; + if bytes.len() as u64 > limit { Err("print helper input exceeds its limit".into()) } else { Ok(bytes) } +} + +/// Read one bounded UTF-8 protocol record, distinguishing EOF from an oversized message. +fn line(input: &mut impl BufRead) -> Result, String> { + let mut bytes = Vec::new(); + input.take(MAX_MESSAGE + 1).read_until(b'\n', &mut bytes).map_err(error)?; + if bytes.len() as u64 > MAX_MESSAGE { + return Err("print helper message exceeds its limit".into()); + } + if bytes.is_empty() { Ok(None) } else { String::from_utf8(bytes).map(|s| Some(s.trim_end().to_owned())).map_err(error) } +} + +/// Serialize complete events under stdout's lock so the driver and listener cannot interleave. +fn emit(event: &Event) -> Result<(), String> { + let mut output = std::io::stdout().lock(); + write!(output, "{PREFIX}").map_err(error)?; + serde_json::to_writer(&mut output, event).map_err(error)?; + writeln!(output).and_then(|_| output.flush()).map_err(error) +} + +/// Remove both plaintext inputs even when one fails; already-removed inputs count as success. +fn remove_inputs(directory: &Path) -> Result<(), String> { + let mut first_error = None; + for name in ["job.json", "input.pdf"] { + if let Err(failure) = std::fs::remove_file(directory.join(name)) + && failure.kind() != std::io::ErrorKind::NotFound + { + first_error.get_or_insert_with(|| error(format!("could not remove {name}: {failure}"))); + } + } + first_error.map_or(Ok(()), Err) +} + +/// Load and erase the private inputs before driver work, while an independent listener can cancel. +fn helper( + directory: &Path, + cancel_request: impl Fn(&craft_print::Cancel) -> bool + Send + 'static, + submit: impl FnOnce(&[u8], &craft_print::Job, &craft_print::Cancel) -> Result, +) -> Result<(), String> { + let mut input = BufReader::new(std::io::stdin()); + emit(&Event::Ready)?; + if line(&mut input)?.as_deref() != Some("start") { + return Err("print helper requires a start acknowledgement".into()); + } + let cancel = craft_print::Cancel::new(); + let listener = cancel.clone(); + std::thread::Builder::new() + .name("print-cancel".into()) + .spawn(move || { + loop { + match line(&mut input) { + Ok(Some(command)) if command == "cancel" => { + if cancel_request(&listener) { + let _ = emit(&Event::Cancelled); + std::process::exit(0); + } + if emit(&Event::Sending).is_err() { + break; + } + } + _ => break, + } + } + // If the parent disappears, never leave a waiting driver process behind. Sending + // jobs finish normally so the OS gets their pages even if the UI has gone away. + if cancel_request(&listener) { + std::process::exit(0); + } + }) + .map_err(error)?; + let request: Request = + serde_json::from_slice(&bounded(std::fs::File::open(directory.join("job.json")).map_err(error)?, MAX_MESSAGE)?).map_err(error)?; + let pdf = bounded(std::fs::File::open(directory.join("input.pdf")).map_err(error)?, MAX_PDF)?; + remove_inputs(directory)?; + let result = submit(&pdf, &request.job(), &cancel); + emit(&Event::Done(result)) +} + +#[cfg(windows)] +/// Handle only the private first-argument flag, before logging or GUI initialization. +pub(crate) fn helper_mode() -> Option { + let mut args = std::env::args_os().skip(1); + if args.next().as_deref() != Some(std::ffi::OsStr::new(FLAG)) { + return None; + } + let result = args.next().ok_or_else(|| "print helper directory missing".to_owned()).and_then(|dir| { + helper(Path::new(&dir), craft_print::Cancel::cancel, |pdf, job, cancel| { + craft_print::submit_cancellable(pdf, job, &mut |_, _| {}, cancel).map_err(error) + }) + }); + if let Err(message) = &result { + let _ = emit(&Event::Done(Err(message.clone()))); + } + Some(i32::from(result.is_err())) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A real test child exercises production pipes, token handling and process cleanup. + #[test] + #[ignore = "launched by the process protocol tests"] + fn helper_process() { + let Some(directory) = std::env::var_os("WORDCRAFT_TEST_PRINT_DIR") else { return }; + if std::env::var("WORDCRAFT_TEST_PRINT_MODE").as_deref() == Ok("waking") { + std::fs::write(Path::new(&directory).join("waking"), b"starting").unwrap(); + loop { + std::thread::park(); + } + } + let result = helper( + Path::new(&directory), + |token| { + if std::env::var("WORDCRAFT_TEST_PRINT_MODE").as_deref() == Ok("sending") { false } else { token.cancel() } + }, + |pdf, job, _token| { + assert_eq!(pdf, b"synthetic PDF"); + assert_eq!(job.printer.as_deref(), Some("test queue")); + match std::env::var("WORDCRAFT_TEST_PRINT_MODE").as_deref() { + Ok("held") => { + let _provider = Mutex::new(()); + let _held = _provider.lock().unwrap(); + std::fs::write(Path::new(&directory).join("held"), b"driver entered").unwrap(); + loop { + std::thread::park(); + } + } + Ok("crash") => std::process::exit(7), + Ok("sending") => { + std::fs::write(Path::new(&directory).join("sending"), b"sending").unwrap(); + while !Path::new(&directory).join("release").exists() { + std::thread::sleep(Duration::from_millis(5)); + } + Ok("spooled synthetic job".into()) + } + _ => Ok("spooled synthetic job".into()), + } + }, + ); + std::process::exit(i32::from(result.is_err())); + } + + /// Launch just the ignored helper entry, without invoking printer hardware. + fn command(directory: &Path, mode: &str) -> std::io::Result { + let mut command = Command::new(std::env::current_exe()?); + let name = module_path!().split_once("::").map_or(module_path!(), |(_, name)| name); + command.args(["--ignored", "--exact", &format!("{name}::helper_process"), "--nocapture"]); + command.env("WORDCRAFT_TEST_PRINT_DIR", directory).env("WORDCRAFT_TEST_PRINT_MODE", mode); + Ok(command) + } + + /// Give the child a literal printer identity to verify transport. + fn job() -> craft_print::Job { + craft_print::Job { printer: Some("test queue".into()), ..Default::default() } + } + + #[test] + /// Cancelling before submission cannot launch a driver process. + fn cancelled_start_never_launches_a_helper() { + let submission = Submission::new(); + assert!(submission.cancel()); + let result = submission.submit_with(b"synthetic PDF", &job(), |_| Err(std::io::Error::other("must not launch"))); + assert_eq!(result, Err(CANCELLED.into())); + } + + #[test] + /// A cancellation during creation withholds the start grant and removes private inputs. + fn cancellation_during_startup_never_starts_the_driver() { + let submission = Submission::new(); + let worker = submission.clone(); + let (started, starting) = std::sync::mpsc::channel(); + let (release, wait) = std::sync::mpsc::channel(); + let running = std::thread::spawn(move || { + worker.submit_with(b"synthetic PDF", &job(), |directory| { + started.send(directory.to_path_buf()).unwrap(); + wait.recv().unwrap(); + command(directory, "held") + }) + }); + let directory = starting.recv_timeout(Duration::from_secs(10)).unwrap(); + assert!(submission.cancel()); + release.send(()).unwrap(); + assert_eq!(running.join().unwrap(), Err(CANCELLED.into())); + assert!(!directory.exists(), "startup cancellation reaps the child and removes its inputs"); + } + + #[test] + /// A hung startup has no permission to send and can be killed without a token ack. + fn an_unresponsive_starting_process_can_be_stopped_without_an_acknowledgement() { + let submission = Submission::new(); + let worker = submission.clone(); + let (path_tx, path_rx) = std::sync::mpsc::channel(); + let running = std::thread::spawn(move || { + worker.submit_with(b"synthetic PDF", &job(), |dir| { + path_tx.send(dir.to_path_buf()).unwrap(); + command(dir, "waking") + }) + }); + let directory = path_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(10); + while !directory.join("waking").exists() && std::time::Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(5)); + } + assert!(directory.join("waking").exists()); + assert!(submission.cancel(), "a process without a start grant cannot send anything"); + assert_eq!(running.join().unwrap(), Err(CANCELLED.into())); + assert!(!directory.exists()); + } + + #[test] + /// Job transport preserves every explicit setting, including Unicode and duplex direction. + fn job_options_survive_the_process_boundary() { + let expected = craft_print::Job { + printer: Some("Office λ".into()), + copies: 3, + collate: false, + duplex: Some(craft_print::Duplex::ShortEdge), + grayscale: true, + scaling: craft_print::Scaling::ShrinkToFit, + title: "A document".into(), + options: vec![("media".into(), "iso_a4_210x297mm".into())], + }; + let bytes = serde_json::to_vec(&Request::from_job(&expected)).unwrap(); + let received: Request = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(received.job(), expected); + } + + #[test] + /// Retry succeeds immediately after cancellation, before joining the old worker. + fn cancelled_driver_does_not_block_the_next_process() { + let submission = Submission::new(); + let worker = submission.clone(); + let (path_tx, path_rx) = std::sync::mpsc::channel(); + let running = std::thread::spawn(move || { + worker.submit_with(b"synthetic PDF", &job(), |dir| { + path_tx.send(dir.to_path_buf()).unwrap(); + command(dir, "held") + }) + }); + let directory = path_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(10); + while !directory.join("held").exists() && std::time::Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(5)); + } + assert!(directory.join("held").exists(), "the child is held inside its driver"); + assert!(!directory.join("input.pdf").exists(), "the stalled driver retains no plaintext PDF"); + assert!(!directory.join("job.json").exists(), "loaded settings were erased before driver work"); + assert!(submission.cancel(), "a stalled driver still acknowledges cancellation"); + let next = Submission::new(); + assert_eq!(next.submit_with(b"synthetic PDF", &job(), |dir| command(dir, "ready")), Ok("spooled synthetic job".into())); + assert_eq!(running.join().unwrap(), Err(CANCELLED.into())); + assert!(!directory.exists(), "the reaped helper's PDF and settings were removed"); + assert!(!next.cancel(), "a completed job keeps its receipt"); + } + + #[test] + /// A refused cancellation keeps the sending process alive until its receipt. + fn cancellation_refusal_preserves_the_sending_job() { + let submission = Submission::new(); + let worker = submission.clone(); + let (path_tx, path_rx) = std::sync::mpsc::channel(); + let running = std::thread::spawn(move || { + worker.submit_with(b"synthetic PDF", &job(), |dir| { + path_tx.send(dir.to_path_buf()).unwrap(); + command(dir, "sending") + }) + }); + let directory = path_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(10); + while !directory.join("sending").exists() && std::time::Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(5)); + } + assert!(directory.join("sending").exists()); + assert!(!submission.cancel(), "a sending job refuses cancellation"); + assert!(matches!(*submission.state(), State::Sending(_))); + assert!(!running.is_finished(), "the child is retained until it finishes sending"); + std::fs::write(directory.join("release"), b"continue").unwrap(); + assert_eq!(running.join().unwrap(), Ok("spooled synthetic job".into())); + assert!(!directory.exists()); + } + + #[test] + /// Closing the parent's pipe stops a waiting driver and permits parent-side cleanup. + fn parent_disconnect_stops_a_waiting_driver() { + let submission = Submission::new(); + let worker = submission.clone(); + let (path_tx, path_rx) = std::sync::mpsc::channel(); + let running = std::thread::spawn(move || { + worker.submit_with(b"synthetic PDF", &job(), |dir| { + path_tx.send(dir.to_path_buf()).unwrap(); + command(dir, "held") + }) + }); + let directory = path_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(10); + while !directory.join("held").exists() && std::time::Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(5)); + } + assert!(directory.join("held").exists()); + *submission.state() = State::Finished; // Drops the parent's pipe as on process exit. + assert_eq!(running.join().unwrap(), Err("the print helper stopped without a receipt".into())); + assert!(!directory.exists()); + } + + #[test] + /// Helper failure removes its input directory; input limits reject oversize data. + fn helper_crashes_are_errors_and_inputs_are_bounded() { + let submission = Submission::new(); + let mut inputs = None; + assert_eq!( + submission.submit_with(b"synthetic PDF", &job(), |dir| { + inputs = Some(dir.to_path_buf()); + command(dir, "crash") + }), + Err("the print helper stopped without a receipt".into()) + ); + assert!(!inputs.unwrap().exists(), "a helper crash removes the PDF and settings"); + assert_eq!(bounded(&b"abcd"[..], 3), Err("print helper input exceeds its limit".into())); + assert_eq!(bounded(&b"abc"[..], 3), Ok(b"abc".to_vec())); + } + + /// Cleanup is repeatable and reports real failures while still attempting both inputs. + #[test] + fn input_cleanup_tolerates_missing_files_and_reports_failures() { + let directory = tempfile::tempdir().unwrap(); + std::fs::write(directory.path().join("job.json"), b"settings").unwrap(); + std::fs::write(directory.path().join("input.pdf"), b"private PDF").unwrap(); + assert_eq!(remove_inputs(directory.path()), Ok(())); + assert_eq!(remove_inputs(directory.path()), Ok(())); + assert!(!directory.path().join("input.pdf").exists()); + std::fs::create_dir(directory.path().join("job.json")).unwrap(); + std::fs::write(directory.path().join("input.pdf"), b"private PDF").unwrap(); + let error = remove_inputs(directory.path()).unwrap_err(); + assert!(error.contains("could not remove job.json"), "{error}"); + assert!(!directory.path().join("input.pdf").exists(), "a settings cleanup failure does not retain the PDF"); + } +} diff --git a/apps/wordcraft/tests/it/appimage_env.rs b/apps/wordcraft/tests/it/appimage_env.rs new file mode 100644 index 0000000000..e1de2f0040 --- /dev/null +++ b/apps/wordcraft/tests/it/appimage_env.rs @@ -0,0 +1,99 @@ +//! A WordCraft started by an AppImage app inherits that app's `APPIMAGE`, which the self-update +//! would take for this copy's file and replace (#5653). The real program, started both ways. + +use std::ffi::OsString; +use std::os::unix::ffi::OsStringExt; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output, Stdio}; +use std::time::{Duration, Instant}; + +const NOTICE: &str = "ignoring the AppImage"; + +/// A folder of its own for one test, removed when it ends. +struct Tmp(PathBuf); + +impl Tmp { + /// An empty folder named after `test` and this process, made afresh. + fn new(test: &str) -> Tmp { + let dir = std::env::temp_dir().join(format!("wordcraft-appimage-it-{test}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + Tmp(dir) + } +} + +impl Drop for Tmp { + /// Removes the folder and what the test left in it. + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +/// Runs the real app with `args`, as an AppImage app with `appimage` mounted at `appdir` would +/// start it, and waits up to 30 s for it to exit (with no display it can't open a window). +fn app(args: &[OsString], appimage: &Path, appdir: &Path) -> Output { + let mut child = Command::new(env!("CARGO_BIN_EXE_wordcraft")) + .args(args) + .env("APPIMAGE", appimage) + .env("APPDIR", appdir) + .env("ARGV0", "other-app") + .env("OWD", "/") + // With no display, an app that tries to open a window fails at once instead. + .env_remove("DISPLAY") + .env_remove("WAYLAND_DISPLAY") + .env_remove("WAYLAND_SOCKET") + .env_remove("WORDCRAFT_CONTROL_PORT") + .env("WORDCRAFT_NO_PREFS", "1") + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + // A start-over that kept the variables would start over for ever, and end here. + let deadline = Instant::now() + Duration::from_secs(30); + while child.try_wait().unwrap().is_none() { + if Instant::now() > deadline { + let _ = child.kill(); + panic!("wordcraft {args:?} was still running after 30 s"); + } + std::thread::sleep(Duration::from_millis(50)); + } + child.wait_with_output().unwrap() +} + +/// The notice comes once: the program it started over as no longer sees the variables. +#[test] +fn another_programs_variables_are_dropped_once_and_the_program_carries_on() { + let tmp = Tmp::new("dropped"); + let other = tmp.0.join("other-app.AppImage"); + std::fs::write(&other, b"another program").unwrap(); + let out = app(&["--version".into()], &other, &tmp.0); + let (stdout, stderr) = (String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(out.status.success(), "status {:?}, stderr: {stderr}", out.status); + assert!(stdout.starts_with("wordcraft "), "stdout: {stdout}"); + assert_eq!(stderr.matches(NOTICE).count(), 1, "stderr: {stderr}"); + assert!(stderr.contains("other-app.AppImage"), "stderr: {stderr}"); + assert_eq!(std::fs::read(&other).unwrap(), b"another program"); +} + +/// Arguments reach the program it starts over as byte for byte, even one that isn't Unicode. +#[test] +fn starting_over_keeps_the_arguments() { + let tmp = Tmp::new("args"); + let out = app(&["--sample".into(), "--control".into(), OsString::from_vec(b"79\xe981".to_vec())], &tmp.0.join("gone"), &tmp.0); + let stderr = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(2), "stderr: {stderr}"); + assert!(stderr.contains("argument 3 is not valid Unicode: 79\u{FFFD}81"), "stderr: {stderr}"); +} + +/// Its own image's variables stay, or the self-update couldn't find the file to replace. +#[test] +fn its_own_images_variables_stay() { + let exe = Path::new(env!("CARGO_BIN_EXE_wordcraft")).canonicalize().unwrap(); + let mount = exe.parent().unwrap(); + let out = app(&["--version".into()], &mount.join("WordCraft.AppImage"), mount); + let (stdout, stderr) = (String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); + assert!(out.status.success(), "status {:?}, stderr: {stderr}", out.status); + assert!(stdout.starts_with("wordcraft "), "stdout: {stdout}"); + assert!(!stderr.contains(NOTICE), "stderr: {stderr}"); +} diff --git a/apps/wordcraft/tests/it/main.rs b/apps/wordcraft/tests/it/main.rs index 9c26dc905b..cfa99b937d 100644 --- a/apps/wordcraft/tests/it/main.rs +++ b/apps/wordcraft/tests/it/main.rs @@ -1,3 +1,5 @@ +#[cfg(target_os = "linux")] +mod appimage_env; mod control_arg; mod help; #[cfg(unix)] diff --git a/contributors/people.toml b/contributors/people.toml index 04fc37ec2f..1f5825f1ad 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -14,6 +14,10 @@ # To remove your names, delete your entry (or set fields empty) in a PR. The next # contributors.py run, and the next build, drop them. +[people.SenuraMalaka] +real_name = "Senura Dissanayake" +sync_github_name = true + [people.divyanshu2074] real_name = "Divyanshu Gupta" sync_github_name = true @@ -197,3 +201,6 @@ display_name = "entropybeater" [people.helenkwok] real_name = "Helen Kwok" + +[people.ashokram-si] +real_name = "Ashok Ram" diff --git a/craft-i18n.rev b/craft-i18n.rev index b223714530..3bd45d9a0d 100644 --- a/craft-i18n.rev +++ b/craft-i18n.rev @@ -1 +1 @@ -9d6d54d6d9ff603cc41e12e5f6ba51af3554ab66 +1e515422afdda2f317af430c1f9d68a5f83935b4 diff --git a/crates/doc/src/cfb_guard.rs b/crates/doc/src/cfb_guard.rs index ec32e4a1a8..3dd790c41f 100644 --- a/crates/doc/src/cfb_guard.rs +++ b/crates/doc/src/cfb_guard.rs @@ -7,8 +7,9 @@ /// makes it allocate gigabytes. Here the DIFAT is walked the way `cfb` walks it (header entries, /// then the DIFAT chain, at most one step per sector; trailing zero and free entries trimmed /// the same way) and the file is refused when it lists one FAT sector twice, or more FAT sectors -/// than its sectors need (one FAT sector covers `sector_len / 4` of them, plus one spare), so the -/// FAT `cfb` builds is about the size the file needs. Anything else that is wrong is left for +/// than the file has sectors — every FAT sector is a sector of the file, so a longer list is +/// duplicates or names `cfb` refuses anyway, and the FAT `cfb` builds (one sector of entries per +/// listed name) stays within about the file's own size. Anything else that is wrong is left for /// `cfb` to report. pub fn check(bytes: &[u8]) -> Result<(), String> { const MAGIC: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1]; @@ -52,12 +53,14 @@ pub fn check(bytes: &[u8]) -> Result<(), String> { while difat.last() == Some(&FREE) { difat.pop(); } - // Each listed FAT sector adds a whole sector of entries. A FAT sector holds the entries of - // `sector_len / 4` sectors, so a file needs no more FAT sectors than that covers (one spare), - // and none of them twice. A free entry in the middle is left for `cfb` to refuse. + // Each listed FAT sector adds a whole sector of entries. A real file lists no more FAT + // sectors than the file has — every FAT sector is a sector of the file, and none of them + // twice (checked below) — so the FAT `cfb` builds stays within about the file's own size. + // The #3642 blowup listed 2048 × 127 names in a ~1 MB file. A tighter bound (one FAT sector + // per `sector_len / 4` sectors plus a spare) refused real files, whose writers reserve FAT + // for growth: an encrypted package of 78 sectors lists 4 and didn't open (#5109). let listed: Vec = difat.into_iter().filter(|&s| s != FREE).collect(); - let needed = num_sectors.div_ceil(sector_len / 4).saturating_add(1); - if listed.len() > needed { + if listed.len() > num_sectors { return Err(format!("the DIFAT lists {} FAT sectors in a file of {num_sectors} sectors", listed.len())); } let mut seen = std::collections::HashSet::with_capacity(listed.len()); @@ -123,10 +126,16 @@ mod tests { } #[test] - fn a_fat_sector_listed_twice_or_more_than_the_file_needs_is_refused() { - // Each list is shorter than the file has sectors, which the guard used to accept. + fn a_fat_sector_listed_twice_or_more_than_the_file_has_is_refused() { + // A duplicate, and a list longer than the file has sectors: every FAT sector is a + // sector of the file, so a longer list is one of the two. assert!(super::check(&header_difat(10, &[0, 0])).is_err_and(|e| e.contains("FAT sector 0 twice"))); - assert!(super::check(&header_difat(10, &[0, 1, 2])).is_err_and(|e| e.contains("3 FAT sectors"))); + assert!( + super::check(&header_difat(10, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).is_err_and(|e| e.contains("11 FAT sectors in a file of 10 sectors")) + ); + // A writer may reserve FAT for growth: a real encrypted package of 78 sectors lists + // 4 FAT sectors and didn't open since the #3642 guard landed (#5109). + assert!(super::check(&header_difat(78, &[0, 1, 2, 3])).is_ok()); assert!(super::check(&header_difat(10, &[0])).is_ok()); assert!(super::check(&header_difat(200, &[0, 1])).is_ok()); } diff --git a/crates/doc/src/edit.rs b/crates/doc/src/edit.rs index 0a869ee70a..fc8fa2bb26 100644 --- a/crates/doc/src/edit.rs +++ b/crates/doc/src/edit.rs @@ -772,6 +772,10 @@ impl Document { if a.path.parent() == b.path.parent() { // Same container: trim the ends, drop the blocks between, join. let (ia, ib) = (a.path.last(), b.path.last()); + // Removing an already-empty paragraph must not impose its style or mark formatting + // on the following content. Test before trimming: deleting actual heading text is + // still an ordinary merge that keeps the first paragraph's properties. + let empty_start = self.para(a.story, &a.path).is_some_and(Paragraph::is_empty); // Kept markers of the paragraphs in between move to the join. let mut moved: Vec<(InlineObject, CharProps)> = Vec::new(); // `kept` is sorted: one walk of each paragraph's objects and runs (#3614). @@ -819,15 +823,18 @@ impl Document { if ib < bl.len() && ia < ib { bl.drain(ia + 1..=ib); } - tail.props = ParaProps::default(); // The deleted marks include `a`'s, so a section break there goes too: the joined // paragraph ends where `b`'s mark did, and its text takes the following section's // setup, as in Word (#2127). let sect = tail.section.take(); let pa = self.para_mut(a.story, &a.path)?; - let keep_props = pa.props.clone(); + let keep_props = if empty_start { tail.props.clone() } else { pa.props.clone() }; + let keep_mark = empty_start.then(|| tail.mark.clone()); pa.append(tail); pa.props = keep_props; + if let Some(mark) = keep_mark { + pa.mark = mark; + } let before = std::mem::replace(&mut pa.section, sect); if a.story == crate::StoryRef::Body && a.path.0.len() == 1 { // Nearest first, as "link to previous" resolved them before the delete. diff --git a/crates/doc/src/kept.rs b/crates/doc/src/kept.rs index ee0d3e9a1a..4f9437972f 100644 --- a/crates/doc/src/kept.rs +++ b/crates/doc/src/kept.rs @@ -478,8 +478,9 @@ fn family_fingerprint(doc: &crate::Document, root: u32, replies: &HashMap u64 { let mut sections: Vec<&crate::SectionProps> = doc.body.iter().filter_map(|b| b.as_para()).filter_map(|p| p.section.as_deref()).collect(); sections.push(&doc.last_section); @@ -490,8 +491,10 @@ pub fn pages_fingerprint(doc: &crate::Document) -> u64 { match serde_json::to_vec(&( sections, parts, + s.even_odd_headers, &s.page_color, &s.page_picture, + s.watermark.as_ref().filter(|w| !w.text.trim().is_empty()), (&s.footnote_format, &s.endnote_format, &s.footnote_pr, &s.endnote_pr), (&doc.footnote_separators, &doc.endnote_separators), )) { diff --git a/crates/doc/src/link.rs b/crates/doc/src/link.rs index 7ceeabb37b..0dc4b17694 100644 --- a/crates/doc/src/link.rs +++ b/crates/doc/src/link.rs @@ -19,6 +19,12 @@ pub fn is_web_or_mail_link(url: &str) -> bool { /// names another machine — following it on Windows leaks the user's NTLM credentials — so it is /// refused too. pub fn is_safe_href(url: &str) -> bool { + // Exporters drop other control characters and noncharacters when they write the link + // (`file\u{85}:` comes out `file:`), so a link holding one is refused, not judged as it + // stands and written as something else. + if url.chars().any(|c| (c.is_control() && !c.is_ascii_control()) || matches!(c, '\u{FFFE}' | '\u{FFFF}')) { + return false; + } let cleaned: String = url.chars().filter(|c| !c.is_ascii_control() && *c != ' ').collect(); // `//host` (protocol-relative) and `\\host` (UNC), with `\` read as `/` the way Windows does. if cleaned.replace('\\', "/").starts_with("//") { @@ -125,6 +131,7 @@ mod tests { ("HYPERLINK\t\"http://example.com\"", Some("http://example.com")), (r#"HYPERLINK"http://example.com""#, Some("http://example.com")), (r#"HYPERLINK "http://example.com/path\latest""#, Some("http://example.com/pathlatest")), + (r#"HYPERLINK "x \* Upper""#, Some("x * Upper")), (r#"HYPERLINK "C:\\Docs\\My file.docx""#, Some(r"C:\Docs\My file.docx")), (r#"HYPERLINK "say \"hi\"""#, Some(r#"say "hi""#)), (r#"HYPERLINK " http://example.com ""#, Some("http://example.com")), @@ -199,6 +206,10 @@ mod tests { "\\\\attacker.example\\share\\x", "/\\attacker.example\\x", " //attacker.example/x", + // Characters the exporters drop on writing don't hide a scheme. + "file\u{85}:///etc/passwd", + "java\u{9F}script:alert(1)", + "/\u{FFFE}/attacker.example/x", "", ] { assert!(!is_safe_href(bad), "{bad:?}"); diff --git a/crates/doc/src/math_edit.rs b/crates/doc/src/math_edit.rs index 9140a73cd1..69708cb787 100644 --- a/crates/doc/src/math_edit.rs +++ b/crates/doc/src/math_edit.rs @@ -769,6 +769,30 @@ pub fn autocorrect(root: &mut Arg, pos: &MathPos) -> Option { Some(MathPos { path: pos.path, off: pos.off - name.chars().count() }) } +/// The character just before the caret is an n-ary operator (∫, ∑…) in plain math text, as +/// AutoCorrect leaves `\int` or `\sum`. +pub fn nary_before(root: &Arg, pos: &MathPos) -> bool { + let pos = clamp(root, pos); + let Some(a) = arg_at(root, &pos.path) else { return false }; + let (i, k) = match locate(a, pos.off) { + Loc::Inside(i, k) => (i, k), + Loc::Before(i) => match i.checked_sub(1).and_then(|i| a.get(i).map(|n| (i, node_units(n)))) { + Some(at) => at, + None => return false, + }, + }; + let Some(MNode::Run(r)) = a.get(i) else { return false }; + !r.nor && !r.lit && k.checked_sub(1).and_then(|k| r.text.chars().nth(k)).is_some_and(crate::math::is_nary_char) +} + +/// The node just before the caret is a built n-ary operator. +pub fn nary_node_before(root: &Arg, pos: &MathPos) -> bool { + let pos = clamp(root, pos); + let Some(a) = arg_at(root, &pos.path) else { return false }; + let Loc::Before(i) = locate(a, pos.off) else { return false }; + i.checked_sub(1).and_then(|i| a.get(i)).is_some_and(|n| matches!(n, MNode::Nary { .. })) +} + /// Characters that make typed linear text build up into structures. fn builds(s: &str) -> bool { s.chars().any(|c| { @@ -872,6 +896,28 @@ pub fn build_up(root: &mut Arg, pos: &MathPos, latex: bool) -> Option { Some(clamp(root, &out)) } +/// Typing that just built an n-ary operator without its operand (`∫_0^1` then Space) goes on in +/// the empty operand, as after inserting one from the gallery, so what is typed next is +/// integrated or summed (#4917). An operator whose operand ends with another (`∫∫`) leads to the +/// innermost one. Other carets, and moves or Enter that build up, stay put. +pub fn into_empty_operand(root: &Arg, pos: &MathPos) -> MathPos { + let mut path = pos.path.clone(); + let mut off = pos.off; + while path.len() < MAX_DEPTH + && let Some(a) = arg_at(root, &path) + && let Loc::Before(i) = locate(a, off) + && let Some(k) = i.checked_sub(1) + && let Some(MNode::Nary { e, .. }) = a.get(k) + { + path.push((k, 2)); + if e.is_empty() { + return MathPos { path, off: 0 }; + } + off = units(e); + } + pos.clone() +} + /// A top-level `#` numbers the equation: wrap it as a one-row equation array. pub fn number_equation(root: &mut Arg) -> bool { if has_number_mark(root) && !matches!(root.as_slice(), [MNode::EqArr { .. }]) { diff --git a/crates/doc/src/numbering.rs b/crates/doc/src/numbering.rs index a5d2ec6e10..5ab94ea877 100644 --- a/crates/doc/src/numbering.rs +++ b/crates/doc/src/numbering.rs @@ -228,7 +228,7 @@ impl Numbering { /// Whether a paragraph in list `num` at `level` gets a label ([`Counters::next_label`] gives /// one), without counting it. pub fn shows_label(&self, num: u32, level: u8) -> bool { - self.abstract_of(num).is_some() && self.level(num, level.min(8)).is_some_and(|d| d.format != NumFormat::None) + self.abstract_of(num).is_some() && self.level(num, level.min(8)).is_some() } pub fn level(&self, num: u32, level: u8) -> Option<&Level> { if let Some((_, l)) = self.num(num)?.level_overrides.iter().find(|(i, _)| *i == level) { @@ -325,12 +325,9 @@ impl Counters { let lv = (level as usize).min(8); // This list's own definition of a level wins over its abstract list's. let level_def = |k: usize| numbering.level(num, k as u8); + // A "none" level (Word's numFmt none) still counts and restarts the levels below it; only + // its number reads as nothing, so its literal text ("RESPONSE:") shows (#4985). let def = level_def(lv)?.clone(); - // A "none" level shows no label at all (Word's numFmt none) — not even its text. - if def.format == NumFormat::None { - self.labels.insert((abs.id, lv as u8), CountedLabel { label: None, levels: Vec::new() }); - return None; - } let st = self.state.entry(abs.id).or_insert([0; 9]); let fl = self.flags.entry(abs.id).or_default(); // Only this level's override, not the num's others: the counters are shared by every @@ -382,7 +379,14 @@ impl Counters { let started = *fl; let numeric = def.format != NumFormat::Bullet; let label = if numeric { render_label(numbering, num, &def, &vals, &started) } else { def.text.clone() }; - self.labels.insert((abs.id, lv as u8), CountedLabel { label: numeric.then(|| label.clone()), levels: template_levels(&def.text) }); + // A "none" level shows its literal text, but has no number of its own for a reference to + // show (#4985): it is kept as one that shows none. + let counted = if def.format == NumFormat::None { + CountedLabel { label: None, levels: Vec::new() } + } else { + CountedLabel { label: numeric.then(|| label.clone()), levels: template_levels(&def.text) } + }; + self.labels.insert((abs.id, lv as u8), counted); Some((label, def)) } /// Which levels of `num`'s list have counted a paragraph yet (a level never used has no @@ -455,9 +459,9 @@ fn render_label(numbering: &Numbering, num: u32, def: &Level, vals: &[u32; 9], s let counted = started.get(k).is_some_and(|f| f.started); let val = if counted { val } else { level_def(k).map(|l| l.start).unwrap_or(1) }; // Legal numbering (w:isLgl) shows every level in Arabic digits, whatever its own - // format (二 of a Chinese outline becomes 2 in "2.1"). + // format (二 of a Chinese outline becomes 2 in "2.1"); a "none" level stays empty. let fmt = match level_def(k).map(|l| l.format).unwrap_or(NumFormat::Decimal) { - NumFormat::DecimalZero => NumFormat::DecimalZero, + f @ (NumFormat::DecimalZero | NumFormat::None) => f, _ if def.legal => NumFormat::Decimal, f => f, }; @@ -556,6 +560,27 @@ mod tests { assert_eq!(n.level(id, 2).map(|l| l.format), Some(NumFormat::DecimalZero)); } + #[test] + fn a_none_level_shows_its_text_and_still_counts() { + // As Word shows them (#4985): a "none" level's literal text is its label, its own number + // reads as nothing (also in legal numbering), and it restarts the levels below it. + let mut n = Numbering::default(); + let id = n.add_list(ListKind::Numbered); + let lvl = |format, text: &str| Level { format, text: text.into(), ..Level::default() }; + let legal = Level { legal: true, ..lvl(NumFormat::Decimal, "%2.%3") }; + if let Some(num) = n.nums.iter_mut().find(|x| x.id == id) { + num.level_overrides = vec![(0, lvl(NumFormat::None, "RESPONSE:")), (1, lvl(NumFormat::None, "[%2]")), (2, legal)]; + } + assert!(n.shows_label(id, 0)); + let mut c = Counters::default(); + assert_eq!(c.next_label(&n, id, 0).unwrap().0, "RESPONSE:"); + assert_eq!(c.next_label(&n, id, 1).unwrap().0, "[]"); + assert_eq!(c.next_label(&n, id, 2).unwrap().0, ".1"); + assert_eq!(c.next_label(&n, id, 2).unwrap().0, ".2"); + assert_eq!(c.next_label(&n, id, 1).unwrap().0, "[]"); + assert_eq!(c.next_label(&n, id, 2).unwrap().0, ".1"); + } + #[test] fn a_start_override_restarts_only_its_own_level_of_a_shared_list() { // One abstract list numbers the headings through two nums: one for levels 1-4, and one diff --git a/crates/doc/src/para/float.rs b/crates/doc/src/para/float.rs index 86f81c94d1..1918a7e5a2 100644 --- a/crates/doc/src/para/float.rs +++ b/crates/doc/src/para/float.rs @@ -186,6 +186,11 @@ pub struct Float { /// top. 0 = unknown, drawn in document order (objects inserted here, or from other formats). #[serde(skip_serializing_if = "is_zero_u32")] pub z: u32, + /// Turned, but the file keeps no room for the turn (`wp:effectExtent` missing or zero, as + /// files from other tools have): Word then keeps an inline one's line to its frame and lets + /// the turned object overhang it (see [`Float::line_pad`]). Cleared by [`Float::set_spin`]. + #[serde(skip_serializing_if = "is_false")] + pub bare_turn: bool, } /// Longest alternative text kept (characters): hostile files and commands are capped. @@ -241,6 +246,7 @@ impl Float { self.rot = wordcraft_geom::normalize_degrees(s.deg); self.flip_h = s.flip_h; self.flip_v = s.flip_v; + self.bare_turn = false; } /// How far a `w` × `h` frame's rotated bounds reach past it on each side (x, y): the room a /// rotated object takes beyond its frame (zero unrotated; negative where the turned object @@ -250,6 +256,23 @@ impl Float { let (bw, bh) = self.spin().extent(w, h); ((bw - w) / 2.0, (bh - h) / 2.0) } + /// A [`Float::bare_turn`] still inline and without effects room: made floating, or given room + /// for a shadow or glow since, it saves, and so lays out, with the turn's room again. + pub fn is_bare_turn(&self) -> bool { + self.bare_turn && self.wrap == Wrap::Inline && self.effect_extent() == [0.0; 4] + } + /// The room an inline `w` × `h` object's line keeps past its frame on each side (x, y) for its + /// turn: its rotated bounds ([`Float::spin_pad`]) — or, for a [`Float::is_bare_turn`], none, except + /// that Word 2013+ layout (`modern`) exchanges the frame's width and height from 45° up to 135° + /// and from 225° up to 315°. + pub fn line_pad(&self, w: f32, h: f32, modern: bool) -> (f32, f32) { + if !self.is_bare_turn() { + return self.spin_pad(w, h); + } + let (w, h) = (wordcraft_geom::finite(w).clamp(0.0, 4000.0), wordcraft_geom::finite(h).clamp(0.0, 4000.0)); + let deg = wordcraft_geom::normalize_degrees(self.rot); + if modern && ((45.0..135.0).contains(°) || (225.0..315.0).contains(°)) { ((h - w) / 2.0, (w - h) / 2.0) } else { (0.0, 0.0) } + } /// The effect extents, finite and clamped (left, top, right, bottom). pub fn effect_extent(&self) -> [f32; 4] { self.effect.map(|v| if v.is_finite() { v.clamp(0.0, 1584.0) } else { 0.0 }) diff --git a/crates/doc/src/para/inline_object.rs b/crates/doc/src/para/inline_object.rs index 56c54d4197..c33eb078e4 100644 --- a/crates/doc/src/para/inline_object.rs +++ b/crates/doc/src/para/inline_object.rs @@ -316,20 +316,34 @@ impl InlineObject { /// A group's members placed in a `w` × `h` box at (`x`, `y`): each member's rectangle /// (x, y, w, h) and the member. Empty for anything else. pub fn group_rects(&self, x: f32, y: f32, w: f32, h: f32) -> Vec<([f32; 4], &InlineObject)> { - let InlineObject::Group { ch_w, ch_h, children, .. } = self else { return Vec::new() }; + let InlineObject::Group { children, .. } = self else { return Vec::new() }; + self.member_rects(x, y, w, h).into_iter().filter_map(|(i, r)| Some((r, &children.get(i)?.obj))).collect() + } + /// [`InlineObject::group_rects`] with each member's index in `children` instead of the member. + pub fn member_rects(&self, x: f32, y: f32, w: f32, h: f32) -> Vec<(usize, [f32; 4])> { + let InlineObject::Group { children, .. } = self else { return Vec::new() }; + let (sx, sy) = self.group_scale(w, h); let fin = |v: f32| if v.is_finite() { v } else { 0.0 }; - let sx = if fin(*ch_w) > 0.0 { fin(w) / ch_w } else { 1.0 }; - let sy = if fin(*ch_h) > 0.0 { fin(h) / ch_h } else { 1.0 }; children .iter() .take(MAX_GROUP_CHILDREN) - .filter_map(|c| { + .enumerate() + .filter_map(|(i, c)| { let (cw, chh, _) = c.obj.frame()?; let r = [x + fin(c.x) * sx, y + fin(c.y) * sy, (fin(cw) * sx).clamp(0.0, 4000.0), (fin(chh) * sy).clamp(0.0, 4000.0)]; - (!matches!(c.obj, InlineObject::Group { .. })).then_some((r, &c.obj)) + (!matches!(c.obj, InlineObject::Group { .. })).then_some((i, r)) }) .collect() } + /// How much a group drawn `w` × `h` scales its members' space (x, y): 1 for a canvas, whose + /// members keep their size. + pub fn group_scale(&self, w: f32, h: f32) -> (f32, f32) { + let InlineObject::Group { ch_w, ch_h, .. } = self else { return (1.0, 1.0) }; + let fin = |v: f32| if v.is_finite() { v } else { 0.0 }; + let sx = if fin(*ch_w) > 0.0 { fin(w) / ch_w } else { 1.0 }; + let sy = if fin(*ch_h) > 0.0 { fin(h) / ch_h } else { 1.0 }; + (sx, sy) + } /// A legacy form field's definition (`FORMCHECKBOX`, `FORMTEXT`, `FORMDROPDOWN`), read from /// the `w:ffData` it keeps; `None` for anything else. pub fn legacy_form(&self) -> Option { diff --git a/crates/doc/src/props.rs b/crates/doc/src/props.rs index a801081845..38feba083c 100644 --- a/crates/doc/src/props.rs +++ b/crates/doc/src/props.rs @@ -27,7 +27,8 @@ pub use char_props::{ }; pub use color::{Rgb, TextColor}; pub use para_props::{ - Align, Border, BorderStyle, Borders, LineSpacing, NumRef, ParaProps, TAB_POS_EPS, TabAlign, TabLeader, TabStop, TextAlignment, TextFrame, + Align, Border, BorderStyle, Borders, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, TAB_POS_EPS, TabAlign, TabLeader, TabStop, + TextAlignment, TextFrame, }; pub use table_props::{ CellPicture, CellProps, HeightRule, PictureRepeat, RowProps, TableFloat, TableLook, TableProps, TextDirection, TextVert, VAlign, VMerge, diff --git a/crates/doc/src/props/para_props.rs b/crates/doc/src/props/para_props.rs index c34d527192..a48540248e 100644 --- a/crates/doc/src/props/para_props.rs +++ b/crates/doc/src/props/para_props.rs @@ -26,6 +26,67 @@ impl Align { } } +/// Paragraph-level text flow (`w:pPr/w:textDirection`, ECMA-376 §17.3.1.41). +/// +/// Kept separate from [`super::TextDirection`], whose three values model the table-cell direction +/// control. These values preserve the exact OOXML direction through JSON and DOCX saves. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum ParagraphTextDirection { + LrTb, + LrTbV, + Tb, + TbRl, + TbRlV, + TbLr, + TbLrV, + BtLr, + TbV, + Rl, + RlV, + Lr, + LrV, +} + +impl ParagraphTextDirection { + pub fn from_ooxml(value: &str) -> Option { + match value { + "lrTb" => Some(Self::LrTb), + "lrTbV" => Some(Self::LrTbV), + "tb" => Some(Self::Tb), + "tbRl" => Some(Self::TbRl), + "tbRlV" => Some(Self::TbRlV), + "tbLr" => Some(Self::TbLr), + "tbLrV" => Some(Self::TbLrV), + "btLr" => Some(Self::BtLr), + "tbV" => Some(Self::TbV), + "rl" => Some(Self::Rl), + "rlV" => Some(Self::RlV), + "lr" => Some(Self::Lr), + "lrV" => Some(Self::LrV), + _ => None, + } + } + + pub fn ooxml(self) -> &'static str { + match self { + Self::LrTb => "lrTb", + Self::LrTbV => "lrTbV", + Self::Tb => "tb", + Self::TbRl => "tbRl", + Self::TbRlV => "tbRlV", + Self::TbLr => "tbLr", + Self::TbLrV => "tbLrV", + Self::BtLr => "btLr", + Self::TbV => "tbV", + Self::Rl => "rl", + Self::RlV => "rlV", + Self::Lr => "lr", + Self::LrV => "lrV", + } + } +} + /// Vertical alignment of characters of different sizes on a line (`w:textAlignment`, /// ECMA-376 §17.3.1.39). #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -391,6 +452,9 @@ pub struct ParaProps { pub suppress_line_numbers: Option, #[serde(skip_serializing_if = "Option::is_none")] pub bidi: Option, + /// Paragraph text direction (`w:textDirection`); `None` inherits from the paragraph style. + #[serde(skip_serializing_if = "Option::is_none")] + pub text_direction: Option, /// Drop cap: lines to drop (0 = none) — applies to the first character(s). #[serde(skip_serializing_if = "Option::is_none")] pub drop_cap: Option, @@ -462,6 +526,7 @@ impl ParaProps { } fn overlay_script_layout(&mut self, patch: &ParaProps) { + overlay_fields!(self, patch; bidi, text_direction, kinsoku, word_wrap, overflow_punct, top_line_punct, auto_space_de, auto_space_dn); overlay_fields!(self, patch; bidi, kinsoku, word_wrap, overflow_punct, top_line_punct, auto_space_de, auto_space_dn, text_alignment); } pub fn overlaid(mut self, patch: &ParaProps) -> ParaProps { diff --git a/crates/doc/src/resolve.rs b/crates/doc/src/resolve.rs index 8c35c6862f..40449e82cb 100644 --- a/crates/doc/src/resolve.rs +++ b/crates/doc/src/resolve.rs @@ -137,9 +137,12 @@ impl ResolvedChar { pub fn uses_complex(&self, c: char) -> bool { self.rtl || self.cs || crate::bidi::is_complex_script(c) } - /// The size glyphs are drawn at (super/subscript shrink to ~2/3). + /// The size glyphs are drawn and measured at. Word sets superscript and subscript text at + /// 0.65 of the run's size rounded to the half point, a font size the format can store + /// (measured in Word for Mac from digit advances: 8 pt → 5, 9 → 6, 11 → 7, 12 → 8, + /// 16 → 10.5, 24 → 15.5, 36 → 23.5). pub fn draw_size(&self) -> f32 { - if self.vert_align == VertAlign::Baseline { self.size } else { self.size * 0.65 } + if self.vert_align == VertAlign::Baseline { self.size } else { (self.size * 0.65 * 2.0).round() / 2.0 } } /// Baseline shift, points (positive = up). pub fn baseline_shift(&self) -> f32 { @@ -453,6 +456,32 @@ fn diff_patch(with: &T, wi mod tests { use super::*; + /// Superscript and subscript sizes as Word for Mac sets them (measured from digit advances in + /// its PDF export): 0.65 of the size, rounded to the half point. + #[test] + fn raised_and_lowered_text_is_sized_to_the_half_point() { + for (size, want) in [ + (8.0, 5.0), + (9.0, 6.0), + (10.0, 6.5), + (11.0, 7.0), + (12.0, 8.0), + (13.0, 8.5), + (14.0, 9.0), + (16.0, 10.5), + (20.0, 13.0), + (24.0, 15.5), + (36.0, 23.5), + ] { + for vert_align in [VertAlign::Superscript, VertAlign::Subscript] { + let rc = ResolvedChar { size, vert_align, ..StyleSheet::builtin().resolve_char(None, &CharProps::default()) }; + assert_eq!(rc.draw_size(), want, "{size} pt {vert_align:?}"); + } + } + let rc = StyleSheet::builtin().resolve_char(None, &CharProps { size: Some(11.0), ..Default::default() }); + assert_eq!(rc.draw_size(), 11.0, "baseline text keeps its size"); + } + /// #2036: only a character itself (direct formatting) has a symbol font of its own; a /// style never gives one to its text. #[test] diff --git a/crates/doc/src/section.rs b/crates/doc/src/section.rs index 1d72468f6f..0918cb961c 100644 --- a/crates/doc/src/section.rs +++ b/crates/doc/src/section.rs @@ -289,13 +289,14 @@ impl NumFormat { pub fn from_ooxml(s: &str) -> NumFormat { NumFormat::ALL.into_iter().find(|f| f.ooxml() == s).unwrap_or(NumFormat::Decimal) } - /// Format `n` (1-based). Formats WordCraft doesn't draw yet (Hebrew, Hindi, Thai, - /// Vietnamese, text amounts) show decimal, as do values past a sequence's end - /// (the 21st item of ①…⑳). + /// Format `n` (1-based). Formats WordCraft doesn't draw yet (Hebrew, Hindi, Thai letters + /// and counting, Vietnamese, text amounts) show decimal, as do values past a sequence's end + /// (the 21st item of ①…⑳). Thai numbers are the decimal number in Thai digits (๑, ๒ … ๑๐). pub fn format(self, n: u32) -> String { use crate::cjk_numbers as cjk; match self { NumFormat::DecimalZero => format!("{n:02}"), + NumFormat::ThaiNumbers => thai_digits(n), NumFormat::UpperRoman => roman(n).to_uppercase(), NumFormat::LowerRoman => roman(n), NumFormat::UpperLetter => letters(n).to_uppercase(), @@ -349,7 +350,6 @@ impl NumFormat { | NumFormat::HindiNumbers | NumFormat::HindiCounting | NumFormat::ThaiLetters - | NumFormat::ThaiNumbers | NumFormat::ThaiCounting | NumFormat::BahtText | NumFormat::DollarText => n.to_string(), @@ -357,6 +357,11 @@ impl NumFormat { } } +/// `n` in Thai digits, ๐ (U+0E50) to ๙ (#5757). +fn thai_digits(n: u32) -> String { + n.to_string().chars().filter_map(|c| c.to_digit(10).and_then(|d| char::from_u32(0x0E50 + d))).collect() +} + /// Chicago Manual of Style note marks: *, †, ‡, §, then doubled (**, ††, …) and so on. fn chicago(n: u32) -> String { const MARKS: [char; 4] = ['*', '\u{2020}', '\u{2021}', '\u{a7}']; @@ -999,6 +1004,18 @@ mod tests { assert_eq!(NumFormat::from_ooxml("klingon"), NumFormat::Decimal, "unknown formats are decimal"); } + /// #5757: Thai numbers are the decimal number in Thai digits, every digit, at any length. + #[test] + fn thai_numbers_use_thai_digits() { + let labels: Vec = [1, 2, 3, 9, 10, 11, 20, 105, 2569].iter().map(|n| NumFormat::ThaiNumbers.format(*n)).collect(); + assert_eq!(labels.join(" "), "๑ ๒ ๓ ๙ ๑๐ ๑๑ ๒๐ ๑๐๕ ๒๕๖๙"); + assert_eq!(NumFormat::ThaiNumbers.format(0), "๐"); + assert_eq!(NumFormat::ThaiNumbers.format(u32::MAX), "๔๒๙๔๙๖๗๒๙๕"); + assert_eq!(NumFormat::from_ooxml("thaiNumbers"), NumFormat::ThaiNumbers); + // Formats not drawn yet still show decimal. + assert_eq!(NumFormat::ThaiLetters.format(3), "3"); + } + #[test] fn russian_letters_follow_word() { // Word's own labels for a russianLower list: no ё, й, ъ or ь, then doubled letters. diff --git a/crates/doc/src/symbol_fonts.rs b/crates/doc/src/symbol_fonts.rs index 775de27736..1d5175540b 100644 --- a/crates/doc/src/symbol_fonts.rs +++ b/crates/doc/src/symbol_fonts.rs @@ -19,8 +19,8 @@ //! //! Tables are indexed by code − 0x20; 0 = no Unicode equivalent. -/// The symbol fonts this module knows, by lowercase family name. -fn table(family: &str) -> Option<&'static [u32; 224]> { +/// The symbol table for a single family name, by lowercase family name. +fn single_table(family: &str) -> Option<&'static [u32; 224]> { let f = family.trim(); if f.eq_ignore_ascii_case("symbol") || f.eq_ignore_ascii_case("symbol (pcl6)") { Some(&SYMBOL) @@ -37,6 +37,11 @@ fn table(family: &str) -> Option<&'static [u32; 224]> { } } +/// The symbol fonts this module knows, by lowercase family name. +fn table(family: &str) -> Option<&'static [u32; 224]> { + if family.contains(';') { family.split(';').map(str::trim).filter(|s| !s.is_empty()).find_map(single_table) } else { single_table(family) } +} + /// Whether `family` is one of Word's symbol fonts (Symbol, Wingdings, Wingdings 2, Wingdings 3, /// Webdings), whose characters [`symbol_char`] maps. pub fn is_symbol_font(family: &str) -> bool { diff --git a/crates/doc/tests/empty_paragraph_merge.rs b/crates/doc/tests/empty_paragraph_merge.rs new file mode 100644 index 0000000000..7368ad1be1 --- /dev/null +++ b/crates/doc/tests/empty_paragraph_merge.rs @@ -0,0 +1,158 @@ +use std::sync::Arc; +use wordcraft_doc::props::{Align, CharProps, ParaProps}; +use wordcraft_doc::{Block, Document, Paragraph, Path, Pos, StoryRef, Table, para_block}; + +/// Build an empty heading followed by explicitly formatted, mixed-run body text. +fn fixture(first: &str, style: Option<&str>) -> Document { + let mut d = Document::from_text(&format!("{first}\nBody paragraph.")); + let p = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap(); + p.props.style = Some("Heading1".into()); + p.props.outline_level = Some(0); + p.mark = CharProps { size: Some(40.0), bold: Some(true), ..Default::default() }; + let p = d.para_mut(StoryRef::Body, &Path::top(1)).unwrap(); + p.props = ParaProps { + style: style.map(str::to_string), + align: Some(Align::Right), + indent_left: Some(18.0), + space_after: Some(7.0), + keep_next: Some(false), + ..Default::default() + }; + p.mark = CharProps { size: Some(11.0), bold: Some(false), lang: Some("en-GB".into()), ..Default::default() }; + p.format(0, 4, &|c| c.italic = Some(true)).unwrap(); + d +} + +/// Compare paragraph, mark and inline-run formatting independently of text. +fn assert_format(p: &Paragraph, expected: &Paragraph) { + assert_eq!(p.props, expected.props, "the following paragraph keeps all its properties"); + assert_eq!(p.mark, expected.mark, "the empty heading's mark must not replace the following mark"); + assert_eq!(serde_json::to_value(&p.runs).unwrap(), serde_json::to_value(&expected.runs).unwrap()); +} + +/// Deleting an empty paragraph preserves the following style, including implicit Normal. +#[test] +fn deleting_an_empty_paragraph_keeps_the_following_format() { + for first_style in ["Heading1", "Heading3", "Normal"] { + for style in [None, Some("Normal"), Some("Heading2")] { + let mut d = fixture("", style); + d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.style = Some(first_style.into()); + let expected = d.para_at(&Pos::body(1, 0)).unwrap().clone(); + assert_eq!(d.delete_range(&Pos::body(0, 0), &Pos::body(1, 0)).unwrap(), Pos::body(0, 0)); + let p = d.para_at(&Pos::body(0, 0)).unwrap(); + assert_eq!(p.text, expected.text); + assert_format(p, &expected); + } + } +} + +/// A selection spanning several paragraphs keeps the surviving tail format in both directions. +#[test] +fn an_empty_start_preserves_the_last_paragraph_over_a_multi_paragraph_selection() { + for backwards in [false, true] { + let mut d = fixture("", None); + d.body.insert(1, para_block(Paragraph::with_text("Removed middle paragraph", Default::default()).styled("Heading2"))); + let mut expected = d.para_at(&Pos::body(2, 0)).unwrap().clone(); + expected.delete(0, 5).unwrap(); + let (a, b) = (Pos::body(0, 0), Pos::body(2, 5)); + d.delete_range(if backwards { &b } else { &a }, if backwards { &a } else { &b }).unwrap(); + assert_eq!(d.body.len(), 1); + let p = d.para_at(&Pos::body(0, 0)).unwrap(); + assert_eq!(p.text, "paragraph."); + assert_format(p, &expected); + } +} + +/// A control whose closing marker survives must move across the join without replacing tail formatting. +#[test] +fn an_empty_start_keeps_a_control_from_a_deleted_middle_paragraph() { + use wordcraft_doc::control::ContentControl; + use wordcraft_doc::{InlineObject, Revision, RevisionKind}; + + let mut d = fixture("", Some("Normal")); + d.body.insert(1, para_block(Paragraph::with_text("Removed", Default::default()))); + d.body.push(para_block(Paragraph::with_text("After", Default::default()))); + d.revisions.push(Revision { kind: RevisionKind::Insert, ..Default::default() }); + d.revisions.push(Revision { kind: RevisionKind::Delete, ..Default::default() }); + let p = d.para_mut(StoryRef::Body, &Path::top(2)).unwrap(); + p.mark.ins = Some(0); + p.mark.del = Some(1); + let expected = p.clone(); + let marker = CharProps { bold: Some(true), ins: Some(0), ..Default::default() }; + d.insert_object(&Pos::body(1, 0), InlineObject::ControlStart { control: Box::new(ContentControl::default()) }, &marker).unwrap(); + d.insert_object(&Pos::body(3, 5), InlineObject::ControlEnd, &Default::default()).unwrap(); + assert_eq!(d.control_ranges(StoryRef::Body).len(), 1); + + d.delete_range(&Pos::body(0, 0), &Pos::body(2, 0)).unwrap(); + let p = d.para_at(&Pos::body(0, 0)).unwrap(); + assert_eq!(p.props, expected.props); + assert_eq!(p.mark, expected.mark, "tail revision IDs and formatting survive the moved marker"); + assert_eq!(p.props_of_char(0), &marker); + assert!(matches!(p.objects.as_slice(), [InlineObject::ControlStart { .. }])); + assert_eq!(p.text, format!("\u{fffc}{}", expected.text)); + let ranges = d.control_ranges(StoryRef::Body); + assert_eq!(ranges.len(), 1); + assert_eq!(ranges[0].start, Pos::body(0, 0)); + assert_eq!(ranges[0].end, Pos::body(1, 5)); + assert!(!d.has_unbalanced_controls()); +} + +/// The empty-paragraph rule applies in header stories and table-cell containers. +#[test] +fn empty_paragraph_merges_work_in_a_header_and_a_table_cell() { + for cell in [false, true] { + let mut d = fixture("", Some("Normal")); + let expected = d.para_at(&Pos::body(1, 0)).unwrap().clone(); + let (story, a, b) = if cell { + let mut table = Table::new(1, 1, 300.0); + table.rows[0].cells[0].blocks = d.body.clone(); + d.body = vec![Arc::new(Block::Table(table)), para_block(Paragraph::new())]; + (StoryRef::Body, Path(vec![0, 0, 0, 0]), Path(vec![0, 0, 0, 1])) + } else { + let id = d.add_part(wordcraft_doc::PartKind::Header, d.body.clone()); + (StoryRef::Part(id), Path::top(0), Path::top(1)) + }; + d.delete_range(&Pos { story, path: a.clone(), off: 0 }, &Pos { story, path: b, off: 0 }).unwrap(); + assert_format(d.para(story, &a).unwrap(), &expected); + } +} + +/// Absorbing an empty section keeps the following format and inherited header. +#[test] +fn deleting_an_empty_section_paragraph_keeps_format_and_linked_headers() { + let mut d = fixture("", None); + let expected = d.para_at(&Pos::body(1, 0)).unwrap().clone(); + let header = d.add_part(wordcraft_doc::PartKind::Header, vec![para_block(Paragraph::with_text("Header", Default::default()))]); + let mut section = d.last_section.clone(); + section.headers.default = Some(header); + d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().section = Some(Box::new(section)); + d.last_section.margin_left = 96.0; + d.delete_range(&Pos::body(0, 0), &Pos::body(1, 0)).unwrap(); + assert_format(d.para_at(&Pos::body(0, 0)).unwrap(), &expected); + assert!(d.para_at(&Pos::body(0, 0)).unwrap().section.is_none()); + assert_eq!(d.last_section.headers.default, Some(header)); + assert_eq!(d.last_section.margin_left, 96.0); +} + +/// Deleting actual text continues to use the first paragraph merge rule. +#[test] +fn joining_nonempty_paragraphs_keeps_the_existing_first_paragraph_rule() { + for off in [0, 7] { + let mut d = fixture("Heading", None); + let expected = d.para_at(&Pos::body(0, 0)).unwrap().props.clone(); + d.delete_range(&Pos::body(0, off), &Pos::body(1, 0)).unwrap(); + assert_eq!(d.para_at(&Pos::body(0, 0)).unwrap().props, expected); + } +} + +/// Removing a soft line break must not change paragraph formatting. +#[test] +fn a_soft_break_is_not_an_empty_paragraph_merge() { + let mut d = fixture("", None); + let p = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap(); + p.insert_text(0, "\nHeading", &Default::default()).unwrap(); + let expected = p.props.clone(); + d.delete_range(&Pos::body(0, 0), &Pos::body(0, 1)).unwrap(); + assert_eq!(d.body.len(), 2); + assert_eq!(d.para_at(&Pos::body(0, 0)).unwrap().props, expected); +} diff --git a/crates/docbin/src/drawing.rs b/crates/docbin/src/drawing.rs index 4fe3b62a6a..33f45de345 100644 --- a/crates/docbin/src/drawing.rs +++ b/crates/docbin/src/drawing.rs @@ -211,7 +211,11 @@ fn leaf(sp: &Sp, w: f32, h: f32, float: Float, b: &Build, pics: &mut Pictures) - kind = ShapeKind::TextBox; } let line = kind == ShapeKind::Line; - let fill = if line || p.filled == Some(false) { None } else { Some(Fill::solid(p.fill_color.unwrap_or(Rgb::WHITE))) }; + // Brackets and braces (85 to 88, bracket and brace pairs 185, 186) are unfilled unless + // `fFilled` says otherwise, as Word's VML shapetypes for them say (`filled="f"`). They are + // drawn as their box here, which a default fill would make an opaque block over the text. + let unfilled = p.filled == Some(false) || (matches!(sp.spt, 85..=88 | 185 | 186) && p.filled != Some(true)); + let fill = if line || unfilled { None } else { Some(Fill::solid(p.fill_color.unwrap_or(Rgb::WHITE))) }; let stroke = if p.line == Some(false) { None } else { Some(p.line_color.unwrap_or(Rgb::BLACK)) }; // Line widths are EMUs; Word's default is 0.75 pt. let stroke_width = p.line_width.map_or(0.75, |e| (e as f32 / 12_700.0).clamp(0.0, 100.0)); diff --git a/crates/docbin/src/fkp.rs b/crates/docbin/src/fkp.rs index 8ed395ac7c..3de272da26 100644 --- a/crates/docbin/src/fkp.rs +++ b/crates/docbin/src/fkp.rs @@ -7,9 +7,26 @@ use crate::DocbinError; /// Largest bin table (entries) we accept. const MAX_BINS: usize = 100_000; +/// A Chpx grpprl and the FCs `[lo, hi)` for which it is the one [`Bins::chpx`] returns. +#[derive(Clone, Copy)] +pub(crate) struct ChpxRun<'w> { + pub(crate) grpprl: &'w [u8], + lo: u32, + hi: u32, +} + +impl ChpxRun<'_> { + pub(crate) fn contains(&self, fc: u32) -> bool { + self.lo <= fc && fc < self.hi + } +} + /// A parsed `PlcBte*`: FC breakpoints and the FKP page each range lives in. pub(crate) struct Bins { fcs: Vec, + /// `fcs` ascends, so the bin of an FC holds every FC up to the next breakpoint (a corrupt + /// table that does not gets no cached ranges, [`ChpxRun`]). + sorted: bool, /// Page number (× 512 = byte offset in the WordDocument stream). pns: Vec, } @@ -20,7 +37,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(), pns: Vec::new() }); + return Ok(Bins { fcs: Vec::new(), sorted: true, pns: Vec::new() }); } 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()))?; @@ -38,36 +55,58 @@ impl Bins { // PnBte* keeps the page number in its low bits; the rest must be zero. pns.push(at(n + 1 + i).ok_or_else(|| DocbinError::Malformed("truncated bin aPn".into()))? & 0x003F_FFFF); } - Ok(Bins { fcs, pns }) + let sorted = fcs.windows(2).all(|w| w.first() <= w.get(1)); + Ok(Bins { fcs, sorted, pns }) } - /// The page containing `fc`, as a slice of the WordDocument stream. - fn page<'w>(&self, word: &'w [u8], fc: u32) -> Option<&'w [u8]> { + /// The page containing `fc`, as a slice of the WordDocument stream, and the FC range + /// `[lo, hi)` of its bin (`hi` is `u32::MAX` for the last; an empty range when the table + /// is not ascending, so nothing is cached from it). + fn page<'w>(&self, word: &'w [u8], fc: u32) -> Option<(&'w [u8], u32, u32)> { let i = self.fcs.partition_point(|&f| f <= fc).checked_sub(1)?; let pn = *self.pns.get(i)?; let at = pn.checked_mul(512)? as usize; - word.get(at..at.checked_add(512)?) + let page = word.get(at..at.checked_add(512)?)?; + if !self.sorted { + return Some((page, 1, 0)); + } + let lo = self.fcs.get(i).copied().unwrap_or(0); + let hi = self.fcs.get(i + 1).copied().unwrap_or(u32::MAX); + Some((page, lo, hi)) } /// 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] { - let Some(page) = self.page(word, fc) else { return &[] }; - let Some(&crun) = page.get(511) else { return &[] }; + 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> { + 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 }; let crun = crun.min(0x65) as usize; let rgfc = |i: usize| -> Option { page.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) }; // rgb array sits right after rgfc (crun+1 u32s); each byte is a half-offset into the // region between rgb's end and crun. let rgb_at = (crun + 1) * 4; + // The last run starting at or before `fc`. It stays the answer for every FC from its own + // start up to the smallest start after it (also in a page whose starts do not ascend). let j = (0..crun).rev().find(|&j| rgfc(j).is_some_and(|f| f <= fc)); - let Some(j) = j else { return &[] }; - let Some(&off) = page.get(rgb_at + j) else { return &[] }; + let first_after = j.map_or(0, |j| j + 1); + let hi = (first_after..crun).filter_map(rgfc).min().map_or(page_hi, |m| m.min(page_hi)); + let lo = j.and_then(rgfc).map_or(page_lo, |f| f.max(page_lo)); + let run = |grpprl: &'w [u8]| ChpxRun { grpprl, lo, hi }; + let Some(j) = j else { return run(&[]) }; + let Some(&off) = page.get(rgb_at + j) else { return run(&[]) }; if off == 0 { - return &[]; + return run(&[]); } let at = off as usize * 2; - let Some(&cb) = page.get(at) else { return &[] }; - page.get(at + 1..at + 1 + cb as usize).unwrap_or(&[]) + let Some(&cb) = page.get(at) else { return run(&[]) }; + run(page.get(at + 1..at + 1 + cb as usize).unwrap_or(&[])) } /// The (istd, grpprl) of the Papx of the paragraph whose mark is just before `fc` @@ -75,7 +114,7 @@ impl Bins { /// 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]) { - let Some(page) = self.page(word, fc) else { return (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. let max_run = (511usize.saturating_sub((crun as usize + 1) * 4)) / 13; diff --git a/crates/docbin/src/lib.rs b/crates/docbin/src/lib.rs index e97e2676d3..e143bd9763 100644 --- a/crates/docbin/src/lib.rs +++ b/crates/docbin/src/lib.rs @@ -260,6 +260,9 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) -> }; let mut doc = Document::new(); + // Word opens Word 97-2003 files in compatibility mode 11 (Word 2003); the model's default + // 15 would apply Word 2013+ justification and line-breaking rules to them. + doc.settings.compat_mode = 11; if let Some(core) = metadata::read(&mut comp) { doc.core = core; } @@ -502,29 +505,37 @@ struct ParaBuild { runs: Vec, props: wordcraft_doc::props::ParaProps, mark: CharProps, - /// The current run's properties, so runs only break when formatting changes. - cur: Option, + /// Which resolved formatting ([`FmtState::generation`]) the last character came from, while + /// the last run is still the one it extended: the same generation means equal properties. + run_gen: Option, /// Inline objects, each anchored at a U+FFFC pushed into `text`. objects: Vec, } impl ParaBuild { fn push(&mut self, c: char, props: CharProps) { + self.run_gen = None; let len = c.len_utf8(); - match &self.cur { - Some(p) if *p == props => { - if let Some(r) = self.runs.last_mut() { - r.len += len; - } - } - _ => { - self.runs.push(Run { len, props: props.clone() }); - self.cur = Some(props); - } + match self.runs.last_mut() { + Some(r) if r.props == props => r.len += len, + _ => self.runs.push(Run { len, props }), } self.text.push(c); } + /// [`ParaBuild::push`] for properties that are resolved once and shared by many characters: + /// they are cloned only when a run starts, and not even compared while `generation` stays + /// the same. + fn push_shared(&mut self, c: char, props: &CharProps, generation: u64) { + let len = c.len_utf8(); + match self.runs.last_mut() { + Some(r) if self.run_gen == Some(generation) || r.props == *props => r.len += len, + _ => self.runs.push(Run { len, props: props.clone() }), + } + self.run_gen = Some(generation); + self.text.push(c); + } + /// Append `c` to the current run, whose properties the caller knows it matches. fn extend(&mut self, c: char) { if let Some(r) = self.runs.last_mut() { @@ -552,7 +563,7 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic // Field assembly state: instruction characters are swallowed, the result is kept until // the end character, then emitted as one object (hyperlinks keep their result text). let mut field: Option = None; - let mut memo: Option> = None; + let mut fmt = FmtState::default(); while cp < cp_end { // Bookmark boundaries at this CP (main-document walks only; subdocument walks get // an empty table and `cp` never matches the main-document CPs). @@ -715,16 +726,16 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic pb.objects.push(InlineObject::NoteRef { kind: *kind, id: *id, custom: String::new() }); } } - '\t' => push_formatted(&mut pb, cp, '\t', ctx, field.as_mut(), &mut memo), - '\u{B}' => push_formatted(&mut pb, cp, '\n', ctx, field.as_mut(), &mut memo), - '\u{C}' => push_formatted(&mut pb, cp, '\u{C}', ctx, field.as_mut(), &mut memo), - '\u{E}' => push_formatted(&mut pb, cp, '\u{E}', ctx, field.as_mut(), &mut memo), - '\u{1E}' => push_formatted(&mut pb, cp, '\u{2011}', ctx, field.as_mut(), &mut memo), - '\u{1F}' => push_formatted(&mut pb, cp, '\u{AD}', ctx, field.as_mut(), &mut memo), + '\t' => push_formatted(&mut pb, cp, '\t', ctx, field.as_mut(), &mut fmt), + '\u{B}' => push_formatted(&mut pb, cp, '\n', ctx, field.as_mut(), &mut fmt), + '\u{C}' => push_formatted(&mut pb, cp, '\u{C}', ctx, field.as_mut(), &mut fmt), + '\u{E}' => push_formatted(&mut pb, cp, '\u{E}', ctx, field.as_mut(), &mut fmt), + '\u{1E}' => push_formatted(&mut pb, cp, '\u{2011}', ctx, field.as_mut(), &mut fmt), + '\u{1F}' => push_formatted(&mut pb, cp, '\u{AD}', ctx, field.as_mut(), &mut fmt), // Object anchors, comment references and other special characters are dropped // (or handled by later passes). c if (c as u32) < 0x20 => {} - c => push_formatted(&mut pb, cp, c, ctx, field.as_mut(), &mut memo), + c => push_formatted(&mut pb, cp, c, ctx, field.as_mut(), &mut fmt), } cp = cp.saturating_add(units); } @@ -854,34 +865,53 @@ fn num_of(ilfo: i32) -> Option { /// plus the piece's Prm, over the paragraph style's base character props. Inside a field, /// the character is recorded into the instruction or the cached result instead of (or as /// well as) the visible text. -fn push_formatted<'a>(pb: &mut ParaBuild, cp: u32, c: char, ctx: &WalkCtx<'a>, field: Option<&mut FieldBuild>, memo: &mut Option>) { - let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0); - let grpprl = ctx.chpx_bins.chpx(ctx.word, fc); - let prm = ctx.pieces.prm_of(cp); - let style = pb.props.style.as_deref(); - let mut props = match memo.as_ref().filter(|m| std::ptr::eq(m.chpx, grpprl) && m.prm == prm && m.style.as_deref() == style) { - Some(m) => m.props.clone(), +fn push_formatted<'a>(pb: &mut ParaBuild, cp: u32, c: char, ctx: &WalkCtx<'a>, field: Option<&mut FieldBuild>, st: &mut FmtState<'a>) { + // The piece and the CHPX run of the previous character usually cover this one too. + let span = match st.span.filter(|s| s.contains(cp)) { + Some(s) => Some(s), None => { - let default; - let base = match style.and_then(|id| ctx.sheet.get(id)) { - Some(st) => &st.chr, - None => { - default = CharProps::default(); - &default - } - }; - let mut props = char_props(grpprl, ctx.fonts, base); - apply_prm(&mut props, ctx.pieces.piece_prm(cp), ctx.fonts, base, ctx.work); - *memo = Some(FmtMemo { chpx: grpprl, prm, style: style.map(str::to_owned), props: props.clone() }); - props + st.span = ctx.pieces.span(cp); + st.span } }; + let fc = span.and_then(|s| s.fc_at(cp)).unwrap_or(0); + let run = match st.chpx.filter(|r| r.contains(fc)) { + Some(r) => r, + None => { + let r = ctx.chpx_bins.chpx_run(ctx.word, fc); + st.chpx = Some(r); + r + } + }; + let grpprl = run.grpprl; + let prm = span.map_or(0, |s| s.prm()); + let style = pb.props.style.as_deref(); + let hit = st.memo.as_ref().is_some_and(|m| std::ptr::eq(m.chpx, grpprl) && m.prm == prm && m.style.as_deref() == style); + if !hit { + let default; + let base = match style.and_then(|id| ctx.sheet.get(id)) { + Some(sty) => &sty.chr, + None => { + default = CharProps::default(); + &default + } + }; + let mut props = char_props(grpprl, ctx.fonts, base); + apply_prm(&mut props, ctx.pieces.piece_prm(cp), ctx.fonts, base, ctx.work); + st.memo = Some(FmtMemo { chpx: grpprl, prm, style: style.map(str::to_owned), props }); + st.generation += 1; + } + let Some(m) = st.memo.as_ref() else { return }; // A symbol character (Insert › Symbol) is stored as the placeholder U+0028 with // sprmCSymbol carrying the font and the character code ([MS-DOC] §2.6.1). + let symbol = if c == '(' { symbol_operand(grpprl) } else { None }; + if field.is_none() && symbol.is_none() { + pb.push_shared(c, &m.props, st.generation); + return; + } + let mut props = m.props.clone(); let mut c = c; - if c == '(' - && let Some((ftc, xchar)) = symbol_operand(grpprl) - { + if let Some((ftc, xchar)) = symbol { if let Some(font) = ctx.fonts.get(usize::from(ftc)) { props.font = Some(font.clone()); } @@ -900,6 +930,18 @@ fn push_formatted<'a>(pb: &mut ParaBuild, cp: u32, c: char, ctx: &WalkCtx<'a>, f } } +/// What the walk carries from one character to the next while it resolves formatting: the piece +/// and the CHPX run the last character was in (so most characters need no search), and the last +/// formatting resolved. +#[derive(Default)] +struct FmtState<'a> { + span: Option>, + chpx: Option>, + memo: Option>, + /// Counts the memo's replacements, so equal values mean the same properties. + generation: u64, +} + /// The last character formatting resolved, reused while the CHPX, the piece's Prm and the /// paragraph style stay the same, so a Prm grpprl (up to 64 KB) is interpreted once per /// formatting change, not once per character (#3692). diff --git a/crates/docbin/src/piece.rs b/crates/docbin/src/piece.rs index d52f968b7a..fda4e5470c 100644 --- a/crates/docbin/src/piece.rs +++ b/crates/docbin/src/piece.rs @@ -117,12 +117,6 @@ impl PieceTable { } } - /// The raw Prm of the piece holding `cp` (0, no properties, when there is none): two CPs - /// with the same value get the same [`PrmRef`]. - pub(crate) fn prm_of(&self, cp: u32) -> u16 { - self.piece_of(cp).map_or(0, |p| p.prm) - } - fn piece_of(&self, cp: u32) -> Option<&Pcd> { let i = self.cps.partition_point(|&c| c <= cp).checked_sub(1)?; if i >= self.pieces.len() { @@ -143,6 +137,17 @@ impl PieceTable { if p.compressed { p.fc.checked_add(step)?.checked_div(2) } else { p.fc.checked_add(step) } } + /// The piece holding `cp`, with the CP range it covers: for every CP in that range the + /// answers of [`PieceTable::fc_of_cp`] and the piece's Prm come from this span alone, + /// so a sequential walk searches the piece table once per piece. + pub(crate) fn span(&self, cp: u32) -> Option> { + let i = self.cps.partition_point(|&c| c <= cp).checked_sub(1)?; + let pcd = self.pieces.get(i)?; + let cp_lo = *self.cps.get(i)?; + let cp_hi = *self.cps.get(i + 1)?; + Some(PieceSpan { cp_lo, cp_hi, pcd }) + } + /// The CP just past the last piece: no text exists at or beyond it. pub(crate) fn cp_end(&self) -> u32 { self.cps.last().copied().unwrap_or(0) @@ -187,6 +192,31 @@ impl PieceTable { } } +/// One piece and the CPs `[cp_lo, cp_hi)` it covers ([`PieceTable::span`]). +#[derive(Clone, Copy)] +pub(crate) struct PieceSpan<'a> { + cp_lo: u32, + cp_hi: u32, + pcd: &'a Pcd, +} + +impl PieceSpan<'_> { + pub(crate) fn contains(&self, cp: u32) -> bool { + self.cp_lo <= cp && cp < self.cp_hi + } + + /// [`PieceTable::fc_of_cp`] for a CP inside the span. + pub(crate) fn fc_at(&self, cp: u32) -> Option { + 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) } + } + + /// The raw Prm of the piece: two CPs with the same value get the same [`PrmRef`]. + pub(crate) fn prm(&self) -> u16 { + self.pcd.prm + } +} + /// What a piece's `Prm` refers to. pub(crate) enum PrmRef<'a> { None, diff --git a/crates/docbin/src/tests.rs b/crates/docbin/src/tests.rs index 1128eca38a..3acc38cd1b 100644 --- a/crates/docbin/src/tests.rs +++ b/crates/docbin/src/tests.rs @@ -337,6 +337,15 @@ fn empty_document_gets_a_paragraph() { assert_eq!(doc.body.len(), 1); } +#[test] +fn a_doc_opens_in_word_97_2003_compatibility_mode() { + let f = text_doc(&[Piece { compressed: false, text: "body\r".into() }], &[], 0); + let doc = read(&f).expect("opens"); + // Word 97-2003 files are mode 11 in Word 2013+; mode 15 would change justification and + // line breaking. + assert_eq!(doc.settings.compat_mode, 11); +} + #[test] fn header_text_stays_out_of_body() { let f = text_doc( @@ -2880,3 +2889,153 @@ fn field_results_keep_their_text_and_runs() { p.objects ); } + +/// A one-page CHPX bin table over `page`: FCs 0..`end` live in page 1 of the returned stream. +fn one_page_bins(page: &[u8], end: u32) -> (Vec, crate::fkp::Bins) { + let mut word = vec![0u8; 512]; + word.extend_from_slice(page); + let plc = [0u32.to_le_bytes().as_slice(), end.to_le_bytes().as_slice(), 1u32.to_le_bytes().as_slice()].concat(); + let bins = crate::fkp::Bins::parse(&plc, 0, 12).expect("bins"); + (word, bins) +} + +/// The CHPX run a lookup reports covers exactly the FCs that give the same grpprl: the walk +/// reuses it for the following characters without asking again. +#[test] +fn a_chpx_run_covers_the_fcs_with_the_same_answer() { + let runs = [(0u32, grpprl(&[(0x0835, &[1])])), (5, Vec::new()), (9, grpprl(&[(0x0836, &[1])])), (30, grpprl(&[(0x0835, &[1])]))]; + let (word, bins) = one_page_bins(&chpx_fkp(&runs, 60), 0x2000); + for fc in (fc_of(0) - 4)..(fc_of(70)) { + let run = bins.chpx_run(&word, fc); + assert_eq!(run.grpprl, bins.chpx(&word, fc), "fc {fc}"); + for other in (fc_of(0) - 4)..(fc_of(70)) { + if run.contains(other) { + assert_eq!(bins.chpx(&word, other), run.grpprl, "fc {fc} run reused at {other}"); + } + } + } + assert!(bins.chpx_run(&word, fc_of(6)).contains(fc_of(8)) && !bins.chpx_run(&word, fc_of(6)).contains(fc_of(9))); +} + +/// A page whose run starts do not ascend (a corrupt file) gives the same answers as before and +/// a range that never claims an FC with a different answer. +#[test] +fn a_chpx_run_is_sound_on_a_page_whose_starts_do_not_ascend() { + let runs = [(10u32, grpprl(&[(0x0835, &[1])])), (3, grpprl(&[(0x0836, &[1])])), (20, Vec::new()), (7, grpprl(&[(0x0835, &[1])]))]; + let (word, bins) = one_page_bins(&chpx_fkp(&runs, 60), 0x2000); + for fc in (fc_of(0) - 4)..(fc_of(70)) { + let run = bins.chpx_run(&word, fc); + assert_eq!(run.grpprl, bins.chpx(&word, fc)); + for other in (fc_of(0) - 4)..(fc_of(70)) { + if run.contains(other) { + assert_eq!(bins.chpx(&word, other), run.grpprl, "fc {fc} run reused at {other}"); + } + } + } +} + +/// A piece span answers exactly what the per-CP lookup answers, in every piece (compressed +/// and UTF-16) and past the last. +#[test] +fn a_piece_span_matches_the_per_cp_lookups() { + let doc = text_doc( + &[ + Piece { compressed: true, text: "abc".into() }, + Piece { compressed: false, text: "d\u{e9}fgh".into() }, + Piece { compressed: true, text: "ij".into() }, + ], + &[], + 0, + ); + let mut comp = crate::open_container(&doc).expect("container"); + let table = crate::stream(&mut comp, "0Table").expect("table"); + let pieces = crate::piece::PieceTable::parse(&table, CLX_AT, table.len() as u32 - CLX_AT).expect("pieces"); + for cp in 0..14 { + let span = pieces.span(cp); + assert_eq!(span.and_then(|s| s.fc_at(cp)), pieces.fc_of_cp(cp), "cp {cp}"); + for other in 0..14 { + if span.is_some_and(|s| s.contains(other)) { + assert_eq!(pieces.span(other).and_then(|s| s.fc_at(other)), pieces.fc_of_cp(other)); + } + } + } + assert!(pieces.span(10).is_none(), "past the last piece"); +} + +/// Characters of one formatting share one `CharProps`: it is cloned when a run starts, and not +/// even compared while the formatting stays the same. +#[test] +fn shared_props_start_one_run_per_formatting() { + use wordcraft_doc::props::CharProps; + let bold = CharProps { bold: Some(true), ..Default::default() }; + let mut pb = super::ParaBuild::default(); + for _ in 0..100_000 { + pb.push_shared('a', &bold, 1); + } + pb.push_shared('b', &CharProps::default(), 2); + pb.push_shared('c', &bold, 3); + pb.push_shared('d', &bold, 4); + assert_eq!(pb.runs.iter().map(|r| r.len).collect::>(), [100_000, 1, 2], "equal props under a new generation join the run"); + pb.push('e', bold.clone()); + pb.push_shared('f', &bold, 4); + assert_eq!( + pb.runs.iter().map(|r| r.len).collect::>(), + [100_000, 1, 4], + "a plain push forgets the generation but still joins an equal run" + ); + assert_eq!(pb.text.len(), 100_005); +} + +/// Several bins, equal and descending breakpoints, a missing page and the last FC: wherever a +/// run is reported as valid it gives the answer a fresh lookup gives. +#[test] +fn chpx_runs_are_sound_across_bins() { + let page_a = chpx_fkp(&[(0, grpprl(&[(0x0835, &[1])])), (8, grpprl(&[(0x0836, &[1])]))], 20); + let page_b = chpx_fkp(&[(20, Vec::new()), (26, grpprl(&[(0x0835, &[1])]))], 40); + let mut word = vec![0u8; 512]; + word.extend_from_slice(&page_a); + word.extend_from_slice(&page_b); + let (a, b, end) = (fc_of(20), fc_of(40), fc_of(60)); + let plc = |fcs: &[u32], pns: &[u32]| { + [fcs.iter().flat_map(|f| f.to_le_bytes()).collect::>(), pns.iter().flat_map(|p| p.to_le_bytes()).collect()].concat() + }; + let tables = [ + ("two bins", plc(&[0, a, end], &[1, 2])), + ("an empty bin", plc(&[0, a, a, end], &[1, 2, 2])), + ("a missing page", plc(&[0, a, end], &[1, 9])), + ("descending breakpoints", plc(&[0, b, a, end], &[1, 2, 1])), + ("fcs up to the maximum", plc(&[0, a, u32::MAX], &[1, 2])), + ]; + for (what, t) in &tables { + let bins = crate::fkp::Bins::parse(t, 0, t.len() as u32).expect("bins"); + let probes: Vec = (0..fc_of(50)).step_by(2).chain([u32::MAX - 1, u32::MAX]).collect(); + for &fc in &probes { + let run = bins.chpx_run(&word, fc); + assert_eq!(run.grpprl, bins.chpx(&word, fc), "{what}: fc {fc}"); + for &other in &probes { + if run.contains(other) { + assert_eq!(bins.chpx(&word, other), run.grpprl, "{what}: fc {fc} run reused at {other}"); + } + } + } + } +} + +/// The same formatting changes through a whole read: runs break exactly where the CHPX changes +/// (with a field and a symbol character in between), and nowhere else. +#[test] +fn formatting_runs_break_where_the_chpx_changes() { + let spec = RichSpec { + text: "aaaabbbb\u{13}PAGE\u{14}12\u{15}ccdd(ee\r", + styles: vec![(1, 0x0FFF, "Normal", Vec::new(), Vec::new())], + fonts: vec!["Times New Roman", "Arial"], + chpx_runs: vec![(0, grpprl(&[(0x0835, &[1])])), (4, Vec::new()), (8, grpprl(&[(0x0835, &[1])])), (20, grpprl(&[(0x0836, &[1])]))], + papx_entries: vec![(30, 0, Vec::new())], + }; + let doc = read(&rich_doc(&spec)).expect("opens"); + let p = para0(&doc); + let shape: Vec<(usize, Option, Option)> = p.runs.iter().map(|r| (r.len, r.props.bold, r.props.italic)).collect(); + // "aaaa" bold, "bbbb" plain plus the field's anchor (3 bytes, default formatting), "ccd" bold + // (the run from the field to CP 20), "d(ee" italic. + assert_eq!(shape, [(4, Some(true), None), (7, None, None), (3, Some(true), None), (4, None, Some(true))], "{shape:?}"); +} diff --git a/crates/docbin/src/tests/drawing.rs b/crates/docbin/src/tests/drawing.rs index ae9ec9e3fe..dc18c3d372 100644 --- a/crates/docbin/src/tests/drawing.rs +++ b/crates/docbin/src/tests/drawing.rs @@ -183,6 +183,41 @@ fn shapes_keep_kind_colours_wrap_and_text_box_text() { assert_eq!(part.blocks.first().and_then(|b| b.as_para()).map(|p| p.text.as_str()), Some("Inside")); } +#[test] +fn braces_and_brackets_are_unfilled_unless_filled_is_set() { + // A right brace (88) in front of the text with no fill properties, and a left bracket (85) + // whose fFilled is set: only the bracket is filled. A plain rectangle (1) keeps its default + // white fill. + let brace = sp(&[fsp(88, 1026, 0xA00)]); + let bracket = sp(&[fsp(85, 1027, 0xA00), fopt(&[(0x01BF, (1 << 20) | (1 << 4))], &[])]); + let rect = sp(&[fsp(1, 1028, 0xA00)]); + let art = art_content(&[], &[brace, bracket, rect]); + let flags = spa_flags(0, 0, 3, false); + let spas = + plc(&[1, 2, 3, 5], &[spa(1026, [0, 0, 240, 1140], flags), spa(1027, [0, 0, 240, 1140], flags), spa(1028, [0, 0, 240, 1140], flags)].concat()); + let text = "A\u{8}\u{8}\u{8}B\r"; + let ccp = text.chars().count() as u32; + let spec = StructSpec { text, extra_pairs: vec![(40, spas), (50, art)], ..structured_spec(text, ccp) }; + let doc = read(&struct_doc(&spec)).expect("opens"); + let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para"); + let fills: Vec<_> = p0 + .objects + .iter() + .map(|o| match o { + InlineObject::Shape { fill, stroke, w, h, .. } => (fill.clone(), *stroke, *w, *h), + other => panic!("{other:?}"), + }) + .collect(); + assert_eq!( + fills, + [ + (None, Some(Rgb::BLACK), 12.0, 57.0), + (Some(Fill::solid(Rgb::WHITE)), Some(Rgb::BLACK), 12.0, 57.0), + (Some(Fill::solid(Rgb::WHITE)), Some(Rgb::BLACK), 12.0, 57.0), + ] + ); +} + #[test] fn text_boxes_pointing_at_the_same_story_text_read_it_once() { // Two text boxes whose PlcftxbxTxt stories are both [0, 7) (the table steps back to CP 0 diff --git a/crates/docx/src/package.rs b/crates/docx/src/package.rs index 3835bdc0b6..656a2ad274 100644 --- a/crates/docx/src/package.rs +++ b/crates/docx/src/package.rs @@ -350,6 +350,7 @@ impl Package { let path = resolve(dir, target); self.canonical(&path).map_or(path, str::to_string) }; + rels.ids.entry(id.to_string()).or_insert(rels.list.len()); rels.list.push(Rel { id: id.to_string(), kind: r.attr("Type").unwrap_or("").to_string(), target: resolved, external }); } rels @@ -405,11 +406,13 @@ pub struct Rel { #[derive(Clone, Debug, Default)] pub struct Rels { pub list: Vec, + /// Each id's first relationship in `list`, so a lookup doesn't scan every relationship. + ids: HashMap, } impl Rels { pub fn by_id(&self, id: &str) -> Option<&Rel> { - self.list.iter().find(|r| r.id == id) + self.ids.get(id).and_then(|&i| self.list.get(i)) } pub fn by_type(&self, full: &str) -> Option<&Rel> { self.list.iter().find(|r| rel_is(&r.kind, full)) @@ -547,6 +550,22 @@ mod tests { assert_eq!(crate::package_title(std::io::Cursor::new(z), 1 << 20).as_deref(), Some("Plan")); } + #[test] + fn relationships_are_looked_up_by_id_the_first_one_winning() { + let mut rels = String::from(r#""#); + for i in 0..20_000 { + rels.push_str(&format!(r#""#)); + } + rels.push_str(r#""#); + let z = zip_of(&[("word/_rels/document.xml.rels", rels.into_bytes())]); + let r = Package::open(&z).unwrap().rels("word/document.xml"); + assert_eq!(r.by_id("rId7").map(|r| r.target.as_str()), Some("https://example.com/7"), "the first rId7 wins"); + assert_eq!(r.by_id("rId19999").map(|r| r.target.as_str()), Some("https://example.com/19999")); + assert!(r.by_id("rId20000").is_none()); + // Hyperlink-style lookups, one per relationship, are each a map lookup. + assert_eq!((0..20_000).filter(|i| r.by_id(&format!("rId{i}")).is_some()).count(), 20_000); + } + #[test] fn decompression_bomb_is_rejected() { let z = zip_of(&[("word/document.xml", vec![b' '; 2_000_000])]); diff --git a/crates/docx/src/read/chart/model.rs b/crates/docx/src/read/chart/model.rs index a3ee99637a..71ee7040f6 100644 --- a/crates/docx/src/read/chart/model.rs +++ b/crates/docx/src/read/chart/model.rs @@ -66,6 +66,9 @@ pub(super) struct Axis { pub(super) struct Series { pub(super) name: String, + /// The series' `c:idx` (an `xsd:unsignedInt`), which picks its automatic colour; `None` + /// when missing or not a valid number. + pub(super) idx: Option, pub(super) color: Option, pub(super) line_w: f32, /// Whether the series is drawn as a line (scatter series without one get markers). diff --git a/crates/docx/src/read/chart/parse.rs b/crates/docx/src/read/chart/parse.rs index 737effde32..3c09fcfe9d 100644 --- a/crates/docx/src/read/chart/parse.rs +++ b/crates/docx/src/read/chart/parse.rs @@ -91,8 +91,11 @@ impl Chart { self.accents.get(i % 6).copied().unwrap_or(Rgb::BLACK) } + /// Colour of series `j`: its own, else the accent its `c:idx` picks (as Word does; `c:order` + /// doesn't matter), else the one its place in the file picks. pub(super) fn series_colour(&self, j: usize) -> Rgb { - self.series.get(j).and_then(|s| s.color).unwrap_or_else(|| self.accent(j)) + let s = self.series.get(j); + s.and_then(|s| s.color).unwrap_or_else(|| self.accent(s.and_then(|s| s.idx).unwrap_or(j))) } /// Colour of point `i` of series `j`: its `c:dPt` override, else the varied accent (pie) or @@ -199,6 +202,7 @@ pub(super) fn text_size(space: &El) -> f32 { impl Series { fn parse(ser: &El, scatter: bool, theme: &[Rgb], cap: usize) -> Series { let name = ser.child("c:tx").and_then(|t| t.find("c:v")).map(|v| label(v.text().trim())).unwrap_or_default(); + let idx = ser.child("c:idx").and_then(|e| e.attr("val")).and_then(|v| v.trim().parse::().ok()).and_then(|v| usize::try_from(v).ok()); let sp = ser.child("c:spPr"); let ln = sp.and_then(|p| p.child("a:ln")); let marker = ser.child("c:marker").and_then(|m| m.child("c:spPr")).and_then(|p| fill_colour(p, theme)); @@ -219,7 +223,7 @@ impl Series { } } let labels = show_values(ser.child("c:dLbls")); - Series { name, color, line_w, line, values, xs, points, fmt, x_fmt, labels, raw: Vec::new() } + Series { name, idx, color, line_w, line, values, xs, points, fmt, x_fmt, labels, raw: Vec::new() } } } diff --git a/crates/docx/src/read/chart/tests.rs b/crates/docx/src/read/chart/tests.rs index 4e064e9a85..b943f1cfd5 100644 --- a/crates/docx/src/read/chart/tests.rs +++ b/crates/docx/src/read/chart/tests.rs @@ -528,3 +528,65 @@ fn negative_sections_and_scientific_numbers() { assert_eq!(format_value(0.0, "0.00E+00", 1.0), "0.00E+00"); assert_eq!(format_value(-12345.0, "0.0E-00", 1.0), "-1.2E04"); } + +// An automatic series colour follows the series' `c:idx`, not its place in the file (#5963), as +// measured in Word: `idx` 3 then 0 are accent4 then accent1, a lone `idx` 2 is accent3, and +// `c:order` changes nothing. + +/// The fill colours of the bars of a column chart of `sers`, and of its legend swatches. +fn column_colours(sers: &[String]) -> (Vec, Vec) { + let plot = format!( + r#"{}"#, + sers.concat() + ); + let items = with_legend(&plot, "r", 300.0, 200.0); + let bars = filled(&items).iter().map(|b| b.0).collect(); + let keys = items + .iter() + .filter_map(|it| if let GraphicItem::Shape { rect, fill, .. } = it { (rect[2] == SWATCH).then_some(*fill).flatten() } else { None }) + .collect(); + (bars, keys) +} + +/// `ser` with its `c:idx` / `c:order` replaced by `idx_order` (an empty string drops them). +fn renumbered(ser: &str, idx_order: &str) -> String { + let start = ser.find("").expect("ser writes c:tx"); + format!("{}{idx_order}{}", &ser[..start], &ser[end..]) +} + +#[test] +fn automatic_series_colours_follow_c_idx() { + let accent = |k: usize| theme()[4 + k]; + let a = ser(0, "A", None, &["Q1"], &["1"]); + let b = ser(1, "B", None, &["Q1"], &["2"]); + // idx 3 first, idx 0 second: accent4, accent1, in the bars and the legend. + let sers = [renumbered(&a, r#""#), renumbered(&b, r#""#)]; + assert_eq!(column_colours(&sers), (vec![accent(3), accent(0)], vec![accent(3), accent(0)])); + // A lone series with idx 2 is accent3. + let lone = [renumbered(&a, r#""#)]; + assert_eq!(column_colours(&lone).0, [accent(2)]); + // c:order doesn't matter: idx 0 and 1 stay accent1 and accent2 with their orders swapped. + let swapped = [renumbered(&a, r#""#), renumbered(&b, r#""#)]; + assert_eq!(column_colours(&swapped).0, [accent(0), accent(1)]); + // A series' own colour still wins. + let own = [renumbered(&ser(0, "A", Some("FF0000"), &["Q1"], &["1"]), r#""#)]; + assert_eq!(column_colours(&own).0, [Rgb(0xFF, 0, 0)]); +} + +#[test] +fn series_without_a_usable_c_idx_take_their_place() { + let accent = |k: usize| theme()[4 + k]; + let a = ser(0, "A", None, &["Q1"], &["1"]); + let b = ser(1, "B", None, &["Q1"], &["2"]); + // No c:idx at all: the place in the file. + assert_eq!(column_colours(&[renumbered(&a, ""), renumbered(&b, "")]).0, [accent(0), accent(1)]); + // Junk, negative and out-of-range values: no panic, the place in the file. + for junk in ["x", "-1", "1e3", "99999999999999999999999"] { + let sers = [renumbered(&a, &format!(r#""#)), renumbered(&b, &format!(r#""#))]; + assert_eq!(column_colours(&sers).0, [accent(0), accent(1)], "{junk:?}"); + } + // The largest unsignedInt `c:idx` allows draws, without a panic. + let big = [renumbered(&a, &format!(r#""#, u32::MAX))]; + assert_eq!(column_colours(&big).0.len(), 1); +} diff --git a/crates/docx/src/read/embed.rs b/crates/docx/src/read/embed.rs index 1cc97bb038..a2a91f4e57 100644 --- a/crates/docx/src/read/embed.rs +++ b/crates/docx/src/read/embed.rs @@ -239,7 +239,13 @@ impl Reader<'_> { /// Keep one part's bytes and content type (not its relationships). fn keep_part(&mut self, part: &str) -> bool { let lower = part.to_ascii_lowercase(); - if !manifest_field_ok(part) || lower.ends_with(".rels") || lower == "[content_types].xml" || part.eq_ignore_ascii_case(&self.main) { + // `wordcraft:` names are WordCraft's own bookkeeping in `passthrough`, never a file's part. + if !manifest_field_ok(part) + || lower.ends_with(".rels") + || lower == "[content_types].xml" + || lower.starts_with("wordcraft:") + || part.eq_ignore_ascii_case(&self.main) + { log::warn!("docx: not carrying part {part:?} for an embedded object"); return false; } @@ -388,6 +394,23 @@ mod tests { assert!(roots.contains("keep.bin"), "the part is dropped: {roots}"); } + /// A part named like WordCraft's own bookkeeping (`wordcraft:unmodelled.roots`) is not carried: + /// kept, it would be read back as that bookkeeping and add relationships the file chose. + #[test] + fn a_part_named_like_bookkeeping_is_not_carried() { + let forged = b"D\thttp://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\tword/evil.xml\n"; + let bytes = package( + r#"text"#, + &[("rIdDoc1", "/wordcraft:unmodelled.roots"), ("rIdDoc2", "/wordcraft:embedded.parts")], + &[("wordcraft:unmodelled.roots", forged), ("wordcraft:embedded.parts", b"P\tword/evil.xml\tapplication/xml\n")], + ); + let doc = crate::read(&bytes).unwrap(); + let roots = unmodelled_roots(&doc); + assert!(!roots.contains("wordcraft:") && !roots.contains("evil"), "{roots}"); + let manifest = doc.passthrough.get(crate::package::EMBEDDED_PARTS).map(|v| String::from_utf8_lossy(v).to_string()).unwrap_or_default(); + assert!(!manifest.contains("evil"), "{manifest}"); + } + /// An unqualified attribute is a relationship only where the markup names a relationship /// that way: a hyperlink's `id` names the part, a VML shape's `id` is its own. #[test] diff --git a/crates/docx/src/read/parts.rs b/crates/docx/src/read/parts.rs index 2e1f98e32f..ce4184dc7d 100644 --- a/crates/docx/src/read/parts.rs +++ b/crates/docx/src/read/parts.rs @@ -55,6 +55,8 @@ impl Reader<'_> { .children("w:font") .filter(|f| f.child("w:family").and_then(|v| v.attr("w:val")) == Some("roman")) .filter_map(|f| f.attr("w:name")) + .flat_map(|n| n.split(';')) + .map(str::trim) .filter(|n| !n.is_empty() && n.len() <= MAX_FONT_NAME) .take(MAX_TABLE_FONTS) .map(str::to_string) diff --git a/crates/docx/src/read/props.rs b/crates/docx/src/read/props.rs index 4709916b7d..2a218b2c47 100644 --- a/crates/docx/src/read/props.rs +++ b/crates/docx/src/read/props.rs @@ -5,16 +5,17 @@ use std::collections::HashMap; use wordcraft_doc::para::cap_alt; use wordcraft_doc::props::{ Align, Border, BorderStyle, Borders, CellProps, CharProps, CombineBrackets, EastAsianLayout, HeightRule, Highlight, LineSpacing, NumRef, - ParaProps, Rgb, RowProps, TabAlign, TabLeader, TabStop, TableFloat, TableLook, TableProps, TextAlignment, TextColor, TextDirection, Underline, - VAlign, VMerge, VertAlign, + ParaProps, ParagraphTextDirection, Rgb, RowProps, TabAlign, TabLeader, TabStop, TableFloat, TableLook, TableProps, TextAlignment, TextColor, + TextDirection, Underline, VAlign, VMerge, VertAlign, }; use wordcraft_doc::section::{ Columns, DocGrid, DocGridType, LineNumberRestart, LineNumbering, MAX_CHAR_SPACE, MAX_GRID_PITCH, NotePos, NoteProps, NoteRestart, NumFormat, PageVAlign, SectionProps, SectionStart, }; +use super::MAX_FONT_NAME; use super::drawing_color::adjust_lum; -use crate::units::{int, line_multiple, measure, on_off, tw, u32_of}; +use crate::units::{int, line_multiple, measure, on_off, tw, tw_i16, tw_u16, u32_of}; /// A text frame (`w:framePr` without a drop cap, ECMA-376 §17.3.1.11), sizes and offsets in /// points (clamped), names kept as OOXML writes them. @@ -145,6 +146,29 @@ pub(crate) fn resolve_level_only(sheet: &wordcraft_doc::styles::StyleSheet, p: & } } +/// A `w:rFonts` value as the document keeps it: `None` when it names no family, else its leading +/// families up to [`MAX_FONT_NAME`] bytes, cut at a `;` so no name is split. A LibreOffice file +/// names its Latin font as a semicolon-separated fallback list, and a real list names a few +/// families; a longer value is corrupt or hostile, and every piece of text drawn in it would pay +/// for its whole length as the list is classified and resolved. Keeping the leading families keeps +/// the family such a list picks, and the rest of the value is dropped like `w:cs`/`w:eastAsia` +/// drop an over-long name. +fn bounded_font_list(v: &str) -> Option { + if v.len() <= MAX_FONT_NAME { + return (!v.is_empty()).then(|| v.to_string()); + } + let mut end = 0usize; + for part in v.split_inclusive(';') { + let next = end + part.len(); + if next > MAX_FONT_NAME { + break; + } + end = next; + } + let kept = v.get(..end)?.trim_end_matches(';'); + (!kept.is_empty()).then(|| kept.to_string()) +} + impl PropCtx { pub fn style_id(&self, id: &str) -> String { self.alias.get(id).cloned().unwrap_or_else(|| id.to_string()) @@ -226,6 +250,8 @@ impl PropCtx { shd_fill(e) } + /// The character properties `e` (a `w:rPr`) sets: direct formatting, plus the fonts, size, + /// weight and posture of the complex-script and East Asian text beside them. pub fn rpr(&self, e: &El) -> CharProps { let mut c = CharProps::default(); for k in e.els() { @@ -234,7 +260,11 @@ impl PropCtx { "w:rFonts" => { // A theme font supersedes the fixed font beside it (`w:asciiTheme` over // `w:ascii`, `w:hAnsiTheme` over `w:hAnsi`). - let named = |a: &str| k.attr(a).filter(|f| !f.is_empty()).map(str::to_string); + // A LibreOffice file names the Latin font as a semicolon-separated fallback + // list. Names are bounded like `w:cs`/`w:eastAsia` below: a longer one is + // corrupt or hostile, and every piece of text drawn in it would pay for its + // whole length as the list is classified (see `bounded_font_list`). + let named = |a: &str| k.attr(a).and_then(bounded_font_list); let themed = |a: &str| k.attr(a).and_then(|t| self.theme_font(t)); // `w:eastAsia` and `w:cs` name fonts for other scripts and stay in `font_ea` / // `font_cs`: `w:hint` only picks between them for ambiguous characters, never for @@ -338,6 +368,7 @@ impl PropCtx { "w:suppressAutoHyphens" => p.suppress_hyphens = Some(on_off(k)), "w:suppressLineNumbers" => p.suppress_line_numbers = Some(on_off(k)), "w:bidi" => p.bidi = Some(on_off(k)), + "w:textDirection" => p.text_direction = k.attr("w:val").and_then(ParagraphTextDirection::from_ooxml), "w:kinsoku" => p.kinsoku = Some(on_off(k)), "w:wordWrap" => p.word_wrap = Some(on_off(k)), "w:overflowPunct" => p.overflow_punct = Some(on_off(k)), @@ -376,10 +407,10 @@ impl PropCtx { p.tabs = Some(tabs); } "w:spacing" => { - if let Some(v) = tw(k, "w:before") { + if let Some(v) = tw_u16(k, "w:before") { p.space_before = Some(v.max(0.0)); } - if let Some(v) = tw(k, "w:after") { + if let Some(v) = tw_u16(k, "w:after") { p.space_after = Some(v.max(0.0)); } // HTML-style automatic spacing; "0" switches an inherited one off. @@ -402,15 +433,15 @@ impl PropCtx { } } "w:ind" => { - if let Some(v) = tw(k, "w:left").or_else(|| tw(k, "w:start")) { + if let Some(v) = tw_i16(k, "w:left").or_else(|| tw_i16(k, "w:start")) { p.indent_left = Some(v); } - if let Some(v) = tw(k, "w:right").or_else(|| tw(k, "w:end")) { + if let Some(v) = tw_i16(k, "w:right").or_else(|| tw_i16(k, "w:end")) { p.indent_right = Some(v); } - if let Some(v) = tw(k, "w:hanging") { + if let Some(v) = tw_i16(k, "w:hanging") { p.indent_first = Some(-v); - } else if let Some(v) = tw(k, "w:firstLine") { + } else if let Some(v) = tw_i16(k, "w:firstLine") { p.indent_first = Some(v); } // Character units (hundredths of a character), which win when non-zero. diff --git a/crates/docx/src/read/story/drawing.rs b/crates/docx/src/read/story/drawing.rs index 3a69dcc4e3..75435a22a7 100644 --- a/crates/docx/src/read/story/drawing.rs +++ b/crates/docx/src/read/story/drawing.rs @@ -41,6 +41,7 @@ impl Reader<'_> { } let source = self.keep_embedded(c.child("a:graphic")?, rels, &[])?; float.set_spin(xfrm_spin(content.child("w14:xfrm"))); + float.bare_turn = !anchored && float.rot != 0.0 && float.effect == [0.0; 4]; let (px, py) = float.spin_pad(w, h); for (e, p) in float.effect.iter_mut().zip([px, py, px, py]) { if *e != 0.0 { @@ -106,7 +107,11 @@ impl Reader<'_> { self.set_shape_id(&mut obj, c.child("wp:docPr").and_then(|p| p.attr("id"))); // Word's effect extent also covers a rotated object's overhang; the model keeps that // apart (it follows from the angle), so take it back out. + // Shadow or glow room the file didn't record: saving records it (and the turn's room). + let fx_room = matches!(&obj, InlineObject::Shape { effects, .. } if effects.extent() != [0.0; 4]); if let Some(f) = obj.float_mut() { + // None recorded: Word keeps an inline one's line to the frame (see `Float::bare_turn`). + f.bare_turn = !anchored && !fx_room && f.rot != 0.0 && f.effect == [0.0; 4]; let (px, py) = f.spin_pad(w, h); for (e, p) in f.effect.iter_mut().zip([px, py, px, py]) { // Zero adds no effects room when rotation contracts this axis. @@ -118,9 +123,33 @@ impl Reader<'_> { Some(obj) } + /// The hyperlink Word keeps on a drawing itself: its `wp:docPr`'s `a:hlinkClick` (ECMA-376 + /// Part 1 §20.4.2.5, §21.1.2.3.5) naming an external hyperlink relationship. Word stores a + /// picture's link there rather than in a `w:hyperlink` around its run (#5267). Its `#name` + /// part, when the address has one, stays part of the address. + pub(super) fn drawing_link(d: &El, rels: &Rels) -> Option { + let frame = d.child("wp:inline").or_else(|| d.child("wp:anchor"))?; + let id = frame.child("wp:docPr")?.child("a:hlinkClick")?.attr("r:id")?; + let rel = rels.by_id(id).filter(|r| r.external && crate::package::rel_is(&r.kind, rt::HYPERLINK))?; + Some(rel.target.trim().to_string()).filter(|u| !u.is_empty()) + } + + /// Native ink or an Office 2016 chart kept with its fallback (`mc:AlternateContent`), and the + /// [`Self::drawing_link`] of the choice it was read from, else of its fallback: another + /// choice's link is not this object's. + pub(super) fn read_alternate(&mut self, sc: &mut StoryCtx, ac: &El, rels: &Rels) -> Option<(InlineObject, Option)> { + let (object, choice) = match self.read_ink_alternate(sc, ac, rels) { + Some(o) => (o, children_of_choice(ac).filter(|e| e.name == "mc:Choice")), + None => (self.read_chartex_alternate(sc, ac, rels)?, chartex_choice(ac)), + }; + let link = + [choice, ac.child("mc:Fallback")].into_iter().flatten().filter_map(|e| e.child("w:drawing")).find_map(|d| Self::drawing_link(d, rels)); + Some((object, link)) + } + /// Keep the selected native-ink branch and its picture fallback as one object. Reading only /// the fallback flattens the ink; reading only the choice loses its preview on the next save. - pub(super) fn read_ink_alternate(&mut self, sc: &mut StoryCtx, ac: &El, rels: &Rels) -> Option { + fn read_ink_alternate(&mut self, sc: &mut StoryCtx, ac: &El, rels: &Rels) -> Option { let choice = children_of_choice(ac).filter(|e| e.name == "mc:Choice")?; let drawing = choice.child("w:drawing")?; if drawing.find("a:graphicData")?.attr("uri") != Some(INK_URI) { @@ -149,10 +178,8 @@ impl Reader<'_> { /// An Office 2016 chart (`cx:chart`: waterfall, sunburst, box & whisker…) drawn from its /// fallback picture, keeping both branches so saving writes the chart back rather than only /// the picture. `None` (read the fallback alone) when it can't be kept whole. - pub(super) fn read_chartex_alternate(&mut self, sc: &mut StoryCtx, ac: &El, rels: &Rels) -> Option { - let choice = ac - .children("mc:Choice") - .find(|c| c.child("w:drawing").and_then(|d| d.find("a:graphicData")).and_then(|g| g.attr("uri")) == Some(CHARTEX_URI))?; + fn read_chartex_alternate(&mut self, sc: &mut StoryCtx, ac: &El, rels: &Rels) -> Option { + let choice = chartex_choice(ac)?; // A requirement in a namespace we can't name couldn't be written back. let requires = choice.attr("Requires").unwrap_or(""); if requires.split_whitespace().next().is_none() || requires.split_whitespace().any(|p| p == "?") { @@ -563,6 +590,11 @@ impl Reader<'_> { } } +/// The `mc:Choice` holding an Office 2016 chart (`cx:chart`) in an `mc:AlternateContent`. +fn chartex_choice(ac: &El) -> Option<&El> { + ac.children("mc:Choice").find(|c| c.child("w:drawing").and_then(|d| d.find("a:graphicData")).and_then(|g| g.attr("uri")) == Some(CHARTEX_URI)) +} + /// The alternative text of a `wp:docPr`, `pic:cNvPr` or `wps:cNvPr` (ECMA-376 /// `CT_NonVisualDrawingProps`): the description (the title when there is none, as before), and /// the title and decorative mark (the `adec:decorative` extension of [MS-ODRAWXML]). diff --git a/crates/docx/src/read/story/runs.rs b/crates/docx/src/read/story/runs.rs index 9dec4c5b48..d43cc3a214 100644 --- a/crates/docx/src/read/story/runs.rs +++ b/crates/docx/src/read/story/runs.rs @@ -98,9 +98,9 @@ impl Reader<'_> { } } "mc:AlternateContent" => { - if let Some(o) = self.read_ink_alternate(sc, k, rels).or_else(|| self.read_chartex_alternate(sc, k, rels)) { + if let Some((o, link)) = self.read_alternate(sc, k, rels) { let props = self.run_props_none(ctx); - self.emit_obj(sc, pb, o, &props); + self.emit_linked_obj(sc, pb, o, &props, link); } else if let Some(c) = children_of_choice(k) { self.read_inline_children(sc, pb, c, rels, ctx, depth + 1); } @@ -237,7 +237,7 @@ impl Reader<'_> { }, "w:drawing" => { if let Some(o) = self.read_drawing(sc, k, rels) { - self.emit_obj(sc, pb, o, props); + self.emit_linked_obj(sc, pb, o, props, Self::drawing_link(k, rels)); } } "w:pict" => { @@ -298,8 +298,8 @@ impl Reader<'_> { ), "w:ruby" => self.read_ruby(sc, pb, k, props), "mc:AlternateContent" => { - if let Some(o) = self.read_ink_alternate(sc, k, rels).or_else(|| self.read_chartex_alternate(sc, k, rels)) { - self.emit_obj(sc, pb, o, props); + if let Some((o, link)) = self.read_alternate(sc, k, rels) { + self.emit_linked_obj(sc, pb, o, props, link); } else if let Some(c) = children_of_choice(k) { for x in c.els() { self.read_run_child(sc, pb, x, (props, rels), note, depth + 1); @@ -376,6 +376,14 @@ impl Reader<'_> { } } + /// [`Self::emit_obj`] for a drawing that carries its own `link`: a link around the run wins. + fn emit_linked_obj(&mut self, sc: &mut StoryCtx, pb: &mut PB, o: InlineObject, props: &CharProps, link: Option) { + match link.filter(|_| props.link.is_none()) { + Some(link) => self.emit_obj(sc, pb, o, &CharProps { link: Some(link), ..props.clone() }), + None => self.emit_obj(sc, pb, o, props), + } + } + pub(super) fn emit_obj(&mut self, sc: &mut StoryCtx, pb: &mut PB, o: InlineObject, props: &CharProps) { match sink(sc) { Some(f) => { diff --git a/crates/docx/src/read/story/vml.rs b/crates/docx/src/read/story/vml.rs index b625b8d4f0..e40d38b3a7 100644 --- a/crates/docx/src/read/story/vml.rs +++ b/crates/docx/src/read/story/vml.rs @@ -224,6 +224,15 @@ impl Reader<'_> { { let id = self.read_textbox(sc, t, rels); if let Some(part) = self.doc.parts.get_mut(&id) { + // Text direction (#5193), as Word reads it: `mso-layout-flow-alt:bottom-to-top` is + // `vert270` whatever `layout-flow` says; otherwise `layout-flow:vertical…` (also + // `vertical-ideographic`, our `eaVert`) is `vert`, and anything else horizontal. + let tb_style = tb.attr("style").unwrap_or(""); + if vml_style(tb_style, "mso-layout-flow-alt").is_some_and(|v| v.eq_ignore_ascii_case("bottom-to-top")) { + part.body.vert = wordcraft_doc::props::TextVert::Vert270; + } else if vml_style(tb_style, "layout-flow").is_some_and(|v| v.to_ascii_lowercase().starts_with("vertical")) { + part.body.vert = wordcraft_doc::props::TextVert::Vert; + } // Internal margins `inset="l,t,r,b"` (ECMA-376 Part 4 §19.1.2.22), each one left // out keeping Word's default; a bare number is in EMUs, VML's base unit. // `o:insetmode="auto"` leaves them to the application. diff --git a/crates/docx/src/read/styles.rs b/crates/docx/src/read/styles.rs index 878edddf98..6e972a7604 100644 --- a/crates/docx/src/read/styles.rs +++ b/crates/docx/src/read/styles.rs @@ -2,6 +2,7 @@ use super::{ AbstractNum, DocxError, El, Level, LevelSuffix, Num, NumFormat, PartFormat, Reader, Style, StyleKind, StyleSheet, TablePart, TableStyleParts, VAlign, flag, int, props, tw, u32_of, }; +use crate::units::tw_i16; use std::sync::Arc; /// Built-in style names Word stores in lower case, and how the UI shows them. @@ -317,12 +318,12 @@ impl Reader<'_> { lv.align = props::align(j); } if let Some(ind) = l.child("w:pPr").and_then(|p| p.child("w:ind")) { - if let Some(v) = tw(ind, "w:left").or_else(|| tw(ind, "w:start")) { + if let Some(v) = tw_i16(ind, "w:left").or_else(|| tw_i16(ind, "w:start")) { lv.indent = v; } - if let Some(h) = tw(ind, "w:hanging") { + if let Some(h) = tw_i16(ind, "w:hanging") { lv.hanging = h; - } else if let Some(f) = tw(ind, "w:firstLine") { + } else if let Some(f) = tw_i16(ind, "w:firstLine") { lv.hanging = -f; } } diff --git a/crates/docx/src/read/toc.rs b/crates/docx/src/read/toc.rs index 1c246732a2..3516044616 100644 --- a/crates/docx/src/read/toc.rs +++ b/crates/docx/src/read/toc.rs @@ -83,13 +83,14 @@ pub(super) fn toc_control_to_toc(body: &mut Blocks) { } // An empty paragraph opening the control takes the field. It may end a section, as above // entries in columns (#3889). - if engine.get(s).and_then(|b| b.as_para()).is_some_and(|p| p.is_empty()) { + let empty_heading = engine.get(s).and_then(|b| b.as_para()).is_some_and(|p| p.is_empty()); + if empty_heading { field_to_heading(&mut engine, s + 1); } else { toc_field_to_heading(&mut engine); } if crate::write::written_toc_span(&engine) == Some((s, last)) { - *body = toc; + *body = if empty_heading { toc } else { engine }; } } @@ -97,7 +98,7 @@ pub(super) fn toc_control_to_toc(body: &mut Blocks) { /// reproduce exactly. Other imported TOCs keep their explicit boundaries, independent of styles. pub(super) fn toc_field_to_heading(body: &mut Blocks) { for i in 1..body.len() { - if body[i - 1].as_para().is_some_and(|p| p.props.style.as_deref() == Some("TOCHeading")) { + if body.get(i - 1).and_then(|b| b.as_para()).is_some_and(|p| p.props.style.as_deref() == Some("TOCHeading")) { field_to_heading(body, i); } } diff --git a/crates/docx/src/units.rs b/crates/docx/src/units.rs index c9862fb047..2e5dfb0cd5 100644 --- a/crates/docx/src/units.rs +++ b/crates/docx/src/units.rs @@ -116,6 +116,27 @@ pub fn tw(e: &El, attr: &str) -> Option { e.attr(attr).and_then(|v| measure(v, 20.0)) } +/// Signed twips Word keeps in 16 bits (paragraph and list indents, ECMA-376 Part 1 §17.3.1.12): +/// a plain integer outside `i16` keeps its low 16 bits, two's complement, as Word reads it +/// (66256 → 720). Values with a unit or a fraction read as [`tw`]. +pub fn tw_i16(e: &El, attr: &str) -> Option { + let v = e.attr(attr)?; + match v.trim().parse::() { + Ok(i) => Some(f32::from(i as i16) / 20.0), + Err(_) => measure(v, 20.0), + } +} + +/// Unsigned twips Word keeps in 16 bits (paragraph spacing, §17.3.1.33): a plain integer above +/// 65535 keeps its low 16 bits (65537 → 1). Other values read as [`tw`]. +pub fn tw_u16(e: &El, attr: &str) -> Option { + let v = e.attr(attr)?; + match v.trim().parse::() { + Ok(i) if i > 0xFFFF => Some(f32::from((i & 0xFFFF) as u16) / 20.0), + _ => measure(v, 20.0), + } +} + /// ST_OnOff: absent `w:val` = on; `0`, `false`, `off` = off. pub fn on_off(e: &El) -> bool { match e.attr("w:val") { @@ -148,6 +169,24 @@ pub fn emu(pt: f32) -> String { mod tests { use super::*; + #[test] + fn twips_beyond_16_bits_wrap() { + let el = |a: &str| El { attrs: vec![("w:v".into(), a.into())], ..El::default() }; + let s = |a: &str| tw_i16(&el(a), "w:v"); + let u = |a: &str| tw_u16(&el(a), "w:v"); + assert_eq!(s("66256"), Some(36.0)); + assert_eq!(s("-65516"), Some(1.0)); + assert_eq!(s("98304"), Some(-1638.4)); + assert_eq!(s("32767"), Some(1638.35)); + assert_eq!(s("-32768"), Some(-1638.4)); + assert_eq!(s("1in"), Some(72.0)); + assert_eq!(s("x"), None); + assert_eq!(u("65537"), Some(0.05)); + assert_eq!(u("65535"), Some(3276.75)); + assert_eq!(u("12pt"), Some(12.0)); + assert_eq!(u("-20"), Some(-1.0)); + } + #[test] fn measures() { assert_eq!(measure("1440", 20.0), Some(72.0)); diff --git a/crates/docx/src/write/embed.rs b/crates/docx/src/write/embed.rs index cc13df58ee..d8f40a209e 100644 --- a/crates/docx/src/write/embed.rs +++ b/crates/docx/src/write/embed.rs @@ -353,11 +353,11 @@ impl super::Writer<'_> { }; self.embeds.markup_bytes = left; let mut root = xml::parse(src.xml.as_bytes()).ok()?; - let mut used = Vec::new(); + let mut used = HashSet::new(); rel_attr_values(&root, &mut used, 0); // Every relationship the markup uses must be writable, or the object would be broken. - let writable = |id: &String| src.refs.iter().any(|r| r.id == *id && (r.external || self.embeds.available(&r.target))); - if !used.iter().all(writable) { + let writable: HashSet<&str> = src.refs.iter().filter(|r| r.external || self.embeds.available(&r.target)).map(|r| r.id.as_str()).collect(); + if !used.iter().all(|id| writable.contains(id.as_str())) { log::warn!("docx: an embedded object's parts are missing; it is left out"); return None; } @@ -499,8 +499,9 @@ impl super::Writer<'_> { } } "wp:effectExtent" => { - // Word's effect extent also covers a rotated object's overhang (its rotated bounds). - let (px, py) = float.spin_pad(w, h); + // Word's effect extent also covers a rotated object's overhang (its rotated bounds), + // unless the file it came from kept none. + let (px, py) = if float.is_bare_turn() { (0.0, 0.0) } else { float.spin_pad(w, h) }; let [el, et, er, eb] = float.effect_extent(); let ext = [el + px, et + py, er + px, eb + py].map(emu); e.attrs.retain(|(k, _)| !matches!(k.as_str(), "l" | "t" | "r" | "b")); @@ -637,13 +638,15 @@ fn set_attr(e: &mut El, k: &str, v: &str) { } } -fn rel_attr_values(e: &El, out: &mut Vec, depth: usize) { +/// The distinct relationship ids `e` uses: a set, so a large tree (a native document's markup +/// never went through the reader's id limit) costs no scan per attribute. +fn rel_attr_values(e: &El, out: &mut HashSet, depth: usize) { if depth > MAX_DEPTH { return; } for (k, v) in &e.attrs { if xml::is_rel_attr(&e.name, k) && !out.contains(v) { - out.push(v.clone()); + out.insert(v.clone()); } } for c in e.els() { @@ -851,6 +854,24 @@ fn valid_part_name(name: &str) -> String { mod tests { use super::*; + #[test] + fn relationship_ids_are_collected_once_each() { + // A native document's embedded markup skips the reader's id limit: 40,000 distinct ids + // (each named twice) were a scan of every id collected so far, per attribute. + let mut xml = String::from( + r#""#, + ); + for i in 0..40_000 { + xml.push_str(&format!(r#""#)); + } + xml.push_str(""); + let root = xml::parse(xml.as_bytes()).unwrap(); + let mut used = HashSet::new(); + rel_attr_values(&root, &mut used, 0); + assert_eq!(used.len(), 40_000); + assert!(used.contains("rId39999")); + } + #[test] fn charts_cut_to_the_limits_on_save_say_so_once() { // A model deserialised as is can hold more than a chart part keeps; the save left the rest diff --git a/crates/docx/src/write/props.rs b/crates/docx/src/write/props.rs index 3968377df4..d22a297a97 100644 --- a/crates/docx/src/write/props.rs +++ b/crates/docx/src/write/props.rs @@ -287,6 +287,9 @@ pub fn ppr_inner(w: &mut W, p: &ParaProps, framed: bool, num_change: Option<&Cha if let Some(a) = p.align { w.val("w:jc", align_val(a)); } + if let Some(direction) = p.text_direction { + w.val("w:textDirection", direction.ooxml()); + } if let Some(t) = p.text_alignment { w.val("w:textAlignment", t.ooxml()); } diff --git a/crates/docx/src/write/sdt.rs b/crates/docx/src/write/sdt.rs index 0f8f5a4a92..1b0f4d401b 100644 --- a/crates/docx/src/write/sdt.rs +++ b/crates/docx/src/write/sdt.rs @@ -86,11 +86,28 @@ pub fn plan(bl: &Blocks) -> Plan<'_> { && (0..off).step_by(OBJ.len_utf8()).all(|o| matches!(p.object_at(o), Some(wordcraft_doc::InlineObject::ControlStart { .. }))) }) }; + // Bookmark, comment and permission markers after the end may stay there: a body-level + // bookmark after a block control (Word's `_GoBack`) is read onto the end of its paragraph, + // and the control still covers the whole paragraph. let at_end = |b: usize, off: usize| { + use wordcraft_doc::InlineObject as O; para(b).is_some_and(|p| { let from = off + OBJ.len_utf8(); p.text.get(from..).is_some_and(|t| t.chars().all(|c| c == OBJ)) - && (from..p.len()).step_by(OBJ.len_utf8()).all(|o| matches!(p.object_at(o), Some(wordcraft_doc::InlineObject::ControlEnd))) + && (from..p.len()).step_by(OBJ.len_utf8()).all(|o| { + matches!( + p.object_at(o), + Some( + O::ControlEnd + | O::BookmarkStart { .. } + | O::BookmarkEnd { .. } + | O::CommentStart { .. } + | O::CommentEnd { .. } + | O::PermStart { .. } + | O::PermEnd { .. } + ) + ) + }) }) }; let mut mode = vec![Mode::Inline; ranges.len()]; diff --git a/crates/docx/src/write/story/drawing.rs b/crates/docx/src/write/story/drawing.rs index f82a736fab..4d158ba349 100644 --- a/crates/docx/src/write/story/drawing.rs +++ b/crates/docx/src/write/story/drawing.rs @@ -10,9 +10,19 @@ pub(super) struct DrawingInfo<'a> { pub docpr: &'a str, pub name: &'a str, pub alt: &'a str, + /// The relationship id of the drawing's hyperlink (`a:hlinkClick`), see [`Writer::drawing_link`]. + pub link: Option<&'a str>, } impl Writer<'_> { + /// The relationship of the hyperlink a drawing in a run with `props` carries on itself, as + /// Word writes a picture's link (`wp:docPr/a:hlinkClick`, #5267): a web or file address. A + /// link to a place in the document (`#name`) is only the `w:hyperlink` around the run. + pub(super) fn drawing_link(props: &wordcraft_doc::CharProps, rels: &mut PartRels) -> Option { + let url = props.link.as_deref().filter(|l| !l.is_empty() && !l.starts_with('#'))?; + Some(rels.add(rt::HYPERLINK, url, true)) + } + /// A picture's `pic:pic` (the image in `media/{file}`), at `off` in its group (or 0, 0). pub(super) fn pic(&mut self, w: &mut W, o: &InlineObject, file: &str, off: (f32, f32), rels: &mut PartRels) { let InlineObject::Image { media, w: iw, h: ih, alt, crop, float, fx, .. } = o else { return }; @@ -20,7 +30,7 @@ impl Writer<'_> { self.used_media.insert(media.clone()); w.open("pic:pic", &[]); w.open("pic:nvPicPr", &[]); - nv_props(w, "pic:cNvPr", "0", file, alt, &float.alt, float.hidden); + nv_props(w, "pic:cNvPr", ("0", file), alt, &float.alt, float.hidden, None); w.open("pic:cNvPicPr", &[]); w.empty("a:picLocks", &[("noChangeAspect", "1"), ("noChangeArrowheads", "1")]); w.close("pic:cNvPicPr"); @@ -66,7 +76,7 @@ impl Writer<'_> { w.open("wps:wsp", &[]); if let Some(id) = id { let name = shape_name(*kind, freeform.as_deref(), id); - nv_props(w, "wps:cNvPr", id, &name, &float.alt.text, &float.alt, float.hidden); + nv_props(w, "wps:cNvPr", (id, &name), &float.alt.text, &float.alt, float.hidden, None); } if kind.is_connector() { // A connector's glued ends (ECMA-376 §20.1.2.2.8 `cNvCxnSpPr`). @@ -235,7 +245,7 @@ impl Writer<'_> { } pub(super) fn drawing_open(&mut self, w: &mut W, info: DrawingInfo<'_>) { - let DrawingInfo { float, size: (iw, ih), docpr, name, alt } = info; + let DrawingInfo { float, size: (iw, ih), docpr, name, alt, link } = info; let (cx, cy) = (emu(iw.max(0.0)), emu(ih.max(0.0))); if float.wrap == Wrap::Inline { w.open("wp:inline", &[("distT", "0"), ("distB", "0"), ("distL", "0"), ("distR", "0")]); @@ -288,8 +298,9 @@ impl Writer<'_> { } } w.empty("wp:extent", &[("cx", &cx), ("cy", &cy)]); - // Word's effect extent also covers a rotated object's overhang (its rotated bounds). - let (px, py) = float.spin_pad(iw, ih); + // Word's effect extent also covers a rotated object's overhang (its rotated bounds), unless + // the file it came from kept none. + let (px, py) = if float.is_bare_turn() { (0.0, 0.0) } else { float.spin_pad(iw, ih) }; let [el, et, er, eb] = float.effect_extent(); let [el, et, er, eb] = [el + px, et + py, er + px, eb + py].map(emu); w.empty("wp:effectExtent", &[("l", &el), ("t", &et), ("r", &er), ("b", &eb)]); @@ -316,7 +327,7 @@ impl Writer<'_> { Wrap::TopAndBottom => w.empty("wp:wrapTopAndBottom", &[]), Wrap::BehindText | Wrap::InFrontOfText => w.empty("wp:wrapNone", &[]), } - nv_props(w, "wp:docPr", docpr, name, alt, &float.alt, float.hidden); + nv_props(w, "wp:docPr", (docpr, name), alt, &float.alt, float.hidden, link); } } @@ -502,8 +513,9 @@ pub(in super::super) fn spin_attrs(spin: wordcraft_geom::Spin) -> Vec<(&'static /// A drawing's `wp:docPr` (or a group member's `pic:cNvPr`/`wps:cNvPr`, ECMA-376 /// `CT_NonVisualDrawingProps`) with its alt text `descr`, its title, and the decorative mark as /// the `adec:decorative` extension ([MS-ODRAWXML]). A description that is only the title (read -/// from a file with no description) is not written twice. -pub(in super::super) fn nv_props(w: &mut W, tag: &str, id: &str, name: &str, descr: &str, alt: &AltText, hidden: bool) { +/// from a file with no description) is not written twice. `link` is the relationship id of the +/// drawing's hyperlink (`a:hlinkClick`, the first child the schema allows). +pub(in super::super) fn nv_props(w: &mut W, tag: &str, (id, name): (&str, &str), descr: &str, alt: &AltText, hidden: bool, link: Option<&str>) { let mut attrs = vec![("id", id), ("name", name)]; if descr != alt.title || descr.is_empty() { attrs.push(("descr", descr)); @@ -514,16 +526,21 @@ pub(in super::super) fn nv_props(w: &mut W, tag: &str, id: &str, name: &str, des if hidden { attrs.push(("hidden", "1")); } - if !alt.decorative { + if !alt.decorative && link.is_none() { w.empty(tag, &attrs); return; } w.open(tag, &attrs); - w.open("a:extLst", &[]); - w.open("a:ext", &[("uri", crate::xml::DECORATIVE_EXT)]); - w.empty("adec:decorative", &[("xmlns:adec", crate::xml::DECORATIVE_NS), ("val", "1")]); - w.close("a:ext"); - w.close("a:extLst"); + if let Some(rid) = link { + w.empty("a:hlinkClick", &[("r:id", rid)]); + } + if alt.decorative { + w.open("a:extLst", &[]); + w.open("a:ext", &[("uri", crate::xml::DECORATIVE_EXT)]); + w.empty("adec:decorative", &[("xmlns:adec", crate::xml::DECORATIVE_NS), ("val", "1")]); + w.close("a:ext"); + w.close("a:extLst"); + } w.close(tag); } diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index ef6c338216..5e8bc21ba8 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -220,13 +220,18 @@ impl Writer<'_> { w.open("mc:Choice", &[("Requires", requires)]); } let docpr = self.next_docpr(); + let link = Self::drawing_link(props, rels); let name = match graphic.kind { wordcraft_doc::graphic::GraphicKind::Chart => format!("Chart {docpr}"), wordcraft_doc::graphic::GraphicKind::Diagram => format!("Diagram {docpr}"), wordcraft_doc::graphic::GraphicKind::Ink => format!("Ink {docpr}"), }; let z = self.z; - self.graphic_frame(w, DrawingInfo { float, size: (*gw, *gh), docpr: &docpr, name: &name, alt }, native.unwrap_or(&root)); + self.graphic_frame( + w, + DrawingInfo { float, size: (*gw, *gh), docpr: &docpr, name: &name, alt, link: link.as_deref() }, + native.unwrap_or(&root), + ); if let Some(fallback) = fallback { w.close("mc:Choice"); match picture { @@ -234,7 +239,11 @@ impl Writer<'_> { // Both branches are one object: same id and stacking order. self.z = z; w.open("mc:Fallback", &[]); - self.graphic_frame(w, DrawingInfo { float, size: (*gw, *gh), docpr: &docpr, name: &name, alt }, picture); + self.graphic_frame( + w, + DrawingInfo { float, size: (*gw, *gh), docpr: &docpr, name: &name, alt, link: link.as_deref() }, + picture, + ); w.close("mc:Fallback"); } None => w.raw(&fallback.to_xml()), @@ -266,8 +275,9 @@ impl Writer<'_> { self.rpr(w, props); w.open("w:drawing", &[]); let docpr = self.next_docpr(); + let link = Self::drawing_link(props, rels); let name = format!("Picture {docpr}"); - self.drawing_open(w, DrawingInfo { float, size: (*iw, *ih), docpr: &docpr, name: &name, alt }); + self.drawing_open(w, DrawingInfo { float, size: (*iw, *ih), docpr: &docpr, name: &name, alt, link: link.as_deref() }); w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.openxmlformats.org/drawingml/2006/picture")]); @@ -355,6 +365,7 @@ impl Writer<'_> { self.rpr(w, props); w.open("w:drawing", &[]); let docpr = self.shape_docpr(extra.id); + let link = Self::drawing_link(props, rels); // Ink is told apart by its name (and its pen), which reading looks for. let name = shape_name(*kind, freeform.as_deref(), &docpr); // The effect extent leaves room for the shadow and glow. @@ -369,7 +380,7 @@ impl Writer<'_> { .map(std::sync::Arc::new); } let float = &float; - self.drawing_open(w, DrawingInfo { float, size: (*sw, *sh), docpr: &docpr, name: &name, alt: o.alt_text() }); + self.drawing_open(w, DrawingInfo { float, size: (*sw, *sh), docpr: &docpr, name: &name, alt: o.alt_text(), link: link.as_deref() }); w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape")]); @@ -389,8 +400,9 @@ impl Writer<'_> { self.rpr(w, props); w.open("w:drawing", &[]); let docpr = self.next_docpr(); + let link = Self::drawing_link(props, rels); let name = format!("{} {docpr}", if canvas.is_some() { "Canvas" } else { "Group" }); - self.drawing_open(w, DrawingInfo { float, size: (*gw, *gh), docpr: &docpr, name: &name, alt: o.alt_text() }); + self.drawing_open(w, DrawingInfo { float, size: (*gw, *gh), docpr: &docpr, name: &name, alt: o.alt_text(), link: link.as_deref() }); w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); let outer = match canvas { diff --git a/crates/docx/src/xml.rs b/crates/docx/src/xml.rs index 0436265b0f..ca12fa5d63 100644 --- a/crates/docx/src/xml.rs +++ b/crates/docx/src/xml.rs @@ -94,6 +94,49 @@ fn prefix_for(uri: &[u8], extra: &[(&'static str, &str)]) -> &'static str { "?" } +/// Word's 2006 namespace (`wne`). A Strict document from Word keeps a text box's content in +/// `wne:txbxContent`; a Transitional one uses `w:txbxContent`. +const WNE: &[u8] = b"http://schemas.microsoft.com/office/word/2006/wordml"; + +/// The canonical prefix of an element named `local` in namespace `uri`, inside an element +/// named `parent` (canonical, `None` at the root). This is [`prefix_for`] with two exceptions +/// for Strict documents (#3548): +/// +/// - Shapes, groups and canvases (`wsp`, `wgp`, `wpc`) and their parts are elements of the +/// WordprocessingDrawing namespace in ECMA-376 Part 1, 5th edition (`dml-wordprocessingDrawing.xsd`, +/// Strict and Transitional alike). A Strict document from Word puts them there; a Transitional +/// one uses Word 2010's own namespaces, whose names the reader knows. So they read as `wps:`, +/// `wpg:` and `wpc:` (see [`drawing_prefix`]). +/// - A shape's text box content reads as `w:txbxContent` from the drawing namespace (the +/// schema's name) and from Word's `wne:txbxContent`. +fn element_prefix(uri: &[u8], local: &str, parent: Option<&str>, extra: &[(&'static str, &str)]) -> &'static str { + match prefix_for(uri, extra) { + "wp" => drawing_prefix(local, parent), + "?" if uri == WNE && local == "txbxContent" && parent == Some("wps:txbx") => "w", + p => p, + } +} + +/// The prefix for `local`, an element of the WordprocessingDrawing namespace inside `parent`. +/// Word 2010's namespaces split what this one holds: a shape and its parts are `wps:`, a group +/// (`wgp`, a nested `grpSp`) and a graphic frame in it are `wpg:`, and a canvas is `wpc:`. Its +/// members are named for what they are. A text box's content is `w:txbxContent`, and anything +/// else (`inline`, `anchor`, `docPr`…) stays `wp:`. +fn drawing_prefix(local: &str, parent: Option<&str>) -> &'static str { + match (parent, local) { + (_, "wsp") => "wps", + (_, "wgp" | "grpSp" | "graphicFrame") => "wpg", + (_, "wpc") => "wpc", + (Some("wps:wsp"), "cNvPr" | "cNvSpPr" | "cNvCnPr" | "spPr" | "style" | "extLst" | "txbx" | "linkedTxbx" | "bodyPr") => "wps", + (Some("wps:txbx"), "txbxContent") => "w", + (Some("wps:txbx" | "wps:linkedTxbx"), "extLst") => "wps", + (Some("wpg:wgp" | "wpg:grpSp"), "cNvPr" | "cNvGrpSpPr" | "grpSpPr" | "extLst") => "wpg", + (Some("wpg:graphicFrame"), "cNvPr" | "cNvFrPr" | "xfrm" | "extLst") => "wpg", + (Some("wpc:wpc"), "bg" | "whole" | "extLst") => "wpc", + _ => "wp", + } +} + /// Compatibility feature namespaces whose elements are not otherwise handled by this tree. /// Keep the reader's existing feature set when canonicalizing `mc:Choice/@Requires` aliases. fn requirement_prefix(uri: &[u8]) -> &'static str { @@ -342,8 +385,8 @@ fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool) -> Res loop { buf.clear(); let (res, ev) = r.read_resolved_event_into(&mut buf).map_err(|e| DocxError::Xml(e.to_string()))?; - let el_prefix = match &res { - ResolveResult::Bound(ns) => Some(prefix_for(ns.as_ref(), extra)), + let el_ns = match &res { + ResolveResult::Bound(ns) => Some(ns.as_ref()), _ => None, }; match ev { @@ -356,8 +399,8 @@ fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool) -> Res return Err(DocxError::Limit("too many XML elements".into())); } let local = String::from_utf8_lossy(e.local_name().as_ref()).into_owned(); - let name = match el_prefix { - Some(p) => format!("{p}:{local}"), + let name = match el_ns { + Some(ns) => format!("{}:{local}", element_prefix(ns, &local, stack.last().map(|p| p.name.as_str()), extra)), None => local, }; let mut el = El { name, attrs: Vec::new(), kids: Vec::new() }; @@ -419,15 +462,22 @@ fn parse_with(bytes: &[u8], extra: &[(&'static str, &str)], strict: bool) -> Res attach(&mut stack, &mut root, el); } } + // Line ends are normalised as XML 1.0 says (§2.11): CR LF and a lone CR are read as + // LF, so a file saved with Windows line ends has no CR in its text. A CR written as + // ` ` is a character reference and stays. Event::Text(t) => { if let Some(top) = stack.last_mut() { - let s = t.decode().map_err(|e| DocxError::Xml(e.to_string()))?; + let s = t.xml_content(XmlVersion::Explicit1_0).map_err(|e| DocxError::Xml(e.to_string()))?; push_text(top, &s); } } Event::CData(t) => { if let Some(top) = stack.last_mut() { - let s = String::from_utf8_lossy(&t).into_owned(); + // CDATA that isn't UTF-8 is decoded lossily, with its line ends normalised the same way. + let s = t + .xml_content(XmlVersion::Explicit1_0) + .map(|s| s.into_owned()) + .unwrap_or_else(|_| String::from_utf8_lossy(&t).replace("\r\n", "\n").replace('\r', "\n")); push_text(top, &s); } } @@ -593,6 +643,42 @@ mod tests { assert!(parse(b"").is_ok(), "lenient parsing still stops at the root"); } + /// Every element name in `e`, depth first. + fn names(e: &El, out: &mut Vec) { + out.push(e.name.clone()); + for c in e.els() { + names(c, out); + } + } + + #[test] + fn strict_drawing_names_read_as_word_2010_names() { + // A Strict shape with a text box, a group with a nested group and a graphic frame, and + // a canvas, each in the one Strict WordprocessingDrawing namespace (#3548). + let xml = br#" + + + + + + +"#; + let mut got = Vec::new(); + names(&parse(xml).unwrap(), &mut got); + let want = "w:body wp:anchor wp:extent wp:docPr wp:cNvGraphicFramePr a:graphic a:graphicData \ + wps:wsp wps:cNvPr wps:cNvSpPr wps:spPr wps:style wps:extLst wps:txbx w:txbxContent w:p wps:bodyPr \ + wpg:wgp wpg:cNvPr wpg:cNvGrpSpPr wpg:grpSpPr wpg:grpSp wpg:cNvGrpSpPr wpg:grpSpPr wps:wsp wps:cNvCnPr wps:spPr wps:linkedTxbx wps:bodyPr \ + wpg:graphicFrame wpg:cNvPr wpg:cNvFrPr wpg:xfrm a:graphic wpg:extLst wpg:extLst \ + wpc:wpc wpc:bg wpc:whole wps:wsp wps:spPr wps:txbx w:txbxContent wps:extLst wpg:wgp wpg:grpSpPr wpc:extLst \ + ?:txbxContent wp:inline wp:docPr wp:bodyPr wp:bg"; + assert_eq!(got.join(" "), want); + // The Transitional namespaces read as before. + let xml = br#""#; + let mut got = Vec::new(); + names(&parse(xml).unwrap(), &mut got); + assert_eq!(got.join(" "), "w:body wp:inline wp:docPr wps:wsp wps:txbx w:txbxContent ?:toolbars"); + } + #[test] fn a_relationship_attribute_is_the_namespaced_one_or_the_alias_of_one() { // The relationship namespace, and VML's `o:relid`. @@ -645,6 +731,28 @@ mod tests { assert_eq!(t.attr("xml:space"), Some("preserve")); } + /// CDATA that isn't UTF-8 is decoded lossily, and its line ends are still normalised. + #[test] + fn line_ends_in_cdata_that_isnt_utf8_are_normalised() { + let x = + b""; + assert_eq!(parse(x).unwrap().child("w:t").unwrap().text(), "a\u{FFFD}\nb\nc"); + } + + /// XML 1.0 §2.11: CR LF and a lone CR in text and CDATA are read as LF; ` ` stays a CR. + #[test] + fn line_ends_in_text_are_normalised_but_a_cr_reference_stays() { + let x = b"a\r\nb\rc d"; + let e = parse(x).unwrap(); + assert_eq!(e.child("w:t").unwrap().text(), "a\nb\nc\rde\nf"); + // The writer escapes a CR, so a CR in the text survives a save. + let x = format!( + "{}", + esc("a\r\nb\rc") + ); + assert_eq!(parse(x.as_bytes()).unwrap().child("w:t").unwrap().text(), "a\r\nb\rc"); + } + #[test] fn choice_requirements_resolve_namespace_aliases_and_rebindings() { let x = br#""#; diff --git a/crates/docx/tests/it/content_controls.rs b/crates/docx/tests/it/content_controls.rs index 63b79c4c4f..ef420e42d8 100644 --- a/crates/docx/tests/it/content_controls.rs +++ b/crates/docx/tests/it/content_controls.rs @@ -342,3 +342,57 @@ fn toc_gallery_around_a_toc_whose_heading_is_in_a_block_control_round_trips() { assert!(xml.rfind("").unwrap() < xml.rfind(">Chapter One<").unwrap(), "pass {pass}: {xml}"); } } + +#[test] +fn word_toc_gallery_with_field_end_after_entries_round_trips_without_update() { + let style = |s: &str, inner: &str| format!(r#"{inner}"#); + let begin = r#" TOC \o "1-3" \h \z \u "#; + let content = format!( + "{}{}{}", + style("TOCHeading", &t("Contents")), + style("TOC1", &format!("{begin}{}", t("Chapter One\t1"))), + style("TOC2", &t("Section\t2")) + ); + let pr = r#""#; + let mut bytes = package(&format!("{}{}", sdt(pr, &content), p(&t("Chapter One")))); + for pass in 0..2 { + let d = read(&bytes).unwrap(); + bytes = write(&d).unwrap(); + let xml = document_xml(&bytes); + assert_eq!(xml.matches("").count(), 1, "pass {pass}: {xml}"); + assert!(xml.contains(r#""#), "pass {pass}: {xml}"); + assert!(xml.find("").unwrap() < xml.rfind(">Chapter One<").unwrap(), "pass {pass}: {xml}"); + } +} + +#[test] +fn range_toc_without_control_stays_unwrapped_and_section_break_toc_control_hands_over() { + let style = |s: &str, inner: &str| format!(r#"{inner}"#); + let begin = r#" TOC \o "1-3" \h \z \u "#; + // A range TOC that was never in a content control (with FieldEnd in its own paragraph) must + // not gain a Table of Contents w:sdt on save. + let bare = format!( + "{}{}{}{}", + style("TOCHeading", &t("Contents")), + style("TOC1", &format!("{begin}{}", t("Chapter One\t1"))), + style("TOC2", &t("Section\t2")), + p(&t("Chapter One")), + ); + let d_bare = read(&package(&bare)).unwrap(); + let xml_bare = document_xml(&write(&d_bare).unwrap()); + assert_eq!(xml_bare.matches("").count(), 0, "{xml_bare}"); + + // #3889: a Table of Contents control opening on an empty paragraph that ends a section above + // the entries hands its control over on read so Update Table reuses the section-break paragraph. + let sect_toc = format!( + r#"{}{}"#, + style("TOC1", &format!("{begin}{}", t("Chapter One\t1"))), + style("TOC2", &t("Section\t2")), + ); + let pr = r#""#; + let d_sect = read(&package(&format!("{}{}", sdt(pr, §_toc), p(&t("Chapter One"))))).unwrap(); + assert!(controls(&d_sect).is_empty()); + assert!(d_sect.body[0].as_para().unwrap().section.is_some()); + let xml_sect = document_xml(&write(&d_sect).unwrap()); + assert!(xml_sect.contains("Chapter One") && xml_sect.contains("Section"), "{xml_sect}"); +} diff --git a/crates/docx/tests/it/fixtures.rs b/crates/docx/tests/it/fixtures.rs index 1df2e96f9a..9d2e902505 100644 --- a/crates/docx/tests/it/fixtures.rs +++ b/crates/docx/tests/it/fixtures.rs @@ -1766,12 +1766,27 @@ const CT_CHARTEX: &str = "application/vnd.ms-office.chartex+xml"; /// A package holding one box & whisker chart the way Word writes it: the `cx:chart` in an /// `mc:Choice` requiring `requires` (bound to `requires_ns`), a picture of it in the `mc:Fallback`. fn chartex_docx(requires: &str, requires_ns: &str) -> Vec { - let frame = r#""#; + chartex_docx_linked(requires, requires_ns, false) +} + +/// [`chartex_docx`], its drawing linked to a web address (`wp:docPr/a:hlinkClick`) when `link`, +/// after a first choice linked elsewhere, which needs a namespace no reader knows, so a reader +/// takes the chart's choice. +fn chartex_docx_linked(requires: &str, requires_ns: &str, link: bool) -> Vec { + let click = if link { r#""# } else { "" }; + let other = if link { + r#""# + } else { + "" + }; + let frame = format!( + r#"{click}"# + ); let body = format!( - r#"Spread{frame}{frame}"# + r#"Spread{other}{frame}{frame}"# ); let doc = format!(r#"{body}"#); - let doc_rels = r#""#; + let doc_rels = r#""#; let chart_rels = r#""#; let types = format!( r#""# @@ -1827,6 +1842,25 @@ fn office_2016_chart_keeps_its_chart_part_and_fallback_through_saves() { } } +/// #5267: a link on an Office 2016 chart's drawing is the chart's link (not an earlier choice's), +/// and saving keeps it on both branches (the writer rebuilds their frames). +#[test] +fn a_link_on_an_office_2016_charts_drawing_is_kept() { + let mut d = wordcraft_docx::read(&chartex_docx_linked("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex", true)).unwrap(); + for _ in 0..2 { + let p = d.body[0].as_para().unwrap(); + let off = p.object_offsets().into_iter().find(|o| matches!(p.object_at(*o), Some(InlineObject::Graphic { .. }))).unwrap(); + assert_eq!(p.props_of_char(off).link.as_deref(), Some("https://example.com/chart")); + let files = unzip(&wordcraft_docx::write(&d).unwrap()); + let body = text(&files, "word/document.xml"); + let ids = attr_values(&body, "a:hlinkClick", "r:id"); + assert_eq!(ids.len(), 2, "on the chart and its fallback picture: {body}"); + assert_eq!(rel(&text(&files, "word/_rels/document.xml.rels"), &ids[0]).1, "https://example.com/chart"); + let entries: Vec<_> = files.iter().map(|(name, bytes)| (name.as_str(), bytes.as_slice())).collect(); + d = wordcraft_docx::read(&zip(&entries)).unwrap(); + } +} + #[test] fn resized_office_2016_chart_resizes_its_fallback_too() { let mut d = wordcraft_docx::read(&chartex_docx("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex")).unwrap(); @@ -2462,6 +2496,31 @@ fn east_asian_number_formats_read_label_and_round_trip() { assert_eq!(formats(&back), want); } +/// #4985: a level with numFmt none and literal text (`RESPONSE:`) labels its paragraph with that +/// text, as in Word, and keeps both through a save. +#[test] +fn a_none_level_s_literal_text_is_its_label() { + use wordcraft_doc::numbering::Counters; + use wordcraft_doc::section::NumFormat; + let numbering = format!( + r#""# + ); + let body = r#"X"#; + let bytes = docx(body, &[("rId1", "numbering", "numbering.xml")], &[("word/numbering.xml", &numbering)]); + let label = |d: &Document| { + let n = paras(d).first().and_then(|p| p.props.numbering)?; + Counters::default().next_label(&d.numbering, n.num, n.level).map(|l| l.0) + }; + let d = wordcraft_docx::read(&bytes).unwrap(); + assert_eq!(label(&d).as_deref(), Some("RESPONSE:")); + let back = wordcraft_docx::read(&wordcraft_docx::write(&d).unwrap()).unwrap(); + assert_eq!( + back.numbering.level(1, 0).map(|l| (l.format, l.text.as_str(), l.indent, l.hanging)), + Some((NumFormat::None, "RESPONSE:", 72.0, 72.0)) + ); + assert_eq!(label(&back).as_deref(), Some("RESPONSE:")); +} + /// #464: a bookmark whose end follows the document's final table keeps its end (it goes into /// the table's last cell) and is saved as a matched pair. #[test] @@ -3888,6 +3947,33 @@ fn bidi_theme_slots_follow_the_theme_font_language() { } } +/// A `w:rFonts` fallback list longer than the reader keeps is cut at the last complete family +/// within the bound, so the family a real list would pick survives while a hostile list cannot +/// cost its whole length in every piece of text drawn in it. +#[test] +fn a_hostile_fallback_list_keeps_its_leading_families() { + let candidates: Vec = (0..500).map(|i| format!("MissingCandidateFont{i}")).collect(); + let list = format!("{};Source Serif 4", candidates.join(";")); + let body = format!(r#"x"#); + let d = read_body(&body); + let c = &d.body[0].as_para().unwrap().runs[0].props; + let kept = c.font.as_deref().expect("the leading families are kept"); + assert!(kept.len() <= 256, "{} bytes kept", kept.len()); + assert!(kept.starts_with("MissingCandidateFont0"), "{kept}"); + // Every kept entry is a whole family name: the cut lands on a `;`, never inside a name. + assert!(kept.split(';').all(|f| f.starts_with("MissingCandidateFont") || f.contains("Source Serif")), "{kept}"); + assert!(!kept.ends_with(';'), "no dangling separator: {kept}"); +} + +/// A real LibreOffice fallback list is read whole: a few families, well under any bound. +#[test] +fn a_real_fallback_list_is_read_whole() { + let body = r#"x"#; + let d = read_body(body); + let c = &d.body[0].as_para().unwrap().runs[0].props; + assert_eq!(c.font.as_deref(), Some("Liberation Sans;Arial")); +} + /// The font table's family classes: Word draws a missing roman font in a serif face. #[test] fn roman_fonts_are_read_from_the_font_table() { @@ -3897,6 +3983,15 @@ fn roman_fonts_are_read_from_the_font_table() { assert_eq!(d.settings.roman_fonts, ["Brand Serif"]); } +/// Semicolon-separated roman font fallback lists in `fontTable.xml` register each candidate family. +#[test] +fn roman_fonts_split_fallback_lists() { + let fonts = r#""#; + let rels = [("rId9", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable", "fontTable.xml")]; + let d = wordcraft_docx::read(&docx("", &rels, &[("word/fontTable.xml", fonts)])).unwrap(); + assert_eq!(d.settings.roman_fonts, ["Liberation Serif", "Times New Roman"]); +} + /// `w:doNotUseHTMLParagraphAutoSpacing` in the settings' `w:compat`: paragraph spacing adds up. #[test] fn html_auto_spacing_off_is_read_and_kept() { @@ -4295,6 +4390,54 @@ fn zero_effects_stay_zero_rotated() { } } +/// A turned picture whose file keeps no room for the turn (no or a zero `wp:effectExtent`) is +/// marked so its line keeps to its frame as in Word, and saves that way; one with the room keeps +/// its rotated bounds. +#[test] +fn bare_turns_are_kept_through_a_save() { + let pic = |effects: &str| { + format!( + r#"{effects}"# + ) + }; + let read = + |body: &str| wordcraft_docx::read(&docx(body, &[("rId1", "image", "media/marker.png")], &[("word/media/marker.png", "synthetic")])).unwrap(); + let bare = |d: &Document| paras(d)[0].objects[0].frame().unwrap().2.bare_turn; + for effects in ["", r#""#] { + let d = read(&pic(effects)); + assert!(bare(&d), "{effects:?}"); + let out = wordcraft_docx::write(&d).unwrap(); + let body = text(&unzip(&out), "word/document.xml"); + assert_eq!(attr_values(&body, "wp:effectExtent", "t"), ["0"]); + assert!(bare(&wordcraft_docx::read(&out).unwrap())); + } + // 120 × 60 pt at 45°: Word's rotated-bounds room, 3.64 pt wider and 33.64 pt taller each side. + let d = read(&pic(r#""#)); + assert!(!bare(&d)); + let mut d = read(&pic("")); + let f = first_object(&mut d).float_mut().unwrap(); + f.set_spin(wordcraft_geom::Spin::new(30.0, false, false)); + assert!(!f.bare_turn, "turned here: the rotated bounds again"); + // A shadow's room added later: saved, and so laid out, with the turn's room again. + let mut d = read(&pic("")); + let f = first_object(&mut d).float_mut().unwrap(); + f.effect = [2.0; 4]; + assert!(!f.is_bare_turn()); + let out = wordcraft_docx::write(&d).unwrap(); + assert_ne!(attr_values(&text(&unzip(&out), "word/document.xml"), "wp:effectExtent", "t"), ["0"]); + // Made floating: saved with the turn's room, so it opens again as an ordinary turn. + let mut d = read(&pic("")); + let f = first_object(&mut d).float_mut().unwrap(); + f.wrap = wordcraft_doc::para::Wrap::Square; + assert!(!f.is_bare_turn()); + let out = wordcraft_docx::write(&d).unwrap(); + assert_ne!(attr_values(&text(&unzip(&out), "word/document.xml"), "wp:effectExtent", "t"), ["0"]); + assert!(!bare(&wordcraft_docx::read(&out).unwrap())); + // Only inline ones: a floating one keeps text clear of its rotated bounds as before. + let anchored = pic("").replace("", "").replace("", ""); + assert!(!bare(&read(&anchored))); +} + /// The math namespace, declared on the math element itself (the fixture root doesn't). const M_NS: &str = r#"xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math""#; diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index e54afbd5f5..df7e28fdff 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -21,8 +21,10 @@ mod heading_levels; mod legacy_forms; mod ligatures; mod malformed; +mod nested_block_controls; mod note_comments; mod passthrough; +mod picture_links; mod positional_tabs; mod privacy; mod roundtrip; @@ -34,9 +36,11 @@ mod section_page_number_type; mod section_text_direction; mod section_valign; mod settings_merge; +mod strict_drawings; mod text_alignment; mod text_boxes; mod toc_ranges; +mod twips_wrap; mod undefined_style; mod vml_shapes; mod word_xml; diff --git a/crates/docx/tests/it/nested_block_controls.rs b/crates/docx/tests/it/nested_block_controls.rs new file mode 100644 index 0000000000..30a0c272b2 --- /dev/null +++ b/crates/docx/tests/it/nested_block_controls.rs @@ -0,0 +1,180 @@ +//! A body-level content control nested directly in another body-level one stays body-level when +//! saved, even when it holds a single paragraph (ECMA-376 §17.5.2.29-30; [MS-DOCX] `w15` +//! repeating sections, which must wrap whole paragraphs or rows). All markup here is synthetic. + +use std::io::Write; + +use wordcraft_doc::{ContentControl, Document, StoryRef}; +use wordcraft_docx::{read, write}; + +const NS: &str = + r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml""#; +const ROOT_RELS: &str = r#""#; + +fn package(body: &str) -> Vec { + let doc = format!(r#"{body}"#); + let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for (name, bytes) in [("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", doc.as_bytes())] { + zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + zw.write_all(bytes).unwrap(); + } + zw.finish().unwrap().into_inner() +} + +fn document_xml(docx: &[u8]) -> String { + let mut z = zip::ZipArchive::new(std::io::Cursor::new(docx)).unwrap(); + let mut s = String::new(); + std::io::Read::read_to_string(&mut z.by_name("word/document.xml").unwrap(), &mut s).unwrap(); + s +} + +fn t(s: &str) -> String { + format!(r#"{s}"#) +} +fn p(inner: &str) -> String { + format!("{inner}") +} +fn sdt(pr: &str, content: &str) -> String { + format!("{pr}{content}") +} + +fn controls(d: &Document) -> Vec { + let mut out = Vec::new(); + wordcraft_doc::control::walk_controls(&d.body, &mut |c| out.push(c.clone())); + out +} + +/// How many `w:p` elements are open at byte `at` of `xml`. +fn open_paragraphs(xml: &str, at: usize) -> usize { + let head = &xml[..at]; + let opened = head.matches("").count() + head.matches("").count() +} + +/// Paragraph depth at the start of the `w:sdt` whose `w:sdtPr` holds `marker`. +fn depth_of_control(xml: &str, marker: &str) -> usize { + let at = xml.find(marker).unwrap_or_else(|| panic!("{marker} not written:\n{xml}")); + let start = xml[..at].rfind("").expect("control start"); + open_paragraphs(xml, start) +} + +fn round_trip(body: &str) -> (Document, Document, String) { + let d = read(&package(body)).unwrap(); + let bytes = write(&d).unwrap(); + let back = read(&bytes).unwrap(); + (d, back, document_xml(&bytes)) +} + +fn repeating_section() -> String { + let field = sdt(r#""#, &t("note 1")); + let item = sdt(r#""#, &p(&format!("{field}{}", t(" ")))); + let section = sdt(r#""#, &item); + format!("{}{section}{}", p(&t("Notes")), p(&t("End"))) +} + +#[test] +fn a_repeating_section_around_one_paragraph_stays_around_the_paragraph() { + let (_, _, xml) = round_trip(&repeating_section()); + assert_eq!(depth_of_control(&xml, r#""#), 0, "section written inside a paragraph:\n{xml}"); + assert_eq!(depth_of_control(&xml, r#""#), 0, "item written inside a paragraph:\n{xml}"); + // The text control stays inside the paragraph, and the item holds that paragraph. + assert_eq!(depth_of_control(&xml, r#""#), 1); + assert!(xml.contains(""#, &p(&t("Only paragraph"))); + let outer = sdt(r#""#, &inner); + let (d, back, xml) = round_trip(&format!("{outer}{}", p(&t("After")))); + assert_eq!(depth_of_control(&xml, r#""#), 0, "{xml}"); + assert_eq!(depth_of_control(&xml, r#""#), 0, "{xml}"); + assert_eq!(controls(&back), controls(&d)); +} + +#[test] +fn nested_block_controls_in_a_table_cell_stay_block_level() { + let inner = sdt(r#""#, &p(&t("In cell"))); + let outer = sdt(r#""#, &inner); + let table = format!(r#"{outer}"#); + let (_, _, xml) = round_trip(&format!("{table}{}", p(&t("After")))); + assert_eq!(depth_of_control(&xml, r#""#), 0, "{xml}"); + assert_eq!(depth_of_control(&xml, r#""#), 0, "{xml}"); +} + +#[test] +fn a_run_level_control_stays_inside_its_paragraph() { + let field = sdt(r#""#, &t("value")); + let (_, _, xml) = round_trip(&p(&format!("{}{field}", t("Name: ")))); + assert_eq!(depth_of_control(&xml, r#""#), 1, "{xml}"); +} + +/// A bound repeating section as Word writes one at the end of a form, optionally followed by +/// the body-level `_GoBack` bookmark Word leaves after the last control. +fn bound_section_body(bound: bool, bookmark: bool) -> String { + let section_binding = if bound { + r#""# + } else { + "" + }; + let trailing = if bookmark { r#""# } else { "" }; + let bind = |x: &str| format!(r#""#); + let field = format!( + r#"{}{}"#, + if bound { bind("/a[1]/n[1]") } else { String::new() }, + t("note 1") + ); + let item = format!( + r#"{}"#, + p(&format!("{field}{}", t(" "))) + ); + let section = format!( + r#"{section_binding}{item}"# + ); + format!(r#"{}{section}{trailing}"#, p(&t("Notes"))) +} + +fn assert_section_block_level(body: &str) { + let (d, back, xml) = round_trip(body); + assert_eq!(depth_of_control(&xml, r#""#), 0, "section written inside a paragraph:\n{xml}"); + assert_eq!(depth_of_control(&xml, r#""#), 0, "item written inside a paragraph:\n{xml}"); + assert_eq!(depth_of_control(&xml, r#""#), 1, "{xml}"); + assert_eq!(controls(&back), controls(&d)); + assert_eq!(back.plain_text(StoryRef::Body), d.plain_text(StoryRef::Body)); +} + +#[test] +fn a_bound_repeating_section_followed_by_a_body_bookmark_stays_block_level() { + assert_section_block_level(&bound_section_body(true, true)); +} + +#[test] +fn a_bound_repeating_section_at_the_end_of_the_body_stays_block_level() { + assert_section_block_level(&bound_section_body(true, false)); +} + +#[test] +fn a_repeating_section_followed_by_a_body_bookmark_stays_block_level() { + assert_section_block_level(&bound_section_body(false, true)); +} + +#[test] +fn a_block_control_followed_by_a_body_bookmark_stays_around_its_paragraph() { + let inner = sdt(r#""#, &p(&t("Last paragraph"))); + let outer = sdt(r#""#, &inner); + let body = format!(r#"{}{outer}"#, p(&t("First"))); + let (d, back, xml) = round_trip(&body); + assert_eq!(depth_of_control(&xml, r#""#), 0, "{xml}"); + assert_eq!(depth_of_control(&xml, r#""#), 0, "{xml}"); + assert_eq!(controls(&back), controls(&d)); + // The bookmark is kept. + assert!(xml.contains(r#"w:name="_GoBack""#), "{xml}"); +} diff --git a/crates/docx/tests/it/picture_links.rs b/crates/docx/tests/it/picture_links.rs new file mode 100644 index 0000000000..e7b5e44c22 --- /dev/null +++ b/crates/docx/tests/it/picture_links.rs @@ -0,0 +1,127 @@ +//! A picture's hyperlink (#5267): Word keeps it on the drawing (`wp:docPr/a:hlinkClick`, +//! ECMA-376 Part 1 §20.4.2.5) rather than in a `w:hyperlink` around the run. Original +//! synthetic packages; the 2×2 picture is generated in code. +use std::io::{Read, Write}; +use std::sync::Arc; +use wordcraft_doc::{CharProps, Document, InlineObject, Pos}; + +fn png() -> Vec { + let mut out = Vec::new(); + image::RgbaImage::from_pixel(2, 2, image::Rgba([20, 90, 200, 255])) + .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png) + .unwrap(); + out +} + +/// An inline picture's run; `link` goes into its `wp:docPr`. +fn picture_run(id: u32, link: &str) -> String { + format!( + r#"{link}"# + ) +} + +fn package(body: &str) -> Vec { + let xml = format!( + r#"{body}"# + ); + let rels = r#""#; + let doc_rels = r#""#; + let types = r#""#; + let png = png(); + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for (name, bytes) in [ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", rels.as_bytes()), + ("word/document.xml", xml.as_bytes()), + ("word/_rels/document.xml.rels", doc_rels.as_bytes()), + ("word/media/square.png", png.as_slice()), + ] { + zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + zip.write_all(bytes).unwrap(); + } + zip.finish().unwrap().into_inner() +} + +/// The link on each picture of the first paragraph, in order. +fn picture_links(doc: &Document) -> Vec> { + let p = doc.body[0].as_para().unwrap(); + p.object_offsets() + .into_iter() + .filter(|off| matches!(p.object_at(*off), Some(InlineObject::Image { .. }))) + .map(|off| p.props_of_char(off).link.clone()) + .collect() +} + +fn part(bytes: &[u8], name: &str) -> String { + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut s = String::new(); + z.by_name(name).unwrap().read_to_string(&mut s).unwrap(); + s +} + +#[test] +fn a_link_on_a_pictures_drawing_is_read() { + let body = format!( + "{}{}{}{}", + // Word's way: the link on the drawing. + picture_run(1, r#""#), + // A click naming a relationship that is no hyperlink, and one naming none. + picture_run(2, r#""#), + picture_run(3, r#""#), + // A link around the run wins over the drawing's. + picture_run(4, r#""#), + ); + let doc = wordcraft_docx::read(&package(&body)).unwrap(); + assert_eq!(picture_links(&doc), [Some("https://example.com/picture".to_string()), None, None, Some("https://example.com/run".to_string())]); +} + +#[test] +fn a_linked_picture_is_saved_with_its_link_on_the_drawing_too() { + let mut doc = Document::new(); + doc.media.insert("square.png".into(), Arc::new(png())); + let picture = || InlineObject::Image { + media: "square.png".into(), + w: 20.0, + h: 20.0, + alt: String::new(), + float: Default::default(), + crop: [0.0; 4], + ole: None, + fx: Default::default(), + }; + let linked = |l: &str| CharProps { link: Some(l.to_string()), ..Default::default() }; + // A place in this document stays a `w:hyperlink w:anchor` only: `a:hlinkClick` names a relationship. + doc.insert_object(&Pos::body(0, 0), picture(), &linked("#Spot")).unwrap(); + doc.insert_object(&Pos::body(0, 0), picture(), &linked("https://example.com/saved")).unwrap(); + let bytes = wordcraft_docx::write(&doc).unwrap(); + let xml = part(&bytes, "word/document.xml"); + let clicks: Vec<&str> = xml.match_indices("").unwrap(), "{xml}"); + let rels = part(&bytes, "word/_rels/document.xml.rels"); + assert!(rels.contains(&format!(r#"Id="{rid}""#)) && rels.contains(r#"Target="https://example.com/saved""#), "{rels}"); + // The run's own hyperlink names the same relationship, so the address is stored once. + assert_eq!(rels.matches("https://example.com/saved").count(), 1, "{rels}"); + let back = wordcraft_docx::read(&bytes).unwrap(); + assert_eq!(picture_links(&back), [Some("https://example.com/saved".to_string()), Some("#Spot".to_string())]); + // The drawing's link alone (as Word writes it) reads back the same. + let without_run_link = xml.replace(&format!(r#""#), "").replacen("", "", 1); + assert_ne!(without_run_link, xml); + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); + let mut out = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for i in 0..z.len() { + let mut f = z.by_index(i).unwrap(); + let name = f.name().to_string(); + let mut data = Vec::new(); + f.read_to_end(&mut data).unwrap(); + if name == "word/document.xml" { + data = without_run_link.clone().into_bytes(); + } + out.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + out.write_all(&data).unwrap(); + } + let edited = out.finish().unwrap().into_inner(); + assert_eq!(picture_links(&wordcraft_docx::read(&edited).unwrap())[0], Some("https://example.com/saved".to_string())); +} diff --git a/crates/docx/tests/it/roundtrip.rs b/crates/docx/tests/it/roundtrip.rs index 9738fd3724..d9cdc374c7 100644 --- a/crates/docx/tests/it/roundtrip.rs +++ b/crates/docx/tests/it/roundtrip.rs @@ -6,8 +6,8 @@ use wordcraft_doc::fill::Fill; use wordcraft_doc::numbering::ListKind; use wordcraft_doc::para::{Anchor, Float, FloatAlign, NoteKind, ShapeKind, Wrap}; use wordcraft_doc::props::{ - Align, Border, BorderStyle, Borders, CharProps, HeightRule, Highlight, LineSpacing, NumRef, ParaProps, Rgb, RowProps, TabAlign, TabLeader, - TabStop, TableLook, TextAlignment, TextColor, TextDirection, Underline, VAlign, VMerge, VertAlign, + Align, Border, BorderStyle, Borders, CharProps, HeightRule, Highlight, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, Rgb, RowProps, + TabAlign, TabLeader, TabStop, TableLook, TextAlignment, TextColor, TextDirection, Underline, VAlign, VMerge, VertAlign, }; use wordcraft_doc::section::{Columns, DocGrid, DocGridType, LineNumberRestart, LineNumbering, NumFormat, PageVAlign, SectionProps, SectionStart}; use wordcraft_doc::styles::{Style, StyleKind}; @@ -195,6 +195,7 @@ fn every_para_prop_round_trips() { suppress_hyphens: Some(true), suppress_line_numbers: Some(true), bidi: Some(false), + text_direction: Some(ParagraphTextDirection::TbRl), drop_cap: None, drop_cap_margin: None, drop_cap_distance: None, diff --git a/crates/docx/tests/it/section_text_direction.rs b/crates/docx/tests/it/section_text_direction.rs index c17656f40b..da44593bd1 100644 --- a/crates/docx/tests/it/section_text_direction.rs +++ b/crates/docx/tests/it/section_text_direction.rs @@ -18,6 +18,25 @@ fn docx(body: &str) -> Vec { zw.finish().unwrap().into_inner() } +fn docx_with_styles(body: &str, styles: &str) -> Vec { + let mut bytes = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let root = r#""#; + let document_rels = r#""#; + let doc = format!( + r#"{body}"# + ); + for (name, value) in [ + ("_rels/.rels", root.as_bytes()), + ("word/_rels/document.xml.rels", document_rels.as_bytes()), + ("word/document.xml", doc.as_bytes()), + ("word/styles.xml", styles.as_bytes()), + ] { + bytes.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + bytes.write_all(value).unwrap(); + } + bytes.finish().unwrap().into_inner() +} + fn document_xml(bytes: &[u8]) -> String { let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); let mut s = String::new(); @@ -59,3 +78,52 @@ fn explicit_horizontal_direction_is_kept_in_sections_and_cells() { let plain = wordcraft_docx::read(&docx(r#"Body"#)).unwrap(); assert!(!document_xml(&wordcraft_docx::write(&plain).unwrap()).contains("textDirection")); } + +#[test] +fn paragraph_text_direction_round_trips_through_two_saves_and_json() { + use wordcraft_doc::props::ParaProps; + use wordcraft_doc::props::ParagraphTextDirection; + + let values = ["lrTb", "lrTbV", "tb", "tbRl", "tbRlV", "tbLr", "tbLrV", "btLr", "tbV", "rl", "rlV", "lr", "lrV"]; + let mut body: String = + values.iter().map(|value| format!(r#"{value}"#)).collect(); + body.push_str(r#"Inherited"#); + let d = wordcraft_docx::read(&docx(&body)).unwrap(); + let paras: Vec<_> = d.body.iter().filter_map(|b| b.as_para()).collect(); + for (para, value) in paras.iter().zip(values) { + assert_eq!(para.props.text_direction, ParagraphTextDirection::from_ooxml(value), "{value}"); + } + assert_eq!(paras[values.len()].props.text_direction, None); + let json = serde_json::to_value(¶s[0].props).unwrap(); + assert_eq!(json["textDirection"], "lrTb"); + let decoded: ParaProps = serde_json::from_value(json).unwrap(); + assert_eq!(decoded.text_direction, Some(ParagraphTextDirection::LrTb)); + + let first = wordcraft_docx::write(&d).unwrap(); + let xml = document_xml(&first); + for value in values { + assert!(xml.contains(&format!(r#""#)), "{xml}"); + } + assert_eq!(xml.matches("w:textDirection").count(), values.len(), "inherited direction stays absent: {xml}"); + let second = wordcraft_docx::write(&wordcraft_docx::read(&first).unwrap()).unwrap(); + let twice = document_xml(&second); + assert_eq!(twice.matches("w:textDirection").count(), values.len(), "{twice}"); + for value in values { + assert!(twice.contains(&format!(r#""#)), "{twice}"); + } +} + +#[test] +fn paragraph_style_text_direction_is_preserved_and_direct_value_overrides_it() { + let styles = r#""#; + let body = r#"StyleOverride"#; + let d = wordcraft_docx::read(&docx_with_styles(body, styles)).unwrap(); + let xml = document_xml(&wordcraft_docx::write(&d).unwrap()); + assert_eq!(xml.matches(r#""#).count(), 2, "both paragraphs keep their style: {xml}"); + assert!(xml.contains(r#""#), "direct horizontal is explicit: {xml}"); + assert_eq!(xml.matches("w:textDirection").count(), 1, "only the direct paragraph writes a direction: {xml}"); + let mut z = zip::ZipArchive::new(std::io::Cursor::new(wordcraft_docx::write(&d).unwrap())).unwrap(); + let mut styles_xml = String::new(); + z.by_name("word/styles.xml").unwrap().read_to_string(&mut styles_xml).unwrap(); + assert!(styles_xml.contains(r#""#), "style value survives: {styles_xml}"); +} diff --git a/crates/docx/tests/it/strict_drawings.rs b/crates/docx/tests/it/strict_drawings.rs new file mode 100644 index 0000000000..70d78ae7bd --- /dev/null +++ b/crates/docx/tests/it/strict_drawings.rs @@ -0,0 +1,182 @@ +//! Shapes, text boxes, groups and canvases in a Strict document (#3548). ISO/IEC 29500 Strict +//! puts them in the one WordprocessingDrawing namespace, where Transitional documents use Word +//! 2010's `wps`, `wpg` and `wpc` namespaces. Fixtures are original and hand-written from +//! ECMA-376 Part 1, 5th edition (`dml-wordprocessingDrawing.xsd`, Strict and Transitional): each +//! drawing is written once and filled in with either set of namespaces. + +use std::io::{Read, Write}; + +use wordcraft_doc::{Document, InlineObject, PartKind}; + +const STRICT_NS: &str = r#"xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main" xmlns:r="http://purl.oclc.org/ooxml/officeDocument/relationships" xmlns:wp="http://purl.oclc.org/ooxml/drawingml/wordprocessingDrawing" xmlns:a="http://purl.oclc.org/ooxml/drawingml/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" w:conformance="strict""#; +const TRANSITIONAL_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas""#; +const STRICT_RELS: &str = r#""#; +const TRANSITIONAL_RELS: &str = r#""#; + +/// How a fixture names its drawing elements: the shape, group and canvas prefixes, and the +/// text box content element. +struct Names { + ns: &'static str, + rels: &'static str, + shape: &'static str, + group: &'static str, + canvas: &'static str, + content: &'static str, +} + +/// Word's Strict output: everything in `wp:`, text box content in `wne:txbxContent`. +const STRICT_WORD: Names = Names { ns: STRICT_NS, rels: STRICT_RELS, shape: "wp", group: "wp", canvas: "wp", content: "wne:txbxContent" }; +/// The Strict schema's own names: the text box content in `wp:` too. +const STRICT_SCHEMA: Names = Names { ns: STRICT_NS, rels: STRICT_RELS, shape: "wp", group: "wp", canvas: "wp", content: "wp:txbxContent" }; +/// The same drawings in a Transitional document. +const TRANSITIONAL: Names = + Names { ns: TRANSITIONAL_NS, rels: TRANSITIONAL_RELS, shape: "wps", group: "wpg", canvas: "wpc", content: "w:txbxContent" }; + +fn zip(entries: &[(&str, &[u8])]) -> Vec { + let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + for (name, bytes) in entries { + zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); + zw.write_all(bytes).unwrap(); + } + zw.finish().unwrap().into_inner() +} + +fn document_xml(bytes: &[u8]) -> String { + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut s = String::new(); + z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); + s +} + +fn solid(rgb: &str) -> String { + format!(r#""#) +} + +/// A `wsp` at (`x`, `y`) EMUs in its group, `cx` × `cy`. `text` gives it a text box. +fn shape(n: &Names, prst: &str, (x, y, cx, cy): (u32, u32, u32, u32), fill: &str, text: Option<&str>) -> String { + let s = n.shape; + let connector = prst.contains("Connector"); + let nv = if connector { + format!("<{s}:cNvCnPr/>") + } else if text.is_some() { + format!(r#"<{s}:cNvSpPr txBox="1"/>"#) + } else { + format!("<{s}:cNvSpPr/>") + }; + let fill = if connector { String::new() } else { solid(fill) }; + let line = + if connector { r#""# } else { "" }; + let txbx = match text { + Some(t) => { + let c = n.content; + format!(r#"<{s}:txbx><{c}>{t}"#) + } + None => String::new(), + }; + format!( + r#"<{s}:wsp><{s}:cNvPr id="{cx}" name="{prst}"/>{nv}<{s}:spPr>{fill}{line}{txbx}<{s}:bodyPr anchor="ctr"/>"# + ) +} + +/// Drawing `id`, anchored `x` EMUs from the page's left edge, 1 in × ½ in. +fn anchor(id: u32, x: u32, uri: &str, payload: &str) -> String { + format!( + r#"{x}914400{payload}"# + ) +} + +/// A rectangle, a text box, an arrow, a group holding a nested group, and a canvas holding a +/// shape and a group, each 1 in × ½ in. +fn package(n: &Names) -> Vec { + const WPS: &str = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"; + const WPG: &str = "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"; + const WPC: &str = "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"; + let full = (0, 0, 914_400, 457_200); + let half = (0, 0, 457_200, 457_200); + let g = n.group; + let group_xfrm = |(x, y, cx, cy): (u32, u32, u32, u32)| { + format!( + r#"<{g}:cNvGrpSpPr/><{g}:grpSpPr>{}"#, + solid("F39C12") + ) + }; + let group = format!( + "<{g}:wgp>{}{}<{g}:grpSp>{}{}", + group_xfrm(full), + shape(n, "ellipse", half, "28B463", None), + group_xfrm((457_200, 0, 457_200, 457_200)), + shape(n, "triangle", half, "8E44AD", None), + ); + let c = n.canvas; + let canvas = format!( + r#"<{c}:wpc><{c}:bg>{}<{c}:whole>{}{}<{g}:wgp>{}{}"#, + solid("FDF2E9"), + solid("A04000"), + shape(n, "roundRect", (45_720, 45_720, 365_760, 365_760), "16A085", None), + group_xfrm((457_200, 45_720, 365_760, 365_760)), + shape(n, "diamond", (0, 0, 365_760, 365_760), "2E86C1", None), + ); + let drawings = [ + anchor(1, 914_400, WPS, &shape(n, "rect", full, "C0392B", None)), + anchor(2, 2_286_000, WPS, &shape(n, "rect", full, "EAF2F8", Some("Strict text box"))), + anchor(3, 3_657_600, WPS, &shape(n, "straightConnector1", full, "", None)), + anchor(4, 5_029_200, WPG, &group), + anchor(5, 6_400_800, WPC, &canvas), + ]; + let doc = format!( + r#"Shapes{}"#, + n.ns, + drawings.concat() + ); + zip(&[("_rels/.rels", n.rels.as_bytes()), ("word/document.xml", doc.as_bytes())]) +} + +fn objects(d: &Document) -> Vec { + d.body[0].as_para().unwrap().objects.clone() +} + +/// The text of each text box story, in reading order. +fn box_texts(d: &Document) -> Vec { + let mut ids = Vec::new(); + d.objects_in_reading_order(&mut |o| ids.extend(o.text_boxes())); + ids.iter() + .filter_map(|id| d.parts.get(id).filter(|p| p.kind == PartKind::TextBox)) + .map(|p| p.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.clone()).collect::>().join("\n")) + .collect() +} + +#[test] +fn strict_shapes_read_like_their_transitional_twins() { + let transitional = wordcraft_docx::read(&package(&TRANSITIONAL)).unwrap(); + let want = objects(&transitional); + // The twin itself reads every drawing: a rectangle, a text box, an arrow, a group whose + // nested group is flattened into it, and a canvas with its background. + assert_eq!(want.len(), 5, "{want:#?}"); + assert!(matches!(&want[0], InlineObject::Shape { story: None, freeform: None, .. }), "{:?}", want[0]); + assert!(matches!(&want[1], InlineObject::Shape { story: Some(_), .. }), "{:?}", want[1]); + assert!(matches!(&want[3], InlineObject::Group { children, canvas: None, .. } if children.len() == 2), "{:?}", want[3]); + assert!( + matches!(&want[4], InlineObject::Group { children, canvas: Some(c), .. } if children.len() == 2 && c.fill.is_some() && c.stroke.is_some()), + "{:?}", + want[4] + ); + assert_eq!(box_texts(&transitional), ["Strict text box"]); + for (name, names) in [("Word's Strict names", &STRICT_WORD), ("the Strict schema's names", &STRICT_SCHEMA)] { + let strict = wordcraft_docx::read(&package(names)).unwrap(); + assert_eq!(objects(&strict), want, "{name}"); + assert_eq!(box_texts(&strict), ["Strict text box"], "{name}"); + } +} + +#[test] +fn strict_shapes_are_saved_and_reopened() { + let strict = wordcraft_docx::read(&package(&STRICT_WORD)).unwrap(); + let saved = wordcraft_docx::write(&strict).unwrap(); + let xml = document_xml(&saved); + for name in ["", "", "", "", "", "{content}"#), + // Text direction (#5193), as Word for Windows reads it: `vertical` and + // `vertical-ideographic` are `vert`; a `bottom-to-top` alt is `vert270` with any + // `layout-flow` or none; `horizontal-ideographic` stays horizontal. + shape("", r#"style="layout-flow:vertical""#), + shape("", r#"style="layout-flow:vertical;mso-layout-flow-alt:bottom-to-top;mso-next-textbox:#Text Box 4""#), + shape("", r#"style="layout-flow:vertical-ideographic""#), + shape("", r#"style="mso-layout-flow-alt:bottom-to-top""#), + shape("", r#"style="layout-flow:horizontal;mso-layout-flow-alt:bottom-to-top""#), + shape("", r#"style="layout-flow:horizontal-ideographic""#), ] .concat(); let doc = format!( @@ -140,13 +149,15 @@ fn vml_text_boxes_keep_their_inset_fit_and_anchor() { let rels = br#""#; let d = wordcraft_docx::read(&zip(&[("_rels/.rels", rels), ("word/document.xml", doc.as_bytes())])).unwrap(); let bodies: Vec = boxes(&d).iter().map(|id| d.parts[id].body).collect(); - assert_eq!(bodies.len(), 6); + assert_eq!(bodies.len(), 12); assert_eq!(bodies[0].inset, Some([0, 0, 0, 0])); assert_eq!((bodies[1].inset, bodies[1].auto_fit, bodies[1].anchor), (Some([36_000, 45_720, 25_400, 45_720]), true, VAlign::Center)); assert_eq!(bodies[2], TextBody::default()); assert_eq!((bodies[3].inset, bodies[3].anchor), (Some([12_700, 45_720, 0, wordcraft_doc::MAX_TEXT_BOX_INSET]), VAlign::Bottom)); assert_eq!(bodies[4].inset, None); assert_eq!(bodies[5].inset, Some([0, 0, 0, 0])); + let verts: Vec = bodies[6..].iter().map(|b| b.vert).collect(); + assert_eq!(verts, [TextVert::Vert, TextVert::Vert270, TextVert::Vert, TextVert::Vert270, TextVert::Vert270, TextVert::Horz]); // Saved and read again, they stay. let back = wordcraft_docx::read(&wordcraft_docx::write(&d).unwrap()).unwrap(); let again: Vec = boxes(&back).iter().map(|id| back.parts[id].body).collect(); diff --git a/crates/docx/tests/it/toc_ranges.rs b/crates/docx/tests/it/toc_ranges.rs index 15d8fa3f02..d384c430a5 100644 --- a/crates/docx/tests/it/toc_ranges.rs +++ b/crates/docx/tests/it/toc_ranges.rs @@ -228,6 +228,7 @@ fn hyperlink_field_targets_follow_the_field_code() { for (para, link) in [ (complex(r#"HYPERLINK "https://example.org/a" \l "sec""#), Some("https://example.org/a#sec")), (complex(r#"hyperlink \o "A tip" \t "_blank" "https://example.org/""#), Some("https://example.org/")), + (complex(r#"HYPERLINK "x \* Upper""#), Some("x * Upper")), (complex(r#"HYPERLINK "C:\\Docs\\My file.docx""#), Some(r"C:\Docs\My file.docx")), (simple(r#"HYPERLINK \l "Sec" \m"#), Some("#Sec")), (simple("HYPERLINK https://example.org/b"), Some("https://example.org/b")), diff --git a/crates/docx/tests/it/twips_wrap.rs b/crates/docx/tests/it/twips_wrap.rs new file mode 100644 index 0000000000..a560070428 --- /dev/null +++ b/crates/docx/tests/it/twips_wrap.rs @@ -0,0 +1,55 @@ +//! Paragraph indents and spacing written as plain twips beyond 16 bits: Word keeps only the low +//! 16 bits (ECMA-376 Part 1 §17.3.1.12 `w:ind`, §17.3.1.33 `w:spacing`). Measured in Word: a +//! `w:left` of 66256 starts the text 36 pt in, a `w:right` of 66976 leaves a 72 pt right indent. +//! Fixtures are hand-written. + +use wordcraft_doc::Document; +use wordcraft_doc::props::ParaProps; + +use crate::fixtures::zip; + +/// A package whose body is `body`. +fn docx(body: &str) -> Vec { + let root = r#""#; + let doc = format!( + r#"{body}"# + ); + zip(&[("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())]) +} + +fn props(body: &str) -> Vec { + let d: Document = wordcraft_docx::read(&docx(body)).unwrap(); + d.body.iter().filter_map(|b| b.as_para()).map(|p| p.props.clone()).collect() +} + +#[test] +fn indents_beyond_16_bits_wrap() { + let p = props( + r#"a + b + c + d"#, + ); + assert_eq!(p[0].indent_left, Some(36.0)); + assert_eq!(p[1].indent_right, Some(72.0)); + // 65556 -> 20 twips; -65516 -> 20 twips. + assert_eq!((p[2].indent_left, p[2].indent_right), (Some(1.0), Some(1.0))); + // 98304 = 0x18000 -> 0x8000 = -32768 as a signed 16-bit value; hanging 65896 -> 360. + assert_eq!((p[3].indent_left, p[3].indent_first), (Some(-1638.4), Some(-18.0))); +} + +#[test] +fn spacing_beyond_16_bits_wraps() { + let p = props(r#"a"#); + assert_eq!((p[0].space_before, p[0].space_after), (Some(0.05), Some(12.0))); +} + +#[test] +fn in_range_and_unit_values_are_unchanged() { + let p = props( + r#"a + b"#, + ); + assert_eq!((p[0].indent_left, p[0].indent_right, p[0].space_before), (Some(1638.35), Some(-1638.4), Some(3276.75))); + assert_eq!((p[1].indent_left, p[1].indent_right, p[1].space_before), (Some(72.0), Some(36.0), Some(12.0))); +} diff --git a/crates/docx/tests/it/word_xml.rs b/crates/docx/tests/it/word_xml.rs index dd614ae59f..99b05e865c 100644 --- a/crates/docx/tests/it/word_xml.rs +++ b/crates/docx/tests/it/word_xml.rs @@ -270,3 +270,12 @@ fn flat_opc_limits_count_what_is_not_kept() { // A part nothing reads, one element over what the XML tree reader takes in a part (8 000 000). assert!(limited(&format!(r#"{}"#, "".repeat(8_000_000)))); } + +/// A Word 2003 XML Document saved with Windows line ends: XML reads CR LF as one line feed (XML +/// 1.0 §2.11), so a line break inside `w:t` is one space, not two. +#[test] +fn word_2003_xml_with_windows_line_ends_reads_one_space_per_line_break() { + let xml = "\r\n\r\na\r\nb\r\n"; + let d = read_xml(xml.as_bytes()).unwrap(); + assert_eq!(paras(&d)[0].plain_text(), "a b"); +} diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index dcbdb7c831..0a8214b612 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -169,6 +169,9 @@ pub(crate) fn delete_selection_effect(s: &mut Session) -> Result Result Result { delete_selection_effect(s).map(|e| e.at) } diff --git a/crates/engine/src/cmd/canvas.rs b/crates/engine/src/cmd/canvas.rs index 8060f45ed7..3e62cdd7f2 100644 --- a/crates/engine/src/cmd/canvas.rs +++ b/crates/engine/src/cmd/canvas.rs @@ -5,6 +5,10 @@ //! top-left corner, unscaled). While a canvas is selected, Insert › Pictures, Shapes and Text Box //! put the new object inside it ([`add_member`]). Fit, Expand and Scale Drawing change its frame; //! Shape Fill and Shape Outline colour its background and border. +//! +//! A member can be picked on its own (#2637, `select.member`; a click on it does this): Shape +//! Format, Size, Rotate, Alt Text, Delete, Cut and Copy then act on that member, and Move or +//! Resize and Nudge move it inside the canvas. The canvas stays the selection in the text. use serde_json::{Value, json}; use wordcraft_doc::para::{CanvasStyle, Float, GroupChild, InlineObject, MAX_GROUP_CHILDREN}; @@ -14,6 +18,29 @@ use super::objects::object_selection; use super::{delete_selection, sel_result}; use crate::{CmdError, CmdResult, CommandSpec, Selection, Session, p}; +/// A member of the selected canvas picked on its own: its index, and the selection it was picked +/// in (it counts only while that selection stands). +#[derive(Clone, Debug, PartialEq)] +pub struct CanvasMember { + sel: Selection, + pub index: usize, +} + +/// The canvas member picked on its own: the canvas's position, the member's index and the member. +pub fn selected_member(s: &Session) -> Option<(Pos, usize, &InlineObject)> { + let m = s.member.as_ref().filter(|m| m.sel == s.sel)?; + let (pos, canvas) = selected_canvas(s)?; + let InlineObject::Group { children, .. } = canvas else { return None }; + let c = children.get(m.index).filter(|c| pickable(c))?; + Some((pos, m.index, &c.obj)) +} + +/// Whether a member can be picked on its own: a picture, shape or text box that is drawn (a +/// group inside a canvas isn't, nor a hidden member). +pub fn pickable(c: &GroupChild) -> bool { + c.obj.is_drawing() && !matches!(c.obj, InlineObject::Group { .. }) && !c.obj.frame().is_some_and(|(_, _, f)| f.hidden) +} + /// Largest canvas side and member offset, points (well past any page). const MAX_SIDE: f32 = 4000.0; /// Smallest canvas side, points. @@ -47,6 +74,10 @@ pub fn specs() -> Vec { CommandSpec::new("canvas.scale", "Scale Drawing", "Shape Format › Drawing Canvas", scale) .params(r#"{"factor": number (0.1–10)} (scales the frame and every member together)"#) .when(has_canvas), + CommandSpec::new("select.member", "Select Canvas Member", "Shape Format › Drawing Canvas", select_member) + .params(r#"{"index": n | null} (picks member `index` of the selected drawing canvas on its own: Shape Format, Size, Rotate, Alt Text, Delete, Cut and Copy then act on it, and Move or Resize and Nudge move it inside the canvas; null: the canvas itself again)"#) + .when(has_canvas) + .pure(), CommandSpec::new("canvas.member", "Canvas Member", "Shape Format › Drawing Canvas", member) .params(r#"{"index": n, "x"?: pt, "y"?: pt, "width"?: pt, "height"?: pt, "delete"?: bool} (moves, resizes or removes member `index` of the selected canvas; x/y from the canvas's top-left; with no change, returns the members)"#) .when(has_canvas), @@ -224,9 +255,171 @@ fn member(s: &mut Session, v: &Value) -> CmdResult { c.obj.set_size(nw.unwrap_or(w0).clamp(1.0, MAX_SIDE), nh.unwrap_or(h0).clamp(1.0, MAX_SIDE)); } })?; + // The indices after a removed member shift down, so a picked member would name another one. + if delete { + s.member = None; + } result(&o) } +/// `select.member`: pick a member of the selected canvas on its own, or (`null`) the canvas again. +fn select_member(s: &mut Session, v: &Value) -> CmdResult { + let Some((_, InlineObject::Group { children, .. })) = selected_canvas(s) else { + return Err(CmdError::Disabled("select a drawing canvas first".into())); + }; + let index = match v.get("index") { + None | Some(Value::Null) => None, + Some(_) => { + Some(p::u64(v, "index").and_then(|i| usize::try_from(i).ok()).ok_or_else(|| CmdError::Params("`index` is a member number".into()))?) + } + }; + let Some(i) = index else { + s.member = None; + return sel_result(s); + }; + let count = children.len(); + let c = children.get(i).ok_or_else(|| CmdError::Params(format!("no member {i} (the canvas has {count})")))?; + if !pickable(c) { + return Err(CmdError::Params(format!("member {i} can't be selected on its own"))); + } + let (w, h) = c.obj.frame().map(|(w, h, _)| (w, h)).unwrap_or((0.0, 0.0)); + let out = json!({"index": i, "x": c.x, "y": c.y, "width": w, "height": h, "object": serde_json::to_value(&c.obj).unwrap_or(Value::Null)}); + s.member = Some(CanvasMember { sel: s.sel.clone(), index: i }); + Ok(out) +} + +/// Delete, Backspace (and their word forms), Cut and Copy with a member picked act on the member +/// alone; the canvas stays selected. Typing and pasting, which would replace the whole canvas, +/// are disabled. `None`: another command, or no member picked. +pub(crate) fn member_command(s: &mut Session, id: &str, _: &Value) -> Option { + let replaces = (id.starts_with("text.") && id != "text.overtype") + || matches!( + id, + "edit.paste" | "edit.pasteText" | "edit.pasteMerge" | "edit.pasteSpecial" | "edit.pasteClipboardItem" | "edit.pasteAllClipboard" + ); + if !replaces && !matches!(id, "edit.cut" | "edit.copy") { + return None; + } + let (pos, index, _) = selected_member(s)?; + Some(match id { + "edit.copy" => copy_member(s, &pos, index), + "edit.cut" => copy_member(s, &pos, index).and_then(|r| remove_member(s, &pos, index).map(|_| r)), + "text.delete" | "text.backspace" | "text.deleteWordBack" | "text.deleteWordForward" => { + remove_member(s, &pos, index).and_then(|_| sel_result(s)) + } + _ => Err(CmdError::Disabled("a shape inside a drawing canvas is selected".into())), + }) +} + +/// Copy member `index` of the canvas at `pos` alone: pasted, it is a picture or shape of its own. +fn copy_member(s: &mut Session, pos: &Pos, index: usize) -> CmdResult { + let end = Pos { off: pos.off + wordcraft_doc::para::OBJ.len_utf8(), ..pos.clone() }; + let mut f = s.doc.copy_range(pos, &end); + let mut copied = false; + for b in &mut f.blocks { + let wordcraft_doc::Block::Para(p) = b else { continue }; + for o in &mut p.objects { + if let InlineObject::Group { children, canvas: Some(_), .. } = o + && let Some(c) = children.get(index) + { + *o = c.obj.clone(); + copied = true; + } + } + } + if !copied { + return Err(CmdError::Failed("the member vanished".into())); + } + s.clipboard_text = f.clipboard_text(); + s.clip_history.push(&f, &s.clipboard_text); + s.clipboard = Some(f); + Ok(super::edit::copy_result(s)) +} + +/// Take member `index` out of the canvas at `pos`; the canvas stays selected. +fn remove_member(s: &mut Session, pos: &Pos, index: usize) -> Result<(), CmdError> { + edit_canvas(s, pos, |_, _, children| { + if index < children.len() { + children.remove(index); + } + })?; + s.member = None; + Ok(()) +} + +/// Move or Resize with a member picked: `x`/`y` (the page point of its top-left, as dragging its +/// frame gives it) and the size become its place and size in the canvas. `None`: no member. +pub(crate) fn member_bounds(s: &mut Session, v: &Value) -> Option { + let (pos, index, obj) = selected_member(s)?; + let (w0, h0, _) = obj.frame()?; + let min = super::objects::min_size(obj); + let to = match (p::f32(v, "x"), p::f32(v, "y")) { + (Some(x), Some(y)) => Some((x, y)), + (None, None) => None, + _ => return Some(Err(CmdError::Params("`x` and `y` go together".into()))), + }; + // `x`/`y` are on the canvas's page: a drop on another page would put the member outside it. + if to.is_some() + && let Some(page) = p::u64(v, "page").and_then(|n| usize::try_from(n).ok()) + && s.layout().object(&pos, s.page_hint).is_some_and(|o| o.page != page) + { + return Some(Err(CmdError::Disabled("a shape can't leave its drawing canvas".into()))); + } + let (nw, nh) = (p::f32(v, "width"), p::f32(v, "height")); + Some(edit_member(s, &pos, index, |frame, (sx, sy), c| { + if let Some((x, y)) = to { + c.x = ((x - frame.x) / sx).clamp(-MAX_SIDE, MAX_SIDE); + c.y = ((y - frame.y) / sy).clamp(-MAX_SIDE, MAX_SIDE); + } + let w = nw.map_or(w0, |w| w / sx).clamp(min, MAX_SIDE); + let h = nh.map_or(h0, |h| h / sy).clamp(min, MAX_SIDE); + if (w, h) != (w0, h0) { + c.obj.set_size(w, h); + } + })) +} + +/// Nudge with a member picked: it moves inside the canvas. `None`: no member. +pub(crate) fn member_nudge(s: &mut Session, dx: f32, dy: f32) -> Option { + let (pos, index, _) = selected_member(s)?; + Some(edit_member(s, &pos, index, |_, (sx, sy), c| { + c.x = (c.x + dx / sx).clamp(-MAX_SIDE, MAX_SIDE); + c.y = (c.y + dy / sy).clamp(-MAX_SIDE, MAX_SIDE); + })) +} + +/// Change member `index` of the canvas at `pos`: `f` gets the canvas's frame on the page, the +/// scale of its members' space and the member. +fn edit_member(s: &mut Session, pos: &Pos, index: usize, f: impl FnOnce(wordcraft_geom::Rect, (f32, f32), &mut GroupChild)) -> CmdResult { + let frame = s.layout().object(pos, s.page_hint).ok_or_else(|| CmdError::Failed("the drawing canvas isn't laid out".into()))?.rect; + let canvas = + s.doc.para_at(pos).and_then(|p| p.object_at(pos.off)).cloned().ok_or_else(|| CmdError::Failed("the drawing canvas vanished".into()))?; + let scale = canvas.group_scale(frame.w, frame.h); + let mut f = Some(f); + let canvas = edit_canvas(s, pos, |_, _, children| { + if let (Some(c), Some(f)) = (children.get_mut(index), f.take()) { + f(frame, scale, c); + } + })?; + let InlineObject::Group { children, .. } = &canvas else { return result(&canvas) }; + let member = children.get(index).map(|c| serde_json::to_value(&c.obj).unwrap_or(Value::Null)).unwrap_or(Value::Null); + Ok(json!({"object": member, "pos": super::pos_json(pos), "member": index})) +} + +/// Change the picked member's own object (Shape Format, Size, Rotate, Alt Text) when `pos` is its +/// canvas. `None`: no member picked there. +pub(crate) fn edit_member_object(s: &mut Session, pos: &Pos, f: impl Fn(&mut InlineObject)) -> Option> { + let (at, index, _) = selected_member(s).filter(|(at, ..)| at == pos)?; + let mut out = None; + let r = edit_canvas(s, &at, |_, _, children| { + if let Some(c) = children.get_mut(index) { + f(&mut c.obj); + out = Some(c.obj.clone()); + } + }); + Some(r.and_then(|_| out.ok_or_else(|| CmdError::Failed("the member vanished".into())))) +} + /// Shape Fill and Shape Outline on a selected canvas colour its background and border. pub fn style_mut(o: &mut InlineObject) -> Option<&mut CanvasStyle> { match o { @@ -327,4 +520,131 @@ mod tests { fn canvas_in_text(s: &Session) -> InlineObject { s.doc.body.first().and_then(|b| b.as_para()).and_then(|p| p.objects.first()).cloned().unwrap() } + + fn member_fill(o: &InlineObject, i: usize) -> Option { + let InlineObject::Group { children, .. } = o else { panic!("{o:?}") }; + match &children[i].obj { + InlineObject::Shape { fill, .. } => fill.clone(), + o => panic!("{o:?}"), + } + } + + /// #2637: a shape inside a drawing canvas can be picked on its own, as in Word. Shape Format, + /// Move or Resize, Nudge, Delete and Copy then act on it rather than on the whole canvas, + /// which stays the selection in the text; the commands that place an object in the text + /// (wrapping, position) don't apply to it. + #[test] + fn a_canvas_member_is_selected_and_changed_on_its_own() { + let mut s = Session::new(wordcraft_doc::Document::new()); + s.run("insert.canvas", &json!({"width": 400, "height": 300})).unwrap(); + s.run("insert.shape", &json!({"kind": "rectangle", "width": 100, "height": 40})).unwrap(); + s.run("insert.shape", &json!({"kind": "ellipse", "width": 50, "height": 50})).unwrap(); + s.run("canvas.member", &json!({"index": 0, "x": 40, "y": 30})).unwrap(); + s.run("canvas.member", &json!({"index": 1, "x": 200, "y": 100})).unwrap(); + let canvas_sel = s.sel.clone(); + assert!(s.run("select.member", &json!({"index": 5})).is_err(), "no such member"); + s.run("select.member", &json!({"index": 1})).unwrap(); + assert_eq!(selected_member(&s).map(|(_, i, _)| i), Some(1)); + assert_eq!(s.sel, canvas_sel, "the canvas stays the selection in the text"); + + // Shape Fill colours the member, not the canvas or the other member. + let other = member_fill(&canvas(&s), 0); + s.run("shape.fill", &json!({"color": "FF0000"})).unwrap(); + let c = canvas(&s); + assert_eq!(member_fill(&c, 1), wordcraft_doc::props::Rgb::parse("FF0000").map(wordcraft_doc::fill::Fill::solid)); + assert_eq!(member_fill(&c, 0), other); + assert_eq!(c.canvas_style().and_then(|st| st.fill.clone()), None); + assert!(selected_member(&s).is_some(), "still picked after a change"); + + // Dragging its frame: the page point becomes its place in the canvas. + let frame = s.layout().object(&selected_canvas(&s).unwrap().0, 0).unwrap().rect; + s.run("arrange.bounds", &json!({"page": 0, "x": frame.x + 10.0, "y": frame.y + 20.0, "width": 60, "height": 30})).unwrap(); + assert_eq!(members(&canvas(&s))[1], (10.0, 20.0, 60.0, 30.0)); + assert_eq!(canvas(&s).frame().map(|(w, h, _)| (w, h)), Some((400.0, 300.0)), "the canvas keeps its frame"); + s.run("arrange.nudge", &json!({"dx": 6, "dy": -6})).unwrap(); + assert_eq!(members(&canvas(&s))[1], (16.0, 14.0, 60.0, 30.0)); + assert!(s.run("arrange.wrap", &json!({"wrap": "square"})).is_err(), "the canvas places its members"); + + // Copy puts the member alone on the clipboard; Delete takes it out of the canvas. + s.run("edit.copy", &json!({})).unwrap(); + let clip = s.clipboard.clone().unwrap(); + let copied: Vec<&InlineObject> = clip.blocks.iter().filter_map(|b| b.as_para()).flat_map(|p| p.objects.iter()).collect(); + assert!(matches!(copied.as_slice(), [InlineObject::Shape { kind: wordcraft_doc::para::ShapeKind::Ellipse, .. }]), "{copied:?}"); + s.run("text.delete", &json!({})).unwrap(); + assert_eq!(members(&canvas(&s)), [(40.0, 30.0, 100.0, 40.0)]); + assert!(selected_member(&s).is_none()); + assert_eq!(s.sel, canvas_sel, "the canvas is still selected"); + // Undo puts the member back. + s.run("edit.undo", &json!({})).unwrap(); + assert_eq!(members(&canvas_in_text(&s)).len(), 2); + + // Picking the canvas again (or anything else) ends the member's selection. + s.run("select.objects", &json!({"index": 0})).unwrap(); + s.run("select.member", &json!({"index": 0})).unwrap(); + s.run("select.member", &json!({"index": null})).unwrap(); + assert!(selected_member(&s).is_none()); + s.run("select.member", &json!({"index": 0})).unwrap(); + s.run("caret.docEnd", &json!({})).unwrap(); + assert!(selected_member(&s).is_none()); + // Pasted outside the canvas, the copied member is a shape of its own in the text. + s.run("edit.paste", &json!({})).unwrap(); + let para = s.doc.body.first().and_then(|b| b.as_para()).unwrap(); + assert!(matches!(para.objects.last(), Some(InlineObject::Shape { kind: wordcraft_doc::para::ShapeKind::Ellipse, .. })), "{:?}", para.objects); + } + + #[test] + fn typing_or_removing_a_member_never_hits_another_shape_or_the_canvas() { + let mut s = Session::new(wordcraft_doc::Document::new()); + s.run("insert.canvas", &json!({"width": 400, "height": 300})).unwrap(); + s.run("insert.shape", &json!({"kind": "rectangle", "width": 100, "height": 40})).unwrap(); + s.run("insert.shape", &json!({"kind": "ellipse", "width": 50, "height": 50})).unwrap(); + s.run("select.member", &json!({"index": 1})).unwrap(); + + // Typing or pasting would replace the whole canvas: refused, and the member stays picked. + for (id, v) in [ + ("text.insert", json!({"text": "x"})), + ("text.newParagraph", json!({})), + ("edit.paste", json!({"text": "x"})), + ("edit.pasteText", json!({"text": "x"})), + ] { + assert!(matches!(s.run(id, &v), Err(CmdError::Disabled(_))), "{id}"); + assert_eq!(members(&canvas(&s)).len(), 2, "{id}"); + } + assert_eq!(selected_member(&s).map(|(_, i, _)| i), Some(1)); + // Ctrl+Backspace removes the member alone, like Backspace. + s.run("text.deleteWordBack", &json!({})).unwrap(); + let sizes: Vec<(f32, f32)> = members(&canvas(&s)).iter().map(|m| (m.2, m.3)).collect(); + assert_eq!(sizes, [(100.0, 40.0)], "the ellipse went, the rectangle stays"); + assert!(selected_member(&s).is_none()); + + // Removing an earlier member through `canvas.member` drops the pick: its index would now + // name the next member. + s.run("insert.shape", &json!({"kind": "ellipse", "width": 50, "height": 50})).unwrap(); + s.run("insert.shape", &json!({"kind": "rectangle", "width": 20, "height": 20})).unwrap(); + s.run("select.member", &json!({"index": 1})).unwrap(); + s.run("canvas.member", &json!({"index": 0, "delete": true})).unwrap(); + assert!(selected_member(&s).is_none(), "the pick doesn't slide onto the member that moved up"); + + // Commands that replace the selection (the whole canvas) are refused too. + s.run("select.member", &json!({"index": 0})).unwrap(); + for (id, v) in [ + ("insert.table", json!({"rows": 1, "cols": 1})), + ("table.quick", json!({})), + ("insert.equation", json!({})), + ("insert.symbol", json!({"char": "§"})), + ("insert.field", json!({"instr": "AUTHOR"})), + ("insert.pageBreak", json!({})), + ] { + assert!(s.run(id, &v).is_err(), "{id}"); + assert_eq!(members(&canvas(&s)).len(), 2, "{id}: the canvas and its members stay"); + } + // Moving the member: on the canvas's page it moves; a drop on another page is refused. + let frame = s.layout().object(&selected_canvas(&s).unwrap().0, 0).unwrap(); + let before = members(&canvas(&s)); + let other = frame.page + 1; + assert!(matches!(s.run("arrange.bounds", &json!({"page": other, "x": 100, "y": 100})), Err(CmdError::Disabled(_)))); + assert_eq!(members(&canvas(&s)), before); + s.run("arrange.bounds", &json!({"page": frame.page, "x": frame.rect.x + 5.0, "y": frame.rect.y + 5.0})).unwrap(); + assert_ne!(members(&canvas(&s)), before); + } } diff --git a/crates/engine/src/cmd/caret.rs b/crates/engine/src/cmd/caret.rs index fa07b3e60a..23b83e7084 100644 --- a/crates/engine/src/cmd/caret.rs +++ b/crates/engine/src/cmd/caret.rs @@ -323,6 +323,7 @@ fn select_all(s: &mut Session, v: &Value) -> CmdResult { } let st = s.sel.focus.story; s.sel = Selection { anchor: s.doc.start_of(st), focus: s.doc.end_of(st) }; + s.pending = None; s.column = None; s.column_mode = false; sel_result(s) @@ -332,6 +333,7 @@ fn select_range(s: &mut Session, v: &Value) -> CmdResult { let a = v.get("anchor").and_then(parse_pos).ok_or_else(|| CmdError::Params("bad `anchor`".into()))?; let b = v.get("focus").and_then(parse_pos).ok_or_else(|| CmdError::Params("bad `focus`".into()))?; s.sel = Selection { anchor: s.doc.clamp(&a), focus: s.doc.clamp(&b) }; + s.pending = None; sel_result(s) } @@ -392,6 +394,7 @@ fn select_unit(s: &mut Session, unit: Unit) -> CmdResult { if s.doc.para_at(&f).is_some() { let (a, b) = unit_range(s, &f, unit); s.sel = Selection { anchor: a, focus: b }; + s.pending = None; } sel_result(s) } @@ -486,6 +489,9 @@ fn select_text(s: &mut Session, v: &Value) -> CmdResult { if count == n { s.sel = Selection { anchor: Pos { story, path: path.clone(), off: i }, focus: Pos { story, path: path.clone(), off: i + needle.len() } }; + // Formatting picked for the old caret doesn't carry over (as `select.word`): what + // replaces the selection takes its formatting. + s.pending = None; return sel_result(s); } } diff --git a/crates/engine/src/cmd/citations/sources.rs b/crates/engine/src/cmd/citations/sources.rs index 5426f880a2..8cc68bd0bd 100644 --- a/crates/engine/src/cmd/citations/sources.rs +++ b/crates/engine/src/cmd/citations/sources.rs @@ -201,7 +201,11 @@ pub(super) fn bib_order(doc: &Document, st: &CiteStyle, lang: Option) -> let mut idx: Vec = if cited.is_empty() { (0..doc.sources.len()).collect() } else if st.order == Order::Cited { - cited.iter().filter_map(|t| doc.sources.iter().position(|x| &x.tag == t)).collect() + let mut by_tag = std::collections::HashMap::new(); + for (i, source) in doc.sources.iter().enumerate() { + by_tag.entry(source.tag.as_str()).or_insert(i); + } + cited.iter().filter_map(|t| by_tag.get(t.as_str()).copied()).collect() } else { let cited: std::collections::HashSet<&str> = cited.iter().map(String::as_str).collect(); doc.sources.iter().enumerate().filter(|(_, x)| cited.contains(x.tag.as_str())).map(|(i, _)| i).collect() @@ -233,6 +237,21 @@ pub fn update_citations(s: &mut Session) -> Result<(), CmdError> { fn refresh_citations(s: &mut Session) -> Result<(), CmdError> { let st = style(&s.bib_style); let order = bib_order(&s.doc, st, crate::cmd::default_lang(s)); + // Native data can repeat tags: keep the first source record and, independently, the first + // bibliography position with that tag. Own the keys so paragraph writes don't borrow sources. + let fallback = order.len().saturating_add(1); + let mut by_tag = std::collections::HashMap::new(); + for (i, source) in s.doc.sources.iter().enumerate() { + by_tag.entry(source.tag.clone()).or_insert((i, fallback)); + } + for (n, &i) in order.iter().enumerate() { + if let Some(source) = s.doc.sources.get(i) + && let Some((_, number)) = by_tag.get_mut(&source.tag) + && *number == fallback + { + *number = n.saturating_add(1); + } + } for path in s.doc.para_paths(StoryRef::Body) { let Some(p) = s.doc.para(StoryRef::Body, &path) else { continue }; let updates: Vec<(usize, String)> = p @@ -242,8 +261,9 @@ fn refresh_citations(s: &mut Session) -> Result<(), CmdError> { .filter_map(|(k, o)| match o { InlineObject::Field { instr, .. } => { let (tag, pages) = citation_field(instr)?; - let src = s.doc.sources.iter().find(|x| x.tag == tag)?; - Some((k, cite(src, st.name, number_of(&s.doc, &order, tag), pages))) + let &(i, number) = by_tag.get(tag)?; + let src = s.doc.sources.get(i)?; + Some((k, cite(src, st.name, number, pages))) } _ => None, }) diff --git a/crates/engine/src/cmd/citations/tests.rs b/crates/engine/src/cmd/citations/tests.rs index 71580316c7..516e99952b 100644 --- a/crates/engine/src/cmd/citations/tests.rs +++ b/crates/engine/src/cmd/citations/tests.rs @@ -785,3 +785,118 @@ fn gost_numbers_follow_its_name_or_title_sort() { assert_eq!(find_style("Harvard - Anglia 2008").map(|x| x.name), Some("Harvard (Anglia)")); assert!(find_style("").is_none() && find_style("Vancouver").is_none()); } + +fn lookup_session(tags: &[&str]) -> Session { + let mut p = Paragraph::new(); + for tag in tags { + p.insert_object( + p.len(), + InlineObject::Field { + instr: format!("CITATION {tag}"), + result: "Original cache".into(), + locked: false, + dirty: true, + code: false, + form: String::new(), + }, + &CharProps::default(), + ) + .unwrap(); + } + let mut doc = Document::new(); + doc.body = vec![wordcraft_doc::para_block(p)]; + Session::new(doc) +} + +#[test] +fn citation_source_lookups_keep_first_duplicates_case_and_style_order() { + let mut s = lookup_session(&["A", "B", "missing", "a", "B"]); + s.doc.sources = [ + ("B", "Zed, Zo", "Zulu"), + ("A", "Able, Ann", "Alpha"), + ("B", "Baker, Ben", "Beta duplicate"), + ("a", "Middle, May", "Middle"), + ("unused", "Other, One", "Unused"), + ] + .into_iter() + .map(|(tag, author, title)| Source { tag: tag.into(), author: author.into(), title: title.into(), year: "2024".into(), ..Default::default() }) + .collect(); + let original = s.doc.clone(); + for st in &STYLES { + s.bib_style = st.name.into(); + let expected = if st.order == Order::Cited { vec![1, 0, 3] } else { vec![1, 2, 3, 0] }; + assert_eq!(sources::bib_order(&s.doc, st, None), expected, "{}", st.name); + update_citations(&mut s).unwrap(); + let p = s.doc.para(StoryRef::Body, &Path::top(0)).unwrap(); + for (k, (source, number)) in [(1, 1), (0, 2), (usize::MAX, 0), (3, 3), (0, 2)].into_iter().enumerate() { + let InlineObject::Field { instr, result, locked, dirty, code, form } = &p.objects[k] else { panic!() }; + let InlineObject::Field { instr: before, .. } = &original.para(StoryRef::Body, &Path::top(0)).unwrap().objects[k] else { panic!() }; + assert_eq!(instr, before); + let expected = original.sources.get(source).map(|x| cite(x, st.name, number, "")).unwrap_or_else(|| "Original cache".into()); + assert_eq!(result, &expected, "{}: {instr}", st.name); + assert!(!locked && *dirty && !code && form.is_empty()); + } + assert_eq!(s.doc.sources, original.sources); + } + // Without citations even duplicate and unused source records remain in the bibliography. + s.doc.body.clear(); + assert_eq!(sources::bib_order(&s.doc, style("IEEE"), None), vec![0, 1, 2, 3, 4]); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn citation_refresh_source_lookup_work_scales_with_document_size() { + fn doc(n: usize) -> Session { + let mut p = Paragraph::new(); + p.text = wordcraft_doc::para::OBJ.to_string().repeat(n); + p.runs = vec![wordcraft_doc::Run { len: p.text.len(), props: CharProps::default() }]; + p.objects = (0..n) + .rev() + .map(|i| InlineObject::Field { + instr: format!("CITATION T{i}"), + result: String::new(), + locked: false, + dirty: false, + code: false, + form: String::new(), + }) + .collect(); + let mut d = Document::new(); + d.body = vec![wordcraft_doc::para_block(p)]; + d.sources = (0..n).map(|i| Source { tag: format!("T{i}"), ..Default::default() }).collect(); + let mut s = Session::new(d); + s.bib_style = "IEEE".into(); + s + } + // Use the running thread's CPU time on Linux; scheduler pauses are not lookup work. + fn cost(f: &mut impl FnMut()) -> f64 { + let ran = || std::fs::read_to_string("/proc/thread-self/schedstat").ok()?.split_whitespace().next()?.parse::().ok(); + let (t, before) = (std::time::Instant::now(), ran()); + f(); + match (before, ran()) { + (Some(a), Some(b)) if b > a => (b - a) as f64 * 1e-9, + _ => t.elapsed().as_secs_f64(), + } + } + const SCALE: usize = 8; + let (mut small, mut big) = (doc(2_000), doc(SCALE * 2_000)); + update_citations(&mut small).unwrap(); + update_citations(&mut big).unwrap(); + let (mut a, mut b) = (f64::INFINITY, f64::INFINITY); + for _ in 0..5 { + a = a.min( + cost(&mut || { + for _ in 0..SCALE { + update_citations(&mut small).unwrap(); + } + }) / SCALE as f64, + ); + b = b.min(cost(&mut || { + update_citations(&mut big).unwrap(); + })); + } + let ratio = b / a.max(1e-9); + assert!(ratio < 32.0, "citation refresh: {a:.6}s -> {b:.6}s, {ratio:.1}x for {SCALE}x input"); + let p = big.doc.para(StoryRef::Body, &Path::top(0)).unwrap(); + assert!(p.objects.iter().enumerate().all(|(i, o)| matches!(o, InlineObject::Field { result, .. } if result == &format!("[{}]", i + 1)))); +} diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 277df7860c..a55ce7571e 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -361,7 +361,7 @@ impl ClipHistory { self.items.is_empty() } - fn push(&mut self, f: &Fragment, text: &str) { + pub(crate) fn push(&mut self, f: &Fragment, text: &str) { // Text plus per-paragraph overhead; pictures are references into the document. let paras = text.matches('\n').count().saturating_add(f.blocks.len()).saturating_add(f.parts.len()).saturating_add(1); let bytes = text.len().saturating_mul(2).saturating_add(paras.saturating_mul(256)); diff --git a/crates/engine/src/cmd/equation.rs b/crates/engine/src/cmd/equation.rs index 28e4c96bf2..9e252af0bf 100644 --- a/crates/engine/src/cmd/equation.rs +++ b/crates/engine/src/cmd/equation.rs @@ -326,8 +326,14 @@ fn type_text(s: &mut Session, v: &Value) -> CmdResult { pos = p; changed = true; } - if let Some(p) = me::build_up(&mut math.nodes, &pos, latex) { - pos = p; + // As in Word, `\int` + Space leaves ∫ waiting for its limits: `_a^b` and then + // Space, or Space alone, builds the operator (#4917). + let waits = changed && c == ' ' && me::nary_before(&math.nodes, &pos); + // Only an operator this build-up makes takes the caret: one already there (from + // the gallery, say) keeps it after it. + let built_before = me::nary_node_before(&math.nodes, &pos); + if !waits && let Some(p) = me::build_up(&mut math.nodes, &pos, latex) { + pos = if built_before { p } else { me::into_empty_operand(&math.nodes, &p) }; changed = true; } if changed { @@ -336,6 +342,14 @@ fn type_text(s: &mut Session, v: &Value) -> CmdResult { if c == ' ' && changed { continue; } + } else if !style.nor && !latex && !c.is_alphanumeric() { + // Word corrects `\name` on any character that can't continue it (`\int_`, + // `\alpha+`), keeps that character and builds nothing up yet. LaTeX input keeps + // its commands (`\sqrt{`). + if let Some(p) = me::autocorrect(&mut math.nodes, &pos) { + pos = p; + transformed = true; + } } pos = me::insert_text(&mut math.nodes, &pos, &c.to_string(), style); } diff --git a/crates/engine/src/cmd/equation/tests.rs b/crates/engine/src/cmd/equation/tests.rs index 6fc52c1d1d..6a568fa40a 100644 --- a/crates/engine/src/cmd/equation/tests.rs +++ b/crates/engine/src/cmd/equation/tests.rs @@ -530,3 +530,112 @@ fn equation_delete_surrounding_moves_the_caret_for_a_one_step_delete() { assert_eq!(linear(&s), "bc"); assert_eq!(s.math.as_ref().unwrap().pos, MathPos::new(Vec::new(), 0)); } + +/// The n-ary operator in the equation, with its limits (shown or not) and operand as text. +fn nary_of(s: &Session) -> Option<(char, Option, Option, String)> { + nodes_of(s, &s.math.as_ref()?.at)?.iter().find_map(|n| match n { + MNode::Nary { chr, sub_hide, sup_hide, sub, sup, e, .. } => { + Some((*chr, (!sub_hide).then(|| to_linear(sub)), (!sup_hide).then(|| to_linear(sup)), to_linear(e))) + } + _ => None, + }) +} + +#[test] +fn an_autocorrected_operator_waits_for_its_limits() { + // #4917: as in Word, `\int` + Space gives a ∫ that `_x^y` then Space decorates; the + // operand typed next goes inside the operator. + let typed = |keys: &[&str]| { + let mut s = session(); + s.run("insert.equation", &json!({})).unwrap(); + for t in keys { + s.run("equation.type", &json!({"text": t})).unwrap(); + } + s + }; + let s = typed(&[r"\int", " "]); + assert_eq!(linear(&s), "∫", "the operator waits, not yet built"); + assert!(nary_of(&s).is_none()); + let s = typed(&[r"\int", " ", "_x^y", " ", "f"]); + assert_eq!(nary_of(&s), Some(('∫', Some("x".into()), Some("y".into()), "f".into()))); + let s = typed(&[r"\sum", " ", "_(k=1)^n", " ", "k"]); + assert_eq!(nary_of(&s), Some(('∑', Some("k=1".into()), Some("n".into()), "k".into()))); + // Only a lower limit. + let s = typed(&[r"\prod", " ", "_i", " ", "a"]); + assert_eq!(nary_of(&s), Some(('∏', Some("i".into()), None, "a".into()))); + // Space alone builds it without limits, the caret in its operand. + let s = typed(&[r"\int", " ", " ", "x"]); + assert_eq!(nary_of(&s), Some(('∫', None, None, "x".into()))); + // Typing the operand straight after it, as before. + let s = typed(&[r"\int", " ", "x", " "]); + assert_eq!(nary_of(&s), Some(('∫', None, None, "x".into()))); + // Enter builds the operator and leaves the caret after it; Right builds it and, at the + // equation's end, leaves the equation, both as before. + let mut s = typed(&[r"\int", " ", "_0^1"]); + s.run("equation.enter", &json!({})).unwrap(); + assert_eq!(nary_of(&s), Some(('∫', Some("0".into()), Some("1".into()), String::new()))); + assert_eq!(s.math.as_ref().unwrap().pos, MathPos::new(Vec::new(), 1)); + let mut s = typed(&[r"\int", " ", "_0^1"]); + let at = s.math.as_ref().unwrap().at.clone(); + s.run("equation.move", &json!({"dir": "right"})).unwrap(); + assert!(s.math.is_none(), "Right at the end leaves the equation"); + assert!(matches!(nodes_of(&s, &at).as_deref(), Some([MNode::Nary { e, .. }]) if e.is_empty())); + // One undo step takes the whole typing away, leaving the empty equation. + let mut s = typed(&[r"\int", " ", "_0^1", " ", "t"]); + let at = s.math.as_ref().unwrap().at.clone(); + s.run("equation.exit", &json!({})).unwrap(); + s.run("edit.undo", &json!({})).unwrap(); + let left = s.doc.para_at(&at).and_then(|p| p.object_at(at.off)).map(|o| o.plain_text().to_string()); + assert_eq!(left.as_deref(), Some(""), "the equation is there, empty"); + // An operator ending another one's operand (`∫∫`) takes what is typed next. + let s = typed(&[r"\int", " ", r"\int", " ", " ", "f"]); + let inner = nary_of(&s).and_then(|_| { + nodes_of(&s, &s.math.as_ref()?.at)?.iter().find_map(|n| match n { + MNode::Nary { e, .. } => match e.as_slice() { + [MNode::Nary { e, .. }] => Some(to_linear(e)), + _ => None, + }, + _ => None, + }) + }); + assert_eq!(inner.as_deref(), Some("f"), "f is the inner operator's operand"); + // Space after an operator that was already there (from the gallery, its operand left empty) + // doesn't send the caret back into it: what is typed next follows the operator. + let mut s = session(); + s.run("insert.equation", &json!({})).unwrap(); + s.run("equation.insertStructure", &json!({"id": "nary.sumLimits"})).unwrap(); + s.run("equation.type", &json!({"text": "k"})).unwrap(); + if let Some(m) = s.math.as_mut() { + m.pos = MathPos::new(Vec::new(), 1); + } + s.run("equation.type", &json!({"text": " x"})).unwrap(); + assert_eq!(nary_of(&s).map(|n| n.3), Some(String::new()), "the operand stays empty"); + assert_eq!(s.math.as_ref().unwrap().pos.path, Vec::new(), "the caret stays after the operator"); +} + +#[test] +fn a_math_name_is_corrected_by_any_character_that_cannot_continue_it() { + // Observed in Word for Windows (#4938): `\int_0^1` typed in one go gives ∫ as soon as `_` is + // typed, so Space then builds the integral with its limits; `\alpha` + `+` gives α+, and a + // letter or digit leaves the name as typed. + let typed = |keys: &[&str]| { + let mut s = session(); + s.run("insert.equation", &json!({})).unwrap(); + for t in keys { + s.run("equation.type", &json!({"text": t})).unwrap(); + } + s + }; + let s = typed(&[r"\int_0^1", " ", "x"]); + assert_eq!(nary_of(&s), Some(('∫', Some("0".into()), Some("1".into()), "x".into()))); + let s = typed(&[r"\alpha+\beta\gamma1"]); + assert_eq!(linear(&s), r"α+β\gamma1"); + let s = typed(&[r"\alpha(x)"]); + assert_eq!(linear(&s), "α(x)"); + // LaTeX input keeps its commands until they are built up. + let mut s = session(); + s.run("equation.inputFormat", &json!({"format": "latex"})).unwrap(); + s.run("insert.equation", &json!({})).unwrap(); + s.run("equation.type", &json!({"text": r"\alpha+"})).unwrap(); + assert_eq!(linear(&s), r"\alpha+"); +} diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 67284a2474..c9d523c5d6 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -253,27 +253,8 @@ fn open(s: &mut Session, v: &Value) -> CmdResult { ); } let password = p::str(v, "password"); - let (mut doc, encrypted) = if let Some(data) = p::str(v, "data") { - let bytes = super::insert::base64_decode(data).ok_or_else(|| CmdError::Params("bad base64".into()))?; - // An empty file (File Explorer's New › Microsoft Word Document makes one) opens as a new - // blank document that saves back to it (#1384). - if bytes.is_empty() { - (normal_document(s), false) - } else { - crate::io::open_bytes_in(path, &bytes, password, &s.region()).map_err(CmdError::Failed)? - } - } else if crate::io::is_empty_file(std::path::Path::new(path)) { - (normal_document(s), false) - } else { - crate::io::open_path_in(std::path::Path::new(path), password, &s.region()).map_err(CmdError::Failed)? - }; - // Plain text and Markdown carry no language: they are in the editing language. - let ext = std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase(); - if let Some(lang) = s.prefs.default_language.as_ref().or(s.editing_lang.as_ref()) - && matches!(ext.as_str(), "txt" | "text" | "md" | "markdown") - { - Arc::make_mut(&mut doc.styles).default_chr.lang = Some(lang.clone()); - } + // An empty file opens as a new blank document that saves back to it (#1384). + let (doc, encrypted) = read_document(s, path, v)?; match s.used_because() { None => s.set_document(doc), Some(why) => { @@ -294,6 +275,53 @@ fn open(s: &mut Session, v: &Value) -> CmdResult { ) } +/// The document the file at `path` holds (or its bytes, base64 in `data`, with `path` only +/// naming it), read with `password`, and whether it was encrypted: what `file.open` and +/// `file.newFromTemplate` read. An empty file (File Explorer's New › Microsoft Word Document makes +/// one) is a new blank document (#1384). Plain text and Markdown carry no language, so they are +/// in the editing language. +fn read_document(s: &Session, path: &str, v: &Value) -> Result<(Document, bool), CmdError> { + let password = p::str(v, "password"); + let (mut doc, encrypted) = if let Some(data) = p::str(v, "data") { + let bytes = super::insert::base64_decode(data).ok_or_else(|| CmdError::Params("bad base64".into()))?; + if bytes.is_empty() { + (normal_document(s), false) + } else { + crate::io::open_bytes_in(path, &bytes, password, &s.region()).map_err(CmdError::Failed)? + } + } else if crate::io::is_empty_file(std::path::Path::new(path)) { + (normal_document(s), false) + } else { + crate::io::open_path_in(std::path::Path::new(path), password, &s.region()).map_err(CmdError::Failed)? + }; + let ext = std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase(); + if let Some(lang) = s.prefs.default_language.as_ref().or(s.editing_lang.as_ref()) + && matches!(ext.as_str(), "txt" | "text" | "md" | "markdown") + { + Arc::make_mut(&mut doc.styles).default_chr.lang = Some(lang.clone()); + } + Ok((doc, encrypted)) +} + +/// File › New from a template (#2852): a new, untitled document with the template's content, +/// attached to the template as in Word, read as `file.open` reads a file (`data`, `password`). +/// It opens beside the open documents, as `file.new` does. With `replaceBlank` an untouched blank +/// document gives way to it, as to a file `file.open` opens: that is how a template the platform +/// hands the app starts (#3433). The template is only read; the new document has no file, so +/// saving it asks where to write, and a protected template's password doesn't come along. +pub(crate) fn new_from_template(s: &mut Session, v: &Value) -> CmdResult { + let path = p::req_str(v, "path")?; + let (mut doc, _) = read_document(s, path, v)?; + doc.settings.attached_template = Some(path.to_string()); + if p::bool(v, "replaceBlank") == Some(true) && s.used_because().is_none() { + s.set_document(doc); + } else { + s.add_document(doc); + } + s.dirty = true; + sel_result(s) +} + /// Close a document with no window to ask about it (agents, headless sessions). fn close_document(s: &mut Session, v: &Value) -> CmdResult { let id = match v.get("id").filter(|x| !x.is_null()) { diff --git a/crates/engine/src/cmd/find_pattern.rs b/crates/engine/src/cmd/find_pattern.rs index 6ceab28a00..104b3d3e91 100644 --- a/crates/engine/src/cmd/find_pattern.rs +++ b/crates/engine/src/cmd/find_pattern.rs @@ -198,7 +198,9 @@ fn special(query: &str, o: &Opts) -> (String, bool) { if (o.ignore_punct && is_punct(c)) || (o.ignore_space && is_space(c)) { continue; } - atoms.push(lit(c)); + // A keyboard apostrophe also finds text produced by smart quotes. Explicit character + // codes above, and literal/regex/wildcard searches, keep exact character matching. + atoms.push(if c == '\'' { "['‘’]".into() } else { lit(c) }); } let filler = match (o.ignore_punct, o.ignore_space) { (true, true) => r"[\p{P} \t\x{A0}]*", diff --git a/crates/engine/src/cmd/fmt_revisions.rs b/crates/engine/src/cmd/fmt_revisions.rs index 330bdfd93f..06f92831c0 100644 --- a/crates/engine/src/cmd/fmt_revisions.rs +++ b/crates/engine/src/cmd/fmt_revisions.rs @@ -212,6 +212,32 @@ pub(crate) fn record(s: &mut Session, id: &str, before: &Document) { } } +/// Builds the tracked paragraph-property change for a command that also changes the paragraph's +/// text. The ordinary after-command recorder deliberately compares only paragraphs with identical +/// text, so mixed text/paragraph-format transformations use this before assigning their props. +/// Made once per command, before it creates revisions: it keeps the revisions as they were then +/// (all `Recorder::own` needs), instead of copying the list for every paragraph. +pub(crate) struct ParaPropTracker { + before: Vec, +} + +impl ParaPropTracker { + pub(crate) fn new(s: &Session) -> Self { + let tracking = s.doc.settings.track_changes && s.prefs.markup.track_formatting; + ParaPropTracker { before: if tracking { s.doc.revisions.clone() } else { Vec::new() } } + } + + pub(crate) fn changed(&self, s: &mut Session, before: &ParaProps, after: &ParaProps) -> Option>> { + if !s.doc.settings.track_changes || !s.prefs.markup.track_formatting { + return after.fmt_change.clone(); + } + let mut r = Recorder { author: s.author.clone(), date: super::now_iso(), before: &self.before, revisions: &mut s.doc.revisions, rid: None }; + r.change(before, after, ParaProps::formatting, |p| &p.fmt_change) + .map(|Replacement(change)| change) + .unwrap_or_else(|| after.fmt_change.clone()) + } +} + /// Record the paragraph formatting a command changed on the paragraph at `path` whose text it also /// changed, which [`record`] doesn't compare: `before` is the paragraph's properties before the /// command. As in [`record`], the author's own earlier change keeps the formatting before it and diff --git a/crates/engine/src/cmd/insert/links.rs b/crates/engine/src/cmd/insert/links.rs index b280d449bb..2a0535a651 100644 --- a/crates/engine/src/cmd/insert/links.rs +++ b/crates/engine/src/cmd/insert/links.rs @@ -63,6 +63,17 @@ pub(super) fn link(s: &mut Session, v: &Value) -> CmdResult { let end = s.doc.insert_text(&at, &text, &props)?; s.sel = Selection::caret(end); s.pending = Some(CharProps::default()); + } else if let Some(text) = retyped_text(s, v) { + // Text to display changed: the selection is retyped with it, then linked (as in Word). + crate::cmd::paste_text(s, &text)?; + let end = s.sel.focus.clone(); + let start = Pos { off: end.off.saturating_sub(text.len()), ..end.clone() }; + s.doc.format_range(&start, &end, &|c| { + c.link = Some(url.clone()); + c.style = style.clone(); + })?; + s.sel = Selection::caret(end); + s.pending = Some(CharProps::default()); } else { let (a, b) = s.sel.ordered(); s.doc.format_range(&a, &b, &|c| { @@ -73,6 +84,33 @@ pub(super) fn link(s: &mut Session, v: &Value) -> CmdResult { sel_result(s) } +/// The `text` to retype a selection with before linking it: only when it differs from the selected +/// text and both fit in one line of one paragraph without pictures or fields, outside any field's +/// result. Otherwise (as Word shows `<>`) the selection keeps its text. +fn retyped_text(s: &Session, v: &Value) -> Option { + use wordcraft_doc::para::{COLUMN_BREAK, LINE_BREAK, OBJ, PAGE_BREAK}; + /// What can't stand in one line of text: breaks, and pictures or fields. + const NOT_ONE_LINE: [char; 7] = [LINE_BREAK, '\r', '\u{2029}', '\u{b}', PAGE_BREAK, COLUMN_BREAK, OBJ]; + let text = p::str(v, "text").filter(|t| !t.is_empty() && t.len() <= 4096)?; + if text.contains(NOT_ONE_LINE) { + return None; + } + let (a, b) = s.sel.ordered(); + if a.story != b.story || a.path != b.path { + return None; + } + let shown = s.doc.para(a.story, &a.path)?.text.get(a.off..b.off)?; + if shown.contains(NOT_ONE_LINE) || shown == text { + return None; + } + // A selection in a field's result (between its markers, which it doesn't hold) is retyped by + // the next update of the field, not here. + if s.doc.field_ranges(a.story).iter().any(|r| r.start < b && a <= r.end) { + return None; + } + Some(text.to_string()) +} + /// Whether `name` can name a bookmark: starts with a letter or underscore, contains only alphanumeric characters /// or underscores, and is at most 40 characters (#3491). Word allows leading underscores for hidden bookmarks (_Toc, _Ref). pub fn bookmark_name_ok(name: &str) -> bool { diff --git a/crates/engine/src/cmd/lists.rs b/crates/engine/src/cmd/lists.rs index 1338eeb5e1..987e9fd537 100644 --- a/crates/engine/src/cmd/lists.rs +++ b/crates/engine/src/cmd/lists.rs @@ -452,6 +452,36 @@ mod tests { assert_eq!(numbered(&mut b), want); } + /// #5757: a list numbered in Thai digits shows them on the page, with the level's text around + /// them (`๑.`, `(๒)`), and keeps its format through a save to .docx and back. + #[test] + fn a_thai_numbered_list_shows_thai_digits_and_survives_docx() { + let mut s = Session::new(Document::new()); + for (i, t) in ["หนึ่ง", "สอง", "สาม", "a", "b", "c", "d", "e", "f", "g", "h"].iter().enumerate() { + if i > 0 { + s.run("text.newParagraph", &json!({})).unwrap(); + } + s.run("text.insert", &json!({"text": t})).unwrap(); + } + s.run("select.all", &json!({})).unwrap(); + let levels = json!([{"format": "thaiNumbers", "text": "%1.", "start": 1}]); + s.run("list.define", &json!({"levels": levels})).unwrap(); + let page: Vec> = numbered(&mut s).into_iter().map(|x| x.0).collect(); + let want: Vec> = + ["๑.", "๒.", "๓.", "๔.", "๕.", "๖.", "๗.", "๘.", "๙.", "๑๐.", "๑๑."].iter().map(|l| Some(l.to_string())).collect(); + assert_eq!(page, want); + let back = wordcraft_docx::read(&wordcraft_docx::write(&s.doc).unwrap()).unwrap(); + let mut b = Session::new(back); + let again: Vec> = numbered(&mut b).into_iter().map(|x| x.0).collect(); + assert_eq!(again, want, "thaiNumbers survives .docx"); + // In parentheses too. + let mut s = Session::new(Document::new()); + s.run("text.insert", &json!({"text": "x"})).unwrap(); + s.run("select.all", &json!({})).unwrap(); + s.run("list.define", &json!({"levels": [{"format": "thaiNumbers", "text": "(%1)", "start": 2}]})).unwrap(); + assert_eq!(numbered(&mut s)[0].0.as_deref(), Some("(๒)")); + } + /// #1793: the other heading presets of Word's List Library, level by level. #[test] fn every_heading_preset_numbers_three_heading_levels() { @@ -459,7 +489,8 @@ mod tests { ("headings", [Some("1"), Some("1.1"), Some("1.1.1")]), ("article", [Some("Article I."), Some("Section 1.01"), Some("(a)")]), ("outlineHeadings", [Some("I."), Some("A."), Some("1.")]), - ("chapter", [Some("Chapter 1"), None, None]), + // The lower levels are "none" levels with no text: an empty label, as in Word (#4985). + ("chapter", [Some("Chapter 1"), Some(""), Some("")]), ] { let mut s = headings("Heading3"); s.run("list.multilevelPreset", &json!({"preset": preset})).unwrap(); diff --git a/crates/engine/src/cmd/mailings/recipients.rs b/crates/engine/src/cmd/mailings/recipients.rs index 8150287b55..e9be3faf37 100644 --- a/crates/engine/src/cmd/mailings/recipients.rs +++ b/crates/engine/src/cmd/mailings/recipients.rs @@ -1,16 +1,18 @@ use super::filter::refresh_shown; use super::{CmdError, CmdResult, Session, Value, json, p}; -/// Parse CSV (RFC 4180-ish: quotes, doubled quotes, commas/semicolons/tabs). -/// The field separator: whichever of `,` `;` and tab the first non-blank line (outside quotes) uses most. +/// The field separator: whichever of `,` `;` and tab the first non-blank record (outside quotes) uses most. fn csv_separator(text: &str) -> char { - let first = text.lines().find(|l| !l.trim().is_empty()).unwrap_or(""); let mut quoted = false; let mut unquoted = String::new(); - for c in first.chars() { + // A quoted header may span physical lines; infer from the first complete record. + for c in text.split_inclusive(['\n', '\r']).skip_while(|line| line.trim().is_empty()).flat_map(str::chars) { if c == '"' { quoted = !quoted; } else if !quoted { + if c == '\n' || c == '\r' { + break; + } unquoted.push(c); } } @@ -26,6 +28,7 @@ fn push_csv_row(rows: &mut Vec>, row: &mut Vec) { } } +/// Parse CSV (RFC 4180-ish: quotes, doubled quotes, commas/semicolons/tabs). pub fn parse_csv(text: &str) -> Vec> { let sep = csv_separator(text); let mut rows = Vec::new(); @@ -112,13 +115,23 @@ fn table_from_rows(fields: Option<&Vec>, arr: &[Value]) -> Result<(Vec Vec> { + let prefix = text + .split_inclusive(['\n', '\r']) + .take_while(|line| headers && line.trim().is_empty()) + .fold(0_usize, |bytes, line| bytes.saturating_add(line.len())); + parse_csv(text.get(prefix..).unwrap_or_default()) +} + pub(super) fn recipients(s: &mut Session, v: &Value) -> CmdResult { let mut sheet = Value::Null; let (headers, rows) = if let Some(arr) = v.get("rows").and_then(Value::as_array) { table_from_rows(v.get("fields").and_then(Value::as_array), arr)? } else { let all = if let Some(c) = p::str(v, "csv") { - parse_csv(c) + recipient_csv(c, p::bool(v, "headers").unwrap_or(true)) } else { let (bytes, ext) = if let Some(d) = p::str(v, "data") { let b = crate::cmd::insert::base64_decode(d.trim()).ok_or_else(|| CmdError::Params("`data` isn't valid base64".into()))?; @@ -175,7 +188,7 @@ pub(super) fn recipients(s: &mut Session, v: &Value) -> CmdResult { sheet = json!(chosen); rows } else { - parse_csv(&decode(&bytes, v)?) + recipient_csv(&decode(&bytes, v)?, p::bool(v, "headers").unwrap_or(true)) } }; header_split(all, p::bool(v, "headers").unwrap_or(true))? diff --git a/crates/engine/src/cmd/mailings/tests.rs b/crates/engine/src/cmd/mailings/tests.rs index 9c6eef7c43..40034eff7a 100644 --- a/crates/engine/src/cmd/mailings/tests.rs +++ b/crates/engine/src/cmd/mailings/tests.rs @@ -46,6 +46,76 @@ fn finishing_a_merge_to_a_file_is_not_an_edit() { let _ = std::fs::remove_dir_all(&dir); } +#[test] +fn csv_delimiter_reads_complete_header_record() { + for sep in [',', ';', '\t'] { + for newline in ["\n", "\r\n", "\r"] { + // All candidate separators and doubled quotes occur on both sides of the newline. + // The real separators follow the closing quote, beyond the first physical line. + let header = format!("Name,;\t \"given\"{newline}family,;\t \"last\""); + let quoted = header.replace('"', "\"\""); + let text = format!("\"{quoted}\"{sep}City{sep}{sep}\r\nAda{sep}Paris{sep}{sep}\nBo{sep}Oslo{sep}{sep}"); + assert_eq!( + parse_csv(&text), + vec![vec![header.as_str(), "City", "", ""], vec!["Ada", "Paris", "", ""], vec!["Bo", "Oslo", "", ""]], + "separator {sep:?}, embedded newline {newline:?}" + ); + // A multiline field after an ordinary column must not end detection either. + let text = format!("ID{sep}\"{quoted}\"{sep}City\n1{sep}Ada{sep}Paris\n2{sep}Bo{sep}Oslo"); + assert_eq!(parse_csv(&text), vec![vec!["ID", header.as_str(), "City"], vec!["1", "Ada", "Paris"], vec!["2", "Bo", "Oslo"]]); + } + } +} + +#[test] +fn csv_delimiter_reads_multiline_headers_after_blank_lines() { + for sep in [',', ';', '\t'] { + for newline in ["\n", "\r\n", "\r"] { + let header = format!("Given{newline}Family"); + for prefix in ["\n\r\n", "\r", "\r\r"] { + let text = format!("{prefix}\"{header}\"{sep}City\rAda{sep}Paris"); + assert_eq!(parse_csv(&text), vec![vec![header.as_str(), "City"], vec!["Ada", "Paris"]]); + } + } + } +} + +#[test] +fn recipient_csv_blank_prefix_keeps_columns_without_dropping_literal_records() { + let mut s = Session::new(Document::new()); + for sep in [',', ';', '\t'] { + for newline in ["\n", "\r\n", "\r"] { + let header = format!("First{newline}Name"); + let csv = format!(" \t{newline}\"{header}\"{sep}Email{newline}Ada{sep}ada@example.com"); + let result = s.run("mailings.recipients", &json!({"csv": csv})).unwrap(); + assert_eq!(result, json!({"fields": [header, "Email"], "records": 1})); + assert_eq!(s.run("mailings.editRecipients", &json!({})).unwrap()["rows"], json!([["Ada", "ada@example.com"]])); + } + } + s.run("mailings.recipients", &json!({"csv": " \nName,Email\nAda,ada@example.com", "headers": false})).unwrap(); + assert_eq!(s.run("mailings.editRecipients", &json!({})).unwrap()["rows"], json!([[" ", ""], ["Name", "Email"], ["Ada", "ada@example.com"]])); + s.run("mailings.recipients", &json!({"csv": "\" \",Email\nAda,ada@example.com"})).unwrap(); + assert_eq!(s.run("mailings.editRecipients", &json!({})).unwrap()["rows"], json!([["Ada", "ada@example.com"]])); + let rows = s.merge.rows.clone(); + assert!(s.run("mailings.recipients", &json!({"csv": " \t\r\n \r"})).is_err()); + assert_eq!(s.merge.rows, rows, "an empty headered list must not replace existing recipients"); + assert_eq!(parse_csv(" \nName,Email\nAda,ada@example.com")[0], vec![" "], "the shared CSV reader keeps literal whitespace rows"); +} + +#[test] +fn csv_delimiter_keeps_record_boundaries_and_ties() { + // Later records cannot choose the header's delimiter. + assert_eq!(parse_csv("Name;City\rAda;Paris,,,,\rBo;Oslo"), vec![vec!["Name", "City"], vec!["Ada", "Paris,,,,"], vec!["Bo", "Oslo"]]); + // Existing ties prefer tab, then semicolon, then comma. + assert_eq!(parse_csv("A,B;C\n1,2;3"), vec![vec!["A,B", "C"], vec!["1,2", "3"]]); + assert_eq!(parse_csv("A;B\tC\n1;2\t3"), vec![vec!["A;B", "C"], vec!["1;2", "3"]]); + assert_eq!(parse_csv("Name\nAda,Paris;Oslo"), vec![vec!["Name"], vec!["Ada,Paris;Oslo"]]); + assert!(parse_csv("").is_empty()); + assert_eq!(parse_csv("\nName\nAda"), vec![vec!["Name"], vec!["Ada"]]); + // The reader remains permissive about an unterminated quoted cell. + assert_eq!(parse_csv("Name,City\nAda,\"Paris\nOslo"), vec![vec!["Name", "City"], vec!["Ada", "Paris\nOslo"]]); +} + #[test] fn csv_delimiter_ignores_quoted_text() { let r = parse_csv("\"Name, first, last\";City\nAnn;Paris\n"); diff --git a/crates/engine/src/cmd/objects/arrange.rs b/crates/engine/src/cmd/objects/arrange.rs index b20989042c..af1ad888b8 100644 --- a/crates/engine/src/cmd/objects/arrange.rs +++ b/crates/engine/src/cmd/objects/arrange.rs @@ -6,6 +6,9 @@ pub(super) const MAX_OFFSET: f32 = 4000.0; /// Resize and/or move the selected object (dragging its frame or handles). pub(super) fn bounds(s: &mut Session, v: &Value) -> CmdResult { + if let Some(r) = crate::cmd::canvas::member_bounds(s, v) { + return r; + } let (pos, obj) = selected(s).ok_or_else(|| CmdError::Disabled("no picture or shape selected".into()))?; let to = match (p::f32(v, "x"), p::f32(v, "y")) { (Some(x), Some(y)) => Some((x, y)), @@ -58,7 +61,7 @@ fn landing(s: &Session, pos: &Pos, to: &Pos) -> Result { /// Move the In Line with Text object at `pos` to the text position `to`, where it stays In Line /// with Text, keeping its character formatting: Word's drag of an inline picture, which lands -/// where its dashed insertion cursor was (#2233, #2234). Dropped on itself, nothing changes. +/// where its insertion cursor was (#2233, #2234). Dropped on itself, nothing changes. /// Returns the object's position afterwards. fn move_in_text(s: &mut Session, pos: Pos, to: &Pos) -> Result { let to = landing(s, &pos, to)?; @@ -161,8 +164,9 @@ fn move_object_once(s: &mut Session, pos: &Pos, page: usize, x: f32, y: f32, (w, Some(path) => (Pos { story, path, off: 0 }, false), None => (nearest_line(&layout, page, y, story).ok_or_else(|| CmdError::Params("no text on that page to anchor to".into()))?, true), }; - // Move its character to the anchor (unless it's already in that paragraph). - let at = if anchor.story == pos.story && anchor.path == pos.path && !on_page { + // Move its character to the anchor, unless it's already in that paragraph and drawn on this + // page (one anchored on a later page of it is drawn there, #4250). + let at = if anchor.story == pos.story && anchor.path == pos.path && !on_page && layout.object(pos, page).is_some_and(|o| o.page == page) { pos.clone() } else { let obj = s.doc.para_at(pos).and_then(|p| p.object_at(pos.off)).cloned().ok_or_else(|| CmdError::Failed("object vanished".into()))?; diff --git a/crates/engine/src/cmd/objects/picture.rs b/crates/engine/src/cmd/objects/picture.rs index 3b514a21e5..a254e0a762 100644 --- a/crates/engine/src/cmd/objects/picture.rs +++ b/crates/engine/src/cmd/objects/picture.rs @@ -76,9 +76,11 @@ pub fn cropped_original_size(s: &Session, o: &InlineObject) -> Option<(f32, f32) pub(super) fn size(s: &mut Session, v: &Value) -> CmdResult { let (_, o) = selected(s).ok_or_else(|| CmdError::Disabled("no object".into()))?; let lock = p::bool(v, "lockAspect").unwrap_or(true); - let (mut w, mut h) = obj_size(&o); + let (w0, h0) = obj_size(&o); + let (mut w, mut h) = (w0, h0); let scale = p::f32(v, "scale"); - if p::bool(v, "original").unwrap_or(false) { + let original = p::bool(v, "original").unwrap_or(false); + if original { let (ow, oh) = cropped_original_size(s, &o).ok_or_else(|| CmdError::Disabled("only a picture whose image can be read has an original size".into()))?; let k = scale.unwrap_or(100.0) / 100.0; @@ -113,7 +115,16 @@ pub(super) fn size(s: &mut Session, v: &Value) -> CmdResult { (None, None) => true, _ => lock, }; - let (w, h) = fit_size(w, h, keep); + // Sides set in proportion (from the original, or from a size whose sides both have a length) + // fit the limits together. Otherwise each side the call sets fits on its own, and a side it + // leaves as it was keeps its size, a straight line's 0 too. + let (w, h) = if keep && (original || (w0 > 0.0 && h0 > 0.0 && (w, h) != (w0, h0))) { + fit_size(w, h, true) + } else { + let (fw, fh) = fit_size(w, h, false); + let left = |new: f32, old: f32| !original && new == old; + (if left(w, w0) { w } else { fw }, if left(h, h0) { h } else { fh }) + }; // Any drawing: a group's members scale with its frame (its child space is kept); a chart or // SmartArt graphic lays itself out again at the new size. let theme = s.doc.settings.theme_colors.clone(); diff --git a/crates/engine/src/cmd/objects/select.rs b/crates/engine/src/cmd/objects/select.rs index ae0f464bb9..435bfcb8a2 100644 --- a/crates/engine/src/cmd/objects/select.rs +++ b/crates/engine/src/cmd/objects/select.rs @@ -12,6 +12,10 @@ pub(super) fn has_object(s: &Session) -> Option<&'static str> { } pub(super) fn has_floating(s: &Session) -> Option<&'static str> { + // A canvas member moves inside its canvas. + if crate::cmd::canvas::selected_member(s).is_some() { + return None; + } match selected(s) { Some((_, InlineObject::Graphic { .. })) => Some(FROZEN), Some((_, o)) if o.is_floating() => None, @@ -51,6 +55,15 @@ pub(super) fn has_movable(s: &Session) -> Option<&'static str> { } } +/// `has_movable` for what places an object in the text (wrapping, position, alignment, Move with +/// Text): not for a member picked inside a drawing canvas, which the canvas places. +pub(super) fn has_placeable(s: &Session) -> Option<&'static str> { + if crate::cmd::canvas::selected_member(s).is_some() { + return Some("a shape inside a drawing canvas is placed by the canvas"); + } + has_movable(s) +} + /// Word's own ink in a group is kept in the form a group member takes; it can't stand on its own /// yet, so such a group stays whole. pub(super) const INK_GROUP: &str = "a group holding Word ink can't be ungrouped yet"; @@ -99,7 +112,9 @@ pub fn object_selection(s: &Session) -> Option<(Pos, &InlineObject)> { o.is_drawing().then_some((a, o)) } -/// The first picture/shape in the selection, or just before a collapsed caret. +/// The first picture/shape in the selection, or just before a collapsed caret. With a canvas +/// member picked ([`crate::cmd::canvas::selected_member`]), that member, at its canvas's position: +/// the commands that change it go through [`edit_obj`], which changes the member. pub fn selected(s: &Session) -> Option<(Pos, InlineObject)> { selected_all(s, 1).into_iter().next() } @@ -107,6 +122,9 @@ pub fn selected(s: &Session) -> Option<(Pos, InlineObject)> { /// The pictures and shapes in the selection (at most `max`), or the one just before a collapsed /// caret. pub fn selected_all(s: &Session, max: usize) -> Vec<(Pos, InlineObject)> { + if let Some((pos, _, member)) = crate::cmd::canvas::selected_member(s) { + return vec![(pos, member.clone())]; + } let mut out = Vec::new(); let (a, b) = s.sel.ordered(); let max = if a == b { 1 } else { max }; @@ -175,8 +193,12 @@ pub(super) fn with_obj(s: &mut Session, f: impl Fn(&mut InlineObject)) -> CmdRes Ok(json!({"object": serde_json::to_value(o).unwrap_or(Value::Null)})) } -/// Change the object at `pos`; returns it afterwards. +/// Change the object at `pos`; returns it afterwards. With a member of the canvas at `pos` +/// picked, the member changes. pub(crate) fn edit_obj(s: &mut Session, pos: &Pos, f: impl Fn(&mut InlineObject)) -> Result { + if let Some(r) = crate::cmd::canvas::edit_member_object(s, pos, &f) { + return r; + } let para = s.doc.para_mut(pos.story, &pos.path)?; let o = para.object_at_mut(pos.off).ok_or_else(|| CmdError::Failed("object vanished".into()))?; f(o); diff --git a/crates/engine/src/cmd/objects/specs.rs b/crates/engine/src/cmd/objects/specs.rs index a4c9ef3084..11967ed4e5 100644 --- a/crates/engine/src/cmd/objects/specs.rs +++ b/crates/engine/src/cmd/objects/specs.rs @@ -2,8 +2,8 @@ use super::arrange::{MAX_OFFSET, bounds, copy_to, move_with_text, unalign}; use super::group::{add_object, group, ungroup}; use super::picture::{adjust, adjust_media, clear_lum, crop, original_media, recolor, relook, remove_background, reset, size}; use super::select::{ - all_objects, alt_text, can_group, edit_obj, has_floating, has_group, has_movable, has_object, has_picture, has_shape, has_shape_or_canvas, - has_sizable, obj_size, objects_list, selected, with_float, with_obj, + all_objects, alt_text, can_group, edit_obj, has_floating, has_group, has_movable, has_object, has_picture, has_placeable, has_shape, + has_shape_or_canvas, has_sizable, obj_size, objects_list, selected, with_float, with_obj, }; use super::shape::{NO_TEXT_BOX, can_link, has_text_box, link_text_box, set_text_body, shape_effects, text_box_target}; use super::{CmdError, CommandSpec, InlineObject, Pos, Rgb, Selection, Spin, TextVert, VAlign, Wrap, WrapText, json, p, sel_result}; @@ -13,7 +13,7 @@ pub fn specs() -> Vec { vec![ CommandSpec::new("picture.size", "Size", "Picture Format › Size", size) .params( - r#"{"width"?: pt, "height"?: pt, "lockAspect"?: bool, "scale"?: percent, "original"?: bool} (scale: of the current size, or with original of the picture's original size, its pixels at 96 ppi less what is cropped; original alone restores that size; one side alone keeps the proportions unless lockAspect is false; 1–4000 pt: when the proportions are kept, a side past 4000 pt shrinks the other with it)"#, + r#"{"width"?: pt, "height"?: pt, "lockAspect"?: bool, "scale"?: percent, "original"?: bool} (scale: of the current size, or with original of the picture's original size, its pixels at 96 ppi less what is cropped; original alone restores that size; one side alone keeps the proportions unless lockAspect is false; 1–4000 pt: when the proportions are kept (from the original, or with both sides having a length), a side past 4000 pt shrinks the other with it; a side the call leaves as it was keeps its size, a straight line's 0 too)"#, ) .when(has_sizable), CommandSpec::new("picture.crop", "Crop", "Picture Format › Size", crop) @@ -132,14 +132,26 @@ pub fn specs() -> Vec { } else { return Err(CmdError::Params("`path` or `data` required".into())); }; - wordcraft_geom::picture::probe(&bytes).map_err(|e| match e { - wordcraft_geom::picture::Refusal::Unsupported(why) => CmdError::Failed(why.into()), - _ => CmdError::Failed("not a supported image".into()), - })?; + let (px_w, px_h) = wordcraft_geom::picture::probe(&bytes) + .map(|p| (p.width, p.height)) + .map_err(|e| match e { + wordcraft_geom::picture::Refusal::Unsupported(why) => CmdError::Failed(why.into()), + _ => CmdError::Failed("not a supported image".into()), + })?; let key = s.doc.add_media(bytes.clone(), crate::cmd::insert::image_ext(&bytes)); with_obj(s, |o| { - if let InlineObject::Image { media, .. } = o { + if let InlineObject::Image { media, w, h, crop, .. } = o { *media = key.clone(); + // The replacement keeps the picture's width and takes its own aspect ratio, so a + // picture of another shape isn't stretched into the old frame. The old crop + // described the old picture, so it is dropped. + if px_w > 0 && px_h > 0 && w.is_finite() && *w > 0.0 { + let nh = *w * px_h as f32 / px_w as f32; + if nh.is_finite() && nh > 0.0 { + *h = nh; + } + } + *crop = [0.0; 4]; } }) }) @@ -193,17 +205,17 @@ pub fn specs() -> Vec { with_float(s, |f| f.wrap = wrap) }) .params(r#"{"wrap": "inline|square|tight|through|topAndBottom|behindText|inFrontOfText"}"#) - .when(has_movable), + .when(has_placeable), CommandSpec::new("arrange.wrapText", "Wrap Text Sides", "Layout › Arrange", |s, v| { let side = p::str(v, "side").unwrap_or("bothSides"); let side = WrapText::from_ooxml(side).ok_or_else(|| CmdError::Params(format!("unknown side {side:?}")))?; with_float(s, |f| f.wrap_text = side) }) .params(r#"{"side": "bothSides|left|right|largest"} (the sides of the selected Square, Tight or Through object text flows on; largest: the wider side, line by line)"#) - .when(has_movable), + .when(has_placeable), CommandSpec::new("arrange.moveWithText", "Move with Text", "Layout › Arrange › Wrap Text", |s, v| move_with_text(s, p::bool(v, "value").unwrap_or(true))) .params(r#"{"value": bool} (true: Move with Text, the floating object's vertical position follows its paragraph; false: Fix Position on Page. It stays where it is)"#) - .when(has_floating), + .when(|s| has_placeable(s).or_else(|| has_floating(s))), CommandSpec::new("arrange.position", "Position", "Layout › Arrange", |s, v| { let preset = p::str(v, "preset").unwrap_or("middleCenter").to_string(); let (x, y) = (p::f32(v, "x"), p::f32(v, "y")); @@ -224,9 +236,9 @@ pub fn specs() -> Vec { }) }) .params(r#"{"preset"?: "topLeft|topCenter|topRight|middleLeft|middleCenter|middleRight|bottomLeft|bottomCenter|bottomRight", "x"?: pt, "y"?: pt}"#) - .when(has_movable), - CommandSpec::new("arrange.bringForward", "Bring Forward", "Layout › Arrange", |s, _| with_float(s, |f| f.wrap = Wrap::InFrontOfText)).when(has_movable), - CommandSpec::new("arrange.sendBackward", "Send Backward", "Layout › Arrange", |s, _| with_float(s, |f| f.wrap = Wrap::BehindText)).when(has_movable), + .when(has_placeable), + CommandSpec::new("arrange.bringForward", "Bring Forward", "Layout › Arrange", |s, _| with_float(s, |f| f.wrap = Wrap::InFrontOfText)).when(has_placeable), + CommandSpec::new("arrange.sendBackward", "Send Backward", "Layout › Arrange", |s, _| with_float(s, |f| f.wrap = Wrap::BehindText)).when(has_placeable), CommandSpec::new("arrange.align", "Align", "Layout › Arrange", |s, v| { let h = p::str(v, "value").unwrap_or("center").to_string(); let tw = crate::cmd::page::sect(s).text_width(); @@ -245,17 +257,20 @@ pub fn specs() -> Vec { }) }) .params(r#"{"value": "left|center|right"}"#) - .when(has_movable), + .when(has_placeable), CommandSpec::new("arrange.selectionPane", "Selection Pane", "Layout › Arrange", objects_list).pure(), CommandSpec::new("arrange.bounds", "Move or Resize", "Layout › Arrange", bounds) .params(r#"{"width"?: pt, "height"?: pt, "x"?: pt, "y"?: pt, "page"?: n, "to"?: Pos} (x/y: the top-left on page `page` (0-based, default its page); moving an inline object by x/y floats it. to: an In Line with Text object moves to that text position and stays inline, as a drag does)"#) .when(has_movable), CommandSpec::new("arrange.copyTo", "Copy to", "Layout › Arrange", copy_to) .params(r#"{"x": pt, "y": pt, "page"?: n} | {"to": Pos} (Ctrl+drag: a copy of the selected object lands with its top-left there, or an In Line with Text one at the text position `to`, and is selected; the original and the clipboard stay as they were)"#) - .when(has_movable), + .when(has_placeable), CommandSpec::new("arrange.nudge", "Nudge", "Layout › Arrange", |s, v| { let d = |k| p::f32(v, k).unwrap_or(0.0).clamp(-MAX_OFFSET, MAX_OFFSET); let (dx, dy) = (d("dx"), d("dy")); + if let Some(r) = crate::cmd::canvas::member_nudge(s, dx, dy) { + return r; + } unalign(s)?; with_float(s, |f| { f.x = (f.x + dx).clamp(-MAX_OFFSET, MAX_OFFSET); diff --git a/crates/engine/src/cmd/objects/tests.rs b/crates/engine/src/cmd/objects/tests.rs index e2ccb1d8b1..13836d60c5 100644 --- a/crates/engine/src/cmd/objects/tests.rs +++ b/crates/engine/src/cmd/objects/tests.rs @@ -14,6 +14,106 @@ fn object_area(s: &mut Session) -> wordcraft_layout::hit::ObjectHit { s.layout().find_object(0, |o| o.text_box.is_none()).unwrap() } +/// #4250: a shape dragged into the middle of a page that only continues a long paragraph +/// stays there, as in Word: anchored at the line beside it, it's drawn on that page, not +/// thrown back to the page the paragraph starts on. One undo step puts it back. +#[test] +fn a_shape_dropped_on_a_page_a_paragraph_continues_onto_stays_there() { + let mut s = Session::new(wordcraft_doc::Document::new()); + s.run("text.insert", &json!({"text": "The quick brown fox jumps over the lazy dog. ".repeat(400)})).unwrap(); + s.run("caret.docStart", &json!({})).unwrap(); + s.run("insert.shape", &json!({"kind": "rectangle"})).unwrap(); + s.run("arrange.wrap", &json!({"wrap": "square"})).unwrap(); + s.run("arrange.bounds", &json!({"page": 0, "x": 100, "y": 250, "width": 100, "height": 50})).unwrap(); + assert!(s.layout().pages.len() > 2); + let before = (object_area(&mut s), s.undo_depth()); + s.run("arrange.bounds", &json!({"page": 1, "x": 100, "y": 275})).unwrap(); + let o = object_area(&mut s); + assert_eq!(o.page, 1, "on the page it was dropped on: {o:?}"); + assert!((o.rect.x - 100.0).abs() < 0.5 && (o.rect.y - 275.0).abs() < 0.5, "where it was dropped: {:?}", o.rect); + assert_eq!(s.undo_depth(), before.1 + 1); + s.run("edit.undo", &json!({})).unwrap(); + let back = object_area(&mut s); + assert_eq!((back.page, back.rect), (before.0.page, before.0.rect)); +} + +/// #4250: and dragged back onto the page the paragraph starts on, it stays there: its anchor +/// goes back to the paragraph's start rather than staying on the later page's line. +#[test] +fn a_shape_dragged_back_to_the_page_its_paragraph_starts_on_stays_there() { + let mut s = Session::new(wordcraft_doc::Document::new()); + s.run("text.insert", &json!({"text": "The quick brown fox jumps over the lazy dog. ".repeat(400)})).unwrap(); + s.run("caret.docStart", &json!({})).unwrap(); + s.run("insert.shape", &json!({"kind": "rectangle"})).unwrap(); + s.run("arrange.wrap", &json!({"wrap": "square"})).unwrap(); + s.run("arrange.bounds", &json!({"page": 1, "x": 100, "y": 275, "width": 100, "height": 50})).unwrap(); + let before = (object_area(&mut s), s.undo_depth()); + assert!(before.0.page == 1 && before.0.off > 0, "anchored on page 2's line: {:?}", before.0); + s.run("arrange.bounds", &json!({"page": 0, "x": 120, "y": 300})).unwrap(); + let o = object_area(&mut s); + assert_eq!((o.page, o.off), (0, 0), "on the page it was dropped on, anchored at the paragraph's start: {o:?}"); + assert!((o.rect.x - 120.0).abs() < 0.5 && (o.rect.y - 300.0).abs() < 0.5, "where it was dropped: {:?}", o.rect); + assert_eq!(s.undo_depth(), before.1 + 1); +} + +/// #4262: dropped beside the first line after a page break inside a paragraph, a shape stays +/// on the page it was dropped on, anchored in that line. +#[test] +fn a_shape_dropped_just_after_a_page_break_in_a_paragraph_stays_there() { + let mut s = Session::new(wordcraft_doc::Document::new()); + let text = "The quick brown fox jumps over the lazy dog. "; + s.run("text.insert", &json!({"text": text.repeat(20)})).unwrap(); + s.run("insert.pageBreak", &json!({})).unwrap(); + s.run("text.insert", &json!({"text": text.repeat(40)})).unwrap(); + assert_eq!(s.doc.body.len(), 1, "one paragraph"); + s.run("caret.docStart", &json!({})).unwrap(); + s.run("insert.shape", &json!({"kind": "rectangle"})).unwrap(); + s.run("arrange.wrap", &json!({"wrap": "square"})).unwrap(); + s.run("arrange.bounds", &json!({"page": 0, "x": 100, "y": 250, "width": 100, "height": 50})).unwrap(); + s.run("arrange.bounds", &json!({"page": 1, "x": 100, "y": 80})).unwrap(); + let o = object_area(&mut s); + let after_break = text.len() * 20 + wordcraft_doc::para::PAGE_BREAK.len_utf8(); + assert_eq!((o.page, o.off), (1, after_break), "on the page it was dropped on, anchored right after the break: {o:?}"); + assert!((o.rect.x - 100.0).abs() < 0.5 && (o.rect.y - 80.0).abs() < 0.5, "where it was dropped: {:?}", o.rect); +} + +/// Leaf #107: wrapping can leave the anchor alone to the left and put the text on the right +/// of the same physical row. A drop after the break must still stay on the destination page. +#[test] +fn a_picture_with_wrap_spacing_dropped_after_a_leading_break_stays_there() { + for gap in [0.0, 6.0, 12.0] { + let mut s = Session::new(wordcraft_doc::Document::from_text("Source\nDestination")); + s.run("caret.set", &json!({"pos": Pos::body(1, 0)})).unwrap(); + s.run("layout.break", &json!({"kind": "page"})).unwrap(); + s.run("caret.set", &json!({"pos": Pos::body(0, 0)})).unwrap(); + let data = super::super::insert::base64_encode(&png(20, 10)); + s.run("insert.picture", &json!({"data": data, "width": 120})).unwrap(); + s.run("arrange.wrap", &json!({"wrap": "square"})).unwrap(); + super::select::with_float(&mut s, |f| { + f.dist = gap; + f.dist_top = gap; + f.dist_bottom = gap; + }) + .unwrap(); + let before = serde_json::to_value(&s.doc.body).unwrap(); + let depth = s.undo_depth(); + s.run("arrange.bounds", &json!({"page": 1, "x": 100, "y": 110})).unwrap(); + let hit = object_area(&mut s); + assert_eq!(hit.page, 1, "gap={gap}: {hit:?}"); + assert!((hit.rect.x - 100.0).abs() < 0.5 && (hit.rect.y - 110.0).abs() < 0.5, "gap={gap}: {:?}", hit.rect); + assert_eq!(s.undo_depth(), depth + 1); + let after = serde_json::to_value(&s.doc.body).unwrap(); + s.run("edit.undo", &json!({})).unwrap(); + assert_eq!(serde_json::to_value(&s.doc.body).unwrap(), before); + s.run("edit.redo", &json!({})).unwrap(); + assert_eq!(serde_json::to_value(&s.doc.body).unwrap(), after); + let mut copy = Session::new(wordcraft_docx::read(&wordcraft_docx::write(&s.doc).unwrap()).unwrap()); + let saved = object_area(&mut copy); + assert_eq!(saved.page, 1, "DOCX gap={gap}: {saved:?}"); + assert!((saved.rect.x - hit.rect.x).abs() < 0.5 && (saved.rect.y - hit.rect.y).abs() < 0.5, "saved at the drop position"); + } +} + /// What a click and a handle or body drag do in the editor (#142, #82): hit-test the object, /// select it, then resize and move it with `arrange.bounds`. fn click_resize_and_drag(insert: &str, params: Value) { @@ -971,6 +1071,66 @@ fn picture_size_past_the_limit_keeps_the_proportions() { } } +/// A side the call leaves as it was keeps its size, even outside 1–4000 pt: a straight line's 0 +/// stays 0 when only its length is typed. Sides set in proportion (the original, or a size whose +/// sides both have a length) fit the limits together; any other side the call sets, on its own. +#[test] +fn picture_size_keeps_the_side_it_leaves() { + let mut s = Session::new(wordcraft_doc::Document::new()); + s.run("insert.shape", &json!({"kind": "straightConnector", "width": 100.0, "height": 0.0})).unwrap(); + let size = |s: &Session| obj_size(&selected(s).unwrap().1); + // A side a scale sets on its own: 0 has no length to scale. + let scaled = |x: f32, k: f32| if x == 0.0 { 0.0 } else { (x * k).clamp(1.0, 4000.0) }; + // Sizes with both sides follow each other: they fit 1–4000 pt by the one factor nearest 1, worked out by hand + // for the calls `width` locked, `height` locked, `scale` 50 and `scale` 200 with a width (in that order). + let states = [ + ((100.0, 0.0), None), + ((0.0, 100.0), None), + ((5000.0, 0.0), None), + ((0.0, 5000.0), None), + ((100.0, 0.5), Some([(200.0, 1.0), (4000.0, 20.0), (200.0, 1.0), (200.0, 1.0)])), + ((5000.0, 50.0), Some([(200.0, 2.0), (4000.0, 40.0), (2500.0, 25.0), (200.0, 2.0)])), + ((100.0, 50.0), Some([(200.0, 100.0), (400.0, 200.0), (50.0, 25.0), (200.0, 100.0)])), + ]; + for ((w0, h0), together) in states { + let follow = |i: usize, alone: (f32, f32)| together.map_or(alone, |t: [(f32, f32); 4]| t[i]); + let cases = [ + (json!({"width": 200.0, "lockAspect": false}), (200.0, h0)), + (json!({"height": 200.0, "lockAspect": false}), (w0, 200.0)), + (json!({"width": 200.0}), follow(0, (200.0, h0))), + (json!({"height": 200.0}), follow(1, (w0, 200.0))), + (json!({"scale": 50}), follow(2, (scaled(w0, 0.5), scaled(h0, 0.5)))), + (json!({"scale": 200, "width": 200.0}), follow(3, (200.0, scaled(h0, 2.0)))), + (json!({"scale": 100}), (w0, h0)), + (json!({}), (w0, h0)), + // The Size dialog: both sides sent, the height as it was. + (json!({"width": 200.0, "height": h0, "lockAspect": false}), (200.0, h0)), + (json!({"width": 200.0, "height": 300.0}), (200.0, 300.0)), + ]; + for (v, (ew, eh)) in cases { + with_obj(&mut s, |o| o.set_size(w0, h0)).unwrap(); + s.run("picture.size", &v).unwrap(); + let (w, h) = size(&s); + assert!((w - ew).abs() < 1e-3 && (h - eh).abs() < 1e-3, "{w0}×{h0} {v}: {w}×{h}, not {ew}×{eh}"); + } + } + // Both sides of a picture's original size fit together, whatever size it had, even when the + // height comes out as it was: 4050×1012.5 pt is 4000×1000, not 4000×1012.5. + let data = super::super::insert::base64_encode(&png(400, 100)); + s.run("insert.picture", &json!({"data": data})).unwrap(); + for (w0, h0) in [(3000.0, 1012.5), (4050.0, 0.0), (100.0, 100.0)] { + with_obj(&mut s, |o| o.set_size(w0, h0)).unwrap(); + s.run("picture.size", &json!({"original": true, "scale": 1350})).unwrap(); + let (w, h) = size(&s); + assert!((w - 4000.0).abs() < 0.01 && (h - 1000.0).abs() < 0.01, "{w0}×{h0}: {w}×{h}"); + } + // A height the original sets is set, though it is the one the picture had: 300×75 pt at + // 5400 % is 16200×4050, so with a width of 200 the height fits to 4000. + with_obj(&mut s, |o| o.set_size(100.0, 4050.0)).unwrap(); + s.run("picture.size", &json!({"original": true, "scale": 5400, "width": 200.0, "lockAspect": false})).unwrap(); + assert_eq!(size(&s), (200.0, 4000.0)); +} + /// A crop handle drop: `picture.crop` with the new size is one edit, one undo step. #[test] fn picture_crop_with_size_is_one_undo_step() { diff --git a/crates/engine/src/cmd/para.rs b/crates/engine/src/cmd/para.rs index 2941925c5f..06068ee65d 100644 --- a/crates/engine/src/cmd/para.rs +++ b/crates/engine/src/cmd/para.rs @@ -16,6 +16,7 @@ mod lists; mod outline; mod styles; mod tabs_borders; +mod tools; use format::{align, asian_typography, cur, direction, line_spacing, ls, set_align, set_props, tog}; pub use format::{cur as selection_para, fmt, indent, outdent}; @@ -26,6 +27,10 @@ use outline::{move_paras, outline_step, sort}; use styles::{apply_style, apply_styles, create_style, delete_style, modify_style, set_visibility, style_id, update_to_match}; pub use tabs_borders::border_sides; use tabs_borders::{borders, tabs}; +use tools::{ + blank_between, convert_breaks, indent_to_spaces, remove_blank, remove_extra_spaces, remove_line_breaks, separate_paragraphs, spaces_to_indent, + trim_leading, +}; pub fn specs() -> Vec { vec![ @@ -183,9 +188,17 @@ pub fn specs() -> Vec { .key(if cfg!(target_os = "macos") { "Ctrl+Shift+Right" } else { "Alt+Shift+Right" }), CommandSpec::new("para.shading", "Shading", "Home › Paragraph", |s, v| { let c = p::str(v, "color").and_then(Rgb::parse); + // Text selected within one paragraph is shaded itself, as in Word, so No Color takes + // off a shading a file gave the text (#4685); a caret or paragraphs shade paragraphs. + // `paragraph` (Borders and Shading › Apply to: Paragraph) shades the paragraphs anyway. + if p::bool(v, "paragraph") != Some(true) && shades_text(s) { + return super::format::apply(s, &|x| x.shading = c); + } fmt(s, &|p| p.shading = c) }) - .params(r#"{"color": "RRGGBB" | null}"#), + .params( + r#"{"color": "RRGGBB" | null, "paragraph"?: bool} (text selected inside one paragraph is shaded itself, as in Word; `paragraph: true` shades the paragraphs instead, as Borders and Shading › Apply to: Paragraph)"#, + ), CommandSpec::new("para.borders", "Borders", "Home › Paragraph", borders).params( r#"{"kind": "bottom|top|left|right|none|all|outside|inside|horizontalLine", "width"?: pt, "color"?: "RRGGBB", "style"?: "single|double|dotted|dashed|thick|triple|dotDash|wave", "sides"?: Sides} where Sides = {"top"|"left"|"bottom"|"right"|"between": {"style"?, "width"?: pt, "color"?: "RRGGBB"} | null}: exactly these sides (others off), instead of `kind`"#, ), @@ -262,5 +275,43 @@ pub fn specs() -> Vec { } sel_result(s) }), + // Advanced paragraph tools (#5043, as in WPS Office): conversions and cleanup over the + // selection, or the whole document with `scope: "all"`. One undo step per command. + CommandSpec::new("para.convertBreaks", "Convert Line Breaks to Paragraphs", "Home › Paragraph › Paragraph Tools", convert_breaks) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.blankBetween", "Insert Blank Paragraphs Between", "Home › Paragraph › Paragraph Tools", blank_between) + .params(r#"{"scope"?: "selection|all", "after"?: bool (also after the last paragraph)}"#), + CommandSpec::new("para.separateParagraphs", "Separate Paragraphs", "Home › Paragraph › Paragraph Tools", separate_paragraphs) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.removeBlank", "Delete Blank Paragraphs", "Home › Paragraph › Paragraph Tools", remove_blank) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.trimLeadingSpaces", "Remove Leading Spaces", "Home › Paragraph › Paragraph Tools", trim_leading) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.removeExtraSpaces", "Remove Extra Spaces", "Home › Paragraph › Paragraph Tools", remove_extra_spaces) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.spacesToIndent", "Convert Spaces to Indentation", "Home › Paragraph › Paragraph Tools", spaces_to_indent) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.indentToSpaces", "Convert Indents to Spaces", "Home › Paragraph › Paragraph Tools", indent_to_spaces) + .params(r#"{"scope"?: "selection|all"}"#), + CommandSpec::new("para.removeLineBreaks", "Remove Line Breaks", "Home › Paragraph › Paragraph Tools", remove_line_breaks) + .params(r#"{"scope"?: "selection|all"}"#), ] } + +/// Whether Shading applies to the selected text rather than to paragraphs: a selection inside one +/// paragraph, short of its mark. A selection reaching past the mark ends at the next paragraph's +/// start, so all of a paragraph's text is still text, except in the last paragraph of its story, +/// cell or box, where it stands for the paragraph with its mark. A selected picture, shape or text +/// box is its object character, not text (a field's object character is). +fn shades_text(s: &Session) -> bool { + let (a, b) = s.sel.ordered(); + if a == b || a.story != b.story || a.path != b.path { + return false; + } + let Some(p) = s.doc.para_at(&a) else { return false }; + if super::objects::object_selection(s).is_some() { + return false; + } + let last = s.doc.next_para(a.story, &a.path).is_none_or(|q| q.parent() != a.path.parent()); + !(last && a.off == 0 && b.off >= p.len()) +} diff --git a/crates/engine/src/cmd/para/tools.rs b/crates/engine/src/cmd/para/tools.rs new file mode 100644 index 0000000000..b900b6c205 --- /dev/null +++ b/crates/engine/src/cmd/para/tools.rs @@ -0,0 +1,332 @@ +//! Advanced paragraph tools (#5043): converting and cleaning up paragraphs, like WPS Office's +//! paragraph tools. Each command works on the paragraphs the selection covers, or on the whole +//! document with `scope: "all"`, and runs as one undo step. Deletions respect Track Changes. + +use serde_json::{Value, json}; + +use wordcraft_doc::para::LINE_BREAK; +use wordcraft_doc::{Path, Pos, RevisionKind, StoryRef}; + +use super::{CmdError, CmdResult, Session, p, sel_result}; +use crate::cmd::{has_next_para, new_revision, split_para, track_delete}; + +/// The maximum number of paragraphs one call may touch: the work, and the blank paragraphs a +/// command can add, stay bounded whatever the document says. +const MAX_TOOL_PARAS: usize = 10_000; + +/// Leading spaces one paragraph may gain from an indent, whatever the file's indent says. +const MAX_INDENT_SPACES: usize = 1_000; + +/// The scope of a tools command: `{"scope": "selection|all"}`, `selection` by default. +/// `all` selects the whole document first, so the command, its undo step and the result all +/// follow the full selection like any other command. +fn scope_all(s: &mut Session, v: &Value) -> Result<(), CmdError> { + match p::str(v, "scope").unwrap_or("selection") { + "selection" => Ok(()), + "all" => { + s.run("select.all", &json!({"whole": true}))?; + Ok(()) + } + x => Err(CmdError::Params(format!("unknown scope `{x}` (selection|all)"))), + } +} + +/// Refuse a paragraph rewrite that would touch content locked against editing. Commands operate +/// on whole paragraphs, so checking each full paragraph also covers controls outside the exact +/// selection offsets in a selected paragraph. +fn check_target_edits(s: &mut Session, story: StoryRef, paths: &[Path]) -> Result<(), CmdError> { + let ranges: Vec<(Pos, Pos)> = paths + .iter() + .filter_map(|path| { + let len = s.doc.para(story, path)?.len(); + Some((Pos { story, path: path.clone(), off: 0 }, Pos { story, path: path.clone(), off: len })) + }) + .collect(); + for (a, b) in ranges { + crate::cmd::controls::check_edit(s, &a, &b)?; + } + Ok(()) +} + +/// The paragraphs a command runs over, in document order, and the story they are in. +fn targets(s: &Session) -> Result<(StoryRef, Vec), CmdError> { + let (a, b) = s.sel.ordered(); + let paths = s.doc.para_paths_in(&a, &b); + if paths.is_empty() { + return Err(CmdError::Failed("no paragraphs selected".into())); + } + if paths.len() > MAX_TOOL_PARAS { + return Err(CmdError::Params(format!("select at most {MAX_TOOL_PARAS} paragraphs to process them in one command"))); + } + Ok((a.story, paths)) +} + +/// Deletes `a..b` inside one paragraph, recording a tracked deletion when Track Changes is on. +/// Offsets past the paragraph's end are clamped, so the document is never half-edited. +fn delete_in_para(s: &mut Session, story: StoryRef, path: &Path, a: usize, b: usize) -> Result<(), CmdError> { + let len = s.doc.para(story, path).ok_or_else(|| CmdError::Failed("no paragraph here".into()))?.len(); + let (a, b) = (a.min(b).min(len), b.min(len)); + if a >= b { + return Ok(()); + } + let at = |off: usize| Pos { story, path: path.clone(), off }; + crate::cmd::controls::check_delete(s, &at(a), &at(b))?; + if s.doc.settings.track_changes { + track_delete(s, &at(a), &at(b))?; + } else { + s.doc.delete_range(&at(a), &at(b))?; + } + Ok(()) +} + +/// The deletions to make in one paragraph, as byte ranges, merged where they touch and applied +/// last first so the earlier offsets stay valid. +fn apply_deletions(s: &mut Session, story: StoryRef, path: &Path, mut ranges: Vec<(usize, usize)>) -> Result<(), CmdError> { + ranges.sort(); + let mut merged: Vec<(usize, usize)> = Vec::new(); + for (a, b) in ranges { + match merged.last_mut() { + Some(last) if a <= last.1 => last.1 = last.1.max(b), + _ => merged.push((a, b)), + } + } + for (a, b) in merged.into_iter().rev() { + delete_in_para(s, story, path, a, b)?; + } + Ok(()) +} + +/// The characters of the paragraph that are not tracked-deleted, as (byte offset, char). Under +/// Track Changes deleted text stays in `para.text`, so every scan that decides what to delete or +/// count goes through this. +fn live_chars(para: &wordcraft_doc::Paragraph) -> impl Iterator + '_ { + para.run_ranges().filter(|(_, c)| c.del.is_none()).flat_map(|(r, _)| para.text[r.clone()].char_indices().map(move |(i, c)| (r.start + i, c))) +} + +/// The byte ranges of every manual line break in the paragraph that is not already deleted. +fn break_ranges(para: &wordcraft_doc::Paragraph) -> Vec<(usize, usize)> { + live_chars(para).filter(|(_, c)| *c == LINE_BREAK).map(|(i, _)| (i, i + 1)).collect() +} + +/// `para.convertBreaks`: every manual line break (Shift+Enter) in the selection becomes a +/// paragraph break. Paragraphs are handled last first: a split only shifts the paths after it. +pub(super) fn convert_breaks(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + let breaks: usize = paths.iter().filter_map(|path| s.doc.para(story, path)).map(|p| break_ranges(p).len()).sum(); + if breaks > MAX_TOOL_PARAS { + return Err(CmdError::Params(format!("convert at most {MAX_TOOL_PARAS} line breaks at once"))); + } + for path in paths.iter().rev() { + let Some(para) = s.doc.para(story, path) else { continue }; + for (i, _) in break_ranges(para).into_iter().rev() { + let new = split_para(s, &Pos { story, path: path.clone(), off: i })?; + // The break stays at the head of the new paragraph; the paragraph break replaces it. + delete_in_para(s, story, &new.path, 0, 1)?; + } + } + sel_result(s) +} + +/// `para.blankBetween`: insert one empty paragraph between the selected paragraphs; `after` +/// adds one blank paragraph after the last selected paragraph too. +pub(super) fn blank_between(s: &mut Session, v: &Value) -> CmdResult { + let after = p::bool(v, "after").unwrap_or(false); + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + let count = paths.len().saturating_sub(1) + usize::from(after); + if count > MAX_TOOL_PARAS { + return Err(CmdError::Params(format!("insert at most {MAX_TOOL_PARAS} blank paragraphs at once"))); + } + // Last first, each split at a paragraph's end: a split only shifts the paths after it. + if after { + let last = paths.last().ok_or_else(|| CmdError::Failed("no paragraph selected".into()))?; + let len = s.doc.para(story, last).map(|p| p.len()).unwrap_or(0); + split_para(s, &Pos { story, path: last.clone(), off: len })?; + } + for path in paths[..paths.len().saturating_sub(1)].iter().rev() { + let len = s.doc.para(story, path).map(|p| p.len()).unwrap_or(0); + split_para(s, &Pos { story, path: path.clone(), off: len })?; + } + sel_result(s) +} + +/// `para.separateParagraphs`: every selected paragraph becomes a paragraph of its own with a +/// blank line between them. +pub(super) fn separate_paragraphs(s: &mut Session, v: &Value) -> CmdResult { + blank_between(s, &json!({"scope": p::str(v, "scope").unwrap_or("selection"), "after": true})) +} + +/// `para.removeBlank`: delete every empty paragraph in the selection. +pub(super) fn remove_blank(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + // Last first: removing a block leaves the paths before it as they are. + for path in paths.iter().rev() { + let Some(para) = s.doc.para(story, path) else { continue }; + // A paragraph must be empty, carry no section break and have a paragraph after it in + // its container: a story's, part's or cell's last mark can't be removed, and a paragraph + // before a table can't be joined across it. + if !para.is_empty() || para.section.is_some() || !has_next_para(s, story, path) { + continue; + } + let next = path.with_last(path.last() + 1); + let (a, b) = (Pos { story, path: path.clone(), off: 0 }, Pos { story, path: next, off: 0 }); + crate::cmd::controls::check_delete(s, &a, &b)?; + if s.doc.settings.track_changes { + track_delete(s, &a, &b)?; + } else { + s.doc.remove_block(story, path)?; + } + } + sel_result(s) +} + +/// The number of leading spaces in the paragraph's text, deleted ones included (the range +/// `trimLeadingSpaces` deletes). +fn leading_spaces(para: &wordcraft_doc::Paragraph) -> usize { + para.text.chars().take_while(|c| *c == ' ').count() +} + +/// The byte offsets of the paragraph's leading spaces that are not tracked-deleted. +fn live_leading_spaces(para: &wordcraft_doc::Paragraph) -> Vec { + live_chars(para).take_while(|(_, c)| *c == ' ').map(|(i, _)| i).collect() +} + +/// `para.trimLeadingSpaces`: delete the spaces at the start of each selected paragraph. +pub(super) fn trim_leading(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + for path in paths { + let Some(para) = s.doc.para(story, &path) else { continue }; + let n = leading_spaces(para); + apply_deletions(s, story, &path, vec![(0, n)])?; + } + sel_result(s) +} + +/// `para.removeExtraSpaces`: every run of spaces outside the paragraph's start becomes one +/// space. Tabs and page breaks are document structure, not redundant spaces, and stay in place. +/// Leading spaces stay (that is `para.trimLeadingSpaces`). +pub(super) fn remove_extra_spaces(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + for path in paths { + let Some(para) = s.doc.para(story, &path) else { continue }; + // Runs of live spaces (deleted characters are skipped, as if absent). A run at the + // paragraph's start stays; a run inside the text collapses to its last space, and one + // that reaches the paragraph's end to its first. + let mut runs = Vec::new(); + let mut run: Vec = Vec::new(); + let mut leading = true; + for (i, c) in live_chars(para) { + if c == ' ' { + run.push(i); + continue; + } + if !leading && run.len() > 1 { + runs.extend(run[..run.len() - 1].iter().map(|&a| (a, a + 1))); + } + run.clear(); + leading = false; + } + if !leading && run.len() > 1 { + runs.extend(run[1..].iter().map(|&a| (a, a + 1))); + } + apply_deletions(s, story, &path, runs)?; + } + sel_result(s) +} + +/// `para.spacesToIndent`: each paragraph's leading spaces become a first-line indent of 72 pt +/// per space (Word's point-based indent from typing spaces), and are deleted. +pub(super) fn spaces_to_indent(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + let tracker = crate::cmd::fmt_revisions::ParaPropTracker::new(s); + for path in paths { + let Some(para) = s.doc.para(story, &path) else { continue }; + let live = live_leading_spaces(para); + let spaces = live.len(); + if spaces == 0 { + continue; + } + let current = para.props.indent_first.unwrap_or(0.0).clamp(-1584.0, 1584.0); + let represented = spaces.min(((1584.0 - current).max(0.0) / 72.0).floor() as usize); + if represented == 0 { + continue; + } + let before = para.props.clone(); + let mut after = before.clone(); + after.indent_first = Some(current + represented as f32 * 72.0); + after.cancel_char_indents(false, false, true); + after.fmt_change = tracker.changed(s, &before, &after); + { + let p = s.doc.para_mut(story, &path)?; + p.props = after; + p.touch(); + } + // A space is one byte; deleted spaces in between stay as they are. + apply_deletions(s, story, &path, live[..represented].iter().map(|&a| (a, a + 1)).collect())?; + } + sel_result(s) +} + +/// `para.indentToSpaces`: each paragraph's first-line and left indents become leading spaces +/// (72 pt per space, as typing spaces indents), and the indents go to zero. +pub(super) fn indent_to_spaces(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + let tracker = crate::cmd::fmt_revisions::ParaPropTracker::new(s); + for path in paths { + let Some(para) = s.doc.para(story, &path) else { continue }; + let pts = para.props.indent_left.unwrap_or(0.0) + para.props.indent_first.unwrap_or(0.0); + let n = (pts / 72.0).round().clamp(0.0, MAX_INDENT_SPACES as f32) as usize; + if n == 0 && para.props.indent_left.unwrap_or(0.0) == 0.0 && para.props.indent_first.unwrap_or(0.0) == 0.0 { + continue; + } + let mut props = s.doc.para(story, &path).map(|p| p.props_at(0)).cloned().unwrap_or_default(); + // The revision first: `new_revision` edits `s.doc`, so it runs before the paragraph borrow. + // The spaces are marked as a tracked insertion through `props.ins`, as typing is. + props.fmt_change = None; + props.del = None; + if s.doc.settings.track_changes && n > 0 { + props.ins = Some(new_revision(s, RevisionKind::Insert)); + } else { + props.ins = None; + } + let before = s.doc.para(story, &path).map(|p| p.props.clone()).unwrap_or_default(); + let mut after = before.clone(); + after.indent_first = Some(0.0); + after.indent_left = Some(0.0); + after.cancel_char_indents(true, false, true); + after.fmt_change = tracker.changed(s, &before, &after); + let p = s.doc.para_mut(story, &path)?; + if n > 0 { + p.insert_text(0, &" ".repeat(n), &props)?; + } + p.props = after; + p.touch(); + } + sel_result(s) +} + +/// `para.removeLineBreaks`: delete every manual line break in the selection, so the lines +/// join into one paragraph. +pub(super) fn remove_line_breaks(s: &mut Session, v: &Value) -> CmdResult { + scope_all(s, v)?; + let (story, paths) = targets(s)?; + check_target_edits(s, story, &paths)?; + for path in paths { + let Some(para) = s.doc.para(story, &path) else { continue }; + apply_deletions(s, story, &path, break_ranges(para))?; + } + sel_result(s) +} diff --git a/crates/engine/src/cmd/protect.rs b/crates/engine/src/cmd/protect.rs index 326989b1e5..8ddf62cd8d 100644 --- a/crates/engine/src/cmd/protect.rs +++ b/crates/engine/src/cmd/protect.rs @@ -652,19 +652,33 @@ pub(crate) fn typing_pos(s: &Session, p: &Pos) -> Pos { if para.objects.is_empty() { return p.clone(); } - let editable: Vec = s.doc.perm_ranges().iter().filter(|r| editable_by(r, &s.author)).map(|r| r.id).collect(); - let is_start = |off: usize| matches!(para.object_at(off), Some(InlineObject::PermStart { id, .. }) if editable.contains(id)); - let is_end = |off: usize| matches!(para.object_at(off), Some(InlineObject::PermEnd { id }) if editable.contains(id)); + let Some(before) = para.text.get(..p.off) else { return p.clone() }; + let Some(after) = para.text.get(p.off..) else { return p.clone() }; + if !before.ends_with(OBJ) && !after.starts_with(OBJ) { + return p.clone(); + } + let editable: HashSet = s.doc.perm_ranges().iter().filter(|r| editable_by(r, &s.author)).map(|r| r.id).collect(); + // Count the preceding objects once. Each adjacent marker can then use the matching + // object directly, forwards for starts and backwards for ends, even with short storage. + let index = before.chars().filter(|c| *c == OBJ).count(); let w = OBJ.len_utf8(); let mut off = p.off; - if is_start(off) { - while is_start(off) { - off += w; + for (ch, object) in after.chars().zip(para.objects.iter().skip(index)) { + if ch != OBJ || !matches!(object, InlineObject::PermStart { id, .. } if editable.contains(id)) { + break; } + off += w; + } + if off > p.off { return Pos { off, ..p.clone() }; } - while off >= w && is_end(off - w) { - off -= w; + if let Some(objects) = para.objects.get(..index) { + for (ch, object) in before.chars().rev().zip(objects.iter().rev()) { + if ch != OBJ || !matches!(object, InlineObject::PermEnd { id } if editable.contains(id)) { + break; + } + off -= w; + } } Pos { off, ..p.clone() } } diff --git a/crates/engine/src/cmd/protect/tests.rs b/crates/engine/src/cmd/protect/tests.rs index 9ff007e9a8..2e6fd90360 100644 --- a/crates/engine/src/cmd/protect/tests.rs +++ b/crates/engine/src/cmd/protect/tests.rs @@ -922,3 +922,159 @@ fn legacy_password_is_verified_and_unknown_forms_refused() { assert!(e.to_string().contains(super::UNSUPPORTED_PASSWORD)); assert!(super::enforced(&s.doc)); } + +/// Consecutive permission boundaries must not recount the paragraph prefix or search all +/// editable IDs once per marker. Both ends are used by typing and Enter. +#[test] +fn typing_boundary_many_permission_markers_is_not_quadratic() { + use wordcraft_doc::para::Run; + let cpu = || std::fs::read_to_string("/proc/thread-self/schedstat").ok()?.split_whitespace().next()?.parse::().ok(); + let measure = |n: usize, at_end: bool| { + let mut p = wordcraft_doc::Paragraph::new(); + p.text.push_str("λ prefix "); + let start = p.text.len(); + for i in 0..n { + p.text.push(OBJ); + p.objects.push(InlineObject::PermStart { id: i as u32, group: "everyone".into(), editor: String::new() }); + } + p.text.push_str("body"); + let end = p.text.len(); + for i in 0..n { + p.text.push(OBJ); + p.objects.push(InlineObject::PermEnd { id: i as u32 }); + } + let after = p.text.len(); + p.runs.push(Run { len: p.text.len(), props: CharProps::default() }); + let mut doc = Document::new(); + doc.body = vec![Block::Para(p).into()]; + doc.settings.protection = Some("readOnly".into()); + let s = Session::new(doc); + let at = Pos::body(0, if at_end { after } else { start }); + let expected = Pos::body(0, if at_end { end } else { start + n * OBJ.len_utf8() }); + let before = cpu(); + for _ in 0..3 { + assert_eq!(typing_pos(std::hint::black_box(&s), &at), expected); + } + match (before, cpu()) { + (Some(a), Some(b)) if b > a => Some((b - a) as f64 * 1e-9), + _ => None, + } + }; + for at_end in [false, true] { + let (small, large) = (measure(2_000, at_end), measure(16_000, at_end)); + let (Some(a), Some(b)) = (small, large) else { + eprintln!("Typing boundary: CPU counter unavailable; semantic checks passed, timing assertion skipped"); + continue; + }; + eprintln!("Typing boundary end={at_end}: small={a:.9}s large={b:.9}s ratio={:.2}", b / a); + assert!(b < a * 24.0 + 0.005, "8x permission markers must not take quadratic boundary work: {a:.9}s -> {b:.9}s"); + } +} + +#[test] +fn typing_boundary_keeps_author_precedence_and_malformed_pairing() { + use wordcraft_doc::para::Run; + // Frozen boundary semantics from before the one-pass lookup. This oracle deliberately + // uses object_at independently of the new traversal, including malformed storage. + let original = |s: &Session, p: &Pos| { + if !matches!(s.doc.settings.protection.as_deref(), Some("readOnly" | "comments")) { + return p.clone(); + } + let Some(para) = s.doc.para_at(p) else { return p.clone() }; + if para.objects.is_empty() { + return p.clone(); + } + let editable: Vec = s.doc.perm_ranges().iter().filter(|r| editable_by(r, &s.author)).map(|r| r.id).collect(); + let start = |off| matches!(para.object_at(off), Some(InlineObject::PermStart { id, .. }) if editable.contains(id)); + let end = |off| matches!(para.object_at(off), Some(InlineObject::PermEnd { id }) if editable.contains(id)); + let mut off = p.off; + if start(off) { + while start(off) { + off += OBJ.len_utf8(); + } + } else { + while off >= OBJ.len_utf8() && end(off - OBJ.len_utf8()) { + off -= OBJ.len_utf8(); + } + } + Pos { off, ..p.clone() } + }; + let objects = [ + InlineObject::PermStart { id: 1, group: "everyone".into(), editor: String::new() }, + InlineObject::PermStart { id: 2, group: String::new(), editor: " ALICE ".into() }, + InlineObject::PermStart { id: 3, group: String::new(), editor: "alice".into() }, + InlineObject::PermEnd { id: 1 }, + InlineObject::PermEnd { id: 3 }, + InlineObject::BookmarkStart { name: "Original".into() }, + InlineObject::PermEnd { id: 7 }, + InlineObject::PermStart { id: 7, group: String::new(), editor: "Bob".into() }, + ]; + for text in + [format!("λ{}x{}終", OBJ.to_string().repeat(4), OBJ.to_string().repeat(4)), OBJ.to_string().repeat(9), format!("λ{OBJ}{OBJ}x{OBJ}{OBJ}{OBJ}")] + { + for count in 0..=objects.len() { + for mode in [None, Some("readOnly"), Some("comments"), Some("forms"), Some("trackedChanges")] { + let mut p = wordcraft_doc::Paragraph::new(); + p.text = text.clone(); + p.objects = objects[..count].to_vec(); + p.runs.push(Run { len: p.text.len(), props: CharProps::default() }); + let mut doc = Document::new(); + doc.body = vec![Block::Para(p).into()]; + doc.settings.protection = mode.map(str::to_string); + let mut s = Session::new(doc); + s.author = " Alice ".into(); + for off in 0..=text.len() + 1 { + let at = Pos::body(0, off); + assert_eq!(typing_pos(&s, &at), original(&s, &at), "mode={mode:?} count={count} off={off} text={text:?}"); + } + } + } + } + let mut s = Session::new(Document::from_text("outside")); + s.doc.settings.protection = Some("readOnly".into()); + assert_eq!(typing_pos(&s, &Pos::body(99, 0)), Pos::body(99, 0)); +} + +#[test] +fn typing_boundary_keeps_duplicate_ids_across_stories_and_start_precedence() { + use wordcraft_doc::para::Run; + let paragraph = |text: String, objects| wordcraft_doc::Paragraph { + runs: vec![Run { len: text.len(), props: CharProps::default() }], + text, + objects, + ..Default::default() + }; + let mut doc = Document::new(); + doc.body = vec![ + Block::Para(paragraph( + format!("{OBJ}x{OBJ}"), + vec![InlineObject::PermStart { id: 1, group: String::new(), editor: "Bob".into() }, InlineObject::PermEnd { id: 1 }], + )) + .into(), + ]; + doc.add_part( + wordcraft_doc::PartKind::Header, + vec![ + Block::Para(paragraph( + format!("{OBJ}y{OBJ}"), + vec![InlineObject::PermStart { id: 1, group: "everyone".into(), editor: String::new() }, InlineObject::PermEnd { id: 1 }], + )) + .into(), + ], + ); + doc.settings.protection = Some("comments".into()); + let mut s = Session::new(doc); + s.author = "Alice".into(); + // Preserve the existing document-wide ID membership, even for a duplicate in another story. + assert_eq!(typing_pos(&s, &Pos::body(0, 0)), Pos::body(0, OBJ.len_utf8())); + assert_eq!(typing_pos(&s, &Pos::body(0, 7)), Pos::body(0, 4)); + s.doc.body = vec![ + Block::Para(paragraph( + format!("{OBJ}{OBJ}x"), + vec![InlineObject::PermEnd { id: 1 }, InlineObject::PermStart { id: 1, group: "everyone".into(), editor: String::new() }], + )) + .into(), + ]; + // A start to the right wins over an end to the left. + assert_eq!(typing_pos(&s, &Pos::body(0, 3)), Pos::body(0, 6)); +} diff --git a/crates/engine/src/cmd/references.rs b/crates/engine/src/cmd/references.rs index 0cfeda45a4..5fb6195e8e 100644 --- a/crates/engine/src/cmd/references.rs +++ b/crates/engine/src/cmd/references.rs @@ -103,8 +103,9 @@ pub fn specs() -> Vec { /// A field instruction split into its name, positional arguments and switches (ECMA-376 /// §17.16.1): `TOC \o "1-3" \h` → name `TOC`, switches `o` = `1-3` and `h`. Quoted text stays -/// whole (without the quotes; `\"` and `\\` inside it are a quote and a backslash). A switch -/// takes the word after it as its argument unless that word is another switch. +/// whole (without the quotes; a backslash inside it makes the next character literal, as in Word: +/// `\"` is a quote, `\\` a backslash, `\*` an asterisk). A switch takes the word after it as its +/// argument unless that word is another switch. #[derive(Clone, Debug, Default, PartialEq)] pub struct FieldCode { pub name: String, @@ -126,7 +127,7 @@ pub fn field_words(instr: &str) -> Vec<(String, bool)> { while let Some(c) = chars.next() { match c { '"' => break, - '\\' if matches!(chars.peek(), Some('"' | '\\')) => w.extend(chars.next()), + '\\' if chars.peek().is_some() => w.extend(chars.next()), c => w.push(c), } } diff --git a/crates/engine/src/cmd/references/fields.rs b/crates/engine/src/cmd/references/fields.rs index 238cf553b3..86d81ce49e 100644 --- a/crates/engine/src/cmd/references/fields.rs +++ b/crates/engine/src/cmd/references/fields.rs @@ -102,7 +102,9 @@ struct SeqCount { /// The fresh results of the unlocked SEQ fields in the body and in the text boxes anchored there /// ([`update_seq`]). A text box's fields count where the box is anchored, before the fields after -/// the anchor in its paragraph and after those before it (measured in Word for Windows). +/// the anchor in its paragraph and after those before it (measured in Word for Windows). A locked +/// SEQ keeps its result but still counts, `\r` included; a SEQ in a tracked deletion keeps its +/// result and doesn't count, with markup shown or not (measured in Word for Windows, exporting). fn seq_results(doc: &wordcraft_doc::Document) -> Vec { let mut count = SeqCount::default(); for path in doc.para_paths(StoryRef::Body) { @@ -120,7 +122,11 @@ fn seq_results(doc: &wordcraft_doc::Document) -> Vec { /// Number the SEQ fields of paragraph `p` (at `path` in `story`) and of the text boxes anchored in it. fn count_seq(doc: &wordcraft_doc::Document, story: StoryRef, path: &Path, p: &Paragraph, count: &mut SeqCount) { - for (k, o) in p.objects.iter().enumerate() { + // The runs come in order and the objects by offset, so one cursor walks the deleted ranges. + let mut deleted = p.deleted_ranges().into_iter().peekable(); + for (k, (off, o)) in p.objects_with_offsets().enumerate() { + while deleted.next_if(|r| r.end <= off).is_some() {} + let in_deletion = deleted.peek().is_some_and(|r| r.contains(&off)); for id in o.text_boxes() { if count.boxes.insert(id) { let part = StoryRef::Part(id); @@ -131,9 +137,9 @@ fn count_seq(doc: &wordcraft_doc::Document, story: StoryRef, path: &Path, p: &Pa } } } - let InlineObject::Field { instr, locked: false, .. } = o else { continue }; + let InlineObject::Field { instr, locked, .. } = o else { continue }; let f = FieldCode::parse(instr); - if f.name != "SEQ" { + if f.name != "SEQ" || in_deletion { continue; } let Some(id) = f.args.first() else { continue }; @@ -149,6 +155,9 @@ fn count_seq(doc: &wordcraft_doc::Document, story: StoryRef, path: &Path, p: &Pa } else if !f.has('c') { n.0 = n.0.saturating_add(1); } + if *locked { + continue; + } let fmt = wordcraft_layout::fields::format_switch(instr).unwrap_or_default(); let r = if f.has('h') { String::new() } else { wordcraft_layout::fields::field_number(instr, fmt, n.0) }; count.out.push((story, path.clone(), k, r)); diff --git a/crates/engine/src/cmd/references/tests.rs b/crates/engine/src/cmd/references/tests.rs index d28b0a0c8e..b2838cd9f9 100644 --- a/crates/engine/src/cmd/references/tests.rs +++ b/crates/engine/src/cmd/references/tests.rs @@ -1,3 +1,4 @@ +use super::properties::{new_property_value, text_result}; use super::*; fn fields(s: &Session) -> Vec<(String, String)> { @@ -446,7 +447,7 @@ fn update_fields_apply_the_capitalization_switches() { "seventh", "Seventh", "BUDGET", - "new \\* Upper" + "new * Upper" ] ); } @@ -548,6 +549,85 @@ fn caption_for_an_object_in_a_cell_stays_in_the_cell() { assert_eq!(texts.get(1).map(String::as_str), Some("Figure 1"), "{texts:?}"); } +/// A fresh scratch directory for the file-name tests below. +#[cfg(not(target_arch = "wasm32"))] +fn filename_scratch_dir(tag: &str) -> std::path::PathBuf { + let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let dir = std::env::temp_dir().join(format!("wordcraft-filename-field-{tag}-{}-{nanos}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +/// #3796: after Save As to a new name, Update Field with the caret in a header shows the new +/// name for FILENAME and the new full path for FILENAME \p. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn filename_fields_in_a_header_follow_save_as() { + let dir = filename_scratch_dir("save-as"); + let (mut s, hdr) = header_fields_session(wordcraft_doc::Document::from_text("body"), &["FILENAME", r"FILENAME \p"]); + let first = dir.join("report_v1.docx"); + let second = dir.join("report_v2.docx"); + s.run("file.save", &json!({"path": first.to_string_lossy()})).unwrap(); + s.run("references.updateFields", &json!({})).unwrap(); + let got = story_fields(&s, StoryRef::Part(hdr)); + assert_eq!(got[0].1, "report_v1.docx"); + assert_eq!(got[1].1, first.to_string_lossy()); + + s.run("file.saveAs", &json!({"path": second.to_string_lossy()})).unwrap(); + s.run("references.updateFields", &json!({})).unwrap(); + let got = story_fields(&s, StoryRef::Part(hdr)); + assert_eq!(got[0].1, "report_v2.docx"); + assert_eq!(got[1].1, second.to_string_lossy()); + let _ = std::fs::remove_dir_all(&dir); +} + +/// #3796: a Word-style `FILENAME \* MERGEFORMAT` field whose cached text names the old file +/// shows the new name after Save As and Update Field. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn filename_mergeformat_field_replaces_its_cached_name_after_save_as() { + let dir = filename_scratch_dir("mergeformat"); + let (mut s, hdr) = header_fields_session(wordcraft_doc::Document::from_text("body"), &[r"FILENAME \* MERGEFORMAT"]); + let first = dir.join("report_v1.docx"); + s.run("file.save", &json!({"path": first.to_string_lossy()})).unwrap(); + // The cached text a file written earlier would carry. + let path = s.doc.para_paths(StoryRef::Part(hdr)).remove(0); + if let Some(InlineObject::Field { result, .. }) = s.doc.para_mut(StoryRef::Part(hdr), &path).unwrap().objects.first_mut() { + *result = "report_v1.docx".into(); + } + s.run("file.saveAs", &json!({"path": dir.join("report_v2.docx").to_string_lossy()})).unwrap(); + s.run("references.updateFields", &json!({})).unwrap(); + assert_eq!(story_fields(&s, StoryRef::Part(hdr))[0].1, "report_v2.docx"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// #3796: renaming the document (File > Info) changes what FILENAME shows on the next Update +/// Field; the extension stays. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn filename_field_in_a_header_follows_rename() { + let dir = filename_scratch_dir("rename"); + let (mut s, hdr) = header_fields_session(wordcraft_doc::Document::from_text("body"), &["FILENAME"]); + s.run("file.save", &json!({"path": dir.join("report_v1.docx").to_string_lossy()})).unwrap(); + s.run("file.rename", &json!({"name": "report_v3"})).unwrap(); + s.run("references.updateFields", &json!({})).unwrap(); + assert_eq!(story_fields(&s, StoryRef::Part(hdr))[0].1, "report_v3.docx"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// A backslash in a quoted field argument makes the next character literal, so `\*` there is an +/// asterisk and not the `\* Upper` switch (Word shows `x * Upper` for `TITLE "x \* Upper"`). +#[test] +fn quoted_field_argument_backslash_escapes() { + for (instr, shown) in [(r#"TITLE "x \* Upper""#, "x * Upper"), (r#"TITLE "x \\* Upper""#, r"x \* Upper"), (r#"TITLE "a \"b\" c""#, r#"a "b" c"#)] + { + let value = new_property_value(instr).unwrap(); + assert_eq!(value, shown, "{instr}"); + assert_eq!(text_result(instr, value), shown, "{instr}"); + } + assert_eq!(text_result(r#"TITLE "ab" \* Upper"#, new_property_value(r#"TITLE "ab" \* Upper"#).unwrap()), "AB"); +} + /// Output (PDF, print, page images) shows the body's unlocked SEQ, STYLEREF and REF fields with /// their fresh results, as Word fills them in when it prints or exports even with "Update fields /// before printing" off; NUMWORDS and a locked field keep their stored results there, and the @@ -713,6 +793,45 @@ fn output_shows_references_to_bookmarks_outside_the_body() { assert_eq!(results, expected); } +/// Printed or exported, a locked SEQ keeps its result but still counts, its `\r` too, and a SEQ +/// in a tracked deletion keeps its result and doesn't count (measured in Word for Windows, +/// exporting with markup shown and hidden: Del 1, 9, 2; Lock 1, 7, 3; R 7, 6). +#[test] +fn output_counts_locked_seq_fields_and_not_deleted_ones() { + let mut s = Session::new(wordcraft_doc::Document::from_text("")); + let fields_in = ["SEQ Del", "SEQ Del", "SEQ Del", "SEQ Lock", "SEQ Lock", "SEQ Lock", "SEQ R \\r 5", "SEQ R"]; + for instr in fields_in { + s.run("insert.field", &json!({"instr": instr, "result": "9"})).unwrap(); + s.run("text.insert", &json!({"text": " "})).unwrap(); + } + let top = wordcraft_doc::Path::top(0); + let para = s.doc.para_mut(StoryRef::Body, &top).unwrap(); + for (k, o) in para.objects.iter_mut().enumerate() { + if let InlineObject::Field { result, locked, .. } = o { + *locked = matches!(k, 4 | 6); + *result = if *locked { "7" } else { "9" }.into(); + } + } + para.touch(); + // The second Del field is a tracked deletion. + let off = s.doc.para(StoryRef::Body, &top).unwrap().object_offsets()[1]; + let at = |off| Pos { story: StoryRef::Body, path: top.clone(), off }; + s.doc.format_range(&at(off), &at(off + '\u{FFFC}'.len_utf8()), &|c| c.del = Some(1)).unwrap(); + let out = output_fields(&s.doc).expect("the stale results change"); + let shown: Vec = out + .para(StoryRef::Body, &top) + .unwrap() + .objects + .iter() + .filter_map(|o| match o { + InlineObject::Field { result, .. } => Some(result.clone()), + _ => None, + }) + .collect(); + assert_eq!(shown, ["1", "9", "2", "1", "7", "3", "7", "6"]); + assert!(fields(&s).iter().all(|f| f.1 == "9" || f.1 == "7"), "the document keeps its results"); +} + /// Printed or exported, a REF to a bookmark the document doesn't have shows Word's error /// (measured in Word for Windows: "Error! Reference source not found."); the editor and the /// document keep the stored result, and a REF showing "above" or "below" (`\p`) keeps it too. diff --git a/crates/engine/src/cmd/review/changes.rs b/crates/engine/src/cmd/review/changes.rs index b41cc7f01b..13990ba89c 100644 --- a/crates/engine/src/cmd/review/changes.rs +++ b/crates/engine/src/cmd/review/changes.rs @@ -54,11 +54,20 @@ fn resolve_para( // An inserted paragraph mark that is rejected, or a deleted one that is accepted, goes away: // the next paragraph joins this one (the split is undone). A paragraph left empty goes away // whole, so the next one keeps its own style and numbering, as in Word. The mark at the end - // of a story or cell can't go, so it just stops being a revision. - if ((del.is_some() && accept) || (ins.is_some() && !accept)) - && (crate::cmd::remove_empty_para(s, story, path)? || crate::cmd::join_next_para(s, story, path)?) - { - return Ok(()); + // of a story or cell can't go, so it just stops being a revision. An empty paragraph whose + // mark carries a section break goes whole too: its section joins the following one, as when + // the break is deleted (#5667). Joining instead gave the next paragraph its properties. + if (del.is_some() && accept) || (ins.is_some() && !accept) { + let sectioned = story == StoryRef::Body + && path.0.len() == 1 + && s.doc.para(story, path).is_some_and(|p| p.is_empty() && p.section.is_some()) + && crate::cmd::has_next_para(s, story, path); + if sectioned { + s.doc.remove_section_break(path.last()); + } + if crate::cmd::remove_empty_para(s, story, path)? || crate::cmd::join_next_para(s, story, path)? { + return Ok(()); + } } let para = s.doc.para_mut(story, path)?; para.mark.ins = None; diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs index 6734e050c4..925e1f6cab 100644 --- a/crates/engine/src/cmd/table.rs +++ b/crates/engine/src/cmd/table.rs @@ -195,7 +195,7 @@ pub fn specs() -> Vec { }) .params(r#"{"text"?: string, "title"?: string} (empty clears)"#)), t(CommandSpec::new("table.sort", "Sort", "Table Layout › Data", sort_table) - .params(r#"{"column"?: n, "descending"?: bool, "header"?: bool}"#)), + .params(r#"{"column"?: non-negative JSON integer (0-based, default 0; must name a column in the table), "descending"?: bool, "header"?: bool}"#)), t(CommandSpec::new("table.toText", "Convert to Text", "Table Layout › Data", to_text) .params(r#"{"separator"?: "tab|comma|paragraph|other", "char"?: string (with other)} (the selected rows, or the whole table)"#)), t(CommandSpec::new("table.formula", "Formula", "Table Layout › Data", formula).params(r#"{"formula"?: "=SUM(ABOVE)"}"#)), diff --git a/crates/engine/src/cmd/table/convert.rs b/crates/engine/src/cmd/table/convert.rs index f8cb418863..e92ce29d9e 100644 --- a/crates/engine/src/cmd/table/convert.rs +++ b/crates/engine/src/cmd/table/convert.rs @@ -8,11 +8,18 @@ use super::{ /// Sort the table's rows by a column. Numbers are read in each cell's language (`1,5` is one and /// a half in Polish) and sort first, numerically; other text sorts in the column's language. pub(super) fn sort_table(s: &mut Session, v: &Value) -> CmdResult { - let col = p::u64(v, "column").unwrap_or(0) as usize; + let column = match v.get("column") { + None => 0, + Some(value) => value.as_u64().ok_or_else(|| CmdError::Params("`column` must be a non-negative JSON integer".into()))?, + }; let desc = p::bool(v, "descending").unwrap_or(false); let header = p::bool(v, "header"); let (tp, _, _) = cell(s)?; let t = s.doc.table(s.sel.focus.story, &tp).ok_or_else(|| CmdError::Failed("no table".into()))?; + let col = usize::try_from(column) + .ok() + .filter(|&col| t.rows.iter().any(|row| col < row.cells.len())) + .ok_or_else(|| CmdError::Params("`column` is outside the table".into()))?; let skip = usize::from(header.unwrap_or(t.rows.first().is_some_and(|r| r.props.header))).min(t.rows.len()); let column = || t.rows.iter().skip(skip).map(|r| r.cells.get(col)); let lang = column().flatten().find_map(|c| cell_lang(&s.doc, c)).or_else(|| crate::cmd::default_lang(s)); diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs index c554cc885f..a62b0b6f25 100644 --- a/crates/engine/src/cmd/text.rs +++ b/crates/engine/src/cmd/text.rs @@ -14,7 +14,7 @@ mod enter_tab; mod unicode; use autoformat::insert; -use delete::{backspace, delete, delete_word_back, delete_word_fwd}; +use delete::{backspace, delete, delete_sentence_back, delete_word_back, delete_word_fwd}; pub use enter_tab::MAX_TYPED_PARAGRAPHS; use enter_tab::{back_tab, new_paragraph, tab}; pub use unicode::{alt_code_char, oem_code_page, system_code_pages, system_oem_code_page, windows_code_pages}; @@ -36,6 +36,7 @@ pub fn specs() -> Vec { CommandSpec::new("text.backspace", "Backspace", "Editing", backspace).key("Backspace"), CommandSpec::new("text.delete", "Delete", "Editing", delete).key("Delete"), CommandSpec::new("text.deleteWordBack", "Delete Previous Word", "Editing", delete_word_back).key("Mod+Backspace / Alt+Backspace"), + CommandSpec::new("text.deleteSentenceBack", "Delete Previous Sentence", "Editing", delete_sentence_back).key("Mod+Shift+Backspace"), CommandSpec::new("text.deleteWordForward", "Delete Next Word", "Editing", delete_word_fwd).key("Mod+Delete / Alt+Delete"), CommandSpec::new("text.nbsp", "Nonbreaking Space", "Insert › Symbols", |s, _| ins_char(s, NBSP)).key("Mod+Shift+Space"), CommandSpec::new("text.nbHyphen", "Nonbreaking Hyphen", "Insert › Symbols", |s, _| ins_char(s, NB_HYPHEN)).key("Mod+Shift+-"), diff --git a/crates/engine/src/cmd/text/delete.rs b/crates/engine/src/cmd/text/delete.rs index 5c692e523f..bb78661566 100644 --- a/crates/engine/src/cmd/text/delete.rs +++ b/crates/engine/src/cmd/text/delete.rs @@ -237,3 +237,20 @@ pub(super) fn delete_word_fwd(s: &mut Session, v: &Value) -> CmdResult { crate::cmd::controls::restore_placeholder(s)?; sel_result(s) } + +/// Delete to the current sentence start, or the preceding sentence when exactly at its boundary. +/// Sentence boundaries match Select Sentence. Never remove a paragraph or cell boundary. +pub(super) fn delete_sentence_back(s: &mut Session, v: &Value) -> CmdResult { + if !s.sel.is_collapsed() { + return backspace(s, v); + } + let mut f = s.sel.focus.clone(); + let Some(para) = s.doc.para_at(&f) else { return sel_result(s) }; + f.off = para.clamp(f.off); + let Some((previous, _)) = para.text.get(..f.off).and_then(|text| text.char_indices().next_back()) else { return sel_result(s) }; + let start = para.sentence_at(previous).0; + s.sel = Selection { anchor: Pos { off: start, ..f.clone() }, focus: f }; + delete_selection(s)?; + crate::cmd::controls::restore_placeholder(s)?; + sel_result(s) +} diff --git a/crates/engine/src/cmd/tools/actions.rs b/crates/engine/src/cmd/tools/actions.rs index e917aa7048..28bc721c13 100644 --- a/crates/engine/src/cmd/tools/actions.rs +++ b/crates/engine/src/cmd/tools/actions.rs @@ -341,3 +341,42 @@ pub(super) fn customize_ribbon(s: &mut Session, v: &Value) -> CmdResult { } Ok(Value::Object(out)) } + +/// The rows of an `insert.spreadsheet` call: `values` (a rectangular array of cells) or `csv`. +pub(super) fn spreadsheet_rows(v: &Value) -> Result>, CmdError> { + // The CSV fallback is for an absent `values`; an explicit null is a malformed argument. + let Some(values) = v.get("values") else { + return Ok(crate::cmd::mailings::parse_csv(p::str(v, "csv").unwrap_or("A,B,C\n1,2,3\n4,5,6"))); + }; + let list = values.as_array().ok_or_else(|| CmdError::Params("`values` must be an array of rows".into()))?; + if list.is_empty() || list.len() > 1000 { + return Err(CmdError::Params("`values` needs 1 to 1000 rows".into())); + } + let mut rows: Vec> = Vec::with_capacity(list.len()); + for (r, row) in list.iter().enumerate() { + let cells = row.as_array().ok_or_else(|| CmdError::Params(format!("`values` row {} is not an array", r + 1)))?; + if cells.is_empty() || cells.len() > 63 { + return Err(CmdError::Params(format!("`values` row {} needs 1 to 63 cells", r + 1))); + } + if rows.first().is_some_and(|f| f.len() != cells.len()) { + return Err(CmdError::Params(format!( + "`values` row {} has {} cells, row 1 has {}", + r + 1, + cells.len(), + rows.first().map_or(0, Vec::len) + ))); + } + let mut out = Vec::with_capacity(cells.len()); + for cell in cells { + out.push(match cell { + Value::String(x) => x.clone(), + Value::Number(x) => x.to_string(), + Value::Bool(x) => x.to_string(), + Value::Null => String::new(), + _ => return Err(CmdError::Params(format!("`values` row {} holds a cell that is not text, a number or a boolean", r + 1))), + }); + } + rows.push(out); + } + Ok(rows) +} diff --git a/crates/engine/src/cmd/tools/specs.rs b/crates/engine/src/cmd/tools/specs.rs index 636faf15b2..fc4daab3e4 100644 --- a/crates/engine/src/cmd/tools/specs.rs +++ b/crates/engine/src/cmd/tools/specs.rs @@ -1,5 +1,6 @@ use super::actions::{ customize_keyboard, customize_ribbon, hf_nav, language, macros, preferred_language, quick_parts, search_commands, select_similar, + spreadsheet_rows, }; use super::autocorrect::autocorrect_options; use super::compare::{COMPARE_PARAMS, compare}; @@ -121,17 +122,11 @@ pub fn specs() -> Vec { CommandSpec::new("file.recover", "Recover Unsaved Documents", "File › Info", |s, v| s.recover(v)) .params(r#"{"restore"?: recoveryId, "discard"?: recoveryId, "refresh"?: true}; desktop only; returns {enabled,busy,documents,error}; restore loads asynchronously without a file path"#) .pure(), - CommandSpec::new("file.newFromTemplate", "New from Template", "File › New", |s, v| { - let path = p::req_str(v, "path")?; - let mut doc = crate::io::open_path(std::path::Path::new(path)).map_err(CmdError::Failed)?; - // As in Word, the new document is attached to the template it came from (#2852). - doc.settings.attached_template = Some(path.to_string()); - s.add_document(doc); - s.dirty = true; - sel_result(s) - }) - .params(r#"{"path": string} → a new, untitled document from the template, attached to it"#) - .pure(), + CommandSpec::new("file.newFromTemplate", "New from Template", "File › New", crate::cmd::file::new_from_template) + .params( + r#"{"path": string, "data"?: base64 (the template's bytes; `path` then only names it), "password"?: string (for a password-protected template), "replaceBlank"?: bool (an untouched blank document gives way to it, as to a file file.open opens)} → a new, untitled document from the template, attached to it"#, + ) + .pure(), CommandSpec::new("file.saveTemplate", "Save as Template", "File › Save As", |s, v| { let path = p::req_str(v, "path")?; // A .dotm keeps its name (#2852); anything else becomes a .dotx. @@ -219,10 +214,10 @@ pub fn specs() -> Vec { }) .params(r#"{"path": string}"#), CommandSpec::new("insert.spreadsheet", "Spreadsheet Table", "Insert › Tables", |s, v| { - let csv = p::str(v, "csv").unwrap_or("A,B,C\n1,2,3\n4,5,6"); - let rows = crate::cmd::mailings::parse_csv(csv); + let rows = spreadsheet_rows(v)?; let cols = rows.iter().map(Vec::len).max().unwrap_or(1).clamp(1, 63); - s.run("insert.table", &json!({"rows": rows.len().max(1), "cols": cols, "style": "GridTable1Light"}))?; + let style = p::str(v, "style").unwrap_or("GridTable1Light"); + s.run("insert.table", &json!({"rows": rows.len().max(1), "cols": cols, "style": style}))?; let Some((tp, _, _)) = s.sel.focus.path.cell() else { return sel_result(s) }; let t = s.doc.table_mut(s.sel.focus.story, &tp)?; for (r, row) in rows.iter().enumerate() { @@ -232,9 +227,14 @@ pub fn specs() -> Vec { } } } + if p::bool(v, "header").unwrap_or(false) + && let Some(first) = t.rows.first_mut() + { + first.props.header = true; + } sel_result(s) }) - .params(r#"{"csv"?: string}"#), + .params(r#"{"csv"?: string, "values"?: [[cell, …], …] (rows of equal length, at most 1000 rows × 63 columns; wins over csv), "header"?: bool (first row repeats on each page), "style"?: table style name (default GridTable1Light)}"#), CommandSpec::new("layout.linkToPrevious", "Link to Previous", "Header & Footer › Navigation", |s, v| { let header = p::bool(v, "footer").map(|f| !f).unwrap_or(true); let block = s.sel.focus.path.0.first().copied().unwrap_or(0) as usize; diff --git a/crates/engine/src/cmd/tools/tests.rs b/crates/engine/src/cmd/tools/tests.rs index 4683817e60..3d2de2d844 100644 --- a/crates/engine/src/cmd/tools/tests.rs +++ b/crates/engine/src/cmd/tools/tests.rs @@ -255,6 +255,59 @@ fn proofing_language_options() { assert!(s.run("review.language", &json!({"lang": "