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

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