Skip to content

Commit

Permalink
feat: svg strokes (#438)
Browse files Browse the repository at this point in the history
* feat: svg strokes

* build: download SVG tests

* docs: update info

* fix: deduplicate error handling
  • Loading branch information
simbleau authored Feb 20, 2024
1 parent 0f1ec38 commit 67af4a1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
9 changes: 7 additions & 2 deletions examples/scenes/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,13 @@ impl SVGDownload {
if reader.read_exact(&mut [0]).is_ok() {
bail!("Size limit exceeded");
}
if limit_exact && file.stream_position().context("Checking file limit")? != size_limit {
bail!("Builtin downloaded file was not as expected");
if limit_exact {
let bytes_downloaded = file.stream_position().context("Checking file limit")?;
if bytes_downloaded != size_limit {
bail!(
"Builtin downloaded file was not as expected. Expected {size_limit}, received {bytes_downloaded}.",
);
}
}
Ok(())
}
Expand Down
60 changes: 60 additions & 0 deletions examples/scenes/src/download/default_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,65 @@ pub(super) fn default_downloads() -> Vec<SVGDownload> {
url: "https://upload.wikimedia.org/wikipedia/commons/5/58/Coat_of_arms_of_the_Kingdom_of_Yugoslavia.svg".to_string(),
name: "Coat of Arms of the Kingdom of Yugoslavia".to_string()
},
SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 383,
}),
url: "https://raw.githubusercontent.com/RazrFalcon/resvg-test-suite/master/tests/painting/stroke-dashoffset/default.svg".to_string(),
name: "SVG Stroke Dasharray Test".to_string()
},
SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 342,
}),
url: "https://raw.githubusercontent.com/RazrFalcon/resvg-test-suite/master/tests/painting/stroke-linecap/butt.svg".to_string(),
name: "SVG Stroke Linecap Butt Test".to_string()
},
SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 344,
}),
url: "https://raw.githubusercontent.com/RazrFalcon/resvg-test-suite/master/tests/painting/stroke-linecap/round.svg".to_string(),
name: "SVG Stroke Linecap Round Test".to_string()
},
SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 346,
}),
url: "https://raw.githubusercontent.com/RazrFalcon/resvg-test-suite/master/tests/painting/stroke-linecap/square.svg".to_string(),
name: "SVG Stroke Linecap Square Test".to_string()
}, SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 381,
}),
url: "https://github.com/RazrFalcon/resvg-test-suite/raw/master/tests/painting/stroke-linejoin/miter.svg".to_string(),
name: "SVG Stroke Linejoin Bevel Test".to_string()
}, SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 381,
}),
url: "https://github.com/RazrFalcon/resvg-test-suite/raw/master/tests/painting/stroke-linejoin/round.svg".to_string(),
name: "SVG Stroke Linejoin Round Test".to_string()
},SVGDownload {
builtin:Some(BuiltinSvgProps {
info: "https://github.com/RazrFalcon/resvg-test-suite",
license: "MIT",
expected_size: 351,
}),
url: "https://github.com/RazrFalcon/resvg-test-suite/raw/master/tests/painting/stroke-miterlimit/default.svg".to_string(),
name: "SVG Stroke Miterlimit Test".to_string()
},
]
}
24 changes: 21 additions & 3 deletions integrations/vello_svg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,28 @@ pub fn render_tree_with<F: FnMut(&mut Scene, &usvg::Node) -> Result<(), E>, E>(
if let Some((brush, brush_transform)) =
paint_to_brush(&stroke.paint, stroke.opacity)
{
// FIXME: handle stroke options such as linecap,
// linejoin, etc.
let mut conv_stroke = Stroke::new(stroke.width.get() as f64)
.with_caps(match stroke.linecap {
usvg::LineCap::Butt => vello::kurbo::Cap::Butt,
usvg::LineCap::Round => vello::kurbo::Cap::Round,
usvg::LineCap::Square => vello::kurbo::Cap::Square,
})
.with_join(match stroke.linejoin {
usvg::LineJoin::Miter | usvg::LineJoin::MiterClip => {
vello::kurbo::Join::Miter
}
usvg::LineJoin::Round => vello::kurbo::Join::Round,
usvg::LineJoin::Bevel => vello::kurbo::Join::Bevel,
})
.with_miter_limit(stroke.miterlimit.get() as f64);
if let Some(dash_array) = stroke.dasharray.as_ref() {
conv_stroke = conv_stroke.with_dashes(
stroke.dashoffset as f64,
dash_array.iter().map(|x| *x as f64),
);
}
scene.stroke(
&Stroke::new(stroke.width.get() as f64),
&conv_stroke,
transform,
&brush,
Some(brush_transform),
Expand Down

0 comments on commit 67af4a1

Please sign in to comment.