Skip to content

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

refactor(idml): split graphic part and spread import helpers (Sonar S107, S3776)

Dylan M. Taylor71d938613852
diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs
index c174c1889..b95d61a8d 100644
--- a/crates/idml/src/export.rs
+++ b/crates/idml/src/export.rs
@@ -431,6 +431,45 @@ impl<'a> Ex<'a> {
)
}
+ /// A `Gradient` element; stop colours missing from the swatches become anonymous colours.
+ fn gradient_el(
+ &mut self,
+ s: &designcraft_color::Swatch,
+ gradient: &designcraft_color::Gradient,
+ colors: &mut Vec<El>,
+ by_value: &mut Vec<(Color, String)>,
+ ) -> El {
+ let id = self.sw(&s.name);
+ let mut g = El::new("Gradient")
+ .attr("Self", &id)
+ .attr("Type", if gradient.kind == GradientKind::Radial { "Radial" } else { "Linear" })
+ .attr("Name", &s.name)
+ .attr("ColorEditable", "true")
+ .attr("ColorRemovable", "true")
+ .attr("Visible", "true")
+ .attr("SwatchCreatorID", "7937");
+ for (i, stop) in gradient.stops.iter().enumerate() {
+ let cid = match by_value.iter().find(|(c, _)| *c == stop.color) {
+ Some((_, id)) => id.clone(),
+ None => {
+ let cid = format!("Color/{}", self.fresh());
+ colors.push(color_el(&cid, "$ID/", "Process", &stop.color, "Normal", (true, true, false)));
+ by_value.push((stop.color, cid.clone()));
+ cid
+ }
+ };
+ let mut e = El::new("GradientStop")
+ .attr("Self", format!("{}GradientStop{i}", id.replace(['/', ' '], "_")))
+ .attr("StopColor", cid)
+ .attr("Location", pct(stop.offset as f64));
+ if i > 0 {
+ e.set("Midpoint", pct(gradient.stops[i - 1].midpoint as f64));
+ }
+ g.push(e);
+ }
+ g
+ }
+
// ---------- Graphic.xml ----------
fn graphic_part(&mut self) -> Vec<u8> {
@@ -440,39 +479,21 @@ impl<'a> Ex<'a> {
let mut tints: Vec<El> = Vec::new();
let mut gradients: Vec<El> = Vec::new();
let mut inks: Vec<El> = Vec::new();
- let color_el = |self_id: &str, name: &str, model: &str, c: &Color, over: &str, editable: bool, removable: bool, visible: bool| {
- let (space, val) = color_value(c);
- El::new("Color")
- .attr("Self", self_id)
- .attr("Model", model)
- .attr("Space", space)
- .attr("ColorValue", val)
- .attr("ColorOverride", over)
- .attr("AlternateSpace", "NoAlternateColor")
- .attr("AlternateColorValue", "")
- .attr("Name", name)
- .attr("ColorEditable", bool_s(editable))
- .attr("ColorRemovable", bool_s(removable))
- .attr("Visible", bool_s(visible))
- .attr("SwatchCreatorID", "7937")
- };
// Specials (always present).
- colors.push(color_el("Color/Black", "Black", "Process", &Color::cmyk(0.0, 0.0, 0.0, 1.0), "Specialblack", false, false, true));
+ colors.push(color_el("Color/Black", "Black", "Process", &Color::cmyk(0.0, 0.0, 0.0, 1.0), "Specialblack", (false, false, true)));
let paper = match d.swatch(swatch::PAPER).map(|s| &s.value) {
Some(SwatchValue::Paper { color }) => *color,
_ => Color::cmyk(0.0, 0.0, 0.0, 0.0),
};
let paper = if paper == Color::WHITE { Color::cmyk(0.0, 0.0, 0.0, 0.0) } else { paper };
- colors.push(color_el("Color/Paper", "Paper", "Process", &paper, "Specialpaper", true, false, true));
+ colors.push(color_el("Color/Paper", "Paper", "Process", &paper, "Specialpaper", (true, false, true)));
colors.push(color_el(
"Color/Registration",
"Registration",
"Registration",
&Color::cmyk(1.0, 1.0, 1.0, 1.0),
"Specialregistration",
- false,
- false,
- true,
+ (false, false, true),
));
for (i, ink) in
[("Process Cyan", 75, 0.61), ("Process Magenta", 15, 0.76), ("Process Yellow", 0, 0.16), ("Process Black", 45, 1.7)].iter().enumerate()
@@ -500,7 +521,7 @@ impl<'a> Ex<'a> {
match &s.value {
SwatchValue::Color { color, color_type } => {
let model = if *color_type == ColorType::Spot { "Spot" } else { "Process" };
- colors.push(color_el(&id, &s.name, model, color, "Normal", true, true, true));
+ colors.push(color_el(&id, &s.name, model, color, "Normal", (true, true, true)));
by_value.push((*color, id.clone()));
if *color_type == ColorType::Spot {
let mut ink = El::new("Ink");
@@ -542,35 +563,7 @@ impl<'a> Ex<'a> {
}
for s in &d.swatches {
let SwatchValue::Gradient { gradient } = &s.value else { continue };
- let id = self.sw(&s.name);
- let mut g = El::new("Gradient")
- .attr("Self", &id)
- .attr("Type", if gradient.kind == GradientKind::Radial { "Radial" } else { "Linear" })
- .attr("Name", &s.name)
- .attr("ColorEditable", "true")
- .attr("ColorRemovable", "true")
- .attr("Visible", "true")
- .attr("SwatchCreatorID", "7937");
- for (i, stop) in gradient.stops.iter().enumerate() {
- let cid = match by_value.iter().find(|(c, _)| *c == stop.color) {
- Some((_, id)) => id.clone(),
- None => {
- let cid = format!("Color/{}", self.fresh());
- colors.push(color_el(&cid, "$ID/", "Process", &stop.color, "Normal", true, true, false));
- by_value.push((stop.color, cid.clone()));
- cid
- }
- };
- let mut e = El::new("GradientStop")
- .attr("Self", format!("{}GradientStop{i}", id.replace(['/', ' '], "_")))
- .attr("StopColor", cid)
- .attr("Location", pct(stop.offset as f64));
- if i > 0 {
- e.set("Midpoint", pct(gradient.stops[i - 1].midpoint as f64));
- }
- g.push(e);
- }
- gradients.push(g);
+ gradients.push(self.gradient_el(s, gradient, &mut colors, &mut by_value));
}
for c in colors {
root.push(c);
@@ -3213,6 +3206,24 @@ fn layer_el(l: &designcraft_doc::Layer) -> El {
with_props(el, vec![color])
}
+/// A `Color` element. `flags` is (editable, removable, visible).
+fn color_el(self_id: &str, name: &str, model: &str, c: &Color, over: &str, (editable, removable, visible): (bool, bool, bool)) -> El {
+ let (space, val) = color_value(c);
+ El::new("Color")
+ .attr("Self", self_id)
+ .attr("Model", model)
+ .attr("Space", space)
+ .attr("ColorValue", val)
+ .attr("ColorOverride", over)
+ .attr("AlternateSpace", "NoAlternateColor")
+ .attr("AlternateColorValue", "")
+ .attr("Name", name)
+ .attr("ColorEditable", bool_s(editable))
+ .attr("ColorRemovable", bool_s(removable))
+ .attr("Visible", bool_s(visible))
+ .attr("SwatchCreatorID", "7937")
+}
+
/// A text variable definition with its type's settings element.
fn text_variable_el(v: &designcraft_doc::vars::TextVariable) -> El {
use designcraft_doc::vars::{Use, VarKind};
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index fed9f49b9..112622c89 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -2494,10 +2494,9 @@ impl<'r> Importer<'r> {
.collect()
}
- fn spread(&mut self, e: &El, id: SpreadId, master: bool) -> Spread {
- let facing = self.settings.facing_pages;
- // Page rectangles in IDML spread coordinates.
- let mut pages: Vec<(Rect, &El)> = Vec::new();
+ /// The page rectangles of a spread element, in IDML spread coordinates.
+ fn page_rects<'a>(&self, e: &'a El) -> Vec<(Rect, &'a El)> {
+ let mut pages: Vec<(Rect, &'a El)> = Vec::new();
for p in e.find_all("Page") {
let gb = nums(p.get("GeometricBounds").unwrap_or(""));
let (t, l, bt, r) =
@@ -2507,6 +2506,40 @@ impl<'r> Importer<'r> {
let c = xf * Point::new(r, bt);
pages.push((Rect::from_points(a, c), p));
}
+ pages
+ }
+
+ /// A page's margins and columns, and whether they are local (differ from its parent page's).
+ fn page_margins(&self, p: &El, r: &Rect, master: bool, parent: Option<SpreadId>, side: PageSide) -> (Margins, Columns, bool) {
+ // A document page's margins are local when they differ from its parent page's; a
+ // page without its own takes the parent page's.
+ let from_parent = if master {
+ None
+ } else {
+ parent
+ .and_then(|id| self.parents.iter().find(|sp| sp.id == id))
+ .and_then(|sp| sp.pages.get(sp.parent_page_for_side(side)?))
+ .map(|q| (q.margins, q.columns.clone()))
+ };
+ let (margins, columns) = match (p.find("MarginPreference"), &from_parent) {
+ (Some(m), _) => {
+ let (margins, columns) = margins_of(m);
+ (margins, fit_columns(columns, &margins, r.width()))
+ }
+ (None, Some(own)) => own.clone(),
+ (None, None) => {
+ let (margins, columns) = self.default_margins.clone().unwrap_or((Margins::uniform(36.0), Columns::default()));
+ (margins, fit_columns(columns, &margins, r.width()))
+ }
+ };
+ let local_margins = from_parent.is_some_and(|(m, c)| m != margins || c != columns);
+ (margins, columns, local_margins)
+ }
+
+ fn spread(&mut self, e: &El, id: SpreadId, master: bool) -> Spread {
+ let facing = self.settings.facing_pages;
+ // Page rectangles in IDML spread coordinates.
+ let pages = self.page_rects(e);
let min_x = pages.iter().map(|(r, _)| r.x0).fold(f64::INFINITY, f64::min);
let min_y = pages.iter().map(|(r, _)| r.y0).fold(f64::INFINITY, f64::min);
let (min_x, min_y) = if min_x.is_finite() { (min_x, min_y) } else { (0.0, 0.0) };
@@ -2533,28 +2566,7 @@ impl<'r> Importer<'r> {
PageSide::Right
};
let parent = p.get("AppliedMaster").and_then(|m| self.parent_ids.get(m)).copied();
- // A document page's margins are local when they differ from its parent page's; a
- // page without its own takes the parent page's.
- let from_parent = if master {
- None
- } else {
- parent
- .and_then(|id| self.parents.iter().find(|sp| sp.id == id))
- .and_then(|sp| sp.pages.get(sp.parent_page_for_side(side)?))
- .map(|q| (q.margins, q.columns.clone()))
- };
- let (margins, columns) = match (p.find("MarginPreference"), &from_parent) {
- (Some(m), _) => {
- let (margins, columns) = margins_of(m);
- (margins, fit_columns(columns, &margins, r.width()))
- }
- (None, Some(own)) => own.clone(),
- (None, None) => {
- let (margins, columns) = self.default_margins.clone().unwrap_or((Margins::uniform(36.0), Columns::default()));
- (margins, fit_columns(columns, &margins, r.width()))
- }
- };
- let local_margins = from_parent.is_some_and(|(m, c)| m != margins || c != columns);
+ let (margins, columns, local_margins) = self.page_margins(p, r, master, parent, side);
out_pages.push(Page {
id: pid,
width: r.width(),