RazrFalcon / ttf-parser

A high-level, safe, zero-allocation TrueType font parser.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some fonts don't return a name because of encoding

MoAlyousef opened this issue · comments

Hello
Thanks for this library. I noticed that some fonts, namely Octicons.ttf in addition to font awesome don't return a name, or more specifically return an "unsupported encoding".

I've tried to modify ttf-parser to allow for lossy utf8 encoding and it seems to work:

// src/tables/name.rs
    pub fn to_string(&self) -> Option<String> {
        if self.is_unicode() {
            self.name_from_utf16_be()
        } else {
            use std::string::ToString;
            Some(String::from_utf8_lossy(self.name).to_string())
        }
    }

Which appears to work on both Linux (wsl) and windows.

What code do you use to get a name? This one works just fine:

    let family_name = face.names().into_iter()
        .find(|name| name.name_id == ttf_parser::name_id::FULL_NAME && name.is_unicode())
        .and_then(|name| name.to_string());

    let post_script_name = face.names().into_iter()
        .find(|name| name.name_id == ttf_parser::name_id::POST_SCRIPT_NAME && name.is_unicode())
        .and_then(|name| name.to_string());

PS: The font-info example should be corrected...

Ah. Yeah I used the code from the font-info example. I'll close this issue.
Thanks for the prompt response.