forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.rs
More file actions
148 lines (128 loc) · 3.48 KB
/
number.rs
File metadata and controls
148 lines (128 loc) · 3.48 KB
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
//! CSS number values.
use super::angle::impl_try_from_angle;
use super::calc::Calc;
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::traits::private::AddInternal;
use crate::traits::{Map, Op, Parse, Sign, ToTypst, Zero};
use cssparser::*;
/// A CSS [`<number>`](https://www.w3.org/TR/css-values-4/#numbers) value.
///
/// Numbers may be explicit or computed by `calc()`, but are always stored and serialized
/// as their computed value.
pub type CSSNumber = f32;
impl<'i> Parse<'i> for CSSNumber {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
match input.try_parse(Calc::parse) {
Ok(Calc::Value(v)) => return Ok(*v),
Ok(Calc::Number(n)) => return Ok(n),
// Numbers are always compatible, so they will always compute to a value.
Ok(_) => return Err(input.new_custom_error(ParserError::InvalidValue)),
_ => {}
}
let number = input.expect_number()?;
Ok(number)
}
}
impl ToTypst for CSSNumber {
fn to_typst<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
let number = *self;
if number != 0.0 && number.abs() < 1.0 {
// cssparser::ToCss:to_css outputs numbers < 1e-6 in exponential notation,
// which is allowed in CSS but not very readable. It's close enough to zero
// that we can just output it as zero.
if number.abs() < 0.000001 {
return dest.write_str("0");
}
let mut s = String::new();
cssparser::ToCss::to_css(self, &mut s)?;
if number < 0.0 {
dest.write_char('-')?;
dest.write_str(s.trim_start_matches("-0"))
} else {
dest.write_str(s.trim_start_matches('0'))
}
} else {
cssparser::ToCss::to_css(self, dest)?;
Ok(())
}
}
}
impl std::convert::Into<Calc<CSSNumber>> for CSSNumber {
fn into(self) -> Calc<CSSNumber> {
Calc::Value(Box::new(self))
}
}
impl std::convert::From<Calc<CSSNumber>> for CSSNumber {
fn from(calc: Calc<CSSNumber>) -> CSSNumber {
match calc {
Calc::Value(v) => *v,
Calc::Number(n) => n,
_ => unreachable!(),
}
}
}
impl AddInternal for CSSNumber {
fn add(self, other: Self) -> Self {
self + other
}
}
impl Op for CSSNumber {
fn op<F: FnOnce(f32, f32) -> f32>(&self, to: &Self, op: F) -> Self {
op(*self, *to)
}
fn op_to<T, F: FnOnce(f32, f32) -> T>(&self, rhs: &Self, op: F) -> T {
op(*self, *rhs)
}
}
impl Map for CSSNumber {
fn map<F: FnOnce(f32) -> f32>(&self, op: F) -> Self {
op(*self)
}
}
impl Sign for CSSNumber {
fn sign(&self) -> f32 {
if *self == 0.0 {
return if f32::is_sign_positive(*self) { 0.0 } else { -0.0 };
}
self.signum()
}
}
impl Zero for CSSNumber {
fn zero() -> Self {
0.0
}
fn is_zero(&self) -> bool {
*self == 0.0
}
}
impl_try_from_angle!(CSSNumber);
/// A CSS [`<integer>`](https://www.w3.org/TR/css-values-4/#integers) value.
pub type CSSInteger = i32;
impl<'i> Parse<'i> for CSSInteger {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
// TODO: calc??
let integer = input.expect_integer()?;
Ok(integer)
}
}
impl ToTypst for CSSInteger {
fn to_typst<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
cssparser::ToCss::to_css(self, dest)?;
Ok(())
}
}
impl Zero for CSSInteger {
fn zero() -> Self {
0
}
fn is_zero(&self) -> bool {
*self == 0
}
}