Skip to content
Permalink
Browse files

Remove duplication for creating gradient stops

  • Loading branch information
pylbrecht committed Dec 15, 2019
1 parent 7c8c230 commit e0547855df7d5ff1d49dd1b5fce65b10391f74f1
Showing with 16 additions and 16 deletions.
  1. +16 −16 components/canvas/raqote_backend.rs
@@ -354,14 +354,14 @@ impl Path {
}
}

fn create_gradient_stops(gradient_stops: Vec<GradientStop>) -> GradientStops {
fn create_gradient_stops(gradient_stops: Vec<CanvasGradientStop>) -> Vec<raqote::GradientStop> {
let mut stops = gradient_stops
.into_iter()
.map(|item| item.as_raqote().clone())
.map(|item| item.to_raqote())
.collect::<Vec<raqote::GradientStop>>();
// https://www.w3.org/html/test/results/2dcontext/annotated-spec/canvas.html#testrefs.2d.gradient.interpolate.overlap
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
GradientStops::Raqote(stops)
stops
}

impl GenericDrawTarget for raqote::DrawTarget {
@@ -396,12 +396,22 @@ impl GenericDrawTarget for raqote::DrawTarget {
dt.get_data_mut().copy_from_slice(s);
raqote::DrawTarget::copy_surface(self, &dt, source.to_box2d(), destination);
}
// TODO(pylbrecht)
// Somehow a duplicate of `create_gradient_stops()` with different types.
// It feels cumbersome to convert GradientStop back and forth just to use
// `create_gradient_stops()`, so I'll leave this here for now.
fn create_gradient_stops(
&self,
gradient_stops: Vec<GradientStop>,
_extend_mode: ExtendMode,
) -> GradientStops {
create_gradient_stops(gradient_stops)
let mut stops = gradient_stops
.into_iter()
.map(|item| item.as_raqote().clone())
.collect::<Vec<raqote::GradientStop>>();
// https://www.w3.org/html/test/results/2dcontext/annotated-spec/canvas.html#testrefs.2d.gradient.interpolate.overlap
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
GradientStops::Raqote(stops)
}

fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> {
@@ -887,25 +897,15 @@ impl<'a> ToRaqotePattern<'_> for FillOrStrokeStyle {
LinearGradient(style) => {
let start = Point2D::new(style.x0 as f32, style.y0 as f32);
let end = Point2D::new(style.x1 as f32, style.y1 as f32);
let mut stops = style
.stops
.iter()
.map(|s| s.to_raqote())
.collect::<Vec<raqote::GradientStop>>();
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
let stops = create_gradient_stops(style.stops);
Some(Pattern::LinearGradient(LinearGradientPattern::new(
start, end, stops,
)))
},
RadialGradient(style) => {
let center1 = Point2D::new(style.x0 as f32, style.y0 as f32);
let center2 = Point2D::new(style.x1 as f32, style.y1 as f32);
let mut stops = style
.stops
.iter()
.map(|s| s.to_raqote())
.collect::<Vec<raqote::GradientStop>>();
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
let stops = create_gradient_stops(style.stops);
Some(Pattern::RadialGradient(RadialGradientPattern::new(
center1,
style.r0 as f32,

0 comments on commit e054785

Please sign in to comment.
You can’t perform that action at this time.