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

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

View File

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

View File

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

View File

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

View File

@@ -17,7 +17,7 @@
use std::collections::HashMap;
use crate::{
common::{BitMatrix, Result},
common::{BitMatrix, Result, CharacterSetECI},
BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer,
};
@@ -108,7 +108,7 @@ impl Writer for PDF417Writer {
if let Some(EncodeHintValue::CharacterSet(cs)) =
hints.get(&EncodeHintType::CHARACTER_SET)
{
encoder.setEncoding(encoding::label::encoding_from_whatwg_label(cs));
encoder.setEncoding(CharacterSetECI::getCharacterSetECIByName(cs));
}
if let Some(EncodeHintValue::Pdf417AutoEci(auto_eci_str)) =
hints.get(&EncodeHintType::PDF417_AUTO_ECI)