Merge pull request #6961 from dotCooCoo/fix/gradient-repeat-runs

Gradients: a repeated gradient with many stops keeps every run
This commit is contained in:
BFlat
2026-11-13 05:22:59 +07:00
committed by GitHub
12 changed files with 560 additions and 52 deletions
+36
View File
@@ -529,6 +529,42 @@ fn a_spread_gradient_expands_into_whole_bands_for_each_run() {
}
}
/// A gradient that repeats across a box in more runs than are written (700 runs of 0.2 pt) is not
/// expanded into its first runs and a flat last color: the fill stays a gradient, and the rest of
/// the object (its stroke) still expands. `object.expand.info` reports no gradient or fill to
/// expand, and Expand with only Fill on reports nothing to expand; the same gradient in 70 runs
/// (10 repeats) reports both.
#[test]
fn a_spread_gradient_with_more_runs_than_written_stays_a_gradient() {
let gradient = |repeats: u32| {
json!({"kind": "linear", "spread": "repeat", "repeats": repeats, "start": [30, 100], "end": [50, 100],
"stops": [{"offset": 0, "color": "#ffff00"}, {"offset": 1, "color": "#004080"}]})
};
for (repeats, expands) in [(10, true), (100, false)] {
let mut s = session();
gradient_rect(&mut s, Rect::new(30.0, 40.0, 170.0, 160.0), gradient(repeats), true);
let info = s.execute("object.expand.info", &json!({})).unwrap();
assert_eq!(
(info["gradient"].clone(), info["fill"].clone(), info["stroke"].clone()),
(json!(expands), json!(expands), json!(true)),
"{repeats}"
);
let only_fill = s.execute("object.expand", &json!({"object": false, "stroke": false}));
assert_eq!(only_fill.is_ok(), expands, "{repeats}: {only_fill:?}");
}
for to in [json!({"steps": 240}), json!({"gradient": "mesh"})] {
let mut s = session();
gradient_rect(&mut s, Rect::new(30.0, 40.0, 170.0, 160.0), gradient(100), true);
let before = render(&s);
run(&mut s, "object.expand", to.clone());
let after = render(&s);
assert!(mean_diff(&before, &after) < 2.0, "{to}: {}", mean_diff(&before, &after));
let mut gradients = 0;
s.doc().unwrap().doc.walk(|n| gradients += usize::from(matches!(n.appearance.fill_paint(), Paint::Gradient(_))));
assert_eq!(gradients, 1, "{to}");
}
}
#[test]
fn the_mesh_option_makes_a_mesh_that_paints_the_gradient() {
for gradient in [
+21
View File
@@ -722,6 +722,27 @@ fn expanding_a_gradient_fill_keeps_each_piece_s_own_paint() {
}
}
/// `object.expand.info` measures each piece a painted effect returns: a piece whose repeating
/// gradient needs more runs across it than are written (1,000) reports no gradient to expand, as
/// Expand leaves it a gradient, and the same gradient in 10 runs reports one.
#[test]
fn expand_info_measures_the_gradient_on_each_painted_piece() {
for (end, expands) in [(120.0, true), (100.2, false)] {
let mut s = session();
let id = rect(&mut s, 10.0, "#ff0000");
let gradient = json!({"kind": "linear", "spread": "repeat", "repeats": 10, "start": [100, 50], "end": [end, 50]});
run(&mut s, "paint.setFill", json!({"ids": [id.0], "gradient": gradient}));
let out = json!({"objects": [{"path": square_at(100.0)}]}).to_string();
install_wat(&mut s, &tpl::returning(&painting("test.engine.expandruns", true), &out));
run(&mut s, "effect.apply", json!({"ids": [id.0], "effect": "plugin.test.engine.expandruns"}));
run(&mut s, "select.set", json!({"ids": [id.0]}));
let info = s.execute("object.expand.info", &json!({})).unwrap();
assert_eq!((info["gradient"].clone(), info["fill"].clone()), (json!(expands), json!(expands)), "{end}");
let only_fill = s.execute("object.expand", &json!({"object": false, "fill": true, "stroke": false}));
assert_eq!(only_fill.is_ok(), expands, "{end}: {only_fill:?}");
}
}
/// Object › Expand of a pattern fill works piece by piece: a piece that keeps the pattern has its
/// tiles expanded, and a piece with a paint of its own keeps that paint.
#[test]