diff --git a/components/app_units/Cargo.toml b/components/app_units/Cargo.toml new file mode 100644 index 00000000000..d324f7cb27f --- /dev/null +++ b/components/app_units/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "app_units" +version = "0.1.0" +authors = ["The Servo Project Developers"] + +[dependencies] +serde = "0.6" +serde_macros = "0.5" +rustc-serialize = "0.3" +euclid = "0.2" 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(&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 for Au { + type Output = Au; + + #[inline] + fn mul(self, other: i32) -> Au { + Au(self.0.wrapping_mul(other)) + } +} + +impl Div for Au { + type Output = Au; + + #[inline] + fn div(self, other: i32) -> Au { + Au(self.0 / other) + } +} + +impl Rem 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}; diff --git a/components/compositing/Cargo.toml b/components/compositing/Cargo.toml index 4a45d43a43e..bfd8e7578d8 100644 --- a/components/compositing/Cargo.toml +++ b/components/compositing/Cargo.toml @@ -7,6 +7,9 @@ authors = ["The Servo Project Developers"] name = "compositing" path = "lib.rs" +[dependencies.app_units] +path = "../app_units" + [dependencies.gfx] path = "../gfx" diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index 20f39eba587..ca0b8e82001 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -2,6 +2,7 @@ * 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 compositor_layer::{CompositorData, CompositorLayer, RcCompositorLayer, WantsScrollEventsFlag}; use compositor_task::{CompositorEventListener, CompositorProxy}; use compositor_task::{CompositorReceiver, InitialCompositorState, Msg}; @@ -46,7 +47,7 @@ use style_traits::viewport::ViewportConstraints; use surface_map::SurfaceMap; use time::{precise_time_ns, precise_time_s}; use url::Url; -use util::geometry::{Au, PagePx, ScreenPx, ViewportPx}; +use util::geometry::{PagePx, ScreenPx, ViewportPx}; use util::opts; use windowing::{self, MouseWindowEvent, WindowEvent, WindowMethods, WindowNavigateMsg}; diff --git a/components/compositing/lib.rs b/components/compositing/lib.rs index d92cb219286..1a0ea4012a3 100644 --- a/components/compositing/lib.rs +++ b/components/compositing/lib.rs @@ -11,6 +11,7 @@ #![deny(unsafe_code)] +extern crate app_units; #[macro_use] extern crate log; #[macro_use] diff --git a/components/gfx/Cargo.toml b/components/gfx/Cargo.toml index 0ae96976805..227f96ecac7 100644 --- a/components/gfx/Cargo.toml +++ b/components/gfx/Cargo.toml @@ -25,6 +25,9 @@ string_cache = "0.1" time = "0.1.12" unicode-script = { version = "0.1", features = ["harfbuzz"] } +[dependencies.app_units] +path = "../app_units" + [dependencies.plugins] path = "../plugins" diff --git a/components/gfx/display_list/mod.rs b/components/gfx/display_list/mod.rs index 7619f9d5ac9..792bb4df7ac 100644 --- a/components/gfx/display_list/mod.rs +++ b/components/gfx/display_list/mod.rs @@ -14,6 +14,7 @@ //! They are therefore not exactly analogous to constructs like Skia pictures, which consist of //! low-level drawing primitives. +use app_units::Au; use azure::azure::AzFloat; use azure::azure_hl::{Color, DrawTarget}; use display_list::optimizer::DisplayListOptimizer; @@ -40,7 +41,7 @@ use style::properties::ComputedValues; use text::TextRun; use text::glyph::CharIndex; use util::cursor::Cursor; -use util::geometry::{self, Au, MAX_RECT, ZERO_RECT}; +use util::geometry::{self, MAX_RECT, ZERO_RECT}; use util::linked_list::prepend_from; use util::mem::HeapSizeOf; use util::opts; diff --git a/components/gfx/display_list/optimizer.rs b/components/gfx/display_list/optimizer.rs index 686007b8788..cda26de2d96 100644 --- a/components/gfx/display_list/optimizer.rs +++ b/components/gfx/display_list/optimizer.rs @@ -4,12 +4,13 @@ //! Transforms a display list to produce a visually-equivalent, but cheaper-to-paint, one. +use app_units::Au; use display_list::{DisplayItem, DisplayList, StackingContext}; use euclid::rect::Rect; use euclid::{Matrix2D, Matrix4}; use std::collections::linked_list::LinkedList; use std::sync::Arc; -use util::geometry::{self, Au}; +use util::geometry; /// Transforms a display list to produce a visually-equivalent, but cheaper-to-paint, one. pub struct DisplayListOptimizer { diff --git a/components/gfx/filters.rs b/components/gfx/filters.rs index e0c6e5ca3ea..78077dcca32 100644 --- a/components/gfx/filters.rs +++ b/components/gfx/filters.rs @@ -4,13 +4,13 @@ //! CSS and SVG filter support. +use app_units::Au; use azure::AzFloat; use azure::azure_hl::{ColorMatrixAttribute, ColorMatrixInput, CompositeInput, DrawTarget}; use azure::azure_hl::{FilterNode, FilterType, LinearTransferAttribute, LinearTransferInput}; use azure::azure_hl::{GaussianBlurAttribute, GaussianBlurInput}; use azure::azure_hl::{Matrix5x4, TableTransferAttribute, TableTransferInput}; use style::computed_values::filter; -use util::geometry::Au; /// Creates a filter pipeline from a set of CSS filters. Returns the destination end of the filter /// pipeline and the opacity. diff --git a/components/gfx/font.rs b/components/gfx/font.rs index 3e64d4628ce..2ed72e55203 100644 --- a/components/gfx/font.rs +++ b/components/gfx/font.rs @@ -2,6 +2,7 @@ * 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 euclid::{Point2D, Rect, Size2D}; use font_template::FontTemplateDescriptor; use platform::font::{FontHandle, FontTable}; @@ -20,7 +21,6 @@ use text::glyph::{GlyphId, GlyphStore}; use text::shaping::ShaperMethods; use unicode_script::Script; use util::cache::HashCache; -use util::geometry::Au; // FontHandle encapsulates access to the platform's font API, // e.g. quartz, FreeType. It provides access to metrics and tables diff --git a/components/gfx/font_context.rs b/components/gfx/font_context.rs index ff2ba9fc256..3ce27835348 100644 --- a/components/gfx/font_context.rs +++ b/components/gfx/font_context.rs @@ -2,6 +2,7 @@ * 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 azure::azure_hl::BackendType; #[cfg(any(target_os = "linux", target_os = "android"))] use azure::scaled_font::FontInfo; @@ -28,7 +29,6 @@ use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; use string_cache::Atom; use style::computed_values::{font_style, font_variant}; use util::cache::HashCache; -use util::geometry::Au; use util::mem::HeapSizeOf; #[cfg(any(target_os = "linux", target_os = "android"))] diff --git a/components/gfx/lib.rs b/components/gfx/lib.rs index 80fe5ced4aa..c1848813464 100644 --- a/components/gfx/lib.rs +++ b/components/gfx/lib.rs @@ -20,6 +20,7 @@ #![plugin(plugins)] #![plugin(serde_macros)] +extern crate app_units; #[macro_use] extern crate bitflags; #[macro_use] diff --git a/components/gfx/paint_context.rs b/components/gfx/paint_context.rs index 87463981aa8..8ac75733061 100644 --- a/components/gfx/paint_context.rs +++ b/components/gfx/paint_context.rs @@ -4,6 +4,7 @@ //! Painting of display lists using Moz2D/Azure. +use app_units::Au; use azure::azure::AzIntSize; use azure::azure_hl::{AntialiasMode, Color, ColorPattern, CompositionOp}; use azure::azure_hl::{CapStyle, JoinStyle}; @@ -34,7 +35,7 @@ use std::{f32, mem, ptr}; use style::computed_values::{border_style, filter, image_rendering, mix_blend_mode}; use text::TextRun; use text::glyph::CharIndex; -use util::geometry::{self, Au, MAX_RECT, ZERO_POINT, ZERO_RECT}; +use util::geometry::{self, MAX_RECT, ZERO_POINT, ZERO_RECT}; use util::opts; use util::range::Range; diff --git a/components/gfx/paint_task.rs b/components/gfx/paint_task.rs index 683d96d2e71..c63c84d9970 100644 --- a/components/gfx/paint_task.rs +++ b/components/gfx/paint_task.rs @@ -4,6 +4,7 @@ //! The task that handles all painting. +use app_units::Au; use azure::AzFloat; use azure::azure_hl::{BackendType, Color, DrawTarget, SurfaceFormat}; use canvas_traits::CanvasMsg; @@ -33,7 +34,7 @@ use std::mem as std_mem; use std::sync::Arc; use std::sync::mpsc::{Receiver, Select, Sender, channel}; use url::Url; -use util::geometry::{Au, ZERO_POINT}; +use util::geometry::ZERO_POINT; use util::opts; use util::task::spawn_named; use util::task::spawn_named_with_send_on_failure; diff --git a/components/gfx/platform/freetype/font.rs b/components/gfx/platform/freetype/font.rs index 3bce3f06c50..cbb785919c7 100644 --- a/components/gfx/platform/freetype/font.rs +++ b/components/gfx/platform/freetype/font.rs @@ -4,6 +4,7 @@ extern crate freetype; +use app_units::Au; use font::{FontHandleMethods, FontMetrics, FontTableMethods}; use font::{FontTableTag, FractionalPixel}; use freetype::freetype::{FTErrorMethods, FT_F26Dot6, FT_Face, FT_FaceRec}; @@ -24,7 +25,6 @@ use std::{mem, ptr}; use style::computed_values::{font_stretch, font_weight}; use text::glyph::GlyphId; use text::util::{fixed_to_float, float_to_fixed}; -use util::geometry::Au; use util::str::c_str_to_string; fn float_to_fixed_ft(f: f64) -> i32 { diff --git a/components/gfx/platform/macos/font.rs b/components/gfx/platform/macos/font.rs index 0cf95aa6c3f..f316dd4d985 100644 --- a/components/gfx/platform/macos/font.rs +++ b/components/gfx/platform/macos/font.rs @@ -8,6 +8,7 @@ extern crate core_foundation; extern crate core_graphics; extern crate core_text; +use app_units::Au; use core_foundation::base::CFIndex; use core_foundation::data::CFData; use core_foundation::string::UniChar; @@ -25,7 +26,6 @@ use std::ptr; use std::sync::Arc; use style::computed_values::{font_stretch, font_weight}; use text::glyph::GlyphId; -use util::geometry::Au; pub struct FontTable { data: CFData, diff --git a/components/gfx/text/glyph.rs b/components/gfx/text/glyph.rs index 71b42fe2c9e..700a44e3d95 100644 --- a/components/gfx/text/glyph.rs +++ b/components/gfx/text/glyph.rs @@ -2,13 +2,13 @@ * 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 euclid::point::Point2D; #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] use simd::u32x4; use std::cmp::{Ordering, PartialOrd}; use std::vec::Vec; use std::{fmt, mem, u16}; -use util::geometry::Au; use util::range::{self, EachIndex, Range, RangeIndex}; use util::vec::*; diff --git a/components/gfx/text/shaping/harfbuzz.rs b/components/gfx/text/shaping/harfbuzz.rs index 5a539358779..d4b05221be0 100644 --- a/components/gfx/text/shaping/harfbuzz.rs +++ b/components/gfx/text/shaping/harfbuzz.rs @@ -2,6 +2,7 @@ * 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 euclid::Point2D; use font::{DISABLE_KERNING_SHAPING_FLAG, Font, FontHandleMethods, FontTableMethods, FontTableTag}; use font::{IGNORE_LIGATURES_SHAPING_FLAG, RTL_FLAG, ShapingOptions}; @@ -37,7 +38,6 @@ use std::{char, cmp, ptr}; use text::glyph::{CharIndex, GlyphData, GlyphId, GlyphStore}; use text::shaping::ShaperMethods; use text::util::{fixed_to_float, float_to_fixed, is_bidi_control}; -use util::geometry::Au; use util::range::Range; macro_rules! hb_tag { diff --git a/components/gfx/text/text_run.rs b/components/gfx/text/text_run.rs index 77ee7a25fa3..7d4023a58e0 100644 --- a/components/gfx/text/text_run.rs +++ b/components/gfx/text/text_run.rs @@ -2,6 +2,7 @@ * 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 font::{Font, FontHandleMethods, FontMetrics, IS_WHITESPACE_SHAPING_FLAG, RunMetrics}; use font::{ShapingOptions}; use platform::font_template::FontTemplateData; @@ -9,7 +10,6 @@ use std::cmp::{Ordering, max}; use std::slice::Iter; use std::sync::Arc; use text::glyph::{CharIndex, GlyphStore}; -use util::geometry::Au; use util::range::Range; use util::vec::{Comparator, FullBinarySearchMethods}; diff --git a/components/layout/Cargo.toml b/components/layout/Cargo.toml index 85f3d42f5f7..8cccb371428 100644 --- a/components/layout/Cargo.toml +++ b/components/layout/Cargo.toml @@ -7,6 +7,9 @@ authors = ["The Servo Project Developers"] name = "layout" path = "lib.rs" +[dependencies.app_units] +path = "../app_units" + [dependencies.azure] git = "https://github.com/servo/rust-azure" diff --git a/components/layout/block.rs b/components/layout/block.rs index 970d6a1c315..8203acaf850 100644 --- a/components/layout/block.rs +++ b/components/layout/block.rs @@ -27,6 +27,7 @@ #![deny(unsafe_code)] +use app_units::{Au, MAX_AU}; use context::LayoutContext; use display_list_builder::{BlockFlowDisplayListBuilding, BorderPaintingMode}; use display_list_builder::{FragmentDisplayListBuilding}; @@ -60,7 +61,7 @@ use style::computed_values::{position, text_align, transform, transform_style}; use style::properties::ComputedValues; use style::values::computed::{LengthOrNone, LengthOrPercentageOrNone}; use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto}; -use util::geometry::{Au, MAX_AU, MAX_RECT}; +use util::geometry::MAX_RECT; use util::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize, WritingMode}; use util::opts; use wrapper::PseudoElementType; diff --git a/components/layout/context.rs b/components/layout/context.rs index 78eb676e02f..e68ce343940 100644 --- a/components/layout/context.rs +++ b/components/layout/context.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use canvas_traits::CanvasMsg; use css::matching::{ApplicableDeclarationsCache, StyleSharingCandidateCache}; use euclid::{Rect, Size2D}; @@ -28,7 +29,6 @@ use std::sync::Arc; use std::sync::mpsc::{Sender, channel}; use style::selector_matching::Stylist; use url::Url; -use util::geometry::Au; use util::mem::HeapSizeOf; use util::opts; diff --git a/components/layout/display_list_builder.rs b/components/layout/display_list_builder.rs index 9d4e7a5f979..94b911cae00 100644 --- a/components/layout/display_list_builder.rs +++ b/components/layout/display_list_builder.rs @@ -10,6 +10,7 @@ #![deny(unsafe_code)] +use app_units::{Au, AU_PER_PX}; use azure::azure_hl::Color; use block::BlockFlow; use canvas_traits::{CanvasMsg, FromLayoutMsg}; @@ -56,7 +57,7 @@ use style::values::specified::{AngleOrCorner, HorizontalDirection, VerticalDirec use table_cell::CollapsedBordersForCell; use url::Url; use util::cursor::Cursor; -use util::geometry::{AU_PER_PX, Au, ZERO_POINT}; +use util::geometry::ZERO_POINT; use util::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize, WritingMode}; use util::opts; use util::range::Range; diff --git a/components/layout/flex.rs b/components/layout/flex.rs index a5b55e37833..fdccc9b1728 100644 --- a/components/layout/flex.rs +++ b/components/layout/flex.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::BlockFlow; use context::LayoutContext; use display_list_builder::FlexFlowDisplayListBuilding; @@ -30,7 +31,6 @@ use style::computed_values::{flex_direction, float}; use style::properties::ComputedValues; use style::properties::style_structs; use style::values::computed::LengthOrPercentageOrAuto; -use util::geometry::Au; use util::logical_geometry::LogicalSize; use util::opts; diff --git a/components/layout/floats.rs b/components/layout/floats.rs index be1947958c3..f406cd2194b 100644 --- a/components/layout/floats.rs +++ b/components/layout/floats.rs @@ -2,11 +2,11 @@ * 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 std::cmp::{max, min}; use std::fmt; use std::i32; use style::computed_values::float; -use util::geometry::Au; use util::logical_geometry::WritingMode; use util::logical_geometry::{LogicalRect, LogicalSize}; use util::persistent_list::PersistentList; diff --git a/components/layout/flow.rs b/components/layout/flow.rs index 8a7d601646b..dfca7f35053 100644 --- a/components/layout/flow.rs +++ b/components/layout/flow.rs @@ -25,6 +25,7 @@ //! line breaks and mapping to CSS boxes, for the purpose of handling `getClientRects()` and //! similar methods. +use app_units::Au; use block::BlockFlow; use context::LayoutContext; use display_list_builder::DisplayListBuildingResult; @@ -57,7 +58,7 @@ use table_colgroup::TableColGroupFlow; use table_row::TableRowFlow; use table_rowgroup::TableRowGroupFlow; use table_wrapper::TableWrapperFlow; -use util::geometry::{Au, ZERO_RECT}; +use util::geometry::ZERO_RECT; use util::logical_geometry::{LogicalRect, LogicalSize, WritingMode}; use wrapper::{PseudoElementType, ThreadSafeLayoutNode}; diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index 00f264fb83c..13cf107227e 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use canvas_traits::CanvasMsg; use context::LayoutContext; use euclid::{Point2D, Rect, Size2D}; @@ -43,7 +44,7 @@ use text; use text::TextRunScanner; use url::Url; use util; -use util::geometry::{Au, ZERO_POINT}; +use util::geometry::ZERO_POINT; use util::logical_geometry::{LogicalMargin, LogicalRect, LogicalSize, WritingMode}; use util::range::*; use util::str::{is_whitespace, slice_chars}; diff --git a/components/layout/inline.rs b/components/layout/inline.rs index e2b028a6880..1f86ac09405 100644 --- a/components/layout/inline.rs +++ b/components/layout/inline.rs @@ -4,6 +4,7 @@ #![deny(unsafe_code)] +use app_units::{Au, MAX_AU}; use block::{AbsoluteAssignBSizesTraversal, AbsoluteStoreOverflowTraversal}; use context::LayoutContext; use display_list_builder::{FragmentDisplayListBuilding, InlineFlowDisplayListBuilding}; @@ -29,7 +30,7 @@ use style::properties::ComputedValues; use text; use unicode_bidi; use util; -use util::geometry::{Au, MAX_AU, ZERO_RECT}; +use util::geometry::ZERO_RECT; use util::logical_geometry::{LogicalRect, LogicalSize, WritingMode}; use util::range::{Range, RangeIndex}; use wrapper::PseudoElementType; diff --git a/components/layout/layout_task.rs b/components/layout/layout_task.rs index 3158753d5c1..3535b8137df 100644 --- a/components/layout/layout_task.rs +++ b/components/layout/layout_task.rs @@ -8,6 +8,7 @@ #![allow(unsafe_code)] use animation; +use app_units::Au; use azure::azure::AzColor; use canvas_traits::CanvasMsg; use construct::ConstructionResult; @@ -78,8 +79,9 @@ use style::properties::longhands::{display, position}; use style::properties::style_structs; use style::selector_matching::Stylist; use style::stylesheets::{CSSRuleIteratorExt, Origin, Stylesheet}; +use style::values::AuExtensionMethods; use url::Url; -use util::geometry::{Au, MAX_RECT, ZERO_POINT}; +use util::geometry::{MAX_RECT, ZERO_POINT}; use util::ipc::OptionalIpcSender; use util::logical_geometry::LogicalPoint; use util::mem::HeapSizeOf; diff --git a/components/layout/lib.rs b/components/layout/lib.rs index 20a1b3323d4..f12335d9c04 100644 --- a/components/layout/lib.rs +++ b/components/layout/lib.rs @@ -18,6 +18,7 @@ #![plugin(string_cache_plugin)] #![plugin(plugins)] +extern crate app_units; #[macro_use] extern crate bitflags; #[macro_use] diff --git a/components/layout/list_item.rs b/components/layout/list_item.rs index 630453e0b09..41c404f6ad9 100644 --- a/components/layout/list_item.rs +++ b/components/layout/list_item.rs @@ -7,6 +7,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::BlockFlow; use context::LayoutContext; use display_list_builder::ListItemFlowDisplayListBuilding; @@ -22,7 +23,6 @@ use std::sync::Arc; use style::computed_values::{list_style_type, position}; use style::properties::ComputedValues; use text; -use util::geometry::Au; use util::logical_geometry::LogicalSize; use util::opts; diff --git a/components/layout/model.rs b/components/layout/model.rs index c7c690f84bf..8ee98a39678 100644 --- a/components/layout/model.rs +++ b/components/layout/model.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use euclid::{Matrix4, SideOffsets2D, Size2D}; use fragment::Fragment; use std::cmp::{max, min}; @@ -14,7 +15,6 @@ use style::computed_values::transform::ComputedMatrix; use style::properties::ComputedValues; use style::values::computed::{BorderRadiusSize, LengthOrPercentageOrAuto}; use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrNone}; -use util::geometry::Au; use util::logical_geometry::LogicalMargin; /// A collapsible margin. See CSS 2.1 ยง 8.3.1. diff --git a/components/layout/multicol.rs b/components/layout/multicol.rs index 6f761152df5..7f21ee58f60 100644 --- a/components/layout/multicol.rs +++ b/components/layout/multicol.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::BlockFlow; use context::LayoutContext; use euclid::{Point2D, Rect}; @@ -15,7 +16,6 @@ use fragment::{Fragment, FragmentBorderBoxIterator}; use std::fmt; use std::sync::Arc; use style::properties::ComputedValues; -use util::geometry::Au; use util::logical_geometry::LogicalSize; pub struct MulticolFlow { diff --git a/components/layout/query.rs b/components/layout/query.rs index 1f1eb3e6a3b..024d3022288 100644 --- a/components/layout/query.rs +++ b/components/layout/query.rs @@ -4,6 +4,7 @@ //! Utilities for querying the layout, as needed by the layout task. +use app_units::Au; use euclid::point::Point2D; use euclid::rect::Rect; use flow_ref::FlowRef; @@ -19,7 +20,6 @@ use script::layout_interface::{ResolvedStyleResponse, ScriptLayoutChan, TrustedN use sequential; use std::sync::{Arc, Mutex}; use util::cursor::Cursor; -use util::geometry::Au; use util::logical_geometry::WritingMode; pub struct LayoutRPCImpl(pub Arc>); diff --git a/components/layout/sequential.rs b/components/layout/sequential.rs index a7c874971dd..cc6abb1184d 100644 --- a/components/layout/sequential.rs +++ b/components/layout/sequential.rs @@ -4,6 +4,7 @@ //! Implements sequential traversals over the DOM and flow trees. +use app_units::Au; use context::{LayoutContext, SharedLayoutContext}; use euclid::point::Point2D; use flow::{PostorderFlowTraversal, PreorderFlowTraversal}; @@ -16,7 +17,7 @@ use traversal::{AssignBSizesAndStoreOverflow, AssignISizes}; use traversal::{BubbleISizes, ConstructFlows, RecalcStyleForNode}; use traversal::{BuildDisplayList, ComputeAbsolutePositions}; use traversal::{PostorderDomTraversal, PreorderDomTraversal}; -use util::geometry::{Au, ZERO_POINT}; +use util::geometry::ZERO_POINT; use util::opts; use wrapper::LayoutNode; diff --git a/components/layout/table.rs b/components/layout/table.rs index eabe20f335f..496a05f80d0 100644 --- a/components/layout/table.rs +++ b/components/layout/table.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::{ISizeConstraintInput, ISizeConstraintSolution}; use block::{self, BlockFlow, CandidateBSizeIterator, ISizeAndMarginsComputer}; use context::LayoutContext; @@ -28,7 +29,6 @@ use style::values::computed::LengthOrPercentageOrAuto; use table_row::{TableRowFlow}; use table_row::{self, CellIntrinsicInlineSize, CollapsedBorder, CollapsedBorderProvenance}; use table_wrapper::TableLayout; -use util::geometry::Au; use util::logical_geometry::LogicalSize; /// A table flow corresponded to the table's internal table fragment under a table wrapper flow. diff --git a/components/layout/table_caption.rs b/components/layout/table_caption.rs index 4e9157bc61e..54c96ac34fa 100644 --- a/components/layout/table_caption.rs +++ b/components/layout/table_caption.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::BlockFlow; use context::LayoutContext; use euclid::{Point2D, Rect}; @@ -14,7 +15,6 @@ use fragment::{Fragment, FragmentBorderBoxIterator}; use std::fmt; use std::sync::Arc; use style::properties::ComputedValues; -use util::geometry::Au; use util::logical_geometry::LogicalSize; /// A table formatting context. diff --git a/components/layout/table_cell.rs b/components/layout/table_cell.rs index 1cdd22bada1..963a7981562 100644 --- a/components/layout/table_cell.rs +++ b/components/layout/table_cell.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::{BlockFlow, ISizeAndMarginsComputer, MarginsMayCollapseFlag}; use context::LayoutContext; use cssparser::Color; @@ -23,7 +24,6 @@ use style::legacy::UnsignedIntegerAttribute; use style::properties::ComputedValues; use table::InternalTable; use table_row::{CollapsedBorder, CollapsedBorderProvenance}; -use util::geometry::Au; use util::logical_geometry::{LogicalMargin, LogicalRect, LogicalSize, WritingMode}; use wrapper::ThreadSafeLayoutNode; diff --git a/components/layout/table_colgroup.rs b/components/layout/table_colgroup.rs index 4cbb41e4c39..12273d0e173 100644 --- a/components/layout/table_colgroup.rs +++ b/components/layout/table_colgroup.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use context::LayoutContext; use euclid::{Point2D, Rect}; use flow::{BaseFlow, Flow, FlowClass, ForceNonfloatedFlag, OpaqueFlow}; @@ -16,7 +17,7 @@ use std::fmt; use std::sync::Arc; use style::properties::ComputedValues; use style::values::computed::LengthOrPercentageOrAuto; -use util::geometry::{Au, ZERO_RECT}; +use util::geometry::ZERO_RECT; use util::logical_geometry::LogicalSize; /// A table formatting context. diff --git a/components/layout/table_row.rs b/components/layout/table_row.rs index 54b47030eb6..24dde406441 100644 --- a/components/layout/table_row.rs +++ b/components/layout/table_row.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::{BlockFlow, ISizeAndMarginsComputer}; use context::LayoutContext; use cssparser::{Color, RGBA}; @@ -27,7 +28,6 @@ use style::properties::ComputedValues; use style::values::computed::LengthOrPercentageOrAuto; use table::{ColumnComputedInlineSize, ColumnIntrinsicInlineSize, InternalTable, VecExt}; use table_cell::{CollapsedBordersForCell, TableCellFlow}; -use util::geometry::Au; use util::logical_geometry::{LogicalSize, PhysicalSide, WritingMode}; /// A single row of a table. diff --git a/components/layout/table_rowgroup.rs b/components/layout/table_rowgroup.rs index 812041cef4a..3d3ad6a10b0 100644 --- a/components/layout/table_rowgroup.rs +++ b/components/layout/table_rowgroup.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::{BlockFlow, ISizeAndMarginsComputer}; use context::LayoutContext; use euclid::{Point2D, Rect}; @@ -20,7 +21,6 @@ use style::computed_values::{border_collapse, border_spacing}; use style::properties::ComputedValues; use table::{ColumnComputedInlineSize, ColumnIntrinsicInlineSize, InternalTable, TableLikeFlow}; use table_row::{self, CollapsedBordersForRow}; -use util::geometry::Au; use util::logical_geometry::{LogicalSize, WritingMode}; /// A table formatting context. diff --git a/components/layout/table_wrapper.rs b/components/layout/table_wrapper.rs index 81b92d5794f..affe0d26b3d 100644 --- a/components/layout/table_wrapper.rs +++ b/components/layout/table_wrapper.rs @@ -13,6 +13,7 @@ #![deny(unsafe_code)] +use app_units::Au; use block::{AbsoluteNonReplaced, BlockFlow, FloatNonReplaced, ISizeAndMarginsComputer, ISizeConstraintInput}; use block::{ISizeConstraintSolution, MarginsMayCollapseFlag}; use context::LayoutContext; @@ -32,7 +33,6 @@ use style::values::CSSFloat; use style::values::computed::LengthOrPercentageOrAuto; use table::{ColumnComputedInlineSize, ColumnIntrinsicInlineSize}; use table_row; -use util::geometry::Au; use util::logical_geometry::LogicalSize; #[derive(Copy, Clone, RustcEncodable, Debug)] diff --git a/components/layout/text.rs b/components/layout/text.rs index fbcd1dc7e7e..6efc5302a61 100644 --- a/components/layout/text.rs +++ b/components/layout/text.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use app_units::Au; use fragment::{Fragment, ScannedTextFragmentInfo, SpecificFragmentInfo, UnscannedTextFragmentInfo}; use gfx::font::{DISABLE_KERNING_SHAPING_FLAG, FontMetrics, IGNORE_LIGATURES_SHAPING_FLAG}; use gfx::font::{RTL_FLAG, RunMetrics, ShapingFlags, ShapingOptions}; @@ -24,7 +25,6 @@ use style::properties::ComputedValues; use style::properties::style_structs::Font as FontStyle; use unicode_bidi::{is_rtl, process_text}; use unicode_script::{get_script, Script}; -use util::geometry::Au; use util::linked_list::split_off_head; use util::logical_geometry::{LogicalSize, WritingMode}; use util::range::{Range, RangeIndex}; diff --git a/components/msg/Cargo.toml b/components/msg/Cargo.toml index a1f6f80c1fa..82f845a7c15 100644 --- a/components/msg/Cargo.toml +++ b/components/msg/Cargo.toml @@ -7,6 +7,9 @@ authors = ["The Servo Project Developers"] name = "msg" path = "lib.rs" +[dependencies.app_units] +path = "../app_units" + [dependencies.util] path = "../util" diff --git a/components/msg/compositor_msg.rs b/components/msg/compositor_msg.rs index 7290a4b9e09..1231a112112 100644 --- a/components/msg/compositor_msg.rs +++ b/components/msg/compositor_msg.rs @@ -2,6 +2,7 @@ * 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 azure::azure_hl::Color; use constellation_msg::{Key, KeyModifiers, KeyState, PipelineId, SubpageId}; use euclid::{Matrix4, Point2D, Rect, Size2D}; @@ -9,7 +10,6 @@ use ipc_channel::ipc::IpcSender; use layers::layers::{BufferRequest, LayerBufferSet}; use layers::platform::surface::NativeDisplay; use std::fmt::{self, Debug, Formatter}; -use util::geometry::Au; /// A newtype struct for denoting the age of messages; prevents race conditions. #[derive(PartialEq, Eq, Debug, Copy, Clone, PartialOrd, Ord, Deserialize, Serialize)] diff --git a/components/msg/lib.rs b/components/msg/lib.rs index 995b1bc5587..f1d6902a39b 100644 --- a/components/msg/lib.rs +++ b/components/msg/lib.rs @@ -5,6 +5,7 @@ #![feature(custom_derive, plugin)] #![plugin(serde_macros, plugins)] +extern crate app_units; #[macro_use] extern crate bitflags; extern crate azure; diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index c7463e73a53..b34aa09b258 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -12,6 +12,9 @@ path = "lib.rs" [features] debugmozjs = ['js/debugmozjs'] +[dependencies.app_units] +path = "../app_units" + [dependencies.plugins] path = "../plugins" diff --git a/components/script/dom/domrect.rs b/components/script/dom/domrect.rs index b2b701e8fc1..6706642b5c5 100644 --- a/components/script/dom/domrect.rs +++ b/components/script/dom/domrect.rs @@ -2,6 +2,7 @@ * 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 dom::bindings::codegen::Bindings::DOMRectBinding; use dom::bindings::codegen::Bindings::DOMRectBinding::DOMRectMethods; use dom::bindings::global::GlobalRef; @@ -9,7 +10,6 @@ use dom::bindings::js::Root; use dom::bindings::num::Finite; use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::window::Window; -use util::geometry::Au; #[dom_struct] pub struct DOMRect { diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index c275ebfd5df..f99bd564dbe 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -4,6 +4,7 @@ //! Element nodes. +use app_units::Au; use cssparser::Color; use devtools_traits::AttrInfo; use dom::activation::Activatable; @@ -75,7 +76,6 @@ use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, parse_sty use style::values::CSSFloat; use style::values::specified::{self, CSSColor, CSSRGBA}; use url::UrlParser; -use util::geometry::Au; use util::str::{DOMString, LengthOrPercentageOrAuto}; #[dom_struct] diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index d679fb12da6..7f131baf52f 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -4,6 +4,7 @@ //! The core DOM types. Defines the basic DOM hierarchy as well as all the HTML elements. +use app_units::Au; use core::nonzero::NonZero; use devtools_traits::NodeInfo; use document_loader::DocumentLoader; @@ -64,7 +65,6 @@ use std::slice::ref_slice; use std::sync::Arc; use string_cache::{Atom, Namespace, QualName}; use style::properties::ComputedValues; -use util::geometry::Au; use util::str::DOMString; use util::task_state; use uuid; diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 4cec46c7814..21b26a529b2 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -2,6 +2,7 @@ * 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 devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType}; use dom::bindings::callback::ExceptionHandling; use dom::bindings::cell::DOMRefCell; @@ -72,7 +73,7 @@ use string_cache::Atom; use time; use timers::{IsInterval, TimerCallback, TimerId, TimerManager}; use url::Url; -use util::geometry::{self, Au, MAX_RECT}; +use util::geometry::{self, MAX_RECT}; use util::str::{DOMString, HTML_SPACE_CHARACTERS}; use util::{breakpoint, opts}; use webdriver_handlers::jsval_to_webdriver; diff --git a/components/script/layout_interface.rs b/components/script/layout_interface.rs index 702cce2d0b0..230ebe79692 100644 --- a/components/script/layout_interface.rs +++ b/components/script/layout_interface.rs @@ -6,6 +6,7 @@ //! interface helps reduce coupling between these two components, and enables //! the DOM to be placed in a separate crate from layout. +use app_units::Au; use dom::node::LayoutData; use euclid::point::Point2D; use euclid::rect::Rect; @@ -28,7 +29,6 @@ use style::animation::PropertyAnimation; use style::media_queries::MediaQueryList; use style::stylesheets::Stylesheet; use url::Url; -use util::geometry::Au; pub use dom::node::TrustedNodeAddress; /// Asynchronous messages that script can send to layout. diff --git a/components/script/lib.rs b/components/script/lib.rs index 6ebf3d12df6..2fcc772e8cb 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -33,6 +33,7 @@ #![plugin(string_cache_plugin)] #![plugin(plugins)] +extern crate app_units; #[macro_use] extern crate bitflags; #[macro_use] diff --git a/components/script_traits/Cargo.toml b/components/script_traits/Cargo.toml index d7547cc5c59..6c8d79567b0 100644 --- a/components/script_traits/Cargo.toml +++ b/components/script_traits/Cargo.toml @@ -7,6 +7,9 @@ authors = ["The Servo Project Developers"] name = "script_traits" path = "lib.rs" +[dependencies.app_units] +path = "../app_units" + [dependencies.msg] path = "../msg" diff --git a/components/script_traits/lib.rs b/components/script_traits/lib.rs index ba0f25d42e0..274c3eb4924 100644 --- a/components/script_traits/lib.rs +++ b/components/script_traits/lib.rs @@ -10,6 +10,7 @@ #![plugin(plugins, serde_macros)] #![deny(missing_docs)] +extern crate app_units; extern crate devtools_traits; extern crate euclid; extern crate ipc_channel; @@ -21,6 +22,7 @@ extern crate serde; extern crate url; extern crate util; +use app_units::Au; use devtools_traits::ScriptToDevtoolsControlMsg; use euclid::point::Point2D; use euclid::rect::Rect; @@ -38,7 +40,6 @@ use profile_traits::{mem, time}; use std::any::Any; use std::sync::mpsc::{Receiver, Sender}; use url::Url; -use util::geometry::Au; /// The address of a node. Layout sends these back. They must be validated via /// `from_untrusted_node_address` before they can be used, because we do not trust layout. diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 08672e8af2b..020b9b9bfe8 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -71,6 +71,16 @@ dependencies = [ "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "app_units" +version = "0.1.0" +dependencies = [ + "euclid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_macros 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "aster" version = "0.4.7" @@ -223,6 +233,7 @@ dependencies = [ name = "compositing" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "azure 0.1.0 (git+https://github.com/servo/rust-azure)", "canvas 0.0.1", "canvas_traits 0.0.1", @@ -580,6 +591,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "gfx" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "azure 0.1.0 (git+https://github.com/servo/rust-azure)", "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "canvas_traits 0.0.1", @@ -930,6 +942,7 @@ dependencies = [ name = "layout" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "azure 0.1.0 (git+https://github.com/servo/rust-azure)", "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "canvas 0.0.1", @@ -1084,6 +1097,7 @@ dependencies = [ name = "msg" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "azure 0.1.0 (git+https://github.com/servo/rust-azure)", "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "canvas_traits 0.0.1", @@ -1439,6 +1453,7 @@ name = "script" version = "0.0.1" dependencies = [ "angle 0.1.0 (git+https://github.com/ecoal95/angle?branch=servo)", + "app_units 0.1.0", "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "canvas 0.0.1", "canvas_traits 0.0.1", @@ -1489,6 +1504,7 @@ dependencies = [ name = "script_traits" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "devtools_traits 0.0.1", "euclid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.1.0 (git+https://github.com/pcwalton/ipc-channel)", @@ -1651,6 +1667,7 @@ dependencies = [ name = "style" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cssparser 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "encoding 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1677,6 +1694,7 @@ dependencies = [ name = "style_tests" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "cssparser 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", @@ -1820,6 +1838,7 @@ dependencies = [ name = "util" version = "0.0.1" dependencies = [ + "app_units 0.1.0", "azure 0.1.0 (git+https://github.com/servo/rust-azure)", "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "cssparser 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 5445a329004..bbc24072aef 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -30,6 +30,9 @@ features = [ "serde-serialization" ] version = "0.2" features = [ "serde_serialization" ] +[dependencies.app_units] +path = "../app_units" + [dependencies] log = "0.3" encoding = "0.2" diff --git a/components/style/animation.rs b/components/style/animation.rs index 01ab377bfe6..30c0b658fec 100644 --- a/components/style/animation.rs +++ b/components/style/animation.rs @@ -2,6 +2,7 @@ * 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::{Color, RGBA}; use euclid::point::Point2D; use properties::ComputedValues; @@ -25,7 +26,6 @@ use properties::longhands::z_index::computed_value::T as ZIndex; use std::cmp::Ordering; use std::iter::repeat; use util::bezier::Bezier; -use util::geometry::Au; use values::CSSFloat; use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone}; use values::computed::{Calc, Length, LengthOrPercentage, Time}; diff --git a/components/style/lib.rs b/components/style/lib.rs index d97ed9edb21..ed898ff2f60 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -16,6 +16,7 @@ #![plugin(serde_macros)] #![plugin(plugins)] +extern crate app_units; #[macro_use] extern crate bitflags; #[macro_use] diff --git a/components/style/media_queries.rs b/components/style/media_queries.rs index f42d6970133..76516747bd1 100644 --- a/components/style/media_queries.rs +++ b/components/style/media_queries.rs @@ -2,11 +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 app_units::Au; use cssparser::{Delimiter, Parser, Token}; use euclid::size::{Size2D, TypedSize2D}; use properties::longhands; use std::ascii::AsciiExt; -use util::geometry::{Au, ViewportPx}; +use util::geometry::ViewportPx; use values::specified; diff --git a/components/style/properties.mako.rs b/components/style/properties.mako.rs index df4f7380526..9bf6da3b891 100644 --- a/components/style/properties.mako.rs +++ b/components/style/properties.mako.rs @@ -14,10 +14,10 @@ use std::intrinsics; use std::mem; use std::sync::Arc; +use app_units::Au; use cssparser::{Parser, Color, RGBA, AtRuleParser, DeclarationParser, DeclarationListParser, parse_important, ToCss, TokenSerializationType}; use url::Url; -use util::geometry::Au; use util::logical_geometry::{LogicalMargin, PhysicalSide, WritingMode}; use euclid::SideOffsets2D; use euclid::size::Size2D; @@ -28,6 +28,7 @@ use computed_values; use parser::{ParserContext, log_css_error}; use selectors::matching::DeclarationBlock; use stylesheets::Origin; +use values::AuExtensionMethods; use values::computed::{self, ToComputedValue}; use values::specified::{Length, BorderStyle}; @@ -274,7 +275,7 @@ pub mod longhands { <%def name="predefined_type(name, type, initial_value, parse_method='parse')"> <%self:longhand name="${name}"> #[allow(unused_imports)] - use util::geometry::Au; + use app_units::Au; pub type SpecifiedValue = specified::${type}; pub mod computed_value { pub use values::computed::${type} as T; @@ -317,9 +318,9 @@ pub mod longhands { % for side in ["top", "right", "bottom", "left"]: <%self:longhand name="border-${side}-width"> + use app_units::Au; use cssparser::ToCss; use std::fmt; - use util::geometry::Au; use values::computed::Context; impl ToCss for SpecifiedValue { @@ -336,7 +337,7 @@ pub mod longhands { #[derive(Clone, PartialEq)] pub struct SpecifiedValue(pub specified::Length); pub mod computed_value { - use util::geometry::Au; + use app_units::Au; pub type T = Au; } #[inline] pub fn get_initial_value() -> computed_value::T { @@ -385,9 +386,10 @@ pub mod longhands { <%self:longhand name="outline-width"> + use app_units::Au; use cssparser::ToCss; use std::fmt; - use util::geometry::Au; + use values::AuExtensionMethods; use values::computed::Context; impl ToCss for SpecifiedValue { @@ -402,7 +404,7 @@ pub mod longhands { #[derive(Clone, PartialEq)] pub struct SpecifiedValue(pub specified::Length); pub mod computed_value { - use util::geometry::Au; + use app_units::Au; pub type T = Au; } pub use super::border_top_width::get_initial_value; @@ -649,6 +651,7 @@ pub mod longhands { <%self:longhand name="line-height"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::CSSFloat; use values::computed::Context; @@ -692,8 +695,8 @@ pub mod longhands { } } pub mod computed_value { + use app_units::Au; use std::fmt; - use util::geometry::Au; use values::CSSFloat; #[derive(PartialEq, Copy, Clone, HeapSizeOf)] pub enum T { @@ -791,8 +794,9 @@ pub mod longhands { }) } pub mod computed_value { + use app_units::Au; use std::fmt; - use util::geometry::Au; + use values::AuExtensionMethods; use values::{CSSFloat, computed}; #[allow(non_camel_case_types)] #[derive(PartialEq, Copy, Clone, HeapSizeOf)] @@ -1397,6 +1401,7 @@ pub mod longhands { <%self:longhand name="background-position"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::computed::Context; pub mod computed_value { @@ -1911,9 +1916,9 @@ pub mod longhands { <%self:longhand name="font-size"> + use app_units::Au; use cssparser::ToCss; use std::fmt; - use util::geometry::Au; use values::computed::Context; impl ToCss for SpecifiedValue { @@ -1925,7 +1930,7 @@ pub mod longhands { #[derive(Clone, PartialEq)] pub struct SpecifiedValue(pub specified::Length); // Percentages are the same as em. pub mod computed_value { - use util::geometry::Au; + use app_units::Au; pub type T = Au; } const MEDIUM_PX: i32 = 16; @@ -2034,6 +2039,7 @@ pub mod longhands { <%self:longhand name="letter-spacing"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::computed::Context; #[derive(Clone, Copy, PartialEq)] @@ -2052,7 +2058,7 @@ pub mod longhands { } pub mod computed_value { - use util::geometry::Au; + use app_units::Au; #[derive(Clone, PartialEq, HeapSizeOf)] pub struct T(pub Option); } @@ -2096,6 +2102,7 @@ pub mod longhands { <%self:longhand name="word-spacing"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::computed::Context; #[derive(Clone, Copy, PartialEq)] @@ -2114,7 +2121,7 @@ pub mod longhands { } pub mod computed_value { - use util::geometry::Au; + use app_units::Au; #[derive(Clone, PartialEq, HeapSizeOf)] pub struct T(pub Option); } @@ -2372,14 +2379,15 @@ pub mod longhands { ${single_keyword("caption-side", "top bottom")} <%self:longhand name="border-spacing"> + use app_units::Au; + use values::AuExtensionMethods; use values::computed::Context; use cssparser::ToCss; use std::fmt; - use util::geometry::Au; pub mod computed_value { - use util::geometry::Au; + use app_units::Au; #[derive(Clone, Copy, Debug, PartialEq, RustcEncodable, HeapSizeOf)] pub struct T { @@ -2536,6 +2544,7 @@ pub mod longhands { <%self:longhand name="column-width" experimental="True"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::computed::Context; #[derive(Clone, Copy, PartialEq)] @@ -2554,7 +2563,7 @@ pub mod longhands { } pub mod computed_value { - use util::geometry::Au; + use app_units::Au; #[derive(Clone, PartialEq, HeapSizeOf)] pub struct T(pub Option); } @@ -2664,6 +2673,7 @@ pub mod longhands { <%self:longhand name="column-gap" experimental="True"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::computed::Context; #[derive(Clone, Copy, PartialEq)] @@ -2682,7 +2692,7 @@ pub mod longhands { } pub mod computed_value { - use util::geometry::Au; + use app_units::Au; #[derive(Clone, PartialEq, HeapSizeOf)] pub struct T(pub Option); } @@ -2771,6 +2781,7 @@ pub mod longhands { <%self:longhand name="box-shadow"> use cssparser::{self, ToCss}; use std::fmt; + use values::AuExtensionMethods; use values::computed::Context; #[derive(Clone, PartialEq)] @@ -2825,8 +2836,8 @@ pub mod longhands { } pub mod computed_value { + use app_units::Au; use std::fmt; - use util::geometry::Au; use values::computed; #[derive(Clone, PartialEq, HeapSizeOf)] @@ -2927,7 +2938,7 @@ pub mod longhands { } pub fn parse_one_box_shadow(input: &mut Parser) -> Result { - use util::geometry::Au; + use app_units::Au; let mut lengths = [specified::Length::Absolute(Au(0)); 4]; let mut lengths_parsed = false; let mut color = None; @@ -2990,13 +3001,14 @@ pub mod longhands { <%self:longhand name="clip"> use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; // NB: `top` and `left` are 0 if `auto` per CSS 2.1 11.1.2. use values::computed::Context; pub mod computed_value { - use util::geometry::Au; + use app_units::Au; #[derive(Clone, PartialEq, Eq, Copy, Debug, HeapSizeOf)] pub struct ClipRect { @@ -3109,8 +3121,8 @@ pub mod longhands { } pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { + use app_units::Au; use std::ascii::AsciiExt; - use util::geometry::Au; use values::specified::Length; if input.try(|input| input.expect_ident_matching("auto")).is_ok() { @@ -3144,7 +3156,7 @@ pub mod longhands { <%self:longhand name="text-shadow"> use cssparser::{self, ToCss}; use std::fmt; - + use values::AuExtensionMethods; use values::computed::Context; #[derive(Clone, PartialEq)] @@ -3173,8 +3185,8 @@ pub mod longhands { } pub mod computed_value { + use app_units::Au; use cssparser::Color; - use util::geometry::Au; #[derive(Clone, PartialEq, Debug, HeapSizeOf)] pub struct T(pub Vec); @@ -3265,7 +3277,7 @@ pub mod longhands { } fn parse_one_text_shadow(input: &mut Parser) -> Result { - use util::geometry::Au; + use app_units::Au; let mut lengths = [specified::Length::Absolute(Au(0)); 3]; let mut lengths_parsed = false; let mut color = None; @@ -3338,6 +3350,7 @@ pub mod longhands { //pub use self::computed_value::T as SpecifiedValue; use cssparser::ToCss; use std::fmt; + use values::AuExtensionMethods; use values::CSSFloat; use values::specified::{Angle, Length}; @@ -3359,7 +3372,7 @@ pub mod longhands { } pub mod computed_value { - use util::geometry::Au; + use app_units::Au; use values::CSSFloat; use values::specified::{Angle}; @@ -3566,12 +3579,12 @@ pub mod longhands { <%self:longhand name="transform"> + use app_units::Au; use values::CSSFloat; use values::computed::Context; use cssparser::ToCss; use std::fmt; - use util::geometry::Au; pub mod computed_value { use values::CSSFloat; @@ -4109,12 +4122,13 @@ pub mod longhands { ${single_keyword("transform-style", "auto flat preserve-3d")} <%self:longhand name="transform-origin"> + use app_units::Au; + use values::AuExtensionMethods; use values::computed::Context; use values::specified::{Length, LengthOrPercentage, Percentage}; use cssparser::ToCss; use std::fmt; - use util::geometry::Au; pub mod computed_value { use values::computed::{Length, LengthOrPercentage}; @@ -5171,7 +5185,7 @@ pub mod shorthands { 'border-%s-radius' % (corner) for corner in ['top-left', 'top-right', 'bottom-right', 'bottom-left'] )}"> - use util::geometry::Au; + use app_units::Au; use values::specified::{Length, LengthOrPercentage}; use values::specified::BorderRadiusSize; diff --git a/components/style/values.rs b/components/style/values.rs index 4eb5b02b20e..47ce425232b 100644 --- a/components/style/values.rs +++ b/components/style/values.rs @@ -4,6 +4,34 @@ pub use cssparser::RGBA; +use app_units::Au; +use std::fmt; + +// This is a re-implementation of the ToCss trait in cssparser. +// It's done here because the app_units crate shouldn't depend +// on cssparser, and it's not possible to implement a trait when +// both the trait and the type are defined in different crates. +pub trait AuExtensionMethods { + /// Serialize `self` in CSS syntax, writing to `dest`. + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write; + + /// Serialize `self` in CSS syntax and return a string. + /// + /// (This is a convenience wrapper for `to_css` and probably should not be overridden.) + #[inline] + fn to_css_string(&self) -> String { + let mut s = String::new(); + self.to_css(&mut s).unwrap(); + s + } +} + +impl AuExtensionMethods for Au { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + write!(dest, "{}px", self.to_f64_px()) + } +} + macro_rules! define_numbered_css_keyword_enum { ($name: ident: $( $css: expr => $variant: ident = $value: expr ),+,) => { define_numbered_css_keyword_enum!($name: $( $css => $variant = $value ),+); @@ -40,6 +68,7 @@ pub type CSSFloat = f32; pub mod specified { + use app_units::Au; use cssparser::{self, CssStringWriter, Parser, ToCss, Token}; use euclid::size::Size2D; use parser::ParserContext; @@ -49,10 +78,9 @@ pub mod specified { use std::fmt::{self, Write}; use std::ops::Mul; use style_traits::values::specified::AllowedNumericType; + use super::AuExtensionMethods; use super::CSSFloat; use url::Url; - use util::geometry::Au; - #[derive(Clone, PartialEq, Debug, HeapSizeOf)] pub struct CSSColor { @@ -1209,13 +1237,14 @@ pub mod specified { } pub mod computed { + use app_units::Au; use euclid::size::Size2D; use properties::longhands; use std::fmt; + use super::AuExtensionMethods; use super::specified::AngleOrCorner; use super::{CSSFloat, specified}; use url::Url; - use util::geometry::Au; pub use cssparser::Color as CSSColor; pub use super::specified::{Angle, BorderStyle, Time}; diff --git a/components/style/viewport.rs b/components/style/viewport.rs index 5f86804b39b..f5bfd66e06d 100644 --- a/components/style/viewport.rs +++ b/components/style/viewport.rs @@ -2,6 +2,7 @@ * 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::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser, parse_important}; use euclid::scale_factor::ScaleFactor; use euclid::size::{Size2D, TypedSize2D}; @@ -12,7 +13,7 @@ use std::collections::hash_map::{Entry, HashMap}; use std::intrinsics; use style_traits::viewport::{Orientation, UserZoom, ViewportConstraints, Zoom}; use stylesheets::Origin; -use util::geometry::{Au, ViewportPx}; +use util::geometry::ViewportPx; use values::computed::{Context, ToComputedValue}; use values::specified::LengthOrPercentageOrAuto; diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index c1f215b0648..7ddaff1e50b 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -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" diff --git a/components/util/geometry.rs b/components/util/geometry.rs index d14b1808f83..224f301141d 100644 --- a/components/util/geometry.rs +++ b/components/util/geometry.rs @@ -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 = Point2D { x: Au(0), @@ -111,136 +86,6 @@ pub static MAX_RECT: Rect = Rect { } }; -pub const MIN_AU: Au = Au(i32::MIN); -pub const MAX_AU: Au = Au(i32::MAX); - -impl Encodable for Au { - fn encode(&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(&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 for Au { - type Output = Au; - - #[inline] - fn mul(self, other: i32) -> Au { - Au(self.0.wrapping_mul(other)) - } -} - -impl Div for Au { - type Output = Au; - - #[inline] - fn div(self, other: i32) -> Au { - Au(self.0 / other) - } -} - -impl Rem 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. diff --git a/components/util/lib.rs b/components/util/lib.rs index 0fd7cc034de..5cfda0e8c7c 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -20,6 +20,7 @@ #![plugin(plugins, serde_macros)] +extern crate app_units; #[macro_use] extern crate bitflags; #[macro_use] diff --git a/components/util/mem.rs b/components/util/mem.rs index aac020b3334..0243dd671b9 100644 --- a/components/util/mem.rs +++ b/components/util/mem.rs @@ -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; diff --git a/components/util/str.rs b/components/util/str.rs index c3af9a26bbc..78a444be094 100644 --- a/components/util/str.rs +++ b/components/util/str.rs @@ -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; diff --git a/tests/unit/style/Cargo.toml b/tests/unit/style/Cargo.toml index e322e0bffe2..ad9d8d1fc6d 100644 --- a/tests/unit/style/Cargo.toml +++ b/tests/unit/style/Cargo.toml @@ -8,6 +8,9 @@ name = "style_tests" path = "lib.rs" doctest = false +[dependencies.app_units] +path = "../../../components/app_units" + [dependencies.style] path = "../../../components/style" diff --git a/tests/unit/style/lib.rs b/tests/unit/style/lib.rs index 70af5f2f62c..3c5d3a21004 100644 --- a/tests/unit/style/lib.rs +++ b/tests/unit/style/lib.rs @@ -5,6 +5,7 @@ #![feature(plugin)] #![plugin(string_cache_plugin)] +extern crate app_units; extern crate cssparser; extern crate euclid; extern crate selectors; diff --git a/tests/unit/style/media_queries.rs b/tests/unit/style/media_queries.rs index 5db734362c4..2841a7b6cda 100644 --- a/tests/unit/style/media_queries.rs +++ b/tests/unit/style/media_queries.rs @@ -2,13 +2,13 @@ * 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 euclid::size::Size2D; use std::borrow::ToOwned; use style::media_queries::*; use style::stylesheets::{Origin, Stylesheet, CSSRuleIteratorExt}; use style::values::specified; use url::Url; -use util::geometry::Au; fn test_media_rule(css: &str, callback: F) where F: Fn(&MediaQueryList, &str) {