From 5684a7518753b06ec7a2f7c81469668d8af7a53a Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Mon, 28 Sep 2015 15:51:21 +1000 Subject: [PATCH] A few clean ups for Au type --- components/gfx/paint_context.rs | 6 ++-- components/gfx/platform/macos/font.rs | 30 ++++++++++++++------ components/util/geometry.rs | 40 --------------------------- 3 files changed, 25 insertions(+), 51 deletions(-) diff --git a/components/gfx/paint_context.rs b/components/gfx/paint_context.rs index 4981f4f8856..87463981aa8 100644 --- a/components/gfx/paint_context.rs +++ b/components/gfx/paint_context.rs @@ -34,7 +34,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_RECT}; +use util::geometry::{self, Au, MAX_RECT, ZERO_POINT, ZERO_RECT}; use util::opts; use util::range::Range; @@ -1344,7 +1344,7 @@ impl<'a> PaintContext<'a> { self.draw_target.set_transform(&draw_target_transform.mul(&Matrix2D::new(0., -1., 1., 0., x, y))); - Point2D::zero() + ZERO_POINT } SidewaysRight => { let x = text.baseline_origin.x.to_f32_px(); @@ -1352,7 +1352,7 @@ impl<'a> PaintContext<'a> { self.draw_target.set_transform(&draw_target_transform.mul(&Matrix2D::new(0., 1., -1., 0., x, y))); - Point2D::zero() + ZERO_POINT } }; diff --git a/components/gfx/platform/macos/font.rs b/components/gfx/platform/macos/font.rs index fa29592d184..0cf95aa6c3f 100644 --- a/components/gfx/platform/macos/font.rs +++ b/components/gfx/platform/macos/font.rs @@ -25,12 +25,26 @@ use std::ptr; use std::sync::Arc; use style::computed_values::{font_stretch, font_weight}; use text::glyph::GlyphId; -use util::geometry::{Au, px_to_pt}; +use util::geometry::Au; pub struct FontTable { data: CFData, } +// assumes 72 points per inch, and 96 px per inch +fn px_to_pt(px: f64) -> f64 { + px / 96. * 72. +} + +// assumes 72 points per inch, and 96 px per inch +fn pt_to_px(pt: f64) -> f64 { + pt / 72. * 96. +} + +fn au_from_pt(pt: f64) -> Au { + Au::from_f64_px(pt_to_px(pt)) +} + impl FontTable { pub fn wrap(data: CFData) -> FontTable { FontTable { data: data } @@ -161,27 +175,27 @@ impl FontHandleMethods for FontHandle { let scale = px_to_pt(self.ctfont.pt_size() as f64) / (ascent + descent); let line_gap = (ascent + descent + leading + 0.5).floor(); - let max_advance_width = Au::from_pt(bounding_rect.size.width as f64); + let max_advance_width = au_from_pt(bounding_rect.size.width as f64); let average_advance = self.glyph_index('0') .and_then(|idx| self.glyph_h_advance(idx)) .map(|advance| Au::from_f64_px(advance)) .unwrap_or(max_advance_width); let metrics = FontMetrics { - underline_size: Au::from_pt(self.ctfont.underline_thickness() as f64), + underline_size: au_from_pt(self.ctfont.underline_thickness() as f64), // TODO(Issue #201): underline metrics are not reliable. Have to pull out of font table // directly. // // 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), + underline_offset: au_from_pt(self.ctfont.underline_position() as f64), 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), + leading: au_from_pt(leading), + x_height: au_from_pt(self.ctfont.x_height() as f64), em_size: em_size, - ascent: Au::from_pt(ascent * scale), - descent: Au::from_pt(descent * scale), + ascent: au_from_pt(ascent * scale), + descent: au_from_pt(descent * scale), max_advance: max_advance_width, average_advance: average_advance, line_gap: Au::from_f64_px(line_gap), diff --git a/components/util/geometry.rs b/components/util/geometry.rs index fb39cfd9919..d14b1808f83 100644 --- a/components/util/geometry.rs +++ b/components/util/geometry.rs @@ -3,7 +3,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use cssparser::ToCss; -use euclid::length::Length; use euclid::num::Zero; use euclid::point::Point2D; use euclid::rect::Rect; @@ -205,29 +204,12 @@ impl Au { Au((px * AU_PER_PX) as i32) } - #[inline] - pub fn from_page_px(px: Length) -> Au { - Au((px.get() * (AU_PER_PX as f32)) 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 } - /// Rounds this app unit down to the previous (left or top) pixel and returns it. - #[inline] - pub fn to_prev_px(self) -> i32 { - ((self.0 as f64) / (AU_PER_PX as f64)).floor() as i32 - } - - /// Rounds this app unit up to the next (right or bottom) pixel and returns it. - #[inline] - pub fn to_next_px(self) -> i32 { - ((self.0 as f64) / (AU_PER_PX as f64)).ceil() as i32 - } - #[inline] pub fn to_nearest_px(self) -> i32 { ((self.0 as f64) / (AU_PER_PX as f64)).round() as i32 @@ -248,39 +230,17 @@ impl Au { (self.0 as f64) / (AU_PER_PX as f64) } - #[inline] - pub fn to_snapped(self) -> Au { - let res = self.0 % AU_PER_PX; - return if res >= 30i32 { return Au(self.0 - res + AU_PER_PX) } - else { return Au(self.0 - res) }; - } - #[inline] pub fn from_f32_px(px: f32) -> Au { Au((px * (AU_PER_PX as f32)) as i32) } - #[inline] - pub fn from_pt(pt: f64) -> Au { - Au::from_f64_px(pt_to_px(pt)) - } - #[inline] pub fn from_f64_px(px: f64) -> Au { Au((px * (AU_PER_PX as f64)) as i32) } } -// assumes 72 points per inch, and 96 px per inch -pub fn pt_to_px(pt: f64) -> f64 { - pt / 72. * 96. -} - -// assumes 72 points per inch, and 96 px per inch -pub fn px_to_pt(px: f64) -> f64 { - px / 96. * 72. -} - /// 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.