| 36 | } |
| 37 | |
| 38 | export class ECIEncoderSet { |
| 39 | private readonly ENCODERS: CharsetEncoder[] = [ |
| 40 | 'IBM437', |
| 41 | 'ISO-8859-2', |
| 42 | 'ISO-8859-3', |
| 43 | 'ISO-8859-4', |
| 44 | 'ISO-8859-5', |
| 45 | 'ISO-8859-6', |
| 46 | 'ISO-8859-7', |
| 47 | 'ISO-8859-8', |
| 48 | 'ISO-8859-9', |
| 49 | 'ISO-8859-10', |
| 50 | 'ISO-8859-11', |
| 51 | 'ISO-8859-13', |
| 52 | 'ISO-8859-14', |
| 53 | 'ISO-8859-15', |
| 54 | 'ISO-8859-16', |
| 55 | 'windows-1250', |
| 56 | 'windows-1251', |
| 57 | 'windows-1252', |
| 58 | 'windows-1256', |
| 59 | 'Shift_JIS', |
| 60 | ].map(name => new CharsetEncoder(Charset.forName(name))); |
| 61 | private encoders: CharsetEncoder[] = []; |
| 62 | private priorityEncoderIndex: number; |
| 63 | |
| 64 | /** |
| 65 | * Constructs an encoder set |
| 66 | * |
| 67 | * @param stringToEncode the string that needs to be encoded |
| 68 | * @param priorityCharset The preferred {@link Charset} or null. |
| 69 | * @param fnc1 fnc1 denotes the character in the input that represents the FNC1 character or -1 for a non-GS1 bar |
| 70 | * code. When specified, it is considered an error to pass it as argument to the methods canEncode() or encode(). |
| 71 | */ |
| 72 | constructor(stringToEncode: string, priorityCharset: Charset, fnc1: number) { |
| 73 | const neededEncoders: CharsetEncoder[] = []; |
| 74 | |
| 75 | // we always need the ISO-8859-1 encoder. It is the default encoding |
| 76 | neededEncoders.push(new CharsetEncoder(StandardCharsets.ISO_8859_1)); |
| 77 | let needUnicodeEncoder = |
| 78 | priorityCharset != null && priorityCharset.name.startsWith('UTF'); |
| 79 | |
| 80 | // Walk over the input string and see if all characters can be encoded with the list of encoders |
| 81 | for (let i = 0; i < stringToEncode.length; i++) { |
| 82 | let canEncode = false; |
| 83 | for (const encoder of neededEncoders) { |
| 84 | const singleCharacter = stringToEncode.charAt(i); |
| 85 | const c = singleCharacter.charCodeAt(0); |
| 86 | |
| 87 | if (c === fnc1 || encoder.canEncode(singleCharacter)) { |
| 88 | canEncode = true; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (!canEncode) { |
| 94 | // for the character at position i we don't yet have an encoder in the list |
| 95 | for (const encoder of this.ENCODERS) { |