fixed datamatrix detection (incorrect) in ean13 2

This commit is contained in:
Henry Schimke
2022-12-29 17:33:45 -06:00
parent 3469f7c599
commit 09f25bfacc
8 changed files with 120 additions and 195 deletions

View File

@@ -37,7 +37,16 @@ pub struct EncoderContext<'a> {
skipAtEnd: u32,
}
impl EncoderContext<'_> {
impl<'a> EncoderContext<'_> {
pub fn with_symbol_info_lookup(
msg: &str,
symbol_lookup: Rc<SymbolInfoLookup<'a>>,
) -> Result<EncoderContext<'a>, Exceptions> {
let mut new_self = EncoderContext::new(msg)?;
new_self.symbol_lookup = symbol_lookup.clone();
Ok(new_self)
}
pub fn new(msg: &str) -> Result<Self, Exceptions> {
//From this point on Strings are not Unicode anymore!
// let msgBinary = ISO_8859_1_ENCODER.encode(msg, encoding::EncoderTrap::Strict).expect("encode to bytes");//msg.getBytes(StandardCharsets.ISO_8859_1);

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
use std::rc::Rc;
use lazy_static::lazy_static;
use crate::datamatrix::encoder::{SymbolInfo, SymbolShapeHint};
@@ -38,15 +40,17 @@ lazy_static! {
// const SIL: SymbolInfoLookup = SymbolInfoLookup::new();
// fn useTestSymbols(lookup: SymbolInfoLookup) -> SymbolInfoLookup {
// lookup.overrideSymbolSet(&TEST_SYMBOLS);
// lookup
// }
fn useTestSymbols(lookup: SymbolInfoLookup) -> SymbolInfoLookup {
let mut lookup = lookup;
lookup.overrideSymbolSet(&TEST_SYMBOLS);
lookup
}
// fn resetSymbols(lookup: SymbolInfoLookup) -> SymbolInfoLookup {
// lookup.overrideSymbolSet(&symbol_info::PROD_SYMBOLS);
// lookup
// }
fn resetSymbols(lookup: SymbolInfoLookup) -> SymbolInfoLookup {
let mut lookup = lookup;
lookup.overrideSymbolSet(&symbol_info::PROD_SYMBOLS);
lookup
}
#[test]
fn testASCIIEncodation() {
@@ -110,34 +114,37 @@ fn testC40EncodationSpecExample() {
#[test]
fn testC40EncodationSpecialCases1() {
unimplemented!();
// //Special tests avoiding ultra-long test strings because these tests are only used
// //with the 16x48 symbol (47 data codewords)
// useTestSymbols();
//Special tests avoiding ultra-long test strings because these tests are only used
//with the 16x48 symbol (47 data codewords)
let substitute_symbols = SymbolInfoLookup::new();
let substitute_symbols = useTestSymbols(substitute_symbols);
// let visualized = encodeHighLevelCompare("AIMAIMAIMAIMAIMAIM", false);
// assert_eq!("230 91 11 91 11 91 11 91 11 91 11 91 11", visualized);
// //case "a": Unlatch is not required
let sil = Rc::new(substitute_symbols.clone());
// visualized = encodeHighLevelCompare("AIMAIMAIMAIMAIMAI", false);
// assert_eq!("230 91 11 91 11 91 11 91 11 91 11 90 241", visualized);
// //case "b": Add trailing shift 0 and Unlatch is not required
let visualized = encodeHighLevelCompareSIL("AIMAIMAIMAIMAIMAIM", false, Some(sil.clone()));
assert_eq!("230 91 11 91 11 91 11 91 11 91 11 91 11", visualized);
//case "a": Unlatch is not required
// visualized = encodeHighLevel("AIMAIMAIMAIMAIMA");
// assert_eq!("230 91 11 91 11 91 11 91 11 91 11 254 66", visualized);
// //case "c": Unlatch and write last character in ASCII
let visualized = encodeHighLevelCompareSIL("AIMAIMAIMAIMAIMAI", false, Some(sil.clone()));
assert_eq!("230 91 11 91 11 91 11 91 11 91 11 90 241", visualized);
//case "b": Add trailing shift 0 and Unlatch is not required
// resetSymbols();
let visualized = encodeHighLevelSIL("AIMAIMAIMAIMAIMA", sil.clone());
assert_eq!("230 91 11 91 11 91 11 91 11 91 11 254 66", visualized);
//case "c": Unlatch and write last character in ASCII
// visualized = encodeHighLevel("AIMAIMAIMAIMAIMAI");
// assert_eq!(
// "230 91 11 91 11 91 11 91 11 91 11 254 66 74 129 237",
// visualized
// );
let substitute_symbols = resetSymbols(substitute_symbols);
let sil = Rc::new(substitute_symbols);
// visualized = encodeHighLevel("AIMAIMAIMA");
// assert_eq!("230 91 11 91 11 91 11 66", visualized);
// //case "d": Skip Unlatch and write last character in ASCII
let visualized = encodeHighLevelSIL("AIMAIMAIMAIMAIMAI", sil.clone());
assert_eq!(
"230 91 11 91 11 91 11 91 11 91 11 254 66 74 129 237",
visualized
);
let visualized = encodeHighLevelSIL("AIMAIMAIMA", sil);
assert_eq!("230 91 11 91 11 91 11 66", visualized);
//case "d": Skip Unlatch and write last character in ASCII
}
#[test]
@@ -556,8 +563,16 @@ fn encodeHighLevel(msg: &str) -> String {
encodeHighLevelCompare(msg, true)
}
fn encodeHighLevelCompare(msg: &str, compareSizeToMinimalEncoder: bool) -> String {
let encoded = high_level_encoder::encodeHighLevel(msg).expect("encodes");
fn encodeHighLevelSIL(msg: &str, sil: Rc<SymbolInfoLookup>) -> String {
encodeHighLevelCompareSIL(msg, true, Some(sil))
}
fn encodeHighLevelCompareSIL(
msg: &str,
compareSizeToMinimalEncoder: bool,
sil: Option<Rc<SymbolInfoLookup>>,
) -> String {
let encoded = high_level_encoder::encodeHighLevelSIL(msg, sil).expect("encodes");
let encoded2 = minimal_encoder::encodeHighLevel(msg).expect("encodes");
assert!(
!compareSizeToMinimalEncoder || encoded2.chars().count() <= encoded.chars().count(),
@@ -568,6 +583,10 @@ fn encodeHighLevelCompare(msg: &str, compareSizeToMinimalEncoder: bool) -> Strin
visualize(&encoded)
}
fn encodeHighLevelCompare(msg: &str, compareSizeToMinimalEncoder: bool) -> String {
encodeHighLevelCompareSIL(msg, compareSizeToMinimalEncoder, None)
}
/**
* Convert a string of char codewords into a different string which lists each character
* using its decimal value.

View File

@@ -22,7 +22,7 @@ use crate::{Dimension, Exceptions};
use super::{
ASCIIEncoder, Base256Encoder, C40Encoder, EdifactEncoder, Encoder, EncoderContext,
SymbolShapeHint, TextEncoder, X12Encoder,
SymbolInfoLookup, SymbolShapeHint, TextEncoder, X12Encoder,
};
const DEFAULT_ENCODING: EncodingRef = encoding::all::ISO_8859_1;
@@ -136,6 +136,27 @@ pub fn encodeHighLevel(msg: &str) -> Result<String, Exceptions> {
encodeHighLevelWithDimensionForceC40(msg, SymbolShapeHint::FORCE_NONE, None, None, false)
}
/**
* Performs message encoding of a DataMatrix message using the algorithm described in annex P
* of ISO/IEC 16022:2000(E).
*
* @param msg the message
* @return the encoded message (the char values range from 0 to 255)
*/
pub fn encodeHighLevelSIL(
msg: &str,
symbol_lookup: Option<Rc<SymbolInfoLookup>>,
) -> Result<String, Exceptions> {
encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
msg,
SymbolShapeHint::FORCE_NONE,
None,
None,
false,
symbol_lookup,
)
}
/**
* Performs message encoding of a DataMatrix message using the algorithm described in annex P
* of ISO/IEC 16022:2000(E).
@@ -155,24 +176,14 @@ pub fn encodeHighLevelWithDimension(
) -> Result<String, Exceptions> {
encodeHighLevelWithDimensionForceC40(msg, shape, minSize, maxSize, false)
}
/**
* Performs message encoding of a DataMatrix message using the algorithm described in annex P
* of ISO/IEC 16022:2000(E).
*
* @param msg the message
* @param shape requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
* {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
* @param minSize the minimum symbol size constraint or null for no constraint
* @param maxSize the maximum symbol size constraint or null for no constraint
* @param forceC40 enforce C40 encoding
* @return the encoded message (the char values range from 0 to 255)
*/
pub fn encodeHighLevelWithDimensionForceC40(
pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
msg: &str,
shape: SymbolShapeHint,
minSize: Option<Dimension>,
maxSize: Option<Dimension>,
forceC40: bool,
symbol_lookup: Option<Rc<SymbolInfoLookup>>,
) -> Result<String, Exceptions> {
//the codewords 0..255 are encoded as Unicode characters
let c40Encoder = Rc::new(C40Encoder::new());
@@ -185,7 +196,12 @@ pub fn encodeHighLevelWithDimensionForceC40(
Rc::new(Base256Encoder::new()),
];
let mut context = EncoderContext::new(msg)?;
let mut context = if let Some(symbol_table) = symbol_lookup {
EncoderContext::with_symbol_info_lookup(msg, symbol_table)?
} else {
EncoderContext::new(msg)?
};
// let mut context = EncoderContext::new(msg)?;
context.setSymbolShape(shape);
context.setSizeConstraints(minSize, maxSize);
@@ -241,6 +257,30 @@ pub fn encodeHighLevelWithDimensionForceC40(
Ok(context.getCodewords().to_owned())
}
/**
* Performs message encoding of a DataMatrix message using the algorithm described in annex P
* of ISO/IEC 16022:2000(E).
*
* @param msg the message
* @param shape requested shape. May be {@code SymbolShapeHint.FORCE_NONE},
* {@code SymbolShapeHint.FORCE_SQUARE} or {@code SymbolShapeHint.FORCE_RECTANGLE}.
* @param minSize the minimum symbol size constraint or null for no constraint
* @param maxSize the maximum symbol size constraint or null for no constraint
* @param forceC40 enforce C40 encoding
* @return the encoded message (the char values range from 0 to 255)
*/
pub fn encodeHighLevelWithDimensionForceC40(
msg: &str,
shape: SymbolShapeHint,
minSize: Option<Dimension>,
maxSize: Option<Dimension>,
forceC40: bool,
) -> Result<String, Exceptions> {
encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
msg, shape, minSize, maxSize, forceC40, None,
)
}
pub fn lookAheadTest(msg: &str, startpos: u32, currentMode: u32) -> usize {
let newMode = lookAheadTestIntern(msg, startpos, currentMode);
if currentMode as usize == X12_ENCODATION && newMode as usize == X12_ENCODATION {

View File

@@ -255,6 +255,7 @@ impl fmt::Display for SymbolInfo {
}
}
#[derive(Clone, Copy)]
pub struct SymbolInfoLookup<'a>(Option<&'a Vec<SymbolInfo>>);
impl<'a> SymbolInfoLookup<'a> {
pub const fn new() -> Self {