Auto merge of #11565 - nox:fonts, r=metajack

Introduce FontFaceRules::effective_sources()

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11565)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-06-07 03:50:18 -05:00
commit b64b21ace0
2 changed files with 45 additions and 9 deletions

View file

@ -6,6 +6,8 @@ use computed_values::font_family::FontFamily;
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser};
use parser::{ParserContext, log_css_error};
use properties::longhands::font_family::parse_one_family;
use std::iter;
use std::slice;
use url::Url;
#[derive(Clone, Debug, HeapSizeOf, PartialEq, Eq, Deserialize, Serialize)]
@ -58,6 +60,36 @@ pub fn parse_font_face_block(context: &ParserContext, input: &mut Parser)
}
}
pub struct EffectiveSourcesIter<'a>(slice::Iter<'a, Source>);
impl FontFaceRule {
/// Returns the list of effective sources for that font-face, that is the
/// sources which don't list any format hint, or the ones which list at
/// least "truetype" or "opentype".
pub fn effective_sources(&self) -> EffectiveSourcesIter {
EffectiveSourcesIter(self.sources.iter())
}
}
impl<'a> iter::Iterator for EffectiveSourcesIter<'a> {
type Item = &'a Source;
fn next(&mut self) -> Option<&'a Source> {
self.0.find(|source| {
if let Source::Url(ref url_source) = **source {
let hints = &url_source.format_hints;
// We support only opentype fonts and truetype is an alias for
// that format. Sources without format hints need to be
// downloaded in case we support them.
hints.is_empty() || hints.iter().any(|hint| {
hint == "truetype" || hint == "opentype" || hint == "woff"
})
} else {
true
}
})
}
}
enum FontFaceDescriptorDeclaration {
Family(FontFamily),
Src(Vec<Source>),