Remove duplication for creating gradient stops

This commit is contained in:
pylbrecht 2019-12-15 10:01:04 +01:00
parent 7c8c230d93
commit e0547855df

View file

@ -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 let mut stops = gradient_stops
.into_iter() .into_iter()
.map(|item| item.as_raqote().clone()) .map(|item| item.to_raqote())
.collect::<Vec<raqote::GradientStop>>(); .collect::<Vec<raqote::GradientStop>>();
// https://www.w3.org/html/test/results/2dcontext/annotated-spec/canvas.html#testrefs.2d.gradient.interpolate.overlap // 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()); stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
GradientStops::Raqote(stops) stops
} }
impl GenericDrawTarget for raqote::DrawTarget { impl GenericDrawTarget for raqote::DrawTarget {
@ -396,12 +396,22 @@ impl GenericDrawTarget for raqote::DrawTarget {
dt.get_data_mut().copy_from_slice(s); dt.get_data_mut().copy_from_slice(s);
raqote::DrawTarget::copy_surface(self, &dt, source.to_box2d(), destination); 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( fn create_gradient_stops(
&self, &self,
gradient_stops: Vec<GradientStop>, gradient_stops: Vec<GradientStop>,
_extend_mode: ExtendMode, _extend_mode: ExtendMode,
) -> GradientStops { ) -> 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> { fn create_path_builder(&self) -> Box<dyn GenericPathBuilder> {
@ -887,12 +897,7 @@ impl<'a> ToRaqotePattern<'_> for FillOrStrokeStyle {
LinearGradient(style) => { LinearGradient(style) => {
let start = Point2D::new(style.x0 as f32, style.y0 as f32); 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 end = Point2D::new(style.x1 as f32, style.y1 as f32);
let mut stops = style let stops = create_gradient_stops(style.stops);
.stops
.iter()
.map(|s| s.to_raqote())
.collect::<Vec<raqote::GradientStop>>();
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
Some(Pattern::LinearGradient(LinearGradientPattern::new( Some(Pattern::LinearGradient(LinearGradientPattern::new(
start, end, stops, start, end, stops,
))) )))
@ -900,12 +905,7 @@ impl<'a> ToRaqotePattern<'_> for FillOrStrokeStyle {
RadialGradient(style) => { RadialGradient(style) => {
let center1 = Point2D::new(style.x0 as f32, style.y0 as f32); 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 center2 = Point2D::new(style.x1 as f32, style.y1 as f32);
let mut stops = style let stops = create_gradient_stops(style.stops);
.stops
.iter()
.map(|s| s.to_raqote())
.collect::<Vec<raqote::GradientStop>>();
stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
Some(Pattern::RadialGradient(RadialGradientPattern::new( Some(Pattern::RadialGradient(RadialGradientPattern::new(
center1, center1,
style.r0 as f32, style.r0 as f32,