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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::context::QuirksMode;
use crate::custom_properties::CssEnvironment;
use crate::font_metrics::FontMetrics;
use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
use crate::media_queries::MediaType;
use crate::properties::ComputedValues;
use crate::values::computed::CSSPixelLength;
use crate::values::computed::Context;
use crate::values::computed::Resolution;
use crate::values::specified::font::FONT_MEDIUM_PX;
use crate::values::specified::ViewportVariant;
use crate::values::KeyframesName;
use app_units::Au;
use cssparser::RGBA;
use euclid::default::Size2D as UntypedSize2D;
use euclid::{Scale, SideOffsets2D, Size2D};
use mime::Mime;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use style_traits::viewport::ViewportConstraints;
use style_traits::{CSSPixel, DevicePixel};
#[derive(Debug, MallocSizeOf)]
pub struct Device {
media_type: MediaType,
viewport_size: Size2D<f32, CSSPixel>,
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
#[ignore_malloc_size_of = "Pure stack type"]
quirks_mode: QuirksMode,
#[ignore_malloc_size_of = "Pure stack type"]
root_font_size: AtomicU32,
#[ignore_malloc_size_of = "Pure stack type"]
used_root_font_size: AtomicBool,
#[ignore_malloc_size_of = "Pure stack type"]
used_viewport_units: AtomicBool,
environment: CssEnvironment,
}
impl Device {
pub fn new(
media_type: MediaType,
quirks_mode: QuirksMode,
viewport_size: Size2D<f32, CSSPixel>,
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
) -> Device {
Device {
media_type,
viewport_size,
device_pixel_ratio,
quirks_mode,
root_font_size: AtomicU32::new(FONT_MEDIUM_PX.to_bits()),
used_root_font_size: AtomicBool::new(false),
used_viewport_units: AtomicBool::new(false),
environment: CssEnvironment,
}
}
#[inline]
pub fn environment(&self) -> &CssEnvironment {
&self.environment
}
pub fn default_computed_values(&self) -> &ComputedValues {
ComputedValues::initial_values()
}
pub fn root_font_size(&self) -> CSSPixelLength {
self.used_root_font_size.store(true, Ordering::Relaxed);
CSSPixelLength::new(f32::from_bits(self.root_font_size.load(Ordering::Relaxed)))
}
pub fn set_root_font_size(&self, size: CSSPixelLength) {
self.root_font_size
.store(size.px().to_bits(), Ordering::Relaxed)
}
pub fn quirks_mode(&self) -> QuirksMode {
self.quirks_mode
}
pub fn set_body_text_color(&self, _color: RGBA) {
}
pub fn animation_name_may_be_referenced(&self, _: &KeyframesName) -> bool {
true
}
pub fn used_root_font_size(&self) -> bool {
self.used_root_font_size.load(Ordering::Relaxed)
}
#[inline]
pub fn au_viewport_size(&self) -> UntypedSize2D<Au> {
Size2D::new(
Au::from_f32_px(self.viewport_size.width),
Au::from_f32_px(self.viewport_size.height),
)
}
pub fn au_viewport_size_for_viewport_unit_resolution(
&self,
_: ViewportVariant,
) -> UntypedSize2D<Au> {
self.used_viewport_units.store(true, Ordering::Relaxed);
self.au_viewport_size()
}
pub fn used_viewport_units(&self) -> bool {
self.used_viewport_units.load(Ordering::Relaxed)
}
pub fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
self.device_pixel_ratio
}
pub fn query_font_metrics(
&self,
_vertical: bool,
_font: &crate::properties::style_structs::Font,
_base_size: CSSPixelLength,
_in_media_query: bool,
_retrieve_math_scales: bool,
) -> FontMetrics {
Default::default()
}
pub fn account_for_viewport_rule(&mut self, constraints: &ViewportConstraints) {
self.viewport_size = constraints.size;
}
pub fn media_type(&self) -> MediaType {
self.media_type.clone()
}
pub fn use_document_colors(&self) -> bool {
true
}
pub fn default_background_color(&self) -> RGBA {
RGBA::new(255, 255, 255, 255)
}
pub fn default_color(&self) -> RGBA {
RGBA::new(0, 0, 0, 255)
}
pub fn safe_area_insets(&self) -> SideOffsets2D<f32, CSSPixel> {
SideOffsets2D::zero()
}
pub fn is_supported_mime_type(&self, mime_type: &str) -> bool {
match mime_type.parse::<Mime>() {
Ok(m) => {
m == mime::IMAGE_BMP
|| m == mime::IMAGE_GIF
|| m == mime::IMAGE_PNG
|| m == mime::IMAGE_JPEG
|| m == "image/x-icon"
|| m == "image/webp"
}
_ => false,
}
}
#[inline]
pub fn is_chrome_document(&self) -> bool {
false
}
}
fn eval_width(context: &Context) -> CSSPixelLength {
CSSPixelLength::new(context.device().au_viewport_size().width.to_f32_px())
}
#[derive(Clone, Copy, Debug, FromPrimitive, Parse, ToCss)]
#[repr(u8)]
enum Scan {
Progressive,
Interlace,
}
fn eval_scan(_: &Context, _: Option<Scan>) -> bool {
false
}
fn eval_resolution(context: &Context) -> Resolution {
Resolution::from_dppx(context.device().device_pixel_ratio.0)
}
fn eval_device_pixel_ratio(context: &Context) -> f32 {
eval_resolution(context).dppx()
}
lazy_static! {
pub static ref MEDIA_FEATURES: [QueryFeatureDescription; 5] = [
feature!(
atom!("width"),
AllowsRanges::Yes,
Evaluator::Length(eval_width),
FeatureFlags::empty(),
),
feature!(
atom!("scan"),
AllowsRanges::No,
keyword_evaluator!(eval_scan, Scan),
FeatureFlags::empty(),
),
feature!(
atom!("resolution"),
AllowsRanges::Yes,
Evaluator::Resolution(eval_resolution),
FeatureFlags::empty(),
),
feature!(
atom!("device-pixel-ratio"),
AllowsRanges::Yes,
Evaluator::Float(eval_device_pixel_ratio),
FeatureFlags::WEBKIT_PREFIX,
),
feature!(
atom!("-moz-device-pixel-ratio"),
AllowsRanges::Yes,
Evaluator::Float(eval_device_pixel_ratio),
FeatureFlags::empty(),
),
];
}