mirror of
https://github.com/penpot/penpot.git
synced 2025-07-19 01:28:54 +02:00
✨ Improve paths deserialization (wasm) (#6501)
* ♻️ Refactor path wasm code to its own wasm submodule * ♻️ Use unified enum for RawSegmentData and transmute to deserialize * ♻️ Move set_shape_path_attrs to wasm::paths module * 💄 Unify repr declarations
This commit is contained in:
parent
eaaca5629e
commit
f9bbf2d524
6 changed files with 197 additions and 125 deletions
|
@ -345,38 +345,6 @@ pub extern "C" fn set_shape_blur(blur_type: u8, hidden: bool, value: f32) {
|
|||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_shape_path_content() {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
let bytes = mem::bytes();
|
||||
let raw_segments = bytes
|
||||
.chunks(size_of::<shapes::RawPathData>())
|
||||
.map(|data| shapes::RawPathData {
|
||||
data: data.try_into().unwrap(),
|
||||
})
|
||||
.collect();
|
||||
shape.set_path_segments(raw_segments).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
// Extracts a string from the bytes slice until the next null byte (0) and returns the result as a `String`.
|
||||
// Updates the `start` index to the end of the extracted string.
|
||||
fn extract_string(start: &mut usize, bytes: &[u8]) -> String {
|
||||
match bytes[*start..].iter().position(|&b| b == 0) {
|
||||
Some(pos) => {
|
||||
let end = *start + pos;
|
||||
let slice = &bytes[*start..end];
|
||||
*start = end + 1; // Move the `start` pointer past the null byte
|
||||
// Call to unsafe function within an unsafe block
|
||||
unsafe { String::from_utf8_unchecked(slice.to_vec()) }
|
||||
}
|
||||
None => {
|
||||
*start = bytes.len(); // Move `start` to the end if no null byte is found
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_shape_corners(r1: f32, r2: f32, r3: f32, r4: f32) {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
|
@ -384,19 +352,6 @@ pub extern "C" fn set_shape_corners(r1: f32, r2: f32, r3: f32, r4: f32) {
|
|||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_shape_path_attrs(num_attrs: u32) {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
let bytes = mem::bytes();
|
||||
let mut start = 0;
|
||||
for _ in 0..num_attrs {
|
||||
let name = extract_string(&mut start, &bytes);
|
||||
let value = extract_string(&mut start, &bytes);
|
||||
shape.set_path_attr(name, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn propagate_modifiers(pixel_precision: bool) -> *mut u8 {
|
||||
let bytes = mem::bytes();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue