Skip to content

Commit

Permalink
Replace usvg::utils::view_box_to_transform with `usvg::ViewBox::to_…
Browse files Browse the repository at this point in the history
…transform`.
  • Loading branch information
RazrFalcon committed Feb 11, 2024
1 parent 782948f commit cadb172
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This changelog also contains important changes in dependencies.
- All shared nodes are stored in `Arc` and not `Rc<RefCell>` now.
- `resvg::render_node` now includes filters bounding box. Meaning that a node with a blur filter
no longer be clipped.
- Replace `usvg::utils::view_box_to_transform` with `usvg::ViewBox::to_transform`.

### Removed
- `usvg::Tree::postprocess()` and `usvg::PostProcessingSteps`. No longer needed.
Expand Down
7 changes: 1 addition & 6 deletions crates/resvg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ pub fn render(
)
.unwrap();

let ts = usvg::utils::view_box_to_transform(
tree.view_box().rect,
tree.view_box().aspect,
tree.size(),
);

let ts = tree.view_box().to_transform(tree.size());
let root_transform = transform.pre_concat(ts);

let ctx = render::Context { max_bbox };
Expand Down
2 changes: 1 addition & 1 deletion crates/resvg/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn render_pattern_pixmap(

let mut transform = tiny_skia::Transform::from_scale(sx, sy);
if let Some(vbox) = pattern.view_box() {
let ts = usvg::utils::view_box_to_transform(vbox.rect, vbox.aspect, rect.size());
let ts = vbox.to_transform(rect.size());
transform = transform.pre_concat(ts);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/usvg/src/parser/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn resolve(

if let Some(vbox) = view_box {
let size = Size::from_wh(r.width() * stroke_scale, r.height() * stroke_scale).unwrap();
let vbox_ts = crate::utils::view_box_to_transform(vbox.rect, vbox.aspect, size);
let vbox_ts = vbox.to_transform(size);
let (sx, sy) = vbox_ts.get_scale();
ts = ts.pre_scale(sx, sy);
} else {
Expand Down
7 changes: 4 additions & 3 deletions crates/usvg/src/parser/use_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use svgtypes::{Length, LengthUnit};

use super::converter;
use super::svgtree::{AId, EId, SvgNode};
use crate::{Group, IsValidLength, Node, NonZeroRect, Path, Size, Transform};
use crate::{Group, IsValidLength, Node, NonZeroRect, Path, Size, Transform, ViewBox};

pub(crate) fn convert(
node: SvgNode,
Expand Down Expand Up @@ -316,10 +316,11 @@ fn viewbox_transform(
}

let size = Size::from_wh(w, h)?;
let vb = linked.parse_viewbox()?;
let rect = linked.parse_viewbox()?;
let aspect = linked
.attribute(AId::PreserveAspectRatio)
.unwrap_or_default();
let view_box = ViewBox { rect, aspect };

Some(crate::utils::view_box_to_transform(vb, aspect, size))
Some(view_box.to_transform(size))
}
81 changes: 39 additions & 42 deletions crates/usvg/src/tree/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use strict_num::ApproxEqUlps;
pub use tiny_skia_path::{NonZeroRect, Rect, Size, Transform};

use crate::AspectRatio;
use crate::{Align, AspectRatio};

/// Approximate zero equality comparisons.
pub trait ApproxZeroUlps: ApproxEqUlps {
Expand Down Expand Up @@ -55,6 +55,44 @@ pub struct ViewBox {
pub aspect: AspectRatio,
}

impl ViewBox {
/// Converts `viewBox` into `Transform`.
pub fn to_transform(&self, img_size: Size) -> Transform {
let vr = self.rect;

let sx = img_size.width() / vr.width();
let sy = img_size.height() / vr.height();

let (sx, sy) = if self.aspect.align == Align::None {
(sx, sy)
} else {
let s = if self.aspect.slice {
if sx < sy {
sy
} else {
sx
}
} else {
if sx > sy {
sy
} else {
sx
}
};

(s, s)
};

let x = -vr.x() * sx;
let y = -vr.y() * sy;
let w = img_size.width() - vr.width() * sx;
let h = img_size.height() - vr.height() * sy;

let (tx, ty) = utils::aligned_pos(self.aspect.align, x, y, w, h);
Transform::from_row(sx, 0.0, 0.0, sy, tx, ty)
}
}

/// A bounding box calculator.
#[derive(Clone, Copy, Debug)]
pub(crate) struct BBox {
Expand Down Expand Up @@ -142,49 +180,8 @@ impl BBox {

/// Some useful utilities.
pub mod utils {
use super::*;
use crate::Align;

/// Converts `viewBox` to `Transform`.
pub fn view_box_to_transform(
view_box: NonZeroRect,
aspect: AspectRatio,
img_size: Size,
) -> Transform {
let vr = view_box;

let sx = img_size.width() / vr.width();
let sy = img_size.height() / vr.height();

let (sx, sy) = if aspect.align == Align::None {
(sx, sy)
} else {
let s = if aspect.slice {
if sx < sy {
sy
} else {
sx
}
} else {
if sx > sy {
sy
} else {
sx
}
};

(s, s)
};

let x = -vr.x() * sx;
let y = -vr.y() * sy;
let w = img_size.width() - vr.width() * sx;
let h = img_size.height() - vr.height() * sy;

let (tx, ty) = aligned_pos(aspect.align, x, y, w, h);
Transform::from_row(sx, 0.0, 0.0, sy, tx, ty)
}

/// Returns object aligned position.
pub fn aligned_pos(align: Align, x: f32, y: f32, w: f32, h: f32) -> (f32, f32) {
match align {
Expand Down

0 comments on commit cadb172

Please sign in to comment.