/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#[cfg(feature = "num_traits")] use num_traits::Zero; #[cfg(feature = "serde_serialization")] use serde::{Serialize, Deserialize, Deserializer};
/// The number of app units in a pixel. pubconst AU_PER_PX: i32 = 60; /// The minimum number of app units, same as in Gecko. pubconst MIN_AU: Au = Au(- ((1 << 30) - 1)); /// The maximum number of app units, same as in Gecko. /// /// (1 << 30) - 1 lets us add/subtract two Au and check for overflow after the operation. pubconst MAX_AU: Au = Au((1 << 30) - 1);
#[repr(transparent)] #[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord, Default)] #[cfg_attr(feature = "serde_serialization", derive(Serialize), serde(transparent))] /// An App Unit, the fundamental unit of length in Servo. Usually /// 1/60th of a pixel (see `AU_PER_PX`) /// /// Please ensure that the values are between `MIN_AU` and `MAX_AU`. /// It is safe to construct invalid `Au` values, but it may lead to /// panics and overflows. pubstruct Au(pub i32);
impl fmt::Debug for Au { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}px", self.to_f64_px())
}
}
#[cfg(feature = "serde_serialization")] impl<'de> Deserialize<'de> for Au { fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Au, D::Error> {
Ok(Au(i32::deserialize(deserializer)?).clamp())
}
}
#[cfg(feature = "num_traits")] impl Zero for Au { #[inline] fn zero() -> Au {
Au(0)
}
impl AddAssign for Au { #[inline] fn add_assign(&mutself, other: Au) {
*self = (*self + other).clamp();
}
}
impl SubAssign for Au { #[inline] fn sub_assign(&mutself, other: Au) {
*self = (*self - other).clamp();
}
}
impl MulAssign<i32> for Au { #[inline] fn mul_assign(&mutself, other: i32) {
*self = (*self * other).clamp();
}
}
impl DivAssign<i32> for Au { #[inline] fn div_assign(&mutself, other: i32) {
*self = (*self / other).clamp();
}
}
impl Au { /// FIXME(pcwalton): Workaround for lack of cross crate inlining of newtype structs! #[inline] pubfn new(value: i32) -> Au {
Au(value).clamp()
}
/// Round this app unit down to the pixel towards zero and return it. #[inline] pubfn to_px(self) -> i32 { self.0 / AU_PER_PX
}
/// Ceil this app unit to the appropriate pixel boundary and return it. #[inline] pubfn ceil_to_px(self) -> i32 {
((self.0as f64) / (AU_PER_PX as f64)).ceil() as i32
}
#[inline] pubfn to_nearest_px(self) -> i32 {
((self.0as f64) / (AU_PER_PX as f64)).round() as i32
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.