mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
* 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
68 lines
4.0 KiB
Rust
68 lines
4.0 KiB
Rust
//! Theme font slots of the East Asian and complex-script fonts (`w:rFonts/@w:eastAsiaTheme`,
|
|
//! `@w:cstheme`, ISO/IEC 29500-1 §17.3.2.26). Word picks those fonts through the slot, so a save
|
|
//! that drops it changes the font of East Asian punctuation and, with it, Word's line breaks.
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
fn docx(body: &str, styles: &str) -> Vec<u8> {
|
|
let root = 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>"#;
|
|
let doc_rels = 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/styles" Target="styles.xml"/></Relationships>"#;
|
|
let doc = format!(
|
|
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>{body}</w:body></w:document>"#
|
|
);
|
|
let styles = format!(
|
|
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">{styles}</w:styles>"#
|
|
);
|
|
let parts = [
|
|
("_rels/.rels", root.to_string()),
|
|
("word/document.xml", doc),
|
|
("word/_rels/document.xml.rels", doc_rels.to_string()),
|
|
("word/styles.xml", styles),
|
|
];
|
|
let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
|
for (name, xml) in &parts {
|
|
zw.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap();
|
|
zw.write_all(xml.as_bytes()).unwrap();
|
|
}
|
|
zw.finish().unwrap().into_inner()
|
|
}
|
|
|
|
fn part(bytes: &[u8], name: &str) -> String {
|
|
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
|
|
let mut s = String::new();
|
|
z.by_name(name).unwrap().read_to_string(&mut s).unwrap();
|
|
s
|
|
}
|
|
|
|
fn rfonts(xml: &str) -> Vec<String> {
|
|
xml.match_indices("<w:rFonts").map(|(i, _)| xml[i..].split('>').next().unwrap_or("").to_string()).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn theme_font_slots_survive_a_save() {
|
|
// As pandoc writes them: theme slots only, in the document defaults, and on a run.
|
|
let styles = r#"<w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr></w:rPrDefault></w:docDefaults>"#;
|
|
let body = r#"<w:p><w:r><w:rPr><w:rFonts w:eastAsiaTheme="majorEastAsia" w:cstheme="bogus"/></w:rPr><w:t>“Quoted” text</w:t></w:r></w:p>"#;
|
|
let d = craft_docx::read(&docx(body, styles)).unwrap();
|
|
let run = &d.body[0].as_para().unwrap().runs[0];
|
|
assert_eq!(run.props.font_ea_theme, Some(craft_wordmodel::props::ThemeFont::MajorEastAsia));
|
|
assert_eq!(run.props.font_cs_theme, None, "a name outside ST_Theme is not kept");
|
|
|
|
let out = craft_docx::write(&d).unwrap();
|
|
let styles_out = rfonts(&part(&out, "word/styles.xml"));
|
|
assert!(
|
|
styles_out.iter().any(|f| f.contains(r#"w:eastAsiaTheme="minorEastAsia""#) && f.contains(r#"w:cstheme="minorBidi""#)),
|
|
"the document defaults keep their theme slots: {styles_out:?}"
|
|
);
|
|
let doc_out = rfonts(&part(&out, "word/document.xml"));
|
|
assert!(doc_out.iter().any(|f| f.contains(r#"w:eastAsiaTheme="majorEastAsia""#)), "the run keeps its slot: {doc_out:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_named_east_asian_font_replaces_the_theme_slot() {
|
|
let mut base = craft_wordmodel::props::CharProps { font_ea_theme: Some(craft_wordmodel::props::ThemeFont::MinorEastAsia), ..Default::default() };
|
|
base.overlay(&craft_wordmodel::props::CharProps { font_ea: Some("SimSun".into()), ..Default::default() });
|
|
assert_eq!(base.font_ea.as_deref(), Some("SimSun"));
|
|
assert_eq!(base.font_ea_theme, None, "the slot would otherwise win over the font just chosen");
|
|
}
|