From f500a00d0401bc1135fb064605806a07a37cabeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Tue, 15 Apr 2025 12:43:25 +0200 Subject: [PATCH] :recycle: Extract wasm-functions for fills and strokes out of main.rs --- render-wasm/src/main.rs | 143 -------------------------------- render-wasm/src/wasm.rs | 2 + render-wasm/src/wasm/fills.rs | 60 ++++++++++++++ render-wasm/src/wasm/strokes.rs | 97 ++++++++++++++++++++++ 4 files changed, 159 insertions(+), 143 deletions(-) create mode 100644 render-wasm/src/wasm/fills.rs create mode 100644 render-wasm/src/wasm/strokes.rs diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 3e09e3d7d..3ea0e1a6a 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -246,32 +246,6 @@ pub extern "C" fn set_children() { } } -#[no_mangle] -pub extern "C" fn add_shape_solid_fill(raw_color: u32) { - with_current_shape!(state, |shape: &mut Shape| { - let color = skia::Color::new(raw_color); - shape.add_fill(shapes::Fill::Solid(color)); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_linear_fill() { - with_current_shape!(state, |shape: &mut Shape| { - let bytes = mem::bytes(); - let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); - shape.add_fill(shapes::Fill::LinearGradient(gradient)); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_radial_fill() { - with_current_shape!(state, |shape: &mut Shape| { - let bytes = mem::bytes(); - let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); - shape.add_fill(shapes::Fill::RadialGradient(gradient)); - }); -} - #[no_mangle] pub extern "C" fn store_image(a: u32, b: u32, c: u32, d: u32) { with_state!(state, { @@ -297,33 +271,6 @@ pub extern "C" fn is_image_cached(a: u32, b: u32, c: u32, d: u32) -> bool { }); } -#[no_mangle] -pub extern "C" fn add_shape_image_fill( - a: u32, - b: u32, - c: u32, - d: u32, - alpha: f32, - width: i32, - height: i32, -) { - with_current_shape!(state, |shape: &mut Shape| { - let id = uuid_from_u32_quartet(a, b, c, d); - shape.add_fill(shapes::Fill::new_image_fill( - id, - (alpha * 0xff as f32).floor() as u8, - (width, height), - )); - }); -} - -#[no_mangle] -pub extern "C" fn clear_shape_fills() { - with_current_shape!(state, |shape: &mut Shape| { - shape.clear_fills(); - }); -} - #[no_mangle] pub extern "C" fn set_shape_svg_raw_content() { with_current_shape!(state, |shape: &mut Shape| { @@ -394,67 +341,6 @@ pub extern "C" fn set_shape_path_content() { }); } -#[no_mangle] -pub extern "C" fn add_shape_center_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { - with_current_shape!(state, |shape: &mut Shape| { - shape.add_stroke(shapes::Stroke::new_center_stroke( - width, style, cap_start, cap_end, - )); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_inner_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { - with_current_shape!(state, |shape: &mut Shape| { - shape.add_stroke(shapes::Stroke::new_inner_stroke( - width, style, cap_start, cap_end, - )); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_outer_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { - with_current_shape!(state, |shape: &mut Shape| { - shape.add_stroke(shapes::Stroke::new_outer_stroke( - width, style, cap_start, cap_end, - )); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_stroke_solid_fill(raw_color: u32) { - with_current_shape!(state, |shape: &mut Shape| { - let color = skia::Color::new(raw_color); - shape - .set_stroke_fill(shapes::Fill::Solid(color)) - .expect("could not add stroke solid fill"); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_stroke_linear_fill() { - with_current_shape!(state, |shape: &mut Shape| { - let bytes = mem::bytes(); - let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); - - shape - .set_stroke_fill(shapes::Fill::LinearGradient(gradient)) - .expect("could not add stroke linear gradient fill"); - }); -} - -#[no_mangle] -pub extern "C" fn add_shape_stroke_radial_fill() { - with_current_shape!(state, |shape: &mut Shape| { - let bytes = mem::bytes(); - let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); - - shape - .set_stroke_fill(shapes::Fill::RadialGradient(gradient)) - .expect("could not add stroke radial gradient fill"); - }); -} - // 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 { @@ -473,35 +359,6 @@ fn extract_string(start: &mut usize, bytes: &[u8]) -> String { } } -#[no_mangle] -pub extern "C" fn add_shape_image_stroke( - a: u32, - b: u32, - c: u32, - d: u32, - alpha: f32, - width: i32, - height: i32, -) { - with_current_shape!(state, |shape: &mut Shape| { - let id = uuid_from_u32_quartet(a, b, c, d); - shape - .set_stroke_fill(shapes::Fill::new_image_fill( - id, - (alpha * 0xff as f32).floor() as u8, - (width, height), - )) - .expect("could not add stroke image fill"); - }); -} - -#[no_mangle] -pub extern "C" fn clear_shape_strokes() { - with_current_shape!(state, |shape: &mut Shape| { - shape.clear_strokes(); - }); -} - #[no_mangle] pub extern "C" fn set_shape_corners(r1: f32, r2: f32, r3: f32, r4: f32) { with_current_shape!(state, |shape: &mut Shape| { diff --git a/render-wasm/src/wasm.rs b/render-wasm/src/wasm.rs index 31ae8a7c1..7d45923d9 100644 --- a/render-wasm/src/wasm.rs +++ b/render-wasm/src/wasm.rs @@ -1,2 +1,4 @@ +pub mod fills; pub mod fonts; +pub mod strokes; pub mod text; diff --git a/render-wasm/src/wasm/fills.rs b/render-wasm/src/wasm/fills.rs new file mode 100644 index 000000000..a6c47fdf1 --- /dev/null +++ b/render-wasm/src/wasm/fills.rs @@ -0,0 +1,60 @@ +use skia_safe as skia; + +use crate::mem; +use crate::shapes; +use crate::utils::uuid_from_u32_quartet; +use crate::with_current_shape; +use crate::STATE; + +#[no_mangle] +pub extern "C" fn add_shape_solid_fill(raw_color: u32) { + with_current_shape!(state, |shape: &mut Shape| { + let color = skia::Color::new(raw_color); + shape.add_fill(shapes::Fill::Solid(color)); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_linear_fill() { + with_current_shape!(state, |shape: &mut Shape| { + let bytes = mem::bytes(); + let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); + shape.add_fill(shapes::Fill::LinearGradient(gradient)); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_radial_fill() { + with_current_shape!(state, |shape: &mut Shape| { + let bytes = mem::bytes(); + let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); + shape.add_fill(shapes::Fill::RadialGradient(gradient)); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_image_fill( + a: u32, + b: u32, + c: u32, + d: u32, + alpha: f32, + width: i32, + height: i32, +) { + with_current_shape!(state, |shape: &mut Shape| { + let id = uuid_from_u32_quartet(a, b, c, d); + shape.add_fill(shapes::Fill::new_image_fill( + id, + (alpha * 0xff as f32).floor() as u8, + (width, height), + )); + }); +} + +#[no_mangle] +pub extern "C" fn clear_shape_fills() { + with_current_shape!(state, |shape: &mut Shape| { + shape.clear_fills(); + }); +} diff --git a/render-wasm/src/wasm/strokes.rs b/render-wasm/src/wasm/strokes.rs new file mode 100644 index 000000000..5aea834cf --- /dev/null +++ b/render-wasm/src/wasm/strokes.rs @@ -0,0 +1,97 @@ +use skia_safe as skia; + +use crate::mem; +use crate::shapes; +use crate::utils::uuid_from_u32_quartet; +use crate::with_current_shape; +use crate::STATE; + +#[no_mangle] +pub extern "C" fn add_shape_center_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { + with_current_shape!(state, |shape: &mut Shape| { + shape.add_stroke(shapes::Stroke::new_center_stroke( + width, style, cap_start, cap_end, + )); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_inner_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { + with_current_shape!(state, |shape: &mut Shape| { + shape.add_stroke(shapes::Stroke::new_inner_stroke( + width, style, cap_start, cap_end, + )); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_outer_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { + with_current_shape!(state, |shape: &mut Shape| { + shape.add_stroke(shapes::Stroke::new_outer_stroke( + width, style, cap_start, cap_end, + )); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_stroke_solid_fill(raw_color: u32) { + with_current_shape!(state, |shape: &mut Shape| { + let color = skia::Color::new(raw_color); + shape + .set_stroke_fill(shapes::Fill::Solid(color)) + .expect("could not add stroke solid fill"); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_stroke_linear_fill() { + with_current_shape!(state, |shape: &mut Shape| { + let bytes = mem::bytes(); + let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); + + shape + .set_stroke_fill(shapes::Fill::LinearGradient(gradient)) + .expect("could not add stroke linear gradient fill"); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_stroke_radial_fill() { + with_current_shape!(state, |shape: &mut Shape| { + let bytes = mem::bytes(); + let gradient = shapes::Gradient::try_from(&bytes[..]).expect("Invalid gradient data"); + + shape + .set_stroke_fill(shapes::Fill::RadialGradient(gradient)) + .expect("could not add stroke radial gradient fill"); + }); +} + +#[no_mangle] +pub extern "C" fn add_shape_image_stroke( + a: u32, + b: u32, + c: u32, + d: u32, + alpha: f32, + width: i32, + height: i32, +) { + with_current_shape!(state, |shape: &mut Shape| { + let id = uuid_from_u32_quartet(a, b, c, d); + shape + .set_stroke_fill(shapes::Fill::new_image_fill( + id, + (alpha * 0xff as f32).floor() as u8, + (width, height), + )) + .expect("could not add stroke image fill"); + }); +} + +#[no_mangle] +pub extern "C" fn clear_shape_strokes() { + with_current_shape!(state, |shape: &mut Shape| { + shape.clear_strokes(); + }); +}