diff --git a/crates/docx/src/lib.rs b/crates/docx/src/lib.rs index 7f47d948b..c3c3e8df3 100644 --- a/crates/docx/src/lib.rs +++ b/crates/docx/src/lib.rs @@ -20,6 +20,19 @@ mod custom; mod flat; mod fonts; mod package; + +static APPLICATION_NAME: std::sync::OnceLock = std::sync::OnceLock::new(); + +/// Names the application written to `docProps/app.xml` (``), for an app that +/// embeds this writer under its own name; "WordCraft" until set. Only the first call counts. +pub fn set_application_name(name: &str) { + let _ = APPLICATION_NAME.set(name.to_owned()); +} + +/// The application `docProps/app.xml` names (see [`set_application_name`]). +pub fn application_name() -> &'static str { + APPLICATION_NAME.get().map_or("WordCraft", String::as_str) +} mod read; mod smart_art; #[cfg(test)] diff --git a/crates/docx/src/write/settings.rs b/crates/docx/src/write/settings.rs index 18f9f8239..3371986c4 100644 --- a/crates/docx/src/write/settings.rs +++ b/crates/docx/src/write/settings.rs @@ -349,7 +349,7 @@ pub(super) fn app_xml(doc: &Document) -> Vec { ("xmlns:vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"), ], ); - w.leaf("Application", &[], "WordCraft"); + w.leaf("Application", &[], crate::application_name()); w.leaf("Words", &[], &doc.word_count().to_string()); w.leaf("Paragraphs", &[], &doc.paragraph_count().to_string()); w.leaf("DocSecurity", &[], "0"); diff --git a/crates/docx/tests/application_name.rs b/crates/docx/tests/application_name.rs new file mode 100644 index 000000000..1973e63ae --- /dev/null +++ b/crates/docx/tests/application_name.rs @@ -0,0 +1,19 @@ +//! An app that embeds the writer names itself in `docProps/app.xml` (its own test binary: the +//! name is set once per process). + +use std::io::Read; + +#[test] +fn the_embedding_app_names_itself_in_app_xml() { + assert_eq!(craft_docx::application_name(), "WordCraft", "the default"); + craft_docx::set_application_name("Example Writer"); + craft_docx::set_application_name("Other"); + assert_eq!(craft_docx::application_name(), "Example Writer", "only the first call counts"); + let mut d = craft_wordmodel::Document::new(); + d.body = vec![craft_wordmodel::para_block(craft_wordmodel::Paragraph::with_text("Text", Default::default()))]; + let bytes = craft_docx::write(&d).unwrap(); + let mut z = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap(); + let mut app = String::new(); + z.by_name("docProps/app.xml").unwrap().read_to_string(&mut app).unwrap(); + assert!(app.contains("Example Writer"), "{app}"); +}