mirror of
https://github.com/penpot/penpot.git
synced 2025-05-18 08:26:11 +02:00
♻️ Refactor adding gradient stops
This commit is contained in:
parent
5e9f533624
commit
7105e49ac2
3 changed files with 32 additions and 26 deletions
|
@ -1,6 +1,6 @@
|
||||||
mod debug;
|
mod debug;
|
||||||
mod math;
|
mod math;
|
||||||
pub mod mem;
|
mod mem;
|
||||||
mod render;
|
mod render;
|
||||||
mod shapes;
|
mod shapes;
|
||||||
mod state;
|
mod state;
|
||||||
|
@ -177,33 +177,20 @@ pub extern "C" fn add_shape_linear_fill(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct RawStopData {
|
|
||||||
color: [u8; 4],
|
|
||||||
offset: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_fill_stops(ptr: *mut RawStopData, n_stops: i32) {
|
pub extern "C" fn add_shape_fill_stops(ptr: *mut shapes::RawStopData, n_stops: u32) {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||||
|
|
||||||
if let Some(shape) = state.current_shape() {
|
if let Some(shape) = state.current_shape() {
|
||||||
|
let len = n_stops as usize;
|
||||||
|
let buffer_size = std::mem::size_of::<shapes::RawStopData>() * len;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let buf = Vec::<RawStopData>::from_raw_parts(ptr, n_stops as usize, n_stops as usize);
|
let buffer = Vec::<shapes::RawStopData>::from_raw_parts(ptr, len, len);
|
||||||
for raw_stop in buf.iter() {
|
shape
|
||||||
let color = skia::Color::from_argb(
|
.add_gradient_stops(buffer)
|
||||||
raw_stop.color[3],
|
.expect("could not add gradient stops");
|
||||||
raw_stop.color[0],
|
mem::free(ptr as *mut u8, buffer_size);
|
||||||
raw_stop.color[1],
|
|
||||||
raw_stop.color[2],
|
|
||||||
);
|
|
||||||
shape
|
|
||||||
.add_gradient_stop(color, (raw_stop.offset as f32) / 100.)
|
|
||||||
.expect("got no fill or an invalid one");
|
|
||||||
}
|
|
||||||
mem::free(
|
|
||||||
ptr as *mut u8,
|
|
||||||
n_stops as usize * std::mem::size_of::<RawStopData>(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,14 +92,16 @@ impl Shape {
|
||||||
self.fills.clear();
|
self.fills.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_gradient_stop(&mut self, color: skia::Color, offset: f32) -> Result<(), String> {
|
pub fn add_gradient_stops(&mut self, buffer: Vec<RawStopData>) -> Result<(), String> {
|
||||||
let fill = self.fills.last_mut().ok_or("Shape has no fills")?;
|
let fill = self.fills.last_mut().ok_or("Shape has no fills")?;
|
||||||
let gradient = match fill {
|
let gradient = match fill {
|
||||||
Fill::LinearGradient(g) => Ok(g),
|
Fill::LinearGradient(g) => Ok(g),
|
||||||
_ => Err("Active fill is not a gradient"),
|
_ => Err("Active fill is not a gradient"),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
gradient.add_stop(color, offset);
|
for stop in buffer.into_iter() {
|
||||||
|
gradient.add_stop(stop.color(), stop.offset());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,23 @@ use super::Color;
|
||||||
use crate::math;
|
use crate::math;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct RawStopData {
|
||||||
|
color: [u8; 4],
|
||||||
|
offset: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawStopData {
|
||||||
|
pub fn color(&self) -> skia::Color {
|
||||||
|
skia::Color::from_argb(self.color[3], self.color[0], self.color[1], self.color[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn offset(&self) -> f32 {
|
||||||
|
self.offset as f32 / 100.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Gradient {
|
pub struct Gradient {
|
||||||
colors: Vec<Color>,
|
colors: Vec<Color>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue