diff --git a/src/aztec/aztec_writer.rs b/src/aztec/aztec_writer.rs index 223bd39f..c6e3fb05 100644 --- a/src/aztec/aztec_writer.rs +++ b/src/aztec/aztec_writer.rs @@ -24,6 +24,9 @@ use crate::{ use super::encoder::{aztec_encoder, AztecCode}; +// make default 0 to match previous behavior +const MARGINS_SIZE: u32 = 0; + /** * Renders an Aztec code as a {@link BitMatrix}. */ @@ -69,11 +72,22 @@ impl Writer for AztecWriter { { layers = *az_layers; } + + let margins = + if let Some(EncodeHintValue::Margin(margin)) = hints.get(&EncodeHintType::MARGIN) { + margin + .parse::() + .map_err(|e| Exceptions::parse_with(format!("could not parse {margin}: {e}")))? + } else { + MARGINS_SIZE + }; + encode( contents, *format, width as u32, height as u32, + margins, charset, ecc_percent, layers, @@ -81,11 +95,13 @@ impl Writer for AztecWriter { } } +#[allow(clippy::too_many_arguments)] fn encode( contents: &str, format: BarcodeFormat, width: u32, height: u32, + margins: u32, charset: Option, ecc_percent: u32, layers: i32, @@ -101,18 +117,20 @@ fn encode( } else { aztec_encoder::encode(contents, ecc_percent, layers)? }; - renderRXingResult(&aztec, width, height) + renderRXingResult(&aztec, width, height, margins) } -fn renderRXingResult(code: &AztecCode, width: u32, height: u32) -> Result { +fn renderRXingResult(code: &AztecCode, width: u32, height: u32, margins: u32) -> Result { let input = code.getMatrix(); let input_width = input.getWidth(); let input_height = input.getHeight(); - let output_width = width.max(input_width); - let output_height = height.max(input_height); + let padded_width = input_width + (margins * 2); + let padded_height = input_height + (margins * 2); + let output_width = width.max(padded_width); + let output_height = height.max(padded_height); - let multiple = (output_width / input_width).min(output_height / input_height); + let multiple = (output_width / padded_width).min(output_height / padded_height); let left_padding = (output_width - (input_width * multiple)) / 2; let top_padding = (output_height - (input_height * multiple)) / 2; diff --git a/src/datamatrix/data_matrix_writer.rs b/src/datamatrix/data_matrix_writer.rs index e14c9046..3cb6bd71 100644 --- a/src/datamatrix/data_matrix_writer.rs +++ b/src/datamatrix/data_matrix_writer.rs @@ -30,6 +30,9 @@ use super::encoder::{ use super::encoder::error_correction; +// make default 0 to match previous behavior +const MARGINS_SIZE: u32 = 0; + /** * This object renders a Data Matrix code as a BitMatrix 2D array of greyscale values. * @@ -177,8 +180,17 @@ impl Writer for DataMatrixWriter { ); placement.place()?; + let margins = + if let Some(EncodeHintValue::Margin(margin)) = hints.get(&EncodeHintType::MARGIN) { + margin + .parse::() + .map_err(|e| Exceptions::parse_with(format!("could not parse {margin}: {e}")))? + } else { + MARGINS_SIZE + }; + //4. step: low-level encoding - Self::encodeLowLevel(&placement, symbolInfo, width as u32, height as u32) + Self::encodeLowLevel(&placement, symbolInfo, width as u32, height as u32, margins) } } @@ -195,6 +207,7 @@ impl DataMatrixWriter { symbolInfo: &SymbolInfo, width: u32, height: u32, + margins: u32, ) -> Result { let symbolWidth = symbolInfo.getSymbolDataWidth()?; let symbolHeight = symbolInfo.getSymbolDataHeight()?; @@ -246,7 +259,7 @@ impl DataMatrixWriter { } } - Self::convertByteMatrixToBitMatrix(&matrix, width, height) + Self::convertByteMatrixToBitMatrix(&matrix, width, height, margins) } /** @@ -261,13 +274,16 @@ impl DataMatrixWriter { matrix: &ByteMatrix, reqWidth: u32, reqHeight: u32, + margins: u32, ) -> Result { let matrixWidth = matrix.getWidth(); let matrixHeight = matrix.getHeight(); - let outputWidth = reqWidth.max(matrixWidth); - let outputHeight = reqHeight.max(matrixHeight); + let paddedWidth = matrixWidth + (margins * 2); + let paddedHeight = matrixHeight + (margins * 2); + let outputWidth = reqWidth.max(paddedWidth); + let outputHeight = reqHeight.max(paddedHeight); - let multiple = (outputWidth / matrixWidth).min(outputHeight / matrixHeight); + let multiple = (outputWidth / paddedWidth).min(outputHeight / paddedHeight); let mut leftPadding = (outputWidth - (matrixWidth * multiple)) / 2; let mut topPadding = (outputHeight - (matrixHeight * multiple)) / 2;