Skip to main content

headless_engine/render/
mod.rs

1pub mod css;
2pub mod layout;
3pub mod paint;
4
5use crate::dom::interactive::InteractiveElement;
6use crate::dom::screenshot::ScreenshotResult;
7use anyhow::Result;
8use base64::Engine;
9pub use layout::LayoutEngine;
10pub use paint::PaintEngine;
11
12pub struct HtmlRenderer;
13
14impl HtmlRenderer {
15    pub async fn render_html_to_screenshot(
16        url: &str,
17        title: &str,
18        html_str: &str,
19        interactive: &[InteractiveElement],
20        width: u32,
21        height: u32,
22    ) -> Result<ScreenshotResult> {
23        let root_box = LayoutEngine::build_and_layout(html_str, interactive, width as f32);
24        let (svg, png_bytes) =
25            PaintEngine::paint_to_svg_and_png(url, title, &root_box, width, height).await?;
26
27        let png_base64 = if !png_bytes.is_empty() {
28            format!(
29                "data:image/png;base64,{}",
30                base64::engine::general_purpose::STANDARD.encode(&png_bytes)
31            )
32        } else {
33            String::new()
34        };
35
36        Ok(ScreenshotResult {
37            width,
38            height,
39            svg,
40            layout_wireframe: String::new(),
41            element_count: interactive.len(),
42            png_bytes,
43            png_base64,
44        })
45    }
46}