mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
Implement font fallback
Prior to this change, if none of the fonts specified in CSS contained a glyph for a codepoint, we tried only one fallback font. If that font didn't contain the glyph, we'd give up. With this change, we try multiple fonts in turn. The font names we try differ across each platform, and based on the codepoint we're trying to match. The current implementation is heavily inspired by the analogous code in Gecko, but I've used to ucd lib to make it more readable, whereas Gecko matches raw unicode ranges. This fixes some of the issues reported in #17267, although colour emoji support is not implemented. == Notes on changes to WPT metadata == === css/css-text/i18n/css3-text-line-break-opclns-* === A bunch of these have started failing on macos when they previously passed. These tests check that the browser automatically inserts line breaks near certain characters that are classified as "opening and closing punctuation". The idea is that if we have e.g. an opening parenthesis, it does not make sense for it to appear at the end of a line box; it should "stick" to the next character and go into the next line box. Before this change, a lot of these codepoints rendered as a missing glyph on Mac and Linux. In some cases, that meant that the test was passing. After this change, a bunch of these codepoints are now rendering glyphs on Mac (but not Linux). In some cases, the test should continue to pass where it previously did when rendering with the missing glyph. However, it seems this has also exposed a layout bug. The "ref" div in these tests contains a <br> element, and it seems that this, combined with these punctuation characters, makes the spacing between glyphs ever so slightly different to the "test" div. (Speculation: might be something to do with shaping?) Therefore I've had to mark a bunch of these tests failing on mac. === css/css-text/i18n/css3-text-line-break-baspglwj-* === Some of these previously passed on Mac due to a missing glyph. Now that we're rendering the correct glyph, they are failing. === css/css-text/word-break/word-break-normal-bo-000.html === The characters now render correctly on Mac, and the test is passing. But we do not find a suitable fallback font on Linux, so it is still failing on that platform. === css/css-text/word-break/word-break-break-all-007.html === This was previously passing on Mac, but only because missing character glyphs were rendered. Now that a fallback font is able to be found, it (correctly) fails. === mozilla/tests/css/font_fallback_* === These are new tests added in this commit. 01 and 02 are marked failing on Linux because the builders don't have the appropriate fonts installed (that will be a follow-up). Fix build errors from rebase FontTemplateDescriptor can no longer just derive(Hash). We need to implement it on each component part, because the components now generally wrap floats, which do not impl Hash because of NaN. However in this case we know that we won't have a NaN, so it is safe to manually impl Hash.
This commit is contained in:
parent
15a677c639
commit
691c6c6f1a
81 changed files with 1192 additions and 360 deletions
|
@ -10,7 +10,7 @@ extern crate style;
|
|||
extern crate webrender_api;
|
||||
|
||||
use app_units::Au;
|
||||
use gfx::font::FontHandleMethods;
|
||||
use gfx::font::{fallback_font_families, FontFamilyDescriptor, FontHandleMethods};
|
||||
use gfx::font_cache_thread::{FontTemplates, FontTemplateInfo};
|
||||
use gfx::font_context::{FontContext, FontContextHandle, FontSource};
|
||||
use gfx::font_template::FontTemplateDescriptor;
|
||||
|
@ -24,29 +24,31 @@ use std::path::PathBuf;
|
|||
use std::rc::Rc;
|
||||
use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps;
|
||||
use style::properties::style_structs::Font as FontStyleStruct;
|
||||
use style::values::computed::Percentage;
|
||||
use style::values::computed::font::{FamilyName, FamilyNameSyntax, FontFamily, FontFamilyList, FontSize};
|
||||
use style::values::computed::font::{FontWeight, SingleFontFamily};
|
||||
use style::values::generics::NonNegative;
|
||||
use style::values::computed::font::{FontStretch, FontWeight, SingleFontFamily};
|
||||
use style::values::generics::font::FontStyle;
|
||||
|
||||
struct TestFontSource {
|
||||
handle: FontContextHandle,
|
||||
families: HashMap<Atom, FontTemplates>,
|
||||
families: HashMap<String, FontTemplates>,
|
||||
find_font_count: Rc<Cell<isize>>,
|
||||
}
|
||||
|
||||
impl TestFontSource {
|
||||
fn new() -> TestFontSource {
|
||||
let mut csstest_ascii = FontTemplates::new();
|
||||
Self::add_face(&mut csstest_ascii, "csstest-ascii");
|
||||
Self::add_face(&mut csstest_ascii, "csstest-ascii", None);
|
||||
|
||||
let mut csstest_basic = FontTemplates::new();
|
||||
Self::add_face(&mut csstest_basic, "csstest-basic-regular");
|
||||
Self::add_face(&mut csstest_basic, "csstest-basic-regular", None);
|
||||
|
||||
let mut fallback = FontTemplates::new();
|
||||
Self::add_face(&mut fallback, "csstest-basic-regular", Some("fallback"));
|
||||
|
||||
let mut families = HashMap::new();
|
||||
families.insert(Atom::from("CSSTest ASCII"), csstest_ascii);
|
||||
families.insert(Atom::from("CSSTest Basic"), csstest_basic);
|
||||
families.insert("CSSTest ASCII".to_owned(), csstest_ascii);
|
||||
families.insert("CSSTest Basic".to_owned(), csstest_basic);
|
||||
families.insert(fallback_font_families(None)[0].to_owned(), fallback);
|
||||
|
||||
TestFontSource {
|
||||
handle: FontContextHandle::new(),
|
||||
|
@ -55,7 +57,7 @@ impl TestFontSource {
|
|||
}
|
||||
}
|
||||
|
||||
fn add_face(family: &mut FontTemplates, name: &str) {
|
||||
fn add_face(family: &mut FontTemplates, name: &str, identifier: Option<&str>) {
|
||||
let mut path: PathBuf = [
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"tests",
|
||||
|
@ -65,9 +67,10 @@ impl TestFontSource {
|
|||
path.push(format!("{}.ttf", name));
|
||||
|
||||
let file = File::open(path).unwrap();
|
||||
let identifier = Atom::from(identifier.unwrap_or(name));
|
||||
|
||||
family.add_template(
|
||||
Atom::from(name),
|
||||
identifier,
|
||||
Some(file.bytes().map(|b| b.unwrap()).collect())
|
||||
)
|
||||
}
|
||||
|
@ -78,17 +81,17 @@ impl FontSource for TestFontSource {
|
|||
webrender_api::FontInstanceKey(webrender_api::IdNamespace(0), 0)
|
||||
}
|
||||
|
||||
fn find_font_template(
|
||||
fn font_template(
|
||||
&mut self,
|
||||
family: SingleFontFamily,
|
||||
desc: FontTemplateDescriptor
|
||||
template_descriptor: FontTemplateDescriptor,
|
||||
family_descriptor: FontFamilyDescriptor,
|
||||
) -> Option<FontTemplateInfo> {
|
||||
let handle = &self.handle;
|
||||
|
||||
self.find_font_count.set(self.find_font_count.get() + 1);
|
||||
self.families
|
||||
.get_mut(family.atom())
|
||||
.and_then(|family| family.find_font_for_style(&desc, handle))
|
||||
.get_mut(family_descriptor.name())
|
||||
.and_then(|family| family.find_font_for_style(&template_descriptor, handle))
|
||||
.map(|template| {
|
||||
FontTemplateInfo {
|
||||
font_template: template,
|
||||
|
@ -96,10 +99,6 @@ impl FontSource for TestFontSource {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn last_resort_font_template(&mut self, _desc: FontTemplateDescriptor) -> FontTemplateInfo {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
fn style() -> FontStyleStruct {
|
||||
|
@ -109,7 +108,7 @@ fn style() -> FontStyleStruct {
|
|||
font_variant_caps: FontVariantCaps::Normal,
|
||||
font_weight: FontWeight::normal(),
|
||||
font_size: FontSize::medium(),
|
||||
font_stretch: NonNegative(Percentage(1.)),
|
||||
font_stretch: FontStretch::hundred(),
|
||||
hash: 0,
|
||||
};
|
||||
style.compute_font_hash();
|
||||
|
@ -162,14 +161,37 @@ fn test_font_group_find_by_codepoint() {
|
|||
let group = context.font_group(Arc::new(style));
|
||||
|
||||
let font = group.borrow_mut().find_by_codepoint(&mut context, 'a').unwrap();
|
||||
assert_eq!(font.borrow().handle.family_name(), "CSSTest ASCII");
|
||||
assert_eq!(&*font.borrow().identifier(), "csstest-ascii");
|
||||
assert_eq!(count.get(), 1, "only the first font in the list should have been loaded");
|
||||
|
||||
let font = group.borrow_mut().find_by_codepoint(&mut context, 'a').unwrap();
|
||||
assert_eq!(font.borrow().handle.family_name(), "CSSTest ASCII");
|
||||
assert_eq!(&*font.borrow().identifier(), "csstest-ascii");
|
||||
assert_eq!(count.get(), 1, "we shouldn't load the same font a second time");
|
||||
|
||||
let font = group.borrow_mut().find_by_codepoint(&mut context, 'á').unwrap();
|
||||
assert_eq!(font.borrow().handle.family_name(), "CSSTest Basic");
|
||||
assert_eq!(&*font.borrow().identifier(), "csstest-basic-regular");
|
||||
assert_eq!(count.get(), 2, "both fonts should now have been loaded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_font_fallback() {
|
||||
let source = TestFontSource::new();
|
||||
let mut context = FontContext::new(source);
|
||||
|
||||
let mut style = style();
|
||||
style.set_font_family(font_family(vec!("CSSTest ASCII")));
|
||||
|
||||
let group = context.font_group(Arc::new(style));
|
||||
|
||||
let font = group.borrow_mut().find_by_codepoint(&mut context, 'a').unwrap();
|
||||
assert_eq!(
|
||||
&*font.borrow().identifier(), "csstest-ascii",
|
||||
"a family in the group should be used if there is a matching glyph"
|
||||
);
|
||||
|
||||
let font = group.borrow_mut().find_by_codepoint(&mut context, 'á').unwrap();
|
||||
assert_eq!(
|
||||
&*font.borrow().identifier(), "fallback",
|
||||
"a fallback font should be used if there is no matching glyph in the group"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ fn test_font_template_descriptor() {
|
|||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use style::values::computed::Percentage;
|
||||
use style::values::computed::font::FontWeight;
|
||||
use style::values::computed::font::{FontStretch, FontWeight};
|
||||
use style::values::generics::NonNegative;
|
||||
use style::values::generics::font::FontStyle;
|
||||
|
||||
|
@ -45,25 +45,25 @@ fn test_font_template_descriptor() {
|
|||
|
||||
assert_eq!(descriptor("DejaVuSans"), FontTemplateDescriptor {
|
||||
weight: FontWeight::normal(),
|
||||
stretch: NonNegative(Percentage(1.)),
|
||||
stretch: FontStretch::hundred(),
|
||||
style: FontStyle::Normal,
|
||||
});
|
||||
|
||||
assert_eq!(descriptor("DejaVuSans-Bold"), FontTemplateDescriptor {
|
||||
weight: FontWeight::bold(),
|
||||
stretch: NonNegative(Percentage(1.)),
|
||||
stretch: FontStretch::hundred(),
|
||||
style: FontStyle::Normal,
|
||||
});
|
||||
|
||||
assert_eq!(descriptor("DejaVuSans-Oblique"), FontTemplateDescriptor {
|
||||
weight: FontWeight::normal(),
|
||||
stretch: NonNegative(Percentage(1.)),
|
||||
stretch: FontStretch::hundred(),
|
||||
style: FontStyle::Italic,
|
||||
});
|
||||
|
||||
assert_eq!(descriptor("DejaVuSansCondensed-BoldOblique"), FontTemplateDescriptor {
|
||||
weight: FontWeight::bold(),
|
||||
stretch: NonNegative(Percentage(0.875)),
|
||||
stretch: FontStretch(NonNegative(Percentage(0.875))),
|
||||
style: FontStyle::Italic,
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue