Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #83 update: drop cap fixes

aionggd241a22f80e7
diff --cc crates/idml/src/tests.rs
index c3fc6eb50,630256579..d37bdb16d
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@@ -1366,225 -1125,106 +1366,347 @@@ fn arabic_controls_import_independent_x
}
#[test]
-fn table_local_fill_counts_enable_inherited_disabled_pattern_colors() {
- let story =
- CASCADE_STORY.replace("AppliedTableStyle=\"ts-child\"", "AppliedTableStyle=\"ts-base\" StartRowFillCount=\"1\" EndRowFillCount=\"1\"");
- let document = import_idml(&fixture_with_table_styles(CASCADE_STYLES, &story)).unwrap();
- let table = document.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- let fills = table.options.alt_rows.as_ref().unwrap();
- assert_eq!((fills.first, fills.next), (1, 1));
- assert_eq!(fills.first_color, "Brand");
- assert_eq!(fills.first_tint, 0.2);
+fn arabic_story_and_table_directions_survive_idml_export() {
+ 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 st = d.story_mut(sid).unwrap();
+ st.direction = designcraft_doc::TextDirection::RightToLeft;
+ let mut t = designcraft_doc::Table::new(42, 1, 2, 0, 0, 200.0);
+ t.options.direction = designcraft_doc::TextDirection::RightToLeft;
+ st.insert_table(0, t);
+ let back = import_idml(&export_idml(&d)).unwrap();
+ let st = back.stories.values().find(|st| !st.tables.is_empty()).unwrap();
+ assert_eq!(st.direction, designcraft_doc::TextDirection::RightToLeft);
+ assert_eq!(st.tables.values().next().unwrap().options.direction, designcraft_doc::TextDirection::RightToLeft);
}
++/// IDML has no attribute for a drop cap's character style: it is the nested style list's leading
++/// "through 1 Dropcap" entry, read into the drop cap style and written back from it.
#[test]
-fn table_absent_and_explicit_default_edges_remain_distinct_on_idml_roundtrip() {
- let story = CASCADE_STORY
- .replace("AppliedTableStyle=\"ts-child\"", "AppliedTableStyle=\"Missing\"")
- .replace("Name=\"1:1\"", "Name=\"1:1\" TopEdgeStrokeWeight=\"1\" TopEdgeStrokePriority=\"0\"");
- let document = import_idml(&fixture_with_table_styles(STYLES, &story)).unwrap();
- for document in [&document, &import_idml(&export_idml(&document)).unwrap()] {
- let table = document.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- assert_eq!(table.cell(1, 0).unwrap().stroke_defined, [false; 4]);
- assert_eq!(table.cell(1, 1).unwrap().stroke_defined, [true, false, false, false]);
- assert_eq!(table.cell(1, 1).unwrap().border_overrides, [false; 4]);
- assert_eq!(table.cell(1, 1).unwrap().stroke_priorities, [0; 4]);
+fn drop_caps_import_and_round_trip() {
- // A style with a drop cap, its character style and a nested style through it; a paragraph
- // overriding it locally.
++ // A style with a drop cap and its character style; a paragraph overriding both locally.
+ let opener = r#"<ParagraphStyle Self="ParagraphStyle/Opener" Name="Opener" DropCapCharacters="1" DropCapLines="3">
- <Properties><DropCapStyle type="object">CharacterStyle/Strong</DropCapStyle><AllNestedStyles type="list"><ListItem type="record">
++ <Properties><AllNestedStyles type="list"><ListItem type="record">
+ <AppliedCharacterStyle type="object">CharacterStyle/Strong</AppliedCharacterStyle><Delimiter type="enumeration">Dropcap</Delimiter>
+ <Repetition type="long">1</Repetition><Inclusive type="boolean">true</Inclusive></ListItem></AllNestedStyles></Properties>
+ </ParagraphStyle>
+ </RootParagraphStyleGroup>"#;
+ let styles = STYLES.replace("</RootParagraphStyleGroup>", opener);
+ let story = r#"<?xml version="1.0" encoding="UTF-8"?>
+<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="16.0">
+ <Story Self="s1">
- <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener" DropCapCharacters="2" DropCapLines="2" DropCapStyle="CharacterStyle/$ID/[No character style]">
++ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener" DropCapCharacters="2" DropCapLines="2">
++ <Properties><AllNestedStyles type="list"><ListItem type="record">
++ <AppliedCharacterStyle type="object">CharacterStyle/$ID/[No character style]</AppliedCharacterStyle><Delimiter type="enumeration">Dropcap</Delimiter>
++ <Repetition type="long">1</Repetition><Inclusive type="boolean">true</Inclusive></ListItem></AllNestedStyles></Properties>
+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Once upon a time</Content></CharacterStyleRange>
+ </ParagraphStyleRange>
+ </Story>
+</idPkg:Story>"#;
+ 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 check = |d: &Document| {
+ let st = d.styles.para("Opener").unwrap();
+ assert_eq!((st.para.drop_cap_chars, st.para.drop_cap_lines), (Some(1), Some(3)));
+ assert_eq!(st.para.drop_cap_style.as_deref(), Some("Strong"));
- let ns = st.para.nested_styles.clone().unwrap();
- assert_eq!((ns[0].style.as_str(), &ns[0].until), ("Strong", &designcraft_doc::NestedUntil::Dropcap));
++ assert_eq!(st.para.nested_styles, Some(vec![designcraft_doc::NestedStyle::drop_cap("Strong")]), "one entry, not duplicated");
+ let p = &d.stories.values().find(|s| s.text.starts_with("Once")).unwrap().paras[0];
+ assert_eq!((p.para.drop_cap_chars, p.para.drop_cap_lines), (Some(2), Some(2)));
+ assert_eq!(p.para.drop_cap_style.as_deref(), Some(st::NO_CHAR_STYLE));
+ };
+ let d = import_idml_with(&bytes, &|_| None).unwrap();
+ check(&d);
- // The drop cap's character style isn't written to IDML (InDesign sets it through a Dropcap
- // nested style); Align Left Edge and Scale for Descenders are, as DropcapDetail.
++ let bytes = export_idml(&d);
++ assert!(!zip_text(&bytes, "").contains("DropCapStyle"), "not an IDML attribute");
++ check(&import_idml(&bytes).unwrap());
++ // A drop cap style set without nested styles is written as one; Align Left Edge and Scale for
++ // Descenders as DropcapDetail.
+ let mut d = d;
+ if let Some(s) = d.styles_mut().para_mut("Opener") {
++ s.para.nested_styles = None;
+ s.para.drop_cap_align_left = Some(false);
+ s.para.drop_cap_scale_descenders = Some(true);
}
+ let bytes = export_idml(&d);
+ assert!(!zip_text(&bytes, "").contains("DropCapStyle"));
+ assert!(zip_text(&bytes, "Resources/Styles.xml").contains(r#"DropcapDetail="2""#));
+ let back = import_idml(&bytes).unwrap();
+ let st = back.styles.para("Opener").unwrap();
- assert_eq!((st.para.drop_cap_chars, st.para.drop_cap_lines, st.para.drop_cap_style.as_deref()), (Some(1), Some(3), None));
++ assert_eq!((st.para.drop_cap_chars, st.para.drop_cap_lines, st.para.drop_cap_style.as_deref()), (Some(1), Some(3), Some("Strong")));
+ assert_eq!((st.para.drop_cap_align_left, st.para.drop_cap_scale_descenders), (Some(false), Some(true)));
- assert_eq!(st.para.nested_styles.as_ref().map(|n| n[0].until.clone()), Some(designcraft_doc::NestedUntil::Dropcap));
++ assert_eq!(st.para.nested_styles, Some(vec![designcraft_doc::NestedStyle::drop_cap("Strong")]));
++}
++
++/// IDML styles and stories with an `Opener` style whose drop cap style is `Strong`, plus `styles`
++/// (more paragraph styles) and `paras` (the story's paragraph ranges). `WORDS` in them stands for
++/// a nested style list of `Strong` through 1 word.
++fn drop_cap_fixture(styles: &str, paras: &str) -> Vec<u8> {
++ let nested = |style: &str, delim: &str| {
++ format!(
++ r#"<AllNestedStyles type="list"><ListItem type="record"><AppliedCharacterStyle type="object">CharacterStyle/{style}</AppliedCharacterStyle><Delimiter type="enumeration">{delim}</Delimiter><Repetition type="long">1</Repetition><Inclusive type="boolean">true</Inclusive></ListItem></AllNestedStyles>"#
++ )
++ };
++ let words = nested("Strong", "AnyWord");
++ let opener = format!(
++ r#"<ParagraphStyle Self="ParagraphStyle/Opener" Name="Opener" DropCapCharacters="1" DropCapLines="3"><Properties>{}</Properties></ParagraphStyle>
++ {}
++ </RootParagraphStyleGroup>"#,
++ nested("Strong", "Dropcap"),
++ styles.replace("WORDS", &words)
++ );
++ let styles = STYLES.replace("</RootParagraphStyleGroup>", &opener);
++ let story = format!(
++ r#"<?xml version="1.0" encoding="UTF-8"?>
++<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="16.0">
++ <Story Self="s1">{}</Story>
++</idPkg:Story>"#,
++ paras.replace("WORDS", &words)
++ );
++ 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),
++ ])
++}
++
++/// A nested style list replaces the inherited one, drop cap style entry included: a style or
++/// paragraph with a list of its own and no Dropcap entry has no drop cap style.
++#[test]
++fn own_nested_styles_clear_an_inherited_drop_cap_style() {
++ let styles = r#"<ParagraphStyle Self="ParagraphStyle/Child" Name="Child"><Properties><BasedOn type="object">ParagraphStyle/Opener</BasedOn>WORDS</Properties></ParagraphStyle>
++ <ParagraphStyle Self="ParagraphStyle/Plain" Name="Plain"><Properties>WORDS</Properties></ParagraphStyle>"#;
++ let paras = r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener"><Properties>WORDS</Properties>
++ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Cleared</Content><Br/></CharacterStyleRange>
++ </ParagraphStyleRange>
++ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener">
++ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Inherited</Content></CharacterStyleRange>
++ </ParagraphStyleRange>"#;
++ let d = import_idml_with(&drop_cap_fixture(styles, paras), &|_| None).unwrap();
++ let cap = |name: &str| d.styles.para(name).unwrap().para.drop_cap_style.clone();
++ assert_eq!(cap("Opener").as_deref(), Some("Strong"));
++ assert_eq!(cap("Child").as_deref(), Some(st::NO_CHAR_STYLE), "its own list replaces Opener's");
++ assert_eq!(cap("Plain"), None, "nothing to clear");
++ let story = d.stories.values().find(|s| s.text.starts_with("Cleared")).unwrap();
++ assert_eq!(story.paras[0].para.drop_cap_style.as_deref(), Some(st::NO_CHAR_STYLE));
++ assert_eq!(story.paras[1].para.drop_cap_style, None);
++ assert_eq!(d.styles.resolve_para(&story.paras[1]).0.drop_cap_style, "Strong");
+ }
+
++/// Paragraphs in table cells and footnotes are cleared like story paragraphs.
+ #[test]
-fn multiline_table_and_cell_style_bases_resolve() {
- let styles = CASCADE_STYLES.replace(">cs-base</BasedOn>", ">\n cs-base \n</BasedOn>").replace(">ts-base</BasedOn>", ">\n ts-base \n</BasedOn>");
- let document = import_idml(&fixture_with_table_styles(&styles, CASCADE_STORY)).unwrap();
- let table = document.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- assert_eq!(table.cell(1, 1).unwrap().strokes[0].weight, 2.0);
- assert_eq!(table.options.border_for(0).weight, 7.0);
++fn own_nested_styles_clear_an_inherited_drop_cap_style_in_cells_and_notes() {
++ let opener = |text: &str| {
++ format!(
++ r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener"><Properties>WORDS</Properties><CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>{text}</Content></CharacterStyleRange></ParagraphStyleRange>"#
++ )
++ };
++ let paras = format!(
++ r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/NormalParagraphStyle"><CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
++<Content>Body</Content><Footnote>{}</Footnote>
++<Table HeaderRowCount="0" FooterRowCount="0"><Row Name="0" MinimumHeight="24"/><Column Name="0" SingleColumnWidth="60"/><Cell Name="0:0">{}</Cell></Table>
++</CharacterStyleRange></ParagraphStyleRange>"#,
++ opener("Note"),
++ opener("Cell")
++ );
++ let d = import_idml_with(&drop_cap_fixture("", &paras), &|_| None).unwrap();
++ let story = d.stories.values().find(|s| !s.notes.is_empty()).expect("story with a footnote");
++ let note = &story.notes[0].text;
++ assert_eq!(note.paras[0].para.drop_cap_style.as_deref(), Some(st::NO_CHAR_STYLE), "{:?}", note.text);
++ let cell = &story.tables.values().next().expect("a table").cell(0, 0).unwrap().text;
++ assert_eq!(cell.paras[0].para.drop_cap_style.as_deref(), Some(st::NO_CHAR_STYLE), "{:?}", cell.text);
+ }
+
++/// A paragraph that clears its style's drop cap style without nested styles of its own round-trips:
++/// it is written with the style's list, the Dropcap entry set to [None].
+ #[test]
-fn legacy_native_documents_export_both_builtin_table_styles_when_missing() {
- let story = CASCADE_STORY.replace("AppliedTableStyle=\"ts-child\"", "AppliedTableStyle=\"TableStyle/$ID/[Basic Table]\"");
- let mut document = import_idml(&fixture_with_story(&story)).unwrap();
- document.styles_mut().table.clear();
- let bytes = export_idml(&document);
- let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap();
- let mut styles = String::new();
- std::io::Read::read_to_string(&mut zip.by_name("Resources/Styles.xml").unwrap(), &mut styles).unwrap();
- assert_eq!(styles.matches("Self=\"TableStyle/$ID/[Basic Table]\"").count(), 1);
- assert_eq!(styles.matches("Self=\"TableStyle/$ID/[No table style]\"").count(), 1);
- let back = import_idml(&bytes).unwrap();
- assert!(back.styles.table.iter().any(|s| s.name == designcraft_doc::BASIC_TABLE));
++fn a_cleared_drop_cap_style_round_trips() {
++ let paras = r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener">
++ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Once upon a time</Content></CharacterStyleRange>
++ </ParagraphStyleRange>"#;
++ let mut d = import_idml_with(&drop_cap_fixture("", paras), &|_| None).unwrap();
++ let sid = *d.stories.keys().next().unwrap();
++ let clear = |d: &mut Document| d.story_mut(sid).unwrap().paras[0].para.drop_cap_style = Some(st::NO_CHAR_STYLE.into());
++ let resolved = |d: &Document| {
++ let st = d.stories.values().find(|s| s.text.starts_with("Once")).unwrap();
++ d.styles.resolve_para(&st.paras[0]).0
++ };
++ clear(&mut d);
++ let back = import_idml(&export_idml(&d)).unwrap();
++ let pp = resolved(&back);
++ assert_eq!(pp.drop_cap_style, st::NO_CHAR_STYLE);
++ assert_eq!(pp.nested_styles, vec![designcraft_doc::NestedStyle::drop_cap(st::NO_CHAR_STYLE)]);
++ // The style's drop cap style without nested styles: the paragraph is written with an empty
++ // list, which clears it on import.
++ if let Some(s) = d.styles_mut().para_mut("Opener") {
++ s.para.nested_styles = None;
++ }
++ assert_eq!(resolved(&d).drop_cap_style, st::NO_CHAR_STYLE);
++ let back = import_idml(&export_idml(&d)).unwrap();
++ assert_eq!(back.styles.para("Opener").unwrap().para.drop_cap_style.as_deref(), Some("Strong"));
++ let pp = resolved(&back);
++ assert_eq!(pp.drop_cap_style, st::NO_CHAR_STYLE);
++ assert!(pp.nested_styles.iter().all(|ns| ns.style != "Strong"), "{:?}", pp.nested_styles);
}
+/// Frames take the corners and Text Frame Options they don't write from their object style chain,
+/// then `[None]`. Per-corner attributes win over the legacy all-corners pair, at each level; a
+/// corner shape without a radius takes the chain's radius.
#[test]
-fn hostile_table_spans_are_clamped_before_arithmetic() {
- let story = CASCADE_STORY.replace("Name=\"1:1\"", "Name=\"1:1\" RowSpan=\"inf\" ColumnSpan=\"inf\"");
- let document = import_idml(&fixture_with_table_styles(CASCADE_STYLES, &story)).unwrap();
- document.check().unwrap();
- let table = document.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- assert_eq!(table.cell(1, 1).unwrap().row_span, 1);
- assert_eq!(table.cell(1, 1).unwrap().col_span, 2);
+fn frames_inherit_object_style_corners_and_text_frame_options() {
+ use designcraft_doc::{AutoSize, FirstBaseline, VerticalJustification};
+ use designcraft_geom::corners::CornerShape as C;
+ // Anchors in the order top-left, bottom-left, bottom-right, top-right.
+ let path = |x: f64| {
+ let pts: String = [(x, 0.0), (x, 50.0), (x + 50.0, 50.0), (x + 50.0, 0.0)]
+ .iter()
+ .map(|(x, y)| format!(r#"<PathPointType Anchor="{x} {y}" LeftDirection="{x} {y}" RightDirection="{x} {y}"/>"#))
+ .collect();
+ format!(
+ r#"<Properties><PathGeometry><GeometryPathType PathOpen="false"><PathPointArray>{pts}</PathPointArray></GeometryPathType></PathGeometry></Properties>"#
+ )
+ };
+ let corners = |opt: &str, r: &str| {
+ ["TopLeft", "TopRight", "BottomLeft", "BottomRight"].map(|n| format!(r#"{n}CornerOption="{opt}" {n}CornerRadius="{r}""#)).join(" ")
+ };
+ let inset = |v: f64| {
+ format!(
+ r#"<Properties><InsetSpacing type="list">{}</InsetSpacing></Properties>"#,
+ r#"<ListItem type="unit">V</ListItem>"#.replace('V', &v.to_string()).repeat(4)
+ )
+ };
+ let designmap = format!(
+ r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="20.2" Self="d">
+<RootObjectStyleGroup Self="ro">
+ <ObjectStyle Self="ObjectStyle/$ID/[None]" Name="$ID/[None]" {none}>
+ <TextFramePreference TextColumnCount="1" TextColumnGutter="12" AutoSizingReferencePoint="CenterPoint" AutoSizingType="Off"/>
+ </ObjectStyle>
+ <ObjectStyle Self="ObjectStyle/Rounded" Name="Rounded" TopLeftCornerOption="RoundedCorner" TopRightCornerOption="RoundedCorner" BottomLeftCornerOption="RoundedCorner" BottomRightCornerOption="RoundedCorner" TopLeftCornerRadius="6">
+ <Properties><BasedOn type="object">ObjectStyle/$ID/[None]</BasedOn></Properties>
+ <TextFramePreference TextColumnCount="2" TextColumnGutter="18" VerticalJustification="CenterAlign" FirstBaselineOffset="LeadingOffset" MinimumFirstBaselineOffset="4" IgnoreWrap="true" AutoSizingType="HeightOnly">{inset}</TextFramePreference>
+ </ObjectStyle>
+ <ObjectStyle Self="ObjectStyle/Child" Name="Child" TopRightCornerOption="BevelCorner">
+ <Properties><BasedOn type="object">ObjectStyle/Rounded</BasedOn></Properties>
+ </ObjectStyle>
+</RootObjectStyleGroup>
+<Story Self="s1"/>
+<Spread Self="sp1"><Page Self="p1"/>
+<TextFrame Self="tf" ParentStory="s1" AppliedObjectStyle="ObjectStyle/Child" BottomRightCornerOption="InverseRoundedCorner" BottomRightCornerRadius="11.34">{p0}<TextFramePreference TextColumnCount="3"/></TextFrame>
+<Rectangle Self="legacy" AppliedObjectStyle="ObjectStyle/Rounded" CornerOption="BevelCorner" CornerRadius="5" TopLeftCornerOption="InsetCorner">{p1}</Rectangle>
+<Rectangle Self="one" TopRightCornerOption="RoundedCorner" TopRightCornerRadius="11.34">{p2}</Rectangle>
+<Rectangle Self="plain" AppliedObjectStyle="ObjectStyle/Rounded">{p3}</Rectangle>
+</Spread>
+</Document>"#,
+ none = corners("None", "12"),
+ inset = inset(3.0),
+ p0 = path(0.0),
+ p1 = path(100.0),
+ p2 = path(200.0),
+ p3 = path(300.0),
+ );
+ let d = import_idml_with(&zip_files(&[("designmap.xml", &designmap)]), &|_| None).unwrap();
+ let item = |n: usize| d.spreads[0].items[n].clone();
+ // Path order: top-left, bottom-left, bottom-right, top-right.
+ let shapes = |n: usize| item(n).corners.corners.map(|c| (c.shape, c.size));
+ assert_eq!(shapes(0), [(C::Rounded, 6.0), (C::Rounded, 12.0), (C::InverseRounded, 11.34), (C::Bevel, 12.0)]);
+ assert_eq!(shapes(1), [(C::Inset, 5.0), (C::Bevel, 5.0), (C::Bevel, 5.0), (C::Bevel, 5.0)]);
+ assert!(item(2).corners.corners.iter().enumerate().all(|(i, c)| (c.shape == C::Rounded) == (i == 3)));
+ assert_eq!(item(2).corners.corners[3].size, 11.34);
+ assert_eq!(shapes(3), [(C::Rounded, 6.0), (C::Rounded, 12.0), (C::Rounded, 12.0), (C::Rounded, 12.0)]);
+ let tf = item(0);
+ let o = &tf.text_frame().unwrap().options;
+ assert_eq!((o.columns, o.gutter, o.inset), (3, 18.0, [3.0; 4]));
+ assert_eq!((o.vertical_justification, o.first_baseline, o.first_baseline_min), (VerticalJustification::Center, FirstBaseline::Leading, 4.0));
+ assert_eq!((o.ignore_wrap, o.auto_size, o.auto_size_ref), (true, AutoSize::HeightOnly, 4));
}
+/// InDesign's designmap lists layers back to front; `Document::layers[0]` is the front layer.
#[test]
-fn signed_idml_edge_priorities_roundtrip_without_clamping() {
- let story = CASCADE_STORY.replace(
- "Name=\"1:1\"",
- "Name=\"1:1\" TopEdgeStrokePriority=\"-5\" LeftEdgeStrokePriority=\"-2147483648\" RightEdgeStrokePriority=\"2147483647\"",
- );
- let document = import_idml(&fixture_with_table_styles(CASCADE_STYLES, &story)).unwrap();
- for document in [&document, &import_idml(&export_idml(&document)).unwrap()] {
- let table = document.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- assert_eq!(table.cell(1, 1).unwrap().stroke_priorities, [-5, i32::MIN, 0, i32::MAX]);
- }
+fn layers_import_and_export_in_stacking_order() {
+ let designmap = r#"<Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" Self="d">
+ <Layer Self="L1" Name="Background"/>
+ <Layer Self="L2" Name="Content"/>
+ <idPkg:Spread src="Spreads/Spread_s.xml"/>
+ </Document>"#;
+ let spread = r#"<idPkg:Spread xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
+ <Spread Self="s">
+ <Page Self="p" GeometricBounds="0 0 100 100" ItemTransform="1 0 0 1 0 0"/>
+ <Rectangle Self="r" ItemLayer="L1">
+ <Properties><PathGeometry><GeometryPathType PathOpen="false"><PathPointArray>
+ <PathPointType Anchor="0 0"/><PathPointType Anchor="0 100"/>
+ <PathPointType Anchor="100 100"/><PathPointType Anchor="100 0"/>
+ </PathPointArray></GeometryPathType></PathGeometry></Properties>
+ </Rectangle>
+ </Spread>
+ </idPkg:Spread>"#;
+ let d = import_idml(&zip_files(&[("designmap.xml", designmap), ("Spreads/Spread_s.xml", spread)])).unwrap();
+ let names = |d: &Document| d.layers.iter().map(|l| l.name.clone()).collect::<Vec<_>>();
+ assert_eq!(names(&d), ["Content", "Background"]);
+ assert_eq!(d.spreads[0].items[0].layer, d.layers[1].id, "the rectangle stays on Background");
+
+ let bytes = export_idml(&d);
+ let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap();
+ let mut map = String::new();
+ std::io::Read::read_to_string(&mut zip.by_name("designmap.xml").unwrap(), &mut map).unwrap();
+ let (bg, content) = (map.find(r#"Name="Background""#).unwrap(), map.find(r#"Name="Content""#).unwrap());
+ assert!(bg < content, "designmap lists the back layer first");
+ let back = import_idml(&bytes).unwrap();
+ assert_eq!(names(&back), ["Content", "Background"]);
+ assert_eq!(back.spreads[0].items[0].layer, back.layers[1].id);
}
+/// Synthetic forward references: both a body anchor and a footnote anchor own
+/// a group containing a text frame whose story is serialized later.
#[test]
-fn alternating_table_strokes_inherit_and_roundtrip_without_flattening_cell_edges() {
- let styles = CASCADE_STYLES.replace(
- "Name=\"BaseTable\"",
- "Name=\"BaseTable\" StartRowStrokeCount=\"1\" EndRowStrokeCount=\"1\" StartRowStrokeWeight=\"0\" EndRowStrokeWeight=\"0\" StartRowStrokeColor=\"Color/Paper\" EndRowStrokeColor=\"Color/Brand\" StartColumnStrokeCount=\"2\" EndColumnStrokeCount=\"1\" StartColumnStrokeWeight=\"1\" EndColumnStrokeWeight=\"2\" EndColumnLineStyle=\"StrokeStyle/$ID/Dotted\" SkipFirstAlternatingStrokeColumns=\"1\"",
- ).replace("Name=\"Table\"", "Name=\"Table\" StartRowStrokeTint=\"25\"");
- let story = CASCADE_STORY.replace("HeaderRowCount=\"1\"", "HeaderRowCount=\"1\" EndRowStrokeColor=\"Swatch/None\"");
- let document = import_idml(&fixture_with_table_styles(&styles, &story)).unwrap();
- let before = document.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- let row = before.options.row_strokes.as_ref().unwrap();
- assert_eq!((row.first, row.next), (1, 1));
- assert_eq!(row.first_stroke.tint, 0.25);
- assert_eq!(row.first_stroke.color, designcraft_color::swatch::PAPER);
- assert_eq!(row.next_stroke.color, designcraft_color::swatch::NONE);
- assert!(!row.uniform_stroke().unwrap().is_visible(), "zero patterns are uniform even when their colors differ");
- let column = before.options.column_strokes.as_ref().unwrap();
- assert_eq!((column.first, column.next, column.skip_first), (2, 1, 1));
- assert_eq!(column.next_stroke.kind, designcraft_doc::StrokeType::Dotted);
- assert!(column.uniform_stroke().is_none(), "nonuniform/skipped patterns are preserved without guessing their phase");
- assert_eq!(before.cell(0, 0).unwrap().strokes[2].weight, 6.0, "cell styles remain higher-precedence edges");
- let back = import_idml(&export_idml(&document)).unwrap();
- let after = back.stories.values().flat_map(|s| s.tables.values()).next().unwrap();
- assert_eq!(after.options.row_strokes, before.options.row_strokes);
- assert_eq!(after.options.column_strokes, before.options.column_strokes);
- let style = back.styles.table.iter().find(|s| s.name == "Table").unwrap();
- assert_eq!(style.row_strokes.first_stroke.weight, None);
- assert_eq!(style.row_strokes.first_stroke.tint, Some(0.25));
+fn round_trips_anchored_text_frames_in_body_and_footnotes() {
+ use designcraft_doc::{AnchorPosition, AnchoredObject, Content, Item, ItemId};
+ use std::sync::Arc;
+ let mut d = Document::new(&NewDocument::default());
+ let layer = d.default_layer();
+ let (_, body) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(20.0, 20.0, 400.0, 600.0), layer, "Body", ParaFormat::default()).unwrap();
+ d.story_mut(body).unwrap().insert_note(4, "Note", ParaFormat::default());
+ for in_note in [false, true] {
+ let (frame, _) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(0.0, 0.0, 60.0, 30.0), layer, "Symbol", ParaFormat::default()).unwrap();
+ let child = d.item(frame).unwrap().clone();
+ d.spread_mut(SpreadRef::Doc(0)).unwrap().items.retain(|i| i.id != frame);
+ let mut group = Item::new(ItemId(d.alloc()), layer, Shape::Group, Default::default());
+ group.content = Content::Group { items: vec![Arc::new(child)] };
+ let anchor = AnchoredObject::new(group, AnchorPosition::Inline { y_offset: 2.0 });
+ let story = d.story_mut(body).unwrap();
+ if in_note {
+ Arc::make_mut(&mut story.notes[0]).text.insert_object(0, anchor);
+ } else {
+ story.insert_object(0, anchor);
+ }
+ }
+ d.check().unwrap();
+ for _ in 0..2 {
+ d = import_idml(&export_idml(&d)).unwrap();
+ d.check().unwrap();
+ assert_eq!(d.stories.len(), 3);
+ let body = d.stories.values().find(|s| !s.notes.is_empty()).unwrap();
+ assert_eq!(body.notes.len(), 1);
+ assert_eq!(body.objects.len(), 1);
+ assert_eq!(body.notes[0].text.objects.len(), 1);
+ let children: Vec<_> = d.anchored_items().into_iter().filter(|(_, i)| i.text_frame().is_some()).collect();
+ assert_eq!(children.len(), 2);
+ for (_, item) in children {
+ let story = d.story(item.text_frame().unwrap().story).unwrap();
+ assert_eq!(story.text, "Symbol");
+ assert_eq!(story.frames, vec![item.id]);
+ }
+ }
}
#[test]