Fix 0pt strokes (#923)

This commit is contained in:
Pg Biel 2023-04-23 09:38:12 -03:00 committed by GitHub
parent fd5e5b1ebb
commit e9a0cf9741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 21 deletions

View File

@ -402,28 +402,33 @@ fn render_shape(
miter_limit,
}) = &shape.stroke
{
let dash = dash_pattern.as_ref().and_then(|pattern| {
// tiny-skia only allows dash patterns with an even number of elements,
// while pdf allows any number.
let len = if pattern.array.len() % 2 == 1 {
pattern.array.len() * 2
} else {
pattern.array.len()
};
let dash_array =
pattern.array.iter().map(|l| l.to_f32()).cycle().take(len).collect();
let width = thickness.to_f32();
sk::StrokeDash::new(dash_array, pattern.phase.to_f32())
});
let paint = paint.into();
let stroke = sk::Stroke {
width: thickness.to_f32(),
line_cap: line_cap.into(),
line_join: line_join.into(),
dash,
miter_limit: miter_limit.0 as f32,
};
canvas.stroke_path(&path, &paint, &stroke, ts, mask);
// Don't draw zero-pt stroke.
if width > 0.0 {
let dash = dash_pattern.as_ref().and_then(|pattern| {
// tiny-skia only allows dash patterns with an even number of elements,
// while pdf allows any number.
let len = if pattern.array.len() % 2 == 1 {
pattern.array.len() * 2
} else {
pattern.array.len()
};
let dash_array =
pattern.array.iter().map(|l| l.to_f32()).cycle().take(len).collect();
sk::StrokeDash::new(dash_array, pattern.phase.to_f32())
});
let paint = paint.into();
let stroke = sk::Stroke {
width,
line_cap: line_cap.into(),
line_join: line_join.into(),
dash,
miter_limit: miter_limit.0 as f32,
};
canvas.stroke_path(&path, &paint, &stroke, ts, mask);
}
}
Some(())

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

@ -69,3 +69,36 @@
// Error: 29-55 expected "solid", "dotted", "densely-dotted", "loosely-dotted", "dashed", "densely-dashed", "loosely-dashed", "dashdotted", "densely-dashdotted", "loosely-dashdotted", array, dictionary, dash pattern, or none
#line(length: 60pt, stroke: (color: red, dash: "dash"))
---
// 0pt strokes must function exactly like 'none' strokes and not draw anything
#rect(width: 10pt, height: 10pt, stroke: none)
#rect(width: 10pt, height: 10pt, stroke: 0pt)
#rect(width: 10pt, height: 10pt, stroke: none, fill: blue)
#rect(width: 10pt, height: 10pt, stroke: 0pt + red, fill: blue)
#line(length: 30pt, stroke: 0pt)
#line(length: 30pt, stroke: (color: red, thickness: 0pt, dash: ("dot", 1pt)))
#table(columns: 2, stroke: none)[A][B]
#table(columns: 2, stroke: 0pt)[A][B]
#path(
fill: red,
stroke: none,
closed: true,
((0%, 0%), (4%, -4%)),
((50%, 50%), (4%, -4%)),
((0%, 50%), (4%, 4%)),
((50%, 0%), (4%, 4%)),
)
#path(
fill: red,
stroke: 0pt,
closed: true,
((0%, 0%), (4%, -4%)),
((50%, 50%), (4%, -4%)),
((0%, 50%), (4%, 4%)),
((50%, 0%), (4%, 4%)),
)