Skip to content

Commit

Permalink
Merge pull request #60 from snail-with-tea/2d_margins
Browse files Browse the repository at this point in the history
Added margins option to DataMatrixWriter & AztecWriter
  • Loading branch information
hschimke authored Dec 24, 2024
2 parents 4ea071e + 60f2a4e commit bd52b9a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
28 changes: 23 additions & 5 deletions src/aztec/aztec_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*/
Expand Down Expand Up @@ -69,23 +72,36 @@ impl Writer for AztecWriter {
{
layers = *az_layers;
}

let margins =
if let Some(EncodeHintValue::Margin(margin)) = hints.get(&EncodeHintType::MARGIN) {
margin
.parse::<u32>()
.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,
)
}
}

#[allow(clippy::too_many_arguments)]
fn encode(
contents: &str,
format: BarcodeFormat,
width: u32,
height: u32,
margins: u32,
charset: Option<CharacterSet>,
ecc_percent: u32,
layers: i32,
Expand All @@ -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<BitMatrix> {
fn renderRXingResult(code: &AztecCode, width: u32, height: u32, margins: u32) -> Result<BitMatrix> {
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;

Expand Down
26 changes: 21 additions & 5 deletions src/datamatrix/data_matrix_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -177,8 +180,17 @@ impl Writer for DataMatrixWriter {
);
placement.place()?;

let margins =
if let Some(EncodeHintValue::Margin(margin)) = hints.get(&EncodeHintType::MARGIN) {
margin
.parse::<u32>()
.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)
}
}

Expand All @@ -195,6 +207,7 @@ impl DataMatrixWriter {
symbolInfo: &SymbolInfo,
width: u32,
height: u32,
margins: u32,
) -> Result<BitMatrix> {
let symbolWidth = symbolInfo.getSymbolDataWidth()?;
let symbolHeight = symbolInfo.getSymbolDataHeight()?;
Expand Down Expand Up @@ -246,7 +259,7 @@ impl DataMatrixWriter {
}
}

Self::convertByteMatrixToBitMatrix(&matrix, width, height)
Self::convertByteMatrixToBitMatrix(&matrix, width, height, margins)
}

/**
Expand All @@ -261,13 +274,16 @@ impl DataMatrixWriter {
matrix: &ByteMatrix,
reqWidth: u32,
reqHeight: u32,
margins: u32,
) -> Result<BitMatrix> {
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;
Expand Down

0 comments on commit bd52b9a

Please sign in to comment.