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 app_units::Au; 6 use euclid::{Point2D, Rect, SideOffsets2D, Size2D, Vector2D}; 7 use style::computed_values::image_rendering::T as ImageRendering; 8 use style::computed_values::mix_blend_mode::T as MixBlendMode; 9 use style::computed_values::transform_style::T as TransformStyle; 10 use style::values::RGBA; 11 use style::values::computed::{BorderStyle, Filter}; 12 use style::values::generics::effects::Filter as GenericFilter; 13 use style::values::specified::border::BorderImageRepeatKeyword; 14 use webrender_api as wr; 15 16 pub trait ToLayout { 17 type Type; to_layout(&self) -> Self::Type18 fn to_layout(&self) -> Self::Type; 19 } 20 21 impl ToLayout for BorderStyle { 22 type Type = wr::BorderStyle; to_layout(&self) -> Self::Type23 fn to_layout(&self) -> Self::Type { 24 match *self { 25 BorderStyle::None => wr::BorderStyle::None, 26 BorderStyle::Solid => wr::BorderStyle::Solid, 27 BorderStyle::Double => wr::BorderStyle::Double, 28 BorderStyle::Dotted => wr::BorderStyle::Dotted, 29 BorderStyle::Dashed => wr::BorderStyle::Dashed, 30 BorderStyle::Hidden => wr::BorderStyle::Hidden, 31 BorderStyle::Groove => wr::BorderStyle::Groove, 32 BorderStyle::Ridge => wr::BorderStyle::Ridge, 33 BorderStyle::Inset => wr::BorderStyle::Inset, 34 BorderStyle::Outset => wr::BorderStyle::Outset, 35 } 36 } 37 } 38 39 impl ToLayout for Filter { 40 type Type = wr::FilterOp; to_layout(&self) -> Self::Type41 fn to_layout(&self) -> Self::Type { 42 match *self { 43 GenericFilter::Blur(radius) => wr::FilterOp::Blur(radius.px()), 44 GenericFilter::Brightness(amount) => wr::FilterOp::Brightness(amount.0), 45 GenericFilter::Contrast(amount) => wr::FilterOp::Contrast(amount.0), 46 GenericFilter::Grayscale(amount) => wr::FilterOp::Grayscale(amount.0), 47 GenericFilter::HueRotate(angle) => wr::FilterOp::HueRotate(angle.radians()), 48 GenericFilter::Invert(amount) => wr::FilterOp::Invert(amount.0), 49 GenericFilter::Opacity(amount) => wr::FilterOp::Opacity(amount.0.into(), amount.0), 50 GenericFilter::Saturate(amount) => wr::FilterOp::Saturate(amount.0), 51 GenericFilter::Sepia(amount) => wr::FilterOp::Sepia(amount.0), 52 // Statically check that DropShadow is impossible. 53 GenericFilter::DropShadow(ref shadow) => match *shadow {}, 54 } 55 } 56 } 57 58 impl ToLayout for ImageRendering { 59 type Type = wr::ImageRendering; to_layout(&self) -> Self::Type60 fn to_layout(&self) -> Self::Type { 61 match *self { 62 ImageRendering::Auto => wr::ImageRendering::Auto, 63 ImageRendering::CrispEdges => wr::ImageRendering::CrispEdges, 64 ImageRendering::Pixelated => wr::ImageRendering::Pixelated, 65 } 66 } 67 } 68 69 impl ToLayout for MixBlendMode { 70 type Type = wr::MixBlendMode; to_layout(&self) -> Self::Type71 fn to_layout(&self) -> Self::Type { 72 match *self { 73 MixBlendMode::Normal => wr::MixBlendMode::Normal, 74 MixBlendMode::Multiply => wr::MixBlendMode::Multiply, 75 MixBlendMode::Screen => wr::MixBlendMode::Screen, 76 MixBlendMode::Overlay => wr::MixBlendMode::Overlay, 77 MixBlendMode::Darken => wr::MixBlendMode::Darken, 78 MixBlendMode::Lighten => wr::MixBlendMode::Lighten, 79 MixBlendMode::ColorDodge => wr::MixBlendMode::ColorDodge, 80 MixBlendMode::ColorBurn => wr::MixBlendMode::ColorBurn, 81 MixBlendMode::HardLight => wr::MixBlendMode::HardLight, 82 MixBlendMode::SoftLight => wr::MixBlendMode::SoftLight, 83 MixBlendMode::Difference => wr::MixBlendMode::Difference, 84 MixBlendMode::Exclusion => wr::MixBlendMode::Exclusion, 85 MixBlendMode::Hue => wr::MixBlendMode::Hue, 86 MixBlendMode::Saturation => wr::MixBlendMode::Saturation, 87 MixBlendMode::Color => wr::MixBlendMode::Color, 88 MixBlendMode::Luminosity => wr::MixBlendMode::Luminosity, 89 } 90 } 91 } 92 93 impl ToLayout for TransformStyle { 94 type Type = wr::TransformStyle; to_layout(&self) -> Self::Type95 fn to_layout(&self) -> Self::Type { 96 match *self { 97 TransformStyle::Auto | TransformStyle::Flat => wr::TransformStyle::Flat, 98 TransformStyle::Preserve3d => wr::TransformStyle::Preserve3D, 99 } 100 } 101 } 102 103 impl ToLayout for RGBA { 104 type Type = wr::ColorF; to_layout(&self) -> Self::Type105 fn to_layout(&self) -> Self::Type { 106 wr::ColorF::new( 107 self.red_f32(), 108 self.green_f32(), 109 self.blue_f32(), 110 self.alpha_f32(), 111 ) 112 } 113 } 114 115 impl ToLayout for Point2D<Au> { 116 type Type = wr::LayoutPoint; to_layout(&self) -> Self::Type117 fn to_layout(&self) -> Self::Type { 118 wr::LayoutPoint::new(self.x.to_f32_px(), self.y.to_f32_px()) 119 } 120 } 121 122 impl ToLayout for Rect<Au> { 123 type Type = wr::LayoutRect; to_layout(&self) -> Self::Type124 fn to_layout(&self) -> Self::Type { 125 wr::LayoutRect::new(self.origin.to_layout(), self.size.to_layout()) 126 } 127 } 128 129 impl ToLayout for SideOffsets2D<Au> { 130 type Type = wr::BorderWidths; to_layout(&self) -> Self::Type131 fn to_layout(&self) -> Self::Type { 132 wr::BorderWidths { 133 left: self.left.to_f32_px(), 134 top: self.top.to_f32_px(), 135 right: self.right.to_f32_px(), 136 bottom: self.bottom.to_f32_px(), 137 } 138 } 139 } 140 141 impl ToLayout for Size2D<Au> { 142 type Type = wr::LayoutSize; to_layout(&self) -> Self::Type143 fn to_layout(&self) -> Self::Type { 144 wr::LayoutSize::new(self.width.to_f32_px(), self.height.to_f32_px()) 145 } 146 } 147 148 impl ToLayout for Vector2D<Au> { 149 type Type = wr::LayoutVector2D; to_layout(&self) -> Self::Type150 fn to_layout(&self) -> Self::Type { 151 wr::LayoutVector2D::new(self.x.to_f32_px(), self.y.to_f32_px()) 152 } 153 } 154 155 impl ToLayout for BorderImageRepeatKeyword { 156 type Type = wr::RepeatMode; 157 to_layout(&self) -> Self::Type158 fn to_layout(&self) -> Self::Type { 159 match *self { 160 BorderImageRepeatKeyword::Stretch => wr::RepeatMode::Stretch, 161 BorderImageRepeatKeyword::Repeat => wr::RepeatMode::Repeat, 162 BorderImageRepeatKeyword::Round => wr::RepeatMode::Round, 163 BorderImageRepeatKeyword::Space => wr::RepeatMode::Space, 164 } 165 } 166 } 167