Class that converts a character string into a sequence of ECIs and bytes The implementation uses the Dijkstra algorithm to produce minimal encodings @author Alex Geller
| 28 | * @author Alex Geller |
| 29 | */ |
| 30 | public class MinimalECIInput implements ECIInput { |
| 31 | |
| 32 | private static final int COST_PER_ECI = 3; // approximated (latch + 2 codewords) |
| 33 | private final int[] bytes; |
| 34 | private final int fnc1; |
| 35 | |
| 36 | /** |
| 37 | * Constructs a minimal input |
| 38 | * |
| 39 | * @param stringToEncode the character string to encode |
| 40 | * @param priorityCharset The preferred {@link Charset}. When the value of the argument is null, the algorithm |
| 41 | * chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority |
| 42 | * charset to encode any character in the input that can be encoded by it if the charset is among the |
| 43 | * supported charsets. |
| 44 | * @param fnc1 denotes the character in the input that represents the FNC1 character or -1 if this is not GS1 |
| 45 | * input. |
| 46 | */ |
| 47 | public MinimalECIInput(String stringToEncode, Charset priorityCharset, int fnc1) { |
| 48 | this.fnc1 = fnc1; |
| 49 | ECIEncoderSet encoderSet = new ECIEncoderSet(stringToEncode, priorityCharset, fnc1); |
| 50 | if (encoderSet.length() == 1) { //optimization for the case when all can be encoded without ECI in ISO-8859-1 |
| 51 | bytes = new int[stringToEncode.length()]; |
| 52 | for (int i = 0; i < bytes.length; i++) { |
| 53 | char c = stringToEncode.charAt(i); |
| 54 | bytes[i] = c == fnc1 ? 1000 : (int) c; |
| 55 | } |
| 56 | } else { |
| 57 | bytes = encodeMinimally(stringToEncode, encoderSet, fnc1); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public int getFNC1Character() { |
| 62 | return fnc1; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the length of this input. The length is the number |
| 67 | * of {@code byte}s, FNC1 characters or ECIs in the sequence. |
| 68 | * |
| 69 | * @return the number of {@code char}s in this sequence |
| 70 | */ |
| 71 | public int length() { |
| 72 | return bytes.length; |
| 73 | } |
| 74 | |
| 75 | public boolean haveNCharacters(int index, int n) { |
| 76 | if (index + n - 1 >= bytes.length) { |
| 77 | return false; |
| 78 | } |
| 79 | for (int i = 0; i < n; i++) { |
| 80 | if (isECI(index + i)) { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected