mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
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.
197 lines
6.7 KiB
Rust
197 lines
6.7 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
use app_units::Au;
|
|
use fnv::FnvHasher;
|
|
use font::{Font, FontDescriptor, FontFamilyDescriptor, FontGroup, FontHandleMethods, FontRef};
|
|
use font_cache_thread::FontTemplateInfo;
|
|
use font_template::FontTemplateDescriptor;
|
|
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
|
|
use platform::font::FontHandle;
|
|
pub use platform::font_context::FontContextHandle;
|
|
use servo_arc::Arc;
|
|
use std::cell::RefCell;
|
|
use std::collections::HashMap;
|
|
use std::default::Default;
|
|
use std::hash::{BuildHasherDefault, Hash, Hasher};
|
|
use std::rc::Rc;
|
|
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
|
|
use style::computed_values::font_variant_caps::T as FontVariantCaps;
|
|
use style::properties::style_structs::Font as FontStyleStruct;
|
|
use webrender_api;
|
|
|
|
static SMALL_CAPS_SCALE_FACTOR: f32 = 0.8; // Matches FireFox (see gfxFont.h)
|
|
|
|
/// An epoch for the font context cache. The cache is flushed if the current epoch does not match
|
|
/// this one.
|
|
static FONT_CACHE_EPOCH: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
pub trait FontSource {
|
|
fn get_font_instance(&mut self, key: webrender_api::FontKey, size: Au) -> webrender_api::FontInstanceKey;
|
|
|
|
fn font_template(
|
|
&mut self,
|
|
template_descriptor: FontTemplateDescriptor,
|
|
family_descriptor: FontFamilyDescriptor,
|
|
) -> Option<FontTemplateInfo>;
|
|
}
|
|
|
|
/// The FontContext represents the per-thread/thread state necessary for
|
|
/// working with fonts. It is the public API used by the layout and
|
|
/// paint code. It talks directly to the font cache thread where
|
|
/// required.
|
|
#[derive(Debug)]
|
|
pub struct FontContext<S: FontSource> {
|
|
platform_handle: FontContextHandle,
|
|
font_source: S,
|
|
|
|
// TODO: The font context holds a strong ref to the cached fonts
|
|
// so they will never be released. Find out a good time to drop them.
|
|
// See bug https://github.com/servo/servo/issues/3300
|
|
font_cache: HashMap<FontCacheKey, Option<FontRef>>,
|
|
|
|
font_group_cache:
|
|
HashMap<FontGroupCacheKey, Rc<RefCell<FontGroup>>, BuildHasherDefault<FnvHasher>>,
|
|
|
|
epoch: usize,
|
|
}
|
|
|
|
impl<S: FontSource> FontContext<S> {
|
|
pub fn new(font_source: S) -> FontContext<S> {
|
|
let handle = FontContextHandle::new();
|
|
FontContext {
|
|
platform_handle: handle,
|
|
font_source,
|
|
font_cache: HashMap::new(),
|
|
font_group_cache: HashMap::with_hasher(Default::default()),
|
|
epoch: 0,
|
|
}
|
|
}
|
|
|
|
fn expire_font_caches_if_necessary(&mut self) {
|
|
let current_epoch = FONT_CACHE_EPOCH.load(Ordering::SeqCst);
|
|
if current_epoch == self.epoch {
|
|
return
|
|
}
|
|
|
|
self.font_cache.clear();
|
|
self.font_group_cache.clear();
|
|
self.epoch = current_epoch
|
|
}
|
|
|
|
/// Returns a `FontGroup` representing fonts which can be used for layout, given the `style`.
|
|
/// Font groups are cached, so subsequent calls with the same `style` will return a reference
|
|
/// to an existing `FontGroup`.
|
|
pub fn font_group(&mut self, style: Arc<FontStyleStruct>) -> Rc<RefCell<FontGroup>> {
|
|
self.expire_font_caches_if_necessary();
|
|
|
|
let cache_key = FontGroupCacheKey {
|
|
size: style.font_size.size(),
|
|
style,
|
|
};
|
|
|
|
if let Some(ref font_group) = self.font_group_cache.get(&cache_key) {
|
|
return (*font_group).clone()
|
|
}
|
|
|
|
let font_group = Rc::new(RefCell::new(FontGroup::new(&cache_key.style)));
|
|
self.font_group_cache.insert(cache_key, font_group.clone());
|
|
font_group
|
|
}
|
|
|
|
/// Returns a font matching the parameters. Fonts are cached, so repeated calls will return a
|
|
/// reference to the same underlying `Font`.
|
|
pub fn font(
|
|
&mut self,
|
|
font_descriptor: &FontDescriptor,
|
|
family_descriptor: &FontFamilyDescriptor,
|
|
) -> Option<FontRef> {
|
|
let cache_key = FontCacheKey {
|
|
font_descriptor: font_descriptor.clone(),
|
|
family_descriptor: family_descriptor.clone(),
|
|
};
|
|
|
|
self.font_cache.get(&cache_key).map(|v| v.clone()).unwrap_or_else(|| {
|
|
debug!(
|
|
"FontContext::font cache miss for font_descriptor={:?} family_descriptor={:?}",
|
|
font_descriptor,
|
|
family_descriptor
|
|
);
|
|
|
|
let font =
|
|
self.font_source.font_template(
|
|
font_descriptor.template_descriptor.clone(),
|
|
family_descriptor.clone(),
|
|
)
|
|
.and_then(|template_info| self.create_font(template_info, font_descriptor.to_owned()).ok())
|
|
.map(|font| Rc::new(RefCell::new(font)));
|
|
|
|
self.font_cache.insert(cache_key, font.clone());
|
|
font
|
|
})
|
|
}
|
|
|
|
/// Create a `Font` for use in layout calculations, from a `FontTemplateData` returned by the
|
|
/// cache thread and a `FontDescriptor` which contains the styling parameters.
|
|
fn create_font(
|
|
&mut self,
|
|
info: FontTemplateInfo,
|
|
descriptor: FontDescriptor
|
|
) -> Result<Font, ()> {
|
|
// TODO: (Bug #3463): Currently we only support fake small-caps
|
|
// painting. We should also support true small-caps (where the
|
|
// font supports it) in the future.
|
|
let actual_pt_size = match descriptor.variant {
|
|
FontVariantCaps::SmallCaps => descriptor.pt_size.scale_by(SMALL_CAPS_SCALE_FACTOR),
|
|
FontVariantCaps::Normal => descriptor.pt_size,
|
|
};
|
|
|
|
let handle = FontHandle::new_from_template(
|
|
&self.platform_handle,
|
|
info.font_template,
|
|
Some(actual_pt_size)
|
|
)?;
|
|
|
|
let font_instance_key = self.font_source.get_font_instance(info.font_key, actual_pt_size);
|
|
Ok(Font::new(handle, descriptor.to_owned(), actual_pt_size, font_instance_key))
|
|
}
|
|
}
|
|
|
|
impl<S: FontSource> MallocSizeOf for FontContext<S> {
|
|
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
|
|
// FIXME(njn): Measure other fields eventually.
|
|
self.platform_handle.size_of(ops)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Eq, Hash, PartialEq)]
|
|
struct FontCacheKey {
|
|
font_descriptor: FontDescriptor,
|
|
family_descriptor: FontFamilyDescriptor,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct FontGroupCacheKey {
|
|
style: Arc<FontStyleStruct>,
|
|
size: Au,
|
|
}
|
|
|
|
impl PartialEq for FontGroupCacheKey {
|
|
fn eq(&self, other: &FontGroupCacheKey) -> bool {
|
|
self.style == other.style && self.size == other.size
|
|
}
|
|
}
|
|
|
|
impl Eq for FontGroupCacheKey {}
|
|
|
|
impl Hash for FontGroupCacheKey {
|
|
fn hash<H>(&self, hasher: &mut H) where H: Hasher {
|
|
self.style.hash.hash(hasher)
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn invalidate_font_caches() {
|
|
FONT_CACHE_EPOCH.fetch_add(1, Ordering::SeqCst);
|
|
}
|