mirror of
https://github.com/penpot/penpot.git
synced 2025-05-31 22:51:37 +02:00
♻️ Switch to a f32 offset for gradient stops
This commit is contained in:
parent
abcd050c69
commit
5765d1c56c
4 changed files with 41 additions and 42 deletions
|
@ -51,7 +51,7 @@
|
||||||
(def GRID-LAYOUT-COLUMN-ENTRY-SIZE 5)
|
(def GRID-LAYOUT-COLUMN-ENTRY-SIZE 5)
|
||||||
(def GRID-LAYOUT-CELL-ENTRY-SIZE 37)
|
(def GRID-LAYOUT-CELL-ENTRY-SIZE 37)
|
||||||
|
|
||||||
|
;; FIXME: use `gradient-byte-size` instead
|
||||||
(defn gradient-stop-get-entries-size
|
(defn gradient-stop-get-entries-size
|
||||||
[stops]
|
[stops]
|
||||||
(mem/get-list-size stops sr-fills/GRADIENT-STOP-SIZE))
|
(mem/get-list-size stops sr-fills/GRADIENT-STOP-SIZE))
|
||||||
|
@ -238,7 +238,7 @@
|
||||||
:linear
|
:linear
|
||||||
(let [size (gradient-byte-size gradient)
|
(let [size (gradient-byte-size gradient)
|
||||||
offset (mem/alloc-bytes size)
|
offset (mem/alloc-bytes size)
|
||||||
heap (mem/get-heap-u8)]
|
heap (mem/get-heap-u32)]
|
||||||
(sr-fills/serialize-linear-fill gradient opacity heap offset)
|
(sr-fills/serialize-linear-fill gradient opacity heap offset)
|
||||||
(h/call wasm/internal-module "_add_shape_linear_fill"))
|
(h/call wasm/internal-module "_add_shape_linear_fill"))
|
||||||
:radial
|
:radial
|
||||||
|
@ -297,11 +297,10 @@
|
||||||
|
|
||||||
(cond
|
(cond
|
||||||
(some? gradient)
|
(some? gradient)
|
||||||
(let [stops (:stops gradient)
|
(let [stops (:stops gradient)
|
||||||
size (gradient-stop-get-entries-size stops)
|
size (gradient-stop-get-entries-size stops)
|
||||||
offset (mem/alloc-bytes size)
|
offset (mem/alloc-bytes size)
|
||||||
heap (mem/get-heap-u8)
|
heap (mem/get-heap-u8)]
|
||||||
mem (js/Uint8Array. (.-buffer heap) offset size)]
|
|
||||||
(if (= (:type gradient) :linear)
|
(if (= (:type gradient) :linear)
|
||||||
(h/call wasm/internal-module "_add_shape_stroke_linear_fill"
|
(h/call wasm/internal-module "_add_shape_stroke_linear_fill"
|
||||||
(:start-x gradient)
|
(:start-x gradient)
|
||||||
|
@ -316,11 +315,7 @@
|
||||||
(:end-y gradient)
|
(:end-y gradient)
|
||||||
opacity
|
opacity
|
||||||
(:width gradient)))
|
(:width gradient)))
|
||||||
(.set mem (js/Uint8Array. (clj->js (flatten (map (fn [stop]
|
(sr-fills/serialize-gradient-stops stops heap offset)
|
||||||
(let [[r g b a] (sr-clr/rgba-bytes-from-hex (:color stop) (:opacity stop))
|
|
||||||
offset (:offset stop)]
|
|
||||||
[r g b a (* 100 offset)]))
|
|
||||||
stops)))))
|
|
||||||
(h/call wasm/internal-module "_add_shape_stroke_stops"))
|
(h/call wasm/internal-module "_add_shape_stroke_stops"))
|
||||||
|
|
||||||
(some? image)
|
(some? image)
|
||||||
|
|
|
@ -3,12 +3,26 @@
|
||||||
[app.common.data.macros :as dm]
|
[app.common.data.macros :as dm]
|
||||||
[app.render-wasm.serializers.color :as clr]))
|
[app.render-wasm.serializers.color :as clr]))
|
||||||
|
|
||||||
(def GRADIENT-STOP-SIZE 5)
|
(def GRADIENT-STOP-SIZE 8)
|
||||||
(def GRADIENT-BASE-SIZE 24)
|
(def GRADIENT-BASE-SIZE 24)
|
||||||
|
|
||||||
|
(defn serialize-gradient-stops
|
||||||
|
[stops heap offset]
|
||||||
|
(let [dview (js/DataView. (.-buffer heap))]
|
||||||
|
(loop [stops (seq stops) offset offset]
|
||||||
|
(when-not (empty? stops)
|
||||||
|
(let [stop (first stops)
|
||||||
|
hex-color (dm/get-prop stop :color)
|
||||||
|
opacity (dm/get-prop stop :opacity)
|
||||||
|
argb (clr/hex->u32argb hex-color opacity)
|
||||||
|
stop-offset (dm/get-prop stop :offset)]
|
||||||
|
(.setUint32 dview offset argb true)
|
||||||
|
(.setFloat32 dview (+ offset 4) stop-offset true)
|
||||||
|
(recur (rest stops) (+ offset GRADIENT-STOP-SIZE)))))))
|
||||||
|
|
||||||
(defn serialize-linear-fill
|
(defn serialize-linear-fill
|
||||||
[gradient opacity heap-u8 offset]
|
[gradient opacity heap offset]
|
||||||
(let [dview (js/DataView. (.-buffer heap-u8))
|
(let [dview (js/DataView. (.-buffer heap))
|
||||||
start-x (dm/get-prop gradient :start-x)
|
start-x (dm/get-prop gradient :start-x)
|
||||||
start-y (dm/get-prop gradient :start-y)
|
start-y (dm/get-prop gradient :start-y)
|
||||||
end-x (dm/get-prop gradient :end-x)
|
end-x (dm/get-prop gradient :end-x)
|
||||||
|
@ -21,14 +35,4 @@
|
||||||
(.setFloat32 dview (+ offset 12) end-y true)
|
(.setFloat32 dview (+ offset 12) end-y true)
|
||||||
(.setFloat32 dview (+ offset 16) opacity true)
|
(.setFloat32 dview (+ offset 16) opacity true)
|
||||||
(.setFloat32 dview (+ offset 20) width true)
|
(.setFloat32 dview (+ offset 20) width true)
|
||||||
(loop [stops (seq stops) idx 0]
|
(serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE))))
|
||||||
(when-not (empty? stops)
|
|
||||||
(let [stop (first stops)
|
|
||||||
hex-color (dm/get-prop stop :color)
|
|
||||||
opacity (dm/get-prop stop :opacity)
|
|
||||||
rgba (clr/hex->u32argb hex-color opacity)
|
|
||||||
stop-offset (* 100 (dm/get-prop stop :offset))
|
|
||||||
dview-offset (+ (* idx 5) offset 24)]
|
|
||||||
(.setUint32 dview dview-offset rgba true)
|
|
||||||
(.setUint8 dview (+ dview-offset 4) stop-offset)
|
|
||||||
(recur (rest stops) (+ idx 1)))))))
|
|
|
@ -306,7 +306,10 @@ pub extern "C" fn add_shape_fill_stops() {
|
||||||
|
|
||||||
let entries: Vec<_> = bytes
|
let entries: Vec<_> = bytes
|
||||||
.chunks(size_of::<shapes::RawStopData>())
|
.chunks(size_of::<shapes::RawStopData>())
|
||||||
.map(|data| shapes::RawStopData::from_bytes(data.try_into().unwrap()))
|
.map(|data| {
|
||||||
|
let raw_stop_bytes: [u8; RAW_STOP_DATA_SIZE] = data.try_into().unwrap();
|
||||||
|
shapes::RawStopData::from(raw_stop_bytes)
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
with_current_shape!(state, |shape: &mut Shape| {
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
|
@ -523,7 +526,10 @@ pub extern "C" fn add_shape_stroke_stops() {
|
||||||
|
|
||||||
let entries: Vec<_> = bytes
|
let entries: Vec<_> = bytes
|
||||||
.chunks(size_of::<shapes::RawStopData>())
|
.chunks(size_of::<shapes::RawStopData>())
|
||||||
.map(|data| shapes::RawStopData::from_bytes(data.try_into().unwrap()))
|
.map(|data| {
|
||||||
|
let raw_stop_bytes: [u8; RAW_STOP_DATA_SIZE] = data.try_into().unwrap();
|
||||||
|
shapes::RawStopData::from(raw_stop_bytes)
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
with_current_shape!(state, |shape: &mut Shape| {
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
|
|
|
@ -43,13 +43,13 @@ impl RawGradientData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const RAW_STOP_DATA_SIZE: usize = 5;
|
pub const RAW_STOP_DATA_SIZE: usize = 8;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct RawStopData {
|
pub struct RawStopData {
|
||||||
color: u32,
|
color: u32,
|
||||||
offset: u8,
|
offset: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RawStopData {
|
impl RawStopData {
|
||||||
|
@ -58,22 +58,16 @@ impl RawStopData {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn offset(&self) -> f32 {
|
pub fn offset(&self) -> f32 {
|
||||||
self.offset as f32 / 100.0
|
self.offset
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_bytes(bytes: [u8; 5]) -> Self {
|
|
||||||
let color_bytes: [u8; 4] = bytes[0..4].try_into().unwrap();
|
|
||||||
Self {
|
|
||||||
color: u32::from_le_bytes(color_bytes),
|
|
||||||
offset: bytes[4],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<[u8; 5]> for RawStopData {
|
impl From<[u8; RAW_STOP_DATA_SIZE]> for RawStopData {
|
||||||
// TODO: remove from_bytes and copy its implementation here
|
fn from(bytes: [u8; RAW_STOP_DATA_SIZE]) -> Self {
|
||||||
fn from(bytes: [u8; 5]) -> Self {
|
Self {
|
||||||
Self::from_bytes(bytes)
|
color: u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
|
||||||
|
offset: f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue