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 https://github.com/RazrFalcon/ttf-parser/blob/master/examples/font2svg.rs#L116 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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.