From a228f6300a6f04329d177a8c1966a84caae78bf5 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 26 Sep 2022 20:49:25 -0500 Subject: [PATCH] encoder test pass --- src/aztec/EncoderTest.rs | 42 ++++++++++------------- src/aztec/decoder.rs | 45 ++++--------------------- src/aztec/encoder/high_level_encoder.rs | 17 +--------- src/aztec/encoder/state.rs | 1 - 4 files changed, 26 insertions(+), 79 deletions(-) diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index 90c7337..154c923 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -14,28 +14,6 @@ * limitations under the License. */ -// package com.google.zxing.aztec.encoder; - -// import com.google.zxing.BarcodeFormat; -// import com.google.zxing.EncodeHintType; -// import com.google.zxing.FormatException; -// import com.google.zxing.RXingResultPoint; -// import com.google.zxing.aztec.AztecDetectorRXingResult; -// import com.google.zxing.aztec.AztecWriter; -// import com.google.zxing.aztec.decoder.Decoder; -// import com.google.zxing.common.BitArray; -// import com.google.zxing.common.BitMatrix; -// import com.google.zxing.common.DecoderRXingResult; -// import org.junit.Assert; -// import org.junit.Test; - -// import java.nio.charset.Charset; -// import java.nio.charset.StandardCharsets; -// import java.util.EnumMap; -// import java.util.Map; -// import java.util.Random; -// import java.util.regex.Pattern; - use std::collections::HashMap; use encoding::EncodingRef; @@ -168,8 +146,24 @@ fn testAztecWriter() { testWriter("Espa\u{00F1}ol", None, 25, true, 1); // Without ECI (implicit ISO-8859-1) testWriter("Espa\u{00F1}ol", Some(ISO_8859_1), 25, true, 1); // Explicit ISO-8859-1 testWriter("\u{20AC} 1 sample data.", Some(WINDOWS_1252), 25, true, 2); // ISO-8859-1 can't encode Euro; Windows-1252 can + testWriter("\u{20AC} 1 sample data.", Some(WINDOWS_1252), 5, true, 2); // ISO-8859-1 can't encode Euro; Windows-1252 can testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 25, true, 2); testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 0, true, 2); + testWriter( + "\u{20AC} 1 sample data.", + Some(encoding::all::UTF_16BE), + 0, + true, + 3, + ); + testWriter("Espa\u{00F1}ol", Some(UTF_8), 25, true, 2); + testWriter("\u{20AC} 1 sample data", Some(UTF_8), 0, true, 2); + testWriter("\u{20AC} 1 sample data. ", Some(UTF_8), 0, true, 2); + testWriter("\u{20AC} 1 sample data .", Some(UTF_8), 0, true, 2); + testWriter("\u{20AC} 1 sample data-.", Some(UTF_8), 0, true, 2); + testWriter("\u{20AC} 1 sample datA.", Some(UTF_8), 0, true, 2); + testWriter("\u{20AC} 1 sample dat1.", Some(UTF_8), 0, true, 2); + testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 0, true, 2); testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 25, true, 2); testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 100, true, 3); testWriter("\u{20AC} 1 sample data.", Some(UTF_8), 300, true, 4); @@ -756,7 +750,7 @@ fn testWriter( assert_eq!(layers, aztec.getLayers(), "Unexpected nr. of layers"); let matrix2 = aztec.getMatrix(); assert_eq!(&matrix, matrix2); - + let mut r = AztecDetectorRXingResult::new( matrix.clone(), NO_POINTS, @@ -767,7 +761,7 @@ fn testWriter( let mut res = decoder::decode(&r).expect("should decode"); assert_eq!(data, res.getText()); - + // Check error correction by introducing up to eccPercent/2 errors let ec_words = aztec.getCodeWords() * ecc_percent / 100 / 2; let mut random = getPseudoRandom(); diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index bd9ba1a..62e90e7 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -122,7 +122,8 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { let mut encdr: &'static dyn encoding::Encoding = encoding::all::ISO_8859_1; let mut index = 0; - while index < end_index { + + 'main: while index < end_index { if shift_table == Table::BINARY { if end_index - index < 5 { break; @@ -139,8 +140,8 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { for _char_count in 0..length { // for (int charCount = 0; charCount < length; charCount++) { if end_index - index < 8 { - index = end_index; // Force outer loop to exit - break; + //index = end_index; // Force outer loop to exit + break 'main; } let code = read_code(corrected_bits, index, 8); decoded_bytes.push(code as u8); @@ -163,18 +164,12 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { let mut n = read_code(corrected_bits, index, 3); index += 3; // flush bytes, FLG changes state - // try { result.push_str( &encdr .decode(&decoded_bytes, encoding::DecoderTrap::Strict) .unwrap(), ); - - // result.push_str(&String::from_utf8(decoded_bytes.clone()).unwrap()); - // result.append(decodedBytes.toString(encoding.name())); - // } catch (UnsupportedEncodingException uee) { - // throw new IllegalStateException(uee); - // } + decoded_bytes.clear(); match n { 0 => result.push(29 as char), // translate FNC1 as ASCII 29 @@ -208,35 +203,8 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { )); } encdr = CharacterSetECI::getCharset(&charset_eci?); - // encdr = charsetECI?::getCharset(); } } - // switch (n) { - // case 0: - // result.append(29 as char); // translate FNC1 as ASCII 29 - // break; - // case 7: - // throw FormatException.getFormatInstance(); // FLG(7) is reserved and illegal - // default: - // // ECI is decimal integer encoded as 1-6 codes in DIGIT mode - // int eci = 0; - // if (endIndex - index < 4 * n) { - // break; - // } - // while (n-- > 0) { - // int nextDigit = readCode(correctedBits, index, 4); - // index += 4; - // if (nextDigit < 2 || nextDigit > 11) { - // throw FormatException.getFormatInstance(); // Not a decimal digit - // } - // eci = eci * 10 + (nextDigit - 2); - // } - // CharacterSetECI charsetECI = CharacterSetECI.getCharacterSetECIByValue(eci); - // if (charsetECI == null) { - // throw FormatException.getFormatInstance(); - // } - // encoding = charsetECI.getCharset(); - // } // Go back to whatever mode we had been in shift_table = latch_table; } else if str.starts_with("CTRL_") { @@ -251,6 +219,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { } } else { // Though stored as a table of strings for convenience, codes actually represent 1 or 2 *bytes*. + // let b = encoding::all::ASCII.encode(str, encoding::EncoderTrap::Strict).unwrap(); let b = str.as_bytes(); //let b = str.getBytes(StandardCharsets.US_ASCII); //decodedBytes.write(b, 0, b.length); @@ -430,7 +399,7 @@ fn correct_bits( // next codewordSize-1 bits are all zeros or all ones corrected_bits.splice( index..index + codeword_size - 1, - vec![data_word > 1; codeword_size], + vec![data_word > 1; codeword_size-1], ); // Arrays.fill(correctedBits, index, index + codewordSize - 1, dataWord > 1); index += codeword_size - 1; diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index 5bd6cb1..da73602 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -278,22 +278,7 @@ impl HighLevelEncoder { b':' if next_char == b' ' => 5, _ => 0, }; - // switch (text[index]) { - // case '\r': - // pairCode = nextChar == '\n' ? 2 : 0; - // break; - // case '.' : - // pairCode = nextChar == ' ' ? 3 : 0; - // break; - // case ',' : - // pairCode = nextChar == ' ' ? 4 : 0; - // break; - // case ':' : - // pairCode = nextChar == ' ' ? 5 : 0; - // break; - // default: - // pairCode = 0; - // } + if pair_code > 0 { // We have one of the four special PUNCT pairs. Treat them specially. // Get a new set of states for the two new characters. diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index 93cc7b5..a6333ab 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -87,7 +87,6 @@ impl State { let eci_digits = encoding::all::ISO_8859_1 .encode(&format!("{}", eci), encoding::EncoderTrap::Strict) .unwrap(); - dbg!(format!("{}", eci)); // let eciDigits = Integer.toString(eci).getBytes(StandardCharsets.ISO_8859_1); token.add(eci_digits.len() as i32, 3); // 1-6: number of ECI digits for eci_digit in &eci_digits {