Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "ZXDataMatrixEncoderContext.h"
  17. #import "ZXDataMatrixSymbolInfo.h"
  18. @interface ZXDataMatrixEncoderContext ()
  19. @property (nonatomic, strong) ZXDimension *maxSize;
  20. @property (nonatomic, strong) ZXDimension *minSize;
  21. @end
  22. @implementation ZXDataMatrixEncoderContext
  23. - (id)initWithMessage:(NSString *)msg {
  24. if (self = [super init]) {
  25. //From this point on Strings are not Unicode anymore!
  26. NSData *msgData = [msg dataUsingEncoding:NSISOLatin1StringEncoding];
  27. if (!msgData) {
  28. [NSException raise:NSInvalidArgumentException format:@"Message contains characters outside ISO-8859-1 encoding."];
  29. }
  30. const char *msgBinary = [msgData bytes];
  31. NSMutableString *sb = [NSMutableString string];
  32. for (int i = 0, c = (int)msg.length; i < c; i++) {
  33. unichar ch = (unichar) (msgBinary[i] & 0xff);
  34. [sb appendFormat:@"%C", ch];
  35. }
  36. _message = [[NSString alloc] initWithString:sb];
  37. _symbolShape = ZXDataMatrixSymbolShapeHintForceNone;
  38. _codewords = [[NSMutableString alloc] initWithCapacity:msg.length];
  39. _newEncoding = -1;
  40. }
  41. return self;
  42. }
  43. - (void)setSizeConstraints:(ZXDimension *)minSize maxSize:(ZXDimension *)maxSize {
  44. self.minSize = minSize;
  45. self.maxSize = maxSize;
  46. }
  47. - (unichar)currentChar {
  48. return [self.message characterAtIndex:self.pos];
  49. }
  50. - (unichar)current {
  51. return [self.message characterAtIndex:self.pos];
  52. }
  53. - (void)writeCodewords:(NSString *)codewords {
  54. [self.codewords appendString:codewords];
  55. }
  56. - (void)writeCodeword:(unichar)codeword {
  57. [self.codewords appendFormat:@"%C", codeword];
  58. }
  59. - (int)codewordCount {
  60. return (int)self.codewords.length;
  61. }
  62. - (void)signalEncoderChange:(int)encoding {
  63. self.newEncoding = encoding;
  64. }
  65. - (void)resetEncoderSignal {
  66. self.newEncoding = -1;
  67. }
  68. - (BOOL)hasMoreCharacters {
  69. return self.pos < [self totalMessageCharCount];
  70. }
  71. - (int)totalMessageCharCount {
  72. return (int)self.message.length - self.skipAtEnd;
  73. }
  74. - (int)remainingCharacters {
  75. return [self totalMessageCharCount] - self.pos;
  76. }
  77. - (void)updateSymbolInfo {
  78. [self updateSymbolInfoWithLength:[self codewordCount]];
  79. }
  80. - (void)updateSymbolInfoWithLength:(int)len {
  81. if (self.symbolInfo == nil || len > self.symbolInfo.dataCapacity) {
  82. self.symbolInfo = [ZXDataMatrixSymbolInfo lookup:len shape:self.symbolShape minSize:self.minSize maxSize:self.maxSize fail:YES];
  83. }
  84. }
  85. - (void)resetSymbolInfo {
  86. self.symbolInfo = nil;
  87. }
  88. @end