Skip to content

Commit

Permalink
Added margins option for DataMatrixWriter
Browse files Browse the repository at this point in the history
By default margins = 0 to match previous behavior.
If margins > 0, behavior matches with QRCodeWriter margins.
  • Loading branch information
snail-with-tea committed Dec 22, 2024
1 parent f7e014f commit 287e57b
Showing 1 changed file with 21 additions and 5 deletions.
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 287e57b

Please sign in to comment.