mirror of
https://github.com/penpot/penpot.git
synced 2025-05-30 13:26:10 +02:00
♻️ Refactor serializing gradient fill
This commit is contained in:
parent
dc3d802d3d
commit
1f58f96e88
4 changed files with 69 additions and 108 deletions
|
@ -234,18 +234,14 @@
|
||||||
(h/call wasm/internal-module "_add_shape_solid_fill" rgba))
|
(h/call wasm/internal-module "_add_shape_solid_fill" rgba))
|
||||||
|
|
||||||
(some? gradient)
|
(some? gradient)
|
||||||
(case (:type gradient)
|
(let [size (gradient-byte-size gradient)
|
||||||
:linear
|
offset (mem/alloc-bytes size)
|
||||||
(let [size (gradient-byte-size gradient)
|
heap (mem/get-heap-u32)]
|
||||||
offset (mem/alloc-bytes size)
|
(sr-fills/serialize-gradient-fill gradient opacity heap offset)
|
||||||
heap (mem/get-heap-u32)]
|
(case (:type gradient)
|
||||||
(sr-fills/serialize-linear-fill gradient opacity heap offset)
|
:linear
|
||||||
(h/call wasm/internal-module "_add_shape_linear_fill"))
|
(h/call wasm/internal-module "_add_shape_linear_fill")
|
||||||
:radial
|
:radial
|
||||||
(let [size (gradient-byte-size gradient)
|
|
||||||
offset (mem/alloc-bytes size)
|
|
||||||
heap (mem/get-heap-u32)]
|
|
||||||
(sr-fills/serialize-radial-fill gradient opacity heap offset)
|
|
||||||
(h/call wasm/internal-module "_add_shape_radial_fill")))
|
(h/call wasm/internal-module "_add_shape_radial_fill")))
|
||||||
|
|
||||||
(some? image)
|
(some? image)
|
||||||
|
@ -284,28 +280,15 @@
|
||||||
|
|
||||||
(cond
|
(cond
|
||||||
(some? gradient)
|
(some? gradient)
|
||||||
(let [stops (:stops gradient)
|
(let [size (gradient-byte-size gradient)
|
||||||
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-u32)]
|
||||||
|
(sr-fills/serialize-gradient-fill gradient opacity heap offset)
|
||||||
(case (:type gradient)
|
(case (:type gradient)
|
||||||
:linear
|
: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-y gradient)
|
|
||||||
(:end-x gradient)
|
|
||||||
(:end-y gradient)
|
|
||||||
opacity)
|
|
||||||
:radial
|
:radial
|
||||||
(h/call wasm/internal-module "_add_shape_stroke_radial_fill"
|
(h/call wasm/internal-module "_add_shape_stroke_radial_fill")))
|
||||||
(:start-x gradient)
|
|
||||||
(:start-y gradient)
|
|
||||||
(:end-x gradient)
|
|
||||||
(:end-y gradient)
|
|
||||||
opacity
|
|
||||||
(:width gradient)))
|
|
||||||
(sr-fills/serialize-gradient-stops stops heap offset)
|
|
||||||
(h/call wasm/internal-module "_add_shape_stroke_stops"))
|
|
||||||
|
|
||||||
(some? image)
|
(some? image)
|
||||||
(let [id (dm/get-prop image :id)
|
(let [id (dm/get-prop image :id)
|
||||||
|
|
|
@ -6,10 +6,22 @@
|
||||||
(def GRADIENT-STOP-SIZE 8)
|
(def GRADIENT-STOP-SIZE 8)
|
||||||
(def GRADIENT-BASE-SIZE 24)
|
(def GRADIENT-BASE-SIZE 24)
|
||||||
|
|
||||||
(defn serialize-gradient-stops
|
(defn serialize-gradient-fill
|
||||||
[stops heap offset]
|
[gradient opacity heap offset]
|
||||||
(let [dview (js/DataView. (.-buffer heap))]
|
(let [dview (js/DataView. (.-buffer heap))
|
||||||
(loop [stops (seq stops) offset offset]
|
start-x (dm/get-prop gradient :start-x)
|
||||||
|
start-y (dm/get-prop gradient :start-y)
|
||||||
|
end-x (dm/get-prop gradient :end-x)
|
||||||
|
end-y (dm/get-prop gradient :end-y)
|
||||||
|
width (or (dm/get-prop gradient :width) 0)
|
||||||
|
stops (dm/get-prop gradient :stops)]
|
||||||
|
(.setFloat32 dview offset start-x true)
|
||||||
|
(.setFloat32 dview (+ offset 4) start-y true)
|
||||||
|
(.setFloat32 dview (+ offset 8) end-x true)
|
||||||
|
(.setFloat32 dview (+ offset 12) end-y true)
|
||||||
|
(.setFloat32 dview (+ offset 16) opacity true)
|
||||||
|
(.setFloat32 dview (+ offset 20) width true)
|
||||||
|
(loop [stops (seq stops) offset (+ offset GRADIENT-BASE-SIZE)]
|
||||||
(when-not (empty? stops)
|
(when-not (empty? stops)
|
||||||
(let [stop (first stops)
|
(let [stop (first stops)
|
||||||
hex-color (dm/get-prop stop :color)
|
hex-color (dm/get-prop stop :color)
|
||||||
|
@ -18,38 +30,4 @@
|
||||||
stop-offset (dm/get-prop stop :offset)]
|
stop-offset (dm/get-prop stop :offset)]
|
||||||
(.setUint32 dview offset argb true)
|
(.setUint32 dview offset argb true)
|
||||||
(.setFloat32 dview (+ offset 4) stop-offset true)
|
(.setFloat32 dview (+ offset 4) stop-offset true)
|
||||||
(recur (rest stops) (+ offset GRADIENT-STOP-SIZE)))))))
|
(recur (rest stops) (+ offset GRADIENT-STOP-SIZE)))))))
|
||||||
|
|
||||||
(defn serialize-linear-fill
|
|
||||||
[gradient opacity heap offset]
|
|
||||||
(let [dview (js/DataView. (.-buffer heap))
|
|
||||||
start-x (dm/get-prop gradient :start-x)
|
|
||||||
start-y (dm/get-prop gradient :start-y)
|
|
||||||
end-x (dm/get-prop gradient :end-x)
|
|
||||||
end-y (dm/get-prop gradient :end-y)
|
|
||||||
stops (dm/get-prop gradient :stops)
|
|
||||||
width 0]
|
|
||||||
(.setFloat32 dview offset start-x true)
|
|
||||||
(.setFloat32 dview (+ offset 4) start-y true)
|
|
||||||
(.setFloat32 dview (+ offset 8) end-x true)
|
|
||||||
(.setFloat32 dview (+ offset 12) end-y true)
|
|
||||||
(.setFloat32 dview (+ offset 16) opacity true)
|
|
||||||
(.setFloat32 dview (+ offset 20) width true)
|
|
||||||
(serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE))))
|
|
||||||
|
|
||||||
(defn serialize-radial-fill
|
|
||||||
[gradient opacity heap offset]
|
|
||||||
(let [dview (js/DataView. (.-buffer heap))
|
|
||||||
start-x (dm/get-prop gradient :start-x)
|
|
||||||
start-y (dm/get-prop gradient :start-y)
|
|
||||||
end-x (dm/get-prop gradient :end-x)
|
|
||||||
end-y (dm/get-prop gradient :end-y)
|
|
||||||
stops (dm/get-prop gradient :stops)
|
|
||||||
width (dm/get-prop gradient :width)]
|
|
||||||
(.setFloat32 dview offset start-x true)
|
|
||||||
(.setFloat32 dview (+ offset 4) start-y true)
|
|
||||||
(.setFloat32 dview (+ offset 8) end-x true)
|
|
||||||
(.setFloat32 dview (+ offset 12) end-y true)
|
|
||||||
(.setFloat32 dview (+ offset 16) opacity true)
|
|
||||||
(.setFloat32 dview (+ offset 20) width true)
|
|
||||||
(serialize-gradient-stops stops heap (+ offset GRADIENT-BASE-SIZE))))
|
|
|
@ -487,42 +487,55 @@ pub extern "C" fn add_shape_stroke_solid_fill(raw_color: u32) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_stroke_linear_fill(
|
pub extern "C" fn add_shape_stroke_linear_fill() {
|
||||||
start_x: f32,
|
|
||||||
start_y: f32,
|
|
||||||
end_x: f32,
|
|
||||||
end_y: f32,
|
|
||||||
opacity: f32,
|
|
||||||
) {
|
|
||||||
with_current_shape!(state, |shape: &mut Shape| {
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
|
let bytes = mem::bytes();
|
||||||
|
let raw_gradient_bytes: [u8; RAW_FILL_DATA_SIZE] =
|
||||||
|
bytes[0..RAW_FILL_DATA_SIZE].try_into().unwrap();
|
||||||
|
let raw_gradient = shapes::RawGradientData::from(raw_gradient_bytes);
|
||||||
|
let stops: Vec<shapes::RawStopData> = bytes[RAW_FILL_DATA_SIZE..]
|
||||||
|
.chunks(RAW_STOP_DATA_SIZE)
|
||||||
|
.map(|chunk| {
|
||||||
|
let data: [u8; RAW_STOP_DATA_SIZE] = chunk.try_into().unwrap();
|
||||||
|
shapes::RawStopData::from(data)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
shape
|
shape
|
||||||
.set_stroke_fill(shapes::Fill::new_linear_gradient(
|
.set_stroke_fill(shapes::Fill::new_linear_gradient_with_stops(
|
||||||
(start_x, start_y),
|
raw_gradient.start(),
|
||||||
(end_x, end_y),
|
raw_gradient.end(),
|
||||||
opacity,
|
raw_gradient.opacity(),
|
||||||
|
stops,
|
||||||
))
|
))
|
||||||
.expect("could not add stroke linear fill");
|
.expect("could not add stroke linear gradient fill");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_shape_stroke_radial_fill(
|
pub extern "C" fn add_shape_stroke_radial_fill() {
|
||||||
start_x: f32,
|
|
||||||
start_y: f32,
|
|
||||||
end_x: f32,
|
|
||||||
end_y: f32,
|
|
||||||
opacity: f32,
|
|
||||||
width: f32,
|
|
||||||
) {
|
|
||||||
with_current_shape!(state, |shape: &mut Shape| {
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
|
let bytes = mem::bytes();
|
||||||
|
let raw_gradient_bytes: [u8; RAW_FILL_DATA_SIZE] =
|
||||||
|
bytes[0..RAW_FILL_DATA_SIZE].try_into().unwrap();
|
||||||
|
let raw_gradient = shapes::RawGradientData::from(raw_gradient_bytes);
|
||||||
|
let stops: Vec<shapes::RawStopData> = bytes[RAW_FILL_DATA_SIZE..]
|
||||||
|
.chunks(RAW_STOP_DATA_SIZE)
|
||||||
|
.map(|chunk| {
|
||||||
|
let data: [u8; RAW_STOP_DATA_SIZE] = chunk.try_into().unwrap();
|
||||||
|
shapes::RawStopData::from(data)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
shape
|
shape
|
||||||
.set_stroke_fill(shapes::Fill::new_radial_gradient(
|
.set_stroke_fill(shapes::Fill::new_radial_gradient_with_stops(
|
||||||
(start_x, start_y),
|
raw_gradient.start(),
|
||||||
(end_x, end_y),
|
raw_gradient.end(),
|
||||||
opacity,
|
raw_gradient.opacity(),
|
||||||
width,
|
raw_gradient.width(),
|
||||||
|
stops,
|
||||||
))
|
))
|
||||||
.expect("could not add stroke radial fill");
|
.expect("could not add stroke radial gradient fill");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -171,10 +171,6 @@ pub enum Fill {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Fill {
|
impl Fill {
|
||||||
pub fn new_linear_gradient(start: (f32, f32), end: (f32, f32), opacity: f32) -> Self {
|
|
||||||
Self::new_linear_gradient_with_stops(start, end, opacity, vec![])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_linear_gradient_with_stops(
|
pub fn new_linear_gradient_with_stops(
|
||||||
start: (f32, f32),
|
start: (f32, f32),
|
||||||
end: (f32, f32),
|
end: (f32, f32),
|
||||||
|
@ -197,15 +193,6 @@ impl Fill {
|
||||||
Self::LinearGradient(gradient)
|
Self::LinearGradient(gradient)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_radial_gradient(
|
|
||||||
start: (f32, f32),
|
|
||||||
end: (f32, f32),
|
|
||||||
opacity: f32,
|
|
||||||
width: f32,
|
|
||||||
) -> Self {
|
|
||||||
Self::new_radial_gradient_with_stops(start, end, opacity, width, vec![])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_radial_gradient_with_stops(
|
pub fn new_radial_gradient_with_stops(
|
||||||
start: (f32, f32),
|
start: (f32, f32),
|
||||||
end: (f32, f32),
|
end: (f32, f32),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue