From 32d5e24922e47322bf67c100fa4c178bc9b430f4 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 5 May 2015 17:34:01 +0200 Subject: [PATCH] Replace Au-related free functions in util::geometry with Au methods. --- components/gfx/platform/freetype/font.rs | 11 +++-- components/gfx/platform/macos/font.rs | 4 +- components/gfx/text/text_run.rs | 4 +- components/layout/block.rs | 10 ++--- components/layout/display_list_builder.rs | 14 +++--- components/layout/fragment.rs | 12 +++--- components/layout/inline.rs | 12 +++--- components/layout/table_wrapper.rs | 5 +-- components/script/dom/htmlimageelement.rs | 5 +-- components/script/dom/window.rs | 8 ++-- components/script/script_task.rs | 5 +-- components/util/geometry.rs | 52 ++++------------------- 12 files changed, 50 insertions(+), 92 deletions(-) diff --git a/components/gfx/platform/freetype/font.rs b/components/gfx/platform/freetype/font.rs index 2d4b760294f..00b0e1f6c02 100644 --- a/components/gfx/platform/freetype/font.rs +++ b/components/gfx/platform/freetype/font.rs @@ -7,7 +7,6 @@ extern crate freetype; use font::{FontHandleMethods, FontMetrics, FontTableMethods}; use font::{FontTableTag, FractionalPixel}; use util::geometry::Au; -use util::geometry; use util::str::c_str_to_string; use platform::font_context::FontContextHandle; use text::glyph::GlyphId; @@ -225,9 +224,9 @@ impl FontHandleMethods for FontHandle { let height = self.font_units_to_au(face.height as f64); let leading = height - (ascent + descent); - let mut strikeout_size = geometry::from_pt(0.0); - let mut strikeout_offset = geometry::from_pt(0.0); - let mut x_height = geometry::from_pt(0.0); + let mut strikeout_size = Au(0); + let mut strikeout_offset = Au(0); + let mut x_height = Au(0); unsafe { let os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2) as *mut TT_OS2; let valid = !os2.is_null() && (*os2).version != 0xffff; @@ -258,7 +257,7 @@ impl FontHandleMethods for FontHandle { line_gap: height, }; - debug!("Font metrics (@{} pt): {:?}", geometry::to_pt(em_size), metrics); + debug!("Font metrics (@{}px): {:?}", em_size.to_frac32_px(), metrics); return metrics; } @@ -297,6 +296,6 @@ impl<'a> FontHandle { // If this isn't true then we're scaling one of the axes wrong assert!(metrics.x_ppem == metrics.y_ppem); - return geometry::from_frac_px(value * x_scale); + return Au::from_frac_px(value * x_scale); } } diff --git a/components/gfx/platform/macos/font.rs b/components/gfx/platform/macos/font.rs index 75115d287be..8f74f99dfbe 100644 --- a/components/gfx/platform/macos/font.rs +++ b/components/gfx/platform/macos/font.rs @@ -182,8 +182,8 @@ impl FontHandleMethods for FontHandle { // see also: https://bugs.webkit.org/show_bug.cgi?id=16768 // see also: https://bugreports.qt-project.org/browse/QTBUG-13364 underline_offset: Au::from_pt(self.ctfont.underline_position() as f64), - strikeout_size: geometry::from_pt(0.0), // FIXME(Issue #942) - strikeout_offset: geometry::from_pt(0.0), // FIXME(Issue #942) + strikeout_size: Au(0), // FIXME(Issue #942) + strikeout_offset: Au(0), // FIXME(Issue #942) leading: Au::from_pt(leading), x_height: Au::from_pt(self.ctfont.x_height() as f64), em_size: em_size, diff --git a/components/gfx/text/text_run.rs b/components/gfx/text/text_run.rs index 4e963bf1b28..20c1f8fb4da 100644 --- a/components/gfx/text/text_run.rs +++ b/components/gfx/text/text_run.rs @@ -8,7 +8,7 @@ use platform::font_template::FontTemplateData; use util::geometry::Au; use util::range::Range; use util::vec::{Comparator, FullBinarySearchMethods}; -use std::cmp::Ordering; +use std::cmp::{Ordering, max}; use std::slice::Iter; use std::sync::Arc; use text::glyph::{CharIndex, GlyphStore}; @@ -322,7 +322,7 @@ impl<'a> TextRun { debug!("iterating outer range {:?}", range); self.natural_word_slices_in_range(range).fold(Au(0), |max_piece_width, slice| { debug!("iterated on {:?}[{:?}]", slice.offset, slice.range); - Au::max(max_piece_width, self.advance_for_range(&slice.range)) + max(max_piece_width, self.advance_for_range(&slice.range)) }) } diff --git a/components/layout/block.rs b/components/layout/block.rs index b01a31804b2..718155f495b 100644 --- a/components/layout/block.rs +++ b/components/layout/block.rs @@ -933,7 +933,7 @@ impl BlockFlow { if is_root { let screen_size = LogicalSize::from_physical(self.fragment.style.writing_mode, layout_context.shared.screen_size); - block_size = Au::max(screen_size.block, block_size) + block_size = max(screen_size.block, block_size) } if is_root || self.formatting_context_type() != FormattingContextType::None || @@ -1478,11 +1478,11 @@ impl Flow for BlockFlow { child_base.intrinsic_inline_sizes.minimum_inline_size); if child_base.flags.contains(CLEARS_LEFT) { - left_float_width = Au::max(left_float_width, left_float_width_accumulator); + left_float_width = max(left_float_width, left_float_width_accumulator); left_float_width_accumulator = Au(0) } if child_base.flags.contains(CLEARS_RIGHT) { - right_float_width = Au::max(right_float_width, right_float_width_accumulator); + right_float_width = max(right_float_width, right_float_width_accumulator); right_float_width_accumulator = Au(0) } @@ -1509,8 +1509,8 @@ impl Flow for BlockFlow { // FIXME(pcwalton): This should consider all float descendants, not just children. // FIXME(pcwalton): This is not well-spec'd; INTRINSIC specifies to do this, but CSS-SIZING // says not to. In practice, Gecko and WebKit both do this. - left_float_width = Au::max(left_float_width, left_float_width_accumulator); - right_float_width = Au::max(right_float_width, right_float_width_accumulator); + left_float_width = max(left_float_width, left_float_width_accumulator); + right_float_width = max(right_float_width, right_float_width_accumulator); computation.content_intrinsic_sizes.preferred_inline_size = max(computation.content_intrinsic_sizes.preferred_inline_size, left_float_width + right_float_width); diff --git a/components/layout/display_list_builder.rs b/components/layout/display_list_builder.rs index b82852e24e3..67ee428fd72 100644 --- a/components/layout/display_list_builder.rs +++ b/components/layout/display_list_builder.rs @@ -52,7 +52,7 @@ use style::values::computed::{Image, LinearGradient, LengthOrPercentage, LengthO use style::values::specified::{AngleOrCorner, HorizontalDirection, VerticalDirection}; use url::Url; use util::cursor::Cursor; -use util::geometry::{self, Au, ZERO_POINT, to_px, to_frac_px}; +use util::geometry::{Au, ZERO_POINT}; use util::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize, WritingMode}; use util::opts; @@ -1025,9 +1025,9 @@ impl FragmentDisplayListBuilding for Fragment { } SpecificFragmentInfo::Canvas(ref canvas_fragment_info) => { let width = canvas_fragment_info.replaced_image_fragment_info - .computed_inline_size.map_or(0, |w| to_px(w) as usize); + .computed_inline_size.map_or(0, |w| w.to_px() as usize); let height = canvas_fragment_info.replaced_image_fragment_info - .computed_block_size.map_or(0, |h| to_px(h) as usize); + .computed_block_size.map_or(0, |h| h.to_px() as usize); let (sender, receiver) = channel::>(); let canvas_data = match canvas_fragment_info.renderer { @@ -1117,10 +1117,10 @@ impl FragmentDisplayListBuilding for Fragment { layout_context: &LayoutContext) { let border_padding = (self.border_padding).to_physical(self.style.writing_mode); let content_size = self.content_box().size.to_physical(self.style.writing_mode); - let iframe_rect = Rect(Point2D(geometry::to_frac_px(offset.x + border_padding.left) as f32, - geometry::to_frac_px(offset.y + border_padding.top) as f32), - Size2D(geometry::to_frac_px(content_size.width) as f32, - geometry::to_frac_px(content_size.height) as f32)); + let iframe_rect = Rect(Point2D((offset.x + border_padding.left).to_frac32_px(), + (offset.y + border_padding.top).to_frac32_px()), + Size2D(content_size.width.to_frac32_px(), + content_size.height.to_frac32_px())); debug!("finalizing position and size of iframe for {:?},{:?}", iframe_fragment.pipeline_id, diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index ea090ea55ea..9063bc652d2 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -47,7 +47,7 @@ use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto}; use style::values::computed::{LengthOrPercentageOrNone}; use text::TextRunScanner; use url::Url; -use util::geometry::{self, Au, ZERO_POINT}; +use util::geometry::{Au, ZERO_POINT}; use util::logical_geometry::{LogicalRect, LogicalSize, LogicalMargin, WritingMode}; use util::range::*; use util::str::is_whitespace; @@ -213,9 +213,9 @@ fn clamp_size(size: Au, let min_size = model::specified(min_size, container_inline_size); let max_size = model::specified_or_none(max_size, container_inline_size); - Au::max(min_size, match max_size { + max(min_size, match max_size { None => size, - Some(max_size) => Au::min(size, max_size), + Some(max_size) => min(size, max_size), }) } @@ -355,7 +355,7 @@ impl ImageFragmentInfo { pub fn tile_image(position: &mut Au, size: &mut Au, virtual_position: Au, image_size: u32) { let image_size = image_size as isize; - let delta_pixels = geometry::to_px(virtual_position - *position); + let delta_pixels = (virtual_position - *position).to_px(); let tile_count = (delta_pixels + image_size - 1) / image_size; let offset = Au::from_px(image_size * tile_count); let new_position = virtual_position - offset; @@ -967,8 +967,8 @@ impl Fragment { let flags = self.quantities_included_in_intrinsic_inline_size(); let style = self.style(); let specified = if flags.contains(INTRINSIC_INLINE_SIZE_INCLUDES_SPECIFIED) { - Au::max(model::specified(style.min_inline_size(), Au(0)), - MaybeAuto::from_style(style.content_inline_size(), Au(0)).specified_or_zero()) + max(model::specified(style.min_inline_size(), Au(0)), + MaybeAuto::from_style(style.content_inline_size(), Au(0)).specified_or_zero()) } else { Au(0) }; diff --git a/components/layout/inline.rs b/components/layout/inline.rs index 82a236ac001..17741e4eadd 100644 --- a/components/layout/inline.rs +++ b/components/layout/inline.rs @@ -397,8 +397,8 @@ impl LineBreaker { fn new_block_size_for_line(&self, new_fragment: &Fragment, layout_context: &LayoutContext) -> Au { - Au::max(self.pending_line.bounds.size.block, - self.new_inline_metrics_for_line(new_fragment, layout_context).block_size()) + max(self.pending_line.bounds.size.block, + self.new_inline_metrics_for_line(new_fragment, layout_context).block_size()) } /// Computes the position of a line that has only the provided fragment. Returns the bounding @@ -1589,10 +1589,10 @@ impl InlineMetrics { pub fn max(&self, other: &InlineMetrics) -> InlineMetrics { InlineMetrics { - block_size_above_baseline: Au::max(self.block_size_above_baseline, - other.block_size_above_baseline), - depth_below_baseline: Au::max(self.depth_below_baseline, other.depth_below_baseline), - ascent: Au::max(self.ascent, other.ascent), + block_size_above_baseline: max(self.block_size_above_baseline, + other.block_size_above_baseline), + depth_below_baseline: max(self.depth_below_baseline, other.depth_below_baseline), + ascent: max(self.ascent, other.ascent), } } } diff --git a/components/layout/table_wrapper.rs b/components/layout/table_wrapper.rs index 73e631e2e25..e7e2fd3e37f 100644 --- a/components/layout/table_wrapper.rs +++ b/components/layout/table_wrapper.rs @@ -695,11 +695,10 @@ fn initial_computed_inline_size(block: &mut BlockFlow, containing_block_inline_size); match inline_size_from_style { MaybeAuto::Auto => { - MaybeAuto::Specified(Au::max(containing_block_inline_size, - minimum_width_of_all_columns)) + MaybeAuto::Specified(max(containing_block_inline_size, minimum_width_of_all_columns)) } MaybeAuto::Specified(inline_size_from_style) => { - MaybeAuto::Specified(Au::max(inline_size_from_style, minimum_width_of_all_columns)) + MaybeAuto::Specified(max(inline_size_from_style, minimum_width_of_all_columns)) } } } diff --git a/components/script/dom/htmlimageelement.rs b/components/script/dom/htmlimageelement.rs index 59c100465c0..760f5942aa0 100644 --- a/components/script/dom/htmlimageelement.rs +++ b/components/script/dom/htmlimageelement.rs @@ -21,7 +21,6 @@ use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; use dom::node::{document_from_node, Node, NodeTypeId, NodeHelpers, NodeDamage, window_from_node}; use dom::virtualmethods::VirtualMethods; use dom::window::WindowHelpers; -use util::geometry::to_px; use util::str::DOMString; use string_cache::Atom; @@ -188,7 +187,7 @@ impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> { fn Width(self) -> u32 { let node: JSRef = NodeCast::from_ref(self); let rect = node.get_bounding_content_box(); - to_px(rect.size.width) as u32 + rect.size.width.to_px() as u32 } fn SetWidth(self, width: u32) { @@ -199,7 +198,7 @@ impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> { fn Height(self) -> u32 { let node: JSRef = NodeCast::from_ref(self); let rect = node.get_bounding_content_box(); - to_px(rect.size.height) as u32 + rect.size.height.to_px() as u32 } fn SetHeight(self, height: u32) { diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 37d79625334..e49f6a1a50e 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -917,10 +917,10 @@ impl Window { } fn should_move_clip_rect(clip_rect: Rect, new_viewport: Rect) -> bool{ - let clip_rect = Rect(Point2D(geometry::to_frac_px(clip_rect.origin.x) as f32, - geometry::to_frac_px(clip_rect.origin.y) as f32), - Size2D(geometry::to_frac_px(clip_rect.size.width) as f32, - geometry::to_frac_px(clip_rect.size.height) as f32)); + let clip_rect = Rect(Point2D(clip_rect.origin.x.to_frac32_px(), + clip_rect.origin.y.to_frac32_px()), + Size2D(clip_rect.size.width.to_frac32_px(), + clip_rect.size.height.to_frac32_px())); // We only need to move the clip rect if the viewport is getting near the edge of // our preexisting clip rect. We use half of the size of the viewport as a heuristic diff --git a/components/script/script_task.rs b/components/script/script_task.rs index 5e776d4db0f..355b9d01210 100644 --- a/components/script/script_task.rs +++ b/components/script/script_task.rs @@ -70,7 +70,6 @@ use net_traits::LoadData as NetLoadData; use net_traits::image_cache_task::{ImageCacheChan, ImageCacheTask, ImageCacheResult}; use net_traits::storage_task::StorageTask; use string_cache::Atom; -use util::geometry::to_frac_px; use util::str::DOMString; use util::task::{spawn_named, spawn_named_with_send_on_failure}; use util::task_state; @@ -85,7 +84,6 @@ use js::rust::{Runtime, RtUtils}; use url::Url; use libc; -use num::ToPrimitive; use std::any::Any; use std::borrow::ToOwned; use std::cell::{Cell, RefCell}; @@ -1182,8 +1180,7 @@ impl ScriptTask { fn scroll_fragment_point(&self, pipeline_id: PipelineId, node: JSRef) { let node: JSRef = NodeCast::from_ref(node); let rect = node.get_bounding_content_box(); - let point = Point2D(to_frac_px(rect.origin.x).to_f32().unwrap(), - to_frac_px(rect.origin.y).to_f32().unwrap()); + let point = Point2D(rect.origin.x.to_frac32_px(), rect.origin.y.to_frac32_px()); // FIXME(#2003, pcwalton): This is pretty bogus when multiple layers are involved. // Really what needs to happen is that this needs to go through layout to ask which // layer the element belongs to, and have it send the scroll message to the diff --git a/components/util/geometry.rs b/components/util/geometry.rs index 5d7c69d0010..515529ad2f7 100644 --- a/components/util/geometry.rs +++ b/components/util/geometry.rs @@ -210,6 +210,13 @@ impl Au { Au((px.get() * 60f32) as i32) } + /// Rounds this app unit down to the pixel towards zero and returns it. + #[inline] + pub fn to_px(&self) -> isize { + let Au(a) = *self; + (a / 60) as isize + } + /// Rounds this app unit down to the previous (left or top) pixel and returns it. #[inline] pub fn to_prev_px(&self) -> isize { @@ -257,27 +264,13 @@ impl Au { #[inline] pub fn from_pt(pt: f64) -> Au { - from_frac_px(pt_to_px(pt)) + Au::from_frac_px(pt_to_px(pt)) } #[inline] pub fn from_frac_px(px: f64) -> Au { Au((px * 60.) as i32) } - - #[inline] - pub fn min(x: Au, y: Au) -> Au { - let Au(xi) = x; - let Au(yi) = y; - if xi < yi { x } else { y } - } - - #[inline] - pub fn max(x: Au, y: Au) -> Au { - let Au(xi) = x; - let Au(yi) = y; - if xi > yi { x } else { y } - } } // assumes 72 points per inch, and 96 px per inch @@ -290,35 +283,6 @@ pub fn px_to_pt(px: f64) -> f64 { px / 96. * 72. } -pub fn from_frac_px(px: f64) -> Au { - Au((px * 60.) as i32) -} - -pub fn from_px(px: isize) -> Au { - Au::from_px(px) -} - -pub fn to_px(au: Au) -> isize { - let Au(a) = au; - (a / 60) as isize -} - -pub fn to_frac_px(au: Au) -> f32 { - let Au(a) = au; - (a as f32) / 60. -} - -// assumes 72 points per inch, and 96 px per inch -pub fn from_pt(pt: f32) -> Au { - from_px((pt / 72. * 96.) as isize) -} - -// assumes 72 points per inch, and 96 px per inch -pub fn to_pt(au: Au) -> f32 { - let Au(a) = au; - (a as f32) / 60. * 72. / 96. -} - /// 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.