mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 17:00:29 +00:00
Merge pull request #6903 from dotCooCoo/feat/gradient-repeat
Gradients: repeat or reflect a gradient's stops a set number of times
This commit is contained in:
@@ -58,7 +58,7 @@ fn cells(px: f64) -> u16 {
|
||||
fn key(g: &GradientPaint, place: Rect, b: Rect, cols: u16, rows: u16, win: &[Range<u16>; 2], ink: Ink, fill: bool) -> u64 {
|
||||
let mut h = std::collections::hash_map::DefaultHasher::new();
|
||||
let gr = &g.gradient;
|
||||
(gr.kind as u8, gr.dither && fill, gr.method as u8).hash(&mut h);
|
||||
(gr.kind as u8, gr.dither && fill, gr.method as u8, gr.painted_spread()).hash(&mut h);
|
||||
for s in &gr.stops {
|
||||
(s.offset.to_bits(), s.opacity.to_bits(), s.midpoint.to_bits()).hash(&mut h);
|
||||
s.color.key().hash(&mut h);
|
||||
|
||||
@@ -19,7 +19,15 @@ pub(crate) fn set_paint(ctx: &mut RenderContext, p: &Paint, bounds: Rect, f: &Fr
|
||||
}
|
||||
Paint::Gradient(g) if g.gradient.kind == GradientKind::Freeform => crate::freeform::set_freeform_paint(ctx, g, bounds, f.ink),
|
||||
Paint::Gradient(g) => {
|
||||
let geom = g.resolve(bounds);
|
||||
// A gradient that repeats or reflects: one run of its stops over its period, which
|
||||
// the renderer extends past both ends.
|
||||
let spread = g.gradient.is_spread();
|
||||
let geom = if spread { g.resolve(bounds).period(g.gradient.kind, g.gradient.repeat_count()) } else { g.resolve(bounds) };
|
||||
let extend = match g.gradient.spread {
|
||||
_ if !spread => peniko::Extend::Pad,
|
||||
vectorcraft_color::GradientSpread::Reflect => peniko::Extend::Reflect,
|
||||
_ => peniko::Extend::Repeat,
|
||||
};
|
||||
let adjusted = f.ink.gradient(&g.gradient);
|
||||
let gradient = adjusted.as_ref().unwrap_or(&g.gradient);
|
||||
let ink = if adjusted.is_some() { f.ink.base() } else { f.ink };
|
||||
@@ -42,9 +50,11 @@ pub(crate) fn set_paint(ctx: &mut RenderContext, p: &Paint, bounds: Rect, f: &Fr
|
||||
}
|
||||
GradientKind::Conic => {
|
||||
// One turn about the origin, carried onto the extent ellipse: its frame starts
|
||||
// the sweep along the vector, and a mirrored frame reverses it.
|
||||
// the sweep along the vector, and a mirrored frame reverses it. Repeated `n`
|
||||
// times, one run takes 1/n of the turn.
|
||||
ctx.set_paint_transform(geom.conic_frame());
|
||||
peniko::Gradient::new_sweep((0.0, 0.0), 0.0, std::f32::consts::TAU).with_stops(stops.as_slice())
|
||||
let turn = if spread { std::f32::consts::TAU / g.gradient.repeat_count() as f32 } else { std::f32::consts::TAU };
|
||||
peniko::Gradient::new_sweep((0.0, 0.0), 0.0, turn).with_stops(stops.as_slice())
|
||||
}
|
||||
// peniko has no diamond gradient: it is drawn as pixels, as a dithered fill is.
|
||||
GradientKind::Diamond => return crate::dither::set_dithered_paint(ctx, g, bounds, bounds, f.ink, false),
|
||||
@@ -57,7 +67,7 @@ pub(crate) fn set_paint(ctx: &mut RenderContext, p: &Paint, bounds: Rect, f: &Fr
|
||||
};
|
||||
// Colour and opacity blend each on its own, as the model samples them and as the PDF
|
||||
// and SVG exports draw them (peniko's default premultiplies).
|
||||
ctx.set_paint(grad.with_interpolation_alpha_space(peniko::InterpolationAlphaSpace::Unpremultiplied));
|
||||
ctx.set_paint(grad.with_extend(extend).with_interpolation_alpha_space(peniko::InterpolationAlphaSpace::Unpremultiplied));
|
||||
true
|
||||
}
|
||||
Paint::Pattern { pattern, xf } => crate::pattern::set_pattern_paint(ctx, pattern, *xf, f),
|
||||
|
||||
@@ -212,11 +212,12 @@ impl Renderer {
|
||||
let tol = 2f64.powi(level);
|
||||
// Reach a pixel past the stroke, so its antialiased edge has colour under it.
|
||||
let slices = effects::stroke::gradient_slices(bp, rule, st, tol, f.px + tol, f.px);
|
||||
let unrolled = g.unrolled();
|
||||
Arc::new(
|
||||
slices
|
||||
.into_iter()
|
||||
.map(|s| {
|
||||
let (b, paint) = (s.shape.bounding_box(), s.paint(g));
|
||||
let (b, paint) = (s.shape.bounding_box(), s.paint(&unrolled));
|
||||
(s.shape, paint, b)
|
||||
})
|
||||
.collect(),
|
||||
|
||||
@@ -47,6 +47,18 @@ fn along_a_quarter_of_the_length_is_a_quarter_of_the_gradient_in_any_direction()
|
||||
}
|
||||
}
|
||||
|
||||
/// A gradient that reflects twice runs its stops twice along the stroke, the second run
|
||||
/// backwards.
|
||||
#[test]
|
||||
fn along_a_reflecting_gradient_runs_its_stops_that_many_times() {
|
||||
let g = Gradient { spread: vectorcraft_color::GradientSpread::Reflect, repeats: 2, ..Gradient::default() };
|
||||
let img = render(&line((20.0, 100.0), (180.0, 100.0)), g, StrokeGradientMode::Along, true);
|
||||
for (f, want) in [(0.125, 0.75), (0.375, 0.25), (0.625, 0.25), (0.875, 0.75)] {
|
||||
let x = 20.0 + 160.0 * f;
|
||||
assert!((grey(&img, x, 100.0) - want).abs() < 0.03, "at {f}: {} vs {want}", grey(&img, x, 100.0));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn across_runs_from_the_left_edge_to_the_right() {
|
||||
// Travelling right, the left edge is the top one (y 90), the right edge the bottom (y 110).
|
||||
|
||||
Reference in New Issue
Block a user