mirror of
https://github.com/penpot/penpot.git
synced 2025-05-31 22:21:37 +02:00
✨ Use same wasm function to add all types of fills
This commit is contained in:
parent
784aecd1a1
commit
cba65972dd
7 changed files with 89 additions and 163 deletions
|
@ -11,7 +11,7 @@ use crate::STATE;
|
|||
#[repr(align(4))]
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
enum RawFillData {
|
||||
pub enum RawFillData {
|
||||
Solid(solid::RawSolidData) = 0x00,
|
||||
Linear(gradient::RawGradientData) = 0x01,
|
||||
Radial(gradient::RawGradientData) = 0x02,
|
||||
|
@ -43,16 +43,16 @@ impl TryFrom<&[u8]> for RawFillData {
|
|||
let fill_type = bytes[0];
|
||||
match fill_type {
|
||||
0x00 => Ok(RawFillData::Solid(solid::RawSolidData::try_from(
|
||||
&bytes[1..],
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
0x01 => Ok(RawFillData::Linear(gradient::RawGradientData::try_from(
|
||||
&bytes[1..],
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
0x02 => Ok(RawFillData::Radial(gradient::RawGradientData::try_from(
|
||||
&bytes[1..],
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
0x03 => Ok(RawFillData::Image(image::RawImageFillData::try_from(
|
||||
&bytes[1..],
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
_ => Err("Invalid fill type".to_string()),
|
||||
}
|
||||
|
@ -68,45 +68,6 @@ pub extern "C" fn add_shape_fill() {
|
|||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_shape_solid_fill() {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
let bytes = mem::bytes();
|
||||
let solid_color =
|
||||
shapes::SolidColor::try_from(&bytes[..]).expect("Invalid solid color data");
|
||||
|
||||
shape.add_fill(shapes::Fill::Solid(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() {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
let bytes = mem::bytes();
|
||||
let image_fill = shapes::ImageFill::try_from(&bytes[..]).expect("Invalid image fill data");
|
||||
|
||||
shape.add_fill(shapes::Fill::Image(image_fill));
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn clear_shape_fills() {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
|
|
|
@ -48,8 +48,9 @@ impl TryFrom<&[u8]> for RawGradientData {
|
|||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; RAW_GRADIENT_DATA_SIZE] = bytes
|
||||
.try_into()
|
||||
.map_err(|_| "Invalid gradient data".to_string())?;
|
||||
.get(0..RAW_GRADIENT_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid gradient fill data".to_string())?;
|
||||
Ok(RawGradientData::from(data))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,8 +50,9 @@ impl TryFrom<&[u8]> for RawImageFillData {
|
|||
|
||||
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; RAW_IMAGE_DATA_SIZE] = value
|
||||
.try_into()
|
||||
.map_err(|_| "Invalid image fill data".to_string())?;
|
||||
.get(0..RAW_IMAGE_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid image fill data".to_string())?;
|
||||
Ok(Self::from(data))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use crate::shapes::{Color, SolidColor};
|
||||
|
||||
const RAW_SOLID_DATA_SIZE: usize = 4;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub struct RawSolidData {
|
||||
|
@ -7,7 +9,7 @@ pub struct RawSolidData {
|
|||
}
|
||||
|
||||
impl From<[u8; 4]> for RawSolidData {
|
||||
fn from(value: [u8; 4]) -> Self {
|
||||
fn from(value: [u8; RAW_SOLID_DATA_SIZE]) -> Self {
|
||||
Self {
|
||||
color: u32::from_le_bytes(value),
|
||||
}
|
||||
|
@ -18,8 +20,8 @@ impl TryFrom<&[u8]> for RawSolidData {
|
|||
type Error = String;
|
||||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; 4] = bytes
|
||||
.get(0..4)
|
||||
let data: [u8; RAW_SOLID_DATA_SIZE] = bytes
|
||||
.get(0..RAW_SOLID_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid solid fill data".to_string())?;
|
||||
Ok(RawSolidData::from(data))
|
||||
|
|
|
@ -31,50 +31,13 @@ pub extern "C" fn add_shape_outer_stroke(width: f32, style: u8, cap_start: u8, c
|
|||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_shape_stroke_solid_fill() {
|
||||
pub extern "C" fn add_shape_stroke_fill() {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
let bytes = mem::bytes();
|
||||
let solid_color =
|
||||
shapes::SolidColor::try_from(&bytes[..]).expect("Invalid solid color data");
|
||||
let raw_fill = super::fills::RawFillData::try_from(&bytes[..]).expect("Invalid fill data");
|
||||
shape
|
||||
.set_stroke_fill(shapes::Fill::Solid(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() {
|
||||
with_current_shape!(state, |shape: &mut Shape| {
|
||||
let bytes = mem::bytes();
|
||||
let image_fill = shapes::ImageFill::try_from(&bytes[..]).expect("Invalid image fill data");
|
||||
|
||||
shape
|
||||
.set_stroke_fill(shapes::Fill::Image(image_fill))
|
||||
.expect("could not add stroke image fill");
|
||||
.set_stroke_fill(raw_fill.into())
|
||||
.expect("could not add stroke fill");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue