Skip to content

Commit 17069cc

Browse files
committed
Try to simplify the variable names and fn args a little
1 parent e02935c commit 17069cc

4 files changed

Lines changed: 35 additions & 34 deletions

File tree

star/src/lower/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
#[cfg(feature = "serde")]
2424
mod length_serde;
2525
mod selector;
26-
pub(crate) mod transform;
26+
mod transform;
2727
mod units;
2828
mod visit;
2929

star/src/lower/visit.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,22 @@ impl<'a, 'input, T: Turtle> XmlVisitor for ConversionVisitor<'a, 'input, T> {
688688
slice: false,
689689
});
690690

691+
let view_box = svgtypes::ViewBox {
692+
x: 0.,
693+
y: 0.,
694+
w: image.width() as f64,
695+
h: image.height() as f64,
696+
};
697+
let image_to_user = get_viewport_transform(
698+
view_box,
699+
Some(preserve_aspect_ratio),
700+
[width, height],
701+
[Some(x), Some(y)],
702+
);
703+
691704
self.comment();
692705
self.terrarium
693-
.image(image, x, y, width, height, preserve_aspect_ratio);
706+
.image(image, image_to_user, preserve_aspect_ratio);
694707
}
695708
// No-op tags
696709
SVG_TAG_NAME | GROUP_TAG_NAME | USE_TAG_NAME | SYMBOL_TAG_NAME => {}

star/src/turtle/elements/image_ops.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,42 @@ use super::RasterImage;
2020
/// The `Turtle` must handle any stretching/scaling as appropriate.
2121
pub fn transform_image(
2222
mut image: DynamicImage,
23-
rect: Box2D<f64>,
24-
transform: &Transform2D<f64>,
23+
image_to_user: Transform2D<f64>,
24+
user_to_final: &Transform2D<f64>,
2525
preserve_aspect_ratio: AspectRatio,
2626
) -> (DynamicImage, Box2D<f64>) {
2727
// TODO: should this be more coarse?
2828
const EPSILON: f64 = f64::EPSILON;
2929
const MAX_NON_AFFINE_SCALE: f64 = 2.;
3030

31-
let corners = [
32-
rect.min,
33-
point(rect.max.x, rect.min.y),
34-
point(rect.min.x, rect.max.y),
35-
rect.max,
31+
let orig_w = image.width();
32+
let orig_h = image.height();
33+
34+
let pixel_corners = [
35+
point(0., 0.),
36+
point(orig_w as f64, 0.),
37+
point(0., orig_h as f64),
38+
point(orig_w as f64, orig_h as f64),
3639
];
37-
let transformed_corners = corners.map(|p| transform.transform_point(p));
40+
let user_corners = pixel_corners.map(|p| image_to_user.transform_point(p));
41+
let user_bounds = Box2D::from_points(user_corners);
42+
43+
let transformed_corners = user_corners.map(|p| user_to_final.transform_point(p));
3844
let transformed_box = Box2D::from_points(transformed_corners);
3945

4046
let (is_transform_axis_aligned, [transformed_x_axis, transformed_y_axis]) = {
41-
let tx = transform.transform_vector(vector(1.0, 0.0));
42-
let ty = transform.transform_vector(vector(0.0, 1.0));
47+
let tx = user_to_final.transform_vector(vector(1.0, 0.0));
48+
let ty = user_to_final.transform_vector(vector(0.0, 1.0));
4349

4450
let aligned = (tx.y.abs() < EPSILON && ty.x.abs() < EPSILON)
4551
|| (tx.x.abs() < EPSILON && ty.y.abs() < EPSILON);
4652

4753
(aligned, [tx, ty])
4854
};
4955

50-
let orig_w = image.width();
51-
let orig_h = image.height();
52-
5356
let aspect_ratios_match = {
5457
let orig_aspect_ratio = orig_w as f64 / orig_h as f64;
55-
let final_aspect_ratio = rect.width() / rect.height();
58+
let final_aspect_ratio = user_bounds.width() / user_bounds.height();
5659
(orig_aspect_ratio - final_aspect_ratio).abs() < EPSILON
5760
};
5861
let is_simple_orthogonal_rotation = is_transform_axis_aligned
@@ -76,19 +79,7 @@ pub fn transform_image(
7679
// During non-aligned rotation, the corners need to be transparent
7780
image = add_alpha_channel(image);
7881

79-
let view_box = svgtypes::ViewBox {
80-
x: 0.,
81-
y: 0.,
82-
w: orig_w as f64,
83-
h: orig_h as f64,
84-
};
85-
let image_viewport_transform = crate::lower::transform::get_viewport_transform(
86-
view_box,
87-
Some(preserve_aspect_ratio),
88-
[rect.width(), rect.height()],
89-
[Some(rect.min.x), Some(rect.min.y)],
90-
);
91-
let image_to_final = image_viewport_transform.then(transform);
82+
let image_to_final = image_to_user.then(user_to_final);
9283

9384
let scale_x = image_to_final.transform_vector(vector(1.0, 0.0)).length();
9485
let scale_y = image_to_final.transform_vector(vector(0.0, 1.0)).length();

star/src/turtle/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,12 @@ impl<T: Turtle + std::fmt::Debug> Terrarium<T> {
387387
pub fn image(
388388
&mut self,
389389
image: image::DynamicImage,
390-
x: f64,
391-
y: f64,
392-
width: f64,
393-
height: f64,
390+
image_to_user: Transform2D<f64>,
394391
preserve_aspect_ratio: svgtypes::AspectRatio,
395392
) {
396393
let (image, transformed_box) = self::elements::image_ops::transform_image(
397394
image,
398-
Box2D::new(point(x, y), point(x + width, y + height)),
395+
image_to_user,
399396
&self.current_transform,
400397
preserve_aspect_ratio,
401398
);

0 commit comments

Comments
 (0)