From 83e5a82388dc016627521dd8e428c2a6fb03fcfb Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Thu, 13 Jul 2017 11:38:07 +1000 Subject: [PATCH] Improve font layout in Linux / Freetype platforms. This patch contains two small changes: * Fix the font size calculation that is passed to Freetype. This now matches exactly how Webrender and Gecko calculate font size to pass to layout. * Enable light hinting by default for fonts when using Freetype. We should make this configurable in the future, but this is a better default than no hinting (and matches what most Linux distros default to). These two changes (along with the pending WR update) fix a lot of the font layout issues on Linux. There is still at least one remaining issue with hidpi displays on Linux that will be fixed in a follow up patch. --- components/gfx/platform/freetype/font.rs | 25 +++++++++++++------ .../html4/background-size-contain.htm.ini | 4 --- 2 files changed, 17 insertions(+), 12 deletions(-) delete mode 100644 tests/wpt/metadata-css/css-backgrounds-3_dev/html4/background-size-contain.htm.ini diff --git a/components/gfx/platform/freetype/font.rs b/components/gfx/platform/freetype/font.rs index b7bcd92d470e..f77f77be494d 100644 --- a/components/gfx/platform/freetype/font.rs +++ b/components/gfx/platform/freetype/font.rs @@ -10,7 +10,7 @@ use freetype::freetype::{FT_F26Dot6, FT_Face, FT_FaceRec}; use freetype::freetype::{FT_Get_Char_Index, FT_Get_Postscript_Name}; use freetype::freetype::{FT_Get_Kerning, FT_Get_Sfnt_Table, FT_Load_Sfnt_Table}; use freetype::freetype::{FT_GlyphSlot, FT_Library, FT_Long, FT_ULong}; -use freetype::freetype::{FT_Kerning_Mode, FT_STYLE_FLAG_BOLD, FT_STYLE_FLAG_ITALIC}; +use freetype::freetype::{FT_Int32, FT_Kerning_Mode, FT_STYLE_FLAG_BOLD, FT_STYLE_FLAG_ITALIC}; use freetype::freetype::{FT_Load_Glyph, FT_Set_Char_Size}; use freetype::freetype::{FT_SizeRec, FT_Size_Metrics, FT_UInt, FT_Vector}; use freetype::freetype::FT_Sfnt_Tag; @@ -23,11 +23,18 @@ use std::sync::Arc; use style::computed_values::{font_stretch, font_weight}; use super::c_str_to_string; use text::glyph::GlyphId; -use text::util::{fixed_to_float, float_to_fixed}; +use text::util::fixed_to_float; -fn float_to_fixed_ft(f: f64) -> i32 { - float_to_fixed(6, f) -} +// This constant is not present in the freetype +// bindings due to bindgen not handling the way +// the macro is defined. +const FT_LOAD_TARGET_LIGHT: FT_Int32 = 1 << 16; + +// Default to slight hinting, which is what most +// Linux distros use by default, and is a better +// default than no hinting. +// TODO(gw): Make this configurable. +const GLYPH_LOAD_FLAGS: FT_Int32 = FT_LOAD_TARGET_LIGHT; fn fixed_to_float_ft(f: i32) -> f64 { fixed_to_float(6, f) @@ -187,7 +194,9 @@ impl FontHandleMethods for FontHandle { fn glyph_h_advance(&self, glyph: GlyphId) -> Option { assert!(!self.face.is_null()); unsafe { - let res = FT_Load_Glyph(self.face, glyph as FT_UInt, 0); + let res = FT_Load_Glyph(self.face, + glyph as FT_UInt, + GLYPH_LOAD_FLAGS); if res.succeeded() { let void_glyph = (*self.face).glyph; let slot: FT_GlyphSlot = mem::transmute(void_glyph); @@ -280,10 +289,10 @@ impl FontHandleMethods for FontHandle { impl<'a> FontHandle { fn set_char_size(face: FT_Face, pt_size: Au) -> Result<(), ()>{ - let char_width = float_to_fixed_ft((0.5f64 + pt_size.to_f64_px()).floor()) as FT_F26Dot6; + let char_size = pt_size.to_f64_px() * 64.0 + 0.5; unsafe { - let result = FT_Set_Char_Size(face, char_width, 0, 0, 0); + let result = FT_Set_Char_Size(face, char_size as FT_F26Dot6, 0, 0, 0); if result.succeeded() { Ok(()) } else { Err(()) } } } diff --git a/tests/wpt/metadata-css/css-backgrounds-3_dev/html4/background-size-contain.htm.ini b/tests/wpt/metadata-css/css-backgrounds-3_dev/html4/background-size-contain.htm.ini deleted file mode 100644 index 6f209931d0d6..000000000000 --- a/tests/wpt/metadata-css/css-backgrounds-3_dev/html4/background-size-contain.htm.ini +++ /dev/null @@ -1,4 +0,0 @@ -[background-size-contain.htm] - type: reftest - expected: - if os == "linux": FAIL