mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
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:
@@ -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(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
181
src/common/eci.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
// )
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user