单军华
2017-07-12 20d1260d26b028897f3c0935c12fc35aa37b2e93
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
/*
 * Copyright 2014 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 "ZXAztecHighLevelEncoder.h"
#import "ZXAztecState.h"
#import "ZXAztecToken.h"
#import "ZXBitArray.h"
#import "ZXByteArray.h"
 
@implementation ZXAztecState
 
- (id)initWithToken:(ZXAztecToken *)token mode:(int)mode binaryBytes:(int)binaryBytes bitCount:(int)bitCount {
  if (self = [super init]) {
    _token = token;
    _mode = mode;
    _binaryShiftByteCount = binaryBytes;
    _bitCount = bitCount;
  }
 
  return self;
}
 
+ (ZXAztecState *)initialState {
  return [[ZXAztecState alloc] initWithToken:[ZXAztecToken empty] mode:ZX_AZTEC_MODE_UPPER binaryBytes:0 bitCount:0];
}
 
// Create a new state representing this state with a latch to a (not
// necessary different) mode, and then a code.
- (ZXAztecState *)latchAndAppend:(int)mode value:(int)value {
  int bitCount = self.bitCount;
  ZXAztecToken *token = self.token;
  if (mode != self.mode) {
    int latch = ZX_AZTEC_LATCH_TABLE[self.mode][mode];
    token = [token add:latch & 0xFFFF bitCount:latch >> 16];
    bitCount += latch >> 16;
  }
  int latchModeBitCount = mode == ZX_AZTEC_MODE_DIGIT ? 4 : 5;
  token = [token add:value bitCount:latchModeBitCount];
  return [[ZXAztecState alloc] initWithToken:token mode:mode binaryBytes:0 bitCount:bitCount + latchModeBitCount];
}
 
// Create a new state representing this state, with a temporary shift
// to a different mode to output a single value.
- (ZXAztecState *)shiftAndAppend:(int)mode value:(int)value {
  //assert binaryShiftByteCount == 0 && this.mode != mode;
  ZXAztecToken *token = self.token;
  int thisModeBitCount = self.mode == ZX_AZTEC_MODE_DIGIT ? 4 : 5;
  // Shifts exist only to UPPER and PUNCT, both with tokens size 5.
  token = [token add:ZX_AZTEC_SHIFT_TABLE[self.mode][mode] bitCount:thisModeBitCount];
  token = [token add:value bitCount:5];
  return [[ZXAztecState alloc] initWithToken:token mode:self.mode binaryBytes:0 bitCount:self.bitCount + thisModeBitCount + 5];
}
 
// Create a new state representing this state, but an additional character
// output in Binary Shift mode.
- (ZXAztecState *)addBinaryShiftChar:(int)index {
  ZXAztecToken *token = self.token;
  int mode = self.mode;
  int bitCount = self.bitCount;
  if (self.mode == ZX_AZTEC_MODE_PUNCT || self.mode == ZX_AZTEC_MODE_DIGIT)  {
    int latch = ZX_AZTEC_LATCH_TABLE[mode][ZX_AZTEC_MODE_UPPER];
    token = [token add:latch & 0xFFFF bitCount:latch >> 16];
    bitCount += latch >> 16;
    mode = ZX_AZTEC_MODE_UPPER;
  }
  int deltaBitCount =
    (self.binaryShiftByteCount == 0 || self.binaryShiftByteCount == 31) ? 18 :
    (self.binaryShiftByteCount == 62) ? 9 : 8;
  ZXAztecState *result = [[ZXAztecState alloc] initWithToken:token mode:mode binaryBytes:self.binaryShiftByteCount + 1 bitCount:bitCount + deltaBitCount];
  if (result.binaryShiftByteCount == 2047 + 31) {
    // The string is as long as it's allowed to be.  We should end it.
    result = [result endBinaryShift:index + 1];
  }
  return result;
}
 
// Create the state identical to this one, but we are no longer in
// Binary Shift mode.
- (ZXAztecState *)endBinaryShift:(int)index {
  if (self.binaryShiftByteCount == 0) {
    return self;
  }
  ZXAztecToken *token = self.token;
  token = [token addBinaryShift:index - self.binaryShiftByteCount byteCount:self.binaryShiftByteCount];
  return [[ZXAztecState alloc] initWithToken:token mode:self.mode binaryBytes:0 bitCount:self.bitCount];
}
 
// Returns true if "this" state is better (or equal) to be in than "that"
// state under all possible circumstances.
- (BOOL)isBetterThanOrEqualTo:(ZXAztecState *)other {
  int mySize = self.bitCount + (ZX_AZTEC_LATCH_TABLE[self.mode][other.mode] >> 16);
  if (other.binaryShiftByteCount > 0 &&
      (self.binaryShiftByteCount == 0 || self.binaryShiftByteCount > other.binaryShiftByteCount)) {
    mySize += 10;     // Cost of entering Binary Shift mode.
  }
  return mySize <= other.bitCount;
}
 
- (ZXBitArray *)toBitArray:(ZXByteArray *)text {
  // Reverse the tokens, so that they are in the order that they should
  // be output
  NSMutableArray *symbols = [NSMutableArray array];
  for (ZXAztecToken *token = [self endBinaryShift:text.length].token; token != nil; token = token.previous) {
    [symbols insertObject:token atIndex:0];
  }
  ZXBitArray *bitArray = [[ZXBitArray alloc] init];
  // Add each token to the result.
  for (ZXAztecToken *symbol in symbols) {
    [symbol appendTo:bitArray text:text];
  }
  return bitArray;
}
 
- (NSString *)description {
  return [NSString stringWithFormat:@"%@ bits=%d bytes=%d", ZX_AZTEC_MODE_NAMES[self.mode],
          self.bitCount, self.binaryShiftByteCount];
}
 
@end