Merge pull request #4 from ftiercelin/ISS1761

Refactor PDF417HighLevelEncoder to avoid code duplication
This commit is contained in:
François Tiercelin 2024-10-22 09:33:21 +01:00 committed by GitHub
commit cfba48be84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 14 deletions

View file

@ -176,24 +176,13 @@ final class PDF417HighLevelEncoder {
}
if (Compaction.TEXT == compaction) {
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) > 255) {
throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " +
(int) msg.charAt(i) +
"). Consider specifying Compaction.AUTO instead of Compaction.TEXT");
}
}
checkCharset(msg,255,"Consider specifying Compaction.AUTO instead of Compaction.TEXT");
}
if (encoding == null && !autoECI) {
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) > 255) {
throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " +
(int) msg.charAt(i) +
"). Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET.");
}
}
checkCharset(msg,255,"Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET.");
}
//the codewords 0..928 are encoded as Unicode characters
StringBuilder sb = new StringBuilder(msg.length());
@ -293,6 +282,22 @@ final class PDF417HighLevelEncoder {
return sb.toString();
}
/**
* Check if input is only made of characters between 0 and the upper limit
* @param input the input
* @param max the upper limit for charset
* @param errorMessage the message to explain the error
* @throws WriterException exception highlighting the offending character and a suggestion to avoid the error
*/
protected static void checkCharset(String input,int max, String errorMessage) throws WriterException {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) > max) {
throw new WriterException("Non-encodable character detected: " + input.charAt(i) + " (Unicode: " +
(int) input.charAt(i) + "). " + errorMessage);
}
}
}
/**
* Encode parts of the message using Text Compaction as described in ISO/IEC 15438:2001(E),

View file

@ -18,6 +18,7 @@ package com.google.zxing.pdf417.encoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
@ -78,6 +79,26 @@ public final class PDF417EncoderTestCase extends Assert {
assertTrue(e.getMessage().contains("Compaction.AUTO"));
}
}
@Test
public void testCheckCharset() throws Exception {
String input = "Hello!";
String errorMessage = UUID.randomUUID().toString();
// no exception
PDF417HighLevelEncoder.checkCharset(input,255,errorMessage);
PDF417HighLevelEncoder.checkCharset(input,1255,errorMessage);
PDF417HighLevelEncoder.checkCharset(input,111,errorMessage);
try {
// should throw an exception for character 'o' because it exceeds upper limit 110
PDF417HighLevelEncoder.checkCharset(input,110,errorMessage);
} catch (WriterException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("111"));
assertTrue(e.getMessage().contains(errorMessage));
}
}
@Test
public void testEncodeIso88591WithSpecialChars() throws Exception {