mirror of
https://github.com/penpot/penpot.git
synced 2025-06-06 22:41:37 +02:00
✨ Serialize solid fills as bytes (wasm)
This commit is contained in:
parent
81f18ad7f4
commit
093fa18839
7 changed files with 73 additions and 24 deletions
|
@ -217,8 +217,12 @@
|
||||||
image (:fill-image fill)]
|
image (:fill-image fill)]
|
||||||
(cond
|
(cond
|
||||||
(some? color)
|
(some? color)
|
||||||
(let [rgba (sr-clr/hex->u32argb color opacity)]
|
(let [size sr-fills/SOLID-BYTE-SIZE
|
||||||
(h/call wasm/internal-module "_add_shape_solid_fill" rgba))
|
offset (mem/alloc-bytes size)
|
||||||
|
heap (mem/get-heap-u32)
|
||||||
|
argb (sr-clr/hex->u32argb color opacity)]
|
||||||
|
(sr-fills/write-solid-fill! offset heap argb)
|
||||||
|
(h/call wasm/internal-module "_add_shape_solid_fill"))
|
||||||
|
|
||||||
(some? gradient)
|
(some? gradient)
|
||||||
(let [size sr-fills/GRADIENT-BYTE-SIZE
|
(let [size sr-fills/GRADIENT-BYTE-SIZE
|
||||||
|
|
|
@ -2,6 +2,14 @@
|
||||||
(:require
|
(:require
|
||||||
[app.render-wasm.serializers.color :as clr]))
|
[app.render-wasm.serializers.color :as clr]))
|
||||||
|
|
||||||
|
(def SOLID-BYTE-SIZE 4)
|
||||||
|
|
||||||
|
(defn write-solid-fill!
|
||||||
|
[offset heap-u32 argb]
|
||||||
|
(let [dview (js/DataView. (.-buffer heap-u32))]
|
||||||
|
(.setUint32 dview offset argb true)
|
||||||
|
(+ offset 4)))
|
||||||
|
|
||||||
(def ^:private GRADIENT-STOP-SIZE 8)
|
(def ^:private GRADIENT-STOP-SIZE 8)
|
||||||
(def ^:private GRADIENT-BASE-SIZE 28)
|
(def ^:private GRADIENT-BASE-SIZE 28)
|
||||||
;; TODO: Define in shape model
|
;; TODO: Define in shape model
|
||||||
|
@ -11,8 +19,8 @@
|
||||||
(+ GRADIENT-BASE-SIZE (* MAX-GRADIENT-STOPS GRADIENT-STOP-SIZE)))
|
(+ GRADIENT-BASE-SIZE (* MAX-GRADIENT-STOPS GRADIENT-STOP-SIZE)))
|
||||||
|
|
||||||
(defn write-gradient-fill!
|
(defn write-gradient-fill!
|
||||||
[offset heap gradient opacity]
|
[offset heap-u32 gradient opacity]
|
||||||
(let [dview (js/DataView. (.-buffer heap))
|
(let [dview (js/DataView. (.-buffer heap-u32))
|
||||||
start-x (:start-x gradient)
|
start-x (:start-x gradient)
|
||||||
start-y (:start-y gradient)
|
start-y (:start-y gradient)
|
||||||
end-x (:end-x gradient)
|
end-x (:end-x gradient)
|
||||||
|
|
|
@ -860,8 +860,11 @@ mod tests {
|
||||||
let mut shape = any_shape();
|
let mut shape = any_shape();
|
||||||
assert_eq!(shape.fills.len(), 0);
|
assert_eq!(shape.fills.len(), 0);
|
||||||
|
|
||||||
shape.add_fill(Fill::Solid(Color::TRANSPARENT));
|
shape.add_fill(Fill::Solid(SolidColor(Color::TRANSPARENT)));
|
||||||
assert_eq!(shape.fills.get(0), Some(&Fill::Solid(Color::TRANSPARENT)))
|
assert_eq!(
|
||||||
|
shape.fills.get(0),
|
||||||
|
Some(&Fill::Solid(SolidColor(Color::TRANSPARENT)))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use skia_safe::{self as skia, Rect};
|
use skia_safe::{self as skia, Rect};
|
||||||
|
|
||||||
use super::Color;
|
pub use super::Color;
|
||||||
use crate::uuid::Uuid;
|
use crate::uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -112,9 +112,12 @@ impl ImageFill {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||||
|
pub struct SolidColor(pub Color);
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Fill {
|
pub enum Fill {
|
||||||
Solid(Color),
|
Solid(SolidColor),
|
||||||
LinearGradient(Gradient),
|
LinearGradient(Gradient),
|
||||||
RadialGradient(Gradient),
|
RadialGradient(Gradient),
|
||||||
Image(ImageFill),
|
Image(ImageFill),
|
||||||
|
@ -132,7 +135,7 @@ impl Fill {
|
||||||
|
|
||||||
pub fn to_paint(&self, rect: &Rect, anti_alias: bool) -> skia::Paint {
|
pub fn to_paint(&self, rect: &Rect, anti_alias: bool) -> skia::Paint {
|
||||||
match self {
|
match self {
|
||||||
Self::Solid(color) => {
|
Self::Solid(SolidColor(color)) => {
|
||||||
let mut p = skia::Paint::default();
|
let mut p = skia::Paint::default();
|
||||||
p.set_color(*color);
|
p.set_color(*color);
|
||||||
p.set_style(skia::PaintStyle::Fill);
|
p.set_style(skia::PaintStyle::Fill);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::shapes::fills::Fill;
|
use crate::shapes::fills::{Fill, SolidColor};
|
||||||
use skia_safe::{self as skia, Rect};
|
use skia_safe::{self as skia, Rect};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
@ -78,10 +78,9 @@ impl Stroke {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_center_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self {
|
pub fn new_center_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self {
|
||||||
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
|
||||||
Stroke {
|
Stroke {
|
||||||
fill: Fill::Solid(transparent),
|
fill: Fill::Solid(SolidColor(skia::Color::TRANSPARENT)),
|
||||||
width: width,
|
width,
|
||||||
style: StrokeStyle::from(style),
|
style: StrokeStyle::from(style),
|
||||||
cap_end: StrokeCap::from(cap_end),
|
cap_end: StrokeCap::from(cap_end),
|
||||||
cap_start: StrokeCap::from(cap_start),
|
cap_start: StrokeCap::from(cap_start),
|
||||||
|
@ -90,10 +89,9 @@ impl Stroke {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_inner_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self {
|
pub fn new_inner_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self {
|
||||||
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
|
||||||
Stroke {
|
Stroke {
|
||||||
fill: Fill::Solid(transparent),
|
fill: Fill::Solid(SolidColor(skia::Color::TRANSPARENT)),
|
||||||
width: width,
|
width,
|
||||||
style: StrokeStyle::from(style),
|
style: StrokeStyle::from(style),
|
||||||
cap_end: StrokeCap::from(cap_end),
|
cap_end: StrokeCap::from(cap_end),
|
||||||
cap_start: StrokeCap::from(cap_start),
|
cap_start: StrokeCap::from(cap_start),
|
||||||
|
@ -102,10 +100,9 @@ impl Stroke {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_outer_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self {
|
pub fn new_outer_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) -> Self {
|
||||||
let transparent = skia::Color::from_argb(0, 0, 0, 0);
|
|
||||||
Stroke {
|
Stroke {
|
||||||
fill: Fill::Solid(transparent),
|
fill: Fill::Solid(SolidColor(skia::Color::TRANSPARENT)),
|
||||||
width: width,
|
width,
|
||||||
style: StrokeStyle::from(style),
|
style: StrokeStyle::from(style),
|
||||||
cap_end: StrokeCap::from(cap_end),
|
cap_end: StrokeCap::from(cap_end),
|
||||||
cap_start: StrokeCap::from(cap_start),
|
cap_start: StrokeCap::from(cap_start),
|
||||||
|
|
|
@ -2,16 +2,18 @@ use skia_safe as skia;
|
||||||
|
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
use crate::shapes;
|
use crate::shapes;
|
||||||
use crate::shapes::Gradient;
|
use crate::shapes::{Gradient, SolidColor};
|
||||||
use crate::utils::uuid_from_u32_quartet;
|
use crate::utils::uuid_from_u32_quartet;
|
||||||
use crate::with_current_shape;
|
use crate::with_current_shape;
|
||||||
use crate::STATE;
|
use crate::STATE;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_solid_fill(raw_color: u32) {
|
pub extern "C" fn add_shape_solid_fill() {
|
||||||
with_current_shape!(state, |shape: &mut Shape| {
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
let color = skia::Color::new(raw_color);
|
let bytes = mem::bytes();
|
||||||
shape.add_fill(shapes::Fill::Solid(color));
|
let solid_color = SolidColor::try_from(&bytes[..]).expect("Invalid solid color data");
|
||||||
|
|
||||||
|
shape.add_fill(shapes::Fill::Solid(solid_color));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,6 +62,38 @@ pub extern "C" fn clear_shape_fills() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct RawSolidData {
|
||||||
|
color: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<[u8; 4]> for RawSolidData {
|
||||||
|
fn from(value: [u8; 4]) -> Self {
|
||||||
|
Self {
|
||||||
|
color: u32::from_le_bytes(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RawSolidData> for SolidColor {
|
||||||
|
fn from(value: RawSolidData) -> Self {
|
||||||
|
Self(skia::Color::new(value.color))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&[u8]> for SolidColor {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||||
|
let raw_solid_bytes: [u8; 4] = bytes[0..4]
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| "Invalid solid fill data".to_string())?;
|
||||||
|
let color = RawSolidData::from(raw_solid_bytes).into();
|
||||||
|
|
||||||
|
Ok(color)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const MAX_GRADIENT_STOPS: usize = 16;
|
const MAX_GRADIENT_STOPS: usize = 16;
|
||||||
const BASE_GRADIENT_DATA_SIZE: usize = 28;
|
const BASE_GRADIENT_DATA_SIZE: usize = 28;
|
||||||
const RAW_GRADIENT_DATA_SIZE: usize =
|
const RAW_GRADIENT_DATA_SIZE: usize =
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub extern "C" fn add_shape_stroke_solid_fill(raw_color: u32) {
|
||||||
with_current_shape!(state, |shape: &mut Shape| {
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
let color = skia::Color::new(raw_color);
|
let color = skia::Color::new(raw_color);
|
||||||
shape
|
shape
|
||||||
.set_stroke_fill(shapes::Fill::Solid(color))
|
.set_stroke_fill(shapes::Fill::Solid(shapes::SolidColor(color)))
|
||||||
.expect("could not add stroke solid fill");
|
.expect("could not add stroke solid fill");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue