JLChen
2021-10-28 e96683081abd5c1a94608dd33d092d8f45371cd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright 2013 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#import "ZXDataMatrixASCIIEncoder.h"
#import "ZXDataMatrixBase256Encoder.h"
#import "ZXDataMatrixC40Encoder.h"
#import "ZXDataMatrixEdifactEncoder.h"
#import "ZXDataMatrixEncoderContext.h"
#import "ZXDataMatrixHighLevelEncoder.h"
#import "ZXDataMatrixSymbolInfo.h"
#import "ZXDataMatrixTextEncoder.h"
#import "ZXDataMatrixX12Encoder.h"
 
/**
 * Padding character
 */
const unichar PAD_CHAR = 129;
 
/**
 * 05 Macro header
 */
static NSString *MACRO_05_HEADER = nil;
 
/**
 * 06 Macro header
 */
static NSString *MACRO_06_HEADER = nil;
 
/**
 * Macro trailer
 */
static NSString *MACRO_TRAILER = nil;
 
@implementation ZXDataMatrixHighLevelEncoder
 
+ (void)initialize {
  if ([self class] != [ZXDataMatrixHighLevelEncoder class]) return;
 
  MACRO_05_HEADER = [[NSString alloc] initWithFormat:@"[)>%C05%C", (unichar)0x001E, (unichar)0x001D];
  MACRO_06_HEADER = [[NSString alloc] initWithFormat:@"[)>%C06%C", (unichar)0x001E, (unichar)0x001D];
  MACRO_TRAILER = [[NSString alloc] initWithFormat:@"%C%C", (unichar)0x001E, (unichar)0x0004];
}
 
+ (unichar)latchToC40 {
  return 230;
}
 
+ (unichar)latchToBase256 {
  return 231;
}
 
+ (unichar)upperShift {
  return 235;
}
 
+ (unichar)macro05 {
  return 236;
}
 
+ (unichar)macro06 {
  return 237;
}
 
+ (unichar)latchToAnsiX12 {
  return 238;
}
 
+ (unichar)latchToText {
  return 239;
}
 
+ (unichar)latchToEdifact {
  return 240;
}
 
+ (unichar)c40Unlatch {
  return 254;
}
 
+ (unichar)x12Unlatch {
  return 254;
}
 
+ (int)asciiEncodation {
  return 0;
}
 
+ (int)c40Encodation {
  return 1;
}
 
+ (int)textEncodation {
  return 2;
}
 
+ (int)x12Encodation {
  return 3;
}
 
+ (int)edifactEncodation {
  return 4;
}
 
+ (int)base256Encodation {
  return 5;
}
 
/*
+ (int8_t *)bytesForMessage:(NSString *)msg {
  return (int8_t *)[[msg dataUsingEncoding:(NSStringEncoding) 0x80000400] bytes]; //See 4.4.3 and annex B of ISO/IEC 15438:2001(E)
}
*/
 
+ (unichar)randomize253State:(unichar)ch codewordPosition:(int)codewordPosition {
  int pseudoRandom = ((149 * codewordPosition) % 253) + 1;
  int tempVariable = ch + pseudoRandom;
  return tempVariable <= 254 ? (unichar) tempVariable : (unichar) (tempVariable - 254);
}
 
+ (NSString *)encodeHighLevel:(NSString *)msg {
  return [self encodeHighLevel:msg shape:ZXDataMatrixSymbolShapeHintForceNone minSize:nil maxSize:nil];
}
 
+ (NSString *)encodeHighLevel:(NSString *)msg shape:(ZXDataMatrixSymbolShapeHint)shape
                      minSize:(ZXDimension *)minSize maxSize:(ZXDimension *)maxSize {
  //the codewords 0..255 are encoded as Unicode characters
  NSArray *encoders = @[[[ZXDataMatrixASCIIEncoder alloc] init],
                        [[ZXDataMatrixC40Encoder alloc] init],
                        [[ZXDataMatrixTextEncoder alloc] init],
                        [[ZXDataMatrixX12Encoder alloc] init],
                        [[ZXDataMatrixEdifactEncoder alloc] init],
                        [[ZXDataMatrixBase256Encoder alloc] init]];
 
  ZXDataMatrixEncoderContext *context = [[ZXDataMatrixEncoderContext alloc] initWithMessage:msg];
  context.symbolShape = shape;
  [context setSizeConstraints:minSize maxSize:maxSize];
 
  if ([msg hasPrefix:MACRO_05_HEADER] && [msg hasSuffix:MACRO_TRAILER]) {
    [context writeCodeword:[self macro05]];
    [context setSkipAtEnd:2];
    context.pos += (int)MACRO_05_HEADER.length;
  } else if ([msg hasPrefix:MACRO_06_HEADER] && [msg hasSuffix:MACRO_TRAILER]) {
    [context writeCodeword:[self macro06]];
    [context setSkipAtEnd:2];
    context.pos += (int)MACRO_06_HEADER.length;
  }
 
  int encodingMode = [self asciiEncodation]; //Default mode
  while ([context hasMoreCharacters]) {
    [encoders[encodingMode] encode:context];
    if (context.newEncoding >= 0) {
      encodingMode = context.newEncoding;
      [context resetEncoderSignal];
    }
  }
  NSUInteger len = context.codewords.length;
  [context updateSymbolInfo];
  int capacity = context.symbolInfo.dataCapacity;
  if (len < capacity) {
    if (encodingMode != [self asciiEncodation] && encodingMode != [self base256Encodation]) {
      [context writeCodeword:(unichar)0x00fe]; //Unlatch (254)
    }
  }
  //Padding
  NSMutableString *codewords = context.codewords;
  if (codewords.length < capacity) {
    [codewords appendFormat:@"%C", PAD_CHAR];
  }
  while (codewords.length < capacity) {
    [codewords appendFormat:@"%C", [self randomize253State:PAD_CHAR codewordPosition:(int)codewords.length + 1]];
  }
 
  return [NSString stringWithString:context.codewords];
}
 
