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
use std::rc::Rc;
use taffy::Size;
use crate::Viewport;
use crate::style::CalcArena;
/// The sizing context used for length value resolving.
#[derive(Clone)]
pub struct SizingContext {
/// The viewport for the image renderer.
pub viewport: Viewport,
/// The nearest query container size (content box) in device pixels.
pub container_size: Size<Option<f32>>,
/// The font size in pixels.
pub font_size: f32,
/// Computed `font-size` of the root element in device pixels. `None` before
/// the root has been resolved; readers should fall back to `viewport.font_size`.
/// https://www.w3.org/TR/css-values-4/#rem
pub root_font_size: Option<f32>,
/// Pixel basis for the `lh` unit.
pub line_height: f32,
/// Pixel basis for the `rlh` unit; `None` before root is resolved.
pub root_line_height: Option<f32>,
/// The calc arena shared by the current layout tree.
pub calc_arena: Rc<CalcArena>,
}
impl SizingContext {
/// Test/bench helper. Exposed (not `#[cfg(test)]`) so dependent crates can
/// build a context in their own tests.
#[doc(hidden)]
pub fn new_test(viewport: Viewport) -> Self {
// Seed device-pixel metrics so `em`/`lh` conversions don't collapse to zero
// or drift at non-1.0 DPR.
let font_size = viewport.font_size * viewport.device_pixel_ratio;
Self {
viewport,
container_size: Size::NONE,
font_size,
root_font_size: None,
line_height: font_size,
root_line_height: None,
calc_arena: Rc::new(CalcArena::default()),
}
}
/// Device-pixel basis for the `rem` unit.
pub fn rem_basis(&self) -> f32 {
self
.root_font_size
.unwrap_or(self.viewport.font_size * self.viewport.device_pixel_ratio)
}
pub fn root_line_height_basis(&self) -> f32 {
self.root_line_height.unwrap_or(self.line_height)
}
pub fn query_container_width(&self) -> f32 {
self
.container_size
.width
.unwrap_or(self.viewport.size.width.unwrap_or_default() as f32)
}
pub fn query_container_height(&self) -> f32 {
self
.container_size
.height
.unwrap_or(self.viewport.size.height.unwrap_or_default() as f32)
}
}