🎉 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

@ -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(