Skip to main content

lightweight_pdf/
lib.rs

1//! Public facade for `lightweight-pdf`: re-exports the `lightweight-pdf-core` builder API
2//! and adds `Document::render()` (ADR-002 — this is the crate users add to
3//! `Cargo.toml`, the place `render()` becomes public).
4
5mod fonts;
6mod images;
7mod render;
8
9pub use fonts::FontRegistry;
10pub use images::ImageEmbedError;
11pub use lightweight_pdf_core::*;
12pub use lightweight_pdf_fonts::FontError;
13pub use lightweight_pdf_layout::{LayoutWarning, LayoutWarningKind};
14pub use render::{DocumentExt, RenderError};
15
16#[cfg(all(feature = "wasm-size-probe", not(feature = "default-fonts")))]
17compile_error!(
18    "wasm-size-probe needs a font source: enable `default-fonts` alongside it (see plan/00a-contracts-and-artifacts.md, point 2)"
19);
20
21/// Internal, non-public measurement export (`plan/00a-contracts-and-artifacts.md`
22/// point 1): renders a small but complete document end-to-end so the real
23/// render path isn't dead-code-eliminated from the size measurement. Not a
24/// public runtime API (ADR-009: no JS-facing API in V1).
25#[cfg(feature = "wasm-size-probe")]
26#[no_mangle]
27pub extern "C" fn lightweight_pdf_wasm_size_probe() -> i32 {
28    let mut doc = Document::new(PageFormat::A4);
29    doc.add(Text::new("Hallo Rechnung").size(18.0).bold());
30    let cell = |s: &str| Text::new(s).size(10.0);
31    doc.add(Row::new().gap(8.0).child(cell("Menge")).child(cell("Preis")));
32    doc.add(Line::new());
33    match doc.render() {
34        // `bytes` comes from rendering the small, fixed document literally
35        // constructed above (not attacker/caller-supplied input), so its
36        // length is bounded far below `i32::MAX` in practice — `unwrap_or`
37        // keeps that a non-panicking fallback rather than an `.expect()` on
38        // this `pub extern "C"` FFI boundary.
39        Ok(bytes) => i32::try_from(bytes.len()).unwrap_or(i32::MAX),
40        Err(_) => -1,
41    }
42}