mirror of
https://github.com/penpot/penpot.git
synced 2025-06-14 17:11:37 +02:00
🎉 Store custom fonts (ttfs) and use them to write texts (wasm) (#6050)
This commit is contained in:
parent
e4c9b736f7
commit
eb6d2fb0eb
14 changed files with 386 additions and 140 deletions
35
render-wasm/src/wasm/fonts.rs
Normal file
35
render-wasm/src/wasm/fonts.rs
Normal 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;
|
||||
});
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue