//! Section text direction (`w:textDirection` in `w:sectPr`, ECMA-376 Part 1 §17.6.20). Fixtures //! are hand-written from the spec. use std::io::{Read, Write}; use wordcraft_doc::props::TextDirection; fn docx(body: &str) -> Vec { let root = r#""#; let doc = format!( r#"{body}"# ); let mut zw = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); for (name, bytes) in [("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())] { zw.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); zw.write_all(bytes).unwrap(); } zw.finish().unwrap().into_inner() } fn document_xml(bytes: &[u8]) -> String { let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); let mut s = String::new(); z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); s } #[test] fn section_text_direction_round_trips_in_schema_order() { // Three sections: vertical East Asian (tbRl), bottom-to-top (btLr) and a value no one knows. let sect = |dir: &str| format!(r#""#); let body = format!( r#"{}縦書き{}UpOdd{}"#, sect("tbRl"), sect("btLr"), sect("diagonal") ); let d = wordcraft_docx::read(&docx(&body)).unwrap(); let dirs: Vec = d.sections().iter().map(|(_, s)| s.text_direction).collect(); assert_eq!(dirs, vec![TextDirection::Down, TextDirection::Up, TextDirection::Horizontal]); let out = wordcraft_docx::write(&d).unwrap(); let xml = document_xml(&out); // After w:titlePg and before w:bidi (CT_SectPr's sequence); a horizontal section writes none. assert!(xml.contains(r#""#), "{xml}"); assert!(xml.contains(r#""#)); assert_eq!(xml.matches("w:textDirection").count(), 2); let back = wordcraft_docx::read(&out).unwrap(); let again: Vec = back.sections().iter().map(|(_, s)| s.text_direction).collect(); assert_eq!(again, dirs); } #[test] fn explicit_horizontal_direction_is_kept_in_sections_and_cells() { let body = r#"CellBody"#; let d = wordcraft_docx::read(&docx(body)).unwrap(); let xml = document_xml(&wordcraft_docx::write(&d).unwrap()); assert_eq!(xml.matches(r#""#).count(), 2, "{xml}"); // A horizontal section or cell that never said so still writes nothing. let plain = wordcraft_docx::read(&docx(r#"Body"#)).unwrap(); assert!(!document_xml(&wordcraft_docx::write(&plain).unwrap()).contains("textDirection")); }