mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-21 20:39:42 +00:00
Compare commits
502
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86352d5cf3 | ||
|
|
bc0e51ee4d | ||
|
|
ed66198574 | ||
|
|
e9750c5cb7 | ||
|
|
3d5a153e3d | ||
|
|
52d17a91df | ||
|
|
323cf78232 | ||
|
|
42a5df5f94 | ||
|
|
f8c66d0692 | ||
|
|
aba890e8a6 | ||
|
|
0cc92c9744 | ||
|
|
b02baeba43 | ||
|
|
3c77297b83 | ||
|
|
60415e055c | ||
|
|
0db15a1d5e | ||
|
|
33afe69094 | ||
|
|
669d5df0b1 | ||
|
|
d1a36fd0c6 | ||
|
|
beae4b82fa | ||
|
|
514085ca44 | ||
|
|
fbd6c7f133 | ||
|
|
a4b898f820 | ||
|
|
388c8e51ff | ||
|
|
4857eee142 | ||
|
|
513a109688 | ||
|
|
eae8363e89 | ||
|
|
953f43baed | ||
|
|
acf5eb8727 | ||
|
|
bcacf7466f | ||
|
|
1f984892ea | ||
|
|
4216089a23 | ||
|
|
0b2a83a352 | ||
|
|
c37baf764d | ||
|
|
ef3626dbb6 | ||
|
|
69ea44776d | ||
|
|
c537ed96b9 | ||
|
|
b5fa2f9ed4 | ||
|
|
89afefba6d | ||
|
|
8e4a487074 | ||
|
|
0294f81d83 | ||
|
|
2bda4e5a41 | ||
|
|
fab8793ce7 | ||
|
|
12ce548607 | ||
|
|
7539fb81cb | ||
|
|
2e185a6a74 | ||
|
|
3f7bff9204 | ||
|
|
d30a8392f9 | ||
|
|
b8f232ca2e | ||
|
|
15c5b68da6 | ||
|
|
1c3839aa16 | ||
|
|
f392c14f6b | ||
|
|
6bf96e65e4 | ||
|
|
48d9c2a46c | ||
|
|
d54fb78544 | ||
|
|
a45e37b90d | ||
|
|
aa6deaee03 | ||
|
|
869350d4c7 | ||
|
|
139aa7a51b | ||
|
|
76d5f13a03 | ||
|
|
b71f257f1b | ||
|
|
d87e6f7930 | ||
|
|
ed09a2158f | ||
|
|
11318f9226 | ||
|
|
0196647dc6 | ||
|
|
10e5bb8bce | ||
|
|
c8e64d72cc | ||
|
|
98619947d7 | ||
|
|
664b033071 | ||
|
|
1a8fd899d3 | ||
|
|
2d286fe252 | ||
|
|
ab90b7142e | ||
|
|
02f8022e57 | ||
|
|
afcb2ce46e | ||
|
|
4671e47b50 | ||
|
|
cd4e63849e | ||
|
|
a1d4495cfb | ||
|
|
07f977372d | ||
|
|
cc27f29a01 | ||
|
|
06c2ae6f9d | ||
|
|
a58b41a716 | ||
|
|
d86d2b69f8 | ||
|
|
a8db235e05 | ||
|
|
4706420e94 | ||
|
|
ae7ae89eb2 | ||
|
|
1327d20055 | ||
|
|
9dba000e86 | ||
|
|
7013411a2f | ||
|
|
411a4915a1 | ||
|
|
639b8133ea | ||
|
|
60619abe4d | ||
|
|
c99c42eaa9 | ||
|
|
ff0856570d | ||
|
|
215485dd9a | ||
|
|
98b258188d | ||
|
|
d6c68cbfbb | ||
|
|
1c3256ab65 | ||
|
|
fa43ecc4bf | ||
|
|
81fa25dda7 | ||
|
|
0c488934e0 | ||
|
|
278aae7235 | ||
|
|
51901610fc | ||
|
|
e0659e45ce | ||
|
|
ad5e1a65fc | ||
|
|
bf426c1804 | ||
|
|
b67d3b8735 | ||
|
|
e3fef2a9c3 | ||
|
|
28af39b610 | ||
|
|
060768f1ed | ||
|
|
45db128c15 | ||
|
|
118eb8af1f | ||
|
|
30c1279fd8 | ||
|
|
f936270317 | ||
|
|
0fab74aa38 | ||
|
|
aef086976b | ||
|
|
92e0e67f3a | ||
|
|
22f01131d8 | ||
|
|
860426adda | ||
|
|
7458a18468 | ||
|
|
49e777cce5 | ||
|
|
a34c94e4c1 | ||
|
|
a42c9eaab3 | ||
|
|
12869b8e02 | ||
|
|
8548b33be1 | ||
|
|
abd5a6e957 | ||
|
|
8dac5c7802 | ||
|
|
2922fe566b | ||
|
|
43f94a5dca | ||
|
|
7095a4eaf1 | ||
|
|
d40d1b2718 | ||
|
|
05f781cfda | ||
|
|
8a97427f51 | ||
|
|
92eac2aa7c | ||
|
|
29a5abf3e7 | ||
|
|
bd9e726cda | ||
|
|
15b608660f | ||
|
|
52fbc7f068 | ||
|
|
d75364067d | ||
|
|
d102b2f08b | ||
|
|
8dc3f0cd5f | ||
|
|
f1b1077327 | ||
|
|
7b8c0ba5eb | ||
|
|
0b61f8b426 | ||
|
|
0a5d5d29f8 | ||
|
|
37e891544d | ||
|
|
bce47689ad | ||
|
|
6272c9a4da | ||
|
|
172e56f12e | ||
|
|
5ad865831f | ||
|
|
b66122f718 | ||
|
|
642f5dd6bf | ||
|
|
0b7cfa86fe | ||
|
|
85eb8bb92e | ||
|
|
52d1668507 | ||
|
|
d590dc7848 | ||
|
|
4b0d399599 | ||
|
|
fda8758ec9 | ||
|
|
c5fe6c6816 | ||
|
|
438f82977a | ||
|
|
3e62a60400 | ||
|
|
72a7afd9b5 | ||
|
|
36ed94a09e | ||
|
|
0655e651fb | ||
|
|
0e3f4dd315 | ||
|
|
e01a88b0be | ||
|
|
ae257ba784 | ||
|
|
0495fbb789 | ||
|
|
e7e9bc2759 | ||
|
|
be75ee90e3 | ||
|
|
dbbc43a89a | ||
|
|
480511eaa5 | ||
|
|
aa262cf7d8 | ||
|
|
2d97c8443e | ||
|
|
d5d2b22dac | ||
|
|
2794ae44e9 | ||
|
|
5125de6f1f | ||
|
|
59030b1bb5 | ||
|
|
4050f1bf8b | ||
|
|
02e74a5d9c | ||
|
|
d5a1c0e181 | ||
|
|
69d2a2e131 | ||
|
|
661a790540 | ||
|
|
04217419b6 | ||
|
|
4afff61f10 | ||
|
|
e76293a564 | ||
|
|
e0d8312fbe | ||
|
|
523e319424 | ||
|
|
735c2a5382 | ||
|
|
7592d1440e | ||
|
|
fde39ab061 | ||
|
|
d34e341d75 | ||
|
|
535b675e8a | ||
|
|
c8618d3e05 | ||
|
|
a891d861b7 | ||
|
|
85434d2c20 | ||
|
|
833c0154aa | ||
|
|
0a3fb5828e | ||
|
|
7634553900 | ||
|
|
cd4f755dcf | ||
|
|
c4d5f70c1d | ||
|
|
de4ca29309 | ||
|
|
b8a7e2acdb | ||
|
|
f0fb31c0c9 | ||
|
|
4cd6ffa564 | ||
|
|
52934ef110 | ||
|
|
3291be0fbf | ||
|
|
5dfb3acf15 | ||
|
|
4259dd1b2e | ||
|
|
e344bb4243 | ||
|
|
4a507de3d6 | ||
|
|
5d139d0f0e | ||
|
|
d07f50dd8b | ||
|
|
87e08051bf | ||
|
|
5c0a62a736 | ||
|
|
855a0fe9bd | ||
|
|
5f1db14954 | ||
|
|
b016ecc26c | ||
|
|
625f598b64 | ||
|
|
fa791d29b2 | ||
|
|
796b39dde6 | ||
|
|
33fa77a892 | ||
|
|
efd476cbc7 | ||
|
|
d3fcf10bcc | ||
|
|
b9936609dc | ||
|
|
7152143e45 | ||
|
|
c40719f431 | ||
|
|
6d0bd73127 | ||
|
|
cd81d5da49 | ||
|
|
b7c2fe8dff | ||
|
|
586f85df96 | ||
|
|
6367400215 | ||
|
|
9655b2da50 | ||
|
|
66e169c343 | ||
|
|
1c6c7c732e | ||
|
|
bd8bc96cb5 | ||
|
|
7830333c34 | ||
|
|
e349439bb6 | ||
|
|
4c8c1f1268 | ||
|
|
caeb87c754 | ||
|
|
0f5a701171 | ||
|
|
677e610886 | ||
|
|
d2a2709e14 | ||
|
|
050b6b0da3 | ||
|
|
6b163b33dc | ||
|
|
4ba444a342 | ||
|
|
4821a13ee0 | ||
|
|
dba2b3b5e9 | ||
|
|
beab4e8e9c | ||
|
|
852e67d587 | ||
|
|
9b6e9564a2 | ||
|
|
c9090ba95a | ||
|
|
c606533853 | ||
|
|
eb100edfd6 | ||
|
|
c38dc17ba9 | ||
|
|
5997aef2f9 | ||
|
|
73356ed82a | ||
|
|
6d3b51fab1 | ||
|
|
750e26657e | ||
|
|
f7e060ac3f | ||
|
|
b61aee61ba | ||
|
|
bc0f691c13 | ||
|
|
cac121fd89 | ||
|
|
ea8c45a4ca | ||
|
|
4774749a05 | ||
|
|
20a10a5f33 | ||
|
|
e53cae3482 | ||
|
|
7855a2d814 | ||
|
|
871401509c | ||
|
|
4ce92e8afd | ||
|
|
746577406c | ||
|
|
0131d5c3aa | ||
|
|
fa59ed0e8a | ||
|
|
b29f3a8a2e | ||
|
|
e7668ae53e | ||
|
|
7aea5f8cab | ||
|
|
3b6547de8f | ||
|
|
e0dd821d4c | ||
|
|
e4310ec5c7 | ||
|
|
7c3a5ccd34 | ||
|
|
ecbabc327c | ||
|
|
34b4caf317 | ||
|
|
8ab285e61d | ||
|
|
490437fa87 | ||
|
|
fa153b932c | ||
|
|
4925e06391 | ||
|
|
5c2e5ce0d0 | ||
|
|
274d40fc4d | ||
|
|
570c39e287 | ||
|
|
595cddaaef | ||
|
|
f1b5f0b4de | ||
|
|
ddb1d37de3 | ||
|
|
d98a335ae8 | ||
|
|
bbc6d94dbe | ||
|
|
d0498254a2 | ||
|
|
9fd9ce54b7 | ||
|
|
d94bbc8fe0 | ||
|
|
da914ce674 | ||
|
|
18a26b41ce | ||
|
|
9f9190f0d7 | ||
|
|
1c7c4e66e0 | ||
|
|
2d6da7a326 | ||
|
|
30a439899b | ||
|
|
875a62043a | ||
|
|
7ca1dd1802 | ||
|
|
729cd0a5fc | ||
|
|
8c46f4fc1b | ||
|
|
2a0a20aec5 | ||
|
|
247c7feaab | ||
|
|
8833c604e0 | ||
|
|
cad8ede1e8 | ||
|
|
beca3f3485 | ||
|
|
a5b1518a10 | ||
|
|
4e334166e5 | ||
|
|
8df01e481f | ||
|
|
4ac7292d62 | ||
|
|
67c9b8ac31 | ||
|
|
7834197fbc | ||
|
|
401e7c9169 | ||
|
|
3daa6b1e78 | ||
|
|
e4ba85346d | ||
|
|
ebdca4feb8 | ||
|
|
44eb54eef9 | ||
|
|
75a905802c | ||
|
|
097cddbd03 | ||
|
|
2c17b04f9a | ||
|
|
7814450d0a | ||
|
|
951b255b80 | ||
|
|
37a627c61e | ||
|
|
0ff06b9d86 | ||
|
|
d5495ea4f8 | ||
|
|
938e13e9c7 | ||
|
|
1787766d6f | ||
|
|
afc01f8328 | ||
|
|
9e391e97f4 | ||
|
|
2113f52db4 | ||
|
|
098cf86dc8 | ||
|
|
e99e28aebd | ||
|
|
4abae255e0 | ||
|
|
0b6e052d73 | ||
|
|
45cfc55116 | ||
|
|
a3b50342f9 | ||
|
|
ce4c401c4a | ||
|
|
b8ae10e453 | ||
|
|
f4f9d28d99 | ||
|
|
e17ad54b15 | ||
|
|
9e2f6ad330 | ||
|
|
6f125461dd | ||
|
|
31a37bcf00 | ||
|
|
2025c48e9b | ||
|
|
83b319e7c3 | ||
|
|
ca1f584ae0 | ||
|
|
7378ac5063 | ||
|
|
7e967d4a3a | ||
|
|
f5038a12e3 | ||
|
|
1bbced51a4 | ||
|
|
9de2227af2 | ||
|
|
7d0ba2be63 | ||
|
|
f69b003fe1 | ||
|
|
c9046233a0 | ||
|
|
8be9e15cd0 | ||
|
|
e9762b11bf | ||
|
|
0e7dc8da53 | ||
|
|
837d2b0748 | ||
|
|
4e1b6a71ab | ||
|
|
220bcf947a | ||
|
|
7d27d8299a | ||
|
|
71dba44a0a | ||
|
|
3a22b72132 | ||
|
|
f342b6ff1f | ||
|
|
95669b9eb9 | ||
|
|
88ac0d7174 | ||
|
|
3b20ea46de | ||
|
|
b2ec73926f | ||
|
|
7f90fd0bf1 | ||
|
|
4524bc6ba9 | ||
|
|
49a3cd223b | ||
|
|
061d40d5eb | ||
|
|
05a37f6fe2 | ||
|
|
d95cb46da5 | ||
|
|
8c44eb1b98 | ||
|
|
933bccf052 | ||
|
|
9aa3d93ecf | ||
|
|
bffb6e6705 | ||
|
|
6bfd55158c | ||
|
|
a449c59232 | ||
|
|
a82d509793 | ||
|
|
4bfd84514c | ||
|
|
e34840a78a | ||
|
|
066af3b620 | ||
|
|
d0f273bbfd | ||
|
|
c40d5159b7 | ||
|
|
841dd538dc | ||
|
|
541483ce0b | ||
|
|
364495deb5 | ||
|
|
c85d454e09 | ||
|
|
1c1ee15cff | ||
|
|
1a950a3631 | ||
|
|
0c859e334d | ||
|
|
f4ff1f842d | ||
|
|
d8899ed345 | ||
|
|
c10df4e7bc | ||
|
|
2a82cc4a22 | ||
|
|
5ce611091e | ||
|
|
878e5e3457 | ||
|
|
3cfa9ca721 | ||
|
|
ff4377bf7e | ||
|
|
71aa12c076 | ||
|
|
cfa99b45ce | ||
|
|
e11943f818 | ||
|
|
33070c575c | ||
|
|
86a1b000ef | ||
|
|
b6d89388c8 | ||
|
|
914e1d5872 | ||
|
|
ba62c2255e | ||
|
|
0ea610d0ff | ||
|
|
a4e9b07701 | ||
|
|
2fb626e548 | ||
|
|
827fc6e07a | ||
|
|
e2b92cde84 | ||
|
|
678af4368d | ||
|
|
ed3c3ea038 | ||
|
|
4940d07dad | ||
|
|
dc2b990602 | ||
|
|
92683b6796 | ||
|
|
d858066cf7 | ||
|
|
006b6480ed | ||
|
|
402ee4893c | ||
|
|
d8afe869f4 | ||
|
|
21bba8e7de | ||
|
|
d5de454641 | ||
|
|
11fce726e3 | ||
|
|
ed97087907 | ||
|
|
2b2398ae55 | ||
|
|
d7c7889c82 | ||
|
|
0b1704677d | ||
|
|
ec1cb78a20 | ||
|
|
8b06dd8666 | ||
|
|
76a7929821 | ||
|
|
28b5cba00c | ||
|
|
14526d0d81 | ||
|
|
97eea6e12f | ||
|
|
8b679452eb | ||
|
|
fd27713d20 | ||
|
|
c488558a1c | ||
|
|
addf03751d | ||
|
|
8578d99ccd | ||
|
|
77bfbd6f11 | ||
|
|
38253611d2 | ||
|
|
876ce9ab48 | ||
|
|
0a23c4febe | ||
|
|
308e2e0a5d | ||
|
|
65f36f9b8a | ||
|
|
f50a5cc5e9 | ||
|
|
12582bb529 | ||
|
|
badd0c5ef9 | ||
|
|
2f46f194a1 | ||
|
|
6bc7810c8c | ||
|
|
852ae36d08 | ||
|
|
f9c6ddba3a | ||
|
|
00d4e7a291 | ||
|
|
bfbb3d05a4 | ||
|
|
9b6d326730 | ||
|
|
212df61bc7 | ||
|
|
0abe06e236 | ||
|
|
843bf26ae8 | ||
|
|
44055678c5 | ||
|
|
9ea1d93a8d | ||
|
|
ab3ad718a0 | ||
|
|
83864cc7fd | ||
|
|
ad7eea67c1 | ||
|
|
be492ddc98 | ||
|
|
e8c852d490 | ||
|
|
7a3c3b40af | ||
|
|
5288fed0ea | ||
|
|
1e306ed5ab | ||
|
|
9e25e978e5 | ||
|
|
3248047b76 | ||
|
|
5edcd6765b | ||
|
|
80bf6ed96b | ||
|
|
665af07277 | ||
|
|
2e7852fa4d | ||
|
|
3ee5840d6d | ||
|
|
ee649d03da | ||
|
|
19094d4f3a | ||
|
|
fb3d600428 | ||
|
|
7b1c0402b0 | ||
|
|
365b8c003e | ||
|
|
1bec25c02d | ||
|
|
c3703ebc73 | ||
|
|
ede933dcc2 | ||
|
|
d955ff2874 | ||
|
|
4388fba5b2 | ||
|
|
bd320e65a5 | ||
|
|
3445b7226d | ||
|
|
f2d7a4fe7d | ||
|
|
25e4986998 | ||
|
|
c341669dbd | ||
|
|
a4d28e2586 | ||
|
|
a912aacf73 | ||
|
|
256d5d084d | ||
|
|
f0c1b64bc8 | ||
|
|
0164ac86fa | ||
|
|
12a1316f10 |
+5
-1
@@ -1 +1,5 @@
|
||||
craft-i18n.rev text eol=lf
|
||||
# Text lives in the repository with LF. A Windows checkout once committed 50 files with CRLF
|
||||
# (#6537) and `cargo fmt --check` still passed, so the gate could not see it. Normalise on add.
|
||||
* text=auto
|
||||
|
||||
craft-i18n.rev text eol=lf
|
||||
@@ -27,6 +27,10 @@ jobs:
|
||||
name: Windows installation policy (no app build)
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
@@ -30,66 +30,31 @@ concurrency:
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
# The print host and UI tests on Linux, Windows and macOS, through the suite's shared gate: it
|
||||
# checks out craft-i18n (craft-i18n.rev) and craft-fonts, and installs the Ubuntu packages through
|
||||
# install-apt-packages, which falls back to mirrors outside ubuntu.com (as on 11 Nov 2026).
|
||||
native:
|
||||
name: Print (${{ matrix.os }})
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-24.04, windows-latest, macos-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
timeout-minutes: 90
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
CARGO_INCREMENTAL: '0'
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
# The interface catalogs and craft-proof's Language data come from craft-i18n at the commit in
|
||||
# craft-i18n.rev, so these tests build with the same data as the gate (ci.yml).
|
||||
- name: Check out craft-i18n
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main
|
||||
|
||||
name: Print
|
||||
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
|
||||
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
|
||||
with:
|
||||
os: '["ubuntu-24.04","windows-latest","macos-latest"]'
|
||||
timeout-minutes: 90
|
||||
# The catalogs include CJK interface languages, so the UI tests need the suite's fonts, as in
|
||||
# the gate (same commit as ci.yml and release.yml).
|
||||
- name: Check out craft-fonts
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@main
|
||||
with:
|
||||
ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
|
||||
- name: Build with craft-fonts
|
||||
shell: bash
|
||||
run: |
|
||||
echo "CRAFT_FONTS_DIR=$GITHUB_WORKSPACE/craft-fonts" >> "$GITHUB_ENV"
|
||||
echo "CRAFT_FONTS_REQUIRED=1" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Install Linux build and software-rendering dependencies
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev \
|
||||
libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev libvulkan1 mesa-vulkan-drivers
|
||||
|
||||
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
|
||||
with:
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
|
||||
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
|
||||
with:
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
- name: Formatting
|
||||
run: cargo fmt --all -- --check
|
||||
|
||||
- name: Native app and UI Clippy
|
||||
run: cargo clippy -p wordcraft -p wordcraft-ui-egui --all-targets --locked -- -D warnings
|
||||
|
||||
- name: Native app and UI tests
|
||||
run: cargo test -p wordcraft -p wordcraft-ui-egui --locked
|
||||
|
||||
- name: Browser compatibility
|
||||
if: runner.os == 'Linux'
|
||||
run: cargo check -p wordcraft-web --target wasm32-unknown-unknown --locked
|
||||
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
packages: >-
|
||||
pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev
|
||||
libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev libvulkan1 mesa-vulkan-drivers
|
||||
# No nextest: the tests run with cargo test, as before.
|
||||
tools: ""
|
||||
# Its own cache, apart from ci.yml's gate, which builds the whole workspace.
|
||||
cache-version: v1-printing
|
||||
command: |
|
||||
cargo fmt --all -- --check
|
||||
cargo clippy -p wordcraft -p wordcraft-ui-egui --all-targets --locked -- -D warnings
|
||||
cargo test -p wordcraft -p wordcraft-ui-egui --locked
|
||||
# Browser compatibility, once.
|
||||
if [ "$RUNNER_OS" = Linux ]; then
|
||||
cargo check -p wordcraft-web --target wasm32-unknown-unknown --locked
|
||||
fi
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
# A nightly dry run on main: every release build except macOS, nothing signed or released
|
||||
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
|
||||
schedule:
|
||||
- cron: "0 3 * * *"
|
||||
push:
|
||||
branches: [release]
|
||||
workflow_dispatch:
|
||||
@@ -18,7 +22,14 @@ concurrency:
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
# Regenerates contributors/contributors.json for the release commit; every build bundles it.
|
||||
credits:
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main
|
||||
release:
|
||||
needs: credits
|
||||
# Only this job publishes the GitHub release, so only it gets write access.
|
||||
permissions:
|
||||
contents: write
|
||||
@@ -26,11 +37,13 @@ jobs:
|
||||
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
|
||||
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
|
||||
with:
|
||||
dry-run: ${{ github.event_name == 'schedule' }}
|
||||
app-name: wordcraft
|
||||
app-title: WordCraft
|
||||
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
web-craft-fonts: true
|
||||
version: ${{ inputs.version }}
|
||||
contributors-artifact: ${{ needs.credits.outputs.artifact-name }}
|
||||
secrets:
|
||||
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
||||
APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
|
||||
|
||||
Generated
+541
-132
File diff suppressed because it is too large
Load Diff
+24
-8
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/wordcraft", "apps/wordcraft-cli", "xtask"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.15.1"
|
||||
version = "0.18.1"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
@@ -36,7 +36,7 @@ wordcraft-mcp = { path = "crates/mcp" }
|
||||
wordcraft-zotero = { path = "crates/zotero" }
|
||||
wordcraft-control-key = { path = "crates/control-key" }
|
||||
wordcraft-ui-egui = { path = "crates/ui-egui" }
|
||||
wordcraft-metafile = { path = "crates/metafile" }
|
||||
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", version = "=0.1.0" }
|
||||
|
||||
# foundation
|
||||
kurbo = { version = "0.13", features = ["serde"] }
|
||||
@@ -66,12 +66,28 @@ wuff = "0.2.9"
|
||||
# `forbid(unsafe_code)` in its default build, no_std-capable (builds for wasm), BSD-3-Clause/MIT.
|
||||
brotli-decompressor = "5"
|
||||
vello_cpu = "0.2"
|
||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "bmp", "tiff"] }
|
||||
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", version = "=0.1.0", default-features = false, features = ["write"] }
|
||||
image = { version = "0.25", default-features = false }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "41fb227542efb8a7748caa1c659f271d8605303c", 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-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 = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] }
|
||||
quick-xml = "0.41"
|
||||
zip = { version = "3", default-features = false, features = ["deflate"] }
|
||||
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" }
|
||||
# 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"
|
||||
@@ -134,7 +150,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 = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" }
|
||||
krilla = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
|
||||
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" }
|
||||
|
||||
+18
@@ -48,3 +48,21 @@ _Avoid_: User dictionary (in the interface)
|
||||
A folder of Hunspell dictionaries you supply, used before the system's (an installed Language pack
|
||||
comes after it).
|
||||
_Avoid_: Custom dictionaries
|
||||
|
||||
## Export
|
||||
|
||||
**Strict export**:
|
||||
An export that writes nothing, and reports why, when a character or picture the output is meant
|
||||
to show would be missing from it. Text the output doesn't show (hidden text, deletions without
|
||||
markup) isn't checked.
|
||||
_Avoid_: Safe mode, validated export
|
||||
|
||||
**Missing glyph**:
|
||||
A character the output draws that no face in the font fallback chain can draw, so it would appear
|
||||
as an empty box.
|
||||
_Avoid_: Tofu, box, notdef character
|
||||
|
||||
**Missing picture**:
|
||||
A picture the document refers to that the output cannot include: its part is absent, its data
|
||||
cannot be decoded, its format is unsupported, or it is linked from outside the file.
|
||||
_Avoid_: Broken image, dropped image
|
||||
|
||||
@@ -124,7 +124,7 @@ Every screenshot below is WordCraft itself, rendered offscreen by its own UI tes
|
||||
| **References** | Table of contents, footnotes and endnotes, citations and bibliography (APA, MLA, Chicago, IEEE), captions, table of figures, cross-references, index, table of authorities |
|
||||
| **Review** | Spelling and grammar with suggestions (English and Polish, by each run's language), thesaurus, word count, comments in margin balloons or a pane, track changes, accept/reject, compare documents, restrict editing, accessibility checker, document inspector |
|
||||
| **Mailings** | Mail merge from CSV, merge fields, address block, greeting line, rules, preview, finish to a document; envelopes and labels |
|
||||
| **View** | Print layout, web layout, draft, read mode, focus, zoom, one/multiple pages, page width, Navigation pane, rulers, gridlines, dark mode; interface in English, 简体中文, 繁體中文, 日本語, Português (Brasil), Español, Українська, Српски / Srpski, Eesti, Türkçe, Русский, Italiano, Deutsch, Polski, Čeština, Magyar, Français, Български, Tiếng Việt, 한국어, Svenska, Hrvatski, Nederlands, Dansk, Română, العربية (mirrored) or Slovenščina (follows the system language by default) |
|
||||
| **View** | Print layout, web layout, draft, read mode, focus, zoom, one/multiple pages, page width, Navigation pane, rulers, gridlines, dark mode; interface in English, 简体中文, 繁體中文, 日本語, Português (Brasil), Español, Українська, Српски / Srpski, Eesti, Türkçe, Русский, Italiano, Deutsch, Polski, Čeština, Magyar, Français, Български, Tiếng Việt, 한국어, Svenska, Hrvatski, Nederlands, Dansk, Română, العربية (mirrored) Slovenščina or Bosanski (follows the system language by default) |
|
||||
| **Files** | .docx read/write, legacy 97-2003 .doc import, PDF export, .odt, .rtf, .html, .md, .tex, .txt import/export, page images |
|
||||
|
||||
The honest picture, area by area, is in [ROADMAP.md](ROADMAP.md), the
|
||||
@@ -157,6 +157,7 @@ Command line:
|
||||
|
||||
```sh
|
||||
wordcraft-cli convert report.docx report.pdf # docx, pdf, odt, rtf, html, md, tex, txt, png
|
||||
wordcraft-cli convert report.docx report.pdf --strict # exit 3, no file, if a character or picture would be left out
|
||||
wordcraft-cli text report.docx # plain text
|
||||
wordcraft-cli inspect report.docx # structure as JSON
|
||||
wordcraft-cli run --template sample \
|
||||
@@ -164,6 +165,40 @@ wordcraft-cli run --template sample \
|
||||
wordcraft-cli run --script job.json # the same from a file: commands, then save to docx and pdf
|
||||
```
|
||||
|
||||
`convert --strict` (a `.pdf` or `.png` output only) and `render --strict` (a PNG of one page) refuse a silently incomplete render: they exit 3
|
||||
and write nothing if the output would draw a character no font has (an empty box) or leave out a
|
||||
picture. Exit codes are 0 (written), 1 (unreadable input, usage error, or `--strict` with another
|
||||
output type, checked before the input is read) and 3 (strict refusal). On exit 3, stderr has one
|
||||
`wordcraft-cli: strict: …` line per kind, then one JSON line:
|
||||
|
||||
```sh
|
||||
wordcraft-cli convert in.docx out.pdf --strict
|
||||
# wordcraft-cli: strict: 1 character cannot be drawn: U+2D30 (×3)
|
||||
# wordcraft-cli: strict: 1 picture(s) left out: word/media/image2.bin (header2.xml): unsupported format
|
||||
# {"schema":1,"strict":"failed","missing_glyphs":[{"cp":"U+2D30","count":3}],"missing_pictures":[{"part":"word/media/image2.bin","source_part":"word/header2.xml","reason":"unsupported_format"}],"truncated":false,"total_missing_glyphs":1,"total_missing_pictures":1,"glyphs_truncated":false,"pictures_truncated":false}
|
||||
```
|
||||
|
||||
The JSON line (schema 1, also what `file.strictReport` returns) has `schema`, `strict`
|
||||
(`"passed"` or `"failed"`), `missing_glyphs` (entries `{"cp", "count"}`, sorted by code point),
|
||||
`missing_pictures` (entries `{"part", "source_part", "reason"}`: the picture's part or target, the
|
||||
story part it sits in such as `word/header2.xml`, and a reason code; `source_part` is also
|
||||
`background` (the page picture), `body`, `header`, `footer`, `footnote`, `endnote`, `comment` or
|
||||
`text box` where the part name is not known (a document not opened from a DOCX), or a chunk path such as
|
||||
`word/afchunk1.docx > word/header1.xml` for a picture inside an embedded DOCX chunk), `truncated`, the two totals
|
||||
`total_missing_glyphs` and `total_missing_pictures`, and `glyphs_truncated` and
|
||||
`pictures_truncated` for each list. The reason codes are `missing_part`, `external_target`,
|
||||
`link_only`, `undecodable` and `unsupported_format`; the stderr lines use plain words for them. When a list is cut, the stderr line gives the total
|
||||
(`300 characters cannot be drawn (first 256 listed)`).
|
||||
Each list stops at 256 entries separately; `truncated` is true when either was cut, and the totals
|
||||
count every distinct character and picture found, those past the cap included. Only characters the output draws count (not hidden text, or deleted text
|
||||
without `--markup`), and a character a fallback font draws is fine. Text inside a WMF or EMF picture counts too, shaped in the font the picture names with no fallback. A Private Use Area character
|
||||
fails only if no installed font maps it. The result depends on the fonts installed on the host
|
||||
(see #5624 for a fixed font set). An external picture target is never fetched: it is reported, not
|
||||
loaded. Pictures dropped when a DOCX is opened (a missing part, an external target, or a `link only` picture with no embedded copy) are reported for DOCX input only. They have no page, so a PNG of any page is also refused for a missing or external picture anywhere in the document, unused headers included. `render --strict` checks only the page it
|
||||
writes (`--page`; `--scale` doesn't matter), prints nothing on stdout when it refuses, and reports the
|
||||
same way. The engine takes `strict: true` on `file.save`, `file.saveAs`, `file.exportPdf` and
|
||||
`file.exportPng`, and `file.strictReport {markup?, page?}` returns the report without writing.
|
||||
|
||||
Web: `cd apps/wordcraft-web && trunk serve`, then open <http://127.0.0.1:8771/?sample>.
|
||||
|
||||
## For agents: CLI and MCP
|
||||
@@ -319,6 +354,11 @@ XWayland:
|
||||
WORDCRAFT_WAYLAND=1 wordcraft
|
||||
```
|
||||
|
||||
XWayland needs the X11 libraries, which the packages only recommend and the tarball doesn't
|
||||
install: libX11, libX11-xcb, libXcursor, libXi and libxkbcommon-x11 (on Debian and Ubuntu,
|
||||
`libx11-6`, `libx11-xcb1`, `libxcursor1`, `libxi6` and `libxkbcommon-x11-0`). Without them the app
|
||||
opens on native Wayland, whatever the setting says, and logs which ones are missing.
|
||||
|
||||
**Arch Linux (AUR).** Community members maintain three AUR packages; the project doesn't publish
|
||||
to the AUR itself. Install one of them:
|
||||
| Package | What it installs | Choose it when |
|
||||
|
||||
+176
-3
File diff suppressed because one or more lines are too long
@@ -21,3 +21,9 @@ wordcraft-mcp = { workspace = true }
|
||||
wordcraft-zotero = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
sys-locale = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
wordcraft-fonts = { workspace = true }
|
||||
wordcraft-geom = { workspace = true, features = ["image"] }
|
||||
image = { workspace = true }
|
||||
craft-zip = { workspace = true }
|
||||
|
||||
+216
-62
@@ -11,23 +11,30 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::ExitCode;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use wordcraft_engine::Session;
|
||||
use wordcraft_engine::{CmdError, Session};
|
||||
|
||||
const USAGE: &str = "\
|
||||
wordcraft-cli — WordCraft from the command line
|
||||
|
||||
USAGE:
|
||||
wordcraft-cli convert <in> <out> [--markup] [--update-fields] [--format wordml2003|flatopc]
|
||||
wordcraft-cli convert <in> <out> [--markup] [--update-fields] [--strict] [--format wordml2003|flatopc]
|
||||
convert between formats (docx, doc [read], pdf, odt, rtf, html, md, tex, txt, json, png,
|
||||
xml); --update-fields refreshes unlocked fields before saving;
|
||||
xml); --update-fields refreshes unlocked fields before saving
|
||||
(if protection blocks updates, they are skipped with a warning);
|
||||
an .xml file is a Word XML Document (Flat OPC), or with
|
||||
--format wordml2003 a Word 2003 XML Document;
|
||||
--markup (Print Markup): comments beside each page and
|
||||
tracked changes in pdf/png, a comment list in html/md/txt
|
||||
tracked changes in pdf/png, a comment list in html/md/txt;
|
||||
--strict (pdf or png output only) writes nothing and exits 3
|
||||
if a character can't be drawn or a picture is missing or
|
||||
undecodable; the report is on stderr, with one JSON line
|
||||
wordcraft-cli info <file> pages, words, paragraphs, properties (JSON)
|
||||
wordcraft-cli text <file> plain text
|
||||
wordcraft-cli inspect <file> document structure (JSON)
|
||||
wordcraft-cli render <file> <out.png> [--page N] [--scale S]
|
||||
wordcraft-cli render <file> <out.png> [--page N] [--scale S] [--strict]
|
||||
--strict writes nothing and exits 3 if a character on the
|
||||
page can't be drawn or a picture is missing or undecodable
|
||||
(report on stderr, as for convert --strict)
|
||||
wordcraft-cli run [--file F | --template T] [--script FILE|-] --cmd 'id={json}' [--cmd …]
|
||||
[--save OUT] [--print] [--author NAME]
|
||||
run commands headlessly, then save (--author names
|
||||
@@ -77,6 +84,14 @@ fn wants_help(cmd: &str, rest: &[String]) -> bool {
|
||||
SUBCOMMANDS.contains(&cmd) && rest.iter().any(|a| a == "--help" || a == "-h")
|
||||
}
|
||||
|
||||
/// A failure whose message may carry text from a file or from the engine's reading of one (a
|
||||
/// picture's part name, a parse error quoting the input): control, bidi and invisible characters
|
||||
/// are escaped, so it can't forge or spoof a line (the CLI's own messages, which have line breaks
|
||||
/// on purpose, are not routed through here). Backslashes stay: messages quote Windows paths.
|
||||
fn engine_error(e: impl fmt::Display) -> Failure {
|
||||
Failure::Error(escape_text(&e.to_string(), false))
|
||||
}
|
||||
|
||||
/// The error every subcommand gives when a required path is not there.
|
||||
fn missing_argument() -> Failure {
|
||||
format!("missing argument\n\n{USAGE}").into()
|
||||
@@ -85,11 +100,11 @@ fn missing_argument() -> Failure {
|
||||
/// Open a document for a subcommand. A password-protected Word file needs its password
|
||||
/// ([`password_from`]). As in the app, a document opened with one is saved with it again, so
|
||||
/// `convert` does not quietly write a copy the password no longer protects.
|
||||
fn open(path: &Path, password: Option<&str>) -> Result<Session, String> {
|
||||
fn open(path: &Path, password: Option<&str>) -> Result<Session, Failure> {
|
||||
// A file that gives no page size (HTML, Markdown, plain text) opens on the system region's
|
||||
// paper, as in the app: A4 in Italy, Letter in the US (#1381).
|
||||
let region = system_region();
|
||||
let (doc, encrypted) = wordcraft_engine::io::open_path_in(path, password, ®ion)?;
|
||||
let (doc, encrypted) = wordcraft_engine::io::open_path_in(path, password, ®ion).map_err(engine_error)?;
|
||||
let mut s = session(doc);
|
||||
s.path = Some(path.into());
|
||||
if encrypted {
|
||||
@@ -98,8 +113,8 @@ fn open(path: &Path, password: Option<&str>) -> Result<Session, String> {
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// The commands that open a file, and which of their arguments not starting with `--` it is (as
|
||||
/// `pos` counts). `run` opens the value of `--file`.
|
||||
/// The commands that open a file, and which positional argument it is, excluding option values.
|
||||
/// `run` opens the value of `--file`.
|
||||
const OPENS_FILE: &[(&str, usize)] = &[("convert", 0), ("info", 0), ("text", 0), ("inspect", 0), ("render", 0), ("zotero", 1)];
|
||||
|
||||
/// The arguments as text. Only the file the command opens may have a name that isn't valid Unicode,
|
||||
@@ -108,8 +123,14 @@ const OPENS_FILE: &[(&str, usize)] = &[("convert", 0), ("info", 0), ("text", 0),
|
||||
fn text_args<S: AsRef<OsStr>>(raw: &[S]) -> Result<(Vec<String>, Option<PathBuf>), String> {
|
||||
let args: Vec<String> = raw.iter().map(|a| a.as_ref().to_string_lossy().into_owned()).collect();
|
||||
let cmd = args.first().map(String::as_str).unwrap_or("");
|
||||
let specs = match cmd {
|
||||
"convert" => CONVERT_OPTIONS,
|
||||
"render" => RENDER_OPTIONS,
|
||||
"zotero" => ZOTERO_OPTIONS,
|
||||
_ => PASSWORD_OPTIONS,
|
||||
};
|
||||
let file_at = match OPENS_FILE.iter().find(|(c, _)| *c == cmd) {
|
||||
Some((_, n)) => args.iter().enumerate().skip(1).filter(|(_, a)| !a.starts_with("--")).nth(*n).map(|(i, _)| i),
|
||||
Some((_, n)) => positional_indices(args.get(1..).unwrap_or_default(), specs).get(*n).map(|i| i + 1),
|
||||
None if cmd == "run" => args.iter().position(|a| a == "--file").map(|i| i + 1),
|
||||
None => None,
|
||||
};
|
||||
@@ -145,8 +166,13 @@ fn password_from(args: &[String]) -> Result<Option<String>, String> {
|
||||
}
|
||||
|
||||
/// The positional arguments, skipping every option's value so an option value is never read as a
|
||||
/// path. Mirrors [`validate_options`], which has already checked the options are known.
|
||||
/// path. Uses the same option specifications as [`validate_options`].
|
||||
fn positionals<'a>(args: &'a [String], specs: &[OptionSpec]) -> Vec<&'a String> {
|
||||
positional_indices(args, specs).into_iter().filter_map(|i| args.get(i)).collect()
|
||||
}
|
||||
|
||||
/// Keep the original indices too, so [`text_args`] can open a positional path by its native name.
|
||||
fn positional_indices(args: &[String], specs: &[OptionSpec]) -> Vec<usize> {
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0;
|
||||
while let Some(arg) = args.get(i) {
|
||||
@@ -156,7 +182,7 @@ fn positionals<'a>(args: &'a [String], specs: &[OptionSpec]) -> Vec<&'a String>
|
||||
i += 2;
|
||||
} else {
|
||||
if !arg.starts_with("--") {
|
||||
out.push(arg);
|
||||
out.push(i);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
@@ -187,7 +213,7 @@ fn listed_text(doc: &wordcraft_doc::Document) -> Result<String, String> {
|
||||
/// The command also checks whether the open document is protected: this name determines which
|
||||
/// named editing exceptions the session can use, so the flag must not bypass that check.
|
||||
fn set_author(s: &mut Session, name: &str) -> Result<(), String> {
|
||||
s.run("file.setAuthor", &json!({"name": name})).map(|_| ()).map_err(|e| format!("--author: {e}"))
|
||||
s.run("file.setAuthor", &json!({"name": name})).map(|_| ()).map_err(|e| escape_text(&format!("--author: {e}"), false))
|
||||
}
|
||||
|
||||
fn arg_value(args: &[String], name: &str) -> Option<String> {
|
||||
@@ -299,10 +325,12 @@ const CONVERT_OPTIONS: &[OptionSpec] = &[
|
||||
OptionSpec { name: "--markup", takes_value: false },
|
||||
OptionSpec { name: "--format", takes_value: true },
|
||||
OptionSpec { name: "--update-fields", takes_value: false },
|
||||
OptionSpec { name: "--strict", takes_value: false },
|
||||
OptionSpec { name: "--password", takes_value: true },
|
||||
OptionSpec { name: "--password-file", takes_value: true },
|
||||
];
|
||||
const RENDER_OPTIONS: &[OptionSpec] = &[
|
||||
OptionSpec { name: "--strict", takes_value: false },
|
||||
OptionSpec { name: "--page", takes_value: true },
|
||||
OptionSpec { name: "--scale", takes_value: true },
|
||||
OptionSpec { name: "--password", takes_value: true },
|
||||
@@ -339,6 +367,9 @@ enum Failure {
|
||||
/// Whoever read stdout closed it (`wordcraft-cli commands --json | head`): there is nobody left
|
||||
/// to write to, so stop quietly and exit 0 like other command-line tools.
|
||||
StdoutClosed,
|
||||
/// `convert --strict` refused: the text for stderr (the report's lines, then its JSON line).
|
||||
/// Exit status 3.
|
||||
Strict(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for Failure {
|
||||
@@ -346,6 +377,7 @@ impl fmt::Display for Failure {
|
||||
match self {
|
||||
Failure::Error(e) => f.write_str(e),
|
||||
Failure::StdoutClosed => f.write_str("stdout closed"),
|
||||
Failure::Strict(report) => f.write_str(report),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -391,11 +423,104 @@ fn valid_connect_addr(addr: &str) -> bool {
|
||||
!host.is_empty() && port.parse::<u16>().is_ok()
|
||||
}
|
||||
|
||||
/// A failed `--strict` export: a strict refusal becomes [`Failure::Strict`] carrying what stderr
|
||||
/// shows (each line of the report as `wordcraft-cli: …`, then its JSON on one line, from
|
||||
/// `file.strictReport` for `page` of a page image, else the PDF); any other error is an ordinary
|
||||
/// failure.
|
||||
fn refusal(s: &mut Session, e: wordcraft_engine::CmdError, markup: bool, page: Option<u64>) -> Failure {
|
||||
if !matches!(&e, wordcraft_engine::CmdError::Failed(m) if m.starts_with("strict: ")) {
|
||||
return engine_error(e);
|
||||
}
|
||||
let mut params = json!({"markup": markup});
|
||||
if let Some(page) = page {
|
||||
params["page"] = json!(page);
|
||||
}
|
||||
Failure::Strict(match s.run("file.strictReport", ¶ms).map_err(|e| e.to_string()) {
|
||||
Ok(json) => {
|
||||
let mut text = String::new();
|
||||
for line in report_lines(&json) {
|
||||
text += &format!("wordcraft-cli: {}\n", escape_peer(&line));
|
||||
}
|
||||
text + &json_line_safe(&json.to_string())
|
||||
}
|
||||
Err(e) => format!("wordcraft-cli: strict: refused, and the report could not be made: {}", escape_text(&e, false)),
|
||||
})
|
||||
}
|
||||
|
||||
/// The report's human lines, from its JSON (the engine's `StrictReport::lines`).
|
||||
fn report_lines(json: &Value) -> Vec<String> {
|
||||
wordcraft_engine::strict::lines_from_json(json)
|
||||
}
|
||||
|
||||
/// Explicitly update unlocked fields in a session before saving the document.
|
||||
///
|
||||
/// If field updates are disabled by document protection (`CmdError::Disabled`), logs a warning to
|
||||
/// stderr and continues conversion rather than aborting. Other command failures are converted to
|
||||
/// [`Failure`] via [`engine_error`].
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `s` - The active document [`Session`] whose unlocked fields should be refreshed.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `Err(Failure)` if the command fails with an error other than `CmdError::Disabled`.
|
||||
fn update_fields(s: &mut Session) -> Result<(), Failure> {
|
||||
if let Err(e) = s.run("references.updateFields", &json!({})) {
|
||||
if matches!(e, CmdError::Disabled(_)) {
|
||||
eprintln!("wordcraft-cli: warning: {e}");
|
||||
} else {
|
||||
return Err(engine_error(e));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `convert <in> <out>`: `rest` is the command line after `convert`, `open` opens the input.
|
||||
fn run_convert(rest: &[String], open: impl Fn(&str, Option<&str>) -> Result<Session, Failure>) -> Result<(), Failure> {
|
||||
validate_options("convert", rest, CONVERT_OPTIONS)?;
|
||||
// `--format wordml2003` (or `flatopc`) picks the kind of `.xml` file; its value isn't a path.
|
||||
let format = match find_option_arg(rest, "--format") {
|
||||
OptionArg::Absent => None,
|
||||
OptionArg::Duplicate => return Err("convert: duplicate --format option".into()),
|
||||
OptionArg::MissingValue => return Err("convert: --format requires a value".into()),
|
||||
OptionArg::Value(value) => Some(value),
|
||||
};
|
||||
let paths = positionals(rest, CONVERT_OPTIONS);
|
||||
let (i, o) = (paths.first().ok_or_else(missing_argument)?.to_string(), paths.get(1).ok_or_else(missing_argument)?.to_string());
|
||||
let markup = rest.iter().any(|a| a == "--markup");
|
||||
let strict = rest.iter().any(|a| a == "--strict");
|
||||
// Before the input is opened: a wrong output type is a usage error, not a file error.
|
||||
let to_png = o.to_ascii_lowercase().ends_with(".png");
|
||||
if strict && !(to_png || o.to_ascii_lowercase().ends_with(".pdf")) {
|
||||
return Err("convert: --strict needs a .pdf or .png output".into());
|
||||
}
|
||||
let password = password_from(rest)?;
|
||||
let mut s = open(&i, password.as_deref())?;
|
||||
if rest.iter().any(|a| a == "--update-fields") {
|
||||
update_fields(&mut s)?;
|
||||
}
|
||||
let r = if to_png {
|
||||
s.run("file.exportPng", &json!({"path": o, "page": 1, "markup": markup, "strict": strict}))
|
||||
} else {
|
||||
let mut params = json!({"path": o, "markup": markup, "strict": strict});
|
||||
if let Some(f) = format {
|
||||
params["format"] = json!(f);
|
||||
}
|
||||
s.run("file.save", ¶ms)
|
||||
};
|
||||
if let Err(e) = r {
|
||||
return Err(if strict { refusal(&mut s, e, markup, to_png.then_some(1)) } else { engine_error(e) });
|
||||
}
|
||||
eprintln!("wrote {o}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_run_script(rest: &[String], script_path: Option<&Path>) -> Result<Option<script::Script>, Failure> {
|
||||
match arg_value(rest, "--script") {
|
||||
Some(path) if !path.starts_with("--") => {
|
||||
let source = script_path.unwrap_or(Path::new(&path));
|
||||
Ok(Some(script::load(source)?))
|
||||
Ok(Some(script::load(source).map_err(engine_error)?))
|
||||
}
|
||||
_ if rest.iter().any(|arg| arg == "--script") => Err("run: --script requires a value".into()),
|
||||
_ => Ok(None),
|
||||
@@ -427,42 +552,13 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
return write_stdout(|o| write!(o, "{USAGE}"));
|
||||
}
|
||||
match cmd {
|
||||
"convert" => {
|
||||
validate_options("convert", &rest, CONVERT_OPTIONS)?;
|
||||
// `--format wordml2003` (or `flatopc`) picks the kind of `.xml` file; its value isn't a path.
|
||||
let format = match find_option_arg(&rest, "--format") {
|
||||
OptionArg::Absent => None,
|
||||
OptionArg::Duplicate => return Err("convert: duplicate --format option".into()),
|
||||
OptionArg::MissingValue => return Err("convert: --format requires a value".into()),
|
||||
OptionArg::Value(value) => Some(value),
|
||||
};
|
||||
let paths = positionals(&rest, CONVERT_OPTIONS);
|
||||
let (i, o) = (paths.first().ok_or_else(missing_argument)?.to_string(), paths.get(1).ok_or_else(missing_argument)?.to_string());
|
||||
let markup = rest.iter().any(|a| a == "--markup");
|
||||
let password = password_from(&rest)?;
|
||||
let mut s = open(&i, password.as_deref())?;
|
||||
if rest.iter().any(|a| a == "--update-fields") {
|
||||
s.run("references.updateFields", &json!({})).map_err(|e| e.to_string())?;
|
||||
}
|
||||
let r = if o.to_ascii_lowercase().ends_with(".png") {
|
||||
s.run("file.exportPng", &json!({"path": o, "page": 1, "markup": markup}))
|
||||
} else {
|
||||
let mut params = json!({"path": o, "markup": markup});
|
||||
if let Some(f) = format {
|
||||
params["format"] = json!(f);
|
||||
}
|
||||
s.run("file.save", ¶ms)
|
||||
};
|
||||
r.map_err(|e| e.to_string())?;
|
||||
eprintln!("wrote {o}");
|
||||
Ok(())
|
||||
}
|
||||
"convert" => run_convert(&rest, open),
|
||||
"info" => {
|
||||
validate_options("info", &rest, PASSWORD_OPTIONS)?;
|
||||
let paths = positionals(&rest, PASSWORD_OPTIONS);
|
||||
let password = password_from(&rest)?;
|
||||
let mut s = open(paths.first().ok_or_else(missing_argument)?, password.as_deref())?;
|
||||
let info = serde_json::to_string_pretty(&s.run("file.info", &json!({})).map_err(|e| e.to_string())?).unwrap_or_default();
|
||||
let info = serde_json::to_string_pretty(&s.run("file.info", &json!({})).map_err(engine_error)?).unwrap_or_default();
|
||||
write_stdout(|o| writeln!(o, "{info}"))
|
||||
}
|
||||
"text" => {
|
||||
@@ -478,7 +574,7 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
let paths = positionals(&rest, PASSWORD_OPTIONS);
|
||||
let password = password_from(&rest)?;
|
||||
let mut s = open(paths.first().ok_or_else(missing_argument)?, password.as_deref())?;
|
||||
let tree = serde_json::to_string_pretty(&s.run("document.inspect", &json!({})).map_err(|e| e.to_string())?).unwrap_or_default();
|
||||
let tree = serde_json::to_string_pretty(&s.run("document.inspect", &json!({})).map_err(engine_error)?).unwrap_or_default();
|
||||
write_stdout(|o| writeln!(o, "{tree}"))
|
||||
}
|
||||
"render" => {
|
||||
@@ -492,7 +588,17 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
let page = parse_page_arg("render", &rest)?.unwrap_or(1);
|
||||
let scale = parse_scale_arg("render", &rest)?.unwrap_or(2.0);
|
||||
let mut s = open(&i, password.as_deref())?;
|
||||
let r = s.run("file.exportPng", &json!({"path": o, "page": page, "scale": scale})).map_err(|e| e.to_string())?;
|
||||
let strict = rest.iter().any(|a| a == "--strict");
|
||||
let mut params = json!({"path": o, "page": page, "scale": scale, "strict": strict});
|
||||
if strict {
|
||||
// The check and its report must see the page as it is written.
|
||||
params["markup"] = json!(false);
|
||||
}
|
||||
let r = match s.run("file.exportPng", ¶ms) {
|
||||
Ok(r) => r,
|
||||
Err(e) if strict => return Err(refusal(&mut s, e, false, Some(page))),
|
||||
Err(e) => return Err(engine_error(e)),
|
||||
};
|
||||
write_stdout(|o| writeln!(o, "{r}"))
|
||||
}
|
||||
"run" => {
|
||||
@@ -511,8 +617,8 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
let mut s = session(wordcraft_doc::Document::new());
|
||||
let first = s.document_id();
|
||||
let params = t.map_or_else(|| json!({}), |t| json!({"template": t}));
|
||||
s.run("file.new", ¶ms).map_err(|e| e.to_string())?;
|
||||
s.close_document(first).map_err(|e| e.to_string())?;
|
||||
s.run("file.new", ¶ms).map_err(engine_error)?;
|
||||
s.close_document(first).map_err(engine_error)?;
|
||||
s
|
||||
}
|
||||
};
|
||||
@@ -521,7 +627,7 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
}
|
||||
let print = rest.iter().any(|a| a == "--print");
|
||||
for (n, (id, params)) in script.iter().flat_map(|c| c.commands.iter()).enumerate() {
|
||||
let r = s.run(id, params).map_err(|e| format!("command {} ({id}): {e}", n + 1))?;
|
||||
let r = s.run(id, params).map_err(|e| engine_error(format!("command {} ({id}): {e}", n + 1)))?;
|
||||
if print {
|
||||
write_stdout(|o| writeln!(o, "{id}: {r}"))?;
|
||||
}
|
||||
@@ -531,10 +637,12 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
if rest.get(i).map(String::as_str) == Some("--cmd") {
|
||||
let spec = rest.get(i + 1).ok_or("--cmd needs a value")?;
|
||||
let (id, params) = match spec.split_once('=') {
|
||||
Some((id, p)) => (id.to_string(), serde_json::from_str::<Value>(p).map_err(|e| format!("{id}: bad JSON params: {e}"))?),
|
||||
Some((id, p)) => {
|
||||
(id.to_string(), serde_json::from_str::<Value>(p).map_err(|e| engine_error(format!("{id}: bad JSON params: {e}")))?)
|
||||
}
|
||||
None => (spec.clone(), json!({})),
|
||||
};
|
||||
let r = s.run(&id, ¶ms).map_err(|e| format!("{id}: {e}"))?;
|
||||
let r = s.run(&id, ¶ms).map_err(|e| engine_error(format!("{id}: {e}")))?;
|
||||
if print {
|
||||
write_stdout(|o| writeln!(o, "{id}: {r}"))?;
|
||||
}
|
||||
@@ -554,7 +662,7 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
return Err(format!("{out}: is a folder, not a file").into());
|
||||
}
|
||||
if !out.to_ascii_lowercase().ends_with(".png") {
|
||||
wordcraft_engine::io::save_bytes(out, &s.doc)?;
|
||||
wordcraft_engine::io::save_bytes(out, &s.doc).map_err(engine_error)?;
|
||||
}
|
||||
}
|
||||
for out in outputs {
|
||||
@@ -570,7 +678,7 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
} else {
|
||||
s.run("file.save", &json!({"path": out}))
|
||||
};
|
||||
r.map_err(|e| e.to_string())?;
|
||||
r.map_err(engine_error)?;
|
||||
eprintln!("wrote {out}");
|
||||
}
|
||||
Ok(())
|
||||
@@ -669,10 +777,12 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
let mut s = open(file, None)?;
|
||||
for spec in rest.iter().zip(rest.iter().skip(1)).filter(|(a, _)| *a == "--cmd").map(|(_, v)| v) {
|
||||
let (id, params) = match spec.split_once('=') {
|
||||
Some((id, p)) => (id.to_string(), serde_json::from_str::<Value>(p).map_err(|e| format!("{id}: bad JSON params: {e}"))?),
|
||||
Some((id, p)) => {
|
||||
(id.to_string(), serde_json::from_str::<Value>(p).map_err(|e| engine_error(format!("{id}: bad JSON params: {e}")))?)
|
||||
}
|
||||
None => (spec.clone(), json!({})),
|
||||
};
|
||||
s.run(&id, ¶ms).map_err(|e| format!("{id}: {e}"))?;
|
||||
s.run(&id, ¶ms).map_err(|e| engine_error(format!("{id}: {e}")))?;
|
||||
}
|
||||
let mut opts = wordcraft_zotero::client::Options::default();
|
||||
if let Some(p) = arg_value(&rest, "--port") {
|
||||
@@ -689,13 +799,13 @@ fn run<S: AsRef<OsStr>>(args: &[S]) -> Result<(), Failure> {
|
||||
}
|
||||
r
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
.map_err(engine_error)?;
|
||||
for a in &host.alerts {
|
||||
eprintln!("Zotero: {}", escape_peer(a));
|
||||
}
|
||||
eprintln!("{} calls, {}", out.calls, if out.completed { "completed" } else { "ended without completing (cancelled?)" });
|
||||
if let Some(o) = arg_value(&rest, "--save") {
|
||||
s.run("file.save", &json!({"path": o})).map_err(|e| e.to_string())?;
|
||||
s.run("file.save", &json!({"path": o})).map_err(engine_error)?;
|
||||
eprintln!("wrote {o}");
|
||||
}
|
||||
Ok(())
|
||||
@@ -719,22 +829,62 @@ fn clip(s: &str) -> String {
|
||||
/// overrides are shown as escapes (`\\`, `\n`, `\u{1b}`), so it can't end its line, drive the
|
||||
/// terminal or pass for other output.
|
||||
fn escape_peer(s: &str) -> String {
|
||||
escape_text(s, true)
|
||||
}
|
||||
|
||||
/// Whether `c` must not reach a terminal as itself: a control character, a line or paragraph
|
||||
/// separator, a bidi control, or one that shows nothing (soft hyphen, zero-width characters, word
|
||||
/// joiners, the byte order mark, tag characters), which can hide or reorder what a line says.
|
||||
fn is_unsafe_char(c: char) -> bool {
|
||||
c.is_control()
|
||||
|| matches!(
|
||||
c,
|
||||
'\u{00ad}'
|
||||
| '\u{061c}'
|
||||
| '\u{200b}'..='\u{200f}'
|
||||
| '\u{2028}'..='\u{202e}'
|
||||
| '\u{2060}'..='\u{2064}'
|
||||
| '\u{2066}'..='\u{2069}'
|
||||
| '\u{feff}'
|
||||
| '\u{e0000}'..='\u{e007f}'
|
||||
)
|
||||
}
|
||||
|
||||
/// [`escape_peer`], with backslashes escaped only when `backslash` (error messages quote Windows
|
||||
/// paths, and a backslash can't forge or hide anything).
|
||||
fn escape_text(s: &str, backslash: bool) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'\\' if backslash => out.push_str("\\\\"),
|
||||
'\n' => out.push_str("\\n"),
|
||||
'\r' => out.push_str("\\r"),
|
||||
'\t' => out.push_str("\\t"),
|
||||
c if c.is_control() || matches!(c, '\u{061c}' | '\u{200e}' | '\u{200f}' | '\u{2028}'..='\u{202e}' | '\u{2066}'..='\u{2069}') => {
|
||||
out.extend(c.escape_unicode())
|
||||
}
|
||||
c if is_unsafe_char(c) => out.extend(c.escape_unicode()),
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A JSON line safe to print: serde escapes control characters below U+0020 but not the other
|
||||
/// characters [`is_unsafe_char`] names, which a crafted file could put in a report. They become
|
||||
/// `\uXXXX` escapes (a surrogate pair above U+FFFF), so the line parses to the same value and
|
||||
/// can't drive or spoof a terminal.
|
||||
fn json_line_safe(json: &str) -> String {
|
||||
let mut out = String::with_capacity(json.len());
|
||||
for c in json.chars() {
|
||||
if is_unsafe_char(c) {
|
||||
for unit in c.encode_utf16(&mut [0u16; 2]) {
|
||||
out.push_str(&format!("\\u{unit:04x}"));
|
||||
}
|
||||
} else {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn parity_markdown(p: &Value) -> String {
|
||||
let mut s = String::from(
|
||||
"# WordCraft feature parity\n\nGenerated by `cargo xtask parity` (`wordcraft-cli parity --markdown`): the word-processor feature catalog (`crates/engine/src/catalog.rs`) compared with the live command registry.\n\n",
|
||||
@@ -775,6 +925,10 @@ fn main() -> ExitCode {
|
||||
eprintln!("wordcraft-cli: {e}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
Err(Failure::Strict(report)) => {
|
||||
eprintln!("{report}");
|
||||
ExitCode::from(3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -211,6 +211,10 @@ fn only_the_opened_file_may_be_other_than_unicode() {
|
||||
let ok = |raw: Vec<OsString>| text_args(&raw).unwrap().1;
|
||||
assert_eq!(ok(vec!["zotero".into(), "refresh".into(), latin1()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["render".into(), latin1(), "out.png".into(), "--page".into(), "2".into()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["render".into(), "--page".into(), "2".into(), latin1(), "out.png".into()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["convert".into(), "--format".into(), "flatopc".into(), latin1(), "out.xml".into()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["info".into(), "--password".into(), "secret".into(), latin1()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["zotero".into(), "--port".into(), "23119".into(), "refresh".into(), latin1()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["run".into(), "--print".into(), "--file".into(), latin1()]), Some(PathBuf::from(latin1())));
|
||||
assert_eq!(ok(vec!["info".into(), "in.docx".into()]), None);
|
||||
// Every command that opens a file reaches the open with it (no such file here), so
|
||||
@@ -228,6 +232,7 @@ fn only_the_opened_file_may_be_other_than_unicode() {
|
||||
for (raw, n) in [
|
||||
(vec!["zotero".into(), latin1(), "in.docx".into()], 2),
|
||||
(vec!["render".into(), "in.docx".into(), latin1()], 3),
|
||||
(vec!["render".into(), "--page".into(), latin1(), "in.docx".into(), "out.png".into()], 3),
|
||||
(vec!["run".into(), "--save".into(), latin1()], 3),
|
||||
(vec!["commands".into(), latin1()], 2),
|
||||
] {
|
||||
@@ -559,3 +564,19 @@ fn script_parse_resolves_paths_against_the_base() {
|
||||
assert_eq!(s.save, vec!["o.docx".to_string()]);
|
||||
assert_eq!(s.commands.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invisible_characters_are_escaped_and_json_lines_still_parse_back() {
|
||||
let hidden = "a\u{ad}b\u{200b}c\u{200d}d\u{2060}e\u{2064}f\u{feff}g\u{e0041}h\u{e007f}i\u{85}j\u{202e}k";
|
||||
let shown = escape_peer(hidden);
|
||||
assert!(!shown.chars().any(is_unsafe_char), "{shown}");
|
||||
assert!(shown.contains("\\u{e0041}") && shown.contains("\\u{ad}"), "{shown}");
|
||||
// Errors keep a Windows path readable.
|
||||
assert_eq!(escape_text("C:\\dir\\a.docx", false), "C:\\dir\\a.docx");
|
||||
|
||||
let value = json!({"part": hidden, "plain": "ünïcode ok"});
|
||||
let line = json_line_safe(&value.to_string());
|
||||
assert!(!line.chars().any(is_unsafe_char), "{line}");
|
||||
assert!(line.contains("\\ud800") || line.contains("\\udb40"), "{line}");
|
||||
assert_eq!(serde_json::from_str::<Value>(&line).unwrap(), value);
|
||||
}
|
||||
|
||||
@@ -47,3 +47,65 @@ fn convert_refresh_is_opt_in() {
|
||||
}
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// Converting a protected document with `--update-fields` skips updating fields with a warning,
|
||||
/// retains the cached field result, and preserves the protection mode on output.
|
||||
#[test]
|
||||
fn convert_protected_document_with_update_fields_skips_with_warning() {
|
||||
let dir = std::env::temp_dir().join(format!("wc-convert-fields-prot-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
|
||||
for (mode, expected_warning) in [("readOnly", "protected"), ("forms", "protected"), ("comments", "comments")] {
|
||||
let mut doc = Document::from_text("Contents\nTarget");
|
||||
doc.para_mut(StoryRef::Body, &Path::top(1)).unwrap().props.page_break_before = Some(true);
|
||||
doc.insert_object(&Pos::body(1, 0), InlineObject::BookmarkStart { name: "target".into() }, &Default::default()).unwrap();
|
||||
doc.insert_object(
|
||||
&Pos::body(0, 0),
|
||||
InlineObject::Field {
|
||||
instr: "PAGEREF target".into(),
|
||||
result: "104".into(),
|
||||
locked: false,
|
||||
dirty: true,
|
||||
code: false,
|
||||
form: String::new(),
|
||||
},
|
||||
&Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
doc.settings.protection = Some(mode.into());
|
||||
let input = dir.join(format!("in_{mode}.json"));
|
||||
std::fs::write(&input, serde_json::to_vec(&doc).unwrap()).unwrap();
|
||||
let docx = dir.join(format!("in_{mode}.docx"));
|
||||
let make = Command::new(env!("CARGO_BIN_EXE_wordcraft-cli")).arg("convert").arg(&input).arg(&docx).output().unwrap();
|
||||
assert!(make.status.success(), "{}", String::from_utf8_lossy(&make.stderr));
|
||||
|
||||
let output = dir.join(format!("out_{mode}.json"));
|
||||
let _ = std::fs::remove_file(&output);
|
||||
|
||||
let mut cmd = Command::new(env!("CARGO_BIN_EXE_wordcraft-cli"));
|
||||
cmd.arg("convert").arg(&docx).arg(&output).arg("--update-fields");
|
||||
let run = cmd.output().unwrap();
|
||||
let stderr = String::from_utf8_lossy(&run.stderr);
|
||||
assert!(run.status.success(), "exit status was failure for mode {mode}: {stderr}");
|
||||
assert!(stderr.contains("warning:"), "stderr missing warning for mode {mode}: {stderr}");
|
||||
assert!(stderr.contains(expected_warning), "stderr missing '{expected_warning}' for mode {mode}: {stderr}");
|
||||
assert!(output.exists(), "output file was not created for mode {mode}");
|
||||
|
||||
let out: Document = serde_json::from_slice(&std::fs::read(&output).unwrap()).unwrap();
|
||||
let results: Vec<_> = out
|
||||
.para(StoryRef::Body, &Path::top(0))
|
||||
.unwrap()
|
||||
.objects
|
||||
.iter()
|
||||
.filter_map(|o| match o {
|
||||
InlineObject::Field { result, .. } => Some(result.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
// Result should remain "104" (cached) because the field could not be updated under protection
|
||||
assert_eq!(results, vec!["104"], "field result was unexpectedly modified for mode {mode}");
|
||||
assert_eq!(out.settings.protection.as_deref(), Some(mode), "protection mode was not preserved for mode {mode}");
|
||||
}
|
||||
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -6,4 +6,5 @@ mod help;
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
mod non_utf8_args;
|
||||
mod picture_then_typing;
|
||||
mod strict;
|
||||
mod text_tracked;
|
||||
|
||||
@@ -53,6 +53,15 @@ fn the_file_a_command_opens_may_have_any_name() {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_option_before_the_input_keeps_its_native_name() {
|
||||
let (dir, file) = folder("option-first");
|
||||
let saved = dir.join("out.xml");
|
||||
ok(&cli(&["convert".into(), "--format".into(), "flatopc".into(), file, saved.clone().into()]));
|
||||
assert!(std::fs::read_to_string(saved).unwrap().contains("Bonjour"));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn any_other_such_argument_is_an_error_naming_it() {
|
||||
let (dir, file) = folder("error");
|
||||
|
||||
@@ -0,0 +1,493 @@
|
||||
//! `convert --strict` (#6005): exit 3 and a report when a PDF or PNG would silently lack a
|
||||
//! character or a picture, nothing written; exit 0 otherwise.
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
|
||||
use wordcraft_doc::Document;
|
||||
use wordcraft_fonts::FontDb;
|
||||
|
||||
const NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture""#;
|
||||
const REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
|
||||
const RELS_NS: &str = "http://schemas.openxmlformats.org/package/2006/relationships";
|
||||
|
||||
fn dir(name: &str) -> PathBuf {
|
||||
let d = std::env::temp_dir().join(format!("wc-cli-strict-{name}-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
fn convert(input: &Path, output: &Path, strict: bool) -> Output {
|
||||
let mut cmd = Command::new(env!("CARGO_BIN_EXE_wordcraft-cli"));
|
||||
cmd.arg("convert").arg(input).arg(output);
|
||||
if strict {
|
||||
cmd.arg("--strict");
|
||||
}
|
||||
cmd.output().unwrap()
|
||||
}
|
||||
|
||||
fn stderr(o: &Output) -> String {
|
||||
String::from_utf8_lossy(&o.stderr).into_owned()
|
||||
}
|
||||
|
||||
/// The report's JSON line: the last line of stderr.
|
||||
fn report_json(o: &Output) -> serde_json::Value {
|
||||
let text = stderr(o);
|
||||
let last = text.lines().last().unwrap_or_default();
|
||||
serde_json::from_str(last).unwrap_or_else(|e| panic!("last stderr line is not JSON ({e}): {text}"))
|
||||
}
|
||||
|
||||
fn json_input(d: &Path, text: &str) -> PathBuf {
|
||||
let path = d.join("in.json");
|
||||
std::fs::write(&path, serde_json::to_vec(&Document::from_text(text)).unwrap()).unwrap();
|
||||
path
|
||||
}
|
||||
|
||||
/// Built without craft-fonts, the host's own fonts decide what is drawable: skip, as the layout
|
||||
/// tests do.
|
||||
fn without_fonts() -> bool {
|
||||
let missing = option_env!("CRAFT_FONTS_DIR").is_none_or(str::is_empty);
|
||||
if missing {
|
||||
eprintln!("skipped: built without craft-fonts (set CRAFT_FONTS_DIR to a checkout)");
|
||||
}
|
||||
missing
|
||||
}
|
||||
|
||||
/// A code point in unassigned plane 4 that no face of the host covers.
|
||||
fn uncovered() -> char {
|
||||
let db = FontDb::global();
|
||||
let primary = wordcraft_fonts::word::resolve(&Document::default().settings.minor_font, false, false).face;
|
||||
(0x40000u32..0x4FFF0)
|
||||
.filter_map(char::from_u32)
|
||||
.find(|c| !primary.covers(*c) && db.fallback_styled(*c, primary.id(), false, false).is_none() && db.fallback_for(*c, primary.id()).is_none())
|
||||
.expect("the host has faces covering all of plane 4")
|
||||
}
|
||||
|
||||
fn png() -> Vec<u8> {
|
||||
wordcraft_geom::picture::encode_format(
|
||||
&image::DynamicImage::from(image::RgbaImage::from_pixel(4, 4, image::Rgba([20, 90, 200, 255])).clone()),
|
||||
image::ImageFormat::Png,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn pic(rid: &str) -> String {
|
||||
format!(
|
||||
r#"<w:r><w:drawing><wp:inline><wp:extent cx="508000" cy="508000"/><wp:docPr id="1" name="P"/><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic><pic:nvPicPr><pic:cNvPr id="0" name="p"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="{rid}"/></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="508000" cy="508000"/></a:xfrm><a:prstGeom prst="rect"/></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r>"#
|
||||
)
|
||||
}
|
||||
|
||||
/// A DOCX whose header holds a picture of `header_media` (`word/media/image2.bin`).
|
||||
fn docx_with_header_picture(header_media: &[u8]) -> Vec<u8> {
|
||||
let doc = format!(
|
||||
r#"<w:document {NS}><w:body><w:p><w:r><w:t>text</w:t></w:r></w:p><w:sectPr><w:headerReference w:type="default" r:id="rIdH"/></w:sectPr></w:body></w:document>"#
|
||||
);
|
||||
let drels = format!(r#"<Relationships xmlns="{RELS_NS}"><Relationship Id="rIdH" Type="{REL}/header" Target="header2.xml"/></Relationships>"#);
|
||||
let hrels = format!(r#"<Relationships xmlns="{RELS_NS}"><Relationship Id="rIdI" Type="{REL}/image" Target="media/image2.bin"/></Relationships>"#);
|
||||
let hdr = format!(r#"<w:hdr {NS}><w:p>{}</w:p></w:hdr>"#, pic("rIdI"));
|
||||
package(
|
||||
&doc,
|
||||
&drels,
|
||||
&[("word/header2.xml", hdr.as_bytes()), ("word/_rels/header2.xml.rels", hrels.as_bytes()), ("word/media/image2.bin", header_media)],
|
||||
)
|
||||
}
|
||||
|
||||
/// A DOCX whose body picture points outside the file, at `target`.
|
||||
fn docx_with_external_picture(target: &str) -> Vec<u8> {
|
||||
let doc = format!(r#"<w:document {NS}><w:body><w:p>{}<w:r><w:t>text</w:t></w:r></w:p></w:body></w:document>"#, pic("rIdX"));
|
||||
let drels = format!(
|
||||
r#"<Relationships xmlns="{RELS_NS}"><Relationship Id="rIdX" Type="{REL}/image" Target="{target}" TargetMode="External"/></Relationships>"#
|
||||
);
|
||||
package(&doc, &drels, &[])
|
||||
}
|
||||
|
||||
fn package(document: &str, document_rels: &str, extra: &[(&str, &[u8])]) -> Vec<u8> {
|
||||
let rels = format!(
|
||||
r#"<Relationships xmlns="{RELS_NS}"><Relationship Id="rId1" Type="{REL}/officeDocument" Target="word/document.xml"/></Relationships>"#
|
||||
);
|
||||
let types = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="png" ContentType="image/png"/><Default Extension="bin" ContentType="application/octet-stream"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>"#;
|
||||
let mut zip = craft_zip::Writer::new();
|
||||
let mut parts: Vec<(&str, &[u8])> = vec![
|
||||
("[Content_Types].xml", types.as_bytes()),
|
||||
("_rels/.rels", rels.as_bytes()),
|
||||
("word/document.xml", document.as_bytes()),
|
||||
("word/_rels/document.xml.rels", document_rels.as_bytes()),
|
||||
];
|
||||
parts.extend_from_slice(extra);
|
||||
for (name, bytes) in parts {
|
||||
zip.add(name, bytes, craft_zip::Compression::Deflated).unwrap();
|
||||
}
|
||||
zip.finish().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multilingual_names_pass_strict() {
|
||||
if without_fonts() {
|
||||
return;
|
||||
}
|
||||
let names = "Łukasz Żmijewski\nGheorghe Ștefănescu\nKrejčí\nŞahın Gül\nLlŷr Grŵn\n王小明\n김민수\nสมชาย ใจดี\nराम शर्मा";
|
||||
let d = dir("names");
|
||||
let input = json_input(&d, names);
|
||||
for out in ["names.pdf", "names.png"] {
|
||||
let out = d.join(out);
|
||||
let r = convert(&input, &out, true);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
assert!(out.exists());
|
||||
}
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_uncovered_code_point_fails_strict_with_a_report_and_no_file() {
|
||||
let c = uncovered();
|
||||
let cp = format!("U+{:04X}", c as u32);
|
||||
let d = dir("uncovered");
|
||||
let input = json_input(&d, &format!("a{c}b{c}{c}"));
|
||||
for out in ["out.pdf", "out.png"] {
|
||||
let out = d.join(out);
|
||||
let r = convert(&input, &out, true);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
let text = stderr(&r);
|
||||
assert!(text.contains(&format!("wordcraft-cli: strict: 1 character cannot be drawn: {cp} (×3)")), "{text}");
|
||||
let json = report_json(&r);
|
||||
assert_eq!(json["schema"], 1);
|
||||
assert_eq!(json["strict"], "failed");
|
||||
assert_eq!(json["missing_glyphs"][0]["cp"], cp.as_str());
|
||||
assert_eq!(json["missing_glyphs"][0]["count"], 3);
|
||||
assert!(!out.exists(), "{} was written", out.display());
|
||||
eprintln!("{text}");
|
||||
// Without --strict the same input converts, as before.
|
||||
let r = convert(&input, &out, false);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
assert!(out.exists());
|
||||
std::fs::remove_file(&out).unwrap();
|
||||
}
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_real_png_in_a_header_passes_strict() {
|
||||
let d = dir("header-png");
|
||||
let input = d.join("in.docx");
|
||||
std::fs::write(&input, docx_with_header_picture(&png())).unwrap();
|
||||
for out in ["out.pdf", "out.png"] {
|
||||
let out = d.join(out);
|
||||
let r = convert(&input, &out, true);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
assert!(out.exists());
|
||||
}
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_corrupt_header_picture_fails_strict_naming_the_part_and_the_header() {
|
||||
let d = dir("header-corrupt");
|
||||
let input = d.join("in.docx");
|
||||
std::fs::write(&input, docx_with_header_picture(b"corrupt bytes, not a picture")).unwrap();
|
||||
for out in ["out.pdf", "out.png"] {
|
||||
let out = d.join(out);
|
||||
let r = convert(&input, &out, true);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
let text = stderr(&r);
|
||||
assert!(text.contains("word/media/image2.bin (header2.xml): unsupported format"), "{text}");
|
||||
let json = report_json(&r);
|
||||
assert_eq!(json["strict"], "failed");
|
||||
assert_eq!(
|
||||
json["missing_pictures"][0],
|
||||
serde_json::json!({"part": "word/media/image2.bin", "source_part": "word/header2.xml", "reason": "unsupported_format"})
|
||||
);
|
||||
assert!(!out.exists());
|
||||
eprintln!("{text}");
|
||||
}
|
||||
// Without --strict the grey box is drawn, as before.
|
||||
let out = d.join("lenient.pdf");
|
||||
assert_eq!(convert(&input, &out, false).status.code(), Some(0));
|
||||
assert!(out.exists());
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_external_picture_target_is_never_fetched_and_is_reported_under_strict() {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
listener.set_nonblocking(true).unwrap();
|
||||
let target = format!("http://{}/canary.png", listener.local_addr().unwrap());
|
||||
let d = dir("external");
|
||||
let input = d.join("in.docx");
|
||||
std::fs::write(&input, docx_with_external_picture(&target)).unwrap();
|
||||
|
||||
let lenient = convert(&input, &d.join("lenient.pdf"), false);
|
||||
assert_eq!(lenient.status.code(), Some(0), "{}", stderr(&lenient));
|
||||
let out = d.join("strict.pdf");
|
||||
let strict = convert(&input, &out, true);
|
||||
assert_eq!(strict.status.code(), Some(3), "{}", stderr(&strict));
|
||||
let json = report_json(&strict);
|
||||
assert_eq!(json["missing_pictures"][0]["reason"], "external_target");
|
||||
assert!(stderr(&strict).contains("external target"));
|
||||
assert!(!out.exists());
|
||||
|
||||
match listener.accept() {
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {}
|
||||
Ok((_, from)) => panic!("the converter connected to the external target from {from}"),
|
||||
Err(e) => panic!("{e}"),
|
||||
}
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_input_exits_1_with_the_usual_message_with_and_without_strict() {
|
||||
let d = dir("bad-input");
|
||||
let good = docx_with_header_picture(&png());
|
||||
let truncated = d.join("truncated.docx");
|
||||
std::fs::write(&truncated, &good[..good.len() / 2]).unwrap();
|
||||
let not_zip = d.join("not-zip.docx");
|
||||
std::fs::write(¬_zip, b"this is plainly not a zip archive").unwrap();
|
||||
for input in [&truncated, ¬_zip] {
|
||||
let plain = convert(input, &d.join("a.pdf"), false);
|
||||
let strict = convert(input, &d.join("b.pdf"), true);
|
||||
assert_eq!(plain.status.code(), Some(1), "{}", stderr(&plain));
|
||||
assert_eq!(strict.status.code(), Some(1), "{}", stderr(&strict));
|
||||
assert_eq!(stderr(&plain), stderr(&strict));
|
||||
assert!(stderr(&plain).starts_with("wordcraft-cli: "), "{}", stderr(&plain));
|
||||
assert!(!d.join("a.pdf").exists() && !d.join("b.pdf").exists());
|
||||
}
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_with_another_output_type_exits_1_before_the_input_is_read() {
|
||||
let d = dir("wrong-output");
|
||||
let missing = d.join("does-not-exist.docx");
|
||||
for out in ["out.docx", "out.txt", "out.odt"] {
|
||||
let r = convert(&missing, &d.join(out), true);
|
||||
assert_eq!(r.status.code(), Some(1));
|
||||
let text = stderr(&r);
|
||||
assert!(text.contains("--strict needs a .pdf or .png output"), "{text}");
|
||||
assert!(!d.join(out).exists());
|
||||
}
|
||||
// Without --strict the same command fails on the missing input.
|
||||
let r = convert(&missing, &d.join("out.docx"), false);
|
||||
assert_eq!(r.status.code(), Some(1));
|
||||
assert!(!stderr(&r).contains("--strict"));
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
fn render(input: &Path, output: &Path, args: &[&str]) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_wordcraft-cli")).arg("render").arg(input).arg(output).args(args).output().unwrap()
|
||||
}
|
||||
|
||||
/// Page 1 has `a{c}b`, page 2 plain text.
|
||||
fn two_page_input(d: &Path, text: &str) -> PathBuf {
|
||||
let mut doc = Document::from_text(&format!("{text}\nSecond page"));
|
||||
doc.para_mut(wordcraft_doc::StoryRef::Body, &wordcraft_doc::Path::top(1)).unwrap().props.page_break_before = Some(true);
|
||||
let path = d.join("two.json");
|
||||
std::fs::write(&path, serde_json::to_vec(&doc).unwrap()).unwrap();
|
||||
path
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_strict_refuses_an_uncovered_character_on_the_page_with_no_stdout_or_file() {
|
||||
let c = uncovered();
|
||||
let cp = format!("U+{:04X}", c as u32);
|
||||
let d = dir("render-glyph");
|
||||
let input = two_page_input(&d, &format!("a{c}b"));
|
||||
let out = d.join("p1.png");
|
||||
let r = render(&input, &out, &["--strict"]);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
assert!(r.stdout.is_empty(), "{}", String::from_utf8_lossy(&r.stdout));
|
||||
assert!(!out.exists());
|
||||
let text = stderr(&r);
|
||||
assert!(text.contains(&format!("wordcraft-cli: strict: 1 character cannot be drawn: {cp} (×1)")), "{text}");
|
||||
assert_eq!(report_json(&r)["missing_glyphs"][0]["cp"], cp.as_str());
|
||||
eprintln!("{text}");
|
||||
|
||||
// The character is not on page 2, and --scale does not matter to the check.
|
||||
let out2 = d.join("p2.png");
|
||||
let r = render(&input, &out2, &["--page", "2", "--strict", "--scale", "0.5"]);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
assert!(out2.exists());
|
||||
assert!(String::from_utf8_lossy(&r.stdout).contains("\"path\""));
|
||||
// And the same page 1 without --strict renders, with the usual stdout.
|
||||
let out3 = d.join("lenient.png");
|
||||
let r = render(&input, &out3, &[]);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
assert!(out3.exists());
|
||||
let json: serde_json::Value = serde_json::from_slice(&r.stdout).unwrap();
|
||||
assert_eq!(json["path"], out3.to_string_lossy().as_ref());
|
||||
assert!(json["width"].as_u64().is_some() && json["bytes"].as_u64().is_some());
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_strict_refuses_a_corrupt_header_picture_and_bad_input_still_exits_1() {
|
||||
let d = dir("render-pic");
|
||||
let input = d.join("in.docx");
|
||||
std::fs::write(&input, docx_with_header_picture(b"corrupt bytes, not a picture")).unwrap();
|
||||
let out = d.join("out.png");
|
||||
let r = render(&input, &out, &["--strict"]);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
assert!(r.stdout.is_empty());
|
||||
assert!(stderr(&r).contains("word/media/image2.bin (header2.xml)"), "{}", stderr(&r));
|
||||
assert!(!out.exists());
|
||||
|
||||
let bad = d.join("bad.docx");
|
||||
std::fs::write(&bad, b"this is plainly not a zip archive").unwrap();
|
||||
let (plain, strict) = (render(&bad, &out, &[]), render(&bad, &out, &["--strict"]));
|
||||
assert_eq!((plain.status.code(), strict.status.code()), (Some(1), Some(1)));
|
||||
assert_eq!(stderr(&plain), stderr(&strict));
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_picture_target_with_a_newline_and_a_bidi_override_cannot_forge_a_report_line() {
|
||||
let d = dir("escape");
|
||||
let input = d.join("in.docx");
|
||||
// XML character references put a newline and U+202E in the target.
|
||||
std::fs::write(&input, docx_with_external_picture("http://example.invalid/a wordcraft-cli: strict: forged‮gnp.png")).unwrap();
|
||||
let out = d.join("out.pdf");
|
||||
let r = convert(&input, &out, true);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
let text = stderr(&r);
|
||||
let lines: Vec<&str> = text.lines().collect();
|
||||
// One human line for the picture, then the JSON line.
|
||||
assert_eq!(lines.len(), 2, "{text}");
|
||||
assert!(lines[0].starts_with("wordcraft-cli: strict: 1 picture(s) left out: "), "{text}");
|
||||
assert!(lines[0].contains("\\nwordcraft-cli: strict: forged\\u{202e}gnp.png"), "{text}");
|
||||
assert!(!text.contains('\u{202e}'), "the raw bidi override reached the terminal");
|
||||
// The JSON keeps the raw value, escaped by the JSON encoder.
|
||||
let json = report_json(&r);
|
||||
let part = json["missing_pictures"][0]["part"].as_str().unwrap();
|
||||
assert!(part.contains('\n') && part.contains('\u{202e}'), "{part:?}");
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_with_strict_cannot_forge_an_error_line_either() {
|
||||
let d = dir("run-escape");
|
||||
let input = d.join("in.docx");
|
||||
std::fs::write(&input, docx_with_external_picture("http://example.invalid/a wordcraft-cli: strict: forged[2J‮gnp.png")).unwrap();
|
||||
let out = d.join("out.pdf");
|
||||
let cmd = format!("file.exportPdf={}", serde_json::json!({"path": out, "strict": true}));
|
||||
let r = Command::new(env!("CARGO_BIN_EXE_wordcraft-cli")).arg("run").arg("--file").arg(&input).arg("--cmd").arg(cmd).output().unwrap();
|
||||
assert_eq!(r.status.code(), Some(1));
|
||||
let text = stderr(&r);
|
||||
assert_eq!(text.lines().count(), 1, "{text}");
|
||||
assert!(text.contains("\\nwordcraft-cli: strict: forged") && text.contains("\\u{202e}"), "{text}");
|
||||
assert!(!text.contains('\u{1b}') && !text.contains('\u{202e}'), "{text:?}");
|
||||
assert!(!out.exists());
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn usage_errors_keep_their_line_breaks() {
|
||||
// Errors go to stderr with the usage after a blank line (`missing argument`, `unknown command`).
|
||||
for args in [&["frobnicate"][..], &["convert"][..]] {
|
||||
let r = Command::new(env!("CARGO_BIN_EXE_wordcraft-cli")).args(args).output().unwrap();
|
||||
let text = stderr(&r);
|
||||
assert_eq!(r.status.code(), Some(1), "{args:?}: {text}");
|
||||
assert!(text.lines().count() > 3, "{args:?}: {text}");
|
||||
assert!(text.lines().any(|l| l == "USAGE:"), "{args:?}: {text}");
|
||||
assert!(!text.contains("\\n"), "{args:?}: {text}");
|
||||
}
|
||||
// No arguments at all prints the usage as help, on stdout.
|
||||
let r = Command::new(env!("CARGO_BIN_EXE_wordcraft-cli")).output().unwrap();
|
||||
let all = format!("{}{}", String::from_utf8_lossy(&r.stdout), stderr(&r));
|
||||
assert!(all.lines().count() > 3 && all.lines().any(|l| l == "USAGE:"), "{all}");
|
||||
}
|
||||
|
||||
/// A minimal EMF (88-byte header, 100 mm square frame) with one EMR_EXTTEXTOUTW record showing
|
||||
/// `text` in the default font.
|
||||
fn emf_with_text(text: &str) -> Vec<u8> {
|
||||
let units: Vec<u16> = text.encode_utf16().collect();
|
||||
let mut rec = vec![0u8; 76];
|
||||
rec[0..4].copy_from_slice(&84u32.to_le_bytes());
|
||||
rec[36..40].copy_from_slice(&10i32.to_le_bytes());
|
||||
rec[40..44].copy_from_slice(&40i32.to_le_bytes());
|
||||
rec[44..48].copy_from_slice(&(units.len() as u32).to_le_bytes());
|
||||
rec[48..52].copy_from_slice(&76u32.to_le_bytes());
|
||||
rec.extend(units.iter().flat_map(|u| u.to_le_bytes()));
|
||||
while !rec.len().is_multiple_of(4) {
|
||||
rec.push(0);
|
||||
}
|
||||
let len = rec.len() as u32;
|
||||
rec[4..8].copy_from_slice(&len.to_le_bytes());
|
||||
let eof: Vec<u8> = [14u32.to_le_bytes(), 20u32.to_le_bytes(), [0; 4], [0; 4], 20u32.to_le_bytes()].concat();
|
||||
let total = 88 + rec.len() + eof.len();
|
||||
let mut h = vec![0u8; 88];
|
||||
h[0..4].copy_from_slice(&1u32.to_le_bytes());
|
||||
h[4..8].copy_from_slice(&88u32.to_le_bytes());
|
||||
h[8..24].copy_from_slice(&[0i32, 0, 100, 100].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>());
|
||||
h[24..40].copy_from_slice(&[0i32, 0, 10000, 10000].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>());
|
||||
h[40..44].copy_from_slice(&0x464D_4520u32.to_le_bytes());
|
||||
h[44..48].copy_from_slice(&0x0001_0000u32.to_le_bytes());
|
||||
h[48..52].copy_from_slice(&(total as u32).to_le_bytes());
|
||||
h[52..56].copy_from_slice(&2u32.to_le_bytes());
|
||||
h[56..58].copy_from_slice(&1u16.to_le_bytes());
|
||||
h[72..76].copy_from_slice(&1000i32.to_le_bytes());
|
||||
h[76..80].copy_from_slice(&1000i32.to_le_bytes());
|
||||
h[80..84].copy_from_slice(&250i32.to_le_bytes());
|
||||
h[84..88].copy_from_slice(&250i32.to_le_bytes());
|
||||
[h, rec, eof].concat()
|
||||
}
|
||||
|
||||
/// The text a metafile picture draws is checked like the page's own: a character no face covers
|
||||
/// refuses the export from `convert` and `render`, naming the code point, and nothing is written.
|
||||
#[test]
|
||||
fn a_character_in_a_metafile_pictures_text_fails_strict_like_the_pages_own() {
|
||||
if without_fonts() {
|
||||
return;
|
||||
}
|
||||
let c = uncovered();
|
||||
let cp = format!("U+{:04X}", c as u32);
|
||||
let d = dir("emf-text");
|
||||
let emf = emf_with_text(&format!("ab{c}"));
|
||||
let input = d.join("in.docx");
|
||||
std::fs::write(&input, docx_with_header_picture(&emf)).unwrap();
|
||||
for out in ["out.pdf", "out.png"] {
|
||||
let out = d.join(out);
|
||||
let r = convert(&input, &out, true);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
assert_eq!(report_json(&r)["missing_glyphs"][0]["cp"], cp.as_str(), "{}", stderr(&r));
|
||||
assert!(!out.exists(), "{} was written", out.display());
|
||||
// Without --strict it converts, the character a hollow box.
|
||||
let r = convert(&input, &out, false);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
std::fs::remove_file(&out).unwrap();
|
||||
}
|
||||
let out = d.join("p1.png");
|
||||
let r = render(&input, &out, &["--strict"]);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
assert!(r.stdout.is_empty() && !out.exists());
|
||||
assert_eq!(report_json(&r)["missing_glyphs"][0]["cp"], cp.as_str());
|
||||
// Text the faces cover passes.
|
||||
std::fs::write(&input, docx_with_header_picture(&emf_with_text("abc"))).unwrap();
|
||||
let r = convert(&input, &d.join("ok.pdf"), true);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
/// The page raster leaves out a metafile page background (it only draws raster ones), so a PNG
|
||||
/// would silently lack it: strict refuses that output, and a PDF, which draws it as vector, passes.
|
||||
#[test]
|
||||
fn a_metafile_page_background_fails_strict_for_png_but_not_for_pdf() {
|
||||
if without_fonts() {
|
||||
return;
|
||||
}
|
||||
let d = dir("emf-background");
|
||||
let mut doc = Document::from_text("text");
|
||||
doc.media.insert("bg.emf".into(), std::sync::Arc::new(emf_with_text("abc")));
|
||||
doc.settings.page_picture = Some(wordcraft_doc::PagePicture { media: "bg.emf".into(), fill_type: "frame".into(), title: String::new() });
|
||||
let input = d.join("in.json");
|
||||
std::fs::write(&input, serde_json::to_vec(&doc).unwrap()).unwrap();
|
||||
let png = d.join("out.png");
|
||||
let r = convert(&input, &png, true);
|
||||
assert_eq!(r.status.code(), Some(3), "{}", stderr(&r));
|
||||
let json = report_json(&r);
|
||||
assert_eq!(json["missing_pictures"][0]["part"], "bg.emf", "{}", stderr(&r));
|
||||
assert_eq!(json["missing_pictures"][0]["source_part"], "background");
|
||||
assert_eq!(json["missing_pictures"][0]["reason"], "unsupported_format");
|
||||
assert!(!png.exists());
|
||||
let pdf = d.join("out.pdf");
|
||||
let r = convert(&input, &pdf, true);
|
||||
assert_eq!(r.status.code(), Some(0), "{}", stderr(&r));
|
||||
assert!(pdf.exists());
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
@@ -17,7 +17,9 @@ path = "src/main.rs"
|
||||
wordcraft-engine = { workspace = true }
|
||||
wordcraft-ui-egui = { workspace = true }
|
||||
# The host messages (storytold/craft-libs): one contract and one bridge for the web builds.
|
||||
craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "2e2312ddaf21bc3d85cd3395d0955c7d239306f6", version = "0.1.0" }
|
||||
# The page command API (storytold/craft-libs#413): window.wordcraft over the control channel.
|
||||
craft-web-api = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" }
|
||||
craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" }
|
||||
egui = { workspace = true }
|
||||
log = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//! `window.wordcraft`: the page command API (`craft-web-api`, storytold/craft-libs#413) over the
|
||||
//! app's own control channel, the one the desktop control server drives (`docs/control-protocol.md`).
|
||||
//!
|
||||
//! The crate owns the JavaScript side (`request`, `execute`, `commands`, `docInspect`, `uiInspect`,
|
||||
//! `screenshot`, as promises with a timeout). This module is WordCraft's part: a [`Request`] that
|
||||
//! queues a [`ControlRequest`] for the app, which answers it on its next frame, and waits for the
|
||||
//! reply. WordCraft's control request has no abandon state, so a call the page gave up on still runs
|
||||
//! when the app reaches it, as it does for a desktop client that timed out.
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::sync::mpsc::{Receiver, TryRecvError, channel};
|
||||
|
||||
use craft_web_api::methods::Methods;
|
||||
use craft_web_api::page::{PageApi, Request};
|
||||
use serde_json::{Value, json};
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use wordcraft_ui_egui::ControlRequest;
|
||||
|
||||
/// The app id in `window.<app>`.
|
||||
const APP: &str = "wordcraft";
|
||||
|
||||
/// How often a waiting call looks for the app's reply, in milliseconds (about a frame).
|
||||
const POLL_MS: i32 = 16;
|
||||
|
||||
/// Put `window.wordcraft` on the page and return the queue the app drains each frame (hand it to
|
||||
/// `WordApp::with_control`). An existing `window.wordcraft` keeps the functions it already has.
|
||||
pub fn install(ctx: egui::Context) -> Receiver<ControlRequest> {
|
||||
let (queue, requests) = channel::<ControlRequest>();
|
||||
let request: Request = Rc::new(move |method, params| {
|
||||
let (control, reply) = ControlRequest::new(method, params);
|
||||
if queue.send(control).is_err() {
|
||||
return Box::pin(async { json!({"ok": false, "error": "the app is not running"}) });
|
||||
}
|
||||
ctx.request_repaint();
|
||||
Box::pin(wait(reply))
|
||||
});
|
||||
if let Err(e) = Rc::new(PageApi::new(APP, Methods::default(), request)).install() {
|
||||
log::warn!("couldn't install window.{APP}: {e:?}");
|
||||
}
|
||||
requests
|
||||
}
|
||||
|
||||
/// The app's reply, polled about once a frame.
|
||||
async fn wait(reply: Receiver<Value>) -> Value {
|
||||
loop {
|
||||
match reply.try_recv() {
|
||||
Ok(reply) => return reply,
|
||||
Err(TryRecvError::Empty) => sleep(POLL_MS).await,
|
||||
Err(TryRecvError::Disconnected) => return json!({"ok": false, "error": "the app dropped the request"}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn sleep(ms: i32) {
|
||||
let promise = js_sys::Promise::new(&mut |resolve, _| {
|
||||
if let Some(window) = web_sys::window() {
|
||||
let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, ms);
|
||||
}
|
||||
});
|
||||
let _ = JsFuture::from(promise).await;
|
||||
}
|
||||
@@ -20,6 +20,8 @@ mod prefs;
|
||||
#[cfg(any(target_arch = "wasm32", test))]
|
||||
mod save_fallback;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
mod api;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
mod host;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
||||
@@ -76,7 +76,8 @@ pub fn start() {
|
||||
if let Some(bridge) = &app_bridge {
|
||||
bridge.connect(&mut services, inbox.clone(), cc.egui_ctx.clone());
|
||||
}
|
||||
let mut app = WordApp::new(session, services);
|
||||
let control = crate::api::install(cc.egui_ctx.clone());
|
||||
let mut app = WordApp::new(session, services).with_control(control);
|
||||
// Pasted pictures wait apart from dropped files: the app inserts one only when the page is the edit target (#1349).
|
||||
app.services.pasted = Some(pasted);
|
||||
app.autosave = false;
|
||||
|
||||
@@ -64,6 +64,11 @@ wl-clipboard-rs = "0.9"
|
||||
# Another program's RTF on X11 (`text/rtf`), which arboard doesn't read. Already in the build
|
||||
# through arboard, at this version.
|
||||
x11rb = "0.13"
|
||||
# Check that the libraries winit's X11 backend opens are installed before choosing XWayland (see
|
||||
# `x11_libraries` in main.rs). Both are already in the build through winit's `x11` feature, at
|
||||
# these versions and with this feature.
|
||||
x11-dl = "2.21"
|
||||
xkbcommon-dl = { version = "0.4.2", features = ["x11"] }
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
# The upstream default-apps button opens Windows Settings; printing uses craft-print instead.
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
//! An AppImage's environment inherited from another program (#5653).
|
||||
//!
|
||||
//! The AppImage runtime gives the program it starts `APPIMAGE` (the image's file), `APPDIR` (where
|
||||
//! it is mounted), `ARGV0` and `OWD`, and whatever that program starts inherits them. So an AppImage
|
||||
//! app that opens a document through `xdg-open` starts an installed WordCraft with *its* `APPIMAGE`,
|
||||
//! and the self-update, which trusts that variable, replaces the other app's file with WordCraft.
|
||||
//!
|
||||
//! This copy is in the image only when its executable is under `APPDIR`. Otherwise the variables
|
||||
//! are another program's, and the process starts over without them, before anything reads them.
|
||||
//! (Removing them in place is `unsafe` under this edition, and `exec` keeps the process id, the
|
||||
//! arguments and the standard streams.)
|
||||
|
||||
use std::ffi::OsStr;
|
||||
use std::io::Write;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
/// What the AppImage runtime sets; the same list as the documented workaround on the issue.
|
||||
const RUNTIME_VARS: [&str; 4] = ["APPIMAGE", "APPDIR", "ARGV0", "OWD"];
|
||||
|
||||
/// Whether the executable runs from the image mounted at `appdir`. Anything unclear counts as not:
|
||||
/// the update then goes through the package or the plain file this copy really is, and at worst
|
||||
/// says it can't, where the other answer would overwrite a file that isn't WordCraft's.
|
||||
fn runs_from_image(exe: Option<&Path>, appdir: Option<&OsStr>) -> bool {
|
||||
let (Some(exe), Some(appdir)) = (exe, appdir) else { return false };
|
||||
let appdir = Path::new(appdir);
|
||||
if !appdir.is_absolute() {
|
||||
return false;
|
||||
}
|
||||
let Ok(appdir) = appdir.canonicalize() else { return false };
|
||||
// No image mounts at the root, where every program on the machine would count as inside it.
|
||||
appdir.parent().is_some() && exe.starts_with(&appdir)
|
||||
}
|
||||
|
||||
/// Starts this process over without another program's AppImage variables; returns only when there
|
||||
/// are none to drop (`None`) or starting over failed. Call it first, before any other thread starts.
|
||||
pub(crate) fn drop_inherited() -> Option<std::io::Error> {
|
||||
let appimage = std::env::var_os("APPIMAGE")?;
|
||||
let exe = std::env::current_exe().ok();
|
||||
if runs_from_image(exe.as_deref(), std::env::var_os("APPDIR").as_deref()) {
|
||||
return None;
|
||||
}
|
||||
// The logger isn't up yet, and wouldn't survive `exec`. Unlike `eprintln!`, never panics.
|
||||
let _ = writeln!(std::io::stderr(), "wordcraft: ignoring the AppImage {:?} inherited from another program", Path::new(&appimage));
|
||||
let mut args = std::env::args_os();
|
||||
// This file, whatever has happened to its path since it started.
|
||||
let mut again = Command::new("/proc/self/exe");
|
||||
if let Some(arg0) = args.next() {
|
||||
again.arg0(arg0);
|
||||
}
|
||||
again.args(args);
|
||||
for var in RUNTIME_VARS {
|
||||
again.env_remove(var);
|
||||
}
|
||||
Some(again.exec())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::runs_from_image;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// A folder of its own for one test, removed when it ends.
|
||||
struct Tmp(PathBuf);
|
||||
|
||||
impl Tmp {
|
||||
/// An empty folder named after `test` and this process, made afresh.
|
||||
fn new(test: &str) -> Tmp {
|
||||
let dir = std::env::temp_dir().join(format!("wordcraft-appimage-{test}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
Tmp(dir)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Tmp {
|
||||
/// Removes the folder and what the test left in it.
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// A mount with the executable inside it, by real path (`/tmp` may be a link).
|
||||
fn mount(root: &Path, name: &str) -> (PathBuf, PathBuf) {
|
||||
let dir = root.join(name);
|
||||
std::fs::create_dir_all(dir.join("usr/bin")).unwrap();
|
||||
let dir = dir.canonicalize().unwrap();
|
||||
let exe = dir.join("usr/bin/wordcraft");
|
||||
(dir, exe)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_executable_in_the_mount_is_the_image() {
|
||||
let tmp = Tmp::new("own");
|
||||
let (dir, exe) = mount(&tmp.0, ".mount_WordCr");
|
||||
assert!(runs_from_image(Some(&exe), Some(dir.as_os_str())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_link_to_the_mount_is_the_same_mount() {
|
||||
let tmp = Tmp::new("link");
|
||||
let (dir, exe) = mount(&tmp.0, ".mount_WordCr");
|
||||
let link = tmp.0.join("link");
|
||||
std::os::unix::fs::symlink(&dir, &link).unwrap();
|
||||
assert!(runs_from_image(Some(&exe), Some(link.as_os_str())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_installed_executable_is_not_in_another_programs_mount() {
|
||||
let tmp = Tmp::new("other");
|
||||
let (other, _) = mount(&tmp.0, ".mount_other");
|
||||
assert!(!runs_from_image(Some(Path::new("/usr/bin/wordcraft")), Some(other.as_os_str())));
|
||||
}
|
||||
|
||||
/// Paths compare by component: one mount's name can start with another's.
|
||||
#[test]
|
||||
fn a_mount_whose_name_starts_the_same_is_another_mount() {
|
||||
let tmp = Tmp::new("prefix");
|
||||
let (other, _) = mount(&tmp.0, ".mount_ab");
|
||||
let (_, exe) = mount(&tmp.0, ".mount_abc");
|
||||
assert!(!runs_from_image(Some(&exe), Some(other.as_os_str())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anything_unclear_is_not_the_image() {
|
||||
let tmp = Tmp::new("unclear");
|
||||
let (dir, exe) = mount(&tmp.0, ".mount_WordCr");
|
||||
// No `APPDIR`, one that's gone (the other program has exited) or empty.
|
||||
assert!(!runs_from_image(Some(&exe), None));
|
||||
assert!(!runs_from_image(Some(&exe), Some(tmp.0.join("gone").as_os_str())));
|
||||
assert!(!runs_from_image(Some(&exe), Some("".as_ref())));
|
||||
// The root, and a relative path that happens to lead here.
|
||||
assert!(!runs_from_image(Some(&exe), Some("/".as_ref())));
|
||||
assert!(!runs_from_image(Some(&exe), Some(".".as_ref())));
|
||||
// No executable path at all.
|
||||
assert!(!runs_from_image(None, Some(dir.as_os_str())));
|
||||
}
|
||||
}
|
||||
@@ -36,14 +36,15 @@ impl AppleEvents {
|
||||
|
||||
/// Open the documents and act on the quit requests that arrived since the last frame. A document
|
||||
/// opens like File › Open: beside the open ones, in place of an untouched blank one, or, when it
|
||||
/// is already open, by showing it. Quitting closes the window, which asks about every document
|
||||
/// is already open, by showing it; a Word template starts a new document based on it, as Word's
|
||||
/// default verb for one does (#3433). Quitting closes the window, which asks about every document
|
||||
/// with unsaved changes.
|
||||
pub fn poll(inbox: &Inbox, app: &mut WordApp, ctx: &egui::Context) {
|
||||
for e in inbox.drain() {
|
||||
match e {
|
||||
Event::Open(paths) => {
|
||||
for p in paths.iter().filter_map(|p| p.to_str()) {
|
||||
match app.open_file(p, None) {
|
||||
match app.open_from_platform(p, None) {
|
||||
// Leave Backstage, as opening from the Open dialog does.
|
||||
Ok(_) => app.ui.backstage = false,
|
||||
Err(e) => {
|
||||
|
||||
@@ -60,6 +60,7 @@ pub fn set_rich(text: &str, html: &str, rtf: &str, mathml: &str) -> bool {
|
||||
set
|
||||
}
|
||||
|
||||
/// Write HTML and plain text to the system clipboard via the cached writer slot, recording ownership.
|
||||
fn set_html_text(html: &str, text: &str) -> bool {
|
||||
WRITER.with(|w| {
|
||||
let Ok(mut slot) = w.try_borrow_mut() else { return false };
|
||||
@@ -68,7 +69,10 @@ fn set_html_text(html: &str, text: &str) -> bool {
|
||||
}
|
||||
let set = slot.as_mut().map(|c| if html.is_empty() { c.set_text(text) } else { c.set().html(html, Some(text)) });
|
||||
match set {
|
||||
Some(Ok(())) => true,
|
||||
Some(Ok(())) => {
|
||||
platform::record_own();
|
||||
true
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
log::warn!("clipboard: {e}");
|
||||
*slot = None;
|
||||
@@ -79,6 +83,74 @@ fn set_html_text(html: &str, text: &str) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
/// Put a copied picture on the clipboard: its pixels, with HTML and text beside it where the
|
||||
/// platform takes them. `false` when it couldn't be set.
|
||||
pub fn set_image(image: &wordcraft_ui_egui::system_clipboard::CopyImage, html: &str, text: &str) -> bool {
|
||||
if platform::set_image(image, html, text) {
|
||||
return true;
|
||||
}
|
||||
if !platform::can_set() {
|
||||
return false;
|
||||
}
|
||||
let set = set_image_arboard(image);
|
||||
if set {
|
||||
platform::after_set_image(image, html, text);
|
||||
}
|
||||
set
|
||||
}
|
||||
|
||||
/// Write image pixels to the clipboard through arboard.
|
||||
fn set_image_arboard(image: &wordcraft_ui_egui::system_clipboard::CopyImage) -> bool {
|
||||
WRITER.with(|w| {
|
||||
let Ok(mut slot) = w.try_borrow_mut() else { return false };
|
||||
if slot.is_none() {
|
||||
*slot = open();
|
||||
}
|
||||
let img = arboard::ImageData { width: image.width, height: image.height, bytes: std::borrow::Cow::Borrowed(&image.rgba) };
|
||||
let set = slot.as_mut().map(|c| c.set().image(img));
|
||||
match set {
|
||||
Some(Ok(())) => {
|
||||
platform::record_own();
|
||||
true
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
log::warn!("clipboard image: {e}");
|
||||
*slot = None;
|
||||
false
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the system clipboard still holds WordCraft's last copy.
|
||||
pub fn is_own() -> bool {
|
||||
platform::is_own()
|
||||
}
|
||||
|
||||
/// Wrap HTML in the Windows CF_HTML header format required by clipboard readers.
|
||||
///
|
||||
/// Offsets are byte offsets from the start of the payload; `EndHTML` excludes the trailing NUL.
|
||||
#[cfg(any(target_os = "windows", test))]
|
||||
fn wrap_cf_html(ctn: &str) -> Vec<u8> {
|
||||
let h_version = "Version:0.9";
|
||||
let h_start_html = "\r\nStartHTML:";
|
||||
let h_end_html = "\r\nEndHTML:";
|
||||
let h_start_frag = "\r\nStartFragment:";
|
||||
let h_end_frag = "\r\nEndFragment:";
|
||||
let c_start_frag = "\r\n<html>\r\n<body>\r\n<!--StartFragment-->\r\n";
|
||||
let c_end_frag = "\r\n<!--EndFragment-->\r\n</body>\r\n</html>\0";
|
||||
let h_len = h_version.len() + h_start_html.len() + 10 + h_end_html.len() + 10 + h_start_frag.len() + 10 + h_end_frag.len() + 10;
|
||||
let n_start_html = h_len + 2;
|
||||
let n_start_frag = h_len + c_start_frag.len();
|
||||
let n_end_frag = n_start_frag + ctn.len();
|
||||
let n_end_html = n_end_frag + c_end_frag.len() - 1;
|
||||
let header = format!(
|
||||
"{h_version}{h_start_html}{n_start_html:010}{h_end_html}{n_end_html:010}{h_start_frag}{n_start_frag:010}{h_end_frag}{n_end_frag:010}{c_start_frag}{ctn}{c_end_frag}"
|
||||
);
|
||||
header.into_bytes()
|
||||
}
|
||||
|
||||
/// The clipboard's text.
|
||||
pub fn text() -> Option<String> {
|
||||
open()?.get_text().ok()
|
||||
@@ -108,8 +180,8 @@ pub fn formats() -> Vec<String> {
|
||||
|
||||
/// The picture on the clipboard: copied files, else image pixels, else (Wayland) an encoded
|
||||
/// picture in a format arboard doesn't decode. A picture that is there but can't be read says
|
||||
/// why (#833). The UI checks the sizes before inserting anything; on Windows they are checked
|
||||
/// from the header before anything is decoded (#3623).
|
||||
/// why (#833). The UI checks the sizes before inserting anything; on Windows and Wayland they are
|
||||
/// checked from the header before anything is decoded (#3623).
|
||||
pub fn picture() -> Option<ClipboardPicture> {
|
||||
let mut clipboard = match arboard::Clipboard::new() {
|
||||
Ok(c) => c,
|
||||
@@ -136,6 +208,10 @@ fn decoded_picture(_: &mut arboard::Clipboard) -> Option<ClipboardPicture> {
|
||||
/// The picture arboard decodes from the clipboard, else an encoded one read past it.
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn decoded_picture(clipboard: &mut arboard::Clipboard) -> Option<ClipboardPicture> {
|
||||
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))))]
|
||||
if let Some(picture) = platform::picture() {
|
||||
return Some(picture);
|
||||
}
|
||||
match clipboard.get_image() {
|
||||
Ok(image) => Some(ClipboardPicture::Pixels { width: image.width, height: image.height, rgba: image.bytes.into_owned() }),
|
||||
Err(arboard::Error::ContentNotAvailable) => platform::encoded_picture().map(ClipboardPicture::Encoded),
|
||||
@@ -160,6 +236,7 @@ mod platform {
|
||||
use std::io::Read;
|
||||
|
||||
use wl_clipboard_rs::paste::{ClipboardType, MimeType, Seat, get_contents, get_mime_types_ordered};
|
||||
use wordcraft_ui_egui::paste_picture::ClipboardPicture;
|
||||
|
||||
fn wayland() -> bool {
|
||||
std::env::var_os("WAYLAND_DISPLAY").is_some()
|
||||
@@ -180,6 +257,39 @@ mod platform {
|
||||
get_mime_types_ordered(ClipboardType::Regular, Seat::Unspecified).unwrap_or_default()
|
||||
}
|
||||
|
||||
const OWNER_MIME: &str = "application/x-wordcraft-owner";
|
||||
|
||||
/// Process-unique ownership token placed into Wayland clipboard offers.
|
||||
fn owner_token() -> [u8; 16] {
|
||||
static TOKEN: std::sync::OnceLock<[u8; 16]> = std::sync::OnceLock::new();
|
||||
*TOKEN.get_or_init(|| {
|
||||
let mut b = [0u8; 16];
|
||||
let _ = getrandom::fill(&mut b);
|
||||
b
|
||||
})
|
||||
}
|
||||
|
||||
static X11_OWNER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||
|
||||
/// Record clipboard ownership for X11 selection tracking.
|
||||
pub fn record_own() {
|
||||
if !wayland() {
|
||||
let win = x11::owner_window().unwrap_or(0);
|
||||
X11_OWNER.store(win, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether WordCraft still owns the Wayland offer or X11 selection.
|
||||
pub fn is_own() -> bool {
|
||||
if wayland() {
|
||||
if formats().iter().any(|m| m == OWNER_MIME) { read(OWNER_MIME, 16).is_some_and(|b| b == owner_token()) } else { false }
|
||||
} else {
|
||||
let current = x11::owner_window().unwrap_or(0);
|
||||
let recorded = X11_OWNER.load(std::sync::atomic::Ordering::Relaxed);
|
||||
recorded != 0 && current == recorded
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether arboard sets the clipboard natively: X11, or Wayland with data-control.
|
||||
pub fn can_set() -> bool {
|
||||
!wayland()
|
||||
@@ -206,6 +316,7 @@ mod platform {
|
||||
sources.push(offer(mime, data));
|
||||
}
|
||||
}
|
||||
sources.push(MimeSource { source: Source::Bytes(owner_token().to_vec().into()), mime_type: MimeType::Specific(OWNER_MIME.into()) });
|
||||
match Options::new().copy_multi(sources) {
|
||||
Ok(()) => true,
|
||||
Err(e) => {
|
||||
@@ -215,6 +326,37 @@ mod platform {
|
||||
}
|
||||
}
|
||||
|
||||
/// Put a copied picture on the Wayland clipboard alongside HTML, text, and ownership token.
|
||||
pub fn set_image(image: &wordcraft_ui_egui::system_clipboard::CopyImage, html: &str, text: &str) -> bool {
|
||||
use wl_clipboard_rs::copy::{MimeSource, MimeType, Options, Source};
|
||||
|
||||
if !wayland() {
|
||||
return false;
|
||||
}
|
||||
let Ok(png) = wordcraft_ui_egui::paste_picture::png_from_rgba(image.width, image.height, image.rgba.clone()) else {
|
||||
return false;
|
||||
};
|
||||
let offer = |mime: &str, data: &str| MimeSource { source: Source::Bytes(data.as_bytes().into()), mime_type: MimeType::Specific(mime.into()) };
|
||||
let mut sources = vec![MimeSource { source: Source::Bytes(png.into()), mime_type: MimeType::Specific("image/png".into()) }];
|
||||
if !text.is_empty() {
|
||||
sources.push(offer("text/plain;charset=utf-8", text));
|
||||
}
|
||||
if !html.is_empty() {
|
||||
sources.push(offer("text/html", html));
|
||||
}
|
||||
sources.push(MimeSource { source: Source::Bytes(owner_token().to_vec().into()), mime_type: MimeType::Specific(OWNER_MIME.into()) });
|
||||
match Options::new().copy_multi(sources) {
|
||||
Ok(()) => true,
|
||||
Err(e) => {
|
||||
log::warn!("clipboard image: {e}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional formats to register after setting image via arboard on Unix.
|
||||
pub fn after_set_image(_image: &wordcraft_ui_egui::system_clipboard::CopyImage, _html: &str, _text: &str) {}
|
||||
|
||||
/// Wayland copies carry their RTF already; X11 ones can't (see the module docs).
|
||||
pub fn add_rtf(_rtf: &str) {}
|
||||
|
||||
@@ -341,6 +483,30 @@ mod platform {
|
||||
let reply = r.convert(target, cap, true)?;
|
||||
(reply.type_ != incr).then_some(reply.value)
|
||||
}
|
||||
|
||||
/// Query X11 for the current window id owning the CLIPBOARD selection.
|
||||
pub fn owner_window() -> Option<Window> {
|
||||
let (conn, _) = x11rb::connect(None).ok()?;
|
||||
let clipboard = atom(&conn, "CLIPBOARD")?;
|
||||
let reply = conn.get_selection_owner(clipboard).ok()?.reply().ok()?;
|
||||
(reply.owner != x11rb::NONE).then_some(reply.owner)
|
||||
}
|
||||
}
|
||||
|
||||
/// The Wayland clipboard's picture, read encoded under the paste's byte cap and refused from
|
||||
/// its header when a paste can't take its size, before anything decodes it. arboard decodes
|
||||
/// and converts the PNG to RGBA with no size limits. `None` when Wayland offers no picture
|
||||
/// (or isn't reachable): arboard is asked then, which also covers X11.
|
||||
pub fn picture() -> Option<ClipboardPicture> {
|
||||
if !wayland() {
|
||||
return None;
|
||||
}
|
||||
let offered = formats();
|
||||
let mime = super::PICTURE_MIMES.into_iter().find(|m| offered.iter().any(|o| o == m))?;
|
||||
Some(match read(mime, wordcraft_ui_egui::paste_picture::MAX_FILE_BYTES) {
|
||||
Some(bytes) => wordcraft_ui_egui::paste_picture::encoded_picture(bytes),
|
||||
None => ClipboardPicture::Unreadable("the clipboard picture is larger than 200 MB, or couldn't be read".into()),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -415,6 +581,40 @@ mod platform {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
const OWNER_FORMAT: &str = "WordCraft Copy Owner";
|
||||
|
||||
/// Process-unique ownership token placed into Windows clipboard marker.
|
||||
fn owner_token() -> [u8; 16] {
|
||||
static TOKEN: std::sync::OnceLock<[u8; 16]> = std::sync::OnceLock::new();
|
||||
*TOKEN.get_or_init(|| {
|
||||
let mut b = [0u8; 16];
|
||||
let _ = getrandom::fill(&mut b);
|
||||
b
|
||||
})
|
||||
}
|
||||
|
||||
/// Record clipboard ownership for Windows clipboard.
|
||||
pub fn record_own() {
|
||||
let Some(format) = raw::register_format(OWNER_FORMAT) else { return };
|
||||
let Ok(_open) = Clipboard::new_attempts(10) else { return };
|
||||
let _ = raw::set_without_clear(format.get(), &owner_token());
|
||||
}
|
||||
|
||||
/// Whether WordCraft still owns the Windows clipboard contents.
|
||||
pub fn is_own() -> bool {
|
||||
let Some(format) = raw::register_format(OWNER_FORMAT) else { return false };
|
||||
let Ok(_open) = Clipboard::new_attempts(10) else { return false };
|
||||
if !raw::is_format_avail(format.get()) {
|
||||
return false;
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
if raw::get_vec(format.get(), &mut out).is_err() {
|
||||
return false;
|
||||
}
|
||||
out == owner_token()
|
||||
}
|
||||
|
||||
/// Windows supports clipboard setting via arboard and clipboard_win.
|
||||
pub fn can_set() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -424,6 +624,29 @@ mod platform {
|
||||
false
|
||||
}
|
||||
|
||||
/// Windows sets the primary image through arboard and supplements via [`after_set_image`].
|
||||
pub fn set_image(_image: &wordcraft_ui_egui::system_clipboard::CopyImage, _html: &str, _text: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Add PNG and CF_HTML formats alongside arboard's image, then mark ownership.
|
||||
pub fn after_set_image(image: &wordcraft_ui_egui::system_clipboard::CopyImage, html: &str, _text: &str) {
|
||||
if let Ok(png) = wordcraft_ui_egui::paste_picture::png_from_rgba(image.width, image.height, image.rgba.clone())
|
||||
&& let Some(format) = raw::register_format("PNG")
|
||||
&& let Ok(_open) = Clipboard::new_attempts(10)
|
||||
{
|
||||
let _ = raw::set_without_clear(format.get(), &png);
|
||||
}
|
||||
if !html.is_empty()
|
||||
&& let Some(format) = raw::register_format("HTML Format")
|
||||
&& let Ok(_open) = Clipboard::new_attempts(10)
|
||||
{
|
||||
let cf = super::wrap_cf_html(html);
|
||||
let _ = raw::set_without_clear(format.get(), &cf);
|
||||
}
|
||||
record_own();
|
||||
}
|
||||
|
||||
/// Add "Rich Text Format" to what arboard just set, NUL-terminated as is usual.
|
||||
pub fn add_rtf(rtf: &str) {
|
||||
let Some(format) = raw::register_format("Rich Text Format") else { return };
|
||||
@@ -466,19 +689,38 @@ mod platform {
|
||||
/// RTF's pasteboard type (`NSPasteboardTypeRTF`, whose extern static needs unsafe to read).
|
||||
const RTF: &str = "public.rtf";
|
||||
|
||||
/// Formats offered on macOS pasteboard.
|
||||
pub fn formats() -> Vec<String> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Read RTF data from macOS pasteboard.
|
||||
pub fn rtf() -> Option<Vec<u8>> {
|
||||
let data = NSPasteboard::generalPasteboard().dataForType(&NSString::from_str(RTF))?;
|
||||
(data.len() <= super::MAX_PASTE_BYTES).then(|| data.to_vec())
|
||||
}
|
||||
|
||||
/// Read encoded picture data from macOS pasteboard.
|
||||
pub fn encoded_picture() -> Option<Vec<u8>> {
|
||||
None
|
||||
}
|
||||
|
||||
static LAST_CHANGE_COUNT: std::sync::atomic::AtomicIsize = std::sync::atomic::AtomicIsize::new(-1);
|
||||
|
||||
/// Record pasteboard changeCount on macOS to verify ownership.
|
||||
pub fn record_own() {
|
||||
let count = NSPasteboard::generalPasteboard().changeCount();
|
||||
LAST_CHANGE_COUNT.store(count, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Whether WordCraft still owns the macOS pasteboard.
|
||||
pub fn is_own() -> bool {
|
||||
let count = NSPasteboard::generalPasteboard().changeCount();
|
||||
let recorded = LAST_CHANGE_COUNT.load(std::sync::atomic::Ordering::Relaxed);
|
||||
recorded >= 0 && count == recorded
|
||||
}
|
||||
|
||||
/// Whether macOS supports setting the pasteboard.
|
||||
pub fn can_set() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -488,6 +730,23 @@ mod platform {
|
||||
false
|
||||
}
|
||||
|
||||
/// macOS sets the primary image through arboard and supplements via [`after_set_image`].
|
||||
pub fn set_image(_image: &wordcraft_ui_egui::system_clipboard::CopyImage, _html: &str, _text: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Set supplemental PNG and HTML representations on macOS pasteboard.
|
||||
pub fn after_set_image(image: &wordcraft_ui_egui::system_clipboard::CopyImage, html: &str, _text: &str) {
|
||||
let board = NSPasteboard::generalPasteboard();
|
||||
if let Ok(png) = wordcraft_ui_egui::paste_picture::png_from_rgba(image.width, image.height, image.rgba.clone()) {
|
||||
let _ = board.setData_forType(Some(&NSData::with_bytes(&png)), &NSString::from_str("public.png"));
|
||||
}
|
||||
if !html.is_empty() {
|
||||
let _ = board.setString_forType(&NSString::from_str(html), &NSString::from_str("public.html"));
|
||||
}
|
||||
record_own();
|
||||
}
|
||||
|
||||
/// Add RTF to the pasteboard item arboard just wrote (it cleared the pasteboard first).
|
||||
pub fn add_rtf(rtf: &str) {
|
||||
if !NSPasteboard::generalPasteboard().setData_forType(Some(&NSData::with_bytes(rtf.as_bytes())), &NSString::from_str(RTF)) {
|
||||
@@ -514,27 +773,80 @@ mod platform {
|
||||
all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")))
|
||||
)))]
|
||||
mod platform {
|
||||
/// Formats on the clipboard.
|
||||
pub fn formats() -> Vec<String> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// The RTF bytes on the clipboard.
|
||||
pub fn rtf() -> Option<Vec<u8>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Encoded picture bytes on the clipboard.
|
||||
pub fn encoded_picture() -> Option<Vec<u8>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Whether the fallback platform can set the clipboard.
|
||||
pub fn can_set() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Record clipboard ownership on the fallback platform.
|
||||
pub fn record_own() {}
|
||||
|
||||
/// Whether WordCraft owns the clipboard on the fallback platform.
|
||||
pub fn is_own() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Set rich text on the fallback platform.
|
||||
pub fn set_rich(_text: &str, _html: &str, _rtf: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Set image on the fallback platform.
|
||||
pub fn set_image(_image: &wordcraft_ui_egui::system_clipboard::CopyImage, _html: &str, _text: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Post-image set hook on the fallback platform.
|
||||
pub fn after_set_image(_image: &wordcraft_ui_egui::system_clipboard::CopyImage, _html: &str, _text: &str) {}
|
||||
|
||||
/// Add RTF to the clipboard on the fallback platform.
|
||||
pub fn add_rtf(_rtf: &str) {}
|
||||
|
||||
/// Add MathML to the clipboard on the fallback platform.
|
||||
pub fn add_mathml(_mathml: &str) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod cf_html_tests {
|
||||
use super::wrap_cf_html;
|
||||
|
||||
fn field(data: &[u8], name: &str) -> usize {
|
||||
let at = data.windows(name.len()).position(|w| w == name.as_bytes()).expect("field") + name.len();
|
||||
std::str::from_utf8(&data[at..at + 10]).expect("offset digits").parse().expect("offset")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cf_html_offsets_delimit_the_fragment_bytes() {
|
||||
let fragment = "<p>Olá, 世界 — café</p>";
|
||||
let data = wrap_cf_html(fragment);
|
||||
|
||||
let start_html = field(&data, "StartHTML:");
|
||||
let end_html = field(&data, "EndHTML:");
|
||||
let start_fragment = field(&data, "StartFragment:");
|
||||
let end_fragment = field(&data, "EndFragment:");
|
||||
|
||||
assert!(start_html < start_fragment, "HTML starts before its fragment");
|
||||
assert!(start_fragment < end_fragment, "fragment has a positive length");
|
||||
assert!(end_fragment < end_html, "fragment ends inside the document");
|
||||
assert_eq!(&data[start_html..start_html + "<html>".len()], b"<html>");
|
||||
assert_eq!(&data[start_fragment..end_fragment], fragment.as_bytes());
|
||||
assert_eq!(end_fragment - start_fragment, fragment.len(), "offsets count UTF-8 bytes");
|
||||
assert_eq!(&data[end_html..], b"\0", "EndHTML excludes the trailing NUL");
|
||||
assert_eq!(end_html, data.len() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::mpsc::{Receiver, Sender, channel};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -21,6 +22,38 @@ const MAX_LINE: usize = 1 << 20;
|
||||
/// A connection that sends no authorised request in this time is closed.
|
||||
const FIRST_REQUEST: Duration = Duration::from_secs(30);
|
||||
|
||||
/// How long a caller waits for the UI to answer a request. A request still queued after this
|
||||
/// is cancelled and never runs.
|
||||
const REPLY_WAIT: Duration = Duration::from_secs(60);
|
||||
|
||||
/// Connections that have not yet sent an authorised request are limited to this many; more are
|
||||
/// closed at once. Each one holds a thread until its first request or [`FIRST_REQUEST`].
|
||||
const MAX_UNAUTHENTICATED: usize = 64;
|
||||
|
||||
/// Counts one unauthenticated connection until it authenticates or ends.
|
||||
struct Admission(Option<Arc<AtomicUsize>>);
|
||||
|
||||
impl Admission {
|
||||
fn enter(count: &Arc<AtomicUsize>, max: usize) -> Option<Self> {
|
||||
if count.fetch_add(1, Ordering::SeqCst) >= max {
|
||||
count.fetch_sub(1, Ordering::SeqCst);
|
||||
return None;
|
||||
}
|
||||
Some(Self(Some(count.clone())))
|
||||
}
|
||||
fn release(&mut self) {
|
||||
if let Some(c) = self.0.take() {
|
||||
c.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Admission {
|
||||
fn drop(&mut self) {
|
||||
self.release();
|
||||
}
|
||||
}
|
||||
|
||||
/// The key file of a running control server; [`KeyFile::remove`] it when the window closes.
|
||||
pub struct KeyFile(PathBuf);
|
||||
|
||||
@@ -37,6 +70,15 @@ impl KeyFile {
|
||||
/// cannot be bound or there is no random key (the control channel is off); the key file is
|
||||
/// `None` when it could not be written (requests are then refused).
|
||||
pub fn start(port: u16, ctx: egui::Context, settings: Option<&Path>) -> Option<(Receiver<ControlRequest>, Option<KeyFile>)> {
|
||||
start_with(port, ctx, settings, |listener, ctx, key| serve_on(listener, ctx, key, FIRST_REQUEST))
|
||||
}
|
||||
|
||||
fn start_with(
|
||||
port: u16,
|
||||
ctx: egui::Context,
|
||||
settings: Option<&Path>,
|
||||
serve: impl FnOnce(TcpListener, egui::Context, String) -> std::io::Result<Receiver<ControlRequest>>,
|
||||
) -> Option<(Receiver<ControlRequest>, Option<KeyFile>)> {
|
||||
let listener = match TcpListener::bind(("127.0.0.1", port)) {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
@@ -67,11 +109,21 @@ pub fn start(port: u16, ctx: egui::Context, settings: Option<&Path>) -> Option<(
|
||||
None
|
||||
}
|
||||
};
|
||||
let rx = match serve(listener, ctx, key) {
|
||||
Ok(rx) => rx,
|
||||
Err(e) => {
|
||||
if let Some(f) = &key_file {
|
||||
f.remove();
|
||||
}
|
||||
log::error!("control server off: cannot start the accept thread ({e})");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
match &key_file {
|
||||
Some(f) => log::info!("control server listening on 127.0.0.1:{port}, key in {}", f.path().display()),
|
||||
None => log::info!("control server listening on 127.0.0.1:{port}"),
|
||||
}
|
||||
Some((serve_on(listener, ctx, key, FIRST_REQUEST), key_file))
|
||||
Some((rx, key_file))
|
||||
}
|
||||
|
||||
/// A new key: 32 random bytes from the operating system, as hex.
|
||||
@@ -103,16 +155,47 @@ fn keys_match(given: &str, key: &str) -> bool {
|
||||
!b.is_empty() && a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
|
||||
}
|
||||
|
||||
fn serve_on(listener: TcpListener, ctx: egui::Context, key: String, first: Duration) -> Receiver<ControlRequest> {
|
||||
fn serve_on(listener: TcpListener, ctx: egui::Context, key: String, first: Duration) -> std::io::Result<Receiver<ControlRequest>> {
|
||||
serve_with(listener, ctx, key, Limits { first, wait: REPLY_WAIT, max_unauthenticated: MAX_UNAUTHENTICATED })
|
||||
}
|
||||
|
||||
/// The time and count budgets of a server (small ones in tests).
|
||||
#[derive(Clone, Copy)]
|
||||
struct Limits {
|
||||
first: Duration,
|
||||
wait: Duration,
|
||||
max_unauthenticated: usize,
|
||||
}
|
||||
|
||||
fn serve_with(listener: TcpListener, ctx: egui::Context, key: String, limits: Limits) -> std::io::Result<Receiver<ControlRequest>> {
|
||||
serve_with_spawner(listener, ctx, key, limits, |accept| std::thread::Builder::new().name("control-accept".into()).spawn(accept).map(|_| ()))
|
||||
}
|
||||
|
||||
fn serve_with_spawner(
|
||||
listener: TcpListener,
|
||||
ctx: egui::Context,
|
||||
key: String,
|
||||
limits: Limits,
|
||||
spawn: impl FnOnce(Box<dyn FnOnce() + Send>) -> std::io::Result<()>,
|
||||
) -> std::io::Result<Receiver<ControlRequest>> {
|
||||
let (tx, rx) = channel::<ControlRequest>();
|
||||
let key: Arc<str> = key.into();
|
||||
std::thread::spawn(move || {
|
||||
let unauthenticated = Arc::new(AtomicUsize::new(0));
|
||||
let accept = move || {
|
||||
for stream in listener.incoming().flatten() {
|
||||
let Some(admission) = Admission::enter(&unauthenticated, limits.max_unauthenticated) else {
|
||||
log::warn!("control server: too many unauthenticated connections, one closed");
|
||||
continue;
|
||||
};
|
||||
let (tx, ctx, key) = (tx.clone(), ctx.clone(), key.clone());
|
||||
std::thread::spawn(move || serve(stream, tx, ctx, &key, first));
|
||||
// A thread that cannot start closes this connection only (the guard releases its slot).
|
||||
if let Err(e) = std::thread::Builder::new().name("control-conn".into()).spawn(move || serve(stream, tx, ctx, &key, limits, admission)) {
|
||||
log::warn!("control server: no thread for a connection: {e}");
|
||||
}
|
||||
}
|
||||
});
|
||||
rx
|
||||
};
|
||||
spawn(Box::new(accept))?;
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
/// The read side of a connection with an optional overall deadline: each read waits only for
|
||||
@@ -175,11 +258,11 @@ fn discard_rest_of_line(r: &mut BufReader<Timed>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn serve(stream: TcpStream, tx: Sender<ControlRequest>, ctx: egui::Context, key: &str, first: Duration) {
|
||||
fn serve(stream: TcpStream, tx: Sender<ControlRequest>, ctx: egui::Context, key: &str, limits: Limits, mut admission: Admission) {
|
||||
let Ok(read) = stream.try_clone() else { return };
|
||||
let mut out = stream;
|
||||
// One overall deadline from accept to the first authorised request; after it, none.
|
||||
let mut reader = BufReader::new(Timed { s: read, deadline: Some(Instant::now() + first) });
|
||||
let mut reader = BufReader::new(Timed { s: read, deadline: Some(Instant::now() + limits.first) });
|
||||
let mut authorised = false;
|
||||
// The key an `auth` request gave this connection.
|
||||
let mut conn_key: Option<String> = None;
|
||||
@@ -228,6 +311,7 @@ fn serve(stream: TcpStream, tx: Sender<ControlRequest>, ctx: egui::Context, key:
|
||||
}
|
||||
if !authorised {
|
||||
authorised = true;
|
||||
admission.release();
|
||||
reader.get_mut().set_deadline(None);
|
||||
}
|
||||
if method == "auth" {
|
||||
@@ -237,12 +321,18 @@ fn serve(stream: TcpStream, tx: Sender<ControlRequest>, ctx: egui::Context, key:
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let (req, rrx) = ControlRequest::new(method, params);
|
||||
let (req, rrx, waiter) = ControlRequest::with_waiter(method, params);
|
||||
if tx.send(req).is_err() {
|
||||
break;
|
||||
}
|
||||
ctx.request_repaint();
|
||||
let r = rrx.recv_timeout(Duration::from_secs(60)).unwrap_or_else(|_| json!({"ok": false, "error": "timeout"}));
|
||||
let r = rrx.recv_timeout(limits.wait).unwrap_or_else(|_| {
|
||||
if waiter.cancel() {
|
||||
json!({"ok": false, "error": "timeout before execution: the request was not run and never will be"})
|
||||
} else {
|
||||
json!({"ok": false, "error": "timeout: outcome unknown, the request had started when the wait ended; inspect the document to check"})
|
||||
}
|
||||
});
|
||||
if !reply(r) {
|
||||
break;
|
||||
}
|
||||
@@ -260,7 +350,7 @@ mod tests {
|
||||
fn boot_with(first: Duration) -> (u16, Receiver<ControlRequest>) {
|
||||
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
(port, serve_on(listener, egui::Context::default(), KEY.to_string(), first))
|
||||
(port, serve_on(listener, egui::Context::default(), KEY.to_string(), first).unwrap())
|
||||
}
|
||||
|
||||
fn boot() -> (u16, Receiver<ControlRequest>) {
|
||||
@@ -512,6 +602,40 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_accept_thread_spawn_failure_aborts_start_and_removes_the_key() {
|
||||
let dir = temp_dir("spawn-failure");
|
||||
let path = wordcraft_control_key::key_path(&dir, 0);
|
||||
let mut bound = None;
|
||||
let result = start_with(0, egui::Context::default(), Some(&dir), |listener, ctx, key| {
|
||||
bound = Some(listener.local_addr().unwrap());
|
||||
assert_eq!(std::fs::read_to_string(&path).unwrap(), key, "startup wrote the key before spawning");
|
||||
serve_with_spawner(listener, ctx, key, small(150, 8), |accept| {
|
||||
drop(accept);
|
||||
Err(std::io::Error::other("injected accept thread spawn failure"))
|
||||
})
|
||||
});
|
||||
assert!(result.is_none(), "a server without its accept thread must not report success");
|
||||
assert!(!path.exists(), "failed startup must remove its published key");
|
||||
let _listener = TcpListener::bind(bound.unwrap()).unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_complete_json_request_at_eof_can_reach_the_ui() {
|
||||
// This is why a bridge write failure before the newline is outcome-unknown.
|
||||
let (port, rx) = boot();
|
||||
let mut c = Conn::open(port);
|
||||
let body = json!({"id": 1, "key": KEY, "method": "engine.execute", "params": {"command": "text.insert", "params": {"text": "once"}}});
|
||||
c.1.write_all(body.to_string().as_bytes()).unwrap();
|
||||
c.1.shutdown(std::net::Shutdown::Write).unwrap();
|
||||
let req = answer(&rx);
|
||||
assert_eq!(req.params["params"]["text"], "once");
|
||||
assert_eq!(c.recv()["ok"], true);
|
||||
assert!(c.closed());
|
||||
assert!(rx.try_recv().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stale_key_file_is_replaced() {
|
||||
let dir = temp_dir("stale");
|
||||
@@ -532,4 +656,52 @@ mod tests {
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
fn boot_limits(limits: Limits) -> (u16, Receiver<ControlRequest>) {
|
||||
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
(port, serve_with(listener, egui::Context::default(), KEY.to_string(), limits).unwrap())
|
||||
}
|
||||
|
||||
fn small(wait_ms: u64, max_unauthenticated: usize) -> Limits {
|
||||
Limits { first: FIRST_REQUEST, wait: Duration::from_millis(wait_ms), max_unauthenticated }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_request_that_timed_out_in_the_queue_never_runs() {
|
||||
let (port, rx) = boot_limits(small(150, 8));
|
||||
let mut c = Conn::open(port);
|
||||
let r = c.call(&format!(r#"{{"id":1,"key":"{KEY}","method":"engine.execute","params":{{"command":"text.insert"}}}}"#));
|
||||
assert_eq!(r["ok"], false);
|
||||
assert!(r["error"].as_str().unwrap_or("").starts_with("timeout before execution"), "{r}");
|
||||
// The UI gets to it late: it must not run it.
|
||||
let req = rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
assert!(!req.start(), "a cancelled request was started");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_request_that_started_before_the_timeout_reports_an_unknown_outcome() {
|
||||
let (port, rx) = boot_limits(small(150, 8));
|
||||
let mut c = Conn::open(port);
|
||||
c.send(&format!(r#"{{"id":1,"key":"{KEY}","method":"engine.execute","params":{{"command":"text.insert"}}}}"#));
|
||||
let req = rx.recv_timeout(Duration::from_secs(5)).unwrap();
|
||||
assert!(req.start());
|
||||
let r = c.recv();
|
||||
assert!(r["error"].as_str().unwrap_or("").contains("outcome unknown"), "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unauthenticated_connections_are_limited() {
|
||||
let (port, rx) = boot_limits(small(5_000, 2));
|
||||
let mut idle = [Conn::open(port), Conn::open(port)];
|
||||
// Give the server time to count both.
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
assert!(Conn::open(port).closed(), "the third idle connection was admitted");
|
||||
// One authenticates: its slot is free again.
|
||||
assert_eq!(idle[0].call(&format!(r#"{{"id":1,"method":"auth","key":"{KEY}"}}"#))["ok"], true);
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
let mut again = Conn::open(port);
|
||||
assert_eq!(again.call(&format!(r#"{{"id":1,"method":"auth","key":"{KEY}"}}"#))["ok"], true);
|
||||
assert!(rx.try_recv().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::mpsc::{Receiver, Sender, channel};
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use wordcraft_ui_egui::{FileDialogAnswer, FileDialogRequest};
|
||||
|
||||
@@ -32,6 +34,59 @@ type Request = (FileDialogRequest, Sender<FileDialogAnswer>);
|
||||
/// The `Services::file_dialog` hook.
|
||||
pub type Hook = Box<dyn Fn(FileDialogRequest) -> Receiver<FileDialogAnswer>>;
|
||||
|
||||
/// Cancels the portal Save dialog the launcher is waiting on, if any.
|
||||
///
|
||||
/// The portal's dialog lives outside the app's window, so a portal that stops answering cannot be
|
||||
/// dismissed from WordCraft. Giving the dialog up (File › Open › Browse in WordCraft…) cancels the
|
||||
/// request: the waiting thread closes it and reports a cancellation, and the built-in browser
|
||||
/// opens instead. A no-op where Save As doesn't ask the portal (macOS and Windows).
|
||||
#[derive(Clone, Default)]
|
||||
pub struct CancelHandle {
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
armed: Arc<Mutex<Option<Armed>>>,
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
next_id: Arc<std::sync::atomic::AtomicU64>,
|
||||
}
|
||||
|
||||
/// The token a request installed in [`CancelHandle`], with the identity that owns it.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
struct Armed {
|
||||
id: u64,
|
||||
token: crate::portal_save::CancelToken,
|
||||
}
|
||||
|
||||
impl CancelHandle {
|
||||
/// Cancels the pending portal request, if there is one.
|
||||
pub fn cancel(&self) {
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
{
|
||||
let mut slot = self.armed.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(armed) = slot.take() {
|
||||
armed.token.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Arms the handle for the portal request about to wait, returning its token and identity.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
fn arm(&self) -> Armed {
|
||||
let id = self.next_id.fetch_add(1, std::sync::atomic::Ordering::Relaxed).wrapping_add(1);
|
||||
let token = crate::portal_save::CancelToken::new();
|
||||
*self.armed.lock().unwrap_or_else(|e| e.into_inner()) = Some(Armed { id, token: token.clone() });
|
||||
Armed { id, token }
|
||||
}
|
||||
|
||||
/// Disarms the handle once the request with `id` has answered, leaving a newer request's
|
||||
/// token alone: an abandoned request finishing late must not make the next one uncancellable.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
fn disarm(&self, id: u64) {
|
||||
let mut slot = self.armed.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if slot.as_ref().is_some_and(|armed| armed.id == id) {
|
||||
*slot = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A picker's answer, waited for off the UI thread.
|
||||
type Picking = Pin<Box<dyn Future<Output = Vec<rfd::FileHandle>> + Send>>;
|
||||
|
||||
@@ -39,6 +94,7 @@ type Picking = Pin<Box<dyn Future<Output = Vec<rfd::FileHandle>> + Send>>;
|
||||
pub struct Launcher {
|
||||
queue: Receiver<Request>,
|
||||
ctx: egui::Context,
|
||||
cancel: CancelHandle,
|
||||
}
|
||||
|
||||
/// The hook for [`Services::file_dialog`](wordcraft_ui_egui::Services::file_dialog) and the
|
||||
@@ -51,10 +107,15 @@ pub fn hook(ctx: &egui::Context) -> (Hook, Launcher) {
|
||||
let _ = queue_tx.send((req, answer));
|
||||
rx
|
||||
});
|
||||
(hook, Launcher { queue, ctx: ctx.clone() })
|
||||
(hook, Launcher { queue, ctx: ctx.clone(), cancel: CancelHandle::default() })
|
||||
}
|
||||
|
||||
impl Launcher {
|
||||
/// A handle that cancels the portal Save dialog the launcher is waiting on, if any.
|
||||
pub fn cancel_handle(&self) -> CancelHandle {
|
||||
self.cancel.clone()
|
||||
}
|
||||
|
||||
/// Show the queued dialogs with the window as their parent, so the portal (or the system)
|
||||
/// keeps them on top of it. Call after every pass that may ask for one.
|
||||
pub fn show(&self, parent: &eframe::Frame) {
|
||||
@@ -63,26 +124,90 @@ impl Launcher {
|
||||
let picking = picker(rfd::AsyncFileDialog::new().set_parent(parent), &req);
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let unparented = picker(rfd::AsyncFileDialog::new(), &req);
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let parent_id = {
|
||||
use eframe::wgpu::rwh::HasWindowHandle;
|
||||
crate::portal_save::parent_id(parent.window_handle().ok().map(|h| h.as_raw()))
|
||||
};
|
||||
// Armed before the thread starts, so giving the dialog up cannot race its start.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let portal_arm = matches!(&req, FileDialogRequest::Save { .. }).then(|| self.cancel.arm());
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let portal_id = portal_arm.as_ref().map(|armed| armed.id);
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let cancel_handle = self.cancel.clone();
|
||||
let ctx = self.ctx.clone();
|
||||
let waiting = std::thread::Builder::new().name("file dialog".into()).spawn(move || {
|
||||
// The pickers only start when first polled: without a system dialog they never do.
|
||||
let picked = match no_system_dialog() {
|
||||
Some(reason) => Err(reason),
|
||||
None => {
|
||||
// Only the portal folds a failure into "cancelled" (#2999).
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
let picked = pick(picking, unparented, TOO_QUICK);
|
||||
{
|
||||
if let FileDialogRequest::Save { name, dir } = &req {
|
||||
let cancel = portal_arm.as_ref().map(|armed| armed.token.clone());
|
||||
let portal_req = crate::portal_save::SaveRequest {
|
||||
parent: parent_id,
|
||||
name: Some(name.clone()),
|
||||
suggested: Some(name.clone()),
|
||||
folder: dir.clone().map(std::path::PathBuf::from),
|
||||
filters: save_filters(name),
|
||||
current: Some(0),
|
||||
cancel,
|
||||
timeout: None,
|
||||
};
|
||||
let portal_res = zbus::blocking::Connection::session()
|
||||
.map_err(|e| e.to_string())
|
||||
.and_then(|conn| crate::portal_save::save_as(&conn, portal_req));
|
||||
match portal_res {
|
||||
Ok(Some(path)) => Ok(vec![path.to_string_lossy().into_owned()]),
|
||||
Ok(None) => Ok(Vec::new()),
|
||||
Err(e) if crate::portal_save::is_cancelled_error(&e) => Ok(Vec::new()),
|
||||
Err(e) if crate::portal_save::is_ambiguous_filter_error(&e) => {
|
||||
log::warn!("portal save format ambiguous ({e}); cancelling save without explicit format choice");
|
||||
Ok(Vec::new())
|
||||
}
|
||||
Err(e) => {
|
||||
// Giving the dialog up cancels the request. A portal that then
|
||||
// fails with another error must not open the rfd dialog, which
|
||||
// nothing would be waiting for.
|
||||
if portal_arm.as_ref().is_some_and(|armed| armed.token.is_cancelled()) {
|
||||
log::info!("portal save dialog given up ({e})");
|
||||
Ok(Vec::new())
|
||||
} else {
|
||||
log::info!("portal save dialog unavailable ({e}), using rfd");
|
||||
let picked = pick(picking, unparented, TOO_QUICK);
|
||||
picked.map(|f| f.iter().map(|f| f.path().to_string_lossy().to_string()).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Only the portal folds a failure into "cancelled" (#2999).
|
||||
let picked = pick(picking, unparented, TOO_QUICK);
|
||||
picked.map(|f| f.iter().map(|f| f.path().to_string_lossy().to_string()).collect())
|
||||
}
|
||||
}
|
||||
#[cfg(not(all(unix, not(target_os = "macos"))))]
|
||||
let picked: Result<_, String> = Ok(pollster::block_on(picking));
|
||||
picked.map(|f| f.iter().map(|f| f.path().to_string_lossy().to_string()).collect())
|
||||
{
|
||||
let picked: Result<_, String> = Ok(pollster::block_on(picking));
|
||||
picked.map(|f| f.iter().map(|f| f.path().to_string_lossy().to_string()).collect())
|
||||
}
|
||||
}
|
||||
};
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
if let Some(armed) = &portal_arm {
|
||||
cancel_handle.disarm(armed.id);
|
||||
}
|
||||
let _ = answer.send(picked);
|
||||
ctx.request_repaint();
|
||||
});
|
||||
// The answer's sender went with the closure, so the app sees the dialog as cancelled.
|
||||
if let Err(e) = waiting {
|
||||
log::error!("couldn't wait for the file dialog: {e}");
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
if let Some(id) = portal_id {
|
||||
self.cancel.disarm(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,6 +334,8 @@ fn save_exts(name: &str) -> Vec<String> {
|
||||
.extension()
|
||||
.and_then(std::ffi::OsStr::to_str)
|
||||
.map(str::to_ascii_lowercase)
|
||||
// A .markdown file is under Markdown's one entry in the list.
|
||||
.map(|e| if e == "markdown" { "md".to_string() } else { e })
|
||||
.filter(|e| wordcraft_engine::io::SAVE_EXTS.contains(&e.as_str()))
|
||||
.unwrap_or_else(|| "docx".to_string());
|
||||
let mut exts: Vec<String> = Vec::with_capacity(wordcraft_engine::io::SAVE_EXTS.len());
|
||||
@@ -220,8 +347,9 @@ fn save_exts(name: &str) -> Vec<String> {
|
||||
/// The "Save as type" list (#1399): one named entry per format, the suggested one first (see
|
||||
/// [`save_exts`] for why the order matters).
|
||||
///
|
||||
/// rfd doesn't report which entry the user picked, so if they choose "PDF" but leave "name.docx"
|
||||
/// in the name box, the extension in the name wins, as the format is chosen by extension.
|
||||
/// On Linux and the BSDs, [`crate::portal_save`] asks the portal directly over D-Bus and gives a
|
||||
/// name the dialog left alone the chosen type's extension (#6180). When falling back to `rfd`, the
|
||||
/// extension in the name wins, as `rfd` doesn't report which entry the user picked.
|
||||
fn save_filters(name: &str) -> Vec<(String, Vec<String>)> {
|
||||
save_exts(name).into_iter().map(|e| (format!("{} (*.{e})", format_name(&e)), vec![e])).collect()
|
||||
}
|
||||
@@ -272,6 +400,37 @@ mod tests {
|
||||
assert_eq!(pick(async { vec![] }, slow_cancel(), quick), Ok(vec![]), "the second dialog cancelled for real");
|
||||
}
|
||||
|
||||
/// Giving the portal Save dialog up cancels the request the launcher armed, and a request
|
||||
/// that has already answered is not affected by a later cancel.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
#[test]
|
||||
fn cancelling_a_pending_portal_request_marks_its_token() {
|
||||
let handle = super::CancelHandle::default();
|
||||
let armed = handle.arm();
|
||||
assert!(!armed.token.is_cancelled());
|
||||
handle.cancel();
|
||||
assert!(armed.token.is_cancelled());
|
||||
|
||||
let answered = handle.arm();
|
||||
handle.disarm(answered.id);
|
||||
handle.cancel();
|
||||
assert!(!answered.token.is_cancelled(), "an answered request is no longer cancelled");
|
||||
}
|
||||
|
||||
/// A request that finishes late must not disarm the token of a request started after it:
|
||||
/// otherwise the newer Save dialog would become uncancellable.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
#[test]
|
||||
fn a_late_answer_keeps_the_newer_requests_token() {
|
||||
let handle = super::CancelHandle::default();
|
||||
let abandoned = handle.arm();
|
||||
let newer = handle.arm();
|
||||
handle.disarm(abandoned.id);
|
||||
handle.cancel();
|
||||
assert!(newer.token.is_cancelled(), "the newer request is still armed");
|
||||
assert!(!abandoned.token.is_cancelled(), "only the armed request is cancelled");
|
||||
}
|
||||
|
||||
/// #1399: the Save dialog's type list has one named entry per format, the suggested one first,
|
||||
/// every savable extension exactly once.
|
||||
#[test]
|
||||
@@ -301,6 +460,8 @@ mod tests {
|
||||
assert_eq!(save_exts("novel.docx")[0], "docx");
|
||||
assert_eq!(save_exts("report.odt")[0], "odt");
|
||||
assert_eq!(save_exts("annual report.pdf")[0], "pdf");
|
||||
assert_eq!(save_exts("notes.md")[0], "md");
|
||||
assert_eq!(save_exts("notes.markdown")[0], "md");
|
||||
// A suggested name always has an extension; anything unreadable falls back to Word's.
|
||||
assert_eq!(save_exts("no extension")[0], "docx");
|
||||
assert_eq!(save_exts("mystery.rtf2")[0], "docx");
|
||||
|
||||
+121
-30
@@ -10,6 +10,8 @@
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
mod animations;
|
||||
#[cfg(target_os = "linux")]
|
||||
mod appimage_env;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod apple_events;
|
||||
mod clipboard;
|
||||
@@ -19,6 +21,8 @@ mod file_dialogs;
|
||||
#[cfg(any(target_os = "windows", target_os = "macos", test))]
|
||||
mod graphics;
|
||||
mod logging;
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
mod portal_save;
|
||||
mod print;
|
||||
mod region;
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -67,6 +71,8 @@ impl eframe::App for App {
|
||||
if let Some((pos, size)) = self.1.and_then(|g| g.fit_on_monitor(&window)) {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(size));
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::OuterPosition(pos));
|
||||
let b = self.0.window_builder().clone().with_inner_size(size);
|
||||
self.0.set_window_builder(b);
|
||||
}
|
||||
// The geometry saved for the next start is the active window's.
|
||||
let prev = self.0.ui.window;
|
||||
@@ -129,6 +135,7 @@ impl eframe::App for App {
|
||||
if let Some(key_file) = &self.2 {
|
||||
key_file.remove();
|
||||
}
|
||||
self.0.start_pending_update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,6 +195,8 @@ fn schedule_macos_terminate(app: &mut WordApp, key_file: Option<&control_server:
|
||||
if let Some(key_file) = key_file {
|
||||
key_file.remove();
|
||||
}
|
||||
// `terminate:` ends the process without dropping the app, so a ready update's helper starts here.
|
||||
app.start_pending_update();
|
||||
dispatch2::DispatchQueue::main().exec_async(|| {
|
||||
if let Some(mtm) = objc2::MainThreadMarker::new() {
|
||||
objc2_app_kit::NSApplication::sharedApplication(mtm).terminate(None);
|
||||
@@ -269,9 +278,16 @@ fn saved_ui() -> Option<UiState> {
|
||||
/// whenever there's an X server: winit has no file drag-and-drop on Wayland, so dropping files from
|
||||
/// the file manager showed a "no" cursor. X11 is only forced with an X server (`DISPLAY`), and
|
||||
/// unforced winit picks Wayland when it's there and X11 otherwise, so a choice that isn't
|
||||
/// available falls back to the other instead of failing to start.
|
||||
/// available falls back to the other instead of failing to start. The X11 libraries are only
|
||||
/// recommended by the packages, so when `x11_libraries` reports some missing (it's asked only when
|
||||
/// X11 would be forced) the app stays on Wayland: forced X11 without them failed to start, and
|
||||
/// winit panicked without libxkbcommon-x11.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
fn uses_x11(setting: wordcraft_ui_egui::DisplayServer, env: impl Fn(&str) -> Option<std::ffi::OsString>) -> (bool, String) {
|
||||
fn uses_x11(
|
||||
setting: wordcraft_ui_egui::DisplayServer,
|
||||
env: impl Fn(&str) -> Option<std::ffi::OsString>,
|
||||
x11_libraries: impl FnOnce() -> Result<(), String>,
|
||||
) -> (bool, String) {
|
||||
use wordcraft_ui_egui::DisplayServer;
|
||||
let set = |name: &str| env(name).is_some_and(|v| !v.is_empty());
|
||||
let (wanted, by) = match env("WORDCRAFT_WAYLAND") {
|
||||
@@ -281,39 +297,60 @@ fn uses_x11(setting: wordcraft_ui_egui::DisplayServer, env: impl Fn(&str) -> Opt
|
||||
Some(_) => (DisplayServer::Wayland, "WORDCRAFT_WAYLAND"),
|
||||
None => (setting, "the setting"),
|
||||
};
|
||||
let x11 = wanted != DisplayServer::Wayland && set("DISPLAY");
|
||||
let missing = if wanted != DisplayServer::Wayland && set("DISPLAY") { x11_libraries().err() } else { None };
|
||||
let x11 = wanted != DisplayServer::Wayland && set("DISPLAY") && missing.is_none();
|
||||
let wayland = set("WAYLAND_DISPLAY") || set("WAYLAND_SOCKET");
|
||||
let runs = if x11 {
|
||||
"X11"
|
||||
} else if set("WAYLAND_DISPLAY") || set("WAYLAND_SOCKET") {
|
||||
} else if wayland {
|
||||
"native Wayland"
|
||||
} else if set("DISPLAY") {
|
||||
"X11 (no Wayland)"
|
||||
} else {
|
||||
"neither (no Wayland or X server found)"
|
||||
};
|
||||
(x11, format!("display: {runs}; {} asks for {}", by, wanted.code()))
|
||||
let mut why = format!("display: {runs}; {} asks for {}", by, wanted.code());
|
||||
if let Some(missing) = missing {
|
||||
why += &format!("; not X11 because its libraries are missing: {missing}");
|
||||
}
|
||||
(x11, why)
|
||||
}
|
||||
|
||||
/// `Err` naming the libraries winit's X11 backend opens at startup that can't be loaded: libX11,
|
||||
/// libXcursor, libX11-xcb (which brings in libxcb), libXi, and libxkbcommon-x11 for the keyboard.
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
fn x11_libraries() -> Result<(), String> {
|
||||
use x11_dl::{xcursor::Xcursor, xinput2::XInput2, xlib::Xlib, xlib_xcb::Xlib_xcb};
|
||||
let mut missing: Vec<String> = [Xlib::open().err(), Xcursor::open().err(), Xlib_xcb::open().err(), XInput2::open().err()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|e| e.to_string())
|
||||
.collect();
|
||||
if xkbcommon_dl::x11::xkbcommon_x11_option().is_none() {
|
||||
missing.push("libxkbcommon-x11.so.0 could not be loaded".into());
|
||||
}
|
||||
if missing.is_empty() { Ok(()) } else { Err(missing.join("; ")) }
|
||||
}
|
||||
|
||||
fn save_prefs(app: &WordApp) {
|
||||
if let Err(e) = try_save_prefs(app) {
|
||||
log::warn!("preferences: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Saves the preferences and says why when it could not. `Ok(false)`: nothing was written
|
||||
/// because preferences are switched off or there is no settings folder.
|
||||
fn try_save_prefs(app: &WordApp) -> Result<bool, String> {
|
||||
if !prefs_enabled() {
|
||||
return;
|
||||
return Ok(false);
|
||||
}
|
||||
if let Some(p) = prefs_path() {
|
||||
if let Some(dir) = p.parent()
|
||||
&& let Err(e) = std::fs::create_dir_all(dir)
|
||||
{
|
||||
log::warn!("preferences: cannot create {}: {e}", dir.display());
|
||||
return;
|
||||
}
|
||||
match serde_json::to_vec_pretty(&app.prefs()) {
|
||||
Ok(bytes) => {
|
||||
if let Err(e) = write_prefs(&p, &bytes) {
|
||||
log::warn!("preferences: cannot save {}: {e}", p.display());
|
||||
}
|
||||
}
|
||||
Err(e) => log::warn!("preferences: cannot serialize: {e}"),
|
||||
}
|
||||
let Some(p) = prefs_path() else { return Ok(false) };
|
||||
if let Some(dir) = p.parent() {
|
||||
std::fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
|
||||
}
|
||||
let bytes = serde_json::to_vec_pretty(&app.prefs()).map_err(|e| format!("cannot serialize: {e}"))?;
|
||||
write_prefs(&p, &bytes).map_err(|e| format!("cannot save {}: {e}", p.display()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Writes `bytes` to a sibling temporary file and renames it over `path`, so a failed or
|
||||
@@ -332,9 +369,10 @@ fn write_prefs(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> {
|
||||
/// through the system spooler (`print`), the system clipboard's text, formatted copies,
|
||||
/// pictures and format names (`clipboard`, #45, #584, #687, #833), the system's animation setting
|
||||
/// (`animations`), and making WordCraft the default app for Word documents (`default_apps`, #295).
|
||||
fn services(file_dialog: file_dialogs::Hook, ctx: &egui::Context) -> Services {
|
||||
fn services(file_dialog: file_dialogs::Hook, cancel_file_dialog: file_dialogs::CancelHandle, ctx: &egui::Context) -> Services {
|
||||
Services {
|
||||
file_dialog: Some(file_dialog),
|
||||
cancel_file_dialog: Some(Box::new(move || cancel_file_dialog.cancel())),
|
||||
printers: Some(print::discover()),
|
||||
print_native: Some(print::submit()),
|
||||
cancel_print: Some(print::cancel()),
|
||||
@@ -346,8 +384,11 @@ fn services(file_dialog: file_dialogs::Hook, ctx: &egui::Context) -> Services {
|
||||
clipboard_text: Some(Box::new(clipboard::text)),
|
||||
clipboard_html: Some(Box::new(clipboard::html)),
|
||||
set_clipboard_rich: Some(Box::new(clipboard::set_rich)),
|
||||
set_clipboard_image: Some(Box::new(clipboard::set_image)),
|
||||
clipboard_is_own: Some(Box::new(clipboard::is_own)),
|
||||
clipboard_rtf: Some(Box::new(clipboard::rtf)),
|
||||
clipboard_formats: Some(Box::new(clipboard::formats)),
|
||||
save_prefs: Some(Box::new(try_save_prefs)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@@ -421,14 +462,15 @@ fn without_launch_args(args: Vec<String>) -> Vec<String> {
|
||||
|
||||
/// Open a file whose name isn't valid Unicode (#1440). `file.open` takes its path as text, so it
|
||||
/// gets the file's bytes under the lossy name, and the document then keeps the real path to save to.
|
||||
/// The bytes are read as `file.open` reads a path: only a regular file, and at most 2 GB.
|
||||
/// The bytes are read as `file.open` reads a path: only a regular file, and at most 2 GB. A Word
|
||||
/// template starts a new document instead ([`WordApp::open_from_platform`]), which has no path.
|
||||
fn open_non_unicode(app: &mut WordApp, path: &std::path::Path) {
|
||||
let name = path.to_string_lossy();
|
||||
let opened = wordcraft_engine::io::read_document(path)
|
||||
.and_then(|bytes| app.run("file.open", serde_json::json!({"path": name, "data": wordcraft_engine::cmd::insert::base64_encode(&bytes)})));
|
||||
.and_then(|bytes| app.open_from_platform(&name, Some(&wordcraft_engine::cmd::insert::base64_encode(&bytes))));
|
||||
match opened {
|
||||
// Not when it waits for a password: that open names the document itself.
|
||||
Ok(r) if r.get("pending").is_none() => app.session.path = Some(path.into()),
|
||||
Ok(r) if r.get("pending").is_none() && !wordcraft_ui_egui::documents::is_word_template(&name) => app.session.path = Some(path.into()),
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
app.report_open_failure(&name, &e);
|
||||
@@ -449,9 +491,22 @@ fn main() -> eframe::Result {
|
||||
if let Some(result) = craft_update::self_update::finish(&wordcraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
|
||||
std::process::exit(i32::from(result.is_err()));
|
||||
}
|
||||
// Linux: before anything reads another program's AppImage variables, or a thread starts (#5653).
|
||||
#[cfg(target_os = "linux")]
|
||||
let start_over_failed = appimage_env::drop_inherited();
|
||||
// Still carrying them, this process must never update itself: the update would replace the
|
||||
// other program's file.
|
||||
#[cfg(target_os = "linux")]
|
||||
let updates_unsafe = start_over_failed.is_some();
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let updates_unsafe = false;
|
||||
// First, so the panic hook and every start-up warning are recorded (`logging`).
|
||||
let logger = logging::install();
|
||||
logging::install_panic_hook();
|
||||
#[cfg(target_os = "linux")]
|
||||
if let Some(e) = start_over_failed {
|
||||
log::error!("couldn't start again without another program's AppImage variables, so the self-update is off in this window: {e}");
|
||||
}
|
||||
let mut control_port: Option<u16> = std::env::var("WORDCRAFT_CONTROL_PORT").ok().and_then(|p| p.parse().ok());
|
||||
let mut files = Vec::new();
|
||||
let mut sample = false;
|
||||
@@ -537,7 +592,7 @@ fn main() -> eframe::Result {
|
||||
}
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
{
|
||||
let (x11, why) = uses_x11(saved.as_ref().map(|ui| ui.display_server).unwrap_or_default(), |name| std::env::var_os(name));
|
||||
let (x11, why) = uses_x11(saved.as_ref().map(|ui| ui.display_server).unwrap_or_default(), |name| std::env::var_os(name), x11_libraries);
|
||||
log::info!("{why}");
|
||||
if x11 {
|
||||
options.event_loop_builder = Some(Box::new(|b| {
|
||||
@@ -561,11 +616,16 @@ fn main() -> eframe::Result {
|
||||
let region = region::system();
|
||||
let doc = if sample { wordcraft_engine::sample::sample_document() } else { wordcraft_engine::blank_document(®ion) };
|
||||
let (file_dialog, dialogs) = file_dialogs::hook(&cc.egui_ctx);
|
||||
let cancel_file_dialog = dialogs.cancel_handle();
|
||||
let mut session = Session::new(doc);
|
||||
session.system_region = region;
|
||||
let mut app = WordApp::new(session, services(file_dialog, &cc.egui_ctx));
|
||||
let mut app = WordApp::new(session, services(file_dialog, cancel_file_dialog, &cc.egui_ctx));
|
||||
app.set_startup_update_check(true);
|
||||
load_prefs(&mut app);
|
||||
// After the preferences, so the reason it gives is in the interface language.
|
||||
if updates_unsafe {
|
||||
app.disable_self_update();
|
||||
}
|
||||
load_user_dictionary(&mut app);
|
||||
// Language packs from the suite's font store; an Install click is the person's own.
|
||||
wordcraft_engine::packs::use_font_store(true);
|
||||
@@ -605,8 +665,9 @@ fn main() -> eframe::Result {
|
||||
if files.is_empty() && other_files.is_empty() && !sample && control_port.is_none() {
|
||||
app.start_screen();
|
||||
}
|
||||
// A Word template named here starts a new document based on it (#3433).
|
||||
for f in files {
|
||||
if let Err(e) = app.run("file.open", serde_json::json!({"path": f})) {
|
||||
if let Err(e) = app.open_from_platform(&f, None) {
|
||||
app.report_open_failure(&f, &e);
|
||||
}
|
||||
}
|
||||
@@ -664,7 +725,7 @@ mod tests {
|
||||
fn the_display_server_follows_the_variable_then_the_setting() {
|
||||
use wordcraft_ui_egui::DisplayServer::{Auto, Wayland, X11};
|
||||
let x11 = |setting, vars: &[(&str, &str)]| {
|
||||
uses_x11(setting, |name| vars.iter().find(|(k, _)| *k == name).map(|(_, v)| std::ffi::OsString::from(v))).0
|
||||
uses_x11(setting, |name| vars.iter().find(|(k, _)| *k == name).map(|(_, v)| std::ffi::OsString::from(v)), || Ok(())).0
|
||||
};
|
||||
let both = [("DISPLAY", ":0"), ("WAYLAND_DISPLAY", "wayland-0")];
|
||||
assert!(x11(Auto, &both), "Automatic keeps XWayland, for file drag-and-drop");
|
||||
@@ -680,10 +741,40 @@ mod tests {
|
||||
let wayland_only = [("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", "")];
|
||||
assert!(!x11(X11, &wayland_only) && !x11(Auto, &wayland_only));
|
||||
// Without Wayland, Wayland isn't forced either: unforced winit picks X11.
|
||||
let (forced, why) = uses_x11(Wayland, |name| (name == "DISPLAY").then(|| ":0".into()));
|
||||
let (forced, why) = uses_x11(Wayland, |name| (name == "DISPLAY").then(|| ":0".into()), || Ok(()));
|
||||
assert!(!forced && why.contains("X11 (no Wayland)"), "{why}");
|
||||
}
|
||||
|
||||
/// Without the X11 libraries (the packages only recommend them) X11 isn't forced, whatever asks
|
||||
/// for it: forced X11 failed to start, and winit panicked without libxkbcommon-x11. The check
|
||||
/// runs only when X11 would be forced.
|
||||
#[test]
|
||||
fn missing_x11_libraries_leave_the_app_on_wayland() {
|
||||
use wordcraft_ui_egui::DisplayServer::{Auto, Wayland, X11};
|
||||
let both = [("DISPLAY", ":0"), ("WAYLAND_DISPLAY", "wayland-0")];
|
||||
for (setting, variable) in [(Auto, None), (X11, None), (Wayland, Some("0"))] {
|
||||
let env = |name: &str| {
|
||||
both.iter()
|
||||
.find(|(k, _)| *k == name)
|
||||
.map(|(_, v)| (*v).into())
|
||||
.or_else(|| (name == "WORDCRAFT_WAYLAND").then_some(variable).flatten().map(Into::into))
|
||||
};
|
||||
let (x11, why) = uses_x11(setting, env, || Err("libxkbcommon-x11.so.0 could not be loaded".into()));
|
||||
assert!(!x11, "{setting:?} with WORDCRAFT_WAYLAND={variable:?}: {why}");
|
||||
assert!(why.contains("native Wayland") && why.contains("libxkbcommon-x11"), "{why}");
|
||||
}
|
||||
let checked = std::cell::Cell::new(false);
|
||||
let (x11, _) = uses_x11(
|
||||
Wayland,
|
||||
|name| both.iter().find(|(k, _)| *k == name).map(|(_, v)| (*v).into()),
|
||||
|| {
|
||||
checked.set(true);
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
assert!(!x11 && !checked.get(), "asked for Wayland, the libraries aren't checked");
|
||||
}
|
||||
|
||||
/// A named pipe or a device under such a name is a warning, not a read: the pipe blocked
|
||||
/// start-up until something wrote to it, and `/dev/zero` read until memory ran out.
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,906 @@
|
||||
//! File › Save As through the XDG desktop portal on Linux and BSDs (#6180).
|
||||
//!
|
||||
//! The GTK file chooser behind the portal keeps the typed name when the user picks another file
|
||||
//! type, and `rfd` 0.17 reads only the file name from the portal's answer, so `Document1.docx`
|
||||
//! with PDF picked in the dropdown came back as `Document1.docx` and was saved as a Word document.
|
||||
//! The portal's answer does say which type was picked (`current_filter`), so Save As asks the portal
|
||||
//! itself over D-Bus via `zbus` and gives a name the dialog left alone that type's extension
|
||||
//! ([`with_chosen_type`]). `rfd` stays the fallback (zenity, or no portal), and this can go once an
|
||||
//! `rfd` release reads the picked type (upstream PolyMeilex/rfd#341).
|
||||
//!
|
||||
//! The dialog is attached to the window on X11 (`x11:<xid>`). On Wayland it has no parent: naming the
|
||||
//! window to the portal needs an xdg-foreign handle, which takes `unsafe` code the workspace forbids.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use zbus::blocking::{MessageIterator, Proxy};
|
||||
use zbus::zvariant::{OwnedObjectPath, OwnedValue, Value};
|
||||
|
||||
/// One file type in the dialog: its label and extensions (`("PDF (*.pdf)", ["pdf"])`).
|
||||
pub type Filter = (String, Vec<String>);
|
||||
|
||||
/// Quicker than anyone can see a dialog and dismiss it (#2999).
|
||||
const TOO_QUICK: Duration = Duration::from_millis(300);
|
||||
|
||||
/// A thread-safe cancellation token to signal cancellation of an ongoing save request.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct CancelToken(std::sync::Arc<std::sync::atomic::AtomicBool>);
|
||||
|
||||
impl CancelToken {
|
||||
/// Creates a new cancellation token in the active (not cancelled) state.
|
||||
pub fn new() -> Self {
|
||||
Self(std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)))
|
||||
}
|
||||
|
||||
/// Signals that the active save request should be cancelled.
|
||||
pub fn cancel(&self) {
|
||||
self.0.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Returns `true` if cancellation has been requested.
|
||||
pub fn is_cancelled(&self) -> bool {
|
||||
self.0.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for CancelToken {
|
||||
/// Compares two cancellation tokens by their current cancellation status.
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.is_cancelled() == other.is_cancelled()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for CancelToken {}
|
||||
|
||||
/// A Save dialog request to pass to the desktop portal.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct SaveRequest {
|
||||
/// The portal's parent window identifier (`x11:<xid>`), or empty.
|
||||
pub parent: String,
|
||||
/// The name the dialog shows: the suggested file name (`Document1.docx`).
|
||||
pub name: Option<String>,
|
||||
/// The suggested file's name (`Document1.docx`): a name that comes back with its extension still
|
||||
/// takes the extension of the type picked ([`with_chosen_type`]).
|
||||
pub suggested: Option<String>,
|
||||
/// The initial folder to open in, if any.
|
||||
pub folder: Option<PathBuf>,
|
||||
/// The list of filters offered in the dropdown.
|
||||
pub filters: Vec<Filter>,
|
||||
/// The index of the filter the dialog starts on.
|
||||
pub current: Option<usize>,
|
||||
/// Optional cancellation token allowing the caller to cancel a pending save request.
|
||||
pub cancel: Option<CancelToken>,
|
||||
/// Optional timeout after which a request waiting for a response is considered stalled.
|
||||
pub timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
/// What the portal dialog returned: the path, as typed, and the filter chosen in the dropdown.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Saved {
|
||||
/// The filesystem path chosen in the dialog.
|
||||
pub path: PathBuf,
|
||||
/// The filter chosen in the dropdown, if reported by the portal: `(label, extensions)`.
|
||||
pub filter: Option<Filter>,
|
||||
}
|
||||
|
||||
/// The portal's parent window identifier for `window`: `x11:<xid>` on X11, empty elsewhere.
|
||||
///
|
||||
/// On Wayland, exporting an xdg-foreign handle requires `unsafe` code forbidden by workspace rules
|
||||
/// (`#![forbid(unsafe_code)]`, Rust Rule 1), so Wayland dialogs remain unparented, matching
|
||||
/// PhotoCraft and VectorCraft.
|
||||
pub fn parent_id(window: Option<eframe::wgpu::rwh::RawWindowHandle>) -> String {
|
||||
use eframe::wgpu::rwh::RawWindowHandle;
|
||||
match window {
|
||||
Some(RawWindowHandle::Xlib(h)) => format!("x11:{:x}", h.window),
|
||||
Some(RawWindowHandle::Xcb(h)) => format!("x11:{:x}", h.window.get()),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a filter tuple into the portal's wire format: label and a list of `(0, "*.ext")` glob pairs.
|
||||
fn wire(filter: &Filter) -> (String, Vec<(u32, String)>) {
|
||||
let globs = filter.1.iter().map(|e| (0u32, if e.is_empty() || e == "*" { "*".to_string() } else { format!("*.{e}") })).collect();
|
||||
(filter.0.clone(), globs)
|
||||
}
|
||||
|
||||
/// The `SaveFile` options dictionary for `req`, answering on `token`.
|
||||
fn options<'a>(req: &'a SaveRequest, token: &'a str) -> HashMap<&'static str, Value<'a>> {
|
||||
let mut o: HashMap<&'static str, Value<'a>> = HashMap::from([("handle_token", Value::from(token)), ("modal", Value::from(true))]);
|
||||
if let Some(name) = &req.name {
|
||||
o.insert("current_name", Value::from(name.as_str()));
|
||||
}
|
||||
if let Some(folder) = &req.folder {
|
||||
// A byte string with its terminating NUL, as the portal specifies.
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
let mut bytes = folder.as_os_str().as_bytes().to_vec();
|
||||
bytes.push(0);
|
||||
o.insert("current_folder", Value::from(bytes));
|
||||
}
|
||||
if !req.filters.is_empty() {
|
||||
let wired = req.filters.iter().map(wire).collect::<Vec<_>>();
|
||||
o.insert("filters", Value::from(wired));
|
||||
}
|
||||
if let Some(f) = req.current.and_then(|i| req.filters.get(i)).or_else(|| req.filters.first()) {
|
||||
o.insert("current_filter", Value::from(wire(f)));
|
||||
}
|
||||
o
|
||||
}
|
||||
|
||||
/// Reads the portal's `Response`: code 0 with the first `uris` entry. 1 is Cancel, and 2 a dialog
|
||||
/// closed some other way (e.g. Esc in GNOME): no file either. When code 2 arrives quicker than
|
||||
/// [`TOO_QUICK`], the portal failed to show its dialog (#2999) and an error is returned so the
|
||||
/// caller can fall back.
|
||||
fn answer(code: u32, results: &HashMap<String, OwnedValue>, elapsed: Duration) -> Result<Option<Saved>, String> {
|
||||
match code {
|
||||
0 => {}
|
||||
1 => return Ok(None),
|
||||
2 if elapsed < TOO_QUICK => return Err("the save dialog closed at once".to_string()),
|
||||
2 => return Ok(None),
|
||||
_ => return Err(format!("the save dialog failed (portal response {code})")),
|
||||
}
|
||||
let uris: Vec<String> = results
|
||||
.get("uris")
|
||||
.ok_or_else(|| "no file in the portal's answer".to_string())?
|
||||
.try_clone()
|
||||
.map_err(|e| e.to_string())?
|
||||
.try_into()
|
||||
.map_err(|e: zbus::zvariant::Error| e.to_string())?;
|
||||
let uri = uris.first().ok_or_else(|| "no file in the portal's answer".to_string())?;
|
||||
let path = file_uri_path(uri).ok_or_else(|| format!("not a local file: {uri}"))?;
|
||||
let filter = results.get("current_filter").and_then(|v| {
|
||||
let val = v.try_clone().ok()?;
|
||||
if let Ok((label, globs)) = <(String, Vec<(u32, String)>)>::try_from(val.clone()) {
|
||||
let mut exts = Vec::new();
|
||||
for (_, g) in globs {
|
||||
let s = g.trim_start_matches('*').trim_start_matches('.');
|
||||
if !s.is_empty() && s != "*" {
|
||||
exts.push(s.to_ascii_lowercase());
|
||||
}
|
||||
}
|
||||
if exts.is_empty() {
|
||||
exts.extend(extract_exts_from_label(&label));
|
||||
}
|
||||
Some((label, exts))
|
||||
} else if let Ok((label, exts)) = <(String, Vec<String>)>::try_from(val.clone()) {
|
||||
Some((label, exts.into_iter().map(|s| s.to_ascii_lowercase()).collect()))
|
||||
} else if let Ok(label) = String::try_from(val) {
|
||||
let exts = extract_exts_from_label(&label);
|
||||
Some((label, exts))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
Ok(Some(Saved { path, filter }))
|
||||
}
|
||||
|
||||
/// Extracts file extensions from a filter label such as `"PDF (*.pdf)"` or `"*.pdf"`.
|
||||
fn extract_exts_from_label(label: &str) -> Vec<String> {
|
||||
let text = if let Some(inside) = label.split('(').nth(1).and_then(|s| s.split(')').next()) { inside } else { label };
|
||||
text.split(|c: char| c == ';' || c == ',' || c.is_whitespace())
|
||||
.filter_map(|part| {
|
||||
let s = part.trim().trim_start_matches('*').trim_start_matches('.');
|
||||
if s.is_empty() || s == "*" || s.contains('/') || s.contains('\\') { None } else { Some(s.to_ascii_lowercase()) }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Converts a `file://` URI from the portal into a local `PathBuf`.
|
||||
/// Returns `None` for non-local URIs or invalid percent-encoding.
|
||||
pub fn file_uri_path(uri: &str) -> Option<PathBuf> {
|
||||
let rest = uri.strip_prefix("file://")?;
|
||||
// Only local files: `file:///…` or `file://localhost/…`.
|
||||
let rest = rest.strip_prefix("localhost").unwrap_or(rest);
|
||||
if !rest.starts_with('/') {
|
||||
return None;
|
||||
}
|
||||
let bytes = rest.as_bytes();
|
||||
let mut out = Vec::with_capacity(bytes.len());
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
if bytes.get(i) == Some(&b'%') {
|
||||
let hex = bytes.get(i + 1..i + 3).and_then(|h| std::str::from_utf8(h).ok()).and_then(|h| u8::from_str_radix(h, 16).ok())?;
|
||||
out.push(hex);
|
||||
i = i.saturating_add(3);
|
||||
} else if let Some(&b) = bytes.get(i) {
|
||||
out.push(b);
|
||||
i = i.saturating_add(1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
Some(PathBuf::from(std::ffi::OsString::from_vec(out)))
|
||||
}
|
||||
|
||||
/// Returns the index of the chosen filter in `filters` by matching the picked index, or the first filter
|
||||
/// if only one was offered.
|
||||
fn chosen_type(filters: &[Filter], picked: Option<usize>) -> Option<usize> {
|
||||
match picked {
|
||||
Some(i) if i < filters.len() => Some(i),
|
||||
None if filters.len() == 1 => Some(0),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `path` with the extension of the file type picked in the save dialog (`chosen`),
|
||||
/// where the dialog left the suggested name's extension alone (#6180).
|
||||
///
|
||||
/// The GTK file chooser keeps the typed name when another type is picked. Only a name that
|
||||
/// still has the extension the dialog opened with (`opened_with`, e.g. `Document1.docx` with
|
||||
/// PDF picked) or none gets the type's extension, as PolyMeilex/rfd#341 does. An extension the
|
||||
/// user explicitly typed that WordCraft saves (e.g. `art.txt` with PDF untouched) is kept,
|
||||
/// and so does one of the type's own extensions. Any extension that WordCraft does not save
|
||||
/// (e.g. `scan.2024`) is treated as part of the stem, and the type's extension is added.
|
||||
pub fn with_chosen_type(path: &Path, opened_with: Option<&str>, chosen: &[String]) -> PathBuf {
|
||||
let Some(first) = chosen.first() else { return path.to_path_buf() };
|
||||
let ext = path.extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase);
|
||||
let opened_ext = opened_with
|
||||
.and_then(|n| Path::new(n).extension())
|
||||
.and_then(|e| e.to_str())
|
||||
.map(str::to_ascii_lowercase)
|
||||
.filter(|e| wordcraft_engine::io::SAVE_EXTS.contains(&e.as_str()));
|
||||
match ext {
|
||||
Some(ext) if chosen.contains(&ext) => path.to_path_buf(),
|
||||
Some(ext) if opened_ext.as_ref() == Some(&ext) => path.with_extension(first),
|
||||
Some(ext) if wordcraft_engine::io::SAVE_EXTS.contains(&ext.as_str()) => path.to_path_buf(),
|
||||
_ => {
|
||||
let mut name = path.as_os_str().to_owned();
|
||||
name.push(".");
|
||||
name.push(first);
|
||||
PathBuf::from(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if `path` exists as a file, directory, or symlink on disk.
|
||||
fn entry_exists(path: &Path) -> bool {
|
||||
match path.symlink_metadata() {
|
||||
Ok(_) => true,
|
||||
Err(e) => e.kind() != std::io::ErrorKind::NotFound,
|
||||
}
|
||||
}
|
||||
|
||||
/// Closes the active portal request handle by calling `org.freedesktop.portal.Request.Close`.
|
||||
pub fn close_request(conn: &zbus::blocking::Connection, portal: Option<&str>, handle: &OwnedObjectPath) {
|
||||
const DESKTOP: &str = "org.freedesktop.portal.Desktop";
|
||||
const REQUEST: &str = "org.freedesktop.portal.Request";
|
||||
let dest = portal.unwrap_or(DESKTOP);
|
||||
if let Ok(proxy) = Proxy::new(conn, dest, handle.as_str(), REQUEST) {
|
||||
let _ = proxy.call_method("Close", &());
|
||||
}
|
||||
}
|
||||
|
||||
/// RAII guard that closes an open portal request handle if dropped before being disarmed.
|
||||
struct RequestGuard<'a> {
|
||||
conn: &'a zbus::blocking::Connection,
|
||||
portal: Option<String>,
|
||||
handle: Option<OwnedObjectPath>,
|
||||
}
|
||||
|
||||
impl<'a> RequestGuard<'a> {
|
||||
/// Creates a new guard for the given request `handle`.
|
||||
fn new(conn: &'a zbus::blocking::Connection, portal: Option<String>, handle: OwnedObjectPath) -> Self {
|
||||
Self { conn, portal, handle: Some(handle) }
|
||||
}
|
||||
|
||||
/// Disarms the guard when a response has been successfully received, preventing `Close` from being called on drop.
|
||||
fn disarm(&mut self) {
|
||||
self.handle = None;
|
||||
}
|
||||
|
||||
/// Returns a reference to the request handle, if still active.
|
||||
fn handle(&self) -> Option<&OwnedObjectPath> {
|
||||
self.handle.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for RequestGuard<'a> {
|
||||
/// Closes the active portal request handle when dropped unless disarmed.
|
||||
fn drop(&mut self) {
|
||||
if let Some(handle) = self.handle.take() {
|
||||
close_request(self.conn, self.portal.as_deref(), &handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Asks the XDG desktop portal on `conn` for the `SaveFile` dialog of `req` and waits for its answer.
|
||||
pub fn save_file(conn: &zbus::blocking::Connection, req: &SaveRequest) -> Result<Option<Saved>, String> {
|
||||
const DESKTOP: &str = "org.freedesktop.portal.Desktop";
|
||||
const REQUEST: &str = "org.freedesktop.portal.Request";
|
||||
static NEXT: AtomicU32 = AtomicU32::new(0);
|
||||
let token = format!("wordcraft_save_{}_{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed));
|
||||
|
||||
// Listen before asking, on every request path: a portal older than version 0.9 ignores
|
||||
// `handle_token` and answers on the request path it returns, and its answer can come before
|
||||
// the call's reply. The stream keeps every `Response` from the time it is registered.
|
||||
let dbus = zbus::blocking::fdo::DBusProxy::new(conn).map_err(|e| e.to_string())?;
|
||||
let rule = zbus::MatchRule::builder()
|
||||
.msg_type(zbus::message::Type::Signal)
|
||||
.sender(DESKTOP)
|
||||
.and_then(|b| b.interface(REQUEST))
|
||||
.and_then(|b| b.member("Response"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.build();
|
||||
dbus.add_match_rule(rule).map_err(|e| e.to_string())?;
|
||||
|
||||
let owner_rule = zbus::MatchRule::builder()
|
||||
.msg_type(zbus::message::Type::Signal)
|
||||
.sender("org.freedesktop.DBus")
|
||||
.and_then(|b| b.interface("org.freedesktop.DBus"))
|
||||
.and_then(|b| b.member("NameOwnerChanged"))
|
||||
.map_err(|e| e.to_string())?
|
||||
.build();
|
||||
dbus.add_match_rule(owner_rule).map_err(|e| e.to_string())?;
|
||||
|
||||
let responses = MessageIterator::from(conn);
|
||||
let chooser = Proxy::new(conn, DESKTOP, "/org/freedesktop/portal/desktop", "org.freedesktop.portal.FileChooser").map_err(|e| e.to_string())?;
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
let call = chooser.call_method("SaveFile", &(req.parent.as_str(), "Save As", options(req, &token))).map_err(|e| e.to_string())?;
|
||||
let handle: OwnedObjectPath = call.body().deserialize().map_err(|e| e.to_string())?;
|
||||
let portal = call.header().sender().map(|s| s.as_str().to_string());
|
||||
|
||||
let mut guard = RequestGuard::new(conn, portal.clone(), handle);
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let _listener = std::thread::Builder::new().name("portal save listener".into()).spawn(move || {
|
||||
for msg in responses.filter_map(Result::ok) {
|
||||
if tx.send(msg).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let reply = loop {
|
||||
if req.cancel.as_ref().is_some_and(CancelToken::is_cancelled) {
|
||||
return Err(ERR_CANCELLED.to_string());
|
||||
}
|
||||
match rx.recv_timeout(Duration::from_millis(100)) {
|
||||
Ok(m) => {
|
||||
let header = m.header();
|
||||
if header.path().map(|p| p.as_str()) == guard.handle().map(|h| h.as_str()) && header.sender().map(|s| s.as_str()) == portal.as_deref()
|
||||
{
|
||||
break Ok(m);
|
||||
}
|
||||
#[allow(clippy::collapsible_if)]
|
||||
if header.interface().map(|i| i.as_str()) == Some("org.freedesktop.DBus")
|
||||
&& header.member().map(|m| m.as_str()) == Some("NameOwnerChanged")
|
||||
{
|
||||
if let Ok((name, old_owner, "")) = m.body().deserialize::<(&str, &str, &str)>() {
|
||||
if (name == DESKTOP || portal.as_deref() == Some(name)) && (portal.is_none() || portal.as_deref() == Some(old_owner)) {
|
||||
break Err("the desktop portal terminated while the save dialog was open".to_string());
|
||||
}
|
||||
if name.starts_with("org.freedesktop.impl.portal.desktop") {
|
||||
break Err("the desktop portal backend terminated while the save dialog was open".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
if req.timeout.is_some_and(|t| start.elapsed() >= t) {
|
||||
break Err("the save dialog request timed out".to_string());
|
||||
}
|
||||
}
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
|
||||
break Err("the save dialog closed without an answer".to_string());
|
||||
}
|
||||
}
|
||||
}?;
|
||||
|
||||
guard.disarm();
|
||||
let elapsed = start.elapsed();
|
||||
let (code, results): (u32, HashMap<String, OwnedValue>) = reply.body().deserialize().map_err(|e| e.to_string())?;
|
||||
answer(code, &results, elapsed)
|
||||
}
|
||||
|
||||
/// Returns true if `path` is located in the XDG document portal (`/run/user/<uid>/doc/…`, or
|
||||
/// `/run/flatpak/doc/…` inside a Flatpak sandbox).
|
||||
fn is_document_portal(path: &Path) -> bool {
|
||||
let s = path.to_string_lossy();
|
||||
((s.starts_with("/run/user/") || s.starts_with("/var/run/user/")) && s.contains("/doc/")) || s.starts_with("/run/flatpak/doc/")
|
||||
}
|
||||
|
||||
/// Error message returned when a pending save request was cancelled by the caller.
|
||||
pub const ERR_CANCELLED: &str = "the save dialog request was cancelled";
|
||||
|
||||
/// Returns true if `err` represents a save request cancelled by the caller (not a cancel in the
|
||||
/// dialog itself, which is `Ok(None)`).
|
||||
pub fn is_cancelled_error(err: &str) -> bool {
|
||||
err == ERR_CANCELLED
|
||||
}
|
||||
|
||||
/// Error message returned when the portal dialog does not report the selected filter and
|
||||
/// the saved filename does not explicitly specify a distinct format.
|
||||
pub const ERR_AMBIGUOUS_FILTER: &str = "the save dialog did not report which file type was picked";
|
||||
|
||||
/// Returns true if `err` represents an ambiguous filter error from the desktop portal.
|
||||
pub fn is_ambiguous_filter_error(err: &str) -> bool {
|
||||
err == ERR_AMBIGUOUS_FILTER
|
||||
}
|
||||
|
||||
/// Finds the index of the filter in `filters` matching `saved_filter`, first by exact label,
|
||||
/// then by unambiguous match against the filter's extensions.
|
||||
fn match_filter(filters: &[Filter], saved_filter: Option<&Filter>) -> Option<usize> {
|
||||
let (label, exts) = saved_filter?;
|
||||
if let Some(pos) = filters.iter().position(|(l, _)| l == label) {
|
||||
return Some(pos);
|
||||
}
|
||||
if !exts.is_empty() {
|
||||
let matching: Vec<usize> = filters
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, (_, f_exts))| exts.iter().any(|e| f_exts.iter().any(|fe| fe.eq_ignore_ascii_case(e))))
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
if matching.len() == 1 {
|
||||
return matching.first().copied();
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns true if the portal did not provide a unique matching filter when multiple filters were offered
|
||||
/// and the user did not explicitly type a filename with a distinct supported format extension.
|
||||
pub fn is_ambiguous_filter(saved: &Saved, req: &SaveRequest) -> bool {
|
||||
if req.filters.len() <= 1 {
|
||||
return false;
|
||||
}
|
||||
if match_filter(&req.filters, saved.filter.as_ref()).is_some() {
|
||||
return false;
|
||||
}
|
||||
let ext = saved.path.extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase);
|
||||
let suggested_ext = req
|
||||
.suggested
|
||||
.as_deref()
|
||||
.and_then(|n| Path::new(n).extension())
|
||||
.and_then(|e| e.to_str())
|
||||
.map(str::to_ascii_lowercase)
|
||||
.filter(|e| wordcraft_engine::io::SAVE_EXTS.contains(&e.as_str()));
|
||||
match ext {
|
||||
Some(ext) if wordcraft_engine::io::SAVE_EXTS.contains(&ext.as_str()) => suggested_ext.as_ref() == Some(&ext),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Save As on Linux and BSDs: the portal's dialog, with the file name given the extension of the
|
||||
/// file type picked in the dropdown. When that changes the name to one that already exists on disk,
|
||||
/// the dialog opens again under the new name, so the portal asks before replacing it (it asked only
|
||||
/// about the typed name).
|
||||
pub fn save_as(conn: &zbus::blocking::Connection, mut req: SaveRequest) -> Result<Option<PathBuf>, String> {
|
||||
loop {
|
||||
let Some(saved) = save_file(conn, &req)? else { return Ok(None) };
|
||||
if is_ambiguous_filter(&saved, &req) {
|
||||
return Err(ERR_AMBIGUOUS_FILTER.to_string());
|
||||
}
|
||||
let picked = match_filter(&req.filters, saved.filter.as_ref());
|
||||
let exts: &[String] = match chosen_type(&req.filters, picked) {
|
||||
Some(i) => req.filters.get(i).map(|(_, e)| e.as_slice()).unwrap_or(&[]),
|
||||
None => &[],
|
||||
};
|
||||
let path = with_chosen_type(&saved.path, req.suggested.as_deref(), exts);
|
||||
let needs_reopen = entry_exists(&path) || (path != saved.path && is_document_portal(&saved.path));
|
||||
if path == saved.path || !needs_reopen {
|
||||
return Ok(Some(path));
|
||||
}
|
||||
req.name = path.file_name().map(|n| n.to_string_lossy().into_owned());
|
||||
req.suggested = req.name.clone();
|
||||
req.folder = path.parent().map(Path::to_path_buf);
|
||||
req.current = picked;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// #6180: the file type picked in the save dialog gives the name its extension where the
|
||||
/// dialog left the one it opened with, and only there.
|
||||
#[test]
|
||||
fn the_picked_file_type_gives_the_name_its_extension() {
|
||||
let exts = |e: &[&str]| e.iter().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||
let (pdf, docx) = (exts(&["pdf"]), exts(&["docx", "docm"]));
|
||||
let opened = Some("Document1.docx");
|
||||
let cases = [
|
||||
// PDF picked, the name as the dialog opened (or renamed, its extension kept).
|
||||
("/docs/Document1.docx", opened, &pdf, "/docs/Document1.pdf"),
|
||||
("/docs/letter.docx", opened, &pdf, "/docs/letter.pdf"),
|
||||
("/docs/LETTER.DOCX", opened, &pdf, "/docs/LETTER.pdf"),
|
||||
// No extension: takes the picked type's extension.
|
||||
("/docs/report", opened, &pdf, "/docs/report.pdf"),
|
||||
// An extension WordCraft doesn't save is part of the name.
|
||||
("/docs/scan.2024", opened, &pdf, "/docs/scan.2024.pdf"),
|
||||
// An opened extension WordCraft doesn't save (e.g. .5 in Report 1.5) is kept as part of the stem.
|
||||
("/docs/Report 1.5", Some("Report 1.5"), &docx, "/docs/Report 1.5.docx"),
|
||||
// Typed, an extension WordCraft saves is the user's: kept, whatever the type says.
|
||||
("/docs/notes.txt", opened, &pdf, "/docs/notes.txt"),
|
||||
("/docs/page.html", opened, &pdf, "/docs/page.html"),
|
||||
("/docs/file.rtf", opened, &pdf, "/docs/file.rtf"),
|
||||
// Already the type's: kept.
|
||||
("/docs/letter.pdf", opened, &pdf, "/docs/letter.pdf"),
|
||||
("/docs/letter.PDF", opened, &pdf, "/docs/letter.PDF"),
|
||||
// The dialog opened on a .dotx template: that is the extension the type replaces.
|
||||
("/docs/memo.dotx", Some("memo.dotx"), &pdf, "/docs/memo.pdf"),
|
||||
("/docs/doc.docx", Some("memo.dotx"), &pdf, "/docs/doc.docx"),
|
||||
// Word document picked for a file named without extension.
|
||||
("/docs/contract", opened, &docx, "/docs/contract.docx"),
|
||||
];
|
||||
for (typed, opened_with, chosen, want) in cases {
|
||||
assert_eq!(
|
||||
with_chosen_type(Path::new(typed), opened_with, chosen),
|
||||
PathBuf::from(want),
|
||||
"failed for typed={typed}, opened_with={opened_with:?}"
|
||||
);
|
||||
}
|
||||
// No type picked: the name as typed is kept.
|
||||
assert_eq!(with_chosen_type(Path::new("/docs/report.docx"), opened, &[]), PathBuf::from("/docs/report.docx"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_uris_become_local_paths() {
|
||||
assert_eq!(file_uri_path("file:///home/me/Document1.docx"), Some(PathBuf::from("/home/me/Document1.docx")));
|
||||
assert_eq!(file_uri_path("file:///home/me/A%20b%C3%A9.pdf"), Some(PathBuf::from("/home/me/A bé.pdf")));
|
||||
assert_eq!(file_uri_path("file://localhost/tmp/x.pdf"), Some(PathBuf::from("/tmp/x.pdf")));
|
||||
assert_eq!(file_uri_path("file:///tmp/a%ZZ.pdf"), None);
|
||||
assert_eq!(file_uri_path("file:///tmp/a%"), None);
|
||||
assert_eq!(file_uri_path("file:///tmp/a%2"), None);
|
||||
assert_eq!(file_uri_path("sftp://host/x.pdf"), None);
|
||||
assert_eq!(file_uri_path("file://host/x.pdf"), None);
|
||||
assert_eq!(file_uri_path("http://example.com/doc.pdf"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x11_windows_are_named_to_the_portal() {
|
||||
use eframe::wgpu::rwh::{RawWindowHandle, XlibWindowHandle};
|
||||
assert_eq!(parent_id(Some(RawWindowHandle::Xlib(XlibWindowHandle::new(0x3a0000f)))), "x11:3a0000f");
|
||||
assert_eq!(parent_id(None), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn answer_parses_portal_response_or_errors_on_quick_failure() {
|
||||
let mut results = HashMap::new();
|
||||
let uris: Vec<String> = vec!["file:///tmp/doc.docx".to_string()];
|
||||
results.insert("uris".to_string(), OwnedValue::try_from(Value::from(uris)).unwrap());
|
||||
let globs: Vec<(u32, String)> = vec![(0u32, "*.docx".to_string())];
|
||||
let filter: (String, Vec<(u32, String)>) = ("Word Document (*.docx)".to_string(), globs);
|
||||
results.insert("current_filter".to_string(), OwnedValue::try_from(Value::from(filter)).unwrap());
|
||||
|
||||
// Code 0 with filter
|
||||
let ans = answer(0, &results, Duration::from_millis(1000)).unwrap();
|
||||
assert_eq!(
|
||||
ans,
|
||||
Some(Saved { path: PathBuf::from("/tmp/doc.docx"), filter: Some(("Word Document (*.docx)".to_string(), vec!["docx".to_string()])) })
|
||||
);
|
||||
|
||||
// Code 0 without filter
|
||||
let mut results_no_filter = HashMap::new();
|
||||
results_no_filter.insert("uris".to_string(), results.get("uris").unwrap().clone());
|
||||
let ans_no_filter = answer(0, &results_no_filter, Duration::from_millis(1000)).unwrap();
|
||||
assert_eq!(ans_no_filter, Some(Saved { path: PathBuf::from("/tmp/doc.docx"), filter: None }));
|
||||
|
||||
// Code 1 (Cancel)
|
||||
assert_eq!(answer(1, &results, Duration::from_millis(50)).unwrap(), None);
|
||||
|
||||
// Code 2 quick (< TOO_QUICK)
|
||||
assert!(answer(2, &results, Duration::from_millis(100)).is_err());
|
||||
|
||||
// Code 2 slow (>= TOO_QUICK)
|
||||
assert_eq!(answer(2, &results, Duration::from_millis(500)).unwrap(), None);
|
||||
|
||||
// Code 3 (failure)
|
||||
assert!(answer(3, &results, Duration::from_millis(1000)).is_err());
|
||||
}
|
||||
|
||||
struct KillOnDrop(std::process::Child);
|
||||
|
||||
impl Drop for KillOnDrop {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.0.kill();
|
||||
let _ = self.0.wait();
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration for spawning a mock portal daemon.
|
||||
struct MockPortalConfig {
|
||||
code: u32,
|
||||
delay_reply: Option<Duration>,
|
||||
exit_after_reply: bool,
|
||||
send_response: bool,
|
||||
close_called: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
|
||||
emit_backend_owner_loss: bool,
|
||||
}
|
||||
|
||||
impl Default for MockPortalConfig {
|
||||
fn default() -> Self {
|
||||
Self { code: 0, delay_reply: None, exit_after_reply: false, send_response: true, close_called: None, emit_backend_owner_loss: false }
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawns a mock portal daemon that answers `SaveFile` and handles `Close` according to `config`.
|
||||
fn mock_portal_with_config(config: MockPortalConfig) -> Option<(zbus::blocking::Connection, KillOnDrop)> {
|
||||
use std::io::BufRead as _;
|
||||
|
||||
let daemon = std::process::Command::new("dbus-daemon")
|
||||
.args(["--session", "--nofork", "--print-address=1"])
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.ok()?;
|
||||
let mut guard = KillOnDrop(daemon);
|
||||
let mut address = String::new();
|
||||
std::io::BufReader::new(guard.0.stdout.take()?).read_line(&mut address).ok()?;
|
||||
let address = address.trim().to_string();
|
||||
let server =
|
||||
zbus::blocking::connection::Builder::address(address.as_str()).ok()?.name("org.freedesktop.portal.Desktop").ok()?.build().ok()?;
|
||||
let incoming = zbus::blocking::MessageIterator::from(&server);
|
||||
let thread_address = address.clone();
|
||||
std::thread::spawn(move || {
|
||||
for msg in incoming {
|
||||
let Ok(msg) = msg else { break };
|
||||
let header = msg.header();
|
||||
if header.message_type() != zbus::message::Type::MethodCall {
|
||||
continue;
|
||||
}
|
||||
if header.member().map(|m| m.as_str()) == Some("Close") {
|
||||
if let Some(flag) = &config.close_called {
|
||||
flag.store(true, Ordering::Relaxed);
|
||||
}
|
||||
let _ = server.reply(&header, &());
|
||||
continue;
|
||||
}
|
||||
if header.member().map(|m| m.as_str()) != Some("SaveFile") {
|
||||
continue;
|
||||
}
|
||||
let Ok((_parent, _title, options)) = msg.body().deserialize::<(String, String, HashMap<String, OwnedValue>)>() else {
|
||||
break;
|
||||
};
|
||||
let text = |key: &str| options.get(key).and_then(|v| String::try_from(v.try_clone().ok()?).ok());
|
||||
let Some(sender) = header.sender().map(|s| s.as_str().trim_start_matches(':').replace('.', "_")) else { break };
|
||||
let token = text("handle_token").unwrap_or_default();
|
||||
let path = format!("/org/freedesktop/portal/desktop/request/{sender}/{token}");
|
||||
let Ok(handle) = OwnedObjectPath::try_from(path.as_str()) else { break };
|
||||
|
||||
if config.exit_after_reply {
|
||||
let _ = server.reply(&header, &handle);
|
||||
drop(server);
|
||||
break;
|
||||
}
|
||||
|
||||
if config.emit_backend_owner_loss {
|
||||
let _ = server.reply(&header, &handle);
|
||||
let addr = thread_address.clone();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
if let Ok(backend) = zbus::blocking::connection::Builder::address(addr.as_str())
|
||||
.and_then(|b| b.name("org.freedesktop.impl.portal.desktop.gtk"))
|
||||
.and_then(|b| b.build())
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
drop(backend);
|
||||
}
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if config.send_response {
|
||||
let to = header.sender().cloned().map(zbus::names::BusName::from);
|
||||
let results: HashMap<&str, Value<'_>> = HashMap::new();
|
||||
let _ = server.emit_signal(to, path.as_str(), "org.freedesktop.portal.Request", "Response", &(config.code, &results));
|
||||
}
|
||||
|
||||
if let Some(delay) = config.delay_reply {
|
||||
std::thread::sleep(delay);
|
||||
}
|
||||
if server.reply(&header, &handle).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let client = zbus::blocking::connection::Builder::address(address.as_str()).ok()?.build().ok()?;
|
||||
Some((client, guard))
|
||||
}
|
||||
|
||||
/// Spawns a mock portal daemon that answers `SaveFile`.
|
||||
/// If `delay_reply` is set, it emits the Response signal immediately and delays the method
|
||||
/// reply by that duration. If `exit_after_reply` is set, it replies to `SaveFile` and exits
|
||||
/// without sending a `Response`.
|
||||
fn mock_portal(code: u32, delay_reply: Option<Duration>, exit_after_reply: bool) -> Option<(zbus::blocking::Connection, KillOnDrop)> {
|
||||
mock_portal_with_config(MockPortalConfig {
|
||||
code,
|
||||
delay_reply,
|
||||
exit_after_reply,
|
||||
send_response: true,
|
||||
close_called: None,
|
||||
emit_backend_owner_loss: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// A delayed SaveFile method reply with an already-queued response code 2 measures elapsed time
|
||||
/// from before calling SaveFile, so it is treated as a user cancellation rather than a quick
|
||||
/// portal failure that re-opens another dialog.
|
||||
#[test]
|
||||
fn delayed_method_reply_with_queued_cancel_is_not_treated_as_quick_failure() {
|
||||
let Some((client, _daemon)) = mock_portal(2, Some(Duration::from_millis(350)), false) else {
|
||||
eprintln!("no dbus-daemon: skipped");
|
||||
return;
|
||||
};
|
||||
let req = SaveRequest {
|
||||
name: Some("test.docx".to_string()),
|
||||
suggested: Some("test.docx".to_string()),
|
||||
filters: vec![("Word Document (*.docx)".to_string(), vec!["docx".to_string()])],
|
||||
..Default::default()
|
||||
};
|
||||
let res = save_file(&client, &req);
|
||||
assert_eq!(res, Ok(None));
|
||||
}
|
||||
|
||||
/// If the desktop portal terminates while a save dialog is open (e.g. crashing after replying
|
||||
/// to SaveFile), save_file detects NameOwnerChanged and returns an error immediately.
|
||||
#[test]
|
||||
fn portal_terminating_while_open_returns_error() {
|
||||
let Some((client, _daemon)) = mock_portal(0, None, true) else {
|
||||
eprintln!("no dbus-daemon: skipped");
|
||||
return;
|
||||
};
|
||||
let req = SaveRequest {
|
||||
name: Some("test.docx".to_string()),
|
||||
suggested: Some("test.docx".to_string()),
|
||||
filters: vec![("Word Document (*.docx)".to_string(), vec!["docx".to_string()])],
|
||||
..Default::default()
|
||||
};
|
||||
let res = save_file(&client, &req);
|
||||
assert!(res.is_err(), "portal termination returns an error so rfd fallback runs: got {res:?}");
|
||||
}
|
||||
|
||||
/// If the desktop portal backend implementation (e.g. GTK, GNOME, KDE) terminates while the
|
||||
/// dialog is open, save_file detects NameOwnerChanged on the implementation service and returns
|
||||
/// an error immediately so the rfd fallback can run.
|
||||
#[test]
|
||||
fn portal_backend_terminating_while_open_returns_error() {
|
||||
let Some((client, _daemon)) =
|
||||
mock_portal_with_config(MockPortalConfig { emit_backend_owner_loss: true, send_response: false, ..Default::default() })
|
||||
else {
|
||||
eprintln!("no dbus-daemon: skipped");
|
||||
return;
|
||||
};
|
||||
let req = SaveRequest {
|
||||
name: Some("test.docx".to_string()),
|
||||
suggested: Some("test.docx".to_string()),
|
||||
filters: vec![("Word Document (*.docx)".to_string(), vec!["docx".to_string()])],
|
||||
..Default::default()
|
||||
};
|
||||
let res = save_file(&client, &req);
|
||||
assert!(res.is_err(), "backend termination returns an error so rfd fallback runs: got {res:?}");
|
||||
assert!(res.as_ref().unwrap_err().contains("backend terminated"), "expected backend termination error: got {res:?}");
|
||||
}
|
||||
|
||||
/// When a save request is cancelled via `CancelToken`, save_file aborts waiting for the response,
|
||||
/// calls Close on the portal request object path, and returns a cancellation error immediately.
|
||||
#[test]
|
||||
fn save_file_cancellation_closes_request_and_returns_error() {
|
||||
let close_called = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let Some((client, _daemon)) =
|
||||
mock_portal_with_config(MockPortalConfig { send_response: false, close_called: Some(close_called.clone()), ..Default::default() })
|
||||
else {
|
||||
eprintln!("no dbus-daemon: skipped");
|
||||
return;
|
||||
};
|
||||
let cancel = CancelToken::new();
|
||||
let cancel_thread = cancel.clone();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
cancel_thread.cancel();
|
||||
});
|
||||
let req = SaveRequest {
|
||||
name: Some("test.docx".to_string()),
|
||||
suggested: Some("test.docx".to_string()),
|
||||
filters: vec![("Word Document (*.docx)".to_string(), vec!["docx".to_string()])],
|
||||
cancel: Some(cancel),
|
||||
..Default::default()
|
||||
};
|
||||
let res = save_file(&client, &req);
|
||||
assert!(res.is_err(), "cancelled request returns error: got {res:?}");
|
||||
assert!(res.as_ref().unwrap_err().contains("cancelled"), "expected cancellation error: got {res:?}");
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
assert!(close_called.load(Ordering::Relaxed), "Close must be called on portal request handle");
|
||||
}
|
||||
|
||||
/// When a save request times out waiting for a stalled portal backend response, save_file aborts,
|
||||
/// calls Close on the portal request object path, and returns a timeout error.
|
||||
#[test]
|
||||
fn save_file_timeout_closes_request_and_returns_error() {
|
||||
let close_called = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let Some((client, _daemon)) =
|
||||
mock_portal_with_config(MockPortalConfig { send_response: false, close_called: Some(close_called.clone()), ..Default::default() })
|
||||
else {
|
||||
eprintln!("no dbus-daemon: skipped");
|
||||
return;
|
||||
};
|
||||
let req = SaveRequest {
|
||||
name: Some("test.docx".to_string()),
|
||||
suggested: Some("test.docx".to_string()),
|
||||
filters: vec![("Word Document (*.docx)".to_string(), vec!["docx".to_string()])],
|
||||
timeout: Some(Duration::from_millis(50)),
|
||||
..Default::default()
|
||||
};
|
||||
let res = save_file(&client, &req);
|
||||
assert!(res.is_err(), "timed out request returns error: got {res:?}");
|
||||
assert!(res.as_ref().unwrap_err().contains("timed out"), "expected timeout error: got {res:?}");
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
assert!(close_called.load(Ordering::Relaxed), "Close must be called on portal request handle");
|
||||
}
|
||||
|
||||
/// `entry_exists` detects existing files and dangling symlinks, distinguishing them from
|
||||
/// absent entries.
|
||||
#[test]
|
||||
fn entry_exists_detects_dangling_symlinks() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let target = dir.path().join("nonexistent_target.txt");
|
||||
let link = dir.path().join("dangling_link.txt");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
std::os::unix::fs::symlink(&target, &link).unwrap();
|
||||
assert!(!link.exists(), "target does not exist so exists() is false");
|
||||
assert!(entry_exists(&link), "symlink entry exists so entry_exists() is true");
|
||||
}
|
||||
assert!(!entry_exists(&target), "nonexistent file does not exist");
|
||||
}
|
||||
|
||||
/// `is_document_portal` identifies paths within the XDG document portal mount, including the
|
||||
/// `/run/flatpak/doc` mount a Flatpak sandbox sees.
|
||||
#[test]
|
||||
fn is_document_portal_detects_portal_fuse_paths() {
|
||||
assert!(is_document_portal(Path::new("/run/user/1000/doc/1234abcd/test.docx")));
|
||||
assert!(is_document_portal(Path::new("/var/run/user/1000/doc/by-app/app/test.docx")));
|
||||
assert!(is_document_portal(Path::new("/run/flatpak/doc/1234abcd/test.docx")));
|
||||
assert!(!is_document_portal(Path::new("/home/user/Documents/test.docx")));
|
||||
assert!(!is_document_portal(Path::new("/tmp/doc/test.docx")));
|
||||
assert!(!is_document_portal(Path::new("/run/flatpak/other/test.docx")));
|
||||
}
|
||||
|
||||
/// When the portal omits `current_filter`, ambiguous saves (unchanged suggested extension or bare name)
|
||||
/// are rejected, while an explicitly typed different format is accepted.
|
||||
#[test]
|
||||
fn ambiguous_filters_require_explicit_format() {
|
||||
let exts = |e: &[&str]| e.iter().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||
let filters = vec![("Word Document (*.docx)".into(), exts(&["docx"])), ("PDF (*.pdf)".into(), exts(&["pdf"]))];
|
||||
let req = SaveRequest { filters: filters.clone(), suggested: Some("Document1.docx".into()), ..Default::default() };
|
||||
|
||||
// Reported filter with matching label is not ambiguous.
|
||||
let with_filter = Saved { path: PathBuf::from("/docs/Document1.docx"), filter: Some(("Word Document (*.docx)".into(), exts(&["docx"]))) };
|
||||
assert_eq!(match_filter(&filters, with_filter.filter.as_ref()), Some(0));
|
||||
assert!(!is_ambiguous_filter(&with_filter, &req));
|
||||
|
||||
// Reported filter with unmatched label but unambiguous extension (e.g. KDE *.pdf) is not ambiguous.
|
||||
let kde_pdf_filter = Saved { path: PathBuf::from("/docs/Document1.docx"), filter: Some(("*.pdf".into(), exts(&["pdf"]))) };
|
||||
assert_eq!(match_filter(&filters, kde_pdf_filter.filter.as_ref()), Some(1));
|
||||
assert!(!is_ambiguous_filter(&kde_pdf_filter, &req));
|
||||
|
||||
// Reported filter with unrecognized extensions and unchanged suggested extension is ambiguous.
|
||||
let unknown_filter = Saved { path: PathBuf::from("/docs/Document1.docx"), filter: Some(("Custom (*.xyz)".into(), exts(&["xyz"]))) };
|
||||
assert_eq!(match_filter(&filters, unknown_filter.filter.as_ref()), None);
|
||||
assert!(is_ambiguous_filter(&unknown_filter, &req));
|
||||
|
||||
// Multiple filters matching same extension is ambiguous when label doesn't match.
|
||||
let duplicate_filters = vec![("Plain Text (*.txt)".into(), exts(&["txt"])), ("Text Document (*.txt)".into(), exts(&["txt"]))];
|
||||
let dup_filter = ("*.txt".to_string(), exts(&["txt"]));
|
||||
assert_eq!(match_filter(&duplicate_filters, Some(&dup_filter)), None);
|
||||
|
||||
// Missing filter with unchanged suggested extension is ambiguous.
|
||||
let unchanged = Saved { path: PathBuf::from("/docs/Document1.docx"), filter: None };
|
||||
assert!(is_ambiguous_filter(&unchanged, &req));
|
||||
|
||||
// Missing filter with bare filename is ambiguous.
|
||||
let bare = Saved { path: PathBuf::from("/docs/Document1"), filter: None };
|
||||
assert!(is_ambiguous_filter(&bare, &req));
|
||||
|
||||
// Missing filter but user explicitly typed a different format is NOT ambiguous.
|
||||
let explicit = Saved { path: PathBuf::from("/docs/Document1.pdf"), filter: None };
|
||||
assert!(!is_ambiguous_filter(&explicit, &req));
|
||||
|
||||
// Single filter offered is never ambiguous even if filter is omitted.
|
||||
let single_req =
|
||||
SaveRequest { filters: vec![("PDF (*.pdf)".into(), exts(&["pdf"]))], suggested: Some("Document1.docx".into()), ..Default::default() };
|
||||
assert!(!is_ambiguous_filter(&unchanged, &single_req));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
//! A WordCraft started by an AppImage app inherits that app's `APPIMAGE`, which the self-update
|
||||
//! would take for this copy's file and replace (#5653). The real program, started both ways.
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::os::unix::ffi::OsStringExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const NOTICE: &str = "ignoring the AppImage";
|
||||
|
||||
/// A folder of its own for one test, removed when it ends.
|
||||
struct Tmp(PathBuf);
|
||||
|
||||
impl Tmp {
|
||||
/// An empty folder named after `test` and this process, made afresh.
|
||||
fn new(test: &str) -> Tmp {
|
||||
let dir = std::env::temp_dir().join(format!("wordcraft-appimage-it-{test}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
Tmp(dir)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Tmp {
|
||||
/// Removes the folder and what the test left in it.
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs the real app with `args`, as an AppImage app with `appimage` mounted at `appdir` would
|
||||
/// start it, and waits up to 30 s for it to exit (with no display it can't open a window).
|
||||
fn app(args: &[OsString], appimage: &Path, appdir: &Path) -> Output {
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_wordcraft"))
|
||||
.args(args)
|
||||
.env("APPIMAGE", appimage)
|
||||
.env("APPDIR", appdir)
|
||||
.env("ARGV0", "other-app")
|
||||
.env("OWD", "/")
|
||||
// With no display, an app that tries to open a window fails at once instead.
|
||||
.env_remove("DISPLAY")
|
||||
.env_remove("WAYLAND_DISPLAY")
|
||||
.env_remove("WAYLAND_SOCKET")
|
||||
.env_remove("WORDCRAFT_CONTROL_PORT")
|
||||
.env("WORDCRAFT_NO_PREFS", "1")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
// A start-over that kept the variables would start over for ever, and end here.
|
||||
let deadline = Instant::now() + Duration::from_secs(30);
|
||||
while child.try_wait().unwrap().is_none() {
|
||||
if Instant::now() > deadline {
|
||||
let _ = child.kill();
|
||||
panic!("wordcraft {args:?} was still running after 30 s");
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
child.wait_with_output().unwrap()
|
||||
}
|
||||
|
||||
/// The notice comes once: the program it started over as no longer sees the variables.
|
||||
#[test]
|
||||
fn another_programs_variables_are_dropped_once_and_the_program_carries_on() {
|
||||
let tmp = Tmp::new("dropped");
|
||||
let other = tmp.0.join("other-app.AppImage");
|
||||
std::fs::write(&other, b"another program").unwrap();
|
||||
let out = app(&["--version".into()], &other, &tmp.0);
|
||||
let (stdout, stderr) = (String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
|
||||
assert!(out.status.success(), "status {:?}, stderr: {stderr}", out.status);
|
||||
assert!(stdout.starts_with("wordcraft "), "stdout: {stdout}");
|
||||
assert_eq!(stderr.matches(NOTICE).count(), 1, "stderr: {stderr}");
|
||||
assert!(stderr.contains("other-app.AppImage"), "stderr: {stderr}");
|
||||
assert_eq!(std::fs::read(&other).unwrap(), b"another program");
|
||||
}
|
||||
|
||||
/// Arguments reach the program it starts over as byte for byte, even one that isn't Unicode.
|
||||
#[test]
|
||||
fn starting_over_keeps_the_arguments() {
|
||||
let tmp = Tmp::new("args");
|
||||
let out = app(&["--sample".into(), "--control".into(), OsString::from_vec(b"79\xe981".to_vec())], &tmp.0.join("gone"), &tmp.0);
|
||||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||||
assert_eq!(out.status.code(), Some(2), "stderr: {stderr}");
|
||||
assert!(stderr.contains("argument 3 is not valid Unicode: 79\u{FFFD}81"), "stderr: {stderr}");
|
||||
}
|
||||
|
||||
/// Its own image's variables stay, or the self-update couldn't find the file to replace.
|
||||
#[test]
|
||||
fn its_own_images_variables_stay() {
|
||||
let exe = Path::new(env!("CARGO_BIN_EXE_wordcraft")).canonicalize().unwrap();
|
||||
let mount = exe.parent().unwrap();
|
||||
let out = app(&["--version".into()], &mount.join("WordCraft.AppImage"), mount);
|
||||
let (stdout, stderr) = (String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
|
||||
assert!(out.status.success(), "status {:?}, stderr: {stderr}", out.status);
|
||||
assert!(stdout.starts_with("wordcraft "), "stdout: {stdout}");
|
||||
assert!(!stderr.contains(NOTICE), "stderr: {stderr}");
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
#[cfg(target_os = "linux")]
|
||||
mod appimage_env;
|
||||
mod control_arg;
|
||||
mod help;
|
||||
#[cfg(unix)]
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
# To remove your names, delete your entry (or set fields empty) in a PR. The next
|
||||
# contributors.py run, and the next build, drop them.
|
||||
|
||||
[people.SenuraMalaka]
|
||||
real_name = "Senura Dissanayake"
|
||||
sync_github_name = true
|
||||
|
||||
[people.divyanshu2074]
|
||||
real_name = "Divyanshu Gupta"
|
||||
sync_github_name = true
|
||||
@@ -197,3 +201,9 @@ display_name = "entropybeater"
|
||||
|
||||
[people.helenkwok]
|
||||
real_name = "Helen Kwok"
|
||||
|
||||
[people.ashokram-si]
|
||||
real_name = "Ashok Ram"
|
||||
|
||||
[people.benandez]
|
||||
sync_github_name = true
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
9d6d54d6d9ff603cc41e12e5f6ba51af3554ab66
|
||||
e56d26244bbac099bfd42e23fb12f78a4be418a7
|
||||
|
||||
+19
-10
@@ -7,8 +7,9 @@
|
||||
/// makes it allocate gigabytes. Here the DIFAT is walked the way `cfb` walks it (header entries,
|
||||
/// then the DIFAT chain, at most one step per sector; trailing zero and free entries trimmed
|
||||
/// the same way) and the file is refused when it lists one FAT sector twice, or more FAT sectors
|
||||
/// than its sectors need (one FAT sector covers `sector_len / 4` of them, plus one spare), so the
|
||||
/// FAT `cfb` builds is about the size the file needs. Anything else that is wrong is left for
|
||||
/// than the file has sectors — every FAT sector is a sector of the file, so a longer list is
|
||||
/// duplicates or names `cfb` refuses anyway, and the FAT `cfb` builds (one sector of entries per
|
||||
/// listed name) stays within about the file's own size. Anything else that is wrong is left for
|
||||
/// `cfb` to report.
|
||||
pub fn check(bytes: &[u8]) -> Result<(), String> {
|
||||
const MAGIC: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
|
||||
@@ -52,12 +53,14 @@ pub fn check(bytes: &[u8]) -> Result<(), String> {
|
||||
while difat.last() == Some(&FREE) {
|
||||
difat.pop();
|
||||
}
|
||||
// Each listed FAT sector adds a whole sector of entries. A FAT sector holds the entries of
|
||||
// `sector_len / 4` sectors, so a file needs no more FAT sectors than that covers (one spare),
|
||||
// and none of them twice. A free entry in the middle is left for `cfb` to refuse.
|
||||
// Each listed FAT sector adds a whole sector of entries. A real file lists no more FAT
|
||||
// sectors than the file has — every FAT sector is a sector of the file, and none of them
|
||||
// twice (checked below) — so the FAT `cfb` builds stays within about the file's own size.
|
||||
// The #3642 blowup listed 2048 × 127 names in a ~1 MB file. A tighter bound (one FAT sector
|
||||
// per `sector_len / 4` sectors plus a spare) refused real files, whose writers reserve FAT
|
||||
// for growth: an encrypted package of 78 sectors lists 4 and didn't open (#5109).
|
||||
let listed: Vec<u32> = difat.into_iter().filter(|&s| s != FREE).collect();
|
||||
let needed = num_sectors.div_ceil(sector_len / 4).saturating_add(1);
|
||||
if listed.len() > needed {
|
||||
if listed.len() > num_sectors {
|
||||
return Err(format!("the DIFAT lists {} FAT sectors in a file of {num_sectors} sectors", listed.len()));
|
||||
}
|
||||
let mut seen = std::collections::HashSet::with_capacity(listed.len());
|
||||
@@ -123,10 +126,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fat_sector_listed_twice_or_more_than_the_file_needs_is_refused() {
|
||||
// Each list is shorter than the file has sectors, which the guard used to accept.
|
||||
fn a_fat_sector_listed_twice_or_more_than_the_file_has_is_refused() {
|
||||
// A duplicate, and a list longer than the file has sectors: every FAT sector is a
|
||||
// sector of the file, so a longer list is one of the two.
|
||||
assert!(super::check(&header_difat(10, &[0, 0])).is_err_and(|e| e.contains("FAT sector 0 twice")));
|
||||
assert!(super::check(&header_difat(10, &[0, 1, 2])).is_err_and(|e| e.contains("3 FAT sectors")));
|
||||
assert!(
|
||||
super::check(&header_difat(10, &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).is_err_and(|e| e.contains("11 FAT sectors in a file of 10 sectors"))
|
||||
);
|
||||
// A writer may reserve FAT for growth: a real encrypted package of 78 sectors lists
|
||||
// 4 FAT sectors and didn't open since the #3642 guard landed (#5109).
|
||||
assert!(super::check(&header_difat(78, &[0, 1, 2, 3])).is_ok());
|
||||
assert!(super::check(&header_difat(10, &[0])).is_ok());
|
||||
assert!(super::check(&header_difat(200, &[0, 1])).is_ok());
|
||||
}
|
||||
|
||||
@@ -77,6 +77,8 @@ impl Document {
|
||||
glossary: Vec::new(),
|
||||
media_index: MediaIndex::default(),
|
||||
kept: Default::default(),
|
||||
missing_pictures: Default::default(),
|
||||
fit_state: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +156,8 @@ impl Document {
|
||||
glossary,
|
||||
media_index: _,
|
||||
kept: _,
|
||||
missing_pictures: _,
|
||||
fit_state: _,
|
||||
} = self;
|
||||
let Document {
|
||||
body: other_body,
|
||||
@@ -174,6 +178,8 @@ impl Document {
|
||||
glossary: other_glossary,
|
||||
media_index: _,
|
||||
kept: _,
|
||||
missing_pictures: _,
|
||||
fit_state: _,
|
||||
} = other;
|
||||
same_blocks(body, other_body)
|
||||
&& same_parts(parts, other_parts)
|
||||
|
||||
+130
-8
@@ -20,6 +20,9 @@ pub struct Fragment {
|
||||
/// gives each box a new story made from these.
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
pub parts: BTreeMap<u32, Part>,
|
||||
/// Media items referenced by objects in this fragment.
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty", with = "crate::byte_store")]
|
||||
pub media: BTreeMap<String, Arc<Vec<u8>>>,
|
||||
}
|
||||
|
||||
impl Fragment {
|
||||
@@ -30,6 +33,8 @@ impl Fragment {
|
||||
pub fn to_document(&self) -> Document {
|
||||
let mut d = Document::new();
|
||||
d.body = self.blocks.iter().cloned().map(Arc::new).collect();
|
||||
d.parts = self.parts.clone();
|
||||
d.media = self.media.clone();
|
||||
d
|
||||
}
|
||||
/// Whether the fragment holds an equation (in tables too), which a copy also puts on the
|
||||
@@ -560,6 +565,44 @@ fn box_ids(p: &Paragraph) -> impl Iterator<Item = u32> + '_ {
|
||||
p.objects.iter().flat_map(InlineObject::text_boxes)
|
||||
}
|
||||
|
||||
/// Remap media references in an inline object.
|
||||
fn remap_media_object(o: &mut InlineObject, map: &BTreeMap<String, String>, depth: usize) {
|
||||
match o {
|
||||
InlineObject::Image { media, .. } => {
|
||||
if let Some(k) = map.get(media.as_str()) {
|
||||
*media = k.clone();
|
||||
}
|
||||
}
|
||||
InlineObject::Graphic { graphic, .. } => {
|
||||
let mapped = |m: &str| map.get(m).filter(|k| k.as_str() != m);
|
||||
if graphic.items.iter().any(|i| matches!(i, crate::graphic::GraphicItem::Image { media, .. } if mapped(media).is_some())) {
|
||||
for i in &mut Arc::make_mut(graphic).items {
|
||||
if let crate::graphic::GraphicItem::Image { media, .. } = i
|
||||
&& let Some(k) = map.get(media.as_str())
|
||||
{
|
||||
*media = k.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
InlineObject::Group { children, .. } if depth < MAX_BOX_DEPTH => {
|
||||
for child in children {
|
||||
remap_media_object(&mut child.obj, map, depth + 1);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remap media references in a block.
|
||||
fn remap_media_block(b: &mut Block, map: &BTreeMap<String, String>) {
|
||||
each_para_mut(b, 0, &mut |p| {
|
||||
for o in &mut p.objects {
|
||||
remap_media_object(o, map, 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
impl Document {
|
||||
/// Convert notes between footnotes and endnotes: footnotes (`foot`) become endnotes and
|
||||
/// endnotes (`end`) footnotes, both at once to swap them. Each note's kind changes, as do its
|
||||
@@ -772,6 +815,10 @@ impl Document {
|
||||
if a.path.parent() == b.path.parent() {
|
||||
// Same container: trim the ends, drop the blocks between, join.
|
||||
let (ia, ib) = (a.path.last(), b.path.last());
|
||||
// Removing an already-empty paragraph must not impose its style or mark formatting
|
||||
// on the following content. Test before trimming: deleting actual heading text is
|
||||
// still an ordinary merge that keeps the first paragraph's properties.
|
||||
let empty_start = self.para(a.story, &a.path).is_some_and(Paragraph::is_empty);
|
||||
// Kept markers of the paragraphs in between move to the join.
|
||||
let mut moved: Vec<(InlineObject, CharProps)> = Vec::new();
|
||||
// `kept` is sorted: one walk of each paragraph's objects and runs (#3614).
|
||||
@@ -819,15 +866,18 @@ impl Document {
|
||||
if ib < bl.len() && ia < ib {
|
||||
bl.drain(ia + 1..=ib);
|
||||
}
|
||||
tail.props = ParaProps::default();
|
||||
// The deleted marks include `a`'s, so a section break there goes too: the joined
|
||||
// paragraph ends where `b`'s mark did, and its text takes the following section's
|
||||
// setup, as in Word (#2127).
|
||||
let sect = tail.section.take();
|
||||
let pa = self.para_mut(a.story, &a.path)?;
|
||||
let keep_props = pa.props.clone();
|
||||
let keep_props = if empty_start { tail.props.clone() } else { pa.props.clone() };
|
||||
let keep_mark = empty_start.then(|| tail.mark.clone());
|
||||
pa.append(tail);
|
||||
pa.props = keep_props;
|
||||
if let Some(mark) = keep_mark {
|
||||
pa.mark = mark;
|
||||
}
|
||||
let before = std::mem::replace(&mut pa.section, sect);
|
||||
if a.story == crate::StoryRef::Body && a.path.0.len() == 1 {
|
||||
// Nearest first, as "link to previous" resolved them before the delete.
|
||||
@@ -967,7 +1017,8 @@ impl Document {
|
||||
// Half a content control (one marker without the other) isn't copied.
|
||||
crate::control::balance_blocks(&mut blocks);
|
||||
let parts = self.text_box_parts(&blocks);
|
||||
Fragment { blocks, parts }
|
||||
let media = self.media_of_blocks(&blocks);
|
||||
Fragment { blocks, parts, media }
|
||||
}
|
||||
|
||||
/// A copy of the block at `path` (a whole table, say) as a fragment, with its text boxes.
|
||||
@@ -989,7 +1040,8 @@ impl Document {
|
||||
fn fragment_of(&self, mut blocks: Vec<Block>) -> Fragment {
|
||||
crate::control::balance_blocks(&mut blocks);
|
||||
let parts = self.text_box_parts(&blocks);
|
||||
Fragment { blocks, parts }
|
||||
let media = self.media_of_blocks(&blocks);
|
||||
Fragment { blocks, parts, media }
|
||||
}
|
||||
|
||||
/// The table whose whole text `a..b` covers when the range runs across its cells: from the
|
||||
@@ -1031,7 +1083,8 @@ impl Document {
|
||||
pub fn body_fragment(&self) -> Fragment {
|
||||
let blocks: Vec<Block> = self.body.iter().map(|b| (**b).clone()).collect();
|
||||
let parts = self.text_box_parts(&blocks);
|
||||
Fragment { blocks, parts }
|
||||
let media = self.media_of_blocks(&blocks);
|
||||
Fragment { blocks, parts, media }
|
||||
}
|
||||
|
||||
/// Another document's body as a fragment of this one, to insert: the media, lists, styles,
|
||||
@@ -1200,7 +1253,7 @@ impl Document {
|
||||
}
|
||||
let mut blocks: Vec<Block> = src.body.iter().map(|b| (**b).clone()).collect();
|
||||
blocks.iter_mut().for_each(|b| link.block(b));
|
||||
Fragment { blocks, parts }
|
||||
Fragment { blocks, parts, media: self.media.clone() }
|
||||
}
|
||||
|
||||
/// [`Self::adopt_with`] as blocks to place in a story directly (a reader building one): its
|
||||
@@ -1208,7 +1261,7 @@ impl Document {
|
||||
/// id the reader hasn't given out (ranges it holds aren't in this document yet); it moves past
|
||||
/// the adopted ones.
|
||||
pub fn adopt_blocks(&mut self, src: Document, keep_styles: bool, perms: &mut u32) -> Vec<Block> {
|
||||
let Fragment { mut blocks, parts } = self.adopt_in(src, keep_styles, perms);
|
||||
let Fragment { mut blocks, parts, .. } = self.adopt_in(src, keep_styles, perms);
|
||||
let mut budget = BoxBudget::new();
|
||||
self.adopt_text_boxes(&mut blocks, &parts, 0, &mut budget);
|
||||
blocks
|
||||
@@ -1234,6 +1287,52 @@ impl Document {
|
||||
out
|
||||
}
|
||||
|
||||
/// Media items referenced by objects in `blocks`, nested text box stories included.
|
||||
pub fn media_of_blocks(&self, blocks: &[Block]) -> BTreeMap<String, Arc<Vec<u8>>> {
|
||||
let mut keys = std::collections::BTreeSet::new();
|
||||
/// Collect media keys from an inline object recursively.
|
||||
fn collect_object(o: &InlineObject, keys: &mut std::collections::BTreeSet<String>, depth: usize) {
|
||||
match o {
|
||||
InlineObject::Image { media, .. } => {
|
||||
keys.insert(media.clone());
|
||||
}
|
||||
InlineObject::Graphic { graphic, .. } => {
|
||||
for item in &graphic.items {
|
||||
if let crate::graphic::GraphicItem::Image { media, .. } = item {
|
||||
keys.insert(media.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
InlineObject::Group { children, .. } if depth < MAX_BOX_DEPTH => {
|
||||
for child in children {
|
||||
collect_object(&child.obj, keys, depth + 1);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut collect_para = |p: &Paragraph| {
|
||||
for o in &p.objects {
|
||||
collect_object(o, &mut keys, 0);
|
||||
}
|
||||
};
|
||||
for b in blocks {
|
||||
each_para(b, 0, &mut collect_para);
|
||||
}
|
||||
for part in self.text_box_parts(blocks).values() {
|
||||
for b in &part.blocks {
|
||||
each_para(b, 0, &mut collect_para);
|
||||
}
|
||||
}
|
||||
let mut out = BTreeMap::new();
|
||||
for k in keys {
|
||||
if let Some(data) = self.media.get(&k) {
|
||||
out.insert(k, data.clone());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn copy_blocks(&self, a: &Pos, b: &Pos) -> Vec<Block> {
|
||||
let (a, b) = order(a, b);
|
||||
let mut out = Vec::new();
|
||||
@@ -1382,6 +1481,18 @@ impl Document {
|
||||
|
||||
/// Insert a fragment at `pos`; returns the position after the pasted content.
|
||||
pub fn insert_fragment(&mut self, pos: &Pos, frag: &Fragment) -> Result<Pos> {
|
||||
let mut media_remap = BTreeMap::new();
|
||||
for (k, v) in &frag.media {
|
||||
if let Some(existing) = self.media.get(k) {
|
||||
if existing.as_slice() != v.as_slice() {
|
||||
let ext = k.rsplit_once('.').map_or("png", |(_, ext)| ext);
|
||||
let new_key = self.add_media((**v).clone(), ext);
|
||||
media_remap.insert(k.clone(), new_key);
|
||||
}
|
||||
} else {
|
||||
self.media.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
// Tables that would nest deeper than the limit here become their paragraphs (#1596).
|
||||
let room = MAX_TABLE_DEPTH.saturating_sub(pos.path.depth());
|
||||
let mut blocks = Vec::with_capacity(frag.blocks.len());
|
||||
@@ -1394,9 +1505,20 @@ impl Document {
|
||||
if self.para_at(pos).is_none() {
|
||||
return Err(DocError::BadPath(pos.path.to_string()));
|
||||
}
|
||||
let mut parts = frag.parts.clone();
|
||||
if !media_remap.is_empty() {
|
||||
for b in &mut blocks {
|
||||
remap_media_block(b, &media_remap);
|
||||
}
|
||||
for part in parts.values_mut() {
|
||||
for b in &mut part.blocks {
|
||||
remap_media_block(Arc::make_mut(b), &media_remap);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Pasted text boxes get stories of their own (never shared with the copy's source).
|
||||
let mut budget = BoxBudget::new();
|
||||
self.adopt_text_boxes(&mut blocks, &frag.parts, 0, &mut budget);
|
||||
self.adopt_text_boxes(&mut blocks, &parts, 0, &mut budget);
|
||||
if budget.skipped > 0 {
|
||||
// Once per paste, not once per box (review of #3874).
|
||||
log::warn!(
|
||||
|
||||
@@ -326,7 +326,7 @@ fn fan_out(story: Vec<Arc<Block>>, refs: usize) -> Fragment {
|
||||
}
|
||||
let mut parts = BTreeMap::new();
|
||||
parts.insert(1, crate::Part { kind: PartKind::TextBox, blocks: story, body: Default::default() });
|
||||
Fragment { blocks: vec![Block::Para(p)], parts }
|
||||
Fragment { blocks: vec![Block::Para(p)], parts, ..Default::default() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -911,3 +911,80 @@ fn an_adopted_notes_text_box_gets_a_story_of_its_own() {
|
||||
assert_eq!(part.kind, PartKind::TextBox);
|
||||
assert!(matches!(part.blocks.first().map(|b| &**b), Some(Block::Para(p)) if p.text == "their box"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn media_of_blocks_collects_graphic_and_group_images() {
|
||||
let mut d = doc(&["test"]);
|
||||
d.media.insert("graphic_img.png".into(), Arc::new(vec![1, 2, 3]));
|
||||
d.media.insert("group_img.png".into(), Arc::new(vec![4, 5, 6]));
|
||||
|
||||
let graphic = crate::graphic::Graphic {
|
||||
kind: crate::graphic::GraphicKind::Diagram,
|
||||
items: vec![crate::graphic::GraphicItem::Image { rect: [0.0, 0.0, 10.0, 10.0], media: "graphic_img.png".into() }],
|
||||
..Default::default()
|
||||
};
|
||||
let group = InlineObject::Group {
|
||||
w: 50.0,
|
||||
h: 50.0,
|
||||
float: Default::default(),
|
||||
ch_w: 50.0,
|
||||
ch_h: 50.0,
|
||||
canvas: None,
|
||||
children: vec![crate::para::GroupChild {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
obj: InlineObject::Image {
|
||||
media: "group_img.png".into(),
|
||||
w: 10.0,
|
||||
h: 10.0,
|
||||
alt: String::new(),
|
||||
float: Default::default(),
|
||||
crop: [0.0; 4],
|
||||
fx: Default::default(),
|
||||
ole: None,
|
||||
},
|
||||
}],
|
||||
};
|
||||
let mut p = Paragraph::with_text(&crate::para::OBJ.to_string(), CharProps::default());
|
||||
p.objects.push(InlineObject::Graphic { w: 100.0, h: 100.0, float: Default::default(), alt: String::new(), graphic: Arc::new(graphic) });
|
||||
p.objects.push(group);
|
||||
let blocks = vec![Block::Para(p)];
|
||||
let collected = d.media_of_blocks(&blocks);
|
||||
assert!(collected.contains_key("graphic_img.png"));
|
||||
assert!(collected.contains_key("group_img.png"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_fragment_remaps_conflicting_media_keys() {
|
||||
let mut d = doc(&["destination"]);
|
||||
d.media.insert("image1.png".into(), Arc::new(vec![1, 1, 1]));
|
||||
|
||||
let mut frag_media = std::collections::BTreeMap::new();
|
||||
frag_media.insert("image1.png".into(), Arc::new(vec![2, 2, 2]));
|
||||
|
||||
let mut p = Paragraph::with_text(&crate::para::OBJ.to_string(), CharProps::default());
|
||||
p.objects.push(InlineObject::Image {
|
||||
media: "image1.png".into(),
|
||||
w: 10.0,
|
||||
h: 10.0,
|
||||
alt: String::new(),
|
||||
float: Default::default(),
|
||||
crop: [0.0; 4],
|
||||
fx: Default::default(),
|
||||
ole: None,
|
||||
});
|
||||
let frag = Fragment { blocks: vec![Block::Para(p)], parts: Default::default(), media: frag_media };
|
||||
|
||||
let pos = Pos::body(0, 0);
|
||||
d.insert_fragment(&pos, &frag).unwrap();
|
||||
// The original destination media is preserved.
|
||||
assert_eq!(d.media["image1.png"].as_slice(), &[1, 1, 1]);
|
||||
// A second media key was created for the conflicting fragment media.
|
||||
assert_eq!(d.media.len(), 2);
|
||||
let other_key = d.media.keys().find(|k| k.as_str() != "image1.png").unwrap();
|
||||
assert_eq!(d.media[other_key].as_slice(), &[2, 2, 2]);
|
||||
// The inserted paragraph references the remapped key.
|
||||
let para = d.para(StoryRef::Body, &Path::top(0)).unwrap();
|
||||
let remapped = para.objects.iter().find_map(|o| if let InlineObject::Image { media, .. } = o { Some(media.as_str()) } else { None }).unwrap();
|
||||
assert_eq!(remapped, other_key);
|
||||
}
|
||||
|
||||
@@ -478,8 +478,9 @@ fn family_fingerprint(doc: &crate::Document, root: u32, replies: &HashMap<u32, V
|
||||
}
|
||||
|
||||
/// What `doc`'s page setup is: each section's setup (where it starts, its size, margins,
|
||||
/// columns, headers and footers), the headers' and footers' content, the page colour and
|
||||
/// picture, and the notes' numbering.
|
||||
/// columns, headers and footers), the headers' and footers' content, whether odd and even pages
|
||||
/// differ, the page colour and picture, the watermark (one with no text isn't written), and the
|
||||
/// notes' numbering.
|
||||
pub fn pages_fingerprint(doc: &crate::Document) -> u64 {
|
||||
let mut sections: Vec<&crate::SectionProps> = doc.body.iter().filter_map(|b| b.as_para()).filter_map(|p| p.section.as_deref()).collect();
|
||||
sections.push(&doc.last_section);
|
||||
@@ -490,8 +491,10 @@ pub fn pages_fingerprint(doc: &crate::Document) -> u64 {
|
||||
match serde_json::to_vec(&(
|
||||
sections,
|
||||
parts,
|
||||
s.even_odd_headers,
|
||||
&s.page_color,
|
||||
&s.page_picture,
|
||||
s.watermark.as_ref().filter(|w| !w.text.trim().is_empty()),
|
||||
(&s.footnote_format, &s.endnote_format, &s.footnote_pr, &s.endnote_pr),
|
||||
(&doc.footnote_separators, &doc.endnote_separators),
|
||||
)) {
|
||||
|
||||
@@ -44,6 +44,7 @@ pub mod picfx;
|
||||
pub mod props;
|
||||
pub mod resolve;
|
||||
pub mod section;
|
||||
pub mod shrink;
|
||||
pub mod smart_art;
|
||||
pub mod styles;
|
||||
pub mod symbol_fonts;
|
||||
@@ -64,6 +65,7 @@ pub use numbering::{ListKind, Numbering};
|
||||
pub use para::{InlineObject, Paragraph, Run};
|
||||
pub use props::{Align, CharProps, ParaProps, Rgb, TextColor};
|
||||
pub use section::SectionProps;
|
||||
pub use shrink::FitState;
|
||||
pub use styles::{Style, StyleKind, StyleSheet};
|
||||
pub use table::{Cell, Row, Table};
|
||||
|
||||
@@ -75,6 +77,7 @@ mod block;
|
||||
mod compare;
|
||||
mod document;
|
||||
mod error;
|
||||
mod missing;
|
||||
mod parts;
|
||||
mod position;
|
||||
mod settings;
|
||||
@@ -84,6 +87,7 @@ pub use block::{Block, Blocks, BoxBudget, MAX_TEXT_BOX_DEPTH};
|
||||
pub use compare::para_block;
|
||||
use document::MediaIndex;
|
||||
pub use error::{DocError, Result};
|
||||
pub use missing::{MAX_MISSING_PICTURES, MissingPicture, MissingPictureReason, MissingPictures, picture_hash};
|
||||
pub use parts::{MAX_LINKED_BOXES, MAX_TEXT_BOX_INSET, NoteSeparators, Part, PartKind, PermRange, TEXT_BOX_INSET, TextBody, free_id};
|
||||
pub use position::{Path, Pos, StoryRef};
|
||||
pub use settings::{
|
||||
@@ -138,6 +142,13 @@ pub struct Document {
|
||||
/// [`kept`]); never saved, ignored by `==`.
|
||||
#[serde(skip)]
|
||||
pub kept: kept::Kept,
|
||||
/// Pictures a reader dropped because their target is missing, external or link-only (strict
|
||||
/// export reports them); never saved, never drawn, ignored by `==`.
|
||||
#[serde(skip)]
|
||||
pub missing_pictures: MissingPictures,
|
||||
/// Cumulative fit-to-page typography reduction state; internal tracking outside user-visible custom properties.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub fit_state: Option<shrink::FitState>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -19,6 +19,12 @@ pub fn is_web_or_mail_link(url: &str) -> bool {
|
||||
/// names another machine — following it on Windows leaks the user's NTLM credentials — so it is
|
||||
/// refused too.
|
||||
pub fn is_safe_href(url: &str) -> bool {
|
||||
// Exporters drop other control characters and noncharacters when they write the link
|
||||
// (`file\u{85}:` comes out `file:`), so a link holding one is refused, not judged as it
|
||||
// stands and written as something else.
|
||||
if url.chars().any(|c| (c.is_control() && !c.is_ascii_control()) || matches!(c, '\u{FFFE}' | '\u{FFFF}')) {
|
||||
return false;
|
||||
}
|
||||
let cleaned: String = url.chars().filter(|c| !c.is_ascii_control() && *c != ' ').collect();
|
||||
// `//host` (protocol-relative) and `\\host` (UNC), with `\` read as `/` the way Windows does.
|
||||
if cleaned.replace('\\', "/").starts_with("//") {
|
||||
@@ -125,6 +131,7 @@ mod tests {
|
||||
("HYPERLINK\t\"http://example.com\"", Some("http://example.com")),
|
||||
(r#"HYPERLINK"http://example.com""#, Some("http://example.com")),
|
||||
(r#"HYPERLINK "http://example.com/path\latest""#, Some("http://example.com/pathlatest")),
|
||||
(r#"HYPERLINK "x \* Upper""#, Some("x * Upper")),
|
||||
(r#"HYPERLINK "C:\\Docs\\My file.docx""#, Some(r"C:\Docs\My file.docx")),
|
||||
(r#"HYPERLINK "say \"hi\"""#, Some(r#"say "hi""#)),
|
||||
(r#"HYPERLINK " http://example.com ""#, Some("http://example.com")),
|
||||
@@ -199,6 +206,10 @@ mod tests {
|
||||
"\\\\attacker.example\\share\\x",
|
||||
"/\\attacker.example\\x",
|
||||
" //attacker.example/x",
|
||||
// Characters the exporters drop on writing don't hide a scheme.
|
||||
"file\u{85}:///etc/passwd",
|
||||
"java\u{9F}script:alert(1)",
|
||||
"/\u{FFFE}/attacker.example/x",
|
||||
"",
|
||||
] {
|
||||
assert!(!is_safe_href(bad), "{bad:?}");
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
//! Pictures a reader could not bring in or an exporter could not draw, for strict export (#6005).
|
||||
//! Kept beside the document while it is open; never saved, never drawn, and not part of the
|
||||
//! document's content (`==` ignores it).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Most pictures remembered per document; past it [`MissingPictures::truncated`] is set.
|
||||
pub const MAX_MISSING_PICTURES: usize = 256;
|
||||
|
||||
/// Most characters of a picture's part or story name a record keeps (names come from the file).
|
||||
pub const MAX_PICTURE_NAME_CHARS: usize = 1024;
|
||||
|
||||
/// Why a picture is not in the document or not drawn.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum MissingPictureReason {
|
||||
/// The relationship names a part the file does not contain, or no relationship at all.
|
||||
MissingPart,
|
||||
/// The target is outside the file (`TargetMode="External"`); it is never fetched.
|
||||
ExternalTarget,
|
||||
/// The picture only links to a file (`r:link`) and holds no embedded copy.
|
||||
LinkOnly,
|
||||
/// The bytes are a picture format we know but they do not decode.
|
||||
Undecodable,
|
||||
/// The bytes are not a picture format we can draw.
|
||||
UnsupportedFormat,
|
||||
}
|
||||
|
||||
impl MissingPictureReason {
|
||||
/// The words used in reports (`"missing part"`, `"external target"`, `"link only"`,
|
||||
/// `"undecodable"`, `"unsupported format"`).
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::MissingPart => "missing part",
|
||||
Self::ExternalTarget => "external target",
|
||||
Self::LinkOnly => "link only",
|
||||
Self::Undecodable => "undecodable",
|
||||
Self::UnsupportedFormat => "unsupported format",
|
||||
}
|
||||
}
|
||||
|
||||
/// The reason a [`code`](Self::code) names.
|
||||
pub fn from_code(code: &str) -> Option<Self> {
|
||||
[Self::MissingPart, Self::ExternalTarget, Self::LinkOnly, Self::Undecodable, Self::UnsupportedFormat].into_iter().find(|r| r.code() == code)
|
||||
}
|
||||
|
||||
/// The stable snake_case code the JSON report carries (`"missing_part"`, `"external_target"`,
|
||||
/// `"link_only"`, `"undecodable"`, `"unsupported_format"`); never reworded.
|
||||
pub fn code(self) -> &'static str {
|
||||
match self {
|
||||
Self::MissingPart => "missing_part",
|
||||
Self::ExternalTarget => "external_target",
|
||||
Self::LinkOnly => "link_only",
|
||||
Self::Undecodable => "undecodable",
|
||||
Self::UnsupportedFormat => "unsupported_format",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One picture that is missing.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct MissingPicture {
|
||||
/// The target as written, or the part name it resolved to (a media key for a decoded one).
|
||||
pub part: String,
|
||||
/// The story part it is in (`word/document.xml`, `word/header2.xml`), as the source file
|
||||
/// names it; empty when not known.
|
||||
pub story: String,
|
||||
pub reason: MissingPictureReason,
|
||||
}
|
||||
|
||||
/// A capped list of [`MissingPicture`]s, with the source names a report needs to say where a
|
||||
/// picture is (a reader fills them; a document made or opened another way leaves them empty).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct MissingPictures {
|
||||
pub list: Vec<MissingPicture>,
|
||||
/// More than [`MAX_MISSING_PICTURES`] were found; the rest are not listed.
|
||||
pub truncated: bool,
|
||||
/// Pictures found, those past the cap included (saturating). Each distinct one counts once.
|
||||
pub total: u64,
|
||||
/// The main story's part name in the source file (`word/document.xml`).
|
||||
pub main: String,
|
||||
/// A header, footer, note or comment story's part id ([`crate::Document::parts`]) to the
|
||||
/// part it was read from (`word/header2.xml`). Ids freed and reused after opening may name
|
||||
/// the old part.
|
||||
pub part_names: std::collections::BTreeMap<u32, std::sync::Arc<str>>,
|
||||
/// A [`crate::Document::media`] key to the part its bytes were read from
|
||||
/// (`word/media/image2.bin`).
|
||||
pub media_parts: std::collections::BTreeMap<String, String>,
|
||||
/// What `list` holds, for an O(1) test of "already there" (a hostile file has many).
|
||||
seen: std::collections::HashSet<MissingPicture>,
|
||||
/// Hashes of the distinct pictures past the cap (8 bytes each), so the total counts them once.
|
||||
unlisted: std::collections::HashSet<u64>,
|
||||
/// The chunk parts whose unlisted pictures [`Self::add_unlisted`] has counted, so a chunk
|
||||
/// imported twice counts them once.
|
||||
counted_chunks: std::collections::HashSet<String>,
|
||||
}
|
||||
|
||||
impl MissingPictures {
|
||||
/// Add `n` pictures of the chunk part `chunk` that its reader counted but did not list. The
|
||||
/// same chunk part counts once however often it is imported (its listed pictures dedupe
|
||||
/// through [`Self::push`]).
|
||||
pub fn add_unlisted(&mut self, chunk: &str, n: u64) {
|
||||
if n > 0 && self.counted_chunks.insert(chunk.to_string()) {
|
||||
self.total = self.total.saturating_add(n);
|
||||
}
|
||||
}
|
||||
|
||||
/// Add one (the same part, story and reason once).
|
||||
pub fn push(&mut self, mut item: MissingPicture) {
|
||||
for name in [&mut item.part, &mut item.story] {
|
||||
if name.len() > MAX_PICTURE_NAME_CHARS {
|
||||
*name = name.chars().take(MAX_PICTURE_NAME_CHARS).collect();
|
||||
}
|
||||
}
|
||||
if self.seen.contains(&item) {
|
||||
return;
|
||||
}
|
||||
if self.list.len() >= MAX_MISSING_PICTURES {
|
||||
self.truncated = true;
|
||||
if self.unlisted.insert(picture_hash(&item)) {
|
||||
self.total = self.total.saturating_add(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.total = self.total.saturating_add(1);
|
||||
self.seen.insert(item.clone());
|
||||
self.list.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
/// A 64-bit hash of a record, to tell the ones past a cap apart without keeping them.
|
||||
pub fn picture_hash(item: &MissingPicture) -> u64 {
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
item.hash(&mut h);
|
||||
h.finish()
|
||||
}
|
||||
|
||||
impl PartialEq for MissingPictures {
|
||||
/// Not part of the document's content.
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -228,7 +228,7 @@ impl Numbering {
|
||||
/// Whether a paragraph in list `num` at `level` gets a label ([`Counters::next_label`] gives
|
||||
/// one), without counting it.
|
||||
pub fn shows_label(&self, num: u32, level: u8) -> bool {
|
||||
self.abstract_of(num).is_some() && self.level(num, level.min(8)).is_some_and(|d| d.format != NumFormat::None)
|
||||
self.abstract_of(num).is_some() && self.level(num, level.min(8)).is_some()
|
||||
}
|
||||
pub fn level(&self, num: u32, level: u8) -> Option<&Level> {
|
||||
if let Some((_, l)) = self.num(num)?.level_overrides.iter().find(|(i, _)| *i == level) {
|
||||
@@ -325,12 +325,9 @@ impl Counters {
|
||||
let lv = (level as usize).min(8);
|
||||
// This list's own definition of a level wins over its abstract list's.
|
||||
let level_def = |k: usize| numbering.level(num, k as u8);
|
||||
// A "none" level (Word's numFmt none) still counts and restarts the levels below it; only
|
||||
// its number reads as nothing, so its literal text ("RESPONSE:") shows (#4985).
|
||||
let def = level_def(lv)?.clone();
|
||||
// A "none" level shows no label at all (Word's numFmt none) — not even its text.
|
||||
if def.format == NumFormat::None {
|
||||
self.labels.insert((abs.id, lv as u8), CountedLabel { label: None, levels: Vec::new() });
|
||||
return None;
|
||||
}
|
||||
let st = self.state.entry(abs.id).or_insert([0; 9]);
|
||||
let fl = self.flags.entry(abs.id).or_default();
|
||||
// Only this level's override, not the num's others: the counters are shared by every
|
||||
@@ -382,7 +379,14 @@ impl Counters {
|
||||
let started = *fl;
|
||||
let numeric = def.format != NumFormat::Bullet;
|
||||
let label = if numeric { render_label(numbering, num, &def, &vals, &started) } else { def.text.clone() };
|
||||
self.labels.insert((abs.id, lv as u8), CountedLabel { label: numeric.then(|| label.clone()), levels: template_levels(&def.text) });
|
||||
// A "none" level shows its literal text, but has no number of its own for a reference to
|
||||
// show (#4985): it is kept as one that shows none.
|
||||
let counted = if def.format == NumFormat::None {
|
||||
CountedLabel { label: None, levels: Vec::new() }
|
||||
} else {
|
||||
CountedLabel { label: numeric.then(|| label.clone()), levels: template_levels(&def.text) }
|
||||
};
|
||||
self.labels.insert((abs.id, lv as u8), counted);
|
||||
Some((label, def))
|
||||
}
|
||||
/// Which levels of `num`'s list have counted a paragraph yet (a level never used has no
|
||||
@@ -455,9 +459,9 @@ fn render_label(numbering: &Numbering, num: u32, def: &Level, vals: &[u32; 9], s
|
||||
let counted = started.get(k).is_some_and(|f| f.started);
|
||||
let val = if counted { val } else { level_def(k).map(|l| l.start).unwrap_or(1) };
|
||||
// Legal numbering (w:isLgl) shows every level in Arabic digits, whatever its own
|
||||
// format (二 of a Chinese outline becomes 2 in "2.1").
|
||||
// format (二 of a Chinese outline becomes 2 in "2.1"); a "none" level stays empty.
|
||||
let fmt = match level_def(k).map(|l| l.format).unwrap_or(NumFormat::Decimal) {
|
||||
NumFormat::DecimalZero => NumFormat::DecimalZero,
|
||||
f @ (NumFormat::DecimalZero | NumFormat::None) => f,
|
||||
_ if def.legal => NumFormat::Decimal,
|
||||
f => f,
|
||||
};
|
||||
@@ -556,6 +560,27 @@ mod tests {
|
||||
assert_eq!(n.level(id, 2).map(|l| l.format), Some(NumFormat::DecimalZero));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_none_level_shows_its_text_and_still_counts() {
|
||||
// As Word shows them (#4985): a "none" level's literal text is its label, its own number
|
||||
// reads as nothing (also in legal numbering), and it restarts the levels below it.
|
||||
let mut n = Numbering::default();
|
||||
let id = n.add_list(ListKind::Numbered);
|
||||
let lvl = |format, text: &str| Level { format, text: text.into(), ..Level::default() };
|
||||
let legal = Level { legal: true, ..lvl(NumFormat::Decimal, "%2.%3") };
|
||||
if let Some(num) = n.nums.iter_mut().find(|x| x.id == id) {
|
||||
num.level_overrides = vec![(0, lvl(NumFormat::None, "RESPONSE:")), (1, lvl(NumFormat::None, "[%2]")), (2, legal)];
|
||||
}
|
||||
assert!(n.shows_label(id, 0));
|
||||
let mut c = Counters::default();
|
||||
assert_eq!(c.next_label(&n, id, 0).unwrap().0, "RESPONSE:");
|
||||
assert_eq!(c.next_label(&n, id, 1).unwrap().0, "[]");
|
||||
assert_eq!(c.next_label(&n, id, 2).unwrap().0, ".1");
|
||||
assert_eq!(c.next_label(&n, id, 2).unwrap().0, ".2");
|
||||
assert_eq!(c.next_label(&n, id, 1).unwrap().0, "[]");
|
||||
assert_eq!(c.next_label(&n, id, 2).unwrap().0, ".1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_start_override_restarts_only_its_own_level_of_a_shared_list() {
|
||||
// One abstract list numbers the headings through two nums: one for levels 1-4, and one
|
||||
|
||||
@@ -186,6 +186,16 @@ pub struct Float {
|
||||
/// top. 0 = unknown, drawn in document order (objects inserted here, or from other formats).
|
||||
#[serde(skip_serializing_if = "is_zero_u32")]
|
||||
pub z: u32,
|
||||
/// The page a drawing anchored to a page is on, from 1 (ODF's `text:anchor-page-number`,
|
||||
/// §19.872); 0 = anchored to its paragraph or not said. Kept for saving: the layout places a
|
||||
/// drawing on its anchor paragraph's page.
|
||||
#[serde(skip_serializing_if = "is_zero_u32")]
|
||||
pub page: u32,
|
||||
/// Turned, but the file keeps no room for the turn (`wp:effectExtent` missing or zero, as
|
||||
/// files from other tools have): Word then keeps an inline one's line to its frame and lets
|
||||
/// the turned object overhang it (see [`Float::line_pad`]). Cleared by [`Float::set_spin`].
|
||||
#[serde(skip_serializing_if = "is_false")]
|
||||
pub bare_turn: bool,
|
||||
}
|
||||
|
||||
/// Longest alternative text kept (characters): hostile files and commands are capped.
|
||||
@@ -241,6 +251,7 @@ impl Float {
|
||||
self.rot = wordcraft_geom::normalize_degrees(s.deg);
|
||||
self.flip_h = s.flip_h;
|
||||
self.flip_v = s.flip_v;
|
||||
self.bare_turn = false;
|
||||
}
|
||||
/// How far a `w` × `h` frame's rotated bounds reach past it on each side (x, y): the room a
|
||||
/// rotated object takes beyond its frame (zero unrotated; negative where the turned object
|
||||
@@ -250,6 +261,23 @@ impl Float {
|
||||
let (bw, bh) = self.spin().extent(w, h);
|
||||
((bw - w) / 2.0, (bh - h) / 2.0)
|
||||
}
|
||||
/// A [`Float::bare_turn`] still inline and without effects room: made floating, or given room
|
||||
/// for a shadow or glow since, it saves, and so lays out, with the turn's room again.
|
||||
pub fn is_bare_turn(&self) -> bool {
|
||||
self.bare_turn && self.wrap == Wrap::Inline && self.effect_extent() == [0.0; 4]
|
||||
}
|
||||
/// The room an inline `w` × `h` object's line keeps past its frame on each side (x, y) for its
|
||||
/// turn: its rotated bounds ([`Float::spin_pad`]) — or, for a [`Float::is_bare_turn`], none, except
|
||||
/// that Word 2013+ layout (`modern`) exchanges the frame's width and height from 45° up to 135°
|
||||
/// and from 225° up to 315°.
|
||||
pub fn line_pad(&self, w: f32, h: f32, modern: bool) -> (f32, f32) {
|
||||
if !self.is_bare_turn() {
|
||||
return self.spin_pad(w, h);
|
||||
}
|
||||
let (w, h) = (wordcraft_geom::finite(w).clamp(0.0, 4000.0), wordcraft_geom::finite(h).clamp(0.0, 4000.0));
|
||||
let deg = wordcraft_geom::normalize_degrees(self.rot);
|
||||
if modern && ((45.0..135.0).contains(°) || (225.0..315.0).contains(°)) { ((h - w) / 2.0, (w - h) / 2.0) } else { (0.0, 0.0) }
|
||||
}
|
||||
/// The effect extents, finite and clamped (left, top, right, bottom).
|
||||
pub fn effect_extent(&self) -> [f32; 4] {
|
||||
self.effect.map(|v| if v.is_finite() { v.clamp(0.0, 1584.0) } else { 0.0 })
|
||||
|
||||
@@ -316,20 +316,34 @@ impl InlineObject {
|
||||
/// A group's members placed in a `w` × `h` box at (`x`, `y`): each member's rectangle
|
||||
/// (x, y, w, h) and the member. Empty for anything else.
|
||||
pub fn group_rects(&self, x: f32, y: f32, w: f32, h: f32) -> Vec<([f32; 4], &InlineObject)> {
|
||||
let InlineObject::Group { ch_w, ch_h, children, .. } = self else { return Vec::new() };
|
||||
let InlineObject::Group { children, .. } = self else { return Vec::new() };
|
||||
self.member_rects(x, y, w, h).into_iter().filter_map(|(i, r)| Some((r, &children.get(i)?.obj))).collect()
|
||||
}
|
||||
/// [`InlineObject::group_rects`] with each member's index in `children` instead of the member.
|
||||
pub fn member_rects(&self, x: f32, y: f32, w: f32, h: f32) -> Vec<(usize, [f32; 4])> {
|
||||
let InlineObject::Group { children, .. } = self else { return Vec::new() };
|
||||
let (sx, sy) = self.group_scale(w, h);
|
||||
let fin = |v: f32| if v.is_finite() { v } else { 0.0 };
|
||||
let sx = if fin(*ch_w) > 0.0 { fin(w) / ch_w } else { 1.0 };
|
||||
let sy = if fin(*ch_h) > 0.0 { fin(h) / ch_h } else { 1.0 };
|
||||
children
|
||||
.iter()
|
||||
.take(MAX_GROUP_CHILDREN)
|
||||
.filter_map(|c| {
|
||||
.enumerate()
|
||||
.filter_map(|(i, c)| {
|
||||
let (cw, chh, _) = c.obj.frame()?;
|
||||
let r = [x + fin(c.x) * sx, y + fin(c.y) * sy, (fin(cw) * sx).clamp(0.0, 4000.0), (fin(chh) * sy).clamp(0.0, 4000.0)];
|
||||
(!matches!(c.obj, InlineObject::Group { .. })).then_some((r, &c.obj))
|
||||
(!matches!(c.obj, InlineObject::Group { .. })).then_some((i, r))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
/// How much a group drawn `w` × `h` scales its members' space (x, y): 1 for a canvas, whose
|
||||
/// members keep their size.
|
||||
pub fn group_scale(&self, w: f32, h: f32) -> (f32, f32) {
|
||||
let InlineObject::Group { ch_w, ch_h, .. } = self else { return (1.0, 1.0) };
|
||||
let fin = |v: f32| if v.is_finite() { v } else { 0.0 };
|
||||
let sx = if fin(*ch_w) > 0.0 { fin(w) / ch_w } else { 1.0 };
|
||||
let sy = if fin(*ch_h) > 0.0 { fin(h) / ch_h } else { 1.0 };
|
||||
(sx, sy)
|
||||
}
|
||||
/// A legacy form field's definition (`FORMCHECKBOX`, `FORMTEXT`, `FORMDROPDOWN`), read from
|
||||
/// the `w:ffData` it keeps; `None` for anything else.
|
||||
pub fn legacy_form(&self) -> Option<crate::legacy_form::LegacyForm> {
|
||||
|
||||
@@ -27,7 +27,8 @@ pub use char_props::{
|
||||
};
|
||||
pub use color::{Rgb, TextColor};
|
||||
pub use para_props::{
|
||||
Align, Border, BorderStyle, Borders, LineSpacing, NumRef, ParaProps, TAB_POS_EPS, TabAlign, TabLeader, TabStop, TextAlignment, TextFrame,
|
||||
Align, Border, BorderStyle, Borders, LineSpacing, NumRef, ParaProps, ParagraphTextDirection, TAB_POS_EPS, TabAlign, TabLeader, TabStop,
|
||||
TextAlignment, TextFrame,
|
||||
};
|
||||
pub use table_props::{
|
||||
CellPicture, CellProps, HeightRule, PictureRepeat, RowProps, TableFloat, TableLook, TableProps, TextDirection, TextVert, VAlign, VMerge,
|
||||
|
||||
@@ -26,6 +26,67 @@ impl Align {
|
||||
}
|
||||
}
|
||||
|
||||
/// Paragraph-level text flow (`w:pPr/w:textDirection`, ECMA-376 §17.3.1.41).
|
||||
///
|
||||
/// Kept separate from [`super::TextDirection`], whose three values model the table-cell direction
|
||||
/// control. These values preserve the exact OOXML direction through JSON and DOCX saves.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum ParagraphTextDirection {
|
||||
LrTb,
|
||||
LrTbV,
|
||||
Tb,
|
||||
TbRl,
|
||||
TbRlV,
|
||||
TbLr,
|
||||
TbLrV,
|
||||
BtLr,
|
||||
TbV,
|
||||
Rl,
|
||||
RlV,
|
||||
Lr,
|
||||
LrV,
|
||||
}
|
||||
|
||||
impl ParagraphTextDirection {
|
||||
pub fn from_ooxml(value: &str) -> Option<Self> {
|
||||
match value {
|
||||
"lrTb" => Some(Self::LrTb),
|
||||
"lrTbV" => Some(Self::LrTbV),
|
||||
"tb" => Some(Self::Tb),
|
||||
"tbRl" => Some(Self::TbRl),
|
||||
"tbRlV" => Some(Self::TbRlV),
|
||||
"tbLr" => Some(Self::TbLr),
|
||||
"tbLrV" => Some(Self::TbLrV),
|
||||
"btLr" => Some(Self::BtLr),
|
||||
"tbV" => Some(Self::TbV),
|
||||
"rl" => Some(Self::Rl),
|
||||
"rlV" => Some(Self::RlV),
|
||||
"lr" => Some(Self::Lr),
|
||||
"lrV" => Some(Self::LrV),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ooxml(self) -> &'static str {
|
||||
match self {
|
||||
Self::LrTb => "lrTb",
|
||||
Self::LrTbV => "lrTbV",
|
||||
Self::Tb => "tb",
|
||||
Self::TbRl => "tbRl",
|
||||
Self::TbRlV => "tbRlV",
|
||||
Self::TbLr => "tbLr",
|
||||
Self::TbLrV => "tbLrV",
|
||||
Self::BtLr => "btLr",
|
||||
Self::TbV => "tbV",
|
||||
Self::Rl => "rl",
|
||||
Self::RlV => "rlV",
|
||||
Self::Lr => "lr",
|
||||
Self::LrV => "lrV",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical alignment of characters of different sizes on a line (`w:textAlignment`,
|
||||
/// ECMA-376 §17.3.1.39).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
@@ -391,6 +452,9 @@ pub struct ParaProps {
|
||||
pub suppress_line_numbers: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bidi: Option<bool>,
|
||||
/// Paragraph text direction (`w:textDirection`); `None` inherits from the paragraph style.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub text_direction: Option<ParagraphTextDirection>,
|
||||
/// Drop cap: lines to drop (0 = none) — applies to the first character(s).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub drop_cap: Option<u8>,
|
||||
@@ -462,6 +526,7 @@ impl ParaProps {
|
||||
}
|
||||
|
||||
fn overlay_script_layout(&mut self, patch: &ParaProps) {
|
||||
overlay_fields!(self, patch; bidi, text_direction, kinsoku, word_wrap, overflow_punct, top_line_punct, auto_space_de, auto_space_dn);
|
||||
overlay_fields!(self, patch; bidi, kinsoku, word_wrap, overflow_punct, top_line_punct, auto_space_de, auto_space_dn, text_alignment);
|
||||
}
|
||||
pub fn overlaid(mut self, patch: &ParaProps) -> ParaProps {
|
||||
|
||||
@@ -137,9 +137,12 @@ impl ResolvedChar {
|
||||
pub fn uses_complex(&self, c: char) -> bool {
|
||||
self.rtl || self.cs || crate::bidi::is_complex_script(c)
|
||||
}
|
||||
/// The size glyphs are drawn at (super/subscript shrink to ~2/3).
|
||||
/// The size glyphs are drawn and measured at. Word sets superscript and subscript text at
|
||||
/// 0.65 of the run's size rounded to the half point, a font size the format can store
|
||||
/// (measured in Word for Mac from digit advances: 8 pt → 5, 9 → 6, 11 → 7, 12 → 8,
|
||||
/// 16 → 10.5, 24 → 15.5, 36 → 23.5).
|
||||
pub fn draw_size(&self) -> f32 {
|
||||
if self.vert_align == VertAlign::Baseline { self.size } else { self.size * 0.65 }
|
||||
if self.vert_align == VertAlign::Baseline { self.size } else { (self.size * 0.65 * 2.0).round() / 2.0 }
|
||||
}
|
||||
/// Baseline shift, points (positive = up).
|
||||
pub fn baseline_shift(&self) -> f32 {
|
||||
@@ -453,6 +456,32 @@ fn diff_patch<T: Serialize + serde::de::DeserializeOwned + Default>(with: &T, wi
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Superscript and subscript sizes as Word for Mac sets them (measured from digit advances in
|
||||
/// its PDF export): 0.65 of the size, rounded to the half point.
|
||||
#[test]
|
||||
fn raised_and_lowered_text_is_sized_to_the_half_point() {
|
||||
for (size, want) in [
|
||||
(8.0, 5.0),
|
||||
(9.0, 6.0),
|
||||
(10.0, 6.5),
|
||||
(11.0, 7.0),
|
||||
(12.0, 8.0),
|
||||
(13.0, 8.5),
|
||||
(14.0, 9.0),
|
||||
(16.0, 10.5),
|
||||
(20.0, 13.0),
|
||||
(24.0, 15.5),
|
||||
(36.0, 23.5),
|
||||
] {
|
||||
for vert_align in [VertAlign::Superscript, VertAlign::Subscript] {
|
||||
let rc = ResolvedChar { size, vert_align, ..StyleSheet::builtin().resolve_char(None, &CharProps::default()) };
|
||||
assert_eq!(rc.draw_size(), want, "{size} pt {vert_align:?}");
|
||||
}
|
||||
}
|
||||
let rc = StyleSheet::builtin().resolve_char(None, &CharProps { size: Some(11.0), ..Default::default() });
|
||||
assert_eq!(rc.draw_size(), 11.0, "baseline text keeps its size");
|
||||
}
|
||||
|
||||
/// #2036: only a character itself (direct formatting) has a symbol font of its own; a
|
||||
/// style never gives one to its text.
|
||||
#[test]
|
||||
|
||||
@@ -289,13 +289,14 @@ impl NumFormat {
|
||||
pub fn from_ooxml(s: &str) -> NumFormat {
|
||||
NumFormat::ALL.into_iter().find(|f| f.ooxml() == s).unwrap_or(NumFormat::Decimal)
|
||||
}
|
||||
/// Format `n` (1-based). Formats WordCraft doesn't draw yet (Hebrew, Hindi, Thai,
|
||||
/// Vietnamese, text amounts) show decimal, as do values past a sequence's end
|
||||
/// (the 21st item of ①…⑳).
|
||||
/// Format `n` (1-based). Formats WordCraft doesn't draw yet (Hebrew, Hindi, Thai letters
|
||||
/// and counting, Vietnamese, text amounts) show decimal, as do values past a sequence's end
|
||||
/// (the 21st item of ①…⑳). Thai numbers are the decimal number in Thai digits (๑, ๒ … ๑๐).
|
||||
pub fn format(self, n: u32) -> String {
|
||||
use crate::cjk_numbers as cjk;
|
||||
match self {
|
||||
NumFormat::DecimalZero => format!("{n:02}"),
|
||||
NumFormat::ThaiNumbers => thai_digits(n),
|
||||
NumFormat::UpperRoman => roman(n).to_uppercase(),
|
||||
NumFormat::LowerRoman => roman(n),
|
||||
NumFormat::UpperLetter => letters(n).to_uppercase(),
|
||||
@@ -349,7 +350,6 @@ impl NumFormat {
|
||||
| NumFormat::HindiNumbers
|
||||
| NumFormat::HindiCounting
|
||||
| NumFormat::ThaiLetters
|
||||
| NumFormat::ThaiNumbers
|
||||
| NumFormat::ThaiCounting
|
||||
| NumFormat::BahtText
|
||||
| NumFormat::DollarText => n.to_string(),
|
||||
@@ -357,6 +357,11 @@ impl NumFormat {
|
||||
}
|
||||
}
|
||||
|
||||
/// `n` in Thai digits, ๐ (U+0E50) to ๙ (#5757).
|
||||
fn thai_digits(n: u32) -> String {
|
||||
n.to_string().chars().filter_map(|c| c.to_digit(10).and_then(|d| char::from_u32(0x0E50 + d))).collect()
|
||||
}
|
||||
|
||||
/// Chicago Manual of Style note marks: *, †, ‡, §, then doubled (**, ††, …) and so on.
|
||||
fn chicago(n: u32) -> String {
|
||||
const MARKS: [char; 4] = ['*', '\u{2020}', '\u{2021}', '\u{a7}'];
|
||||
@@ -999,6 +1004,18 @@ mod tests {
|
||||
assert_eq!(NumFormat::from_ooxml("klingon"), NumFormat::Decimal, "unknown formats are decimal");
|
||||
}
|
||||
|
||||
/// #5757: Thai numbers are the decimal number in Thai digits, every digit, at any length.
|
||||
#[test]
|
||||
fn thai_numbers_use_thai_digits() {
|
||||
let labels: Vec<String> = [1, 2, 3, 9, 10, 11, 20, 105, 2569].iter().map(|n| NumFormat::ThaiNumbers.format(*n)).collect();
|
||||
assert_eq!(labels.join(" "), "๑ ๒ ๓ ๙ ๑๐ ๑๑ ๒๐ ๑๐๕ ๒๕๖๙");
|
||||
assert_eq!(NumFormat::ThaiNumbers.format(0), "๐");
|
||||
assert_eq!(NumFormat::ThaiNumbers.format(u32::MAX), "๔๒๙๔๙๖๗๒๙๕");
|
||||
assert_eq!(NumFormat::from_ooxml("thaiNumbers"), NumFormat::ThaiNumbers);
|
||||
// Formats not drawn yet still show decimal.
|
||||
assert_eq!(NumFormat::ThaiLetters.format(3), "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn russian_letters_follow_word() {
|
||||
// Word's own labels for a russianLower list: no ё, й, ъ or ь, then doubled letters.
|
||||
|
||||
@@ -0,0 +1,755 @@
|
||||
//! Fit One Page typography helper.
|
||||
//!
|
||||
//! Adjusts font sizes and paragraph spacing across styles and editable stories
|
||||
//! while preserving visual hierarchy, clamping to safe bounds, and excluding
|
||||
//! fixed framing stories like headers and footers.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::edit::{each_para, each_para_mut};
|
||||
use crate::para::Paragraph;
|
||||
use crate::parts::PartKind;
|
||||
use crate::props::{CharProps, LineSpacing, ParaProps};
|
||||
use crate::styles::StyleSheet;
|
||||
use crate::{Block, Document};
|
||||
|
||||
/// Minimum font size allowed in points (WordCraft floor).
|
||||
const MIN_SHRINK_FONT_SIZE: f32 = 4.0;
|
||||
|
||||
/// Maximum font size allowed in points.
|
||||
const MAX_SHRINK_FONT_SIZE: f32 = 1638.0;
|
||||
|
||||
/// Safe line spacing floor factor (preventing line collision / crowding).
|
||||
const MIN_LINE_SPACING_FACTOR: f32 = 0.94;
|
||||
|
||||
/// Cumulative fit-to-page typography reduction state.
|
||||
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FitState {
|
||||
/// Cumulative font reduction delta in points.
|
||||
pub cum_delta: f32,
|
||||
/// Cumulative line spacing scale factor.
|
||||
pub cum_space: f32,
|
||||
/// Unique effective body font sizes present immediately after the reduction was applied.
|
||||
pub reduced_font_sizes: Vec<f32>,
|
||||
/// Unique effective line spacing values present immediately after the reduction was applied.
|
||||
pub reduced_line_spacings: Vec<LineSpacing>,
|
||||
/// Unique effective paragraph spacing values present immediately after the reduction was applied.
|
||||
#[serde(default)]
|
||||
pub reduced_paragraph_spacings: Vec<f32>,
|
||||
/// Unique effective body font sizes before the first Fit One Page reduction was applied.
|
||||
#[serde(default)]
|
||||
pub original_font_sizes: Vec<f32>,
|
||||
/// Unique effective line spacing values before the first Fit One Page reduction was applied.
|
||||
#[serde(default)]
|
||||
pub original_line_spacings: Vec<LineSpacing>,
|
||||
/// Unique effective paragraph spacing values before the first Fit One Page reduction was applied.
|
||||
#[serde(default)]
|
||||
pub original_paragraph_spacings: Vec<f32>,
|
||||
/// Original effective body font sizes by formatting location key before the first fit.
|
||||
#[serde(default)]
|
||||
pub original_font_locations: std::collections::BTreeMap<String, f32>,
|
||||
/// Original effective line spacing values by formatting location key before the first fit.
|
||||
#[serde(default)]
|
||||
pub original_line_spacing_locations: std::collections::BTreeMap<String, LineSpacing>,
|
||||
/// Original effective paragraph spacing values by formatting location key before the first fit.
|
||||
#[serde(default)]
|
||||
pub original_paragraph_spacing_locations: std::collections::BTreeMap<String, f32>,
|
||||
}
|
||||
|
||||
/// Scale a font size down by `delta_pt`.
|
||||
#[inline]
|
||||
fn scale_size(size: f32, delta_pt: f32) -> f32 {
|
||||
if delta_pt == 0.0 {
|
||||
return size;
|
||||
}
|
||||
// Round up so the reduction never exceeds `delta_pt`, and never enlarge.
|
||||
let target = (size - delta_pt).clamp(MIN_SHRINK_FONT_SIZE, MAX_SHRINK_FONT_SIZE);
|
||||
((target * 2.0).ceil() / 2.0).min(size)
|
||||
}
|
||||
|
||||
/// Scale a spacing value (e.g. `space_before`, `space_after`) by `space_factor`.
|
||||
///
|
||||
/// Caps the result at the original nonnegative spacing value and rounds up to hundredths (with a
|
||||
/// tiny epsilon for floating-point noise), so a trial never removes more spacing than its factor
|
||||
/// permits and never increases spacing. Rounding down would over-compress very small values.
|
||||
#[inline]
|
||||
fn scale_spacing(val: f32, space_factor: f32) -> f32 {
|
||||
let non_neg = val.max(0.0);
|
||||
let scaled = (non_neg * space_factor).clamp(0.0, 1584.0);
|
||||
((scaled * 100.0 - 1e-4).ceil() / 100.0).min(non_neg)
|
||||
}
|
||||
|
||||
/// Scale fixed line spacing in points respecting the safe readability floor and rounding upward to half-points.
|
||||
///
|
||||
/// Rounds upward to half-points so the result never falls below the 0.94 floor, and caps at `pts` so
|
||||
/// reduction never increases fixed line spacing.
|
||||
#[inline]
|
||||
fn scale_fixed_line_spacing(pts: f32, space_factor: f32) -> f32 {
|
||||
let floor = pts * MIN_LINE_SPACING_FACTOR;
|
||||
let scaled = (pts * space_factor).max(floor).clamp(0.0, 1584.0);
|
||||
// Round upward to half-points (0.5 pt) so the resulting spacing never falls below the 0.94 floor.
|
||||
((scaled * 2.0).ceil() / 2.0).min(pts)
|
||||
}
|
||||
|
||||
/// Scale line spacing by `space_factor` respecting the safe readability floor.
|
||||
#[inline]
|
||||
fn scale_line_spacing(ls: LineSpacing, space_factor: f32) -> LineSpacing {
|
||||
match ls {
|
||||
LineSpacing::Multiple(m) => {
|
||||
// Guard against ascender/descender collisions: keep line spacing at or above
|
||||
// safe readability floor of 0.94 factor (~6% max compression).
|
||||
let floor = m * MIN_LINE_SPACING_FACTOR;
|
||||
let new_m = (m * space_factor).max(floor).clamp(0.5, 132.0);
|
||||
// Round upward to a hundredth so the resulting multiple never falls below the 0.94 floor.
|
||||
LineSpacing::Multiple(((new_m * 100.0).ceil() / 100.0).min(m))
|
||||
}
|
||||
LineSpacing::AtLeast(pts) => LineSpacing::AtLeast(scale_fixed_line_spacing(pts, space_factor)),
|
||||
LineSpacing::Exactly(pts) => LineSpacing::Exactly(scale_fixed_line_spacing(pts, space_factor)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Scale explicit font sizes on a `CharProps`.
|
||||
#[inline]
|
||||
fn scale_char_props(c: &mut CharProps, delta_pt: f32) {
|
||||
if delta_pt > 0.0 {
|
||||
if let Some(s) = c.size.as_mut() {
|
||||
*s = scale_size(*s, delta_pt);
|
||||
}
|
||||
if let Some(s) = c.size_cs.as_mut() {
|
||||
*s = scale_size(*s, delta_pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Scale explicit paragraph spacing on a `ParaProps`.
|
||||
#[inline]
|
||||
fn scale_para_props(p: &mut ParaProps, space_factor: f32) {
|
||||
if space_factor < 1.0 - f32::EPSILON {
|
||||
if let Some(sb) = p.space_before.as_mut() {
|
||||
*sb = scale_spacing(*sb, space_factor);
|
||||
}
|
||||
if let Some(sa) = p.space_after.as_mut() {
|
||||
*sa = scale_spacing(*sa, space_factor);
|
||||
}
|
||||
if let Some(ls) = p.line_spacing.as_mut() {
|
||||
*ls = scale_line_spacing(*ls, space_factor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds all text box IDs transitively reachable from Header and Footer parts
|
||||
/// (including linked text box chains and nested text boxes).
|
||||
pub fn header_footer_text_boxes(doc: &Document) -> BTreeSet<u32> {
|
||||
let mut hf_boxes: BTreeSet<u32> = BTreeSet::new();
|
||||
let mut queue: Vec<u32> = Vec::new();
|
||||
|
||||
for part in doc.parts.values() {
|
||||
if matches!(part.kind, PartKind::Header | PartKind::Footer) {
|
||||
for b in &part.blocks {
|
||||
each_para(b, 0, &mut |p| {
|
||||
for obj in &p.objects {
|
||||
for box_id in obj.text_boxes() {
|
||||
if hf_boxes.insert(box_id) {
|
||||
queue.push(box_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while let Some(box_id) = queue.pop() {
|
||||
if let Some(part) = doc.parts.get(&box_id) {
|
||||
if let Some(next_id) = part.body.next
|
||||
&& hf_boxes.insert(next_id)
|
||||
{
|
||||
queue.push(next_id);
|
||||
}
|
||||
for b in &part.blocks {
|
||||
each_para(b, 0, &mut |p| {
|
||||
for obj in &p.objects {
|
||||
for nested_id in obj.text_boxes() {
|
||||
if hf_boxes.insert(nested_id) {
|
||||
queue.push(nested_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
hf_boxes
|
||||
}
|
||||
|
||||
/// Freezes resolved character and paragraph formatting on a single paragraph using the provided stylesheet and optional table-style context.
|
||||
fn freeze_para_formatting(
|
||||
p: &mut Paragraph,
|
||||
styles: &StyleSheet,
|
||||
table_chr: Option<&CharProps>,
|
||||
table_para: Option<&ParaProps>,
|
||||
delta_pt: f32,
|
||||
space_factor: f32,
|
||||
) {
|
||||
let style = p.props.style.clone();
|
||||
if delta_pt > 0.0 {
|
||||
for r in &mut p.runs {
|
||||
if r.props.size.is_none() {
|
||||
let resolved = styles.resolve_char_in(style.as_deref(), table_chr, &r.props).size;
|
||||
r.props.size = Some(resolved);
|
||||
}
|
||||
if r.props.size_cs.is_none() {
|
||||
let resolved_cs = styles.resolve_char_in(style.as_deref(), table_chr, &r.props).size_cs;
|
||||
r.props.size_cs = Some(resolved_cs);
|
||||
}
|
||||
}
|
||||
if p.mark.size.is_none() {
|
||||
let resolved = styles.resolve_char_in(style.as_deref(), table_chr, &p.mark).size;
|
||||
p.mark.size = Some(resolved);
|
||||
}
|
||||
if p.mark.size_cs.is_none() {
|
||||
let resolved_cs = styles.resolve_char_in(style.as_deref(), table_chr, &p.mark).size_cs;
|
||||
p.mark.size_cs = Some(resolved_cs);
|
||||
}
|
||||
}
|
||||
if space_factor < 1.0 - f32::EPSILON {
|
||||
let resolved_p = styles.resolve_para_in(&p.props, table_para);
|
||||
if p.props.space_before.is_none() {
|
||||
p.props.space_before = Some(resolved_p.space_before);
|
||||
}
|
||||
if p.props.space_after.is_none() {
|
||||
p.props.space_after = Some(resolved_p.space_after);
|
||||
}
|
||||
if p.props.line_spacing.is_none() {
|
||||
p.props.line_spacing = Some(resolved_p.line_spacing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursively freezes resolved formatting on blocks while preserving table-style context.
|
||||
fn freeze_blocks_formatting(
|
||||
blocks: &mut [Arc<Block>],
|
||||
styles: &StyleSheet,
|
||||
table_chr: Option<&CharProps>,
|
||||
table_para: Option<&ParaProps>,
|
||||
delta_pt: f32,
|
||||
space_factor: f32,
|
||||
) {
|
||||
use crate::styles::PartSet;
|
||||
|
||||
for b in blocks {
|
||||
let b = Arc::make_mut(b);
|
||||
match b {
|
||||
Block::Para(p) => {
|
||||
freeze_para_formatting(p, styles, table_chr, table_para, delta_pt, space_factor);
|
||||
}
|
||||
Block::Table(tbl) => {
|
||||
let style = tbl.props.style.as_deref().and_then(|id| styles.table_style(id));
|
||||
let cell_para = style.as_ref().map(|st| &st.para);
|
||||
let nrows = tbl.rows.len();
|
||||
let ncols = tbl.cols();
|
||||
let cell_grids: Vec<Vec<usize>> =
|
||||
(0..nrows).map(|ri| (0..tbl.rows[ri].cells.len()).map(|ci| tbl.grid_col(ri, ci)).collect()).collect();
|
||||
for (ri, row) in tbl.rows.iter_mut().enumerate() {
|
||||
for (ci, cell) in row.cells.iter_mut().enumerate() {
|
||||
let mut cell_chr = CharProps::default();
|
||||
if let Some(st) = &style {
|
||||
let sizes = (st.parts.band_size.unwrap_or(0), st.parts.col_band_size.unwrap_or(0));
|
||||
let g = cell_grids.get(ri).and_then(|r| r.get(ci)).copied().unwrap_or(0);
|
||||
let span = cell.span();
|
||||
let region = PartSet::of_cell(&tbl.props.look, sizes, (ri, nrows), (g, span, ncols));
|
||||
cell_chr.overlay(&st.chr);
|
||||
for part in region.iter() {
|
||||
let f = st.part(part);
|
||||
cell_chr.overlay(&f.chr);
|
||||
}
|
||||
}
|
||||
freeze_blocks_formatting(&mut cell.blocks, styles, Some(&cell_chr), cell_para, delta_pt, space_factor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Fits font sizes across the document by `delta_pt` points and scales paragraph
|
||||
/// spacing by `space_factor`.
|
||||
///
|
||||
/// Clean-room typographical helper for Fit One Page. Preserves relative typography
|
||||
/// hierarchies between headings, body text, and direct run formatting.
|
||||
/// Clamps font sizes to `[4.0, 1638.0]` pt and rounds to 0.5 pt (half-points).
|
||||
/// Headers, footers, and comments are explicitly excluded and retain their exact original sizes.
|
||||
/// Footnotes are included and shrink alongside body text.
|
||||
pub fn fit_document_typography(doc: &mut Document, delta_pt: f32, space_factor: f32) {
|
||||
if !delta_pt.is_finite() || !space_factor.is_finite() {
|
||||
return;
|
||||
}
|
||||
let delta_pt = delta_pt.max(0.0);
|
||||
let space_factor = space_factor.clamp(0.1, 1.0);
|
||||
|
||||
if delta_pt == 0.0 && (space_factor - 1.0).abs() < f32::EPSILON {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Find all text boxes reachable from Header and Footer parts (including nested text boxes and chains).
|
||||
let hf_boxes = header_footer_text_boxes(doc);
|
||||
|
||||
// Freeze header, footer, and comment font sizes and spacing (and reachable text boxes) before modifying styles,
|
||||
// preserving table-style context so header/footer table cells retain their conditional table formatting,
|
||||
// and comment stories retain their inherited formatting from shared styles.
|
||||
let old_styles = doc.styles.clone();
|
||||
for (id, part) in doc.parts.iter_mut() {
|
||||
if matches!(part.kind, PartKind::Header | PartKind::Footer | PartKind::Comment) || hf_boxes.contains(id) {
|
||||
freeze_blocks_formatting(&mut part.blocks, &old_styles, None, None, delta_pt, space_factor);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Adjust styles: document defaults, character styles, paragraph styles, table style regions.
|
||||
let st = Arc::make_mut(&mut doc.styles);
|
||||
|
||||
if delta_pt > 0.0 {
|
||||
// Default character formatting
|
||||
let default_size = st.default_chr.size.unwrap_or(crate::props::DEFAULT_FONT_SIZE);
|
||||
st.default_chr.size = Some(scale_size(default_size, delta_pt));
|
||||
if let Some(cs) = st.default_chr.size_cs.as_mut() {
|
||||
*cs = scale_size(*cs, delta_pt);
|
||||
}
|
||||
}
|
||||
|
||||
// Default paragraph formatting
|
||||
scale_para_props(&mut st.default_para, space_factor);
|
||||
|
||||
// All named styles (excluding header/footer styles if any)
|
||||
for style in &mut st.styles {
|
||||
let is_hf = style.id.eq_ignore_ascii_case("header")
|
||||
|| style.id.eq_ignore_ascii_case("footer")
|
||||
|| style.name.eq_ignore_ascii_case("header")
|
||||
|| style.name.eq_ignore_ascii_case("footer");
|
||||
if is_hf {
|
||||
continue;
|
||||
}
|
||||
|
||||
if delta_pt > 0.0 {
|
||||
scale_char_props(&mut style.chr, delta_pt);
|
||||
}
|
||||
scale_para_props(&mut style.para, space_factor);
|
||||
|
||||
if let Some(t) = style.table.as_mut()
|
||||
&& delta_pt > 0.0
|
||||
{
|
||||
for c in [&mut t.header_chr, &mut t.band_chr, &mut t.first_col_chr, &mut t.last_col_chr, &mut t.total_chr, &mut t.col_band_chr] {
|
||||
scale_char_props(c, delta_pt);
|
||||
}
|
||||
for part in t.regions.values_mut() {
|
||||
scale_char_props(&mut part.chr, delta_pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Helper to shrink editable paragraph content (runs, mark, spacing).
|
||||
let mut shrink_paragraph = |p: &mut Paragraph| {
|
||||
scale_para_props(&mut p.props, space_factor);
|
||||
|
||||
if delta_pt > 0.0 {
|
||||
// Scale explicit paragraph mark size
|
||||
scale_char_props(&mut p.mark, delta_pt);
|
||||
|
||||
// Scale explicit run sizes
|
||||
for r in &mut p.runs {
|
||||
scale_char_props(&mut r.props, delta_pt);
|
||||
}
|
||||
}
|
||||
|
||||
p.touch();
|
||||
};
|
||||
|
||||
// 4. Traverse main body story.
|
||||
for b in &mut doc.body {
|
||||
each_para_mut(Arc::make_mut(b), 0, &mut shrink_paragraph);
|
||||
}
|
||||
|
||||
// 5. Traverse secondary editable stories (footnotes, body text boxes), excluding headers, footers,
|
||||
// comments, and text boxes reachable from headers or footers.
|
||||
for (id, part) in doc.parts.iter_mut() {
|
||||
if matches!(part.kind, PartKind::Header | PartKind::Footer | PartKind::Comment) || hf_boxes.contains(id) {
|
||||
continue;
|
||||
}
|
||||
for b in &mut part.blocks {
|
||||
each_para_mut(Arc::make_mut(b), 0, &mut shrink_paragraph);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::block::Block;
|
||||
use crate::para::Paragraph;
|
||||
use crate::props::{CharProps, LineSpacing};
|
||||
use crate::table::Table;
|
||||
use crate::{Document, para_block};
|
||||
|
||||
#[test]
|
||||
fn test_shrink_preserves_hierarchy() {
|
||||
let mut doc = Document::new();
|
||||
// Setup Heading 1 paragraph and Normal paragraph
|
||||
let h1_para = Paragraph::with_text("Heading 1 Title", CharProps::default()).styled("Heading1");
|
||||
let normal_para = Paragraph::with_text("Body text in Normal style", CharProps::default()).styled("Normal");
|
||||
let mut custom_run_para = Paragraph::with_text("Normal prefix ", CharProps::default()).styled("Normal");
|
||||
custom_run_para.insert_text(custom_run_para.len(), "Special large text", &CharProps { size: Some(24.0), ..Default::default() }).unwrap();
|
||||
|
||||
doc.body = vec![para_block(h1_para), para_block(normal_para), para_block(custom_run_para)];
|
||||
|
||||
let h1_before = doc.styles.resolve_char(Some("Heading1"), &CharProps::default()).size;
|
||||
let normal_before = doc.styles.resolve_char(Some("Normal"), &CharProps::default()).size;
|
||||
|
||||
fit_document_typography(&mut doc, 0.5, 1.0);
|
||||
|
||||
let h1_after = doc.styles.resolve_char(Some("Heading1"), &CharProps::default()).size;
|
||||
let normal_after = doc.styles.resolve_char(Some("Normal"), &CharProps::default()).size;
|
||||
|
||||
// Both styles scaled down by exactly 0.5 pt
|
||||
assert_eq!(h1_after, h1_before - 0.5);
|
||||
assert_eq!(normal_after, normal_before - 0.5);
|
||||
// Relative hierarchy preserved
|
||||
assert_eq!(h1_after - normal_after, h1_before - normal_before);
|
||||
|
||||
// Explicit run in paragraph 3 was 24.0 pt, now 23.5 pt
|
||||
let p3 = doc.body[2].as_para().unwrap();
|
||||
assert_eq!(p3.runs[1].props.size, Some(23.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_excludes_headers_and_footers() {
|
||||
let mut doc = Document::new();
|
||||
let body_para = Paragraph::with_text("Main story body text", CharProps::default());
|
||||
doc.body = vec![para_block(body_para)];
|
||||
|
||||
let header_para = Paragraph::with_text("Header Text", CharProps { size: Some(10.0), ..Default::default() });
|
||||
let footer_para = Paragraph::with_text("Footer Text Page 1", CharProps::default()); // inherits
|
||||
|
||||
let h_id = doc.add_part(PartKind::Header, vec![para_block(header_para)]);
|
||||
let f_id = doc.add_part(PartKind::Footer, vec![para_block(footer_para)]);
|
||||
|
||||
let footer_before_size = doc.styles.resolve_char(None, &CharProps::default()).size;
|
||||
|
||||
fit_document_typography(&mut doc, 0.5, 1.0);
|
||||
|
||||
// Body default style shrank
|
||||
let body_after_size = doc.styles.resolve_char(None, &CharProps::default()).size;
|
||||
assert_eq!(body_after_size, footer_before_size - 0.5);
|
||||
|
||||
// Header retained its explicit 10.0 pt size
|
||||
let h_part = doc.parts.get(&h_id).unwrap();
|
||||
let h_p = h_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(h_p.runs[0].props.size, Some(10.0));
|
||||
|
||||
// Footer retained its original resolved size
|
||||
let f_part = doc.parts.get(&f_id).unwrap();
|
||||
let f_p = f_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(f_p.runs[0].props.size, Some(footer_before_size));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_scales_footnotes() {
|
||||
let mut doc = Document::new();
|
||||
let body_para = Paragraph::with_text("Main body with note", CharProps::default());
|
||||
doc.body = vec![para_block(body_para)];
|
||||
|
||||
let fn_para = Paragraph::with_text("Footnote content", CharProps { size: Some(10.0), ..Default::default() });
|
||||
let fn_id = doc.add_part(PartKind::Footnote, vec![para_block(fn_para)]);
|
||||
|
||||
fit_document_typography(&mut doc, 0.5, 1.0);
|
||||
|
||||
let fn_part = doc.parts.get(&fn_id).unwrap();
|
||||
let fn_p = fn_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(fn_p.runs[0].props.size, Some(9.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_clamps_to_min_floor() {
|
||||
let mut doc = Document::new();
|
||||
let mut p = Paragraph::with_text("Very tiny text", CharProps { size: Some(4.2), ..Default::default() });
|
||||
p.insert_text(p.len(), "Floor text", &CharProps { size: Some(4.0), ..Default::default() }).unwrap();
|
||||
doc.body = vec![para_block(p)];
|
||||
|
||||
fit_document_typography(&mut doc, 1.0, 1.0);
|
||||
|
||||
let p_after = doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p_after.runs[0].props.size, Some(4.0));
|
||||
assert_eq!(p_after.runs[1].props.size, Some(4.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_zero_delta_identity() {
|
||||
let mut doc = Document::new();
|
||||
let h1_para = Paragraph::with_text("Heading 1 Title", CharProps::default()).styled("Heading1");
|
||||
let normal_para = Paragraph::with_text("Body text", CharProps::default());
|
||||
doc.body = vec![para_block(h1_para), para_block(normal_para)];
|
||||
doc.add_part(PartKind::Header, vec![para_block(Paragraph::with_text("Header", CharProps::default()))]);
|
||||
doc.add_part(PartKind::Footnote, vec![para_block(Paragraph::with_text("Note", CharProps::default()))]);
|
||||
|
||||
let before = doc.clone();
|
||||
fit_document_typography(&mut doc, 0.0, 1.0);
|
||||
assert_eq!(doc, before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_paragraph_spacing() {
|
||||
let mut doc = Document::new();
|
||||
let mut p = Paragraph::with_text("Spaced paragraph", CharProps::default());
|
||||
p.props.space_before = Some(18.0);
|
||||
p.props.space_after = Some(12.0);
|
||||
p.props.line_spacing = Some(LineSpacing::Multiple(1.15));
|
||||
doc.body = vec![para_block(p)];
|
||||
|
||||
fit_document_typography(&mut doc, 0.5, 0.9);
|
||||
|
||||
let p_after = doc.body[0].as_para().unwrap();
|
||||
// 18.0 * 0.9 = 16.2, capped at the requested reduction
|
||||
assert_eq!(p_after.props.space_before, Some(16.2));
|
||||
// 12.0 * 0.9 = 10.8, capped at the requested reduction
|
||||
assert_eq!(p_after.props.space_after, Some(10.8));
|
||||
// 1.15 * 0.9 = 1.035 -> clamped to safe line spacing floor (1.15 * 0.94 = 1.081) -> ceil hundredth 1.09
|
||||
assert_eq!(p_after.props.line_spacing, Some(LineSpacing::Multiple(1.09)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spacing_scaling_never_exceeds_requested_compression() {
|
||||
// 0.26 * 0.94 = 0.2444 pt: it must not round down to zero and remove all spacing.
|
||||
let tiny = scale_spacing(0.26, 0.94);
|
||||
assert!(tiny > 0.0 && tiny <= 0.26, "tiny spacing must shrink without vanishing: {tiny}");
|
||||
// 0.019 * 0.94 = 0.01786 pt: rounding down to hundredths would remove 47%, so round up.
|
||||
assert_eq!(scale_spacing(0.019, 0.94), 0.019);
|
||||
// 18.0 * 0.9 = 16.2 pt: the reduction stays at the requested 1.8 pt.
|
||||
assert_eq!(scale_spacing(18.0, 0.9), 16.2);
|
||||
assert_eq!(scale_spacing(0.0, 0.9), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line_spacing_floor_prevents_overlap() {
|
||||
let mut doc = Document::new();
|
||||
let mut p = Paragraph::with_text("Single spaced text", CharProps::default());
|
||||
p.props.line_spacing = Some(LineSpacing::Multiple(1.0));
|
||||
doc.body = vec![para_block(p)];
|
||||
|
||||
// Even with aggressive space_factor = 0.5, line spacing multiple must not fall below safe floor (0.94)
|
||||
fit_document_typography(&mut doc, 0.5, 0.5);
|
||||
|
||||
let p_after = doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p_after.props.line_spacing, Some(LineSpacing::Multiple(0.94)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_text_boxes_are_frozen_and_do_not_shrink() {
|
||||
let mut doc = Document::new();
|
||||
let body_p = Paragraph::with_text("Body text", CharProps { size: Some(12.0), ..Default::default() });
|
||||
doc.body = vec![para_block(body_p)];
|
||||
|
||||
let header_box_id = doc
|
||||
.add_part(PartKind::TextBox, vec![para_block(Paragraph::with_text("Header box", CharProps { size: Some(14.0), ..Default::default() }))]);
|
||||
let mut header_p = Paragraph::with_text("Header prefix ", CharProps { size: Some(10.0), ..Default::default() });
|
||||
let shape = crate::para::InlineObject::Shape {
|
||||
kind: crate::para::ShapeKind::TextBox,
|
||||
w: 144.0,
|
||||
h: 72.0,
|
||||
fill: None,
|
||||
stroke: None,
|
||||
stroke_width: 0.75,
|
||||
float: Default::default(),
|
||||
story: Some(header_box_id),
|
||||
freeform: None,
|
||||
effects: Default::default(),
|
||||
extra: Default::default(),
|
||||
};
|
||||
header_p.objects.push(shape);
|
||||
doc.add_part(PartKind::Header, vec![para_block(header_p)]);
|
||||
|
||||
let body_box_id =
|
||||
doc.add_part(PartKind::TextBox, vec![para_block(Paragraph::with_text("Body box", CharProps { size: Some(16.0), ..Default::default() }))]);
|
||||
|
||||
fit_document_typography(&mut doc, 2.0, 1.0);
|
||||
|
||||
let b_p = doc.body[0].as_para().unwrap();
|
||||
assert_eq!(b_p.runs[0].props.size, Some(10.0));
|
||||
|
||||
let bb_part = doc.parts.get(&body_box_id).unwrap();
|
||||
let bb_p = bb_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(bb_p.runs[0].props.size, Some(14.0));
|
||||
|
||||
let hb_part = doc.parts.get(&header_box_id).unwrap();
|
||||
let hb_p = hb_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(hb_p.runs[0].props.size, Some(14.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_table_cells() {
|
||||
let mut doc = Document::new();
|
||||
let mut table = Table::new(1, 1, 100.0);
|
||||
let cell_p = Paragraph::with_text("Cell text", CharProps { size: Some(12.0), ..Default::default() });
|
||||
table.rows[0].cells[0].blocks = vec![para_block(cell_p)];
|
||||
doc.body = vec![Arc::new(Block::Table(table))];
|
||||
|
||||
fit_document_typography(&mut doc, 0.5, 1.0);
|
||||
|
||||
let tbl = doc.body[0].as_table().unwrap();
|
||||
let cell_p_after = tbl.rows[0].cells[0].blocks[0].as_para().unwrap();
|
||||
assert_eq!(cell_p_after.runs[0].props.size, Some(11.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exact_line_spacing_preserves_floor_upward() {
|
||||
// LineSpacing::Exactly(7.5) with space_factor = 0.90 (below MIN_LINE_SPACING_FACTOR 0.94)
|
||||
// Floor = 7.5 * 0.94 = 7.05 pt. Rounding upward to half-points yields 7.5 pt (>= 7.05 pt).
|
||||
let ls = scale_line_spacing(LineSpacing::Exactly(7.5), 0.90);
|
||||
assert_eq!(ls, LineSpacing::Exactly(7.5));
|
||||
|
||||
// LineSpacing::Exactly(10.0) with space_factor = 0.90:
|
||||
// Floor = 10.0 * 0.94 = 9.4 pt. Rounding upward to half-points yields 9.5 pt (>= 9.4 pt).
|
||||
let ls2 = scale_line_spacing(LineSpacing::Exactly(10.0), 0.90);
|
||||
assert_eq!(ls2, LineSpacing::Exactly(9.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spacing_only_leaves_unrounded_font_sizes_unchanged() {
|
||||
let mut doc = Document::new();
|
||||
// A run with fractional 9.3 pt font
|
||||
let p = Paragraph::with_text("Unrounded size text", CharProps { size: Some(9.3), ..Default::default() });
|
||||
doc.body = vec![para_block(p)];
|
||||
|
||||
// Spacing-only trial (delta_pt = 0.0, space_factor = 0.94)
|
||||
fit_document_typography(&mut doc, 0.0, 0.94);
|
||||
|
||||
let p_after = doc.body[0].as_para().unwrap();
|
||||
assert_eq!(p_after.runs[0].props.size, Some(9.3), "spacing-only trial must not round or scale font size");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scale_size_never_enlarges_sub_floor_font() {
|
||||
// 2.0 pt run (below 4.0 pt floor) with delta 0.5 pt must not be enlarged to 4.0 pt
|
||||
assert_eq!(scale_size(2.0, 0.5), 2.0);
|
||||
// 3.0 pt run with delta 0.5 pt must not be enlarged to 4.0 pt
|
||||
assert_eq!(scale_size(3.0, 0.5), 3.0);
|
||||
// 5.0 pt run with delta 0.5 pt shrinks to 4.5 pt
|
||||
assert_eq!(scale_size(5.0, 0.5), 4.5);
|
||||
// 12.0 pt run with delta 0.3 pt rounds up to 12.0 pt so reduction (0.0 pt) never exceeds delta (0.3 pt)
|
||||
assert_eq!(scale_size(12.0, 0.3), 12.0);
|
||||
// Zero delta leaves size unchanged
|
||||
assert_eq!(scale_size(2.0, 0.0), 2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_excludes_comment_stories() {
|
||||
let mut doc = Document::new();
|
||||
let comment_p = Paragraph::with_text("Comment text", CharProps { size: Some(8.0), ..Default::default() });
|
||||
let comment_id = doc.add_part(PartKind::Comment, vec![para_block(comment_p)]);
|
||||
|
||||
// Shrink with 1.0 pt delta
|
||||
fit_document_typography(&mut doc, 1.0, 0.94);
|
||||
|
||||
let comment_part = doc.parts.get(&comment_id).unwrap();
|
||||
let p_after = comment_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(p_after.runs[0].props.size, Some(8.0), "comments must be excluded from typography shrink pass");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shrink_preserves_comment_inherited_formatting() {
|
||||
let mut doc = Document::new();
|
||||
// Setup a named style "CommentText" with 12.0 pt font and 6.0 pt spacing after.
|
||||
let comment_style = crate::styles::Style {
|
||||
id: "CommentText".into(),
|
||||
name: "Comment Text".into(),
|
||||
kind: crate::styles::StyleKind::Paragraph,
|
||||
chr: CharProps { size: Some(12.0), ..Default::default() },
|
||||
para: ParaProps { space_after: Some(6.0), line_spacing: Some(LineSpacing::Multiple(1.2)), ..Default::default() },
|
||||
..Default::default()
|
||||
};
|
||||
Arc::make_mut(&mut doc.styles).upsert(comment_style);
|
||||
|
||||
// Comment paragraph inheriting from "CommentText" without explicit run size or paragraph spacing
|
||||
let mut comment_p = Paragraph::with_text("Inherited comment text", CharProps::default());
|
||||
comment_p.props.style = Some("CommentText".to_string());
|
||||
let comment_id = doc.add_part(PartKind::Comment, vec![para_block(comment_p)]);
|
||||
|
||||
// Shrink with 1.0 pt delta and 0.94 space factor
|
||||
fit_document_typography(&mut doc, 1.0, 0.94);
|
||||
|
||||
// Shared style "CommentText" is scaled (12.0 -> 11.0 pt)
|
||||
let scaled_style = doc.styles.get("CommentText").unwrap();
|
||||
assert_eq!(scaled_style.chr.size, Some(11.0));
|
||||
|
||||
// The comment story must have frozen its original inherited formatting before styles were modified
|
||||
let comment_part = doc.parts.get(&comment_id).unwrap();
|
||||
let p_after = comment_part.blocks[0].as_para().unwrap();
|
||||
assert_eq!(p_after.runs[0].props.size, Some(12.0), "comment run size must remain 12.0 pt from old styles");
|
||||
assert_eq!(p_after.props.space_after, Some(6.0), "comment paragraph space_after must remain 6.0 pt from old styles");
|
||||
assert_eq!(p_after.props.line_spacing, Some(LineSpacing::Multiple(1.2)), "comment paragraph line_spacing must remain 1.2 from old styles");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_table_text_inherits_size_from_table_style() {
|
||||
let mut doc = Document::new();
|
||||
// Setup a custom table style with 9.0 pt font
|
||||
let st = crate::styles::Style {
|
||||
id: "HeaderTableStyle".into(),
|
||||
name: "HeaderTableStyle".into(),
|
||||
kind: crate::styles::StyleKind::Table,
|
||||
chr: CharProps { size: Some(9.0), ..Default::default() },
|
||||
table: Some(crate::styles::TableStyleParts::default()),
|
||||
..Default::default()
|
||||
};
|
||||
Arc::make_mut(&mut doc.styles).upsert(st);
|
||||
|
||||
// Create a table in a Header part that uses HeaderTableStyle
|
||||
let mut table = crate::Table::new(1, 1, 100.0);
|
||||
table.props.style = Some("HeaderTableStyle".to_string());
|
||||
// Cell paragraph with no explicit font size on run (inherits from table style)
|
||||
let cell_p = Paragraph::with_text("Header cell text", CharProps::default());
|
||||
table.rows[0].cells[0].blocks = vec![crate::para_block(cell_p)];
|
||||
|
||||
let header_id = doc.add_part(PartKind::Header, vec![Arc::new(Block::Table(table))]);
|
||||
|
||||
// Run shrink with delta_pt = 1.0 (body font would shrink)
|
||||
fit_document_typography(&mut doc, 1.0, 0.95);
|
||||
|
||||
// Verify that the header table cell's run preserved the 9.0 pt size from the table style,
|
||||
// and was not frozen to the document default (11.0 pt)
|
||||
let header_part = doc.parts.get(&header_id).unwrap();
|
||||
if let Block::Table(tbl) = header_part.blocks[0].as_ref() {
|
||||
let p = tbl.rows[0].cells[0].blocks[0].as_para().unwrap();
|
||||
assert_eq!(p.runs[0].props.size, Some(9.0), "header table text must inherit and freeze table style font size");
|
||||
} else {
|
||||
panic!("expected table in header part");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scale_line_spacing_never_increases_fixed_or_multiple() {
|
||||
// LineSpacing::Exactly(7.45) with space_factor = 0.94:
|
||||
// 7.45 * 0.94 = 7.003 -> ceil to half-point would yield 7.5 pt, but capping at 7.45 ensures no enlargement
|
||||
assert_eq!(scale_fixed_line_spacing(7.45, 0.94), 7.45);
|
||||
// Regular scaling: 10.0 pt with 0.94 -> 9.4 -> ceil to half-point = 9.5 pt <= 10.0 pt
|
||||
assert_eq!(scale_fixed_line_spacing(10.0, 0.94), 9.5);
|
||||
|
||||
// LineSpacing::Multiple with small factor never enlarges
|
||||
let ls = LineSpacing::Multiple(0.942);
|
||||
let scaled = scale_line_spacing(ls, 0.94);
|
||||
if let LineSpacing::Multiple(m) = scaled {
|
||||
assert!(m <= 0.942);
|
||||
} else {
|
||||
panic!("expected Multiple");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scale_spacing_never_increases_value() {
|
||||
// Spacing 0.3 pt scaled by 0.94 yields 0.282 pt, rounded up to 0.29 and never above 0.3 pt.
|
||||
assert_eq!(scale_spacing(0.3, 0.94), 0.29);
|
||||
// 12.0 pt * 0.94 = 11.28 pt, capped at the requested reduction.
|
||||
assert_eq!(scale_spacing(12.0, 0.94), 11.28);
|
||||
// Zero spacing remains zero.
|
||||
assert_eq!(scale_spacing(0.0, 0.94), 0.0);
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@ use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::props::{
|
||||
Align, Border, BorderStyle, Borders, CharProps, LineSpacing, ParaProps, Rgb, TabAlign, TabLeader, TabStop, TableLook, TextColor, Underline,
|
||||
VAlign,
|
||||
Align, Border, BorderStyle, Borders, CharProps, LineSpacing, ParaProps, PropChange, Rgb, TabAlign, TabLeader, TabStop, TableLook, TextColor,
|
||||
Underline, VAlign,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
|
||||
@@ -46,6 +46,13 @@ pub struct Style {
|
||||
/// (`w:locked`).
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub locked: bool,
|
||||
/// A tracked change of the style's paragraph properties (`w:pPrChange`).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub para_change: Option<Box<PropChange<ParaProps>>>,
|
||||
/// A tracked change of the style's run properties (`w:rPrChange`). Kept separate because a
|
||||
/// foreign DOCX can attribute the two property changes to different revisions.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub chr_change: Option<Box<PropChange<CharProps>>>,
|
||||
/// The default style of its kind (`w:default`), whatever its id: localized documents call
|
||||
/// the default character, table and list styles something other than the English ids.
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
//!
|
||||
//! Tables are indexed by code − 0x20; 0 = no Unicode equivalent.
|
||||
|
||||
/// The symbol fonts this module knows, by lowercase family name.
|
||||
fn table(family: &str) -> Option<&'static [u32; 224]> {
|
||||
/// The symbol table for a single family name, by lowercase family name.
|
||||
fn single_table(family: &str) -> Option<&'static [u32; 224]> {
|
||||
let f = family.trim();
|
||||
if f.eq_ignore_ascii_case("symbol") || f.eq_ignore_ascii_case("symbol (pcl6)") {
|
||||
Some(&SYMBOL)
|
||||
@@ -37,6 +37,11 @@ fn table(family: &str) -> Option<&'static [u32; 224]> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The symbol fonts this module knows, by lowercase family name.
|
||||
fn table(family: &str) -> Option<&'static [u32; 224]> {
|
||||
if family.contains(';') { family.split(';').map(str::trim).filter(|s| !s.is_empty()).find_map(single_table) } else { single_table(family) }
|
||||
}
|
||||
|
||||
/// Whether `family` is one of Word's symbol fonts (Symbol, Wingdings, Wingdings 2, Wingdings 3,
|
||||
/// Webdings), whose characters [`symbol_char`] maps.
|
||||
pub fn is_symbol_font(family: &str) -> bool {
|
||||
|
||||
@@ -786,3 +786,31 @@ fn lone_table_open_markers_mixed_tables_and_paired() {
|
||||
assert!(res1.controls.open.is_empty());
|
||||
assert_eq!(res1.controls.close, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_picture_names_are_capped_at_a_char_boundary() {
|
||||
let mut d = Document::new();
|
||||
let long: String = "é".repeat(5000);
|
||||
d.missing_pictures.push(MissingPicture { part: long.clone(), story: long, reason: MissingPictureReason::MissingPart });
|
||||
let item = &d.missing_pictures.list[0];
|
||||
assert_eq!((item.part.chars().count(), item.story.chars().count()), (1024, 1024));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_pictures_are_capped_deduplicated_and_not_content() {
|
||||
let mut d = Document::new();
|
||||
let item = |n: usize| MissingPicture { part: format!("p{n}"), story: "word/document.xml".into(), reason: MissingPictureReason::MissingPart };
|
||||
d.missing_pictures.push(item(0));
|
||||
d.missing_pictures.push(item(0));
|
||||
assert_eq!(d.missing_pictures.list.len(), 1);
|
||||
for n in 1..MAX_MISSING_PICTURES + 3 {
|
||||
d.missing_pictures.push(item(n));
|
||||
}
|
||||
assert_eq!(d.missing_pictures.list.len(), MAX_MISSING_PICTURES);
|
||||
assert!(d.missing_pictures.truncated);
|
||||
let e = Document::new();
|
||||
assert_eq!(d, e);
|
||||
assert!(d.same_content(&e));
|
||||
assert!(!serde_json::to_string(&d).unwrap().contains("p0"));
|
||||
assert_eq!(MissingPictureReason::ExternalTarget.label(), "external target");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
use std::sync::Arc;
|
||||
use wordcraft_doc::props::{Align, CharProps, ParaProps};
|
||||
use wordcraft_doc::{Block, Document, Paragraph, Path, Pos, StoryRef, Table, para_block};
|
||||
|
||||
/// Build an empty heading followed by explicitly formatted, mixed-run body text.
|
||||
fn fixture(first: &str, style: Option<&str>) -> Document {
|
||||
let mut d = Document::from_text(&format!("{first}\nBody paragraph."));
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap();
|
||||
p.props.style = Some("Heading1".into());
|
||||
p.props.outline_level = Some(0);
|
||||
p.mark = CharProps { size: Some(40.0), bold: Some(true), ..Default::default() };
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(1)).unwrap();
|
||||
p.props = ParaProps {
|
||||
style: style.map(str::to_string),
|
||||
align: Some(Align::Right),
|
||||
indent_left: Some(18.0),
|
||||
space_after: Some(7.0),
|
||||
keep_next: Some(false),
|
||||
..Default::default()
|
||||
};
|
||||
p.mark = CharProps { size: Some(11.0), bold: Some(false), lang: Some("en-GB".into()), ..Default::default() };
|
||||
p.format(0, 4, &|c| c.italic = Some(true)).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
/// Compare paragraph, mark and inline-run formatting independently of text.
|
||||
fn assert_format(p: &Paragraph, expected: &Paragraph) {
|
||||
assert_eq!(p.props, expected.props, "the following paragraph keeps all its properties");
|
||||
assert_eq!(p.mark, expected.mark, "the empty heading's mark must not replace the following mark");
|
||||
assert_eq!(serde_json::to_value(&p.runs).unwrap(), serde_json::to_value(&expected.runs).unwrap());
|
||||
}
|
||||
|
||||
/// Deleting an empty paragraph preserves the following style, including implicit Normal.
|
||||
#[test]
|
||||
fn deleting_an_empty_paragraph_keeps_the_following_format() {
|
||||
for first_style in ["Heading1", "Heading3", "Normal"] {
|
||||
for style in [None, Some("Normal"), Some("Heading2")] {
|
||||
let mut d = fixture("", style);
|
||||
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().props.style = Some(first_style.into());
|
||||
let expected = d.para_at(&Pos::body(1, 0)).unwrap().clone();
|
||||
assert_eq!(d.delete_range(&Pos::body(0, 0), &Pos::body(1, 0)).unwrap(), Pos::body(0, 0));
|
||||
let p = d.para_at(&Pos::body(0, 0)).unwrap();
|
||||
assert_eq!(p.text, expected.text);
|
||||
assert_format(p, &expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A selection spanning several paragraphs keeps the surviving tail format in both directions.
|
||||
#[test]
|
||||
fn an_empty_start_preserves_the_last_paragraph_over_a_multi_paragraph_selection() {
|
||||
for backwards in [false, true] {
|
||||
let mut d = fixture("", None);
|
||||
d.body.insert(1, para_block(Paragraph::with_text("Removed middle paragraph", Default::default()).styled("Heading2")));
|
||||
let mut expected = d.para_at(&Pos::body(2, 0)).unwrap().clone();
|
||||
expected.delete(0, 5).unwrap();
|
||||
let (a, b) = (Pos::body(0, 0), Pos::body(2, 5));
|
||||
d.delete_range(if backwards { &b } else { &a }, if backwards { &a } else { &b }).unwrap();
|
||||
assert_eq!(d.body.len(), 1);
|
||||
let p = d.para_at(&Pos::body(0, 0)).unwrap();
|
||||
assert_eq!(p.text, "paragraph.");
|
||||
assert_format(p, &expected);
|
||||
}
|
||||
}
|
||||
|
||||
/// A control whose closing marker survives must move across the join without replacing tail formatting.
|
||||
#[test]
|
||||
fn an_empty_start_keeps_a_control_from_a_deleted_middle_paragraph() {
|
||||
use wordcraft_doc::control::ContentControl;
|
||||
use wordcraft_doc::{InlineObject, Revision, RevisionKind};
|
||||
|
||||
let mut d = fixture("", Some("Normal"));
|
||||
d.body.insert(1, para_block(Paragraph::with_text("Removed", Default::default())));
|
||||
d.body.push(para_block(Paragraph::with_text("After", Default::default())));
|
||||
d.revisions.push(Revision { kind: RevisionKind::Insert, ..Default::default() });
|
||||
d.revisions.push(Revision { kind: RevisionKind::Delete, ..Default::default() });
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(2)).unwrap();
|
||||
p.mark.ins = Some(0);
|
||||
p.mark.del = Some(1);
|
||||
let expected = p.clone();
|
||||
let marker = CharProps { bold: Some(true), ins: Some(0), ..Default::default() };
|
||||
d.insert_object(&Pos::body(1, 0), InlineObject::ControlStart { control: Box::new(ContentControl::default()) }, &marker).unwrap();
|
||||
d.insert_object(&Pos::body(3, 5), InlineObject::ControlEnd, &Default::default()).unwrap();
|
||||
assert_eq!(d.control_ranges(StoryRef::Body).len(), 1);
|
||||
|
||||
d.delete_range(&Pos::body(0, 0), &Pos::body(2, 0)).unwrap();
|
||||
let p = d.para_at(&Pos::body(0, 0)).unwrap();
|
||||
assert_eq!(p.props, expected.props);
|
||||
assert_eq!(p.mark, expected.mark, "tail revision IDs and formatting survive the moved marker");
|
||||
assert_eq!(p.props_of_char(0), &marker);
|
||||
assert!(matches!(p.objects.as_slice(), [InlineObject::ControlStart { .. }]));
|
||||
assert_eq!(p.text, format!("\u{fffc}{}", expected.text));
|
||||
let ranges = d.control_ranges(StoryRef::Body);
|
||||
assert_eq!(ranges.len(), 1);
|
||||
assert_eq!(ranges[0].start, Pos::body(0, 0));
|
||||
assert_eq!(ranges[0].end, Pos::body(1, 5));
|
||||
assert!(!d.has_unbalanced_controls());
|
||||
}
|
||||
|
||||
/// The empty-paragraph rule applies in header stories and table-cell containers.
|
||||
#[test]
|
||||
fn empty_paragraph_merges_work_in_a_header_and_a_table_cell() {
|
||||
for cell in [false, true] {
|
||||
let mut d = fixture("", Some("Normal"));
|
||||
let expected = d.para_at(&Pos::body(1, 0)).unwrap().clone();
|
||||
let (story, a, b) = if cell {
|
||||
let mut table = Table::new(1, 1, 300.0);
|
||||
table.rows[0].cells[0].blocks = d.body.clone();
|
||||
d.body = vec![Arc::new(Block::Table(table)), para_block(Paragraph::new())];
|
||||
(StoryRef::Body, Path(vec![0, 0, 0, 0]), Path(vec![0, 0, 0, 1]))
|
||||
} else {
|
||||
let id = d.add_part(wordcraft_doc::PartKind::Header, d.body.clone());
|
||||
(StoryRef::Part(id), Path::top(0), Path::top(1))
|
||||
};
|
||||
d.delete_range(&Pos { story, path: a.clone(), off: 0 }, &Pos { story, path: b, off: 0 }).unwrap();
|
||||
assert_format(d.para(story, &a).unwrap(), &expected);
|
||||
}
|
||||
}
|
||||
|
||||
/// Absorbing an empty section keeps the following format and inherited header.
|
||||
#[test]
|
||||
fn deleting_an_empty_section_paragraph_keeps_format_and_linked_headers() {
|
||||
let mut d = fixture("", None);
|
||||
let expected = d.para_at(&Pos::body(1, 0)).unwrap().clone();
|
||||
let header = d.add_part(wordcraft_doc::PartKind::Header, vec![para_block(Paragraph::with_text("Header", Default::default()))]);
|
||||
let mut section = d.last_section.clone();
|
||||
section.headers.default = Some(header);
|
||||
d.para_mut(StoryRef::Body, &Path::top(0)).unwrap().section = Some(Box::new(section));
|
||||
d.last_section.margin_left = 96.0;
|
||||
d.delete_range(&Pos::body(0, 0), &Pos::body(1, 0)).unwrap();
|
||||
assert_format(d.para_at(&Pos::body(0, 0)).unwrap(), &expected);
|
||||
assert!(d.para_at(&Pos::body(0, 0)).unwrap().section.is_none());
|
||||
assert_eq!(d.last_section.headers.default, Some(header));
|
||||
assert_eq!(d.last_section.margin_left, 96.0);
|
||||
}
|
||||
|
||||
/// Deleting actual text continues to use the first paragraph merge rule.
|
||||
#[test]
|
||||
fn joining_nonempty_paragraphs_keeps_the_existing_first_paragraph_rule() {
|
||||
for off in [0, 7] {
|
||||
let mut d = fixture("Heading", None);
|
||||
let expected = d.para_at(&Pos::body(0, 0)).unwrap().props.clone();
|
||||
d.delete_range(&Pos::body(0, off), &Pos::body(1, 0)).unwrap();
|
||||
assert_eq!(d.para_at(&Pos::body(0, 0)).unwrap().props, expected);
|
||||
}
|
||||
}
|
||||
|
||||
/// Removing a soft line break must not change paragraph formatting.
|
||||
#[test]
|
||||
fn a_soft_break_is_not_an_empty_paragraph_merge() {
|
||||
let mut d = fixture("", None);
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(0)).unwrap();
|
||||
p.insert_text(0, "\nHeading", &Default::default()).unwrap();
|
||||
let expected = p.props.clone();
|
||||
d.delete_range(&Pos::body(0, 0), &Pos::body(0, 1)).unwrap();
|
||||
assert_eq!(d.body.len(), 2);
|
||||
assert_eq!(d.para_at(&Pos::body(0, 0)).unwrap().props, expected);
|
||||
}
|
||||
@@ -9,17 +9,12 @@ description = "WordCraft reader for the Word 97-2003 binary file format (.doc, [
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-doc-binary = { workspace = true }
|
||||
craft-wordmodel = { workspace = true }
|
||||
wordcraft-doc = { workspace = true }
|
||||
wordcraft-geom = { workspace = true }
|
||||
cfb = { workspace = true }
|
||||
image = { workspace = true }
|
||||
miniz_oxide = { workspace = true }
|
||||
sha1 = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
wordcraft-doc = { workspace = true, features = ["test-support"] }
|
||||
proptest = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
cfb = { workspace = true }
|
||||
|
||||
@@ -1,455 +0,0 @@
|
||||
//! Password-protected Word 97-2003 files ([MS-DOC] §2.2.6, [MS-OFFCRYPTO] §2.3.5-2.3.7).
|
||||
//!
|
||||
//! Three schemes exist for `.doc`:
|
||||
//!
|
||||
//! * **XOR obfuscation** (`fObfuscated`): a 32-bit password verifier sits in `FibBase.lKey` and
|
||||
//! the streams are XORed with a 16-byte array derived from the password (method 2).
|
||||
//! * **RC4** (header version 1.1): `EncryptionHeader` at the start of the Table stream; the key
|
||||
//! per 512-byte block comes from MD5.
|
||||
//! * **RC4 CryptoAPI** (header version 2/3/4 . 2): like RC4 but with a SHA-1 key derivation, a
|
||||
//! key size of 40-128 bits and a variable-size header.
|
||||
//!
|
||||
//! In all three the WordDocument stream (past its first 68 bytes), the Table stream (past its
|
||||
//! header, for RC4) and the whole Data stream are transformed. The password is checked against
|
||||
//! the stored verifier before anything is decrypted, so a wrong password is reported and never
|
||||
//! yields a garbled document. Everything is written from the public specifications.
|
||||
|
||||
use sha1::{Digest, Sha1};
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
/// Bytes at the start of the WordDocument stream that are stored untransformed.
|
||||
pub(crate) const FIB_PLAIN: usize = 68;
|
||||
/// RC4 re-keys every this many bytes of a stream.
|
||||
const BLOCK: usize = 512;
|
||||
/// Longest password considered (the specifications' limit for the RC4 schemes).
|
||||
const MAX_PASSWORD_CHARS: usize = 255;
|
||||
/// XOR obfuscation uses at most this many single-byte password characters.
|
||||
const MAX_XOR_PASSWORD: usize = 15;
|
||||
/// `FibBase.flags`: the document is encrypted (`fEncrypted`) / obfuscated (`fObfuscated`).
|
||||
pub(crate) const F_ENCRYPTED: u16 = 0x0100;
|
||||
pub(crate) const F_OBFUSCATED: u16 = 0x8000;
|
||||
/// `EncryptionHeaderFlags`: `fCryptoAPI`, `fExternal`, `fAES`.
|
||||
const H_CRYPTO_API: u32 = 0x04;
|
||||
const H_EXTERNAL: u32 = 0x10;
|
||||
const H_AES: u32 = 0x20;
|
||||
|
||||
fn unsupported(what: &str) -> DocbinError {
|
||||
DocbinError::Encryption(what.into())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Primitives
|
||||
|
||||
/// MD5 ([RFC 1321]); the RC4 scheme is defined on it.
|
||||
pub(crate) fn md5(data: &[u8]) -> [u8; 16] {
|
||||
const S: [u32; 64] = [
|
||||
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16,
|
||||
23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
|
||||
];
|
||||
// K[i] = floor(2^32 * abs(sin(i + 1))), as the RFC defines it.
|
||||
let k: Vec<u32> = (0..64u32).map(|i| (f64::from(i + 1).sin().abs() * 4_294_967_296.0) as u32).collect();
|
||||
let (mut a0, mut b0, mut c0, mut d0) = (0x6745_2301u32, 0xefcd_ab89u32, 0x98ba_dcfeu32, 0x1032_5476u32);
|
||||
let mut msg = Vec::with_capacity(data.len() + 72);
|
||||
msg.extend_from_slice(data);
|
||||
msg.push(0x80);
|
||||
while msg.len() % 64 != 56 {
|
||||
msg.push(0);
|
||||
}
|
||||
msg.extend_from_slice(&(data.len() as u64).wrapping_mul(8).to_le_bytes());
|
||||
for chunk in msg.as_chunks::<64>().0 {
|
||||
let mut m = [0u32; 16];
|
||||
let words = chunk.as_chunks::<4>().0;
|
||||
for (w, b) in m.iter_mut().zip(words) {
|
||||
*w = u32::from_le_bytes(*b);
|
||||
}
|
||||
let (mut a, mut b, mut c, mut d) = (a0, b0, c0, d0);
|
||||
for i in 0..64usize {
|
||||
let (f, g) = match i / 16 {
|
||||
0 => ((b & c) | (!b & d), i),
|
||||
1 => ((d & b) | (!d & c), (5 * i + 1) % 16),
|
||||
2 => (b ^ c ^ d, (3 * i + 5) % 16),
|
||||
_ => (c ^ (b | !d), (7 * i) % 16),
|
||||
};
|
||||
let f = f.wrapping_add(a).wrapping_add(k[i]).wrapping_add(m[g]);
|
||||
a = d;
|
||||
d = c;
|
||||
c = b;
|
||||
b = b.wrapping_add(f.rotate_left(S[i]));
|
||||
}
|
||||
a0 = a0.wrapping_add(a);
|
||||
b0 = b0.wrapping_add(b);
|
||||
c0 = c0.wrapping_add(c);
|
||||
d0 = d0.wrapping_add(d);
|
||||
}
|
||||
let mut out = [0u8; 16];
|
||||
let (cells, _) = out.as_chunks_mut::<4>();
|
||||
for (o, v) in cells.iter_mut().zip([a0, b0, c0, d0]) {
|
||||
*o = v.to_le_bytes();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// SHA-1, as [MS-OFFCRYPTO] §2.3.5 specifies for the RC4 CryptoAPI key derivation.
|
||||
/// The digest is part of the file format, not a choice made here: a stronger hash would
|
||||
/// make password-protected `.doc` files written by Word unreadable.
|
||||
fn sha1(parts: &[&[u8]]) -> Vec<u8> {
|
||||
let mut h = Sha1::new(); // NOSONAR: required by the MS-OFFCRYPTO RC4 CryptoAPI format
|
||||
for p in parts {
|
||||
h.update(p);
|
||||
}
|
||||
h.finalize().to_vec()
|
||||
}
|
||||
|
||||
/// The RC4 stream cipher.
|
||||
pub(crate) struct Rc4 {
|
||||
s: [u8; 256],
|
||||
i: u8,
|
||||
j: u8,
|
||||
}
|
||||
|
||||
impl Rc4 {
|
||||
pub(crate) fn new(key: &[u8]) -> Rc4 {
|
||||
let mut s = [0u8; 256];
|
||||
for (n, v) in s.iter_mut().enumerate() {
|
||||
*v = n as u8;
|
||||
}
|
||||
let kl = key.len().max(1);
|
||||
let mut j = 0u8;
|
||||
for i in 0..256usize {
|
||||
let k = key.get(i % kl).copied().unwrap_or(0);
|
||||
j = j.wrapping_add(s[i]).wrapping_add(k);
|
||||
s.swap(i, usize::from(j));
|
||||
}
|
||||
Rc4 { s, i: 0, j: 0 }
|
||||
}
|
||||
|
||||
fn next(&mut self) -> u8 {
|
||||
self.i = self.i.wrapping_add(1);
|
||||
self.j = self.j.wrapping_add(self.s[usize::from(self.i)]);
|
||||
self.s.swap(usize::from(self.i), usize::from(self.j));
|
||||
self.s[usize::from(self.s[usize::from(self.i)].wrapping_add(self.s[usize::from(self.j)]))]
|
||||
}
|
||||
|
||||
pub(crate) fn apply(&mut self, data: &mut [u8]) {
|
||||
for b in data {
|
||||
*b ^= self.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn utf16le(password: &str) -> Vec<u8> {
|
||||
password.encode_utf16().take(MAX_PASSWORD_CHARS).flat_map(u16::to_le_bytes).collect()
|
||||
}
|
||||
|
||||
fn u16_at(d: &[u8], at: usize) -> Option<u16> {
|
||||
Some(u16::from_le_bytes(d.get(at..at.checked_add(2)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
fn u32_at(d: &[u8], at: usize) -> Option<u32> {
|
||||
Some(u32::from_le_bytes(d.get(at..at.checked_add(4)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// XOR obfuscation (method 1 key/verifier pieces, method 2 array and transform)
|
||||
|
||||
const PAD_ARRAY: [u8; 15] = [0xBB, 0xFF, 0xFF, 0xBA, 0xFF, 0xFF, 0xB9, 0x80, 0x00, 0xBE, 0x0F, 0x00, 0xBF, 0x0F, 0x00];
|
||||
const INITIAL_CODE: [u16; 15] =
|
||||
[0xE1F0, 0x1D0F, 0xCC9C, 0x84C0, 0x110C, 0x0E10, 0xF1CE, 0x313E, 0x1872, 0xE139, 0xD40F, 0x84F9, 0x280C, 0xA96A, 0x4EC3];
|
||||
const XOR_MATRIX: [u16; 105] = [
|
||||
0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09, 0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF, 0x4563, 0x8AC6, 0x05AD, 0x0B5A,
|
||||
0x16B4, 0x2D68, 0x5AD0, 0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40, 0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5, 0x6F45,
|
||||
0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A, 0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9, 0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4,
|
||||
0x7B68, 0xF6D0, 0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC, 0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10, 0xAA51, 0x4483,
|
||||
0x8906, 0x022D, 0x045A, 0x08B4, 0x1168, 0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C, 0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6,
|
||||
0x1DAD, 0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC, 0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4,
|
||||
];
|
||||
|
||||
/// `CreateXorKey_Method1`. `pw` is 1 to 15 bytes.
|
||||
fn xor_key(pw: &[u8]) -> u16 {
|
||||
let mut key = INITIAL_CODE.get(pw.len().wrapping_sub(1)).copied().unwrap_or(0);
|
||||
let mut current = 0x68i32;
|
||||
for &ch in pw.iter().rev() {
|
||||
let mut ch = ch;
|
||||
for _ in 0..7 {
|
||||
if ch & 0x40 != 0
|
||||
&& let Some(m) = usize::try_from(current).ok().and_then(|c| XOR_MATRIX.get(c))
|
||||
{
|
||||
key ^= m;
|
||||
}
|
||||
ch <<= 1;
|
||||
current -= 1;
|
||||
}
|
||||
}
|
||||
key
|
||||
}
|
||||
|
||||
/// `CreatePasswordVerifier_Method1`.
|
||||
fn verifier_method1(pw: &[u8]) -> u16 {
|
||||
let mut v = 0u16;
|
||||
for &b in pw.iter().rev().chain(std::iter::once(&(pw.len() as u8))) {
|
||||
let i1 = u16::from(v & 0x4000 != 0);
|
||||
let i2 = v.wrapping_mul(2) & 0x7FFF;
|
||||
v = (i1 | i2) ^ u16::from(b);
|
||||
}
|
||||
v ^ 0xCE4B
|
||||
}
|
||||
|
||||
/// `CreatePasswordVerifier_Method2`: the 32-bit value stored in `FibBase.lKey`.
|
||||
pub(crate) fn xor_verifier(pw: &[u8]) -> u32 {
|
||||
(u32::from(xor_key(pw)) << 16) | u32::from(verifier_method1(pw))
|
||||
}
|
||||
|
||||
/// `CreateXorArray_Method2`.
|
||||
pub(crate) fn xor_array(pw: &[u8]) -> [u8; 16] {
|
||||
let key = (xor_verifier(pw) >> 16) as u16;
|
||||
let (high, low) = ((key >> 8) as u8, key as u8);
|
||||
let mut arr = [0u8; 16];
|
||||
for (i, slot) in arr.iter_mut().enumerate() {
|
||||
*slot = pw.get(i).copied().unwrap_or_else(|| PAD_ARRAY.get(i.saturating_sub(pw.len())).copied().unwrap_or(0));
|
||||
}
|
||||
let (pairs, _) = arr.as_chunks_mut::<2>();
|
||||
for pair in pairs {
|
||||
pair[0] = (pair[0] ^ low).rotate_right(1);
|
||||
pair[1] = (pair[1] ^ high).rotate_right(1);
|
||||
}
|
||||
arr
|
||||
}
|
||||
|
||||
/// The single-byte forms of a password the XOR scheme tries: the system-codepage form (Latin-1
|
||||
/// here) and the "low byte, or high byte when the low one is zero" form. Each is cut to 15.
|
||||
fn xor_candidates(password: &str) -> Vec<Vec<u8>> {
|
||||
let mut out: Vec<Vec<u8>> = Vec::new();
|
||||
let latin: Option<Vec<u8>> = password.chars().take(MAX_XOR_PASSWORD).map(|c| u8::try_from(u32::from(c)).ok()).collect();
|
||||
if let Some(l) = latin {
|
||||
out.push(l);
|
||||
}
|
||||
let folded: Vec<u8> = password.encode_utf16().take(MAX_XOR_PASSWORD).map(|u| if u & 0xFF == 0 { (u >> 8) as u8 } else { u as u8 }).collect();
|
||||
if !out.contains(&folded) {
|
||||
out.push(folded);
|
||||
}
|
||||
out.retain(|p| !p.is_empty());
|
||||
out
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// The cipher
|
||||
|
||||
/// How the streams of one file are transformed, once the password has been verified.
|
||||
pub(crate) enum Cipher {
|
||||
Xor { array: [u8; 16] },
|
||||
Rc4 { base: Vec<u8>, crypto_api: bool, key_len: usize, table_skip: usize },
|
||||
}
|
||||
|
||||
impl Cipher {
|
||||
/// Check `password` against the file's verifier and set up the cipher. `flags` and `lkey` come
|
||||
/// from the FIB base, `table` is the (still encrypted) Table stream.
|
||||
pub(crate) fn open(flags: u16, lkey: u32, table: &[u8], password: &str) -> Result<Cipher, DocbinError> {
|
||||
if flags & F_OBFUSCATED != 0 {
|
||||
for pw in xor_candidates(password) {
|
||||
if xor_verifier(&pw) == lkey {
|
||||
return Ok(Cipher::Xor { array: xor_array(&pw) });
|
||||
}
|
||||
}
|
||||
return Err(DocbinError::WrongPassword);
|
||||
}
|
||||
let short = || DocbinError::Malformed("the encryption header is truncated".into());
|
||||
let major = u16_at(table, 0).ok_or_else(short)?;
|
||||
let minor = u16_at(table, 2).ok_or_else(short)?;
|
||||
match (major, minor) {
|
||||
(1, 1) => Self::open_rc4(table, password),
|
||||
(2..=4, 2) => Self::open_crypto_api(table, password),
|
||||
_ => Err(unsupported(&format!("unknown encryption version {major}.{minor}"))),
|
||||
}
|
||||
}
|
||||
|
||||
fn open_rc4(table: &[u8], password: &str) -> Result<Cipher, DocbinError> {
|
||||
let short = || DocbinError::Malformed("the encryption header is truncated".into());
|
||||
let salt = table.get(4..20).ok_or_else(short)?;
|
||||
let enc_verifier = table.get(20..36).ok_or_else(short)?;
|
||||
let enc_hash = table.get(36..52).ok_or_else(short)?;
|
||||
let h0 = md5(&utf16le(password));
|
||||
let mut buf = Vec::with_capacity(336);
|
||||
for _ in 0..16 {
|
||||
buf.extend_from_slice(&h0[..5]);
|
||||
buf.extend_from_slice(salt);
|
||||
}
|
||||
let base = md5(&buf)[..5].to_vec();
|
||||
let cipher = Cipher::Rc4 { base, crypto_api: false, key_len: 16, table_skip: 52 };
|
||||
let mut rc4 = Rc4::new(&cipher.block_key(0));
|
||||
let mut verifier = [0u8; 16];
|
||||
verifier.copy_from_slice(enc_verifier);
|
||||
let mut hash = [0u8; 16];
|
||||
hash.copy_from_slice(enc_hash);
|
||||
rc4.apply(&mut verifier);
|
||||
rc4.apply(&mut hash);
|
||||
if md5(&verifier) != hash {
|
||||
return Err(DocbinError::WrongPassword);
|
||||
}
|
||||
Ok(cipher)
|
||||
}
|
||||
|
||||
fn open_crypto_api(table: &[u8], password: &str) -> Result<Cipher, DocbinError> {
|
||||
let short = || DocbinError::Malformed("the encryption header is truncated".into());
|
||||
let hflags = u32_at(table, 4).ok_or_else(short)?;
|
||||
if hflags & H_EXTERNAL != 0 {
|
||||
return Err(unsupported("the document uses an external encryption provider"));
|
||||
}
|
||||
// [MS-OFFCRYPTO] defines AES only for the encrypted packages of Word 2007 and later
|
||||
// (which open through the .docx reader whatever their extension); a Word 97-2003 file
|
||||
// may only use XOR, RC4 or RC4 CryptoAPI, so there is no public scheme to follow here.
|
||||
if hflags & H_AES != 0 {
|
||||
return Err(unsupported(
|
||||
"this Word 97-2003 file claims AES encryption, which is only defined for the newer encrypted-package format; it can't be opened",
|
||||
));
|
||||
}
|
||||
if hflags & H_CRYPTO_API == 0 {
|
||||
return Err(unsupported("unknown encryption header"));
|
||||
}
|
||||
let hsize = usize::try_from(u32_at(table, 8).ok_or_else(short)?).unwrap_or(usize::MAX);
|
||||
// EncryptionHeader: flags, sizeExtra, algId, algIdHash, keySize, providerType, 2 reserved
|
||||
// words and the CSP name; it starts at byte 12.
|
||||
if hsize < 32 || hsize > table.len() {
|
||||
return Err(DocbinError::Malformed("the encryption header has a bad size".into()));
|
||||
}
|
||||
let alg_id = u32_at(table, 20).ok_or_else(short)?;
|
||||
let alg_hash = u32_at(table, 24).ok_or_else(short)?;
|
||||
if !matches!(alg_id, 0 | 0x6801) {
|
||||
return Err(unsupported(&format!("encryption algorithm 0x{alg_id:X}")));
|
||||
}
|
||||
if !matches!(alg_hash, 0 | 0x8004) {
|
||||
return Err(unsupported(&format!("hash algorithm 0x{alg_hash:X}")));
|
||||
}
|
||||
let mut bits = u32_at(table, 28).ok_or_else(short)?;
|
||||
if bits == 0 {
|
||||
bits = 40;
|
||||
}
|
||||
if !(40..=128).contains(&bits) || bits % 8 != 0 {
|
||||
return Err(unsupported(&format!("a {bits}-bit key")));
|
||||
}
|
||||
let key_len = (bits / 8) as usize;
|
||||
// EncryptionVerifier: saltSize, salt[16], encryptedVerifier[16], verifierHashSize,
|
||||
// encryptedVerifierHash[20].
|
||||
let v = 12 + hsize;
|
||||
if u32_at(table, v) != Some(16) {
|
||||
return Err(DocbinError::Malformed("the encryption salt has a bad size".into()));
|
||||
}
|
||||
let salt = table.get(v + 4..v + 20).ok_or_else(short)?;
|
||||
let enc_verifier = table.get(v + 20..v + 36).ok_or_else(short)?;
|
||||
if u32_at(table, v + 36) != Some(20) {
|
||||
return Err(unsupported("a verifier hash that is not SHA-1"));
|
||||
}
|
||||
let enc_hash = table.get(v + 40..v + 60).ok_or_else(short)?;
|
||||
let mut pw = utf16le(password);
|
||||
let base = sha1(&[salt, &pw]);
|
||||
pw.fill(0);
|
||||
let cipher = Cipher::Rc4 { base, crypto_api: true, key_len, table_skip: v + 60 };
|
||||
let mut rc4 = Rc4::new(&cipher.block_key(0));
|
||||
let mut verifier = enc_verifier.to_vec();
|
||||
let mut hash = enc_hash.to_vec();
|
||||
rc4.apply(&mut verifier);
|
||||
rc4.apply(&mut hash);
|
||||
if sha1(&[&verifier]) != hash {
|
||||
return Err(DocbinError::WrongPassword);
|
||||
}
|
||||
Ok(cipher)
|
||||
}
|
||||
|
||||
/// The RC4 key of stream block `block`.
|
||||
fn block_key(&self, block: u32) -> Vec<u8> {
|
||||
match self {
|
||||
Cipher::Xor { .. } => Vec::new(),
|
||||
Cipher::Rc4 { base, crypto_api: false, .. } => md5(&[base.as_slice(), &block.to_le_bytes()].concat()).to_vec(),
|
||||
Cipher::Rc4 { base, crypto_api: true, key_len, .. } => {
|
||||
let mut k = sha1(&[base, &block.to_le_bytes()]);
|
||||
k.truncate(*key_len);
|
||||
// A 40-bit key is zero-padded to 128 bits.
|
||||
if *key_len == 5 {
|
||||
k.resize(16, 0);
|
||||
}
|
||||
k
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypt `data` in place; its first `skip` bytes are stored untransformed.
|
||||
fn decrypt(&self, data: &mut [u8], skip: usize) {
|
||||
match self {
|
||||
Cipher::Xor { array } => {
|
||||
for (i, b) in data.iter_mut().enumerate().skip(skip) {
|
||||
let a = array[i % 16];
|
||||
// Zero bytes, and bytes equal to the array element, were left as they were.
|
||||
if *b != 0 && *b != a {
|
||||
*b ^= a;
|
||||
}
|
||||
}
|
||||
}
|
||||
Cipher::Rc4 { .. } => {
|
||||
for (n, chunk) in data.chunks_mut(BLOCK).enumerate() {
|
||||
let Ok(block) = u32::try_from(n) else { return };
|
||||
let mut rc4 = Rc4::new(&self.block_key(block));
|
||||
let start = n * BLOCK;
|
||||
// The keystream runs over the untransformed bytes too.
|
||||
let plain = skip.saturating_sub(start).min(chunk.len());
|
||||
let head = chunk.get(..plain).map(<[u8]>::to_vec).unwrap_or_default();
|
||||
rc4.apply(chunk);
|
||||
if let Some(dst) = chunk.get_mut(..plain) {
|
||||
dst.copy_from_slice(&head);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn decrypt_word(&self, data: &mut [u8]) {
|
||||
self.decrypt(data, FIB_PLAIN);
|
||||
}
|
||||
|
||||
pub(crate) fn decrypt_table(&self, data: &mut [u8]) {
|
||||
let skip = match self {
|
||||
Cipher::Xor { .. } => 0,
|
||||
Cipher::Rc4 { table_skip, .. } => *table_skip,
|
||||
};
|
||||
self.decrypt(data, skip);
|
||||
}
|
||||
|
||||
pub(crate) fn decrypt_data(&self, data: &mut [u8]) {
|
||||
self.decrypt(data, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hex(b: &[u8]) -> String {
|
||||
b.iter().map(|x| format!("{x:02x}")).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn md5_matches_the_rfc_1321_vectors() {
|
||||
assert_eq!(hex(&md5(b"")), "d41d8cd98f00b204e9800998ecf8427e");
|
||||
assert_eq!(hex(&md5(b"a")), "0cc175b9c0f1b6a831c399e269772661");
|
||||
assert_eq!(hex(&md5(b"abc")), "900150983cd24fb0d6963f7d28e17f72");
|
||||
assert_eq!(hex(&md5(b"message digest")), "f96b697d7cb7938d525a2f31aaf161d0");
|
||||
assert_eq!(
|
||||
hex(&md5(b"12345678901234567890123456789012345678901234567890123456789012345678901234567890")),
|
||||
"57edf4a22be3c955ac49da2e2107b67a"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rc4_matches_a_published_vector() {
|
||||
let mut d = b"Plaintext".to_vec();
|
||||
Rc4::new(b"Key").apply(&mut d);
|
||||
assert_eq!(hex(&d), "bbf316e8d940af0ad3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xor_candidates_cut_to_fifteen_and_skip_empty() {
|
||||
assert!(xor_candidates("").is_empty());
|
||||
assert_eq!(xor_candidates("abcdefghijklmnopqrst")[0].len(), 15);
|
||||
// Not representable as single bytes: only the folded form remains.
|
||||
assert_eq!(xor_candidates("\u{4e2d}").len(), 1);
|
||||
}
|
||||
}
|
||||
@@ -1,279 +0,0 @@
|
||||
//! Floating shapes and pictures: the `PlcfSpa` anchors of the main document and the headers
|
||||
//! ([MS-DOC] §2.8.27 PlcfSpa, §2.9.253 Spa) joined with the OfficeArt shapes they name, and the
|
||||
//! text box stories (`PlcftxbxTxt`, §2.8.30) that fill text boxes.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use wordcraft_doc::fill::Fill;
|
||||
use wordcraft_doc::para::{Anchor, Float, FloatAlign, GroupChild, InlineObject, ShapeKind, Wrap};
|
||||
use wordcraft_doc::props::Rgb;
|
||||
|
||||
use crate::media::Pictures;
|
||||
use crate::officeart::{Node, Sp};
|
||||
|
||||
/// Most anchors (and text box stories) read per PLC.
|
||||
const MAX_ANCHORS: usize = 10_000;
|
||||
/// `Spa` and `FTXBXS` record sizes.
|
||||
const CB_SPA: usize = 26;
|
||||
const CB_FTXBXS: usize = 22;
|
||||
/// Largest frame or offset kept (points; 22 inches, Word's own limit for sizes).
|
||||
const MAX_PT: f32 = 1584.0;
|
||||
/// Deepest group nesting flattened.
|
||||
const MAX_GROUP_DEPTH: usize = 8;
|
||||
|
||||
/// The (CP, data) entries of a PLC with `cb`-byte data elements, the trailing CP dropped.
|
||||
fn plc(table: &[u8], (fc, lcb): (u32, u32), cb: usize) -> Vec<(u32, &[u8])> {
|
||||
let Some(blob) = table.get(fc as usize..(fc as usize).saturating_add(lcb as usize)) else { return Vec::new() };
|
||||
let n = blob.len().saturating_sub(4) / (4 + cb);
|
||||
let n = n.min(MAX_ANCHORS);
|
||||
let data_at = (n + 1) * 4;
|
||||
(0..n)
|
||||
.filter_map(|i| {
|
||||
let cp = u32::from_le_bytes(blob.get(i * 4..i * 4 + 4)?.try_into().ok()?);
|
||||
let at = data_at + i * cb;
|
||||
Some((cp, blob.get(at..at + cb)?))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The text box stories of a `PlcftxbxTxt` (or `PlcfHdrtxbxTxt`): (shape spid, story CP range
|
||||
/// relative to the text box subdocument).
|
||||
///
|
||||
/// The CPs of a well-formed PLC ascend, so the stories are disjoint. A story starting before
|
||||
/// the end of the last one kept (a hostile table pointing many text boxes at the same text,
|
||||
/// each read into its own part) is dropped: every subdocument CP becomes at most one story.
|
||||
pub(crate) fn textbox_stories(table: &[u8], pair: Option<(u32, u32)>) -> Vec<(u32, (u32, u32))> {
|
||||
let Some(pair) = pair else { return Vec::new() };
|
||||
let Some(blob) = table.get(pair.0 as usize..(pair.0 as usize).saturating_add(pair.1 as usize)) else { return Vec::new() };
|
||||
let mut out = Vec::new();
|
||||
let mut kept_to = 0u32;
|
||||
for (i, (cp, ftxbxs)) in plc(table, pair, CB_FTXBXS).iter().enumerate() {
|
||||
// The story runs to the next entry's CP (the PLC's last CP for the last entry).
|
||||
let Some(end) = blob.get((i + 1) * 4..(i + 1) * 4 + 4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes) else { continue };
|
||||
let Some(lid) = ftxbxs.get(14..18).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes) else { continue };
|
||||
if end > *cp && *cp >= kept_to && lid != 0 {
|
||||
out.push((lid, (*cp, end)));
|
||||
kept_to = end;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// What building the floating objects needs.
|
||||
pub(crate) struct Build<'a> {
|
||||
/// The BLIP store and the stream holding BLIPs not embedded in it (WordDocument).
|
||||
pub(crate) bstore: &'a [&'a [u8]],
|
||||
pub(crate) delay: &'a [u8],
|
||||
/// Text box part ids by shape spid.
|
||||
pub(crate) stories: &'a HashMap<u32, u32>,
|
||||
}
|
||||
|
||||
/// The floating objects anchored by `PlcfSpa` (at `pair`), as (absolute anchor CP, object),
|
||||
/// sorted by CP. `cp_base` turns the PLC's CPs into absolute ones (the header subdocument's
|
||||
/// start for `PlcSpaHdr`).
|
||||
pub(crate) fn anchors(
|
||||
table: &[u8],
|
||||
pair: Option<(u32, u32)>,
|
||||
cp_base: u32,
|
||||
shapes: &HashMap<u32, Node>,
|
||||
b: &Build,
|
||||
pics: &mut Pictures,
|
||||
) -> Vec<(u32, InlineObject)> {
|
||||
let Some(pair) = pair else { return Vec::new() };
|
||||
let mut out = Vec::new();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for (cp, spa) in plc(table, pair, CB_SPA) {
|
||||
let i32_at = |o: usize| spa.get(o..o + 4).and_then(|b| b.try_into().ok()).map(i32::from_le_bytes).unwrap_or(0);
|
||||
let spid = i32_at(0) as u32;
|
||||
let [xl, yt, xr, yb] = [4, 8, 12, 16].map(i32_at);
|
||||
let flags = spa.get(20..22).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0);
|
||||
let Some(node) = shapes.get(&spid) else { continue };
|
||||
// Word anchors each shape once. A repeated spid would build another copy of the shape,
|
||||
// and of every member of a group, per anchor (#3710).
|
||||
if !seen.insert(spid) {
|
||||
continue;
|
||||
}
|
||||
let tw = |v: i64| (v as f32 / 20.0).clamp(-2.0 * MAX_PT, 2.0 * MAX_PT);
|
||||
let (mut x, mut y) = (tw(xl as i64), tw(yt as i64));
|
||||
let (mut w, mut h) = (tw(xr as i64 - xl as i64).clamp(0.0, MAX_PT), tw(yb as i64 - yt as i64).clamp(0.0, MAX_PT));
|
||||
let sp = node.sp();
|
||||
let rot = wordcraft_geom::normalize_degrees(if sp.props.rot.is_finite() { sp.props.rot } else { 0.0 });
|
||||
// A shape turned by 45°–135° (or 225°–315°) is anchored by its turned bounds: the
|
||||
// unrotated frame has the other aspect, about the same centre.
|
||||
if (45.0..135.0).contains(&rot) || (225.0..315.0).contains(&rot) {
|
||||
x += (w - h) / 2.0;
|
||||
y += (h - w) / 2.0;
|
||||
std::mem::swap(&mut w, &mut h);
|
||||
}
|
||||
let mut float = spa_float(flags, &sp.props);
|
||||
float.x = x;
|
||||
float.y = y;
|
||||
float.rot = rot;
|
||||
float.flip_h = sp.flip_h();
|
||||
float.flip_v = sp.flip_v();
|
||||
let obj = match node {
|
||||
Node::Shape(sp) => leaf(sp, w, h, float, b, pics),
|
||||
Node::Group(sp, members) => group(sp, members, w, h, float, b, pics),
|
||||
};
|
||||
match obj {
|
||||
Some(obj) => out.push((cp_base.saturating_add(cp), obj)),
|
||||
None => pics.dropped(cp_base.saturating_add(cp)),
|
||||
}
|
||||
}
|
||||
out.sort_by_key(|(cp, _)| *cp);
|
||||
out
|
||||
}
|
||||
|
||||
/// Placement from a `Spa`'s flags (fHdr, bx, by, wr, wrk, fRcaSimple, fBelowText, fAnchorLock)
|
||||
/// and the shape's position properties, which take precedence when present.
|
||||
fn spa_float(flags: u16, p: &crate::officeart::Props) -> Float {
|
||||
let bx = (flags >> 1) & 3;
|
||||
let by = (flags >> 3) & 3;
|
||||
let wr = (flags >> 5) & 0xF;
|
||||
let below = flags & (1 << 14) != 0;
|
||||
let wrap = match wr {
|
||||
1 => Wrap::TopAndBottom,
|
||||
3 => {
|
||||
if below {
|
||||
Wrap::BehindText
|
||||
} else {
|
||||
Wrap::InFrontOfText
|
||||
}
|
||||
}
|
||||
4 => Wrap::Tight,
|
||||
5 => Wrap::Through,
|
||||
_ => Wrap::Square,
|
||||
};
|
||||
let h_rel = match p.posrelh.unwrap_or(bx as u32) {
|
||||
0 => Anchor::Margin,
|
||||
1 => Anchor::Page,
|
||||
3 => Anchor::Character,
|
||||
_ => Anchor::Column,
|
||||
};
|
||||
let v_rel = match p.posrelv.unwrap_or(by as u32) {
|
||||
0 => Anchor::Margin,
|
||||
1 => Anchor::Page,
|
||||
3 => Anchor::Line,
|
||||
_ => Anchor::Paragraph,
|
||||
};
|
||||
let align = |v: Option<u32>| match v {
|
||||
Some(1) => Some(FloatAlign::Start),
|
||||
Some(2) => Some(FloatAlign::Center),
|
||||
Some(3) => Some(FloatAlign::End),
|
||||
Some(4) => Some(FloatAlign::Inside),
|
||||
Some(5) => Some(FloatAlign::Outside),
|
||||
_ => None,
|
||||
};
|
||||
// Distances from text, EMUs; Word's defaults are 0.125" at the sides, none above and below.
|
||||
let emu = |v: Option<u32>, def: f32| v.map_or(def, |e| (e as f32 / 12_700.0).clamp(0.0, MAX_PT));
|
||||
let [dl, dt, dr, db] = p.wrap_dist;
|
||||
Float {
|
||||
wrap,
|
||||
h_rel,
|
||||
v_rel,
|
||||
h_align: align(p.posh),
|
||||
v_align: align(p.posv),
|
||||
dist: emu(dl, 9.0).max(emu(dr, 9.0)),
|
||||
dist_top: emu(dt, 0.0),
|
||||
dist_bottom: emu(db, 0.0),
|
||||
..Float::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// One shape as a picture or a drawn shape, `w` × `h` points.
|
||||
fn leaf(sp: &Sp, w: f32, h: f32, float: Float, b: &Build, pics: &mut Pictures) -> Option<InlineObject> {
|
||||
let p = &sp.props;
|
||||
if let Some(pib) = p.pib {
|
||||
let media = pics.bse(pib, b.bstore, b.delay)?;
|
||||
return Some(InlineObject::Image { media, w, h, alt: p.alt.clone(), float, crop: p.crop, fx: Default::default(), ole: None });
|
||||
}
|
||||
let story = b.stories.get(&sp.spid).copied().filter(|_| sp.textbox || sp.spt == 202);
|
||||
let mut kind = match sp.spt {
|
||||
1 => ShapeKind::Rectangle,
|
||||
2 => ShapeKind::RoundedRectangle,
|
||||
3 => ShapeKind::Ellipse,
|
||||
4 => ShapeKind::Diamond,
|
||||
5 => ShapeKind::Triangle,
|
||||
6 => ShapeKind::RightTriangle,
|
||||
12 | 187 => ShapeKind::Star,
|
||||
13 | 69 => ShapeKind::Arrow,
|
||||
66 => ShapeKind::ArrowLeft,
|
||||
67 => ShapeKind::ArrowDown,
|
||||
68 => ShapeKind::ArrowUp,
|
||||
20 | 32 => ShapeKind::Line,
|
||||
74 => ShapeKind::Heart,
|
||||
202 => ShapeKind::TextBox,
|
||||
// A freeform's path is not read: better left out than drawn as a box.
|
||||
0 => return None,
|
||||
_ => ShapeKind::Rectangle,
|
||||
};
|
||||
if story.is_some() && kind == ShapeKind::Rectangle {
|
||||
kind = ShapeKind::TextBox;
|
||||
}
|
||||
let line = kind == ShapeKind::Line;
|
||||
let fill = if line || p.filled == Some(false) { None } else { Some(Fill::solid(p.fill_color.unwrap_or(Rgb::WHITE))) };
|
||||
let stroke = if p.line == Some(false) { None } else { Some(p.line_color.unwrap_or(Rgb::BLACK)) };
|
||||
// Line widths are EMUs; Word's default is 0.75 pt.
|
||||
let stroke_width = p.line_width.map_or(0.75, |e| (e as f32 / 12_700.0).clamp(0.0, 100.0));
|
||||
Some(InlineObject::Shape {
|
||||
kind,
|
||||
w,
|
||||
h,
|
||||
fill,
|
||||
stroke,
|
||||
stroke_width,
|
||||
float,
|
||||
story,
|
||||
freeform: None,
|
||||
effects: Default::default(),
|
||||
extra: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
/// A group: its members flattened into the group's own coordinate space (nested groups mapped
|
||||
/// through their child anchors).
|
||||
fn group(sp: &Sp, members: &[Node], w: f32, h: f32, float: Float, b: &Build, pics: &mut Pictures) -> Option<InlineObject> {
|
||||
let [l, t, r, btm] = sp.group_space.unwrap_or([0, 0, 0, 0]);
|
||||
let (cw, ch) = ((r as f64 - l as f64) as f32, (btm as f64 - t as f64) as f32);
|
||||
let (ch_w, ch_h) = if cw > 0.0 && ch > 0.0 { (cw, ch) } else { (w.max(1.0), h.max(1.0)) };
|
||||
let mut children = Vec::new();
|
||||
// Map: outer = x0 + inner × s; the top group's space starts at its FSPGR origin.
|
||||
members_into(members, (-(l as f32), -(t as f32), 1.0, 1.0), 0, b, pics, &mut children);
|
||||
if children.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(InlineObject::Group { w, h, float, ch_w, ch_h, children, canvas: None })
|
||||
}
|
||||
|
||||
fn members_into(nodes: &[Node], t: (f32, f32, f32, f32), depth: usize, b: &Build, pics: &mut Pictures, out: &mut Vec<GroupChild>) {
|
||||
let (x0, y0, sx, sy) = t;
|
||||
let fit = |v: f32| if v.is_finite() { v.clamp(-1e7, 1e7) } else { 0.0 };
|
||||
for n in nodes {
|
||||
if out.len() >= wordcraft_doc::para::MAX_GROUP_CHILDREN {
|
||||
return;
|
||||
}
|
||||
let sp = n.sp();
|
||||
let Some([l, t, r, btm]) = sp.child else { continue };
|
||||
let (ox, oy) = (l as f32, t as f32);
|
||||
let (ew, eh) = ((r as f64 - l as f64) as f32, (btm as f64 - t as f64) as f32);
|
||||
match n {
|
||||
Node::Group(_, members) => {
|
||||
if depth >= MAX_GROUP_DEPTH {
|
||||
continue;
|
||||
}
|
||||
let [gl, gt, gr, gb] = sp.group_space.unwrap_or([l, t, r, btm]);
|
||||
let (gw, gh) = ((gr as f64 - gl as f64) as f32, (gb as f64 - gt as f64) as f32);
|
||||
let (kx, ky) = if gw > 0.0 && gh > 0.0 { (ew / gw, eh / gh) } else { (1.0, 1.0) };
|
||||
let nt = (fit(x0 + (ox - gl as f32 * kx) * sx), fit(y0 + (oy - gt as f32 * ky) * sy), fit(sx * kx), fit(sy * ky));
|
||||
members_into(members, nt, depth + 1, b, pics, out);
|
||||
}
|
||||
Node::Shape(sp) => {
|
||||
let float = Float { rot: sp.props.rot, flip_h: sp.flip_h(), flip_v: sp.flip_v(), ..Float::default() };
|
||||
let (w, h) = (fit(ew * sx).max(0.0), fit(eh * sy).max(0.0));
|
||||
if let Some(obj) = leaf(sp, w, h, float, b, pics) {
|
||||
out.push(GroupChild { x: fit(x0 + ox * sx), y: fit(y0 + oy * sy), obj });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
//! The File Information Block ([MS-DOC] §2.5): `FibBase` plus the variable sections
|
||||
//! (`FibRgW97`, `FibRgLw97`, the `RgFcLcb` blob, `FibRgCswNew`) at the start of the
|
||||
//! WordDocument stream.
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
/// `wIdent` of every Word 97-2003 file (Word 6/95 wrote 0xA5DC).
|
||||
const W_IDENT: u16 = 0xA5EC;
|
||||
/// Offsets within the FIB, in bytes (see the spec's Fib layout).
|
||||
const OFF_FLAGS: usize = 0x0A;
|
||||
const OFF_CSW: usize = 0x20;
|
||||
const OFF_CSLW: usize = 0x3E;
|
||||
const OFF_RGLW: usize = 0x40;
|
||||
const OFF_CB_RG_FCLCB: usize = 0x98;
|
||||
/// `csw` (count of 16-bit values in `FibRgW97`) — fixed by the spec.
|
||||
const CSW: u16 = 0x000E;
|
||||
/// `cslw` (count of 32-bit values in `FibRgLw97`) — fixed by the spec.
|
||||
const CSLW: u16 = 0x0016;
|
||||
/// `cbRgFcLcb` for each known `nFib` ([MS-DOC] Fib.cbRgFcLcb table).
|
||||
const CB_RG_FCLCB: &[(u16, u16)] = &[(0x00C1, 0x005D), (0x00D9, 0x006C), (0x0101, 0x0088), (0x010C, 0x00A4), (0x0112, 0x00B7)];
|
||||
|
||||
/// Indices of `fc`/`lcb` pairs inside `FibRgFcLcb97` (each pair is 8 bytes: fc then lcb).
|
||||
pub(crate) mod pair {
|
||||
pub(crate) const STSHF: usize = 1;
|
||||
pub(crate) const PLCF_FND_REF: usize = 2;
|
||||
pub(crate) const PLCF_FND_TXT: usize = 3;
|
||||
pub(crate) const PLCF_SED: usize = 6;
|
||||
pub(crate) const PLCF_HDD: usize = 11;
|
||||
pub(crate) const PLCF_BTE_CHPX: usize = 12;
|
||||
pub(crate) const PLCF_BTE_PAPX: usize = 13;
|
||||
pub(crate) const STTBF_FFN: usize = 15;
|
||||
pub(crate) const STTBF_BKMK: usize = 21;
|
||||
pub(crate) const PLCF_BKF: usize = 22;
|
||||
pub(crate) const PLCF_BKL: usize = 23;
|
||||
pub(crate) const CLX: usize = 33;
|
||||
/// `fcPlcSpaMom` / `fcPlcSpaHdr`: floating shape anchors of the main document / headers.
|
||||
pub(crate) const PLC_SPA_MOM: usize = 40;
|
||||
pub(crate) const PLC_SPA_HDR: usize = 41;
|
||||
pub(crate) const PLCF_END_REF: usize = 46;
|
||||
pub(crate) const PLCF_END_TXT: usize = 47;
|
||||
/// `fcDggInfo`: the OfficeArt drawing content (BLIP store, shapes).
|
||||
pub(crate) const DGG_INFO: usize = 50;
|
||||
/// `fcPlcftxbxTxt` / `fcPlcfHdrtxbxTxt`: text box stories of the main document / headers.
|
||||
pub(crate) const PLCF_TXBX_TXT: usize = 56;
|
||||
pub(crate) const PLCF_HDR_TXBX_TXT: usize = 58;
|
||||
/// `fcPlfLst`: the list definitions (LSTF array, with the LVLs appended after the blob).
|
||||
pub(crate) const PLF_LST: usize = 73;
|
||||
/// `fcPlfLfo`: the list format overrides (LFO array with LFOData appended).
|
||||
pub(crate) const PLF_LFO: usize = 74;
|
||||
}
|
||||
|
||||
/// Character-position ranges of the subdocuments, from `FibRgLw97`.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub(crate) struct SubDocs {
|
||||
pub(crate) text: u32,
|
||||
pub(crate) ftn: u32,
|
||||
pub(crate) hdd: u32,
|
||||
pub(crate) atn: u32,
|
||||
pub(crate) edn: u32,
|
||||
pub(crate) txbx: u32,
|
||||
pub(crate) hdr_txbx: u32,
|
||||
}
|
||||
|
||||
impl SubDocs {
|
||||
/// Total CPs across all subdocuments, checked against a cap.
|
||||
pub(crate) fn total(&self) -> Result<u64, DocbinError> {
|
||||
let t = u64::from(self.text)
|
||||
+ u64::from(self.ftn)
|
||||
+ u64::from(self.hdd)
|
||||
+ u64::from(self.atn)
|
||||
+ u64::from(self.edn)
|
||||
+ u64::from(self.txbx)
|
||||
+ u64::from(self.hdr_txbx);
|
||||
// Word's own documents stay far below this; a larger total means a corrupt FIB
|
||||
// (the ccp fields are signed and "MUST be 0, 1, or greater").
|
||||
if t > 100_000_000 {
|
||||
return Err(DocbinError::Limit(format!("document claims {t} characters")));
|
||||
}
|
||||
Ok(t)
|
||||
}
|
||||
}
|
||||
|
||||
/// What the unencrypted head of a password-protected file says (see [`peek_encryption`]).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct Protection {
|
||||
pub(crate) flags: u16,
|
||||
/// `FibBase.lKey`: the XOR verifier, or the size of the RC4 header in the Table stream.
|
||||
pub(crate) lkey: u32,
|
||||
pub(crate) which_tbl_stm: bool,
|
||||
}
|
||||
|
||||
/// Is the WordDocument stream that of an encrypted Word 97-2003 file? Only the first 68 bytes are
|
||||
/// stored in the clear; this reads nothing else. `None` for anything else, including streams that
|
||||
/// are too short or have a foreign magic (the full parse reports those).
|
||||
pub(crate) fn peek_encryption(word: &[u8]) -> Option<Protection> {
|
||||
if word.len() < crate::crypt::FIB_PLAIN || word.get(0..2)? != W_IDENT.to_le_bytes() {
|
||||
return None;
|
||||
}
|
||||
let flags = u16::from_le_bytes([*word.get(OFF_FLAGS)?, *word.get(OFF_FLAGS + 1)?]);
|
||||
if flags & crate::crypt::F_ENCRYPTED == 0 {
|
||||
return None;
|
||||
}
|
||||
let lkey = u32::from_le_bytes([*word.get(0x0E)?, *word.get(0x0F)?, *word.get(0x10)?, *word.get(0x11)?]);
|
||||
Some(Protection { flags, lkey, which_tbl_stm: flags & 0x0200 != 0 })
|
||||
}
|
||||
|
||||
/// The parsed FIB.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Fib {
|
||||
/// Selects 1Table (true) or 0Table (false).
|
||||
pub(crate) which_tbl_stm: bool,
|
||||
pub(crate) ccp: SubDocs,
|
||||
/// The `RgFcLcb` pairs flattened as fc₀, lcb₀, fc₁, lcb₁, …
|
||||
pub(crate) pairs: Vec<u32>,
|
||||
}
|
||||
|
||||
impl Fib {
|
||||
/// The (fc, lcb) pair at `FibRgFcLcb97` index `i` (later blobs follow contiguously).
|
||||
pub(crate) fn pair(&self, i: usize) -> Option<(u32, u32)> {
|
||||
let fc = *self.pairs.get(i * 2)?;
|
||||
let lcb = *self.pairs.get(i * 2 + 1)?;
|
||||
Some((fc, lcb))
|
||||
}
|
||||
|
||||
/// Parse the FIB from the head of the WordDocument stream.
|
||||
pub(crate) fn parse(word: &[u8]) -> Result<Fib, DocbinError> {
|
||||
let u16le = |at: usize| -> u16 { u16::from_le_bytes([word.get(at).copied().unwrap_or(0), word.get(at + 1).copied().unwrap_or(0)]) };
|
||||
let u32le = |at: usize| -> u32 {
|
||||
u32::from_le_bytes([
|
||||
word.get(at).copied().unwrap_or(0),
|
||||
word.get(at + 1).copied().unwrap_or(0),
|
||||
word.get(at + 2).copied().unwrap_or(0),
|
||||
word.get(at + 3).copied().unwrap_or(0),
|
||||
])
|
||||
};
|
||||
if word.len() < OFF_RGLW + 88 {
|
||||
return Err(DocbinError::NotWord(format!("WordDocument stream is {} bytes", word.len())));
|
||||
}
|
||||
let w_ident = u16le(0);
|
||||
if w_ident == 0xA5DC {
|
||||
return Err(DocbinError::NotWord("Word 6.0/95 format, only Word 97-2003 is supported".into()));
|
||||
}
|
||||
if w_ident != W_IDENT {
|
||||
return Err(DocbinError::NotWord(format!("magic 0x{w_ident:04X}, expected 0x{W_IDENT:04X}")));
|
||||
}
|
||||
let flags = u16le(OFF_FLAGS);
|
||||
let nfib = u16le(2);
|
||||
if u16le(OFF_CSW) != CSW {
|
||||
return Err(DocbinError::NotWord(format!("csw is 0x{:04X}", u16le(OFF_CSW))));
|
||||
}
|
||||
if u16le(OFF_CSLW) != CSLW {
|
||||
return Err(DocbinError::NotWord(format!("cslw is 0x{:04X}", u16le(OFF_CSLW))));
|
||||
}
|
||||
// rglw u32 indices per FibRgLw97: cbMac, r1, r2, ccpText, ccpFtn, ccpHdd, r3, ccpAtn,
|
||||
// ccpEdn, ccpTxbx, ccpHdrTxbx, …
|
||||
let lw = |i: usize| -> u32 { u32le(OFF_RGLW + i * 4) };
|
||||
// The ccp fields are signed in the spec; negative values are corrupt input, clamped.
|
||||
let ccp = |i: usize| -> u32 { (lw(i) as i32).max(0) as u32 };
|
||||
let sub = SubDocs { text: ccp(3), ftn: ccp(4), hdd: ccp(5), atn: ccp(7), edn: ccp(8), txbx: ccp(9), hdr_txbx: ccp(10) };
|
||||
sub.total()?;
|
||||
|
||||
let cb_rg_fclcb = u16le(OFF_CB_RG_FCLCB) as usize;
|
||||
if cb_rg_fclcb == 0 {
|
||||
return Err(DocbinError::NotWord("empty RgFcLcb".into()));
|
||||
}
|
||||
if let Some(&(_, expected)) = CB_RG_FCLCB.iter().find(|(n, _)| *n == nfib)
|
||||
&& cb_rg_fclcb < expected as usize
|
||||
{
|
||||
// Word itself always writes the full blob for its version; anything smaller is
|
||||
// corrupt, but a *larger* blob (newer writer) is fine — we just read what's there.
|
||||
return Err(DocbinError::NotWord(format!("cbRgFcLcb is 0x{cb_rg_fclcb:04X} for nFib 0x{nfib:04X}")));
|
||||
}
|
||||
let blob_off = OFF_CB_RG_FCLCB + 2;
|
||||
let blob_len = cb_rg_fclcb.saturating_mul(8);
|
||||
if word.len() < blob_off + blob_len {
|
||||
return Err(DocbinError::NotWord("truncated RgFcLcb".into()));
|
||||
}
|
||||
let mut pairs = Vec::with_capacity(cb_rg_fclcb * 2);
|
||||
for i in 0..cb_rg_fclcb * 2 {
|
||||
pairs.push(u32le(blob_off + i * 4));
|
||||
}
|
||||
|
||||
Ok(Fib { which_tbl_stm: flags & 0x0200 != 0, ccp: sub, pairs })
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
//! Character and paragraph formatting lookup: the bin tables (`PlcBteChpx`/`PlcBtePapx`) in
|
||||
//! the Table stream point at 512-byte FKP pages in the WordDocument stream, which map FC
|
||||
//! ranges to CHPX (grpprls) and PAPX (istd + grpprl) ([MS-DOC] §2.4.6.1/2.4.6.2).
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
/// Largest bin table (entries) we accept.
|
||||
const MAX_BINS: usize = 100_000;
|
||||
|
||||
/// A parsed `PlcBte*`: FC breakpoints and the FKP page each range lives in.
|
||||
pub(crate) struct Bins {
|
||||
fcs: Vec<u32>,
|
||||
/// Page number (× 512 = byte offset in the WordDocument stream).
|
||||
pns: Vec<u32>,
|
||||
}
|
||||
|
||||
impl Bins {
|
||||
/// Parse the Plc at `table[fc..fc+lcb]` (aFc[n+1] u32s + aPn[n] u32s).
|
||||
pub(crate) fn parse(table: &[u8], fc: u32, lcb: u32) -> Result<Bins, DocbinError> {
|
||||
let fc = fc as usize;
|
||||
let lcb = lcb as usize;
|
||||
if lcb < 4 || !(lcb - 4).is_multiple_of(8) {
|
||||
return Ok(Bins { fcs: Vec::new(), pns: Vec::new() });
|
||||
}
|
||||
let end = fc.checked_add(lcb).ok_or_else(|| DocbinError::Malformed("bin table overflow".into()))?;
|
||||
let plc = table.get(fc..end).ok_or_else(|| DocbinError::Malformed("bin table outside the Table stream".into()))?;
|
||||
let n = (lcb - 4) / 8;
|
||||
if n > MAX_BINS {
|
||||
return Err(DocbinError::Limit(format!("bin table has {n} entries")));
|
||||
}
|
||||
let at = |i: usize| -> Option<u32> { plc.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) };
|
||||
let mut fcs = Vec::with_capacity(n + 1);
|
||||
for i in 0..=n {
|
||||
fcs.push(at(i).ok_or_else(|| DocbinError::Malformed("truncated bin aFc".into()))?);
|
||||
}
|
||||
let mut pns = Vec::with_capacity(n);
|
||||
for i in 0..n {
|
||||
// PnBte* keeps the page number in its low bits; the rest must be zero.
|
||||
pns.push(at(n + 1 + i).ok_or_else(|| DocbinError::Malformed("truncated bin aPn".into()))? & 0x003F_FFFF);
|
||||
}
|
||||
Ok(Bins { fcs, pns })
|
||||
}
|
||||
|
||||
/// The page containing `fc`, as a slice of the WordDocument stream.
|
||||
fn page<'w>(&self, word: &'w [u8], fc: u32) -> Option<&'w [u8]> {
|
||||
let i = self.fcs.partition_point(|&f| f <= fc).checked_sub(1)?;
|
||||
let pn = *self.pns.get(i)?;
|
||||
let at = pn.checked_mul(512)? as usize;
|
||||
word.get(at..at.checked_add(512)?)
|
||||
}
|
||||
|
||||
/// The grpprl of the Chpx covering `fc` ([MS-DOC] ChpxFkp: rgfc u32s, rgb offsets ×2,
|
||||
/// crun in the last byte).
|
||||
pub(crate) fn chpx<'w>(&self, word: &'w [u8], fc: u32) -> &'w [u8] {
|
||||
let Some(page) = self.page(word, fc) else { return &[] };
|
||||
let Some(&crun) = page.get(511) else { return &[] };
|
||||
let crun = crun.min(0x65) as usize;
|
||||
let rgfc = |i: usize| -> Option<u32> { page.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) };
|
||||
// rgb array sits right after rgfc (crun+1 u32s); each byte is a half-offset into the
|
||||
// region between rgb's end and crun.
|
||||
let rgb_at = (crun + 1) * 4;
|
||||
let j = (0..crun).rev().find(|&j| rgfc(j).is_some_and(|f| f <= fc));
|
||||
let Some(j) = j else { return &[] };
|
||||
let Some(&off) = page.get(rgb_at + j) else { return &[] };
|
||||
if off == 0 {
|
||||
return &[];
|
||||
}
|
||||
let at = off as usize * 2;
|
||||
let Some(&cb) = page.get(at) else { return &[] };
|
||||
page.get(at + 1..at + 1 + cb as usize).unwrap_or(&[])
|
||||
}
|
||||
|
||||
/// The (istd, grpprl) of the Papx of the paragraph whose mark is just before `fc`
|
||||
/// ([MS-DOC] PapxFkp: rgfc u32s — each the FC *following* a paragraph mark —, rgbx BX
|
||||
/// entries (1 offset byte + 12 PHE bytes), crun in the last byte; the PAPX itself is a
|
||||
/// PapxInFkp with the cb/cb' size quirk). Query with the FC of the character after the mark.
|
||||
pub(crate) fn papx<'w>(&self, word: &'w [u8], fc: u32) -> (u16, &'w [u8]) {
|
||||
let Some(page) = self.page(word, fc) else { return (0, &[]) };
|
||||
let Some(&crun) = page.get(511) else { return (0, &[]) };
|
||||
// BX is 13 bytes; cap crun at what fits before byte 511.
|
||||
let max_run = (511usize.saturating_sub((crun as usize + 1) * 4)) / 13;
|
||||
let crun = (crun as usize).min(max_run);
|
||||
let rgfc = |i: usize| -> Option<u32> { page.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) };
|
||||
let rgbx_at = (crun + 1) * 4;
|
||||
let j = (0..crun).rev().find(|&j| rgfc(j).is_some_and(|f| f <= fc));
|
||||
let Some(j) = j else { return (0, &[]) };
|
||||
let Some(&off) = page.get(rgbx_at + j * 13) else { return (0, &[]) };
|
||||
if off == 0 {
|
||||
return (0, &[]);
|
||||
}
|
||||
let at = off as usize * 2;
|
||||
let u16at = |o: usize| -> Option<u16> { page.get(o..o + 2).map(|b| u16::from_le_bytes([b[0], b[1]])) };
|
||||
let Some(cb) = page.get(at).copied() else { return (0, &[]) };
|
||||
// PapxInFkp: if cb is nonzero the GrpPrlAndIstd (istd u16 + grpprl) is 2×cb-1 bytes;
|
||||
// if cb is zero, the next byte cb' (1 byte) gives 2×cb' bytes of it.
|
||||
let (data_at, data_len) = if cb != 0 {
|
||||
(at + 1, 2 * cb as usize - 1)
|
||||
} else {
|
||||
let cb2 = page.get(at + 1).copied().unwrap_or(0) as usize;
|
||||
(at + 2, 2 * cb2)
|
||||
};
|
||||
if data_len < 2 {
|
||||
return (0, &[]);
|
||||
}
|
||||
let istd = u16at(data_at).unwrap_or(0);
|
||||
(istd, page.get(data_at + 2..data_at + data_len).unwrap_or(&[]))
|
||||
}
|
||||
}
|
||||
@@ -1,572 +0,0 @@
|
||||
//! Sprms → model properties, and style-chain resolution.
|
||||
|
||||
use wordcraft_doc::Rgb;
|
||||
use wordcraft_doc::props::{
|
||||
Align, Border, BorderStyle, Borders, CharProps, Highlight, LineSpacing, ParaProps, TabAlign, TabLeader, TabStop, TextColor, Underline, VertAlign,
|
||||
};
|
||||
use wordcraft_doc::styles::{Style, StyleKind, StyleSheet};
|
||||
|
||||
use crate::sprm::{self, Prl, Toggle};
|
||||
|
||||
// Character sprms ([MS-DOC] §2.6.1).
|
||||
const C_BOLD: u16 = 0x0835;
|
||||
const C_ITALIC: u16 = 0x0836;
|
||||
const C_STRIKE: u16 = 0x0837;
|
||||
const C_SMALL_CAPS: u16 = 0x083A;
|
||||
const C_CAPS: u16 = 0x083B;
|
||||
const C_HIDDEN: u16 = 0x083C; // sprmCFVanish
|
||||
const C_KUL: u16 = 0x2A3E; // underline style (Kul)
|
||||
const C_ICO: u16 = 0x2A42; // palette colour
|
||||
const C_CR: u16 = 0x6870; // explicit COLORREF
|
||||
const C_HPS: u16 = 0x4A43; // half-points
|
||||
const C_HPS_POS: u16 = 0x4845; // super/subscript by half-points
|
||||
const C_FTC0: u16 = 0x4A4F; // font index (ASCII)
|
||||
const C_FTC1: u16 = 0x4A50; // East Asian font index (sprmCRgFtc1)
|
||||
const C_FTC_BI: u16 = 0x4A5E; // complex-script font index (sprmCFtcBi)
|
||||
const C_HIGHLIGHT: u16 = 0x2A0C;
|
||||
const C_ISTD: u16 = 0x4A30; // character style
|
||||
|
||||
// Paragraph sprms ([MS-DOC] §2.6.2).
|
||||
const P_JC: u16 = 0x2461;
|
||||
const P_JC80: u16 = 0x2403;
|
||||
const P_DXA_LEFT: u16 = 0x845E;
|
||||
const P_DXA_LEFT80: u16 = 0x840F;
|
||||
const P_DXA_RIGHT: u16 = 0x845D;
|
||||
const P_DXA_RIGHT80: u16 = 0x840E;
|
||||
const P_DXA_FIRST: u16 = 0x8460; // sprmPDxaLeft1
|
||||
const P_DXA_FIRST80: u16 = 0x8411; // sprmPDxaLeft1_80
|
||||
// Indents in hundredths of a character (sprmPDxcRight, sprmPDxcLeft, sprmPDxcLeft1).
|
||||
const P_DXC_RIGHT: u16 = 0x4455;
|
||||
const P_DXC_LEFT: u16 = 0x4456;
|
||||
const P_DXC_FIRST: u16 = 0x4457;
|
||||
const P_DYA_BEFORE: u16 = 0xA413;
|
||||
const P_DYA_AFTER: u16 = 0xA414;
|
||||
const P_DYA_LINE: u16 = 0x6412;
|
||||
const P_KEEP: u16 = 0x2405; // keep lines together
|
||||
const P_KEEP_FOLLOW: u16 = 0x2406; // keep with next
|
||||
const P_PAGE_BREAK_BEFORE: u16 = 0x2407;
|
||||
const P_OUTLVL: u16 = 0x2640;
|
||||
// Paragraph borders ([MS-DOC] §2.6.2). The 80 forms carry a Brc80 (palette colour), the
|
||||
// newer ones a BrcOperand: cb(1) + Brc(8, with a full COLORREF).
|
||||
const P_BRC_TOP80: u16 = 0x6424;
|
||||
const P_BRC_LEFT80: u16 = 0x6425;
|
||||
const P_BRC_BOTTOM80: u16 = 0x6426;
|
||||
const P_BRC_RIGHT80: u16 = 0x6427;
|
||||
const P_BRC_BETWEEN80: u16 = 0x6428;
|
||||
const P_BRC_TOP: u16 = 0xC64E;
|
||||
const P_BRC_LEFT: u16 = 0xC64F;
|
||||
const P_BRC_BOTTOM: u16 = 0xC650;
|
||||
const P_BRC_RIGHT: u16 = 0xC651;
|
||||
const P_BRC_BETWEEN: u16 = 0xC652;
|
||||
|
||||
/// Word's 16-colour `Ico` palette ([MS-DOC] §2.9.135), 0 = automatic.
|
||||
const ICO: [Rgb; 17] = [
|
||||
Rgb(0, 0, 0),
|
||||
Rgb(0, 0, 0),
|
||||
Rgb(0, 0, 255),
|
||||
Rgb(0, 255, 255),
|
||||
Rgb(0, 255, 0),
|
||||
Rgb(255, 0, 255),
|
||||
Rgb(255, 0, 0),
|
||||
Rgb(255, 255, 0),
|
||||
Rgb(255, 255, 255),
|
||||
Rgb(0, 0, 128),
|
||||
Rgb(0, 128, 128),
|
||||
Rgb(0, 128, 0),
|
||||
Rgb(128, 0, 128),
|
||||
Rgb(128, 0, 0),
|
||||
Rgb(128, 128, 0),
|
||||
Rgb(128, 128, 128),
|
||||
Rgb(192, 192, 192),
|
||||
];
|
||||
|
||||
/// Highlight palette ([MS-DOC] §2.9.135 Highlight, order of Word's highlighter row).
|
||||
const HL: [Highlight; 17] = [
|
||||
Highlight::None,
|
||||
Highlight::Yellow,
|
||||
Highlight::BrightGreen,
|
||||
Highlight::Turquoise,
|
||||
Highlight::Pink,
|
||||
Highlight::Blue,
|
||||
Highlight::Red,
|
||||
Highlight::DarkBlue,
|
||||
Highlight::Teal,
|
||||
Highlight::Green,
|
||||
Highlight::Violet,
|
||||
Highlight::DarkRed,
|
||||
Highlight::DarkYellow,
|
||||
Highlight::Gray50,
|
||||
Highlight::Gray25,
|
||||
Highlight::None,
|
||||
Highlight::None,
|
||||
];
|
||||
|
||||
/// The `Ico` palette colour (0 = automatic → `None`).
|
||||
pub(crate) fn ico(i: u8) -> Option<Rgb> {
|
||||
if i == 0 {
|
||||
return None;
|
||||
}
|
||||
ICO.get(i as usize).copied()
|
||||
}
|
||||
|
||||
/// The model border style a `brcType` stands for ([MS-DOC] §2.9.22 BrcType; 0x02, thick, is in
|
||||
/// the BRC of Microsoft's Word 97-2007 specification). Types the model doesn't carry (the gap
|
||||
/// variants, 3-D, inset/outset) draw as single lines, as the DOCX reader maps them.
|
||||
fn brc_style(t: u8) -> BorderStyle {
|
||||
match t {
|
||||
0x00 => BorderStyle::None,
|
||||
0x02 => BorderStyle::Thick,
|
||||
0x03 => BorderStyle::Double,
|
||||
0x06 => BorderStyle::Dotted,
|
||||
0x07 | 0x16 => BorderStyle::Dashed,
|
||||
0x08 | 0x09 => BorderStyle::DotDash,
|
||||
0x0A => BorderStyle::Triple,
|
||||
0x14 | 0x15 => BorderStyle::Wave,
|
||||
_ => BorderStyle::Single,
|
||||
}
|
||||
}
|
||||
|
||||
/// The border width in points: `dptLineWidth` in 1/8-point steps from 2 to 96, as DOCX `w:sz`
|
||||
/// reads (1-point steps for page borders, which never reach a paragraph property).
|
||||
fn brc_width(t: u8, dpt: u8) -> f32 {
|
||||
if t >= 0x40 { f32::from(dpt) } else { (f32::from(dpt.max(2)) / 8.0).min(12.0) }
|
||||
}
|
||||
|
||||
/// One border from a Brc80 operand ([MS-DOC] §2.9.18). All bits set means "no border"; a
|
||||
/// truncated operand is ignored.
|
||||
pub(crate) fn brc80(op: &[u8]) -> Option<Border> {
|
||||
let [dpt, ty, ico_index, tail, ..] = op else { return None };
|
||||
if [*dpt, *ty, *ico_index, *tail] == [0xFF; 4] {
|
||||
return Some(Border::default());
|
||||
}
|
||||
Some(Border { style: brc_style(*ty), width: brc_width(*ty, *dpt), color: ico(*ico_index), space: f32::from(tail & 0x1F) })
|
||||
}
|
||||
|
||||
/// One border from a `BrcOperand` ([MS-DOC] §2.9.21): cb(1, MUST be 8) + Brc(8).
|
||||
fn brc(op: &[u8]) -> Option<Border> {
|
||||
if op.first() != Some(&8) {
|
||||
return None;
|
||||
}
|
||||
let b = op.get(1..9)?;
|
||||
if b.iter().all(|x| *x == 0xFF) {
|
||||
return Some(Border::default());
|
||||
}
|
||||
let color = match b.get(..4) {
|
||||
Some([0, 0, 0, 0]) => None, // automatic
|
||||
Some(px) => Some(Rgb(px[0], px[1], px[2])),
|
||||
None => None,
|
||||
};
|
||||
Some(Border { style: brc_style(b[5]), width: brc_width(b[5], b[4]), color, space: f32::from(b[6] & 0x1F) })
|
||||
}
|
||||
|
||||
/// Put a decoded border on the side its sprm names (`between` = the border between this
|
||||
/// paragraph and the next).
|
||||
fn set_border(p: &mut ParaProps, op: u16, b: Border) {
|
||||
let borders = p.borders.get_or_insert_with(Borders::default);
|
||||
let slot = match op {
|
||||
P_BRC_TOP80 | P_BRC_TOP => &mut borders.top,
|
||||
P_BRC_LEFT80 | P_BRC_LEFT => &mut borders.left,
|
||||
P_BRC_BOTTOM80 | P_BRC_BOTTOM => &mut borders.bottom,
|
||||
P_BRC_RIGHT80 | P_BRC_RIGHT => &mut borders.right,
|
||||
_ => &mut borders.between,
|
||||
};
|
||||
*slot = Some(b);
|
||||
}
|
||||
|
||||
fn u16_of(p: &Prl) -> Option<u16> {
|
||||
match p.operand {
|
||||
[b0, b1, ..] => Some(u16::from_le_bytes([*b0, *b1])),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn s16_of(p: &Prl) -> Option<i16> {
|
||||
u16_of(p).map(|v| v as i16)
|
||||
}
|
||||
|
||||
fn set(props: &mut Option<bool>, t: Toggle, base: bool) {
|
||||
*props = Some(match t {
|
||||
Toggle::On => true,
|
||||
Toggle::Off => false,
|
||||
Toggle::Invert => !base,
|
||||
Toggle::Keep => return,
|
||||
});
|
||||
}
|
||||
|
||||
/// Apply a character-property sprm. `base` carries the style-inherited values so toggle
|
||||
/// operands can invert them; `fonts` maps sprmCRgFtc0 indices to names.
|
||||
pub(crate) fn apply_char(p: &mut CharProps, prl: &Prl, base: &CharProps, fonts: &[String]) {
|
||||
let op = prl.op;
|
||||
match op {
|
||||
C_BOLD => set(&mut p.bold, sprm::toggle(prl), base.bold.unwrap_or(false)),
|
||||
C_ITALIC => set(&mut p.italic, sprm::toggle(prl), base.italic.unwrap_or(false)),
|
||||
C_STRIKE => set(&mut p.strike, sprm::toggle(prl), base.strike.unwrap_or(false)),
|
||||
C_SMALL_CAPS => set(&mut p.small_caps, sprm::toggle(prl), base.small_caps.unwrap_or(false)),
|
||||
C_CAPS => set(&mut p.caps, sprm::toggle(prl), base.caps.unwrap_or(false)),
|
||||
C_HIDDEN => set(&mut p.hidden, sprm::toggle(prl), base.hidden.unwrap_or(false)),
|
||||
C_KUL => {
|
||||
p.underline = match prl.operand.first().copied().unwrap_or(0) {
|
||||
0x00 => Some(Underline::None),
|
||||
0x01 => Some(Underline::Single),
|
||||
0x02 => Some(Underline::Words),
|
||||
0x03 => Some(Underline::Double),
|
||||
0x04 => Some(Underline::Dotted),
|
||||
0x06 => Some(Underline::Thick),
|
||||
0x07 => Some(Underline::Dash),
|
||||
0x09 => Some(Underline::DotDash),
|
||||
0x0A => Some(Underline::DotDotDash),
|
||||
0x0B => Some(Underline::Wave),
|
||||
0x14 => Some(Underline::Dotted),
|
||||
0x17 => Some(Underline::Thick),
|
||||
0x18 => Some(Underline::Dash),
|
||||
0x19 => Some(Underline::DotDash),
|
||||
0x1A => Some(Underline::DotDotDash),
|
||||
0x1B => Some(Underline::DoubleWave),
|
||||
_ => Some(Underline::Single),
|
||||
};
|
||||
}
|
||||
C_ICO => {
|
||||
if let Some(rgb) = ICO.get(prl.operand.first().copied().unwrap_or(0) as usize) {
|
||||
p.color = Some(if rgb == &Rgb::BLACK && prl.operand.first() == Some(&0) { TextColor::Auto } else { TextColor::Rgb(*rgb) });
|
||||
}
|
||||
}
|
||||
C_CR => {
|
||||
// COLORREF 0x00BBGGRR, stored little-endian: operand bytes are R, G, B.
|
||||
if let [r, g, b, ..] = prl.operand
|
||||
&& let (r, g, b) = (*r, *g, *b)
|
||||
{
|
||||
p.color = Some(TextColor::Rgb(Rgb(r, g, b)));
|
||||
}
|
||||
}
|
||||
C_HPS => {
|
||||
if let Some(hps) = u16_of(prl) {
|
||||
p.size = Some((hps as f32 / 2.0).clamp(1.0, 1638.0));
|
||||
}
|
||||
}
|
||||
C_HPS_POS => {
|
||||
if let Some(pos) = s16_of(prl) {
|
||||
p.vert_align = Some(match pos {
|
||||
pos if pos > 0 => VertAlign::Superscript,
|
||||
pos if pos < 0 => VertAlign::Subscript,
|
||||
_ => VertAlign::Baseline,
|
||||
});
|
||||
}
|
||||
}
|
||||
C_FTC0 => {
|
||||
if let Some(name) = u16_of(prl).and_then(|i| fonts.get(i as usize)) {
|
||||
p.font = Some(name.clone());
|
||||
}
|
||||
}
|
||||
C_FTC1 => {
|
||||
if let Some(name) = u16_of(prl).and_then(|i| fonts.get(i as usize)) {
|
||||
p.font_ea = Some(name.clone());
|
||||
}
|
||||
}
|
||||
C_FTC_BI => {
|
||||
if let Some(name) = u16_of(prl).and_then(|i| fonts.get(i as usize)) {
|
||||
p.font_cs = Some(name.clone());
|
||||
}
|
||||
}
|
||||
C_HIGHLIGHT => {
|
||||
if let Some(h) = HL.get(prl.operand.first().copied().unwrap_or(0) as usize) {
|
||||
p.highlight = Some(*h);
|
||||
}
|
||||
}
|
||||
C_ISTD => {} // resolved separately through the style table
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Custom tab stops from `sprmPChgTabsPapx` / `sprmPChgTabs` ([MS-DOC] PChgTabsPapxOperand,
|
||||
/// PChgTabsOperand): the stops removed become `Clear` stops (they hide inherited ones) and the
|
||||
/// stops added carry their alignment and leader (TBD: jc in bits 0–2, tlc in bits 3–5).
|
||||
fn change_tabs(p: &mut ParaProps, prl: &Prl) {
|
||||
let op = prl.operand;
|
||||
let i16_at = |i: usize| op.get(i..i + 2).map(|b| f32::from(i16::from_le_bytes([b[0], b[1]])) / 20.0);
|
||||
// Both operands start with cb; sprmPChgTabs's removals also carry a "close" distance each.
|
||||
let close = prl.op == sprm::P_CHG_TABS;
|
||||
let mut at = 1;
|
||||
let Some(&del) = op.get(at) else { return };
|
||||
let del = usize::from(del.min(64));
|
||||
at += 1;
|
||||
let mut removed = Vec::with_capacity(del);
|
||||
for k in 0..del {
|
||||
if let Some(x) = i16_at(at + 2 * k) {
|
||||
removed.push(x);
|
||||
}
|
||||
}
|
||||
at += 2 * del * if close { 2 } else { 1 };
|
||||
let Some(&add) = op.get(at) else { return };
|
||||
let add = usize::from(add.min(64));
|
||||
at += 1;
|
||||
let mut tabs = p.tabs.take().unwrap_or_default();
|
||||
// Removed within 25 twips (1.25 pt), as the format says.
|
||||
tabs.retain(|t| !removed.iter().any(|x| (t.pos - x).abs() <= 1.25));
|
||||
for x in removed {
|
||||
tabs.push(TabStop { pos: x, align: TabAlign::Clear, leader: TabLeader::None });
|
||||
}
|
||||
for k in 0..add {
|
||||
let (Some(pos), Some(&tbd)) = (i16_at(at + 2 * k), op.get(at + 2 * add + k)) else { break };
|
||||
let align = match tbd & 7 {
|
||||
1 => TabAlign::Center,
|
||||
2 => TabAlign::Right,
|
||||
3 => TabAlign::Decimal,
|
||||
4 => TabAlign::Bar,
|
||||
_ => TabAlign::Left,
|
||||
};
|
||||
let leader = match (tbd >> 3) & 7 {
|
||||
1 => TabLeader::Dot,
|
||||
2 => TabLeader::Hyphen,
|
||||
3 | 4 => TabLeader::Underscore,
|
||||
5 => TabLeader::MiddleDot,
|
||||
_ => TabLeader::None,
|
||||
};
|
||||
tabs.retain(|t| (t.pos - pos).abs() > 0.05);
|
||||
tabs.push(TabStop { pos, align, leader });
|
||||
}
|
||||
tabs.sort_by(|a, b| a.pos.total_cmp(&b.pos));
|
||||
p.tabs = Some(tabs);
|
||||
}
|
||||
|
||||
/// Apply a paragraph-property sprm.
|
||||
pub(crate) fn apply_para(p: &mut ParaProps, prl: &Prl) {
|
||||
let tw = |v: i32| (v as f32 / 20.0).clamp(-1000.0, 1000.0);
|
||||
let s16 = |prl: &Prl| -> Option<i16> { u16_of(prl).map(|v| v as i16) };
|
||||
match prl.op {
|
||||
sprm::P_CHG_TABS | sprm::P_CHG_TABS_PAPX => change_tabs(p, prl),
|
||||
P_JC | P_JC80 => {
|
||||
p.align = match prl.operand.first().copied().unwrap_or(0) {
|
||||
1 => Some(Align::Center),
|
||||
2 => Some(Align::Right),
|
||||
3 => Some(Align::Justify),
|
||||
4 => Some(Align::Distribute),
|
||||
_ => Some(Align::Left),
|
||||
};
|
||||
}
|
||||
P_DXA_LEFT | P_DXA_LEFT80 => {
|
||||
if let Some(v) = s16(prl) {
|
||||
p.indent_left = Some(tw(v as i32));
|
||||
}
|
||||
}
|
||||
P_DXA_RIGHT | P_DXA_RIGHT80 => {
|
||||
if let Some(v) = s16(prl) {
|
||||
p.indent_right = Some(tw(v as i32));
|
||||
}
|
||||
}
|
||||
// A two-byte operand, like the other indents (spra 4).
|
||||
P_DXA_FIRST | P_DXA_FIRST80 => {
|
||||
if let Some(v) = s16(prl) {
|
||||
p.indent_first = Some(tw(v as i32));
|
||||
}
|
||||
}
|
||||
P_DXC_RIGHT | P_DXC_LEFT | P_DXC_FIRST => {
|
||||
if let Some(v) = s16(prl) {
|
||||
let chars = Some(f32::from(v) / 100.0);
|
||||
match prl.op {
|
||||
P_DXC_RIGHT => p.indent_right_chars = chars,
|
||||
P_DXC_LEFT => p.indent_left_chars = chars,
|
||||
_ => p.indent_first_chars = chars,
|
||||
}
|
||||
}
|
||||
}
|
||||
P_DYA_BEFORE => {
|
||||
if let Some(v) = s16(prl) {
|
||||
p.space_before = Some((v as f32 / 20.0).clamp(0.0, 3168.0));
|
||||
}
|
||||
}
|
||||
P_DYA_AFTER => {
|
||||
if let Some(v) = s16(prl) {
|
||||
p.space_after = Some((v as f32 / 20.0).clamp(0.0, 3168.0));
|
||||
}
|
||||
}
|
||||
// LSPD ([MS-DOC] §2.9.150): a signed dyaLine, then fMultLinespace. In lines (240ths) when
|
||||
// fMultLinespace is 1; else twips, at least when positive and exactly when negative.
|
||||
P_DYA_LINE => {
|
||||
if let Some(v) = s16(prl) {
|
||||
let mult = prl.operand.get(2..4).is_some_and(|b| b == [1, 0]);
|
||||
let v = f32::from(v);
|
||||
p.line_spacing = Some(if mult {
|
||||
LineSpacing::Multiple((v.abs() / 240.0).clamp(0.06, 132.0))
|
||||
} else if v < 0.0 {
|
||||
LineSpacing::Exactly((-v / 20.0).clamp(0.7, 1584.0))
|
||||
} else {
|
||||
LineSpacing::AtLeast((v / 20.0).clamp(0.0, 1584.0))
|
||||
});
|
||||
}
|
||||
}
|
||||
P_KEEP => {
|
||||
if matches!(sprm::toggle(prl), Toggle::On) {
|
||||
p.keep_lines = Some(true);
|
||||
}
|
||||
}
|
||||
P_KEEP_FOLLOW => {
|
||||
if matches!(sprm::toggle(prl), Toggle::On) {
|
||||
p.keep_next = Some(true);
|
||||
}
|
||||
}
|
||||
P_PAGE_BREAK_BEFORE => {
|
||||
if matches!(sprm::toggle(prl), Toggle::On) {
|
||||
p.page_break_before = Some(true);
|
||||
}
|
||||
}
|
||||
P_OUTLVL => {
|
||||
if let Some(&l) = prl.operand.first() {
|
||||
p.outline_level = Some(l.min(8));
|
||||
}
|
||||
}
|
||||
P_BRC_TOP80 | P_BRC_LEFT80 | P_BRC_BOTTOM80 | P_BRC_RIGHT80 | P_BRC_BETWEEN80 => {
|
||||
if let Some(b) = brc80(prl.operand) {
|
||||
set_border(p, prl.op, b);
|
||||
}
|
||||
}
|
||||
P_BRC_TOP | P_BRC_LEFT | P_BRC_BOTTOM | P_BRC_RIGHT | P_BRC_BETWEEN => {
|
||||
if let Some(b) = brc(prl.operand) {
|
||||
set_border(p, prl.op, b);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the model stylesheet: resolve each raw style's chain (base → this) and the ids we
|
||||
/// reference from paragraphs.
|
||||
pub(crate) fn stylesheet(raw: &[crate::stsh::RawStyle], fonts: &[String], standard: crate::stsh::StandardFonts) -> StyleSheet {
|
||||
let mut sheet = StyleSheet::empty();
|
||||
// Word's base character properties: the stylesheet's standard fonts (Aptos in a Word 365
|
||||
// file, whose font table still starts with Times New Roman, #5066), else font index 0 of the
|
||||
// table; 10 pt ([MS-DOC] sprmCHps).
|
||||
let font = |i: Option<u16>| i.and_then(|i| fonts.get(usize::from(i))).cloned();
|
||||
sheet.default_chr.font = font(standard.ascii).or_else(|| fonts.first().cloned());
|
||||
sheet.default_chr.font_ea = font(standard.east_asian);
|
||||
sheet.default_chr.font_cs = font(standard.bidi);
|
||||
if sheet.default_chr.size.is_none() {
|
||||
sheet.default_chr.size = Some(10.0);
|
||||
}
|
||||
for (istd, st) in raw.iter().enumerate() {
|
||||
if st.name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let id = style_id(&st.name);
|
||||
let kind = match st.stk {
|
||||
2 => StyleKind::Character,
|
||||
3 => StyleKind::Table,
|
||||
4 => StyleKind::Numbering,
|
||||
_ => StyleKind::Paragraph,
|
||||
};
|
||||
let base = base_of(raw, istd).and_then(|i| raw.get(i)).map(|b| style_id(&b.name));
|
||||
// Inherit: properties come from the base chain, root first, then our own grpprls.
|
||||
let mut chr = CharProps::default();
|
||||
let mut para = ParaProps::default();
|
||||
for s in style_chain(raw, istd).into_iter().filter_map(|i| raw.get(i)) {
|
||||
apply_chain(&mut chr, &s.chpx, fonts);
|
||||
apply_chain_para(&mut para, &s.papx);
|
||||
}
|
||||
if para.outline_level.is_none() && kind == StyleKind::Paragraph {
|
||||
let h = heading_level(&st.name);
|
||||
if h > 0 {
|
||||
para.outline_level = Some(h - 1);
|
||||
}
|
||||
}
|
||||
sheet.styles.push(Style { id, name: display_name(&st.name), kind, based_on: base, chr, para, ..Default::default() });
|
||||
}
|
||||
sheet
|
||||
}
|
||||
|
||||
/// Most `istdBase` links followed; a longer chain (or a cycle) in a corrupt file stops there.
|
||||
const MAX_STYLE_DEPTH: usize = 16;
|
||||
|
||||
/// The parent of style `istd`: its zero-based `istdBase` ([MS-DOC] §2.9.260), when that names
|
||||
/// another existing, named style (0x0FFF means none).
|
||||
fn base_of(raw: &[crate::stsh::RawStyle], istd: usize) -> Option<usize> {
|
||||
let b = raw.get(istd)?.istd_base;
|
||||
let b = usize::from(b);
|
||||
(b != 0x0FFF && b != istd && raw.get(b).is_some_and(|s| !s.name.is_empty())).then_some(b)
|
||||
}
|
||||
|
||||
/// Style `istd` and its ancestors, root first and `istd` last. Cycles and chains deeper than
|
||||
/// [`MAX_STYLE_DEPTH`] are cut.
|
||||
fn style_chain(raw: &[crate::stsh::RawStyle], istd: usize) -> Vec<usize> {
|
||||
let mut chain = vec![istd];
|
||||
let mut cur = istd;
|
||||
while chain.len() < MAX_STYLE_DEPTH {
|
||||
let Some(b) = base_of(raw, cur) else { break };
|
||||
if chain.contains(&b) {
|
||||
break;
|
||||
}
|
||||
chain.push(b);
|
||||
cur = b;
|
||||
}
|
||||
chain.reverse();
|
||||
chain
|
||||
}
|
||||
|
||||
/// The style-id form of a stored (lower-case) built-in name, matching the docx reader's ids.
|
||||
pub(crate) fn style_id(name: &str) -> String {
|
||||
let lower = name.trim().to_ascii_lowercase();
|
||||
match lower.as_str() {
|
||||
"normal" => "Normal".into(),
|
||||
"title" => "Title".into(),
|
||||
"subtitle" => "Subtitle".into(),
|
||||
"quote" | "body text" | "body text 2" | "body text 3" | "body text indent" | "body text indent 2" | "body text indent 3" => {
|
||||
let mut c = lower.chars();
|
||||
match c.next() {
|
||||
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
"default paragraph font" => "Default Paragraph Font".into(),
|
||||
_ => {
|
||||
let mut out = String::with_capacity(name.len());
|
||||
let mut up = true;
|
||||
for ch in name.trim().chars() {
|
||||
if ch == ' ' {
|
||||
up = true;
|
||||
out.push(ch);
|
||||
} else if up {
|
||||
out.extend(ch.to_uppercase());
|
||||
up = false;
|
||||
} else {
|
||||
out.push(ch);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// "heading 3" → 3; 0 when not a heading.
|
||||
fn heading_level(name: &str) -> u8 {
|
||||
let lower = name.trim().to_ascii_lowercase();
|
||||
lower.strip_prefix("heading ").and_then(|d| d.parse().ok()).filter(|&l: &u8| (1..=9).contains(&l)).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn display_name(name: &str) -> String {
|
||||
let lower = name.trim().to_ascii_lowercase();
|
||||
let h = heading_level(&lower);
|
||||
if h > 0 {
|
||||
return format!("Heading {h}");
|
||||
}
|
||||
match lower.as_str() {
|
||||
"body text" => "Body Text".into(),
|
||||
_ => style_id(name),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a grpprl of character sprms with a toggle base.
|
||||
fn apply_chain(chr: &mut CharProps, grpprl: &[u8], fonts: &[String]) {
|
||||
let base = chr.clone();
|
||||
for prl in sprm::iter(grpprl) {
|
||||
if prl.sgc() == 2 {
|
||||
apply_char(chr, &prl, &base, fonts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a grpprl of paragraph sprms.
|
||||
fn apply_chain_para(para: &mut ParaProps, grpprl: &[u8]) {
|
||||
for prl in sprm::iter(grpprl) {
|
||||
if prl.sgc() == 1 {
|
||||
apply_para(para, &prl);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-950
@@ -1,963 +1,31 @@
|
||||
//! WordCraft reader for the Word 97-2003 binary file format (`.doc`, [MS-DOC]).
|
||||
//! WordCraft adapter for reading Word 97-2003 binary documents (`.doc`, [MS-DOC]).
|
||||
//!
|
||||
//! [`read`] turns the bytes of a `.doc` file into a [`wordcraft_doc::Document`]. There is no
|
||||
//! writer: saving always goes through the other formats. The reader is written from the public
|
||||
//! specification ([MS-DOC], Word (.doc) Binary File Format, Microsoft Open Specifications) and,
|
||||
//! like every WordCraft parser, is lenient by design (unknown structures are skipped, bad numbers
|
||||
//! fall back to defaults) and bounded (stream sizes, piece counts and property counts are
|
||||
//! capped), so hostile input yields an error or a best-effort document, never a panic.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
//! The file-format implementation lives in `craft-doc-binary`. This crate adapts the shared
|
||||
//! model to WordCraft's application model while keeping the public read and password APIs stable.
|
||||
//! Stories are converted paragraph by paragraph, and embedded media buffers are moved without a
|
||||
//! full-document serialization copy.
|
||||
//!
|
||||
//! [MS-DOC]: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-doc/
|
||||
|
||||
mod crypt;
|
||||
mod drawing;
|
||||
mod fib;
|
||||
mod fkp;
|
||||
mod fmt;
|
||||
mod list;
|
||||
mod media;
|
||||
mod metadata;
|
||||
mod notes;
|
||||
mod officeart;
|
||||
mod piece;
|
||||
mod sections;
|
||||
mod sprm;
|
||||
mod stsh;
|
||||
mod table;
|
||||
mod model_bridge;
|
||||
|
||||
use std::io::{Read, Seek};
|
||||
use std::sync::Arc;
|
||||
pub use craft_doc_binary::DocbinError;
|
||||
pub use model_bridge::from_shared_model;
|
||||
|
||||
use wordcraft_doc::link::hyperlink_target;
|
||||
use wordcraft_doc::para::InlineObject;
|
||||
use wordcraft_doc::para::NoteKind;
|
||||
use wordcraft_doc::para::Paragraph;
|
||||
use wordcraft_doc::para::Run;
|
||||
use wordcraft_doc::props::CharProps;
|
||||
use wordcraft_doc::props::NumRef;
|
||||
use wordcraft_doc::section::HeaderSet;
|
||||
use wordcraft_doc::styles::StyleSheet;
|
||||
use wordcraft_doc::{Document, PartKind};
|
||||
|
||||
use fkp::Bins;
|
||||
use piece::{PieceTable, PrmRef};
|
||||
use sprm::Prl;
|
||||
use table::ParaOut;
|
||||
|
||||
/// Largest stream we materialise (bytes); the engine already caps whole files at 2 GB.
|
||||
const MAX_STREAM: u64 = 1 << 30;
|
||||
/// Most header/footer parts we create (matches the docx reader's cap).
|
||||
const MAX_PARTS: usize = 50_000;
|
||||
/// Hard ceiling on characters materialised across every story walk of one document (body,
|
||||
/// headers, notes, text boxes), whatever the file's size. The working budget is tied to the
|
||||
/// WordDocument stream (see [`read`]); this caps even a maximal stream.
|
||||
const MAX_TOTAL_CHARS: u64 = 64 << 20;
|
||||
/// Most paragraph and cell marks across every story walk of one document. The character
|
||||
/// budget does not see them: a mark is one byte of the file but about 1 KB of document, so a
|
||||
/// file of nothing but marks would grow memory about a thousand times faster than its size.
|
||||
/// The cap bounds a flood to about 2 GB while reading (about 4 GB at the very cap, when the
|
||||
/// blocks are assembled). Past it the read fails with [`DocbinError::Limit`]. Real documents stay far below it:
|
||||
/// 50 paragraphs on each of 10,000 pages is 500,000, and a 10,000-page one-column table (46
|
||||
/// rows, each a cell mark plus a row mark) is about 920,000.
|
||||
const MAX_PARAGRAPHS: usize = 2_000_000;
|
||||
/// Bytes of repeated work per document beyond the text itself: piece Prm grpprls (Prcs, up
|
||||
/// to 64 KB each) interpreted (#3692), hyperlink targets (#3708), picture alt text (#3709)
|
||||
/// and floating objects (#3710) copied into every run or anchor. Past it Prms are skipped,
|
||||
/// copied strings left empty and floating objects dropped, each kind logged once a document
|
||||
/// ([`Work::log`]).
|
||||
const MAX_WORK: u64 = 64 << 20;
|
||||
|
||||
/// What the [`MAX_WORK`] budget pays for, and leaves out once it is spent.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Cost {
|
||||
/// A piece's Prm grpprl: past the budget the piece keeps its CHPX formatting only.
|
||||
Formatting,
|
||||
/// A hyperlink target copied into a run of its result: past it the text is kept, unlinked.
|
||||
LinkTarget,
|
||||
/// A picture's alt text copied into an anchor: past it the picture has none.
|
||||
AltText,
|
||||
/// A floating object copied into its anchor: past it the object is left out.
|
||||
FloatingObject,
|
||||
}
|
||||
|
||||
impl Cost {
|
||||
const ALL: [Cost; 4] = [Cost::Formatting, Cost::LinkTarget, Cost::AltText, Cost::FloatingObject];
|
||||
|
||||
/// What is left out, for the log.
|
||||
fn left_out(self) -> &'static str {
|
||||
match self {
|
||||
Cost::Formatting => "some text keeps only part of its formatting",
|
||||
Cost::LinkTarget => "some hyperlinks' text is kept without its link",
|
||||
Cost::AltText => "some pictures are left without their alt text",
|
||||
Cost::FloatingObject => "some floating shapes and pictures are left out",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One document's [`MAX_WORK`] budget, and what it has left out (each kind logged once when
|
||||
/// the document has been read, [`Work::log`]).
|
||||
struct Work {
|
||||
left: std::cell::Cell<u64>,
|
||||
dropped: std::cell::Cell<[bool; 4]>,
|
||||
}
|
||||
|
||||
impl Work {
|
||||
fn new(budget: u64) -> Self {
|
||||
Work { left: std::cell::Cell::new(budget), dropped: std::cell::Cell::new([false; 4]) }
|
||||
}
|
||||
|
||||
/// Spends `cost` bytes on `what`: false (and `what` noted as left out) when they aren't left.
|
||||
fn charge(&self, cost: u64, what: Cost) -> bool {
|
||||
match self.left.get().checked_sub(cost) {
|
||||
Some(left) => {
|
||||
self.left.set(left);
|
||||
true
|
||||
}
|
||||
None => {
|
||||
let mut dropped = self.dropped.get();
|
||||
if let Some(d) = Cost::ALL.iter().position(|c| *c == what).and_then(|i| dropped.get_mut(i)) {
|
||||
*d = true;
|
||||
}
|
||||
self.dropped.set(dropped);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The kinds of work left out so far, in [`Cost::ALL`] order.
|
||||
fn dropped(&self) -> Vec<Cost> {
|
||||
Cost::ALL.iter().zip(self.dropped.get()).filter(|(_, d)| *d).map(|(c, _)| *c).collect()
|
||||
}
|
||||
|
||||
/// Logs one warning for each kind left out.
|
||||
fn log(&self) {
|
||||
let dropped = self.dropped();
|
||||
for c in &dropped {
|
||||
log::warn!(".doc: the document repeats more than WordCraft copies ({MAX_WORK} bytes): {}", c.left_out());
|
||||
}
|
||||
#[cfg(test)]
|
||||
LAST_DROPPED.with(|d| *d.borrow_mut() = dropped);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// What the last document read on this thread left out for the work budget ([`Work::log`]).
|
||||
static LAST_DROPPED: std::cell::RefCell<Vec<Cost>> = const { std::cell::RefCell::new(Vec::new()) };
|
||||
}
|
||||
/// `sprmCPicLocation`: the fc of a picture in the Data stream.
|
||||
const C_PIC_LOCATION: u16 = 0x6A03;
|
||||
/// `sprmCSymbol`: the record font and character of a symbol character ([MS-DOC] §2.6.1); the
|
||||
/// text stream holds the placeholder U+0028 at its position.
|
||||
const C_SYMBOL: u16 = 0x6A09;
|
||||
/// `sprmPIlvl` / `sprmPIlfo`: the paragraph's list level and list.
|
||||
const P_ILVL: u16 = 0x260A;
|
||||
const P_ILFO: u16 = 0x460B;
|
||||
|
||||
/// Errors from reading a Word 97-2003 binary file.
|
||||
#[derive(Debug, thiserror::Error, Clone, PartialEq)]
|
||||
pub enum DocbinError {
|
||||
/// The bytes are not a readable compound file (OLE2/CFB container).
|
||||
#[error("not a Word document (compound file): {0}")]
|
||||
Container(String),
|
||||
/// The compound file is readable but is not a Word 97-2003 document.
|
||||
#[error("not a Word 97-2003 document: {0}")]
|
||||
NotWord(String),
|
||||
/// The document is password-protected and no password was given.
|
||||
#[error("this document is protected with a password")]
|
||||
PasswordRequired,
|
||||
/// The password does not open the document.
|
||||
#[error("the password is incorrect")]
|
||||
WrongPassword,
|
||||
/// The document is encrypted in a way WordCraft can't read (AES, an external provider, an
|
||||
/// unknown header).
|
||||
#[error("encrypted document: {0}")]
|
||||
Encryption(String),
|
||||
/// The input exceeds a safety limit (stream size, piece count…).
|
||||
#[error("limit exceeded: {0}")]
|
||||
Limit(String),
|
||||
/// A structure is malformed beyond recovery.
|
||||
#[error("malformed document: {0}")]
|
||||
Malformed(String),
|
||||
}
|
||||
|
||||
/// Is this `.doc` password-protected (XOR obfuscation, RC4 or RC4 CryptoAPI)? False for
|
||||
/// anything that is not a readable Word 97-2003 file.
|
||||
/// Whether the input is a readable Word 97-2003 document protected by a password.
|
||||
pub fn is_encrypted(bytes: &[u8]) -> bool {
|
||||
let Ok(mut comp) = open_container(bytes) else { return false };
|
||||
stream(&mut comp, "WordDocument").is_some_and(|w| fib::peek_encryption(&w).is_some())
|
||||
craft_doc_binary::is_encrypted(bytes)
|
||||
}
|
||||
|
||||
/// Open the compound file around a `.doc`, after [`wordcraft_doc::cfb_guard::check`].
|
||||
fn open_container(bytes: &[u8]) -> Result<cfb::CompoundFile<std::io::Cursor<&[u8]>>, DocbinError> {
|
||||
wordcraft_doc::cfb_guard::check(bytes).map_err(DocbinError::Container)?;
|
||||
cfb::CompoundFile::open(std::io::Cursor::new(bytes)).map_err(|e| DocbinError::Container(e.to_string()))
|
||||
}
|
||||
|
||||
/// Read a `.doc` (Word 97-2003 binary) file into a [`Document`]. A password-protected file
|
||||
/// gives [`DocbinError::PasswordRequired`]; see [`read_with_password`].
|
||||
pub fn read(bytes: &[u8]) -> Result<Document, DocbinError> {
|
||||
/// Read a Word 97-2003 binary document into WordCraft's document model.
|
||||
pub fn read(bytes: &[u8]) -> Result<wordcraft_doc::Document, DocbinError> {
|
||||
read_with_password(bytes, None)
|
||||
}
|
||||
|
||||
/// [`read`] for a file that may be password-protected: it is checked against the file's stored
|
||||
/// verifier ([`DocbinError::WrongPassword`] when it does not match) and then decrypted. The
|
||||
/// password is ignored for an unprotected file.
|
||||
pub fn read_with_password(bytes: &[u8], password: Option<&str>) -> Result<Document, DocbinError> {
|
||||
read_limited(bytes, password, MAX_PARAGRAPHS)
|
||||
}
|
||||
|
||||
/// [`read_with_password`] with the paragraph cap as a parameter, so tests need not build a
|
||||
/// file of [`MAX_PARAGRAPHS`] marks.
|
||||
fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) -> Result<Document, DocbinError> {
|
||||
let mut comp = open_container(bytes)?;
|
||||
let mut word = stream(&mut comp, "WordDocument").ok_or_else(|| DocbinError::NotWord("no WordDocument stream".into()))?;
|
||||
if word.len() as u64 > MAX_STREAM {
|
||||
return Err(DocbinError::Limit(format!("WordDocument stream is {} bytes", word.len())));
|
||||
}
|
||||
let enc = fib::peek_encryption(&word);
|
||||
// The FIB names the Table stream (0Table or 1Table); fall back to whichever exists,
|
||||
// since files written by other writers sometimes carry the wrong bit.
|
||||
let which = if let Some(e) = enc { e.which_tbl_stm } else { fib::Fib::parse(&word)?.which_tbl_stm };
|
||||
let named = if which { "1Table" } else { "0Table" };
|
||||
let other = if which { "0Table" } else { "1Table" };
|
||||
let mut table = stream(&mut comp, named).or_else(|| stream(&mut comp, other)).unwrap_or_default();
|
||||
if table.len() as u64 > MAX_STREAM {
|
||||
return Err(DocbinError::Limit(format!("Table stream is {} bytes", table.len())));
|
||||
}
|
||||
let mut data = stream(&mut comp, "Data").unwrap_or_default();
|
||||
if data.len() as u64 > MAX_STREAM {
|
||||
return Err(DocbinError::Limit(format!("Data stream is {} bytes", data.len())));
|
||||
}
|
||||
if let Some(e) = enc {
|
||||
// Verify the password first; only then is anything decrypted.
|
||||
let password = password.ok_or(DocbinError::PasswordRequired)?;
|
||||
let cipher = crypt::Cipher::open(e.flags, e.lkey, &table, password)?;
|
||||
cipher.decrypt_word(&mut word);
|
||||
cipher.decrypt_table(&mut table);
|
||||
cipher.decrypt_data(&mut data);
|
||||
}
|
||||
let fib = fib::Fib::parse(&word)?;
|
||||
|
||||
// The piece table locates every character in the WordDocument stream.
|
||||
let (fc_clx, lcb_clx) =
|
||||
fib.pair(fib::pair::CLX).filter(|(_, lcb)| *lcb > 0).ok_or_else(|| DocbinError::Malformed("the FIB has no piece table (Clx)".into()))?;
|
||||
let pieces = piece::PieceTable::parse(&table, fc_clx, lcb_clx)?;
|
||||
|
||||
// Styles, fonts and the formatting bin tables; each is optional in broken files.
|
||||
let fonts = fib.pair(fib::pair::STTBF_FFN).map(|(f, l)| stsh::fonts(&table, f, l)).unwrap_or_default();
|
||||
let raw_styles = match fib.pair(fib::pair::STSHF) {
|
||||
Some((f, l)) => stsh::parse(&table, f, l)?,
|
||||
None => Vec::new(),
|
||||
};
|
||||
let standard_fonts = fib.pair(fib::pair::STSHF).map(|(f, l)| stsh::standard_fonts(&table, f, l)).unwrap_or_default();
|
||||
let chpx_bins = match fib.pair(fib::pair::PLCF_BTE_CHPX) {
|
||||
Some((f, l)) => Bins::parse(&table, f, l)?,
|
||||
None => Bins::parse(&[], 0, 0)?,
|
||||
};
|
||||
let papx_bins = match fib.pair(fib::pair::PLCF_BTE_PAPX) {
|
||||
Some((f, l)) => Bins::parse(&table, f, l)?,
|
||||
None => Bins::parse(&[], 0, 0)?,
|
||||
};
|
||||
|
||||
let mut doc = Document::new();
|
||||
if let Some(core) = metadata::read(&mut comp) {
|
||||
doc.core = core;
|
||||
}
|
||||
doc.body.clear();
|
||||
doc.styles = Arc::new(fmt::stylesheet(&raw_styles, &fonts, standard_fonts));
|
||||
doc.numbering = Arc::new(list::parse(&table, &fib, &fonts));
|
||||
let sheet: StyleSheet = StyleSheet::clone(&doc.styles);
|
||||
let sects = sections::parse(&word, &table, &fib);
|
||||
let stories = sections::header_stories(&table, &fib);
|
||||
// Subdocument layout in the CP space: main document, footnotes, headers, comments,
|
||||
// endnotes, text boxes, header text boxes.
|
||||
// Story ranges from the Table stream are hostile: each is clamped to its subdocument's
|
||||
// length here and to the last piece's CP inside `walk`.
|
||||
let ftn_base = fib.ccp.text;
|
||||
let hdd_base = fib.ccp.text.saturating_add(fib.ccp.ftn);
|
||||
let edn_base = hdd_base.saturating_add(fib.ccp.hdd).saturating_add(fib.ccp.atn);
|
||||
let mut pics = media::Pictures::for_input(word.len().saturating_add(data.len()));
|
||||
// Every materialised character comes from at least one byte of the WordDocument stream, so a
|
||||
// legitimate document's total text (body plus every header, footnote and text box, each read
|
||||
// once) cannot exceed its stream. A hostile piece or story table that aliases the same bytes
|
||||
// many times can, so the walk is held to twice the stream's size — generous for real files,
|
||||
// tight against aliasing — and never past the absolute ceiling.
|
||||
let budget = std::cell::Cell::new((word.len() as u64).saturating_mul(2).min(MAX_TOTAL_CHARS));
|
||||
let work = Work::new(MAX_WORK);
|
||||
let paragraphs = ParaBudget { left: std::cell::Cell::new(max_paragraphs), exceeded: std::cell::Cell::new(false) };
|
||||
|
||||
let ctx = WalkCtx {
|
||||
word: &word,
|
||||
data: &data,
|
||||
pieces: &pieces,
|
||||
chpx_bins: &chpx_bins,
|
||||
papx_bins: &papx_bins,
|
||||
fonts: &fonts,
|
||||
raw_styles: &raw_styles,
|
||||
sheet: &sheet,
|
||||
notes: &[],
|
||||
marks: &[],
|
||||
drawn: &[],
|
||||
sect_ends: &[],
|
||||
budget: &budget,
|
||||
paragraphs: ¶graphs,
|
||||
work: &work,
|
||||
};
|
||||
|
||||
// Drawings: text box stories become parts first, then the floating shapes and pictures
|
||||
// anchored in the main document and the headers, found by their anchor CP while walking.
|
||||
let txbx_base = edn_base.saturating_add(fib.ccp.edn);
|
||||
let hdr_txbx_base = txbx_base.saturating_add(fib.ccp.txbx);
|
||||
let art = fib
|
||||
.pair(fib::pair::DGG_INFO)
|
||||
.and_then(|(f, l)| table.get(f as usize..(f as usize).saturating_add(l as usize)))
|
||||
.map(officeart::content)
|
||||
.unwrap_or_default();
|
||||
let mut tb_stories = std::collections::HashMap::new();
|
||||
for (pair, base, len) in [(fib::pair::PLCF_TXBX_TXT, txbx_base, fib.ccp.txbx), (fib::pair::PLCF_HDR_TXBX_TXT, hdr_txbx_base, fib.ccp.hdr_txbx)] {
|
||||
for (spid, ab) in drawing::textbox_stories(&table, fib.pair(pair)) {
|
||||
let Some((a, b)) = story(base, len, ab) else { continue };
|
||||
if doc.parts.len() >= MAX_PARTS || tb_stories.contains_key(&spid) {
|
||||
continue;
|
||||
}
|
||||
let blocks = story_blocks(&ctx, a, b, &mut pics, max_paragraphs)?;
|
||||
tb_stories.insert(spid, doc.add_part(PartKind::TextBox, blocks));
|
||||
}
|
||||
}
|
||||
let build = drawing::Build { bstore: &art.bstore, delay: &word, stories: &tb_stories };
|
||||
let mut drawn = drawing::anchors(&table, fib.pair(fib::pair::PLC_SPA_MOM), 0, &art.main, &build, &mut pics);
|
||||
drawn.extend(drawing::anchors(&table, fib.pair(fib::pair::PLC_SPA_HDR), hdd_base, &art.header, &build, &mut pics));
|
||||
drawn.sort_by_key(|(cp, _)| *cp);
|
||||
let ctx = WalkCtx { drawn: &drawn, ..ctx };
|
||||
|
||||
// Header and footer parts: skip the six separator stories, then six stories per section
|
||||
// in the order even header, odd (default) header, even footer, odd (default) footer,
|
||||
// first header, first footer. Empty stories inherit from the previous section.
|
||||
let mut hf_ids: Vec<(usize, usize, u32)> = Vec::new();
|
||||
for si in 0..sects.len() {
|
||||
for k in 0..6usize {
|
||||
let Some(&ab) = stories.get(6 + si * 6 + k) else { break };
|
||||
let Some((a, b)) = story(hdd_base, fib.ccp.hdd, ab) else { continue };
|
||||
if doc.parts.len() >= MAX_PARTS {
|
||||
continue;
|
||||
}
|
||||
let kind = match k {
|
||||
0 | 1 | 4 => PartKind::Header,
|
||||
_ => PartKind::Footer,
|
||||
};
|
||||
let blocks = story_blocks(&ctx, a, b, &mut pics, max_paragraphs)?;
|
||||
if !blocks.is_empty() {
|
||||
let id = doc.add_part(kind, blocks);
|
||||
hf_ids.push((si, k, id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Footnote and endnote stories: each text range becomes a part; the reference CPs then
|
||||
// link to the part ids during the main walk.
|
||||
let mut note_links: Vec<(u32, NoteKind, u32)> = Vec::new();
|
||||
for (cp, ab) in notes::parse_notes(&table, &fib, fib::pair::PLCF_FND_REF, fib::pair::PLCF_FND_TXT) {
|
||||
let Some((a, b)) = story(ftn_base, fib.ccp.ftn, ab) else { continue };
|
||||
let blocks = story_blocks(&ctx, a, b, &mut pics, max_paragraphs)?;
|
||||
if !blocks.is_empty() && doc.parts.len() < MAX_PARTS {
|
||||
let id = doc.add_part(PartKind::Footnote, blocks);
|
||||
note_links.push((cp, NoteKind::Footnote, id));
|
||||
}
|
||||
}
|
||||
for (cp, ab) in notes::parse_notes(&table, &fib, fib::pair::PLCF_END_REF, fib::pair::PLCF_END_TXT) {
|
||||
let Some((a, b)) = story(edn_base, fib.ccp.edn, ab) else { continue };
|
||||
let blocks = story_blocks(&ctx, a, b, &mut pics, max_paragraphs)?;
|
||||
if !blocks.is_empty() && doc.parts.len() < MAX_PARTS {
|
||||
let id = doc.add_part(PartKind::Endnote, blocks);
|
||||
note_links.push((cp, NoteKind::Endnote, id));
|
||||
}
|
||||
}
|
||||
// Sorted so the main walk finds each reference by binary search.
|
||||
note_links.sort_by_key(|(cp, _, _)| *cp);
|
||||
let marks = notes::parse_bookmarks(&table, &fib);
|
||||
let mut sect_ends: Vec<u32> = sects.iter().map(|s| s.end_cp).collect();
|
||||
sect_ends.sort_unstable();
|
||||
let ctx = WalkCtx { notes: ¬e_links, marks: &marks, sect_ends: §_ends, ..ctx };
|
||||
|
||||
// Sections: each section's properties attach to the paragraph that ends it (its story of
|
||||
// header/footer parts included); the section reaching the end of the text is the final one.
|
||||
let mut paras = walk(&ctx, 0, fib.ccp.text, &mut pics);
|
||||
// The first paragraph ending at each CP, found once rather than by a scan per section.
|
||||
let mut ending: std::collections::HashMap<u32, usize> = std::collections::HashMap::new();
|
||||
for (i, p) in paras.iter().enumerate() {
|
||||
ending.entry(p.end_cp).or_insert(i);
|
||||
}
|
||||
let last = sects.last().map(|s| s.props.clone());
|
||||
for (si, sec) in sects.iter().enumerate() {
|
||||
let mut props = sec.props.clone();
|
||||
let id_of = |k: usize| hf_ids.iter().find(|(s, kk, _)| *s == si && *kk == k).map(|(_, _, id)| *id);
|
||||
props.headers = HeaderSet { even: id_of(0), default: id_of(1), first: id_of(4) };
|
||||
props.footers = HeaderSet { even: id_of(2), default: id_of(3), first: id_of(5) };
|
||||
let is_final = si + 1 == sects.len() || sec.end_cp >= fib.ccp.text;
|
||||
if is_final {
|
||||
continue; // handled below through `last`
|
||||
}
|
||||
if let Some(p) = ending.get(&sec.end_cp).and_then(|&i| paras.get_mut(i)) {
|
||||
p.para.section = Some(Box::new(props));
|
||||
}
|
||||
}
|
||||
if let Some(mut l) = last {
|
||||
let si = sects.len().saturating_sub(1);
|
||||
let id_of = |k: usize| hf_ids.iter().find(|(s, kk, _)| *s == si && *kk == k).map(|(_, _, id)| *id);
|
||||
l.headers = HeaderSet { even: id_of(0), default: id_of(1), first: id_of(4) };
|
||||
l.footers = HeaderSet { even: id_of(2), default: id_of(3), first: id_of(5) };
|
||||
doc.last_section = l;
|
||||
}
|
||||
|
||||
if paragraphs.exceeded.get() {
|
||||
return Err(paragraph_limit(max_paragraphs));
|
||||
}
|
||||
doc.media.extend(pics.media);
|
||||
doc.body = table::assemble(paras);
|
||||
doc.ensure_nonempty();
|
||||
work.log();
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
fn paragraph_limit(max_paragraphs: usize) -> DocbinError {
|
||||
DocbinError::Limit(format!("the document has more than {max_paragraphs} paragraphs and table cells"))
|
||||
}
|
||||
|
||||
/// Walks one story into blocks. A walk that spent the shared paragraph budget is refused before
|
||||
/// its partial list is assembled, so a flood does not build blocks it will throw away.
|
||||
fn story_blocks(ctx: &WalkCtx<'_>, a: u32, b: u32, pics: &mut media::Pictures, max_paragraphs: usize) -> Result<wordcraft_doc::Blocks, DocbinError> {
|
||||
let paras = walk(ctx, a, b, pics);
|
||||
if ctx.paragraphs.exceeded.get() {
|
||||
return Err(paragraph_limit(max_paragraphs));
|
||||
}
|
||||
Ok(table::assemble(paras))
|
||||
}
|
||||
|
||||
/// A story's CP range `[a, b)`, relative to a subdocument starting at `base` with `len`
|
||||
/// CPs, as absolute CPs clamped to that subdocument; `None` when empty.
|
||||
fn story(base: u32, len: u32, (a, b): (u32, u32)) -> Option<(u32, u32)> {
|
||||
let (a, b) = (a.min(len), b.min(len));
|
||||
if b <= a {
|
||||
return None;
|
||||
}
|
||||
Some((base.checked_add(a)?, base.checked_add(b)?))
|
||||
}
|
||||
|
||||
/// Everything the walker needs besides its CP range.
|
||||
struct WalkCtx<'a> {
|
||||
word: &'a [u8],
|
||||
data: &'a [u8],
|
||||
pieces: &'a PieceTable,
|
||||
chpx_bins: &'a Bins,
|
||||
papx_bins: &'a Bins,
|
||||
fonts: &'a [String],
|
||||
raw_styles: &'a [stsh::RawStyle],
|
||||
sheet: &'a StyleSheet,
|
||||
/// (reference CP in the main document, kind, part id) for notes.
|
||||
notes: &'a [(u32, NoteKind, u32)],
|
||||
/// Bookmark boundaries (main-document CPs), sorted for a sequential walk.
|
||||
marks: &'a [(u32, notes::Mark)],
|
||||
/// Floating shapes and pictures by anchor CP (absolute), sorted.
|
||||
drawn: &'a [(u32, InlineObject)],
|
||||
/// Where sections end (main-document CPs, sorted): the section mark (0x0C) just before
|
||||
/// each ends the section's last paragraph, it isn't a page break inside it.
|
||||
sect_ends: &'a [u32],
|
||||
/// Characters still allowed across every story walk of this document. Story ranges (headers,
|
||||
/// footnotes, text boxes) are hostile and can each span a whole subdocument, so thousands of
|
||||
/// them could otherwise materialise the same text thousands of times; this bounds the total.
|
||||
budget: &'a std::cell::Cell<u64>,
|
||||
/// Paragraph and cell marks still allowed across every story walk ([`MAX_PARAGRAPHS`]).
|
||||
paragraphs: &'a ParaBudget,
|
||||
/// Repeated-work bytes left ([`MAX_WORK`]), and what they left out.
|
||||
work: &'a Work,
|
||||
}
|
||||
|
||||
/// The paragraph and cell marks still allowed in this document, and whether a walk ran into
|
||||
/// the end of them (the read then fails).
|
||||
struct ParaBudget {
|
||||
left: std::cell::Cell<usize>,
|
||||
exceeded: std::cell::Cell<bool>,
|
||||
}
|
||||
|
||||
impl ParaBudget {
|
||||
/// Spends one mark; false (and the overrun noted) when none are left.
|
||||
fn spend(&self) -> bool {
|
||||
match self.left.get().checked_sub(1) {
|
||||
Some(left) => {
|
||||
self.left.set(left);
|
||||
true
|
||||
}
|
||||
None => {
|
||||
self.exceeded.set(true);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// State while assembling one paragraph from per-CP formatting.
|
||||
#[derive(Default)]
|
||||
struct ParaBuild {
|
||||
text: String,
|
||||
runs: Vec<Run>,
|
||||
props: wordcraft_doc::props::ParaProps,
|
||||
mark: CharProps,
|
||||
/// The current run's properties, so runs only break when formatting changes.
|
||||
cur: Option<CharProps>,
|
||||
/// Inline objects, each anchored at a U+FFFC pushed into `text`.
|
||||
objects: Vec<InlineObject>,
|
||||
}
|
||||
|
||||
impl ParaBuild {
|
||||
fn push(&mut self, c: char, props: CharProps) {
|
||||
let len = c.len_utf8();
|
||||
match &self.cur {
|
||||
Some(p) if *p == props => {
|
||||
if let Some(r) = self.runs.last_mut() {
|
||||
r.len += len;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.runs.push(Run { len, props: props.clone() });
|
||||
self.cur = Some(props);
|
||||
}
|
||||
}
|
||||
self.text.push(c);
|
||||
}
|
||||
|
||||
/// Append `c` to the current run, whose properties the caller knows it matches.
|
||||
fn extend(&mut self, c: char) {
|
||||
if let Some(r) = self.runs.last_mut() {
|
||||
r.len += c.len_utf8();
|
||||
}
|
||||
self.text.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// Walk a CP range, splitting paragraphs at 0x0D/0x07 marks, applying the direct character
|
||||
/// formatting of each CP and the paragraph formatting of each mark. Works for the main
|
||||
/// document and for any subdocument range (headers, footers, notes…). Field characters,
|
||||
/// note references, bookmarks and inline pictures become inline objects; pictures go into
|
||||
/// `pics`, which shares one media entry per Data-stream location.
|
||||
///
|
||||
/// The range is clamped to the last piece's CP, and unreadable stretches (gaps, truncated
|
||||
/// pieces) are skipped a whole piece at a time, so the work is bounded by the text that
|
||||
/// really exists, not by the CP numbers a file claims.
|
||||
fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pictures) -> Vec<ParaOut> {
|
||||
let cp_end = cp_end.min(ctx.pieces.cp_end());
|
||||
let mut out = Vec::new();
|
||||
let mut pb = ParaBuild::default();
|
||||
let mut cp = cp_start;
|
||||
let mut marks = ctx.marks.iter().peekable();
|
||||
// Field assembly state: instruction characters are swallowed, the result is kept until
|
||||
// the end character, then emitted as one object (hyperlinks keep their result text).
|
||||
let mut field: Option<FieldBuild> = None;
|
||||
let mut memo: Option<FmtMemo<'a>> = None;
|
||||
while cp < cp_end {
|
||||
// Bookmark boundaries at this CP (main-document walks only; subdocument walks get
|
||||
// an empty table and `cp` never matches the main-document CPs).
|
||||
// `<=` so a boundary inside a skipped stretch is still emitted (late) rather than
|
||||
// blocking every later one.
|
||||
while marks.peek().is_some_and(|&(mcp, _)| *mcp <= cp) {
|
||||
if let Some((_, m)) = marks.next() {
|
||||
let obj = match m {
|
||||
notes::Mark::Start(name) => InlineObject::BookmarkStart { name: name.clone() },
|
||||
notes::Mark::End(name) => InlineObject::BookmarkEnd { name: name.clone() },
|
||||
};
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.objects.push(obj);
|
||||
}
|
||||
}
|
||||
let (c, units) = match ctx.pieces.char_at(ctx.word, cp) {
|
||||
Ok(found) => found,
|
||||
Err(next) => {
|
||||
cp = next;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
// Every real character counts against the document-wide budget; once it is spent, stop
|
||||
// (a hostile file with thousands of story ranges over the same text can't multiply on).
|
||||
if ctx.budget.get() == 0 {
|
||||
break;
|
||||
}
|
||||
ctx.budget.set(ctx.budget.get() - 1);
|
||||
// A section mark (0x0C) at a section's end is that section's last paragraph mark.
|
||||
let section_mark = c == '\u{C}' && ctx.sect_ends.binary_search(&cp.saturating_add(1)).is_ok();
|
||||
match c {
|
||||
'\r' | '\u{7}' | '\u{C}' if c != '\u{C}' || section_mark => {
|
||||
if !ctx.paragraphs.spend() {
|
||||
break;
|
||||
}
|
||||
// Paragraph mark: its PAPX covers the FC range of the paragraph it ends, so
|
||||
// querying at the mark's own FC finds that paragraph's properties.
|
||||
let fc_mark = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
let (istd, papx) = ctx.papx_bins.papx(ctx.word, fc_mark);
|
||||
if let Some(st) = ctx.raw_styles.get(istd as usize).filter(|s| !s.name.is_empty()) {
|
||||
pb.props.style = Some(fmt::style_id(&st.name));
|
||||
}
|
||||
let mut row = table::decode(papx);
|
||||
row.cell_mark = c == '\u{7}';
|
||||
// Only table marks keep their table context; a plain paragraph would carry
|
||||
// a `RowInfo` (about 300 bytes) it never uses.
|
||||
let row = row.is_table().then(|| Box::new(row));
|
||||
let mut ilvl = 0u8;
|
||||
let mut ilfo = 0i32;
|
||||
for prl in sprm::iter(papx) {
|
||||
if prl.op == P_ILVL {
|
||||
ilvl = prl.operand.first().copied().unwrap_or(0).min(8);
|
||||
continue;
|
||||
}
|
||||
if prl.op == P_ILFO {
|
||||
ilfo = match prl.operand {
|
||||
[b0, b1, ..] => i16::from_le_bytes([*b0, *b1]) as i32,
|
||||
_ => 0,
|
||||
};
|
||||
continue;
|
||||
}
|
||||
if prl.sgc() == 1 {
|
||||
fmt::apply_para(&mut pb.props, &prl);
|
||||
}
|
||||
}
|
||||
if let Some(num) = num_of(ilfo) {
|
||||
pb.props.numbering = Some(NumRef { num, level: ilvl });
|
||||
}
|
||||
let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
pb.mark = char_props(ctx.chpx_bins.chpx(ctx.word, fc), ctx.fonts, &CharProps::default());
|
||||
let done = std::mem::take(&mut pb);
|
||||
out.push(ParaOut {
|
||||
para: Paragraph {
|
||||
text: done.text,
|
||||
runs: done.runs,
|
||||
props: done.props,
|
||||
mark: done.mark,
|
||||
objects: done.objects,
|
||||
..Default::default()
|
||||
},
|
||||
end_cp: cp.saturating_add(1),
|
||||
row,
|
||||
});
|
||||
}
|
||||
// Field begin/separator/end ([MS-DOC] §2.9.84 Plcfld).
|
||||
'\u{13}' => {
|
||||
match field.as_mut() {
|
||||
Some(f) => {
|
||||
f.depth = f.depth.saturating_add(1);
|
||||
}
|
||||
None => field = Some(FieldBuild { depth: 1, instr: String::new(), sep: None }),
|
||||
}
|
||||
cp = cp.saturating_add(1);
|
||||
continue;
|
||||
}
|
||||
'\u{14}' => {
|
||||
if let Some(f) = field.as_mut()
|
||||
&& f.depth == 1
|
||||
&& f.sep.is_none()
|
||||
{
|
||||
f.sep = Some(FieldResult::default());
|
||||
}
|
||||
cp = cp.saturating_add(1);
|
||||
continue;
|
||||
}
|
||||
'\u{15}' => {
|
||||
if let Some(mut f) = field.take() {
|
||||
if f.depth > 1 {
|
||||
f.depth -= 1;
|
||||
field = Some(f);
|
||||
} else {
|
||||
finish_field(&mut pb, f, ctx.work);
|
||||
}
|
||||
}
|
||||
cp = cp.saturating_add(1);
|
||||
continue;
|
||||
}
|
||||
'\u{1}' => {
|
||||
// Inline picture: the CHPX of the anchor carries sprmCPicLocation.
|
||||
let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
let fc_pic = pic_location(ctx.chpx_bins.chpx(ctx.word, fc));
|
||||
match fc_pic.and_then(|at| pics.get(ctx.data, at)) {
|
||||
Some(p) => {
|
||||
// Every anchor of a picture gets its own copy of the alt text; the
|
||||
// copies are charged to the work budget (#3709).
|
||||
let alt = if ctx.work.charge(p.alt.len() as u64, Cost::AltText) { p.alt.clone() } else { String::new() };
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.objects.push(InlineObject::Image {
|
||||
media: p.key.clone(),
|
||||
w: p.w,
|
||||
h: p.h,
|
||||
alt,
|
||||
float: Default::default(),
|
||||
crop: p.crop,
|
||||
fx: Default::default(),
|
||||
ole: None,
|
||||
});
|
||||
}
|
||||
None => pics.dropped(cp),
|
||||
}
|
||||
}
|
||||
'\u{8}' => {
|
||||
// Drawn object anchor: the floating shape or picture anchored at this CP.
|
||||
let at = ctx.drawn.partition_point(|(c, _)| *c < cp);
|
||||
// Each copy (a story can be walked more than once) is charged to the work budget
|
||||
// by what a clone copies: a picture's alt text, a group with all its members (#3710).
|
||||
for (_, obj) in ctx.drawn.get(at..).unwrap_or_default().iter().take_while(|(c, _)| *c == cp) {
|
||||
if !ctx.work.charge(object_cost(obj), Cost::FloatingObject) {
|
||||
continue;
|
||||
}
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.objects.push(obj.clone());
|
||||
}
|
||||
}
|
||||
'\u{2}' => {
|
||||
// Footnote or endnote reference; custom symbols share the same character.
|
||||
let at = ctx.notes.partition_point(|(c, _, _)| *c < cp);
|
||||
if let Some((_, kind, id)) = ctx.notes.get(at).filter(|(c, _, _)| *c == cp) {
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.objects.push(InlineObject::NoteRef { kind: *kind, id: *id, custom: String::new() });
|
||||
}
|
||||
}
|
||||
'\t' => push_formatted(&mut pb, cp, '\t', ctx, field.as_mut(), &mut memo),
|
||||
'\u{B}' => push_formatted(&mut pb, cp, '\n', ctx, field.as_mut(), &mut memo),
|
||||
'\u{C}' => push_formatted(&mut pb, cp, '\u{C}', ctx, field.as_mut(), &mut memo),
|
||||
'\u{E}' => push_formatted(&mut pb, cp, '\u{E}', ctx, field.as_mut(), &mut memo),
|
||||
'\u{1E}' => push_formatted(&mut pb, cp, '\u{2011}', ctx, field.as_mut(), &mut memo),
|
||||
'\u{1F}' => push_formatted(&mut pb, cp, '\u{AD}', ctx, field.as_mut(), &mut memo),
|
||||
// Object anchors, comment references and other special characters are dropped
|
||||
// (or handled by later passes).
|
||||
c if (c as u32) < 0x20 => {}
|
||||
c => push_formatted(&mut pb, cp, c, ctx, field.as_mut(), &mut memo),
|
||||
}
|
||||
cp = cp.saturating_add(units);
|
||||
}
|
||||
if !pb.text.is_empty() {
|
||||
let done = pb;
|
||||
out.push(ParaOut {
|
||||
para: Paragraph { text: done.text, runs: done.runs, props: done.props, mark: done.mark, objects: done.objects, ..Default::default() },
|
||||
end_cp: cp_end,
|
||||
row: None,
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Field assembly: `instr` collects everything before the separator (nested fields
|
||||
/// flattened), `sep` holds the cached result once the separator was seen.
|
||||
struct FieldBuild {
|
||||
depth: u32,
|
||||
instr: String,
|
||||
sep: Option<FieldResult>,
|
||||
}
|
||||
|
||||
/// A field's cached result: its text and, as runs, the formatting of that text. A run is added
|
||||
/// only when the formatting changes, so a result of millions of equally formatted characters
|
||||
/// costs one `CharProps`, not one per character (365 bytes each).
|
||||
#[derive(Default)]
|
||||
struct FieldResult {
|
||||
text: String,
|
||||
/// (byte length in `text`, formatting) in order; adjacent runs differ.
|
||||
runs: Vec<(usize, CharProps)>,
|
||||
}
|
||||
|
||||
impl FieldResult {
|
||||
fn push(&mut self, c: char, props: CharProps) {
|
||||
let len = c.len_utf8();
|
||||
match self.runs.last_mut() {
|
||||
Some((n, last)) if *last == props => *n += len,
|
||||
_ => self.runs.push((len, props)),
|
||||
}
|
||||
self.text.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// Bytes a clone of `obj` copies: the object itself plus a picture's alt text, a group with all
|
||||
/// its members (#3710).
|
||||
fn object_cost(obj: &InlineObject) -> u64 {
|
||||
let shallow = std::mem::size_of::<InlineObject>() as u64;
|
||||
match obj {
|
||||
InlineObject::Image { alt, .. } => shallow.saturating_add(alt.len() as u64),
|
||||
InlineObject::Group { children, .. } => children.iter().fold(shallow, |n, c| n.saturating_add(object_cost(&c.obj))),
|
||||
_ => shallow,
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a finished field: HYPERLINK keeps its result text as a linked run; every other
|
||||
/// field becomes one inline object with its cached result.
|
||||
///
|
||||
/// The target is copied once per run of equally formatted result text, not per character,
|
||||
/// and each copy is charged to `work` ([`MAX_WORK`]); once that is spent the rest of the
|
||||
/// result is kept as plain text (#3708).
|
||||
fn finish_field(pb: &mut ParaBuild, f: FieldBuild, work: &Work) {
|
||||
let instr = f.instr.trim().to_string();
|
||||
let result = f.sep.unwrap_or_default();
|
||||
if let Some(target) = hyperlink_target(&instr) {
|
||||
let cost = target.len() as u64;
|
||||
let mut chars = result.text.chars();
|
||||
for (len, props) in result.runs {
|
||||
let mut linked = props;
|
||||
if work.charge(cost, Cost::LinkTarget) {
|
||||
linked.link = Some(target.clone());
|
||||
}
|
||||
let mut left = len;
|
||||
let Some(first) = chars.next() else { break };
|
||||
pb.push(first, linked);
|
||||
left = left.saturating_sub(first.len_utf8());
|
||||
while left > 0 {
|
||||
let Some(c) = chars.next() else { break };
|
||||
pb.extend(c);
|
||||
left = left.saturating_sub(c.len_utf8());
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
pb.push('\u{FFFC}', CharProps::default());
|
||||
pb.objects.push(InlineObject::Field { instr, result: result.text, locked: false, dirty: false, code: false, form: String::new() });
|
||||
}
|
||||
|
||||
/// `sprmCPicLocation` from a CHPX grpprl: the fc of the picture in the Data stream.
|
||||
fn pic_location(chpx: &[u8]) -> Option<u32> {
|
||||
for prl in sprm::iter(chpx) {
|
||||
if prl.op == C_PIC_LOCATION {
|
||||
return match prl.operand {
|
||||
[b0, b1, b2, b3, ..] => Some(u32::from_le_bytes([*b0, *b1, *b2, *b3])),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// `sprmCSymbol` from a CHPX grpprl: the font table index and the character code of a symbol
|
||||
/// character ([MS-DOC] §2.6.1, CSymbolOperand).
|
||||
fn symbol_operand(chpx: &[u8]) -> Option<(u16, u16)> {
|
||||
for prl in sprm::iter(chpx) {
|
||||
if prl.op == C_SYMBOL
|
||||
&& let [f0, f1, c0, c1, ..] = prl.operand
|
||||
{
|
||||
return Some((u16::from_le_bytes([*f0, *f1]), u16::from_le_bytes([*c0, *c1])));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// `sprmPIlfo` operand (a signed 16-bit value, [MS-DOC] §2.6.2) → the num id, if the
|
||||
/// paragraph is in a list. 0x0001-0x07FE is a 1-based LFO index; 0xF802-0xFFFF (-2046..=-1)
|
||||
/// is the negation of one and keeps the paragraph's own indents, which we do by leaving the
|
||||
/// level's indents out of the paragraph (they never enter `ParaProps` anyway). 0 and 0xF801
|
||||
/// mean "not in a list".
|
||||
fn num_of(ilfo: i32) -> Option<u32> {
|
||||
match ilfo {
|
||||
0x0001..=0x07FE | -0x07FE..=-1 => Some(ilfo.unsigned_abs()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve the character properties for the character at `cp`: the CHPX grpprl of its FC,
|
||||
/// plus the piece's Prm, over the paragraph style's base character props. Inside a field,
|
||||
/// the character is recorded into the instruction or the cached result instead of (or as
|
||||
/// well as) the visible text.
|
||||
fn push_formatted<'a>(pb: &mut ParaBuild, cp: u32, c: char, ctx: &WalkCtx<'a>, field: Option<&mut FieldBuild>, memo: &mut Option<FmtMemo<'a>>) {
|
||||
let fc = ctx.pieces.fc_of_cp(cp).unwrap_or(0);
|
||||
let grpprl = ctx.chpx_bins.chpx(ctx.word, fc);
|
||||
let prm = ctx.pieces.prm_of(cp);
|
||||
let style = pb.props.style.as_deref();
|
||||
let mut props = match memo.as_ref().filter(|m| std::ptr::eq(m.chpx, grpprl) && m.prm == prm && m.style.as_deref() == style) {
|
||||
Some(m) => m.props.clone(),
|
||||
None => {
|
||||
let default;
|
||||
let base = match style.and_then(|id| ctx.sheet.get(id)) {
|
||||
Some(st) => &st.chr,
|
||||
None => {
|
||||
default = CharProps::default();
|
||||
&default
|
||||
}
|
||||
};
|
||||
let mut props = char_props(grpprl, ctx.fonts, base);
|
||||
apply_prm(&mut props, ctx.pieces.piece_prm(cp), ctx.fonts, base, ctx.work);
|
||||
*memo = Some(FmtMemo { chpx: grpprl, prm, style: style.map(str::to_owned), props: props.clone() });
|
||||
props
|
||||
}
|
||||
};
|
||||
// A symbol character (Insert › Symbol) is stored as the placeholder U+0028 with
|
||||
// sprmCSymbol carrying the font and the character code ([MS-DOC] §2.6.1).
|
||||
let mut c = c;
|
||||
if c == '('
|
||||
&& let Some((ftc, xchar)) = symbol_operand(grpprl)
|
||||
{
|
||||
if let Some(font) = ctx.fonts.get(usize::from(ftc)) {
|
||||
props.font = Some(font.clone());
|
||||
}
|
||||
if let Some(sym) = char::from_u32(u32::from(xchar)) {
|
||||
c = sym;
|
||||
}
|
||||
}
|
||||
match field {
|
||||
Some(f) => match f.sep.as_mut() {
|
||||
// Inside a field, characters are recorded for the field object instead of shown;
|
||||
// `finish_field` emits the visible text (hyperlink runs or one object anchor).
|
||||
Some(result) => result.push(c, props),
|
||||
None => f.instr.push(c),
|
||||
},
|
||||
None => pb.push(c, props),
|
||||
}
|
||||
}
|
||||
|
||||
/// The last character formatting resolved, reused while the CHPX, the piece's Prm and the
|
||||
/// paragraph style stay the same, so a Prm grpprl (up to 64 KB) is interpreted once per
|
||||
/// formatting change, not once per character (#3692).
|
||||
struct FmtMemo<'a> {
|
||||
/// The CHPX grpprl, compared by address: it is a slice of the WordDocument stream.
|
||||
chpx: &'a [u8],
|
||||
prm: u16,
|
||||
style: Option<String>,
|
||||
props: CharProps,
|
||||
}
|
||||
|
||||
/// Apply a piece's Prm (Prm0 short sprm or Prm1 grpprl) to character properties. A Prm1
|
||||
/// grpprl is charged to `work`; once that is spent the grpprl is skipped.
|
||||
fn apply_prm(props: &mut CharProps, prm: PrmRef<'_>, fonts: &[String], base: &CharProps, work: &Work) {
|
||||
match prm {
|
||||
PrmRef::None => {}
|
||||
PrmRef::Sprm0 { isprm, val } => {
|
||||
if let Some(op) = sprm::from_prm0(isprm) {
|
||||
let operand = [val];
|
||||
let prl = Prl { op, operand: &operand };
|
||||
if prl.sgc() == 2 {
|
||||
fmt::apply_char(props, &prl, base, fonts);
|
||||
}
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl(Some(g)) => {
|
||||
if !work.charge(g.len() as u64, Cost::Formatting) {
|
||||
return;
|
||||
}
|
||||
for prl in sprm::iter(g) {
|
||||
if prl.sgc() == 2 {
|
||||
fmt::apply_char(props, &prl, base, fonts);
|
||||
}
|
||||
}
|
||||
}
|
||||
PrmRef::Grpprl(None) => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Character props from a CHPX grpprl applied over `base`.
|
||||
fn char_props(grpprl: &[u8], fonts: &[String], base: &CharProps) -> CharProps {
|
||||
let mut p = base.clone();
|
||||
for prl in sprm::iter(grpprl) {
|
||||
if prl.sgc() == 2 {
|
||||
fmt::apply_char(&mut p, &prl, base, fonts);
|
||||
}
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
/// Read a root-level stream fully, or `None` if it is absent.
|
||||
fn stream<F: Read + Seek>(comp: &mut cfb::CompoundFile<F>, name: &str) -> Option<Vec<u8>> {
|
||||
let mut s = comp.open_stream(format!("/{name}")).ok()?;
|
||||
let mut v = Vec::new();
|
||||
// Read with a cap one byte above the limit so oversized streams are still caught by the
|
||||
// caller instead of exhausting memory here.
|
||||
s.by_ref().take(MAX_STREAM + 1).read_to_end(&mut v).ok()?;
|
||||
Some(v)
|
||||
/// Read a document that may be password-protected, preserving the shared reader's diagnostics.
|
||||
pub fn read_with_password(bytes: &[u8], password: Option<&str>) -> Result<wordcraft_doc::Document, DocbinError> {
|
||||
let shared = craft_doc_binary::read_with_password(bytes, password)?;
|
||||
from_shared_model(shared)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,302 +0,0 @@
|
||||
//! Lists: `PlfLst` (the LSTF definitions with their LVLs appended) and `PlfLfo` (the LFO
|
||||
//! instances with LFOLVL overrides) resolved into the numbering model
|
||||
//! ([MS-DOC] §2.8.1 "Determining List Formatting of a Paragraph", §2.9).
|
||||
|
||||
use wordcraft_doc::numbering::{AbstractNum, Level, LevelSuffix, Num, Numbering};
|
||||
use wordcraft_doc::props::{Align, CharProps, ParaProps};
|
||||
use wordcraft_doc::section::NumFormat;
|
||||
|
||||
use crate::fib::{Fib, pair};
|
||||
use crate::fmt;
|
||||
use crate::sprm;
|
||||
|
||||
/// Lists (and therefore LFOs) we are willing to read.
|
||||
const MAX_LISTS: usize = 2_000;
|
||||
/// Override levels per LFO ([MS-DOC] LFOLVL count is at most 9 by construction).
|
||||
const MAX_LFO_LVL: usize = 9;
|
||||
|
||||
/// One list definition: an LSTF plus its levels (one if `fSimpleList`, else nine).
|
||||
struct ListDef {
|
||||
lsid: i32,
|
||||
levels: Vec<RawLvl>,
|
||||
}
|
||||
|
||||
/// One LVL, as stored ([MS-DOC] §2.9.182): LVLF fields, the two grpprls and the number text.
|
||||
struct RawLvl {
|
||||
start: u32,
|
||||
nfc: u8,
|
||||
jc: u8,
|
||||
flags: u8,
|
||||
ixch_follow: u8,
|
||||
rgbxch_nums: [u8; 9],
|
||||
papx: Vec<u8>,
|
||||
chpx: Vec<u8>,
|
||||
/// The number text with each placeholder left as its raw char.
|
||||
xst: Vec<u16>,
|
||||
}
|
||||
|
||||
/// A bullet level's character, with the 0xF000 flag bits stripped and the common
|
||||
/// Symbol/Wingdings glyphs mapped to their Unicode equivalents so every font renders them.
|
||||
fn bullet_char(c: u16) -> char {
|
||||
match c & 0x0FFF {
|
||||
0xB7 => '\u{2022}', // Symbol bullet
|
||||
0xA7 => '\u{25AA}', // Wingdings small square
|
||||
0x6F => '\u{25CB}', // Symbol open circle
|
||||
0x77 | 0x78 => '\u{25CF}', // Wingdings filled circles
|
||||
other => char::from_u32(other as u32).unwrap_or('\u{2022}'),
|
||||
}
|
||||
}
|
||||
|
||||
/// MSONFC → model number format ([MS-OSHARED] §2.2.1.3; 0x17 = bullets, 0xFF = none). Codes
|
||||
/// 0x00 to 0x27 run in the order of ST_NumberFormat (decimal, upperRoman … 0x16 decimalZero, 0x17
|
||||
/// bullet … 0x25 chineseCounting, 0x26 chineseLegalSimplified, 0x27 chineseCountingThousand);
|
||||
/// later codes are drawn as decimal.
|
||||
fn num_format(nfc: u8) -> NumFormat {
|
||||
match nfc {
|
||||
0xFF => NumFormat::None,
|
||||
0x00..=0x27 => NumFormat::ALL.get(nfc as usize).copied().unwrap_or(NumFormat::Decimal),
|
||||
_ => NumFormat::Decimal,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn number_format_codes() {
|
||||
assert_eq!(num_format(0x04), NumFormat::LowerLetter);
|
||||
assert_eq!(num_format(0x16), NumFormat::DecimalZero);
|
||||
assert_eq!(num_format(0x17), NumFormat::Bullet);
|
||||
assert_eq!(num_format(0x25), NumFormat::ChineseCounting);
|
||||
assert_eq!(num_format(0x27), NumFormat::ChineseCountingThousand);
|
||||
assert_eq!(num_format(0x60), NumFormat::Decimal);
|
||||
}
|
||||
|
||||
/// Read the lists of a document; empty when the FIB carries none (or they are unreadable).
|
||||
pub(crate) fn parse(table: &[u8], fib: &Fib, fonts: &[String]) -> Numbering {
|
||||
let defs = match fib.pair(pair::PLF_LST) {
|
||||
Some((fc, _)) => parse_plf_lst(table, fc),
|
||||
None => Vec::new(),
|
||||
};
|
||||
if defs.is_empty() {
|
||||
return Numbering::default();
|
||||
}
|
||||
let mut numbering = Numbering::default();
|
||||
for (i, def) in defs.iter().enumerate() {
|
||||
numbering.abstracts.push(AbstractNum {
|
||||
id: i as u32,
|
||||
name: None,
|
||||
levels: def.levels.iter().enumerate().map(|(j, l)| level_of(l, j, fonts)).collect(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
if let Some((fc, lcb)) = fib.pair(pair::PLF_LFO)
|
||||
&& lcb > 0
|
||||
{
|
||||
parse_plf_lfo(table, fc, &defs, &mut numbering, fonts);
|
||||
}
|
||||
numbering
|
||||
}
|
||||
|
||||
/// `PlfLst`: cLst u16, then cLst × LSTF (28 bytes each); the LVLs follow the whole array,
|
||||
/// one per simple list and nine per multi-level list, in LSTF order. `lcbPlfLst` covers only
|
||||
/// the LSTFs, so the LVLs are read past it, bounded by the stream.
|
||||
fn parse_plf_lst(table: &[u8], fc: u32) -> Vec<ListDef> {
|
||||
let at = fc as usize;
|
||||
let Some(c_lst) = table.get(at..at.saturating_add(2)).map(|b| u16::from_le_bytes([b[0], b[1]])) else { return Vec::new() };
|
||||
let c_lst = c_lst as usize;
|
||||
if c_lst == 0 || c_lst > MAX_LISTS {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut defs = Vec::with_capacity(c_lst);
|
||||
// LSTF: lsid i32, tplc, rgistdPara[9], flags byte (bit 0 = fSimpleList), grfhic.
|
||||
let mut counts = Vec::with_capacity(c_lst);
|
||||
for i in 0..c_lst {
|
||||
let f = at.saturating_add(2).saturating_add(i.saturating_mul(28));
|
||||
let lsid = table.get(f..f.saturating_add(4)).map(|b| i32::from_le_bytes([b[0], b[1], b[2], b[3]])).unwrap_or(-1);
|
||||
let simple = table.get(f.saturating_add(26)).copied().unwrap_or(0) & 1 != 0;
|
||||
defs.push(ListDef { lsid, levels: Vec::new() });
|
||||
counts.push(if simple { 1 } else { 9 });
|
||||
}
|
||||
let mut off = at.saturating_add(2).saturating_add(c_lst.saturating_mul(28));
|
||||
for (def, n) in defs.iter_mut().zip(counts) {
|
||||
for _ in 0..n {
|
||||
match parse_lvl(table, off) {
|
||||
Some((lvl, len)) => {
|
||||
def.levels.push(lvl);
|
||||
off = off.saturating_add(len);
|
||||
}
|
||||
None => return defs,
|
||||
}
|
||||
}
|
||||
}
|
||||
defs
|
||||
}
|
||||
|
||||
/// One LVL ([MS-DOC] §2.9.182): LVLF (28 bytes) + grpprlPapx + grpprlChpx + Xst.
|
||||
/// Returns the level and its total length in bytes.
|
||||
fn parse_lvl(table: &[u8], at: usize) -> Option<(RawLvl, usize)> {
|
||||
let lvlf = table.get(at..at.saturating_add(28))?;
|
||||
let start = u32::from_le_bytes([lvlf[0], lvlf[1], lvlf[2], lvlf[3]]);
|
||||
let nfc = lvlf[4];
|
||||
// Byte 5 packs jc (bits 0-1), fLegal (bit 2), fNoRestart (bit 3), …
|
||||
let jc_flags = lvlf[5];
|
||||
let mut rgbxch_nums = [0u8; 9];
|
||||
rgbxch_nums.copy_from_slice(&lvlf[6..15]);
|
||||
let ixch_follow = lvlf[15];
|
||||
let cb_chpx = lvlf[24] as usize;
|
||||
let cb_papx = lvlf[25] as usize;
|
||||
let mut at = at.saturating_add(28);
|
||||
// Field order in an LVL: lvlf, grpprlPapx, grpprlChpx, then the Xst.
|
||||
let papx = table.get(at..at.saturating_add(cb_papx)).unwrap_or(&[]).to_vec();
|
||||
at = at.saturating_add(cb_papx);
|
||||
let chpx = table.get(at..at.saturating_add(cb_chpx)).unwrap_or(&[]).to_vec();
|
||||
at = at.saturating_add(cb_chpx);
|
||||
let cch = table.get(at..at.saturating_add(2)).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0);
|
||||
let cch = cch.min(64) as usize;
|
||||
let chars = table.get(at.saturating_add(2)..at.saturating_add(2).saturating_add(cch * 2))?;
|
||||
let xst: Vec<u16> = chars.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes([b[0], b[1]])).collect();
|
||||
let len = 28 + cb_chpx + cb_papx + 2 + cch * 2;
|
||||
Some((RawLvl { start: start.min(0x7FFF), nfc, jc: jc_flags & 3, flags: jc_flags, ixch_follow, rgbxch_nums, papx, chpx, xst }, len))
|
||||
}
|
||||
|
||||
/// `PlfLfo`: lfoMac u32, then lfoMac × LFO (16 bytes), then lfoMac × LFOData in parallel.
|
||||
/// Each LFOData is a CP (skipped) followed by clfolvl × LFOLVL (8 bytes + optional LVL).
|
||||
fn parse_plf_lfo(table: &[u8], fc: u32, defs: &[ListDef], numbering: &mut Numbering, fonts: &[String]) {
|
||||
let at = fc as usize;
|
||||
let Some(lfo_mac) = table.get(at..at.saturating_add(4)).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) else { return };
|
||||
let lfo_mac = lfo_mac.min(MAX_LISTS as u32) as usize;
|
||||
let mut lsids = Vec::with_capacity(lfo_mac);
|
||||
let mut clfolvls = Vec::with_capacity(lfo_mac);
|
||||
for i in 0..lfo_mac {
|
||||
let f = at.saturating_add(4).saturating_add(i.saturating_mul(16));
|
||||
let lsid = table.get(f..f.saturating_add(4)).map(|b| i32::from_le_bytes([b[0], b[1], b[2], b[3]])).unwrap_or(-1);
|
||||
let clfolvl = table.get(f.saturating_add(12)).copied().unwrap_or(0);
|
||||
lsids.push(lsid);
|
||||
clfolvls.push(clfolvl);
|
||||
}
|
||||
let mut off = at.saturating_add(4).saturating_add(lfo_mac.saturating_mul(16));
|
||||
for (i, (&lsid, &clfolvl)) in lsids.iter().zip(&clfolvls).enumerate() {
|
||||
let Some(def) = defs.iter().position(|d| d.lsid == lsid) else {
|
||||
// Keep the LFOData walk in step even for unknown lists.
|
||||
off = skip_lfodatas(table, off, clfolvl);
|
||||
continue;
|
||||
};
|
||||
// LFOData: cp u32, then clfolvl × LFOLVL.
|
||||
let mut start_overrides = Vec::new();
|
||||
let mut overrides = Vec::new();
|
||||
off = off.saturating_add(4);
|
||||
for _ in 0..(clfolvl as usize).min(MAX_LFO_LVL) {
|
||||
let Some(hdr) = table.get(off..off.saturating_add(8)) else { break };
|
||||
let i_start = u32::from_le_bytes([hdr[0], hdr[1], hdr[2], hdr[3]]);
|
||||
let flags = hdr[4];
|
||||
let lvl = (flags & 0x0F) as usize;
|
||||
let f_start = flags & 0x10 != 0;
|
||||
let f_fmt = flags & 0x20 != 0;
|
||||
off = off.saturating_add(8);
|
||||
let mut fmt_lvl = None;
|
||||
if f_fmt {
|
||||
match parse_lvl(table, off) {
|
||||
Some((l, len)) => {
|
||||
fmt_lvl = Some(l);
|
||||
off = off.saturating_add(len);
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
if f_start {
|
||||
start_overrides.push((lvl as u8, i_start.min(0x7FFF)));
|
||||
}
|
||||
if let Some(l) = fmt_lvl {
|
||||
overrides.push((lvl, l));
|
||||
}
|
||||
}
|
||||
// Skip any LFOLVLs we did not consume (clfolvl above the cap).
|
||||
if clfolvl as usize > MAX_LFO_LVL {
|
||||
off = skip_lfodatas(table, off, clfolvl - MAX_LFO_LVL as u8);
|
||||
}
|
||||
let abstract_id = if overrides.is_empty() {
|
||||
def as u32
|
||||
} else {
|
||||
// A formatting override replaces whole levels: give the LFO its own abstract
|
||||
// built from the base levels with the overrides applied.
|
||||
let mut levels = numbering.abstracts.get(def).map(|a| a.levels.clone()).unwrap_or_default();
|
||||
for (lvl, raw) in overrides {
|
||||
if let Some(slot) = levels.get_mut(lvl) {
|
||||
*slot = level_of(&raw, lvl, fonts);
|
||||
}
|
||||
}
|
||||
let id = numbering.abstracts.len() as u32;
|
||||
numbering.abstracts.push(AbstractNum { id, name: None, levels, ..Default::default() });
|
||||
id
|
||||
};
|
||||
numbering.nums.push(Num { id: (i + 1) as u32, abstract_id, start_overrides, ..Default::default() });
|
||||
}
|
||||
}
|
||||
|
||||
/// Advance past `n` LFOLVLs whose bodies we do not need; bounded and best-effort.
|
||||
fn skip_lfodatas(table: &[u8], mut off: usize, n: u8) -> usize {
|
||||
for _ in 0..n {
|
||||
let Some(hdr) = table.get(off..off.saturating_add(8)) else { return off };
|
||||
let f_fmt = hdr[4] & 0x20 != 0;
|
||||
off = off.saturating_add(8);
|
||||
if f_fmt && let Some((_, len)) = parse_lvl(table, off) {
|
||||
off = off.saturating_add(len);
|
||||
}
|
||||
}
|
||||
off
|
||||
}
|
||||
|
||||
/// Turn a stored LVL into a model level.
|
||||
fn level_of(l: &RawLvl, lvl_index: usize, fonts: &[String]) -> Level {
|
||||
// Number text: characters listed in rgbxchNums are level placeholders (their value is
|
||||
// the zero-based level); bullet chars carry 0xF000 flag bits to strip.
|
||||
let mut text = String::new();
|
||||
for (i, &c) in l.xst.iter().enumerate() {
|
||||
if l.rgbxch_nums.contains(&(i as u8 + 1)) && c <= 8 {
|
||||
text.push('%');
|
||||
text.push((b'1' + c as u8) as char);
|
||||
} else {
|
||||
text.push(bullet_char(c));
|
||||
}
|
||||
}
|
||||
if text.is_empty() {
|
||||
text.push('\u{2022}');
|
||||
}
|
||||
// Indents from the level's grpprlPapx.
|
||||
let mut props = ParaProps::default();
|
||||
for prl in sprm::iter(&l.papx) {
|
||||
if prl.sgc() == 1 {
|
||||
fmt::apply_para(&mut props, &prl);
|
||||
}
|
||||
}
|
||||
let indent = props.indent_left.unwrap_or(18.0 * (lvl_index as f32 + 1.0)).clamp(0.0, 1000.0);
|
||||
let hanging = props.indent_first.map(|f| (-f).clamp(0.0, 1000.0)).unwrap_or(18.0);
|
||||
let mut chr = CharProps::default();
|
||||
let base = chr.clone();
|
||||
for prl in sprm::iter(&l.chpx) {
|
||||
if prl.sgc() == 2 {
|
||||
fmt::apply_char(&mut chr, &prl, &base, fonts);
|
||||
}
|
||||
}
|
||||
Level {
|
||||
start: l.start.max(1),
|
||||
format: num_format(l.nfc),
|
||||
text,
|
||||
align: match l.jc {
|
||||
1 => Align::Center,
|
||||
2 => Align::Right,
|
||||
_ => Align::Left,
|
||||
},
|
||||
indent,
|
||||
hanging,
|
||||
suffix: match l.ixch_follow {
|
||||
1 => LevelSuffix::Space,
|
||||
2 => LevelSuffix::Nothing,
|
||||
_ => LevelSuffix::Tab,
|
||||
},
|
||||
chr,
|
||||
restart: l.flags & 0x08 == 0,
|
||||
legal: l.flags & 0x04 != 0,
|
||||
style: None,
|
||||
..Level::default()
|
||||
}
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
//! Inline pictures: `sprmCPicLocation` → a `PICF` in the Data stream → embedded image bytes
|
||||
//! ([MS-DOC] §2.9.190 PICF, §1.3.5 Pictures), and the shared store of every picture the
|
||||
//! document reads (inline and floating).
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::officeart;
|
||||
|
||||
/// `PICF.cbHeader` — the fixed size of the PICF structure.
|
||||
const CB_HEADER: usize = 0x44;
|
||||
/// Offsets inside PICF: `PICMID.dxaGoal/dyaGoal/mx/my` start at 28.
|
||||
const OFF_PICMID: usize = 28;
|
||||
|
||||
/// Most distinct pictures one document materialises from the Data stream.
|
||||
const MAX_PICTURES: usize = 5_000;
|
||||
/// Most picture bytes one document copies out of the Data stream, all pictures together.
|
||||
pub(crate) const MAX_MEDIA_BYTES: usize = 256 << 20;
|
||||
/// Most bytes one document decodes pictures into (metafiles inflated, DIB pixels), charged
|
||||
/// per attempt whether it succeeds or not: retained media is capped separately, by
|
||||
/// `MAX_MEDIA_BYTES` (#3711, #3712).
|
||||
const MAX_DECODE_WORK: usize = 512 << 20;
|
||||
/// The decode budget grows with the input: up to this many bytes per byte of the WordDocument
|
||||
/// and Data streams. An uncompressed DIB declares at most 32 bytes of pixels per file byte (1
|
||||
/// bit per pixel, 4 bytes charged each), so 64 passes any such picture. RLE8/RLE4 bitmaps
|
||||
/// reach about 510 times that and rely on the `MAX_DECODE_WORK` floor instead; a decompression
|
||||
/// bomb (ratios around 1e6) still hits the cap.
|
||||
const DECODE_WORK_PER_INPUT_BYTE: usize = 64;
|
||||
/// The most the decode budget grows to, whatever the input.
|
||||
const MAX_DECODE_WORK_CEILING: usize = 2 << 30;
|
||||
|
||||
/// The decode budget for a document whose pictures live in `input` stream bytes, never less
|
||||
/// than `floor`.
|
||||
pub(crate) fn decode_work(input: usize, floor: usize) -> usize {
|
||||
input.saturating_mul(DECODE_WORK_PER_INPUT_BYTE).clamp(floor, floor.max(MAX_DECODE_WORK_CEILING))
|
||||
}
|
||||
|
||||
/// The document's pictures, keyed by their location in the Data stream: anchors that share a
|
||||
/// `fc_pic` share one media entry (or one failure), and the total bytes copied are capped, so a
|
||||
/// file with thousands of anchors on one huge picture costs one copy, not thousands. Pictures of
|
||||
/// the drawing layer's BLIP store are shared the same way, by store index.
|
||||
pub(crate) struct Pictures {
|
||||
/// fc_pic → the inline picture already materialised (`None`: unreadable or over the cap,
|
||||
/// not retried).
|
||||
by_fc: HashMap<u32, Option<Inline>>,
|
||||
/// BLIP store index → media key (`None`: unreadable, not retried).
|
||||
by_bse: HashMap<u32, Option<String>>,
|
||||
/// (stream, foDelay, record length) of a BLIP record outside the store → media key: store
|
||||
/// entries and inline pictures that alias one record share one decode (`None`: failed, not
|
||||
/// retried). The stream is told apart by its address.
|
||||
by_delay: HashMap<(usize, usize, usize), Option<String>>,
|
||||
/// The media entries, merged into the document at the end.
|
||||
pub(crate) media: BTreeMap<String, Arc<Vec<u8>>>,
|
||||
total: usize,
|
||||
/// The byte cap (`MAX_MEDIA_BYTES`; smaller in tests).
|
||||
pub(crate) limit: usize,
|
||||
/// Decode work left (`MAX_DECODE_WORK`; smaller in tests).
|
||||
pub(crate) work: officeart::Decode,
|
||||
/// Anchors whose picture was dropped (only the first few are logged).
|
||||
dropped: usize,
|
||||
/// Pictures kept as placeholders (formats we can't draw).
|
||||
placeholders: usize,
|
||||
}
|
||||
|
||||
impl Default for Pictures {
|
||||
fn default() -> Self {
|
||||
Pictures {
|
||||
by_fc: HashMap::new(),
|
||||
by_bse: HashMap::new(),
|
||||
by_delay: HashMap::new(),
|
||||
media: BTreeMap::new(),
|
||||
total: 0,
|
||||
limit: MAX_MEDIA_BYTES,
|
||||
work: officeart::Decode::new(MAX_DECODE_WORK),
|
||||
dropped: 0,
|
||||
placeholders: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An inline picture: its media key, display size (points), crop fractions and alt text.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Inline {
|
||||
pub(crate) key: String,
|
||||
pub(crate) w: f32,
|
||||
pub(crate) h: f32,
|
||||
pub(crate) crop: [f32; 4],
|
||||
pub(crate) alt: String,
|
||||
}
|
||||
|
||||
impl Pictures {
|
||||
/// An empty store whose decode budget scales with the `input` stream bytes (WordDocument
|
||||
/// plus Data) the pictures can come from.
|
||||
pub(crate) fn for_input(input: usize) -> Self {
|
||||
Pictures { work: officeart::Decode::new(decode_work(input, MAX_DECODE_WORK)), ..Pictures::default() }
|
||||
}
|
||||
|
||||
/// The inline picture at `fc_pic` in the Data stream, copying its bytes the first time.
|
||||
/// `None` when the header is malformed, the picture can't be read, or a picture
|
||||
/// count/byte cap is reached.
|
||||
pub(crate) fn get(&mut self, data: &[u8], fc_pic: u32) -> Option<&Inline> {
|
||||
if !self.by_fc.contains_key(&fc_pic) {
|
||||
if self.by_fc.len().saturating_add(self.by_bse.len()) >= MAX_PICTURES {
|
||||
return None;
|
||||
}
|
||||
let pic = self.read(data, fc_pic);
|
||||
self.by_fc.insert(fc_pic, pic);
|
||||
}
|
||||
self.by_fc.get(&fc_pic)?.as_ref()
|
||||
}
|
||||
|
||||
/// Read the inline picture at `fc_pic`: nothing is decoded or copied beyond what is left of
|
||||
/// the byte cap, and nothing at all once it is used up.
|
||||
fn read(&mut self, data: &[u8], fc_pic: u32) -> Option<Inline> {
|
||||
let room = self.limit.saturating_sub(self.total);
|
||||
if room == 0 {
|
||||
return None;
|
||||
}
|
||||
match locate(data, fc_pic, self)? {
|
||||
Found::Range(pic) => {
|
||||
let bytes = data.get(pic.range.clone()).filter(|b| b.len() <= room)?.to_vec();
|
||||
let key = self.insert(bytes, pic.ext, false)?;
|
||||
Some(Inline { key, w: pic.w, h: pic.h, crop: [0.0; 4], alt: String::new() })
|
||||
}
|
||||
Found::Shape { w, h, key, props } => Some(Inline { key: key?, w, h, crop: props.crop, alt: props.alt }),
|
||||
}
|
||||
}
|
||||
|
||||
/// The media key of BLIP store entry `pib` (1-based), decoding it the first time; `delay`
|
||||
/// is the stream that holds BLIPs not embedded in the store (the WordDocument stream).
|
||||
pub(crate) fn bse(&mut self, pib: u32, bstore: &[&[u8]], delay: &[u8]) -> Option<String> {
|
||||
if let Some(hit) = self.by_bse.get(&pib) {
|
||||
return hit.clone();
|
||||
}
|
||||
if self.by_fc.len().saturating_add(self.by_bse.len()) >= MAX_PICTURES {
|
||||
return None;
|
||||
}
|
||||
let fbse = bstore.get((pib as usize).checked_sub(1)?)?;
|
||||
let key = self.decode_bse(fbse, delay);
|
||||
self.by_bse.insert(pib, key.clone());
|
||||
key
|
||||
}
|
||||
|
||||
/// Decode a store entry's BLIP, once per delay-stream record however many entries name it.
|
||||
fn decode_bse(&mut self, fbse: &[u8], delay: &[u8]) -> Option<String> {
|
||||
let (rec, at) = officeart::fbse_record(fbse, delay)?;
|
||||
let at = at.map(|(fo, used)| (delay.as_ptr() as usize, fo, used));
|
||||
if let Some(hit) = at.and_then(|at| self.by_delay.get(&at)) {
|
||||
return hit.clone();
|
||||
}
|
||||
let room = self.limit.saturating_sub(self.total);
|
||||
let key = officeart::blip(&rec, room, &mut self.work).and_then(|b| self.insert(b.bytes, b.ext, b.placeholder));
|
||||
if let Some(at) = at {
|
||||
self.by_delay.insert(at, key.clone());
|
||||
}
|
||||
key
|
||||
}
|
||||
|
||||
/// Add picture bytes as a new media entry, within the byte cap.
|
||||
fn insert(&mut self, bytes: Vec<u8>, ext: &str, placeholder: bool) -> Option<String> {
|
||||
let total = self.total.checked_add(bytes.len()).filter(|&t| t <= self.limit)?;
|
||||
let key = format!("image{}.{ext}", self.media.len() + 1);
|
||||
if placeholder {
|
||||
if self.placeholders < 10 {
|
||||
log::warn!("docbin: picture `{key}` is in a format WordCraft can't draw ({ext}); it shows as a placeholder");
|
||||
}
|
||||
self.placeholders = self.placeholders.saturating_add(1);
|
||||
}
|
||||
self.media.insert(key.clone(), Arc::new(bytes));
|
||||
self.total = total;
|
||||
Some(key)
|
||||
}
|
||||
|
||||
/// Note a picture anchor that could not be materialised.
|
||||
pub(crate) fn dropped(&mut self, cp: u32) {
|
||||
if self.dropped < 10 {
|
||||
log::warn!("docbin: picture at CP {cp} could not be read (or a picture cap was reached) and was dropped");
|
||||
}
|
||||
self.dropped = self.dropped.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a picture's image bytes sit in the Data stream, and its display size in points.
|
||||
struct Located {
|
||||
w: f32,
|
||||
h: f32,
|
||||
range: Range<usize>,
|
||||
ext: &'static str,
|
||||
}
|
||||
|
||||
/// What a `PICFAndOfficeArtData` holds: raw image bytes (older writers), or an
|
||||
/// `OfficeArtInlineSpContainer` (Word 97 and later) whose BLIP is decoded.
|
||||
enum Found {
|
||||
Range(Located),
|
||||
Shape { w: f32, h: f32, key: Option<String>, props: officeart::Props },
|
||||
}
|
||||
|
||||
/// `PICF.mfpf.mm` values of pictures stored as a shape (`MM_SHAPE`) or a shape with a file
|
||||
/// name (`MM_SHAPEFILE`).
|
||||
const MM_SHAPE: u16 = 0x0064;
|
||||
const MM_SHAPEFILE: u16 = 0x0066;
|
||||
|
||||
/// Locate the picture at `fc_pic` in the Data stream ([MS-DOC] §2.9.192 PICFAndOfficeArtData);
|
||||
/// the BLIP is decoded once per record and charged to the decode budget.
|
||||
fn locate(data: &[u8], fc_pic: u32, pics: &mut Pictures) -> Option<Found> {
|
||||
let base = fc_pic as usize;
|
||||
let pic = data.get(base..)?;
|
||||
let lcb = u32::from_le_bytes(pic.get(0..4)?.try_into().ok()?) as usize;
|
||||
let cb_header = u16::from_le_bytes(pic.get(4..6)?.try_into().ok()?) as usize;
|
||||
let cb_header = if (0x20..=0x100).contains(&cb_header) { cb_header } else { CB_HEADER };
|
||||
let mm = u16::from_le_bytes(pic.get(6..8)?.try_into().ok()?);
|
||||
let m = pic.get(OFF_PICMID..OFF_PICMID + 8)?;
|
||||
let dxa_goal = i16::from_le_bytes([m[0], m[1]]) as f32;
|
||||
let dya_goal = i16::from_le_bytes([m[2], m[3]]) as f32;
|
||||
let mx = u16::from_le_bytes([m[4], m[5]]) as f32;
|
||||
let my = u16::from_le_bytes([m[6], m[7]]) as f32;
|
||||
let scale = |r: f32| if r > 0.0 { (r / 1000.0).clamp(0.01, 100.0) } else { 1.0 };
|
||||
let end = lcb.min(pic.len());
|
||||
if mm == MM_SHAPE || mm == MM_SHAPEFILE {
|
||||
let mut at = cb_header;
|
||||
if mm == MM_SHAPEFILE {
|
||||
at = at.checked_add(1 + *pic.get(at)? as usize)?;
|
||||
}
|
||||
let (key, props) = inline_shape(pic.get(at..end)?, data, pics)?;
|
||||
// The goal size is before cropping; the crop fractions take their share off it.
|
||||
let [cl, ct, cr, cb] = props.crop;
|
||||
let w = (dxa_goal / 20.0 * (1.0 - cl - cr).max(0.01) * scale(mx)).clamp(1.0, 1584.0);
|
||||
let h = (dya_goal / 20.0 * (1.0 - ct - cb).max(0.01) * scale(my)).clamp(1.0, 1584.0);
|
||||
return Some(Found::Shape { w, h, key, props });
|
||||
}
|
||||
let w = (dxa_goal / 20.0 * scale(mx)).clamp(1.0, 1584.0);
|
||||
let h = (dya_goal / 20.0 * scale(my)).clamp(1.0, 1584.0);
|
||||
let body = pic.get(cb_header..end)?;
|
||||
let at = sniff(body)?;
|
||||
let image = body.get(at..)?;
|
||||
let start = base.checked_add(cb_header)?.checked_add(at)?;
|
||||
Some(Found::Range(Located { w, h, range: start..start.checked_add(image.len())?, ext: ext_of(image) }))
|
||||
}
|
||||
|
||||
/// An `OfficeArtInlineSpContainer`: the picture shape, then the `OfficeArtFBSE` records its
|
||||
/// `pib` indexes (BLIPs embedded, or at `foDelay` in the Data stream). The BLIP is decoded
|
||||
/// once per record, however many pictures (and FBSE sizes) name it.
|
||||
fn inline_shape(buf: &[u8], data: &[u8], pics: &mut Pictures) -> Option<(Option<String>, officeart::Props)> {
|
||||
let mut budget = 10_000usize;
|
||||
let recs = officeart::children(buf, &mut budget);
|
||||
let sp = recs.iter().find(|r| r.typ == officeart::rt::SP_CONTAINER).map(|r| officeart::shape(r.body, &mut budget))?;
|
||||
let fbses: Vec<&[u8]> = recs.iter().filter(|r| r.typ == officeart::rt::FBSE).map(|r| r.body).collect();
|
||||
let fbse = sp.props.pib.and_then(|p| fbses.get((p as usize).checked_sub(1)?)).or(fbses.first())?;
|
||||
let key = pics.decode_bse(fbse, data);
|
||||
Some((key, sp.props))
|
||||
}
|
||||
|
||||
/// Offset of the first embedded raster image inside the payload, skipping the small
|
||||
/// metafile header some writers put in front of PNG/JPEG data. `None` for metafiles.
|
||||
fn sniff(body: &[u8]) -> Option<usize> {
|
||||
for at in [0usize, 8, 12, 16, 20, 22, 24, 32] {
|
||||
if let Some(b) = body.get(at..)
|
||||
&& (b.starts_with(b"\x89PNG") || b.starts_with(b"\xFF\xD8\xFF") || b.starts_with(b"GIF8") || b.starts_with(b"BM"))
|
||||
{
|
||||
return Some(at);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn ext_of(b: &[u8]) -> &'static str {
|
||||
if b.starts_with(b"\x89PNG") {
|
||||
"png"
|
||||
} else if b.starts_with(b"\xFF\xD8\xFF") {
|
||||
"jpeg"
|
||||
} else if b.starts_with(b"GIF8") {
|
||||
"gif"
|
||||
} else {
|
||||
"bmp"
|
||||
}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
//! Optional document text properties from [MS-OLEPS] §2.21 and §2.25.1.
|
||||
//! Broken metadata must not prevent opening the document or allocate from unchecked sizes.
|
||||
|
||||
use std::io::{Read, Seek};
|
||||
|
||||
use wordcraft_doc::CoreProps;
|
||||
|
||||
const MAX_STREAM: u64 = 2 << 20;
|
||||
const MAX_STRING: usize = 64 << 10;
|
||||
const SUMMARY_FMTID: &[u8] = &[0xe0, 0x85, 0x9f, 0xf2, 0xf9, 0x4f, 0x68, 0x10, 0xab, 0x91, 0x08, 0x00, 0x2b, 0x27, 0xb3, 0xd9];
|
||||
|
||||
pub(crate) fn read<F: Read + Seek>(comp: &mut cfb::CompoundFile<F>) -> Option<CoreProps> {
|
||||
let stream = comp.open_stream("/\u{5}SummaryInformation").ok()?;
|
||||
if stream.len() > MAX_STREAM {
|
||||
return None;
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
stream.take(MAX_STREAM + 1).read_to_end(&mut bytes).ok()?;
|
||||
if bytes.len() as u64 > MAX_STREAM {
|
||||
return None;
|
||||
}
|
||||
parse(&bytes)
|
||||
}
|
||||
|
||||
fn u32_at(bytes: &[u8], offset: usize) -> Option<u32> {
|
||||
Some(u32::from_le_bytes(bytes.get(offset..offset.checked_add(4)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
fn parse(bytes: &[u8]) -> Option<CoreProps> {
|
||||
if bytes.get(..2)? != [0xfe, 0xff] || bytes.get(28..44)? != SUMMARY_FMTID || u32_at(bytes, 24)? < 1 {
|
||||
return None;
|
||||
}
|
||||
if u16::from_le_bytes(bytes.get(2..4)?.try_into().ok()?) > 1 {
|
||||
return None;
|
||||
}
|
||||
let offset = u32_at(bytes, 44)? as usize;
|
||||
if offset < 48 || !offset.is_multiple_of(4) {
|
||||
return None;
|
||||
}
|
||||
let size = u32_at(bytes, offset)? as usize;
|
||||
let set = bytes.get(offset..offset.checked_add(size)?)?;
|
||||
let count = u32_at(set, 4)? as usize;
|
||||
if count > 4096 {
|
||||
return None;
|
||||
}
|
||||
let table_end = 8 + count * 8;
|
||||
let table = set.get(8..table_end)?;
|
||||
// Offsets are relative to the property set. Bound each value by the next property so a
|
||||
// forged string length cannot read another property's bytes as part of the title.
|
||||
let property = |id: u32| -> Option<&[u8]> {
|
||||
let entry = table.as_chunks::<8>().0.iter().find(|entry| u32_at(entry.as_slice(), 0) == Some(id))?.as_slice();
|
||||
let start = u32_at(entry, 4)? as usize;
|
||||
if start < table_end || !start.is_multiple_of(4) {
|
||||
return None;
|
||||
}
|
||||
let end = table
|
||||
.as_chunks::<8>()
|
||||
.0
|
||||
.iter()
|
||||
.filter_map(|e| u32_at(e, 4))
|
||||
.map(|n| n as usize)
|
||||
.filter(|n| *n > start)
|
||||
.min()
|
||||
.unwrap_or(set.len())
|
||||
.min(set.len());
|
||||
set.get(start..end)
|
||||
};
|
||||
let codepage = property(1)
|
||||
.and_then(|cp| {
|
||||
let vt = u32_at(cp, 0)?;
|
||||
if vt == 2 || vt == 3 { Some(u16::from_le_bytes(cp.get(4..6)?.try_into().ok()?)) } else { None }
|
||||
})
|
||||
.unwrap_or(1252);
|
||||
let get = |id| property(id).and_then(|value| text(value, codepage)).unwrap_or_default();
|
||||
let get_filetime = |id| property(id).and_then(filetime_iso).unwrap_or_default();
|
||||
Some(CoreProps {
|
||||
title: get(2),
|
||||
subject: get(3),
|
||||
creator: get(4),
|
||||
keywords: get(5),
|
||||
description: get(6),
|
||||
last_modified_by: get(8),
|
||||
revision: get(9).parse().unwrap_or_default(),
|
||||
created: get_filetime(12),
|
||||
modified: get_filetime(13),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// A `VT_FILETIME` property value as an ISO-8601 UTC string (`YYYY-MM-DDTHH:MM:SSZ`), or `None`
|
||||
/// if it is not a FILETIME, is zero, or predates the 1601 FILETIME epoch.
|
||||
fn filetime_iso(value: &[u8]) -> Option<String> {
|
||||
let kind = u32_at(value, 0)?;
|
||||
if kind != 64 {
|
||||
return None;
|
||||
}
|
||||
let ft_low = u32_at(value, 4)? as u64;
|
||||
let ft_high = u32_at(value, 8)? as u64;
|
||||
let ft = (ft_high << 32) | ft_low;
|
||||
if ft == 0 {
|
||||
return None;
|
||||
}
|
||||
const EPOCH_DIFFERENCE: u64 = 116_444_736_000_000_000;
|
||||
let unix_100ns = ft.checked_sub(EPOCH_DIFFERENCE)?;
|
||||
let unix_secs = i64::try_from(unix_100ns / 10_000_000).ok()?;
|
||||
let (days, sod) = (unix_secs.div_euclid(86_400), unix_secs.rem_euclid(86_400));
|
||||
let (h, mi, s) = (sod / 3600, sod / 60 % 60, sod % 60);
|
||||
let (y, m, d) = civil(days);
|
||||
// `{y:04}` rather than `%Y`, which writes a `+` before years past 9999.
|
||||
Some(format!("{y:04}-{m:02}-{d:02}T{h:02}:{mi:02}:{s:02}Z"))
|
||||
}
|
||||
|
||||
/// Days since 1970-01-01 → proleptic Gregorian (year, month, day).
|
||||
///
|
||||
/// A FILETIME can land past `NaiveDate`'s ±262143-year range, where the old hand-rolled converter
|
||||
/// still produced a date; dropping to `None` there would change the output. The Gregorian calendar
|
||||
/// repeats every 400 years (146_097 days), so chrono converts the day within one cycle and whole
|
||||
/// cycles go back into the year (an `i128` so the extremes can't overflow), keeping chrono as the
|
||||
/// only calendar logic and reproducing the old result exactly. 1970-01-01 is `num_days_from_ce` 719_163.
|
||||
fn civil(days: i64) -> (i64, u32, u32) {
|
||||
use chrono::Datelike;
|
||||
let (cycles, rest) = (days.div_euclid(146_097), days.rem_euclid(146_097));
|
||||
// `rest + 719_163` is 719_163..865_259, always a valid CE day.
|
||||
match i32::try_from(rest + 719_163).ok().and_then(chrono::NaiveDate::from_num_days_from_ce_opt) {
|
||||
Some(d) => {
|
||||
let y = i128::from(d.year()) + i128::from(cycles) * 400;
|
||||
(i64::try_from(y).unwrap_or(if y < 0 { i64::MIN } else { i64::MAX }), d.month(), d.day())
|
||||
}
|
||||
None => (1970, 1, 1), // unreachable
|
||||
}
|
||||
}
|
||||
|
||||
fn text(value: &[u8], codepage: u16) -> Option<String> {
|
||||
let kind = u32_at(value, 0)?;
|
||||
let count = u32_at(value, 4)? as usize;
|
||||
let size = match kind {
|
||||
30 => count, // CodePageString length is in bytes, even for code page 1200.
|
||||
31 => count.checked_mul(2)?, // UnicodeString length is in UTF-16 units.
|
||||
_ => return None,
|
||||
};
|
||||
if size > MAX_STRING {
|
||||
return None;
|
||||
}
|
||||
let bytes = value.get(8..8usize.checked_add(size)?)?;
|
||||
if kind == 31 || codepage == 1200 {
|
||||
if !bytes.len().is_multiple_of(2) {
|
||||
return None;
|
||||
}
|
||||
let units = bytes.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes([b[0], b[1]])).take_while(|u| *u != 0);
|
||||
Some(char::decode_utf16(units).map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER)).collect())
|
||||
} else {
|
||||
let bytes = bytes.split(|b| *b == 0).next().unwrap_or_default();
|
||||
if codepage == 65001 {
|
||||
Some(String::from_utf8_lossy(bytes).into_owned())
|
||||
} else if wordcraft_doc::encoding::SINGLE_BYTE_CODE_PAGES.contains(&codepage) {
|
||||
Some(bytes.iter().map(|b| wordcraft_doc::encoding::windows_byte(codepage, *b)).collect())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::filetime_iso;
|
||||
|
||||
/// A `FILETIME` property (VT_FILETIME = 64) as `YYYY-MM-DDTHH:MM:SSZ`. The values are pinned
|
||||
/// to what the old hand-rolled `civil` produced.
|
||||
fn value(ft: u64) -> Vec<u8> {
|
||||
let mut v = 64u32.to_le_bytes().to_vec();
|
||||
v.extend_from_slice(&((ft & 0xffff_ffff) as u32).to_le_bytes());
|
||||
v.extend_from_slice(&((ft >> 32) as u32).to_le_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filetime_to_iso() {
|
||||
// 2020-01-01T00:00:00Z and the same day at 13:45:09.
|
||||
assert_eq!(filetime_iso(&value(132_223_104_000_000_000)).as_deref(), Some("2020-01-01T00:00:00Z"));
|
||||
assert_eq!(filetime_iso(&value(132_223_599_090_000_000)).as_deref(), Some("2020-01-01T13:45:09Z"));
|
||||
// Year 10000 keeps the plain zero-padded year (no leading `+` that chrono's `%Y` would add).
|
||||
assert_eq!(filetime_iso(&value(2_650_467_744_000_000_000)).as_deref(), Some("10000-01-01T00:00:00Z"));
|
||||
// The largest FILETIME (u64::MAX) lands in year 60056: past year 9999 (so the plain year
|
||||
// matters) and the old `civil` converted it without ever dropping to the epoch. chrono's
|
||||
// own range (year 262143) isn't reachable here — a u64 FILETIME caps at unix seconds
|
||||
// ~1.83e12 — but the 400-year cycle keeps the result exact regardless.
|
||||
assert_eq!(filetime_iso(&value(u64::MAX)).as_deref(), Some("60056-05-28T05:36:10Z"));
|
||||
// Zero, a non-FILETIME kind and times before the 1601 epoch are rejected, not a panic.
|
||||
assert_eq!(filetime_iso(&value(0)), None);
|
||||
assert_eq!(filetime_iso(&[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), None);
|
||||
assert_eq!(filetime_iso(&value(1)), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
//! Bounded conversion from the shared Word model to WordCraft's app model.
|
||||
//!
|
||||
//! The model crates intentionally have independent Rust types. Convert document stories a
|
||||
//! paragraph at a time, recursively through tables, so a large document never needs a second
|
||||
//! serialized copy of its complete body in memory. Embedded media and opaque streams are moved
|
||||
//! directly because both models use the same `Arc<Vec<u8>>` representation.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
fn convert<T, U>(value: T) -> Result<U, DocbinError>
|
||||
where
|
||||
T: Serialize,
|
||||
U: DeserializeOwned,
|
||||
{
|
||||
let bytes = serde_json::to_vec(&value).map_err(|error| DocbinError::Malformed(format!("shared model conversion: {error}")))?;
|
||||
drop(value);
|
||||
serde_json::from_slice(&bytes).map_err(|error| DocbinError::Malformed(format!("shared model conversion: {error}")))
|
||||
}
|
||||
|
||||
fn blocks(source: craft_wordmodel::Blocks) -> Result<wordcraft_doc::Blocks, DocbinError> {
|
||||
source
|
||||
.into_iter()
|
||||
.map(|block| {
|
||||
let block = Arc::try_unwrap(block).unwrap_or_else(|shared| (*shared).clone());
|
||||
block_value(block).map(Arc::new)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn block_value(source: craft_wordmodel::Block) -> Result<wordcraft_doc::Block, DocbinError> {
|
||||
match source {
|
||||
craft_wordmodel::Block::Para(paragraph) => paragraph_value(paragraph).map(wordcraft_doc::Block::Para),
|
||||
craft_wordmodel::Block::Table(table) => table_value(table).map(wordcraft_doc::Block::Table),
|
||||
}
|
||||
}
|
||||
|
||||
fn paragraph_value(source: craft_wordmodel::para::Paragraph) -> Result<wordcraft_doc::para::Paragraph, DocbinError> {
|
||||
let craft_wordmodel::para::Paragraph { text, runs, props, mark, objects, section, .. } = source;
|
||||
let mut paragraph = wordcraft_doc::para::Paragraph::new();
|
||||
paragraph.text = text;
|
||||
paragraph.runs =
|
||||
runs.into_iter().map(|run| Ok(wordcraft_doc::para::Run { len: run.len, props: convert(run.props)? })).collect::<Result<_, DocbinError>>()?;
|
||||
paragraph.props = convert(props)?;
|
||||
paragraph.mark = convert(mark)?;
|
||||
paragraph.objects = objects.into_iter().map(object_value).collect::<Result<_, _>>()?;
|
||||
paragraph.section = section.map(|section| convert(*section).map(Box::new)).transpose()?;
|
||||
Ok(paragraph)
|
||||
}
|
||||
|
||||
fn object_value(source: craft_wordmodel::para::InlineObject) -> Result<wordcraft_doc::para::InlineObject, DocbinError> {
|
||||
use craft_wordmodel::para::InlineObject as Source;
|
||||
use wordcraft_doc::para::InlineObject as Target;
|
||||
|
||||
match source {
|
||||
Source::Group { w, h, float, ch_w, ch_h, children, canvas } => Ok(Target::Group {
|
||||
w,
|
||||
h,
|
||||
float: convert(float)?,
|
||||
ch_w,
|
||||
ch_h,
|
||||
children: children.into_iter().map(group_child_value).collect::<Result<_, _>>()?,
|
||||
canvas: canvas.map(convert).transpose()?,
|
||||
}),
|
||||
other => convert(other),
|
||||
}
|
||||
}
|
||||
|
||||
fn group_child_value(source: craft_wordmodel::para::GroupChild) -> Result<wordcraft_doc::para::GroupChild, DocbinError> {
|
||||
Ok(wordcraft_doc::para::GroupChild { x: source.x, y: source.y, obj: object_value(source.obj)? })
|
||||
}
|
||||
|
||||
fn table_value(source: craft_wordmodel::table::Table) -> Result<wordcraft_doc::table::Table, DocbinError> {
|
||||
Ok(wordcraft_doc::table::Table {
|
||||
props: convert(source.props)?,
|
||||
grid: source.grid,
|
||||
rows: source.rows.into_iter().map(row_value).collect::<Result<_, _>>()?,
|
||||
controls: convert(source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn row_value(source: craft_wordmodel::table::Row) -> Result<wordcraft_doc::table::Row, DocbinError> {
|
||||
Ok(wordcraft_doc::table::Row {
|
||||
props: convert(source.props)?,
|
||||
cells: source.cells.into_iter().map(cell_value).collect::<Result<_, _>>()?,
|
||||
controls: convert(source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn cell_value(source: craft_wordmodel::table::Cell) -> Result<wordcraft_doc::table::Cell, DocbinError> {
|
||||
Ok(wordcraft_doc::table::Cell { props: convert(source.props)?, blocks: blocks(source.blocks)?, controls: convert(source.controls)? })
|
||||
}
|
||||
|
||||
fn part_value(source: craft_wordmodel::Part) -> Result<wordcraft_doc::Part, DocbinError> {
|
||||
Ok(wordcraft_doc::Part { kind: convert(source.kind)?, blocks: blocks(source.blocks)?, body: convert(source.body)? })
|
||||
}
|
||||
|
||||
fn glossary_value(source: craft_wordmodel::BuildingBlock) -> Result<wordcraft_doc::BuildingBlock, DocbinError> {
|
||||
Ok(wordcraft_doc::BuildingBlock {
|
||||
name: source.name,
|
||||
gallery: source.gallery,
|
||||
category: source.category,
|
||||
description: source.description,
|
||||
guid: source.guid,
|
||||
style: source.style,
|
||||
types: source.types,
|
||||
behaviors: source.behaviors,
|
||||
blocks: blocks(source.blocks)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Converts the shared reader's model without copying the full document or its embedded media.
|
||||
///
|
||||
/// The DOC reader leaves WordCraft-only transient fit state, editor fragments, and style change
|
||||
/// annotations at their defaults; the persistent document fields are transferred here. Body and
|
||||
/// table-cell stories are mapped one paragraph at a time, while shared media buffers are moved.
|
||||
pub fn from_shared_model(source: craft_wordmodel::Document) -> Result<wordcraft_doc::Document, DocbinError> {
|
||||
let craft_wordmodel::Document {
|
||||
body,
|
||||
last_section,
|
||||
parts,
|
||||
styles,
|
||||
numbering,
|
||||
comments,
|
||||
revisions,
|
||||
settings,
|
||||
core,
|
||||
sources,
|
||||
custom_props,
|
||||
footnote_separators,
|
||||
endnote_separators,
|
||||
media,
|
||||
passthrough,
|
||||
glossary,
|
||||
..
|
||||
} = source;
|
||||
|
||||
let mut document = wordcraft_doc::Document::new();
|
||||
document.body = blocks(body)?;
|
||||
document.last_section = convert(last_section)?;
|
||||
document.parts = parts.into_iter().map(|(id, part)| Ok((id, part_value(part)?))).collect::<Result<_, DocbinError>>()?;
|
||||
document.styles = Arc::new(convert((*styles).clone())?);
|
||||
document.numbering = Arc::new(convert((*numbering).clone())?);
|
||||
document.comments = convert(comments)?;
|
||||
document.revisions = convert(revisions)?;
|
||||
document.settings = convert(settings)?;
|
||||
document.core = convert(core)?;
|
||||
document.sources = convert(sources)?;
|
||||
document.custom_props = convert(custom_props)?;
|
||||
document.footnote_separators = convert(footnote_separators)?;
|
||||
document.endnote_separators = convert(endnote_separators)?;
|
||||
document.media = media;
|
||||
document.passthrough = passthrough;
|
||||
document.glossary = glossary.into_iter().map(glossary_value).collect::<Result<_, _>>()?;
|
||||
Ok(document)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bridges_nested_stories_and_moves_media_without_copying() {
|
||||
let mut source = craft_wordmodel::Document::new();
|
||||
let long_text = "x".repeat(4 << 20);
|
||||
let mut body_para = craft_wordmodel::para::Paragraph::with_text(&long_text, Default::default());
|
||||
body_para.objects.push(craft_wordmodel::para::InlineObject::Group {
|
||||
w: 32.0,
|
||||
h: 24.0,
|
||||
float: Default::default(),
|
||||
ch_w: 64.0,
|
||||
ch_h: 48.0,
|
||||
children: vec![craft_wordmodel::para::GroupChild {
|
||||
x: 4.0,
|
||||
y: 6.0,
|
||||
obj: craft_wordmodel::para::InlineObject::Field {
|
||||
instr: "PAGE".into(),
|
||||
result: "7".into(),
|
||||
locked: true,
|
||||
dirty: false,
|
||||
code: false,
|
||||
form: String::new(),
|
||||
},
|
||||
}],
|
||||
canvas: None,
|
||||
});
|
||||
let cell = craft_wordmodel::table::Cell::with_text("nested cell");
|
||||
let table = craft_wordmodel::table::Table {
|
||||
rows: vec![craft_wordmodel::table::Row { cells: vec![cell], ..Default::default() }],
|
||||
..Default::default()
|
||||
};
|
||||
source.body = vec![Arc::new(craft_wordmodel::Block::Para(body_para)), Arc::new(craft_wordmodel::Block::Table(table))];
|
||||
source.parts.insert(
|
||||
12,
|
||||
craft_wordmodel::Part {
|
||||
kind: craft_wordmodel::PartKind::TextBox,
|
||||
blocks: vec![craft_wordmodel::para_block(craft_wordmodel::para::Paragraph::with_text("part story", Default::default()))],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
source.settings.compat_mode = 11;
|
||||
let image = Arc::new(vec![0x5a; 1 << 20]);
|
||||
source.media.insert("image1.png".into(), image.clone());
|
||||
|
||||
let document = from_shared_model(source).expect("model conversion");
|
||||
assert_eq!(document.body.len(), 2);
|
||||
assert_eq!(document.body[0].as_para().map(|p| p.text.as_str()), Some(long_text.as_str()));
|
||||
let group = match &document.body[0].as_para().expect("paragraph").objects[0] {
|
||||
wordcraft_doc::para::InlineObject::Group { children, ch_w, .. } => {
|
||||
assert_eq!(*ch_w, 64.0);
|
||||
children
|
||||
}
|
||||
other => panic!("expected grouped object, got {other:?}"),
|
||||
};
|
||||
assert_eq!((group[0].x, group[0].y), (4.0, 6.0));
|
||||
assert!(
|
||||
matches!(&group[0].obj, wordcraft_doc::para::InlineObject::Field { instr, result, locked: true, .. } if instr == "PAGE" && result == "7")
|
||||
);
|
||||
let table = document.body[1].as_table().expect("table block");
|
||||
assert_eq!(table.rows[0].cells[0].blocks[0].as_para().map(|p| p.text.as_str()), Some("nested cell"));
|
||||
assert_eq!(document.parts[&12].blocks[0].as_para().map(|p| p.text.as_str()), Some("part story"));
|
||||
assert_eq!(document.settings.compat_mode, 11);
|
||||
assert!(Arc::ptr_eq(document.media.get("image1.png").expect("moved media"), &image));
|
||||
assert!(document.fit_state.is_none());
|
||||
}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
//! Footnotes, endnotes and bookmarks: `PlcffndRef`/`PlcffndTxt`, `PlcfendRef`/`PlcfendTxt`,
|
||||
//! and `SttbfBkmk` + `Plcfbkf`/`Plcfbkl` ([MS-DOC] §2.9).
|
||||
|
||||
/// Cap on notes and bookmarks per document.
|
||||
const MAX: usize = 10_000;
|
||||
|
||||
/// Parse the note reference/story PLCs. `ref_pair`/`txt_pair` are the FIB pair indices
|
||||
/// (`(2, 3)` for footnotes, `(46, 47)` for endnotes); story ranges are relative to the
|
||||
/// note subdocument.
|
||||
///
|
||||
/// The story CPs of a well-formed PLC ascend, so the stories are disjoint. A hostile table
|
||||
/// that steps back would make many notes read the same text again (each one materialised
|
||||
/// as its own part), so a story starting before the end of the last one kept is dropped:
|
||||
/// every subdocument CP becomes at most one note.
|
||||
pub(crate) fn parse_notes(table: &[u8], fib: &crate::fib::Fib, ref_pair: usize, txt_pair: usize) -> Vec<(u32, (u32, u32))> {
|
||||
let refs = fib.pair(ref_pair).and_then(|(fc, lcb)| plc_of(table, fc, lcb, 2)).unwrap_or_default();
|
||||
let txts = fib.pair(txt_pair).and_then(|(fc, lcb)| plc_of(table, fc, lcb, 0)).unwrap_or_default();
|
||||
if txts.len() < 2 {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut kept_to = 0u32;
|
||||
// The reference PLC's aFtnIdx order matches the story order; pair them by index.
|
||||
for (i, &cp) in refs.iter().enumerate() {
|
||||
if let (Some(&a), Some(&b)) = (txts.get(i), txts.get(i + 1))
|
||||
&& b > a
|
||||
&& a >= kept_to
|
||||
{
|
||||
out.push((cp, (a, b)));
|
||||
kept_to = b;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The CPs of a PLC (the trailing element is a limit and is kept so callers can pair
|
||||
/// range ends) plus the data-element size check. Returns the CPs only.
|
||||
fn plc_of(table: &[u8], fc: u32, lcb: u32, data_size: usize) -> Option<Vec<u32>> {
|
||||
if lcb == 0 {
|
||||
return None;
|
||||
}
|
||||
let plc = table.get(fc as usize..(fc as usize).saturating_add(lcb as usize))?;
|
||||
if plc.len() < 4 {
|
||||
return None;
|
||||
}
|
||||
// aCP has one more element than aData: n = (len - data_size) / (4 + data_size).
|
||||
let n = (plc.len() - data_size) / (4 + data_size);
|
||||
if n == 0 || n > MAX {
|
||||
return None;
|
||||
}
|
||||
let mut cps = Vec::with_capacity(n.min(MAX));
|
||||
for i in 0..=n.min(MAX) {
|
||||
match plc.get(i * 4..i * 4 + 4) {
|
||||
Some(b) => cps.push(u32::from_le_bytes([b[0], b[1], b[2], b[3]])),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
Some(cps)
|
||||
}
|
||||
|
||||
/// A bookmark boundary to insert while walking.
|
||||
pub(crate) enum Mark {
|
||||
Start(String),
|
||||
End(String),
|
||||
}
|
||||
|
||||
/// Parse `SttbfBkmk` + `Plcfbkf` + `Plcfbkl` into (cp, event) pairs sorted for a sequential
|
||||
/// walk. Hidden bookmarks (leading `_`) are kept: the engine can decide.
|
||||
pub(crate) fn parse_bookmarks(table: &[u8], fib: &crate::fib::Fib) -> Vec<(u32, Mark)> {
|
||||
let names = fib.pair(crate::fib::pair::STTBF_BKMK).and_then(|(fc, lcb)| sttb_strings(table, fc, lcb)).unwrap_or_default();
|
||||
let starts = fib.pair(crate::fib::pair::PLCF_BKF).and_then(|(fc, lcb)| plc_of(table, fc, lcb, 4)).unwrap_or_default();
|
||||
let ends = fib.pair(crate::fib::pair::PLCF_BKL).and_then(|(fc, lcb)| plc_of(table, fc, lcb, 2)).unwrap_or_default();
|
||||
let n = starts.len().saturating_sub(1).min(ends.len().saturating_sub(1)).min(names.len()).min(MAX);
|
||||
let mut out = Vec::with_capacity(n * 2);
|
||||
for i in 0..n {
|
||||
let (Some(&a), Some(&b)) = (starts.get(i), ends.get(i)) else { break };
|
||||
if b < a {
|
||||
continue;
|
||||
}
|
||||
if let Some(name) = names.get(i) {
|
||||
if name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
out.push((a, Mark::Start(name.clone())));
|
||||
out.push((b, Mark::End(name.clone())));
|
||||
}
|
||||
}
|
||||
out.sort_by_key(|(cp, m)| (*cp, matches!(m, Mark::End(_))));
|
||||
out
|
||||
}
|
||||
|
||||
/// Strings of an STTB: extended (fExtend = 0xFFFF, 2-byte length and UTF-16 chars) or not
|
||||
/// (1-byte length and Windows-1252 chars). `cbExtra` bytes after each string are skipped.
|
||||
/// Names are kept to 40 characters, but the reader always advances by the stored length.
|
||||
fn sttb_strings(table: &[u8], fc: u32, lcb: u32) -> Option<Vec<String>> {
|
||||
let b = table.get(fc as usize..(fc as usize).saturating_add(lcb as usize))?;
|
||||
let extended = b.get(0..2)? == [0xFF, 0xFF];
|
||||
let head = if extended { 2 } else { 0 };
|
||||
let h = b.get(head..head + 4)?;
|
||||
let c_data = u16::from_le_bytes([h[0], h[1]]) as usize;
|
||||
let cb_extra = u16::from_le_bytes([h[2], h[3]]) as usize;
|
||||
if c_data > MAX {
|
||||
return None;
|
||||
}
|
||||
let mut at = head + 4;
|
||||
let mut out = Vec::with_capacity(c_data);
|
||||
for _ in 0..c_data {
|
||||
let (cch, unit) = if extended {
|
||||
match b.get(at..at + 2) {
|
||||
Some(x) => (u16::from_le_bytes([x[0], x[1]]) as usize, 2),
|
||||
None => break,
|
||||
}
|
||||
} else {
|
||||
match b.get(at) {
|
||||
Some(&x) => (x as usize, 1),
|
||||
None => break,
|
||||
}
|
||||
};
|
||||
at += unit;
|
||||
let Some(chars) = b.get(at..at + cch * unit) else { break };
|
||||
at += cch * unit;
|
||||
// The string's extra data must be there too, or the table is cut short.
|
||||
if b.get(at..at + cb_extra).is_none() {
|
||||
break;
|
||||
}
|
||||
at += cb_extra;
|
||||
let s: String = if extended {
|
||||
chars.as_chunks::<2>().0.iter().take(40).map(|c| char::from_u32(u16::from_le_bytes(*c) as u32).unwrap_or('\u{FFFD}')).collect()
|
||||
} else {
|
||||
chars.iter().take(40).map(|&c| wordcraft_doc::encoding::cp1252(c)).collect()
|
||||
};
|
||||
out.push(s);
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::sttb_strings;
|
||||
|
||||
fn ext_sttb(names: &[&str], extra: &[u8]) -> Vec<u8> {
|
||||
let mut b = vec![0xFF, 0xFF];
|
||||
b.extend_from_slice(&(names.len() as u16).to_le_bytes());
|
||||
b.extend_from_slice(&(extra.len() as u16).to_le_bytes());
|
||||
for n in names {
|
||||
let units: Vec<u16> = n.encode_utf16().collect();
|
||||
b.extend_from_slice(&(units.len() as u16).to_le_bytes());
|
||||
for u in units {
|
||||
b.extend_from_slice(&u.to_le_bytes());
|
||||
}
|
||||
b.extend_from_slice(extra);
|
||||
}
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_bookmark_name_does_not_desync_table() {
|
||||
let long = "A".repeat(50);
|
||||
let b = ext_sttb(&[&long, "bm2"], &[]);
|
||||
let got = sttb_strings(&b, 0, b.len() as u32).unwrap();
|
||||
assert_eq!(got, vec!["A".repeat(40), "bm2".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sttb_extra_data_is_skipped() {
|
||||
let b = ext_sttb(&["first", "bm2"], &[1, 2, 3, 4]);
|
||||
let got = sttb_strings(&b, 0, b.len() as u32).unwrap();
|
||||
assert_eq!(got, vec!["first".to_string(), "bm2".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_without_its_extra_data_is_dropped() {
|
||||
let mut b = ext_sttb(&["first", "bm2"], &[1, 2, 3, 4]);
|
||||
b.truncate(b.len() - 2);
|
||||
let got = sttb_strings(&b, 0, b.len() as u32).unwrap();
|
||||
assert_eq!(got, vec!["first".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_extended_sttb_reads_byte_strings() {
|
||||
let mut b = Vec::new();
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
b.extend_from_slice(&2u16.to_le_bytes());
|
||||
for n in ["one", "caf\u{e9}"] {
|
||||
let bytes: Vec<u8> = n.chars().map(|c| c as u8).collect();
|
||||
b.push(bytes.len() as u8);
|
||||
b.extend_from_slice(&bytes);
|
||||
b.extend_from_slice(&[9, 9]);
|
||||
}
|
||||
let got = sttb_strings(&b, 0, b.len() as u32).unwrap();
|
||||
assert_eq!(got, vec!["one".to_string(), "caf\u{e9}".to_string()]);
|
||||
}
|
||||
}
|
||||
@@ -1,552 +0,0 @@
|
||||
//! OfficeArt (Escher) drawing records, as Word 97-2003 stores them ([MS-ODRAW], public Microsoft
|
||||
//! Open Specifications): the record header, the BLIP store and its pictures, shape containers with
|
||||
//! their property tables, and the `OfficeArtContent` of the Table stream ([MS-DOC] §2.9.172).
|
||||
//!
|
||||
//! Everything here walks hostile bytes: record lengths are clamped to their parent, nesting depth
|
||||
//! and record counts are capped, and decompressed metafiles are size-limited.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
|
||||
use image::ImageEncoder;
|
||||
|
||||
use wordcraft_doc::props::Rgb;
|
||||
|
||||
/// Deepest container nesting walked (groups inside groups…).
|
||||
const MAX_DEPTH: usize = 12;
|
||||
/// Most records walked in one drawing tree or BLIP store.
|
||||
const MAX_RECORDS: usize = 200_000;
|
||||
/// Most BLIP store entries kept.
|
||||
const MAX_BSE: usize = 10_000;
|
||||
/// Most drawings (main document, headers) read from `OfficeArtContent`.
|
||||
const MAX_DRAWINGS: usize = 8;
|
||||
/// Largest decompressed metafile (bytes).
|
||||
const MAX_METAFILE: usize = 64 << 20;
|
||||
/// Largest DIB converted to PNG, per side (pixels).
|
||||
const MAX_DIB_SIDE: u32 = 16_384;
|
||||
|
||||
/// Record types ([MS-ODRAW] §2.2, §2.3).
|
||||
pub(crate) mod rt {
|
||||
pub(crate) const DGG_CONTAINER: u16 = 0xF000;
|
||||
pub(crate) const BSTORE_CONTAINER: u16 = 0xF001;
|
||||
pub(crate) const DG_CONTAINER: u16 = 0xF002;
|
||||
pub(crate) const SPGR_CONTAINER: u16 = 0xF003;
|
||||
pub(crate) const SP_CONTAINER: u16 = 0xF004;
|
||||
pub(crate) const FBSE: u16 = 0xF007;
|
||||
pub(crate) const FSPGR: u16 = 0xF009;
|
||||
pub(crate) const FSP: u16 = 0xF00A;
|
||||
pub(crate) const FOPT: u16 = 0xF00B;
|
||||
pub(crate) const CLIENT_TEXTBOX: u16 = 0xF00D;
|
||||
pub(crate) const CHILD_ANCHOR: u16 = 0xF00F;
|
||||
pub(crate) const SECONDARY_FOPT: u16 = 0xF121;
|
||||
pub(crate) const TERTIARY_FOPT: u16 = 0xF122;
|
||||
pub(crate) const BLIP_EMF: u16 = 0xF01A;
|
||||
pub(crate) const BLIP_WMF: u16 = 0xF01B;
|
||||
pub(crate) const BLIP_PICT: u16 = 0xF01C;
|
||||
pub(crate) const BLIP_JPEG: u16 = 0xF01D;
|
||||
pub(crate) const BLIP_PNG: u16 = 0xF01E;
|
||||
pub(crate) const BLIP_DIB: u16 = 0xF01F;
|
||||
pub(crate) const BLIP_TIFF: u16 = 0xF029;
|
||||
pub(crate) const BLIP_JPEG_CMYK: u16 = 0xF02A;
|
||||
}
|
||||
|
||||
/// One record: header fields and its body (clamped to what the parent holds).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct Rec<'a> {
|
||||
pub(crate) inst: u16,
|
||||
pub(crate) typ: u16,
|
||||
pub(crate) body: &'a [u8],
|
||||
}
|
||||
|
||||
/// The record at the start of `buf` and the bytes it spans (header included). A body longer
|
||||
/// than `buf` is cut to what is there.
|
||||
pub(crate) fn record(buf: &[u8]) -> Option<(Rec<'_>, usize)> {
|
||||
let h = buf.get(0..8)?;
|
||||
let ver_inst = u16::from_le_bytes([h[0], h[1]]);
|
||||
let typ = u16::from_le_bytes([h[2], h[3]]);
|
||||
let len = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize;
|
||||
let rest = buf.get(8..)?;
|
||||
let len = len.min(rest.len());
|
||||
let body = rest.get(..len)?;
|
||||
Some((Rec { inst: ver_inst >> 4, typ, body }, 8 + len))
|
||||
}
|
||||
|
||||
/// The sibling records of a container body, at most `budget` of them (the budget is shared
|
||||
/// across a whole walk, so a deep or wide tree cannot multiply the work).
|
||||
pub(crate) fn children<'a>(body: &'a [u8], budget: &mut usize) -> Vec<Rec<'a>> {
|
||||
let mut out = Vec::new();
|
||||
let mut at = 0usize;
|
||||
while *budget > 0 {
|
||||
let Some((rec, used)) = body.get(at..).and_then(record) else { break };
|
||||
*budget -= 1;
|
||||
out.push(rec);
|
||||
at = at.saturating_add(used);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn u16_at(b: &[u8], at: usize) -> Option<u16> {
|
||||
Some(u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
fn u32_at(b: &[u8], at: usize) -> Option<u32> {
|
||||
Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
fn i32_at(b: &[u8], at: usize) -> Option<i32> {
|
||||
u32_at(b, at).map(|v| v as i32)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Pictures (BLIPs).
|
||||
|
||||
/// A decoded picture: bytes ready for `Document::media` and their file extension.
|
||||
pub(crate) struct Blip {
|
||||
pub(crate) bytes: Vec<u8>,
|
||||
pub(crate) ext: &'static str,
|
||||
/// The format cannot be drawn (Macintosh PICT): the picture is kept as a placeholder of
|
||||
/// the right size.
|
||||
pub(crate) placeholder: bool,
|
||||
}
|
||||
|
||||
/// The picture of an `OfficeArtFBSE` body: its embedded BLIP, or the BLIP stored at `foDelay`
|
||||
/// in `delay` (the WordDocument stream for the document's BLIP store, the Data stream for
|
||||
/// inline pictures). `limit` caps the bytes produced.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn fbse_blip(fbse: &[u8], delay: &[u8], limit: usize, work: &mut Decode) -> Option<Blip> {
|
||||
let (rec, _) = fbse_record(fbse, delay)?;
|
||||
blip(&rec, limit, work)
|
||||
}
|
||||
|
||||
/// The document's decode budget: bytes of inflating and pixel decoding left, and how many
|
||||
/// attempts it has refused (only the first few are logged).
|
||||
pub(crate) struct Decode {
|
||||
pub(crate) left: usize,
|
||||
refused: usize,
|
||||
}
|
||||
|
||||
impl Decode {
|
||||
pub(crate) fn new(left: usize) -> Self {
|
||||
Decode { left, refused: 0 }
|
||||
}
|
||||
|
||||
/// Take `cost` off the budget; `false` (and a log line) when it doesn't fit.
|
||||
fn charge(&mut self, cost: usize, what: &str) -> bool {
|
||||
match self.left.checked_sub(cost) {
|
||||
Some(left) if cost > 0 => {
|
||||
self.left = left;
|
||||
true
|
||||
}
|
||||
_ => {
|
||||
if self.refused < 10 {
|
||||
log::warn!(
|
||||
"docbin: a {what} needing {cost} bytes of decode work is over the decode budget ({} left) and was not decoded",
|
||||
self.left
|
||||
);
|
||||
}
|
||||
self.refused = self.refused.saturating_add(1);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The BLIP record of an `OfficeArtFBSE`: embedded after it, or in the delay stream, where
|
||||
/// its (`foDelay`, record length) location is returned too (entries can share one record,
|
||||
/// whatever sizes their FBSEs claim: the record's own header is what counts).
|
||||
pub(crate) fn fbse_record<'a>(fbse: &'a [u8], delay: &'a [u8]) -> Option<(Rec<'a>, Option<(usize, usize)>)> {
|
||||
// btWin32, btMacOS, rgbUid[16], tag, size, cRef, foDelay, unused, cbName, unused×2.
|
||||
let fo_delay = u32_at(fbse, 28)? as usize;
|
||||
let cb_name = *fbse.get(33)? as usize;
|
||||
let at = 36usize.checked_add(cb_name)?;
|
||||
if let Some((rec, _)) = fbse.get(at..).and_then(record) {
|
||||
return Some((rec, None));
|
||||
}
|
||||
// Not embedded: the record sits in the delay stream.
|
||||
let (rec, used) = record(delay.get(fo_delay..)?)?;
|
||||
Some((rec, Some((fo_delay, used))))
|
||||
}
|
||||
|
||||
/// Decode one `OfficeArtBlip*` record ([MS-ODRAW] §2.2.23-§2.2.31). `work` is the
|
||||
/// document's decode budget: inflating a metafile or decoding a DIB is charged to it before
|
||||
/// it runs, whether it succeeds or not, and nothing is decoded once it is spent.
|
||||
pub(crate) fn blip(rec: &Rec, limit: usize, work: &mut Decode) -> Option<Blip> {
|
||||
// Each type has two instance values; the odd one carries a second 16-byte UID.
|
||||
let uids = if rec.inst & 1 == 1 { 32 } else { 16 };
|
||||
match rec.typ {
|
||||
rt::BLIP_PNG | rt::BLIP_JPEG | rt::BLIP_JPEG_CMYK | rt::BLIP_DIB | rt::BLIP_TIFF => {
|
||||
let data = rec.body.get(uids + 1..)?; // + tag byte
|
||||
if data.is_empty() || data.len() > limit {
|
||||
return None;
|
||||
}
|
||||
match rec.typ {
|
||||
rt::BLIP_PNG => Some(Blip { bytes: data.to_vec(), ext: "png", placeholder: false }),
|
||||
rt::BLIP_JPEG | rt::BLIP_JPEG_CMYK => Some(Blip { bytes: data.to_vec(), ext: "jpeg", placeholder: false }),
|
||||
rt::BLIP_TIFF => Some(Blip { bytes: data.to_vec(), ext: "tiff", placeholder: false }),
|
||||
_ => dib_to_png(data, limit, work),
|
||||
}
|
||||
}
|
||||
rt::BLIP_EMF | rt::BLIP_WMF | rt::BLIP_PICT => {
|
||||
// OfficeArtMetafileHeader: cbSize, rcBounds, ptSize, cbSave, compression, filter.
|
||||
let hdr = rec.body.get(uids..uids + 34)?;
|
||||
let cb_size = u32_at(hdr, 0)? as usize;
|
||||
let cb_save = u32_at(hdr, 28)? as usize;
|
||||
let compression = *hdr.get(32)?;
|
||||
let raw = rec.body.get(uids + 34..)?;
|
||||
let raw = raw.get(..cb_save.min(raw.len()))?;
|
||||
let cap = limit.min(MAX_METAFILE);
|
||||
let bytes = if compression == 0 {
|
||||
let want = if cb_size > 0 { cb_size.min(cap) } else { cap };
|
||||
// The compressed stream is read whole whatever it inflates to: a record that
|
||||
// claims to inflate to nothing still costs its input (#3871).
|
||||
if !work.charge(want.max(raw.len()), "metafile") {
|
||||
return None;
|
||||
}
|
||||
miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(raw, want).ok()?
|
||||
} else {
|
||||
if raw.len() > cap {
|
||||
return None;
|
||||
}
|
||||
raw.to_vec()
|
||||
};
|
||||
if bytes.is_empty() {
|
||||
return None;
|
||||
}
|
||||
match rec.typ {
|
||||
rt::BLIP_EMF => Some(Blip { bytes, ext: "emf", placeholder: false }),
|
||||
rt::BLIP_WMF => Some(Blip { bytes, ext: "wmf", placeholder: false }),
|
||||
_ => Some(Blip { bytes, ext: "pict", placeholder: true }),
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A device-independent bitmap (BITMAPINFOHEADER + palette + pixels) as a PNG: the DIB gets
|
||||
/// the 14-byte BMP file header it lacks, is decoded with size limits and re-encoded. When the
|
||||
/// bitmap can't be decoded, or its declared pixels (4 bytes each) don't fit in `work`, the
|
||||
/// BMP bytes are kept (and render as a grey placeholder).
|
||||
fn dib_to_png(dib: &[u8], limit: usize, work: &mut Decode) -> Option<Blip> {
|
||||
let header = u32_at(dib, 0)? as usize;
|
||||
if !(12..=124).contains(&header) {
|
||||
return None;
|
||||
}
|
||||
let (bits, used, compression) =
|
||||
if header == 12 { (u16_at(dib, 10)? as usize, 0, 0) } else { (u16_at(dib, 14)? as usize, u32_at(dib, 32)? as usize, u32_at(dib, 16)?) };
|
||||
let entry = if header == 12 { 3 } else { 4 };
|
||||
let colors = if used > 0 {
|
||||
used.min(256)
|
||||
} else if bits <= 8 {
|
||||
1usize << bits
|
||||
} else {
|
||||
0
|
||||
};
|
||||
// BI_BITFIELDS masks follow a 40-byte header.
|
||||
let masks = if header == 40 && (compression == 3 || compression == 6) { if compression == 3 { 12 } else { 16 } } else { 0 };
|
||||
let off_bits = 14usize.checked_add(header)?.checked_add(colors * entry)?.checked_add(masks)?;
|
||||
let file_size = 14usize.checked_add(dib.len()).filter(|&size| size <= limit)?;
|
||||
// The declared size is what decoding allocates, however small the (RLE) data is.
|
||||
let (w, h) = if header == 12 {
|
||||
(u64::from(u16_at(dib, 4)?), u64::from(u16_at(dib, 6)?))
|
||||
} else {
|
||||
(u64::from(u32_at(dib, 4)?), u64::from((u32_at(dib, 8)? as i32).unsigned_abs()))
|
||||
};
|
||||
let cost = w.saturating_mul(h).saturating_mul(4).try_into().unwrap_or(usize::MAX);
|
||||
// Copying the DIB into a BMP is work too, however few pixels it declares: it is charged
|
||||
// before the copy is made (#3871). A bitmap whose pixels don't fit still keeps its BMP
|
||||
// bytes as a placeholder, if the copy fits.
|
||||
let decode = work.charge(cost.max(dib.len()), "bitmap");
|
||||
if !decode && !work.charge(dib.len(), "bitmap") {
|
||||
return None;
|
||||
}
|
||||
let mut bmp = Vec::with_capacity(file_size);
|
||||
bmp.extend_from_slice(b"BM");
|
||||
bmp.extend_from_slice(&u32::try_from(file_size).ok()?.to_le_bytes());
|
||||
bmp.extend_from_slice(&[0, 0, 0, 0]);
|
||||
bmp.extend_from_slice(&u32::try_from(off_bits).ok()?.to_le_bytes());
|
||||
bmp.extend_from_slice(dib);
|
||||
let placeholder = |bmp| Some(Blip { bytes: bmp, ext: "bmp", placeholder: true });
|
||||
if !decode {
|
||||
return placeholder(bmp);
|
||||
}
|
||||
let mut reader = image::ImageReader::new(std::io::Cursor::new(&bmp));
|
||||
reader.set_format(image::ImageFormat::Bmp);
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_image_width = Some(MAX_DIB_SIDE);
|
||||
limits.max_image_height = Some(MAX_DIB_SIDE);
|
||||
limits.max_alloc = Some((256 << 20).min(limit as u64));
|
||||
reader.limits(limits);
|
||||
let mut png = PictureWriter { bytes: Vec::new(), limit };
|
||||
match reader.decode() {
|
||||
Ok(img)
|
||||
if image::codecs::png::PngEncoder::new(&mut png).write_image(img.as_bytes(), img.width(), img.height(), img.color().into()).is_ok() =>
|
||||
{
|
||||
Some(Blip { bytes: png.bytes, ext: "png", placeholder: false })
|
||||
}
|
||||
_ => placeholder(bmp),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cap encoded bytes while the PNG encoder writes, before the buffer can grow.
|
||||
struct PictureWriter {
|
||||
bytes: Vec<u8>,
|
||||
limit: usize,
|
||||
}
|
||||
|
||||
impl Write for PictureWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
let Some(end) = self.bytes.len().checked_add(buf.len()).filter(|&end| end <= self.limit) else {
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "picture exceeds remaining media budget"));
|
||||
};
|
||||
if end > self.bytes.capacity() {
|
||||
let new_capacity = self.bytes.capacity().saturating_mul(2).max(end).min(self.limit);
|
||||
self.bytes.try_reserve_exact(new_capacity - self.bytes.len()).map_err(std::io::Error::other)?;
|
||||
}
|
||||
self.bytes.extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Shapes.
|
||||
|
||||
/// The shape properties we use, from `OfficeArtFOPT` and its secondary/tertiary tables
|
||||
/// ([MS-ODRAW] §2.3).
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub(crate) struct Props {
|
||||
/// `pib`: 1-based index of the picture in the BLIP store.
|
||||
pub(crate) pib: Option<u32>,
|
||||
/// Rotation, degrees clockwise.
|
||||
pub(crate) rot: f32,
|
||||
/// Crop fractions (left, top, right, bottom).
|
||||
pub(crate) crop: [f32; 4],
|
||||
pub(crate) fill_color: Option<Rgb>,
|
||||
pub(crate) filled: Option<bool>,
|
||||
pub(crate) line_color: Option<Rgb>,
|
||||
/// Line width in EMUs.
|
||||
pub(crate) line_width: Option<u32>,
|
||||
pub(crate) line: Option<bool>,
|
||||
/// An arrowhead at either end of a line.
|
||||
pub(crate) arrowhead: bool,
|
||||
/// `posh`/`posrelh`/`posv`/`posrelv`: alignment and reference frame (Word 2000 and later).
|
||||
pub(crate) posh: Option<u32>,
|
||||
pub(crate) posrelh: Option<u32>,
|
||||
pub(crate) posv: Option<u32>,
|
||||
pub(crate) posrelv: Option<u32>,
|
||||
/// Distance from wrapped text (left, top, right, bottom), EMUs.
|
||||
pub(crate) wrap_dist: [Option<u32>; 4],
|
||||
/// `wzDescription`: the alternative text.
|
||||
pub(crate) alt: String,
|
||||
}
|
||||
|
||||
impl Props {
|
||||
/// Apply one property table (`OfficeArtFOPT` or a secondary/tertiary one): `count` 6-byte
|
||||
/// entries, then the complex data of the entries flagged complex, in order.
|
||||
pub(crate) fn apply(&mut self, body: &[u8], count: u16) {
|
||||
let n = (count as usize).min(body.len() / 6);
|
||||
let mut complex_at = n * 6;
|
||||
for i in 0..n {
|
||||
let Some(opid) = u16_at(body, i * 6) else { break };
|
||||
let Some(op) = u32_at(body, i * 6 + 2) else { break };
|
||||
let pid = opid & 0x3FFF;
|
||||
if opid & 0x8000 != 0 {
|
||||
let start = complex_at;
|
||||
complex_at = complex_at.saturating_add(op as usize);
|
||||
if pid == 0x0381 {
|
||||
let end = complex_at.min(body.len());
|
||||
if let Some(data) = body.get(start..end) {
|
||||
let units: Vec<u16> =
|
||||
data.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).take_while(|u| *u != 0).take(4096).collect();
|
||||
self.alt = String::from_utf16_lossy(&units);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let fixed = |v: u32| (v as i32) as f32 / 65_536.0;
|
||||
match pid {
|
||||
0x0004 => self.rot = fixed(op),
|
||||
0x0100..=0x0103 => {
|
||||
// cropFromTop, cropFromBottom, cropFromLeft, cropFromRight.
|
||||
let slot = [1, 3, 0, 2][(pid - 0x0100) as usize];
|
||||
let v = fixed(op);
|
||||
self.crop[slot] = if v.is_finite() { v.clamp(0.0, 0.99) } else { 0.0 };
|
||||
}
|
||||
0x0104 => self.pib = Some(op),
|
||||
0x0181 => self.fill_color = color(op),
|
||||
// fFilled (bit 4) is valid when fUsefFilled (bit 20) is set.
|
||||
0x01BF if op & (1 << 20) != 0 => self.filled = Some(op & (1 << 4) != 0),
|
||||
0x01BF => {}
|
||||
0x01C0 => self.line_color = color(op),
|
||||
0x01CB => self.line_width = Some(op),
|
||||
0x01D0 | 0x01D1 => self.arrowhead |= op != 0,
|
||||
// fLine (bit 3) is valid when fUsefLine (bit 19) is set.
|
||||
0x01FF if op & (1 << 19) != 0 => self.line = Some(op & (1 << 3) != 0),
|
||||
0x01FF => {}
|
||||
0x0384..=0x0387 => self.wrap_dist[(pid - 0x0384) as usize] = Some(op),
|
||||
0x038F => self.posh = Some(op),
|
||||
0x0390 => self.posrelh = Some(op),
|
||||
0x0391 => self.posv = Some(op),
|
||||
0x0392 => self.posrelv = Some(op),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An `OfficeArtCOLORREF` holding a literal RGB value (palette, scheme and system colour
|
||||
/// references are not resolved).
|
||||
fn color(v: u32) -> Option<Rgb> {
|
||||
let [r, g, b, flags] = v.to_le_bytes();
|
||||
// fPaletteIndex, fSchemeIndex, fSysIndex.
|
||||
if flags & 0x19 != 0 {
|
||||
return None;
|
||||
}
|
||||
Some(Rgb(r, g, b))
|
||||
}
|
||||
|
||||
/// One shape (`OfficeArtSpContainer`).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct Sp {
|
||||
pub(crate) spid: u32,
|
||||
/// Shape type (`MSOSPT`), from the FSP's instance.
|
||||
pub(crate) spt: u16,
|
||||
/// FSP flags: fGroup, fChild, fPatriarch, fDeleted, fOleShape, fHaveMaster, fFlipH, fFlipV…
|
||||
pub(crate) flags: u32,
|
||||
pub(crate) props: Props,
|
||||
/// `OfficeArtChildAnchor` (left, top, right, bottom) in the parent group's space.
|
||||
pub(crate) child: Option<[i32; 4]>,
|
||||
/// `OfficeArtFSPGR`: a group's own coordinate space.
|
||||
pub(crate) group_space: Option<[i32; 4]>,
|
||||
/// Has an `OfficeArtClientTextbox` (its text is in the text box story).
|
||||
pub(crate) textbox: bool,
|
||||
}
|
||||
|
||||
impl Sp {
|
||||
pub(crate) fn flip_h(&self) -> bool {
|
||||
self.flags & (1 << 6) != 0
|
||||
}
|
||||
pub(crate) fn flip_v(&self) -> bool {
|
||||
self.flags & (1 << 7) != 0
|
||||
}
|
||||
fn deleted(&self) -> bool {
|
||||
self.flags & (1 << 3) != 0
|
||||
}
|
||||
}
|
||||
|
||||
/// A shape or a group of shapes.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) enum Node {
|
||||
Shape(Sp),
|
||||
/// A group: its own shape record (FSPGR, anchor) and members.
|
||||
Group(Sp, Vec<Node>),
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub(crate) fn sp(&self) -> &Sp {
|
||||
match self {
|
||||
Node::Shape(sp) | Node::Group(sp, _) => sp,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one `OfficeArtSpContainer`.
|
||||
pub(crate) fn shape(body: &[u8], budget: &mut usize) -> Sp {
|
||||
let mut sp = Sp::default();
|
||||
for rec in children(body, budget) {
|
||||
match rec.typ {
|
||||
rt::FSP => {
|
||||
sp.spt = rec.inst;
|
||||
sp.spid = u32_at(rec.body, 0).unwrap_or(0);
|
||||
sp.flags = u32_at(rec.body, 4).unwrap_or(0);
|
||||
}
|
||||
rt::FOPT | rt::SECONDARY_FOPT | rt::TERTIARY_FOPT => sp.props.apply(rec.body, rec.inst),
|
||||
rt::FSPGR | rt::CHILD_ANCHOR => {
|
||||
let r = [0, 4, 8, 12].map(|o| i32_at(rec.body, o));
|
||||
if let [Some(l), Some(t), Some(r), Some(b)] = r {
|
||||
if rec.typ == rt::FSPGR {
|
||||
sp.group_space = Some([l, t, r, b]);
|
||||
} else {
|
||||
sp.child = Some([l, t, r, b]);
|
||||
}
|
||||
}
|
||||
}
|
||||
rt::CLIENT_TEXTBOX => sp.textbox = true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
sp
|
||||
}
|
||||
|
||||
/// Parse an `OfficeArtSpgrContainer`: the first shape is the group itself, the rest its members.
|
||||
fn group(body: &[u8], depth: usize, budget: &mut usize) -> Option<Node> {
|
||||
let recs = children(body, budget);
|
||||
let mut it = recs.iter();
|
||||
let head = it.find(|r| r.typ == rt::SP_CONTAINER).map(|r| shape(r.body, budget))?;
|
||||
let mut members = Vec::new();
|
||||
for rec in it {
|
||||
if let Some(n) = node(rec, depth + 1, budget) {
|
||||
members.push(n);
|
||||
}
|
||||
}
|
||||
Some(Node::Group(head, members))
|
||||
}
|
||||
|
||||
/// A shape or group record as a node (`None` for other records, deleted shapes or past the
|
||||
/// depth cap).
|
||||
fn node(rec: &Rec, depth: usize, budget: &mut usize) -> Option<Node> {
|
||||
match rec.typ {
|
||||
rt::SP_CONTAINER => Some(shape(rec.body, budget)).filter(|s| !s.deleted()).map(Node::Shape),
|
||||
rt::SPGR_CONTAINER if depth < MAX_DEPTH => group(rec.body, depth, budget),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The document's drawings: the BLIP store and the top-level shapes of each drawing by spid.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Content<'a> {
|
||||
/// `OfficeArtFBSE` bodies, in BLIP store order (`pib` 1 = the first).
|
||||
pub(crate) bstore: Vec<&'a [u8]>,
|
||||
/// The main document's shapes (`dgglbl` 0) and the headers' (`dgglbl` 1), by spid.
|
||||
pub(crate) main: HashMap<u32, Node>,
|
||||
pub(crate) header: HashMap<u32, Node>,
|
||||
}
|
||||
|
||||
/// Parse `OfficeArtContent` (the `fcDggInfo` blob of the Table stream): an
|
||||
/// `OfficeArtDggContainer` followed by (`dgglbl`, `OfficeArtDgContainer`) drawings.
|
||||
pub(crate) fn content(blob: &[u8]) -> Content<'_> {
|
||||
let mut out = Content::default();
|
||||
let mut budget = MAX_RECORDS;
|
||||
let Some((dgg, mut at)) = record(blob) else { return out };
|
||||
if dgg.typ == rt::DGG_CONTAINER {
|
||||
for rec in children(dgg.body, &mut budget) {
|
||||
if rec.typ == rt::BSTORE_CONTAINER {
|
||||
out.bstore = children(rec.body, &mut budget).into_iter().filter(|r| r.typ == rt::FBSE).map(|r| r.body).take(MAX_BSE).collect();
|
||||
}
|
||||
}
|
||||
}
|
||||
for _ in 0..MAX_DRAWINGS {
|
||||
let Some(&label) = blob.get(at) else { break };
|
||||
let Some((dg, used)) = blob.get(at + 1..).and_then(record) else { break };
|
||||
at = at.saturating_add(1 + used);
|
||||
if dg.typ != rt::DG_CONTAINER {
|
||||
continue;
|
||||
}
|
||||
let into = if label == 0 { &mut out.main } else { &mut out.header };
|
||||
for rec in children(dg.body, &mut budget) {
|
||||
if rec.typ != rt::SPGR_CONTAINER {
|
||||
continue;
|
||||
}
|
||||
// The top group: its first shape is the patriarch; the members are the drawing's shapes.
|
||||
if let Some(Node::Group(_, members)) = group(rec.body, 0, &mut budget) {
|
||||
for n in members {
|
||||
into.entry(n.sp().spid).or_insert(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -1,200 +0,0 @@
|
||||
//! The piece table ([MS-DOC] `Clx`/`Pcdt`/`PlcPcd`): where every character position lives in
|
||||
//! the WordDocument stream, and how it is encoded.
|
||||
|
||||
use wordcraft_doc::encoding::cp1252;
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
/// Most pieces any real file has; more means a corrupt `PlcPcd`.
|
||||
const MAX_PIECES: usize = 1_000_000;
|
||||
|
||||
/// One piece: text at FcCompressed `fc` (30-bit offset + compression bit).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Pcd {
|
||||
fc: u32,
|
||||
compressed: bool,
|
||||
/// `Prm`: either a single sprm (Prm0) or an index into the Clx's Prc grpprls (Prm1).
|
||||
prm: u16,
|
||||
}
|
||||
|
||||
/// The parsed piece table plus the grpprls of the Prcs that Prm1 references.
|
||||
pub(crate) struct PieceTable {
|
||||
/// n+1 character-position starts.
|
||||
cps: Vec<u32>,
|
||||
pieces: Vec<Pcd>,
|
||||
prc_grpprls: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl PieceTable {
|
||||
/// Parse the `Clx` at `table[fc .. fc+lcb]`.
|
||||
pub(crate) fn parse(table: &[u8], fc: u32, lcb: u32) -> Result<PieceTable, DocbinError> {
|
||||
let fc = fc as usize;
|
||||
let lcb = lcb as usize;
|
||||
if lcb == 0 {
|
||||
return Err(DocbinError::Malformed("empty Clx".into()));
|
||||
}
|
||||
let end = fc.checked_add(lcb).ok_or_else(|| DocbinError::Malformed("Clx offset overflow".into()))?;
|
||||
let clx = table.get(fc..end).ok_or_else(|| DocbinError::Malformed(format!("Clx at {fc}+{lcb} is outside the Table stream")))?;
|
||||
// A Clx is an array of Prc (clxt 0x01, each holding a grpprl that Prm1 values can
|
||||
// reference) followed by the Pcdt (clxt 0x02) that holds the piece table.
|
||||
let mut prc_grpprls = Vec::new();
|
||||
let mut i = 0usize;
|
||||
let plc = loop {
|
||||
let Some(&clxt) = clx.get(i) else {
|
||||
return Err(DocbinError::Malformed("Clx ends before the Pcdt".into()));
|
||||
};
|
||||
match clxt {
|
||||
0x01 => {
|
||||
let cb = clx
|
||||
.get(i + 1..i + 3)
|
||||
.map(|b| u16::from_le_bytes([b[0], b[1]]) as usize)
|
||||
.ok_or_else(|| DocbinError::Malformed("truncated Prc".into()))?;
|
||||
let stop = i.checked_add(3).and_then(|v| v.checked_add(cb)).ok_or_else(|| DocbinError::Malformed("Prc size overflow".into()))?;
|
||||
if let Some(g) = clx.get(i + 3..stop)
|
||||
&& prc_grpprls.len() < MAX_PIECES
|
||||
{
|
||||
prc_grpprls.push(g.to_vec());
|
||||
}
|
||||
i = stop;
|
||||
}
|
||||
0x02 => {
|
||||
let lcb_plc = clx
|
||||
.get(i + 1..i + 5)
|
||||
.map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize)
|
||||
.ok_or_else(|| DocbinError::Malformed("truncated Pcdt".into()))?;
|
||||
let start = i.checked_add(5).ok_or_else(|| DocbinError::Malformed("Pcdt offset overflow".into()))?;
|
||||
let stop = start.checked_add(lcb_plc).ok_or_else(|| DocbinError::Malformed("Pcdt size overflow".into()))?;
|
||||
break clx.get(start..stop).ok_or_else(|| DocbinError::Malformed("PlcPcd is outside the Clx".into()))?;
|
||||
}
|
||||
other => return Err(DocbinError::Malformed(format!("unexpected Clx item 0x{other:02X}"))),
|
||||
}
|
||||
};
|
||||
// PlcPcd: aCp[n+1] u32s followed by aPcd[n] Pcds (8 bytes each).
|
||||
if plc.len() < 4 || (plc.len() - 4) % 12 != 0 {
|
||||
return Err(DocbinError::Malformed(format!("PlcPcd is {} bytes", plc.len())));
|
||||
}
|
||||
let n = (plc.len() - 4) / 12;
|
||||
if n > MAX_PIECES {
|
||||
return Err(DocbinError::Limit(format!("piece table has {n} pieces")));
|
||||
}
|
||||
let mut cps = Vec::with_capacity(n + 1);
|
||||
for j in 0..=n {
|
||||
let at = j * 4;
|
||||
let Some(b) = plc.get(at..at + 4) else {
|
||||
return Err(DocbinError::Malformed("truncated aCp".into()));
|
||||
};
|
||||
// Keep the CPs non-decreasing so binary search over them is sound: an inverted
|
||||
// piece in a corrupt file becomes an empty one.
|
||||
let c = u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
|
||||
cps.push(c.max(cps.last().copied().unwrap_or(0)));
|
||||
}
|
||||
let mut pieces = Vec::with_capacity(n);
|
||||
for j in 0..n {
|
||||
let at = (n + 1) * 4 + j * 8 + 2;
|
||||
let Some(fc_raw) = plc.get(at..at + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) else {
|
||||
return Err(DocbinError::Malformed("truncated aPcd".into()));
|
||||
};
|
||||
let prm = plc.get(at + 4..at + 6).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0);
|
||||
pieces.push(Pcd { fc: fc_raw & 0x3FFF_FFFF, compressed: fc_raw & 0x4000_0000 != 0, prm });
|
||||
}
|
||||
Ok(PieceTable { cps, pieces, prc_grpprls })
|
||||
}
|
||||
|
||||
/// The Prls this piece's `Prm` adds. Bit 0 (`fComplex`) discriminates: a Prm0
|
||||
/// ([MS-DOC] §2.9.215) holds a 7-bit `isprm` in bits 1-7 and its one-byte operand in
|
||||
/// bits 8-15; a Prm1 (§2.9.216) holds a zero-based index into the Clx's Prcs in bits
|
||||
/// 1-15.
|
||||
pub(crate) fn piece_prm(&self, cp: u32) -> PrmRef<'_> {
|
||||
let Some(p) = self.piece_of(cp) else { return PrmRef::None };
|
||||
if p.prm & 1 == 0 {
|
||||
let isprm = ((p.prm >> 1) & 0x7F) as u8;
|
||||
if isprm == 0 {
|
||||
return PrmRef::None;
|
||||
}
|
||||
PrmRef::Sprm0 { isprm, val: (p.prm >> 8) as u8 }
|
||||
} else {
|
||||
PrmRef::Grpprl(self.prc_grpprls.get((p.prm >> 1) as usize).map(Vec::as_slice))
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw Prm of the piece holding `cp` (0, no properties, when there is none): two CPs
|
||||
/// with the same value get the same [`PrmRef`].
|
||||
pub(crate) fn prm_of(&self, cp: u32) -> u16 {
|
||||
self.piece_of(cp).map_or(0, |p| p.prm)
|
||||
}
|
||||
|
||||
fn piece_of(&self, cp: u32) -> Option<&Pcd> {
|
||||
let i = self.cps.partition_point(|&c| c <= cp).checked_sub(1)?;
|
||||
if i >= self.pieces.len() {
|
||||
return None;
|
||||
}
|
||||
self.pieces.get(i)
|
||||
}
|
||||
|
||||
/// The FC of a character position in the space the FKP bin tables use: true byte
|
||||
/// offsets into the WordDocument stream. A compressed piece stores its base `fc`
|
||||
/// doubled (`byte = fc/2`), so the doubling unwinds here; an uncompressed piece's `fc`
|
||||
/// already is its byte offset and each character covers two bytes.
|
||||
pub(crate) fn fc_of_cp(&self, cp: u32) -> Option<u32> {
|
||||
let p = self.piece_of(cp)?;
|
||||
let i = self.cps.partition_point(|&c| c <= cp).checked_sub(1)?;
|
||||
let cp0 = *self.cps.get(i)?;
|
||||
let step = cp.checked_sub(cp0)?.checked_mul(2)?;
|
||||
if p.compressed { p.fc.checked_add(step)?.checked_div(2) } else { p.fc.checked_add(step) }
|
||||
}
|
||||
|
||||
/// The CP just past the last piece: no text exists at or beyond it.
|
||||
pub(crate) fn cp_end(&self) -> u32 {
|
||||
self.cps.last().copied().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// The character stored at `cp` (control characters included) and how many CPs it
|
||||
/// covers (2 for a surrogate pair), found by binary search over the pieces.
|
||||
/// `Err(next)` when there is no readable character at `cp` (a gap between pieces, an
|
||||
/// inverted piece, or piece bytes past the end of the stream): `next` is the CP to
|
||||
/// resume at, always greater than `cp` (`u32::MAX` past the last piece), so callers
|
||||
/// skip an unreadable range in one step instead of one CP at a time.
|
||||
pub(crate) fn char_at(&self, word: &[u8], cp: u32) -> Result<(char, u32), u32> {
|
||||
let i = self.cps.partition_point(|&c| c <= cp);
|
||||
// The next piece boundary above `cp`, or the end of the CP space.
|
||||
let next = self.cps.get(i).copied().filter(|&n| n > cp).unwrap_or(u32::MAX);
|
||||
let Some(i) = i.checked_sub(1) else { return Err(next) };
|
||||
let (Some(p), Some(&cp0)) = (self.pieces.get(i), self.cps.get(i)) else { return Err(u32::MAX) };
|
||||
let Some(off) = cp.checked_sub(cp0) else { return Err(next) };
|
||||
let off = off as usize;
|
||||
if p.compressed {
|
||||
// 8-bit, Windows-1252, one byte per character, stored at fc/2.
|
||||
let at = ((p.fc / 2) as usize).checked_add(off);
|
||||
return match at.and_then(|a| word.get(a)) {
|
||||
Some(&b) => Ok((cp1252(b), 1)),
|
||||
None => Err(next),
|
||||
};
|
||||
}
|
||||
// UTF-16LE, one 16-bit unit per character, stored at fc.
|
||||
let unit = |k: usize| -> Option<u16> {
|
||||
let at = (p.fc as usize).checked_add(k.checked_mul(2)?)?;
|
||||
word.get(at..at.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]]))
|
||||
};
|
||||
let Some(u) = unit(off) else { return Err(next) };
|
||||
if (0xD800..0xDC00).contains(&u)
|
||||
&& cp.saturating_add(1) < next
|
||||
&& let Some(lo) = unit(off.saturating_add(1)).filter(|lo| (0xDC00..0xE000).contains(lo))
|
||||
&& let Some(Ok(c)) = char::decode_utf16([u, lo]).next()
|
||||
{
|
||||
return Ok((c, 2));
|
||||
}
|
||||
Ok((char::from_u32(u as u32).unwrap_or('\u{FFFD}'), 1))
|
||||
}
|
||||
}
|
||||
|
||||
/// What a piece's `Prm` refers to.
|
||||
pub(crate) enum PrmRef<'a> {
|
||||
None,
|
||||
/// Prm0: a short sprm index into the isprm table, with a 1-byte operand.
|
||||
Sprm0 {
|
||||
isprm: u8,
|
||||
val: u8,
|
||||
},
|
||||
/// Prm1: a grpprl from a Prc (absent if out of range).
|
||||
Grpprl(Option<&'a [u8]>),
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
//! Sections and headers/footers: `PlcfSed` → `Sed` → `Sepx` (grpprls of section sprms) and
|
||||
//! the `Plcfhdd` stories of the header subdocument ([MS-DOC] §2.8.25, §2.9.244).
|
||||
|
||||
use wordcraft_doc::section::NumFormat;
|
||||
use wordcraft_doc::section::{Columns, SectionProps, SectionStart};
|
||||
|
||||
use crate::fib::{Fib, pair};
|
||||
use crate::sprm::{self, Prl};
|
||||
|
||||
/// Most sections we read.
|
||||
const MAX_SECTIONS: usize = 1024;
|
||||
|
||||
// Section sprms ([MS-DOC] §2.6.4).
|
||||
const S_BKC: u16 = 0x3009;
|
||||
const S_TITLE_PAGE: u16 = 0x300A; // sprmSFTitlePage
|
||||
const S_CCOLUMNS: u16 = 0x500B;
|
||||
const S_PGN_START: u16 = 0x300C; // sprmSPgnStart
|
||||
const S_ORIENTATION: u16 = 0x301D; // sprmSBOrientation
|
||||
const S_HDR_TOP: u16 = 0xB017;
|
||||
const S_HDR_BOTTOM: u16 = 0xB018;
|
||||
const S_XA_PAGE: u16 = 0xB01F;
|
||||
const S_YA_PAGE: u16 = 0xB020;
|
||||
const S_DXA_LEFT: u16 = 0xB021;
|
||||
const S_DXA_RIGHT: u16 = 0xB022;
|
||||
const S_DYA_TOP: u16 = 0x9023;
|
||||
const S_DYA_BOTTOM: u16 = 0x9024;
|
||||
const S_DXA_GUTTER: u16 = 0xB025;
|
||||
|
||||
/// One section: covers paragraphs up to (excluding) `end_cp`, with its properties.
|
||||
pub(crate) struct Section {
|
||||
pub(crate) end_cp: u32,
|
||||
pub(crate) props: SectionProps,
|
||||
}
|
||||
|
||||
/// Parse `PlcfSed` and each section's `Sepx` grpprl.
|
||||
pub(crate) fn parse(word: &[u8], table: &[u8], fib: &Fib) -> Vec<Section> {
|
||||
let Some((fc, lcb)) = fib.pair(pair::PLCF_SED) else { return Vec::new() };
|
||||
let Some(plc) = table.get(fc as usize..(fc as usize).saturating_add(lcb as usize)) else { return Vec::new() };
|
||||
// PlcSed: aCp[n+1] u32s + aSed[n] (Sed = fn 2, fcSepx 4, fnMpr 2, fcMpr 4).
|
||||
if plc.len() < 4 || (plc.len() - 4) % 16 != 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let n = (plc.len() - 4) / 16;
|
||||
let cp_at = |i: usize| -> Option<u32> { plc.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) };
|
||||
let mut out = Vec::with_capacity(n.min(MAX_SECTIONS));
|
||||
for i in 0..n.min(MAX_SECTIONS) {
|
||||
let Some(end_cp) = cp_at(i + 1) else { break };
|
||||
let sed_at = (n + 1) * 4 + i * 12;
|
||||
let fc_sepx = plc.get(sed_at + 2..sed_at + 6).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])).unwrap_or(0xFFFF_FFFF);
|
||||
let mut props = SectionProps::default();
|
||||
// Sepx: cb u16, then a grpprl of section sprms.
|
||||
let mut explicit_orient = false;
|
||||
if let Some(grpprl) =
|
||||
word.get(fc_sepx as usize..).and_then(|sepx| sepx.get(..2).map(|b| u16::from_le_bytes([b[0], b[1]]) as usize)).and_then(|cb| {
|
||||
let at = (fc_sepx as usize).checked_add(2)?;
|
||||
word.get(at..at.checked_add(cb)?)
|
||||
})
|
||||
{
|
||||
for prl in sprm::iter(grpprl) {
|
||||
// Only a defined operand counts as explicit; anything else leaves the
|
||||
// orientation to the page shape.
|
||||
if prl.op == S_ORIENTATION && matches!(prl.operand.first(), Some(1 | 2)) {
|
||||
explicit_orient = true;
|
||||
}
|
||||
apply_section(&mut props, &prl);
|
||||
}
|
||||
}
|
||||
// Without an explicit orientation sprm, infer it from the page shape.
|
||||
if !explicit_orient {
|
||||
if props.page_w < props.page_h {
|
||||
props.landscape = false;
|
||||
} else if props.page_w > props.page_h {
|
||||
props.landscape = true;
|
||||
}
|
||||
}
|
||||
out.push(Section { end_cp, props });
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Apply a section sprm to the model's section properties. All the measurement sprms here
|
||||
/// carry 16-bit twip operands ([MS-DOC] §2.6.4).
|
||||
fn apply_section(s: &mut SectionProps, prl: &Prl) {
|
||||
let tw = |v: i32| (v as f32 / 20.0).clamp(0.0, 1584.0);
|
||||
let s16 = |prl: &Prl| -> Option<i16> {
|
||||
match prl.operand {
|
||||
[b0, b1, ..] => Some(i16::from_le_bytes([*b0, *b1])),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
match prl.op {
|
||||
S_BKC => {
|
||||
s.start = match prl.operand.first().copied().unwrap_or(2) {
|
||||
// SBkcOperand ([MS-DOC] sprmSBkc): bkcContinuous, bkcNewColumn, bkcNewPage,
|
||||
// bkcEvenPage, bkcOddPage; anything else falls back to the default (new page).
|
||||
0 => SectionStart::Continuous,
|
||||
1 => SectionStart::NextColumn,
|
||||
3 => SectionStart::EvenPage,
|
||||
4 => SectionStart::OddPage,
|
||||
_ => SectionStart::NextPage,
|
||||
};
|
||||
}
|
||||
S_TITLE_PAGE => s.title_page = prl.operand.first().is_some_and(|&v| v != 0),
|
||||
S_CCOLUMNS => {
|
||||
if let Some(c) = s16(prl) {
|
||||
s.columns = Columns { count: (c as u32).saturating_add(1).min(44), ..Default::default() };
|
||||
}
|
||||
}
|
||||
S_PGN_START => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.page_num_start = Some(v.max(0) as u32);
|
||||
s.page_num_format = NumFormat::Decimal;
|
||||
}
|
||||
}
|
||||
// SBOrientationOperand ([MS-DOC] §2.9.236): 1 portrait, 2 landscape.
|
||||
S_ORIENTATION => match prl.operand.first() {
|
||||
Some(1) => s.landscape = false,
|
||||
Some(2) => s.landscape = true,
|
||||
_ => {}
|
||||
},
|
||||
S_HDR_TOP => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.header = tw(v as i32);
|
||||
}
|
||||
}
|
||||
S_HDR_BOTTOM => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.footer = tw(v as i32);
|
||||
}
|
||||
}
|
||||
S_XA_PAGE => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.page_w = tw(v as i32).max(7.2);
|
||||
}
|
||||
}
|
||||
S_YA_PAGE => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.page_h = tw(v as i32).max(7.2);
|
||||
}
|
||||
}
|
||||
S_DXA_LEFT => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.margin_left = tw(v as i32);
|
||||
}
|
||||
}
|
||||
S_DXA_RIGHT => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.margin_right = tw(v as i32);
|
||||
}
|
||||
}
|
||||
S_DYA_TOP => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.margin_top = tw(v as i32);
|
||||
}
|
||||
}
|
||||
S_DYA_BOTTOM => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.margin_bottom = tw(v as i32);
|
||||
}
|
||||
}
|
||||
S_DXA_GUTTER => {
|
||||
if let Some(v) = s16(prl) {
|
||||
s.gutter = tw(v as i32);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// The header stories' CP ranges from `Plcfhdd` (a PLC of bare CPs): story i spans
|
||||
/// `[cps[i], cps[i+1])`. Stories 0-5 are footnote/endnote separators; every section then owns
|
||||
/// six stories in the order even header, odd (default) header, even footer, odd (default)
|
||||
/// footer, first header, first footer. The CPs are relative to the header subdocument.
|
||||
///
|
||||
/// The CPs of a well-formed PLC ascend, so the stories are disjoint. A story starting before
|
||||
/// the end of an earlier one (a hostile table pointing many sections at the same text, each
|
||||
/// read into its own part) is left empty, like a slot linked to the previous section's:
|
||||
/// every subdocument CP becomes at most one story.
|
||||
pub(crate) fn header_stories(table: &[u8], fib: &Fib) -> Vec<(u32, u32)> {
|
||||
let Some((fc, lcb)) = fib.pair(pair::PLCF_HDD) else { return Vec::new() };
|
||||
let Some(plc) = table.get(fc as usize..(fc as usize).saturating_add(lcb as usize)) else { return Vec::new() };
|
||||
// Only the six separator stories and six per section (at most 1024) are ever used.
|
||||
let n = (lcb as usize / 4).min(6 + 6 * MAX_SECTIONS + 1);
|
||||
let cp_at = |i: usize| -> Option<u32> { plc.get(i * 4..i * 4 + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) };
|
||||
let mut stories = Vec::with_capacity(n);
|
||||
let mut kept_to = 0u32;
|
||||
for i in 0..n.saturating_sub(1) {
|
||||
if let (Some(a), Some(b)) = (cp_at(i), cp_at(i + 1)) {
|
||||
let b = if a < kept_to { a } else { b.max(a) };
|
||||
kept_to = kept_to.max(b);
|
||||
stories.push((a, b));
|
||||
}
|
||||
}
|
||||
stories
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
//! Sprm decoding ([MS-DOC] §2.6). A Prl is a 2-byte opcode followed by an operand whose
|
||||
//! size comes from the opcode's `spra` field (with two variable-length exceptions).
|
||||
|
||||
/// `sprmPChgTabs`: custom tab stops added and removed ([MS-DOC] §2.6.3, PChgTabsOperand).
|
||||
pub(crate) const P_CHG_TABS: u16 = 0xC615;
|
||||
/// `sprmPChgTabsPapx`: the same for paragraph and style formatting (PChgTabsPapxOperand).
|
||||
pub(crate) const P_CHG_TABS_PAPX: u16 = 0xC60D;
|
||||
/// `sprmTDefTable`: table cell borders and widths.
|
||||
const T_DEF_TABLE: u16 = 0xD608;
|
||||
|
||||
/// One decoded Prl: an opcode plus its operand bytes.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct Prl<'a> {
|
||||
pub(crate) op: u16,
|
||||
pub(crate) operand: &'a [u8],
|
||||
}
|
||||
|
||||
impl Prl<'_> {
|
||||
/// Which kind of property this sprm modifies: 1 paragraph, 2 character, 3 picture,
|
||||
/// 4 section, 5 table ([MS-DOC] Sprm.sgc).
|
||||
pub(crate) fn sgc(&self) -> u8 {
|
||||
((self.op >> 10) & 7) as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate the Prls of a grpprl. Malformed tails are dropped.
|
||||
pub(crate) fn iter(grpprl: &[u8]) -> impl Iterator<Item = Prl<'_>> {
|
||||
let mut rest = grpprl;
|
||||
std::iter::from_fn(move || {
|
||||
if rest.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let op = u16::from_le_bytes([rest[0], rest[1]]);
|
||||
let after = &rest[2..];
|
||||
// Operand size per spra, with the exceptions whose size byte lies.
|
||||
let len = match (op >> 13, op) {
|
||||
// PChgTabsOperand.cb is one byte, the size after it; 255 means "work it out" from the
|
||||
// two counts: cb, cTabs, 4 bytes per removed stop, cTabs, 3 bytes per added stop.
|
||||
(_, P_CHG_TABS) => match after.first().copied() {
|
||||
Some(255) => after.get(1).and_then(|&del| {
|
||||
let add = after.get(2 + 4 * del as usize).copied()?;
|
||||
Some(1 + 1 + 4 * del as usize + 1 + 3 * add as usize)
|
||||
}),
|
||||
Some(cb) => Some(1 + cb as usize),
|
||||
None => None,
|
||||
},
|
||||
// TDefTableOperand.cb counts the bytes after cb, incremented by 1, so the
|
||||
// operand is one byte longer than cb.
|
||||
(_, T_DEF_TABLE) => after.get(..2).map(|b| 1 + u16::from_le_bytes([b[0], b[1]]) as usize),
|
||||
(0, _) | (1, _) => Some(1),
|
||||
(2, _) | (4, _) | (5, _) => Some(2),
|
||||
(3, _) => Some(4),
|
||||
(7, _) => Some(3),
|
||||
(6, _) => after.first().map(|&n| 1 + n as usize),
|
||||
_ => None,
|
||||
}?;
|
||||
let operand = after.get(..len)?;
|
||||
rest = &after[len.min(after.len())..];
|
||||
Some(Prl { op, operand })
|
||||
})
|
||||
}
|
||||
|
||||
/// A ToggleOperand value: 0 = off, 1 = on, 0x80 = leave unchanged, 0x81 = invert.
|
||||
pub(crate) enum Toggle {
|
||||
Off,
|
||||
On,
|
||||
Keep,
|
||||
Invert,
|
||||
}
|
||||
|
||||
pub(crate) fn toggle(p: &Prl) -> Toggle {
|
||||
match p.operand.first().copied().unwrap_or(0) {
|
||||
0x81 => Toggle::Invert,
|
||||
0x80 => Toggle::Keep,
|
||||
0x00 => Toggle::Off,
|
||||
_ => Toggle::On,
|
||||
}
|
||||
}
|
||||
|
||||
/// The isprm indices of Prm0 that map to sprms we understand ([MS-DOC] §2.9.215 table).
|
||||
pub(crate) fn from_prm0(isprm: u8) -> Option<u16> {
|
||||
Some(match isprm {
|
||||
0x05 => 0x2461, // sprmPJc
|
||||
0x07 => 0x2405, // sprmPFKeep
|
||||
0x08 => 0x2406, // sprmPFKeepFollow
|
||||
0x09 => 0x2407, // sprmPFPageBreakBefore
|
||||
0x33 => 0x2435, // sprmPWidowControl (low byte of the operand is the value)
|
||||
0x78 => 0x2640, // sprmPOutLvl
|
||||
0x4D => 0x2A0C, // sprmCHighlight
|
||||
0x55 => 0x0835, // sprmCFBold
|
||||
0x56 => 0x0836, // sprmCFItalic
|
||||
0x57 => 0x0837, // sprmCFStrike
|
||||
0x58 => 0x0838, // sprmCFOutline
|
||||
0x59 => 0x0839, // sprmCFShadow
|
||||
0x5A => 0x083A, // sprmCFSmallCaps
|
||||
0x5B => 0x083B, // sprmCFCaps
|
||||
0x5C => 0x083C, // sprmCFVanish
|
||||
0x5E => 0x2A3E, // sprmCKul
|
||||
0x62 => 0x2A42, // sprmCIco
|
||||
0x74 => 0x0854, // sprmCFImprint
|
||||
0x75 => 0x0855, // sprmCFSpec
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
//! The stylesheet (`STSH`) and the font table (`SttbfFfn`) from the Table stream
|
||||
//! ([MS-DOC] §2.9.271 and §2.9.247).
|
||||
|
||||
use crate::DocbinError;
|
||||
|
||||
/// Largest stylesheet we accept (styles are 0x0FFE max by the spec; stay stricter).
|
||||
const MAX_STYLES: usize = 4096;
|
||||
|
||||
/// One raw style record (`STD`).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct RawStyle {
|
||||
/// 1 paragraph, 2 character, 3 table, 4 numbering.
|
||||
pub(crate) stk: u8,
|
||||
/// Zero-based index of the parent style; 0x0FFF = none.
|
||||
pub(crate) istd_base: u16,
|
||||
pub(crate) name: String,
|
||||
/// Paragraph-formatting UPX grpprl (paragraph styles).
|
||||
pub(crate) papx: Vec<u8>,
|
||||
/// Character-formatting UPX grpprl (paragraph and character styles).
|
||||
pub(crate) chpx: Vec<u8>,
|
||||
}
|
||||
|
||||
/// The stylesheet's standard fonts, the ones text takes when neither its style nor its own
|
||||
/// formatting names a font: font-table indexes of the ASCII, East Asian and other
|
||||
/// (non-East-Asian, non-ASCII) fonts (`Stshif.rgftcStandardChpStsh`, [MS-DOC] §2.9.273) and the
|
||||
/// complex-script font (`Stshi.ftcBi`, §2.9.272). `None` where the header is too short for one.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub(crate) struct StandardFonts {
|
||||
pub(crate) ascii: Option<u16>,
|
||||
pub(crate) east_asian: Option<u16>,
|
||||
pub(crate) other: Option<u16>,
|
||||
pub(crate) bidi: Option<u16>,
|
||||
}
|
||||
|
||||
/// `StshiLsd.cbLSD`: the size of a latent style's `LSD` ([MS-DOC] `StshiLsd`).
|
||||
const LSD_SIZE: u16 = 4;
|
||||
|
||||
/// The [`StandardFonts`] in the header (`Stshi`) of the `STSH` at `table[fc..fc+lcb]`: Word 365
|
||||
/// writes Aptos there while the font table's first entry is still Times New Roman (#5066).
|
||||
pub(crate) fn standard_fonts(table: &[u8], fc: u32, lcb: u32) -> StandardFonts {
|
||||
let Some(stsh) = (fc as usize).checked_add(lcb as usize).and_then(|end| table.get(fc as usize..end)) else { return StandardFonts::default() };
|
||||
let cb_stshi = stsh.get(0..2).map_or(0, |b| u16::from_le_bytes([b[0], b[1]]) as usize);
|
||||
let stshi = stsh.get(2..).map_or(&[][..], |s| s.get(..cb_stshi.min(s.len())).unwrap_or(&[]));
|
||||
// Stshif: cstd, cbSTDBaseInFile, flags, stiMaxWhenSaved, istdMaxFixedWhenSaved,
|
||||
// nVerBuiltInNamesWhenSaved (2 bytes each), then rgftcStandardChpStsh (3 × 2); ftcBi follows.
|
||||
let at = |o: usize| stshi.get(o..o + 2).map(|b| u16::from_le_bytes([b[0], b[1]]));
|
||||
// ftcBi is optional, and the latent style data (`StshiLsd`, whose `cbLSD` MUST be 4) follows
|
||||
// it: a 4 right after it says it is there, a 4 in its place with more data after says it was
|
||||
// left out. A header that ends right after the field (as Word 97's) has it, a 4 included.
|
||||
let bidi = match (at(18), at(20)) {
|
||||
(b, Some(LSD_SIZE) | None) => b,
|
||||
(Some(LSD_SIZE), Some(_)) => None,
|
||||
(b, _) => b,
|
||||
};
|
||||
StandardFonts { ascii: at(12), east_asian: at(14), other: at(16), bidi }
|
||||
}
|
||||
|
||||
/// Parse the `STSH` at `table[fc..fc+lcb]`.
|
||||
pub(crate) fn parse(table: &[u8], fc: u32, lcb: u32) -> Result<Vec<RawStyle>, DocbinError> {
|
||||
let fc = fc as usize;
|
||||
let lcb = lcb as usize;
|
||||
let end = fc.checked_add(lcb).ok_or_else(|| DocbinError::Malformed("STSH overflow".into()))?;
|
||||
let stsh = table.get(fc..end).ok_or_else(|| DocbinError::Malformed("STSH outside the Table stream".into()))?;
|
||||
if stsh.len() < 4 {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
// LPStshi: cbStshi u16 + Stshi; the style count and the size of each Stdf are what we need.
|
||||
let cb_stshi = u16::from_le_bytes([stsh[0], stsh[1]]) as usize;
|
||||
let stshi = stsh.get(2..2 + cb_stshi).unwrap_or(&[]);
|
||||
if stshi.len() < 4 {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let cstd = u16::from_le_bytes([stshi[0], stshi[1]]) as usize;
|
||||
let cb_std_base = u16::from_le_bytes([stshi[2], stshi[3]]) as usize;
|
||||
// 10 (Word 97) or 18 (with StdfPost2000); anything else is corrupt but tolerated at 10.
|
||||
let cb_std_base = if cb_std_base == 0x12 { 18 } else { 10 };
|
||||
let cstd = cstd.min(MAX_STYLES);
|
||||
let mut rest = stsh.get(2 + cb_stshi..).unwrap_or(&[]);
|
||||
let mut out = Vec::with_capacity(cstd);
|
||||
for _ in 0..cstd {
|
||||
if rest.len() < 2 || out.len() >= MAX_STYLES {
|
||||
break;
|
||||
}
|
||||
let cb_std = u16::from_le_bytes([rest[0], rest[1]]) as usize;
|
||||
let (std, next) = rest.split_at(rest.len().min(2 + cb_std));
|
||||
rest = next;
|
||||
if cb_std == 0 {
|
||||
out.push(RawStyle::default()); // empty style: keeps istd numbering aligned
|
||||
continue;
|
||||
}
|
||||
let Some(stdf) = std.get(2..2 + cb_std_base.min(cb_std.saturating_sub(2))) else {
|
||||
out.push(RawStyle::default());
|
||||
continue;
|
||||
};
|
||||
// StdfBase ([MS-DOC] §2.9.260, 10 bytes): sti(12)+flags(4); stk(4)+istdBase(12);
|
||||
// cupx(4)+istdNext(12); bchUpe(16) (= cbStd); grfstd(16). istdBase is a zero-based
|
||||
// index into the stylesheet, 0x0FFF meaning "no parent".
|
||||
let stk = (stdf.get(2).copied().unwrap_or(1) & 0x0F).max(1);
|
||||
let w1 = stdf.get(2..4).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0xFFF1);
|
||||
let istd_base = w1 >> 4;
|
||||
let w2 = stdf.get(4..6).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0);
|
||||
let cupx = (w2 & 0x0F) as usize;
|
||||
let _istd_next = w2 >> 4; // next-paragraph style; unused for now
|
||||
let mut at = 2 + cb_std_base;
|
||||
// xstzName: Xst (cch u16 + UTF-16LE) + terminator u16.
|
||||
let mut name = String::new();
|
||||
if let Some(cch) = std.get(at..at + 2).map(|b| u16::from_le_bytes([b[0], b[1]])).map(|c| (c as usize).min(256)) {
|
||||
if let Some(units) = std.get(at + 2..at + 2 + cch * 2) {
|
||||
let chars: Vec<u16> = units.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect();
|
||||
name = String::from_utf16_lossy(&chars);
|
||||
}
|
||||
at += 2 + cch * 2 + 2;
|
||||
}
|
||||
// grLPUpxSw: cupx entries, each cbUpx u16 + data (+ pad to even).
|
||||
let mut upxs: Vec<Vec<u8>> = Vec::new();
|
||||
for _ in 0..cupx.min(3) {
|
||||
let Some(cb) = std.get(at..at + 2).map(|b| u16::from_le_bytes([b[0], b[1]])).map(|v| v as usize) else { break };
|
||||
let data = std.get(at + 2..at + 2 + cb).unwrap_or(&[]).to_vec();
|
||||
upxs.push(data);
|
||||
at += 2 + cb + (cb & 1); // padded to even; cb in the header excludes the pad
|
||||
}
|
||||
// Paragraph styles: UPX0 is a PAPX (istd u16 + grpprl), UPX1 a raw grpprl of
|
||||
// character sprms; character styles: UPX0 is the character grpprl.
|
||||
let mut papx = Vec::new();
|
||||
let mut chpx = Vec::new();
|
||||
if stk == 1 {
|
||||
if let Some(u) = upxs.first() {
|
||||
papx = u.get(2..).unwrap_or(&[]).to_vec(); // skip the istd of GrpPrlAndIstd
|
||||
}
|
||||
if let Some(u) = upxs.get(1) {
|
||||
chpx = u.clone();
|
||||
}
|
||||
} else if stk == 2
|
||||
&& let Some(u) = upxs.first()
|
||||
{
|
||||
chpx = u.clone();
|
||||
}
|
||||
out.push(RawStyle { stk, istd_base, name, papx, chpx });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Font names from the `SttbfFfn` at `table[fc..fc+lcb]` ([MS-DOC] §2.9.286): an STTB
|
||||
/// (§2.2.4) whose entries are FFN records (§2.9.82). Each entry is a length (`cchData`: a
|
||||
/// one-byte byte count in the non-extended table the spec requires, a two-byte count of
|
||||
/// 16-bit units in an extended one) followed by the FFN, and that length alone decides where
|
||||
/// the next entry starts: an FFN ends with its primary name and an optional alternate name
|
||||
/// (`xszAlt`), so scanning for the primary name's terminator would land inside the latter.
|
||||
pub(crate) fn fonts(table: &[u8], fc: u32, lcb: u32) -> Vec<String> {
|
||||
let end = match (fc as usize).checked_add(lcb as usize) {
|
||||
Some(e) if e <= table.len() => e,
|
||||
_ => return Vec::new(),
|
||||
};
|
||||
let Some(sttb) = table.get(fc as usize..end) else { return Vec::new() };
|
||||
let u16at = |o: usize| -> Option<u16> { sttb.get(o..o.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]])) };
|
||||
let Some(first) = u16at(0) else { return Vec::new() };
|
||||
// FFN: ffid(1) wWeight(2) chs(1) ixchSzAlt(1) panose(10) fs(24) = 39 bytes, then the
|
||||
// primary name, NUL-terminated, as UTF-16LE in the Word 97 files we accept (8-bit names
|
||||
// from older writers fall back to Windows-1252).
|
||||
let name_of = |ffn: &[u8]| -> String {
|
||||
let tail = ffn.get(FFN_HEADER.min(ffn.len())..).unwrap_or(&[]);
|
||||
let units: Vec<u16> = tail.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).take_while(|&u| u != 0).take(64).collect();
|
||||
let s = String::from_utf16_lossy(&units);
|
||||
if s.chars().any(|c| c as u32 > 0x7F || c.is_ascii_graphic() || c == ' ') || s.is_empty() {
|
||||
s
|
||||
} else {
|
||||
// An 8-bit name reads as garbage through UTF-16; fall back to Windows-1252.
|
||||
tail.iter().take_while(|&&b| b != 0).take(64).map(|&b| wordcraft_doc::encoding::cp1252(b)).collect()
|
||||
}
|
||||
};
|
||||
// STTB header: [fExtend 0xFFFF] cData(2) cbExtra(2); each entry is followed by cbExtra
|
||||
// bytes (0 for font tables, but skip them if present).
|
||||
let (extended, cdata, cb_extra, mut at) =
|
||||
if first == 0xFFFF { (true, u16at(2).unwrap_or(0), u16at(4).unwrap_or(0), 6usize) } else { (false, first, u16at(2).unwrap_or(0), 4usize) };
|
||||
let mut out = Vec::new();
|
||||
for _ in 0..cdata.min(1024) {
|
||||
let (len_bytes, cb) = if extended {
|
||||
let Some(cb) = u16at(at) else { break };
|
||||
(2usize, usize::from(cb).saturating_mul(2))
|
||||
} else {
|
||||
let Some(&cb) = sttb.get(at) else { break };
|
||||
(1usize, usize::from(cb))
|
||||
};
|
||||
let start = at.saturating_add(len_bytes);
|
||||
let stop = start.saturating_add(cb);
|
||||
// A record cut short by the end of the table still yields what it holds.
|
||||
out.push(name_of(sttb.get(start..stop.min(sttb.len())).unwrap_or(&[])));
|
||||
at = stop.saturating_add(usize::from(cb_extra));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Bytes of an FFN before its primary name ([MS-DOC] §2.9.82).
|
||||
const FFN_HEADER: usize = 39;
|
||||
@@ -1,519 +0,0 @@
|
||||
//! Tables: the table sprms (sgc 5) carried by a row-end (TTP) mark's grpprl, and the
|
||||
//! assembly of cell-ended paragraphs into [`Table`] blocks ([MS-DOC] §2.6.3, §2.4.3).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use wordcraft_doc::Rgb;
|
||||
use wordcraft_doc::para::Paragraph;
|
||||
use wordcraft_doc::props::{Align, Borders, CellProps, HeightRule, RowProps, TableProps, VAlign, VMerge};
|
||||
use wordcraft_doc::table::{Cell, Row, Table};
|
||||
use wordcraft_doc::{Block, Blocks, para_block};
|
||||
|
||||
use crate::fmt;
|
||||
use crate::sprm::{self, Prl};
|
||||
|
||||
// Paragraph sprms for table membership.
|
||||
const P_IN_TABLE: u16 = 0x2416;
|
||||
const P_TTP: u16 = 0x2417;
|
||||
/// `sprmPItap`: table depth (we only model depth 1; deeper rows fall back to paragraphs).
|
||||
const P_ITAP: u16 = 0x6649;
|
||||
|
||||
// Table sprms ([MS-DOC] §2.6.3).
|
||||
const T_JC: u16 = 0x5400;
|
||||
const T_JC_90: u16 = 0x548A;
|
||||
const T_DXA_LEFT: u16 = 0x9601;
|
||||
const T_DXA_GAP_HALF: u16 = 0x9602;
|
||||
const T_TABLE_HEADER: u16 = 0x3404;
|
||||
const T_TABLE_BORDERS_80: u16 = 0xD605;
|
||||
const T_ROW_HEIGHT: u16 = 0x9407;
|
||||
const T_DEF_TABLE: u16 = 0xD608;
|
||||
const T_DEF_TABLE_SHD_80: u16 = 0xD609;
|
||||
/// `sprmTDefTableShd` / `Shd2nd` / `Shd3rd`: 10-byte `Shd`s for cells 0-21, 22-43, 44-62.
|
||||
const T_DEF_TABLE_SHD: u16 = 0xD612;
|
||||
const T_DEF_TABLE_SHD_2ND: u16 = 0xD616;
|
||||
const T_DEF_TABLE_SHD_3RD: u16 = 0xD60C;
|
||||
/// `sprmTDefTableShdRaw` / `Raw2nd` / `Raw3rd`: the same `Shd` arrays over the same cell
|
||||
/// ranges, which Word 2007+ writes next to the above.
|
||||
const T_DEF_TABLE_SHD_RAW: u16 = 0xD670;
|
||||
const T_DEF_TABLE_SHD_RAW_2ND: u16 = 0xD671;
|
||||
const T_DEF_TABLE_SHD_RAW_3RD: u16 = 0xD672;
|
||||
const T_CANT_SPLIT_90: u16 = 0x3403;
|
||||
const T_MERGE: u16 = 0x5624;
|
||||
const T_VERT_MERGE: u16 = 0xD62B;
|
||||
const T_VERT_ALIGN: u16 = 0xD62C;
|
||||
const T_SET_SHD: u16 = 0xD62D;
|
||||
const T_CANT_SPLIT: u16 = 0x3466;
|
||||
const T_CELL_WIDTH: u16 = 0xD635;
|
||||
|
||||
/// Most grid columns we build from a `sprmTDefTable` (the model's own cap is 63; one more
|
||||
/// edge is fine but merged rows can sum spans slightly over, so clamp when assembling).
|
||||
const MAX_COLS: usize = 63;
|
||||
|
||||
/// What a paragraph mark says about its table context, from its PAPX.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct RowInfo {
|
||||
pub(crate) in_table: bool,
|
||||
pub(crate) ttp: bool,
|
||||
/// The mark itself was 0x07 (a cell end), set by the walker.
|
||||
pub(crate) cell_mark: bool,
|
||||
/// Table depth from `sprmPItap` (0 or 1 supported; deeper tables degrade to paragraphs).
|
||||
pub(crate) depth: u32,
|
||||
/// Cell-edge positions (points) from `sprmTDefTable` — [MS-DOC] defines them per row,
|
||||
/// so each row keeps its own; ascending in well-formed rows, the first edge usually 0
|
||||
/// (the row origin). Rows without a `sprmTDefTable` keep this empty.
|
||||
pub(crate) edges: Vec<f32>,
|
||||
height: Option<f32>,
|
||||
height_rule: HeightRule,
|
||||
header: bool,
|
||||
cant_split: bool,
|
||||
align: Option<Align>,
|
||||
indent: Option<f32>,
|
||||
borders: Option<Borders>,
|
||||
/// Horizontal merges: (itcFirst, itcLim) cell-index ranges.
|
||||
merges: Vec<(u8, u8)>,
|
||||
/// Per-cell overrides keyed by (pre-merge) cell index.
|
||||
vmerge: Vec<(u8, VMerge)>,
|
||||
/// Later entries win: the `Shd` arrays override the `Shd80` fallback before them, and an
|
||||
/// automatic `Shd` (`None`) clears it.
|
||||
shading: Vec<(u8, Option<Rgb>)>,
|
||||
widths: Vec<(u8, f32)>,
|
||||
width_pcts: Vec<(u8, f32)>,
|
||||
valign: Vec<(u8, VAlign)>,
|
||||
}
|
||||
|
||||
/// Read the table context out of a paragraph mark's PAPX grpprl.
|
||||
impl RowInfo {
|
||||
/// Does the mark belong to a table: inside one, ending a row, or ending a cell?
|
||||
pub(crate) fn is_table(&self) -> bool {
|
||||
self.in_table || self.ttp || self.cell_mark
|
||||
}
|
||||
|
||||
/// The row height from `sprmTDyaRowHeight`, for tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn height(&self) -> Option<f32> {
|
||||
self.height
|
||||
}
|
||||
|
||||
/// The fill cell `i` ends up with (the last shading entry for it wins), for tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn shading_of(&self, i: u8) -> Option<Rgb> {
|
||||
self.shading.iter().rfind(|(k, _)| *k == i).and_then(|(_, s)| *s)
|
||||
}
|
||||
|
||||
/// The width cell `i` ends up with, for tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn width_of(&self, i: u8) -> Option<f32> {
|
||||
self.widths.iter().rfind(|(k, _)| *k == i).map(|(_, w)| *w)
|
||||
}
|
||||
|
||||
/// How many per-cell overrides the row keeps, for tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn overrides(&self) -> usize {
|
||||
self.vmerge.len() + self.shading.len() + self.widths.len() + self.width_pcts.len() + self.valign.len() + self.merges.len()
|
||||
}
|
||||
|
||||
/// The width percentage cell `i` ends up with, for tests.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn width_pct_of(&self, i: u8) -> Option<f32> {
|
||||
self.width_pcts.iter().rfind(|(k, _)| *k == i).map(|(_, w)| *w)
|
||||
}
|
||||
}
|
||||
|
||||
/// Set cell `c`'s override to `v`, replacing an earlier one: one slot per cell, however often
|
||||
/// a PAPX repeats the sprm, so a shared PAPX can't pile up copies on every paragraph mark.
|
||||
fn set<T>(slots: &mut Vec<(u8, T)>, c: u8, v: T) {
|
||||
match slots.iter_mut().find(|(k, _)| *k == c) {
|
||||
Some(slot) => slot.1 = v,
|
||||
None => slots.push((c, v)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn decode(papx: &[u8]) -> RowInfo {
|
||||
let mut r = RowInfo::default();
|
||||
let mut gap_half: Option<i16> = None;
|
||||
let mut dxa_left: Option<i16> = None;
|
||||
for prl in sprm::iter(papx) {
|
||||
// The membership sprms are paragraph sprms (sgc 1); the rest here are table sprms
|
||||
// (sgc 5), so match opcodes directly.
|
||||
match prl.op {
|
||||
P_IN_TABLE => r.in_table = prl.operand.first().copied().unwrap_or(0) != 0,
|
||||
P_TTP => r.ttp = prl.operand.first().copied().unwrap_or(0) != 0,
|
||||
P_ITAP => r.depth = i32_of(&prl).unwrap_or(0).max(0) as u32,
|
||||
T_DEF_TABLE => r.edges = def_table_edges(prl.operand),
|
||||
T_TABLE_HEADER => r.header = prl.operand.first().copied().unwrap_or(0) != 0,
|
||||
T_CANT_SPLIT | T_CANT_SPLIT_90 => r.cant_split = prl.operand.first().copied().unwrap_or(0) != 0,
|
||||
T_ROW_HEIGHT => {
|
||||
if let Some(v) = s16(&prl) {
|
||||
if v < 0 {
|
||||
r.height_rule = HeightRule::Exact;
|
||||
r.height = Some((-(v as f32) / 20.0).clamp(0.0, 1584.0));
|
||||
} else {
|
||||
r.height_rule = HeightRule::AtLeast;
|
||||
r.height = Some((v as f32 / 20.0).clamp(0.0, 1584.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
T_JC | T_JC_90 => {
|
||||
r.align = match u16_of(&prl).unwrap_or(0) {
|
||||
1 => Some(Align::Center),
|
||||
2 => Some(Align::Right),
|
||||
_ => Some(Align::Left),
|
||||
};
|
||||
}
|
||||
T_DXA_LEFT => dxa_left = s16(&prl),
|
||||
T_DXA_GAP_HALF => gap_half = s16(&prl),
|
||||
T_TABLE_BORDERS_80 => r.borders = table_borders(prl.operand),
|
||||
T_MERGE => {
|
||||
// Rows apply only the first MAX_COLS merges.
|
||||
if let [a, b, ..] = prl.operand
|
||||
&& b > a
|
||||
&& r.merges.len() < MAX_COLS
|
||||
{
|
||||
r.merges.push((*a, *b));
|
||||
}
|
||||
}
|
||||
T_VERT_MERGE => {
|
||||
// cb, itc, flag: 1 = continues the merge from above, 3 = restarts it.
|
||||
if let [_, itc, flag, ..] = prl.operand {
|
||||
let m = match flag {
|
||||
3 => VMerge::Restart,
|
||||
1 => VMerge::Continue,
|
||||
_ => VMerge::None,
|
||||
};
|
||||
// The first override for a cell wins.
|
||||
if m != VMerge::None && !r.vmerge.iter().any(|(k, _)| k == itc) {
|
||||
r.vmerge.push((*itc, m));
|
||||
}
|
||||
}
|
||||
}
|
||||
T_SET_SHD => {
|
||||
if let Some(rgb) = prl.operand.get(3..).and_then(shd_rgb)
|
||||
&& let [a, b, ..] = prl.operand.get(1..3).unwrap_or(&[])
|
||||
{
|
||||
for c in *a..(*b).min(MAX_COLS as u8) {
|
||||
set(&mut r.shading, c, Some(rgb));
|
||||
}
|
||||
}
|
||||
}
|
||||
T_DEF_TABLE_SHD_80 => {
|
||||
let cells = prl.operand.get(1..).unwrap_or(&[]);
|
||||
for (i, b) in cells.as_chunks::<2>().0.iter().take(MAX_COLS).enumerate() {
|
||||
if let Some(rgb) = shd80_rgb(b) {
|
||||
set(&mut r.shading, i as u8, Some(rgb));
|
||||
}
|
||||
}
|
||||
}
|
||||
T_DEF_TABLE_SHD | T_DEF_TABLE_SHD_RAW | T_DEF_TABLE_SHD_2ND | T_DEF_TABLE_SHD_RAW_2ND | T_DEF_TABLE_SHD_3RD | T_DEF_TABLE_SHD_RAW_3RD => {
|
||||
// Each array covers its own cell range (0-21, 22-43, 44-62); extra entries
|
||||
// are ignored so an over-long array can't override the next range.
|
||||
let (first, lim) = match prl.op {
|
||||
T_DEF_TABLE_SHD_2ND | T_DEF_TABLE_SHD_RAW_2ND => (22, 44),
|
||||
T_DEF_TABLE_SHD_3RD | T_DEF_TABLE_SHD_RAW_3RD => (44, MAX_COLS),
|
||||
_ => (0, 22),
|
||||
};
|
||||
let cells = prl.operand.get(1..).unwrap_or(&[]);
|
||||
let raw = matches!(prl.op, T_DEF_TABLE_SHD_RAW | T_DEF_TABLE_SHD_RAW_2ND | T_DEF_TABLE_SHD_RAW_3RD);
|
||||
for (i, b) in cells.as_chunks::<10>().0.iter().take(lim - first).enumerate() {
|
||||
// In the Raw arrays a nil Shd (ipat 0xFFFF) means "as the table style
|
||||
// shades it", so it leaves any earlier fill alone.
|
||||
if raw && b.get(8..10) == Some(&[0xFF, 0xFF]) {
|
||||
continue;
|
||||
}
|
||||
set(&mut r.shading, (first + i) as u8, shd_rgb(b));
|
||||
}
|
||||
}
|
||||
T_CELL_WIDTH => {
|
||||
// Operand: cb (1 byte), itcFirst (1 byte), itcLim (1 byte), ftsWidth (1 byte), wWidth (2 bytes).
|
||||
// [MS-DOC] §2.9.290 sprmTCellWidth (0xD635).
|
||||
// ftsWidth: 3 = twips (dxa), 2 = percentage (50ths of a percent).
|
||||
if let [_, a, b, fts, w0, w1, ..] = prl.operand {
|
||||
if *fts == 3 {
|
||||
let w = (i16::from_le_bytes([*w0, *w1]) as f32 / 20.0).clamp(0.0, 1584.0);
|
||||
for c in *a..(*b).min(MAX_COLS as u8) {
|
||||
set(&mut r.widths, c, w);
|
||||
}
|
||||
} else if *fts == 2 {
|
||||
let pct = (u16::from_le_bytes([*w0, *w1]) as f32 / 50.0).clamp(0.0, 100.0);
|
||||
for c in *a..(*b).min(MAX_COLS as u8) {
|
||||
set(&mut r.width_pcts, c, pct);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
T_VERT_ALIGN => {
|
||||
if let [_, a, b, va, ..] = prl.operand {
|
||||
let v = match va {
|
||||
1 => VAlign::Center,
|
||||
2 => VAlign::Bottom,
|
||||
_ => VAlign::Top,
|
||||
};
|
||||
for c in *a..(*b).min(MAX_COLS as u8) {
|
||||
set(&mut r.valign, c, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// The table indent is the row origin minus half the cell gap.
|
||||
if let Some(l) = dxa_left {
|
||||
let gap = gap_half.unwrap_or(0) as f32 / 20.0;
|
||||
r.indent = Some(((l as f32 / 20.0) - gap).clamp(-1584.0, 1584.0));
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/// A finished paragraph plus the bookkeeping the block assembly needs.
|
||||
pub(crate) struct ParaOut {
|
||||
pub(crate) para: Paragraph,
|
||||
/// CP just past the terminating mark.
|
||||
pub(crate) end_cp: u32,
|
||||
/// Table context from the mark's PAPX; `None` for a paragraph outside a table.
|
||||
pub(crate) row: Option<Box<RowInfo>>,
|
||||
}
|
||||
|
||||
/// Group consecutive in-table paragraphs into [`Table`] blocks; the rest pass through.
|
||||
/// Rows that never close (or tables nested deeper than we model) degrade to paragraphs so
|
||||
/// no text is lost.
|
||||
pub(crate) fn assemble(paras: Vec<ParaOut>) -> Blocks {
|
||||
let mut out: Blocks = Vec::new();
|
||||
let mut rows: Vec<Row> = Vec::new();
|
||||
let mut cells: Vec<Cell> = Vec::new();
|
||||
let mut cell: Blocks = Vec::new();
|
||||
let mut grid: Vec<f32> = Vec::new();
|
||||
let mut props = TableProps::default();
|
||||
let mut open = false;
|
||||
let mut depth_seen = 0u32;
|
||||
|
||||
fn flush_cell(cells: &mut Vec<Cell>, cell: &mut Blocks) {
|
||||
let mut blocks = std::mem::take(cell);
|
||||
if blocks.is_empty() {
|
||||
blocks.push(para_block(Paragraph::new()));
|
||||
}
|
||||
cells.push(Cell { props: CellProps { span: 1, ..Default::default() }, blocks, controls: Default::default() });
|
||||
}
|
||||
fn build_row(mut cells: Vec<Cell>, info: &RowInfo) -> Row {
|
||||
// Per-cell overrides first (sprm cell indices count pre-merge cells), then merges.
|
||||
for (i, c) in cells.iter_mut().enumerate() {
|
||||
let i = i as u8;
|
||||
if let Some((_, m)) = info.vmerge.iter().find(|(k, _)| *k == i) {
|
||||
c.props.vmerge = *m;
|
||||
}
|
||||
if let Some((_, w)) = info.widths.iter().rfind(|(k, _)| *k == i) {
|
||||
c.props.width = Some(*w);
|
||||
}
|
||||
if let Some((_, pct)) = info.width_pcts.iter().rfind(|(k, _)| *k == i) {
|
||||
c.props.width_pct = Some(*pct);
|
||||
}
|
||||
if let Some((_, s)) = info.shading.iter().rfind(|(k, _)| *k == i) {
|
||||
c.props.shading = *s;
|
||||
}
|
||||
if let Some((_, v)) = info.valign.iter().rfind(|(k, _)| *k == i) {
|
||||
c.props.valign = Some(*v);
|
||||
}
|
||||
}
|
||||
for &(a, b) in info.merges.iter().take(MAX_COLS) {
|
||||
let a = a as usize;
|
||||
let b = (b as usize).min(cells.len());
|
||||
if a < b && b <= cells.len() && b - a > 1 {
|
||||
let removed: Vec<Cell> = cells.drain(a + 1..b).collect();
|
||||
if let Some(first) = cells.get_mut(a) {
|
||||
first.props.span = ((b - a) as u32).min(63);
|
||||
for c in removed {
|
||||
first.blocks.extend(c.blocks.into_iter().filter(|x| x.as_para().is_none_or(|p| !p.is_empty())));
|
||||
}
|
||||
if first.blocks.len() > 1 && first.blocks.first().and_then(|x| x.as_para()).is_some_and(Paragraph::is_empty) {
|
||||
first.blocks.remove(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row {
|
||||
props: RowProps {
|
||||
height: info.height,
|
||||
height_rule: info.height_rule,
|
||||
header: info.header,
|
||||
cant_split: info.cant_split,
|
||||
..Default::default()
|
||||
},
|
||||
cells,
|
||||
controls: Default::default(),
|
||||
}
|
||||
}
|
||||
fn flush_table(out: &mut Blocks, rows: &mut Vec<Row>, cells: &mut Vec<Cell>, cell: &mut Blocks, grid: &[f32], props: &TableProps, nested: bool) {
|
||||
if !cells.is_empty() || !cell.is_empty() {
|
||||
flush_cell(cells, cell);
|
||||
// A stray trailing row (no TTP of its own): size its cells from the grid.
|
||||
let mut stray = build_row(std::mem::take(cells), &RowInfo::default());
|
||||
for (i, c) in stray.cells.iter_mut().enumerate() {
|
||||
if c.props.width.is_none() && c.props.span == 1 {
|
||||
c.props.width = grid.get(i).copied();
|
||||
}
|
||||
}
|
||||
rows.push(stray);
|
||||
}
|
||||
if rows.is_empty() {
|
||||
return;
|
||||
}
|
||||
if nested {
|
||||
// Unclosed or nested deeper than we model: keep the text, drop the table shape.
|
||||
for r in rows.iter() {
|
||||
for c in &r.cells {
|
||||
out.extend(c.blocks.iter().cloned());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out.push(Arc::new(Block::Table(Table {
|
||||
props: props.clone(),
|
||||
grid: grid.to_vec(),
|
||||
rows: std::mem::take(rows),
|
||||
controls: Default::default(),
|
||||
})));
|
||||
}
|
||||
rows.clear();
|
||||
}
|
||||
|
||||
for p in paras {
|
||||
let Some(info) = p.row else {
|
||||
if open {
|
||||
flush_table(&mut out, &mut rows, &mut cells, &mut cell, &grid, &props, depth_seen > 1);
|
||||
grid.clear();
|
||||
// Table props are per-table (first row carrying the sprm wins within the
|
||||
// table): reset the accumulator so one table's borders, align, indent and
|
||||
// cell margins never leak into the next (the repro's first table wrote
|
||||
// Word's default 108-twip gap, and every later table — including the
|
||||
// change history with its own 40-twip gap — inherited 5.4 pt margins).
|
||||
props = TableProps::default();
|
||||
open = false;
|
||||
depth_seen = 0;
|
||||
}
|
||||
out.push(para_block(p.para));
|
||||
continue;
|
||||
};
|
||||
if !open {
|
||||
open = true;
|
||||
}
|
||||
depth_seen = depth_seen.max(info.depth);
|
||||
if grid.is_empty() {
|
||||
// Column widths from the table's first `sprmTDefTable`.
|
||||
grid = info.edges.windows(2).take(MAX_COLS).map(|w| (w[1] - w[0]).max(0.0)).collect();
|
||||
}
|
||||
if info.borders.is_some() && props.borders.is_none() {
|
||||
props.borders = info.borders;
|
||||
}
|
||||
if props.align.is_none() {
|
||||
props.align = info.align;
|
||||
}
|
||||
if props.indent.is_none() {
|
||||
props.indent = info.indent;
|
||||
}
|
||||
if info.cell_mark {
|
||||
// A row-end (TTP) mark terminates the row. Word writes rows as N content
|
||||
// cells each ended by their own cell mark, followed by an empty row-end mark
|
||||
// that carries the row's TDefTable (the 3GPP change history: 8 content marks
|
||||
// + an empty TTP whose TDefTable defines itcMac = 8). When that mark ends no
|
||||
// content of its own and the TDefTable already has a column per flushed cell,
|
||||
// it is a pure row terminator, not the end of a phantom (N+1)th cell — such a
|
||||
// cell has no column (width None) and shifts every real cell's span one grid
|
||||
// column over. A TTP mark with content, or one short of the TDefTable's
|
||||
// columns, still ends a real cell (rows without a TDefTable keep today's
|
||||
// shape: the count is unknowable, so the mark always flushes).
|
||||
let row_end_only = info.ttp && p.para.text.is_empty() && cell.is_empty() && !info.edges.is_empty() && cells.len() + 1 >= info.edges.len();
|
||||
// ...but a row-end mark with no cells before it still makes a (one-cell) row.
|
||||
let row_end_only = row_end_only && !cells.is_empty();
|
||||
if !p.para.text.is_empty() {
|
||||
cell.push(para_block(p.para));
|
||||
}
|
||||
if !row_end_only {
|
||||
flush_cell(&mut cells, &mut cell);
|
||||
}
|
||||
if info.ttp {
|
||||
let row = build_row(std::mem::take(&mut cells), &info);
|
||||
rows.push(row);
|
||||
}
|
||||
} else {
|
||||
cell.push(para_block(p.para));
|
||||
}
|
||||
}
|
||||
if open {
|
||||
flush_table(&mut out, &mut rows, &mut cells, &mut cell, &grid, &props, depth_seen > 1);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Cell-edge positions (points) from a `sprmTDefTable` operand: cb u16, itcMac u8, then
|
||||
/// itcMac+1 twip boundaries followed by the TC80s (skipped). Edges are returned as stored:
|
||||
/// per-row, in operand order; sorting and capping happen when the table grid is assembled.
|
||||
fn def_table_edges(operand: &[u8]) -> Vec<f32> {
|
||||
let itc = operand.get(2).copied().unwrap_or(0) as usize;
|
||||
if itc == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
// itcMac is a u8, so this allocation stays tiny; the loop stops at the operand's end.
|
||||
let mut edges = Vec::with_capacity((itc + 1).min(256));
|
||||
for i in 0..=itc {
|
||||
match operand.get(3 + i * 2..3 + i * 2 + 2) {
|
||||
Some(b) => edges.push(i16::from_le_bytes([b[0], b[1]]) as f32 / 20.0),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
edges
|
||||
}
|
||||
|
||||
/// Table borders from `sprmTTableBorders80`: cb (0x18) then six Brc80s in the order top,
|
||||
/// left, bottom, right, inside-horizontal, inside-vertical, read as paragraph borders are.
|
||||
fn table_borders(operand: &[u8]) -> Option<Borders> {
|
||||
let b = |i: usize| operand.get(1 + i * 4..1 + i * 4 + 4).and_then(fmt::brc80);
|
||||
Some(Borders { top: b(0), left: b(1), bottom: b(2), right: b(3), between: b(4), inside_v: b(5) })
|
||||
}
|
||||
|
||||
/// Background colour from a 10-byte `Shd`: cvFore, cvBack (COLORREFs), ipat. Solid patterns
|
||||
/// show the foreground; clear (0) and the other patterns show the background (Word writes a
|
||||
/// plain cell fill as clear with cvBack); nil (0xFFFF) and automatic colours are no fill.
|
||||
fn shd_rgb(b: &[u8]) -> Option<Rgb> {
|
||||
let cv = |b: &[u8]| -> Option<Rgb> {
|
||||
match b {
|
||||
[_, _, _, 0xFF] => None, // fAuto
|
||||
[r, g, bl, _] => Some(Rgb(*r, *g, *bl)),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
let ipat = b.get(8..10).map(|x| u16::from_le_bytes([x[0], x[1]]))?;
|
||||
match ipat {
|
||||
0xFFFF => None,
|
||||
1 => cv(b.get(0..4)?),
|
||||
_ => cv(b.get(4..8)?),
|
||||
}
|
||||
}
|
||||
|
||||
/// Background colour from a 2-byte `Shd80` (icoFore, icoBack, ipat bit-fields). As in
|
||||
/// [`shd_rgb`], solid shows the foreground and clear (0) the background, which is how Word
|
||||
/// 97 writes a plain fill; ico 0 is automatic (no fill).
|
||||
fn shd80_rgb(b: &[u8]) -> Option<Rgb> {
|
||||
let v = u16::from_le_bytes([*b.first()?, *b.get(1)?]);
|
||||
let fore = v & 0x1F;
|
||||
let back = (v >> 5) & 0x1F;
|
||||
let ipat = v >> 10;
|
||||
if fore == 0x1F && back == 0x1F {
|
||||
return None;
|
||||
}
|
||||
fmt::ico(if ipat == 1 { fore as u8 } else { back as u8 })
|
||||
}
|
||||
|
||||
fn u16_of(p: &Prl) -> Option<u16> {
|
||||
match p.operand {
|
||||
[b0, b1, ..] => Some(u16::from_le_bytes([*b0, *b1])),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn s16(p: &Prl) -> Option<i16> {
|
||||
u16_of(p).map(|v| v as i16)
|
||||
}
|
||||
|
||||
fn i32_of(p: &Prl) -> Option<i32> {
|
||||
match p.operand {
|
||||
[b0, b1, b2, b3, ..] => Some(i32::from_le_bytes([*b0, *b1, *b2, *b3])),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
+20
-2732
File diff suppressed because it is too large
Load Diff
@@ -1,618 +0,0 @@
|
||||
//! Pictures and drawings: OfficeArt records ([MS-ODRAW]) hand-built for these tests from the
|
||||
//! specification, with tiny pictures generated in code. No file produced by Word is used.
|
||||
|
||||
use proptest::prelude::*;
|
||||
use wordcraft_doc::fill::Fill;
|
||||
use wordcraft_doc::para::{Anchor, InlineObject, ShapeKind, Wrap};
|
||||
use wordcraft_doc::props::Rgb;
|
||||
|
||||
use super::{StructSpec, grpprl, plc, read, struct_doc, structured_spec};
|
||||
use crate::officeart;
|
||||
|
||||
/// One OfficeArt record: header (recVer, recInstance, recType, recLen) + body.
|
||||
fn rec(ver: u16, inst: u16, typ: u16, body: &[u8]) -> Vec<u8> {
|
||||
let vi = (inst << 4) | (ver & 0xF);
|
||||
[vi.to_le_bytes().as_slice(), &typ.to_le_bytes(), &(body.len() as u32).to_le_bytes(), body].concat()
|
||||
}
|
||||
|
||||
fn container(typ: u16, kids: &[Vec<u8>]) -> Vec<u8> {
|
||||
rec(0xF, 0, typ, &kids.concat())
|
||||
}
|
||||
|
||||
/// `OfficeArtFSP`: shape type, spid, flags.
|
||||
fn fsp(spt: u16, spid: u32, flags: u32) -> Vec<u8> {
|
||||
rec(2, spt, 0xF00A, &[spid.to_le_bytes(), flags.to_le_bytes()].concat())
|
||||
}
|
||||
|
||||
/// `OfficeArtFOPT` with simple properties and complex ones (data appended in order).
|
||||
fn fopt(simple: &[(u16, u32)], complex: &[(u16, Vec<u8>)]) -> Vec<u8> {
|
||||
let mut body = Vec::new();
|
||||
for (pid, op) in simple {
|
||||
body.extend_from_slice(&pid.to_le_bytes());
|
||||
body.extend_from_slice(&op.to_le_bytes());
|
||||
}
|
||||
for (pid, data) in complex {
|
||||
body.extend_from_slice(&(pid | 0x8000).to_le_bytes());
|
||||
body.extend_from_slice(&(data.len() as u32).to_le_bytes());
|
||||
}
|
||||
for (_, data) in complex {
|
||||
body.extend_from_slice(data);
|
||||
}
|
||||
rec(3, (simple.len() + complex.len()) as u16, 0xF00B, &body)
|
||||
}
|
||||
|
||||
/// A shape container with the given records.
|
||||
fn sp(kids: &[Vec<u8>]) -> Vec<u8> {
|
||||
container(0xF004, kids)
|
||||
}
|
||||
|
||||
/// A real 2×2 PNG.
|
||||
fn tiny_png() -> Vec<u8> {
|
||||
let img = image::RgbaImage::from_pixel(2, 2, image::Rgba([10, 20, 30, 255]));
|
||||
let mut out = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
/// `OfficeArtBlipPNG` (one UID, tag) and the `OfficeArtFBSE` embedding it.
|
||||
fn fbse_png(png: &[u8]) -> Vec<u8> {
|
||||
let blip = rec(0, 0x6E0, 0xF01E, &[[0u8; 16].as_slice(), &[0xFF], png].concat());
|
||||
let mut body = vec![6u8, 6];
|
||||
body.extend_from_slice(&[0u8; 16]); // rgbUid
|
||||
body.extend_from_slice(&0u16.to_le_bytes()); // tag
|
||||
body.extend_from_slice(&(blip.len() as u32).to_le_bytes()); // size
|
||||
body.extend_from_slice(&1u32.to_le_bytes()); // cRef
|
||||
body.extend_from_slice(&0u32.to_le_bytes()); // foDelay
|
||||
body.extend_from_slice(&[0, 0, 0, 0]); // unused1, cbName, unused2, unused3
|
||||
body.extend_from_slice(&blip);
|
||||
rec(2, 6, 0xF007, &body)
|
||||
}
|
||||
|
||||
/// UTF-16LE, NUL-terminated (a complex string property).
|
||||
fn wz(s: &str) -> Vec<u8> {
|
||||
s.encode_utf16().chain([0]).flat_map(u16::to_le_bytes).collect()
|
||||
}
|
||||
|
||||
/// A `Spa` (26 bytes): spid, bounds in twips, flags, cTxbx.
|
||||
fn spa(spid: u32, [l, t, r, b]: [i32; 4], flags: u16) -> Vec<u8> {
|
||||
[spid.to_le_bytes().as_slice(), &l.to_le_bytes(), &t.to_le_bytes(), &r.to_le_bytes(), &b.to_le_bytes(), &flags.to_le_bytes(), &0u32.to_le_bytes()]
|
||||
.concat()
|
||||
}
|
||||
|
||||
/// `Spa` flags from bx, by, wr and fBelowText.
|
||||
fn spa_flags(bx: u16, by: u16, wr: u16, below: bool) -> u16 {
|
||||
(bx << 1) | (by << 3) | (wr << 5) | (u16::from(below) << 14)
|
||||
}
|
||||
|
||||
/// `OfficeArtContent`: a DggContainer holding a BLIP store, then one main-document drawing.
|
||||
fn art_content(fbses: &[Vec<u8>], shapes: &[Vec<u8>]) -> Vec<u8> {
|
||||
let bstore = rec(0xF, fbses.len() as u16, 0xF001, &fbses.concat());
|
||||
let dgg = container(0xF000, &[bstore]);
|
||||
let patriarch = sp(&[fsp(0, 1024, 0b101)]);
|
||||
let spgr = container(0xF003, &[[patriarch].as_slice(), shapes].concat());
|
||||
let dg = container(0xF002, &[rec(0, 1, 0xF008, &[0u8; 8]), spgr]);
|
||||
[dgg, vec![0u8], dg].concat()
|
||||
}
|
||||
|
||||
/// A PICF for an inline OfficeArt picture: `MM_SHAPE`, 1 × 0.5 inch goal size at 100 %.
|
||||
fn picf_shape(rest: &[u8]) -> Vec<u8> {
|
||||
let mut picf = vec![0u8; 0x44];
|
||||
picf[0..4].copy_from_slice(&((0x44 + rest.len()) as u32).to_le_bytes());
|
||||
picf[4..6].copy_from_slice(&0x44u16.to_le_bytes());
|
||||
picf[6..8].copy_from_slice(&0x0064u16.to_le_bytes()); // mfpf.mm = MM_SHAPE
|
||||
picf[28..30].copy_from_slice(&1440i16.to_le_bytes());
|
||||
picf[30..32].copy_from_slice(&720i16.to_le_bytes());
|
||||
picf[32..34].copy_from_slice(&1000u16.to_le_bytes());
|
||||
picf[34..36].copy_from_slice(&1000u16.to_le_bytes());
|
||||
[picf.as_slice(), rest].concat()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_officeart_picture_with_crop_and_alt_text() {
|
||||
let png = tiny_png();
|
||||
// A picture frame whose pib names the first FBSE that follows it, a quarter cropped
|
||||
// off the left, with alt text.
|
||||
let shape = sp(&[fsp(75, 1025, 0xA00), fopt(&[(0x0102, 0x4000), (0x0104, 1)], &[(0x0381, wz("Logo"))])]);
|
||||
let data = picf_shape(&[shape, fbse_png(&png)].concat());
|
||||
let text = "Pic\u{1}!\r";
|
||||
let ccp = text.chars().count() as u32;
|
||||
let spec = StructSpec { text, data_stream: data, chpx_runs: vec![(0, grpprl(&[(0x6A03, &0u32.to_le_bytes())]))], ..structured_spec(text, ccp) };
|
||||
let doc = read(&struct_doc(&spec)).expect("opens");
|
||||
let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para");
|
||||
assert_eq!(p0.text, "Pic\u{FFFC}!");
|
||||
let Some(InlineObject::Image { media, w, h, alt, crop, float, .. }) = p0.objects.first() else { panic!("no image: {:?}", p0.objects) };
|
||||
assert_eq!((*w, *h), (54.0, 36.0)); // 72 pt less a quarter, × 36 pt
|
||||
assert_eq!(*crop, [0.25, 0.0, 0.0, 0.0]);
|
||||
assert_eq!(alt, "Logo");
|
||||
assert_eq!(float.wrap, Wrap::Inline);
|
||||
assert_eq!(doc.media.get(media).map(|b| b.as_slice()), Some(png.as_slice()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn floating_picture_from_the_blip_store_keeps_position_and_wrap() {
|
||||
let png = tiny_png();
|
||||
let pic = sp(&[fsp(75, 1025, 0xA00), fopt(&[(0x0104, 1)], &[])]);
|
||||
let art = art_content(&[fbse_png(&png)], &[pic]);
|
||||
// Two inches by one, 1" from the page's left edge and 0.5" below the top margin; square wrap.
|
||||
let spas = plc(&[1, 3], &spa(1025, [1440, 720, 4320, 2160], spa_flags(1, 0, 2, false)));
|
||||
let text = "A\u{8}B\r";
|
||||
let ccp = text.chars().count() as u32;
|
||||
let spec = StructSpec { text, extra_pairs: vec![(40, spas), (50, art)], ..structured_spec(text, ccp) };
|
||||
let doc = read(&struct_doc(&spec)).expect("opens");
|
||||
let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para");
|
||||
assert_eq!(p0.text, "A\u{FFFC}B");
|
||||
let Some(InlineObject::Image { media, w, h, float, .. }) = p0.objects.first() else { panic!("no image: {:?}", p0.objects) };
|
||||
assert_eq!((*w, *h), (144.0, 72.0));
|
||||
assert_eq!((float.wrap, float.h_rel, float.v_rel, float.x, float.y), (Wrap::Square, Anchor::Page, Anchor::Margin, 72.0, 36.0));
|
||||
assert_eq!(doc.media.get(media).map(|b| b.as_slice()), Some(png.as_slice()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shapes_keep_kind_colours_wrap_and_text_box_text() {
|
||||
// A red ellipse with a 2 pt blue outline behind the text, and an unfilled text box.
|
||||
let ellipse = sp(&[fsp(3, 1026, 0xA00), fopt(&[(0x0181, 0x0000_00FF), (0x01C0, 0x00FF_0000), (0x01CB, 25_400)], &[])]);
|
||||
let tb = sp(&[fsp(202, 1027, 0xA00), fopt(&[(0x01BF, 1 << 20)], &[]), rec(0, 0, 0xF00D, &0x0001_0000u32.to_le_bytes())]);
|
||||
let art = art_content(&[], &[ellipse, tb]);
|
||||
let spas =
|
||||
plc(&[1, 2, 4], &[spa(1026, [0, 0, 1440, 1440], spa_flags(2, 2, 3, true)), spa(1027, [0, 0, 2880, 720], spa_flags(0, 0, 1, false))].concat());
|
||||
// The text box story: an FTXBXS whose lid is the shape's spid (cTxbx, cReusable, fReusable,
|
||||
// reserved, lid, txidUndo).
|
||||
let ftxbxs =
|
||||
[1i32.to_le_bytes().as_slice(), &0i32.to_le_bytes(), &0i16.to_le_bytes(), &0i32.to_le_bytes(), &1027u32.to_le_bytes(), &0i32.to_le_bytes()]
|
||||
.concat();
|
||||
let txbx = plc(&[0, 7], &ftxbxs);
|
||||
let text = "A\u{8}\u{8}B\r";
|
||||
let ccp = text.chars().count() as u32;
|
||||
// The text box story is stored as the subdocument right after the main text; the endnote
|
||||
// slot of the fixture writes it there and the FIB is then patched to call it the text box
|
||||
// subdocument (ccpEdn = 0, ccpTxbx = 7).
|
||||
let spec =
|
||||
StructSpec { text, endnote_stories: vec!["Inside\r"], extra_pairs: vec![(40, spas), (50, art), (56, txbx)], ..structured_spec(text, ccp) };
|
||||
let file = patch_word(&struct_doc(&spec), &[(0x60, 0), (0x64, 7)]);
|
||||
let doc = read(&file).expect("opens");
|
||||
let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para");
|
||||
assert_eq!(p0.objects.len(), 2, "{:?}", p0.objects);
|
||||
let Some(InlineObject::Shape { kind, w, h, fill, stroke, stroke_width, float, .. }) = p0.objects.first() else { panic!("{:?}", p0.objects) };
|
||||
assert_eq!((*kind, *w, *h), (ShapeKind::Ellipse, 72.0, 72.0));
|
||||
assert_eq!((fill.clone(), *stroke, *stroke_width), (Some(Fill::solid(Rgb(255, 0, 0))), Some(Rgb(0, 0, 255)), 2.0));
|
||||
assert_eq!((float.wrap, float.h_rel, float.v_rel), (Wrap::BehindText, Anchor::Column, Anchor::Paragraph));
|
||||
let Some(InlineObject::Shape { kind, fill, float, story, .. }) = p0.objects.get(1) else { panic!("{:?}", p0.objects) };
|
||||
assert_eq!((*kind, fill.clone(), float.wrap), (ShapeKind::TextBox, None, Wrap::TopAndBottom));
|
||||
let part = story.and_then(|id| doc.parts.get(&id)).expect("text box story part");
|
||||
assert_eq!(part.kind, wordcraft_doc::PartKind::TextBox);
|
||||
assert_eq!(part.blocks.first().and_then(|b| b.as_para()).map(|p| p.text.as_str()), Some("Inside"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_boxes_pointing_at_the_same_story_text_read_it_once() {
|
||||
// Two text boxes whose PlcftxbxTxt stories are both [0, 7) (the table steps back to CP 0
|
||||
// in between): each was read into its own part, so a hostile table could copy one story
|
||||
// thousands of times. The second story is dropped and its box stays empty.
|
||||
let tb = |spid: u32| sp(&[fsp(202, spid, 0xA00), fopt(&[(0x01BF, 1 << 20)], &[]), rec(0, 0, 0xF00D, &0x0001_0000u32.to_le_bytes())]);
|
||||
let art = art_content(&[], &[tb(1027), tb(1028)]);
|
||||
let flags = spa_flags(0, 0, 1, false);
|
||||
let spas = plc(&[1, 2, 4], &[spa(1027, [0, 0, 2880, 720], flags), spa(1028, [0, 0, 2880, 720], flags)].concat());
|
||||
// FTXBXS: cTxbx, cReusable, fReusable, reserved, lid, txidUndo.
|
||||
let ftxbxs = |lid: u32| {
|
||||
[1i32.to_le_bytes().as_slice(), &0i32.to_le_bytes(), &0i16.to_le_bytes(), &0i32.to_le_bytes(), &lid.to_le_bytes(), &0i32.to_le_bytes()]
|
||||
.concat()
|
||||
};
|
||||
let txbx = plc(&[0, 7, 0, 7], &[ftxbxs(1027), ftxbxs(1), ftxbxs(1028)].concat());
|
||||
let text = "A\u{8}\u{8}B\r";
|
||||
let ccp = text.chars().count() as u32;
|
||||
// As above: the endnote slot holds the text box subdocument, then the FIB is patched.
|
||||
let spec =
|
||||
StructSpec { text, endnote_stories: vec!["Inside\r"], extra_pairs: vec![(40, spas), (50, art), (56, txbx)], ..structured_spec(text, ccp) };
|
||||
let doc = read(&patch_word(&struct_doc(&spec), &[(0x60, 0), (0x64, 7)])).expect("opens");
|
||||
let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para");
|
||||
let stories: Vec<Option<u32>> =
|
||||
p0.objects.iter().filter_map(|o| if let InlineObject::Shape { story, .. } = o { Some(*story) } else { None }).collect();
|
||||
let [Some(first), None] = stories[..] else { panic!("stories: {stories:?}") };
|
||||
let part = doc.parts.get(&first).expect("text box story part");
|
||||
assert_eq!(part.blocks.first().and_then(|b| b.as_para()).map(|p| p.text.as_str()), Some("Inside"));
|
||||
assert_eq!(doc.parts.len(), 1);
|
||||
}
|
||||
|
||||
/// Overwrite little-endian u32s in the WordDocument stream of a compound file.
|
||||
fn patch_word(file: &[u8], at: &[(usize, u32)]) -> Vec<u8> {
|
||||
use std::io::Read;
|
||||
let mut comp = cfb::CompoundFile::open(std::io::Cursor::new(file.to_vec())).unwrap();
|
||||
let mut streams = Vec::new();
|
||||
for name in ["WordDocument", "0Table", "Data"] {
|
||||
if let Ok(mut s) = comp.open_stream(format!("/{name}")) {
|
||||
let mut v = Vec::new();
|
||||
s.read_to_end(&mut v).unwrap();
|
||||
if name == "WordDocument" {
|
||||
for (o, x) in at {
|
||||
v[*o..*o + 4].copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
}
|
||||
streams.push((name, v));
|
||||
}
|
||||
}
|
||||
let refs: Vec<(&str, &[u8])> = streams.iter().map(|(n, v)| (*n, v.as_slice())).collect();
|
||||
super::cfb_file(&refs)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dib_becomes_png_and_compressed_metafiles_inflate() {
|
||||
// A 2×1 24-bit DIB (BITMAPINFOHEADER, rows padded to 4 bytes).
|
||||
let mut dib = Vec::new();
|
||||
for v in [40u32, 2, 1] {
|
||||
dib.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
dib.extend_from_slice(&1u16.to_le_bytes());
|
||||
dib.extend_from_slice(&24u16.to_le_bytes());
|
||||
dib.extend_from_slice(&[0u8; 24]);
|
||||
dib.extend_from_slice(&[0, 0, 255, 0, 255, 0, 0, 0]);
|
||||
let r = rec(0, 0x7A8, 0xF01F, &[[0u8; 16].as_slice(), &[0xFF], &dib].concat());
|
||||
let (dib_rec, _) = officeart::record(&r).unwrap();
|
||||
let blip = officeart::blip(&dib_rec, 1 << 20, &mut officeart::Decode::new(usize::MAX)).expect("DIB decodes");
|
||||
assert_eq!(blip.ext, "png");
|
||||
let img = image::load_from_memory(&blip.bytes).unwrap().to_rgb8();
|
||||
assert_eq!((img.width(), img.height(), img.get_pixel(0, 0).0), (2, 1, [255, 0, 0]));
|
||||
|
||||
// A deflated WMF stays a WMF (the metafile renderer draws it).
|
||||
let wmf: Vec<u8> = [1u16, 9, 0x300].iter().flat_map(|v| v.to_le_bytes()).chain([0u8; 12]).collect();
|
||||
let packed = miniz_oxide::deflate::compress_to_vec_zlib(&wmf, 6);
|
||||
let mut hdr = Vec::new();
|
||||
hdr.extend_from_slice(&(wmf.len() as u32).to_le_bytes());
|
||||
hdr.extend_from_slice(&[0u8; 24]); // rcBounds, ptSize
|
||||
hdr.extend_from_slice(&(packed.len() as u32).to_le_bytes());
|
||||
hdr.extend_from_slice(&[0x00, 0xFE]); // compression = deflate, filter
|
||||
let r = rec(0, 0x216, 0xF01B, &[[0u8; 16].as_slice(), &hdr, &packed].concat());
|
||||
let (wmf_rec, _) = officeart::record(&r).unwrap();
|
||||
let blip = officeart::blip(&wmf_rec, 1 << 20, &mut officeart::Decode::new(usize::MAX)).expect("WMF inflates");
|
||||
assert_eq!((blip.ext, blip.bytes), ("wmf", wmf));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deeply_nested_groups_and_lying_lengths_are_bounded() {
|
||||
// 2000 nested group containers: the walk stops at its depth cap (no stack overflow).
|
||||
let mut g = sp(&[fsp(1, 2000, 0)]);
|
||||
for _ in 0..2_000 {
|
||||
g = container(0xF003, &[sp(&[fsp(0, 2001, 1)]), g]);
|
||||
}
|
||||
let _ = officeart::content(&art_content(&[], &[g]));
|
||||
// A metafile claiming 4 GB uncompressed with garbage data, and an FBSE pointing far out.
|
||||
let mut hdr = vec![0xFFu8; 4];
|
||||
hdr.extend_from_slice(&[0u8; 24]);
|
||||
hdr.extend_from_slice(&u32::MAX.to_le_bytes());
|
||||
hdr.extend_from_slice(&[0, 0]);
|
||||
let r = rec(0, 0x3D4, 0xF01A, &[[0u8; 16].as_slice(), &hdr, &[0x78, 0x9C, 1, 2, 3]].concat());
|
||||
let (emf, _) = officeart::record(&r).unwrap();
|
||||
assert!(officeart::blip(&emf, 1 << 20, &mut officeart::Decode::new(usize::MAX)).is_none());
|
||||
let mut fbse = vec![0u8; 36];
|
||||
fbse[28..32].copy_from_slice(&u32::MAX.to_le_bytes());
|
||||
assert!(officeart::fbse_blip(&fbse, &[0u8; 64], 1 << 20, &mut officeart::Decode::new(usize::MAX)).is_none());
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(128))]
|
||||
|
||||
#[test]
|
||||
fn junk_drawings_never_panic(art in proptest::collection::vec(any::<u8>(), 0..2048), spas in proptest::collection::vec(any::<u8>(), 0..512), data in proptest::collection::vec(any::<u8>(), 0..1024)) {
|
||||
let text = "A\u{8}\u{1}\u{8}B\r";
|
||||
let ccp = text.chars().count() as u32;
|
||||
let picf = picf_shape(&data);
|
||||
let spec = StructSpec {
|
||||
text,
|
||||
data_stream: picf,
|
||||
chpx_runs: vec![(0, grpprl(&[(0x6A03, &0u32.to_le_bytes())]))],
|
||||
extra_pairs: vec![(40, spas), (50, art.clone()), (56, art)],
|
||||
..structured_spec(text, ccp)
|
||||
};
|
||||
let _ = read(&struct_doc(&spec));
|
||||
}
|
||||
}
|
||||
|
||||
// A 1×1 24-bit DIB, including its padded four-byte row.
|
||||
fn one_pixel_dib() -> Vec<u8> {
|
||||
let mut dib = [40u32, 1, 1].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<_>>();
|
||||
dib.extend_from_slice(&1u16.to_le_bytes());
|
||||
dib.extend_from_slice(&24u16.to_le_bytes());
|
||||
dib.extend_from_slice(&[0u8; 24]);
|
||||
dib.extend_from_slice(&[30, 60, 90, 0]);
|
||||
dib
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dib_conversion_caps_header_and_encoded_bytes() {
|
||||
let dib = one_pixel_dib();
|
||||
let record = rec(0, 0x7A8, officeart::rt::BLIP_DIB, &[[0u8; 16].as_slice(), &[0xFF], &dib].concat());
|
||||
let (record, _) = officeart::record(&record).unwrap();
|
||||
let bmp_size = 14 + dib.len();
|
||||
assert!(officeart::blip(&record, bmp_size - 1, &mut officeart::Decode::new(usize::MAX)).is_none(), "the BMP header also needs room");
|
||||
let fallback = officeart::blip(&record, bmp_size, &mut officeart::Decode::new(usize::MAX)).expect("the BMP fallback fits exactly");
|
||||
assert_eq!((fallback.ext, fallback.placeholder, fallback.bytes.len()), ("bmp", true, bmp_size));
|
||||
let img = image::load_from_memory(&fallback.bytes).unwrap().to_rgb8();
|
||||
assert_eq!((img.width(), img.height(), img.get_pixel(0, 0).0), (1, 1, [90, 60, 30]));
|
||||
let normal = officeart::blip(&record, 512, &mut officeart::Decode::new(usize::MAX)).expect("normal conversion fits");
|
||||
assert_eq!((normal.ext, normal.placeholder), ("png", false));
|
||||
assert!(normal.bytes.len() > bmp_size && normal.bytes.len() <= 512);
|
||||
assert_eq!(image::load_from_memory(&normal.bytes).unwrap().width(), 1);
|
||||
}
|
||||
|
||||
/// #3709: every anchor of one inline picture got its own copy of the alt text (up to 4096
|
||||
/// UTF-16 units). Copies are charged to the document's work budget; past it alt is empty.
|
||||
#[test]
|
||||
fn alt_text_copies_across_many_anchors_are_bounded() {
|
||||
let alt: String = "€".repeat(4096); // 12 KB as UTF-8
|
||||
let shape = sp(&[fsp(75, 1025, 0xA00), fopt(&[(0x0104, 1)], &[(0x0381, wz(&alt))])]);
|
||||
let data = picf_shape(&[shape, fbse_png(&tiny_png())].concat());
|
||||
let text: &'static str = Box::leak(format!("{}\r", "\u{1}".repeat(8000)).into_boxed_str());
|
||||
let ccp = text.chars().count() as u32;
|
||||
let spec = StructSpec { text, data_stream: data, chpx_runs: vec![(0, grpprl(&[(0x6A03, &0u32.to_le_bytes())]))], ..structured_spec(text, ccp) };
|
||||
let doc = read(&struct_doc(&spec)).expect("opens");
|
||||
let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para");
|
||||
let alts: Vec<&str> = p0.objects.iter().filter_map(|o| if let InlineObject::Image { alt, .. } = o { Some(alt.as_str()) } else { None }).collect();
|
||||
assert_eq!(alts.len(), 8000, "every anchor keeps its picture");
|
||||
assert_eq!(alts.first().copied(), Some(alt.as_str()));
|
||||
let total: usize = alts.iter().map(|a| a.len()).sum();
|
||||
assert!(total <= 64 << 20, "alt bytes {total}");
|
||||
assert_eq!(alts.last().copied(), Some(""));
|
||||
// Leaving it out is logged, once (#3871 review).
|
||||
assert_eq!(crate::LAST_DROPPED.with(|d| d.borrow().clone()), [crate::Cost::AltText]);
|
||||
}
|
||||
|
||||
/// #3710: every PlcSpa entry naming a group built another copy of all its members. A shape is
|
||||
/// anchored once; repeated spids are skipped.
|
||||
#[test]
|
||||
fn a_group_named_by_many_anchors_is_built_once() {
|
||||
let rect = |l: i32| rec(0, 0, 0xF00F, &[l, 0, l + 10, 10].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<_>>());
|
||||
let head = sp(&[fsp(0, 3000, 0x201), rec(1, 0, 0xF009, &[0i32, 0, 10_000, 10].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<_>>())]);
|
||||
let members: Vec<Vec<u8>> = (0..1000).map(|i| sp(&[fsp(1, 4000 + i as u32, 0xA02), rect(i * 10)])).collect();
|
||||
let group = container(0xF003, &[vec![head], members].concat());
|
||||
let art = art_content(&[], &[group]);
|
||||
let n = 5000u32;
|
||||
let cps: Vec<u32> = (1..=n + 1).collect();
|
||||
let spas: Vec<u8> = (0..n).flat_map(|_| spa(3000, [0, 0, 1440, 1440], spa_flags(0, 0, 3, false))).collect();
|
||||
let text = "A\u{8}\u{8}\u{8}B\r";
|
||||
let ccp = text.chars().count() as u32;
|
||||
let spec = StructSpec { text, extra_pairs: vec![(40, plc(&cps, &spas)), (50, art)], ..structured_spec(text, ccp) };
|
||||
let t = std::time::Instant::now();
|
||||
let doc = read(&struct_doc(&spec)).expect("opens");
|
||||
// About 10 ms on a loaded machine, so 10 s is a backstop; the old code (a copy per entry,
|
||||
// 6-8 s here) is caught by the group count below, one group per anchor ([1000; 3]).
|
||||
assert!(t.elapsed().as_secs() < 10, "opened in {:?}", t.elapsed());
|
||||
let p0 = doc.body.first().and_then(|b| b.as_para()).expect("para");
|
||||
let groups: Vec<usize> =
|
||||
p0.objects.iter().filter_map(|o| if let InlineObject::Group { children, .. } = o { Some(children.len()) } else { None }).collect();
|
||||
assert_eq!(groups, [1000]);
|
||||
}
|
||||
|
||||
/// #3710: a floating picture's copy is charged its alt text too, and a group every member's.
|
||||
#[test]
|
||||
fn a_floating_copy_is_charged_its_alt_text() {
|
||||
let image = |alt: String| InlineObject::Image {
|
||||
media: String::new(),
|
||||
w: 1.0,
|
||||
h: 1.0,
|
||||
alt,
|
||||
float: Default::default(),
|
||||
crop: Default::default(),
|
||||
fx: Default::default(),
|
||||
ole: None,
|
||||
};
|
||||
let bare = crate::object_cost(&image(String::new()));
|
||||
assert_eq!(crate::object_cost(&image("€".repeat(4096))), bare + 3 * 4096);
|
||||
let member = |obj| wordcraft_doc::para::GroupChild { x: 0.0, y: 0.0, obj };
|
||||
let group = InlineObject::Group {
|
||||
w: 1.0,
|
||||
h: 1.0,
|
||||
float: Default::default(),
|
||||
ch_w: 1.0,
|
||||
ch_h: 1.0,
|
||||
children: vec![member(image("x".repeat(100))), member(image(String::new()))],
|
||||
canvas: None,
|
||||
};
|
||||
assert_eq!(crate::object_cost(&group), 3 * bare + 100);
|
||||
}
|
||||
|
||||
/// A deflated WMF BLIP record declaring `size` uncompressed bytes.
|
||||
fn packed_wmf(size: u32, packed: &[u8]) -> Vec<u8> {
|
||||
let mut hdr = size.to_le_bytes().to_vec();
|
||||
hdr.extend_from_slice(&[0u8; 24]); // rcBounds, ptSize
|
||||
hdr.extend_from_slice(&(packed.len() as u32).to_le_bytes());
|
||||
hdr.extend_from_slice(&[0x00, 0xFE]); // compression = deflate, filter
|
||||
rec(0, 0x216, officeart::rt::BLIP_WMF, &[[0u8; 16].as_slice(), &hdr, packed].concat())
|
||||
}
|
||||
|
||||
/// An `OfficeArtFBSE` body whose BLIP (of `size` bytes) sits at `fo_delay` in the delay stream.
|
||||
fn delayed_fbse(size: usize, fo_delay: usize) -> Vec<u8> {
|
||||
let mut fbse = vec![0u8; 36];
|
||||
fbse[20..24].copy_from_slice(&(size as u32).to_le_bytes());
|
||||
fbse[28..32].copy_from_slice(&(fo_delay as u32).to_le_bytes());
|
||||
fbse
|
||||
}
|
||||
|
||||
/// #3711: store entries aliasing one delay-stream metafile each inflated (and copied) it again.
|
||||
/// Now the record is decoded once and every entry shares its media key.
|
||||
#[test]
|
||||
fn store_entries_aliasing_one_delayed_metafile_decode_it_once() {
|
||||
let wmf = vec![0u8; 1 << 20];
|
||||
let delay = packed_wmf(wmf.len() as u32, &miniz_oxide::deflate::compress_to_vec_zlib(&wmf, 6));
|
||||
let fbse = delayed_fbse(delay.len(), 0);
|
||||
let store: Vec<&[u8]> = vec![&fbse; 5_000];
|
||||
let mut pics = crate::media::Pictures::default();
|
||||
pics.work = officeart::Decode::new(8 << 20);
|
||||
let keys: Vec<_> = (1..=store.len() as u32).map(|pib| pics.bse(pib, &store, &delay)).collect();
|
||||
assert!(keys.iter().all(|k| k.is_some() && *k == keys[0]), "every entry shares one picture");
|
||||
assert_eq!(pics.work.left, (8 << 20) - wmf.len(), "inflated once");
|
||||
}
|
||||
|
||||
/// #3711: distinct metafiles that fail to inflate are charged their declared size anyway, so
|
||||
/// a document of them stops decoding once the work budget is spent.
|
||||
#[test]
|
||||
fn failing_metafile_inflations_spend_the_decode_budget() {
|
||||
let records: Vec<Vec<u8>> = (0..10u8).map(|i| packed_wmf(1 << 20, &[0x78, 0x9C, i, 1, 2, 3])).collect();
|
||||
let delay = records.concat();
|
||||
let mut at = 0;
|
||||
let fbses: Vec<Vec<u8>> = records
|
||||
.iter()
|
||||
.map(|r| {
|
||||
at += r.len();
|
||||
delayed_fbse(r.len(), at - r.len())
|
||||
})
|
||||
.collect();
|
||||
let store: Vec<&[u8]> = fbses.iter().map(Vec::as_slice).collect();
|
||||
let mut pics = crate::media::Pictures::default();
|
||||
pics.work = officeart::Decode::new(3 << 20);
|
||||
for pib in 1..=store.len() as u32 {
|
||||
assert!(pics.bse(pib, &store, &delay).is_none());
|
||||
}
|
||||
assert_eq!(pics.work.left, 0, "three attempts spent the budget; the rest weren't tried");
|
||||
}
|
||||
|
||||
/// #3712: a DIB's declared pixels are charged before decoding (an RLE bitmap is tiny on disk but
|
||||
/// decodes to w × h × 4 bytes); past the budget it stays a BMP placeholder, undecoded.
|
||||
#[test]
|
||||
fn dib_decoding_is_charged_its_declared_pixels() {
|
||||
let blip_of = |dib: &[u8]| rec(0, 0x7A8, officeart::rt::BLIP_DIB, &[[0u8; 16].as_slice(), &[0xFF], dib].concat());
|
||||
let one = blip_of(&one_pixel_dib());
|
||||
let (one, _) = officeart::record(&one).unwrap();
|
||||
let mut work = officeart::Decode::new(3);
|
||||
assert!(officeart::blip(&one, 512, &mut work).is_none(), "not even the copy fits: nothing is copied");
|
||||
assert_eq!(work.left, 3, "nothing decoded, nothing charged");
|
||||
let dib_len = one.body.len() - 17;
|
||||
let mut work = officeart::Decode::new(dib_len);
|
||||
assert_eq!(officeart::blip(&one, 512, &mut work).map(|b| (b.ext, b.placeholder)), Some(("png", false)));
|
||||
assert_eq!(work.left, 0, "the copy is charged, not just 4 bytes a pixel");
|
||||
|
||||
// 8-bit RLE declaring 16384 × 16384 (1 GiB decoded) in a few bytes.
|
||||
let mut big = [40u32, 16_384, 16_384].iter().flat_map(|v| v.to_le_bytes()).collect::<Vec<_>>();
|
||||
big.extend_from_slice(&1u16.to_le_bytes());
|
||||
big.extend_from_slice(&8u16.to_le_bytes());
|
||||
big.extend_from_slice(&1u32.to_le_bytes()); // BI_RLE8
|
||||
big.extend_from_slice(&[0u8; 12]);
|
||||
big.extend_from_slice(&1u32.to_le_bytes()); // one palette colour
|
||||
big.extend_from_slice(&[0u8; 4]); // clrImportant
|
||||
big.extend_from_slice(&[0u8; 4]); // the palette colour
|
||||
big.extend_from_slice(&[0, 0, 0, 1]); // end of line, end of bitmap
|
||||
let big = blip_of(&big);
|
||||
let (big, _) = officeart::record(&big).unwrap();
|
||||
let mut work = officeart::Decode::new(512 << 20);
|
||||
assert_eq!(officeart::blip(&big, 1 << 20, &mut work).map(|b| (b.ext, b.placeholder)), Some(("bmp", true)));
|
||||
assert_eq!(work.left, (512 << 20) - (big.body.len() - 17), "only the copy of the DIB was charged, not its declared pixels");
|
||||
}
|
||||
|
||||
/// An EMF `OfficeArtBlip` record (compressed) with `raw` as its stream, claiming `cb_size`.
|
||||
fn emf_blip(cb_size: u32, raw: &[u8]) -> Vec<u8> {
|
||||
let mut hdr = vec![0u8; 34];
|
||||
hdr[0..4].copy_from_slice(&cb_size.to_le_bytes());
|
||||
hdr[28..32].copy_from_slice(&(raw.len() as u32).to_le_bytes());
|
||||
hdr[32] = 0; // compressed
|
||||
hdr[33] = 0xFE;
|
||||
rec(0, 0x3D4, 0xF01A, &[[0u8; 16].as_slice(), &hdr, raw].concat())
|
||||
}
|
||||
|
||||
/// #3871 review: the zlib input is charged to the decode budget, not just what it inflates to.
|
||||
#[test]
|
||||
fn compressed_metafile_input_is_charged_to_the_decode_budget() {
|
||||
let raw = vec![0x5Au8; 1 << 20]; // not even zlib: costs its input all the same
|
||||
let r = emf_blip(1, &raw);
|
||||
let (emf, _) = officeart::record(&r).unwrap();
|
||||
let mut work = officeart::Decode::new(10 << 20);
|
||||
assert!(officeart::blip(&emf, 64 << 20, &mut work).is_none());
|
||||
assert_eq!(work.left, (10 << 20) - raw.len(), "1 MB of input charged, however little it claims to inflate to");
|
||||
// Ten more of them exhaust the budget; none is inflated past it.
|
||||
let mut work = officeart::Decode::new(3 << 20);
|
||||
for _ in 0..10 {
|
||||
let _ = officeart::blip(&emf, 64 << 20, &mut work);
|
||||
}
|
||||
assert_eq!(work.left, 0, "three attempts fit, the rest are refused");
|
||||
}
|
||||
|
||||
/// An inline `PICFAndOfficeArtData` (shape, FBSE) whose BLIP sits at `fo_delay` in Data; the
|
||||
/// FBSE claims `size` bytes.
|
||||
fn inline_pic(fo_delay: u32, size: u32) -> Vec<u8> {
|
||||
let mut fbse = vec![0u8; 36];
|
||||
fbse[20..24].copy_from_slice(&size.to_le_bytes());
|
||||
fbse[28..32].copy_from_slice(&fo_delay.to_le_bytes());
|
||||
let body = [container(0xF004, &[]), rec(2, 6, 0xF007, &fbse)].concat();
|
||||
let mut picf = vec![0u8; 0x44];
|
||||
picf[0..4].copy_from_slice(&((0x44 + body.len()) as u32).to_le_bytes());
|
||||
picf[4..6].copy_from_slice(&0x44u16.to_le_bytes());
|
||||
picf[6..8].copy_from_slice(&0x64u16.to_le_bytes()); // MM_SHAPE
|
||||
picf[28..30].copy_from_slice(&1440i16.to_le_bytes());
|
||||
picf[30..32].copy_from_slice(&1440i16.to_le_bytes());
|
||||
[picf, body].concat()
|
||||
}
|
||||
|
||||
/// #3871 review: inline pictures that alias one BLIP record share one decode, whatever sizes
|
||||
/// their FBSEs claim and wherever their PICFs sit.
|
||||
#[test]
|
||||
fn inline_pictures_aliasing_one_blip_record_are_decoded_once() {
|
||||
let blip = emf_blip(1, &vec![0x5Au8; 64 << 10]);
|
||||
let a = inline_pic(0, 70_000);
|
||||
let b = inline_pic(0, 80_000);
|
||||
let c = inline_pic(0, 90_000);
|
||||
let blip_at = (a.len() + b.len() + c.len()) as u32;
|
||||
let (a, b, c) = (inline_pic(blip_at, 70_000), inline_pic(blip_at, 80_000), inline_pic(blip_at, 90_000));
|
||||
let data = [a.clone(), b.clone(), c, blip].concat();
|
||||
let mut pics = crate::media::Pictures::default();
|
||||
let start = pics.work.left;
|
||||
let _ = pics.get(&data, 0);
|
||||
let _ = pics.get(&data, a.len() as u32);
|
||||
let _ = pics.get(&data, (a.len() + b.len()) as u32);
|
||||
assert_eq!(start - pics.work.left, 64 << 10, "one record, one charge");
|
||||
}
|
||||
|
||||
/// A 1-bit-per-pixel DIB of `side`×`side` pixels with `bytes` of pixel data.
|
||||
fn dib_1bpp(side: u32, bytes: usize) -> Vec<u8> {
|
||||
let mut d = vec![0u8; 40];
|
||||
d[0..4].copy_from_slice(&40u32.to_le_bytes());
|
||||
d[4..8].copy_from_slice(&side.to_le_bytes());
|
||||
d[8..12].copy_from_slice(&side.to_le_bytes());
|
||||
d[12..14].copy_from_slice(&1u16.to_le_bytes());
|
||||
d[14..16].copy_from_slice(&1u16.to_le_bytes());
|
||||
d.extend_from_slice(&[0, 0, 0, 0, 255, 255, 255, 0]); // palette
|
||||
d.extend(std::iter::repeat_n(0u8, bytes));
|
||||
d
|
||||
}
|
||||
|
||||
fn dib_record(dib: &[u8]) -> Vec<u8> {
|
||||
rec(0, 0x7A8, 0xF01F, &[[0u8; 16].as_slice(), &[0xFF], dib].concat())
|
||||
}
|
||||
|
||||
/// #3871 review: the decode budget scales with the input, so a document of many legitimate
|
||||
/// large scans decodes them all; a high-ratio set is still capped.
|
||||
#[test]
|
||||
fn the_decode_budget_scales_with_the_input_but_still_caps_bombs() {
|
||||
let floor = 1 << 20;
|
||||
// 20 legitimate 1024² 1-bit scans: 4 MB charged each, 128 KB of file each.
|
||||
let real = dib_record(&dib_1bpp(1024, 1024 * 1024 / 8));
|
||||
let (rec_real, _) = officeart::record(&real).unwrap();
|
||||
let input = 20 * real.len();
|
||||
let mut work = officeart::Decode::new(crate::media::decode_work(input, floor));
|
||||
for i in 0..20 {
|
||||
let b = officeart::blip(&rec_real, 64 << 20, &mut work).unwrap();
|
||||
assert!(!b.placeholder, "scan {i} decodes");
|
||||
}
|
||||
// Declared 20000² (1.6 GB each) in a few dozen bytes each: a placeholder, never decoded.
|
||||
let bomb = dib_record(&dib_1bpp(20_000, 16));
|
||||
let (rec_bomb, _) = officeart::record(&bomb).unwrap();
|
||||
let mut work = officeart::Decode::new(crate::media::decode_work(20 * bomb.len(), floor));
|
||||
for _ in 0..20 {
|
||||
assert!(officeart::blip(&rec_bomb, 64 << 20, &mut work).unwrap().placeholder);
|
||||
}
|
||||
assert!(work.left <= crate::media::decode_work(20 * bomb.len(), floor), "nothing was refunded or invented");
|
||||
}
|
||||
|
||||
/// #3871 review: the copy of a DIB into a BMP is charged before it is made, however few pixels
|
||||
/// the header declares; with the budget spent nothing is copied at all.
|
||||
#[test]
|
||||
fn a_dib_with_a_large_tail_is_charged_for_its_copy() {
|
||||
let big = dib_record(&dib_1bpp(1, 1 << 20)); // 1 pixel declared, 1 MB of trailing bytes
|
||||
let (big, _) = officeart::record(&big).unwrap();
|
||||
let dib_len = big.body.len() - 17;
|
||||
let mut work = officeart::Decode::new(10 << 20);
|
||||
assert!(!officeart::blip(&big, 64 << 20, &mut work).unwrap().placeholder);
|
||||
assert_eq!(10 << 20, work.left + dib_len, "charged for the bytes, not just 4 per pixel");
|
||||
// Room for the copy: decoded. Not even that: nothing is copied.
|
||||
let mut work = officeart::Decode::new(dib_len + 1);
|
||||
assert!(officeart::blip(&big, 64 << 20, &mut work).is_some());
|
||||
let mut work = officeart::Decode::new(dib_len - 1);
|
||||
assert!(officeart::blip(&big, 64 << 20, &mut work).is_none());
|
||||
assert_eq!(work.left, dib_len - 1);
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
//! Password-protected files. The fixtures are encrypted here with a small encryptor written from
|
||||
//! [MS-DOC] §2.2.6 and [MS-OFFCRYPTO] §2.3.5-2.3.7; no file produced by Word is used.
|
||||
|
||||
use std::io::Read;
|
||||
|
||||
use sha1::{Digest, Sha1};
|
||||
|
||||
use super::{Piece, body_texts, cfb_file, text_doc};
|
||||
use crate::DocbinError;
|
||||
use crate::crypt::{F_ENCRYPTED, F_OBFUSCATED, Rc4, md5, xor_array, xor_verifier};
|
||||
use crate::{is_encrypted, read, read_with_password};
|
||||
|
||||
const TEXT: &str = "Secret \u{20ac} plans";
|
||||
/// Where the piece table's pair lives in the FIB: `fcClx` of pair 33.
|
||||
const CLX_PAIR: usize = 0x9A + 33 * 8;
|
||||
|
||||
fn plain() -> (Vec<u8>, Vec<u8>) {
|
||||
let f = text_doc(&[Piece { compressed: false, text: format!("{TEXT}\r") }], &[], 0);
|
||||
let mut comp = cfb::CompoundFile::open(std::io::Cursor::new(f)).unwrap();
|
||||
let mut get = |n: &str| {
|
||||
let mut v = Vec::new();
|
||||
comp.open_stream(format!("/{n}")).unwrap().read_to_end(&mut v).unwrap();
|
||||
v
|
||||
};
|
||||
(get("WordDocument"), get("0Table"))
|
||||
}
|
||||
|
||||
fn rewrap(word: &[u8], table: &[u8]) -> Vec<u8> {
|
||||
cfb_file(&[("WordDocument", word), ("0Table", table)])
|
||||
}
|
||||
|
||||
fn put16(v: &mut [u8], at: usize, x: u16) {
|
||||
v[at..at + 2].copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
|
||||
fn put32(v: &mut [u8], at: usize, x: u32) {
|
||||
v[at..at + 4].copy_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
|
||||
fn utf16(s: &str) -> Vec<u8> {
|
||||
s.encode_utf16().flat_map(u16::to_le_bytes).collect()
|
||||
}
|
||||
|
||||
fn sha1(parts: &[&[u8]]) -> Vec<u8> {
|
||||
let mut h = Sha1::new();
|
||||
for p in parts {
|
||||
h.update(p);
|
||||
}
|
||||
h.finalize().to_vec()
|
||||
}
|
||||
|
||||
/// Encrypt (= RC4 is symmetric) `data` in 512-byte blocks; the first `skip` bytes stay as they are
|
||||
/// but the keystream still runs over them.
|
||||
fn rc4_stream(data: &mut [u8], skip: usize, key: &dyn Fn(u32) -> Vec<u8>) {
|
||||
for (n, chunk) in data.chunks_mut(512).enumerate() {
|
||||
let orig = chunk.to_vec();
|
||||
Rc4::new(&key(n as u32)).apply(chunk);
|
||||
let keep = skip.saturating_sub(n * 512).min(chunk.len());
|
||||
chunk[..keep].copy_from_slice(&orig[..keep]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Put `header` in front of the table and set the FIB's encryption fields.
|
||||
fn assemble(mut word: Vec<u8>, table: &[u8], header: &[u8], flags: u16, lkey: u32) -> (Vec<u8>, Vec<u8>) {
|
||||
let fl = u16::from_le_bytes([word[0x0A], word[0x0B]]) | flags;
|
||||
put16(&mut word, 0x0A, fl);
|
||||
put32(&mut word, 0x0E, lkey);
|
||||
let fc = u32::from_le_bytes(word[CLX_PAIR..CLX_PAIR + 4].try_into().unwrap());
|
||||
put32(&mut word, CLX_PAIR, fc + header.len() as u32);
|
||||
([word].concat(), [header, table].concat())
|
||||
}
|
||||
|
||||
fn xor_file(pw: &[u8]) -> Vec<u8> {
|
||||
let (word, table) = plain();
|
||||
let (mut word, mut table) = assemble(word, &table, &[], F_ENCRYPTED | F_OBFUSCATED, xor_verifier(pw));
|
||||
let arr = xor_array(pw);
|
||||
for (data, skip) in [(&mut word, 68), (&mut table, 0)] {
|
||||
for (i, b) in data.iter_mut().enumerate().skip(skip) {
|
||||
if *b != 0 && *b ^ arr[i % 16] != 0 {
|
||||
*b ^= arr[i % 16];
|
||||
}
|
||||
}
|
||||
}
|
||||
rewrap(&word, &table)
|
||||
}
|
||||
|
||||
fn rc4_header(pw: &str) -> (Vec<u8>, Box<dyn Fn(u32) -> Vec<u8>>) {
|
||||
let salt = [7u8; 16];
|
||||
let h0 = md5(&utf16(pw));
|
||||
let h1 = md5(&(0..16).flat_map(|_| [&h0[..5], &salt[..]].concat()).collect::<Vec<u8>>());
|
||||
let trunc = h1[..5].to_vec();
|
||||
let key: Box<dyn Fn(u32) -> Vec<u8>> = Box::new(move |b| md5(&[trunc.as_slice(), &b.to_le_bytes()].concat()).to_vec());
|
||||
let verifier = *b"0123456789abcdef";
|
||||
let mut enc = [verifier.to_vec(), md5(&verifier).to_vec()].concat();
|
||||
Rc4::new(&key(0)).apply(&mut enc);
|
||||
([&[1, 0, 1, 0][..], &salt, &enc].concat(), key)
|
||||
}
|
||||
|
||||
fn capi_header(pw: &str, bits: u32, hflags: u32) -> (Vec<u8>, Box<dyn Fn(u32) -> Vec<u8>>) {
|
||||
let salt = [9u8; 16];
|
||||
let h0 = sha1(&[&salt, &utf16(pw)]);
|
||||
let n = (bits / 8) as usize;
|
||||
let key: Box<dyn Fn(u32) -> Vec<u8>> = Box::new(move |b| {
|
||||
let mut k = sha1(&[&h0, &b.to_le_bytes()])[..n].to_vec();
|
||||
if n == 5 {
|
||||
k.resize(16, 0);
|
||||
}
|
||||
k
|
||||
});
|
||||
let verifier = *b"fedcba9876543210";
|
||||
let mut enc = [verifier.to_vec(), sha1(&[&verifier])].concat();
|
||||
Rc4::new(&key(0)).apply(&mut enc);
|
||||
let mut body = Vec::new();
|
||||
for x in [hflags, 0, 0x6801, 0x8004, bits, 1, 0, 0] {
|
||||
body.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
body.extend_from_slice(&utf16("Test RC4 provider\0"));
|
||||
let mut h = vec![3, 0, 2, 0];
|
||||
h.extend_from_slice(&hflags.to_le_bytes());
|
||||
h.extend_from_slice(&(body.len() as u32).to_le_bytes());
|
||||
h.extend_from_slice(&body);
|
||||
h.extend_from_slice(&16u32.to_le_bytes());
|
||||
h.extend_from_slice(&salt);
|
||||
h.extend_from_slice(&enc[..16]);
|
||||
h.extend_from_slice(&20u32.to_le_bytes());
|
||||
h.extend_from_slice(&enc[16..]);
|
||||
(h, key)
|
||||
}
|
||||
|
||||
fn rc4_file_with(header: Vec<u8>, key: &dyn Fn(u32) -> Vec<u8>) -> Vec<u8> {
|
||||
let (word, table) = plain();
|
||||
let (mut word, mut table) = assemble(word, &table, &header, F_ENCRYPTED, header.len() as u32);
|
||||
rc4_stream(&mut word, 68, key);
|
||||
rc4_stream(&mut table, header.len(), key);
|
||||
rewrap(&word, &table)
|
||||
}
|
||||
|
||||
fn rc4_file(pw: &str) -> Vec<u8> {
|
||||
let (h, k) = rc4_header(pw);
|
||||
rc4_file_with(h, &*k)
|
||||
}
|
||||
|
||||
fn capi_file(pw: &str, bits: u32) -> Vec<u8> {
|
||||
let (h, k) = capi_header(pw, bits, 0x04);
|
||||
rc4_file_with(h, &*k)
|
||||
}
|
||||
|
||||
fn check(f: &[u8], pw: &str, wrong: &str) {
|
||||
assert!(is_encrypted(f));
|
||||
assert_eq!(read(f).unwrap_err(), DocbinError::PasswordRequired);
|
||||
assert_eq!(read_with_password(f, Some(wrong)).unwrap_err(), DocbinError::WrongPassword);
|
||||
assert_eq!(read_with_password(f, Some("")).unwrap_err(), DocbinError::WrongPassword);
|
||||
let doc = read_with_password(f, Some(pw)).unwrap();
|
||||
assert_eq!(body_texts(&doc), [TEXT]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xor_obfuscation_round_trip() {
|
||||
check(&xor_file(b"hunter2"), "hunter2", "hunter3");
|
||||
// Longer than 15 characters: only the first 15 count, as in the format.
|
||||
let long = xor_file(b"abcdefghijklmno");
|
||||
assert_eq!(body_texts(&read_with_password(&long, Some("abcdefghijklmnopqrst")).unwrap()), [TEXT]);
|
||||
// A non-ASCII (Latin-1) password.
|
||||
check(&xor_file(&[b'p', 0xE4, b's', b's']), "p\u{e4}ss", "pass");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rc4_round_trip() {
|
||||
check(&rc4_file("correct horse"), "correct horse", "Correct horse");
|
||||
check(&rc4_file("p\u{e4}ssw\u{f6}rd \u{1f511}"), "p\u{e4}ssw\u{f6}rd \u{1f511}", "password");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rc4_crypto_api_round_trip() {
|
||||
check(&capi_file("battery staple", 128), "battery staple", "battery stapl");
|
||||
// 40-bit keys are zero-padded to 128 bits.
|
||||
check(&capi_file("battery staple", 40), "battery staple", "x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unencrypted_files_ignore_the_password() {
|
||||
let f = text_doc(&[Piece { compressed: false, text: "Plain\r".into() }], &[], 0);
|
||||
assert!(!is_encrypted(&f));
|
||||
assert_eq!(body_texts(&read_with_password(&f, Some("whatever")).unwrap()), ["Plain"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tampered_verifier_is_a_wrong_password_never_a_garbled_document() {
|
||||
let (h, k) = rc4_header("pw");
|
||||
let mut bad = h.clone();
|
||||
bad[40] ^= 1; // inside the encrypted verifier hash
|
||||
assert_eq!(read_with_password(&rc4_file_with(bad, &*k), Some("pw")).unwrap_err(), DocbinError::WrongPassword);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_variants_say_so() {
|
||||
// AES and external providers, an unknown header version.
|
||||
for hflags in [0x04 | 0x20, 0x10] {
|
||||
let (h, k) = capi_header("pw", 128, hflags);
|
||||
let e = read_with_password(&rc4_file_with(h, &*k), Some("pw")).unwrap_err();
|
||||
assert!(matches!(e, DocbinError::Encryption(_)), "{e:?}");
|
||||
if hflags & 0x20 != 0 {
|
||||
assert!(e.to_string().contains("only defined for the newer encrypted-package format"), "{e}");
|
||||
}
|
||||
}
|
||||
let (mut h, k) = rc4_header("pw");
|
||||
h[0] = 9;
|
||||
assert!(matches!(read_with_password(&rc4_file_with(h, &*k), Some("pw")), Err(DocbinError::Encryption(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn damaged_headers_are_errors_never_panics() {
|
||||
for f in [rc4_header("pw"), capi_header("pw", 128, 0x04)] {
|
||||
let (h, k) = f;
|
||||
let full = rc4_file_with(h.clone(), &*k);
|
||||
let (word, table) = {
|
||||
let mut comp = cfb::CompoundFile::open(std::io::Cursor::new(&full)).unwrap();
|
||||
let mut get = |n: &str| {
|
||||
let mut v = Vec::new();
|
||||
comp.open_stream(format!("/{n}")).unwrap().read_to_end(&mut v).unwrap();
|
||||
v
|
||||
};
|
||||
(get("WordDocument"), get("0Table"))
|
||||
};
|
||||
// Truncated everywhere in and around the header: an error, not a panic.
|
||||
for n in 0..h.len() + 8 {
|
||||
let r = read_with_password(&rewrap(&word, &table[..n.min(table.len())]), Some("pw"));
|
||||
assert!(r.is_err(), "length {n}");
|
||||
}
|
||||
// Every header byte garbled.
|
||||
for at in 0..h.len() {
|
||||
let mut t = table.clone();
|
||||
t[at] ^= 0xFF;
|
||||
let _ = read_with_password(&rewrap(&word, &t), Some("pw"));
|
||||
}
|
||||
}
|
||||
// A header size claiming more than the stream holds.
|
||||
let (mut h, k) = capi_header("pw", 128, 0x04);
|
||||
h[8..12].copy_from_slice(&u32::MAX.to_le_bytes());
|
||||
assert!(read_with_password(&rc4_file_with(h, &*k), Some("pw")).is_err());
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
//! OLE property sets constructed from MS-OLEPS, exercised through the public DOC reader.
|
||||
use super::{Piece, body_texts, text_doc};
|
||||
use std::io::{Cursor, Seek, SeekFrom, Write};
|
||||
|
||||
fn summary(cp: u16, values: &[(u32, u32, Vec<u8>)]) -> Vec<u8> {
|
||||
let mut props = vec![(1, [2u32.to_le_bytes().as_slice(), &cp.to_le_bytes(), &[0, 0]].concat())];
|
||||
for (id, kind, bytes) in values {
|
||||
let size = if *kind == 31 { bytes.len() / 2 } else { bytes.len() } as u32;
|
||||
let mut value = [kind.to_le_bytes().as_slice(), &size.to_le_bytes(), bytes].concat();
|
||||
while !value.len().is_multiple_of(4) {
|
||||
value.push(0);
|
||||
}
|
||||
props.push((*id, value));
|
||||
}
|
||||
let mut section = vec![0; 8 + 8 * props.len()];
|
||||
section[4..8].copy_from_slice(&(props.len() as u32).to_le_bytes());
|
||||
for (i, (id, value)) in props.iter().enumerate() {
|
||||
section[8 + i * 8..12 + i * 8].copy_from_slice(&id.to_le_bytes());
|
||||
let offset = section.len() as u32;
|
||||
section[12 + i * 8..16 + i * 8].copy_from_slice(&offset.to_le_bytes());
|
||||
section.extend_from_slice(value);
|
||||
}
|
||||
let size = section.len() as u32;
|
||||
section[..4].copy_from_slice(&size.to_le_bytes());
|
||||
let mut header = vec![0; 48];
|
||||
header[..2].copy_from_slice(&[0xfe, 0xff]);
|
||||
header[24..28].copy_from_slice(&1u32.to_le_bytes());
|
||||
header[28..44].copy_from_slice(&[0xe0, 0x85, 0x9f, 0xf2, 0xf9, 0x4f, 0x68, 0x10, 0xab, 0x91, 0x08, 0, 0x2b, 0x27, 0xb3, 0xd9]);
|
||||
header[44..48].copy_from_slice(&48u32.to_le_bytes());
|
||||
header.extend(section);
|
||||
header
|
||||
}
|
||||
|
||||
fn open_with_metadata(bytes: &[u8], template: bool) -> wordcraft_doc::Document {
|
||||
let file = text_doc(&[Piece { compressed: false, text: "Body stays readable\r".into() }], &[], 0);
|
||||
let mut cfb = cfb::CompoundFile::open(Cursor::new(file)).unwrap();
|
||||
cfb.create_stream("/\u{5}SummaryInformation").unwrap().write_all(bytes).unwrap();
|
||||
if template {
|
||||
let mut stream = cfb.open_stream("/WordDocument").unwrap();
|
||||
stream.seek(SeekFrom::Start(0x0a)).unwrap();
|
||||
stream.write_all(&0x1001u16.to_le_bytes()).unwrap();
|
||||
}
|
||||
let doc = crate::read(&cfb.into_inner().into_inner()).unwrap();
|
||||
assert_eq!(body_texts(&doc), ["Body stays readable"]);
|
||||
doc
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doc_and_dot_import_summary_text_properties() {
|
||||
let data = summary(
|
||||
1252,
|
||||
&[
|
||||
(2, 30, b"R\xe9sum\xe9\0".to_vec()),
|
||||
(3, 30, b"Subject\0".to_vec()),
|
||||
(4, 30, b"Writer\0".to_vec()),
|
||||
(5, 30, b"one;two\0".to_vec()),
|
||||
(6, 30, b"Description\0".to_vec()),
|
||||
(8, 30, b"Editor\0".to_vec()),
|
||||
(9, 30, b"12\0".to_vec()),
|
||||
],
|
||||
);
|
||||
for template in [false, true] {
|
||||
let c = open_with_metadata(&data, template).core;
|
||||
assert_eq!(c.title, "Résumé");
|
||||
assert_eq!(c.subject, "Subject");
|
||||
assert_eq!(c.creator, "Writer");
|
||||
assert_eq!(c.keywords, "one;two");
|
||||
assert_eq!(c.description, "Description");
|
||||
assert_eq!(c.last_modified_by, "Editor");
|
||||
assert_eq!(c.revision, 12);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn summary_handles_codepages_and_utf16_length_units() {
|
||||
let wide: Vec<u8> = "Unicode ä 文 😀\0".encode_utf16().flat_map(u16::to_le_bytes).collect();
|
||||
for kind in [30, 31] {
|
||||
assert_eq!(open_with_metadata(&summary(1200, &[(2, kind, wide.clone())]), false).core.title, "Unicode ä 文 😀");
|
||||
}
|
||||
assert_eq!(open_with_metadata(&summary(65001, &[(2, 30, "UTF-8 ä\0".as_bytes().to_vec())]), false).core.title, "UTF-8 ä");
|
||||
assert_eq!(open_with_metadata(&summary(1251, &[(2, 30, vec![0xcf, 0xf0, 0xe8, 0xe2, 0xe5, 0xf2, 0])]), false).core.title, "Привет");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_or_oversized_summary_does_not_break_doc_open() {
|
||||
let valid = summary(1252, &[(2, 30, b"Title\0".to_vec())]);
|
||||
for n in [0, 2, 24, 44, 47, 55, valid.len() - 5] {
|
||||
assert!(open_with_metadata(&valid[..n], false).core.title.is_empty());
|
||||
}
|
||||
for (at, value) in [(44, u32::MAX), (48, u32::MAX), (52, u32::MAX), (68, 4), (84, u32::MAX)] {
|
||||
let mut bad = valid.clone();
|
||||
bad[at..at + 4].copy_from_slice(&value.to_le_bytes());
|
||||
assert!(open_with_metadata(&bad, false).core.title.is_empty(), "offset {at}");
|
||||
}
|
||||
let huge = summary(1252, &[(2, 30, vec![b'x'; (64 << 10) + 1])]);
|
||||
assert!(open_with_metadata(&huge, false).core.title.is_empty());
|
||||
assert!(open_with_metadata(&vec![0; (2 << 20) + 1], false).core.title.is_empty());
|
||||
}
|
||||
|
||||
fn custom_summary(props: &[(u32, Vec<u8>)], num_property_sets: u32) -> Vec<u8> {
|
||||
let mut section = vec![0; 8 + 8 * props.len()];
|
||||
section[4..8].copy_from_slice(&(props.len() as u32).to_le_bytes());
|
||||
for (i, (id, value)) in props.iter().enumerate() {
|
||||
section[8 + i * 8..12 + i * 8].copy_from_slice(&id.to_le_bytes());
|
||||
let offset = section.len() as u32;
|
||||
section[12 + i * 8..16 + i * 8].copy_from_slice(&offset.to_le_bytes());
|
||||
section.extend_from_slice(value);
|
||||
}
|
||||
let size = section.len() as u32;
|
||||
section[..4].copy_from_slice(&size.to_le_bytes());
|
||||
let mut header = vec![0; 28 + 20 * num_property_sets as usize];
|
||||
header[..2].copy_from_slice(&[0xfe, 0xff]);
|
||||
header[24..28].copy_from_slice(&num_property_sets.to_le_bytes());
|
||||
// First property set FMTID and offset
|
||||
header[28..44].copy_from_slice(&[0xe0, 0x85, 0x9f, 0xf2, 0xf9, 0x4f, 0x68, 0x10, 0xab, 0x91, 0x08, 0, 0x2b, 0x27, 0xb3, 0xd9]);
|
||||
let first_offset = header.len() as u32;
|
||||
header[44..48].copy_from_slice(&first_offset.to_le_bytes());
|
||||
if num_property_sets > 1 {
|
||||
// Dummy second property set header
|
||||
header[48..64].copy_from_slice(&[1; 16]);
|
||||
let second_offset = (first_offset + size) as u32;
|
||||
header[64..68].copy_from_slice(&second_offset.to_le_bytes());
|
||||
}
|
||||
header.extend(section);
|
||||
header
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn summary_defaults_codepage_and_accepts_vti4_and_num_property_sets() {
|
||||
let title_prop = [30u32.to_le_bytes().as_slice(), &7u32.to_le_bytes(), b"Default\0"].concat();
|
||||
// 1. Missing PID_CODEPAGE (property 1) should default to 1252 per MS-OLEPS.
|
||||
let no_cp = custom_summary(&[(2, title_prop.clone())], 1);
|
||||
assert_eq!(open_with_metadata(&no_cp, false).core.title, "Default");
|
||||
|
||||
// 2. PID_CODEPAGE as VT_I4 (type 3).
|
||||
let cp_vti4 = [3u32.to_le_bytes().as_slice(), &1252u32.to_le_bytes()].concat();
|
||||
let with_vti4 = custom_summary(&[(1, cp_vti4), (2, title_prop.clone())], 1);
|
||||
assert_eq!(open_with_metadata(&with_vti4, false).core.title, "Default");
|
||||
|
||||
// 3. Stream with NumPropertySets = 2.
|
||||
let multi_sets = custom_summary(&[(2, title_prop)], 2);
|
||||
assert_eq!(open_with_metadata(&multi_sets, false).core.title, "Default");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn summary_parses_filetime_created_and_modified_dates() {
|
||||
// 2024-03-15T12:30:45Z = 133549794450000000 in 100ns since 1601-01-01
|
||||
let ft_val: u64 = 133_549_794_450_000_000;
|
||||
let ft_low = (ft_val & 0xFFFFFFFF) as u32;
|
||||
let ft_high = (ft_val >> 32) as u32;
|
||||
let filetime_bytes = [64u32.to_le_bytes().as_slice(), &ft_low.to_le_bytes(), &ft_high.to_le_bytes()].concat();
|
||||
|
||||
let stream = custom_summary(
|
||||
&[
|
||||
(12, filetime_bytes.clone()), // PIDSI_CREATE_DTM
|
||||
(13, filetime_bytes), // PIDSI_LASTSAVE_DTM
|
||||
],
|
||||
1,
|
||||
);
|
||||
|
||||
let core = open_with_metadata(&stream, false).core;
|
||||
assert_eq!(core.created, "2024-03-15T12:30:45Z");
|
||||
assert_eq!(core.modified, "2024-03-15T12:30:45Z");
|
||||
}
|
||||
+10
-21
@@ -9,30 +9,19 @@ description = "WordCraft DOCX (OOXML WordprocessingML) reader and writer"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-docx = { workspace = true }
|
||||
craft-wordmodel = { workspace = true }
|
||||
wordcraft-doc = { workspace = true }
|
||||
wordcraft-geom = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
unicode-bidi = { workspace = true }
|
||||
cfb = { workspace = true }
|
||||
aes = { workspace = true }
|
||||
sha1 = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
hmac = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
getrandom = { workspace = true }
|
||||
zeroize = { workspace = true }
|
||||
|
||||
# The browser's `crypto.getRandomValues` for the salts and keys of encrypted saves.
|
||||
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
|
||||
getrandom = { workspace = true, features = ["wasm_js"] }
|
||||
craft-xml = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
wordcraft-doc = { workspace = true, features = ["test-support"] }
|
||||
serde_json = { workspace = true }
|
||||
wordcraft-geom = { workspace = true, features = ["image"] }
|
||||
base64 = { workspace = true }
|
||||
cfb = { workspace = true }
|
||||
log = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
image = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
craft-zip = { workspace = true }
|
||||
|
||||
@@ -1,304 +0,0 @@
|
||||
//! Word bibliography sources: a `b:Sources` document — the custom XML part of a document, or
|
||||
//! Word's own `Sources.xml` master list, imported through Manage Sources (#2640).
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use wordcraft_doc::Source;
|
||||
|
||||
use crate::DocxError;
|
||||
use crate::xml::El;
|
||||
|
||||
/// Bibliography sources kept from one file (Word's own source manager holds far fewer).
|
||||
pub const MAX_SOURCES: usize = 10_000;
|
||||
/// Characters kept of a bibliography's citation style name.
|
||||
pub const MAX_STYLE_NAME: usize = 64;
|
||||
|
||||
/// A `b:Sources` document read into the document model: its sources in file order, and the
|
||||
/// citation style it names (`b:Sources/@StyleName`), when it has one.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Sources {
|
||||
pub sources: Vec<Source>,
|
||||
pub style: Option<String>,
|
||||
}
|
||||
|
||||
/// The bibliography namespace Word's own `Sources.xml` files may carry instead of OOXML's (the
|
||||
/// VBA `Source.XML` format, `schemas.microsoft.com/office/word/2004/10/bibliography`).
|
||||
const WORD_2004_BIBLIOGRAPHY: &str = "http://schemas.microsoft.com/office/word/2004/10/bibliography";
|
||||
|
||||
/// Read a Word bibliography sources file — Word's `Sources.xml`, or any `b:Sources` document in
|
||||
/// one of the bibliography namespaces (ECMA-376's, or Word's own 2004/10) — into [`Sources`].
|
||||
pub fn read_sources(bytes: &[u8]) -> Result<Sources, DocxError> {
|
||||
// `parse_ns` maps Word's 2004/10 namespace onto the canonical `b` prefix as well, so both
|
||||
// formats read; the root check then refuses an un-namespaced or unrelated `<Sources>`.
|
||||
let root = crate::xml::parse_ns(bytes, &[("b", WORD_2004_BIBLIOGRAPHY)])?;
|
||||
if root.name != "b:Sources" {
|
||||
return Err(DocxError::NotWord(format!("root element is {}", root.name)));
|
||||
}
|
||||
Ok(parse(&root, &mut Seen::default(), MAX_SOURCES))
|
||||
}
|
||||
|
||||
/// Bibliography sources already read: tags, and (title, author) of untagged ones.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Seen {
|
||||
tags: HashSet<String>,
|
||||
untagged: HashSet<(String, String)>,
|
||||
}
|
||||
|
||||
/// The fresh sources and style of a parsed `b:Sources` root element: `seen` carries the tags and
|
||||
/// untagged (title, author) keys already taken, so repeated parts collapse into one source, and
|
||||
/// at most `room` fresh sources are built (later ones are left unread, bounding hostile files).
|
||||
pub(crate) fn parse(root: &El, seen: &mut Seen, room: usize) -> Sources {
|
||||
let style = root
|
||||
.attrs
|
||||
.iter()
|
||||
.find(|(k, _)| k.rsplit(':').next() == Some("StyleName"))
|
||||
.map(|(_, v)| v.trim())
|
||||
.filter(|name| !name.is_empty())
|
||||
.map(|name| name.chars().take(MAX_STYLE_NAME).collect());
|
||||
let child_text = |el: &El, local: &str| el.els().find(|e| e.local() == local).map(|e| e.text()).unwrap_or_default();
|
||||
let parse_persons = |parent: &El| -> String {
|
||||
let mut names = Vec::new();
|
||||
let person_els: Vec<&El> = {
|
||||
let in_lists: Vec<&El> =
|
||||
parent.els().filter(|e| e.local() == "NameList").flat_map(|n| n.els().filter(|e| e.local() == "Person")).collect();
|
||||
if in_lists.is_empty() { parent.els().filter(|e| e.local() == "Person").collect() } else { in_lists }
|
||||
};
|
||||
for name_el in person_els {
|
||||
let last = child_text(name_el, "Last");
|
||||
let first = child_text(name_el, "First");
|
||||
let middle = child_text(name_el, "Middle");
|
||||
let name = if !first.is_empty() && !middle.is_empty() {
|
||||
format!("{last}, {first} {middle}")
|
||||
} else if !first.is_empty() {
|
||||
format!("{last}, {first}")
|
||||
} else {
|
||||
last
|
||||
};
|
||||
if !name.is_empty() {
|
||||
names.push(name);
|
||||
}
|
||||
}
|
||||
names.join("; ")
|
||||
};
|
||||
|
||||
let mut out = Sources { sources: Vec::new(), style };
|
||||
for src_el in root.els().filter(|e| e.local() == "Source") {
|
||||
if out.sources.len() >= room {
|
||||
log::warn!("docx: more than {MAX_SOURCES} bibliography sources; the rest are dropped");
|
||||
break;
|
||||
}
|
||||
let tag = child_text(src_el, "Tag");
|
||||
let raw_kind = child_text(src_el, "SourceType");
|
||||
let kind = crate::write::from_source_type(&raw_kind);
|
||||
let (author, corporate, editor) = match src_el.els().find(|e| e.local() == "Author") {
|
||||
Some(a) => {
|
||||
let role = a.els().find(|e| e.local() == "Author");
|
||||
let auth = role.map(parse_persons).filter(|s| !s.is_empty()).unwrap_or_else(|| parse_persons(a));
|
||||
// Word's Corporate Author: one name, kept whole (`b:Corporate`, #4848).
|
||||
let corporate = role.map(|r| child_text(r, "Corporate").trim().to_string()).unwrap_or_default();
|
||||
let ed = a.els().find(|e| e.local() == "Editor").map(parse_persons).unwrap_or_default();
|
||||
(auth, corporate, ed)
|
||||
}
|
||||
None => (String::new(), String::new(), String::new()),
|
||||
};
|
||||
let editor = if editor.is_empty() { src_el.els().find(|e| e.local() == "Editor").map(parse_persons).unwrap_or_default() } else { editor };
|
||||
let title = child_text(src_el, "Title");
|
||||
let year = child_text(src_el, "Year");
|
||||
let month = child_text(src_el, "Month");
|
||||
let day = child_text(src_el, "Day");
|
||||
let city = child_text(src_el, "City");
|
||||
let publisher = child_text(src_el, "Publisher");
|
||||
let journal = child_text(src_el, "JournalName");
|
||||
let edition = child_text(src_el, "Edition");
|
||||
let volume = child_text(src_el, "Volume");
|
||||
let issue = child_text(src_el, "Issue");
|
||||
let pages = child_text(src_el, "Pages");
|
||||
let url = child_text(src_el, "URL");
|
||||
let accessed = match child_text(src_el, "Accessed") {
|
||||
a if !a.is_empty() => a,
|
||||
_ => {
|
||||
let y = child_text(src_el, "YearAccessed");
|
||||
let m = child_text(src_el, "MonthAccessed");
|
||||
let d = child_text(src_el, "DayAccessed");
|
||||
if !y.is_empty() && !m.is_empty() && !d.is_empty() {
|
||||
format!("{y}-{m}-{d}")
|
||||
} else if !y.is_empty() && !m.is_empty() {
|
||||
format!("{y}-{m}")
|
||||
} else {
|
||||
y
|
||||
}
|
||||
}
|
||||
};
|
||||
let doi = match child_text(src_el, "DOI") {
|
||||
d if !d.is_empty() => d,
|
||||
_ => child_text(src_el, "StandardNumber"),
|
||||
};
|
||||
|
||||
// Keep sources with some content, once each (by tag, or title and author untagged).
|
||||
if tag.is_empty() && title.is_empty() && author.is_empty() && corporate.is_empty() && editor.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let fresh = if tag.is_empty() {
|
||||
let who = [&author, &corporate, &editor].into_iter().find(|w| !w.is_empty()).cloned().unwrap_or_default();
|
||||
seen.untagged.insert((title.clone(), who))
|
||||
} else {
|
||||
seen.tags.insert(tag.clone())
|
||||
};
|
||||
if !fresh {
|
||||
continue;
|
||||
}
|
||||
out.sources.push(Source {
|
||||
tag,
|
||||
kind,
|
||||
author,
|
||||
corporate,
|
||||
title,
|
||||
year,
|
||||
city,
|
||||
publisher,
|
||||
journal,
|
||||
volume,
|
||||
pages,
|
||||
url,
|
||||
editor,
|
||||
edition,
|
||||
issue,
|
||||
month,
|
||||
day,
|
||||
accessed,
|
||||
doi,
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const WORD_SOURCES: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<b:Sources xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography"
|
||||
xmlns="http://schemas.openxmlformats.org/officeDocument/2006/bibliography"
|
||||
SelectedStyle="\MLA.XSL" StyleName="MLA">
|
||||
<b:Source>
|
||||
<b:Tag>Riv21</b:Tag>
|
||||
<b:SourceType>Book</b:SourceType>
|
||||
<b:Author>
|
||||
<b:Author>
|
||||
<b:NameList>
|
||||
<b:Person><b:Last>Rivera</b:Last><b:First>Alex</b:First></b:Person>
|
||||
<b:Person><b:Last>Chen</b:Last><b:First>Mei</b:First><b:Middle>L.</b:Middle></b:Person>
|
||||
</b:NameList>
|
||||
</b:Author>
|
||||
</b:Author>
|
||||
<b:Title>Shared Spaces</b:Title>
|
||||
<b:Year>2021</b:Year>
|
||||
<b:City>Portland</b:City>
|
||||
<b:Publisher>Harbor Books</b:Publisher>
|
||||
</b:Source>
|
||||
<b:Source>
|
||||
<b:Tag>Web19</b:Tag>
|
||||
<b:SourceType>InternetSite</b:SourceType>
|
||||
<b:Author>
|
||||
<b:Author>
|
||||
<b:NameList>
|
||||
<b:Person><b:Last>Okafor</b:Last><b:First>Ada</b:First></b:Person>
|
||||
</b:NameList>
|
||||
</b:Author>
|
||||
</b:Author>
|
||||
<b:Title>Dockyards of the north</b:Title>
|
||||
<b:Year>2019</b:Year>
|
||||
<b:URL>https://example.org/docks</b:URL>
|
||||
<b:YearAccessed>2024</b:YearAccessed>
|
||||
<b:MonthAccessed>May</b:MonthAccessed>
|
||||
<b:DayAccessed>17</b:DayAccessed>
|
||||
<b:StandardNumber>10.1234/x</b:StandardNumber>
|
||||
</b:Source>
|
||||
<b:Source>
|
||||
<b:Tag>NoDate</b:Tag>
|
||||
<b:SourceType>JournalArticle</b:SourceType>
|
||||
<b:Title>Untitled notes</b:Title>
|
||||
<b:JournalName>Field Notes</b:JournalName>
|
||||
<b:Volume>4</b:Volume>
|
||||
</b:Source>
|
||||
<b:Source>
|
||||
<b:Tag>Riv21</b:Tag>
|
||||
<b:SourceType>Book</b:SourceType>
|
||||
<b:Title>A duplicate of Riv21</b:Title>
|
||||
<b:Year>2021</b:Year>
|
||||
</b:Source>
|
||||
<b:Source>
|
||||
<b:SourceType>Book</b:SourceType>
|
||||
</b:Source>
|
||||
</b:Sources>"#;
|
||||
|
||||
/// A Word `Sources.xml` (`b:Sources`): its style, the `b:Source` elements with their names,
|
||||
/// kinds and fallback fields; a repeated tag and an entry with no content at all are dropped.
|
||||
#[test]
|
||||
fn a_word_sources_file_reads_into_sources() {
|
||||
let f = read_sources(WORD_SOURCES.as_bytes()).unwrap();
|
||||
assert_eq!(f.style.as_deref(), Some("MLA"), "the citation style comes from StyleName");
|
||||
assert_eq!(
|
||||
f.sources.len(),
|
||||
3,
|
||||
"the repeated tag and the empty b:Source are dropped: {:?}",
|
||||
f.sources.iter().map(|s| &s.tag).collect::<Vec<_>>()
|
||||
);
|
||||
let book = &f.sources[0];
|
||||
assert_eq!((book.tag.as_str(), book.kind.as_str()), ("Riv21", "book"));
|
||||
assert_eq!(book.author, "Rivera, Alex; Chen, Mei L.");
|
||||
assert_eq!(book.title, "Shared Spaces", "the first source of a repeated tag wins");
|
||||
assert_eq!(
|
||||
(book.title.as_str(), book.year.as_str(), book.city.as_str(), book.publisher.as_str()),
|
||||
("Shared Spaces", "2021", "Portland", "Harbor Books")
|
||||
);
|
||||
let web = &f.sources[1];
|
||||
assert_eq!(web.kind, "website");
|
||||
assert_eq!(web.author, "Okafor, Ada");
|
||||
assert_eq!(web.accessed, "2024-May-17", "assemble the accessed date from its parts");
|
||||
assert_eq!(web.doi, "10.1234/x", "StandardNumber stands in for a missing DOI");
|
||||
let notes = &f.sources[2];
|
||||
assert_eq!((notes.kind.as_str(), notes.journal.as_str(), notes.volume.as_str()), ("article", "Field Notes", "4"));
|
||||
}
|
||||
|
||||
/// Anything whose root isn't `b:Sources` is refused, not read as an empty bibliography.
|
||||
#[test]
|
||||
fn a_file_that_is_not_b_sources_is_an_error() {
|
||||
assert!(read_sources(b"<html><body>hi</body></html>").is_err());
|
||||
assert!(matches!(read_sources(b"not xml at all"), Err(DocxError::Xml(_))));
|
||||
assert!(read_sources(b"<Sources><Source><Tag>A</Tag></Source></Sources>").is_err(), "no namespace");
|
||||
assert!(read_sources(br#"<o:Sources xmlns:o="urn:example:other"><o:Source/></o:Sources>"#).is_err(), "an unrelated namespace");
|
||||
}
|
||||
|
||||
/// #4848: a corporate author (Word's Corporate Author box, `b:Corporate`) is read whole, and
|
||||
/// an untagged source with only a corporate author is still kept, once.
|
||||
#[test]
|
||||
fn a_corporate_author_is_read_whole() {
|
||||
let xml = br#"<b:Sources xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography">
|
||||
<b:Source><b:Tag>Wor20</b:Tag><b:SourceType>Report</b:SourceType>
|
||||
<b:Author><b:Author><b:Corporate> World Health Organization </b:Corporate></b:Author></b:Author>
|
||||
<b:Title>World report on hearing</b:Title><b:Year>2020</b:Year></b:Source>
|
||||
<b:Source><b:SourceType>Book</b:SourceType>
|
||||
<b:Author><b:Author><b:Corporate>Harbor Guild, Inc.</b:Corporate></b:Author></b:Author></b:Source>
|
||||
<b:Source><b:SourceType>Book</b:SourceType>
|
||||
<b:Author><b:Author><b:Corporate>Harbor Guild, Inc.</b:Corporate></b:Author></b:Author></b:Source>
|
||||
</b:Sources>"#;
|
||||
let f = read_sources(xml).unwrap();
|
||||
assert_eq!(f.sources.len(), 2, "{:?}", f.sources);
|
||||
assert_eq!((f.sources[0].corporate.as_str(), f.sources[0].author.as_str()), ("World Health Organization", ""));
|
||||
assert_eq!(f.sources[1].corporate, "Harbor Guild, Inc.", "a comma doesn't split a corporate name");
|
||||
}
|
||||
|
||||
/// Word's own 2004/10 bibliography namespace (`Sources.xml` written by scripts and older
|
||||
/// builds, the VBA `Source.XML` format) reads as well as OOXML's.
|
||||
#[test]
|
||||
fn word_2004_bibliography_namespace_is_accepted() {
|
||||
let xml = br#"<b:Sources xmlns:b="http://schemas.microsoft.com/office/word/2004/10/bibliography" StyleName="MLA">
|
||||
<b:Source><b:Tag>Riv21</b:Tag><b:SourceType>Book</b:SourceType><b:Title>Shared Spaces</b:Title><b:Year>2021</b:Year></b:Source>
|
||||
</b:Sources>"#;
|
||||
let f = read_sources(xml).unwrap();
|
||||
assert_eq!(f.style.as_deref(), Some("MLA"));
|
||||
assert_eq!(f.sources.len(), 1);
|
||||
assert_eq!(f.sources[0].tag, "Riv21");
|
||||
}
|
||||
}
|
||||
@@ -1,550 +0,0 @@
|
||||
//! Charts WordCraft makes ([`ChartSpec`]) as chart parts (ECMA-376 Part 1 §21.2) and back.
|
||||
//!
|
||||
//! [`chart_xml`] writes a `c:chartSpace` with the data as literals in the part itself
|
||||
//! (`c:strLit`, `c:numLit`): no embedded workbook (`c:externalData`) and no external references.
|
||||
//! [`chart_items`] draws a chart through the same code that draws chart parts read from files.
|
||||
//! [`spec_of`] turns a chart part back into the model only when writing that model again gives
|
||||
//! the same markup, so a chart with anything the model leaves out stays as it was read.
|
||||
|
||||
use wordcraft_doc::chart::{ChartSeries, ChartSpec, ChartType, LegendPos, MAX_POINTS, MAX_SERIES};
|
||||
use wordcraft_doc::graphic::GraphicItem;
|
||||
use wordcraft_doc::props::Rgb;
|
||||
|
||||
use crate::xml::{self, El, MAX_DEPTH, Node, W};
|
||||
|
||||
const NS_C: &str = "http://schemas.openxmlformats.org/drawingml/2006/chart";
|
||||
const NS_A: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";
|
||||
const NS_R: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
|
||||
/// Content type of a chart part.
|
||||
pub(crate) const CHART_CT: &str = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml";
|
||||
/// `a:graphicData` URI of a chart.
|
||||
pub(crate) const CHART_URI: &str = "http://schemas.openxmlformats.org/drawingml/2006/chart";
|
||||
/// Axis ids (any pair of distinct numbers will do; Word keeps them).
|
||||
const CAT_AX: &str = "500000001";
|
||||
const VAL_AX: &str = "500000002";
|
||||
|
||||
/// The chart part for `spec`: a complete XML document, written as [`ChartSpec::sanitize`] leaves
|
||||
/// it (a value that isn't finite is a gap).
|
||||
pub fn chart_xml(spec: &ChartSpec) -> String {
|
||||
chart_part(spec).0
|
||||
}
|
||||
|
||||
/// [`chart_xml`], and whether the chart limits left series or points of `spec` out of it.
|
||||
pub(crate) fn chart_part(spec: &ChartSpec) -> (String, bool) {
|
||||
let kept = bounded(spec);
|
||||
let points = spec.series.iter().map(|s| s.values.len()).max().unwrap_or(0).max(spec.categories.len());
|
||||
let cut = kept.series.len() < spec.series.len() || kept.points() < points;
|
||||
(write_chart(&kept), cut)
|
||||
}
|
||||
|
||||
fn write_chart(spec: &ChartSpec) -> String {
|
||||
let mut w = W::new();
|
||||
w.open("c:chartSpace", &[("xmlns:c", NS_C), ("xmlns:a", NS_A), ("xmlns:r", NS_R)]);
|
||||
w.empty("c:roundedCorners", &[("val", "0")]);
|
||||
w.open("c:chart", &[]);
|
||||
if let Some(t) = &spec.title {
|
||||
w.open("c:title", &[]);
|
||||
w.open("c:tx", &[]);
|
||||
w.open("c:rich", &[]);
|
||||
w.empty("a:bodyPr", &[]);
|
||||
w.empty("a:lstStyle", &[]);
|
||||
w.open("a:p", &[]);
|
||||
w.open("a:r", &[]);
|
||||
w.leaf("a:t", &[], t);
|
||||
w.close("a:r");
|
||||
w.close("a:p");
|
||||
w.close("c:rich");
|
||||
w.close("c:tx");
|
||||
w.empty("c:overlay", &[("val", "0")]);
|
||||
w.close("c:title");
|
||||
}
|
||||
w.empty("c:autoTitleDeleted", &[("val", if spec.title.is_some() { "0" } else { "1" })]);
|
||||
w.open("c:plotArea", &[]);
|
||||
w.empty("c:layout", &[]);
|
||||
plot(&mut w, spec);
|
||||
w.close("c:plotArea");
|
||||
if let Some(pos) = spec.legend {
|
||||
w.open("c:legend", &[]);
|
||||
let p = match pos {
|
||||
LegendPos::Right => "r",
|
||||
LegendPos::Left => "l",
|
||||
LegendPos::Top => "t",
|
||||
LegendPos::Bottom => "b",
|
||||
};
|
||||
w.empty("c:legendPos", &[("val", p)]);
|
||||
w.empty("c:overlay", &[("val", "0")]);
|
||||
w.close("c:legend");
|
||||
}
|
||||
w.empty("c:plotVisOnly", &[("val", "1")]);
|
||||
w.empty("c:dispBlanksAs", &[("val", "gap")]);
|
||||
w.close("c:chart");
|
||||
// A white chart area without a border, in the document's own background colour.
|
||||
w.open("c:spPr", &[]);
|
||||
w.open("a:solidFill", &[]);
|
||||
w.empty("a:schemeClr", &[("val", "bg1")]);
|
||||
w.close("a:solidFill");
|
||||
w.open("a:ln", &[]);
|
||||
w.empty("a:noFill", &[]);
|
||||
w.close("a:ln");
|
||||
w.close("c:spPr");
|
||||
w.close("c:chartSpace");
|
||||
w.s
|
||||
}
|
||||
|
||||
/// `spec` held to the chart limits. A model deserialised as is (a `.json` document) never went
|
||||
/// through [`ChartSpec::sanitize`], and every series repeats every category, so its size would
|
||||
/// multiply (#3648). Only what the limits keep is copied before sanitizing.
|
||||
fn bounded(spec: &ChartSpec) -> ChartSpec {
|
||||
ChartSpec {
|
||||
kind: spec.kind,
|
||||
categories: spec.categories.iter().take(MAX_POINTS).cloned().collect(),
|
||||
series: spec
|
||||
.series
|
||||
.iter()
|
||||
.take(MAX_SERIES)
|
||||
.map(|s| ChartSeries { name: s.name.clone(), values: s.values.iter().take(MAX_POINTS).copied().collect() })
|
||||
.collect(),
|
||||
title: spec.title.clone(),
|
||||
legend: spec.legend,
|
||||
data_labels: spec.data_labels,
|
||||
}
|
||||
.sanitized()
|
||||
}
|
||||
|
||||
/// What `spec` draws as, `w` × `h` points, in the colours of `theme` (series take the accents in
|
||||
/// turn). Empty when the chart has no series.
|
||||
pub fn chart_items(spec: &ChartSpec, theme: &[Rgb], w: f32, h: f32) -> Vec<GraphicItem> {
|
||||
match xml::parse(chart_xml(spec).as_bytes()) {
|
||||
Ok(space) => crate::read::chart::chart_items(&space, theme, w, h),
|
||||
Err(_) => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The chart type element and the series in it, then the axes.
|
||||
fn plot(w: &mut W, spec: &ChartSpec) {
|
||||
let k = spec.kind;
|
||||
let el = match k {
|
||||
ChartType::Column | ChartType::ColumnStacked | ChartType::Bar | ChartType::BarStacked => "c:barChart",
|
||||
ChartType::Line => "c:lineChart",
|
||||
ChartType::Area => "c:areaChart",
|
||||
ChartType::Pie => "c:pieChart",
|
||||
ChartType::Doughnut => "c:doughnutChart",
|
||||
ChartType::Scatter => "c:scatterChart",
|
||||
};
|
||||
let stacked = matches!(k, ChartType::ColumnStacked | ChartType::BarStacked);
|
||||
let horizontal = matches!(k, ChartType::Bar | ChartType::BarStacked);
|
||||
w.open(el, &[]);
|
||||
match k {
|
||||
ChartType::Column | ChartType::ColumnStacked | ChartType::Bar | ChartType::BarStacked => {
|
||||
w.empty("c:barDir", &[("val", if horizontal { "bar" } else { "col" })]);
|
||||
w.empty("c:grouping", &[("val", if stacked { "stacked" } else { "clustered" })]);
|
||||
}
|
||||
ChartType::Line | ChartType::Area => w.empty("c:grouping", &[("val", "standard")]),
|
||||
ChartType::Scatter => w.empty("c:scatterStyle", &[("val", "lineMarker")]),
|
||||
ChartType::Pie | ChartType::Doughnut => {}
|
||||
}
|
||||
w.empty("c:varyColors", &[("val", if k.is_round() { "1" } else { "0" })]);
|
||||
// Pie and doughnut charts show the first series; the others are kept for other types.
|
||||
for (i, s) in spec.series.iter().enumerate() {
|
||||
series(w, spec, i, s);
|
||||
}
|
||||
if spec.data_labels {
|
||||
w.open("c:dLbls", &[]);
|
||||
for (name, on) in [
|
||||
("c:showLegendKey", false),
|
||||
("c:showVal", true),
|
||||
("c:showCatName", false),
|
||||
("c:showSerName", false),
|
||||
("c:showPercent", false),
|
||||
("c:showBubbleSize", false),
|
||||
] {
|
||||
w.empty(name, &[("val", if on { "1" } else { "0" })]);
|
||||
}
|
||||
w.close("c:dLbls");
|
||||
}
|
||||
match k {
|
||||
ChartType::Column | ChartType::ColumnStacked | ChartType::Bar | ChartType::BarStacked => {
|
||||
w.empty("c:gapWidth", &[("val", "150")]);
|
||||
if stacked {
|
||||
w.empty("c:overlap", &[("val", "100")]);
|
||||
}
|
||||
}
|
||||
ChartType::Line => w.empty("c:marker", &[("val", "1")]),
|
||||
ChartType::Pie => w.empty("c:firstSliceAng", &[("val", "0")]),
|
||||
ChartType::Doughnut => {
|
||||
w.empty("c:firstSliceAng", &[("val", "0")]);
|
||||
w.empty("c:holeSize", &[("val", "50")]);
|
||||
}
|
||||
ChartType::Area | ChartType::Scatter => {}
|
||||
}
|
||||
if !k.is_round() {
|
||||
w.empty("c:axId", &[("val", CAT_AX)]);
|
||||
w.empty("c:axId", &[("val", VAL_AX)]);
|
||||
}
|
||||
w.close(el);
|
||||
match k {
|
||||
ChartType::Pie | ChartType::Doughnut => {}
|
||||
ChartType::Scatter => {
|
||||
value_axis(w, CAT_AX, VAL_AX, "b", false, "midCat");
|
||||
value_axis(w, VAL_AX, CAT_AX, "l", true, "midCat");
|
||||
}
|
||||
_ => {
|
||||
category_axis(w, if horizontal { "l" } else { "b" });
|
||||
value_axis(w, VAL_AX, CAT_AX, if horizontal { "b" } else { "l" }, true, "between");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One `c:ser` (ECMA-376 element order: idx, order, tx, spPr, marker, cat/xVal, val/yVal, smooth).
|
||||
fn series(w: &mut W, spec: &ChartSpec, i: usize, s: &ChartSeries) {
|
||||
let k = spec.kind;
|
||||
let i = i.to_string();
|
||||
w.open("c:ser", &[]);
|
||||
w.empty("c:idx", &[("val", &i)]);
|
||||
w.empty("c:order", &[("val", &i)]);
|
||||
if !s.name.is_empty() {
|
||||
w.open("c:tx", &[]);
|
||||
w.leaf("c:v", &[], &s.name);
|
||||
w.close("c:tx");
|
||||
}
|
||||
match k {
|
||||
// Markers only: the series has no line.
|
||||
ChartType::Scatter => {
|
||||
w.open("c:spPr", &[]);
|
||||
w.open("a:ln", &[("w", "19050")]);
|
||||
w.empty("a:noFill", &[]);
|
||||
w.close("a:ln");
|
||||
w.close("c:spPr");
|
||||
}
|
||||
ChartType::Column | ChartType::ColumnStacked | ChartType::Bar | ChartType::BarStacked => {
|
||||
w.empty("c:invertIfNegative", &[("val", "0")]);
|
||||
}
|
||||
ChartType::Line => {
|
||||
w.open("c:marker", &[]);
|
||||
w.empty("c:symbol", &[("val", "none")]);
|
||||
w.close("c:marker");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if k == ChartType::Scatter {
|
||||
let xs: Option<Vec<Option<f64>>> = spec
|
||||
.categories
|
||||
.iter()
|
||||
.map(|c| if c.is_empty() { Some(None) } else { c.parse::<f64>().ok().filter(|v| v.is_finite()).map(Some) })
|
||||
.collect();
|
||||
w.open("c:xVal", &[]);
|
||||
match xs {
|
||||
Some(xs) => num_lit(w, &xs),
|
||||
None => str_lit(w, &spec.categories),
|
||||
}
|
||||
w.close("c:xVal");
|
||||
w.open("c:yVal", &[]);
|
||||
num_lit(w, &s.values);
|
||||
w.close("c:yVal");
|
||||
} else {
|
||||
w.open("c:cat", &[]);
|
||||
str_lit(w, &spec.categories);
|
||||
w.close("c:cat");
|
||||
w.open("c:val", &[]);
|
||||
num_lit(w, &s.values);
|
||||
w.close("c:val");
|
||||
}
|
||||
if matches!(k, ChartType::Line | ChartType::Scatter) {
|
||||
w.empty("c:smooth", &[("val", "0")]);
|
||||
}
|
||||
w.close("c:ser");
|
||||
}
|
||||
|
||||
fn str_lit(w: &mut W, items: &[String]) {
|
||||
w.open("c:strLit", &[]);
|
||||
w.empty("c:ptCount", &[("val", &items.len().to_string())]);
|
||||
for (i, s) in items.iter().enumerate().filter(|(_, s)| !s.is_empty()) {
|
||||
w.open("c:pt", &[("idx", &i.to_string())]);
|
||||
w.leaf("c:v", &[], s);
|
||||
w.close("c:pt");
|
||||
}
|
||||
w.close("c:strLit");
|
||||
}
|
||||
|
||||
fn num_lit(w: &mut W, values: &[Option<f64>]) {
|
||||
w.open("c:numLit", &[]);
|
||||
w.leaf("c:formatCode", &[], "General");
|
||||
w.empty("c:ptCount", &[("val", &values.len().to_string())]);
|
||||
for (i, v) in values.iter().enumerate() {
|
||||
if let Some(v) = v.filter(|v| v.is_finite()) {
|
||||
w.open("c:pt", &[("idx", &i.to_string())]);
|
||||
w.leaf("c:v", &[], &num(v));
|
||||
w.close("c:pt");
|
||||
}
|
||||
}
|
||||
w.close("c:numLit");
|
||||
}
|
||||
|
||||
/// A value as the shortest text that reads back as the same number.
|
||||
fn num(v: f64) -> String {
|
||||
if v == 0.0 { "0".to_string() } else { format!("{v}") }
|
||||
}
|
||||
|
||||
fn category_axis(w: &mut W, pos: &str) {
|
||||
w.open("c:catAx", &[]);
|
||||
w.empty("c:axId", &[("val", CAT_AX)]);
|
||||
w.open("c:scaling", &[]);
|
||||
w.empty("c:orientation", &[("val", "minMax")]);
|
||||
w.close("c:scaling");
|
||||
w.empty("c:delete", &[("val", "0")]);
|
||||
w.empty("c:axPos", &[("val", pos)]);
|
||||
w.empty("c:majorTickMark", &[("val", "none")]);
|
||||
w.empty("c:minorTickMark", &[("val", "none")]);
|
||||
w.empty("c:tickLblPos", &[("val", "nextTo")]);
|
||||
w.empty("c:crossAx", &[("val", VAL_AX)]);
|
||||
w.empty("c:crosses", &[("val", "autoZero")]);
|
||||
w.empty("c:auto", &[("val", "1")]);
|
||||
w.empty("c:lblAlgn", &[("val", "ctr")]);
|
||||
w.empty("c:lblOffset", &[("val", "100")]);
|
||||
w.empty("c:noMultiLvlLbl", &[("val", "0")]);
|
||||
w.close("c:catAx");
|
||||
}
|
||||
|
||||
/// A value axis: with gridlines and no axis line (`grid`), else with its line and no gridlines.
|
||||
fn value_axis(w: &mut W, id: &str, cross: &str, pos: &str, grid: bool, between: &str) {
|
||||
w.open("c:valAx", &[]);
|
||||
w.empty("c:axId", &[("val", id)]);
|
||||
w.open("c:scaling", &[]);
|
||||
w.empty("c:orientation", &[("val", "minMax")]);
|
||||
w.close("c:scaling");
|
||||
w.empty("c:delete", &[("val", "0")]);
|
||||
w.empty("c:axPos", &[("val", pos)]);
|
||||
if grid {
|
||||
w.empty("c:majorGridlines", &[]);
|
||||
}
|
||||
w.empty("c:majorTickMark", &[("val", "none")]);
|
||||
w.empty("c:minorTickMark", &[("val", "none")]);
|
||||
w.empty("c:tickLblPos", &[("val", "nextTo")]);
|
||||
if grid {
|
||||
w.open("c:spPr", &[]);
|
||||
w.open("a:ln", &[]);
|
||||
w.empty("a:noFill", &[]);
|
||||
w.close("a:ln");
|
||||
w.close("c:spPr");
|
||||
}
|
||||
w.empty("c:crossAx", &[("val", cross)]);
|
||||
w.empty("c:crosses", &[("val", "autoZero")]);
|
||||
w.empty("c:crossBetween", &[("val", between)]);
|
||||
w.close("c:valAx");
|
||||
}
|
||||
|
||||
/// The chart a chart part holds, when the part is exactly what [`chart_xml`] writes for it (so
|
||||
/// editing and saving it loses nothing). `None` for any other chart.
|
||||
pub(crate) fn spec_of(space: &El) -> Option<ChartSpec> {
|
||||
let chart = space.child("c:chart")?;
|
||||
let plot = chart.child("c:plotArea")?;
|
||||
let ty = plot.els().find(|e| e.name.ends_with("Chart"))?;
|
||||
let val = |e: &El, n: &str| e.child(n).and_then(|c| c.attr("val")).map(str::to_string);
|
||||
let bar = |stacked_kind, clustered_kind| if val(ty, "c:grouping").as_deref() == Some("stacked") { stacked_kind } else { clustered_kind };
|
||||
let kind = match ty.name.as_str() {
|
||||
"c:barChart" if val(ty, "c:barDir").as_deref() == Some("bar") => bar(ChartType::BarStacked, ChartType::Bar),
|
||||
"c:barChart" => bar(ChartType::ColumnStacked, ChartType::Column),
|
||||
"c:lineChart" => ChartType::Line,
|
||||
"c:areaChart" => ChartType::Area,
|
||||
"c:pieChart" => ChartType::Pie,
|
||||
"c:doughnutChart" => ChartType::Doughnut,
|
||||
"c:scatterChart" => ChartType::Scatter,
|
||||
_ => return None,
|
||||
};
|
||||
let scatter = kind == ChartType::Scatter;
|
||||
let sers: Vec<&El> = ty.children("c:ser").take(wordcraft_doc::chart::MAX_SERIES + 1).collect();
|
||||
// More than the model holds can't be the same chart: don't build it to find out.
|
||||
let count = |s: &&El| s.find("c:ptCount").and_then(|c| c.attr("val")).and_then(|v| v.parse::<usize>().ok()).unwrap_or(0);
|
||||
let longest = sers.iter().map(count).max().unwrap_or(0);
|
||||
if sers.len() > wordcraft_doc::chart::MAX_SERIES
|
||||
|| longest > wordcraft_doc::chart::MAX_POINTS
|
||||
|| sers.len().saturating_mul(longest) > wordcraft_doc::chart::MAX_CELLS
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let categories = sers.first().and_then(|s| s.child(if scatter { "c:xVal" } else { "c:cat" })).map(literal).unwrap_or_default();
|
||||
let series = sers
|
||||
.iter()
|
||||
.map(|s| ChartSeries {
|
||||
name: s.child("c:tx").and_then(|t| t.child("c:v")).map(|v| v.text()).unwrap_or_default(),
|
||||
values: s
|
||||
.child(if scatter { "c:yVal" } else { "c:val" })
|
||||
.map(literal)
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(|v| v.parse::<f64>().ok().filter(|v| v.is_finite()))
|
||||
.collect(),
|
||||
})
|
||||
.collect();
|
||||
let title = chart.child("c:title").and_then(|t| t.find("a:t")).map(|t| t.text());
|
||||
let legend = chart.child("c:legend").map(|l| match val(l, "c:legendPos").as_deref() {
|
||||
Some("l") => LegendPos::Left,
|
||||
Some("t") => LegendPos::Top,
|
||||
Some("b") => LegendPos::Bottom,
|
||||
_ => LegendPos::Right,
|
||||
});
|
||||
let data_labels = ty.child("c:dLbls").and_then(|d| d.child("c:showVal")).and_then(|v| v.attr("val")) == Some("1");
|
||||
let spec = ChartSpec { kind, categories, series, title, legend, data_labels }.sanitized();
|
||||
// Editable only when nothing would be lost: writing the model again gives the same part.
|
||||
let again = xml::parse(chart_xml(&spec).as_bytes()).ok()?;
|
||||
same(space, &again, 0).then_some(spec)
|
||||
}
|
||||
|
||||
/// The points of a `c:strLit` / `c:numLit` (or a cache) as texts by index, `ptCount` long.
|
||||
fn literal(e: &El) -> Vec<String> {
|
||||
let Some(lit) = e.els().find(|c| matches!(c.name.as_str(), "c:strLit" | "c:numLit")) else { return Vec::new() };
|
||||
let n = lit
|
||||
.child("c:ptCount")
|
||||
.and_then(|c| c.attr("val"))
|
||||
.and_then(|v| v.parse::<usize>().ok())
|
||||
.unwrap_or(0)
|
||||
.min(wordcraft_doc::chart::MAX_POINTS + 1);
|
||||
let mut out = vec![String::new(); n];
|
||||
for p in lit.children("c:pt") {
|
||||
if let Some(slot) = p.attr("idx").and_then(|i| i.parse::<usize>().ok()).and_then(|i| out.get_mut(i)) {
|
||||
*slot = p.child("c:v").map(|v| v.text()).unwrap_or_default();
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether two elements are the same markup: names, attributes (in any order), child elements in
|
||||
/// order and text (whitespace-only text between elements ignored).
|
||||
fn same(a: &El, b: &El, depth: usize) -> bool {
|
||||
if depth > MAX_DEPTH || a.name != b.name || a.attrs.len() != b.attrs.len() {
|
||||
return false;
|
||||
}
|
||||
if !a.attrs.iter().all(|(k, v)| b.attr(k) == Some(v.as_str()) && b.attrs.iter().any(|(bk, _)| bk == k)) {
|
||||
return false;
|
||||
}
|
||||
let text = |e: &El| -> String {
|
||||
e.kids
|
||||
.iter()
|
||||
.filter_map(|n| match n {
|
||||
Node::Text(t) if !t.trim().is_empty() => Some(t.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
if text(a) != text(b) {
|
||||
return false;
|
||||
}
|
||||
let (mut x, mut y) = (a.els(), b.els());
|
||||
loop {
|
||||
match (x.next(), y.next()) {
|
||||
(None, None) => return true,
|
||||
(Some(p), Some(q)) if same(p, q, depth + 1) => {}
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use wordcraft_doc::THEME_COLORS;
|
||||
|
||||
fn parse(spec: &ChartSpec) -> El {
|
||||
xml::parse(chart_xml(spec).as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
/// Children of `e` by name, in order.
|
||||
fn names(e: &El) -> Vec<&str> {
|
||||
e.els().map(|c| c.name.as_str()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chart_past_the_limits_is_written_within_them() {
|
||||
// A model deserialised as is (a `.json` document) never went through `sanitize`, and every
|
||||
// series repeats every category: 300 series of 4,500 points wrote 2.7 million points (#3648).
|
||||
use wordcraft_doc::chart::MAX_CELLS;
|
||||
let spec = ChartSpec {
|
||||
kind: ChartType::Column,
|
||||
categories: (0..4500).map(|i| i.to_string()).collect(),
|
||||
series: (0..300).map(|i| ChartSeries { name: format!("S{i}"), values: vec![Some(1.0); 4500] }).collect(),
|
||||
title: None,
|
||||
legend: None,
|
||||
data_labels: false,
|
||||
};
|
||||
let x = chart_xml(&spec);
|
||||
let series = x.matches("<c:ser>").count();
|
||||
let points = x.matches("<c:pt ").count();
|
||||
assert!((1..=MAX_SERIES).contains(&series), "{series} series");
|
||||
assert!(points <= 2 * MAX_CELLS, "{points} points");
|
||||
assert!(!x.contains(&format!("<c:pt idx=\"{MAX_POINTS}\"")), "past the point limit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_type_reads_back_as_itself() {
|
||||
for kind in ChartType::ALL {
|
||||
let mut spec = ChartSpec::sample(kind);
|
||||
spec.title = Some("Sales & <costs>".into());
|
||||
spec.data_labels = true;
|
||||
spec.legend = Some(LegendPos::Right);
|
||||
spec.series[0].values[1] = None;
|
||||
spec.categories[2] = String::new();
|
||||
let spec = spec.sanitized();
|
||||
assert_eq!(spec_of(&parse(&spec)).as_ref(), Some(&spec), "{kind:?}");
|
||||
assert!(!chart_items(&spec, &THEME_COLORS, 300.0, 200.0).is_empty(), "{kind:?} draws");
|
||||
}
|
||||
let plain = ChartSpec { legend: None, ..ChartSpec::sample(ChartType::Line) };
|
||||
assert_eq!(spec_of(&parse(&plain)), Some(plain));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chart_with_more_than_the_model_stays_read_only() {
|
||||
let spec = ChartSpec::sample(ChartType::Column);
|
||||
let x = chart_xml(&spec).replace("<c:roundedCorners val=\"0\"/>", "<c:roundedCorners val=\"0\"/><c:style val=\"2\"/>");
|
||||
assert_eq!(spec_of(&xml::parse(x.as_bytes()).unwrap()), None);
|
||||
let x = chart_xml(&spec).replace("<c:v>3.2</c:v>", "<c:v>3.20</c:v>");
|
||||
assert_eq!(spec_of(&xml::parse(x.as_bytes()).unwrap()), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn elements_come_in_schema_order() {
|
||||
let mut spec = ChartSpec::sample(ChartType::ColumnStacked);
|
||||
spec.title = Some("T".into());
|
||||
spec.data_labels = true;
|
||||
let space = parse(&spec);
|
||||
assert_eq!(names(&space), ["c:roundedCorners", "c:chart", "c:spPr"]);
|
||||
let chart = space.child("c:chart").unwrap();
|
||||
assert_eq!(names(chart), ["c:title", "c:autoTitleDeleted", "c:plotArea", "c:legend", "c:plotVisOnly", "c:dispBlanksAs"]);
|
||||
let plot = chart.child("c:plotArea").unwrap();
|
||||
assert_eq!(names(plot), ["c:layout", "c:barChart", "c:catAx", "c:valAx"]);
|
||||
let bar = plot.child("c:barChart").unwrap();
|
||||
assert_eq!(
|
||||
names(bar),
|
||||
["c:barDir", "c:grouping", "c:varyColors", "c:ser", "c:ser", "c:ser", "c:dLbls", "c:gapWidth", "c:overlap", "c:axId", "c:axId"]
|
||||
);
|
||||
assert_eq!(names(bar.child("c:ser").unwrap()), ["c:idx", "c:order", "c:tx", "c:invertIfNegative", "c:cat", "c:val"]);
|
||||
let val_ax = plot.child("c:valAx").unwrap();
|
||||
assert_eq!(
|
||||
names(val_ax),
|
||||
[
|
||||
"c:axId",
|
||||
"c:scaling",
|
||||
"c:delete",
|
||||
"c:axPos",
|
||||
"c:majorGridlines",
|
||||
"c:majorTickMark",
|
||||
"c:minorTickMark",
|
||||
"c:tickLblPos",
|
||||
"c:spPr",
|
||||
"c:crossAx",
|
||||
"c:crosses",
|
||||
"c:crossBetween"
|
||||
]
|
||||
);
|
||||
let line = parse(&ChartSpec { data_labels: true, ..ChartSpec::sample(ChartType::Line) });
|
||||
let lc = line.child("c:chart").unwrap().child("c:plotArea").unwrap().child("c:lineChart").unwrap();
|
||||
assert_eq!(names(lc), ["c:grouping", "c:varyColors", "c:ser", "c:ser", "c:ser", "c:dLbls", "c:marker", "c:axId", "c:axId"]);
|
||||
assert_eq!(names(lc.child("c:ser").unwrap()), ["c:idx", "c:order", "c:tx", "c:marker", "c:cat", "c:val", "c:smooth"]);
|
||||
let sc = parse(&ChartSpec::sample(ChartType::Scatter));
|
||||
let sc = sc.child("c:chart").unwrap().child("c:plotArea").unwrap().child("c:scatterChart").unwrap();
|
||||
assert_eq!(names(sc.child("c:ser").unwrap()), ["c:idx", "c:order", "c:tx", "c:spPr", "c:xVal", "c:yVal", "c:smooth"]);
|
||||
let dn = parse(&ChartSpec::sample(ChartType::Doughnut));
|
||||
let dn = dn.child("c:chart").unwrap().child("c:plotArea").unwrap();
|
||||
assert_eq!(names(dn), ["c:layout", "c:doughnutChart"]);
|
||||
assert_eq!(names(dn.child("c:doughnutChart").unwrap()), ["c:varyColors", "c:ser", "c:firstSliceAng", "c:holeSize"]);
|
||||
}
|
||||
}
|
||||
@@ -1,987 +0,0 @@
|
||||
//! Password-protected Word documents: ECMA-376 document encryption with the *agile* method
|
||||
//! (Word 2010 and later), written from Microsoft's public Open Specification [MS-OFFCRYPTO]
|
||||
//! §2.3.4.4 and §2.3.4.10–§2.3.4.15, with the data-spaces streams of §2.1 / §2.3.4.1–§2.3.4.3.
|
||||
//!
|
||||
//! An encrypted package is not a zip but an OLE compound file ([MS-CFB]) holding
|
||||
//! `EncryptionInfo` (how the key is derived from the password, as XML) and `EncryptedPackage`
|
||||
//! (the original zip, AES-CBC encrypted in 4096-byte segments). A random *intermediate key*
|
||||
//! encrypts the package; the password, hashed `spinCount` times with a salt, encrypts that key and
|
||||
//! a verifier that tells a wrong password apart; an HMAC over the encrypted stream checks that it
|
||||
//! hasn't been damaged or tampered with.
|
||||
//!
|
||||
//! Older packages (Word 2007) use the *Standard* method (§2.3.4.5–§2.3.4.9): a binary
|
||||
//! `EncryptionInfo`, one AES key derived from the password with SHA-1 and 50,000 spins, and the
|
||||
//! whole package AES-ECB encrypted. [`decrypt`] reads both; [`encrypt`] writes agile only.
|
||||
//!
|
||||
//! Files are hostile: every stream read is capped, every length is checked before slicing, and the
|
||||
//! spin count is bounded by the specification's maximum. Passwords and keys are wiped from memory
|
||||
//! when dropped ([`Zeroizing`]) and never logged.
|
||||
|
||||
use std::io::{Cursor, Read, Write};
|
||||
|
||||
use aes::cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit};
|
||||
use base64::Engine as _;
|
||||
use hmac::Mac;
|
||||
use sha2::Digest;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::DocxError;
|
||||
use crate::xml;
|
||||
|
||||
/// The spin count Word uses, and WordCraft writes.
|
||||
pub const DEFAULT_SPIN_COUNT: u32 = 100_000;
|
||||
/// The largest spin count the specification allows (§2.3.4.10 `ST_SpinCount`): it bounds the
|
||||
/// work a hostile file can ask for.
|
||||
const MAX_SPIN_COUNT: u32 = 10_000_000;
|
||||
/// Word's limit on password length, in characters.
|
||||
pub const MAX_PASSWORD_CHARS: usize = 255;
|
||||
/// `EncryptedPackage` is encrypted in segments of this many bytes (§2.3.4.15).
|
||||
const SEGMENT: usize = 4096;
|
||||
/// Largest `EncryptionInfo` stream read (the XML is a few kilobytes).
|
||||
const MAX_INFO: u64 = 1 << 20;
|
||||
/// AES's block size.
|
||||
const BLOCK: usize = 16;
|
||||
/// Salt length written.
|
||||
const SALT: usize = 16;
|
||||
|
||||
/// Block keys (§2.3.4.13, §2.3.4.14).
|
||||
const BK_VERIFIER_INPUT: [u8; 8] = [0xfe, 0xa7, 0xd2, 0x76, 0x3b, 0x4b, 0x9e, 0x79];
|
||||
const BK_VERIFIER_VALUE: [u8; 8] = [0xd7, 0xaa, 0x0f, 0x6d, 0x30, 0x61, 0x34, 0x4e];
|
||||
const BK_KEY_VALUE: [u8; 8] = [0x14, 0x6e, 0x0b, 0xe7, 0xab, 0xac, 0xd0, 0xd6];
|
||||
const BK_HMAC_KEY: [u8; 8] = [0x5f, 0xb2, 0xad, 0x01, 0x0c, 0xb9, 0xe1, 0xf6];
|
||||
const BK_HMAC_VALUE: [u8; 8] = [0xa0, 0x67, 0x7f, 0x02, 0xb2, 0x2c, 0x84, 0x33];
|
||||
|
||||
/// Standard encryption (§2.3.2, §2.3.4.5–§2.3.4.7): `EncryptionHeader.Flags` bits, the AES and
|
||||
/// SHA-1 algorithm ids, the provider type, the fixed spin count and the SHA-1 size.
|
||||
const F_CRYPTO_API: u32 = 0x04;
|
||||
const F_EXTERNAL: u32 = 0x10;
|
||||
const F_AES: u32 = 0x20;
|
||||
const STANDARD_FLAGS: u32 = F_CRYPTO_API | F_AES;
|
||||
const ALG_AES_128: u32 = 0x660E;
|
||||
const ALG_AES_192: u32 = 0x660F;
|
||||
const ALG_AES_256: u32 = 0x6610;
|
||||
const ALG_SHA1: u32 = 0x8004;
|
||||
const PROV_RSA_AES: u32 = 0x18;
|
||||
const STANDARD_SPIN_COUNT: u32 = 50_000;
|
||||
const SHA1_SIZE: usize = 20;
|
||||
|
||||
/// An OLE compound file starts with these bytes ([MS-CFB] §2.2).
|
||||
const CFB_MAGIC: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
|
||||
|
||||
const NS_ENCRYPTION: &str = "http://schemas.microsoft.com/office/2006/encryption";
|
||||
const NS_PASSWORD: &str = "http://schemas.microsoft.com/office/2006/keyEncryptor/password";
|
||||
|
||||
/// Is this an encrypted Office package (a compound file with `EncryptionInfo` and
|
||||
/// `EncryptedPackage` streams) rather than a zip?
|
||||
pub fn is_encrypted(bytes: &[u8]) -> bool {
|
||||
if bytes.get(..8) != Some(&CFB_MAGIC[..]) || wordcraft_doc::cfb_guard::check(bytes).is_err() {
|
||||
return false;
|
||||
}
|
||||
match cfb::CompoundFile::open(Cursor::new(bytes)) {
|
||||
Ok(comp) => comp.is_stream("/EncryptionInfo") && comp.is_stream("/EncryptedPackage"),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The zip package inside an encrypted Office file. Without a password (`None`) this says
|
||||
/// [`DocxError::PasswordRequired`] once it knows the file is one it can decrypt; a password that
|
||||
/// doesn't fit gives [`DocxError::WrongPassword`].
|
||||
pub fn decrypt(bytes: &[u8], password: Option<&str>) -> Result<Vec<u8>, DocxError> {
|
||||
wordcraft_doc::cfb_guard::check(bytes).map_err(|e| bad(format!("not a readable compound file ({e})")))?;
|
||||
let mut comp = cfb::CompoundFile::open(Cursor::new(bytes)).map_err(|e| bad(format!("not a readable compound file ({e})")))?;
|
||||
let info = stream(&mut comp, "/EncryptionInfo", MAX_INFO)?;
|
||||
let package = stream(&mut comp, "/EncryptedPackage", bytes.len() as u64)?;
|
||||
if matches!(info.get(..4), Some([2..=4, 0, 2, 0])) {
|
||||
return decrypt_standard(&info, &package, password);
|
||||
}
|
||||
let info = parse_info(&info)?;
|
||||
let Some(password) = password else { return Err(DocxError::PasswordRequired) };
|
||||
let pe = &info.password;
|
||||
let hn = iterate(pe.hash, &pe.salt, password, pe.spin_count);
|
||||
// The verifier (§2.3.4.13): a random value and its hash, both encrypted with keys derived
|
||||
// from the password. They match only for the right password.
|
||||
let key_input = derive_key(pe.hash, &hn, &BK_VERIFIER_INPUT, pe.key_bytes);
|
||||
let key_value = derive_key(pe.hash, &hn, &BK_VERIFIER_VALUE, pe.key_bytes);
|
||||
let iv = make_iv(pe.hash, &pe.salt, None, pe.block_size);
|
||||
let input = Zeroizing::new(cbc_decrypt(&key_input, &iv, &pe.verifier_input)?);
|
||||
let input = input.get(..pe.salt_size).ok_or_else(|| bad("the password verifier is too short"))?;
|
||||
let value = Zeroizing::new(cbc_decrypt(&key_value, &iv, &pe.verifier_hash)?);
|
||||
let value = value.get(..pe.hash.size()).ok_or_else(|| bad("the password verifier hash is too short"))?;
|
||||
if !same(&pe.hash.hash(&[input]), value) {
|
||||
return Err(DocxError::WrongPassword);
|
||||
}
|
||||
// The intermediate key that encrypts the package.
|
||||
let key_key = derive_key(pe.hash, &hn, &BK_KEY_VALUE, pe.key_bytes);
|
||||
let key = Zeroizing::new(cbc_decrypt(&key_key, &iv, &pe.key_value)?);
|
||||
let key = Zeroizing::new(key.get(..info.key_bytes).ok_or_else(|| bad("the encrypted key is too short"))?.to_vec());
|
||||
// Data integrity (§2.3.4.14): an HMAC of the whole EncryptedPackage stream. It is required:
|
||||
// a file that could drop it would let altered ciphertext, or an altered StreamSize, through
|
||||
// unauthenticated (`parse_info` refuses a file without it).
|
||||
let (enc_key, enc_value) = &info.integrity;
|
||||
let hmac_key = Zeroizing::new(cbc_decrypt(&key, &make_iv(info.hash, &info.salt, Some(&BK_HMAC_KEY), info.block_size), enc_key)?);
|
||||
// The key is the salt, zero-padded to the block size; HMAC pads short keys with zeros
|
||||
// anyway, so the padded key gives the same HMAC.
|
||||
let hmac_key = hmac_key.get(..hmac_key.len().min(info.hash.size())).unwrap_or(&[]);
|
||||
let expected = cbc_decrypt(&key, &make_iv(info.hash, &info.salt, Some(&BK_HMAC_VALUE), info.block_size), enc_value)?;
|
||||
let expected = expected.get(..info.hash.size()).ok_or_else(|| bad("the integrity check value is too short"))?;
|
||||
if !same(&info.hash.hmac(hmac_key, &package)?, expected) {
|
||||
return Err(bad("the document is damaged (its integrity check failed)"));
|
||||
}
|
||||
// The package itself (§2.3.4.15): StreamSize, then 4096-byte segments, each with its own IV.
|
||||
let size = package
|
||||
.get(..8)
|
||||
.and_then(|b| <[u8; 8]>::try_from(b).ok())
|
||||
.map(u64::from_le_bytes)
|
||||
.ok_or_else(|| bad("the encrypted package is truncated"))?;
|
||||
let data = package.get(8..).unwrap_or(&[]);
|
||||
let size = usize::try_from(size).ok().filter(|s| *s <= data.len()).ok_or_else(|| bad("the encrypted package is truncated"))?;
|
||||
let needed = size.div_ceil(BLOCK).saturating_mul(BLOCK);
|
||||
let data = data.get(..needed).ok_or_else(|| bad("the encrypted package is truncated"))?;
|
||||
let mut out = Vec::with_capacity(needed);
|
||||
for (i, seg) in data.chunks(SEGMENT).enumerate() {
|
||||
let i = u32::try_from(i).map_err(|_| bad("the encrypted package is too large"))?;
|
||||
let iv = make_iv(info.hash, &info.salt, Some(&i.to_le_bytes()), info.block_size);
|
||||
out.extend_from_slice(&cbc_decrypt(&key, &iv, seg)?);
|
||||
}
|
||||
out.truncate(size);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// [`decrypt`] for Standard encryption (§2.3.4.5–§2.3.4.9): one AES key from the password,
|
||||
/// checked with the verifier, and the whole package AES-ECB encrypted after its 8-byte size.
|
||||
fn decrypt_standard(info: &[u8], package: &[u8], password: Option<&str>) -> Result<Vec<u8>, DocxError> {
|
||||
let s = parse_standard(info)?;
|
||||
let Some(password) = password else { return Err(DocxError::PasswordRequired) };
|
||||
let key = standard_key(s.salt, password, s.key_bytes);
|
||||
// The verifier (§2.3.4.9): a random value and its SHA-1, both encrypted with the key.
|
||||
let verifier = Zeroizing::new(ecb(&key, s.verifier, false)?);
|
||||
let hash = Zeroizing::new(ecb(&key, s.verifier_hash, false)?);
|
||||
let hash = hash.get(..SHA1_SIZE).ok_or_else(|| bad("the password verifier hash is too short"))?;
|
||||
if !same(&Hash::Sha1.hash(&[&verifier]), hash) {
|
||||
return Err(DocxError::WrongPassword);
|
||||
}
|
||||
// The package (§2.3.4.4): StreamSize, then the data; bytes past the last block are ignored.
|
||||
let size = package
|
||||
.get(..8)
|
||||
.and_then(|b| <[u8; 8]>::try_from(b).ok())
|
||||
.map(u64::from_le_bytes)
|
||||
.ok_or_else(|| bad("the encrypted package is truncated"))?;
|
||||
let data = package.get(8..).unwrap_or(&[]);
|
||||
let size = usize::try_from(size).ok().filter(|s| *s <= data.len()).ok_or_else(|| bad("the encrypted package is truncated"))?;
|
||||
let needed = size.div_ceil(BLOCK).saturating_mul(BLOCK);
|
||||
let data = data.get(..needed).ok_or_else(|| bad("the encrypted package is truncated"))?;
|
||||
let mut out = ecb(&key, data, false)?;
|
||||
out.truncate(size);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// What a Standard `EncryptionInfo` stream says (§2.3.4.5): the key size, and the verifier's salt
|
||||
/// and encrypted values.
|
||||
struct Standard<'a> {
|
||||
key_bytes: usize,
|
||||
salt: &'a [u8],
|
||||
verifier: &'a [u8],
|
||||
verifier_hash: &'a [u8],
|
||||
}
|
||||
|
||||
fn parse_standard(info: &[u8]) -> Result<Standard<'_>, DocxError> {
|
||||
let short = || bad("EncryptionInfo is truncated");
|
||||
let u32_at = |at: usize| -> Result<u32, DocxError> {
|
||||
let b = at.checked_add(4).and_then(|end| info.get(at..end)).ok_or_else(short)?;
|
||||
<[u8; 4]>::try_from(b).map(u32::from_le_bytes).map_err(|_| short())
|
||||
};
|
||||
let flags = u32_at(4)?;
|
||||
if flags & F_EXTERNAL != 0 {
|
||||
return Err(bad("this file uses extensible encryption, which WordCraft can't open"));
|
||||
}
|
||||
if flags & STANDARD_FLAGS != STANDARD_FLAGS {
|
||||
return Err(bad("this file's encryption isn't AES, which WordCraft can't open"));
|
||||
}
|
||||
// EncryptionHeader (§2.3.2): Flags, SizeExtra, AlgID, AlgIDHash, KeySize, ProviderType, two
|
||||
// reserved words, then the CSP name, which isn't needed.
|
||||
let header_size = usize::try_from(u32_at(8)?).map_err(|_| short())?;
|
||||
if header_size < 32 {
|
||||
return Err(bad("the encryption header is too short"));
|
||||
}
|
||||
let verifier_at = header_size.checked_add(12).filter(|&v| v <= info.len()).ok_or_else(short)?;
|
||||
// AlgID 0 and AlgIDHash 0 mean "set by the flags": AES-128 and SHA-1.
|
||||
let bits = match u32_at(20)? {
|
||||
0 | ALG_AES_128 => 128,
|
||||
ALG_AES_192 => 192,
|
||||
ALG_AES_256 => 256,
|
||||
id => return Err(bad(format!("encryption algorithm 0x{id:X} isn't supported"))),
|
||||
};
|
||||
let hash_id = u32_at(24)?;
|
||||
if !matches!(hash_id, 0 | ALG_SHA1) {
|
||||
return Err(bad(format!("hash algorithm 0x{hash_id:X} isn't supported")));
|
||||
}
|
||||
if u32_at(28)? != bits {
|
||||
return Err(bad("the key size doesn't match the encryption algorithm"));
|
||||
}
|
||||
// EncryptionVerifier (§2.3.3): SaltSize (16), Salt, EncryptedVerifier, VerifierHashSize (20
|
||||
// for SHA-1), EncryptedVerifierHash (32 bytes for AES).
|
||||
if u32_at(verifier_at)? != SALT as u32 {
|
||||
return Err(bad("the salt isn't 16 bytes"));
|
||||
}
|
||||
let field = |at: usize, len: usize| verifier_at.checked_add(at).and_then(|a| info.get(a..a.checked_add(len)?)).ok_or_else(short);
|
||||
let salt = field(4, SALT)?;
|
||||
let verifier = field(4 + SALT, BLOCK)?;
|
||||
if u32_at(verifier_at.saturating_add(4 + SALT + BLOCK))? != SHA1_SIZE as u32 {
|
||||
return Err(bad("the password verifier hash isn't SHA-1"));
|
||||
}
|
||||
let verifier_hash = field(8 + SALT + BLOCK, 2 * BLOCK)?;
|
||||
Ok(Standard { key_bytes: bits as usize / 8, salt, verifier, verifier_hash })
|
||||
}
|
||||
|
||||
/// Encrypt a zip package with `password` (agile encryption: AES-256-CBC, SHA-512, Word's spin
|
||||
/// count), as Word's "Encrypt with Password" does.
|
||||
pub fn encrypt(package: &[u8], password: &str) -> Result<Vec<u8>, DocxError> {
|
||||
encrypt_with_spin_count(package, password, DEFAULT_SPIN_COUNT)
|
||||
}
|
||||
|
||||
/// [`encrypt`] with a chosen spin count (tests use a small one; files should use the default).
|
||||
pub fn encrypt_with_spin_count(package: &[u8], password: &str, spin_count: u32) -> Result<Vec<u8>, DocxError> {
|
||||
check_password(password)?;
|
||||
let spin_count = spin_count.min(MAX_SPIN_COUNT);
|
||||
let hash = Hash::Sha512;
|
||||
let key_bytes = 32;
|
||||
let key_data_salt = random(SALT)?;
|
||||
let password_salt = random(SALT)?;
|
||||
let key = random(key_bytes)?;
|
||||
let verifier = random(SALT)?;
|
||||
// The HMAC key is a salt as long as the hash (MS-OFFCRYPTO §2.3.4.14).
|
||||
let hmac_salt = random(hash.size())?;
|
||||
|
||||
// The package, segment by segment; the last segment is zero-padded to the block size.
|
||||
let mut stream = Vec::with_capacity(package.len().saturating_add(8 + BLOCK));
|
||||
stream.extend_from_slice(&(package.len() as u64).to_le_bytes());
|
||||
for (i, seg) in package.chunks(SEGMENT).enumerate() {
|
||||
let i = u32::try_from(i).map_err(|_| bad("the document is too large to encrypt"))?;
|
||||
let iv = make_iv(hash, &key_data_salt, Some(&i.to_le_bytes()), BLOCK);
|
||||
stream.extend_from_slice(&cbc_encrypt(&key, &iv, &padded(seg))?);
|
||||
}
|
||||
|
||||
// The password key encryptor (§2.3.4.13).
|
||||
let hn = iterate(hash, &password_salt, password, spin_count);
|
||||
let iv = make_iv(hash, &password_salt, None, BLOCK);
|
||||
let enc_verifier_input = cbc_encrypt(&derive_key(hash, &hn, &BK_VERIFIER_INPUT, key_bytes), &iv, &padded(&verifier))?;
|
||||
let enc_verifier_hash = cbc_encrypt(&derive_key(hash, &hn, &BK_VERIFIER_VALUE, key_bytes), &iv, &padded(&hash.hash(&[&verifier])))?;
|
||||
let enc_key_value = cbc_encrypt(&derive_key(hash, &hn, &BK_KEY_VALUE, key_bytes), &iv, &padded(&key))?;
|
||||
|
||||
// Data integrity (§2.3.4.14).
|
||||
let enc_hmac_key = cbc_encrypt(&key, &make_iv(hash, &key_data_salt, Some(&BK_HMAC_KEY), BLOCK), &padded(&hmac_salt))?;
|
||||
let hmac = hash.hmac(&hmac_salt, &stream)?;
|
||||
let enc_hmac_value = cbc_encrypt(&key, &make_iv(hash, &key_data_salt, Some(&BK_HMAC_VALUE), BLOCK), &padded(&hmac))?;
|
||||
|
||||
let b64 = |b: &[u8]| base64::engine::general_purpose::STANDARD.encode(b);
|
||||
let params = format!(
|
||||
r#"saltSize="{SALT}" blockSize="{BLOCK}" keyBits="{}" hashSize="{}" cipherAlgorithm="AES" cipherChaining="ChainingModeCBC" hashAlgorithm="{}""#,
|
||||
key_bytes * 8,
|
||||
hash.size(),
|
||||
hash.name(),
|
||||
);
|
||||
let xml = format!(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n\
|
||||
<encryption xmlns=\"{NS_ENCRYPTION}\" xmlns:p=\"{NS_PASSWORD}\">\
|
||||
<keyData {params} saltValue=\"{}\"/>\
|
||||
<dataIntegrity encryptedHmacKey=\"{}\" encryptedHmacValue=\"{}\"/>\
|
||||
<keyEncryptors><keyEncryptor uri=\"{NS_PASSWORD}\">\
|
||||
<p:encryptedKey spinCount=\"{spin_count}\" {params} saltValue=\"{}\" encryptedVerifierHashInput=\"{}\" encryptedVerifierHashValue=\"{}\" encryptedKeyValue=\"{}\"/>\
|
||||
</keyEncryptor></keyEncryptors></encryption>",
|
||||
b64(&key_data_salt),
|
||||
b64(&enc_hmac_key),
|
||||
b64(&enc_hmac_value),
|
||||
b64(&password_salt),
|
||||
b64(&enc_verifier_input),
|
||||
b64(&enc_verifier_hash),
|
||||
b64(&enc_key_value),
|
||||
);
|
||||
// EncryptionInfo (§2.3.4.10): version 4.4, reserved 0x40, the XML.
|
||||
let mut info = vec![4, 0, 4, 0, 0x40, 0, 0, 0];
|
||||
info.extend_from_slice(xml.as_bytes());
|
||||
|
||||
let io = |e: std::io::Error| bad(format!("couldn't write the compound file ({e})"));
|
||||
let mut comp = cfb::CompoundFile::create_with_version(cfb::Version::V3, Cursor::new(Vec::new())).map_err(io)?;
|
||||
for (path, data) in [
|
||||
("/\u{6}DataSpaces/Version", data_space_version()),
|
||||
("/\u{6}DataSpaces/DataSpaceMap", data_space_map()),
|
||||
("/\u{6}DataSpaces/DataSpaceInfo/StrongEncryptionDataSpace", data_space_definition()),
|
||||
("/\u{6}DataSpaces/TransformInfo/StrongEncryptionTransform/\u{6}Primary", primary_transform()),
|
||||
("/EncryptionInfo", info),
|
||||
("/EncryptedPackage", stream),
|
||||
] {
|
||||
if let Some((parent, _)) = path.rsplit_once('/')
|
||||
&& !parent.is_empty()
|
||||
{
|
||||
comp.create_storage_all(parent).map_err(io)?;
|
||||
}
|
||||
comp.create_stream(path).map_err(io)?.write_all(&data).map_err(io)?;
|
||||
}
|
||||
comp.flush().map_err(io)?;
|
||||
Ok(comp.into_inner().into_inner())
|
||||
}
|
||||
|
||||
/// A package encrypted with the *Standard* method (§2.3.4.5–§2.3.4.9: AES-ECB, SHA-1, 50,000
|
||||
/// spins), as Word 2007 wrote it, for tests only: WordCraft saves with the agile method
|
||||
/// ([`encrypt`]). `key_bits` is 128, 192 or 256. The compound file holds just the two streams
|
||||
/// [`decrypt`] reads.
|
||||
#[doc(hidden)]
|
||||
pub fn encrypt_standard_for_tests(package: &[u8], password: &str, key_bits: u32) -> Result<Vec<u8>, DocxError> {
|
||||
check_password(password)?;
|
||||
let alg_id = match key_bits {
|
||||
128 => ALG_AES_128,
|
||||
192 => ALG_AES_192,
|
||||
256 => ALG_AES_256,
|
||||
_ => return Err(bad(format!("unsupported AES key size {key_bits}"))),
|
||||
};
|
||||
let salt = random(SALT)?;
|
||||
let verifier = random(SALT)?;
|
||||
let key = standard_key(&salt, password, key_bits as usize / 8);
|
||||
let mut header = Vec::new();
|
||||
for v in [STANDARD_FLAGS, 0, alg_id, ALG_SHA1, key_bits, PROV_RSA_AES, 0, 0] {
|
||||
header.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
// CSPName: an empty null-terminated UTF-16 string.
|
||||
header.extend_from_slice(&[0, 0]);
|
||||
let mut info = vec![3, 0, 2, 0];
|
||||
info.extend_from_slice(&STANDARD_FLAGS.to_le_bytes());
|
||||
info.extend_from_slice(&(header.len() as u32).to_le_bytes());
|
||||
info.extend_from_slice(&header);
|
||||
info.extend_from_slice(&(SALT as u32).to_le_bytes());
|
||||
info.extend_from_slice(&salt);
|
||||
info.extend_from_slice(&ecb(&key, &verifier, true)?);
|
||||
info.extend_from_slice(&(SHA1_SIZE as u32).to_le_bytes());
|
||||
info.extend_from_slice(&ecb(&key, &padded(&Hash::Sha1.hash(&[&verifier])), true)?);
|
||||
let mut stream = (package.len() as u64).to_le_bytes().to_vec();
|
||||
stream.extend_from_slice(&ecb(&key, &padded(package), true)?);
|
||||
let io = |e: std::io::Error| bad(format!("couldn't write the compound file ({e})"));
|
||||
let mut comp = cfb::CompoundFile::create_with_version(cfb::Version::V3, Cursor::new(Vec::new())).map_err(io)?;
|
||||
comp.create_stream("/EncryptionInfo").map_err(io)?.write_all(&info).map_err(io)?;
|
||||
comp.create_stream("/EncryptedPackage").map_err(io)?.write_all(&stream).map_err(io)?;
|
||||
comp.flush().map_err(io)?;
|
||||
Ok(comp.into_inner().into_inner())
|
||||
}
|
||||
|
||||
/// Refuse a password Word wouldn't accept: empty, or longer than [`MAX_PASSWORD_CHARS`].
|
||||
pub fn check_password(password: &str) -> Result<(), DocxError> {
|
||||
if password.is_empty() {
|
||||
return Err(bad("the password is empty"));
|
||||
}
|
||||
if password.chars().count() > MAX_PASSWORD_CHARS {
|
||||
return Err(bad(format!("passwords are at most {MAX_PASSWORD_CHARS} characters")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Restrict Editing's password (`w:documentProtection`, ECMA-376 Part 1 §17.15.1.29): the
|
||||
/// attributes that store its hash — SHA-512 of a random 16-byte salt and the password as UTF-16LE,
|
||||
/// hashed again `spin_count` times with the iteration number appended ([MS-OFFCRYPTO] §2.4.2.4,
|
||||
/// the ISO write-protection method). Protection isn't encryption: the document stays readable,
|
||||
/// the password only guards turning protection off.
|
||||
pub fn protection_hash(password: &str, spin_count: u32) -> Result<Vec<(String, String)>, DocxError> {
|
||||
check_password(password)?;
|
||||
let spin_count = spin_count.min(MAX_SPIN_COUNT);
|
||||
let salt = random(SALT)?;
|
||||
let hash = iterate_appended(Hash::Sha512, &salt, password, spin_count);
|
||||
let b64 = |b: &[u8]| base64::engine::general_purpose::STANDARD.encode(b);
|
||||
Ok(vec![
|
||||
("w:algorithmName".into(), "SHA-512".into()),
|
||||
("w:hashValue".into(), b64(&hash)),
|
||||
("w:saltValue".into(), b64(&salt)),
|
||||
("w:spinCount".into(), spin_count.to_string()),
|
||||
])
|
||||
}
|
||||
|
||||
/// What checking a Restrict Editing password found ([`check_protection_password`]).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PasswordCheck {
|
||||
/// The protection has no password: anyone may stop it.
|
||||
NoPassword,
|
||||
/// The password matches the stored hash.
|
||||
Correct,
|
||||
/// The password doesn't match.
|
||||
Incorrect,
|
||||
/// The hash uses an algorithm or form WordCraft can't check, or is damaged. The protection
|
||||
/// must stay on: guessing would let any password through.
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
/// Does `password` match a Restrict Editing hash (attributes as [`protection_hash`] makes them, or
|
||||
/// as read from a file)? Two forms are checked:
|
||||
///
|
||||
/// - `w:algorithmName` / `w:hashValue` / `w:saltValue` / `w:spinCount`: the password itself is
|
||||
/// hashed ([`iterate_appended`]).
|
||||
/// - `w:cryptAlgorithmSid` / `w:hash` / `w:salt` / `w:cryptSpinCount` (ECMA-376 Part 4 §14.8.1):
|
||||
/// the password first becomes Word's legacy 32-bit key ([`legacy_key`]), whose bytes, reversed
|
||||
/// and written as an upper-case hex string, are hashed the same way.
|
||||
///
|
||||
/// Anything else — an algorithm other than SHA-1/SHA-256/SHA-384/SHA-512, an application-defined
|
||||
/// algorithm (`w:algIdExt`), a class other than `hash`, a spin count above the 10,000,000 Word
|
||||
/// accepts, damaged values — is [`PasswordCheck::Unsupported`], never a match.
|
||||
pub fn check_protection_password(attrs: &[(String, String)], password: &str) -> PasswordCheck {
|
||||
let get = |k: &str| attrs.iter().find(|(n, _)| n == k).map(|(_, v)| v.trim());
|
||||
if get("w:hashValue").is_some() {
|
||||
let Some(hash) = get("w:algorithmName").and_then(Hash::parse) else { return PasswordCheck::Unsupported };
|
||||
return verify(hash, get("w:hashValue"), get("w:saltValue"), get("w:spinCount"), password);
|
||||
}
|
||||
if get("w:hash").is_none() {
|
||||
// §17.15.1.29: without a hash no password is associated with the protection.
|
||||
return PasswordCheck::NoPassword;
|
||||
}
|
||||
let class_ok = get("w:cryptAlgorithmClass").is_none_or(|c| c == "hash");
|
||||
let type_ok = get("w:cryptAlgorithmType").is_none_or(|t| t == "typeAny");
|
||||
if !class_ok || !type_ok || get("w:algIdExt").is_some() {
|
||||
return PasswordCheck::Unsupported;
|
||||
}
|
||||
// Part 4 §14.8.1 lists 1 MD2, 2 MD4, 3 MD5, 4 SHA-1, 12 SHA-256, 13 SHA-384, 14 SHA-512.
|
||||
let hash = match get("w:cryptAlgorithmSid").and_then(|v| v.parse::<u32>().ok()) {
|
||||
Some(4) => Hash::Sha1,
|
||||
Some(12) => Hash::Sha256,
|
||||
Some(13) => Hash::Sha384,
|
||||
Some(14) => Hash::Sha512,
|
||||
_ => return PasswordCheck::Unsupported,
|
||||
};
|
||||
verify(hash, get("w:hash"), get("w:salt"), get("w:cryptSpinCount"), &legacy_key_hex(password))
|
||||
}
|
||||
|
||||
/// Hash `input` with the stored salt and spin count and compare with the stored hash. Damaged or
|
||||
/// out-of-range values are [`PasswordCheck::Unsupported`].
|
||||
fn verify(hash: Hash, want: Option<&str>, salt: Option<&str>, spin_count: Option<&str>, input: &str) -> PasswordCheck {
|
||||
let dec = |v: &str| base64::engine::general_purpose::STANDARD.decode(v).ok();
|
||||
let Some(want) = want.and_then(dec) else { return PasswordCheck::Unsupported };
|
||||
// §17.15.1.29: without a salt nothing is prepended.
|
||||
let salt = match salt {
|
||||
Some(v) => match dec(v) {
|
||||
Some(s) => s,
|
||||
None => return PasswordCheck::Unsupported,
|
||||
},
|
||||
None => Vec::new(),
|
||||
};
|
||||
// Word accepts spin counts from 0 to 10,000,000 ([MS-OI29500] 2.1.402 d); a missing one is 0.
|
||||
let spin_count = match spin_count.map(str::parse::<u32>) {
|
||||
None => 0,
|
||||
Some(Ok(n)) if n <= MAX_SPIN_COUNT => n,
|
||||
Some(_) => return PasswordCheck::Unsupported,
|
||||
};
|
||||
if want.len() != hash.size() || salt.len() > 1024 {
|
||||
return PasswordCheck::Unsupported;
|
||||
}
|
||||
if same(&iterate_appended(hash, &salt, input, spin_count), &want) { PasswordCheck::Correct } else { PasswordCheck::Incorrect }
|
||||
}
|
||||
|
||||
/// The initial value of the legacy key's high-order word, by password length 1–15 (ECMA-376
|
||||
/// Part 4 §14.8.1, "Initial code array").
|
||||
const LEGACY_INITIAL_CODE: [u16; 15] =
|
||||
[0xE1F0, 0x1D0F, 0xCC9C, 0x84C0, 0x110C, 0x0E10, 0xF1CE, 0x313E, 0x1872, 0xE139, 0xD40F, 0x84F9, 0x280C, 0xA96A, 0x4EC3];
|
||||
|
||||
/// The legacy key's encryption matrix (Part 4 §14.8.1): one row per character position, rows
|
||||
/// "Last-14" to "Last", one column per bit 0–6. The password's last character uses the last row.
|
||||
const LEGACY_MATRIX: [[u16; 7]; 15] = [
|
||||
[0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09],
|
||||
[0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF],
|
||||
[0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0],
|
||||
[0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40],
|
||||
[0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5],
|
||||
[0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A],
|
||||
[0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9],
|
||||
[0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0],
|
||||
[0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC],
|
||||
[0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10],
|
||||
[0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168],
|
||||
[0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C],
|
||||
[0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD],
|
||||
[0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC],
|
||||
[0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4],
|
||||
];
|
||||
|
||||
/// Word's legacy 32-bit password key (ECMA-376 Part 4 §14.8.1): the password as UTF-16 without a
|
||||
/// leading BOM, cut to 15 characters, each character reduced to one byte (its low byte, or its
|
||||
/// high byte when the low byte is 0); the high-order word comes from the initial code array and
|
||||
/// the encryption matrix, the low-order word from rotating and XOR-ing the bytes backwards.
|
||||
fn legacy_key(password: &str) -> u32 {
|
||||
let mut units = password.encode_utf16().peekable();
|
||||
units.next_if_eq(&0xFEFF);
|
||||
let bytes: Zeroizing<Vec<u8>> =
|
||||
Zeroizing::new(units.take(LEGACY_INITIAL_CODE.len()).map(|u| if u & 0xFF != 0 { (u & 0xFF) as u8 } else { (u >> 8) as u8 }).collect());
|
||||
let Some(&initial) = bytes.len().checked_sub(1).and_then(|i| LEGACY_INITIAL_CODE.get(i)) else { return 0 };
|
||||
let mut high = initial;
|
||||
let first_row = LEGACY_MATRIX.len() - bytes.len();
|
||||
for (row, &c) in LEGACY_MATRIX.iter().skip(first_row).zip(bytes.iter()) {
|
||||
for (bit, &code) in row.iter().enumerate() {
|
||||
if c & (1 << bit) != 0 {
|
||||
high ^= code;
|
||||
}
|
||||
}
|
||||
}
|
||||
let rotate = |w: u16| ((w >> 14) & 0x0001) | ((w << 1) & 0x7FFF);
|
||||
let mut low = 0u16;
|
||||
for &c in bytes.iter().rev() {
|
||||
low = rotate(low) ^ u16::from(c);
|
||||
}
|
||||
// `bytes` holds at most 15 values, so its length fits a word.
|
||||
low = rotate(low) ^ (bytes.len() as u16) ^ 0xCE4B;
|
||||
(u32::from(high) << 16) | u32::from(low)
|
||||
}
|
||||
|
||||
/// What the legacy form hashes: the key's bytes in reversed order (Part 4 §14.8.1: 0x64CEED7E
|
||||
/// becomes 7EEDCE64), as an upper-case hex string — hashed as UTF-16LE like a password, as Word
|
||||
/// does ([MS-OE376] 2.1.410 a).
|
||||
fn legacy_key_hex(password: &str) -> Zeroizing<String> {
|
||||
let key = legacy_key(password);
|
||||
Zeroizing::new(key.to_le_bytes().iter().map(|b| format!("{b:02X}")).collect())
|
||||
}
|
||||
|
||||
/// The ISO write-protection hash ([MS-OFFCRYPTO] §2.4.2.4): H0 = H(salt + password as UTF-16LE),
|
||||
/// Hn = H(Hn-1 + iterator) — the iterator after the hash, unlike encryption's [`iterate`].
|
||||
fn iterate_appended(hash: Hash, salt: &[u8], password: &str, spin_count: u32) -> Zeroizing<Vec<u8>> {
|
||||
let pw: Zeroizing<Vec<u8>> = Zeroizing::new(password.encode_utf16().flat_map(u16::to_le_bytes).collect());
|
||||
let mut h = Zeroizing::new(hash.hash(&[salt, &pw]));
|
||||
for i in 0..spin_count.min(MAX_SPIN_COUNT) {
|
||||
*h = hash.hash(&[&h, &i.to_le_bytes()]);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
fn bad(msg: impl Into<String>) -> DocxError {
|
||||
DocxError::Encryption(msg.into())
|
||||
}
|
||||
|
||||
/// A root-level stream, at most `cap` bytes.
|
||||
fn stream<F: Read + std::io::Seek>(comp: &mut cfb::CompoundFile<F>, path: &str, cap: u64) -> Result<Vec<u8>, DocxError> {
|
||||
let s = comp.open_stream(path).map_err(|e| bad(format!("can't read {} ({e})", path.trim_start_matches('/'))))?;
|
||||
let mut v = Vec::new();
|
||||
s.take(cap.saturating_add(1)).read_to_end(&mut v).map_err(|e| bad(format!("can't read {} ({e})", path.trim_start_matches('/'))))?;
|
||||
if v.len() as u64 > cap {
|
||||
return Err(DocxError::Limit(format!("{} is too large", path.trim_start_matches('/'))));
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// The hash algorithms agile encryption names that WordCraft supports.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Hash {
|
||||
Sha1,
|
||||
Sha256,
|
||||
Sha384,
|
||||
Sha512,
|
||||
}
|
||||
|
||||
impl Hash {
|
||||
fn parse(s: &str) -> Option<Hash> {
|
||||
match s.to_ascii_uppercase().replace('-', "").as_str() {
|
||||
"SHA1" => Some(Hash::Sha1),
|
||||
"SHA256" => Some(Hash::Sha256),
|
||||
"SHA384" => Some(Hash::Sha384),
|
||||
"SHA512" => Some(Hash::Sha512),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Hash::Sha1 => "SHA-1",
|
||||
Hash::Sha256 => "SHA256",
|
||||
Hash::Sha384 => "SHA384",
|
||||
Hash::Sha512 => "SHA512",
|
||||
}
|
||||
}
|
||||
|
||||
fn size(self) -> usize {
|
||||
match self {
|
||||
Hash::Sha1 => 20,
|
||||
Hash::Sha256 => 32,
|
||||
Hash::Sha384 => 48,
|
||||
Hash::Sha512 => 64,
|
||||
}
|
||||
}
|
||||
|
||||
/// The hash of the concatenated `parts`.
|
||||
fn hash(self, parts: &[&[u8]]) -> Vec<u8> {
|
||||
fn go<D: Digest>(parts: &[&[u8]]) -> Vec<u8> {
|
||||
let mut d = D::new();
|
||||
for p in parts {
|
||||
d.update(p);
|
||||
}
|
||||
d.finalize().to_vec()
|
||||
}
|
||||
match self {
|
||||
Hash::Sha1 => go::<sha1::Sha1>(parts),
|
||||
Hash::Sha256 => go::<sha2::Sha256>(parts),
|
||||
Hash::Sha384 => go::<sha2::Sha384>(parts),
|
||||
Hash::Sha512 => go::<sha2::Sha512>(parts),
|
||||
}
|
||||
}
|
||||
|
||||
fn hmac(self, key: &[u8], msg: &[u8]) -> Result<Vec<u8>, DocxError> {
|
||||
fn go<M: Mac + KeyInit>(key: &[u8], msg: &[u8]) -> Result<Vec<u8>, DocxError> {
|
||||
let mut m = <M as KeyInit>::new_from_slice(key).map_err(|_| bad("bad integrity key"))?;
|
||||
m.update(msg);
|
||||
Ok(m.finalize().into_bytes().to_vec())
|
||||
}
|
||||
match self {
|
||||
Hash::Sha1 => go::<hmac::Hmac<sha1::Sha1>>(key, msg),
|
||||
Hash::Sha256 => go::<hmac::Hmac<sha2::Sha256>>(key, msg),
|
||||
Hash::Sha384 => go::<hmac::Hmac<sha2::Sha384>>(key, msg),
|
||||
Hash::Sha512 => go::<hmac::Hmac<sha2::Sha512>>(key, msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The password hash after `spin_count` iterations (§2.3.4.11): H0 = H(salt + password as
|
||||
/// UTF-16LE), Hn = H(iterator + Hn-1).
|
||||
fn iterate(hash: Hash, salt: &[u8], password: &str, spin_count: u32) -> Zeroizing<Vec<u8>> {
|
||||
let pw: Zeroizing<Vec<u8>> = Zeroizing::new(password.encode_utf16().flat_map(u16::to_le_bytes).collect());
|
||||
let mut h = Zeroizing::new(hash.hash(&[salt, &pw]));
|
||||
for i in 0..spin_count.min(MAX_SPIN_COUNT) {
|
||||
*h = hash.hash(&[&i.to_le_bytes(), &h]);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
/// A key from the iterated password hash and a block key (§2.3.4.11): H(Hn + blockKey),
|
||||
/// truncated to `len`, or padded with 0x36.
|
||||
fn derive_key(hash: Hash, hn: &[u8], block_key: &[u8], len: usize) -> Zeroizing<Vec<u8>> {
|
||||
let mut k = Zeroizing::new(hash.hash(&[hn, block_key]));
|
||||
k.resize(len, 0x36);
|
||||
k
|
||||
}
|
||||
|
||||
/// Standard encryption's key (§2.3.4.7): the SHA-1 password hash after 50,000 iterations, hashed
|
||||
/// with block number 0; then X1 = SHA-1 of that hash XOR-ed into 64 bytes of 0x36, X2 the same
|
||||
/// with 0x5C, and the key is the first `len` bytes of X1 + X2.
|
||||
fn standard_key(salt: &[u8], password: &str, len: usize) -> Zeroizing<Vec<u8>> {
|
||||
let hn = iterate(Hash::Sha1, salt, password, STANDARD_SPIN_COUNT);
|
||||
let hfinal = Zeroizing::new(Hash::Sha1.hash(&[&hn, &0u32.to_le_bytes()]));
|
||||
let x = |fill: u8| {
|
||||
let mut buf = Zeroizing::new([fill; 64]);
|
||||
for (b, h) in buf.iter_mut().zip(hfinal.iter()) {
|
||||
*b ^= h;
|
||||
}
|
||||
Zeroizing::new(Hash::Sha1.hash(&[&buf[..]]))
|
||||
};
|
||||
let mut key = Zeroizing::new(x(0x36).to_vec());
|
||||
key.extend_from_slice(&x(0x5C));
|
||||
key.truncate(len);
|
||||
key
|
||||
}
|
||||
|
||||
/// AES-ECB without padding (Standard encryption, §2.3.4.7–§2.3.4.9): `data` must be whole blocks.
|
||||
fn ecb(key: &[u8], data: &[u8], encrypt: bool) -> Result<Vec<u8>, DocxError> {
|
||||
let aes = Aes::new(key)?;
|
||||
let (blocks, rest) = data.as_chunks::<BLOCK>();
|
||||
if !rest.is_empty() {
|
||||
return Err(bad("encrypted data isn't a whole number of blocks"));
|
||||
}
|
||||
let mut out = Vec::with_capacity(data.len());
|
||||
for b in blocks {
|
||||
let mut x = aes::Block::from(*b);
|
||||
if encrypt {
|
||||
aes.encrypt(&mut x);
|
||||
} else {
|
||||
aes.decrypt(&mut x);
|
||||
}
|
||||
out.extend_from_slice(&x);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// An initialization vector (§2.3.4.12): H(salt + blockKey), or the salt itself, truncated to
|
||||
/// the block size or padded with 0x36.
|
||||
fn make_iv(hash: Hash, salt: &[u8], block_key: Option<&[u8]>, block_size: usize) -> Vec<u8> {
|
||||
let mut iv = match block_key {
|
||||
Some(b) => hash.hash(&[salt, b]),
|
||||
None => salt.to_vec(),
|
||||
};
|
||||
iv.resize(block_size, 0x36);
|
||||
iv
|
||||
}
|
||||
|
||||
/// `data` zero-padded to a whole number of blocks.
|
||||
fn padded(data: &[u8]) -> Zeroizing<Vec<u8>> {
|
||||
let mut v = Zeroizing::new(data.to_vec());
|
||||
v.resize(data.len().div_ceil(BLOCK).saturating_mul(BLOCK), 0);
|
||||
v
|
||||
}
|
||||
|
||||
fn random(n: usize) -> Result<Zeroizing<Vec<u8>>, DocxError> {
|
||||
let mut v = Zeroizing::new(vec![0u8; n]);
|
||||
getrandom::fill(&mut v).map_err(|e| bad(format!("no secure random numbers available ({e})")))?;
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Compare without stopping at the first difference.
|
||||
fn same(a: &[u8], b: &[u8]) -> bool {
|
||||
a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
|
||||
}
|
||||
|
||||
/// AES with a 128-, 192- or 256-bit key.
|
||||
enum Aes {
|
||||
A128(aes::Aes128),
|
||||
A192(aes::Aes192),
|
||||
A256(aes::Aes256),
|
||||
}
|
||||
|
||||
impl Aes {
|
||||
fn new(key: &[u8]) -> Result<Aes, DocxError> {
|
||||
let err = |_| bad("bad AES key");
|
||||
Ok(match key.len() {
|
||||
16 => Aes::A128(aes::Aes128::new_from_slice(key).map_err(err)?),
|
||||
24 => Aes::A192(aes::Aes192::new_from_slice(key).map_err(err)?),
|
||||
32 => Aes::A256(aes::Aes256::new_from_slice(key).map_err(err)?),
|
||||
n => return Err(bad(format!("unsupported AES key length {} bits", n * 8))),
|
||||
})
|
||||
}
|
||||
|
||||
fn encrypt(&self, b: &mut aes::Block) {
|
||||
match self {
|
||||
Aes::A128(c) => c.encrypt_block(b),
|
||||
Aes::A192(c) => c.encrypt_block(b),
|
||||
Aes::A256(c) => c.encrypt_block(b),
|
||||
}
|
||||
}
|
||||
|
||||
fn decrypt(&self, b: &mut aes::Block) {
|
||||
match self {
|
||||
Aes::A128(c) => c.decrypt_block(b),
|
||||
Aes::A192(c) => c.decrypt_block(b),
|
||||
Aes::A256(c) => c.decrypt_block(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// AES-CBC without padding: `data` must be whole blocks.
|
||||
fn cbc_encrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, DocxError> {
|
||||
let aes = Aes::new(key)?;
|
||||
let (blocks, rest) = data.as_chunks::<BLOCK>();
|
||||
let mut prev: [u8; BLOCK] = iv.try_into().map_err(|_| bad("bad initialization vector"))?;
|
||||
if !rest.is_empty() {
|
||||
return Err(bad("encrypted data isn't a whole number of blocks"));
|
||||
}
|
||||
let mut out = Vec::with_capacity(data.len());
|
||||
for b in blocks {
|
||||
let mut x = aes::Block::default();
|
||||
for ((o, p), v) in x.iter_mut().zip(b).zip(&prev) {
|
||||
*o = p ^ v;
|
||||
}
|
||||
aes.encrypt(&mut x);
|
||||
prev.copy_from_slice(&x);
|
||||
out.extend_from_slice(&x);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// AES-CBC decryption without padding.
|
||||
fn cbc_decrypt(key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, DocxError> {
|
||||
let aes = Aes::new(key)?;
|
||||
let (blocks, rest) = data.as_chunks::<BLOCK>();
|
||||
if !rest.is_empty() {
|
||||
return Err(bad("encrypted data isn't a whole number of blocks"));
|
||||
}
|
||||
let mut prev: [u8; BLOCK] = iv.try_into().map_err(|_| bad("bad initialization vector"))?;
|
||||
let mut out = Vec::with_capacity(data.len());
|
||||
for b in blocks {
|
||||
let mut x = aes::Block::default();
|
||||
x.copy_from_slice(b);
|
||||
aes.decrypt(&mut x);
|
||||
for (o, v) in x.iter_mut().zip(&prev) {
|
||||
*o ^= v;
|
||||
}
|
||||
prev = *b;
|
||||
out.extend_from_slice(&x);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// What `EncryptionInfo` says.
|
||||
struct Info {
|
||||
hash: Hash,
|
||||
salt: Vec<u8>,
|
||||
block_size: usize,
|
||||
key_bytes: usize,
|
||||
/// encryptedHmacKey, encryptedHmacValue.
|
||||
integrity: (Vec<u8>, Vec<u8>),
|
||||
password: PasswordEncryptor,
|
||||
}
|
||||
|
||||
struct PasswordEncryptor {
|
||||
hash: Hash,
|
||||
salt: Vec<u8>,
|
||||
salt_size: usize,
|
||||
block_size: usize,
|
||||
key_bytes: usize,
|
||||
spin_count: u32,
|
||||
verifier_input: Vec<u8>,
|
||||
verifier_hash: Vec<u8>,
|
||||
key_value: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Cipher parameters shared by `keyData` and `encryptedKey`.
|
||||
struct Params {
|
||||
hash: Hash,
|
||||
salt: Vec<u8>,
|
||||
salt_size: usize,
|
||||
block_size: usize,
|
||||
key_bytes: usize,
|
||||
}
|
||||
|
||||
fn parse_info(info: &[u8]) -> Result<Info, DocxError> {
|
||||
let (major, minor) = match info.get(..4) {
|
||||
Some([a, b, c, d]) => (u16::from_le_bytes([*a, *b]), u16::from_le_bytes([*c, *d])),
|
||||
_ => return Err(bad("EncryptionInfo is truncated")),
|
||||
};
|
||||
match (major, minor) {
|
||||
(4, 4) => {}
|
||||
(3 | 4, 3) => return Err(bad("this file uses extensible encryption, which WordCraft can't open")),
|
||||
_ => return Err(bad(format!("unknown encryption version {major}.{minor}"))),
|
||||
}
|
||||
let root = xml::parse(info.get(8..).unwrap_or(&[]))?;
|
||||
if root.local() != "encryption" {
|
||||
return Err(bad("EncryptionInfo has no <encryption> element"));
|
||||
}
|
||||
let key_data = root.els().find(|e| e.local() == "keyData").ok_or_else(|| bad("EncryptionInfo has no keyData"))?;
|
||||
let kd = params(key_data)?;
|
||||
// The integrity check is mandatory (§2.3.4.14): a file is not trusted to opt out of it.
|
||||
let di = root
|
||||
.els()
|
||||
.find(|e| e.local() == "dataIntegrity")
|
||||
.ok_or_else(|| bad("EncryptionInfo has no dataIntegrity, so the document cannot be checked for tampering"))?;
|
||||
let integrity = (b64_attr(di, "encryptedHmacKey")?, b64_attr(di, "encryptedHmacValue")?);
|
||||
// The password key encryptor is the `encryptedKey` with a spin count (a certificate key
|
||||
// encryptor has none).
|
||||
let encryptors = root.els().find(|e| e.local() == "keyEncryptors").ok_or_else(|| bad("EncryptionInfo has no keyEncryptors"))?;
|
||||
let ek = encryptors
|
||||
.els()
|
||||
.filter(|e| e.local() == "keyEncryptor")
|
||||
.flat_map(|e| e.els())
|
||||
.find(|e| e.local() == "encryptedKey" && e.attr("spinCount").is_some())
|
||||
.ok_or_else(|| bad("this file isn't protected with a password (only certificate encryption is listed)"))?;
|
||||
let pe = params(ek)?;
|
||||
let spin_count: u32 = ek.attr("spinCount").and_then(|s| s.trim().parse().ok()).ok_or_else(|| bad("bad spinCount"))?;
|
||||
if spin_count > MAX_SPIN_COUNT {
|
||||
return Err(bad(format!("spinCount {spin_count} is over the allowed maximum")));
|
||||
}
|
||||
Ok(Info {
|
||||
hash: kd.hash,
|
||||
salt: kd.salt,
|
||||
block_size: kd.block_size,
|
||||
key_bytes: kd.key_bytes,
|
||||
integrity,
|
||||
password: PasswordEncryptor {
|
||||
hash: pe.hash,
|
||||
salt: pe.salt,
|
||||
salt_size: pe.salt_size,
|
||||
block_size: pe.block_size,
|
||||
key_bytes: pe.key_bytes,
|
||||
spin_count,
|
||||
verifier_input: b64_attr(ek, "encryptedVerifierHashInput")?,
|
||||
verifier_hash: b64_attr(ek, "encryptedVerifierHashValue")?,
|
||||
key_value: b64_attr(ek, "encryptedKeyValue")?,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn params(e: &xml::El) -> Result<Params, DocxError> {
|
||||
let num = |k: &str| -> Result<usize, DocxError> { e.attr(k).and_then(|s| s.trim().parse::<usize>().ok()).ok_or_else(|| bad(format!("bad {k}"))) };
|
||||
let cipher = e.attr("cipherAlgorithm").unwrap_or("");
|
||||
if !cipher.eq_ignore_ascii_case("AES") {
|
||||
return Err(bad(format!("the {cipher:?} cipher isn't supported")));
|
||||
}
|
||||
let chaining = e.attr("cipherChaining").unwrap_or("");
|
||||
if chaining != "ChainingModeCBC" {
|
||||
return Err(bad(format!("the {chaining:?} chaining mode isn't supported")));
|
||||
}
|
||||
let name = e.attr("hashAlgorithm").unwrap_or("");
|
||||
let hash = Hash::parse(name).ok_or_else(|| bad(format!("the {name:?} hash algorithm isn't supported")))?;
|
||||
if num("hashSize")? != hash.size() {
|
||||
return Err(bad("hashSize doesn't match the hash algorithm"));
|
||||
}
|
||||
let block_size = num("blockSize")?;
|
||||
if block_size != BLOCK {
|
||||
return Err(bad(format!("AES needs a block size of {BLOCK}, not {block_size}")));
|
||||
}
|
||||
let key_bits = num("keyBits")?;
|
||||
if !matches!(key_bits, 128 | 192 | 256) {
|
||||
return Err(bad(format!("unsupported AES key size {key_bits}")));
|
||||
}
|
||||
let salt = b64_attr(e, "saltValue")?;
|
||||
let salt_size = num("saltSize")?;
|
||||
if salt.is_empty() || salt.len() != salt_size {
|
||||
return Err(bad("the salt doesn't match saltSize"));
|
||||
}
|
||||
Ok(Params { hash, salt, salt_size, block_size, key_bytes: key_bits / 8 })
|
||||
}
|
||||
|
||||
fn b64_attr(e: &xml::El, k: &str) -> Result<Vec<u8>, DocxError> {
|
||||
let s = e.attr(k).ok_or_else(|| bad(format!("{k} is missing")))?;
|
||||
let s: String = s.chars().filter(|c| !c.is_ascii_whitespace()).collect();
|
||||
base64::engine::general_purpose::STANDARD.decode(s).map_err(|_| bad(format!("{k} isn't valid base64")))
|
||||
}
|
||||
|
||||
/// A UNICODE-LP-P4 string (§2.1.2): byte length, UTF-16LE, zero-padded to 4 bytes.
|
||||
fn lp_p4(out: &mut Vec<u8>, s: &str) {
|
||||
let units: Vec<u8> = s.encode_utf16().flat_map(u16::to_le_bytes).collect();
|
||||
out.extend_from_slice(&(units.len() as u32).to_le_bytes());
|
||||
out.extend_from_slice(&units);
|
||||
while !out.len().is_multiple_of(4) {
|
||||
out.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// `\x06DataSpaces\Version` (§2.1.5).
|
||||
fn data_space_version() -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
lp_p4(&mut v, "Microsoft.Container.DataSpaces");
|
||||
for _ in 0..3 {
|
||||
v.extend_from_slice(&[1, 0, 0, 0]);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// `\x06DataSpaces\DataSpaceMap` (§2.1.6, §2.3.4.1): EncryptedPackage → StrongEncryptionDataSpace.
|
||||
fn data_space_map() -> Vec<u8> {
|
||||
let mut entry = Vec::new();
|
||||
entry.extend_from_slice(&1u32.to_le_bytes());
|
||||
entry.extend_from_slice(&0u32.to_le_bytes());
|
||||
lp_p4(&mut entry, "EncryptedPackage");
|
||||
lp_p4(&mut entry, "StrongEncryptionDataSpace");
|
||||
let mut v = Vec::new();
|
||||
v.extend_from_slice(&8u32.to_le_bytes());
|
||||
v.extend_from_slice(&1u32.to_le_bytes());
|
||||
v.extend_from_slice(&((entry.len() + 4) as u32).to_le_bytes());
|
||||
v.extend_from_slice(&entry);
|
||||
v
|
||||
}
|
||||
|
||||
/// `\x06DataSpaces\DataSpaceInfo\StrongEncryptionDataSpace` (§2.1.7, §2.3.4.2).
|
||||
fn data_space_definition() -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
v.extend_from_slice(&8u32.to_le_bytes());
|
||||
v.extend_from_slice(&1u32.to_le_bytes());
|
||||
lp_p4(&mut v, "StrongEncryptionTransform");
|
||||
v
|
||||
}
|
||||
|
||||
/// `\x06DataSpaces\TransformInfo\StrongEncryptionTransform\x06Primary` (§2.1.8, §2.1.9,
|
||||
/// §2.3.4.3): the encryption transform, with a null EncryptionName as agile encryption requires.
|
||||
fn primary_transform() -> Vec<u8> {
|
||||
let mut id = Vec::new();
|
||||
lp_p4(&mut id, "{FF9A3F03-56EF-4613-BDD5-5A41C1D07246}");
|
||||
let mut v = Vec::new();
|
||||
// TransformLength: the bytes before TransformName.
|
||||
v.extend_from_slice(&((8 + id.len()) as u32).to_le_bytes());
|
||||
v.extend_from_slice(&1u32.to_le_bytes());
|
||||
v.extend_from_slice(&id);
|
||||
lp_p4(&mut v, "Microsoft.Container.EncryptionTransform");
|
||||
for _ in 0..3 {
|
||||
v.extend_from_slice(&[1, 0, 0, 0]);
|
||||
}
|
||||
// EncryptionTransformInfo: null name, block size, cipher mode 0, reserved 4.
|
||||
v.extend_from_slice(&0u32.to_le_bytes());
|
||||
v.extend_from_slice(&(BLOCK as u32).to_le_bytes());
|
||||
v.extend_from_slice(&0u32.to_le_bytes());
|
||||
v.extend_from_slice(&4u32.to_le_bytes());
|
||||
v
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -1,393 +0,0 @@
|
||||
#[test]
|
||||
fn protection_password_round_trip() {
|
||||
use super::PasswordCheck::*;
|
||||
let attrs = super::protection_hash("s3cret", 1000).unwrap();
|
||||
assert_eq!(super::check_protection_password(&attrs, "s3cret"), Correct);
|
||||
assert_eq!(super::check_protection_password(&attrs, "S3cret"), Incorrect);
|
||||
// Damaged attributes are refused, never accepted.
|
||||
let mut bad = attrs.clone();
|
||||
bad.retain(|(k, _)| k != "w:hashValue");
|
||||
bad.push(("w:hashValue".into(), "not base64!".into()));
|
||||
assert_eq!(super::check_protection_password(&bad, "s3cret"), Unsupported);
|
||||
let mut odd = attrs.clone();
|
||||
odd.retain(|(k, _)| k != "w:algorithmName");
|
||||
odd.push(("w:algorithmName".into(), "WHIRLPOOL".into()));
|
||||
assert_eq!(super::check_protection_password(&odd, "s3cret"), Unsupported);
|
||||
assert_eq!(super::check_protection_password(&[("w:cryptProviderType".into(), "rsaAES".into())], "x"), NoPassword);
|
||||
}
|
||||
|
||||
/// The worked example of ECMA-376 Part 4 §14.8.1: "Example" gives the key 0x64CEED7E, hashed
|
||||
/// as "7EEDCE64".
|
||||
#[test]
|
||||
fn legacy_key_matches_the_specification_example() {
|
||||
assert_eq!(super::legacy_key("Example"), 0x64CE_ED7E);
|
||||
assert_eq!(super::legacy_key_hex("Example").as_str(), "7EEDCE64");
|
||||
assert_eq!(super::legacy_key(""), 0);
|
||||
// A leading BOM is dropped; only the first 15 characters count.
|
||||
assert_eq!(super::legacy_key("\u{FEFF}Example"), 0x64CE_ED7E);
|
||||
assert_eq!(super::legacy_key("abcdefghijklmnopq"), super::legacy_key("abcdefghijklmno"));
|
||||
assert_ne!(super::legacy_key("abcdefghijklmno"), super::legacy_key("abcdefghijklmn"));
|
||||
}
|
||||
|
||||
/// The legacy form (`w:cryptAlgorithmSid`, `w:hash`, `w:salt`, `w:cryptSpinCount`), as Word
|
||||
/// writes it. The expected hash was computed independently from the specification's stages.
|
||||
#[test]
|
||||
fn legacy_protection_hash_is_verified() {
|
||||
use super::PasswordCheck::*;
|
||||
let legacy = |sid: &str, hash: &str, spin: &str| -> Vec<(String, String)> {
|
||||
vec![
|
||||
("w:cryptProviderType".into(), "rsaFull".into()),
|
||||
("w:cryptAlgorithmClass".into(), "hash".into()),
|
||||
("w:cryptAlgorithmType".into(), "typeAny".into()),
|
||||
("w:cryptAlgorithmSid".into(), sid.into()),
|
||||
("w:cryptSpinCount".into(), spin.into()),
|
||||
("w:hash".into(), hash.into()),
|
||||
("w:salt".into(), "AAECAwQFBgcICQoLDA0ODw==".into()),
|
||||
]
|
||||
};
|
||||
let attrs = legacy("4", KNOWN_LEGACY_SHA1, "100000");
|
||||
assert_eq!(super::check_protection_password(&attrs, "Example"), Correct);
|
||||
assert_eq!(super::check_protection_password(&attrs, "example"), Incorrect);
|
||||
assert_eq!(super::check_protection_password(&attrs, ""), Incorrect);
|
||||
// MD5 (and the other algorithms WordCraft lacks), hostile spin counts: refused.
|
||||
assert_eq!(super::check_protection_password(&legacy("3", KNOWN_LEGACY_SHA1, "100000"), "Example"), Unsupported);
|
||||
assert_eq!(super::check_protection_password(&legacy("4", KNOWN_LEGACY_SHA1, "4294967295"), "Example"), Unsupported);
|
||||
assert_eq!(super::check_protection_password(&legacy("4", KNOWN_LEGACY_SHA1, "-1"), "Example"), Unsupported);
|
||||
let mut ext = attrs.clone();
|
||||
ext.push(("w:algIdExt".into(), "0000000A".into()));
|
||||
assert_eq!(super::check_protection_password(&ext, "Example"), Unsupported);
|
||||
// No algorithm named: the very old bare-hash form isn't defined, so it's refused.
|
||||
let bare = vec![("w:hash".to_string(), KNOWN_LEGACY_SHA1.to_string())];
|
||||
assert_eq!(super::check_protection_password(&bare, "Example"), Unsupported);
|
||||
}
|
||||
|
||||
/// SHA-1 of the salt 00 01 … 0F and "7EEDCE64" as UTF-16LE, iterated 100,000 times.
|
||||
const KNOWN_LEGACY_SHA1: &str = "pEWelKoH6SVpvMmyPAJ8bHA/g/s=";
|
||||
|
||||
use super::*;
|
||||
|
||||
fn zip_package() -> Vec<u8> {
|
||||
let mut doc = wordcraft_doc::Document::from_text("Top secret plans\nSecond paragraph");
|
||||
doc.core.title = "Secret".into();
|
||||
crate::write(&doc).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_with_a_password() {
|
||||
let pkg = zip_package();
|
||||
let enc = encrypt_with_spin_count(&pkg, "pässwörd 🔑", 16).unwrap();
|
||||
assert!(is_encrypted(&enc));
|
||||
assert!(!is_encrypted(&pkg));
|
||||
// The compound file carries the data spaces Word expects.
|
||||
let comp = cfb::CompoundFile::open(Cursor::new(&enc)).unwrap();
|
||||
assert!(comp.is_stream("/\u{6}DataSpaces/TransformInfo/StrongEncryptionTransform/\u{6}Primary"));
|
||||
assert_eq!(decrypt(&enc, Some("pässwörd 🔑")).unwrap(), pkg);
|
||||
let doc = crate::read_with_password(&enc, Some("pässwörd 🔑")).unwrap();
|
||||
assert!(doc.plain_text(wordcraft_doc::StoryRef::Body).contains("Top secret plans"));
|
||||
assert_eq!(doc.core.title, "Secret");
|
||||
// Without a password, or with the wrong one, it says so instead of opening blank.
|
||||
assert_eq!(crate::read(&enc), Err(DocxError::PasswordRequired));
|
||||
assert_eq!(crate::read_with_password(&enc, None), Err(DocxError::PasswordRequired));
|
||||
assert_eq!(decrypt(&enc, Some("Pässwörd 🔑")), Err(DocxError::WrongPassword));
|
||||
// A package that is a whole number of segments, and an empty one.
|
||||
for n in [0usize, SEGMENT, SEGMENT * 2 + 1] {
|
||||
let data: Vec<u8> = (0..n).map(|i| (i * 7) as u8).collect();
|
||||
let enc = encrypt_with_spin_count(&data, "x", 1).unwrap();
|
||||
assert_eq!(decrypt(&enc, Some("x")).unwrap(), data);
|
||||
}
|
||||
assert!(encrypt(&pkg, "").is_err());
|
||||
assert!(encrypt(&pkg, &"x".repeat(MAX_PASSWORD_CHARS + 1)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_without_data_integrity_is_refused() {
|
||||
let enc = encrypt_with_spin_count(&zip_package(), "pw", 4).unwrap();
|
||||
let mut comp = cfb::CompoundFile::open(Cursor::new(enc)).unwrap();
|
||||
let mut info = Vec::new();
|
||||
comp.open_stream("/EncryptionInfo").unwrap().read_to_end(&mut info).unwrap();
|
||||
let xml = String::from_utf8(info[8..].to_vec()).unwrap();
|
||||
let mut stripped = info[..8].to_vec();
|
||||
stripped.extend_from_slice(xml.replacen("<dataIntegrity", "<nothing", 1).as_bytes());
|
||||
comp.create_stream("/EncryptionInfo").unwrap().write_all(&stripped).unwrap();
|
||||
comp.flush().unwrap();
|
||||
let enc = comp.into_inner().into_inner();
|
||||
assert!(matches!(decrypt(&enc, Some("pw")), Err(DocxError::Encryption(m)) if m.contains("dataIntegrity")));
|
||||
assert!(crate::read_with_password(&enc, Some("pw")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tampered_package_fails_the_integrity_check() {
|
||||
let pkg = zip_package();
|
||||
let enc = encrypt_with_spin_count(&pkg, "pw", 4).unwrap();
|
||||
let mut comp = cfb::CompoundFile::open(Cursor::new(enc)).unwrap();
|
||||
let mut data = Vec::new();
|
||||
comp.open_stream("/EncryptedPackage").unwrap().read_to_end(&mut data).unwrap();
|
||||
let last = data.len() - 1;
|
||||
data[last] ^= 1;
|
||||
comp.create_stream("/EncryptedPackage").unwrap().write_all(&data).unwrap();
|
||||
comp.flush().unwrap();
|
||||
let enc = comp.into_inner().into_inner();
|
||||
assert!(matches!(decrypt(&enc, Some("pw")), Err(DocxError::Encryption(m)) if m.contains("integrity")));
|
||||
}
|
||||
|
||||
/// A compound file with the given EncryptionInfo and EncryptedPackage streams.
|
||||
fn cfb_with(info: &[u8], package: &[u8]) -> Vec<u8> {
|
||||
let mut comp = cfb::CompoundFile::create(Cursor::new(Vec::new())).unwrap();
|
||||
comp.create_stream("/EncryptionInfo").unwrap().write_all(info).unwrap();
|
||||
comp.create_stream("/EncryptedPackage").unwrap().write_all(package).unwrap();
|
||||
comp.flush().unwrap();
|
||||
comp.into_inner().into_inner()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_encryption_info_errors_without_panicking() {
|
||||
let good = encrypt_with_spin_count(&zip_package(), "pw", 2).unwrap();
|
||||
let mut comp = cfb::CompoundFile::open(Cursor::new(&good)).unwrap();
|
||||
let mut info = Vec::new();
|
||||
comp.open_stream("/EncryptionInfo").unwrap().read_to_end(&mut info).unwrap();
|
||||
let mut package = Vec::new();
|
||||
comp.open_stream("/EncryptedPackage").unwrap().read_to_end(&mut package).unwrap();
|
||||
let xml = String::from_utf8(info[8..].to_vec()).unwrap();
|
||||
// (EncryptionInfo, EncryptedPackage, must fail). Truncations only must not panic: the
|
||||
// XML reader is lenient, so cutting the closing tags still reads.
|
||||
let mut cases: Vec<(Vec<u8>, Vec<u8>, bool)> = Vec::new();
|
||||
for n in (0..info.len()).step_by(7) {
|
||||
cases.push((info[..n].to_vec(), package.clone(), n < info.len() / 2));
|
||||
}
|
||||
for n in [0, 4, 8, 9, 100, package.len() - 1] {
|
||||
cases.push((info.clone(), package[..n].to_vec(), true));
|
||||
}
|
||||
// Standard and unknown versions, absurd numbers, bad base64, a huge StreamSize.
|
||||
let with = |from: &str, to: &str| {
|
||||
let mut v = info[..8].to_vec();
|
||||
v.extend_from_slice(xml.replacen(from, to, 2).as_bytes());
|
||||
v
|
||||
};
|
||||
cases.push(([3u8, 0, 2, 0, 0x24, 0, 0, 0].to_vec(), package.clone(), true));
|
||||
cases.push(([9u8, 0, 9, 0].to_vec(), package.clone(), true));
|
||||
cases.push((with("spinCount=\"2\"", "spinCount=\"4294967295\""), package.clone(), true));
|
||||
cases.push((with("keyBits=\"256\"", "keyBits=\"8\""), package.clone(), true));
|
||||
cases.push((with("blockSize=\"16\"", "blockSize=\"4096\""), package.clone(), true));
|
||||
cases.push((with("hashSize=\"64\"", "hashSize=\"0\""), package.clone(), true));
|
||||
cases.push((with("saltSize=\"16\"", "saltSize=\"99999999999999999999\""), package.clone(), true));
|
||||
cases.push((with("SHA512", "MD5"), package.clone(), true));
|
||||
cases.push((with("ChainingModeCBC", "ChainingModeCFB"), package.clone(), true));
|
||||
cases.push((with("saltValue=\"", "saltValue=\"!!"), package.clone(), true));
|
||||
cases.push((with("encryptedKeyValue=\"", "encryptedKeyValue=\"AAAA"), package.clone(), true));
|
||||
// Data integrity is mandatory: a file without it is refused, never opened unchecked.
|
||||
cases.push((with("<dataIntegrity", "<nothing"), package.clone(), true));
|
||||
let mut huge = package.clone();
|
||||
huge[..8].copy_from_slice(&u64::MAX.to_le_bytes());
|
||||
cases.push((with("<dataIntegrity", "<nothing"), huge, true));
|
||||
for (i, p, must_fail) in cases {
|
||||
let file = cfb_with(&i, &p);
|
||||
let r = decrypt(&file, Some("pw"));
|
||||
assert!(!must_fail || r.is_err());
|
||||
let _ = crate::read_with_password(&file, Some("pw"));
|
||||
assert!(crate::read(&file).is_err());
|
||||
}
|
||||
// A file that only looks like a compound file.
|
||||
let mut fake = CFB_MAGIC.to_vec();
|
||||
fake.extend_from_slice(&[0u8; 600]);
|
||||
assert!(!is_encrypted(&fake));
|
||||
assert!(decrypt(&fake, Some("pw")).is_err());
|
||||
}
|
||||
|
||||
/// The key a real-world Standard-encrypted package derives: Apache POI's test file
|
||||
/// `protected_passtika.xlsx` (Apache-2.0 test data), password "tika", written by Excel
|
||||
/// itself. The value was computed from [MS-OFFCRYPTO] §2.3.4.7 independently of this code —
|
||||
/// the file decrypts to a valid package under it and nothing else opens that file — so real
|
||||
/// Word 2007 documents stay pinned, not just spec-derived ones.
|
||||
#[test]
|
||||
fn standard_key_matches_a_real_world_package() {
|
||||
let salt: [u8; 16] = [0x19, 0xd0, 0xc7, 0xf3, 0x95, 0x28, 0x78, 0x43, 0x6a, 0x49, 0x49, 0xd7, 0xf0, 0xa2, 0xcf, 0x1e];
|
||||
const KEY: &str = "8d4d00143a5f2e4918af7e6e7410d46c";
|
||||
let hex = |b: &[u8]| b.iter().map(|x| format!("{x:02x}")).collect::<String>();
|
||||
assert_eq!(hex(&standard_key(&salt, "tika", 16)), KEY);
|
||||
// A different password or salt gives a different key; the key is exactly the length
|
||||
// asked for, for each AES width.
|
||||
assert_ne!(hex(&standard_key(&salt, "tika2", 16)), KEY);
|
||||
assert_ne!(hex(&standard_key(&[0; 16], "tika", 16)), KEY);
|
||||
assert_eq!(standard_key(&salt, "tika", 24).len(), 24);
|
||||
assert_eq!(standard_key(&salt, "tika", 32).len(), 32);
|
||||
}
|
||||
|
||||
/// Standard encryption's key derivation (§2.3.4.7) for the password "Password1234_" and the
|
||||
/// salt 00 01 … 0F. The expected keys were computed outside WordCraft (Python's hashlib)
|
||||
/// following the specification's steps, so the encryptor and decryptor can't share a mistake.
|
||||
#[test]
|
||||
fn standard_key_matches_an_independent_derivation() {
|
||||
let salt: Vec<u8> = (0..16).collect();
|
||||
let hex = |b: &[u8]| b.iter().map(|x| format!("{x:02x}")).collect::<String>();
|
||||
assert_eq!(hex(&standard_key(&salt, "Password1234_", 16)), "340fc93a8e2154db8304d055df81cebc");
|
||||
assert_eq!(hex(&standard_key(&salt, "Password1234_", 32)), "340fc93a8e2154db8304d055df81cebcef5e89fb8e5eb520efdca76f3330e032");
|
||||
assert_eq!(standard_key(&salt, "Password1234_", 24).len(), 24);
|
||||
// AES-ECB against FIPS-197 Appendix C.1 and C.3.
|
||||
let pt: Vec<u8> = (0..16u8).map(|i| i * 0x11).collect();
|
||||
let k128: Vec<u8> = (0..16).collect();
|
||||
let k256: Vec<u8> = (0..32).collect();
|
||||
assert_eq!(hex(&ecb(&k128, &pt, true).unwrap()), "69c4e0d86a7b0430d8cdb78070b4c55a");
|
||||
assert_eq!(hex(&ecb(&k256, &pt, true).unwrap()), "8ea2b7ca516745bfeafc49904b496089");
|
||||
assert_eq!(ecb(&k256, &ecb(&k256, &pt, true).unwrap(), false).unwrap(), pt);
|
||||
assert!(ecb(&k128, &pt[..15], false).is_err());
|
||||
}
|
||||
|
||||
/// Standard encryption (Word 2007's AES-ECB method) opens with the right password, for each
|
||||
/// AES key size, and says when the password is missing or wrong — as agile files do.
|
||||
#[test]
|
||||
fn standard_encryption_round_trips() {
|
||||
let pkg = zip_package();
|
||||
for bits in [128, 192, 256] {
|
||||
let enc = encrypt_standard_for_tests(&pkg, "pässwörd 🔑", bits).unwrap();
|
||||
assert!(is_encrypted(&enc));
|
||||
assert_eq!(decrypt(&enc, Some("pässwörd 🔑")), Ok(pkg.clone()), "AES-{bits}");
|
||||
assert_eq!(decrypt(&enc, None), Err(DocxError::PasswordRequired));
|
||||
assert_eq!(decrypt(&enc, Some("Pässwörd 🔑")), Err(DocxError::WrongPassword));
|
||||
assert_eq!(crate::read(&enc), Err(DocxError::PasswordRequired));
|
||||
}
|
||||
let enc = encrypt_standard_for_tests(&pkg, "pw", 128).unwrap();
|
||||
let doc = crate::read_with_password(&enc, Some("pw")).unwrap();
|
||||
assert!(doc.plain_text(wordcraft_doc::StoryRef::Body).contains("Top secret plans"));
|
||||
// Sizes on and off the block boundary, and empty.
|
||||
for n in [0usize, 1, 15, 16, 17, SEGMENT + 3] {
|
||||
let data: Vec<u8> = (0..n).map(|i| (i * 7) as u8).collect();
|
||||
assert_eq!(decrypt(&encrypt_standard_for_tests(&data, "x", 256).unwrap(), Some("x")).unwrap(), data);
|
||||
}
|
||||
assert!(encrypt_standard_for_tests(&pkg, "pw", 64).is_err());
|
||||
assert!(encrypt_standard_for_tests(&pkg, "", 128).is_err());
|
||||
}
|
||||
|
||||
/// A Standard-encrypted file assembled byte by byte from [MS-OFFCRYPTO] §2.3.4.5, §2.3.2, §2.3.3
|
||||
/// and §2.3.4.4 with literal field values, without the test encryptor, so a layout mistake
|
||||
/// shared by the encryptor and the reader can't pass. Only the key derivation and AES-ECB
|
||||
/// (pinned by `standard_key_matches_an_independent_derivation`) are reused.
|
||||
fn hand_built_standard(payload: &[u8], password: &str) -> Vec<u8> {
|
||||
let salt: [u8; 16] = [0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF];
|
||||
let verifier: [u8; 16] = *b"verifier-16bytes";
|
||||
let key = standard_key(&salt, password, 16);
|
||||
let mut hash = sha1::Sha1::digest(verifier).to_vec();
|
||||
hash.resize(32, 0); // the hash, zero-padded to two AES blocks
|
||||
let csp: Vec<u8> = "WordCraft test provider".encode_utf16().chain([0]).flat_map(u16::to_le_bytes).collect();
|
||||
|
||||
let mut header = Vec::new();
|
||||
header.extend_from_slice(&0x24u32.to_le_bytes()); // Flags: fCryptoAPI | fAES
|
||||
header.extend_from_slice(&0u32.to_le_bytes()); // SizeExtra
|
||||
header.extend_from_slice(&0x660Eu32.to_le_bytes()); // AlgID: AES-128
|
||||
header.extend_from_slice(&0x8004u32.to_le_bytes()); // AlgIDHash: SHA-1
|
||||
header.extend_from_slice(&128u32.to_le_bytes()); // KeySize, in bits
|
||||
header.extend_from_slice(&0x18u32.to_le_bytes()); // ProviderType: AES
|
||||
header.extend_from_slice(&0u32.to_le_bytes()); // Reserved1
|
||||
header.extend_from_slice(&0u32.to_le_bytes()); // Reserved2
|
||||
header.extend_from_slice(&csp); // CSPName: UTF-16LE, NUL-terminated
|
||||
|
||||
let mut info = Vec::new();
|
||||
info.extend_from_slice(&3u16.to_le_bytes()); // Version major
|
||||
info.extend_from_slice(&2u16.to_le_bytes()); // Version minor
|
||||
info.extend_from_slice(&0x24u32.to_le_bytes()); // Flags: fCryptoAPI | fAES
|
||||
info.extend_from_slice(&(header.len() as u32).to_le_bytes()); // HeaderSize
|
||||
info.extend_from_slice(&header);
|
||||
info.extend_from_slice(&16u32.to_le_bytes()); // SaltSize
|
||||
info.extend_from_slice(&salt); // Salt
|
||||
info.extend_from_slice(&ecb(&key, &verifier, true).unwrap()); // EncryptedVerifier
|
||||
info.extend_from_slice(&20u32.to_le_bytes()); // VerifierHashSize (SHA-1)
|
||||
info.extend_from_slice(&ecb(&key, &hash, true).unwrap()); // EncryptedVerifierHash
|
||||
|
||||
let mut padded = payload.to_vec();
|
||||
padded.resize(payload.len().next_multiple_of(16), 0);
|
||||
let mut package = (payload.len() as u64).to_le_bytes().to_vec(); // StreamSize
|
||||
package.extend_from_slice(&ecb(&key, &padded, true).unwrap());
|
||||
package.extend_from_slice(&[0xEE, 0xEE, 0xEE]); // trailing padding the spec allows
|
||||
cfb_with(&info, &package)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standard_package_built_by_hand_from_the_spec_opens() {
|
||||
let payload: Vec<u8> = (0..37u8).map(|i| i.wrapping_mul(5)).collect();
|
||||
let file = hand_built_standard(&payload, "pw");
|
||||
assert!(is_encrypted(&file));
|
||||
assert_eq!(decrypt(&file, Some("pw")), Ok(payload.clone()));
|
||||
assert_eq!(decrypt(&file, Some("PW")), Err(DocxError::WrongPassword));
|
||||
assert_eq!(decrypt(&file, None), Err(DocxError::PasswordRequired));
|
||||
// The test encryptor writes the same fixed fields: version and flags (bytes 0..8) and the
|
||||
// header's eight words (12..44); HeaderSize differs with the CSP name, the rest is random.
|
||||
let (hand, _) = streams(&file);
|
||||
let (made, _) = streams(&encrypt_standard_for_tests(&payload, "pw", 128).unwrap());
|
||||
assert_eq!(hand[..8], made[..8]);
|
||||
assert_eq!(hand[12..44], made[12..44]);
|
||||
}
|
||||
|
||||
/// The two streams of an encrypted package.
|
||||
fn streams(file: &[u8]) -> (Vec<u8>, Vec<u8>) {
|
||||
let mut comp = cfb::CompoundFile::open(Cursor::new(file)).unwrap();
|
||||
let mut info = Vec::new();
|
||||
comp.open_stream("/EncryptionInfo").unwrap().read_to_end(&mut info).unwrap();
|
||||
let mut package = Vec::new();
|
||||
comp.open_stream("/EncryptedPackage").unwrap().read_to_end(&mut package).unwrap();
|
||||
(info, package)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_standard_encryption_info_errors_without_panicking() {
|
||||
let data = b"a small package".to_vec();
|
||||
let (info, package) = streams(&encrypt_standard_for_tests(&data, "pw", 128).unwrap());
|
||||
// Layout: version 0, flags 4, header size 8, header 12 (flags, sizeExtra, algId 20,
|
||||
// algIdHash 24, keySize 28, provider, 2 reserved, an empty CSP name), then the verifier at
|
||||
// 46: saltSize, salt 50, encrypted verifier 66, verifierHashSize 82, its hash 86..118.
|
||||
assert_eq!(info.len(), 118);
|
||||
let put = |at: usize, v: u32| {
|
||||
let mut i = info.clone();
|
||||
i[at..at + 4].copy_from_slice(&v.to_le_bytes());
|
||||
i
|
||||
};
|
||||
let mut cases: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
|
||||
for n in 0..info.len() {
|
||||
cases.push((info[..n].to_vec(), package.clone()));
|
||||
}
|
||||
for v in [0, 4, 31, 0x1000, u32::MAX] {
|
||||
cases.push((put(8, v), package.clone()));
|
||||
}
|
||||
// No AES, no CryptoAPI, an external (extensible) provider.
|
||||
for v in [F_CRYPTO_API, F_AES, STANDARD_FLAGS | F_EXTERNAL, 0] {
|
||||
cases.push((put(4, v), package.clone()));
|
||||
}
|
||||
// RC4, MD5, key sizes that don't fit the algorithm.
|
||||
cases.push((put(20, 0x6801), package.clone()));
|
||||
cases.push((put(24, 0x8003), package.clone()));
|
||||
for v in [0, 64, 192, 256, u32::MAX] {
|
||||
cases.push((put(28, v), package.clone()));
|
||||
}
|
||||
for v in [0, 15, 17, u32::MAX] {
|
||||
cases.push((put(46, v), package.clone()));
|
||||
}
|
||||
for v in [0, 16, 32, u32::MAX] {
|
||||
cases.push((put(82, v), package.clone()));
|
||||
}
|
||||
// The package: truncated, an absurd StreamSize, a StreamSize past the data, data that
|
||||
// isn't whole blocks.
|
||||
for n in [0, 4, 8, 9, package.len() - 1] {
|
||||
cases.push((info.clone(), package[..n].to_vec()));
|
||||
}
|
||||
for size in [u64::MAX, 1 << 40, package.len() as u64] {
|
||||
let mut p = package.clone();
|
||||
p[..8].copy_from_slice(&size.to_le_bytes());
|
||||
cases.push((info.clone(), p));
|
||||
}
|
||||
for (i, p) in cases {
|
||||
let file = cfb_with(&i, &p);
|
||||
assert!(decrypt(&file, Some("pw")).is_err());
|
||||
let _ = crate::read_with_password(&file, Some("pw"));
|
||||
assert!(crate::read(&file).is_err());
|
||||
}
|
||||
// Version 2.2 and 4.2 are Standard too; trailing bytes after the last block are ignored.
|
||||
for v in [2u8, 4] {
|
||||
let mut i = info.clone();
|
||||
i[0] = v;
|
||||
assert_eq!(decrypt(&cfb_with(&i, &package), Some("pw")).unwrap(), data);
|
||||
}
|
||||
let mut p = package.clone();
|
||||
p.extend_from_slice(&[1, 2, 3]);
|
||||
assert_eq!(decrypt(&cfb_with(&info, &p), Some("pw")).unwrap(), data);
|
||||
// AlgID and AlgIDHash 0 mean "from the flags": AES-128 and SHA-1.
|
||||
assert_eq!(decrypt(&cfb_with(&put(20, 0), &package), Some("pw")).unwrap(), data);
|
||||
assert_eq!(decrypt(&cfb_with(&put(24, 0), &package), Some("pw")).unwrap(), data);
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
//! Custom document properties (`docProps/custom.xml`, ECMA-376 Part 1 §22.3). Citation
|
||||
//! managers keep their document preferences here (Zotero: `ZOTERO_PREF_1`…), so they must
|
||||
//! survive a round trip intact.
|
||||
|
||||
use wordcraft_doc::CustomProp;
|
||||
|
||||
use crate::xml::{El, MAX_DEPTH, Node, W, esc};
|
||||
|
||||
/// The format identifier every custom property carries (§22.3.2.2).
|
||||
const FMTID: &str = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";
|
||||
/// Most properties we keep from one file.
|
||||
const MAX_PROPS: usize = 10_000;
|
||||
|
||||
/// Simple variant types kept as text.
|
||||
const SIMPLE: &[&str] = &[
|
||||
"lpwstr", "lpstr", "bstr", "i1", "i2", "i4", "i8", "int", "ui1", "ui2", "ui4", "ui8", "uint", "r4", "r8", "decimal", "bool", "date", "filetime",
|
||||
"cy", "error", "clsid",
|
||||
];
|
||||
|
||||
/// Properties of a parsed `docProps/custom.xml` root.
|
||||
pub fn read(root: &El) -> Vec<CustomProp> {
|
||||
let mut out = Vec::new();
|
||||
for p in root.els().filter(|e| e.local() == "property").take(MAX_PROPS) {
|
||||
let Some(name) = p.attr("name").filter(|n| !n.is_empty()) else { continue };
|
||||
let Some(v) = p.els().next() else { continue };
|
||||
let kind = v.local();
|
||||
let prop = if SIMPLE.contains(&kind) {
|
||||
CustomProp {
|
||||
name: name.to_string(),
|
||||
kind: kind.to_string(),
|
||||
value: v.text(),
|
||||
link_target: p.attr("linkTarget").map(str::to_string),
|
||||
odf_user_field: false,
|
||||
}
|
||||
} else {
|
||||
let mut xml = String::new();
|
||||
el_xml(v, &mut xml, 0);
|
||||
CustomProp {
|
||||
name: name.to_string(),
|
||||
kind: "raw".into(),
|
||||
value: xml,
|
||||
link_target: p.attr("linkTarget").map(str::to_string),
|
||||
odf_user_field: false,
|
||||
}
|
||||
};
|
||||
out.push(prop);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `docProps/custom.xml` for `props`.
|
||||
pub fn write(props: &[CustomProp]) -> Vec<u8> {
|
||||
let mut w = W::new();
|
||||
w.open(
|
||||
"Properties",
|
||||
&[
|
||||
("xmlns", "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"),
|
||||
("xmlns:vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"),
|
||||
],
|
||||
);
|
||||
// Property ids start at 2 (§22.3.2.2).
|
||||
for (pid, p) in (2u32..).zip(props.iter().filter(|p| !p.name.is_empty())) {
|
||||
if p.kind == "raw" && !raw_is_wellformed(&p.value) {
|
||||
continue;
|
||||
}
|
||||
let pid = pid.to_string();
|
||||
let mut attrs = vec![("fmtid", FMTID), ("pid", &pid), ("name", &p.name)];
|
||||
if let Some(target) = &p.link_target {
|
||||
attrs.push(("linkTarget", target));
|
||||
}
|
||||
w.open("property", &attrs);
|
||||
if p.kind == "raw" {
|
||||
w.raw(&p.value);
|
||||
} else {
|
||||
let kind = if SIMPLE.contains(&p.kind.as_str()) { p.kind.as_str() } else { "lpwstr" };
|
||||
w.leaf(&format!("vt:{kind}"), &[], &p.value);
|
||||
}
|
||||
w.close("property");
|
||||
}
|
||||
w.close("Properties");
|
||||
w.into_bytes()
|
||||
}
|
||||
|
||||
/// A raw value is written only if it parses inside a `vt`-namespaced wrapper.
|
||||
fn raw_is_wellformed(xml: &str) -> bool {
|
||||
let doc = format!(r#"<x xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">{xml}</x>"#);
|
||||
crate::xml::parse(doc.as_bytes()).is_ok()
|
||||
}
|
||||
|
||||
/// Serialise an element with its canonical prefixes.
|
||||
fn el_xml(e: &El, out: &mut String, depth: usize) {
|
||||
if depth > MAX_DEPTH {
|
||||
return;
|
||||
}
|
||||
out.push('<');
|
||||
out.push_str(&e.name);
|
||||
for (k, v) in &e.attrs {
|
||||
if k.starts_with("xmlns") {
|
||||
continue;
|
||||
}
|
||||
out.push(' ');
|
||||
out.push_str(k);
|
||||
out.push_str("=\"");
|
||||
out.push_str(&esc(v));
|
||||
out.push('"');
|
||||
}
|
||||
if e.kids.is_empty() {
|
||||
out.push_str("/>");
|
||||
return;
|
||||
}
|
||||
out.push('>');
|
||||
for k in &e.kids {
|
||||
match k {
|
||||
Node::El(c) => el_xml(c, out, depth + 1),
|
||||
Node::Text(t) => out.push_str(&esc(t)),
|
||||
}
|
||||
}
|
||||
out.push_str("</");
|
||||
out.push_str(&e.name);
|
||||
out.push('>');
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//! The Fit One Page budget in a DOCX custom XML part.
|
||||
//!
|
||||
//! Word keeps custom XML stores as ordinary package parts; our own store is namespaced so it can
|
||||
//! never be mistaken for a user's data. It is not a document property, so it stays out of
|
||||
//! `docProps/custom.xml`.
|
||||
|
||||
use craft_docx::DocxError;
|
||||
use wordcraft_doc::FitState;
|
||||
|
||||
/// The part this crate writes and reads back.
|
||||
pub(crate) const PART: &str = "customxml/wordcraftfit.xml";
|
||||
/// Root element of the store.
|
||||
pub(crate) const ROOT: &str = "wordcraftFit";
|
||||
/// Namespace of the store.
|
||||
pub(crate) const NS: &str = "urn:wordcraft:fit";
|
||||
/// Marker attribute the parser keeps (namespace declarations are stripped from `El`).
|
||||
pub(crate) const KIND: &str = "wordcraft-fit-v1";
|
||||
|
||||
/// Serialize `fit` as the custom XML store.
|
||||
pub(crate) fn write(fit: &FitState) -> Result<Vec<u8>, DocxError> {
|
||||
let json = serde_json::to_string(fit).map_err(|e| DocxError::Xml(e.to_string()))?;
|
||||
let esc_json = craft_xml::esc(&json);
|
||||
Ok(format!(r#"<?xml version="1.0" encoding="UTF-8"?><{ROOT} xmlns="{NS}" kind="{KIND}"><fitState>{}</fitState></{ROOT}>"#, esc_json).into_bytes())
|
||||
}
|
||||
|
||||
/// Read the state from a custom XML part's bytes, if it is ours.
|
||||
pub(crate) fn read_from_bytes(bytes: &[u8]) -> Option<FitState> {
|
||||
let root = craft_xml::parse(bytes).ok()?;
|
||||
if root.local() != ROOT || root.attr("kind") != Some(KIND) {
|
||||
return None;
|
||||
}
|
||||
let text = root.child_text("fitState")?;
|
||||
serde_json::from_str(text.as_ref()).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use wordcraft_doc::props::LineSpacing;
|
||||
|
||||
fn state() -> FitState {
|
||||
FitState {
|
||||
cum_delta: 1.0,
|
||||
cum_space: 0.94,
|
||||
reduced_font_sizes: vec![11.0],
|
||||
reduced_line_spacings: vec![LineSpacing::Multiple(0.94)],
|
||||
reduced_paragraph_spacings: vec![18.8],
|
||||
original_font_sizes: vec![12.0],
|
||||
original_line_spacings: vec![LineSpacing::Multiple(1.0)],
|
||||
original_paragraph_spacings: vec![20.0],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// The cumulative budget survives a real DOCX write and read, not just `serde_json`.
|
||||
#[test]
|
||||
fn fit_state_round_trips_through_docx() {
|
||||
let mut doc = wordcraft_doc::Document::from_text("Alpha\nBeta");
|
||||
doc.fit_state = Some(state());
|
||||
let bytes = crate::write(&doc).expect("write docx");
|
||||
let back = crate::read(&bytes).expect("read docx");
|
||||
assert_eq!(back.fit_state, doc.fit_state);
|
||||
}
|
||||
}
|
||||
@@ -1,608 +0,0 @@
|
||||
//! Word XML Document: a whole WordprocessingML package as one XML file (the Flat OPC format Word
|
||||
//! saves as "Word XML Document (*.xml)"): a `pkg:package` root holding one `pkg:part` per package
|
||||
//! part, each with its name and content type, and the part's XML inline (`pkg:xmlData`) or its
|
||||
//! bytes in base64 (`pkg:binaryData`).
|
||||
//!
|
||||
//! Reading unpacks the parts into an in-memory [`Package`] and hands it to the DOCX reader;
|
||||
//! writing runs the DOCX writer and lays its parts out in one file. Bounded like the zip reader:
|
||||
//! every part counts against the part count, part size and total size limits whether it's kept
|
||||
//! or not (a repeated name), data is checked before it's copied, and nesting and elements are
|
||||
//! capped as the XML tree reader caps a part's.
|
||||
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
|
||||
use base64::Engine as _;
|
||||
use quick_xml::events::{BytesStart, Event};
|
||||
use quick_xml::name::ResolveResult;
|
||||
use quick_xml::reader::NsReader;
|
||||
use wordcraft_doc::Document;
|
||||
|
||||
use crate::package::{ContentTypes, MAX_ENTRIES, MAX_PART, MAX_TOTAL, Package};
|
||||
use crate::xml::{self, W, esc};
|
||||
use crate::{DocxError, Flavor};
|
||||
|
||||
/// The Flat OPC package namespace.
|
||||
pub const PKG_NS: &str = "http://schemas.microsoft.com/office/2006/xmlPackage";
|
||||
/// The WordprocessingML 2003 namespace (Word 2003 XML Document).
|
||||
pub const W2003_NS: &str = "http://schemas.microsoft.com/office/word/2003/wordml";
|
||||
|
||||
/// What kind of XML file a `.xml` holds.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum XmlKind {
|
||||
/// A Flat OPC package (Word XML Document, Word 2007 and later).
|
||||
FlatOpc,
|
||||
/// A Word 2003 XML Document (WordprocessingML 2003).
|
||||
WordMl2003,
|
||||
/// Some other XML, or not XML at all.
|
||||
Other,
|
||||
}
|
||||
|
||||
/// Elements and declarations looked at before the root element (comments, processing
|
||||
/// instructions) before giving up.
|
||||
const MAX_PROLOG_EVENTS: usize = 10_000;
|
||||
|
||||
/// The text of `bytes` as UTF-8: UTF-16 with a byte-order mark is transcoded, a UTF-8 one dropped.
|
||||
fn utf8_text(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> {
|
||||
match xml::utf16_to_utf8(bytes) {
|
||||
Some(s) => std::borrow::Cow::Owned(s.into_bytes()),
|
||||
None => std::borrow::Cow::Borrowed(bytes.strip_prefix(&[0xEF, 0xBB, 0xBF][..]).unwrap_or(bytes)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Tell a Word XML file by its root element: `pkg:package` (Flat OPC) or `w:wordDocument`
|
||||
/// (Word 2003), each in its namespace.
|
||||
pub fn sniff_xml(bytes: &[u8]) -> XmlKind {
|
||||
let text = utf8_text(bytes);
|
||||
let mut r = NsReader::from_reader(text.as_ref());
|
||||
for _ in 0..MAX_PROLOG_EVENTS {
|
||||
match r.read_resolved_event() {
|
||||
Ok((res, Event::Start(e) | Event::Empty(e))) => {
|
||||
let ns = match res {
|
||||
ResolveResult::Bound(ns) => ns.as_ref().to_vec(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
return match (e.local_name().as_ref(), ns.as_slice()) {
|
||||
(b"package", ns) if ns == PKG_NS.as_bytes() => XmlKind::FlatOpc,
|
||||
(b"wordDocument", ns) if ns == W2003_NS.as_bytes() => XmlKind::WordMl2003,
|
||||
_ => XmlKind::Other,
|
||||
};
|
||||
}
|
||||
Ok((_, Event::Eof)) | Err(_) => return XmlKind::Other,
|
||||
Ok(_) => {}
|
||||
}
|
||||
}
|
||||
XmlKind::Other
|
||||
}
|
||||
|
||||
/// Read a Flat OPC file.
|
||||
pub fn read(bytes: &[u8]) -> Result<Document, DocxError> {
|
||||
crate::read::read_opened(&unpack(bytes)?, None)
|
||||
}
|
||||
|
||||
/// The data element of the part being read.
|
||||
enum Data {
|
||||
/// `pkg:xmlData`: its content starts at this byte offset.
|
||||
Xml(usize),
|
||||
/// `pkg:binaryData`: the base64 text so far.
|
||||
Binary(String),
|
||||
}
|
||||
|
||||
/// The part being read: name, content type, data.
|
||||
struct Part {
|
||||
name: String,
|
||||
content_type: String,
|
||||
data: Option<Data>,
|
||||
}
|
||||
|
||||
/// Deepest element nesting read: a part's inline XML starts below the package, part and data
|
||||
/// elements and may nest as deep as the XML tree reader takes ([`xml::MAX_DEPTH`] open elements
|
||||
/// and an empty one inside the deepest).
|
||||
const MAX_NESTING: usize = 3 + xml::MAX_DEPTH + 1;
|
||||
|
||||
/// Most elements in one child of the package: a part, its data element and as many as the XML
|
||||
/// tree reader takes in a part ([`xml::MAX_ELEMENTS`]).
|
||||
const MAX_PART_ELEMENTS: usize = 2 + xml::MAX_ELEMENTS;
|
||||
|
||||
/// Longest base64 text of a part that decodes to at most `bytes` (whitespace isn't kept).
|
||||
fn max_base64(bytes: u64) -> u64 {
|
||||
(bytes / 3).saturating_add(1).saturating_mul(4)
|
||||
}
|
||||
|
||||
/// Bytes the next part may have: its own size limit, or what's left of the package's.
|
||||
fn room(total: u64) -> u64 {
|
||||
MAX_PART.min(MAX_TOTAL.saturating_sub(total))
|
||||
}
|
||||
|
||||
/// Unpack a Flat OPC file into its parts, with a `[Content_Types].xml` made from the parts'
|
||||
/// content types (unless the file carries one).
|
||||
pub(crate) fn unpack(bytes: &[u8]) -> Result<Package, DocxError> {
|
||||
let text = utf8_text(bytes);
|
||||
let src: &[u8] = text.as_ref();
|
||||
let mut r = NsReader::from_reader(src);
|
||||
r.config_mut().check_end_names = true;
|
||||
let mut depth = 0usize;
|
||||
// Children of the package (parts or anything else), and elements in the current one.
|
||||
let mut entries = 0usize;
|
||||
let mut elements = 0usize;
|
||||
let mut files: BTreeMap<String, Vec<u8>> = BTreeMap::new();
|
||||
let mut types: Vec<(String, String)> = Vec::new();
|
||||
let mut total: u64 = 0;
|
||||
let mut part: Option<Part> = None;
|
||||
// `xmlns` declarations on the package, part and data elements, in scope for the inline XML.
|
||||
let mut scope: Vec<Vec<(String, String)>> = Vec::new();
|
||||
let bad = |e: quick_xml::Error| DocxError::Xml(e.to_string());
|
||||
loop {
|
||||
let before = usize::try_from(r.buffer_position()).unwrap_or(usize::MAX);
|
||||
let (res, ev) = r.read_resolved_event().map_err(bad)?;
|
||||
let in_pkg = matches!(&res, ResolveResult::Bound(ns) if ns.as_ref() == PKG_NS.as_bytes());
|
||||
match ev {
|
||||
Event::Start(ref e) | Event::Empty(ref e) => {
|
||||
let empty = matches!(ev, Event::Empty(_));
|
||||
depth += 1;
|
||||
if depth > MAX_NESTING {
|
||||
return Err(DocxError::Limit("XML nesting too deep".into()));
|
||||
}
|
||||
if depth == 2 {
|
||||
entries += 1;
|
||||
elements = 0;
|
||||
if entries > MAX_ENTRIES {
|
||||
return Err(DocxError::Limit(format!("more than {MAX_ENTRIES} parts")));
|
||||
}
|
||||
}
|
||||
elements += 1;
|
||||
if elements > MAX_PART_ELEMENTS {
|
||||
return Err(DocxError::Limit("too many XML elements".into()));
|
||||
}
|
||||
let local = e.local_name().as_ref().to_vec();
|
||||
match depth {
|
||||
1 if !(in_pkg && local == b"package") => {
|
||||
return Err(DocxError::NotWord("the root element isn't a Flat OPC pkg:package".into()));
|
||||
}
|
||||
2 if in_pkg && local == b"part" => {
|
||||
let name = attr(e, b"name").unwrap_or_default();
|
||||
let content_type = attr(e, b"contentType").unwrap_or_default();
|
||||
part = Some(Part { name, content_type, data: None });
|
||||
}
|
||||
3 if in_pkg => {
|
||||
if let Some(p) = part.as_mut().filter(|p| p.data.is_none()) {
|
||||
let pos = usize::try_from(r.buffer_position()).unwrap_or(usize::MAX);
|
||||
p.data = match local.as_slice() {
|
||||
// `<pkg:xmlData/>`: an empty part.
|
||||
b"xmlData" if empty => Some(Data::Binary(String::new())),
|
||||
b"xmlData" => Some(Data::Xml(pos)),
|
||||
b"binaryData" => Some(Data::Binary(String::new())),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if depth <= 3 {
|
||||
scope.push(ns_decls(e));
|
||||
}
|
||||
if empty {
|
||||
if depth == 2
|
||||
&& let Some(p) = part.take()
|
||||
{
|
||||
finish_part(p, &mut files, &mut types, &mut total)?;
|
||||
}
|
||||
if depth <= 3 {
|
||||
scope.pop();
|
||||
}
|
||||
depth -= 1;
|
||||
}
|
||||
}
|
||||
Event::End(_) => {
|
||||
// The inline XML ends where its data element's end tag starts.
|
||||
if depth == 3
|
||||
&& let Some(p) = part.as_mut()
|
||||
&& let Some(Data::Xml(start)) = p.data
|
||||
{
|
||||
p.data = None;
|
||||
let frag = src.get(start..before).unwrap_or_default();
|
||||
// Refused before it's copied when it can't fit.
|
||||
if frag.len() as u64 > room(total) {
|
||||
return Err(DocxError::Limit(format!("part {} is larger than the size limit", p.name)));
|
||||
}
|
||||
let xml = with_scope(frag, &scope);
|
||||
store(&p.name, &p.content_type, xml, &mut files, &mut types, &mut total)?;
|
||||
}
|
||||
if depth == 2
|
||||
&& let Some(p) = part.take()
|
||||
{
|
||||
finish_part(p, &mut files, &mut types, &mut total)?;
|
||||
}
|
||||
if depth <= 3 {
|
||||
scope.pop();
|
||||
}
|
||||
depth = depth.saturating_sub(1);
|
||||
if depth == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Event::Text(ref t) if depth == 3 => add_base64(&mut part, t, total)?,
|
||||
Event::CData(ref t) if depth == 3 => add_base64(&mut part, t, total)?,
|
||||
Event::GeneralRef(ref g) if depth == 3 => match g.resolve_char_ref() {
|
||||
// A character reference stands for its character (`A` is an `A`).
|
||||
Ok(Some(c)) => add_base64(&mut part, c.encode_utf8(&mut [0; 4]).as_bytes(), total)?,
|
||||
// An entity isn't base64: binary data holding one is broken, not shorter.
|
||||
_ => {
|
||||
if let Some(Part { name, data: Some(Data::Binary(_)), .. }) = &part {
|
||||
return Err(DocxError::Xml(format!("part {name} has invalid base64 data")));
|
||||
}
|
||||
}
|
||||
},
|
||||
Event::Eof => break,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if files.is_empty() {
|
||||
return Err(DocxError::NotWord("the Flat OPC package has no parts".into()));
|
||||
}
|
||||
if !files.contains_key("[Content_Types].xml") {
|
||||
let mut w = W::new();
|
||||
w.open("Types", &[("xmlns", "http://schemas.openxmlformats.org/package/2006/content-types")]);
|
||||
for (name, ct) in &types {
|
||||
w.empty("Override", &[("PartName", &format!("/{name}")), ("ContentType", ct)]);
|
||||
}
|
||||
w.close("Types");
|
||||
files.insert("[Content_Types].xml".into(), w.into_bytes());
|
||||
}
|
||||
Ok(Package::from_parts(files))
|
||||
}
|
||||
|
||||
/// End of a `pkg:part`: store its binary data (inline XML was stored when it ended).
|
||||
fn finish_part(p: Part, files: &mut BTreeMap<String, Vec<u8>>, types: &mut Vec<(String, String)>, total: &mut u64) -> Result<(), DocxError> {
|
||||
match p.data {
|
||||
Some(Data::Binary(b64)) => {
|
||||
let bytes = base64::engine::general_purpose::STANDARD_NO_PAD
|
||||
.decode(b64.trim_end_matches('='))
|
||||
.map_err(|_| DocxError::Xml(format!("part {} has invalid base64 data", p.name)))?;
|
||||
store(&p.name, &p.content_type, bytes, files, types, total)
|
||||
}
|
||||
// A part without data, or whose inline XML was already stored.
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Text (or CDATA) inside a part's `pkg:binaryData`: its base64 without whitespace, checked
|
||||
/// against the size limits before it's copied.
|
||||
fn add_base64(part: &mut Option<Part>, text: &[u8], total: u64) -> Result<(), DocxError> {
|
||||
let Some(Part { data: Some(Data::Binary(b64)), .. }) = part else { return Ok(()) };
|
||||
let s = std::str::from_utf8(text).map_err(|e| DocxError::Xml(e.to_string()))?;
|
||||
let more = s.bytes().filter(|b| !b.is_ascii_whitespace()).count();
|
||||
if b64.len().saturating_add(more) as u64 > max_base64(room(total)) {
|
||||
return Err(DocxError::Limit("a binary part is larger than the size limit".into()));
|
||||
}
|
||||
b64.extend(s.chars().filter(|c| !c.is_ascii_whitespace()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add a part, within the size limits; a repeated or empty name isn't stored but counts against
|
||||
/// them all the same (as a repeated zip entry does).
|
||||
fn store(
|
||||
name: &str,
|
||||
content_type: &str,
|
||||
bytes: Vec<u8>,
|
||||
files: &mut BTreeMap<String, Vec<u8>>,
|
||||
types: &mut Vec<(String, String)>,
|
||||
total: &mut u64,
|
||||
) -> Result<(), DocxError> {
|
||||
let name = name.trim_start_matches('/');
|
||||
let len = bytes.len() as u64;
|
||||
if len > MAX_PART {
|
||||
return Err(DocxError::Limit(format!("part {name} is larger than the size limit")));
|
||||
}
|
||||
*total = total.saturating_add(len);
|
||||
if *total > MAX_TOTAL {
|
||||
return Err(DocxError::Limit("the package is larger than the size limit".into()));
|
||||
}
|
||||
if name.is_empty() || files.contains_key(name) {
|
||||
return Ok(());
|
||||
}
|
||||
if !content_type.is_empty() && !content_type.contains(['\r', '\n']) {
|
||||
types.push((name.to_string(), content_type.to_string()));
|
||||
}
|
||||
files.insert(name.to_string(), bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An attribute's unescaped value, by local name (any prefix).
|
||||
fn attr(e: &BytesStart, local: &[u8]) -> Option<String> {
|
||||
e.attributes().with_checks(false).flatten().find(|a| a.key.local_name().as_ref() == local).map(|a| {
|
||||
match a.normalized_value(quick_xml::XmlVersion::Explicit1_0) {
|
||||
Ok(v) => v.into_owned(),
|
||||
Err(_) => String::from_utf8_lossy(&a.value).into_owned(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// The `xmlns` / `xmlns:p` declarations of a start tag, as written (value still escaped).
|
||||
fn ns_decls(e: &BytesStart) -> Vec<(String, String)> {
|
||||
e.attributes()
|
||||
.with_checks(false)
|
||||
.flatten()
|
||||
.filter(|a| a.key.as_ref() == b"xmlns" || a.key.as_ref().starts_with(b"xmlns:"))
|
||||
.filter(|a| !a.value.contains(&b'"'))
|
||||
.map(|a| (String::from_utf8_lossy(a.key.as_ref()).into_owned(), String::from_utf8_lossy(&a.value).into_owned()))
|
||||
.take(256)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The inline XML of a part as a document of its own: namespaces it inherits from the package
|
||||
/// elements are declared on its root element (unless it declares them itself).
|
||||
fn with_scope(frag: &[u8], scope: &[Vec<(String, String)>]) -> Vec<u8> {
|
||||
let decls: Vec<&(String, String)> = scope.iter().flatten().filter(|(k, _)| k != "xmlns:pkg").collect();
|
||||
let Some(open) = root_tag_start(frag) else { return frag.to_vec() };
|
||||
if decls.is_empty() {
|
||||
return frag.get(open..).unwrap_or_default().to_vec();
|
||||
}
|
||||
let rest = frag.get(open..).unwrap_or_default();
|
||||
let name_end = rest.iter().position(|b| b.is_ascii_whitespace() || *b == b'>' || *b == b'/').unwrap_or(rest.len());
|
||||
let tag_end = rest.iter().position(|b| *b == b'>').unwrap_or(rest.len());
|
||||
let tag = String::from_utf8_lossy(rest.get(1..tag_end).unwrap_or_default());
|
||||
let start = BytesStart::from_content(tag.as_ref(), name_end.saturating_sub(1));
|
||||
let declared: HashSet<Vec<u8>> = start
|
||||
.attributes()
|
||||
.with_checks(false)
|
||||
.flatten()
|
||||
.filter(|a| a.key.as_ref() == b"xmlns" || a.key.as_ref().starts_with(b"xmlns:"))
|
||||
.map(|a| a.key.as_ref().to_vec())
|
||||
.collect();
|
||||
let mut out = Vec::with_capacity(rest.len() + 256);
|
||||
out.extend_from_slice(rest.get(..name_end).unwrap_or_default());
|
||||
// Innermost declarations win: walk from the data element out, skipping prefixes seen.
|
||||
let mut seen: Vec<&str> = Vec::new();
|
||||
for (k, v) in decls.iter().rev() {
|
||||
if declared.contains(k.as_bytes()) || seen.contains(&k.as_str()) {
|
||||
continue;
|
||||
}
|
||||
seen.push(k);
|
||||
out.extend_from_slice(format!(" {k}=\"{v}\"").as_bytes());
|
||||
}
|
||||
out.extend_from_slice(rest.get(name_end..).unwrap_or_default());
|
||||
out
|
||||
}
|
||||
|
||||
/// Offset of the `<` of the first element in `frag` (after whitespace, comments and processing
|
||||
/// instructions).
|
||||
fn root_tag_start(frag: &[u8]) -> Option<usize> {
|
||||
let mut i = 0usize;
|
||||
loop {
|
||||
while frag.get(i).is_some_and(u8::is_ascii_whitespace) {
|
||||
i += 1;
|
||||
}
|
||||
let rest = frag.get(i..)?;
|
||||
if rest.starts_with(b"<!--") {
|
||||
i += rest.windows(3).position(|w| w == b"-->")? + 3;
|
||||
} else if rest.starts_with(b"<?") {
|
||||
i += rest.windows(2).position(|w| w == b"?>")? + 2;
|
||||
} else if rest.first() == Some(&b'<') {
|
||||
return Some(i);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Write `doc` as a Word XML Document (Flat OPC).
|
||||
pub fn write(doc: &Document) -> Result<Vec<u8>, DocxError> {
|
||||
let parts = crate::write::write_parts(doc, Flavor::Document)?;
|
||||
let types = parts
|
||||
.iter()
|
||||
.find(|(n, _)| n == "[Content_Types].xml")
|
||||
.map(|(n, b)| ContentTypes::read(&Package::from_parts(BTreeMap::from([(n.clone(), b.clone())]))))
|
||||
.unwrap_or_default();
|
||||
let mut s = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<?mso-application progid=\"Word.Document\"?>\n");
|
||||
s.push_str(&format!("<pkg:package xmlns:pkg=\"{PKG_NS}\">"));
|
||||
for (name, bytes) in &parts {
|
||||
if name == "[Content_Types].xml" {
|
||||
continue;
|
||||
}
|
||||
let ct = types.of(name).unwrap_or("application/octet-stream");
|
||||
s.push_str(&format!("<pkg:part pkg:name=\"/{}\" pkg:contentType=\"{}\"", esc(name), esc(ct)));
|
||||
match xml_fragment(ct, bytes) {
|
||||
Some(x) => {
|
||||
s.push_str("><pkg:xmlData>");
|
||||
s.push_str(x);
|
||||
s.push_str("</pkg:xmlData></pkg:part>");
|
||||
}
|
||||
None => {
|
||||
s.push_str(" pkg:compression=\"store\"><pkg:binaryData>");
|
||||
let b64 = base64::engine::general_purpose::STANDARD.encode(bytes);
|
||||
for (i, chunk) in b64.as_bytes().chunks(76).enumerate() {
|
||||
if i > 0 {
|
||||
s.push('\n');
|
||||
}
|
||||
s.push_str(std::str::from_utf8(chunk).unwrap_or_default());
|
||||
}
|
||||
s.push_str("</pkg:binaryData></pkg:part>");
|
||||
}
|
||||
}
|
||||
}
|
||||
s.push_str("</pkg:package>");
|
||||
Ok(s.into_bytes())
|
||||
}
|
||||
|
||||
/// An XML part's markup without its XML declaration, to inline as `pkg:xmlData`; `None` for
|
||||
/// a part that isn't UTF-8 XML (or has a DOCTYPE), which goes in base64.
|
||||
fn xml_fragment<'a>(ct: &str, bytes: &'a [u8]) -> Option<&'a str> {
|
||||
let ct = ct.to_ascii_lowercase();
|
||||
if !(ct.ends_with("+xml") || ct.ends_with("/xml")) {
|
||||
return None;
|
||||
}
|
||||
let s = std::str::from_utf8(bytes).ok()?;
|
||||
let s = s.strip_prefix('\u{FEFF}').unwrap_or(s).trim_start();
|
||||
let s = match s.strip_prefix("<?xml") {
|
||||
Some(rest) => rest.split_once("?>").map(|(_, r)| r)?.trim_start(),
|
||||
None => s,
|
||||
};
|
||||
(s.starts_with('<') && !s.contains("<!DOCTYPE") && one_element(s)).then_some(s)
|
||||
}
|
||||
|
||||
/// Whether `s` is exactly one well-formed element, with only whitespace, comments and processing
|
||||
/// instructions around it. Kept bytes are inlined into `pkg:xmlData` only then; anything else could
|
||||
/// close the wrapper and add parts of its own, so it goes out as `pkg:binaryData` (#3742).
|
||||
fn one_element(s: &str) -> bool {
|
||||
let mut r = quick_xml::Reader::from_str(s);
|
||||
r.config_mut().check_comments = true;
|
||||
let (mut depth, mut roots) = (0usize, 0usize);
|
||||
loop {
|
||||
let attrs_ok = |e: &BytesStart| e.attributes().all(|a| a.is_ok());
|
||||
match r.read_event() {
|
||||
Ok(Event::Start(e)) => {
|
||||
if !attrs_ok(&e) || (depth == 0 && roots > 0) {
|
||||
return false;
|
||||
}
|
||||
roots += usize::from(depth == 0);
|
||||
depth += 1;
|
||||
}
|
||||
Ok(Event::Empty(e)) => {
|
||||
if !attrs_ok(&e) || (depth == 0 && roots > 0) {
|
||||
return false;
|
||||
}
|
||||
roots += usize::from(depth == 0);
|
||||
}
|
||||
Ok(Event::End(_)) => match depth.checked_sub(1) {
|
||||
Some(d) => depth = d,
|
||||
None => return false,
|
||||
},
|
||||
Ok(Event::Text(t)) if depth == 0 => {
|
||||
if !t.iter().all(u8::is_ascii_whitespace) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Ok(Event::CData(_) | Event::GeneralRef(_)) if depth == 0 => return false,
|
||||
Ok(Event::Decl(_) | Event::DocType(_)) | Err(_) => return false,
|
||||
Ok(Event::Eof) => return depth == 0 && roots == 1,
|
||||
Ok(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sniffs_root_elements() {
|
||||
assert_eq!(
|
||||
sniff_xml(
|
||||
format!("<?xml version=\"1.0\"?><?mso-application progid=\"Word.Document\"?><!-- x --><p:package xmlns:p=\"{PKG_NS}\"/>").as_bytes()
|
||||
),
|
||||
XmlKind::FlatOpc
|
||||
);
|
||||
assert_eq!(sniff_xml(format!("\u{FEFF}<w:wordDocument xmlns:w=\"{W2003_NS}\"/>").as_bytes()), XmlKind::WordMl2003);
|
||||
assert_eq!(sniff_xml(b"<package/>"), XmlKind::Other);
|
||||
assert_eq!(sniff_xml(b"<html><body/></html>"), XmlKind::Other);
|
||||
assert_eq!(sniff_xml(b"\x00\xff garbage"), XmlKind::Other);
|
||||
assert_eq!(sniff_xml(b""), XmlKind::Other);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_xml_inherits_namespaces_from_the_package() {
|
||||
let frag = b"\n <!-- c --> <w:document><w:body/></w:document>";
|
||||
let scope = vec![vec![("xmlns:pkg".to_string(), PKG_NS.to_string()), ("xmlns:w".to_string(), "urn:w".to_string())]];
|
||||
let out = String::from_utf8(with_scope(frag, &scope)).unwrap();
|
||||
assert_eq!(out, "<w:document xmlns:w=\"urn:w\"><w:body/></w:document>");
|
||||
let own = b"<w:document xmlns:w=\"urn:own\"/>";
|
||||
assert_eq!(with_scope(own, &scope), own.to_vec());
|
||||
assert_eq!(with_scope(b"no element", &scope), b"no element".to_vec());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inherited_namespaces_do_not_rescan_a_long_root_tag() {
|
||||
let scope = vec![(0..256).map(|i| (format!("xmlns:p{i}"), format!("urn:{i}"))).collect::<Vec<_>>()];
|
||||
let frag = format!("<w:document{}><w:body/></w:document>", " a=\"1\"".repeat(4_000_000));
|
||||
let out = with_scope(frag.as_bytes(), &scope);
|
||||
let out = String::from_utf8(out).unwrap();
|
||||
assert_eq!(out.matches("xmlns:p").count(), 256);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nesting_is_capped_where_the_xml_tree_reader_stops() {
|
||||
let inline = |levels: usize| format!("{}<b/>{}", "<a>".repeat(levels), "</a>".repeat(levels));
|
||||
let package = |levels: usize| {
|
||||
format!(
|
||||
"<pkg:package xmlns:pkg=\"{PKG_NS}\"><pkg:part pkg:name=\"/a.xml\"><pkg:xmlData>{}</pkg:xmlData></pkg:part></pkg:package>",
|
||||
inline(levels)
|
||||
)
|
||||
};
|
||||
assert!(xml::parse(inline(xml::MAX_DEPTH).as_bytes()).is_ok());
|
||||
assert!(unpack(package(xml::MAX_DEPTH).as_bytes()).is_ok());
|
||||
assert!(matches!(xml::parse(inline(xml::MAX_DEPTH + 1).as_bytes()), Err(DocxError::Limit(_))));
|
||||
assert!(matches!(unpack(package(xml::MAX_DEPTH + 1).as_bytes()), Err(DocxError::Limit(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kept_xml_that_would_break_out_of_its_part_is_written_in_base64() {
|
||||
// A part kept from a file is written back as it was read; inlined as is, markup that
|
||||
// closes the wrapper adds parts of its own (here a second core properties part, read
|
||||
// before the real one) (#3742).
|
||||
let evil = format!(
|
||||
"<a/></pkg:xmlData></pkg:part><pkg:part pkg:name=\"/docProps/core.xml\" pkg:contentType=\"application/vnd.openxmlformats-package.core-properties+xml\"><pkg:xmlData><cp:coreProperties xmlns:cp=\"{}\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"><dc:title>Injected</dc:title></cp:coreProperties></pkg:xmlData></pkg:part><pkg:part pkg:name=\"/x.xml\"><pkg:xmlData><b>",
|
||||
"http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
|
||||
);
|
||||
let w_ns = "xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"";
|
||||
let rel = |id: &str, kind: &str, target: &str| format!("<Relationship Id=\"{id}\" Type=\"{kind}\" Target=\"{target}\"/>");
|
||||
let rels = format!(
|
||||
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">{}{}</Relationships>",
|
||||
rel("rId1", crate::package::rt::OFFICE_DOC, "word/document.xml"),
|
||||
rel("rId2", crate::package::rt::CUSTOM_XML, "customXml/item1.xml")
|
||||
);
|
||||
let types = "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"xml\" ContentType=\"application/xml\"/></Types>";
|
||||
let z = crate::package::zip_entries(&[
|
||||
("[Content_Types].xml".into(), types.as_bytes().to_vec()),
|
||||
("_rels/.rels".into(), rels.into_bytes()),
|
||||
("word/document.xml".into(), format!("<w:document {w_ns}><w:body><w:p/></w:body></w:document>").into_bytes()),
|
||||
("customXml/item1.xml".into(), evil.clone().into_bytes()),
|
||||
])
|
||||
.unwrap();
|
||||
let mut doc = crate::read(&z).unwrap();
|
||||
assert!(doc.passthrough.values().any(|b| b.as_slice() == evil.as_bytes()), "the part is kept as read");
|
||||
doc.core.title = "Real".into();
|
||||
let flat = write(&doc).unwrap();
|
||||
let text = String::from_utf8_lossy(&flat);
|
||||
assert_eq!(text.matches("pkg:name=\"/docProps/core.xml\"").count(), 1, "{text}");
|
||||
let back = read(&flat).unwrap();
|
||||
assert_eq!(back.core.title, "Real");
|
||||
assert!(back.passthrough.values().any(|b| b.as_slice() == evil.as_bytes()), "the part survives, in base64");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_one_well_formed_element_is_inlined() {
|
||||
let ct = "application/xml";
|
||||
for ok in ["<a/>", "<?xml version=\"1.0\"?>\n<a x=\"1\"><b>t & <![CDATA[c]]></b></a>\n<!-- end -->", "<!-- c --><a/><?pi x?>"] {
|
||||
assert!(xml_fragment(ct, ok.as_bytes()).is_some(), "{ok}");
|
||||
}
|
||||
for bad in [
|
||||
"<a/><b/>",
|
||||
"<a></b>",
|
||||
"<a>",
|
||||
"<a/></x>",
|
||||
"<a/>text",
|
||||
"<a/>&",
|
||||
"<a x=1/>",
|
||||
"<a/><?xml version=\"1.0\"?>",
|
||||
"<a></a></pkg:xmlData>",
|
||||
"<a><!-- x -- y --></a>",
|
||||
"<a/><!-- x--->",
|
||||
] {
|
||||
assert!(xml_fragment(ct, bad.as_bytes()).is_none(), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_and_nameless_parts_count_against_the_size_limits() {
|
||||
let (mut files, mut types, mut total) = (BTreeMap::new(), Vec::new(), 0u64);
|
||||
for name in ["/a.bin", "/a.bin", ""] {
|
||||
store(name, "", vec![0; 10], &mut files, &mut types, &mut total).unwrap();
|
||||
}
|
||||
assert_eq!((files.len(), total), (1, 30));
|
||||
// Near the package limit, what's left of it is all the next part may have.
|
||||
total = MAX_TOTAL - 5;
|
||||
assert_eq!(room(total), 5);
|
||||
assert_eq!(max_base64(5), 8);
|
||||
assert!(matches!(store("/b.bin", "", vec![0; 6], &mut files, &mut types, &mut total), Err(DocxError::Limit(_))));
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
//! Fonts a file embeds through its font table (#1171).
|
||||
//!
|
||||
//! The reader keeps the font table and its font parts as opaque passthrough parts, for writing
|
||||
//! back. [`embedded_fonts`] hands their font files to whoever lays the document out, undoing the
|
||||
//! obfuscation Word applies to them (ECMA-376 Part 1, 17.8.1).
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use wordcraft_doc::Document;
|
||||
|
||||
use crate::package::{EMBEDDED_PARTS, EmbeddedManifest};
|
||||
|
||||
/// Most font files taken from one document.
|
||||
const MAX_FONTS: usize = 64;
|
||||
|
||||
/// Most bytes of font files copied out of one document: one part's limit, however many
|
||||
/// references the font table makes (each reference is a copy; 64 of them to one large part
|
||||
/// were 64 copies, #3692).
|
||||
const MAX_FONT_BYTES: u64 = crate::package::MAX_PART;
|
||||
|
||||
/// The font table's references to embedded font files, one per style.
|
||||
const EMBEDS: [&str; 4] = ["w:embedRegular", "w:embedBold", "w:embedItalic", "w:embedBoldItalic"];
|
||||
|
||||
/// A font file a document embeds, and the name its font table gives the font: the name the text
|
||||
/// asks for, which a subset Word embeds doesn't carry inside (#2240).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct EmbeddedFont {
|
||||
/// `w:font/@w:name`.
|
||||
pub name: String,
|
||||
/// The font file, de-obfuscated.
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
/// The font files `doc`'s font table embeds, de-obfuscated, in table order. Not checked to be
|
||||
/// fonts: that is up to the font database. Empty for a document read from anything but a package
|
||||
/// with a font table.
|
||||
pub fn embedded_fonts(doc: &Document) -> Vec<EmbeddedFont> {
|
||||
fonts_within(doc, MAX_FONT_BYTES)
|
||||
}
|
||||
|
||||
/// [`embedded_fonts`], copying at most `budget` bytes of font files.
|
||||
fn fonts_within(doc: &Document, mut budget: u64) -> Vec<EmbeddedFont> {
|
||||
let budget_total = budget;
|
||||
let manifest = doc.passthrough.get(EMBEDDED_PARTS).map(|m| EmbeddedManifest::parse(m)).unwrap_or_default();
|
||||
// The table is kept under the spelling the package stores it as (#3872).
|
||||
let Some(key) = crate::package::font_table_key(&doc.passthrough) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let Some(table) = doc.passthrough.get(key).and_then(|b| crate::xml::parse(b).ok()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let Some(part) = manifest.parts.get(key) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
// A reference repeating one already taken (same file, key and name) gives the same font.
|
||||
let mut taken: HashSet<(&str, [u8; 16], &str)> = HashSet::new();
|
||||
let mut over_budget = 0usize;
|
||||
let embeds = table
|
||||
.children("w:font")
|
||||
.flat_map(|f| f.els().filter(|e| EMBEDS.contains(&e.name.as_str())).map(move |e| (f.attr("w:name").unwrap_or(""), e)));
|
||||
for (name, embed) in embeds {
|
||||
if out.len() >= MAX_FONTS {
|
||||
log::warn!("docx: more than {MAX_FONTS} embedded fonts; the rest are not loaded");
|
||||
break;
|
||||
}
|
||||
let Some(rel) = embed.attr("r:id").and_then(|id| part.rels.iter().find(|r| r.id == id && !r.external)) else {
|
||||
continue;
|
||||
};
|
||||
let Some(bytes) = doc.passthrough.get(&rel.target) else {
|
||||
continue;
|
||||
};
|
||||
// No key: the file is stored as is, as with the all-zero key.
|
||||
let key = match embed.attr("w:fontKey") {
|
||||
Some(k) => match font_key(k) {
|
||||
Some(key) => key,
|
||||
None => {
|
||||
log::warn!("docx: embedded font {} has an unreadable key {k:?}", rel.target);
|
||||
continue;
|
||||
}
|
||||
},
|
||||
None => [0; 16],
|
||||
};
|
||||
if !taken.insert((rel.target.as_str(), key, name)) {
|
||||
continue;
|
||||
}
|
||||
// Checked before the copy is made.
|
||||
let Some(left) = budget.checked_sub(bytes.len() as u64) else {
|
||||
over_budget += 1;
|
||||
continue;
|
||||
};
|
||||
budget = left;
|
||||
let mut data = bytes.to_vec();
|
||||
for (i, b) in data.iter_mut().take(32).enumerate() {
|
||||
*b ^= key.get(i % 16).copied().unwrap_or(0);
|
||||
}
|
||||
out.push(EmbeddedFont { name: name.to_string(), data });
|
||||
}
|
||||
if over_budget > 0 {
|
||||
// Once for the document, not once per font.
|
||||
log::warn!("docx: embedded fonts past the {budget_total} byte budget; {over_budget} of them are not loaded");
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The obfuscation key of a `w:fontKey` GUID (`{XXXXXXXX-XXXX-…}`): its 16 bytes, reversed.
|
||||
fn font_key(guid: &str) -> Option<[u8; 16]> {
|
||||
let digits: Vec<u8> = guid.bytes().filter(|c| !matches!(c, b'{' | b'}' | b'-')).collect();
|
||||
if digits.len() != 32 {
|
||||
return None;
|
||||
}
|
||||
let mut key = [0u8; 16];
|
||||
for (i, pair) in digits.chunks(2).enumerate() {
|
||||
let byte = u8::from_str_radix(std::str::from_utf8(pair).ok()?, 16).ok()?;
|
||||
*key.get_mut(15 - i)? = byte;
|
||||
}
|
||||
Some(key)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::*;
|
||||
use crate::package::FONT_TABLE_PART;
|
||||
|
||||
/// A document whose font table makes `refs` references (`(font name, part)`) to font parts
|
||||
/// `parts` (`(name, size)`).
|
||||
fn doc_with(refs: &[(&str, &str)], parts: &[(&str, usize)]) -> Document {
|
||||
let mut table = String::from(
|
||||
r#"<w:fonts xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">"#,
|
||||
);
|
||||
let mut manifest = format!("P\t{FONT_TABLE_PART}\tapplication/xml\n");
|
||||
for (i, (font, part)) in refs.iter().enumerate() {
|
||||
table.push_str(&format!(r#"<w:font w:name="{font}"><w:embedRegular r:id="rId{i}"/></w:font>"#));
|
||||
manifest.push_str(&format!("R\t{FONT_TABLE_PART}\trId{i}\tfont\t{part}\tI\n"));
|
||||
}
|
||||
table.push_str("</w:fonts>");
|
||||
let mut doc = Document::default();
|
||||
doc.passthrough.insert(FONT_TABLE_PART.into(), Arc::new(table.into_bytes()));
|
||||
doc.passthrough.insert(EMBEDDED_PARTS.into(), Arc::new(manifest.into_bytes()));
|
||||
for (part, size) in parts {
|
||||
doc.passthrough.insert((*part).into(), Arc::new(vec![7u8; *size]));
|
||||
}
|
||||
doc
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_font_referenced_again_is_copied_once() {
|
||||
// Each reference was its own copy: the 64 references allowed to one 256 MB part were
|
||||
// 16 GB of copies (#3692).
|
||||
let refs = vec![("Body", "word/fonts/a.odttf"); MAX_FONTS];
|
||||
let fonts = embedded_fonts(&doc_with(&refs, &[("word/fonts/a.odttf", 1000)]));
|
||||
assert_eq!(fonts.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_copies_stop_at_the_byte_budget() {
|
||||
let refs: Vec<(String, &str)> = (0..MAX_FONTS).map(|i| (format!("Font {i}"), "word/fonts/a.odttf")).collect();
|
||||
let refs: Vec<(&str, &str)> = refs.iter().map(|(n, p)| (n.as_str(), *p)).collect();
|
||||
let doc = doc_with(&refs, &[("word/fonts/a.odttf", 1000)]);
|
||||
let (fonts, warned) = crate::test_log::warnings(|| fonts_within(&doc, 3500));
|
||||
assert_eq!(fonts.len(), 3, "three copies fit");
|
||||
let budget: Vec<_> = warned.iter().filter(|w| w.contains("byte budget")).collect();
|
||||
assert_eq!(budget.len(), 1, "one warning for the document, not one per font: {warned:?}");
|
||||
assert!(budget.iter().all(|w| w.contains(&format!("{} of them", MAX_FONTS - 3))), "{budget:?}");
|
||||
assert_eq!(embedded_fonts(&doc).len(), MAX_FONTS, "well within the real budget");
|
||||
}
|
||||
}
|
||||
+130
-158
@@ -1,183 +1,155 @@
|
||||
//! WordCraft DOCX reader and writer (ECMA-376 / ISO/IEC 29500 WordprocessingML).
|
||||
//!
|
||||
//! [`read`] turns a `.docx` package into a [`wordcraft_doc::Document`]; [`write`] does the
|
||||
//! reverse. Both are written from the public specification. The reader is lenient (unknown
|
||||
//! markup is skipped, bad numbers fall back to defaults) and bounded (zip entry sizes, total
|
||||
//! decompressed size, XML depth and element counts are capped), so hostile input yields an
|
||||
//! error or a best-effort document, never a panic.
|
||||
//! Delegates file-format parsing and serialization to `craft-docx`, adapting
|
||||
//! between the shared `craft-wordmodel` and WordCraft's application document model.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
mod bibliography;
|
||||
mod chart_spec;
|
||||
mod crypt;
|
||||
mod custom;
|
||||
mod flat;
|
||||
mod fonts;
|
||||
mod package;
|
||||
mod read;
|
||||
mod smart_art;
|
||||
#[cfg(test)]
|
||||
mod test_log;
|
||||
mod units;
|
||||
mod wordml2003;
|
||||
mod wordml2003_write;
|
||||
mod write;
|
||||
mod xml;
|
||||
mod fit;
|
||||
mod model_bridge;
|
||||
|
||||
pub use bibliography::{MAX_SOURCES, Sources, read_sources};
|
||||
pub use chart_spec::{chart_items, chart_xml};
|
||||
pub use crypt::{
|
||||
DEFAULT_SPIN_COUNT, MAX_PASSWORD_CHARS, PasswordCheck, check_password, check_protection_password, decrypt, encrypt, encrypt_standard_for_tests,
|
||||
encrypt_with_spin_count, is_encrypted, protection_hash,
|
||||
pub use craft_docx::{
|
||||
DEFAULT_SPIN_COUNT, DocxError, EmbeddedFont, Flavor, MAX_PASSWORD_CHARS, MAX_SOURCES, MAX_XML_FILE, PASSWORD_REQUIRED, PasswordCheck,
|
||||
WRONG_PASSWORD, XmlKind, check_password, check_protection_password, encrypt, encrypt_standard_for_tests, encrypt_with_spin_count, package_title,
|
||||
protection_hash, sniff_xml,
|
||||
};
|
||||
pub use flat::{XmlKind, sniff_xml, write as write_flat};
|
||||
pub use fonts::{EmbeddedFont, embedded_fonts};
|
||||
pub use read::{AltChunk, ChunkImporter, ReadOptions, read};
|
||||
pub use smart_art::smart_art_items;
|
||||
pub use wordml2003_write::write as write_wordml2003;
|
||||
pub use write::{write, write_as};
|
||||
|
||||
/// Largest `.xml` file read as a Word XML document.
|
||||
pub const MAX_XML_FILE: usize = 1 << 30;
|
||||
|
||||
/// Read a Word XML file (`.xml`): a Word XML Document (Flat OPC, the whole package in one XML
|
||||
/// file) or a Word 2003 XML Document (WordprocessingML 2003), told apart by the root element.
|
||||
/// Any other XML gives [`DocxError::NotWordXml`].
|
||||
pub fn read_xml(bytes: &[u8]) -> Result<wordcraft_doc::Document, DocxError> {
|
||||
if bytes.len() > MAX_XML_FILE {
|
||||
return Err(DocxError::Limit(format!("XML file of {} bytes", bytes.len())));
|
||||
}
|
||||
match sniff_xml(bytes) {
|
||||
XmlKind::FlatOpc => flat::read(bytes),
|
||||
XmlKind::WordMl2003 => wordml2003::read(bytes),
|
||||
XmlKind::Other => Err(DocxError::NotWordXml),
|
||||
}
|
||||
pub fn decrypt(bytes: &[u8], password: Option<&str>) -> Result<Vec<u8>, DocxError> {
|
||||
wordcraft_doc::cfb_guard::check(bytes).map_err(DocxError::Xml)?;
|
||||
craft_docx::decrypt(bytes, password)
|
||||
}
|
||||
|
||||
/// The title (`dc:title`) in a package's core properties part (`docProps/core.xml`, ECMA-376
|
||||
/// Part 2 §11), trimmed; `None` when the part can't be read or has no title. Only the package's
|
||||
/// directory and at most `max_part` bytes of that part are inflated: for listing templates by
|
||||
/// name without unpacking the whole package. A package over 64 MiB, or one declaring more entries
|
||||
/// than `MAX_ENTRIES` (20,000), has none (#3660).
|
||||
pub fn package_title<R: std::io::Read>(package: R, max_part: u64) -> Option<String> {
|
||||
use std::io::Read;
|
||||
const MAX_PACKAGE: u64 = 64 << 20;
|
||||
let mut bytes = Vec::new();
|
||||
package.take(MAX_PACKAGE + 1).read_to_end(&mut bytes).ok()?;
|
||||
if bytes.len() as u64 > MAX_PACKAGE {
|
||||
return None;
|
||||
}
|
||||
wordcraft_doc::zip_guard::check(&bytes, package::MAX_ENTRIES as u64).ok()?;
|
||||
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).ok()?;
|
||||
let part = zip.by_name("docProps/core.xml").ok()?;
|
||||
let mut xml = Vec::new();
|
||||
part.take(max_part).read_to_end(&mut xml).ok()?;
|
||||
let root = xml::parse(&xml).ok()?;
|
||||
let title = root.child("dc:title")?.text();
|
||||
let title = title.trim();
|
||||
(!title.is_empty()).then(|| title.to_string())
|
||||
pub fn is_encrypted(bytes: &[u8]) -> bool {
|
||||
wordcraft_doc::cfb_guard::check(bytes).is_ok() && craft_docx::is_encrypted(bytes)
|
||||
}
|
||||
|
||||
/// What [`DocxError::PasswordRequired`] says.
|
||||
pub const PASSWORD_REQUIRED: &str = "this document is protected with a password";
|
||||
/// What [`DocxError::WrongPassword`] says.
|
||||
pub const WRONG_PASSWORD: &str = "the password is incorrect";
|
||||
pub use model_bridge::{from_shared_model, to_shared_model};
|
||||
|
||||
/// The `a:ext` URI under a Drawing Canvas's `wpc:extLst` that holds its turn and flips as an
|
||||
/// `a:xfrm` (`rot`, `flipH`, `flipV`). Neither the canvas schema nor `wp:anchor` carries a
|
||||
/// transform, so this rides in the extension list Word and other readers skip.
|
||||
pub(crate) const CANVAS_SPIN_EXT: &str = "urn:wordcraft:canvas-xfrm";
|
||||
/// An alternative format chunk's content, as a [`ChunkImporter`] gets it.
|
||||
pub struct AltChunk<'a> {
|
||||
pub content_type: &'a str,
|
||||
pub part: &'a str,
|
||||
pub bytes: &'a [u8],
|
||||
}
|
||||
|
||||
/// Read a package that may be password-protected ([`is_encrypted`]): it is decrypted with
|
||||
/// `password` first. An encrypted package without a password gives
|
||||
/// [`DocxError::PasswordRequired`]; a wrong one, [`DocxError::WrongPassword`].
|
||||
/// Turns a chunk into a document; `None` leaves the chunk out.
|
||||
pub type ChunkImporter<'a> = &'a dyn Fn(&AltChunk) -> Option<wordcraft_doc::Document>;
|
||||
|
||||
/// How [`read_with`] reads a package.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct ReadOptions<'a> {
|
||||
pub password: Option<&'a str>,
|
||||
pub chunks: Option<ChunkImporter<'a>>,
|
||||
}
|
||||
|
||||
/// Read a package into WordCraft's document model.
|
||||
pub fn read(bytes: &[u8]) -> Result<wordcraft_doc::Document, DocxError> {
|
||||
read_with(bytes, &Default::default())
|
||||
}
|
||||
|
||||
/// Read a package with a password.
|
||||
pub fn read_with_password(bytes: &[u8], password: Option<&str>) -> Result<wordcraft_doc::Document, DocxError> {
|
||||
read_with(bytes, &ReadOptions { password, ..Default::default() })
|
||||
}
|
||||
|
||||
/// Read a package with options: the password of an encrypted one ([`read_with_password`]) and
|
||||
/// an importer for `w:altChunk` content in formats other than WordprocessingML (see
|
||||
/// [`ChunkImporter`]).
|
||||
/// Read a package with options.
|
||||
pub fn read_with(bytes: &[u8], opts: &ReadOptions) -> Result<wordcraft_doc::Document, DocxError> {
|
||||
if is_encrypted(bytes) {
|
||||
let package = zeroize::Zeroizing::new(decrypt(bytes, opts.password)?);
|
||||
return read::read_package(&package, opts.chunks);
|
||||
}
|
||||
read::read_package(bytes, opts.chunks)
|
||||
let bridge_importer = |shared_chunk: &craft_docx::AltChunk| -> Option<craft_wordmodel::Document> {
|
||||
let importer = opts.chunks?;
|
||||
let chunk = AltChunk { content_type: shared_chunk.content_type, part: shared_chunk.part, bytes: shared_chunk.bytes };
|
||||
let doc = importer(&chunk)?;
|
||||
to_shared_model(&doc).ok()
|
||||
};
|
||||
|
||||
let shared_opts =
|
||||
craft_docx::ReadOptions { password: opts.password, chunks: opts.chunks.as_ref().map(|_| &bridge_importer as craft_docx::ChunkImporter) };
|
||||
|
||||
let shared = craft_docx::read_with(bytes, &shared_opts)?;
|
||||
from_shared_model(shared)
|
||||
}
|
||||
|
||||
/// Which kind of WordprocessingML package to write. The main part's content type differs per
|
||||
/// kind, and Word refuses a file whose content type doesn't match its extension.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum Flavor {
|
||||
/// `.docx`
|
||||
#[default]
|
||||
Document,
|
||||
/// `.docm`: may carry a VBA project.
|
||||
MacroDocument,
|
||||
/// `.dotx`
|
||||
Template,
|
||||
/// `.dotm`: may carry a VBA project.
|
||||
MacroTemplate,
|
||||
/// Read a Word XML file (`.xml`).
|
||||
pub fn read_xml(bytes: &[u8]) -> Result<wordcraft_doc::Document, DocxError> {
|
||||
let shared = craft_docx::read_xml(bytes)?;
|
||||
from_shared_model(shared)
|
||||
}
|
||||
|
||||
impl Flavor {
|
||||
/// The flavour a file extension (without the dot, any case) names.
|
||||
pub fn from_ext(ext: &str) -> Option<Flavor> {
|
||||
match ext.to_ascii_lowercase().as_str() {
|
||||
"docx" => Some(Flavor::Document),
|
||||
"docm" => Some(Flavor::MacroDocument),
|
||||
"dotx" => Some(Flavor::Template),
|
||||
"dotm" => Some(Flavor::MacroTemplate),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Content type of the main document part.
|
||||
pub fn main_content_type(self) -> &'static str {
|
||||
match self {
|
||||
Flavor::Document => "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
|
||||
Flavor::MacroDocument => "application/vnd.ms-word.document.macroEnabled.main+xml",
|
||||
Flavor::Template => "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml",
|
||||
Flavor::MacroTemplate => "application/vnd.ms-word.template.macroEnabledTemplate.main+xml",
|
||||
}
|
||||
}
|
||||
|
||||
/// Can this package hold macros?
|
||||
pub fn macros(self) -> bool {
|
||||
matches!(self, Flavor::MacroDocument | Flavor::MacroTemplate)
|
||||
}
|
||||
/// Write a document as `.docx`.
|
||||
pub fn write(doc: &wordcraft_doc::Document) -> Result<Vec<u8>, DocxError> {
|
||||
let shared = to_shared_model(doc)?;
|
||||
craft_docx::write(&shared)
|
||||
}
|
||||
|
||||
/// Errors from reading or writing a DOCX package.
|
||||
#[derive(Debug, thiserror::Error, Clone, PartialEq)]
|
||||
pub enum DocxError {
|
||||
/// The bytes are not a readable zip archive.
|
||||
#[error("not a valid zip package: {0}")]
|
||||
Zip(String),
|
||||
/// A required part is absent.
|
||||
#[error("missing part: {0}")]
|
||||
MissingPart(String),
|
||||
/// A part is not well-formed XML.
|
||||
#[error("malformed XML: {0}")]
|
||||
Xml(String),
|
||||
/// The input exceeds a safety limit (size, nesting, element count).
|
||||
#[error("limit exceeded: {0}")]
|
||||
Limit(String),
|
||||
/// The package is readable but isn't a WordprocessingML document.
|
||||
#[error("not a Word document: {0}")]
|
||||
NotWord(String),
|
||||
/// An `.xml` file that is neither a Word XML Document (Flat OPC) nor Word 2003 XML.
|
||||
#[error("not a Word XML document")]
|
||||
NotWordXml,
|
||||
/// The package is encrypted and no password was given.
|
||||
#[error("{}", PASSWORD_REQUIRED)]
|
||||
PasswordRequired,
|
||||
/// The password doesn't open the package.
|
||||
#[error("{}", WRONG_PASSWORD)]
|
||||
WrongPassword,
|
||||
/// The package is encrypted in a way WordCraft can't read, or its encryption data is damaged
|
||||
/// (or encrypting failed).
|
||||
#[error("encrypted document: {0}")]
|
||||
Encryption(String),
|
||||
/// Write a document with a specific package flavor.
|
||||
pub fn write_as(doc: &wordcraft_doc::Document, flavor: Flavor) -> Result<Vec<u8>, DocxError> {
|
||||
let shared = to_shared_model(doc)?;
|
||||
craft_docx::write_as(&shared, flavor)
|
||||
}
|
||||
|
||||
/// Write a document as a Flat OPC XML file.
|
||||
pub fn write_flat(doc: &wordcraft_doc::Document) -> Result<Vec<u8>, DocxError> {
|
||||
let shared = to_shared_model(doc)?;
|
||||
craft_docx::write_flat(&shared)
|
||||
}
|
||||
|
||||
/// Write a document as Word 2003 XML.
|
||||
pub fn write_wordml2003(doc: &wordcraft_doc::Document) -> Result<Vec<u8>, DocxError> {
|
||||
let shared = to_shared_model(doc)?;
|
||||
craft_docx::write_wordml2003(&shared)
|
||||
}
|
||||
|
||||
/// Draw a chart.
|
||||
pub fn chart_items(spec: &wordcraft_doc::chart::ChartSpec, theme: &[wordcraft_doc::Rgb], w: f32, h: f32) -> Vec<wordcraft_doc::graphic::GraphicItem> {
|
||||
let (Ok(shared_spec), Ok(shared_theme)) = (
|
||||
model_bridge::convert_ref::<_, craft_wordmodel::chart::ChartSpec>(spec),
|
||||
model_bridge::convert_ref::<_, Vec<craft_wordmodel::props::Rgb>>(theme),
|
||||
) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let shared_items = craft_docx::chart_items(&shared_spec, &shared_theme, w, h);
|
||||
model_bridge::convert(shared_items).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Write a chart part.
|
||||
pub fn chart_xml(spec: &wordcraft_doc::chart::ChartSpec) -> Vec<u8> {
|
||||
let Ok(shared_spec) = model_bridge::convert_ref(spec) else {
|
||||
return Vec::new();
|
||||
};
|
||||
craft_docx::chart_xml(&shared_spec).into_bytes()
|
||||
}
|
||||
|
||||
/// Draw SmartArt.
|
||||
pub fn smart_art_items(
|
||||
spec: &wordcraft_doc::smart_art::SmartArtSpec,
|
||||
theme: &[wordcraft_doc::Rgb],
|
||||
w: f32,
|
||||
h: f32,
|
||||
) -> Vec<wordcraft_doc::graphic::GraphicItem> {
|
||||
let (Ok(shared_spec), Ok(shared_theme)) = (
|
||||
model_bridge::convert_ref::<_, craft_wordmodel::smart_art::SmartArtSpec>(spec),
|
||||
model_bridge::convert_ref::<_, Vec<craft_wordmodel::props::Rgb>>(theme),
|
||||
) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let shared_items = craft_docx::smart_art_items(&shared_spec, &shared_theme, w, h);
|
||||
model_bridge::convert(shared_items).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Read bibliography sources from a `b:Sources` file.
|
||||
pub fn read_sources(bytes: &[u8]) -> Result<Sources, DocxError> {
|
||||
let shared = craft_docx::read_sources(bytes)?;
|
||||
Ok(Sources { sources: model_bridge::convert(shared.sources).map_err(|e| DocxError::Xml(e.to_string()))?, style: shared.style })
|
||||
}
|
||||
|
||||
/// The font files embedded in the document's font table.
|
||||
pub fn embedded_fonts(doc: &wordcraft_doc::Document) -> Vec<EmbeddedFont> {
|
||||
let mut shared = craft_wordmodel::Document::new();
|
||||
shared.passthrough = doc.passthrough.clone();
|
||||
craft_docx::embedded_fonts(&shared)
|
||||
}
|
||||
|
||||
/// A `b:Sources` document read into the document model.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Sources {
|
||||
pub sources: Vec<wordcraft_doc::Source>,
|
||||
pub style: Option<String>,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,498 @@
|
||||
//! Bounded conversion between craft-wordmodel and WordCraft's document model.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_docx::DocxError;
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
pub(crate) fn convert<T, U>(value: T) -> Result<U, DocxError>
|
||||
where
|
||||
T: Serialize,
|
||||
U: DeserializeOwned,
|
||||
{
|
||||
let bytes = serde_json::to_vec(&value).map_err(|error| DocxError::Xml(format!("shared model conversion: {error}")))?;
|
||||
drop(value);
|
||||
serde_json::from_slice(&bytes).map_err(|error| DocxError::Xml(format!("shared model conversion: {error}")))
|
||||
}
|
||||
|
||||
pub(crate) fn convert_ref<T, U>(value: &T) -> Result<U, DocxError>
|
||||
where
|
||||
T: Serialize + ?Sized,
|
||||
U: DeserializeOwned,
|
||||
{
|
||||
let bytes = serde_json::to_vec(value).map_err(|error| DocxError::Xml(format!("shared model conversion: {error}")))?;
|
||||
serde_json::from_slice(&bytes).map_err(|error| DocxError::Xml(format!("shared model conversion: {error}")))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// From Shared Model -> WordCraft Document
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type GraphicCache = HashMap<usize, Arc<wordcraft_doc::graphic::Graphic>>;
|
||||
|
||||
fn blocks_from_shared(source: craft_wordmodel::Blocks, graphics: &mut GraphicCache) -> Result<wordcraft_doc::Blocks, DocxError> {
|
||||
source
|
||||
.into_iter()
|
||||
.map(|block| {
|
||||
let block = Arc::try_unwrap(block).unwrap_or_else(|shared| (*shared).clone());
|
||||
block_value_from_shared(block, graphics).map(Arc::new)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn block_value_from_shared(source: craft_wordmodel::Block, graphics: &mut GraphicCache) -> Result<wordcraft_doc::Block, DocxError> {
|
||||
match source {
|
||||
craft_wordmodel::Block::Para(paragraph) => paragraph_value_from_shared(paragraph, graphics).map(wordcraft_doc::Block::Para),
|
||||
craft_wordmodel::Block::Table(table) => table_value_from_shared(table, graphics).map(wordcraft_doc::Block::Table),
|
||||
}
|
||||
}
|
||||
|
||||
fn paragraph_value_from_shared(
|
||||
source: craft_wordmodel::para::Paragraph,
|
||||
graphics: &mut GraphicCache,
|
||||
) -> Result<wordcraft_doc::para::Paragraph, DocxError> {
|
||||
let craft_wordmodel::para::Paragraph { text, runs, props, mark, objects, section, .. } = source;
|
||||
let mut paragraph = wordcraft_doc::para::Paragraph::new();
|
||||
paragraph.text = text;
|
||||
paragraph.runs =
|
||||
runs.into_iter().map(|run| Ok(wordcraft_doc::para::Run { len: run.len, props: convert(run.props)? })).collect::<Result<_, DocxError>>()?;
|
||||
paragraph.props = convert(props)?;
|
||||
paragraph.mark = convert(mark)?;
|
||||
paragraph.objects = objects.into_iter().map(|o| object_value_from_shared(o, graphics)).collect::<Result<_, _>>()?;
|
||||
paragraph.section = section.map(|section| convert(*section).map(Box::new)).transpose()?;
|
||||
Ok(paragraph)
|
||||
}
|
||||
|
||||
fn object_value_from_shared(
|
||||
source: craft_wordmodel::para::InlineObject,
|
||||
graphics: &mut GraphicCache,
|
||||
) -> Result<wordcraft_doc::para::InlineObject, DocxError> {
|
||||
use craft_wordmodel::para::InlineObject as Source;
|
||||
use wordcraft_doc::para::InlineObject as Target;
|
||||
|
||||
match source {
|
||||
Source::Image { media, w, h, alt, float, crop, fx, ole } => Ok(Target::Image {
|
||||
media,
|
||||
w,
|
||||
h,
|
||||
alt,
|
||||
float: convert(float)?,
|
||||
crop,
|
||||
fx: convert(fx)?,
|
||||
ole: ole.map(|o| convert(Arc::try_unwrap(o).unwrap_or_else(|s| (*s).clone())).map(Arc::new)).transpose()?,
|
||||
}),
|
||||
Source::Graphic { w, h, alt, float, graphic } => {
|
||||
let key = Arc::as_ptr(&graphic) as usize;
|
||||
let graphic = if let Some(cached) = graphics.get(&key) {
|
||||
Arc::clone(cached)
|
||||
} else {
|
||||
let converted: Arc<wordcraft_doc::graphic::Graphic> = Arc::new(convert(Arc::try_unwrap(graphic).unwrap_or_else(|s| (*s).clone()))?);
|
||||
graphics.insert(key, Arc::clone(&converted));
|
||||
converted
|
||||
};
|
||||
Ok(Target::Graphic { w, h, alt, float: convert(float)?, graphic })
|
||||
}
|
||||
Source::Group { w, h, float, ch_w, ch_h, children, canvas } => Ok(Target::Group {
|
||||
w,
|
||||
h,
|
||||
float: convert(float)?,
|
||||
ch_w,
|
||||
ch_h,
|
||||
children: children.into_iter().map(|c| group_child_value_from_shared(c, graphics)).collect::<Result<_, _>>()?,
|
||||
canvas: canvas.map(convert).transpose()?,
|
||||
}),
|
||||
other => convert(other),
|
||||
}
|
||||
}
|
||||
|
||||
fn group_child_value_from_shared(
|
||||
source: craft_wordmodel::para::GroupChild,
|
||||
graphics: &mut GraphicCache,
|
||||
) -> Result<wordcraft_doc::para::GroupChild, DocxError> {
|
||||
Ok(wordcraft_doc::para::GroupChild { x: source.x, y: source.y, obj: object_value_from_shared(source.obj, graphics)? })
|
||||
}
|
||||
|
||||
fn table_value_from_shared(source: craft_wordmodel::table::Table, graphics: &mut GraphicCache) -> Result<wordcraft_doc::table::Table, DocxError> {
|
||||
Ok(wordcraft_doc::table::Table {
|
||||
props: convert(source.props)?,
|
||||
grid: source.grid,
|
||||
rows: source.rows.into_iter().map(|r| row_value_from_shared(r, graphics)).collect::<Result<_, _>>()?,
|
||||
controls: convert(source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn row_value_from_shared(source: craft_wordmodel::table::Row, graphics: &mut GraphicCache) -> Result<wordcraft_doc::table::Row, DocxError> {
|
||||
Ok(wordcraft_doc::table::Row {
|
||||
props: convert(source.props)?,
|
||||
cells: source.cells.into_iter().map(|c| cell_value_from_shared(c, graphics)).collect::<Result<_, _>>()?,
|
||||
controls: convert(source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn cell_value_from_shared(source: craft_wordmodel::table::Cell, graphics: &mut GraphicCache) -> Result<wordcraft_doc::table::Cell, DocxError> {
|
||||
Ok(wordcraft_doc::table::Cell {
|
||||
props: convert(source.props)?,
|
||||
blocks: blocks_from_shared(source.blocks, graphics)?,
|
||||
controls: convert(source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn part_value_from_shared(source: craft_wordmodel::Part, graphics: &mut GraphicCache) -> Result<wordcraft_doc::Part, DocxError> {
|
||||
Ok(wordcraft_doc::Part { kind: convert(source.kind)?, blocks: blocks_from_shared(source.blocks, graphics)?, body: convert(source.body)? })
|
||||
}
|
||||
|
||||
fn glossary_value_from_shared(
|
||||
source: craft_wordmodel::BuildingBlock,
|
||||
graphics: &mut GraphicCache,
|
||||
) -> Result<wordcraft_doc::BuildingBlock, DocxError> {
|
||||
Ok(wordcraft_doc::BuildingBlock {
|
||||
name: source.name,
|
||||
gallery: source.gallery,
|
||||
category: source.category,
|
||||
description: source.description,
|
||||
guid: source.guid,
|
||||
style: source.style,
|
||||
types: source.types,
|
||||
behaviors: source.behaviors,
|
||||
blocks: blocks_from_shared(source.blocks, graphics)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn missing_pictures_from_shared(source: craft_wordmodel::MissingPictures) -> Result<wordcraft_doc::MissingPictures, DocxError> {
|
||||
let mut target = wordcraft_doc::MissingPictures::default();
|
||||
target.truncated = source.truncated;
|
||||
target.total = source.total;
|
||||
target.main = source.main;
|
||||
target.part_names = source.part_names;
|
||||
target.media_parts = source.media_parts;
|
||||
for item in source.list {
|
||||
target.list.push(convert(item)?);
|
||||
}
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
/// Converts the shared reader's model without copying the full document or its embedded media.
|
||||
pub fn from_shared_model(mut source: craft_wordmodel::Document) -> Result<wordcraft_doc::Document, DocxError> {
|
||||
let mut fit_state = None;
|
||||
let fit_key = source.passthrough.keys().find(|k| k.eq_ignore_ascii_case(crate::fit::PART)).cloned();
|
||||
if let Some(key) = fit_key
|
||||
&& let Some(xml_bytes) = source.passthrough.get(&key)
|
||||
&& let Some(state) = crate::fit::read_from_bytes(xml_bytes)
|
||||
{
|
||||
fit_state = Some(state);
|
||||
source.passthrough.remove(&key);
|
||||
}
|
||||
|
||||
let craft_wordmodel::Document {
|
||||
body,
|
||||
last_section,
|
||||
parts,
|
||||
styles,
|
||||
numbering,
|
||||
comments,
|
||||
revisions,
|
||||
settings,
|
||||
core,
|
||||
sources,
|
||||
custom_props,
|
||||
footnote_separators,
|
||||
endnote_separators,
|
||||
media,
|
||||
passthrough,
|
||||
glossary,
|
||||
missing_pictures,
|
||||
..
|
||||
} = source;
|
||||
|
||||
let mut graphics = GraphicCache::new();
|
||||
let mut document = wordcraft_doc::Document::new();
|
||||
document.body = blocks_from_shared(body, &mut graphics)?;
|
||||
document.last_section = convert(last_section)?;
|
||||
document.parts = parts.into_iter().map(|(id, part)| Ok((id, part_value_from_shared(part, &mut graphics)?))).collect::<Result<_, DocxError>>()?;
|
||||
document.styles = Arc::new(convert((*styles).clone())?);
|
||||
document.numbering = Arc::new(convert((*numbering).clone())?);
|
||||
document.comments = convert(comments)?;
|
||||
document.revisions = convert(revisions)?;
|
||||
document.settings = convert(settings)?;
|
||||
document.core = convert(core)?;
|
||||
document.sources = convert(sources)?;
|
||||
document.custom_props = convert(custom_props)?;
|
||||
document.footnote_separators = convert(footnote_separators)?;
|
||||
document.endnote_separators = convert(endnote_separators)?;
|
||||
document.media = media;
|
||||
document.passthrough = passthrough;
|
||||
document.glossary = glossary.into_iter().map(|g| glossary_value_from_shared(g, &mut graphics)).collect::<Result<_, _>>()?;
|
||||
document.missing_pictures = missing_pictures_from_shared(missing_pictures)?;
|
||||
document.fit_state = fit_state;
|
||||
Ok(document)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// To Shared Model (from WordCraft Document)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn blocks_to_shared(source: &wordcraft_doc::Blocks) -> Result<craft_wordmodel::Blocks, DocxError> {
|
||||
source.iter().map(|block| block_value_to_shared(block).map(Arc::new)).collect()
|
||||
}
|
||||
|
||||
fn block_value_to_shared(source: &wordcraft_doc::Block) -> Result<craft_wordmodel::Block, DocxError> {
|
||||
match source {
|
||||
wordcraft_doc::Block::Para(paragraph) => paragraph_value_to_shared(paragraph).map(craft_wordmodel::Block::Para),
|
||||
wordcraft_doc::Block::Table(table) => table_value_to_shared(table).map(craft_wordmodel::Block::Table),
|
||||
}
|
||||
}
|
||||
|
||||
fn finite(v: f32) -> f32 {
|
||||
if v.is_finite() { v } else { 0.0 }
|
||||
}
|
||||
|
||||
fn sanitize_para_props(mut props: wordcraft_doc::ParaProps) -> wordcraft_doc::ParaProps {
|
||||
if let Some(ls) = &mut props.line_spacing {
|
||||
match ls {
|
||||
wordcraft_doc::props::LineSpacing::Multiple(v) => *v = finite(*v),
|
||||
wordcraft_doc::props::LineSpacing::Exactly(v) => *v = finite(*v),
|
||||
wordcraft_doc::props::LineSpacing::AtLeast(v) => *v = finite(*v),
|
||||
}
|
||||
}
|
||||
for field in [&mut props.indent_left, &mut props.indent_right, &mut props.indent_first, &mut props.space_before, &mut props.space_after] {
|
||||
if let Some(v) = field
|
||||
&& !v.is_finite()
|
||||
{
|
||||
*v = 0.0;
|
||||
}
|
||||
}
|
||||
props
|
||||
}
|
||||
|
||||
fn sanitize_char_props(mut props: wordcraft_doc::CharProps) -> wordcraft_doc::CharProps {
|
||||
for field in [&mut props.size, &mut props.spacing] {
|
||||
if let Some(v) = field
|
||||
&& !v.is_finite()
|
||||
{
|
||||
*v = 0.0;
|
||||
}
|
||||
}
|
||||
props
|
||||
}
|
||||
|
||||
fn paragraph_value_to_shared(source: &wordcraft_doc::para::Paragraph) -> Result<craft_wordmodel::para::Paragraph, DocxError> {
|
||||
let mut paragraph = craft_wordmodel::para::Paragraph::new();
|
||||
paragraph.text = source.text.clone();
|
||||
paragraph.runs = source
|
||||
.runs
|
||||
.iter()
|
||||
.map(|run| Ok(craft_wordmodel::para::Run { len: run.len, props: convert(sanitize_char_props(run.props.clone()))? }))
|
||||
.collect::<Result<_, DocxError>>()?;
|
||||
paragraph.props = convert(sanitize_para_props(source.props.clone()))?;
|
||||
paragraph.mark = convert(sanitize_char_props(source.mark.clone()))?;
|
||||
paragraph.objects = source.objects.iter().map(object_value_to_shared).collect::<Result<_, _>>()?;
|
||||
paragraph.section = source.section.as_ref().map(|section| convert_ref(section.as_ref()).map(Box::new)).transpose()?;
|
||||
Ok(paragraph)
|
||||
}
|
||||
|
||||
fn object_value_to_shared(source: &wordcraft_doc::para::InlineObject) -> Result<craft_wordmodel::para::InlineObject, DocxError> {
|
||||
use craft_wordmodel::para::InlineObject as Target;
|
||||
use wordcraft_doc::para::InlineObject as Source;
|
||||
|
||||
match source {
|
||||
Source::Image { media, w, h, alt, float, crop, fx, ole } => Ok(Target::Image {
|
||||
media: media.clone(),
|
||||
w: finite(*w),
|
||||
h: finite(*h),
|
||||
alt: alt.clone(),
|
||||
float: convert_ref(float)?,
|
||||
crop: [finite(crop[0]), finite(crop[1]), finite(crop[2]), finite(crop[3])],
|
||||
fx: convert_ref(fx)?,
|
||||
ole: ole.as_ref().map(|o| convert_ref(o.as_ref()).map(Arc::new)).transpose()?,
|
||||
}),
|
||||
Source::Group { w, h, float, ch_w, ch_h, children, canvas } => Ok(Target::Group {
|
||||
w: *w,
|
||||
h: *h,
|
||||
float: convert_ref(float)?,
|
||||
ch_w: *ch_w,
|
||||
ch_h: *ch_h,
|
||||
children: children.iter().map(group_child_value_to_shared).collect::<Result<_, _>>()?,
|
||||
canvas: canvas.as_ref().map(convert_ref).transpose()?,
|
||||
}),
|
||||
other => convert_ref(other),
|
||||
}
|
||||
}
|
||||
|
||||
fn group_child_value_to_shared(source: &wordcraft_doc::para::GroupChild) -> Result<craft_wordmodel::para::GroupChild, DocxError> {
|
||||
Ok(craft_wordmodel::para::GroupChild { x: source.x, y: source.y, obj: object_value_to_shared(&source.obj)? })
|
||||
}
|
||||
|
||||
fn table_value_to_shared(source: &wordcraft_doc::table::Table) -> Result<craft_wordmodel::table::Table, DocxError> {
|
||||
Ok(craft_wordmodel::table::Table {
|
||||
props: convert_ref(&source.props)?,
|
||||
grid: source.grid.clone(),
|
||||
rows: source.rows.iter().map(row_value_to_shared).collect::<Result<_, _>>()?,
|
||||
controls: convert_ref(&source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn row_value_to_shared(source: &wordcraft_doc::table::Row) -> Result<craft_wordmodel::table::Row, DocxError> {
|
||||
Ok(craft_wordmodel::table::Row {
|
||||
props: convert_ref(&source.props)?,
|
||||
cells: source.cells.iter().map(cell_value_to_shared).collect::<Result<_, _>>()?,
|
||||
controls: convert_ref(&source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn cell_value_to_shared(source: &wordcraft_doc::table::Cell) -> Result<craft_wordmodel::table::Cell, DocxError> {
|
||||
Ok(craft_wordmodel::table::Cell {
|
||||
props: convert_ref(&source.props)?,
|
||||
blocks: blocks_to_shared(&source.blocks)?,
|
||||
controls: convert_ref(&source.controls)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn part_value_to_shared(source: &wordcraft_doc::Part) -> Result<craft_wordmodel::Part, DocxError> {
|
||||
Ok(craft_wordmodel::Part { kind: convert_ref(&source.kind)?, blocks: blocks_to_shared(&source.blocks)?, body: convert_ref(&source.body)? })
|
||||
}
|
||||
|
||||
fn glossary_value_to_shared(source: &wordcraft_doc::BuildingBlock) -> Result<craft_wordmodel::BuildingBlock, DocxError> {
|
||||
Ok(craft_wordmodel::BuildingBlock {
|
||||
name: source.name.clone(),
|
||||
gallery: source.gallery.clone(),
|
||||
category: source.category.clone(),
|
||||
description: source.description.clone(),
|
||||
guid: source.guid.clone(),
|
||||
style: source.style.clone(),
|
||||
types: source.types.clone(),
|
||||
behaviors: source.behaviors.clone(),
|
||||
blocks: blocks_to_shared(&source.blocks)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn missing_pictures_to_shared(source: &wordcraft_doc::MissingPictures) -> Result<craft_wordmodel::MissingPictures, DocxError> {
|
||||
let mut target = craft_wordmodel::MissingPictures::default();
|
||||
target.truncated = source.truncated;
|
||||
target.total = source.total;
|
||||
target.main = source.main.clone();
|
||||
target.part_names = source.part_names.clone();
|
||||
target.media_parts = source.media_parts.clone();
|
||||
for item in &source.list {
|
||||
target.list.push(convert_ref(item)?);
|
||||
}
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
/// Converts a WordCraft document to the shared model for serialization.
|
||||
pub fn to_shared_model(source: &wordcraft_doc::Document) -> Result<craft_wordmodel::Document, DocxError> {
|
||||
let mut document = craft_wordmodel::Document::new();
|
||||
document.body = blocks_to_shared(&source.body)?;
|
||||
document.last_section = convert_ref(&source.last_section)?;
|
||||
document.parts = source.parts.iter().map(|(&id, part)| Ok((id, part_value_to_shared(part)?))).collect::<Result<_, DocxError>>()?;
|
||||
document.styles = Arc::new(convert_ref(source.styles.as_ref())?);
|
||||
document.numbering = Arc::new(convert_ref(source.numbering.as_ref())?);
|
||||
document.comments = convert_ref(&source.comments)?;
|
||||
document.revisions = convert_ref(&source.revisions)?;
|
||||
document.settings = convert_ref(&source.settings)?;
|
||||
document.core = convert_ref(&source.core)?;
|
||||
document.sources = convert_ref(&source.sources)?;
|
||||
document.custom_props = convert_ref(&source.custom_props)?;
|
||||
document.footnote_separators = convert_ref(&source.footnote_separators)?;
|
||||
document.endnote_separators = convert_ref(&source.endnote_separators)?;
|
||||
document.media = source.media.clone();
|
||||
document.passthrough = source.passthrough.clone();
|
||||
document.glossary = source.glossary.iter().map(glossary_value_to_shared).collect::<Result<_, _>>()?;
|
||||
document.missing_pictures = missing_pictures_to_shared(&source.missing_pictures)?;
|
||||
|
||||
// Handle fit_state preservation through custom XML passthrough
|
||||
if let Some(fit) = &source.fit_state {
|
||||
let xml = crate::fit::write(fit)?;
|
||||
document.passthrough.insert(crate::fit::PART.into(), Arc::new(xml));
|
||||
const CUSTOM_XML_ROOTS: &str = "wordcraft:customXml.roots";
|
||||
const EMBEDDED_PARTS: &str = "wordcraft:embedded.parts";
|
||||
|
||||
let manifest_entry = format!("P\t{}\tapplication/xml\n", crate::fit::PART);
|
||||
let manifest = match document.passthrough.get_mut(EMBEDDED_PARTS) {
|
||||
Some(existing) => {
|
||||
let mut text = String::from_utf8_lossy(existing).to_string();
|
||||
if !text.contains(crate::fit::PART) {
|
||||
text.push_str(&manifest_entry);
|
||||
}
|
||||
Arc::new(text.into_bytes())
|
||||
}
|
||||
None => Arc::new(manifest_entry.into_bytes()),
|
||||
};
|
||||
document.passthrough.insert(EMBEDDED_PARTS.into(), manifest);
|
||||
|
||||
let root_entry = format!("D\thttp://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml\t{}\n", crate::fit::PART);
|
||||
let roots = match document.passthrough.get_mut(CUSTOM_XML_ROOTS) {
|
||||
Some(existing) => {
|
||||
let mut text = String::from_utf8_lossy(existing).to_string();
|
||||
if !text.contains(crate::fit::PART) {
|
||||
text.push_str(&root_entry);
|
||||
}
|
||||
Arc::new(text.into_bytes())
|
||||
}
|
||||
None => Arc::new(root_entry.into_bytes()),
|
||||
};
|
||||
document.passthrough.insert(CUSTOM_XML_ROOTS.into(), roots);
|
||||
}
|
||||
|
||||
Ok(document)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bridges_nested_stories_and_moves_media_both_directions() {
|
||||
let mut source = craft_wordmodel::Document::new();
|
||||
let long_text = "x".repeat(1 << 16);
|
||||
let mut body_para = craft_wordmodel::para::Paragraph::with_text(&long_text, Default::default());
|
||||
body_para.objects.push(craft_wordmodel::para::InlineObject::Group {
|
||||
w: 32.0,
|
||||
h: 24.0,
|
||||
float: Default::default(),
|
||||
ch_w: 64.0,
|
||||
ch_h: 48.0,
|
||||
children: vec![craft_wordmodel::para::GroupChild {
|
||||
x: 4.0,
|
||||
y: 6.0,
|
||||
obj: craft_wordmodel::para::InlineObject::Field {
|
||||
instr: "PAGE".into(),
|
||||
result: "7".into(),
|
||||
locked: true,
|
||||
dirty: false,
|
||||
code: false,
|
||||
form: String::new(),
|
||||
},
|
||||
}],
|
||||
canvas: None,
|
||||
});
|
||||
let cell = craft_wordmodel::table::Cell::with_text("nested cell");
|
||||
let table = craft_wordmodel::table::Table {
|
||||
rows: vec![craft_wordmodel::table::Row { cells: vec![cell], ..Default::default() }],
|
||||
..Default::default()
|
||||
};
|
||||
source.body = vec![Arc::new(craft_wordmodel::Block::Para(body_para)), Arc::new(craft_wordmodel::Block::Table(table))];
|
||||
source.parts.insert(
|
||||
12,
|
||||
craft_wordmodel::Part {
|
||||
kind: craft_wordmodel::PartKind::TextBox,
|
||||
blocks: vec![craft_wordmodel::para_block(craft_wordmodel::para::Paragraph::with_text("part story", Default::default()))],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
source.settings.compat_mode = 11;
|
||||
let image = Arc::new(vec![0x5a; 1 << 16]);
|
||||
source.media.insert("image1.png".into(), image.clone());
|
||||
|
||||
let doc = from_shared_model(source).expect("from_shared_model");
|
||||
assert_eq!(doc.body.len(), 2);
|
||||
assert_eq!(doc.body[0].as_para().map(|p| p.text.as_str()), Some(long_text.as_str()));
|
||||
assert!(Arc::ptr_eq(doc.media.get("image1.png").expect("media"), &image));
|
||||
|
||||
let back = to_shared_model(&doc).expect("to_shared_model");
|
||||
assert_eq!(back.body.len(), 2);
|
||||
assert_eq!(back.body[0].as_para().map(|p| p.text.as_str()), Some(long_text.as_str()));
|
||||
assert!(Arc::ptr_eq(back.media.get("image1.png").expect("media"), &image));
|
||||
}
|
||||
}
|
||||
@@ -1,655 +0,0 @@
|
||||
//! OPC package access: bounded zip reading, relationships, part names.
|
||||
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::io::{Cursor, Read, Write};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::DocxError;
|
||||
use crate::xml::{self, El};
|
||||
|
||||
/// Largest single part we decompress.
|
||||
pub const MAX_PART: u64 = 256 * 1024 * 1024;
|
||||
/// Largest total of all decompressed parts.
|
||||
pub const MAX_TOTAL: u64 = 512 * 1024 * 1024;
|
||||
/// Most zip entries we look at.
|
||||
pub const MAX_ENTRIES: usize = 20_000;
|
||||
|
||||
/// Relationship type suffixes (after `.../relationships/`).
|
||||
pub mod rt {
|
||||
pub const OFFICE_DOC: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
|
||||
pub const CORE: &str = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";
|
||||
pub const APP: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
|
||||
pub const CUSTOM: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties";
|
||||
pub const CUSTOM_XML_PROPS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps";
|
||||
pub const STYLES: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
|
||||
pub const NUMBERING: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering";
|
||||
pub const SETTINGS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings";
|
||||
pub const ATTACHED_TEMPLATE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate";
|
||||
pub const FONT_TABLE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable";
|
||||
pub const THEME: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
|
||||
pub const FOOTNOTES: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes";
|
||||
pub const ENDNOTES: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes";
|
||||
pub const COMMENTS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
|
||||
pub const COMMENTS_EX: &str = "http://schemas.microsoft.com/office/2011/relationships/commentsExtended";
|
||||
pub const COMMENTS_IDS: &str = "http://schemas.microsoft.com/office/2016/09/relationships/commentsIds";
|
||||
pub const COMMENTS_EXTENSIBLE: &str = "http://schemas.microsoft.com/office/2018/08/relationships/commentsExtensible";
|
||||
pub const HEADER: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header";
|
||||
pub const FOOTER: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer";
|
||||
pub const IMAGE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
pub const HYPERLINK: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
|
||||
pub const VBA_PROJECT: &str = "http://schemas.microsoft.com/office/2006/relationships/vbaProject";
|
||||
pub const CHART: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart";
|
||||
pub const DIAGRAM_DATA: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData";
|
||||
pub const DIAGRAM_DRAWING: &str = "http://schemas.microsoft.com/office/2007/relationships/diagramDrawing";
|
||||
pub const DIAGRAM_LAYOUT: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramLayout";
|
||||
pub const DIAGRAM_QUICK_STYLE: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramQuickStyle";
|
||||
pub const DIAGRAM_COLORS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramColors";
|
||||
pub const CUSTOM_XML: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";
|
||||
pub const GLOSSARY: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument";
|
||||
pub const AF_CHUNK: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk";
|
||||
}
|
||||
|
||||
/// Content type of a glossary document part (ECMA-376 Part 1 §11.3.8).
|
||||
pub const GLOSSARY_CONTENT_TYPE: &str = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml";
|
||||
/// `Document::passthrough` key: a fingerprint of `Document::glossary` as read, so a save can tell
|
||||
/// an untouched glossary (written back verbatim) from an edited one (written from the model).
|
||||
/// Not a package part: WordCraft's own bookkeeping.
|
||||
pub const GLOSSARY_READ: &str = "wordcraft:glossary.read";
|
||||
/// Most building blocks read from one glossary.
|
||||
pub const MAX_GLOSSARY_ENTRIES: usize = 10_000;
|
||||
/// The glossary entry types `w:type` takes (ST_DocPartType); others aren't kept.
|
||||
pub const GLOSSARY_TYPES: [&str; 7] = ["none", "normal", "autoExp", "toolbar", "speller", "formFld", "bbPlcHdr"];
|
||||
/// The insertion behaviours `w:behavior` takes (ST_DocPartBehavior); others aren't kept.
|
||||
pub const GLOSSARY_BEHAVIORS: [&str; 3] = ["content", "p", "pg"];
|
||||
|
||||
/// A fingerprint of `glossary` (FNV-1a over its debug form): stable across runs and builds that
|
||||
/// keep the model's shape, and cheap (nothing is allocated).
|
||||
pub fn glossary_fingerprint(glossary: &[wordcraft_doc::BuildingBlock]) -> String {
|
||||
struct Fnv(u64);
|
||||
impl std::fmt::Write for Fnv {
|
||||
fn write_str(&mut self, s: &str) -> std::fmt::Result {
|
||||
for b in s.bytes() {
|
||||
self.0 = (self.0 ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let mut h = Fnv(0xcbf2_9ce4_8422_2325);
|
||||
// Writing into `Fnv` never fails.
|
||||
let _ = std::fmt::Write::write_fmt(&mut h, format_args!("{glossary:?}"));
|
||||
format!("{:016x}", h.0)
|
||||
}
|
||||
|
||||
/// `Document::passthrough` key for a macro project, kept as opaque bytes (never parsed or run).
|
||||
pub const VBA_PROJECT_PART: &str = "word/vbaProject.bin";
|
||||
/// `Document::passthrough` key listing the parts the project relates to (VBA data, signatures…),
|
||||
/// one `relationship type \t part name \t content type` line each; each part's bytes are kept
|
||||
/// under its part name. Not a package part: WordCraft's own bookkeeping.
|
||||
pub const VBA_RELATED: &str = "wordcraft:vbaProject.related";
|
||||
/// Most parts we carry along with a macro project.
|
||||
pub const MAX_VBA_RELATED: usize = 64;
|
||||
|
||||
/// `Document::passthrough` key listing the parts that objects read from a file refer to (charts,
|
||||
/// SmartArt diagrams, OLE objects: see `wordcraft_doc::graphic::Embedded`), with their content
|
||||
/// types and own relationships, as [`EmbeddedManifest`] lines. Each part's bytes are kept under its
|
||||
/// part name. Not a package part: WordCraft's own bookkeeping.
|
||||
pub const EMBEDDED_PARTS: &str = "wordcraft:embedded.parts";
|
||||
/// The font table kept from a file (read by `Reader::keep_part_tree`), with the fonts it embeds.
|
||||
pub const FONT_TABLE_PART: &str = "word/fontTable.xml";
|
||||
|
||||
/// The spelling the font table is stored under in `parts` (part names are case-insensitive, so a
|
||||
/// package may spell it `WORD/FONTTABLE.XML`), preferring the exact [`FONT_TABLE_PART`].
|
||||
pub fn font_table_key<V>(parts: &BTreeMap<String, V>) -> Option<&str> {
|
||||
if let Some((k, _)) = parts.get_key_value(FONT_TABLE_PART) {
|
||||
return Some(k.as_str());
|
||||
}
|
||||
parts.keys().find(|k| k.eq_ignore_ascii_case(FONT_TABLE_PART)).map(String::as_str)
|
||||
}
|
||||
/// Custom XML roots kept by the package (P) or main document (D): `owner\ttype\tpart`.
|
||||
/// Their opaque bytes and related parts use the same bounded manifest as embedded objects.
|
||||
pub const CUSTOM_XML_ROOTS: &str = "wordcraft:customXml.roots";
|
||||
/// The original `word/settings.xml` when it holds settings the model doesn't (merged into on
|
||||
/// save by `write::settings_merge`). Not a package part: WordCraft's own bookkeeping.
|
||||
pub const SETTINGS_ORIG: &str = "wordcraft:settings.orig";
|
||||
/// Extended properties (`docProps/app.xml`) the model doesn't hold but a save keeps
|
||||
/// ([`APP_KEPT_PROPS`]): `name\tvalue` lines. Not a package part: WordCraft's own bookkeeping.
|
||||
pub const APP_KEPT: &str = "wordcraft:app.kept";
|
||||
/// The `docProps/app.xml` properties carried over from the file. Counts (pages, lines,
|
||||
/// characters, editing time) and the producing application's version are not: they describe
|
||||
/// the file Word saved, not the one WordCraft writes.
|
||||
pub const APP_KEPT_PROPS: &[&str] =
|
||||
&["Template", "Manager", "Company", "HyperlinkBase", "ScaleCrop", "LinksUpToDate", "SharedDoc", "HyperlinksChanged"];
|
||||
/// Parts WordCraft doesn't model, carried verbatim: `owner\ttype\tpart` lines (owner `P` for
|
||||
/// the package, `D` for the main document), with their bytes and relationships in the
|
||||
/// [`EmbeddedManifest`]. Not a package part: WordCraft's own bookkeeping.
|
||||
pub const UNMODELLED_ROOTS: &str = "wordcraft:unmodelled.roots";
|
||||
/// Most parts we carry along for a file's embedded objects.
|
||||
pub const MAX_EMBEDDED_PARTS: usize = 4096;
|
||||
/// Most relationships we keep for one such part.
|
||||
pub const MAX_EMBEDDED_RELS: usize = 1024;
|
||||
/// Longest markup we keep for one embedded object.
|
||||
pub const MAX_EMBEDDED_XML: usize = 1024 * 1024;
|
||||
|
||||
/// One part an embedded object needs: its content type and its own relationships (internal
|
||||
/// targets are part names).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct EmbeddedPart {
|
||||
pub content_type: String,
|
||||
pub rels: Vec<Rel>,
|
||||
}
|
||||
|
||||
/// The parts kept for embedded objects, by part name (see [`EMBEDDED_PARTS`]). Stored as text:
|
||||
/// `P\tpart\tcontent type` per part, then `R\tpart\tid\ttype\ttarget\tI|E` per relationship of
|
||||
/// that part (`E`: `target` is an external URL).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct EmbeddedManifest {
|
||||
pub parts: BTreeMap<String, EmbeddedPart>,
|
||||
}
|
||||
|
||||
/// Can `s` be a field of a manifest line?
|
||||
pub fn manifest_field_ok(s: &str) -> bool {
|
||||
!s.is_empty() && !s.contains(['\t', '\r', '\n'])
|
||||
}
|
||||
|
||||
impl EmbeddedManifest {
|
||||
pub fn parse(bytes: &[u8]) -> EmbeddedManifest {
|
||||
let mut m = EmbeddedManifest::default();
|
||||
for line in String::from_utf8_lossy(bytes).lines() {
|
||||
let f: Vec<&str> = line.split('\t').collect();
|
||||
match f.as_slice() {
|
||||
["P", part, ct] if m.parts.len() < MAX_EMBEDDED_PARTS => {
|
||||
m.parts.entry((*part).to_string()).or_insert_with(|| EmbeddedPart { content_type: (*ct).to_string(), rels: Vec::new() });
|
||||
}
|
||||
["R", part, id, kind, target, mode] => {
|
||||
if let Some(p) = m.parts.get_mut(*part)
|
||||
&& p.rels.len() < MAX_EMBEDDED_RELS
|
||||
&& !p.rels.iter().any(|r| r.id == *id)
|
||||
{
|
||||
p.rels.push(Rel { id: (*id).to_string(), kind: (*kind).to_string(), target: (*target).to_string(), external: *mode == "E" });
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut s = String::new();
|
||||
for (part, p) in &self.parts {
|
||||
if !manifest_field_ok(part) || !manifest_field_ok(&p.content_type) {
|
||||
continue;
|
||||
}
|
||||
s.push_str(&format!("P\t{part}\t{}\n", p.content_type));
|
||||
}
|
||||
for (part, p) in &self.parts {
|
||||
for r in p.rels.iter().filter(|r| [&r.id, &r.kind, &r.target].iter().all(|f| manifest_field_ok(f))) {
|
||||
s.push_str(&format!("R\t{part}\t{}\t{}\t{}\t{}\n", r.id, r.kind, r.target, if r.external { "E" } else { "I" }));
|
||||
}
|
||||
}
|
||||
s.into_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
/// Does relationship type `t` end with `suffix` (ignoring the transitional/strict prefix)?
|
||||
pub fn rel_is(t: &str, full: &str) -> bool {
|
||||
let tail = |s: &str| s.rsplit('/').next().unwrap_or(s).to_string();
|
||||
t == full || tail(t) == tail(full)
|
||||
}
|
||||
|
||||
/// The decompressed parts of a package (bounded). Shared, so a part the document keeps as it is (a
|
||||
/// picture, an embedded object) is handed over rather than copied.
|
||||
pub struct Package {
|
||||
files: BTreeMap<String, Arc<Vec<u8>>>,
|
||||
/// Lowercased part name → its stored spelling (the first in name order): part names are
|
||||
/// case-insensitive (ECMA-376 Part 2 §6.2.2.3), so every spelling is one part.
|
||||
lower: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Package {
|
||||
pub fn open(bytes: &[u8]) -> Result<Package, DocxError> {
|
||||
Package::open_limited(bytes, MAX_PART, MAX_TOTAL)
|
||||
}
|
||||
|
||||
pub fn open_limited(bytes: &[u8], max_part: u64, max_total: u64) -> Result<Package, DocxError> {
|
||||
// Before `zip` parses every entry the end record declares (#3660).
|
||||
wordcraft_doc::zip_guard::check(bytes, MAX_ENTRIES as u64).map_err(DocxError::Limit)?;
|
||||
let mut zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| DocxError::Zip(e.to_string()))?;
|
||||
if zip.len() > MAX_ENTRIES {
|
||||
return Err(DocxError::Limit(format!("{} zip entries", zip.len())));
|
||||
}
|
||||
let mut files = BTreeMap::new();
|
||||
let mut total: u64 = 0;
|
||||
for i in 0..zip.len() {
|
||||
let mut f = match zip.by_index(i) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
log::warn!("docx: skipping unreadable zip entry {i}: {e}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if f.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let name = f.name().trim_start_matches('/').to_string();
|
||||
if f.size() > max_part {
|
||||
return Err(DocxError::Limit(format!("part {name} declares {} bytes", f.size())));
|
||||
}
|
||||
let room = max_part.min(max_total.saturating_sub(total));
|
||||
let mut buf = Vec::with_capacity(f.size().min(16 * 1024 * 1024) as usize);
|
||||
let read = (&mut f).take(room + 1).read_to_end(&mut buf);
|
||||
// Charged whether or not the read succeeds: an entry inflates in full before its CRC
|
||||
// is checked, so failed entries would otherwise inflate without limit (#3723).
|
||||
total = total.saturating_add(buf.len() as u64);
|
||||
if buf.len() as u64 > room {
|
||||
return Err(DocxError::Limit(format!("part {name} decompresses past the size limit")));
|
||||
}
|
||||
if let Err(e) = read {
|
||||
// A corrupt entry: keep going unless it's a part we can't do without.
|
||||
log::warn!("docx: zip entry {name} unreadable: {e}");
|
||||
continue;
|
||||
}
|
||||
files.insert(name, Arc::new(buf));
|
||||
}
|
||||
Ok(Package::new(files))
|
||||
}
|
||||
|
||||
/// The bytes of all its parts.
|
||||
pub fn size(&self) -> u64 {
|
||||
self.files.values().map(|f| f.len() as u64).sum()
|
||||
}
|
||||
|
||||
/// A package from parts already in memory (a Flat OPC file, a converted document), by part
|
||||
/// name without the leading `/`.
|
||||
pub fn from_parts(files: BTreeMap<String, Vec<u8>>) -> Package {
|
||||
Package::new(files.into_iter().map(|(name, bytes)| (name, Arc::new(bytes))).collect())
|
||||
}
|
||||
|
||||
fn new(mut files: BTreeMap<String, Arc<Vec<u8>>>) -> Package {
|
||||
let mut lower: HashMap<String, String> = HashMap::new();
|
||||
let mut collides = false;
|
||||
for name in files.keys() {
|
||||
match lower.entry(name.to_ascii_lowercase()) {
|
||||
Entry::Occupied(_) => collides = true,
|
||||
Entry::Vacant(v) => {
|
||||
v.insert(name.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
if !collides {
|
||||
return Package { files, lower };
|
||||
}
|
||||
// Entries whose names differ only in case are one part, so every spelling of the name
|
||||
// reaches the same stored entry (#3704). The spelling kept is the one a relationship
|
||||
// names, so the part the document points at is the one read; the first in byte order only
|
||||
// when no relationship names any of them.
|
||||
let named = rel_targets(&files);
|
||||
for name in files.keys().rev().filter(|n| named.contains(n.as_str())) {
|
||||
lower.insert(name.to_ascii_lowercase(), name.clone());
|
||||
}
|
||||
let before = files.len();
|
||||
files.retain(|name, _| lower.get(&name.to_ascii_lowercase()) == Some(name));
|
||||
let dropped = before.saturating_sub(files.len());
|
||||
log::warn!("docx: {dropped} zip entries are other spellings of parts already read; they are left out");
|
||||
Package { files, lower }
|
||||
}
|
||||
|
||||
/// The stored spelling of part `name` (leading `/` ignored; case-insensitive).
|
||||
pub fn canonical(&self, name: &str) -> Option<&str> {
|
||||
self.lower.get(&name.trim_start_matches('/').to_ascii_lowercase()).map(String::as_str)
|
||||
}
|
||||
|
||||
/// Part bytes by name (leading `/` ignored; case-insensitive fallback).
|
||||
pub fn get(&self, name: &str) -> Option<&[u8]> {
|
||||
self.get_shared(name).map(|v| v.as_slice())
|
||||
}
|
||||
|
||||
/// [`Package::get`], as the shared bytes themselves: for a part the document keeps whole, which
|
||||
/// then costs no copy (a picture document's pictures used to be held twice while it opened).
|
||||
pub fn get_shared(&self, name: &str) -> Option<&Arc<Vec<u8>>> {
|
||||
self.canonical(name).and_then(|n| self.files.get(n))
|
||||
}
|
||||
|
||||
/// [`Package::xml`] for the main document part: malformed XML is an error, not a truncated read.
|
||||
pub fn xml_strict(&self, name: &str) -> Result<Option<El>, DocxError> {
|
||||
match self.get(name) {
|
||||
Some(b) => xml::parse_strict(b).map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn xml(&self, name: &str) -> Result<Option<El>, DocxError> {
|
||||
match self.get(name) {
|
||||
Some(b) => xml::parse(b).map(Some),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Relationships of `part` (from `dir/_rels/name.rels`).
|
||||
pub fn rels(&self, part: &str) -> Rels {
|
||||
let (dir, file) = split_dir(part);
|
||||
let path = if dir.is_empty() { format!("_rels/{file}.rels") } else { format!("{dir}/_rels/{file}.rels") };
|
||||
let mut rels = Rels::default();
|
||||
let root = match self.xml(&path) {
|
||||
Ok(Some(root)) => root,
|
||||
Ok(None) => return rels,
|
||||
Err(e) => {
|
||||
log::warn!("docx: ignoring unreadable relationships {path}: {e}");
|
||||
return rels;
|
||||
}
|
||||
};
|
||||
for r in root.els().filter(|e| e.local() == "Relationship") {
|
||||
let (Some(id), Some(target)) = (r.attr("Id"), r.attr("Target")) else { continue };
|
||||
let external = r.attr("TargetMode").is_some_and(|m| m.eq_ignore_ascii_case("External"));
|
||||
// Internal targets name the part as stored, so a part reached under several spellings
|
||||
// is kept, cached and written once (#3704).
|
||||
let resolved = if external {
|
||||
target.to_string()
|
||||
} else {
|
||||
let path = resolve(dir, target);
|
||||
self.canonical(&path).map_or(path, str::to_string)
|
||||
};
|
||||
rels.list.push(Rel { id: id.to_string(), kind: r.attr("Type").unwrap_or("").to_string(), target: resolved, external });
|
||||
}
|
||||
rels
|
||||
}
|
||||
}
|
||||
|
||||
/// `[Content_Types].xml`: Override by part name, then Default by extension (both case-insensitive).
|
||||
#[derive(Default)]
|
||||
pub struct ContentTypes {
|
||||
overrides: HashMap<String, String>,
|
||||
defaults: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl ContentTypes {
|
||||
pub fn read(pkg: &Package) -> ContentTypes {
|
||||
let mut ct = ContentTypes::default();
|
||||
let root = match pkg.xml("[Content_Types].xml") {
|
||||
Ok(Some(root)) => root,
|
||||
Ok(None) => return ct,
|
||||
Err(e) => {
|
||||
log::warn!("docx: ignoring unreadable [Content_Types].xml: {e}");
|
||||
return ct;
|
||||
}
|
||||
};
|
||||
for e in root.els() {
|
||||
let (Some(key), Some(t)) = (e.attr("PartName").or_else(|| e.attr("Extension")), e.attr("ContentType")) else { continue };
|
||||
let map = if e.local() == "Override" { &mut ct.overrides } else { &mut ct.defaults };
|
||||
map.entry(key.trim_start_matches('/').to_ascii_lowercase()).or_insert_with(|| t.to_string());
|
||||
}
|
||||
ct
|
||||
}
|
||||
|
||||
/// Content type of `part` (a package path without the leading `/`).
|
||||
pub fn of(&self, part: &str) -> Option<&str> {
|
||||
let part = part.trim_start_matches('/').to_ascii_lowercase();
|
||||
if let Some(t) = self.overrides.get(&part) {
|
||||
return Some(t);
|
||||
}
|
||||
let ext = part.rsplit_once('.').map(|(_, e)| e)?;
|
||||
self.defaults.get(ext).map(String::as_str)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Rel {
|
||||
pub id: String,
|
||||
pub kind: String,
|
||||
/// Package path (internal) or URL (external).
|
||||
pub target: String,
|
||||
pub external: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Rels {
|
||||
pub list: Vec<Rel>,
|
||||
}
|
||||
|
||||
impl Rels {
|
||||
pub fn by_id(&self, id: &str) -> Option<&Rel> {
|
||||
self.list.iter().find(|r| r.id == id)
|
||||
}
|
||||
pub fn by_type(&self, full: &str) -> Option<&Rel> {
|
||||
self.list.iter().find(|r| rel_is(&r.kind, full))
|
||||
}
|
||||
}
|
||||
|
||||
fn split_dir(part: &str) -> (&str, &str) {
|
||||
let p = part.trim_start_matches('/');
|
||||
match p.rfind('/') {
|
||||
Some(i) => (p.get(..i).unwrap_or(""), p.get(i + 1..).unwrap_or(p)),
|
||||
None => ("", p),
|
||||
}
|
||||
}
|
||||
|
||||
/// Every internal part name the relationship parts in `files` target, spelled as they spell it.
|
||||
fn rel_targets(files: &BTreeMap<String, Arc<Vec<u8>>>) -> HashSet<String> {
|
||||
let mut named = HashSet::new();
|
||||
for (name, bytes) in files {
|
||||
// `dir/_rels/file.rels` relates `dir/file`, so its targets resolve against `dir`.
|
||||
let lower = name.to_ascii_lowercase();
|
||||
let Some(at) = lower.rfind("_rels/").filter(|_| lower.ends_with(".rels")) else { continue };
|
||||
let dir = name.get(..at).unwrap_or("").trim_end_matches('/');
|
||||
let Ok(root) = xml::parse(bytes) else { continue };
|
||||
for r in root.els().filter(|e| e.local() == "Relationship") {
|
||||
let external = r.attr("TargetMode").is_some_and(|m| m.eq_ignore_ascii_case("External"));
|
||||
if let (Some(target), false) = (r.attr("Target"), external) {
|
||||
named.insert(resolve(dir, target));
|
||||
}
|
||||
}
|
||||
}
|
||||
named
|
||||
}
|
||||
|
||||
/// Resolve a relative target against a source directory into a package path.
|
||||
pub fn resolve(dir: &str, target: &str) -> String {
|
||||
let target = target.split('#').next().unwrap_or(target);
|
||||
let mut parts: Vec<&str> = if target.starts_with('/') { Vec::new() } else { dir.split('/').filter(|s| !s.is_empty()).collect() };
|
||||
for seg in target.split('/') {
|
||||
match seg {
|
||||
"" | "." => {}
|
||||
".." => {
|
||||
parts.pop();
|
||||
}
|
||||
s => parts.push(s),
|
||||
}
|
||||
}
|
||||
parts.join("/")
|
||||
}
|
||||
|
||||
/// The relative reference from part `from` to part `to` (both package paths), as a
|
||||
/// relationship target: `word/document.xml` → `word/charts/chart1.xml` is `charts/chart1.xml`.
|
||||
pub fn relative(from: &str, to: &str) -> String {
|
||||
let from_dir: Vec<&str> = from.split('/').filter(|s| !s.is_empty()).collect();
|
||||
let from_dir = from_dir.get(..from_dir.len().saturating_sub(1)).unwrap_or(&[]);
|
||||
let to: Vec<&str> = to.split('/').filter(|s| !s.is_empty()).collect();
|
||||
let to_dir = to.get(..to.len().saturating_sub(1)).unwrap_or(&[]);
|
||||
let common = from_dir.iter().zip(to_dir).take_while(|(a, b)| a == b).count();
|
||||
let mut out: Vec<&str> = vec![".."; from_dir.len() - common];
|
||||
out.extend(to.get(common..).unwrap_or(&[]));
|
||||
let s = out.join("/");
|
||||
// A relative reference whose first segment has a colon would read as a URI scheme.
|
||||
if s.split('/').next().is_some_and(|seg| seg.contains(':')) { format!("./{s}") } else { s }
|
||||
}
|
||||
|
||||
/// Build a zip from (name, bytes) entries.
|
||||
pub fn zip_entries(entries: &[(String, Vec<u8>)]) -> Result<Vec<u8>, DocxError> {
|
||||
let mut zw = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated).compression_level(Some(6));
|
||||
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
for (name, bytes) in entries {
|
||||
// Already-compressed media gain nothing from deflate.
|
||||
let lower = name.to_ascii_lowercase();
|
||||
let o = if [".png", ".jpg", ".jpeg", ".gif", ".webp"].iter().any(|e| lower.ends_with(e)) { stored } else { opts };
|
||||
zw.start_file(name.as_str(), o).map_err(|e| DocxError::Zip(e.to_string()))?;
|
||||
zw.write_all(bytes).map_err(|e| DocxError::Zip(e.to_string()))?;
|
||||
}
|
||||
let c = zw.finish().map_err(|e| DocxError::Zip(e.to_string()))?;
|
||||
Ok(c.into_inner())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn zip_of(entries: &[(&str, Vec<u8>)]) -> Vec<u8> {
|
||||
let e: Vec<(String, Vec<u8>)> = entries.iter().map(|(n, b)| (n.to_string(), b.clone())).collect();
|
||||
zip_entries(&e).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn case_colliding_entries_are_one_part() {
|
||||
let z = zip_of(&[("word/media/Image.png", b"upper".to_vec()), ("word/media/image.png", b"lower".to_vec())]);
|
||||
let p = Package::open(&z).unwrap();
|
||||
for spelling in ["word/media/Image.png", "word/media/image.png", "/WORD/MEDIA/IMAGE.PNG"] {
|
||||
assert_eq!(p.canonical(spelling), Some("word/media/Image.png"), "{spelling}");
|
||||
assert_eq!(p.get(spelling), Some(&b"upper"[..]), "{spelling}");
|
||||
}
|
||||
assert_eq!(p.files.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_spelling_a_relationship_names_is_kept() {
|
||||
// The first spelling in byte order ("Image.png") was kept even when the document's
|
||||
// relationship named the other one, so the picture shown was not the one it pointed at.
|
||||
let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="image" Target="media/image.png"/></Relationships>"#;
|
||||
let z = zip_of(&[
|
||||
("word/_rels/document.xml.rels", rels.as_bytes().to_vec()),
|
||||
("word/media/Image.png", b"upper".to_vec()),
|
||||
("word/media/image.png", b"lower".to_vec()),
|
||||
("word/media/Other.png", b"first".to_vec()),
|
||||
("word/media/other.png", b"second".to_vec()),
|
||||
]);
|
||||
let (p, warned) = crate::test_log::warnings(|| Package::open(&z).unwrap());
|
||||
assert_eq!(p.canonical("word/media/IMAGE.png"), Some("word/media/image.png"));
|
||||
assert_eq!(p.get("word/media/Image.png"), Some(&b"lower"[..]));
|
||||
assert_eq!(p.get("word/media/OTHER.PNG"), Some(&b"first"[..]), "no relationship names it: first in byte order");
|
||||
assert_eq!(p.files.len(), 3);
|
||||
let folded: Vec<_> = warned.iter().filter(|w| w.contains("other spellings")).collect();
|
||||
assert_eq!(folded.len(), 1, "one warning for the package: {warned:?}");
|
||||
assert!(folded.iter().all(|w| w.contains("2 zip entries")), "{folded:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_package_declaring_too_many_entries_is_refused_before_zip_reads_them() {
|
||||
// #3660: a ZIP64 end record declaring more than MAX_ENTRIES.
|
||||
let bomb = wordcraft_doc::zip_guard::zip64_entry_count_sample(MAX_ENTRIES as u64 + 1);
|
||||
assert!(matches!(Package::open(&bomb), Err(DocxError::Limit(e)) if e.contains("declares")));
|
||||
assert_eq!(crate::package_title(std::io::Cursor::new(bomb), 1 << 20), None);
|
||||
// A real package still opens, and its title still reads.
|
||||
let z = zip_of(&[(
|
||||
"docProps/core.xml",
|
||||
br#"<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>Plan</dc:title></cp:coreProperties>"#.to_vec(),
|
||||
)]);
|
||||
assert!(Package::open(&z).is_ok());
|
||||
assert_eq!(crate::package_title(std::io::Cursor::new(z), 1 << 20).as_deref(), Some("Plan"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decompression_bomb_is_rejected() {
|
||||
let z = zip_of(&[("word/document.xml", vec![b' '; 2_000_000])]);
|
||||
assert!(z.len() < 100_000);
|
||||
assert!(matches!(Package::open_limited(&z, 1_000_000, 10_000_000), Err(DocxError::Limit(_))));
|
||||
assert!(matches!(Package::open_limited(&z, 10_000_000, 1_000_000), Err(DocxError::Limit(_))));
|
||||
assert!(Package::open_limited(&z, 10_000_000, 10_000_000).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entries_that_fail_their_check_still_count_against_the_total() {
|
||||
// Each entry inflates in full before its CRC is checked; a failed one was dropped without
|
||||
// being charged, so any number of them inflated (#3723).
|
||||
let names: Vec<String> = (0..20).map(|i| format!("p{i}.bin")).collect();
|
||||
let entries: Vec<(&str, Vec<u8>)> = names.iter().map(|n| (n.as_str(), vec![0u8; 1_000_000])).collect();
|
||||
let mut z = zip_of(&entries);
|
||||
let mut patched = 0;
|
||||
for sig in [[0x50u8, 0x4B, 0x01, 0x02], [0x50, 0x4B, 0x03, 0x04]] {
|
||||
let crc_at = if sig[2] == 1 { 16 } else { 14 };
|
||||
let found: Vec<usize> = z.windows(4).enumerate().filter(|(_, w)| *w == sig).map(|(i, _)| i).collect();
|
||||
for pos in found {
|
||||
z[pos + crc_at] ^= 0xFF;
|
||||
patched += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(patched, 40);
|
||||
assert!(matches!(Package::open_limited(&z, 2_000_000, 4_000_000), Err(DocxError::Limit(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lying_declared_size_is_rejected_or_bounded() {
|
||||
let mut z = zip_of(&[("a.xml", b"<a/>".to_vec())]);
|
||||
// Patch the uncompressed size in the central directory and local header to ~4 GB.
|
||||
let mut patched = 0;
|
||||
for sig in [[0x50u8, 0x4B, 0x01, 0x02], [0x50, 0x4B, 0x03, 0x04]] {
|
||||
if let Some(pos) = z.windows(4).position(|w| w == sig) {
|
||||
let off = if sig[2] == 1 { pos + 24 } else { pos + 22 };
|
||||
z[off..off + 4].copy_from_slice(&0xFFFF_FFF0u32.to_le_bytes());
|
||||
patched += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(patched, 2);
|
||||
match Package::open(&z) {
|
||||
Err(_) => {}
|
||||
Ok(p) => assert!(p.get("a.xml").is_none_or(|b| b.len() < 1024)),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_document_shares_its_pictures_with_the_package() {
|
||||
// Each picture was copied out of the package, so a picture document held its pictures
|
||||
// twice while it opened, and left the package's copies behind as freed memory.
|
||||
let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture""#;
|
||||
let document = format!(
|
||||
r#"<w:document {ns}><w:body><w:p><w:r><w:drawing><wp:inline><wp:extent cx="635000" cy="635000"/><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic><pic:blipFill><a:blip r:embed="rIdImg"/></pic:blipFill></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p></w:body></w:document>"#
|
||||
);
|
||||
let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rIdImg" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/pic.png"/></Relationships>"#;
|
||||
let z = zip_of(&[
|
||||
("word/document.xml", document.into_bytes()),
|
||||
("word/_rels/document.xml.rels", rels.as_bytes().to_vec()),
|
||||
("word/media/pic.png", b"\x89PNG picture bytes".to_vec()),
|
||||
]);
|
||||
let pkg = Package::open(&z).unwrap();
|
||||
let doc = crate::read::read_opened(&pkg, None).unwrap();
|
||||
let held = doc.media.values().next().expect("the picture is read");
|
||||
assert!(Arc::ptr_eq(held, pkg.get_shared("word/media/pic.png").unwrap()), "the same bytes, not a copy");
|
||||
assert_eq!(pkg.get("/WORD/media/PIC.png"), Some(&b"\x89PNG picture bytes"[..]), "lookups are unchanged");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_targets() {
|
||||
assert_eq!(resolve("word", "media/image1.png"), "word/media/image1.png");
|
||||
assert_eq!(resolve("word", "../customXml/item1.xml"), "customXml/item1.xml");
|
||||
assert_eq!(resolve("word", "/word/styles.xml"), "word/styles.xml");
|
||||
assert_eq!(resolve("", "word/document.xml"), "word/document.xml");
|
||||
assert_eq!(resolve("word", "../../../x"), "x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_targets_resolve_back() {
|
||||
for (from, to, want) in [
|
||||
("word/document.xml", "word/charts/chart1.xml", "charts/chart1.xml"),
|
||||
("word/charts/chart1.xml", "word/embeddings/book.xlsx", "../embeddings/book.xlsx"),
|
||||
("word/charts/chart1.xml", "word/charts/colors1.xml", "colors1.xml"),
|
||||
("word/document.xml", "customXml/item1.xml", "../customXml/item1.xml"),
|
||||
("word/document.xml", "word/a:b.bin", "./a:b.bin"),
|
||||
] {
|
||||
let r = relative(from, to);
|
||||
assert_eq!(r, want);
|
||||
assert_eq!(resolve(from.rsplit_once('/').map_or("", |(d, _)| d), &r), to);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedded_manifest_round_trips_and_ignores_junk() {
|
||||
let mut m = EmbeddedManifest::default();
|
||||
let rel = Rel { id: "rId1".into(), kind: "k".into(), target: "word/embeddings/x.xlsx".into(), external: false };
|
||||
let url = Rel { id: "rId2".into(), kind: "k".into(), target: "https://example.com/x".into(), external: true };
|
||||
m.parts.insert("word/charts/chart1.xml".into(), EmbeddedPart { content_type: "ct".into(), rels: vec![rel, url] });
|
||||
let back = EmbeddedManifest::parse(&m.to_bytes());
|
||||
let p = &back.parts["word/charts/chart1.xml"];
|
||||
assert_eq!((p.content_type.as_str(), p.rels.len(), p.rels[1].external), ("ct", 2, true));
|
||||
let junk = EmbeddedManifest::parse(b"R\tnope\trId1\tk\tt\tI\nP\tonly-two\n\t\t\n\xff\xfe");
|
||||
assert!(junk.parts.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -1,459 +0,0 @@
|
||||
//! DOCX → [`Document`].
|
||||
|
||||
pub(crate) mod chart;
|
||||
mod chunk;
|
||||
pub(crate) mod diagram;
|
||||
mod drawing_color;
|
||||
mod embed;
|
||||
mod freeform;
|
||||
mod glossary;
|
||||
pub(crate) mod math;
|
||||
mod props;
|
||||
mod sdt;
|
||||
mod shape_style;
|
||||
mod story;
|
||||
mod wordart;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
|
||||
use wordcraft_doc::control::{ControlEvent, ControlKind, control_events, pair_events};
|
||||
use wordcraft_doc::graphic::{Graphic, GraphicKind};
|
||||
use wordcraft_doc::numbering::{AbstractNum, Level, LevelSuffix, Num};
|
||||
use wordcraft_doc::para::NoteKind;
|
||||
use wordcraft_doc::para::OBJ;
|
||||
use wordcraft_doc::props::Rgb;
|
||||
use wordcraft_doc::props::VAlign;
|
||||
use wordcraft_doc::section::NumFormat;
|
||||
use wordcraft_doc::styles::{PartFormat, Style, StyleKind, StyleSheet, TablePart, TableStyleParts};
|
||||
use wordcraft_doc::{
|
||||
Block, Blocks, Comment, Document, InlineObject, PagePicture, Paragraph, PartKind, Revision, RevisionKind, Zoom, ZoomFit, para_block,
|
||||
};
|
||||
|
||||
use crate::DocxError;
|
||||
use crate::bibliography::{MAX_SOURCES, Seen};
|
||||
use crate::package::{ContentTypes, EMBEDDED_PARTS, EmbeddedManifest, MAX_VBA_RELATED, Package, Rels, VBA_PROJECT_PART, VBA_RELATED, rel_is, rt};
|
||||
use crate::units::{flag, int, on_off, tw, u32_of};
|
||||
use crate::xml::El;
|
||||
pub(crate) use props::PropCtx;
|
||||
use story::StoryCtx;
|
||||
|
||||
mod meta;
|
||||
mod parts;
|
||||
mod styles;
|
||||
mod toc;
|
||||
|
||||
pub(crate) use meta::PROTECTION_HASH_ATTRS;
|
||||
pub(crate) use styles::LOWER_NAMES;
|
||||
use toc::{is_toc, toc_control_to_toc, toc_field_to_heading, unstyle_toc_links};
|
||||
|
||||
use chunk::ChunkState;
|
||||
pub use chunk::{AltChunk, ChunkImporter, ReadOptions};
|
||||
|
||||
/// Most fonts read from a font table, and the longest name (hostile files).
|
||||
const MAX_TABLE_FONTS: usize = 2000;
|
||||
const MAX_FONT_NAME: usize = 256;
|
||||
|
||||
/// Most parts (headers, notes, comments, text boxes) we create from one file.
|
||||
const MAX_PARTS: usize = 50_000;
|
||||
/// Deepest chain of headers/footers whose own sections reference further headers/footers.
|
||||
const MAX_HF_DEPTH: usize = 4;
|
||||
/// Most graphic work (items plus path segments, see `story::graphic_work`) built for one file's
|
||||
/// charts and diagrams; later ones are left empty.
|
||||
const MAX_GRAPHIC_WORK: usize = 2_000_000;
|
||||
/// Custom XML parts looked at for bibliography sources.
|
||||
const MAX_BIBLIOGRAPHY_PARTS: usize = 64;
|
||||
|
||||
/// Longest attached-template path we keep (bytes); a longer one is not a real path.
|
||||
const MAX_TEMPLATE_PATH: usize = 32 * 1024;
|
||||
|
||||
pub(crate) struct Reader<'p> {
|
||||
pkg: &'p Package,
|
||||
pub doc: Document,
|
||||
pub pc: PropCtx,
|
||||
revs: HashMap<(u8, String, String, Option<String>), u32>,
|
||||
/// Move ranges (`w:moveFromRangeStart` …) seen so far.
|
||||
pub moves: MoveCtx,
|
||||
media_by_path: HashMap<String, String>,
|
||||
hf_by_path: HashMap<String, u32>,
|
||||
/// Header/footer parts being read right now (see [`MAX_HF_DEPTH`]).
|
||||
hf_depth: usize,
|
||||
pub footnotes: HashMap<i64, u32>,
|
||||
pub endnotes: HashMap<i64, u32>,
|
||||
/// The note being read (kind, part id): its `w:footnoteRef` / `w:endnoteRef` mark is a
|
||||
/// reference to itself.
|
||||
current_note: Option<(NoteKind, u32)>,
|
||||
/// Reading a footnote or endnote separator or continuation notice (where the `w:separator`
|
||||
/// and `w:continuationSeparator` run elements belong).
|
||||
in_note_sep: bool,
|
||||
pub comment_map: HashMap<String, u32>,
|
||||
pub comments_ended: HashSet<u32>,
|
||||
pub bookmarks: HashMap<String, String>,
|
||||
/// Restrict Editing exception ranges (`w:permStart`): the file's id → ours.
|
||||
pub perms: HashMap<String, u32>,
|
||||
/// The next permission range id to give out (chunks' ranges take theirs from it too).
|
||||
pub next_perm: u32,
|
||||
/// Chart and diagram parts by path, parsed once.
|
||||
graphic_parts: HashMap<String, Option<Arc<El>>>,
|
||||
/// Charts and diagrams by (kind, part path, width bits, height bits), built once.
|
||||
graphics: HashMap<(GraphicKind, String, u32, u32), Arc<Graphic>>,
|
||||
/// Drawing ids connectors are glued to (`a:stCxn`, `a:endCxn`), and whether any shape's id
|
||||
/// was kept while reading (those not glued to are dropped at the end).
|
||||
pub glued: HashSet<u32>,
|
||||
pub shape_ids: bool,
|
||||
/// The main document part's name.
|
||||
main: String,
|
||||
/// The package's content types, read when first needed.
|
||||
content_types: Option<ContentTypes>,
|
||||
/// Parts kept for charts, diagrams and OLE objects (see `embed`).
|
||||
embedded: EmbeddedManifest,
|
||||
/// What alternative format chunks may still bring in, and how (see [`chunk`]).
|
||||
chunks: ChunkState<'p>,
|
||||
/// Linked text boxes as read: (`wps:txbx/@id` or `wps:linkedTxbx/@id`, `seq` (0 for the box
|
||||
/// holding the text), text box part). See [`link_text_boxes`].
|
||||
pub txbx_links: Vec<(u32, u32, u32)>,
|
||||
/// The theme's fill styles, which shapes' style references point to.
|
||||
pub theme_fills: shape_style::ThemeFills,
|
||||
/// The fill of the group being read (what its members' `a:grpFill` stands for).
|
||||
pub group_fill: Option<wordcraft_doc::fill::Fill>,
|
||||
}
|
||||
|
||||
/// The package path that internal relationship `id` points at, if it has type `kind` (an `rt`
|
||||
/// constant): a crafted id can't make a chart of some other, possibly huge, part.
|
||||
fn part_of(rels: &Rels, id: &str, kind: &str) -> Option<String> {
|
||||
rels.by_id(id).filter(|r| !r.external && rel_is(&r.kind, kind)).map(|r| r.target.clone())
|
||||
}
|
||||
|
||||
/// Read a `.docx` package.
|
||||
pub fn read(bytes: &[u8]) -> Result<Document, DocxError> {
|
||||
// A password-protected document is a compound file, not a zip: say so rather than failing
|
||||
// as a broken zip (`read_with_password` opens it).
|
||||
if crate::is_encrypted(bytes) {
|
||||
return crate::read_with_password(bytes, None);
|
||||
}
|
||||
read_package(bytes, None)
|
||||
}
|
||||
|
||||
/// [`read`] for bytes known not to be an encrypted package; `chunks` imports alternative format
|
||||
/// chunks in formats other than WordprocessingML.
|
||||
pub(crate) fn read_package(bytes: &[u8], chunks: Option<ChunkImporter>) -> Result<Document, DocxError> {
|
||||
read_opened(&Package::open(bytes)?, chunks)
|
||||
}
|
||||
|
||||
/// Read a package whose parts are already unpacked (from a zip, a Flat OPC file or a converted
|
||||
/// Word 2003 XML document).
|
||||
pub(crate) fn read_opened(pkg: &Package, chunks: Option<ChunkImporter>) -> Result<Document, DocxError> {
|
||||
read_opened_in(pkg, ChunkState::new(chunks)).map(|(doc, ..)| doc)
|
||||
}
|
||||
|
||||
/// [`read_opened`] with what chunks may still bring in; also gives the chunks, chunk bytes and
|
||||
/// graphic work left of that.
|
||||
fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize, u64, usize), DocxError> {
|
||||
let root_rels = pkg.rels("");
|
||||
let main = root_rels.by_type(rt::OFFICE_DOC).filter(|r| !r.external).map(|r| r.target.clone()).unwrap_or_else(|| "word/document.xml".to_string());
|
||||
let Some(root) = pkg.xml_strict(&main)? else {
|
||||
return Err(DocxError::MissingPart(main));
|
||||
};
|
||||
if root.name != "w:document" {
|
||||
return Err(DocxError::NotWord(format!("root element is {}", root.name)));
|
||||
}
|
||||
let rels = pkg.rels(&main);
|
||||
let mut doc = Document::new();
|
||||
doc.body.clear();
|
||||
let mut r = Reader {
|
||||
pkg,
|
||||
doc,
|
||||
pc: PropCtx::default(),
|
||||
revs: HashMap::new(),
|
||||
moves: MoveCtx::default(),
|
||||
media_by_path: HashMap::new(),
|
||||
hf_by_path: HashMap::new(),
|
||||
hf_depth: 0,
|
||||
footnotes: HashMap::new(),
|
||||
endnotes: HashMap::new(),
|
||||
current_note: None,
|
||||
in_note_sep: false,
|
||||
comment_map: HashMap::new(),
|
||||
comments_ended: HashSet::new(),
|
||||
bookmarks: HashMap::new(),
|
||||
perms: HashMap::new(),
|
||||
next_perm: 0,
|
||||
graphic_parts: HashMap::new(),
|
||||
graphics: HashMap::new(),
|
||||
glued: HashSet::new(),
|
||||
shape_ids: false,
|
||||
main: main.clone(),
|
||||
content_types: None,
|
||||
embedded: EmbeddedManifest::default(),
|
||||
txbx_links: Vec::new(),
|
||||
theme_fills: Default::default(),
|
||||
group_fill: None,
|
||||
chunks,
|
||||
};
|
||||
r.pc.major_font = r.doc.settings.major_font.clone();
|
||||
r.pc.minor_font = r.doc.settings.minor_font.clone();
|
||||
let part = |t: &str| rels.by_type(t).filter(|x| !x.external).map(|x| x.target.clone());
|
||||
|
||||
// The font table and the fonts embedded through it ride along as opaque parts.
|
||||
if let Some(p) = part(rt::FONT_TABLE) {
|
||||
r.keep_part_tree(&p);
|
||||
r.read_font_families(&p);
|
||||
}
|
||||
if let Some(p) = part(rt::THEME) {
|
||||
r.read_theme(&p);
|
||||
}
|
||||
// Secondary parts are optional: a broken one is skipped (logged), not fatal.
|
||||
let lenient = |what: &str, res: Result<(), DocxError>| {
|
||||
if let Err(e) = res {
|
||||
log::warn!("docx: ignoring unreadable {what}: {e}");
|
||||
}
|
||||
};
|
||||
// Styles come before settings, but their theme colours already follow its colour mapping, and
|
||||
// their `…Bidi` theme fonts its theme font language.
|
||||
if let Some(p) = part(rt::SETTINGS) {
|
||||
r.read_color_mapping(&p);
|
||||
}
|
||||
if let Some(p) = part(rt::STYLES) {
|
||||
lenient("styles", r.read_styles(&p));
|
||||
}
|
||||
if let Some(p) = part(rt::NUMBERING) {
|
||||
lenient("numbering", r.read_numbering(&p));
|
||||
}
|
||||
// Without a stated mode (or settings at all) Word lays a document out as Word 2007 did.
|
||||
r.doc.settings.compat_mode = wordcraft_doc::LEGACY_COMPAT_MODE;
|
||||
if let Some(p) = part(rt::SETTINGS) {
|
||||
lenient("settings", r.read_settings(&p));
|
||||
}
|
||||
// Comments before notes: a comment range inside a footnote or endnote resolves its id against
|
||||
// the comments already read, and was dropped when the notes came first.
|
||||
if let Some(p) = part(rt::COMMENTS) {
|
||||
let ex = part(rt::COMMENTS_EX);
|
||||
let ids = part(rt::COMMENTS_IDS);
|
||||
let cex = part(rt::COMMENTS_EXTENSIBLE);
|
||||
lenient("comments", r.read_comments(&p, ex.as_deref(), ids.as_deref(), cex.as_deref()));
|
||||
}
|
||||
if let Some(p) = part(rt::FOOTNOTES) {
|
||||
lenient("footnotes", r.read_notes(&p, true));
|
||||
}
|
||||
if let Some(p) = part(rt::ENDNOTES) {
|
||||
lenient("endnotes", r.read_notes(&p, false));
|
||||
}
|
||||
// Bibliography sources live in a custom XML data store (`b:Sources`), related from the
|
||||
// package root or the main part; Word's own files sometimes leave the relationship out, so
|
||||
// the conventional `customXml/itemN.xml` names are tried too. Each part is read at most once.
|
||||
let mut bib_parts: Vec<String> = Vec::new();
|
||||
for rel in root_rels.list.iter().chain(rels.list.iter()) {
|
||||
if rel_is(&rel.kind, rt::CUSTOM_XML) && !rel.external && !bib_parts.contains(&rel.target) {
|
||||
bib_parts.push(rel.target.clone());
|
||||
if bib_parts.len() >= MAX_BIBLIOGRAPHY_PARTS {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 1..=16 {
|
||||
let path = format!("customXml/item{i}.xml");
|
||||
if bib_parts.len() < MAX_BIBLIOGRAPHY_PARTS && pkg.get(&path).is_some() && !bib_parts.contains(&path) {
|
||||
bib_parts.push(path);
|
||||
}
|
||||
}
|
||||
let mut seen_sources = Seen::default();
|
||||
for path in &bib_parts {
|
||||
if r.doc.sources.len() >= MAX_SOURCES {
|
||||
break;
|
||||
}
|
||||
lenient("customXml bibliography", r.read_bibliography(path, &mut seen_sources));
|
||||
}
|
||||
|
||||
if let Some(bg) = root.child("w:background").and_then(|b| b.attr("w:color")).and_then(Rgb::parse) {
|
||||
r.doc.settings.page_color = Some(bg);
|
||||
}
|
||||
// A picture or texture fill: `w:background/v:background/v:fill` with a relationship id.
|
||||
if let Some(fill) = root.child("w:background").and_then(|b| b.child("v:background")).and_then(|b| b.child("v:fill")) {
|
||||
let rid = fill.attr("r:id").or_else(|| fill.attr("r:embed")).or_else(|| fill.attr("o:relid"));
|
||||
if let Some(media) = rid.and_then(|id| r.media_for(&rels, id)) {
|
||||
r.doc.settings.page_picture = Some(PagePicture {
|
||||
media,
|
||||
fill_type: fill.attr("type").unwrap_or("").chars().take(32).collect(),
|
||||
title: fill.attr("o:title").unwrap_or("").chars().take(255).collect(),
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(body) = root.child("w:body") {
|
||||
let mut sc = StoryCtx::default();
|
||||
let mut blocks = Blocks::new();
|
||||
r.read_blocks(&mut sc, body, &rels, &mut blocks, r.chunks.depth);
|
||||
r.flush_pending(&mut sc, &mut blocks);
|
||||
r.doc.body = blocks;
|
||||
unstyle_toc_links(&mut r.doc);
|
||||
toc_control_to_toc(&mut r.doc.body);
|
||||
toc_field_to_heading(&mut r.doc.body);
|
||||
if let Some(s) = body.child("w:sectPr") {
|
||||
r.doc.last_section = r.read_section(s, &rels);
|
||||
}
|
||||
}
|
||||
// A watermark in the box Word's Design › Watermark gives it for the page keeps no box of its
|
||||
// own: it follows the page, as when it was made.
|
||||
let (pw, ph) = (r.doc.last_section.page_w, r.doc.last_section.page_h);
|
||||
if let Some(wm) = r.doc.settings.watermark.as_mut()
|
||||
&& let Some([w, h]) = wm.size
|
||||
{
|
||||
let [dw, dh] = wordcraft_doc::Watermark { size: None, ..wm.clone() }.frame(pw, ph);
|
||||
if (w - dw).abs() < 0.06 && (h - dh).abs() < 0.06 {
|
||||
wm.size = None;
|
||||
}
|
||||
}
|
||||
|
||||
let core = root_rels.by_type(rt::CORE).map(|x| x.target.clone()).unwrap_or_else(|| "docProps/core.xml".into());
|
||||
r.read_core(&core);
|
||||
if let Some(app) = root_rels.by_type(rt::APP).map(|x| x.target.clone()) {
|
||||
r.read_app(&app);
|
||||
}
|
||||
if let Some(c) = root_rels.by_type(rt::CUSTOM)
|
||||
&& let Ok(Some(x)) = r.xml(&c.target)
|
||||
{
|
||||
r.doc.custom_props = crate::custom::read(&x);
|
||||
}
|
||||
// A macro project and every part it relates to (VBA data, signatures…) ride along as opaque
|
||||
// bytes, never parsed or run, so a .docm/.dotm saved again carries exactly what the file had.
|
||||
// Only the relationships and content types are read.
|
||||
if let Some(v) = part(rt::VBA_PROJECT)
|
||||
&& let Some(b) = pkg.get_shared(&v).filter(|b| !b.is_empty())
|
||||
{
|
||||
r.doc.passthrough.insert(VBA_PROJECT_PART.into(), b.clone());
|
||||
r.read_vba_related(&v);
|
||||
}
|
||||
r.read_glossary(&rels);
|
||||
r.keep_custom_xml(&root_rels, &rels);
|
||||
r.keep_unmodelled(&root, &root_rels, &rels);
|
||||
if !r.embedded.parts.is_empty() {
|
||||
let manifest = r.embedded.to_bytes();
|
||||
r.doc.passthrough.insert(EMBEDDED_PARTS.into(), Arc::new(manifest));
|
||||
}
|
||||
r.finish_moves();
|
||||
let links = std::mem::take(&mut r.txbx_links);
|
||||
link_text_boxes(&mut r.doc, links);
|
||||
if r.shape_ids {
|
||||
keep_glued_ids(&mut r.doc, &r.glued);
|
||||
}
|
||||
let (left, bytes, graphic) = (r.chunks.left, r.chunks.bytes, r.chunks.graphic);
|
||||
let mut doc = r.doc;
|
||||
doc.ensure_nonempty();
|
||||
Ok((doc, left, bytes, graphic))
|
||||
}
|
||||
|
||||
/// Most move ranges open at once, and most move names tracked, per file.
|
||||
const MAX_OPEN_MOVES: usize = 64;
|
||||
const MAX_MOVES: usize = 100_000;
|
||||
|
||||
/// The move ranges of a file (ECMA-376 §17.13.5.22–27): a `w:moveFrom` / `w:moveTo` is one end
|
||||
/// of a move when it sits inside a range of that kind; the range's name links both ends.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct MoveCtx {
|
||||
/// Open source and destination ranges: (`w:id`, `w:name`).
|
||||
open: [Vec<(String, String)>; 2],
|
||||
/// Per name: a source range started / ended, a destination range started / ended (bits 0–3).
|
||||
seen: HashMap<String, u8>,
|
||||
/// The range of each kind that ended last in the paragraph being read (for its mark).
|
||||
pub closed_here: [Option<String>; 2],
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
/// A `w:moveFromRangeStart/End` or `w:moveToRangeStart/End`.
|
||||
pub fn move_range(&mut self, e: &El) {
|
||||
let (side, start) = match e.name.as_str() {
|
||||
"w:moveFromRangeStart" => (0, true),
|
||||
"w:moveFromRangeEnd" => (0, false),
|
||||
"w:moveToRangeStart" => (1, true),
|
||||
"w:moveToRangeEnd" => (1, false),
|
||||
_ => return,
|
||||
};
|
||||
let id = e.attr("w:id").unwrap_or("").chars().take(64).collect::<String>();
|
||||
let m = &mut self.moves;
|
||||
let Some(open) = m.open.get_mut(side) else { return };
|
||||
if start {
|
||||
let name = e.attr("w:name").unwrap_or("").chars().take(256).collect::<String>();
|
||||
if name.is_empty() || open.len() >= MAX_OPEN_MOVES || (m.seen.len() >= MAX_MOVES && !m.seen.contains_key(&name)) {
|
||||
return;
|
||||
}
|
||||
*m.seen.entry(name.clone()).or_default() |= 1 << (side * 2);
|
||||
open.push((id, name));
|
||||
} else if let Some(i) = open.iter().rposition(|(x, _)| *x == id) {
|
||||
let (_, name) = open.remove(i);
|
||||
*m.seen.entry(name.clone()).or_default() |= 2 << (side * 2);
|
||||
if let Some(c) = m.closed_here.get_mut(side) {
|
||||
*c = Some(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The revision of a `w:moveFrom` (`to` false) or `w:moveTo`: one end of the innermost open
|
||||
/// range of its kind, else (no range) a plain deletion or insertion.
|
||||
pub fn move_revision(&mut self, to: bool, e: &El, mark: bool) -> u32 {
|
||||
let side = usize::from(to);
|
||||
let open = self.moves.open.get(side).and_then(|o| o.last()).map(|(_, n)| n.clone());
|
||||
// A paragraph mark comes after its paragraph's text: a range that ended in it counts.
|
||||
let name = if mark { open.or_else(|| self.moves.closed_here.get(side).cloned().flatten()) } else { open };
|
||||
let kind = if to { RevisionKind::Insert } else { RevisionKind::Delete };
|
||||
self.revision_named(kind, e, name)
|
||||
}
|
||||
|
||||
/// Moves missing an end (a source or destination range, or its text) are plain deletions
|
||||
/// and insertions.
|
||||
fn finish_moves(&mut self) {
|
||||
let mut ends: HashMap<&str, u8> = HashMap::new();
|
||||
for r in &self.doc.revisions {
|
||||
if let Some(n) = r.move_name.as_deref() {
|
||||
*ends.entry(n).or_default() |= if r.kind == RevisionKind::Delete { 1 } else { 2 };
|
||||
}
|
||||
}
|
||||
let ok: HashSet<String> =
|
||||
ends.into_iter().filter(|(n, e)| *e == 3 && self.moves.seen.get(*n) == Some(&0b1111)).map(|(n, _)| n.to_string()).collect();
|
||||
for r in &mut self.doc.revisions {
|
||||
if r.move_name.as_ref().is_some_and(|n| !ok.contains(n)) {
|
||||
r.move_name = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Link the text boxes read as (id, seq, part) into chains (see [`Reader::txbx_links`]): each
|
||||
/// id's boxes in `seq` order after the box holding the text. A repeated seq, a chain without its
|
||||
/// first box or more than [`wordcraft_doc::MAX_LINKED_BOXES`] boxes: the extra boxes stay apart.
|
||||
fn link_text_boxes(doc: &mut Document, mut links: Vec<(u32, u32, u32)>) {
|
||||
links.sort_by_key(|l| (l.0, l.1));
|
||||
links.dedup_by_key(|l| (l.0, l.1));
|
||||
for group in links.chunk_by(|a, b| a.0 == b.0) {
|
||||
if group.first().is_none_or(|l| l.1 != 0) {
|
||||
continue;
|
||||
}
|
||||
let chain: Vec<u32> = group.iter().take(wordcraft_doc::MAX_LINKED_BOXES).map(|l| l.2).collect();
|
||||
for pair in chain.windows(2) {
|
||||
if let [a, b] = pair
|
||||
&& let Some(p) = doc.parts.get_mut(a)
|
||||
{
|
||||
p.body.next = Some(*b);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop the drawing ids read with shapes, except those connectors are glued to (so a shape
|
||||
/// without connectors reads back as it was written).
|
||||
fn keep_glued_ids(doc: &mut Document, glued: &HashSet<u32>) {
|
||||
let has_id = |o: &InlineObject| match o {
|
||||
InlineObject::Shape { extra, .. } => extra.id != 0,
|
||||
InlineObject::Group { children, .. } => children.iter().any(|c| matches!(&c.obj, InlineObject::Shape { extra, .. } if extra.id != 0)),
|
||||
_ => false,
|
||||
};
|
||||
let clear = |o: &mut InlineObject| {
|
||||
if let InlineObject::Shape { extra, .. } = o
|
||||
&& !glued.contains(&extra.id)
|
||||
{
|
||||
extra.id = 0;
|
||||
}
|
||||
};
|
||||
doc.objects_mut(&has_id, &mut |o| match o {
|
||||
InlineObject::Group { children, .. } => children.iter_mut().for_each(|c| clear(&mut c.obj)),
|
||||
o => clear(o),
|
||||
});
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
//! Charts (ECMA-376 Part 1 §21.2): the cached data of a `c:chartSpace` drawn as items in points.
|
||||
//!
|
||||
//! Bar and column (clustered, stacked or 100% stacked), line, area, pie, doughnut and scatter
|
||||
//! charts are drawn from the first supported chart type of the plot area, with their values as
|
||||
//! data labels where `c:dLbls` shows them (`c:showVal`). Radar charts (standard, with markers or
|
||||
//! filled) draw as spokes, concentric grid polygons and one polygon per series. A chart of only other types (stock, bubble…)
|
||||
//! draws a placeholder: its frame, its title and the type it is. Text widths are estimated ([`CHAR_W`] em a character), so label places are approximate.
|
||||
|
||||
use std::f64::consts::{FRAC_PI_2, TAU};
|
||||
|
||||
use wordcraft_doc::graphic::{GraphicItem, PathSeg, TextAlign};
|
||||
use wordcraft_doc::para::ShapeKind;
|
||||
use wordcraft_doc::props::Rgb;
|
||||
|
||||
use super::drawing_color::{fill_colour, slot};
|
||||
use crate::units::{int, measure};
|
||||
use crate::xml::El;
|
||||
|
||||
mod canvas;
|
||||
mod format;
|
||||
mod model;
|
||||
mod parse;
|
||||
mod plot;
|
||||
mod polar;
|
||||
|
||||
#[cfg(test)]
|
||||
use canvas::SWATCH;
|
||||
pub(super) use canvas::text_w;
|
||||
use canvas::{Rect, clip_text, legend, text};
|
||||
#[cfg(test)]
|
||||
use format::{civil, excel_stamp, format_value};
|
||||
use model::{Axis, Grouping, Kind, Pos, Series};
|
||||
#[cfg(test)]
|
||||
use parse::{axis, category_text, labels, numbers};
|
||||
use parse::{read_series, text_size, title_of};
|
||||
#[cfg(test)]
|
||||
use plot::{Scale, narrow, nice_scale};
|
||||
use plot::{backdrop, cartesian};
|
||||
use polar::{pie, radar};
|
||||
|
||||
/// Most series read from one chart.
|
||||
const MAX_SERIES: usize = 256;
|
||||
/// Most points read from one series.
|
||||
const MAX_POINTS: usize = 10_000;
|
||||
/// Most points read from one chart, all series together.
|
||||
const MAX_CELLS: usize = 100_000;
|
||||
/// Longest series name or category label kept.
|
||||
const MAX_LABEL: usize = 200;
|
||||
/// Most categories (spokes) of a radar chart.
|
||||
const MAX_SPOKES: usize = 100;
|
||||
/// Most series of a radar chart.
|
||||
const MAX_RADAR_SERIES: usize = 50;
|
||||
/// Most axis ticks drawn.
|
||||
const MAX_TICKS: usize = 20;
|
||||
/// Default text size, points.
|
||||
const FONT: f32 = 9.0;
|
||||
/// Average character width estimate, em (charts and diagrams measure text before shaping).
|
||||
pub(super) const CHAR_W: f32 = 0.55;
|
||||
/// Widest share of the chart a legend entry's text takes.
|
||||
const LEGEND_SHARE: f32 = 0.4;
|
||||
/// Most rows of a legend above or below the plot.
|
||||
const MAX_LEGEND_ROWS: usize = 3;
|
||||
const TITLE_SIZE: f32 = 14.0;
|
||||
/// Space between the frame and the content, points.
|
||||
const PAD: f32 = 6.0;
|
||||
/// Line height as a multiple of the font size.
|
||||
const LINE: f32 = 1.3;
|
||||
/// Gap between a label and its axis, points.
|
||||
const GAP: f32 = 4.0;
|
||||
/// Default width of a line series, points (Word's 2.25 pt).
|
||||
const LINE_W: f32 = 2.25;
|
||||
const TEXT: Rgb = Rgb(0x59, 0x59, 0x59);
|
||||
const GRID: Rgb = Rgb(0xD9, 0xD9, 0xD9);
|
||||
const AXIS: Rgb = Rgb(0xBF, 0xBF, 0xBF);
|
||||
|
||||
/// Items for a chart part, sized `w` x `h` points: a placeholder when only unsupported chart
|
||||
/// types are in it, empty when there's nothing to draw.
|
||||
pub(crate) fn chart_items(space: &El, theme: &[Rgb], w: f32, h: f32) -> Vec<GraphicItem> {
|
||||
let mut out = Vec::new();
|
||||
if !(w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
|
||||
return out;
|
||||
}
|
||||
let Some(chart) = Chart::parse(space, theme) else {
|
||||
placeholder(&mut out, space, theme, w, h);
|
||||
return out;
|
||||
};
|
||||
backdrop(&mut out, space, theme, w, h);
|
||||
let mut area = Rect { x: PAD, y: PAD, w: (w - 2.0 * PAD).max(1.0), h: (h - 2.0 * PAD).max(1.0) };
|
||||
if let Some(t) = &chart.title {
|
||||
area = title_block(&mut out, t, area);
|
||||
}
|
||||
if let Some(pos) = chart.legend {
|
||||
// Word lists bar series in the order they are drawn: reversed for clustered horizontal bars
|
||||
// and for stacked columns with a vertical legend.
|
||||
let vertical = matches!(pos, Pos::Left | Pos::Right);
|
||||
let reverse = chart.kind == Kind::Bar && if chart.horizontal { !chart.stacked() } else { chart.stacked() && vertical };
|
||||
let mut entries = chart.legend_entries();
|
||||
if reverse {
|
||||
entries.reverse();
|
||||
}
|
||||
area = legend(&mut out, &entries, pos, area, chart.text_size);
|
||||
}
|
||||
match chart.kind {
|
||||
Kind::Pie | Kind::Doughnut => pie(&mut out, &chart, area),
|
||||
Kind::Radar => radar(&mut out, &chart, area),
|
||||
_ => cartesian(&mut out, &chart, area),
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The title at the top of `area`; returns the room left under it.
|
||||
fn title_block(out: &mut Vec<GraphicItem>, t: &str, area: Rect) -> Rect {
|
||||
let lh = TITLE_SIZE * LINE;
|
||||
text(out, Rect { x: area.x, y: area.y, w: area.w, h: lh }, t, TITLE_SIZE, TextAlign::Center, true);
|
||||
Rect { y: area.y + lh + PAD, h: (area.h - lh - PAD).max(1.0), ..area }
|
||||
}
|
||||
|
||||
/// A chart whose plot area holds only chart types WordCraft doesn't draw (stock, bubble…): its
|
||||
/// frame with a light border, its title and a line saying which type it is, so it isn't blank
|
||||
/// space. Nothing when the plot area has no chart type at all, or a supported one with nothing
|
||||
/// to draw.
|
||||
fn placeholder(out: &mut Vec<GraphicItem>, space: &El, theme: &[Rgb], w: f32, h: f32) {
|
||||
let Some(chart) = space.child("c:chart") else { return };
|
||||
let Some(plot) = chart.child("c:plotArea") else { return };
|
||||
if plot.els().any(|e| Kind::of(&e.name).is_some()) {
|
||||
return;
|
||||
}
|
||||
let Some(ty) = plot.els().find(|e| e.name.ends_with("Chart")) else { return };
|
||||
backdrop(out, space, theme, w, h);
|
||||
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill: None, stroke: Some(AXIS), stroke_width: 0.75 });
|
||||
let mut area = Rect { x: PAD, y: PAD, w: (w - 2.0 * PAD).max(1.0), h: (h - 2.0 * PAD).max(1.0) };
|
||||
if let Some(t) = title_of(chart, &read_series(ty, false, theme)) {
|
||||
area = title_block(out, &t, area);
|
||||
}
|
||||
let size = text_size(space);
|
||||
let lh = size * LINE;
|
||||
let msg = format!("{} (not supported yet)", unsupported_name(&ty.name));
|
||||
let line = Rect { x: area.x, y: area.y + ((area.h - lh) / 2.0).max(0.0), w: area.w, h: lh };
|
||||
text(out, line, &clip_text(&msg, area.w, size), size, TextAlign::Center, false);
|
||||
}
|
||||
|
||||
/// What a placeholder calls a chart type element WordCraft doesn't draw.
|
||||
fn unsupported_name(name: &str) -> &'static str {
|
||||
match name {
|
||||
"c:stockChart" => "Stock chart",
|
||||
"c:bubbleChart" => "Bubble chart",
|
||||
"c:surfaceChart" | "c:surface3DChart" => "Surface chart",
|
||||
"c:ofPieChart" => "Pie of pie chart",
|
||||
"c:bar3DChart" => "3-D bar chart",
|
||||
"c:line3DChart" => "3-D line chart",
|
||||
"c:area3DChart" => "3-D area chart",
|
||||
"c:pie3DChart" => "3-D pie chart",
|
||||
_ => "Chart",
|
||||
}
|
||||
}
|
||||
|
||||
struct Chart {
|
||||
kind: Kind,
|
||||
horizontal: bool,
|
||||
grouping: Grouping,
|
||||
/// Gap between groups as a fraction of a bar's width.
|
||||
gap: f32,
|
||||
/// Overlap of clustered bars, -1..1 (1 = all on top of each other).
|
||||
overlap: f32,
|
||||
/// Doughnut hole, fraction of the outer radius.
|
||||
hole: f32,
|
||||
first_angle: f64,
|
||||
/// Radar: series polygons are filled (`c:radarStyle` = `filled`).
|
||||
radar_fill: bool,
|
||||
/// Radar: points carry markers (`c:radarStyle` = `marker`).
|
||||
radar_marks: bool,
|
||||
vary: bool,
|
||||
/// Number of categories (scatter: points per series at most).
|
||||
n: usize,
|
||||
cats: Vec<String>,
|
||||
series: Vec<Series>,
|
||||
accents: Vec<Rgb>,
|
||||
title: Option<String>,
|
||||
legend: Option<Pos>,
|
||||
/// Category axis (scatter: the x axis).
|
||||
cat: Axis,
|
||||
val: Axis,
|
||||
text_size: f32,
|
||||
val_fmt: String,
|
||||
/// Serial days count from 1904 (`c:date1904`), not 1900.
|
||||
date1904: bool,
|
||||
/// Outline of the plot area, when the file draws one.
|
||||
plot_border: Option<Rgb>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -1,210 +0,0 @@
|
||||
use super::model::Pos;
|
||||
use super::{CHAR_W, GAP, GraphicItem, LEGEND_SHARE, LINE, MAX_LEGEND_ROWS, PAD, PathSeg, Rgb, ShapeKind, TEXT, TextAlign};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(super) struct Rect {
|
||||
pub(super) x: f32,
|
||||
pub(super) y: f32,
|
||||
pub(super) w: f32,
|
||||
pub(super) h: f32,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
pub(super) fn right(self) -> f32 {
|
||||
self.x + self.w
|
||||
}
|
||||
pub(super) fn bottom(self) -> f32 {
|
||||
self.y + self.h
|
||||
}
|
||||
pub(super) fn arr(self) -> [f32; 4] {
|
||||
[self.x, self.y, self.w, self.h]
|
||||
}
|
||||
}
|
||||
|
||||
/// Estimated text width, points: text is placed before it's shaped, at [`CHAR_W`] a character.
|
||||
pub(in super::super) fn text_w(s: &str, size: f32) -> f32 {
|
||||
s.chars().count() as f32 * size * CHAR_W
|
||||
}
|
||||
|
||||
/// `s`, cut short with an ellipsis to fit `width` points by the [`text_w`] estimate.
|
||||
pub(super) fn clip_text(s: &str, width: f32, size: f32) -> String {
|
||||
let fit = (width / (size * CHAR_W)).floor().max(0.0) as usize;
|
||||
if s.chars().count() <= fit {
|
||||
return s.to_string();
|
||||
}
|
||||
let mut out: String = s.chars().take(fit.saturating_sub(1)).collect();
|
||||
out.push('…');
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) fn widest(items: &[String], size: f32) -> f32 {
|
||||
items.iter().map(|s| text_w(s, size)).fold(0.0, f32::max)
|
||||
}
|
||||
|
||||
pub(super) fn text(out: &mut Vec<GraphicItem>, rect: Rect, s: &str, size: f32, align: TextAlign, bold: bool) {
|
||||
out.push(GraphicItem::Text { rect: rect.arr(), text: s.to_string(), size, color: TEXT, bold, align, font: None });
|
||||
}
|
||||
|
||||
/// Text centred on `cx` with its top at `y`.
|
||||
pub(super) fn text_at(out: &mut Vec<GraphicItem>, cx: f32, y: f32, s: &str, size: f32) {
|
||||
let w = text_w(s, size) + 4.0;
|
||||
text(out, Rect { x: cx - w / 2.0, y, w, h: size * LINE }, s, size, TextAlign::Center, false);
|
||||
}
|
||||
|
||||
pub(super) fn fill(out: &mut Vec<GraphicItem>, segs: Vec<PathSeg>, color: Rgb) {
|
||||
if !segs.is_empty() {
|
||||
out.push(GraphicItem::Path { segs, fill: Some(color), stroke: None, stroke_width: 0.0 });
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn stroke(out: &mut Vec<GraphicItem>, segs: Vec<PathSeg>, color: Rgb, width: f32) {
|
||||
if !segs.is_empty() {
|
||||
out.push(GraphicItem::Path { segs, fill: None, stroke: Some(color), stroke_width: width });
|
||||
}
|
||||
}
|
||||
|
||||
fn swatch(out: &mut Vec<GraphicItem>, x: f32, y: f32, color: Rgb) {
|
||||
out.push(GraphicItem::Shape { rect: [x, y, SWATCH, SWATCH], kind: ShapeKind::Rectangle, fill: Some(color), stroke: None, stroke_width: 0.0 });
|
||||
}
|
||||
|
||||
pub(super) fn seg(x0: f32, y0: f32, x1: f32, y1: f32) -> Vec<PathSeg> {
|
||||
vec![PathSeg::Move(x0, y0), PathSeg::Line(x1, y1)]
|
||||
}
|
||||
|
||||
pub(super) fn rect_segs(x0: f32, y0: f32, x1: f32, y1: f32) -> Vec<PathSeg> {
|
||||
vec![PathSeg::Move(x0, y0), PathSeg::Line(x1, y0), PathSeg::Line(x1, y1), PathSeg::Line(x0, y1), PathSeg::Close]
|
||||
}
|
||||
|
||||
/// Polyline pieces: a gap (`None`) starts a new sub-path.
|
||||
pub(super) fn runs(pts: impl Iterator<Item = Option<(f32, f32)>>) -> Vec<PathSeg> {
|
||||
let mut segs = Vec::new();
|
||||
let mut open = false;
|
||||
for p in pts {
|
||||
match p {
|
||||
Some((x, y)) => {
|
||||
segs.push(if open { PathSeg::Line(x, y) } else { PathSeg::Move(x, y) });
|
||||
open = true;
|
||||
}
|
||||
None => open = false,
|
||||
}
|
||||
}
|
||||
segs
|
||||
}
|
||||
|
||||
/// Words of `s` in lines of at most `width` points (a word wider than that keeps its own line).
|
||||
pub(super) fn wrap(s: &str, width: f32, size: f32) -> Vec<String> {
|
||||
let mut lines: Vec<String> = Vec::new();
|
||||
let mut cur = String::new();
|
||||
for word in s.split_whitespace() {
|
||||
let joined = if cur.is_empty() { word.to_string() } else { format!("{cur} {word}") };
|
||||
if cur.is_empty() || text_w(&joined, size) <= width {
|
||||
cur = joined;
|
||||
} else {
|
||||
lines.push(std::mem::take(&mut cur));
|
||||
cur = word.to_string();
|
||||
}
|
||||
}
|
||||
if !cur.is_empty() {
|
||||
lines.push(cur);
|
||||
}
|
||||
if lines.is_empty() {
|
||||
lines.push(String::new());
|
||||
}
|
||||
lines.truncate(3);
|
||||
lines
|
||||
}
|
||||
|
||||
pub(super) const SWATCH: f32 = 7.0;
|
||||
|
||||
/// Space between legend entries in a row, points.
|
||||
const SEP: f32 = 10.0;
|
||||
|
||||
/// Legend in the given side of `area`; returns the area left for the plot.
|
||||
pub(super) fn legend(out: &mut Vec<GraphicItem>, entries: &[(String, Rgb)], pos: Pos, area: Rect, fs: f32) -> Rect {
|
||||
if entries.is_empty() {
|
||||
return area;
|
||||
}
|
||||
// Entry texts are cut to a share of the chart, so a long name can't push the legend out.
|
||||
let entries: Vec<(String, Rgb)> = entries.iter().map(|(s, c)| (clip_text(s, area.w * LEGEND_SHARE, fs), *c)).collect();
|
||||
match pos {
|
||||
Pos::Right | Pos::Left => side_legend(out, &entries, pos == Pos::Left, area, fs),
|
||||
Pos::Top | Pos::Bottom => row_legend(out, &entries, pos == Pos::Top, area, fs),
|
||||
}
|
||||
}
|
||||
|
||||
/// A legend in a column at the left or right of `area`, keeping only the entries that fit above
|
||||
/// its bottom; returns the area left for the plot.
|
||||
fn side_legend(out: &mut Vec<GraphicItem>, entries: &[(String, Rgb)], left: bool, area: Rect, fs: f32) -> Rect {
|
||||
let lh = fs * LINE;
|
||||
let entries = entries.get(..((area.h / lh).floor().max(0.0) as usize).min(entries.len())).unwrap_or(&[]);
|
||||
if entries.is_empty() {
|
||||
return area;
|
||||
}
|
||||
let tw = entries.iter().map(|(s, _)| text_w(s, fs)).fold(0.0, f32::max);
|
||||
let block_w = SWATCH + GAP + tw;
|
||||
let block_h = lh * entries.len() as f32;
|
||||
let x = if left { area.x } else { area.right() - block_w };
|
||||
let y0 = area.y + ((area.h - block_h) / 2.0).max(0.0);
|
||||
for (k, (s, c)) in entries.iter().enumerate() {
|
||||
let y = y0 + lh * k as f32;
|
||||
swatch(out, x, y + (lh - SWATCH) / 2.0, *c);
|
||||
text(out, Rect { x: x + SWATCH + GAP, y, w: tw + 2.0, h: lh }, s, fs, TextAlign::Left, false);
|
||||
}
|
||||
let used = block_w + PAD;
|
||||
let mut area = area;
|
||||
area.w = (area.w - used).max(1.0);
|
||||
if left {
|
||||
area.x += used;
|
||||
}
|
||||
area
|
||||
}
|
||||
|
||||
/// A legend in centred rows the width of `area` above or below it (at most
|
||||
/// [`MAX_LEGEND_ROWS`], and half its height); returns the area left for the plot.
|
||||
fn row_legend(out: &mut Vec<GraphicItem>, entries: &[(String, Rgb)], top: bool, area: Rect, fs: f32) -> Rect {
|
||||
let lh = fs * LINE;
|
||||
let max_rows = MAX_LEGEND_ROWS.min((area.h / 2.0 / lh).floor().max(1.0) as usize);
|
||||
let rows = legend_rows(entries, area.w, fs, max_rows);
|
||||
let y0 = if top { area.y } else { area.bottom() - lh * rows.len() as f32 };
|
||||
for (r, row) in rows.iter().enumerate() {
|
||||
let total = row.iter().map(|(_, _, w)| SWATCH + GAP + w).sum::<f32>() + SEP * row.len().saturating_sub(1) as f32;
|
||||
let mut x = area.x + ((area.w - total) / 2.0).max(0.0);
|
||||
let y = y0 + lh * r as f32;
|
||||
for (s, c, w) in row {
|
||||
swatch(out, x, y + (lh - SWATCH) / 2.0, *c);
|
||||
text(out, Rect { x: x + SWATCH + GAP, y, w: w + 2.0, h: lh }, s, fs, TextAlign::Left, false);
|
||||
x += SWATCH + GAP + w + SEP;
|
||||
}
|
||||
}
|
||||
let used = lh * rows.len() as f32 + PAD;
|
||||
let mut area = area;
|
||||
area.h = (area.h - used).max(1.0);
|
||||
if top {
|
||||
area.y += used;
|
||||
}
|
||||
area
|
||||
}
|
||||
|
||||
/// Legend entries (text, colour, text width) packed into at most `max_rows` rows of `width`
|
||||
/// points; the entries past the last row are left out.
|
||||
fn legend_rows(entries: &[(String, Rgb)], width: f32, fs: f32, max_rows: usize) -> Vec<Vec<(String, Rgb, f32)>> {
|
||||
let mut rows: Vec<Vec<(String, Rgb, f32)>> = Vec::new();
|
||||
let mut row_w = 0.0;
|
||||
for (s, c) in entries {
|
||||
let w = text_w(s, fs);
|
||||
let entry_w = SWATCH + GAP + w;
|
||||
let fits = !rows.is_empty() && row_w + SEP + entry_w <= width;
|
||||
if fits {
|
||||
row_w += SEP + entry_w;
|
||||
} else if rows.len() < max_rows {
|
||||
rows.push(Vec::new());
|
||||
row_w = entry_w;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
if let Some(row) = rows.last_mut() {
|
||||
row.push((s.clone(), *c, w));
|
||||
}
|
||||
}
|
||||
rows
|
||||
}
|
||||
@@ -1,370 +0,0 @@
|
||||
/// A piece of a format section: literal text, a number character (`#`, `0`, `,`, `.`), or the
|
||||
/// exponent of a scientific format (`E+` when true, `E-` when false).
|
||||
enum Tok {
|
||||
Lit(String),
|
||||
Num(char),
|
||||
Exp(bool),
|
||||
}
|
||||
|
||||
/// The sections of a format code (positive; negative; zero; text), split outside quotes.
|
||||
fn sections(code: &str) -> Vec<&str> {
|
||||
let mut out = Vec::new();
|
||||
let (mut start, mut quoted, mut escaped) = (0, false, false);
|
||||
for (i, c) in code.char_indices() {
|
||||
match c {
|
||||
_ if escaped => escaped = false,
|
||||
'\\' if !quoted => escaped = true,
|
||||
'"' => quoted = !quoted,
|
||||
';' if !quoted => {
|
||||
out.push(code.get(start..i).unwrap_or(""));
|
||||
start = i + 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out.push(code.get(start..).unwrap_or(""));
|
||||
out
|
||||
}
|
||||
|
||||
/// The first section of a format code.
|
||||
fn first_section(code: &str) -> &str {
|
||||
sections(code).first().copied().unwrap_or("")
|
||||
}
|
||||
|
||||
/// The pieces of one format section. Quoted text, escapes and `[$€-407]` currency symbols are
|
||||
/// literal; other brackets (`[Red]`) are dropped; `_x` (room for `x`) is a space; `*x` is dropped.
|
||||
fn format_toks(section: &str) -> Vec<Tok> {
|
||||
let mut toks = Vec::new();
|
||||
let mut chars = section.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
match c {
|
||||
'"' => toks.push(Tok::Lit(chars.by_ref().take_while(|q| *q != '"').collect())),
|
||||
'[' => {
|
||||
let inner: String = chars.by_ref().take_while(|q| *q != ']').collect();
|
||||
if let Some(sym) = currency(&inner) {
|
||||
toks.push(Tok::Lit(sym.to_string()));
|
||||
}
|
||||
}
|
||||
'\\' => toks.extend(chars.next().map(|n| Tok::Lit(n.to_string()))),
|
||||
'_' => {
|
||||
chars.next();
|
||||
toks.push(Tok::Lit(" ".to_string()));
|
||||
}
|
||||
'*' => {
|
||||
chars.next();
|
||||
}
|
||||
'E' | 'e' if matches!(chars.peek(), Some('+' | '-')) => toks.push(Tok::Exp(chars.next() == Some('+'))),
|
||||
'#' | '0' | ',' | '.' => toks.push(Tok::Num(c)),
|
||||
c => toks.push(Tok::Lit(c.to_string())),
|
||||
}
|
||||
}
|
||||
toks
|
||||
}
|
||||
|
||||
/// The symbol of a `$€-407` bracket content (`[$€-407]`), if it is one.
|
||||
fn currency(inner: &str) -> Option<&str> {
|
||||
inner.strip_prefix('$').map(|rest| rest.split('-').next().unwrap_or(""))
|
||||
}
|
||||
|
||||
/// Tick text for `v` in a number format code (`#,##0`, `0.0`, `0%`, `0.00E+00`, and literal text
|
||||
/// such as `"€"0.00` or `#,##0.00 [$€-407]`). A negative number takes the second section when
|
||||
/// there is one, which shows it without a minus (`#,##0;(#,##0)`). `General` shows as many
|
||||
/// decimals as the step needs.
|
||||
pub(super) fn format_value(v: f64, code: &str, step: f64) -> String {
|
||||
let v = if v.abs() < step * 1e-9 { 0.0 } else { v };
|
||||
let secs = sections(code);
|
||||
match secs.get(1) {
|
||||
Some(neg) if v < 0.0 => format_section(-v, neg, step),
|
||||
_ => format_section(v, secs.first().copied().unwrap_or(""), step),
|
||||
}
|
||||
}
|
||||
|
||||
/// `v` in one section of a format code.
|
||||
fn format_section(v: f64, section: &str, step: f64) -> String {
|
||||
let toks = format_toks(section);
|
||||
let is_num = |t: &Tok| matches!(t, Tok::Num(_) | Tok::Exp(_));
|
||||
let lit = |r: &[Tok]| -> String {
|
||||
r.iter()
|
||||
.filter_map(|t| match t {
|
||||
Tok::Lit(l) => Some(l.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
let (Some(s), Some(e)) = (toks.iter().position(is_num), toks.iter().rposition(is_num)) else { return general(v, step) };
|
||||
let body = toks.get(s..=e).unwrap_or(&[]);
|
||||
let (pre, suf) = (lit(toks.get(..s).unwrap_or(&[])), lit(toks.get(e + 1..).unwrap_or(&[])));
|
||||
let digits = |r: &[Tok]| -> String { r.iter().filter_map(|t| if let Tok::Num(c) = t { Some(*c) } else { None }).collect() };
|
||||
if let Some(k) = body.iter().position(|t| matches!(t, Tok::Exp(_))) {
|
||||
let plus = matches!(body.get(k), Some(Tok::Exp(true)));
|
||||
let mantissa = digits(body.get(..k).unwrap_or(&[]));
|
||||
let exp_digits = digits(body.get(k + 1..).unwrap_or(&[])).len();
|
||||
return format!("{pre}{}{suf}", scientific(v, decimals(&mantissa), exp_digits, plus));
|
||||
}
|
||||
let body = digits(body);
|
||||
let pct = pre.contains('%') || suf.contains('%');
|
||||
let v = if pct { v * 100.0 } else { v };
|
||||
let digits = format!("{:.*}", decimals(&body), v.abs());
|
||||
let (ip, fp) = match digits.split_once('.') {
|
||||
Some((a, b)) => (a.to_string(), format!(".{b}")),
|
||||
None => (digits.clone(), String::new()),
|
||||
};
|
||||
let mut grouped = String::new();
|
||||
for (k, ch) in ip.chars().enumerate() {
|
||||
if body.contains(',') && k > 0 && (ip.len() - k) % 3 == 0 {
|
||||
grouped.push(',');
|
||||
}
|
||||
grouped.push(ch);
|
||||
}
|
||||
let sign = if v < 0.0 && digits.chars().any(|c| c != '0' && c != '.') { "-" } else { "" };
|
||||
format!("{sign}{pre}{grouped}{fp}{suf}")
|
||||
}
|
||||
|
||||
/// Decimal places of a number body (`#,##0.00` has 2).
|
||||
fn decimals(body: &str) -> usize {
|
||||
body.split_once('.').map_or(0, |(_, d)| d.chars().filter(|c| matches!(c, '0' | '#')).count()).min(12)
|
||||
}
|
||||
|
||||
/// `v` with no number format (`General`): as many decimals as the tick step needs.
|
||||
fn general(v: f64, step: f64) -> String {
|
||||
let mut d = 0;
|
||||
while d < 6 && (step * 10f64.powi(d)).fract().abs() > 1e-9 {
|
||||
d += 1;
|
||||
}
|
||||
let s = format!("{:.*}", d as usize, v);
|
||||
if s.trim_start_matches(['-', '0', '.']).is_empty() { s.trim_start_matches('-').to_string() } else { s }
|
||||
}
|
||||
|
||||
/// `v` in scientific notation: `decimals` mantissa decimals, at least `exp_digits` exponent
|
||||
/// digits, and a `+` on positive exponents when `plus` (`0.00E+00`: 1.23E+04).
|
||||
fn scientific(v: f64, decimals: usize, exp_digits: usize, plus: bool) -> String {
|
||||
let s = format!("{:.*e}", decimals, v.abs());
|
||||
let (mantissa, exp) = s.split_once('e').unwrap_or((s.as_str(), "0"));
|
||||
let exp: i32 = if v == 0.0 { 0 } else { exp.parse().unwrap_or(0) };
|
||||
let sign = if exp < 0 {
|
||||
"-"
|
||||
} else if plus {
|
||||
"+"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let minus = if v < 0.0 { "-" } else { "" };
|
||||
format!("{minus}{mantissa}E{sign}{:0w$}", exp.unsigned_abs(), w = exp_digits.clamp(1, 4))
|
||||
}
|
||||
|
||||
/// A moment as Excel shows a serial day number.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(super) struct Stamp {
|
||||
pub(super) y: i64,
|
||||
pub(super) m: u32,
|
||||
pub(super) d: u32,
|
||||
/// Day of the week, 0 = Sunday.
|
||||
weekday: u32,
|
||||
/// Seconds into the day.
|
||||
secs: u32,
|
||||
}
|
||||
|
||||
/// The moment of Excel serial day `serial` as Excel shows it, from 0 ("January 0, 1900") to
|
||||
/// 31 December 9999. In the 1900 system serial 60 is Excel's phantom 29 February 1900, and
|
||||
/// serials before it count from 31 December 1899; in the 1904 system (`date1904`) serial 0 is
|
||||
/// 1 January 1904. `None` outside that range.
|
||||
pub(super) fn excel_stamp(serial: f64, date1904: bool) -> Option<Stamp> {
|
||||
let serial = if date1904 { serial + 1462.0 } else { serial };
|
||||
if !(0.0..2_958_466.0).contains(&serial) {
|
||||
return None;
|
||||
}
|
||||
let whole = serial.floor();
|
||||
let secs = (((serial - whole) * 86_400.0).round() as u32).min(86_399);
|
||||
let day = whole as i64;
|
||||
let (y, m, d) = match day {
|
||||
0 => (1900, 1, 0),
|
||||
60 => (1900, 2, 29),
|
||||
1..=59 => civil(day + 1),
|
||||
_ => civil(day),
|
||||
};
|
||||
// Excel's weekdays count serial 1 as a Sunday.
|
||||
Some(Stamp { y, m, d, weekday: (day - 1).rem_euclid(7) as u32, secs })
|
||||
}
|
||||
|
||||
/// Calendar date (year, month, day) of day `n` counted from 30 December 1899 (an Excel serial
|
||||
/// day number). Serial `n` is `n - 25_569` days from 1970-01-01.
|
||||
///
|
||||
/// The caller's serial guard keeps `n` inside chrono's range, but the Gregorian calendar repeats
|
||||
/// every 400 years (146_097 days), so this stays exact for any `n` without hand-rolled calendar
|
||||
/// math: chrono converts the day within its cycle and whole cycles go back into the year (an
|
||||
/// i128 so the extremes can't overflow). 1970-01-01 is `num_days_from_ce` 719_163, so serial `n`
|
||||
/// is CE day `n - 25_569 + 719_163`.
|
||||
pub(super) fn civil(n: i64) -> (i64, u32, u32) {
|
||||
use chrono::Datelike;
|
||||
// CE day of serial `n`, in i128 so no `n` overflows.
|
||||
let from_ce = i128::from(n) - 25_569 + 719_163;
|
||||
let (cycles, rest) = (from_ce.div_euclid(146_097), from_ce.rem_euclid(146_097));
|
||||
match i32::try_from(rest).ok().and_then(chrono::NaiveDate::from_num_days_from_ce_opt) {
|
||||
Some(date) => {
|
||||
let y = i128::from(date.year()) + cycles * 400;
|
||||
(i64::try_from(y).unwrap_or(if y < 0 { i64::MIN } else { i64::MAX }), date.month(), date.day())
|
||||
}
|
||||
// Unreachable: every `rest` in 0..146_097 is a valid CE day.
|
||||
None => (1900, 1, 1),
|
||||
}
|
||||
}
|
||||
|
||||
const MONTHS: [&str; 12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
||||
|
||||
const WEEKDAYS: [&str; 7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
||||
|
||||
/// Whether a format code (outside its quoted and bracketed parts) has date or time fields.
|
||||
pub(super) fn is_date_code(code: &str) -> bool {
|
||||
format_toks_raw(code).any(|c| matches!(c.to_ascii_lowercase(), 'y' | 'd' | 'm' | 'h' | 's'))
|
||||
}
|
||||
|
||||
/// The characters of a format code's first section outside quotes and brackets.
|
||||
fn format_toks_raw(code: &str) -> impl Iterator<Item = char> + '_ {
|
||||
let mut quoted = false;
|
||||
let mut bracket = false;
|
||||
first_section(code).chars().filter(move |c| match c {
|
||||
'"' => {
|
||||
quoted = !quoted;
|
||||
false
|
||||
}
|
||||
'[' if !quoted => {
|
||||
bracket = true;
|
||||
false
|
||||
}
|
||||
']' if bracket => {
|
||||
bracket = false;
|
||||
false
|
||||
}
|
||||
_ => !quoted && !bracket,
|
||||
})
|
||||
}
|
||||
|
||||
/// A piece of a date format: literal text, a field (`y`, `m`, `d`, `h`, `s`, or `n` for minutes)
|
||||
/// with its length, or AM/PM (`AM/PM` when true, `A/P` when false).
|
||||
enum DTok {
|
||||
Lit(String),
|
||||
Field(char, usize),
|
||||
AmPm(bool),
|
||||
}
|
||||
|
||||
/// The pieces of a date format's first section. `m` or `mm` right after an hour or right before
|
||||
/// a seconds field is minutes.
|
||||
fn date_toks(code: &str) -> Vec<DTok> {
|
||||
let section: Vec<char> = first_section(code).chars().collect();
|
||||
let mut toks = Vec::new();
|
||||
let mut i = 0;
|
||||
while let Some(&c) = section.get(i) {
|
||||
let at = i;
|
||||
i += 1;
|
||||
match c {
|
||||
'"' => {
|
||||
let lit: String = section.get(i..).unwrap_or(&[]).iter().take_while(|q| **q != '"').collect();
|
||||
i += lit.chars().count() + 1;
|
||||
toks.push(DTok::Lit(lit));
|
||||
}
|
||||
'[' => {
|
||||
let inner: String = section.get(i..).unwrap_or(&[]).iter().take_while(|q| **q != ']').collect();
|
||||
i += inner.chars().count() + 1;
|
||||
toks.extend(currency(&inner).map(|s| DTok::Lit(s.to_string())));
|
||||
}
|
||||
'\\' | '_' | '*' => {
|
||||
let next = section.get(i).copied();
|
||||
i += 1;
|
||||
match (c, next) {
|
||||
('\\', Some(n)) => toks.push(DTok::Lit(n.to_string())),
|
||||
('_', _) => toks.push(DTok::Lit(" ".to_string())),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
'a' | 'A' if starts_ci(§ion, at, "AM/PM") => {
|
||||
i += 4;
|
||||
toks.push(DTok::AmPm(true));
|
||||
}
|
||||
'a' | 'A' if starts_ci(§ion, at, "A/P") => {
|
||||
i += 2;
|
||||
toks.push(DTok::AmPm(false));
|
||||
}
|
||||
_ if matches!(c.to_ascii_lowercase(), 'y' | 'm' | 'd' | 'h' | 's') => {
|
||||
let mut n = 1;
|
||||
while section.get(i).is_some_and(|q| q.eq_ignore_ascii_case(&c)) {
|
||||
i += 1;
|
||||
n += 1;
|
||||
}
|
||||
toks.push(DTok::Field(c.to_ascii_lowercase(), n));
|
||||
}
|
||||
c => toks.push(DTok::Lit(c.to_string())),
|
||||
}
|
||||
}
|
||||
mark_minutes(&mut toks);
|
||||
toks
|
||||
}
|
||||
|
||||
/// Whether `chars` from `at` on start with `pat`, ignoring ASCII case.
|
||||
fn starts_ci(chars: &[char], at: usize, pat: &str) -> bool {
|
||||
pat.chars().enumerate().all(|(k, p)| chars.get(at + k).is_some_and(|c| c.eq_ignore_ascii_case(&p)))
|
||||
}
|
||||
|
||||
/// Turns the `m`/`mm` fields that mean minutes (after an hour, or before seconds) into `n`.
|
||||
fn mark_minutes(toks: &mut [DTok]) {
|
||||
let fields: Vec<usize> = toks.iter().enumerate().filter(|(_, t)| matches!(t, DTok::Field(..))).map(|(k, _)| k).collect();
|
||||
let kind = |k: Option<&usize>, toks: &[DTok]| match k.and_then(|k| toks.get(*k)) {
|
||||
Some(DTok::Field(c, _)) => Some(*c),
|
||||
_ => None,
|
||||
};
|
||||
for (j, k) in fields.iter().enumerate() {
|
||||
let after_hour = j.checked_sub(1).and_then(|p| kind(fields.get(p), toks)) == Some('h');
|
||||
let before_secs = kind(fields.get(j + 1), toks) == Some('s');
|
||||
if let Some(DTok::Field(c @ 'm', n)) = toks.get_mut(*k)
|
||||
&& *n <= 2
|
||||
&& (after_hour || before_secs)
|
||||
{
|
||||
*c = 'n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A moment in a date format: day, month, year, weekday (`ddd`, `dddd`), hour, minute and
|
||||
/// second fields replaced (12-hour with AM/PM); quoted text and currency kept, other brackets
|
||||
/// dropped.
|
||||
pub(super) fn date_text(t: Stamp, code: &str) -> String {
|
||||
let toks = date_toks(code);
|
||||
let twelve = toks.iter().any(|k| matches!(k, DTok::AmPm(_)));
|
||||
let hour = t.secs / 3600;
|
||||
let mut out = String::new();
|
||||
for tok in &toks {
|
||||
match tok {
|
||||
DTok::Lit(s) => out.push_str(s),
|
||||
DTok::AmPm(long) => out.push_str(match (hour < 12, long) {
|
||||
(true, true) => "AM",
|
||||
(false, true) => "PM",
|
||||
(true, false) => "A",
|
||||
(false, false) => "P",
|
||||
}),
|
||||
DTok::Field(c, n) => out.push_str(&field_text(t, *c, *n, twelve)),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// One date or time field (`c` as in [`DTok::Field`], `n` letters long).
|
||||
fn field_text(t: Stamp, c: char, n: usize, twelve: bool) -> String {
|
||||
let month = MONTHS.get((t.m as usize).clamp(1, 12) - 1).copied().unwrap_or("");
|
||||
let weekday = WEEKDAYS.get(t.weekday as usize).copied().unwrap_or("");
|
||||
let pad = |v: u32| if n >= 2 { format!("{v:02}") } else { v.to_string() };
|
||||
let hour = t.secs / 3600;
|
||||
match c {
|
||||
'y' if n <= 2 => format!("{:02}", t.y.rem_euclid(100)),
|
||||
'y' => format!("{:04}", t.y),
|
||||
'd' if n <= 2 => pad(t.d),
|
||||
'd' if n == 3 => weekday.get(..3).unwrap_or(weekday).to_string(),
|
||||
'd' => weekday.to_string(),
|
||||
'm' if n <= 2 => pad(t.m),
|
||||
'm' if n == 3 => month.get(..3).unwrap_or(month).to_string(),
|
||||
'm' if n == 4 => month.to_string(),
|
||||
'm' => month.get(..1).unwrap_or(month).to_string(),
|
||||
'h' if twelve => pad(if hour.is_multiple_of(12) { 12 } else { hour % 12 }),
|
||||
'h' => pad(hour),
|
||||
'n' => pad(t.secs / 60 % 60),
|
||||
_ => pad(t.secs % 60),
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
use super::Rgb;
|
||||
|
||||
/// A data label to draw once the chart is: the value at (`x`, `y`), centred there (`inside`, in
|
||||
/// white on the fill) or just beside it (above, or right of a horizontal bar's end).
|
||||
pub(super) struct DataLabel {
|
||||
pub(super) x: f32,
|
||||
pub(super) y: f32,
|
||||
pub(super) text: String,
|
||||
pub(super) inside: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Kind {
|
||||
Bar,
|
||||
Line,
|
||||
Area,
|
||||
Pie,
|
||||
Doughnut,
|
||||
Scatter,
|
||||
Radar,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
pub(super) fn of(name: &str) -> Option<Kind> {
|
||||
Some(match name {
|
||||
"c:barChart" => Kind::Bar,
|
||||
"c:lineChart" => Kind::Line,
|
||||
"c:areaChart" => Kind::Area,
|
||||
"c:pieChart" => Kind::Pie,
|
||||
"c:doughnutChart" => Kind::Doughnut,
|
||||
"c:scatterChart" => Kind::Scatter,
|
||||
"c:radarChart" => Kind::Radar,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Grouping {
|
||||
Clustered,
|
||||
Stacked,
|
||||
Percent,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Pos {
|
||||
Right,
|
||||
Left,
|
||||
Top,
|
||||
Bottom,
|
||||
}
|
||||
|
||||
/// One value axis (or category axis) as the file states it.
|
||||
#[derive(Default)]
|
||||
pub(super) struct Axis {
|
||||
pub(super) deleted: bool,
|
||||
pub(super) grid: bool,
|
||||
pub(super) line: bool,
|
||||
/// `c:crossBetween val="midCat"`: categories sit on the tick marks, not between them.
|
||||
pub(super) mid: bool,
|
||||
pub(super) min: Option<f64>,
|
||||
pub(super) max: Option<f64>,
|
||||
/// Number format of the tick labels, when not linked to the source data.
|
||||
pub(super) fmt: Option<String>,
|
||||
}
|
||||
|
||||
pub(super) struct Series {
|
||||
pub(super) name: String,
|
||||
pub(super) color: Option<Rgb>,
|
||||
pub(super) line_w: f32,
|
||||
/// Whether the series is drawn as a line (scatter series without one get markers).
|
||||
pub(super) line: bool,
|
||||
pub(super) values: Vec<Option<f64>>,
|
||||
/// Scatter x values (index + 1 when the file has none).
|
||||
pub(super) xs: Vec<Option<f64>>,
|
||||
/// Per-point colours from `c:dPt`.
|
||||
pub(super) points: Vec<Option<Rgb>>,
|
||||
pub(super) fmt: Option<String>,
|
||||
/// Format code of the scatter x values' cache.
|
||||
pub(super) x_fmt: Option<String>,
|
||||
/// Data labels on or off for this series (`c:dLbls` of its own), when it says.
|
||||
pub(super) labels: Option<bool>,
|
||||
/// The values as the file has them, for data labels (`values` become shares in a 100%
|
||||
/// stacked chart); empty when the series shows no labels.
|
||||
pub(super) raw: Vec<Option<f64>>,
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
use super::format::{date_text, excel_stamp, format_value, is_date_code};
|
||||
use super::model::{Axis, Grouping, Kind, Pos, Series};
|
||||
use super::plot::label;
|
||||
use super::{Chart, El, FONT, LINE_W, MAX_CELLS, MAX_POINTS, MAX_RADAR_SERIES, MAX_SERIES, MAX_SPOKES, Rgb, fill_colour, int, measure, slot};
|
||||
|
||||
impl Chart {
|
||||
pub(super) fn parse(space: &El, theme: &[Rgb]) -> Option<Chart> {
|
||||
let chart = space.child("c:chart")?;
|
||||
let plot = chart.child("c:plotArea")?;
|
||||
let ty = plot.els().find(|e| Kind::of(&e.name).is_some())?;
|
||||
let kind = Kind::of(&ty.name)?;
|
||||
let scatter = kind == Kind::Scatter;
|
||||
let date1904 = space.child("c:date1904").is_some_and(|d| d.attr("val").is_none_or(truthy));
|
||||
let mut series = read_series(ty, scatter, theme);
|
||||
if series.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let cats = ty.children("c:ser").find_map(|s| s.child("c:cat")).map(|c| labels(Some(c), MAX_POINTS, date1904)).unwrap_or_default();
|
||||
let n = if kind == Kind::Radar {
|
||||
series.iter().map(|s| s.values.len()).max().unwrap_or(0).max(cats.len()).min(MAX_SPOKES)
|
||||
} else if matches!(kind, Kind::Pie | Kind::Doughnut) {
|
||||
series.first().map_or(0, |s| s.values.len()).max(cats.len())
|
||||
} else {
|
||||
series.iter().map(|s| s.values.len()).max().unwrap_or(0).max(cats.len())
|
||||
};
|
||||
// Every series is padded to `n` points, so keep only as many as the cell budget holds.
|
||||
if let Some(keep) = MAX_CELLS.checked_div(n) {
|
||||
series.truncate(keep.max(1));
|
||||
}
|
||||
if kind == Kind::Radar {
|
||||
series.truncate(MAX_RADAR_SERIES);
|
||||
}
|
||||
let grouping = match ty.child("c:grouping").and_then(|g| g.attr("val")) {
|
||||
Some("stacked") if matches!(kind, Kind::Bar | Kind::Area | Kind::Line) => Grouping::Stacked,
|
||||
Some("percentStacked") if matches!(kind, Kind::Bar | Kind::Area | Kind::Line) => Grouping::Percent,
|
||||
_ => Grouping::Clustered,
|
||||
};
|
||||
let mut cats = cats;
|
||||
cats.resize(n, String::new());
|
||||
let radar_style = ty.child("c:radarStyle").and_then(|r| r.attr("val"));
|
||||
let type_labels = show_values(ty.child("c:dLbls"));
|
||||
for s in &mut series {
|
||||
s.values.resize(n, None);
|
||||
s.xs.resize(n, None);
|
||||
s.labels = Some(s.labels.or(type_labels).unwrap_or(false));
|
||||
if s.labels == Some(true) {
|
||||
s.raw = s.values.clone();
|
||||
}
|
||||
}
|
||||
if grouping == Grouping::Percent {
|
||||
to_percent(&mut series, n);
|
||||
}
|
||||
let (cat, val) = chart_axes(plot, scatter);
|
||||
let val_fmt = if grouping == Grouping::Percent {
|
||||
"0%".to_string()
|
||||
} else {
|
||||
val.fmt.clone().or_else(|| series.iter().find_map(|s| s.fmt.clone())).unwrap_or_else(|| "General".to_string())
|
||||
};
|
||||
let num = |n: &str| ty.child(n).and_then(|g| g.attr("val")).and_then(int);
|
||||
Some(Chart {
|
||||
kind,
|
||||
horizontal: kind == Kind::Bar && ty.child("c:barDir").and_then(|d| d.attr("val")) == Some("bar"),
|
||||
grouping,
|
||||
gap: num("c:gapWidth").unwrap_or(150).clamp(0, 500) as f32 / 100.0,
|
||||
overlap: num("c:overlap").unwrap_or(0).clamp(-100, 100) as f32 / 100.0,
|
||||
hole: num("c:holeSize").unwrap_or(50).clamp(10, 90) as f32 / 100.0,
|
||||
first_angle: num("c:firstSliceAng").unwrap_or(0).clamp(0, 360) as f64,
|
||||
radar_fill: radar_style == Some("filled"),
|
||||
radar_marks: radar_style == Some("marker"),
|
||||
vary: matches!(kind, Kind::Pie | Kind::Doughnut) && ty.child("c:varyColors").and_then(|v| v.attr("val")).is_none_or(truthy),
|
||||
n,
|
||||
cats,
|
||||
title: title_of(chart, &series),
|
||||
series,
|
||||
accents: (4..10).map(|i| slot(theme, i)).collect(),
|
||||
legend: legend_pos(chart),
|
||||
cat,
|
||||
val,
|
||||
text_size: text_size(space),
|
||||
val_fmt,
|
||||
date1904,
|
||||
plot_border: plot.child("c:spPr").and_then(|p| p.child("a:ln")).and_then(|l| fill_colour(l, theme)),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn stacked(&self) -> bool {
|
||||
self.grouping != Grouping::Clustered
|
||||
}
|
||||
|
||||
fn accent(&self, i: usize) -> Rgb {
|
||||
self.accents.get(i % 6).copied().unwrap_or(Rgb::BLACK)
|
||||
}
|
||||
|
||||
pub(super) fn series_colour(&self, j: usize) -> Rgb {
|
||||
self.series.get(j).and_then(|s| s.color).unwrap_or_else(|| self.accent(j))
|
||||
}
|
||||
|
||||
/// Colour of point `i` of series `j`: its `c:dPt` override, else the varied accent (pie) or
|
||||
/// the series colour.
|
||||
pub(super) fn point_colour(&self, j: usize, i: usize) -> Rgb {
|
||||
if let Some(Some(c)) = self.series.get(j).and_then(|s| s.points.get(i)) {
|
||||
return *c;
|
||||
}
|
||||
if self.vary { self.accent(i) } else { self.series_colour(j) }
|
||||
}
|
||||
|
||||
pub(super) fn legend_entries(&self) -> Vec<(String, Rgb)> {
|
||||
if matches!(self.kind, Kind::Pie | Kind::Doughnut) {
|
||||
return self
|
||||
.cats
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, c)| (if c.is_empty() { (i + 1).to_string() } else { c.clone() }, self.point_colour(0, i)))
|
||||
.collect();
|
||||
}
|
||||
self.series
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(j, s)| (if s.name.is_empty() { format!("Series {}", j + 1) } else { s.name.clone() }, self.series_colour(j)))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// The series of a chart type element, within the chart's cell budget.
|
||||
pub(super) fn read_series(ty: &El, scatter: bool, theme: &[Rgb]) -> Vec<Series> {
|
||||
let mut budget = MAX_CELLS;
|
||||
let mut series = Vec::new();
|
||||
for ser in ty.children("c:ser").take(MAX_SERIES) {
|
||||
let s = Series::parse(ser, scatter, theme, budget.min(MAX_POINTS));
|
||||
budget = budget.saturating_sub(s.values.len().max(s.xs.len()));
|
||||
series.push(s);
|
||||
}
|
||||
series
|
||||
}
|
||||
|
||||
/// Values as their share of each category's total (100% stacked charts).
|
||||
fn to_percent(series: &mut [Series], n: usize) {
|
||||
for i in 0..n {
|
||||
let total: f64 = series.iter().map(|s| s.values.get(i).copied().flatten().map_or(0.0, f64::abs)).sum();
|
||||
for s in series.iter_mut() {
|
||||
if let Some(Some(v)) = s.values.get_mut(i) {
|
||||
*v = if total > 0.0 { *v / total } else { 0.0 };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The category axis (scatter: the horizontal value axis) and the value axis of a plot area.
|
||||
fn chart_axes(plot: &El, scatter: bool) -> (Axis, Axis) {
|
||||
let vals_ax: Vec<&El> = plot.children("c:valAx").collect();
|
||||
let horiz_pos = |a: &&El| matches!(a.child("c:axPos").and_then(|p| p.attr("val")), Some("b" | "t"));
|
||||
let (val_el, x_el) = if scatter {
|
||||
let x = vals_ax.iter().copied().find(horiz_pos).or_else(|| vals_ax.first().copied());
|
||||
let y = vals_ax.iter().copied().find(|a| !horiz_pos(a)).or_else(|| vals_ax.iter().copied().find(|a| Some(*a) != x));
|
||||
(y, x)
|
||||
} else {
|
||||
(vals_ax.first().copied(), None)
|
||||
};
|
||||
let cat_el = plot.els().find(|e| matches!(e.name.as_str(), "c:catAx" | "c:dateAx"));
|
||||
(axis(if scatter { x_el } else { cat_el }), axis(val_el))
|
||||
}
|
||||
|
||||
/// The chart's title. An empty `c:title` shows the series name (Word's automatic title of a
|
||||
/// one-series chart) unless the automatic title is deleted.
|
||||
pub(super) fn title_of(chart: &El, series: &[Series]) -> Option<String> {
|
||||
let auto_deleted = chart.child("c:autoTitleDeleted").and_then(|a| a.attr("val")).is_some_and(truthy);
|
||||
let auto = if !auto_deleted && series.len() == 1 { series.first().map(|s| s.name.clone()).filter(|s| !s.is_empty()) } else { None };
|
||||
chart.child("c:title").and_then(|t| title_text(t).or(auto))
|
||||
}
|
||||
|
||||
/// Whether a `c:dLbls` shows the values (`None` when there's none, or it doesn't say).
|
||||
fn show_values(d: Option<&El>) -> Option<bool> {
|
||||
let d = d?;
|
||||
if d.child("c:delete").and_then(|e| e.attr("val")).is_some_and(truthy) {
|
||||
return Some(false);
|
||||
}
|
||||
d.child("c:showVal").map(|v| v.attr("val").is_none_or(truthy))
|
||||
}
|
||||
|
||||
fn legend_pos(chart: &El) -> Option<Pos> {
|
||||
chart.child("c:legend").map(|l| match l.child("c:legendPos").and_then(|p| p.attr("val")).unwrap_or("r") {
|
||||
"l" => Pos::Left,
|
||||
"t" | "tr" => Pos::Top,
|
||||
"b" => Pos::Bottom,
|
||||
_ => Pos::Right,
|
||||
})
|
||||
}
|
||||
|
||||
/// The chart's text size (its `c:txPr` default run size), points.
|
||||
pub(super) fn text_size(space: &El) -> f32 {
|
||||
space
|
||||
.child("c:txPr")
|
||||
.and_then(|t| t.find("a:defRPr"))
|
||||
.and_then(|d| d.attr("sz"))
|
||||
.and_then(int)
|
||||
.map_or(FONT, |v| (v as f32 / 100.0).clamp(6.0, 24.0))
|
||||
}
|
||||
|
||||
impl Series {
|
||||
fn parse(ser: &El, scatter: bool, theme: &[Rgb], cap: usize) -> Series {
|
||||
let name = ser.child("c:tx").and_then(|t| t.find("c:v")).map(|v| label(v.text().trim())).unwrap_or_default();
|
||||
let sp = ser.child("c:spPr");
|
||||
let ln = sp.and_then(|p| p.child("a:ln"));
|
||||
let marker = ser.child("c:marker").and_then(|m| m.child("c:spPr")).and_then(|p| fill_colour(p, theme));
|
||||
let color = sp.and_then(|p| fill_colour(p, theme)).or_else(|| ln.and_then(|l| fill_colour(l, theme))).or(marker);
|
||||
let line_w = ln.and_then(|l| l.attr("w")).and_then(|v| measure(v, 12_700.0)).map_or(LINE_W, |w| w.clamp(0.0, 20.0));
|
||||
let line = ln.is_none_or(|l| l.child("a:noFill").is_none());
|
||||
let (values, fmt) = numbers(ser.child(if scatter { "c:yVal" } else { "c:val" }), cap);
|
||||
let (mut xs, x_fmt) = if scatter { numbers(ser.child("c:xVal"), cap) } else { (Vec::new(), None) };
|
||||
if scatter && xs.is_empty() {
|
||||
xs = (1..=values.len()).map(|i| Some(i as f64)).collect();
|
||||
}
|
||||
let mut points = vec![None; values.len()];
|
||||
for dpt in ser.children("c:dPt") {
|
||||
let i = dpt.child("c:idx").and_then(|e| e.attr("val")).and_then(int).and_then(|i| usize::try_from(i).ok());
|
||||
let c = dpt.child("c:spPr").and_then(|p| fill_colour(p, theme));
|
||||
if let Some(slot) = i.and_then(|i| points.get_mut(i)) {
|
||||
*slot = c;
|
||||
}
|
||||
}
|
||||
let labels = show_values(ser.child("c:dLbls"));
|
||||
Series { name, color, line_w, line, values, xs, points, fmt, x_fmt, labels, raw: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Longest number-format code honoured (the limit spreadsheet apps use); longer ones are ignored.
|
||||
const MAX_FORMAT_CODE: usize = 255;
|
||||
|
||||
/// Category labels of a `c:cat` (string or number cache), capped. Numbers show in the cache's
|
||||
/// format code (dates included); text and `General` numbers as they are stored.
|
||||
pub(super) fn labels(el: Option<&El>, cap: usize, date1904: bool) -> Vec<String> {
|
||||
let Some(el) = el else { return Vec::new() };
|
||||
let Some(cache) = el.find("c:strCache").or_else(|| el.find("c:numCache")).or_else(|| el.find("c:strLit")).or_else(|| el.find("c:numLit")) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let code =
|
||||
cache.child("c:formatCode").map(|f| f.text()).filter(|f| !f.is_empty() && f.len() <= MAX_FORMAT_CODE && !f.eq_ignore_ascii_case("General"));
|
||||
pts(cache, cap)
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
let text = p.unwrap_or_default();
|
||||
match (code.as_deref(), text.trim().parse::<f64>()) {
|
||||
(Some(code), Ok(v)) if v.is_finite() => label(&category_text(v, code, 1.0, date1904)),
|
||||
_ => label(&text),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A number in its format code: a date or time for date codes (serial days, from 1904 when
|
||||
/// `date1904`), else as [`format_value`] with tick step `step`.
|
||||
pub(super) fn category_text(v: f64, code: &str, step: f64, date1904: bool) -> String {
|
||||
match excel_stamp(v, date1904) {
|
||||
Some(t) if is_date_code(code) => date_text(t, code),
|
||||
_ => format_value(v, code, step),
|
||||
}
|
||||
}
|
||||
|
||||
/// Values of a `c:val`-style element (number cache or literal), capped; with its format code.
|
||||
pub(super) fn numbers(el: Option<&El>, cap: usize) -> (Vec<Option<f64>>, Option<String>) {
|
||||
let Some(cache) = el.and_then(|e| e.find("c:numCache").or_else(|| e.find("c:numLit"))) else {
|
||||
return (Vec::new(), None);
|
||||
};
|
||||
let fmt = cache.child("c:formatCode").map(|f| f.text()).filter(|f| !f.is_empty() && f.len() <= MAX_FORMAT_CODE);
|
||||
let vals = pts(cache, cap).into_iter().map(|p| p.and_then(|t| t.trim().parse::<f64>().ok()).filter(|v| v.is_finite())).collect();
|
||||
(vals, fmt)
|
||||
}
|
||||
|
||||
/// The `c:pt` texts of a cache by index. Indexes and counts from the file are capped.
|
||||
fn pts(cache: &El, cap: usize) -> Vec<Option<String>> {
|
||||
let declared = cache.child("c:ptCount").and_then(|c| c.attr("val")).and_then(int).unwrap_or(0).clamp(0, cap as i64) as usize;
|
||||
let mut out: Vec<Option<String>> = vec![None; declared];
|
||||
for p in cache.children("c:pt") {
|
||||
let Some(i) = p.attr("idx").and_then(int).and_then(|i| usize::try_from(i).ok()) else { continue };
|
||||
if i >= cap {
|
||||
continue;
|
||||
}
|
||||
if out.len() <= i {
|
||||
out.resize(i + 1, None);
|
||||
}
|
||||
if let Some(slot) = out.get_mut(i) {
|
||||
*slot = p.child("c:v").map(|v| v.text());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) fn axis(a: Option<&El>) -> Axis {
|
||||
let Some(a) = a else { return Axis { deleted: true, ..Default::default() } };
|
||||
let scaling = a.child("c:scaling");
|
||||
let num =
|
||||
|n: &str| scaling.and_then(|s| s.child(n)).and_then(|e| e.attr("val")).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite());
|
||||
Axis {
|
||||
deleted: a.child("c:delete").and_then(|d| d.attr("val")).is_some_and(truthy),
|
||||
grid: a.child("c:majorGridlines").is_some(),
|
||||
line: a.child("c:spPr").and_then(|p| p.child("a:ln")).and_then(|l| l.child("a:noFill")).is_none(),
|
||||
mid: a.child("c:crossBetween").and_then(|c| c.attr("val")) == Some("midCat"),
|
||||
min: num("c:min"),
|
||||
max: num("c:max"),
|
||||
fmt: a
|
||||
.child("c:numFmt")
|
||||
.filter(|f| f.attr("sourceLinked") != Some("1"))
|
||||
.and_then(|f| f.attr("formatCode"))
|
||||
.filter(|f| f.len() <= MAX_FORMAT_CODE)
|
||||
.map(str::to_string),
|
||||
}
|
||||
}
|
||||
|
||||
fn title_text(t: &El) -> Option<String> {
|
||||
let tx = t.child("c:tx")?;
|
||||
let s = match tx.child("c:rich") {
|
||||
Some(rich) => {
|
||||
let mut s = String::new();
|
||||
rich_into(rich, &mut s);
|
||||
s
|
||||
}
|
||||
None => tx.find("c:v").map(|v| v.text()).unwrap_or_default(),
|
||||
};
|
||||
let s = label(s.trim());
|
||||
(!s.is_empty()).then_some(s)
|
||||
}
|
||||
|
||||
/// The `a:t` runs under an element, in order.
|
||||
fn rich_into(e: &El, s: &mut String) {
|
||||
for c in e.els() {
|
||||
if c.name == "a:t" {
|
||||
s.push_str(&c.text());
|
||||
} else {
|
||||
rich_into(c, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn truthy(v: &str) -> bool {
|
||||
matches!(v.trim(), "1" | "true")
|
||||
}
|
||||
@@ -1,463 +0,0 @@
|
||||
use super::canvas::{Rect, fill, rect_segs, runs, seg, stroke, text, text_at, text_w, widest, wrap};
|
||||
use super::format::format_value;
|
||||
use super::model::{DataLabel, Grouping, Kind, Series};
|
||||
use super::parse::category_text;
|
||||
use super::polar::{draw_data_labels, label_of};
|
||||
use super::{AXIS, Chart, El, GAP, GRID, GraphicItem, LINE, MAX_LABEL, MAX_TICKS, PathSeg, Rgb, ShapeKind, TextAlign, fill_colour, measure};
|
||||
|
||||
pub(super) fn label(s: &str) -> String {
|
||||
s.chars().take(MAX_LABEL).collect()
|
||||
}
|
||||
|
||||
/// Chart area: the chart space's fill and border (white and borderless by default).
|
||||
pub(super) fn backdrop(out: &mut Vec<GraphicItem>, space: &El, theme: &[Rgb], w: f32, h: f32) {
|
||||
let sp = space.child("c:spPr");
|
||||
let fill = match sp {
|
||||
Some(p) if p.child("a:noFill").is_some() => None,
|
||||
Some(p) => fill_colour(p, theme).or(Some(Rgb::WHITE)),
|
||||
None => Some(Rgb::WHITE),
|
||||
};
|
||||
let ln = sp.and_then(|p| p.child("a:ln"));
|
||||
let stroke = ln.and_then(|l| fill_colour(l, theme));
|
||||
let stroke_width = ln.and_then(|l| l.attr("w")).and_then(|v| measure(v, 12_700.0)).unwrap_or(0.75).clamp(0.0, 20.0);
|
||||
if fill.is_some() || stroke.is_some() {
|
||||
out.push(GraphicItem::Shape { rect: [0.0, 0.0, w, h], kind: ShapeKind::Rectangle, fill, stroke, stroke_width });
|
||||
}
|
||||
}
|
||||
|
||||
/// An axis scale: `lo` to `hi` in steps of `step`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(super) struct Scale {
|
||||
pub(super) lo: f64,
|
||||
pub(super) hi: f64,
|
||||
pub(super) step: f64,
|
||||
}
|
||||
|
||||
impl Scale {
|
||||
pub(super) fn ticks(&self) -> Vec<f64> {
|
||||
let n = (((self.hi - self.lo) / self.step + 1e-9).floor().max(0.0) as usize).min(MAX_TICKS);
|
||||
(0..=n).map(|k| self.lo + k as f64 * self.step).filter(|t| *t <= self.hi + 1e-9).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A scale for data `dlo..dhi` (zero included when `zero`), as Excel and Word pick it: an end
|
||||
/// away from zero sits 5% of the data span beyond the data (an end at zero stays there), rounded
|
||||
/// out to a step of 1, 2, 2.5, 5 or 10 times a power of ten (about seven steps). `min`/`max`
|
||||
/// from the file replace the ends.
|
||||
pub(super) fn nice_scale(dlo: f64, dhi: f64, zero: bool, min: Option<f64>, max: Option<f64>) -> Scale {
|
||||
let (dlo, dhi) = if dhi > dlo { (dlo, dhi) } else { (dlo, dlo + 1.0) };
|
||||
let span = dhi - dlo;
|
||||
// With zero, the ends reach it: all-negative data still starts its bars at zero.
|
||||
let (ends_lo, ends_hi) = if zero { (dlo.min(0.0), dhi.max(0.0)) } else { (dlo, dhi) };
|
||||
let lo = min.unwrap_or(if ends_lo < 0.0 { ends_lo - 0.05 * span } else { ends_lo });
|
||||
let hi = max.unwrap_or(if ends_hi > 0.0 { ends_hi + 0.05 * span } else { ends_hi });
|
||||
if !(hi > lo && hi.is_finite() && lo.is_finite()) {
|
||||
return Scale { lo: 0.0, hi: 1.0, step: 0.2 };
|
||||
}
|
||||
let raw = (hi - lo) / 7.5;
|
||||
let mag = 10f64.powf(raw.log10().floor());
|
||||
let step = [1.0, 2.0, 2.5, 5.0, 10.0].iter().map(|m| m * mag).find(|s| *s >= raw).unwrap_or(10.0 * mag);
|
||||
let lo_r = if min.is_some() { lo } else { ((lo / step) + 1e-9).floor() * step };
|
||||
let hi_r = if max.is_some() { hi } else { ((hi / step) - 1e-9).ceil() * step };
|
||||
if !(step.is_finite() && step > 0.0 && lo_r.is_finite() && hi_r.is_finite() && hi_r > lo_r) {
|
||||
return Scale { lo: 0.0, hi: 1.0, step: 0.2 };
|
||||
}
|
||||
Scale { lo: lo_r, hi: hi_r, step }
|
||||
}
|
||||
|
||||
/// The plot rectangle and the scales that map values and categories onto it.
|
||||
struct Frame {
|
||||
plot: Rect,
|
||||
horiz: bool,
|
||||
n: usize,
|
||||
vs: Scale,
|
||||
xs: Option<Scale>,
|
||||
mid: bool,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
/// Width of one category band.
|
||||
fn band(&self) -> f32 {
|
||||
let len = if self.horiz { self.plot.h } else { self.plot.w };
|
||||
len / self.n.max(1) as f32
|
||||
}
|
||||
|
||||
/// Start of category `i`'s band: its left edge, or for bars its top edge (category 0 at the
|
||||
/// bottom).
|
||||
fn band_start(&self, i: usize) -> f32 {
|
||||
let k = i as f32;
|
||||
if self.horiz { self.plot.bottom() - (k + 1.0) * self.band() } else { self.plot.x + k * self.band() }
|
||||
}
|
||||
|
||||
/// A value's position along the value direction: y for columns, x for bars.
|
||||
fn val(&self, v: f64) -> f32 {
|
||||
let t = ((v - self.vs.lo) / (self.vs.hi - self.vs.lo)).clamp(0.0, 1.0) as f32;
|
||||
if self.horiz { self.plot.x + t * self.plot.w } else { self.plot.bottom() - t * self.plot.h }
|
||||
}
|
||||
|
||||
/// x of category `i` for line and area points.
|
||||
fn cat_x(&self, i: usize) -> f32 {
|
||||
if !self.mid {
|
||||
return self.plot.x + (i as f32 + 0.5) * self.band();
|
||||
}
|
||||
if self.n <= 1 { self.plot.x + self.plot.w / 2.0 } else { self.plot.x + self.plot.w * i as f32 / (self.n - 1) as f32 }
|
||||
}
|
||||
|
||||
/// x of a scatter point.
|
||||
fn scat_x(&self, x: f64) -> f32 {
|
||||
match self.xs {
|
||||
Some(s) => self.plot.x + ((x - s.lo) / (s.hi - s.lo)).clamp(0.0, 1.0) as f32 * self.plot.w,
|
||||
None => self.plot.x,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The value scale and, for scatter charts, the x scale, with their ticks and tick texts.
|
||||
pub(super) struct Axes {
|
||||
pub(super) vs: Scale,
|
||||
vticks: Vec<(f64, String)>,
|
||||
xs: Option<Scale>,
|
||||
xticks: Vec<(f64, String)>,
|
||||
}
|
||||
|
||||
impl Axes {
|
||||
pub(super) fn of(c: &Chart) -> Axes {
|
||||
let (dlo, dhi) = extent(c);
|
||||
let (vmin, vmax) = if c.grouping == Grouping::Percent { (Some(0.0), Some(1.0)) } else { (c.val.min, c.val.max) };
|
||||
// Lines and scatters don't need zero for a narrow range (1000..1050 stays on its own scale).
|
||||
let zero = !matches!(c.kind, Kind::Line | Kind::Scatter) || !narrow(dlo, dhi);
|
||||
let vs = nice_scale(dlo, dhi, zero, vmin, vmax);
|
||||
let vticks = vs.ticks().into_iter().map(|t| (t, format_value(t, &c.val_fmt, vs.step))).collect();
|
||||
let xs = (c.kind == Kind::Scatter).then(|| {
|
||||
let (xl, xh) = x_extent(c);
|
||||
nice_scale(xl, xh, false, c.cat.min, c.cat.max)
|
||||
});
|
||||
// Scatter x values show in the axis's own format, else their cache's (dates included).
|
||||
let xfmt = c.cat.fmt.clone().or_else(|| c.series.iter().find_map(|s| s.x_fmt.clone())).unwrap_or_else(|| "General".to_string());
|
||||
let xticks = xs.map(|s| s.ticks().into_iter().map(|t| (t, category_text(t, &xfmt, s.step, c.date1904))).collect()).unwrap_or_default();
|
||||
Axes { vs, vticks, xs, xticks }
|
||||
}
|
||||
}
|
||||
|
||||
/// The room around the plot for axis labels, and the category labels as they'll be drawn.
|
||||
struct Margins {
|
||||
left: f32,
|
||||
top: f32,
|
||||
bottom: f32,
|
||||
/// Category labels in lines (none for scatter charts or a deleted category axis).
|
||||
wrapped: Vec<Vec<String>>,
|
||||
/// Every `skip`-th category label is shown under a column chart.
|
||||
skip: usize,
|
||||
}
|
||||
|
||||
/// The margins the axis labels need around the plot in `area`. Category labels under a column
|
||||
/// chart wrap to the band; labels that still don't fit show every k-th category.
|
||||
fn axis_margins(c: &Chart, ax: &Axes, area: Rect) -> Margins {
|
||||
let fs = c.text_size;
|
||||
let lh = fs * LINE;
|
||||
let scatter = c.kind == Kind::Scatter;
|
||||
let (cat_on, val_on) = (!c.cat.deleted, !c.val.deleted);
|
||||
let left = if c.horizontal {
|
||||
if cat_on { widest(&c.cats, fs).min(area.w * 0.4) + GAP } else { 0.0 }
|
||||
} else if val_on {
|
||||
ax.vticks.iter().map(|(_, s)| text_w(s, fs)).fold(0.0, f32::max) + GAP
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let band_w = (area.w - left).max(1.0) / c.n as f32;
|
||||
let wrapped: Vec<Vec<String>> = if cat_on && !scatter {
|
||||
let width = if c.horizontal { left - GAP } else { band_w };
|
||||
c.cats.iter().map(|s| wrap(s, width, fs)).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let widest_line = wrapped.iter().flatten().map(|s| text_w(s, fs)).fold(0.0, f32::max);
|
||||
let skip = ((widest_line / band_w).ceil() as usize).max(1);
|
||||
let label_lines = wrapped.iter().map(Vec::len).max().unwrap_or(1) as f32;
|
||||
let bottom = if cat_on && !c.horizontal {
|
||||
if scatter { lh + GAP } else { label_lines * lh + GAP }
|
||||
} else if c.horizontal && val_on {
|
||||
lh + GAP
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let top = if !c.horizontal && val_on { lh / 2.0 } else { 0.0 };
|
||||
Margins { left, top, bottom, wrapped, skip }
|
||||
}
|
||||
|
||||
/// A bar, column, line, area or scatter chart in `area`: gridlines, series, axes, then labels.
|
||||
pub(super) fn cartesian(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
if c.n == 0 {
|
||||
return;
|
||||
}
|
||||
let ax = Axes::of(c);
|
||||
let m = axis_margins(c, &ax, area);
|
||||
let plot = Rect { x: area.x + m.left, y: area.y + m.top, w: area.w - m.left, h: area.h - m.top - m.bottom };
|
||||
if plot.w < 1.0 || plot.h < 1.0 {
|
||||
return;
|
||||
}
|
||||
let g = Frame { plot, horiz: c.horizontal, n: c.n, vs: ax.vs, xs: ax.xs, mid: c.val.mid };
|
||||
if c.val.grid {
|
||||
draw_grid(out, &g, &ax.vticks);
|
||||
}
|
||||
let mut labels = Vec::new();
|
||||
match c.kind {
|
||||
Kind::Bar => bars(out, c, &g, &mut labels),
|
||||
Kind::Line => lines(out, c, &g, &mut labels),
|
||||
Kind::Area => areas(out, c, &g, &mut labels),
|
||||
Kind::Scatter => draw_scatter(out, c, &g, &mut labels),
|
||||
Kind::Pie | Kind::Doughnut | Kind::Radar => {}
|
||||
}
|
||||
draw_axes(out, c, &g);
|
||||
draw_data_labels(out, &labels, c.text_size, g.horiz && c.kind == Kind::Bar);
|
||||
if !c.val.deleted {
|
||||
draw_value_labels(out, c, &g, &ax.vticks, Rect { w: m.left, ..area });
|
||||
}
|
||||
if !c.cat.deleted {
|
||||
draw_category_labels(out, c, &g, &ax.xticks, &m, area);
|
||||
}
|
||||
}
|
||||
|
||||
/// Major gridlines at the value ticks.
|
||||
fn draw_grid(out: &mut Vec<GraphicItem>, g: &Frame, ticks: &[(f64, String)]) {
|
||||
let plot = g.plot;
|
||||
for (t, _) in ticks {
|
||||
let p = g.val(*t);
|
||||
let segs = if g.horiz { seg(p, plot.y, p, plot.bottom()) } else { seg(plot.x, p, plot.right(), p) };
|
||||
stroke(out, segs, GRID, 0.75);
|
||||
}
|
||||
}
|
||||
|
||||
/// The category axis line, the plot area's outline and the value axis line, as the file shows them.
|
||||
fn draw_axes(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame) {
|
||||
let plot = g.plot;
|
||||
if !c.cat.deleted && c.cat.line {
|
||||
let segs = if g.horiz { seg(plot.x, plot.y, plot.x, plot.bottom()) } else { seg(plot.x, plot.bottom(), plot.right(), plot.bottom()) };
|
||||
stroke(out, segs, AXIS, 0.75);
|
||||
}
|
||||
if let Some(color) = c.plot_border {
|
||||
stroke(out, rect_segs(plot.x, plot.y, plot.right(), plot.bottom()), color, 0.75);
|
||||
}
|
||||
if !c.val.deleted && c.val.line {
|
||||
let segs = if g.horiz { seg(plot.x, plot.bottom(), plot.right(), plot.bottom()) } else { seg(plot.x, plot.y, plot.x, plot.bottom()) };
|
||||
stroke(out, segs, AXIS, 0.75);
|
||||
}
|
||||
}
|
||||
|
||||
/// Value tick labels: under the plot for bars, else right-aligned in `margin` (the area's left
|
||||
/// margin).
|
||||
fn draw_value_labels(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, ticks: &[(f64, String)], margin: Rect) {
|
||||
let (fs, lh) = (c.text_size, c.text_size * LINE);
|
||||
for (t, s) in ticks {
|
||||
let p = g.val(*t);
|
||||
if g.horiz {
|
||||
text_at(out, p, g.plot.bottom() + GAP / 2.0, s, fs);
|
||||
} else {
|
||||
text(out, Rect { x: margin.x, y: p - lh / 2.0, w: margin.w - GAP, h: lh }, s, fs, TextAlign::Right, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Category labels: scatter x ticks under the plot, bar labels in the left margin, column
|
||||
/// labels under their bands (every `m.skip`-th).
|
||||
fn draw_category_labels(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, xticks: &[(f64, String)], m: &Margins, area: Rect) {
|
||||
let (fs, lh) = (c.text_size, c.text_size * LINE);
|
||||
let plot = g.plot;
|
||||
if c.kind == Kind::Scatter {
|
||||
for (t, s) in xticks {
|
||||
text_at(out, g.scat_x(*t), plot.bottom() + GAP / 2.0, s, fs);
|
||||
}
|
||||
} else if g.horiz {
|
||||
for (i, lines) in m.wrapped.iter().enumerate() {
|
||||
let top = g.band_start(i) + g.band() / 2.0 - lines.len() as f32 * lh / 2.0;
|
||||
for (k, s) in lines.iter().enumerate() {
|
||||
text(out, Rect { x: area.x, y: top + k as f32 * lh, w: m.left - GAP, h: lh }, s, fs, TextAlign::Right, false);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let band = g.band();
|
||||
for (i, lines) in m.wrapped.iter().enumerate().filter(|(i, _)| i % m.skip == 0) {
|
||||
for (k, s) in lines.iter().enumerate() {
|
||||
let y = plot.bottom() + GAP / 2.0 + k as f32 * lh;
|
||||
text(out, Rect { x: g.cat_x(i) - band / 2.0, y, w: band, h: lh }, s, fs, TextAlign::Center, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether data `lo..hi` is a narrow band away from zero: its span is under a sixth of the end
|
||||
/// farther from zero (the mirror image for negative data).
|
||||
pub(super) fn narrow(lo: f64, hi: f64) -> bool {
|
||||
if lo > 0.0 {
|
||||
(hi - lo) / hi < 1.0 / 6.0
|
||||
} else if hi < 0.0 {
|
||||
(hi - lo) / lo.abs() < 1.0 / 6.0
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Lowest and highest value of the plot; stacked series add up per category.
|
||||
fn extent(c: &Chart) -> (f64, f64) {
|
||||
let mut pts: Vec<f64> = Vec::new();
|
||||
if c.stacked() {
|
||||
for i in 0..c.n {
|
||||
let (mut p, mut q) = (0.0, 0.0);
|
||||
for s in &c.series {
|
||||
match s.values.get(i).copied().flatten() {
|
||||
Some(v) if v >= 0.0 => p += v,
|
||||
Some(v) => q += v,
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
pts.push(p);
|
||||
pts.push(q);
|
||||
}
|
||||
} else {
|
||||
pts.extend(c.series.iter().flat_map(|s| s.values.iter().flatten().copied()));
|
||||
}
|
||||
let lo = pts.iter().copied().fold(f64::INFINITY, f64::min);
|
||||
let hi = pts.iter().copied().fold(f64::NEG_INFINITY, f64::max);
|
||||
if lo.is_finite() && hi.is_finite() { (lo, hi) } else { (0.0, 0.0) }
|
||||
}
|
||||
|
||||
/// Range of scatter x values (index + 1 where missing).
|
||||
fn x_extent(c: &Chart) -> (f64, f64) {
|
||||
let xs: Vec<f64> = c.series.iter().flat_map(|s| s.xs.iter().flatten().copied()).collect();
|
||||
let lo = xs.iter().copied().fold(f64::INFINITY, f64::min);
|
||||
let hi = xs.iter().copied().fold(f64::NEG_INFINITY, f64::max);
|
||||
if lo.is_finite() && hi.is_finite() { (lo, hi) } else { (0.0, 1.0) }
|
||||
}
|
||||
|
||||
fn bars(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataLabel>) {
|
||||
let stacked = c.stacked();
|
||||
let slots = if stacked { 1 } else { c.series.len().max(1) };
|
||||
let ov = if stacked { 0.0 } else { c.overlap };
|
||||
let ns = slots as f32;
|
||||
let band = g.band();
|
||||
let thick = band / (ns - (ns - 1.0) * ov + c.gap);
|
||||
let group = thick * (ns - (ns - 1.0) * ov);
|
||||
let mut pos = vec![0.0f64; g.n];
|
||||
let mut neg = vec![0.0f64; g.n];
|
||||
for (j, s) in c.series.iter().enumerate().filter(|(_, s)| has_values(s)) {
|
||||
let slot = if stacked { 0 } else { j };
|
||||
let slot = (if g.horiz { slots.saturating_sub(1 + slot) } else { slot }) as f32;
|
||||
for i in 0..g.n {
|
||||
let Some(v) = s.values.get(i).copied().flatten() else { continue };
|
||||
let (v0, v1) = if !stacked {
|
||||
(0.0, v)
|
||||
} else if v >= 0.0 {
|
||||
let Some(p) = pos.get_mut(i) else { continue };
|
||||
let b = *p;
|
||||
*p += v;
|
||||
(b, *p)
|
||||
} else {
|
||||
let Some(q) = neg.get_mut(i) else { continue };
|
||||
let b = *q;
|
||||
*q += v;
|
||||
(b, *q)
|
||||
};
|
||||
let a = g.band_start(i) + (band - group) / 2.0 + slot * thick * (1.0 - ov);
|
||||
let (p0, p1) = (g.val(v0), g.val(v1));
|
||||
let (p0, p1) = (p0.min(p1), p0.max(p1));
|
||||
let segs = if g.horiz { rect_segs(p0, a, p1, a + thick) } else { rect_segs(a, p0, a + thick, p1) };
|
||||
fill(out, segs, c.point_colour(j, i));
|
||||
if let Some(text) = label_of(s, i) {
|
||||
let mid = a + thick / 2.0;
|
||||
let (x, y) = match (g.horiz, stacked) {
|
||||
(true, true) => ((p0 + p1) / 2.0, mid),
|
||||
(false, true) => (mid, (p0 + p1) / 2.0),
|
||||
// Beyond the bar's end: right of a positive bar, above a positive column.
|
||||
(true, false) => (if v >= 0.0 { p1 } else { p0 }, mid),
|
||||
(false, false) => (mid, if v >= 0.0 { p0 } else { p1 }),
|
||||
};
|
||||
labels.push(DataLabel { x, y, text, inside: stacked });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a series has any point to draw.
|
||||
pub(super) fn has_values(s: &Series) -> bool {
|
||||
s.values.iter().any(Option::is_some)
|
||||
}
|
||||
|
||||
/// Line series; stacked ones are drawn at the running total of the series up to them.
|
||||
fn lines(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataLabel>) {
|
||||
let mut total = vec![0.0f64; g.n];
|
||||
for (j, s) in c.series.iter().enumerate().filter(|(_, s)| has_values(s)) {
|
||||
let pts: Vec<Option<(f32, f32)>> = (0..g.n)
|
||||
.map(|i| {
|
||||
let v = s.values.get(i).copied().flatten()?;
|
||||
let y = match total.get_mut(i) {
|
||||
Some(t) if c.stacked() => {
|
||||
*t += v;
|
||||
*t
|
||||
}
|
||||
_ => v,
|
||||
};
|
||||
Some((g.cat_x(i), g.val(y)))
|
||||
})
|
||||
.collect();
|
||||
for (i, p) in pts.iter().enumerate() {
|
||||
if let (Some((x, y)), Some(text)) = (p, label_of(s, i)) {
|
||||
labels.push(DataLabel { x: *x, y: *y, text, inside: false });
|
||||
}
|
||||
}
|
||||
stroke(out, runs(pts.into_iter()), c.series_colour(j), s.line_w);
|
||||
}
|
||||
}
|
||||
|
||||
fn areas(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataLabel>) {
|
||||
let mut base = vec![0.0f64; g.n];
|
||||
for (j, s) in c.series.iter().enumerate().filter(|(_, s)| has_values(s)) {
|
||||
let vals = (0..g.n).map(|i| s.values.get(i).copied().flatten().unwrap_or(0.0));
|
||||
let top: Vec<f64> = base.iter().zip(vals).map(|(b, v)| b + v).collect();
|
||||
let mut segs = Vec::with_capacity(2 * g.n + 1);
|
||||
for (i, t) in top.iter().enumerate() {
|
||||
let (x, y) = (g.cat_x(i), g.val(*t));
|
||||
segs.push(if i == 0 { PathSeg::Move(x, y) } else { PathSeg::Line(x, y) });
|
||||
}
|
||||
for (i, b) in base.iter().enumerate().rev() {
|
||||
segs.push(PathSeg::Line(g.cat_x(i), g.val(*b)));
|
||||
}
|
||||
segs.push(PathSeg::Close);
|
||||
fill(out, segs, c.series_colour(j));
|
||||
for (i, (t, b)) in top.iter().zip(&base).enumerate() {
|
||||
if let Some(text) = label_of(s, i) {
|
||||
labels.push(DataLabel { x: g.cat_x(i), y: g.val((t + b) / 2.0), text, inside: true });
|
||||
}
|
||||
}
|
||||
if c.stacked() {
|
||||
base = top;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Scatter series as lines, or as small markers when the series has no line.
|
||||
fn draw_scatter(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataLabel>) {
|
||||
const MARKER: f32 = 6.0;
|
||||
for (j, s) in c.series.iter().enumerate() {
|
||||
let pts: Vec<Option<(f32, f32)>> =
|
||||
s.xs.iter()
|
||||
.zip(&s.values)
|
||||
.map(|(x, y)| match (x, y) {
|
||||
(Some(x), Some(y)) => Some((g.scat_x(*x), g.val(*y))),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
for (i, p) in pts.iter().enumerate() {
|
||||
if let (Some((x, y)), Some(text)) = (p, label_of(s, i)) {
|
||||
labels.push(DataLabel { x: *x, y: *y - MARKER / 2.0, text, inside: false });
|
||||
}
|
||||
}
|
||||
if s.line {
|
||||
stroke(out, runs(pts.iter().copied()), c.series_colour(j), s.line_w);
|
||||
continue;
|
||||
}
|
||||
for (x, y) in pts.iter().flatten() {
|
||||
let rect = [x - MARKER / 2.0, y - MARKER / 2.0, MARKER, MARKER];
|
||||
out.push(GraphicItem::Shape { rect, kind: ShapeKind::Ellipse, fill: Some(c.series_colour(j)), stroke: None, stroke_width: 0.0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
use super::canvas::{Rect, clip_text, fill, stroke, text, text_w, widest};
|
||||
use super::format::format_value;
|
||||
use super::model::{DataLabel, Kind, Series};
|
||||
use super::plot::{Axes, has_values};
|
||||
use super::{Chart, FRAC_PI_2, GAP, GRID, GraphicItem, LINE, PathSeg, Rgb, ShapeKind, TAU, TEXT, TextAlign};
|
||||
|
||||
pub(super) fn pie(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
let Some(s) = c.series.first() else { return };
|
||||
let vals: Vec<f64> = s.values.iter().map(|v| v.unwrap_or(0.0).max(0.0)).collect();
|
||||
let total: f64 = vals.iter().sum();
|
||||
let r = (area.w.min(area.h) / 2.0 - 2.0) as f64;
|
||||
if !(total > 0.0 && total.is_finite() && r >= 1.0) {
|
||||
return;
|
||||
}
|
||||
let (cx, cy) = ((area.x + area.w / 2.0) as f64, (area.y + area.h / 2.0) as f64);
|
||||
let inner = if c.kind == Kind::Doughnut { r * c.hole as f64 } else { 0.0 };
|
||||
let mut a = c.first_angle.to_radians();
|
||||
let mut labels = Vec::new();
|
||||
for (i, v) in vals.iter().enumerate() {
|
||||
if *v <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let b = a + v / total * TAU;
|
||||
let mut segs = Vec::new();
|
||||
if inner > 0.0 {
|
||||
segs.push(pt(polar(cx, cy, r, a)));
|
||||
arc(&mut segs, (cx, cy, r), a, b);
|
||||
segs.push(line_to(polar(cx, cy, inner, b)));
|
||||
arc(&mut segs, (cx, cy, inner), b, a);
|
||||
} else {
|
||||
segs.push(pt((cx, cy)));
|
||||
segs.push(line_to(polar(cx, cy, r, a)));
|
||||
arc(&mut segs, (cx, cy, r), a, b);
|
||||
}
|
||||
segs.push(PathSeg::Close);
|
||||
fill(out, segs, c.point_colour(0, i));
|
||||
if let Some(text) = label_of(s, i) {
|
||||
// Halfway across the ring (pie: two thirds out from the centre).
|
||||
let at = if inner > 0.0 { (r + inner) / 2.0 } else { r * 0.65 };
|
||||
let (x, y) = polar(cx, cy, at, (a + b) / 2.0);
|
||||
labels.push(DataLabel { x: x as f32, y: y as f32, text, inside: true });
|
||||
}
|
||||
a = b;
|
||||
}
|
||||
draw_data_labels(out, &labels, c.text_size, false);
|
||||
}
|
||||
|
||||
/// A radar chart in `area`: category spokes from the centre, concentric grid polygons at the value
|
||||
/// ticks, one polygon per series (filled, or outlined with markers) and the category labels at the
|
||||
/// spoke ends. The lowest tick is the centre.
|
||||
pub(super) fn radar(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
const MARKER: f32 = 5.0;
|
||||
if c.n == 0 {
|
||||
return;
|
||||
}
|
||||
let fs = c.text_size;
|
||||
let lh = fs * LINE;
|
||||
let labelled = c.cats.iter().any(|s| !s.is_empty());
|
||||
let side = if labelled { widest(&c.cats, fs).min(area.w * 0.25) + GAP + 2.0 } else { 0.0 };
|
||||
let vert = if labelled { lh + GAP } else { 0.0 };
|
||||
let r = ((area.w / 2.0 - side).min(area.h / 2.0 - vert) - 2.0) as f64;
|
||||
if r.is_nan() || r < 8.0 {
|
||||
return;
|
||||
}
|
||||
let (cx, cy) = ((area.x + area.w / 2.0) as f64, (area.y + area.h / 2.0) as f64);
|
||||
let vs = Axes::of(c).vs;
|
||||
let span = vs.hi - vs.lo;
|
||||
let angle = |i: usize| i as f64 / c.n as f64 * TAU;
|
||||
let at = |v: f64, i: usize| {
|
||||
let t = if span > 0.0 { ((v - vs.lo) / span).clamp(0.0, 1.0) } else { 0.0 };
|
||||
polar(cx, cy, r * t, angle(i))
|
||||
};
|
||||
let ticks = vs.ticks();
|
||||
if c.val.grid {
|
||||
for t in ticks.iter().skip(1) {
|
||||
let mut segs: Vec<PathSeg> = (0..c.n).map(|i| if i == 0 { pt(at(*t, i)) } else { line_to(at(*t, i)) }).collect();
|
||||
segs.push(PathSeg::Close);
|
||||
stroke(out, segs, GRID, 0.75);
|
||||
}
|
||||
}
|
||||
for i in 0..c.n {
|
||||
let end = polar(cx, cy, r, angle(i));
|
||||
stroke(out, vec![pt((cx, cy)), line_to(end)], GRID, 0.75);
|
||||
}
|
||||
let mut labels = Vec::new();
|
||||
for (j, s) in c.series.iter().enumerate().filter(|(_, s)| has_values(s)) {
|
||||
let col = c.series_colour(j);
|
||||
// A missing value counts as the centre, as Word draws it.
|
||||
let ring: Vec<(f64, f64)> = (0..c.n).map(|i| at(s.values.get(i).copied().flatten().unwrap_or(vs.lo), i)).collect();
|
||||
let mut segs: Vec<PathSeg> = ring.iter().enumerate().map(|(i, p)| if i == 0 { pt(*p) } else { line_to(*p) }).collect();
|
||||
segs.push(PathSeg::Close);
|
||||
if c.radar_fill {
|
||||
fill(out, segs.clone(), col);
|
||||
}
|
||||
stroke(out, segs, col, s.line_w);
|
||||
for (i, p) in ring.iter().enumerate() {
|
||||
if c.radar_marks && s.values.get(i).copied().flatten().is_some() {
|
||||
let rect = [p.0 as f32 - MARKER / 2.0, p.1 as f32 - MARKER / 2.0, MARKER, MARKER];
|
||||
out.push(GraphicItem::Shape { rect, kind: ShapeKind::Ellipse, fill: Some(col), stroke: None, stroke_width: 0.0 });
|
||||
}
|
||||
if let Some(text) = label_of(s, i) {
|
||||
labels.push(DataLabel { x: p.0 as f32, y: p.1 as f32, text, inside: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
draw_data_labels(out, &labels, fs, false);
|
||||
if !c.val.deleted {
|
||||
for t in &ticks {
|
||||
let y = at(*t, 0).1 as f32;
|
||||
let s = format_value(*t, &c.val_fmt, vs.step);
|
||||
let w = text_w(&s, fs) + 4.0;
|
||||
text(out, Rect { x: cx as f32 - GAP / 2.0 - w, y: y - lh / 2.0, w, h: lh }, &s, fs, TextAlign::Right, false);
|
||||
}
|
||||
}
|
||||
if labelled {
|
||||
for (i, s) in c.cats.iter().enumerate().filter(|(_, s)| !s.is_empty()) {
|
||||
let a = angle(i);
|
||||
let (x, y) = polar(cx, cy, r + GAP as f64, a);
|
||||
let (x, y) = (x as f32, y as f32);
|
||||
let w = side - GAP;
|
||||
let txt = clip_text(s, w, fs);
|
||||
let (sx, sy) = (a.sin(), a.cos());
|
||||
let (rect, align) = if sx > 0.2 {
|
||||
(Rect { x, y: y - lh / 2.0, w, h: lh }, TextAlign::Left)
|
||||
} else if sx < -0.2 {
|
||||
(Rect { x: x - w, y: y - lh / 2.0, w, h: lh }, TextAlign::Right)
|
||||
} else {
|
||||
let cw = (text_w(&txt, fs) + 4.0).max(1.0);
|
||||
(Rect { x: x - cw / 2.0, y: if sy > 0.0 { y - lh } else { y }, w: cw, h: lh }, TextAlign::Center)
|
||||
};
|
||||
text(out, rect, &txt, fs, align, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The data label of point `i` of series `s`, when it shows one: the value as the file has it,
|
||||
/// in the series' number format.
|
||||
pub(super) fn label_of(s: &Series, i: usize) -> Option<String> {
|
||||
if s.labels != Some(true) {
|
||||
return None;
|
||||
}
|
||||
let v = s.raw.get(i).copied().flatten()?;
|
||||
let fmt = s.fmt.as_deref().filter(|f| !f.eq_ignore_ascii_case("General"));
|
||||
Some(match fmt {
|
||||
Some(f) => format_value(v, f, 1.0),
|
||||
None => general_value(v),
|
||||
})
|
||||
}
|
||||
|
||||
/// A value in the `General` format: whole numbers without decimals, others with up to six.
|
||||
fn general_value(v: f64) -> String {
|
||||
if v.fract() == 0.0 && v.abs() < 1e15 {
|
||||
return format!("{v:.0}");
|
||||
}
|
||||
let s = format!("{v:.6}");
|
||||
let s = s.trim_end_matches('0').trim_end_matches('.');
|
||||
if s == "-0" { "0".to_string() } else { s.to_string() }
|
||||
}
|
||||
|
||||
/// Data labels over the drawn chart. Beside labels sit above their point, or right of it for
|
||||
/// horizontal bars (`beside_right`).
|
||||
pub(super) fn draw_data_labels(out: &mut Vec<GraphicItem>, labels: &[DataLabel], fs: f32, beside_right: bool) {
|
||||
let lh = fs * LINE;
|
||||
for l in labels {
|
||||
let w = text_w(&l.text, fs) + 4.0;
|
||||
let (rect, align) = if l.inside {
|
||||
(Rect { x: l.x - w / 2.0, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Center)
|
||||
} else if beside_right {
|
||||
(Rect { x: l.x + GAP / 2.0, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Left)
|
||||
} else {
|
||||
(Rect { x: l.x - w / 2.0, y: l.y - lh, w, h: lh }, TextAlign::Center)
|
||||
};
|
||||
let color = if l.inside { Rgb::WHITE } else { TEXT };
|
||||
out.push(GraphicItem::Text { rect: rect.arr(), text: l.text.clone(), size: fs, color, bold: false, align, font: None });
|
||||
}
|
||||
}
|
||||
|
||||
/// A point at clockwise angle `t` (radians from 12 o'clock) on a circle of radius `r`.
|
||||
fn polar(cx: f64, cy: f64, r: f64, t: f64) -> (f64, f64) {
|
||||
(cx + r * t.sin(), cy - r * t.cos())
|
||||
}
|
||||
|
||||
fn pt(p: (f64, f64)) -> PathSeg {
|
||||
PathSeg::Move(p.0 as f32, p.1 as f32)
|
||||
}
|
||||
|
||||
fn line_to(p: (f64, f64)) -> PathSeg {
|
||||
PathSeg::Line(p.0 as f32, p.1 as f32)
|
||||
}
|
||||
|
||||
/// Cubic segments along a circle from angle `a` to `b` (the current point is at `a`). Each piece
|
||||
/// spans at most a quarter turn.
|
||||
fn arc(segs: &mut Vec<PathSeg>, (cx, cy, r): (f64, f64, f64), a: f64, b: f64) {
|
||||
let n = (((b - a).abs() / FRAC_PI_2).ceil() as usize).clamp(1, 16);
|
||||
let d = (b - a) / n as f64;
|
||||
let k = 4.0 / 3.0 * (d / 4.0).tan();
|
||||
for j in 0..n {
|
||||
let t0 = a + d * j as f64;
|
||||
let t1 = t0 + d;
|
||||
let p0 = polar(cx, cy, r, t0);
|
||||
let p1 = polar(cx, cy, r, t1);
|
||||
let c1 = (p0.0 + k * r * t0.cos(), p0.1 + k * r * t0.sin());
|
||||
let c2 = (p1.0 - k * r * t1.cos(), p1.1 - k * r * t1.sin());
|
||||
segs.push(PathSeg::Cubic(c1.0 as f32, c1.1 as f32, c2.0 as f32, c2.1 as f32, p1.0 as f32, p1.1 as f32));
|
||||
}
|
||||
}
|
||||
@@ -1,530 +0,0 @@
|
||||
use super::*;
|
||||
use wordcraft_doc::THEME_COLORS;
|
||||
|
||||
const NS: &str =
|
||||
r#"xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main""#;
|
||||
|
||||
fn space(plot: &str) -> El {
|
||||
let x = format!(r#"<c:chartSpace {NS}><c:chart><c:plotArea>{plot}</c:plotArea></c:chart></c:chartSpace>"#);
|
||||
crate::xml::parse(x.as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
fn theme() -> Vec<Rgb> {
|
||||
THEME_COLORS.to_vec()
|
||||
}
|
||||
|
||||
fn ser(i: usize, name: &str, color: Option<&str>, cats: &[&str], vals: &[&str]) -> String {
|
||||
let fill = color.map_or(String::new(), |c| format!(r#"<c:spPr><a:solidFill><a:srgbClr val="{c}"/></a:solidFill></c:spPr>"#));
|
||||
let cat_pts: String = cats.iter().enumerate().map(|(k, c)| format!(r#"<c:pt idx="{k}"><c:v>{c}</c:v></c:pt>"#)).collect();
|
||||
let val_pts: String = vals.iter().enumerate().map(|(k, v)| format!(r#"<c:pt idx="{k}"><c:v>{v}</c:v></c:pt>"#)).collect();
|
||||
format!(
|
||||
r#"<c:ser><c:idx val="{i}"/><c:order val="{i}"/><c:tx><c:strRef><c:strCache><c:ptCount val="1"/><c:pt idx="0"><c:v>{name}</c:v></c:pt></c:strCache></c:strRef></c:tx>{fill}<c:cat><c:strRef><c:strCache><c:ptCount val="{nc}"/>{cat_pts}</c:strCache></c:strRef></c:cat><c:val><c:numRef><c:numCache><c:formatCode>General</c:formatCode><c:ptCount val="{nv}"/>{val_pts}</c:numCache></c:numRef></c:val></c:ser>"#,
|
||||
nc = cats.len(),
|
||||
nv = vals.len()
|
||||
)
|
||||
}
|
||||
|
||||
fn filled(items: &[GraphicItem]) -> Vec<(Rgb, f32, f32)> {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|it| match it {
|
||||
GraphicItem::Path { segs, fill: Some(c), .. } => {
|
||||
let ys: Vec<f32> = segs
|
||||
.iter()
|
||||
.filter_map(|s| match s {
|
||||
PathSeg::Move(_, y) | PathSeg::Line(_, y) => Some(*y),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let lo = ys.iter().copied().fold(f32::INFINITY, f32::min);
|
||||
let hi = ys.iter().copied().fold(f32::NEG_INFINITY, f32::max);
|
||||
Some((*c, lo, hi))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clustered_columns_draw_one_bar_per_point() {
|
||||
let plot = format!(
|
||||
r#"<c:barChart><c:barDir val="col"/><c:grouping val="clustered"/>{}{}<c:axId val="1"/><c:axId val="2"/></c:barChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/><c:majorGridlines/></c:valAx>"#,
|
||||
ser(0, "North", Some("FF0000"), &["Q1", "Q2", "Q3"], &["4", "8", "2"]),
|
||||
ser(1, "South", Some("00FF00"), &["Q1", "Q2", "Q3"], &["2", "6", "4"]),
|
||||
);
|
||||
let items = chart_items(&space(&plot), &theme(), 300.0, 200.0);
|
||||
let bars = filled(&items);
|
||||
assert_eq!(bars.len(), 6);
|
||||
let heights: Vec<f32> = bars.iter().map(|(_, lo, hi)| hi - lo).collect();
|
||||
// Scale 0..8: North Q2 is the full plot height, North Q1 half of it.
|
||||
assert!((heights[1] / heights[0] - 2.0).abs() < 1e-3, "{heights:?}");
|
||||
// South (second series) Q1 is 2 of 8, Q2 is 6 of 8.
|
||||
assert!((heights[3] / heights[1] - 0.25).abs() < 1e-3, "{heights:?}");
|
||||
assert!((heights[4] / heights[1] - 0.75).abs() < 1e-3, "{heights:?}");
|
||||
assert_eq!(bars[0].0, Rgb(0xFF, 0, 0));
|
||||
assert_eq!(bars[3].0, Rgb(0, 0xFF, 0));
|
||||
assert!(items.iter().any(|it| matches!(it, GraphicItem::Text { text, .. } if text == "Q2")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn data_labels_show_the_values() {
|
||||
let texts = |items: &[GraphicItem]| -> Vec<String> {
|
||||
items.iter().filter_map(|it| if let GraphicItem::Text { text, .. } = it { Some(text.clone()) } else { None }).collect()
|
||||
};
|
||||
let labels = r#"<c:dLbls><c:showLegendKey val="0"/><c:showVal val="1"/></c:dLbls>"#;
|
||||
let plot = format!(
|
||||
r#"<c:barChart><c:barDir val="col"/><c:grouping val="clustered"/>{}{labels}<c:axId val="1"/><c:axId val="2"/></c:barChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#,
|
||||
ser(0, "North", None, &["Q1", "Q2"], &["4.25", "17"]),
|
||||
);
|
||||
let shown = texts(&chart_items(&space(&plot), &theme(), 300.0, 200.0));
|
||||
assert!(shown.contains(&"4.25".to_string()) && shown.contains(&"17".to_string()), "{shown:?}");
|
||||
let pie = format!(r#"<c:pieChart><c:varyColors val="1"/>{}{labels}</c:pieChart>"#, ser(0, "S", None, &["A", "B"], &["1", "3"]));
|
||||
let shown = texts(&chart_items(&space(&pie), &theme(), 200.0, 150.0));
|
||||
assert!(shown.contains(&"3".to_string()), "{shown:?}");
|
||||
// Without `c:dLbls`, no values are written next to the points.
|
||||
let plain = plot.replace(labels, "");
|
||||
assert!(!texts(&chart_items(&space(&plain), &theme(), 300.0, 200.0)).contains(&"4.25".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pie_draws_one_slice_per_point() {
|
||||
let plot =
|
||||
format!(r#"<c:pieChart><c:varyColors val="1"/>{}</c:pieChart>"#, ser(0, "Share", None, &["A", "B", "C", "D"], &["50", "25", "15", "10"]));
|
||||
let items = chart_items(&space(&plot), &theme(), 200.0, 150.0);
|
||||
let slices = filled(&items);
|
||||
assert_eq!(slices.len(), 4);
|
||||
assert_eq!(slices[0].0, theme()[4], "first slice is accent1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doughnut_slices_are_annuli() {
|
||||
let plot =
|
||||
format!(r#"<c:doughnutChart><c:varyColors val="1"/>{}<c:holeSize val="50"/></c:doughnutChart>"#, ser(0, "S", None, &["A", "B"], &["1", "3"]));
|
||||
let items = chart_items(&space(&plot), &theme(), 200.0, 150.0);
|
||||
assert_eq!(items.iter().filter(|it| matches!(it, GraphicItem::Path { fill: Some(_), .. })).count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nice_scale_rounds_to_round_steps() {
|
||||
assert_eq!(nice_scale(0.0, 22.3, true, None, None), Scale { lo: 0.0, hi: 25.0, step: 5.0 });
|
||||
assert_eq!(nice_scale(0.0, 100.0, true, None, None), Scale { lo: 0.0, hi: 120.0, step: 20.0 });
|
||||
assert_eq!(nice_scale(0.0, 22.3, true, Some(0.0), Some(30.0)).hi, 30.0);
|
||||
let flat = nice_scale(0.0, 0.0, true, None, None);
|
||||
assert!(flat.lo == 0.0 && (flat.hi - 1.2).abs() < 1e-9 && flat.step == 0.2, "{flat:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_formats() {
|
||||
assert_eq!(format_value(1234567.0, "#,##0", 1.0), "1,234,567");
|
||||
assert_eq!(format_value(-1500.0, "#,##0", 500.0), "-1,500");
|
||||
assert_eq!(format_value(0.25, "0%", 0.05), "25%");
|
||||
assert_eq!(format_value(0.5, "General", 0.25), "0.50");
|
||||
assert_eq!(format_value(10.0, "\"€\"0.00", 5.0), "€10.00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_values_never_panic() {
|
||||
let bodies = [
|
||||
format!(
|
||||
r#"<c:barChart><c:barDir val="col"/><c:grouping val="stacked"/><c:gapWidth val="-9999999999999999999999"/><c:overlap val="NaN"/>{}<c:axId val="1"/></c:barChart><c:valAx><c:axId val="1"/><c:scaling><c:max val="1e400"/><c:min val="-1e308"/></c:scaling></c:valAx>"#,
|
||||
r#"<c:ser><c:tx><c:v>x</c:v></c:tx><c:cat><c:strRef><c:strCache><c:ptCount val="99999999999"/><c:pt idx="18446744073709551615"><c:v>a</c:v></c:pt><c:pt idx="-3"><c:v>b</c:v></c:pt></c:strCache></c:strRef></c:cat><c:val><c:numRef><c:numCache><c:ptCount val="3"/><c:pt idx="0"><c:v>1e400</c:v></c:pt><c:pt idx="1"><c:v>NaN</c:v></c:pt><c:pt idx="2"><c:v>-1e308</c:v></c:pt><c:pt idx="99999"><c:v>5</c:v></c:pt></c:numCache></c:numRef></c:val></c:ser><c:ser><c:val><c:numRef><c:numCache><c:pt idx="0"><c:v>1e308</c:v></c:pt><c:pt idx="0"><c:v>1e308</c:v></c:pt></c:numCache></c:numRef></c:val></c:ser>"#
|
||||
),
|
||||
r#"<c:scatterChart><c:ser><c:xVal><c:numLit><c:ptCount val="2"/><c:pt idx="0"><c:v>-1e308</c:v></c:pt><c:pt idx="1"><c:v>1e308</c:v></c:pt></c:numLit></c:xVal><c:yVal><c:numLit><c:ptCount val="2"/><c:pt idx="0"><c:v>1</c:v></c:pt><c:pt idx="1"><c:v>2</c:v></c:pt></c:numLit></c:yVal></c:ser></c:scatterChart>"#.to_string(),
|
||||
r#"<c:pieChart><c:ser><c:val><c:numLit><c:ptCount val="2"/><c:pt idx="0"><c:v>-4</c:v></c:pt><c:pt idx="1"><c:v>1e308</c:v></c:pt></c:numLit></c:val><c:dPt><c:idx val="9999999999999"/></c:dPt></c:ser><c:firstSliceAng val="-9"/></c:pieChart>"#.to_string(),
|
||||
];
|
||||
for body in &bodies {
|
||||
let s = space(body);
|
||||
for (w, h) in [(0.0, 10.0), (f32::NAN, 10.0), (-1.0, 1.0), (1e30, 1e30), (50.0, 40.0)] {
|
||||
let _ = chart_items(&s, &theme(), w, h);
|
||||
}
|
||||
}
|
||||
assert!(chart_items(&space(&bodies[0]), &theme(), f32::NAN, 10.0).is_empty());
|
||||
assert!(chart_items(&El::default(), &theme(), 100.0, 100.0).is_empty());
|
||||
}
|
||||
|
||||
fn texts(items: &[GraphicItem]) -> Vec<&str> {
|
||||
items.iter().filter_map(|it| if let GraphicItem::Text { text, .. } = it { Some(text.as_str()) } else { None }).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_chart_types_draw_a_placeholder() {
|
||||
let ser = r#"<c:ser><c:val><c:numLit><c:ptCount val="1"/><c:pt idx="0"><c:v>1</c:v></c:pt></c:numLit></c:val></c:ser>"#;
|
||||
let title = r#"<c:title><c:tx><c:rich><a:p><a:r><a:t>Skills</a:t></a:r></a:p></c:rich></c:tx></c:title>"#;
|
||||
let x = format!(r#"<c:chartSpace {NS}><c:chart>{title}<c:plotArea><c:bubbleChart>{ser}</c:bubbleChart></c:plotArea></c:chart></c:chartSpace>"#);
|
||||
let bubble = chart_items(&crate::xml::parse(x.as_bytes()).unwrap(), &theme(), 300.0, 200.0);
|
||||
assert_eq!(texts(&bubble), ["Skills", "Bubble chart (not supported yet)"]);
|
||||
let framed = bubble.iter().any(|it| matches!(it, GraphicItem::Shape { rect, stroke: Some(_), .. } if *rect == [0.0, 0.0, 300.0, 200.0]));
|
||||
assert!(framed, "a light border marks the chart's frame: {bubble:?}");
|
||||
|
||||
let stock = chart_items(&space(&format!("<c:stockChart>{ser}{ser}{ser}</c:stockChart>")), &theme(), 300.0, 200.0);
|
||||
assert_eq!(texts(&stock), ["Stock chart (not supported yet)"]);
|
||||
let other = chart_items(&space(&format!("<c:fancyChart>{ser}</c:fancyChart>")), &theme(), 300.0, 200.0);
|
||||
assert_eq!(texts(&other), ["Chart (not supported yet)"]);
|
||||
// A supported type in the plot area is drawn, an unsupported one beside it is skipped.
|
||||
let combo = chart_items(&space(&format!("<c:stockChart>{ser}</c:stockChart><c:lineChart>{ser}</c:lineChart>")), &theme(), 300.0, 200.0);
|
||||
assert!(!texts(&combo).iter().any(|t| t.contains("not supported")), "{combo:?}");
|
||||
}
|
||||
|
||||
fn radar_chart(style: &str, sers: &[String]) -> Vec<GraphicItem> {
|
||||
let plot = format!(r#"<c:radarChart><c:radarStyle val="{style}"/>{}</c:radarChart><c:valAx><c:majorGridlines/></c:valAx>"#, sers.concat());
|
||||
chart_items(&space(&plot), &theme(), 300.0, 220.0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn radar_charts_draw_spokes_grid_and_series_polygons() {
|
||||
let cats = ["Speed", "Power", "Range", "Armor", "Luck"];
|
||||
let sers = [ser(0, "A", Some("FF0000"), &cats, &["3", "4", "5", "2", "1"]), ser(1, "B", Some("0000FF"), &cats, &["2", "2", "3", "5", "4"])];
|
||||
let std = radar_chart("standard", &sers);
|
||||
assert!(!texts(&std).iter().any(|t| t.contains("not supported")), "{std:?}");
|
||||
for c in cats {
|
||||
assert!(texts(&std).contains(&c), "category label {c}: {:?}", texts(&std));
|
||||
}
|
||||
let stroked = |items: &[GraphicItem], col: Rgb| {
|
||||
items.iter().filter(|it| matches!(it, GraphicItem::Path { stroke: Some(c), segs, .. } if *c == col && segs.len() == 6)).count()
|
||||
};
|
||||
assert_eq!(stroked(&std, Rgb(0xFF, 0, 0)), 1, "one closed pentagon per series: {std:?}");
|
||||
assert_eq!(stroked(&std, Rgb(0, 0, 0xFF)), 1);
|
||||
assert!(filled(&std).is_empty(), "standard radar is not filled");
|
||||
assert!(!std.iter().any(|it| matches!(it, GraphicItem::Shape { kind: ShapeKind::Ellipse, .. })));
|
||||
// 5 spokes (2 segs each) plus grid pentagons.
|
||||
let spokes = std.iter().filter(|it| matches!(it, GraphicItem::Path { stroke: Some(c), segs, .. } if *c == GRID && segs.len() == 2)).count();
|
||||
assert_eq!(spokes, 5);
|
||||
assert!(std.iter().any(|it| matches!(it, GraphicItem::Path { stroke: Some(c), segs, .. } if *c == GRID && segs.len() == 6)));
|
||||
|
||||
let marked = radar_chart("marker", &sers);
|
||||
let dots = marked.iter().filter(|it| matches!(it, GraphicItem::Shape { kind: ShapeKind::Ellipse, .. })).count();
|
||||
assert_eq!(dots, 10, "a marker per point");
|
||||
|
||||
let solid = radar_chart("filled", &sers);
|
||||
let f = filled(&solid);
|
||||
assert_eq!(f.len(), 2);
|
||||
assert!(f.iter().any(|(c, ..)| *c == Rgb(0xFF, 0, 0)) && f.iter().any(|(c, ..)| *c == Rgb(0, 0, 0xFF)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn radar_charts_show_a_legend() {
|
||||
let cats = ["a", "b", "c"];
|
||||
let plot = format!("<c:radarChart>{}</c:radarChart>", ser(0, "Team", None, &cats, &["1", "2", "3"]));
|
||||
let items = with_legend(&plot, "r", 300.0, 200.0);
|
||||
assert!(texts(&items).contains(&"Team"), "{:?}", texts(&items));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_radar_charts_never_panic() {
|
||||
// No categories and no values.
|
||||
let empty = r#"<c:ser><c:cat><c:strLit><c:ptCount val="0"/></c:strLit></c:cat><c:val><c:numLit><c:ptCount val="0"/></c:numLit></c:val></c:ser>"#;
|
||||
let _ = chart_items(&space(&format!("<c:radarChart>{empty}</c:radarChart>")), &theme(), 300.0, 200.0);
|
||||
let none = chart_items(&space("<c:radarChart/>"), &theme(), 300.0, 200.0);
|
||||
assert!(none.is_empty(), "{none:?}");
|
||||
// One category: a degenerate polygon, nothing to crash on; two as well.
|
||||
for n in 1..=2 {
|
||||
let cats: Vec<String> = (0..n).map(|i| format!("c{i}")).collect();
|
||||
let refs: Vec<&str> = cats.iter().map(String::as_str).collect();
|
||||
let vals = vec!["5"; n];
|
||||
let items = radar_chart("filled", &[ser(0, "S", None, &refs, &vals)]);
|
||||
assert!(texts(&items).contains(&"c0"), "{items:?}");
|
||||
}
|
||||
// NaN, infinities, junk and a tiny frame.
|
||||
let junk = ser(0, "S", None, &["a", "b", "c"], &["NaN", "inf", "1e999"]);
|
||||
let _ = radar_chart("filled", &[junk]);
|
||||
let small =
|
||||
chart_items(&space(&format!("<c:radarChart>{}</c:radarChart>", ser(0, "S", None, &["a", "b", "c"], &["1", "2", "3"]))), &theme(), 20.0, 20.0);
|
||||
let _ = small;
|
||||
// Caps: thousands of categories and series stay bounded.
|
||||
let cats: Vec<String> = (0..5000).map(|i| format!("c{i}")).collect();
|
||||
let refs: Vec<&str> = cats.iter().map(String::as_str).collect();
|
||||
let vals = vec!["1"; 5000];
|
||||
let many: Vec<String> = (0..300).map(|i| ser(i, "S", None, &refs, &vals)).collect();
|
||||
let items = radar_chart("marker", &many);
|
||||
let polygons = items.iter().filter(|it| matches!(it, GraphicItem::Path { stroke: Some(c), .. } if *c != GRID)).count();
|
||||
assert!(polygons <= MAX_RADAR_SERIES, "{polygons}");
|
||||
let spokes = items.iter().filter(|it| matches!(it, GraphicItem::Path { stroke: Some(c), segs, .. } if *c == GRID && segs.len() == 2)).count();
|
||||
assert_eq!(spokes, MAX_SPOKES);
|
||||
}
|
||||
|
||||
/// Chart items for a chart with a legend at `pos` (`r`, `b`…).
|
||||
fn with_legend(plot: &str, pos: &str, w: f32, h: f32) -> Vec<GraphicItem> {
|
||||
let x = format!(
|
||||
r#"<c:chartSpace {NS}><c:chart><c:plotArea>{plot}</c:plotArea><c:legend><c:legendPos val="{pos}"/></c:legend></c:chart></c:chartSpace>"#
|
||||
);
|
||||
chart_items(&crate::xml::parse(x.as_bytes()).unwrap(), &theme(), w, h)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn series_are_capped_to_the_cell_budget() {
|
||||
// 256 series each declaring 10,000 points: only the 10 that fit the cell budget are kept.
|
||||
let sers: String = (0..256)
|
||||
.map(|i| format!(r#"<c:ser><c:idx val="{i}"/><c:val><c:numRef><c:numCache><c:ptCount val="10000"/><c:pt idx="0"><c:v>1</c:v></c:pt></c:numCache></c:numRef></c:val></c:ser>"#))
|
||||
.collect();
|
||||
let plot = format!(r#"<c:lineChart><c:grouping val="standard"/>{sers}<c:axId val="1"/></c:lineChart>"#);
|
||||
let items = chart_items(&space(&plot), &theme(), 300.0, 200.0);
|
||||
let strokes = items.iter().filter(|it| matches!(it, GraphicItem::Path { fill: None, stroke: Some(_), .. })).count();
|
||||
assert_eq!(strokes, MAX_CELLS / 10_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn series_without_values_draw_nothing() {
|
||||
let plot = format!(
|
||||
r#"<c:areaChart><c:grouping val="standard"/>{}{}<c:axId val="1"/></c:areaChart>"#,
|
||||
ser(0, "A", Some("FF0000"), &["Q1", "Q2"], &["4", "8"]),
|
||||
ser(1, "Empty", Some("00FF00"), &["Q1", "Q2"], &[]),
|
||||
);
|
||||
let fills = filled(&chart_items(&space(&plot), &theme(), 300.0, 200.0));
|
||||
assert_eq!(fills.len(), 1);
|
||||
assert_eq!(fills[0].0, Rgb(0xFF, 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_scale_starts_bars_at_zero_for_negative_data() {
|
||||
// All-negative data pads below only: the axis ends at zero, as positive data's starts there.
|
||||
assert_eq!(nice_scale(-10.0, -2.0, true, None, None), Scale { lo: -12.0, hi: 0.0, step: 2.0 });
|
||||
assert_eq!(nice_scale(-1050.0, -1000.0, false, None, None).hi, -1000.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn narrow_line_scales_leave_zero_out_but_bars_keep_it() {
|
||||
assert!(narrow(1000.0, 1050.0) && narrow(-1050.0, -1000.0));
|
||||
assert!(!narrow(10.0, 100.0) && !narrow(0.0, 1.0));
|
||||
assert_eq!(nice_scale(1000.0, 1050.0, false, None, None).lo, 1000.0);
|
||||
assert_eq!(nice_scale(1000.0, 1050.0, true, None, None).lo, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn horizontal_bar_labels_wrap_in_a_column_of_forty_percent() {
|
||||
let long = "A category label that is much too long for its column";
|
||||
let plot = format!(
|
||||
r#"<c:barChart><c:barDir val="bar"/><c:grouping val="clustered"/>{}<c:axId val="1"/><c:axId val="2"/></c:barChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#,
|
||||
ser(0, "S", Some("FF0000"), &[long, "Short"], &["4", "8"]),
|
||||
);
|
||||
let items = chart_items(&space(&plot), &theme(), 300.0, 200.0);
|
||||
let labels: Vec<(&str, f32)> = items
|
||||
.iter()
|
||||
.filter_map(|it| match it {
|
||||
GraphicItem::Text { rect, text, align: TextAlign::Right, .. } => Some((text.as_str(), rect[2])),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert!(labels.iter().all(|(_, w)| *w <= 0.4 * 300.0), "{labels:?}");
|
||||
assert!(labels.iter().filter(|(t, _)| long.contains(t)).count() > 1, "wrapped: {labels:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stacked_lines_add_up_like_areas() {
|
||||
let stacked = format!(
|
||||
r#"<c:lineChart><c:grouping val="stacked"/>{}{}<c:axId val="1"/></c:lineChart>"#,
|
||||
ser(0, "A", Some("FF0000"), &["Q1", "Q2"], &["0", "10"]),
|
||||
ser(1, "B", Some("00FF00"), &["Q1", "Q2"], &["0", "5"]),
|
||||
);
|
||||
let single = format!(
|
||||
r#"<c:lineChart><c:grouping val="standard"/>{}<c:axId val="1"/></c:lineChart>"#,
|
||||
ser(0, "T", Some("0000FF"), &["Q1", "Q2"], &["0", "15"])
|
||||
);
|
||||
// Where the last point of the line drawn in `colour` is.
|
||||
let end_y = |items: &[GraphicItem], colour: Rgb| {
|
||||
items.iter().find_map(|it| match it {
|
||||
GraphicItem::Path { segs, stroke: Some(c), .. } if *c == colour => match segs.last() {
|
||||
Some(PathSeg::Line(_, y)) => Some(*y),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
};
|
||||
let top = end_y(&chart_items(&space(&stacked), &theme(), 300.0, 200.0), Rgb(0, 0xFF, 0)).expect("stacked line");
|
||||
let total = end_y(&chart_items(&space(&single), &theme(), 300.0, 200.0), Rgb(0, 0, 0xFF)).expect("single line");
|
||||
assert!((top - total).abs() < 1e-3, "stacked {top} vs {total}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn category_numbers_show_in_their_format() {
|
||||
// Excel serial days: 45292 is 1 January 2024, 45323 is 1 February 2024.
|
||||
let cat = |code: &str| -> Vec<String> {
|
||||
let x = format!(
|
||||
r#"<c:cat {NS}><c:numRef><c:numCache><c:formatCode>{code}</c:formatCode><c:ptCount val="2"/><c:pt idx="0"><c:v>45292</c:v></c:pt><c:pt idx="1"><c:v>45323</c:v></c:pt></c:numCache></c:numRef></c:cat>"#
|
||||
);
|
||||
labels(Some(&crate::xml::parse(x.as_bytes()).unwrap()), 10, false)
|
||||
};
|
||||
assert_eq!(cat("m/d/yyyy"), ["1/1/2024", "2/1/2024"]);
|
||||
assert_eq!(cat("d/m/yyyy"), ["1/1/2024", "1/2/2024"]);
|
||||
assert_eq!(cat("mmm-yy"), ["Jan-24", "Feb-24"]);
|
||||
assert_eq!(cat("yyyy"), ["2024", "2024"]);
|
||||
assert_eq!(cat("0.00"), ["45292.00", "45323.00"]);
|
||||
let ymd = |v: f64| excel_stamp(v, false).map(|t| (t.y, t.m, t.d));
|
||||
assert_eq!(ymd(61.0), Some((1900, 3, 1)));
|
||||
assert_eq!(ymd(45292.0), Some((2024, 1, 1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn category_format_code_length_is_capped() {
|
||||
let cat = |code: &str| -> Vec<String> {
|
||||
let x = format!(
|
||||
r#"<c:cat {NS}><c:numRef><c:numCache><c:formatCode>{code}</c:formatCode><c:ptCount val="1"/><c:pt idx="0"><c:v>5</c:v></c:pt></c:numCache></c:numRef></c:cat>"#
|
||||
);
|
||||
labels(Some(&crate::xml::parse(x.as_bytes()).unwrap()), 10, false)
|
||||
};
|
||||
let ok = format!("{}0", "x".repeat(254));
|
||||
assert!(cat(&ok)[0].starts_with("xxx"));
|
||||
let long = format!("{}0", "x".repeat(300));
|
||||
assert_eq!(cat(&long), ["5"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_format_code_length_is_capped() {
|
||||
let val = |code: &str| -> Option<String> {
|
||||
let x = format!(
|
||||
r#"<c:val {NS}><c:numRef><c:numCache><c:formatCode>{code}</c:formatCode><c:ptCount val="1"/><c:pt idx="0"><c:v>5</c:v></c:pt></c:numCache></c:numRef></c:val>"#
|
||||
);
|
||||
numbers(Some(&crate::xml::parse(x.as_bytes()).unwrap()), 10).1
|
||||
};
|
||||
let ok = format!("{}0", "x".repeat(254));
|
||||
assert_eq!(val(&ok), Some(ok.clone()));
|
||||
assert_eq!(val(&format!("{}0", "x".repeat(300))), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn axis_format_code_length_is_capped() {
|
||||
let fmt = |code: &str| -> Option<String> {
|
||||
let x = format!(r#"<c:valAx {NS}><c:numFmt formatCode="{code}" sourceLinked="0"/></c:valAx>"#);
|
||||
axis(Some(&crate::xml::parse(x.as_bytes()).unwrap())).fmt
|
||||
};
|
||||
let ok = format!("{}0", "x".repeat(254));
|
||||
assert_eq!(fmt(&ok), Some(ok.clone()));
|
||||
assert_eq!(fmt(&format!("{}0", "x".repeat(300))), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn civil_is_exact_beyond_chronos_range() {
|
||||
// Serial 1 is 1899-12-31, serial 60 Excel's phantom 1900-02-29 handled by `excel_stamp`.
|
||||
assert_eq!(civil(1), (1899, 12, 31));
|
||||
// Past `NaiveDate`'s ±262143 the 400-year cycles carry the year, exactly, both sides of
|
||||
// the epoch; values from the pre-chrono proleptic Gregorian formula.
|
||||
assert_eq!(civil(100_000_000), (275_690, 9, 11));
|
||||
assert_eq!(civil(-100_000_000), (-271_891, 4, 18));
|
||||
// The extremes must not overflow or panic (i128 intermediates).
|
||||
let _ = (civil(i64::MAX), civil(i64::MIN));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_formats_keep_literal_text_and_currency() {
|
||||
assert_eq!(format_value(1234.5, "#,##0.00 [$€-407]", 0.25), "1,234.50 €");
|
||||
assert_eq!(format_value(5.0, "[Red]0", 1.0), "5");
|
||||
assert_eq!(format_value(3.0, "\"Items: \"0", 1.0), "Items: 3");
|
||||
assert_eq!(format_value(3.0, "0\" units\"", 1.0), "3 units");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn side_legend_keeps_only_the_entries_that_fit() {
|
||||
let sers: String = (0..10).map(|i| ser(i, &format!("S{i}"), Some("FF0000"), &["Q1"], &["1"])).collect();
|
||||
let plot = format!(
|
||||
r#"<c:barChart><c:barDir val="col"/><c:grouping val="clustered"/>{sers}<c:axId val="1"/><c:axId val="2"/></c:barChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#
|
||||
);
|
||||
let items = with_legend(&plot, "r", 300.0, 60.0);
|
||||
let swatches = items.iter().filter(|it| matches!(it, GraphicItem::Shape { rect, .. } if rect[2] == 7.0)).count();
|
||||
assert!(swatches > 0 && swatches < 10, "{swatches} swatches");
|
||||
let bottom =
|
||||
items.iter().filter_map(|it| if let GraphicItem::Text { rect, .. } = it { Some(rect[1] + rect[3]) } else { None }).fold(0.0, f32::max);
|
||||
assert!(bottom <= 60.0, "legend text passes the chart's bottom: {bottom}");
|
||||
}
|
||||
|
||||
/// The swatches of a legend: their (x, y).
|
||||
fn swatches(items: &[GraphicItem]) -> Vec<(f32, f32)> {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|it| if let GraphicItem::Shape { rect, .. } = it { (rect[2] == SWATCH).then_some((rect[0], rect[1])) } else { None })
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A column chart of one category with a series per name.
|
||||
fn columns(names: &[&str]) -> String {
|
||||
let sers: String = names.iter().enumerate().map(|(i, n)| ser(i, n, Some("FF0000"), &["Q1"], &["1"])).collect();
|
||||
format!(
|
||||
r#"<c:barChart><c:barDir val="col"/><c:grouping val="clustered"/>{sers}<c:axId val="1"/><c:axId val="2"/></c:barChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bottom_legend_wraps_into_rows_inside_the_chart() {
|
||||
let names: Vec<String> = (0..12).map(|i| format!("Region number {i}")).collect();
|
||||
let names: Vec<&str> = names.iter().map(String::as_str).collect();
|
||||
let items = with_legend(&columns(&names), "b", 200.0, 200.0);
|
||||
let sw = swatches(&items);
|
||||
let rows: std::collections::BTreeSet<i32> = sw.iter().map(|(_, y)| *y as i32).collect();
|
||||
assert!(rows.len() > 1 && rows.len() <= MAX_LEGEND_ROWS, "rows {rows:?}");
|
||||
assert!(sw.iter().all(|(x, _)| *x >= 0.0 && *x <= 200.0), "{sw:?}");
|
||||
for it in &items {
|
||||
if let GraphicItem::Text { rect, text, .. } = it {
|
||||
assert!(rect[0] >= 0.0 && rect[0] + text_w(text, FONT) <= 200.0 + 1e-3, "{text:?} at {rect:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_legend_names_are_cut_to_a_share_of_the_chart() {
|
||||
let long = "A very long series name that would push the legend past the left edge of the chart";
|
||||
let items = with_legend(&columns(&[long, "Short"]), "r", 200.0, 150.0);
|
||||
assert!(swatches(&items).iter().all(|(x, _)| *x >= 0.0), "{:?}", swatches(&items));
|
||||
let cut = items.iter().find_map(|it| match it {
|
||||
GraphicItem::Text { text, rect, .. } if text.ends_with('…') => Some((text.clone(), rect[2])),
|
||||
_ => None,
|
||||
});
|
||||
let (text, w) = cut.expect("the long name is cut");
|
||||
assert!(text_w(&text, FONT) <= 200.0 * LEGEND_SHARE && w <= 200.0 * LEGEND_SHARE + 2.0, "{text:?} {w}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scatter_dates_show_as_dates() {
|
||||
let pts = |vals: &[&str]| -> String { vals.iter().enumerate().map(|(k, v)| format!(r#"<c:pt idx="{k}"><c:v>{v}</c:v></c:pt>"#)).collect() };
|
||||
let plot = format!(
|
||||
r#"<c:scatterChart><c:ser><c:idx val="0"/><c:xVal><c:numRef><c:numCache><c:formatCode>m/d/yyyy</c:formatCode><c:ptCount val="3"/>{}</c:numCache></c:numRef></c:xVal><c:yVal><c:numRef><c:numCache><c:formatCode>General</c:formatCode><c:ptCount val="3"/>{}</c:numCache></c:numRef></c:yVal></c:ser><c:axId val="1"/><c:axId val="2"/></c:scatterChart><c:valAx><c:axId val="1"/><c:axPos val="b"/></c:valAx><c:valAx><c:axId val="2"/><c:axPos val="l"/></c:valAx>"#,
|
||||
pts(&["45292", "45323", "45352"]),
|
||||
pts(&["1", "2", "3"])
|
||||
);
|
||||
let items = chart_items(&space(&plot), &theme(), 300.0, 200.0);
|
||||
let texts: Vec<&str> = items.iter().filter_map(|it| if let GraphicItem::Text { text, .. } = it { Some(text.as_str()) } else { None }).collect();
|
||||
assert!(texts.iter().any(|t| t.ends_with("/2024")), "{texts:?}");
|
||||
assert!(!texts.iter().any(|t| t.starts_with("45")), "serials shown: {texts:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn date1904_counts_from_1904() {
|
||||
assert_eq!(category_text(0.0, "m/d/yyyy", 1.0, true), "1/1/1904");
|
||||
assert_eq!(category_text(1462.0, "m/d/yyyy", 1.0, false), "1/1/1904");
|
||||
let x = format!(
|
||||
r#"<c:chartSpace {NS}><c:date1904 val="1"/><c:chart><c:plotArea><c:lineChart><c:ser><c:cat><c:numRef><c:numCache><c:formatCode>yyyy-mm-dd</c:formatCode><c:ptCount val="1"/><c:pt idx="0"><c:v>366</c:v></c:pt></c:numCache></c:numRef></c:cat><c:val><c:numLit><c:ptCount val="1"/><c:pt idx="0"><c:v>1</c:v></c:pt></c:numLit></c:val></c:ser></c:lineChart><c:catAx/><c:valAx/></c:plotArea></c:chart></c:chartSpace>"#
|
||||
);
|
||||
let chart = Chart::parse(&crate::xml::parse(x.as_bytes()).unwrap(), &theme()).expect("chart");
|
||||
assert_eq!(chart.cats, ["1905-01-01"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn date_codes_show_times_weekdays_and_early_serials() {
|
||||
let t = |v: f64, code: &str| category_text(v, code, 1.0, false);
|
||||
// 45292 is Monday 1 January 2024; 13:05:09 into it.
|
||||
let at = 45292.0 + (13.0 * 3600.0 + 5.0 * 60.0 + 9.0) / 86_400.0;
|
||||
assert_eq!(t(at, "hh:mm:ss"), "13:05:09");
|
||||
assert_eq!(t(at, "h:mm AM/PM"), "1:05 PM");
|
||||
assert_eq!(t(at, "m:ss"), "5:09");
|
||||
assert_eq!(t(45292.0, "h:mm a/p"), "12:00 A");
|
||||
assert_eq!(t(at, "mm/dd"), "01/01");
|
||||
assert_eq!(t(45292.0, "dddd, mmmm d"), "Monday, January 1");
|
||||
assert_eq!(t(45292.0, "ddd d mmm"), "Mon 1 Jan");
|
||||
// Serials before March 1900, Excel's phantom 29 February included.
|
||||
assert_eq!(t(0.0, "m/d/yyyy"), "1/0/1900");
|
||||
assert_eq!(t(1.0, "m/d/yyyy"), "1/1/1900");
|
||||
assert_eq!(t(59.0, "m/d/yyyy"), "2/28/1900");
|
||||
assert_eq!(t(60.0, "m/d/yyyy"), "2/29/1900");
|
||||
assert_eq!(t(61.0, "m/d/yyyy dddd"), "3/1/1900 Thursday");
|
||||
// Outside the calendar: a number.
|
||||
assert_eq!(t(-1.0, "m/d/yyyy"), "-1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_sections_and_scientific_numbers() {
|
||||
assert_eq!(format_value(-5.0, "#,##0;(#,##0)", 1.0), "(5)");
|
||||
assert_eq!(format_value(5.0, "#,##0;(#,##0)", 1.0), "5");
|
||||
assert_eq!(format_value(-1500.0, "#,##0_);(#,##0)", 500.0), "(1,500)");
|
||||
assert_eq!(format_value(-2.0, "0\";\"0;-0", 1.0), "-2");
|
||||
assert_eq!(format_value(12345.0, "0.00E+00", 1.0), "1.23E+04");
|
||||
assert_eq!(format_value(0.00012, "0.0E+0", 0.0001), "1.2E-4");
|
||||
assert_eq!(format_value(0.0, "0.00E+00", 1.0), "0.00E+00");
|
||||
assert_eq!(format_value(-12345.0, "0.0E-00", 1.0), "-1.2E04");
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
//! `w:altChunk` (ECMA-376 §17.17.2.1): content kept in another format (HTML, MHT, RTF, plain
|
||||
//! text, another WordprocessingML document) that Word brings into the document where the chunk
|
||||
//! stands when it opens the file. A nested WordprocessingML package is read here; other formats
|
||||
//! go to the caller's [`ChunkImporter`] (the format crates sit beside this one), and without one
|
||||
//! the chunk is left out.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use wordcraft_doc::{Block, Blocks, Document};
|
||||
|
||||
use super::story::StoryCtx;
|
||||
use super::{Reader, part_of};
|
||||
use crate::package::{ContentTypes, MAX_PART, MAX_TOTAL, Package, Rels, rt};
|
||||
use crate::xml::El;
|
||||
|
||||
/// Most chunks brought in from one file (nested documents' chunks included).
|
||||
pub(crate) const MAX_CHUNKS: usize = 256;
|
||||
/// Deepest a WordprocessingML chunk may nest in another.
|
||||
pub(crate) const MAX_CHUNK_NESTING: u8 = 4;
|
||||
|
||||
/// An alternative format chunk's content, as a [`ChunkImporter`] gets it.
|
||||
pub struct AltChunk<'a> {
|
||||
/// The part's content type (`application/xhtml+xml`, `message/rfc822`, `application/rtf`,
|
||||
/// `text/plain`…), empty when the package doesn't give one.
|
||||
pub content_type: &'a str,
|
||||
/// The part's name (`word/afchunk.mht`): its extension tells the format when the content type
|
||||
/// doesn't.
|
||||
pub part: &'a str,
|
||||
pub bytes: &'a [u8],
|
||||
}
|
||||
|
||||
/// Turns a chunk the docx reader can't read itself into a document whose body takes the chunk's
|
||||
/// place; `None` leaves the chunk out.
|
||||
pub type ChunkImporter<'a> = &'a dyn Fn(&AltChunk) -> Option<Document>;
|
||||
|
||||
/// How [`crate::read_with`] reads a package.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct ReadOptions<'a> {
|
||||
/// The password of an encrypted package.
|
||||
pub password: Option<&'a str>,
|
||||
/// Imports `w:altChunk` content in formats other than WordprocessingML.
|
||||
pub chunks: Option<ChunkImporter<'a>>,
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
/// Bring the chunk `e` names into `out` where it stands (`depth`: of the XML around it).
|
||||
pub(crate) fn read_alt_chunk(&mut self, sc: &mut StoryCtx, e: &El, rels: &Rels, out: &mut Blocks, depth: usize) {
|
||||
let Some(part) = e.attr("r:id").and_then(|id| part_of(rels, id, rt::AF_CHUNK)) else { return };
|
||||
let pkg = self.pkg;
|
||||
let Some(bytes) = pkg.get(&part) else {
|
||||
log::warn!("docx: alternative format chunk {part} is missing");
|
||||
return;
|
||||
};
|
||||
// Every chunk's bytes count against one budget for the whole file, however often the same
|
||||
// part is named and however deep chunks nest.
|
||||
if self.chunks.left == 0 || bytes.len() as u64 > self.chunks.bytes {
|
||||
log::warn!("docx: more than {MAX_CHUNKS} alternative format chunks or {MAX_TOTAL} bytes of them; the rest are left out");
|
||||
return;
|
||||
}
|
||||
self.chunks.left -= 1;
|
||||
self.chunks.bytes -= bytes.len() as u64;
|
||||
let ct = self.content_types.get_or_insert_with(|| ContentTypes::read(pkg)).of(&part).unwrap_or("").to_string();
|
||||
let chunk = AltChunk { content_type: &ct, part: &part, bytes };
|
||||
// A WordprocessingML chunk is a package; one that isn't Word's fails to read (logged).
|
||||
let is_docx = bytes.starts_with(b"PK\x03\x04");
|
||||
let doc = if is_docx { self.read_nested(bytes, depth) } else { self.chunks.import.and_then(|f| f(&chunk)) };
|
||||
let Some(mut doc) = doc else {
|
||||
log::warn!("docx: alternative format chunk {part} ({ct}) is in a format WordCraft can't bring in");
|
||||
return;
|
||||
};
|
||||
// The chunk flows in the section it stands in; its own section breaks don't come along.
|
||||
for b in &mut doc.body {
|
||||
if let Block::Para(p) = &**b
|
||||
&& p.section.is_some()
|
||||
&& let Block::Para(p) = Arc::make_mut(b)
|
||||
{
|
||||
p.section = None;
|
||||
}
|
||||
}
|
||||
// `w:matchSrc`: a Word document's content keeps its own styles (other formats' formatting
|
||||
// is their own already).
|
||||
let keep = is_docx && e.child("w:altChunkPr").and_then(|p| p.child("w:matchSrc")).is_some_and(crate::units::on_off);
|
||||
let mut blocks = self.doc.adopt_blocks(doc, keep, &mut self.next_perm);
|
||||
// Bookmarks and comment marks read before the chunk stay before it.
|
||||
self.flush_pending_before(sc, out, &mut blocks);
|
||||
out.extend(blocks.into_iter().map(Arc::new));
|
||||
}
|
||||
|
||||
/// A WordprocessingML chunk: its document, read with the same importer and what is left of
|
||||
/// the budgets, its XML depth counting on from `depth`.
|
||||
fn read_nested(&mut self, bytes: &[u8], depth: usize) -> Option<Document> {
|
||||
if self.chunks.nesting >= MAX_CHUNK_NESTING {
|
||||
log::warn!("docx: alternative format chunks nest deeper than {MAX_CHUNK_NESTING}; the inner ones are left out");
|
||||
return None;
|
||||
}
|
||||
let pkg =
|
||||
Package::open_limited(bytes, MAX_PART, self.chunks.bytes).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?;
|
||||
self.chunks.bytes = self.chunks.bytes.saturating_sub(pkg.size());
|
||||
let state = ChunkState { nesting: self.chunks.nesting + 1, depth: depth + 1, ..self.chunks };
|
||||
let (doc, left, bytes, graphic) =
|
||||
super::read_opened_in(&pkg, state).map_err(|e| log::warn!("docx: unreadable nested document chunk: {e}")).ok()?;
|
||||
self.chunks.left = left;
|
||||
self.chunks.bytes = bytes;
|
||||
self.chunks.graphic = graphic;
|
||||
Some(doc)
|
||||
}
|
||||
}
|
||||
|
||||
/// What a reader may still bring in from chunks.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct ChunkState<'a> {
|
||||
/// Chunks left in the file's budget ([`MAX_CHUNKS`]).
|
||||
pub left: usize,
|
||||
/// Chunk bytes (nested packages unpacked) left in the file's budget.
|
||||
pub bytes: u64,
|
||||
/// Graphic work left in the file's budget ([`super::MAX_GRAPHIC_WORK`]): nested documents'
|
||||
/// charts and diagrams count against it too.
|
||||
pub graphic: usize,
|
||||
/// How deep in nested WordprocessingML chunks this reader is.
|
||||
pub nesting: u8,
|
||||
/// The XML depth its stories start at (a nested document's: that of the chunk naming it).
|
||||
pub depth: usize,
|
||||
pub import: Option<ChunkImporter<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> ChunkState<'a> {
|
||||
pub fn new(import: Option<ChunkImporter<'a>>) -> Self {
|
||||
ChunkState { left: MAX_CHUNKS, bytes: MAX_TOTAL, graphic: super::MAX_GRAPHIC_WORK, nesting: 0, depth: 0, import }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Write;
|
||||
|
||||
use wordcraft_doc::{Block, Document, InlineObject};
|
||||
|
||||
use super::ChunkState;
|
||||
use crate::package::Package;
|
||||
|
||||
/// A package whose body is `body`, with the document relationship `rel` (id, type, target) to
|
||||
/// the part `(name, bytes)`.
|
||||
fn package(body: &str, (id, ty, target): (&str, &str, &str), (name, bytes): (&str, &[u8])) -> Vec<u8> {
|
||||
let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main""#;
|
||||
let rels =
|
||||
|inner: &str| format!(r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{inner}</Relationships>"#);
|
||||
let rel = |id: &str, ty: &str, target: &str| {
|
||||
format!(r#"<Relationship Id="{id}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/{ty}" Target="{target}"/>"#)
|
||||
};
|
||||
let files: [(&str, Vec<u8>); 4] = [
|
||||
("_rels/.rels", rels(&rel("rId1", "officeDocument", "word/document.xml")).into_bytes()),
|
||||
("word/document.xml", format!(r#"<w:document {ns}><w:body>{body}</w:body></w:document>"#).into_bytes()),
|
||||
("word/_rels/document.xml.rels", rels(&rel(id, ty, target)).into_bytes()),
|
||||
(name, bytes.to_vec()),
|
||||
];
|
||||
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
for (name, bytes) in files {
|
||||
zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
||||
zw.write_all(&bytes).unwrap();
|
||||
}
|
||||
zw.finish().unwrap().into_inner()
|
||||
}
|
||||
|
||||
/// Whether the body's first paragraph's chart is drawn.
|
||||
fn drawn(d: &Document) -> bool {
|
||||
let Some(Block::Para(p)) = d.body.first().map(|b| &**b) else { panic!("no paragraph") };
|
||||
p.objects.iter().any(|o| matches!(o, InlineObject::Graphic { graphic, .. } if !graphic.items.is_empty()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_nested_documents_charts_count_against_the_files_graphic_budget() {
|
||||
let chart = r#"<c:chartSpace xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"><c:chart><c:plotArea><c:barChart><c:ser><c:val><c:numLit><c:ptCount val="2"/><c:pt idx="0"><c:v>1</c:v></c:pt><c:pt idx="1"><c:v>2</c:v></c:pt></c:numLit></c:val></c:ser></c:barChart></c:plotArea></c:chart></c:chartSpace>"#;
|
||||
let drawing = r#"<w:p><w:r><w:drawing><wp:inline><wp:extent cx="2743200" cy="1828800"/><wp:docPr id="5" name="G"/><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart"><c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" r:id="rIdChart"/></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p>"#;
|
||||
let inner = package(drawing, ("rIdChart", "chart", "charts/chart1.xml"), ("word/charts/chart1.xml", chart.as_bytes()));
|
||||
let outer = package(r#"<w:altChunk r:id="rIdC"/>"#, ("rIdC", "aFChunk", "afchunk.docx"), ("word/afchunk.docx", &inner));
|
||||
let pkg = Package::open(&outer).unwrap();
|
||||
let read = |graphic| {
|
||||
let (d, .., left) = super::super::read_opened_in(&pkg, ChunkState { graphic, ..ChunkState::new(None) }).unwrap();
|
||||
(drawn(&d), left)
|
||||
};
|
||||
let (shown, left) = read(super::super::MAX_GRAPHIC_WORK);
|
||||
assert!(shown);
|
||||
assert!(left < super::super::MAX_GRAPHIC_WORK, "the nested chart's work is the file's");
|
||||
// With the file's budget spent, the nested document doesn't get one of its own.
|
||||
assert_eq!(read(0), (false, 0));
|
||||
}
|
||||
}
|
||||
@@ -1,697 +0,0 @@
|
||||
//! SmartArt diagrams: the ready-made drawing (`dsp:drawing`) Word keeps beside the diagram data,
|
||||
//! converted to [`GraphicItem`]s. The diagram's layout isn't run; the saved drawing is the picture.
|
||||
|
||||
use wordcraft_doc::graphic::{GraphicItem, PathSeg, TextAlign};
|
||||
use wordcraft_doc::para::ShapeKind;
|
||||
use wordcraft_doc::props::Rgb;
|
||||
|
||||
use super::Reader;
|
||||
use super::chart::text_w;
|
||||
use super::drawing_color::{color, paint};
|
||||
use crate::package::{Rel, Rels, rel_is, rt};
|
||||
use crate::units::{MAX_LEN_PT, int};
|
||||
use crate::xml::El;
|
||||
|
||||
/// Deepest `dsp:grpSp` nesting followed.
|
||||
const MAX_GROUP_DEPTH: usize = 8;
|
||||
/// Most shapes read from one drawing.
|
||||
const MAX_SHAPES: usize = 10_000;
|
||||
/// Most items emitted for one diagram.
|
||||
const MAX_ITEMS: usize = 50_000;
|
||||
/// EMU per point.
|
||||
const EMU_PT: f64 = 12_700.0;
|
||||
/// Largest coordinate or extent accepted, EMU (about 78,000 pt).
|
||||
const MAX_EMU: f64 = 1.0e9;
|
||||
/// Line height as a multiple of the font size.
|
||||
const LINE: f32 = 1.2;
|
||||
|
||||
/// An affine map per axis (`x' = kx·x + ox`, `y' = ky·y + oy`).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Xf {
|
||||
kx: f64,
|
||||
ox: f64,
|
||||
ky: f64,
|
||||
oy: f64,
|
||||
}
|
||||
|
||||
impl Xf {
|
||||
const ID: Xf = Xf { kx: 1.0, ox: 0.0, ky: 1.0, oy: 0.0 };
|
||||
|
||||
/// `self` applied after `inner`.
|
||||
fn then(self, inner: Xf) -> Xf {
|
||||
Xf { kx: self.kx * inner.kx, ox: self.kx * inner.ox + self.ox, ky: self.ky * inner.ky, oy: self.ky * inner.oy + self.oy }
|
||||
}
|
||||
|
||||
/// The box `[x, y, w, h]` mapped to corners `[x0, y0, x1, y1]`.
|
||||
fn rect(self, b: [f64; 4]) -> [f64; 4] {
|
||||
let (x0, x1) = (self.kx * b[0] + self.ox, self.kx * (b[0] + b[2]) + self.ox);
|
||||
let (y0, y1) = (self.ky * b[1] + self.oy, self.ky * (b[1] + b[3]) + self.oy);
|
||||
[x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)]
|
||||
}
|
||||
}
|
||||
|
||||
/// A shape's outline in its own box: points and whether it's closed (a fill needs closed).
|
||||
type Poly = (Vec<(f64, f64)>, bool);
|
||||
|
||||
enum Geo {
|
||||
Kind(ShapeKind),
|
||||
Poly(Vec<(f64, f64)>, bool),
|
||||
}
|
||||
|
||||
/// Points, clamped into the range layout accepts (NaN, from a degenerate transform, is 0).
|
||||
fn pt(v: f64) -> f32 {
|
||||
if v.is_nan() { 0.0 } else { v.clamp(-MAX_LEN_PT as f64, MAX_LEN_PT as f64) as f32 }
|
||||
}
|
||||
|
||||
/// An EMU attribute of `e`, finite and bounded.
|
||||
fn emu(e: &El, attr: &str) -> Option<f64> {
|
||||
int(e.attr(attr)?).map(|v| (v as f64).clamp(-MAX_EMU, MAX_EMU))
|
||||
}
|
||||
|
||||
/// `a:off` / `a:ext` of an element (a shape's `a:xfrm`, `dsp:txXfrm`) as `[x, y, w, h]` in EMU.
|
||||
fn xfrm_box(x: &El) -> Option<[f64; 4]> {
|
||||
let (off, ext) = (x.child("a:off")?, x.child("a:ext")?);
|
||||
Some([emu(off, "x")?, emu(off, "y")?, emu(ext, "cx")?.max(0.0), emu(ext, "cy")?.max(0.0)])
|
||||
}
|
||||
|
||||
/// Child-space to parent-space scale of a group, from its `a:xfrm` (`off`/`ext` against `chOff`/`chExt`).
|
||||
fn group_xf(grp: &El) -> Xf {
|
||||
let Some(x) = grp.child("dsp:grpSpPr").and_then(|p| p.child("a:xfrm")) else { return Xf::ID };
|
||||
let v = |tag: &str, a: &str| x.child(tag).and_then(|e| emu(e, a)).unwrap_or(0.0);
|
||||
let scale = |e: f64, c: f64| if c > 0.0 && e >= 0.0 { e / c } else { 1.0 };
|
||||
let kx = scale(v("a:ext", "cx"), v("a:chExt", "cx"));
|
||||
let ky = scale(v("a:ext", "cy"), v("a:chExt", "cy"));
|
||||
Xf { kx, ox: v("a:off", "x") - v("a:chOff", "x") * kx, ky, oy: v("a:off", "y") - v("a:chOff", "y") * ky }
|
||||
}
|
||||
|
||||
/// Shapes in `tree` (depth-first through groups), each with its transform into the drawing.
|
||||
fn collect<'a>(tree: &'a El, xf: Xf, depth: usize, out: &mut Vec<(Xf, &'a El)>) {
|
||||
for e in tree.els() {
|
||||
match e.name.as_str() {
|
||||
"dsp:sp" if out.len() < MAX_SHAPES => out.push((xf, e)),
|
||||
"dsp:grpSp" if depth < MAX_GROUP_DEPTH => collect(e, xf.then(group_xf(e)), depth + 1, out),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The items of a SmartArt drawing, scaled so its extent fills the `w` × `h` (points) frame.
|
||||
/// `image` maps a relationship id of the drawing part to a `Document::media` key.
|
||||
pub(crate) fn diagram_items(drawing: &El, theme: &[Rgb], w: f32, h: f32, image: &mut dyn FnMut(&str) -> Option<String>) -> Vec<GraphicItem> {
|
||||
let Some(tree) = drawing.find("dsp:spTree") else { return Vec::new() };
|
||||
let mut shapes = Vec::new();
|
||||
collect(tree, Xf::ID, 0, &mut shapes);
|
||||
let placed: Vec<(Xf, &El, [f64; 4])> = shapes
|
||||
.into_iter()
|
||||
.filter_map(|(xf, sp)| {
|
||||
let b = sp.child("dsp:spPr").and_then(|p| p.child("a:xfrm")).and_then(xfrm_box)?;
|
||||
Some((xf, sp, b))
|
||||
})
|
||||
.collect();
|
||||
// The drawing's extent (from its origin) against the frame. One scale for both axes, never
|
||||
// above 1:1: Word draws the drawing at its own size when it fits the frame.
|
||||
let (ext_x, ext_y) = placed.iter().fold((0.0f64, 0.0f64), |(mx, my), (xf, _, b)| {
|
||||
let r = xf.rect(*b);
|
||||
(mx.max(r[2]), my.max(r[3]))
|
||||
});
|
||||
let frame = |f: f32| if f.is_finite() && f > 0.0 { f as f64 * EMU_PT } else { 0.0 };
|
||||
let fit_of = |frame: f64, ext: f64| if frame > 0.0 && ext > 0.0 { frame / ext } else { 1.0 };
|
||||
let fit = [1.0, fit_of(frame(w), ext_x), fit_of(frame(h), ext_y)].into_iter().fold(f64::INFINITY, f64::min);
|
||||
let root = Xf { kx: fit / EMU_PT, ox: 0.0, ky: fit / EMU_PT, oy: 0.0 };
|
||||
let mut out = Vec::new();
|
||||
for (xf, sp, b) in placed {
|
||||
if out.len() >= MAX_ITEMS {
|
||||
break;
|
||||
}
|
||||
shape(sp, root.then(xf), b, theme, fit as f32, image, &mut out);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// One `dsp:sp`: its picture or outline, then its text.
|
||||
/// `fit` is the drawing's scale into the frame, which text sizes follow.
|
||||
fn shape(sp: &El, xf: Xf, b: [f64; 4], theme: &[Rgb], fit: f32, image: &mut dyn FnMut(&str) -> Option<String>, out: &mut Vec<GraphicItem>) {
|
||||
let [x0, y0, x1, y1] = xf.rect(b);
|
||||
if ![x0, y0, x1, y1].iter().all(|v| v.is_finite()) {
|
||||
return;
|
||||
}
|
||||
let (w, h) = (x1 - x0, y1 - y0);
|
||||
let sppr = sp.child("dsp:spPr");
|
||||
let xfrm = sppr.and_then(|p| p.child("a:xfrm"));
|
||||
let attr = |n: &str| xfrm.and_then(|x| x.attr(n));
|
||||
let rot = attr("rot").and_then(int).unwrap_or(0).rem_euclid(21_600_000) as f64 / 60_000.0;
|
||||
let flip_h = attr("flipH").is_some_and(on);
|
||||
let flip_v = attr("flipV").is_some_and(on);
|
||||
let turned = rot != 0.0 || flip_h || flip_v;
|
||||
let rect = [pt(x0), pt(y0), pt(w), pt(h)];
|
||||
let filled;
|
||||
if let Some(blip) = sppr.and_then(|p| p.child("a:blipFill")).and_then(|f| f.find("a:blip")) {
|
||||
// A picture fill stretches into the shape's box; no clipping to the outline.
|
||||
if let Some(media) = blip.attr("r:embed").and_then(image) {
|
||||
out.push(GraphicItem::Image { rect, media });
|
||||
}
|
||||
filled = false;
|
||||
} else {
|
||||
let fill = fill_of(sp, sppr, theme);
|
||||
let (stroke, stroke_width) = line_of(sp, sppr, theme);
|
||||
let prst = sppr.and_then(|p| p.child("a:prstGeom")).and_then(|g| g.attr("prst")).unwrap_or("rect");
|
||||
let geom = sppr.and_then(|p| p.child("a:prstGeom"));
|
||||
let geo = match kind_of(prst) {
|
||||
Some(k) if !turned || prst == "ellipse" => Geo::Kind(k),
|
||||
_ => match outline(prst, w, h, geom) {
|
||||
Some((pts, closed)) => Geo::Poly(pts, closed),
|
||||
None => Geo::Kind(kind_of(prst).unwrap_or(ShapeKind::Rectangle)),
|
||||
},
|
||||
};
|
||||
match geo {
|
||||
Geo::Kind(kind) => out.push(GraphicItem::Shape { rect, kind, fill, stroke, stroke_width }),
|
||||
Geo::Poly(pts, closed) if !pts.is_empty() => {
|
||||
// Flip in the box, then turn about its centre (clockwise, y down).
|
||||
let (s, c) = rot.to_radians().sin_cos();
|
||||
let (cx, cy) = (w / 2.0, h / 2.0);
|
||||
let map = |(px, py): (f64, f64)| {
|
||||
let px = if flip_h { w - px } else { px };
|
||||
let py = if flip_v { h - py } else { py };
|
||||
let (dx, dy) = (px - cx, py - cy);
|
||||
(x0 + cx + dx * c - dy * s, y0 + cy + dx * s + dy * c)
|
||||
};
|
||||
let mut segs = Vec::with_capacity(pts.len() + 1);
|
||||
for (i, p) in pts.into_iter().enumerate() {
|
||||
let (x, y) = map(p);
|
||||
segs.push(if i == 0 { PathSeg::Move(pt(x), pt(y)) } else { PathSeg::Line(pt(x), pt(y)) });
|
||||
}
|
||||
if closed {
|
||||
segs.push(PathSeg::Close);
|
||||
}
|
||||
let fill = if closed { fill } else { None };
|
||||
out.push(GraphicItem::Path { segs, fill, stroke, stroke_width });
|
||||
}
|
||||
Geo::Poly(..) => {}
|
||||
}
|
||||
filled = fill.is_some();
|
||||
}
|
||||
text(sp, xf, b, theme, filled, fit, out);
|
||||
}
|
||||
|
||||
fn on(v: &str) -> bool {
|
||||
v == "1" || v == "true"
|
||||
}
|
||||
|
||||
/// The colour of a shape's style reference (`a:fillRef`, `a:lnRef`); index 0 means none.
|
||||
fn style_colour(sp: &El, which: &str, theme: &[Rgb]) -> Option<Rgb> {
|
||||
let r = sp.child("dsp:style")?.child(which)?;
|
||||
if r.attr("idx") == Some("0") {
|
||||
return None;
|
||||
}
|
||||
color(r, theme)
|
||||
}
|
||||
|
||||
/// A shape's fill: its own `a:*Fill`, else the style's fill colour.
|
||||
fn fill_of(sp: &El, sppr: Option<&El>, theme: &[Rgb]) -> Option<Rgb> {
|
||||
let own = sppr.and_then(|p| p.els().find(|e| matches!(e.name.as_str(), "a:solidFill" | "a:noFill" | "a:gradFill" | "a:pattFill")));
|
||||
match own {
|
||||
Some(e) => paint(e, theme),
|
||||
None => style_colour(sp, "a:fillRef", theme),
|
||||
}
|
||||
}
|
||||
|
||||
/// A shape's outline colour and width in points (`a:ln`, else the style's line at 0.75 pt).
|
||||
fn line_of(sp: &El, sppr: Option<&El>, theme: &[Rgb]) -> (Option<Rgb>, f32) {
|
||||
let ln = sppr.and_then(|p| p.child("a:ln"));
|
||||
let width = ln.and_then(|l| l.attr("w")).and_then(int).map_or(0.75, |v| (v as f64 / EMU_PT).clamp(0.0, 100.0) as f32);
|
||||
let own = ln.and_then(|l| l.els().find(|e| matches!(e.name.as_str(), "a:solidFill" | "a:noFill" | "a:gradFill")));
|
||||
match own {
|
||||
Some(e) if e.name == "a:noFill" => (None, 0.0),
|
||||
Some(e) => (paint(e, theme), width),
|
||||
None => (style_colour(sp, "a:lnRef", theme), width),
|
||||
}
|
||||
}
|
||||
|
||||
/// The preset's own [`ShapeKind`], for the presets that have one. `None` means a polygon.
|
||||
fn kind_of(prst: &str) -> Option<ShapeKind> {
|
||||
Some(match prst {
|
||||
"roundRect" => ShapeKind::RoundedRectangle,
|
||||
"ellipse" => ShapeKind::Ellipse,
|
||||
"triangle" => ShapeKind::Triangle,
|
||||
"diamond" => ShapeKind::Diamond,
|
||||
// A line runs from its box's top-left to its bottom-right corner (a path; the Line shape
|
||||
// kind draws the other diagonal).
|
||||
"line" | "straightConnector1" => return None,
|
||||
"rightArrow" => ShapeKind::Arrow,
|
||||
"star5" | "star4" | "star6" => ShapeKind::Star,
|
||||
"heart" => ShapeKind::Heart,
|
||||
"leftArrow" | "upArrow" | "downArrow" | "leftRightArrow" | "upDownArrow" | "chevron" | "homePlate" | "hexagon" | "pentagon" | "trapezoid"
|
||||
| "parallelogram" | "rtTriangle" | "pie" | "blockArc" => return None,
|
||||
_ => ShapeKind::Rectangle,
|
||||
})
|
||||
}
|
||||
|
||||
/// A preset's adjust value (`a:avLst` `a:gd name`, `fmla="val N"`) in its raw units, `def` when absent.
|
||||
fn raw_adj(geom: Option<&El>, name: &str, def: f64) -> f64 {
|
||||
geom.and_then(|g| g.child("a:avLst"))
|
||||
.and_then(|l| l.children("a:gd").find(|d| d.attr("name") == Some(name)))
|
||||
.and_then(|d| d.attr("fmla"))
|
||||
.and_then(|f| f.strip_prefix("val "))
|
||||
.and_then(|v| v.trim().parse::<f64>().ok())
|
||||
.filter(|v| v.is_finite())
|
||||
.map_or(def, |v| v.clamp(-1.0e9, 1.0e9))
|
||||
}
|
||||
|
||||
/// A preset's adjust value as a fraction of the box (raw / 100000), `def` (a fraction) when absent.
|
||||
fn adj(geom: Option<&El>, name: &str, def: f64) -> f64 {
|
||||
raw_adj(geom, name, def * 100_000.0) / 100_000.0
|
||||
}
|
||||
|
||||
/// A pie slice (`pie`) or block arc (`blockArc`) from `adj1` to `adj2` (60000ths of a degree,
|
||||
/// clockwise from 3 o'clock); the block arc's band is `adj3` of the short side thick.
|
||||
fn sector(block: bool, w: f64, h: f64, geom: Option<&El>) -> Poly {
|
||||
let deg = |n: &str, def: f64| raw_adj(geom, n, def) / 60_000.0;
|
||||
let (st, en) = if block { (deg("adj1", 10_800_000.0), deg("adj2", 0.0)) } else { (deg("adj1", 0.0), deg("adj2", 16_200_000.0)) };
|
||||
let sweep = (en - st).rem_euclid(360.0);
|
||||
let (hw, hh) = (w / 2.0, h / 2.0);
|
||||
let arc = move |rx: f64, ry: f64| {
|
||||
(0..=36).map(move |k| {
|
||||
let t = (st + sweep * k as f64 / 36.0).to_radians();
|
||||
(hw + rx * t.cos(), hh + ry * t.sin())
|
||||
})
|
||||
};
|
||||
if block {
|
||||
let band = lim(w.min(h) * adj(geom, "adj3", 0.25), w.min(h) / 2.0);
|
||||
let pts = arc(hw, hh).chain(arc(hw - band, hh - band).rev()).collect();
|
||||
(pts, true)
|
||||
} else {
|
||||
let mut pts = vec![(hw, hh)];
|
||||
pts.extend(arc(hw, hh));
|
||||
(pts, true)
|
||||
}
|
||||
}
|
||||
|
||||
/// `v` limited to `[0, hi]` (never panics, unlike `clamp` with `hi < 0`).
|
||||
fn lim(v: f64, hi: f64) -> f64 {
|
||||
v.max(0.0).min(hi.max(0.0))
|
||||
}
|
||||
|
||||
/// The outline of a polygon preset in a `w` × `h` box (points), or `None` for presets with no
|
||||
/// polygon (ellipse, heart, stars). Unknown presets are rectangles.
|
||||
fn outline(prst: &str, w: f64, h: f64, geom: Option<&El>) -> Option<Poly> {
|
||||
let ss = w.min(h);
|
||||
let a = |n: &str, d: f64| adj(geom, n, d);
|
||||
let (hw, hh) = (w / 2.0, h / 2.0);
|
||||
let pts = match prst {
|
||||
"line" | "straightConnector1" => return Some((vec![(0.0, 0.0), (w, h)], false)),
|
||||
"pie" | "blockArc" => return Some(sector(prst == "blockArc", w, h, geom)),
|
||||
"ellipse" | "heart" | "star5" | "star4" | "star6" => return None,
|
||||
"roundRect" => return Some((rounded(w, h, lim(ss * a("adj", 0.16667), ss / 2.0)), true)),
|
||||
"triangle" => {
|
||||
let apex = lim(w * a("adj", 0.5), w);
|
||||
vec![(apex, 0.0), (w, h), (0.0, h)]
|
||||
}
|
||||
"rtTriangle" => vec![(0.0, 0.0), (0.0, h), (w, h)],
|
||||
"diamond" => vec![(hw, 0.0), (w, hh), (hw, h), (0.0, hh)],
|
||||
"rightArrow" | "leftArrow" | "upArrow" | "downArrow" => {
|
||||
let (len, thick) = if prst == "upArrow" || prst == "downArrow" { (h, w) } else { (w, h) };
|
||||
arrow(len, thick, a("adj1", 0.5), a("adj2", 0.5), false)
|
||||
.into_iter()
|
||||
.map(|(u, v)| match prst {
|
||||
"leftArrow" => (w - u, v),
|
||||
"upArrow" => (v, h - u),
|
||||
"downArrow" => (v, u),
|
||||
_ => (u, v),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
"leftRightArrow" | "upDownArrow" => {
|
||||
let (len, thick) = if prst == "upDownArrow" { (h, w) } else { (w, h) };
|
||||
arrow(len, thick, a("adj1", 0.5), a("adj2", 0.5), true)
|
||||
.into_iter()
|
||||
.map(|(u, v)| if prst == "upDownArrow" { (v, u) } else { (u, v) })
|
||||
.collect()
|
||||
}
|
||||
"chevron" => {
|
||||
let x1 = lim(ss * a("adj", 0.5), w);
|
||||
vec![(0.0, 0.0), (w - x1, 0.0), (w, hh), (w - x1, h), (0.0, h), (x1, hh)]
|
||||
}
|
||||
"homePlate" => {
|
||||
let x1 = w - lim(ss * a("adj", 0.5), w);
|
||||
vec![(0.0, 0.0), (x1, 0.0), (w, hh), (x1, h), (0.0, h)]
|
||||
}
|
||||
"hexagon" => {
|
||||
let x1 = lim(ss * a("adj", 0.25), w / 2.0);
|
||||
vec![(0.0, hh), (x1, 0.0), (w - x1, 0.0), (w, hh), (w - x1, h), (x1, h)]
|
||||
}
|
||||
"pentagon" => (0..5)
|
||||
.map(|k| {
|
||||
let t = (-90.0 + 72.0 * k as f64).to_radians();
|
||||
(hw + hw * t.cos(), hh + hh * t.sin())
|
||||
})
|
||||
.collect(),
|
||||
"trapezoid" => {
|
||||
let x2 = lim(ss * a("adj", 0.25), w / 2.0);
|
||||
vec![(0.0, h), (x2, 0.0), (w - x2, 0.0), (w, h)]
|
||||
}
|
||||
"parallelogram" => {
|
||||
let x2 = lim(ss * a("adj", 0.25), w);
|
||||
vec![(0.0, h), (x2, 0.0), (w, 0.0), (w - x2, h)]
|
||||
}
|
||||
_ => vec![(0.0, 0.0), (w, 0.0), (w, h), (0.0, h)],
|
||||
};
|
||||
Some((pts, true))
|
||||
}
|
||||
|
||||
/// A block arrow along `len` (tip at `len`) across `thick`. `a1` is the shaft thickness and `a2`
|
||||
/// the head length, as fractions of the box; `double` puts a head on both ends.
|
||||
fn arrow(len: f64, thick: f64, a1: f64, a2: f64, double: bool) -> Vec<(f64, f64)> {
|
||||
let head = lim(len.min(thick) * a2, len / 2.0);
|
||||
let half = lim(thick * a1 / 2.0, thick / 2.0);
|
||||
let c = thick / 2.0;
|
||||
if double {
|
||||
vec![
|
||||
(0.0, c),
|
||||
(head, 0.0),
|
||||
(head, c - half),
|
||||
(len - head, c - half),
|
||||
(len - head, 0.0),
|
||||
(len, c),
|
||||
(len - head, thick),
|
||||
(len - head, c + half),
|
||||
(head, c + half),
|
||||
(head, thick),
|
||||
]
|
||||
} else {
|
||||
vec![(0.0, c - half), (len - head, c - half), (len - head, 0.0), (len, c), (len - head, thick), (len - head, c + half), (0.0, c + half)]
|
||||
}
|
||||
}
|
||||
|
||||
/// A `w` × `h` rectangle with corners of radius `r`, each corner in six steps.
|
||||
fn rounded(w: f64, h: f64, r: f64) -> Vec<(f64, f64)> {
|
||||
let mut pts = Vec::with_capacity(28);
|
||||
for (cx, cy, start) in [(w - r, r, -90.0), (w - r, h - r, 0.0), (r, h - r, 90.0), (r, r, 180.0)] {
|
||||
for k in 0..=6 {
|
||||
let t = (start + 15.0 * k as f64).to_radians();
|
||||
pts.push((cx + r * t.cos(), cy + r * t.sin()));
|
||||
}
|
||||
}
|
||||
pts
|
||||
}
|
||||
|
||||
/// One line of a text item before it's placed.
|
||||
struct Line {
|
||||
text: String,
|
||||
size: f32,
|
||||
color: Rgb,
|
||||
bold: bool,
|
||||
align: TextAlign,
|
||||
font: Option<String>,
|
||||
}
|
||||
|
||||
/// The text of a `dsp:txBody`: one item per (wrapped) line, stacked and centred in the text box.
|
||||
fn text(sp: &El, xf: Xf, b: [f64; 4], theme: &[Rgb], filled: bool, fit: f32, out: &mut Vec<GraphicItem>) {
|
||||
let Some(tb) = sp.child("dsp:txBody") else { return };
|
||||
let body = tb.child("a:bodyPr");
|
||||
let scale = body
|
||||
.and_then(|p| p.child("a:normAutofit"))
|
||||
.and_then(|n| n.attr("fontScale"))
|
||||
.and_then(int)
|
||||
.map_or(1.0, |v| (v as f32 / 100_000.0).clamp(0.1, 1.0));
|
||||
let inset = |n: &str, def: f64| body.and_then(|p| p.attr(n)).and_then(int).map_or(def, |v| (v as f64).clamp(0.0, MAX_EMU));
|
||||
let text_box = match sp.child("dsp:txXfrm").and_then(xfrm_box) {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
let (l, t, r, bo) = (inset("lIns", 91_440.0), inset("tIns", 45_720.0), inset("rIns", 91_440.0), inset("bIns", 45_720.0));
|
||||
[b[0] + l, b[1] + t, (b[2] - l - r).max(0.0), (b[3] - t - bo).max(0.0)]
|
||||
}
|
||||
};
|
||||
let [x0, y0, x1, y1] = xf.rect(text_box);
|
||||
let (x0, y0, tw, th) = (pt(x0), pt(y0), pt(x1 - x0), pt(y1 - y0));
|
||||
// Each line is one item: stop at the diagram's item limit, however much text there is.
|
||||
let room = MAX_ITEMS.saturating_sub(out.len());
|
||||
let fallback = theme.get(if filled { 1 } else { 0 }).copied().unwrap_or(if filled { Rgb::WHITE } else { Rgb::BLACK });
|
||||
let font_ref = sp.child("dsp:style").and_then(|s| s.child("a:fontRef")).and_then(|f| color(f, theme));
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
for p in tb.children("a:p") {
|
||||
if lines.len() >= room {
|
||||
break;
|
||||
}
|
||||
let text: String = p.children("a:r").filter_map(|r| r.child("a:t")).map(|t| t.text()).collect();
|
||||
let text: String = text.chars().map(|c| if c.is_control() { ' ' } else { c }).collect();
|
||||
let text = text.trim();
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let run = p.children("a:r").find(|r| r.child("a:t").is_some());
|
||||
let rpr = run.and_then(|r| r.child("a:rPr"));
|
||||
// The file's size (18 pt when unset), reduced by the autofit and scaled with the drawing.
|
||||
let base = rpr.and_then(|r| r.attr("sz")).and_then(int).map_or(18.0, |v| (v as f32 / 100.0).clamp(1.0, 400.0));
|
||||
let size = base * scale * fit;
|
||||
let color = rpr.and_then(|r| r.child("a:solidFill")).and_then(|f| color(f, theme)).or(font_ref).unwrap_or(fallback);
|
||||
let bold = rpr.and_then(|r| r.attr("b")).is_some_and(on);
|
||||
let align = match p.child("a:pPr").and_then(|q| q.attr("algn")) {
|
||||
Some("l" | "just" | "dist") => TextAlign::Left,
|
||||
Some("r") => TextAlign::Right,
|
||||
_ => TextAlign::Center,
|
||||
};
|
||||
let font = rpr
|
||||
.and_then(|r| r.child("a:latin"))
|
||||
.and_then(|l| l.attr("typeface"))
|
||||
.filter(|t| !t.is_empty() && !t.starts_with('+'))
|
||||
.map(str::to_string);
|
||||
for line in wrap(text, size, tw, room - lines.len()) {
|
||||
lines.push(Line { text: line, size, color, bold, align, font: font.clone() });
|
||||
}
|
||||
}
|
||||
let total: f32 = lines.iter().map(|l| l.size * LINE).sum();
|
||||
let mut y = y0 + (th - total) / 2.0;
|
||||
for l in lines {
|
||||
let lh = l.size * LINE;
|
||||
out.push(GraphicItem::Text { rect: [x0, y, tw, lh], text: l.text, size: l.size, color: l.color, bold: l.bold, align: l.align, font: l.font });
|
||||
y += lh;
|
||||
}
|
||||
}
|
||||
|
||||
/// How wide `s` is at `size` points by the estimate chart and diagram text uses.
|
||||
pub(crate) fn text_width(s: &str, size: f32) -> f32 {
|
||||
text_w(s, size)
|
||||
}
|
||||
|
||||
/// Splits `text` into at most `max` lines of whole words that fit `width` points (by the shared
|
||||
/// text width estimate); a word wider than that keeps its own line.
|
||||
pub(crate) fn wrap(text: &str, size: f32, width: f32, max: usize) -> Vec<String> {
|
||||
let limit = if width > 0.0 && size > 0.0 { width } else { f32::INFINITY };
|
||||
let space = text_w(" ", size);
|
||||
let mut lines = Vec::new();
|
||||
let (mut cur, mut cur_w) = (String::new(), 0.0f32);
|
||||
for word in text.split_whitespace() {
|
||||
let w = text_w(word, size);
|
||||
if !cur.is_empty() && cur_w + space + w > limit {
|
||||
if lines.len() + 1 >= max {
|
||||
break;
|
||||
}
|
||||
lines.push(std::mem::take(&mut cur));
|
||||
cur_w = 0.0;
|
||||
}
|
||||
if !cur.is_empty() {
|
||||
cur.push(' ');
|
||||
cur_w += space;
|
||||
}
|
||||
cur.push_str(word);
|
||||
cur_w += w;
|
||||
}
|
||||
if !cur.is_empty() && lines.len() < max {
|
||||
lines.push(cur);
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
/// The drawing part of a SmartArt diagram (`a:graphicData` with `dgm:relIds`): the one its
|
||||
/// data part names, else the drawing numbered like the data part. `None` when there's none.
|
||||
pub(super) fn diagram_part(&mut self, gd: &El, rels: &Rels) -> Option<String> {
|
||||
let dm = gd.child("dgm:relIds")?.attr("r:dm")?;
|
||||
let data = super::part_of(rels, dm, rt::DIAGRAM_DATA)?;
|
||||
let ext_id = self.graphic_part(&data).and_then(|d| d.find("dsp:dataModelExt").and_then(|e| e.attr("relId")).map(str::to_string));
|
||||
let is_drawing = |r: &&Rel| !r.external && rel_is(&r.kind, rt::DIAGRAM_DRAWING);
|
||||
let drawing = ext_id.as_deref().and_then(|id| rels.by_id(id)).filter(is_drawing).or_else(|| {
|
||||
// No usable relationship id: the drawing whose number matches the data part's.
|
||||
let num = |t: &str| t.rsplit('/').next().unwrap_or(t).chars().filter(char::is_ascii_digit).collect::<String>();
|
||||
let n = num(&data);
|
||||
rels.list.iter().filter(is_drawing).find(|r| !n.is_empty() && num(&r.target) == n)
|
||||
});
|
||||
drawing.map(|r| r.target.clone())
|
||||
}
|
||||
|
||||
/// The id of the relationship to a SmartArt diagram's drawing part, as its data part names it
|
||||
/// (`dsp:dataModelExt relId`): the id the parts use, not the markup, so it's kept with them.
|
||||
pub(super) fn diagram_drawing_rel(&mut self, gd: &El, rels: &Rels) -> Option<String> {
|
||||
let dm = gd.child("dgm:relIds")?.attr("r:dm")?;
|
||||
let data = super::part_of(rels, dm, rt::DIAGRAM_DATA)?;
|
||||
let id = self.graphic_part(&data)?.find("dsp:dataModelExt")?.attr("relId")?.to_string();
|
||||
rels.by_id(&id).is_some_and(|r| !r.external && rel_is(&r.kind, rt::DIAGRAM_DRAWING)).then_some(id)
|
||||
}
|
||||
|
||||
/// The SmartArt model of a diagram WordCraft wrote (its data part exactly as WordCraft writes
|
||||
/// it); `None` for any other.
|
||||
pub(super) fn smart_art_spec(&mut self, gd: &El, rels: &Rels) -> Option<wordcraft_doc::smart_art::SmartArtSpec> {
|
||||
let dm = gd.child("dgm:relIds")?.attr("r:dm")?;
|
||||
let data = super::part_of(rels, dm, rt::DIAGRAM_DATA)?;
|
||||
crate::smart_art::spec_of(self.graphic_part(&data)?.as_ref())
|
||||
}
|
||||
|
||||
/// The items of the SmartArt drawing part at `path`, fitted to `w` × `h` points.
|
||||
pub(super) fn diagram_drawing(&mut self, path: &str, w: f32, h: f32) -> Vec<GraphicItem> {
|
||||
let Some(root) = self.graphic_part(path) else { return Vec::new() };
|
||||
let drels = self.pkg.rels(path);
|
||||
let theme = self.doc.settings.theme_colors.clone();
|
||||
let mut image = |id: &str| self.media_for(&drels, id);
|
||||
diagram_items(&root, &theme, w, h, &mut image)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use wordcraft_doc::THEME_COLORS;
|
||||
|
||||
const NS: &str = r#"xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#;
|
||||
|
||||
fn drawing(body: &str) -> El {
|
||||
crate::xml::parse(format!("<dsp:drawing {NS}><dsp:spTree>{body}</dsp:spTree></dsp:drawing>").as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
/// Items for a drawing whose only image relationship is `rId1`.
|
||||
fn items(body: &str, w: f32, h: f32) -> Vec<GraphicItem> {
|
||||
let root = drawing(body);
|
||||
diagram_items(&root, &THEME_COLORS, w, h, &mut |id| (id == "rId1").then(|| "image1.png".to_string()))
|
||||
}
|
||||
|
||||
/// A `dsp:sp` with the given geometry (`prst`, `xfrm` attributes) and extra spPr/txBody XML.
|
||||
fn sp(xfrm: &str, prst: &str, rest: &str) -> String {
|
||||
format!(
|
||||
r#"<dsp:sp><dsp:spPr><a:xfrm {xfrm}><a:off x="0" y="0"/><a:ext cx="1270000" cy="1270000"/></a:xfrm><a:prstGeom prst="{prst}"><a:avLst/></a:prstGeom>{rest}</dsp:spPr></dsp:sp>"#
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_rect_takes_theme_colour_and_text() {
|
||||
let body = r#"<dsp:sp><dsp:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="1270000" cy="635000"/></a:xfrm><a:prstGeom prst="roundRect"><a:avLst/></a:prstGeom><a:solidFill><a:schemeClr val="accent1"/></a:solidFill><a:ln><a:noFill/></a:ln></dsp:spPr><dsp:style><a:fontRef idx="minor"><a:schemeClr val="lt1"/></a:fontRef></dsp:style><dsp:txBody><a:bodyPr/><a:p><a:pPr algn="ctr"/><a:r><a:rPr sz="2000" b="1"/><a:t>Foo</a:t></a:r></a:p></dsp:txBody></dsp:sp>"#;
|
||||
let out = items(body, 100.0, 50.0);
|
||||
let GraphicItem::Shape { kind, fill, stroke, rect, .. } = &out[0] else { panic!("shape first: {out:?}") };
|
||||
assert_eq!(*kind, ShapeKind::RoundedRectangle);
|
||||
assert_eq!(*fill, Some(Rgb(0x15, 0x60, 0x82)));
|
||||
assert_eq!(*stroke, None);
|
||||
assert_eq!(*rect, [0.0, 0.0, 100.0, 50.0]);
|
||||
let GraphicItem::Text { text, size, color, bold, align, .. } = &out[1] else { panic!("text second: {out:?}") };
|
||||
assert_eq!(text, "Foo");
|
||||
assert_eq!((*size, *color, *bold, *align), (20.0, Rgb::WHITE, true, TextAlign::Center));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chevron_is_a_path() {
|
||||
let out = items(&sp("", "chevron", ""), 100.0, 100.0);
|
||||
let GraphicItem::Path { segs, .. } = &out[0] else { panic!("path: {out:?}") };
|
||||
assert_eq!(segs.len(), 7);
|
||||
assert_eq!(segs.last(), Some(&PathSeg::Close));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotated_rect_is_a_rotated_path() {
|
||||
let out = items(&sp(r#"rot="2700000""#, "rect", r#"<a:solidFill><a:srgbClr val="FF0000"/></a:solidFill>"#), 100.0, 100.0);
|
||||
let GraphicItem::Path { segs, fill, .. } = &out[0] else { panic!("path: {out:?}") };
|
||||
assert_eq!(*fill, Some(Rgb(255, 0, 0)));
|
||||
let PathSeg::Move(x, y) = segs[0] else { panic!("move") };
|
||||
// A 100 pt square turned 45 degrees about its centre: the top-left corner moves to the
|
||||
// middle of the top edge's extension.
|
||||
assert!((x - 50.0).abs() < 0.01 && (y + 20.71).abs() < 0.01, "got {x} {y}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lum_mod_darkens_the_theme_colour() {
|
||||
let rest = r#"<a:solidFill><a:schemeClr val="accent1"><a:lumMod val="50000"/></a:schemeClr></a:solidFill>"#;
|
||||
let out = items(&sp("", "rect", rest), 10.0, 10.0);
|
||||
let GraphicItem::Shape { fill, .. } = &out[0] else { panic!("shape") };
|
||||
let Rgb(r, g, b) = fill.expect("fill");
|
||||
// accent1 (156082) at half its luminance.
|
||||
assert!(r < 0x15 && g < 0x60 && b < 0x82, "got {r:02x}{g:02x}{b:02x}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn picture_fill_is_an_image() {
|
||||
let rest = r#"<a:blipFill><a:blip r:embed="rId1"/><a:stretch><a:fillRect/></a:stretch></a:blipFill>"#;
|
||||
let out = items(&sp("", "ellipse", rest), 10.0, 10.0);
|
||||
assert!(matches!(&out[0], GraphicItem::Image { media, .. } if media == "image1.png"), "{out:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_scales_the_drawing_and_groups_transform_children() {
|
||||
// A 2 x 2 inch drawing in a 72 pt frame: a group at the origin holds a half-size child.
|
||||
let grp = r#"<dsp:grpSp><dsp:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="1270000" cy="1270000"/><a:chOff x="0" y="0"/><a:chExt cx="2540000" cy="2540000"/></a:xfrm></dsp:grpSpPr><dsp:sp><dsp:spPr><a:xfrm><a:off x="1270000" y="1270000"/><a:ext cx="1270000" cy="1270000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></dsp:spPr></dsp:sp></dsp:grpSp>"#;
|
||||
let out = items(grp, 72.0, 72.0);
|
||||
let GraphicItem::Shape { rect, .. } = &out[0] else { panic!("shape: {out:?}") };
|
||||
// The group's child sits at half of the 1 in group extent (x = 0.5 in): 36 pt of the 72 pt frame.
|
||||
assert_eq!(*rect, [36.0, 36.0, 36.0, 36.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_text_wraps_into_several_lines() {
|
||||
let body = r#"<dsp:sp><dsp:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="1270000" cy="1270000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></dsp:spPr><dsp:txBody><a:bodyPr lIns="0" rIns="0"/><a:p><a:r><a:rPr sz="1000"/><a:t>one two three four five six</a:t></a:r></a:p></dsp:txBody></dsp:sp>"#;
|
||||
let out = items(body, 30.0, 30.0);
|
||||
let texts: Vec<&str> = out.iter().filter_map(|i| if let GraphicItem::Text { text, .. } = i { Some(text.as_str()) } else { None }).collect();
|
||||
assert!(texts.len() > 1, "{texts:?}");
|
||||
assert_eq!(texts.join(" "), "one two three four five six");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_numbers_and_nesting_do_not_panic() {
|
||||
let huge = format!(
|
||||
r#"<dsp:sp><dsp:spPr><a:xfrm rot="zzz" flipH="1"><a:off x="99999999999999999999999" y="-1e400"/><a:ext cx="-5" cy="NaN"/></a:xfrm><a:prstGeom prst="leftRightArrow"><a:avLst><a:gd name="adj1" fmla="val 1e999"/><a:gd name="adj2" fmla="val -7"/></a:avLst></a:prstGeom></dsp:spPr><dsp:txBody><a:bodyPr lIns="99999999999999999999" tIns="-9"/><a:p><a:r><a:rPr sz="99999999999999"/><a:t>x</a:t></a:r></a:p></dsp:txBody></dsp:sp>{}"#,
|
||||
sp(r#"rot="2700000""#, "chevron", "")
|
||||
);
|
||||
let mut nested = String::new();
|
||||
for _ in 0..40 {
|
||||
nested = format!(
|
||||
r#"<dsp:grpSp><dsp:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/><a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm></dsp:grpSpPr>{nested}{}</dsp:grpSp>"#,
|
||||
sp("", "rect", "")
|
||||
);
|
||||
}
|
||||
let _ = items(&huge, f32::NAN, -3.0);
|
||||
let _ = items(&huge, f32::INFINITY, 1.0e30);
|
||||
let _ = items(&nested, 100.0, 100.0);
|
||||
let root = drawing(&huge);
|
||||
assert!(diagram_items(&root, &[], 10.0, 10.0, &mut |_| None).len() <= MAX_ITEMS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_follows_autofit_and_the_drawing_fit() {
|
||||
// A 100 pt drawing: `fontScale` 50% halves its 20 pt text and the 18 pt default alike.
|
||||
let body = r#"<dsp:sp><dsp:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="1270000" cy="1270000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></dsp:spPr><dsp:txBody><a:bodyPr><a:normAutofit fontScale="50000"/></a:bodyPr><a:p><a:r><a:rPr sz="2000"/><a:t>Big</a:t></a:r></a:p><a:p><a:r><a:rPr/><a:t>Small</a:t></a:r></a:p></dsp:txBody></dsp:sp>"#;
|
||||
let sizes = |out: Vec<GraphicItem>| -> Vec<(String, f32)> {
|
||||
out.into_iter().filter_map(|i| if let GraphicItem::Text { text, size, .. } = i { Some((text, size)) } else { None }).collect()
|
||||
};
|
||||
assert_eq!(sizes(items(body, 100.0, 100.0)), [("Big".to_string(), 10.0), ("Small".to_string(), 9.0)]);
|
||||
// In a 50 pt frame the whole drawing is at half size, so its text is too.
|
||||
assert_eq!(sizes(items(body, 50.0, 50.0)), [("Big".to_string(), 5.0), ("Small".to_string(), 4.5)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_huge_text_body_stops_at_the_item_limit() {
|
||||
let words = "word ".repeat(MAX_ITEMS + 100);
|
||||
let body = format!(
|
||||
r#"<dsp:sp><dsp:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="12700" cy="12700"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></dsp:spPr><dsp:txBody><a:bodyPr lIns="0" rIns="0"/><a:p><a:r><a:t>{words}</a:t></a:r></a:p><a:p><a:r><a:t>{words}</a:t></a:r></a:p></dsp:txBody></dsp:sp>"#
|
||||
);
|
||||
let out = items(&body, 1.0, 1.0);
|
||||
assert_eq!(out.len(), MAX_ITEMS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_far_outside_nested_groups_stays_finite() {
|
||||
// Eight groups each scaling 1e9: a shape at -1e9 EMU ends up beyond f32 before clamping.
|
||||
let mut tree = r#"<dsp:sp><dsp:spPr><a:xfrm><a:off x="-1000000000" y="0"/><a:ext cx="1000000000" cy="0"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></dsp:spPr><dsp:txBody><a:bodyPr/><a:p><a:r><a:t>Far</a:t></a:r></a:p></dsp:txBody></dsp:sp>"#.to_string();
|
||||
for _ in 0..MAX_GROUP_DEPTH {
|
||||
tree = format!(
|
||||
r#"<dsp:grpSp><dsp:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="1000000000" cy="1"/><a:chOff x="0" y="0"/><a:chExt cx="1" cy="1"/></a:xfrm></dsp:grpSpPr>{tree}</dsp:grpSp>"#
|
||||
);
|
||||
}
|
||||
let out = items(&tree, 100.0, 100.0);
|
||||
let rects: Vec<[f32; 4]> = out.iter().filter_map(|i| if let GraphicItem::Text { rect, .. } = i { Some(*rect) } else { None }).collect();
|
||||
assert!(!rects.is_empty() && rects.iter().flatten().all(|v| v.is_finite()), "{rects:?}");
|
||||
}
|
||||
}
|
||||
@@ -1,296 +0,0 @@
|
||||
//! DrawingML colours shared by charts and SmartArt: a colour element with its modifiers, and the
|
||||
//! paint of a fill or line (solid, the first stop of a gradient, a pattern's foreground).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use wordcraft_doc::THEME_COLORS;
|
||||
use wordcraft_doc::fill::{Fill, Gradient, GradientFlip, GradientKind, PathShape};
|
||||
use wordcraft_doc::props::Rgb;
|
||||
use wordcraft_doc::wordart::{GradStop, MAX_STOPS};
|
||||
|
||||
use crate::units::int;
|
||||
use crate::xml::El;
|
||||
|
||||
/// Theme colour `i` (0 dk1, 1 lt1, 2 dk2, 3 lt2, 4..9 accent1..6, 10 hlink, 11 folHlink), falling
|
||||
/// back to the default theme.
|
||||
pub(crate) fn slot(theme: &[Rgb], i: usize) -> Rgb {
|
||||
theme.get(i).or_else(|| THEME_COLORS.get(i)).copied().unwrap_or(Rgb::BLACK)
|
||||
}
|
||||
|
||||
/// The colour of a fill or line element: a solid colour, the first stop of a gradient, or the
|
||||
/// foreground of a pattern. `None` for no fill and picture fills.
|
||||
pub(crate) fn paint(e: &El, theme: &[Rgb]) -> Option<Rgb> {
|
||||
match e.name.as_str() {
|
||||
"a:gradFill" => e.child("a:gsLst").and_then(|l| l.child("a:gs")).and_then(|g| color(g, theme)),
|
||||
"a:pattFill" => e.child("a:fgClr").and_then(|c| color(c, theme)),
|
||||
"a:noFill" | "a:blipFill" => None,
|
||||
_ => color(e, theme),
|
||||
}
|
||||
}
|
||||
|
||||
/// The fill of a `…Pr` or `a:ln` element (its first solid, gradient or pattern fill), as [`paint`].
|
||||
pub(crate) fn fill_colour(e: &El, theme: &[Rgb]) -> Option<Rgb> {
|
||||
let f = e.els().find(|c| matches!(c.name.as_str(), "a:solidFill" | "a:gradFill" | "a:pattFill"))?;
|
||||
paint(f, theme)
|
||||
}
|
||||
|
||||
fn is_color(e: &El) -> bool {
|
||||
matches!(e.name.as_str(), "a:srgbClr" | "a:schemeClr" | "a:sysClr" | "a:prstClr" | "a:scrgbClr" | "a:hslClr")
|
||||
}
|
||||
|
||||
/// A colour element (`a:srgbClr`, `a:schemeClr`, `a:sysClr`), or the one inside `e`, with its
|
||||
/// `lumMod` / `lumOff` / `tint` / `shade` / `satMod` applied in document order.
|
||||
pub(crate) fn color(e: &El, theme: &[Rgb]) -> Option<Rgb> {
|
||||
color_alpha(e, theme, None).map(|(c, _)| c)
|
||||
}
|
||||
|
||||
/// [`color`] and its transparency, percent (`a:alpha` is opacity, so 30% opaque is 70%
|
||||
/// transparent). `ph` is the colour and transparency `phClr` stands for (in a theme's fill
|
||||
/// styles: the colour of the shape's style reference).
|
||||
pub(crate) fn color_alpha(e: &El, theme: &[Rgb], ph: Option<(Rgb, f32)>) -> Option<(Rgb, f32)> {
|
||||
let c = if is_color(e) { e } else { e.els().find(|c| is_color(c))? };
|
||||
let mut opacity = 100.0;
|
||||
let base = match c.name.as_str() {
|
||||
"a:srgbClr" => Rgb::parse(c.attr("val")?)?,
|
||||
"a:schemeClr" if c.attr("val") == Some("phClr") => {
|
||||
let (rgb, t) = ph?;
|
||||
opacity -= t;
|
||||
rgb
|
||||
}
|
||||
"a:schemeClr" => *theme.get(scheme_slot(c.attr("val")?)?)?,
|
||||
"a:sysClr" => match c.attr("lastClr").and_then(Rgb::parse) {
|
||||
Some(rgb) => rgb,
|
||||
None => sys_color(c.attr("val")?)?,
|
||||
},
|
||||
"a:prstClr" => preset_color(c.attr("val")?)?,
|
||||
"a:scrgbClr" => {
|
||||
// Linear 0–100 000 per channel, to sRGB.
|
||||
let ch = |n: &str| -> u8 {
|
||||
let l = c.attr(n).and_then(int).map_or(0.0, |v| (v as f64 / 100_000.0).clamp(0.0, 1.0));
|
||||
let s = if l <= 0.003_130_8 { l * 12.92 } else { 1.055 * l.powf(1.0 / 2.4) - 0.055 };
|
||||
(s * 255.0).round().clamp(0.0, 255.0) as u8
|
||||
};
|
||||
Rgb(ch("r"), ch("g"), ch("b"))
|
||||
}
|
||||
"a:hslClr" => {
|
||||
let v = |n: &str| c.attr(n).and_then(int).unwrap_or(0) as f64;
|
||||
let hue = (v("hue") / 60_000.0).rem_euclid(360.0) / 360.0;
|
||||
Rgb::from_hsl(hue, (v("sat") / 100_000.0).clamp(0.0, 1.0), (v("lum") / 100_000.0).clamp(0.0, 1.0))
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
let val = |m: &El| m.attr("val").and_then(int).map_or(0.0, |v| (v as f64 / 100_000.0).clamp(-10.0, 10.0));
|
||||
let mut rgb = base;
|
||||
for m in c.els() {
|
||||
rgb = match m.name.as_str() {
|
||||
n @ ("a:lumMod" | "a:lumOff" | "a:tint" | "a:shade" | "a:satMod") => modified(rgb, n.trim_start_matches("a:"), val(m)),
|
||||
"a:alpha" => {
|
||||
opacity = val(m) as f32 * 100.0;
|
||||
rgb
|
||||
}
|
||||
"a:alphaMod" => {
|
||||
opacity *= val(m) as f32;
|
||||
rgb
|
||||
}
|
||||
"a:alphaOff" => {
|
||||
opacity += val(m) as f32 * 100.0;
|
||||
rgb
|
||||
}
|
||||
_ => rgb,
|
||||
};
|
||||
}
|
||||
Some((rgb, (100.0 - opacity).clamp(0.0, 100.0)))
|
||||
}
|
||||
|
||||
/// Fill element `e` (`a:solidFill`, `a:gradFill`; a pattern as its foreground colour) as a
|
||||
/// shape's fill; `None` for no fill, picture and group fills. `ph` as in [`color_alpha`].
|
||||
pub(crate) fn fill(e: &El, theme: &[Rgb], ph: Option<(Rgb, f32)>) -> Option<Fill> {
|
||||
let solid = |c: Option<&El>| c.and_then(|c| color_alpha(c, theme, ph)).map(|(color, transparency)| Fill::Solid { color, transparency });
|
||||
match e.name.as_str() {
|
||||
"a:solidFill" => solid(Some(e)),
|
||||
"a:pattFill" => solid(e.child("a:fgClr")),
|
||||
"a:gradFill" => {
|
||||
let pct = |v: Option<&str>| v.and_then(int).map_or(0.0, |v| v.clamp(-100_000_000, 100_000_000) as f32 / 1000.0);
|
||||
let rect = |r: Option<&El>| r.map_or([0.0; 4], |r| ["l", "t", "r", "b"].map(|k| pct(r.attr(k))));
|
||||
let stops = e
|
||||
.child("a:gsLst")
|
||||
.into_iter()
|
||||
.flat_map(|l| l.children("a:gs"))
|
||||
.take(MAX_STOPS * 4)
|
||||
.filter_map(|g| color_alpha(g, theme, ph).map(|(color, transparency)| GradStop { pos: pct(g.attr("pos")), color, transparency }))
|
||||
.collect();
|
||||
let on = |v: Option<&str>| v.is_some_and(|v| v == "1" || v == "true");
|
||||
let kind = match (e.child("a:path"), e.child("a:lin")) {
|
||||
(Some(p), _) => {
|
||||
let shape = match p.attr("path") {
|
||||
Some("rect") => PathShape::Rect,
|
||||
Some("shape") => PathShape::Shape,
|
||||
_ => PathShape::Circle,
|
||||
};
|
||||
GradientKind::Path { shape, focus: rect(p.child("a:fillToRect")) }
|
||||
}
|
||||
(None, l) => GradientKind::Linear {
|
||||
angle: l.and_then(|l| l.attr("ang")).and_then(int).map_or(0.0, |a| (a % 21_600_000) as f32 / 60_000.0),
|
||||
scaled: on(l.and_then(|l| l.attr("scaled"))),
|
||||
},
|
||||
};
|
||||
let flip = match e.attr("flip") {
|
||||
Some("x") => GradientFlip::X,
|
||||
Some("y") => GradientFlip::Y,
|
||||
Some("xy") => GradientFlip::Xy,
|
||||
_ => GradientFlip::None,
|
||||
};
|
||||
let tile = e.child("a:tileRect").map(|r| rect(Some(r)));
|
||||
Fill::Gradient(Arc::new(Gradient { stops, kind, tile, rot_with_shape: on(e.attr("rotWithShape")), flip })).sanitized()
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A preset colour (`a:prstClr/@val`, ECMA-376 Part 1 §20.1.10.48): the common ones; others are
|
||||
/// unknown (`None`).
|
||||
pub(crate) fn preset_color(name: &str) -> Option<Rgb> {
|
||||
let lower = name.to_ascii_lowercase();
|
||||
Some(match lower.as_str() {
|
||||
"black" => Rgb(0, 0, 0),
|
||||
"white" => Rgb(0xFF, 0xFF, 0xFF),
|
||||
"red" => Rgb(0xFF, 0, 0),
|
||||
"green" => Rgb(0, 0x80, 0),
|
||||
"lime" => Rgb(0, 0xFF, 0),
|
||||
"blue" => Rgb(0, 0, 0xFF),
|
||||
"yellow" => Rgb(0xFF, 0xFF, 0),
|
||||
"cyan" | "aqua" => Rgb(0, 0xFF, 0xFF),
|
||||
"magenta" | "fuchsia" => Rgb(0xFF, 0, 0xFF),
|
||||
"gray" | "grey" => Rgb(0x80, 0x80, 0x80),
|
||||
"silver" => Rgb(0xC0, 0xC0, 0xC0),
|
||||
"ltgray" | "ltgrey" | "lightgray" | "lightgrey" => Rgb(0xD3, 0xD3, 0xD3),
|
||||
"dkgray" | "dkgrey" | "darkgray" | "darkgrey" => Rgb(0xA9, 0xA9, 0xA9),
|
||||
"maroon" => Rgb(0x80, 0, 0),
|
||||
"navy" => Rgb(0, 0, 0x80),
|
||||
"olive" => Rgb(0x80, 0x80, 0),
|
||||
"purple" => Rgb(0x80, 0, 0x80),
|
||||
"teal" => Rgb(0, 0x80, 0x80),
|
||||
"orange" => Rgb(0xFF, 0xA5, 0),
|
||||
"brown" => Rgb(0xA5, 0x2A, 0x2A),
|
||||
"pink" => Rgb(0xFF, 0xC0, 0xCB),
|
||||
"gold" => Rgb(0xFF, 0xD7, 0),
|
||||
"darkblue" | "dkblue" => Rgb(0, 0, 0x8B),
|
||||
"darkred" | "dkred" => Rgb(0x8B, 0, 0),
|
||||
"darkgreen" | "dkgreen" => Rgb(0, 0x64, 0),
|
||||
"lightblue" | "ltblue" => Rgb(0xAD, 0xD8, 0xE6),
|
||||
"coral" => Rgb(0xFF, 0x7F, 0x50),
|
||||
"salmon" => Rgb(0xFA, 0x80, 0x72),
|
||||
"tomato" => Rgb(0xFF, 0x63, 0x47),
|
||||
"violet" => Rgb(0xEE, 0x82, 0xEE),
|
||||
"indigo" => Rgb(0x4B, 0, 0x82),
|
||||
"beige" => Rgb(0xF5, 0xF5, 0xDC),
|
||||
"ivory" => Rgb(0xFF, 0xFF, 0xF0),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// A system colour without its `lastClr`: the usual light-theme values of the common ones.
|
||||
fn sys_color(name: &str) -> Option<Rgb> {
|
||||
Some(match name {
|
||||
"windowText" | "btnText" | "menuText" | "captionText" | "infoText" => Rgb(0, 0, 0),
|
||||
"window" | "menu" | "highlightText" => Rgb(0xFF, 0xFF, 0xFF),
|
||||
"btnFace" | "3dLight" => Rgb(0xF0, 0xF0, 0xF0),
|
||||
"grayText" | "btnShadow" => Rgb(0xA0, 0xA0, 0xA0),
|
||||
"highlight" => Rgb(0x00, 0x78, 0xD7),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Index into `Document::settings.theme_colors` (dk1, lt1, dk2, lt2, accent1..6, hlink, folHlink).
|
||||
fn scheme_slot(name: &str) -> Option<usize> {
|
||||
Some(match name {
|
||||
"dk1" | "tx1" => 0,
|
||||
"lt1" | "bg1" => 1,
|
||||
"dk2" | "tx2" => 2,
|
||||
"lt2" | "bg2" => 3,
|
||||
"accent1" => 4,
|
||||
"accent2" => 5,
|
||||
"accent3" => 6,
|
||||
"accent4" => 7,
|
||||
"accent5" => 8,
|
||||
"accent6" => 9,
|
||||
"hlink" => 10,
|
||||
"folHlink" => 11,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Theme colour `name` (`accent1`, `tx1`…) of `theme`.
|
||||
pub(crate) fn scheme(name: &str, theme: &[Rgb]) -> Option<Rgb> {
|
||||
scheme_slot(name).map(|i| slot(theme, i))
|
||||
}
|
||||
|
||||
/// `c` with its luminance changed by `f`.
|
||||
pub(crate) fn lum(c: Rgb, f: impl Fn(f64) -> f64) -> Rgb {
|
||||
adjust_lum(c, f)
|
||||
}
|
||||
|
||||
/// `c` with the DrawingML colour modifier `name` at `v` (see [`Rgb::modified`]).
|
||||
fn modified(c: Rgb, name: &str, v: f64) -> Rgb {
|
||||
c.modified(name, v)
|
||||
}
|
||||
|
||||
/// `c` with its HSL luminance changed by `f`.
|
||||
pub(crate) fn adjust_lum(c: Rgb, f: impl Fn(f64) -> f64) -> Rgb {
|
||||
c.with_lum(f)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use wordcraft_doc::THEME_COLORS;
|
||||
|
||||
use super::*;
|
||||
|
||||
const NS: &str = r#"xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main""#;
|
||||
|
||||
/// The element `name` with `attrs` and `body`, parsed.
|
||||
fn el(name: &str, attrs: &str, body: &str) -> El {
|
||||
crate::xml::parse(format!(r#"<{name} {NS} {attrs}>{body}</{name}>"#).as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gradient_and_pattern_fills_take_their_first_colour() {
|
||||
let theme = THEME_COLORS.to_vec();
|
||||
let grad = el("a:gradFill", "", r#"<a:gsLst><a:gs pos="0"><a:srgbClr val="FF0000"/></a:gs></a:gsLst>"#);
|
||||
assert_eq!(paint(&grad, &theme), Some(Rgb(255, 0, 0)));
|
||||
let patt =
|
||||
el("a:pattFill", r#"prst="dkDnDiag""#, r#"<a:fgClr><a:srgbClr val="00FF00"/></a:fgClr><a:bgClr><a:srgbClr val="0000FF"/></a:bgClr>"#);
|
||||
assert_eq!(paint(&patt, &theme), Some(Rgb(0, 255, 0)));
|
||||
let ln = el("a:ln", "", r#"<a:gradFill><a:gsLst><a:gs pos="0"><a:srgbClr val="FF0000"/></a:gs></a:gsLst></a:gradFill>"#);
|
||||
assert_eq!(fill_colour(&ln, &theme), Some(Rgb(255, 0, 0)));
|
||||
assert_eq!(fill_colour(&el("a:ln", "", "<a:noFill/>"), &theme), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tint_and_shade_move_towards_white_and_black() {
|
||||
let theme = THEME_COLORS.to_vec();
|
||||
let near = |c: Option<Rgb>| c.is_some_and(|Rgb(r, g, b)| [r, g, b].iter().all(|x| x.abs_diff(128) <= 1));
|
||||
assert!(near(color(&el("a:srgbClr", r#"val="000000""#, r#"<a:tint val="50000"/>"#), &theme)));
|
||||
assert!(near(color(&el("a:srgbClr", r#"val="FFFFFF""#, r#"<a:shade val="50000"/>"#), &theme)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modifiers_apply_in_the_order_written() {
|
||||
let theme = THEME_COLORS.to_vec();
|
||||
let a = el("a:srgbClr", r#"val="808080""#, r#"<a:lumMod val="50000"/><a:lumOff val="50000"/>"#);
|
||||
let b = el("a:srgbClr", r#"val="808080""#, r#"<a:lumOff val="50000"/><a:lumMod val="50000"/>"#);
|
||||
assert_ne!(color(&a, &theme), color(&b, &theme));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preset_colors_are_case_insensitive_and_include_common_names() {
|
||||
assert_eq!(preset_color("red"), Some(Rgb(255, 0, 0)));
|
||||
assert_eq!(preset_color("Red"), Some(Rgb(255, 0, 0)));
|
||||
assert_eq!(preset_color("darkBlue"), Some(Rgb(0, 0, 0x8B)));
|
||||
assert_eq!(preset_color("dkBlue"), Some(Rgb(0, 0, 0x8B)));
|
||||
assert_eq!(preset_color("lightGray"), Some(Rgb(0xD3, 0xD3, 0xD3)));
|
||||
assert_eq!(preset_color("ltGray"), Some(Rgb(0xD3, 0xD3, 0xD3)));
|
||||
assert_eq!(preset_color("gold"), Some(Rgb(0xFF, 0xD7, 0)));
|
||||
assert_eq!(preset_color("unknownColor"), None);
|
||||
}
|
||||
}
|
||||
@@ -1,414 +0,0 @@
|
||||
//! Objects kept for writing back: charts, SmartArt diagrams and OLE objects.
|
||||
//!
|
||||
//! WordCraft draws charts and diagrams and shows an OLE object's picture, but doesn't edit them.
|
||||
//! So that saving doesn't lose them, the reader keeps each one's own markup (an `a:graphic` or a
|
||||
//! `w:object`) as a [`wordcraft_doc::graphic::Embedded`] on the object, and every part that markup
|
||||
//! reaches through relationships (chart, embedded workbook, colours and style; diagram data, layout,
|
||||
//! quick style, colours and drawing; OLE embedding and its picture…) as opaque bytes in
|
||||
//! `Document::passthrough`, with their content types and relationships in an
|
||||
//! [`EmbeddedManifest`](crate::package::EmbeddedManifest). The font table and the fonts it embeds
|
||||
//! are kept the same way.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use wordcraft_doc::graphic::{Embedded, EmbeddedRef};
|
||||
|
||||
use super::Reader;
|
||||
use crate::package::{
|
||||
CUSTOM_XML_ROOTS, ContentTypes, MAX_EMBEDDED_PARTS, MAX_EMBEDDED_RELS, MAX_EMBEDDED_XML, Rel, Rels, manifest_field_ok, rel_is, rt,
|
||||
};
|
||||
use crate::xml::{El, MAX_DEPTH, NAMESPACES, esc, is_rel_attr};
|
||||
|
||||
/// Relationship types (last path segment) the reader models or the writer produces itself, so
|
||||
/// they are never carried verbatim: carrying them would duplicate or contradict the written part.
|
||||
/// Types the main document refers to by id (headers, pictures, objects…) are listed too.
|
||||
pub const HANDLED_RELS: &[&str] = &[
|
||||
"officeDocument",
|
||||
"core-properties",
|
||||
"extended-properties",
|
||||
"custom-properties",
|
||||
"styles",
|
||||
"numbering",
|
||||
"settings",
|
||||
"theme",
|
||||
"fontTable",
|
||||
"footnotes",
|
||||
"endnotes",
|
||||
"comments",
|
||||
"commentsExtended",
|
||||
"commentsIds",
|
||||
"commentsExtensible",
|
||||
"customXml",
|
||||
"vbaProject",
|
||||
"header",
|
||||
"footer",
|
||||
"image",
|
||||
"hyperlink",
|
||||
"oleObject",
|
||||
"package",
|
||||
"chart",
|
||||
"chartEx",
|
||||
"diagramData",
|
||||
"diagramDrawing",
|
||||
"diagramLayout",
|
||||
"diagramQuickStyle",
|
||||
"diagramColors",
|
||||
"video",
|
||||
"audio",
|
||||
"media",
|
||||
"control",
|
||||
"subDocument",
|
||||
"ink",
|
||||
];
|
||||
|
||||
/// Most relationship ids one object's markup may use.
|
||||
const MAX_IDS: usize = 64;
|
||||
|
||||
/// The relationship ids the main document's markup refers to: the ids it names in a
|
||||
/// relationship attribute (`r:id="rId5"`, VML's `o:relid`) — the same rule the embedded objects
|
||||
/// use ([`rel_ids`]).
|
||||
///
|
||||
/// One pass over the markup the reader has already parsed, not one search per id: the ids are
|
||||
/// looked up as they are met, so the cost is the size of the document rather than its size
|
||||
/// times the number of relationships (#12). A name that is not a relationship is ignored, and a
|
||||
/// name that is not one of the document's relationship ids is not collected. An id that only
|
||||
/// appears as text is not a reference: the text does not name a part, and treating it as one
|
||||
/// lost the part on save.
|
||||
fn referenced_ids<'a>(root: &El, document: &'a Rels) -> HashSet<&'a [u8]> {
|
||||
// The ids the loop in `keep_unmodelled` can ask about: the first `MAX_EMBEDDED_RELS`
|
||||
// non-external relationships, and no more, so the set stays bounded however many the file
|
||||
// carries.
|
||||
let ids: HashSet<&[u8]> = document.list.iter().filter(|r| !r.external).take(MAX_EMBEDDED_RELS).map(|r| r.id.as_bytes()).collect();
|
||||
let mut found = HashSet::new();
|
||||
if ids.is_empty() {
|
||||
return found;
|
||||
}
|
||||
collect_rel_ids(root, &ids, &mut found, 0);
|
||||
found
|
||||
}
|
||||
|
||||
/// The ids `e` names in a relationship attribute that are in `ids`, collected into `found`.
|
||||
///
|
||||
/// The attribute is a relationship attribute by [`is_rel_attr`]: one in the relationship
|
||||
/// namespace, VML's `o:relid`, or the unqualified alias of one on an element that carries a
|
||||
/// relationship — the same attribute the reader resolves a relationship from, so a part the
|
||||
/// markup names is not carried over a second time.
|
||||
fn collect_rel_ids<'a>(e: &El, ids: &HashSet<&'a [u8]>, found: &mut HashSet<&'a [u8]>, depth: usize) {
|
||||
if depth > MAX_DEPTH {
|
||||
return;
|
||||
}
|
||||
for (k, v) in &e.attrs {
|
||||
if is_rel_attr(&e.name, k)
|
||||
&& let Some(id) = ids.get(v.as_bytes())
|
||||
{
|
||||
found.insert(*id);
|
||||
}
|
||||
}
|
||||
for c in e.els() {
|
||||
collect_rel_ids(c, ids, found, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
/// Keep custom XML stores reached from the main document or package, without evaluating
|
||||
/// bindings. A bibliography already loaded into the source model belongs to its writer.
|
||||
pub(super) fn keep_custom_xml(&mut self, package: &Rels, document: &Rels) {
|
||||
let mut roots = String::new();
|
||||
for (owner, rels) in [("P", package), ("D", document)] {
|
||||
for rel in rels.list.iter().filter(|r| !r.external && rel_is(&r.kind, rt::CUSTOM_XML)).take(MAX_EMBEDDED_RELS) {
|
||||
if ![&rel.kind, &rel.target].iter().all(|s| manifest_field_ok(s)) {
|
||||
continue;
|
||||
}
|
||||
if !self.doc.sources.is_empty() && self.xml(&rel.target).ok().flatten().is_some_and(|r| r.local() == "Sources") {
|
||||
continue;
|
||||
}
|
||||
// Native ink shares the custom XML relationship type, but its part belongs to the
|
||||
// drawing that refers to it: kept with it, or dropped with it (no orphan part).
|
||||
let pkg = self.pkg;
|
||||
if self.content_types.get_or_insert_with(|| ContentTypes::read(pkg)).of(&rel.target) == Some("application/inkml+xml") {
|
||||
continue;
|
||||
}
|
||||
if self.keep_part_tree(&rel.target) {
|
||||
roots.push_str(&format!("{owner}\t{}\t{}\n", rel.kind, rel.target));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !roots.is_empty() {
|
||||
self.doc.passthrough.insert(CUSTOM_XML_ROOTS.into(), Arc::new(roots.into_bytes()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep the parts the package and the main document relate to that WordCraft neither models
|
||||
/// nor writes itself (web settings, styles-with-effects, add-in task panes, the thumbnail, a
|
||||
/// glossary…), with every part they reach, so saving carries them unchanged. Skipped: the
|
||||
/// relationship types the writer produces or the reader models ([`HANDLED_RELS`]) and any
|
||||
/// relationship the main document's markup refers to by id (its target goes with the
|
||||
/// element, or not at all).
|
||||
pub(super) fn keep_unmodelled(&mut self, root: &El, package: &Rels, document: &Rels) {
|
||||
// The ids the main document's markup refers to, in one pass over the markup: an id is
|
||||
// named in a relationship attribute (`r:id="rId5"`). One scan for all of them, not one
|
||||
// search per relationship — a large document with many relationships took minutes to
|
||||
// open (#12).
|
||||
let referenced = referenced_ids(root, document);
|
||||
let mut roots = String::new();
|
||||
// One glossary per main document (its first relationship; see `read_glossary`).
|
||||
let mut glossary = false;
|
||||
for (owner, rels) in [("P", package), ("D", document)] {
|
||||
for rel in rels.list.iter().filter(|r| !r.external).take(MAX_EMBEDDED_RELS) {
|
||||
let tail = rel.kind.rsplit('/').next().unwrap_or("");
|
||||
if HANDLED_RELS.contains(&tail)
|
||||
|| (owner == "D" && referenced.contains(rel.id.as_bytes()))
|
||||
|| (owner == "D" && tail == "glossaryDocument" && std::mem::replace(&mut glossary, true))
|
||||
|| ![&rel.kind, &rel.target].iter().all(|s| manifest_field_ok(s))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if self.keep_part_tree(&rel.target) {
|
||||
roots.push_str(&format!("{owner}\t{}\t{}\n", rel.kind, rel.target));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !roots.is_empty() {
|
||||
self.doc.passthrough.insert(crate::package::UNMODELLED_ROOTS.into(), Arc::new(roots.into_bytes()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep `el` (an object's `a:graphic` or `w:object`, read with `rels`) and the parts it refers
|
||||
/// to, so the object can be written back. `extra` names relationships the object's parts use
|
||||
/// by id although its markup doesn't (a SmartArt drawing, named in the diagram's data part).
|
||||
/// `None` when it can't be written back faithfully: a relationship it uses is missing, a part
|
||||
/// is missing, or it's too big.
|
||||
pub(super) fn keep_embedded(&mut self, el: &El, rels: &Rels, extra: &[String]) -> Option<Arc<Embedded>> {
|
||||
let mut ids = Vec::new();
|
||||
rel_ids(el, &mut ids, 0)?;
|
||||
let mut refs = Vec::new();
|
||||
for id in &ids {
|
||||
let rel = rels.by_id(id)?;
|
||||
if !rel.external && !self.keep_part_tree(&rel.target) {
|
||||
return None;
|
||||
}
|
||||
refs.push(EmbeddedRef { id: id.clone(), kind: rel.kind.clone(), target: rel.target.clone(), external: rel.external });
|
||||
}
|
||||
for id in extra.iter().filter(|id| !ids.contains(id)) {
|
||||
if let Some(rel) = rels.by_id(id).filter(|r| !r.external)
|
||||
&& self.keep_part_tree(&rel.target)
|
||||
{
|
||||
refs.push(EmbeddedRef { id: id.clone(), kind: rel.kind.clone(), target: rel.target.clone(), external: false });
|
||||
}
|
||||
}
|
||||
let xml = standalone_xml(el)?;
|
||||
Some(Arc::new(Embedded { xml, refs }))
|
||||
}
|
||||
|
||||
/// Keep part `root` and every part it reaches through its relationships. False when `root`
|
||||
/// itself can't be kept.
|
||||
pub(super) fn keep_part_tree(&mut self, root: &str) -> bool {
|
||||
if self.embedded.parts.contains_key(root) {
|
||||
return true;
|
||||
}
|
||||
if !self.keep_part(root) {
|
||||
return false;
|
||||
}
|
||||
let mut todo = vec![root.to_string()];
|
||||
while let Some(part) = todo.pop() {
|
||||
let mut kept: Vec<Rel> = Vec::new();
|
||||
for r in self.pkg.rels(&part).list {
|
||||
if kept.len() >= MAX_EMBEDDED_RELS {
|
||||
log::warn!("docx: part {part} has more than {MAX_EMBEDDED_RELS} relationships; the rest are dropped");
|
||||
break;
|
||||
}
|
||||
if ![&r.id, &r.kind, &r.target].iter().all(|f| manifest_field_ok(f)) || kept.iter().any(|k| k.id == r.id) {
|
||||
continue;
|
||||
}
|
||||
if !r.external && !self.embedded.parts.contains_key(&r.target) {
|
||||
if !self.keep_part(&r.target) {
|
||||
continue;
|
||||
}
|
||||
todo.push(r.target.clone());
|
||||
}
|
||||
kept.push(r);
|
||||
}
|
||||
if let Some(p) = self.embedded.parts.get_mut(&part) {
|
||||
p.rels = kept;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Keep one part's bytes and content type (not its relationships).
|
||||
fn keep_part(&mut self, part: &str) -> bool {
|
||||
let lower = part.to_ascii_lowercase();
|
||||
if !manifest_field_ok(part) || lower.ends_with(".rels") || lower == "[content_types].xml" || part.eq_ignore_ascii_case(&self.main) {
|
||||
log::warn!("docx: not carrying part {part:?} for an embedded object");
|
||||
return false;
|
||||
}
|
||||
if self.embedded.parts.len() >= MAX_EMBEDDED_PARTS {
|
||||
log::warn!("docx: more than {MAX_EMBEDDED_PARTS} parts for embedded objects; the rest are dropped");
|
||||
return false;
|
||||
}
|
||||
let Some(bytes) = self.pkg.get_shared(part) else {
|
||||
log::warn!("docx: part {part} of an embedded object is missing");
|
||||
return false;
|
||||
};
|
||||
let pkg = self.pkg;
|
||||
let ct = self.content_types.get_or_insert_with(|| ContentTypes::read(pkg)).of(part).unwrap_or("application/octet-stream");
|
||||
let content_type = if manifest_field_ok(ct) { ct.to_string() } else { "application/octet-stream".to_string() };
|
||||
self.doc.passthrough.entry(part.to_string()).or_insert_with(|| bytes.clone());
|
||||
self.embedded.parts.insert(part.to_string(), crate::package::EmbeddedPart { content_type, rels: Vec::new() });
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// The relationship ids `e` uses (attributes in the relationships namespace, VML's `o:relid`,
|
||||
/// and the unqualified alias of one on an element that carries a relationship), in document
|
||||
/// order. `None` past [`MAX_IDS`].
|
||||
fn rel_ids(e: &El, out: &mut Vec<String>, depth: usize) -> Option<()> {
|
||||
if depth > MAX_DEPTH {
|
||||
return Some(());
|
||||
}
|
||||
for (k, v) in &e.attrs {
|
||||
if is_rel_attr(&e.name, k) && !out.contains(v) {
|
||||
if out.len() >= MAX_IDS {
|
||||
return None;
|
||||
}
|
||||
out.push(v.clone());
|
||||
}
|
||||
}
|
||||
for c in e.els() {
|
||||
rel_ids(c, out, depth + 1)?;
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// `e` as a standalone element: its XML with the namespace declarations its canonical prefixes
|
||||
/// need. `None` when too long, or in a namespace we can't name.
|
||||
fn standalone_xml(e: &El) -> Option<String> {
|
||||
let xml = e.to_xml();
|
||||
let rest = xml.get(1 + e.name.len()..).filter(|_| !xml.is_empty())?;
|
||||
let mut s = format!("<{}", e.name);
|
||||
for (p, u) in NAMESPACES {
|
||||
s.push_str(&format!(" xmlns:{p}=\"{}\"", esc(u)));
|
||||
}
|
||||
s.push_str(rest);
|
||||
(s.len() <= MAX_EMBEDDED_XML).then_some(s)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Write;
|
||||
|
||||
use crate::package::UNMODELLED_ROOTS;
|
||||
|
||||
const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#;
|
||||
const ROOT_RELS: &str = r#"<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>"#;
|
||||
const CONTENT_TYPES: &str = r#"<?xml version="1.0" encoding="UTF-8"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Default Extension="bin" ContentType="application/octet-stream"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>"#;
|
||||
|
||||
/// A package whose main part holds `body` (the XML inside `w:body`), and which relates to
|
||||
/// `rels` parts of a type the reader doesn't model, so each one is looked for in the part.
|
||||
fn package(body: &str, rels: &[(&str, &str)], parts: &[(&str, &[u8])]) -> Vec<u8> {
|
||||
let doc = format!(r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document {W_NS}><w:body>{body}</w:body></w:document>"#);
|
||||
let mut r = String::from(r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">"#);
|
||||
for (id, target) in rels {
|
||||
r.push_str(&format!(
|
||||
r#"<Relationship Id="{id}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings" Target="{target}"/>"#
|
||||
));
|
||||
}
|
||||
r.push_str("</Relationships>");
|
||||
|
||||
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let opts = zip::write::SimpleFileOptions::default();
|
||||
zw.start_file("[Content_Types].xml", opts).unwrap();
|
||||
zw.write_all(CONTENT_TYPES.as_bytes()).unwrap();
|
||||
zw.start_file("_rels/.rels", opts).unwrap();
|
||||
zw.write_all(ROOT_RELS.as_bytes()).unwrap();
|
||||
zw.start_file("word/document.xml", opts).unwrap();
|
||||
zw.write_all(doc.as_bytes()).unwrap();
|
||||
zw.start_file("word/_rels/document.xml.rels", opts).unwrap();
|
||||
zw.write_all(r.as_bytes()).unwrap();
|
||||
for (name, bytes) in parts {
|
||||
zw.start_file(name, opts).unwrap();
|
||||
zw.write_all(bytes).unwrap();
|
||||
}
|
||||
zw.finish().unwrap().into_inner()
|
||||
}
|
||||
|
||||
/// The parts the reader carries over unchanged, as one string.
|
||||
fn unmodelled_roots(d: &wordcraft_doc::Document) -> String {
|
||||
d.passthrough.get(UNMODELLED_ROOTS).map(|v| String::from_utf8_lossy(v).to_string()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Opening a document costs the size of its main part, not that size times the number of its
|
||||
/// relationships (#12). Eight megabytes of main part with a thousand relationships used to
|
||||
/// take half a minute; the scan is now one pass over the markup.
|
||||
#[test]
|
||||
fn a_document_with_many_relationships_opens_in_the_time_its_part_takes() {
|
||||
let rels: Vec<(String, String)> = (0..1024).map(|i| (format!("rIdT{i}"), format!("file{i}.bin"))).collect();
|
||||
let rels: Vec<(&str, &str)> = rels.iter().map(|(a, b)| (a.as_str(), b.as_str())).collect();
|
||||
let body = format!(r#"<w:p><w:r><w:t xml:space="preserve">{}</w:t></w:r></w:p>"#, "a".repeat(8 << 20));
|
||||
let bytes = package(&body, &rels, &[]);
|
||||
let t = std::time::Instant::now();
|
||||
let doc = crate::read(&bytes).unwrap();
|
||||
let took = t.elapsed();
|
||||
assert_eq!(doc.plain_text(wordcraft_doc::StoryRef::Body).len(), 8 << 20, "the text is there");
|
||||
assert!(took.as_secs() < 15, "opening it took {took:?}");
|
||||
}
|
||||
|
||||
/// The scan finds the relationships the markup names, so a part the document already refers
|
||||
/// to is not carried over a second time (#12).
|
||||
#[test]
|
||||
fn a_relationship_the_markup_names_is_not_carried_over_again() {
|
||||
// The markup names the relationship, so it is not carried over a second time as an
|
||||
// unmodelled part. (The part itself survives only if the writer writes it with the
|
||||
// element that names it; the reader does not carry it on its own.)
|
||||
let named = package(
|
||||
r#"<w:p><w:r><w:t>before</w:t></w:r></w:p><w:sectPr><w:printerSettings r:id="rIdDoc1"/></w:sectPr>"#,
|
||||
&[("rIdDoc1", "keep.bin")],
|
||||
&[("word/keep.bin", b"kept")],
|
||||
);
|
||||
let roots = unmodelled_roots(&crate::read(&named).unwrap());
|
||||
assert!(!roots.contains("keep.bin"), "the part is carried twice: {roots}");
|
||||
|
||||
// A part the markup doesn't name is carried over, as before.
|
||||
let unnamed = package(r#"<w:p><w:r><w:t>no mention here</w:t></w:r></w:p>"#, &[("rIdDoc1", "keep.bin")], &[("word/keep.bin", b"kept")]);
|
||||
let roots = unmodelled_roots(&crate::read(&unnamed).unwrap());
|
||||
assert!(roots.contains("keep.bin"), "the part is dropped: {roots}");
|
||||
}
|
||||
|
||||
/// An id the markup only mentions as text does not name a part: the part is carried over,
|
||||
/// not dropped because the text looked like a reference.
|
||||
#[test]
|
||||
fn a_part_the_markup_only_mentions_is_carried_over() {
|
||||
let mentioned = package(
|
||||
r#"<w:p><w:r><w:t xml:space="preserve">before "rIdDoc1" after</w:t></w:r></w:p>"#,
|
||||
&[("rIdDoc1", "keep.bin")],
|
||||
&[("word/keep.bin", b"kept")],
|
||||
);
|
||||
let roots = unmodelled_roots(&crate::read(&mentioned).unwrap());
|
||||
assert!(roots.contains("keep.bin"), "the part is dropped: {roots}");
|
||||
}
|
||||
|
||||
/// An unqualified attribute is a relationship only where the markup names a relationship
|
||||
/// that way: a hyperlink's `id` names the part, a VML shape's `id` is its own.
|
||||
#[test]
|
||||
fn an_unqualified_attribute_names_a_relationship_only_where_it_is_one() {
|
||||
// A hyperlink whose relationship id is written without the prefix: the reader resolves
|
||||
// it (the alias `El::attr` accepts), so the part it names is not carried over twice.
|
||||
let named = package(
|
||||
r#"<w:p><w:hyperlink id="rIdDoc1"><w:r><w:t>link</w:t></w:r></w:hyperlink></w:p>"#,
|
||||
&[("rIdDoc1", "keep.bin")],
|
||||
&[("word/keep.bin", b"kept")],
|
||||
);
|
||||
let roots = unmodelled_roots(&crate::read(&named).unwrap());
|
||||
assert!(!roots.contains("keep.bin"), "the part is carried twice: {roots}");
|
||||
|
||||
// A VML shape's `id` is its own: it names no part, so the part is carried over.
|
||||
let own = package(
|
||||
r#"<w:p><w:r><w:pict><v:shape id="rIdDoc1" style="width:10pt;height:10pt"/></w:pict></w:r></w:p>"#,
|
||||
&[("rIdDoc1", "keep.bin")],
|
||||
&[("word/keep.bin", b"kept")],
|
||||
);
|
||||
let roots = unmodelled_roots(&crate::read(&own).unwrap());
|
||||
assert!(roots.contains("keep.bin"), "the part is dropped: {roots}");
|
||||
}
|
||||
}
|
||||
@@ -1,392 +0,0 @@
|
||||
//! Freeform shapes (DrawingML custom geometry, `a:custGeom`; legacy VML paths) and ink strokes
|
||||
//! drawn with them.
|
||||
|
||||
use wordcraft_doc::freeform::{FreePath, Freeform, InkTool, MAX_PATHS, MAX_POINTS};
|
||||
|
||||
use crate::units::int;
|
||||
use crate::xml::El;
|
||||
|
||||
/// Straight pieces a Bézier curve is flattened into.
|
||||
const CURVE_STEPS: usize = 8;
|
||||
/// Points read from one path segment. A conformant `a:lnTo`/`a:moveTo` has one, `a:quadBezTo`
|
||||
/// two and `a:cubicBezTo` three; the cap bounds the de Casteljau control polygon so a segment
|
||||
/// stuffed with points can't make curve flattening quadratic in the point count.
|
||||
const MAX_SEG_POINTS: usize = 8;
|
||||
/// Largest path coordinate kept, in EMUs.
|
||||
const MAX_EMU: f64 = 1.0e12;
|
||||
|
||||
/// The paths of the custom geometry in `sppr` (`wps:spPr`), mapped onto a `w` × `h` shape
|
||||
/// (points): each `a:path` in its own `w` × `h` space (EMUs of the shape's size when it gives
|
||||
/// none). Lines and Bézier curves (flattened) are kept; arcs are drawn straight to their end.
|
||||
/// `None` when there is no `a:custGeom` or it has no points.
|
||||
pub fn cust_geom(sppr: &El, w: f32, h: f32) -> Option<Freeform> {
|
||||
let list = sppr.child("a:custGeom")?.child("a:pathLst")?;
|
||||
let mut paths = Vec::new();
|
||||
for p in list.children("a:path").take(MAX_PATHS) {
|
||||
// Subpaths count too: stop once the shape has as many paths as it may keep.
|
||||
if paths.len() >= MAX_PATHS {
|
||||
break;
|
||||
}
|
||||
let dim = |n: &str, shape: f32| p.attr(n).and_then(int).filter(|v| *v > 0).map_or(shape as f64 * 12_700.0, |v| v as f64);
|
||||
let (pw, ph) = (dim("w", w), dim("h", h));
|
||||
let scale = |[x, y]: [f64; 2]| -> [f32; 2] {
|
||||
let sx = if pw > 0.0 { w as f64 / pw } else { 1.0 / 12_700.0 };
|
||||
let sy = if ph > 0.0 { h as f64 / ph } else { 1.0 / 12_700.0 };
|
||||
[(x * sx) as f32, (y * sy) as f32]
|
||||
};
|
||||
let pt = |e: &El| -> Option<[f64; 2]> {
|
||||
let c = |n: &str| e.attr(n).and_then(int).map(|v| (v as f64).clamp(-MAX_EMU, MAX_EMU));
|
||||
Some([c("x")?, c("y")?])
|
||||
};
|
||||
// Each subpath (from an `a:moveTo`) becomes a path of its own.
|
||||
let mut cur: Vec<[f64; 2]> = Vec::new();
|
||||
let mut done: Vec<(Vec<[f64; 2]>, bool)> = Vec::new();
|
||||
let fill_none = p.attr("fill") == Some("none");
|
||||
for seg in p.els() {
|
||||
if cur.len() >= MAX_POINTS || done.len() >= MAX_PATHS {
|
||||
break;
|
||||
}
|
||||
let pts: Vec<[f64; 2]> = seg.children("a:pt").filter_map(pt).take(MAX_SEG_POINTS).collect();
|
||||
match seg.local() {
|
||||
"moveTo" => {
|
||||
if !cur.is_empty() {
|
||||
done.push((std::mem::take(&mut cur), false));
|
||||
}
|
||||
cur.extend(pts.first());
|
||||
}
|
||||
"lnTo" => cur.extend(pts.first()),
|
||||
"cubicBezTo" | "quadBezTo" => {
|
||||
let Some(&start) = cur.last() else {
|
||||
cur.extend(pts.last());
|
||||
continue;
|
||||
};
|
||||
let ctrl: Vec<[f64; 2]> = std::iter::once(start).chain(pts.iter().copied()).collect();
|
||||
for i in 1..=CURVE_STEPS {
|
||||
cur.push(bezier(&ctrl, i as f64 / CURVE_STEPS as f64));
|
||||
}
|
||||
}
|
||||
"close" if !cur.is_empty() => done.push((std::mem::take(&mut cur), true)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if !cur.is_empty() {
|
||||
// A path drawn back to where it started is closed, though it has no `a:close` (#743:
|
||||
// Word's cover pages), so it's filled.
|
||||
let back = cur.len() > 2 && cur.first() == cur.last();
|
||||
done.push((cur, back));
|
||||
}
|
||||
for (pts, closed) in done.into_iter().take(MAX_PATHS.saturating_sub(paths.len())) {
|
||||
paths.push(FreePath { pts: pts.into_iter().take(MAX_POINTS).map(scale).collect(), closed: closed && !fill_none });
|
||||
}
|
||||
}
|
||||
let f = Freeform { w, h, paths, alpha: 1.0, ink: None, order: None }.sanitized();
|
||||
(!f.paths.is_empty()).then_some(f)
|
||||
}
|
||||
|
||||
/// Largest VML path coordinate kept (coordinate-space units).
|
||||
const MAX_VML: f64 = 1.0e12;
|
||||
/// Characters of a VML `path` attribute read; the rest is ignored.
|
||||
const MAX_VML_PATH: usize = 1 << 20;
|
||||
/// Parameters of a VML path kept, all commands together.
|
||||
const MAX_VML_PARAMS: usize = MAX_POINTS * 8;
|
||||
|
||||
/// A legacy VML freeform (`v:shape` with a `path`, ECMA-376 Part 4 §14.1.2.19).
|
||||
pub struct VmlPath {
|
||||
pub freeform: Freeform,
|
||||
/// `nf` in the path: no fill.
|
||||
pub no_fill: bool,
|
||||
/// `ns` in the path: no outline.
|
||||
pub no_stroke: bool,
|
||||
}
|
||||
|
||||
/// The VML path commands that are two letters long.
|
||||
const VML_TWO: [&str; 11] = ["nf", "ns", "ae", "al", "at", "ar", "wa", "wr", "qx", "qy", "qb"];
|
||||
|
||||
/// The VML `path` of a shape, mapped onto its `w` × `h` frame (points) through its `coordsize`
|
||||
/// (default `1000,1000`) and `coordorigin` (default `0,0`). Commands: `m`/`t` move (absolute /
|
||||
/// relative), `l`/`r` line, `c`/`v` cubic Bézier (flattened), `x` close, `e` end, `nf`/`ns` no
|
||||
/// fill / no outline; arcs (`at`, `ar`, `wa`, `wr`) and quadrants (`qx`, `qy`) are drawn straight
|
||||
/// to their end, `qb` straight to its last point, and ellipses (`ae`, `al`) are skipped. An empty
|
||||
/// parameter is 0. `None` when the path has no points, or uses formulas (`@n`, from `v:formulas`,
|
||||
/// which aren't evaluated): such a shape isn't drawn rather than drawn wrong.
|
||||
pub fn vml_path(path: &str, coordsize: Option<&str>, coordorigin: Option<&str>, w: f32, h: f32) -> Option<VmlPath> {
|
||||
if path.contains('@') {
|
||||
return None;
|
||||
}
|
||||
let pair = |v: Option<&str>| -> Option<(f64, f64)> {
|
||||
let v = v?;
|
||||
let (a, b) = v.split_once(',').unwrap_or((v, ""));
|
||||
let n = |s: &str| int(s).map(|i| (i as f64).clamp(-MAX_VML, MAX_VML));
|
||||
Some((n(a)?, n(b).unwrap_or(0.0)))
|
||||
};
|
||||
let size = |v: f64| if v > 0.0 { v } else { 1000.0 };
|
||||
let (cw, ch) = pair(coordsize).map_or((1000.0, 1000.0), |(a, b)| (size(a), size(b)));
|
||||
let (ox, oy) = pair(coordorigin).unwrap_or((0.0, 0.0));
|
||||
let (sx, sy) = (w as f64 / cw, h as f64 / ch);
|
||||
let scale = |[x, y]: [f64; 2]| -> [f32; 2] { [((x - ox) * sx) as f32, ((y - oy) * sy) as f32] };
|
||||
|
||||
let mut d = Draw::default();
|
||||
let (mut no_fill, mut no_stroke) = (false, false);
|
||||
for (cmd, params) in vml_commands(path) {
|
||||
if d.full() {
|
||||
break;
|
||||
}
|
||||
let pts: Vec<[f64; 2]> = params.as_chunks::<2>().0.to_vec();
|
||||
match cmd.as_str() {
|
||||
"m" | "t" => {
|
||||
for (i, &p) in pts.iter().enumerate() {
|
||||
let p = if cmd == "t" { d.rel(p) } else { p };
|
||||
if i == 0 {
|
||||
d.move_to(p);
|
||||
} else {
|
||||
d.line_to(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
"l" | "r" | "qx" | "qy" => {
|
||||
for &p in &pts {
|
||||
let p = if cmd == "r" { d.rel(p) } else { p };
|
||||
d.line_to(p);
|
||||
}
|
||||
}
|
||||
"c" | "v" => {
|
||||
for c in pts.as_chunks::<3>().0 {
|
||||
let start = d.at;
|
||||
let ctrl: Vec<[f64; 2]> = std::iter::once(start).chain(c.iter().map(|&p| if cmd == "v" { d.rel(p) } else { p })).collect();
|
||||
for i in 1..=CURVE_STEPS {
|
||||
d.line_to(bezier(&ctrl, i as f64 / CURVE_STEPS as f64));
|
||||
}
|
||||
}
|
||||
}
|
||||
"qb" => {
|
||||
if let Some(&p) = pts.last() {
|
||||
d.line_to(p);
|
||||
}
|
||||
}
|
||||
// Bounding box, then start and end: `at`/`wa` draw a line to the start, `ar`/`wr` move there.
|
||||
"at" | "ar" | "wa" | "wr" => {
|
||||
for [_, _, s, e] in pts.as_chunks::<4>().0 {
|
||||
if matches!(cmd.as_str(), "ar" | "wr") {
|
||||
d.move_to(*s);
|
||||
} else {
|
||||
d.line_to(*s);
|
||||
}
|
||||
d.line_to(*e);
|
||||
}
|
||||
}
|
||||
"x" => d.end(true),
|
||||
"e" => d.end(false),
|
||||
"nf" => no_fill = true,
|
||||
"ns" => no_stroke = true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
d.end(false);
|
||||
let paths = d.done.into_iter().map(|(pts, closed)| FreePath { pts: pts.into_iter().map(scale).collect(), closed }).collect();
|
||||
let freeform = Freeform { w, h, paths, alpha: 1.0, ink: None, order: None }.sanitized();
|
||||
(!freeform.paths.is_empty()).then_some(VmlPath { freeform, no_fill, no_stroke })
|
||||
}
|
||||
|
||||
/// The pen while a VML path is drawn: where it is, where the subpath began, the subpath being
|
||||
/// drawn and those done (closed or not).
|
||||
#[derive(Default)]
|
||||
struct Draw {
|
||||
at: [f64; 2],
|
||||
start: [f64; 2],
|
||||
cur: Vec<[f64; 2]>,
|
||||
done: Vec<(Vec<[f64; 2]>, bool)>,
|
||||
}
|
||||
|
||||
impl Draw {
|
||||
fn full(&self) -> bool {
|
||||
self.done.len() >= MAX_PATHS
|
||||
}
|
||||
|
||||
fn rel(&self, [x, y]: [f64; 2]) -> [f64; 2] {
|
||||
[(self.at[0] + x).clamp(-MAX_VML, MAX_VML), (self.at[1] + y).clamp(-MAX_VML, MAX_VML)]
|
||||
}
|
||||
|
||||
fn move_to(&mut self, p: [f64; 2]) {
|
||||
self.end(false);
|
||||
self.at = p;
|
||||
self.start = p;
|
||||
self.cur.push(p);
|
||||
}
|
||||
|
||||
fn line_to(&mut self, p: [f64; 2]) {
|
||||
if self.cur.is_empty() {
|
||||
self.cur.push(self.at);
|
||||
self.start = self.at;
|
||||
}
|
||||
if self.cur.len() < MAX_POINTS {
|
||||
self.cur.push(p);
|
||||
}
|
||||
self.at = p;
|
||||
}
|
||||
|
||||
/// Finish the subpath being drawn, closed back to its start or not.
|
||||
fn end(&mut self, closed: bool) {
|
||||
if !self.cur.is_empty() && !self.full() {
|
||||
self.done.push((std::mem::take(&mut self.cur), closed));
|
||||
}
|
||||
self.cur.clear();
|
||||
if closed {
|
||||
self.at = self.start;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The commands of a VML path with their parameters: a command is a letter (or one of
|
||||
/// [`VML_TWO`]) followed by numbers separated by commas or spaces. An empty parameter (nothing
|
||||
/// before a comma) is 0, and so is a formula reference (`@n`, not expected here: see
|
||||
/// [`vml_path`]); fractions are dropped. Anything else is skipped.
|
||||
fn vml_commands(path: &str) -> Vec<(String, Vec<f64>)> {
|
||||
let mut out: Vec<(String, Vec<f64>)> = Vec::new();
|
||||
let mut chars = path.chars().take(MAX_VML_PATH).peekable();
|
||||
// Whether a number was read since the command or the last comma.
|
||||
let mut has_num = true;
|
||||
let mut kept = 0usize;
|
||||
let mut push = |out: &mut Vec<(String, Vec<f64>)>, v: f64| {
|
||||
if kept < MAX_VML_PARAMS
|
||||
&& let Some((_, p)) = out.last_mut()
|
||||
{
|
||||
p.push(v);
|
||||
kept += 1;
|
||||
}
|
||||
};
|
||||
while let Some(c) = chars.next() {
|
||||
if c.is_ascii_alphabetic() {
|
||||
// Cap the commands too, so a path of bare letters can't build a million entries.
|
||||
if out.len() >= MAX_VML_PARAMS {
|
||||
break;
|
||||
}
|
||||
if !has_num {
|
||||
push(&mut out, 0.0);
|
||||
}
|
||||
has_num = false;
|
||||
let lower = c.to_ascii_lowercase();
|
||||
let mut cmd = lower.to_string();
|
||||
if let Some(&n) = chars.peek() {
|
||||
let two: String = [lower, n.to_ascii_lowercase()].iter().collect();
|
||||
if VML_TWO.contains(&two.as_str()) {
|
||||
chars.next();
|
||||
cmd = two;
|
||||
}
|
||||
}
|
||||
out.push((cmd, Vec::new()));
|
||||
} else if c == ',' {
|
||||
if !has_num {
|
||||
push(&mut out, 0.0);
|
||||
}
|
||||
has_num = false;
|
||||
} else if matches!(c, '@' | '-' | '+' | '.') || c.is_ascii_digit() {
|
||||
let mut v = c.to_digit(10).map_or(0.0, f64::from);
|
||||
let mut frac = c == '.';
|
||||
while let Some(&n) = chars.peek() {
|
||||
if n == '.' {
|
||||
frac = true;
|
||||
} else if n.is_ascii_digit() {
|
||||
if !frac {
|
||||
v = (v * 10.0 + n.to_digit(10).map_or(0.0, f64::from)).min(MAX_VML);
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
chars.next();
|
||||
}
|
||||
let v = match c {
|
||||
'@' => 0.0,
|
||||
'-' => -v,
|
||||
_ => v,
|
||||
};
|
||||
has_num = true;
|
||||
push(&mut out, v);
|
||||
}
|
||||
// Whitespace and anything else only separate numbers.
|
||||
}
|
||||
if !has_num {
|
||||
push(&mut out, 0.0);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The pen of an ink stroke written by WordCraft, from its drawing's name (`Ink Pen 3`,
|
||||
/// `Ink Highlighter 4`).
|
||||
pub fn ink_tool(name: &str) -> Option<InkTool> {
|
||||
let mut words = name.split_whitespace();
|
||||
if words.next() != Some("Ink") {
|
||||
return None;
|
||||
}
|
||||
words.next().and_then(InkTool::parse)
|
||||
}
|
||||
|
||||
/// The drawing order of an ink stroke written by WordCraft, from its drawing's name
|
||||
/// (`Ink Pen 3 #12`: the twelfth stroke drawn), for Ink Replay.
|
||||
pub fn ink_order(name: &str) -> Option<u32> {
|
||||
name.split_whitespace().skip(3).find_map(|w| w.strip_prefix('#')?.parse().ok())
|
||||
}
|
||||
|
||||
/// The outline opacity of `ln` (`a:ln`): its colour's `a:alpha`, 0–1.
|
||||
pub fn line_alpha(ln: Option<&El>) -> f32 {
|
||||
ln.and_then(|l| l.child("a:solidFill"))
|
||||
.and_then(|f| f.els().next())
|
||||
.and_then(|c| c.child("a:alpha"))
|
||||
.and_then(|a| a.attr("val"))
|
||||
.and_then(int)
|
||||
.map_or(1.0, |v| (v.clamp(0, 100_000) as f32) / 100_000.0)
|
||||
}
|
||||
|
||||
/// The point at `t` on the Bézier curve with control points `c` (de Casteljau).
|
||||
fn bezier(c: &[[f64; 2]], t: f64) -> [f64; 2] {
|
||||
let mut p: Vec<[f64; 2]> = c.to_vec();
|
||||
while p.len() > 1 {
|
||||
p = p
|
||||
.windows(2)
|
||||
.filter_map(|w| match w {
|
||||
[a, b] => Some([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
p.first().copied().unwrap_or([0.0, 0.0])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn sppr(seg: &str) -> crate::xml::El {
|
||||
let xml = format!(
|
||||
"<wps:spPr xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" \
|
||||
xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\">\
|
||||
<a:custGeom><a:pathLst><a:path w=\"100\" h=\"100\">\
|
||||
<a:moveTo><a:pt x=\"0\" y=\"0\"/></a:moveTo>{seg}</a:path></a:pathLst></a:custGeom></wps:spPr>"
|
||||
);
|
||||
crate::xml::parse(xml.as_bytes()).expect("parse spPr")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vml_path_of_bare_letters_builds_a_bounded_command_list() {
|
||||
let path = "x".repeat(MAX_VML_PATH);
|
||||
let n = vml_commands(&path).len();
|
||||
assert!(n <= MAX_VML_PARAMS, "{n} commands");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bezier_segment_with_many_points_is_bounded() {
|
||||
// One cubicBezTo stuffed with 50 000 points must not make de Casteljau quadratic: the
|
||||
// control polygon is capped, so flattening stays a handful of points and returns at once.
|
||||
let pts = "<a:pt x=\"1\" y=\"1\"/>".repeat(50_000);
|
||||
let sppr = sppr(&format!("<a:cubicBezTo>{pts}</a:cubicBezTo>"));
|
||||
let f = cust_geom(&sppr, 100.0, 100.0).expect("a path");
|
||||
assert!(f.paths.iter().all(|p| p.pts.len() <= 1 + CURVE_STEPS), "{:?}", f.paths.iter().map(|p| p.pts.len()).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_conformant_cubic_still_flattens_to_a_curve() {
|
||||
let sppr = sppr("<a:cubicBezTo><a:pt x=\"0\" y=\"100\"/><a:pt x=\"100\" y=\"100\"/><a:pt x=\"100\" y=\"0\"/></a:cubicBezTo>");
|
||||
let f = cust_geom(&sppr, 100.0, 100.0).expect("a path");
|
||||
assert_eq!(f.paths.len(), 1);
|
||||
assert!(f.paths[0].pts.len() >= 2 && f.paths[0].pts.len() <= 1 + CURVE_STEPS);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user