diff --git a/crates/docx/src/fonts.rs b/crates/docx/src/fonts.rs index 5591f9000c..f63930b571 100644 --- a/crates/docx/src/fonts.rs +++ b/crates/docx/src/fonts.rs @@ -14,10 +14,20 @@ const MAX_FONTS: usize = 64; /// 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, +} + /// 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> { +pub fn embedded_fonts(doc: &Document) -> Vec { let Some(table) = doc.passthrough.get(FONT_TABLE_PART).and_then(|b| crate::xml::parse(b).ok()) else { return Vec::new(); }; @@ -26,7 +36,10 @@ pub fn embedded_fonts(doc: &Document) -> Vec> { return Vec::new(); }; let mut out = Vec::new(); - for embed in table.children("w:font").flat_map(|f| f.els().filter(|e| EMBEDS.contains(&e.name.as_str()))) { + 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; @@ -52,7 +65,7 @@ pub fn embedded_fonts(doc: &Document) -> Vec> { for (i, b) in data.iter_mut().take(32).enumerate() { *b ^= key.get(i % 16).copied().unwrap_or(0); } - out.push(data); + out.push(EmbeddedFont { name: name.to_string(), data }); } out } diff --git a/crates/docx/src/lib.rs b/crates/docx/src/lib.rs index ba432169c5..3c871aba8b 100644 --- a/crates/docx/src/lib.rs +++ b/crates/docx/src/lib.rs @@ -27,7 +27,7 @@ pub use crypt::{ is_encrypted, protection_hash, }; pub use flat::{XmlKind, sniff_xml, write as write_flat}; -pub use fonts::embedded_fonts; +pub use fonts::{EmbeddedFont, embedded_fonts}; pub use read::read; pub use smart_art::smart_art_items; pub use wordml2003_write::write as write_wordml2003; diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index d4dfcdf481..b8ce0dbf11 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -172,7 +172,10 @@ pub fn open_bytes_in(name: &str, bytes: &[u8], password: Option<&str>, region: & /// installed then lays out and exports with it instead of a fallback. Faces already available /// stay as they are. fn load_embedded_fonts(doc: &Document) { - let files: Vec> = wordcraft_docx::embedded_fonts(doc).iter().filter_map(|f| wordcraft_fonts::decode_font_file(f).ok()).collect(); + let files: Vec<(Vec, String)> = wordcraft_docx::embedded_fonts(doc) + .into_iter() + .filter_map(|f| wordcraft_fonts::decode_font_file(&f.data).ok().map(|data| (data, f.name))) + .collect(); if !files.is_empty() { wordcraft_fonts::FontDb::global().add_document_fonts(files); } diff --git a/crates/engine/src/tests_fonts.rs b/crates/engine/src/tests_fonts.rs index 83c59cb593..29a072b0a8 100644 --- a/crates/engine/src/tests_fonts.rs +++ b/crates/engine/src/tests_fonts.rs @@ -174,3 +174,21 @@ fn fonts_embedded_in_a_docx_lay_out_its_text() { assert!((x - mono).abs() < 0.01, "{family}: measured with the embedded font: {x} vs {mono} (missing {missing})"); } } + +/// A font Word embeds as a subset ("embed only the characters used") goes by a made-up family +/// inside (`___WRD_EMBED_SUB_46`), and the text asks for it by the font table's name +/// (`OBRBWW+Century Schoolbook`): the text is measured with it all the same (#2240). It fell back +/// to a substitute, as if nothing were embedded. +#[test] +fn a_subset_font_word_embeds_lays_out_text_set_in_its_table_name() { + const TEXT: &str = "illicit little lilies fill tiny jill"; + const FAMILY: &str = "QWERTY+JetBrains Sub"; + let end_x = + |bytes: &[u8]| Session::new(crate::io::open_bytes("e.docx", bytes).unwrap()).layout().caret_on(&Pos::body(0, TEXT.len()), 0).unwrap().x; + let mono = end_x(&docx_embedding("JetBrains Mono", None, TEXT)); + let missing = end_x(&docx_embedding(FAMILY, None, TEXT)); + assert!((missing - mono).abs() > 1.0, "not embedded, the text falls back: {missing} vs {mono}"); + let subset = renamed("JetBrainsMono-Regular.ttf", "JetBrains Mono", "___WRD_EMBED_7"); + let x = end_x(&docx_embedding(FAMILY, Some((&subset, "{6A0E3B57-1C2D-4E8F-9A7B-3D5C1E0F2B64}")), TEXT)); + assert!((x - mono).abs() < 0.01, "measured with the embedded subset: {x} vs {mono} (missing {missing})"); +} diff --git a/crates/fonts/src/fontdb.rs b/crates/fonts/src/fontdb.rs index e38c234d1e..798ec6aca3 100644 --- a/crates/fonts/src/fontdb.rs +++ b/crates/fonts/src/fontdb.rs @@ -377,6 +377,9 @@ struct SysFallback { tried: std::collections::HashSet<&'static str>, } +/// The longest name a document's font table gives an embedded font that faces take on. +const MAX_FAMILY_NAME: usize = 128; + /// Characters remembered as covered by no installed font (a cap on memory: once full the set /// starts over, and the coverage index answers again quickly). #[cfg(not(target_arch = "wasm32"))] @@ -1133,20 +1136,26 @@ impl FontDb { /// Add a user font (TTF/OTF/TTC bytes). Returns the number of faces added (0 if unparseable or /// every face was already present). pub fn add_font(&self, bytes: Vec) -> usize { - self.add_fonts(vec![bytes]) + self.add_fonts(vec![(bytes, None)]) } /// Add the faces of several font files at once: lookups see all of them or none, never a /// family with only some of its styles (which would resolve, and be cached as, the wrong - /// face). Faces already present (same family and style) are skipped. Returns how many were - /// added. - fn add_fonts(&self, files: Vec>) -> usize { + /// face). A file given a `name` its faces don't go by (a document's font table's, see + /// [`FontDb::add_document_fonts`]) has them go by that name instead. Faces already present + /// (same family and style) are skipped. Returns how many were added. + fn add_fonts(&self, files: Vec<(Vec, Option)>) -> usize { let mut new = Vec::new(); - for bytes in files { + for (bytes, name) in files { let data = Arc::new(bytes); + let name: Option = name.map(|n| n.trim().chars().take(MAX_FAMILY_NAME).collect()).filter(|n: &String| !n.is_empty()); for f in enumerate_faces(&data) { if let Some(mut face) = make_face(FontBytes::Owned(data.clone()), f.index, f.family, f.style, f.legacy, f.coords) { face.local_names = f.local; + if let Some(n) = name.as_deref().filter(|n| !face.named(n) && !face.local_names.iter().any(|l| l.name.eq_ignore_ascii_case(n))) { + face.family = n.to_string(); + face.legacy_family = n.to_string(); + } new.push(Arc::new(face)); } } @@ -1260,7 +1269,7 @@ impl FontDb { p.dedup(); p }; - let files = paths.iter().filter_map(|p| std::fs::read(p).ok()).collect(); + let files = paths.iter().filter_map(|p| std::fs::read(p).ok()).map(|b| (b, None)).collect(); self.add_fonts(files) > 0 } diff --git a/crates/fonts/src/fontdb/user.rs b/crates/fonts/src/fontdb/user.rs index 2fb09a43d8..0f6d3c4c44 100644 --- a/crates/fonts/src/fontdb/user.rs +++ b/crates/fonts/src/fontdb/user.rs @@ -245,10 +245,13 @@ impl FontDb { /// Add the fonts a document embeds (#1171) for this run: not recorded as user fonts, never /// stored. Faces already available are kept. Lookups made while the family was missing are - /// forgotten ([`FontDb::generation`]), so text set in it now resolves to it. Returns how many - /// faces were added. - pub fn add_document_fonts(&self, files: Vec>) -> usize { - let added = self.add_fonts(files); + /// forgotten ([`FontDb::generation`]), so text set in it now resolves to it. Each file comes + /// with the name the document's font table gives it, which its faces answer to as well: Word + /// embeds a subset ("embed only the characters used") under a made-up family + /// (`___WRD_EMBED_SUB_46`), and the text asks for it by the table's name (#2240). Returns how + /// many faces were added. + pub fn add_document_fonts(&self, files: Vec<(Vec, String)>) -> usize { + let added = self.add_fonts(files.into_iter().map(|(data, name)| (data, Some(name))).collect()); if added > 0 { self.fonts_changed(); } diff --git a/crates/fonts/src/tests_sysfonts.rs b/crates/fonts/src/tests_sysfonts.rs index 19b36be188..cfd5f4421a 100644 --- a/crates/fonts/src/tests_sysfonts.rs +++ b/crates/fonts/src/tests_sysfonts.rs @@ -587,7 +587,7 @@ fn circle_without_text_face() { fn circle_checks_face_coverage() { let db = FontDb::with_font_dirs(Vec::new()); db.set_system_fallback(false); - db.add_document_fonts(vec![named_face("SourceSerif4-Regular.ttf", "Times New Roman")]); + db.add_document_fonts(vec![(named_face("SourceSerif4-Regular.ttf", "Times New Roman"), "Times New Roman".to_string())]); let preferred = db.face("Times New Roman", "Regular"); assert!(!preferred.covers('●')); let primary = db.face("Source Serif 4", "Regular"); diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 5ecf5fe652..13bd079e0f 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -98,7 +98,7 @@ Measured by grepping `crates/docx/src/read` and `src/write` for the OOXML elemen | Native Word ink (`w14:contentPart`, InkML) | 🟡 | 🟡 | Drawing-level ink kept opaque with its InkML part, relationship and content type; direct run-level `w:contentPart` and run-level `mc:AlternateContent` keep the native branch and VML picture fallback. Direct content parts remain direct through repeated save/reopen cycles, without a native rendering. Drawing-level ink shows the preview or a missing-picture box. No native stroke rendering, eraser, conversion or replay; ink nested in groups/canvases remains unsupported | | 3D models | ❌ | ❌ | Dropped on read, not preserved on save | | Equations (OMML `m:oMath`) | ✅ | ✅ | #191; manual breaks (`m:brk`) and the settings' `m:mathPr` wrapping options (#326) | -| Themes, font table, settings, compatibility mode | ✅ | ✅ | `w:allowSpaceOfSameStyleInTable` read and written in schema order; absent/false default off, enabled flag retained in modes 11/12/14/15. Embedded fonts (`w:embedRegular`) not used for rendering, but the font table, its relationships and the embedded font parts are written back unchanged (#770); attached template (`w:attachedTemplate` and its external relationship) and `w:linkStyles` round-trip (#377). `w:removePersonalInformation` and `w:removeDateAndTime` round-trip and are honoured on save (#1062): comment and revision authors written as "Author" (initials "A"), no `dc:creator`/`cp:lastModifiedBy`, no annotation dates; the open document keeps its values (`file.privacyOptions` sets them, `file.privacy` reads them; File › Options › Save › Privacy for this document). The template path comes from the file, so opening never reads it: styles update from it only on an explicit Update Styles. Settings the model doesn't hold (other `w:compatSetting`s, rsids, document variables, proofing state, colour scheme mapping…) survive a save: the writer merges into the file's own `settings.xml` ([below](#settings-preservation)); `w:latentStyles`, `w:default` on localized default styles and lists' `w:nsid`/`w:tmpl`/`w:multiLevelType` round-trip | +| Themes, font table, settings, compatibility mode | ✅ | ✅ | `w:allowSpaceOfSameStyleInTable` read and written in schema order; absent/false default off, enabled flag retained in modes 11/12/14/15. Embedded fonts (`w:embedRegular`, `w:embedBold`…) lay out and export the text set in them for the session (#1176), subsets Word embeds ("embed only the characters used", a made-up family such as `___WRD_EMBED_SUB_46` inside) found by the font table's name (#2240); the font table, its relationships and the embedded font parts are written back unchanged (#770); attached template (`w:attachedTemplate` and its external relationship) and `w:linkStyles` round-trip (#377). `w:removePersonalInformation` and `w:removeDateAndTime` round-trip and are honoured on save (#1062): comment and revision authors written as "Author" (initials "A"), no `dc:creator`/`cp:lastModifiedBy`, no annotation dates; the open document keeps its values (`file.privacyOptions` sets them, `file.privacy` reads them; File › Options › Save › Privacy for this document). The template path comes from the file, so opening never reads it: styles update from it only on an explicit Update Styles. Settings the model doesn't hold (other `w:compatSetting`s, rsids, document variables, proofing state, colour scheme mapping…) survive a save: the writer merges into the file's own `settings.xml` ([below](#settings-preservation)); `w:latentStyles`, `w:default` on localized default styles and lists' `w:nsid`/`w:tmpl`/`w:multiLevelType` round-trip | | Custom XML parts, document properties, bibliography sources | 🟡 | 🟡 | Custom XML stores reached from the package/main document preserve opaque data, properties and relationships; binding evaluation remains absent. Custom properties round-trip; bibliography model I/O remains separate (#532 open) | | Section text direction (`w:textDirection` in `w:sectPr`, §17.6.20) | ✅ | ✅ | `tbRl`, `btLr` (and `tbRlV`/`tbLrV`, read as `tbRl`); `lrTbV` and unknown values read as horizontal (#491) | | Section vertical alignment (`w:vAlign` in `w:sectPr`, §17.6.23) | ✅ | ✅ | `top`, `center`, `bottom` and `both` (Justified, read as Center before #1320); unknown values read as top, top writes nothing (`crates/docx/tests/section_valign.rs`). ODT/RTF don't carry it | @@ -236,6 +236,7 @@ Imported text fields use the first nonempty cached result run's formatting on th | 2026-10-14 | trivial | ODT export writes sections: each new-page section gets its own page layout and master page (the first stays "Standard"), named by its first paragraph or table with the page-numbering restart; a continuous section with columns is a columned `text:section` (gap 19) | | 2026-10-18 | trivial | #2445: pending Save As answers keep their originating document identity, so a replacement document is never written to an old picker destination; real Save-button and focused-editor shortcut controls verified | | 2026-10-16 | trivial | #2445: interactive Save on import-only .doc/.dot paths uses the existing DOCX Save As flow; cancel and failed writes keep the draft, and Close waits for a successful save. Explicit/script saves still report unsupported legacy output | +| 2026-10-16 | trivial | Fonts Word embeds as subsets are found by the font table's name (`OBRBWW+Century Schoolbook`), not only the family inside the file, so the text set in them uses them instead of a substitute (#2240) | | 2026-10-17 | trivial | Paste: an HTML `
` (LibreOffice's both-sides indent) pastes as a Normal paragraph with its margins as indents, not the centred italic Quote style; opening .html unchanged (#1270) | | 2026-10-15 | trivial | `wordcraft-cli convert --update-fields` explicitly refreshes unlocked fields before conversion; ordinary conversion keeps cached results. Field refresh can change pagination | | 2026-10-16 | trivial | Cached textual `REF` results with `\h` export links to their bookmarks. Code view and existing enclosing hyperlinks remain unchanged; unresolved bookmarks emit no PDF link. |