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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "ZXDataMatrixASCIIEncoder.h"
  17. #import "ZXDataMatrixEncoderContext.h"
  18. #import "ZXDataMatrixHighLevelEncoder.h"
  19. @implementation ZXDataMatrixASCIIEncoder
  20. - (int)encodingMode {
  21. return [ZXDataMatrixHighLevelEncoder asciiEncodation];
  22. }
  23. - (void)encode:(ZXDataMatrixEncoderContext *)context {
  24. //step B
  25. int n = [ZXDataMatrixHighLevelEncoder determineConsecutiveDigitCount:context.message startpos:context.pos];
  26. if (n >= 2) {
  27. [context writeCodeword:[self encodeASCIIDigits:[context.message characterAtIndex:context.pos]
  28. digit2:[context.message characterAtIndex:context.pos + 1]]];
  29. context.pos += 2;
  30. } else {
  31. unichar c = [context currentChar];
  32. int newMode = [ZXDataMatrixHighLevelEncoder lookAheadTest:context.message startpos:context.pos currentMode:[self encodingMode]];
  33. if (newMode != [self encodingMode]) {
  34. if (newMode == [ZXDataMatrixHighLevelEncoder base256Encodation]) {
  35. [context writeCodeword:[ZXDataMatrixHighLevelEncoder latchToBase256]];
  36. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder base256Encodation]];
  37. return;
  38. } else if (newMode == [ZXDataMatrixHighLevelEncoder c40Encodation]) {
  39. [context writeCodeword:[ZXDataMatrixHighLevelEncoder latchToC40]];
  40. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder c40Encodation]];
  41. return;
  42. } else if (newMode == [ZXDataMatrixHighLevelEncoder x12Encodation]) {
  43. [context writeCodeword:[ZXDataMatrixHighLevelEncoder latchToAnsiX12]];
  44. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder x12Encodation]];
  45. } else if (newMode == [ZXDataMatrixHighLevelEncoder textEncodation]) {
  46. [context writeCodeword:[ZXDataMatrixHighLevelEncoder latchToText]];
  47. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder textEncodation]];
  48. } else if (newMode == [ZXDataMatrixHighLevelEncoder edifactEncodation]) {
  49. [context writeCodeword:[ZXDataMatrixHighLevelEncoder latchToEdifact]];
  50. [context signalEncoderChange:[ZXDataMatrixHighLevelEncoder edifactEncodation]];
  51. } else {
  52. @throw [NSException exceptionWithName:@"IllegalStateException" reason:@"Illegal mode" userInfo:nil];
  53. }
  54. } else if ([ZXDataMatrixHighLevelEncoder isExtendedASCII:c]) {
  55. [context writeCodeword:[ZXDataMatrixHighLevelEncoder upperShift]];
  56. [context writeCodeword:(unichar)(c - 128 + 1)];
  57. context.pos++;
  58. } else {
  59. [context writeCodeword:(unichar)(c + 1)];
  60. context.pos++;
  61. }
  62. }
  63. }
  64. - (unichar)encodeASCIIDigits:(unichar)digit1 digit2:(unichar)digit2 {
  65. if ([ZXDataMatrixHighLevelEncoder isDigit:digit1] && [ZXDataMatrixHighLevelEncoder isDigit:digit2]) {
  66. int num = (digit1 - 48) * 10 + (digit2 - 48);
  67. return (unichar) (num + 130);
  68. }
  69. @throw [NSException exceptionWithName:NSInvalidArgumentException
  70. reason:[NSString stringWithFormat:@"not digits: %C %C", digit1, digit2]
  71. userInfo:nil];
  72. }
  73. @end