1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 use canvas_traits::canvas::{FillOrStrokeStyle, RepetitionStyle, SurfaceStyle}; 6 use dom::bindings::codegen::Bindings::CanvasPatternBinding; 7 use dom::bindings::reflector::{Reflector, reflect_dom_object}; 8 use dom::bindings::root::DomRoot; 9 use dom::canvasgradient::ToFillOrStrokeStyle; 10 use dom::globalscope::GlobalScope; 11 use dom_struct::dom_struct; 12 use euclid::Size2D; 13 14 // https://html.spec.whatwg.org/multipage/#canvaspattern 15 #[dom_struct] 16 pub struct CanvasPattern { 17 reflector_: Reflector, 18 surface_data: Vec<u8>, 19 surface_size: Size2D<i32>, 20 repeat_x: bool, 21 repeat_y: bool, 22 origin_clean: bool, 23 } 24 25 impl CanvasPattern { new_inherited(surface_data: Vec<u8>, surface_size: Size2D<i32>, repeat: RepetitionStyle, origin_clean: bool) -> CanvasPattern26 fn new_inherited(surface_data: Vec<u8>, 27 surface_size: Size2D<i32>, 28 repeat: RepetitionStyle, 29 origin_clean: bool) 30 -> CanvasPattern { 31 let (x, y) = match repeat { 32 RepetitionStyle::Repeat => (true, true), 33 RepetitionStyle::RepeatX => (true, false), 34 RepetitionStyle::RepeatY => (false, true), 35 RepetitionStyle::NoRepeat => (false, false), 36 }; 37 38 CanvasPattern { 39 reflector_: Reflector::new(), 40 surface_data: surface_data, 41 surface_size: surface_size, 42 repeat_x: x, 43 repeat_y: y, 44 origin_clean: origin_clean, 45 } 46 } new(global: &GlobalScope, surface_data: Vec<u8>, surface_size: Size2D<i32>, repeat: RepetitionStyle, origin_clean: bool) -> DomRoot<CanvasPattern>47 pub fn new(global: &GlobalScope, 48 surface_data: Vec<u8>, 49 surface_size: Size2D<i32>, 50 repeat: RepetitionStyle, 51 origin_clean: bool) 52 -> DomRoot<CanvasPattern> { 53 reflect_dom_object( 54 Box::new(CanvasPattern::new_inherited( 55 surface_data, surface_size, repeat, origin_clean 56 )), 57 global, 58 CanvasPatternBinding::Wrap 59 ) 60 } origin_is_clean(&self) -> bool61 pub fn origin_is_clean(&self) -> bool { 62 self.origin_clean 63 } 64 } 65 66 impl<'a> ToFillOrStrokeStyle for &'a CanvasPattern { to_fill_or_stroke_style(self) -> FillOrStrokeStyle67 fn to_fill_or_stroke_style(self) -> FillOrStrokeStyle { 68 FillOrStrokeStyle::Surface(SurfaceStyle::new(self.surface_data.clone(), 69 self.surface_size, 70 self.repeat_x, 71 self.repeat_y)) 72 } 73 } 74