Skip to content

Commit e392cd7

Browse files
committed
Initial implementation of pattern fill style for canvas.
1 parent 100e2b4 commit e392cd7

5 files changed

Lines changed: 146 additions & 5 deletions

File tree

‎components/canvas/canvas_paint_task.rs‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use azure::azure::AzFloat;
66
use azure::azure_hl::{DrawTarget, SurfaceFormat, BackendType, StrokeOptions, DrawOptions, Pattern};
77
use azure::azure_hl::{ColorPattern, PathBuilder, DrawSurfaceOptions, Filter};
8-
use azure::azure_hl::{GradientStop, LinearGradientPattern, RadialGradientPattern, ExtendMode};
8+
use azure::azure_hl::{GradientStop, LinearGradientPattern, RadialGradientPattern, SurfacePattern, ExtendMode};
99
use azure::azure_hl::{JoinStyle, CapStyle, CompositionOp};
1010
use canvas_msg::{CanvasMsg, Canvas2dMsg, CanvasCommonMsg};
1111
use geom::matrix2d::Matrix2D;
@@ -620,11 +620,28 @@ impl RadialGradientStyle {
620620
}
621621
}
622622

623+
#[derive(Clone)]
624+
pub struct SurfaceStyle {
625+
pub surface_data: Vec<u8>,
626+
pub surface_size: Size2D<i32>
627+
}
628+
629+
impl SurfaceStyle {
630+
pub fn new(surface_data: Vec<u8>, surface_size: Size2D<i32>)
631+
-> SurfaceStyle {
632+
SurfaceStyle {
633+
surface_data: surface_data,
634+
surface_size: surface_size,
635+
}
636+
}
637+
}
638+
623639
#[derive(Clone)]
624640
pub enum FillOrStrokeStyle {
625641
Color(RGBA),
626642
LinearGradient(LinearGradientStyle),
627643
RadialGradient(RadialGradientStyle),
644+
Surface(SurfaceStyle),
628645
}
629646

630647
impl FillOrStrokeStyle {
@@ -664,6 +681,16 @@ impl FillOrStrokeStyle {
664681
radial_gradient_style.r0 as AzFloat, radial_gradient_style.r1 as AzFloat,
665682
drawtarget.create_gradient_stops(&gradient_stops, ExtendMode::Clamp),
666683
&Matrix2D::identity()))
684+
},
685+
FillOrStrokeStyle::Surface(ref surface_style) => {
686+
let source_surface = drawtarget.create_source_surface_from_data(
687+
&surface_style.surface_data,
688+
surface_style.surface_size,
689+
surface_style.surface_size.width * 4,
690+
SurfaceFormat::B8G8R8A8);
691+
692+
Pattern::Surface(SurfacePattern::new(
693+
source_surface.azure_source_surface))
667694
}
668695
}
669696
}
@@ -721,6 +748,26 @@ impl LineJoinStyle {
721748
}
722749
}
723750

