1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use app_units::Au;
use euclid::default::{Point2D, Rect, SideOffsets2D, Size2D, Vector2D};
use style::color::{AbsoluteColor, ColorSpace};
use style::computed_values::image_rendering::T as ImageRendering;
use style::computed_values::mix_blend_mode::T as MixBlendMode;
use style::computed_values::transform_style::T as TransformStyle;
use style::values::computed::{BorderStyle, Filter};
use style::values::specified::border::BorderImageRepeatKeyword;
use webrender_api as wr;

pub trait ToLayout {
    type Type;
    fn to_layout(&self) -> Self::Type;
}

pub trait FilterToLayout {
    type Type;
    fn to_layout(&self, current_color: &AbsoluteColor) -> Self::Type;
}

impl ToLayout for BorderStyle {
    type Type = wr::BorderStyle;
    fn to_layout(&self) -> Self::Type {
        match *self {
            BorderStyle::None => wr::BorderStyle::None,
            BorderStyle::Solid => wr::BorderStyle::Solid,
            BorderStyle::Double => wr::BorderStyle::Double,
            BorderStyle::Dotted => wr::BorderStyle::Dotted,
            BorderStyle::Dashed => wr::BorderStyle::Dashed,
            BorderStyle::Hidden => wr::BorderStyle::Hidden,
            BorderStyle::Groove => wr::BorderStyle::Groove,
            BorderStyle::Ridge => wr::BorderStyle::Ridge,
            BorderStyle::Inset => wr::BorderStyle::Inset,
            BorderStyle::Outset => wr::BorderStyle::Outset,
        }
    }
}

impl FilterToLayout for Filter {
    type Type = wr::FilterOp;
    fn to_layout(&self, current_color: &AbsoluteColor) -> Self::Type {
        match *self {
            Filter::Blur(radius) => wr::FilterOp::Blur(radius.px(), radius.px()),
            Filter::Brightness(amount) => wr::FilterOp::Brightness(amount.0),
            Filter::Contrast(amount) => wr::FilterOp::Contrast(amount.0),
            Filter::Grayscale(amount) => wr::FilterOp::Grayscale(amount.0),
            Filter::HueRotate(angle) => wr::FilterOp::HueRotate(angle.radians()),
            Filter::Invert(amount) => wr::FilterOp::Invert(amount.0),
            Filter::Opacity(amount) => wr::FilterOp::Opacity(amount.0.into(), amount.0),
            Filter::Saturate(amount) => wr::FilterOp::Saturate(amount.0),
            Filter::Sepia(amount) => wr::FilterOp::Sepia(amount.0),
            Filter::DropShadow(ref shadow) => wr::FilterOp::DropShadow(wr::Shadow {
                blur_radius: shadow.blur.px(),
                offset: wr::units::LayoutVector2D::new(
                    shadow.horizontal.px(),
                    shadow.vertical.px(),
                ),
                color: shadow
                    .color
                    .clone()
                    .resolve_to_absolute(current_color)
                    .to_layout(),
            }),
            // Statically check that Url is impossible.
            Filter::Url(ref url) => match *url {},
        }
    }
}

impl ToLayout for ImageRendering {
    type Type = wr::ImageRendering;
    fn to_layout(&self) -> Self::Type {
        match *self {
            ImageRendering::Auto => wr::ImageRendering::Auto,
            ImageRendering::CrispEdges => wr::ImageRendering::CrispEdges,
            ImageRendering::Pixelated => wr::ImageRendering::Pixelated,
        }
    }
}

impl ToLayout for MixBlendMode {
    type Type = wr::MixBlendMode;
    fn to_layout(&self) -> Self::Type {
        match *self {
            MixBlendMode::Normal => wr::MixBlendMode::Normal,
            MixBlendMode::Multiply => wr::MixBlendMode::Multiply,
            MixBlendMode::Screen => wr::MixBlendMode::Screen,
            MixBlendMode::Overlay => wr::MixBlendMode::Overlay,
            MixBlendMode::Darken => wr::MixBlendMode::Darken,
            MixBlendMode::Lighten => wr::MixBlendMode::Lighten,
            MixBlendMode::ColorDodge => wr::MixBlendMode::ColorDodge,
            MixBlendMode::ColorBurn => wr::MixBlendMode::ColorBurn,
            MixBlendMode::HardLight => wr::MixBlendMode::HardLight,
            MixBlendMode::SoftLight => wr::MixBlendMode::SoftLight,
            MixBlendMode::Difference => wr::MixBlendMode::Difference,
            MixBlendMode::Exclusion => wr::MixBlendMode::Exclusion,
            MixBlendMode::Hue => wr::MixBlendMode::Hue,
            MixBlendMode::Saturation => wr::MixBlendMode::Saturation,
            MixBlendMode::Color => wr::MixBlendMode::Color,
            MixBlendMode::Luminosity => wr::MixBlendMode::Luminosity,
        }
    }
}

impl ToLayout for TransformStyle {
    type Type = wr::TransformStyle;
    fn to_layout(&self) -> Self::Type {
        match *self {
            TransformStyle::Flat => wr::TransformStyle::Flat,
            TransformStyle::Preserve3d => wr::TransformStyle::Preserve3D,
        }
    }
}

impl ToLayout for AbsoluteColor {
    type Type = wr::ColorF;
    fn to_layout(&self) -> Self::Type {
        let rgba = self.to_color_space(ColorSpace::Srgb);
        wr::ColorF::new(
            rgba.components.0.clamp(0.0, 1.0),
            rgba.components.1.clamp(0.0, 1.0),
            rgba.components.2.clamp(0.0, 1.0),
            rgba.alpha,
        )
    }
}

impl ToLayout for Point2D<Au> {
    type Type = wr::units::LayoutPoint;
    fn to_layout(&self) -> Self::Type {
        wr::units::LayoutPoint::new(self.x.to_f32_px(), self.y.to_f32_px())
    }
}

impl ToLayout for Rect<Au> {
    type Type = wr::units::LayoutRect;
    fn to_layout(&self) -> Self::Type {
        wr::units::LayoutRect::from_origin_and_size(self.origin.to_layout(), self.size.to_layout())
    }
}

impl ToLayout for SideOffsets2D<Au> {
    type Type = wr::units::LayoutSideOffsets;
    fn to_layout(&self) -> Self::Type {
        wr::units::LayoutSideOffsets::new(
            self.top.to_f32_px(),
            self.right.to_f32_px(),
            self.bottom.to_f32_px(),
            self.left.to_f32_px(),
        )
    }
}

impl ToLayout for Size2D<Au> {
    type Type = wr::units::LayoutSize;
    fn to_layout(&self) -> Self::Type {
        wr::units::LayoutSize::new(self.width.to_f32_px(), self.height.to_f32_px())
    }
}

impl ToLayout for Vector2D<Au> {
    type Type = wr::units::LayoutVector2D;
    fn to_layout(&self) -> Self::Type {
        wr::units::LayoutVector2D::new(self.x.to_f32_px(), self.y.to_f32_px())
    }
}

impl ToLayout for BorderImageRepeatKeyword {
    type Type = wr::RepeatMode;

    fn to_layout(&self) -> Self::Type {
        match *self {
            BorderImageRepeatKeyword::Stretch => wr::RepeatMode::Stretch,
            BorderImageRepeatKeyword::Repeat => wr::RepeatMode::Repeat,
            BorderImageRepeatKeyword::Round => wr::RepeatMode::Round,
            BorderImageRepeatKeyword::Space => wr::RepeatMode::Space,
        }
    }
}