alexheretic / ab-glyph

Rust API for loading, scaling, positioning and rasterizing OpenType font glyphs
Apache License 2.0
372 stars 24 forks source link

Add Font::codepoint_ids #24

Closed alexheretic closed 4 years ago

alexheretic commented 4 years ago

Provide an abstraction for mapping GlyphId -> char.

pub trait Font {
    fn codepoint_ids(&self) -> crate::CodepointIdIter<'_>;
}
let font = FontRef::try_from_slice(include_bytes!("../../dev/fonts/Exo2-Light.otf"))?;

// Iterate over pairs, each id will appear at most once.
let mut codepoint_ids = font.codepoint_ids();
assert_eq!(codepoint_ids.next(), Some((GlyphId(408), '\r')));
assert_eq!(codepoint_ids.next(), Some((GlyphId(1), ' ')));
assert_eq!(codepoint_ids.next(), Some((GlyphId(75), '!')));

// Build a lookup map for all ids
let map: HashMap<_, _> = font.codepoint_ids().collect();
assert_eq!(map.get(&GlyphId(75)), Some(&'!'));

Resolves #18

robinkrahl commented 4 years ago

Looks good to me. Thanks for implementing this so quickly!