751+
#[derive(Copy, Clone, PartialEq)]
752+
pub enum RepetitionStyle {
753+
Repeat = 0,
754+
RepeatX = 1,
755+
RepeatY = 2,
756+
NoRepeat = 3,
757+
}
758+
759+
impl RepetitionStyle {
760+
pub fn from_str(string: &str) -> Option<RepetitionStyle> {
761+
match string {
762+
"repeat" => Some(RepetitionStyle::Repeat),
763+
"repeat-x" => Some(RepetitionStyle::RepeatX),
764+
"repeat-y" => Some(RepetitionStyle::RepeatY),
765+
"no-repeat" => Some(RepetitionStyle::NoRepeat),
766+
_ => None
767+
}
768+
}
769+
}
770+
724771
#[derive(Copy, Clone, PartialEq)]
725772
pub enum CompositionStyle {
726773
SrcIn,

‎components/script/dom/bindings/trace.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ use script_task::ScriptChan;
3636

3737
use canvas::canvas_paint_task::{CanvasGradientStop, LinearGradientStyle, RadialGradientStyle};
3838
use canvas::canvas_paint_task::{LineCapStyle, LineJoinStyle, CompositionOrBlending};
39+
use canvas::canvas_paint_task::{RepetitionStyle};
3940
use cssparser::RGBA;
4041
use encoding::types::EncodingRef;
4142
use geom::matrix2d::Matrix2D;
4243
use geom::rect::Rect;
44+
use geom::size::Size2D;
4345
use html5ever::tree_builder::QuirksMode;
4446
use hyper::header::Headers;
4547
use hyper::method::Method;
@@ -249,6 +251,7 @@ no_jsmanaged_fields!(isize, i8, i16, i32, i64);
249251
no_jsmanaged_fields!(Sender<T>);
250252
no_jsmanaged_fields!(Receiver<T>);
251253
no_jsmanaged_fields!(Rect<T>);
254+
no_jsmanaged_fields!(Size2D<T>);
252255
no_jsmanaged_fields!(Arc<T>);
253256
no_jsmanaged_fields!(Image, ImageCacheChan, ImageCacheTask, ScriptControlChan);
254257
no_jsmanaged_fields!(Atom, Namespace);
@@ -272,6 +275,7 @@ no_jsmanaged_fields!(Matrix2D<T>);
272275
no_jsmanaged_fields!(StorageType);
273276
no_jsmanaged_fields!(CanvasGradientStop, LinearGradientStyle, RadialGradientStyle);
274277
no_jsmanaged_fields!(LineCapStyle, LineJoinStyle, CompositionOrBlending);
278+
no_jsmanaged_fields!(RepetitionStyle);
275279

276280
impl JSTraceable for Box<ScriptChan+Send> {
277281
#[inline]

‎components/script/dom/canvaspattern.rs‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,50 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use dom::bindings::utils::Reflector;
5+
use canvas::canvas_paint_task::{FillOrStrokeStyle, SurfaceStyle, RepetitionStyle};
6+
use dom::bindings::codegen::Bindings::CanvasPatternBinding;
7+
use dom::bindings::global::GlobalRef;
8+
use dom::bindings::js::{JSRef, Temporary};
9+
use dom::bindings::utils::{Reflector, reflect_dom_object};
10+
use dom::canvasgradient::{ToFillOrStrokeStyle};
11+
use geom::size::Size2D;
612

713
// https://html.spec.whatwg.org/multipage/#canvaspattern
814
#[dom_struct]
915
pub struct CanvasPattern {
1016
reflector_: Reflector,
17+
surface_data: Vec<u8>,
18+
surface_size: Size2D<i32>,
19+
repeat_x: bool,
20+
repeat_y: bool,
1121
}
1222

23+
impl CanvasPattern {
24+
fn new_inherited(surface_data: Vec<u8>, surface_size: Size2D<i32>, repeat: RepetitionStyle) -> CanvasPattern {
25+
let (x, y) = match repeat {
26+
RepetitionStyle::Repeat => (true, true),
27+
RepetitionStyle::RepeatX => (true, false),
28+
RepetitionStyle::RepeatY => (false, true),
29+
RepetitionStyle::NoRepeat => (false, false),
30+
};
1331

32+
CanvasPattern {
33+
reflector_: Reflector::new(),
34+
surface_data: surface_data,
35+
surface_size: surface_size,
36+
repeat_x: x,
37+
repeat_y: y,
38+
}
39+
}
40+
pub fn new(global: GlobalRef, surface_data: Vec<u8>, surface_size: Size2D<i32>, repeat: RepetitionStyle) -> Temporary<CanvasPattern> {
41+
reflect_dom_object(box CanvasPattern::new_inherited(surface_data, surface_size, repeat),
42+
global, CanvasPatternBinding::Wrap)
43+
}
44+
}
45+
46+
impl<'a> ToFillOrStrokeStyle for JSRef<'a, CanvasPattern> {
47+
fn to_fill_or_stroke_style(&self) -> FillOrStrokeStyle {
48+
FillOrStrokeStyle::Surface(
49+
SurfaceStyle::new(self.surface_data.clone(), self.surface_size))
50+
}
51+
}

‎components/script/dom/canvasrenderingcontext2d.rs‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use dom::bindings::js::{JS, JSRef, LayoutJS, Rootable, Temporary};
1616
use dom::bindings::num::Finite;
1717
use dom::bindings::utils::{Reflector, reflect_dom_object};
1818
use dom::canvasgradient::{CanvasGradient, CanvasGradientStyle, ToFillOrStrokeStyle};
19+
use dom::canvaspattern::CanvasPattern;
1920
use dom::htmlcanvaselement::{HTMLCanvasElement, HTMLCanvasElementHelpers};
2021
use dom::htmlimageelement::{HTMLImageElement, HTMLImageElementHelpers};
2122
use dom::imagedata::{ImageData, ImageDataHelpers};
@@ -30,7 +31,7 @@ use geom::size::Size2D;
3031

3132
use canvas::canvas_msg::{CanvasMsg, Canvas2dMsg, CanvasCommonMsg};
3233
use canvas::canvas_paint_task::{CanvasPaintTask, FillOrStrokeStyle};
33-
use canvas::canvas_paint_task::{LinearGradientStyle, RadialGradientStyle};
34+
use canvas::canvas_paint_task::{LinearGradientStyle, RadialGradientStyle, RepetitionStyle};
3435
use canvas::canvas_paint_task::{LineCapStyle, LineJoinStyle, CompositionOrBlending};
3536

3637
use net_traits::image::base::Image;
@@ -824,7 +825,9 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
824825
StringOrCanvasGradientOrCanvasPattern::eCanvasGradient(gradient) => {
825826
self.renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetFillStyle(gradient.root().r().to_fill_or_stroke_style()))).unwrap();
826827
}
827-
_ => {}
828+
StringOrCanvasGradientOrCanvasPattern::eCanvasPattern(pattern) => {
829+
self.renderer.send(CanvasMsg::Canvas2d(Canvas2dMsg::SetFillStyle(pattern.root().r().to_fill_or_stroke_style()))).unwrap();
830+
}
828831
}
829832
}
830833

