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