repurpose CharacterSetECI as encoding abstraction

This commit is contained in:
Henry Schimke
2023-03-02 15:50:56 -06:00
parent 37dc1be041
commit 5deb1ddbe2
29 changed files with 304 additions and 337 deletions

View File

@@ -16,8 +16,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use encoding::EncodingRef;
use crate::{ use crate::{
aztec::{ aztec::{
aztec_detector_result::AztecDetectorRXingResult, aztec_detector_result::AztecDetectorRXingResult,
@@ -25,7 +23,7 @@ use crate::{
encoder::HighLevelEncoder, encoder::HighLevelEncoder,
shared_test_methods::{stripSpace, toBitArray, toBooleanArray}, shared_test_methods::{stripSpace, toBitArray, toBooleanArray},
}, },
BarcodeFormat, EncodeHintType, EncodeHintValue, Point, BarcodeFormat, EncodeHintType, EncodeHintValue, Point, common::CharacterSetECI,
}; };
use super::{encoder::aztec_encoder, AztecWriter}; use super::{encoder::aztec_encoder, AztecWriter};
@@ -34,8 +32,6 @@ use crate::Writer;
use rand::Rng; use rand::Rng;
use encoding::Encoding;
/** /**
* Aztec 2D generator unit tests. * Aztec 2D generator unit tests.
* *
@@ -43,10 +39,10 @@ use encoding::Encoding;
* @author Frank Yellin * @author Frank Yellin
*/ */
const ISO_8859_1: EncodingRef = encoding::all::ISO_8859_1; //StandardCharsets.ISO_8859_1; const ISO_8859_1: CharacterSetECI = CharacterSetECI::ISO8859_1; //StandardCharsets.ISO_8859_1;
const UTF_8: EncodingRef = encoding::all::UTF_8; //StandardCharsets.UTF_8; const UTF_8: CharacterSetECI = CharacterSetECI::UTF8; //StandardCharsets.UTF_8;
const ISO_8859_15: EncodingRef = encoding::all::ISO_8859_15; //Charset.forName("ISO-8859-15"); const ISO_8859_15: CharacterSetECI = CharacterSetECI::ISO8859_15; //Charset.forName("ISO-8859-15");
const WINDOWS_1252: EncodingRef = encoding::all::WINDOWS_1252; //Charset.forName("Windows-1252"); const WINDOWS_1252: CharacterSetECI = CharacterSetECI::Cp1252; //Charset.forName("Windows-1252");
// const DOTX: &str = "[^.X]"; // const DOTX: &str = "[^.X]";
// const SPACES: &str = "\\s+"; // const SPACES: &str = "\\s+";
@@ -140,8 +136,9 @@ X X X X X X X X X X X X X
#[test] #[test]
fn testAztecWriter() { fn testAztecWriter() {
let shift_jis: EncodingRef = let shift_jis: CharacterSetECI =
encoding::label::encoding_from_whatwg_label("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", 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("Espa\u{00F1}ol", Some(ISO_8859_1), 25, true, 1); // Explicit ISO-8859-1
testWriter("\u{20AC} 1 sample data.", Some(WINDOWS_1252), 25, true, 2); // ISO-8859-1 can't encode Euro; Windows-1252 can testWriter("\u{20AC} 1 sample data.", Some(WINDOWS_1252), 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(ISO_8859_15), 0, true, 2);
testWriter( testWriter(
"\u{20AC} 1 sample data.", "\u{20AC} 1 sample data.",
Some(encoding::all::UTF_16BE), Some(CharacterSetECI::UnicodeBigUnmarked),
0, 0,
true, true,
3, 3,
@@ -711,7 +708,7 @@ fn testEncodeDecode(data: &str, compact: bool, layers: u32) {
fn testWriter( fn testWriter(
data: &str, data: &str,
charset: Option<EncodingRef>, charset: Option<CharacterSetECI>,
ecc_percent: u32, ecc_percent: u32,
compact: bool, compact: bool,
layers: u32, layers: u32,
@@ -721,7 +718,7 @@ fn testWriter(
if charset.is_some() { if charset.is_some() {
hints.insert( hints.insert(
EncodeHintType::CHARACTER_SET, EncodeHintType::CHARACTER_SET,
EncodeHintValue::CharacterSet(charset.unwrap().name().to_owned()), EncodeHintValue::CharacterSet(charset.unwrap().getCharsetName().to_string()),
); );
} }
// if (null != charset) { // if (null != charset) {
@@ -737,7 +734,7 @@ fn testWriter(
let cset = match charset { let cset = match charset {
Some(cs) => cs, Some(cs) => cs,
None => encoding::all::ISO_8859_1, None => CharacterSetECI::ISO8859_1,
}; };
let aztec = aztec_encoder::encode_with_charset( let aztec = aztec_encoder::encode_with_charset(
data, data,
@@ -820,10 +817,8 @@ fn testStuffBits(wordSize: usize, bits: &str, expected: &str) {
fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) { fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) {
let bits = HighLevelEncoder::with_charset( let bits = HighLevelEncoder::with_charset(
encoding::all::UTF_8 CharacterSetECI::UTF8.encode(s).expect("should encode to bytes"),
.encode(s, encoding::EncoderTrap::Strict) CharacterSetECI::UTF8,
.expect("should encode to bytes"),
encoding::all::UTF_8,
) )
.encode() .encode()
.expect("high level ok"); .expect("high level ok");
@@ -843,9 +838,7 @@ fn testHighLevelEncodeStringUtf8(s: &str, expectedBits: &str) {
fn testHighLevelEncodeString(s: &str, expectedBits: &str) { fn testHighLevelEncodeString(s: &str, expectedBits: &str) {
let bits = HighLevelEncoder::new( let bits = HighLevelEncoder::new(
encoding::all::ISO_8859_1 CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"),
.encode(s, encoding::EncoderTrap::Strict)
.expect("should encode to bytes"),
) )
.encode() .encode()
.expect("high level ok"); .expect("high level ok");
@@ -864,9 +857,7 @@ fn testHighLevelEncodeString(s: &str, expectedBits: &str) {
fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) { fn testHighLevelEncodeStringCount(s: &str, expectedReceivedBits: u32) {
let bits = HighLevelEncoder::new( let bits = HighLevelEncoder::new(
encoding::all::ISO_8859_1 CharacterSetECI::ISO8859_1.encode(s).expect("should encode to bytes"),
.encode(s, encoding::EncoderTrap::Strict)
.expect("should encode to bytes"),
) )
.encode() .encode()
.expect("high level ok"); .expect("high level ok");

View File

@@ -16,10 +16,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use encoding::EncodingRef;
use crate::{ use crate::{
common::{BitMatrix, Result}, common::{BitMatrix, Result, CharacterSetECI},
exceptions::Exceptions, exceptions::Exceptions,
BarcodeFormat, EncodeHintType, EncodeHintValue, Writer, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer,
}; };
@@ -58,10 +56,7 @@ impl Writer for AztecWriter {
hints.get(&EncodeHintType::CHARACTER_SET) hints.get(&EncodeHintType::CHARACTER_SET)
{ {
if cset_name.to_lowercase() != "iso-8859-1" { if cset_name.to_lowercase() != "iso-8859-1" {
charset = Some( charset = CharacterSetECI::getCharacterSetECIByName(cset_name);
encoding::label::encoding_from_whatwg_label(cset_name)
.ok_or(Exceptions::ILLEGAL_ARGUMENT)?,
);
} }
} }
if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) = if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) =
@@ -91,7 +86,7 @@ fn encode(
format: BarcodeFormat, format: BarcodeFormat,
width: u32, width: u32,
height: u32, height: u32,
charset: Option<EncodingRef>, charset: Option<CharacterSetECI>,
ecc_percent: u32, ecc_percent: u32,
layers: i32, layers: i32,
) -> Result<BitMatrix> { ) -> Result<BitMatrix> {

View File

@@ -113,8 +113,8 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
// Intermediary buffer of decoded bytes, which is decoded into a string and flushed // Intermediary buffer of decoded bytes, which is decoded into a string and flushed
// when character encoding changes (ECI) or input ends. // when character encoding changes (ECI) or input ends.
let mut decoded_bytes: Vec<u8> = Vec::new(); let mut decoded_bytes: Vec<u8> = 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; let mut index = 0;
@@ -161,8 +161,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
// flush bytes, FLG changes state // flush bytes, FLG changes state
result.push_str( result.push_str(
&encdr &encdr
.decode(&decoded_bytes, encoding::DecoderTrap::Strict) .decode(&decoded_bytes)?,
.map_err(Exceptions::illegal_state_with)?,
); );
decoded_bytes.clear(); decoded_bytes.clear();
@@ -190,7 +189,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
if charset_eci.is_err() { if charset_eci.is_err() {
return Err(Exceptions::format_with("Charset must exist")); 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 // Go back to whatever mode we had been in
@@ -207,7 +206,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
} }
} else { } else {
// Though stored as a table of strings for convenience, codes actually represent 1 or 2 *bytes*. // 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.as_bytes();
//let b = str.getBytes(StandardCharsets.US_ASCII); //let b = str.getBytes(StandardCharsets.US_ASCII);
//decodedBytes.write(b, 0, b.length); //decodedBytes.write(b, 0, b.length);
@@ -220,7 +219,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
} }
} }
//try { //try {
if let Ok(str) = encdr.decode(&decoded_bytes, encoding::DecoderTrap::Strict) { if let Ok(str) = encdr.decode(&decoded_bytes) {
result.push_str(&str); result.push_str(&str);
} else { } else {
return Err(Exceptions::illegal_state_with("bad encoding")); return Err(Exceptions::illegal_state_with("bad encoding"));

View File

@@ -14,14 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
use encoding::Encoding;
use crate::{ use crate::{
common::{ common::{
reedsolomon::{ reedsolomon::{
get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder, get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder,
}, },
BitArray, BitMatrix, Result, BitArray, BitMatrix, Result, CharacterSetECI,
}, },
exceptions::Exceptions, exceptions::Exceptions,
}; };
@@ -51,10 +49,9 @@ pub const WORD_SIZE: [u32; 33] = [
* @return Aztec symbol matrix with metadata * @return Aztec symbol matrix with metadata
*/ */
pub fn encode_simple(data: &str) -> Result<AztecCode> { pub fn encode_simple(data: &str) -> Result<AztecCode> {
let Ok(bytes) = encoding::all::ISO_8859_1 let Ok(bytes) =CharacterSetECI::ISO8859_1.encode_replace(data) else {
.encode(data, encoding::EncoderTrap::Replace) else { return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1")));
return Err(Exceptions::illegal_argument_with(format!("'{data}' cannot be encoded as ISO_8859_1"))); };
};
encode_bytes_simple(&bytes) encode_bytes_simple(&bytes)
} }
@@ -68,7 +65,7 @@ pub fn encode_simple(data: &str) -> Result<AztecCode> {
* @return Aztec symbol matrix with metadata * @return Aztec symbol matrix with metadata
*/ */
pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result<AztecCode> { pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result<AztecCode> {
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) encode_bytes(&bytes, minECCPercent, userSpecifiedLayers)
} else { } else {
Err(Exceptions::illegal_argument_with(format!( Err(Exceptions::illegal_argument_with(format!(
@@ -93,9 +90,9 @@ pub fn encode_with_charset(
data: &str, data: &str,
minECCPercent: u32, minECCPercent: u32,
userSpecifiedLayers: i32, userSpecifiedLayers: i32,
charset: encoding::EncodingRef, charset: CharacterSetECI,
) -> Result<AztecCode> { ) -> Result<AztecCode> {
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) encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
} else { } else {
Err(Exceptions::illegal_argument_with(format!( Err(Exceptions::illegal_argument_with(format!(
@@ -132,7 +129,7 @@ pub fn encode_bytes(
data, data,
minECCPercent, minECCPercent,
userSpecifiedLayers, userSpecifiedLayers,
encoding::all::ISO_8859_1, CharacterSetECI::ISO8859_1,
) )
} }
@@ -151,7 +148,7 @@ pub fn encode_bytes_with_charset(
data: &[u8], data: &[u8],
min_eccpercent: u32, min_eccpercent: u32,
user_specified_layers: i32, user_specified_layers: i32,
charset: encoding::EncodingRef, charset: CharacterSetECI,
) -> Result<AztecCode> { ) -> Result<AztecCode> {
// High-level encode // High-level encode
let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?; let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?;

View File

@@ -35,7 +35,7 @@ use super::{State, Token};
*/ */
pub struct HighLevelEncoder { pub struct HighLevelEncoder {
text: Vec<u8>, text: Vec<u8>,
charset: encoding::EncodingRef, charset: CharacterSetECI,
} }
impl HighLevelEncoder { impl HighLevelEncoder {
@@ -229,11 +229,11 @@ impl HighLevelEncoder {
pub fn new(text: Vec<u8>) -> Self { pub fn new(text: Vec<u8>) -> Self {
Self { Self {
text, text,
charset: encoding::all::ISO_8859_1, charset: CharacterSetECI::ISO8859_1,
} }
} }
pub fn with_charset(text: Vec<u8>, charset: encoding::EncodingRef) -> Self { pub fn with_charset(text: Vec<u8>, charset: CharacterSetECI) -> Self {
Self { text, charset } Self { text, charset }
} }
@@ -242,16 +242,16 @@ impl HighLevelEncoder {
*/ */
pub fn encode(&self) -> Result<BitArray> { pub fn encode(&self) -> Result<BitArray> {
let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0); let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0);
if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) { //if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) {
if eci != CharacterSetECI::ISO8859_1 { if self.charset != CharacterSetECI::ISO8859_1 {
//} && eci != CharacterSetECI::Cp1252 { //} && eci != CharacterSetECI::Cp1252 {
initial_state = initial_state.appendFLGn(CharacterSetECI::getValue(&eci))?; initial_state = initial_state.appendFLGn(self.charset.getValue())?;
} }
} else { // } else {
return Err(Exceptions::illegal_argument_with( // return Err(Exceptions::illegal_argument_with(
"No ECI code for character set", // "No ECI code for character set",
)); // ));
} // }
// if self.charset != null { // if self.charset != null {
// CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset); // CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset);
// if (null == eci) { // if (null == eci) {

View File

@@ -16,10 +16,8 @@
use std::fmt; use std::fmt;
use encoding::Encoding;
use crate::{ use crate::{
common::{BitArray, Result}, common::{BitArray, Result, CharacterSetECI},
exceptions::Exceptions, exceptions::Exceptions,
}; };
@@ -88,8 +86,8 @@ impl State {
)); ));
// throw new IllegalArgumentException("ECI code must be between 0 and 999999"); // throw new IllegalArgumentException("ECI code must be between 0 and 999999");
} else { } else {
let Ok(eci_digits) = encoding::all::ISO_8859_1 let Ok(eci_digits) = CharacterSetECI::ISO8859_1
.encode(&format!("{eci}"), encoding::EncoderTrap::Strict) .encode(&format!("{eci}"))
else { else {
return Err(Exceptions::ILLEGAL_ARGUMENT) return Err(Exceptions::ILLEGAL_ARGUMENT)
}; };

View File

@@ -20,7 +20,7 @@ use regex::Regex;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::RXingResult; use crate::{RXingResult, common::CharacterSetECI};
use uriparse::URI; use uriparse::URI;
@@ -404,9 +404,9 @@ fn maybeAppendFragment(fragmentBuffer: &mut Vec<u8>, charset: &str, result: &mut
if charset.is_empty() { if charset.is_empty() {
fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default()); fragment = String::from_utf8(fragmentBytes).unwrap_or_else(|_| String::default());
// fragment = new String(fragmentBytes, StandardCharsets.UTF_8); // 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) = fragment = if let Ok(encoded_result) =
enc.decode(&fragmentBytes, encoding::DecoderTrap::Strict) enc.decode(&fragmentBytes)
{ {
encoded_result encoded_result
} else { } else {

View File

@@ -23,12 +23,13 @@
// import java.nio.charset.StandardCharsets; // import java.nio.charset.StandardCharsets;
// import java.util.Random; // import java.util.Random;
use encoding::{Encoding, EncodingRef};
use rand::Rng; use rand::Rng;
use std::collections::HashMap; use std::collections::HashMap;
use crate::common::StringUtils; use crate::common::StringUtils;
use super::CharacterSetECI;
#[test] #[test]
fn test_random() { fn test_random() {
let mut r = rand::thread_rng(); let mut r = rand::thread_rng();
@@ -38,10 +39,9 @@ fn test_random() {
// *byte = r.gen(); // *byte = r.gen();
// } // }
assert_eq!( assert_eq!(
encoding::all::UTF_8.name(), CharacterSetECI::UTF8,
StringUtils::guessCharset(&bytes, &HashMap::new()) StringUtils::guessCharset(&bytes, &HashMap::new())
.unwrap() .unwrap()
.name()
); );
} }
@@ -50,7 +50,7 @@ fn test_short_shift_jis1() {
// 金魚 // 金魚
do_test( do_test(
&[0x8b, 0xe0, 0x8b, 0x9b], &[0x8b, 0xe0, 0x8b, 0x9b],
encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), CharacterSetECI::SJIS,
"SJIS", "SJIS",
); );
} }
@@ -58,7 +58,7 @@ fn test_short_shift_jis1() {
#[test] #[test]
fn test_short_iso885911() { fn test_short_iso885911() {
// båd // 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] #[test]
@@ -66,7 +66,7 @@ fn test_short_utf8() {
// Español // Español
do_test( do_test(
&[0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c], &[0x45, 0x73, 0x70, 0x61, 0xc3, 0xb1, 0x6f, 0x6c],
encoding::all::UTF_8, CharacterSetECI::UTF8,
"UTF8", "UTF8",
); );
} }
@@ -76,7 +76,7 @@ fn test_mixed_shift_jis1() {
// Hello 金! // Hello 金!
do_test( do_test(
&[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21], &[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x8b, 0xe0, 0x21],
encoding::label::encoding_from_whatwg_label("SJIS").unwrap(), CharacterSetECI::SJIS,
"SJIS", "SJIS",
); );
} }
@@ -86,8 +86,8 @@ fn test_utf16_be() {
// 调压柜 // 调压柜
do_test( do_test(
&[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc], &[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc],
encoding::all::UTF_16BE, CharacterSetECI::UnicodeBigUnmarked,
encoding::all::UTF_16BE.name(), &CharacterSetECI::UnicodeBigUnmarked.getCharsetName(),
); );
} }
@@ -96,15 +96,15 @@ fn test_utf16_le() {
// 调压柜 // 调压柜
do_test( do_test(
&[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67], &[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67],
encoding::all::UTF_16LE, CharacterSetECI::UTF16LE,
encoding::all::UTF_16LE.name(), &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 guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap();
let guessedEncoding = StringUtils::guessEncoding(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); assert_eq!(encoding, guessedEncoding);
} }

View File

@@ -14,15 +14,6 @@
* limitations under the License. * 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 encoding::EncodingRef;
use crate::common::Result; use crate::common::Result;
@@ -59,6 +50,7 @@ pub enum CharacterSetECI {
Cp1252, //(23, "windows-1252"), Cp1252, //(23, "windows-1252"),
Cp1256, //(24, "windows-1256"), Cp1256, //(24, "windows-1256"),
UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"), UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"),
UTF16LE,
UTF8, //(26, "UTF-8"), UTF8, //(26, "UTF-8"),
ASCII, //(new int[] {27, 170}, "US-ASCII"), ASCII, //(new int[] {27, 170}, "US-ASCII"),
Big5, //(28), Big5, //(28),
@@ -101,8 +93,8 @@ impl CharacterSetECI {
Self::getValue(self) Self::getValue(self)
} }
pub fn getValue(cs_eci: &CharacterSetECI) -> u32 { pub fn getValue(&self) -> u32 {
match cs_eci { match self {
CharacterSetECI::Cp437 => 0, CharacterSetECI::Cp437 => 0,
CharacterSetECI::ISO8859_1 => 1, CharacterSetECI::ISO8859_1 => 1,
CharacterSetECI::ISO8859_2 => 4, CharacterSetECI::ISO8859_2 => 4,
@@ -125,6 +117,7 @@ impl CharacterSetECI {
CharacterSetECI::Cp1252 => 23, CharacterSetECI::Cp1252 => 23,
CharacterSetECI::Cp1256 => 24, CharacterSetECI::Cp1256 => 24,
CharacterSetECI::UnicodeBigUnmarked => 25, CharacterSetECI::UnicodeBigUnmarked => 25,
CharacterSetECI::UTF16LE => 100025,
CharacterSetECI::UTF8 => 26, CharacterSetECI::UTF8 => 26,
CharacterSetECI::ASCII => 27, CharacterSetECI::ASCII => 27,
CharacterSetECI::Big5 => 28, CharacterSetECI::Big5 => 28,
@@ -136,8 +129,8 @@ impl CharacterSetECI {
pub fn getCharset(&self, ) -> EncodingRef { pub fn getCharset(&self, ) -> EncodingRef {
let name = match self { let name = match self {
// CharacterSetECI::Cp437 => "CP437", // CharacterSetECI::Cp437 => "CP437",
CharacterSetECI::Cp437 => "UTF-8", CharacterSetECI::Cp437 => "cp437",
CharacterSetECI::ISO8859_1 => "ISO-8859-1", CharacterSetECI::ISO8859_1 => return encoding::all::ISO_8859_1,
CharacterSetECI::ISO8859_2 => "ISO-8859-2", CharacterSetECI::ISO8859_2 => "ISO-8859-2",
CharacterSetECI::ISO8859_3 => "ISO-8859-3", CharacterSetECI::ISO8859_3 => "ISO-8859-3",
CharacterSetECI::ISO8859_4 => "ISO-8859-4", CharacterSetECI::ISO8859_4 => "ISO-8859-4",
@@ -152,12 +145,13 @@ impl CharacterSetECI {
// CharacterSetECI::ISO8859_14 => "ISO-8859-14", // CharacterSetECI::ISO8859_14 => "ISO-8859-14",
CharacterSetECI::ISO8859_15 => "ISO-8859-15", CharacterSetECI::ISO8859_15 => "ISO-8859-15",
CharacterSetECI::ISO8859_16 => "ISO-8859-16", CharacterSetECI::ISO8859_16 => "ISO-8859-16",
CharacterSetECI::SJIS => "Shift_JIS", CharacterSetECI::SJIS => "shift_jis",
CharacterSetECI::Cp1250 => "windows-1250", CharacterSetECI::Cp1250 => "windows-1250",
CharacterSetECI::Cp1251 => "windows-1251", CharacterSetECI::Cp1251 => "windows-1251",
CharacterSetECI::Cp1252 => "windows-1252", CharacterSetECI::Cp1252 => "windows-1252",
CharacterSetECI::Cp1256 => "windows-1256", CharacterSetECI::Cp1256 => "windows-1256",
CharacterSetECI::UnicodeBigUnmarked => "UTF-16BE", CharacterSetECI::UnicodeBigUnmarked => "UTF-16BE",
CharacterSetECI::UTF16LE => "UTF-16LE",
CharacterSetECI::UTF8 => "UTF-8", CharacterSetECI::UTF8 => "UTF-8",
CharacterSetECI::ASCII => "US-ASCII", CharacterSetECI::ASCII => "US-ASCII",
CharacterSetECI::Big5 => "Big5", CharacterSetECI::Big5 => "Big5",
@@ -167,6 +161,41 @@ impl CharacterSetECI {
encoding::label::encoding_from_whatwg_label(name).unwrap() 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 * @param charset Java character set object
* @return CharacterSetECI representing ECI for character encoding, or null if it is legal * @return CharacterSetECI representing ECI for character encoding, or null if it is legal
@@ -179,7 +208,7 @@ impl CharacterSetECI {
charset.name() charset.name()
}; };
match name { match name {
"CP437" => Some(CharacterSetECI::Cp437), "cp437" => Some(CharacterSetECI::Cp437),
"iso-8859-1" => Some(CharacterSetECI::ISO8859_1), "iso-8859-1" => Some(CharacterSetECI::ISO8859_1),
"iso-8859-2" => Some(CharacterSetECI::ISO8859_2), "iso-8859-2" => Some(CharacterSetECI::ISO8859_2),
"iso-8859-3" => Some(CharacterSetECI::ISO8859_3), "iso-8859-3" => Some(CharacterSetECI::ISO8859_3),
@@ -201,7 +230,7 @@ impl CharacterSetECI {
"windows-1252" => Some(CharacterSetECI::Cp1252), "windows-1252" => Some(CharacterSetECI::Cp1252),
"windows-1256" => Some(CharacterSetECI::Cp1256), "windows-1256" => Some(CharacterSetECI::Cp1256),
"utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked), "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked),
"utf-8" => Some(CharacterSetECI::UTF8), "utf-8" | "utf8" => Some(CharacterSetECI::UTF8),
"us-ascii" => Some(CharacterSetECI::ASCII), "us-ascii" => Some(CharacterSetECI::ASCII),
"big5" => Some(CharacterSetECI::Big5), "big5" => Some(CharacterSetECI::Big5),
"gb2312" => Some(CharacterSetECI::GB18030), "gb2312" => Some(CharacterSetECI::GB18030),
@@ -255,34 +284,34 @@ impl CharacterSetECI {
* but unsupported * but unsupported
*/ */
pub fn getCharacterSetECIByName(name: &str) -> Option<CharacterSetECI> { pub fn getCharacterSetECIByName(name: &str) -> Option<CharacterSetECI> {
match name { match name.to_lowercase().as_str() {
"CP437" => Some(CharacterSetECI::Cp437), "cp437" => Some(CharacterSetECI::Cp437),
"ISO-8859-1" => Some(CharacterSetECI::ISO8859_1), "iso-8859-1" => Some(CharacterSetECI::ISO8859_1),
"ISO-8859-2" => Some(CharacterSetECI::ISO8859_2), "iso-8859-2" => Some(CharacterSetECI::ISO8859_2),
"ISO-8859-3" => Some(CharacterSetECI::ISO8859_3), "iso-8859-3" => Some(CharacterSetECI::ISO8859_3),
"ISO-8859-4" => Some(CharacterSetECI::ISO8859_4), "iso-8859-4" => Some(CharacterSetECI::ISO8859_4),
"ISO-8859-5" => Some(CharacterSetECI::ISO8859_5), "iso-8859-5" => Some(CharacterSetECI::ISO8859_5),
// "ISO-8859-6" => Some(CharacterSetECI::ISO8859_6), // "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-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-10" => Some(CharacterSetECI::ISO8859_10),
// "ISO-8859-11" => Some(CharacterSetECI::ISO8859_11), // "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-14" => Some(CharacterSetECI::ISO8859_14),
"ISO-8859-15" => Some(CharacterSetECI::ISO8859_15), "iso-8859-15" => Some(CharacterSetECI::ISO8859_15),
"ISO-8859-16" => Some(CharacterSetECI::ISO8859_16), "iso-8859-16" => Some(CharacterSetECI::ISO8859_16),
"Shift_JIS" => Some(CharacterSetECI::SJIS), "shift_jis" => Some(CharacterSetECI::SJIS),
"windows-1250" => Some(CharacterSetECI::Cp1250), "windows-1250" => Some(CharacterSetECI::Cp1250),
"windows-1251" => Some(CharacterSetECI::Cp1251), "windows-1251" => Some(CharacterSetECI::Cp1251),
"windows-1252" => Some(CharacterSetECI::Cp1252), "windows-1252" => Some(CharacterSetECI::Cp1252),
"windows-1256" => Some(CharacterSetECI::Cp1256), "windows-1256" => Some(CharacterSetECI::Cp1256),
"UTF-16BE" => Some(CharacterSetECI::UnicodeBigUnmarked), "utf-16be" => Some(CharacterSetECI::UnicodeBigUnmarked),
"UTF-8" => Some(CharacterSetECI::UTF8), "utf-8" | "utf8" => Some(CharacterSetECI::UTF8),
"US-ASCII" => Some(CharacterSetECI::ASCII), "us-ascii" => Some(CharacterSetECI::ASCII),
"Big5" => Some(CharacterSetECI::Big5), "big5" => Some(CharacterSetECI::Big5),
"GB2312" => Some(CharacterSetECI::GB18030), "gb2312" => Some(CharacterSetECI::GB18030),
"EUC-KR" => Some(CharacterSetECI::EUC_KR), "euc-kr" => Some(CharacterSetECI::EUC_KR),
_ => None, _ => None,
} }
} }
@@ -296,7 +325,14 @@ impl CharacterSetECI {
} }
pub fn decode(&self, input:&[u8]) -> Result<String> { pub fn decode(&self, input:&[u8]) -> Result<String> {
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())) self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string()))
}
} }
pub fn decode_replace(&self, input:&[u8]) -> Result<String> { pub fn decode_replace(&self, input:&[u8]) -> Result<String> {

View File

@@ -14,31 +14,17 @@
* limitations under the License. * 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 unicode_segmentation::UnicodeSegmentation;
use super::CharacterSetECI; use super::CharacterSetECI;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
static ENCODERS: Lazy<Vec<EncodingRef>> = Lazy::new(|| { static ENCODERS: Lazy<Vec<CharacterSetECI>> = Lazy::new(|| {
let mut enc_vec = Vec::new(); let mut enc_vec = Vec::new();
for name in NAMES { for name in NAMES {
if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(name) { if let Some(enc) = CharacterSetECI::getCharacterSetECIByName(name) {
// try { enc_vec.push(enc);
enc_vec.push(CharacterSetECI::getCharset(&enc));
// } catch (UnsupportedCharsetException e) {
// continue
// }
} }
} }
enc_vec enc_vec
@@ -83,7 +69,7 @@ const NAMES: [&str; 20] = [
*/ */
#[derive(Clone)] #[derive(Clone)]
pub struct ECIEncoderSet { pub struct ECIEncoderSet {
encoders: Vec<EncodingRef>, encoders: Vec<CharacterSetECI>,
priorityEncoderIndex: Option<usize>, priorityEncoderIndex: Option<usize>,
} }
@@ -98,22 +84,23 @@ impl ECIEncoderSet {
*/ */
pub fn new( pub fn new(
stringToEncodeMain: &str, stringToEncodeMain: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
fnc1: Option<&str>, fnc1: Option<&str>,
) -> Self { ) -> Self {
// List of encoders that potentially encode characters not in ISO-8859-1 in one byte. // List of encoders that potentially encode characters not in ISO-8859-1 in one byte.
let mut encoders: Vec<EncodingRef>; let mut encoders: Vec<CharacterSetECI>;
let mut priorityEncoderIndexValue = None; let mut priorityEncoderIndexValue = None;
let mut neededEncoders: Vec<EncodingRef> = Vec::new(); let mut neededEncoders: Vec<CharacterSetECI> = Vec::new();
let stringToEncode = stringToEncodeMain.graphemes(true).collect::<Vec<&str>>(); let stringToEncode = stringToEncodeMain.graphemes(true).collect::<Vec<&str>>();
//we always need the ISO-8859-1 encoder. It is the default encoding //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 { 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 { } else {
false false
}; };
@@ -126,7 +113,7 @@ impl ECIEncoderSet {
// for (CharsetEncoder encoder : neededEncoders) { // for (CharsetEncoder encoder : neededEncoders) {
let c = stringToEncode.get(i).unwrap(); let c = stringToEncode.get(i).unwrap();
if (fnc1.is_some() && c == fnc1.as_ref().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; canEncode = true;
break; break;
@@ -140,8 +127,7 @@ impl ECIEncoderSet {
// for (CharsetEncoder encoder : ENCODERS) { // for (CharsetEncoder encoder : ENCODERS) {
if encoder if encoder
.encode( .encode(
stringToEncode.get(i).unwrap(), stringToEncode.get(i).unwrap()
encoding::EncoderTrap::Strict,
) )
.is_ok() .is_ok()
{ {
@@ -163,7 +149,7 @@ impl ECIEncoderSet {
if neededEncoders.len() == 1 && !needUnicodeEncoder { if neededEncoders.len() == 1 && !needUnicodeEncoder {
//the entire input can be encoded by the ISO-8859-1 encoder //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 { } else {
// we need more than one single byte encoder or we need a Unicode encoder. // 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 // 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(encoder);
} }
encoders.push(encoding::all::UTF_8); encoders.push(CharacterSetECI::UTF8);
encoders.push(encoding::all::UTF_16BE); encoders.push(CharacterSetECI::UnicodeBigUnmarked);
} }
//Compute priorityEncoderIndex by looking up priorityCharset in encoders //Compute priorityEncoderIndex by looking up priorityCharset in encoders
@@ -187,7 +173,8 @@ impl ECIEncoderSet {
// for i in 0..encoders.len() { // for i in 0..encoders.len() {
for (i, encoder) in encoders.iter().enumerate() { for (i, encoder) in encoders.iter().enumerate() {
// for (int i = 0; i < encoders.length; i++) { // 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); priorityEncoderIndexValue = Some(i);
break; break;
} }
@@ -195,7 +182,7 @@ impl ECIEncoderSet {
} }
// } // }
//invariants //invariants
assert_eq!(encoders[0].name(), encoding::all::ISO_8859_1.name()); assert_eq!(encoders[0], CharacterSetECI::ISO8859_1);
Self { Self {
encoders, encoders,
priorityEncoderIndex: priorityEncoderIndexValue, priorityEncoderIndex: priorityEncoderIndexValue,
@@ -212,13 +199,13 @@ impl ECIEncoderSet {
pub fn getCharsetName(&self, index: usize) -> Option<&'static str> { pub fn getCharsetName(&self, index: usize) -> Option<&'static str> {
if index < self.len() { if index < self.len() {
Some(self.encoders[index].name()) Some(self.encoders[index].getCharsetName())
} else { } else {
None None
} }
} }
pub fn getCharset(&self, index: usize) -> Option<EncodingRef> { pub fn getCharset(&self, index: usize) -> Option<CharacterSetECI> {
if index < self.len() { if index < self.len() {
Some(self.encoders[index]) Some(self.encoders[index])
} else { } else {
@@ -227,9 +214,10 @@ impl ECIEncoderSet {
} }
pub fn getECIValue(&self, encoderIndex: usize) -> u32 { pub fn getECIValue(&self, encoderIndex: usize) -> u32 {
CharacterSetECI::getValue( self.encoders[encoderIndex].getValue()
&CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(), // CharacterSetECI::getValue(
) // &CharacterSetECI::getCharacterSetECI(self.encoders[encoderIndex]).unwrap(),
// )
} }
/* /*
@@ -242,7 +230,7 @@ impl ECIEncoderSet {
pub fn canEncode(&self, c: &str, encoderIndex: usize) -> Option<bool> { pub fn canEncode(&self, c: &str, encoderIndex: usize) -> Option<bool> {
if encoderIndex < self.len() { if encoderIndex < self.len() {
let encoder = self.encoders[encoderIndex]; 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()) Some(enc_data.is_ok())
} else { } else {
@@ -253,7 +241,7 @@ impl ECIEncoderSet {
pub fn encode_char(&self, c: &str, encoderIndex: usize) -> Option<Vec<u8>> { pub fn encode_char(&self, c: &str, encoderIndex: usize) -> Option<Vec<u8>> {
if encoderIndex < self.len() { if encoderIndex < self.len() {
let encoder = self.encoders[encoderIndex]; let encoder = self.encoders[encoderIndex];
let enc_data = encoder.encode(c, encoding::EncoderTrap::Strict); let enc_data = encoder.encode(c);
enc_data.ok() enc_data.ok()
// assert!(enc_data.is_ok()); // assert!(enc_data.is_ok());
// enc_data.unwrap() // enc_data.unwrap()
@@ -265,7 +253,7 @@ impl ECIEncoderSet {
pub fn encode_string(&self, s: &str, encoderIndex: usize) -> Option<Vec<u8>> { pub fn encode_string(&self, s: &str, encoderIndex: usize) -> Option<Vec<u8>> {
if encoderIndex < self.len() { if encoderIndex < self.len() {
let encoder = self.encoders[encoderIndex]; let encoder = self.encoders[encoderIndex];
encoder.encode(s, encoding::EncoderTrap::Strict).ok() encoder.encode(s).ok()
} else { } else {
None None
} }

View File

@@ -23,8 +23,6 @@
use std::fmt; use std::fmt;
use encoding::{Encoding, EncodingRef};
use crate::common::Result; use crate::common::Result;
use super::CharacterSetECI; use super::CharacterSetECI;
@@ -37,7 +35,7 @@ use super::CharacterSetECI;
pub struct ECIStringBuilder { pub struct ECIStringBuilder {
current_bytes: Vec<u8>, current_bytes: Vec<u8>,
result: String, result: String,
current_charset: Option<EncodingRef>, //= StandardCharsets.ISO_8859_1; current_charset: Option<CharacterSetECI>, //= StandardCharsets.ISO_8859_1;
} }
impl ECIStringBuilder { impl ECIStringBuilder {
@@ -45,14 +43,14 @@ impl ECIStringBuilder {
Self { Self {
current_bytes: Vec::new(), current_bytes: Vec::new(),
result: String::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 { pub fn with_capacity(initial_capacity: usize) -> Self {
Self { Self {
current_bytes: Vec::with_capacity(initial_capacity), current_bytes: Vec::with_capacity(initial_capacity),
result: String::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<()> { pub fn appendECI(&mut self, value: u32) -> Result<()> {
self.encodeCurrentBytesIfAny(); self.encodeCurrentBytesIfAny();
if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) { self.current_charset = CharacterSetECI::getCharacterSetECIByValue(value).ok();
// dbg!(
// character_set_eci, // if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) {
// CharacterSetECI::getCharset(&character_set_eci).name(), // // dbg!(
// CharacterSetECI::getCharset(&character_set_eci).whatwg_name() // // character_set_eci,
// ); // // CharacterSetECI::getCharset(&character_set_eci).name(),
self.current_charset = Some(CharacterSetECI::getCharset(&character_set_eci)); // // CharacterSetECI::getCharset(&character_set_eci).whatwg_name()
} else { // // );
self.current_charset = None // self.current_charset = Some(character_set_eci);
} // } else {
// self.current_charset = None
// }
// self.current_charset = CharacterSetECI::getCharset(&character_set_eci); // self.current_charset = CharacterSetECI::getCharset(&character_set_eci);
Ok(()) Ok(())
@@ -126,7 +126,7 @@ impl ECIStringBuilder {
/// This function can panic /// This function can panic
pub fn encodeCurrentBytesIfAny(&mut self) { pub fn encodeCurrentBytesIfAny(&mut self) {
if let Some(encoder) = self.current_charset { 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() { if !self.current_bytes.is_empty() {
self.result.push_str( self.result.push_str(
&String::from_utf8(std::mem::take(&mut self.current_bytes)).unwrap(), &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); let bytes = std::mem::take(&mut self.current_bytes);
self.current_bytes.clear(); self.current_bytes.clear();
let encoded_value = encoder let encoded_value = encoder
.decode(&bytes, encoding::DecoderTrap::Strict) .decode(&bytes)
.unwrap(); .unwrap();
self.result.push_str(&encoded_value); self.result.push_str(&encoded_value);
} }

View File

@@ -16,13 +16,12 @@
use std::{fmt, rc::Rc}; use std::{fmt, rc::Rc};
use encoding::EncodingRef;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use crate::common::Result; use crate::common::Result;
use crate::Exceptions; use crate::Exceptions;
use super::{ECIEncoderSet, ECIInput}; use super::{ECIEncoderSet, ECIInput, CharacterSetECI};
//* approximated (latch + 2 codewords) //* approximated (latch + 2 codewords)
pub const COST_PER_ECI: usize = 3; pub const COST_PER_ECI: usize = 3;
@@ -194,7 +193,7 @@ impl MinimalECIInput {
*/ */
pub fn new( pub fn new(
stringToEncodeInput: &str, stringToEncodeInput: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
fnc1: Option<&str>, fnc1: Option<&str>,
) -> Self { ) -> Self {
let stringToEncode = stringToEncodeInput.graphemes(true).collect::<Vec<&str>>(); let stringToEncode = stringToEncodeInput.graphemes(true).collect::<Vec<&str>>();

View File

@@ -14,12 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
use encoding::{Encoding, EncodingRef};
use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary}; use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use super::CharacterSetECI;
/** /**
* Common string-related functions. * Common string-related functions.
* *
@@ -50,8 +50,8 @@ const ASSUME_SHIFT_JIS: bool = false;
// static SHIFT_JIS: &'static str = "SJIS"; // static SHIFT_JIS: &'static str = "SJIS";
// static GB2312: &'static str = "GB2312"; // static GB2312: &'static str = "GB2312";
pub static SHIFT_JIS_CHARSET: Lazy<EncodingRef> = pub static SHIFT_JIS_CHARSET: CharacterSetECI =
Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); CharacterSetECI::SJIS;
// private static final boolean ASSUME_SHIFT_JIS = // private static final boolean ASSUME_SHIFT_JIS =
// SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
@@ -67,14 +67,14 @@ impl StringUtils {
*/ */
pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> { pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> {
let c = StringUtils::guessCharset(bytes, hints)?; let c = StringUtils::guessCharset(bytes, hints)?;
if c.name() == encoding::label::encoding_from_whatwg_label("SJIS")?.name() { if c == CharacterSetECI::SJIS {
Some("SJIS") Some("SJIS")
} else if c.name() == encoding::all::UTF_8.name() { } else if c == CharacterSetECI::UTF8 {
Some("UTF8") Some("UTF8")
} else if c.name() == encoding::all::ISO_8859_1.name() { } else if c == CharacterSetECI::ISO8859_1 {
Some("ISO8859_1") Some("ISO8859_1")
} else { } else {
Some(c.name()) Some(&c.getCharsetName())
} }
} }
@@ -87,12 +87,12 @@ impl StringUtils {
* or the platform default encoding if * or the platform default encoding if
* none of these can possibly be correct * none of these can possibly be correct
*/ */
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<EncodingRef> { pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<CharacterSetECI> {
if let Some(DecodeHintValue::CharacterSet(cs_name)) = if let Some(DecodeHintValue::CharacterSet(cs_name)) =
hints.get(&DecodeHintType::CHARACTER_SET) hints.get(&DecodeHintType::CHARACTER_SET)
{ {
// if let DecodeHintValue::CharacterSet(cs_name) = hint { // 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) { // 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)) && ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE))
{ {
if bytes[0] == 0xFE && bytes[1] == 0xFF { if bytes[0] == 0xFE && bytes[1] == 0xFF {
return Some(encoding::all::UTF_16BE); return Some(CharacterSetECI::UnicodeBigUnmarked);
} else { } 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 // 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) { 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 // 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 if can_be_shift_jis
@@ -232,7 +232,7 @@ impl StringUtils {
|| sjis_max_katakana_word_length >= 3 || sjis_max_katakana_word_length >= 3
|| sjis_max_double_bytes_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: // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
// - If we saw // - If we saw
@@ -243,23 +243,23 @@ impl StringUtils {
return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2) return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2)
|| iso_high_other * 10 >= length || iso_high_other * 10 >= length
{ {
encoding::label::encoding_from_whatwg_label("SJIS") Some(CharacterSetECI::SJIS)
} else { } 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 // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
if can_be_iso88591 { if can_be_iso88591 {
return Some(encoding::all::ISO_8859_1); return Some(CharacterSetECI::ISO8859_1);
} }
if can_be_shift_jis { if can_be_shift_jis {
return Some(encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); return Some(CharacterSetECI::SJIS);
} }
if can_be_utf8 { if can_be_utf8 {
return Some(encoding::all::UTF_8); return Some(CharacterSetECI::UTF8);
} }
// Otherwise, we take a wild guess with platform encoding // Otherwise, we take a wild guess with platform encoding
Some(encoding::all::UTF_8) Some(CharacterSetECI::UTF8)
} }
} }

View File

@@ -17,10 +17,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use encoding::EncodingRef;
use crate::{ use crate::{
common::{BitMatrix, Result}, common::{BitMatrix, Result, CharacterSetECI},
qrcode::encoder::ByteMatrix, qrcode::encoder::ByteMatrix,
BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer,
}; };
@@ -117,14 +115,14 @@ impl Writer for DataMatrixWriter {
false false
}; };
let mut charset: Option<EncodingRef> = None; let mut charset: Option<CharacterSetECI> = None;
let hasEncodingHint = hints.contains_key(&EncodeHintType::CHARACTER_SET); let hasEncodingHint = hints.contains_key(&EncodeHintType::CHARACTER_SET);
if hasEncodingHint { if hasEncodingHint {
let Some(EncodeHintValue::CharacterSet(char_set_name)) = let Some(EncodeHintValue::CharacterSet(char_set_name)) =
hints.get(&EncodeHintType::CHARACTER_SET) else { hints.get(&EncodeHintType::CHARACTER_SET) else {
return Err(Exceptions::illegal_argument_with("charset does not exist")) 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()); // charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
} }
encoded = minimal_encoder::encodeHighLevelWithDetails( encoded = minimal_encoder::encodeHighLevelWithDetails(

View File

@@ -14,10 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
use encoding::Encoding;
use crate::{ use crate::{
common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result}, common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result, CharacterSetECI},
Exceptions, Exceptions,
}; };
@@ -730,9 +728,10 @@ fn decodeBase256Segment(
codewordPosition += 1; codewordPosition += 1;
} }
result.append_string( result.append_string(
&encoding::all::ISO_8859_1
.decode(&bytes, encoding::DecoderTrap::Strict) &CharacterSetECI::ISO8859_1
.map_err(Exceptions::parse_with)?, .decode(&bytes)
?,
); );
byteSegments.push(bytes); byteSegments.push(bytes);

View File

@@ -16,13 +16,12 @@
use std::rc::Rc; use std::rc::Rc;
use crate::common::Result; use crate::common::{Result, CharacterSetECI};
use crate::{Dimension, Exceptions}; use crate::{Dimension, Exceptions};
use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint}; 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> { pub struct EncoderContext<'a> {
symbol_lookup: Rc<SymbolInfoLookup<'a>>, symbol_lookup: Rc<SymbolInfoLookup<'a>>,
@@ -59,10 +58,10 @@ impl<'a> EncoderContext<'_> {
// sb.append(ch); // sb.append(ch);
// } // }
let sb = if let Ok(encoded_bytes) = 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 ISO_8859_1_ENCODER
.decode(&encoded_bytes, encoding::DecoderTrap::Strict) .decode(&encoded_bytes)
.map_err(|e| { .map_err(|e| {
Exceptions::parse_with(format!("round trip decode should always work: {e}")) Exceptions::parse_with(format!("round trip decode should always work: {e}"))
})? })?

View File

@@ -18,7 +18,7 @@ use std::rc::Rc;
use once_cell::sync::Lazy; 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}; 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", 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); 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", 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); visualized);
} }

View File

@@ -16,9 +16,7 @@
use std::rc::Rc; use std::rc::Rc;
use encoding::{self, EncodingRef}; use crate::common::{Result, CharacterSetECI};
use crate::common::Result;
use crate::{Dimension, Exceptions}; use crate::{Dimension, Exceptions};
use super::{ use super::{
@@ -26,7 +24,7 @@ use super::{
SymbolInfoLookup, SymbolShapeHint, TextEncoder, X12Encoder, SymbolInfoLookup, SymbolShapeHint, TextEncoder, X12Encoder,
}; };
#[allow(dead_code)] #[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 * DataMatrix ECC 200 data encoder following the algorithm described in ISO/IEC 16022:200(E) in

View File

@@ -16,16 +16,14 @@
use std::{fmt, rc::Rc}; use std::{fmt, rc::Rc};
use encoding::{self, EncodingRef};
use crate::{ use crate::{
common::{ECIInput, MinimalECIInput, Result}, common::{ECIInput, MinimalECIInput, Result, CharacterSetECI},
Exceptions, Exceptions,
}; };
use super::{high_level_encoder, SymbolShapeHint}; 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 * Encoder that encodes minimally
@@ -153,7 +151,7 @@ pub fn encodeHighLevel(msg: &str) -> Result<String> {
*/ */
pub fn encodeHighLevelWithDetails( pub fn encodeHighLevelWithDetails(
msg: &str, msg: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
fnc1: Option<char>, fnc1: Option<char>,
shape: SymbolShapeHint, shape: SymbolShapeHint,
) -> Result<String> { ) -> Result<String> {
@@ -174,8 +172,8 @@ pub fn encodeHighLevelWithDetails(
} }
Ok(ISO_8859_1_ENCODER Ok(ISO_8859_1_ENCODER
.decode( .decode(
&encode(msg, priorityCharset, fnc1, shape, macroId)?, &encode(msg, priorityCharset, fnc1, shape, macroId)?
encoding::DecoderTrap::Strict,
) )
.expect("should decode")) .expect("should decode"))
// return new String(encode(msg, priorityCharset, fnc1, shape, macroId), StandardCharsets.ISO_8859_1); // return new String(encode(msg, priorityCharset, fnc1, shape, macroId), StandardCharsets.ISO_8859_1);
@@ -197,7 +195,7 @@ pub fn encodeHighLevelWithDetails(
*/ */
fn encode( fn encode(
input: &str, input: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
fnc1: Option<char>, fnc1: Option<char>,
shape: SymbolShapeHint, shape: SymbolShapeHint,
macroId: i32, macroId: i32,
@@ -1398,7 +1396,7 @@ struct Input {
impl Input { impl Input {
pub fn new( pub fn new(
stringToEncode: &str, stringToEncode: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
fnc1: Option<char>, fnc1: Option<char>,
shape: SymbolShapeHint, shape: SymbolShapeHint,
macroId: i32, macroId: i32,

View File

@@ -15,9 +15,9 @@
* limitations under the License. * limitations under the License.
*/ */
use encoding::{Encoding, EncodingRef};
use java_rand; use java_rand;
use crate::common::CharacterSetECI;
use crate::pdf417::decoder::decoded_bit_stream_parser; use crate::pdf417::decoder::decoded_bit_stream_parser;
use crate::pdf417::encoder::{pdf_417_high_level_encoder_test_adapter, Compaction}; use crate::pdf417::encoder::{pdf_417_high_level_encoder_test_adapter, Compaction};
use crate::pdf417::PDF417RXingResultMetadata; use crate::pdf417::PDF417RXingResultMetadata;
@@ -307,8 +307,8 @@ fn testBinaryData() {
random.next_bytes(&mut bytes); random.next_bytes(&mut bytes);
total += encodeDecode( total += encodeDecode(
&encoding::all::ISO_8859_1 &CharacterSetECI::ISO8859_1
.decode(&bytes, encoding::DecoderTrap::Strict) .decode(&bytes)
.expect("decode bytes"), .expect("decode bytes"),
); );
} }
@@ -437,7 +437,7 @@ fn encodeDecode(input: &str) -> u32 {
fn encodeDecodeWithAll( fn encodeDecodeWithAll(
input: &str, input: &str,
charset: Option<EncodingRef>, charset: Option<CharacterSetECI>,
autoECI: bool, autoECI: bool,
decode: bool, decode: bool,
) -> u32 { ) -> u32 {
@@ -523,7 +523,7 @@ fn performECITest(
// for (int i = 0; i < 1000; i++) { // for (int i = 0; i < 1000; i++) {
let s = generateText(&mut random, 100, chars, weights); let s = generateText(&mut random, 100, chars, weights);
minLength += encodeDecodeWithAll(&s, None, true, true); 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!(expectedMinLength, minLength);
assert_eq!(expectedUTFLength, utfLength); assert_eq!(expectedUTFLength, utfLength);

View File

@@ -18,9 +18,7 @@
* This file has been modified from its original form in Barcode4J. * This file has been modified from its original form in Barcode4J.
*/ */
use encoding::EncodingRef; use crate::common::{Result, CharacterSetECI};
use crate::common::Result;
use crate::Exceptions; use crate::Exceptions;
use super::{ use super::{
@@ -34,7 +32,7 @@ pub struct PDF417 {
barcodeMatrix: Option<BarcodeMatrix>, barcodeMatrix: Option<BarcodeMatrix>,
compact: bool, compact: bool,
compaction: Compaction, compaction: Compaction,
encoding: Option<EncodingRef>, encoding: Option<CharacterSetECI>,
minCols: u32, minCols: u32,
maxCols: u32, maxCols: u32,
maxRows: u32, maxRows: u32,
@@ -347,7 +345,7 @@ impl PDF417 {
/** /**
* @param encoding sets character encoding to use * @param encoding sets character encoding to use
*/ */
pub fn setEncoding(&mut self, encoding: Option<EncodingRef>) { pub fn setEncoding(&mut self, encoding: Option<CharacterSetECI>) {
self.encoding = encoding; self.encoding = encoding;
} }
} }

View File

@@ -20,8 +20,6 @@
use std::{any::TypeId, fmt::Display, str::FromStr}; use std::{any::TypeId, fmt::Display, str::FromStr};
use encoding::EncodingRef;
use crate::{ use crate::{
common::{CharacterSetECI, ECIInput, MinimalECIInput, Result}, common::{CharacterSetECI, ECIInput, MinimalECIInput, Result},
Exceptions, Exceptions,
@@ -125,7 +123,7 @@ const TEXT_PUNCTUATION_RAW: [u8; 30] = [
40, 41, 63, 123, 125, 39, 0, 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] = { const MIXED: [i8; 128] = {
let mut mixed = [-1_i8; 128]; let mut mixed = [-1_i8; 128];
@@ -174,7 +172,7 @@ const PUNCTUATION: [i8; 128] = {
pub fn encodeHighLevel( pub fn encodeHighLevel(
msg: &str, msg: &str,
compaction: Compaction, compaction: Compaction,
encoding: Option<EncodingRef>, encoding: Option<CharacterSetECI>,
autoECI: bool, autoECI: bool,
) -> Result<String> { ) -> Result<String> {
let mut encoding = encoding; let mut encoding = encoding;
@@ -199,14 +197,16 @@ pub fn encodeHighLevel(
input = Box::new(NoECIInput::new(msg.to_owned())); input = Box::new(NoECIInput::new(msg.to_owned()));
if encoding.is_none() { if encoding.is_none() {
encoding = Some(DEFAULT_ENCODING); encoding = Some(DEFAULT_ENCODING);
} else if DEFAULT_ENCODING.name() } else if &DEFAULT_ENCODING
!= encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)?.name() != encoding.as_ref().ok_or(Exceptions::ILLEGAL_STATE)?
{ {
if let Some(eci) = // if let Some(eci) =
CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?) // CharacterSetECI::getCharacterSetECI(encoding.ok_or(Exceptions::ILLEGAL_STATE)?)
{ // {
encodingECI(CharacterSetECI::getValue(&eci) as i32, &mut sb)?; // 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 let msgBytes = encoding
.as_ref() .as_ref()
.ok_or(Exceptions::ILLEGAL_STATE)? .ok_or(Exceptions::ILLEGAL_STATE)?
.encode(&input.to_string(), encoding::EncoderTrap::Strict) .encode(&input.to_string())
.unwrap_or_default(); //input.to_string().getBytes(encoding); .unwrap_or_default(); //input.to_string().getBytes(encoding);
encodeBinary( encodeBinary(
&msgBytes, &msgBytes,
@@ -286,7 +286,7 @@ pub fn encodeHighLevel(
if let Ok(enc_str) = encoding if let Ok(enc_str) = encoding
.as_ref() .as_ref()
.ok_or(Exceptions::ILLEGAL_STATE)? .ok_or(Exceptions::ILLEGAL_STATE)?
.encode(&str, encoding::EncoderTrap::Strict) .encode(&str)
{ {
Some(enc_str) Some(enc_str)
} else { } else {
@@ -747,7 +747,7 @@ fn determineConsecutiveTextCount<T: ECIInput + ?Sized>(
fn determineConsecutiveBinaryCount<T: ECIInput + ?Sized + 'static>( fn determineConsecutiveBinaryCount<T: ECIInput + ?Sized + 'static>(
input: &Box<T>, input: &Box<T>,
startpos: u32, startpos: u32,
encoding: Option<EncodingRef>, encoding: Option<CharacterSetECI>,
) -> Result<u32> { ) -> Result<u32> {
let len = input.length(); let len = input.length();
let mut idx = startpos as usize; let mut idx = startpos as usize;
@@ -770,8 +770,8 @@ fn determineConsecutiveBinaryCount<T: ECIInput + ?Sized + 'static>(
if let Some(encoder) = encoding { if let Some(encoder) = encoding {
let can_encode = encoder let can_encode = encoder
.encode( .encode(
&input.charAt(idx)?.to_string(), &input.charAt(idx)?.to_string()
encoding::EncoderTrap::Strict,
) )
.is_ok(); .is_ok();
@@ -856,11 +856,11 @@ impl Display for NoECIInput {
*/ */
#[cfg(test)] #[cfg(test)]
mod PDF417EncoderTestCase { 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] #[test]
fn testEncodeAuto() { 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"); .expect("encode");
assert_eq!("\u{039f}\u{001A}\u{0385}ABCD", encoded); assert_eq!("\u{039f}\u{001A}\u{0385}ABCD", encoded);
} }
@@ -871,7 +871,7 @@ mod PDF417EncoderTestCase {
encodeHighLevel( encodeHighLevel(
"1%§s ?aG$", "1%§s ?aG$",
Compaction::AUTO, Compaction::AUTO,
Some(encoding::all::UTF_8), Some(CharacterSetECI::UTF8),
false, false,
) )
.expect("encode"); .expect("encode");
@@ -883,7 +883,7 @@ mod PDF417EncoderTestCase {
encodeHighLevel( encodeHighLevel(
"asdfg§asd", "asdfg§asd",
Compaction::AUTO, Compaction::AUTO,
Some(encoding::all::ISO_8859_1), Some(CharacterSetECI::ISO8859_1),
false, false,
) )
.expect("encode"); .expect("encode");
@@ -891,7 +891,7 @@ mod PDF417EncoderTestCase {
#[test] #[test]
fn testEncodeText() { 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"); .expect("encode");
assert_eq!("Ο\u{001A}\u{0001}?", encoded); assert_eq!("Ο\u{001A}\u{0001}?", encoded);
} }
@@ -901,7 +901,7 @@ mod PDF417EncoderTestCase {
let encoded = encodeHighLevel( let encoded = encodeHighLevel(
"1234", "1234",
Compaction::NUMERIC, Compaction::NUMERIC,
Some(encoding::all::UTF_8), Some(CharacterSetECI::UTF8),
false, false,
) )
.expect("encode"); .expect("encode");
@@ -911,7 +911,7 @@ mod PDF417EncoderTestCase {
#[test] #[test]
fn testEncodeByte() { 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"); .expect("encode");
assert_eq!("\u{039f}\u{001A}\u{0385}abcd", encoded); assert_eq!("\u{039f}\u{001A}\u{0385}abcd", encoded);
} }

View File

@@ -14,9 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
use encoding::EncodingRef; use crate::common::{Result, CharacterSetECI};
use crate::common::Result;
use super::{pdf_417_high_level_encoder, Compaction}; use super::{pdf_417_high_level_encoder, Compaction};
@@ -27,7 +25,7 @@ use super::{pdf_417_high_level_encoder, Compaction};
pub fn encodeHighLevel( pub fn encodeHighLevel(
msg: &str, msg: &str,
compaction: Compaction, compaction: Compaction,
encoding: Option<EncodingRef>, encoding: Option<CharacterSetECI>,
autoECI: bool, autoECI: bool,
) -> Result<String> { ) -> Result<String> {
pdf_417_high_level_encoder::encodeHighLevel(msg, compaction, encoding, autoECI) pdf_417_high_level_encoder::encodeHighLevel(msg, compaction, encoding, autoECI)

View File

@@ -17,7 +17,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::{ use crate::{
common::{BitMatrix, Result}, common::{BitMatrix, Result, CharacterSetECI},
BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer,
}; };
@@ -108,7 +108,7 @@ impl Writer for PDF417Writer {
if let Some(EncodeHintValue::CharacterSet(cs)) = if let Some(EncodeHintValue::CharacterSet(cs)) =
hints.get(&EncodeHintType::CHARACTER_SET) 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)) = if let Some(EncodeHintValue::Pdf417AutoEci(auto_eci_str)) =
hints.get(&EncodeHintType::PDF417_AUTO_ECI) hints.get(&EncodeHintType::PDF417_AUTO_ECI)

View File

@@ -204,9 +204,9 @@ fn decodeHanziSegment(bits: &mut BitSource, result: &mut String, count: usize) -
} }
let gb_encoder = let gb_encoder =
encoding::label::encoding_from_whatwg_label("GBK").ok_or(Exceptions::ILLEGAL_STATE)?; CharacterSetECI::GB18030;
let encode_string = gb_encoder 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}")))?; .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?;
result.push_str(&encode_string); result.push_str(&encode_string);
Ok(()) Ok(())
@@ -250,7 +250,7 @@ fn decodeKanjiSegment(
let encoder = { let encoder = {
let _ = currentCharacterSetECI; let _ = currentCharacterSetECI;
let _ = hints; let _ = hints;
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::FORMAT)? CharacterSetECI::SJIS
}; };
#[cfg(feature = "allow_forced_iso_ied_18004_compliance")] #[cfg(feature = "allow_forced_iso_ied_18004_compliance")]
@@ -258,16 +258,16 @@ fn decodeKanjiSegment(
hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT)
{ {
if let Some(ccse) = &currentCharacterSetECI { if let Some(ccse) = &currentCharacterSetECI {
CharacterSetECI::getCharset(ccse) CharacterSetECI::getCharacterSetECIByName(ccse)
} else { } else {
encoding::all::ISO_8859_1 CharacterSetECI::ISO8859_1
} }
} else { } else {
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::FORMAT)? CharacterSetECI::SJIS
}; };
let encode_string = encoder let encode_string = encoder
.decode(&buffer, encoding::DecoderTrap::Strict) .decode(&buffer)
.map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?; .map_err(|e| Exceptions::parse_with(format!("unable to decode buffer {buffer:?}: {e}")))?;
result.push_str(&encode_string); result.push_str(&encode_string);
@@ -308,37 +308,26 @@ fn decodeByteSegment(
if let Some(DecodeHintValue::QrAssumeSpecConformInput(true)) = if let Some(DecodeHintValue::QrAssumeSpecConformInput(true)) =
hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT) hints.get(&DecodeHintType::QR_ASSUME_SPEC_CONFORM_INPUT)
{ {
encoding::all::ISO_8859_1 CharacterSetECI::ISO8859_1
} else { } else {
StringUtils::guessCharset(&readBytes, hints) StringUtils::guessCharset(&readBytes, hints)
} }
} else { } else {
CharacterSetECI::getCharset( currentCharacterSetECI.ok_or(Exceptions::ILLEGAL_STATE)?
currentCharacterSetECI // CharacterSetECI::getCharset(
.as_ref() // currentCharacterSetECI
.ok_or(Exceptions::ILLEGAL_STATE)?, // .as_ref()
) // .ok_or(Exceptions::ILLEGAL_STATE)?,
// )
}; };
let encode_string = if currentCharacterSetECI.is_some() let encode_string =
&& 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 {
encoding encoding
.decode(&readBytes, encoding::DecoderTrap::Strict) .decode(&readBytes)
.map_err(|e| { .map_err(|e| {
Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}")) Exceptions::parse_with(format!("unable to decode buffer {readBytes:?}: {e}"))
})? })?
}; ;
result.push_str(&encode_string); result.push_str(&encode_string);
byteSegments.push(readBytes); byteSegments.push(readBytes);

View File

@@ -17,20 +17,19 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::{ use crate::{
common::BitArray, common::{BitArray, CharacterSetECI},
qrcode::{ qrcode::{
decoder::{ErrorCorrectionLevel, Mode, Version}, decoder::{ErrorCorrectionLevel, Mode, Version},
encoder::{qrcode_encoder, MinimalEncoder}, encoder::{qrcode_encoder, MinimalEncoder},
}, },
EncodeHintType, EncodeHintValue, EncodeHintType, EncodeHintValue,
}; };
use encoding::EncodingRef;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use super::QRCode; use super::QRCode;
static SHIFT_JIS_CHARSET: Lazy<EncodingRef> = static SHIFT_JIS_CHARSET: Lazy<CharacterSetECI> =
Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); Lazy::new(|| CharacterSetECI::SJIS);
/** /**
* @author satorux@google.com (Satoru Takabayashi) - creator * @author satorux@google.com (Satoru Takabayashi) - creator
@@ -1075,10 +1074,9 @@ fn testMinimalEncoder41() {
#[test] #[test]
fn testMinimalEncoder42() { fn testMinimalEncoder42() {
// test halfwidth Katakana character (they are single byte encoded in Shift_JIS) // 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( verifyMinimalEncoding(
"Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}", "Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}",
"ECI(windows-31j),BYTE(Katakana:......)", "ECI(shift_jis),BYTE(Katakana:......)",
None, None,
false, 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 // 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 // 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 // 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( verifyMinimalEncoding(
"Katakana:\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}", "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, None,
false, false,
); );
@@ -1112,7 +1109,7 @@ fn testMinimalEncoder44() {
fn verifyMinimalEncoding( fn verifyMinimalEncoding(
input: &str, input: &str,
expectedRXingResult: &str, expectedRXingResult: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
isGS1: bool, isGS1: bool,
) { ) {
let result = MinimalEncoder::encode_with_details( let result = MinimalEncoder::encode_with_details(
@@ -1198,7 +1195,7 @@ fn verifyNotGS1EncodedData(qrCode: &QRCode) {
fn shiftJISString(bytes: &[u8]) -> String { fn shiftJISString(bytes: &[u8]) -> String {
SHIFT_JIS_CHARSET SHIFT_JIS_CHARSET
.decode(bytes, encoding::DecoderTrap::Strict) .decode(bytes)
.expect("decode should be ok") .expect("decode should be ok")
// return new String(bytes, StringUtils.SHIFT_JIS_CHARSET); // return new String(bytes, StringUtils.SHIFT_JIS_CHARSET);
} }

View File

@@ -16,10 +16,8 @@
use std::{fmt, rc::Rc}; use std::{fmt, rc::Rc};
use encoding::EncodingRef;
use crate::{ use crate::{
common::{BitArray, ECIEncoderSet, Result}, common::{BitArray, ECIEncoderSet, Result, CharacterSetECI},
qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef}, qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef},
Exceptions, Exceptions,
}; };
@@ -107,7 +105,7 @@ impl MinimalEncoder {
*/ */
pub fn new( pub fn new(
stringToEncode: &str, stringToEncode: &str,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
isGS1: bool, isGS1: bool,
ecLevel: ErrorCorrectionLevel, ecLevel: ErrorCorrectionLevel,
) -> Self { ) -> Self {
@@ -142,7 +140,7 @@ impl MinimalEncoder {
pub fn encode_with_details( pub fn encode_with_details(
stringToEncode: &str, stringToEncode: &str,
version: Option<VersionRef>, version: Option<VersionRef>,
priorityCharset: Option<EncodingRef>, priorityCharset: Option<CharacterSetECI>,
isGS1: bool, isGS1: bool,
ecLevel: ErrorCorrectionLevel, ecLevel: ErrorCorrectionLevel,
) -> Result<RXingResultList> { ) -> Result<RXingResultList> {
@@ -1044,10 +1042,10 @@ impl fmt::Display for RXingResultNode {
result.push('('); result.push('(');
if self.mode == Mode::ECI { if self.mode == Mode::ECI {
result.push_str( result.push_str(
self.encoders &self.encoders
.getCharset(self.charsetEncoderIndex) .getCharset(self.charsetEncoderIndex)
.ok_or(fmt::Error)? .ok_or(fmt::Error)?
.name(), .getCharsetName(),
); );
} else { } else {
let sub_string: String = self let sub_string: String = self

View File

@@ -19,9 +19,6 @@
*/ */
use std::collections::HashMap; use std::collections::HashMap;
use encoding::EncodingRef;
use once_cell::sync::Lazy;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use crate::{ use crate::{
@@ -35,8 +32,8 @@ use crate::{
use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode}; use super::{mask_util, matrix_util, BlockPair, ByteMatrix, MinimalEncoder, QRCode};
static SHIFT_JIS_CHARSET: Lazy<EncodingRef> = static SHIFT_JIS_CHARSET: CharacterSetECI =
Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap()); CharacterSetECI::SJIS;
// The original table is defined in the table 5 of JISX0510:2004 (p.19). // The original table is defined in the table 5 of JISX0510:2004 (p.19).
const ALPHANUMERIC_TABLE: [i8; 96] = [ 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 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. // 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. // Basically it applies four rules and summate all penalties.
@@ -101,7 +98,7 @@ pub fn encode_with_hints(
if has_encoding_hint { if has_encoding_hint {
if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) { if let Some(EncodeHintValue::CharacterSet(v)) = hints.get(&EncodeHintType::CHARACTER_SET) {
encoding = 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 { let encoding = if let Some(encoding) = encoding {
encoding encoding
} else if let Ok(_encs) = } else if let Ok(_encs) =
DEFAULT_BYTE_MODE_ENCODING.encode(content, encoding::EncoderTrap::Strict) DEFAULT_BYTE_MODE_ENCODING.encode(content)
{ {
DEFAULT_BYTE_MODE_ENCODING DEFAULT_BYTE_MODE_ENCODING
} else { } else {
has_encoding_hint = true; 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 // 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 // Append ECI segment if applicable
if mode == Mode::BYTE && has_encoding_hint { if mode == Mode::BYTE && has_encoding_hint {
if let Some(eci) = CharacterSetECI::getCharacterSetECI(encoding) { appendECI(&encoding, &mut header_bits)?;
appendECI(&eci, &mut header_bits)?;
}
} }
// Append the FNC1 mode header for GS1 formatted data if applicable // 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 { 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; * 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}. * 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 { fn chooseModeWithEncoding(content: &str, encoding: CharacterSetECI) -> Mode {
if SHIFT_JIS_CHARSET.name() == encoding.name() && isOnlyDoubleByteKanji(content) { if SHIFT_JIS_CHARSET == encoding && isOnlyDoubleByteKanji(content) {
// Choose Kanji mode if all input are double-byte characters // Choose Kanji mode if all input are double-byte characters
return Mode::KANJI; return Mode::KANJI;
} }
@@ -337,7 +332,7 @@ fn chooseModeWithEncoding(content: &str, encoding: EncodingRef) -> Mode {
} }
pub fn isOnlyDoubleByteKanji(content: &str) -> bool { 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 byt
} else { } else {
return false; return false;
@@ -638,7 +633,7 @@ pub fn appendBytes(
content: &str, content: &str,
mode: Mode, mode: Mode,
bits: &mut BitArray, bits: &mut BitArray,
encoding: EncodingRef, encoding: CharacterSetECI,
) -> Result<()> { ) -> Result<()> {
match mode { match mode {
Mode::NUMERIC => appendNumericBytes(content, bits), Mode::NUMERIC => appendNumericBytes(content, bits),
@@ -725,9 +720,9 @@ pub fn appendAlphanumericBytes(content: &str, bits: &mut BitArray) -> Result<()>
Ok(()) 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 let bytes = encoding
.encode(content, encoding::EncoderTrap::Strict) .encode(content)
.map_err(|e| Exceptions::writer_with(format!("error {e}")))?; .map_err(|e| Exceptions::writer_with(format!("error {e}")))?;
for b in bytes { for b in bytes {
bits.appendBits(b as u32, 8)?; 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 sjis = &SHIFT_JIS_CHARSET;
let bytes = sjis let bytes = sjis
.encode(content, encoding::EncoderTrap::Strict) .encode(content)
.map_err(|e| Exceptions::writer_with(format!("error {e}")))?; .map_err(|e| Exceptions::writer_with(format!("error {e}")))?;
if bytes.len() % 2 != 0 { if bytes.len() % 2 != 0 {
return Err(Exceptions::writer_with("Kanji byte size not even")); return Err(Exceptions::writer_with("Kanji byte size not even"));

View File

@@ -23,9 +23,8 @@ use std::{
rc::Rc, rc::Rc,
}; };
use encoding::Encoding;
use rxing::{ use rxing::{
common::{HybridBinarizer, Result}, common::{HybridBinarizer, Result, CharacterSetECI},
multi::MultipleBarcodeReader, multi::MultipleBarcodeReader,
pdf417::PDF417RXingResultMetadata, pdf417::PDF417RXingResultMetadata,
BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue, BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue,
@@ -646,9 +645,7 @@ impl<T: MultipleBarcodeReader + Reader> PDF417MultiImageSpanAbstractBlackBoxTest
if ext == "bin" { if ext == "bin" {
let mut buffer: Vec<u8> = Vec::new(); let mut buffer: Vec<u8> = Vec::new();
File::open(&file)?.read_to_end(&mut buffer)?; File::open(&file)?.read_to_end(&mut buffer)?;
encoding::all::ISO_8859_1 CharacterSetECI::ISO8859_1.decode_replace(&buffer).expect("decode")
.decode(&buffer, encoding::DecoderTrap::Replace)
.expect("decode")
} else { } else {
read_to_string(&file).expect("ok") read_to_string(&file).expect("ok")
} }