|
| 1 | +use cssparser::*; |
| 2 | +use crate::traits::{Parse, ToCss}; |
| 3 | +use crate::printer::Printer; |
| 4 | +use std::fmt::Write; |
| 5 | +use super::calc::Calc; |
| 6 | +use std::f32::consts::PI; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, PartialEq)] |
| 9 | +pub enum Angle { |
| 10 | + Deg(f32), |
| 11 | + Grad(f32), |
| 12 | + Rad(f32), |
| 13 | + Turn(f32), |
| 14 | + Calc(Calc<Angle>) |
| 15 | +} |
| 16 | + |
| 17 | +impl Parse for Angle { |
| 18 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> { |
| 19 | + let location = input.current_source_location(); |
| 20 | + let token = input.next()?; |
| 21 | + match *token { |
| 22 | + Token::Dimension { value, ref unit, .. } => { |
| 23 | + match_ignore_ascii_case! { unit, |
| 24 | + "deg" => Ok(Angle::Deg(value)), |
| 25 | + "grad" => Ok(Angle::Grad(value)), |
| 26 | + "turn" => Ok(Angle::Turn(value)), |
| 27 | + "rad" => Ok(Angle::Rad(value)), |
| 28 | + _ => return Err(location.new_unexpected_token_error(token.clone())), |
| 29 | + } |
| 30 | + }, |
| 31 | + Token::Function(ref name) => { |
| 32 | + match_ignore_ascii_case! { name, |
| 33 | + "calc" => { |
| 34 | + match Calc::parse(input)? { |
| 35 | + Calc::Value(v) => Ok(*v), |
| 36 | + v => Ok(Angle::Calc(v)) |
| 37 | + } |
| 38 | + }, |
| 39 | + _ => Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid)) |
| 40 | + } |
| 41 | + } |
| 42 | + ref token => return Err(location.new_unexpected_token_error(token.clone())), |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl ToCss for Angle { |
| 48 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write { |
| 49 | + let (value, unit) = match self { |
| 50 | + Angle::Deg(val) => (*val, "deg"), |
| 51 | + Angle::Grad(val) => (*val, "grad"), |
| 52 | + Angle::Rad(val) => { |
| 53 | + if let Some(deg) = self.to_degrees() { |
| 54 | + // We print 5 digits of precision by default. |
| 55 | + // Switch to degrees if there are an even number of them. |
| 56 | + if (deg * 100000.0).round().fract() == 0.0 { |
| 57 | + (deg, "deg") |
| 58 | + } else { |
| 59 | + (*val, "rad") |
| 60 | + } |
| 61 | + } else { |
| 62 | + (*val, "rad") |
| 63 | + } |
| 64 | + }, |
| 65 | + Angle::Turn(val) => (*val, "turn"), |
| 66 | + Angle::Calc(calc) => { |
| 67 | + if let Calc::Value(v) = calc { |
| 68 | + v.to_css(dest)?; |
| 69 | + } else { |
| 70 | + dest.write_str("calc(")?; |
| 71 | + calc.to_css(dest)?; |
| 72 | + dest.write_char(')')?; |
| 73 | + } |
| 74 | + return Ok(()) |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + use cssparser::ToCss; |
| 79 | + let int_value = if value.fract() == 0.0 { |
| 80 | + Some(value as i32) |
| 81 | + } else { |
| 82 | + None |
| 83 | + }; |
| 84 | + let token = Token::Dimension { |
| 85 | + has_sign: value < 0.0, |
| 86 | + value, |
| 87 | + int_value, |
| 88 | + unit: CowRcStr::from(unit) |
| 89 | + }; |
| 90 | + if value != 0.0 && value.abs() < 1.0 { |
| 91 | + let mut s = String::new(); |
| 92 | + token.to_css(&mut s)?; |
| 93 | + if value < 0.0 { |
| 94 | + dest.write_char('-')?; |
| 95 | + dest.write_str(s.trim_start_matches("-0")) |
| 96 | + } else { |
| 97 | + dest.write_str(s.trim_start_matches('0')) |
| 98 | + } |
| 99 | + } else { |
| 100 | + token.to_css(dest) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl Angle { |
| 106 | + pub fn is_zero(&self) -> bool { |
| 107 | + use Angle::*; |
| 108 | + match self { |
| 109 | + Deg(v) | Rad(v) | Grad(v) | Turn(v) => *v == 0.0, |
| 110 | + Calc(_) => false |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + pub fn to_radians(&self) -> Option<f32> { |
| 115 | + const RAD_PER_DEG: f32 = PI / 180.0; |
| 116 | + let r = match self { |
| 117 | + Angle::Deg(deg) => deg * RAD_PER_DEG, |
| 118 | + Angle::Rad(rad) => *rad, |
| 119 | + Angle::Grad(grad) => grad * 180.0 / 200.0 * RAD_PER_DEG, |
| 120 | + Angle::Turn(turn) => turn * 360.0 * RAD_PER_DEG, |
| 121 | + Angle::Calc(_) => return None |
| 122 | + }; |
| 123 | + Some(r) |
| 124 | + } |
| 125 | + |
| 126 | + pub fn to_degrees(&self) -> Option<f32> { |
| 127 | + const DEG_PER_RAD: f32 = 180.0 / PI; |
| 128 | + let d = match self { |
| 129 | + Angle::Deg(deg) => *deg, |
| 130 | + Angle::Rad(rad) => rad * DEG_PER_RAD, |
| 131 | + Angle::Grad(grad) => grad * 180.0 / 200.0, |
| 132 | + Angle::Turn(turn) => turn * 360.0, |
| 133 | + Angle::Calc(_) => return None |
| 134 | + }; |
| 135 | + Some(d) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl std::ops::Mul<f32> for Angle { |
| 140 | + type Output = Self; |
| 141 | + |
| 142 | + fn mul(self, other: f32) -> Angle { |
| 143 | + match self { |
| 144 | + Angle::Deg(v) => Angle::Deg(v * other), |
| 145 | + Angle::Rad(v) => Angle::Deg(v * other), |
| 146 | + Angle::Grad(v) => Angle::Deg(v * other), |
| 147 | + Angle::Turn(v) => Angle::Deg(v * other), |
| 148 | + Angle::Calc(c) => Angle::Calc(c * other) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl std::ops::Add<Angle> for Angle { |
| 154 | + type Output = Self; |
| 155 | + |
| 156 | + fn add(self, other: Angle) -> Angle { |
| 157 | + match (self, other) { |
| 158 | + (Angle::Calc(a), Angle::Calc(b)) => Angle::Calc(a + b), |
| 159 | + (Angle::Calc(a), b) => Angle::Calc(a + Calc::Value(Box::new(b))), |
| 160 | + (a, Angle::Calc(b)) => Angle::Calc(Calc::Value(Box::new(a)) + b), |
| 161 | + (a, b) => Angle::Deg(a.to_degrees().unwrap() + b.to_degrees().unwrap()) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl std::cmp::PartialEq<f32> for Angle { |
| 167 | + fn eq(&self, other: &f32) -> bool { |
| 168 | + match self { |
| 169 | + Angle::Deg(a) | Angle::Rad(a) | Angle::Grad(a) | Angle::Turn(a) => a == other, |
| 170 | + Angle::Calc(_) => false |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +impl std::cmp::PartialOrd<f32> for Angle { |
| 176 | + fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> { |
| 177 | + match self { |
| 178 | + Angle::Deg(a) | Angle::Rad(a) | Angle::Grad(a) | Angle::Turn(a) => a.partial_cmp(other), |
| 179 | + Angle::Calc(_) => None |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments