//! Text to module matrix, and the geometry shared by the raster and SVG renderers. use qrcode::bits::Bits; use qrcode::types::QrError as RawError; use qrcode::{Color, EcLevel, QrCode, Version}; use crate::{LOGO_FRACTION, Level, MAX_QUIET_ZONE, MAX_SIZE_PX, QrError, Style}; /// The module matrix, row-major, `true` = dark. pub(crate) struct Matrix { pub width: u32, pub dark: Vec, } impl Matrix { pub fn is_dark(&self, x: u32, y: u32) -> bool { self.dark[(y * self.width + x) as usize] } } /// Encodes `text` at `level`. ASCII uses the encoder's optimal segmentation (numeric, alphanumeric, byte); anything /// else is one UTF-8 byte segment, because the optimiser would put some UTF-8 byte pairs in Kanji mode, which phone /// scanners turn into Japanese characters. pub(crate) fn encode(text: &str, level: Level, logo: bool) -> Result { if text.trim().is_empty() { return Err(QrError::Empty); } let ec = level.ec(); let code = if text.is_ascii() { QrCode::with_error_correction_level(text.as_bytes(), ec) } else { byte_mode(text.as_bytes(), ec) } .map_err(|e| match e { RawError::DataTooLong => QrError::TooLong { level, logo }, other => QrError::Encode(other.to_string()), })?; let width = code.width() as u32; let dark = code.to_colors().into_iter().map(|c| c == Color::Dark).collect(); Ok(Matrix { width, dark }) } fn byte_mode(data: &[u8], ec: EcLevel) -> qrcode::QrResult { for v in 1..=40 { let mut bits = Bits::new(Version::Normal(v)); if bits.push_byte_data(data).is_ok() && bits.push_terminator(ec).is_ok() { return QrCode::with_bits(bits, ec); } } Err(RawError::DataTooLong) } /// Pixel geometry of one rendering. pub(crate) struct Geometry { /// Modules across the code, quiet zone excluded. pub modules: u32, pub quiet: u32, /// Pixels per module. pub scale: u32, } impl Geometry { pub fn new(modules: u32, style: &Style) -> Result { if style.quiet_zone > MAX_QUIET_ZONE { return Err(QrError::QuietZoneTooLarge { max: MAX_QUIET_ZONE }); } if style.size_px > MAX_SIZE_PX { return Err(QrError::SizeTooLarge { max_px: MAX_SIZE_PX }); } let total = modules + 2 * style.quiet_zone; let scale = style.size_px / total; if scale == 0 { return Err(QrError::SizeTooSmall { min_px: total }); } Ok(Geometry { modules, quiet: style.quiet_zone, scale }) } /// Modules across the whole image, quiet zone included. pub fn total_modules(&self) -> u32 { self.modules + 2 * self.quiet } pub fn size_px(&self) -> u32 { self.total_modules() * self.scale } } /// Where the logo goes. The plate is whole modules (all cleared to the light colour), an odd number across so it /// sits exactly in the middle, with at least half a module of padding around the logo. pub(crate) struct Plate { /// Plate, in modules, relative to the code's top-left corner (quiet zone excluded). pub x: u32, pub y: u32, pub w: u32, pub h: u32, /// The logo's box, in pixels, relative to the code's top-left corner (quiet zone excluded). pub logo_x: u32, pub logo_y: u32, pub logo_w: u32, pub logo_h: u32, } impl Plate { pub fn new(g: &Geometry, logo_w: u32, logo_h: u32) -> Plate { let code_px = f64::from(g.modules * g.scale); let max_px = code_px * LOGO_FRACTION; let k = max_px / f64::from(logo_w.max(logo_h)); let lw = ((f64::from(logo_w) * k).floor() as u32).max(1); let lh = ((f64::from(logo_h) * k).floor() as u32).max(1); let s = f64::from(g.scale); let odd = |n: u32| if n.is_multiple_of(2) { n + 1 } else { n }; let w = odd((f64::from(lw) / s + 1.0).ceil() as u32).min(g.modules); let h = odd((f64::from(lh) / s + 1.0).ceil() as u32).min(g.modules); let (x, y) = ((g.modules - w) / 2, (g.modules - h) / 2); Plate { x, y, w, h, logo_x: x * g.scale + (w * g.scale - lw) / 2, logo_y: y * g.scale + (h * g.scale - lh) / 2, logo_w: lw, logo_h: lh, } } pub fn contains(&self, mx: u32, my: u32) -> bool { mx >= self.x && mx < self.x + self.w && my >= self.y && my < self.y + self.h } } /// Encodes for a rendering: checks the colours, forces level H with a logo, and lays out the pixels. pub(crate) fn prepare( text: &str, level: Level, style: &Style, logo: bool, ) -> Result<(Matrix, Geometry, Level), QrError> { crate::colour::check(style.dark, style.light)?; let level = if logo { Level::H } else { level }; let matrix = encode(text, level, logo)?; let geometry = Geometry::new(matrix.width, style)?; Ok((matrix, geometry, level)) }