mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
repurpose CharacterSetECI as encoding abstraction
This commit is contained in:
@@ -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()?;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user