mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
mostly passing
This commit is contained in:
@@ -35,7 +35,7 @@ use super::{AztecCode, HighLevelEncoder};
|
||||
*/
|
||||
|
||||
pub const DEFAULT_EC_PERCENT: u32 = 33; // default minimal percentage of error check words
|
||||
pub const DEFAULT_AZTEC_LAYERS: u32 = 0;
|
||||
pub const DEFAULT_AZTEC_LAYERS: i32 = 0;
|
||||
pub const MAX_NB_BITS: u32 = 32;
|
||||
pub const MAX_NB_BITS_COMPACT: u32 = 4;
|
||||
|
||||
@@ -69,7 +69,7 @@ pub fn encode_simple(data: &str) -> Result<AztecCode, Exceptions> {
|
||||
pub fn encode(
|
||||
data: &str,
|
||||
minECCPercent: u32,
|
||||
userSpecifiedLayers: u32,
|
||||
userSpecifiedLayers: i32,
|
||||
) -> Result<AztecCode, Exceptions> {
|
||||
let bytes = encoding::all::ISO_8859_1
|
||||
.encode(data, encoding::EncoderTrap::Strict)
|
||||
@@ -92,12 +92,12 @@ pub fn encode(
|
||||
pub fn encode_with_charset(
|
||||
data: &str,
|
||||
minECCPercent: u32,
|
||||
userSpecifiedLayers: u32,
|
||||
userSpecifiedLayers: i32,
|
||||
charset: &'static dyn encoding::Encoding,
|
||||
) -> Result<AztecCode, Exceptions> {
|
||||
let bytes = charset
|
||||
.encode(data, encoding::EncoderTrap::Replace)
|
||||
.unwrap(); //data.getBytes(null != charset ? charset : StandardCharsets.ISO_8859_1);
|
||||
.encode(data, encoding::EncoderTrap::Strict)
|
||||
.expect("must be encodeable"); //data.getBytes(null != charset ? charset : StandardCharsets.ISO_8859_1);
|
||||
encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ pub fn encode_bytes_simple(data: &[u8]) -> Result<AztecCode, Exceptions> {
|
||||
pub fn encode_bytes(
|
||||
data: &[u8],
|
||||
minECCPercent: u32,
|
||||
userSpecifiedLayers: u32,
|
||||
userSpecifiedLayers: i32,
|
||||
) -> Result<AztecCode, Exceptions> {
|
||||
encode_bytes_with_charset(
|
||||
data,
|
||||
@@ -146,24 +146,24 @@ pub fn encode_bytes(
|
||||
*/
|
||||
pub fn encode_bytes_with_charset(
|
||||
data: &[u8],
|
||||
minECCPercent: u32,
|
||||
userSpecifiedLayers: u32,
|
||||
min_eccpercent: u32,
|
||||
user_specified_layers: i32,
|
||||
charset: &'static dyn encoding::Encoding,
|
||||
) -> Result<AztecCode, Exceptions> {
|
||||
// High-level encode
|
||||
let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?;
|
||||
|
||||
// stuff bits and choose symbol size
|
||||
let eccBits = bits.getSize() as u32 * minECCPercent / 100 + 11;
|
||||
let totalSizeBits = bits.getSize() as u32 + eccBits;
|
||||
let ecc_bits = bits.getSize() as u32 * min_eccpercent / 100 + 11;
|
||||
let total_size_bits = bits.getSize() as u32 + ecc_bits;
|
||||
let mut compact;
|
||||
let mut layers;
|
||||
let mut totalBitsInLayerVar;
|
||||
let mut wordSize;
|
||||
let mut stuffedBits;
|
||||
if userSpecifiedLayers != DEFAULT_AZTEC_LAYERS {
|
||||
compact = userSpecifiedLayers < 0;
|
||||
layers = userSpecifiedLayers;
|
||||
let mut layers:u32;
|
||||
let mut total_bits_in_layer_var;
|
||||
let mut word_size;
|
||||
let mut stuffed_bits;
|
||||
if user_specified_layers != DEFAULT_AZTEC_LAYERS {
|
||||
compact = user_specified_layers < 0;
|
||||
layers = i32::abs(user_specified_layers) as u32;
|
||||
if layers
|
||||
> (if compact {
|
||||
MAX_NB_BITS_COMPACT
|
||||
@@ -173,27 +173,27 @@ pub fn encode_bytes_with_charset(
|
||||
{
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
"Illegal value {} for layers",
|
||||
userSpecifiedLayers
|
||||
user_specified_layers
|
||||
)));
|
||||
}
|
||||
totalBitsInLayerVar = totalBitsInLayer(layers, compact);
|
||||
wordSize = WORD_SIZE[layers as usize];
|
||||
let usableBitsInLayers = totalBitsInLayerVar - (totalBitsInLayerVar % wordSize);
|
||||
stuffedBits = stuffBits(&bits, wordSize as usize);
|
||||
if stuffedBits.getSize() as u32 + eccBits > usableBitsInLayers {
|
||||
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
|
||||
word_size = WORD_SIZE[layers as usize];
|
||||
let usable_bits_in_layers = total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
|
||||
stuffed_bits = stuffBits(&bits, word_size as usize);
|
||||
if stuffed_bits.getSize() as u32 + ecc_bits > usable_bits_in_layers {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"Data to large for user specified layer".to_owned(),
|
||||
));
|
||||
}
|
||||
if compact && stuffedBits.getSize() as u32 > wordSize * 64 {
|
||||
if compact && stuffed_bits.getSize() as u32 > word_size * 64 {
|
||||
// Compact format only allows 64 data words, though C4 can hold more words than that
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"Data to large for user specified layer".to_owned(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
wordSize = 0;
|
||||
stuffedBits = BitArray::new();
|
||||
word_size = 0;
|
||||
stuffed_bits = BitArray::new();
|
||||
// We look at the possible table sizes in the order Compact1, Compact2, Compact3,
|
||||
// Compact4, Normal4,... Normal(i) for i < 4 isn't typically used since Compact(i+1)
|
||||
// is the same size, but has more data.
|
||||
@@ -207,37 +207,38 @@ pub fn encode_bytes_with_charset(
|
||||
}
|
||||
compact = i <= 3;
|
||||
layers = if compact { i + 1 } else { i };
|
||||
totalBitsInLayerVar = totalBitsInLayer(layers, compact);
|
||||
if totalSizeBits > totalBitsInLayerVar as u32 {
|
||||
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
|
||||
if total_size_bits > total_bits_in_layer_var as u32 {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// [Re]stuff the bits if this is the first opportunity, or if the
|
||||
// wordSize has changed
|
||||
if stuffedBits.getSize() == 0 || wordSize != WORD_SIZE[layers as usize] {
|
||||
wordSize = WORD_SIZE[layers as usize];
|
||||
stuffedBits = stuffBits(&bits, wordSize as usize);
|
||||
if stuffed_bits.getSize() == 0 || word_size != WORD_SIZE[layers as usize] {
|
||||
word_size = WORD_SIZE[layers as usize];
|
||||
stuffed_bits = stuffBits(&bits, word_size as usize);
|
||||
}
|
||||
let usableBitsInLayers = totalBitsInLayerVar - (totalBitsInLayerVar % wordSize);
|
||||
if compact && stuffedBits.getSize() as u32 > wordSize * 64 {
|
||||
let usable_bits_in_layers =
|
||||
total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
|
||||
if compact && stuffed_bits.getSize() as u32 > word_size * 64 {
|
||||
// Compact format only allows 64 data words, though C4 can hold more words than that
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if stuffedBits.getSize() as u32 + eccBits <= usableBitsInLayers {
|
||||
if stuffed_bits.getSize() as u32 + ecc_bits <= usable_bits_in_layers {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
let message_bits = generateCheckWords(
|
||||
&stuffedBits,
|
||||
totalBitsInLayerVar as usize,
|
||||
wordSize as usize,
|
||||
&stuffed_bits,
|
||||
total_bits_in_layer_var as usize,
|
||||
word_size as usize,
|
||||
);
|
||||
|
||||
// generate mode message
|
||||
let messageSizeInWords = stuffedBits.getSize() as u32 / wordSize;
|
||||
let messageSizeInWords = stuffed_bits.getSize() as u32 / word_size;
|
||||
let modeMessage = generateModeMessage(compact, layers as u32, messageSizeInWords);
|
||||
|
||||
// allocate symbol
|
||||
@@ -541,7 +542,7 @@ pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
|
||||
return out;
|
||||
}
|
||||
|
||||
fn totalBitsInLayer(layers: u32, compact: bool) -> u32 {
|
||||
fn total_bits_in_layer(layers: u32, compact: bool) -> u32 {
|
||||
((if compact { 88 } else { 112 }) + 16 * layers) * layers
|
||||
// return ((compact ? 88 : 112) + 16 * layers) * layers;
|
||||
}
|
||||
|
||||
@@ -127,8 +127,8 @@ impl HighLevelEncoder {
|
||||
char_map[Self::MODE_DIGIT][b'.' as usize] = 13;
|
||||
let mixed_table = [
|
||||
'\0', ' ', '\u{1}', '\u{2}', '\u{3}', '\u{4}', '\u{5}', '\u{6}', '\u{7}', '\u{8}',
|
||||
'\t', '\n', '\u{13}', '\u{f}', '\r', '\u{33}', '\u{34}', '\u{35}', '\u{36}', '\u{37}',
|
||||
'@', '\\', '^', '_', '`', '|', '~', '\u{177}',
|
||||
'\t', '\n', '\u{000b}', '\u{000c}', '\r', '\u{001b}', '\u{001c}', '\u{001d}', '\u{001e}', '\u{001f}',
|
||||
'@', '\\', '^', '_', '`', '|', '~', '\u{007f}',
|
||||
];
|
||||
let mut i = 0;
|
||||
while i < mixed_table.len() {
|
||||
@@ -138,15 +138,15 @@ impl HighLevelEncoder {
|
||||
// for (int i = 0; i < mixedTable.length; i++) {
|
||||
// CHAR_MAP[MODE_MIXED][mixedTable[i]] = i;
|
||||
// }
|
||||
let punctTable = [
|
||||
let punct_table = [
|
||||
b'\0', b'\r', b'\0', b'\0', b'\0', b'\0', b'!', b'\'', b'#', b'$', b'%', b'&', b'\'',
|
||||
b'(', b')', b'*', b'+', b',', b'-', b'.', b'/', b':', b';', b'<', b'=', b'>', b'?',
|
||||
b'[', b']', b'{', b'}',
|
||||
];
|
||||
let mut i = 0;
|
||||
while i < punctTable.len() {
|
||||
if punctTable[i] > 0u8 {
|
||||
char_map[Self::MODE_PUNCT][punctTable[i] as usize] = i as u8;
|
||||
while i < punct_table.len() {
|
||||
if punct_table[i] > 0u8 {
|
||||
char_map[Self::MODE_PUNCT][punct_table[i] as usize] = i as u8;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
@@ -245,7 +245,7 @@ impl HighLevelEncoder {
|
||||
pub fn encode(&self) -> Result<BitArray, Exceptions> {
|
||||
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 eci != CharacterSetECI::ISO8859_1 {//} && eci != CharacterSetECI::Cp1252 {
|
||||
initial_state = initial_state.appendFLGn(CharacterSetECI::getValue(&eci))?;
|
||||
}
|
||||
} else {
|
||||
@@ -332,6 +332,8 @@ impl HighLevelEncoder {
|
||||
// }
|
||||
// });
|
||||
// Convert it to a bit array, and return.
|
||||
// dbg!(min_state.clone().toBitArray(&self.text).to_string());
|
||||
|
||||
Ok(min_state.toBitArray(&self.text))
|
||||
}
|
||||
|
||||
|
||||
@@ -85,8 +85,9 @@ impl State {
|
||||
// throw new IllegalArgumentException("ECI code must be between 0 and 999999");
|
||||
} else {
|
||||
let eci_digits = encoding::all::ISO_8859_1
|
||||
.encode(&format!("{}", eci), encoding::EncoderTrap::Replace)
|
||||
.encode(&format!("{}", eci), encoding::EncoderTrap::Strict)
|
||||
.unwrap();
|
||||
dbg!(format!("{}", eci));
|
||||
// let eciDigits = Integer.toString(eci).getBytes(StandardCharsets.ISO_8859_1);
|
||||
token.add(eci_digits.len() as i32, 3); // 1-6: number of ECI digits
|
||||
for eci_digit in &eci_digits {
|
||||
|
||||
Reference in New Issue
Block a user