#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LengthValue {
Auto,
Px(f64),
Em(f64),
Percent(f64),
}
impl Eq for LengthValue {}
impl LengthValue {
#[must_use]
pub fn resolve(self, containing_width: f64, font_size: f64) -> Option<f64> {
match self {
Self::Auto => None,
Self::Px(v) => Some(v),
Self::Em(v) => Some(v * font_size),
Self::Percent(p) => Some(p * containing_width / 100.0),
}
}
#[must_use]
pub fn resolve_or_zero(self, containing_width: f64, font_size: f64) -> f64 {
self.resolve(containing_width, font_size).unwrap_or(0.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
x: f64,
y: f64,
}
impl Eq for Point {}
impl Point {
#[must_use]
pub fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
#[must_use]
pub fn x(&self) -> f64 {
self.x
}
#[must_use]
pub fn y(&self) -> f64 {
self.y
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
origin: Point,
width: f64,
height: f64,
}
impl Eq for Rect {}
impl Rect {
#[must_use]
pub fn new(origin: Point, width: f64, height: f64) -> Self {
Self {
origin,
width,
height,
}
}
#[must_use]
pub fn origin(&self) -> Point {
self.origin
}
#[must_use]
pub fn width(&self) -> f64 {
self.width
}
#[must_use]
pub fn height(&self) -> f64 {
self.height
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Edges {
top: f64,
right: f64,
bottom: f64,
left: f64,
}
impl Eq for Edges {}
impl Edges {
#[must_use]
pub fn new(top: f64, right: f64, bottom: f64, left: f64) -> Self {
Self {
top,
right,
bottom,
left,
}
}
#[must_use]
pub fn zero() -> Self {
Self::default()
}
#[must_use]
pub fn top(&self) -> f64 {
self.top
}
#[must_use]
pub fn right(&self) -> f64 {
self.right
}
#[must_use]
pub fn bottom(&self) -> f64 {
self.bottom
}
#[must_use]
pub fn left(&self) -> f64 {
self.left
}
#[must_use]
pub fn horizontal(&self) -> f64 {
self.left + self.right
}
#[must_use]
pub fn vertical(&self) -> f64 {
self.top + self.bottom
}
}
pub const DEFAULT_FONT_SIZE_PX: f64 = 16.0;