I want to build the following code as wasm(wasi) on runtime like wasmtime or lucet.
use font_kit::handle::Handle;
use font_kit::loaders::default::Font;
use std::sync::Arc;
use std::fs::File;
use std::io::Read;
fn main() {
// Load a font data
let mut font_data = File::open("./fonts/NotoSansJP-Bold.otf").unwrap();
let mut font_buf: Vec<u8> = Vec::new();
font_data.read_to_end(&mut font_buf).unwrap();
// Load a font.
let font_data = Arc::new(font_buf);
let font = Handle::from_memory(font_data, 0);
let font_context = Font::from_handle(&font).unwrap();
println!("{:?}", font_context.metrics());
let glyph_id = font_context.glyph_for_char('あ').unwrap();
let adv = font_context.advance(glyph_id).unwrap();
println!("{:?}", adv);
}
So I ran export PKG_CONFIG_ALLOW_CROSS=1 && cargo build wasm32-wasi on mac os.
However, freetype will print an error message saying unknown import: env::FT_Get_Char_Index has not been defined.
When I run font-kit using cargo run with font-kit = { version = "0.7", features = ["loader-freetype-default"] }, this works.
…unless you've patched pkg-config to use a custom sysroot specifically for the target platform, and prevented it from seeing macOS libraries. If you haven't set up a sysroot with wasm-compatible .pc files and wasm-specific libraries, it can only screw things up even worse than they already were. pkg-config is old and dumb and has zero support for cross-compilation.
In your case you need to either configure dependencies not to use freetype, or somehow build a wasm version of freetype, and teach pkg-config how to find it (I'm not sure if that is even possible).