ps_anyrender_svg/lib.rs
1// Copyright 2023 the Vello Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Render an SVG into any impl of [`anyrender::PaintScene`].
5//!
6//! This currently lacks support for some important SVG features. Known missing features include: masking, filter effects, group backgrounds
7//! path shape-rendering, and patterns.
8
9// LINEBENDER LINT SET - lib.rs - v1
10// See https://linebender.org/wiki/canonical-lints/
11// These lints aren't included in Cargo.toml because they
12// shouldn't apply to examples and tests
13#![warn(unused_crate_dependencies)]
14#![warn(clippy::print_stdout, clippy::print_stderr)]
15// END LINEBENDER LINT SET
16#![cfg_attr(docsrs, feature(doc_cfg))]
17// The following lints are part of the Linebender standard set,
18// but resolving them has been deferred for now.
19// Feel free to send a PR that solves one or more of these.
20#![allow(missing_docs, clippy::shadow_unrelated, clippy::missing_errors_doc)]
21#![cfg_attr(test, allow(unused_crate_dependencies))] // Some dev dependencies are only used in tests
22
23mod error;
24mod render;
25mod util;
26
27pub use error::Error;
28pub use usvg;
29
30use anyrender::PaintScene;
31use kurbo::Affine;
32
33/// Append an SVG to an [`anyrender::PaintScene`].
34///
35/// This will draw a red box over (some) unsupported elements.
36pub fn render_svg_str<S: PaintScene>(
37 scene: &mut S,
38 svg: &str,
39 transform: Affine,
40) -> Result<(), Error> {
41 let opt = usvg::Options::default();
42 let tree = usvg::Tree::from_str(svg, &opt)?;
43 render_svg_tree(scene, &tree, transform);
44 Ok(())
45}
46
47/// Append an SVG to an [`anyrender::PaintScene`] (with custom error handling).
48///
49/// See the [module level documentation](crate#unsupported-features) for a list of some unsupported svg features
50pub fn render_svg_str_with<S: PaintScene, F: FnMut(&mut S, &usvg::Node)>(
51 scene: &mut S,
52 svg: &str,
53 transform: Affine,
54 error_handler: &mut F,
55) -> Result<(), Error> {
56 let opt = usvg::Options::default();
57 let tree = usvg::Tree::from_str(svg, &opt)?;
58 render_svg_tree_with(scene, &tree, transform, error_handler);
59 Ok(())
60}
61
62/// Append a [`usvg::Tree`] to an [`anyrender::PaintScene`].
63///
64/// This will draw a red box over (some) unsupported elements.
65pub fn render_svg_tree<S: PaintScene>(scene: &mut S, svg: &usvg::Tree, transform: Affine) {
66 render_svg_tree_with(scene, svg, transform, &mut util::default_error_handler);
67}
68
69/// Append a [`usvg::Tree`] to an [`anyrender::PaintScene`] (with custom error handling).
70///
71/// See the [module level documentation](crate#unsupported-features) for a list of some unsupported svg features
72pub fn render_svg_tree_with<S: PaintScene, F: FnMut(&mut S, &usvg::Node)>(
73 scene: &mut S,
74 svg: &usvg::Tree,
75 transform: Affine,
76 error_handler: &mut F,
77) {
78 render::render_group(
79 scene,
80 svg.root(),
81 Affine::IDENTITY,
82 transform,
83 error_handler,
84 );
85}
86
87#[cfg(test)]
88mod tests {
89 // CI will fail unless cargo nextest can execute at least one test per workspace.
90 // Delete this dummy test once we have an actual real test.
91 #[test]
92 fn dummy_test_until_we_have_a_real_test() {}
93}