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

ZXDataMatrixEdifactEncoder.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "ZXDataMatrixEdifactEncoder.h"
  17. #import "ZXDataMatrixEncoderContext.h"
  18. #import "ZXDataMatrixHighLevelEncoder.h"
  19. #import "ZXDataMatrixSymbolInfo.h"
  20. @implementation ZXDataMatrixEdifactEncoder
  21. - (int)encodingMode {
  22. return [ZXDataMatrixHighLevelEncoder edifactEncodation];
  23. }
  24. - (void)encode:(ZXDataMatrixEncoderContext *)context {
  25. //step F
  26. NSMutableString *buffer = [NSMutableString string];
  27. while ([context hasMoreCharacters]) {
  28. unichar c = [context currentChar];
  29. [self encodeChar:c buffer:buffer];
  30. context.pos++;
  31. NSUInteger count = buffer.length;
  32. if (count >= 4) {
  33. [context writeCodewords:[self encodeToCodewords:buffer startpos:0]];
  34. [buffer deleteCharactersInRange:NSMakeRange(0, 4)];
  35. int newMode = [ZXDataMatrixHighLevelEncoder lookAheadTest:context.message startpos:context.pos currentMode:[self encodingMode]];
  36. if (newMode != [self encodingMode]) {
  37. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder asciiEncodation]];
  38. break;
  39. }
  40. }
  41. }
  42. [buffer appendFormat:@"%C", (unichar) 31]; //Unlatch
  43. [self handleEOD:context buffer:buffer];
  44. }
  45. /**
  46. * Handle "end of data" situations
  47. *
  48. * @param context the encoder context
  49. * @param buffer the buffer with the remaining encoded characters
  50. */
  51. - (void)handleEOD:(ZXDataMatrixEncoderContext *)context buffer:(NSMutableString *)buffer {
  52. @try {
  53. NSUInteger count = buffer.length;
  54. if (count == 0) {
  55. return; //Already finished
  56. }
  57. if (count == 1) {
  58. //Only an unlatch at the end
  59. [context updateSymbolInfo];
  60. int available = context.symbolInfo.dataCapacity - context.codewordCount;
  61. int remaining = [context remainingCharacters];
  62. if (remaining == 0 && available <= 2) {
  63. return; //No unlatch
  64. }
  65. }
  66. if (count > 4) {
  67. @throw [NSException exceptionWithName:@"IllegalStateException"
  68. reason:@"Count must not exceed 4"
  69. userInfo:nil];
  70. }
  71. int restChars = (int)count - 1;
  72. NSString *encoded = [self encodeToCodewords:buffer startpos:0];
  73. BOOL endOfSymbolReached = ![context hasMoreCharacters];
  74. BOOL restInAscii = endOfSymbolReached && restChars <= 2;
  75. if (restChars <= 2) {
  76. [context updateSymbolInfoWithLength:context.codewordCount + restChars];
  77. int available = context.symbolInfo.dataCapacity - context.codewordCount;
  78. if (available >= 3) {
  79. restInAscii = NO;
  80. [context updateSymbolInfoWithLength:context.codewordCount + (int)encoded.length];
  81. //available = context.symbolInfo.dataCapacity - context.codewordCount;
  82. }
  83. }
  84. if (restInAscii) {
  85. [context resetSymbolInfo];
  86. context.pos -= restChars;
  87. } else {
  88. [context writeCodewords:encoded];
  89. }
  90. } @finally {
  91. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder asciiEncodation]];
  92. }
  93. }
  94. - (void)encodeChar:(unichar)c buffer:(NSMutableString *)sb {
  95. if (c >= ' ' && c <= '?') {
  96. [sb appendFormat:@"%C", c];
  97. } else if (c >= '@' && c <= '^') {
  98. [sb appendFormat:@"%C", (unichar) (c - 64)];
  99. } else {
  100. [ZXDataMatrixHighLevelEncoder illegalCharacter:c];
  101. }
  102. }
  103. - (NSString *)encodeToCodewords:(NSMutableString *)sb startpos:(int)startPos {
  104. int len = (int)sb.length - startPos;
  105. if (len == 0) {
  106. @throw [NSException exceptionWithName:@"IllegalStateException"
  107. reason:@"Buffer must not be empty"
  108. userInfo:nil];
  109. }
  110. unichar c1 = [sb characterAtIndex:startPos];
  111. unichar c2 = len >= 2 ? [sb characterAtIndex:startPos + 1] : 0;
  112. unichar c3 = len >= 3 ? [sb characterAtIndex:startPos + 2] : 0;
  113. unichar c4 = len >= 4 ? [sb characterAtIndex:startPos + 3] : 0;
  114. int v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4;
  115. unichar cw1 = (unichar) ((v >> 16) & 255);
  116. unichar cw2 = (unichar) ((v >> 8) & 255);
  117. unichar cw3 = (unichar) (v & 255);
  118. NSMutableString *res = [NSMutableString stringWithCapacity:3];
  119. [res appendFormat:@"%C", cw1];
  120. if (len >= 2) {
  121. [res appendFormat:@"%C", cw2];
  122. }
  123. if (len >= 3) {
  124. [res appendFormat:@"%C", cw3];
  125. }
  126. return [NSString stringWithString:res];
  127. }
  128. @end