2020-07-04 11:04:51 -04:00
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
ops::{Div, Mul},
|
|
|
|
};
|
2020-01-04 22:55:04 -05:00
|
|
|
|
2020-07-08 21:39:30 -04:00
|
|
|
use crate::interner::InternedString;
|
|
|
|
|
2020-03-19 16:24:31 -04:00
|
|
|
pub(crate) use conversion::UNIT_CONVERSION_TABLE;
|
2020-03-18 10:08:40 -04:00
|
|
|
|
2020-03-19 16:24:31 -04:00
|
|
|
mod conversion;
|
2020-03-18 10:08:40 -04:00
|
|
|
|
2020-07-07 01:06:22 -04:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
2020-01-20 13:15:47 -05:00
|
|
|
pub(crate) enum Unit {
|
2020-01-04 22:55:04 -05:00
|
|
|
// Absolute units
|
|
|
|
/// Pixels
|
|
|
|
Px,
|
|
|
|
/// Millimeters
|
|
|
|
Mm,
|
|
|
|
/// Inches
|
|
|
|
In,
|
|
|
|
/// Centimeters
|
|
|
|
Cm,
|
|
|
|
/// Quarter-millimeters
|
|
|
|
Q,
|
|
|
|
/// Points
|
|
|
|
Pt,
|
|
|
|
/// Picas
|
|
|
|
Pc,
|
2020-01-25 10:11:26 -05:00
|
|
|
|
2020-01-04 22:55:04 -05:00
|
|
|
// Font relative units
|
|
|
|
/// Font size of the parent element
|
|
|
|
Em,
|
|
|
|
/// Font size of the root element
|
|
|
|
Rem,
|
|
|
|
/// Line height of the element
|
|
|
|
Lh,
|
|
|
|
/// x-height of the element's font
|
|
|
|
Ex,
|
|
|
|
/// The advance measure (width) of the glyph "0" of the element's font
|
|
|
|
Ch,
|
2020-04-17 13:06:54 -04:00
|
|
|
/// Represents the "cap height" (nominal height of capital letters) of the element's font
|
2020-01-04 22:55:04 -05:00
|
|
|
Cap,
|
2020-04-17 13:06:54 -04:00
|
|
|
/// Equal to the used advance measure of the "水" (CJK water ideograph, U+6C34) glyph
|
|
|
|
/// found in the font used to render it
|
2020-01-04 22:55:04 -05:00
|
|
|
Ic,
|
2020-04-17 13:06:54 -04:00
|
|
|
/// Equal to the computed value of the line-height property on the root element
|
|
|
|
/// (typically <html>), converted to an absolute length
|
2020-01-04 22:55:04 -05:00
|
|
|
Rlh,
|
|
|
|
|
|
|
|
// Viewport relative units
|
|
|
|
/// 1% of the viewport's width
|
|
|
|
Vw,
|
|
|
|
/// 1% of the viewport's height
|
|
|
|
Vh,
|
|
|
|
/// 1% of the viewport's smaller dimension
|
|
|
|
Vmin,
|
|
|
|
/// 1% of the viewport's larger dimension
|
|
|
|
Vmax,
|
2020-04-17 13:06:54 -04:00
|
|
|
/// Equal to 1% of the size of the initial containing block, in the direction of the root
|
|
|
|
/// element's inline axis
|
2020-01-04 22:55:04 -05:00
|
|
|
Vi,
|
2020-04-17 13:06:54 -04:00
|
|
|
/// Equal to 1% of the size of the initial containing block, in the direction of the root
|
|
|
|
/// element's block axis
|
2020-01-04 22:55:04 -05:00
|
|
|
Vb,
|
|
|
|
|
|
|
|
// Angle units
|
|
|
|
/// Represents an angle in degrees. One full circle is 360deg
|
|
|
|
Deg,
|
|
|
|
/// Represents an angle in gradians. One full circle is 400grad
|
|
|
|
Grad,
|
2020-04-17 13:06:54 -04:00
|
|
|
/// Represents an angle in radians. One full circle is 2π radians which approximates to 6.283rad
|
2020-01-04 22:55:04 -05:00
|
|
|
Rad,
|
|
|
|
/// Represents an angle in a number of turns. One full circle is 1turn
|
|
|
|
Turn,
|
|
|
|
|
|
|
|
// Time units
|
|
|
|
/// Represents a time in seconds
|
|
|
|
S,
|
|
|
|
/// Represents a time in milliseconds
|
|
|
|
Ms,
|
|
|
|
|
|
|
|
// Frequency units
|
|
|
|
/// Represents a frequency in hertz
|
|
|
|
Hz,
|
|
|
|
/// Represents a frequency in kilohertz
|
|
|
|
Khz,
|
|
|
|
|
|
|
|
// Resolution units
|
|
|
|
/// Represents the number of dots per inch
|
|
|
|
Dpi,
|
|
|
|
/// Represents the number of dots per centimeter
|
|
|
|
Dpcm,
|
|
|
|
/// Represents the number of dots per px unit
|
|
|
|
Dppx,
|
|
|
|
/// Alias for dppx
|
|
|
|
X,
|
|
|
|
|
2020-01-25 10:11:26 -05:00
|
|
|
// Other units
|
2020-01-04 22:55:04 -05:00
|
|
|
/// Represents a fraction of the available space in the grid container
|
|
|
|
Fr,
|
2020-02-15 08:16:17 -05:00
|
|
|
Percent,
|
2020-01-22 00:54:43 -05:00
|
|
|
|
|
|
|
/// Unknown unit
|
2020-07-08 21:39:30 -04:00
|
|
|
Unknown(InternedString),
|
2020-01-04 22:55:04 -05:00
|
|
|
/// Unspecified unit
|
|
|
|
None,
|
2020-03-16 21:29:00 -04:00
|
|
|
|
2020-03-17 10:06:24 -04:00
|
|
|
/// Units multiplied together
|
2020-07-08 21:39:30 -04:00
|
|
|
/// Boxed under the assumption that mul units are exceedingly rare
|
2022-02-08 20:22:38 -05:00
|
|
|
#[allow(clippy::box_collection)]
|
2020-07-08 21:39:30 -04:00
|
|
|
Mul(Box<Vec<Unit>>),
|
2020-07-04 11:04:51 -04:00
|
|
|
|
|
|
|
/// Units divided by each other
|
|
|
|
Div(Box<DivUnit>),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
2020-02-15 08:16:17 -05:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub(crate) enum UnitKind {
|
|
|
|
Absolute,
|
|
|
|
FontRelative,
|
|
|
|
ViewportRelative,
|
|
|
|
Angle,
|
|
|
|
Time,
|
|
|
|
Frequency,
|
|
|
|
Resolution,
|
|
|
|
Other,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2020-07-07 01:06:22 -04:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
2020-07-04 11:04:51 -04:00
|
|
|
pub(crate) struct DivUnit {
|
|
|
|
numer: Unit,
|
|
|
|
denom: Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DivUnit {
|
|
|
|
pub const fn new(numer: Unit, denom: Unit) -> Self {
|
|
|
|
Self { numer, denom }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DivUnit {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
if self.numer == Unit::None {
|
|
|
|
write!(f, "{}^-1", self.denom)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}/{}", self.numer, self.denom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
impl Mul<Unit> for DivUnit {
|
|
|
|
type Output = Unit;
|
|
|
|
fn mul(self, rhs: Unit) -> Self::Output {
|
|
|
|
match rhs {
|
|
|
|
Unit::Mul(..) => todo!(),
|
|
|
|
Unit::Div(..) => todo!(),
|
|
|
|
Unit::None => todo!(),
|
|
|
|
_ => {
|
|
|
|
if self.denom == rhs {
|
|
|
|
self.numer
|
|
|
|
} else {
|
|
|
|
match self.denom {
|
|
|
|
Unit::Mul(..) => todo!(),
|
|
|
|
Unit::Div(..) => unreachable!(),
|
|
|
|
_ => match self.numer {
|
|
|
|
Unit::Mul(..) => todo!(),
|
|
|
|
Unit::Div(..) => unreachable!(),
|
|
|
|
Unit::None => {
|
2020-07-08 21:39:30 -04:00
|
|
|
let numer = Unit::Mul(Box::new(vec![rhs]));
|
2020-07-04 11:04:51 -04:00
|
|
|
Unit::Div(Box::new(DivUnit::new(numer, self.denom)))
|
|
|
|
}
|
|
|
|
_ => {
|
2020-07-08 21:39:30 -04:00
|
|
|
let numer = Unit::Mul(Box::new(vec![self.numer, rhs]));
|
2020-07-04 11:04:51 -04:00
|
|
|
Unit::Div(Box::new(DivUnit::new(numer, self.denom)))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// impl Div<Unit> for DivUnit {
|
|
|
|
// type Output = Unit;
|
|
|
|
// fn div(self, rhs: Unit) -> Self::Output {
|
|
|
|
// todo!()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
impl Mul<Unit> for Unit {
|
|
|
|
type Output = Unit;
|
|
|
|
fn mul(self, rhs: Unit) -> Self::Output {
|
|
|
|
match self {
|
|
|
|
Unit::Mul(u) => match rhs {
|
|
|
|
Unit::Mul(u2) => {
|
2020-07-08 21:39:30 -04:00
|
|
|
let mut unit1 = *u;
|
2022-09-02 17:00:07 -04:00
|
|
|
unit1.extend_from_slice(&u2);
|
2020-07-08 21:39:30 -04:00
|
|
|
Unit::Mul(Box::new(unit1))
|
2020-07-04 11:04:51 -04:00
|
|
|
}
|
|
|
|
Unit::Div(..) => todo!(),
|
|
|
|
_ => {
|
2020-07-08 21:39:30 -04:00
|
|
|
let mut unit1 = *u;
|
2020-07-04 11:04:51 -04:00
|
|
|
unit1.push(rhs);
|
2020-07-08 21:39:30 -04:00
|
|
|
Unit::Mul(Box::new(unit1))
|
2020-07-04 11:04:51 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Unit::Div(div) => *div * rhs,
|
|
|
|
_ => match rhs {
|
|
|
|
Unit::Mul(u2) => {
|
|
|
|
let mut unit1 = vec![self];
|
2022-09-02 17:00:07 -04:00
|
|
|
unit1.extend_from_slice(&u2);
|
2020-07-08 21:39:30 -04:00
|
|
|
Unit::Mul(Box::new(unit1))
|
2020-07-04 11:04:51 -04:00
|
|
|
}
|
|
|
|
Unit::Div(..) => todo!(),
|
2020-07-08 21:39:30 -04:00
|
|
|
_ => Unit::Mul(Box::new(vec![self, rhs])),
|
2020-07-04 11:04:51 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Div<Unit> for Unit {
|
|
|
|
type Output = Unit;
|
2022-02-08 20:22:38 -05:00
|
|
|
#[allow(clippy::if_same_then_else)]
|
2020-07-04 11:04:51 -04:00
|
|
|
fn div(self, rhs: Unit) -> Self::Output {
|
|
|
|
if let Unit::Div(..) = self {
|
|
|
|
todo!()
|
|
|
|
} else if let Unit::Div(..) = rhs {
|
|
|
|
todo!()
|
|
|
|
} else {
|
|
|
|
Unit::Div(Box::new(DivUnit::new(self, rhs)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 08:16:17 -05:00
|
|
|
impl Unit {
|
|
|
|
pub fn comparable(&self, other: &Unit) -> bool {
|
2020-03-31 02:10:22 -04:00
|
|
|
if other == &Unit::None {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-15 08:16:17 -05:00
|
|
|
match self.kind() {
|
|
|
|
UnitKind::FontRelative | UnitKind::ViewportRelative | UnitKind::Other => self == other,
|
|
|
|
UnitKind::None => true,
|
2020-03-31 02:10:22 -04:00
|
|
|
u => other.kind() == u,
|
2020-02-15 08:16:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 13:12:06 -04:00
|
|
|
/// Used internally to determine if two units are comparable or not
|
|
|
|
fn kind(&self) -> UnitKind {
|
2020-02-15 08:16:17 -05:00
|
|
|
match self {
|
2020-02-16 11:59:04 -05:00
|
|
|
Unit::Px | Unit::Mm | Unit::In | Unit::Cm | Unit::Q | Unit::Pt | Unit::Pc => {
|
|
|
|
UnitKind::Absolute
|
|
|
|
}
|
2020-02-15 08:16:17 -05:00
|
|
|
Unit::Em
|
|
|
|
| Unit::Rem
|
|
|
|
| Unit::Lh
|
|
|
|
| Unit::Ex
|
|
|
|
| Unit::Ch
|
|
|
|
| Unit::Cap
|
|
|
|
| Unit::Ic
|
|
|
|
| Unit::Rlh => UnitKind::FontRelative,
|
2020-02-16 11:59:04 -05:00
|
|
|
Unit::Vw | Unit::Vh | Unit::Vmin | Unit::Vmax | Unit::Vi | Unit::Vb => {
|
|
|
|
UnitKind::ViewportRelative
|
|
|
|
}
|
|
|
|
Unit::Deg | Unit::Grad | Unit::Rad | Unit::Turn => UnitKind::Angle,
|
|
|
|
Unit::S | Unit::Ms => UnitKind::Time,
|
|
|
|
Unit::Hz | Unit::Khz => UnitKind::Frequency,
|
|
|
|
Unit::Dpi | Unit::Dpcm | Unit::Dppx | Unit::X => UnitKind::Resolution,
|
2020-02-15 08:16:17 -05:00
|
|
|
Unit::None => UnitKind::None,
|
2020-07-04 11:04:51 -04:00
|
|
|
Unit::Fr | Unit::Percent | Unit::Unknown(..) | Unit::Mul(..) | Unit::Div(..) => {
|
|
|
|
UnitKind::Other
|
|
|
|
}
|
2020-02-15 08:16:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:01:40 -04:00
|
|
|
impl From<String> for Unit {
|
|
|
|
fn from(unit: String) -> Self {
|
|
|
|
match unit.to_ascii_lowercase().as_str() {
|
|
|
|
"px" => Unit::Px,
|
|
|
|
"mm" => Unit::Mm,
|
|
|
|
"in" => Unit::In,
|
|
|
|
"cm" => Unit::Cm,
|
|
|
|
"q" => Unit::Q,
|
|
|
|
"pt" => Unit::Pt,
|
|
|
|
"pc" => Unit::Pc,
|
|
|
|
"em" => Unit::Em,
|
|
|
|
"rem" => Unit::Rem,
|
|
|
|
"lh" => Unit::Lh,
|
|
|
|
"%" => Unit::Percent,
|
|
|
|
"ex" => Unit::Ex,
|
|
|
|
"ch" => Unit::Ch,
|
|
|
|
"cap" => Unit::Cap,
|
|
|
|
"ic" => Unit::Ic,
|
|
|
|
"rlh" => Unit::Rlh,
|
|
|
|
"vw" => Unit::Vw,
|
|
|
|
"vh" => Unit::Vh,
|
|
|
|
"vmin" => Unit::Vmin,
|
|
|
|
"vmax" => Unit::Vmax,
|
|
|
|
"vi" => Unit::Vi,
|
|
|
|
"vb" => Unit::Vb,
|
|
|
|
"deg" => Unit::Deg,
|
|
|
|
"grad" => Unit::Grad,
|
|
|
|
"rad" => Unit::Rad,
|
|
|
|
"turn" => Unit::Turn,
|
|
|
|
"s" => Unit::S,
|
|
|
|
"ms" => Unit::Ms,
|
|
|
|
"hz" => Unit::Hz,
|
|
|
|
"khz" => Unit::Khz,
|
|
|
|
"dpi" => Unit::Dpi,
|
|
|
|
"dpcm" => Unit::Dpcm,
|
|
|
|
"dppx" => Unit::Dppx,
|
|
|
|
"x" => Unit::X,
|
|
|
|
"fr" => Unit::Fr,
|
2020-07-08 21:39:30 -04:00
|
|
|
_ => Unit::Unknown(InternedString::get_or_intern(unit)),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Unit {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Unit::Px => write!(f, "px"),
|
|
|
|
Unit::Mm => write!(f, "mm"),
|
|
|
|
Unit::In => write!(f, "in"),
|
|
|
|
Unit::Cm => write!(f, "cm"),
|
2020-03-18 10:08:40 -04:00
|
|
|
Unit::Q => write!(f, "q"),
|
2020-01-04 22:55:04 -05:00
|
|
|
Unit::Pt => write!(f, "pt"),
|
|
|
|
Unit::Pc => write!(f, "pc"),
|
|
|
|
Unit::Em => write!(f, "em"),
|
|
|
|
Unit::Rem => write!(f, "rem"),
|
|
|
|
Unit::Lh => write!(f, "lh"),
|
|
|
|
Unit::Percent => write!(f, "%"),
|
|
|
|
Unit::Ex => write!(f, "ex"),
|
|
|
|
Unit::Ch => write!(f, "ch"),
|
|
|
|
Unit::Cap => write!(f, "cap"),
|
|
|
|
Unit::Ic => write!(f, "ic"),
|
|
|
|
Unit::Rlh => write!(f, "rlh"),
|
|
|
|
Unit::Vw => write!(f, "vw"),
|
|
|
|
Unit::Vh => write!(f, "vh"),
|
|
|
|
Unit::Vmin => write!(f, "vmin"),
|
|
|
|
Unit::Vmax => write!(f, "vmax"),
|
|
|
|
Unit::Vi => write!(f, "vi"),
|
|
|
|
Unit::Vb => write!(f, "vb"),
|
|
|
|
Unit::Deg => write!(f, "deg"),
|
|
|
|
Unit::Grad => write!(f, "grad"),
|
|
|
|
Unit::Rad => write!(f, "rad"),
|
|
|
|
Unit::Turn => write!(f, "turn"),
|
|
|
|
Unit::S => write!(f, "s"),
|
|
|
|
Unit::Ms => write!(f, "ms"),
|
|
|
|
Unit::Hz => write!(f, "Hz"),
|
|
|
|
Unit::Khz => write!(f, "kHz"),
|
|
|
|
Unit::Dpi => write!(f, "dpi"),
|
|
|
|
Unit::Dpcm => write!(f, "dpcm"),
|
2020-01-17 14:44:55 -05:00
|
|
|
Unit::Dppx => write!(f, "dppx"),
|
|
|
|
Unit::X => write!(f, "x"),
|
2020-01-04 22:55:04 -05:00
|
|
|
Unit::Fr => write!(f, "fr"),
|
2020-01-22 00:54:43 -05:00
|
|
|
Unit::Unknown(s) => write!(f, "{}", s),
|
2020-05-06 12:01:40 -04:00
|
|
|
Unit::None => Ok(()),
|
2020-03-17 10:06:24 -04:00
|
|
|
Unit::Mul(u) => write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
2020-03-18 20:11:14 -04:00
|
|
|
u.iter()
|
|
|
|
.map(ToString::to_string)
|
2020-03-17 10:06:24 -04:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("*")
|
|
|
|
),
|
2020-07-04 11:04:51 -04:00
|
|
|
Unit::Div(u) => write!(f, "{}", u),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|