114 lines
4.5 KiB
Rust
114 lines
4.5 KiB
Rust
//! SVG output: the structure, and a rasterisation of its module path that must decode back.
|
|
|
|
mod common;
|
|
|
|
use common::{decode_gray, logo_png};
|
|
use image::{GrayImage, Luma};
|
|
use pictor_qr::{Level, QrError, Style, render_rgba, render_svg};
|
|
|
|
/// The SVG's dark modules as horizontal runs `(x, y, len)` in module units (quiet zone included), parsed from the
|
|
/// path's `M{x} {y}h{len}v1h-{len}z` segments.
|
|
fn runs(svg: &str) -> Vec<(u32, u32, u32)> {
|
|
let start = svg.find(" d=\"").expect("a path with d") + 4;
|
|
let end = start + svg[start..].find('"').unwrap();
|
|
let d = &svg[start..end];
|
|
d.split('M')
|
|
.filter(|s| !s.is_empty())
|
|
.map(|seg| {
|
|
let (xy, rest) = seg.split_once('h').expect("h after M");
|
|
let (x, y) = xy.split_once(' ').expect("x y");
|
|
let (len, tail) = rest.split_once('v').expect("v after h");
|
|
assert_eq!(tail, format!("1h-{len}z"), "segment {seg}");
|
|
(x.parse().unwrap(), y.parse().unwrap(), len.parse().unwrap())
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn view_box_size(svg: &str) -> u32 {
|
|
let start = svg.find("viewBox=\"0 0 ").unwrap() + 13;
|
|
let rest = &svg[start..];
|
|
rest[..rest.find(' ').unwrap()].parse().unwrap()
|
|
}
|
|
|
|
fn rasterise(svg: &str, scale: u32) -> GrayImage {
|
|
let n = view_box_size(svg);
|
|
let mut img = GrayImage::from_pixel(n * scale, n * scale, Luma([255]));
|
|
for (x, y, len) in runs(svg) {
|
|
for py in y * scale..(y + 1) * scale {
|
|
for px in x * scale..(x + len) * scale {
|
|
img.put_pixel(px, py, Luma([0]));
|
|
}
|
|
}
|
|
}
|
|
img
|
|
}
|
|
|
|
#[test]
|
|
fn svg_structure() {
|
|
let text = "Příliš žluťoučký kůň";
|
|
let style = Style { size_px: 400, ..Style::default() };
|
|
let svg = render_svg(text, Level::M, &style, None).unwrap();
|
|
assert!(svg.starts_with("<svg "), "{}", &svg[..40]);
|
|
assert!(svg.trim_end().ends_with("</svg>"));
|
|
assert!(svg.contains("xmlns=\"http://www.w3.org/2000/svg\""));
|
|
assert_eq!(svg.matches("<path").count(), 1);
|
|
assert!(!svg.contains("<image"));
|
|
|
|
// Same geometry as the raster output, and as many dark modules as the raster has.
|
|
let r = render_rgba(text, Level::M, &style, None).unwrap();
|
|
assert_eq!(view_box_size(&svg), r.modules + 8);
|
|
assert!(svg.contains(&format!("width=\"{}\" height=\"{}\"", r.size_px, r.size_px)));
|
|
let dark_svg: u32 = runs(&svg).iter().map(|r| r.2).sum();
|
|
let mut dark_png = 0;
|
|
for my in 0..r.modules + 8 {
|
|
for mx in 0..r.modules + 8 {
|
|
let p = r.image.get_pixel(mx * r.module_px + r.module_px / 2, my * r.module_px + r.module_px / 2);
|
|
if p.0[0] < 128 {
|
|
dark_png += 1;
|
|
}
|
|
}
|
|
}
|
|
assert_eq!(dark_svg, dark_png);
|
|
}
|
|
|
|
#[test]
|
|
fn svg_rasterised_decodes_at_all_levels() {
|
|
let text = "WIFI:T:WPA;S:Domov\\;5G;P:heslo\\:x;;";
|
|
for level in [Level::L, Level::M, Level::Q, Level::H] {
|
|
let svg = render_svg(text, level, &Style::default(), None).unwrap();
|
|
assert_eq!(decode_gray(rasterise(&svg, 6)).0, text, "{level:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn svg_colours() {
|
|
let style = Style { dark: [16, 32, 96, 255], light: [255, 250, 225, 128], ..Style::default() };
|
|
let svg = render_svg("colour", Level::M, &style, None).unwrap();
|
|
assert!(svg.contains("fill=\"#102060\""), "{svg}");
|
|
assert!(svg.contains("fill=\"#fffae1\" fill-opacity=\"0.502\""), "{svg}");
|
|
}
|
|
|
|
#[test]
|
|
fn svg_with_logo_embeds_data_uri_and_clears_modules() {
|
|
let text = "https://example.cz/logo";
|
|
let png = logo_png(120, 60);
|
|
let with = render_svg(text, Level::L, &Style::default(), Some(&png)).unwrap();
|
|
let without_h = render_svg(text, Level::H, &Style::default(), None).unwrap();
|
|
assert_eq!(with.matches("<image").count(), 1);
|
|
assert!(with.contains("xlink:href=\"data:image/png;base64,iVBORw0KGgo"), "a base64 PNG data URI");
|
|
assert!(with.contains("preserveAspectRatio=\"xMidYMid meet\""));
|
|
// Level H was used (same module count as an H code without a logo), and modules under the plate are gone.
|
|
assert_eq!(view_box_size(&with), view_box_size(&without_h));
|
|
let dark_with: u32 = runs(&with).iter().map(|r| r.2).sum();
|
|
let dark_without: u32 = runs(&without_h).iter().map(|r| r.2).sum();
|
|
assert!(dark_with < dark_without);
|
|
// The cleared code (without drawing the logo) still decodes.
|
|
assert_eq!(decode_gray(rasterise(&with, 6)).0, text);
|
|
}
|
|
|
|
#[test]
|
|
fn svg_rejects_a_broken_logo() {
|
|
let err = render_svg("x", Level::M, &Style::default(), Some(b"not a png")).unwrap_err();
|
|
assert!(matches!(err, QrError::Logo(_)), "{err:?}");
|
|
}
|