use std::sync::OnceLock;
pub const RASTER_LONG_EDGE: u32 = 1600;
pub const MAX_SVG_BYTES: u64 = 20 * 1024 * 1024;
fn fontdb() -> &'static resvg::usvg::fontdb::Database {
static DB: OnceLock<resvg::usvg::fontdb::Database> = OnceLock::new();
DB.get_or_init(|| {
use resvg::usvg::fontdb::{Family, Query};
let mut db = resvg::usvg::fontdb::Database::new();
db.load_system_fonts();
let generics_resolve = db
.query(&Query {
families: &[Family::SansSerif, Family::Serif, Family::Monospace],
..Default::default()
})
.is_some();
if !generics_resolve {
let first = db.faces().next().map(|f| f.families[0].0.clone());
if let Some(name) = first {
db.set_sans_serif_family(name.clone());
db.set_serif_family(name.clone());
db.set_monospace_family(name.clone());
db.set_cursive_family(name.clone());
db.set_fantasy_family(name);
}
}
db
})
}
#[cfg(test)]
pub fn has_fonts() -> bool {
fontdb().faces().next().is_some()
}
pub fn rasterize(svg: &[u8]) -> Result<(Vec<u8>, u32, u32), String> {
let opts = resvg::usvg::Options {
fontdb: std::sync::Arc::new(fontdb().clone()),
font_family: String::from("sans-serif"),
..Default::default()
};
let tree = resvg::usvg::Tree::from_data(svg, &opts).map_err(|e| e.to_string())?;
let size = tree.size();
let (nw, nh) = (size.width().max(1.0), size.height().max(1.0));
let long = nw.max(nh);
let scale = (RASTER_LONG_EDGE as f32 / long).min(8.0);
let (w, h) = (
(nw * scale).round().max(1.0) as u32,
(nh * scale).round().max(1.0) as u32,
);
let mut pixmap = resvg::tiny_skia::Pixmap::new(w, h)
.ok_or_else(|| String::from("raster dimensions overflow"))?;
resvg::render(
&tree,
resvg::tiny_skia::Transform::from_scale(scale, scale),
&mut pixmap.as_mut(),
);
let mut rgba = pixmap.data().to_vec();
for px in rgba.chunks_exact_mut(4) {
let a = px[3] as u16;
if a > 0 && a < 255 {
px[0] = (px[0] as u16 * 255 / a).min(255) as u8;
px[1] = (px[1] as u16 * 255 / a).min(255) as u8;
px[2] = (px[2] as u16 * 255 / a).min(255) as u8;
}
}
let img = image::RgbaImage::from_raw(w, h, rgba)
.ok_or_else(|| String::from("raster buffer size mismatch"))?;
let mut png = std::io::Cursor::new(Vec::new());
img.write_to(&mut png, image::ImageFormat::Png)
.map_err(|e| e.to_string())?;
Ok((png.into_inner(), w, h))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rasterizes_shapes_to_a_decodable_png_at_the_long_edge() {
let svg = br##"<svg xmlns="http://www.w3.org/2000/svg" width="400" height="200">
<rect x="0" y="0" width="400" height="200" fill="#ff0000"/>
</svg>"##;
let (png, w, h) = rasterize(svg).unwrap();
assert_eq!((w, h), (RASTER_LONG_EDGE, RASTER_LONG_EDGE / 2));
let img = image::load_from_memory(&png).unwrap().to_rgba8();
assert_eq!(img.dimensions(), (w, h));
let px = img.get_pixel(w / 2, h / 2);
assert_eq!((px[0], px[1], px[2], px[3]), (255, 0, 0, 255), "solid red");
}
#[test]
fn tiny_svgs_are_not_upscaled_past_8x() {
let svg = br##"<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16">
<circle cx="8" cy="8" r="8" fill="#00ff00"/>
</svg>"##;
let (_, w, h) = rasterize(svg).unwrap();
assert_eq!((w, h), (128, 128), "16px icon caps at 8x, not 1600px");
}
#[test]
fn text_elements_shape_when_the_host_has_fonts() {
if !has_fonts() {
return;
}
let svg = br##"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="60">
<text x="10" y="40" font-size="40" fill="#000000">Hi</text>
</svg>"##;
let (png, w, h) = rasterize(svg).unwrap();
let img = image::load_from_memory(&png).unwrap().to_rgba8();
let inked = img.pixels().filter(|p| p[3] > 0).count();
assert!(
inked > 100,
"text must rasterise to visible pixels, got {inked} inked of {}",
w * h
);
}
#[test]
fn invalid_svg_reports_an_error_instead_of_panicking() {
let _ = rasterize(b"<svg"); assert!(rasterize(b"not xml at all").is_err());
assert!(rasterize(b"").is_err());
}
}