layout/display_list/
conversions.rs

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 https://mozilla.org/MPL/2.0/. */
4
5use app_units::Au;
6use style::color::AbsoluteColor;
7use style::computed_values::background_blend_mode::SingleComputedValue as BackgroundBlendMode;
8use style::computed_values::image_rendering::T as ComputedImageRendering;
9use style::computed_values::mix_blend_mode::T as ComputedMixBlendMode;
10use style::computed_values::text_decoration_style::T as ComputedTextDecorationStyle;
11use style::computed_values::transform_style::T as ComputedTransformStyle;
12use style::values::computed::Filter as ComputedFilter;
13use style::values::specified::border::BorderImageRepeatKeyword;
14use webrender_api::{
15    FilterOp, ImageRendering, LineStyle, MixBlendMode, RepeatMode, Shadow, TransformStyle, units,
16};
17
18use crate::geom::{PhysicalPoint, PhysicalRect, PhysicalSides, PhysicalSize};
19
20pub trait ToWebRender {
21    type Type;
22    fn to_webrender(&self) -> Self::Type;
23}
24
25pub trait FilterToWebRender {
26    type Type;
27    fn to_webrender(&self, current_color: &AbsoluteColor) -> Self::Type;
28}
29
30impl FilterToWebRender for ComputedFilter {
31    type Type = FilterOp;
32    fn to_webrender(&self, current_color: &AbsoluteColor) -> Self::Type {
33        match *self {
34            ComputedFilter::Blur(radius) => FilterOp::Blur(radius.px(), radius.px()),
35            ComputedFilter::Brightness(amount) => FilterOp::Brightness(amount.0),
36            ComputedFilter::Contrast(amount) => FilterOp::Contrast(amount.0),
37            ComputedFilter::Grayscale(amount) => FilterOp::Grayscale(amount.0),
38            ComputedFilter::HueRotate(angle) => FilterOp::HueRotate(angle.degrees()),
39            ComputedFilter::Invert(amount) => FilterOp::Invert(amount.0),
40            ComputedFilter::Opacity(amount) => FilterOp::Opacity(amount.0.into(), amount.0),
41            ComputedFilter::Saturate(amount) => FilterOp::Saturate(amount.0),
42            ComputedFilter::Sepia(amount) => FilterOp::Sepia(amount.0),
43            ComputedFilter::DropShadow(ref shadow) => FilterOp::DropShadow(Shadow {
44                blur_radius: shadow.blur.px(),
45                offset: units::LayoutVector2D::new(shadow.horizontal.px(), shadow.vertical.px()),
46                color: super::rgba(shadow.color.clone().resolve_to_absolute(current_color)),
47            }),
48            // Statically check that Url is impossible.
49            ComputedFilter::Url(ref url) => match *url {},
50        }
51    }
52}
53
54impl ToWebRender for ComputedMixBlendMode {
55    type Type = MixBlendMode;
56    fn to_webrender(&self) -> Self::Type {
57        match *self {
58            ComputedMixBlendMode::Normal => MixBlendMode::Normal,
59            ComputedMixBlendMode::Multiply => MixBlendMode::Multiply,
60            ComputedMixBlendMode::Screen => MixBlendMode::Screen,
61            ComputedMixBlendMode::Overlay => MixBlendMode::Overlay,
62            ComputedMixBlendMode::Darken => MixBlendMode::Darken,
63            ComputedMixBlendMode::Lighten => MixBlendMode::Lighten,
64            ComputedMixBlendMode::ColorDodge => MixBlendMode::ColorDodge,
65            ComputedMixBlendMode::ColorBurn => MixBlendMode::ColorBurn,
66            ComputedMixBlendMode::HardLight => MixBlendMode::HardLight,
67            ComputedMixBlendMode::SoftLight => MixBlendMode::SoftLight,
68            ComputedMixBlendMode::Difference => MixBlendMode::Difference,
69            ComputedMixBlendMode::Exclusion => MixBlendMode::Exclusion,
70            ComputedMixBlendMode::Hue => MixBlendMode::Hue,
71            ComputedMixBlendMode::Saturation => MixBlendMode::Saturation,
72            ComputedMixBlendMode::Color => MixBlendMode::Color,
73            ComputedMixBlendMode::Luminosity => MixBlendMode::Luminosity,
74            ComputedMixBlendMode::PlusLighter => MixBlendMode::PlusLighter,
75        }
76    }
77}
78
79impl ToWebRender for ComputedTransformStyle {
80    type Type = TransformStyle;
81    fn to_webrender(&self) -> Self::Type {
82        match *self {
83            ComputedTransformStyle::Flat => TransformStyle::Flat,
84            ComputedTransformStyle::Preserve3d => TransformStyle::Preserve3D,
85        }
86    }
87}
88
89impl ToWebRender for PhysicalPoint<Au> {
90    type Type = units::LayoutPoint;
91    fn to_webrender(&self) -> Self::Type {
92        units::LayoutPoint::new(self.x.to_f32_px(), self.y.to_f32_px())
93    }
94}
95
96impl ToWebRender for PhysicalSize<Au> {
97    type Type = units::LayoutSize;
98    fn to_webrender(&self) -> Self::Type {
99        units::LayoutSize::new(self.width.to_f32_px(), self.height.to_f32_px())
100    }
101}
102
103impl ToWebRender for PhysicalRect<Au> {
104    type Type = units::LayoutRect;
105    fn to_webrender(&self) -> Self::Type {
106        units::LayoutRect::from_origin_and_size(
107            self.origin.to_webrender(),
108            self.size.to_webrender(),
109        )
110    }
111}
112
113impl ToWebRender for PhysicalSides<Au> {
114    type Type = units::LayoutSideOffsets;
115    fn to_webrender(&self) -> Self::Type {
116        units::LayoutSideOffsets::new(
117            self.top.to_f32_px(),
118            self.right.to_f32_px(),
119            self.bottom.to_f32_px(),
120            self.left.to_f32_px(),
121        )
122    }
123}
124
125impl ToWebRender for ComputedTextDecorationStyle {
126    type Type = LineStyle;
127    fn to_webrender(&self) -> Self::Type {
128        match *self {
129            ComputedTextDecorationStyle::Solid | ComputedTextDecorationStyle::Double => {
130                LineStyle::Solid
131            },
132            ComputedTextDecorationStyle::Dotted => LineStyle::Dotted,
133            ComputedTextDecorationStyle::Dashed => LineStyle::Dashed,
134            ComputedTextDecorationStyle::Wavy => LineStyle::Wavy,
135            ComputedTextDecorationStyle::MozNone => {
136                unreachable!("Should never try to draw a moz-none text decoration")
137            },
138        }
139    }
140}
141
142impl ToWebRender for BorderImageRepeatKeyword {
143    type Type = RepeatMode;
144
145    fn to_webrender(&self) -> Self::Type {
146        match *self {
147            BorderImageRepeatKeyword::Stretch => RepeatMode::Stretch,
148            BorderImageRepeatKeyword::Repeat => RepeatMode::Repeat,
149            BorderImageRepeatKeyword::Round => RepeatMode::Round,
150            BorderImageRepeatKeyword::Space => RepeatMode::Space,
151        }
152    }
153}
154
155impl ToWebRender for ComputedImageRendering {
156    type Type = ImageRendering;
157
158    fn to_webrender(&self) -> Self::Type {
159        match self {
160            ComputedImageRendering::Auto => ImageRendering::Auto,
161            ComputedImageRendering::CrispEdges => ImageRendering::CrispEdges,
162            ComputedImageRendering::Pixelated => ImageRendering::Pixelated,
163        }
164    }
165}
166
167impl ToWebRender for BackgroundBlendMode {
168    type Type = MixBlendMode;
169    fn to_webrender(&self) -> Self::Type {
170        match *self {
171            Self::Normal => MixBlendMode::Normal,
172            Self::Multiply => MixBlendMode::Multiply,
173            Self::Screen => MixBlendMode::Screen,
174            Self::Overlay => MixBlendMode::Overlay,
175            Self::Darken => MixBlendMode::Darken,
176            Self::Lighten => MixBlendMode::Lighten,
177            Self::ColorDodge => MixBlendMode::ColorDodge,
178            Self::ColorBurn => MixBlendMode::ColorBurn,
179            Self::HardLight => MixBlendMode::HardLight,
180            Self::SoftLight => MixBlendMode::SoftLight,
181            Self::Difference => MixBlendMode::Difference,
182            Self::Exclusion => MixBlendMode::Exclusion,
183            Self::Hue => MixBlendMode::Hue,
184            Self::Saturation => MixBlendMode::Saturation,
185            Self::Color => MixBlendMode::Color,
186            Self::Luminosity => MixBlendMode::Luminosity,
187        }
188    }
189}