♻️ Extract wasm-functions for fills and strokes out of main.rs

This commit is contained in:
Belén Albeza 2025-04-15 12:43:25 +02:00
parent 64a2a08d24
commit f500a00d04
4 changed files with 159 additions and 143 deletions

View file

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

View file

@ -1,2 +1,4 @@
pub mod fills;
pub mod fonts;
pub mod strokes;
pub mod text;

View file

@ -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();
});
}

View file

@ -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();
});
}