pub mod ast;
pub mod builder;
pub mod error;
pub mod parser;
pub use ast::Diagram;
pub use parser::WinnowParser as MermaidParser;
use builder::build_diagram_with_config;
pub use builder::types::OutputConfig;
use lievisual::render::{Renderer, SvgRenderer, SvgSizing};
fn scene_to_svg(scene: &lievisual::Scene) -> String {
let bg_css = if scene.background.a == 0 {
"transparent".to_string()
} else {
scene.background.to_hex()
};
let mut renderer = SvgRenderer::new(scene.width, scene.height)
.with_background(scene.background)
.with_sizing(SvgSizing::Intrinsic)
.with_style(format!(
"max-width: {:.3}px; background-color: {bg_css};",
scene.width
));
renderer.render_scene(scene);
renderer.into_string()
}
fn scene_to_png(scene: &lievisual::Scene) -> Vec<u8> {
use lievisual::render::VelloPixmapRenderer;
let w = (scene.width.round() as i64).max(1) as u32;
let h = (scene.height.round() as i64).max(1) as u32;
let mut renderer = VelloPixmapRenderer::new(w, h).with_background(scene.background);
renderer.render_png(scene)
}
pub use lievisual::{FontSource, parse_generic_family, register_font, register_font_generic};
pub fn render(mermaid_text: &str, width: u32, height: u32) -> error::DiagramResult<String> {
let diagram = MermaidParser::parse_mermaid(mermaid_text)?;
let config = OutputConfig {
width: Some(width as f64),
height: Some(height as f64),
..OutputConfig::default()
};
let scene = build_diagram_with_config(&diagram, &config)?;
Ok(scene_to_svg(&scene))
}
const MAX_PNG_PIXELS: f64 = 8192.0 * 8192.0;
fn render_scene_png_checked(scene: &lievisual::Scene) -> error::DiagramResult<Vec<u8>> {
let pixels = scene.width.max(0.0) * scene.height.max(0.0);
if !pixels.is_finite() || pixels > MAX_PNG_PIXELS {
return Err(error::DiagramError::RenderError(format!(
"PNG canvas too large: {:.0}x{:.0} ({} pixels, max {})",
scene.width, scene.height, pixels, MAX_PNG_PIXELS
)));
}
Ok(scene_to_png(scene))
}
pub fn render_png(mermaid_text: &str, width: u32, height: u32) -> error::DiagramResult<Vec<u8>> {
let diagram = MermaidParser::parse_mermaid(mermaid_text)?;
let config = OutputConfig {
width: Some(width as f64),
height: Some(height as f64),
upscale: true,
..OutputConfig::default()
};
let scene = build_diagram_with_config(&diagram, &config)?;
render_scene_png_checked(&scene)
}
pub fn render_with_config(
mermaid_text: &str,
config: &OutputConfig,
) -> error::DiagramResult<String> {
let diagram = MermaidParser::parse_mermaid(mermaid_text)?;
let scene = build_diagram_with_config(&diagram, config)?;
Ok(scene_to_svg(&scene))
}
pub fn render_png_with_config(
mermaid_text: &str,
config: &OutputConfig,
) -> error::DiagramResult<Vec<u8>> {
let diagram = MermaidParser::parse_mermaid(mermaid_text)?;
let scene = build_diagram_with_config(&diagram, config)?;
render_scene_png_checked(&scene)
}
pub use lievisual::{SketchFillStyle, SketchKind, SketchKinds, SketchOptions};
fn build_sketch_scene(
mermaid_text: &str,
config: &OutputConfig,
sketch: &SketchOptions,
) -> error::DiagramResult<lievisual::Scene> {
let diagram = MermaidParser::parse_mermaid(mermaid_text)?;
let mut scene = build_diagram_with_config(&diagram, config)?;
let mut sketch = sketch.clone();
if sketch.min_fill_area.is_none() {
sketch.min_fill_area = Some(SKETCH_MIN_FILL_AREA);
}
lievisual::roughen_scene_fit(&mut scene, &sketch, builder::CANVAS_MARGIN);
Ok(scene)
}
const SKETCH_MIN_FILL_AREA: f64 = 1000.0;
pub fn render_sketch(mermaid_text: &str, width: u32, height: u32) -> error::DiagramResult<String> {
render_sketch_with_config(
mermaid_text,
&OutputConfig {
width: Some(width as f64),
height: Some(height as f64),
..OutputConfig::default()
},
&SketchOptions::new().with_fill(SketchFillStyle::Hachure),
)
}
pub fn render_sketch_with_config(
mermaid_text: &str,
config: &OutputConfig,
sketch: &SketchOptions,
) -> error::DiagramResult<String> {
let scene = build_sketch_scene(mermaid_text, config, sketch)?;
Ok(scene_to_svg(&scene))
}
pub fn render_png_sketch(
mermaid_text: &str,
width: u32,
height: u32,
) -> error::DiagramResult<Vec<u8>> {
render_png_sketch_with_config(
mermaid_text,
&OutputConfig {
width: Some(width as f64),
height: Some(height as f64),
upscale: true,
..OutputConfig::default()
},
&SketchOptions::new().with_fill(SketchFillStyle::Hachure),
)
}
pub fn render_png_sketch_with_config(
mermaid_text: &str,
config: &OutputConfig,
sketch: &SketchOptions,
) -> error::DiagramResult<Vec<u8>> {
let scene = build_sketch_scene(mermaid_text, config, sketch)?;
render_scene_png_checked(&scene)
}
#[cfg(test)]
mod tests {
use super::*;
use lievisual::geometry::Color;
#[test]
fn svg_is_nonempty() {
let scene = lievisual::Scene::new(100.0, 100.0);
assert!(!scene_to_svg(&scene).is_empty());
}
#[test]
fn svg_background_follows_scene_background() {
let scene = lievisual::Scene::new(100.0, 100.0).with_background(Color::WHITE);
let svg = scene_to_svg(&scene);
assert!(svg.contains("background-color: #ffffff;"), "{svg}");
assert!(
svg.contains(r##"<rect x="0" y="0" width="100.00" height="100.00" fill="#ffffff""##),
"不透明背景应铺满画布: {svg}"
);
let scene = lievisual::Scene::new(100.0, 100.0).with_background(Color::TRANSPARENT);
let svg = scene_to_svg(&scene);
assert!(svg.contains("background-color: transparent;"), "{svg}");
assert!(
svg.contains(r##"fill-opacity="0.000""##),
"透明背景不应产生可见像素: {svg}"
);
}
#[test]
fn transparent_output_config_renders_no_visible_background() {
let config = OutputConfig {
background: Color::TRANSPARENT,
..Default::default()
};
let svg = render_with_config("flowchart TD\nA --> B", &config).expect("render");
assert!(!svg.contains("<svg width=\""), "{svg}");
assert!(svg.contains("viewBox="), "{svg}");
assert!(svg.contains("background-color: transparent;"), "{svg}");
assert!(
svg.contains(r##"fill-opacity="0.000""##),
"透明底不应有可见像素: {svg}"
);
}
}