Skip to content

Commit

Permalink
Merge pull request #30 from LaurenzV/stroke-width-fix
Browse files Browse the repository at this point in the history
Fix bug where scaling wasn't properly applied to stroke widths
  • Loading branch information
laurmaedje committed May 19, 2023
2 parents 29682b1 + 84e29b3 commit 35f4bb8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ fn render_path_partial(

if stroke {
if let Some(stroke) = &path.stroke {
content.set_line_width(ctx.c.px_to_pt(stroke.width.get()));
content.set_line_width(
ctx.c.px_to_pt(stroke.width.get() * ctx.c.compute_scale()),
);

match stroke.linecap {
LineCap::Butt => content.set_line_cap(LineCapStyle::ButtCap),
Expand Down
16 changes: 16 additions & 0 deletions src/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ impl CoordToPdf {
self.transform.apply(point.0, point.1)
}

/// Compute the scale (e.g. to adapt the stroke width).
pub fn compute_scale(&self) -> f64 {
let mut complete_transform = Transform::new_scale(self.factor_x, self.factor_y);
complete_transform.append(&self.transform);
let (x_scale, y_scale) = complete_transform.get_scale();

if x_scale.is_finite() && y_scale.is_finite() {
let scale = x_scale.max(y_scale);
if scale > 0.0 {
return scale;
}
}

1.0
}

/// Set a pre-transformation, overriding the old one.
pub fn concat_transform(&mut self, add_transform: Transform) -> Transform {
let old = self.transform.clone();
Expand Down
6 changes: 6 additions & 0 deletions tests/scaled_line_with_line_width.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 35f4bb8

Please sign in to comment.