mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +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
|
@ -3,6 +3,7 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use app_units::Au;
|
||||
use font::{FontFamilyDescriptor, FontFamilyName, FontSearchScope};
|
||||
use font_context::FontSource;
|
||||
use font_template::{FontTemplate, FontTemplateDescriptor};
|
||||
use fontsan;
|
||||
|
@ -13,7 +14,6 @@ use platform::font_context::FontContextHandle;
|
|||
use platform::font_list::SANS_SERIF_FONT_FAMILY;
|
||||
use platform::font_list::for_each_available_family;
|
||||
use platform::font_list::for_each_variation;
|
||||
use platform::font_list::last_resort_font_families;
|
||||
use platform::font_list::system_default_family;
|
||||
use platform::font_template::FontTemplateData;
|
||||
use servo_atoms::Atom;
|
||||
|
@ -24,7 +24,7 @@ use std::collections::HashMap;
|
|||
use std::ops::Deref;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use style::font_face::{EffectiveSources, Source};
|
||||
use style::values::computed::font::{SingleFontFamily, FamilyName};
|
||||
use style::values::computed::font::FamilyName;
|
||||
use webrender_api;
|
||||
|
||||
/// A list of font templates that make up a given font family.
|
||||
|
@ -103,8 +103,7 @@ impl FontTemplates {
|
|||
/// Commands that the FontContext sends to the font cache thread.
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub enum Command {
|
||||
GetFontTemplate(SingleFontFamily, FontTemplateDescriptor, IpcSender<Reply>),
|
||||
GetLastResortFontTemplate(FontTemplateDescriptor, IpcSender<Reply>),
|
||||
GetFontTemplate(FontTemplateDescriptor, FontFamilyDescriptor, IpcSender<Reply>),
|
||||
GetFontInstance(webrender_api::FontKey, Au, IpcSender<webrender_api::FontInstanceKey>),
|
||||
AddWebFont(LowercaseString, EffectiveSources, IpcSender<()>),
|
||||
AddDownloadedWebFont(LowercaseString, ServoUrl, Vec<u8>, IpcSender<()>),
|
||||
|
@ -123,7 +122,7 @@ pub enum Reply {
|
|||
struct FontCache {
|
||||
port: IpcReceiver<Command>,
|
||||
channel_to_self: IpcSender<Command>,
|
||||
generic_fonts: HashMap<SingleFontFamily, LowercaseString>,
|
||||
generic_fonts: HashMap<FontFamilyName, LowercaseString>,
|
||||
local_families: HashMap<LowercaseString, FontTemplates>,
|
||||
web_families: HashMap<LowercaseString, FontTemplates>,
|
||||
font_context: FontContextHandle,
|
||||
|
@ -133,27 +132,28 @@ struct FontCache {
|
|||
font_instances: HashMap<(webrender_api::FontKey, Au), webrender_api::FontInstanceKey>,
|
||||
}
|
||||
|
||||
fn populate_generic_fonts() -> HashMap<SingleFontFamily, LowercaseString> {
|
||||
fn populate_generic_fonts() -> HashMap<FontFamilyName, LowercaseString> {
|
||||
let mut generic_fonts = HashMap::with_capacity(5);
|
||||
|
||||
append_map(&mut generic_fonts, SingleFontFamily::Generic(atom!("serif")), "Times New Roman");
|
||||
append_map(&mut generic_fonts, SingleFontFamily::Generic(atom!("sans-serif")), SANS_SERIF_FONT_FAMILY);
|
||||
append_map(&mut generic_fonts, SingleFontFamily::Generic(atom!("cursive")), "Apple Chancery");
|
||||
append_map(&mut generic_fonts, SingleFontFamily::Generic(atom!("fantasy")), "Papyrus");
|
||||
append_map(&mut generic_fonts, SingleFontFamily::Generic(atom!("monospace")), "Menlo");
|
||||
append_map(&mut generic_fonts, "serif", "Times New Roman");
|
||||
append_map(&mut generic_fonts, "sans-serif", SANS_SERIF_FONT_FAMILY);
|
||||
append_map(&mut generic_fonts, "cursive", "Apple Chancery");
|
||||
append_map(&mut generic_fonts, "fantasy", "Papyrus");
|
||||
append_map(&mut generic_fonts, "monospace", "Menlo");
|
||||
|
||||
fn append_map(generic_fonts: &mut HashMap<SingleFontFamily, LowercaseString>,
|
||||
font_family: SingleFontFamily,
|
||||
mapped_name: &str) {
|
||||
let family_name = {
|
||||
let opt_system_default = system_default_family(font_family.name());
|
||||
match opt_system_default {
|
||||
Some(system_default) => LowercaseString::new(&system_default),
|
||||
None => LowercaseString::new(mapped_name)
|
||||
}
|
||||
fn append_map(
|
||||
generic_fonts: &mut HashMap<FontFamilyName, LowercaseString>,
|
||||
generic_name: &str,
|
||||
mapped_name: &str,
|
||||
) {
|
||||
let family_name = match system_default_family(generic_name) {
|
||||
Some(system_default) => LowercaseString::new(&system_default),
|
||||
None => LowercaseString::new(mapped_name)
|
||||
};
|
||||
|
||||
generic_fonts.insert(font_family, family_name);
|
||||
let generic_name = FontFamilyName::Generic(Atom::from(generic_name));
|
||||
|
||||
generic_fonts.insert(generic_name, family_name);
|
||||
}
|
||||
|
||||
|
||||
|
@ -166,14 +166,10 @@ impl FontCache {
|
|||
let msg = self.port.recv().unwrap();
|
||||
|
||||
match msg {
|
||||
Command::GetFontTemplate(family, descriptor, result) => {
|
||||
let maybe_font_template = self.find_font_template(&family, &descriptor);
|
||||
Command::GetFontTemplate(template_descriptor, family_descriptor, result) => {
|
||||
let maybe_font_template = self.find_font_template(&template_descriptor, &family_descriptor);
|
||||
let _ = result.send(Reply::GetFontTemplateReply(maybe_font_template));
|
||||
}
|
||||
Command::GetLastResortFontTemplate(descriptor, result) => {
|
||||
let font_template = self.last_resort_font_template(&descriptor);
|
||||
let _ = result.send(Reply::GetFontTemplateReply(Some(font_template)));
|
||||
}
|
||||
Command::GetFontInstance(font_key, size, result) => {
|
||||
let webrender_api = &self.webrender_api;
|
||||
|
||||
|
@ -320,23 +316,28 @@ impl FontCache {
|
|||
});
|
||||
}
|
||||
|
||||
fn transform_family(&self, family: &SingleFontFamily) -> LowercaseString {
|
||||
match self.generic_fonts.get(family) {
|
||||
None => LowercaseString::new(family.name()),
|
||||
fn transform_family(&self, family_name: &FontFamilyName) -> LowercaseString {
|
||||
match self.generic_fonts.get(family_name) {
|
||||
None => LowercaseString::from(family_name),
|
||||
Some(mapped_family) => (*mapped_family).clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn find_font_in_local_family(&mut self, family_name: &LowercaseString, desc: &FontTemplateDescriptor)
|
||||
-> Option<Arc<FontTemplateData>> {
|
||||
fn find_font_in_local_family(
|
||||
&mut self,
|
||||
template_descriptor: &FontTemplateDescriptor,
|
||||
family_name: &FontFamilyName,
|
||||
) -> Option<Arc<FontTemplateData>> {
|
||||
let family_name = self.transform_family(family_name);
|
||||
|
||||
// TODO(Issue #188): look up localized font family names if canonical name not found
|
||||
// look up canonical name
|
||||
if self.local_families.contains_key(family_name) {
|
||||
debug!("FontList: Found font family with name={}", &**family_name);
|
||||
let s = self.local_families.get_mut(family_name).unwrap();
|
||||
if self.local_families.contains_key(&family_name) {
|
||||
debug!("FontList: Found font family with name={}", &*family_name);
|
||||
let s = self.local_families.get_mut(&family_name).unwrap();
|
||||
|
||||
if s.templates.is_empty() {
|
||||
for_each_variation(family_name, |path| {
|
||||
for_each_variation(&family_name, |path| {
|
||||
s.add_template(Atom::from(&*path), None);
|
||||
});
|
||||
}
|
||||
|
@ -344,20 +345,23 @@ impl FontCache {
|
|||
// TODO(Issue #192: handle generic font families, like 'serif' and 'sans-serif'.
|
||||
// if such family exists, try to match style to a font
|
||||
|
||||
s.find_font_for_style(desc, &self.font_context)
|
||||
s.find_font_for_style(template_descriptor, &self.font_context)
|
||||
} else {
|
||||
debug!("FontList: Couldn't find font family with name={}", &**family_name);
|
||||
debug!("FontList: Couldn't find font family with name={}", &*family_name);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn find_font_in_web_family(&mut self, family: &SingleFontFamily, desc: &FontTemplateDescriptor)
|
||||
-> Option<Arc<FontTemplateData>> {
|
||||
let family_name = LowercaseString::new(family.name());
|
||||
fn find_font_in_web_family(
|
||||
&mut self,
|
||||
template_descriptor: &FontTemplateDescriptor,
|
||||
family_name: &FontFamilyName,
|
||||
) -> Option<Arc<FontTemplateData>> {
|
||||
let family_name = LowercaseString::from(family_name);
|
||||
|
||||
if self.web_families.contains_key(&family_name) {
|
||||
let templates = self.web_families.get_mut(&family_name).unwrap();
|
||||
templates.find_font_for_style(desc, &self.font_context)
|
||||
templates.find_font_for_style(template_descriptor, &self.font_context)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -385,32 +389,21 @@ impl FontCache {
|
|||
}
|
||||
}
|
||||
|
||||
fn find_font_template(&mut self, family: &SingleFontFamily, desc: &FontTemplateDescriptor)
|
||||
-> Option<FontTemplateInfo> {
|
||||
let template = self.find_font_in_web_family(family, desc)
|
||||
.or_else(|| {
|
||||
let transformed_family = self.transform_family(family);
|
||||
self.find_font_in_local_family(&transformed_family, desc)
|
||||
});
|
||||
|
||||
template.map(|template| {
|
||||
self.get_font_template_info(template)
|
||||
})
|
||||
}
|
||||
|
||||
fn last_resort_font_template(&mut self, desc: &FontTemplateDescriptor)
|
||||
-> FontTemplateInfo {
|
||||
let last_resort = last_resort_font_families();
|
||||
|
||||
for family in &last_resort {
|
||||
let family = LowercaseString::new(family);
|
||||
let maybe_font_in_family = self.find_font_in_local_family(&family, desc);
|
||||
if let Some(family) = maybe_font_in_family {
|
||||
return self.get_font_template_info(family)
|
||||
fn find_font_template(
|
||||
&mut self,
|
||||
template_descriptor: &FontTemplateDescriptor,
|
||||
family_descriptor: &FontFamilyDescriptor,
|
||||
) -> Option<FontTemplateInfo> {
|
||||
match family_descriptor.scope {
|
||||
FontSearchScope::Any => {
|
||||
self.find_font_in_web_family(&template_descriptor, &family_descriptor.name)
|
||||
.or_else(|| self.find_font_in_local_family(&template_descriptor, &family_descriptor.name))
|
||||
}
|
||||
}
|
||||
|
||||
panic!("Unable to find any fonts that match (do you have fallback fonts installed?)");
|
||||
FontSearchScope::Local => {
|
||||
self.find_font_in_local_family(&template_descriptor, &family_descriptor.name)
|
||||
}
|
||||
}.map(|t| self.get_font_template_info(t))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -480,11 +473,14 @@ impl FontSource for FontCacheThread {
|
|||
instance_key.unwrap()
|
||||
}
|
||||
|
||||
fn find_font_template(&mut self, family: SingleFontFamily, desc: FontTemplateDescriptor)
|
||||
-> Option<FontTemplateInfo> {
|
||||
fn font_template(
|
||||
&mut self,
|
||||
template_descriptor: FontTemplateDescriptor,
|
||||
family_descriptor: FontFamilyDescriptor,
|
||||
) -> Option<FontTemplateInfo> {
|
||||
let (response_chan, response_port) =
|
||||
ipc::channel().expect("failed to create IPC channel");
|
||||
self.chan.send(Command::GetFontTemplate(family, desc, response_chan))
|
||||
self.chan.send(Command::GetFontTemplate(template_descriptor, family_descriptor, response_chan))
|
||||
.expect("failed to send message to font cache thread");
|
||||
|
||||
let reply = response_port.recv();
|
||||
|
@ -501,27 +497,6 @@ impl FontSource for FontCacheThread {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn last_resort_font_template(&mut self, desc: FontTemplateDescriptor)
|
||||
-> FontTemplateInfo {
|
||||
let (response_chan, response_port) =
|
||||
ipc::channel().expect("failed to create IPC channel");
|
||||
self.chan.send(Command::GetLastResortFontTemplate(desc, response_chan))
|
||||
.expect("failed to send message to font cache thread");
|
||||
|
||||
let reply = response_port.recv();
|
||||
if reply.is_err() {
|
||||
let font_thread_has_closed = self.chan.send(Command::Ping).is_err();
|
||||
assert!(font_thread_has_closed, "Failed to receive a response from live font cache");
|
||||
panic!("Font cache thread has already exited.");
|
||||
}
|
||||
|
||||
match reply.unwrap() {
|
||||
Reply::GetFontTemplateReply(data) => {
|
||||
data.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
|
||||
|
@ -537,6 +512,12 @@ impl LowercaseString {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a FontFamilyName> for LowercaseString {
|
||||
fn from(family_name: &'a FontFamilyName) -> LowercaseString {
|
||||
LowercaseString::new(family_name.name())
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for LowercaseString {
|
||||
type Target = str;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue