//! Section text direction (`w:textDirection` in `w:sectPr`, ECMA-376 Part 1 §17.6.20). Fixtures //! are hand-written from the spec. use wordcraft_doc::props::TextDirection; 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 docx_with_styles(body: &str, styles: &str) -> Vec { let mut bytes = craft_zip::Writer::new(); let root = r#""#; let document_rels = r#""#; let doc = format!( r#"{body}"# ); for (name, value) in [ ("_rels/.rels", root.as_bytes()), ("word/_rels/document.xml.rels", document_rels.as_bytes()), ("word/document.xml", doc.as_bytes()), ("word/styles.xml", styles.as_bytes()), ] { bytes.add(name, value, craft_zip::Compression::Deflated).unwrap(); } bytes.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 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")); } #[test] fn paragraph_text_direction_round_trips_through_two_saves_and_json() { use wordcraft_doc::props::ParaProps; use wordcraft_doc::props::ParagraphTextDirection; let values = ["lrTb", "lrTbV", "tb", "tbRl", "tbRlV", "tbLr", "tbLrV", "btLr", "tbV", "rl", "rlV", "lr", "lrV"]; let mut body: String = values.iter().map(|value| format!(r#"{value}"#)).collect(); body.push_str(r#"Inherited"#); let d = wordcraft_docx::read(&docx(&body)).unwrap(); let paras: Vec<_> = d.body.iter().filter_map(|b| b.as_para()).collect(); for (para, value) in paras.iter().zip(values) { assert_eq!(para.props.text_direction, ParagraphTextDirection::from_ooxml(value), "{value}"); } assert_eq!(paras[values.len()].props.text_direction, None); let json = serde_json::to_value(¶s[0].props).unwrap(); assert_eq!(json["textDirection"], "lrTb"); let decoded: ParaProps = serde_json::from_value(json).unwrap(); assert_eq!(decoded.text_direction, Some(ParagraphTextDirection::LrTb)); let first = wordcraft_docx::write(&d).unwrap(); let xml = document_xml(&first); for value in values { assert!(xml.contains(&format!(r#""#)), "{xml}"); } assert_eq!(xml.matches("w:textDirection").count(), values.len(), "inherited direction stays absent: {xml}"); let second = wordcraft_docx::write(&wordcraft_docx::read(&first).unwrap()).unwrap(); let twice = document_xml(&second); assert_eq!(twice.matches("w:textDirection").count(), values.len(), "{twice}"); for value in values { assert!(twice.contains(&format!(r#""#)), "{twice}"); } } #[test] fn paragraph_style_text_direction_is_preserved_and_direct_value_overrides_it() { let styles = r#""#; let body = r#"StyleOverride"#; let d = wordcraft_docx::read(&docx_with_styles(body, styles)).unwrap(); let xml = document_xml(&wordcraft_docx::write(&d).unwrap()); assert_eq!(xml.matches(r#""#).count(), 2, "both paragraphs keep their style: {xml}"); assert!(xml.contains(r#""#), "direct horizontal is explicit: {xml}"); assert_eq!(xml.matches("w:textDirection").count(), 1, "only the direct paragraph writes a direction: {xml}"); let _z_bytes = wordcraft_docx::write(&d).unwrap(); let mut z = craft_zip::Archive::open(&_z_bytes, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap(); let styles_xml = String::from_utf8(z.read("word/styles.xml", 16 << 20).unwrap().unwrap()).unwrap(); assert!(styles_xml.contains(r#""#), "style value survives: {styles_xml}"); }