Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2014 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 "ZXAztecHighLevelEncoder.h"
  17. #import "ZXAztecState.h"
  18. #import "ZXAztecToken.h"
  19. #import "ZXBitArray.h"
  20. #import "ZXByteArray.h"
  21. @implementation ZXAztecState
  22. - (id)initWithToken:(ZXAztecToken *)token mode:(int)mode binaryBytes:(int)binaryBytes bitCount:(int)bitCount {
  23. if (self = [super init]) {
  24. _token = token;
  25. _mode = mode;
  26. _binaryShiftByteCount = binaryBytes;
  27. _bitCount = bitCount;
  28. }
  29. return self;
  30. }
  31. + (ZXAztecState *)initialState {
  32. return [[ZXAztecState alloc] initWithToken:[ZXAztecToken empty] mode:ZX_AZTEC_MODE_UPPER binaryBytes:0 bitCount:0];
  33. }
  34. // Create a new state representing this state with a latch to a (not
  35. // necessary different) mode, and then a code.
  36. - (ZXAztecState *)latchAndAppend:(int)mode value:(int)value {
  37. int bitCount = self.bitCount;
  38. ZXAztecToken *token = self.token;
  39. if (mode != self.mode) {
  40. int latch = ZX_AZTEC_LATCH_TABLE[self.mode][mode];
  41. token = [token add:latch & 0xFFFF bitCount:latch >> 16];
  42. bitCount += latch >> 16;
  43. }
  44. int latchModeBitCount = mode == ZX_AZTEC_MODE_DIGIT ? 4 : 5;
  45. token = [token add:value bitCount:latchModeBitCount];
  46. return [[ZXAztecState alloc] initWithToken:token mode:mode binaryBytes:0 bitCount:bitCount + latchModeBitCount];
  47. }
  48. // Create a new state representing this state, with a temporary shift
  49. // to a different mode to output a single value.
  50. - (ZXAztecState *)shiftAndAppend:(int)mode value:(int)value {
  51. //assert binaryShiftByteCount == 0 && this.mode != mode;
  52. ZXAztecToken *token = self.token;
  53. int thisModeBitCount = self.mode == ZX_AZTEC_MODE_DIGIT ? 4 : 5;
  54. // Shifts exist only to UPPER and PUNCT, both with tokens size 5.
  55. token = [token add:ZX_AZTEC_SHIFT_TABLE[self.mode][mode] bitCount:thisModeBitCount];
  56. token = [token add:value bitCount:5];
  57. return [[ZXAztecState alloc] initWithToken:token mode:self.mode binaryBytes:0 bitCount:self.bitCount + thisModeBitCount + 5];
  58. }
  59. // Create a new state representing this state, but an additional character
  60. // output in Binary Shift mode.
  61. - (ZXAztecState *)addBinaryShiftChar:(int)index {
  62. ZXAztecToken *token = self.token;
  63. int mode = self.mode;
  64. int bitCount = self.bitCount;
  65. if (self.mode == ZX_AZTEC_MODE_PUNCT || self.mode == ZX_AZTEC_MODE_DIGIT) {
  66. int latch = ZX_AZTEC_LATCH_TABLE[mode][ZX_AZTEC_MODE_UPPER];
  67. token = [token add:latch & 0xFFFF bitCount:latch >> 16];
  68. bitCount += latch >> 16;
  69. mode = ZX_AZTEC_MODE_UPPER;
  70. }
  71. int deltaBitCount =
  72. (self.binaryShiftByteCount == 0 || self.binaryShiftByteCount == 31) ? 18 :
  73. (self.binaryShiftByteCount == 62) ? 9 : 8;
  74. ZXAztecState *result = [[ZXAztecState alloc] initWithToken:token mode:mode binaryBytes:self.binaryShiftByteCount + 1 bitCount:bitCount + deltaBitCount];
  75. if (result.binaryShiftByteCount == 2047 + 31) {
  76. // The string is as long as it's allowed to be. We should end it.
  77. result = [result endBinaryShift:index + 1];
  78. }
  79. return result;
  80. }
  81. // Create the state identical to this one, but we are no longer in
  82. // Binary Shift mode.
  83. - (ZXAztecState *)endBinaryShift:(int)index {
  84. if (self.binaryShiftByteCount == 0) {
  85. return self;
  86. }
  87. ZXAztecToken *token = self.token;
  88. token = [token addBinaryShift:index - self.binaryShiftByteCount byteCount:self.binaryShiftByteCount];
  89. return [[ZXAztecState alloc] initWithToken:token mode:self.mode binaryBytes:0 bitCount:self.bitCount];
  90. }
  91. // Returns true if "this" state is better (or equal) to be in than "that"
  92. // state under all possible circumstances.
  93. - (BOOL)isBetterThanOrEqualTo:(ZXAztecState *)other {
  94. int mySize = self.bitCount + (ZX_AZTEC_LATCH_TABLE[self.mode][other.mode] >> 16);
  95. if (other.binaryShiftByteCount > 0 &&
  96. (self.binaryShiftByteCount == 0 || self.binaryShiftByteCount > other.binaryShiftByteCount)) {
  97. mySize += 10; // Cost of entering Binary Shift mode.
  98. }
  99. return mySize <= other.bitCount;
  100. }
  101. - (ZXBitArray *)toBitArray:(ZXByteArray *)text {
  102. // Reverse the tokens, so that they are in the order that they should
  103. // be output
  104. NSMutableArray *symbols = [NSMutableArray array];
  105. for (ZXAztecToken *token = [self endBinaryShift:text.length].token; token != nil; token = token.previous) {
  106. [symbols insertObject:token atIndex:0];
  107. }
  108. ZXBitArray *bitArray = [[ZXBitArray alloc] init];
  109. // Add each token to the result.
  110. for (ZXAztecToken *symbol in symbols) {
  111. [symbol appendTo:bitArray text:text];
  112. }
  113. return bitArray;
  114. }
  115. - (NSString *)description {
  116. return [NSString stringWithFormat:@"%@ bits=%d bytes=%d", ZX_AZTEC_MODE_NAMES[self.mode],
  117. self.bitCount, self.binaryShiftByteCount];
  118. }
  119. @end