From 37dc1be0413def3a50513d0d32251a0e46797b9b Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Wed, 1 Mar 2023 20:53:51 -0600 Subject: [PATCH 01/10] add encode / decode methods This will eventually entirely replace EncoderRef being passed around --- src/common/character_set_eci.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 61b607a..2e97b1f 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -26,7 +26,7 @@ use encoding::EncodingRef; use crate::common::Result; -use crate::Exceptions; +use crate::{Exceptions}; /** * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1 @@ -133,8 +133,8 @@ impl CharacterSetECI { } } - pub fn getCharset(cs_eci: &CharacterSetECI) -> EncodingRef { - let name = match cs_eci { + pub fn getCharset(&self, ) -> EncodingRef { + let name = match self { // CharacterSetECI::Cp437 => "CP437", CharacterSetECI::Cp437 => "UTF-8", CharacterSetECI::ISO8859_1 => "ISO-8859-1", @@ -286,4 +286,20 @@ impl CharacterSetECI { _ => None, } } + + pub fn encode(&self, input: &str) -> Result> { + self.getCharset().encode(input, encoding::EncoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } + + pub fn encode_replace(&self, input: &str) -> Result> { + self.getCharset().encode(input, encoding::EncoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + } + + pub fn decode(&self, input:&[u8]) -> Result { + self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } + + pub fn decode_replace(&self, input:&[u8]) -> Result { + self.getCharset().decode(input, encoding::DecoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + } } From 5deb1ddbe2b0b34b227be0d17ab0793082264bf0 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 2 Mar 2023 15:50:56 -0600 Subject: [PATCH 02/10] repurpose CharacterSetECI as encoding abstraction --- src/aztec/EncoderTest.rs | 41 +++---- src/aztec/aztec_writer.rs | 11 +- src/aztec/decoder.rs | 13 +-- src/aztec/encoder/aztec_encoder.rs | 21 ++-- src/aztec/encoder/high_level_encoder.rs | 22 ++-- src/aztec/encoder/state.rs | 8 +- src/client/result/VCardResultParser.rs | 6 +- src/common/StringUtilsTestCase.rs | 26 ++--- src/common/character_set_eci.rs | 106 ++++++++++++------ src/common/eci_encoder_set.rs | 64 +++++------ src/common/eci_string_builder.rs | 34 +++--- src/common/minimal_eci_input.rs | 5 +- src/common/string_utils.rs | 40 +++---- src/datamatrix/data_matrix_writer.rs | 8 +- .../decoder/decoded_bit_stream_parser.rs | 11 +- src/datamatrix/encoder/encoder_context.rs | 9 +- .../encoder/high_level_encode_test_case.rs | 4 +- src/datamatrix/encoder/high_level_encoder.rs | 6 +- src/datamatrix/encoder/minimal_encoder.rs | 16 ++- .../decoder/pdf_417_decoder_test_case.rs | 10 +- src/pdf417/encoder/pdf_417.rs | 8 +- .../encoder/pdf_417_high_level_encoder.rs | 46 ++++---- ...pdf_417_high_level_encoder_test_adapter.rs | 6 +- src/pdf417/pdf_417_writer.rs | 4 +- .../decoder/decoded_bit_stream_parser.rs | 45 +++----- src/qrcode/encoder/EncoderTestCase.rs | 17 ++- src/qrcode/encoder/minimal_encoder.rs | 12 +- src/qrcode/encoder/qrcode_encoder.rs | 35 +++--- tests/common/pdf_417_multiimage_span.rs | 7 +- 29 files changed, 304 insertions(+), 337 deletions(-) diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index bf7be82..00a5763 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -16,8 +16,6 @@ use std::collections::HashMap; -use encoding::EncodingRef; - use crate::{ aztec::{ aztec_detector_result::AztecDetectorRXingResult, @@ -25,7 +23,7 @@ use crate::{ encoder::HighLevelEncoder, shared_test_methods::{stripSpace, toBitArray, toBooleanArray}, }, - BarcodeFormat, EncodeHintType, EncodeHintValue, Point, + BarcodeFormat, EncodeHintType, EncodeHintValue, Point, common::CharacterSetECI, }; use super::{encoder::aztec_encoder, AztecWriter}; @@ -34,8 +32,6 @@ use crate::Writer; use rand::Rng; -use encoding::Encoding; - /** * Aztec 2D generator unit tests. * @@ -43,10 +39,10 @@ use encoding::Encoding; * @author Frank Yellin */ -const ISO_8859_1: EncodingRef = encoding::all::ISO_8859_1; //StandardCharsets.ISO_8859_1; -const UTF_8: EncodingRef = encoding::all::UTF_8; //StandardCharsets.UTF_8; -const ISO_8859_15: EncodingRef = encoding::all::ISO_8859_15; //Charset.forName("ISO-8859-15"); -const WINDOWS_1252: EncodingRef = encoding::all::WINDOWS_1252; //Charset.forName("Windows-1252"); +const ISO_8859_1: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1; +const UTF_8: CharacterSetECI = CharacterSetECI::UTF8; //StandardCharsets.UTF_8; +const ISO_8859_15: CharacterSetECI = CharacterSetECI::ISO8859_15; //Charset.forName("ISO-8859-15"); +const WINDOWS_1252: CharacterSetECI = CharacterSetECI::Cp1252; //Charset.forName("Windows-1252"); // const DOTX: &str = "[^.X]"; // const SPACES: &str = "\\s+"; @@ -140,8 +136,9 @@ X X X X X X X X X X X X X #[test] fn testAztecWriter() { - let shift_jis: EncodingRef = - encoding::label::encoding_from_whatwg_label("Shift_JIS").expect("must exist"); + let shift_jis: CharacterSetECI = + CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); + 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 @@ -150,7 +147,7 @@ fn testAztecWriter() { testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 0, true, 2); testWriter( "\u{20AC} 1 sample data.", - Some(encoding::all::UTF_16BE), + Some(CharacterSetECI::UnicodeBigUnmarked), 0, true, 3, @@ -711,7 +708,7 @@ fn testEncodeDecode(data: &str, compact: bool, layers: u32) { fn testWriter( data: &str, - charset: Option, + charset: Option, ecc_percent: u32, compact: bool, layers: u32, @@ -721,7 +718,7 @@ fn testWriter( if charset.is_some() { hints.insert( EncodeHintType::CHARACTER_SET, - EncodeHintValue::CharacterSet(charset.unwrap().name().to_owned()), + EncodeHintValue::CharacterSet(charset.unwrap().getCharsetName().to_string()), ); } // if (null != charset) { @@ -737,7 +734,7 @@ fn testWriter( let cset = match charset { Some(cs) => cs, - None => encoding::all::ISO_8859_1, + None => CharacterSetECI::ISO8859_1, }; let aztec = aztec_encoder::encode_with_charset( data, @@ -820,10 +817,8 @@ fn testStuffBits(wordSize: usize, bits: &str, expected: &str) { fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::with_charset( - encoding::all::UTF_8 - .encode(s, encoding::EncoderTrap::Strict) - .expect("should encode to bytes"), - encoding::all::UTF_8, + CharacterSetECI::UTF8.encode(s).expect("should encode to bytes"), + CharacterSetECI::UTF8, ) .encode() .expect("high level ok"); @@ -843,9 +838,7 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { fn testHighLevelEncodeString(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::new( - encoding::all::ISO_8859_1 - .encode(s, encoding::EncoderTrap::Strict) - .expect("should encode to bytes"), + CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), ) .encode() .expect("high level ok"); @@ -864,9 +857,7 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) { fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) { let bits = HighLevelEncoder::new( - encoding::all::ISO_8859_1 - .encode(s, encoding::EncoderTrap::Strict) - .expect("should encode to bytes"), + CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), ) .encode() .expect("high level ok"); diff --git a/src/aztec/aztec_writer.rs b/src/aztec/aztec_writer.rs index 7eb7118..7bfa320 100644 --- a/src/aztec/aztec_writer.rs +++ b/src/aztec/aztec_writer.rs @@ -16,10 +16,8 @@ use std::collections::HashMap; -use encoding::EncodingRef; - use crate::{ - common::{BitMatrix, Result}, + common::{BitMatrix, Result, CharacterSetECI}, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer, }; @@ -58,10 +56,7 @@ impl Writer for AztecWriter { hints.get(&EncodeHintType::CHARACTER_SET) { if cset_name.to_lowercase() != "iso-8859-1" { - charset = Some( - encoding::label::encoding_from_whatwg_label(cset_name) - .ok_or(Exceptions::ILLEGAL_ARGUMENT)?, - ); + charset = CharacterSetECI::getCharacterSetECIByName(cset_name); } } if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) = @@ -91,7 +86,7 @@ fn encode( format: BarcodeFormat, width: u32, height: u32, - charset: Option, + charset: Option, ecc_percent: u32, layers: i32, ) -> Result { diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index a0902db..14863fb 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -113,8 +113,8 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // Intermediary buffer of decoded bytes, which is decoded into a string and flushed // when character encoding changes (ECI) or input ends. let mut decoded_bytes: Vec = Vec::new(); - // let mut encdr: &'static dyn encoding::Encoding = encoding::all::UTF_8; - let mut encdr: encoding::EncodingRef = encoding::all::ISO_8859_1; + + let mut encdr: CharacterSetECI = CharacterSetECI::ISO8859_1; let mut index = 0; @@ -161,8 +161,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // flush bytes, FLG changes state result.push_str( &encdr - .decode(&decoded_bytes, encoding::DecoderTrap::Strict) - .map_err(Exceptions::illegal_state_with)?, + .decode(&decoded_bytes)?, ); decoded_bytes.clear(); @@ -190,7 +189,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { if charset_eci.is_err() { return Err(Exceptions::format_with("Charset must exist")); } - encdr = CharacterSetECI::getCharset(&charset_eci?); + encdr = charset_eci?; } } // Go back to whatever mode we had been in @@ -207,7 +206,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); @@ -220,7 +219,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { } } //try { - if let Ok(str) = encdr.decode(&decoded_bytes, encoding::DecoderTrap::Strict) { + if let Ok(str) = encdr.decode(&decoded_bytes) { result.push_str(&str); } else { return Err(Exceptions::illegal_state_with("bad encoding")); diff --git a/src/aztec/encoder/aztec_encoder.rs b/src/aztec/encoder/aztec_encoder.rs index d5b784c..899d06f 100644 --- a/src/aztec/encoder/aztec_encoder.rs +++ b/src/aztec/encoder/aztec_encoder.rs @@ -14,14 +14,12 @@ * limitations under the License. */ -use encoding::Encoding; - use crate::{ common::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder, }, - BitArray, BitMatrix, Result, + BitArray, BitMatrix, Result, CharacterSetECI, }, exceptions::Exceptions, }; @@ -51,10 +49,9 @@ pub const WORD_SIZE: [u32; 33] = [ * @return Aztec symbol matrix with metadata */ pub fn encode_simple(data: &str) -> Result { - let Ok(bytes) = encoding::all::ISO_8859_1 - .encode(data, encoding::EncoderTrap::Replace) else { - return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1"))); - }; + let Ok(bytes) =CharacterSetECI::ISO8859_1.encode_replace(data) else { + return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1"))); + }; encode_bytes_simple(&bytes) } @@ -68,7 +65,7 @@ pub fn encode_simple(data: &str) -> Result { * @return Aztec symbol matrix with metadata */ pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result { - if let Ok(bytes) = encoding::all::ISO_8859_1.encode(data, encoding::EncoderTrap::Strict) { + if let Ok(bytes) = CharacterSetECI::ISO8859_1.encode(data) { encode_bytes(&bytes, minECCPercent, userSpecifiedLayers) } else { Err(Exceptions::illegal_argument_with(format!( @@ -93,9 +90,9 @@ pub fn encode_with_charset( data: &str, minECCPercent: u32, userSpecifiedLayers: i32, - charset: encoding::EncodingRef, + charset: CharacterSetECI, ) -> Result { - if let Ok(bytes) = charset.encode(data, encoding::EncoderTrap::Strict) { + if let Ok(bytes) = charset.encode(data) { encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset) } else { Err(Exceptions::illegal_argument_with(format!( @@ -132,7 +129,7 @@ pub fn encode_bytes( data, minECCPercent, userSpecifiedLayers, - encoding::all::ISO_8859_1, + CharacterSetECI::ISO8859_1, ) } @@ -151,7 +148,7 @@ pub fn encode_bytes_with_charset( data: &[u8], min_eccpercent: u32, user_specified_layers: i32, - charset: encoding::EncodingRef, + charset: CharacterSetECI, ) -> Result { // High-level encode let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?; diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index 12bc719..3fa495a 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -35,7 +35,7 @@ use super::{State, Token}; */ pub struct HighLevelEncoder { text: Vec, - charset: encoding::EncodingRef, + charset: CharacterSetECI, } impl HighLevelEncoder { @@ -229,11 +229,11 @@ impl HighLevelEncoder { pub fn new(text: Vec) -> Self { Self { text, - charset: encoding::all::ISO_8859_1, + charset: CharacterSetECI::ISO8859_1, } } - pub fn with_charset(text: Vec, charset: encoding::EncodingRef) -> Self { + pub fn with_charset(text: Vec, charset: CharacterSetECI) -> Self { Self { text, charset } } @@ -242,16 +242,16 @@ impl HighLevelEncoder { */ pub fn encode(&self) -> Result { let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0); - if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { - if eci != CharacterSetECI::ISO8859_1 { + //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { + if self.charset != CharacterSetECI::ISO8859_1 { //} && eci != CharacterSetECI::Cp1252 { - initial_state = initial_state.appendFLGn(CharacterSetECI::getValue(&eci))?; + initial_state = initial_state.appendFLGn(self.charset.getValue())?; } - } else { - return Err(Exceptions::illegal_argument_with( - "No ECI code for character set", - )); - } + // } else { + // return Err(Exceptions::illegal_argument_with( + // "No ECI code for character set", + // )); + // } // if self.charset != null { // CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset); // if (null == eci) { diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index e90f3ef..1a20425 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -16,10 +16,8 @@ use std::fmt; -use encoding::Encoding; - use crate::{ - common::{BitArray, Result}, + common::{BitArray, Result, CharacterSetECI}, exceptions::Exceptions, }; @@ -88,8 +86,8 @@ impl State { )); // throw new IllegalArgumentException("ECI code must be between 0 and 999999"); } else { - let Ok(eci_digits) = encoding::all::ISO_8859_1 - .encode(&format!("{eci}"), encoding::EncoderTrap::Strict) + let Ok(eci_digits) = CharacterSetECI::ISO8859_1 + .encode(&format!("{eci}")) else { return Err(Exceptions::ILLEGAL_ARGUMENT) }; diff --git a/src/client/result/VCardResultParser.rs b/src/client/result/VCardResultParser.rs index d21617b..290b95a 100644 --- a/src/client/result/VCardResultParser.rs +++ b/src/client/result/VCardResultParser.rs @@ -20,7 +20,7 @@ use regex::Regex; use once_cell::sync::Lazy; -use crate::RXingResult; +use crate::{RXingResult, common::CharacterSetECI}; use uriparse::URI; @@ -404,9 +404,9 @@ fn maybeAppendFragment(fragmentBuffer: &mut Vec, charset: &str, result: &mut if charset.is_empty() { fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()); // fragment = new String(fragmentBytes, StandardCharsets.UTF_8); - } else if let Some(enc) = encoding::label::encoding_from_whatwg_label(charset) { + } else if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(charset) { fragment = if let Ok(encoded_result) = - enc.decode(&fragmentBytes, encoding::DecoderTrap::Strict) + enc.decode(&fragmentBytes) { encoded_result } else { diff --git a/src/common/StringUtilsTestCase.rs b/src/common/StringUtilsTestCase.rs index 4f3ec67..882ae3c 100644 --- a/src/common/StringUtilsTestCase.rs +++ b/src/common/StringUtilsTestCase.rs @@ -23,12 +23,13 @@ // import java.nio.charset.StandardCharsets; // import java.util.Random; -use encoding::{Encoding, EncodingRef}; use rand::Rng; use std::collections::HashMap; use crate::common::StringUtils; +use super::CharacterSetECI; + #[test] fn test_random() { let mut r = rand::thread_rng(); @@ -38,10 +39,9 @@ fn test_random() { // *byte = r.gen(); // } assert_eq!( - encoding::all::UTF_8.name(), + CharacterSetECI::UTF8, StringUtils::guessCharset(&bytes, &HashMap::new()) .unwrap() - .name() ); } @@ -50,7 +50,7 @@ fn test_short_shift_jis1() { // 金魚 do_test( &[0x8b, 0xe0, 0x8b, 0x9b], - encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), + CharacterSetECI::SJIS, "SJIS", ); } @@ -58,7 +58,7 @@ fn test_short_shift_jis1() { #[test] fn test_short_iso885911() { // båd - do_test(&[0x62, 0xe5, 0x64], encoding::all::ISO_8859_1, "ISO8859_1"); + do_test(&[0x62, 0xe5, 0x64], CharacterSetECI::ISO8859_1, "ISO8859_1"); } #[test] @@ -66,7 +66,7 @@ fn test_short_utf8() { // Español do_test( &[0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c], - encoding::all::UTF_8, + CharacterSetECI::UTF8, "UTF8", ); } @@ -76,7 +76,7 @@ fn test_mixed_shift_jis1() { // Hello 金! do_test( &[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21], - encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), + CharacterSetECI::SJIS, "SJIS", ); } @@ -86,8 +86,8 @@ fn test_utf16_be() { // 调压柜 do_test( &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], - encoding::all::UTF_16BE, - encoding::all::UTF_16BE.name(), + CharacterSetECI::UnicodeBigUnmarked, + &CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), ); } @@ -96,15 +96,15 @@ fn test_utf16_le() { // 调压柜 do_test( &[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67], - encoding::all::UTF_16LE, - encoding::all::UTF_16LE.name(), + CharacterSetECI::UTF16LE, + &CharacterSetECI::UTF16LE.getCharsetName(), ); } -fn do_test(bytes: &[u8], charset: EncodingRef, encoding: &str) { +fn do_test(bytes: &[u8], charset: CharacterSetECI, encoding: &str) { let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap(); let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new()).unwrap(); - assert_eq!(charset.name(), guessedCharset.name()); + assert_eq!(charset, guessedCharset); assert_eq!(encoding, guessedEncoding); } diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 2e97b1f..25af1fe 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -14,15 +14,6 @@ * limitations under the License. */ -// package com.google.zxing.common; - -// import com.google.zxing.FormatException; - -// import java.nio.charset.Charset; - -// import java.util.HashMap; -// import java.util.Map; - use encoding::EncodingRef; use crate::common::Result; @@ -59,6 +50,7 @@ pub enum CharacterSetECI { Cp1252, //(23, "windows-1252"), Cp1256, //(24, "windows-1256"), UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"), + UTF16LE, UTF8, //(26, "UTF-8"), ASCII, //(new int[] {27, 170}, "US-ASCII"), Big5, //(28), @@ -101,8 +93,8 @@ impl CharacterSetECI { Self::getValue(self) } - pub fn getValue(cs_eci: &CharacterSetECI) -> u32 { - match cs_eci { + pub fn getValue(&self) -> u32 { + match self { CharacterSetECI::Cp437 => 0, CharacterSetECI::ISO8859_1 => 1, CharacterSetECI::ISO8859_2 => 4, @@ -125,6 +117,7 @@ impl CharacterSetECI { CharacterSetECI::Cp1252 => 23, CharacterSetECI::Cp1256 => 24, CharacterSetECI::UnicodeBigUnmarked => 25, + CharacterSetECI::UTF16LE => 100025, CharacterSetECI::UTF8 => 26, CharacterSetECI::ASCII => 27, CharacterSetECI::Big5 => 28, @@ -136,8 +129,8 @@ impl CharacterSetECI { pub fn getCharset(&self, ) -> EncodingRef { let name = match self { // CharacterSetECI::Cp437 => "CP437", - CharacterSetECI::Cp437 => "UTF-8", - CharacterSetECI::ISO8859_1 => "ISO-8859-1", + CharacterSetECI::Cp437 => "cp437", + CharacterSetECI::ISO8859_1 => return encoding::all::ISO_8859_1, CharacterSetECI::ISO8859_2 => "ISO-8859-2", CharacterSetECI::ISO8859_3 => "ISO-8859-3", CharacterSetECI::ISO8859_4 => "ISO-8859-4", @@ -152,12 +145,13 @@ impl CharacterSetECI { // CharacterSetECI::ISO8859_14 => "ISO-8859-14", CharacterSetECI::ISO8859_15 => "ISO-8859-15", CharacterSetECI::ISO8859_16 => "ISO-8859-16", - CharacterSetECI::SJIS => "Shift_JIS", + CharacterSetECI::SJIS => "shift_jis", CharacterSetECI::Cp1250 => "windows-1250", CharacterSetECI::Cp1251 => "windows-1251", CharacterSetECI::Cp1252 => "windows-1252", CharacterSetECI::Cp1256 => "windows-1256", CharacterSetECI::UnicodeBigUnmarked => "UTF-16BE", + CharacterSetECI::UTF16LE => "UTF-16LE", CharacterSetECI::UTF8 => "UTF-8", CharacterSetECI::ASCII => "US-ASCII", CharacterSetECI::Big5 => "Big5", @@ -167,6 +161,41 @@ impl CharacterSetECI { encoding::label::encoding_from_whatwg_label(name).unwrap() } + pub fn getCharsetName(&self, ) -> &'static str { + match self { + // CharacterSetECI::Cp437 => "CP437", + CharacterSetECI::Cp437 => "cp437", + CharacterSetECI::ISO8859_1 => "iso-8859-1", + CharacterSetECI::ISO8859_2 => "iso-8859-2", + CharacterSetECI::ISO8859_3 => "iso-8859-3", + CharacterSetECI::ISO8859_4 => "iso-8859-4", + CharacterSetECI::ISO8859_5 => "iso-8859-5", + // CharacterSetECI::ISO8859_6 => "ISO-8859-6", + CharacterSetECI::ISO8859_7 => "iso-8859-7", + // CharacterSetECI::ISO8859_8 => "ISO-8859-8", + CharacterSetECI::ISO8859_9 => "iso-8859-9", + // CharacterSetECI::ISO8859_10 => "ISO-8859-10", + // CharacterSetECI::ISO8859_11 => "ISO-8859-11", + CharacterSetECI::ISO8859_13 => "iso-8859-13", + // CharacterSetECI::ISO8859_14 => "ISO-8859-14", + CharacterSetECI::ISO8859_15 => "iso-8859-15", + CharacterSetECI::ISO8859_16 => "iso-8859-16", + CharacterSetECI::SJIS => "shift_jis", + CharacterSetECI::Cp1250 => "windows-1250", + CharacterSetECI::Cp1251 => "windows-1251", + CharacterSetECI::Cp1252 => "windows-1252", + CharacterSetECI::Cp1256 => "windows-1256", + CharacterSetECI::UnicodeBigUnmarked => "utf-16be", + CharacterSetECI::UTF16LE => "utf-16le", + CharacterSetECI::UTF8 => "utf-8", + CharacterSetECI::ASCII => "us-ascii", + CharacterSetECI::Big5 => "big5", + CharacterSetECI::GB18030 => "gb2312", + CharacterSetECI::EUC_KR => "euc-kr", + } + + } + /** * @param charset Java character set object * @return CharacterSetECI representing ECI for character encoding, or null if it is legal @@ -179,7 +208,7 @@ impl CharacterSetECI { charset.name() }; match name { - "CP437" => Some(CharacterSetECI::Cp437), + "cp437" => Some(CharacterSetECI::Cp437), "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), @@ -201,7 +230,7 @@ impl CharacterSetECI { "windows-1252" => Some(CharacterSetECI::Cp1252), "windows-1256" => Some(CharacterSetECI::Cp1256), "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), - "utf-8" => Some(CharacterSetECI::UTF8), + "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), "us-ascii" => Some(CharacterSetECI::ASCII), "big5" => Some(CharacterSetECI::Big5), "gb2312" => Some(CharacterSetECI::GB18030), @@ -255,34 +284,34 @@ impl CharacterSetECI { * but unsupported */ pub fn getCharacterSetECIByName(name: &str) -> Option { - match name { - "CP437" => Some(CharacterSetECI::Cp437), - "ISO-8859-1" => Some(CharacterSetECI::ISO8859_1), - "ISO-8859-2" => Some(CharacterSetECI::ISO8859_2), - "ISO-8859-3" => Some(CharacterSetECI::ISO8859_3), - "ISO-8859-4" => Some(CharacterSetECI::ISO8859_4), - "ISO-8859-5" => Some(CharacterSetECI::ISO8859_5), + match name.to_lowercase().as_str() { + "cp437" => Some(CharacterSetECI::Cp437), + "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), + "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), + "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), + "iso-8859-4" => Some(CharacterSetECI::ISO8859_4), + "iso-8859-5" => Some(CharacterSetECI::ISO8859_5), // "ISO-8859-6" => Some(CharacterSetECI::ISO8859_6), - "ISO-8859-7" => Some(CharacterSetECI::ISO8859_7), + "iso-8859-7" => Some(CharacterSetECI::ISO8859_7), // "ISO-8859-8" => Some(CharacterSetECI::ISO8859_8), - "ISO-8859-9" => Some(CharacterSetECI::ISO8859_9), + "iso-8859-9" => Some(CharacterSetECI::ISO8859_9), // "ISO-8859-10" => Some(CharacterSetECI::ISO8859_10), // "ISO-8859-11" => Some(CharacterSetECI::ISO8859_11), - "ISO-8859-13" => Some(CharacterSetECI::ISO8859_13), + "iso-8859-13" => Some(CharacterSetECI::ISO8859_13), // "ISO-8859-14" => Some(CharacterSetECI::ISO8859_14), - "ISO-8859-15" => Some(CharacterSetECI::ISO8859_15), - "ISO-8859-16" => Some(CharacterSetECI::ISO8859_16), - "Shift_JIS" => Some(CharacterSetECI::SJIS), + "iso-8859-15" => Some(CharacterSetECI::ISO8859_15), + "iso-8859-16" => Some(CharacterSetECI::ISO8859_16), + "shift_jis" => Some(CharacterSetECI::SJIS), "windows-1250" => Some(CharacterSetECI::Cp1250), "windows-1251" => Some(CharacterSetECI::Cp1251), "windows-1252" => Some(CharacterSetECI::Cp1252), "windows-1256" => Some(CharacterSetECI::Cp1256), - "UTF-16BE" => Some(CharacterSetECI::UnicodeBigUnmarked), - "UTF-8" => Some(CharacterSetECI::UTF8), - "US-ASCII" => Some(CharacterSetECI::ASCII), - "Big5" => Some(CharacterSetECI::Big5), - "GB2312" => Some(CharacterSetECI::GB18030), - "EUC-KR" => Some(CharacterSetECI::EUC_KR), + "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), + "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), + "us-ascii" => Some(CharacterSetECI::ASCII), + "big5" => Some(CharacterSetECI::Big5), + "gb2312" => Some(CharacterSetECI::GB18030), + "euc-kr" => Some(CharacterSetECI::EUC_KR), _ => None, } } @@ -296,7 +325,14 @@ impl CharacterSetECI { } pub fn decode(&self, input:&[u8]) -> Result { + if self == &CharacterSetECI::Cp437 { + use codepage_437::BorrowFromCp437; + use codepage_437::CP437_CONTROL; + + Ok(String::borrow_from_cp437(&input, &CP437_CONTROL)) + }else { self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } } pub fn decode_replace(&self, input:&[u8]) -> Result { diff --git a/src/common/eci_encoder_set.rs b/src/common/eci_encoder_set.rs index fb02f3c..0b4d9a9 100644 --- a/src/common/eci_encoder_set.rs +++ b/src/common/eci_encoder_set.rs @@ -14,31 +14,17 @@ * limitations under the License. */ -// package com.google.zxing.common; - -// import java.nio.charset.Charset; -// import java.nio.charset.CharsetEncoder; -// import java.nio.charset.StandardCharsets; -// import java.nio.charset.UnsupportedCharsetException; -// import java.util.ArrayList; -// import java.util.List; - -use encoding::{Encoding, EncodingRef}; use unicode_segmentation::UnicodeSegmentation; use super::CharacterSetECI; use once_cell::sync::Lazy; -static ENCODERS: Lazy> = Lazy::new(|| { +static ENCODERS: Lazy> = Lazy::new(|| { let mut enc_vec = Vec::new(); for name in NAMES { if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(name) { - // try { - enc_vec.push(CharacterSetECI::getCharset(&enc)); - // } catch (UnsupportedCharsetException e) { - // continue - // } + enc_vec.push(enc); } } enc_vec @@ -83,7 +69,7 @@ const NAMES: [&str; 20] = [ */ #[derive(Clone)] pub struct ECIEncoderSet { - encoders: Vec, + encoders: Vec, priorityEncoderIndex: Option, } @@ -98,22 +84,23 @@ impl ECIEncoderSet { */ pub fn new( stringToEncodeMain: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option<&str>, ) -> Self { // List of encoders that potentially encode characters not in ISO-8859-1 in one byte. - let mut encoders: Vec; + let mut encoders: Vec; let mut priorityEncoderIndexValue = None; - let mut neededEncoders: Vec = Vec::new(); + let mut neededEncoders: Vec = Vec::new(); let stringToEncode = stringToEncodeMain.graphemes(true).collect::>(); //we always need the ISO-8859-1 encoder. It is the default encoding - neededEncoders.push(encoding::all::ISO_8859_1); + neededEncoders.push(CharacterSetECI::ISO8859_1); let mut needUnicodeEncoder = if let Some(pc) = priorityCharset { - pc.name().starts_with("UTF") || pc.name().starts_with("utf") + //pc.name().starts_with("UTF") || pc.name().starts_with("utf") + pc == CharacterSetECI::UTF8 || pc == CharacterSetECI::UnicodeBigUnmarked } else { false }; @@ -126,7 +113,7 @@ impl ECIEncoderSet { // for (CharsetEncoder encoder : neededEncoders) { let c = stringToEncode.get(i).unwrap(); if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) - || encoder.encode(c, encoding::EncoderTrap::Strict).is_ok() + || encoder.encode(c).is_ok() { canEncode = true; break; @@ -140,8 +127,7 @@ impl ECIEncoderSet { // for (CharsetEncoder encoder : ENCODERS) { if encoder .encode( - stringToEncode.get(i).unwrap(), - encoding::EncoderTrap::Strict, + stringToEncode.get(i).unwrap() ) .is_ok() { @@ -163,7 +149,7 @@ impl ECIEncoderSet { if neededEncoders.len() == 1 && !needUnicodeEncoder { //the entire input can be encoded by the ISO-8859-1 encoder - encoders = vec![encoding::all::ISO_8859_1]; + encoders = vec![CharacterSetECI::ISO8859_1]; } else { // we need more than one single byte encoder or we need a Unicode encoder. // In this case we append a UTF-8 and UTF-16 encoder to the list @@ -177,8 +163,8 @@ impl ECIEncoderSet { encoders.push(encoder); } - encoders.push(encoding::all::UTF_8); - encoders.push(encoding::all::UTF_16BE); + encoders.push(CharacterSetECI::UTF8); + encoders.push(CharacterSetECI::UnicodeBigUnmarked); } //Compute priorityEncoderIndex by looking up priorityCharset in encoders @@ -187,7 +173,8 @@ impl ECIEncoderSet { // for i in 0..encoders.len() { for (i, encoder) in encoders.iter().enumerate() { // for (int i = 0; i < encoders.length; i++) { - if priorityCharset.as_ref().unwrap().name() == encoder.name() { + // if priorityCharset.as_ref().unwrap().name() == encoder.name() { + if priorityCharset.as_ref().unwrap() == encoder { priorityEncoderIndexValue = Some(i); break; } @@ -195,7 +182,7 @@ impl ECIEncoderSet { } // } //invariants - assert_eq!(encoders[0].name(), encoding::all::ISO_8859_1.name()); + assert_eq!(encoders[0], CharacterSetECI::ISO8859_1); Self { encoders, priorityEncoderIndex: priorityEncoderIndexValue, @@ -212,13 +199,13 @@ impl ECIEncoderSet { pub fn getCharsetName(&self, index: usize) -> Option<&'static str> { if index < self.len() { - Some(self.encoders[index].name()) + Some(self.encoders[index].getCharsetName()) } else { None } } - pub fn getCharset(&self, index: usize) -> Option { + pub fn getCharset(&self, index: usize) -> Option { if index < self.len() { Some(self.encoders[index]) } else { @@ -227,9 +214,10 @@ impl ECIEncoderSet { } pub fn getECIValue(&self, encoderIndex: usize) -> u32 { - CharacterSetECI::getValue( - &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), - ) + self.encoders[encoderIndex].getValue() + // CharacterSetECI::getValue( + // &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), + // ) } /* @@ -242,7 +230,7 @@ impl ECIEncoderSet { pub fn canEncode(&self, c: &str, encoderIndex: usize) -> Option { if encoderIndex < self.len() { let encoder = self.encoders[encoderIndex]; - let enc_data = encoder.encode(c, encoding::EncoderTrap::Strict); + let enc_data = encoder.encode(c); Some(enc_data.is_ok()) } else { @@ -253,7 +241,7 @@ impl ECIEncoderSet { pub fn encode_char(&self, c: &str, encoderIndex: usize) -> Option> { if encoderIndex < self.len() { let encoder = self.encoders[encoderIndex]; - let enc_data = encoder.encode(c, encoding::EncoderTrap::Strict); + let enc_data = encoder.encode(c); enc_data.ok() // assert!(enc_data.is_ok()); // enc_data.unwrap() @@ -265,7 +253,7 @@ impl ECIEncoderSet { pub fn encode_string(&self, s: &str, encoderIndex: usize) -> Option> { if encoderIndex < self.len() { let encoder = self.encoders[encoderIndex]; - encoder.encode(s, encoding::EncoderTrap::Strict).ok() + encoder.encode(s).ok() } else { None } diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index 7f338a8..7491b96 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -23,8 +23,6 @@ use std::fmt; -use encoding::{Encoding, EncodingRef}; - use crate::common::Result; use super::CharacterSetECI; @@ -37,7 +35,7 @@ use super::CharacterSetECI; pub struct ECIStringBuilder { current_bytes: Vec, result: String, - current_charset: Option, //= StandardCharsets.ISO_8859_1; + current_charset: Option, //= StandardCharsets.ISO_8859_1; } impl ECIStringBuilder { @@ -45,14 +43,14 @@ impl ECIStringBuilder { Self { current_bytes: Vec::new(), result: String::new(), - current_charset: Some(encoding::all::ISO_8859_1), + current_charset: Some(CharacterSetECI::ISO8859_1), } } pub fn with_capacity(initial_capacity: usize) -> Self { Self { current_bytes: Vec::with_capacity(initial_capacity), result: String::with_capacity(initial_capacity), - current_charset: Some(encoding::all::ISO_8859_1), + current_charset: Some(CharacterSetECI::ISO8859_1), } } @@ -106,16 +104,18 @@ impl ECIStringBuilder { pub fn appendECI(&mut self, value: u32) -> Result<()> { self.encodeCurrentBytesIfAny(); - if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { - // dbg!( - // character_set_eci, - // CharacterSetECI::getCharset(&character_set_eci).name(), - // CharacterSetECI::getCharset(&character_set_eci).whatwg_name() - // ); - self.current_charset = Some(CharacterSetECI::getCharset(&character_set_eci)); - } else { - self.current_charset = None - } + self.current_charset = CharacterSetECI::getCharacterSetECIByValue(value).ok(); + + // if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { + // // dbg!( + // // character_set_eci, + // // CharacterSetECI::getCharset(&character_set_eci).name(), + // // CharacterSetECI::getCharset(&character_set_eci).whatwg_name() + // // ); + // self.current_charset = Some(character_set_eci); + // } else { + // self.current_charset = None + // } // self.current_charset = CharacterSetECI::getCharset(&character_set_eci); Ok(()) @@ -126,7 +126,7 @@ impl ECIStringBuilder { /// This function can panic pub fn encodeCurrentBytesIfAny(&mut self) { if let Some(encoder) = self.current_charset { - if encoder.name() == encoding::all::UTF_8.name() { + if encoder == CharacterSetECI::UTF8 { if !self.current_bytes.is_empty() { self.result.push_str( &String::from_utf8(std::mem::take(&mut self.current_bytes)).unwrap(), @@ -137,7 +137,7 @@ impl ECIStringBuilder { let bytes = std::mem::take(&mut self.current_bytes); self.current_bytes.clear(); let encoded_value = encoder - .decode(&bytes, encoding::DecoderTrap::Strict) + .decode(&bytes) .unwrap(); self.result.push_str(&encoded_value); } diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index b6b9cc1..51e586d 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -16,13 +16,12 @@ use std::{fmt, rc::Rc}; -use encoding::EncodingRef; use unicode_segmentation::UnicodeSegmentation; use crate::common::Result; use crate::Exceptions; -use super::{ECIEncoderSet, ECIInput}; +use super::{ECIEncoderSet, ECIInput, CharacterSetECI}; //* approximated (latch + 2 codewords) pub const COST_PER_ECI: usize = 3; @@ -194,7 +193,7 @@ impl MinimalECIInput { */ pub fn new( stringToEncodeInput: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option<&str>, ) -> Self { let stringToEncode = stringToEncodeInput.graphemes(true).collect::>(); diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index b472916..f601c50 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -14,12 +14,12 @@ * limitations under the License. */ -use encoding::{Encoding, EncodingRef}; - use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary}; use once_cell::sync::Lazy; +use super::CharacterSetECI; + /** * Common string-related functions. * @@ -50,8 +50,8 @@ const ASSUME_SHIFT_JIS: bool = false; // static SHIFT_JIS: &'static str = "SJIS"; // static GB2312: &'static str = "GB2312"; -pub static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); +pub static SHIFT_JIS_CHARSET: CharacterSetECI = + CharacterSetECI::SJIS; // private static final boolean ASSUME_SHIFT_JIS = // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || @@ -67,14 +67,14 @@ impl StringUtils { */ pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> { let c = StringUtils::guessCharset(bytes, hints)?; - if c.name() == encoding::label::encoding_from_whatwg_label("SJIS")?.name() { + if c == CharacterSetECI::SJIS { Some("SJIS") - } else if c.name() == encoding::all::UTF_8.name() { + } else if c == CharacterSetECI::UTF8 { Some("UTF8") - } else if c.name() == encoding::all::ISO_8859_1.name() { + } else if c == CharacterSetECI::ISO8859_1 { Some("ISO8859_1") } else { - Some(c.name()) + Some(&c.getCharsetName()) } } @@ -87,12 +87,12 @@ impl StringUtils { * or the platform default encoding if * none of these can possibly be correct */ - pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option { + pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option { if let Some(DecodeHintValue::CharacterSet(cs_name)) = hints.get(&DecodeHintType::CHARACTER_SET) { // if let DecodeHintValue::CharacterSet(cs_name) = hint { - return encoding::label::encoding_from_whatwg_label(cs_name); + return CharacterSetECI::getCharacterSetECIByName(cs_name) // } } // if hints.contains_key(&DecodeHintType::CHARACTER_SET) { @@ -104,9 +104,9 @@ impl StringUtils { && ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE)) { if bytes[0] == 0xFE && bytes[1] == 0xFF { - return Some(encoding::all::UTF_16BE); + return Some(CharacterSetECI::UnicodeBigUnmarked); } else { - return Some(encoding::all::UTF_16LE); + return Some(CharacterSetECI::UTF16LE); } } @@ -224,7 +224,7 @@ impl StringUtils { // Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) { - return Some(encoding::all::UTF_8); + return Some(CharacterSetECI::UTF8); } // Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done if can_be_shift_jis @@ -232,7 +232,7 @@ impl StringUtils { || sjis_max_katakana_word_length >= 3 || sjis_max_double_bytes_word_length >= 3) { - return encoding::label::encoding_from_whatwg_label("SJIS"); + return Some(CharacterSetECI::SJIS); //encoding::label::encoding_from_whatwg_label("SJIS"); } // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is: // - If we saw @@ -243,23 +243,23 @@ impl StringUtils { return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2) || iso_high_other * 10 >= length { - encoding::label::encoding_from_whatwg_label("SJIS") + Some(CharacterSetECI::SJIS) } else { - Some(encoding::all::ISO_8859_1) + Some(CharacterSetECI::ISO8859_1) }; } // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding if can_be_iso88591 { - return Some(encoding::all::ISO_8859_1); + return Some(CharacterSetECI::ISO8859_1); } if can_be_shift_jis { - return Some(encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); + return Some(CharacterSetECI::SJIS); } if can_be_utf8 { - return Some(encoding::all::UTF_8); + return Some(CharacterSetECI::UTF8); } // Otherwise, we take a wild guess with platform encoding - Some(encoding::all::UTF_8) + Some(CharacterSetECI::UTF8) } } diff --git a/src/datamatrix/data_matrix_writer.rs b/src/datamatrix/data_matrix_writer.rs index cafffb9..4409fb4 100644 --- a/src/datamatrix/data_matrix_writer.rs +++ b/src/datamatrix/data_matrix_writer.rs @@ -17,10 +17,8 @@ use std::collections::HashMap; -use encoding::EncodingRef; - use crate::{ - common::{BitMatrix, Result}, + common::{BitMatrix, Result, CharacterSetECI}, qrcode::encoder::ByteMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -117,14 +115,14 @@ impl Writer for DataMatrixWriter { false }; - let mut charset: Option = None; + let mut charset: Option = None; let hasEncodingHint = hints.contains_key(&EncodeHintType::CHARACTER_SET); if hasEncodingHint { let Some(EncodeHintValue::CharacterSet(char_set_name)) = hints.get(&EncodeHintType::CHARACTER_SET) else { return Err(Exceptions::illegal_argument_with("charset does not exist")) }; - charset = encoding::label::encoding_from_whatwg_label(char_set_name); + charset = CharacterSetECI::getCharacterSetECIByName(char_set_name);//encoding::label::encoding_from_whatwg_label(char_set_name); // charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); } encoded = minimal_encoder::encodeHighLevelWithDetails( diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index fbde09d..9048601 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -14,10 +14,8 @@ * limitations under the License. */ -use encoding::Encoding; - use crate::{ - common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result}, + common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result, CharacterSetECI}, Exceptions, }; @@ -730,9 +728,10 @@ fn decodeBase256Segment( codewordPosition += 1; } result.append_string( - &encoding::all::ISO_8859_1 - .decode(&bytes, encoding::DecoderTrap::Strict) - .map_err(Exceptions::parse_with)?, + + &CharacterSetECI::ISO8859_1 + .decode(&bytes) + ?, ); byteSegments.push(bytes); diff --git a/src/datamatrix/encoder/encoder_context.rs b/src/datamatrix/encoder/encoder_context.rs index 51fd9a3..121ec92 100644 --- a/src/datamatrix/encoder/encoder_context.rs +++ b/src/datamatrix/encoder/encoder_context.rs @@ -16,13 +16,12 @@ use std::rc::Rc; -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use crate::{Dimension, Exceptions}; use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint}; -use encoding::{self, EncodingRef}; -const ISO_8859_1_ENCODER: EncodingRef = encoding::all::ISO_8859_1; +const ISO_8859_1_ENCODER: CharacterSetECI = CharacterSetECI::ISO8859_1; pub struct EncoderContext<'a> { symbol_lookup: Rc>, @@ -59,10 +58,10 @@ impl<'a> EncoderContext<'_> { // sb.append(ch); // } let sb = if let Ok(encoded_bytes) = - ISO_8859_1_ENCODER.encode(msg, encoding::EncoderTrap::Strict) + ISO_8859_1_ENCODER.encode(msg) { ISO_8859_1_ENCODER - .decode(&encoded_bytes, encoding::DecoderTrap::Strict) + .decode(&encoded_bytes) .map_err(|e| { Exceptions::parse_with(format!("round trip decode should always work: {e}")) })? diff --git a/src/datamatrix/encoder/high_level_encode_test_case.rs b/src/datamatrix/encoder/high_level_encode_test_case.rs index f875b7c..f8c1244 100644 --- a/src/datamatrix/encoder/high_level_encode_test_case.rs +++ b/src/datamatrix/encoder/high_level_encode_test_case.rs @@ -18,7 +18,7 @@ use std::rc::Rc; use once_cell::sync::Lazy; -use crate::datamatrix::encoder::{SymbolInfo, SymbolShapeHint}; +use crate::{datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, common::CharacterSetECI}; use super::{high_level_encoder, minimal_encoder, symbol_info, SymbolInfoLookup}; @@ -534,7 +534,7 @@ fn testECIs() { assert_eq!("239 209 151 206 214 92 122 140 35 158 144 162 52 205 55 171 137 23 67 206 218 175 147 113 15 254 116 33 241 25 231 186 14 212 64 253 151 252 159 33 41 241 27 231 83 171 53 209 35 25 134 6 42 33 35 239 184 31 193 234 7 252 205 101 127 241 209 34 24 5 22 23 221 148 179 239 128 140 92 187 106 204 198 59 19 25 114 248 118 36 254 231 106 196 19 239 101 27 107 69 189 112 236 156 252 16 174 125 24 10 125 116 42 129", visualized); - let visualized = visualize(&minimal_encoder::encodeHighLevelWithDetails("that particularly stands out to me is \u{0625}\u{0650}\u{062C}\u{064E}\u{0651}\u{0627}\u{0635} (\u{02BE}\u{0101}\u{1E63}) \"pear\", suggested to have originated from Hebrew \u{05D0}\u{05B7}\u{05D2}\u{05B8}\u{05BC}\u{05E1} (ag\u{00E1}s)", Some(encoding::all::UTF_8), None , SymbolShapeHint::FORCE_NONE).expect("encode")); + let visualized = visualize(&minimal_encoder::encodeHighLevelWithDetails("that particularly stands out to me is \u{0625}\u{0650}\u{062C}\u{064E}\u{0651}\u{0627}\u{0635} (\u{02BE}\u{0101}\u{1E63}) \"pear\", suggested to have originated from Hebrew \u{05D0}\u{05B7}\u{05D2}\u{05B8}\u{05BC}\u{05E1} (ag\u{00E1}s)", Some(CharacterSetECI::UTF8), None , SymbolShapeHint::FORCE_NONE).expect("encode")); assert_eq!("241 27 239 209 151 206 214 92 122 140 35 158 144 162 52 205 55 171 137 23 67 206 218 175 147 113 15 254 116 33 231 202 33 131 77 154 119 225 163 238 206 28 249 93 36 150 151 53 108 246 145 228 217 71 199 42 33 35 239 184 31 193 234 7 252 205 101 127 241 209 34 24 5 22 23 221 148 179 239 128 140 92 187 106 204 198 59 19 25 114 248 118 36 254 231 43 133 212 175 38 220 44 6 125 49 172 93 189 209 111 61 217 203 62 116 42 129 1 151 46 196 91 241 137 32 182 77 227 122 18 168 63 213 108 4 154 49 199 94 244 140 35 185 80", visualized); } diff --git a/src/datamatrix/encoder/high_level_encoder.rs b/src/datamatrix/encoder/high_level_encoder.rs index ace5b68..5606d7e 100644 --- a/src/datamatrix/encoder/high_level_encoder.rs +++ b/src/datamatrix/encoder/high_level_encoder.rs @@ -16,9 +16,7 @@ use std::rc::Rc; -use encoding::{self, EncodingRef}; - -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use crate::{Dimension, Exceptions}; use super::{ @@ -26,7 +24,7 @@ use super::{ SymbolInfoLookup, SymbolShapeHint, TextEncoder, X12Encoder, }; #[allow(dead_code)] -const DEFAULT_ENCODING: EncodingRef = encoding::all::ISO_8859_1; +const DEFAULT_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; /** * DataMatrix ECC 200 data encoder following the algorithm described in ISO/IEC 16022:200(E) in diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index dc85258..1d5dd06 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -16,16 +16,14 @@ use std::{fmt, rc::Rc}; -use encoding::{self, EncodingRef}; - use crate::{ - common::{ECIInput, MinimalECIInput, Result}, + common::{ECIInput, MinimalECIInput, Result, CharacterSetECI}, Exceptions, }; use super::{high_level_encoder, SymbolShapeHint}; -const ISO_8859_1_ENCODER: EncodingRef = encoding::all::ISO_8859_1; +const ISO_8859_1_ENCODER: CharacterSetECI = CharacterSetECI::ISO8859_1; /** * Encoder that encodes minimally @@ -153,7 +151,7 @@ pub fn encodeHighLevel(msg: &str) -> Result { */ pub fn encodeHighLevelWithDetails( msg: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, ) -> Result { @@ -174,8 +172,8 @@ pub fn encodeHighLevelWithDetails( } Ok(ISO_8859_1_ENCODER .decode( - &encode(msg, priorityCharset, fnc1, shape, macroId)?, - encoding::DecoderTrap::Strict, + &encode(msg, priorityCharset, fnc1, shape, macroId)? + ) .expect("should decode")) // return new String(encode(msg, priorityCharset, fnc1, shape, macroId), StandardCharsets.ISO_8859_1); @@ -197,7 +195,7 @@ pub fn encodeHighLevelWithDetails( */ fn encode( input: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, macroId: i32, @@ -1398,7 +1396,7 @@ struct Input { impl Input { pub fn new( stringToEncode: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, macroId: i32, diff --git a/src/pdf417/decoder/pdf_417_decoder_test_case.rs b/src/pdf417/decoder/pdf_417_decoder_test_case.rs index add6d90..58ab3c9 100644 --- a/src/pdf417/decoder/pdf_417_decoder_test_case.rs +++ b/src/pdf417/decoder/pdf_417_decoder_test_case.rs @@ -15,9 +15,9 @@ * limitations under the License. */ -use encoding::{Encoding, EncodingRef}; use java_rand; +use crate::common::CharacterSetECI; use crate::pdf417::decoder::decoded_bit_stream_parser; use crate::pdf417::encoder::{pdf_417_high_level_encoder_test_adapter, Compaction}; use crate::pdf417::PDF417RXingResultMetadata; @@ -307,8 +307,8 @@ fn testBinaryData() { random.next_bytes(&mut bytes); total += encodeDecode( - &encoding::all::ISO_8859_1 - .decode(&bytes, encoding::DecoderTrap::Strict) + &CharacterSetECI::ISO8859_1 + .decode(&bytes) .expect("decode bytes"), ); } @@ -437,7 +437,7 @@ fn encodeDecode(input: &str) -> u32 { fn encodeDecodeWithAll( input: &str, - charset: Option, + charset: Option, autoECI: bool, decode: bool, ) -> u32 { @@ -523,7 +523,7 @@ fn performECITest( // for (int i = 0; i < 1000; i++) { let s = generateText(&mut random, 100, chars, weights); minLength += encodeDecodeWithAll(&s, None, true, true); - utfLength += encodeDecodeWithAll(&s, Some(encoding::all::UTF_8), false, true); + utfLength += encodeDecodeWithAll(&s, Some(CharacterSetECI::UTF8), false, true); } assert_eq!(expectedMinLength, minLength); assert_eq!(expectedUTFLength, utfLength); diff --git a/src/pdf417/encoder/pdf_417.rs b/src/pdf417/encoder/pdf_417.rs index 46b7a90..3ee462b 100644 --- a/src/pdf417/encoder/pdf_417.rs +++ b/src/pdf417/encoder/pdf_417.rs @@ -18,9 +18,7 @@ * This file has been modified from its original form in Barcode4J. */ -use encoding::EncodingRef; - -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use crate::Exceptions; use super::{ @@ -34,7 +32,7 @@ pub struct PDF417 { barcodeMatrix: Option, compact: bool, compaction: Compaction, - encoding: Option, + encoding: Option, minCols: u32, maxCols: u32, maxRows: u32, @@ -347,7 +345,7 @@ impl PDF417 { /** * @param encoding sets character encoding to use */ - pub fn setEncoding(&mut self, encoding: Option) { + pub fn setEncoding(&mut self, encoding: Option) { self.encoding = encoding; } } diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index b35144f..cc7285e 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -20,8 +20,6 @@ use std::{any::TypeId, fmt::Display, str::FromStr}; -use encoding::EncodingRef; - use crate::{ common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, Exceptions, @@ -125,7 +123,7 @@ const TEXT_PUNCTUATION_RAW: [u8; 30] = [ 40, 41, 63, 123, 125, 39, 0, ]; -const DEFAULT_ENCODING: EncodingRef = encoding::all::ISO_8859_1; //StandardCharsets.ISO_8859_1; +const DEFAULT_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1; const MIXED: [i8; 128] = { let mut mixed = [-1_i8; 128]; @@ -174,7 +172,7 @@ const PUNCTUATION: [i8; 128] = { pub fn encodeHighLevel( msg: &str, compaction: Compaction, - encoding: Option, + encoding: Option, autoECI: bool, ) -> Result { let mut encoding = encoding; @@ -199,14 +197,16 @@ pub fn encodeHighLevel( input = Box::new(NoECIInput::new(msg.to_owned())); if encoding.is_none() { encoding = Some(DEFAULT_ENCODING); - } else if DEFAULT_ENCODING.name() - != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)?.name() + } else if &DEFAULT_ENCODING + != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)? { - if let Some(eci) = - CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) - { - encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; - } + // if let Some(eci) = + // CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) + // { + // encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; + // } + + encodingECI(CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, &mut sb)?; } } @@ -226,7 +226,7 @@ pub fn encodeHighLevel( let msgBytes = encoding .as_ref() .ok_or(Exceptions::ILLEGAL_STATE)? - .encode(&input.to_string(), encoding::EncoderTrap::Strict) + .encode(&input.to_string()) .unwrap_or_default(); //input.to_string().getBytes(encoding); encodeBinary( &msgBytes, @@ -286,7 +286,7 @@ pub fn encodeHighLevel( if let Ok(enc_str) = encoding .as_ref() .ok_or(Exceptions::ILLEGAL_STATE)? - .encode(&str, encoding::EncoderTrap::Strict) + .encode(&str) { Some(enc_str) } else { @@ -747,7 +747,7 @@ fn determineConsecutiveTextCount( fn determineConsecutiveBinaryCount( input: &Box, startpos: u32, - encoding: Option, + encoding: Option, ) -> Result { let len = input.length(); let mut idx = startpos as usize; @@ -770,8 +770,8 @@ fn determineConsecutiveBinaryCount( if let Some(encoder) = encoding { let can_encode = encoder .encode( - &input.charAt(idx)?.to_string(), - encoding::EncoderTrap::Strict, + &input.charAt(idx)?.to_string() + ) .is_ok(); @@ -856,11 +856,11 @@ impl Display for NoECIInput { */ #[cfg(test)] mod PDF417EncoderTestCase { - use crate::pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}; + use crate::{pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, common::CharacterSetECI}; #[test] fn testEncodeAuto() { - let encoded = encodeHighLevel("ABCD", Compaction::AUTO, Some(encoding::all::UTF_8), false) + let encoded = encodeHighLevel("ABCD", Compaction::AUTO, Some(CharacterSetECI::UTF8), false) .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0385}ABCD", encoded); } @@ -871,7 +871,7 @@ mod PDF417EncoderTestCase { encodeHighLevel( "1%§s ?aG$", Compaction::AUTO, - Some(encoding::all::UTF_8), + Some(CharacterSetECI::UTF8), false, ) .expect("encode"); @@ -883,7 +883,7 @@ mod PDF417EncoderTestCase { encodeHighLevel( "asdfg§asd", Compaction::AUTO, - Some(encoding::all::ISO_8859_1), + Some(CharacterSetECI::ISO8859_1), false, ) .expect("encode"); @@ -891,7 +891,7 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeText() { - let encoded = encodeHighLevel("ABCD", Compaction::TEXT, Some(encoding::all::UTF_8), false) + let encoded = encodeHighLevel("ABCD", Compaction::TEXT, Some(CharacterSetECI::UTF8), false) .expect("encode"); assert_eq!("Ο\u{001A}\u{0001}?", encoded); } @@ -901,7 +901,7 @@ mod PDF417EncoderTestCase { let encoded = encodeHighLevel( "1234", Compaction::NUMERIC, - Some(encoding::all::UTF_8), + Some(CharacterSetECI::UTF8), false, ) .expect("encode"); @@ -911,7 +911,7 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeByte() { - let encoded = encodeHighLevel("abcd", Compaction::BYTE, Some(encoding::all::UTF_8), false) + let encoded = encodeHighLevel("abcd", Compaction::BYTE, Some(CharacterSetECI::UTF8), false) .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0385}abcd", encoded); } diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs index 3382821..ee37383 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs @@ -14,9 +14,7 @@ * limitations under the License. */ -use encoding::EncodingRef; - -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use super::{pdf_417_high_level_encoder, Compaction}; @@ -27,7 +25,7 @@ use super::{pdf_417_high_level_encoder, Compaction}; pub fn encodeHighLevel( msg: &str, compaction: Compaction, - encoding: Option, + encoding: Option, autoECI: bool, ) -> Result { pdf_417_high_level_encoder::encodeHighLevel(msg, compaction, encoding, autoECI) diff --git a/src/pdf417/pdf_417_writer.rs b/src/pdf417/pdf_417_writer.rs index 10c46db..9f81929 100644 --- a/src/pdf417/pdf_417_writer.rs +++ b/src/pdf417/pdf_417_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result}, + common::{BitMatrix, Result, CharacterSetECI}, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -108,7 +108,7 @@ impl Writer for PDF417Writer { if let Some(EncodeHintValue::CharacterSet(cs)) = hints.get(&EncodeHintType::CHARACTER_SET) { - encoder.setEncoding(encoding::label::encoding_from_whatwg_label(cs)); + encoder.setEncoding(CharacterSetECI::getCharacterSetECIByName(cs)); } if let Some(EncodeHintValue::Pdf417AutoEci(auto_eci_str)) = hints.get(&EncodeHintType::PDF417_AUTO_ECI) diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index 4d6451e..d24b3da 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -204,9 +204,9 @@ fn decodeHanziSegment(bits: &mut BitSource, result: &mut String, count: usize) - } let gb_encoder = - encoding::label::encoding_from_whatwg_label("GBK").ok_or(Exceptions::ILLEGAL_STATE)?; + CharacterSetECI::GB18030; let encode_string = gb_encoder - .decode(&buffer, encoding::DecoderTrap::Strict) + .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; result.push_str(&encode_string); Ok(()) @@ -250,7 +250,7 @@ fn decodeKanjiSegment( let encoder = { let _ = currentCharacterSetECI; let _ = hints; - encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::FORMAT)? + CharacterSetECI::SJIS }; #[cfg(feature = "allow_forced_iso_ied_18004_compliance")] @@ -258,16 +258,16 @@ fn decodeKanjiSegment( hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) { if let Some(ccse) = ¤tCharacterSetECI { - CharacterSetECI::getCharset(ccse) + CharacterSetECI::getCharacterSetECIByName(ccse) } else { - encoding::all::ISO_8859_1 + CharacterSetECI::ISO8859_1 } } else { - encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::FORMAT)? + CharacterSetECI::SJIS }; let encode_string = encoder - .decode(&buffer, encoding::DecoderTrap::Strict) + .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; result.push_str(&encode_string); @@ -308,37 +308,26 @@ fn decodeByteSegment( if let Some(DecodeHintValue::QrAssumeSpecConformInput(true)) = hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) { - encoding::all::ISO_8859_1 + CharacterSetECI::ISO8859_1 } else { StringUtils::guessCharset(&readBytes, hints) } } else { - CharacterSetECI::getCharset( - currentCharacterSetECI - .as_ref() - .ok_or(Exceptions::ILLEGAL_STATE)?, - ) + currentCharacterSetECI.ok_or(Exceptions::ILLEGAL_STATE)? + // CharacterSetECI::getCharset( + // currentCharacterSetECI + // .as_ref() + // .ok_or(Exceptions::ILLEGAL_STATE)?, + // ) }; - let encode_string = if currentCharacterSetECI.is_some() - && currentCharacterSetECI - .as_ref() - .ok_or(Exceptions::ILLEGAL_STATE)? - == &CharacterSetECI::Cp437 - { - { - use codepage_437::BorrowFromCp437; - use codepage_437::CP437_CONTROL; - - String::borrow_from_cp437(&readBytes, &CP437_CONTROL) - } - } else { + let encode_string = encoding - .decode(&readBytes, encoding::DecoderTrap::Strict) + .decode(&readBytes) .map_err(|e| { Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) })? - }; + ; result.push_str(&encode_string); byteSegments.push(readBytes); diff --git a/src/qrcode/encoder/EncoderTestCase.rs b/src/qrcode/encoder/EncoderTestCase.rs index ba728a4..13a754e 100644 --- a/src/qrcode/encoder/EncoderTestCase.rs +++ b/src/qrcode/encoder/EncoderTestCase.rs @@ -17,20 +17,19 @@ use std::collections::HashMap; use crate::{ - common::BitArray, + common::{BitArray, CharacterSetECI}, qrcode::{ decoder::{ErrorCorrectionLevel, Mode, Version}, encoder::{qrcode_encoder, MinimalEncoder}, }, EncodeHintType, EncodeHintValue, }; -use encoding::EncodingRef; use once_cell::sync::Lazy; use super::QRCode; -static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); +static SHIFT_JIS_CHARSET: Lazy = + Lazy::new(|| CharacterSetECI::SJIS); /** * @author satorux@google.com (Satoru Takabayashi) - creator @@ -1075,10 +1074,9 @@ fn testMinimalEncoder41() { #[test] fn testMinimalEncoder42() { // test halfwidth Katakana character (they are single byte encoded in Shift_JIS) - // NOTE: Changed to windows-31j because that is what is supported in encoding crate verifyMinimalEncoding( "Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}", - "ECI(windows-31j),BYTE(Katakana:......)", + "ECI(shift_jis),BYTE(Katakana:......)", None, false, ); @@ -1100,10 +1098,9 @@ fn testMinimalEncoder44() { // The character \u30A2 encodes as double byte in Shift_JIS but KANJI is not more compact in this case because // KANJI is only more compact when it encodes pairs of characters. In the case of mixed text it can however be // that Shift_JIS encoding is more compact as in this example - // NOTE: Changed to windows-31j because that is what is supported in encoding crate verifyMinimalEncoding( "Katakana:\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}", - "ECI(windows-31j),BYTE(Katakana:.a.a.a.a.a.)", + "ECI(shift_jis),BYTE(Katakana:.a.a.a.a.a.)", None, false, ); @@ -1112,7 +1109,7 @@ fn testMinimalEncoder44() { fn verifyMinimalEncoding( input: &str, expectedRXingResult: &str, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ) { let result = MinimalEncoder::encode_with_details( @@ -1198,7 +1195,7 @@ fn verifyNotGS1EncodedData(qrCode: &QRCode) { fn shiftJISString(bytes: &[u8]) -> String { SHIFT_JIS_CHARSET - .decode(bytes, encoding::DecoderTrap::Strict) + .decode(bytes) .expect("decode should be ok") // return new String(bytes, StringUtils.SHIFT_JIS_CHARSET); } diff --git a/src/qrcode/encoder/minimal_encoder.rs b/src/qrcode/encoder/minimal_encoder.rs index a367701..392469f 100644 --- a/src/qrcode/encoder/minimal_encoder.rs +++ b/src/qrcode/encoder/minimal_encoder.rs @@ -16,10 +16,8 @@ use std::{fmt, rc::Rc}; -use encoding::EncodingRef; - use crate::{ - common::{BitArray, ECIEncoderSet, Result}, + common::{BitArray, ECIEncoderSet, Result, CharacterSetECI}, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, Exceptions, }; @@ -107,7 +105,7 @@ impl MinimalEncoder { */ pub fn new( stringToEncode: &str, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ecLevel: ErrorCorrectionLevel, ) -> Self { @@ -142,7 +140,7 @@ impl MinimalEncoder { pub fn encode_with_details( stringToEncode: &str, version: Option, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ecLevel: ErrorCorrectionLevel, ) -> Result { @@ -1044,10 +1042,10 @@ impl fmt::Display for RXingResultNode { result.push('('); if self.mode == Mode::ECI { result.push_str( - self.encoders + &self.encoders .getCharset(self.charsetEncoderIndex) .ok_or(fmt::Error)? - .name(), + .getCharsetName(), ); } else { let sub_string: String = self diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index 9d99b64..311dc8c 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -19,9 +19,6 @@ */ use std::collections::HashMap; -use encoding::EncodingRef; - -use once_cell::sync::Lazy; use unicode_segmentation::UnicodeSegmentation; use crate::{ @@ -35,8 +32,8 @@ use crate::{ use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; -static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); +static SHIFT_JIS_CHARSET: CharacterSetECI = + CharacterSetECI::SJIS; // The original table is defined in the table 5 of JISX0510:2004 (p.19). const ALPHANUMERIC_TABLE: [i8; 96] = [ @@ -48,7 +45,7 @@ const ALPHANUMERIC_TABLE: [i8; 96] = [ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f ]; -pub const DEFAULT_BYTE_MODE_ENCODING: EncodingRef = encoding::all::ISO_8859_1; +pub const DEFAULT_BYTE_MODE_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; // The mask penalty calculation is complicated. See Table 21 of JISX0510:2004 (p.45) for details. // Basically it applies four rules and summate all penalties. @@ -101,7 +98,7 @@ pub fn encode_with_hints( if has_encoding_hint { if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) { encoding = - Some(encoding::label::encoding_from_whatwg_label(v).ok_or(Exceptions::WRITER)?) + Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) } } @@ -126,12 +123,12 @@ pub fn encode_with_hints( let encoding = if let Some(encoding) = encoding { encoding } else if let Ok(_encs) = - DEFAULT_BYTE_MODE_ENCODING.encode(content, encoding::EncoderTrap::Strict) + DEFAULT_BYTE_MODE_ENCODING.encode(content) { DEFAULT_BYTE_MODE_ENCODING } else { has_encoding_hint = true; - encoding::all::UTF_8 + CharacterSetECI::UTF8 }; // Pick an encoding mode appropriate for the content. Note that this will not attempt to use @@ -144,9 +141,7 @@ pub fn encode_with_hints( // Append ECI segment if applicable if mode == Mode::BYTE && has_encoding_hint { - if let Some(eci) = CharacterSetECI::getCharacterSetECI(encoding) { - appendECI(&eci, &mut header_bits)?; - } + appendECI(&encoding, &mut header_bits)?; } // Append the FNC1 mode header for GS1 formatted data if applicable @@ -304,15 +299,15 @@ pub fn getAlphanumericCode(code: u32) -> i8 { } pub fn chooseMode(content: &str) -> Mode { - chooseModeWithEncoding(content, encoding::all::ISO_8859_1) + chooseModeWithEncoding(content, CharacterSetECI::ISO8859_1) } /** * Choose the best mode by examining the content. Note that 'encoding' is used as a hint; * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ -fn chooseModeWithEncoding(content: &str, encoding: EncodingRef) -> Mode { - if SHIFT_JIS_CHARSET.name() == encoding.name() && isOnlyDoubleByteKanji(content) { +fn chooseModeWithEncoding(content: &str, encoding: CharacterSetECI) -> Mode { + if SHIFT_JIS_CHARSET == encoding && isOnlyDoubleByteKanji(content) { // Choose Kanji mode if all input are double-byte characters return Mode::KANJI; } @@ -337,7 +332,7 @@ fn chooseModeWithEncoding(content: &str, encoding: EncodingRef) -> Mode { } pub fn isOnlyDoubleByteKanji(content: &str) -> bool { - let bytes = if let Ok(byt) = SHIFT_JIS_CHARSET.encode(content, encoding::EncoderTrap::Strict) { + let bytes = if let Ok(byt) = SHIFT_JIS_CHARSET.encode(content) { byt } else { return false; @@ -638,7 +633,7 @@ pub fn appendBytes( content: &str, mode: Mode, bits: &mut BitArray, - encoding: EncodingRef, + encoding: CharacterSetECI, ) -> Result<()> { match mode { Mode::NUMERIC => appendNumericBytes(content, bits), @@ -725,9 +720,9 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()> Ok(()) } -pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: EncodingRef) -> Result<()> { +pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: CharacterSetECI) -> Result<()> { let bytes = encoding - .encode(content, encoding::EncoderTrap::Strict) + .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; for b in bytes { bits.appendBits(b as u32, 8)?; @@ -739,7 +734,7 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<()> { let sjis = &SHIFT_JIS_CHARSET; let bytes = sjis - .encode(content, encoding::EncoderTrap::Strict) + .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; if bytes.len() % 2 != 0 { return Err(Exceptions::writer_with("Kanji byte size not even")); diff --git a/tests/common/pdf_417_multiimage_span.rs b/tests/common/pdf_417_multiimage_span.rs index 7a98925..e7203ba 100644 --- a/tests/common/pdf_417_multiimage_span.rs +++ b/tests/common/pdf_417_multiimage_span.rs @@ -23,9 +23,8 @@ use std::{ rc::Rc, }; -use encoding::Encoding; use rxing::{ - common::{HybridBinarizer, Result}, + common::{HybridBinarizer, Result, CharacterSetECI}, multi::MultipleBarcodeReader, pdf417::PDF417RXingResultMetadata, BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue, @@ -646,9 +645,7 @@ impl PDF417MultiImageSpanAbstractBlackBoxTest if ext == "bin" { let mut buffer: Vec = Vec::new(); File::open(&file)?.read_to_end(&mut buffer)?; - encoding::all::ISO_8859_1 - .decode(&buffer, encoding::DecoderTrap::Replace) - .expect("decode") + CharacterSetECI::ISO8859_1.decode_replace(&buffer).expect("decode") } else { read_to_string(&file).expect("ok") } From 800efb7984e466a056dc80a0f5ea378f4150949d Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 2 Mar 2023 15:54:40 -0600 Subject: [PATCH 03/10] cargo clippy && fmt --- src/aztec/EncoderTest.rs | 19 ++++++--- src/aztec/aztec_writer.rs | 2 +- src/aztec/decoder.rs | 9 ++-- src/aztec/encoder/aztec_encoder.rs | 2 +- src/aztec/encoder/high_level_encoder.rs | 13 +++--- src/aztec/encoder/state.rs | 2 +- src/client/result/VCardResultParser.rs | 6 +-- src/common/StringUtilsTestCase.rs | 13 ++---- src/common/character_set_eci.rs | 41 +++++++++++-------- src/common/eci_encoder_set.rs | 11 +---- src/common/eci_string_builder.rs | 4 +- src/common/minimal_eci_input.rs | 2 +- src/common/string_utils.rs | 11 ++--- src/datamatrix/data_matrix_writer.rs | 5 ++- .../decoder/decoded_bit_stream_parser.rs | 9 +--- src/datamatrix/encoder/encoder_context.rs | 14 +++---- .../encoder/high_level_encode_test_case.rs | 5 ++- src/datamatrix/encoder/high_level_encoder.rs | 2 +- src/datamatrix/encoder/minimal_encoder.rs | 7 +--- src/pdf417/encoder/pdf_417.rs | 2 +- .../encoder/pdf_417_high_level_encoder.rs | 21 +++++----- ...pdf_417_high_level_encoder_test_adapter.rs | 2 +- src/pdf417/pdf_417_writer.rs | 2 +- .../decoder/decoded_bit_stream_parser.rs | 13 ++---- src/qrcode/encoder/EncoderTestCase.rs | 3 +- src/qrcode/encoder/minimal_encoder.rs | 4 +- src/qrcode/encoder/qrcode_encoder.rs | 16 ++++---- tests/common/pdf_417_multiimage_span.rs | 6 ++- 28 files changed, 111 insertions(+), 135 deletions(-) diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index 00a5763..b69f38a 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -23,7 +23,8 @@ use crate::{ encoder::HighLevelEncoder, shared_test_methods::{stripSpace, toBitArray, toBooleanArray}, }, - BarcodeFormat, EncodeHintType, EncodeHintValue, Point, common::CharacterSetECI, + common::CharacterSetECI, + BarcodeFormat, EncodeHintType, EncodeHintValue, Point, }; use super::{encoder::aztec_encoder, AztecWriter}; @@ -137,8 +138,8 @@ X X X X X X X X X X X X X #[test] fn testAztecWriter() { let shift_jis: CharacterSetECI = - CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); - + CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); + 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 @@ -817,7 +818,9 @@ fn testStuffBits(wordSize: usize, bits: &str, expected: &str) { fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::with_charset( - CharacterSetECI::UTF8.encode(s).expect("should encode to bytes"), + CharacterSetECI::UTF8 + .encode(s) + .expect("should encode to bytes"), CharacterSetECI::UTF8, ) .encode() @@ -838,7 +841,9 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { fn testHighLevelEncodeString(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::new( - CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), + CharacterSetECI::ISO8859_1 + .encode(s) + .expect("should encode to bytes"), ) .encode() .expect("high level ok"); @@ -857,7 +862,9 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) { fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) { let bits = HighLevelEncoder::new( - CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), + CharacterSetECI::ISO8859_1 + .encode(s) + .expect("should encode to bytes"), ) .encode() .expect("high level ok"); diff --git a/src/aztec/aztec_writer.rs b/src/aztec/aztec_writer.rs index 7bfa320..24f183a 100644 --- a/src/aztec/aztec_writer.rs +++ b/src/aztec/aztec_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result, CharacterSetECI}, + common::{BitMatrix, CharacterSetECI, Result}, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer, }; diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index 14863fb..48cf998 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -113,7 +113,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // Intermediary buffer of decoded bytes, which is decoded into a string and flushed // when character encoding changes (ECI) or input ends. let mut decoded_bytes: Vec = Vec::new(); - + let mut encdr: CharacterSetECI = CharacterSetECI::ISO8859_1; let mut index = 0; @@ -159,10 +159,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { let mut n = read_code(corrected_bits, index, 3); index += 3; // flush bytes, FLG changes state - result.push_str( - &encdr - .decode(&decoded_bytes)?, - ); + result.push_str(&encdr.decode(&decoded_bytes)?); decoded_bytes.clear(); match n { @@ -206,7 +203,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 = str.as_bytes(); //let b = str.getBytes(StandardCharsets.US_ASCII); //decodedBytes.write(b, 0, b.length); diff --git a/src/aztec/encoder/aztec_encoder.rs b/src/aztec/encoder/aztec_encoder.rs index 899d06f..dcc2db7 100644 --- a/src/aztec/encoder/aztec_encoder.rs +++ b/src/aztec/encoder/aztec_encoder.rs @@ -19,7 +19,7 @@ use crate::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder, }, - BitArray, BitMatrix, Result, CharacterSetECI, + BitArray, BitMatrix, CharacterSetECI, Result, }, exceptions::Exceptions, }; diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index 3fa495a..29bf492 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -14,10 +14,7 @@ * limitations under the License. */ -use crate::{ - common::{BitArray, CharacterSetECI, Result}, - exceptions::Exceptions, -}; +use crate::common::{BitArray, CharacterSetECI, Result}; use super::{State, Token}; @@ -243,10 +240,10 @@ impl HighLevelEncoder { pub fn encode(&self) -> Result { let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0); //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { - if self.charset != CharacterSetECI::ISO8859_1 { - //} && eci != CharacterSetECI::Cp1252 { - initial_state = initial_state.appendFLGn(self.charset.getValue())?; - } + if self.charset != CharacterSetECI::ISO8859_1 { + //} && eci != CharacterSetECI::Cp1252 { + initial_state = initial_state.appendFLGn(self.charset.getValue())?; + } // } else { // return Err(Exceptions::illegal_argument_with( // "No ECI code for character set", diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index 1a20425..73150a7 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -17,7 +17,7 @@ use std::fmt; use crate::{ - common::{BitArray, Result, CharacterSetECI}, + common::{BitArray, CharacterSetECI, Result}, exceptions::Exceptions, }; diff --git a/src/client/result/VCardResultParser.rs b/src/client/result/VCardResultParser.rs index 290b95a..dd85017 100644 --- a/src/client/result/VCardResultParser.rs +++ b/src/client/result/VCardResultParser.rs @@ -20,7 +20,7 @@ use regex::Regex; use once_cell::sync::Lazy; -use crate::{RXingResult, common::CharacterSetECI}; +use crate::{common::CharacterSetECI, RXingResult}; use uriparse::URI; @@ -405,9 +405,7 @@ fn maybeAppendFragment(fragmentBuffer: &mut Vec, charset: &str, result: &mut fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()); // fragment = new String(fragmentBytes, StandardCharsets.UTF_8); } else if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(charset) { - fragment = if let Ok(encoded_result) = - enc.decode(&fragmentBytes) - { + fragment = if let Ok(encoded_result) = enc.decode(&fragmentBytes) { encoded_result } else { String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()) diff --git a/src/common/StringUtilsTestCase.rs b/src/common/StringUtilsTestCase.rs index 882ae3c..78cf3e0 100644 --- a/src/common/StringUtilsTestCase.rs +++ b/src/common/StringUtilsTestCase.rs @@ -40,19 +40,14 @@ fn test_random() { // } assert_eq!( CharacterSetECI::UTF8, - StringUtils::guessCharset(&bytes, &HashMap::new()) - .unwrap() + StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap() ); } #[test] fn test_short_shift_jis1() { // 金魚 - do_test( - &[0x8b, 0xe0, 0x8b, 0x9b], - CharacterSetECI::SJIS, - "SJIS", - ); + do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSetECI::SJIS, "SJIS"); } #[test] @@ -87,7 +82,7 @@ fn test_utf16_be() { do_test( &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], CharacterSetECI::UnicodeBigUnmarked, - &CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), + CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), ); } @@ -97,7 +92,7 @@ fn test_utf16_le() { do_test( &[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67], CharacterSetECI::UTF16LE, - &CharacterSetECI::UTF16LE.getCharsetName(), + CharacterSetECI::UTF16LE.getCharsetName(), ); } diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 25af1fe..8dbeffe 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -17,7 +17,7 @@ use encoding::EncodingRef; use crate::common::Result; -use crate::{Exceptions}; +use crate::Exceptions; /** * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1 @@ -51,11 +51,11 @@ pub enum CharacterSetECI { Cp1256, //(24, "windows-1256"), UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"), UTF16LE, - UTF8, //(26, "UTF-8"), - ASCII, //(new int[] {27, 170}, "US-ASCII"), - Big5, //(28), - GB18030, //(29, "GB2312", "EUC_CN", "GBK"), - EUC_KR, //(30, "EUC-KR"); + UTF8, //(26, "UTF-8"), + ASCII, //(new int[] {27, 170}, "US-ASCII"), + Big5, //(28), + GB18030, //(29, "GB2312", "EUC_CN", "GBK"), + EUC_KR, //(30, "EUC-KR"); } impl CharacterSetECI { // private static final Map VALUE_TO_ECI = new HashMap<>(); @@ -126,7 +126,7 @@ impl CharacterSetECI { } } - pub fn getCharset(&self, ) -> EncodingRef { + pub fn getCharset(&self) -> EncodingRef { let name = match self { // CharacterSetECI::Cp437 => "CP437", CharacterSetECI::Cp437 => "cp437", @@ -161,8 +161,8 @@ impl CharacterSetECI { encoding::label::encoding_from_whatwg_label(name).unwrap() } - pub fn getCharsetName(&self, ) -> &'static str { - match self { + pub fn getCharsetName(&self) -> &'static str { + match self { // CharacterSetECI::Cp437 => "CP437", CharacterSetECI::Cp437 => "cp437", CharacterSetECI::ISO8859_1 => "iso-8859-1", @@ -193,7 +193,6 @@ impl CharacterSetECI { CharacterSetECI::GB18030 => "gb2312", CharacterSetECI::EUC_KR => "euc-kr", } - } /** @@ -317,25 +316,33 @@ impl CharacterSetECI { } pub fn encode(&self, input: &str) -> Result> { - self.getCharset().encode(input, encoding::EncoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + self.getCharset() + .encode(input, encoding::EncoderTrap::Strict) + .map_err(|e| Exceptions::format_with(e.to_string())) } pub fn encode_replace(&self, input: &str) -> Result> { - self.getCharset().encode(input, encoding::EncoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + self.getCharset() + .encode(input, encoding::EncoderTrap::Replace) + .map_err(|e| Exceptions::format_with(e.to_string())) } - pub fn decode(&self, input:&[u8]) -> Result { + pub fn decode(&self, input: &[u8]) -> Result { if self == &CharacterSetECI::Cp437 { use codepage_437::BorrowFromCp437; use codepage_437::CP437_CONTROL; Ok(String::borrow_from_cp437(&input, &CP437_CONTROL)) - }else { - self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } else { + self.getCharset() + .decode(input, encoding::DecoderTrap::Strict) + .map_err(|e| Exceptions::format_with(e.to_string())) } } - pub fn decode_replace(&self, input:&[u8]) -> Result { - self.getCharset().decode(input, encoding::DecoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + pub fn decode_replace(&self, input: &[u8]) -> Result { + self.getCharset() + .decode(input, encoding::DecoderTrap::Replace) + .map_err(|e| Exceptions::format_with(e.to_string())) } } diff --git a/src/common/eci_encoder_set.rs b/src/common/eci_encoder_set.rs index 0b4d9a9..c5dcf14 100644 --- a/src/common/eci_encoder_set.rs +++ b/src/common/eci_encoder_set.rs @@ -112,9 +112,7 @@ impl ECIEncoderSet { for encoder in &neededEncoders { // for (CharsetEncoder encoder : neededEncoders) { let c = stringToEncode.get(i).unwrap(); - if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) - || encoder.encode(c).is_ok() - { + if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) || encoder.encode(c).is_ok() { canEncode = true; break; } @@ -125,12 +123,7 @@ impl ECIEncoderSet { // for encoder in ENCODERS { let encoder = ENCODERS.get(i_encoder).unwrap(); // for (CharsetEncoder encoder : ENCODERS) { - if encoder - .encode( - stringToEncode.get(i).unwrap() - ) - .is_ok() - { + if encoder.encode(stringToEncode.get(i).unwrap()).is_ok() { //Good, we found an encoder that can encode the character. We add him to the list and continue scanning //the input neededEncoders.push(*encoder); diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index 7491b96..e06fdff 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -136,9 +136,7 @@ impl ECIStringBuilder { } else if !self.current_bytes.is_empty() { let bytes = std::mem::take(&mut self.current_bytes); self.current_bytes.clear(); - let encoded_value = encoder - .decode(&bytes) - .unwrap(); + let encoded_value = encoder.decode(&bytes).unwrap(); self.result.push_str(&encoded_value); } } else { diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index 51e586d..f7c80bd 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -21,7 +21,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::common::Result; use crate::Exceptions; -use super::{ECIEncoderSet, ECIInput, CharacterSetECI}; +use super::{CharacterSetECI, ECIEncoderSet, ECIInput}; //* approximated (latch + 2 codewords) pub const COST_PER_ECI: usize = 3; diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index f601c50..2d2f623 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -16,8 +16,6 @@ use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary}; -use once_cell::sync::Lazy; - use super::CharacterSetECI; /** @@ -50,8 +48,7 @@ const ASSUME_SHIFT_JIS: bool = false; // static SHIFT_JIS: &'static str = "SJIS"; // static GB2312: &'static str = "GB2312"; -pub static SHIFT_JIS_CHARSET: CharacterSetECI = - CharacterSetECI::SJIS; +pub static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS; // private static final boolean ASSUME_SHIFT_JIS = // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || @@ -74,7 +71,7 @@ impl StringUtils { } else if c == CharacterSetECI::ISO8859_1 { Some("ISO8859_1") } else { - Some(&c.getCharsetName()) + Some(c.getCharsetName()) } } @@ -92,7 +89,7 @@ impl StringUtils { hints.get(&DecodeHintType::CHARACTER_SET) { // if let DecodeHintValue::CharacterSet(cs_name) = hint { - return CharacterSetECI::getCharacterSetECIByName(cs_name) + return CharacterSetECI::getCharacterSetECIByName(cs_name); // } } // if hints.contains_key(&DecodeHintType::CHARACTER_SET) { @@ -260,6 +257,6 @@ impl StringUtils { return Some(CharacterSetECI::UTF8); } // Otherwise, we take a wild guess with platform encoding - Some(CharacterSetECI::UTF8) + Some(CharacterSetECI::UTF8) } } diff --git a/src/datamatrix/data_matrix_writer.rs b/src/datamatrix/data_matrix_writer.rs index 4409fb4..708f810 100644 --- a/src/datamatrix/data_matrix_writer.rs +++ b/src/datamatrix/data_matrix_writer.rs @@ -18,7 +18,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result, CharacterSetECI}, + common::{BitMatrix, CharacterSetECI, Result}, qrcode::encoder::ByteMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -122,7 +122,8 @@ impl Writer for DataMatrixWriter { hints.get(&EncodeHintType::CHARACTER_SET) else { return Err(Exceptions::illegal_argument_with("charset does not exist")) }; - charset = CharacterSetECI::getCharacterSetECIByName(char_set_name);//encoding::label::encoding_from_whatwg_label(char_set_name); + charset = CharacterSetECI::getCharacterSetECIByName(char_set_name); + //encoding::label::encoding_from_whatwg_label(char_set_name); // charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); } encoded = minimal_encoder::encodeHighLevelWithDetails( diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index 9048601..786f062 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result, CharacterSetECI}, + common::{BitSource, CharacterSetECI, DecoderRXingResult, ECIStringBuilder, Result}, Exceptions, }; @@ -727,12 +727,7 @@ fn decodeBase256Segment( *byte = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8; codewordPosition += 1; } - result.append_string( - - &CharacterSetECI::ISO8859_1 - .decode(&bytes) - ?, - ); + result.append_string(&CharacterSetECI::ISO8859_1.decode(&bytes)?); byteSegments.push(bytes); Ok(()) diff --git a/src/datamatrix/encoder/encoder_context.rs b/src/datamatrix/encoder/encoder_context.rs index 121ec92..9b19fb7 100644 --- a/src/datamatrix/encoder/encoder_context.rs +++ b/src/datamatrix/encoder/encoder_context.rs @@ -16,7 +16,7 @@ use std::rc::Rc; -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use crate::{Dimension, Exceptions}; use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint}; @@ -57,14 +57,10 @@ impl<'a> EncoderContext<'_> { // } // sb.append(ch); // } - let sb = if let Ok(encoded_bytes) = - ISO_8859_1_ENCODER.encode(msg) - { - ISO_8859_1_ENCODER - .decode(&encoded_bytes) - .map_err(|e| { - Exceptions::parse_with(format!("round trip decode should always work: {e}")) - })? + let sb = if let Ok(encoded_bytes) = ISO_8859_1_ENCODER.encode(msg) { + ISO_8859_1_ENCODER.decode(&encoded_bytes).map_err(|e| { + Exceptions::parse_with(format!("round trip decode should always work: {e}")) + })? } else { return Err(Exceptions::illegal_argument_with( "Message contains characters outside ISO-8859-1 encoding.", diff --git a/src/datamatrix/encoder/high_level_encode_test_case.rs b/src/datamatrix/encoder/high_level_encode_test_case.rs index f8c1244..09895b5 100644 --- a/src/datamatrix/encoder/high_level_encode_test_case.rs +++ b/src/datamatrix/encoder/high_level_encode_test_case.rs @@ -18,7 +18,10 @@ use std::rc::Rc; use once_cell::sync::Lazy; -use crate::{datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, common::CharacterSetECI}; +use crate::{ + common::CharacterSetECI, + datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, +}; use super::{high_level_encoder, minimal_encoder, symbol_info, SymbolInfoLookup}; diff --git a/src/datamatrix/encoder/high_level_encoder.rs b/src/datamatrix/encoder/high_level_encoder.rs index 5606d7e..a0dd06b 100644 --- a/src/datamatrix/encoder/high_level_encoder.rs +++ b/src/datamatrix/encoder/high_level_encoder.rs @@ -16,7 +16,7 @@ use std::rc::Rc; -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use crate::{Dimension, Exceptions}; use super::{ diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index 1d5dd06..3cf89b2 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{ECIInput, MinimalECIInput, Result, CharacterSetECI}, + common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, Exceptions, }; @@ -171,10 +171,7 @@ pub fn encodeHighLevelWithDetails( msg = &msg[high_level_encoder::MACRO_06_HEADER.chars().count()..(msg.chars().count() - 2)]; } Ok(ISO_8859_1_ENCODER - .decode( - &encode(msg, priorityCharset, fnc1, shape, macroId)? - - ) + .decode(&encode(msg, priorityCharset, fnc1, shape, macroId)?) .expect("should decode")) // return new String(encode(msg, priorityCharset, fnc1, shape, macroId), StandardCharsets.ISO_8859_1); } diff --git a/src/pdf417/encoder/pdf_417.rs b/src/pdf417/encoder/pdf_417.rs index 3ee462b..070f59d 100644 --- a/src/pdf417/encoder/pdf_417.rs +++ b/src/pdf417/encoder/pdf_417.rs @@ -18,7 +18,7 @@ * This file has been modified from its original form in Barcode4J. */ -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use crate::Exceptions; use super::{ diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index cc7285e..2d959c7 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -197,16 +197,17 @@ pub fn encodeHighLevel( input = Box::new(NoECIInput::new(msg.to_owned())); if encoding.is_none() { encoding = Some(DEFAULT_ENCODING); - } else if &DEFAULT_ENCODING - != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)? - { + } else if &DEFAULT_ENCODING != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)? { // if let Some(eci) = // CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) // { // encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; // } - encodingECI(CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, &mut sb)?; + encodingECI( + CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, + &mut sb, + )?; } } @@ -768,12 +769,7 @@ fn determineConsecutiveBinaryCount( } if let Some(encoder) = encoding { - let can_encode = encoder - .encode( - &input.charAt(idx)?.to_string() - - ) - .is_ok(); + let can_encode = encoder.encode(&input.charAt(idx)?.to_string()).is_ok(); if !can_encode { if TypeId::of::() != TypeId::of::() { @@ -856,7 +852,10 @@ impl Display for NoECIInput { */ #[cfg(test)] mod PDF417EncoderTestCase { - use crate::{pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, common::CharacterSetECI}; + use crate::{ + common::CharacterSetECI, + pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, + }; #[test] fn testEncodeAuto() { diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs index ee37383..7554081 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use super::{pdf_417_high_level_encoder, Compaction}; diff --git a/src/pdf417/pdf_417_writer.rs b/src/pdf417/pdf_417_writer.rs index 9f81929..77510c4 100644 --- a/src/pdf417/pdf_417_writer.rs +++ b/src/pdf417/pdf_417_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result, CharacterSetECI}, + common::{BitMatrix, CharacterSetECI, Result}, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index d24b3da..f9cd559 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -203,8 +203,7 @@ fn decodeHanziSegment(bits: &mut BitSource, result: &mut String, count: usize) - count -= 1; } - let gb_encoder = - CharacterSetECI::GB18030; + let gb_encoder = CharacterSetECI::GB18030; let encode_string = gb_encoder .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; @@ -321,13 +320,9 @@ fn decodeByteSegment( // ) }; - let encode_string = - encoding - .decode(&readBytes) - .map_err(|e| { - Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) - })? - ; + let encode_string = encoding.decode(&readBytes).map_err(|e| { + Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) + })?; result.push_str(&encode_string); byteSegments.push(readBytes); diff --git a/src/qrcode/encoder/EncoderTestCase.rs b/src/qrcode/encoder/EncoderTestCase.rs index 13a754e..abcfe6b 100644 --- a/src/qrcode/encoder/EncoderTestCase.rs +++ b/src/qrcode/encoder/EncoderTestCase.rs @@ -28,8 +28,7 @@ use once_cell::sync::Lazy; use super::QRCode; -static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| CharacterSetECI::SJIS); +static SHIFT_JIS_CHARSET: Lazy = Lazy::new(|| CharacterSetECI::SJIS); /** * @author satorux@google.com (Satoru Takabayashi) - creator diff --git a/src/qrcode/encoder/minimal_encoder.rs b/src/qrcode/encoder/minimal_encoder.rs index 392469f..d072620 100644 --- a/src/qrcode/encoder/minimal_encoder.rs +++ b/src/qrcode/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{BitArray, ECIEncoderSet, Result, CharacterSetECI}, + common::{BitArray, CharacterSetECI, ECIEncoderSet, Result}, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, Exceptions, }; @@ -1042,7 +1042,7 @@ impl fmt::Display for RXingResultNode { result.push('('); if self.mode == Mode::ECI { result.push_str( - &self.encoders + self.encoders .getCharset(self.charsetEncoderIndex) .ok_or(fmt::Error)? .getCharsetName(), diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index 311dc8c..8b6b812 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -32,8 +32,7 @@ use crate::{ use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; -static SHIFT_JIS_CHARSET: CharacterSetECI = - CharacterSetECI::SJIS; +static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS; // The original table is defined in the table 5 of JISX0510:2004 (p.19). const ALPHANUMERIC_TABLE: [i8; 96] = [ @@ -97,8 +96,7 @@ pub fn encode_with_hints( let mut has_encoding_hint = hints.contains_key(&EncodeHintType::CHARACTER_SET); if has_encoding_hint { if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) { - encoding = - Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) + encoding = Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) } } @@ -122,9 +120,7 @@ pub fn encode_with_hints( //Switch to default encoding let encoding = if let Some(encoding) = encoding { encoding - } else if let Ok(_encs) = - DEFAULT_BYTE_MODE_ENCODING.encode(content) - { + } else if let Ok(_encs) = DEFAULT_BYTE_MODE_ENCODING.encode(content) { DEFAULT_BYTE_MODE_ENCODING } else { has_encoding_hint = true; @@ -720,7 +716,11 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()> Ok(()) } -pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: CharacterSetECI) -> Result<()> { +pub fn append8BitBytes( + content: &str, + bits: &mut BitArray, + encoding: CharacterSetECI, +) -> Result<()> { let bytes = encoding .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; diff --git a/tests/common/pdf_417_multiimage_span.rs b/tests/common/pdf_417_multiimage_span.rs index e7203ba..9e8969d 100644 --- a/tests/common/pdf_417_multiimage_span.rs +++ b/tests/common/pdf_417_multiimage_span.rs @@ -24,7 +24,7 @@ use std::{ }; use rxing::{ - common::{HybridBinarizer, Result, CharacterSetECI}, + common::{CharacterSetECI, HybridBinarizer, Result}, multi::MultipleBarcodeReader, pdf417::PDF417RXingResultMetadata, BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue, @@ -645,7 +645,9 @@ impl PDF417MultiImageSpanAbstractBlackBoxTest if ext == "bin" { let mut buffer: Vec = Vec::new(); File::open(&file)?.read_to_end(&mut buffer)?; - CharacterSetECI::ISO8859_1.decode_replace(&buffer).expect("decode") + CharacterSetECI::ISO8859_1 + .decode_replace(&buffer) + .expect("decode") } else { read_to_string(&file).expect("ok") } From c4fec7d2ee86af4df1c5c0dc645a2b67e9cad2aa Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Wed, 1 Mar 2023 20:53:51 -0600 Subject: [PATCH 04/10] add encode / decode methods This will eventually entirely replace EncoderRef being passed around --- src/common/character_set_eci.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 61b607a..2e97b1f 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -26,7 +26,7 @@ use encoding::EncodingRef; use crate::common::Result; -use crate::Exceptions; +use crate::{Exceptions}; /** * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1 @@ -133,8 +133,8 @@ impl CharacterSetECI { } } - pub fn getCharset(cs_eci: &CharacterSetECI) -> EncodingRef { - let name = match cs_eci { + pub fn getCharset(&self, ) -> EncodingRef { + let name = match self { // CharacterSetECI::Cp437 => "CP437", CharacterSetECI::Cp437 => "UTF-8", CharacterSetECI::ISO8859_1 => "ISO-8859-1", @@ -286,4 +286,20 @@ impl CharacterSetECI { _ => None, } } + + pub fn encode(&self, input: &str) -> Result> { + self.getCharset().encode(input, encoding::EncoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } + + pub fn encode_replace(&self, input: &str) -> Result> { + self.getCharset().encode(input, encoding::EncoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + } + + pub fn decode(&self, input:&[u8]) -> Result { + self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } + + pub fn decode_replace(&self, input:&[u8]) -> Result { + self.getCharset().decode(input, encoding::DecoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + } } From a0b8b6886969aad3ae56c291a98bc53b1a761806 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 2 Mar 2023 15:50:56 -0600 Subject: [PATCH 05/10] repurpose CharacterSetECI as encoding abstraction --- src/aztec/EncoderTest.rs | 41 +++---- src/aztec/aztec_writer.rs | 11 +- src/aztec/decoder.rs | 13 +-- src/aztec/encoder/aztec_encoder.rs | 21 ++-- src/aztec/encoder/high_level_encoder.rs | 22 ++-- src/aztec/encoder/state.rs | 8 +- src/client/result/VCardResultParser.rs | 6 +- src/common/StringUtilsTestCase.rs | 26 ++--- src/common/character_set_eci.rs | 106 ++++++++++++------ src/common/eci_encoder_set.rs | 64 +++++------ src/common/eci_string_builder.rs | 34 +++--- src/common/minimal_eci_input.rs | 5 +- src/common/string_utils.rs | 40 +++---- src/datamatrix/data_matrix_writer.rs | 8 +- .../decoder/decoded_bit_stream_parser.rs | 11 +- src/datamatrix/encoder/encoder_context.rs | 9 +- .../encoder/high_level_encode_test_case.rs | 4 +- src/datamatrix/encoder/high_level_encoder.rs | 6 +- src/datamatrix/encoder/minimal_encoder.rs | 16 ++- .../decoder/pdf_417_decoder_test_case.rs | 10 +- src/pdf417/encoder/pdf_417.rs | 8 +- .../encoder/pdf_417_high_level_encoder.rs | 46 ++++---- ...pdf_417_high_level_encoder_test_adapter.rs | 6 +- src/pdf417/pdf_417_writer.rs | 4 +- .../decoder/decoded_bit_stream_parser.rs | 45 +++----- src/qrcode/encoder/EncoderTestCase.rs | 17 ++- src/qrcode/encoder/minimal_encoder.rs | 12 +- src/qrcode/encoder/qrcode_encoder.rs | 35 +++--- tests/common/pdf_417_multiimage_span.rs | 7 +- 29 files changed, 304 insertions(+), 337 deletions(-) diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index bf7be82..00a5763 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -16,8 +16,6 @@ use std::collections::HashMap; -use encoding::EncodingRef; - use crate::{ aztec::{ aztec_detector_result::AztecDetectorRXingResult, @@ -25,7 +23,7 @@ use crate::{ encoder::HighLevelEncoder, shared_test_methods::{stripSpace, toBitArray, toBooleanArray}, }, - BarcodeFormat, EncodeHintType, EncodeHintValue, Point, + BarcodeFormat, EncodeHintType, EncodeHintValue, Point, common::CharacterSetECI, }; use super::{encoder::aztec_encoder, AztecWriter}; @@ -34,8 +32,6 @@ use crate::Writer; use rand::Rng; -use encoding::Encoding; - /** * Aztec 2D generator unit tests. * @@ -43,10 +39,10 @@ use encoding::Encoding; * @author Frank Yellin */ -const ISO_8859_1: EncodingRef = encoding::all::ISO_8859_1; //StandardCharsets.ISO_8859_1; -const UTF_8: EncodingRef = encoding::all::UTF_8; //StandardCharsets.UTF_8; -const ISO_8859_15: EncodingRef = encoding::all::ISO_8859_15; //Charset.forName("ISO-8859-15"); -const WINDOWS_1252: EncodingRef = encoding::all::WINDOWS_1252; //Charset.forName("Windows-1252"); +const ISO_8859_1: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1; +const UTF_8: CharacterSetECI = CharacterSetECI::UTF8; //StandardCharsets.UTF_8; +const ISO_8859_15: CharacterSetECI = CharacterSetECI::ISO8859_15; //Charset.forName("ISO-8859-15"); +const WINDOWS_1252: CharacterSetECI = CharacterSetECI::Cp1252; //Charset.forName("Windows-1252"); // const DOTX: &str = "[^.X]"; // const SPACES: &str = "\\s+"; @@ -140,8 +136,9 @@ X X X X X X X X X X X X X #[test] fn testAztecWriter() { - let shift_jis: EncodingRef = - encoding::label::encoding_from_whatwg_label("Shift_JIS").expect("must exist"); + let shift_jis: CharacterSetECI = + CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); + 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 @@ -150,7 +147,7 @@ fn testAztecWriter() { testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 0, true, 2); testWriter( "\u{20AC} 1 sample data.", - Some(encoding::all::UTF_16BE), + Some(CharacterSetECI::UnicodeBigUnmarked), 0, true, 3, @@ -711,7 +708,7 @@ fn testEncodeDecode(data: &str, compact: bool, layers: u32) { fn testWriter( data: &str, - charset: Option, + charset: Option, ecc_percent: u32, compact: bool, layers: u32, @@ -721,7 +718,7 @@ fn testWriter( if charset.is_some() { hints.insert( EncodeHintType::CHARACTER_SET, - EncodeHintValue::CharacterSet(charset.unwrap().name().to_owned()), + EncodeHintValue::CharacterSet(charset.unwrap().getCharsetName().to_string()), ); } // if (null != charset) { @@ -737,7 +734,7 @@ fn testWriter( let cset = match charset { Some(cs) => cs, - None => encoding::all::ISO_8859_1, + None => CharacterSetECI::ISO8859_1, }; let aztec = aztec_encoder::encode_with_charset( data, @@ -820,10 +817,8 @@ fn testStuffBits(wordSize: usize, bits: &str, expected: &str) { fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::with_charset( - encoding::all::UTF_8 - .encode(s, encoding::EncoderTrap::Strict) - .expect("should encode to bytes"), - encoding::all::UTF_8, + CharacterSetECI::UTF8.encode(s).expect("should encode to bytes"), + CharacterSetECI::UTF8, ) .encode() .expect("high level ok"); @@ -843,9 +838,7 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { fn testHighLevelEncodeString(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::new( - encoding::all::ISO_8859_1 - .encode(s, encoding::EncoderTrap::Strict) - .expect("should encode to bytes"), + CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), ) .encode() .expect("high level ok"); @@ -864,9 +857,7 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) { fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) { let bits = HighLevelEncoder::new( - encoding::all::ISO_8859_1 - .encode(s, encoding::EncoderTrap::Strict) - .expect("should encode to bytes"), + CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), ) .encode() .expect("high level ok"); diff --git a/src/aztec/aztec_writer.rs b/src/aztec/aztec_writer.rs index 7eb7118..7bfa320 100644 --- a/src/aztec/aztec_writer.rs +++ b/src/aztec/aztec_writer.rs @@ -16,10 +16,8 @@ use std::collections::HashMap; -use encoding::EncodingRef; - use crate::{ - common::{BitMatrix, Result}, + common::{BitMatrix, Result, CharacterSetECI}, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer, }; @@ -58,10 +56,7 @@ impl Writer for AztecWriter { hints.get(&EncodeHintType::CHARACTER_SET) { if cset_name.to_lowercase() != "iso-8859-1" { - charset = Some( - encoding::label::encoding_from_whatwg_label(cset_name) - .ok_or(Exceptions::ILLEGAL_ARGUMENT)?, - ); + charset = CharacterSetECI::getCharacterSetECIByName(cset_name); } } if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) = @@ -91,7 +86,7 @@ fn encode( format: BarcodeFormat, width: u32, height: u32, - charset: Option, + charset: Option, ecc_percent: u32, layers: i32, ) -> Result { diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index a0902db..14863fb 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -113,8 +113,8 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // Intermediary buffer of decoded bytes, which is decoded into a string and flushed // when character encoding changes (ECI) or input ends. let mut decoded_bytes: Vec = Vec::new(); - // let mut encdr: &'static dyn encoding::Encoding = encoding::all::UTF_8; - let mut encdr: encoding::EncodingRef = encoding::all::ISO_8859_1; + + let mut encdr: CharacterSetECI = CharacterSetECI::ISO8859_1; let mut index = 0; @@ -161,8 +161,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // flush bytes, FLG changes state result.push_str( &encdr - .decode(&decoded_bytes, encoding::DecoderTrap::Strict) - .map_err(Exceptions::illegal_state_with)?, + .decode(&decoded_bytes)?, ); decoded_bytes.clear(); @@ -190,7 +189,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { if charset_eci.is_err() { return Err(Exceptions::format_with("Charset must exist")); } - encdr = CharacterSetECI::getCharset(&charset_eci?); + encdr = charset_eci?; } } // Go back to whatever mode we had been in @@ -207,7 +206,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); @@ -220,7 +219,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { } } //try { - if let Ok(str) = encdr.decode(&decoded_bytes, encoding::DecoderTrap::Strict) { + if let Ok(str) = encdr.decode(&decoded_bytes) { result.push_str(&str); } else { return Err(Exceptions::illegal_state_with("bad encoding")); diff --git a/src/aztec/encoder/aztec_encoder.rs b/src/aztec/encoder/aztec_encoder.rs index cbb7d56..99e9265 100644 --- a/src/aztec/encoder/aztec_encoder.rs +++ b/src/aztec/encoder/aztec_encoder.rs @@ -14,14 +14,12 @@ * limitations under the License. */ -use encoding::Encoding; - use crate::{ common::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder, }, - BitArray, BitMatrix, Result, + BitArray, BitMatrix, Result, CharacterSetECI, }, exceptions::Exceptions, }; @@ -51,10 +49,9 @@ pub const WORD_SIZE: [u32; 33] = [ * @return Aztec symbol matrix with metadata */ pub fn encode_simple(data: &str) -> Result { - let Ok(bytes) = encoding::all::ISO_8859_1 - .encode(data, encoding::EncoderTrap::Replace) else { - return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1"))); - }; + let Ok(bytes) =CharacterSetECI::ISO8859_1.encode_replace(data) else { + return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1"))); + }; encode_bytes_simple(&bytes) } @@ -68,7 +65,7 @@ pub fn encode_simple(data: &str) -> Result { * @return Aztec symbol matrix with metadata */ pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result { - if let Ok(bytes) = encoding::all::ISO_8859_1.encode(data, encoding::EncoderTrap::Strict) { + if let Ok(bytes) = CharacterSetECI::ISO8859_1.encode(data) { encode_bytes(&bytes, minECCPercent, userSpecifiedLayers) } else { Err(Exceptions::illegal_argument_with(format!( @@ -93,9 +90,9 @@ pub fn encode_with_charset( data: &str, minECCPercent: u32, userSpecifiedLayers: i32, - charset: encoding::EncodingRef, + charset: CharacterSetECI, ) -> Result { - if let Ok(bytes) = charset.encode(data, encoding::EncoderTrap::Strict) { + if let Ok(bytes) = charset.encode(data) { encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset) } else { Err(Exceptions::illegal_argument_with(format!( @@ -132,7 +129,7 @@ pub fn encode_bytes( data, minECCPercent, userSpecifiedLayers, - encoding::all::ISO_8859_1, + CharacterSetECI::ISO8859_1, ) } @@ -151,7 +148,7 @@ pub fn encode_bytes_with_charset( data: &[u8], min_eccpercent: u32, user_specified_layers: i32, - charset: encoding::EncodingRef, + charset: CharacterSetECI, ) -> Result { // High-level encode let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?; diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index 12bc719..3fa495a 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -35,7 +35,7 @@ use super::{State, Token}; */ pub struct HighLevelEncoder { text: Vec, - charset: encoding::EncodingRef, + charset: CharacterSetECI, } impl HighLevelEncoder { @@ -229,11 +229,11 @@ impl HighLevelEncoder { pub fn new(text: Vec) -> Self { Self { text, - charset: encoding::all::ISO_8859_1, + charset: CharacterSetECI::ISO8859_1, } } - pub fn with_charset(text: Vec, charset: encoding::EncodingRef) -> Self { + pub fn with_charset(text: Vec, charset: CharacterSetECI) -> Self { Self { text, charset } } @@ -242,16 +242,16 @@ impl HighLevelEncoder { */ pub fn encode(&self) -> Result { let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0); - if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { - if eci != CharacterSetECI::ISO8859_1 { + //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { + if self.charset != CharacterSetECI::ISO8859_1 { //} && eci != CharacterSetECI::Cp1252 { - initial_state = initial_state.appendFLGn(CharacterSetECI::getValue(&eci))?; + initial_state = initial_state.appendFLGn(self.charset.getValue())?; } - } else { - return Err(Exceptions::illegal_argument_with( - "No ECI code for character set", - )); - } + // } else { + // return Err(Exceptions::illegal_argument_with( + // "No ECI code for character set", + // )); + // } // if self.charset != null { // CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset); // if (null == eci) { diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index e90f3ef..1a20425 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -16,10 +16,8 @@ use std::fmt; -use encoding::Encoding; - use crate::{ - common::{BitArray, Result}, + common::{BitArray, Result, CharacterSetECI}, exceptions::Exceptions, }; @@ -88,8 +86,8 @@ impl State { )); // throw new IllegalArgumentException("ECI code must be between 0 and 999999"); } else { - let Ok(eci_digits) = encoding::all::ISO_8859_1 - .encode(&format!("{eci}"), encoding::EncoderTrap::Strict) + let Ok(eci_digits) = CharacterSetECI::ISO8859_1 + .encode(&format!("{eci}")) else { return Err(Exceptions::ILLEGAL_ARGUMENT) }; diff --git a/src/client/result/VCardResultParser.rs b/src/client/result/VCardResultParser.rs index d21617b..290b95a 100644 --- a/src/client/result/VCardResultParser.rs +++ b/src/client/result/VCardResultParser.rs @@ -20,7 +20,7 @@ use regex::Regex; use once_cell::sync::Lazy; -use crate::RXingResult; +use crate::{RXingResult, common::CharacterSetECI}; use uriparse::URI; @@ -404,9 +404,9 @@ fn maybeAppendFragment(fragmentBuffer: &mut Vec, charset: &str, result: &mut if charset.is_empty() { fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()); // fragment = new String(fragmentBytes, StandardCharsets.UTF_8); - } else if let Some(enc) = encoding::label::encoding_from_whatwg_label(charset) { + } else if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(charset) { fragment = if let Ok(encoded_result) = - enc.decode(&fragmentBytes, encoding::DecoderTrap::Strict) + enc.decode(&fragmentBytes) { encoded_result } else { diff --git a/src/common/StringUtilsTestCase.rs b/src/common/StringUtilsTestCase.rs index 4f3ec67..882ae3c 100644 --- a/src/common/StringUtilsTestCase.rs +++ b/src/common/StringUtilsTestCase.rs @@ -23,12 +23,13 @@ // import java.nio.charset.StandardCharsets; // import java.util.Random; -use encoding::{Encoding, EncodingRef}; use rand::Rng; use std::collections::HashMap; use crate::common::StringUtils; +use super::CharacterSetECI; + #[test] fn test_random() { let mut r = rand::thread_rng(); @@ -38,10 +39,9 @@ fn test_random() { // *byte = r.gen(); // } assert_eq!( - encoding::all::UTF_8.name(), + CharacterSetECI::UTF8, StringUtils::guessCharset(&bytes, &HashMap::new()) .unwrap() - .name() ); } @@ -50,7 +50,7 @@ fn test_short_shift_jis1() { // 金魚 do_test( &[0x8b, 0xe0, 0x8b, 0x9b], - encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), + CharacterSetECI::SJIS, "SJIS", ); } @@ -58,7 +58,7 @@ fn test_short_shift_jis1() { #[test] fn test_short_iso885911() { // båd - do_test(&[0x62, 0xe5, 0x64], encoding::all::ISO_8859_1, "ISO8859_1"); + do_test(&[0x62, 0xe5, 0x64], CharacterSetECI::ISO8859_1, "ISO8859_1"); } #[test] @@ -66,7 +66,7 @@ fn test_short_utf8() { // Español do_test( &[0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c], - encoding::all::UTF_8, + CharacterSetECI::UTF8, "UTF8", ); } @@ -76,7 +76,7 @@ fn test_mixed_shift_jis1() { // Hello 金! do_test( &[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21], - encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), + CharacterSetECI::SJIS, "SJIS", ); } @@ -86,8 +86,8 @@ fn test_utf16_be() { // 调压柜 do_test( &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], - encoding::all::UTF_16BE, - encoding::all::UTF_16BE.name(), + CharacterSetECI::UnicodeBigUnmarked, + &CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), ); } @@ -96,15 +96,15 @@ fn test_utf16_le() { // 调压柜 do_test( &[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67], - encoding::all::UTF_16LE, - encoding::all::UTF_16LE.name(), + CharacterSetECI::UTF16LE, + &CharacterSetECI::UTF16LE.getCharsetName(), ); } -fn do_test(bytes: &[u8], charset: EncodingRef, encoding: &str) { +fn do_test(bytes: &[u8], charset: CharacterSetECI, encoding: &str) { let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap(); let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new()).unwrap(); - assert_eq!(charset.name(), guessedCharset.name()); + assert_eq!(charset, guessedCharset); assert_eq!(encoding, guessedEncoding); } diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 2e97b1f..25af1fe 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -14,15 +14,6 @@ * limitations under the License. */ -// package com.google.zxing.common; - -// import com.google.zxing.FormatException; - -// import java.nio.charset.Charset; - -// import java.util.HashMap; -// import java.util.Map; - use encoding::EncodingRef; use crate::common::Result; @@ -59,6 +50,7 @@ pub enum CharacterSetECI { Cp1252, //(23, "windows-1252"), Cp1256, //(24, "windows-1256"), UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"), + UTF16LE, UTF8, //(26, "UTF-8"), ASCII, //(new int[] {27, 170}, "US-ASCII"), Big5, //(28), @@ -101,8 +93,8 @@ impl CharacterSetECI { Self::getValue(self) } - pub fn getValue(cs_eci: &CharacterSetECI) -> u32 { - match cs_eci { + pub fn getValue(&self) -> u32 { + match self { CharacterSetECI::Cp437 => 0, CharacterSetECI::ISO8859_1 => 1, CharacterSetECI::ISO8859_2 => 4, @@ -125,6 +117,7 @@ impl CharacterSetECI { CharacterSetECI::Cp1252 => 23, CharacterSetECI::Cp1256 => 24, CharacterSetECI::UnicodeBigUnmarked => 25, + CharacterSetECI::UTF16LE => 100025, CharacterSetECI::UTF8 => 26, CharacterSetECI::ASCII => 27, CharacterSetECI::Big5 => 28, @@ -136,8 +129,8 @@ impl CharacterSetECI { pub fn getCharset(&self, ) -> EncodingRef { let name = match self { // CharacterSetECI::Cp437 => "CP437", - CharacterSetECI::Cp437 => "UTF-8", - CharacterSetECI::ISO8859_1 => "ISO-8859-1", + CharacterSetECI::Cp437 => "cp437", + CharacterSetECI::ISO8859_1 => return encoding::all::ISO_8859_1, CharacterSetECI::ISO8859_2 => "ISO-8859-2", CharacterSetECI::ISO8859_3 => "ISO-8859-3", CharacterSetECI::ISO8859_4 => "ISO-8859-4", @@ -152,12 +145,13 @@ impl CharacterSetECI { // CharacterSetECI::ISO8859_14 => "ISO-8859-14", CharacterSetECI::ISO8859_15 => "ISO-8859-15", CharacterSetECI::ISO8859_16 => "ISO-8859-16", - CharacterSetECI::SJIS => "Shift_JIS", + CharacterSetECI::SJIS => "shift_jis", CharacterSetECI::Cp1250 => "windows-1250", CharacterSetECI::Cp1251 => "windows-1251", CharacterSetECI::Cp1252 => "windows-1252", CharacterSetECI::Cp1256 => "windows-1256", CharacterSetECI::UnicodeBigUnmarked => "UTF-16BE", + CharacterSetECI::UTF16LE => "UTF-16LE", CharacterSetECI::UTF8 => "UTF-8", CharacterSetECI::ASCII => "US-ASCII", CharacterSetECI::Big5 => "Big5", @@ -167,6 +161,41 @@ impl CharacterSetECI { encoding::label::encoding_from_whatwg_label(name).unwrap() } + pub fn getCharsetName(&self, ) -> &'static str { + match self { + // CharacterSetECI::Cp437 => "CP437", + CharacterSetECI::Cp437 => "cp437", + CharacterSetECI::ISO8859_1 => "iso-8859-1", + CharacterSetECI::ISO8859_2 => "iso-8859-2", + CharacterSetECI::ISO8859_3 => "iso-8859-3", + CharacterSetECI::ISO8859_4 => "iso-8859-4", + CharacterSetECI::ISO8859_5 => "iso-8859-5", + // CharacterSetECI::ISO8859_6 => "ISO-8859-6", + CharacterSetECI::ISO8859_7 => "iso-8859-7", + // CharacterSetECI::ISO8859_8 => "ISO-8859-8", + CharacterSetECI::ISO8859_9 => "iso-8859-9", + // CharacterSetECI::ISO8859_10 => "ISO-8859-10", + // CharacterSetECI::ISO8859_11 => "ISO-8859-11", + CharacterSetECI::ISO8859_13 => "iso-8859-13", + // CharacterSetECI::ISO8859_14 => "ISO-8859-14", + CharacterSetECI::ISO8859_15 => "iso-8859-15", + CharacterSetECI::ISO8859_16 => "iso-8859-16", + CharacterSetECI::SJIS => "shift_jis", + CharacterSetECI::Cp1250 => "windows-1250", + CharacterSetECI::Cp1251 => "windows-1251", + CharacterSetECI::Cp1252 => "windows-1252", + CharacterSetECI::Cp1256 => "windows-1256", + CharacterSetECI::UnicodeBigUnmarked => "utf-16be", + CharacterSetECI::UTF16LE => "utf-16le", + CharacterSetECI::UTF8 => "utf-8", + CharacterSetECI::ASCII => "us-ascii", + CharacterSetECI::Big5 => "big5", + CharacterSetECI::GB18030 => "gb2312", + CharacterSetECI::EUC_KR => "euc-kr", + } + + } + /** * @param charset Java character set object * @return CharacterSetECI representing ECI for character encoding, or null if it is legal @@ -179,7 +208,7 @@ impl CharacterSetECI { charset.name() }; match name { - "CP437" => Some(CharacterSetECI::Cp437), + "cp437" => Some(CharacterSetECI::Cp437), "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), @@ -201,7 +230,7 @@ impl CharacterSetECI { "windows-1252" => Some(CharacterSetECI::Cp1252), "windows-1256" => Some(CharacterSetECI::Cp1256), "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), - "utf-8" => Some(CharacterSetECI::UTF8), + "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), "us-ascii" => Some(CharacterSetECI::ASCII), "big5" => Some(CharacterSetECI::Big5), "gb2312" => Some(CharacterSetECI::GB18030), @@ -255,34 +284,34 @@ impl CharacterSetECI { * but unsupported */ pub fn getCharacterSetECIByName(name: &str) -> Option { - match name { - "CP437" => Some(CharacterSetECI::Cp437), - "ISO-8859-1" => Some(CharacterSetECI::ISO8859_1), - "ISO-8859-2" => Some(CharacterSetECI::ISO8859_2), - "ISO-8859-3" => Some(CharacterSetECI::ISO8859_3), - "ISO-8859-4" => Some(CharacterSetECI::ISO8859_4), - "ISO-8859-5" => Some(CharacterSetECI::ISO8859_5), + match name.to_lowercase().as_str() { + "cp437" => Some(CharacterSetECI::Cp437), + "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), + "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), + "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), + "iso-8859-4" => Some(CharacterSetECI::ISO8859_4), + "iso-8859-5" => Some(CharacterSetECI::ISO8859_5), // "ISO-8859-6" => Some(CharacterSetECI::ISO8859_6), - "ISO-8859-7" => Some(CharacterSetECI::ISO8859_7), + "iso-8859-7" => Some(CharacterSetECI::ISO8859_7), // "ISO-8859-8" => Some(CharacterSetECI::ISO8859_8), - "ISO-8859-9" => Some(CharacterSetECI::ISO8859_9), + "iso-8859-9" => Some(CharacterSetECI::ISO8859_9), // "ISO-8859-10" => Some(CharacterSetECI::ISO8859_10), // "ISO-8859-11" => Some(CharacterSetECI::ISO8859_11), - "ISO-8859-13" => Some(CharacterSetECI::ISO8859_13), + "iso-8859-13" => Some(CharacterSetECI::ISO8859_13), // "ISO-8859-14" => Some(CharacterSetECI::ISO8859_14), - "ISO-8859-15" => Some(CharacterSetECI::ISO8859_15), - "ISO-8859-16" => Some(CharacterSetECI::ISO8859_16), - "Shift_JIS" => Some(CharacterSetECI::SJIS), + "iso-8859-15" => Some(CharacterSetECI::ISO8859_15), + "iso-8859-16" => Some(CharacterSetECI::ISO8859_16), + "shift_jis" => Some(CharacterSetECI::SJIS), "windows-1250" => Some(CharacterSetECI::Cp1250), "windows-1251" => Some(CharacterSetECI::Cp1251), "windows-1252" => Some(CharacterSetECI::Cp1252), "windows-1256" => Some(CharacterSetECI::Cp1256), - "UTF-16BE" => Some(CharacterSetECI::UnicodeBigUnmarked), - "UTF-8" => Some(CharacterSetECI::UTF8), - "US-ASCII" => Some(CharacterSetECI::ASCII), - "Big5" => Some(CharacterSetECI::Big5), - "GB2312" => Some(CharacterSetECI::GB18030), - "EUC-KR" => Some(CharacterSetECI::EUC_KR), + "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), + "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), + "us-ascii" => Some(CharacterSetECI::ASCII), + "big5" => Some(CharacterSetECI::Big5), + "gb2312" => Some(CharacterSetECI::GB18030), + "euc-kr" => Some(CharacterSetECI::EUC_KR), _ => None, } } @@ -296,7 +325,14 @@ impl CharacterSetECI { } pub fn decode(&self, input:&[u8]) -> Result { + if self == &CharacterSetECI::Cp437 { + use codepage_437::BorrowFromCp437; + use codepage_437::CP437_CONTROL; + + Ok(String::borrow_from_cp437(&input, &CP437_CONTROL)) + }else { self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } } pub fn decode_replace(&self, input:&[u8]) -> Result { diff --git a/src/common/eci_encoder_set.rs b/src/common/eci_encoder_set.rs index fb02f3c..0b4d9a9 100644 --- a/src/common/eci_encoder_set.rs +++ b/src/common/eci_encoder_set.rs @@ -14,31 +14,17 @@ * limitations under the License. */ -// package com.google.zxing.common; - -// import java.nio.charset.Charset; -// import java.nio.charset.CharsetEncoder; -// import java.nio.charset.StandardCharsets; -// import java.nio.charset.UnsupportedCharsetException; -// import java.util.ArrayList; -// import java.util.List; - -use encoding::{Encoding, EncodingRef}; use unicode_segmentation::UnicodeSegmentation; use super::CharacterSetECI; use once_cell::sync::Lazy; -static ENCODERS: Lazy> = Lazy::new(|| { +static ENCODERS: Lazy> = Lazy::new(|| { let mut enc_vec = Vec::new(); for name in NAMES { if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(name) { - // try { - enc_vec.push(CharacterSetECI::getCharset(&enc)); - // } catch (UnsupportedCharsetException e) { - // continue - // } + enc_vec.push(enc); } } enc_vec @@ -83,7 +69,7 @@ const NAMES: [&str; 20] = [ */ #[derive(Clone)] pub struct ECIEncoderSet { - encoders: Vec, + encoders: Vec, priorityEncoderIndex: Option, } @@ -98,22 +84,23 @@ impl ECIEncoderSet { */ pub fn new( stringToEncodeMain: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option<&str>, ) -> Self { // List of encoders that potentially encode characters not in ISO-8859-1 in one byte. - let mut encoders: Vec; + let mut encoders: Vec; let mut priorityEncoderIndexValue = None; - let mut neededEncoders: Vec = Vec::new(); + let mut neededEncoders: Vec = Vec::new(); let stringToEncode = stringToEncodeMain.graphemes(true).collect::>(); //we always need the ISO-8859-1 encoder. It is the default encoding - neededEncoders.push(encoding::all::ISO_8859_1); + neededEncoders.push(CharacterSetECI::ISO8859_1); let mut needUnicodeEncoder = if let Some(pc) = priorityCharset { - pc.name().starts_with("UTF") || pc.name().starts_with("utf") + //pc.name().starts_with("UTF") || pc.name().starts_with("utf") + pc == CharacterSetECI::UTF8 || pc == CharacterSetECI::UnicodeBigUnmarked } else { false }; @@ -126,7 +113,7 @@ impl ECIEncoderSet { // for (CharsetEncoder encoder : neededEncoders) { let c = stringToEncode.get(i).unwrap(); if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) - || encoder.encode(c, encoding::EncoderTrap::Strict).is_ok() + || encoder.encode(c).is_ok() { canEncode = true; break; @@ -140,8 +127,7 @@ impl ECIEncoderSet { // for (CharsetEncoder encoder : ENCODERS) { if encoder .encode( - stringToEncode.get(i).unwrap(), - encoding::EncoderTrap::Strict, + stringToEncode.get(i).unwrap() ) .is_ok() { @@ -163,7 +149,7 @@ impl ECIEncoderSet { if neededEncoders.len() == 1 && !needUnicodeEncoder { //the entire input can be encoded by the ISO-8859-1 encoder - encoders = vec![encoding::all::ISO_8859_1]; + encoders = vec![CharacterSetECI::ISO8859_1]; } else { // we need more than one single byte encoder or we need a Unicode encoder. // In this case we append a UTF-8 and UTF-16 encoder to the list @@ -177,8 +163,8 @@ impl ECIEncoderSet { encoders.push(encoder); } - encoders.push(encoding::all::UTF_8); - encoders.push(encoding::all::UTF_16BE); + encoders.push(CharacterSetECI::UTF8); + encoders.push(CharacterSetECI::UnicodeBigUnmarked); } //Compute priorityEncoderIndex by looking up priorityCharset in encoders @@ -187,7 +173,8 @@ impl ECIEncoderSet { // for i in 0..encoders.len() { for (i, encoder) in encoders.iter().enumerate() { // for (int i = 0; i < encoders.length; i++) { - if priorityCharset.as_ref().unwrap().name() == encoder.name() { + // if priorityCharset.as_ref().unwrap().name() == encoder.name() { + if priorityCharset.as_ref().unwrap() == encoder { priorityEncoderIndexValue = Some(i); break; } @@ -195,7 +182,7 @@ impl ECIEncoderSet { } // } //invariants - assert_eq!(encoders[0].name(), encoding::all::ISO_8859_1.name()); + assert_eq!(encoders[0], CharacterSetECI::ISO8859_1); Self { encoders, priorityEncoderIndex: priorityEncoderIndexValue, @@ -212,13 +199,13 @@ impl ECIEncoderSet { pub fn getCharsetName(&self, index: usize) -> Option<&'static str> { if index < self.len() { - Some(self.encoders[index].name()) + Some(self.encoders[index].getCharsetName()) } else { None } } - pub fn getCharset(&self, index: usize) -> Option { + pub fn getCharset(&self, index: usize) -> Option { if index < self.len() { Some(self.encoders[index]) } else { @@ -227,9 +214,10 @@ impl ECIEncoderSet { } pub fn getECIValue(&self, encoderIndex: usize) -> u32 { - CharacterSetECI::getValue( - &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), - ) + self.encoders[encoderIndex].getValue() + // CharacterSetECI::getValue( + // &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), + // ) } /* @@ -242,7 +230,7 @@ impl ECIEncoderSet { pub fn canEncode(&self, c: &str, encoderIndex: usize) -> Option { if encoderIndex < self.len() { let encoder = self.encoders[encoderIndex]; - let enc_data = encoder.encode(c, encoding::EncoderTrap::Strict); + let enc_data = encoder.encode(c); Some(enc_data.is_ok()) } else { @@ -253,7 +241,7 @@ impl ECIEncoderSet { pub fn encode_char(&self, c: &str, encoderIndex: usize) -> Option> { if encoderIndex < self.len() { let encoder = self.encoders[encoderIndex]; - let enc_data = encoder.encode(c, encoding::EncoderTrap::Strict); + let enc_data = encoder.encode(c); enc_data.ok() // assert!(enc_data.is_ok()); // enc_data.unwrap() @@ -265,7 +253,7 @@ impl ECIEncoderSet { pub fn encode_string(&self, s: &str, encoderIndex: usize) -> Option> { if encoderIndex < self.len() { let encoder = self.encoders[encoderIndex]; - encoder.encode(s, encoding::EncoderTrap::Strict).ok() + encoder.encode(s).ok() } else { None } diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index 7f338a8..7491b96 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -23,8 +23,6 @@ use std::fmt; -use encoding::{Encoding, EncodingRef}; - use crate::common::Result; use super::CharacterSetECI; @@ -37,7 +35,7 @@ use super::CharacterSetECI; pub struct ECIStringBuilder { current_bytes: Vec, result: String, - current_charset: Option, //= StandardCharsets.ISO_8859_1; + current_charset: Option, //= StandardCharsets.ISO_8859_1; } impl ECIStringBuilder { @@ -45,14 +43,14 @@ impl ECIStringBuilder { Self { current_bytes: Vec::new(), result: String::new(), - current_charset: Some(encoding::all::ISO_8859_1), + current_charset: Some(CharacterSetECI::ISO8859_1), } } pub fn with_capacity(initial_capacity: usize) -> Self { Self { current_bytes: Vec::with_capacity(initial_capacity), result: String::with_capacity(initial_capacity), - current_charset: Some(encoding::all::ISO_8859_1), + current_charset: Some(CharacterSetECI::ISO8859_1), } } @@ -106,16 +104,18 @@ impl ECIStringBuilder { pub fn appendECI(&mut self, value: u32) -> Result<()> { self.encodeCurrentBytesIfAny(); - if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { - // dbg!( - // character_set_eci, - // CharacterSetECI::getCharset(&character_set_eci).name(), - // CharacterSetECI::getCharset(&character_set_eci).whatwg_name() - // ); - self.current_charset = Some(CharacterSetECI::getCharset(&character_set_eci)); - } else { - self.current_charset = None - } + self.current_charset = CharacterSetECI::getCharacterSetECIByValue(value).ok(); + + // if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { + // // dbg!( + // // character_set_eci, + // // CharacterSetECI::getCharset(&character_set_eci).name(), + // // CharacterSetECI::getCharset(&character_set_eci).whatwg_name() + // // ); + // self.current_charset = Some(character_set_eci); + // } else { + // self.current_charset = None + // } // self.current_charset = CharacterSetECI::getCharset(&character_set_eci); Ok(()) @@ -126,7 +126,7 @@ impl ECIStringBuilder { /// This function can panic pub fn encodeCurrentBytesIfAny(&mut self) { if let Some(encoder) = self.current_charset { - if encoder.name() == encoding::all::UTF_8.name() { + if encoder == CharacterSetECI::UTF8 { if !self.current_bytes.is_empty() { self.result.push_str( &String::from_utf8(std::mem::take(&mut self.current_bytes)).unwrap(), @@ -137,7 +137,7 @@ impl ECIStringBuilder { let bytes = std::mem::take(&mut self.current_bytes); self.current_bytes.clear(); let encoded_value = encoder - .decode(&bytes, encoding::DecoderTrap::Strict) + .decode(&bytes) .unwrap(); self.result.push_str(&encoded_value); } diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index b6b9cc1..51e586d 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -16,13 +16,12 @@ use std::{fmt, rc::Rc}; -use encoding::EncodingRef; use unicode_segmentation::UnicodeSegmentation; use crate::common::Result; use crate::Exceptions; -use super::{ECIEncoderSet, ECIInput}; +use super::{ECIEncoderSet, ECIInput, CharacterSetECI}; //* approximated (latch + 2 codewords) pub const COST_PER_ECI: usize = 3; @@ -194,7 +193,7 @@ impl MinimalECIInput { */ pub fn new( stringToEncodeInput: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option<&str>, ) -> Self { let stringToEncode = stringToEncodeInput.graphemes(true).collect::>(); diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index b472916..f601c50 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -14,12 +14,12 @@ * limitations under the License. */ -use encoding::{Encoding, EncodingRef}; - use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary}; use once_cell::sync::Lazy; +use super::CharacterSetECI; + /** * Common string-related functions. * @@ -50,8 +50,8 @@ const ASSUME_SHIFT_JIS: bool = false; // static SHIFT_JIS: &'static str = "SJIS"; // static GB2312: &'static str = "GB2312"; -pub static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); +pub static SHIFT_JIS_CHARSET: CharacterSetECI = + CharacterSetECI::SJIS; // private static final boolean ASSUME_SHIFT_JIS = // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || @@ -67,14 +67,14 @@ impl StringUtils { */ pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> { let c = StringUtils::guessCharset(bytes, hints)?; - if c.name() == encoding::label::encoding_from_whatwg_label("SJIS")?.name() { + if c == CharacterSetECI::SJIS { Some("SJIS") - } else if c.name() == encoding::all::UTF_8.name() { + } else if c == CharacterSetECI::UTF8 { Some("UTF8") - } else if c.name() == encoding::all::ISO_8859_1.name() { + } else if c == CharacterSetECI::ISO8859_1 { Some("ISO8859_1") } else { - Some(c.name()) + Some(&c.getCharsetName()) } } @@ -87,12 +87,12 @@ impl StringUtils { * or the platform default encoding if * none of these can possibly be correct */ - pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option { + pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option { if let Some(DecodeHintValue::CharacterSet(cs_name)) = hints.get(&DecodeHintType::CHARACTER_SET) { // if let DecodeHintValue::CharacterSet(cs_name) = hint { - return encoding::label::encoding_from_whatwg_label(cs_name); + return CharacterSetECI::getCharacterSetECIByName(cs_name) // } } // if hints.contains_key(&DecodeHintType::CHARACTER_SET) { @@ -104,9 +104,9 @@ impl StringUtils { && ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE)) { if bytes[0] == 0xFE && bytes[1] == 0xFF { - return Some(encoding::all::UTF_16BE); + return Some(CharacterSetECI::UnicodeBigUnmarked); } else { - return Some(encoding::all::UTF_16LE); + return Some(CharacterSetECI::UTF16LE); } } @@ -224,7 +224,7 @@ impl StringUtils { // Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) { - return Some(encoding::all::UTF_8); + return Some(CharacterSetECI::UTF8); } // Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done if can_be_shift_jis @@ -232,7 +232,7 @@ impl StringUtils { || sjis_max_katakana_word_length >= 3 || sjis_max_double_bytes_word_length >= 3) { - return encoding::label::encoding_from_whatwg_label("SJIS"); + return Some(CharacterSetECI::SJIS); //encoding::label::encoding_from_whatwg_label("SJIS"); } // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is: // - If we saw @@ -243,23 +243,23 @@ impl StringUtils { return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2) || iso_high_other * 10 >= length { - encoding::label::encoding_from_whatwg_label("SJIS") + Some(CharacterSetECI::SJIS) } else { - Some(encoding::all::ISO_8859_1) + Some(CharacterSetECI::ISO8859_1) }; } // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding if can_be_iso88591 { - return Some(encoding::all::ISO_8859_1); + return Some(CharacterSetECI::ISO8859_1); } if can_be_shift_jis { - return Some(encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); + return Some(CharacterSetECI::SJIS); } if can_be_utf8 { - return Some(encoding::all::UTF_8); + return Some(CharacterSetECI::UTF8); } // Otherwise, we take a wild guess with platform encoding - Some(encoding::all::UTF_8) + Some(CharacterSetECI::UTF8) } } diff --git a/src/datamatrix/data_matrix_writer.rs b/src/datamatrix/data_matrix_writer.rs index cafffb9..4409fb4 100644 --- a/src/datamatrix/data_matrix_writer.rs +++ b/src/datamatrix/data_matrix_writer.rs @@ -17,10 +17,8 @@ use std::collections::HashMap; -use encoding::EncodingRef; - use crate::{ - common::{BitMatrix, Result}, + common::{BitMatrix, Result, CharacterSetECI}, qrcode::encoder::ByteMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -117,14 +115,14 @@ impl Writer for DataMatrixWriter { false }; - let mut charset: Option = None; + let mut charset: Option = None; let hasEncodingHint = hints.contains_key(&EncodeHintType::CHARACTER_SET); if hasEncodingHint { let Some(EncodeHintValue::CharacterSet(char_set_name)) = hints.get(&EncodeHintType::CHARACTER_SET) else { return Err(Exceptions::illegal_argument_with("charset does not exist")) }; - charset = encoding::label::encoding_from_whatwg_label(char_set_name); + charset = CharacterSetECI::getCharacterSetECIByName(char_set_name);//encoding::label::encoding_from_whatwg_label(char_set_name); // charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); } encoded = minimal_encoder::encodeHighLevelWithDetails( diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index fbde09d..9048601 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -14,10 +14,8 @@ * limitations under the License. */ -use encoding::Encoding; - use crate::{ - common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result}, + common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result, CharacterSetECI}, Exceptions, }; @@ -730,9 +728,10 @@ fn decodeBase256Segment( codewordPosition += 1; } result.append_string( - &encoding::all::ISO_8859_1 - .decode(&bytes, encoding::DecoderTrap::Strict) - .map_err(Exceptions::parse_with)?, + + &CharacterSetECI::ISO8859_1 + .decode(&bytes) + ?, ); byteSegments.push(bytes); diff --git a/src/datamatrix/encoder/encoder_context.rs b/src/datamatrix/encoder/encoder_context.rs index 51fd9a3..121ec92 100644 --- a/src/datamatrix/encoder/encoder_context.rs +++ b/src/datamatrix/encoder/encoder_context.rs @@ -16,13 +16,12 @@ use std::rc::Rc; -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use crate::{Dimension, Exceptions}; use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint}; -use encoding::{self, EncodingRef}; -const ISO_8859_1_ENCODER: EncodingRef = encoding::all::ISO_8859_1; +const ISO_8859_1_ENCODER: CharacterSetECI = CharacterSetECI::ISO8859_1; pub struct EncoderContext<'a> { symbol_lookup: Rc>, @@ -59,10 +58,10 @@ impl<'a> EncoderContext<'_> { // sb.append(ch); // } let sb = if let Ok(encoded_bytes) = - ISO_8859_1_ENCODER.encode(msg, encoding::EncoderTrap::Strict) + ISO_8859_1_ENCODER.encode(msg) { ISO_8859_1_ENCODER - .decode(&encoded_bytes, encoding::DecoderTrap::Strict) + .decode(&encoded_bytes) .map_err(|e| { Exceptions::parse_with(format!("round trip decode should always work: {e}")) })? diff --git a/src/datamatrix/encoder/high_level_encode_test_case.rs b/src/datamatrix/encoder/high_level_encode_test_case.rs index f875b7c..f8c1244 100644 --- a/src/datamatrix/encoder/high_level_encode_test_case.rs +++ b/src/datamatrix/encoder/high_level_encode_test_case.rs @@ -18,7 +18,7 @@ use std::rc::Rc; use once_cell::sync::Lazy; -use crate::datamatrix::encoder::{SymbolInfo, SymbolShapeHint}; +use crate::{datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, common::CharacterSetECI}; use super::{high_level_encoder, minimal_encoder, symbol_info, SymbolInfoLookup}; @@ -534,7 +534,7 @@ fn testECIs() { assert_eq!("239 209 151 206 214 92 122 140 35 158 144 162 52 205 55 171 137 23 67 206 218 175 147 113 15 254 116 33 241 25 231 186 14 212 64 253 151 252 159 33 41 241 27 231 83 171 53 209 35 25 134 6 42 33 35 239 184 31 193 234 7 252 205 101 127 241 209 34 24 5 22 23 221 148 179 239 128 140 92 187 106 204 198 59 19 25 114 248 118 36 254 231 106 196 19 239 101 27 107 69 189 112 236 156 252 16 174 125 24 10 125 116 42 129", visualized); - let visualized = visualize(&minimal_encoder::encodeHighLevelWithDetails("that particularly stands out to me is \u{0625}\u{0650}\u{062C}\u{064E}\u{0651}\u{0627}\u{0635} (\u{02BE}\u{0101}\u{1E63}) \"pear\", suggested to have originated from Hebrew \u{05D0}\u{05B7}\u{05D2}\u{05B8}\u{05BC}\u{05E1} (ag\u{00E1}s)", Some(encoding::all::UTF_8), None , SymbolShapeHint::FORCE_NONE).expect("encode")); + let visualized = visualize(&minimal_encoder::encodeHighLevelWithDetails("that particularly stands out to me is \u{0625}\u{0650}\u{062C}\u{064E}\u{0651}\u{0627}\u{0635} (\u{02BE}\u{0101}\u{1E63}) \"pear\", suggested to have originated from Hebrew \u{05D0}\u{05B7}\u{05D2}\u{05B8}\u{05BC}\u{05E1} (ag\u{00E1}s)", Some(CharacterSetECI::UTF8), None , SymbolShapeHint::FORCE_NONE).expect("encode")); assert_eq!("241 27 239 209 151 206 214 92 122 140 35 158 144 162 52 205 55 171 137 23 67 206 218 175 147 113 15 254 116 33 231 202 33 131 77 154 119 225 163 238 206 28 249 93 36 150 151 53 108 246 145 228 217 71 199 42 33 35 239 184 31 193 234 7 252 205 101 127 241 209 34 24 5 22 23 221 148 179 239 128 140 92 187 106 204 198 59 19 25 114 248 118 36 254 231 43 133 212 175 38 220 44 6 125 49 172 93 189 209 111 61 217 203 62 116 42 129 1 151 46 196 91 241 137 32 182 77 227 122 18 168 63 213 108 4 154 49 199 94 244 140 35 185 80", visualized); } diff --git a/src/datamatrix/encoder/high_level_encoder.rs b/src/datamatrix/encoder/high_level_encoder.rs index ace5b68..5606d7e 100644 --- a/src/datamatrix/encoder/high_level_encoder.rs +++ b/src/datamatrix/encoder/high_level_encoder.rs @@ -16,9 +16,7 @@ use std::rc::Rc; -use encoding::{self, EncodingRef}; - -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use crate::{Dimension, Exceptions}; use super::{ @@ -26,7 +24,7 @@ use super::{ SymbolInfoLookup, SymbolShapeHint, TextEncoder, X12Encoder, }; #[allow(dead_code)] -const DEFAULT_ENCODING: EncodingRef = encoding::all::ISO_8859_1; +const DEFAULT_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; /** * DataMatrix ECC 200 data encoder following the algorithm described in ISO/IEC 16022:200(E) in diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index dc85258..1d5dd06 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -16,16 +16,14 @@ use std::{fmt, rc::Rc}; -use encoding::{self, EncodingRef}; - use crate::{ - common::{ECIInput, MinimalECIInput, Result}, + common::{ECIInput, MinimalECIInput, Result, CharacterSetECI}, Exceptions, }; use super::{high_level_encoder, SymbolShapeHint}; -const ISO_8859_1_ENCODER: EncodingRef = encoding::all::ISO_8859_1; +const ISO_8859_1_ENCODER: CharacterSetECI = CharacterSetECI::ISO8859_1; /** * Encoder that encodes minimally @@ -153,7 +151,7 @@ pub fn encodeHighLevel(msg: &str) -> Result { */ pub fn encodeHighLevelWithDetails( msg: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, ) -> Result { @@ -174,8 +172,8 @@ pub fn encodeHighLevelWithDetails( } Ok(ISO_8859_1_ENCODER .decode( - &encode(msg, priorityCharset, fnc1, shape, macroId)?, - encoding::DecoderTrap::Strict, + &encode(msg, priorityCharset, fnc1, shape, macroId)? + ) .expect("should decode")) // return new String(encode(msg, priorityCharset, fnc1, shape, macroId), StandardCharsets.ISO_8859_1); @@ -197,7 +195,7 @@ pub fn encodeHighLevelWithDetails( */ fn encode( input: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, macroId: i32, @@ -1398,7 +1396,7 @@ struct Input { impl Input { pub fn new( stringToEncode: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, macroId: i32, diff --git a/src/pdf417/decoder/pdf_417_decoder_test_case.rs b/src/pdf417/decoder/pdf_417_decoder_test_case.rs index add6d90..58ab3c9 100644 --- a/src/pdf417/decoder/pdf_417_decoder_test_case.rs +++ b/src/pdf417/decoder/pdf_417_decoder_test_case.rs @@ -15,9 +15,9 @@ * limitations under the License. */ -use encoding::{Encoding, EncodingRef}; use java_rand; +use crate::common::CharacterSetECI; use crate::pdf417::decoder::decoded_bit_stream_parser; use crate::pdf417::encoder::{pdf_417_high_level_encoder_test_adapter, Compaction}; use crate::pdf417::PDF417RXingResultMetadata; @@ -307,8 +307,8 @@ fn testBinaryData() { random.next_bytes(&mut bytes); total += encodeDecode( - &encoding::all::ISO_8859_1 - .decode(&bytes, encoding::DecoderTrap::Strict) + &CharacterSetECI::ISO8859_1 + .decode(&bytes) .expect("decode bytes"), ); } @@ -437,7 +437,7 @@ fn encodeDecode(input: &str) -> u32 { fn encodeDecodeWithAll( input: &str, - charset: Option, + charset: Option, autoECI: bool, decode: bool, ) -> u32 { @@ -523,7 +523,7 @@ fn performECITest( // for (int i = 0; i < 1000; i++) { let s = generateText(&mut random, 100, chars, weights); minLength += encodeDecodeWithAll(&s, None, true, true); - utfLength += encodeDecodeWithAll(&s, Some(encoding::all::UTF_8), false, true); + utfLength += encodeDecodeWithAll(&s, Some(CharacterSetECI::UTF8), false, true); } assert_eq!(expectedMinLength, minLength); assert_eq!(expectedUTFLength, utfLength); diff --git a/src/pdf417/encoder/pdf_417.rs b/src/pdf417/encoder/pdf_417.rs index 46b7a90..3ee462b 100644 --- a/src/pdf417/encoder/pdf_417.rs +++ b/src/pdf417/encoder/pdf_417.rs @@ -18,9 +18,7 @@ * This file has been modified from its original form in Barcode4J. */ -use encoding::EncodingRef; - -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use crate::Exceptions; use super::{ @@ -34,7 +32,7 @@ pub struct PDF417 { barcodeMatrix: Option, compact: bool, compaction: Compaction, - encoding: Option, + encoding: Option, minCols: u32, maxCols: u32, maxRows: u32, @@ -347,7 +345,7 @@ impl PDF417 { /** * @param encoding sets character encoding to use */ - pub fn setEncoding(&mut self, encoding: Option) { + pub fn setEncoding(&mut self, encoding: Option) { self.encoding = encoding; } } diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index e865347..7aef5f5 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -20,8 +20,6 @@ use std::{any::TypeId, fmt::Display, str::FromStr}; -use encoding::EncodingRef; - use crate::{ common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, Exceptions, @@ -125,7 +123,7 @@ const TEXT_PUNCTUATION_RAW: [u8; 30] = [ 40, 41, 63, 123, 125, 39, 0, ]; -const DEFAULT_ENCODING: EncodingRef = encoding::all::ISO_8859_1; //StandardCharsets.ISO_8859_1; +const DEFAULT_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1; const MIXED: [i8; 128] = { let mut mixed = [-1_i8; 128]; @@ -174,7 +172,7 @@ const PUNCTUATION: [i8; 128] = { pub fn encodeHighLevel( msg: &str, compaction: Compaction, - encoding: Option, + encoding: Option, autoECI: bool, ) -> Result { let mut encoding = encoding; @@ -199,14 +197,16 @@ pub fn encodeHighLevel( input = Box::new(NoECIInput::new(msg.to_owned())); if encoding.is_none() { encoding = Some(DEFAULT_ENCODING); - } else if DEFAULT_ENCODING.name() - != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)?.name() + } else if &DEFAULT_ENCODING + != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)? { - if let Some(eci) = - CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) - { - encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; - } + // if let Some(eci) = + // CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) + // { + // encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; + // } + + encodingECI(CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, &mut sb)?; } } @@ -230,7 +230,7 @@ pub fn encodeHighLevel( let msgBytes = encoding .as_ref() .ok_or(Exceptions::ILLEGAL_STATE)? - .encode(&input.to_string(), encoding::EncoderTrap::Strict) + .encode(&input.to_string()) .unwrap_or_default(); //input.to_string().getBytes(encoding); encodeBinary( &msgBytes, @@ -290,7 +290,7 @@ pub fn encodeHighLevel( if let Ok(enc_str) = encoding .as_ref() .ok_or(Exceptions::ILLEGAL_STATE)? - .encode(&str, encoding::EncoderTrap::Strict) + .encode(&str) { Some(enc_str) } else { @@ -757,7 +757,7 @@ fn determineConsecutiveTextCount(input: &T, startpos: u32) fn determineConsecutiveBinaryCount( input: &T, startpos: u32, - encoding: Option, + encoding: Option, ) -> Result { let len = input.length(); let mut idx = startpos as usize; @@ -780,8 +780,8 @@ fn determineConsecutiveBinaryCount( if let Some(encoder) = encoding { let can_encode = encoder .encode( - &input.charAt(idx)?.to_string(), - encoding::EncoderTrap::Strict, + &input.charAt(idx)?.to_string() + ) .is_ok(); @@ -866,11 +866,11 @@ impl Display for NoECIInput { */ #[cfg(test)] mod PDF417EncoderTestCase { - use crate::pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}; + use crate::{pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, common::CharacterSetECI}; #[test] fn testEncodeAuto() { - let encoded = encodeHighLevel("ABCD", Compaction::AUTO, Some(encoding::all::UTF_8), false) + let encoded = encodeHighLevel("ABCD", Compaction::AUTO, Some(CharacterSetECI::UTF8), false) .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0385}ABCD", encoded); } @@ -881,7 +881,7 @@ mod PDF417EncoderTestCase { encodeHighLevel( "1%§s ?aG$", Compaction::AUTO, - Some(encoding::all::UTF_8), + Some(CharacterSetECI::UTF8), false, ) .expect("encode"); @@ -893,7 +893,7 @@ mod PDF417EncoderTestCase { encodeHighLevel( "asdfg§asd", Compaction::AUTO, - Some(encoding::all::ISO_8859_1), + Some(CharacterSetECI::ISO8859_1), false, ) .expect("encode"); @@ -901,7 +901,7 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeText() { - let encoded = encodeHighLevel("ABCD", Compaction::TEXT, Some(encoding::all::UTF_8), false) + let encoded = encodeHighLevel("ABCD", Compaction::TEXT, Some(CharacterSetECI::UTF8), false) .expect("encode"); assert_eq!("Ο\u{001A}\u{0001}?", encoded); } @@ -911,7 +911,7 @@ mod PDF417EncoderTestCase { let encoded = encodeHighLevel( "1234", Compaction::NUMERIC, - Some(encoding::all::UTF_8), + Some(CharacterSetECI::UTF8), false, ) .expect("encode"); @@ -921,7 +921,7 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeByte() { - let encoded = encodeHighLevel("abcd", Compaction::BYTE, Some(encoding::all::UTF_8), false) + let encoded = encodeHighLevel("abcd", Compaction::BYTE, Some(CharacterSetECI::UTF8), false) .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0385}abcd", encoded); } diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs index 3382821..ee37383 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs @@ -14,9 +14,7 @@ * limitations under the License. */ -use encoding::EncodingRef; - -use crate::common::Result; +use crate::common::{Result, CharacterSetECI}; use super::{pdf_417_high_level_encoder, Compaction}; @@ -27,7 +25,7 @@ use super::{pdf_417_high_level_encoder, Compaction}; pub fn encodeHighLevel( msg: &str, compaction: Compaction, - encoding: Option, + encoding: Option, autoECI: bool, ) -> Result { pdf_417_high_level_encoder::encodeHighLevel(msg, compaction, encoding, autoECI) diff --git a/src/pdf417/pdf_417_writer.rs b/src/pdf417/pdf_417_writer.rs index 10c46db..9f81929 100644 --- a/src/pdf417/pdf_417_writer.rs +++ b/src/pdf417/pdf_417_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result}, + common::{BitMatrix, Result, CharacterSetECI}, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -108,7 +108,7 @@ impl Writer for PDF417Writer { if let Some(EncodeHintValue::CharacterSet(cs)) = hints.get(&EncodeHintType::CHARACTER_SET) { - encoder.setEncoding(encoding::label::encoding_from_whatwg_label(cs)); + encoder.setEncoding(CharacterSetECI::getCharacterSetECIByName(cs)); } if let Some(EncodeHintValue::Pdf417AutoEci(auto_eci_str)) = hints.get(&EncodeHintType::PDF417_AUTO_ECI) diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index bf97309..f84dbd5 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -204,9 +204,9 @@ fn decodeHanziSegment(bits: &mut BitSource, result: &mut String, count: usize) - } let gb_encoder = - encoding::label::encoding_from_whatwg_label("GBK").ok_or(Exceptions::ILLEGAL_STATE)?; + CharacterSetECI::GB18030; let encode_string = gb_encoder - .decode(&buffer, encoding::DecoderTrap::Strict) + .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; result.push_str(&encode_string); Ok(()) @@ -250,7 +250,7 @@ fn decodeKanjiSegment( let encoder = { let _ = currentCharacterSetECI; let _ = hints; - encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::FORMAT)? + CharacterSetECI::SJIS }; #[cfg(feature = "allow_forced_iso_ied_18004_compliance")] @@ -258,16 +258,16 @@ fn decodeKanjiSegment( hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) { if let Some(ccse) = ¤tCharacterSetECI { - CharacterSetECI::getCharset(ccse) + CharacterSetECI::getCharacterSetECIByName(ccse) } else { - encoding::all::ISO_8859_1 + CharacterSetECI::ISO8859_1 } } else { - encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::FORMAT)? + CharacterSetECI::SJIS }; let encode_string = encoder - .decode(&buffer, encoding::DecoderTrap::Strict) + .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; result.push_str(&encode_string); @@ -308,37 +308,26 @@ fn decodeByteSegment( if let Some(DecodeHintValue::QrAssumeSpecConformInput(true)) = hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) { - encoding::all::ISO_8859_1 + CharacterSetECI::ISO8859_1 } else { StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::ILLEGAL_STATE)? } } else { - CharacterSetECI::getCharset( - currentCharacterSetECI - .as_ref() - .ok_or(Exceptions::ILLEGAL_STATE)?, - ) + currentCharacterSetECI.ok_or(Exceptions::ILLEGAL_STATE)? + // CharacterSetECI::getCharset( + // currentCharacterSetECI + // .as_ref() + // .ok_or(Exceptions::ILLEGAL_STATE)?, + // ) }; - let encode_string = if currentCharacterSetECI.is_some() - && currentCharacterSetECI - .as_ref() - .ok_or(Exceptions::ILLEGAL_STATE)? - == &CharacterSetECI::Cp437 - { - { - use codepage_437::BorrowFromCp437; - use codepage_437::CP437_CONTROL; - - String::borrow_from_cp437(&readBytes, &CP437_CONTROL) - } - } else { + let encode_string = encoding - .decode(&readBytes, encoding::DecoderTrap::Strict) + .decode(&readBytes) .map_err(|e| { Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) })? - }; + ; result.push_str(&encode_string); byteSegments.push(readBytes); diff --git a/src/qrcode/encoder/EncoderTestCase.rs b/src/qrcode/encoder/EncoderTestCase.rs index ba728a4..13a754e 100644 --- a/src/qrcode/encoder/EncoderTestCase.rs +++ b/src/qrcode/encoder/EncoderTestCase.rs @@ -17,20 +17,19 @@ use std::collections::HashMap; use crate::{ - common::BitArray, + common::{BitArray, CharacterSetECI}, qrcode::{ decoder::{ErrorCorrectionLevel, Mode, Version}, encoder::{qrcode_encoder, MinimalEncoder}, }, EncodeHintType, EncodeHintValue, }; -use encoding::EncodingRef; use once_cell::sync::Lazy; use super::QRCode; -static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); +static SHIFT_JIS_CHARSET: Lazy = + Lazy::new(|| CharacterSetECI::SJIS); /** * @author satorux@google.com (Satoru Takabayashi) - creator @@ -1075,10 +1074,9 @@ fn testMinimalEncoder41() { #[test] fn testMinimalEncoder42() { // test halfwidth Katakana character (they are single byte encoded in Shift_JIS) - // NOTE: Changed to windows-31j because that is what is supported in encoding crate verifyMinimalEncoding( "Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}", - "ECI(windows-31j),BYTE(Katakana:......)", + "ECI(shift_jis),BYTE(Katakana:......)", None, false, ); @@ -1100,10 +1098,9 @@ fn testMinimalEncoder44() { // The character \u30A2 encodes as double byte in Shift_JIS but KANJI is not more compact in this case because // KANJI is only more compact when it encodes pairs of characters. In the case of mixed text it can however be // that Shift_JIS encoding is more compact as in this example - // NOTE: Changed to windows-31j because that is what is supported in encoding crate verifyMinimalEncoding( "Katakana:\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}", - "ECI(windows-31j),BYTE(Katakana:.a.a.a.a.a.)", + "ECI(shift_jis),BYTE(Katakana:.a.a.a.a.a.)", None, false, ); @@ -1112,7 +1109,7 @@ fn testMinimalEncoder44() { fn verifyMinimalEncoding( input: &str, expectedRXingResult: &str, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ) { let result = MinimalEncoder::encode_with_details( @@ -1198,7 +1195,7 @@ fn verifyNotGS1EncodedData(qrCode: &QRCode) { fn shiftJISString(bytes: &[u8]) -> String { SHIFT_JIS_CHARSET - .decode(bytes, encoding::DecoderTrap::Strict) + .decode(bytes) .expect("decode should be ok") // return new String(bytes, StringUtils.SHIFT_JIS_CHARSET); } diff --git a/src/qrcode/encoder/minimal_encoder.rs b/src/qrcode/encoder/minimal_encoder.rs index 24e14b4..1c03bdf 100644 --- a/src/qrcode/encoder/minimal_encoder.rs +++ b/src/qrcode/encoder/minimal_encoder.rs @@ -16,10 +16,8 @@ use std::{fmt, rc::Rc}; -use encoding::EncodingRef; - use crate::{ - common::{BitArray, ECIEncoderSet, Result}, + common::{BitArray, ECIEncoderSet, Result, CharacterSetECI}, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, Exceptions, }; @@ -107,7 +105,7 @@ impl MinimalEncoder { */ pub fn new( stringToEncode: &str, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ecLevel: ErrorCorrectionLevel, ) -> Self { @@ -142,7 +140,7 @@ impl MinimalEncoder { pub fn encode_with_details( stringToEncode: &str, version: Option, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ecLevel: ErrorCorrectionLevel, ) -> Result { @@ -1044,10 +1042,10 @@ impl fmt::Display for RXingResultNode { result.push('('); if self.mode == Mode::ECI { result.push_str( - self.encoders + &self.encoders .getCharset(self.charsetEncoderIndex) .ok_or(fmt::Error)? - .name(), + .getCharsetName(), ); } else { let sub_string: String = self diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index cbe5257..abfdaad 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -19,9 +19,6 @@ */ use std::collections::HashMap; -use encoding::EncodingRef; - -use once_cell::sync::Lazy; use unicode_segmentation::UnicodeSegmentation; use crate::{ @@ -35,8 +32,8 @@ use crate::{ use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; -static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); +static SHIFT_JIS_CHARSET: CharacterSetECI = + CharacterSetECI::SJIS; // The original table is defined in the table 5 of JISX0510:2004 (p.19). const ALPHANUMERIC_TABLE: [i8; 96] = [ @@ -48,7 +45,7 @@ const ALPHANUMERIC_TABLE: [i8; 96] = [ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f ]; -pub const DEFAULT_BYTE_MODE_ENCODING: EncodingRef = encoding::all::ISO_8859_1; +pub const DEFAULT_BYTE_MODE_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; // The mask penalty calculation is complicated. See Table 21 of JISX0510:2004 (p.45) for details. // Basically it applies four rules and summate all penalties. @@ -101,7 +98,7 @@ pub fn encode_with_hints( if has_encoding_hint { if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) { encoding = - Some(encoding::label::encoding_from_whatwg_label(v).ok_or(Exceptions::WRITER)?) + Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) } } @@ -126,12 +123,12 @@ pub fn encode_with_hints( let encoding = if let Some(encoding) = encoding { encoding } else if let Ok(_encs) = - DEFAULT_BYTE_MODE_ENCODING.encode(content, encoding::EncoderTrap::Strict) + DEFAULT_BYTE_MODE_ENCODING.encode(content) { DEFAULT_BYTE_MODE_ENCODING } else { has_encoding_hint = true; - encoding::all::UTF_8 + CharacterSetECI::UTF8 }; // Pick an encoding mode appropriate for the content. Note that this will not attempt to use @@ -144,9 +141,7 @@ pub fn encode_with_hints( // Append ECI segment if applicable if mode == Mode::BYTE && has_encoding_hint { - if let Some(eci) = CharacterSetECI::getCharacterSetECI(encoding) { - appendECI(&eci, &mut header_bits)?; - } + appendECI(&encoding, &mut header_bits)?; } // Append the FNC1 mode header for GS1 formatted data if applicable @@ -304,15 +299,15 @@ pub fn getAlphanumericCode(code: u32) -> i8 { } pub fn chooseMode(content: &str) -> Mode { - chooseModeWithEncoding(content, encoding::all::ISO_8859_1) + chooseModeWithEncoding(content, CharacterSetECI::ISO8859_1) } /** * Choose the best mode by examining the content. Note that 'encoding' is used as a hint; * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ -fn chooseModeWithEncoding(content: &str, encoding: EncodingRef) -> Mode { - if SHIFT_JIS_CHARSET.name() == encoding.name() && isOnlyDoubleByteKanji(content) { +fn chooseModeWithEncoding(content: &str, encoding: CharacterSetECI) -> Mode { + if SHIFT_JIS_CHARSET == encoding && isOnlyDoubleByteKanji(content) { // Choose Kanji mode if all input are double-byte characters return Mode::KANJI; } @@ -337,7 +332,7 @@ fn chooseModeWithEncoding(content: &str, encoding: EncodingRef) -> Mode { } pub fn isOnlyDoubleByteKanji(content: &str) -> bool { - let bytes = if let Ok(byt) = SHIFT_JIS_CHARSET.encode(content, encoding::EncoderTrap::Strict) { + let bytes = if let Ok(byt) = SHIFT_JIS_CHARSET.encode(content) { byt } else { return false; @@ -638,7 +633,7 @@ pub fn appendBytes( content: &str, mode: Mode, bits: &mut BitArray, - encoding: EncodingRef, + encoding: CharacterSetECI, ) -> Result<()> { match mode { Mode::NUMERIC => appendNumericBytes(content, bits), @@ -725,9 +720,9 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()> Ok(()) } -pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: EncodingRef) -> Result<()> { +pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: CharacterSetECI) -> Result<()> { let bytes = encoding - .encode(content, encoding::EncoderTrap::Strict) + .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; for b in bytes { bits.appendBits(b as u32, 8)?; @@ -739,7 +734,7 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<()> { let sjis = &SHIFT_JIS_CHARSET; let bytes = sjis - .encode(content, encoding::EncoderTrap::Strict) + .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; if bytes.len() % 2 != 0 { return Err(Exceptions::writer_with("Kanji byte size not even")); diff --git a/tests/common/pdf_417_multiimage_span.rs b/tests/common/pdf_417_multiimage_span.rs index 7578206..a0a00ea 100644 --- a/tests/common/pdf_417_multiimage_span.rs +++ b/tests/common/pdf_417_multiimage_span.rs @@ -23,9 +23,8 @@ use std::{ rc::Rc, }; -use encoding::Encoding; use rxing::{ - common::{HybridBinarizer, Result}, + common::{HybridBinarizer, Result, CharacterSetECI}, multi::MultipleBarcodeReader, pdf417::PDF417RXingResultMetadata, BarcodeFormat, Binarizer, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, @@ -645,9 +644,7 @@ impl PDF417MultiImageSpanAbstractBlackBoxTest if ext == "bin" { let mut buffer: Vec = Vec::new(); File::open(&file)?.read_to_end(&mut buffer)?; - encoding::all::ISO_8859_1 - .decode(&buffer, encoding::DecoderTrap::Replace) - .expect("decode") + CharacterSetECI::ISO8859_1.decode_replace(&buffer).expect("decode") } else { read_to_string(&file).expect("ok") } From 9431031147875368e0eb902ccd8d3ee03bc75301 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 2 Mar 2023 15:54:40 -0600 Subject: [PATCH 06/10] cargo clippy && fmt --- src/aztec/EncoderTest.rs | 19 ++++++--- src/aztec/aztec_writer.rs | 2 +- src/aztec/decoder.rs | 9 ++-- src/aztec/encoder/aztec_encoder.rs | 2 +- src/aztec/encoder/high_level_encoder.rs | 13 +++--- src/aztec/encoder/state.rs | 2 +- src/client/result/VCardResultParser.rs | 6 +-- src/common/StringUtilsTestCase.rs | 13 ++---- src/common/character_set_eci.rs | 41 +++++++++++-------- src/common/eci_encoder_set.rs | 11 +---- src/common/eci_string_builder.rs | 4 +- src/common/minimal_eci_input.rs | 2 +- src/common/string_utils.rs | 11 ++--- src/datamatrix/data_matrix_writer.rs | 5 ++- .../decoder/decoded_bit_stream_parser.rs | 9 +--- src/datamatrix/encoder/encoder_context.rs | 14 +++---- .../encoder/high_level_encode_test_case.rs | 5 ++- src/datamatrix/encoder/high_level_encoder.rs | 2 +- src/datamatrix/encoder/minimal_encoder.rs | 7 +--- src/pdf417/encoder/pdf_417.rs | 2 +- .../encoder/pdf_417_high_level_encoder.rs | 21 +++++----- ...pdf_417_high_level_encoder_test_adapter.rs | 2 +- src/pdf417/pdf_417_writer.rs | 2 +- .../decoder/decoded_bit_stream_parser.rs | 13 ++---- src/qrcode/encoder/EncoderTestCase.rs | 3 +- src/qrcode/encoder/minimal_encoder.rs | 4 +- src/qrcode/encoder/qrcode_encoder.rs | 16 ++++---- tests/common/pdf_417_multiimage_span.rs | 6 ++- 28 files changed, 111 insertions(+), 135 deletions(-) diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index 00a5763..b69f38a 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -23,7 +23,8 @@ use crate::{ encoder::HighLevelEncoder, shared_test_methods::{stripSpace, toBitArray, toBooleanArray}, }, - BarcodeFormat, EncodeHintType, EncodeHintValue, Point, common::CharacterSetECI, + common::CharacterSetECI, + BarcodeFormat, EncodeHintType, EncodeHintValue, Point, }; use super::{encoder::aztec_encoder, AztecWriter}; @@ -137,8 +138,8 @@ X X X X X X X X X X X X X #[test] fn testAztecWriter() { let shift_jis: CharacterSetECI = - CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); - + CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); + 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 @@ -817,7 +818,9 @@ fn testStuffBits(wordSize: usize, bits: &str, expected: &str) { fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::with_charset( - CharacterSetECI::UTF8.encode(s).expect("should encode to bytes"), + CharacterSetECI::UTF8 + .encode(s) + .expect("should encode to bytes"), CharacterSetECI::UTF8, ) .encode() @@ -838,7 +841,9 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { fn testHighLevelEncodeString(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::new( - CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), + CharacterSetECI::ISO8859_1 + .encode(s) + .expect("should encode to bytes"), ) .encode() .expect("high level ok"); @@ -857,7 +862,9 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) { fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) { let bits = HighLevelEncoder::new( - CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"), + CharacterSetECI::ISO8859_1 + .encode(s) + .expect("should encode to bytes"), ) .encode() .expect("high level ok"); diff --git a/src/aztec/aztec_writer.rs b/src/aztec/aztec_writer.rs index 7bfa320..24f183a 100644 --- a/src/aztec/aztec_writer.rs +++ b/src/aztec/aztec_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result, CharacterSetECI}, + common::{BitMatrix, CharacterSetECI, Result}, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer, }; diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index 14863fb..48cf998 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -113,7 +113,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // Intermediary buffer of decoded bytes, which is decoded into a string and flushed // when character encoding changes (ECI) or input ends. let mut decoded_bytes: Vec = Vec::new(); - + let mut encdr: CharacterSetECI = CharacterSetECI::ISO8859_1; let mut index = 0; @@ -159,10 +159,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { let mut n = read_code(corrected_bits, index, 3); index += 3; // flush bytes, FLG changes state - result.push_str( - &encdr - .decode(&decoded_bytes)?, - ); + result.push_str(&encdr.decode(&decoded_bytes)?); decoded_bytes.clear(); match n { @@ -206,7 +203,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 = str.as_bytes(); //let b = str.getBytes(StandardCharsets.US_ASCII); //decodedBytes.write(b, 0, b.length); diff --git a/src/aztec/encoder/aztec_encoder.rs b/src/aztec/encoder/aztec_encoder.rs index 99e9265..cef5c04 100644 --- a/src/aztec/encoder/aztec_encoder.rs +++ b/src/aztec/encoder/aztec_encoder.rs @@ -19,7 +19,7 @@ use crate::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder, }, - BitArray, BitMatrix, Result, CharacterSetECI, + BitArray, BitMatrix, CharacterSetECI, Result, }, exceptions::Exceptions, }; diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index 3fa495a..29bf492 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -14,10 +14,7 @@ * limitations under the License. */ -use crate::{ - common::{BitArray, CharacterSetECI, Result}, - exceptions::Exceptions, -}; +use crate::common::{BitArray, CharacterSetECI, Result}; use super::{State, Token}; @@ -243,10 +240,10 @@ impl HighLevelEncoder { pub fn encode(&self) -> Result { let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0); //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { - if self.charset != CharacterSetECI::ISO8859_1 { - //} && eci != CharacterSetECI::Cp1252 { - initial_state = initial_state.appendFLGn(self.charset.getValue())?; - } + if self.charset != CharacterSetECI::ISO8859_1 { + //} && eci != CharacterSetECI::Cp1252 { + initial_state = initial_state.appendFLGn(self.charset.getValue())?; + } // } else { // return Err(Exceptions::illegal_argument_with( // "No ECI code for character set", diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index 1a20425..73150a7 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -17,7 +17,7 @@ use std::fmt; use crate::{ - common::{BitArray, Result, CharacterSetECI}, + common::{BitArray, CharacterSetECI, Result}, exceptions::Exceptions, }; diff --git a/src/client/result/VCardResultParser.rs b/src/client/result/VCardResultParser.rs index 290b95a..dd85017 100644 --- a/src/client/result/VCardResultParser.rs +++ b/src/client/result/VCardResultParser.rs @@ -20,7 +20,7 @@ use regex::Regex; use once_cell::sync::Lazy; -use crate::{RXingResult, common::CharacterSetECI}; +use crate::{common::CharacterSetECI, RXingResult}; use uriparse::URI; @@ -405,9 +405,7 @@ fn maybeAppendFragment(fragmentBuffer: &mut Vec, charset: &str, result: &mut fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()); // fragment = new String(fragmentBytes, StandardCharsets.UTF_8); } else if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(charset) { - fragment = if let Ok(encoded_result) = - enc.decode(&fragmentBytes) - { + fragment = if let Ok(encoded_result) = enc.decode(&fragmentBytes) { encoded_result } else { String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()) diff --git a/src/common/StringUtilsTestCase.rs b/src/common/StringUtilsTestCase.rs index 882ae3c..78cf3e0 100644 --- a/src/common/StringUtilsTestCase.rs +++ b/src/common/StringUtilsTestCase.rs @@ -40,19 +40,14 @@ fn test_random() { // } assert_eq!( CharacterSetECI::UTF8, - StringUtils::guessCharset(&bytes, &HashMap::new()) - .unwrap() + StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap() ); } #[test] fn test_short_shift_jis1() { // 金魚 - do_test( - &[0x8b, 0xe0, 0x8b, 0x9b], - CharacterSetECI::SJIS, - "SJIS", - ); + do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSetECI::SJIS, "SJIS"); } #[test] @@ -87,7 +82,7 @@ fn test_utf16_be() { do_test( &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], CharacterSetECI::UnicodeBigUnmarked, - &CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), + CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), ); } @@ -97,7 +92,7 @@ fn test_utf16_le() { do_test( &[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67], CharacterSetECI::UTF16LE, - &CharacterSetECI::UTF16LE.getCharsetName(), + CharacterSetECI::UTF16LE.getCharsetName(), ); } diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 25af1fe..8dbeffe 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -17,7 +17,7 @@ use encoding::EncodingRef; use crate::common::Result; -use crate::{Exceptions}; +use crate::Exceptions; /** * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1 @@ -51,11 +51,11 @@ pub enum CharacterSetECI { Cp1256, //(24, "windows-1256"), UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"), UTF16LE, - UTF8, //(26, "UTF-8"), - ASCII, //(new int[] {27, 170}, "US-ASCII"), - Big5, //(28), - GB18030, //(29, "GB2312", "EUC_CN", "GBK"), - EUC_KR, //(30, "EUC-KR"); + UTF8, //(26, "UTF-8"), + ASCII, //(new int[] {27, 170}, "US-ASCII"), + Big5, //(28), + GB18030, //(29, "GB2312", "EUC_CN", "GBK"), + EUC_KR, //(30, "EUC-KR"); } impl CharacterSetECI { // private static final Map VALUE_TO_ECI = new HashMap<>(); @@ -126,7 +126,7 @@ impl CharacterSetECI { } } - pub fn getCharset(&self, ) -> EncodingRef { + pub fn getCharset(&self) -> EncodingRef { let name = match self { // CharacterSetECI::Cp437 => "CP437", CharacterSetECI::Cp437 => "cp437", @@ -161,8 +161,8 @@ impl CharacterSetECI { encoding::label::encoding_from_whatwg_label(name).unwrap() } - pub fn getCharsetName(&self, ) -> &'static str { - match self { + pub fn getCharsetName(&self) -> &'static str { + match self { // CharacterSetECI::Cp437 => "CP437", CharacterSetECI::Cp437 => "cp437", CharacterSetECI::ISO8859_1 => "iso-8859-1", @@ -193,7 +193,6 @@ impl CharacterSetECI { CharacterSetECI::GB18030 => "gb2312", CharacterSetECI::EUC_KR => "euc-kr", } - } /** @@ -317,25 +316,33 @@ impl CharacterSetECI { } pub fn encode(&self, input: &str) -> Result> { - self.getCharset().encode(input, encoding::EncoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + self.getCharset() + .encode(input, encoding::EncoderTrap::Strict) + .map_err(|e| Exceptions::format_with(e.to_string())) } pub fn encode_replace(&self, input: &str) -> Result> { - self.getCharset().encode(input, encoding::EncoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + self.getCharset() + .encode(input, encoding::EncoderTrap::Replace) + .map_err(|e| Exceptions::format_with(e.to_string())) } - pub fn decode(&self, input:&[u8]) -> Result { + pub fn decode(&self, input: &[u8]) -> Result { if self == &CharacterSetECI::Cp437 { use codepage_437::BorrowFromCp437; use codepage_437::CP437_CONTROL; Ok(String::borrow_from_cp437(&input, &CP437_CONTROL)) - }else { - self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string())) + } else { + self.getCharset() + .decode(input, encoding::DecoderTrap::Strict) + .map_err(|e| Exceptions::format_with(e.to_string())) } } - pub fn decode_replace(&self, input:&[u8]) -> Result { - self.getCharset().decode(input, encoding::DecoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string())) + pub fn decode_replace(&self, input: &[u8]) -> Result { + self.getCharset() + .decode(input, encoding::DecoderTrap::Replace) + .map_err(|e| Exceptions::format_with(e.to_string())) } } diff --git a/src/common/eci_encoder_set.rs b/src/common/eci_encoder_set.rs index 0b4d9a9..c5dcf14 100644 --- a/src/common/eci_encoder_set.rs +++ b/src/common/eci_encoder_set.rs @@ -112,9 +112,7 @@ impl ECIEncoderSet { for encoder in &neededEncoders { // for (CharsetEncoder encoder : neededEncoders) { let c = stringToEncode.get(i).unwrap(); - if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) - || encoder.encode(c).is_ok() - { + if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) || encoder.encode(c).is_ok() { canEncode = true; break; } @@ -125,12 +123,7 @@ impl ECIEncoderSet { // for encoder in ENCODERS { let encoder = ENCODERS.get(i_encoder).unwrap(); // for (CharsetEncoder encoder : ENCODERS) { - if encoder - .encode( - stringToEncode.get(i).unwrap() - ) - .is_ok() - { + if encoder.encode(stringToEncode.get(i).unwrap()).is_ok() { //Good, we found an encoder that can encode the character. We add him to the list and continue scanning //the input neededEncoders.push(*encoder); diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index 7491b96..e06fdff 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -136,9 +136,7 @@ impl ECIStringBuilder { } else if !self.current_bytes.is_empty() { let bytes = std::mem::take(&mut self.current_bytes); self.current_bytes.clear(); - let encoded_value = encoder - .decode(&bytes) - .unwrap(); + let encoded_value = encoder.decode(&bytes).unwrap(); self.result.push_str(&encoded_value); } } else { diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index 51e586d..f7c80bd 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -21,7 +21,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::common::Result; use crate::Exceptions; -use super::{ECIEncoderSet, ECIInput, CharacterSetECI}; +use super::{CharacterSetECI, ECIEncoderSet, ECIInput}; //* approximated (latch + 2 codewords) pub const COST_PER_ECI: usize = 3; diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index f601c50..2d2f623 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -16,8 +16,6 @@ use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary}; -use once_cell::sync::Lazy; - use super::CharacterSetECI; /** @@ -50,8 +48,7 @@ const ASSUME_SHIFT_JIS: bool = false; // static SHIFT_JIS: &'static str = "SJIS"; // static GB2312: &'static str = "GB2312"; -pub static SHIFT_JIS_CHARSET: CharacterSetECI = - CharacterSetECI::SJIS; +pub static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS; // private static final boolean ASSUME_SHIFT_JIS = // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || @@ -74,7 +71,7 @@ impl StringUtils { } else if c == CharacterSetECI::ISO8859_1 { Some("ISO8859_1") } else { - Some(&c.getCharsetName()) + Some(c.getCharsetName()) } } @@ -92,7 +89,7 @@ impl StringUtils { hints.get(&DecodeHintType::CHARACTER_SET) { // if let DecodeHintValue::CharacterSet(cs_name) = hint { - return CharacterSetECI::getCharacterSetECIByName(cs_name) + return CharacterSetECI::getCharacterSetECIByName(cs_name); // } } // if hints.contains_key(&DecodeHintType::CHARACTER_SET) { @@ -260,6 +257,6 @@ impl StringUtils { return Some(CharacterSetECI::UTF8); } // Otherwise, we take a wild guess with platform encoding - Some(CharacterSetECI::UTF8) + Some(CharacterSetECI::UTF8) } } diff --git a/src/datamatrix/data_matrix_writer.rs b/src/datamatrix/data_matrix_writer.rs index 4409fb4..708f810 100644 --- a/src/datamatrix/data_matrix_writer.rs +++ b/src/datamatrix/data_matrix_writer.rs @@ -18,7 +18,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result, CharacterSetECI}, + common::{BitMatrix, CharacterSetECI, Result}, qrcode::encoder::ByteMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -122,7 +122,8 @@ impl Writer for DataMatrixWriter { hints.get(&EncodeHintType::CHARACTER_SET) else { return Err(Exceptions::illegal_argument_with("charset does not exist")) }; - charset = CharacterSetECI::getCharacterSetECIByName(char_set_name);//encoding::label::encoding_from_whatwg_label(char_set_name); + charset = CharacterSetECI::getCharacterSetECIByName(char_set_name); + //encoding::label::encoding_from_whatwg_label(char_set_name); // charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); } encoded = minimal_encoder::encodeHighLevelWithDetails( diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index 9048601..786f062 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result, CharacterSetECI}, + common::{BitSource, CharacterSetECI, DecoderRXingResult, ECIStringBuilder, Result}, Exceptions, }; @@ -727,12 +727,7 @@ fn decodeBase256Segment( *byte = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8; codewordPosition += 1; } - result.append_string( - - &CharacterSetECI::ISO8859_1 - .decode(&bytes) - ?, - ); + result.append_string(&CharacterSetECI::ISO8859_1.decode(&bytes)?); byteSegments.push(bytes); Ok(()) diff --git a/src/datamatrix/encoder/encoder_context.rs b/src/datamatrix/encoder/encoder_context.rs index 121ec92..9b19fb7 100644 --- a/src/datamatrix/encoder/encoder_context.rs +++ b/src/datamatrix/encoder/encoder_context.rs @@ -16,7 +16,7 @@ use std::rc::Rc; -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use crate::{Dimension, Exceptions}; use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint}; @@ -57,14 +57,10 @@ impl<'a> EncoderContext<'_> { // } // sb.append(ch); // } - let sb = if let Ok(encoded_bytes) = - ISO_8859_1_ENCODER.encode(msg) - { - ISO_8859_1_ENCODER - .decode(&encoded_bytes) - .map_err(|e| { - Exceptions::parse_with(format!("round trip decode should always work: {e}")) - })? + let sb = if let Ok(encoded_bytes) = ISO_8859_1_ENCODER.encode(msg) { + ISO_8859_1_ENCODER.decode(&encoded_bytes).map_err(|e| { + Exceptions::parse_with(format!("round trip decode should always work: {e}")) + })? } else { return Err(Exceptions::illegal_argument_with( "Message contains characters outside ISO-8859-1 encoding.", diff --git a/src/datamatrix/encoder/high_level_encode_test_case.rs b/src/datamatrix/encoder/high_level_encode_test_case.rs index f8c1244..09895b5 100644 --- a/src/datamatrix/encoder/high_level_encode_test_case.rs +++ b/src/datamatrix/encoder/high_level_encode_test_case.rs @@ -18,7 +18,10 @@ use std::rc::Rc; use once_cell::sync::Lazy; -use crate::{datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, common::CharacterSetECI}; +use crate::{ + common::CharacterSetECI, + datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, +}; use super::{high_level_encoder, minimal_encoder, symbol_info, SymbolInfoLookup}; diff --git a/src/datamatrix/encoder/high_level_encoder.rs b/src/datamatrix/encoder/high_level_encoder.rs index 5606d7e..a0dd06b 100644 --- a/src/datamatrix/encoder/high_level_encoder.rs +++ b/src/datamatrix/encoder/high_level_encoder.rs @@ -16,7 +16,7 @@ use std::rc::Rc; -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use crate::{Dimension, Exceptions}; use super::{ diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index 1d5dd06..3cf89b2 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{ECIInput, MinimalECIInput, Result, CharacterSetECI}, + common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, Exceptions, }; @@ -171,10 +171,7 @@ pub fn encodeHighLevelWithDetails( msg = &msg[high_level_encoder::MACRO_06_HEADER.chars().count()..(msg.chars().count() - 2)]; } Ok(ISO_8859_1_ENCODER - .decode( - &encode(msg, priorityCharset, fnc1, shape, macroId)? - - ) + .decode(&encode(msg, priorityCharset, fnc1, shape, macroId)?) .expect("should decode")) // return new String(encode(msg, priorityCharset, fnc1, shape, macroId), StandardCharsets.ISO_8859_1); } diff --git a/src/pdf417/encoder/pdf_417.rs b/src/pdf417/encoder/pdf_417.rs index 3ee462b..070f59d 100644 --- a/src/pdf417/encoder/pdf_417.rs +++ b/src/pdf417/encoder/pdf_417.rs @@ -18,7 +18,7 @@ * This file has been modified from its original form in Barcode4J. */ -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use crate::Exceptions; use super::{ diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index 7aef5f5..dd9717d 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -197,16 +197,17 @@ pub fn encodeHighLevel( input = Box::new(NoECIInput::new(msg.to_owned())); if encoding.is_none() { encoding = Some(DEFAULT_ENCODING); - } else if &DEFAULT_ENCODING - != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)? - { + } else if &DEFAULT_ENCODING != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)? { // if let Some(eci) = // CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) // { // encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; // } - encodingECI(CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, &mut sb)?; + encodingECI( + CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, + &mut sb, + )?; } } @@ -778,12 +779,7 @@ fn determineConsecutiveBinaryCount( } if let Some(encoder) = encoding { - let can_encode = encoder - .encode( - &input.charAt(idx)?.to_string() - - ) - .is_ok(); + let can_encode = encoder.encode(&input.charAt(idx)?.to_string()).is_ok(); if !can_encode { if TypeId::of::() != TypeId::of::() { @@ -866,7 +862,10 @@ impl Display for NoECIInput { */ #[cfg(test)] mod PDF417EncoderTestCase { - use crate::{pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, common::CharacterSetECI}; + use crate::{ + common::CharacterSetECI, + pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, + }; #[test] fn testEncodeAuto() { diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs index ee37383..7554081 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -use crate::common::{Result, CharacterSetECI}; +use crate::common::{CharacterSetECI, Result}; use super::{pdf_417_high_level_encoder, Compaction}; diff --git a/src/pdf417/pdf_417_writer.rs b/src/pdf417/pdf_417_writer.rs index 9f81929..77510c4 100644 --- a/src/pdf417/pdf_417_writer.rs +++ b/src/pdf417/pdf_417_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, Result, CharacterSetECI}, + common::{BitMatrix, CharacterSetECI, Result}, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index f84dbd5..b8682c8 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -203,8 +203,7 @@ fn decodeHanziSegment(bits: &mut BitSource, result: &mut String, count: usize) - count -= 1; } - let gb_encoder = - CharacterSetECI::GB18030; + let gb_encoder = CharacterSetECI::GB18030; let encode_string = gb_encoder .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; @@ -321,13 +320,9 @@ fn decodeByteSegment( // ) }; - let encode_string = - encoding - .decode(&readBytes) - .map_err(|e| { - Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) - })? - ; + let encode_string = encoding.decode(&readBytes).map_err(|e| { + Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) + })?; result.push_str(&encode_string); byteSegments.push(readBytes); diff --git a/src/qrcode/encoder/EncoderTestCase.rs b/src/qrcode/encoder/EncoderTestCase.rs index 13a754e..abcfe6b 100644 --- a/src/qrcode/encoder/EncoderTestCase.rs +++ b/src/qrcode/encoder/EncoderTestCase.rs @@ -28,8 +28,7 @@ use once_cell::sync::Lazy; use super::QRCode; -static SHIFT_JIS_CHARSET: Lazy = - Lazy::new(|| CharacterSetECI::SJIS); +static SHIFT_JIS_CHARSET: Lazy = Lazy::new(|| CharacterSetECI::SJIS); /** * @author satorux@google.com (Satoru Takabayashi) - creator diff --git a/src/qrcode/encoder/minimal_encoder.rs b/src/qrcode/encoder/minimal_encoder.rs index 1c03bdf..703ffec 100644 --- a/src/qrcode/encoder/minimal_encoder.rs +++ b/src/qrcode/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{BitArray, ECIEncoderSet, Result, CharacterSetECI}, + common::{BitArray, CharacterSetECI, ECIEncoderSet, Result}, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, Exceptions, }; @@ -1042,7 +1042,7 @@ impl fmt::Display for RXingResultNode { result.push('('); if self.mode == Mode::ECI { result.push_str( - &self.encoders + self.encoders .getCharset(self.charsetEncoderIndex) .ok_or(fmt::Error)? .getCharsetName(), diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index abfdaad..753290d 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -32,8 +32,7 @@ use crate::{ use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; -static SHIFT_JIS_CHARSET: CharacterSetECI = - CharacterSetECI::SJIS; +static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS; // The original table is defined in the table 5 of JISX0510:2004 (p.19). const ALPHANUMERIC_TABLE: [i8; 96] = [ @@ -97,8 +96,7 @@ pub fn encode_with_hints( let mut has_encoding_hint = hints.contains_key(&EncodeHintType::CHARACTER_SET); if has_encoding_hint { if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) { - encoding = - Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) + encoding = Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) } } @@ -122,9 +120,7 @@ pub fn encode_with_hints( //Switch to default encoding let encoding = if let Some(encoding) = encoding { encoding - } else if let Ok(_encs) = - DEFAULT_BYTE_MODE_ENCODING.encode(content) - { + } else if let Ok(_encs) = DEFAULT_BYTE_MODE_ENCODING.encode(content) { DEFAULT_BYTE_MODE_ENCODING } else { has_encoding_hint = true; @@ -720,7 +716,11 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()> Ok(()) } -pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: CharacterSetECI) -> Result<()> { +pub fn append8BitBytes( + content: &str, + bits: &mut BitArray, + encoding: CharacterSetECI, +) -> Result<()> { let bytes = encoding .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; diff --git a/tests/common/pdf_417_multiimage_span.rs b/tests/common/pdf_417_multiimage_span.rs index a0a00ea..5c7d465 100644 --- a/tests/common/pdf_417_multiimage_span.rs +++ b/tests/common/pdf_417_multiimage_span.rs @@ -24,7 +24,7 @@ use std::{ }; use rxing::{ - common::{HybridBinarizer, Result, CharacterSetECI}, + common::{CharacterSetECI, HybridBinarizer, Result}, multi::MultipleBarcodeReader, pdf417::PDF417RXingResultMetadata, BarcodeFormat, Binarizer, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, @@ -644,7 +644,9 @@ impl PDF417MultiImageSpanAbstractBlackBoxTest if ext == "bin" { let mut buffer: Vec = Vec::new(); File::open(&file)?.read_to_end(&mut buffer)?; - CharacterSetECI::ISO8859_1.decode_replace(&buffer).expect("decode") + CharacterSetECI::ISO8859_1 + .decode_replace(&buffer) + .expect("decode") } else { read_to_string(&file).expect("ok") } From 15859b9f10d0bc2797426d30567c359040a33582 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 4 Mar 2023 11:47:39 -0600 Subject: [PATCH 07/10] rename character set and rename methods --- src/aztec/EncoderTest.rs | 30 +- src/aztec/aztec_writer.rs | 6 +- src/aztec/decoder.rs | 6 +- src/aztec/encoder/aztec_encoder.rs | 12 +- src/aztec/encoder/high_level_encoder.rs | 12 +- src/aztec/encoder/state.rs | 4 +- src/client/result/VCardResultParser.rs | 4 +- src/common/StringUtilsTestCase.rs | 22 +- src/common/character_set_eci.rs | 373 ++++++++---------- src/common/eci_encoder_set.rs | 32 +- src/common/eci_string_builder.rs | 13 +- src/common/minimal_eci_input.rs | 4 +- src/common/string_utils.rs | 36 +- src/datamatrix/data_matrix_writer.rs | 6 +- .../decoder/decoded_bit_stream_parser.rs | 4 +- src/datamatrix/encoder/encoder_context.rs | 4 +- .../encoder/high_level_encode_test_case.rs | 4 +- src/datamatrix/encoder/high_level_encoder.rs | 4 +- src/datamatrix/encoder/minimal_encoder.rs | 10 +- .../decoder/pdf_417_decoder_test_case.rs | 8 +- src/pdf417/encoder/pdf_417.rs | 6 +- .../encoder/pdf_417_high_level_encoder.rs | 24 +- ...pdf_417_high_level_encoder_test_adapter.rs | 4 +- src/pdf417/pdf_417_writer.rs | 4 +- .../decoder/decoded_bit_stream_parser.rs | 20 +- src/qrcode/encoder/EncoderTestCase.rs | 6 +- src/qrcode/encoder/minimal_encoder.rs | 8 +- src/qrcode/encoder/qrcode_encoder.rs | 22 +- tests/common/pdf_417_multiimage_span.rs | 4 +- 29 files changed, 329 insertions(+), 363 deletions(-) diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index b69f38a..bea4a93 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -23,7 +23,7 @@ use crate::{ encoder::HighLevelEncoder, shared_test_methods::{stripSpace, toBitArray, toBooleanArray}, }, - common::CharacterSetECI, + common::CharacterSet, BarcodeFormat, EncodeHintType, EncodeHintValue, Point, }; @@ -40,10 +40,10 @@ use rand::Rng; * @author Frank Yellin */ -const ISO_8859_1: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1; -const UTF_8: CharacterSetECI = CharacterSetECI::UTF8; //StandardCharsets.UTF_8; -const ISO_8859_15: CharacterSetECI = CharacterSetECI::ISO8859_15; //Charset.forName("ISO-8859-15"); -const WINDOWS_1252: CharacterSetECI = CharacterSetECI::Cp1252; //Charset.forName("Windows-1252"); +const ISO_8859_1: CharacterSet = CharacterSet::ISO8859_1; //StandardCharsets.ISO_8859_1; +const UTF_8: CharacterSet = CharacterSet::UTF8; //StandardCharsets.UTF_8; +const ISO_8859_15: CharacterSet = CharacterSet::ISO8859_15; //Charset.forName("ISO-8859-15"); +const WINDOWS_1252: CharacterSet = CharacterSet::Cp1252; //Charset.forName("Windows-1252"); // const DOTX: &str = "[^.X]"; // const SPACES: &str = "\\s+"; @@ -137,8 +137,8 @@ X X X X X X X X X X X X X #[test] fn testAztecWriter() { - let shift_jis: CharacterSetECI = - CharacterSetECI::getCharacterSetECIByName("Shift_JIS").expect("must exist"); + let shift_jis: CharacterSet = + CharacterSet::get_character_set_by_name("Shift_JIS").expect("must exist"); 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 @@ -148,7 +148,7 @@ fn testAztecWriter() { testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 0, true, 2); testWriter( "\u{20AC} 1 sample data.", - Some(CharacterSetECI::UnicodeBigUnmarked), + Some(CharacterSet::UnicodeBigUnmarked), 0, true, 3, @@ -709,7 +709,7 @@ fn testEncodeDecode(data: &str, compact: bool, layers: u32) { fn testWriter( data: &str, - charset: Option, + charset: Option, ecc_percent: u32, compact: bool, layers: u32, @@ -719,7 +719,7 @@ fn testWriter( if charset.is_some() { hints.insert( EncodeHintType::CHARACTER_SET, - EncodeHintValue::CharacterSet(charset.unwrap().getCharsetName().to_string()), + EncodeHintValue::CharacterSet(charset.unwrap().get_charset_name().to_string()), ); } // if (null != charset) { @@ -735,7 +735,7 @@ fn testWriter( let cset = match charset { Some(cs) => cs, - None => CharacterSetECI::ISO8859_1, + None => CharacterSet::ISO8859_1, }; let aztec = aztec_encoder::encode_with_charset( data, @@ -818,10 +818,10 @@ fn testStuffBits(wordSize: usize, bits: &str, expected: &str) { fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::with_charset( - CharacterSetECI::UTF8 + CharacterSet::UTF8 .encode(s) .expect("should encode to bytes"), - CharacterSetECI::UTF8, + CharacterSet::UTF8, ) .encode() .expect("high level ok"); @@ -841,7 +841,7 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { fn testHighLevelEncodeString(s: &str, expectedBits: &str) { let bits = HighLevelEncoder::new( - CharacterSetECI::ISO8859_1 + CharacterSet::ISO8859_1 .encode(s) .expect("should encode to bytes"), ) @@ -862,7 +862,7 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) { fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) { let bits = HighLevelEncoder::new( - CharacterSetECI::ISO8859_1 + CharacterSet::ISO8859_1 .encode(s) .expect("should encode to bytes"), ) diff --git a/src/aztec/aztec_writer.rs b/src/aztec/aztec_writer.rs index 24f183a..223bd39 100644 --- a/src/aztec/aztec_writer.rs +++ b/src/aztec/aztec_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, CharacterSetECI, Result}, + common::{BitMatrix, CharacterSet, Result}, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer, }; @@ -56,7 +56,7 @@ impl Writer for AztecWriter { hints.get(&EncodeHintType::CHARACTER_SET) { if cset_name.to_lowercase() != "iso-8859-1" { - charset = CharacterSetECI::getCharacterSetECIByName(cset_name); + charset = CharacterSet::get_character_set_by_name(cset_name); } } if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) = @@ -86,7 +86,7 @@ fn encode( format: BarcodeFormat, width: u32, height: u32, - charset: Option, + charset: Option, ecc_percent: u32, layers: i32, ) -> Result { diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index 48cf998..71673cf 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -19,7 +19,7 @@ use crate::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonDecoder, }, - BitMatrix, CharacterSetECI, DecoderRXingResult, DetectorRXingResult, Result, + BitMatrix, CharacterSet, DecoderRXingResult, DetectorRXingResult, Result, }, exceptions::Exceptions, }; @@ -114,7 +114,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { // when character encoding changes (ECI) or input ends. let mut decoded_bytes: Vec = Vec::new(); - let mut encdr: CharacterSetECI = CharacterSetECI::ISO8859_1; + let mut encdr: CharacterSet = CharacterSet::ISO8859_1; let mut index = 0; @@ -182,7 +182,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { eci = eci * 10 + (next_digit - 2); n -= 1; } - let charset_eci = CharacterSetECI::getCharacterSetECIByValue(eci); + let charset_eci = CharacterSet::get_character_set_by_eci(eci); if charset_eci.is_err() { return Err(Exceptions::format_with("Charset must exist")); } diff --git a/src/aztec/encoder/aztec_encoder.rs b/src/aztec/encoder/aztec_encoder.rs index cef5c04..fa54464 100644 --- a/src/aztec/encoder/aztec_encoder.rs +++ b/src/aztec/encoder/aztec_encoder.rs @@ -19,7 +19,7 @@ use crate::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder, }, - BitArray, BitMatrix, CharacterSetECI, Result, + BitArray, BitMatrix, CharacterSet, Result, }, exceptions::Exceptions, }; @@ -49,7 +49,7 @@ pub const WORD_SIZE: [u32; 33] = [ * @return Aztec symbol matrix with metadata */ pub fn encode_simple(data: &str) -> Result { - let Ok(bytes) =CharacterSetECI::ISO8859_1.encode_replace(data) else { + let Ok(bytes) =CharacterSet::ISO8859_1.encode_replace(data) else { return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1"))); }; encode_bytes_simple(&bytes) @@ -65,7 +65,7 @@ pub fn encode_simple(data: &str) -> Result { * @return Aztec symbol matrix with metadata */ pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result { - if let Ok(bytes) = CharacterSetECI::ISO8859_1.encode(data) { + if let Ok(bytes) = CharacterSet::ISO8859_1.encode(data) { encode_bytes(&bytes, minECCPercent, userSpecifiedLayers) } else { Err(Exceptions::illegal_argument_with(format!( @@ -90,7 +90,7 @@ pub fn encode_with_charset( data: &str, minECCPercent: u32, userSpecifiedLayers: i32, - charset: CharacterSetECI, + charset: CharacterSet, ) -> Result { if let Ok(bytes) = charset.encode(data) { encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset) @@ -129,7 +129,7 @@ pub fn encode_bytes( data, minECCPercent, userSpecifiedLayers, - CharacterSetECI::ISO8859_1, + CharacterSet::ISO8859_1, ) } @@ -148,7 +148,7 @@ pub fn encode_bytes_with_charset( data: &[u8], min_eccpercent: u32, user_specified_layers: i32, - charset: CharacterSetECI, + charset: CharacterSet, ) -> Result { // High-level encode let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?; diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index 29bf492..da5b709 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -use crate::common::{BitArray, CharacterSetECI, Result}; +use crate::common::{BitArray, CharacterSet, Result}; use super::{State, Token}; @@ -32,7 +32,7 @@ use super::{State, Token}; */ pub struct HighLevelEncoder { text: Vec, - charset: CharacterSetECI, + charset: CharacterSet, } impl HighLevelEncoder { @@ -226,11 +226,11 @@ impl HighLevelEncoder { pub fn new(text: Vec) -> Self { Self { text, - charset: CharacterSetECI::ISO8859_1, + charset: CharacterSet::ISO8859_1, } } - pub fn with_charset(text: Vec, charset: CharacterSetECI) -> Self { + pub fn with_charset(text: Vec, charset: CharacterSet) -> Self { Self { text, charset } } @@ -240,9 +240,9 @@ impl HighLevelEncoder { pub fn encode(&self) -> Result { let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0); //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { - if self.charset != CharacterSetECI::ISO8859_1 { + if self.charset != CharacterSet::ISO8859_1 { //} && eci != CharacterSetECI::Cp1252 { - initial_state = initial_state.appendFLGn(self.charset.getValue())?; + initial_state = initial_state.appendFLGn(self.charset.get_eci_value())?; } // } else { // return Err(Exceptions::illegal_argument_with( diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index 73150a7..b4383b1 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -17,7 +17,7 @@ use std::fmt; use crate::{ - common::{BitArray, CharacterSetECI, Result}, + common::{BitArray, CharacterSet, Result}, exceptions::Exceptions, }; @@ -86,7 +86,7 @@ impl State { )); // throw new IllegalArgumentException("ECI code must be between 0 and 999999"); } else { - let Ok(eci_digits) = CharacterSetECI::ISO8859_1 + let Ok(eci_digits) = CharacterSet::ISO8859_1 .encode(&format!("{eci}")) else { return Err(Exceptions::ILLEGAL_ARGUMENT) diff --git a/src/client/result/VCardResultParser.rs b/src/client/result/VCardResultParser.rs index dd85017..ce66f7c 100644 --- a/src/client/result/VCardResultParser.rs +++ b/src/client/result/VCardResultParser.rs @@ -20,7 +20,7 @@ use regex::Regex; use once_cell::sync::Lazy; -use crate::{common::CharacterSetECI, RXingResult}; +use crate::{common::CharacterSet, RXingResult}; use uriparse::URI; @@ -404,7 +404,7 @@ fn maybeAppendFragment(fragmentBuffer: &mut Vec, charset: &str, result: &mut if charset.is_empty() { fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()); // fragment = new String(fragmentBytes, StandardCharsets.UTF_8); - } else if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(charset) { + } else if let Some(enc) = CharacterSet::get_character_set_by_name(charset) { fragment = if let Ok(encoded_result) = enc.decode(&fragmentBytes) { encoded_result } else { diff --git a/src/common/StringUtilsTestCase.rs b/src/common/StringUtilsTestCase.rs index 78cf3e0..b34ee2d 100644 --- a/src/common/StringUtilsTestCase.rs +++ b/src/common/StringUtilsTestCase.rs @@ -28,7 +28,7 @@ use std::collections::HashMap; use crate::common::StringUtils; -use super::CharacterSetECI; +use super::CharacterSet; #[test] fn test_random() { @@ -39,7 +39,7 @@ fn test_random() { // *byte = r.gen(); // } assert_eq!( - CharacterSetECI::UTF8, + CharacterSet::UTF8, StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap() ); } @@ -47,13 +47,13 @@ fn test_random() { #[test] fn test_short_shift_jis1() { // 金魚 - do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSetECI::SJIS, "SJIS"); + do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSet::SJIS, "SJIS"); } #[test] fn test_short_iso885911() { // båd - do_test(&[0x62, 0xe5, 0x64], CharacterSetECI::ISO8859_1, "ISO8859_1"); + do_test(&[0x62, 0xe5, 0x64], CharacterSet::ISO8859_1, "ISO8859_1"); } #[test] @@ -61,7 +61,7 @@ fn test_short_utf8() { // Español do_test( &[0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c], - CharacterSetECI::UTF8, + CharacterSet::UTF8, "UTF8", ); } @@ -71,7 +71,7 @@ fn test_mixed_shift_jis1() { // Hello 金! do_test( &[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21], - CharacterSetECI::SJIS, + CharacterSet::SJIS, "SJIS", ); } @@ -81,8 +81,8 @@ fn test_utf16_be() { // 调压柜 do_test( &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], - CharacterSetECI::UnicodeBigUnmarked, - CharacterSetECI::UnicodeBigUnmarked.getCharsetName(), + CharacterSet::UnicodeBigUnmarked, + CharacterSet::UnicodeBigUnmarked.get_charset_name(), ); } @@ -91,12 +91,12 @@ fn test_utf16_le() { // 调压柜 do_test( &[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67], - CharacterSetECI::UTF16LE, - CharacterSetECI::UTF16LE.getCharsetName(), + CharacterSet::UTF16LE, + CharacterSet::UTF16LE.get_charset_name(), ); } -fn do_test(bytes: &[u8], charset: CharacterSetECI, encoding: &str) { +fn do_test(bytes: &[u8], charset: CharacterSet, encoding: &str) { let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap(); let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new()).unwrap(); assert_eq!(charset, guessedCharset); diff --git a/src/common/character_set_eci.rs b/src/common/character_set_eci.rs index 8dbeffe..67b2760 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set_eci.rs @@ -26,7 +26,7 @@ use crate::Exceptions; * @author Sean Owen */ #[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub enum CharacterSetECI { +pub enum CharacterSet { // Enum name is a Java encoding valid for java.lang and java.io Cp437, //(new int[]{0,2}), ISO8859_1, //(new int[]{1,3}, "ISO-8859-1"), @@ -56,187 +56,152 @@ pub enum CharacterSetECI { Big5, //(28), GB18030, //(29, "GB2312", "EUC_CN", "GBK"), EUC_KR, //(30, "EUC-KR"); + // Unknown, + // Binary, } -impl CharacterSetECI { - // private static final Map VALUE_TO_ECI = new HashMap<>(); - // private static final Map NAME_TO_ECI = new HashMap<>(); - // static { - // for (CharacterSetECI eci : values()) { - // for (int value : eci.values) { - // VALUE_TO_ECI.put(value, eci); - // } - // NAME_TO_ECI.put(eci.name(), eci); - // for (String name : eci.otherEncodingNames) { - // NAME_TO_ECI.put(name, eci); - // } - // } - // } - - // private final int[] values; - // private final String[] otherEncodingNames; - - // CharacterSetECI(int value) { - // this(new int[] {value}); - // } - - // CharacterSetECI(int value, String... otherEncodingNames) { - // this.values = new int[] {value}; - // this.otherEncodingNames = otherEncodingNames; - // } - - // CharacterSetECI(int[] values, String... otherEncodingNames) { - // this.values = values; - // this.otherEncodingNames = otherEncodingNames; - // } - - pub fn getValueSelf(&self) -> u32 { - Self::getValue(self) - } - - pub fn getValue(&self) -> u32 { +impl CharacterSet { + pub fn get_eci_value(&self) -> u32 { match self { - CharacterSetECI::Cp437 => 0, - CharacterSetECI::ISO8859_1 => 1, - CharacterSetECI::ISO8859_2 => 4, - CharacterSetECI::ISO8859_3 => 5, - CharacterSetECI::ISO8859_4 => 6, - CharacterSetECI::ISO8859_5 => 7, + CharacterSet::Cp437 => 0, + CharacterSet::ISO8859_1 => 1, + CharacterSet::ISO8859_2 => 4, + CharacterSet::ISO8859_3 => 5, + CharacterSet::ISO8859_4 => 6, + CharacterSet::ISO8859_5 => 7, // CharacterSetECI::ISO8859_6 => 8, - CharacterSetECI::ISO8859_7 => 9, + CharacterSet::ISO8859_7 => 9, // CharacterSetECI::ISO8859_8 => 10, - CharacterSetECI::ISO8859_9 => 11, + CharacterSet::ISO8859_9 => 11, // CharacterSetECI::ISO8859_10 => 12, // CharacterSetECI::ISO8859_11 => 13, - CharacterSetECI::ISO8859_13 => 15, + CharacterSet::ISO8859_13 => 15, // CharacterSetECI::ISO8859_14 => 16, - CharacterSetECI::ISO8859_15 => 17, - CharacterSetECI::ISO8859_16 => 18, - CharacterSetECI::SJIS => 20, - CharacterSetECI::Cp1250 => 21, - CharacterSetECI::Cp1251 => 22, - CharacterSetECI::Cp1252 => 23, - CharacterSetECI::Cp1256 => 24, - CharacterSetECI::UnicodeBigUnmarked => 25, - CharacterSetECI::UTF16LE => 100025, - CharacterSetECI::UTF8 => 26, - CharacterSetECI::ASCII => 27, - CharacterSetECI::Big5 => 28, - CharacterSetECI::GB18030 => 29, - CharacterSetECI::EUC_KR => 30, + CharacterSet::ISO8859_15 => 17, + CharacterSet::ISO8859_16 => 18, + CharacterSet::SJIS => 20, + CharacterSet::Cp1250 => 21, + CharacterSet::Cp1251 => 22, + CharacterSet::Cp1252 => 23, + CharacterSet::Cp1256 => 24, + CharacterSet::UnicodeBigUnmarked => 25, + CharacterSet::UTF16LE => 100025, + CharacterSet::UTF8 => 26, + CharacterSet::ASCII => 27, + CharacterSet::Big5 => 28, + CharacterSet::GB18030 => 29, + CharacterSet::EUC_KR => 30, } } - pub fn getCharset(&self) -> EncodingRef { + fn get_base_encoder(&self) -> EncodingRef { let name = match self { - // CharacterSetECI::Cp437 => "CP437", - CharacterSetECI::Cp437 => "cp437", - CharacterSetECI::ISO8859_1 => return encoding::all::ISO_8859_1, - CharacterSetECI::ISO8859_2 => "ISO-8859-2", - CharacterSetECI::ISO8859_3 => "ISO-8859-3", - CharacterSetECI::ISO8859_4 => "ISO-8859-4", - CharacterSetECI::ISO8859_5 => "ISO-8859-5", + CharacterSet::Cp437 => "cp437", + CharacterSet::ISO8859_1 => return encoding::all::ISO_8859_1, + CharacterSet::ISO8859_2 => "ISO-8859-2", + CharacterSet::ISO8859_3 => "ISO-8859-3", + CharacterSet::ISO8859_4 => "ISO-8859-4", + CharacterSet::ISO8859_5 => "ISO-8859-5", // CharacterSetECI::ISO8859_6 => "ISO-8859-6", - CharacterSetECI::ISO8859_7 => "ISO-8859-7", + CharacterSet::ISO8859_7 => "ISO-8859-7", // CharacterSetECI::ISO8859_8 => "ISO-8859-8", - CharacterSetECI::ISO8859_9 => "ISO-8859-9", + CharacterSet::ISO8859_9 => "ISO-8859-9", // CharacterSetECI::ISO8859_10 => "ISO-8859-10", // CharacterSetECI::ISO8859_11 => "ISO-8859-11", - CharacterSetECI::ISO8859_13 => "ISO-8859-13", + CharacterSet::ISO8859_13 => "ISO-8859-13", // CharacterSetECI::ISO8859_14 => "ISO-8859-14", - CharacterSetECI::ISO8859_15 => "ISO-8859-15", - CharacterSetECI::ISO8859_16 => "ISO-8859-16", - CharacterSetECI::SJIS => "shift_jis", - CharacterSetECI::Cp1250 => "windows-1250", - CharacterSetECI::Cp1251 => "windows-1251", - CharacterSetECI::Cp1252 => "windows-1252", - CharacterSetECI::Cp1256 => "windows-1256", - CharacterSetECI::UnicodeBigUnmarked => "UTF-16BE", - CharacterSetECI::UTF16LE => "UTF-16LE", - CharacterSetECI::UTF8 => "UTF-8", - CharacterSetECI::ASCII => "US-ASCII", - CharacterSetECI::Big5 => "Big5", - CharacterSetECI::GB18030 => "GB2312", - CharacterSetECI::EUC_KR => "EUC-KR", + CharacterSet::ISO8859_15 => "ISO-8859-15", + CharacterSet::ISO8859_16 => "ISO-8859-16", + CharacterSet::SJIS => "shift_jis", + CharacterSet::Cp1250 => "windows-1250", + CharacterSet::Cp1251 => "windows-1251", + CharacterSet::Cp1252 => "windows-1252", + CharacterSet::Cp1256 => "windows-1256", + CharacterSet::UnicodeBigUnmarked => "UTF-16BE", + CharacterSet::UTF16LE => "UTF-16LE", + CharacterSet::UTF8 => "UTF-8", + CharacterSet::ASCII => "US-ASCII", + CharacterSet::Big5 => "Big5", + CharacterSet::GB18030 => "GB2312", + CharacterSet::EUC_KR => "EUC-KR", }; encoding::label::encoding_from_whatwg_label(name).unwrap() } - pub fn getCharsetName(&self) -> &'static str { + pub fn get_charset_name(&self) -> &'static str { match self { - // CharacterSetECI::Cp437 => "CP437", - CharacterSetECI::Cp437 => "cp437", - CharacterSetECI::ISO8859_1 => "iso-8859-1", - CharacterSetECI::ISO8859_2 => "iso-8859-2", - CharacterSetECI::ISO8859_3 => "iso-8859-3", - CharacterSetECI::ISO8859_4 => "iso-8859-4", - CharacterSetECI::ISO8859_5 => "iso-8859-5", + CharacterSet::Cp437 => "cp437", + CharacterSet::ISO8859_1 => "iso-8859-1", + CharacterSet::ISO8859_2 => "iso-8859-2", + CharacterSet::ISO8859_3 => "iso-8859-3", + CharacterSet::ISO8859_4 => "iso-8859-4", + CharacterSet::ISO8859_5 => "iso-8859-5", // CharacterSetECI::ISO8859_6 => "ISO-8859-6", - CharacterSetECI::ISO8859_7 => "iso-8859-7", + CharacterSet::ISO8859_7 => "iso-8859-7", // CharacterSetECI::ISO8859_8 => "ISO-8859-8", - CharacterSetECI::ISO8859_9 => "iso-8859-9", + CharacterSet::ISO8859_9 => "iso-8859-9", // CharacterSetECI::ISO8859_10 => "ISO-8859-10", // CharacterSetECI::ISO8859_11 => "ISO-8859-11", - CharacterSetECI::ISO8859_13 => "iso-8859-13", + CharacterSet::ISO8859_13 => "iso-8859-13", // CharacterSetECI::ISO8859_14 => "ISO-8859-14", - CharacterSetECI::ISO8859_15 => "iso-8859-15", - CharacterSetECI::ISO8859_16 => "iso-8859-16", - CharacterSetECI::SJIS => "shift_jis", - CharacterSetECI::Cp1250 => "windows-1250", - CharacterSetECI::Cp1251 => "windows-1251", - CharacterSetECI::Cp1252 => "windows-1252", - CharacterSetECI::Cp1256 => "windows-1256", - CharacterSetECI::UnicodeBigUnmarked => "utf-16be", - CharacterSetECI::UTF16LE => "utf-16le", - CharacterSetECI::UTF8 => "utf-8", - CharacterSetECI::ASCII => "us-ascii", - CharacterSetECI::Big5 => "big5", - CharacterSetECI::GB18030 => "gb2312", - CharacterSetECI::EUC_KR => "euc-kr", + CharacterSet::ISO8859_15 => "iso-8859-15", + CharacterSet::ISO8859_16 => "iso-8859-16", + CharacterSet::SJIS => "shift_jis", + CharacterSet::Cp1250 => "windows-1250", + CharacterSet::Cp1251 => "windows-1251", + CharacterSet::Cp1252 => "windows-1252", + CharacterSet::Cp1256 => "windows-1256", + CharacterSet::UnicodeBigUnmarked => "utf-16be", + CharacterSet::UTF16LE => "utf-16le", + CharacterSet::UTF8 => "utf-8", + CharacterSet::ASCII => "us-ascii", + CharacterSet::Big5 => "big5", + CharacterSet::GB18030 => "gb2312", + CharacterSet::EUC_KR => "euc-kr", } } - /** - * @param charset Java character set object - * @return CharacterSetECI representing ECI for character encoding, or null if it is legal - * but unsupported - */ - pub fn getCharacterSetECI(charset: EncodingRef) -> Option { - let name = if let Some(nm) = charset.whatwg_name() { - nm - } else { - charset.name() - }; - match name { - "cp437" => Some(CharacterSetECI::Cp437), - "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), - "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), - "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), - "iso-8859-4" => Some(CharacterSetECI::ISO8859_4), - "iso-8859-5" => Some(CharacterSetECI::ISO8859_5), - // "iso-8859-6" => Some(CharacterSetECI::ISO8859_6), - "iso-8859-7" => Some(CharacterSetECI::ISO8859_7), - // "iso-8859-8" => Some(CharacterSetECI::ISO8859_8), - "iso-8859-9" => Some(CharacterSetECI::ISO8859_9), - // "iso-8859-10" => Some(CharacterSetECI::ISO8859_10), - // "iso-8859-11" => Some(CharacterSetECI::ISO8859_11), - "iso-8859-13" => Some(CharacterSetECI::ISO8859_13), - // "iso-8859-14" => Some(CharacterSetECI::ISO8859_14), - "iso-8859-15" => Some(CharacterSetECI::ISO8859_15), - "iso-8859-16" => Some(CharacterSetECI::ISO8859_16), - "shift_jis" => Some(CharacterSetECI::SJIS), - "windows-1250" => Some(CharacterSetECI::Cp1250), - "windows-1251" => Some(CharacterSetECI::Cp1251), - "windows-1252" => Some(CharacterSetECI::Cp1252), - "windows-1256" => Some(CharacterSetECI::Cp1256), - "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), - "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), - "us-ascii" => Some(CharacterSetECI::ASCII), - "big5" => Some(CharacterSetECI::Big5), - "gb2312" => Some(CharacterSetECI::GB18030), - "euc-kr" => Some(CharacterSetECI::EUC_KR), - _ => None, - } - } + // /** + // * @param charset Java character set object + // * @return CharacterSetECI representing ECI for character encoding, or null if it is legal + // * but unsupported + // */ + // fn get_character_set_eci(charset: EncodingRef) -> Option { + // let name = if let Some(nm) = charset.whatwg_name() { + // nm + // } else { + // charset.name() + // }; + // match name { + // "cp437" => Some(CharacterSetECI::Cp437), + // "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), + // "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), + // "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), + // "iso-8859-4" => Some(CharacterSetECI::ISO8859_4), + // "iso-8859-5" => Some(CharacterSetECI::ISO8859_5), + // // "iso-8859-6" => Some(CharacterSetECI::ISO8859_6), + // "iso-8859-7" => Some(CharacterSetECI::ISO8859_7), + // // "iso-8859-8" => Some(CharacterSetECI::ISO8859_8), + // "iso-8859-9" => Some(CharacterSetECI::ISO8859_9), + // // "iso-8859-10" => Some(CharacterSetECI::ISO8859_10), + // // "iso-8859-11" => Some(CharacterSetECI::ISO8859_11), + // "iso-8859-13" => Some(CharacterSetECI::ISO8859_13), + // // "iso-8859-14" => Some(CharacterSetECI::ISO8859_14), + // "iso-8859-15" => Some(CharacterSetECI::ISO8859_15), + // "iso-8859-16" => Some(CharacterSetECI::ISO8859_16), + // "shift_jis" => Some(CharacterSetECI::SJIS), + // "windows-1250" => Some(CharacterSetECI::Cp1250), + // "windows-1251" => Some(CharacterSetECI::Cp1251), + // "windows-1252" => Some(CharacterSetECI::Cp1252), + // "windows-1256" => Some(CharacterSetECI::Cp1256), + // "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), + // "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), + // "us-ascii" => Some(CharacterSetECI::ASCII), + // "big5" => Some(CharacterSetECI::Big5), + // "gb2312" => Some(CharacterSetECI::GB18030), + // "euc-kr" => Some(CharacterSetECI::EUC_KR), + // _ => None, + // } + // } /** * @param value character set ECI value @@ -244,35 +209,35 @@ impl CharacterSetECI { * unsupported * @throws FormatException if ECI value is invalid */ - pub fn getCharacterSetECIByValue(value: u32) -> Result { + pub fn get_character_set_by_eci(value: u32) -> Result { match value { - 0 | 2 => Ok(CharacterSetECI::Cp437), - 1 | 3 => Ok(CharacterSetECI::ISO8859_1), - 4 => Ok(CharacterSetECI::ISO8859_2), - 5 => Ok(CharacterSetECI::ISO8859_3), - 6 => Ok(CharacterSetECI::ISO8859_4), - 7 => Ok(CharacterSetECI::ISO8859_5), + 0 | 2 => Ok(CharacterSet::Cp437), + 1 | 3 => Ok(CharacterSet::ISO8859_1), + 4 => Ok(CharacterSet::ISO8859_2), + 5 => Ok(CharacterSet::ISO8859_3), + 6 => Ok(CharacterSet::ISO8859_4), + 7 => Ok(CharacterSet::ISO8859_5), // 8 => Ok(CharacterSetECI::ISO8859_6), - 9 => Ok(CharacterSetECI::ISO8859_7), + 9 => Ok(CharacterSet::ISO8859_7), // 10 => Ok(CharacterSetECI::ISO8859_8), - 11 => Ok(CharacterSetECI::ISO8859_9), + 11 => Ok(CharacterSet::ISO8859_9), // 12 => Ok(CharacterSetECI::ISO8859_10), // 13 => Ok(CharacterSetECI::ISO8859_11), - 15 => Ok(CharacterSetECI::ISO8859_13), + 15 => Ok(CharacterSet::ISO8859_13), // 16 => Ok(CharacterSetECI::ISO8859_14), - 17 => Ok(CharacterSetECI::ISO8859_15), - 18 => Ok(CharacterSetECI::ISO8859_16), - 20 => Ok(CharacterSetECI::SJIS), - 21 => Ok(CharacterSetECI::Cp1250), - 22 => Ok(CharacterSetECI::Cp1251), - 23 => Ok(CharacterSetECI::Cp1252), - 24 => Ok(CharacterSetECI::Cp1256), - 25 => Ok(CharacterSetECI::UnicodeBigUnmarked), - 26 => Ok(CharacterSetECI::UTF8), - 27 | 170 => Ok(CharacterSetECI::ASCII), - 28 => Ok(CharacterSetECI::Big5), - 29 => Ok(CharacterSetECI::GB18030), - 30 => Ok(CharacterSetECI::EUC_KR), + 17 => Ok(CharacterSet::ISO8859_15), + 18 => Ok(CharacterSet::ISO8859_16), + 20 => Ok(CharacterSet::SJIS), + 21 => Ok(CharacterSet::Cp1250), + 22 => Ok(CharacterSet::Cp1251), + 23 => Ok(CharacterSet::Cp1252), + 24 => Ok(CharacterSet::Cp1256), + 25 => Ok(CharacterSet::UnicodeBigUnmarked), + 26 => Ok(CharacterSet::UTF8), + 27 | 170 => Ok(CharacterSet::ASCII), + 28 => Ok(CharacterSet::Big5), + 29 => Ok(CharacterSet::GB18030), + 30 => Ok(CharacterSet::EUC_KR), _ => Err(Exceptions::not_found_with("Bad ECI Value")), } } @@ -282,66 +247,66 @@ impl CharacterSetECI { * @return CharacterSetECI representing ECI for character encoding, or null if it is legal * but unsupported */ - pub fn getCharacterSetECIByName(name: &str) -> Option { + pub fn get_character_set_by_name(name: &str) -> Option { match name.to_lowercase().as_str() { - "cp437" => Some(CharacterSetECI::Cp437), - "iso-8859-1" => Some(CharacterSetECI::ISO8859_1), - "iso-8859-2" => Some(CharacterSetECI::ISO8859_2), - "iso-8859-3" => Some(CharacterSetECI::ISO8859_3), - "iso-8859-4" => Some(CharacterSetECI::ISO8859_4), - "iso-8859-5" => Some(CharacterSetECI::ISO8859_5), + "cp437" => Some(CharacterSet::Cp437), + "iso-8859-1" => Some(CharacterSet::ISO8859_1), + "iso-8859-2" => Some(CharacterSet::ISO8859_2), + "iso-8859-3" => Some(CharacterSet::ISO8859_3), + "iso-8859-4" => Some(CharacterSet::ISO8859_4), + "iso-8859-5" => Some(CharacterSet::ISO8859_5), // "ISO-8859-6" => Some(CharacterSetECI::ISO8859_6), - "iso-8859-7" => Some(CharacterSetECI::ISO8859_7), + "iso-8859-7" => Some(CharacterSet::ISO8859_7), // "ISO-8859-8" => Some(CharacterSetECI::ISO8859_8), - "iso-8859-9" => Some(CharacterSetECI::ISO8859_9), + "iso-8859-9" => Some(CharacterSet::ISO8859_9), // "ISO-8859-10" => Some(CharacterSetECI::ISO8859_10), // "ISO-8859-11" => Some(CharacterSetECI::ISO8859_11), - "iso-8859-13" => Some(CharacterSetECI::ISO8859_13), + "iso-8859-13" => Some(CharacterSet::ISO8859_13), // "ISO-8859-14" => Some(CharacterSetECI::ISO8859_14), - "iso-8859-15" => Some(CharacterSetECI::ISO8859_15), - "iso-8859-16" => Some(CharacterSetECI::ISO8859_16), - "shift_jis" => Some(CharacterSetECI::SJIS), - "windows-1250" => Some(CharacterSetECI::Cp1250), - "windows-1251" => Some(CharacterSetECI::Cp1251), - "windows-1252" => Some(CharacterSetECI::Cp1252), - "windows-1256" => Some(CharacterSetECI::Cp1256), - "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), - "utf-8" | "utf8" => Some(CharacterSetECI::UTF8), - "us-ascii" => Some(CharacterSetECI::ASCII), - "big5" => Some(CharacterSetECI::Big5), - "gb2312" => Some(CharacterSetECI::GB18030), - "euc-kr" => Some(CharacterSetECI::EUC_KR), + "iso-8859-15" => Some(CharacterSet::ISO8859_15), + "iso-8859-16" => Some(CharacterSet::ISO8859_16), + "shift_jis" => Some(CharacterSet::SJIS), + "windows-1250" => Some(CharacterSet::Cp1250), + "windows-1251" => Some(CharacterSet::Cp1251), + "windows-1252" => Some(CharacterSet::Cp1252), + "windows-1256" => Some(CharacterSet::Cp1256), + "utf-16be" => Some(CharacterSet::UnicodeBigUnmarked), + "utf-8" | "utf8" => Some(CharacterSet::UTF8), + "us-ascii" => Some(CharacterSet::ASCII), + "big5" => Some(CharacterSet::Big5), + "gb2312" => Some(CharacterSet::GB18030), + "euc-kr" => Some(CharacterSet::EUC_KR), _ => None, } } pub fn encode(&self, input: &str) -> Result> { - self.getCharset() + self.get_base_encoder() .encode(input, encoding::EncoderTrap::Strict) .map_err(|e| Exceptions::format_with(e.to_string())) } pub fn encode_replace(&self, input: &str) -> Result> { - self.getCharset() + self.get_base_encoder() .encode(input, encoding::EncoderTrap::Replace) .map_err(|e| Exceptions::format_with(e.to_string())) } pub fn decode(&self, input: &[u8]) -> Result { - if self == &CharacterSetECI::Cp437 { + if self == &CharacterSet::Cp437 { use codepage_437::BorrowFromCp437; use codepage_437::CP437_CONTROL; Ok(String::borrow_from_cp437(&input, &CP437_CONTROL)) } else { - self.getCharset() + self.get_base_encoder() .decode(input, encoding::DecoderTrap::Strict) .map_err(|e| Exceptions::format_with(e.to_string())) } } pub fn decode_replace(&self, input: &[u8]) -> Result { - self.getCharset() + self.get_base_encoder() .decode(input, encoding::DecoderTrap::Replace) .map_err(|e| Exceptions::format_with(e.to_string())) } diff --git a/src/common/eci_encoder_set.rs b/src/common/eci_encoder_set.rs index c5dcf14..003cebe 100644 --- a/src/common/eci_encoder_set.rs +++ b/src/common/eci_encoder_set.rs @@ -16,14 +16,14 @@ use unicode_segmentation::UnicodeSegmentation; -use super::CharacterSetECI; +use super::CharacterSet; use once_cell::sync::Lazy; -static ENCODERS: Lazy> = Lazy::new(|| { +static ENCODERS: Lazy> = Lazy::new(|| { let mut enc_vec = Vec::new(); for name in NAMES { - if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(name) { + if let Some(enc) = CharacterSet::get_character_set_by_name(name) { enc_vec.push(enc); } } @@ -69,7 +69,7 @@ const NAMES: [&str; 20] = [ */ #[derive(Clone)] pub struct ECIEncoderSet { - encoders: Vec, + encoders: Vec, priorityEncoderIndex: Option, } @@ -84,23 +84,23 @@ impl ECIEncoderSet { */ pub fn new( stringToEncodeMain: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option<&str>, ) -> Self { // List of encoders that potentially encode characters not in ISO-8859-1 in one byte. - let mut encoders: Vec; + let mut encoders: Vec; let mut priorityEncoderIndexValue = None; - let mut neededEncoders: Vec = Vec::new(); + let mut neededEncoders: Vec = Vec::new(); let stringToEncode = stringToEncodeMain.graphemes(true).collect::>(); //we always need the ISO-8859-1 encoder. It is the default encoding - neededEncoders.push(CharacterSetECI::ISO8859_1); + neededEncoders.push(CharacterSet::ISO8859_1); let mut needUnicodeEncoder = if let Some(pc) = priorityCharset { //pc.name().starts_with("UTF") || pc.name().starts_with("utf") - pc == CharacterSetECI::UTF8 || pc == CharacterSetECI::UnicodeBigUnmarked + pc == CharacterSet::UTF8 || pc == CharacterSet::UnicodeBigUnmarked } else { false }; @@ -142,7 +142,7 @@ impl ECIEncoderSet { if neededEncoders.len() == 1 && !needUnicodeEncoder { //the entire input can be encoded by the ISO-8859-1 encoder - encoders = vec![CharacterSetECI::ISO8859_1]; + encoders = vec![CharacterSet::ISO8859_1]; } else { // we need more than one single byte encoder or we need a Unicode encoder. // In this case we append a UTF-8 and UTF-16 encoder to the list @@ -156,8 +156,8 @@ impl ECIEncoderSet { encoders.push(encoder); } - encoders.push(CharacterSetECI::UTF8); - encoders.push(CharacterSetECI::UnicodeBigUnmarked); + encoders.push(CharacterSet::UTF8); + encoders.push(CharacterSet::UnicodeBigUnmarked); } //Compute priorityEncoderIndex by looking up priorityCharset in encoders @@ -175,7 +175,7 @@ impl ECIEncoderSet { } // } //invariants - assert_eq!(encoders[0], CharacterSetECI::ISO8859_1); + assert_eq!(encoders[0], CharacterSet::ISO8859_1); Self { encoders, priorityEncoderIndex: priorityEncoderIndexValue, @@ -192,13 +192,13 @@ impl ECIEncoderSet { pub fn getCharsetName(&self, index: usize) -> Option<&'static str> { if index < self.len() { - Some(self.encoders[index].getCharsetName()) + Some(self.encoders[index].get_charset_name()) } else { None } } - pub fn getCharset(&self, index: usize) -> Option { + pub fn getCharset(&self, index: usize) -> Option { if index < self.len() { Some(self.encoders[index]) } else { @@ -207,7 +207,7 @@ impl ECIEncoderSet { } pub fn getECIValue(&self, encoderIndex: usize) -> u32 { - self.encoders[encoderIndex].getValue() + self.encoders[encoderIndex].get_eci_value() // CharacterSetECI::getValue( // &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), // ) diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index e06fdff..6ee3dae 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -25,7 +25,7 @@ use std::fmt; use crate::common::Result; -use super::CharacterSetECI; +use super::CharacterSet; /** * Class that converts a sequence of ECIs and bytes into a string @@ -35,7 +35,7 @@ use super::CharacterSetECI; pub struct ECIStringBuilder { current_bytes: Vec, result: String, - current_charset: Option, //= StandardCharsets.ISO_8859_1; + current_charset: Option, //= StandardCharsets.ISO_8859_1; } impl ECIStringBuilder { @@ -43,14 +43,14 @@ impl ECIStringBuilder { Self { current_bytes: Vec::new(), result: String::new(), - current_charset: Some(CharacterSetECI::ISO8859_1), + current_charset: Some(CharacterSet::ISO8859_1), } } pub fn with_capacity(initial_capacity: usize) -> Self { Self { current_bytes: Vec::with_capacity(initial_capacity), result: String::with_capacity(initial_capacity), - current_charset: Some(CharacterSetECI::ISO8859_1), + current_charset: Some(CharacterSet::ISO8859_1), } } @@ -104,7 +104,8 @@ impl ECIStringBuilder { pub fn appendECI(&mut self, value: u32) -> Result<()> { self.encodeCurrentBytesIfAny(); - self.current_charset = CharacterSetECI::getCharacterSetECIByValue(value).ok(); + self.current_charset = CharacterSet::get_character_set_by_eci(value).ok(); + // if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { // // dbg!( @@ -126,7 +127,7 @@ impl ECIStringBuilder { /// This function can panic pub fn encodeCurrentBytesIfAny(&mut self) { if let Some(encoder) = self.current_charset { - if encoder == CharacterSetECI::UTF8 { + if encoder == CharacterSet::UTF8 { if !self.current_bytes.is_empty() { self.result.push_str( &String::from_utf8(std::mem::take(&mut self.current_bytes)).unwrap(), diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index f7c80bd..fca820d 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -21,7 +21,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::common::Result; use crate::Exceptions; -use super::{CharacterSetECI, ECIEncoderSet, ECIInput}; +use super::{CharacterSet, ECIEncoderSet, ECIInput}; //* approximated (latch + 2 codewords) pub const COST_PER_ECI: usize = 3; @@ -193,7 +193,7 @@ impl MinimalECIInput { */ pub fn new( stringToEncodeInput: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option<&str>, ) -> Self { let stringToEncode = stringToEncodeInput.graphemes(true).collect::>(); diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index 2d2f623..911b15b 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -16,7 +16,7 @@ use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary}; -use super::CharacterSetECI; +use super::CharacterSet; /** * Common string-related functions. @@ -48,7 +48,7 @@ const ASSUME_SHIFT_JIS: bool = false; // static SHIFT_JIS: &'static str = "SJIS"; // static GB2312: &'static str = "GB2312"; -pub static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS; +pub static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::SJIS; // private static final boolean ASSUME_SHIFT_JIS = // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || @@ -64,14 +64,14 @@ impl StringUtils { */ pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> { let c = StringUtils::guessCharset(bytes, hints)?; - if c == CharacterSetECI::SJIS { + if c == CharacterSet::SJIS { Some("SJIS") - } else if c == CharacterSetECI::UTF8 { + } else if c == CharacterSet::UTF8 { Some("UTF8") - } else if c == CharacterSetECI::ISO8859_1 { + } else if c == CharacterSet::ISO8859_1 { Some("ISO8859_1") } else { - Some(c.getCharsetName()) + Some(c.get_charset_name()) } } @@ -84,12 +84,12 @@ impl StringUtils { * or the platform default encoding if * none of these can possibly be correct */ - pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option { + pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option { if let Some(DecodeHintValue::CharacterSet(cs_name)) = hints.get(&DecodeHintType::CHARACTER_SET) { // if let DecodeHintValue::CharacterSet(cs_name) = hint { - return CharacterSetECI::getCharacterSetECIByName(cs_name); + return CharacterSet::get_character_set_by_name(cs_name); // } } // if hints.contains_key(&DecodeHintType::CHARACTER_SET) { @@ -101,9 +101,9 @@ impl StringUtils { && ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE)) { if bytes[0] == 0xFE && bytes[1] == 0xFF { - return Some(CharacterSetECI::UnicodeBigUnmarked); + return Some(CharacterSet::UnicodeBigUnmarked); } else { - return Some(CharacterSetECI::UTF16LE); + return Some(CharacterSet::UTF16LE); } } @@ -221,7 +221,7 @@ impl StringUtils { // Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) { - return Some(CharacterSetECI::UTF8); + return Some(CharacterSet::UTF8); } // Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done if can_be_shift_jis @@ -229,7 +229,7 @@ impl StringUtils { || sjis_max_katakana_word_length >= 3 || sjis_max_double_bytes_word_length >= 3) { - return Some(CharacterSetECI::SJIS); //encoding::label::encoding_from_whatwg_label("SJIS"); + return Some(CharacterSet::SJIS); //encoding::label::encoding_from_whatwg_label("SJIS"); } // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is: // - If we saw @@ -240,23 +240,23 @@ impl StringUtils { return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2) || iso_high_other * 10 >= length { - Some(CharacterSetECI::SJIS) + Some(CharacterSet::SJIS) } else { - Some(CharacterSetECI::ISO8859_1) + Some(CharacterSet::ISO8859_1) }; } // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding if can_be_iso88591 { - return Some(CharacterSetECI::ISO8859_1); + return Some(CharacterSet::ISO8859_1); } if can_be_shift_jis { - return Some(CharacterSetECI::SJIS); + return Some(CharacterSet::SJIS); } if can_be_utf8 { - return Some(CharacterSetECI::UTF8); + return Some(CharacterSet::UTF8); } // Otherwise, we take a wild guess with platform encoding - Some(CharacterSetECI::UTF8) + Some(CharacterSet::UTF8) } } diff --git a/src/datamatrix/data_matrix_writer.rs b/src/datamatrix/data_matrix_writer.rs index 708f810..ac14e12 100644 --- a/src/datamatrix/data_matrix_writer.rs +++ b/src/datamatrix/data_matrix_writer.rs @@ -18,7 +18,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, CharacterSetECI, Result}, + common::{BitMatrix, CharacterSet, Result}, qrcode::encoder::ByteMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -115,14 +115,14 @@ impl Writer for DataMatrixWriter { false }; - let mut charset: Option = None; + let mut charset: Option = None; let hasEncodingHint = hints.contains_key(&EncodeHintType::CHARACTER_SET); if hasEncodingHint { let Some(EncodeHintValue::CharacterSet(char_set_name)) = hints.get(&EncodeHintType::CHARACTER_SET) else { return Err(Exceptions::illegal_argument_with("charset does not exist")) }; - charset = CharacterSetECI::getCharacterSetECIByName(char_set_name); + charset = CharacterSet::get_character_set_by_name(char_set_name); //encoding::label::encoding_from_whatwg_label(char_set_name); // charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); } diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index 786f062..90922b3 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, CharacterSetECI, DecoderRXingResult, ECIStringBuilder, Result}, + common::{BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Result}, Exceptions, }; @@ -727,7 +727,7 @@ fn decodeBase256Segment( *byte = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8; codewordPosition += 1; } - result.append_string(&CharacterSetECI::ISO8859_1.decode(&bytes)?); + result.append_string(&CharacterSet::ISO8859_1.decode(&bytes)?); byteSegments.push(bytes); Ok(()) diff --git a/src/datamatrix/encoder/encoder_context.rs b/src/datamatrix/encoder/encoder_context.rs index 9b19fb7..3a1658c 100644 --- a/src/datamatrix/encoder/encoder_context.rs +++ b/src/datamatrix/encoder/encoder_context.rs @@ -16,12 +16,12 @@ use std::rc::Rc; -use crate::common::{CharacterSetECI, Result}; +use crate::common::{CharacterSet, Result}; use crate::{Dimension, Exceptions}; use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint}; -const ISO_8859_1_ENCODER: CharacterSetECI = CharacterSetECI::ISO8859_1; +const ISO_8859_1_ENCODER: CharacterSet = CharacterSet::ISO8859_1; pub struct EncoderContext<'a> { symbol_lookup: Rc>, diff --git a/src/datamatrix/encoder/high_level_encode_test_case.rs b/src/datamatrix/encoder/high_level_encode_test_case.rs index 09895b5..2e563a4 100644 --- a/src/datamatrix/encoder/high_level_encode_test_case.rs +++ b/src/datamatrix/encoder/high_level_encode_test_case.rs @@ -19,7 +19,7 @@ use std::rc::Rc; use once_cell::sync::Lazy; use crate::{ - common::CharacterSetECI, + common::CharacterSet, datamatrix::encoder::{SymbolInfo, SymbolShapeHint}, }; @@ -537,7 +537,7 @@ fn testECIs() { assert_eq!("239 209 151 206 214 92 122 140 35 158 144 162 52 205 55 171 137 23 67 206 218 175 147 113 15 254 116 33 241 25 231 186 14 212 64 253 151 252 159 33 41 241 27 231 83 171 53 209 35 25 134 6 42 33 35 239 184 31 193 234 7 252 205 101 127 241 209 34 24 5 22 23 221 148 179 239 128 140 92 187 106 204 198 59 19 25 114 248 118 36 254 231 106 196 19 239 101 27 107 69 189 112 236 156 252 16 174 125 24 10 125 116 42 129", visualized); - let visualized = visualize(&minimal_encoder::encodeHighLevelWithDetails("that particularly stands out to me is \u{0625}\u{0650}\u{062C}\u{064E}\u{0651}\u{0627}\u{0635} (\u{02BE}\u{0101}\u{1E63}) \"pear\", suggested to have originated from Hebrew \u{05D0}\u{05B7}\u{05D2}\u{05B8}\u{05BC}\u{05E1} (ag\u{00E1}s)", Some(CharacterSetECI::UTF8), None , SymbolShapeHint::FORCE_NONE).expect("encode")); + let visualized = visualize(&minimal_encoder::encodeHighLevelWithDetails("that particularly stands out to me is \u{0625}\u{0650}\u{062C}\u{064E}\u{0651}\u{0627}\u{0635} (\u{02BE}\u{0101}\u{1E63}) \"pear\", suggested to have originated from Hebrew \u{05D0}\u{05B7}\u{05D2}\u{05B8}\u{05BC}\u{05E1} (ag\u{00E1}s)", Some(CharacterSet::UTF8), None , SymbolShapeHint::FORCE_NONE).expect("encode")); assert_eq!("241 27 239 209 151 206 214 92 122 140 35 158 144 162 52 205 55 171 137 23 67 206 218 175 147 113 15 254 116 33 231 202 33 131 77 154 119 225 163 238 206 28 249 93 36 150 151 53 108 246 145 228 217 71 199 42 33 35 239 184 31 193 234 7 252 205 101 127 241 209 34 24 5 22 23 221 148 179 239 128 140 92 187 106 204 198 59 19 25 114 248 118 36 254 231 43 133 212 175 38 220 44 6 125 49 172 93 189 209 111 61 217 203 62 116 42 129 1 151 46 196 91 241 137 32 182 77 227 122 18 168 63 213 108 4 154 49 199 94 244 140 35 185 80", visualized); } diff --git a/src/datamatrix/encoder/high_level_encoder.rs b/src/datamatrix/encoder/high_level_encoder.rs index a0dd06b..5a87ff1 100644 --- a/src/datamatrix/encoder/high_level_encoder.rs +++ b/src/datamatrix/encoder/high_level_encoder.rs @@ -16,7 +16,7 @@ use std::rc::Rc; -use crate::common::{CharacterSetECI, Result}; +use crate::common::{CharacterSet, Result}; use crate::{Dimension, Exceptions}; use super::{ @@ -24,7 +24,7 @@ use super::{ SymbolInfoLookup, SymbolShapeHint, TextEncoder, X12Encoder, }; #[allow(dead_code)] -const DEFAULT_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; +const DEFAULT_ENCODING: CharacterSet = CharacterSet::ISO8859_1; /** * DataMatrix ECC 200 data encoder following the algorithm described in ISO/IEC 16022:200(E) in diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index 3cf89b2..e35ee30 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -17,13 +17,13 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, + common::{CharacterSet, ECIInput, MinimalECIInput, Result}, Exceptions, }; use super::{high_level_encoder, SymbolShapeHint}; -const ISO_8859_1_ENCODER: CharacterSetECI = CharacterSetECI::ISO8859_1; +const ISO_8859_1_ENCODER: CharacterSet = CharacterSet::ISO8859_1; /** * Encoder that encodes minimally @@ -151,7 +151,7 @@ pub fn encodeHighLevel(msg: &str) -> Result { */ pub fn encodeHighLevelWithDetails( msg: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, ) -> Result { @@ -192,7 +192,7 @@ pub fn encodeHighLevelWithDetails( */ fn encode( input: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, macroId: i32, @@ -1393,7 +1393,7 @@ struct Input { impl Input { pub fn new( stringToEncode: &str, - priorityCharset: Option, + priorityCharset: Option, fnc1: Option, shape: SymbolShapeHint, macroId: i32, diff --git a/src/pdf417/decoder/pdf_417_decoder_test_case.rs b/src/pdf417/decoder/pdf_417_decoder_test_case.rs index 58ab3c9..e7cfeae 100644 --- a/src/pdf417/decoder/pdf_417_decoder_test_case.rs +++ b/src/pdf417/decoder/pdf_417_decoder_test_case.rs @@ -17,7 +17,7 @@ use java_rand; -use crate::common::CharacterSetECI; +use crate::common::CharacterSet; use crate::pdf417::decoder::decoded_bit_stream_parser; use crate::pdf417::encoder::{pdf_417_high_level_encoder_test_adapter, Compaction}; use crate::pdf417::PDF417RXingResultMetadata; @@ -307,7 +307,7 @@ fn testBinaryData() { random.next_bytes(&mut bytes); total += encodeDecode( - &CharacterSetECI::ISO8859_1 + &CharacterSet::ISO8859_1 .decode(&bytes) .expect("decode bytes"), ); @@ -437,7 +437,7 @@ fn encodeDecode(input: &str) -> u32 { fn encodeDecodeWithAll( input: &str, - charset: Option, + charset: Option, autoECI: bool, decode: bool, ) -> u32 { @@ -523,7 +523,7 @@ fn performECITest( // for (int i = 0; i < 1000; i++) { let s = generateText(&mut random, 100, chars, weights); minLength += encodeDecodeWithAll(&s, None, true, true); - utfLength += encodeDecodeWithAll(&s, Some(CharacterSetECI::UTF8), false, true); + utfLength += encodeDecodeWithAll(&s, Some(CharacterSet::UTF8), false, true); } assert_eq!(expectedMinLength, minLength); assert_eq!(expectedUTFLength, utfLength); diff --git a/src/pdf417/encoder/pdf_417.rs b/src/pdf417/encoder/pdf_417.rs index 070f59d..184d6b5 100644 --- a/src/pdf417/encoder/pdf_417.rs +++ b/src/pdf417/encoder/pdf_417.rs @@ -18,7 +18,7 @@ * This file has been modified from its original form in Barcode4J. */ -use crate::common::{CharacterSetECI, Result}; +use crate::common::{CharacterSet, Result}; use crate::Exceptions; use super::{ @@ -32,7 +32,7 @@ pub struct PDF417 { barcodeMatrix: Option, compact: bool, compaction: Compaction, - encoding: Option, + encoding: Option, minCols: u32, maxCols: u32, maxRows: u32, @@ -345,7 +345,7 @@ impl PDF417 { /** * @param encoding sets character encoding to use */ - pub fn setEncoding(&mut self, encoding: Option) { + pub fn setEncoding(&mut self, encoding: Option) { self.encoding = encoding; } } diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index dd9717d..af78ccd 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -21,7 +21,7 @@ use std::{any::TypeId, fmt::Display, str::FromStr}; use crate::{ - common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, + common::{CharacterSet, ECIInput, MinimalECIInput, Result}, Exceptions, }; @@ -123,7 +123,7 @@ const TEXT_PUNCTUATION_RAW: [u8; 30] = [ 40, 41, 63, 123, 125, 39, 0, ]; -const DEFAULT_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1; +const DEFAULT_ENCODING: CharacterSet = CharacterSet::ISO8859_1; //StandardCharsets.ISO_8859_1; const MIXED: [i8; 128] = { let mut mixed = [-1_i8; 128]; @@ -172,7 +172,7 @@ const PUNCTUATION: [i8; 128] = { pub fn encodeHighLevel( msg: &str, compaction: Compaction, - encoding: Option, + encoding: Option, autoECI: bool, ) -> Result { let mut encoding = encoding; @@ -205,7 +205,7 @@ pub fn encodeHighLevel( // } encodingECI( - CharacterSetECI::getValue(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, + CharacterSet::get_eci_value(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, &mut sb, )?; } @@ -758,7 +758,7 @@ fn determineConsecutiveTextCount(input: &T, startpos: u32) fn determineConsecutiveBinaryCount( input: &T, startpos: u32, - encoding: Option, + encoding: Option, ) -> Result { let len = input.length(); let mut idx = startpos as usize; @@ -863,13 +863,13 @@ impl Display for NoECIInput { #[cfg(test)] mod PDF417EncoderTestCase { use crate::{ - common::CharacterSetECI, + common::CharacterSet, pdf417::encoder::{pdf_417_high_level_encoder::encodeHighLevel, Compaction}, }; #[test] fn testEncodeAuto() { - let encoded = encodeHighLevel("ABCD", Compaction::AUTO, Some(CharacterSetECI::UTF8), false) + let encoded = encodeHighLevel("ABCD", Compaction::AUTO, Some(CharacterSet::UTF8), false) .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0385}ABCD", encoded); } @@ -880,7 +880,7 @@ mod PDF417EncoderTestCase { encodeHighLevel( "1%§s ?aG$", Compaction::AUTO, - Some(CharacterSetECI::UTF8), + Some(CharacterSet::UTF8), false, ) .expect("encode"); @@ -892,7 +892,7 @@ mod PDF417EncoderTestCase { encodeHighLevel( "asdfg§asd", Compaction::AUTO, - Some(CharacterSetECI::ISO8859_1), + Some(CharacterSet::ISO8859_1), false, ) .expect("encode"); @@ -900,7 +900,7 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeText() { - let encoded = encodeHighLevel("ABCD", Compaction::TEXT, Some(CharacterSetECI::UTF8), false) + let encoded = encodeHighLevel("ABCD", Compaction::TEXT, Some(CharacterSet::UTF8), false) .expect("encode"); assert_eq!("Ο\u{001A}\u{0001}?", encoded); } @@ -910,7 +910,7 @@ mod PDF417EncoderTestCase { let encoded = encodeHighLevel( "1234", Compaction::NUMERIC, - Some(CharacterSetECI::UTF8), + Some(CharacterSet::UTF8), false, ) .expect("encode"); @@ -920,7 +920,7 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeByte() { - let encoded = encodeHighLevel("abcd", Compaction::BYTE, Some(CharacterSetECI::UTF8), false) + let encoded = encodeHighLevel("abcd", Compaction::BYTE, Some(CharacterSet::UTF8), false) .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0385}abcd", encoded); } diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs index 7554081..3c76aea 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder_test_adapter.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -use crate::common::{CharacterSetECI, Result}; +use crate::common::{CharacterSet, Result}; use super::{pdf_417_high_level_encoder, Compaction}; @@ -25,7 +25,7 @@ use super::{pdf_417_high_level_encoder, Compaction}; pub fn encodeHighLevel( msg: &str, compaction: Compaction, - encoding: Option, + encoding: Option, autoECI: bool, ) -> Result { pdf_417_high_level_encoder::encodeHighLevel(msg, compaction, encoding, autoECI) diff --git a/src/pdf417/pdf_417_writer.rs b/src/pdf417/pdf_417_writer.rs index 77510c4..774abd2 100644 --- a/src/pdf417/pdf_417_writer.rs +++ b/src/pdf417/pdf_417_writer.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitMatrix, CharacterSetECI, Result}, + common::{BitMatrix, CharacterSet, Result}, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, }; @@ -108,7 +108,7 @@ impl Writer for PDF417Writer { if let Some(EncodeHintValue::CharacterSet(cs)) = hints.get(&EncodeHintType::CHARACTER_SET) { - encoder.setEncoding(CharacterSetECI::getCharacterSetECIByName(cs)); + encoder.setEncoding(CharacterSet::get_character_set_by_name(cs)); } if let Some(EncodeHintValue::Pdf417AutoEci(auto_eci_str)) = hints.get(&EncodeHintType::PDF417_AUTO_ECI) diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index b8682c8..b0e45b4 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, CharacterSetECI, DecoderRXingResult, Result, StringUtils}, + common::{BitSource, CharacterSet, DecoderRXingResult, Result, StringUtils}, DecodingHintDictionary, Exceptions, }; @@ -91,7 +91,7 @@ pub fn decode( Mode::ECI => { // Count doesn't apply to ECI let value = parseECIValue(&mut bits)?; - currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue(value).ok(); + currentCharacterSetECI = CharacterSet::get_character_set_by_eci(value).ok(); if currentCharacterSetECI.is_none() { return Err(Exceptions::format_with(format!( "Value of {value} not valid" @@ -203,7 +203,7 @@ fn decodeHanziSegment(bits: &mut BitSource, result: &mut String, count: usize) - count -= 1; } - let gb_encoder = CharacterSetECI::GB18030; + let gb_encoder = CharacterSet::GB18030; let encode_string = gb_encoder .decode(&buffer) .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; @@ -215,7 +215,7 @@ fn decodeKanjiSegment( bits: &mut BitSource, result: &mut String, count: usize, - currentCharacterSetECI: Option, + currentCharacterSetECI: Option, hints: &DecodingHintDictionary, ) -> Result<()> { // Don't crash trying to read more bits than we have available. @@ -249,7 +249,7 @@ fn decodeKanjiSegment( let encoder = { let _ = currentCharacterSetECI; let _ = hints; - CharacterSetECI::SJIS + CharacterSet::SJIS }; #[cfg(feature = "allow_forced_iso_ied_18004_compliance")] @@ -257,12 +257,12 @@ fn decodeKanjiSegment( hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) { if let Some(ccse) = ¤tCharacterSetECI { - CharacterSetECI::getCharacterSetECIByName(ccse) + CharacterSet::getCharacterSetECIByName(ccse) } else { - CharacterSetECI::ISO8859_1 + CharacterSet::ISO8859_1 } } else { - CharacterSetECI::SJIS + CharacterSet::SJIS }; let encode_string = encoder @@ -278,7 +278,7 @@ fn decodeByteSegment( bits: &mut BitSource, result: &mut String, count: usize, - currentCharacterSetECI: Option, + currentCharacterSetECI: Option, byteSegments: &mut Vec>, hints: &DecodingHintDictionary, ) -> Result<()> { @@ -307,7 +307,7 @@ fn decodeByteSegment( if let Some(DecodeHintValue::QrAssumeSpecConformInput(true)) = hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) { - CharacterSetECI::ISO8859_1 + CharacterSet::ISO8859_1 } else { StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::ILLEGAL_STATE)? } diff --git a/src/qrcode/encoder/EncoderTestCase.rs b/src/qrcode/encoder/EncoderTestCase.rs index abcfe6b..96f4f44 100644 --- a/src/qrcode/encoder/EncoderTestCase.rs +++ b/src/qrcode/encoder/EncoderTestCase.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{BitArray, CharacterSetECI}, + common::{BitArray, CharacterSet}, qrcode::{ decoder::{ErrorCorrectionLevel, Mode, Version}, encoder::{qrcode_encoder, MinimalEncoder}, @@ -28,7 +28,7 @@ use once_cell::sync::Lazy; use super::QRCode; -static SHIFT_JIS_CHARSET: Lazy = Lazy::new(|| CharacterSetECI::SJIS); +static SHIFT_JIS_CHARSET: Lazy = Lazy::new(|| CharacterSet::SJIS); /** * @author satorux@google.com (Satoru Takabayashi) - creator @@ -1108,7 +1108,7 @@ fn testMinimalEncoder44() { fn verifyMinimalEncoding( input: &str, expectedRXingResult: &str, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ) { let result = MinimalEncoder::encode_with_details( diff --git a/src/qrcode/encoder/minimal_encoder.rs b/src/qrcode/encoder/minimal_encoder.rs index 703ffec..45b5929 100644 --- a/src/qrcode/encoder/minimal_encoder.rs +++ b/src/qrcode/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{BitArray, CharacterSetECI, ECIEncoderSet, Result}, + common::{BitArray, CharacterSet, ECIEncoderSet, Result}, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, Exceptions, }; @@ -105,7 +105,7 @@ impl MinimalEncoder { */ pub fn new( stringToEncode: &str, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ecLevel: ErrorCorrectionLevel, ) -> Self { @@ -140,7 +140,7 @@ impl MinimalEncoder { pub fn encode_with_details( stringToEncode: &str, version: Option, - priorityCharset: Option, + priorityCharset: Option, isGS1: bool, ecLevel: ErrorCorrectionLevel, ) -> Result { @@ -1045,7 +1045,7 @@ impl fmt::Display for RXingResultNode { self.encoders .getCharset(self.charsetEncoderIndex) .ok_or(fmt::Error)? - .getCharsetName(), + .get_charset_name(), ); } else { let sub_string: String = self diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index 753290d..9eccc37 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -24,7 +24,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ common::{ reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonEncoder}, - BitArray, CharacterSetECI, Result, + BitArray, CharacterSet, Result, }, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, EncodeHintType, EncodeHintValue, EncodingHintDictionary, Exceptions, @@ -32,7 +32,7 @@ use crate::{ use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; -static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS; +static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::SJIS; // The original table is defined in the table 5 of JISX0510:2004 (p.19). const ALPHANUMERIC_TABLE: [i8; 96] = [ @@ -44,7 +44,7 @@ const ALPHANUMERIC_TABLE: [i8; 96] = [ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f ]; -pub const DEFAULT_BYTE_MODE_ENCODING: CharacterSetECI = CharacterSetECI::ISO8859_1; +pub const DEFAULT_BYTE_MODE_ENCODING: CharacterSet = CharacterSet::ISO8859_1; // The mask penalty calculation is complicated. See Table 21 of JISX0510:2004 (p.45) for details. // Basically it applies four rules and summate all penalties. @@ -96,7 +96,7 @@ pub fn encode_with_hints( let mut has_encoding_hint = hints.contains_key(&EncodeHintType::CHARACTER_SET); if has_encoding_hint { if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) { - encoding = Some(CharacterSetECI::getCharacterSetECIByName(v).ok_or(Exceptions::WRITER)?) + encoding = Some(CharacterSet::get_character_set_by_name(v).ok_or(Exceptions::WRITER)?) } } @@ -124,7 +124,7 @@ pub fn encode_with_hints( DEFAULT_BYTE_MODE_ENCODING } else { has_encoding_hint = true; - CharacterSetECI::UTF8 + CharacterSet::UTF8 }; // Pick an encoding mode appropriate for the content. Note that this will not attempt to use @@ -295,14 +295,14 @@ pub fn getAlphanumericCode(code: u32) -> i8 { } pub fn chooseMode(content: &str) -> Mode { - chooseModeWithEncoding(content, CharacterSetECI::ISO8859_1) + chooseModeWithEncoding(content, CharacterSet::ISO8859_1) } /** * Choose the best mode by examining the content. Note that 'encoding' is used as a hint; * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ -fn chooseModeWithEncoding(content: &str, encoding: CharacterSetECI) -> Mode { +fn chooseModeWithEncoding(content: &str, encoding: CharacterSet) -> Mode { if SHIFT_JIS_CHARSET == encoding && isOnlyDoubleByteKanji(content) { // Choose Kanji mode if all input are double-byte characters return Mode::KANJI; @@ -629,7 +629,7 @@ pub fn appendBytes( content: &str, mode: Mode, bits: &mut BitArray, - encoding: CharacterSetECI, + encoding: CharacterSet, ) -> Result<()> { match mode { Mode::NUMERIC => appendNumericBytes(content, bits), @@ -719,7 +719,7 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()> pub fn append8BitBytes( content: &str, bits: &mut BitArray, - encoding: CharacterSetECI, + encoding: CharacterSet, ) -> Result<()> { let bytes = encoding .encode(content) @@ -762,8 +762,8 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<()> { Ok(()) } -fn appendECI(eci: &CharacterSetECI, bits: &mut BitArray) -> Result<()> { +fn appendECI(eci: &CharacterSet, bits: &mut BitArray) -> Result<()> { bits.appendBits(Mode::ECI.getBits() as u32, 4)?; // This is correct for values up to 127, which is all we need now. - bits.appendBits(eci.getValueSelf(), 8) + bits.appendBits(eci.get_eci_value(), 8) } diff --git a/tests/common/pdf_417_multiimage_span.rs b/tests/common/pdf_417_multiimage_span.rs index 5c7d465..d7a3bf3 100644 --- a/tests/common/pdf_417_multiimage_span.rs +++ b/tests/common/pdf_417_multiimage_span.rs @@ -24,7 +24,7 @@ use std::{ }; use rxing::{ - common::{CharacterSetECI, HybridBinarizer, Result}, + common::{CharacterSet, HybridBinarizer, Result}, multi::MultipleBarcodeReader, pdf417::PDF417RXingResultMetadata, BarcodeFormat, Binarizer, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, @@ -644,7 +644,7 @@ impl PDF417MultiImageSpanAbstractBlackBoxTest if ext == "bin" { let mut buffer: Vec = Vec::new(); File::open(&file)?.read_to_end(&mut buffer)?; - CharacterSetECI::ISO8859_1 + CharacterSet::ISO8859_1 .decode_replace(&buffer) .expect("decode") } else { From 8a0744e53400f42cfed3f4ec0e5752c04e275952 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 4 Mar 2023 14:13:50 -0600 Subject: [PATCH 08/10] move to using Eci enum Decouples the Eci and CharacterSet concepts within the library. Character sets can now exist independently from Eci encodation schemes. --- src/aztec/EncoderTest.rs | 2 +- src/aztec/decoder.rs | 8 +- src/aztec/encoder/high_level_encoder.rs | 2 +- src/aztec/encoder/state.rs | 6 +- src/common/StringUtilsTestCase.rs | 8 +- ...{character_set_eci.rs => character_set.rs} | 251 ++++++++++-------- src/common/eci.rs | 181 +++++++++++++ src/common/eci_encoder_set.rs | 10 +- src/common/eci_input.rs | 4 +- src/common/eci_string_builder.rs | 19 +- src/common/minimal_eci_input.rs | 8 +- src/common/mod.rs | 7 +- src/common/string_utils.rs | 12 +- .../decoder/decoded_bit_stream_parser.rs | 8 +- src/datamatrix/encoder/minimal_encoder.rs | 4 +- .../decoder/decoded_bit_stream_parser.rs | 10 +- .../encoder/pdf_417_high_level_encoder.rs | 23 +- .../decoder/decoded_bit_stream_parser.rs | 8 +- src/qrcode/encoder/EncoderTestCase.rs | 2 +- src/qrcode/encoder/minimal_encoder.rs | 2 +- src/qrcode/encoder/qrcode_encoder.rs | 10 +- 21 files changed, 403 insertions(+), 182 deletions(-) rename src/common/{character_set_eci.rs => character_set.rs} (57%) create mode 100644 src/common/eci.rs diff --git a/src/aztec/EncoderTest.rs b/src/aztec/EncoderTest.rs index bea4a93..3b98728 100644 --- a/src/aztec/EncoderTest.rs +++ b/src/aztec/EncoderTest.rs @@ -148,7 +148,7 @@ fn testAztecWriter() { testWriter("\u{20AC} 1 sample data.", Some(ISO_8859_15), 0, true, 2); testWriter( "\u{20AC} 1 sample data.", - Some(CharacterSet::UnicodeBigUnmarked), + Some(CharacterSet::UTF16BE), 0, true, 3, diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index 71673cf..e0d1dc6 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -19,7 +19,7 @@ use crate::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonDecoder, }, - BitMatrix, CharacterSet, DecoderRXingResult, DetectorRXingResult, Result, + BitMatrix, CharacterSet, DecoderRXingResult, DetectorRXingResult, Result, Eci, }, exceptions::Exceptions, }; @@ -182,11 +182,11 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { eci = eci * 10 + (next_digit - 2); n -= 1; } - let charset_eci = CharacterSet::get_character_set_by_eci(eci); - if charset_eci.is_err() { + let charset_eci : Eci= eci.into(); + if charset_eci == Eci::Unknown { return Err(Exceptions::format_with("Charset must exist")); } - encdr = charset_eci?; + encdr = charset_eci.into(); } } // Go back to whatever mode we had been in diff --git a/src/aztec/encoder/high_level_encoder.rs b/src/aztec/encoder/high_level_encoder.rs index da5b709..6fe56d4 100644 --- a/src/aztec/encoder/high_level_encoder.rs +++ b/src/aztec/encoder/high_level_encoder.rs @@ -242,7 +242,7 @@ impl HighLevelEncoder { //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { if self.charset != CharacterSet::ISO8859_1 { //} && eci != CharacterSetECI::Cp1252 { - initial_state = initial_state.appendFLGn(self.charset.get_eci_value())?; + initial_state = initial_state.appendFLGn(self.charset.into())?; } // } else { // return Err(Exceptions::illegal_argument_with( diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index b4383b1..99b9772 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -17,7 +17,7 @@ use std::fmt; use crate::{ - common::{BitArray, CharacterSet, Result}, + common::{BitArray, CharacterSet, Result, Eci}, exceptions::Exceptions, }; @@ -71,7 +71,7 @@ impl State { self.bit_count } - pub fn appendFLGn(self, eci: u32) -> Result { + pub fn appendFLGn(self, eci: Eci) -> Result { let bit_count = self.bit_count; let mode = self.mode; let result = self.shiftAndAppend(HighLevelEncoder::MODE_PUNCT as u32, 0); // 0: FLG(n) @@ -80,7 +80,7 @@ impl State { /*if eci < 0 { token.add(0, 3); // 0: FNC1 } else */ - if eci > 999999 { + if eci as u32 > 999999 { return Err(Exceptions::illegal_argument_with( "ECI code must be between 0 and 999999", )); diff --git a/src/common/StringUtilsTestCase.rs b/src/common/StringUtilsTestCase.rs index b34ee2d..dbcface 100644 --- a/src/common/StringUtilsTestCase.rs +++ b/src/common/StringUtilsTestCase.rs @@ -47,7 +47,7 @@ fn test_random() { #[test] fn test_short_shift_jis1() { // 金魚 - do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSet::SJIS, "SJIS"); + do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSet::Shift_JIS, "SJIS"); } #[test] @@ -71,7 +71,7 @@ fn test_mixed_shift_jis1() { // Hello 金! do_test( &[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21], - CharacterSet::SJIS, + CharacterSet::Shift_JIS, "SJIS", ); } @@ -81,8 +81,8 @@ fn test_utf16_be() { // 调压柜 do_test( &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], - CharacterSet::UnicodeBigUnmarked, - CharacterSet::UnicodeBigUnmarked.get_charset_name(), + CharacterSet::UTF16BE, + CharacterSet::UTF16BE.get_charset_name(), ); } diff --git a/src/common/character_set_eci.rs b/src/common/character_set.rs similarity index 57% rename from src/common/character_set_eci.rs rename to src/common/character_set.rs index 67b2760..0bc6214 100644 --- a/src/common/character_set_eci.rs +++ b/src/common/character_set.rs @@ -34,64 +34,72 @@ pub enum CharacterSet { ISO8859_3, //(5, "ISO-8859-3"), ISO8859_4, //(6, "ISO-8859-4"), ISO8859_5, //(7, "ISO-8859-5"), - //ISO8859_6, //(8, "ISO-8859-6"), + ISO8859_6, //(8, "ISO-8859-6"), ISO8859_7, //(9, "ISO-8859-7"), - //ISO8859_8, //(10, "ISO-8859-8"), + ISO8859_8, //(10, "ISO-8859-8"), ISO8859_9, //(11, "ISO-8859-9"), - // ISO8859_10, //(12, "ISO-8859-10"), - // ISO8859_11, //(13, "ISO-8859-11"), + ISO8859_10, //(12, "ISO-8859-10"), + ISO8859_11, //(13, "ISO-8859-11"), ISO8859_13, //(15, "ISO-8859-13"), - // ISO8859_14, //(16, "ISO-8859-14"), + ISO8859_14, //(16, "ISO-8859-14"), ISO8859_15, //(17, "ISO-8859-15"), ISO8859_16, //(18, "ISO-8859-16"), - SJIS, //(20, "Shift_JIS"), + Shift_JIS, //(20, "Shift_JIS"), Cp1250, //(21, "windows-1250"), Cp1251, //(22, "windows-1251"), Cp1252, //(23, "windows-1252"), Cp1256, //(24, "windows-1256"), - UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"), + UTF16BE, //(25, "UTF-16BE", "UnicodeBig"), + UTF8, //(26, "UTF-8"), + ASCII, //(new int[] {27, 170}, "US-ASCII"), + Big5, //(28), + GB2312, + GB18030, //(29, "GB2312", "EUC_CN", "GBK"), + EUC_KR, //(30, "EUC-KR"); UTF16LE, - UTF8, //(26, "UTF-8"), - ASCII, //(new int[] {27, 170}, "US-ASCII"), - Big5, //(28), - GB18030, //(29, "GB2312", "EUC_CN", "GBK"), - EUC_KR, //(30, "EUC-KR"); - // Unknown, - // Binary, + UTF32BE, + UTF32LE, + Binary, + Unknown, } impl CharacterSet { - pub fn get_eci_value(&self) -> u32 { - match self { - CharacterSet::Cp437 => 0, - CharacterSet::ISO8859_1 => 1, - CharacterSet::ISO8859_2 => 4, - CharacterSet::ISO8859_3 => 5, - CharacterSet::ISO8859_4 => 6, - CharacterSet::ISO8859_5 => 7, - // CharacterSetECI::ISO8859_6 => 8, - CharacterSet::ISO8859_7 => 9, - // CharacterSetECI::ISO8859_8 => 10, - CharacterSet::ISO8859_9 => 11, - // CharacterSetECI::ISO8859_10 => 12, - // CharacterSetECI::ISO8859_11 => 13, - CharacterSet::ISO8859_13 => 15, - // CharacterSetECI::ISO8859_14 => 16, - CharacterSet::ISO8859_15 => 17, - CharacterSet::ISO8859_16 => 18, - CharacterSet::SJIS => 20, - CharacterSet::Cp1250 => 21, - CharacterSet::Cp1251 => 22, - CharacterSet::Cp1252 => 23, - CharacterSet::Cp1256 => 24, - CharacterSet::UnicodeBigUnmarked => 25, - CharacterSet::UTF16LE => 100025, - CharacterSet::UTF8 => 26, - CharacterSet::ASCII => 27, - CharacterSet::Big5 => 28, - CharacterSet::GB18030 => 29, - CharacterSet::EUC_KR => 30, - } - } + // pub fn get_eci_value(&self) -> u32 { + // match self { + // CharacterSet::Cp437 => 0, + // CharacterSet::ISO8859_1 => 1, + // CharacterSet::ISO8859_2 => 4, + // CharacterSet::ISO8859_3 => 5, + // CharacterSet::ISO8859_4 => 6, + // CharacterSet::ISO8859_5 => 7, + // // CharacterSetECI::ISO8859_6 => 8, + // CharacterSet::ISO8859_7 => 9, + // // CharacterSetECI::ISO8859_8 => 10, + // CharacterSet::ISO8859_9 => 11, + // // CharacterSetECI::ISO8859_10 => 12, + // // CharacterSetECI::ISO8859_11 => 13, + // CharacterSet::ISO8859_13 => 15, + // // CharacterSetECI::ISO8859_14 => 16, + // CharacterSet::ISO8859_15 => 17, + // CharacterSet::ISO8859_16 => 18, + // CharacterSet::Shift_JIS => 20, + // CharacterSet::Cp1250 => 21, + // CharacterSet::Cp1251 => 22, + // CharacterSet::Cp1252 => 23, + // CharacterSet::Cp1256 => 24, + // CharacterSet::UTF16BE => 25, + // CharacterSet::UTF8 => 26, + // CharacterSet::ASCII => 27, + // CharacterSet::Big5 => 28, + // CharacterSet::GB2312 => 29, + // CharacterSet::GB18030 => 32, + // CharacterSet::EUC_KR => 30, + // CharacterSet::UTF16LE => 33, + // CharacterSet::UTF32BE => 34, + // CharacterSet::UTF32LE => 35, + // CharacterSet::Binary => 899, + // _=>1000, + // } + // } fn get_base_encoder(&self) -> EncodingRef { let name = match self { @@ -101,28 +109,33 @@ impl CharacterSet { CharacterSet::ISO8859_3 => "ISO-8859-3", CharacterSet::ISO8859_4 => "ISO-8859-4", CharacterSet::ISO8859_5 => "ISO-8859-5", - // CharacterSetECI::ISO8859_6 => "ISO-8859-6", + CharacterSet::ISO8859_6 => "ISO-8859-6", CharacterSet::ISO8859_7 => "ISO-8859-7", - // CharacterSetECI::ISO8859_8 => "ISO-8859-8", + CharacterSet::ISO8859_8 => "ISO-8859-8", CharacterSet::ISO8859_9 => "ISO-8859-9", - // CharacterSetECI::ISO8859_10 => "ISO-8859-10", - // CharacterSetECI::ISO8859_11 => "ISO-8859-11", + CharacterSet::ISO8859_10 => "ISO-8859-10", + CharacterSet::ISO8859_11 => "ISO-8859-11", CharacterSet::ISO8859_13 => "ISO-8859-13", - // CharacterSetECI::ISO8859_14 => "ISO-8859-14", + CharacterSet::ISO8859_14 => "ISO-8859-14", CharacterSet::ISO8859_15 => "ISO-8859-15", CharacterSet::ISO8859_16 => "ISO-8859-16", - CharacterSet::SJIS => "shift_jis", + CharacterSet::Shift_JIS => "shift_jis", CharacterSet::Cp1250 => "windows-1250", CharacterSet::Cp1251 => "windows-1251", CharacterSet::Cp1252 => "windows-1252", CharacterSet::Cp1256 => "windows-1256", - CharacterSet::UnicodeBigUnmarked => "UTF-16BE", + CharacterSet::UTF16BE => "UTF-16BE", CharacterSet::UTF16LE => "UTF-16LE", CharacterSet::UTF8 => "UTF-8", CharacterSet::ASCII => "US-ASCII", CharacterSet::Big5 => "Big5", - CharacterSet::GB18030 => "GB2312", + CharacterSet::GB18030 => "GB18030", + CharacterSet::GB2312 => "GB2312", CharacterSet::EUC_KR => "EUC-KR", + CharacterSet::UTF32BE => "utf-32be", + CharacterSet::UTF32LE => "utf-32le", + CharacterSet::Binary => "binary", + CharacterSet::Unknown => "unknown", }; encoding::label::encoding_from_whatwg_label(name).unwrap() } @@ -135,28 +148,33 @@ impl CharacterSet { CharacterSet::ISO8859_3 => "iso-8859-3", CharacterSet::ISO8859_4 => "iso-8859-4", CharacterSet::ISO8859_5 => "iso-8859-5", - // CharacterSetECI::ISO8859_6 => "ISO-8859-6", + CharacterSet::ISO8859_6 => "ISO-8859-6", CharacterSet::ISO8859_7 => "iso-8859-7", - // CharacterSetECI::ISO8859_8 => "ISO-8859-8", + CharacterSet::ISO8859_8 => "ISO-8859-8", CharacterSet::ISO8859_9 => "iso-8859-9", - // CharacterSetECI::ISO8859_10 => "ISO-8859-10", - // CharacterSetECI::ISO8859_11 => "ISO-8859-11", + CharacterSet::ISO8859_10 => "ISO-8859-10", + CharacterSet::ISO8859_11 => "ISO-8859-11", CharacterSet::ISO8859_13 => "iso-8859-13", - // CharacterSetECI::ISO8859_14 => "ISO-8859-14", + CharacterSet::ISO8859_14 => "ISO-8859-14", CharacterSet::ISO8859_15 => "iso-8859-15", CharacterSet::ISO8859_16 => "iso-8859-16", - CharacterSet::SJIS => "shift_jis", + CharacterSet::Shift_JIS => "shift_jis", CharacterSet::Cp1250 => "windows-1250", CharacterSet::Cp1251 => "windows-1251", CharacterSet::Cp1252 => "windows-1252", CharacterSet::Cp1256 => "windows-1256", - CharacterSet::UnicodeBigUnmarked => "utf-16be", + CharacterSet::UTF16BE => "utf-16be", CharacterSet::UTF16LE => "utf-16le", CharacterSet::UTF8 => "utf-8", CharacterSet::ASCII => "us-ascii", CharacterSet::Big5 => "big5", - CharacterSet::GB18030 => "gb2312", + CharacterSet::GB18030 => "gb18030", + CharacterSet::GB2312 => "gb2312", CharacterSet::EUC_KR => "euc-kr", + CharacterSet::UTF32BE => "utf-32be", + CharacterSet::UTF32LE => "utf-32le", + CharacterSet::Binary => "binary", + CharacterSet::Unknown => "unknown", } } @@ -203,44 +221,49 @@ impl CharacterSet { // } // } - /** - * @param value character set ECI value - * @return {@code CharacterSetECI} representing ECI of given value, or null if it is legal but - * unsupported - * @throws FormatException if ECI value is invalid - */ - pub fn get_character_set_by_eci(value: u32) -> Result { - match value { - 0 | 2 => Ok(CharacterSet::Cp437), - 1 | 3 => Ok(CharacterSet::ISO8859_1), - 4 => Ok(CharacterSet::ISO8859_2), - 5 => Ok(CharacterSet::ISO8859_3), - 6 => Ok(CharacterSet::ISO8859_4), - 7 => Ok(CharacterSet::ISO8859_5), - // 8 => Ok(CharacterSetECI::ISO8859_6), - 9 => Ok(CharacterSet::ISO8859_7), - // 10 => Ok(CharacterSetECI::ISO8859_8), - 11 => Ok(CharacterSet::ISO8859_9), - // 12 => Ok(CharacterSetECI::ISO8859_10), - // 13 => Ok(CharacterSetECI::ISO8859_11), - 15 => Ok(CharacterSet::ISO8859_13), - // 16 => Ok(CharacterSetECI::ISO8859_14), - 17 => Ok(CharacterSet::ISO8859_15), - 18 => Ok(CharacterSet::ISO8859_16), - 20 => Ok(CharacterSet::SJIS), - 21 => Ok(CharacterSet::Cp1250), - 22 => Ok(CharacterSet::Cp1251), - 23 => Ok(CharacterSet::Cp1252), - 24 => Ok(CharacterSet::Cp1256), - 25 => Ok(CharacterSet::UnicodeBigUnmarked), - 26 => Ok(CharacterSet::UTF8), - 27 | 170 => Ok(CharacterSet::ASCII), - 28 => Ok(CharacterSet::Big5), - 29 => Ok(CharacterSet::GB18030), - 30 => Ok(CharacterSet::EUC_KR), - _ => Err(Exceptions::not_found_with("Bad ECI Value")), - } - } + // /** + // * @param value character set ECI value + // * @return {@code CharacterSetECI} representing ECI of given value, or null if it is legal but + // * unsupported + // * @throws FormatException if ECI value is invalid + // */ + // pub fn get_character_set_by_eci(value: u32) -> Result { + // match value { + // 0 | 2 => Ok(CharacterSet::Cp437), + // 1 | 3 => Ok(CharacterSet::ISO8859_1), + // 4 => Ok(CharacterSet::ISO8859_2), + // 5 => Ok(CharacterSet::ISO8859_3), + // 6 => Ok(CharacterSet::ISO8859_4), + // 7 => Ok(CharacterSet::ISO8859_5), + // // 8 => Ok(CharacterSetECI::ISO8859_6), + // 9 => Ok(CharacterSet::ISO8859_7), + // // 10 => Ok(CharacterSetECI::ISO8859_8), + // 11 => Ok(CharacterSet::ISO8859_9), + // // 12 => Ok(CharacterSetECI::ISO8859_10), + // // 13 => Ok(CharacterSetECI::ISO8859_11), + // 15 => Ok(CharacterSet::ISO8859_13), + // // 16 => Ok(CharacterSetECI::ISO8859_14), + // 17 => Ok(CharacterSet::ISO8859_15), + // 18 => Ok(CharacterSet::ISO8859_16), + // 20 => Ok(CharacterSet::Shift_JIS), + // 21 => Ok(CharacterSet::Cp1250), + // 22 => Ok(CharacterSet::Cp1251), + // 23 => Ok(CharacterSet::Cp1252), + // 24 => Ok(CharacterSet::Cp1256), + // 25 => Ok(CharacterSet::UTF16BE), + // 26 => Ok(CharacterSet::UTF8), + // 27 => Ok(CharacterSet::ASCII), + // 28 => Ok(CharacterSet::Big5), + // 32 => Ok(CharacterSet::GB18030), + // 29 => Ok(CharacterSet::GB2312), + // 30 => Ok(CharacterSet::EUC_KR), + // 33 => Ok(CharacterSet::UTF16LE), + // 34 => Ok(CharacterSet::UTF32BE), + // 35 => Ok(CharacterSet::UTF32LE), + // 899 => Ok(CharacterSet::Binary), + // _ => Err(Exceptions::not_found_with("Bad ECI Value")), + // } + // } /** * @param name character set ECI encoding name @@ -255,35 +278,47 @@ impl CharacterSet { "iso-8859-3" => Some(CharacterSet::ISO8859_3), "iso-8859-4" => Some(CharacterSet::ISO8859_4), "iso-8859-5" => Some(CharacterSet::ISO8859_5), - // "ISO-8859-6" => Some(CharacterSetECI::ISO8859_6), + "ISO-8859-6" => Some(CharacterSet::ISO8859_6), "iso-8859-7" => Some(CharacterSet::ISO8859_7), - // "ISO-8859-8" => Some(CharacterSetECI::ISO8859_8), + "ISO-8859-8" => Some(CharacterSet::ISO8859_8), "iso-8859-9" => Some(CharacterSet::ISO8859_9), - // "ISO-8859-10" => Some(CharacterSetECI::ISO8859_10), - // "ISO-8859-11" => Some(CharacterSetECI::ISO8859_11), + "ISO-8859-10" => Some(CharacterSet::ISO8859_10), + "ISO-8859-11" => Some(CharacterSet::ISO8859_11), "iso-8859-13" => Some(CharacterSet::ISO8859_13), - // "ISO-8859-14" => Some(CharacterSetECI::ISO8859_14), + "ISO-8859-14" => Some(CharacterSet::ISO8859_14), "iso-8859-15" => Some(CharacterSet::ISO8859_15), "iso-8859-16" => Some(CharacterSet::ISO8859_16), - "shift_jis" => Some(CharacterSet::SJIS), + "shift_jis" => Some(CharacterSet::Shift_JIS), "windows-1250" => Some(CharacterSet::Cp1250), "windows-1251" => Some(CharacterSet::Cp1251), "windows-1252" => Some(CharacterSet::Cp1252), "windows-1256" => Some(CharacterSet::Cp1256), - "utf-16be" => Some(CharacterSet::UnicodeBigUnmarked), + "utf-16be" => Some(CharacterSet::UTF16BE), "utf-8" | "utf8" => Some(CharacterSet::UTF8), "us-ascii" => Some(CharacterSet::ASCII), "big5" => Some(CharacterSet::Big5), - "gb2312" => Some(CharacterSet::GB18030), + "gb2312" => Some(CharacterSet::GB2312), + "gb18030" => Some(CharacterSet::GB18030), "euc-kr" => Some(CharacterSet::EUC_KR), + "utf-32be"=>Some(CharacterSet::UTF32BE) , + "utf-32le"=>Some(CharacterSet::UTF32LE) , + "binary"=>Some(CharacterSet::Binary) , + "unknown" => Some(CharacterSet::Unknown), _ => None, } } pub fn encode(&self, input: &str) -> Result> { + if self == &CharacterSet::Cp437 { + use codepage_437::ToCp437; + use codepage_437::CP437_CONTROL; + + input.to_cp437(&CP437_CONTROL).map(|data| data.to_vec()).map_err(|e| Exceptions::format_with(format!("{e:?}"))) + }else { self.get_base_encoder() .encode(input, encoding::EncoderTrap::Strict) .map_err(|e| Exceptions::format_with(e.to_string())) + } } pub fn encode_replace(&self, input: &str) -> Result> { diff --git a/src/common/eci.rs b/src/common/eci.rs new file mode 100644 index 0000000..e4918d6 --- /dev/null +++ b/src/common/eci.rs @@ -0,0 +1,181 @@ +use std::fmt::Display; + +use super::CharacterSet; + +#[derive(Copy,Clone,Debug,PartialEq, Eq)] +pub enum Eci { + Unknown = -1, + Cp437 = 2, // obsolete + ISO8859_1 = 3, + ISO8859_2 = 4, + ISO8859_3 = 5, + ISO8859_4 = 6, + ISO8859_5 = 7, + ISO8859_6 = 8, + ISO8859_7 = 9, + ISO8859_8 = 10, + ISO8859_9 = 11, + ISO8859_10 = 12, + ISO8859_11 = 13, + ISO8859_13 = 15, + ISO8859_14 = 16, + ISO8859_15 = 17, + ISO8859_16 = 18, + Shift_JIS = 20, + Cp1250 = 21, + Cp1251 = 22, + Cp1252 = 23, + Cp1256 = 24, + UTF16BE = 25, + UTF8 = 26, + ASCII = 27, + Big5 = 28, + GB2312 = 29, + EUC_KR = 30, + GB18030 = 32, + UTF16LE = 33, + UTF32BE = 34, + UTF32LE = 35, + ISO646_Inv = 170, + Binary = 899, +} + +impl Eci { + pub fn can_encode(self) -> bool { + (self as i32) >= 899 + } +} + +impl From for Eci { + fn from(value: u32) -> Self { + (value as i32).into() + } +} + +impl From for Eci { + fn from(value: i32) -> Self { + match value { + 0 | 2 => Eci::Cp437, + 1 | 3 => Eci::ISO8859_1, + 4 => Eci::ISO8859_2, + 5 => Eci::ISO8859_3, + 6 => Eci::ISO8859_4, + 7 => Eci::ISO8859_5, + 8 => Eci::ISO8859_6, + 9 => Eci::ISO8859_7, + 10 => Eci::ISO8859_8, + 11 => Eci::ISO8859_9, + 12 => Eci::ISO8859_10, + 13 => Eci::ISO8859_11, + 15 => Eci::ISO8859_13, + 16 => Eci::ISO8859_14, + 17 => Eci::ISO8859_15, + 18 => Eci::ISO8859_16, + 20 => Eci::Shift_JIS, + 21 => Eci::Cp1250, + 22 => Eci::Cp1251, + 23 => Eci::Cp1252, + 24 => Eci::Cp1256, + 25 => Eci::UTF16BE, + 26 => Eci::UTF8, + 27 => Eci::ASCII, + 28 => Eci::Big5, + 29 => Eci::GB18030, + 30 => Eci::EUC_KR, + 32 => Eci::GB18030, + 33 => Eci::UTF16LE, + 34 => Eci::UTF32BE, + 35 => Eci::UTF32LE, + 170 => Eci::ASCII, + 898 => Eci::Binary, + _ => Eci::Unknown, + } + } +} + +impl From for Eci { + fn from(value: CharacterSet) -> Self { + match value { + CharacterSet::Cp437 => Eci::Cp437, + CharacterSet::ISO8859_1 => Eci::ISO8859_1, + CharacterSet::ISO8859_2 => Eci::ISO8859_2, + CharacterSet::ISO8859_3 => Eci::ISO8859_3, + CharacterSet::ISO8859_4 => Eci::ISO8859_4, + CharacterSet::ISO8859_5 => Eci::ISO8859_5, + CharacterSet::ISO8859_7 => Eci::ISO8859_7, + CharacterSet::ISO8859_9 => Eci::ISO8859_9, + CharacterSet::ISO8859_13 => Eci::ISO8859_13, + CharacterSet::ISO8859_15 => Eci::ISO8859_15, + CharacterSet::ISO8859_16 => Eci::ISO8859_16, + CharacterSet::Shift_JIS => Eci::Shift_JIS, + CharacterSet::Cp1250 => Eci::Cp1250, + CharacterSet::Cp1251 => Eci::Cp1251, + CharacterSet::Cp1252 => Eci::Cp1252, + CharacterSet::Cp1256 => Eci::Cp1256, + CharacterSet::UTF16BE => Eci::UTF16BE, + CharacterSet::UTF8 => Eci::UTF8, + CharacterSet::ASCII => Eci::ASCII, + CharacterSet::Big5 => Eci::Big5, + CharacterSet::GB2312 => Eci::GB2312, + CharacterSet::GB18030 => Eci::GB18030, + CharacterSet::EUC_KR => Eci::EUC_KR, + CharacterSet::UTF16LE => Eci::UTF16LE, + CharacterSet::UTF32BE => Eci::UTF32BE, + CharacterSet::UTF32LE => Eci::UTF32LE, + CharacterSet::Binary => Eci::Binary, + CharacterSet::ISO8859_6 => Eci::ISO8859_6, + CharacterSet::ISO8859_8 => Eci::ISO8859_8, + CharacterSet::ISO8859_10 => Eci::ISO8859_10, + CharacterSet::ISO8859_11 =>Eci::ISO8859_11, + CharacterSet::ISO8859_14 => Eci::ISO8859_14, + _=>Eci::Unknown, + } + } +} + +impl From for CharacterSet { + fn from(value: Eci) -> Self { + match value { + Eci::Cp437 => CharacterSet::Cp437, + Eci::ISO8859_1 => CharacterSet::ISO8859_1, + Eci::ISO8859_2 => CharacterSet::ISO8859_2, + Eci::ISO8859_3 => CharacterSet::ISO8859_3, + Eci::ISO8859_4 => CharacterSet::ISO8859_4, + Eci::ISO8859_5 => CharacterSet::ISO8859_5, + Eci::ISO8859_6 => CharacterSet::ISO8859_6, + Eci::ISO8859_7 => CharacterSet::ISO8859_7, + Eci::ISO8859_8 => CharacterSet::ISO8859_8, + Eci::ISO8859_9 => CharacterSet::ISO8859_9, + Eci::ISO8859_10 => CharacterSet::ISO8859_10, + Eci::ISO8859_11 => CharacterSet::ISO8859_11, + Eci::ISO8859_13 => CharacterSet::ISO8859_13, + Eci::ISO8859_14 => CharacterSet::ISO8859_14, + Eci::ISO8859_15 => CharacterSet::ISO8859_15, + Eci::ISO8859_16 => CharacterSet::ISO8859_16, + Eci::Shift_JIS => CharacterSet::Shift_JIS, + Eci::Cp1250 => CharacterSet::Cp1250, + Eci::Cp1251 => CharacterSet::Cp1251, + Eci::Cp1252 => CharacterSet::Cp1252, + Eci::Cp1256 => CharacterSet::Cp1256, + Eci::UTF16BE => CharacterSet::UTF16BE, + Eci::UTF8 => CharacterSet::UTF8, + Eci::ASCII => CharacterSet::ASCII, + Eci::Big5 => CharacterSet::Big5, + Eci::GB2312 => CharacterSet::GB2312, + Eci::EUC_KR => CharacterSet::EUC_KR, + Eci::GB18030 => CharacterSet::GB18030, + Eci::UTF16LE => CharacterSet::UTF16LE, + Eci::UTF32BE => CharacterSet::UTF32BE, + Eci::UTF32LE => CharacterSet::UTF32LE, + Eci::ISO646_Inv => CharacterSet::ASCII, + Eci::Binary => CharacterSet::Binary, + _ => CharacterSet::Unknown, + } + } +} + +impl Display for Eci { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{}", *self as i32) + } +} \ No newline at end of file diff --git a/src/common/eci_encoder_set.rs b/src/common/eci_encoder_set.rs index 003cebe..2ac723b 100644 --- a/src/common/eci_encoder_set.rs +++ b/src/common/eci_encoder_set.rs @@ -16,7 +16,7 @@ use unicode_segmentation::UnicodeSegmentation; -use super::CharacterSet; +use super::{CharacterSet, Eci}; use once_cell::sync::Lazy; @@ -100,7 +100,7 @@ impl ECIEncoderSet { neededEncoders.push(CharacterSet::ISO8859_1); let mut needUnicodeEncoder = if let Some(pc) = priorityCharset { //pc.name().starts_with("UTF") || pc.name().starts_with("utf") - pc == CharacterSet::UTF8 || pc == CharacterSet::UnicodeBigUnmarked + pc == CharacterSet::UTF8 || pc == CharacterSet::UTF16BE } else { false }; @@ -157,7 +157,7 @@ impl ECIEncoderSet { } encoders.push(CharacterSet::UTF8); - encoders.push(CharacterSet::UnicodeBigUnmarked); + encoders.push(CharacterSet::UTF16BE); } //Compute priorityEncoderIndex by looking up priorityCharset in encoders @@ -206,8 +206,8 @@ impl ECIEncoderSet { } } - pub fn getECIValue(&self, encoderIndex: usize) -> u32 { - self.encoders[encoderIndex].get_eci_value() + pub fn get_eci(&self, encoderIndex: usize) -> Eci { + self.encoders[encoderIndex].into() // CharacterSetECI::getValue( // &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), // ) diff --git a/src/common/eci_input.rs b/src/common/eci_input.rs index 8e9e773..33b6f4b 100644 --- a/src/common/eci_input.rs +++ b/src/common/eci_input.rs @@ -20,6 +20,8 @@ use std::fmt::Display; use crate::common::Result; +use super::Eci; + /** * Interface to navigate a sequence of ECIs and bytes. * @@ -105,6 +107,6 @@ pub trait ECIInput: Display { * @throws IllegalArgumentException * if the value at the {@code index} argument is not an ECI (@see #isECI) */ - fn getECIValue(&self, index: usize) -> Result; + fn getECIValue(&self, index: usize) -> Result; fn haveNCharacters(&self, index: usize, n: usize) -> Result; } diff --git a/src/common/eci_string_builder.rs b/src/common/eci_string_builder.rs index 6ee3dae..4378d21 100644 --- a/src/common/eci_string_builder.rs +++ b/src/common/eci_string_builder.rs @@ -25,7 +25,7 @@ use std::fmt; use crate::common::Result; -use super::CharacterSet; +use super::{CharacterSet, Eci}; /** * Class that converts a sequence of ECIs and bytes into a string @@ -35,7 +35,7 @@ use super::CharacterSet; pub struct ECIStringBuilder { current_bytes: Vec, result: String, - current_charset: Option, //= StandardCharsets.ISO_8859_1; + current_charset: CharacterSet, //= StandardCharsets.ISO_8859_1; } impl ECIStringBuilder { @@ -43,14 +43,14 @@ impl ECIStringBuilder { Self { current_bytes: Vec::new(), result: String::new(), - current_charset: Some(CharacterSet::ISO8859_1), + current_charset: CharacterSet::ISO8859_1, } } pub fn with_capacity(initial_capacity: usize) -> Self { Self { current_bytes: Vec::with_capacity(initial_capacity), result: String::with_capacity(initial_capacity), - current_charset: Some(CharacterSet::ISO8859_1), + current_charset: CharacterSet::ISO8859_1, } } @@ -101,11 +101,10 @@ impl ECIStringBuilder { * @param value ECI value to append, as an int * @throws FormatException on invalid ECI value */ - pub fn appendECI(&mut self, value: u32) -> Result<()> { + pub fn appendECI(&mut self, eci: Eci) -> Result<()> { self.encodeCurrentBytesIfAny(); - self.current_charset = CharacterSet::get_character_set_by_eci(value).ok(); - + self.current_charset = eci.into(); //CharacterSet::get_character_set_by_eci(value).ok(); // if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { // // dbg!( @@ -126,8 +125,8 @@ impl ECIStringBuilder { /// /// This function can panic pub fn encodeCurrentBytesIfAny(&mut self) { - if let Some(encoder) = self.current_charset { - if encoder == CharacterSet::UTF8 { + if ![CharacterSet::Binary, CharacterSet::Unknown].contains(&self.current_charset) { + if self.current_charset == CharacterSet::UTF8 { if !self.current_bytes.is_empty() { self.result.push_str( &String::from_utf8(std::mem::take(&mut self.current_bytes)).unwrap(), @@ -137,7 +136,7 @@ impl ECIStringBuilder { } else if !self.current_bytes.is_empty() { let bytes = std::mem::take(&mut self.current_bytes); self.current_bytes.clear(); - let encoded_value = encoder.decode(&bytes).unwrap(); + let encoded_value = self.current_charset.decode(&bytes).unwrap(); self.result.push_str(&encoded_value); } } else { diff --git a/src/common/minimal_eci_input.rs b/src/common/minimal_eci_input.rs index fca820d..80619a3 100644 --- a/src/common/minimal_eci_input.rs +++ b/src/common/minimal_eci_input.rs @@ -21,7 +21,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::common::Result; use crate::Exceptions; -use super::{CharacterSet, ECIEncoderSet, ECIInput}; +use super::{CharacterSet, ECIEncoderSet, ECIInput, Eci}; //* approximated (latch + 2 codewords) pub const COST_PER_ECI: usize = 3; @@ -154,7 +154,7 @@ impl ECIInput for MinimalECIInput { * @throws IllegalArgumentException * if the value at the {@code index} argument is not an ECI (@see #isECI) */ - fn getECIValue(&self, index: usize) -> Result { + fn getECIValue(&self, index: usize) -> Result { if index >= self.length() { return Err(Exceptions::INDEX_OUT_OF_BOUNDS); } @@ -163,7 +163,7 @@ impl ECIInput for MinimalECIInput { "value at {index} is not an ECI but a character" ))); } - Ok((self.bytes[index] as u32 - 256) as i32) + Ok(Eci::from(self.bytes[index] as u32 - 256)) } fn haveNCharacters(&self, index: usize, n: usize) -> Result { @@ -383,7 +383,7 @@ impl MinimalECIInput { // 0..0, // [256 as u16 + encoderSet.getECIValue(c.encoderIndex) as u16], // ); - intsAL.insert(0, 256_u16 + encoderSet.getECIValue(c.encoderIndex) as u16); + intsAL.insert(0, 256_u16 + encoderSet.get_eci(c.encoderIndex) as u16); } current = c.previous.clone(); } diff --git a/src/common/mod.rs b/src/common/mod.rs index 257e8a6..6a829d1 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -88,8 +88,8 @@ pub use grid_sampler::*; mod default_grid_sampler; pub use default_grid_sampler::*; -mod character_set_eci; -pub use character_set_eci::*; +mod character_set; +pub use character_set::*; mod eci_string_builder; pub use eci_string_builder::*; @@ -106,6 +106,9 @@ pub use global_histogram_binarizer::*; mod hybrid_binarizer; pub use hybrid_binarizer::*; +mod eci; +pub use eci::*; + #[cfg(feature = "otsu_level")] mod otsu_level_binarizer; #[cfg(feature = "otsu_level")] diff --git a/src/common/string_utils.rs b/src/common/string_utils.rs index 911b15b..f16c762 100644 --- a/src/common/string_utils.rs +++ b/src/common/string_utils.rs @@ -48,7 +48,7 @@ const ASSUME_SHIFT_JIS: bool = false; // static SHIFT_JIS: &'static str = "SJIS"; // static GB2312: &'static str = "GB2312"; -pub static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::SJIS; +pub static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::Shift_JIS; // private static final boolean ASSUME_SHIFT_JIS = // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || @@ -64,7 +64,7 @@ impl StringUtils { */ pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> { let c = StringUtils::guessCharset(bytes, hints)?; - if c == CharacterSet::SJIS { + if c == CharacterSet::Shift_JIS { Some("SJIS") } else if c == CharacterSet::UTF8 { Some("UTF8") @@ -101,7 +101,7 @@ impl StringUtils { && ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE)) { if bytes[0] == 0xFE && bytes[1] == 0xFF { - return Some(CharacterSet::UnicodeBigUnmarked); + return Some(CharacterSet::UTF16BE); } else { return Some(CharacterSet::UTF16LE); } @@ -229,7 +229,7 @@ impl StringUtils { || sjis_max_katakana_word_length >= 3 || sjis_max_double_bytes_word_length >= 3) { - return Some(CharacterSet::SJIS); //encoding::label::encoding_from_whatwg_label("SJIS"); + return Some(CharacterSet::Shift_JIS); //encoding::label::encoding_from_whatwg_label("SJIS"); } // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is: // - If we saw @@ -240,7 +240,7 @@ impl StringUtils { return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2) || iso_high_other * 10 >= length { - Some(CharacterSet::SJIS) + Some(CharacterSet::Shift_JIS) } else { Some(CharacterSet::ISO8859_1) }; @@ -251,7 +251,7 @@ impl StringUtils { return Some(CharacterSet::ISO8859_1); } if can_be_shift_jis { - return Some(CharacterSet::SJIS); + return Some(CharacterSet::Shift_JIS); } if can_be_utf8 { return Some(CharacterSet::UTF8); diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index 90922b3..b113023 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Result}, + common::{BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Result, Eci}, Exceptions, }; @@ -739,19 +739,19 @@ fn decodeBase256Segment( fn decodeECISegment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result { let firstByte = bits.readBits(8)?; if firstByte <= 127 { - result.appendECI(firstByte - 1)?; + result.appendECI(Eci::from(firstByte - 1))?; return Ok(true); } let secondByte = bits.readBits(8)?; if firstByte <= 191 { - result.appendECI((firstByte - 128) * 254 + 127 + secondByte - 1)?; + result.appendECI(Eci::from((firstByte - 128) * 254 + 127 + secondByte - 1))?; return Ok((firstByte - 128) * 254 + 127 + secondByte - 1 > 900); } let thirdByte = bits.readBits(8)?; - result.appendECI((firstByte - 192) * 64516 + 16383 + (secondByte - 1) * 254 + thirdByte - 1)?; + result.appendECI(Eci::from((firstByte - 192) * 64516 + 16383 + (secondByte - 1) * 254 + thirdByte - 1))?; Ok((firstByte - 192) * 64516 + 16383 + (secondByte - 1) * 254 + thirdByte - 1 > 900) } diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index e35ee30..4bf0386 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{CharacterSet, ECIInput, MinimalECIInput, Result}, + common::{CharacterSet, ECIInput, MinimalECIInput, Result, Eci}, Exceptions, }; @@ -1441,7 +1441,7 @@ impl Input { fn isFNC1(&self, index: usize) -> Result { self.internal.isFNC1(index) } - fn getECIValue(&self, index: usize) -> Result { + fn getECIValue(&self, index: usize) -> Result { self.internal.getECIValue(index) } } diff --git a/src/pdf417/decoder/decoded_bit_stream_parser.rs b/src/pdf417/decoder/decoded_bit_stream_parser.rs index 566e74a..83fa2c7 100644 --- a/src/pdf417/decoder/decoded_bit_stream_parser.rs +++ b/src/pdf417/decoder/decoded_bit_stream_parser.rs @@ -19,7 +19,7 @@ use num::{self, bigint::ToBigUint, BigUint}; use std::rc::Rc; use crate::{ - common::{DecoderRXingResult, ECIStringBuilder, Result}, + common::{DecoderRXingResult, ECIStringBuilder, Result, Eci}, pdf417::PDF417RXingResultMetadata, Exceptions, }; @@ -128,7 +128,7 @@ pub fn decode(codewords: &[u32], ecLevel: &str) -> Result { codeIndex = numericCompaction(codewords, codeIndex, &mut result)? } ECI_CHARSET => { - result.appendECI(codewords[codeIndex])?; + result.appendECI(Eci::from(codewords[codeIndex]))?; codeIndex += 1; } ECI_GENERAL_PURPOSE => @@ -387,7 +387,7 @@ fn textCompaction( subMode, ) .ok_or(Exceptions::ILLEGAL_STATE)?; - result.appendECI(codewords[codeIndex])?; + result.appendECI(Eci::from(codewords[codeIndex]))?; codeIndex += 1; textCompactionData = vec![0; (codewords[0] as usize - codeIndex) * 2]; byteCompactionData = vec![0; (codewords[0] as usize - codeIndex) * 2]; @@ -616,7 +616,7 @@ fn byteCompaction( //handle leading ECIs while codeIndex < codewords[0] as usize && codewords[codeIndex] == ECI_CHARSET { codeIndex += 1; - result.appendECI(codewords[codeIndex])?; + result.appendECI(Eci::from(codewords[codeIndex]))?; codeIndex += 1; } @@ -654,7 +654,7 @@ fn byteCompaction( if code < TEXT_COMPACTION_MODE_LATCH { result.append_byte(code as u8); } else if code == ECI_CHARSET { - result.appendECI(codewords[codeIndex])?; + result.appendECI(Eci::from(codewords[codeIndex]))?; codeIndex += 1; } else { codeIndex -= 1; diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index af78ccd..f76d6d4 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -21,7 +21,7 @@ use std::{any::TypeId, fmt::Display, str::FromStr}; use crate::{ - common::{CharacterSet, ECIInput, MinimalECIInput, Result}, + common::{CharacterSet, ECIInput, MinimalECIInput, Result, Eci}, Exceptions, }; @@ -205,7 +205,8 @@ pub fn encodeHighLevel( // } encodingECI( - CharacterSet::get_eci_value(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, + Eci::from(encoding.ok_or(Exceptions::ILLEGAL_STATE)?), + //CharacterSet::get_eci_value(&encoding.ok_or(Exceptions::ILLEGAL_STATE)?) as i32, &mut sb, )?; } @@ -797,17 +798,17 @@ fn determineConsecutiveBinaryCount( Ok(idx as u32 - startpos) } -fn encodingECI(eci: i32, sb: &mut String) -> Result<()> { - if (0..900).contains(&eci) { +fn encodingECI(eci: Eci, sb: &mut String) -> Result<()> { + if (0..900).contains(&(eci as i32)) { sb.push(char::from_u32(ECI_CHARSET).ok_or(Exceptions::PARSE)?); sb.push(char::from_u32(eci as u32).ok_or(Exceptions::PARSE)?); - } else if eci < 810900 { + } else if (eci as i32 )< 810900 { sb.push(char::from_u32(ECI_GENERAL_PURPOSE).ok_or(Exceptions::PARSE)?); - sb.push(char::from_u32((eci / 900 - 1) as u32).ok_or(Exceptions::PARSE)?); - sb.push(char::from_u32((eci % 900) as u32).ok_or(Exceptions::PARSE)?); - } else if eci < 811800 { + sb.push(char::from_u32(((eci as i32) / 900 - 1) as u32).ok_or(Exceptions::PARSE)?); + sb.push(char::from_u32(((eci as i32) % 900) as u32).ok_or(Exceptions::PARSE)?); + } else if (eci as i32) < 811800 { sb.push(char::from_u32(ECI_USER_DEFINED).ok_or(Exceptions::PARSE)?); - sb.push(char::from_u32((810900 - eci) as u32).ok_or(Exceptions::PARSE)?); + sb.push(char::from_u32((810900 - (eci as i32)) as u32).ok_or(Exceptions::PARSE)?); } else { return Err(Exceptions::writer_with(format!( "ECI number not in valid range from 0..811799, but was {eci}" @@ -838,8 +839,8 @@ impl ECIInput for NoECIInput { Ok(false) } - fn getECIValue(&self, _index: usize) -> Result { - Ok(-1) + fn getECIValue(&self, _index: usize) -> Result { + Ok(Eci::Unknown) } fn haveNCharacters(&self, index: usize, n: usize) -> Result { diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index b0e45b4..3a76efb 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, CharacterSet, DecoderRXingResult, Result, StringUtils}, + common::{BitSource, CharacterSet, DecoderRXingResult, Result, StringUtils, Eci}, DecodingHintDictionary, Exceptions, }; @@ -91,7 +91,7 @@ pub fn decode( Mode::ECI => { // Count doesn't apply to ECI let value = parseECIValue(&mut bits)?; - currentCharacterSetECI = CharacterSet::get_character_set_by_eci(value).ok(); + currentCharacterSetECI = CharacterSet::from(Eci::from(value)).into();//CharacterSet::get_character_set_by_eci(value).ok(); if currentCharacterSetECI.is_none() { return Err(Exceptions::format_with(format!( "Value of {value} not valid" @@ -249,7 +249,7 @@ fn decodeKanjiSegment( let encoder = { let _ = currentCharacterSetECI; let _ = hints; - CharacterSet::SJIS + CharacterSet::Shift_JIS }; #[cfg(feature = "allow_forced_iso_ied_18004_compliance")] @@ -262,7 +262,7 @@ fn decodeKanjiSegment( CharacterSet::ISO8859_1 } } else { - CharacterSet::SJIS + CharacterSet::Shift_JIS }; let encode_string = encoder diff --git a/src/qrcode/encoder/EncoderTestCase.rs b/src/qrcode/encoder/EncoderTestCase.rs index 96f4f44..e938636 100644 --- a/src/qrcode/encoder/EncoderTestCase.rs +++ b/src/qrcode/encoder/EncoderTestCase.rs @@ -28,7 +28,7 @@ use once_cell::sync::Lazy; use super::QRCode; -static SHIFT_JIS_CHARSET: Lazy = Lazy::new(|| CharacterSet::SJIS); +static SHIFT_JIS_CHARSET: Lazy = Lazy::new(|| CharacterSet::Shift_JIS); /** * @author satorux@google.com (Satoru Takabayashi) - creator diff --git a/src/qrcode/encoder/minimal_encoder.rs b/src/qrcode/encoder/minimal_encoder.rs index 45b5929..42a9cb4 100644 --- a/src/qrcode/encoder/minimal_encoder.rs +++ b/src/qrcode/encoder/minimal_encoder.rs @@ -1001,7 +1001,7 @@ impl RXingResultNode { )?; } if self.mode == Mode::ECI { - bits.appendBits(self.encoders.getECIValue(self.charsetEncoderIndex), 8)?; + bits.appendBits(self.encoders.get_eci(self.charsetEncoderIndex) as u32, 8)?; } else if self.characterLength > 0 { // append data qrcode_encoder::appendBytes( diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index 9eccc37..94e743b 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -24,7 +24,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ common::{ reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonEncoder}, - BitArray, CharacterSet, Result, + BitArray, CharacterSet, Result, Eci, }, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, EncodeHintType, EncodeHintValue, EncodingHintDictionary, Exceptions, @@ -32,7 +32,7 @@ use crate::{ use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; -static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::SJIS; +static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::Shift_JIS; // The original table is defined in the table 5 of JISX0510:2004 (p.19). const ALPHANUMERIC_TABLE: [i8; 96] = [ @@ -137,7 +137,7 @@ pub fn encode_with_hints( // Append ECI segment if applicable if mode == Mode::BYTE && has_encoding_hint { - appendECI(&encoding, &mut header_bits)?; + appendECI(encoding.into(), &mut header_bits)?; } // Append the FNC1 mode header for GS1 formatted data if applicable @@ -762,8 +762,8 @@ pub fn appendKanjiBytes(content: &str, bits: &mut BitArray) -> Result<()> { Ok(()) } -fn appendECI(eci: &CharacterSet, bits: &mut BitArray) -> Result<()> { +fn appendECI(eci: Eci, bits: &mut BitArray) -> Result<()> { bits.appendBits(Mode::ECI.getBits() as u32, 4)?; // This is correct for values up to 127, which is all we need now. - bits.appendBits(eci.get_eci_value(), 8) + bits.appendBits(eci as u32, 8) } From 69c0119c949e26a6dd60201b3fad58bdcc5b3a10 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 4 Mar 2023 14:17:56 -0600 Subject: [PATCH 09/10] clippy --fix && fmt --- benches/benchmarks.rs | 6 +- src/aztec/decoder.rs | 4 +- src/aztec/encoder/state.rs | 2 +- src/common/character_set.rs | 75 ++++++++++--------- src/common/eci.rs | 10 +-- .../decoder/decoded_bit_stream_parser.rs | 6 +- src/datamatrix/encoder/minimal_encoder.rs | 2 +- .../decoder/decoded_bit_stream_parser.rs | 2 +- .../encoder/pdf_417_high_level_encoder.rs | 13 +--- .../decoder/decoded_bit_stream_parser.rs | 4 +- src/qrcode/encoder/qrcode_encoder.rs | 8 +- 11 files changed, 64 insertions(+), 68 deletions(-) diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index d39b825..b5f9166 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -3,6 +3,7 @@ use rxing::aztec::AztecReader; use rxing::common::HybridBinarizer; use rxing::datamatrix::DataMatrixReader; use rxing::maxicode::MaxiCodeReader; +use rxing::multi::{GenericMultipleBarcodeReader, MultipleBarcodeReader}; use rxing::oned::rss::expanded::RSSExpandedReader; use rxing::oned::rss::RSS14Reader; use rxing::oned::{ @@ -11,10 +12,9 @@ use rxing::oned::{ }; use rxing::pdf417::PDF417Reader; use rxing::qrcode::QRCodeReader; +use rxing::MultiFormatReader; use rxing::{BinaryBitmap, BufferedImageLuminanceSource, Reader}; use std::path::Path; -use rxing::multi::{GenericMultipleBarcodeReader, MultipleBarcodeReader}; -use rxing::MultiFormatReader; fn get_image( path: impl AsRef, @@ -188,7 +188,7 @@ fn upce_benchmark(c: &mut Criterion) { fn multi_barcode_benchmark(c: &mut Criterion) { let mut image = get_image("test_resources/blackbox/multi-1/1.png"); c.bench_function("multi_barcode", |b| { - b.iter( || { + b.iter(|| { let mut reader = GenericMultipleBarcodeReader::new(MultiFormatReader::default()); let _res = reader.decode_multiple(&mut image); }); diff --git a/src/aztec/decoder.rs b/src/aztec/decoder.rs index e0d1dc6..8462973 100644 --- a/src/aztec/decoder.rs +++ b/src/aztec/decoder.rs @@ -19,7 +19,7 @@ use crate::{ reedsolomon::{ get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonDecoder, }, - BitMatrix, CharacterSet, DecoderRXingResult, DetectorRXingResult, Result, Eci, + BitMatrix, CharacterSet, DecoderRXingResult, DetectorRXingResult, Eci, Result, }, exceptions::Exceptions, }; @@ -182,7 +182,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result { eci = eci * 10 + (next_digit - 2); n -= 1; } - let charset_eci : Eci= eci.into(); + let charset_eci: Eci = eci.into(); if charset_eci == Eci::Unknown { return Err(Exceptions::format_with("Charset must exist")); } diff --git a/src/aztec/encoder/state.rs b/src/aztec/encoder/state.rs index 99b9772..2ba21f3 100644 --- a/src/aztec/encoder/state.rs +++ b/src/aztec/encoder/state.rs @@ -17,7 +17,7 @@ use std::fmt; use crate::{ - common::{BitArray, CharacterSet, Result, Eci}, + common::{BitArray, CharacterSet, Eci, Result}, exceptions::Exceptions, }; diff --git a/src/common/character_set.rs b/src/common/character_set.rs index 0bc6214..f3669bf 100644 --- a/src/common/character_set.rs +++ b/src/common/character_set.rs @@ -28,34 +28,34 @@ use crate::Exceptions; #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum CharacterSet { // Enum name is a Java encoding valid for java.lang and java.io - Cp437, //(new int[]{0,2}), - ISO8859_1, //(new int[]{1,3}, "ISO-8859-1"), - ISO8859_2, //(4, "ISO-8859-2"), - ISO8859_3, //(5, "ISO-8859-3"), - ISO8859_4, //(6, "ISO-8859-4"), - ISO8859_5, //(7, "ISO-8859-5"), - ISO8859_6, //(8, "ISO-8859-6"), - ISO8859_7, //(9, "ISO-8859-7"), - ISO8859_8, //(10, "ISO-8859-8"), - ISO8859_9, //(11, "ISO-8859-9"), - ISO8859_10, //(12, "ISO-8859-10"), - ISO8859_11, //(13, "ISO-8859-11"), + Cp437, //(new int[]{0,2}), + ISO8859_1, //(new int[]{1,3}, "ISO-8859-1"), + ISO8859_2, //(4, "ISO-8859-2"), + ISO8859_3, //(5, "ISO-8859-3"), + ISO8859_4, //(6, "ISO-8859-4"), + ISO8859_5, //(7, "ISO-8859-5"), + ISO8859_6, //(8, "ISO-8859-6"), + ISO8859_7, //(9, "ISO-8859-7"), + ISO8859_8, //(10, "ISO-8859-8"), + ISO8859_9, //(11, "ISO-8859-9"), + ISO8859_10, //(12, "ISO-8859-10"), + ISO8859_11, //(13, "ISO-8859-11"), ISO8859_13, //(15, "ISO-8859-13"), - ISO8859_14, //(16, "ISO-8859-14"), - ISO8859_15, //(17, "ISO-8859-15"), - ISO8859_16, //(18, "ISO-8859-16"), - Shift_JIS, //(20, "Shift_JIS"), - Cp1250, //(21, "windows-1250"), - Cp1251, //(22, "windows-1251"), - Cp1252, //(23, "windows-1252"), - Cp1256, //(24, "windows-1256"), - UTF16BE, //(25, "UTF-16BE", "UnicodeBig"), - UTF8, //(26, "UTF-8"), - ASCII, //(new int[] {27, 170}, "US-ASCII"), - Big5, //(28), + ISO8859_14, //(16, "ISO-8859-14"), + ISO8859_15, //(17, "ISO-8859-15"), + ISO8859_16, //(18, "ISO-8859-16"), + Shift_JIS, //(20, "Shift_JIS"), + Cp1250, //(21, "windows-1250"), + Cp1251, //(22, "windows-1251"), + Cp1252, //(23, "windows-1252"), + Cp1256, //(24, "windows-1256"), + UTF16BE, //(25, "UTF-16BE", "UnicodeBig"), + UTF8, //(26, "UTF-8"), + ASCII, //(new int[] {27, 170}, "US-ASCII"), + Big5, //(28), GB2312, - GB18030, //(29, "GB2312", "EUC_CN", "GBK"), - EUC_KR, //(30, "EUC-KR"); + GB18030, //(29, "GB2312", "EUC_CN", "GBK"), + EUC_KR, //(30, "EUC-KR"); UTF16LE, UTF32BE, UTF32LE, @@ -278,9 +278,9 @@ impl CharacterSet { "iso-8859-3" => Some(CharacterSet::ISO8859_3), "iso-8859-4" => Some(CharacterSet::ISO8859_4), "iso-8859-5" => Some(CharacterSet::ISO8859_5), - "ISO-8859-6" => Some(CharacterSet::ISO8859_6), + "iso-8859-6" => Some(CharacterSet::ISO8859_6), "iso-8859-7" => Some(CharacterSet::ISO8859_7), - "ISO-8859-8" => Some(CharacterSet::ISO8859_8), + "iso-8859-8" => Some(CharacterSet::ISO8859_8), "iso-8859-9" => Some(CharacterSet::ISO8859_9), "ISO-8859-10" => Some(CharacterSet::ISO8859_10), "ISO-8859-11" => Some(CharacterSet::ISO8859_11), @@ -300,9 +300,9 @@ impl CharacterSet { "gb2312" => Some(CharacterSet::GB2312), "gb18030" => Some(CharacterSet::GB18030), "euc-kr" => Some(CharacterSet::EUC_KR), - "utf-32be"=>Some(CharacterSet::UTF32BE) , - "utf-32le"=>Some(CharacterSet::UTF32LE) , - "binary"=>Some(CharacterSet::Binary) , + "utf-32be" => Some(CharacterSet::UTF32BE), + "utf-32le" => Some(CharacterSet::UTF32LE), + "binary" => Some(CharacterSet::Binary), "unknown" => Some(CharacterSet::Unknown), _ => None, } @@ -313,11 +313,14 @@ impl CharacterSet { use codepage_437::ToCp437; use codepage_437::CP437_CONTROL; - input.to_cp437(&CP437_CONTROL).map(|data| data.to_vec()).map_err(|e| Exceptions::format_with(format!("{e:?}"))) - }else { - self.get_base_encoder() - .encode(input, encoding::EncoderTrap::Strict) - .map_err(|e| Exceptions::format_with(e.to_string())) + input + .to_cp437(&CP437_CONTROL) + .map(|data| data.to_vec()) + .map_err(|e| Exceptions::format_with(format!("{e:?}"))) + } else { + self.get_base_encoder() + .encode(input, encoding::EncoderTrap::Strict) + .map_err(|e| Exceptions::format_with(e.to_string())) } } diff --git a/src/common/eci.rs b/src/common/eci.rs index e4918d6..da47109 100644 --- a/src/common/eci.rs +++ b/src/common/eci.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use super::CharacterSet; -#[derive(Copy,Clone,Debug,PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Eci { Unknown = -1, Cp437 = 2, // obsolete @@ -126,9 +126,9 @@ impl From for Eci { CharacterSet::ISO8859_6 => Eci::ISO8859_6, CharacterSet::ISO8859_8 => Eci::ISO8859_8, CharacterSet::ISO8859_10 => Eci::ISO8859_10, - CharacterSet::ISO8859_11 =>Eci::ISO8859_11, + CharacterSet::ISO8859_11 => Eci::ISO8859_11, CharacterSet::ISO8859_14 => Eci::ISO8859_14, - _=>Eci::Unknown, + _ => Eci::Unknown, } } } @@ -176,6 +176,6 @@ impl From for CharacterSet { impl Display for Eci { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f,"{}", *self as i32) + write!(f, "{}", *self as i32) } -} \ No newline at end of file +} diff --git a/src/datamatrix/decoder/decoded_bit_stream_parser.rs b/src/datamatrix/decoder/decoded_bit_stream_parser.rs index b113023..7f3bb88 100644 --- a/src/datamatrix/decoder/decoded_bit_stream_parser.rs +++ b/src/datamatrix/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Result, Eci}, + common::{BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Eci, Result}, Exceptions, }; @@ -751,7 +751,9 @@ fn decodeECISegment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Resu let thirdByte = bits.readBits(8)?; - result.appendECI(Eci::from((firstByte - 192) * 64516 + 16383 + (secondByte - 1) * 254 + thirdByte - 1))?; + result.appendECI(Eci::from( + (firstByte - 192) * 64516 + 16383 + (secondByte - 1) * 254 + thirdByte - 1, + ))?; Ok((firstByte - 192) * 64516 + 16383 + (secondByte - 1) * 254 + thirdByte - 1 > 900) } diff --git a/src/datamatrix/encoder/minimal_encoder.rs b/src/datamatrix/encoder/minimal_encoder.rs index 4bf0386..dbf77ce 100755 --- a/src/datamatrix/encoder/minimal_encoder.rs +++ b/src/datamatrix/encoder/minimal_encoder.rs @@ -17,7 +17,7 @@ use std::{fmt, rc::Rc}; use crate::{ - common::{CharacterSet, ECIInput, MinimalECIInput, Result, Eci}, + common::{CharacterSet, ECIInput, Eci, MinimalECIInput, Result}, Exceptions, }; diff --git a/src/pdf417/decoder/decoded_bit_stream_parser.rs b/src/pdf417/decoder/decoded_bit_stream_parser.rs index 83fa2c7..6b4b0cf 100644 --- a/src/pdf417/decoder/decoded_bit_stream_parser.rs +++ b/src/pdf417/decoder/decoded_bit_stream_parser.rs @@ -19,7 +19,7 @@ use num::{self, bigint::ToBigUint, BigUint}; use std::rc::Rc; use crate::{ - common::{DecoderRXingResult, ECIStringBuilder, Result, Eci}, + common::{DecoderRXingResult, ECIStringBuilder, Eci, Result}, pdf417::PDF417RXingResultMetadata, Exceptions, }; diff --git a/src/pdf417/encoder/pdf_417_high_level_encoder.rs b/src/pdf417/encoder/pdf_417_high_level_encoder.rs index f76d6d4..2133370 100644 --- a/src/pdf417/encoder/pdf_417_high_level_encoder.rs +++ b/src/pdf417/encoder/pdf_417_high_level_encoder.rs @@ -21,7 +21,7 @@ use std::{any::TypeId, fmt::Display, str::FromStr}; use crate::{ - common::{CharacterSet, ECIInput, MinimalECIInput, Result, Eci}, + common::{CharacterSet, ECIInput, Eci, MinimalECIInput, Result}, Exceptions, }; @@ -802,7 +802,7 @@ fn encodingECI(eci: Eci, sb: &mut String) -> Result<()> { if (0..900).contains(&(eci as i32)) { sb.push(char::from_u32(ECI_CHARSET).ok_or(Exceptions::PARSE)?); sb.push(char::from_u32(eci as u32).ok_or(Exceptions::PARSE)?); - } else if (eci as i32 )< 810900 { + } else if (eci as i32) < 810900 { sb.push(char::from_u32(ECI_GENERAL_PURPOSE).ok_or(Exceptions::PARSE)?); sb.push(char::from_u32(((eci as i32) / 900 - 1) as u32).ok_or(Exceptions::PARSE)?); sb.push(char::from_u32(((eci as i32) % 900) as u32).ok_or(Exceptions::PARSE)?); @@ -908,13 +908,8 @@ mod PDF417EncoderTestCase { #[test] fn testEncodeNumeric() { - let encoded = encodeHighLevel( - "1234", - Compaction::NUMERIC, - Some(CharacterSet::UTF8), - false, - ) - .expect("encode"); + let encoded = encodeHighLevel("1234", Compaction::NUMERIC, Some(CharacterSet::UTF8), false) + .expect("encode"); assert_eq!("\u{039f}\u{001A}\u{0386}\u{C}\u{01b2}", encoded); // converted \f to \u{0046} } diff --git a/src/qrcode/decoder/decoded_bit_stream_parser.rs b/src/qrcode/decoder/decoded_bit_stream_parser.rs index 3a76efb..5924d37 100644 --- a/src/qrcode/decoder/decoded_bit_stream_parser.rs +++ b/src/qrcode/decoder/decoded_bit_stream_parser.rs @@ -15,7 +15,7 @@ */ use crate::{ - common::{BitSource, CharacterSet, DecoderRXingResult, Result, StringUtils, Eci}, + common::{BitSource, CharacterSet, DecoderRXingResult, Eci, Result, StringUtils}, DecodingHintDictionary, Exceptions, }; @@ -91,7 +91,7 @@ pub fn decode( Mode::ECI => { // Count doesn't apply to ECI let value = parseECIValue(&mut bits)?; - currentCharacterSetECI = CharacterSet::from(Eci::from(value)).into();//CharacterSet::get_character_set_by_eci(value).ok(); + currentCharacterSetECI = CharacterSet::from(Eci::from(value)).into(); //CharacterSet::get_character_set_by_eci(value).ok(); if currentCharacterSetECI.is_none() { return Err(Exceptions::format_with(format!( "Value of {value} not valid" diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index 94e743b..abd4243 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -24,7 +24,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ common::{ reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonEncoder}, - BitArray, CharacterSet, Result, Eci, + BitArray, CharacterSet, Eci, Result, }, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, EncodeHintType, EncodeHintValue, EncodingHintDictionary, Exceptions, @@ -716,11 +716,7 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()> Ok(()) } -pub fn append8BitBytes( - content: &str, - bits: &mut BitArray, - encoding: CharacterSet, -) -> Result<()> { +pub fn append8BitBytes(content: &str, bits: &mut BitArray, encoding: CharacterSet) -> Result<()> { let bytes = encoding .encode(content) .map_err(|e| Exceptions::writer_with(format!("error {e}")))?; From a8215572097c0c1878a3148eb6e4baf844aa50a5 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 4 Mar 2023 14:43:01 -0600 Subject: [PATCH 10/10] disable some charactersets to match zxing We should be able to allow these, research needs to be done into why they do not work. --- src/common/character_set.rs | 40 ++++++++++++++++++------------------- src/common/eci.rs | 20 +++++++++---------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/common/character_set.rs b/src/common/character_set.rs index f3669bf..b65696c 100644 --- a/src/common/character_set.rs +++ b/src/common/character_set.rs @@ -34,14 +34,14 @@ pub enum CharacterSet { ISO8859_3, //(5, "ISO-8859-3"), ISO8859_4, //(6, "ISO-8859-4"), ISO8859_5, //(7, "ISO-8859-5"), - ISO8859_6, //(8, "ISO-8859-6"), + // ISO8859_6, //(8, "ISO-8859-6"), ISO8859_7, //(9, "ISO-8859-7"), - ISO8859_8, //(10, "ISO-8859-8"), + // ISO8859_8, //(10, "ISO-8859-8"), ISO8859_9, //(11, "ISO-8859-9"), - ISO8859_10, //(12, "ISO-8859-10"), - ISO8859_11, //(13, "ISO-8859-11"), + // ISO8859_10, //(12, "ISO-8859-10"), + // ISO8859_11, //(13, "ISO-8859-11"), ISO8859_13, //(15, "ISO-8859-13"), - ISO8859_14, //(16, "ISO-8859-14"), + // ISO8859_14, //(16, "ISO-8859-14"), ISO8859_15, //(17, "ISO-8859-15"), ISO8859_16, //(18, "ISO-8859-16"), Shift_JIS, //(20, "Shift_JIS"), @@ -109,14 +109,14 @@ impl CharacterSet { CharacterSet::ISO8859_3 => "ISO-8859-3", CharacterSet::ISO8859_4 => "ISO-8859-4", CharacterSet::ISO8859_5 => "ISO-8859-5", - CharacterSet::ISO8859_6 => "ISO-8859-6", + // CharacterSet::ISO8859_6 => "ISO-8859-6", CharacterSet::ISO8859_7 => "ISO-8859-7", - CharacterSet::ISO8859_8 => "ISO-8859-8", + // CharacterSet::ISO8859_8 => "ISO-8859-8", CharacterSet::ISO8859_9 => "ISO-8859-9", - CharacterSet::ISO8859_10 => "ISO-8859-10", - CharacterSet::ISO8859_11 => "ISO-8859-11", + // CharacterSet::ISO8859_10 => "ISO-8859-10", + // CharacterSet::ISO8859_11 => "ISO-8859-11", CharacterSet::ISO8859_13 => "ISO-8859-13", - CharacterSet::ISO8859_14 => "ISO-8859-14", + // CharacterSet::ISO8859_14 => "ISO-8859-14", CharacterSet::ISO8859_15 => "ISO-8859-15", CharacterSet::ISO8859_16 => "ISO-8859-16", CharacterSet::Shift_JIS => "shift_jis", @@ -148,14 +148,14 @@ impl CharacterSet { CharacterSet::ISO8859_3 => "iso-8859-3", CharacterSet::ISO8859_4 => "iso-8859-4", CharacterSet::ISO8859_5 => "iso-8859-5", - CharacterSet::ISO8859_6 => "ISO-8859-6", + // CharacterSet::ISO8859_6 => "ISO-8859-6", CharacterSet::ISO8859_7 => "iso-8859-7", - CharacterSet::ISO8859_8 => "ISO-8859-8", + // CharacterSet::ISO8859_8 => "ISO-8859-8", CharacterSet::ISO8859_9 => "iso-8859-9", - CharacterSet::ISO8859_10 => "ISO-8859-10", - CharacterSet::ISO8859_11 => "ISO-8859-11", + // CharacterSet::ISO8859_10 => "ISO-8859-10", + // CharacterSet::ISO8859_11 => "ISO-8859-11", CharacterSet::ISO8859_13 => "iso-8859-13", - CharacterSet::ISO8859_14 => "ISO-8859-14", + // CharacterSet::ISO8859_14 => "ISO-8859-14", CharacterSet::ISO8859_15 => "iso-8859-15", CharacterSet::ISO8859_16 => "iso-8859-16", CharacterSet::Shift_JIS => "shift_jis", @@ -278,14 +278,14 @@ impl CharacterSet { "iso-8859-3" => Some(CharacterSet::ISO8859_3), "iso-8859-4" => Some(CharacterSet::ISO8859_4), "iso-8859-5" => Some(CharacterSet::ISO8859_5), - "iso-8859-6" => Some(CharacterSet::ISO8859_6), + // "iso-8859-6" => Some(CharacterSet::ISO8859_6), "iso-8859-7" => Some(CharacterSet::ISO8859_7), - "iso-8859-8" => Some(CharacterSet::ISO8859_8), + // "iso-8859-8" => Some(CharacterSet::ISO8859_8), "iso-8859-9" => Some(CharacterSet::ISO8859_9), - "ISO-8859-10" => Some(CharacterSet::ISO8859_10), - "ISO-8859-11" => Some(CharacterSet::ISO8859_11), + // "ISO-8859-10" => Some(CharacterSet::ISO8859_10), + // "ISO-8859-11" => Some(CharacterSet::ISO8859_11), "iso-8859-13" => Some(CharacterSet::ISO8859_13), - "ISO-8859-14" => Some(CharacterSet::ISO8859_14), + // "ISO-8859-14" => Some(CharacterSet::ISO8859_14), "iso-8859-15" => Some(CharacterSet::ISO8859_15), "iso-8859-16" => Some(CharacterSet::ISO8859_16), "shift_jis" => Some(CharacterSet::Shift_JIS), diff --git a/src/common/eci.rs b/src/common/eci.rs index da47109..e33893e 100644 --- a/src/common/eci.rs +++ b/src/common/eci.rs @@ -123,11 +123,11 @@ impl From for Eci { CharacterSet::UTF32BE => Eci::UTF32BE, CharacterSet::UTF32LE => Eci::UTF32LE, CharacterSet::Binary => Eci::Binary, - CharacterSet::ISO8859_6 => Eci::ISO8859_6, - CharacterSet::ISO8859_8 => Eci::ISO8859_8, - CharacterSet::ISO8859_10 => Eci::ISO8859_10, - CharacterSet::ISO8859_11 => Eci::ISO8859_11, - CharacterSet::ISO8859_14 => Eci::ISO8859_14, + // CharacterSet::ISO8859_6 => Eci::ISO8859_6, + // CharacterSet::ISO8859_8 => Eci::ISO8859_8, + // CharacterSet::ISO8859_10 => Eci::ISO8859_10, + // CharacterSet::ISO8859_11 => Eci::ISO8859_11, + // CharacterSet::ISO8859_14 => Eci::ISO8859_14, _ => Eci::Unknown, } } @@ -142,14 +142,14 @@ impl From for CharacterSet { Eci::ISO8859_3 => CharacterSet::ISO8859_3, Eci::ISO8859_4 => CharacterSet::ISO8859_4, Eci::ISO8859_5 => CharacterSet::ISO8859_5, - Eci::ISO8859_6 => CharacterSet::ISO8859_6, + // Eci::ISO8859_6 => CharacterSet::ISO8859_6, Eci::ISO8859_7 => CharacterSet::ISO8859_7, - Eci::ISO8859_8 => CharacterSet::ISO8859_8, + // Eci::ISO8859_8 => CharacterSet::ISO8859_8, Eci::ISO8859_9 => CharacterSet::ISO8859_9, - Eci::ISO8859_10 => CharacterSet::ISO8859_10, - Eci::ISO8859_11 => CharacterSet::ISO8859_11, + // Eci::ISO8859_10 => CharacterSet::ISO8859_10, + // Eci::ISO8859_11 => CharacterSet::ISO8859_11, Eci::ISO8859_13 => CharacterSet::ISO8859_13, - Eci::ISO8859_14 => CharacterSet::ISO8859_14, + // Eci::ISO8859_14 => CharacterSet::ISO8859_14, Eci::ISO8859_15 => CharacterSet::ISO8859_15, Eci::ISO8859_16 => CharacterSet::ISO8859_16, Eci::Shift_JIS => CharacterSet::Shift_JIS,