mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Add test for FontContext/FontGroup functionality
Unfortunately, this required quite a bit of changes to the non-test code. That's because FontContext depends on a FontCacheThread, which in turn depends on a CoreResourceThread and therefore lots of other data structures. It seemed like it would be very difficult to instantiate a FontContext as it was, and even if we could it seems like overkill to have all these data structures present for a relatively focused test. Therefore, I created a FontSource trait which represents the interface which FontContext uses to talk to FontCacheThread. FontCacheThread then implements FontSource. Then, in the test, we can create a dummy implementation of FontSource rather than using FontCacheThread. This actually has the advantage that we can make our dummy implementation behave in certain specific way which are useful for testing, for example it can count the number of times find_font_template() is called, which helps us verify that caching/lazy-loading is working as intended.
This commit is contained in:
parent
f22e5ef3bd
commit
e4acb3f77f
61 changed files with 381 additions and 62 deletions
|
@ -4,7 +4,7 @@
|
|||
|
||||
use app_units::Au;
|
||||
use euclid::{Point2D, Rect, Size2D};
|
||||
use font_context::FontContext;
|
||||
use font_context::{FontContext, FontSource};
|
||||
use font_template::FontTemplateDescriptor;
|
||||
use ordered_float::NotNaN;
|
||||
use platform::font::{FontHandle, FontTable};
|
||||
|
@ -341,23 +341,33 @@ impl FontGroup {
|
|||
/// `codepoint`. If no such font is found, returns the first available font or fallback font
|
||||
/// (which will cause a "glyph not found" character to be rendered). If no font at all can be
|
||||
/// found, returns None.
|
||||
pub fn find_by_codepoint(&mut self, mut font_context: &mut FontContext, codepoint: char) -> Option<FontRef> {
|
||||
pub fn find_by_codepoint<S: FontSource>(
|
||||
&mut self,
|
||||
mut font_context: &mut FontContext<S>,
|
||||
codepoint: char
|
||||
) -> Option<FontRef> {
|
||||
self.find(&mut font_context, |font| font.borrow().has_glyph_for(codepoint))
|
||||
.or_else(|| self.first(&mut font_context))
|
||||
}
|
||||
|
||||
pub fn first(&mut self, mut font_context: &mut FontContext) -> Option<FontRef> {
|
||||
pub fn first<S: FontSource>(
|
||||
&mut self,
|
||||
mut font_context: &mut FontContext<S>
|
||||
) -> Option<FontRef> {
|
||||
self.find(&mut font_context, |_| true)
|
||||
}
|
||||
|
||||
/// Find a font which returns true for `predicate`. This method mutates because we may need to
|
||||
/// load new font data in the process of finding a suitable font.
|
||||
fn find<P>(
|
||||
fn find<S, P>(
|
||||
&mut self,
|
||||
mut font_context: &mut FontContext,
|
||||
mut font_context: &mut FontContext<S>,
|
||||
mut predicate: P
|
||||
) -> Option<FontRef>
|
||||
where P: FnMut(&FontRef) -> bool {
|
||||
where
|
||||
S: FontSource,
|
||||
P: FnMut(&FontRef) -> bool
|
||||
{
|
||||
self.families.iter_mut()
|
||||
.filter_map(|family| family.font(&mut font_context))
|
||||
.find(|f| predicate(f))
|
||||
|
@ -392,7 +402,7 @@ impl FontGroupFamily {
|
|||
/// Returns the font within this family which matches the style. We'll fetch the data from the
|
||||
/// `FontContext` the first time this method is called, and return a cached reference on
|
||||
/// subsequent calls.
|
||||
fn font(&mut self, font_context: &mut FontContext) -> Option<FontRef> {
|
||||
fn font<S: FontSource>(&mut self, font_context: &mut FontContext<S>) -> Option<FontRef> {
|
||||
if !self.loaded {
|
||||
self.font = font_context.font(&self.descriptor, &self.family);
|
||||
self.loaded = true;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue