From 0c3793eaddfea825e4af0ff04f6cd8f0846f86d9 Mon Sep 17 00:00:00 2001 From: Kevin Joseph <47004376+kevinj24fr@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:35:36 +0100 Subject: [PATCH] craft-docx: theme font slots survive a save (#2197) * craft-docx: theme font slots survive a save w:rFonts' w:eastAsiaTheme and w:cstheme slots are kept on read and written back. Word picks the East Asian and complex-script fonts through them, so dropping them changed which font Word used for East Asian punctuation and its line breaks. The Latin slots (w:asciiTheme, w:hAnsiTheme) are kept when the run's font came from them and written back only while they still name that font in the document's theme, so a font chosen later is not hidden. craft-wordmodel: a one-byte ThemeFont and CharProps font_ascii_theme, font_hansi_theme, font_ea_theme and font_cs_theme. Overlaying a named font without its slot clears the slot; minus keeps a slot whose font stays. Ported from storytold/wordcraft#5772. * craft-docx: keep the theme's East Asian and script fonts; exact slot replacement The saved theme wrote empty a:ea and a:cs faces and no per-script fonts, so a kept w:eastAsiaTheme or w:cstheme slot named no font in Word. The reader now keeps the theme's ea, cs and a:font script entries (THEME_FONTS) and the writer puts them back. craft-wordmodel: a named font replaces both Latin slots with its own, so a patch with one slot no longer leaves the other stale. minus keeps a font equal to the base's when its slots differ, so a font that clears the base's slots still clears them when overlaid. latin_slot compares the theme face without cloning it. * craft-docx: theme fonts keep faces and scripts apart; limit only usable scripts THEME_FONTS lines now say whether they hold a default face (a:ea, a:cs) or a per-script font, so an a:font script="cs" is no longer written back as the a:cs face. The 256-entry limit applies to usable per-script fonts only, so empty default faces no longer push the last one out. A test pins down Latin slots that name different faces: the model has one Latin font, so only the slot that still names it is written. * craft-docx: a long theme script id never pushes out one font resolution uses The kept per-script fonts count script ids of 8 characters or fewer (the ones font resolution takes) and longer ones (language tags such as zh-Hant-TW) apart, 256 each, so a long one can't use up the place of a mapping resolution keeps. * craft-docx: THEME_FONTS names both per-script caps --- crates/docx/src/package.rs | 7 + crates/docx/src/read/parts.rs | 27 ++++ crates/docx/src/read/props.rs | 14 +- crates/docx/src/write/parts.rs | 2 + crates/docx/src/write/props.rs | 65 +++++++- crates/docx/src/write/settings.rs | 20 ++- crates/docx/tests/it/latin_theme_slots.rs | 186 ++++++++++++++++++++++ crates/docx/tests/it/main.rs | 2 + crates/docx/tests/it/roundtrip.rs | 8 +- crates/docx/tests/it/theme_font_slots.rs | 67 ++++++++ crates/wordmodel/src/props.rs | 65 +++++++- crates/wordmodel/src/props/char_props.rs | 132 ++++++++++++++- 12 files changed, 580 insertions(+), 15 deletions(-) create mode 100644 crates/docx/tests/it/latin_theme_slots.rs create mode 100644 crates/docx/tests/it/theme_font_slots.rs diff --git a/crates/docx/src/package.rs b/crates/docx/src/package.rs index 6f90f56ba..67152ee83 100644 --- a/crates/docx/src/package.rs +++ b/crates/docx/src/package.rs @@ -114,6 +114,13 @@ 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"; +/// The theme's East Asian, complex-script and per-script fonts (`a:ea`, `a:cs`, `a:font +/// script="Hans"` in `a:majorFont` / `a:minorFont`), which the model doesn't hold: `major|minor +/// \tface\tea|cs\ttypeface` and `major|minor\tscript\t