repurpose CharacterSetECI as encoding abstraction

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

View File

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

View File

@@ -16,10 +16,8 @@
use std::collections::HashMap;
use encoding::EncodingRef;
use crate::{
common::{BitMatrix, Result},
common::{BitMatrix, Result, CharacterSetECI},
exceptions::Exceptions,
BarcodeFormat, EncodeHintType, EncodeHintValue, Writer,
};
@@ -58,10 +56,7 @@ impl Writer for AztecWriter {
hints.get(&EncodeHintType::CHARACTER_SET)
{
if cset_name.to_lowercase() != "iso-8859-1" {
charset = Some(
encoding::label::encoding_from_whatwg_label(cset_name)
.ok_or(Exceptions::ILLEGAL_ARGUMENT)?,
);
charset = CharacterSetECI::getCharacterSetECIByName(cset_name);
}
}
if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) =
@@ -91,7 +86,7 @@ fn encode(
format: BarcodeFormat,
width: u32,
height: u32,
charset: Option<EncodingRef>,
charset: Option<CharacterSetECI>,
ecc_percent: u32,
layers: i32,
) -> 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
// when character encoding changes (ECI) or input ends.
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;
@@ -161,8 +161,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
// flush bytes, FLG changes state
result.push_str(
&encdr
.decode(&decoded_bytes, encoding::DecoderTrap::Strict)
.map_err(Exceptions::illegal_state_with)?,
.decode(&decoded_bytes)?,
);
decoded_bytes.clear();
@@ -190,7 +189,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
if charset_eci.is_err() {
return Err(Exceptions::format_with("Charset must exist"));
}
encdr = CharacterSetECI::getCharset(&charset_eci?);
encdr = charset_eci?;
}
}
// Go back to whatever mode we had been in
@@ -207,7 +206,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
}
} else {
// Though stored as a table of strings for convenience, codes actually represent 1 or 2 *bytes*.
// let b = encoding::all::ASCII.encode(str, encoding::EncoderTrap::Strict).unwrap();
let b = str.as_bytes();
//let b = str.getBytes(StandardCharsets.US_ASCII);
//decodedBytes.write(b, 0, b.length);
@@ -220,7 +219,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
}
}
//try {
if let Ok(str) = encdr.decode(&decoded_bytes, encoding::DecoderTrap::Strict) {
if let Ok(str) = encdr.decode(&decoded_bytes) {
result.push_str(&str);
} else {
return Err(Exceptions::illegal_state_with("bad encoding"));

View File

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

View File

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

View File

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