[qr] QR: payloads, PNG and SVG, centre logo at level H, round-trip tests
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
//! QR codes for Pictor's Toolbox: payload builders (text, URL, Wi-Fi, contact), raster (RGBA, PNG) and SVG output,
|
||||
//! an optional centre logo (which forces error-correction level H), and a colour check that refuses combinations
|
||||
//! scanners will not read.
|
||||
//!
|
||||
//! Every error is a [`QrError`] whose `Display` is a plain-English sentence meant to be shown to the user.
|
||||
|
||||
mod base64;
|
||||
mod colour;
|
||||
mod encode;
|
||||
mod error;
|
||||
mod payload;
|
||||
mod render;
|
||||
mod svg;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub use colour::{contrast_ok, contrast_ratio};
|
||||
pub use error::QrError;
|
||||
pub use payload::{Payload, WifiSecurity, payload_string};
|
||||
pub use render::{Rendered, encode_png, render_png, render_rgba};
|
||||
pub use svg::render_svg;
|
||||
|
||||
/// The largest output, in pixels across, that the renderers will produce.
|
||||
pub const MAX_SIZE_PX: u32 = 4096;
|
||||
/// The widest quiet zone, in modules, that the renderers accept.
|
||||
pub const MAX_QUIET_ZONE: u32 = 40;
|
||||
/// The largest logo PNG, in bytes, that [`render_svg`] accepts.
|
||||
pub const MAX_LOGO_BYTES: usize = 8 * 1024 * 1024;
|
||||
/// The largest logo, in pixels across either side, that the renderers accept.
|
||||
pub const MAX_LOGO_PX: u32 = 4096;
|
||||
/// The logo's largest side, as a fraction of the code's width (quiet zone excluded).
|
||||
pub const LOGO_FRACTION: f64 = 0.22;
|
||||
|
||||
/// Error-correction level: how much of the code can be damaged or covered and still read.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
|
||||
pub enum Level {
|
||||
/// About 7% recoverable; the most content.
|
||||
L,
|
||||
/// About 15% recoverable; the usual choice.
|
||||
#[default]
|
||||
M,
|
||||
/// About 25% recoverable.
|
||||
Q,
|
||||
/// About 30% recoverable; the least content. Forced when a logo is drawn.
|
||||
H,
|
||||
}
|
||||
|
||||
impl Level {
|
||||
pub(crate) fn ec(self) -> qrcode::EcLevel {
|
||||
match self {
|
||||
Level::L => qrcode::EcLevel::L,
|
||||
Level::M => qrcode::EcLevel::M,
|
||||
Level::Q => qrcode::EcLevel::Q,
|
||||
Level::H => qrcode::EcLevel::H,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Level {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Level::L => "L",
|
||||
Level::M => "M",
|
||||
Level::Q => "Q",
|
||||
Level::H => "H",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// How the code looks.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Style {
|
||||
/// Target width and height in pixels. Modules are scaled by a whole number of pixels, so the output may be a
|
||||
/// little smaller; [`Rendered::size_px`] reports the real size.
|
||||
pub size_px: u32,
|
||||
/// Light border around the code, in modules. Scanners want 4; less works on most phones.
|
||||
pub quiet_zone: u32,
|
||||
/// The modules' colour, RGBA.
|
||||
pub dark: [u8; 4],
|
||||
/// The background (and quiet zone, and logo plate) colour, RGBA. May be transparent.
|
||||
pub light: [u8; 4],
|
||||
}
|
||||
|
||||
impl Default for Style {
|
||||
fn default() -> Self {
|
||||
Style { size_px: 512, quiet_zone: 4, dark: [0, 0, 0, 255], light: [255, 255, 255, 255] }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user