From c91887f49eb7ddbe350936e16aae13b46491bf33 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 08:02:41 -0500 Subject: [PATCH] Read Ruby properties through shared IDML model --- Cargo.lock | 22 +++++----- Cargo.toml | 4 +- crates/idml/src/import/text_attrs.rs | 8 ++-- crates/idml/src/ruby.rs | 66 +++++++++++++--------------- 4 files changed, 47 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4991a18b6..586a55e15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1242,10 +1242,10 @@ dependencies = [ [[package]] name = "craft-idml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76#aa2526750ec74b177191542bf2bc18b58231fe76" +source = "git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3#86651c65e6b084b10ff16002d9e0526efd6546d3" dependencies = [ - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76)", - "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3)", "thiserror 2.0.21", ] @@ -1656,7 +1656,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +source = "git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3#86651c65e6b084b10ff16002d9e0526efd6546d3" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1665,7 +1665,7 @@ dependencies = [ [[package]] name = "craft-xml" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76#aa2526750ec74b177191542bf2bc18b58231fe76" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ "quick-xml 0.41.0", "thiserror 2.0.21", @@ -1730,7 +1730,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" +source = "git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3#86651c65e6b084b10ff16002d9e0526efd6546d3" dependencies = [ "flate2", "thiserror 2.0.21", @@ -1740,7 +1740,7 @@ dependencies = [ [[package]] name = "craft-zip" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76#aa2526750ec74b177191542bf2bc18b58231fe76" +source = "git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551#8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551" dependencies = [ "flate2", "thiserror 2.0.21", @@ -2021,7 +2021,7 @@ dependencies = [ "craft-prepress", "craft-print", "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3)", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f23b0f33f0d00b157476533a43cbbcfd901aa1f1)", "designcraft-color", "designcraft-compose", @@ -2103,9 +2103,9 @@ name = "designcraft-idml" version = "0.20.1" dependencies = [ "craft-file-formats", - "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76)", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3)", "craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)", - "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76)", + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3)", "designcraft-color", "designcraft-compose", "designcraft-doc", @@ -2222,7 +2222,7 @@ version = "0.20.1" dependencies = [ "craft-docx", "craft-file-formats", - "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=aa2526750ec74b177191542bf2bc18b58231fe76)", + "craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=86651c65e6b084b10ff16002d9e0526efd6546d3)", "craft-rtf", "craft-wordmodel", "craft-xlsx", diff --git a/Cargo.toml b/Cargo.toml index 920f09cfb..48b28869c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,10 +16,10 @@ craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "4052591363 craft-designcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "19692b6190465671e41967c75d0cd4c439d0c84c", version = "=0.1.0" } craft-eps = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-epub = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } -craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "aa2526750ec74b177191542bf2bc18b58231fe76", version = "=0.1.0" } +craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "86651c65e6b084b10ff16002d9e0526efd6546d3", version = "=0.1.0" } craft-indd = { git = "https://github.com/storytold/craft-libs", rev = "47bf5650b0d503b97bfb5e841746c4957897d11f", version = "=0.1.0" } craft-svg = { git = "https://github.com/storytold/craft-libs", rev = "cc23ffd4d26f3074808e2187d332564817624f8f", version = "=0.1.0", features = ["tree-render", "tree-text"] } -craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "aa2526750ec74b177191542bf2bc18b58231fe76", version = "=0.1.0" } +craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "86651c65e6b084b10ff16002d9e0526efd6546d3", version = "=0.1.0" } craft-xml-dtd = { package = "craft-xml", git = "https://github.com/storytold/craft-libs", rev = "f23b0f33f0d00b157476533a43cbbcfd901aa1f1", version = "=0.1.0" } craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "ba24fe2435adb3c81ad6057faf36365fbba736cf", version = "=0.1.0" } diff --git a/crates/idml/src/import/text_attrs.rs b/crates/idml/src/import/text_attrs.rs index a0522086e..25c90f862 100644 --- a/crates/idml/src/import/text_attrs.rs +++ b/crates/idml/src/import/text_attrs.rs @@ -121,11 +121,11 @@ impl<'r> Importer<'r> { a.warichu_alignment = e.prop("WarichuAlignment").as_deref().and_then(crate::cjk::warichu_align_in); a.warichu_chars_before_break = e.num("WarichuCharsBeforeBreak").map(|v| v.max(0.0) as u32); a.warichu_chars_after_break = e.num("WarichuCharsAfterBreak").map(|v| v.max(0.0) as u32); - // InDesign writes the flag as a number (1); older writers wrote a boolean. - if let Some(on) = e.boolean("RubyFlag").or_else(|| e.num("RubyFlag").map(|v| v != 0.0)) { - a.ruby = Some(if on { e.prop("RubyString").unwrap_or_default() } else { String::new() }); + let ruby_values = craft_idml::model::RubyValues::from_el(&crate::xml::to_craft(e)); + if let Some(on) = ruby_values.flag { + a.ruby = Some(if on { ruby_values.text.clone().unwrap_or_default() } else { String::new() }); } - crate::ruby::read(e, &mut a, |r| self.swatch_ref(r)); + crate::ruby::read_values(&ruby_values, &mut a, |r| self.swatch_ref(r)); if let Some(k) = e.get("KentenKind") { a.kenten = Some(k != "None"); a.kenten_character = crate::cjk::kenten_character(k).map(str::to_string); diff --git a/crates/idml/src/ruby.rs b/crates/idml/src/ruby.rs index 352da63a1..dc4f8878e 100644 --- a/crates/idml/src/ruby.rs +++ b/crates/idml/src/ruby.rs @@ -50,52 +50,46 @@ fn name(table: &[(T, &'static str)], v: T) -> Option<&'stat table.iter().find(|(t, _)| *t == v).map(|(_, n)| *n) } -fn finite(e: &El, k: &str) -> Option { - e.num(k).filter(|v| v.is_finite()) -} - -/// Read the ruby settings of a style or range element into `a`. `swatch` maps a swatch -/// reference to the document's swatch name. -pub(crate) fn read(e: &El, a: &mut CharAttrs, mut swatch: impl FnMut(&str) -> String) { - a.ruby_type = e.prop("RubyType").and_then(|v| lookup(TYPES, &v)); - a.ruby_alignment = e.prop("RubyAlignment").and_then(|v| lookup(ALIGNMENTS, &v)); - a.ruby_position = e.prop("RubyPosition").and_then(|v| lookup(POSITIONS, &v)); - a.ruby_x_offset = finite(e, "RubyXOffset").map(|v| v.clamp(-1000.0, 1000.0)); - a.ruby_y_offset = finite(e, "RubyYOffset").map(|v| v.clamp(-1000.0, 1000.0)); - if let Some(f) = e.prop("RubyFont") { +/// Read neutral Ruby values into `a`. `swatch` maps a swatch reference to the document's swatch name. +pub(crate) fn read_values(values: &craft_idml::model::RubyValues, a: &mut CharAttrs, mut swatch: impl FnMut(&str) -> String) { + a.ruby_type = values.kind.as_deref().and_then(|value| lookup(TYPES, value)); + a.ruby_alignment = values.alignment.as_deref().and_then(|value| lookup(ALIGNMENTS, value)); + a.ruby_position = values.position.as_deref().and_then(|value| lookup(POSITIONS, value)); + a.ruby_x_offset = values.x_offset.map(|v| v.clamp(-1000.0, 1000.0)); + a.ruby_y_offset = values.y_offset.map(|v| v.clamp(-1000.0, 1000.0)); + if let Some(f) = &values.font { let f = f.trim(); let fam = f.split('\t').next().unwrap_or(f); a.ruby_font = Some(if fam == "$ID/" { String::new() } else { fam.to_string() }); } - if let Some(s) = e.prop("RubyFontStyle") { + if let Some(s) = &values.font_style { let s = s.trim(); a.ruby_font_style = Some(if s == "Nothing" { String::new() } else { s.to_string() }); } // -1: automatic (half the parent size). - a.ruby_font_size = finite(e, "RubyFontSize").map(|v| (v > 0.0).then_some(v.clamp(0.1, 1296.0))); - a.ruby_x_scale = finite(e, "RubyXScale").map(|v| (v / 100.0).clamp(0.01, 10.0)); - a.ruby_y_scale = finite(e, "RubyYScale").map(|v| (v / 100.0).clamp(0.01, 10.0)); - a.ruby_open_type_pro = e.boolean("RubyOpenTypePro"); - a.ruby_auto_tcy_digits = finite(e, "RubyAutoTcyDigits").map(|v| v.clamp(0.0, 9.0) as u32); - a.ruby_auto_tcy_include_roman = e.boolean("RubyAutoTcyIncludeRoman"); - a.ruby_auto_tcy_auto_scale = e.boolean("RubyAutoTcyAutoScale"); - a.ruby_overhang = e.boolean("RubyOverhang"); - a.ruby_overhang_amount = e.prop("RubyParentOverhangAmount").and_then(|v| lookup(OVERHANGS, &v)); - a.ruby_parent_spacing = e.prop("RubyParentSpacing").and_then(|v| lookup(SPACINGS, &v)); - a.ruby_auto_align = e.boolean("RubyAutoAlign"); - a.ruby_auto_scaling = e.boolean("RubyAutoScaling"); - a.ruby_scaling_min = finite(e, "RubyParentScalingPercent").map(|v| (v / 100.0).clamp(0.1, 1.0)); - let color = |r: String, swatch: &mut dyn FnMut(&str) -> String| if r.trim() == TEXT_COLOR { String::new() } else { swatch(r.trim()) }; - a.ruby_fill = e.prop("RubyFill").map(|r| color(r, &mut swatch)); - a.ruby_stroke = e.prop("RubyStroke").map(|r| color(r, &mut swatch)); + a.ruby_font_size = values.font_size.map(|v| (v > 0.0).then_some(v.clamp(0.1, 1296.0))); + a.ruby_x_scale = values.x_scale.map(|v| (v / 100.0).clamp(0.01, 10.0)); + a.ruby_y_scale = values.y_scale.map(|v| (v / 100.0).clamp(0.01, 10.0)); + a.ruby_open_type_pro = values.open_type_pro; + a.ruby_auto_tcy_digits = values.auto_tcy_digits.map(|v| v.clamp(0.0, 9.0) as u32); + a.ruby_auto_tcy_include_roman = values.auto_tcy_include_roman; + a.ruby_auto_tcy_auto_scale = values.auto_tcy_auto_scale; + a.ruby_overhang = values.overhang; + a.ruby_overhang_amount = values.parent_overhang.as_deref().and_then(|value| lookup(OVERHANGS, value)); + a.ruby_parent_spacing = values.parent_spacing.as_deref().and_then(|value| lookup(SPACINGS, value)); + a.ruby_auto_align = values.auto_align; + a.ruby_auto_scaling = values.auto_scaling; + a.ruby_scaling_min = values.parent_scaling.map(|v| (v / 100.0).clamp(0.1, 1.0)); + let color = |r: &str, swatch: &mut dyn FnMut(&str) -> String| if r.trim() == TEXT_COLOR { String::new() } else { swatch(r.trim()) }; + a.ruby_fill = values.fill.as_deref().map(|r| color(r, &mut swatch)); + a.ruby_stroke = values.stroke.as_deref().map(|r| color(r, &mut swatch)); // -1: automatic (the text's tint, stroke weight). let tint = |v: f64| (v >= 0.0).then_some((v / 100.0).clamp(0.0, 1.0) as f32); - a.ruby_fill_tint = finite(e, "RubyTint").map(tint); - a.ruby_stroke_tint = finite(e, "RubyStrokeTint").map(tint); - a.ruby_stroke_weight = finite(e, "RubyWeight").map(|v| (v >= 0.0).then_some(v.min(800.0))); - let overprint = |k: &str| e.prop(k).and_then(|v| overprint_in(&v)); - a.ruby_overprint_fill = overprint("RubyOverprintFill"); - a.ruby_overprint_stroke = overprint("RubyOverprintStroke"); + a.ruby_fill_tint = values.fill_tint.map(tint); + a.ruby_stroke_tint = values.stroke_tint.map(tint); + a.ruby_stroke_weight = values.stroke_weight.map(|v| (v >= 0.0).then_some(v.min(800.0))); + a.ruby_overprint_fill = values.overprint_fill.as_deref().and_then(overprint_in); + a.ruby_overprint_stroke = values.overprint_stroke.as_deref().and_then(overprint_in); } /// Ruby parent-text options (OpenType Pro, auto tate-chu-yoko, overhang, spacing, scaling).