lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
use super::*;
use crate::composition::{ImageAsset, PanelImage};
use base64::{engine::general_purpose::STANDARD, Engine as _};

#[test]
fn asset_font_is_sealed_with_its_css_alias_and_embedding_permission() {
    let regular = build_test_font_with_coverage("Private asset face", &['A']);
    for permitted in [true, false] {
        let mut tables = super::admission::tables(&regular);
        let os2 = &mut tables.iter_mut().find(|(tag, _)| tag == b"OS/2").unwrap().1;
        // The source's own OpenType permission is the input to preserve-text
        // admission. Outlining is a distinct device operation.
        set_u16(os2, 8, if permitted { 0 } else { 2 });
        let font = build_sfnt(tables);
        let directory = tempfile::tempdir().unwrap();
        fs::create_dir(directory.path().join("styles")).unwrap();
        fs::write(directory.path().join("shape.ttf"), &font).unwrap();
        fs::write(directory.path().join("styles/paper.css"),
            "@font-face { font-family: 'Local alias'; src: url('../shape.ttf'); } text { font-family: 'Local alias'; font-size: 20px; }",
        ).unwrap();
        let path = directory.path().join("drawing.svg");
        fs::write(
            &path,
            r#"<?xml-stylesheet href="styles/paper.css" type="text/css"?>
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="32"><text x="0" y="22">A</text></svg>"#,
        )
        .unwrap();
        let asset = ImageAsset::from_path(&path).unwrap();
        directory.close().unwrap();
        let output = crate::Canvas::grid(1, 1)
            .unwrap()
            .size(160, 100)
            .panel(0, 0, PanelImage::new(asset).size(80., 64.))
            .unwrap()
            .render()
            .unwrap();
        assert!(output.image_font_substitutions().is_empty());
        let target = tempfile::tempdir().unwrap();
        let outlined = target.path().join("outline.svg");
        output.svg(&outlined).unwrap();
        output
            .png_preview(target.path().join("raster.png"))
            .unwrap();
        let preserved = target.path().join("preserve.svg");
        if permitted {
            output.svg_preserve_text(&preserved).unwrap();
            let file = fs::read_to_string(preserved).unwrap();
            let doc = roxmltree::Document::parse(&file).unwrap();
            let image = doc
                .descendants()
                .find(|node| node.has_tag_name("image"))
                .unwrap();
            let url = data_url::DataUrl::process(image.attribute("href").unwrap()).unwrap();
            let nested = String::from_utf8(url.decode_to_vec().unwrap().0).unwrap();
            assert!(nested.contains("Local alias"));
            assert!(nested.contains(&STANDARD.encode(&font)));
            assert!(nested.contains(">A</text>"));
        } else {
            fs::write(&preserved, b"existing").unwrap();
            let error = output.svg_preserve_text(&preserved).unwrap_err();
            assert!(error.to_string().contains("embedding permissions"));
            assert_eq!(fs::read(preserved).unwrap(), b"existing");
        }
    }
}

#[test]
fn missing_visible_asset_glyph_refuses_only_outline_devices() {
    // Some host fonts (including last-resort faces) cover Unicode
    // noncharacters. Use an actual finite cmap rather than guessing which
    // scalar the host cannot supply.
    let font = build_test_font_with_coverage("Asset fixture", &['A']);
    super::super::svg::with_fixture_fonts(vec![font], || {
        let render = |scalar| {
            let source = format!("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"32\"><text x=\"1\" y=\"22\" font-family=\"Asset fixture\" font-size=\"20\">{scalar}</text></svg>");
            let asset = ImageAsset::from_svg_bytes(source.as_bytes()).unwrap();
            crate::Canvas::grid(1, 1)
                .unwrap()
                .size(160, 100)
                .panel(0, 0, PanelImage::new(asset).size(80., 64.))
                .unwrap()
                .render()
                .unwrap()
        };
        let folder = tempfile::tempdir().unwrap();
        let covered = render('A');
        covered.svg(folder.path().join("covered.svg")).unwrap();
        covered
            .png_preview(folder.path().join("covered.png"))
            .unwrap();

        let output = render('B');
        output
            .svg_preserve_text(folder.path().join("author.svg"))
            .unwrap();
        let error = output.svg(folder.path().join("outline.svg")).unwrap_err();
        assert!(error.to_string().contains("glyph"), "{error}");
        assert!(output
            .png_preview(folder.path().join("raster.png"))
            .is_err());
    });
}