mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-20 17:16:01 +00:00
Use shared IDML text frame preferences model
This commit is contained in:
3 files changed
+45
-76
No files matched your search
Generated
+11
-11
@@ -1242,10 +1242,10 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-idml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c205fc236#c205fc2360e4b3498e356a8fb8d763e2e54d7829"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a4432f087#a4432f08707fca90bf53128011c5dde0d87a2dfd"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c205fc236)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c205fc236)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a4432f087)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a4432f087)",
|
||||
"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=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a4432f087#a4432f08707fca90bf53128011c5dde0d87a2dfd"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1674,7 +1674,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c205fc236#c205fc2360e4b3498e356a8fb8d763e2e54d7829"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"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=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a4432f087#a4432f08707fca90bf53128011c5dde0d87a2dfd"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1750,7 +1750,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c205fc236#c205fc2360e4b3498e356a8fb8d763e2e54d7829"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf#ba24fe2435adb3c81ad6057faf36365fbba736cf"
|
||||
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=c205fc236)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a4432f087)",
|
||||
"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=c205fc236)",
|
||||
"craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a4432f087)",
|
||||
"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=c205fc236)",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a4432f087)",
|
||||
"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=c205fc236)",
|
||||
"craft-idml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a4432f087)",
|
||||
"craft-rtf",
|
||||
"craft-wordmodel",
|
||||
"craft-xlsx",
|
||||
|
||||
+2
-2
@@ -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 = "c205fc236", version = "=0.1.0" }
|
||||
craft-idml = { git = "https://github.com/storytold/craft-libs", rev = "a4432f087", 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 = "c205fc236", version = "=0.1.0" }
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "a4432f087", 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" }
|
||||
|
||||
@@ -76,73 +76,72 @@ pub(super) fn frame_grid_relative(v: &str) -> designcraft_doc::FrameGridRelative
|
||||
|
||||
/// Text Frame Options from `TextFramePreference` elements: each attribute from the first that sets it.
|
||||
pub(super) fn text_frame_options(prefs: &[&El]) -> TextFrameOptions {
|
||||
let e = Prefs(prefs);
|
||||
let elements: Vec<_> = prefs.iter().map(|pref| crate::xml::to_craft(pref)).collect();
|
||||
let model_elements: Vec<_> = elements.iter().collect();
|
||||
let e = craft_idml::model::TextFramePreferences::from_elements(&model_elements);
|
||||
let mut o = TextFrameOptions::default();
|
||||
if let Some(v) = e.num("TextColumnCount") {
|
||||
if let Some(v) = e.column_count {
|
||||
o.columns = v.clamp(1.0, 216.0) as u32;
|
||||
}
|
||||
if let Some(v) = e.num("TextColumnGutter") {
|
||||
if let Some(v) = e.column_gutter {
|
||||
o.gutter = v;
|
||||
}
|
||||
o.columns_kind = if e.prop("UseFixedColumnWidth").as_deref().map(str::trim) == Some("true") {
|
||||
o.columns_kind = if e.fixed_width {
|
||||
ColumnsKind::FixedWidth
|
||||
} else if e.prop("UseFlexibleColumnWidth").as_deref().map(str::trim) == Some("true") {
|
||||
} else if e.flexible_width {
|
||||
ColumnsKind::FlexibleWidth
|
||||
} else {
|
||||
ColumnsKind::FixedNumber
|
||||
};
|
||||
if o.columns_kind != ColumnsKind::FixedNumber
|
||||
&& let Some(v) = e.num("TextColumnFixedWidth")
|
||||
&& let Some(v) = e.fixed_column_width
|
||||
{
|
||||
o.column_width = v;
|
||||
}
|
||||
o.balance_columns = e.prop("VerticalBalanceColumns").as_deref().map(str::trim) == Some("true");
|
||||
if let Some(p) = prefs.iter().find(|p| p.prop("InsetSpacing").is_some())
|
||||
&& let Some(inset) = inset_spacing(p)
|
||||
{
|
||||
o.balance_columns = e.balance_columns;
|
||||
if let Some(inset) = e.inset {
|
||||
o.inset = inset;
|
||||
}
|
||||
if let Some(v) = e.prop("VerticalJustification") {
|
||||
if let Some(v) = e.vertical_justification.as_deref() {
|
||||
o.vertical_justification = names::vj_in(v.trim());
|
||||
}
|
||||
if let Some(v) = e.num("VerticalThreshold") {
|
||||
if let Some(v) = e.vertical_threshold {
|
||||
o.vj_paragraph_spacing_limit = v;
|
||||
}
|
||||
if let Some(v) = e.prop("FirstBaselineOffset") {
|
||||
if let Some(v) = e.first_baseline.as_deref() {
|
||||
o.first_baseline = names::first_baseline_in(v.trim());
|
||||
}
|
||||
if let Some(v) = e.num("MinimumFirstBaselineOffset") {
|
||||
if let Some(v) = e.minimum_first_baseline {
|
||||
o.first_baseline_min = v;
|
||||
}
|
||||
o.ignore_wrap = e.prop("IgnoreWrap").as_deref().map(str::trim) == Some("true");
|
||||
if let Some(v) = e.prop("AutoSizingType") {
|
||||
o.ignore_wrap = e.ignore_wrap;
|
||||
if let Some(v) = e.auto_sizing_type.as_deref() {
|
||||
o.auto_size = names::auto_size_in(v.trim());
|
||||
}
|
||||
if let Some(v) = e.prop("AutoSizingReferencePoint") {
|
||||
if let Some(v) = e.auto_sizing_reference.as_deref() {
|
||||
o.auto_size_ref = names::REF_POINTS.iter().position(|p| *p == v.trim()).unwrap_or(1) as u8;
|
||||
}
|
||||
let size = |k: &str| e.num(k).filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 10_000.0));
|
||||
o.auto_size_use_min_height = e.prop("UseMinimumHeightForAutoSizing").as_deref().map(str::trim) == Some("true");
|
||||
o.auto_size_min_height = size("MinimumHeightForAutoSizing").unwrap_or(0.0);
|
||||
o.auto_size_use_min_width = e.prop("UseMinimumWidthForAutoSizing").as_deref().map(str::trim) == Some("true");
|
||||
o.auto_size_min_width = size("MinimumWidthForAutoSizing").unwrap_or(0.0);
|
||||
o.auto_size_no_line_breaks = e.prop("UseNoLineBreaksForAutoSizing").as_deref().map(str::trim) == Some("true");
|
||||
let size = |v: Option<f64>| v.map(|v| v.clamp(0.0, 10_000.0));
|
||||
o.auto_size_use_min_height = e.use_minimum_height;
|
||||
o.auto_size_min_height = size(e.minimum_height).unwrap_or(0.0);
|
||||
o.auto_size_use_min_width = e.use_minimum_width;
|
||||
o.auto_size_min_width = size(e.minimum_width).unwrap_or(0.0);
|
||||
o.auto_size_no_line_breaks = e.no_line_breaks;
|
||||
// Column rules (the colour is a swatch reference: see `Importer::frame_options`).
|
||||
o.column_rule = e.prop("ColumnRuleOverride").as_deref().map(str::trim) == Some("true");
|
||||
let finite = |k: &str| e.num(k).filter(|v| v.is_finite());
|
||||
if let Some(v) = finite("ColumnRuleStrokeWidth") {
|
||||
o.column_rule = e.column_rule_override;
|
||||
if let Some(v) = e.column_rule_width {
|
||||
o.column_rule_weight = v.clamp(0.0, 1000.0);
|
||||
}
|
||||
if let Some(t) = tint(finite("ColumnRuleStrokeTint")) {
|
||||
if let Some(t) = tint(e.column_rule_tint) {
|
||||
o.column_rule_tint = t.clamp(0.0, 1.0);
|
||||
}
|
||||
for (k, v) in [
|
||||
("ColumnRuleOffset", &mut o.column_rule_offset),
|
||||
("ColumnRuleTopInset", &mut o.column_rule_top_inset),
|
||||
("ColumnRuleBottomInset", &mut o.column_rule_bottom_inset),
|
||||
for (value, slot) in [
|
||||
(e.column_rule_offset, &mut o.column_rule_offset),
|
||||
(e.column_rule_top_inset, &mut o.column_rule_top_inset),
|
||||
(e.column_rule_bottom_inset, &mut o.column_rule_bottom_inset),
|
||||
] {
|
||||
if let Some(x) = finite(k) {
|
||||
*v = x.clamp(-1440.0, 1440.0);
|
||||
if let Some(x) = value {
|
||||
*slot = x.clamp(-1440.0, 1440.0);
|
||||
}
|
||||
}
|
||||
o
|
||||
@@ -155,36 +154,6 @@ impl Prefs<'_> {
|
||||
pub(super) fn prop(&self, k: &str) -> Option<String> {
|
||||
self.0.iter().find_map(|e| e.prop(k))
|
||||
}
|
||||
fn num(&self, k: &str) -> Option<f64> {
|
||||
// The nearest preference wins; within it, attributes precede property children.
|
||||
self.prop(k).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite())
|
||||
}
|
||||
}
|
||||
|
||||
/// A scalar applies to every edge; a list is top, left, bottom, right. Keep the
|
||||
/// existing property-element precedence over the legacy scalar attribute, even
|
||||
/// when the property is malformed. Finite negative insets remain supported.
|
||||
fn inset_spacing(e: &El) -> Option<[f64; 4]> {
|
||||
let number = |s: &str| s.trim().parse::<f64>().ok().filter(|v| v.is_finite());
|
||||
let Some(property) = e.prop_el("InsetSpacing") else {
|
||||
return e.get("InsetSpacing").and_then(number).map(|v| [v; 4]);
|
||||
};
|
||||
let mut items = property.elements();
|
||||
if property.get("type") == Some("list") || property.find("ListItem").is_some() {
|
||||
let mut inset = [0.0; 4];
|
||||
for edge in &mut inset {
|
||||
let item = items.next()?;
|
||||
if item.local() != "ListItem" || item.elements().next().is_some() {
|
||||
return None;
|
||||
}
|
||||
*edge = number(&item.text_content())?;
|
||||
}
|
||||
items.next().is_none().then_some(inset)
|
||||
} else if items.next().is_none() {
|
||||
number(&property.text_content()).map(|v| [v; 4])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user