Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added margins option to DataMatrixWriter & AztecWriter #60

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading