Font for Wasm image manipuation

I want to build a wasm program that can do image manipulation with create image.

However, manipulate includes writing text on image, and that would need a font.
In a PC, I can do this:

let font_data: &[u8] = include_bytes!(r"./Lato-Bold.ttf");
    let font = Font::try_from_bytes(font_data).unwrap();

Unfortunately, that would not work with wasm.
So what are my best approaches?
(Like can include_bytes! load from url, i.e. a statically served Lato-Bold.ttf file?)
Thank you

The include_bytes!() macro should still work for WebAssembly - all it does is embed a file's contents into your program as a constant at compile time. If you want to use a font that is only known at runtime, then you'll need to set things up so the font is downloaded and passed to your crate at runtime.

If this is running in the browser, you could use fetch() to download the font into an ArrayBuffer, then convert to a Uint8Array and call to_vec() to get the font file as a Vec<u8>.

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.