You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "ZXDataMatrixHighLevelEncoder.h"
  18. #import "ZXDataMatrixSymbolInfo.h"
  19. #import "ZXDataMatrixX12Encoder.h"
  20. @implementation ZXDataMatrixX12Encoder
  21. - (int)encodingMode {
  22. return [ZXDataMatrixHighLevelEncoder x12Encodation];
  23. }
  24. - (void)encode:(ZXDataMatrixEncoderContext *)context {
  25. //step C
  26. NSMutableString *buffer = [NSMutableString string];
  27. while ([context hasMoreCharacters]) {
  28. unichar c = [context currentChar];
  29. context.pos++;
  30. [self encodeChar:c buffer:buffer];
  31. NSUInteger count = buffer.length;
  32. if ((count % 3) == 0) {
  33. [self writeNextTriplet:context buffer:buffer];
  34. int newMode = [ZXDataMatrixHighLevelEncoder lookAheadTest:context.message startpos:context.pos currentMode:[self encodingMode]];
  35. if (newMode != [self encodingMode]) {
  36. [context signalEncoderChange:newMode];
  37. break;
  38. }
  39. }
  40. }
  41. [self handleEOD:context buffer:buffer];
  42. }
  43. - (int)encodeChar:(unichar)c buffer:(NSMutableString *)sb {
  44. if (c == '\r') {
  45. [sb appendString:@"\0"];
  46. } else if (c == '*') {
  47. [sb appendString:@"\1"];
  48. } else if (c == '>') {
  49. [sb appendString:@"\2"];
  50. } else if (c == ' ') {
  51. [sb appendString:@"\3"];
  52. } else if (c >= '0' && c <= '9') {
  53. [sb appendFormat:@"%C", (unichar) (c - 48 + 4)];
  54. } else if (c >= 'A' && c <= 'Z') {
  55. [sb appendFormat:@"%C", (unichar) (c - 65 + 14)];
  56. } else {
  57. [ZXDataMatrixHighLevelEncoder illegalCharacter:c];
  58. }
  59. return 1;
  60. }
  61. - (void)handleEOD:(ZXDataMatrixEncoderContext *)context buffer:(NSMutableString *)buffer {
  62. [context updateSymbolInfo];
  63. int available = context.symbolInfo.dataCapacity - [context codewordCount];
  64. int count = (int)buffer.length;
  65. context.pos -= count;
  66. if (context.remainingCharacters > 1 || available > 1 ||
  67. context.remainingCharacters != available) {
  68. [context writeCodeword:[ZXDataMatrixHighLevelEncoder x12Unlatch]];
  69. }
  70. if (context.newEncoding < 0) {
  71. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder asciiEncodation]];
  72. }
  73. }
  74. @end