Skip to content

Commit

Permalink
Remove duplication for creating gradient stops
Browse files Browse the repository at this point in the history
  • Loading branch information
pylbrecht committed Dec 17, 2019
1 parent 7c8c230 commit e054785
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions components/canvas/raqote_backend.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e054785

Please sign in to comment.