Get list of valid chars from https://docs.rs/ttf-parser/0.12.0/ttf_parser/struct.Face.html

crate: ttf_parser::Face - Rust

After we load font "foobar.ttf", I want to get a list of all valid chars that this Font represents.

Here is what I have so far:

  1. I know we can construct valid GlyphId by running ttf-parser/font2svg.rs at master · RazrFalcon/ttf-parser · GitHub and calling ttf::GlyphId on it

  2. I also know that we have lib.rs - source which has type: pub fn glyph_index(&self, c: char) -> Option<GlyphId>

However, I need something that gives me either GlyphId -> char or Font -> Vec<char> -- I want way to enumerate all the "valid" chars of a font.

I think you can do something like this:

for table in face.character_mapping_subtables() {
    assert!(table.is_unicode(), "Assuming code points are Unicode chars.");
    table.codepoints(|codepoint| {
        let c = char::from_u32(codepoint).unwrap()
        if face.glyph_index(c).is_some() {
            // do stuff with `c`
        }
    })
}
1 Like