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
//! The Unit enum defines css units
use crate::value::Number;
use num_traits::one;
use std::f64::consts::FRAC_1_PI;
use std::fmt;
/// Units in css.
///
/// As defined in <https://www.w3.org/TR/css3-values/>
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Unit {
/// `em` unit, lengths in em-like dimension.
Em,
/// `ex` unit, lengths in em-like dimension.
Ex,
/// `ch` unit, lengths in em-like dimension.
Ch,
/// `ch` unit, lengths in rem-like dimension.
Rem,
/// `vw` unit, length relative to viewport width.
Vw,
/// `vh` unit, length relative to viewport height.
Vh,
/// `vmin` unit, length relative to min viewport size.
Vmin,
/// `vmax` unit, length relative to max viewport size.
Vmax,
/// `cm` unit, absolute length.
Cm,
/// `mm` unit, absolute length.
Mm,
/// `q` unit, absolute length (4Q == 1mm).
Q,
/// `in` unit, absolute length in inch.
In,
/// `pt` unit, absolute length (72pt == 1in).
Pt,
/// `pc`unit, absolute length (1pc == 12pt, 6pc == 1in).
Pc,
/// `px`unit, originally pixel size, but does not really mean anything now.
Px,
/// `deg` unit, angle in degrees (360 to a turn).
Deg,
/// `grad` unit, angle in grad (400 to a turn).
Grad,
/// `rad` unit, angle in degrees (2pi to a turn).
Rad,
/// `turn` unit, angle in turns.
Turn,
/// `s` unit, time in seconds.
S,
/// `ms` unit, time in milliseconds.
Ms,
/// `hz` unit, frequency in Hz.
Hz,
/// `khz` unit, frequency in kHz.
Khz,
/// `dpi` unit, resolution in dots per inch.
Dpi,
/// `dpcm` unit, resolution in dots per cm.
Dpcm,
/// `dppx` unit, resolution in dots per px unit.
Dppx,
/// `%` unit, a percentage of something.
Percent,
/// `fr` unit, for grid-relative lengths.
Fr,
/// No unit.
None,
/// An unknown (but named) unit.
Unknown(String),
}
/// Dimension of a unit.
///
/// Units of the same dimension can be converted to each other.
/// There are multiple "length" dimensions, since font-based,
/// window-based and absolute lengths can't be converted to each
/// other.
///
/// This type is for compatibility in sass functions.
/// See also [`CssDimension`].
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Dimension {
/// An absolute length, can be converted to metric.
LengthAbs,
/// A length relative to viewport width.
LengthVw,
/// A length relative to viewport height.
LengthVh,
/// A length relative to viewport size (min or max).
LengthVx,
/// A length relatvie to base font size.
LengthRem,
/// A length relative to font size.
LenghtEm,
/// An angle.
Angle,
/// A duration.
Time,
/// A frequency.
Frequency,
/// A resolution (number of pixels per length).
Resolution,
/// No dimension (no unit, percentage, or grid fraction).
None,
/// The dimension of an unknown (but named) unit.
Unknown(String),
}
impl Unit {
/// Get the dimension of this unit.
pub fn dimension(&self) -> Dimension {
match *self {
Self::Cm
| Self::Mm
| Self::Q
| Self::In
| Self::Pc
| Self::Pt
| Self::Px => Dimension::LengthAbs,
Self::Vw => Dimension::LengthVw,
Self::Vh => Dimension::LengthVh,
Self::Vmin | Self::Vmax => Dimension::LengthVx,
Self::Ch | Self::Em | Self::Ex => Dimension::LenghtEm,
Self::Rem => Dimension::LengthRem,
Self::Deg | Self::Grad | Self::Rad | Self::Turn => {
Dimension::Angle
}
Self::S | Self::Ms => Dimension::Time,
Self::Hz | Self::Khz => Dimension::Frequency,
Self::Dpi | Self::Dpcm | Self::Dppx => Dimension::Resolution,
Self::Percent | Self::Fr | Self::None => Dimension::None,
Self::Unknown(ref name) => Dimension::Unknown(name.clone()),
}
}
/// Get a scaling factor to convert this unit to another unit.
///
/// Returns None if the units are of different dimension.
pub fn scale_to(&self, other: &Self) -> Option<Number> {
if self == other {
Some(one())
} else if self.dimension() == other.dimension() {
Some(self.scale_factor() / other.scale_factor())
} else {
None
}
}
/// Some of these are exact and correct, others are more arbitrary.
/// When comparing 10cm to 4in, these factors will give correct results.
/// When comparing rems to vw, who can say?
pub(crate) fn scale_factor(&self) -> Number {
#[allow(clippy::match_same_arms)]
match *self {
Self::Em | Self::Rem => Number::rational(10, 2),
Self::Ex => Number::rational(10, 3),
Self::Ch => Number::rational(10, 4),
Self::Vw | Self::Vh | Self::Vmin | Self::Vmax => one(),
Self::Cm => Number::rational(10, 1),
Self::Mm => one(),
Self::Q => Number::rational(1, 4),
Self::In => Number::rational(254, 10),
Self::Pt => Number::rational(254, 720),
Self::Pc => Number::rational(254, 60),
Self::Px => Number::rational(254, 960),
Self::Deg => Number::rational(1, 360),
Self::Grad => Number::rational(1, 400),
Self::Rad => (FRAC_1_PI / 2.0).into(), // 1/(2 pi)
Self::Turn => one(),
Self::S => one(),
Self::Ms => Number::rational(1, 1000),
Self::Hz => one(),
Self::Khz => Number::rational(1000, 1),
Self::Dpi => Number::rational(1, 96),
Self::Dpcm => Number::rational(254, 9600),
Self::Dppx => one(),
Self::Percent => Number::rational(1, 100),
Self::Fr => one(),
Self::None => one(),
Self::Unknown(_) => one(),
}
}
}
impl fmt::Display for Unit {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
match *self {
// Distance units, <length> type
Self::Em => write!(out, "em"),
Self::Ex => write!(out, "ex"),
Self::Ch => write!(out, "ch"),
Self::Rem => write!(out, "rem"),
Self::Vw => write!(out, "vw"),
Self::Vh => write!(out, "vh"),
Self::Vmin => write!(out, "vmin"),
Self::Vmax => write!(out, "vmax"),
Self::Cm => write!(out, "cm"),
Self::Mm => write!(out, "mm"),
Self::Q => write!(out, "Q"),
Self::In => write!(out, "in"),
Self::Pt => write!(out, "pt"),
Self::Pc => write!(out, "pc"),
Self::Px => write!(out, "px"),
// <angle> type
Self::Deg => write!(out, "deg"),
Self::Grad => write!(out, "grad"),
Self::Rad => write!(out, "rad"),
Self::Turn => write!(out, "turn"),
// <time> type
Self::S => write!(out, "s"),
Self::Ms => write!(out, "ms"),
// <frequency> type
Self::Hz => write!(out, "Hz"),
Self::Khz => write!(out, "kHz"),
// <resolution>
Self::Dpi => write!(out, "dpi"),
Self::Dpcm => write!(out, "dpcm"),
Self::Dppx => write!(out, "dppx"),
// Special units
Self::Percent => write!(out, "%"),
Self::Fr => write!(out, "fr"),
Self::None => Ok(()),
Self::Unknown(ref name) => out.write_str(name),
}
}
}
/// Dimension of a unit.
///
/// Units of the same dimension can be converted to each other.
/// There is a single "length" dimension, since all lengths _can_ be
/// converted to each other in the browser, where all are converted to
/// device pixels anyway.
///
/// This type is for compatibility in css functions.
/// See also [`Dimension`].
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum CssDimension {
/// A length of any kind.
Length,
/// An angle.
Angle,
/// A duration.
Time,
/// A frequency.
Frequency,
/// A resolution (number of pixels per length).
Resolution,
/// No dimension (no unit, percentage, or grid fraction).
None,
/// The dimension of an unknown (but named) unit.
Unknown(String),
}
impl From<Dimension> for CssDimension {
fn from(dim: Dimension) -> Self {
match dim {
Dimension::LengthAbs
| Dimension::LengthVw
| Dimension::LengthVh
| Dimension::LengthVx
| Dimension::LengthRem
| Dimension::LenghtEm => Self::Length,
Dimension::Angle => Self::Angle,
Dimension::Time => Self::Time,
Dimension::Frequency => Self::Frequency,
Dimension::Resolution => Self::Resolution,
Dimension::None => Self::None,
Dimension::Unknown(s) => Self::Unknown(s),
}
}
}