move to using Eci enum

Decouples the Eci and CharacterSet concepts within the library. Character sets can now exist independently from Eci encodation schemes.
This commit is contained in:
Henry Schimke
2023-03-04 14:13:50 -06:00
parent 15859b9f10
commit 8a0744e534
21 changed files with 403 additions and 182 deletions

View File

@@ -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,

View File

@@ -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<String> {
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

View File

@@ -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(

View File

@@ -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<Self> {
pub fn appendFLGn(self, eci: Eci) -> Result<Self> {
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",
));

View File

@@ -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(),
);
}

View File

@@ -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<CharacterSet> {
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<CharacterSet> {
// 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<Vec<u8>> {
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<Vec<u8>> {

181
src/common/eci.rs Normal file
View File

@@ -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<u32> for Eci {
fn from(value: u32) -> Self {
(value as i32).into()
}
}
impl From<i32> 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<CharacterSet> 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<Eci> 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)
}
}

View File

@@ -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(),
// )

View File

@@ -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<i32>;
fn getECIValue(&self, index: usize) -> Result<Eci>;
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool>;
}

View File

@@ -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<u8>,
result: String,
current_charset: Option<CharacterSet>, //= 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 {

View File

@@ -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<i32> {
fn getECIValue(&self, index: usize) -> Result<Eci> {
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<bool> {
@@ -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();
}

View File

@@ -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")]

View File

@@ -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);

View File

@@ -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<bool> {
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)
}

View File

@@ -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<bool> {
self.internal.isFNC1(index)
}
fn getECIValue(&self, index: usize) -> Result<i32> {
fn getECIValue(&self, index: usize) -> Result<Eci> {
self.internal.getECIValue(index)
}
}

View File

@@ -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<DecoderRXingResult> {
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;

View File

@@ -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<T: ECIInput + ?Sized + 'static>(
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<i32> {
Ok(-1)
fn getECIValue(&self, _index: usize) -> Result<Eci> {
Ok(Eci::Unknown)
}
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool> {

View File

@@ -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

View File

@@ -28,7 +28,7 @@ use once_cell::sync::Lazy;
use super::QRCode;
static SHIFT_JIS_CHARSET: Lazy<CharacterSet> = Lazy::new(|| CharacterSet::SJIS);
static SHIFT_JIS_CHARSET: Lazy<CharacterSet> = Lazy::new(|| CharacterSet::Shift_JIS);
/**
* @author satorux@google.com (Satoru Takabayashi) - creator

View File

@@ -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(

View File

@@ -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)
}