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
.CooCoo 1a56d041de Dependencies: take craft-libs main at 453b0da782 (#6886)
* Dependencies: take craft-libs main at 3af2280ee3

Every craft-libs git dependency (the workspace's 25, ui-egui's 3, the app's
craft-print, the web build's 2 and the krilla patch) moves to craft-libs main
at 3af2280ee3 from the 8 revisions they were pinned at. Cargo.lock changes
those sources, moves winprint and craft-winprint-gdi with craft-print, drops
the second craft-tiff, and adds cap-primitives and its dependencies, which
craft-font-store's Language pack recovery uses on native builds.

- craft_print::Job has two new fields. WordCraft sets grey_pages to false,
  since it does not turn the spooled pages gray, and driver_settings to None.
- DOCX text boxes, rectangles, rounded rectangles and ellipses are now
  written in mc:AlternateContent with a VML fallback that repeats the text
  box's text. Two tests that counted w:txbxContent elements, or the story
  text, now count the fallback copy as well.

* Printing: the isolated print helper keeps the job's grey pages and driver settings

The Windows print helper process rebuilt each job with grey_pages off and no
driver settings, whatever the job carried. Its request now carries both: the
driver settings travel as the printer's name and the DEVMODE in hex, and
damaged settings are an error. The request file may now be up to 2 MiB larger,
the size of the largest driver settings craft-print accepts, written as hex.

Comments in Cargo.toml and apps/wordcraft/Cargo.toml now describe the shared
craft-libs commit, and the shared-story text box test says where its two copies
are written.

* DOCX: open picture frame shapes and keep chart formatting from craft-libs

At this craft-libs commit the DOCX reader returns a picture frame shape
(`frame` preset) as its own shape kind, and charts carry series formatting
and axis settings. WordCraft's model had neither:

- A document with a picture frame shape failed to open with "unknown
  variant `frame`". The model bridge now reads it as a rectangle, as the
  earlier reader did.
- The chart model drops nothing the shared chart model holds: series fill,
  line width, line and fill off, markers, smooth lines, data labels and
  number format, and the category and value axes. Before, saving a chart
  with any of these wrote the chart again without them.
- A new scatter series is drawn as markers without a line, as before.
  Change Chart Type sets this when a chart becomes a scatter chart or stops
  being one, Edit Data keeps each replaced series' formatting, and Data
  Labels replaces each series' own label setting.

* Charts: draw turned axis titles and keep stored scatter charts without lines

At this craft-libs commit a chart's vertical axis title is a text item
turned -90°. WordCraft's graphic text item now carries that angle, and the
text is laid out along its box and drawn turned about the box's center.

Scatter series saved in a .json document or a recovery snapshot before
series formatting existed have no `noLine` value. They now read as markers
without a line, as they were drawn when saved.

* Charts: draw opened ODT scatter charts without lines

The ODF reader at this craft-libs commit does not read a scatter chart's
line setting, so a scatter chart saved as ODT and opened again was drawn
with lines between its points. The ODF writer writes every scatter series
as markers without a line, and the opened chart is now drawn that way.

* Charts: set scatter series to markers only for opened ODF files alone

Opened ODT, OTT and FODT files set their scatter series to markers
without a line, since the ODF reader does not read a series' line
setting. Other formats that go through the same import path keep their
series as read. A series that arrives with its markers turned off gets
them back, as Change Chart Type does, so it is still drawn.

The test for an opened scatter chart now also covers flat ODT, checks
the drawn items against the markers-only drawing, and checks that the
chart's paragraph is still written as read.

* Dependencies: take craft-libs main at 453b0da782

Every craft-libs dependency, and the craft-proof note in Cargo.toml,
moves from 3af2280ee3 to 453b0da782. Cargo.lock adds craft-dds, which
craft-image-codecs now uses; no crates.io version changes.

- craft-xls builds its test fixtures only for tests and its
  test-support feature, and they return a Result. wordcraft-formats
  gets a test-support feature that turns it on; sheet::xls_fixture is
  behind it and returns Result, and wordcraft-engine's tests enable it.
- craft_metafile::Picture has new report fields; the render tests'
  literals fill them with Default.
- The docx tests follow the writer: the font table is kept as read,
  the flat OPC comparison allows a kept part without its XML
  declaration, and the bibliography store is related from the main
  part, once, after a second save too.

* DOCX: gutter position Top is kept when a document is opened and saved

craft-libs now reads w:gutterAtTop into its settings model instead of
keeping it as unread XML, and WordCraft's settings had no field for it,
so opening and saving a document dropped the setting. Settings gets
gutter_at_top (off by default), which the document bridge carries both
ways. Layout still puts the gutter on the left.

* Charts: Change Chart Type gives a pie chart's value axis the new type's gridlines

A pie or doughnut chart keeps a plain value axis (no gridlines, axis
line on), and a new chart of any other type has gridlines and no axis
line. Change Chart Type kept the old axis, so a pie chart changed to a
column chart was drawn and saved without gridlines and with an axis
line. Change Chart Type between a round chart and another type now sets
the value axis to the new type's when it is still the old type's; an
axis that was changed stays as it is.

The progress log gets a line for the chart formatting, turned axis
titles, picture frame shapes and gutter position this branch brings.
The gutter_at_top doc comment states only what the field holds.
2026-11-17 07:24:29 -05:00
Mex 880cda902f M14: clarify finite positive CLI scale errors (#6691) 2026-11-17 13:16:17 +01:00
pakiatandClaude Opus 5.5 24885e830c Recovery: removing a record's folder waits for files Windows is still deleting (#6666)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 13:00:45 +01:00
.CooCoo 40fd69b6d8 backstage: the side menu scrolls when the window is too short for it (#6639) (#6647) 2026-11-17 12:47:26 +01:00
Thân Minh Nghĩa b7b528fbcf Layout: the automatic space after a Latin run beside East Asian text follows the Latin face (#6589) 2026-11-17 12:34:29 +01:00
Thân Minh Nghĩa c7e84ae43d #6282: print preview follows FILENAME text, fonts, texture limit and exact size (#6478) 2026-11-17 12:22:13 +01:00
Menonro c7c7421acf Shapes: a rounded rectangle without a corner size of its own takes the preset's default (#6546) (#6547) 2026-11-17 12:07:38 +01:00
Thân Minh Nghĩa bbfacd8f62 #6438: a right-to-left table's move handle sits at its top-right corner, and dragging it moves the whole table (#6477) 2026-11-17 11:54:39 +01:00
Thân Minh Nghĩa 4029e29905 #6503: a straight " in Find also finds curly double quotes (#6507) 2026-11-17 11:38:25 +01:00
Thân Minh Nghĩa 328e3738a0 #5231: Alt column widths on the ruler are in the measurement unit, written the region's way (#6408) 2026-11-17 11:23:53 +01:00
Joseph AngladaandClaude Fable 5.2 0b99c871e9 Layout: typing in a long document is about 5 times faster with proofing on (#6207)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 11:08:21 +01:00
tpawley2001 c99dd5cc6e References: TOC, index, table of figures and page cross-references use the section's page number format (#3856) (#5971) 2026-11-17 10:56:37 +01:00
Ahmad Bagheri 53e05c2420 Linux: draw on the GPU that drives the screen (#6016) (#6158) 2026-11-17 10:40:08 +01:00
Senura Dissanayake 8fced5d045 Compare: "Show changes at:" keeps its labels on one line (#6046) (#6145) 2026-11-17 10:24:06 +01:00
keithcsqueensu de694152a9 Equations: an empty shown n-ary limit survives linear text (#6076) (#6085) 2026-11-17 10:13:06 +01:00
Mex a216825578 M14: keep selected file names intact in the built-in browser (#6777) 2026-11-17 09:57:16 +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
133 changed files with 6316 additions and 972 deletions
Generated
+150 -54
View File
@@ -185,6 +185,12 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "ambient-authority"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9d4ee0d472d1cd2e28c97dfa124b3d8d992e10eb0a035f33f5d12e3a177ba3b"
[[package]]
name = "android-activity"
version = "0.6.1"
@@ -739,6 +745,24 @@ dependencies = [
"wayland-client",
]
[[package]]
name = "cap-primitives"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b5f74729fd2f44701d1a8eb47e906cdb3ccd9ec0f02baad85a744b791940b18"
dependencies = [
"ambient-authority",
"fs-set-times",
"io-extras",
"io-lifetimes 3.0.1",
"ipnet",
"maybe-owned",
"rustix 1.1.5",
"rustix-linux-procfs",
"windows-sys 0.61.2",
"winx",
]
[[package]]
name = "cc"
version = "1.6.0"
@@ -1036,7 +1060,7 @@ dependencies = [
[[package]]
name = "craft-atomic"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=762a06a4c6b48b522b7d4de84a87917ef90265e6#762a06a4c6b48b522b7d4de84a87917ef90265e6"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"rustix 1.1.5",
]
@@ -1044,17 +1068,25 @@ dependencies = [
[[package]]
name = "craft-cfb"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
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=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
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=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"rayon",
]
[[package]]
name = "craft-doc-binary"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"chrono",
"craft-cfb",
@@ -1070,7 +1102,7 @@ dependencies = [
[[package]]
name = "craft-docx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"base64 0.23.1",
"chrono",
@@ -1090,7 +1122,7 @@ dependencies = [
[[package]]
name = "craft-drawingml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-xml",
"kurbo",
@@ -1101,7 +1133,7 @@ dependencies = [
[[package]]
name = "craft-flow"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-wordmodel",
"image",
@@ -1111,19 +1143,24 @@ dependencies = [
[[package]]
name = "craft-font-catalog"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=762a06a4c6b48b522b7d4de84a87917ef90265e6#762a06a4c6b48b522b7d4de84a87917ef90265e6"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"fontique",
"harfrust 0.13.3",
"kurbo",
"skrifa 0.47.0",
"thiserror 2.0.21",
"unicode-bidi",
"unicode-segmentation",
"windows-registry",
]
[[package]]
name = "craft-font-store"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=762a06a4c6b48b522b7d4de84a87917ef90265e6#762a06a4c6b48b522b7d4de84a87917ef90265e6"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"cap-primitives",
"craft-atomic",
"craft-font-catalog",
"flate2",
@@ -1142,12 +1179,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
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=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-codepage",
"craft-flow",
@@ -1160,7 +1197,7 @@ dependencies = [
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -1183,13 +1220,17 @@ dependencies = [
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-dds",
"craft-jpeg",
"craft-metadata",
"craft-tiff",
"crc32fast",
"exr",
"flate2",
"half",
"hayro-jpeg2000",
"image",
"image-webp",
"jpeg-encoder",
@@ -1206,7 +1247,7 @@ dependencies = [
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"thiserror 2.0.21",
]
@@ -1214,7 +1255,7 @@ dependencies = [
[[package]]
name = "craft-latex"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-flow",
"craft-math",
@@ -1225,7 +1266,7 @@ dependencies = [
[[package]]
name = "craft-markdown"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-flow",
"craft-html",
@@ -1235,7 +1276,7 @@ dependencies = [
[[package]]
name = "craft-math"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-xml",
"serde",
@@ -1244,11 +1285,11 @@ dependencies = [
[[package]]
name = "craft-metadata"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"brotli-decompressor 6.3.1",
"chrono",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c)",
"craft-tiff",
"quick-xml 0.41.0",
"serde",
]
@@ -1256,7 +1297,7 @@ dependencies = [
[[package]]
name = "craft-metafile"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"encoding_rs",
"image",
@@ -1267,7 +1308,7 @@ dependencies = [
[[package]]
name = "craft-mime"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"chardetng",
"chrono",
@@ -1278,7 +1319,7 @@ dependencies = [
[[package]]
name = "craft-odf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"aes",
"base64 0.23.1",
@@ -1304,7 +1345,7 @@ dependencies = [
[[package]]
name = "craft-ods"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-odf",
"craft-sheet",
@@ -1315,7 +1356,7 @@ dependencies = [
[[package]]
name = "craft-offcrypto"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"aes",
"base64 0.23.1",
@@ -1332,7 +1373,7 @@ dependencies = [
[[package]]
name = "craft-opc"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-xml",
"craft-zip",
@@ -1343,7 +1384,7 @@ dependencies = [
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"krilla",
"thiserror 2.0.21",
@@ -1352,7 +1393,7 @@ dependencies = [
[[package]]
name = "craft-print"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=a3e0b5a9440d18f19422a9dca13516747b7d3181#a3e0b5a9440d18f19422a9dca13516747b7d3181"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-winprint-gdi",
"log",
@@ -1366,7 +1407,7 @@ dependencies = [
[[package]]
name = "craft-proof"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=01315d9c92b5b0e69082d35ca56099461a1efd45#01315d9c92b5b0e69082d35ca56099461a1efd45"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"log",
"miniz_oxide 0.9.1",
@@ -1377,7 +1418,7 @@ dependencies = [
[[package]]
name = "craft-rtf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-codepage",
"craft-flow",
@@ -1390,21 +1431,12 @@ dependencies = [
[[package]]
name = "craft-sheet"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
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=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
dependencies = [
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6ef2c053dfa758a9e292ff6ce4398bcd84aab1a1#6ef2c053dfa758a9e292ff6ce4398bcd84aab1a1"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"serde",
"thiserror 2.0.21",
@@ -1413,7 +1445,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -1433,7 +1465,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"chrono",
"dunce",
@@ -1460,7 +1492,7 @@ dependencies = [
[[package]]
name = "craft-web-api"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=fd5e2feff6ea25bed371a99cc732b3968d621505#fd5e2feff6ea25bed371a99cc732b3968d621505"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"js-sys",
"serde_json",
@@ -1471,7 +1503,7 @@ dependencies = [
[[package]]
name = "craft-web-host"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=fd5e2feff6ea25bed371a99cc732b3968d621505#fd5e2feff6ea25bed371a99cc732b3968d621505"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"wasm-bindgen",
"web-sys",
@@ -1480,7 +1512,7 @@ dependencies = [
[[package]]
name = "craft-window"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"egui",
"objc2 0.6.5",
@@ -1491,7 +1523,7 @@ dependencies = [
[[package]]
name = "craft-winprint-gdi"
version = "0.1.0"
source = "git+https://github.com/storytold/winprint.rs?rev=417ac3c4d14d3da5485a99c7d5ce8f05664ec312#417ac3c4d14d3da5485a99c7d5ce8f05664ec312"
source = "git+https://github.com/storytold/winprint.rs?rev=64e7afd4d1483512385012c340ecaaaf0f3beb7f#64e7afd4d1483512385012c340ecaaaf0f3beb7f"
dependencies = [
"windows-sys 0.61.2",
]
@@ -1499,7 +1531,7 @@ dependencies = [
[[package]]
name = "craft-wordmodel"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"base64 0.23.1",
"craft-math",
@@ -1513,7 +1545,7 @@ dependencies = [
[[package]]
name = "craft-xls"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-cfb",
"craft-sheet",
@@ -1526,7 +1558,7 @@ dependencies = [
[[package]]
name = "craft-xlsx"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"craft-drawingml",
"craft-opc",
@@ -1538,16 +1570,17 @@ dependencies = [
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"quick-xml 0.41.0",
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=41fb227542efb8a7748caa1c659f271d8605303c#41fb227542efb8a7748caa1c659f271d8605303c"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"flate2",
"thiserror 2.0.21",
@@ -2325,6 +2358,17 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "fs-set-times"
version = "0.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94e7099f6313ecacbe1256e8ff9d617b75d1bcb16a6fddef94866d225a01a14a"
dependencies = [
"io-lifetimes 2.0.4",
"rustix 1.1.5",
"windows-sys 0.52.0",
]
[[package]]
name = "fs_extra"
version = "1.3.0"
@@ -3195,6 +3239,28 @@ dependencies = [
"hybrid-array",
]
[[package]]
name = "io-extras"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20fd6de4ccfcc187e38bc21cfa543cb5a302cb86a8b114eb7f0bf0dc9f8ac00f"
dependencies = [
"io-lifetimes 3.0.1",
"windows-sys 0.52.0",
]
[[package]]
name = "io-lifetimes"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06432fb54d3be7964ecd3649233cddf80db2832f47fec34c01f65b3d9d774983"
[[package]]
name = "io-lifetimes"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0fb0570afe1fed943c5c3d4102d5358592d8625fda6a0007fdbe65a92fba96"
[[package]]
name = "ipnet"
version = "2.12.2"
@@ -3350,7 +3416,7 @@ dependencies = [
[[package]]
name = "krilla"
version = "0.8.2"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
source = "git+https://github.com/storytold/craft-libs?rev=93dac78c93a7f1fd66e2022edeb089233b1dc45a#93dac78c93a7f1fd66e2022edeb089233b1dc45a"
dependencies = [
"base64 0.22.1",
"bumpalo",
@@ -3486,6 +3552,12 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f"
[[package]]
name = "maybe-owned"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4"
[[package]]
name = "md-5"
version = "0.10.6"
@@ -4979,6 +5051,16 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rustix-linux-procfs"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fc84bf7e9aa16c4f2c758f27412dc9841341e16aa682d9c7ac308fe3ee12056"
dependencies = [
"once_cell",
"rustix 1.1.5",
]
[[package]]
name = "rustls"
version = "0.23.46"
@@ -7158,7 +7240,7 @@ dependencies = [
[[package]]
name = "winprint"
version = "0.2.1"
source = "git+https://github.com/storytold/winprint.rs?rev=5f80b9baf6b58937c401656634e42d33f00ddf49#5f80b9baf6b58937c401656634e42d33f00ddf49"
source = "git+https://github.com/storytold/winprint.rs?rev=8ae097ef941b432b0e7671b1ec3a506800e65e57#8ae097ef941b432b0e7671b1ec3a506800e65e57"
dependencies = [
"flate2",
"fmt-derive",
@@ -7173,6 +7255,7 @@ dependencies = [
"windows",
"windows-core",
"windows-implement",
"windows-numerics",
"xml",
]
@@ -7202,6 +7285,16 @@ version = "0.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ef0ffc427f045c0cc9ebffd6f4f91153dcd2a1547b5c3c29ee3f541e16e95c6"
[[package]]
name = "winx"
version = "0.36.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f3fd376f71958b862e7afb20cfe5a22830e1963462f3a17f49d82a6c1d1f42d"
dependencies = [
"bitflags 2.13.2",
"windows-sys 0.52.0",
]
[[package]]
name = "wit-bindgen"
version = "0.57.1"
@@ -7292,6 +7385,8 @@ name = "wordcraft-doc"
version = "0.18.1"
dependencies = [
"base64 0.23.1",
"craft-codepage",
"craft-zip",
"log",
"proptest",
"serde",
@@ -7375,6 +7470,7 @@ dependencies = [
"harfrust 0.13.3",
"kurbo",
"log",
"serde_json",
"skrifa 0.47.0",
"unicode-bidi",
"unicode-script",
@@ -7416,7 +7512,7 @@ name = "wordcraft-geom"
version = "0.18.1"
dependencies = [
"craft-image-codecs",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6ef2c053dfa758a9e292ff6ce4398bcd84aab1a1)",
"craft-tiff",
"image",
"serde",
]
+34 -32
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 the merge of #590 (ca-ES-valencia reads the ca-valencia
# pack), after #589 (French elision suggestions first) and #497 (flag-based compounding), which also
# has #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 = "01315d9c92b5b0e69082d35ca56099461a1efd45", version = "=0.1.0" }
craft-font-store = { git = "https://github.com/storytold/craft-libs", rev = "762a06a4c6b48b522b7d4de84a87917ef90265e6", version = "=0.1.0" }
craft-font-catalog = { git = "https://github.com/storytold/craft-libs", rev = "762a06a4c6b48b522b7d4de84a87917ef90265e6", version = "=0.1.0" }
# 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 = "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 = "41fb227542efb8a7748caa1c659f271d8605303c", 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 = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "6ef2c053dfa758a9e292ff6ce4398bcd84aab1a1", version = "=0.1.0" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", 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 = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["doc"] }
craft-doc-binary = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-flow = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-odf = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["text"] }
craft-markdown = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-html = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["flow"] }
craft-latex = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-math = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0", features = ["detect"] }
craft-sheet = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-ods = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", 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 = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
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" }
+6 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -266,7 +266,7 @@ fn parse_scale_arg(command: &str, args: &[String]) -> Result<Option<f64>, String
OptionArg::Value(val) => {
let scale = val.parse::<f64>().map_err(|_| format!("{command}: invalid --scale `{val}`: not a number"))?;
if !scale.is_finite() || scale <= 0.0 {
return Err(format!("{command}: invalid --scale `{val}`: scale must be a positive number"));
return Err(format!("{command}: invalid --scale `{val}`: scale must be a finite positive number"));
}
Ok(Some(scale))
}
+8 -8
View File
@@ -267,19 +267,19 @@ fn render_rejects_missing_or_invalid_page_and_scale() {
assert_eq!(run(&args(&["render", "missing.docx", "out.png", "--page", "-1"])), Err("render: invalid --page `-1`: not a valid integer".into()));
assert_eq!(
run(&args(&["render", "missing.docx", "out.png", "--scale", "0"])),
Err("render: invalid --scale `0`: scale must be a positive number".into())
Err("render: invalid --scale `0`: scale must be a finite positive number".into())
);
assert_eq!(
run(&args(&["render", "missing.docx", "out.png", "--scale", "-2"])),
Err("render: invalid --scale `-2`: scale must be a positive number".into())
Err("render: invalid --scale `-2`: scale must be a finite positive number".into())
);
assert_eq!(
run(&args(&["render", "missing.docx", "out.png", "--scale", "NaN"])),
Err("render: invalid --scale `NaN`: scale must be a positive number".into())
Err("render: invalid --scale `NaN`: scale must be a finite positive number".into())
);
assert_eq!(
run(&args(&["render", "missing.docx", "out.png", "--scale", "inf"])),
Err("render: invalid --scale `inf`: scale must be a positive number".into())
Err("render: invalid --scale `inf`: scale must be a finite positive number".into())
);
// Duplicate options
@@ -322,19 +322,19 @@ fn run_rejects_missing_or_invalid_page_and_scale() {
);
assert_eq!(
run(&args(&["run", "--template", "sample", "--save", "out.png", "--scale", "0"])),
Err("run: invalid --scale `0`: scale must be a positive number".into())
Err("run: invalid --scale `0`: scale must be a finite positive number".into())
);
assert_eq!(
run(&args(&["run", "--template", "sample", "--save", "out.png", "--scale", "-1"])),
Err("run: invalid --scale `-1`: scale must be a positive number".into())
Err("run: invalid --scale `-1`: scale must be a finite positive number".into())
);
assert_eq!(
run(&args(&["run", "--template", "sample", "--save", "out.png", "--scale", "NaN"])),
Err("run: invalid --scale `NaN`: scale must be a positive number".into())
Err("run: invalid --scale `NaN`: scale must be a finite positive number".into())
);
assert_eq!(
run(&args(&["run", "--template", "sample", "--save", "out.png", "--scale", "inf"])),
Err("run: invalid --scale `inf`: scale must be a positive number".into())
Err("run: invalid --scale `inf`: scale must be a finite positive number".into())
);
// Duplicate options
+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 = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" }
craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", 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());
+2 -2
View File
@@ -84,9 +84,9 @@ windows = { version = "0.62", features = ["UI_ViewManagement", "System_UserProfi
clipboard-win = { version = "5.4", features = ["std"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Pinned apart from craft-ui, which doesn't depend on it: `printer_queues`, the printer list that
# 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 = "a3e0b5a9440d18f19422a9dca13516747b7d3181" }
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.
+298
View File
@@ -0,0 +1,298 @@
//! Linux: draw on the GPU that drives the screen.
//!
//! egui-wgpu picks the discrete GPU by default (`PowerPreference::HighPerformance`). On a laptop
//! whose integrated GPU drives the panel, every frame drawn on the discrete one is copied across
//! to it, and the discrete GPU leaves its idle state whenever the window redraws: on an Intel HD
//! 530 + NVIDIA Quadro M4000M laptop, from 7 W at P8 to over 20 W at P0/P1. Forcing the integrated
//! GPU instead breaks desktops whose screen is on the discrete card (storytold/pdfcraft#96), and a
//! desktop with a monitor on each card needs the discrete one (storytold/pdfcraft#445). So the
//! choice follows the connected displays, as in craft-anything (storytold/craft-anything#571):
//!
//! 1. `WGPU_ADAPTER_NAME` (part of the adapter's name) or `WGPU_POWER_PREF` (`high`, `low`,
//! `none`), when set, decide;
//! 2. a connected built-in panel: the GPU behind it;
//! 3. displays on only one GPU: that GPU;
//! 4. otherwise (displays on several GPUs, or nothing readable): the discrete GPU, as before.
mod drm;
use drm::DisplayCard;
use eframe::wgpu::{DeviceType, PowerPreference};
/// What we know of one adapter wgpu offers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Candidate {
pub name: String,
pub vendor: u32,
pub device: u32,
/// `0000:05:00.0`, lowercase; empty when the driver doesn't say (always, under GL).
pub pci_slot: String,
pub device_type: DeviceType,
pub gl: bool,
}
/// What the person asked for through wgpu's environment variables.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Asked {
pub adapter_name: Option<String>,
pub power: Option<PowerPreference>,
}
/// Why an adapter was chosen, for the log.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Why {
AskedByName,
AskedPower,
BuiltInPanel,
OnlyGpuWithDisplays,
Fallback,
}
/// The index of the adapter to draw with, and why; `None` when there is none.
pub(crate) fn choose(candidates: &[Candidate], cards: &[DisplayCard], asked: &Asked) -> Option<(usize, Why)> {
if let Some(want) = asked.adapter_name.as_deref().map(str::to_lowercase).filter(|w| !w.is_empty()) {
// As wgpu's own `WGPU_ADAPTER_NAME`: part of the name, any case. No match: the usual choice.
if let Some(i) = candidates.iter().position(|c| c.name.to_lowercase().contains(&want)) {
return Some((i, Why::AskedByName));
}
log::warn!("WGPU_ADAPTER_NAME={want}: no graphics adapter has that in its name");
}
if let Some(power) = asked.power {
return by_kind(candidates, power).map(|i| (i, Why::AskedPower));
}
let panels: Vec<&DisplayCard> = cards.iter().filter(|c| c.built_in_panel).collect();
let with_displays: Vec<&DisplayCard> = cards.iter().filter(|c| c.displays > 0).collect();
let target = if !panels.is_empty() {
Some((panels, Why::BuiltInPanel))
} else if with_displays.len() == 1 {
Some((with_displays, Why::OnlyGpuWithDisplays))
} else {
None
};
if let Some((wanted, why)) = target {
// Vulkan before GL for the same card; otherwise wgpu's order.
let found = candidates.iter().enumerate().filter(|(_, c)| wanted.iter().any(|card| drives(card, c, cards))).min_by_key(|(_, c)| c.gl);
if let Some((i, _)) = found {
return Some((i, why));
}
}
by_kind(candidates, PowerPreference::HighPerformance).map(|i| (i, Why::Fallback))
}
/// Whether `card` is the GPU behind `adapter`: by PCI address when the driver gives one, else by
/// PCI ids, which count only when no other card has the same ids (two cards of one model); GL
/// gives only a vendor, which counts only when no other card has that vendor.
fn drives(card: &DisplayCard, adapter: &Candidate, all: &[DisplayCard]) -> bool {
if !adapter.pci_slot.is_empty() && !card.pci_slot.is_empty() {
return adapter.pci_slot == card.pci_slot;
}
if adapter.device != 0 {
return adapter.vendor == card.vendor
&& adapter.device == card.device
&& all.iter().filter(|c| c.vendor == card.vendor && c.device == card.device).count() == 1;
}
adapter.vendor != 0 && adapter.vendor == card.vendor && all.iter().filter(|c| c.vendor == card.vendor).count() == 1
}
/// The first adapter in wgpu's own order for that power preference.
fn by_kind(candidates: &[Candidate], power: PowerPreference) -> Option<usize> {
let rank = |t: DeviceType| match (power, t) {
(PowerPreference::None, _) => 0,
(PowerPreference::LowPower, DeviceType::IntegratedGpu) | (PowerPreference::HighPerformance, DeviceType::DiscreteGpu) => 1,
(_, DeviceType::IntegratedGpu | DeviceType::DiscreteGpu) => 2,
(_, DeviceType::Other) => 3,
(_, DeviceType::VirtualGpu) => 4,
(_, DeviceType::Cpu) => 5,
};
candidates.iter().enumerate().min_by_key(|(_, c)| rank(c.device_type)).map(|(i, _)| i)
}
#[cfg(target_os = "linux")]
impl Candidate {
fn of(info: &eframe::wgpu::AdapterInfo) -> Self {
Candidate {
name: info.name.clone(),
vendor: info.vendor,
device: info.device,
pci_slot: info.device_pci_bus_id.to_ascii_lowercase(),
device_type: info.device_type,
gl: info.backend == eframe::wgpu::Backend::Gl,
}
}
}
#[cfg(target_os = "linux")]
impl Asked {
fn from_env() -> Self {
Asked { adapter_name: std::env::var("WGPU_ADAPTER_NAME").ok(), power: PowerPreference::from_env() }
}
}
/// The adapter that draws the window, among those that can draw to it.
#[cfg(target_os = "linux")]
pub(crate) fn pick_adapter(adapters: &[eframe::wgpu::Adapter], surface: Option<&eframe::wgpu::Surface<'_>>) -> Result<eframe::wgpu::Adapter, String> {
let usable: Vec<&eframe::wgpu::Adapter> = adapters.iter().filter(|a| surface.is_none_or(|s| a.is_surface_supported(s))).collect();
let candidates: Vec<Candidate> = usable.iter().map(|a| Candidate::of(&a.get_info())).collect();
let cards = drm::display_cards();
let picked = choose(&candidates, &cards, &Asked::from_env()).and_then(|(i, why)| Some((*usable.get(i)?, why)));
match picked {
Some((a, why)) => {
log::info!("drawing with {:?} ({why:?}); displays: {cards:?}", a.get_info());
Ok(a.clone())
}
None => Err(format!("None of the {} graphics adapters found can draw this window.", adapters.len())),
}
}
#[cfg(test)]
mod tests {
use super::*;
const AMD: (u32, u32, &str) = (0x1002, 0x1638, "0000:05:00.0");
const NVIDIA: (u32, u32, &str) = (0x10de, 0x25a2, "0000:01:00.0");
const INTEL: (u32, u32, &str) = (0x8086, 0xa780, "0000:00:02.0");
fn vulkan(name: &str, (vendor, device, slot): (u32, u32, &str), device_type: DeviceType) -> Candidate {
Candidate { name: name.into(), vendor, device, pci_slot: slot.into(), device_type, gl: false }
}
fn gl(name: &str, vendor: u32, device_type: DeviceType) -> Candidate {
Candidate { name: name.into(), vendor, device: 0, pci_slot: String::new(), device_type, gl: true }
}
fn card((vendor, device, slot): (u32, u32, &str), displays: u32, built_in_panel: bool) -> DisplayCard {
DisplayCard { vendor, device, pci_slot: slot.into(), displays, built_in_panel }
}
/// What wgpu offers on a laptop whose AMD iGPU drives the panel, in its order (Vulkan, then GL).
fn laptop_adapters() -> Vec<Candidate> {
vec![
vulkan("AMD Radeon Graphics (RADV RENOIR)", AMD, DeviceType::IntegratedGpu),
vulkan("NVIDIA GeForce RTX 3050 Laptop GPU", NVIDIA, DeviceType::DiscreteGpu),
gl("AMD Radeon Graphics (radeonsi, renoir)", 0x1002, DeviceType::IntegratedGpu),
]
}
/// The iGPU drives the panel; egui-wgpu's default drew on the discrete GPU.
#[test]
fn a_laptop_draws_on_the_gpu_behind_its_panel() {
let cards = [card(NVIDIA, 0, false), card(AMD, 1, true)];
assert_eq!(choose(&laptop_adapters(), &cards, &Asked::default()), Some((0, Why::BuiltInPanel)));
// The same with the discrete GPU listed first.
let mut swapped = laptop_adapters();
swapped.swap(0, 1);
assert_eq!(choose(&swapped, &cards, &Asked::default()), Some((1, Why::BuiltInPanel)));
}
/// The panel's GPU stays the choice with an external monitor on the discrete card as well.
#[test]
fn the_panel_wins_over_a_monitor_on_the_other_card() {
let cards = [card(NVIDIA, 1, false), card(AMD, 1, true)];
assert_eq!(choose(&laptop_adapters(), &cards, &Asked::default()), Some((0, Why::BuiltInPanel)));
}
/// A desktop with its monitor on the discrete card and the iGPU left on (a low-power default
/// draws on the iGPU there, and the window can't start: the case the panel rule must not break).
#[test]
fn a_desktop_draws_on_the_only_card_with_a_monitor() {
let adapters =
[vulkan("Intel(R) Graphics (RPL-S)", INTEL, DeviceType::IntegratedGpu), vulkan("NVIDIA GeForce RTX", NVIDIA, DeviceType::DiscreteGpu)];
let cards = [card(INTEL, 0, false), card(NVIDIA, 1, false)];
assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::OnlyGpuWithDisplays)));
// And on the iGPU when that's where the only monitor is.
let cards = [card(INTEL, 2, false), card(NVIDIA, 0, false)];
assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((0, Why::OnlyGpuWithDisplays)));
}
/// A desktop with a monitor on each card: the discrete GPU, as before (an iGPU choice there
/// lost its surface as soon as it was set up).
#[test]
fn monitors_on_two_cards_keep_the_discrete_gpu() {
let adapters =
[vulkan("Intel(R) Graphics (RPL-S)", INTEL, DeviceType::IntegratedGpu), vulkan("NVIDIA GeForce RTX", NVIDIA, DeviceType::DiscreteGpu)];
let cards = [card(INTEL, 1, false), card(NVIDIA, 1, false)];
assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::Fallback)));
}
/// Nothing readable (no `/sys`, or a card the drivers don't name): the discrete GPU, as before.
#[test]
fn without_display_facts_the_old_choice_stands() {
assert_eq!(choose(&laptop_adapters(), &[], &Asked::default()), Some((1, Why::Fallback)));
let unknown = [card((0x1234, 0x1, "0000:09:00.0"), 1, true)];
assert_eq!(choose(&laptop_adapters(), &unknown, &Asked::default()), Some((1, Why::Fallback)));
assert_eq!(choose(&[], &[card(AMD, 1, true)], &Asked::default()), None);
}
/// The environment variables still decide: prime-run style requests reach the NVIDIA card.
#[test]
fn wgpu_environment_variables_decide_when_set() {
let cards = [card(NVIDIA, 0, false), card(AMD, 1, true)];
let asked = |name: Option<&str>, power: Option<PowerPreference>| Asked { adapter_name: name.map(Into::into), power };
assert_eq!(choose(&laptop_adapters(), &cards, &asked(None, Some(PowerPreference::HighPerformance))), Some((1, Why::AskedPower)));
assert_eq!(choose(&laptop_adapters(), &cards, &asked(None, Some(PowerPreference::LowPower))), Some((0, Why::AskedPower)));
assert_eq!(choose(&laptop_adapters(), &cards, &asked(None, Some(PowerPreference::None))), Some((0, Why::AskedPower)));
assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some("nvidia"), None)), Some((1, Why::AskedByName)));
assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some("RadeonSI"), Some(PowerPreference::HighPerformance))), Some((2, Why::AskedByName)));
// A name nothing has: the usual choice, not a failure.
assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some("matrox"), None)), Some((0, Why::BuiltInPanel)));
assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some(""), None)), Some((0, Why::BuiltInPanel)));
}
/// Under `WGPU_BACKEND=gl` there's no PCI address or device id: the vendor alone finds the
/// card, but only when no other card shares it.
#[test]
fn gl_adapters_are_matched_by_vendor_only_when_unambiguous() {
let adapters =
[gl("NVIDIA GeForce RTX 3050", 0x10de, DeviceType::Other), gl("AMD Radeon Graphics (radeonsi)", 0x1002, DeviceType::IntegratedGpu)];
let cards = [card(NVIDIA, 0, false), card(AMD, 1, true)];
assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::BuiltInPanel)));
// Two AMD cards: the vendor can't tell them apart, so the old choice stands.
let two_amd = [card((0x1002, 0x73df, "0000:03:00.0"), 0, false), card(AMD, 1, true)];
let adapters = [gl("AMD Radeon RX 6700", 0x1002, DeviceType::Other), gl("AMD Radeon Graphics", 0x1002, DeviceType::IntegratedGpu)];
assert_eq!(choose(&adapters, &two_amd, &Asked::default()), Some((1, Why::Fallback)));
}
/// Two adapters for the panel's card (Vulkan and GL): Vulkan, whatever the order.
#[test]
fn vulkan_comes_before_gl_for_the_same_card() {
let adapters = [
gl("AMD Radeon Graphics (radeonsi)", 0x1002, DeviceType::IntegratedGpu),
vulkan("AMD Radeon Graphics (RADV RENOIR)", AMD, DeviceType::IntegratedGpu),
];
assert_eq!(choose(&adapters, &[card(AMD, 1, true)], &Asked::default()), Some((1, Why::BuiltInPanel)));
}
/// A laptop with the lid closed and a monitor on the NVIDIA card: the switched-off panel isn't
/// read as one (`drm` skips it), so the card with the monitor draws.
#[test]
fn a_closed_laptop_draws_on_the_card_with_the_monitor() {
let cards = [card(NVIDIA, 1, false), card(AMD, 0, false)];
assert_eq!(choose(&laptop_adapters(), &cards, &Asked::default()), Some((1, Why::OnlyGpuWithDisplays)));
}
/// Two cards of one model have the same PCI ids: only the PCI address tells them apart. Without
/// one from the driver the display rule gives way to the old choice instead of guessing.
#[test]
fn identical_cards_are_told_apart_only_by_pci_address() {
const SECOND: (u32, u32, &str) = (0x10de, 0x25a2, "0000:02:00.0");
let cards = [card(NVIDIA, 0, false), card(SECOND, 1, false)];
let with_slots = [vulkan("NVIDIA A", NVIDIA, DeviceType::DiscreteGpu), vulkan("NVIDIA B", SECOND, DeviceType::DiscreteGpu)];
assert_eq!(choose(&with_slots, &cards, &Asked::default()), Some((1, Why::OnlyGpuWithDisplays)));
let without: Vec<Candidate> = with_slots.iter().cloned().map(|c| Candidate { pci_slot: String::new(), ..c }).collect();
assert_eq!(choose(&without, &cards, &Asked::default()), Some((0, Why::Fallback)));
}
/// The laptop this was measured on: Intel HD 530 behind the panel (`card2-eDP-1`), a Quadro
/// M4000M with nothing connected, and the Quadro offered first by wgpu.
#[test]
fn an_intel_and_nvidia_laptop_draws_on_intel() {
const HD530: (u32, u32, &str) = (0x8086, 0x191b, "0000:00:02.0");
const M4000M: (u32, u32, &str) = (0x10de, 0x13f9, "0000:01:00.0");
let adapters = [
vulkan("Quadro M4000M", M4000M, DeviceType::DiscreteGpu),
vulkan("Intel(R) HD Graphics 530 (SKL GT2)", HD530, DeviceType::IntegratedGpu),
];
let cards = [card(M4000M, 0, false), card(HD530, 1, true)];
assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::BuiltInPanel)));
}
}
+237
View File
@@ -0,0 +1,237 @@
//! Which graphics cards drive displays, as the system reports them (Linux: `/sys/class/drm`).
//!
//! A window drawn on one card and shown on a screen attached to another is copied across on every
//! frame, so the app draws on the card that drives the screen (see the parent module).
use std::io::Read as _;
use std::path::Path;
/// A graphics card and the displays connected to it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct DisplayCard {
/// PCI vendor id: 0x1002 AMD, 0x10de NVIDIA, 0x8086 Intel.
pub vendor: u32,
/// PCI device id.
pub device: u32,
/// PCI address in the form `0000:05:00.0`, lowercase; empty when the system doesn't give one.
pub pci_slot: String,
/// Connected displays that are switched on, of any kind, built-in panel included.
pub displays: u32,
/// A connected, switched-on built-in panel (eDP, LVDS or DSI): this card drives a laptop's
/// own screen.
pub built_in_panel: bool,
}
/// Connector kinds that are a computer's own screen rather than a monitor plugged into it.
const BUILT_IN_PANELS: &[&str] = &["eDP", "LVDS", "DSI"];
/// At most this many entries are read from the DRM folder.
const MAX_ENTRIES: usize = 512;
/// At most this many bytes are read from one sysfs file.
const MAX_FILE: u64 = 4096;
/// The graphics cards on this computer and what's connected to them. Empty where the system
/// doesn't say (a sandbox without `/sys`).
#[cfg(target_os = "linux")]
pub(crate) fn display_cards() -> Vec<DisplayCard> {
display_cards_in(Path::new("/sys/class/drm"))
}
/// [`display_cards`] read from a DRM folder laid out like `/sys/class/drm`: a `cardN` entry per
/// card, with the card's PCI ids in `cardN/device/uevent`, and a `cardN-<kind>-<n>` entry per
/// connector, with `connected` or `disconnected` in its `status` and, where the kernel gives it,
/// `enabled` or `disabled` in its `enabled`. Cards without PCI ids (a firmware framebuffer) are
/// left out.
pub(crate) fn display_cards_in(drm: &Path) -> Vec<DisplayCard> {
let Ok(entries) = std::fs::read_dir(drm) else {
return Vec::new();
};
let mut names: Vec<String> = entries.take(MAX_ENTRIES).filter_map(|e| e.ok()?.file_name().into_string().ok()).collect();
names.sort();
let cards = names.iter().filter(|n| is_card(n));
cards
.filter_map(|card| {
let uevent = read_small(&drm.join(card).join("device").join("uevent"))?;
let (vendor, device) = pci_ids(&uevent)?;
let pci_slot = uevent_value(&uevent, "PCI_SLOT_NAME").unwrap_or_default().to_ascii_lowercase();
let prefix = format!("{card}-");
let mut out = DisplayCard { vendor, device, pci_slot, displays: 0, built_in_panel: false };
for connector in names.iter().filter_map(|n| n.strip_prefix(&prefix)) {
let dir = drm.join(format!("{prefix}{connector}"));
let status = read_small(&dir.join("status")).unwrap_or_default();
// A panel can be connected but switched off (a closed lid with a monitor in use):
// `enabled` then reads `disabled`, and it drives nothing. No `enabled`: as before.
let enabled = read_small(&dir.join("enabled")).unwrap_or_default();
if status.trim() != "connected" || enabled.trim() == "disabled" {
continue;
}
out.displays = out.displays.saturating_add(1);
if BUILT_IN_PANELS.contains(&connector_kind(connector)) {
out.built_in_panel = true;
}
}
Some(out)
})
.collect()
}
/// `card0`, `card12`: a card, not a connector (`card0-eDP-1`) or a render node.
fn is_card(name: &str) -> bool {
name.strip_prefix("card").is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()))
}
/// `eDP-1` → `eDP`, `HDMI-A-1` → `HDMI-A`: the connector's name without its number.
fn connector_kind(connector: &str) -> &str {
match connector.rsplit_once('-') {
Some((kind, n)) if !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()) => kind,
_ => connector,
}
}
/// `PCI_ID=1002:1638` → (0x1002, 0x1638).
fn pci_ids(uevent: &str) -> Option<(u32, u32)> {
let (vendor, device) = uevent_value(uevent, "PCI_ID")?.split_once(':')?;
Some((u32::from_str_radix(vendor.trim(), 16).ok()?, u32::from_str_radix(device.trim(), 16).ok()?))
}
fn uevent_value<'a>(uevent: &'a str, key: &str) -> Option<&'a str> {
uevent.lines().find_map(|l| l.strip_prefix(key)?.strip_prefix('=')).map(str::trim)
}
fn read_small(path: &Path) -> Option<String> {
let mut text = String::new();
std::fs::File::open(path).ok()?.take(MAX_FILE).read_to_string(&mut text).ok()?;
Some(text)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
/// A folder of its own under the system's temporary folder, removed when dropped.
struct TempDir(PathBuf);
impl TempDir {
fn new() -> Self {
static NEXT: AtomicUsize = AtomicUsize::new(0);
let n = NEXT.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("wordcraft-drm-test-{}-{n}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).expect("temp dir");
TempDir(dir)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
/// A fake `/sys/class/drm`: cards as (name, PCI_ID, PCI_SLOT_NAME), connectors as
/// (name, status).
fn drm(cards: &[(&str, &str, &str)], connectors: &[(&str, &str)]) -> TempDir {
let dir = TempDir::new();
for (card, id, slot) in cards {
let device = dir.path().join(card).join("device");
fs::create_dir_all(&device).expect("card folder");
fs::write(device.join("uevent"), format!("DRIVER=x\nPCI_CLASS=30000\nPCI_ID={id}\nPCI_SLOT_NAME={slot}\n")).expect("uevent");
}
for (connector, status) in connectors {
let c = dir.path().join(connector);
fs::create_dir_all(&c).expect("connector folder");
fs::write(c.join("status"), format!("{status}\n")).expect("status");
}
fs::create_dir_all(dir.path().join("renderD128")).expect("render node");
fs::write(dir.path().join("version"), "drm 1.1.0\n").expect("version");
dir
}
/// A laptop whose AMD iGPU drives the panel, with an NVIDIA card that has no outputs.
#[test]
fn a_laptop_panel_is_found_on_the_integrated_gpu() {
let d = drm(
&[("card0", "10DE:25A2", "0000:01:00.0"), ("card1", "1002:1638", "0000:05:00.0")],
&[("card1-DP-1", "disconnected"), ("card1-eDP-1", "connected"), ("card1-HDMI-A-1", "disconnected")],
);
let cards = display_cards_in(d.path());
assert_eq!(
cards,
vec![
DisplayCard { vendor: 0x10de, device: 0x25a2, pci_slot: "0000:01:00.0".into(), displays: 0, built_in_panel: false },
DisplayCard { vendor: 0x1002, device: 0x1638, pci_slot: "0000:05:00.0".into(), displays: 1, built_in_panel: true },
]
);
}
/// A desktop with a monitor on each card: displays on both, no built-in panel.
#[test]
fn monitors_on_two_cards_are_counted_on_each() {
let d = drm(
&[("card0", "8086:A780", "0000:00:02.0"), ("card1", "10de:2206", "0000:01:00.0")],
&[("card0-HDMI-A-1", "connected"), ("card1-DP-1", "connected"), ("card1-DP-2", "connected"), ("card1-DP-3", "disconnected")],
);
let cards = display_cards_in(d.path());
assert_eq!(cards.iter().map(|c| (c.displays, c.built_in_panel)).collect::<Vec<_>>(), vec![(1, false), (2, false)]);
}
/// `card10` is a different card from `card1`, and its connectors aren't `card1`'s.
#[test]
fn card_numbers_are_not_prefixes_of_each_other() {
let d = drm(&[("card1", "1002:1638", "0000:05:00.0"), ("card10", "10de:25a2", "0000:01:00.0")], &[("card10-DP-1", "connected")]);
let cards = display_cards_in(d.path());
assert_eq!(cards.iter().map(|c| (c.vendor, c.displays)).collect::<Vec<_>>(), vec![(0x1002, 0), (0x10de, 1)]);
}
/// LVDS and DSI panels are built in too; HDMI-A is a monitor, whatever its number.
#[test]
fn built_in_panels_are_told_from_monitors_by_connector_kind() {
assert_eq!(connector_kind("eDP-1"), "eDP");
assert_eq!(connector_kind("HDMI-A-1"), "HDMI-A");
assert_eq!(connector_kind("DSI-12"), "DSI");
assert_eq!(connector_kind("Virtual"), "Virtual");
let d = drm(&[("card0", "8086:9a49", "0000:00:02.0")], &[("card0-LVDS-1", "connected")]);
assert!(display_cards_in(d.path()).iter().all(|c| c.built_in_panel));
}
/// A firmware framebuffer has no PCI ids; unreadable or odd entries are skipped, never a panic.
#[test]
fn what_cant_be_read_is_left_out() {
let d = drm(&[("card0", "nonsense", ""), ("card1", "1002:", "")], &[("card0-Virtual-1", "connected")]);
fs::create_dir_all(d.path().join("card2")).expect("card without device");
fs::create_dir_all(d.path().join("card")).expect("bare name");
assert!(display_cards_in(d.path()).is_empty());
assert!(display_cards_in(&d.path().join("missing")).is_empty());
assert_eq!(pci_ids("PCI_ID=10de:25a2"), Some((0x10de, 0x25a2)));
assert_eq!(pci_ids("PCI_IDX=10de:25a2"), None);
}
/// A status of `unknown` (some virtual outputs) isn't a connected display.
#[test]
fn only_connected_counts() {
let d = drm(&[("card0", "1af4:1050", "0000:00:01.0")], &[("card0-Virtual-1", "unknown")]);
assert_eq!(display_cards_in(d.path()).first().map(|c| c.displays), Some(0));
}
/// A laptop with its lid closed and a monitor on the discrete card: the panel still reads
/// `connected` but is switched off, so it neither counts nor marks its card as the panel's.
#[test]
fn a_switched_off_panel_does_not_count() {
let d = drm(
&[("card0", "10de:25a2", "0000:01:00.0"), ("card1", "1002:1638", "0000:05:00.0")],
&[("card0-HDMI-A-1", "connected"), ("card1-eDP-1", "connected")],
);
fs::write(d.path().join("card0-HDMI-A-1").join("enabled"), "enabled\n").expect("enabled");
fs::write(d.path().join("card1-eDP-1").join("enabled"), "disabled\n").expect("enabled");
let cards = display_cards_in(d.path());
assert_eq!(cards.iter().map(|c| (c.vendor, c.displays, c.built_in_panel)).collect::<Vec<_>>(), vec![(0x10de, 1, false), (0x1002, 0, false)]);
}
}
+10
View File
@@ -20,6 +20,8 @@ mod default_apps;
mod file_dialogs;
#[cfg(any(target_os = "windows", target_os = "macos", test))]
mod graphics;
#[cfg(any(target_os = "linux", test))]
mod linux_gpu;
mod logging;
#[cfg(all(unix, not(target_os = "macos")))]
mod portal_save;
@@ -685,6 +687,13 @@ fn main() -> eframe::Result {
}));
}
}
// egui-wgpu draws on the discrete GPU by default; on a laptop whose integrated GPU drives the
// panel, that wakes the discrete GPU whenever the window redraws. Draw on the GPU that drives
// the screen; `WGPU_ADAPTER_NAME` and `WGPU_POWER_PREF` still decide (see linux_gpu.rs).
#[cfg(target_os = "linux")]
if let eframe::egui_wgpu::WgpuSetup::CreateNew(setup) = &mut options.wgpu_options.wgpu_setup {
setup.native_adapter_selector = Some(std::sync::Arc::new(linux_gpu::pick_adapter));
}
// Registered before the event loop starts, so it catches the Finder event that launched us as
// well as later ones. Lives until the event loop returns; the app creator only borrows it.
#[cfg(target_os = "macos")]
@@ -719,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()
{
+2
View File
@@ -89,9 +89,11 @@ pub fn submit() -> Submit {
Duplex::Off => craft_print::Duplex::Off,
}),
grayscale: settings.grayscale,
grey_pages: false,
scaling: craft_print::Scaling::ShrinkToFit,
title: "WordCraft document".into(),
options,
driver_settings: None,
};
#[cfg(not(windows))]
let sent = craft_print::submit_cancellable(&bytes, &job, &mut |_, _| {}, &token).map_err(|error| error.to_string());
+40 -8
View File
@@ -15,6 +15,8 @@ use serde::{Deserialize, Serialize};
const FLAG: &str = "--wordcraft-print-worker";
const PREFIX: &str = "WORDCRAFT_PRINT:";
const MAX_MESSAGE: u64 = 64 * 1024;
/// The job's options, and a driver's settings of up to 1 MiB written as hex.
const MAX_REQUEST: u64 = MAX_MESSAGE + 2 * 1024 * 1024;
const MAX_PDF: u64 = 512 * 1024 * 1024;
const CANCELLED: &str = "the print job was cancelled before anything was sent to the printer";
@@ -32,9 +34,12 @@ struct Request {
collate: bool,
duplex: Option<Duplex>,
grayscale: bool,
grey_pages: bool,
shrink: bool,
title: String,
options: Vec<(String, String)>,
/// The printer the driver's settings were chosen for, and their DEVMODE as hex.
driver_settings: Option<(String, String)>,
}
impl Request {
@@ -50,15 +55,21 @@ impl Request {
craft_print::Duplex::ShortEdge => Duplex::ShortEdge,
}),
grayscale: job.grayscale,
grey_pages: job.grey_pages,
shrink: job.scaling == craft_print::Scaling::ShrinkToFit,
title: job.title.clone(),
options: job.options.clone(),
driver_settings: job.driver_settings.as_ref().map(|settings| (settings.printer().to_owned(), settings.dev_mode().to_hex())),
}
}
/// Restore the job without substituting printer defaults or changing copy semantics.
fn job(self) -> craft_print::Job {
craft_print::Job {
fn job(self) -> Result<craft_print::Job, String> {
let driver_settings = match self.driver_settings {
Some((printer, hex)) => Some(craft_print::DriverSettings::new(printer, craft_print::rgb::DevMode::from_hex(&hex).map_err(error)?)),
None => None,
};
Ok(craft_print::Job {
printer: self.printer,
copies: self.copies,
collate: self.collate,
@@ -68,10 +79,12 @@ impl Request {
Duplex::ShortEdge => craft_print::Duplex::ShortEdge,
}),
grayscale: self.grayscale,
grey_pages: self.grey_pages,
scaling: if self.shrink { craft_print::Scaling::ShrinkToFit } else { craft_print::Scaling::Exact },
title: self.title,
options: self.options,
}
driver_settings,
})
}
}
@@ -166,7 +179,7 @@ impl Submission {
return Err("the print PDF exceeds the 512 MiB helper limit".into());
}
let request = serde_json::to_vec(&Request::from_job(job)).map_err(error)?;
if request.len() as u64 > MAX_MESSAGE {
if request.len() as u64 > MAX_REQUEST {
return Err("the print settings exceed the helper message limit".into());
}
let directory = tempfile::tempdir().map_err(error)?;
@@ -323,10 +336,10 @@ fn helper(
})
.map_err(error)?;
let request: Request =
serde_json::from_slice(&bounded(std::fs::File::open(directory.join("job.json")).map_err(error)?, MAX_MESSAGE)?).map_err(error)?;
serde_json::from_slice(&bounded(std::fs::File::open(directory.join("job.json")).map_err(error)?, MAX_REQUEST)?).map_err(error)?;
let pdf = bounded(std::fs::File::open(directory.join("input.pdf")).map_err(error)?, MAX_PDF)?;
remove_inputs(directory)?;
let result = submit(&pdf, &request.job(), &cancel);
let result = submit(&pdf, &request.job()?, &cancel);
emit(&Event::Done(result))
}
@@ -463,21 +476,40 @@ mod tests {
}
#[test]
/// Job transport preserves every explicit setting, including Unicode and duplex direction.
/// Job transport preserves every explicit setting, including Unicode, duplex direction and the
/// driver's own settings.
fn job_options_survive_the_process_boundary() {
// A DEVMODEW header of 156 bytes (`dmSize` at 68) and a 4-byte private part (`dmDriverExtra` at 70).
let mut dev_mode = vec![0u8; 160];
dev_mode[68..70].copy_from_slice(&156u16.to_le_bytes());
dev_mode[70..72].copy_from_slice(&4u16.to_le_bytes());
dev_mode[156..].copy_from_slice(&[1, 2, 3, 4]);
let dev_mode = craft_print::rgb::DevMode::from_bytes(dev_mode).unwrap();
let expected = craft_print::Job {
printer: Some("Office λ".into()),
copies: 3,
collate: false,
duplex: Some(craft_print::Duplex::ShortEdge),
grayscale: true,
grey_pages: true,
scaling: craft_print::Scaling::ShrinkToFit,
title: "A document".into(),
options: vec![("media".into(), "iso_a4_210x297mm".into())],
driver_settings: Some(craft_print::DriverSettings::new("Office λ", dev_mode)),
};
let bytes = serde_json::to_vec(&Request::from_job(&expected)).unwrap();
let received: Request = serde_json::from_slice(&bytes).unwrap();
assert_eq!(received.job(), expected);
assert_eq!(received.job(), Ok(expected));
}
#[test]
/// Damaged driver settings on the wire are an error, not a job printed with other settings.
fn damaged_driver_settings_are_refused() {
for hex in ["", "0", "zz", "00"] {
let mut request = Request::from_job(&job());
request.driver_settings = Some(("Office".into(), hex.into()));
assert!(request.job().is_err(), "{hex:?}");
}
}
#[test]
+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 }
+259 -5
View File
@@ -11,6 +11,8 @@
use serde::{Deserialize, Serialize};
use crate::props::Rgb;
/// Most series in a chart.
pub const MAX_SERIES: usize = 255;
/// Most categories (points per series) in a chart.
@@ -123,17 +125,119 @@ impl LegendPos {
}
}
/// One series: its name and a value per category (`None` = no value, a gap).
/// One series: its name, a value per category (`None` = no value, a gap) and how it is drawn:
/// fill, line, markers, smooth lines, its own data labels and number format. Unset fields follow
/// the chart's defaults.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChartSeries {
pub name: String,
#[serde(default)]
pub values: Vec<Option<f64>>,
/// Fill color; `None`: the chart's accent color for the series.
#[serde(default)]
pub fill: Option<Rgb>,
/// "None" fill: the series draws no fill.
#[serde(default)]
pub no_fill: bool,
/// Line width in points; `None`: the chart's default.
#[serde(default)]
pub line_w: Option<f32>,
/// "None" line: the series draws no line.
#[serde(default)]
pub no_line: bool,
/// Markers on points; `None`: unset (scatter charts draw them; a saved scatter series then has
/// no `c:marker`).
#[serde(default)]
pub markers: Option<bool>,
/// Draw the line as a smooth curve.
#[serde(default)]
pub smooth: bool,
/// Data labels for this series; `None`: follow the chart's setting.
#[serde(default)]
pub labels: Option<bool>,
/// Number format of this series's labels and values; `None`: linked to the source.
#[serde(default)]
pub fmt: Option<String>,
}
impl ChartSeries {
/// A new series of a `kind` chart: a scatter series is drawn as markers without a line.
pub fn new(kind: ChartType, name: String, values: Vec<Option<f64>>) -> ChartSeries {
ChartSeries { name, values, no_line: kind == ChartType::Scatter, ..Default::default() }
}
}
/// One chart axis: title, scale bounds, tick-label number format, gridlines, tick labels and the
/// axis line.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChartAxis {
/// `None`: no axis title; text cut to [`MAX_TEXT`] characters.
#[serde(default)]
pub title: Option<String>,
/// Fixed lower bound; `None`: automatic.
#[serde(default)]
pub min: Option<f64>,
/// Fixed upper bound; `None`: automatic.
#[serde(default)]
pub max: Option<f64>,
/// Tick-label number format (`#,##0`, `0.00`, `0%`…); `None`: linked to the source data.
#[serde(default)]
pub fmt: Option<String>,
/// Major gridlines drawn along this axis.
#[serde(default)]
pub gridlines: bool,
/// Tick labels; `false` hides them.
#[serde(default = "the_true")]
pub labels: bool,
/// The axis line.
#[serde(default = "the_true")]
pub line: bool,
}
fn the_true() -> bool {
true
}
impl Default for ChartAxis {
fn default() -> Self {
ChartAxis { title: None, min: None, max: None, fmt: None, gridlines: false, labels: true, line: true }
}
}
/// The value axis as a new chart has it: gridlines, labels, no axis line.
pub fn value_axis() -> ChartAxis {
ChartAxis { gridlines: true, line: false, ..ChartAxis::default() }
}
impl ChartAxis {
/// Make the axis safe to store: texts cleaned, bounds finite, `min` under `max` (an
/// impossible pair drops both), the format code cut to [`MAX_FMT`] characters.
pub fn sanitize(&mut self) {
self.title = self.title.as_deref().map(clean_text).filter(|t| !t.is_empty());
self.min = self.min.filter(|v| v.is_finite());
self.max = self.max.filter(|v| v.is_finite());
if matches!((self.min, self.max), (Some(lo), Some(hi)) if lo >= hi) {
self.min = None;
self.max = None;
}
self.fmt = self.fmt.as_deref().map(clean_fmt).filter(|f| !f.is_empty());
}
}
/// Longest axis number format kept, characters.
pub const MAX_FMT: usize = 128;
/// A number format code: control characters as spaces, ends trimmed, at most [`MAX_FMT`]
/// characters.
pub fn clean_fmt(s: &str) -> String {
let s: String = s.chars().take(MAX_FMT * 4).map(|c| if c.is_control() { ' ' } else { c }).collect();
s.trim().chars().take(MAX_FMT).collect::<String>().trim_end().to_string()
}
/// A chart WordCraft can edit.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ChartSpec {
#[serde(rename = "type")]
@@ -151,6 +255,78 @@ pub struct ChartSpec {
/// Show each point's value next to it.
#[serde(default)]
pub data_labels: bool,
/// The category axis (scatter charts: the x axis).
#[serde(default)]
pub cat_axis: ChartAxis,
/// The value axis (scatter charts: the y axis); gridlines on and no axis line in a new chart.
#[serde(default = "value_axis")]
pub val_axis: ChartAxis,
}
/// A [`ChartSpec`] as stored. A scatter series saved without `noLine` (by a version without
/// series formatting) is drawn as markers without a line, as that version drew it.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct StoredSpec {
#[serde(rename = "type")]
kind: ChartType,
#[serde(default)]
categories: Vec<String>,
#[serde(default)]
series: Vec<StoredSeries>,
#[serde(default)]
title: Option<String>,
#[serde(default)]
legend: Option<LegendPos>,
#[serde(default)]
data_labels: bool,
#[serde(default)]
cat_axis: ChartAxis,
#[serde(default = "value_axis")]
val_axis: ChartAxis,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct StoredSeries {
#[serde(default)]
no_line: Option<bool>,
#[serde(flatten)]
series: ChartSeries,
}
impl<'de> Deserialize<'de> for ChartSpec {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = StoredSpec::deserialize(deserializer)?;
let scatter = s.kind == ChartType::Scatter;
Ok(ChartSpec {
kind: s.kind,
categories: s.categories,
series: s.series.into_iter().map(|x| ChartSeries { no_line: x.no_line.unwrap_or(scatter), ..x.series }).collect(),
title: s.title,
legend: s.legend,
data_labels: s.data_labels,
cat_axis: s.cat_axis,
val_axis: s.val_axis,
})
}
}
impl Default for ChartSpec {
/// An empty chart whose value axis is the one a new chart (and a missing JSON field) has:
/// gridlines on, no axis line.
fn default() -> Self {
ChartSpec {
kind: ChartType::default(),
categories: Vec::new(),
series: Vec::new(),
title: None,
legend: None,
data_labels: false,
cat_axis: ChartAxis::default(),
val_axis: value_axis(),
}
}
}
impl ChartSpec {
@@ -168,10 +344,14 @@ impl ChartSpec {
ChartSpec {
kind,
categories: categories.into_iter().map(str::to_string).collect(),
series: series.into_iter().map(|(n, v)| ChartSeries { name: n.to_string(), values: v.into_iter().map(Some).collect() }).collect(),
series: series.into_iter().map(|(n, v)| ChartSeries::new(kind, n.to_string(), v.into_iter().map(Some).collect())).collect(),
title: None,
legend: Some(LegendPos::Bottom),
data_labels: false,
cat_axis: ChartAxis::default(),
// Pie and doughnut charts have no axes: their axes stay plain, so the chart reads back
// as the same model.
val_axis: if kind.is_round() { ChartAxis::default() } else { value_axis() },
}
}
@@ -203,8 +383,12 @@ impl ChartSpec {
*v = v.filter(|x| x.is_finite());
}
s.values.resize(n, None);
s.fmt = s.fmt.as_deref().map(clean_fmt).filter(|f| !f.is_empty());
s.line_w = s.line_w.filter(|w| w.is_finite()).map(|w| w.clamp(0.0, 20.0));
}
self.title = self.title.as_deref().map(clean_text).filter(|t| !t.is_empty());
self.cat_axis.sanitize();
self.val_axis.sanitize();
}
/// The chart after `sanitize`.
@@ -229,7 +413,11 @@ mod tests {
let mut c = ChartSpec {
categories: vec!["a".into(); MAX_POINTS + 10],
series: (0..MAX_SERIES + 5)
.map(|i| ChartSeries { name: format!("{}\n{}", "x".repeat(400), i), values: vec![Some(f64::NAN), Some(f64::INFINITY), Some(1.0)] })
.map(|i| ChartSeries {
name: format!("{}\n{}", "x".repeat(400), i),
values: vec![Some(f64::NAN), Some(f64::INFINITY), Some(1.0)],
..Default::default()
})
.collect(),
title: Some(" ".into()),
..Default::default()
@@ -250,7 +438,7 @@ mod tests {
fn short_categories_are_padded_to_the_longest_series() {
let c = ChartSpec {
categories: vec!["a".into()],
series: vec![ChartSeries { name: "s".into(), values: vec![Some(1.0), Some(2.0)] }],
series: vec![ChartSeries { name: "s".into(), values: vec![Some(1.0), Some(2.0)], ..Default::default() }],
..Default::default()
}
.sanitized();
@@ -258,4 +446,70 @@ mod tests {
assert_eq!(ChartType::from_id("BARSTACKED"), Some(ChartType::BarStacked));
assert_eq!(LegendPos::from_id("top"), Some(LegendPos::Top));
}
/// A scatter series stored without `noLine` reads as markers without a line; a stored value
/// and the other chart types keep theirs.
#[test]
fn stored_scatter_series_without_a_line_setting_draw_no_line() {
let read = |json: &str| -> Vec<bool> { serde_json::from_str::<ChartSpec>(json).unwrap().series.iter().map(|s| s.no_line).collect() };
let series = r#"[{"name":"a","values":[1,null,2.5]},{"name":"b","values":[3],"noLine":false}]"#;
assert_eq!(read(&format!(r#"{{"type":"scatter","series":{series}}}"#)), [true, false]);
assert_eq!(read(&format!(r#"{{"type":"line","series":{series}}}"#)), [false, false]);
let c: ChartSpec = serde_json::from_str(&format!(r#"{{"type":"scatter","series":{series}}}"#)).unwrap();
assert_eq!(c.series[0].values, [Some(1.0), None, Some(2.5)]);
// What this version writes reads back as itself.
for kind in ChartType::ALL {
let spec = ChartSpec::sample(kind);
assert_eq!(serde_json::from_str::<ChartSpec>(&serde_json::to_string(&spec).unwrap()).unwrap(), spec, "{kind:?}");
}
}
#[test]
fn default_and_missing_json_axis_fields_agree() {
let from_json: ChartSpec = serde_json::from_str(r#"{"type":"column"}"#).unwrap();
let d = ChartSpec::default();
assert_eq!((d.cat_axis.clone(), d.val_axis.clone()), (from_json.cat_axis, from_json.val_axis));
assert!(d.val_axis.gridlines && !d.val_axis.line);
}
#[test]
fn axis_bounds_and_formats_are_cleaned() {
let mut a = ChartAxis {
title: Some(" Sales\n".into()),
min: Some(f64::NAN),
max: Some(50.0),
fmt: Some("x".repeat(MAX_FMT + 40)),
gridlines: true,
labels: false,
line: true,
};
a.sanitize();
assert_eq!(a.title.as_deref(), Some("Sales"));
assert_eq!(a.min, None);
assert_eq!(a.max, Some(50.0));
assert_eq!(a.fmt.as_deref().map(|f| f.chars().count()), Some(MAX_FMT));
// min not under max drops the pair; a format of only spaces is no format.
let mut b = ChartAxis { min: Some(50.0), max: Some(50.0), fmt: Some(" ".into()), ..Default::default() };
b.sanitize();
assert_eq!((b.min, b.max), (None, None));
assert_eq!(b.fmt, None);
let mut spec = ChartSpec {
series: vec![ChartSeries { name: "s".into(), values: vec![Some(1.0)], ..Default::default() }],
categories: vec!["a".into()],
..Default::default()
};
spec.series[0].line_w = Some(f32::NAN);
spec.series[0].fmt = Some("0.00".into());
spec.sanitize();
assert_eq!(spec.series[0].line_w, None);
assert_eq!(spec.series[0].fmt.as_deref(), Some("0.00"));
let mut spec = ChartSpec {
series: vec![ChartSeries { name: "s".into(), values: vec![], line_w: Some(100.0), ..Default::default() }],
..Default::default()
};
spec.sanitize();
assert_eq!(spec.series[0].line_w, Some(20.0), "a wild width clamps to 20 pt");
}
}
+9 -3
View File
@@ -270,8 +270,11 @@ impl ShapeExtra {
/// The largest rounded-rectangle corner: half the shorter side, which makes its ends round.
pub const MAX_CORNER: u32 = 50_000;
/// A rounded rectangle's corner radius without its own: 16% of the shorter side.
pub const DEFAULT_CORNER: f32 = 0.16;
/// A rounded rectangle's corner radius without its own: a sixth of the shorter side, the preset's
/// default `adj` of 16667 (ECMA-376 §20.1.10.56). Measured in Word for Windows: a 300 × 100 pt
/// rounded rectangle without `adj` is drawn with corners of 16.67 pt, a 300 × 40 pt one of 6.67
/// pt and a 60 × 150 pt one of 10 pt.
pub const DEFAULT_CORNER: f32 = 16_667.0 / 100_000.0;
/// The corner radius, in points, of a `w` × `h` rounded rectangle with `corner` (see
/// [`ShapeExtra::corner`]).
@@ -521,7 +524,10 @@ mod tests {
#[test]
fn corner_radius_follows_the_shorter_side() {
let near = |a: f32, b: f32| (a - b).abs() < 1e-4;
assert!(near(corner_radius(None, 200.0, 100.0), 16.0));
assert!(near(corner_radius(None, 200.0, 100.0), 16.667));
assert!(near(corner_radius(None, 300.0, 40.0), 6.6668));
assert!(near(corner_radius(None, 60.0, 150.0), 10.0002));
assert!(near(corner_radius(None, 200.0, 100.0), corner_radius(Some(16_667), 200.0, 100.0)), "the preset's default");
assert!(near(corner_radius(Some(0), 200.0, 100.0), 0.0));
assert!(near(corner_radius(Some(3_565), 100.0, 200.0), 3.565));
assert!(near(corner_radius(Some(MAX_CORNER), 200.0, 100.0), 50.0));
+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
@@ -127,6 +127,10 @@ pub enum GraphicItem {
align: TextAlign,
#[serde(default)]
font: Option<String>,
/// Degrees clockwise the text turns about `rect`'s center as it is drawn (a chart's vertical
/// axis title is turned -90°).
#[serde(default)]
angle: f32,
},
/// A picture (`Document::media` key) stretched into `rect`.
Image { rect: [f32; 4], media: String },
+3 -2
View File
@@ -610,11 +610,12 @@ fn lin_node(n: &MNode, s: &mut String, depth: usize) {
}
MNode::Nary { chr, sub_hide, sup_hide, sub, sup, e, .. } => {
s.push(*chr);
if !sub_hide && !sub.is_empty() {
// A shown limit is written even when empty (`_()`): left out, it reads back hidden (#6076).
if !sub_hide {
s.push('_');
lin_script(sub, s, depth);
}
if !sup_hide && !sup.is_empty() {
if !sup_hide {
s.push('^');
lin_script(sup, s, depth);
}
+3
View File
@@ -80,6 +80,8 @@ pub struct Settings {
pub default_tab: f32,
pub even_odd_headers: bool,
pub mirror_margins: bool,
/// The gutter is added to the top margin instead of the left one (`w:gutterAtTop`).
pub gutter_at_top: bool,
pub auto_hyphenation: bool,
/// Hyphenation zone (`w:hyphenationZone`), points: a line is hyphenated only when breaking
/// it at a word boundary would leave more than this empty at its end.
@@ -302,6 +304,7 @@ impl Default for Settings {
default_tab: 36.0,
even_odd_headers: false,
mirror_margins: false,
gutter_at_top: false,
auto_hyphenation: false,
hyphenation_zone: DEFAULT_HYPHENATION_ZONE,
hyphenate_caps: true,
+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);
+2 -2
View File
@@ -265,10 +265,10 @@ mod tests {
}
assert!(shape_outline(ShapeKind::Rectangle, 10.0, 10.0, None).is_none());
// The end of the bottom-right corner: in the frame's corner when the corner is square,
// 16% of the height (8 of 100 wide) in from it by default.
// a sixth of the height (8.33 of 100 wide) in from it by default.
let corner_pt = |corner| shape_outline(ShapeKind::RoundedRectangle, 100.0, 50.0, corner).unwrap()[6];
assert!(corner_pt(Some(0)).0 > 0.999 && corner_pt(Some(0)).1 > 0.999, "{:?}", corner_pt(Some(0)));
assert!((corner_pt(None).0 - 0.92).abs() < 0.001, "{:?}", corner_pt(None));
assert!((corner_pt(None).0 - 0.916_67).abs() < 0.0001, "{:?}", corner_pt(None));
}
#[test]
+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);
}
}
}
}
+24
View File
@@ -0,0 +1,24 @@
//! An n-ary operator's empty but shown limit survives linear text (#6076): half-typed `∑_` has an
//! empty lower limit that is shown, and `to_linear` writes it as `_()`. Left out, it reads back as a
//! hidden limit.
use wordcraft_doc::math::{MNode, MRun, parse_linear, to_linear};
#[test]
fn an_empty_shown_limit_prints_back_the_same() {
for s in ["∑_", "∑^", "∫_0^▒x", "∑_()^n▒i"] {
let n = parse_linear(s);
assert_eq!(parse_linear(&to_linear(&n)), n, "{s} → {}", to_linear(&n));
}
}
#[test]
fn hidden_and_filled_limits_print_as_before() {
let nary = |sub_hide, sup_hide, sub: &str, sup: &str| {
let arg = |t: &str| if t.is_empty() { Vec::new() } else { vec![MNode::Run(MRun::new(t))] };
vec![MNode::Nary { chr: '∑', lim_loc: None, grow: false, sub_hide, sup_hide, sub: arg(sub), sup: arg(sup), e: arg("x") }]
};
assert_eq!(to_linear(&nary(true, true, "", "")), "∑▒x");
assert_eq!(to_linear(&nary(false, false, "i", "n")), "∑_i^n▒x");
assert_eq!(to_linear(&nary(false, true, "", "")), "∑_()▒x");
}
+113
View File
@@ -94,6 +94,15 @@ fn object_value_from_shared(
};
Ok(Target::Graphic { w, h, alt, float: convert(float)?, graphic })
}
// This model has no picture frame shape: DrawingML's `frame` preset is read as a
// rectangle, without the frame's border width.
mut shape @ Source::Shape { kind: craft_wordmodel::para::ShapeKind::Frame, .. } => {
if let Source::Shape { kind, extra, .. } = &mut shape {
*kind = craft_wordmodel::para::ShapeKind::Rectangle;
extra.corner = None;
}
convert(shape)
}
Source::Group { w, h, float, ch_w, ch_h, children, canvas } => Ok(Target::Group {
w,
h,
@@ -442,6 +451,110 @@ pub fn to_shared_model(source: &wordcraft_doc::Document) -> Result<craft_wordmod
mod tests {
use super::*;
fn doc_with(objects: Vec<craft_wordmodel::para::InlineObject>) -> craft_wordmodel::Document {
let mut source = craft_wordmodel::Document::new();
let mut para = craft_wordmodel::para::Paragraph::new();
para.objects = objects;
source.body = vec![Arc::new(craft_wordmodel::Block::Para(para))];
source
}
fn objects(doc: &wordcraft_doc::Document) -> Vec<wordcraft_doc::para::InlineObject> {
doc.body.first().and_then(|b| b.as_para()).map(|p| p.objects.clone()).unwrap_or_default()
}
fn chart_object(spec: craft_wordmodel::chart::ChartSpec) -> craft_wordmodel::para::InlineObject {
let graphic = craft_wordmodel::graphic::Graphic { chart: Some(Arc::new(spec)), ..Default::default() };
craft_wordmodel::para::InlineObject::Graphic { w: 100.0, h: 60.0, alt: String::new(), float: Default::default(), graphic: Arc::new(graphic) }
}
fn charts(doc: &wordcraft_doc::Document) -> Vec<Option<wordcraft_doc::chart::ChartSpec>> {
objects(doc)
.iter()
.filter_map(|o| match o {
wordcraft_doc::para::InlineObject::Graphic { graphic, .. } => Some(graphic.chart.as_deref().cloned()),
_ => None,
})
.collect()
}
/// A picture frame shape opens as a rectangle instead of failing the whole document.
#[test]
fn a_frame_shape_reads_as_a_rectangle() {
let shape = |kind| craft_wordmodel::para::InlineObject::Shape {
kind,
w: 40.0,
h: 30.0,
fill: None,
stroke: None,
stroke_width: 1.0,
float: Default::default(),
story: None,
freeform: None,
effects: Default::default(),
extra: craft_wordmodel::connector::ShapeExtra { corner: Some(2604), ..Default::default() },
};
let doc = from_shared_model(doc_with(vec![shape(craft_wordmodel::para::ShapeKind::Frame)])).expect("from_shared_model");
let found = objects(&doc);
let [wordcraft_doc::para::InlineObject::Shape { kind, w, extra, .. }] = found.as_slice() else { panic!("{found:?}") };
assert_eq!((*kind, *w, extra.corner), (wordcraft_doc::para::ShapeKind::Rectangle, 40.0, None));
}
/// A chart's series formatting and axis settings survive the trip through this model, so
/// saving writes the chart part the reader read.
#[test]
fn chart_formatting_survives_the_bridge() {
use craft_wordmodel::chart::{ChartSpec, ChartType};
let mut formatted = ChartSpec::sample(ChartType::Line);
formatted.series[0].fill = Some(craft_wordmodel::props::Rgb(0x12, 0x34, 0x56));
formatted.series[0].line_w = Some(2.5);
formatted.series[1].smooth = true;
formatted.series[1].markers = Some(true);
formatted.series[2].labels = Some(true);
formatted.series[2].fmt = Some("0.0".into());
formatted.val_axis.title = Some("Sales".into());
formatted.val_axis.min = Some(0.0);
formatted.cat_axis.gridlines = true;
let mut scatter_lines = ChartSpec::sample(ChartType::Scatter);
scatter_lines.series[0].no_line = false;
let all = [formatted, ChartSpec::sample(ChartType::Scatter), ChartSpec::sample(ChartType::Pie), scatter_lines];
let doc = from_shared_model(doc_with(all.iter().cloned().map(chart_object).collect())).expect("from_shared_model");
assert!(charts(&doc).iter().all(Option::is_some));
let back = to_shared_model(&doc).expect("to_shared_model");
let back: Vec<ChartSpec> = back
.body
.first()
.and_then(|b| b.as_para())
.map(|p| {
p.objects
.iter()
.filter_map(|o| match o {
craft_wordmodel::para::InlineObject::Graphic { graphic, .. } => graphic.chart.as_deref().cloned(),
_ => None,
})
.collect()
})
.unwrap_or_default();
assert_eq!(back, all);
}
/// A scatter chart this model makes is written as markers without lines, the same part the
/// shared model writes for its own new scatter chart.
#[test]
fn a_new_scatter_chart_is_written_without_lines() {
let local = wordcraft_doc::chart::ChartSpec::sample(wordcraft_doc::chart::ChartType::Scatter);
assert!(!local.series.is_empty() && local.series.iter().all(|s| s.no_line));
assert_eq!(
crate::chart_xml(&local),
craft_docx::chart_xml(&craft_wordmodel::chart::ChartSpec::sample(craft_wordmodel::chart::ChartType::Scatter)).into_bytes()
);
for kind in wordcraft_doc::chart::ChartType::ALL {
let local = wordcraft_doc::chart::ChartSpec::sample(kind);
let shared: craft_wordmodel::chart::ChartSpec = convert_ref(&local).expect("convert");
assert_eq!(shared, craft_wordmodel::chart::ChartSpec::sample(shared.kind), "{kind:?}");
}
}
#[test]
fn bridges_nested_stories_and_moves_media_both_directions() {
let mut source = craft_wordmodel::Document::new();
@@ -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);
@@ -217,6 +219,8 @@ const PINNED_PARTS: &[&str] = &[
"word/styles.xml",
"word/settings.xml",
"word/theme/theme1.xml",
// The fonts the document uses, with their families: it brings no font table of its own.
"word/fontTable.xml",
"docProps/core.xml",
"docProps/app.xml",
];
+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">"#);
+51 -7
View File
@@ -989,7 +989,8 @@ fn toc_field_skips_nested_stories() {
let mut zip = craft_zip::Archive::open(&bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap();
let xml = String::from_utf8(zip.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap();
let boxes: Vec<(usize, usize)> = xml.match_indices("<w:txbxContent>").map(|(a, _)| (a, a + xml[a..].find("</w:txbxContent>").unwrap())).collect();
assert_eq!(boxes.len(), 2, "{xml}");
// Each text box is written twice: in the drawing and in its VML fallback.
assert_eq!(boxes.len(), 4, "{xml}");
let in_box = |i: usize| boxes.iter().any(|(a, b)| (*a..*b).contains(&i));
for kind in ["begin", "separate", "end"] {
let at: Vec<usize> = xml.match_indices(&format!(r#"fldCharType="{kind}""#)).map(|(i, _)| i).collect();
@@ -997,7 +998,7 @@ fn toc_field_skips_nested_stories() {
assert!(!in_box(at[0]), "field {kind} written inside a text box: {xml}");
}
let (begin, end) = (xml.find(r#"fldCharType="begin""#).unwrap(), xml.find(r#"fldCharType="end""#).unwrap());
assert!(begin > boxes[0].1 && end > boxes[1].1 && end < xml.find("</w:sdt>").unwrap(), "{xml}");
assert!(begin > boxes[1].1 && end > boxes[3].1 && end < xml.find("</w:sdt>").unwrap(), "{xml}");
}
#[test]
@@ -1567,6 +1568,29 @@ fn attached_template_and_link_styles_round_trip() {
assert!(!plain.settings.link_styles);
}
/// Gutter position Top (`w:gutterAtTop`) is kept when a document is opened and saved again, and
/// is not written when it is off.
#[test]
fn gutter_at_top_survives_open_and_save() {
let settings_xml = |bytes: &[u8]| {
let mut zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap();
String::from_utf8(zip.read("word/settings.xml", 16 << 20).unwrap().unwrap()).unwrap()
};
let mut d = Document::new();
d.settings.gutter_at_top = true;
let first = wordcraft_docx::write(&d).expect("write");
assert_eq!(settings_xml(&first).matches("<w:gutterAtTop/>").count(), 1);
let opened = wordcraft_docx::read(&first).expect("read");
assert!(opened.settings.gutter_at_top);
let saved = wordcraft_docx::write(&opened).expect("write again");
assert_eq!(settings_xml(&saved).matches("<w:gutterAtTop/>").count(), 1);
assert!(wordcraft_docx::read(&saved).expect("read again").settings.gutter_at_top);
let plain = wordcraft_docx::write(&Document::new()).expect("write plain");
assert!(!settings_xml(&plain).contains("gutterAtTop"));
}
/// #995: the zoom a document was saved at (`w:zoom`), One Page (`fullPage`) included, is read
/// and written back.
#[test]
@@ -1922,8 +1946,21 @@ fn flat_opc_round_trip_equals_docx() {
assert!(text.starts_with("<?xml") && text.contains("<?mso-application progid=\"Word.Document\"?>"));
assert!(text.contains("pkg:name=\"/word/document.xml\"") && text.contains("<pkg:binaryData>"));
assert_eq!(wordcraft_docx::sniff_xml(&xml), wordcraft_docx::XmlKind::FlatOpc);
let back = wordcraft_docx::read_xml(&xml).expect("read flat");
assert_eq!(back, rt(&d));
let mut back = wordcraft_docx::read_xml(&xml).expect("read flat");
// `pkg:xmlData` inlines a part without its XML declaration, so a kept XML part (the font
// table) comes back without one; the markup is the same.
let mut want = rt(&d);
for doc in [&mut back, &mut want] {
for v in doc.passthrough.values_mut() {
if let Some(rest) =
v.strip_prefix(b"<?xml").and_then(|r| r.iter().position(|&b| b == b'>').map(|e| r[e + 1..].trim_ascii_start().to_vec()))
{
*v = Arc::new(rest);
}
}
}
assert!(back.passthrough.contains_key("word/fontTable.xml"));
assert_eq!(back, want);
assert_eq!(back.media.len(), 1);
}
@@ -2398,6 +2435,10 @@ fn bibliography_sources_round_trip() {
assert_bibliography_parts(&bytes);
assert_bibliography_sources_round_trip(&bytes);
// Saved again after reading, the store keeps one relationship, from the main part.
let again = wordcraft_docx::write(&wordcraft_docx::read(&bytes).expect("read")).expect("write again");
assert_bibliography_parts(&again);
assert_eq!(wordcraft_docx::read(&again).expect("read again").sources.len(), d.sources.len());
}
/// #4848: a corporate author saves as Word's `b:Corporate`, in place of a name list (a role holds
@@ -2438,9 +2479,12 @@ fn assert_bibliography_parts(bytes: &[u8]) {
assert!(zip.contains("customXml/itemProps1.xml"));
assert!(zip.contains("customXml/_rels/item1.xml.rels"));
let rels_xml = String::from_utf8(zip.read("_rels/.rels", 16 << 20).unwrap().unwrap()).unwrap();
assert!(rels_xml.contains("relationships/customXml"), "{rels_xml}");
assert!(rels_xml.contains("Target=\"customXml/item1.xml\""), "{rels_xml}");
// The sources' data store is related from the main part's relationships only.
let rels_xml = String::from_utf8(zip.read("word/_rels/document.xml.rels", 16 << 20).unwrap().unwrap()).unwrap();
assert_eq!(rels_xml.matches("relationships/customXml\"").count(), 1, "{rels_xml}");
assert!(rels_xml.contains("Target=\"../customXml/item1.xml\""), "{rels_xml}");
let root_xml = String::from_utf8(zip.read("_rels/.rels", 16 << 20).unwrap().unwrap()).unwrap();
assert!(!root_xml.contains("relationships/customXml\""), "{root_xml}");
let ct_xml = String::from_utf8(zip.read("[Content_Types].xml", 16 << 20).unwrap().unwrap()).unwrap();
assert!(ct_xml.contains("PartName=\"/customXml/item1.xml\""), "{ct_xml}");
+3 -1
View File
@@ -261,5 +261,7 @@ fn story_shared_by_many_shapes_is_written_once() {
}
let xml = document_xml(&wordcraft_docx::write(&d).unwrap());
assert!(xml.len() < 2_000_000, "document.xml is {} bytes", xml.len());
assert_eq!(xml.matches(text.as_str()).count(), 1);
// The story is written for the first shape only: in its drawing and in that drawing's VML
// fallback. The other shapes do not repeat it.
assert_eq!(xml.matches(text.as_str()).count(), 2);
}
+1
View File
@@ -59,6 +59,7 @@ rustix = { version = "1.1", default-features = false, features = ["std", "fs"] }
[dev-dependencies]
wordcraft-doc = { workspace = true, features = ["test-support"] }
wordcraft-formats = { workspace = true, features = ["test-support"] }
encoding_rs = { workspace = true }
craft-zip = { workspace = true }
cfb = { workspace = true }
+32
View File
@@ -0,0 +1,32 @@
//! Typing timing: opens a document, types at the middle of it, and prints the layout time of
//! each keystroke (the pagination every edit pays for).
//!
//! `cargo run --release -p wordcraft-engine --example typing_bench -- doc.docx [keys]`
use serde_json::json;
use wordcraft_engine::Session;
fn main() -> Result<(), String> {
let args: Vec<String> = std::env::args().skip(1).collect();
let path = args.first().ok_or("usage: typing_bench doc.docx [keys]")?;
let keys: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(40);
let mut s = Session::new(wordcraft_engine::doc::Document::new());
s.run("file.open", &json!({"path": path})).map_err(|e| e.to_string())?;
let first = s.layout().ms;
let mid = s.doc.body.len() / 2;
let block = (mid..s.doc.body.len()).chain((0..mid).rev()).find(|&i| s.doc.body.get(i).and_then(|b| b.as_para()).is_some()).unwrap_or(mid);
s.run("caret.set", &json!({"pos": {"story": "body", "path": [block], "off": 0}})).map_err(|e| e.to_string())?;
let mut ms = Vec::with_capacity(keys);
for _ in 0..keys {
s.run("text.insert", &json!({"text": "a"})).map_err(|e| e.to_string())?;
ms.push(s.layout().ms);
}
ms.sort_by(f64::total_cmp);
let med = ms.get(ms.len() / 2).copied().unwrap_or(0.0);
println!(
"blocks {}, first layout {first:.1} ms, per key: median {med:.1} ms, min {:.1} ms",
s.doc.body.len(),
ms.first().copied().unwrap_or(0.0)
);
Ok(())
}
+42 -6
View File
@@ -9,7 +9,7 @@
use std::sync::Arc;
use serde_json::{Value, json};
use wordcraft_doc::chart::{ChartSeries, ChartSpec, ChartType, LegendPos, MAX_POINTS, MAX_SERIES};
use wordcraft_doc::chart::{ChartAxis, ChartSeries, ChartSpec, ChartType, LegendPos, MAX_POINTS, MAX_SERIES, value_axis};
use wordcraft_doc::graphic::{Graphic, GraphicKind};
use wordcraft_doc::para::{Float, InlineObject};
use wordcraft_doc::props::Rgb;
@@ -33,6 +33,23 @@ pub fn specs() -> Vec<CommandSpec> {
CommandSpec::new("chart.type", "Change Chart Type", "Chart Design › Type", |s, v| {
let kind = chart_type(v)?.ok_or_else(|| CmdError::Params(format!("`type` is required: {TYPES}")))?;
edit(s, |c| {
// A scatter series is drawn as markers without a line; the other types draw it.
if (c.kind == ChartType::Scatter) != (kind == ChartType::Scatter) {
for series in &mut c.series {
series.no_line = kind == ChartType::Scatter;
if series.no_line && series.markers == Some(false) {
series.markers = None;
}
}
}
// Pie and doughnut charts have a plain value axis and the other types a new
// chart's value axis; an axis left as the old type had it takes the new type's.
if c.kind.is_round() != kind.is_round() {
let (from, to) = if kind.is_round() { (value_axis(), ChartAxis::default()) } else { (ChartAxis::default(), value_axis()) };
if c.val_axis == from {
c.val_axis = to;
}
}
c.kind = kind;
Ok(())
})
@@ -52,7 +69,15 @@ pub fn specs() -> Vec<CommandSpec> {
c.categories = cats.clone();
}
if let Some(series) = &series {
c.series = series.clone();
// Each series keeps the formatting of the one it replaces.
c.series = series
.iter()
.enumerate()
.map(|(i, (name, values))| match c.series.get(i) {
Some(old) => ChartSeries { name: name.clone(), values: values.clone(), ..old.clone() },
None => ChartSeries::new(c.kind, name.clone(), values.clone()),
})
.collect();
}
Ok(())
})
@@ -99,6 +124,10 @@ pub fn specs() -> Vec<CommandSpec> {
let on = p::bool(v, "on").or_else(|| p::bool(v, "value"));
edit(s, move |c| {
c.data_labels = on.unwrap_or(!c.data_labels);
// The chart's setting replaces each series' own.
for series in &mut c.series {
series.labels = None;
}
Ok(())
})
})
@@ -152,7 +181,14 @@ fn insert(s: &mut Session, v: &Value) -> CmdResult {
let mut spec = ChartSpec::sample(kind);
if let Some(d) = v.get("data") {
spec.categories = d.get("categories").map(categories).transpose()?.unwrap_or_default();
spec.series = d.get("series").map(series_list).transpose()?.unwrap_or_default();
spec.series = d
.get("series")
.map(series_list)
.transpose()?
.unwrap_or_default()
.into_iter()
.map(|(name, values)| ChartSeries::new(kind, name, values))
.collect();
if spec.series.is_empty() {
return Err(CmdError::Params(format!("`data.series` needs at least one series: {DATA}")));
}
@@ -220,8 +256,8 @@ fn cell_text(v: &Value) -> String {
}
}
/// Series from JSON: `[{"name", "values": [number|null|numeric string]}]`.
fn series_list(v: &Value) -> Result<Vec<ChartSeries>, CmdError> {
/// Series names and values from JSON: `[{"name", "values": [number|null|numeric string]}]`.
fn series_list(v: &Value) -> Result<Vec<(String, Vec<Option<f64>>)>, CmdError> {
let a = v.as_array().ok_or_else(|| CmdError::Params(format!("`series`: {DATA}")))?;
a.iter()
.take(MAX_SERIES)
@@ -232,7 +268,7 @@ fn series_list(v: &Value) -> Result<Vec<ChartSeries>, CmdError> {
None | Some(Value::Null) => Vec::new(),
Some(_) => return Err(CmdError::Params("`values`: an array of numbers (null for none)".into())),
};
Ok(ChartSeries { name, values })
Ok((name, values))
})
.collect()
}
+1
View File
@@ -19,6 +19,7 @@ mod sources;
mod styles;
use cross_refs::cross_ref;
pub(crate) use cross_refs::leading_breaks;
pub use cross_refs::{caption_labels, ref_targets};
#[cfg(test)]
use generated::ENTRY_STYLE;
+39 -15
View File
@@ -1,5 +1,5 @@
use super::generated::page_of;
use super::{CharProps, CmdError, CmdResult, Document, InlineObject, Paragraph, Pos, Selection, Session, StoryRef, Value, delete_selection, json, p};
use crate::cmd::references::page_label;
pub(super) fn cross_ref(s: &mut Session, v: &Value) -> CmdResult {
let to = p::str(v, "to").unwrap_or("heading");
@@ -10,9 +10,10 @@ pub(super) fn cross_ref(s: &mut Session, v: &Value) -> CmdResult {
let find = |ts: Vec<(String, Pos)>| ts.into_iter().find(|(t, _)| t == target || t.starts_with(target));
let (text, pos) = find(targets).ok_or_else(|| CmdError::Params(format!("no {to} `{target}`")))?;
let show = p::str(v, "show").unwrap_or("text");
let page = page_of(s, &pos);
let result = match show {
"page" => page.to_string(),
// The page the target's text starts on (after its leading page breaks), as its footer
// shows it.
"page" => page_label(s, &Pos { off: leading_breaks(&s.doc, &pos), ..pos.clone() }),
"number" => text.split(':').next().unwrap_or(&text).trim().to_string(),
"aboveBelow" => {
if pos < s.sel.focus {
@@ -152,6 +153,23 @@ pub fn ref_targets(s: &Session, to: &str) -> Vec<(String, Pos)> {
targets
}
/// The offset just past the page breaks at the start of the paragraph at `pos`, skipping bookmark
/// marks among them (a `_Ref` bookmark may start before the breaks): its text starts there.
pub(crate) fn leading_breaks(doc: &Document, pos: &Pos) -> usize {
let Some(p) = doc.para(pos.story, &pos.path) else { return 0 };
let mut after = 0;
for (i, c) in p.text.char_indices() {
if c == '\u{0C}' {
after = i.saturating_add(c.len_utf8());
} else if c != wordcraft_doc::para::OBJ
|| !matches!(p.object_at(i), Some(InlineObject::BookmarkStart { .. } | InlineObject::BookmarkEnd { .. }))
{
break;
}
}
after
}
/// The hidden `_Ref` bookmark around the target paragraph at `pos` (its label and number only
/// when `number_only`), reusing one already there. Returns its name and `at` moved past the
/// bookmark marks inserted before it.
@@ -159,16 +177,22 @@ fn ref_bookmark(s: &mut Session, pos: &Pos, number_only: bool, mut at: Pos) -> R
const MARK: usize = wordcraft_doc::para::OBJ.len_utf8();
let para = s.doc.para(pos.story, &pos.path).ok_or_else(|| CmdError::Params("cross-reference target is gone".into()))?;
let end = if number_only { para.text.find(':').unwrap_or(para.len()) } else { para.len() };
if let Some(InlineObject::BookmarkStart { name }) = para.object_at(0)
&& name.starts_with("_Ref")
{
// Reuse it when it covers the same extent: up to the colon, or the whole paragraph.
let same = para.object_offsets().into_iter().any(|o| {
matches!(para.object_at(o), Some(InlineObject::BookmarkEnd { name: n }) if n == name)
&& para.text.get(o.saturating_add(MARK)..).is_some_and(|rest| if number_only { rest.starts_with(':') } else { rest.is_empty() })
});
if same {
return Ok((name.clone(), at));
// The bookmark starts after the leading page breaks, so `PAGEREF` reads the page the text is on.
let start = leading_breaks(&s.doc, pos).min(end);
// Reuse one that starts there or at the paragraph's start (before the breaks): the field's
// result still reads the page the text is on.
for from in [start, 0] {
if let Some(InlineObject::BookmarkStart { name }) = para.object_at(from)
&& name.starts_with("_Ref")
{
// Reuse it when it covers the same extent: up to the colon, or the whole paragraph.
let same = para.object_offsets().into_iter().any(|o| {
matches!(para.object_at(o), Some(InlineObject::BookmarkEnd { name: n }) if n == name)
&& para.text.get(o.saturating_add(MARK)..).is_some_and(|rest| if number_only { rest.starts_with(':') } else { rest.is_empty() })
});
if same {
return Ok((name.clone(), at));
}
}
}
let next =
@@ -176,11 +200,11 @@ fn ref_bookmark(s: &mut Session, pos: &Pos, number_only: bool, mut at: Pos) -> R
let name = format!("_Ref{next:09}");
let props = CharProps::default();
s.doc.insert_object(&Pos { off: end, ..pos.clone() }, InlineObject::BookmarkEnd { name: name.clone() }, &props)?;
s.doc.insert_object(&Pos { off: 0, ..pos.clone() }, InlineObject::BookmarkStart { name: name.clone() }, &props)?;
s.doc.insert_object(&Pos { off: start, ..pos.clone() }, InlineObject::BookmarkStart { name: name.clone() }, &props)?;
if at.story == pos.story && at.path == pos.path {
if at.off >= end {
at.off = at.off.saturating_add(2 * MARK);
} else if at.off > 0 {
} else if at.off > start {
at.off = at.off.saturating_add(MARK);
}
}
+14 -8
View File
@@ -6,7 +6,9 @@ use super::{
Block, CharProps, CmdError, CmdResult, Document, FieldCode, InlineObject, Paragraph, Path, Pos, Session, SortKey, StoryRef, TabAlign, TabLeader,
TabStop, Value, delete_selection, heading_letter, p, sel_result, sort_key,
};
use crate::cmd::references::{page_at, page_formats, page_text};
use std::sync::Arc;
use wordcraft_doc::section::NumFormat;
/// The TOC field for `references.tableOfFigures` (ECMA-376 §17.16.5.68): `\c "<label>"` lists the
/// captions that label's SEQ field numbers, `\a "<label>"` lists them without their label and
@@ -331,9 +333,11 @@ fn add_entry_style(s: &mut Session) {
}
}
pub(super) fn page_of(s: &mut Session, pos: &Pos) -> u32 {
/// The page holding `pos`: its index in the layout (to order and deduplicate by) and its number
/// in its section's format (`ii`), as the list writes it. Page "1" (index 0) when no page holds it.
fn page_of(s: &mut Session, formats: &[NumFormat], pos: &Pos) -> (usize, String) {
let l = s.layout();
l.caret(pos).and_then(|c| l.pages.get(c.page)).map(|p| p.number).unwrap_or(1)
page_at(&l, formats, pos).map_or_else(|| (0, "1".into()), |(i, n, fmt)| (i, page_text(n, fmt)))
}
/// Entry lines (text, hanging indent) for a generated list.
@@ -370,7 +374,7 @@ fn entries_for(s: &mut Session, kind: &str) -> Vec<(String, bool)> {
}
}
}
let mut with_pages: Vec<(String, u32)> = marks.into_iter().map(|(e, _)| (e, 0)).collect();
let mut with_pages: Vec<(String, (usize, String))> = marks.into_iter().map(|(e, _)| (e, (0, String::new()))).collect();
// Pages (layout once).
let positions: Vec<Pos> = {
let mut v = Vec::new();
@@ -393,8 +397,9 @@ fn entries_for(s: &mut Session, kind: &str) -> Vec<(String, bool)> {
}
v
};
let formats = page_formats(s);
for (i, pos) in positions.iter().enumerate() {
let pg = page_of(s, pos);
let pg = page_of(s, &formats, pos);
if let Some(x) = with_pages.get_mut(i) {
x.1 = pg;
}
@@ -403,14 +408,15 @@ fn entries_for(s: &mut Session, kind: &str) -> Vec<(String, bool)> {
// `\n` without page numbers; `\p` the separator before them (a tab by default).
let f = FieldCode::parse(kind);
let sep = f.arg('p').unwrap_or("\t");
return with_pages.into_iter().map(|(t, p)| (if f.has('n') { t } else { format!("{t}{sep}{p}") }, false)).collect();
return with_pages.into_iter().map(|(t, p)| (if f.has('n') { t } else { format!("{t}{sep}{}", p.1) }, false)).collect();
}
// Index / TOA: group by entry, sort, list pages.
let lang = crate::cmd::default_lang(s);
let mut grouped: std::collections::BTreeMap<(SortKey, String), Vec<u32>> = Default::default();
// Each page once, in page order (by its place in the layout, not its label: `ii` before `1`).
let mut grouped: std::collections::BTreeMap<(SortKey, String), Vec<(usize, String)>> = Default::default();
for (e, p) in with_pages {
let pages = grouped.entry((sort_key(&e, lang.as_deref()), e)).or_default();
if !pages.contains(&p) {
if !pages.iter().any(|(i, _)| *i == p.0) {
pages.push(p);
}
}
@@ -424,7 +430,7 @@ fn entries_for(s: &mut Session, kind: &str) -> Vec<(String, bool)> {
out.push((l, false));
}
}
let list = pages.iter().map(ToString::to_string).collect::<Vec<_>>().join(", ");
let list = pages.iter().map(|(_, label)| label.as_str()).collect::<Vec<_>>().join(", ");
out.push(if kind.starts_with("INDEX") { (format!("{e}, {list}"), true) } else { (format!("{e}\t{list}"), false) });
}
out
+66
View File
@@ -488,6 +488,32 @@ fn table_of_authorities_updates() {
assert!(after.matches("Doe v. Roe").count() > t.matches("Doe v. Roe").count(), "{after}");
}
/// #3856: the table of figures and the table of authorities write each page as its footer shows
/// it, in the section's number format; the authority cited on pages i and ii lists both.
#[test]
fn figures_and_authorities_follow_the_page_number_format() {
let mut s = Session::new(wordcraft_doc::Document::from_text("Smith v. Jones held it.\nSmith v. Jones again."));
s.run("select.text", &json!({"text": "Smith v. Jones again"})).unwrap();
s.run("caret.home", &json!({})).unwrap();
s.run("layout.break", &json!({"kind": "page"})).unwrap();
s.run("references.caption", &json!({"label": "Figure", "text": "The court"})).unwrap();
for occurrence in [1, 2] {
s.run("select.text", &json!({"text": "Smith v. Jones", "occurrence": occurrence})).unwrap();
s.run("references.markCitation", &json!({})).unwrap();
}
s.run("caret.docStart", &json!({})).unwrap();
s.run("layout.pageNumberFormat", &json!({"format": "lowerRoman", "start": 1})).unwrap();
s.run("caret.docEnd", &json!({})).unwrap();
s.run("text.newParagraph", &json!({})).unwrap();
s.run("references.tableOfFigures", &json!({})).unwrap();
s.run("caret.docEnd", &json!({})).unwrap();
s.run("text.newParagraph", &json!({})).unwrap();
s.run("references.tableOfAuthorities", &json!({})).unwrap();
let t = s.doc.plain_text(StoryRef::Body);
assert!(t.contains("Figure 1: The court\tii"), "{t}");
assert!(t.contains("Smith v. Jones\ti, ii"), "{t}");
}
/// Field instructions and results in the body, in order.
fn fields(s: &Session) -> Vec<(String, String)> {
let mut out = Vec::new();
@@ -845,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();
@@ -900,3 +927,42 @@ fn citation_refresh_source_lookup_work_scales_with_document_size() {
let p = big.doc.para(StoryRef::Body, &Path::top(0)).unwrap();
assert!(p.objects.iter().enumerate().all(|(i, o)| matches!(o, InlineObject::Field { result, .. } if result == &format!("[{}]", i + 1))));
}
/// A heading that starts with a page break and has a `_Ref` bookmark from before the break: the
/// page cross-reference reuses it and reads the heading's page, and still does after Update Field.
#[test]
fn a_ref_bookmark_before_a_page_break_reads_the_heading_page() {
let mut s = Session::new(wordcraft_doc::Document::from_text("Front\nPreface"));
s.run("select.text", &json!({"text": "Preface"})).unwrap();
s.run("styles.apply", &json!({"style": "Heading 1"})).unwrap();
s.run("caret.home", &json!({})).unwrap();
s.run("layout.break", &json!({"kind": "page"})).unwrap();
let path =
s.doc.para_paths(StoryRef::Body).into_iter().find(|p| s.doc.para(StoryRef::Body, p).is_some_and(|p| p.text.contains("Preface"))).unwrap();
let len = s.doc.para(StoryRef::Body, &path).unwrap().len();
let props = CharProps::default();
s.doc
.insert_object(
&Pos { story: StoryRef::Body, path: path.clone(), off: len },
InlineObject::BookmarkEnd { name: "_Ref000000003".into() },
&props,
)
.unwrap();
s.doc.insert_object(&Pos { story: StoryRef::Body, path, off: 0 }, InlineObject::BookmarkStart { name: "_Ref000000003".into() }, &props).unwrap();
s.run("caret.docEnd", &json!({})).unwrap();
s.run("insert.crossReference", &json!({"to": "heading", "target": "Preface", "show": "page"})).unwrap();
let pageref = |s: &Session| {
s.doc
.para_paths(StoryRef::Body)
.into_iter()
.flat_map(|p| s.doc.para(StoryRef::Body, &p).map(|p| p.objects.clone()).unwrap_or_default())
.find_map(|o| match o {
InlineObject::Field { instr, result, .. } if instr.starts_with("PAGEREF") => Some((instr, result)),
_ => None,
})
.unwrap()
};
assert_eq!(pageref(&s), ("PAGEREF _Ref000000003 \\h".to_string(), "2".to_string()));
crate::cmd::references::update_fields_in(&mut s, true).unwrap();
assert_eq!(pageref(&s).1, "2");
}
+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,
+8 -3
View File
@@ -198,9 +198,14 @@ fn special(query: &str, o: &Opts) -> (String, bool) {
if (o.ignore_punct && is_punct(c)) || (o.ignore_space && is_space(c)) {
continue;
}
// A keyboard apostrophe also finds text produced by smart quotes. Explicit character
// codes above, and literal/regex/wildcard searches, keep exact character matching.
atoms.push(if c == '\'' { "['‘’]".into() } else { lit(c) });
// A keyboard apostrophe or double quote also finds text produced by smart quotes.
// Explicit character codes above, and literal/regex/wildcard searches, keep exact
// character matching.
atoms.push(match c {
'\'' => "['‘’]".into(),
'"' => "[\"“”]".into(),
_ => lit(c),
});
}
let filler = match (o.ignore_punct, o.ignore_space) {
(true, true) => r"[\p{P} \t\x{A0}]*",
+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}))
}
+12 -6
View File
@@ -10,7 +10,7 @@ use wordcraft_doc::props::{CharProps, TabAlign, TabLeader, TabStop};
use wordcraft_doc::section::SectionStart;
use wordcraft_doc::{Block, Paragraph, Path, Pos, StoryRef};
use super::references::{FieldCode, field_arg, leader};
use super::references::{FieldCode, field_arg, leader, page_at, page_formats, page_text};
use super::sel_result;
use crate::{CmdError, CmdResult, CommandSpec, Session, p};
@@ -162,6 +162,9 @@ fn insert_index(s: &mut Session, v: &Value) -> CmdResult {
/// One page reference of an entry.
#[derive(Clone, Debug, PartialEq)]
struct PageRef {
/// The page's index in the layout (the first page of a range): pages are told apart by it,
/// not by their labels, which repeat across sections.
at: usize,
page: String,
bold: bool,
italic: bool,
@@ -190,8 +193,10 @@ fn collect(s: &mut Session, lang: Option<&str>) -> BTreeMap<Vec<SortKey>, Node>
}
}
}
let formats = page_formats(s);
let l = s.layout();
let page = |pos: &Pos| l.caret(pos).and_then(|c| l.pages.get(c.page)).map(|p| p.number).unwrap_or(1);
// A page's index in the layout and its number as its footer shows it (`ii`).
let page = |pos: &Pos| page_at(&l, &formats, pos).map_or_else(|| (0, "1".to_string()), |(i, n, fmt)| (i, page_text(n, fmt)));
let bookmark_end = |name: &str| {
s.doc.para_paths(StoryRef::Body).into_iter().find_map(|path| {
let p = s.doc.para(StoryRef::Body, &path)?;
@@ -222,11 +227,12 @@ fn collect(s: &mut Session, lang: Option<&str>) -> BTreeMap<Vec<SortKey>, Node>
}
let range = f.arg('r').and_then(|bm| {
let a = starts.iter().find(|(n, _)| n == bm).map(|(_, p)| page(p))?;
let b = bookmark_end(bm).map(|p| page(&p)).unwrap_or(a);
Some(if b > a { format!("{a}\u{2013}{b}") } else { a.to_string() })
let b = bookmark_end(bm).map(|p| page(&p)).unwrap_or_else(|| a.clone());
Some(if b.0 > a.0 { (a.0, format!("{}\u{2013}{}", a.1, b.1)) } else { a })
});
let r = PageRef { page: range.unwrap_or_else(|| page(&pos).to_string()), bold: f.has('b'), italic: f.has('i') };
match node.pages.iter_mut().find(|x| x.page == r.page) {
let (at, label) = range.unwrap_or_else(|| page(&pos));
let r = PageRef { at, page: label, bold: f.has('b'), italic: f.has('i') };
match node.pages.iter_mut().find(|x| x.at == r.at && x.page == r.page) {
Some(x) => {
x.bold |= r.bold;
x.italic |= r.italic;
+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()))?;
+2 -1
View File
@@ -617,7 +617,8 @@ fn recipients_from_an_xls_workbook() {
let book = wordcraft_formats::sheet::xls_fixture(&[(
"收件人",
vec![row(&["姓名", "城市", "数量"]), row(&["张三", "北京", "12"]), row(&["李四", "上海", "2"])],
)]);
)])
.unwrap();
let mut s = Session::new(Document::new());
let r = s.run("mailings.recipients", &json!({"path": "名单.xls", "data": b64(&book)})).unwrap();
assert_eq!(r, json!({"fields": ["姓名", "城市", "数量"], "records": 2, "sheet": "收件人"}));
+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
@@ -20,6 +20,7 @@ use fields::caption;
pub use fields::{
CHAPTER_SEPARATORS, REFERENCE_NOT_FOUND, output_fields, refresh_date_fields, seq_format, update_fields, update_fields_in, update_seq,
};
pub(crate) use fields::{page_at, page_formats, page_label, page_text};
pub(super) use notes::note_options;
use notes::{note, note_refs};
pub use properties::{bookmark_text, date_picture};
+42 -9
View File
@@ -317,6 +317,45 @@ pub fn update_fields(s: &mut Session) -> Result<(), CmdError> {
update_fields_in(s, false)
}
/// Each section's page number format (`w:pgNumType w:fmt`), by section index.
pub(crate) fn page_formats(s: &Session) -> Vec<NumFormat> {
s.doc.sections().into_iter().map(|(_, sec)| sec.page_num_format).collect()
}
/// The page holding `pos` in `l`: its index in the layout, its number, and its section's page
/// number format (`formats` from [`page_formats`]). `None` when no page holds it.
pub(crate) fn page_at(l: &wordcraft_layout::DocLayout, formats: &[NumFormat], pos: &Pos) -> Option<(usize, u32, NumFormat)> {
let c = l.caret(pos)?;
let page = l.pages.get(c.page)?;
Some((c.page, page.number, formats.get(page.section).copied().unwrap_or_default()))
}
/// A page number as the page's footer shows it (`ii` in a lowerRoman section), as the `PAGE`
/// and `PAGEREF` fields write it without a `\*` switch.
pub(crate) fn page_text(number: u32, fmt: NumFormat) -> String {
wordcraft_layout::fields::field_number("", fmt, number)
}
/// The page number of the page holding `pos`, in its section's number format: what generated
/// lists (TOC, index, tables of figures and authorities) and page cross-references write. Page
/// "1" when no page holds it.
pub(crate) fn page_label(s: &mut Session, pos: &Pos) -> String {
let formats = page_formats(s);
let l = s.layout();
page_at(&l, &formats, pos).map_or_else(|| "1".into(), |(_, n, fmt)| page_text(n, fmt))
}
/// Where `PAGEREF` reads bookmark `name` (starting at `pos`): a hidden `_Ref` bookmark from the
/// start of a heading that starts with page breaks reads the page its text is on, as when its
/// cross-reference was inserted.
fn bookmark_page_pos(doc: &wordcraft_doc::Document, name: &str, pos: Pos) -> Pos {
if pos.off == 0 && name.starts_with("_Ref") {
let off = crate::cmd::citations::leading_breaks(doc, &pos);
return Pos { off, ..pos };
}
pos
}
/// [`update_fields`], or with `all` the whole document: the body (with SEQ, citations and TOC) and
/// every part story, wherever the caret is (what printing needs).
pub fn update_fields_in(s: &mut Session, all: bool) -> Result<(), CmdError> {
@@ -326,18 +365,12 @@ pub fn update_fields_in(s: &mut Session, all: bool) -> Result<(), CmdError> {
update_seq(s)?;
}
let mut pages: std::collections::HashMap<String, (u32, NumFormat)> = Default::default();
let formats: Vec<NumFormat> = s.doc.sections().into_iter().map(|(_, sec)| sec.page_num_format).collect();
let formats = page_formats(s);
let marks = s.doc.bookmarks();
for (name, pos) in marks {
let pos = bookmark_page_pos(&s.doc, &name, pos);
let l = s.layout();
let page_info = l
.caret(&pos)
.and_then(|c| l.pages.get(c.page))
.map(|p| {
let format = formats.get(p.section).copied().unwrap_or_default();
(p.number, format)
})
.unwrap_or((1, NumFormat::Decimal));
let page_info = page_at(&l, &formats, &pos).map_or((1, NumFormat::Decimal), |(_, n, fmt)| (n, fmt));
pages.entry(name).or_insert(page_info);
}
let stories: Vec<StoryRef> =
+5 -4
View File
@@ -1,6 +1,6 @@
use super::{
Block, CharProps, CmdError, CmdResult, Counters, FieldCode, InlineObject, LevelSuffix, NumFormat, OBJ, Paragraph, Path, Pos, Selection, Session,
StoryRef, TabAlign, TabLeader, TabStop, Value, delete_selection, p, sel_result,
StoryRef, TabAlign, TabLeader, TabStop, Value, delete_selection, p, page_at, page_formats, page_text, sel_result,
};
/// A table-of-contents title is a heading, not document body: longer titles are rejected
@@ -575,6 +575,7 @@ fn regenerate_toc(s: &mut Session, leader: Option<TabLeader>, tabs: &[(String, V
};
let no_pages = f.has('n').then(|| f.arg('n').and_then(level_range).unwrap_or((1, 9)));
let sep = f.arg('p').map(str::to_string).unwrap_or_else(|| "\t".into());
let formats = page_formats(s);
for pass in 0..2 {
// Pass 1 lays out with the entries in place, so headings below the TOC sit `entries.len()`
// blocks further down and their pages account for the TOC's own length.
@@ -594,8 +595,8 @@ fn regenerate_toc(s: &mut Session, leader: Option<TabLeader>, tabs: &[(String, V
{
*top = top.saturating_add(u32::try_from(shift).unwrap_or(u32::MAX));
}
let page =
l.caret(&Pos { story: StoryRef::Body, path: target, off: e.lead }).and_then(|c| l.pages.get(c.page)).map(|pg| pg.number).unwrap_or(1);
let page = page_at(&l, &formats, &Pos { story: StoryRef::Body, path: target, off: e.lead })
.map_or_else(|| "1".into(), |(_, n, fmt)| page_text(n, fmt));
let show_page = no_pages.is_none_or(|(a, b)| !(a..=b).contains(&(e.level + 1)));
let link = links.get(k).cloned().flatten();
// With a bookmark to point at, the page number is a `PAGEREF` field, as Word writes
@@ -609,7 +610,7 @@ fn regenerate_toc(s: &mut Session, leader: Option<TabLeader>, tabs: &[(String, V
let props = CharProps { link, ..Default::default() };
let mut para = Paragraph::with_text(&text, props.clone()).styled(&format!("TOC{}", e.level + 1));
if let Some(instr) = pageref {
let field = InlineObject::Field { instr, result: page.to_string(), locked: false, dirty: false, code: false, form: String::new() };
let field = InlineObject::Field { instr, result: page.clone(), locked: false, dirty: false, code: false, form: String::new() };
para.insert_object(para.len(), field, &props)?;
}
para.mark = CharProps::default();
+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;
+23 -16
View File
@@ -2,10 +2,11 @@
//! column, left of a row and along a cell's left edge inside it select the column, row or cell,
//! and a drag from one selects every one up to the pointer; insert buttons left of a row border
//! and above a column border add a row or column there; the move handle just past the table's
//! top-left corner selects the table or, dragged, moves it (the table then floats where it's
//! dropped, the text wrapping around it, as in Word); a double-click on a column border fits the
//! column left of it to its contents. The app sends each click or finished drag here in page
//! points, so each is one command (one undo step when it changes the document).
//! top-left corner (top-right in a right-to-left table) selects the table or, dragged, moves it
//! (the table then floats where it's dropped, the text wrapping around it, as in Word); a
//! double-click on a column border fits the column left of it to its contents. The app sends
//! each click or finished drag here in page points, so each is one command (one undo step when
//! it changes the document).
use std::collections::BTreeMap;
@@ -34,7 +35,7 @@ const INSERT_REACH: f32 = 3.5;
const INSERT_OUT: f32 = 14.0;
/// An insert button's centre sits this far out from the table's edge (points).
const BUTTON_OUT: f32 = 8.0;
/// The move handle's side and its gap from the table's top-left corner (points).
/// The move handle's side and its gap from the table's top corner (points).
const HANDLE: f32 = 8.0;
const HANDLE_GAP: f32 = 2.0;
/// The farthest a drag moves a table (points; Word's 22 inches).
@@ -44,14 +45,14 @@ pub fn specs() -> Vec<CommandSpec> {
vec![
CommandSpec::new("table.selectAt", "Select Table Cells by Clicking", "Table Layout › Table › Select", select_at)
.params(
r#"{"page": n, "x": pt, "y": pt, "toX"?: pt, "toY"?: pt, "reach"?: pt, "extend"?: bool, "anchorRow"?: n} (a click in page points, 0-based page, on a selection area of a table in the caret's story: just above a column selects the column, left of a row the row, along a cell's left edge inside it the cell, the move handle past the top-left corner the table; with toX/toY (a drag) every column, row or cell from there to that point; the page margin beside a table (its leading side: right for a right-to-left table) is a row area too, and `extend` (Shift+click) on one takes the rows from the selected ones' anchor, or from row `anchorRow` (0-based) when given, so a drag keeps its press's anchor. `reach` (default 3) is the column borders' resize reach, which the cell areas keep clear of.)"#,
r#"{"page": n, "x": pt, "y": pt, "toX"?: pt, "toY"?: pt, "reach"?: pt, "extend"?: bool, "anchorRow"?: n} (a click in page points, 0-based page, on a selection area of a table in the caret's story: just above a column selects the column, left of a row the row, along a cell's left edge inside it the cell, the move handle past the top-left corner (top-right in a right-to-left table) the table; with toX/toY (a drag) every column, row or cell from there to that point; the page margin beside a table (its leading side: right for a right-to-left table) is a row area too, and `extend` (Shift+click) on one takes the rows from the selected ones' anchor, or from row `anchorRow` (0-based) when given, so a drag keeps its press's anchor. `reach` (default 3) is the column borders' resize reach, which the cell areas keep clear of.)"#,
)
.pure(),
CommandSpec::new("table.insertAt", "Insert Row or Column by Clicking", "Table Layout › Rows & Columns", insert_at).params(
r#"{"page": n, "x": pt, "y": pt, "reach"?: pt} (a click on a table's insert button: left of a row border adds a row there, above a column border a column; the new row or column is selected, as in Word)"#,
),
CommandSpec::new("table.moveDrag", "Move Table by Dragging", "Table Layout › Table", move_drag).params(
r#"{"page": n, "x": pt, "y": pt, "toX": pt, "toY": pt, "reach"?: pt} (a drag from the move handle past a table's top-left corner: the table floats where it's dropped, the text wrapping around it; tables in the main text only)"#,
r#"{"page": n, "x": pt, "y": pt, "toX": pt, "toY": pt, "reach"?: pt} (a drag from the move handle past a table's top-left corner (top-right in a right-to-left table): the table floats where it's dropped, the text wrapping around it; tables in the main text only)"#,
),
CommandSpec::new("table.fitColumn", "AutoFit Column by Double-Clicking", "Table Layout › Cell Size", fit_column).params(
r#"{"page": n, "x": pt, "y": pt, "reach"?: pt} (a double-click on a column border: the column left of it fits its contents and the columns right of it keep their widths, moving with the border, so the table narrows or widens (up to the right margin), as in Word)"#,
@@ -67,7 +68,8 @@ pub enum Spot {
/// The insert button of horizontal border `n` (0 = the table's top, `n` = the bottom of row
/// `n - 1`).
InsertRow(usize),
/// The insert button of grid line `n` (0 = the table's left edge).
/// The insert button of grid line `n` (0 = the table's left edge, or a right-to-left table's
/// right edge).
InsertColumn(usize),
/// The selection area above grid column `n`.
Column(usize),
@@ -92,9 +94,11 @@ pub struct InsertMark {
pub to: (f32, f32),
}
/// The move handle's square (page points), just past the table's top-left corner.
/// The move handle's square (page points), just past the table's top-left corner, or its
/// top-right one when the table runs right to left (#6438).
pub fn handle(geom: &ResizeTarget) -> Rect {
Rect::new(geom.left() - HANDLE_GAP - HANDLE, geom.top() - HANDLE_GAP - HANDLE, HANDLE, HANDLE)
let x = if geom.rtl { geom.right() + HANDLE_GAP } else { geom.left() - HANDLE_GAP - HANDLE };
Rect::new(x, geom.top() - HANDLE_GAP - HANDLE, HANDLE, HANDLE)
}
/// Whether the table's first row is on this page (its move handle is there).
@@ -116,10 +120,12 @@ fn nearest(lines: impl Iterator<Item = (usize, f32)>, at: f32, reach: f32) -> Op
lines.map(|(i, v)| (i, (at - v).abs())).filter(|(_, d)| *d <= reach).min_by(|a, b| a.1.total_cmp(&b.1)).map(|(i, _)| i)
}
/// The grid column at page x `x` (the first or the last beyond the table).
/// The grid column at page x `x` (the first or the last beyond the table); a right-to-left
/// table's count from the right.
fn column_at(geom: &ResizeTarget, x: f32) -> usize {
let n = geom.cols.len().saturating_sub(1);
geom.cols.iter().skip(1).take_while(|cx| **cx <= x).count().min(n.saturating_sub(1))
let drawn = geom.cols.iter().skip(1).take_while(|cx| **cx <= x).count().min(n.saturating_sub(1));
if geom.rtl { n.saturating_sub(1).saturating_sub(drawn) } else { drawn }
}
/// The drawn row at page y `y` (the first or the last beyond the table).
@@ -140,7 +146,7 @@ fn spot_in(geom: &ResizeTarget, cells: &[&CellBox], x: f32, y: f32, reach: f32,
if y < top
&& let Some(line) = nearest(lines, x, INSERT_REACH)
{
return Some(Spot::InsertColumn(line));
return Some(Spot::InsertColumn(geom.grid_line(line)));
}
if y >= top - COLUMN_ABOVE && x >= left && x <= right {
return Some(Spot::Column(column_at(geom, x)));
@@ -230,7 +236,8 @@ pub fn handle_table(l: &DocLayout, doc: &Document, story: StoryRef, page: usize,
.into_iter()
.filter(|g| {
let h = handle(g);
g.story == story && starts_here(g) && x >= h.x - pad && x <= g.right() && y >= h.y - pad && y <= g.bottom()
let (x0, x1) = if g.rtl { (g.left(), h.right() + pad) } else { (h.x - pad, g.right()) };
g.story == story && starts_here(g) && x >= x0 && x <= x1 && y >= h.y - pad && y <= g.bottom()
})
.max_by_key(|g| g.table.0.len())
}
@@ -256,7 +263,7 @@ pub fn caret_handles(l: &DocLayout, doc: &Document, story: StoryRef, table: &Pat
let Some(g) = table_resize::tables_on(l, doc, page).into_iter().find(|g| g.story == story && g.table == *table) else {
return (None, None);
};
(first.then(|| handle(&g)), last.then(|| g.handle()))
(first.then(|| handle(&g)), (last && g.resizable_columns()).then(|| g.handle()))
}
/// The insert button a target is, if it's one.
@@ -269,7 +276,7 @@ pub fn insert_mark(t: &Target) -> Option<InsertMark> {
Some(InsertMark { center: (g.left() - BUTTON_OUT, y), from: (g.left(), y), to: (g.right(), y) })
}
Spot::InsertColumn(line) => {
let x = g.cols.get(line).copied()?;
let x = g.cols.get(g.grid_line(line)).copied()?;
Some(InsertMark { center: (x, g.top() - BUTTON_OUT), from: (x, g.top()), to: (x, g.bottom()) })
}
_ => None,
@@ -379,6 +379,79 @@ fn dragging_the_handle_moves_the_table() {
assert!(!movable(&nested));
}
fn rtl_table_session(rows: usize, cols: usize) -> Session {
let mut s = table_session(rows, cols);
s.run("table.rtl", &json!({"value": true})).unwrap();
s
}
/// #6438: a right-to-left table's first column is drawn at the right, and its move handle sits
/// past its top-right corner, not by the first cell's left edge in the middle of the table. The
/// pointer finds the table's edges where they are drawn; the handle selects it.
#[test]
fn a_right_to_left_tables_move_handle_is_past_its_top_right_corner() {
let mut s = rtl_table_session(3, 4);
let (first, last) = (cell_rect(&mut s, 0, 0), cell_rect(&mut s, 0, 3));
assert!(first.x > last.right(), "the first column is drawn at the right: {first:?} {last:?}");
let g = table_resize::tables_on(&s.layout(), &s.doc, 0)[0].clone();
assert_eq!((g.left(), g.right()), (last.x, first.right()), "the table's drawn edges");
let h = handle(&g);
assert!(h.x > first.right() && h.bottom() < first.y, "past the top-right corner: {h:?}, {first:?}");
assert_eq!(spot_at(&mut s, h.x + h.w / 2.0, h.y + h.h / 2.0), Some(Spot::Handle));
assert_ne!(spot_at(&mut s, first.x - 6.0, first.y - 6.0), Some(Spot::Handle), "not by the first cell's left edge");
// The caret's table shows it there, and the pointer finds it on its way there.
let l = s.layout();
assert_eq!(caret_handles(&l, &s.doc, StoryRef::Body, &table_path(&s), 0).0, Some(h));
assert_eq!(handle_table(&l, &s.doc, StoryRef::Body, 0, h.right(), h.bottom(), REACH).map(|g| g.table), Some(table_path(&s)));
s.run("table.selectAt", &json!({"page": 0, "x": h.x + 4.0, "y": h.y + 4.0})).unwrap();
assert_eq!(s.cell_range().map(|r| (r.rows, r.cols)), Some(((0, 2), (0, 3))));
}
/// #6438: dragging a right-to-left table's move handle moves the whole table as far as the
/// pointer went, as with any table, instead of putting its left edge where its first cell's was
/// (past the right margin, all but one column off the page). Dragged far right, it stays on the
/// page.
#[test]
fn dragging_a_right_to_left_tables_handle_moves_the_whole_table() {
let mut s = rtl_table_session(3, 4);
let row = |s: &mut Session| (0..4).map(|c| cell_rect(s, 0, c)).collect::<Vec<_>>();
let before = row(&mut s);
let h = handle(&table_resize::tables_on(&s.layout(), &s.doc, 0)[0]);
let (hx, hy) = (h.x + h.w / 2.0, h.y + h.h / 2.0);
s.run("table.moveDrag", &json!({"page": 0, "x": hx, "y": hy, "toX": hx - 40.0, "toY": hy + 30.0})).unwrap();
assert!(first_table(&s).props.float.is_some());
for (a, b) in before.iter().zip(row(&mut s)) {
assert!((b.x - (a.x - 40.0)).abs() < 0.5 && (b.y - (a.y + 30.0)).abs() < 0.5 && (b.w - a.w).abs() < 0.5, "{a:?} → {b:?}");
}
let h = handle(&table_resize::tables_on(&s.layout(), &s.doc, 0)[0]);
let (hx, hy) = (h.x + h.w / 2.0, h.y + h.h / 2.0);
s.run("table.moveDrag", &json!({"page": 0, "x": hx, "y": hy, "toX": hx + 1000.0, "toY": hy})).unwrap();
let (moved, page_w) = (row(&mut s), s.layout().pages[0].w);
assert!(moved[0].right() <= page_w + 0.5 && moved[0].right() > page_w - 1.0, "at the page's right edge: {moved:?}");
assert!((moved[0].right() - moved[3].x - (before[0].right() - before[3].x)).abs() < 0.5, "the whole table: {moved:?}");
}
/// A right-to-left table's columns are found where they are drawn: the area above its right
/// column selects the first, above its left one the last; the insert button on the border left
/// of the first cell adds the second column.
#[test]
fn a_right_to_left_tables_columns_are_found_where_they_are_drawn() {
let mut s = rtl_table_session(2, 3);
fill(&mut s);
let (c0, c1, c2) = (cell_rect(&mut s, 0, 0), cell_rect(&mut s, 0, 1), cell_rect(&mut s, 0, 2));
assert_eq!(spot_at(&mut s, c0.x + c0.w / 2.0, c0.y - 3.0), Some(Spot::Column(0)));
assert_eq!(spot_at(&mut s, c2.x + c2.w / 2.0, c2.y - 3.0), Some(Spot::Column(2)));
s.run("table.selectAt", &json!({"page": 0, "x": c2.x + c2.w / 2.0, "y": c2.y - 3.0})).unwrap();
assert_eq!(s.selected_text(), "r0c2\nr1c2");
s.run("table.selectAt", &json!({"page": 0, "x": c0.x + 20.0, "y": c0.y - 3.0, "toX": c1.x + 20.0, "toY": c1.y - 3.0})).unwrap();
assert_eq!(s.cell_range().map(|r| r.cols), Some((0, 1)), "a drag from the first column to the second");
assert_eq!(spot_at(&mut s, c0.x, c0.y - 8.0), Some(Spot::InsertColumn(1)));
s.run("table.insertAt", &json!({"page": 0, "x": c0.x, "y": c0.y - 8.0})).unwrap();
assert_eq!(first_table(&s).cols(), 4);
assert!(para(&s, 0, 0).plain_text() == "r0c0" && para(&s, 0, 1).is_empty() && para(&s, 0, 2).plain_text() == "r0c1");
assert_eq!(s.cell_range().map(|r| r.cols), Some((1, 1)), "it's selected");
}
/// A double-click on a column border fits the column left of it to its text; as in Word, the
/// columns right of it keep their widths and move along, so the table narrows. The right
/// edge's column fits the same way.
+49 -10
View File
@@ -4,7 +4,8 @@
//! only in their rows. The geometry comes from the layout (what is on the page, in
//! page points), so the result is what the drag showed even when autofit or a percentage width
//! made the drawn columns differ from the stored grid. The app sends each finished drag here,
//! so every drag is one undo step.
//! so every drag is one undo step. A right-to-left table's rows resize; its columns and corner
//! don't yet (`ResizeTarget::resizable_columns`).
use serde_json::Value;
use wordcraft_doc::props::{Align, HeightRule, VMerge};
@@ -72,7 +73,8 @@ pub struct DrawnRow {
pub row: usize,
pub top: f32,
pub bottom: f32,
/// Grid lines that are cell borders in this row (a merged cell hides the ones it covers).
/// Drawn grid lines (`ResizeTarget::cols`' indices) that are cell borders in this row (a
/// merged cell hides the ones it covers).
pub(crate) edges: Vec<usize>,
}
@@ -82,12 +84,16 @@ pub struct ResizeTarget {
pub edge: Edge,
pub story: StoryRef,
pub table: Path,
/// Drawn grid lines, page x, left to right (columns + 1 of them).
/// Drawn grid lines, page x, left to right (columns + 1 of them); a right-to-left table's
/// grid runs the other way (`grid_line`).
pub cols: Vec<f32>,
/// Drawn rows on this page, top to bottom.
pub rows: Vec<DrawnRow>,
/// Whether the table's last row is on this page (the handle is shown only then).
pub ends_here: bool,
/// Whether the table runs right to left (`w:bidiVisual`): its first column is drawn at the
/// right.
pub rtl: bool,
/// When cells of the table are selected and the column border is one of theirs
/// (`limit_to_selection`): their rows, each with the grid lines its cells' borders are on.
/// The border moves only there; empty, it moves in the whole column.
@@ -131,6 +137,18 @@ impl ResizeTarget {
pub fn handle(&self) -> Rect {
Rect::new(self.right() + 1.0, self.bottom() + 1.0, HANDLE, HANDLE)
}
/// The grid line that drawn line `line` (`cols`' index) is, or the other way round: the same
/// in a left-to-right table, counted from the other edge in a right-to-left one.
pub fn grid_line(&self, line: usize) -> usize {
if self.rtl { self.cols.len().saturating_sub(1).saturating_sub(line) } else { line }
}
/// Whether the table's column borders and resize handle are grabbed. Not a right-to-left
/// table's: its grid runs from its right edge, and these drags, made for a grid running from
/// the left, would put the new widths on the wrong columns. Mirrored ones aren't built yet
/// (#6438).
pub fn resizable_columns(&self) -> bool {
!self.rtl
}
/// The new widths and heights a drag that moved the pointer by (`dx`, `dy`) points gives;
/// `share` says who makes up a column border's move (`true`: Shift is held).
@@ -340,18 +358,23 @@ pub(crate) fn cell_boxes(l: &DocLayout, page: usize) -> Vec<CellBox> {
.collect()
}
/// The drawn geometry of one table on a page, from its cells' boxes.
/// The drawn geometry of one table on a page, from its cells' boxes: its grid lines as drawn,
/// left to right, and its rows.
fn geometry(t: &Table, boxes: &[&CellBox]) -> Option<(Vec<f32>, Vec<DrawnRow>)> {
let n = t.cols();
if n == 0 || n > wordcraft_doc::table::MAX_COLS || boxes.is_empty() {
return None;
}
// Lines are counted from the left as drawn. A right-to-left table's grid runs from its right
// edge (`layout::table`), so a cell's left edge is the grid line it ends at, from the right.
let rtl = t.props.bidi_visual;
let mut lines: Vec<Option<f32>> = vec![None; n + 1];
let mut rows: Vec<DrawnRow> = Vec::new();
for b in boxes {
let g = t.grid_col(b.row, b.cell);
let span = t.rows.get(b.row).and_then(|r| r.cells.get(b.cell)).map_or(1, wordcraft_doc::Cell::span);
let end = g.saturating_add(span).min(n);
let (g, end) = if rtl { (n.saturating_sub(end), n.saturating_sub(g)) } else { (g, end) };
if let Some(x) = lines.get_mut(g) {
x.get_or_insert(b.rect.x);
}
@@ -371,7 +394,10 @@ fn geometry(t: &Table, boxes: &[&CellBox]) -> Option<(Vec<f32>, Vec<DrawnRow>)>
}
// A line no cell on this page ends at: between its drawn neighbours, as the grid shares it.
let (first, last) = (lines.first().copied().flatten()?, lines.last().copied().flatten()?);
let weight = |i: usize| t.grid.get(i).copied().filter(|w| t.grid.len() == n && w.is_finite() && *w > 0.0).unwrap_or(1.0);
let weight = |i: usize| {
let i = if rtl { n.saturating_sub(1).saturating_sub(i) } else { i };
t.grid.get(i).copied().filter(|w| t.grid.len() == n && w.is_finite() && *w > 0.0).unwrap_or(1.0)
};
let mut cols = Vec::with_capacity(n + 1);
let mut prev = (0usize, first);
for i in 0..=n {
@@ -418,14 +444,20 @@ pub(crate) fn tables_on(l: &DocLayout, doc: &Document, page: usize) -> Vec<Resiz
let mine: Vec<&CellBox> = boxes.iter().filter(|b| b.story == story && b.table == *path).collect();
let (cols, rows) = geometry(t, &mine)?;
let ends_here = rows.last().is_some_and(|r| r.row + 1 == t.rows.len());
Some(ResizeTarget { edge: Edge::Corner, story, table: path.clone(), cols, rows, ends_here, only: Vec::new() })
let rtl = t.props.bidi_visual;
Some(ResizeTarget { edge: Edge::Corner, story, table: path.clone(), cols, rows, ends_here, rtl, only: Vec::new() })
})
.collect()
}
fn on_handle(t: &ResizeTarget, x: f32, y: f32, reach: f32) -> bool {
let h = t.handle();
t.ends_here && x >= h.x - reach / 2.0 && x <= h.right() + reach / 2.0 && y >= h.y - reach / 2.0 && y <= h.bottom() + reach / 2.0
t.ends_here
&& t.resizable_columns()
&& x >= h.x - reach / 2.0
&& x <= h.right() + reach / 2.0
&& y >= h.y - reach / 2.0
&& y <= h.bottom() + reach / 2.0
}
/// The innermost table under page point (`x`, `y`) whose resize handle is on this page, or whose
@@ -437,7 +469,11 @@ pub fn table_under(l: &DocLayout, doc: &Document, page: usize, x: f32, y: f32, r
let reach = if reach.is_finite() { reach.clamp(0.5, 24.0) } else { REACH };
tables_on(l, doc, page)
.into_iter()
.filter(|t| t.ends_here && ((x >= t.left() && x <= t.right() && y >= t.top() && y <= t.bottom()) || on_handle(t, x, y, reach)))
.filter(|t| {
t.ends_here
&& t.resizable_columns()
&& ((x >= t.left() && x <= t.right() && y >= t.top() && y <= t.bottom()) || on_handle(t, x, y, reach))
})
.max_by_key(|t| t.table.0.len())
}
@@ -464,9 +500,12 @@ pub fn resize_target(l: &DocLayout, doc: &Document, page: usize, x: f32, y: f32,
if x < left - reach || x > right + reach || y < top - reach || y > bottom + reach {
continue;
}
// Column borders: in the row under the pointer, only lines that are cell borders there.
// Column borders: in the row under the pointer, only lines that are cell borders there
// (none of a right-to-left table's yet, `resizable_columns`).
let row_here = target.rows.iter().find(|r| y >= r.top && y <= r.bottom).cloned();
if let Some(r) = &row_here {
if let Some(r) = &row_here
&& target.resizable_columns()
{
for line in 0..target.cols.len() {
// The table's left edge is grabbed from outside it (a point in, for the border's
// width): inside starts the first cell.
@@ -128,6 +128,32 @@ fn the_corner_handle_scales_the_table() {
assert!(((d1.bottom() - a1.y) - h * 2.0).abs() < 0.5, "{h} → {}", d1.bottom() - a1.y);
}
/// #6438: a right-to-left table's columns run from its right edge, and mirrored drags for its
/// column borders and corner aren't built yet: they aren't grabbed, so a drag leaves its columns
/// alone (one used to squeeze all but the first to nothing). Its rows' borders resize as in any
/// table.
#[test]
fn a_right_to_left_tables_columns_keep_their_widths() {
let mut s = table_session(2, 3);
s.run("table.rtl", &json!({"value": true})).unwrap();
let start = first_table(&s);
let (a, c) = (cell_rect(&mut s, 0, 0), cell_rect(&mut s, 1, 2));
let y = a.y + a.h / 2.0;
let l = s.layout();
for x in [a.x, a.x + 1.0, a.x - 1.0, a.right(), c.x, c.right()] {
assert!(resize_target(&l, &s.doc, 0, x, y, REACH).is_none_or(|t| !matches!(t.edge, Edge::Column(_))), "x {x}");
let _ = s.run("table.resizeDrag", &json!({"page": 0, "x": x, "y": y, "toX": x - 30.0, "toY": y}));
assert_eq!(first_table(&s), start, "x {x}");
}
let below = c.bottom() + 1.0 + HANDLE / 2.0;
for (x, y) in [(a.right() + 1.0 + HANDLE / 2.0, below), (c.x - 1.0 - HANDLE / 2.0, below), (a.x + 10.0, y)] {
assert!(table_under(&l, &s.doc, 0, x, y, REACH).is_none(), "no corner handle at {x}, {y}");
}
let x = a.x + a.w / 2.0;
s.run("table.resizeDrag", &json!({"page": 0, "x": x, "y": a.bottom() + 1.0, "toX": x, "toY": a.bottom() + 21.0})).unwrap();
assert!((cell_rect(&mut s, 0, 0).h - (a.h + 20.0)).abs() < 0.1, "the row border moves");
}
/// A merged cell hides the border it covers: no column border to grab in its middle.
#[test]
fn merged_cells_have_no_border_inside() {
+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);
}
+36 -9
View File
@@ -16,10 +16,11 @@ pub fn open(ext: &str, bytes: &[u8], page: &wordcraft_doc::SectionProps) -> Opti
// that state no size.
other => wordcraft_formats::import_on(other, bytes, page).map(|r| {
r.map(|mut doc| {
if !matches!(other, "odt" | "ott" | "fodt") {
let odf = matches!(other, "odt" | "ott" | "fodt");
if !odf {
crate::io::fit_pictures(&mut doc);
}
draw_charts(&mut doc);
draw_charts(&mut doc, odf);
doc
})
}),
@@ -27,8 +28,9 @@ pub fn open(ext: &str, bytes: &[u8], page: &wordcraft_doc::SectionProps) -> Opti
}
/// Draw the charts a reader left undrawn (ODT charts come as their chart model alone): in the
/// document's theme colours, at their size.
fn draw_charts(doc: &mut Document) {
/// document's theme colours, at their size. With `odf`, scatter series are set to markers without
/// a line first (see `markers_only_scatter`).
pub(crate) fn draw_charts(doc: &mut Document, odf: bool) {
use wordcraft_doc::para::InlineObject;
/// Groups nest at most this deep here (the readers flatten deeper ones).
const DEPTH: usize = 16;
@@ -39,16 +41,41 @@ fn draw_charts(doc: &mut Document) {
_ => false,
}
}
fn draw(o: &mut InlineObject, theme: &[wordcraft_doc::Rgb], depth: usize) {
fn draw(o: &mut InlineObject, theme: &[wordcraft_doc::Rgb], odf: bool, depth: usize) {
match o {
InlineObject::Group { children, .. } if depth < DEPTH => children.iter_mut().for_each(|c| draw(&mut c.obj, theme, depth + 1)),
o if undrawn(o, depth) => crate::cmd::chart::redraw(o, theme),
InlineObject::Group { children, .. } if depth < DEPTH => children.iter_mut().for_each(|c| draw(&mut c.obj, theme, odf, depth + 1)),
o if undrawn(o, depth) => {
if odf {
markers_only_scatter(o);
}
crate::cmd::chart::redraw(o, theme)
}
_ => {}
}
}
// The ODF reader does not read a scatter chart's line setting, so every scatter series of an
// opened ODF file is set to markers without a line, as the ODF writer writes them. A series
// with its markers turned off gets them back, as Change Chart Type does, so it is still drawn.
fn markers_only_scatter(o: &mut InlineObject) {
if let InlineObject::Graphic { graphic, .. } = o
&& let Some(spec) = graphic.chart.as_deref()
&& spec.kind == wordcraft_doc::chart::ChartType::Scatter
&& spec.series.iter().any(|s| !s.no_line || s.markers == Some(false))
{
let mut spec = spec.clone();
for series in &mut spec.series {
series.no_line = true;
if series.markers == Some(false) {
series.markers = None;
}
}
std::sync::Arc::make_mut(graphic).chart = Some(std::sync::Arc::new(spec));
}
}
let theme = doc.settings.theme_colors.clone();
// (Drawing them doesn't change the document: markup kept as read for them still is.)
doc.fill_objects(&|o| undrawn(o, 0), &mut |o| draw(o, &theme, 0));
// (Markup kept as read for these charts is still written as read after they are drawn,
// scatter charts included.)
doc.fill_objects(&|o| undrawn(o, 0), &mut |o| draw(o, &theme, odf, 0));
}
/// Save to a format other than plain text / JSON.
+18 -1
View File
@@ -15,6 +15,9 @@ const MAX_FILES: usize = 16;
const DISK_BUDGET: u64 = 2 << 30;
/// Times a record lock held for a moment (by a process being started) is tried again.
const LOCK_RETRIES: u32 = 10;
/// How many times, 10 ms apart, a record's emptied folder is removed again while it isn't empty
/// yet ([`remove_record_dir`]).
const REMOVE_RETRIES: u32 = 100;
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -347,11 +350,25 @@ impl Store {
self.entries.remove(id);
self.locks.remove(id);
fs::remove_file(dir.join("owner.lock")).map_err(|e| e.to_string())?;
fs::remove_dir(&dir).map_err(|e| e.to_string())?;
remove_record_dir(&dir, |d| fs::remove_dir(d)).map_err(|e| e.to_string())?;
sync_dir(&self.root).map_err(|e| e.to_string())?;
Ok(())
}
}
/// Removes a record's emptied folder `dir` with `remove`. On Windows a file just deleted stays in
/// its folder, pending deletion, while another process still has it open (an antivirus scanner
/// or a search indexer looking at a file just written), so the folder isn't empty yet and
/// removing it fails ("The directory is not empty", os error 145): wait that out, retrying
/// [`REMOVE_RETRIES`] times 10 ms apart, as [`Store::lock`] waits out a lock held for a moment.
fn remove_record_dir(dir: &Path, mut remove: impl FnMut(&Path) -> io::Result<()>) -> io::Result<()> {
for _ in 0..REMOVE_RETRIES {
match remove(dir) {
Err(e) if e.kind() == io::ErrorKind::DirectoryNotEmpty => std::thread::sleep(Duration::from_millis(10)),
done => return done,
}
}
remove(dir)
}
struct Limited<W> {
inner: W,
remaining: u64,
@@ -619,3 +619,37 @@ fn finish_recovery_covers_every_open_document() {
s.finish_recovery();
assert_eq!(records(&t.0), [dirty]);
}
/// Removing a record's emptied folder waits out a file Windows hasn't finished deleting (another
/// process, such as an antivirus scanner, still has it open, so the folder isn't empty yet: os
/// error 145), which made saving with a password, and discarding a closed document's copy, fail
/// now and then. It doesn't wait on other errors, nor forever.
#[test]
fn removing_a_record_folder_waits_for_files_still_being_deleted() {
let not_empty = || io::Error::from(io::ErrorKind::DirectoryNotEmpty);
let mut pending = 3;
let removed = remove_record_dir(Path::new("record"), |_| {
if pending > 0 {
pending -= 1;
Err(not_empty())
} else {
Ok(())
}
});
assert!(removed.is_ok(), "{removed:?}");
assert_eq!(pending, 0);
let mut calls = 0;
let stuck = remove_record_dir(Path::new("record"), |_| {
calls += 1;
Err(not_empty())
});
assert_eq!(stuck.map_err(|e| e.kind()), Err(io::ErrorKind::DirectoryNotEmpty));
assert_eq!(calls, REMOVE_RETRIES + 1);
let mut calls = 0;
let denied = remove_record_dir(Path::new("record"), |_| {
calls += 1;
Err(io::Error::from(io::ErrorKind::PermissionDenied))
});
assert_eq!(denied.map_err(|e| e.kind()), Err(io::ErrorKind::PermissionDenied));
assert_eq!(calls, 1);
}
+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();
+351 -2
View File
@@ -18,6 +18,7 @@ mod fit_one_page;
mod font_sizes;
mod fonts;
mod ime_noop;
mod page_number_formats_in_lists;
mod para_tools;
mod revision_checks;
mod typing;
@@ -1051,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)}));
@@ -7763,6 +7765,81 @@ fn chart_edits_are_one_undo_step_each() {
assert_eq!((*w, graphic.w, graphic.h), (300.0, 300.0, 150.0));
}
/// Scatter series are drawn as markers without a line: inserted with data, changed to scatter
/// and back, and added by Edit Data. Edit Data keeps each replaced series' formatting.
#[test]
fn scatter_chart_series_have_no_line_and_edit_data_keeps_formatting() {
let no_lines = |s: &Session| first_chart(&s.doc).unwrap().series.iter().map(|x| x.no_line).collect::<Vec<_>>();
let mut s = s();
run(&mut s, "insert.chart", json!({"type": "scatter", "data": {"categories": [1, 2], "series": [{"name": "A", "values": [1, 2]}]}}));
assert_eq!(no_lines(&s), [true]);
run(&mut s, "chart.type", json!({"type": "line"}));
assert_eq!(no_lines(&s), [false]);
run(&mut s, "chart.type", json!({"type": "scatter"}));
assert_eq!(no_lines(&s), [true]);
// Formatting read from a file, set on the chart's model.
let set = |s: &mut Session, f: &dyn Fn(&mut wordcraft_doc::chart::ChartSpec)| {
let mut spec = first_chart(&s.doc).unwrap();
f(&mut spec);
let pos = Pos::body(0, 0);
let p = s.doc.para_mut(pos.story, &pos.path).unwrap();
let off = p.object_offsets()[0];
if let Some(wordcraft_doc::para::InlineObject::Graphic { graphic, .. }) = p.object_at_mut(off) {
std::sync::Arc::make_mut(graphic).chart = Some(std::sync::Arc::new(spec));
}
};
// A series' formatting stays through Edit Data; a new series gets the type's default.
set(&mut s, &|c| c.series[0].fill = Some(wordcraft_doc::props::Rgb(0x12, 0x34, 0x56)));
run(&mut s, "chart.editData", json!({"data": {"series": [{"name": "B", "values": [3, 4]}, {"name": "C", "values": [5, 6]}]}}));
let c = first_chart(&s.doc).unwrap();
assert_eq!(c.series[0].fill, Some(wordcraft_doc::props::Rgb(0x12, 0x34, 0x56)));
assert_eq!((c.series[0].name.as_str(), c.series[1].name.as_str()), ("B", "C"));
assert_eq!(no_lines(&s), [true, true]);
// A line-only series made a scatter series again gets its markers back, so it stays visible.
set(&mut s, &|c| c.series[0].markers = Some(false));
run(&mut s, "chart.type", json!({"type": "line"}));
run(&mut s, "chart.type", json!({"type": "scatter"}));
assert_eq!(first_chart(&s.doc).unwrap().series[0].markers, None);
// The chart's Data Labels setting replaces a series' own.
set(&mut s, &|c| c.series[1].labels = Some(true));
run(&mut s, "chart.dataLabels", json!({"on": false}));
let c = first_chart(&s.doc).unwrap();
assert!(!c.data_labels && c.series.iter().all(|x| x.labels.is_none()));
}
/// A pie chart changed to a column chart gets a new column chart's value axis, and back; an
/// axis that was changed stays as it is.
#[test]
fn chart_type_between_round_and_axis_charts_sets_the_value_axis() {
use wordcraft_doc::chart::{ChartAxis, value_axis};
let mut s = s();
run(&mut s, "insert.chart", json!({"type": "pie"}));
assert_eq!(first_chart(&s.doc).unwrap().val_axis, ChartAxis::default());
run(&mut s, "chart.type", json!({"type": "column"}));
assert_eq!(first_chart(&s.doc).unwrap().val_axis, value_axis());
run(&mut s, "chart.type", json!({"type": "doughnut"}));
assert_eq!(first_chart(&s.doc).unwrap().val_axis, ChartAxis::default());
let mut s = self::s();
run(&mut s, "insert.chart", json!({"type": "column"}));
let pos = Pos::body(0, 0);
let p = s.doc.para_mut(pos.story, &pos.path).unwrap();
let off = p.object_offsets()[0];
if let Some(wordcraft_doc::para::InlineObject::Graphic { graphic, .. }) = p.object_at_mut(off) {
let g = std::sync::Arc::make_mut(graphic);
let mut spec = (**g.chart.as_ref().unwrap()).clone();
spec.val_axis.max = Some(50.0);
g.chart = Some(std::sync::Arc::new(spec));
}
run(&mut s, "chart.type", json!({"type": "pie"}));
run(&mut s, "chart.type", json!({"type": "bar"}));
assert_eq!(first_chart(&s.doc).unwrap().val_axis, ChartAxis { max: Some(50.0), ..value_axis() });
}
#[test]
fn hostile_chart_data_is_capped() {
use wordcraft_doc::chart::{MAX_CELLS, MAX_POINTS, MAX_SERIES, MAX_TEXT};
@@ -8472,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
@@ -10253,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!([]));
}
@@ -12007,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};
@@ -13028,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]
@@ -13168,6 +13443,80 @@ fn an_odt_charts_paragraph_is_written_as_read_after_drawing_it() {
assert!(content.contains(r#"draw:name="Chart1""#) && !content.contains("wcChart"), "{content}");
}
/// A scatter chart saved as ODT (or flat ODT) and opened again is still drawn as markers without
/// lines, and its paragraph is still written as read.
#[test]
fn an_odt_scatter_chart_reopens_without_lines() {
use wordcraft_doc::para::InlineObject;
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "insert.chart", json!({"type": "scatter"}));
let odt = wordcraft_formats::odt::export(&s.doc).unwrap();
let fodt = wordcraft_formats::odt::export_flat(&s.doc).unwrap();
for (name, bytes) in [("t.odt", odt), ("t.fodt", fodt)] {
let doc = crate::io::open_bytes(name, &bytes).unwrap();
let c = first_chart(&doc).expect("the chart has its model");
assert_eq!(c.kind, wordcraft_doc::chart::ChartType::Scatter);
assert!(!c.series.is_empty() && c.series.iter().all(|x| x.no_line), "{name}: {:?}", c.series);
// The drawn items are the markers-only drawing, not the one with lines between points.
let p = doc.para_at(&Pos::body(0, 0)).unwrap();
let (items, w, h) = p
.object_offsets()
.into_iter()
.find_map(|off| match p.object_at(off) {
Some(InlineObject::Graphic { graphic, w, h, .. }) if graphic.chart.is_some() => Some((graphic.items.clone(), *w, *h)),
_ => None,
})
.expect("the chart is drawn");
let theme = &doc.settings.theme_colors;
assert_eq!(items, crate::cmd::chart::chart_graphic(c.clone(), theme, w, h).items, "{name}");
let mut lines = c.clone();
lines.series.iter_mut().for_each(|x| x.no_line = false);
assert_ne!(items, crate::cmd::chart::chart_graphic(lines, theme, w, h).items, "{name}");
}
let doc = crate::io::open_bytes("t.odt", &wordcraft_formats::odt::export(&s.doc).unwrap()).unwrap();
let again = wordcraft_formats::odt::export(&doc).unwrap();
let mut z = craft_zip::Archive::open(&again, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap();
let content = String::from_utf8(z.read("content.xml", 16 << 20).unwrap().unwrap()).unwrap();
assert!(content.contains(r#"draw:name="Chart1""#) && !content.contains("wcChart"), "{content}");
}
/// An undrawn scatter series with its markers turned off is not left with neither a line nor
/// markers when an opened ODF file's scatter charts are set to markers only; other formats keep
/// their series as read.
#[test]
fn an_opened_odf_scatter_series_keeps_its_markers() {
use std::sync::Arc;
use wordcraft_doc::para::InlineObject;
let mut s = Session::new(wordcraft_doc::Document::new());
run(&mut s, "insert.chart", json!({"type": "scatter"}));
let undrawn = |doc: &wordcraft_doc::Document| {
let mut doc = doc.clone();
doc.fill_objects(&|o| matches!(o, InlineObject::Graphic { graphic, .. } if graphic.chart.is_some()), &mut |o| {
if let InlineObject::Graphic { graphic, .. } = o {
let g = Arc::make_mut(graphic);
if let Some(spec) = g.chart.as_deref() {
let mut spec = spec.clone();
for series in &mut spec.series {
series.no_line = false;
series.markers = Some(false);
}
g.chart = Some(Arc::new(spec));
}
g.items.clear();
}
});
doc
};
let mut odf = undrawn(&s.doc);
crate::io_ext::draw_charts(&mut odf, true);
let c = first_chart(&odf).expect("the chart has its model");
assert!(!c.series.is_empty() && c.series.iter().all(|x| x.no_line && x.markers.is_none()), "{:?}", c.series);
let mut other = undrawn(&s.doc);
crate::io_ext::draw_charts(&mut other, false);
let c = first_chart(&other).expect("the chart has its model");
assert!(c.series.iter().all(|x| !x.no_line && x.markers == Some(false)), "{:?}", c.series);
}
/// A picture an OpenDocument file makes wider than the text (a rule across the whole page) stays
/// that wide, and its paragraph is written as read: opening a file changes nothing in it.
#[test]
@@ -46,3 +46,49 @@ fn normal_apostrophe_search_keeps_exact_modes_and_original_byte_ranges() {
run(&mut s, "edit.undo", json!({}));
assert_eq!(text(&s), original);
}
/// A keyboard double quote finds the curly ones smart quotes type, as a keyboard apostrophe does.
#[test]
fn keyboard_double_quote_finds_smart_quoted_typing() {
let mut s = s();
for ch in "\"hi\"".chars() {
run(&mut s, "text.insert", json!({"text": ch.to_string()}));
}
assert_eq!(text(&s), "“hi”");
for (query, count) in [("\"", 2), ("\"hi", 1), ("hi\"", 1), ("\"hi\"", 1), ("\"HI\"", 1)] {
assert_eq!(run(&mut s, "edit.find", json!({"text": query}))["count"], count, "{query}");
}
assert_eq!(run(&mut s, "edit.find", json!({"text": "\"HI\"", "matchCase": true}))["count"], 0);
assert_eq!(run(&mut s, "edit.replaceAll", json!({"text": "\"hi\"", "with": "[^&]"}))["replaced"], 1);
assert_eq!(text(&s), "[“hi”]");
run(&mut s, "edit.undo", json!({}));
assert_eq!(text(&s), "“hi”");
}
/// Straight and curly double quotes: only a typed `"` in a normal search reads as all three.
#[test]
fn normal_double_quote_search_keeps_exact_modes_and_original_byte_ranges() {
let mut s = s();
let original = "🙂 \"hi\" “hi” ”hi“ 'hi' ″hi″";
run(&mut s, "document.setText", json!({"text": original}));
let found = run(&mut s, "edit.find", json!({"text": "\"hi\""}));
assert_eq!(found["count"], 3);
let ranges: Vec<_> =
found["matches"].as_array().unwrap().iter().map(|m| (m["start"]["off"].as_u64().unwrap(), m["end"]["off"].as_u64().unwrap())).collect();
assert_eq!(ranges, [(5, 9), (10, 18), (19, 27)]);
assert_eq!(run(&mut s, "edit.find", json!({"text": "\""}))["count"], 6);
for options in [json!({"special": false}), json!({"wildcards": true}), json!({"regex": true})] {
let mut exact = super::s();
run(&mut exact, "document.setText", json!({"text": original}));
let mut params = options;
params["text"] = json!("\"hi\"");
assert_eq!(run(&mut exact, "edit.find", params)["count"], 1);
}
for query in ["^034hi^034", "^u8220hi^u8221", "“hi”", "”hi“"] {
assert_eq!(run(&mut s, "edit.find", json!({"text": query}))["count"], 1, "{query}");
}
assert_eq!(run(&mut s, "edit.replaceAll", json!({"text": "\"hi\"", "with": "<^&>"}))["replaced"], 3);
assert_eq!(text(&s), "🙂 <\"hi\"> <“hi”> <”hi“> 'hi' ″hi″");
run(&mut s, "edit.undo", json!({}));
assert_eq!(text(&s), original);
}
+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));
@@ -0,0 +1,260 @@
//! Generated lists and page cross-references write a page as its footer shows it: in the number
//! format of the page's section (`w:pgNumType w:fmt`), as Word does (#3856). The front matter is
//! numbered i, ii; the body restarts at 1.
use serde_json::{Value, json};
use wordcraft_doc::StoryRef;
use wordcraft_doc::para::InlineObject;
use crate::Session;
fn run(s: &mut Session, id: &str, v: Value) {
s.run(id, &v).unwrap_or_else(|e| panic!("{id}: {e}"));
}
/// Page i: "Front"; page ii: "Preface" (Heading 1); next-page section break; page 1: "Chapter one"
/// (Heading 1). Section 1 lowerRoman from 1, section 2 decimal from 1.
fn two_sections() -> Session {
let mut s = Session::new(wordcraft_doc::Document::from_text("Front\nPreface\nChapter one"));
for h in ["Preface", "Chapter one"] {
run(&mut s, "select.text", json!({"text": h}));
run(&mut s, "styles.apply", json!({"style": "Heading 1"}));
}
run(&mut s, "select.text", json!({"text": "Preface"}));
run(&mut s, "caret.home", json!({}));
run(&mut s, "layout.break", json!({"kind": "page"}));
run(&mut s, "select.text", json!({"text": "Chapter one"}));
run(&mut s, "caret.home", json!({}));
run(&mut s, "layout.break", json!({"kind": "nextPage"}));
run(&mut s, "select.text", json!({"text": "Chapter one"}));
run(&mut s, "layout.pageNumberFormat", json!({"format": "decimal", "start": 1}));
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "layout.pageNumberFormat", json!({"format": "lowerRoman", "start": 1}));
s
}
fn styled(s: &Session, prefix: &str) -> Vec<String> {
s.doc
.body
.iter()
.filter_map(|b| b.as_para())
.filter(|p| p.props.style.as_deref().is_some_and(|st| st.starts_with(prefix) && st != "TOCHeading"))
.map(|p| p.plain_text())
.collect()
}
fn field_results(s: &Session, name: &str) -> Vec<String> {
let mut out = Vec::new();
for path in s.doc.para_paths(StoryRef::Body) {
for o in &s.doc.para(StoryRef::Body, &path).unwrap().objects {
if let InlineObject::Field { instr, result, .. } = o
&& instr.trim_start().starts_with(name)
{
out.push(result.clone());
}
}
}
out
}
#[test]
fn toc_page_numbers_follow_the_section_format() {
let mut s = two_sections();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "references.toc", json!({"levels": 1, "hyperlinks": false, "rightAlign": false}));
assert_eq!(styled(&s, "TOC1"), ["Preface ii", "Chapter one 1"]);
}
#[test]
fn toc_pageref_results_follow_the_section_format() {
let mut s = two_sections();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "references.toc", json!({"levels": 1, "hyperlinks": true, "rightAlign": false}));
assert_eq!(field_results(&s, "PAGEREF"), ["ii", "1"]);
}
#[test]
fn toc_upper_roman_and_letters() {
let mut s = two_sections();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "layout.pageNumberFormat", json!({"format": "upperRoman", "start": 4}));
run(&mut s, "select.text", json!({"text": "Chapter one"}));
run(&mut s, "layout.pageNumberFormat", json!({"format": "lowerLetter", "start": 3}));
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "references.toc", json!({"levels": 1, "hyperlinks": false, "rightAlign": false}));
// Preface is the section's 2nd page: 4 + 1 = 5 = V; Chapter one starts at 3 = c.
assert_eq!(styled(&s, "TOC1"), ["Preface V", "Chapter one c"]);
}
#[test]
fn index_pages_follow_the_section_format() {
let mut s = two_sections();
run(&mut s, "select.text", json!({"text": "Preface"}));
run(&mut s, "references.markEntry", json!({"entry": "Preface"}));
run(&mut s, "select.text", json!({"text": "Chapter one"}));
run(&mut s, "references.markEntry", json!({"entry": "Chapter"}));
run(&mut s, "caret.docEnd", json!({}));
run(&mut s, "references.index", json!({"columns": 1, "rightAlign": false}));
let text = s.doc.plain_text(StoryRef::Body);
// The index is the last thing in the document: its lines start with the entry.
let entry = |name: &str| text.lines().rfind(|l| l.starts_with(name)).unwrap_or_default().to_string();
let preface = entry("Preface");
assert!(preface.ends_with("ii"), "index line {preface:?} in {text:?}");
let chapter = entry("Chapter");
assert!(chapter.ends_with('1') && chapter != "Chapter one", "index line {chapter:?} in {text:?}");
}
#[test]
fn page_cross_reference_follows_the_section_format() {
let mut s = two_sections();
run(&mut s, "caret.docEnd", json!({}));
// Before this change the field was inserted with "1", not even the decimal 2: find out which
// page it reads, too.
run(&mut s, "insert.crossReference", json!({"to": "heading", "target": "Preface", "show": "page"}));
assert_eq!(field_results(&s, "PAGEREF"), ["ii"]);
}
#[test]
fn update_fields_still_agree() {
// F9 already formats PAGEREF by the section; inserting must give the same as updating.
let mut s = two_sections();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "references.toc", json!({"levels": 1, "hyperlinks": true, "rightAlign": false}));
let inserted = field_results(&s, "PAGEREF");
crate::cmd::references::update_fields_in(&mut s, true).unwrap();
assert_eq!(field_results(&s, "PAGEREF"), inserted);
}
// PR #5971 review: a decimal-only guard, `1, 1` across two sections, index page ranges and
// `_Ref` bookmark reuse.
/// "Front" / page break / "Preface" (Heading 1) / page break / "Chapter one" (Heading 1), one
/// decimal section: pages 1, 2, 3.
fn decimal_only() -> Session {
let mut s = Session::new(wordcraft_doc::Document::from_text("Front\nPreface\nChapter one"));
for h in ["Preface", "Chapter one"] {
run(&mut s, "select.text", json!({"text": h}));
run(&mut s, "styles.apply", json!({"style": "Heading 1"}));
run(&mut s, "caret.home", json!({}));
run(&mut s, "layout.break", json!({"kind": "page"}));
}
s
}
/// The last line of the body's text that starts with `name` (the index is at the end).
fn index_line(s: &Session, name: &str) -> String {
let text = s.doc.plain_text(StoryRef::Body);
text.lines().rfind(|l| l.starts_with(name)).unwrap_or_default().to_string()
}
/// The body paragraph whose text contains `text`, and the byte offset where `text` starts in it.
fn find(s: &Session, text: &str) -> (wordcraft_doc::Path, usize) {
s.doc
.para_paths(StoryRef::Body)
.into_iter()
.find_map(|path| s.doc.para(StoryRef::Body, &path).and_then(|p| p.text.find(text)).map(|o| (path, o)))
.unwrap_or_else(|| panic!("no paragraph with {text:?}"))
}
fn add_mark(s: &mut Session, path: &wordcraft_doc::Path, off: usize, obj: InlineObject) {
let at = wordcraft_doc::Pos { story: StoryRef::Body, path: path.clone(), off };
s.doc.insert_object(&at, obj, &wordcraft_doc::CharProps::default()).unwrap();
}
#[test]
fn decimal_only_documents_keep_plain_page_numbers() {
let mut s = decimal_only();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "references.toc", json!({"levels": 1, "hyperlinks": false, "rightAlign": false}));
assert_eq!(styled(&s, "TOC1"), ["Preface 2", "Chapter one 3"]);
let mut s = decimal_only();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "references.toc", json!({"levels": 1, "hyperlinks": true, "rightAlign": false}));
assert_eq!(field_results(&s, "PAGEREF"), ["2", "3"]);
let mut s = decimal_only();
run(&mut s, "caret.docEnd", json!({}));
run(&mut s, "insert.crossReference", json!({"to": "heading", "target": "Preface", "show": "page"}));
assert_eq!(field_results(&s, "PAGEREF"), ["2"]);
let mut s = decimal_only();
run(&mut s, "select.text", json!({"text": "Preface"}));
run(&mut s, "references.markEntry", json!({"entry": "Preface"}));
run(&mut s, "select.text", json!({"text": "Chapter one"}));
run(&mut s, "references.markEntry", json!({"entry": "Chapter"}));
run(&mut s, "caret.docEnd", json!({}));
run(&mut s, "references.index", json!({"columns": 1, "rightAlign": false}));
let (preface, chapter) = (index_line(&s, "Preface"), index_line(&s, "Chapter"));
assert!(preface.ends_with('2') && !preface.ends_with("12"), "{preface:?}");
assert!(chapter.ends_with('3') && chapter != "Chapter one", "{chapter:?}");
}
/// The same entry on page 1 of two sections lists both pages: the index merges pages by layout
/// page, not by the number shown.
#[test]
fn the_same_label_on_two_pages_is_listed_twice() {
let mut s = two_sections();
run(&mut s, "caret.docStart", json!({}));
run(&mut s, "layout.pageNumberFormat", json!({"format": "decimal", "start": 1}));
for t in ["Front", "Chapter one"] {
run(&mut s, "select.text", json!({"text": t}));
run(&mut s, "references.markEntry", json!({"entry": "Topic"}));
}
run(&mut s, "caret.docEnd", json!({}));
run(&mut s, "references.index", json!({"columns": 1, "rightAlign": false}));
let line = index_line(&s, "Topic");
assert!(line.ends_with("1, 1"), "{line:?}");
}
/// An index entry for bookmark `Span` (`XE \r`), the bookmark from "Preface" (page ii) to `end`.
fn range_entry(end: Option<(&str, bool)>) -> String {
let mut s = two_sections();
let (path, off) = find(&s, "Preface");
// The end first, so the start's offset stays put when both are in one paragraph.
if let Some((text, at_end_of_para)) = end {
let (epath, eoff) = find(&s, text);
let eoff = if at_end_of_para { s.doc.para(StoryRef::Body, &epath).unwrap().len() } else { eoff + text.len() };
add_mark(&mut s, &epath, eoff, InlineObject::BookmarkEnd { name: "Span".into() });
}
add_mark(&mut s, &path, off, InlineObject::BookmarkStart { name: "Span".into() });
run(&mut s, "select.text", json!({"text": "Front"}));
run(&mut s, "references.markEntry", json!({"entry": "Span", "bookmark": "Span"}));
run(&mut s, "caret.docEnd", json!({}));
run(&mut s, "references.index", json!({"columns": 1, "rightAlign": false}));
index_line(&s, "Span")
}
#[test]
fn an_index_range_across_sections_uses_each_ends_format() {
let line = range_entry(Some(("Chapter one", true)));
assert!(line.ends_with("ii\u{2013}1"), "{line:?}");
}
#[test]
fn an_index_range_on_one_page_is_one_page() {
let line = range_entry(Some(("Preface", false)));
assert!(line.ends_with("ii") && !line.contains('\u{2013}'), "{line:?}");
}
#[test]
fn an_index_range_without_its_end_is_the_start_page() {
let line = range_entry(None);
assert!(line.ends_with("ii") && !line.contains('\u{2013}'), "{line:?}");
}
/// A heading that starts with a page break and already has a `_Ref` bookmark around it from
/// offset 0: a page cross-reference reuses it and reads the heading's page.
#[test]
fn a_ref_bookmark_before_the_page_break_is_reused() {
let mut s = two_sections();
let (path, _) = find(&s, "Preface");
let len = s.doc.para(StoryRef::Body, &path).unwrap().len();
add_mark(&mut s, &path, len, InlineObject::BookmarkEnd { name: "_Ref000000007".into() });
add_mark(&mut s, &path, 0, InlineObject::BookmarkStart { name: "_Ref000000007".into() });
run(&mut s, "caret.docEnd", json!({}));
run(&mut s, "insert.crossReference", json!({"to": "heading", "target": "Preface", "show": "page"}));
let refs: Vec<String> = s.doc.bookmarks().into_iter().map(|b| b.0).filter(|n| n.starts_with("_Ref")).collect();
assert_eq!(refs, ["_Ref000000007"], "no second _Ref bookmark");
assert_eq!(field_results(&s, "PAGEREF"), ["ii"]);
}
+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."
}
]
}
+5
View File
@@ -67,6 +67,11 @@ pub struct FontFace {
/// values).
pub cap_height: f64,
pub x_height: f64,
/// Average character width in font units (`OS/2.xAvgCharWidth`), half the em for an East
/// Asian font, whose average a platform's text metrics report as its half-width cell rather
/// than the full-width figure in the table. Word's automatic spacing between East Asian and
/// Latin text is half of it (see `wordcraft_layout::para::grid::auto_space`).
pub avg_char_width: f64,
pub shaper: harfrust::ShaperData,
/// Variable fonts: the named instance's axis settings (user units; empty = default instance),
/// the normalised location and the shaper's view of it.
+17 -1
View File
@@ -14,6 +14,20 @@ pub(super) fn east_asian_font(f: &skrifa::FontRef<'_>) -> bool {
}
}
/// The font's average character width in font units: `OS/2.xAvgCharWidth`, or half the em when
/// the table is missing or says nothing. An East Asian font gets half the em whatever its table
/// says, because a platform's text metrics report a double-byte font's average as the width of
/// its half-width cell, not the full-width figure the table carries (Yu Gothic says 1972 of
/// 2048, and Word spaces it as 1024).
fn avg_char_width(f: &skrifa::FontRef<'_>, upem: f64) -> f64 {
use skrifa::raw::TableProvider;
let half = upem / 2.0;
if east_asian_font(f) {
return half;
}
f.os2().ok().map(|t| f64::from(t.x_avg_char_width())).filter(|v| v.is_finite() && *v > 0.0).unwrap_or(half).min(upem)
}
/// A family name in another language, from a font's `name` table.
#[derive(Clone, Debug, PartialEq)]
pub struct LocalName {
@@ -371,6 +385,7 @@ pub(super) fn make_face(
.map(|v| v >= 0.5)
.or(axis(b"slnt").map(|v| v.abs() > 0.1))
.unwrap_or(!matches!(a.style, skrifa::attribute::Style::Normal) || style_italic(&style));
let upem = m.units_per_em.max(1) as f64;
Some(FontFace {
id: NEXT_ID.fetch_add(1, Ordering::Relaxed),
family,
@@ -381,11 +396,12 @@ pub(super) fn make_face(
postscript: coords.is_empty().then(|| name(&f, &[StringId::POSTSCRIPT_NAME])).flatten(),
weight,
italic,
upem: m.units_per_em.max(1) as f64,
upem,
ascent: m.ascent as f64,
descent: -(m.descent as f64),
cap_height: m.cap_height.map(|v| v as f64).filter(|v| *v > 0.0).unwrap_or(m.ascent as f64 * 0.72),
x_height: m.x_height.map(|v| v as f64).filter(|v| *v > 0.0).unwrap_or(m.ascent as f64 * 0.5),
avg_char_width: avg_char_width(&f, upem),
shaper,
coords,
location,
+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);
+18
View File
@@ -579,6 +579,24 @@ fn east_asian_fonts_are_listed_for_the_asian_text_font_box() {
assert_eq!(listed(&db), 1);
}
#[test]
fn an_east_asian_faces_average_character_width_is_its_half_width_cell() {
// #6583: Word's automatic space beside East Asian text is half the average character width
// of the face before it, and a platform's text metrics report a double-byte font's average
// as the width of its half-width cell, not the full-width figure its OS/2 table carries. The
// two faces here share one OS/2 table and differ only in their code pages.
let dir = font_dir("avgcharwidth");
let song = with_family_names("SourceSans3-Regular.ttf", &[(0x0409, "Songti Width")]);
std::fs::write(dir.join("song.ttf"), with_code_pages(&song, 1 << 18)).unwrap();
let db = FontDb::with_font_dirs(vec![dir]);
let latin = db.face(FAMILY, "Regular");
let asian = db.face("Songti Width", "Regular");
assert!(latin.upem > 0.0 && !latin.is_east_asian() && asian.is_east_asian());
// The Latin face keeps the table's figure, which is not half the em.
assert!(latin.avg_char_width > latin.upem / 2.0, "{} of {}", latin.avg_char_width, latin.upem);
assert_eq!(asian.avg_char_width, asian.upem / 2.0);
}
/// #848: browsing the font list drew each family's name in its own face, loading every family
/// shown for good (tens of megabytes per trip through the Font box). Preview faces are read for
/// the preview alone: however many families are previewed, the database keeps no font data, and
+1
View File
@@ -8,6 +8,7 @@
#![forbid(unsafe_code)]
pub mod color;
pub mod download;
mod fontdb;
pub mod math;
mod script;
+6 -1
View File
@@ -8,6 +8,11 @@ description = "WordCraft import/export: plain text, Markdown, HTML, RTF, OpenDoc
[lints]
workspace = true
[features]
# `sheet::xls_fixture`, the binary workbook builder other crates' tests share; turned on only as a
# dev-dependency, so never built into the app.
test-support = ["craft-xls/test-support"]
[dependencies]
wordcraft-doc = { workspace = true }
wordcraft-docx = { workspace = true }
@@ -28,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).
@@ -36,3 +40,4 @@ chrono = { workspace = true }
wordcraft-doc = { workspace = true, features = ["test-support"] }
craft-image-codecs = { workspace = true }
proptest = { workspace = true }
chardetng = { workspace = true }
+3 -2
View File
@@ -109,9 +109,10 @@ pub fn read_rows(bytes: &[u8], pick: Pick<'_>) -> Result<(Vec<String>, Vec<Vec<S
Err("not an Excel or OpenDocument spreadsheet".into())
}
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn xls_fixture(sheets: &[(&str, Vec<Vec<String>>)]) -> Vec<u8> {
craft_xls::testing::workbook(sheets)
pub fn xls_fixture(sheets: &[(&str, Vec<Vec<String>>)]) -> Result<Vec<u8>, String> {
craft_xls::testing::workbook(sheets).map_err(|e| e.to_string())
}
pub fn field_names(row: &[String]) -> Vec<String> {
+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();
+2 -2
View File
@@ -276,7 +276,7 @@ impl Ctx<'_> {
}
/// `p` laid out on its own, as the paragraph after a kept one is measured before it is placed
/// (no wrap areas, proofing, typed word or resized text boxes), through the cache when
/// (no wrap areas, typed word or resized text boxes), through the cache when
/// [`Ctx::para_labelled`] would lay it out with those same inputs: it is then not laid out a
/// second time when it is placed. Counts nothing (list labels, equation numbers, page totals).
/// `None` when the inputs differ.
@@ -285,7 +285,7 @@ impl Ctx<'_> {
.objects
.iter()
.any(|o| matches!(o, InlineObject::Shape { story: Some(_), float, .. } if float.wrap == Wrap::Inline && float.rot == 0.0));
if self.opts.proofing || self.boxes.inside() || resized || self.hide_deleted != self.opts.hides_deleted(StoryRef::Body) {
if self.boxes.inside() || resized || self.hide_deleted != self.opts.hides_deleted(StoryRef::Body) {
return None;
}
let kept = (self.eq_count, self.uses_totals);
+13 -1
View File
@@ -35,6 +35,18 @@ pub(super) fn graphic_draws(doc: &Document, g: &Graphic, rect: Rect, alpha: f32)
cap: Default::default(),
})
}
GraphicItem::Text { rect: r, angle, .. } if angle.is_finite() && angle.rem_euclid(360.0) != 0.0 => {
// Turned text is laid out level in its box turned the same way, then turned about
// the box's center.
let at = inside(r);
let (cx, cy) = (at.x + at.w / 2.0, at.y + at.h / 2.0);
let quarter = ((angle / 90.0).round() as i64).rem_euclid(2) == 1;
let level = if quarter { Rect::new(cx - at.h / 2.0, cy - at.w / 2.0, at.h, at.w) } else { at };
let items = text_glyphs(doc, level, it, alpha, k);
if !items.is_empty() {
out.push(Draw::Rotated { cx, cy, spin: Spin::new(*angle, false, false), items });
}
}
GraphicItem::Text { rect: r, .. } => out.extend(text_glyphs(doc, inside(r), it, alpha, k)),
GraphicItem::Image { rect: r, media } => out.push(Draw::Image {
rect: inside(r),
@@ -280,7 +292,7 @@ pub fn seg_path(segs: &[PathSeg]) -> BezPath {
/// or the item's rectangle or size isn't a finite number (a degenerate scale: `r` itself is always
/// finite, as [`Rect::new`] makes it).
fn text_glyphs(doc: &Document, r: Rect, it: &GraphicItem, alpha: f32, k: f32) -> Vec<Draw> {
let GraphicItem::Text { rect, text, size, color, bold, align, font } = it else { return Vec::new() };
let GraphicItem::Text { rect, text, size, color, bold, align, font, .. } = it else { return Vec::new() };
let size = *size * k;
if !rect.iter().chain([&size]).all(|v| v.is_finite()) {
return Vec::new();
+39 -13
View File
@@ -1,5 +1,5 @@
use super::builder::Builder;
use super::model::ClKind;
use super::model::{ClKind, StyleRun};
use super::{Arc, CharProps, Document, WrapText};
/// The section's document grid as layout applies it (`w:docGrid`, ECMA-376 §17.6.5).
@@ -73,19 +73,40 @@ pub(super) fn snap_chars(b: &mut Builder, text: &str, grid: &Grid) {
}
}
/// Automatic spacing (`w:autoSpaceDE`, `w:autoSpaceDN`): a quarter of the Asian character's font
/// size between Asian text and Latin text or digits that touch, across runs. Measured on Word's
/// PDF of a Chinese document at 16 pt (#2358), as JIS X 4051 asks. A field or note number is
/// classed by the text it shows. The room goes after the first of the two characters; returns it
/// by cluster index.
/// The room automatic spacing leaves after a Latin character drawn in `st`: half its face's
/// average character width, the figure a platform's text metrics report. Times New Roman gives
/// 0.199 of the font size, Calibri 0.258, Cambria 0.304 (see [`auto_space`]). A face that is
/// itself East Asian averages its half-width cell, so a digit set in SimSun gives a quarter of
/// the size like the East Asian text around it.
fn auto_space_room(st: &StyleRun) -> f32 {
let ratio = if st.face.upem > 0.0 { st.face.avg_char_width / st.face.upem } else { 0.5 };
let ratio = if ratio.is_finite() { ratio.clamp(0.0, 1.0) as f32 } else { 0.5 };
if st.size.is_finite() { (st.size * ratio / 2.0).clamp(0.0, 400.0) } else { 0.0 }
}
/// Automatic spacing (`w:autoSpaceDE`, `w:autoSpaceDN`): room between Asian text and Latin text or
/// digits that touch, across runs. The room goes after the first of the two characters and takes
/// that character's own size: a quarter of it in front of a Latin run, half the Latin face's
/// average character width after it ([`auto_space_room`]). A field or note number is classed by
/// the text it shows. Returns the room by cluster index.
///
/// The quarter in front is Word's PDF of a Chinese document at 16 pt (#2358), as JIS X 4051 asks.
/// The room after a Latin run was measured in Word 16.0.17932.21056 over 26 faces, both orders,
/// 10.5 to 400 pt, Japanese and Chinese (#6583), through
/// `Range.Information(wdHorizontalPositionRelativeToPage)` rather than a PDF, since
/// `ExportAsFixedFormat` hung: the room in front of a Latin run is a quarter of the font size
/// whatever face the Latin text uses, while the room after it follows that face — 0.1986 of the
/// size for Times New Roman, 0.2203 for Arial, 0.2575 for Calibri, 0.2980 for Courier New,
/// 0.3041 for Cambria — which is half each face's average character width. With different sizes
/// either side, the room after the Latin run takes the Latin size, not the Asian one.
pub(super) fn auto_space(b: &mut Builder, text: &str, de: bool, dn: bool) -> Vec<(usize, f32)> {
let mut out = Vec::new();
if !de && !dn {
return out;
}
let mut shown = b.shown.iter().map(|s| (s.0, s.1.as_str())).peekable();
// The previous text cluster: index, its last character, its font size.
let mut prev: Option<(usize, char, f32)> = None;
// The previous text cluster: index and its last character.
let mut prev: Option<(usize, char)> = None;
for k in 0..b.clusters.len() {
let Some(c) = b.clusters.get(k) else { break };
match c.kind {
@@ -105,18 +126,23 @@ pub(super) fn auto_space(b: &mut Builder, text: &str, de: bool, dn: bool) -> Vec
prev = None;
continue;
};
let size = b.styles.get(c.style as usize).map_or(0.0, |st| st.size);
if let Some((pk, pc, psize)) = prev
if let Some((pk, pc)) = prev
&& let Some(side) = crate::kinsoku::auto_space(pc, first, de, dn)
{
let asian = if side == crate::kinsoku::AsianSide::Left { psize } else { size };
let gap = if asian.is_finite() { (asian / 4.0).clamp(0.0, 400.0) } else { 0.0 };
let style = b.clusters.get(pk).map(|p| p.style as usize);
let st = style.and_then(|i| b.styles.get(i));
let gap = match side {
// In front of the Latin run the Asian character carries the room (#2358).
crate::kinsoku::AsianSide::Left => st.map_or(0.0, |st| if st.size.is_finite() { (st.size / 4.0).clamp(0.0, 400.0) } else { 0.0 }),
// After it the Latin character carries the room, so its own face decides (#6583).
crate::kinsoku::AsianSide::Right => st.map_or(0.0, auto_space_room),
};
if let Some(pc) = b.clusters.get_mut(pk) {
pc.adv += gap;
out.push((pk, gap));
}
}
prev = Some((k, last, size));
prev = Some((k, last));
}
out
}
+52 -3
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
@@ -3035,8 +3039,8 @@ fn cache_reuses_unchanged_paragraphs() {
}
/// The paragraph after a heading kept with it is measured before the heading is placed. That
/// layout goes into the cache under the key it is placed with, so it is laid out once, and the
/// pages are those of the uncached measurement (proofing on measures without the cache).
/// layout goes into the cache under the key it is placed with, so it is laid out once, with
/// proofing off or on, and proofing moves nothing.
#[test]
fn kept_headings_measure_the_next_paragraph_through_the_cache() {
let mut d = Document::new();
@@ -3053,7 +3057,10 @@ fn kept_headings_measure_the_next_paragraph_through_the_cache() {
assert!(l.pages.len() > 2, "{}", l.pages.len());
assert_eq!(c.len(), d.body.len(), "one layout per paragraph");
assert_eq!(c.misses, d.body.len() as u64, "each paragraph laid out once");
let proofed = layout(&d, &mut LayoutCache::new(), &LayoutOptions { proofing: true, ..Default::default() });
let mut c = LayoutCache::new();
let proofed = layout(&d, &mut c, &LayoutOptions { proofing: true, ..Default::default() });
assert_eq!(c.misses, d.body.len() as u64, "each paragraph laid out once with proofing on");
assert!(c.hits >= 40, "each kept heading should reuse its following paragraph layout with proofing on; got {} cache hits", c.hits);
let starts = |l: &DocLayout| (0..d.body.len()).map(|i| l.caret(&Pos::body(i, 0)).map(|c| (c.page, c.top.to_bits()))).collect::<Vec<_>>();
assert_eq!(starts(&l), starts(&proofed));
}
@@ -8208,6 +8215,7 @@ fn graphic_items_draw_inside_the_object() {
bold: false,
align: TextAlign::Center,
font: None,
angle: 0.0,
},
];
let mut d = Document::from_text("Chart");
@@ -8238,6 +8246,7 @@ fn graphic_text_in_a_degenerate_rectangle_is_not_drawn() {
bold: false,
align: TextAlign::Center,
font: None,
angle: 0.0,
};
let items = vec![text([f32::NAN, 0.0, 10.0, 10.0], 10.0), text([0.0, 0.0, f32::INFINITY, 10.0], 10.0), text([0.0, 0.0, 10.0, 10.0], f32::NAN)];
let graphic = Graphic { kind: GraphicKind::Diagram, items, ..Default::default() };
@@ -8248,6 +8257,45 @@ fn graphic_text_in_a_degenerate_rectangle_is_not_drawn() {
assert!(!draws.iter().any(|x| matches!(x, display::Draw::Glyphs { text, .. } if text == "Q1")));
}
/// A chart's vertical axis title (text turned -90°) is laid out along its tall box and drawn
/// turned about the box's center.
#[test]
fn turned_graphic_text_is_drawn_turned_about_its_box() {
crate::test_fonts();
use wordcraft_doc::graphic::{Graphic, GraphicItem, GraphicKind, TextAlign};
use wordcraft_doc::props::Rgb;
let items = vec![GraphicItem::Text {
rect: [0.0, 20.0, 20.0, 100.0],
text: "Sales".into(),
size: 10.0,
color: Rgb::BLACK,
bold: false,
align: TextAlign::Center,
font: None,
angle: -90.0,
}];
let graphic = Graphic { kind: GraphicKind::Chart, items, ..Default::default() };
let obj = InlineObject::Graphic { w: 216.0, h: 144.0, alt: String::new(), float: Default::default(), graphic: Arc::new(graphic) };
let mut d = Document::from_text("Chart");
d.insert_object(&Pos::body(0, 0), obj, &Default::default()).unwrap();
let draws = flat(display::page_display(&d, &lay(&d).pages[0], &Default::default()));
let turned = draws
.iter()
.find_map(|x| match x {
display::Draw::Rotated { cx, cy, spin, items } => Some((*cx, *cy, *spin, items)),
_ => None,
})
.expect("the title is turned");
let (cx, cy, spin, items) = turned;
assert_eq!(spin.deg, 270.0);
let Some(display::Draw::Glyphs { glyphs, text, .. }) = items.first() else { panic!("{items:?}") };
assert_eq!(text, "Sales");
// Laid out level across the box's 100 pt height, centered on the box's center.
let (first, last) = (glyphs[0].1, glyphs[glyphs.len() - 1].1);
assert!(first > cx - 50.0 && last < cx + 50.0 && first < cx && last > cx, "{first} {last} about {cx}");
assert!((glyphs[0].2 - cy).abs() < 10.0, "baseline {} near {cy}", glyphs[0].2);
}
#[test]
fn chart_text_the_font_lacks_is_drawn_in_a_fallback_face() {
crate::test_fonts();
@@ -8268,6 +8316,7 @@ fn chart_text_the_font_lacks_is_drawn_in_a_fallback_face() {
bold: false,
align: TextAlign::Center,
font: None,
angle: 0.0,
}];
let graphic = Graphic { kind: GraphicKind::Chart, items, ..Default::default() };
let obj = InlineObject::Graphic { w: 216.0, h: 144.0, alt: String::new(), float: Default::default(), graphic: Arc::new(graphic) };
+74 -26
View File
@@ -1,7 +1,20 @@
//! Automatic space between Asian text and Latin text or digits (`w:autoSpaceDE`, `w:autoSpaceDN`,
//! #2358). Word's PDF of the issue's file shows about a quarter of the font size between 院 and 94
//! at 16 pt. Assertions compare a layout with the flags on to the same layout with them off, never
//! font metrics (fonts differ between machines; CJK glyphs may be missing).
//! Automatic space between Asian text and Latin text or digits (`w:autoSpaceDE`, `w:autoSpaceDN`).
//! The room goes after the first of the two characters and takes that character's own size.
//!
//! - **In front of a Latin run** it is a quarter of the font size: Word's PDF of the #2358 file
//! shows about that between 院 and 94 at 16 pt, as JIS X 4051 asks.
//! - **After a Latin run** it is half the Latin face's average character width — 0.1986 of the
//! font size for Times New Roman, 0.2203 for Arial, 0.2575 for Calibri, 0.2980 for Courier New,
//! 0.3041 for Cambria. Measured in Word 16.0.17932.21056 over 26 faces, both orders, 10.5 to
//! 400 pt, Japanese and Chinese (#6583), from
//! `Range.Information(wdHorizontalPositionRelativeToPage)` rather than a PDF, because
//! `ExportAsFixedFormat` hung on the machine it was measured on. The room in front stayed a
//! quarter of the size whatever face the Latin text used, and the room after a Latin run of
//! several characters followed the last of them.
//!
//! Assertions compare a layout with the flags on to the same layout with them off, and read the
//! average character width from the face the layout actually drew with rather than naming a
//! figure (fonts differ between machines; CJK glyphs may be missing).
use crate::para::ClKind;
use crate::*;
@@ -41,39 +54,74 @@ fn gaps(d: &Document) -> Vec<f32> {
panic!("the layouts with and without the automatic space draw with other faces");
}
/// The default font size of `d`'s first paragraph, a quarter of it.
/// The default font size of `d`'s first paragraph, a quarter of it: the room in front of a Latin
/// run (#2358).
fn quarter(d: &Document) -> f32 {
let pl = first_para(&lay(d));
let size = pl.clusters.first().and_then(|c| pl.styles.get(c.style as usize)).unwrap().size;
(size / 4.0 * 100.0).round() / 100.0
}
#[test]
fn digits_and_latin_beside_asian_text_get_a_quarter_of_the_font_size() {
crate::test_fonts();
let mut d = Document::from_text("法院94年A字");
let q = quarter(&d);
assert!(q > 0.0);
// 院|9 and 4|年 (DN), 年|A and A|字 (DE): the space follows the first of the two.
assert_eq!(gaps(&d), [0.0, q, 0.0, q, q, q, 0.0]);
para0(&mut d).props.auto_space_dn = Some(false);
assert_eq!(gaps(&d), [0.0, 0.0, 0.0, 0.0, q, q, 0.0], "numbers off");
let p = para0(&mut d);
p.props.auto_space_dn = None;
p.props.auto_space_de = Some(false);
assert_eq!(gaps(&d), [0.0, q, 0.0, q, 0.0, 0.0, 0.0], "Latin off");
/// Half the average character width of the face that draws the cluster starting at byte `at`:
/// the room after a Latin character (#6583).
fn after(d: &Document, at: usize) -> f32 {
let pl = first_para(&lay(d));
let c = pl.clusters.iter().find(|c| c.start == at).unwrap();
let st = pl.styles.get(c.style as usize).unwrap();
let room = st.size * (st.face.avg_char_width / st.face.upem) as f32 / 2.0;
(room * 100.0).round() / 100.0
}
#[test]
fn the_space_crosses_runs_and_takes_the_asian_size() {
fn digits_and_latin_beside_asian_text_get_a_quarter_in_front_and_the_latin_average_after() {
crate::test_fonts();
let mut d = Document::from_text("法院94年A字");
let q = quarter(&d);
// 4 is at byte 7, A at byte 11: the room after each follows the face that draws it.
let (a4, aa) = (after(&d, 7), after(&d, 11));
assert!(q > 0.0 && a4 > 0.0 && aa > 0.0);
assert_ne!(a4, q, "the test face must not average half its em, or this proves nothing");
// 院|9 and 4|年 (DN), 年|A and A|字 (DE): the room follows the first of the two.
assert_eq!(gaps(&d), [0.0, q, 0.0, a4, q, aa, 0.0]);
para0(&mut d).props.auto_space_dn = Some(false);
assert_eq!(gaps(&d), [0.0, 0.0, 0.0, 0.0, q, aa, 0.0], "numbers off");
let p = para0(&mut d);
p.props.auto_space_dn = None;
p.props.auto_space_de = Some(false);
assert_eq!(gaps(&d), [0.0, q, 0.0, a4, 0.0, 0.0, 0.0], "Latin off");
}
#[test]
fn the_space_crosses_runs_and_takes_the_size_of_the_character_before_it() {
crate::test_fonts();
let mut d = Document::from_text("漢A漢");
let size = |s: f32| move |c: &mut CharProps| c.size = Some(s);
let p = para0(&mut d);
p.format(0, 3, &size(20.0)).unwrap();
p.format(3, 4, &size(10.0)).unwrap();
p.format(3, 4, &size(12.0)).unwrap();
p.format(4, 7, &size(20.0)).unwrap();
assert_eq!(gaps(&d), [5.0, 5.0, 0.0]);
// 漢|A takes a quarter of the 20 pt 漢. A|漢 takes the 12 pt A, not the 20 pt 漢: in Word a
// 100 pt digit before a 400 pt ideograph moved it 19.85 pt and a 400 pt digit before a
// 100 pt one moved it 80.65 pt, both the Latin figure at the Latin size (#6583).
let a = after(&d, 3);
assert!(a > 0.0 && a < 5.0);
assert_eq!(gaps(&d), [5.0, a, 0.0]);
}
#[test]
fn the_room_after_a_latin_run_follows_the_latin_face() {
crate::test_fonts();
// Two faces whose average character widths differ: the room after the digit differs with
// them, while the room in front of it stays a quarter of the size.
let mut rooms = Vec::new();
for family in ["Source Sans 3", "JetBrains Mono"] {
let mut d = Document::from_text("漢9漢");
para0(&mut d).format(3, 4, &|c: &mut CharProps| c.font = Some(family.to_string())).unwrap();
let (q, a) = (quarter(&d), after(&d, 3));
assert_eq!(gaps(&d), [q, a, 0.0], "{family}");
rooms.push(a);
}
assert_ne!(rooms[0], rooms[1], "the two faces must differ in average character width");
}
#[test]
@@ -97,8 +145,9 @@ fn a_field_result_is_classed_by_the_text_it_shows() {
assert!(pl.shown.iter().any(|s| s.0 == at && s.1 == "12"), "the field shows its result: {:?}", pl.shown);
let q = quarter(&d);
assert_eq!(pl.clusters[at].kind, ClKind::Text);
// 漢|1 and 2|漢: the field's first and last characters, not the object it stands for.
assert_eq!(gaps(&d), [q, q, 0.0]);
// 漢|1 and 2|漢: the field's first and last characters, not the object it stands for. The
// room after the field follows the face it draws its result in.
assert_eq!(gaps(&d), [q, after(&d, 3), 0.0]);
}
#[test]
@@ -114,8 +163,7 @@ fn a_character_grid_the_paragraph_snaps_to_turns_it_off() {
assert!(gaps(&d).iter().all(|g| *g == 0.0), "{:?}", gaps(&d));
// A line grid alone, or a paragraph that doesn't snap, keeps it.
for d in [with(DocGridType::Lines, true), with(DocGridType::LinesAndChars, false)] {
let q = quarter(&d);
assert_eq!(gaps(&d), [0.0, q, 0.0, q, 0.0]);
assert_eq!(gaps(&d), [0.0, quarter(&d), 0.0, after(&d, 7), 0.0]);
}
}
@@ -51,6 +51,7 @@ fn chart_with(d: &mut Document, label: &str) {
bold: false,
align: TextAlign::Center,
font: None,
angle: 0.0,
}];
let graphic = Graphic { kind: GraphicKind::Chart, items, ..Default::default() };
let obj = InlineObject::Graphic { w: 216.0, h: 144.0, alt: String::new(), float: Default::default(), graphic: Arc::new(graphic) };
+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
),
+5
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
@@ -982,6 +983,7 @@ fn inline_chart_is_a_figure_with_its_text_inside_it() {
bold: false,
align: TextAlign::Center,
font: None,
angle: 0.0,
}];
let graphic = Graphic { kind: GraphicKind::Chart, items, ..Default::default() };
let obj = InlineObject::Graphic { w: 216.0, h: 144.0, alt: "Sales by quarter".into(), float: Default::default(), graphic: Arc::new(graphic) };
@@ -1916,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();
@@ -3120,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<_>>()
@@ -3216,6 +3220,7 @@ fn chart_and_equation_text_keep_their_pdf_bytes() {
bold: false,
align: TextAlign::Center,
font: None,
angle: 0.0,
}];
let graphic = Graphic { kind: GraphicKind::Chart, items, ..Default::default() };
let obj = InlineObject::Graphic { w: 216.0, h: 144.0, alt: String::new(), float: Default::default(), graphic: Arc::new(graphic) };
+3 -3
View File
@@ -2009,7 +2009,7 @@ fn undecodable_pictures_keep_their_grey_box_pixels() {
fn geometry_bytes_counts_pattern_tiles_and_clips_once() {
use craft_metafile::{ClipPath, Tile};
let segs = |n: usize| vec![Seg::Line(0.0, 0.0); n];
let pic = |items: Vec<Item>| Picture { width: 10.0, height: 10.0, items, skipped_records: 0 };
let pic = |items: Vec<Item>| Picture { width: 10.0, height: 10.0, items, ..Default::default() };
let base = crate::media::geometry_bytes(&pic(vec![]));
let tile = Arc::new(Tile { width: 1000, height: 1000, rgba: vec![0; 4_000_000], cell: [1.0, 1.0], origin: [0.0, 0.0] });
let pattern = |tile: &Arc<Tile>| Item::Pattern { segs: segs(1000), even_odd: false, tile: tile.clone(), region: None, clips: vec![] };
@@ -2042,7 +2042,7 @@ fn geometry_bytes_counts_pattern_tiles_and_clips_once() {
#[test]
fn geometry_bytes_charges_the_capacity_of_segment_and_dash_vectors() {
use craft_metafile::LineStyle;
let pic = |items: Vec<Item>| Picture { width: 10.0, height: 10.0, items, skipped_records: 0 };
let pic = |items: Vec<Item>| Picture { width: 10.0, height: 10.0, items, ..Default::default() };
let path = |segs: Vec<Seg>, dash: Vec<f32>| Item::Path {
segs,
fill: None,
@@ -2066,7 +2066,7 @@ fn geometry_bytes_charges_the_capacity_of_segment_and_dash_vectors() {
#[test]
fn geometry_bytes_counts_dash_arrays_and_text_records() {
use craft_metafile::{Font, HAlign, LineStyle, Text, VAlign};
let pic = |items: Vec<Item>| Picture { width: 10.0, height: 10.0, items, skipped_records: 0 };
let pic = |items: Vec<Item>| Picture { width: 10.0, height: 10.0, items, ..Default::default() };
let path = |dash: usize| Item::Path {
segs: vec![Seg::Line(0.0, 0.0); 2],
fill: None,
+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 = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
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 = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
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 = "92d392cff132eaf53e13afff77f38b5c11b73aa0", 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;
+68 -48
View File
@@ -130,59 +130,79 @@ pub fn show(app: &mut WordApp, ui: &mut Ui) {
app.canvas.want_focus = true;
}
ui.add_space(if mac { 12.0 } else { 6.0 });
for (id, label, icon) in PAGES {
// No document open (#1129): Info, Save, Save As, Print and Export are greyed out.
let on = has_document || works_without_document(id);
let (r, resp) = ui.add_enabled_ui(on, |ui| ui.allocate_exact_size(vec2(200.0, 38.0), Sense::click())).inner;
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, tl!(label)));
let active = app.ui.backstage_page == id;
if active {
ui.painter().rect_filled(r, 0.0, egui::Color32::from_white_alpha(46));
} else if on && resp.hovered() {
ui.painter().rect_filled(r, 0.0, egui::Color32::from_white_alpha(26));
}
let ink = if on { egui::Color32::WHITE } else { egui::Color32::from_white_alpha(110) };
icons::paint(
ui.painter(),
Rect::from_center_size(pos2(crate::rtl::lead_x(rtl, r, 31.0), r.center().y), vec2(18.0, 18.0)),
icon,
ink,
ink,
);
ui.painter().text(
pos2(crate::rtl::lead_x(rtl, r, 52.0), r.center().y),
crate::rtl::lead_center(rtl),
tl!(label),
if active { semibold(14.0) } else { medium(14.0) },
ink,
);
if on
&& app.ui.keytips == crate::keytips::Phase::Backstage
&& let Some(letter) = keytip_letter(id)
{
crate::keytips::badge(ui, Rect::from_min_size(pos2(r.min.x + 16.0, r.min.y), vec2(20.0, 16.0)), letter);
}
if resp.clicked() {
go_to_page(app, id);
}
}
ui.with_layout(egui::Layout::bottom_up(egui::Align::Min), |ui| {
for (label, id) in bottom_links(app) {
let (r, resp) = ui.allocate_exact_size(vec2(200.0, 32.0), Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), tl!(label)));
if resp.hovered() {
// A window too short for the whole menu scrolls it below the back button (#6639).
// When the menu fits, About and the community link stay at the bottom.
let links = bottom_links(app);
let spacing = ui.spacing().item_spacing.y;
let links_h = (32.0 + spacing) * links.len() as f32 - spacing;
let fits = ui.available_height();
egui::ScrollArea::vertical().id_salt("backstage_nav_scroll").auto_shrink([false, false]).show(ui, |ui| {
let top = ui.cursor().top();
for (id, label, icon) in PAGES {
// No document open (#1129): Info, Save, Save As, Print and Export are greyed out.
let on = has_document || works_without_document(id);
let (r, resp) = ui.add_enabled_ui(on, |ui| ui.allocate_exact_size(vec2(200.0, 38.0), Sense::click())).inner;
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, tl!(label)));
// A row reached with Tab scrolls into view.
if resp.gained_focus() {
resp.scroll_to_me(None);
}
let active = app.ui.backstage_page == id;
if active {
ui.painter().rect_filled(r, 0.0, egui::Color32::from_white_alpha(46));
} else if on && resp.hovered() {
ui.painter().rect_filled(r, 0.0, egui::Color32::from_white_alpha(26));
}
let at = pos2(crate::rtl::lead_x(rtl, r, 22.0), r.center().y);
ui.painter().text(at, crate::rtl::lead_center(rtl), tl!(label), regular(13.0), egui::Color32::WHITE);
let ink = if on { egui::Color32::WHITE } else { egui::Color32::from_white_alpha(110) };
icons::paint(
ui.painter(),
Rect::from_center_size(pos2(crate::rtl::lead_x(rtl, r, 31.0), r.center().y), vec2(18.0, 18.0)),
icon,
ink,
ink,
);
ui.painter().text(
pos2(crate::rtl::lead_x(rtl, r, 52.0), r.center().y),
crate::rtl::lead_center(rtl),
tl!(label),
if active { semibold(14.0) } else { medium(14.0) },
ink,
);
if on
&& app.ui.keytips == crate::keytips::Phase::Backstage
&& let Some(letter) = keytip_letter(id)
{
crate::keytips::badge(ui, Rect::from_min_size(pos2(r.min.x + 16.0, r.min.y), vec2(20.0, 16.0)), letter);
}
if resp.clicked() {
if id == "discord" {
let _ = app.run("ui.discord", json!({}));
} else {
app.dialog = crate::dialogs::Dialog::open("about", app);
}
go_to_page(app, id);
}
}
let gap = top + fits - ui.cursor().top() - links_h;
if gap > 0.0 {
ui.add_space(gap);
}
ui.allocate_ui_with_layout(vec2(200.0, links_h), egui::Layout::bottom_up(egui::Align::Min), |ui| {
for (label, id) in links {
let (r, resp) = ui.allocate_exact_size(vec2(200.0, 32.0), Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), tl!(label)));
if resp.gained_focus() {
resp.scroll_to_me(None);
}
if resp.hovered() {
ui.painter().rect_filled(r, 0.0, egui::Color32::from_white_alpha(26));
}
let at = pos2(crate::rtl::lead_x(rtl, r, 22.0), r.center().y);
ui.painter().text(at, crate::rtl::lead_center(rtl), tl!(label), regular(13.0), egui::Color32::WHITE);
if resp.clicked() {
if id == "discord" {
let _ = app.run("ui.discord", json!({}));
} else {
app.dialog = crate::dialogs::Dialog::open("about", app);
}
}
}
});
});
});
egui::CentralPanel::default().frame(egui::Frame::NONE.fill(t.ribbon).inner_margin(egui::Margin::symmetric(0, 30))).show(ui, |ui| {
+119
View File
@@ -1511,6 +1511,125 @@ fn the_file_page_scrolls_with_the_wheel_at_the_window_edge() {
assert!((bar.right() - 1432.0).abs() < 1.0, "expected the scrollbar at the window edge: {bar:?}");
}
/// The File page in a window `height` points tall, left to right or, with `rtl`, in a right-to-left
/// interface (a fixture catalog, so the test does not need the translations). With `mac`, the
/// File page covers the integrated macOS title bar.
fn file_page_harness(rtl: bool, mac: bool, height: f32) -> egui_kittest::Harness<'static, WordApp> {
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Services::default());
a.integrated_titlebar = mac;
a.mac_titlebar = crate::chrome::MacTitleBar { height: 28.0, content_left: 70.0 };
a.run("ui.backstage", json!({"value": true, "page": "home"})).unwrap();
let ar = crate::i18n::fixture_lang("ar", "\tHome\tHome\n");
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, height)).build_ui_state(
move |ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
// Draw the File page in a fixture language even without embedded catalogs.
crate::i18n::set_current(if rtl { ar } else { crate::i18n::Lang::EN });
crate::rtl::sync(&ctx);
assert_eq!(crate::rtl::on(), rtl);
super::show(app, ui);
crate::i18n::set_current(crate::i18n::Lang::EN);
},
a,
);
h.run_steps(4);
h
}
/// The rectangle of the side menu's button named `label`, a 200 point wide row (the page itself
/// may have a control with the same name).
fn side_menu_button(h: &egui_kittest::Harness<'static, WordApp>, label: &str) -> egui::Rect {
let label = crate::i18n::t(label).to_string();
h.query_all_by_label(&label)
.map(|n| n.rect())
.find(|r| (r.width() - 200.0).abs() < 0.5)
.unwrap_or_else(|| panic!("the side menu's {label} button"))
}
/// #6639: in a window too short for the File page's side menu, the wheel over the menu scrolls
/// it, so Options and About can be reached. Checked left to right and right to left, where the
/// menu is on the right.
#[test]
fn the_side_menu_scrolls_with_the_wheel_in_a_short_window() {
// Both directions run before the test fails, so each one's result is reported.
let mut misses = Vec::new();
for (rtl, x) in [(false, 100.0), (true, 1340.0)] {
let language = if rtl { "right to left" } else { "left to right" };
let height = 360.0;
// The harness keeps an 8 point margin, so the window's bottom edge is at y=352.
let bottom = height - 8.0;
let mut h = file_page_harness(rtl, false, height);
let menu = side_menu_button(&h, "Home");
assert!(menu.min.x <= x && x <= menu.max.x, "{language}: the menu is under the pointer: {menu:?}");
for label in ["Options", "About"] {
let r = side_menu_button(&h, label);
assert!(r.max.y > bottom, "{language}: {label} starts below the window's bottom edge: {r:?}");
}
h.event(egui::Event::PointerMoved(egui::pos2(x, 200.0)));
h.event(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: egui::vec2(0.0, -2000.0),
phase: egui::TouchPhase::Move,
modifiers: egui::Modifiers::NONE,
source: egui::MouseWheelSource::Unknown,
});
h.run_steps(4);
for label in ["Options", "About"] {
let r = side_menu_button(&h, label);
if r.max.y > bottom + 0.5 {
misses.push(format!("{language}: the wheel did not bring {label} into the window: {r:?}"));
}
}
assert!(h.state().ui.backstage, "{language}: File stays open");
}
assert!(misses.is_empty(), "{misses:#?}");
}
/// #6639: in a window too short for the side menu, Tab to About scrolls the menu so that About
/// is in the window, left to right and right to left.
#[test]
fn tab_to_about_scrolls_it_into_a_short_window() {
let mut misses = Vec::new();
for rtl in [false, true] {
let language = if rtl { "right to left" } else { "left to right" };
let bottom = 360.0 - 8.0;
let mut h = file_page_harness(rtl, false, 360.0);
let about_focused = |h: &egui_kittest::Harness<'static, WordApp>| {
h.query_all_by_label("About").find(|n| (n.rect().width() - 200.0).abs() < 0.5 && n.is_focused()).map(|n| n.rect())
};
let mut about = None;
for _ in 0..40 {
h.key_press(egui::Key::Tab);
h.run_steps(4);
about = about_focused(&h);
if about.is_some() {
break;
}
}
let Some(r) = about else {
panic!("{language}: Tab reaches the side menu's About button");
};
if r.max.y > bottom + 0.5 {
misses.push(format!("{language}: About has the focus below the window's bottom edge: {r:?}"));
}
}
assert!(misses.is_empty(), "{misses:#?}");
}
/// #6639: when the whole side menu fits, About stays at the menu's bottom, above its 12 point
/// margin, as it was before the menu could scroll, with and without the macOS title bar.
#[test]
fn about_stays_at_the_bottom_of_a_side_menu_that_fits() {
for (rtl, mac) in [(false, false), (true, false), (false, true), (true, true)] {
let language = if rtl { "right to left" } else { "left to right" };
let h = file_page_harness(rtl, mac, 900.0);
let about = side_menu_button(&h, "About");
let bottom = 900.0 - 8.0 - 12.0;
assert!((about.max.y - bottom).abs() < 0.5, "{language}, mac {mac}: About sits at the bottom of the menu: {about:?}");
}
}
/// Recursively collects bounding rectangles and text entries from egui shapes.
fn collect_shapes(shape: &egui::Shape, rects: &mut Vec<egui::Rect>, texts: &mut Vec<(String, egui::Pos2, f32)>) {
match shape {
+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)))

Some files were not shown because too many files have changed in this diff Show More