+ (int)lookAheadTest:(NSString *)msg startpos:(int)startpos currentMode:(int)currentMode {
  if (startpos >= msg.length) {
    return currentMode;
  }
  float charCounts[6];
  //step J
  if (currentMode == [self asciiEncodation]) {
    charCounts[0] = 0;
    charCounts[1] = 1;
    charCounts[2] = 1;
    charCounts[3] = 1;
    charCounts[4] = 1;
    charCounts[5] = 1.25f;
  } else {
    charCounts[0] = 1;
    charCounts[1] = 2;
    charCounts[2] = 2;
    charCounts[3] = 2;
    charCounts[4] = 2;
    charCounts[5] = 2.25f;
    charCounts[currentMode] = 0;
  }
 
  int charsProcessed = 0;
  while (YES) {
    //step K
    if ((startpos + charsProcessed) == msg.length) {
      int min = INT_MAX;
      int8_t mins[6];
      int intCharCounts[6];
      min = [self findMinimums:charCounts intCharCounts:intCharCounts min:min mins:mins];
      int minCount = [self minimumCount:mins];
 
      if (intCharCounts[[self asciiEncodation]] == min) {
        return [self asciiEncodation];
      }
      if (minCount == 1 && mins[[self base256Encodation]] > 0) {
        return [self base256Encodation];
      }
      if (minCount == 1 && mins[[self edifactEncodation]] > 0) {
        return [self edifactEncodation];
      }
      if (minCount == 1 && mins[[self textEncodation]] > 0) {
        return [self textEncodation];
      }
      if (minCount == 1 && mins[[self x12Encodation]] > 0) {
        return [self x12Encodation];
      }
      return [self c40Encodation];
    }
 
    unichar c = [msg characterAtIndex:startpos + charsProcessed];
    charsProcessed++;
 
    //step L
    if ([self isDigit:c]) {
      charCounts[[self asciiEncodation]] += 0.5;
    } else if ([self isExtendedASCII:c]) {
      charCounts[[self asciiEncodation]] = (int) ceil(charCounts[[self asciiEncodation]]);
      charCounts[[self asciiEncodation]] += 2;
    } else {
      charCounts[[self asciiEncodation]] = (int) ceil(charCounts[[self asciiEncodation]]);
      charCounts[[self asciiEncodation]]++;
    }
 
    //step M
    if ([self isNativeC40:c]) {
      charCounts[[self c40Encodation]] += 2.0f / 3.0f;
    } else if ([self isExtendedASCII:c]) {
      charCounts[[self c40Encodation]] += 8.0f / 3.0f;
    } else {
      charCounts[[self c40Encodation]] += 4.0f / 3.0f;
    }
 
    //step N
    if ([self isNativeText:c]) {
      charCounts[[self textEncodation]] += 2.0f / 3.0f;
    } else if ([self isExtendedASCII:c]) {
      charCounts[[self textEncodation]] += 8.0f / 3.0f;
    } else {
      charCounts[[self textEncodation]] += 4.0f / 3.0f;
    }
 
    //step O
    if ([self isNativeX12:c]) {
      charCounts[[self x12Encodation]] += 2.0f / 3.0f;
    } else if ([self isExtendedASCII:c]) {
      charCounts[[self x12Encodation]] += 13.0f / 3.0f;
    } else {
      charCounts[[self x12Encodation]] += 10.0f / 3.0f;
    }
 
    //step P
    if ([self isNativeEDIFACT:c]) {
      charCounts[[self edifactEncodation]] += 3.0f / 4.0f;
    } else if ([self isExtendedASCII:c]) {
      charCounts[[self edifactEncodation]] += 17.0f / 4.0f;
    } else {
      charCounts[[self edifactEncodation]] += 13.0f / 4.0f;
    }
 
    // step Q
    if ([self isSpecialB256:c]) {
      charCounts[[self base256Encodation]] += 4;
    } else {
      charCounts[[self base256Encodation]]++;
    }
 
    //step R
    if (charsProcessed >= 4) {
      int intCharCounts[6];
      int8_t mins[6];
      [self findMinimums:charCounts intCharCounts:intCharCounts min:INT_MAX mins:mins];
      int minCount = [self minimumCount:mins];
 
      if (intCharCounts[[self asciiEncodation]] < intCharCounts[[self base256Encodation]]
          && intCharCounts[[self asciiEncodation]] < intCharCounts[[self c40Encodation]]
          && intCharCounts[[self asciiEncodation]] < intCharCounts[[self textEncodation]]
          && intCharCounts[[self asciiEncodation]] < intCharCounts[[self x12Encodation]]
          && intCharCounts[[self asciiEncodation]] < intCharCounts[[self edifactEncodation]]) {
        return [self asciiEncodation];
      }
      if (intCharCounts[[self base256Encodation]] < intCharCounts[[self asciiEncodation]]
          || (mins[[self c40Encodation]] + mins[[self textEncodation]] + mins[[self x12Encodation]] + mins[[self edifactEncodation]]) == 0) {
        return [self base256Encodation];
      }
      if (minCount == 1 && mins[[self edifactEncodation]] > 0) {
        return [self edifactEncodation];
      }
      if (minCount == 1 && mins[[self textEncodation]] > 0) {
        return [self textEncodation];
      }
      if (minCount == 1 && mins[[self x12Encodation]] > 0) {
        return [self x12Encodation];
      }
      if (intCharCounts[[self c40Encodation]] + 1 < intCharCounts[[self asciiEncodation]]
          && intCharCounts[[self c40Encodation]] + 1 < intCharCounts[[self base256Encodation]]
          && intCharCounts[[self c40Encodation]] + 1 < intCharCounts[[self edifactEncodation]]
          && intCharCounts[[self c40Encodation]] + 1 < intCharCounts[[self textEncodation]]) {
        if (intCharCounts[[self c40Encodation]] < intCharCounts[[self x12Encodation]]) {
          return [self c40Encodation];
        }
        if (intCharCounts[[self c40Encodation]] == intCharCounts[[self x12Encodation]]) {
          int p = startpos + charsProcessed + 1;
          while (p < msg.length) {
            char tc = [msg characterAtIndex:p];
            if ([self isX12TermSep:tc]) {
              return [self x12Encodation];
            }
            if (![self isNativeX12:tc]) {
              break;
            }
            p++;
          }
          return [self c40Encodation];
        }
      }
    }
  }
}
 
