gfx: Perform fuzzy matching on font weights if an exact match wasn't

found.

Partially addresses #190.
Partially addresses #9487.
This commit is contained in:
Patrick Walton 2016-05-09 15:59:10 -07:00
parent 0617727f5b
commit 479c6c9c42
2 changed files with 86 additions and 38 deletions

View file

@ -18,6 +18,7 @@ use std::borrow::ToOwned;
use std::collections::HashMap;
use std::mem;
use std::sync::{Arc, Mutex};
use std::u32;
use string_cache::Atom;
use style::font_face::Source;
use style::properties::longhands::font_family::computed_value::FontFamily;
@ -51,9 +52,6 @@ impl FontTemplates {
// TODO(Issue #189): optimize lookup for
// regular/bold/italic/bolditalic with fixed offsets and a
// static decision table for fallback between these values.
// TODO(Issue #190): if not in the fast path above, do
// expensive matching of weights, etc.
for template in &mut self.templates {
let maybe_template = template.data_for_descriptor(fctx, desc);
if maybe_template.is_some() {
@ -61,6 +59,22 @@ impl FontTemplates {
}
}
// We didn't find an exact match. Do more expensive fuzzy matching.
// TODO(#190): Do a better job.
let (mut best_template_data, mut best_distance) = (None, u32::MAX);
for template in &mut self.templates {
if let Some((template_data, distance)) =
template.data_for_approximate_descriptor(fctx, desc) {
if distance < best_distance {
best_template_data = Some(template_data);
best_distance = distance
}
}
}
if best_template_data.is_some() {
return best_template_data
}
// If a request is made for a font family that exists,
// pick the first valid font in the family if we failed
// to find an exact match for the descriptor.
@ -81,8 +95,7 @@ impl FontTemplates {
}
}
let template = FontTemplate::new(identifier,
maybe_data);
let template = FontTemplate::new(identifier, maybe_data);
self.templates.push(template);
}
}