From 6cd2c712ab2873ecffbf5e06f03244a905d44a67 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Fri, 16 May 2025 12:20:14 +0200 Subject: [PATCH] :sparkles: Pixel precision for new renderer --- .../app/main/data/workspace/modifiers.cljs | 15 +++++----- frontend/src/app/render_wasm/api.cljs | 8 ++--- render-wasm/src/main.rs | 8 ++--- render-wasm/src/shapes/modifiers.rs | 30 +++++++++++++++++++ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/frontend/src/app/main/data/workspace/modifiers.cljs b/frontend/src/app/main/data/workspace/modifiers.cljs index 87274e79c5..f67f3f35c5 100644 --- a/frontend/src/app/main/data/workspace/modifiers.cljs +++ b/frontend/src/app/main/data/workspace/modifiers.cljs @@ -529,17 +529,15 @@ ptk/WatchEvent (watch [_ state _] (wasm.api/clean-modifiers) - (let [prev-wasm-props (:prev-wasm-props state) wasm-props (:wasm-props state) - objects (dsh/lookup-page-objects state)] - + objects (dsh/lookup-page-objects state) + pixel-precision false] (set-wasm-props! objects prev-wasm-props wasm-props) - (let [structure-entries (parse-structure-modifiers modif-tree)] (wasm.api/set-structure-modifiers structure-entries) (let [geometry-entries (parse-geometry-modifiers modif-tree) - selrect (wasm.api/propagate-apply geometry-entries)] + selrect (wasm.api/propagate-apply geometry-entries pixel-precision)] (rx/of (set-temporary-selrect selrect)))))))) #_:clj-kondo/ignore @@ -549,15 +547,18 @@ :as params}] (ptk/reify ::apply-wasm-modifiesr ptk/WatchEvent - (watch [_ _ _] + (watch [_ state _] (let [geometry-entries (parse-geometry-modifiers modif-tree) + snap-pixel? + (and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid)) + transforms (into {} (map (fn [{:keys [id transform]}] [id transform])) - (wasm.api/propagate-modifiers geometry-entries)) + (wasm.api/propagate-modifiers geometry-entries snap-pixel?)) ids (into (set (keys modif-tree)) (keys transforms)) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index f5fffbd3e4..cd843009cf 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -771,7 +771,7 @@ (h/call wasm/internal-module "_set_structure_modifiers")))) (defn propagate-modifiers - [entries] + [entries pixel-precision] (when (d/not-empty? entries) (let [offset (mem/alloc-bytes-32 (modifier-get-entries-size entries)) heapf32 (mem/get-heap-f32) @@ -785,7 +785,7 @@ (sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-TRANSFORM-OFFSET))) (recur (rest entries) (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-SIZE)))))) - (let [result-offset (h/call wasm/internal-module "_propagate_modifiers") + (let [result-offset (h/call wasm/internal-module "_propagate_modifiers" pixel-precision) heapf32 (mem/get-heap-f32) heapu32 (mem/get-heap-u32) len (aget heapu32 (mem/ptr8->ptr32 result-offset)) @@ -797,7 +797,7 @@ result)))) (defn propagate-apply - [entries] + [entries pixel-precision] (when (d/not-empty? entries) (let [offset (mem/alloc-bytes-32 (modifier-get-entries-size entries)) heapf32 (mem/get-heap-f32) @@ -811,7 +811,7 @@ (sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-TRANSFORM-OFFSET))) (recur (rest entries) (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-SIZE)))))) - (let [offset (h/call wasm/internal-module "_propagate_apply") + (let [offset (h/call wasm/internal-module "_propagate_apply" pixel-precision) heapf32 (mem/get-heap-f32) width (aget heapf32 (mem/ptr8->ptr32 (+ offset 0))) height (aget heapf32 (mem/ptr8->ptr32 (+ offset 4))) diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 602ea625f8..a850a9bafc 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -389,7 +389,7 @@ pub extern "C" fn set_shape_path_attrs(num_attrs: u32) { } #[no_mangle] -pub extern "C" fn propagate_modifiers() -> *mut u8 { +pub extern "C" fn propagate_modifiers(pixel_precision: bool) -> *mut u8 { let bytes = mem::bytes(); let entries: Vec<_> = bytes @@ -398,13 +398,13 @@ pub extern "C" fn propagate_modifiers() -> *mut u8 { .collect(); with_state!(state, { - let (result, _) = shapes::propagate_modifiers(state, &entries); + let (result, _) = shapes::propagate_modifiers(state, &entries, pixel_precision); mem::write_vec(result) }) } #[no_mangle] -pub extern "C" fn propagate_apply() -> *mut u8 { +pub extern "C" fn propagate_apply(pixel_precision: bool) -> *mut u8 { let bytes = mem::bytes(); let entries: Vec<_> = bytes @@ -413,7 +413,7 @@ pub extern "C" fn propagate_apply() -> *mut u8 { .collect(); with_state!(state, { - let (result, bounds) = shapes::propagate_modifiers(state, &entries); + let (result, bounds) = shapes::propagate_modifiers(state, &entries, pixel_precision); for entry in result { state.modifiers.insert(entry.id, entry.transform); diff --git a/render-wasm/src/shapes/modifiers.rs b/render-wasm/src/shapes/modifiers.rs index a1d5cd00d2..798c18d441 100644 --- a/render-wasm/src/shapes/modifiers.rs +++ b/render-wasm/src/shapes/modifiers.rs @@ -103,9 +103,35 @@ fn calculate_group_bounds( shape_bounds.with_points(result) } +fn set_pixel_precision(transform: &mut Matrix, bounds: &mut Bounds) { + let tr = bounds.transform_matrix().unwrap_or_default(); + let tr_inv = tr.invert().unwrap_or_default(); + + let x = bounds.min_x().round(); + let y = bounds.min_y().round(); + + let mut round_transform = Matrix::scale(( + bounds.width().round() / bounds.width(), + bounds.height().round() / bounds.height(), + )); + round_transform.post_concat(&tr); + round_transform.pre_concat(&tr_inv); + + transform.post_concat(&round_transform); + bounds.transform_mut(&round_transform); + + let dx = x - bounds.min_x(); + let dy = y - bounds.min_y(); + + let round_transform = Matrix::translate((dx, dy)); + transform.post_concat(&round_transform); + bounds.transform_mut(&round_transform); +} + pub fn propagate_modifiers( state: &State, modifiers: &[TransformEntry], + pixel_precision: bool, ) -> (Vec, HashMap) { let shapes = &state.shapes; @@ -161,6 +187,10 @@ pub fn propagate_modifiers( } } + if pixel_precision { + set_pixel_precision(&mut transform, &mut shape_bounds_after); + } + if entry.propagate { let mut children = propagate_children( shape,