♻️ Adapt linear gradient type so it can be used for radial too (wasm)

This commit is contained in:
Belén Albeza 2025-04-14 16:38:43 +02:00
parent f40ef26c69
commit abcd050c69
6 changed files with 32 additions and 28 deletions

View file

@ -17,8 +17,8 @@ mod wasm;
use crate::mem::SerializableResult;
use crate::shapes::{
BoolType, ConstraintH, ConstraintV, StructureEntry, TransformEntry, Type,
RAW_LINEAR_FILL_DATA_SIZE, RAW_STOP_DATA_SIZE,
BoolType, ConstraintH, ConstraintV, StructureEntry, TransformEntry, Type, RAW_FILL_DATA_SIZE,
RAW_STOP_DATA_SIZE,
};
use crate::utils::uuid_from_u32_quartet;
use crate::uuid::Uuid;
@ -260,12 +260,11 @@ pub extern "C" fn add_shape_solid_fill(raw_color: u32) {
#[no_mangle]
pub extern "C" fn add_shape_linear_fill() {
with_current_shape!(state, |shape: &mut Shape| {
let stops_offset = RAW_LINEAR_FILL_DATA_SIZE;
let bytes = mem::bytes();
let raw_fill_data: [u8; RAW_LINEAR_FILL_DATA_SIZE] =
bytes[0..stops_offset].try_into().unwrap();
let raw_fill = shapes::RawLinearFillData::from(raw_fill_data);
let stops: Vec<shapes::RawStopData> = bytes[stops_offset..]
let raw_gradient_bytes: [u8; RAW_FILL_DATA_SIZE] =
bytes[0..RAW_FILL_DATA_SIZE].try_into().unwrap();
let raw_gradient = shapes::RawGradientData::from(raw_gradient_bytes);
let stops: Vec<shapes::RawStopData> = bytes[RAW_FILL_DATA_SIZE..]
.chunks(RAW_STOP_DATA_SIZE)
.map(|chunk| {
let data: [u8; RAW_STOP_DATA_SIZE] = chunk.try_into().unwrap();
@ -274,9 +273,9 @@ pub extern "C" fn add_shape_linear_fill() {
.collect();
shape.add_fill(shapes::Fill::new_linear_gradient_with_stops(
raw_fill.start(),
raw_fill.end(),
raw_fill.opacity(),
raw_gradient.start(),
raw_gradient.end(),
raw_gradient.opacity(),
stops,
));
});

View file

@ -20,7 +20,7 @@ pub extern "C" fn alloc_bytes(len: usize) -> *mut u8 {
if ptr.is_null() {
panic!("Allocation failed");
}
// TODO: Esto quizá se podría eliminar.
// TODO: Maybe this could be removed.
ptr::write_bytes(ptr, 0, len);
*guard = Some(Box::new(Vec::from_raw_parts(ptr, len, len)));
ptr

View file

@ -3,33 +3,33 @@ use skia_safe::{self as skia, Rect};
use super::Color;
use crate::uuid::Uuid;
pub const RAW_LINEAR_FILL_DATA_SIZE: usize = 21;
pub const RAW_FILL_DATA_SIZE: usize = 24;
#[derive(Debug)]
#[repr(C)]
pub struct RawLinearFillData {
pub struct RawGradientData {
start_x: f32,
start_y: f32,
end_x: f32,
end_y: f32,
opacity: f32,
stop_count: u8,
width: f32,
}
impl From<[u8; 21]> for RawLinearFillData {
fn from(bytes: [u8; 21]) -> Self {
impl From<[u8; RAW_FILL_DATA_SIZE]> for RawGradientData {
fn from(bytes: [u8; RAW_FILL_DATA_SIZE]) -> Self {
Self {
start_x: f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
start_y: f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
end_x: f32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]),
end_y: f32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]),
opacity: f32::from_le_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]),
stop_count: bytes[20],
width: f32::from_le_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]),
}
}
}
impl RawLinearFillData {
impl RawGradientData {
pub fn start(&self) -> (f32, f32) {
(self.start_x, self.start_y)
}