DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge storytold/main into fix/h1246-idml-round-trip-renames-styles-w
Brandon Thomasd45a7baf4c3d
diff --cc crates/idml/src/tests.rsindex d7106ad48,c56a10246..35374a5e0--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@@ -4369,41 -4398,104 +4398,143 @@@ fn cell_first_baseline_comes_from_cell_assert_eq!(s2.first_baseline, Some(FirstBaseline::CapHeight));}+ /// A graphic placed in a table cell is written into the cell, not dropped.+ #[test]+ fn exports_graphic_in_table_cell() {+ let mut d = Document::new(&NewDocument::default());+ let lid = d.default_layer();+ let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(40.0, 40.0, 400.0, 400.0), lid, "Intro", ParaFormat::default()).unwrap();+ let asset = designcraft_doc::AssetId(d.alloc());+ let id = d.add_asset(designcraft_doc::Asset {+ id: asset,+ name: "tick.png".into(),+ mime: "image/png".into(),+ data: std::sync::Arc::new(vec![1, 2, 3]),+ ..Default::default()+ });+ let mut t = designcraft_doc::Table::new(d.alloc(), 2, 2, 0, 0, 200.0);+ t.cell_mut(1, 0).unwrap().graphic = Some(designcraft_doc::Graphic {+ asset: id,+ size: (24.0, 24.0),+ xf: designcraft_geom::Affine::IDENTITY,+ clip: None,+ auto_fit: Default::default(),+ fit_align: 4,+ crop: [0.0; 4],+ wrap: Default::default(),+ fill: Default::default(),+ });+ d.story_mut(sid).unwrap().insert_table(5, t);+ let story = zip_text(&export_idml(&d), "Stories/");+ let cell = story.split("<Cell ").nth(3).unwrap_or_default();+ let cell = cell.split("</Cell>").next().unwrap_or_default();+ assert!(cell.contains("<Image "), "{story}");+ assert!(cell.contains("ClipContentToCell=\"true\""), "{story}");+ }++ /// Non-finite numbers in colour values, page bounds and item transforms (NaN, infinities and+ /// values like 1e300 that overflow `f32`) never reach the model: JSON would write them as null.+ #[test]+ fn non_finite_number_lists_never_reach_the_model() {+ let graphic = GRAPHIC.replace(r#"ColorValue="255 0 0""#, r#"ColorValue="NaN 1e300 inf""#);+ let spread = SPREAD+ .replace(r#"Self="p2" AppliedMaster="m1" GeometricBounds="0 0 700 500""#, r#"Self="p2" AppliedMaster="m1" GeometricBounds="0 0 NaN 500""#)+ .replace(r#"ItemTransform="1 0 0 1 50 -300""#, r#"ItemTransform="1 0 0 1 inf -300""#);+ let bytes = zip_files(&[+ ("designmap.xml", DESIGNMAP),+ ("Resources/Graphic.xml", &graphic),+ ("Resources/Styles.xml", STYLES),+ ("Resources/Preferences.xml", PREFS),+ ("MasterSpreads/MasterSpread_m1.xml", MASTER),+ ("Spreads/Spread_sp1.xml", &spread),+ ("Stories/Story_s1.xml", STORY),+ ]);+ let doc = import_idml(&bytes).unwrap();+ for s in &doc.swatches {+ if let SwatchValue::Color { color, .. } | SwatchValue::Paper { color } = &s.value {+ let parts: Vec<f32> = match *color {+ Color::Rgb { r, g, b } => vec![r, g, b],+ Color::Cmyk { c, m, y, k } => vec![c, m, y, k],+ Color::Gray { k } => vec![k],+ };+ assert!(parts.iter().all(|v| v.is_finite()), "swatch {} is not finite: {color:?}", s.name);+ }+ }+ for sp in doc.spreads.iter().chain(&doc.parents) {+ for p in &sp.pages {+ assert!(p.width.is_finite() && p.height.is_finite() && p.x.is_finite(), "page {p:?}");+ }+ }+ assert_eq!(doc.non_finite_item(), None);+ }++ /// Cell Options › Text › Rotation: the angle survives the round trip, and the import says it is+ /// not composed — narrow header columns of vertical labels come in horizontal and wrap mid-word,+ /// and nothing said so (#1468).+ #[test]+ fn rotated_cell_text_round_trips_and_the_import_says_it_is_not_composed() {+ let mut d = Document::new(&NewDocument::default());+ let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(0.0, 0.0, 300.0, 300.0), d.default_layer(), "", ParaFormat::default()).unwrap();+ let mut t = designcraft_doc::Table::new(7, 1, 3, 0, 0, 300.0);+ t.cell_mut(0, 0).unwrap().rotation = 90.0;+ t.cell_mut(0, 1).unwrap().rotation = 270.0;+ d.story_mut(sid).unwrap().insert_table(0, t);+ let bytes = export_idml(&d);+ let story = zip_text(&bytes, "Stories/");+ assert!(story.contains(r#"RotationAngle="90""#) && story.contains(r#"RotationAngle="270""#), "{story}");++ let back = import_idml_report(&bytes, &|_| None).unwrap();+ let t = back.document.stories.values().find(|st| !st.tables.is_empty()).unwrap().tables.values().next().unwrap().clone();+ assert_eq!((t.cell(0, 0).unwrap().rotation, t.cell(0, 1).unwrap().rotation, t.cell(0, 2).unwrap().rotation), (90.0, 270.0, 0.0));+ let about: Vec<&String> = back.warnings.iter().filter(|w| w.contains("rotated text")).collect();+ assert_eq!(about.len(), 1, "one line for the file, not one per cell: {:?}", back.warnings);+ assert!(about[0].starts_with("2 table cell(s)"), "{}", about[0]);+ assert!(about[0].contains("composed horizontally"), "{}", about[0]);++ // A file with no rotated cell says nothing.+ let mut plain = Document::new(&NewDocument::default());+ let (_, sid) =+ plain.add_text_frame(SpreadRef::Doc(0), Rect::new(0.0, 0.0, 300.0, 300.0), plain.default_layer(), "", ParaFormat::default()).unwrap();+ plain.story_mut(sid).unwrap().insert_table(0, designcraft_doc::Table::new(8, 1, 2, 0, 0, 200.0));+ let quiet = import_idml_report(&export_idml(&plain), &|_| None).unwrap();+ assert!(quiet.warnings.iter().all(|w| !w.contains("rotated text")), "{:?}", quiet.warnings);+ }+++#[test]+fn style_names_with_a_literal_colon_survive_a_round_trip() {+ use designcraft_doc::styles::{CellStyle, CharacterStyle, ObjectStyle, ParagraphStyle, TableStyle};+ let names = ["Title: Big", "Group/Child: X", "Back\\slash: \\:"];+ let mut d = small_doc();+ for n in names {+ let st = d.styles_mut();+ st.paragraph.push(ParagraphStyle {+ name: n.into(),+ based_on: None,+ next_style: None,+ para: Default::default(),+ chars: Default::default(),+ shortcut: String::new(),+ });+ st.character.push(CharacterStyle { name: n.into(), based_on: None, chars: Default::default(), shortcut: String::new() });+ st.cell.push(CellStyle { name: n.into(), ..Default::default() });+ st.table.push(TableStyle { name: n.into(), ..Default::default() });+ st.object.push(ObjectStyle { name: n.into(), ..Default::default() });+ }+ let back = import_idml(&export_idml(&d)).unwrap();+ for n in names {+ assert!(+ back.styles.paragraph.iter().any(|s| s.name == n),+ "paragraph {n:?}: {:?}",+ back.styles.paragraph.iter().map(|s| &s.name).collect::<Vec<_>>()+ );+ assert!(back.styles.character.iter().any(|s| s.name == n), "character {n:?}");+ assert!(back.styles.cell.iter().any(|s| s.name == n), "cell {n:?}");+ assert!(back.styles.table.iter().any(|s| s.name == n), "table {n:?}");+ assert!(back.styles.object.iter().any(|s| s.name == n), "object {n:?}");+ }+ // A group is still written as an IDML `:` group path.+ assert_eq!(names::style_name_out(names::PARA_BUILTINS, "Group/Child"), "Group:Child");+ for n in names {+ assert_eq!(names::style_name_in(names::PARA_BUILTINS, &names::style_name_out(names::PARA_BUILTINS, n)), n);+ }+}