use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, OnceLock};
use mbook::{Diagram, Formula, Picture};
use resvg::tiny_skia::{Color, Pixmap, Transform};
use resvg::usvg::{self, fontdb};
use super::picture::{self, Png};
use super::{FONT, Format, Result};
use crate::figure::{self, Figure, Palette, Setting};
const MAX_FIGURE_PIXELS: f32 = 16_777_216.0;
#[derive(Clone, Copy)]
pub(super) enum Graphic<'a> {
Picture(&'a Picture),
Diagram(&'a Diagram, Setting),
Formula(&'a Formula, Setting),
}
impl<'a> Graphic<'a> {
pub(super) fn key(self) -> (&'a str, Option<Setting>) {
match self {
Self::Picture(picture) => (picture.path(), None),
Self::Diagram(diagram, setting) => (diagram.path(), Some(setting)),
Self::Formula(formula, setting) => (formula.path(), Some(setting)),
}
}
}
pub(super) struct Drawn {
pub(super) png: Png,
pub(super) width: f32,
pub(super) height: f32,
pub(super) baseline: f32,
pub(super) figure: Option<Setting>,
}
pub(super) struct Drawings<'a> {
format: Format,
text: f32,
density: f32,
drawn: HashMap<(&'a str, Option<Setting>), Option<Rc<Drawn>>>,
}
impl<'a> Drawings<'a> {
pub(super) fn new(format: Format, text: f32, density: f32) -> Self {
Self {
format,
text,
density,
drawn: HashMap::new(),
}
}
pub(super) fn draw(&mut self, graphic: Graphic<'a>) -> Result<Option<Rc<Drawn>>> {
let key = graphic.key();
if let Some(drawn) = self.drawn.get(&key) {
return Ok(drawn.clone());
}
let palette = Palette::default();
let drawn = match graphic {
Graphic::Picture(source) => {
let png = picture::png(source, self.format)?;
let (width, height) = (pixels(png.width), pixels(png.height));
Some(Drawn {
png,
width,
height,
baseline: height,
figure: None,
})
}
Graphic::Diagram(source, setting) => figure::diagram(source.source(), palette)
.ok()
.and_then(|drawn| self.raster(&drawn, setting, Some(palette.page))),
Graphic::Formula(source, setting) => figure::formula(source.source(), setting, palette)
.ok()
.and_then(|drawn| self.raster(&drawn, setting, None)),
}
.map(Rc::new);
self.drawn.insert(key, drawn.clone());
Ok(drawn)
}
fn raster(&self, figure: &Figure, setting: Setting, background: Option<u32>) -> Option<Drawn> {
let scale = self.text / figure.em();
let png = rasterise(figure, scale * self.density, background)?;
Some(Drawn {
png,
width: figure.width() * scale,
height: figure.height() * scale,
baseline: figure.baseline() * scale,
figure: Some(setting),
})
}
}
fn pixels(count: u32) -> f32 {
f32::from(u16::try_from(count).unwrap_or(u16::MAX))
}
fn fonts() -> Arc<fontdb::Database> {
static FONTS: OnceLock<Arc<fontdb::Database>> = OnceLock::new();
Arc::clone(FONTS.get_or_init(|| {
let mut fonts = fontdb::Database::new();
fonts.load_system_fonts();
fonts.load_font_data(FONT.to_vec());
fonts.set_sans_serif_family("Noto Sans");
Arc::new(fonts)
}))
}
fn rasterise(figure: &Figure, scale: f32, background: Option<u32>) -> Option<Png> {
let options = usvg::Options {
fontdb: fonts(),
..usvg::Options::default()
};
let tree = usvg::Tree::from_str(figure.svg(), &options).ok()?;
let size = tree.size();
let area = size.width() * size.height();
let scale = scale.min((MAX_FIGURE_PIXELS / area).sqrt());
let size = usvg::Size::from_wh(size.width() * scale, size.height() * scale)?.to_int_size();
let mut pixmap = Pixmap::new(size.width(), size.height())?;
if let Some(colour) = background {
let [_, red, green, blue] = colour.to_be_bytes();
pixmap.fill(Color::from_rgba8(red, green, blue, 255));
}
resvg::render(
&tree,
Transform::from_scale(scale, scale),
&mut pixmap.as_mut(),
);
Some(Png {
bytes: pixmap.encode_png().ok()?,
width: size.width(),
height: size.height(),
})
}
#[cfg(test)]
mod tests {
use super::*;
fn formula(source: &str) -> Figure {
figure::formula(source, Setting::Line, Palette::default()).unwrap()
}
#[test]
fn a_figure_is_drawn_at_the_scale_it_is_given() {
let drawn = formula("x+y");
let once = rasterise(&drawn, 1.0, None).unwrap();
let twice = rasterise(&drawn, 2.0, None).unwrap();
assert!((pixels(once.width) - drawn.width()).abs() <= 1.0);
assert!(twice.width.abs_diff(2 * once.width) <= 1);
assert!(twice.height.abs_diff(2 * once.height) <= 1);
}
#[test]
fn a_large_figure_is_drawn_within_the_pixel_limit() {
let drawn = formula("x+y");
let png = rasterise(&drawn, 10_000.0, None).unwrap();
assert!(pixels(png.width) * pixels(png.height) <= MAX_FIGURE_PIXELS * 1.01);
}
#[test]
fn a_figure_on_a_page_is_drawn_on_its_colour() {
let png = rasterise(&formula("x"), 1.0, Some(0x00ff_0000)).unwrap();
let image = image::load_from_memory(&png.bytes).unwrap().into_rgba8();
assert_eq!(image.get_pixel(0, 0).0, [255, 0, 0, 255]);
let bare = rasterise(&formula("x"), 1.0, None).unwrap();
let image = image::load_from_memory(&bare.bytes).unwrap().into_rgba8();
assert_eq!(image.get_pixel(0, 0).0[3], 0);
}
#[test]
fn a_document_that_is_no_svg_is_not_drawn() {
let broken = formula("x").with_svg("not svg");
assert!(rasterise(&broken, 1.0, None).is_none());
}
}