Skip to content

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

refactor(idml): extract helpers to cut cognitive complexity (Sonar S3776) (#1723)

Dylan M. Taylor75d5b015d38a
diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs
index ef692bfa4..c174c1889 100644
--- a/crates/idml/src/export.rs
+++ b/crates/idml/src/export.rs
@@ -309,28 +309,7 @@ impl<'a> Ex<'a> {
);
}
for t in &d.styles.mojikumi_tables {
- let mut e = El::new("MojikumiTable").attr("Self", format!("MojikumiTable/{}", escape_id(&t.name))).attr("Name", &t.name);
- if !t.based_on.is_empty() {
- e.set("BasedOnMojikumiSet", &t.based_on);
- }
- if !t.overrides.is_empty() {
- let mut list = El::new("OverrideMojikumiAkiList");
- for r in &t.overrides {
- list.push(
- El::new("OverrideMojikumiAkiType")
- .attr("TargetMojikumiClass", r.target_class)
- .attr("SideMojikumiClass", r.side_class)
- .attr("SideIsAfterTarget", bool_s(r.after))
- .attr("Minimum", num(r.minimum))
- .attr("Desired", num(r.desired))
- .attr("Maximum", num(r.maximum))
- .attr("CompressionPriority", r.priority)
- .attr("AkiDoesNotFloat", bool_s(r.does_not_float)),
- );
- }
- e.push(El::new("Properties").child(list));
- }
- root.push(e);
+ root.push(mojikumi_table_el(t));
}
root.push(El::new("idPkg:Graphic").attr("src", "Resources/Graphic.xml"));
root.push(El::new("idPkg:Fonts").attr("src", "Resources/Fonts.xml"));
@@ -364,28 +343,7 @@ impl<'a> Ex<'a> {
}
// IDML lists layers back to front.
for l in d.layers.iter().rev() {
- let color = match names::layer_color_out(l.color) {
- Some(n) => p("LayerColor", "enumeration", n),
- None => {
- let mut e = El::new("LayerColor").attr("type", "list");
- for c in l.color {
- e.push(p("ListItem", "double", num(c as f64)));
- }
- e
- }
- };
- let el = El::new("Layer")
- .attr("Self", uid(l.id.0))
- .attr("Name", &l.name)
- .attr("Visible", bool_s(l.visible))
- .attr("Locked", bool_s(l.locked))
- .attr("IgnoreWrap", bool_s(l.suppress_wrap_when_hidden))
- .attr("ShowGuides", bool_s(l.show_guides))
- .attr("LockGuides", "false")
- .attr("UI", "true")
- .attr("Expendable", "true")
- .attr("Printable", bool_s(l.printable));
- root.push(with_props(el, vec![color]));
+ root.push(layer_el(l));
}
for (name, _) in masters {
root.push(El::new("idPkg:MasterSpread").attr("src", name));
@@ -1828,33 +1786,7 @@ impl<'a> Ex<'a> {
.attr("ItemTransform", format!("1 0 0 1 {} {}", num(pg.x - ox), num(-oy)))
.child(margin_el(pg));
let mut pe = pe;
- // Ruler guides, measured from the ruler origin written in the preferences: the
- // spread's top left, the page's, or the spine (the import reads them the same way).
- let left = sp.pages.iter().map(|p| p.x).fold(f64::INFINITY, f64::min);
- let left = if left.is_finite() { left } else { 0.0 };
- let spine = sp.pages.iter().map(|p| p.x - left).filter(|x| *x > 1e-6).fold(f64::INFINITY, f64::min);
- let spine = if spine.is_finite() && facing { spine } else { 0.0 };
- let origin_x = match d.settings.ruler_origin {
- designcraft_doc::RulerOrigin::Spread => left,
- designcraft_doc::RulerOrigin::Page => pg.x,
- designcraft_doc::RulerOrigin::Spine => left + spine,
- };
- for g in &pg.guides {
- let vertical = g.orientation == designcraft_doc::Orientation::Vertical;
- let mut ge = El::new("Guide")
- .attr("Self", format!("{}g{}", uid(pg.id.0), pe.elements().count()))
- .attr("Orientation", if vertical { "Vertical" } else { "Horizontal" })
- .attr("Location", num(if vertical { g.position - origin_x } else { g.position }))
- .attr("FitToPage", bool_s(!g.spread))
- .attr("ViewThreshold", "5")
- .attr("Locked", bool_s(g.locked))
- .attr("PageIndex", (first_page + i).to_string())
- .attr("GuideType", if g.liquid { "Liquid" } else { "Ruler" });
- if let Some(l) = g.layer {
- ge.set("ItemLayer", uid(l.0));
- }
- pe.push(ge);
- }
+ push_page_guides(d, sp, pg, &mut pe, first_page + i);
el.push(pe);
}
let origin = Affine::translate((-ox, -oy));
@@ -3197,6 +3129,90 @@ fn metadata_xmp(d: &Document) -> String {
)
}
+/// The page's ruler guides, measured from the ruler origin written in the preferences.
+fn push_page_guides(d: &Document, sp: &Spread, pg: &designcraft_doc::Page, pe: &mut El, page_index: usize) {
+ let facing = d.settings.facing_pages;
+ // Ruler guides, measured from the ruler origin written in the preferences: the
+ // spread's top left, the page's, or the spine (the import reads them the same way).
+ let left = sp.pages.iter().map(|p| p.x).fold(f64::INFINITY, f64::min);
+ let left = if left.is_finite() { left } else { 0.0 };
+ let spine = sp.pages.iter().map(|p| p.x - left).filter(|x| *x > 1e-6).fold(f64::INFINITY, f64::min);
+ let spine = if spine.is_finite() && facing { spine } else { 0.0 };
+ let origin_x = match d.settings.ruler_origin {
+ designcraft_doc::RulerOrigin::Spread => left,
+ designcraft_doc::RulerOrigin::Page => pg.x,
+ designcraft_doc::RulerOrigin::Spine => left + spine,
+ };
+ for g in &pg.guides {
+ let vertical = g.orientation == designcraft_doc::Orientation::Vertical;
+ let mut ge = El::new("Guide")
+ .attr("Self", format!("{}g{}", uid(pg.id.0), pe.elements().count()))
+ .attr("Orientation", if vertical { "Vertical" } else { "Horizontal" })
+ .attr("Location", num(if vertical { g.position - origin_x } else { g.position }))
+ .attr("FitToPage", bool_s(!g.spread))
+ .attr("ViewThreshold", "5")
+ .attr("Locked", bool_s(g.locked))
+ .attr("PageIndex", page_index.to_string())
+ .attr("GuideType", if g.liquid { "Liquid" } else { "Ruler" });
+ if let Some(l) = g.layer {
+ ge.set("ItemLayer", uid(l.0));
+ }
+ pe.push(ge);
+ }
+}
+
+/// A `MojikumiTable` element with its spacing overrides.
+fn mojikumi_table_el(t: &designcraft_doc::cjk::MojikumiTable) -> El {
+ let mut e = El::new("MojikumiTable").attr("Self", format!("MojikumiTable/{}", escape_id(&t.name))).attr("Name", &t.name);
+ if !t.based_on.is_empty() {
+ e.set("BasedOnMojikumiSet", &t.based_on);
+ }
+ if !t.overrides.is_empty() {
+ let mut list = El::new("OverrideMojikumiAkiList");
+ for r in &t.overrides {
+ list.push(
+ El::new("OverrideMojikumiAkiType")
+ .attr("TargetMojikumiClass", r.target_class)
+ .attr("SideMojikumiClass", r.side_class)
+ .attr("SideIsAfterTarget", bool_s(r.after))
+ .attr("Minimum", num(r.minimum))
+ .attr("Desired", num(r.desired))
+ .attr("Maximum", num(r.maximum))
+ .attr("CompressionPriority", r.priority)
+ .attr("AkiDoesNotFloat", bool_s(r.does_not_float)),
+ );
+ }
+ e.push(El::new("Properties").child(list));
+ }
+ e
+}
+
+/// A `Layer` element with its colour property.
+fn layer_el(l: &designcraft_doc::Layer) -> El {
+ let color = match names::layer_color_out(l.color) {
+ Some(n) => p("LayerColor", "enumeration", n),
+ None => {
+ let mut e = El::new("LayerColor").attr("type", "list");
+ for c in l.color {
+ e.push(p("ListItem", "double", num(c as f64)));
+ }
+ e
+ }
+ };
+ let el = El::new("Layer")
+ .attr("Self", uid(l.id.0))
+ .attr("Name", &l.name)
+ .attr("Visible", bool_s(l.visible))
+ .attr("Locked", bool_s(l.locked))
+ .attr("IgnoreWrap", bool_s(l.suppress_wrap_when_hidden))
+ .attr("ShowGuides", bool_s(l.show_guides))
+ .attr("LockGuides", "false")
+ .attr("UI", "true")
+ .attr("Expendable", "true")
+ .attr("Printable", bool_s(l.printable));
+ with_props(el, vec![color])
+}
+
/// 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};
@@ -3338,6 +3354,10 @@ impl Ex<'_> {
if let Some(v) = a.kenten_overprint_stroke {
el.set("KentenOverprintStroke", crate::cjk::overprint_out(v));
}
+ self.shatai_chars(el, a);
+ }
+
+ fn shatai_chars(&self, el: &mut El, a: &CharAttrs) {
if let Some(v) = a.shatai_magnification {
el.set("ShataiMagnification", num(v));
}
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index 039d1e562..fed9f49b9 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -1641,28 +1641,7 @@ impl<'r> Importer<'r> {
a.strikethrough_tint = tint;
}
}
- // OpenType features (only when the element sets any).
- let mut otf_list: Vec<String> = Vec::new();
- let mut any = false;
- for (tag, _, attr) in designcraft_doc::otf::TOGGLES {
- if let Some(on) = e.boolean(attr) {
- designcraft_doc::otf::set(&mut otf_list, tag, on);
- any = true;
- }
- }
- if let Some(fig) = e.prop("OTFFigureStyle")
- && let Some((id, ..)) = designcraft_doc::otf::FIGURES.iter().find(|f| f.3 == fig.trim())
- {
- designcraft_doc::otf::set_figures(&mut otf_list, id);
- any = true;
- }
- if let Some(m) = e.num("OTFStylisticSets") {
- designcraft_doc::otf::set_stylistic_sets(&mut otf_list, m.max(0.0) as u32);
- any = true;
- }
- if any {
- a.otf_features = Some(otf_list);
- }
+ read_otf_features(e, &mut a);
a.glyph_form = e.prop("GlyphForm");
a.no_break = e.boolean("NoBreak");
a.tate_chu_yoko = e.boolean("Tatechuyoko");
@@ -3889,6 +3868,31 @@ impl Importer<'_> {
}
}
+/// OpenType features (only when the element sets any).
+fn read_otf_features(e: &El, a: &mut CharAttrs) {
+ let mut otf_list: Vec<String> = Vec::new();
+ let mut any = false;
+ for (tag, _, attr) in designcraft_doc::otf::TOGGLES {
+ if let Some(on) = e.boolean(attr) {
+ designcraft_doc::otf::set(&mut otf_list, tag, on);
+ any = true;
+ }
+ }
+ if let Some(fig) = e.prop("OTFFigureStyle")
+ && let Some((id, ..)) = designcraft_doc::otf::FIGURES.iter().find(|f| f.3 == fig.trim())
+ {
+ designcraft_doc::otf::set_figures(&mut otf_list, id);
+ any = true;
+ }
+ if let Some(m) = e.num("OTFStylisticSets") {
+ designcraft_doc::otf::set_stylistic_sets(&mut otf_list, m.max(0.0) as u32);
+ any = true;
+ }
+ if any {
+ a.otf_features = Some(otf_list);
+ }
+}
+
#[cfg(test)]
mod numeric_property_tests {
use super::*;
diff --git a/crates/idml/src/ruby.rs b/crates/idml/src/ruby.rs
index 601792f6c..352da63a1 100644
--- a/crates/idml/src/ruby.rs
+++ b/crates/idml/src/ruby.rs
@@ -98,11 +98,45 @@ pub(crate) fn read(e: &El, a: &mut CharAttrs, mut swatch: impl FnMut(&str) -> St
a.ruby_overprint_stroke = overprint("RubyOverprintStroke");
}
+/// Ruby parent-text options (OpenType Pro, auto tate-chu-yoko, overhang, spacing, scaling).
+fn write_parent_options(el: &mut El, a: &CharAttrs) {
+ let b = |v: bool| if v { "true" } else { "false" };
+ if let Some(v) = a.ruby_open_type_pro {
+ el.set("RubyOpenTypePro", b(v));
+ }
+ if let Some(v) = a.ruby_auto_tcy_digits {
+ el.set("RubyAutoTcyDigits", v);
+ }
+ if let Some(v) = a.ruby_auto_tcy_include_roman {
+ el.set("RubyAutoTcyIncludeRoman", b(v));
+ }
+ if let Some(v) = a.ruby_auto_tcy_auto_scale {
+ el.set("RubyAutoTcyAutoScale", b(v));
+ }
+ if let Some(v) = a.ruby_overhang {
+ el.set("RubyOverhang", b(v));
+ }
+ if let Some(v) = a.ruby_overhang_amount.and_then(|v| name(OVERHANGS, v)) {
+ el.set("RubyParentOverhangAmount", v);
+ }
+ if let Some(v) = a.ruby_parent_spacing.and_then(|v| name(SPACINGS, v)) {
+ el.set("RubyParentSpacing", v);
+ }
+ if let Some(v) = a.ruby_auto_align {
+ el.set("RubyAutoAlign", b(v));
+ }
+ if let Some(v) = a.ruby_auto_scaling {
+ el.set("RubyAutoScaling", b(v));
+ }
+ if let Some(v) = a.ruby_scaling_min {
+ el.set("RubyParentScalingPercent", num(v * 100.0));
+ }
+}
+
/// Write the ruby settings set in `a`: attributes on `el`, fonts and colours as `props`. `sw`
/// maps a swatch name to its IDML reference. Returns the ruby font (family, style) to list in
/// the fonts part.
pub(crate) fn write(el: &mut El, props: &mut Vec<El>, a: &CharAttrs, sw: impl Fn(&str) -> String) -> Option<(String, String)> {
- let b = |v: bool| if v { "true" } else { "false" };
let prop = |k: &str, ty: &str, v: String| El::new(k).attr("type", ty).text(v);
if let Some(v) = a.ruby_type.and_then(|v| name(TYPES, v)) {
el.set("RubyType", v);
@@ -134,36 +168,7 @@ pub(crate) fn write(el: &mut El, props: &mut Vec<El>, a: &CharAttrs, sw: impl Fn
if let Some(v) = a.ruby_y_scale {
el.set("RubyYScale", num(v * 100.0));
}
- if let Some(v) = a.ruby_open_type_pro {
- el.set("RubyOpenTypePro", b(v));
- }
- if let Some(v) = a.ruby_auto_tcy_digits {
- el.set("RubyAutoTcyDigits", v);
- }
- if let Some(v) = a.ruby_auto_tcy_include_roman {
- el.set("RubyAutoTcyIncludeRoman", b(v));
- }
- if let Some(v) = a.ruby_auto_tcy_auto_scale {
- el.set("RubyAutoTcyAutoScale", b(v));
- }
- if let Some(v) = a.ruby_overhang {
- el.set("RubyOverhang", b(v));
- }
- if let Some(v) = a.ruby_overhang_amount.and_then(|v| name(OVERHANGS, v)) {
- el.set("RubyParentOverhangAmount", v);
- }
- if let Some(v) = a.ruby_parent_spacing.and_then(|v| name(SPACINGS, v)) {
- el.set("RubyParentSpacing", v);
- }
- if let Some(v) = a.ruby_auto_align {
- el.set("RubyAutoAlign", b(v));
- }
- if let Some(v) = a.ruby_auto_scaling {
- el.set("RubyAutoScaling", b(v));
- }
- if let Some(v) = a.ruby_scaling_min {
- el.set("RubyParentScalingPercent", num(v * 100.0));
- }
+ write_parent_options(el, a);
for (k, c) in [("RubyFill", &a.ruby_fill), ("RubyStroke", &a.ruby_stroke)] {
if let Some(c) = c {
props.push(if c.is_empty() { prop(k, "string", TEXT_COLOR.into()) } else { prop(k, "object", sw(c.as_str())) });