Pixel precision for new renderer

This commit is contained in:
alonso.torres 2025-05-16 12:20:14 +02:00
parent 6b5703c1fe
commit 6cd2c712ab
4 changed files with 46 additions and 15 deletions

View file

@ -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))

View file

@ -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)))

View file

@ -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);

View file

@ -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<TransformEntry>, HashMap<Uuid, Bounds>) {
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,