#[cfg(feature = "png")]
mod raster;
use crate::color::Color;
use crate::dim::Dim;
use crate::error::Error;
use crate::font::MathFont;
use crate::layout::MathBox;
use crate::parser::parse;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PngBackground {
Transparent,
White,
Color(Color),
}
#[derive(Clone, Debug)]
pub struct PngOptions {
pub font_size_pt: Dim,
pub dpi: Dim,
pub color: Color,
pub background: PngBackground,
pub display: bool,
}
impl Default for PngOptions {
fn default() -> Self {
Self {
font_size_pt: Dim::from_i64(12),
dpi: Dim::from_i64(144),
color: Color::rgb(0, 0, 0),
background: PngBackground::Transparent,
display: false,
}
}
}
impl PngOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
}
pub fn latex_to_png(latex: &str, font: &MathFont, options: &PngOptions) -> Result<Vec<u8>, Error> {
let ast = parse(latex)?;
#[cfg(not(feature = "png"))]
{
let _ = (font, options, ast);
Err(Error::Unsupported {
what: "png renderer".into(),
})
}
#[cfg(feature = "png")]
{
use crate::layout::{layout, MathStyle};
let style = if options.display {
MathStyle::Display
} else {
MathStyle::Text
};
let tree = layout(&ast, font, style)?;
render_png(&tree, font, options)
}
}
pub fn render_png(tree: &MathBox, font: &MathFont, options: &PngOptions) -> Result<Vec<u8>, Error> {
#[cfg(not(feature = "png"))]
{
let _ = (tree, font, options);
Err(Error::Unsupported {
what: "png renderer".into(),
})
}
#[cfg(feature = "png")]
{
raster::render(tree, font, options)
}
}
#[cfg(all(test, not(feature = "png")))]
mod tests {
use super::*;
use crate::font::MathFont;
use crate::layout::MathBox;
#[test]
fn png_is_unsupported() {
let font = MathFont::stix_two_math().expect("STIX");
let err = render_png(&MathBox::empty(), &font, &PngOptions::new()).expect_err("png");
assert!(err.to_string().contains("png"), "{err}");
let err = latex_to_png("x", &font, &PngOptions::new()).expect_err("png latex");
assert!(err.to_string().contains("png"), "{err}");
}
}