From 4a53c873f5587601c28798785df4b8f61475abf4 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 15 Sep 2015 16:23:14 -0700 Subject: [PATCH] util: Make the number of app units per pixel a constant instead of repeating the literal value everywhere. Factoring this value out into a public constant makes it easier to create `Au` constants in other modules. --- components/util/geometry.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/components/util/geometry.rs b/components/util/geometry.rs index f25b93420cc..ea88b0d1713 100644 --- a/components/util/geometry.rs +++ b/components/util/geometry.rs @@ -56,6 +56,9 @@ pub enum ViewportPx {} #[derive(RustcEncodable, Debug, Copy, Clone)] pub enum PagePx {} +/// The number of app units in a pixel. +pub const AU_PER_PX: i32 = 60; + // In summary, the hierarchy of pixel units and the factors to convert from one to the next: // // DevicePixel @@ -202,57 +205,57 @@ impl Au { #[inline] pub fn from_px(px: i32) -> Au { - Au((px * 60) as i32) + Au((px * AU_PER_PX) as i32) } #[inline] pub fn from_page_px(px: Length) -> Au { - Au((px.get() * 60f32) as i32) + Au((px.get() * (AU_PER_PX as f32)) as i32) } /// Rounds this app unit down to the pixel towards zero and returns it. #[inline] pub fn to_px(self) -> i32 { - self.0 / 60 + self.0 / AU_PER_PX } /// Rounds this app unit down to the previous (left or top) pixel and returns it. #[inline] pub fn to_prev_px(self) -> i32 { - ((self.0 as f64) / 60f64).floor() as i32 + ((self.0 as f64) / (AU_PER_PX as f64)).floor() as i32 } /// Rounds this app unit up to the next (right or bottom) pixel and returns it. #[inline] pub fn to_next_px(self) -> i32 { - ((self.0 as f64) / 60f64).ceil() as i32 + ((self.0 as f64) / (AU_PER_PX as f64)).ceil() as i32 } #[inline] pub fn to_nearest_px(self) -> i32 { - ((self.0 as f64) / 60f64).round() as i32 + ((self.0 as f64) / (AU_PER_PX as f64)).round() as i32 } #[inline] pub fn to_f32_px(self) -> f32 { - (self.0 as f32) / 60f32 + (self.0 as f32) / (AU_PER_PX as f32) } #[inline] pub fn to_f64_px(self) -> f64 { - (self.0 as f64) / 60f64 + (self.0 as f64) / (AU_PER_PX as f64) } #[inline] pub fn to_snapped(self) -> Au { - let res = self.0 % 60i32; - return if res >= 30i32 { return Au(self.0 - res + 60i32) } + let res = self.0 % AU_PER_PX; + return if res >= 30i32 { return Au(self.0 - res + AU_PER_PX) } else { return Au(self.0 - res) }; } #[inline] pub fn from_f32_px(px: f32) -> Au { - Au((px * 60f32) as i32) + Au((px * (AU_PER_PX as f32)) as i32) } #[inline] @@ -262,7 +265,7 @@ impl Au { #[inline] pub fn from_f64_px(px: f64) -> Au { - Au((px * 60.) as i32) + Au((px * (AU_PER_PX as f64)) as i32) } }