blueprinter 0.2.0

Hand-drawn diagram raster renderer CLI — Mermaid / SVG → PNG / WebP with watercolor bleed (aquarelle) and glyph-path text jitter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/// Export SVG to raster formats (PNG, WebP)
use std::path::Path;

use aquarelle::{render_aquarelle_bleed_pass, AquarelleBleedParams};
use resvg::{tiny_skia, usvg};

/// usvg defaults to an empty font database, so any `<text>` in the input is
/// silently dropped from raster output. We load system fonts so common faces
/// (Arial, Helvetica, the macOS / Linux defaults) resolve, and optionally
/// load every TTF/OTF in a user-supplied directory on top so the output is
/// reproducible across machines that have different system fonts.
fn options_with_fonts(extra_dir: Option<&Path>) -> usvg::Options<'static> {
    let mut opt = usvg::Options::default();
    opt.fontdb_mut().load_system_fonts();
    if let Some(dir) = extra_dir {
        opt.fontdb_mut().load_fonts_dir(dir);
    }
    opt
}

pub fn export_to_png(
    svg: &str,
    dimensions: Option<(Option<u32>, Option<u32>)>,
    scale: f32,
    font_dir: Option<&Path>,
    bleed_params: Option<AquarelleBleedParams>,
    seed: u64,
) -> Result<Vec<u8>, String> {
    let tree = usvg::Tree::from_str(svg, &options_with_fonts(font_dir))
        .map_err(|e| format!("Failed to parse SVG: {e}"))?;

    let (width, height) = calculate_dimensions(&tree, dimensions, scale)?;

    let mut pixmap = tiny_skia::Pixmap::new(width, height).ok_or("Failed to create pixmap")?;
    pixmap.fill(tiny_skia::Color::WHITE);

    let render_ts = tiny_skia::Transform::from_scale(scale, scale);
    resvg::render(&tree, render_ts, &mut pixmap.as_mut());

    if let Some(params) = bleed_params {
        // tiny-skia's Pixmap is the same type aquarelle exposes (re-exported
        // via aquarelle's own tiny-skia dep). The raster bleed pass runs
        // after resvg has drawn the SVG, replacing the previous SVG-filter
        // prototype for sumi/watercolor.
        render_aquarelle_bleed_pass(&mut pixmap, params, seed);
    }

    pixmap
        .encode_png()
        .map_err(|e| format!("Failed to encode PNG: {e}"))
}

/// Encodes the rasterized SVG as lossless WebP. Lossless preserves the sharp
/// edges of strokes and screentone patterns, which the lossy encoder smears.
pub fn export_to_webp(
    svg: &str,
    dimensions: Option<(Option<u32>, Option<u32>)>,
    scale: f32,
    font_dir: Option<&Path>,
    bleed_params: Option<AquarelleBleedParams>,
    seed: u64,
) -> Result<Vec<u8>, String> {
    let tree = usvg::Tree::from_str(svg, &options_with_fonts(font_dir))
        .map_err(|e| format!("Failed to parse SVG: {e}"))?;

    let (width, height) = calculate_dimensions(&tree, dimensions, scale)?;

    let mut pixmap = tiny_skia::Pixmap::new(width, height).ok_or("Failed to create pixmap")?;
    pixmap.fill(tiny_skia::Color::WHITE);

    let render_ts = tiny_skia::Transform::from_scale(scale, scale);
    resvg::render(&tree, render_ts, &mut pixmap.as_mut());

    if let Some(params) = bleed_params {
        // See export_to_png for the Pixmap-sharing rationale: aquarelle
        // re-exports tiny-skia, so the bleed pass operates on the same
        // pixmap type resvg just wrote into.
        render_aquarelle_bleed_pass(&mut pixmap, params, seed);
    }

    let encoder = webp::Encoder::from_rgba(pixmap.data(), width, height);
    Ok(encoder.encode_lossless().to_vec())
}

