🎉 Basic wasm fonts support

This commit is contained in:
Alejandro Alonso 2024-12-26 13:13:17 +01:00 committed by AzazelN28
parent 6335a183ba
commit 751df46dc9
4 changed files with 67 additions and 1 deletions

View file

@ -286,6 +286,27 @@ pub extern "C" fn add_shape_fill_stops(ptr: *mut shapes::RawStopData, n_stops: u
}
}
#[no_mangle]
pub extern "C" fn store_font(family_name_size: u32, font_size: u32) {
let state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
unsafe {
let font_bytes =
Vec::<u8>::from_raw_parts(mem::buffer_ptr(), font_size as usize, font_size as usize);
let family_name = String::from_raw_parts(
mem::buffer_ptr().add(font_size as usize),
family_name_size as usize,
family_name_size as usize,
);
match state.render_state().add_font(family_name, &font_bytes) {
Err(msg) => {
eprintln!("{}", msg);
}
_ => {}
}
mem::free_bytes();
}
}
#[no_mangle]
pub extern "C" fn store_image(a: u32, b: u32, c: u32, d: u32, size: u32) {
let state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");

View file

@ -24,6 +24,7 @@ pub trait Renderable {
surface: &mut skia::Surface,
images: &ImageStore,
scale: f32,
font_provider: &skia::textlayout::TypefaceFontProvider,
) -> Result<(), String>;
fn blend_mode(&self) -> BlendMode;
fn opacity(&self) -> f32;
@ -55,6 +56,7 @@ pub(crate) struct RenderState {
pub final_surface: skia::Surface,
pub drawing_surface: skia::Surface,
pub debug_surface: skia::Surface,
pub font_provider: skia::textlayout::TypefaceFontProvider,
pub cached_surface_image: Option<CachedSurfaceImage>,
options: RenderOptions,
pub viewbox: Viewbox,
@ -74,12 +76,19 @@ impl RenderState {
.new_surface_with_dimensions((width, height))
.unwrap();
let mut font_provider = skia::textlayout::TypefaceFontProvider::new();
let default_font = skia::FontMgr::default()
.new_from_data(include_bytes!("fonts/RobotoMono-Regular.ttf"), None)
.expect("Failed to load font");
font_provider.register_typeface(default_font, "robotomono-regular");
RenderState {
gpu_state,
final_surface,
drawing_surface,
debug_surface,
cached_surface_image: None,
font_provider,
options: RenderOptions::default(),
viewbox: Viewbox::new(width as f32, height as f32),
images: ImageStore::new(),
@ -87,6 +96,15 @@ impl RenderState {
}
}
pub fn add_font(&mut self, family_name: String, font_data: &[u8]) -> Result<(), String> {
let typeface = skia::FontMgr::default()
.new_from_data(font_data, None)
.expect("Failed to add font");
self.font_provider
.register_typeface(typeface, family_name.as_ref());
Ok(())
}
pub fn add_image(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> {
self.images.add(id, image_data)
}
@ -164,7 +182,7 @@ impl RenderState {
pub fn render_single_element(&mut self, element: &impl Renderable) {
let scale = self.viewbox.zoom * self.options.dpr();
element
.render(&mut self.drawing_surface, &self.images, scale)
.render(&mut self.drawing_surface, &self.images, scale, &self.font_provider)
.unwrap();
self.drawing_surface.draw(

View file

@ -11,6 +11,7 @@ impl Renderable for Shape {
surface: &mut skia_safe::Surface,
images: &ImageStore,
scale: f32,
font_provider: &skia::textlayout::TypefaceFontProvider,
) -> Result<(), String> {
let transform = self.transform.to_skia_matrix();