@@ -944,6 +947,54 @@ impl<'a> CanvasRenderingContext2DMethods for JSRef<'a, CanvasRenderingContext2D>
944947
CanvasGradientStyle::Radial(RadialGradientStyle::new(x0, y0, r0, x1, y1, r1, Vec::new()))))
945948
}
946949

950+
// https://html.spec.whatwg.org/multipage/#dom-context-2d-createpattern
951+
fn CreatePattern(self, image: HTMLImageElementOrHTMLCanvasElementOrCanvasRenderingContext2D,
952+
repetition: DOMString) -> Fallible<Temporary<CanvasPattern>> {
953+
match image {
954+
HTMLImageElementOrHTMLCanvasElementOrCanvasRenderingContext2D::eHTMLCanvasElement(_) => {
955+
return Err(Type("Not implemented".to_owned()));
956+
}
957+
HTMLImageElementOrHTMLCanvasElementOrCanvasRenderingContext2D::eCanvasRenderingContext2D(_) => {
958+
return Err(Type("Not implemented".to_owned()));
959+
}
960+
HTMLImageElementOrHTMLCanvasElementOrCanvasRenderingContext2D::eHTMLImageElement(image) => {
961+
let image = image.root();
962+
let image_element = image.r();
963+
964+
let url = match image_element.get_url() {
965+
Some(url) => url,
966+
None => return Err(InvalidState),
967+
};
968+
969+
let img = match self.request_image_from_cache(url) {
970+
Some(img) => img,
971+
None => return Err(InvalidState),
972+
};
973+
974+
let image_size = Size2D(img.width as f64, img.height as f64);
975+
let image_data = match img.pixels {
976+
PixelsByColorType::RGBA8(ref pixels) => pixels.to_vec(),
977+
PixelsByColorType::K8(_) => panic!("K8 color type not supported"),
978+
PixelsByColorType::RGB8(_) => panic!("RGB8 color type not supported"),
979+
PixelsByColorType::KA8(_) => panic!("KA8 color type not supported"),
980+
};
981+
982+
if let Some(rep) = RepetitionStyle::from_str(&repetition) {
983+
match rep {
984+
RepetitionStyle::Repeat => return Err(Type("Not implemented".to_owned())),
985+
RepetitionStyle::RepeatX => return Err(Type("Not implemented".to_owned())),
986+
RepetitionStyle::RepeatY => return Err(Type("Not implemented".to_owned())),
987+
RepetitionStyle::NoRepeat => return Ok(CanvasPattern::new(self.global.root().r(),
988+
image_data,
989+
Size2D(image_size.width as i32, image_size.height as i32),
990+
rep)),
991+
}
992+
}
993+
return Err(Type("Invalid repetition type".to_owned()));
994+
}
995+
}
996+
}
997+
947998
// https://html.spec.whatwg.org/multipage/#dom-context-2d-linewidth
948999
fn LineWidth(self) -> f64 {
9491000
let state = self.state.borrow();

‎components/script/dom/webidls/CanvasRenderingContext2D.webidl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ interface CanvasRenderingContext2D {
6262
CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1);
6363
[Throws]
6464
CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
65-
//CanvasPattern createPattern(CanvasImageSource image, [TreatNullAs=EmptyString] DOMString repetition);
65+
[Throws]
66+
CanvasPattern createPattern(CanvasImageSource image, [TreatNullAs=EmptyString] DOMString repetition);
6667

6768
// shadows
6869
// attribute unrestricted double shadowOffsetX; // (default 0)

0 commit comments

Comments
 (0)