Merge pull request #3267

This commit is contained in:
autocraft-bot[bot] committed 2026-12-02 19:52:47 +00:00
commit d4de53a18f
8 files changed
+134 -41

No files matched your search

+58 -4
View File
@@ -25,12 +25,43 @@ pub(super) fn pct(v: f64) -> String {
num(v * 100.0)
}
/// (model, space, value) for a colour.
/// Use the coarsest decimal that still imports as the original normalized channel.
fn color_units(value: f32, scale: f64) -> f64 {
if !value.is_finite() || value == 0.0 {
return 0.0;
}
let scaled = f64::from(value) * scale;
let mut factor = 1.0;
for _ in 0..=9 {
let rounded = (scaled * factor).round() / factor;
if (rounded / scale) as f32 == value {
return rounded;
}
factor *= 10.0;
}
scaled
}
pub(super) fn color_percent(value: f32) -> f64 {
color_units(value, 100.0)
}
pub(super) fn tint_value(value: f32) -> String {
color_percent(value).to_string()
}
/// (space, value) for a colour.
pub(super) fn color_value(c: &Color) -> (&'static str, String) {
match *c {
Color::Cmyk { c, m, y, k } => ("CMYK", format!("{} {} {} {}", pct(c as f64), pct(m as f64), pct(y as f64), pct(k as f64))),
Color::Rgb { r, g, b } => ("RGB", format!("{} {} {}", num(r as f64 * 255.0), num(g as f64 * 255.0), num(b as f64 * 255.0))),
Color::Gray { k } => ("CMYK", format!("0 0 0 {}", pct(k as f64))),
Color::Cmyk { c, m, y, k } => {
let [c, m, y, k] = [c, m, y, k].map(color_percent);
("CMYK", format!("{c} {m} {y} {k}"))
}
Color::Rgb { r, g, b } => {
let [r, g, b] = [r, g, b].map(|v| color_units(v, 255.0));
("RGB", format!("{r} {g} {b}"))
}
Color::Gray { k } => ("CMYK", format!("0 0 0 {}", color_percent(k))),
}
}
@@ -50,3 +81,26 @@ pub(super) fn change_type(mark: Option<designcraft_doc::ChangeMark>) -> Option<&
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_and_tint_serialize_special_values_as_zero() {
for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -0.0] {
assert_eq!(color_value(&Color::Rgb { r: value, g: 127.125 / 255.0, b: value }), ("RGB", "0 127.125 0".to_owned()));
assert_eq!(color_value(&Color::Cmyk { c: value, m: 0.85, y: 0.9, k: value }), ("CMYK", "0 85 90 0".to_owned()));
assert_eq!(color_value(&Color::Gray { k: value }), ("CMYK", "0 0 0 0".to_owned()));
assert_eq!(tint_value(value), "0");
}
assert_eq!(tint_value(0.4), "40");
}
#[test]
fn tiny_tints_keep_their_round_trip_precision() {
let tint = 1e-12_f32;
let exported = tint_value(tint).parse::<f64>().unwrap();
assert_eq!((exported / 100.0) as f32, tint);
}
}
+2 -2
View File
@@ -1,4 +1,4 @@
use super::common::{color_value, pct};
use super::common::{color_value, pct, tint_value};
use super::{Color, ColorType, Ex, GradientKind, SwatchValue, escape_id, num, swatch};
use craft_idml::model::{
ColorDefinition, ColorGroupDefinition, ColorGroupMember, GradientDefinition, GradientStopDefinition, GraphicPart, InkDefinition,
@@ -107,7 +107,7 @@ impl<'a> Ex<'a> {
}
}
SwatchValue::Tint { base, tint } => {
graphic.tints.push(TintDefinition { self_id: id, value: pct(*tint as f64), base_color: self.sw(base), name: s.name.clone() });
graphic.tints.push(TintDefinition { self_id: id, value: tint_value(*tint), base_color: self.sw(base), name: s.name.clone() });
}
_ => {}
}
+4 -4
View File
@@ -1,4 +1,4 @@
use super::common::{affine_s, bool_s, pct, uid, with_props};
use super::common::{affine_s, bool_s, color_percent, tint_value, uid, with_props};
use super::{Affine, ColumnsKind, Content, El, Ex, Item, Node, OBJECT_BUILTINS, PathData, Shape, TextFrameOptions, base64_encode, names, num, pt};
impl<'a> Ex<'a> {
@@ -61,7 +61,7 @@ impl<'a> Ex<'a> {
el.set("AppliedObjectStyle", names::style_self("ObjectStyle", OBJECT_BUILTINS, os));
el.set("ItemTransform", affine_s(xf));
el.set("FillColor", self.sw(&it.fill.swatch));
el.set("FillTint", pct(it.fill.tint as f64));
el.set("FillTint", tint_value(it.fill.tint));
el.set("OverprintFill", bool_s(it.fill.overprint));
if let Some([x0, y0, x1, y1]) = it.fill.gradient_vector {
// Gradient Swatch tool vector: start, length and angle in the item's space.
@@ -325,7 +325,7 @@ impl<'a> Ex<'a> {
&& let Some(id) = self.swatch_ids.get(&g.fill.swatch)
{
el.set("FillColor", id.clone());
el.set("FillTint", pct(g.fill.tint as f64));
el.set("FillTint", tint_value(g.fill.tint));
}
let wanted = !data.is_empty() && embedding;
// An image with no link of its own has nowhere else to live, so it is always embedded
@@ -435,7 +435,7 @@ pub(super) fn text_frame_pref(o: &TextFrameOptions, rule_color: &str) -> El {
no_line_breaks: o.auto_size_no_line_breaks,
column_rule_override: o.column_rule,
column_rule_width: Some(o.column_rule_weight),
column_rule_tint: Some(f64::from(o.column_rule_tint) * 100.0),
column_rule_tint: Some(color_percent(o.column_rule_tint)),
column_rule_offset: Some(o.column_rule_offset),
column_rule_top_inset: Some(o.column_rule_top_inset),
column_rule_bottom_inset: Some(o.column_rule_bottom_inset),
+2 -2
View File
@@ -1,4 +1,4 @@
use super::common::{change_type, uid, with_props, xref_format_self};
use super::common::{change_type, color_percent, uid, with_props, xref_format_self};
use super::{CHAR_BUILTINS, CharFormat, El, Ex, ListType, NO_CHAR_ID, Node, PARA_BUILTINS, ParaAttrs, ParaFormat, Story, TOC_STYLE_ID, names, st};
impl<'a> Ex<'a> {
@@ -53,7 +53,7 @@ impl<'a> Ex<'a> {
span_columns: Some(o.span_columns),
rule_on: Some(o.rule.on),
rule_weight: Some(o.rule.weight),
rule_tint: Some(f64::from(o.rule.tint) * 100.0),
rule_tint: Some(color_percent(o.rule.tint)),
rule_left_indent: Some(o.rule.left_indent),
rule_width: Some(o.rule.width),
rule_offset: Some(o.rule.offset),
+15 -15
View File
@@ -1,4 +1,4 @@
use super::common::{bool_s, p, pct, pkg_root, with_props};
use super::common::{bool_s, color_percent, p, pkg_root, tint_value, with_props};
use super::item::text_frame_pref;
use super::story::with_list_tab;
use super::{CHAR_BUILTINS, El, Ex, NO_PARA_ID, OBJECT_BUILTINS, PARA_BUILTINS, Rule, TOC_STYLE_ID, TOC_STYLE_NAME, document, names, num, st};
@@ -120,7 +120,7 @@ impl<'a> Ex<'a> {
}
if let Some(f) = &os.fill {
el.set("FillColor", self.sw(&f.swatch));
el.set("FillTint", pct(f.tint as f64));
el.set("FillTint", tint_value(f.tint));
}
if let Some(s) = &os.stroke {
self.stroke_attrs(&mut el, s);
@@ -162,13 +162,13 @@ impl<'a> Ex<'a> {
let mut out = craft_idml::model::StrokeValues {
weight: Some(s.weight),
color: Some(self.sw(&s.color)),
tint: Some(f64::from(s.tint) * 100.0),
tint: Some(color_percent(s.tint)),
kind: Some(names::stroke_type_out(&s.kind)),
..craft_idml::model::StrokeValues::default()
};
if s.gap_color != designcraft_color::swatch::NONE || s.gap_tint != 1.0 {
out.gap_color = Some(self.sw(&s.gap_color));
out.gap_tint = Some(f64::from(s.gap_tint) * 100.0);
out.gap_tint = Some(color_percent(s.gap_tint));
}
out
}
@@ -181,7 +181,7 @@ impl<'a> Ex<'a> {
out.color = Some(self.sw(value));
}
if let Some(value) = attrs.tint {
out.tint = Some(f64::from(value) * 100.0);
out.tint = Some(color_percent(value));
}
if let Some(value) = &attrs.kind {
out.kind = Some(names::stroke_type_out(value));
@@ -190,7 +190,7 @@ impl<'a> Ex<'a> {
out.gap_color = Some(self.sw(value));
}
if let Some(value) = attrs.gap_tint {
out.gap_tint = Some(f64::from(value) * 100.0);
out.gap_tint = Some(color_percent(value));
}
}
@@ -198,10 +198,10 @@ impl<'a> Ex<'a> {
craft_idml::model::AlternatingFillValues {
first_count: attrs.first.map(f64::from),
first_color: attrs.first_color.as_deref().map(|c| self.sw(c)),
first_tint: attrs.first_tint.map(|t| f64::from(t) * 100.0),
first_tint: attrs.first_tint.map(color_percent),
next_count: attrs.next.map(f64::from),
next_color: attrs.next_color.as_deref().map(|c| self.sw(c)),
next_tint: attrs.next_tint.map(|t| f64::from(t) * 100.0),
next_tint: attrs.next_tint.map(color_percent),
skip_first: attrs.skip_first.map(f64::from),
skip_last: attrs.skip_last.map(f64::from),
}
@@ -253,7 +253,7 @@ impl<'a> Ex<'a> {
name: Some(names::style_name_out(names::CELL_BUILTINS, &cs.name)),
based_on: (base != designcraft_doc::NO_CELL_STYLE).then(|| names::style_self("CellStyle", names::CELL_BUILTINS, base)),
fill: cs.fill.as_deref().map(|f| self.sw(f)),
fill_tint: cs.fill_tint.map(|t| f64::from(t) * 100.0),
fill_tint: cs.fill_tint.map(color_percent),
insets,
vertical_justification: cs.vj.map(|vj| names::vj_out(vj).into()),
first_baseline: cs.first_baseline.map(|fb| names::first_baseline_out(fb).into()),
@@ -297,10 +297,10 @@ impl<'a> Ex<'a> {
if let Some(a) = &ts.alt_rows {
row_fills.first_color = Some(self.sw(&a.first_color));
row_fills.first_count = Some(f64::from(a.first));
row_fills.first_tint = Some(f64::from(a.first_tint) * 100.0);
row_fills.first_tint = Some(color_percent(a.first_tint));
row_fills.next_color = Some(self.sw(&a.next_color));
row_fills.next_count = Some(f64::from(a.next));
row_fills.next_tint = Some(f64::from(a.next_tint) * 100.0);
row_fills.next_tint = Some(color_percent(a.next_tint));
}
let overlay = self.map_alt_fill_attrs(&ts.row_fills);
row_fills.first_count = overlay.first_count.or(row_fills.first_count);
@@ -385,7 +385,7 @@ impl<'a> Ex<'a> {
pub(super) fn rule(&mut self, el: &mut El, props: &mut Vec<El>, k: &str, r: &Rule) {
el.set(k, bool_s(r.on));
el.set(&format!("{k}LineWeight"), num(r.weight));
el.set(&format!("{k}Tint"), pct(r.tint as f64));
el.set(&format!("{k}Tint"), tint_value(r.tint));
el.set(&format!("{k}Width"), if r.column_width { "ColumnWidth" } else { "TextWidth" });
el.set(&format!("{k}Offset"), num(r.offset));
el.set(&format!("{k}LeftIndent"), num(r.left_indent));
@@ -397,7 +397,7 @@ impl<'a> Ex<'a> {
} else {
el.set(&format!("{k}GapColor"), self.sw(&r.gap_color));
}
el.set(&format!("{k}GapTint"), pct(r.gap_tint as f64));
el.set(&format!("{k}GapTint"), tint_value(r.gap_tint));
if k == "RuleAbove" {
el.set("KeepRuleAboveInFrame", bool_s(r.keep_in_frame));
}
@@ -411,7 +411,7 @@ impl<'a> Ex<'a> {
pub(super) fn stroke_attrs(&self, el: &mut El, s: &designcraft_doc::Stroke) {
el.set("StrokeColor", self.sw(&s.swatch));
el.set("StrokeWeight", num(if s.is_none() { 0.0 } else { s.weight }));
el.set("StrokeTint", pct(s.tint as f64));
el.set("StrokeTint", tint_value(s.tint));
el.set("StrokeType", names::stroke_type_out(&s.kind));
el.set("StrokeAlignment", names::stroke_align_out(s.align));
el.set("EndCap", names::cap_out(s.cap));
@@ -420,7 +420,7 @@ impl<'a> Ex<'a> {
el.set("LeftLineEnd", names::arrow_out(s.start));
el.set("RightLineEnd", names::arrow_out(s.end));
el.set("GapColor", self.sw(&s.gap_swatch));
el.set("GapTint", pct(s.gap_tint as f64));
el.set("GapTint", tint_value(s.gap_tint));
el.set("OverprintStroke", bool_s(s.overprint));
el.set("OverprintGap", bool_s(s.gap_overprint));
}
+6 -6
View File
@@ -1,15 +1,15 @@
use super::common::{bool_s, pct, uid};
use super::common::{bool_s, tint_value, uid};
use super::{Affine, CharFormat, Content, El, Ex, Item, ParaFormat, Shape, names, num};
impl<'a> Ex<'a> {
pub(super) fn cell_stroke_attrs(&self, el: &mut El, prefix: &str, s: &designcraft_doc::CellStroke) {
el.set(&format!("{prefix}StrokeWeight"), num(s.weight));
el.set(&format!("{prefix}StrokeColor"), self.sw(&s.color));
el.set(&format!("{prefix}StrokeTint"), pct(s.tint as f64));
el.set(&format!("{prefix}StrokeTint"), tint_value(s.tint));
el.set(&format!("{prefix}StrokeType"), names::stroke_type_out(&s.kind));
if s.gap_color != designcraft_color::swatch::NONE || s.gap_tint != 1.0 {
el.set(&format!("{prefix}StrokeGapColor"), self.sw(&s.gap_color));
el.set(&format!("{prefix}StrokeGapTint"), pct(s.gap_tint as f64));
el.set(&format!("{prefix}StrokeGapTint"), tint_value(s.gap_tint));
}
}
@@ -42,10 +42,10 @@ impl<'a> Ex<'a> {
if let Some(a) = alt {
el.set(&format!("Start{kind}FillColor"), self.sw(&a.first_color));
el.set(&format!("Start{kind}FillCount"), a.first);
el.set(&format!("Start{kind}FillTint"), pct(a.first_tint as f64));
el.set(&format!("Start{kind}FillTint"), tint_value(a.first_tint));
el.set(&format!("End{kind}FillColor"), self.sw(&a.next_color));
el.set(&format!("End{kind}FillCount"), a.next);
el.set(&format!("End{kind}FillTint"), pct(a.next_tint as f64));
el.set(&format!("End{kind}FillTint"), tint_value(a.next_tint));
el.set(&format!("SkipFirstAlternatingFill{kind}s"), a.skip_first);
el.set(&format!("SkipLastAlternatingFill{kind}s"), a.skip_last);
} else {
@@ -112,7 +112,7 @@ impl<'a> Ex<'a> {
),
)
.attr("FillColor", self.sw(&cell.fill))
.attr("FillTint", pct(cell.fill_tint as f64))
.attr("FillTint", tint_value(cell.fill_tint))
.attr("TopInset", num(cell.insets[0]))
.attr("LeftInset", num(cell.insets[1]))
.attr("BottomInset", num(cell.insets[2]))
+8 -8
View File
@@ -1,4 +1,4 @@
use super::common::{bool_s, p, pct};
use super::common::{bool_s, color_percent, p, pct, tint_value};
use super::{CHAR_BUILTINS, CharAttrs, El, Ex, ListType, ParaAttrs, SpanColumns, escape_id, names, num, st};
impl<'a> Ex<'a> {
@@ -26,13 +26,13 @@ impl<'a> Ex<'a> {
el.set("FillColor", self.sw(v));
}
if let Some(v) = a.fill_tint {
el.set("FillTint", pct(v as f64));
el.set("FillTint", tint_value(v));
}
if let Some(v) = &a.stroke {
el.set("StrokeColor", self.sw(v));
}
if let Some(v) = a.stroke_tint {
el.set("StrokeTint", pct(v as f64));
el.set("StrokeTint", tint_value(v));
}
if let Some(v) = a.stroke_weight {
el.set("StrokeWeight", num(v));
@@ -167,7 +167,7 @@ impl<'a> Ex<'a> {
el.set(&format!("{k}Color"), if c.is_empty() { "Text Color".into() } else { self.sw(c) });
}
if let Some(t) = t {
el.set(&format!("{k}Tint"), num(t as f64 * 100.0));
el.set(&format!("{k}Tint"), tint_value(t));
}
}
}
@@ -448,12 +448,12 @@ impl<'a> Ex<'a> {
let values = craft_idml::model::ParagraphBoxValues {
shading_on: a.shading_on,
shading_color: a.shading_color.as_ref().map(|color| self.sw(color)),
shading_tint: a.shading_tint.map(|tint| f64::from(tint) * 100.0),
shading_tint: a.shading_tint.map(color_percent),
shading_offsets: a.shading_offsets,
shading_overprint: a.shading_overprint,
border_on: a.border_on,
border_color: a.border_color.as_ref().map(|color| self.sw(color)),
border_tint: a.border_tint.map(|tint| f64::from(tint) * 100.0),
border_tint: a.border_tint.map(color_percent),
border_weights: a.border_weights,
border_offsets: a.border_offsets,
border_type: a.border_type.as_ref().map(names::stroke_type_out),
@@ -466,7 +466,7 @@ impl<'a> Ex<'a> {
border_display_if_splits: a.border_display_if_splits,
border_merge: a.border_merge,
border_gap_color: a.border_gap_color.as_ref().map(|color| self.sw(color)),
border_gap_tint: a.border_gap_tint.map(|tint| f64::from(tint) * 100.0),
border_gap_tint: a.border_gap_tint.map(color_percent),
border_overprint: a.border_overprint,
border_gap_overprint: a.border_gap_overprint,
shading_width: a.shading_width.map(|width| names::box_width_out(width).to_string()),
@@ -569,7 +569,7 @@ impl Ex<'_> {
}
for (key, tint) in [("KentenTint", a.kenten_fill_tint), ("KentenStrokeTint", a.kenten_stroke_tint)] {
if let Some(t) = tint {
el.set(key, t.map_or_else(|| "-1".to_string(), |t| num(f64::from(t) * 100.0)));
el.set(key, t.map_or_else(|| "-1".to_string(), tint_value));
}
}
if let Some(w) = a.kenten_stroke_weight {
+39
View File
@@ -852,6 +852,45 @@ fn imports_hand_written_fixture() {
assert_fixture_items(&d);
}
#[test]
fn color_and_tint_export_preserves_decimal_values_and_round_trips() {
for (cmyk, tint) in [("0 85 90 5", "40"), ("0 85.125 90 5", "40.125"), ("0 85 90 5", "0"), ("0 85 90 5", "100")] {
let rgb = "17 127.125 255";
let graphic = GRAPHIC
.replace("10 20 30 40", cmyk)
.replace("255 0 0", rgb)
.replace(r#"Name="$ID/" Visible="false""#, r#"Name="Process Ink" Visible="true""#)
.replace(r#"TintValue="50""#, &format!(r#"TintValue="{tint}""#));
let spread = SPREAD.replace(r#"FillColor="Tint/Brand 50%25""#, &format!(r#"FillColor="Tint/Brand 50%25" FillTint="{tint}""#));
let input = zip_files(&[
("designmap.xml", DESIGNMAP),
("Resources/Graphic.xml", &graphic),
("Resources/Styles.xml", STYLES),
("Resources/Preferences.xml", PREFS),
("MasterSpreads/MasterSpread_m1.xml", MASTER),
("Spreads/Spread_sp1.xml", &spread),
("Stories/Story_s1.xml", STORY),
]);
let original = import_idml_with(&input, &|_| None).unwrap();
let output = export_idml(&original);
let graphics = zip_text(&output, "Resources/Graphic.xml");
for attr in [format!(r#"ColorValue="{cmyk}""#), format!(r#"ColorValue="{rgb}""#), format!(r#"TintValue="{tint}""#)] {
assert!(graphics.contains(&attr), "missing {attr}");
}
assert!(zip_text(&output, "Spreads/").contains(&format!(r#"FillTint="{tint}""#)));
let back = import_idml_with(&output, &|_| None).unwrap();
for name in ["Process Ink", "Brand", "Brand 50%"] {
assert_eq!(back.swatch(name).unwrap().value, original.swatch(name).unwrap().value);
}
for doc in [&original, &back] {
let frame = doc.stories.values().next().unwrap().frames[0];
let fill = &doc.item(frame).unwrap().fill;
assert_eq!(fill.swatch, "Brand 50%");
assert_eq!(fill.tint, (tint.parse::<f64>().unwrap() / 100.0) as f32);
}
}
}
fn assert_fixture_settings_and_pages(d: &Document) {
assert_eq!(d.title, "Fixture");
assert!(d.settings.facing_pages);