Address review feedback

* reorganize GlyphCache::get_or_insert() to use the HashMap entry API
* change DrawGlyphs::hint() parameter name from `yes` to `hint` for clarity
* change Layout::path_data() to return bytes instead of [f32; 2]. Path segments could be encoded as i16, making this incorrect
This commit is contained in:
Chad Brokaw 2023-03-06 18:29:43 -05:00
parent 69dd838d09
commit 86b1a66af0
3 changed files with 18 additions and 15 deletions

View file

@ -48,21 +48,24 @@ impl GlyphCache {
style: &Style, style: &Style,
scaler: &mut GlyphProvider, scaler: &mut GlyphProvider,
) -> Option<CachedRange> { ) -> Option<CachedRange> {
let encoding_cache = &mut self.encoding;
let mut encode_glyph = || {
let start = encoding_cache.stream_offsets();
scaler.encode_glyph(key.glyph_id as u16, style, encoding_cache)?;
let end = encoding_cache.stream_offsets();
Some(CachedRange { start, end })
};
// For now, only cache non-zero filled glyphs so we don't need to keep style // For now, only cache non-zero filled glyphs so we don't need to keep style
// as part of the key. // as part of the key.
let is_nz_fill = matches!(style, Style::Fill(Fill::NonZero)); let range = if matches!(style, Style::Fill(Fill::NonZero)) {
if is_nz_fill { use std::collections::hash_map::Entry;
if let Some(range) = self.glyphs.get(&key) { match self.glyphs.entry(key) {
return Some(*range); Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => *entry.insert(encode_glyph()?),
} }
} } else {
let start = self.encoding.stream_offsets(); encode_glyph()?
scaler.encode_glyph(key.glyph_id as u16, style, &mut self.encoding)?; };
let end = self.encoding.stream_offsets();
let range = CachedRange { start, end };
if is_nz_fill {
self.glyphs.insert(key, range);
}
Some(range) Some(range)
} }
} }

View file

@ -74,7 +74,7 @@ impl Layout {
} }
/// Returns the path data stream. /// Returns the path data stream.
pub fn path_data<'a>(&self, data: &'a [u8]) -> &'a [[f32; 2]] { pub fn path_data<'a>(&self, data: &'a [u8]) -> &'a [u8] {
let start = self.path_data_base as usize * 4; let start = self.path_data_base as usize * 4;
let end = self.draw_tag_base as usize * 4; let end = self.draw_tag_base as usize * 4;
bytemuck::cast_slice(&data[start..end]) bytemuck::cast_slice(&data[start..end])

View file

@ -245,8 +245,8 @@ impl<'a> DrawGlyphs<'a> {
/// Sets whether to enable hinting. /// Sets whether to enable hinting.
/// ///
/// The default value is `false`. /// The default value is `false`.
pub fn hint(mut self, yes: bool) -> Self { pub fn hint(mut self, hint: bool) -> Self {
self.run.hint = yes; self.run.hint = hint;
self self
} }