Compare commits

...
Author SHA1 Message Date
Martin BallandClaude Opus 5.5 9fd9a12957 Merge main into cutover/shared-codepage; craft-codepage at craft-libs main 93dac78c
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 20:56:27 +00:00
Martin BallandClaude Opus 5.5 1531007d5e #778: plain-text detection uses craft-mime detect (#6950)
The local is_text/decode/legacy copies in formats::txt are replaced by a re-export of
craft_mime::detect. The old code stays as a test oracle, with differential and random-buffer
tests showing identical output. chardetng stays only as a dev-dependency of that oracle.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:47:52 -05:00
30584c81ad #778: pin craft-libs main with the shared cutover prerequisites (#6954)
* #778: take craft-libs integration/wordcraft-778 at beeea924

Every craft-libs git dependency (workspace, ui-egui, wordcraft, wordcraft-web,
krilla patch) moves from 453b0da782 to beeea92451 (craft-libs main 0649a07aa
plus open PRs #2061-#2064). Cargo.lock: only craft-libs sources move, plus
windows-sys edge changes inside craft-libs crates; no crates.io versions change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* #778: docx stacking-order test follows the shared reader's one-more z

craft-libs 5f453efa9 (craft-docx: a relativeHeight of 0 stays the lowest
stacking order) reads Float::z as one more than the file's relativeHeight and
writes stored orders one less, so a file's 0 is no longer "none". The test
expected the old values; relative order and the round trip are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* #778: take craft-libs integration/wordcraft-778 at 24a15211

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* deps: pin craft-libs to the merged shared cutover prerequisites

* #778: pin craft-libs main at 93dac78c (with #2061-#2064)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Dylan M. Taylor <dylan@dylanmtaylor.com>
2026-11-17 15:45:46 -05:00
Martin BallandClaude Opus 5.5 4bb5407a51 #778: Cargo.lock: wordcraft-doc depends on craft-codepage after merging main
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 20:40:15 +00:00
Martin Ball 55f1a7857f Merge remote-tracking branch 'origin/main' into HEAD
; Conflicts:
;	Cargo.lock
2026-11-17 20:40:06 +00:00
Aleksander HelgakerandClaude Fable 5.2 8331f2e68d Fonts: tell where a missing font can be downloaded (Get the Missing Fonts) (#6826)
* Fonts: tell where a missing font can be downloaded (Get the Missing Fonts)

A document opened from a file that is missing fonts their publisher gives
away (Microsoft's Aptos family, Cascadia Code and Mono) brings up a box
naming each font, the font drawn instead, the publisher's download page and
how to install the download. Once per document; never for a new document or
one a script opened. "Don't show this again" turns it off (File > Options >
General turns it back on); the status bar's missing-fonts menu opens it by
hand (ui.dialog fontDownload).

font.missing reports the same page as "download", so agents get it without
the box. WordCraft links to the page only: nothing is downloaded or bundled.

The desktop and web apps switch the box on at start-up
(tell_about_font_downloads); tests and ui_shot don't, so what they show
doesn't depend on the fonts installed where they run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBVQLv63guWCuWUM3SctPP

* Get the Missing Fonts: the box with nothing to download is one row tall

Its Close button's far-side layout took the rest of the window's height.
The button row is now a row of its own, shared by both forms of the box.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBVQLv63guWCuWUM3SctPP

* Floating windows: a flat title bar and room around the content

Dialogs, the Read Aloud player and the built-in file browser had a grey
band behind a centred title and 6 pt margins. The title bar is now the
window's own colour with no band or line under it, and the title and the
content have 18 pt of room. The close box follows the new title bar height.

egui fills the front window's title bar with the colour of an open menu
button, so show_window makes that colour the window's while the title bar
is drawn and puts it back before the content is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBVQLv63guWCuWUM3SctPP

* Get the Missing Fonts: a headline, a card to each download, numbered steps

The box was a plain table and a paragraph of steps. It now opens with the
warning mark and how many fonts are missing; lists each download once
(Aptos and Aptos Display come in one) with the fonts drawn instead, the
publisher and a Download button that opens the publisher's page; and gives
the two steps that add the download, with Add Font… beside its step.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBVQLv63guWCuWUM3SctPP

* Fonts: the download pages are a JSON list (font_downloads.json)

The list of fonts their publisher gives away, and each publisher's page,
was a table in the code. It is now crates/fonts/src/font_downloads.json,
built into the app: adding a font is a new entry there. Entries without a
family, a publisher or an https page are left out, and a list that isn't
JSON has no downloads rather than failing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBVQLv63guWCuWUM3SctPP

* Get the Missing Fonts: licence notice, and fonts offered with limits

The box says that fonts come with licence terms and that following the
publisher's terms is up to the user. A font whose publisher limits who may
use it is marked "restricted" in font_downloads.json, says so on its card
and in font.missing ("restricted"), and has a card of its own.

The list gains Selawik (Microsoft's open-source font) and, marked
restricted, Segoe UI Variable, Segoe Fluent Icons and Segoe MDL2 Assets
from Microsoft's Windows design downloads page. Plain Segoe UI is not
offered for download and is not matched.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBVQLv63guWCuWUM3SctPP

* Get the Missing Fonts: the downloads scroll in a short window

With many downloads in a short window the box ran past the bottom of the
screen, leaving the steps, Add Font…, Don't show this again and Close out
of reach. The cards now scroll in the room the window has left, between
96 and 320 pt, and the rest of the box stays in view.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* Get the Missing Fonts: all above the Close row scrolls in a short window

Only the download cards scrolled, within fixed limits, so in a window
shorter than about 480 pt the opt-out and Close could still fall off
screen. Everything above the row with Don't show this again and Close now
scrolls in the room the window has left, and that row stays on screen.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* Floating windows: the flat title bar and wider margins leave this change

Reverts 2d39c54e and its revision-history row. The restyle changes every
dialog, the Read Aloud player and the file browser, and is to be reviewed
on its own.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* Get the Missing Fonts: the headline counts the fonts that can be downloaded

The box lists only the missing fonts with a download, so "1 font in this
document isn't installed" was wrong for a document missing others too. It
now says "1 missing font can be downloaded", and the line under it no longer
mixes one and many.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-17 21:05:13 +01:00
Martin BallandClaude Opus 5.5 1d71d904d2 #778: a clipboard DIB's size is read by craft-image-codecs' BMP probe (#6949)
`dib_header_dims` read a DIB header's width and height by hand. It now
wraps the header's first 12 bytes as a BMP and asks
`craft_image_codecs::bmp_probe::dimensions`. Header sizes other than 12
and 40 or more are still refused here, since the probe reads them as
info headers. The old reader stays as a test oracle.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 14:47:31 -05:00
Martin BallandClaude Opus 5.5 e28c25639b #778: the ZIP entry-count guard delegates to craft-zip (#6948)
* #778: zip_guard delegates to craft-zip's entry-count guard

wordcraft_doc::zip_guard::check and zip64_entry_count_sample now call craft-zip, which also
treats a central-directory size of u32::MAX as a ZIP64 marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* #778: Cargo.lock: wordcraft-doc depends on craft-zip

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* #778: zip_guard: craft-zip test-support as a dev-dependency

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 14:47:27 -05:00
Martin BallandClaude Opus 5.5 7b3c2efb1f CI: skip every performance test under coverage instrumentation (#6873)
SonarCloud's coverage shards run tests built with -Cinstrument-coverage
on shared runners. Tests that assert a wall-clock limit or a time
ratio swing there: the megabyte-word test took 5.7-23 s against a
15 s limit, and three linear-time tests read 24-31x against 24x. Each
one that flakes turns main red after merging.

Ignore under cfg(coverage) every test whose purpose is performance
(linear time, bounded, capped, fast). The gate CI still runs them all
uninstrumented. Behaviour tests that only carry a generous time guard
(fuzz, never-panics, clipboard, regex, render, recent fonts) and the
SSIP speech timeout tests keep running, so their checks stay covered.


Claude-Session: https://claude.ai/code/session_01FypgCXtSjB6e5jmy9Uq7u8

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 20:29:14 +01:00
Martin Ball 9e2691290f Merge remote-tracking branch 'origin/main' into repin/6785
; Conflicts:
;	Cargo.toml
2026-11-17 16:53:27 +00:00
Michael MuandClaude Sonnet 5.5 8b212e6665 KeyTips: a compact button's badge no longer covers its icon (#4497) (#4503)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 16:42:25 +01:00
Diego GueriniandClaude Opus 5.5 26b3ae2cc2 inspect_document marks the page fields of headers and footers (#4467) (#4474)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:26:33 +01:00
Martin BallandClaude Opus 5.5 e784a2aaed Tests: review.language's no-proofing case uses zxx, not fr-FR (#6944)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:12:46 +01:00
pakiatandClaude Opus 5.5 65212d6856 Shapes: lines move by their body and resize by their ends (#4393)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:59:23 +01:00
Thân Minh Nghĩa 5c8a6e1d75 #3917: Text Outline and Shape Outline › Weight show a line sample for each weight (#3933) 2026-11-17 15:45:51 +01:00
Thân Minh NghĩaandClaude Opus 5.5 83f18db12d #2786: Home › Bullets: Define New Bullet… dialog and Change List Level › (#2860)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:29:27 +01:00
Dylan M. Taylor cd839d35b6 Regenerate codepage dependency ordering in Cargo lockfile 2026-11-15 12:00:40 -05:00
Dylan M. Taylor ae022437c8 doc: reuse shared legacy codepage tables with parity tests 2026-11-15 09:40:33 -05:00
78 changed files with 3460 additions and 709 deletions
Generated
+45 -42
View File
@@ -1060,7 +1060,7 @@ dependencies = [
[[package]]
name = "craft-atomic"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"rustix 1.1.5",
]
@@ -1068,17 +1068,17 @@ dependencies = [
[[package]]
name = "craft-cfb"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
[[package]]
name = "craft-codepage"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
[[package]]
name = "craft-dds"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"rayon",
]
@@ -1086,7 +1086,7 @@ dependencies = [
[[package]]
name = "craft-doc-binary"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"chrono",
"craft-cfb",
@@ -1102,7 +1102,7 @@ dependencies = [
[[package]]
name = "craft-docx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"base64 0.23.1",
"chrono",
@@ -1122,7 +1122,7 @@ dependencies = [
[[package]]
name = "craft-drawingml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-xml",
"kurbo",
@@ -1133,7 +1133,7 @@ dependencies = [
[[package]]
name = "craft-flow"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-wordmodel",
"image",
@@ -1143,7 +1143,7 @@ dependencies = [
[[package]]
name = "craft-font-catalog"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"fontique",
"harfrust 0.13.3",
@@ -1158,7 +1158,7 @@ dependencies = [
[[package]]
name = "craft-font-store"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"cap-primitives",
"craft-atomic",
@@ -1179,12 +1179,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
[[package]]
name = "craft-html"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-codepage",
"craft-flow",
@@ -1197,7 +1197,7 @@ dependencies = [
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -1220,7 +1220,7 @@ dependencies = [
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-dds",
"craft-jpeg",
@@ -1247,7 +1247,7 @@ dependencies = [
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"thiserror 2.0.21",
]
@@ -1255,7 +1255,7 @@ dependencies = [
[[package]]
name = "craft-latex"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-flow",
"craft-math",
@@ -1266,7 +1266,7 @@ dependencies = [
[[package]]
name = "craft-markdown"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-flow",
"craft-html",
@@ -1276,7 +1276,7 @@ dependencies = [
[[package]]
name = "craft-math"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-xml",
"serde",
@@ -1285,7 +1285,7 @@ dependencies = [
[[package]]
name = "craft-metadata"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"brotli-decompressor 6.3.1",
"chrono",
@@ -1297,7 +1297,7 @@ dependencies = [
[[package]]
name = "craft-metafile"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"encoding_rs",
"image",
@@ -1308,7 +1308,7 @@ dependencies = [
[[package]]
name = "craft-mime"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"chardetng",
"chrono",
@@ -1319,7 +1319,7 @@ dependencies = [
[[package]]
name = "craft-odf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"aes",
"base64 0.23.1",
@@ -1345,7 +1345,7 @@ dependencies = [
[[package]]
name = "craft-ods"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-odf",
"craft-sheet",
@@ -1356,7 +1356,7 @@ dependencies = [
[[package]]
name = "craft-offcrypto"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"aes",
"base64 0.23.1",
@@ -1373,7 +1373,7 @@ dependencies = [
[[package]]
name = "craft-opc"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-xml",
"craft-zip",
@@ -1384,7 +1384,7 @@ dependencies = [
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"krilla",
"thiserror 2.0.21",
@@ -1393,7 +1393,7 @@ dependencies = [
[[package]]
name = "craft-print"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-winprint-gdi",
"log",
@@ -1407,7 +1407,7 @@ dependencies = [
[[package]]
name = "craft-proof"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"log",
"miniz_oxide 0.9.1",
@@ -1418,7 +1418,7 @@ dependencies = [
[[package]]
name = "craft-rtf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-codepage",
"craft-flow",
@@ -1431,12 +1431,12 @@ dependencies = [
[[package]]
name = "craft-sheet"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"serde",
"thiserror 2.0.21",
@@ -1445,7 +1445,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -1465,7 +1465,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"chrono",
"dunce",
@@ -1492,7 +1492,7 @@ dependencies = [
[[package]]
name = "craft-web-api"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"js-sys",
"serde_json",
@@ -1503,7 +1503,7 @@ dependencies = [
[[package]]
name = "craft-web-host"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"wasm-bindgen",
"web-sys",
@@ -1512,7 +1512,7 @@ dependencies = [
[[package]]
name = "craft-window"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"egui",
"objc2 0.6.5",
@@ -1531,7 +1531,7 @@ dependencies = [
[[package]]
name = "craft-wordmodel"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"base64 0.23.1",
"craft-math",
@@ -1545,7 +1545,7 @@ dependencies = [
[[package]]
name = "craft-xls"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-cfb",
"craft-sheet",
@@ -1558,7 +1558,7 @@ dependencies = [
[[package]]
name = "craft-xlsx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-drawingml",
"craft-opc",
@@ -1570,7 +1570,7 @@ dependencies = [
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"quick-xml 0.41.0",
"serde",
@@ -1580,7 +1580,7 @@ dependencies = [
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"flate2",
"thiserror 2.0.21",
@@ -3416,7 +3416,7 @@ dependencies = [
[[package]]
name = "krilla"
version = "0.8.2"
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"base64 0.22.1",
"bumpalo",
@@ -7385,6 +7385,8 @@ name = "wordcraft-doc"
version = "0.18.1"
dependencies = [
"base64 0.23.1",
"craft-codepage",
"craft-zip",
"log",
"proptest",
"serde",
@@ -7468,6 +7470,7 @@ dependencies = [
"harfrust 0.13.3",
"kurbo",
"log",
"serde_json",
"skrifa 0.47.0",
"unicode-bidi",
"unicode-script",
+31 -29
View File
@@ -24,19 +24,20 @@ wordcraft-pdf = { path = "crates/pdf" }
# Proofing is craft-libs' craft-proof (it was crates/proof); its Language data is craft-i18n's
# language-data/, embedded from CRAFT_I18N_DIR (craft-i18n.rev). The package keeps the
# `wordcraft-proof` name here so `wordcraft_proof::` paths are unchanged.
# craft-proof is storytold/craft-libs main at 453b0da782. That commit has #590 (ca-ES-valencia
# craft-proof is storytold/craft-libs main at 93dac78c. That tree has #590 (ca-ES-valencia
# reads the ca-valencia pack), #589 (French elision suggestions first), #497 (flag-based
# compounding), #496 (review follow-ups, en-AU) and the merged #482–#490/#495 stack; the font
# store's `language-pack` kind is #486 (CP4.1).
wordcraft-proof = { package = "craft-proof", git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-font-store = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
wordcraft-proof = { package = "craft-proof", git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-font-store = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
wordcraft-engine = { path = "crates/engine" }
wordcraft-mcp = { path = "crates/mcp" }
wordcraft-zotero = { path = "crates/zotero" }
wordcraft-control-key = { path = "crates/control-key" }
wordcraft-ui-egui = { path = "crates/ui-egui" }
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-codepage = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
# foundation
kurbo = { version = "0.13", features = ["serde"] }
@@ -67,35 +68,36 @@ wuff = "0.2.9"
brotli-decompressor = "5"
vello_cpu = "0.2"
image = { version = "0.25", default-features = false }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0", default-features = false, features = ["write"] }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0", default-features = false, features = ["write"] }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["egui"] }
craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0", features = ["doc"] }
craft-doc-binary = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-flow = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-odf = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0", features = ["text"] }
craft-markdown = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-html = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0", features = ["flow"] }
craft-latex = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-math = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0", features = ["detect"] }
craft-sheet = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-ods = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "=0.1.0" }
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", features = ["egui"] }
craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0", features = ["doc"] }
craft-doc-binary = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-flow = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-odf = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0", features = ["text"] }
craft-markdown = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-html = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0", features = ["flow"] }
craft-latex = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-math = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0", features = ["detect"] }
craft-sheet = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-ods = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "=0.1.0" }
# zlib streams (compressed metafile pictures in `.doc` drawings); already in the lock as a dependency of other crates.
miniz_oxide = "0.9"
cfb = "0.15"
# Legacy text encodings (GBK/GB18030, Big5, Shift_JIS, EUC-KR, single-byte code pages) for plain
# text and CSV recipient lists (#778): encoding_rs ((Apache-2.0 OR MIT) AND BSD-3-Clause) and its
# detector chardetng (Apache-2.0 OR MIT), both pure Rust (Firefox's).
# text and CSV recipient lists (#778): encoding_rs ((Apache-2.0 OR MIT) AND BSD-3-Clause), pure
# Rust (Firefox's). Detection is craft-mime's `detect` feature.
encoding_rs = "0.8"
# chardetng (Apache-2.0 OR MIT): only the formats test oracle for the old detector uses it directly.
chardetng = "1"
# Password-protected (agile-encrypted) Word documents: pure-Rust RustCrypto primitives.
aes = "0.9"
@@ -153,7 +155,7 @@ debug = 0
# Required by craft-pdf: the backend rejects writers that failed during unwinding.
[patch.crates-io]
krilla = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
krilla = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
+5 -1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -18,8 +18,8 @@ wordcraft-engine = { workspace = true }
wordcraft-ui-egui = { workspace = true }
# The host messages (storytold/craft-libs): one contract and one bridge for the web builds.
# 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 = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "0.1.0" }
craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", version = "0.1.0" }
craft-web-api = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "0.1.0" }
craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", version = "0.1.0" }
egui = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }
+1
View File
@@ -81,6 +81,7 @@ pub fn start() {
// 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;
app.tell_about_font_downloads();
// Preferences from an earlier visit (#2132), as the desktop app reads `ui.json`.
let stored = stored_prefs();
crate::prefs::restore(&mut app, stored.as_deref());
+1 -1
View File
@@ -86,7 +86,7 @@ clipboard-win = { version = "5.4", features = ["std"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# At the same craft-libs commit as the workspace's crates: `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 = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a" }
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.
+1
View File
@@ -728,6 +728,7 @@ fn main() -> eframe::Result {
app.replace_startup_document(doc);
}
app.watch_desktop_theme(&cc.egui_ctx);
app.tell_about_font_downloads();
if prefs_enabled()
&& let Some(dir) = wordcraft_control_key::settings_dir()
{
+4 -1
View File
@@ -13,10 +13,12 @@ workspace = true
# `zip_guard::zip64_entry_count_sample`) and the step count of
# `work::count`; turned on only as a dev-dependency, so never built into
# the app.
test-support = []
test-support = ["craft-zip/test-support"]
[dependencies]
craft-codepage = { workspace = true }
wordcraft-geom = { workspace = true }
craft-zip = { workspace = true }
base64 = { workspace = true }
log = { workspace = true }
serde = { workspace = true }
@@ -25,4 +27,5 @@ thiserror = { workspace = true }
unicode-segmentation = { workspace = true }
[dev-dependencies]
craft-zip = { workspace = true, features = ["test-support"] }
proptest = { workspace = true }
+44 -337
View File
@@ -1,345 +1,52 @@
//! Windows-1252 (the byte encoding legacy Word text pieces use) and the other single-byte Windows
//! code pages RTF files name with `\ansicpg` or a font's `\fcharset` (Cyrillic 1251, Central
//! European 1250, Greek, Turkish, Hebrew, Arabic, Baltic, Vietnamese, Thai), Mac OS Roman
//! (10000, RTF `\mac`), and the East Asian multibyte code pages (932 Shift-JIS, 936 GBK, 949 UHC,
//! 950 Big5), whose bytes decode as runs rather than one at a time.
//! Legacy character decoding delegates to craft-codepage; document fallback policy stays here.
pub use craft_codepage::{
MAC_ROMAN_CODE_PAGE, MULTIBYTE_CODE_PAGES, OEM_CODE_PAGES, SINGLE_BYTE_CODE_PAGES, cp1252, is_multibyte_code_page, multibyte_encoding_name,
oem_byte, windows_byte,
};
/// Windows-1252 code points for 0x80..=0x9F.
const CP1252: [u16; 32] = [
0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, 0x0090, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178,
];
/// One Windows-1252 byte as a char.
pub fn cp1252(b: u8) -> char {
if (0x80..0xA0).contains(&b) {
CP1252.get((b - 0x80) as usize).and_then(|u| char::from_u32(u32::from(*u))).unwrap_or('\u{FFFD}')
} else {
b as char
}
/// Word's supported single-byte document code pages (symbol-font policy is separate).
pub fn is_byte_code_page(cp: u16) -> bool {
SINGLE_BYTE_CODE_PAGES.contains(&cp) || OEM_CODE_PAGES.contains(&cp) || cp == MAC_ROMAN_CODE_PAGE
}
/// Windows-874 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP874: [u16; 128] = [
0x20AC, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2026, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A0, 0x0E01, 0x0E02, 0x0E03,
0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15,
0x0E16, 0x0E17, 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F, 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27,
0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, 0x0E38, 0x0E39,
0x0E3A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x0E3F, 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B,
0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD,
];
/// Windows-1250 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1250: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, 0xFFFD, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A, 0x00A0, 0x02C7, 0x02D8, 0x0141,
0x00A4, 0x0104, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B, 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E,
0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB,
0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD,
0x0163, 0x02D9,
];
/// Windows-1251 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1251: [u16; 128] = [
0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F, 0x0452, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F, 0x00A0, 0x040E, 0x045E, 0x0408,
0x00A4, 0x0490, 0x00A6, 0x00A7, 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407, 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5,
0x00B6, 0x00B7, 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457, 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429,
0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B,
0x043C, 0x043D, 0x043E, 0x043F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D,
0x044E, 0x044F,
];
/// Windows-1253 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1253: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A0, 0x0385, 0x0386, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0xFFFD, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5,
0x00B6, 0x00B7, 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0, 0x03A1, 0xFFFD, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9,
0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB,
0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD,
0x03CE, 0xFFFD,
];
/// Windows-1254 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1254: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0xFFFD, 0xFFFD, 0x0178, 0x00A0, 0x00A1, 0x00A2, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x00D9,
0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB,
0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131,
0x015F, 0x00FF,
];
/// Windows-1255 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1255: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A0, 0x00A1, 0x00A2, 0x00A3,
0x20AA, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF, 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB,
0x05DC, 0x05DD, 0x05DE, 0x05DF, 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E,
0x200F, 0xFFFD,
];
/// Windows-1256 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1256: [u16; 128] = [
0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688, 0x06AF, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA, 0x00A0, 0x060C, 0x00A2, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F, 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7, 0x0637, 0x0638,
0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643, 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB,
0x0649, 0x064A, 0x00EE, 0x00EF, 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7, 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E,
0x200F, 0x06D2,
];
/// Windows-1257 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1257: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, 0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0x00A8, 0x02C7, 0x00B8, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0x00AF, 0x02DB, 0xFFFD, 0x00A0, 0xFFFD, 0x00A2, 0x00A3,
0x00A4, 0xFFFD, 0x00A6, 0x00A7, 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6, 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112,
0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B, 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, 0x0172, 0x0141,
0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF, 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, 0x010D, 0x00E9, 0x017A, 0x0117,
0x0123, 0x0137, 0x012B, 0x013C, 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C,
0x017E, 0x02D9,
];
/// Windows-1258 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1258: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0xFFFD, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0xFFFD, 0x203A, 0x0153, 0xFFFD, 0xFFFD, 0x0178, 0x00A0, 0x00A1, 0x00A2, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF, 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7, 0x00D8, 0x00D9,
0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB,
0x0301, 0x00ED, 0x00EE, 0x00EF, 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0,
0x20AB, 0x00FF,
];
/// Mac OS Roman (code page 10000, RTF `\mac`) code points for 0x80..=0xFF, with the euro sign at
/// 0xDB (Mac OS 8.5 and later).
const MAC_ROMAN: [u16; 128] = [
0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 0x00EA, 0x00EB,
0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 0x2020, 0x00B0, 0x00A2, 0x00A3,
0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5,
0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB,
0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178,
0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE,
0x00CF, 0x00CC, 0x00D3, 0x00D4, 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD,
0x02DB, 0x02C7,
];
/// IBM PC (OEM) code page 437 code points for 0x80..=0xFF (the Unicode Consortium's CP437
/// mapping file).
const CP437: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, 0x00C9, 0x00E6,
0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561,
0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518,
0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4,
0x221E, 0x03C6, 0x03B5, 0x2229, 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2,
0x25A0, 0x00A0,
];
/// OEM code page 850 (DOS Latin-1, Western European) code points for 0x80..=0xFF (the Unicode
/// Consortium's CP850 mapping file).
const CP850: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, 0x00C9, 0x00E6,
0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1,
0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, 0x00CF, 0x2518,
0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, 0x00DE, 0x00DA, 0x00DB, 0x00D9,
0x00FD, 0x00DD, 0x00AF, 0x00B4, 0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2,
0x25A0, 0x00A0,
];
/// OEM code page 852 (DOS Latin-2, Central European) code points for 0x80..=0xFF (the Unicode Consortium's CP852
/// mapping file).
const CP852: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7, 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106, 0x00C9, 0x0139,
0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x0104, 0x0105, 0x017D, 0x017E, 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1,
0x00C2, 0x011A, 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE, 0x011B, 0x2518,
0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580, 0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161, 0x0154, 0x00DA, 0x0155, 0x0170,
0x00FD, 0x00DD, 0x0163, 0x00B4, 0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159,
0x25A0, 0x00A0,
];
/// OEM code page 866 (DOS Cyrillic) code points for 0x80..=0xFF (the Unicode Consortium's CP866
/// mapping file).
const CP866: [u16; 128] = [
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, 0x0420, 0x0421,
0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, 0x0430, 0x0431, 0x0432, 0x0433,
0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561,
0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518,
0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B,
0x044C, 0x044D, 0x044E, 0x044F, 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4,
0x25A0, 0x00A0,
];
/// OEM code page 737 (DOS Greek) code points for 0x80..=0xFF (the Unicode Consortium's CP737
/// mapping file).
const CP737: [u16; 128] = [
0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0, 0x03A1, 0x03A3,
0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC,
0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561,
0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518,
0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD, 0x03CB, 0x03CE, 0x0386, 0x0388,
0x0389, 0x038A, 0x038C, 0x038E, 0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2,
0x25A0, 0x00A0,
];
/// OEM code page 857 (DOS Turkish; U+FFFD where the byte is unassigned) code points for 0x80..=0xFF (the Unicode Consortium's CP857
/// mapping file).
const CP857: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5, 0x00C9, 0x00E6,
0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x00F1, 0x00D1, 0x011E, 0x011F, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1,
0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, 0xFFFD, 0x00CD, 0x00CE, 0x00CF, 0x2518,
0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0xFFFD, 0x00D7, 0x00DA, 0x00DB, 0x00D9,
0x00EC, 0x00FF, 0x00AF, 0x00B4, 0x00AD, 0x00B1, 0xFFFD, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2,
0x25A0, 0x00A0,
];
/// The glyphs the OEM code pages show for 0x01..=0x1F (smileys, card suits, arrows), as
/// Windows' Alt+numpad entry produces them rather than the control characters.
const OEM_GLYPHS: [u16; 31] = [
0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C, 0x25BA, 0x25C4, 0x2195,
0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC,
];
/// The OEM code pages [`oem_byte`] decodes.
pub const OEM_CODE_PAGES: [u16; 6] = [437, 737, 850, 852, 857, 866];
/// One byte of an OEM code page in [`OEM_CODE_PAGES`] (any other code page reads as 437) as a
/// char, with the graphic glyphs for 0x01..=0x1F and 0x7F (⌂); 0x00 stays NUL.
pub fn oem_byte(code_page: u16, b: u8) -> char {
let table: &[u16; 128] = match code_page {
737 => &CP737,
850 => &CP850,
852 => &CP852,
857 => &CP857,
866 => &CP866,
_ => &CP437,
};
let u = match b {
0x01..=0x1F => OEM_GLYPHS.get(usize::from(b - 1)).copied(),
0x7F => Some(0x2302),
0x80..=0xFF => table.get(usize::from(b - 0x80)).copied(),
_ => Some(u16::from(b)),
};
u.and_then(|u| char::from_u32(u32::from(u))).unwrap_or('\u{FFFD}')
/// Decode document text without applying OEM control glyphs or symbol-font remapping.
pub fn code_page_byte(cp: u16, b: u8) -> char {
if b >= 0x80 && OEM_CODE_PAGES.contains(&cp) { oem_byte(cp, b) } else { windows_byte(cp, b) }
}
/// The single-byte Windows code pages [`windows_byte`] decodes (1252 included).
pub const SINGLE_BYTE_CODE_PAGES: [u16; 10] = [874, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258];
/// Mac OS Roman, RTF's `\mac` character set.
pub const MAC_ROMAN_CODE_PAGE: u16 = 10000;
/// One byte of a single-byte code page as a char: a Windows one in [`SINGLE_BYTE_CODE_PAGES`] or
/// Mac OS Roman ([`MAC_ROMAN_CODE_PAGE`]); any other code page reads as Windows-1252.
pub fn windows_byte(code_page: u16, b: u8) -> char {
let table: &[u16; 128] = match code_page {
874 => &CP874,
1250 => &CP1250,
1251 => &CP1251,
1253 => &CP1253,
1254 => &CP1254,
1255 => &CP1255,
1256 => &CP1256,
1257 => &CP1257,
1258 => &CP1258,
MAC_ROMAN_CODE_PAGE => &MAC_ROMAN,
_ => return cp1252(b),
};
if b < 0x80 {
return b as char;
}
table.get(usize::from(b - 0x80)).and_then(|u| char::from_u32(u32::from(*u))).unwrap_or('\u{FFFD}')
/// Whether Word's document import accepts this encoding.
pub fn is_text_code_page(cp: u16) -> bool {
is_byte_code_page(cp) || is_multibyte_code_page(cp)
}
/// Whether [`code_page_byte`] decodes `code_page`: a Windows one ([`SINGLE_BYTE_CODE_PAGES`]), an
/// OEM (DOS) one ([`OEM_CODE_PAGES`]) or Mac OS Roman. Document formats (RTF) name all three.
pub fn is_byte_code_page(code_page: u16) -> bool {
SINGLE_BYTE_CODE_PAGES.contains(&code_page) || OEM_CODE_PAGES.contains(&code_page) || code_page == MAC_ROMAN_CODE_PAGE
}
/// A byte of 8-bit document text in `code_page` (see [`is_byte_code_page`]) as a char. Below 0x80
/// it is ASCII in every code page (text, so an OEM page's glyphs for control codes don't apply).
pub fn code_page_byte(code_page: u16, b: u8) -> char {
if b >= 0x80 && OEM_CODE_PAGES.contains(&code_page) { oem_byte(code_page, b) } else { windows_byte(code_page, b) }
}
/// The East Asian multibyte code pages a document format names with `\ansicpg` or a font's `\cpg`:
/// 932 Shift-JIS (Japanese), 936 GBK (Simplified Chinese), 949 UHC (Korean) and 950 Big5
/// (Traditional Chinese).
pub const MULTIBYTE_CODE_PAGES: [u16; 4] = [932, 936, 949, 950];
/// Whether `code_page` is one of the East Asian multibyte code pages ([`MULTIBYTE_CODE_PAGES`]):
/// its bytes decode as a run, a lead byte together with its trail byte.
pub fn is_multibyte_code_page(code_page: u16) -> bool {
multibyte_encoding_name(code_page).is_some()
}
/// The WHATWG encoding name of an East Asian multibyte code page (see [`MULTIBYTE_CODE_PAGES`]),
/// for `encoding_rs::Encoding::for_label`; `None` for the single-byte code pages, whose bytes
/// decode one at a time, and anything else.
pub fn multibyte_encoding_name(code_page: u16) -> Option<&'static str> {
Some(match code_page {
932 => "shift_jis",
936 => "gbk",
949 => "euc-kr",
950 => "big5",
_ => return None,
})
}
/// Whether a document format can name `code_page` for its text: a single-byte Windows, DOS or Mac
/// one ([`is_byte_code_page`]) or an East Asian multibyte one ([`is_multibyte_code_page`]).
pub fn is_text_code_page(code_page: u16) -> bool {
is_byte_code_page(code_page) || is_multibyte_code_page(code_page)
}
/// The code page of an RTF or Word font charset (`\fcharset`): 204 Russian → 1251, 238 Eastern
/// European → 1250, 77 Mac → 10000, 254 PC / 255 OEM → 437, 128 Japanese → 932, 129 Korean → 949,
/// 134 Simplified Chinese → 936, 136 Traditional Chinese → 950… `None` for the ANSI and default
/// charsets (the document's code page applies) and symbol fonts.
/// Preserve Word's existing symbol-font selection policy, not the shared symbol extension.
pub fn charset_code_page(charset: i32) -> Option<u16> {
Some(match charset {
77 => MAC_ROMAN_CODE_PAGE,
128 => 932,
129 => 949,
134 => 936,
136 => 950,
161 => 1253,
162 => 1254,
163 => 1258,
177 => 1255,
178 => 1256,
186 => 1257,
204 => 1251,
222 => 874,
238 => 1250,
254 | 255 => 437,
_ => return None,
})
if charset == 2 { None } else { craft_codepage::charset_code_page(charset) }
}
#[cfg(test)]
#[allow(dead_code)]
#[path = "encoding_legacy_tests.rs"]
mod legacy;
#[cfg(test)]
mod parity {
use super::*;
#[test]
fn every_supported_byte_and_fallback_matches_original() {
let pages = [0, 42, 437, 737, 850, 852, 857, 866, 874, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 10000, 9999, u16::MAX];
for cp in pages {
for b in 0..=u8::MAX {
assert_eq!(windows_byte(cp, b), legacy::windows_byte(cp, b), "windows {cp}/{b}");
assert_eq!(oem_byte(cp, b), legacy::oem_byte(cp, b), "oem {cp}/{b}");
assert_eq!(code_page_byte(cp, b), legacy::code_page_byte(cp, b), "text {cp}/{b}");
assert_eq!(cp1252(b), legacy::cp1252(b));
}
}
for cp in 0..=u16::MAX {
assert_eq!(is_byte_code_page(cp), legacy::is_byte_code_page(cp));
assert_eq!(is_text_code_page(cp), legacy::is_text_code_page(cp));
assert_eq!(multibyte_encoding_name(cp), legacy::multibyte_encoding_name(cp));
}
for charset in -1..=256 {
assert_eq!(charset_code_page(charset), legacy::charset_code_page(charset));
}
}
}
#[cfg(test)]
+428
View File
@@ -0,0 +1,428 @@
// Frozen pre-cutover implementation, test-only parity oracle. Original authors retained in file history.
//! Windows-1252 (the byte encoding legacy Word text pieces use) and the other single-byte Windows
//! code pages RTF files name with `\ansicpg` or a font's `\fcharset` (Cyrillic 1251, Central
//! European 1250, Greek, Turkish, Hebrew, Arabic, Baltic, Vietnamese, Thai), Mac OS Roman
//! (10000, RTF `\mac`), and the East Asian multibyte code pages (932 Shift-JIS, 936 GBK, 949 UHC,
//! 950 Big5), whose bytes decode as runs rather than one at a time.
/// Windows-1252 code points for 0x80..=0x9F.
const CP1252: [u16; 32] = [
0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, 0x0090, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178,
];
/// One Windows-1252 byte as a char.
pub fn cp1252(b: u8) -> char {
if (0x80..0xA0).contains(&b) {
CP1252.get((b - 0x80) as usize).and_then(|u| char::from_u32(u32::from(*u))).unwrap_or('\u{FFFD}')
} else {
b as char
}
}
/// Windows-874 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP874: [u16; 128] = [
0x20AC, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2026, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A0, 0x0E01, 0x0E02, 0x0E03,
0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15,
0x0E16, 0x0E17, 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F, 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27,
0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F, 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, 0x0E38, 0x0E39,
0x0E3A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x0E3F, 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B,
0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0xFFFD, 0xFFFD,
0xFFFD, 0xFFFD,
];
/// Windows-1250 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1250: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, 0xFFFD, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A, 0x00A0, 0x02C7, 0x02D8, 0x0141,
0x00A4, 0x0104, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B, 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E,
0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB,
0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD,
0x0163, 0x02D9,
];
/// Windows-1251 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1251: [u16; 128] = [
0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F, 0x0452, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F, 0x00A0, 0x040E, 0x045E, 0x0408,
0x00A4, 0x0490, 0x00A6, 0x00A7, 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407, 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5,
0x00B6, 0x00B7, 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457, 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429,
0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B,
0x043C, 0x043D, 0x043E, 0x043F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D,
0x044E, 0x044F,
];
/// Windows-1253 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1253: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A0, 0x0385, 0x0386, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0xFFFD, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5,
0x00B6, 0x00B7, 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0, 0x03A1, 0xFFFD, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9,
0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB,
0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD,
0x03CE, 0xFFFD,
];
/// Windows-1254 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1254: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0xFFFD, 0xFFFD, 0x0178, 0x00A0, 0x00A1, 0x00A2, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, 0x00D8, 0x00D9,
0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB,
0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131,
0x015F, 0x00FF,
];
/// Windows-1255 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1255: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A0, 0x00A1, 0x00A2, 0x00A3,
0x20AA, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF, 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3, 0x05F4, 0xFFFD,
0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB,
0x05DC, 0x05DD, 0x05DE, 0x05DF, 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E,
0x200F, 0xFFFD,
];
/// Windows-1256 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1256: [u16; 128] = [
0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688, 0x06AF, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA, 0x00A0, 0x060C, 0x00A2, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F, 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7, 0x0637, 0x0638,
0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643, 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB,
0x0649, 0x064A, 0x00EE, 0x00EF, 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7, 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E,
0x200F, 0x06D2,
];
/// Windows-1257 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1257: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0xFFFD, 0x201E, 0x2026, 0x2020, 0x2021, 0xFFFD, 0x2030, 0xFFFD, 0x2039, 0xFFFD, 0x00A8, 0x02C7, 0x00B8, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0xFFFD, 0x2122, 0xFFFD, 0x203A, 0xFFFD, 0x00AF, 0x02DB, 0xFFFD, 0x00A0, 0xFFFD, 0x00A2, 0x00A3,
0x00A4, 0xFFFD, 0x00A6, 0x00A7, 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6, 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112,
0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B, 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7, 0x0172, 0x0141,
0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF, 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113, 0x010D, 0x00E9, 0x017A, 0x0117,
0x0123, 0x0137, 0x012B, 0x013C, 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7, 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C,
0x017E, 0x02D9,
];
/// Windows-1258 code points for 0x80..=0xFF (U+FFFD where the byte is unassigned).
const CP1258: [u16; 128] = [
0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0xFFFD, 0x2039, 0x0152, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2018,
0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0xFFFD, 0x203A, 0x0153, 0xFFFD, 0xFFFD, 0x0178, 0x00A0, 0x00A1, 0x00A2, 0x00A3,
0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5,
0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF, 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7, 0x00D8, 0x00D9,
0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB,
0x0301, 0x00ED, 0x00EE, 0x00EF, 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0,
0x20AB, 0x00FF,
];
/// Mac OS Roman (code page 10000, RTF `\mac`) code points for 0x80..=0xFF, with the euro sign at
/// 0xDB (Mac OS 8.5 and later).
const MAC_ROMAN: [u16; 128] = [
0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 0x00EA, 0x00EB,
0x00ED, 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 0x2020, 0x00B0, 0x00A2, 0x00A3,
0x00A7, 0x2022, 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5,
0x2202, 0x2211, 0x220F, 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB,
0x00BB, 0x2026, 0x00A0, 0x00C0, 0x00C3, 0x00D5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178,
0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, 0xFB02, 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE,
0x00CF, 0x00CC, 0x00D3, 0x00D4, 0xF8FF, 0x00D2, 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD,
0x02DB, 0x02C7,
];
/// IBM PC (OEM) code page 437 code points for 0x80..=0xFF (the Unicode Consortium's CP437
/// mapping file).
const CP437: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, 0x00C9, 0x00E6,
0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561,
0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518,
0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4,
0x221E, 0x03C6, 0x03B5, 0x2229, 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2,
0x25A0, 0x00A0,
];
/// OEM code page 850 (DOS Latin-1, Western European) code points for 0x80..=0xFF (the Unicode
/// Consortium's CP850 mapping file).
const CP850: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5, 0x00C9, 0x00E6,
0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1,
0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, 0x00CF, 0x2518,
0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, 0x00DE, 0x00DA, 0x00DB, 0x00D9,
0x00FD, 0x00DD, 0x00AF, 0x00B4, 0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2,
0x25A0, 0x00A0,
];
/// OEM code page 852 (DOS Latin-2, Central European) code points for 0x80..=0xFF (the Unicode Consortium's CP852
/// mapping file).
const CP852: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7, 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106, 0x00C9, 0x0139,
0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x0104, 0x0105, 0x017D, 0x017E, 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1,
0x00C2, 0x011A, 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE, 0x011B, 0x2518,
0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580, 0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161, 0x0154, 0x00DA, 0x0155, 0x0170,
0x00FD, 0x00DD, 0x0163, 0x00B4, 0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159,
0x25A0, 0x00A0,
];
/// OEM code page 866 (DOS Cyrillic) code points for 0x80..=0xFF (the Unicode Consortium's CP866
/// mapping file).
const CP866: [u16; 128] = [
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, 0x0420, 0x0421,
0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, 0x0430, 0x0431, 0x0432, 0x0433,
0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561,
0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518,
0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B,
0x044C, 0x044D, 0x044E, 0x044F, 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4,
0x25A0, 0x00A0,
];
/// OEM code page 737 (DOS Greek) code points for 0x80..=0xFF (the Unicode Consortium's CP737
/// mapping file).
const CP737: [u16; 128] = [
0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0, 0x03A1, 0x03A3,
0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC,
0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561,
0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518,
0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580, 0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD, 0x03CB, 0x03CE, 0x0386, 0x0388,
0x0389, 0x038A, 0x038C, 0x038E, 0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2,
0x25A0, 0x00A0,
];
/// OEM code page 857 (DOS Turkish; U+FFFD where the byte is unassigned) code points for 0x80..=0xFF (the Unicode Consortium's CP857
/// mapping file).
const CP857: [u16; 128] = [
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5, 0x00C9, 0x00E6,
0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F, 0x00E1, 0x00ED, 0x00F3, 0x00FA,
0x00F1, 0x00D1, 0x011E, 0x011F, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB, 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1,
0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510, 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3,
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4, 0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, 0xFFFD, 0x00CD, 0x00CE, 0x00CF, 0x2518,
0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580, 0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0xFFFD, 0x00D7, 0x00DA, 0x00DB, 0x00D9,
0x00EC, 0x00FF, 0x00AF, 0x00B4, 0x00AD, 0x00B1, 0xFFFD, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2,
0x25A0, 0x00A0,
];
/// The glyphs the OEM code pages show for 0x01..=0x1F (smileys, card suits, arrows), as
/// Windows' Alt+numpad entry produces them rather than the control characters.
const OEM_GLYPHS: [u16; 31] = [
0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C, 0x25BA, 0x25C4, 0x2195,
0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC,
];
/// The OEM code pages [`oem_byte`] decodes.
pub const OEM_CODE_PAGES: [u16; 6] = [437, 737, 850, 852, 857, 866];
/// One byte of an OEM code page in [`OEM_CODE_PAGES`] (any other code page reads as 437) as a
/// char, with the graphic glyphs for 0x01..=0x1F and 0x7F (⌂); 0x00 stays NUL.
pub fn oem_byte(code_page: u16, b: u8) -> char {
let table: &[u16; 128] = match code_page {
737 => &CP737,
850 => &CP850,
852 => &CP852,
857 => &CP857,
866 => &CP866,
_ => &CP437,
};
let u = match b {
0x01..=0x1F => OEM_GLYPHS.get(usize::from(b - 1)).copied(),
0x7F => Some(0x2302),
0x80..=0xFF => table.get(usize::from(b - 0x80)).copied(),
_ => Some(u16::from(b)),
};
u.and_then(|u| char::from_u32(u32::from(u))).unwrap_or('\u{FFFD}')
}
/// The single-byte Windows code pages [`windows_byte`] decodes (1252 included).
pub const SINGLE_BYTE_CODE_PAGES: [u16; 10] = [874, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258];
/// Mac OS Roman, RTF's `\mac` character set.
pub const MAC_ROMAN_CODE_PAGE: u16 = 10000;
/// One byte of a single-byte code page as a char: a Windows one in [`SINGLE_BYTE_CODE_PAGES`] or
/// Mac OS Roman ([`MAC_ROMAN_CODE_PAGE`]); any other code page reads as Windows-1252.
pub fn windows_byte(code_page: u16, b: u8) -> char {
let table: &[u16; 128] = match code_page {
874 => &CP874,
1250 => &CP1250,
1251 => &CP1251,
1253 => &CP1253,
1254 => &CP1254,
1255 => &CP1255,
1256 => &CP1256,
1257 => &CP1257,
1258 => &CP1258,
MAC_ROMAN_CODE_PAGE => &MAC_ROMAN,
_ => return cp1252(b),
};
if b < 0x80 {
return b as char;
}
table.get(usize::from(b - 0x80)).and_then(|u| char::from_u32(u32::from(*u))).unwrap_or('\u{FFFD}')
}
/// Whether [`code_page_byte`] decodes `code_page`: a Windows one ([`SINGLE_BYTE_CODE_PAGES`]), an
/// OEM (DOS) one ([`OEM_CODE_PAGES`]) or Mac OS Roman. Document formats (RTF) name all three.
pub fn is_byte_code_page(code_page: u16) -> bool {
SINGLE_BYTE_CODE_PAGES.contains(&code_page) || OEM_CODE_PAGES.contains(&code_page) || code_page == MAC_ROMAN_CODE_PAGE
}
/// A byte of 8-bit document text in `code_page` (see [`is_byte_code_page`]) as a char. Below 0x80
/// it is ASCII in every code page (text, so an OEM page's glyphs for control codes don't apply).
pub fn code_page_byte(code_page: u16, b: u8) -> char {
if b >= 0x80 && OEM_CODE_PAGES.contains(&code_page) { oem_byte(code_page, b) } else { windows_byte(code_page, b) }
}
/// The East Asian multibyte code pages a document format names with `\ansicpg` or a font's `\cpg`:
/// 932 Shift-JIS (Japanese), 936 GBK (Simplified Chinese), 949 UHC (Korean) and 950 Big5
/// (Traditional Chinese).
pub const MULTIBYTE_CODE_PAGES: [u16; 4] = [932, 936, 949, 950];
/// Whether `code_page` is one of the East Asian multibyte code pages ([`MULTIBYTE_CODE_PAGES`]):
/// its bytes decode as a run, a lead byte together with its trail byte.
pub fn is_multibyte_code_page(code_page: u16) -> bool {
multibyte_encoding_name(code_page).is_some()
}
/// The WHATWG encoding name of an East Asian multibyte code page (see [`MULTIBYTE_CODE_PAGES`]),
/// for `encoding_rs::Encoding::for_label`; `None` for the single-byte code pages, whose bytes
/// decode one at a time, and anything else.
pub fn multibyte_encoding_name(code_page: u16) -> Option<&'static str> {
Some(match code_page {
932 => "shift_jis",
936 => "gbk",
949 => "euc-kr",
950 => "big5",
_ => return None,
})
}
/// Whether a document format can name `code_page` for its text: a single-byte Windows, DOS or Mac
/// one ([`is_byte_code_page`]) or an East Asian multibyte one ([`is_multibyte_code_page`]).
pub fn is_text_code_page(code_page: u16) -> bool {
is_byte_code_page(code_page) || is_multibyte_code_page(code_page)
}
/// The code page of an RTF or Word font charset (`\fcharset`): 204 Russian → 1251, 238 Eastern
/// European → 1250, 77 Mac → 10000, 254 PC / 255 OEM → 437, 128 Japanese → 932, 129 Korean → 949,
/// 134 Simplified Chinese → 936, 136 Traditional Chinese → 950… `None` for the ANSI and default
/// charsets (the document's code page applies) and symbol fonts.
pub fn charset_code_page(charset: i32) -> Option<u16> {
Some(match charset {
77 => MAC_ROMAN_CODE_PAGE,
128 => 932,
129 => 949,
134 => 936,
136 => 950,
161 => 1253,
162 => 1254,
163 => 1258,
177 => 1255,
178 => 1256,
186 => 1257,
204 => 1251,
222 => 874,
238 => 1250,
254 | 255 => 437,
_ => return None,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn single_byte_code_pages() {
let decode = |cp: u16, bytes: &[u8]| bytes.iter().map(|&b| windows_byte(cp, b)).collect::<String>();
assert_eq!(decode(1251, &[0xD1, 0xF7, 0xE5, 0xF2, 0x20, 0xB9]), "Счет №");
assert_eq!(decode(1251, &[0xA8, 0xB8]), "Ёё");
assert_eq!(decode(1250, &[0xA3, 0xF3, 0x9F]), "Łóź");
assert_eq!(decode(1253, &[0xC1, 0xE2]), "Αβ");
assert_eq!(decode(1252, &[0x80, 0xE9]), "ێ");
assert_eq!(decode(9999, &[0xE9]), "é");
// Mac OS Roman (#1653): accents, the euro sign at 0xDB, typographic quotes.
assert_eq!(decode(10000, &[0x80, 0x8E, 0xDB, 0xF1, 0xF2, 0xF3, 0xD2, 0xD3, 0x41]), "Äé€ÒÚÛ“”A");
assert_eq!(charset_code_page(77), Some(10000));
assert_eq!(charset_code_page(254), Some(437));
assert_eq!(charset_code_page(255), Some(437));
assert_eq!(charset_code_page(204), Some(1251));
assert_eq!(charset_code_page(0), None);
for cp in SINGLE_BYTE_CODE_PAGES {
for b in 0..=255u8 {
let _ = windows_byte(cp, b);
}
}
}
#[test]
fn windows_1255_holam_haser_for_vav() {
assert_eq!(windows_byte(1255, 0xCA), '\u{05BA}');
}
/// #1653: one decoder for document text in any code page RTF names: Windows, DOS or Mac.
#[test]
fn document_text_in_windows_dos_and_mac_code_pages() {
let decode = |cp: u16, bytes: &[u8]| bytes.iter().map(|&b| code_page_byte(cp, b)).collect::<String>();
assert_eq!(decode(437, &[0x82, 0xA0, 0x41, 0x01]), "éáA\u{1}", "DOS text; ASCII stays ASCII");
assert_eq!(decode(850, &[0x9D, 0xE9]), "ØÚ");
assert_eq!(decode(10000, &[0x8E]), "é");
assert_eq!(decode(1251, &[0xC4]), "Д");
for cp in [437, 850, 866, 1250, 1252, 10000] {
assert!(is_byte_code_page(cp), "{cp}");
}
assert!(!is_byte_code_page(932) && !is_byte_code_page(0) && !SINGLE_BYTE_CODE_PAGES.contains(&10000));
}
/// #1653: the East Asian charsets and code pages RTF names are multibyte: their bytes decode
/// as runs (a lead byte and its trail byte together), not one at a time.
#[test]
fn east_asian_code_pages_are_multibyte() {
assert_eq!(charset_code_page(128), Some(932), "Japanese");
assert_eq!(charset_code_page(129), Some(949), "Korean");
assert_eq!(charset_code_page(134), Some(936), "Simplified Chinese");
assert_eq!(charset_code_page(136), Some(950), "Traditional Chinese");
assert_eq!(charset_code_page(255), Some(437), "OEM is single-byte");
assert_eq!(MULTIBYTE_CODE_PAGES, [932, 936, 949, 950]);
for cp in MULTIBYTE_CODE_PAGES {
assert!(is_multibyte_code_page(cp) && is_text_code_page(cp), "{cp}");
assert!(!is_byte_code_page(cp) && !SINGLE_BYTE_CODE_PAGES.contains(&cp), "{cp}");
assert!(multibyte_encoding_name(cp).is_some(), "{cp}");
}
assert!(is_text_code_page(1251) && is_text_code_page(437) && is_text_code_page(10000));
assert!(!is_text_code_page(0) && !is_text_code_page(9999));
}
#[test]
fn oem_code_pages() {
let decode = |cp: u16, bytes: &[u8]| bytes.iter().map(|&b| oem_byte(cp, b)).collect::<String>();
assert_eq!(decode(437, &[0x82, 0xE9, 0x01, 0x7F]), "éΘ☺⌂");
assert_eq!(decode(850, &[0x9B, 0xE9]), "øÚ");
assert_eq!(decode(852, &[0xA5, 0x88, 0xBE, 0xA9]), "ąłżę");
assert_eq!(decode(866, &[0x8F, 0xE0, 0xA8, 0xA2, 0xA5, 0xE2, 0xF0, 0xFC]), "ПриветЁ№");
assert_eq!(decode(737, &[0x80, 0x99, 0xE0]), "Αβω");
assert_eq!(decode(857, &[0x8D, 0x98, 0xA6, 0x9F]), "ıİĞş");
assert_eq!(oem_byte(857, 0xD5), '\u{FFFD}', "unassigned in 857");
assert_eq!(decode(9999, &[0xE9]), "Θ", "unknown pages read as 437");
for cp in OEM_CODE_PAGES {
for b in 0..=255u8 {
let _ = oem_byte(cp, b);
}
}
}
}
+4
View File
@@ -144,6 +144,7 @@ fn parts_and_media() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn many_distinct_media_add_in_linear_time() {
// An import with tens of thousands of small distinct images (same length, differing only at
// the end) used to compare each against every earlier one.
@@ -645,6 +646,7 @@ fn para_of_objects(objs: Vec<InlineObject>) -> Paragraph {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn object_walks_are_linear_in_the_paragraph() {
// #3614: loops of object_offsets + object_at recounted the objects before each one, so a
// paragraph of 40,000 markers took seconds per walk.
@@ -691,6 +693,7 @@ fn object_walks_are_linear_in_the_paragraph() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn deleting_across_many_lone_control_markers_is_not_quadratic() {
// A millisecond now; the old delete took 18 s at 1,500 markers and 114 s at 3,000.
let n = 3_000;
@@ -725,6 +728,7 @@ fn control_markers_kept_are_sorted() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn lone_table_open_markers_linear_time() {
let mut d = Document::new();
let mut t = Table::new(1, 1, 100.0);
+145 -119
View File
@@ -1,131 +1,21 @@
//! A bytes-only check of a ZIP package's declared entry count before the `zip` crate opens it
//! (#3660). Every reader of `.docx`, `.odt` and spreadsheet packages calls [`check`] first.
//!
//! `zip::ZipArchive::new` reserves room for, and parses, as many central directory headers as the
//! end record declares before its caller can look at `len()`. A ZIP64 end record can declare
//! millions of entries for a file of a few hundred megabytes (only 46 bytes each have to fit), and
//! each parsed entry costs several times that in memory. Here the end records are found the way
//! `zip` finds them (APPNOTE.TXT §4.3.14 to §4.3.16), and a package declaring more than the
//! reader's limit is refused before `zip` parses anything. Anything else that is wrong is left for
//! `zip` to report.
const EOCD: &[u8; 4] = b"PK\x05\x06";
const EOCD64_LOCATOR: &[u8; 4] = b"PK\x06\x07";
const EOCD64: &[u8; 4] = b"PK\x06\x06";
const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02";
/// End records looked at, at most. A package has one; a few more can turn up inside stored data.
const MAX_END_RECORDS: usize = 16;
//! A bytes-only check of a ZIP package's declared entry count, run before anything opens the ZIP
//! (#3660). The logic lives in `craft-zip`; this wraps its error as a `String`.
/// Refuse `bytes` when the end record `zip` would use declares more than `max_entries` entries.
///
/// `zip` searches the file backwards for an end of central directory record. A ZIP64 one points
/// through its locator at the ZIP64 end record, which `zip` looks for from the locator's offset
/// on: every such record there is checked, and as `zip` may fall back to an earlier end record
/// when one doesn't parse, the search goes on. A plain one is used when it declares no entries, or
/// a central directory header lies between its directory offset and itself: it is the last one
/// looked at.
pub fn check(bytes: &[u8], max_entries: u64) -> Result<(), String> {
let u16_at = |at: usize| bytes.get(at..at.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]]));
let u32_at = |at: usize| bytes.get(at..at.checked_add(4)?).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
let u64_at = |at: usize| bytes.get(at..at.checked_add(8)?).and_then(|b| <[u8; 8]>::try_from(b).ok()).map(u64::from_le_bytes);
let refuse = |n: u64| -> Result<(), String> {
if n > max_entries { Err(format!("the package declares {n} entries, more than {max_entries}")) } else { Ok(()) }
};
let mut end = bytes.len();
let mut seen = 0;
while let Some(at) = bytes.get(..end).and_then(|b| b.windows(4).rposition(|w| w == EOCD)) {
end = at;
seen += 1;
if seen > MAX_END_RECORDS {
return Err(format!("more than {MAX_END_RECORDS} end of central directory records"));
}
// `zip` skips a record whose comment runs past the end of the file.
let Some(comment) = u16_at(at + 20) else { continue };
if at + 22 + usize::from(comment) > bytes.len() {
continue;
}
let (Some(on_disk), Some(total), Some(cd_offset)) = (u16_at(at + 8), u16_at(at + 10), u32_at(at + 16)) else { continue };
let zip64 = total == u16::MAX || cd_offset == u32::MAX;
if zip64
&& let Some(locator) = at.checked_sub(20)
&& bytes.get(locator..locator + 4) == Some(EOCD64_LOCATOR.as_slice())
{
let (Some(eocd64), Some(disks)) = (u64_at(locator + 8), u32_at(locator + 16)) else { continue };
let Some(from) = usize::try_from(eocd64).ok().filter(|&f| f < locator) else { continue };
if disks > 1 {
continue;
}
let records = bytes.get(from..locator).unwrap_or_default().windows(4).enumerate().filter(|(_, w)| *w == EOCD64);
for (i, _) in records {
let p = from + i;
refuse(u64_at(p + 24).unwrap_or(0).max(u64_at(p + 32).unwrap_or(0)))?;
}
continue;
}
if total == 0 {
return Ok(());
}
let cd = cd_offset as usize;
if cd < at && bytes.get(cd..at).is_some_and(|b| b.windows(4).any(|w| w == CENTRAL_HEADER)) {
return refuse(u64::from(on_disk.max(total)));
}
}
Ok(())
craft_zip::check_entry_count(bytes, max_entries).map_err(|e| e.to_string())
}
/// A package of one empty stored file, `a`, whose central directory lists it `entries` times (at
/// most as many as fit in 64 MiB) under a ZIP64 end record that declares them all: `zip` 3.0.0
/// parses and keeps every one of them before its caller can count them. Regression input for the
/// readers' tests: built only for tests, this crate's and (through the `test-support` feature,
/// which they turn on as a dev-dependency) the readers', never into the app.
/// A ZIP64 package declaring `entries` entries: regression input for the readers' tests.
#[cfg(any(test, feature = "test-support"))]
pub fn zip64_entry_count_sample(entries: u64) -> Vec<u8> {
// Local file header (APPNOTE.TXT §4.3.7): version 10, stored, no data, name "a".
let mut f = b"PK\x03\x04".to_vec();
f.extend_from_slice(&[10, 0, 0, 0, 0, 0, 0, 0, 0x21, 0]); // version, flags, method, time, date
f.extend_from_slice(&[0; 12]); // crc, sizes
f.extend_from_slice(&[1, 0, 0, 0]); // name and extra lengths
f.push(b'a');
let cd_at = f.len() as u64;
// Its central header (§4.3.12), 47 bytes.
let mut header = CENTRAL_HEADER.to_vec();
header.extend_from_slice(&[10, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0x21, 0]); // versions, flags, method, time, date
header.extend_from_slice(&[0; 12]); // crc, sizes
header.extend_from_slice(&[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); // name, extra, comment, disk, attributes
header.extend_from_slice(&[0; 4]); // local header offset
header.push(b'a');
let count = entries.min((64 << 20) / header.len() as u64);
for _ in 0..count {
f.extend_from_slice(&header);
}
let cd_len = f.len() as u64 - cd_at;
let eocd64_at = f.len() as u64;
// ZIP64 end of central directory record (§4.3.14), 56 bytes.
f.extend_from_slice(EOCD64);
f.extend_from_slice(&44u64.to_le_bytes()); // size of the rest of the record
f.extend_from_slice(&[45, 0, 45, 0]); // versions
f.extend_from_slice(&[0; 8]); // this disk, disk with the central directory
f.extend_from_slice(&entries.to_le_bytes()); // entries on this disk
f.extend_from_slice(&entries.to_le_bytes()); // entries
f.extend_from_slice(&cd_len.to_le_bytes());
f.extend_from_slice(&cd_at.to_le_bytes());
// Its locator (§4.3.15), 20 bytes.
f.extend_from_slice(EOCD64_LOCATOR);
f.extend_from_slice(&0u32.to_le_bytes());
f.extend_from_slice(&eocd64_at.to_le_bytes());
f.extend_from_slice(&1u32.to_le_bytes());
// End of central directory record (§4.3.16), 22 bytes, each count and offset "see ZIP64".
f.extend_from_slice(EOCD);
f.extend_from_slice(&[0; 4]);
f.extend_from_slice(&[0xFF; 12]);
f.extend_from_slice(&[0; 2]);
f
}
pub use craft_zip::zip64_entry_count_sample;
#[cfg(test)]
mod tests {
use super::{CENTRAL_HEADER, EOCD, check, zip64_entry_count_sample};
use super::{check, zip64_entry_count_sample};
const EOCD: &[u8; 4] = b"PK\x05\x06";
const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02";
/// A plain package's tail: one central header at 0, then an end record declaring `entries`.
fn plain(entries: u16, comment: &[u8]) -> Vec<u8> {
@@ -188,4 +78,140 @@ mod tests {
let f: Vec<u8> = (0..20).flat_map(|_| EOCD.iter().copied().chain(record)).collect();
assert!(check(&f, 20_000).is_err_and(|e| e.contains("end of central directory records")));
}
/// The pre-cutover implementation, kept as a differential oracle.
mod old {
const EOCD: &[u8; 4] = b"PK\x05\x06";
const EOCD64_LOCATOR: &[u8; 4] = b"PK\x06\x07";
const EOCD64: &[u8; 4] = b"PK\x06\x06";
const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02";
/// End records looked at, at most. A package has one; a few more can turn up inside stored data.
const MAX_END_RECORDS: usize = 16;
/// Refuse `bytes` when the end record `zip` would use declares more than `max_entries` entries.
///
/// `zip` searches the file backwards for an end of central directory record. A ZIP64 one points
/// through its locator at the ZIP64 end record, which `zip` looks for from the locator's offset
/// on: every such record there is checked, and as `zip` may fall back to an earlier end record
/// when one doesn't parse, the search goes on. A plain one is used when it declares no entries, or
/// a central directory header lies between its directory offset and itself: it is the last one
/// looked at.
pub fn check(bytes: &[u8], max_entries: u64) -> Result<(), String> {
let u16_at = |at: usize| bytes.get(at..at.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]]));
let u32_at = |at: usize| bytes.get(at..at.checked_add(4)?).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
let u64_at = |at: usize| bytes.get(at..at.checked_add(8)?).and_then(|b| <[u8; 8]>::try_from(b).ok()).map(u64::from_le_bytes);
let refuse = |n: u64| -> Result<(), String> {
if n > max_entries { Err(format!("the package declares {n} entries, more than {max_entries}")) } else { Ok(()) }
};
let mut end = bytes.len();
let mut seen = 0;
while let Some(at) = bytes.get(..end).and_then(|b| b.windows(4).rposition(|w| w == EOCD)) {
end = at;
seen += 1;
if seen > MAX_END_RECORDS {
return Err(format!("more than {MAX_END_RECORDS} end of central directory records"));
}
// `zip` skips a record whose comment runs past the end of the file.
let Some(comment) = u16_at(at + 20) else { continue };
if at + 22 + usize::from(comment) > bytes.len() {
continue;
}
let (Some(on_disk), Some(total), Some(cd_offset)) = (u16_at(at + 8), u16_at(at + 10), u32_at(at + 16)) else { continue };
let zip64 = total == u16::MAX || cd_offset == u32::MAX;
if zip64
&& let Some(locator) = at.checked_sub(20)
&& bytes.get(locator..locator + 4) == Some(EOCD64_LOCATOR.as_slice())
{
let (Some(eocd64), Some(disks)) = (u64_at(locator + 8), u32_at(locator + 16)) else { continue };
let Some(from) = usize::try_from(eocd64).ok().filter(|&f| f < locator) else { continue };
if disks > 1 {
continue;
}
let records = bytes.get(from..locator).unwrap_or_default().windows(4).enumerate().filter(|(_, w)| *w == EOCD64);
for (i, _) in records {
let p = from + i;
refuse(u64_at(p + 24).unwrap_or(0).max(u64_at(p + 32).unwrap_or(0)))?;
}
continue;
}
if total == 0 {
return Ok(());
}
let cd = cd_offset as usize;
if cd < at && bytes.get(cd..at).is_some_and(|b| b.windows(4).any(|w| w == CENTRAL_HEADER)) {
return refuse(u64::from(on_disk.max(total)));
}
}
Ok(())
}
}
fn sentinel_sample() -> Vec<u8> {
// ZIP64 sample whose plain end record claims one entry and a real directory offset but
// sets the directory size to u32::MAX, the ZIP64 marker `zip` honours.
let mut f = zip64_entry_count_sample(30_000);
let n = f.len();
f[n - 14..n - 12].copy_from_slice(&1u16.to_le_bytes());
f[n - 12..n - 10].copy_from_slice(&1u16.to_le_bytes());
f[n - 10..n - 6].copy_from_slice(&u32::MAX.to_le_bytes());
f[n - 6..n - 2].copy_from_slice(&31u32.to_le_bytes());
f
}
fn corpus() -> Vec<Vec<u8>> {
let mut seed = 0x2545_F491_4F6C_DD1Du64;
let mut rnd = |n: usize| -> Vec<u8> {
(0..n)
.map(|_| {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
(seed >> 24) as u8
})
.collect()
};
let mut truncated = zip64_entry_count_sample(10);
truncated.truncate(truncated.len() - 5);
vec![
vec![],
plain(3, b""),
plain(30_000, b"c"),
zip64_entry_count_sample(1),
zip64_entry_count_sample(10),
zip64_entry_count_sample(30_000),
zip64_entry_count_sample(5_000_000),
truncated,
rnd(1),
rnd(100),
rnd(5000),
]
}
#[test]
fn agrees_with_the_old_implementation_except_for_the_sentinel() {
for (i, input) in corpus().iter().enumerate() {
for max in [0u64, 9, 10, 20_000] {
assert_eq!(check(input, max).is_ok(), old::check(input, max).is_ok(), "input {i}, max {max}");
}
}
}
#[test]
fn a_directory_size_sentinel_alone_cannot_bypass_the_count_limit() {
let f = sentinel_sample();
assert!(old::check(&f, 20_000).is_ok(), "the old check missed it");
assert!(check(&f, 20_000).is_err_and(|e| e.contains("30000 entries")));
}
#[test]
fn no_truncation_of_a_zip64_package_panics() {
for f in [zip64_entry_count_sample(10), sentinel_sample()] {
for end in 0..=f.len().min(4096) {
let _ = check(&f[..end], 5);
}
for start in f.len().saturating_sub(300)..f.len() {
let _ = check(&f[start..], 5);
}
}
}
}
@@ -27,6 +27,7 @@ fn cpu_cost(mut f: impl FnMut()) -> f64 {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn many_comments_save_without_quadratic_id_searches() {
for imported in [true, false] {
let small = comments(4_000, imported);
@@ -74,6 +74,7 @@ fn cost_ratio(scale: usize, mut small: impl FnMut(), mut big: impl FnMut()) -> (
/// looked for each paragraph's markers in the whole set of markers, once per paragraph, so a
/// document with 32,000 controls took four seconds to save where it takes a tenth of one now.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn a_document_with_many_inline_content_controls_saves_in_linear_time() {
let scale = 4;
let small = inline_controls(8_000);
@@ -94,6 +95,7 @@ fn a_document_with_many_inline_content_controls_saves_in_linear_time() {
/// The same for the controls written around whole paragraphs: the writer compared every control
/// with every other one to find the ones that clash, so the save was quadratic in their number.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn a_document_with_many_block_content_controls_saves_in_linear_time() {
let scale = 4;
let small = block_controls(8_000);
+4 -2
View File
@@ -1280,10 +1280,12 @@ fn anchor_stacking_order_is_read_and_kept() {
paras(d)[0].objects.iter().filter_map(|o| if let InlineObject::Shape { float, .. } = o { Some(float.z) } else { None }).collect()
};
let d = read_body(&body);
assert_eq!(zs(&d), [251660288, 251657214, 0]);
// The shared reader keeps one more than the file's `relativeHeight` (so a file's 0 is the
// lowest order, not "none") and the writer writes one less.
assert_eq!(zs(&d), [251660289, 251657215, 0]);
let again = wordcraft_docx::read(&wordcraft_docx::write(&d).unwrap()).unwrap();
let z = zs(&again);
assert_eq!(&z[..2], &[251660288, 251657214]);
assert_eq!(&z[..2], &[251660289, 251657215]);
assert!(z[2] > 0, "an object without one gets an order on saving: {z:?}");
}
+2
View File
@@ -152,6 +152,7 @@ fn many_fields_and_huge_attribute_values() {
/// A paragraph with 2,000,000 attributes deflates to ~15 KB. Opening it is refused with a
/// limit error, or the extras are dropped and the text survives (#3702, WC-06).
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn millions_of_attributes_on_one_paragraph_are_capped() {
let attrs: String = (0..2_000_000).map(|i| format!(" a{i}=\"\"")).collect();
let x = format!("<w:document {W_NS}><w:body><w:p{attrs}><w:r><w:t>kept</w:t></w:r></w:p></w:body></w:document>");
@@ -222,6 +223,7 @@ fn malformed_or_hostile_bibliography_never_panics() {
/// A file with a huge number of bibliography sources (duplicates and distinct ones) reads in
/// linear time and keeps at most 10,000 of them.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn hostile_bibliography_source_count_is_capped() {
let doc = format!("<w:document {W_NS}><w:body><w:p><w:r><w:t>body</w:t></w:r></w:p></w:body></w:document>");
let mut sources = String::from(r#"<b:Sources xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" StyleName="MLA">"#);
+1
View File
@@ -871,6 +871,7 @@ fn citation_source_lookups_keep_first_duplicates_case_and_style_order() {
#[cfg(not(target_arch = "wasm32"))]
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn citation_refresh_source_lookup_work_scales_with_document_size() {
fn doc(n: usize) -> Session {
let mut p = Paragraph::new();
+24 -7
View File
@@ -20,9 +20,12 @@ pub const SNAP: f32 = 12.0;
/// Largest offset a routed connector gets, points.
const MAX_OFFSET: f32 = 4000.0;
/// `shape.connect` is offered for what it runs on: a floating connector or line in the body text.
fn has_connector(s: &Session) -> Option<&'static str> {
match super::objects::selected(s) {
Some((_, InlineObject::Shape { kind, .. })) if kind.is_connector() => None,
Some((pos, InlineObject::Shape { kind, float, .. })) if kind.is_open() => {
(pos.story != StoryRef::Body || float.wrap == Wrap::Inline).then_some("only floating connectors in the body text can be glued")
}
_ => Some("select a connector first"),
}
}
@@ -31,7 +34,7 @@ pub fn specs() -> Vec<CommandSpec> {
vec![
CommandSpec::new("shape.connect", "Connect Shapes", "Shape Format › Insert Shapes", connect)
.params(
r#"{"start"?: end, "end"?: end} end: {"path": [..], "off": n, "site"?: n} (glue to that body shape's connection site; nearest one when omitted) | {"x": pt, "y": pt, "page"?: n} (drag the end there: it glues to a shape's site within 12 pt, else stays free there) | null (unglue)"#,
r#"{"start"?: end, "end"?: end} end: {"path": [..], "off": n, "site"?: n} (glue to that body shape's connection site; nearest one when omitted) | {"x": pt, "y": pt, "page"?: n} (drag the end there: it glues to a shape's site within 12 pt, else stays free there) | null (unglue). A plain line's ends only move to points: lines don't glue"#,
)
.when(has_connector),
]
@@ -186,13 +189,23 @@ fn ensure_id(s: &mut Session, pos: &Pos) -> Result<u32, CmdError> {
Ok(id)
}
/// `shape.connect`: glue, move or unglue the selected connector's ends.
/// A floating line or connector's start and end on its page: its frame's ends (flips
/// included), turned with the frame by `deg` about its centre.
pub fn page_ends(r: wordcraft_geom::Rect, flip_h: bool, flip_v: bool, deg: f32) -> ((f32, f32), (f32, f32)) {
let (a, b) = ends(r.x, r.y, r.w, r.h, flip_h, flip_v);
let turn = wordcraft_geom::Spin::new(deg, false, false);
let (cx, cy) = (r.x + r.w / 2.0, r.y + r.h / 2.0);
(turn.apply(cx, cy, a.0, a.1), turn.apply(cx, cy, b.0, b.1))
}
/// `shape.connect`: glue, move or unglue the selected connector's ends, or move a line's.
fn connect(s: &mut Session, v: &Value) -> CmdResult {
let (pos, obj) = super::objects::selected(s).ok_or_else(|| CmdError::Disabled("select a connector first".into()))?;
let InlineObject::Shape { kind, float, .. } = &obj else { return Err(CmdError::Disabled("select a connector first".into())) };
if !kind.is_connector() {
if !kind.is_open() {
return Err(CmdError::Disabled("select a connector first".into()));
}
let line = !kind.is_connector();
if pos.story != StoryRef::Body || float.wrap == Wrap::Inline {
return Err(CmdError::Disabled("only floating connectors in the body text can be glued".into()));
}
@@ -200,7 +213,7 @@ fn connect(s: &mut Session, v: &Value) -> CmdResult {
let layout = s.layout();
let hit = layout.object(&pos, s.page_hint).ok_or_else(|| CmdError::Failed("the connector isn't laid out".into()))?;
let r = hit.rect;
let (mut p0, mut p1) = ends(r.x, r.y, r.w, r.h, fh, fv);
let (mut p0, mut p1) = page_ends(r, fh, fv, hit.spin.deg);
let mut glue: [Option<Option<ConnEnd>>; 2] = [None, None];
for (i, key) in ["start", "end"].into_iter().enumerate() {
let Some(e) = v.get(key) else { continue };
@@ -209,6 +222,9 @@ fn connect(s: &mut Session, v: &Value) -> CmdResult {
continue;
}
let here = if i == 0 { p0 } else { p1 };
if line && e.get("path").is_some() {
return Err(CmdError::Params(format!("`{key}`: a line's ends move to points; lines don't glue to shapes")));
}
let (target, site) = if e.get("path").is_some() {
let at = body_pos(e).ok_or_else(|| CmdError::Params(format!("`{key}`: no shape there")))?;
let th = layout
@@ -234,9 +250,9 @@ fn connect(s: &mut Session, v: &Value) -> CmdResult {
if page != hit.page {
return Err(CmdError::Params("a connector's ends stay on its page".into()));
}
// The nearest site of a shape (not a connector) within reach.
// The nearest site of a shape (not a connector) within reach; a line's end stays free.
let mut best: Option<(Pos, u32, f32)> = None;
if let Some(pg) = layout.pages.get(page) {
if !line && let Some(pg) = layout.pages.get(page) {
for it in &pg.items {
let wordcraft_layout::Placed::Object { rect, spin, story: StoryRef::Body, path, off, .. } = it else { continue };
let at = Pos { story: StoryRef::Body, path: path.clone(), off: *off };
@@ -316,6 +332,7 @@ mod tests {
/// Drawing commands must not search every page again for each glued connector.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn rerouting_many_pages_does_not_rescan_all_objects_per_connector() {
use wordcraft_doc::{Document, Paragraph, para_block};
let mut doc = Document::new();
+88 -2
View File
@@ -651,6 +651,77 @@ fn info(s: &mut Session, _: &Value) -> CmdResult {
}))
}
/// Most fields [`story_fields`] lists for one story, and how deep it goes into nested tables.
const MAX_STORY_FIELDS: usize = 200;
const MAX_STORY_DEPTH: usize = 8;
/// The fields of a story's paragraphs, tables included: the code, the result stored with it, and
/// whether the page shows a value of its own (#4467). A header or footer's text carries the
/// stored result of a page field (`1` for a PAGE, the page count when it was inserted for a
/// NUMPAGES); each page shows its own, as the layout draws it. A STYLEREF counts too: the layout
/// looks its text up from the page it lands on. False when a limit cut the list short.
fn story_fields(blocks: &wordcraft_doc::Blocks, depth: usize, out: &mut Vec<Value>) -> bool {
if depth > MAX_STORY_DEPTH {
return false;
}
for b in blocks {
match &**b {
Block::Para(p) => {
for o in &p.objects {
if let wordcraft_doc::para::InlineObject::Field { instr, result, .. } = o {
if out.len() >= MAX_STORY_FIELDS {
return false;
}
out.push(field_json(instr, result));
}
}
}
Block::Table(t) => {
for cell in t.rows.iter().flat_map(|r| &r.cells) {
if !story_fields(&cell.blocks, depth + 1, out) {
return false;
}
}
}
}
}
true
}
/// The range fields of `story` (a `FieldStart` … `FieldEnd` pair: what the DOCX reader makes of a
/// field whose result has tracked changes of its own or spans paragraphs, and of every TOC),
/// listed as [`story_fields`] lists inline ones, after them; the result is the text between the
/// markers as it is, at most [`MAX_RANGE_RESULT`] characters of it (`resultTruncated` when it is
/// cut: a bibliography or a long TOC). False when a limit cut the list short.
fn range_fields(doc: &Document, story: StoryRef, out: &mut Vec<Value>) -> bool {
for r in doc.field_ranges(story) {
if out.len() >= MAX_STORY_FIELDS {
return false;
}
let from = wordcraft_doc::Pos { off: r.start.off.saturating_add(wordcraft_doc::para::OBJ.len_utf8()), ..r.start.clone() };
let text = doc.copy_range(&from, &r.end).plain_text();
let mut chars = text.chars().filter(|c| *c != wordcraft_doc::para::OBJ);
let result: String = chars.by_ref().take(MAX_RANGE_RESULT).collect();
let mut field = field_json(&r.instr, &result);
if chars.next().is_some() {
field["resultTruncated"] = json!(true);
}
out.push(field);
}
true
}
/// Longest result `document.inspect` gives a range field, in characters.
const MAX_RANGE_RESULT: usize = 2000;
/// A field as `document.inspect` lists it: its code, its stored result, and whether each page
/// shows a value of its own.
fn field_json(instr: &str, result: &str) -> Value {
let name = wordcraft_layout::fields::field_name(instr);
let per_page = matches!(name.as_str(), "PAGE" | "NUMPAGES" | "SECTIONPAGES" | "SECTION" | "PAGEREF" | "STYLEREF");
json!({"code": instr.trim(), "result": result, "perPage": per_page})
}
/// Structure for agents: blocks with text and formatting summary, selection, stats.
fn inspect(s: &mut Session, v: &Value) -> CmdResult {
let with_text = p::bool(v, "text").unwrap_or(true);
@@ -702,8 +773,23 @@ fn inspect(s: &mut Session, v: &Value) -> CmdResult {
}
}
let l = s.layout();
let parts: Vec<Value> =
s.doc.parts.iter().map(|(id, p)| json!({"id": id, "kind": p.kind, "text": s.doc.plain_text(StoryRef::Part(*id))})).collect();
let parts: Vec<Value> = s
.doc
.parts
.iter()
.map(|(id, p)| {
let mut o = json!({"id": id, "kind": p.kind, "text": s.doc.plain_text(StoryRef::Part(*id))});
let mut fields = Vec::new();
let complete = story_fields(&p.blocks, 0, &mut fields) && range_fields(&s.doc, StoryRef::Part(*id), &mut fields);
if !fields.is_empty() {
o["fields"] = json!(fields);
}
if !complete {
o["fieldsTruncated"] = json!(true);
}
o
})
.collect();
Ok(json!({
"blocks": blocks,
"parts": parts,
+15 -2
View File
@@ -24,7 +24,7 @@ pub fn specs() -> Vec<CommandSpec> {
CommandSpec::new("font.remove", "Remove Added Font", "Home › Font", remove).params(r#"{"family": string}"#).pure(),
CommandSpec::new("font.loadSystem", "Load System Installed Fonts…", "Home › Font", load_system).pure(),
CommandSpec::new("font.missing", "Missing Fonts", "Status Bar", missing)
.params(r#"{} → {"missing": [{"font": string, "shownAs": [string] (the families drawn instead), "metricCompatible": bool (its substitute measures like the font)}]} — the text the current view shows"#)
.params(r#"{} → {"missing": [{"font": string, "shownAs": [string] (the families drawn instead), "metricCompatible": bool (its substitute measures like the font), "download"?: {"name": string, "publisher": string, "url": string, "restricted": bool (the publisher limits who may use it: check its licence terms)} (its publisher's download page, when it offers the font at no charge)}]} — the text the current view shows"#)
.pure(),
]
}
@@ -136,6 +136,9 @@ pub struct MissingFont {
/// Fallback faces for characters the substitute lacks don't count: the font itself might
/// lack them too (an emoji in Calibri text).
pub metric_compatible: bool,
/// Its publisher's download page, when the publisher offers the font at no charge
/// ([`wordcraft_fonts::download`]): installing it shows the document as it was written.
pub download: Option<&'static wordcraft_fonts::download::FontDownload>,
}
/// The family text set in `font` is drawn in when `font` isn't installed (None when it is), as
@@ -174,6 +177,7 @@ pub fn missing_fonts(doc: &Document, layout: &DocLayout) -> Vec<MissingFont> {
// Judged by the name layout looked up, spaces and all: "Arial " isn't Arial to it, so
// its text is in a substitute even where Arial is installed.
metric_compatible: shown_as(doc, &recorded).is_some_and(|s| !warns(font, &s)),
download: wordcraft_fonts::download::download_for(font),
});
if !m.shown_as.contains(&family) {
m.shown_as.push(family);
@@ -216,7 +220,16 @@ fn missing(s: &mut Session, _: &Value) -> CmdResult {
(_, n) if n > NAMED => format!("Not installed: {named} and {} more", n - NAMED),
_ => format!("Not installed: {named}"),
};
let fonts: Vec<Value> = found.iter().map(|f| json!({"font": f.font, "shownAs": f.shown_as, "metricCompatible": f.metric_compatible})).collect();
let fonts: Vec<Value> = found
.iter()
.map(|f| {
let mut font = json!({"font": f.font, "shownAs": f.shown_as, "metricCompatible": f.metric_compatible});
if let Some(d) = f.download {
font["download"] = json!({"name": d.name, "publisher": d.publisher, "url": d.url, "restricted": d.restricted});
}
font
})
.collect();
Ok(json!({"missing": fonts}))
}
+98
View File
@@ -157,6 +157,104 @@ fn default_level(i: usize) -> Level {
levels_for(ListKind::Numbered).into_iter().nth(i).unwrap_or_default()
}
/// Define New Bullet (#2786): the selected paragraphs go into a list of their own whose bullet is
/// `char`, drawn in `font` (with `size`, `bold`, `italic` and `color` when given) and aligned by
/// `align` at every level they use. A list of its own, so
/// no other list using the same character changes, and it never toggles a bullet off the way
/// `para.bullets` does. Each paragraph keeps its level (level 1 when it wasn't in a list).
pub fn define_bullet(s: &mut Session, v: &Value) -> CmdResult {
let ch = match v.get("char") {
None => '★',
Some(c) => {
let c = c.as_str().ok_or_else(|| CmdError::Params("char must be one character".into()))?;
let mut it = c.chars();
match (it.next(), it.next()) {
(Some(c), None) if !c.is_control() => c,
_ => return Err(CmdError::Params("char must be one character".into())),
}
}
};
let font = match v.get("font") {
None => None,
Some(f) => {
let f = f.as_str().map(str::trim).ok_or_else(|| CmdError::Params("font must be a family name".into()))?;
if f.len() > 128 {
return Err(CmdError::Params("font name too long".into()));
}
(!f.is_empty()).then(|| f.to_string())
}
};
let size = match v.get("size") {
None => None,
Some(n) => match n.as_f64() {
Some(pt) if (1.0..=1638.0).contains(&pt) => Some(pt as f32),
_ => return Err(CmdError::Params("size must be 1 to 1638 points".into())),
},
};
let flag = |k: &str| -> Result<Option<bool>, CmdError> {
match v.get(k) {
None => Ok(None),
Some(b) => b.as_bool().map(Some).ok_or_else(|| CmdError::Params(format!("{k} must be true or false"))),
}
};
let (bold, italic) = (flag("bold")?, flag("italic")?);
let color = match v.get("color") {
None => None,
Some(c) => {
let c = c.as_str().ok_or_else(|| CmdError::Params("color must be RRGGBB or auto".into()))?;
Some(TextColor::parse(c).ok_or_else(|| CmdError::Params("color must be RRGGBB or auto".into()))?)
}
};
let align = match p::str(v, "align") {
None if v.get("align").is_none() => None,
Some("left") => Some(Align::Left),
Some("center") => Some(Align::Center),
Some("right") => Some(Align::Right),
_ => return Err(CmdError::Params("align must be left, center or right".into())),
};
// A paragraph's level, from its own numbering or its style's (List Bullet 2 numbers its
// paragraphs at level 2 without any of their own).
let styles = s.doc.styles.clone();
let level_of = |pp: &wordcraft_doc::props::ParaProps| styles.resolve_para(pp).numbering.filter(|n| n.num != 0).map_or(0, |n| n.level);
let story = s.sel.focus.story;
let used: std::collections::BTreeSet<u8> =
super::selected_paths(s).iter().filter_map(|p| s.doc.para(story, p)).map(|x| level_of(&x.props)).collect();
let mut levels = levels_for(ListKind::BulletChar(ch));
for l in used {
if let Some(lv) = levels.get_mut(usize::from(l)) {
lv.text = ch.to_string();
lv.chr.font = font.clone();
if size.is_some() {
lv.chr.size = size;
}
if bold.is_some() {
lv.chr.bold = bold;
}
if italic.is_some() {
lv.chr.italic = italic;
}
if color.is_some() {
lv.chr.color = color;
}
if let Some(a) = align {
lv.align = a;
}
}
}
let num = Arc::make_mut(&mut s.doc.numbering).try_add_levels(None, levels)?;
super::para::fmt(s, &|pp| {
let level = level_of(pp);
pp.numbering = Some(NumRef { num, level });
pp.indent_left = None;
pp.indent_first = None;
pp.indent_left_chars = None;
pp.indent_first_chars = None;
if pp.style.is_none() || pp.style.as_deref() == Some("Normal") {
pp.style = Some("ListParagraph".into());
}
})
}
fn get(s: &mut Session, v: &Value) -> CmdResult {
let num =
p::u64(v, "num").map(|n| n.min(u32::MAX as u64) as u32).or_else(|| caret_list(s)).ok_or_else(|| CmdError::Failed("not in a list".into()))?;
+3 -2
View File
@@ -21,8 +21,9 @@ pub(super) fn bounds(s: &mut Session, v: &Value) -> CmdResult {
}
let (w0, h0) = obj_size(&obj);
let min = min_size(&obj);
let w = p::f32(v, "width").unwrap_or(w0).clamp(min, MAX_OFFSET);
let h = p::f32(v, "height").unwrap_or(h0).clamp(min, MAX_OFFSET);
// A size not given stays as it is: a move must not give a level line (0 high) a height.
let w = p::f32(v, "width").map_or(w0, |w| w.clamp(min, MAX_OFFSET));
let h = p::f32(v, "height").map_or(h0, |h| h.clamp(min, MAX_OFFSET));
if (w, h) != (w0, h0) {
with_obj(s, |o| o.set_size(w, h))?;
}
+105
View File
@@ -152,6 +152,111 @@ fn click_resize_and_drag(insert: &str, params: Value) {
assert_eq!((object_area(&mut s).rect.x, object_area(&mut s).rect.y), (250.0, 300.0), "{insert}");
}
/// Moving keeps a size it isn't given: a level line (0 high, as Word files store a rule)
/// moved by its body stays level, though a height given to it is still at least the minimum.
#[test]
fn moving_a_level_line_keeps_it_level() {
let line = InlineObject::Shape {
kind: wordcraft_doc::para::ShapeKind::Line,
w: 200.0,
h: 0.0,
fill: None,
stroke: None,
stroke_width: 1.0,
float: wordcraft_doc::para::Float { wrap: Wrap::InFrontOfText, x: 50.0, y: 100.0, ..Default::default() },
story: None,
freeform: None,
effects: Default::default(),
extra: Default::default(),
};
let mut s = Session::new(wordcraft_doc::Document::new());
s.doc.insert_object(&Pos::body(0, 0), line, &Default::default()).unwrap();
s.run("select.range", &json!({"anchor": Pos::body(0, 0), "focus": Pos::body(0, wordcraft_doc::para::OBJ.len_utf8())})).unwrap();
s.run("arrange.bounds", &json!({"page": 0, "x": 150, "y": 300})).unwrap();
let o = s.layout().object(&Pos::body(0, 0), 0).unwrap();
assert_eq!((o.rect.x, o.rect.y, o.rect.w, o.rect.h), (150.0, 300.0, 200.0, 0.0));
s.run("arrange.bounds", &json!({"height": 0})).unwrap();
assert_eq!(s.layout().object(&Pos::body(0, 0), 0).unwrap().rect.h, min_size(&selected(&s).unwrap().1));
}
/// Connect Shapes is offered for the lines and connectors it moves: floating ones in the body
/// text, not one In Line with Text or in a header.
#[test]
fn connect_is_offered_only_for_floating_lines_in_the_body() {
use wordcraft_doc::para::ShapeKind;
let shape = |kind, wrap| InlineObject::Shape {
kind,
w: 200.0,
h: 0.0,
fill: None,
stroke: None,
stroke_width: 1.0,
float: wordcraft_doc::para::Float { wrap, x: 50.0, y: 100.0, ..Default::default() },
story: None,
freeform: None,
effects: Default::default(),
extra: Default::default(),
};
for kind in [ShapeKind::Line, ShapeKind::StraightConnector] {
for (wrap, offered) in [(Wrap::InFrontOfText, true), (Wrap::Inline, false)] {
let mut s = Session::new(wordcraft_doc::Document::new());
s.doc.insert_object(&Pos::body(0, 0), shape(kind, wrap), &Default::default()).unwrap();
s.run("select.range", &json!({"anchor": Pos::body(0, 0), "focus": Pos::body(0, wordcraft_doc::para::OBJ.len_utf8())})).unwrap();
assert_eq!(s.why_disabled("shape.connect").is_none(), offered, "{kind:?} {wrap:?}");
assert_eq!(s.run("shape.connect", &json!({"end": {"x": 300.0, "y": 150.0}})).is_ok(), offered, "{kind:?} {wrap:?}: runs when offered");
}
}
}
/// Moving one end of a slanted, flipped line leaves the other where the page shows it: the ends
/// `shape.connect` reads and writes are the drawn ones (checked on the rendered page).
#[test]
fn moving_an_end_of_a_flipped_slanted_line_keeps_the_other() {
use wordcraft_doc::para::ShapeKind;
// Flipped vertically, a line in the frame (300, 600) 100 × 100 runs from its bottom-left
// (300, 700) to its top-right (400, 600), as in Word.
let line = InlineObject::Shape {
kind: ShapeKind::Line,
w: 100.0,
h: 100.0,
fill: None,
stroke: Some(wordcraft_doc::props::Rgb::BLACK),
stroke_width: 3.0,
float: wordcraft_doc::para::Float {
wrap: Wrap::InFrontOfText,
h_rel: wordcraft_doc::para::Anchor::Page,
v_rel: wordcraft_doc::para::Anchor::Page,
x: 300.0,
y: 600.0,
flip_v: true,
..Default::default()
},
story: None,
freeform: None,
effects: Default::default(),
extra: Default::default(),
};
let mut s = Session::new(wordcraft_doc::Document::new());
s.doc.insert_object(&Pos::body(0, 0), line, &Default::default()).unwrap();
s.run("select.range", &json!({"anchor": Pos::body(0, 0), "focus": Pos::body(0, wordcraft_doc::para::OBJ.len_utf8())})).unwrap();
let inked = |s: &Session, x: f32, y: f32| {
let l = s.export_layout();
let r = wordcraft_render::render_page(&s.doc, &l.pages[0], 1.0, &Default::default());
let dark = |px: i64, py: i64| {
let i = ((py * i64::from(r.width) + px) * 4) as usize;
r.pixels.get(i..i + 3).is_some_and(|p| p.iter().all(|&c| c < 128))
};
(-3..=3).any(|dx| (-3..=3).any(|dy| dark(x as i64 + dx, y as i64 + dy)))
};
assert!(inked(&s, 302.0, 698.0) && inked(&s, 398.0, 602.0), "drawn from bottom-left to top-right");
assert!(!inked(&s, 302.0, 602.0), "not from the top-left");
// Its end goes to (450, 650): the start stays at (300, 700).
s.run("shape.connect", &json!({"end": {"x": 450.0, "y": 650.0}})).unwrap();
assert!(inked(&s, 303.0, 699.0), "the start stays where it was drawn");
assert!(inked(&s, 447.0, 651.0), "the end is where it was dragged");
assert!(!inked(&s, 398.0, 602.0), "the end left its old place");
}
/// Ctrl+drag: a copy lands where dropped and is selected; the original stays, the clipboard
/// is untouched, and it is one undo step.
#[test]
+1
View File
@@ -799,6 +799,7 @@ fn deleting_a_comment_in_a_parent_cycle_ends() {
/// A very long reply chain (as a hostile file could hold) is deleted in one pass over the
/// comments, not one pass per reply: 50,000 replies would otherwise freeze the app.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn deleting_a_long_reply_chain_stays_fast() {
let mut s = Session::new(Document::new());
s.view.proofing = false;
+3 -4
View File
@@ -316,10 +316,9 @@ pub fn specs() -> Vec<CommandSpec> {
sel_result(s)
})
.params(r#"{"value": n}"#),
CommandSpec::new("para.defineBullet", "Define New Bullet", "Home › Paragraph › Bullets", |s, v| {
s.run("para.bullets", &json!({"kind": p::str(v, "char").unwrap_or("★")}))
})
.params(r#"{"char": string}"#),
CommandSpec::new("para.defineBullet", "Define New Bullet", "Home › Paragraph › Bullets", crate::cmd::lists::define_bullet).params(
r#"{"char"?: one character (default ★), "font"?: family (the bullet's own font; "" = the text's), "size"?: pt, "bold"?: bool, "italic"?: bool, "color"?: "RRGGBB" | "auto", "align"?: "left|center|right"} (the selected paragraphs get a list of their own; each keeps its level)"#,
),
CommandSpec::new("para.defineNumber", "Define New Number Format", "Home › Paragraph › Numbering", |s, v| {
let fmt = wordcraft_doc::section::NumFormat::from_ooxml(p::str(v, "format").unwrap_or("decimal"));
let text = p::str(v, "text").unwrap_or("%1.").to_string();
+2 -2
View File
@@ -47,8 +47,8 @@ fn theme(s: &Session) -> Vec<Rgb> {
}
/// The text box story the selection formats as a whole: the selected shape's, or the one the
/// caret is in.
fn box_story(s: &Session) -> Option<u32> {
/// caret is in. The ribbon reads the current effects from the same place.
pub fn box_story(s: &Session) -> Option<u32> {
if let Some((_, InlineObject::Shape { story: Some(id), .. })) = super::objects::object_selection(s) {
return Some(*id);
}
+1
View File
@@ -102,6 +102,7 @@ fn tidy_matches_replacing_until_nothing_changes() {
/// Nested empty brackets are removed in one pass: 40,000 pairs took seconds when each pass
/// removed one level (#3680).
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn tidy_is_linear_on_nested_brackets() {
let n = 40_000;
let started = std::time::Instant::now();
+201 -2
View File
@@ -1052,6 +1052,7 @@ fn rep_mode(text: &str, q: &str, with: &str, extra: serde_json::Value) -> String
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn hostile_find_patterns_are_bounded() {
let mut s = s();
run(&mut s, "document.setText", json!({"text": &"a".repeat(5000)}));
@@ -8548,6 +8549,83 @@ fn colour_commands_take_any_custom_hex() {
assert_eq!(s.doc.settings.page_color, wordcraft_doc::Rgb::parse("123ABC"));
}
/// #2786: Define New Bullet puts the selection in a list of its own: the character, its font and
/// its alignment at the levels the paragraphs use, each paragraph keeping its level. A list that
/// already uses the same character is left alone, defining again never toggles the bullet off,
/// it survives a DOCX round trip, and bad params are errors that change nothing.
#[test]
fn define_bullet_gives_the_selection_a_bullet_of_its_own() {
use wordcraft_doc::props::Align;
let mut s = Session::new(wordcraft_doc::Document::from_text("One\nTwo\nThree"));
let num_at = |s: &Session, i: usize| s.doc.para_at(&Pos::body(i, 0)).unwrap().props.numbering.unwrap();
// "Three" already has a ♦ list, defined the ordinary way.
s.sel = crate::Selection::caret(Pos::body(2, 0));
run(&mut s, "para.bullets", json!({"kind": "♦"}));
let other = num_at(&s, 2).num;
let other_level = s.doc.numbering.level(other, 0).unwrap().clone();
// "Two" sits at level 2 of a bulleted list before the definition.
s.sel = crate::Selection::caret(Pos::body(1, 0));
run(&mut s, "para.bullets", json!({}));
run(&mut s, "para.listLevel", json!({"level": 1}));
s.sel = crate::Selection { anchor: Pos::body(0, 0), focus: Pos::body(1, 3) };
run(&mut s, "para.defineBullet", json!({"char": "♦", "font": "Segoe UI Symbol", "align": "center"}));
let (one, two) = (num_at(&s, 0), num_at(&s, 1));
assert_eq!(one.num, two.num, "one list for the selection");
assert_ne!(one.num, other, "not the existing ♦ list");
assert_eq!((one.level, two.level), (0, 1), "each paragraph keeps its level");
for lvl in [0, 1] {
let l = s.doc.numbering.level(one.num, lvl).unwrap();
assert_eq!((l.text.as_str(), l.chr.font.as_deref(), l.align), ("♦", Some("Segoe UI Symbol"), Align::Center), "level {lvl}");
}
assert_eq!(s.doc.numbering.level(other, 0).unwrap(), &other_level, "the other ♦ list is unchanged");
// Defining the same bullet again keeps it (para.bullets would toggle it off).
run(&mut s, "para.defineBullet", json!({"char": "♦"}));
assert_eq!(s.doc.numbering.level(num_at(&s, 0).num, 0).unwrap().text, "♦");
// DOCX keeps the character, its font and the alignment.
run(&mut s, "para.defineBullet", json!({"char": "✓", "font": "Segoe UI Symbol", "align": "right", "size": 14, "bold": true, "color": "C00000"}));
let back = wordcraft_docx::read(&wordcraft_docx::write(&s.doc).unwrap()).unwrap();
let n = back.para_at(&Pos::body(0, 0)).unwrap().props.numbering.unwrap();
let l = back.numbering.level(n.num, 0).unwrap();
assert_eq!((l.text.as_str(), l.chr.font.as_deref(), l.align), ("✓", Some("Segoe UI Symbol"), Align::Right));
// The Font dialog's size, bold and colour reach the bullet too.
assert_eq!((l.chr.size, l.chr.bold), (Some(14.0), Some(true)));
assert_eq!(l.chr.color, wordcraft_doc::props::TextColor::parse("C00000"));
// A paragraph numbered only by its style keeps the style's level (review of #2860).
let mut s2 = Session::new(wordcraft_doc::Document::from_text("Styled"));
let styled = Arc::make_mut(&mut s2.doc.numbering).try_add_list(wordcraft_doc::ListKind::Bullet).unwrap();
Arc::make_mut(&mut s2.doc.styles).get_mut("ListParagraph").unwrap().para.numbering = Some(wordcraft_doc::props::NumRef { num: styled, level: 1 });
s2.doc.para_mut(wordcraft_doc::StoryRef::Body, &wordcraft_doc::Path::top(0)).unwrap().props.style = Some("ListParagraph".into());
assert!(s2.doc.para_at(&Pos::body(0, 0)).unwrap().props.numbering.is_none(), "no numbering of its own");
s2.sel = crate::Selection::caret(Pos::body(0, 0));
run(&mut s2, "para.defineBullet", json!({"char": "♦"}));
let n = s2.doc.para_at(&Pos::body(0, 0)).unwrap().props.numbering.unwrap();
assert_eq!(n.level, 1, "the style's level is kept");
assert_eq!(s2.doc.numbering.level(n.num, 1).unwrap().text, "♦");
// No params: ★, as before.
s.sel = crate::Selection::caret(Pos::body(2, 0));
run(&mut s, "para.defineBullet", json!({}));
assert_eq!(s.doc.numbering.level(num_at(&s, 2).num, 0).unwrap().text, "★");
let doc = s.doc.clone();
let long = "x".repeat(200);
for junk in [
json!({"char": ""}),
json!({"char": "ab"}),
json!({"char": 5}),
json!({"char": "\u{7}"}),
json!({"char": "•", "align": "middle"}),
json!({"char": "•", "align": 1}),
json!({"char": "•", "font": 3}),
json!({"char": "•", "font": long}),
json!({"char": "•", "size": 0}),
json!({"char": "•", "size": "big"}),
json!({"char": "•", "bold": "yes"}),
json!({"char": "•", "color": "purple"}),
] {
assert!(s.run("para.defineBullet", &junk).is_err(), "{junk}");
}
assert_eq!(s.doc, doc);
}
/// #4685: Home › Paragraph › Shading on text selected inside a paragraph shades that text, as in
/// Word, so No Color takes off a text shading a file gave it (an orange "highlight" from a .docx is
/// `w:shd`, which Text Highlight Color › No Color leaves, in Word too). A caret or a whole
@@ -10329,8 +10407,9 @@ fn review_language_validates_tags_and_marks_text_unchecked() {
assert_eq!(run(&mut s, "format.state", json!({}))["lang"], "pl");
run(&mut s, "review.language", json!({}));
assert_eq!(run(&mut s, "format.state", json!({}))["lang"], "en-US");
// A language without proofing tools: no marks.
run(&mut s, "review.language", json!({"lang": "fr-FR"}));
// A language without proofing tools: no marks. `zxx` (no linguistic content) never has
// them; a real language such as fr-FR does wherever its system Hunspell dictionary is installed.
run(&mut s, "review.language", json!({"lang": "zxx"}));
assert_eq!(run(&mut s, "review.issues", json!({})), json!([]));
}
@@ -12083,6 +12162,7 @@ fn inspect_document_removes_the_note_of_a_hidden_reference() {
/// #1344: many hidden pieces (index entries are hidden text) are removed quickly, and the
/// found list is capped.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn inspect_document_removes_thousands_of_hidden_pieces() {
use wordcraft_doc::props::CharProps;
use wordcraft_doc::{Paragraph, para_block};
@@ -13104,6 +13184,125 @@ fn scale_font_sizes_reaches_char_table_and_cs_sizes_and_unset_default() {
assert_eq!(grid.regions.get(&TablePart::Band2Horz).unwrap().chr.size, Some(10.0));
}
/// #4467: `document.inspect` lists a footer's fields and marks the ones each page shows its own
/// value of, so a caller knows "1 of 1" is a stored result and not what page 2 shows.
#[test]
fn inspect_marks_page_fields_in_headers_and_footers() {
let mut s = s();
let id = run(&mut s, "insert.pageNumber", json!({"format": "x of y"}))["story"].as_u64().unwrap();
let doc = run(&mut s, "document.inspect", json!({}));
let part = doc["parts"].as_array().unwrap().iter().find(|p| p["id"] == id).unwrap().clone();
assert_eq!(part["text"], "Page 1 of 1");
assert_eq!(part["fields"], json!([{"code": "PAGE", "result": "1", "perPage": true}, {"code": "NUMPAGES", "result": "1", "perPage": true}]));
}
/// #4467 review: a STYLEREF in a footer's table cell is listed as per page too (the layout looks
/// it up on each page), and a part with more fields than `document.inspect` lists says so.
#[test]
fn inspect_lists_cell_fields_and_marks_a_cut_list() {
use wordcraft_doc::{Block, CharProps, InlineObject, Paragraph, Table, para_block};
let field =
|instr: &str| InlineObject::Field { instr: instr.into(), result: "x".into(), locked: false, dirty: false, code: false, form: String::new() };
let mut s = s();
let id = run(&mut s, "insert.pageNumber", json!({}))["story"].as_u64().unwrap();
let key = u32::try_from(id).unwrap();
let mut cell = Paragraph::new();
cell.insert_object(0, field("STYLEREF \"Heading 1\""), &CharProps::default()).unwrap();
let mut table = Table::new(1, 1, 200.0);
table.rows[0].cells[0].blocks = vec![para_block(cell)];
s.doc.parts.get_mut(&key).unwrap().blocks.push(std::sync::Arc::new(Block::Table(table)));
let part = |s: &mut Session| {
let doc = run(s, "document.inspect", json!({}));
doc["parts"].as_array().unwrap().iter().find(|p| p["id"] == id).unwrap().clone()
};
let p = part(&mut s);
assert_eq!(p["fields"][1], json!({"code": "STYLEREF \"Heading 1\"", "result": "x", "perPage": true}));
assert!(p.get("fieldsTruncated").is_none(), "{p}");
let mut many = Paragraph::new();
for _ in 0..250 {
many.insert_object(0, field("AUTHOR"), &CharProps::default()).unwrap();
}
s.doc.parts.get_mut(&key).unwrap().blocks.push(para_block(many));
let p = part(&mut s);
assert_eq!(p["fields"].as_array().unwrap().len(), 200);
assert_eq!(p["fieldsTruncated"], true);
}
/// #4467 review: a field kept as a range (`FieldStart` … `FieldEnd`, as the DOCX reader keeps a
/// PAGE whose result has tracked changes, or a TOC spanning paragraphs) is listed too, its result
/// the text between the markers.
#[test]
fn inspect_lists_range_fields() {
use wordcraft_doc::{CharProps, InlineObject, Paragraph, para_block};
let start = |instr: &str| InlineObject::FieldStart { instr: instr.into(), locked: false, dirty: false };
let mut s = s();
let id = run(&mut s, "insert.pageNumber", json!({}))["story"].as_u64().unwrap();
let key = u32::try_from(id).unwrap();
let c = CharProps::default();
let mut page = Paragraph::new();
page.insert_object(0, start("PAGE"), &c).unwrap();
page.insert_text(page.len(), "7 ", &c).unwrap();
page.insert_object(page.len(), InlineObject::FieldEnd, &c).unwrap();
let mut first = Paragraph::new();
first.insert_object(0, start("TOC \\o \"1-3\""), &c).unwrap();
first.insert_text(first.len(), "One", &c).unwrap();
let mut second = Paragraph::with_text("Two", c.clone());
second.insert_object(second.len(), InlineObject::FieldEnd, &c).unwrap();
// A result longer than `document.inspect` gives: cut, and marked.
let mut long = Paragraph::new();
long.insert_object(0, start("BIBLIOGRAPHY"), &c).unwrap();
long.insert_text(long.len(), &"x".repeat(2500), &c).unwrap();
long.insert_object(long.len(), InlineObject::FieldEnd, &c).unwrap();
let blocks = &mut s.doc.parts.get_mut(&key).unwrap().blocks;
blocks.extend([para_block(page), para_block(first), para_block(second), para_block(long)]);
let doc = run(&mut s, "document.inspect", json!({}));
let part = doc["parts"].as_array().unwrap().iter().find(|p| p["id"] == id).unwrap().clone();
let fields = part["fields"].as_array().unwrap();
assert!(fields.contains(&json!({"code": "PAGE", "result": "7 ", "perPage": true})), "spaces kept: {fields:?}");
assert!(fields.contains(&json!({"code": "TOC \\o \"1-3\"", "result": "One\nTwo", "perPage": false})), "{fields:?}");
let bib = fields.iter().find(|f| f["code"] == "BIBLIOGRAPHY").unwrap();
assert_eq!((bib["result"].as_str().map(str::len), &bib["resultTruncated"]), (Some(2000), &json!(true)));
assert!(fields.iter().filter(|f| f["code"] != "BIBLIOGRAPHY").all(|f| f.get("resultTruncated").is_none()));
assert!(part.get("fieldsTruncated").is_none(), "{part}");
}
/// #4467 review: a field name in lower case is a page field all the same, and a field in a table
/// nested deeper than `document.inspect` walks (8 tables) is left out with `fieldsTruncated`.
#[test]
fn inspect_reads_field_names_in_any_case_and_bounds_nested_tables() {
use wordcraft_doc::{Block, CharProps, InlineObject, Paragraph, Table, para_block};
let field =
|instr: &str| InlineObject::Field { instr: instr.into(), result: "1".into(), locked: false, dirty: false, code: false, form: String::new() };
// A field `depth` tables deep.
let nested = |depth: usize| {
let mut p = Paragraph::new();
p.insert_object(0, field("page"), &CharProps::default()).unwrap();
let mut block = para_block(p);
for _ in 0..depth {
let mut t = Table::new(1, 1, 200.0);
t.rows[0].cells[0].blocks = vec![block];
block = std::sync::Arc::new(Block::Table(t));
}
block
};
for (depth, listed) in [(0, true), (8, true), (9, false)] {
let mut s = s();
let id = run(&mut s, "insert.pageNumber", json!({}))["story"].as_u64().unwrap();
let key = u32::try_from(id).unwrap();
s.doc.parts.get_mut(&key).unwrap().blocks = vec![nested(depth)];
let doc = run(&mut s, "document.inspect", json!({}));
let part = doc["parts"].as_array().unwrap().iter().find(|p| p["id"] == id).unwrap().clone();
if listed {
assert_eq!(part["fields"], json!([{"code": "page", "result": "1", "perPage": true}]), "{depth}");
assert!(part.get("fieldsTruncated").is_none(), "{depth}: {part}");
} else {
assert!(part.get("fields").is_none(), "{depth}: {part}");
assert_eq!(part["fieldsTruncated"], true, "{depth}");
}
}
}
/// #3676: a 1-based page parameter becomes a 0-based index without wrapping.
/// On 32-bit and wasm the old cast truncated 2^32 to 0 and `0 - 1` panicked.
#[test]
+21
View File
@@ -371,6 +371,27 @@ fn missing_fonts_from_command_search_say_so_in_the_status_line() {
assert!(s.status.ends_with(" and 35 more") && s.status.len() < 300, "{}", s.status);
}
/// A missing font its publisher gives away says where to get it; others don't.
#[test]
fn a_missing_font_its_publisher_gives_away_names_the_download_page() {
if wordcraft_fonts::FontDb::global().families().iter().any(|f| f.eq_ignore_ascii_case("Aptos")) {
return; // Installed here: not missing.
}
let mut s = Session::new(doc_of(vec![para_in("Text", "Aptos"), para_in("Text", "Wordcraft Nowhere Face")]));
let layout = s.layout();
let m = crate::cmd::font::missing_fonts(&s.doc, &layout);
let aptos = m.iter().find(|f| f.font == "Aptos").unwrap();
assert_eq!(aptos.download.map(|d| d.publisher.as_str()), Some("Microsoft"));
assert!(m.iter().find(|f| f.font == "Wordcraft Nowhere Face").unwrap().download.is_none());
let out = s.run("font.missing", &json!({})).unwrap();
let listed = out["missing"].as_array().unwrap();
let aptos = listed.iter().find(|f| f["font"] == "Aptos").unwrap();
assert!(aptos["download"]["url"].as_str().unwrap().starts_with("https://www.microsoft.com/"), "{aptos}");
assert_eq!(aptos["download"]["publisher"], "Microsoft");
assert_eq!(aptos["download"]["restricted"], false);
assert!(listed.iter().find(|f| f["font"] == "Wordcraft Nowhere Face").unwrap().get("download").is_none());
}
#[test]
fn a_huge_font_name_is_kept_short() {
let name = format!("Wordcraft {}", "é".repeat(100_000));
+57
View File
@@ -141,6 +141,63 @@ fn dragging_a_connector_end_glues_it_to_the_nearest_site() {
run(&mut s, "arrange.nudge", json!({"dx": 1}));
}
/// A plain line's ends move to points the way a connector's free ends do, but never glue: not
/// even dropped on a shape's connection site, nor by reference. A turned line's ends are where
/// it is drawn, and moving one leaves the other there.
#[test]
fn dragging_a_line_end_moves_it_freely() {
let float =
|x: f32, y: f32, rot: f32| Float { wrap: Wrap::InFrontOfText, h_rel: Anchor::Page, v_rel: Anchor::Page, x, y, rot, ..Default::default() };
let shape = |kind, w, h, float, extra| InlineObject::Shape {
kind,
w,
h,
fill: None,
stroke: None,
stroke_width: 1.0,
float,
story: None,
freeform: None,
effects: Default::default(),
extra,
};
let mut p = Paragraph::with_text("Body ", CharProps::default());
for o in [
shape(ShapeKind::Rectangle, 72.0, 36.0, float(100.0, 200.0, 0.0), ShapeExtra { id: 1, ..Default::default() }),
shape(ShapeKind::Line, 200.0, 0.0, float(300.0, 300.0, 0.0), ShapeExtra::default()),
shape(ShapeKind::Line, 200.0, 0.0, float(300.0, 500.0, 90.0), ShapeExtra::default()),
] {
let end = p.len();
p.insert_object(end, o, &CharProps::default()).unwrap();
}
let mut d = Document::new();
d.body = vec![para_block(p)];
let mut s = Session::new(d);
let line_ends = |s: &mut Session, off: usize| {
let pos = Pos::body(0, off);
let hit = s.layout().object(&pos, 0).unwrap();
let Some(InlineObject::Shape { float, extra, .. }) = s.doc.para_at(&pos).and_then(|p| p.object_at(pos.off)).cloned() else { panic!() };
let (a, b) = crate::cmd::connectors::page_ends(hit.rect, float.flip_h, float.flip_v, hit.spin.deg);
let round = |p: (f32, f32)| ((p.0 * 100.0).round() / 100.0, (p.1 * 100.0).round() / 100.0);
(round(a), round(b), extra)
};
select(&mut s, 8);
run(&mut s, "shape.connect", json!({"end": {"x": 500.0, "y": 350.0}}));
assert_eq!(line_ends(&mut s, 8).0, (300.0, 300.0), "the start stays");
assert_eq!(line_ends(&mut s, 8).1, (500.0, 350.0));
// Right on the rectangle's left site (100, 218): a connector would glue; the line doesn't.
run(&mut s, "shape.connect", json!({"start": {"x": 101.0, "y": 217.0}}));
let (a, _, extra) = line_ends(&mut s, 8);
assert_eq!((a, extra.start, extra.end), ((101.0, 217.0), None, None));
assert!(s.run("shape.connect", &json!({"end": {"path": [0], "off": 5}})).is_err(), "a line doesn't glue by reference");
// Turned a quarter: drawn from (400, 400) to (400, 600). Its end moves down; its start stays.
select(&mut s, 11);
assert_eq!(line_ends(&mut s, 11).0, (400.0, 400.0));
run(&mut s, "shape.connect", json!({"end": {"x": 400.0, "y": 650.0}}));
let (a, b, _) = line_ends(&mut s, 11);
assert_eq!((a, b), ((400.0, 400.0), (400.0, 650.0)));
}
#[test]
fn wordart_is_inserted_styled_and_bent() {
let mut s = Session::new(Document::new());
+1
View File
@@ -21,3 +21,4 @@ brotli-decompressor = { workspace = true }
unicode-bidi = { workspace = true }
unicode-script = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }
+149
View File
@@ -0,0 +1,149 @@
//! Fonts a document may ask for that their publisher gives away, and where: when one isn't
//! installed, the app can say where to get it instead of only naming the substitute.
//!
//! Only a publisher's own page is listed, for fonts it offers for download at no charge. The app
//! links to the page; it never downloads, bundles or redistributes the fonts, and following the
//! publisher's licence terms is the user's part. A font whose publisher limits who may use it
//! is marked [`FontDownload::restricted`], so the app can say so beside the link. Most Office
//! fonts (Calibri, Cambria, Consolas, Segoe UI…) come only with Windows or Office, so they are
//! not here.
//!
//! The list is `font_downloads.json` beside this file, built into the app: adding a font is a
//! new entry there.
/// The list: `{"downloads": [{"family", "publisher", "url", "restricted"?, "note"?}]}`.
const LIST: &str = include_str!("font_downloads.json");
/// Most entries read from the list.
const MAX_DOWNLOADS: usize = 1024;
/// Where a font family can be downloaded at no charge.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FontDownload {
/// The name of the download: the family or collection the page offers. It covers the
/// family itself and every name that continues it after a space ("Aptos" covers "Aptos
/// Display", "Aptos Narrow", "Aptos Mono"…).
pub name: String,
/// Who publishes the font and hosts the page.
pub publisher: String,
/// The publisher's download page.
pub url: String,
/// The publisher limits who may use the font or what for (its design resources for one
/// platform, say): worth saying beside the link.
pub restricted: bool,
}
/// The entries of `list` that name a family, a publisher and an `https` page; others are left
/// out, and a list that isn't JSON has none.
fn parse(list: &str) -> Vec<FontDownload> {
let Ok(json) = serde_json::from_str::<serde_json::Value>(list) else {
log::warn!("font downloads: the list isn't JSON");
return Vec::new();
};
let entries = json.get("downloads").and_then(|d| d.as_array()).map(Vec::as_slice).unwrap_or_default();
entries
.iter()
.take(MAX_DOWNLOADS)
.filter_map(|e| {
let field = |k: &str| e.get(k).and_then(|v| v.as_str()).map(str::trim).filter(|v| !v.is_empty());
let (name, publisher, url) = (field("family")?, field("publisher")?, field("url")?);
let restricted = e.get("restricted").and_then(|v| v.as_bool()).unwrap_or(false);
url.starts_with("https://").then(|| FontDownload { name: name.into(), publisher: publisher.into(), url: url.into(), restricted })
})
.collect()
}
/// The built-in list, read once.
fn downloads() -> &'static [FontDownload] {
static DOWNLOADS: std::sync::OnceLock<Vec<FontDownload>> = std::sync::OnceLock::new();
DOWNLOADS.get_or_init(|| parse(LIST))
}
/// Where `family` can be downloaded free of charge from its publisher, if it can. The name is
/// matched whatever its case, with a charset suffix ("Aptos CE") or alternates ("Aptos;Arial")
/// as a document may write it.
pub fn download_for(family: &str) -> Option<&'static FontDownload> {
family.split(';').map(str::trim).filter(|f| !f.is_empty()).find_map(|f| {
let f = crate::word::charset_base_family(f).trim();
downloads().iter().find(|d| {
// Compared as bytes: `get` on a `str` would refuse a split inside a character.
let (name, f) = (d.name.as_bytes(), f.as_bytes());
f.get(..name.len()).is_some_and(|head| head.eq_ignore_ascii_case(name)) && matches!(f.get(name.len()), None | Some(b' '))
})
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn microsoft_fonts_offered_free_of_charge_have_a_download_page() {
let aptos = download_for("Aptos").unwrap();
assert_eq!(aptos.publisher, "Microsoft");
assert!(aptos.url.starts_with("https://www.microsoft.com/"));
for family in ["aptos", "Aptos Display", "APTOS NARROW", "Aptos Mono", "Aptos Serif", " Aptos ", "Aptos;Arial", "Aptos CE"] {
assert_eq!(download_for(family), Some(aptos), "{family}");
}
assert_eq!(download_for("Cascadia Code").map(|d| d.name.as_str()), Some("Cascadia Code"));
assert_eq!(download_for("Cascadia Mono PL").map(|d| d.name.as_str()), Some("Cascadia Mono"));
assert_eq!(download_for("Selawik Semibold").map(|d| (d.name.as_str(), d.restricted)), Some(("Selawik", false)));
assert!(!aptos.restricted);
}
/// Fonts offered with limits on their use are listed, and say so. Plain Segoe UI isn't
/// offered for download, and the Segoe UI Variable entry doesn't stand in for it.
#[test]
fn fonts_with_limited_licences_are_marked() {
for family in ["Segoe UI Variable", "Segoe UI Variable Text", "segoe fluent icons", "Segoe MDL2 Assets"] {
let d = download_for(family).unwrap_or_else(|| panic!("{family}"));
assert!(d.restricted, "{family}");
assert!(d.url.starts_with("https://learn.microsoft.com/"), "{}", d.url);
}
for family in ["Segoe UI", "Segoe UI Semibold", "Segoe UI Symbol", "Segoe Print"] {
assert_eq!(download_for(family), None, "{family}");
}
}
#[test]
fn other_fonts_have_none() {
// Fonts that come only with Windows or Office, look-alike names, and junk.
for family in ["Calibri", "Cambria", "Consolas", "Arial", "Aptosa", "Apt", "", ";", "Aptosé", "é", "Cascadia"] {
assert_eq!(download_for(family), None, "{family}");
}
}
#[test]
fn every_page_is_https_on_the_publishers_site() {
// Every entry of the file is read: none is left out for a missing field.
let entries = serde_json::from_str::<serde_json::Value>(LIST).unwrap()["downloads"].as_array().unwrap().len();
assert_eq!(downloads().len(), entries);
assert!(entries >= 3);
for d in downloads() {
let hosts = ["https://www.microsoft.com/", "https://learn.microsoft.com/", "https://github.com/microsoft/"];
assert!(hosts.iter().any(|h| d.url.starts_with(h)), "{}", d.url);
}
}
#[test]
fn a_broken_list_has_no_downloads() {
assert!(parse("not json").is_empty());
assert!(parse("{}").is_empty());
assert!(parse(r#"{"downloads": 7}"#).is_empty());
// Entries without a family, a publisher or an https page are left out.
let list = r#"{"downloads": [
{"family": "A", "publisher": "P", "url": "http://plain.example"},
{"family": "", "publisher": "P", "url": "https://x.example"},
{"publisher": "P", "url": "https://x.example"},
{"family": 3, "publisher": "P", "url": "https://x.example"},
"junk",
{"family": " Kept ", "publisher": "P", "url": "https://x.example"}
]}"#;
let read = parse(list);
assert_eq!(read.len(), 1);
assert_eq!(read[0].name, "Kept");
assert!(!read[0].restricted, "not restricted unless it says so");
let marked = parse(r#"{"downloads": [{"family": "A", "publisher": "P", "url": "https://x.example", "restricted": true}]}"#);
assert!(marked[0].restricted);
}
}
+50
View File
@@ -0,0 +1,50 @@
{
"about": "Fonts a document may ask for that their publisher offers for download at no charge, and the publisher's own download page. WordCraft links to the page; it never downloads, bundles or redistributes the fonts, and the user is responsible for the publisher's licence terms. 'family' also covers every name that continues it after a space: 'Aptos' covers 'Aptos Display'. 'restricted': true marks a font whose publisher limits who may use it or what for; the app then says so beside the download.",
"downloads": [
{
"family": "Aptos",
"publisher": "Microsoft",
"url": "https://www.microsoft.com/download/details.aspx?id=106087",
"note": "Microsoft Download Center, 'Microsoft Aptos Fonts': the whole Aptos family."
},
{
"family": "Cascadia Code",
"publisher": "Microsoft",
"url": "https://github.com/microsoft/cascadia-code/releases",
"note": "Microsoft's own repository; SIL Open Font License."
},
{
"family": "Cascadia Mono",
"publisher": "Microsoft",
"url": "https://github.com/microsoft/cascadia-code/releases",
"note": "Comes in the Cascadia Code release."
},
{
"family": "Selawik",
"publisher": "Microsoft",
"url": "https://github.com/microsoft/fonts",
"note": "Microsoft's open-source stand-in for Segoe UI; SIL Open Font License."
},
{
"family": "Segoe UI Variable",
"publisher": "Microsoft",
"url": "https://learn.microsoft.com/windows/apps/design/downloads",
"restricted": true,
"note": "Microsoft's Windows design downloads page. Offered with the design resources for Windows apps; plain 'Segoe UI' is not offered for download and is not matched by this entry."
},
{
"family": "Segoe Fluent Icons",
"publisher": "Microsoft",
"url": "https://learn.microsoft.com/windows/apps/design/downloads",
"restricted": true,
"note": "Microsoft's Windows design downloads page."
},
{
"family": "Segoe MDL2 Assets",
"publisher": "Microsoft",
"url": "https://learn.microsoft.com/windows/apps/design/downloads",
"restricted": true,
"note": "Microsoft's Windows design downloads page."
}
]
}
+1
View File
@@ -450,6 +450,7 @@ fn long_names_are_cut_before_faces_copy_them() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn named_instances_and_collection_members_are_capped() {
// #3716: a file's faces are capped before they are built, however the file multiplies them.
let font = variable_font(300);
+1
View File
@@ -8,6 +8,7 @@
#![forbid(unsafe_code)]
pub mod color;
pub mod download;
mod fontdb;
pub mod math;
mod script;
+1 -1
View File
@@ -33,7 +33,6 @@ craft-zip = { workspace = true }
wordcraft-geom = { workspace = true, features = ["image"] }
craft-metafile = { workspace = true }
encoding_rs = { workspace = true }
chardetng = { workspace = true }
log = { workspace = true }
chrono = { workspace = true }
# Password-protected ODT (ODF 1.3 Part 2 §3.4): AES-256-CBC, SHA-1/SHA-256, PBKDF2 (HMAC-SHA-1).
@@ -41,3 +40,4 @@ chrono = { workspace = true }
wordcraft-doc = { workspace = true, features = ["test-support"] }
craft-image-codecs = { workspace = true }
proptest = { workspace = true }
chardetng = { workspace = true }
+4
View File
@@ -488,6 +488,7 @@ fn code_blocks_and_rules() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn deep_nesting_is_bounded() {
let html = "<div><table><tr><td>".repeat(5000) + "x";
let d = import("html", html.as_bytes()).unwrap().unwrap();
@@ -566,6 +567,7 @@ fn latex_export_carries_no_commands_from_the_document() {
/// past them for the last text made a megabyte of labels take tens of seconds), and labels
/// separated by text are all kept.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn latex_labels_in_a_row_are_read_in_linear_time() {
let started = std::time::Instant::now();
for src in ["\\label{a} ".repeat(100_000) + "x", "\\label{a}".repeat(100_000) + "x", "\\hypertarget{a}{} ".repeat(100_000)] {
@@ -743,6 +745,7 @@ fn decorative_picture_exports_with_empty_alt_text() {
/// Hostile Markdown and HTML that rescanned the rest of the text from every `[`, `![`, `<` or `&`
/// took quadratic time: a few hundred KB froze the importer for tens of seconds.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn hostile_importer_input_takes_linear_time() {
use std::time::{Duration, Instant};
let cases: [(&str, String); 5] = [
@@ -764,6 +767,7 @@ fn hostile_importer_input_takes_linear_time() {
/// bracket scan over them looking for a closing run through the rest of the text: superlinear
/// time on hostile Markdown.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn unmatched_backtick_runs_take_linear_time() {
use std::time::{Duration, Instant};
// Runs of 1, 2, … 2000 backticks (2 MB), none of them closed.
+5 -88
View File
@@ -10,95 +10,12 @@ pub use wordcraft_doc::encoding::cp1252;
use crate::model::{FBlock, ListCounter, from_doc};
fn utf16(b: &[u8], le: bool) -> String {
let units: Vec<u16> = b.as_chunks::<2>().0.iter().map(|c| if le { u16::from_le_bytes(*c) } else { u16::from_be_bytes(*c) }).collect();
String::from_utf16_lossy(&units)
}
#[cfg(test)]
mod legacy_oracle_tests;
/// UTF-16 without a BOM, told by its zero bytes: `Some(true)` little-endian, `Some(false)`
/// big-endian.
fn unmarked_utf16(b: &[u8]) -> Option<bool> {
if b.len() < 4 || !b.len().is_multiple_of(2) {
return None;
}
let sample = b.get(..b.len().min(512))?;
let even_zero = sample.iter().step_by(2).filter(|x| **x == 0).count();
let odd_zero = sample.iter().skip(1).step_by(2).filter(|x| **x == 0).count();
let half = sample.len() / 2;
if odd_zero * 10 >= half * 4 && even_zero * 10 < half {
return Some(true);
}
if even_zero * 10 >= half * 4 && odd_zero * 10 < half {
return Some(false);
}
None
}
/// Does `b` look like plain text, so a file with an extension WordCraft doesn't know (a
/// playlist, a log, a config file…) can open as text the way Word's text import opens it?
///
/// Text has a BOM, or reads as UTF-16 by its zero bytes, or has no NUL byte and next to no
/// other control characters in its first 64 KB. PDFs are refused by their signature (they
/// start as text). An empty file is text.
pub fn is_text(b: &[u8]) -> bool {
if b.starts_with(&[0xEF, 0xBB, 0xBF]) || b.starts_with(&[0xFF, 0xFE]) || b.starts_with(&[0xFE, 0xFF]) {
return true;
}
if b.starts_with(b"%PDF-") {
return false;
}
let sample = b.get(..b.len().min(64 << 10)).unwrap_or(b);
// Tab, line feed, form feed, carriage return, DOS end-of-file (^Z) and escape appear in text;
// other controls in more than 1 of 100 characters mean binary.
let odd = |c: u32| c == 0 || (c < 0x20 && !matches!(c, 0x09 | 0x0A | 0x0C | 0x0D | 0x1A | 0x1B)) || c == 0x7F;
if let Some(le) = unmarked_utf16(sample) {
let units = sample.as_chunks::<2>().0;
let n = units.iter().filter(|c| odd(u32::from(if le { u16::from_le_bytes(**c) } else { u16::from_be_bytes(**c) }))).count();
return n.saturating_mul(100) <= units.len();
}
if sample.contains(&0) {
return false;
}
sample.iter().filter(|c| odd(u32::from(**c))).count().saturating_mul(100) <= sample.len()
}
/// Decode text bytes: BOMs first, then UTF-16 without BOM (by its zero bytes), UTF-8, and the
/// legacy encoding the bytes look like ([`legacy`]) as the last resort.
pub fn decode(b: &[u8]) -> String {
if let Some(rest) = b.strip_prefix(&[0xEF, 0xBB, 0xBF]) {
return String::from_utf8_lossy(rest).into_owned();
}
if let Some(rest) = b.strip_prefix(&[0xFF, 0xFE]) {
return utf16(rest, true);
}
if let Some(rest) = b.strip_prefix(&[0xFE, 0xFF]) {
return utf16(rest, false);
}
if let Some(le) = unmarked_utf16(b) {
return utf16(b, le);
}
match std::str::from_utf8(b) {
Ok(s) => s.to_string(),
Err(_) => legacy(b),
}
}
/// Most bytes looked at to guess a legacy encoding.
const SNIFF: usize = 1 << 20;
/// Bytes that aren't UTF-8 in the legacy encoding they look like (chardetng, the detector
/// Firefox uses for unlabelled pages): GBK/GB18030 for Simplified Chinese, Big5, Shift_JIS or
/// EUC-JP, EUC-KR, or a single-byte Windows / ISO code page; Windows-1252 as before when the
/// guess is a single-byte Western one.
pub fn legacy(b: &[u8]) -> String {
let mut det = chardetng::EncodingDetector::new(chardetng::Iso2022JpDetection::Deny);
det.feed(b.get(..SNIFF).unwrap_or(b), b.len() <= SNIFF);
let enc = det.guess(None, chardetng::Utf8Detection::Deny);
if enc == encoding_rs::WINDOWS_1252 {
return b.iter().map(|c| cp1252(*c)).collect();
}
enc.decode_without_bom_handling(b).0.into_owned()
}
// Detection is shared with the other Craft apps. WordCraft's `cp1252` and encoding_rs agree on
// every byte (pinned by `legacy_oracle_tests`), so a Windows-1252 guess needs no local mapping.
pub use craft_mime::detect::{decode, is_text, legacy};
/// Decode bytes in a named encoding (a WHATWG label such as `gbk`, `gb18030`, `big5`,
/// `shift_jis`, `euc-kr`, `utf-16le`, `windows-1251`); `None` when the label isn't known.
@@ -0,0 +1,210 @@
//! The pre-cutover local implementations, kept as an oracle: the shared `craft_mime::detect`
//! functions plus the cp1252 wrapper must agree with them byte for byte.
use super::cp1252;
fn utf16(b: &[u8], le: bool) -> String {
let units: Vec<u16> = b.as_chunks::<2>().0.iter().map(|c| if le { u16::from_le_bytes(*c) } else { u16::from_be_bytes(*c) }).collect();
String::from_utf16_lossy(&units)
}
/// UTF-16 without a BOM, told by its zero bytes: `Some(true)` little-endian, `Some(false)`
/// big-endian.
fn unmarked_utf16(b: &[u8]) -> Option<bool> {
if b.len() < 4 || !b.len().is_multiple_of(2) {
return None;
}
let sample = b.get(..b.len().min(512))?;
let even_zero = sample.iter().step_by(2).filter(|x| **x == 0).count();
let odd_zero = sample.iter().skip(1).step_by(2).filter(|x| **x == 0).count();
let half = sample.len() / 2;
if odd_zero * 10 >= half * 4 && even_zero * 10 < half {
return Some(true);
}
if even_zero * 10 >= half * 4 && odd_zero * 10 < half {
return Some(false);
}
None
}
/// Does `b` look like plain text, so a file with an extension WordCraft doesn't know (a
/// playlist, a log, a config file…) can open as text the way Word's text import opens it?
///
/// Text has a BOM, or reads as UTF-16 by its zero bytes, or has no NUL byte and next to no
/// other control characters in its first 64 KB. PDFs are refused by their signature (they
/// start as text). An empty file is text.
fn is_text(b: &[u8]) -> bool {
if b.starts_with(&[0xEF, 0xBB, 0xBF]) || b.starts_with(&[0xFF, 0xFE]) || b.starts_with(&[0xFE, 0xFF]) {
return true;
}
if b.starts_with(b"%PDF-") {
return false;
}
let sample = b.get(..b.len().min(64 << 10)).unwrap_or(b);
// Tab, line feed, form feed, carriage return, DOS end-of-file (^Z) and escape appear in text;
// other controls in more than 1 of 100 characters mean binary.
let odd = |c: u32| c == 0 || (c < 0x20 && !matches!(c, 0x09 | 0x0A | 0x0C | 0x0D | 0x1A | 0x1B)) || c == 0x7F;
if let Some(le) = unmarked_utf16(sample) {
let units = sample.as_chunks::<2>().0;
let n = units.iter().filter(|c| odd(u32::from(if le { u16::from_le_bytes(**c) } else { u16::from_be_bytes(**c) }))).count();
return n.saturating_mul(100) <= units.len();
}
if sample.contains(&0) {
return false;
}
sample.iter().filter(|c| odd(u32::from(**c))).count().saturating_mul(100) <= sample.len()
}
/// Decode text bytes: BOMs first, then UTF-16 without BOM (by its zero bytes), UTF-8, and the
/// legacy encoding the bytes look like ([`legacy`]) as the last resort.
fn decode(b: &[u8]) -> String {
if let Some(rest) = b.strip_prefix(&[0xEF, 0xBB, 0xBF]) {
return String::from_utf8_lossy(rest).into_owned();
}
if let Some(rest) = b.strip_prefix(&[0xFF, 0xFE]) {
return utf16(rest, true);
}
if let Some(rest) = b.strip_prefix(&[0xFE, 0xFF]) {
return utf16(rest, false);
}
if let Some(le) = unmarked_utf16(b) {
return utf16(b, le);
}
match std::str::from_utf8(b) {
Ok(s) => s.to_string(),
Err(_) => legacy(b),
}
}
/// Most bytes looked at to guess a legacy encoding.
const SNIFF: usize = 1 << 20;
/// Bytes that aren't UTF-8 in the legacy encoding they look like (chardetng, the detector
/// Firefox uses for unlabelled pages): GBK/GB18030 for Simplified Chinese, Big5, Shift_JIS or
/// EUC-JP, EUC-KR, or a single-byte Windows / ISO code page; Windows-1252 as before when the
/// guess is a single-byte Western one.
fn legacy(b: &[u8]) -> String {
let mut det = chardetng::EncodingDetector::new(chardetng::Iso2022JpDetection::Deny);
det.feed(b.get(..SNIFF).unwrap_or(b), b.len() <= SNIFF);
let enc = det.guess(None, chardetng::Utf8Detection::Deny);
if enc == encoding_rs::WINDOWS_1252 {
return b.iter().map(|c| cp1252(*c)).collect();
}
enc.decode_without_bom_handling(b).0.into_owned()
}
fn samples() -> Vec<Vec<u8>> {
let mut v: Vec<Vec<u8>> = vec![vec![]];
for b in 0..=255u8 {
v.push(vec![b]);
}
for b in 0x80..=0x9Fu8 {
v.push([b"caf".as_slice(), &[b], b" ok text here"].concat());
v.push(vec![b, b'a', b'b']);
}
// Western text with each byte Windows-1252 leaves undefined: the case the cp1252 wrapper is for.
for b in [0x81u8, 0x8D, 0x8F, 0x90, 0x9D] {
v.push([b"caf\xE9 au lait ".as_slice(), &[b], b" ok"].concat());
v.push([b"Le caf\xE9 de la gare ".as_slice(), &[b]].concat());
v.push([b"r\xE9sum\xE9 ".as_slice(), &[b]].concat());
}
v.push("héllo wörld € naïve".as_bytes().to_vec());
v.push([&[0xEF, 0xBB, 0xBF][..], "héllo".as_bytes()].concat());
v.push([&[0xEF, 0xBB, 0xBF][..], &[0xE9, 0x80]].concat());
for (bom, le) in [(&[0xFF, 0xFE][..], true), (&[0xFE, 0xFF][..], false)] {
let t = "héllo wörld 日本語 😀";
v.push([bom, &t.encode_utf16().flat_map(|u| if le { u.to_le_bytes() } else { u.to_be_bytes() }).collect::<Vec<u8>>()[..]].concat());
v.push([bom, b"h\0i\0!"].concat());
let raw: Vec<u8> = "hello world, plain ascii".encode_utf16().flat_map(|u| if le { u.to_le_bytes() } else { u.to_be_bytes() }).collect();
v.push(raw);
let raw: Vec<u8> =
"héllo wörld, mostly ascii \u{20AC}".encode_utf16().flat_map(|u| if le { u.to_le_bytes() } else { u.to_be_bytes() }).collect();
v.push(raw);
}
v.push(vec![0xC3, 0x28, 0xA0, 0xA1, 0xE2, 0x82]);
v.push(b"#EXTM3U\r\n#EXTINF:1,x\r\n".to_vec());
v.push(b"%PDF-1.7\n%\xE2\xE3".to_vec());
v.push(b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR".to_vec());
v.push(vec![1, 0, 2, 0, 3]);
for (enc, text) in [
(encoding_rs::GBK, "这是一个用简体中文写的文本文件,用来测试编码检测。"),
(encoding_rs::BIG5, "這是一個用繁體中文寫的文字檔案,用來測試編碼偵測。"),
(encoding_rs::SHIFT_JIS, "これは日本語で書かれたテキストファイルです。エンコーディングの検出をテストします。"),
(encoding_rs::EUC_KR, "이것은 한국어로 작성된 텍스트 파일입니다. 인코딩 감지를 테스트합니다."),
(encoding_rs::WINDOWS_1251, "Это текстовый файл на русском языке для проверки определения кодировки."),
] {
let (b, _, _) = enc.encode(text);
v.push(b.into_owned());
}
// Past the 1 MiB sniff limit, and a 2 MiB input.
let mut big = vec![b'a'; 1 << 20];
big.extend_from_slice(&[0xE9; 3]);
v.push(big);
let mut big = "héllo wörld ".repeat((2 << 20) / 13).into_bytes();
big.push(0xE9);
v.push(big);
v.push(vec![0x80; 2 << 20]);
v
}
fn assert_same(b: &[u8]) {
let head: Vec<u8> = b.iter().take(24).copied().collect();
assert_eq!(is_text(b), super::is_text(b), "is_text {head:02x?}");
assert_eq!(legacy(b), super::legacy(b), "legacy {head:02x?}");
assert_eq!(decode(b), super::decode(b), "decode {head:02x?}");
}
#[test]
fn shared_detect_matches_the_old_local_copies() {
for b in samples() {
assert_same(&b);
}
}
#[test]
fn cp1252_agrees_with_encoding_rs_on_every_byte() {
// Why a Windows-1252 guess needs no local mapping: both give U+0081, U+008D, U+008F, U+0090
// and U+009D for the bytes the code page leaves undefined.
for b in 0..=255u8 {
let one = [b];
let (s, _) = encoding_rs::WINDOWS_1252.decode_without_bom_handling(&one);
assert_eq!(s.chars().collect::<Vec<_>>(), vec![cp1252(b)], "byte {b:#x}");
}
}
#[test]
fn random_buffers_never_panic_and_match() {
let mut s: u64 = 0x2545_F491_4F6C_DD1D;
let mut next = || {
s = s.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407);
(s >> 33) as u32
};
for i in 0..3000 {
let len = (next() % 700) as usize;
let mode = i % 4;
let b: Vec<u8> = (0..len)
.map(|_| {
let r = next();
match mode {
0 => r as u8,
1 => 0x80 | (r as u8 & 0x1F), // 0x80..=0x9F
2 => {
if r % 3 == 0 {
0
} else {
(r >> 8) as u8 % 0x80
}
}
_ => {
if r % 5 == 0 {
0x80 + (r >> 8) as u8 % 0x80
} else {
b'a' + (r >> 8) as u8 % 26
}
}
}
})
.collect();
assert_same(&b);
}
}
+69 -10
View File
@@ -320,19 +320,16 @@ pub fn probe_dib(dib: Vec<u8>) -> Result<Probe<'static>, Refusal> {
Ok(Probe { bytes: Cow::Owned(bmp), format: ImageFormat::Bmp, width, height, peak, alpha })
}
/// A DIB's width and height, read by the shared BMP probe. Header sizes other than 12 (core) and
/// 40 or more (info) are refused here: the probe would read them as info headers.
fn dib_header_dims(dib: &[u8]) -> Option<(u32, u32)> {
let bi_size = u32::from_le_bytes(dib.get(0..4)?.try_into().ok()?);
if bi_size == 12 {
let w = u16::from_le_bytes(dib.get(4..6)?.try_into().ok()?);
let h = u16::from_le_bytes(dib.get(6..8)?.try_into().ok()?);
Some((u32::from(w), u32::from(h)))
} else if bi_size >= 40 {
let w = i32::from_le_bytes(dib.get(4..8)?.try_into().ok()?);
let h = i32::from_le_bytes(dib.get(8..12)?.try_into().ok()?);
Some((u32::try_from(w).ok()?, h.unsigned_abs()))
} else {
None
if bi_size != 12 && bi_size < 40 {
return None;
}
// Only the first 12 header bytes are read, so only they are wrapped: this runs for DIBs too
// big to decode, which must not be copied whole.
craft_image_codecs::bmp_probe::dimensions(&dib_to_bmp(dib.get(..12).unwrap_or(dib)))
}
/// A `width` × `height` picture of `native` bytes a pixel, with its peak (see [`peak_bytes`]).
@@ -577,6 +574,68 @@ fn dib_layout(mut dib: Vec<u8>) -> Vec<u8> {
mod tests {
use super::*;
// Hand-written reader the shared probe replaced; kept as the oracle.
fn dib_header_dims_old(dib: &[u8]) -> Option<(u32, u32)> {
let bi_size = u32::from_le_bytes(dib.get(0..4)?.try_into().ok()?);
if bi_size == 12 {
let w = u16::from_le_bytes(dib.get(4..6)?.try_into().ok()?);
let h = u16::from_le_bytes(dib.get(6..8)?.try_into().ok()?);
Some((u32::from(w), u32::from(h)))
} else if bi_size >= 40 {
let w = i32::from_le_bytes(dib.get(4..8)?.try_into().ok()?);
let h = i32::from_le_bytes(dib.get(8..12)?.try_into().ok()?);
Some((u32::try_from(w).ok()?, h.unsigned_abs()))
} else {
None
}
}
fn hdr(size: u32, w: i32, h: i32, len: usize) -> Vec<u8> {
let mut v = [0u8; 124].to_vec();
v.resize(124.max(len), 0);
v[0..4].copy_from_slice(&size.to_le_bytes());
v[4..8].copy_from_slice(&w.to_le_bytes());
v[8..12].copy_from_slice(&h.to_le_bytes());
v.truncate(len);
v
}
#[test]
fn dib_dims_match_the_hand_written_reader() {
let sizes = [0u32, 11, 12, 13, 16, 39, 40, 41, 52, 108, 124, u32::MAX];
let dims = [(0, 0), (1, 1), (3, -2), (0, 5), (7, i32::MIN), (i32::MIN, 1), (-1, 1), (65536, 70000), (i32::MAX, i32::MAX)];
for &s in &sizes {
for &(w, h) in &dims {
for len in 0..=130 {
let d = hdr(s, w, h, len);
assert_eq!(dib_header_dims(&d), dib_header_dims_old(&d), "size {s} w {w} h {h} len {len}");
}
}
}
assert_eq!(dib_header_dims(&hdr(40, 3, -2, 40)), Some((3, 2)));
let mut core = hdr(12, 3, 0, 12);
core[6..8].copy_from_slice(&2u16.to_le_bytes());
assert_eq!(dib_header_dims(&core), Some((3, 2)));
}
#[test]
fn dib_dims_never_panic_on_junk() {
let mut x = 0x2545_f491_4f6c_dd1du64;
let mut buf = [0u8; 64];
for _ in 0..2000 {
for b in buf.iter_mut() {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
*b = x as u8;
}
for n in 0..=buf.len() {
assert_eq!(dib_header_dims(&buf[..n]), dib_header_dims_old(&buf[..n]));
let _ = probe_dib(buf[..n].to_vec());
}
}
}
fn encode(img: impl Into<DynamicImage>, format: ImageFormat) -> Vec<u8> {
let dyn_img = img.into();
let codec_img = dynamic_to_codec(&dyn_img).unwrap();
+4
View File
@@ -923,6 +923,7 @@ fn a_long_unbreakable_word_lays_out_in_linear_time() {
/// A word a megabyte long lays out in a fraction of a second: it used to take half a minute.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn a_megabyte_long_word_lays_out_quickly() {
let d = long_word_beside_a_float(1_000_000);
let t = std::time::Instant::now();
@@ -1143,6 +1144,7 @@ fn a_word_with_alternating_advances_lays_out_in_linear_time() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn hidden_separators_with_automatic_hyphenation_lay_out_in_linear_time() {
crate::test_fonts();
// "hyphenation " n times with every space hidden: one joined word of 11n letters reaches the
@@ -1175,6 +1177,7 @@ fn page_break_paragraphs(n: usize) -> Document {
/// the section list by walking the whole body, so a document of n paragraphs paid n body-length
/// scans: a two-megabyte file took half a minute before the first page was drawn.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn a_document_of_many_paragraphs_lays_out_in_linear_time() {
let (t1, tk, ratio) = layout_time_ratio(page_break_paragraphs, 2_500, &LayoutOptions::default());
eprintln!("t(n) {t1:.4} s, t(8n) {tk:.4} s, ratio {ratio:.1}");
@@ -1182,6 +1185,7 @@ fn a_document_of_many_paragraphs_lays_out_in_linear_time() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn many_hidden_float_anchors_lay_out_in_linear_time() {
crate::test_fonts();
// n square-wrapped shapes, each anchored in its own hidden run (bold alternating, so runs do not
+1
View File
@@ -117,6 +117,7 @@ fn resolve_uses_the_given_system_locale() {
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn context_mode_on_a_very_long_digit_run_stays_linear() {
crate::test_fonts();
// Every digit used to scan back over all the digits and spaces before it (O(n²)).
+1 -1
View File
@@ -145,7 +145,7 @@ pub fn tool_definitions() -> Value {
tool(
"inspect_document",
"Inspect document",
"Structure: blocks with text, styles, formatting runs, lists, tables, sections, parts, selection, pages.",
"Structure: blocks with text, styles, formatting runs, lists, tables, sections, parts (headers, footers, notes: their text and fields; a field with perPage true, such as PAGE, NUMPAGES or STYLEREF, shows its own value on each page, so its stored result is not what every page shows: render_page shows it; fieldsTruncated means a part has more than the 200 fields listed; a range field's result, such as a TOC's, is cut at 2000 characters, with resultTruncated), selection, pages.",
with_doc(obj(json!({}), &[])),
true
),
+3
View File
@@ -390,6 +390,7 @@ fn cost_ratio(scale: usize, mut small: impl FnMut(), mut big: impl FnMut()) -> (
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn long_fragmented_hidden_headings_export_in_linear_time() {
// Headings of n chars in one-char runs: all hidden with bold alternating, and hidden and visible
// alternating. Layout and the bookmark/tag titles once scanned every left-out range for every
@@ -1917,6 +1918,7 @@ fn comments_print_in_balloons_beside_scaled_pages() {
/// references) exports in time proportional to its length: each link looked its bookmark up by
/// walking the whole document, so NIH's 67-page protocol template took 10.6 s (0.9 s now).
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn internal_links_export_in_linear_time() {
let doc = |n: usize| {
let mut d = Document::new();
@@ -3121,6 +3123,7 @@ fn only_safe_link_targets_become_annotations() {
/// without one), found in one pass: a long run of glyphs sharing a cluster used to rescan the rest
/// of the run for every glyph.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn cluster_ends_match_the_next_larger_cluster_in_linear_time() {
let brute = |cl: &[usize], len: usize| -> Vec<usize> {
(0..cl.len()).map(|i| cl.iter().skip(i + 1).copied().find(|n| *n > cl[i]).unwrap_or(len)).collect::<Vec<_>>()
+3 -3
View File
@@ -9,14 +9,14 @@ description = "WordCraft egui front end (Word-style ribbon UI); swappable: nothi
workspace = true
[dependencies]
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a" }
# Interface translations: lookup, fallback, plurals and system-locale matching over the craft-i18n
# catalogs that `build.rs` embeds from `CRAFT_I18N_DIR` (storytold/craft-i18n, pinned in
# `craft-i18n.rev`). Same craft-libs rev as craft-ui; it brings lazy per-locale parsing
# (storytold/craft-libs#247), ICU4X plural rules and bidi isolates around inserted values.
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a" }
# One native window per open document: the window list, the Window menu and the egui viewports.
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["egui"] }
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "93dac78c93a7f1fd66e2022edeb089233b1dc45a", features = ["egui"] }
wordcraft-geom = { workspace = true, features = ["image"] }
wordcraft-doc = { workspace = true }
wordcraft-fonts = { workspace = true }
+2
View File
@@ -110,6 +110,8 @@ impl WordApp {
integrated_titlebar: false,
mac_titlebar: chrome::MacTitleBar::default(),
updates_rt: updates_ui::Runtime::default(),
font_notice_seen: Vec::new(),
font_notices: false,
packs_rt: Default::default(),
control_rx: None,
shots: crate::control::Shots::default(),
+1
View File
@@ -129,6 +129,7 @@ impl WordApp {
let window = self.windows.shown();
let moved = |app: &WordApp| app.windows.shown() != window;
content_controls::popup(self, ctx);
crate::font_download::tick(self);
dialogs::show(self, ctx);
if moved(self) {
return;
+4
View File
@@ -853,6 +853,10 @@ pub fn status_bar(app: &mut WordApp, ui: &mut Ui) {
ui.label(crate::i18n::tn((count - SHOWN) as u64, "…and {n} more", "…and {n} more"));
}
ui.separator();
if missing.iter().any(|f| f.download.is_some()) && ui.button(tl!("Get the Missing Fonts…")).clicked() {
let _ = app.run("ui.dialog", json!({"name": "fontDownload"}));
ui.close();
}
if ui.button(tl!("Add Font…")).clicked() {
let _ = app.run("font.add", json!({}));
ui.close();
+11
View File
@@ -226,6 +226,17 @@ fn handle_in(app: &mut WordApp, doc: u64, method: &str, p: &Value) -> Outcome {
/// drag, key, wheel and text input, screenshots and renders, resize, focus and quit, and unknown
/// ids as commands. It refuses closing unsaved work and document operations without a document.
pub fn handle(app: &mut WordApp, ctx: &egui::Context, req: &ControlRequest) -> Outcome {
let before = app.session.document_id();
let outcome = handle_request(app, ctx, req);
// A document a script opened doesn't bring up the missing-fonts box.
let after = app.session.document_id();
if after != before {
crate::font_download::skip(app, after);
}
outcome
}
fn handle_request(app: &mut WordApp, ctx: &egui::Context, req: &ControlRequest) -> Outcome {
let p = &req.params;
if DOC_METHODS.contains(&req.method.as_str()) {
match addressed(app, p) {
+31 -2
View File
@@ -42,6 +42,7 @@ pub(crate) use find::swallow_shortcut_text;
pub use find::{FindExtra, FindTab, GoToWhat};
#[cfg(test)]
use find::{command_matches, goto_page};
pub use font::font_for_bullet;
#[cfg(test)]
use font::{apply_font, preview_size};
use font::{east_asian_char, font_props};
@@ -97,6 +98,11 @@ pub enum Dialog {
/// alone stays as it is.
#[serde(skip)]
start: serde_json::Map<String, Value>,
/// Formatting a bullet for Define New Bullet (#2786): OK hands the font, size, bold,
/// italic and colour back to that dialog instead of formatting the text, and Cancel goes
/// back to it unchanged.
#[serde(skip)]
bullet: Option<Box<crate::dialogs_bullet::DefineBulletForm>>,
},
Paragraph {
/// Right-to-left reading order (Direction in Word's Paragraph dialog).
@@ -315,6 +321,12 @@ pub enum Dialog {
#[serde(skip)]
note: String,
},
/// The document's missing fonts that can be downloaded, and how ([`crate::font_download`]).
FontDownload {
fonts: Vec<crate::font_download::Row>,
/// "Don't show this again".
dont_show: bool,
},
/// Tabs: 0 About, 1 Contributors, 2 Models.
About {
tab: u8,
@@ -488,14 +500,27 @@ pub enum Dialog {
}
impl Dialog {
/// The Define New Bullet form a Font or Symbol dialog opened from it carries (#2786).
fn bullet_form(&mut self) -> Option<crate::dialogs_bullet::DefineBulletForm> {
match self {
Dialog::Font { bullet, .. } => bullet.take().map(|b| *b),
Dialog::Insert(i) => match i.as_mut() {
crate::dialogs_insert::InsertDialog::Symbol(f) => f.bullet.take().map(|b| *b),
_ => None,
},
_ => None,
}
}
/// Whether the dialog blocks the rest of the window while it is open. As in Word for Mac
/// (16.114.1, #3859), all are but those you keep working beside.
/// (16.114.1, #3859), all are but those you keep working beside. Symbol opened from Define
/// New Bullet picks the bullet for it, so it blocks the window as that dialog does (#2786).
pub fn is_modal(&self) -> bool {
use crate::dialogs_insert::InsertDialog;
use crate::dialogs_refs::RefsDialog;
match self {
Dialog::Find { .. } | Dialog::InsertMergeField { .. } => false,
Dialog::Insert(d) => !matches!(**d, InsertDialog::Symbol(_)),
Dialog::Insert(d) => !matches!(&**d, InsertDialog::Symbol(f) if f.bullet.is_none()),
Dialog::Refs(d) => !matches!(**d, RefsDialog::MarkEntry(_) | RefsDialog::CrossRef(_)),
_ => true,
}
@@ -531,6 +556,7 @@ impl Dialog {
Dialog::Icons { .. } => "icons",
Dialog::PasteSpecial { .. } => "pasteSpecial",
Dialog::About { .. } => "about",
Dialog::FontDownload { .. } => "fontDownload",
Dialog::SaveChanges { .. } => "saveChanges",
Dialog::DiscardRecovery { .. } => "discardRecovery",
Dialog::RecipientList { .. } => "recipientList",
@@ -634,6 +660,7 @@ impl Dialog {
kern: kern.map(|k| k > 0.0),
kern_at: kern.filter(|k| *k > 0.0).unwrap_or(12.0) as f32,
start: serde_json::Map::new(),
bullet: None,
};
let props = font_props(&d);
if let Dialog::Font { start, .. } = &mut d {
@@ -783,6 +810,7 @@ impl Dialog {
"pasteSpecial" => Dialog::paste_special(app, &json!({})),
"renameDocument" => Dialog::RenameDocument { name: app.title_stem(), message: String::new(), focus: true },
"about" => Dialog::About { tab: 0 },
"fontDownload" => crate::font_download::open(app),
"contributors" => Dialog::About { tab: 1 },
"models" => Dialog::About { tab: 2 },
"languageData" => Dialog::About { tab: 3 },
@@ -905,6 +933,7 @@ impl Dialog {
}
}
pub mod dialogs_bullet;
pub mod dialogs_chart;
pub mod dialogs_controls;
pub mod dialogs_insert;
+16 -1
View File
@@ -188,6 +188,7 @@ pub fn show(app: &mut WordApp, ctx: &egui::Context) {
Dialog::Icons { .. } => "Icons",
Dialog::PasteSpecial { .. } => "Paste Special",
Dialog::About { .. } => "About WordCraft",
Dialog::FontDownload { .. } => "Get the Missing Fonts",
Dialog::SaveChanges { .. } => "WordCraft",
Dialog::DiscardRecovery { .. } => "Discard recovery copy",
Dialog::RecipientList { .. } => "Recipient List",
@@ -261,7 +262,14 @@ pub fn show(app: &mut WordApp, ctx: &egui::Context) {
});
}
count_dialog_frame(ctx, id);
close |= crate::window_close::show(ctx, &shown.response, &Tokens::get(ctx));
if crate::window_close::show(ctx, &shown.response, &Tokens::get(ctx)) && !close {
close = true;
// The title bar's close box is Cancel: a dialog opened from Define New Bullet goes
// back to it, as its Cancel button does (#2786).
if back_to_bullet(app, &mut d) {
return;
}
}
// A file browser it opened (Attach…, Browse…) stays above it.
if d.is_modal() && !close && !app.file_browser_open() {
block_behind(app, ctx, &shown.response);
@@ -278,6 +286,13 @@ pub fn show(app: &mut WordApp, ctx: &egui::Context) {
}
}
/// Back to Define New Bullet from a Font or Symbol dialog it opened, when `d` is one (#2786).
fn back_to_bullet(app: &mut WordApp, d: &mut Dialog) -> bool {
let Some(form) = d.bullet_form() else { return false };
app.dialog = Some(Dialog::Insert(Box::new(crate::dialogs_insert::InsertDialog::DefineBullet(form))));
true
}
/// Cancel key for dialogs: Escape everywhere, plus ⌘. (Command+Period) on macOS.
pub(crate) fn cancel_key_pressed(ui: &Ui) -> bool {
ui.input(|i| i.key_pressed(egui::Key::Escape) || (cfg!(target_os = "macos") && i.modifiers.command && i.key_pressed(egui::Key::Period)))
@@ -0,0 +1,370 @@
//! Define New Bullet (Home › Paragraph › Bullets, #2786): the bullet character (picked in the
//! Symbol dialog), its font (in the app's Font dialog) and its alignment, with a preview. It
//! ends in one command, `para.defineBullet`, so agents get the same result without the dialog.
use egui::{Ui, vec2};
use serde::Serialize;
use serde_json::json;
use wordcraft_doc::props::Align;
use crate::WordApp;
use crate::dialogs::Dialog;
use crate::dialogs_insert::{InsertDialog, SymbolForm};
use crate::theme::{Tokens, regular, semibold};
/// The alignments the dialog offers: `para.defineBullet`'s `align` and their names.
pub const ALIGNMENTS: [(&str, &str); 3] = [("left", "Left"), ("center", "Center"), ("right", "Right")];
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DefineBulletForm {
/// The bullet character.
pub ch: String,
/// The bullet's own font; empty = the text's.
pub font: String,
/// The bullet's size in points; empty = the text's.
pub size: String,
pub bold: bool,
pub italic: bool,
/// `RRGGBB`; empty or `auto` = the text's colour.
pub color: String,
/// `left`, `center` or `right`.
pub align: String,
}
impl DefineBulletForm {
/// Starts from the bullet the caret's paragraph has (its level's character, font and
/// alignment), or a plain • when it has none.
pub fn new(app: &WordApp) -> DefineBulletForm {
let doc = &app.session.doc;
let level = doc
.para_at(&app.session.sel.focus)
.and_then(|p| doc.styles.resolve_para(&p.props).numbering)
.filter(|n| n.num != 0)
.and_then(|n| doc.numbering.level(n.num, n.level))
.filter(|l| l.format == wordcraft_doc::section::NumFormat::Bullet && l.text.chars().count() == 1);
match level {
Some(l) => DefineBulletForm {
ch: l.text.clone(),
font: l.chr.font.clone().unwrap_or_default(),
size: l.chr.size.map(|s| s.to_string()).unwrap_or_default(),
bold: l.chr.bold.unwrap_or(false),
italic: l.chr.italic.unwrap_or(false),
color: match l.chr.color {
Some(wordcraft_doc::props::TextColor::Rgb(c)) => c.hex(),
_ => String::new(),
},
align: match l.align {
Align::Center => "center",
Align::Right => "right",
_ => "left",
}
.into(),
},
None => DefineBulletForm::plain("•"),
}
}
/// A bullet `ch` in the text's own formatting, left aligned.
pub fn plain(ch: &str) -> DefineBulletForm {
DefineBulletForm {
ch: ch.into(),
font: String::new(),
size: String::new(),
bold: false,
italic: false,
color: String::new(),
align: "left".into(),
}
}
/// The bullet's formatting as the preview draws it, at [`Self::preview_pt`].
pub fn chr(&self) -> wordcraft_doc::props::CharProps {
wordcraft_doc::props::CharProps {
size: Some(self.preview_pt()),
font: (!self.font.is_empty()).then(|| self.font.clone()),
bold: self.bold.then_some(true),
italic: self.italic.then_some(true),
color: wordcraft_doc::Rgb::parse(&self.color).map(wordcraft_doc::props::TextColor::Rgb),
..Default::default()
}
}
/// The bullet's size in the preview, in points: its own size, or the text's (11 pt) when it
/// has none, kept to what the preview's rows can show (4–20 pt).
pub fn preview_pt(&self) -> f32 {
let pt = self.size.trim().parse::<f32>().ok().filter(|pt| pt.is_finite() && (1.0..=1638.0).contains(pt)).unwrap_or(11.0);
pt.clamp(4.0, 20.0)
}
/// The `para.defineBullet` params this form stands for.
pub fn params(&self) -> serde_json::Value {
let mut v = json!({"char": self.ch, "font": self.font, "align": self.align, "bold": self.bold, "italic": self.italic});
// No size is the text's size; anything else goes to the command, which rejects a bad one.
let size = self.size.trim();
if !size.is_empty() {
v["size"] = match size.parse::<f64>() {
Ok(pt) if pt.is_finite() => json!(pt),
_ => json!(size),
};
}
v["color"] = json!(if wordcraft_doc::Rgb::parse(&self.color).is_some() { self.color.as_str() } else { "auto" });
v
}
}
/// Draws the dialog; returns true to close it (OK, Cancel, or Symbol… / Font…, which hand the
/// dialog over to the Symbol or Font dialog and get it back when it closes).
pub fn body(app: &mut WordApp, ui: &mut Ui, f: &mut DefineBulletForm) -> bool {
let t = Tokens::get(ui.ctx());
ui.set_width(340.0);
ui.label(egui::RichText::new(tl!("Bullet character")).font(semibold(12.0)));
ui.add_space(4.0);
let (mut to_symbol, mut to_font) = (false, false);
ui.horizontal(|ui| {
to_symbol = ui.button(tl!("Symbol…")).clicked();
to_font = ui.button(tl!("Font…")).clicked();
});
if to_symbol {
let form = f.clone();
app.dialog = Some(Dialog::Insert(Box::new(InsertDialog::Symbol(SymbolForm::for_bullet(app, form)))));
return true;
}
if to_font {
// The app's own Font dialog, formatting the bullet and handing it back.
if let Some(d) = crate::dialogs::font_for_bullet(app, f.clone()) {
app.dialog = Some(d);
return true;
}
}
ui.add_space(6.0);
ui.horizontal(|ui| {
ui.label(tl!("Alignment:"));
ui.add_space(6.0);
for (k, n) in ALIGNMENTS {
ui.radio_value(&mut f.align, k.to_string(), tl!(n));
}
});
ui.add_space(8.0);
ui.label(egui::RichText::new(tl!("Preview")).font(regular(12.0)).color(t.text_dim));
preview(app, ui, f);
ui.add_space(6.0);
let (ok, cancel) = crate::dialogs_insert::buttons(ui, "OK", "Cancel", f.ch.chars().count() == 1);
if ok && let Err(e) = app.run("para.defineBullet", f.params()) {
// A bad setting (a size out of range…) keeps the dialog open to correct it.
app.status(e);
return false;
}
ok || cancel
}
/// The preview, drawn like Define New Multilevel List's: two plain lines of text, three bulleted
/// ones and two plain lines again, as grey bars on a white page. The bullet sits at the first
/// level's aligned-at position (18 pt), aligned there as chosen, and its text starts at the text
/// indent (36 pt); the bullet itself is laid out by the layout engine, so a symbol font shows the
/// character the page will.
fn preview(app: &mut WordApp, ui: &mut Ui, f: &DefineBulletForm) {
let t = Tokens::get(ui.ctx());
let row = 17.0;
let (rect, _) = ui.allocate_exact_size(vec2(340.0, row * 7.0 + 10.0), egui::Sense::hover());
let p = ui.painter_at(rect);
p.rect_filled(rect, 2.0, egui::Color32::WHITE);
p.rect_stroke(rect, 2.0, egui::Stroke::new(1.0, t.border_strong), egui::StrokeKind::Inside);
// Positions in points, scaled so 4.5 inches fit, as in Define New Multilevel List.
let scale = (rect.width() - 16.0) / 324.0;
let left = rect.left() + 8.0;
let (aligned_at, text_at) = (left + 18.0 * scale, left + 36.0 * scale);
let bar = |y: f32, from: f32, gray: u8| {
let r = egui::Rect::from_min_max(egui::pos2(from, y - 2.0), egui::pos2(rect.right() - 8.0, y + 2.0));
p.rect_filled(r, 1.0, egui::Color32::from_gray(gray));
};
// Room for the character at its size (`previews::glyph` draws it at `chr`'s size).
let chr = wordcraft_doc::props::CharProps { size: Some(f.preview_pt() * scale), ..f.chr() };
let glyph = f.preview_pt() * scale / 0.7;
for i in 0..7 {
let y = rect.top() + 5.0 + row * i as f32 + row / 2.0;
if !(2..5).contains(&i) {
bar(y, left, 205);
continue;
}
let x = match f.align.as_str() {
"center" => aligned_at - glyph / 2.0,
"right" => aligned_at - glyph,
_ => aligned_at,
};
let r = egui::Rect::from_min_size(egui::pos2(x, y - glyph / 2.0), vec2(glyph, glyph));
crate::previews::glyph(app, ui, r, &f.ch, &chr, egui::Color32::WHITE);
bar(y, text_at.max(r.right() + 2.0), 120);
}
}
#[cfg(test)]
mod tests {
use super::*;
use egui_kittest::kittest::Queryable;
use wordcraft_engine::Session;
/// The interface fonts take effect from the frame after they're installed: the first frame
/// installs them and draws nothing.
fn fonts_ready(ui: &Ui) -> bool {
let id = egui::Id::unique("define_bullet_test_fonts");
let ready = ui.ctx().data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
if !ready {
crate::theme::install_fonts(ui.ctx());
ui.ctx().data_mut(|d| d.insert_temp(id, true));
}
ready
}
fn app_with_bullet() -> WordApp {
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("One\nTwo")), crate::Services::default());
a.run("para.bullets", json!({"kind": "▪"})).unwrap();
a
}
fn level_at(app: &WordApp, i: usize) -> wordcraft_doc::numbering::Level {
let doc = &app.session.doc;
let n = doc.para_at(&wordcraft_doc::Pos::body(i, 0)).unwrap().props.numbering.unwrap();
doc.numbering.level(n.num, n.level).unwrap().clone()
}
/// The form starts from the caret's bullet; OK ends in `para.defineBullet` with the
/// character, its font and the alignment.
#[test]
fn ok_defines_the_bullet_the_form_shows() {
let app = app_with_bullet();
let f = DefineBulletForm::new(&app);
let font = level_at(&app, 0).chr.font.unwrap_or_default();
assert_eq!((f.ch.as_str(), f.font.as_str(), f.align.as_str()), ("▪", font.as_str(), "left"));
let mut h = egui_kittest::Harness::builder().with_size(vec2(500.0, 500.0)).build_ui_state(
|ui, (app, f): &mut (WordApp, DefineBulletForm)| {
if fonts_ready(ui) {
body(app, ui, f);
}
},
(app, DefineBulletForm { font: "Segoe UI Symbol".into(), align: "right".into(), ..DefineBulletForm::plain("†") }),
);
h.run_steps(2);
h.get_by_label("OK").click();
h.run_steps(2);
let l = level_at(&h.state().0, 0);
assert_eq!((l.text.as_str(), l.chr.font.as_deref(), l.align), ("†", Some("Segoe UI Symbol"), Align::Right));
}
/// #2786 review: a size the command rejects (not a number, out of range) is an error OK
/// reports, keeping the dialog open and the list as it was; no size is the text's size.
#[test]
fn ok_with_a_bad_size_keeps_the_dialog_open() {
assert!(DefineBulletForm::plain("†").params().get("size").is_none(), "no size: the text's size");
for bad in ["abc", "5000", "0"] {
let app = app_with_bullet();
let before = level_at(&app, 0);
let form = DefineBulletForm { size: bad.into(), ..DefineBulletForm::plain("†") };
let mut h = egui_kittest::Harness::builder().with_size(vec2(500.0, 500.0)).build_ui_state(
|ui, (app, f, closed): &mut (WordApp, DefineBulletForm, bool)| {
if fonts_ready(ui) && body(app, ui, f) {
*closed = true;
}
},
(app, form, false),
);
h.run_steps(2);
h.get_by_label("OK").click();
h.run_steps(2);
assert!(!h.state().2, "{bad}: the dialog stays open");
assert_eq!(level_at(&h.state().0, 0).text, before.text, "{bad}: the list is unchanged");
}
}
/// #2786 review: the preview draws the bullet at its own size, the text's when it has none,
/// within what the rows can show.
#[test]
fn the_preview_draws_the_bullets_size() {
let at = |size: &str| DefineBulletForm { size: size.into(), ..DefineBulletForm::plain("•") }.preview_pt();
assert_eq!(at(""), 11.0);
assert_eq!(at("16"), 16.0);
assert!(at("8") < at("11") && at("11") < at("18"));
assert_eq!(at("72"), 20.0, "kept to the rows");
assert_eq!(at("abc"), 11.0);
// The preview draws the character with these props: `previews::glyph` keeps their size.
let f = DefineBulletForm { size: "16".into(), ..DefineBulletForm::plain("•") };
assert_eq!(f.chr().size, Some(16.0));
}
/// Symbol… hands the form to the Symbol dialog, whose OK brings it back with the character
/// picked there; Cancel brings it back unchanged.
#[test]
fn symbol_picks_the_character_and_returns() {
let form = DefineBulletForm { align: "center".into(), ..DefineBulletForm::plain("▪") };
let mut h = egui_kittest::Harness::builder().with_size(vec2(500.0, 500.0)).build_ui_state(
|ui, (app, f): &mut (WordApp, DefineBulletForm)| {
if fonts_ready(ui) {
body(app, ui, f);
}
},
(app_with_bullet(), form.clone()),
);
h.run_steps(2);
h.get_by_label("Symbol…").click();
h.run_steps(2);
let mut app = std::mem::replace(&mut h.state_mut().0, app_with_bullet());
drop(h);
let Some(dialog) = app.dialog.take() else { panic!("the Symbol dialog is open") };
// Picking the bullet, it blocks the window as Define New Bullet does (#3859).
assert!(dialog.is_modal(), "the bullet's Symbol dialog is modal");
let Dialog::Insert(d) = dialog else { panic!("the Symbol dialog is open") };
let InsertDialog::Symbol(mut sym) = *d else { panic!("the Symbol dialog is open") };
assert_eq!(sym.bullet.as_deref().map(|b| b.align.as_str()), Some("center"), "it carries the form");
sym.selected = Some('†');
let mut h = egui_kittest::Harness::builder().with_size(vec2(700.0, 700.0)).build_ui_state(
|ui, (app, d): &mut (WordApp, InsertDialog)| {
if fonts_ready(ui) {
crate::dialogs_insert::body(app, ui, d);
}
},
(app, InsertDialog::Symbol(sym)),
);
h.run_steps(2);
h.get_by_label("OK").click();
h.run_steps(2);
let back = match h.state_mut().0.dialog.take() {
Some(Dialog::Insert(d)) => match *d {
InsertDialog::DefineBullet(f) => f,
_ => panic!("back to Define New Bullet"),
},
_ => panic!("back to Define New Bullet"),
};
assert_eq!((back.ch.as_str(), back.align.as_str()), ("†", "center"));
// Nothing was inserted into the text.
assert_eq!(h.state().0.session.doc.para_at(&wordcraft_doc::Pos::body(0, 0)).unwrap().text, "One");
}
/// The Symbol dialog's title-bar close box goes back to Define New Bullet, as Cancel does.
#[test]
fn closing_the_symbol_dialog_returns_to_the_bullet() {
let mut app = app_with_bullet();
let form = DefineBulletForm { align: "center".into(), ..DefineBulletForm::plain("▪") };
let sym = SymbolForm::for_bullet(&app, form);
app.dialog = Some(Dialog::Insert(Box::new(InsertDialog::Symbol(sym))));
let mut h = egui_kittest::Harness::builder().with_size(vec2(1000.0, 800.0)).build_ui_state(
|ui, app: &mut WordApp| {
if fonts_ready(ui) {
let ctx = ui.ctx().clone();
crate::dialogs::show(app, &ctx);
}
},
app,
);
h.run_steps(3);
h.get_by_label("Close").click();
h.run_steps(3);
let back = match h.state_mut().dialog.take() {
Some(Dialog::Insert(d)) => match *d {
InsertDialog::DefineBullet(f) => f,
_ => panic!("back to Define New Bullet"),
},
_ => panic!("back to Define New Bullet"),
};
assert_eq!((back.ch.as_str(), back.align.as_str()), ("▪", "center"));
}
}
+8 -2
View File
@@ -38,6 +38,7 @@ pub enum InsertDialog {
SmartArt(crate::dialogs_smart_art::SmartArtForm),
ChartType(crate::dialogs_chart::ChartTypeForm),
ChartData(crate::dialogs_chart::ChartDataForm),
DefineBullet(crate::dialogs_bullet::DefineBulletForm),
DropCap(DropCapForm),
}
@@ -53,6 +54,7 @@ impl InsertDialog {
InsertDialog::ChartType(f) if f.change => "changeChartType",
InsertDialog::ChartType(_) => "insertChart",
InsertDialog::ChartData(_) => "chartData",
InsertDialog::DefineBullet(_) => "defineBullet",
InsertDialog::DropCap(_) => "dropCap",
}
}
@@ -68,14 +70,16 @@ impl InsertDialog {
InsertDialog::ChartType(f) if f.change => "Change Chart Type",
InsertDialog::ChartType(_) => "Insert Chart",
InsertDialog::ChartData(_) => "Edit Data",
InsertDialog::DefineBullet(_) => "Define New Bullet",
InsertDialog::DropCap(_) => "Drop Cap",
}
}
}
/// The dialog `ui.dialog` opens by `name`: `columns`, `symbol`, `specialCharacters`, `field`,
/// `insertChart`, `changeChartType` or `chartData` (the last two for the selected chart), or
/// `insertSmartArt`, or `dropCap` (Drop Cap Options…, where the paragraph can take one).
/// `insertChart`, `changeChartType` or `chartData` (the last two for the selected chart),
/// `insertSmartArt`, `defineBullet` (Home › Bullets › Define New Bullet…), or `dropCap` (Drop Cap
/// Options…, where the paragraph can take one).
pub fn open(name: &str, app: &WordApp) -> Option<InsertDialog> {
Some(match name {
"dateTime" => InsertDialog::DateTime(DateTimeForm::new(app)),
@@ -87,6 +91,7 @@ pub fn open(name: &str, app: &WordApp) -> Option<InsertDialog> {
"insertChart" => InsertDialog::ChartType(crate::dialogs_chart::ChartTypeForm::new(app, false)?),
"changeChartType" => InsertDialog::ChartType(crate::dialogs_chart::ChartTypeForm::new(app, true)?),
"chartData" => InsertDialog::ChartData(crate::dialogs_chart::ChartDataForm::new(app)?),
"defineBullet" => InsertDialog::DefineBullet(crate::dialogs_bullet::DefineBulletForm::new(app)),
"dropCap" => InsertDialog::DropCap(DropCapForm::read(app)?),
_ => return None,
})
@@ -102,6 +107,7 @@ pub fn body(app: &mut WordApp, ui: &mut Ui, d: &mut InsertDialog) -> bool {
InsertDialog::SmartArt(f) => crate::dialogs_smart_art::picker_ui(app, ui, f),
InsertDialog::ChartType(f) => crate::dialogs_chart::type_ui(app, ui, f),
InsertDialog::ChartData(f) => crate::dialogs_chart::data_ui(app, ui, f),
InsertDialog::DefineBullet(f) => crate::dialogs_bullet::body(app, ui, f),
InsertDialog::DropCap(f) => drop_cap_ui(app, ui, f),
}
}
@@ -131,6 +131,10 @@ pub struct SymbolForm {
/// Scroll the grid to this row on the next frame.
#[serde(skip)]
scroll_to: Option<usize>,
/// Picking a bullet for Define New Bullet (#2786): OK hands the character and its font back
/// to that dialog instead of inserting it, and Cancel goes back to it unchanged.
#[serde(skip)]
pub bullet: Option<Box<crate::dialogs_bullet::DefineBulletForm>>,
}
/// Grid geometry.
@@ -151,12 +155,45 @@ impl SymbolForm {
let recent = app.ui.recent_symbols.first().cloned();
let font = recent.as_ref().map(|r| r.font.clone()).unwrap_or_default();
let selected = recent.and_then(|r| r.ch.chars().next());
let mut f =
SymbolForm { tab, font, selected, code: String::new(), special: 0, text_font, chars: Vec::new(), chars_of: None, scroll_to: None };
let mut f = SymbolForm {
tab,
font,
selected,
code: String::new(),
special: 0,
text_font,
chars: Vec::new(),
chars_of: None,
scroll_to: None,
bullet: None,
};
f.sync_code();
f
}
/// The Symbol dialog picking `form`'s bullet: it opens on the bullet's font and character.
pub fn for_bullet(app: &WordApp, form: crate::dialogs_bullet::DefineBulletForm) -> SymbolForm {
let mut f = SymbolForm::new(app, 0);
f.font = form.font.clone();
f.selected = form.ch.chars().next();
f.load();
f.sync_code();
f.scroll_to = f.selected.and_then(|c| f.row_of(c));
f.bullet = Some(Box::new(form));
f
}
/// Back to Define New Bullet, with `picked` (a character and its font) when one was chosen.
fn back_to_bullet(&mut self, app: &mut WordApp, picked: Option<(String, String)>) -> bool {
let Some(mut form) = self.bullet.take() else { return false };
if let Some((ch, font)) = picked {
form.ch = ch;
form.font = font;
}
app.dialog = Some(crate::dialogs::Dialog::Insert(Box::new(super::InsertDialog::DefineBullet(*form))));
true
}
/// The font the grid shows: the chosen one, or the text's.
pub fn shown_font(&self) -> &str {
if self.font.is_empty() { &self.text_font } else { &self.font }
@@ -269,11 +306,16 @@ fn symbol_cell(app: &mut WordApp, ui: &mut Ui, font: &str, c: char, selected: bo
pub(super) fn symbol_ui(app: &mut WordApp, ui: &mut Ui, f: &mut SymbolForm) -> bool {
let t = Tokens::get(ui.ctx());
ui.set_width(GRID_COLS as f32 * CELL + 24.0);
let names = [tl!("Symbols").to_string(), tl!("Special Characters").to_string()];
if let Some(k) = crate::widgets::dialog_tabs(ui, &names, f.tab as usize) {
f.tab = k as u8;
if f.bullet.is_some() {
// A bullet is one character in a font: the Special Characters tab has nothing for it.
f.tab = 0;
} else {
let names = [tl!("Symbols").to_string(), tl!("Special Characters").to_string()];
if let Some(k) = crate::widgets::dialog_tabs(ui, &names, f.tab as usize) {
f.tab = k as u8;
}
ui.add_space(6.0);
}
ui.add_space(6.0);
let mut insert: Option<(Value, String, String)> = None;
if f.tab == 1 {
if special_ui(app, ui, f, &mut insert, &t) {
@@ -326,8 +368,10 @@ pub(super) fn symbol_ui(app: &mut WordApp, ui: &mut Ui, f: &mut SymbolForm) -> b
let rows = f.chars.len().div_ceil(GRID_COLS);
let mut area = egui::ScrollArea::vertical().id_salt("symbol_grid").max_height(CELL * GRID_ROWS as f32).auto_shrink([false, false]);
if let Some(row) = f.scroll_to.take() {
// Keep the row in view: it lands on the grid's second row when there is room.
area = area.vertical_scroll_offset(row.saturating_sub(1) as f32 * CELL);
// Keep the row in view: it lands on the grid's second row when there is room. Rows
// are `CELL` plus the item spacing apart (`show_rows` adds it between rows).
let pitch = CELL + ui.spacing().item_spacing.y;
area = area.vertical_scroll_offset(row.saturating_sub(1) as f32 * pitch);
}
let font = f.shown_font().to_string();
let chars = f.chars.clone();
@@ -398,15 +442,23 @@ pub(super) fn symbol_ui(app: &mut WordApp, ui: &mut Ui, f: &mut SymbolForm) -> b
if double && let Some(p) = f.params() {
insert = Some((p, f.selected.map(String::from).unwrap_or_default(), f.font.clone()));
}
// The Insert button.
let (ok, close) = buttons(ui, "Insert", "Close", f.selected.is_some());
// The Insert button (OK while picking a bullet).
let (ok_label, close_label) = if f.bullet.is_some() { ("OK", "Cancel") } else { ("Insert", "Close") };
let (ok, close) = buttons(ui, ok_label, close_label, f.selected.is_some());
if ok && let Some(p) = f.params() {
insert = Some((p, f.selected.map(String::from).unwrap_or_default(), f.font.clone()));
}
if close {
f.back_to_bullet(app, None);
return true;
}
}
if f.bullet.is_some()
&& let Some((_, ch, font)) = insert
{
remember_symbol(app, &ch, &font);
return f.back_to_bullet(app, Some((ch, font)));
}
if let Some((params, ch, font)) = insert {
// Special characters with their own command insert through it (non-breaking hyphen…).
let r = match params.get("command").and_then(Value::as_str) {
+4
View File
@@ -40,6 +40,7 @@ pub(super) fn body(app: &mut WordApp, ui: &mut Ui, d: &mut Dialog) -> bool {
Dialog::Icons { query, category, selected } => icons_body(app, ui, query, category, selected),
Dialog::PasteSpecial { .. } => paste_special_dialog(app, ui, d),
Dialog::About { .. } => about_dialog(ui, d),
Dialog::FontDownload { fonts, dont_show } => crate::font_download::body(app, ui, fonts, dont_show),
Dialog::DiscardRecovery { .. } => discard_recovery_dialog(app, ui, d),
Dialog::SaveChanges { .. } => save_changes_dialog(app, ui, d),
Dialog::RecipientList { fields, rows, message, filter } => recipient_list(app, ui, fields, rows, message, filter),
@@ -247,6 +248,9 @@ fn font_dialog(app: &mut WordApp, ui: &mut Ui, d: &mut Dialog) -> bool {
font_sample_box(app, ui, sample_paper(app), &sample_props, (*sup, *sub), *hidden);
});
let (ok, cancel) = buttons(ui, tl!("OK"));
if (ok || cancel) && super::font::hand_back_bullet(app, d, ok) {
return true;
}
if ok {
apply_font(app, d);
}
+42
View File
@@ -167,6 +167,48 @@ pub(super) fn east_asian_char(c: char) -> bool {
matches!(u32::from(c), 0x1100..=0x11FF | 0x2E80..=0x9FFF | 0xA960..=0xA97F | 0xAC00..=0xD7FF | 0xF900..=0xFAFF | 0xFE30..=0xFE4F | 0xFF00..=0xFFEF | 0x20000..=0x3FFFF)
}
/// The Font dialog formatting Define New Bullet's bullet (#2786): it opens on the bullet's font,
/// size, bold, italic and colour, and carries the form back (see `Dialog::Font::bullet`).
pub fn font_for_bullet(app: &mut WordApp, form: crate::dialogs_bullet::DefineBulletForm) -> Option<Dialog> {
let mut d = Dialog::open("font", app)?;
if let Dialog::Font { font, font_was, size, bold, italic, color, .. } = &mut d {
if !form.font.is_empty() {
*font = app.previews.font_shown(&form.font);
*font_was = font.clone();
}
*size = form.size.clone();
*bold = form.bold;
*italic = form.italic;
*color = form.color.clone();
}
let props = font_props(&d);
if let Dialog::Font { start, bullet, .. } = &mut d {
*start = props;
*bullet = Some(Box::new(form));
}
Some(d)
}
/// The Font dialog's OK or Cancel while it formats Define New Bullet's bullet (#2786): back to
/// that dialog, with the font (its stored English name, and only when the user changed it, so a
/// bullet in the text's font keeps following the text), size, bold, italic and colour on OK.
/// False when the dialog isn't formatting a bullet.
pub(super) fn hand_back_bullet(app: &mut WordApp, d: &mut Dialog, ok: bool) -> bool {
let Dialog::Font { bullet, font, font_was, size, bold, italic, color, .. } = d else { return false };
let Some(mut form) = bullet.take() else { return false };
if ok {
if font.trim() != font_was.trim() {
form.font = app.previews.font_stored(font.trim());
}
form.size = size.trim().to_string();
form.bold = *bold;
form.italic = *italic;
form.color = color.clone();
}
app.dialog = Some(Dialog::Insert(Box::new(crate::dialogs_insert::InsertDialog::DefineBullet(*form))));
true
}
/// The Font dialog's OK: one formatting step that sets only the properties the user changed, so
/// formatting the dialog can't show or left alone stays as it is. Fonts are set by their stored
/// (English) names, the Latin font for Persian/Arabic text too (as `format.font`).
+82
View File
@@ -895,6 +895,88 @@ fn modify_style_font_box_finds_the_family() {
assert_eq!(rc.font, wordcraft_doc::styles::HEADING_FONT);
}
/// #2786: Define New Bullet's Font… is this Font dialog, opened on the bullet's formatting. OK
/// hands the font, size and bold back to Define New Bullet without formatting the text;
/// Cancel, and the title bar's close box, go back with the form as it was.
#[test]
fn font_dialog_formats_a_bullet_and_hands_it_back() {
use crate::dialogs_bullet::DefineBulletForm;
use crate::dialogs_insert::InsertDialog;
use egui_kittest::kittest::Queryable;
let back = |button: &str| -> (DefineBulletForm, wordcraft_doc::props::CharProps) {
let mut app = app_with("Example");
let _ = app.run("para.bullets", json!({"kind": "▪"}));
let form = DefineBulletForm { font: "Arial".into(), ..DefineBulletForm::plain("▪") };
app.dialog = font_for_bullet(&mut app, form);
let Some(Dialog::Font { size, bold, bullet, .. }) = &mut app.dialog else { panic!("the Font dialog is open") };
assert!(bullet.is_some(), "it carries the form");
assert!(!*bold && size.is_empty(), "it shows the bullet's formatting, not the text's");
*size = "16".into();
*bold = true;
let mut fonts = false;
let mut h = egui_kittest::Harness::builder().with_size(vec2(1440.0, 900.0)).build_ui_state(
move |ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
if fonts {
show(app, &ctx);
} else {
crate::theme::install_fonts_for(&ctx, false);
fonts = true;
}
},
app,
);
h.run_steps(3);
h.get_by_label(button).click();
h.run_steps(3);
let app = h.state_mut();
let Some(Dialog::Insert(d)) = app.dialog.take() else { panic!("back to Define New Bullet") };
let InsertDialog::DefineBullet(f) = *d else { panic!("back to Define New Bullet") };
let text = app.session.doc.para_at(&wordcraft_doc::Pos::body(0, 0)).map(|p| p.props_of_char(0).clone()).unwrap_or_default();
(f, text)
};
let (f, text) = back("OK");
assert_eq!((f.ch.as_str(), f.font.as_str(), f.size.as_str(), f.bold), ("▪", "Arial", "16", true));
assert_ne!(text.bold, Some(true), "the text isn't formatted");
assert_ne!(text.size, Some(16.0), "the text isn't formatted");
for button in ["Cancel", "Close"] {
let (f, _) = back(button);
assert_eq!((f.font.as_str(), f.size.as_str(), f.bold), ("Arial", "", false), "{button} keeps the form");
}
// A bullet in the text's font (no font of its own) keeps it when OK doesn't change the
// font box, though the box shows the text's font (review of #2860).
let mut app = app_with("Example");
let _ = app.run("para.bullets", json!({"kind": "▪"}));
let Some(mut d) = font_for_bullet(&mut app, DefineBulletForm::plain("▪")) else { panic!("the Font dialog opens") };
if let Dialog::Font { font, .. } = &d {
assert!(!font.is_empty(), "the box shows the text's font");
}
if let Dialog::Font { bullet: Some(b), font, font_was, .. } = &mut d {
assert_eq!(font, font_was);
assert!(b.font.is_empty());
}
app.dialog = Some(d);
let mut fonts = false;
let mut h = egui_kittest::Harness::builder().with_size(vec2(1440.0, 900.0)).build_ui_state(
move |ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
if fonts {
show(app, &ctx);
} else {
crate::theme::install_fonts_for(&ctx, false);
fonts = true;
}
},
app,
);
h.run_steps(3);
h.get_by_label("OK").click();
h.run_steps(3);
let Some(Dialog::Insert(d)) = h.state_mut().dialog.take() else { panic!("back to Define New Bullet") };
let InsertDialog::DefineBullet(f) = *d else { panic!("back to Define New Bullet") };
assert_eq!(f.font, "", "still the text's font");
}
/// A dialog was pinned by its anchor, so dragging its title bar did nothing (#732).
#[test]
fn dragging_a_dialog_title_bar_moves_it() {
+308
View File
@@ -0,0 +1,308 @@
//! "Get the missing fonts": a document opened from a file that asks for fonts their publisher
//! gives away (Microsoft's Aptos, the default in recent Word documents) is told about once, in a
//! box that names each font, the font drawn in its place, the publisher's download page and how
//! to install the download. "Don't show this again" turns the box off (File › Options › General
//! turns it back on); the status bar's missing-fonts menu opens it by hand.
//!
//! The list is `font.missing`'s ([`wordcraft_engine::cmd::font::missing_fonts`]), so agents read
//! the same fonts and pages without the box. WordCraft only links to the page: it never
//! downloads or bundles the fonts.
use serde::Serialize;
use crate::WordApp;
use crate::dialogs::Dialog;
use crate::theme::{Tokens, semibold};
/// Most documents the box has been considered for that are remembered: beyond that the oldest
/// are forgotten (a document shown again after hundreds of others may be told about twice).
const REMEMBERED: usize = 256;
/// Most fonts the box lists: a hostile file can name thousands.
const LISTED: usize = 12;
/// The height the box takes around what scrolls: the title bar, the row with Close and the
/// window's margins.
const ROOM_AROUND_SCROLL: f32 = 190.0;
/// The least height what scrolls gets, however short the window.
const SCROLL_MIN: f32 = 80.0;
/// One missing font the box lists.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Row {
/// The font the document asks for.
pub font: String,
/// The families its text is drawn in instead.
pub shown_as: Vec<String>,
/// The download the font comes in (a family: "Aptos" holds Aptos Display too).
pub download: String,
/// Who publishes the font and hosts the page.
pub publisher: String,
/// The publisher's download page.
pub url: String,
/// The publisher limits who may use the font: the box says so beside the download.
pub restricted: bool,
}
/// The shown document's missing fonts that can be downloaded.
pub(crate) fn rows(app: &mut WordApp) -> Vec<Row> {
let layout = app.session.layout();
let missing = app.cached_missing_fonts(&layout).0;
missing
.iter()
.filter_map(|f| {
let d = f.download?;
Some(Row {
font: f.font.clone(),
shown_as: f.shown_as.clone(),
download: d.name.clone(),
publisher: d.publisher.clone(),
url: d.url.clone(),
restricted: d.restricted,
})
})
.take(LISTED)
.collect()
}
/// The box for the shown document (`ui.dialog {"name": "fontDownload"}`), listing nothing when
/// none of its missing fonts can be downloaded.
pub(crate) fn open(app: &mut WordApp) -> Dialog {
Dialog::FontDownload { fonts: rows(app), dont_show: !app.ui.font_download_notice }
}
/// The document `id` has been considered for the box, and is not considered again.
fn remember(app: &mut WordApp, id: u64) {
if app.font_notice_seen.len() >= REMEMBERED {
app.font_notice_seen.remove(0);
}
app.font_notice_seen.push(id);
}
/// A document a script opened is not told about: programmatic calls never open dialogs.
pub(crate) fn skip(app: &mut WordApp, id: u64) {
if !app.font_notice_seen.contains(&id) {
remember(app, id);
}
}
impl WordApp {
/// Bring the box up by itself when a document opened from a file is missing fonts that can be
/// downloaded. The desktop and web apps call this once at start-up; unit tests and `ui_shot`
/// don't, so what they show doesn't depend on the fonts installed where they run.
pub fn tell_about_font_downloads(&mut self) {
self.font_notices = true;
}
}
/// Show the box once for a document opened from a file, when it is missing fonts that can be
/// downloaded and the box hasn't been turned off. Called every frame; it waits for an open
/// dialog or the File tab to close rather than replace them.
pub(crate) fn tick(app: &mut WordApp) {
let id = app.session.document_id();
if !app.font_notices || app.font_notice_seen.contains(&id) || app.dialog.is_some() || app.ui.backstage {
return;
}
remember(app, id);
// A new document has no file: its fonts are WordCraft's choice, not a document's from elsewhere.
if !app.ui.font_download_notice || app.session.path.is_none() {
return;
}
let fonts = rows(app);
if !fonts.is_empty() {
app.dialog = Some(Dialog::FontDownload { fonts, dont_show: false });
}
}
/// How to install a downloaded font on this system, after the files are unpacked.
fn system_install_steps() -> &'static str {
if cfg!(target_arch = "wasm32") {
""
} else if cfg!(target_os = "windows") {
tl!("Or install it for every app: select the font files, right-click and choose Install, then restart WordCraft.")
} else if cfg!(target_os = "macos") {
tl!("Or install it for every app: double-click each font file and choose Install in Font Book, then restart WordCraft.")
} else {
tl!("Or install it for every app: copy the font files to the .local/share/fonts folder in your home folder, then restart WordCraft.")
}
}
/// The Close button (Escape too) at the far side, in a row of its own: the far-side layout
/// then takes one row's height and not the rest of the window's.
fn close_row(ui: &mut egui::Ui) -> bool {
let mut close = ui.input(|i| i.key_pressed(egui::Key::Escape));
ui.horizontal(|ui| {
ui.with_layout(crate::rtl::far_side(ui, egui::Align::Center), |ui| {
close |= ui.add(crate::widgets::primary_button(ui, tl!("Close"))).clicked();
});
});
close
}
/// The fonts one download brings: listed once, however many of its fonts are missing.
struct Group<'a> {
/// Its missing fonts.
fonts: Vec<&'a str>,
/// The families they are drawn in instead, each once.
shown_as: Vec<&'a str>,
publisher: &'a str,
url: &'a str,
/// The publisher limits who may use them.
restricted: bool,
}
/// `fonts` by download page, in the order first met.
fn groups(fonts: &[Row]) -> Vec<Group<'_>> {
let mut out: Vec<Group> = Vec::new();
for f in fonts {
let at = match out.iter().position(|g| g.url == f.url && g.publisher == f.publisher && g.restricted == f.restricted) {
Some(at) => at,
None => {
out.push(Group { fonts: Vec::new(), shown_as: Vec::new(), publisher: &f.publisher, url: &f.url, restricted: f.restricted });
out.len() - 1
}
};
if let Some(g) = out.get_mut(at) {
g.fonts.push(&f.font);
for s in &f.shown_as {
if !g.shown_as.contains(&s.as_str()) {
g.shown_as.push(s);
}
}
}
}
out
}
/// A step's number in a filled circle.
fn step_badge(ui: &mut egui::Ui, n: usize, t: &Tokens) {
let (r, _) = ui.allocate_exact_size(egui::vec2(20.0, 20.0), egui::Sense::hover());
ui.painter().circle_filled(r.center(), 10.0, t.accent);
ui.painter().text(r.center(), egui::Align2::CENTER_CENTER, n.to_string(), semibold(12.0), t.on_accent);
}
/// The box's content: a headline, a card to a download with its button, the two steps that
/// add the downloaded font (Add Font… beside the step that uses it), then the opt-out and Close.
/// Returns true to close.
pub(crate) fn body(app: &mut WordApp, ui: &mut egui::Ui, fonts: &[Row], dont_show: &mut bool) -> bool {
ui.set_width(520.0);
if fonts.is_empty() {
ui.label(tl!("None of the fonts this document is missing can be downloaded free of charge from their publisher."));
ui.add_space(8.0);
return close_row(ui);
}
let t = Tokens::get(ui.ctx());
let mut open_url = None;
let mut add = false;
// Everything above the row with Close scrolls when the window is too short for it, so
// that row is on screen and the rest in reach however many downloads there are.
let room = (ui.ctx().content_rect().height() - ROOM_AROUND_SCROLL).max(SCROLL_MIN);
egui::ScrollArea::vertical().id_salt("font_downloads").max_height(room).show(ui, |ui| {
ui.horizontal(|ui| {
let (r, _) = ui.allocate_exact_size(egui::vec2(34.0, 34.0), egui::Sense::hover());
crate::icons::paint(ui.painter(), r, "fontMissing", t.icon, t.accent);
ui.add_space(4.0);
ui.vertical(|ui| {
ui.spacing_mut().item_spacing.y = 3.0;
// `fonts` holds only the missing fonts that can be downloaded: the document may
// be missing others, so the headline counts downloads, not missing fonts.
let headline = crate::i18n::tn(fonts.len() as u64, "{n} missing font can be downloaded", "{n} missing fonts can be downloaded");
ui.label(egui::RichText::new(headline).font(semibold(15.0)));
let note =
tl!("Text in a missing font is shown in another font, so it may not look as it was written. The downloads are free of charge.");
ui.label(egui::RichText::new(note).color(t.text_dim));
});
});
ui.add_space(12.0);
for g in groups(fonts) {
egui::Frame::NONE
.fill(t.panel)
.stroke(egui::Stroke::new(1.0, t.border))
.corner_radius(8)
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
ui.set_width(ui.available_width());
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.spacing_mut().item_spacing.y = 2.0;
// A label to a name: each is the document's own word, not interface text.
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
for (i, font) in g.fonts.iter().enumerate() {
if i > 0 {
ui.label(egui::RichText::new(", ").font(semibold(14.0)));
}
ui.label(egui::RichText::new(*font).font(semibold(14.0)));
}
});
let shown = crate::i18n::fmt(
tl!("Shown as {fonts} · {publisher}"),
&[("fonts", &g.shown_as.join(", ")), ("publisher", g.publisher)],
);
ui.label(egui::RichText::new(shown).color(t.text_dim));
if g.restricted {
let limits = tl!("Its licence limits who may use it: read the terms first.");
ui.label(egui::RichText::new(limits).small().color(t.text_dim));
}
});
ui.with_layout(crate::rtl::far_side(ui, egui::Align::Center), |ui| {
let tip = crate::i18n::fmt(tl!("Download from {publisher}"), &[("publisher", g.publisher)]);
if ui.add(crate::widgets::primary_button(ui, tl!("Download"))).on_hover_text(format!("{tip}\n{}", g.url)).clicked() {
open_url = Some(g.url.to_string());
}
});
});
});
ui.add_space(6.0);
}
if open_url.is_some() {
// The publisher's page, in the browser: WordCraft downloads nothing itself.
app.canvas.open_url = open_url;
}
// Whose part the licence is: WordCraft only points at the publisher's page.
let terms = tl!("Fonts come with licence terms. Read the publisher's terms on the download page: following them is up to you.");
ui.label(egui::RichText::new(terms).small().color(t.text_dim));
ui.add_space(12.0);
ui.label(egui::RichText::new(tl!("Then add it to WordCraft")).font(semibold(14.0)));
ui.add_space(4.0);
ui.horizontal(|ui| {
step_badge(ui, 1, &t);
ui.label(tl!("Download the font. If it comes as a .zip file, unpack it."));
});
ui.add_space(2.0);
ui.horizontal(|ui| {
step_badge(ui, 2, &t);
ui.label(tl!("Pick the font files:"));
add = ui.button(tl!("Add Font…")).clicked();
});
let system = system_install_steps();
if !system.is_empty() {
ui.add_space(6.0);
ui.label(egui::RichText::new(system).small().color(t.text_dim));
}
});
ui.add_space(12.0);
ui.separator();
ui.add_space(8.0);
// The downloads are the accent buttons; Close (Escape too) is plain beside them.
let mut close = ui.input(|i| i.key_pressed(egui::Key::Escape));
ui.horizontal(|ui| {
if ui.checkbox(dont_show, tl!("Don't show this again")).changed() {
// Kept at once, however the box is closed.
app.ui.font_download_notice = !*dont_show;
}
ui.with_layout(crate::rtl::far_side(ui, egui::Align::Center), |ui| {
close |= ui.button(tl!("Close")).clicked();
});
});
if add {
// The file picker replaces the box; the added font lays the document out again.
let _ = app.run("font.add", serde_json::json!({}));
return true;
}
close
}
#[cfg(test)]
mod tests;
+253
View File
@@ -0,0 +1,253 @@
use egui_kittest::kittest::Queryable;
use serde_json::json;
use wordcraft_engine::Session;
use super::*;
/// Aptos is installed on this machine: nothing is missing, so there is nothing to tell.
fn aptos_installed() -> bool {
wordcraft_fonts::FontDb::global().families().iter().any(|f| f.eq_ignore_ascii_case("Aptos"))
}
/// A document with text in Aptos (which Microsoft gives away) and in a font nobody has,
/// opened from a file when `opened`.
fn app(opened: bool) -> WordApp {
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
a.session.run("format.font", &json!({"name": "Aptos"})).unwrap();
a.session.run("text.insert", &json!({"text": "Lanterns"})).unwrap();
a.session.run("text.newParagraph", &json!({})).unwrap();
a.session.run("format.font", &json!({"name": "Wordcraft Nowhere Face"})).unwrap();
a.session.run("text.insert", &json!({"text": "Harbour"})).unwrap();
if opened {
a.session.path = Some("letter.docx".into());
}
a.tell_about_font_downloads();
a
}
fn harness(app: WordApp) -> egui_kittest::Harness<'static, WordApp> {
harness_sized(app, egui::vec2(1440.0, 900.0))
}
fn harness_sized(app: WordApp, size: egui::Vec2) -> egui_kittest::Harness<'static, WordApp> {
let mut h = egui_kittest::Harness::builder().with_size(size).build_ui_state(
|ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
app,
);
h.run_steps(4);
h
}
fn shown(h: &egui_kittest::Harness<'static, WordApp>) -> Option<&'static str> {
h.state().dialog.as_ref().map(Dialog::name)
}
#[test]
fn an_opened_document_missing_a_downloadable_font_is_told_about_once() {
if aptos_installed() {
return;
}
let mut h = harness(app(true));
let Some(Dialog::FontDownload { fonts, dont_show }) = h.state().dialog.clone() else { panic!("no box: {:?}", shown(&h)) };
// Only the font that can be downloaded, with what it is drawn in and Microsoft's page.
assert_eq!(fonts.len(), 1, "{fonts:?}");
assert_eq!((fonts[0].font.as_str(), fonts[0].publisher.as_str()), ("Aptos", "Microsoft"));
assert!(fonts[0].url.starts_with("https://www.microsoft.com/"), "{}", fonts[0].url);
assert!(!fonts[0].shown_as.is_empty() && !fonts[0].shown_as.iter().any(|f| f == "Aptos"), "{:?}", fonts[0].shown_as);
assert!(!dont_show);
// The box says so: the font, its substitute and publisher, the download, how to add it,
// and the opt-out.
// The headline counts what can be downloaded, not every font the document is missing.
assert!(h.query_by_label("1 missing font can be downloaded").is_some());
assert!(h.query_by_label_contains("isn't installed").is_none(), "other fonts may be missing too");
assert!(h.query_by_label("Aptos").is_some());
assert!(h.query_by_label(format!("Shown as {} · Microsoft", fonts[0].shown_as.join(", ")).as_str()).is_some());
assert!(h.query_by_label_contains("following them is up to you").is_some(), "whose part the licence is");
assert!(h.query_by_label_contains("limits who may use it").is_none(), "Aptos has no such limit");
assert!(h.query_by_label("Pick the font files:").is_some());
assert!(h.query_by_role_and_label(egui::accesskit::Role::Button, "Add Font…").is_some());
assert!(h.query_by_label("Don't show this again").is_some());
// Download opens the publisher's page in the browser; nothing is downloaded here.
h.get_by_role_and_label(egui::accesskit::Role::Button, "Download").click();
let mut opened = false;
for _ in 0..4 {
h.step();
opened |= h.output().platform_output.commands.iter().any(|c| matches!(c, egui::OutputCommand::OpenUrl(u) if u.url == fonts[0].url));
}
assert!(opened, "the page is opened");
assert_eq!(shown(&h), Some("fontDownload"), "and the box stays for the next step");
// Closed, it stays closed for this document.
// The box's own Close button, below the title bar's.
let close =
h.query_all_by_role_and_label(egui::accesskit::Role::Button, "Close").max_by(|a, b| a.rect().center().y.total_cmp(&b.rect().center().y));
close.expect("a Close button").click();
h.run_steps(4);
assert_eq!(shown(&h), None);
assert!(h.state().ui.font_download_notice, "closing alone keeps the box on");
}
#[test]
fn the_box_comes_up_only_where_the_host_asked_for_it() {
let mut a = app(true);
a.font_notices = false;
let h = harness(a);
assert_eq!(shown(&h), None, "headless: tests and ui_shot");
}
#[test]
fn a_new_document_is_not_told_about() {
let h = harness(app(false));
assert_eq!(shown(&h), None, "its fonts are WordCraft's choice");
}
#[test]
fn dont_show_this_again_turns_the_box_off() {
if aptos_installed() {
return;
}
let mut h = harness(app(true));
assert_eq!(shown(&h), Some("fontDownload"));
h.get_by_label("Don't show this again").click();
h.run_steps(2);
assert!(!h.state().ui.font_download_notice, "kept at once");
// Saved between runs, and read back.
let saved = serde_json::to_value(h.state().prefs()).unwrap();
assert_eq!(saved["fontDownloadNotice"], json!(false), "{saved}");
// The next opened document isn't told about.
let mut a = app(true);
a.ui.font_download_notice = false;
let h = harness(a);
assert_eq!(shown(&h), None);
}
#[test]
fn the_box_waits_for_an_open_dialog_and_can_be_opened_by_hand() {
if aptos_installed() {
return;
}
let mut a = app(true);
a.dialog = Dialog::open("wordCount", &mut a);
let mut h = harness(a);
assert_eq!(shown(&h), Some("wordCount"), "the open dialog stays");
h.state_mut().dialog = None;
h.run_steps(3);
assert_eq!(shown(&h), Some("fontDownload"), "then the box");
h.state_mut().dialog = None;
h.run_steps(3);
assert_eq!(shown(&h), None);
// The status bar's menu (`ui.dialog`) opens it again, even with the box turned off.
h.state_mut().ui.font_download_notice = false;
h.state_mut().run("ui.dialog", json!({"name": "fontDownload"})).unwrap();
let Some(Dialog::FontDownload { fonts, dont_show }) = h.state().dialog.clone() else { panic!("no box") };
assert_eq!(fonts.len(), 1);
assert!(dont_show, "the box shows the setting");
}
#[test]
fn a_document_a_script_opened_is_not_told_about() {
let mut a = app(true);
let id = a.session.document_id();
skip(&mut a, id);
skip(&mut a, id);
assert_eq!(a.font_notice_seen, vec![id]);
let h = harness(a);
assert_eq!(shown(&h), None);
}
#[test]
fn a_document_with_nothing_to_download_says_so_when_asked() {
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Lanterns")), crate::Services::default());
a.session.run("select.all", &json!({})).unwrap();
a.session.run("format.font", &json!({"name": "Wordcraft Nowhere Face"})).unwrap();
a.session.path = Some("letter.docx".into());
a.tell_about_font_downloads();
let mut h = harness(a);
assert_eq!(shown(&h), None, "a font nobody gives away brings up no box");
h.state_mut().run("ui.dialog", json!({"name": "fontDownload"})).unwrap();
h.run_steps(3);
assert!(h.query_by_label_contains("None of the fonts").is_some());
}
/// Fonts that come in one download share a card and a Download button.
#[test]
fn fonts_of_one_download_are_listed_together() {
let row = |font: &str, shown: &str, url: &str| Row {
font: font.into(),
shown_as: vec![shown.into()],
download: font.into(),
publisher: "Microsoft".into(),
url: url.into(),
restricted: false,
};
let fonts =
[row("Aptos", "Carlito", "https://a"), row("Cascadia Code", "Liberation Mono", "https://c"), row("Aptos Display", "Carlito", "https://a")];
let g = groups(&fonts);
assert_eq!(g.len(), 2);
assert_eq!(
(g[0].fonts.as_slice(), g[0].shown_as.as_slice(), g[0].url),
(["Aptos", "Aptos Display"].as_slice(), ["Carlito"].as_slice(), "https://a")
);
assert_eq!((g[1].fonts.as_slice(), g[1].url), (["Cascadia Code"].as_slice(), "https://c"));
}
/// As many downloads as the box lists, in a short window and a very short one: what is above
/// the row with Close scrolls, so the opt-out and Close stay on screen and the box in the window.
#[test]
fn many_downloads_in_a_short_window_leave_the_opt_out_and_close_on_screen() {
for height in [600.0, 400.0] {
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Lanterns")), crate::Services::default());
let fonts: Vec<Row> = (0..LISTED)
.map(|i| Row {
font: format!("Harbour Face {i}"),
shown_as: vec!["Carlito".into()],
download: format!("Harbour Face {i}"),
publisher: "Lantern Works".into(),
url: format!("https://example.org/{i}"),
restricted: false,
})
.collect();
a.dialog = Some(Dialog::FontDownload { fonts, dont_show: false });
let h = harness_sized(a, egui::vec2(1000.0, height));
// The steps are still there, to scroll to.
assert!(h.query_by_label("Add Font…").is_some());
for label in ["Don't show this again", "Close"] {
// Close is also the window's close box: every match is on screen.
let rects: Vec<egui::Rect> = h.query_all_by_label(label).map(|n| n.rect()).collect();
assert!(!rects.is_empty(), "no {label}");
for r in rects {
assert!(r.min.y >= 0.0 && r.max.y <= height, "{label} is at {r:?}, outside the {height} pt window");
}
}
}
}
/// A font whose publisher limits its use says so on its card, and has a card of its own.
#[test]
fn a_font_with_a_limited_licence_says_so() {
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Lanterns")), crate::Services::default());
a.session.run("select.all", &json!({})).unwrap();
a.session.run("format.font", &json!({"name": "Segoe UI Variable"})).unwrap();
let layout = a.session.layout();
if !wordcraft_engine::cmd::font::missing_fonts(&a.session.doc, &layout).iter().any(|f| f.font == "Segoe UI Variable") {
return; // Installed here: not missing.
}
a.dialog = Dialog::open("fontDownload", &mut a);
let Some(Dialog::FontDownload { fonts, .. }) = a.dialog.clone() else { panic!("no box") };
assert!(fonts.iter().any(|f| f.font == "Segoe UI Variable" && f.restricted), "{fonts:?}");
let h = harness(a);
assert!(h.query_by_label_contains("limits who may use it").is_some());
}
#[test]
fn only_so_many_documents_are_remembered() {
let mut a = app(false);
for id in 0..(REMEMBERED as u64 + 50) {
skip(&mut a, id);
}
assert_eq!(a.font_notice_seen.len(), REMEMBERED);
assert_eq!(a.font_notice_seen.last(), Some(&(REMEMBERED as u64 + 49)));
}
+2
View File
@@ -306,6 +306,7 @@ fn untranslated_interface_text() {
"tableProperties",
"pasteSpecial",
"about",
"fontDownload",
"contributors",
"models",
"languageData",
@@ -752,6 +753,7 @@ fn the_arabic_interface_is_mirrored_and_never_panics() {
"enclose",
"pasteSpecial",
"about",
"fontDownload",
"contributors",
"models",
"insertMergeField",
+24 -2
View File
@@ -544,6 +544,21 @@ pub fn controls_public(tab: &str) -> Vec<ControlInfo> {
.collect()
}
/// Compact ribbon buttons (the 24×22 icon buttons) are at most this tall.
const COMPACT_H: f32 = 26.0;
/// Where a badge `width` wide goes for a control at `rect`. A big control has room for the badge
/// in its top-left corner. A compact button is nearly all icon (17×17 in 24×22), so there the
/// badge is smaller and hangs out over the button's corner, in the padding and the gap to the
/// neighbours, instead of sitting on the glyph (#4497).
pub fn badge_rect(rect: Rect, width: f32) -> Rect {
if rect.height() <= COMPACT_H {
Rect::from_min_size(rect.min + Vec2::new(-4.0, -4.0), Vec2::new((width + 3.0).max(12.0), 12.0))
} else {
Rect::from_min_size(rect.min + Vec2::new(2.0, 1.0), Vec2::new((width + 4.0).max(15.0), 15.0))
}
}
/// A keytip badge: letters or toolbar numbers, high contrast in both themes.
pub fn badge(ui: &Ui, rect: Rect, letter: &str) {
let count = letter.chars().count();
@@ -551,9 +566,16 @@ pub fn badge(ui: &Ui, rect: Rect, letter: &str) {
return;
}
let t = Tokens::get(ui.ctx());
let font = semibold(if count > 3 { 8.5 } else { 10.0 });
let compact = rect.height() <= COMPACT_H;
let font = semibold(if count > 3 || (compact && count > 1) {
8.5
} else if compact {
9.0
} else {
10.0
});
let width = ui.ctx().fonts_mut(|f| f.layout_no_wrap(letter.to_string(), font.clone(), t.on_accent).size().x);
let r = Rect::from_min_size(rect.min + Vec2::new(2.0, 1.0), Vec2::new((width + 4.0).max(15.0), 15.0));
let r = badge_rect(rect, width);
let p = ui.painter();
p.rect_filled(r, CornerRadius::same(3), t.accent);
p.rect_stroke(r, CornerRadius::same(3), Stroke::new(1.0, t.menu), egui::StrokeKind::Inside);
+19
View File
@@ -12,6 +12,25 @@ fn input_frame(ctx: &egui::Context, app: &mut WordApp, events: Vec<egui::Event>)
ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| logic(app, ui.ctx())).drop_without_applying_deltas();
}
/// #4497: a compact button is nearly all icon (`widgets::small`: 17×17 in 24×22), and its
/// badge sat on 63% of the glyph. It now hangs out over the corner and covers a sliver, for a
/// one-letter and a two-letter badge alike; a big control keeps its corner badge.
#[test]
fn a_compact_buttons_badge_leaves_its_icon_visible() {
let button = Rect::from_min_size(egui::pos2(100.0, 60.0), Vec2::new(24.0, 22.0));
let icon = Rect::from_center_size(egui::pos2(button.min.x + 12.0, button.center().y), Vec2::splat(17.0));
for (width, most) in [(7.0, 0.10), (14.0, 0.20)] {
let r = badge_rect(button, width);
let covered = r.intersect(icon);
let share = covered.width().max(0.0) * covered.height().max(0.0) / (icon.width() * icon.height());
assert!(share <= most, "a badge {width} wide covers {share:.2} of the icon (at most {most})");
assert!(r.min.x >= button.min.x - 4.0 && r.min.y >= button.min.y - 4.0, "it hangs out of the corner by no more than 4 px");
assert!(r.width() >= 12.0 && r.height() >= 12.0, "and stays big enough to read");
}
let big = Rect::from_min_size(egui::pos2(100.0, 60.0), Vec2::new(44.0, 66.0));
assert_eq!(badge_rect(big, 7.0), Rect::from_min_size(egui::pos2(102.0, 61.0), Vec2::new(15.0, 15.0)));
}
#[test]
fn native_alt_taps_toggle_once_in_either_event_order() {
let alt = egui::Modifiers { alt: true, ..Default::default() };
+11
View File
@@ -43,6 +43,7 @@ pub mod equation_tab;
#[cfg(not(target_arch = "wasm32"))]
pub mod file_browser;
pub mod file_dialogs;
pub mod font_download;
pub mod format_shape;
pub mod frame;
pub mod guides;
@@ -91,6 +92,7 @@ use serde_json::{Value, json};
use wordcraft_engine::Session;
pub use control::ControlRequest;
pub use dialogs::dialogs_bullet;
pub use dialogs::dialogs_chart;
pub use dialogs::dialogs_controls;
pub use dialogs::dialogs_insert;
@@ -434,6 +436,9 @@ pub struct UiState {
/// spaces, or characters without spaces. Right-click the count in the status bar, or File ›
/// Options › Display. Junk saved values read as words.
pub status_count: StatusCount,
/// File › Options › General: tell about a document's missing fonts that can be downloaded
/// ([`font_download`]); the box's "Don't show this again" turns it off.
pub font_download_notice: bool,
/// How far a mouse-wheel notch scrolls (#6146).
#[serde(default, alias = "scrollSpeed")]
pub scroll_speed: ScrollSpeed,
@@ -539,6 +544,7 @@ impl Default for UiState {
dictionary: Vec::new(),
display_server: DisplayServer::Auto,
status_count: StatusCount::default(),
font_download_notice: true,
scroll_speed: ScrollSpeed::default(),
touchpad_scroll_speed: ScrollSpeed::default(),
}
@@ -689,6 +695,11 @@ pub struct WordApp {
/// Focus (#1678): the ribbon is dropped down from the Focus bar, over the page.
pub(crate) focus_ribbon: bool,
pub(crate) updates_rt: updates_ui::Runtime,
/// The documents (by id) the missing-fonts download box has been considered for
/// ([`font_download::tick`]), in every window.
pub(crate) font_notice_seen: Vec<u64>,
/// Whether that box comes up by itself ([`WordApp::tell_about_font_downloads`]).
pub(crate) font_notices: bool,
/// Language pack installs, the missing-pack bar ([`language_packs`]).
pub(crate) packs_rt: language_packs::Runtime,
control_rx: Option<std::sync::mpsc::Receiver<ControlRequest>>,
+20 -8
View File
@@ -73,15 +73,21 @@ pub struct ObjectDrag {
/// How near (screen points) a press must be to a connector's end to grab it.
const END_GRAB: f32 = 7.0;
/// The selected connector's start and end on its page (points), and its layout.
/// The selected floating connector's start and end on its page (points), and its layout; or a
/// floating line's, whose ends move the same way (they just don't glue); both in the body text,
/// where `shape.connect` moves them. Such a line shows
/// and takes handles at its ends only, as Word's do: the frame's handles would sit on a level
/// line, so pressing it there would resize it into a slant rather than move it.
fn connector_ends(app: &WordApp, layout: &DocLayout) -> Option<(ObjectHit, (f32, f32), (f32, f32))> {
let (pos, InlineObject::Shape { kind, float, .. }) = selected(app)? else { return None };
if !kind.is_connector() {
// Where `shape.connect` can't move its ends (In Line with Text, a header or footer), a line or
// connector keeps its frame's handles instead.
if !kind.is_open() || pos.story != StoryRef::Body || float.wrap == Wrap::Inline {
return None;
}
let (flip_h, flip_v) = (float.flip_h, float.flip_v);
let o = layout.object(&pos, app.session.page_hint)?;
let (a, b) = wordcraft_doc::connector::ends(o.rect.x, o.rect.y, o.rect.w, o.rect.h, flip_h, flip_v);
let (a, b) = wordcraft_engine::cmd::connectors::page_ends(o.rect, flip_h, flip_v, o.spin.deg);
Some((o, a, b))
}
@@ -656,7 +662,9 @@ fn object_link(app: &WordApp, object: &ObjectHit) -> Option<String> {
/// being edited (body, or the header/footer).
fn grab_at(app: &WordApp, layout: &DocLayout, pages: &[Rect], scale: f32, at: Pos2) -> Option<(ObjectHit, Grab)> {
let story = active_story(app, layout);
let handle = active(app, layout).filter(|(o, _)| o.story == story && !fixed(app, o)).and_then(|(o, _)| {
// A connector or line is resized by its ends (`connector_end_at`), not by a frame.
let framed = connector_ends(app, layout).is_none();
let handle = active(app, layout).filter(|(o, _)| framed && o.story == story && !fixed(app, o)).and_then(|(o, _)| {
let f = screen(pages, scale, o.page, o.rect)?;
if !size_only(app, &o) && frame::rotate_handle_at(f, o.spin.deg, at) {
return Some((o, Grab::Rotate));
@@ -811,8 +819,10 @@ pub fn paint(app: &WordApp, painter: &Painter, t: &Tokens, layout: &DocLayout, p
paint_end_drag(app, d, painter, t, layout, pages, scale);
return;
}
if let Some((o, a, b)) = connector_ends(app, layout) {
// A connector: a handle at each end (filled when glued).
// A connector or line: a handle at each end (filled when glued). While it is dragged by its
// body, the drag preview below shows instead (the page leaves it out meanwhile).
let body_drag = app.canvas.obj_drag.as_ref().is_some_and(|d| d.drag.moved);
if !body_drag && let Some((o, a, b)) = connector_ends(app, layout) {
let glued = match selected(app) {
Some((_, InlineObject::Shape { extra, .. })) => (extra.start.is_some(), extra.end.is_some()),
_ => (false, false),
@@ -893,14 +903,16 @@ fn paint_drop_caret(d: &ObjectDrag, painter: &Painter, layout: &DocLayout, pages
}
/// A connector's end being dragged: a line from its other end to the pointer, and the connection
/// sites of the shapes near the pointer (the one it would glue to, filled).
/// sites of the shapes near the pointer (the one it would glue to, filled). A line's end doesn't
/// glue, so it shows no sites.
fn paint_end_drag(app: &WordApp, d: &ObjectDrag, painter: &Painter, t: &Tokens, layout: &DocLayout, pages: &[Rect], scale: f32) {
let Some((which, at)) = d.end.filter(|_| d.drag.moved) else { return };
let Some((_, a, b)) = connector_ends(app, layout) else { return };
let fixed = if which { a } else { b };
let snap = wordcraft_engine::cmd::connectors::SNAP;
let glues = matches!(selected(app), Some((_, InlineObject::Shape { kind, .. })) if kind.is_connector());
let mut best: Option<((f32, f32), f32)> = None;
if let Some(pg) = layout.pages.get(d.object.page) {
if glues && let Some(pg) = layout.pages.get(d.object.page) {
for it in &pg.items {
let wordcraft_layout::Placed::Object { rect, spin, story: StoryRef::Body, path, off, .. } = it else { continue };
let pos = Pos { story: StoryRef::Body, path: path.clone(), off: *off };
+103
View File
@@ -423,6 +423,109 @@ fn floating_rect(w: f32, h: f32) -> InlineObject {
}
}
/// A floating line `w` wide and `h` tall (0: level, as Word files often store a rule).
fn floating_line(w: f32, h: f32, flip_v: bool) -> InlineObject {
InlineObject::Shape {
kind: ShapeKind::Line,
w,
h,
fill: None,
stroke: Some(wordcraft_doc::Rgb(0, 0, 0)),
stroke_width: 0.75,
float: wordcraft_doc::para::Float { wrap: Wrap::InFrontOfText, x: 60.0, y: 120.0, flip_v, ..Default::default() },
story: None,
freeform: None,
effects: Default::default(),
extra: Default::default(),
}
}
/// A selected level line, pressed in its middle and dragged, moves and stays level: its
/// middle is its body, not a resize handle (a level line's top and bottom edge handles sit on
/// it there, and grabbing one gave it a height, so it came out slanted).
#[test]
fn a_level_line_dragged_by_its_middle_moves_and_stays_level() {
use crate::table_resize::tests::{H, button, steps};
for flip_v in [false, true] {
let (mut app, hit) = app_with(floating_line(300.0, 0.0, flip_v));
app.run("view.commentsPane", json!({"value": false})).unwrap();
app.run("select.range", json!({"anchor": hit.pos(), "focus": Pos { off: hit.off + OBJ.len_utf8(), ..hit.pos() }})).unwrap();
let mut h: H = rotation_window(app);
let r = h.state_mut().session.layout().object(&Pos::body(0, 0), 0).unwrap().rect;
let at = |h: &mut H, x: f32, y: f32| crate::canvas::page_to_screen(h.state_mut(), 0, x, y).unwrap();
let start = at(&mut h, r.x + r.w / 2.0, r.y);
// Along the page and a little down, as a hand drags.
let end = start + egui::vec2(60.0, 3.0);
h.event(egui::Event::PointerMoved(start));
button(&h, start, true);
h.step();
for k in 1..=4 {
h.event(egui::Event::PointerMoved(start + (end - start) * (k as f32 / 4.0)));
h.step();
}
assert_eq!(h.state().canvas.obj_drag.as_ref().map(|d| d.drag.grab), Some(Grab::Move), "flip_v {flip_v}: the middle moves it");
// While it moves, it shows where the pointer has it (the page leaves it out
// meanwhile), not only as its end handles back where it was: something is drawn along
// the line's new place, between its ends.
h.step();
let img = h.render().unwrap();
let ppp = h.ctx.pixels_per_point();
let (x0, x1) = (((end.x - 100.0) * ppp) as u32, ((end.x + 100.0) * ppp) as u32);
let (y0, y1) = (((end.y - 3.0) * ppp) as u32, ((end.y + 3.0) * ppp) as u32);
let pointer = Rect::from_min_size(end - egui::vec2(2.0, 2.0), egui::vec2(20.0, 26.0));
let drawn = (y0..y1)
.flat_map(|y| (x0..x1).map(move |x| (x, y)))
.filter(|&(x, y)| !pointer.contains(egui::pos2(x as f32 / ppp, y as f32 / ppp)))
.filter(|&(x, y)| img.get_pixel(x, y).0[..3].iter().any(|c| *c < 200))
.count();
assert!(drawn > 50, "flip_v {flip_v}: the dragged line shows at its new place ({drawn} pixels)");
button(&h, end, false);
steps(&mut h, 3);
let n = h.state_mut().session.layout().object(&Pos::body(0, 0), 0).unwrap().rect;
assert!(n.h.abs() < 0.01 && (n.w - r.w).abs() < 0.01, "flip_v {flip_v}: still a level line as long: {r:?} -> {n:?}");
assert!(n.x > r.x + 10.0, "flip_v {flip_v}: moved along: {r:?} -> {n:?}");
// Its end is a handle: dragged down, that end goes there and the other stays.
let right = at(&mut h, n.right(), n.y);
let down = right + egui::vec2(0.0, 40.0);
h.event(egui::Event::PointerMoved(right));
button(&h, right, true);
h.step();
for k in 1..=4 {
h.event(egui::Event::PointerMoved(right + (down - right) * (k as f32 / 4.0)));
h.step();
}
button(&h, down, false);
steps(&mut h, 3);
let e = h.state_mut().session.layout().object(&Pos::body(0, 0), 0).unwrap().rect;
// Screen points per page point.
let scale = (at(&mut h, n.x + 100.0, n.y).x - at(&mut h, n.x, n.y).x) / 100.0;
assert!((e.x - n.x).abs() < 0.5 && (e.y - n.y).abs() < 0.5, "flip_v {flip_v}: the left end stays: {n:?} -> {e:?}");
assert!(
(e.right() - n.right()).abs() < 0.5 && (e.bottom() - (n.y + 40.0 / scale)).abs() < 0.5,
"flip_v {flip_v}: the right end follows: {e:?}"
);
}
}
/// End handles go only to the lines and connectors `shape.connect` moves: floating ones in the
/// body text. One In Line with Text keeps its frame's handles, so it can still be resized on the
/// page.
#[test]
fn only_floating_lines_and_connectors_take_end_handles() {
for kind in [ShapeKind::Line, ShapeKind::StraightConnector] {
for (wrap, ends) in [(Wrap::InFrontOfText, true), (Wrap::Inline, false)] {
let mut obj = floating_line(200.0, 40.0, false);
if let InlineObject::Shape { kind: k, float, .. } = &mut obj {
(*k, float.wrap) = (kind, wrap);
}
let (mut app, hit) = app_with(obj);
app.run("select.range", json!({"anchor": hit.pos(), "focus": Pos { off: hit.off + OBJ.len_utf8(), ..hit.pos() }})).unwrap();
let layout = app.session.layout();
assert_eq!(connector_ends(&app, &layout).is_some(), ends, "{kind:?} {wrap:?}");
}
}
}
/// A text box doesn't snap to the objects inside it (they move with it).
#[test]
fn a_text_box_does_not_snap_to_its_own_contents() {
+1
View File
@@ -250,6 +250,7 @@ fn general(app: &mut WordApp, ui: &mut Ui) {
let _ = app.run("view.darkMode", json!({"value": dark_page}));
}
ui.checkbox(&mut app.ui.show_discord, tl!("Show the community button in the title bar"));
ui.checkbox(&mut app.ui.font_download_notice, tl!("Tell me when a document's missing fonts can be downloaded"));
default_app_button(app, ui);
ui.add_space(8.0);
section(ui, "Start-up");
+26
View File
@@ -556,6 +556,32 @@ fn snippet_blocks(base: &Document, blocks: Vec<Block>, options: SnippetOptions)
Some(egui::ColorImage::from_rgba_unmultiplied([img.width as usize, img.height as usize], &px))
}
/// One character formatted by `chr` (at its size, or fitted to `r` without one), centred in `r`
/// and drawn with `tint`:
/// laid out like document text, so symbol fonts show what the page will (Define New Bullet, #2786).
pub fn glyph(app: &mut WordApp, ui: &mut Ui, r: Rect, ch: &str, chr: &CharProps, tint: egui::Color32) {
let ppp = ui.ctx().pixels_per_point();
let key = format!("glyph:{ch}:{chr:?}:{ppp}:{}x{}", r.width(), r.height());
let tex = app.previews.get_or(ui.ctx(), &key, || {
let chr = CharProps { size: Some(chr.size.unwrap_or(r.height() * 0.7)), ..chr.clone() };
let mut p = Paragraph::with_text(ch, chr);
p.props.align = Some(wordcraft_doc::props::Align::Center);
p.props.space_before = Some(0.0);
p.props.space_after = Some(0.0);
snippet_blocks(
&Document::new(),
vec![Block::Para(p)],
SnippetOptions { width: r.width(), height: r.height(), pixels_per_point: ppp, margin: 0.0, paper: None, dark: false },
)
});
if let Some(hd) = tex {
let sz = hd.size_vec2() / ppp;
let ir = Rect::from_min_size(r.min, sz.min(r.size()));
let uv = Rect::from_min_max(pos2(0.0, 0.0), pos2((ir.width() / sz.x).min(1.0), (ir.height() / sz.y).min(1.0)));
ui.painter().with_clip_rect(r).image(hd.id(), ir, uv, tint);
}
}
/// Manage Styles' preview of one style (`ty`: the `styles.manage` type), `size` points wide and
/// tall: a line of sample text, or a small table for a table style.
pub fn style_preview(app: &mut WordApp, ui: &mut Ui, id: &str, ty: &str, size: egui::Vec2) {
+1
View File
@@ -35,6 +35,7 @@ use charts::{chart_design, smart_art_design};
pub(crate) use common::mi;
#[cfg(test)]
pub(crate) use common::size_menu;
pub(crate) use common::weight_menu;
use common::{custom_groups, edge_cue};
use design::design;
#[cfg(test)]
+33 -1
View File
@@ -1,5 +1,5 @@
use super::language::mi_check;
use super::{Align2, MeasureExt, Rect, Sense, Tokens, Ui, Value, WordApp, big, group, json, small, vec2};
use super::{Align2, MeasureExt, Rect, Sense, Stroke, Tokens, Ui, Value, WordApp, big, group, json, pos2, small, vec2};
/// Custom groups (Customize Ribbon, #379): up to three commands as large buttons, more as small
/// labelled buttons in columns of three. Each shows the command's own icon and label.
@@ -145,6 +145,38 @@ pub(crate) fn size_menu(ui: &mut Ui, app: &mut WordApp) {
mi(ui, app, crate::dialogs::PAPER_LABELS.more, "ui.dialog", json!({"name": "pageSetup"}));
}
/// The outline weights Word offers, in points: Text Outline, Shape Outline and the border dialogs.
const OUTLINE_WEIGHTS: [f32; 9] = [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0];
/// An outline's Weight submenu (#3917): each weight as its label and a line sample drawn at that
/// weight, the `current` one highlighted. Choosing one runs `id` with `params(weight)`.
pub(crate) fn weight_menu(ui: &mut Ui, app: &mut WordApp, id: &str, current: Option<f32>, params: impl Fn(f32) -> Value) {
let enabled = crate::widgets::enabled(app, id);
let t = Tokens::get(ui.ctx());
let color = if enabled { t.text } else { t.text_disabled };
// A hairline under a device pixel would vanish, so the thinnest samples get one.
let pixel = 1.0 / ui.ctx().pixels_per_point();
let labels = OUTLINE_WEIGHTS.map(|w| crate::i18n::fmt(tl!("{n} pt"), &[("n", &w.to_string())]));
// The samples start a short gap after the widest label and end as far from the right edge as
// the labels start from the left one.
let font = egui::TextStyle::Button.resolve(ui.style());
let widest = labels.iter().map(|l| ui.painter().layout_no_wrap(l.clone(), font.clone(), color).size().x).fold(0.0, f32::max);
let pad = ui.spacing().button_padding.x;
for (w, label) in OUTLINE_WEIGHTS.into_iter().zip(&labels) {
let checked = current.is_some_and(|c| (c - w).abs() < 0.01);
let resp = ui.add_enabled(enabled, egui::Button::new(label.as_str()).selected(checked).min_size(vec2(200.0, 22.0)));
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, enabled, checked, label));
let r = resp.rect;
let y = r.center().y;
let (x0, x1) = (r.left() + pad + widest + 12.0, r.right() - pad);
ui.painter().line_segment([pos2(x0, y), pos2(x1, y)], Stroke::new(w.max(pixel), color));
if resp.clicked() {
crate::widgets::run_from_ribbon(app, id, params(w));
ui.close();
}
}
}
pub(crate) fn mi(ui: &mut Ui, app: &mut WordApp, label: &str, id: &str, params: Value) {
mi_text(ui, app, tl!(label), id, params);
}
+18
View File
@@ -26,6 +26,23 @@ pub(super) fn painter_double_click(app: &mut WordApp) {
let _ = app.run("edit.formatPainter", json!({"sticky": true}));
}
/// Change List Level › and Define New Bullet… at the foot of the Bullets drop-down (#2786).
/// Change List Level is for a paragraph in a list of its own numbering, the one `para.listLevel`
/// changes (a style's numbering it leaves alone).
fn bullet_list_items(ui: &mut Ui, app: &mut WordApp) {
ui.separator();
let in_list = app.session.doc.para_at(&app.session.sel.focus).and_then(|p| p.props.numbering).is_some_and(|n| n.num != 0);
ui.add_enabled_ui(in_list, |ui| {
ui.menu_button(tl!("Change List Level"), |ui| {
for level in 0..9u8 {
let label = format!("{} {}", tl!("Level"), level + 1);
mi_text(ui, app, &label, "para.listLevel", json!({"level": level}));
}
});
});
mi(ui, app, "Define New Bullet…", "ui.dialog", json!({"name": "defineBullet"}));
}
pub(super) fn home(app: &mut WordApp, ui: &mut Ui) {
let st = app.session.run("format.state", &json!({})).unwrap_or_default();
let flag = |k: &str| st.get(k).and_then(Value::as_bool).unwrap_or(false);
@@ -196,6 +213,7 @@ pub(super) fn home(app: &mut WordApp, ui: &mut Ui) {
}
});
mi(ui, app, "None", "para.bullets", json!({"off": true}));
bullet_list_items(ui, app);
});
split(
ui,
+22 -11
View File
@@ -1,5 +1,5 @@
use super::arrange::{align_menu, rotate_menu, rotation_field, wrap_menu};
use super::common::{mi, size_field, stack};
use super::common::{mi, size_field, stack, weight_menu};
use super::language::mi_check;
use super::{Target, Ui, WordApp, big, group, json, menu_button, small, vec2};
@@ -43,12 +43,28 @@ pub(crate) fn gradient_menu(ui: &mut Ui, app: &mut WordApp, fill: Option<&wordcr
}
}
/// The outline (colour, width) Shape Outline changes: the selected shape's, or the selected
/// drawing canvas's border.
pub(super) fn selected_outline(s: &wordcraft_engine::Session) -> Option<(wordcraft_doc::Rgb, f32)> {
match wordcraft_engine::cmd::objects::selected(s)?.1 {
wordcraft_doc::para::InlineObject::Shape { stroke, stroke_width, .. } => stroke.map(|c| (c, stroke_width)),
o => o.canvas_style().and_then(|c| c.stroke.map(|stroke| (stroke, c.stroke_width))),
}
}
/// Shape Outline › Weight: a new weight keeps the outline's colour (black when it has none).
pub(super) fn shape_weight_menu(ui: &mut Ui, app: &mut WordApp) {
let outline = selected_outline(&app.session);
let color = outline.map_or(wordcraft_doc::Rgb::BLACK, |o| o.0).hex();
weight_menu(ui, app, "shape.outline", outline.map(|o| o.1), |w| json!({"color": color, "width": w}));
}
/// Shape Format: fill, outline and effects of the selected shape, and its arrangement.
pub(super) fn shape_format(app: &mut WordApp, ui: &mut Ui) {
let (stroke, glow_size, fill) = match wordcraft_engine::cmd::objects::selected(&app.session) {
Some((_, wordcraft_doc::para::InlineObject::Shape { stroke, effects, fill, .. })) => (stroke, effects.glow.map(|g| g.size), fill),
Some((_, o)) => (None, None, o.canvas_style().and_then(|c| c.fill.clone())),
_ => (None, None, None),
let (glow_size, fill) = match wordcraft_engine::cmd::objects::selected(&app.session) {
Some((_, wordcraft_doc::para::InlineObject::Shape { effects, fill, .. })) => (effects.glow.map(|g| g.size), fill),
Some((_, o)) => (None, o.canvas_style().and_then(|c| c.fill.clone())),
_ => (None, None),
};
let theme = app.session.doc.settings.theme_colors.clone();
group(ui, "Shape Styles", None, app, |ui, app| {
@@ -72,12 +88,7 @@ pub(super) fn shape_format(app: &mut WordApp, ui: &mut Ui) {
mi(ui, app, "No Color", "shape.outline", json!({"color": null}));
crate::colors::menu(ui, app, &theme, Target::cmd("shape.outline"));
ui.separator();
ui.menu_button(tl!("Weight"), |ui| {
let color = stroke.unwrap_or(wordcraft_doc::Rgb::BLACK).hex();
for w in [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0] {
mi(ui, app, &format!("{w} pt"), "shape.outline", json!({"color": color, "width": w}));
}
});
ui.menu_button(tl!("Weight"), |ui| shape_weight_menu(ui, app));
},
);
menu_button(
+62
View File
@@ -199,6 +199,68 @@ fn design_style_set_gallery_reads_from_the_right_in_rtl() {
}
}
/// #3917: an outline's Weight submenu offers Word's nine weights, each a named entry with a
/// line sample drawn at its weight; the current weight is highlighted, and choosing one sets it.
#[test]
fn weight_menu_draws_each_weight_and_applies_it() {
use egui::accesskit::Toggled;
use egui_kittest::kittest::{NodeT, Queryable};
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Lanterns")), crate::Services::default());
a.run("select.all", json!({})).unwrap();
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 400.0)).build_ui_state(
|ui, app: &mut WordApp| {
let current = crate::wordart_menus::current_outline(app).map(|c| c.1);
weight_menu(ui, app, "format.textEffects", current, |w| json!({"outline": {"color": "1F4E79", "width": w}}));
},
a,
);
h.run_steps(2);
let labels = ["0.25 pt", "0.5 pt", "0.75 pt", "1 pt", "1.5 pt", "2.25 pt", "3 pt", "4.5 pt", "6 pt"];
for label in labels {
let node = h.get_by_label(label);
assert_eq!(node.accesskit_node().toggled(), Some(Toggled::False), "{label} not highlighted with no outline set");
}
// One line sample per weight, at that weight.
let widths: Vec<f32> = h
.output()
.shapes
.iter()
.filter_map(|s| match &s.shape {
egui::Shape::LineSegment { stroke, .. } => Some(stroke.width),
_ => None,
})
.collect();
assert_eq!(widths, [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0].map(|w: f32| w.max(1.0)), "samples, a pixel at least");
let outline = |h: &egui_kittest::Harness<'_, WordApp>| crate::wordart_menus::current_outline(h.state());
for (label, width) in [("6 pt", 6.0), ("0.25 pt", 0.25)] {
h.get_by_label(label).click();
h.run_steps(2);
assert_eq!(outline(&h), Some((wordcraft_doc::Rgb::parse("1F4E79").unwrap(), width)), "{label}");
assert_eq!(h.get_by_label(label).accesskit_node().toggled(), Some(Toggled::True), "{label} is highlighted once chosen");
assert_eq!(h.get_by_label("1 pt").accesskit_node().toggled(), Some(Toggled::False));
}
}
/// #3917 review: Shape Outline › Weight on a drawing canvas reads the canvas's border, so its
/// weight is highlighted and a new weight keeps the border's colour instead of turning it black.
#[test]
fn shape_outline_weight_keeps_a_canvas_borders_colour() {
use super::shape::{selected_outline, shape_weight_menu};
use egui::accesskit::Toggled;
use egui_kittest::kittest::{NodeT, Queryable};
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
a.run("insert.canvas", json!({})).unwrap();
a.run("shape.outline", json!({"color": "1F4E79", "width": 1.5})).unwrap();
let blue = wordcraft_doc::Rgb::parse("1F4E79");
assert_eq!(selected_outline(&a.session), blue.map(|c| (c, 1.5)), "the canvas's border");
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 400.0)).build_ui_state(|ui, app: &mut WordApp| shape_weight_menu(ui, app), a);
h.run_steps(2);
assert_eq!(h.get_by_label("1.5 pt").accesskit_node().toggled(), Some(Toggled::True), "the border's weight");
h.get_by_label("3 pt").click();
h.run_steps(2);
assert_eq!(selected_outline(&h.state().session), blue.map(|c| (c, 3.0)), "a new weight keeps the border blue");
}
/// #2738: Home › Underline's drop-down: every line sample is a named entry (assistive
/// technology, tooltips) that applies its own style; Words only, None and Underline Color ›
/// Automatic do what they say.
+3
View File
@@ -175,6 +175,8 @@ impl WordApp {
mac_titlebar: _,
control_rx: _,
updates_rt: _,
font_notice_seen: _,
font_notices: _,
packs_rt: _,
// The control channel's screenshots and input go to the active window, whichever it is.
shots: _,
@@ -315,6 +317,7 @@ impl WordApp {
print_backgrounds: _,
update_fields_before_print: _,
dictionary: _,
font_download_notice: _,
// Where each task pane sits, and their order.
styles_pane_dock: _,
styles_pane_float: _,
+69 -10
View File
@@ -132,9 +132,7 @@ pub fn outline_menu(ui: &mut Ui, app: &mut WordApp, theme: &[Rgb], current: Opti
ui.separator();
ui.menu_button(crate::tl!("Weight"), |ui| {
let color = current.map_or(Rgb::BLACK, |c| c.0).hex();
for w in [0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0] {
mi(ui, app, &format!("{w} pt"), "format.textEffects", json!({"outline": {"color": color, "width": w}}));
}
crate::ribbon::weight_menu(ui, app, "format.textEffects", current.map(|c| c.1), |w| json!({"outline": {"color": color, "width": w}}));
});
}
@@ -209,14 +207,75 @@ pub fn transform_menu(ui: &mut Ui, app: &mut WordApp) {
});
}
/// The selected shape's text outline (colour, width), for the Weight menu.
/// The text outline (colour, width) Text Outline would change, for its Weight menu: read where
/// `format.textEffects` applies, so choosing a weight keeps that text's colour. A selected shape,
/// or a caret in a text box, formats the box's whole text, read from its start; otherwise the
/// selection, read as typing reads it (the first selected character, or the caret's).
pub fn current_outline(app: &WordApp) -> Option<(Rgb, f32)> {
let story = match wordcraft_engine::cmd::objects::object_selection(&app.session) {
Some((_, wordcraft_doc::InlineObject::Shape { story: Some(id), .. })) => wordcraft_doc::StoryRef::Part(*id),
_ => app.session.sel.focus.story,
let s = &app.session;
let props = match wordcraft_engine::cmd::wordart::box_story(s) {
Some(id) => s.doc.para_at(&s.doc.start_of(wordcraft_doc::StoryRef::Part(id)))?.props_at(0).clone(),
None => s.typing_props(),
};
let start = app.session.doc.start_of(story);
let para = app.session.doc.para_at(&start)?;
let fx = para.props_at(0).text_effects.as_deref()?;
let fx = props.text_effects.as_deref()?;
fx.outline.and_then(|o| o.color.map(|c| (c, o.width)))
}
#[cfg(test)]
mod tests {
use super::*;
use wordcraft_engine::Session;
fn outline_at(app: &WordApp, para: usize) -> Option<(Rgb, f32)> {
let p = app.session.doc.para_at(&wordcraft_doc::Pos::body(para, 0))?;
let fx = p.props_of_char(0).text_effects.as_deref()?;
fx.outline.and_then(|o| o.color.map(|c| (c, o.width)))
}
/// #3917 review: the Weight menu reads the outline where Text Outline applies, the selection,
/// not the first character of the story. Its highlight is the selected text's weight, and a
/// new weight keeps that text's colour and leaves other paragraphs alone.
#[test]
fn the_weight_menu_reads_and_keeps_the_selected_texts_outline() {
use egui::accesskit::Toggled;
use egui_kittest::kittest::{NodeT, Queryable};
let rgb = |h: &str| Rgb::parse(h);
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Red line\nBlue line")), crate::Services::default());
a.run("select.text", json!({"text": "Red line"})).unwrap();
a.run("format.textEffects", json!({"outline": {"color": "C00000", "width": 1.0}})).unwrap();
a.run("select.text", json!({"text": "Blue line"})).unwrap();
a.run("format.textEffects", json!({"outline": {"color": "1F4E79", "width": 3.0}})).unwrap();
assert_eq!(current_outline(&a), rgb("1F4E79").map(|c| (c, 3.0)), "the selection's outline, not the first paragraph's");
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(500.0, 700.0)).build_ui_state(
|ui, app: &mut WordApp| {
let theme = app.session.doc.settings.theme_colors.clone();
let current = current_outline(app);
outline_menu(ui, app, &theme, current);
},
a,
);
h.run_steps(2);
h.get_by_label("Weight").hover();
h.run();
h.get_by_label("Weight").click();
h.run();
assert_eq!(h.get_by_label("3 pt").accesskit_node().toggled(), Some(Toggled::True), "the selected text's weight");
assert_eq!(h.get_by_label("1 pt").accesskit_node().toggled(), Some(Toggled::False));
h.get_by_label("6 pt").click();
h.run_steps(2);
assert_eq!(outline_at(h.state(), 1), rgb("1F4E79").map(|c| (c, 6.0)), "the new weight keeps the selected text's colour");
assert_eq!(outline_at(h.state(), 0), rgb("C00000").map(|c| (c, 1.0)), "the other paragraph is untouched");
}
/// A caret in a text box formats the box's whole text, so its outline is read from the box's
/// start; back in the body, the body text's (none here).
#[test]
fn a_caret_in_a_text_box_reads_the_boxs_outline() {
let mut a = WordApp::new(Session::new(wordcraft_doc::Document::from_text("Body")), crate::Services::default());
a.run("insert.textBox", json!({"text": "Boxed words"})).unwrap();
a.run("format.textEffects", json!({"outline": {"color": "00B050", "width": 2.25}})).unwrap();
assert_eq!(current_outline(&a), Rgb::parse("00B050").map(|c| (c, 2.25)));
a.run("caret.set", json!({"pos": wordcraft_doc::Pos::body(0, 2)})).unwrap();
assert_eq!(current_outline(&a), None, "the body text has no outline");
}
}
+2
View File
@@ -56,6 +56,7 @@ fn listing_stops_at_the_field_budget() {
// Finding 16: rich text with alternating formatting is built in one pass, not run by run with
// every earlier run merged again.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn rich_text_with_many_runs_is_linear() {
let html: String = "<i>a</i>b".repeat(50_000);
let t = Instant::now();
@@ -82,6 +83,7 @@ fn call(b: &mut Bridge, s: &mut Session, method: &str, args: Value) -> Result<Va
// Finding 32: Document_convert checks each distinct id once against one list of the fields.
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn convert_with_one_id_repeated_is_one_check() {
let mut doc = Document::from_text("");
// Checking each id against a fresh list of the fields took 81 s with 300 of them; with 1,000
+2 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -175,7 +175,7 @@ test source in File › Options › Language and on the bar's Install.
| `list_documents` | the open documents: `id`, `title`, `path`, `dirty`, `active` |
| `type_text` | type at the caret (`paragraphs: true` splits lines into paragraphs: one undo step; each break is the Enter it stands for, so AutoFormat and AutoCorrect run and a one-line control refuses; at most 10,000 lines — the cap is `text.insert`'s own, so a control-channel or CLI call is held to it too) |
| `select_text` | select the n-th occurrence of some text |
| `get_text` / `inspect_document` | read the document back (verify without screenshots) |
| `get_text` / `inspect_document` | read the document back (verify without screenshots); a header or footer's `fields` mark those with `perPage` (PAGE, NUMPAGES, STYLEREF…), whose stored result isn't what every page shows (`render_page` shows that); `fieldsTruncated` means a part has more than the 200 listed; a field kept as a range (a TOC, a bibliography) gives at most 2000 characters of its result, with `resultTruncated` when it has more |
| `render_page` | a page as PNG |
| `parity` | feature coverage |
| `screenshot`, `click`, `key`, `ui_inspect` | app only (`--connect`) |
+4 -2
View File
File diff suppressed because one or more lines are too long