From 00e3a2144d7722b794a9773f8ba66ee30e27d041 Mon Sep 17 00:00:00 2001 From: Daniel Hedlund Date: Sun, 8 Dec 2013 06:35:53 -0800 Subject: [PATCH 1/2] Document the Au struct and add similar font metrics debug as mac --- src/components/gfx/platform/android/font.rs | 7 +++++-- src/components/gfx/platform/linux/font.rs | 7 +++++-- src/components/util/geometry.rs | 8 ++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/components/gfx/platform/android/font.rs b/src/components/gfx/platform/android/font.rs index f8f78eb1100..c350f8b820c 100644 --- a/src/components/gfx/platform/android/font.rs +++ b/src/components/gfx/platform/android/font.rs @@ -244,7 +244,7 @@ impl FontHandleMethods for FontHandle { } } - return FontMetrics { + let metrics = FontMetrics { underline_size: underline_size, underline_offset: underline_offset, strikeout_size: strikeout_size, @@ -255,7 +255,10 @@ impl FontHandleMethods for FontHandle { ascent: ascent, descent: -descent, // linux font's seem to use the opposite sign from mac max_advance: max_advance - } + }; + + debug!("Font metrics (@{:f} pt): {:?}", geometry::to_pt(em_size), metrics); + return metrics; } fn get_table_for_tag(&self, _: FontTableTag) -> Option { diff --git a/src/components/gfx/platform/linux/font.rs b/src/components/gfx/platform/linux/font.rs index 04b288ee8c9..ca01f2ee949 100644 --- a/src/components/gfx/platform/linux/font.rs +++ b/src/components/gfx/platform/linux/font.rs @@ -243,7 +243,7 @@ impl FontHandleMethods for FontHandle { } } - return FontMetrics { + let metrics = FontMetrics { underline_size: underline_size, underline_offset: underline_offset, strikeout_size: strikeout_size, @@ -254,7 +254,10 @@ impl FontHandleMethods for FontHandle { ascent: ascent, descent: -descent, // linux font's seem to use the opposite sign from mac max_advance: max_advance - } + }; + + debug!("Font metrics (@{:f} pt): {:?}", geometry::to_pt(em_size), metrics); + return metrics; } fn get_table_for_tag(&self, _: FontTableTag) -> Option { diff --git a/src/components/util/geometry.rs b/src/components/util/geometry.rs index 5b7aae9f749..4b2cb0c2e43 100644 --- a/src/components/util/geometry.rs +++ b/src/components/util/geometry.rs @@ -9,6 +9,9 @@ use geom::size::Size2D; use std::num::{NumCast, One, Zero}; use std::fmt; +// 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. pub struct Au(i32); // We don't use #[deriving] here for inlining. @@ -225,3 +228,8 @@ pub fn from_pt(pt: f64) -> Au { from_px((pt / 72f64 * 96f64) as int) } +// assumes 72 points per inch, and 96 px per inch +pub fn to_pt(au: Au) -> f64 { + (*au as f64) / 60f64 * 72f64 / 96f64 +} + From 79789a18de4afefcf7407cc24081f1cf4b27466b Mon Sep 17 00:00:00 2001 From: Daniel Hedlund Date: Sun, 8 Dec 2013 11:25:20 -0800 Subject: [PATCH 2/2] Implement font leading metric for linux platform Fixes #76. --- src/components/gfx/platform/android/font.rs | 11 ++++++++++- src/components/gfx/platform/linux/font.rs | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/gfx/platform/android/font.rs b/src/components/gfx/platform/android/font.rs index c350f8b820c..745157f5354 100644 --- a/src/components/gfx/platform/android/font.rs +++ b/src/components/gfx/platform/android/font.rs @@ -231,6 +231,15 @@ impl FontHandleMethods for FontHandle { let descent = self.font_units_to_au(face.descender as float); let max_advance = self.font_units_to_au(face.max_advance_width as float); + // 'leading' is supposed to be the vertical distance between two baselines, + // reflected by the height attibute in freetype. On OS X (w/ CTFont), + // leading represents the distance between the bottom of a line descent to + // the top of the next line's ascent or: (line_height - ascent - descent), + // see http://stackoverflow.com/a/5635981 for CTFont implementation. + // Convert using a formular similar to what CTFont returns for consistency. + let height = self.font_units_to_au(face.height as float); + 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); @@ -249,7 +258,7 @@ impl FontHandleMethods for FontHandle { underline_offset: underline_offset, strikeout_size: strikeout_size, strikeout_offset: strikeout_offset, - leading: geometry::from_pt(0.0), //FIXME + leading: leading, x_height: x_height, em_size: em_size, ascent: ascent, diff --git a/src/components/gfx/platform/linux/font.rs b/src/components/gfx/platform/linux/font.rs index ca01f2ee949..cb2573a9a13 100644 --- a/src/components/gfx/platform/linux/font.rs +++ b/src/components/gfx/platform/linux/font.rs @@ -230,6 +230,15 @@ impl FontHandleMethods for FontHandle { let descent = self.font_units_to_au(face.descender as f64); let max_advance = self.font_units_to_au(face.max_advance_width as f64); + // 'leading' is supposed to be the vertical distance between two baselines, + // reflected by the height attibute in freetype. On OS X (w/ CTFont), + // leading represents the distance between the bottom of a line descent to + // the top of the next line's ascent or: (line_height - ascent - descent), + // see http://stackoverflow.com/a/5635981 for CTFont implementation. + // Convert using a formular similar to what CTFont returns for consistency. + 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); @@ -248,7 +257,7 @@ impl FontHandleMethods for FontHandle { underline_offset: underline_offset, strikeout_size: strikeout_size, strikeout_offset: strikeout_offset, - leading: geometry::from_pt(0.0), //FIXME + leading: leading, x_height: x_height, em_size: em_size, ascent: ascent,