diff options
author | bors-servo <metajack+bors@gmail.com> | 2015-09-30 15:19:33 -0600 |
---|---|---|
committer | bors-servo <metajack+bors@gmail.com> | 2015-09-30 15:19:33 -0600 |
commit | 35888e5a1d48511ec54ddf8c58f1c7b0c47c5d3c (patch) | |
tree | 06790c2e03a77bdbb988361dcf8e0b34b97dec4b /components/app_units/src | |
parent | fb6d0946cb3bac713bc20794f17a40fa7a12bc00 (diff) | |
parent | 339a3f869b539ae6da49f5d34568789d0abf3e00 (diff) | |
download | servo-35888e5a1d48511ec54ddf8c58f1c7b0c47c5d3c.tar.gz servo-35888e5a1d48511ec54ddf8c58f1c7b0c47c5d3c.zip |
Auto merge of #7795 - glennw:app-units-crate, r=SimonSapin
Split Au type into separate crate, with minimal dependencies.
<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7795)
<!-- Reviewable:end -->
Diffstat (limited to 'components/app_units/src')
-rw-r--r-- | components/app_units/src/app_unit.rs | 154 | ||||
-rw-r--r-- | components/app_units/src/lib.rs | 16 |
2 files changed, 170 insertions, 0 deletions
diff --git a/components/app_units/src/app_unit.rs b/components/app_units/src/app_unit.rs new file mode 100644 index 00000000000..a94c2bac04b --- /dev/null +++ b/components/app_units/src/app_unit.rs @@ -0,0 +1,154 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use euclid::num::Zero; +use rustc_serialize::{Encodable, Encoder}; +use std::default::Default; +use std::fmt; +use std::i32; +use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; + +/// The number of app units in a pixel. +pub const AU_PER_PX: i32 = 60; + +#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord, Deserialize, Serialize)] +pub struct Au(pub i32); + +impl Default for Au { + #[inline] + fn default() -> Au { + Au(0) + } +} + +impl Zero for Au { + #[inline] + fn zero() -> Au { + Au(0) + } +} + +pub const MIN_AU: Au = Au(i32::MIN); +pub const MAX_AU: Au = Au(i32::MAX); + +impl Encodable for Au { + fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> { + e.emit_f64(self.to_f64_px()) + } +} + +impl fmt::Debug for Au { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}px", self.to_f64_px()) + } +} + +impl Add for Au { + type Output = Au; + + #[inline] + fn add(self, other: Au) -> Au { + Au(self.0.wrapping_add(other.0)) + } +} + +impl Sub for Au { + type Output = Au; + + #[inline] + fn sub(self, other: Au) -> Au { + Au(self.0.wrapping_sub(other.0)) + } + +} + +impl Mul<i32> for Au { + type Output = Au; + + #[inline] + fn mul(self, other: i32) -> Au { + Au(self.0.wrapping_mul(other)) + } +} + +impl Div<i32> for Au { + type Output = Au; + + #[inline] + fn div(self, other: i32) -> Au { + Au(self.0 / other) + } +} + +impl Rem<i32> for Au { + type Output = Au; + + #[inline] + fn rem(self, other: i32) -> Au { + Au(self.0 % other) + } +} + +impl Neg for Au { + type Output = Au; + + #[inline] + fn neg(self) -> Au { + Au(-self.0) + } +} + +impl Au { + /// FIXME(pcwalton): Workaround for lack of cross crate inlining of newtype structs! + #[inline] + pub fn new(value: i32) -> Au { + Au(value) + } + + #[inline] + pub fn scale_by(self, factor: f32) -> Au { + Au(((self.0 as f32) * factor) as i32) + } + + #[inline] + pub fn from_px(px: i32) -> Au { + Au((px * AU_PER_PX) as i32) + } + + /// Rounds this app unit down to the pixel towards zero and returns it. + #[inline] + pub fn to_px(self) -> i32 { + self.0 / AU_PER_PX + } + + #[inline] + pub fn to_nearest_px(self) -> i32 { + ((self.0 as f64) / (AU_PER_PX as f64)).round() as i32 + } + + #[inline] + pub fn to_nearest_pixel(self, pixels_per_px: f32) -> f32 { + ((self.0 as f32) / (AU_PER_PX as f32) * pixels_per_px).round() / pixels_per_px + } + + #[inline] + pub fn to_f32_px(self) -> f32 { + (self.0 as f32) / (AU_PER_PX as f32) + } + + #[inline] + pub fn to_f64_px(self) -> f64 { + (self.0 as f64) / (AU_PER_PX as f64) + } + + #[inline] + pub fn from_f32_px(px: f32) -> Au { + Au((px * (AU_PER_PX as f32)) as i32) + } + + #[inline] + pub fn from_f64_px(px: f64) -> Au { + Au((px * (AU_PER_PX as f64)) as i32) + } +} diff --git a/components/app_units/src/lib.rs b/components/app_units/src/lib.rs new file mode 100644 index 00000000000..2d1952dc3aa --- /dev/null +++ b/components/app_units/src/lib.rs @@ -0,0 +1,16 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#![feature(custom_derive)] +#![feature(plugin)] + +#![plugin(serde_macros)] + +extern crate euclid; +extern crate rustc_serialize; +extern crate serde; + +mod app_unit; + +pub use app_unit::{Au, MIN_AU, MAX_AU, AU_PER_PX}; |