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
//! 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, } /// 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. #[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, } impl Unit { /// Get the dimension of this unit. pub fn dimension(&self) -> Dimension { match *self { Unit::Cm | Unit::Mm | Unit::Q | Unit::In | Unit::Pc | Unit::Pt | Unit::Px => Dimension::LengthAbs, Unit::Vw => Dimension::LengthVw, Unit::Vh => Dimension::LengthVh, Unit::Vmin | Unit::Vmax => Dimension::LengthVx, Unit::Ch | Unit::Em | Unit::Ex => Dimension::LenghtEm, Unit::Rem => Dimension::LengthRem, Unit::Deg | Unit::Grad | Unit::Rad | Unit::Turn => { Dimension::Angle } Unit::S | Unit::Ms => Dimension::Time, Unit::Hz | Unit::Khz => Dimension::Frequency, Unit::Dpi | Unit::Dpcm | Unit::Dppx => Dimension::Resolution, Unit::Percent | Unit::Fr | Unit::None => Dimension::None, } } /// 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? fn scale_factor(&self) -> Number { match *self { Unit::Em | Unit::Rem => Number::rational(10, 2), Unit::Ex => Number::rational(10, 3), Unit::Ch => Number::rational(10, 4), Unit::Vw | Unit::Vh | Unit::Vmin | Unit::Vmax => one(), Unit::Cm => Number::rational(10, 1), Unit::Mm => one(), Unit::Q => Number::rational(1, 4), Unit::In => Number::rational(254, 10), Unit::Pt => Number::rational(254, 720), Unit::Pc => Number::rational(254, 60), Unit::Px => Number::rational(254, 960), Unit::Deg => Number::rational(1, 360), Unit::Grad => Number::rational(1, 400), Unit::Rad => (FRAC_1_PI / 2.0).into(), // 1/(2 pi) Unit::Turn => one(), Unit::S => one(), Unit::Ms => Number::rational(1, 1000), Unit::Hz => one(), Unit::Khz => Number::rational(1000, 1), Unit::Dpi => Number::rational(1, 96), Unit::Dpcm => Number::rational(254, 9600), Unit::Dppx => one(), Unit::Percent => Number::rational(1, 100), Unit::Fr => one(), Unit::None => one(), } } } impl fmt::Display for Unit { fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { match *self { // Distance units, <length> type Unit::Em => write!(out, "em"), Unit::Ex => write!(out, "ex"), Unit::Ch => write!(out, "ch"), Unit::Rem => write!(out, "rem"), Unit::Vw => write!(out, "vw"), Unit::Vh => write!(out, "vh"), Unit::Vmin => write!(out, "vmin"), Unit::Vmax => write!(out, "vmax"), Unit::Cm => write!(out, "cm"), Unit::Mm => write!(out, "mm"), Unit::Q => write!(out, "q"), Unit::In => write!(out, "in"), Unit::Pt => write!(out, "pt"), Unit::Pc => write!(out, "pc"), Unit::Px => write!(out, "px"), // <angle> type Unit::Deg => write!(out, "deg"), Unit::Grad => write!(out, "grad"), Unit::Rad => write!(out, "rad"), Unit::Turn => write!(out, "turn"), // <time> type Unit::S => write!(out, "s"), Unit::Ms => write!(out, "ms"), // <frequency> type Unit::Hz => write!(out, "Hz"), Unit::Khz => write!(out, "kHz"), // <resolution> Unit::Dpi => write!(out, "dpi"), Unit::Dpcm => write!(out, "dpcm"), Unit::Dppx => write!(out, "dppx"), // Special units Unit::Percent => write!(out, "%"), Unit::Fr => write!(out, "fr"), Unit::None => Ok(()), } } }