Skip to main content

eternal_styler/
computed.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
5//! Styles after the cascade: what layout and painting read.
6
7use crate::values::{
8    BorderStyle, Color, Corners, FontFamily, FontStyle, FontWeight, Sides, TextAlign,
9};
10
11/// A length in pixels, or a percentage of something layout knows about.
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub enum LengthPercentage {
14    Px(f32),
15    /// A fraction, so `50%` is `Percent(0.5)`.
16    Percent(f32),
17}
18
19impl LengthPercentage {
20    pub const ZERO: LengthPercentage = LengthPercentage::Px(0.0);
21
22    /// The value in pixels, with percentages taken of `basis`.
23    pub fn resolve(self, basis: f32) -> f32 {
24        match self {
25            Self::Px(px) => px,
26            Self::Percent(p) => p * basis,
27        }
28    }
29}
30
31/// `width`, `height`, `min-width`, `min-height` and the margins.
32#[derive(Clone, Copy, Debug, PartialEq, Default)]
33pub enum LengthPercentageOrAuto {
34    #[default]
35    Auto,
36    Px(f32),
37    /// A fraction, so `50%` is `Percent(0.5)`.
38    Percent(f32),
39}
40
41impl LengthPercentageOrAuto {
42    /// The value in pixels, or `None` for `auto`.
43    pub fn resolve(self, basis: f32) -> Option<f32> {
44        match self {
45            Self::Auto => None,
46            Self::Px(px) => Some(px),
47            Self::Percent(p) => Some(p * basis),
48        }
49    }
50}
51
52/// `max-width` and `max-height`.
53#[derive(Clone, Copy, Debug, PartialEq, Default)]
54pub enum LengthPercentageOrNone {
55    #[default]
56    None,
57    Px(f32),
58    /// A fraction, so `50%` is `Percent(0.5)`.
59    Percent(f32),
60}
61
62impl LengthPercentageOrNone {
63    /// The value in pixels, or `None` for `none`.
64    pub fn resolve(self, basis: f32) -> Option<f32> {
65        match self {
66            Self::None => None,
67            Self::Px(px) => Some(px),
68            Self::Percent(p) => Some(p * basis),
69        }
70    }
71}
72
73/// `line-height`.
74#[derive(Clone, Copy, Debug, PartialEq, Default)]
75pub enum LineHeight {
76    /// Whatever the font's metrics say. Toolkits usually use around 1.2
77    /// times the font size.
78    #[default]
79    Normal,
80    /// A multiple of the font size.
81    Number(f32),
82    Px(f32),
83}
84
85impl LineHeight {
86    /// The value in pixels for a given font size, or `None` for `normal`.
87    pub fn resolve(self, font_size: f32) -> Option<f32> {
88        match self {
89            Self::Normal => None,
90            Self::Number(n) => Some(n * font_size),
91            Self::Px(px) => Some(px),
92        }
93    }
94}
95
96/// Every property's value for one element, after the cascade, inheritance
97/// and unit resolution.
98///
99/// Lengths are in pixels. `em` and `rem` are gone; percentages remain, since
100/// only layout knows what they are a percentage of. `currentcolor` has been
101/// replaced by the element's colour.
102///
103/// [`Default`] gives the initial value of every property, which is what a
104/// root element with no matching rules gets.
105#[derive(Clone, Debug, PartialEq)]
106pub struct ComputedStyle {
107    pub width: LengthPercentageOrAuto,
108    pub height: LengthPercentageOrAuto,
109    pub min_width: LengthPercentageOrAuto,
110    pub min_height: LengthPercentageOrAuto,
111    pub max_width: LengthPercentageOrNone,
112    pub max_height: LengthPercentageOrNone,
113
114    pub margin: Sides<LengthPercentageOrAuto>,
115    pub padding: Sides<LengthPercentage>,
116    pub row_gap: LengthPercentage,
117    pub column_gap: LengthPercentage,
118
119    pub color: Color,
120    pub background_color: Color,
121    /// `0.0..=1.0`
122    pub opacity: f32,
123
124    /// In pixels. Zero wherever the side's style is `None` or `Hidden`,
125    /// whatever width was declared.
126    pub border_width: Sides<f32>,
127    pub border_style: Sides<BorderStyle>,
128    pub border_color: Sides<Color>,
129    pub border_radius: Corners<LengthPercentage>,
130
131    pub font_family: FontFamily,
132    /// In pixels.
133    pub font_size: f32,
134    pub font_weight: FontWeight,
135    pub font_style: FontStyle,
136    pub line_height: LineHeight,
137    /// In pixels. `normal` is zero.
138    pub letter_spacing: f32,
139    pub text_align: TextAlign,
140}
141
142impl ComputedStyle {
143    /// The initial value of every property.
144    pub fn initial() -> Self {
145        Self {
146            width: LengthPercentageOrAuto::Auto,
147            height: LengthPercentageOrAuto::Auto,
148            min_width: LengthPercentageOrAuto::Auto,
149            min_height: LengthPercentageOrAuto::Auto,
150            max_width: LengthPercentageOrNone::None,
151            max_height: LengthPercentageOrNone::None,
152            margin: Sides::all(LengthPercentageOrAuto::Px(0.0)),
153            padding: Sides::all(LengthPercentage::ZERO),
154            row_gap: LengthPercentage::ZERO,
155            column_gap: LengthPercentage::ZERO,
156            color: Color::BLACK,
157            background_color: Color::TRANSPARENT,
158            opacity: 1.0,
159            border_width: Sides::all(0.0),
160            border_style: Sides::all(BorderStyle::None),
161            border_color: Sides::all(Color::BLACK),
162            border_radius: Corners::all(LengthPercentage::ZERO),
163            font_family: FontFamily::default(),
164            font_size: 16.0,
165            font_weight: FontWeight::NORMAL,
166            font_style: FontStyle::Normal,
167            line_height: LineHeight::Normal,
168            letter_spacing: 0.0,
169            text_align: TextAlign::Start,
170        }
171    }
172
173    /// Copies the inherited properties from `parent`.
174    pub(crate) fn inherit_from(&mut self, parent: &ComputedStyle) {
175        self.color = parent.color;
176        self.font_family = parent.font_family.clone();
177        self.font_size = parent.font_size;
178        self.font_weight = parent.font_weight;
179        self.font_style = parent.font_style;
180        self.line_height = parent.line_height;
181        self.letter_spacing = parent.letter_spacing;
182        self.text_align = parent.text_align;
183    }
184}
185
186impl Default for ComputedStyle {
187    fn default() -> Self {
188        Self::initial()
189    }
190}