feat: calculate size of rasterized png using content area

This commit is contained in:
2025-07-10 11:53:43 -05:00
parent 430a6ca7ac
commit babae191a4
2 changed files with 14 additions and 7 deletions

View File

@@ -1,3 +1,5 @@
use std::default;
use resvg::usvg::{fontdb, TreeParsing, TreeTextToPath}; use resvg::usvg::{fontdb, TreeParsing, TreeTextToPath};
use resvg::{tiny_skia, usvg}; use resvg::{tiny_skia, usvg};
@@ -45,8 +47,13 @@ impl Rasterizer {
resvg::Tree::from_usvg(&tree) resvg::Tree::from_usvg(&tree)
}; };
let pixmap_size = tree.size.to_int_size(); let content_area = tree.content_area.unwrap();
let mut pixmap = tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).unwrap();
let mut pixmap = tiny_skia::Pixmap::new(
(content_area.width() + content_area.left() * 2f32).ceil() as u32,
(content_area.height() + content_area.top() * 2f32).ceil() as u32,
)
.unwrap();
tree.render(tiny_skia::Transform::default(), &mut pixmap.as_mut()); tree.render(tiny_skia::Transform::default(), &mut pixmap.as_mut());
pixmap.encode_png().map_err(|_| RenderError { pixmap.encode_png().map_err(|_| RenderError {

View File

@@ -1,6 +1,6 @@
<svg width="512" height="34" xmlns="http://www.w3.org/2000/svg" font-family="Roboto Mono" font-size="27"> <svg xmlns="http://www.w3.org/2000/svg" font-family="Roboto Mono"
<text x="8" y="27">{{ text }}</text> font-size="27">
<style> <text x="8" y="27" text-anchor="start">
text {{ text }}
</style> </text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 191 B

After

Width:  |  Height:  |  Size: 167 B