Skip to content

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

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

aiongg64b834f2e0b2
diff --cc crates/idml/src/import.rs
index 06acfc77d,916f9cc66..ea5f16012
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@@ -2132,17 -1829,34 +2132,32 @@@ impl<'r> Importer<'r>
rcf.over.position = None;
b.push(&designcraft_doc::FOOTNOTE_REF.to_string(), &rcf);
}
- // Not supported yet: skip their content entirely.
- "Rectangle" | "Oval" | "Polygon" | "GraphicLine" | "Group" => {
- // An anchored object (text frames inside stories aren't supported yet).
- let has_text = c.find_all("TextFrame").next().is_some();
- if !has_text && let Some(item) = self.item(c, designcraft_geom::Affine::IDENTITY) {
+ // Anchored page items, including groups containing text frames.
+ "Rectangle" | "Oval" | "Polygon" | "GraphicLine" | "Group" | "TextFrame" => {
+ if 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"),