fn calculate_dimensions(
    tree: &usvg::Tree,
    dimensions: Option<(Option<u32>, Option<u32>)>,
    scale: f32,
) -> Result<(u32, u32), String> {
    let svg_size = tree.size();
    let svg_aspect_ratio = svg_size.width() / svg_size.height();

    let (width, height) = match dimensions {
        // Both width and height specified
        Some((Some(w), Some(h))) => (w, h),

        // Width only specified → preserve aspect ratio, calculate height
        Some((Some(w), None)) => {
            let h = (w as f32 / svg_aspect_ratio) as u32;
            (w, h)
        }

        // Height only specified → preserve aspect ratio, calculate width
        Some((None, Some(h))) => {
            let w = (h as f32 * svg_aspect_ratio) as u32;
            (w, h)
        }

        // Neither specified → apply scale
        None => (
            (svg_size.width() * scale) as u32,
            (svg_size.height() * scale) as u32,
        ),

        // This case is impossible (type system ensures it)
        Some((None, None)) => unreachable!(),
    };

    if width == 0 || height == 0 {
        return Err("Invalid dimensions: width and height must be greater than 0".to_string());
    }

    Ok((width, height))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_export_to_png_simple_svg() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <circle cx="50" cy="50" r="40" fill="red"/>
        </svg>"#;

        let result = export_to_png(svg, None, 1.0, None, None, 0);
        assert!(result.is_ok());
        let data = result.unwrap();
        assert!(!data.is_empty());
        // PNG magic number
        assert_eq!(&data[0..8], &[137, 80, 78, 71, 13, 10, 26, 10]);
    }

    #[test]
    fn test_export_to_png_with_scale() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <rect x="10" y="10" width="80" height="80" fill="blue"/>
        </svg>"#;

        let result = export_to_png(svg, None, 2.0, None, None, 0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_export_to_png_with_both_dimensions() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <line x1="0" y1="0" x2="100" y2="100" stroke="black"/>
        </svg>"#;

        let result = export_to_png(svg, Some((Some(200), Some(200))), 1.0, None, None, 0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_export_to_png_with_width_only() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <rect x="10" y="10" width="80" height="80" fill="blue"/>
        </svg>"#;

        let result = export_to_png(svg, Some((Some(200), None)), 1.0, None, None, 0);
        assert!(result.is_ok());
        // Aspect ratio should be preserved (200 x 200 for square SVG)
    }

    #[test]
    fn test_export_to_png_with_height_only() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">
            <rect x="10" y="10" width="180" height="80" fill="green"/>
        </svg>"#;

        let result = export_to_png(svg, Some((None, Some(100))), 1.0, None, None, 0);
        assert!(result.is_ok());
        // Aspect ratio should be preserved (200 x 100 for 2:1 SVG)
    }

    #[test]
    fn test_export_invalid_svg() {
        let svg = "not valid svg";
        let result = export_to_png(svg, None, 1.0, None, None, 0);
        assert!(result.is_err());
    }

    #[test]
    fn test_export_to_webp_simple_svg() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <circle cx="50" cy="50" r="40" fill="red"/>
        </svg>"#;

        let data = export_to_webp(svg, None, 1.0, None, None, 0).expect("webp encode");
        assert!(!data.is_empty());
        // RIFF container header + WEBP fourcc
        assert_eq!(&data[0..4], b"RIFF");
        assert_eq!(&data[8..12], b"WEBP");
    }

    #[test]
    fn test_export_to_webp_with_scale() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <rect x="10" y="10" width="80" height="80" fill="blue"/>
        </svg>"#;

        let data = export_to_webp(svg, None, 2.0, None, None, 0).expect("webp encode");
        assert_eq!(&data[0..4], b"RIFF");
    }

    #[test]
    fn test_export_to_webp_with_explicit_dimensions() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
            <line x1="0" y1="0" x2="100" y2="100" stroke="black"/>
        </svg>"#;

        let data = export_to_webp(svg, Some((Some(200), Some(200))), 1.0, None, None, 0)
            .expect("webp encode");
        assert_eq!(&data[0..4], b"RIFF");
    }

    #[test]
    fn test_export_to_webp_invalid_svg() {
        assert!(export_to_webp("not valid svg", None, 1.0, None, None, 0).is_err());
    }

    /// Smoke test: raster export must accept SVG text without crashing when
    /// system fonts are absent, sparse, or mapped differently across hosts.
    /// A stronger glyph-visibility assertion is too environment-dependent for
    /// CI because both "with fonts" and "without fonts" can legitimately
    /// rasterize to the same blank image on minimal runners.
    #[test]
    fn test_export_renders_text_glyphs() {
        let svg = r##"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="80">
            <rect width="200" height="80" fill="#ffffff"/>
            <text x="20" y="50" font-size="32" fill="#000000">Hello</text>
        </svg>"##;

        let with_fonts = export_to_png(svg, None, 1.0, None, None, 0).unwrap();
        assert!(!with_fonts.is_empty());
        assert_eq!(&with_fonts[0..8], &[137, 80, 78, 71, 13, 10, 26, 10]);
    }

    #[test]
    fn test_export_to_png_bleed_params_changes_output() {
        // Bleed pass must actually alter the rendered pixels. Same SVG + same
        // seed, only difference is bleed_params None vs sumi-equivalent Some.
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
            <circle cx="25" cy="25" r="15" fill="black"/>
        </svg>"#;

        let without = export_to_png(svg, None, 1.0, None, None, 42).expect("plain png");
        let with = export_to_png(
            svg,
            None,
            1.0,
            None,
            Some(AquarelleBleedParams {
                radius: 3.0,
                intensity: 0.3,
                halo: 0.0,
            }),
            42,
        )
        .expect("bleed png");

        assert_ne!(without, with);
    }

    #[test]
    fn test_export_to_png_bleed_deterministic_same_seed() {
        // Same SVG + same params + same seed must produce byte-identical PNG.
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
            <circle cx="25" cy="25" r="15" fill="black"/>
        </svg>"#;

        let params = AquarelleBleedParams {
            radius: 6.0,
            intensity: 0.5,
            halo: 0.4,
        };
        let a = export_to_png(svg, None, 1.0, None, Some(params), 12345).expect("a");
        let b = export_to_png(svg, None, 1.0, None, Some(params), 12345).expect("b");

        assert_eq!(a, b);
    }

    #[test]
    fn test_export_to_png_bleed_differs_by_seed() {
        // Bleed pass injects seed-dependent paper-grain noise, so two different
        // seeds with the same params must produce different pixel data.
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
            <circle cx="25" cy="25" r="15" fill="black"/>
        </svg>"#;

        let params = AquarelleBleedParams {
            radius: 6.0,
            intensity: 0.5,
            halo: 0.4,
        };
        let a = export_to_png(svg, None, 1.0, None, Some(params), 1).expect("seed 1");
        let b = export_to_png(svg, None, 1.0, None, Some(params), 999_999).expect("seed 999999");

        assert_ne!(a, b);
    }

    #[test]
    fn test_export_to_webp_bleed_params_changes_output() {
        // WebP path must apply the bleed pass symmetrically to the PNG path.
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
            <circle cx="25" cy="25" r="15" fill="black"/>
        </svg>"#;

        let without = export_to_webp(svg, None, 1.0, None, None, 42).expect("plain webp");
        let with = export_to_webp(
            svg,
            None,
            1.0,
            None,
            Some(AquarelleBleedParams {
                radius: 3.0,
                intensity: 0.3,
                halo: 0.0,
            }),
            42,
        )
        .expect("bleed webp");

        assert_ne!(without, with);
    }

    #[test]
    fn test_export_to_webp_bleed_deterministic_same_seed() {
        // Mirror of the PNG determinism test: same SVG + same params + same
        // seed must produce byte-identical WebP. WebP encoding is lossless
        // here, so any drift would point at non-determinism in the bleed pass
        // itself rather than the encoder.
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
            <circle cx="25" cy="25" r="15" fill="black"/>
        </svg>"#;

        let params = AquarelleBleedParams {
            radius: 6.0,
            intensity: 0.5,
            halo: 0.4,
        };
        let a = export_to_webp(svg, None, 1.0, None, Some(params), 12345).expect("a");
        let b = export_to_webp(svg, None, 1.0, None, Some(params), 12345).expect("b");

        assert_eq!(a, b);
    }

    #[test]
    fn test_export_seed_fallback_equivalent_to_seed_42() {
        // run_pipeline collapses `style.seed = None` to a fixed 42 fallback
        // before calling export. We assert the export layer is deterministic
        // under that exact contract: two PNG outputs with seed=42 (the
        // fallback value) must match byte-for-byte, so a None caller can rely
        // on the fallback being reproducible.
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50">
            <circle cx="25" cy="25" r="15" fill="black"/>
        </svg>"#;

        let params = AquarelleBleedParams {
            radius: 3.0,
            intensity: 0.3,
            halo: 0.0,
        };
        let a = export_to_png(svg, None, 1.0, None, Some(params), 42).expect("a");
        let b = export_to_png(svg, None, 1.0, None, Some(params), 42).expect("b");

        assert_eq!(a, b);
    }

    #[test]
    fn test_export_invalid_svg_with_bleed_params_still_errs() {
        // Invalid SVG must error out before the bleed pass runs (no panic,
        // no partial output written to a pixmap that never existed).
        let result = export_to_png(
            "not valid svg",
            None,
            1.0,
            None,
            Some(AquarelleBleedParams {
                radius: 6.0,
                intensity: 0.5,
                halo: 0.4,
            }),
            42,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_font_dir_arg_does_not_break_export() {
        // Smoke test only — passing a non-existent dir must not crash; usvg's
        // load_fonts_dir silently ignores missing paths. The image still renders
        // (system fonts cover any required face).
        let svg = r##"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="40">
            <rect width="80" height="40" fill="#ffffff"/>
            <text x="10" y="25" font-size="14" fill="#000000">Hi</text>
        </svg>"##;
        let bogus = Path::new("/this/path/does/not/exist/blueprinter-test");
        let data = export_to_png(svg, None, 1.0, Some(bogus), None, 0).expect("export");
        assert!(!data.is_empty());
    }
}