Skip to content
Open
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
1 change: 1 addition & 0 deletions crates/c-api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ pub extern "C" fn resvg_render(
let mut pixmap = tiny_skia::PixmapMut::from_bytes(pixmap, width, height).unwrap();

resvg::render(&tree.0, transform.to_tiny_skia(), &mut pixmap)
.expect("Failed to render to pixmap");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we panic or fail quielty?

}

/// @brief Renders a Node by ID onto the image.
Expand Down
3 changes: 2 additions & 1 deletion crates/resvg/examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn main() {

let pixmap_size = tree.size().to_int_size();
let mut pixmap = tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();
resvg::render(&tree, tiny_skia::Transform::default(), &mut pixmap.as_mut());
resvg::render(&tree, tiny_skia::Transform::default(), &mut pixmap.as_mut())
.expect("Failed to render");
pixmap.save_png(&args[2]).unwrap();
}
2 changes: 1 addition & 1 deletion crates/resvg/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn render_vector(
pixmap: &mut tiny_skia::PixmapMut,
) -> Option<()> {
let mut sub_pixmap = tiny_skia::Pixmap::new(pixmap.width(), pixmap.height()).unwrap();
crate::render(tree, transform, &mut sub_pixmap.as_mut());
crate::render(tree, transform, &mut sub_pixmap.as_mut())?;
pixmap.draw_pixmap(
0,
0,
Expand Down
15 changes: 8 additions & 7 deletions crates/resvg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@ mod render;
/// Can be used to position SVG inside the `pixmap`.
///
/// The produced content is in the sRGB color space.
#[must_use]
pub fn render(
tree: &usvg::Tree,
transform: tiny_skia::Transform,
pixmap: &mut tiny_skia::PixmapMut,
) {
) -> Option<()> {
let target_size = tiny_skia::IntSize::from_wh(pixmap.width(), pixmap.height()).unwrap();
let max_bbox = tiny_skia::IntRect::from_xywh(
-(target_size.width() as i32) * 2,
-(target_size.height() as i32) * 2,
target_size.width() * 5,
target_size.height() * 5,
)
.unwrap();
(-(target_size.width() as i32)).checked_add(2)?,
(-(target_size.height() as i32)).checked_add(2)?,
target_size.width().checked_mul(5)?,
target_size.height().checked_mul(5)?,
)?;

let ctx = render::Context { max_bbox };
render::render_nodes(tree.root(), &ctx, transform, pixmap);
Some(())
}

/// Renders a node onto the pixmap.
Expand Down
2 changes: 1 addition & 1 deletion crates/resvg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ fn render_svg(args: &Args, tree: &usvg::Tree) -> Result<tiny_skia::Pixmap, Strin

let ts = args.fit_to.fit_to_transform(tree.size().to_int_size());

resvg::render(tree, ts, &mut pixmap.as_mut());
resvg::render(tree, ts, &mut pixmap.as_mut()).ok_or("Failed to render svg")?;

if args.export_area_drawing {
trim_pixmap(tree, ts, &pixmap).unwrap_or(pixmap)
Expand Down
4 changes: 2 additions & 2 deletions crates/resvg/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn render_inner(name: &str, test_mode: TestMode) -> usize {
size.width() as f32 / tree.size().width() as f32,
size.height() as f32 / tree.size().height() as f32,
);
resvg::render(&tree, render_ts, &mut pixmap.as_mut());
resvg::render(&tree, render_ts, &mut pixmap.as_mut()).expect("Failed to render");
Copy link
Contributor Author

@Its-Just-Nans Its-Just-Nans Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best would be to resturn a Option<usize> or a Result<usize, String>

I don't really like to .expect() but I do it now for this MR for no breaking change

}
TestMode::Node(id) => {
let node = tree.node_by_id(id).unwrap();
Expand All @@ -97,7 +97,7 @@ pub fn render_inner(name: &str, test_mode: TestMode) -> usize {
size = tree.size().to_int_size().scale_by(scale).unwrap();
pixmap = tiny_skia::Pixmap::new(size.width(), size.height()).unwrap();
let render_ts = tiny_skia::Transform::from_scale(scale, scale);
resvg::render(&tree, render_ts, &mut pixmap.as_mut());
resvg::render(&tree, render_ts, &mut pixmap.as_mut()).expect("Failed to render");
}
}

Expand Down