diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-03-30 20:51:09 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-03-30 20:58:46 +0100 |
| commit | 5ca303ecadff190800dd55a5a5ae224dc28a3920 (patch) | |
| tree | a49504b831981de1041a3f7f0ca44a3892f14ef1 /src/font.rs | |
| parent | adfd7dd0735d8a4efeac634c53bbf386e91a4c41 (diff) | |
Make things more consistent ♻
Diffstat (limited to 'src/font.rs')
| -rw-r--r-- | src/font.rs | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/src/font.rs b/src/font.rs index 298f0ffd..c88d4578 100644 --- a/src/font.rs +++ b/src/font.rs @@ -143,8 +143,8 @@ impl Font { loca: None, glyphs: Vec::with_capacity(chars.len()), chars, - records: Vec::new(), - body: Vec::new(), + records: vec![], + body: vec![], }; subsetter.subset(needed_tables, optional_tables) @@ -152,7 +152,7 @@ impl Font { } /// Font metrics relevant to the typesetting engine. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct FontMetrics { /// Whether the font is italic. pub is_italic: bool, @@ -275,10 +275,10 @@ macro_rules! font_info { } /// Criteria to filter fonts. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct FontFilter<'a> { - /// A fallback list of font families we accept. The first family in this list, that also - /// satisfies the other conditions shall be returned. + /// A fallback list of font families to accept. The first family in this list, that also + /// satisfies the other conditions, shall be returned. pub families: &'a [FontFamily], /// If some, matches only italic/non-italic fonts, otherwise any. pub italic: Option<bool>, @@ -298,13 +298,6 @@ impl<'a> FontFilter<'a> { } } - /// Whether this filter matches the given info. - pub fn matches(&self, info: &FontInfo) -> bool { - self.italic.map(|i| i == info.italic).unwrap_or(true) - && self.bold.map(|i| i == info.bold).unwrap_or(true) - && self.families.iter().any(|family| info.families.contains(family)) - } - /// Set the italic value to something. pub fn italic(&mut self, italic: bool) -> &mut Self { self.italic = Some(italic); self @@ -314,6 +307,13 @@ impl<'a> FontFilter<'a> { pub fn bold(&mut self, bold: bool) -> &mut Self { self.bold = Some(bold); self } + + /// Whether this filter matches the given info. + pub fn matches(&self, info: &FontInfo) -> bool { + self.italic.map(|i| i == info.italic).unwrap_or(true) + && self.bold.map(|i| i == info.bold).unwrap_or(true) + && self.families.iter().any(|family| info.families.contains(family)) + } } /// A family of fonts (either generic or named). @@ -326,6 +326,7 @@ pub enum FontFamily { } /// A font provider serving fonts from a folder on the local file system. +#[derive(Debug)] pub struct FileSystemFontProvider { base: PathBuf, paths: Vec<PathBuf>, @@ -346,29 +347,36 @@ impl FileSystemFontProvider { /// ("NotoSans-Italic.ttf", font_info!(["NotoSans", SansSerif], italic)), /// ]); /// ``` + #[inline] pub fn new<B, I, P>(base: B, infos: I) -> FileSystemFontProvider where B: Into<PathBuf>, I: IntoIterator<Item = (P, FontInfo)>, P: Into<PathBuf>, { - let mut paths = Vec::new(); - let mut font_infos = Vec::new(); - - for (path, info) in infos.into_iter() { + // Find out how long the iterator is at least, to reserve the correct + // capacity for the vectors. + let iter = infos.into_iter(); + let min = iter.size_hint().0; + + // Split the iterator into two seperated vectors. + let mut paths = Vec::with_capacity(min); + let mut infos = Vec::with_capacity(min); + for (path, info) in iter { paths.push(path.into()); - font_infos.push(info); + infos.push(info); } FileSystemFontProvider { base: base.into(), paths, - infos: font_infos, + infos, } } } impl FontProvider for FileSystemFontProvider { + #[inline] fn get(&self, info: &FontInfo) -> Option<Box<dyn FontData>> { let index = self.infos.iter().position(|i| i == info)?; let path = &self.paths[index]; @@ -376,16 +384,17 @@ impl FontProvider for FileSystemFontProvider { Some(Box::new(file) as Box<FontData>) } + #[inline] fn available<'a>(&'a self) -> &'a [FontInfo] { &self.infos } } #[derive(Debug)] -struct Subsetter<'p> { +struct Subsetter<'d> { // Original font - font: &'p Font, - reader: OpenTypeReader<Cursor<&'p [u8]>>, + font: &'d Font, + reader: OpenTypeReader<Cursor<&'d [u8]>>, outlines: Outlines, tables: Vec<TableRecord>, cmap: Option<CharMap>, @@ -399,7 +408,7 @@ struct Subsetter<'p> { body: Vec<u8>, } -impl<'p> Subsetter<'p> { +impl<'d> Subsetter<'d> { fn subset<I1, S1, I2, S2>(mut self, needed_tables: I1, optional_tables: I2) -> FontResult<Font> where @@ -726,7 +735,7 @@ impl<'p> Subsetter<'p> { })) } - fn get_table_data(&self, tag: Tag) -> FontResult<&'p [u8]> { + fn get_table_data(&self, tag: Tag) -> FontResult<&'d [u8]> { let record = match self.tables.binary_search_by_key(&tag, |r| r.tag) { Ok(index) => &self.tables[index], Err(_) => return Err(FontError::MissingTable(tag.to_string())), |
