🎉 Store custom fonts (ttfs) and use them to write texts (wasm) (#6050)

This commit is contained in:
Belén Albeza 2025-03-14 12:45:15 +01:00 committed by GitHub
parent e4c9b736f7
commit eb6d2fb0eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 386 additions and 140 deletions

View file

@ -0,0 +1,35 @@
use crate::mem;
use crate::utils::uuid_from_u32_quartet;
use crate::with_state;
use crate::STATE;
use crate::shapes::FontFamily;
#[no_mangle]
pub extern "C" fn store_font(a: u32, b: u32, c: u32, d: u32, weight: u32, style: u8) {
with_state!(state, {
let id = uuid_from_u32_quartet(a, b, c, d);
let font_bytes = mem::bytes();
let family = FontFamily::new(id, weight, style.into());
let res = state.render_state().fonts_mut().add(family, &font_bytes);
match res {
Err(msg) => {
eprintln!("{}", msg);
}
_ => {}
}
});
}
#[no_mangle]
pub extern "C" fn is_font_uploaded(a: u32, b: u32, c: u32, d: u32, weight: u32, style: u8) -> bool {
with_state!(state, {
let id = uuid_from_u32_quartet(a, b, c, d);
let family = FontFamily::new(id, weight, style.into());
let res = state.render_state().fonts().has_family(&family);
return res;
});
}

View file

@ -1,4 +1,6 @@
use crate::mem;
use crate::shapes::FontFamily;
use crate::utils::uuid_from_u32_quartet;
use crate::with_current_shape;
use crate::STATE;
@ -20,14 +22,25 @@ pub extern "C" fn add_text_paragraph() {
}
#[no_mangle]
pub extern "C" fn add_text_leaf() {
pub extern "C" fn add_text_leaf(
a: u32,
b: u32,
c: u32,
d: u32,
weight: u32,
style: u8,
font_size: f32,
) {
let font_id = uuid_from_u32_quartet(a, b, c, d);
let font_family = FontFamily::new(font_id, weight, style.into());
let bytes = mem::bytes();
let text = unsafe {
String::from_utf8_unchecked(bytes) // TODO: handle this error
};
with_current_shape!(state, |shape: &mut Shape| {
let res = shape.add_text_leaf(&text);
let res = shape.add_text_leaf(text, font_family, font_size);
if let Err(err) = res {
eprintln!("{}", err);
}