[librsvg: 3/27] Length.to_user() - new function; take a NormalizeParams
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 3/27] Length.to_user() - new function; take a NormalizeParams
- Date: Mon, 10 May 2021 16:06:42 +0000 (UTC)
commit e68ac6a325b54d50077d8e457abb387680947106
Author: Federico Mena Quintero <federico gnome org>
Date: Fri May 7 19:17:02 2021 -0500
Length.to_user() - new function; take a NormalizeParams
Reimplement Length.normalize() in terms of it; we'll replace all of
its uses with to_user().
src/length.rs | 52 ++++++++++++++++++++++++++++++++--------------------
1 file changed, 32 insertions(+), 20 deletions(-)
---
diff --git a/src/length.rs b/src/length.rs
index 446c7478..7ad88245 100644
--- a/src/length.rs
+++ b/src/length.rs
@@ -377,9 +377,21 @@ impl<N: Normalize, V: Validate> CssLength<N, V> {
/// to pixels based on the current viewport (e.g. for lengths with percent units), and
/// based on the current element's set of `ComputedValues` (e.g. for lengths with `Em`
/// units that need to be resolved against the current font size).
+ ///
+ /// Those parameters can be obtained with `NormalizeParams::new()`.
pub fn normalize(&self, values: &ComputedValues, params: &ViewParams) -> f64 {
- let params = NormalizeParams::new(values, params);
+ self.to_user(&NormalizeParams::new(values, params))
+ }
+ /// Convert a Length with units into user-space coordinates.
+ ///
+ /// Lengths may come with non-pixel units, and when rendering, they need to be normalized
+ /// to pixels based on the current viewport (e.g. for lengths with percent units), and
+ /// based on the current element's set of `ComputedValues` (e.g. for lengths with `Em`
+ /// units that need to be resolved against the current font size).
+ ///
+ /// Those parameters can be obtained with `NormalizeParams::new()`.
+ pub fn to_user(&self, params: &NormalizeParams) -> f64 {
match self.unit {
LengthUnit::Px => self.length,
@@ -548,81 +560,81 @@ mod tests {
#[test]
fn normalize_default_works() {
- let params = ViewParams::new(Dpi::new(40.0, 40.0), 100.0, 100.0);
-
+ let view_params = ViewParams::new(Dpi::new(40.0, 40.0), 100.0, 100.0);
let values = ComputedValues::default();
+ let params = NormalizeParams::new(&values, &view_params);
assert_approx_eq_cairo!(
- Length::<Both>::new(10.0, LengthUnit::Px).normalize(&values, ¶ms),
+ Length::<Both>::new(10.0, LengthUnit::Px).to_user(¶ms),
10.0
);
}
#[test]
fn normalize_absolute_units_works() {
- let params = ViewParams::new(Dpi::new(40.0, 50.0), 100.0, 100.0);
-
+ let view_params = ViewParams::new(Dpi::new(40.0, 50.0), 100.0, 100.0);
let values = ComputedValues::default();
+ let params = NormalizeParams::new(&values, &view_params);
assert_approx_eq_cairo!(
- Length::<Horizontal>::new(10.0, LengthUnit::In).normalize(&values, ¶ms),
+ Length::<Horizontal>::new(10.0, LengthUnit::In).to_user(¶ms),
400.0
);
assert_approx_eq_cairo!(
- Length::<Vertical>::new(10.0, LengthUnit::In).normalize(&values, ¶ms),
+ Length::<Vertical>::new(10.0, LengthUnit::In).to_user(¶ms),
500.0
);
assert_approx_eq_cairo!(
- Length::<Horizontal>::new(10.0, LengthUnit::Cm).normalize(&values, ¶ms),
+ Length::<Horizontal>::new(10.0, LengthUnit::Cm).to_user(¶ms),
400.0 / CM_PER_INCH
);
assert_approx_eq_cairo!(
- Length::<Horizontal>::new(10.0, LengthUnit::Mm).normalize(&values, ¶ms),
+ Length::<Horizontal>::new(10.0, LengthUnit::Mm).to_user(¶ms),
400.0 / MM_PER_INCH
);
assert_approx_eq_cairo!(
- Length::<Horizontal>::new(10.0, LengthUnit::Pt).normalize(&values, ¶ms),
+ Length::<Horizontal>::new(10.0, LengthUnit::Pt).to_user(¶ms),
400.0 / POINTS_PER_INCH
);
assert_approx_eq_cairo!(
- Length::<Horizontal>::new(10.0, LengthUnit::Pc).normalize(&values, ¶ms),
+ Length::<Horizontal>::new(10.0, LengthUnit::Pc).to_user(¶ms),
400.0 / PICA_PER_INCH
);
}
#[test]
fn normalize_percent_works() {
- let params = ViewParams::new(Dpi::new(40.0, 40.0), 100.0, 200.0);
-
+ let view_params = ViewParams::new(Dpi::new(40.0, 40.0), 100.0, 200.0);
let values = ComputedValues::default();
+ let params = NormalizeParams::new(&values, &view_params);
assert_approx_eq_cairo!(
- Length::<Horizontal>::new(0.05, LengthUnit::Percent).normalize(&values, ¶ms),
+ Length::<Horizontal>::new(0.05, LengthUnit::Percent).to_user(¶ms),
5.0
);
assert_approx_eq_cairo!(
- Length::<Vertical>::new(0.05, LengthUnit::Percent).normalize(&values, ¶ms),
+ Length::<Vertical>::new(0.05, LengthUnit::Percent).to_user(¶ms),
10.0
);
}
#[test]
fn normalize_font_em_ex_works() {
- let params = ViewParams::new(Dpi::new(40.0, 40.0), 100.0, 200.0);
-
+ let view_params = ViewParams::new(Dpi::new(40.0, 40.0), 100.0, 200.0);
let values = ComputedValues::default();
+ let params = NormalizeParams::new(&values, &view_params);
// These correspond to the default size for the font-size
// property and the way we compute Em/Ex from that.
assert_approx_eq_cairo!(
- Length::<Vertical>::new(1.0, LengthUnit::Em).normalize(&values, ¶ms),
+ Length::<Vertical>::new(1.0, LengthUnit::Em).to_user(¶ms),
12.0
);
assert_approx_eq_cairo!(
- Length::<Vertical>::new(1.0, LengthUnit::Ex).normalize(&values, ¶ms),
+ Length::<Vertical>::new(1.0, LengthUnit::Ex).to_user(¶ms),
6.0
);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]