Skip to content

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

IDML: anchored objects take unset anchor settings from their object style (#555)

aiongg25c215faf9be
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index 46c3253b6..ddd51dc06 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -1835,20 +1835,34 @@ impl<'r> Importer<'r> {
let has_text = c.find_all("TextFrame").next().is_some();
if !has_text && let Some(item) = self.item(c, designcraft_geom::Affine::IDENTITY) {
use designcraft_doc::anchored::AnchorAlign as A;
- let set = c.find("AnchoredObjectSetting");
- let get = |k: &str| set.and_then(|s| s.num(k)).unwrap_or(0.0);
- let position = match set.and_then(|s| s.get("AnchoredPosition")) {
- Some("AboveLine") => designcraft_doc::AnchorPosition::AboveLine {
- align: match set.and_then(|s| s.get("HorizontalAlignment")) {
- Some("CenterAlign") => A::Center,
- Some("RightAlign") => A::Right,
+ // Each setting the item leaves unset comes from its object style chain,
+ // then from the IDML defaults.
+ let levels: Vec<&El> = std::iter::once(c).chain(self.object_style_chain(c)).collect();
+ let s = |k: &str| {
+ let set = levels.iter().filter_map(|l| l.find("AnchoredObjectSetting")).find_map(|x| x.get(k));
+ set.unwrap_or(match k {
+ "AnchoredPosition" => "InlinePosition",
+ "AnchorPoint" => "BottomRightAnchor",
+ "HorizontalAlignment" => "LeftAlign",
+ "HorizontalReferencePoint" => "TextFrame",
+ "VerticalAlignment" => "BottomAlign",
+ "VerticalReferencePoint" => "LineBaseline",
+ "PinPosition" => "true",
+ _ => "",
+ })
+ };
+ let get = |k: &str| s(k).trim().parse::<f64>().ok().filter(|v| v.is_finite()).unwrap_or(0.0);
+ let position = match s("AnchoredPosition") {
+ "AboveLine" => designcraft_doc::AnchorPosition::AboveLine {
+ align: match s("HorizontalAlignment") {
+ "CenterAlign" => A::Center,
+ "RightAlign" => A::Right,
_ => A::Left,
},
space_before: get("AnchorSpaceAbove"),
space_after: get("AnchorYoffset"),
},
- Some("Anchored") => {
- let s = |k: &str| set.and_then(|x| x.get(k)).unwrap_or("");
+ "Anchored" => {
let col = match s("HorizontalAlignment") {
"CenterAlign" => 1,
"RightAlign" => 2,
@@ -1987,25 +2001,33 @@ impl<'r> Importer<'r> {
})
}
- /// Resolve an item attribute, falling back to its object style chain.
- fn attr_or_style(&self, e: &El, k: &str) -> Option<String> {
- if let Some(v) = e.get(k) {
- return Some(v.to_string());
- }
+ /// An item's object style chain: its applied style, the styles that one is based on, then
+ /// `[None]` (cycle-safe).
+ fn object_style_chain(&self, e: &El) -> Vec<&El> {
+ let mut chain: Vec<&El> = Vec::new();
let mut os = e.get("AppliedObjectStyle").map(str::to_string);
- let mut depth = 0;
while let Some(id) = os {
- depth += 1;
- if depth > 16 {
- break;
- }
let Some(s) = self.object_els.get(&id) else { break };
- if let Some(v) = s.get(k) {
- return Some(v.to_string());
+ if chain.len() >= 16 || chain.iter().any(|c| std::ptr::eq(*c, s)) {
+ break;
}
- os = s.prop("BasedOn").map(|b| if b.starts_with("ObjectStyle/") { b } else { format!("ObjectStyle/{}", names::escape_id(&b)) });
+ chain.push(s);
+ os = s.prop("BasedOn").map(|b| {
+ let b = b.trim();
+ if b.starts_with("ObjectStyle/") { b.to_string() } else { format!("ObjectStyle/{}", names::escape_id(b)) }
+ });
}
- None
+ if let Some(none) = self.object_els.get("ObjectStyle/$ID/[None]")
+ && !chain.iter().any(|c| std::ptr::eq(*c, none))
+ {
+ chain.push(none);
+ }
+ chain
+ }
+
+ /// Resolve an item attribute, falling back to its object style chain.
+ fn attr_or_style(&self, e: &El, k: &str) -> Option<String> {
+ e.get(k).or_else(|| self.object_style_chain(e).into_iter().find_map(|s| s.get(k))).map(str::to_string)
}
fn stroke_from(&mut self, e: &El, item: Option<&El>) -> Stroke {
diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs
index 23ec99bdf..3035d75d3 100644
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@ -775,6 +775,60 @@ fn round_trips_anchored_objects() {
assert_eq!(s.objects[1].position, AnchorPosition::AboveLine { align: AnchorAlign::Center, space_before: 3.0, space_after: 4.0 });
}
+/// Anchored settings an item leaves unset come from its object style chain, attribute by attribute.
+#[test]
+fn anchored_objects_take_unset_settings_from_object_styles() {
+ use designcraft_doc::{AnchorPosition, anchored::AnchorRelative as R};
+ let styles = STYLES.replace(
+ r#"<ObjectStyle Self="ObjectStyle/$ID/[None]" Name="$ID/[None]"/>"#,
+ r#"<ObjectStyle Self="ObjectStyle/$ID/[None]" Name="$ID/[None]">
+ <AnchoredObjectSetting AnchoredPosition="InlinePosition" SpineRelative="false" LockPosition="false" PinPosition="true"
+ AnchorPoint="BottomRightAnchor" HorizontalAlignment="LeftAlign" HorizontalReferencePoint="TextFrame"
+ VerticalAlignment="BottomAlign" VerticalReferencePoint="LineBaseline" AnchorXoffset="0" AnchorYoffset="0" AnchorSpaceAbove="0"/>
+ </ObjectStyle>
+ <ObjectStyle Self="ObjectStyle/Side" Name="Side"><AnchoredObjectSetting HorizontalReferencePoint="PageMargins" PinPosition="false"/></ObjectStyle>
+ <ObjectStyle Self="ObjectStyle/Note" Name="Note"><Properties><BasedOn type="object">ObjectStyle/Side</BasedOn></Properties></ObjectStyle>"#,
+ );
+ let rect = |style: &str, setting: &str| {
+ format!(
+ r#"<Rectangle Self="r{style}" AppliedObjectStyle="ObjectStyle/{style}" ItemTransform="1 0 0 1 0 0">
+ <Properties><PathGeometry><GeometryPathType PathOpen="false"><PathPointArray>
+ <PathPointType Anchor="0 0"/><PathPointType Anchor="0 10"/><PathPointType Anchor="10 10"/><PathPointType Anchor="10 0"/>
+ </PathPointArray></GeometryPathType></PathGeometry></Properties>
+ <AnchoredObjectSetting AnchoredPosition="Anchored" {setting}/>
+ </Rectangle>"#
+ )
+ };
+ let story = format!(
+ r#"<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"><Story Self="s1">
+ <ParagraphStyleRange><CharacterStyleRange><Content>a</Content>{}{}{}</CharacterStyleRange></ParagraphStyleRange>
+ </Story></idPkg:Story>"#,
+ rect("$ID/[None]", r#"AnchorXoffset="5" AnchorYoffset="-3""#),
+ rect("$ID/[None]", r#"AnchorPoint="TopLeftAnchor" VerticalReferencePoint="PageEdge""#),
+ rect("Note", r#"AnchorXoffset="2""#),
+ );
+ let d = import_idml(&fixture_with_table_styles(&styles, &story)).unwrap();
+ let s = d.stories.values().find(|s| !s.objects.is_empty()).unwrap();
+ let custom = |x_relative, y_relative, x_offset, y_offset, object_point, keep_within_column| AnchorPosition::Custom {
+ x_relative,
+ y_relative,
+ x_offset,
+ y_offset,
+ object_point,
+ ref_point: 6,
+ keep_within_column,
+ };
+ let positions: Vec<_> = s.objects.iter().map(|o| o.position.clone()).collect();
+ assert_eq!(
+ positions,
+ [
+ custom(R::TextFrame, R::Anchor, 5.0, -3.0, 8, true),
+ custom(R::TextFrame, R::PageEdge, 0.0, 0.0, 0, true),
+ custom(R::PageMargin, R::Anchor, 2.0, 0.0, 8, false),
+ ]
+ );
+}
+
#[test]
fn round_trips_frames_with_pasted_in_items() {
let mut d = Document::new(&NewDocument::default());