fonts: Improve font fallback (#32286)

- Better detect situations where emoji is necessary by looking ahead one
  character while laying out. This allow processing Unicode presentation
  selectors. When detecting emoji, put emoji fonts at the front of
  fallback lists for all platforms.

  This enables monochrome emoji on Windows. Full-color emoji on Windows
  probably needs full support for processing the COLR table and drawing
  separate glyph color layers.

- Improve the font fallback list on FreeType platforms. Ideally, Servo
  would be able to look through the entire font list to find the best
  font for a certain character, but until that time we can make sure the
  font list contains the "Noto Sans" fonts which cover most situations.

Fixes #31664.
Fixes #12944.
This commit is contained in:
Martin Robinson 2024-05-27 12:02:26 +02:00 committed by GitHub
parent 5f0866379a
commit 43a3c9c319
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 610 additions and 292 deletions

View file

@ -193,7 +193,6 @@ impl PlatformFontMethods for PlatformFont {
can_do_fast_shaping: false,
};
handle.h_kern_subtable = handle.find_h_kern_subtable();
// TODO (#11310): Implement basic support for GPOS and GSUB.
handle.can_do_fast_shaping = handle.h_kern_subtable.is_some() &&
handle.table_for_tag(GPOS).is_none() &&
handle.table_for_tag(GSUB).is_none();

View file

@ -17,6 +17,7 @@ use webrender_api::NativeFontHandle;
use crate::font_template::{FontTemplate, FontTemplateDescriptor};
use crate::platform::font::CoreTextFontTraitsMapping;
use crate::text::util::unicode_plane;
use crate::text::FallbackFontSelectionOptions;
/// An identifier for a local font on a MacOS system. These values comes from the CoreText
/// CTFontCollection. Note that `path` here is required. We do not load fonts that do not
@ -94,16 +95,14 @@ pub fn system_default_family(_generic_name: &str) -> Option<String> {
/// Get the list of fallback fonts given an optional codepoint. This is
/// based on `gfxPlatformMac::GetCommonFallbackFonts()` in Gecko from
/// <https://searchfox.org/mozilla-central/source/gfx/thebes/gfxPlatformMac.cpp>.
pub fn fallback_font_families(codepoint: Option<char>) -> Vec<&'static str> {
let mut families = vec!["Lucida Grande"];
let Some(codepoint) = codepoint else {
families.push("Geneva");
families.push("Arial Unicode MS");
return families;
};
pub fn fallback_font_families(options: FallbackFontSelectionOptions) -> Vec<&'static str> {
let mut families = Vec::new();
if options.prefer_emoji_presentation {
families.push("Apple Color Emoji");
}
let script = Script::from(codepoint);
if let Some(block) = codepoint.block() {
let script = Script::from(options.character);
if let Some(block) = options.character.block() {
match block {
// In most cases, COMMON and INHERITED characters will be merged into
// their context, but if they occur without any specific script context
@ -126,7 +125,7 @@ pub fn fallback_font_families(codepoint: Option<char>) -> Vec<&'static str> {
_ if matches!(script, Script::Bopomofo | Script::Han) => {
// TODO: Need to differentiate between traditional and simplified Han here!
families.push("Songti SC");
if codepoint as u32 > 0x10000 {
if options.character as u32 > 0x10000 {
// macOS installations with MS Office may have these -ExtB fonts
families.push("SimSun-ExtB");
}
@ -306,17 +305,19 @@ pub fn fallback_font_families(codepoint: Option<char>) -> Vec<&'static str> {
}
// https://en.wikipedia.org/wiki/Plane_(Unicode)#Supplementary_Multilingual_Plane
let unicode_plane = unicode_plane(codepoint);
let unicode_plane = unicode_plane(options.character);
if let 1 = unicode_plane {
let b = (codepoint as u32) >> 8;
if b >= 0x1f0 && b < 0x1f7 {
families.push("Apple Color Emoji");
let b = (options.character as u32) >> 8;
if b == 0x27 {
families.push("Zapf Dingbats");
}
families.push("Geneva");
families.push("Apple Symbols");
families.push("STIXGeneral");
families.push("Hiragino Sans");
families.push("Hiragino Kaku Gothic ProN");
}
families.push("Geneva");
families.push("Arial Unicode MS");
families
}