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.

ZXDataMatrixSymbolInfo.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "ZXDataMatrixSymbolInfo.h"
  17. #import "ZXDataMatrixSymbolInfo144.h"
  18. #import "ZXDimension.h"
  19. static NSArray *PROD_SYMBOLS = nil;
  20. static NSArray *symbols = nil;
  21. @implementation ZXDataMatrixSymbolInfo
  22. + (void)initialize {
  23. if ([self class] != [ZXDataMatrixSymbolInfo class]) return;
  24. PROD_SYMBOLS = @[[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:3 errorCodewords:5 matrixWidth:8 matrixHeight:8 dataRegions:1],
  25. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:5 errorCodewords:7 matrixWidth:10 matrixHeight:10 dataRegions:1],
  26. /*rect*/[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:YES dataCapacity:5 errorCodewords:7 matrixWidth:16 matrixHeight:6 dataRegions:1],
  27. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:8 errorCodewords:10 matrixWidth:12 matrixHeight:12 dataRegions:1],
  28. /*rect*/[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:YES dataCapacity:10 errorCodewords:11 matrixWidth:14 matrixHeight:6 dataRegions:2],
  29. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:12 errorCodewords:12 matrixWidth:14 matrixHeight:14 dataRegions:1],
  30. /*rect*/[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:YES dataCapacity:16 errorCodewords:14 matrixWidth:24 matrixHeight:10 dataRegions:1],
  31. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:18 errorCodewords:14 matrixWidth:16 matrixHeight:16 dataRegions:1],
  32. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:22 errorCodewords:18 matrixWidth:18 matrixHeight:18 dataRegions:1],
  33. /*rect*/[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:YES dataCapacity:22 errorCodewords:18 matrixWidth:16 matrixHeight:10 dataRegions:2],
  34. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:30 errorCodewords:20 matrixWidth:20 matrixHeight:20 dataRegions:1],
  35. /*rect*/[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:YES dataCapacity:32 errorCodewords:24 matrixWidth:16 matrixHeight:14 dataRegions:2],
  36. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:36 errorCodewords:24 matrixWidth:22 matrixHeight:22 dataRegions:1],
  37. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:44 errorCodewords:28 matrixWidth:24 matrixHeight:24 dataRegions:1],
  38. /*rect*/[[ZXDataMatrixSymbolInfo alloc] initWithRectangular:YES dataCapacity:49 errorCodewords:28 matrixWidth:22 matrixHeight:14 dataRegions:2],
  39. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:62 errorCodewords:36 matrixWidth:14 matrixHeight:14 dataRegions:4],
  40. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:86 errorCodewords:42 matrixWidth:16 matrixHeight:16 dataRegions:4],
  41. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:114 errorCodewords:48 matrixWidth:18 matrixHeight:18 dataRegions:4],
  42. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:144 errorCodewords:56 matrixWidth:20 matrixHeight:20 dataRegions:4],
  43. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:174 errorCodewords:68 matrixWidth:22 matrixHeight:22 dataRegions:4],
  44. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:204 errorCodewords:84 matrixWidth:24 matrixHeight:24 dataRegions:4 rsBlockData:102 rsBlockError:42],
  45. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:280 errorCodewords:112 matrixWidth:14 matrixHeight:14 dataRegions:16 rsBlockData:140 rsBlockError:56],
  46. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:368 errorCodewords:144 matrixWidth:16 matrixHeight:16 dataRegions:16 rsBlockData:92 rsBlockError:36],
  47. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:456 errorCodewords:192 matrixWidth:18 matrixHeight:18 dataRegions:16 rsBlockData:114 rsBlockError:48],
  48. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:576 errorCodewords:224 matrixWidth:20 matrixHeight:20 dataRegions:16 rsBlockData:144 rsBlockError:56],
  49. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:696 errorCodewords:272 matrixWidth:22 matrixHeight:22 dataRegions:16 rsBlockData:174 rsBlockError:68],
  50. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:816 errorCodewords:336 matrixWidth:24 matrixHeight:24 dataRegions:16 rsBlockData:136 rsBlockError:56],
  51. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:1050 errorCodewords:408 matrixWidth:18 matrixHeight:18 dataRegions:36 rsBlockData:175 rsBlockError:68],
  52. [[ZXDataMatrixSymbolInfo alloc] initWithRectangular:NO dataCapacity:1304 errorCodewords:496 matrixWidth:20 matrixHeight:20 dataRegions:36 rsBlockData:163 rsBlockError:62],
  53. [[ZXDataMatrixSymbolInfo144 alloc] init]];
  54. symbols = PROD_SYMBOLS;
  55. }
  56. + (NSArray *)prodSymbols {
  57. return PROD_SYMBOLS;
  58. }
  59. + (void)overrideSymbolSet:(NSArray *)override {
  60. symbols = override;
  61. }
  62. - (id)initWithRectangular:(BOOL)rectangular dataCapacity:(int)dataCapacity errorCodewords:(int)errorCodewords
  63. matrixWidth:(int)matrixWidth matrixHeight:(int)matrixHeight dataRegions:(int)dataRegions {
  64. return [self initWithRectangular:rectangular dataCapacity:dataCapacity errorCodewords:errorCodewords
  65. matrixWidth:matrixWidth matrixHeight:matrixHeight dataRegions:dataRegions
  66. rsBlockData:dataCapacity rsBlockError:errorCodewords];
  67. }
  68. - (id)initWithRectangular:(BOOL)rectangular dataCapacity:(int)dataCapacity errorCodewords:(int)errorCodewords
  69. matrixWidth:(int)matrixWidth matrixHeight:(int)matrixHeight dataRegions:(int)dataRegions
  70. rsBlockData:(int)rsBlockData rsBlockError:(int)rsBlockError {
  71. if (self = [super init]) {
  72. _rectangular = rectangular;
  73. _dataCapacity = dataCapacity;
  74. _errorCodewords = errorCodewords;
  75. _matrixWidth = matrixWidth;
  76. _matrixHeight = matrixHeight;
  77. _dataRegions = dataRegions;
  78. _rsBlockData = rsBlockData;
  79. _rsBlockError = rsBlockError;
  80. }
  81. return self;
  82. }
  83. + (ZXDataMatrixSymbolInfo *)lookup:(int)dataCodewords {
  84. return [self lookup:dataCodewords shape:ZXDataMatrixSymbolShapeHintForceNone fail:YES];
  85. }
  86. + (ZXDataMatrixSymbolInfo *)lookup:(int)dataCodewords shape:(ZXDataMatrixSymbolShapeHint)shape {
  87. return [self lookup:dataCodewords shape:shape fail:YES];
  88. }
  89. + (ZXDataMatrixSymbolInfo *)lookup:(int)dataCodewords allowRectangular:(BOOL)allowRectangular fail:(BOOL)fail {
  90. ZXDataMatrixSymbolShapeHint shape = allowRectangular
  91. ? ZXDataMatrixSymbolShapeHintForceNone : ZXDataMatrixSymbolShapeHintForceSquare;
  92. return [self lookup:dataCodewords shape:shape fail:fail];
  93. }
  94. + (ZXDataMatrixSymbolInfo *)lookup:(int)dataCodewords shape:(ZXDataMatrixSymbolShapeHint)shape fail:(BOOL)fail {
  95. return [self lookup:dataCodewords shape:shape minSize:nil maxSize:nil fail:fail];
  96. }
  97. + (ZXDataMatrixSymbolInfo *)lookup:(int)dataCodewords shape:(ZXDataMatrixSymbolShapeHint)shape minSize:(ZXDimension *)minSize
  98. maxSize:(ZXDimension *)maxSize fail:(BOOL)fail {
  99. for (ZXDataMatrixSymbolInfo *symbol in symbols) {
  100. if (shape == ZXDataMatrixSymbolShapeHintForceSquare && symbol.rectangular) {
  101. continue;
  102. }
  103. if (shape == ZXDataMatrixSymbolShapeHintForceRectangle && !symbol.rectangular) {
  104. continue;
  105. }
  106. if (minSize != nil
  107. && ([symbol symbolWidth] < minSize.width
  108. || [symbol symbolHeight] < minSize.height)) {
  109. continue;
  110. }
  111. if (maxSize != nil
  112. && ([symbol symbolWidth] > maxSize.width
  113. || [symbol symbolHeight] > maxSize.height)) {
  114. continue;
  115. }
  116. if (dataCodewords <= symbol.dataCapacity) {
  117. return symbol;
  118. }
  119. }
  120. if (fail) {
  121. [NSException raise:NSInvalidArgumentException format:@"Can't find a symbol arrangement that matches the message. Data codewords: %d", dataCodewords];
  122. }
  123. return nil;
  124. }
  125. - (int)horizontalDataRegions {
  126. switch (_dataRegions) {
  127. case 1:
  128. return 1;
  129. case 2:
  130. return 2;
  131. case 4:
  132. return 2;
  133. case 16:
  134. return 4;
  135. case 36:
  136. return 6;
  137. default:
  138. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Cannot handle this number of data regions" userInfo:nil];
  139. }
  140. }
  141. - (int)verticalDataRegions {
  142. switch (_dataRegions) {
  143. case 1:
  144. return 1;
  145. case 2:
  146. return 1;
  147. case 4:
  148. return 2;
  149. case 16:
  150. return 4;
  151. case 36:
  152. return 6;
  153. default:
  154. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Cannot handle this number of data regions" userInfo:nil];
  155. }
  156. }
  157. - (int)symbolDataWidth {
  158. return [self horizontalDataRegions] * _matrixWidth;
  159. }
  160. - (int)symbolDataHeight {
  161. return [self verticalDataRegions] * _matrixHeight;
  162. }
  163. - (int)symbolWidth {
  164. return [self symbolDataWidth] + ([self horizontalDataRegions] * 2);
  165. }
  166. - (int)symbolHeight {
  167. return [self symbolDataHeight] + ([self verticalDataRegions] * 2);
  168. }
  169. - (int)codewordCount {
  170. return _dataCapacity + _errorCodewords;
  171. }
  172. - (int)interleavedBlockCount {
  173. return _dataCapacity / _rsBlockData;
  174. }
  175. - (int)dataLengthForInterleavedBlock:(int)index {
  176. return _rsBlockData;
  177. }
  178. - (int)errorLengthForInterleavedBlock:(int)index {
  179. return _rsBlockError;
  180. }
  181. - (NSString *)description {
  182. NSMutableString *sb = [NSMutableString string];
  183. [sb appendString:_rectangular ? @"Rectangular Symbol:" : @"Square Symbol:"];
  184. [sb appendFormat:@" data region %dx%d", _matrixWidth, _matrixHeight];
  185. [sb appendFormat:@", symbol size %dx%d", [self symbolWidth], [self symbolHeight]];
  186. [sb appendFormat:@", symbol data size %dx%d", [self symbolDataWidth], [self symbolDataHeight]];
  187. [sb appendFormat:@", codewords %d+%d", _dataCapacity, _errorCodewords];
  188. return [NSString stringWithString:sb];
  189. }
  190. @end