//! A paragraph's vertical text alignment (`w:textAlignment`, ECMA-376 Part 1 ยง17.3.1.39, values //! from ST_TextAlignment): read and written back as read, in the schema's place after `w:jc`. //! Word sizes a line by it when a list label is larger than the text, so a save that drops it //! moves every line after. Fixtures are hand-written from the spec. use wordcraft_doc::props::TextAlignment; fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); let mut zw = craft_zip::Writer::new(); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { zw.add(name, bytes, craft_zip::Compression::Deflated).unwrap(); } zw.finish().unwrap() } fn document_xml(bytes: &[u8]) -> String { let mut z = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); String::from_utf8(z.read("word/document.xml", 16 << 20).unwrap().unwrap()).unwrap() } #[test] fn text_alignment_is_written_back_as_read() { let para = |ppr: &str, text: &str| format!(r#"{ppr}{text}"#); let body = [ para(r#""#, "A"), para(r#""#, "B"), para(r#""#, "C"), para(r#""#, "D"), para(r#""#, "E"), para(r#""#, "F"), para("", "G"), ] .concat(); let d = wordcraft_docx::read(&docx(&body)).unwrap(); let got: Vec> = d.body.iter().filter_map(|b| b.as_para()).map(|p| p.props.text_alignment).collect(); let want = vec![ Some(TextAlignment::Top), Some(TextAlignment::Center), Some(TextAlignment::Baseline), Some(TextAlignment::Bottom), Some(TextAlignment::Auto), None, None, ]; assert_eq!(got, want); let out = wordcraft_docx::write(&d).unwrap(); let xml = document_xml(&out); assert!(xml.contains(r#""#), "{xml}"); for v in ["center", "baseline", "bottom", "auto"] { assert_eq!(xml.matches(&format!(r#""#)).count(), 1, "{v}: {xml}"); } assert_eq!(xml.matches("w:textAlignment").count(), 5, "an unknown value and none write nothing: {xml}"); let back = wordcraft_docx::read(&out).unwrap(); assert_eq!(back.body.iter().filter_map(|b| b.as_para()).map(|p| p.props.text_alignment).collect::>(), want); }