Replace Primitive::Translate with Transform

This commit is contained in:
Héctor Ramón Jiménez 2023-10-23 03:13:28 +02:00
parent 759f0e9225
commit 5467c19c80
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
11 changed files with 205 additions and 133 deletions

View file

@ -3,7 +3,7 @@ use tiny_skia::Size;
use crate::core::{Background, Color, Gradient, Rectangle, Vector};
use crate::graphics::backend;
use crate::graphics::text;
use crate::graphics::Viewport;
use crate::graphics::{Transformation, Viewport};
use crate::primitive::{self, Primitive};
use std::borrow::Cow;
@ -106,7 +106,7 @@ impl Backend {
clip_mask,
region,
scale_factor,
Vector::ZERO,
Transformation::IDENTITY,
);
}
@ -146,7 +146,7 @@ impl Backend {
clip_mask: &mut tiny_skia::Mask,
clip_bounds: Rectangle,
scale_factor: f32,
translation: Vector,
transformation: Transformation,
) {
match primitive {
Primitive::Quad {
@ -164,7 +164,7 @@ impl Backend {
"Quad with non-normal height!"
);
let physical_bounds = (*bounds + translation) * scale_factor;
let physical_bounds = (*bounds * transformation) * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -173,11 +173,8 @@ impl Backend {
let clip_mask = (!physical_bounds.is_within(&clip_bounds))
.then_some(clip_mask as &_);
let transform = tiny_skia::Transform::from_translate(
translation.x,
translation.y,
)
.post_scale(scale_factor, scale_factor);
let transform = into_transform(transformation)
.post_scale(scale_factor, scale_factor);
// Make sure the border radius is not larger than the bounds
let border_width = border
@ -199,7 +196,7 @@ impl Backend {
y: bounds.y + shadow.offset.y - shadow.blur_radius,
width: bounds.width + shadow.blur_radius * 2.0,
height: bounds.height + shadow.blur_radius * 2.0,
} + translation)
} * transformation)
* scale_factor;
let radii = fill_border_radius
@ -451,7 +448,7 @@ impl Backend {
clip_bounds: text_clip_bounds,
} => {
let physical_bounds =
(*text_clip_bounds + translation) * scale_factor;
*text_clip_bounds * transformation * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -462,7 +459,7 @@ impl Backend {
self.text_pipeline.draw_paragraph(
paragraph,
*position + translation,
*position * transformation,
*color,
scale_factor,
pixels,
@ -476,7 +473,7 @@ impl Backend {
clip_bounds: text_clip_bounds,
} => {
let physical_bounds =
(*text_clip_bounds + translation) * scale_factor;
(*text_clip_bounds * transformation) * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -487,7 +484,7 @@ impl Backend {
self.text_pipeline.draw_editor(
editor,
*position + translation,
*position * transformation,
*color,
scale_factor,
pixels,
@ -507,7 +504,7 @@ impl Backend {
clip_bounds: text_clip_bounds,
} => {
let physical_bounds =
(*text_clip_bounds + translation) * scale_factor;
*text_clip_bounds * transformation * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -518,7 +515,7 @@ impl Backend {
self.text_pipeline.draw_cached(
content,
*bounds + translation,
*bounds * transformation,
*color,
*size,
*line_height,
@ -542,7 +539,7 @@ impl Backend {
};
let physical_bounds =
(*text_clip_bounds + translation) * scale_factor;
*text_clip_bounds * transformation * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -553,7 +550,7 @@ impl Backend {
self.text_pipeline.draw_raw(
&buffer,
*position + translation,
*position * transformation,
*color,
scale_factor,
pixels,
@ -566,7 +563,7 @@ impl Backend {
filter_method,
bounds,
} => {
let physical_bounds = (*bounds + translation) * scale_factor;
let physical_bounds = (*bounds * transformation) * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -575,11 +572,8 @@ impl Backend {
let clip_mask = (!physical_bounds.is_within(&clip_bounds))
.then_some(clip_mask as &_);
let transform = tiny_skia::Transform::from_translate(
translation.x,
translation.y,
)
.post_scale(scale_factor, scale_factor);
let transform = into_transform(transformation)
.post_scale(scale_factor, scale_factor);
self.raster_pipeline.draw(
handle,
@ -602,7 +596,7 @@ impl Backend {
bounds,
color,
} => {
let physical_bounds = (*bounds + translation) * scale_factor;
let physical_bounds = (*bounds * transformation) * scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
return;
@ -614,7 +608,7 @@ impl Backend {
self.vector_pipeline.draw(
handle,
*color,
(*bounds + translation) * scale_factor,
(*bounds * transformation) * scale_factor,
pixels,
clip_mask,
);
@ -637,7 +631,7 @@ impl Backend {
y: bounds.y(),
width: bounds.width(),
height: bounds.height(),
} + translation)
} * transformation)
* scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
@ -651,11 +645,8 @@ impl Backend {
path,
paint,
*rule,
tiny_skia::Transform::from_translate(
translation.x,
translation.y,
)
.post_scale(scale_factor, scale_factor),
into_transform(transformation)
.post_scale(scale_factor, scale_factor),
clip_mask,
);
}
@ -671,7 +662,7 @@ impl Backend {
y: bounds.y(),
width: bounds.width(),
height: bounds.height(),
} + translation)
} * transformation)
* scale_factor;
if !clip_bounds.intersects(&physical_bounds) {
@ -685,11 +676,8 @@ impl Backend {
path,
paint,
stroke,
tiny_skia::Transform::from_translate(
translation.x,
translation.y,
)
.post_scale(scale_factor, scale_factor),
into_transform(transformation)
.post_scale(scale_factor, scale_factor),
clip_mask,
);
}
@ -701,12 +689,12 @@ impl Backend {
clip_mask,
clip_bounds,
scale_factor,
translation,
transformation,
);
}
}
Primitive::Translate {
translation: offset,
Primitive::Transform {
transformation: new_transformation,
content,
} => {
self.draw_primitive(
@ -715,11 +703,11 @@ impl Backend {
clip_mask,
clip_bounds,
scale_factor,
translation + *offset,
transformation * *new_transformation,
);
}
Primitive::Clip { bounds, content } => {
let bounds = (*bounds + translation) * scale_factor;
let bounds = (*bounds * transformation) * scale_factor;
if bounds == clip_bounds {
self.draw_primitive(
@ -728,7 +716,7 @@ impl Backend {
clip_mask,
bounds,
scale_factor,
translation,
transformation,
);
} else if let Some(bounds) = clip_bounds.intersection(&bounds) {
if bounds.x + bounds.width <= 0.0
@ -749,7 +737,7 @@ impl Backend {
clip_mask,
bounds,
scale_factor,
translation,
transformation,
);
adjust_clip_mask(clip_mask, clip_bounds);
@ -762,7 +750,7 @@ impl Backend {
clip_mask,
clip_bounds,
scale_factor,
translation,
transformation,
);
}
}
@ -780,6 +768,17 @@ fn into_color(color: Color) -> tiny_skia::Color {
.expect("Convert color from iced to tiny_skia")
}
fn into_transform(transformation: Transformation) -> tiny_skia::Transform {
tiny_skia::Transform {
sx: transformation.scale_x(),
kx: 0.0,
ky: 0.0,
sy: transformation.scale_y(),
tx: transformation.translation_x(),
ty: transformation.translation_y(),
}
}
fn rounded_rectangle(
bounds: Rectangle,
border_radius: [f32; 4],

View file

@ -3,7 +3,7 @@ use crate::core::{Pixels, Point, Rectangle, Size, Vector};
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::stroke::{self, Stroke};
use crate::graphics::geometry::{Path, Style, Text};
use crate::graphics::Gradient;
use crate::graphics::{Gradient, Transformation};
use crate::primitive::{self, Primitive};
pub struct Frame {
@ -181,8 +181,8 @@ impl Frame {
}
pub fn clip(&mut self, frame: Self, at: Point) {
self.primitives.push(Primitive::Translate {
translation: Vector::new(at.x, at.y),
self.primitives.push(Primitive::Transform {
transformation: Transformation::translate(at.x, at.y),
content: Box::new(frame.into_primitive()),
});
}