mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-12 08:52:32 +00:00
* Add craft-wordmodel: WordCraft's document model without its editing code WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX, RTF, ODT and other text-format crates can read into and write from one model. Module paths, type names and serde attributes are unchanged: an app that re-exports the crate keeps its native file format. Left in the app: range editing (edit.rs), IME text and equation editing. Not model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs. * craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs The DOCX reader and writer walk an equation's runs. The walk and the child accessors it uses were in WordCraft's equation editor (math_edit.rs), which stays in the app; they are structure, not editing. * craft-wordmodel: keep the document operations (edit.rs) with the model The DOCX and RTF readers use them: alternative-format chunks are merged into a document with adopt_blocks, and a reader formats ranges with format_range and inserts objects. They operate on the document, not on a view, so they belong beside it. The tests that drove them come back with them; the paragraph walks stay in walk.rs and edit re-exports them. base64 0.23, as WordCraft and craft-offcrypto use. * craft-docx: WordCraft's crates/docx as it is at wordcraft 07f97737 The source, unchanged, so the next commit shows only what the move changes. This commit does not build on its own (it still names wordcraft-doc and wordcraft-geom, and is not a workspace member yet). * craft-docx: on craft-wordmodel, craft-opc, craft-xml and craft-offcrypto - The package (zip reading, part names, relationships, content types, zip writing) is craft-opc's; the module keeps WordCraft's bookkeeping (passthrough keys, the embedded-part manifest) and its own Rel/Rels shape. - Parts are parsed with craft-xml's canonical names (the Strict drawing names and mc:Choice/@Requires through its hooks), then given the tree shape the reader was written against. - Encryption and Restrict Editing hashes are craft-offcrypto's; this crate maps its errors and the w:documentProtection attributes. - wordcraft-doc is craft-wordmodel; wordcraft-geom's helpers are its geom. Tests that covered what now lives in craft-opc and craft-offcrypto are left to those crates; the DIFAT regression is craft-cfb's. Test-only zip use follows zip 9's fallible names. The attribute-cap test times the read only.
71 lines
4.2 KiB
Rust
71 lines
4.2 KiB
Rust
//! The document default font size (`w:docDefaults/w:rPrDefault/w:rPr/w:sz`) across a save and
|
|
//! reopen. An unset default is resolved as `DEFAULT_FONT_SIZE`; the writer must say so, because
|
|
//! the reader reads an omitted size as 10 pt.
|
|
use craft_wordmodel::Document;
|
|
use craft_wordmodel::props::{CharProps, DEFAULT_FONT_SIZE};
|
|
use std::io::{Read, Write};
|
|
use std::sync::Arc;
|
|
fn resolved(d: &Document) -> f32 {
|
|
d.styles.resolve_char(None, &CharProps::default()).size
|
|
}
|
|
fn styles_xml(bytes: &[u8]) -> String {
|
|
let mut archive = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
|
|
let mut xml = String::new();
|
|
archive.by_name("word/styles.xml").unwrap().read_to_string(&mut xml).unwrap();
|
|
xml
|
|
}
|
|
fn defaults(xml: &str) -> &str {
|
|
xml.split("<w:docDefaults>").nth(1).unwrap().split("</w:docDefaults>").next().unwrap()
|
|
}
|
|
#[test]
|
|
fn an_unset_default_size_keeps_its_effective_size_across_save_and_reopen() {
|
|
let mut d = Document::new();
|
|
Arc::make_mut(&mut d.styles).default_chr.size = None;
|
|
assert_eq!(resolved(&d), DEFAULT_FONT_SIZE);
|
|
let bytes = craft_docx::write(&d).unwrap();
|
|
assert!(defaults(&styles_xml(&bytes)).contains(r#"<w:sz w:val="22"/>"#));
|
|
let reopened = craft_docx::read(&bytes).unwrap();
|
|
assert_eq!(resolved(&reopened), DEFAULT_FONT_SIZE);
|
|
// Saving again changes nothing further.
|
|
let again = craft_docx::read(&craft_docx::write(&reopened).unwrap()).unwrap();
|
|
assert_eq!(resolved(&again), DEFAULT_FONT_SIZE);
|
|
}
|
|
#[test]
|
|
fn an_explicit_default_size_is_written_as_it_is() {
|
|
for size in [8.0, 10.0, 11.0, 12.5, 18.0] {
|
|
let mut d = Document::new();
|
|
Arc::make_mut(&mut d.styles).default_chr.size = Some(size);
|
|
let bytes = craft_docx::write(&d).unwrap();
|
|
assert_eq!(resolved(&craft_docx::read(&bytes).unwrap()), size);
|
|
}
|
|
}
|
|
#[test]
|
|
fn a_file_that_omits_the_default_size_is_still_read_as_ten_points() {
|
|
// The reader is unchanged: only what WordCraft writes differs. A package without a size
|
|
// anywhere keeps its established reading.
|
|
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 = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>x</w:t></w:r></w:p></w:body></w:document>"#;
|
|
let styles = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:docDefaults><w:rPrDefault><w:rPr/></w:rPrDefault></w:docDefaults></w:styles>"#;
|
|
let 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 mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
|
for (name, body) in [("_rels/.rels", root), ("word/document.xml", doc), ("word/_rels/document.xml.rels", rels), ("word/styles.xml", styles)] {
|
|
zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap();
|
|
zw.write_all(body.as_bytes()).unwrap();
|
|
}
|
|
let d = craft_docx::read(&zw.finish().unwrap().into_inner()).unwrap();
|
|
assert_eq!(d.styles.default_chr.size, Some(10.0));
|
|
}
|
|
#[test]
|
|
fn an_unset_default_size_does_not_take_a_styles_size() {
|
|
// The written default is the resolver's own fallback, not what the Normal style resolves to,
|
|
// so a style that sets a size keeps it and one that does not still falls back to the default.
|
|
let mut d = Document::new();
|
|
Arc::make_mut(&mut d.styles).default_chr.size = None;
|
|
if let Some(normal) = Arc::make_mut(&mut d.styles).get_mut("Normal") {
|
|
normal.chr.size = Some(14.0);
|
|
}
|
|
let reopened = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
|
|
assert_eq!(reopened.styles.default_chr.size, Some(DEFAULT_FONT_SIZE));
|
|
assert_eq!(resolved(&reopened), resolved(&d));
|
|
}
|