mirror of
https://github.com/penpot/penpot.git
synced 2025-01-06 14:50:20 -05:00
♻️ Refactor image fills
This commit is contained in:
parent
7105e49ac2
commit
58c4e53ee4
4 changed files with 44 additions and 28 deletions
|
@ -9,7 +9,6 @@ mod view;
|
|||
|
||||
use skia_safe as skia;
|
||||
|
||||
use crate::shapes::Image;
|
||||
use crate::state::State;
|
||||
use crate::utils::uuid_from_u32_quartet;
|
||||
|
||||
|
@ -201,18 +200,15 @@ pub extern "C" fn store_image(a: u32, b: u32, c: u32, d: u32, ptr: *mut u8, size
|
|||
panic!("Invalid data, null pointer or zero size");
|
||||
}
|
||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||
let render_state = state.render_state();
|
||||
let id = uuid_from_u32_quartet(a, b, c, d);
|
||||
|
||||
unsafe {
|
||||
let image_bytes = Vec::<u8>::from_raw_parts(ptr, size as usize, size as usize);
|
||||
let image_data = skia::Data::new_copy(&*image_bytes);
|
||||
match Image::from_encoded(image_data) {
|
||||
Some(image) => {
|
||||
render_state.images.insert(id.to_string(), image);
|
||||
}
|
||||
None => {
|
||||
eprintln!("Error on image decoding");
|
||||
match state.render_state().add_image(id, &image_bytes) {
|
||||
Err(msg) => {
|
||||
eprintln!("{}", msg);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
mem::free(ptr as *mut u8, size as usize * std::mem::size_of::<u8>());
|
||||
}
|
||||
|
@ -221,9 +217,8 @@ pub extern "C" fn store_image(a: u32, b: u32, c: u32, d: u32, ptr: *mut u8, size
|
|||
#[no_mangle]
|
||||
pub extern "C" fn is_image_cached(a: u32, b: u32, c: u32, d: u32) -> bool {
|
||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||
let render_state = state.render_state();
|
||||
let id = uuid_from_u32_quartet(a, b, c, d);
|
||||
render_state.images.contains_key(&id.to_string())
|
||||
state.render_state().has_image(&id)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
@ -233,8 +228,8 @@ pub extern "C" fn add_shape_image_fill(
|
|||
c: u32,
|
||||
d: u32,
|
||||
alpha: f32,
|
||||
width: f32,
|
||||
height: f32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
) {
|
||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
||||
let id = uuid_from_u32_quartet(a, b, c, d);
|
||||
|
@ -242,8 +237,7 @@ pub extern "C" fn add_shape_image_fill(
|
|||
shape.add_fill(shapes::Fill::new_image_fill(
|
||||
id,
|
||||
(alpha * 0xff as f32).floor() as u8,
|
||||
height,
|
||||
width,
|
||||
(width, height),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ pub(crate) struct RenderState {
|
|||
pub cached_surface_image: Option<CachedSurfaceImage>,
|
||||
options: RenderOptions,
|
||||
pub viewbox: Viewbox,
|
||||
pub images: HashMap<String, Image>,
|
||||
images: HashMap<Uuid, Image>,
|
||||
}
|
||||
|
||||
impl RenderState {
|
||||
|
@ -125,6 +125,18 @@ impl RenderState {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_image(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> {
|
||||
let image_data = skia::Data::new_copy(image_data);
|
||||
let image = Image::from_encoded(image_data).ok_or("Error decoding image data")?;
|
||||
|
||||
self.images.insert(id, image);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn has_image(&mut self, id: &Uuid) -> bool {
|
||||
self.images.contains_key(id)
|
||||
}
|
||||
|
||||
pub fn set_debug_flags(&mut self, debug: u32) {
|
||||
self.options.debug_flags = debug;
|
||||
}
|
||||
|
@ -214,12 +226,12 @@ impl RenderState {
|
|||
|
||||
for fill in shape.fills().rev() {
|
||||
if let Fill::Image(image_fill) = fill {
|
||||
let image = self.images.get(&image_fill.id.to_string());
|
||||
let image = self.images.get(&image_fill.id());
|
||||
if let Some(image) = image {
|
||||
draw_image_in_container(
|
||||
&self.drawing_surface.canvas(),
|
||||
&image,
|
||||
(image_fill.width, image_fill.height),
|
||||
image_fill.size(),
|
||||
shape.selrect,
|
||||
&fill.to_paint(&shape.selrect),
|
||||
);
|
||||
|
|
|
@ -60,10 +60,20 @@ impl Gradient {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ImageFill {
|
||||
pub id: Uuid,
|
||||
pub alpha: u8,
|
||||
pub height: f32,
|
||||
pub width: f32,
|
||||
id: Uuid,
|
||||
opacity: u8,
|
||||
height: i32,
|
||||
width: i32,
|
||||
}
|
||||
|
||||
impl ImageFill {
|
||||
pub fn size(&self) -> (i32, i32) {
|
||||
(self.width, self.height)
|
||||
}
|
||||
|
||||
pub fn id(&self) -> Uuid {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -84,10 +94,10 @@ impl Fill {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn new_image_fill(id: Uuid, alpha: u8, height: f32, width: f32) -> Self {
|
||||
pub fn new_image_fill(id: Uuid, opacity: u8, (width, height): (i32, i32)) -> Self {
|
||||
Self::Image(ImageFill {
|
||||
id,
|
||||
alpha,
|
||||
opacity,
|
||||
height,
|
||||
width,
|
||||
})
|
||||
|
@ -116,7 +126,7 @@ impl Fill {
|
|||
p.set_style(skia::PaintStyle::Fill);
|
||||
p.set_anti_alias(true);
|
||||
p.set_blend_mode(skia::BlendMode::SrcOver);
|
||||
p.set_alpha(image_fill.alpha);
|
||||
p.set_alpha(image_fill.opacity);
|
||||
p
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@ pub type Image = skia::Image;
|
|||
pub fn draw_image_in_container(
|
||||
canvas: &skia::Canvas,
|
||||
image: &Image,
|
||||
size: (f32, f32),
|
||||
size: (i32, i32),
|
||||
container: skia::Rect,
|
||||
paint: &skia::Paint,
|
||||
) {
|
||||
let width = size.0;
|
||||
let height = size.1;
|
||||
let width = size.0 as f32;
|
||||
let height = size.1 as f32;
|
||||
let image_aspect_ratio = width / height;
|
||||
|
||||
// Container size
|
||||
|
|
Loading…
Reference in a new issue