+ (int)findMinimums:(float *)charCounts intCharCounts:(int *)intCharCounts min:(int)min mins:(int8_t *)mins {
  memset(mins, 0, 6);
  for (int i = 0; i < 6; i++) {
    intCharCounts[i] = (int) ceil(charCounts[i]);
    int current = intCharCounts[i];
    if (min > current) {
      min = current;
      memset(mins, 0, 6);
    }
    if (min == current) {
      mins[i]++;
    }
  }
  return min;
}
 
+ (int)minimumCount:(int8_t *)mins {
  int minCount = 0;
  for (int i = 0; i < 6; i++) {
    minCount += mins[i];
  }
  return minCount;
}
 
+ (BOOL)isDigit:(unichar)ch {
  return ch >= '0' && ch <= '9';
}
 
+ (BOOL)isExtendedASCII:(unichar)ch {
  return ch >= 128 && ch <= 255;
}
 
+ (BOOL)isNativeC40:(unichar)ch {
  return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z');
}
 
+ (BOOL)isNativeText:(unichar)ch {
  return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z');
}
 
+ (BOOL)isNativeX12:(unichar)ch {
  return [self isX12TermSep:ch] || (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z');
}
 
+ (BOOL)isX12TermSep:(unichar)ch {
  return (ch == '\r') //CR
    || (ch == '*')
    || (ch == '>');
}
 
+ (BOOL)isNativeEDIFACT:(unichar)ch {
  return ch >= ' ' && ch <= '^';
}
 
+ (BOOL)isSpecialB256:(unichar)ch {
  return NO; //TODO NOT IMPLEMENTED YET!!!
}
 
+ (int)determineConsecutiveDigitCount:(NSString *)msg startpos:(int)startpos {
  int count = 0;
  NSUInteger len = msg.length;
  int idx = startpos;
  if (idx < len) {
    unichar ch = [msg characterAtIndex:idx];
    while ([self isDigit:ch] && idx < len) {
      count++;
      idx++;
      if (idx < len) {
        ch = [msg characterAtIndex:idx];
      }
    }
  }
  return count;
}
 
+ (void)illegalCharacter:(unichar)c {
  NSString *hex = [NSString stringWithFormat:@"%x", c];
  hex = [[@"0000" substringWithRange:NSMakeRange(0, hex.length)] stringByAppendingString:hex];
  [NSException raise:NSInvalidArgumentException format:@"Illegal character: %C (0x%@)", c, hex];
}
 
@end