Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where scaling wasn't properly applied to stroke widths #30

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.