Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ZXDataMatrixHighLevelEncoder.m 12KB

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