mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Split Au type into separate crate, with minimal dependencies.
This commit is contained in:
parent
fb6d0946cb
commit
339a3f869b
72 changed files with 376 additions and 235 deletions
|
@ -41,6 +41,9 @@ features = [ "serde_serialization" ]
|
|||
[dependencies.selectors]
|
||||
git = "https://github.com/servo/rust-selectors"
|
||||
|
||||
[dependencies.app_units]
|
||||
path = "../app_units"
|
||||
|
||||
[dependencies]
|
||||
log = "0.3"
|
||||
bitflags = "0.3"
|
||||
|
|
|
@ -2,16 +2,12 @@
|
|||
* 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 cssparser::ToCss;
|
||||
use euclid::num::Zero;
|
||||
use app_units::{Au, MAX_AU};
|
||||
use euclid::point::Point2D;
|
||||
use euclid::rect::Rect;
|
||||
use euclid::size::Size2D;
|
||||
use rustc_serialize::{Encodable, Encoder};
|
||||
use std::default::Default;
|
||||
use std::fmt;
|
||||
use std::i32;
|
||||
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
|
||||
use std::ops::Add;
|
||||
|
||||
// Units for use with euclid::length and euclid::scale_factor.
|
||||
|
||||
|
@ -52,9 +48,6 @@ 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
|
||||
|
@ -65,24 +58,6 @@ pub const AU_PER_PX: i32 = 60;
|
|||
// An Au is an "App Unit" and represents 1/60th of a CSS pixel. It was
|
||||
// originally proposed in 2002 as a standard unit of measure in Gecko.
|
||||
// See https://bugzilla.mozilla.org/show_bug.cgi?id=177805 for more info.
|
||||
//
|
||||
// FIXME: Implement Au using Length and ScaleFactor instead of a custom type.
|
||||
#[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 static ZERO_POINT: Point2D<Au> = Point2D {
|
||||
x: Au(0),
|
||||
|
@ -111,136 +86,6 @@ pub static MAX_RECT: Rect<Au> = Rect {
|
|||
}
|
||||
};
|
||||
|
||||
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 ToCss for Au {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
write!(dest, "{}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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the rect contains the given point. Points on the top or left sides of the rect
|
||||
/// are considered inside the rectangle, while points on the right or bottom sides of the rect are
|
||||
/// not considered inside the rectangle.
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#![plugin(plugins, serde_macros)]
|
||||
|
||||
extern crate app_units;
|
||||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
#[macro_use]
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
//! Data structure measurement.
|
||||
|
||||
use app_units::Au;
|
||||
use azure::azure_hl::Color;
|
||||
use cssparser::Color as CSSParserColor;
|
||||
use cssparser::{RGBA, TokenSerializationType};
|
||||
|
@ -11,7 +12,7 @@ use cursor::Cursor;
|
|||
use euclid::length::Length;
|
||||
use euclid::scale_factor::ScaleFactor;
|
||||
use euclid::{Matrix2D, Matrix4, Point2D, Rect, SideOffsets2D, Size2D};
|
||||
use geometry::{Au, PagePx, ViewportPx};
|
||||
use geometry::{PagePx, ViewportPx};
|
||||
use html5ever::tree_builder::QuirksMode;
|
||||
use hyper::header::ContentType;
|
||||
use hyper::http::RawStatus;
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
* 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 app_units::Au;
|
||||
use cssparser::{self, Color, RGBA};
|
||||
use geometry::Au;
|
||||
use libc::c_char;
|
||||
use num_lib::ToPrimitive;
|
||||
use std::ascii::AsciiExt;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue