BinaryShiftToken.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. using System;
  17. namespace FastReport.Barcode.Aztec
  18. {
  19. internal sealed class BinaryShiftToken : Token
  20. {
  21. private readonly short binaryShiftStart;
  22. private readonly short binaryShiftByteCount;
  23. public BinaryShiftToken(Token previous,
  24. int binaryShiftStart,
  25. int binaryShiftByteCount)
  26. : base(previous)
  27. {
  28. this.binaryShiftStart = (short) binaryShiftStart;
  29. this.binaryShiftByteCount = (short) binaryShiftByteCount;
  30. }
  31. public override void appendTo(BitArray bitArray, byte[] text)
  32. {
  33. for (int i = 0; i < binaryShiftByteCount; i++)
  34. {
  35. if (i == 0 || (i == 31 && binaryShiftByteCount <= 62))
  36. {
  37. // We need a header before the first character, and before
  38. // character 31 when the total byte code is <= 62
  39. bitArray.appendBits(31, 5); // BINARY_SHIFT
  40. if (binaryShiftByteCount > 62)
  41. {
  42. bitArray.appendBits(binaryShiftByteCount - 31, 16);
  43. }
  44. else if (i == 0)
  45. {
  46. // 1 <= binaryShiftByteCode <= 62
  47. bitArray.appendBits(Math.Min(binaryShiftByteCount, (short) 31), 5);
  48. }
  49. else
  50. {
  51. // 32 <= binaryShiftCount <= 62 and i == 31
  52. bitArray.appendBits(binaryShiftByteCount - 31, 5);
  53. }
  54. }
  55. bitArray.appendBits(text[binaryShiftStart + i], 8);
  56. }
  57. }
  58. public override String ToString()
  59. {
  60. return "<" + binaryShiftStart + "::" + (binaryShiftStart + binaryShiftByteCount - 1) + '>';
  61. }
  62. }
  63. }