Mode.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Qrcode\Decoder;
  18. /**
  19. * <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
  20. * data can be encoded to bits in the QR code standard.</p>
  21. *
  22. * @author Sean Owen
  23. */
  24. class Mode
  25. {
  26. public static $TERMINATOR;
  27. public static $NUMERIC;
  28. public static $ALPHANUMERIC;
  29. public static $STRUCTURED_APPEND;
  30. public static $BYTE;
  31. public static $ECI;
  32. public static $KANJI;
  33. public static $FNC1_FIRST_POSITION;
  34. public static $FNC1_SECOND_POSITION;
  35. public static $HANZI;
  36. public function __construct(private $characterCountBitsForVersions, private $bits)
  37. {
  38. }
  39. public static function Init(): void
  40. {
  41. self::$TERMINATOR = new Mode([0, 0, 0], 0x00); // Not really a mode...
  42. self::$NUMERIC = new Mode([10, 12, 14], 0x01);
  43. self::$ALPHANUMERIC = new Mode([9, 11, 13], 0x02);
  44. self::$STRUCTURED_APPEND = new Mode([0, 0, 0], 0x03); // Not supported
  45. self::$BYTE = new Mode([8, 16, 16], 0x04);
  46. self::$ECI = new Mode([0, 0, 0], 0x07); // character counts don't apply
  47. self::$KANJI = new Mode([8, 10, 12], 0x08);
  48. self::$FNC1_FIRST_POSITION = new Mode([0, 0, 0], 0x05);
  49. self::$FNC1_SECOND_POSITION = new Mode([0, 0, 0], 0x09);
  50. /** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
  51. self::$HANZI = new Mode([8, 10, 12], 0x0D);
  52. }
  53. /**
  54. * @param four $bits bits encoding a QR Code data mode
  55. *
  56. * @return Mode encoded by these bits
  57. * @throws InvalidArgumentException if bits do not correspond to a known mode
  58. */
  59. public static function forBits($bits)
  60. {
  61. return match ($bits) {
  62. 0x0 => self::$TERMINATOR,
  63. 0x1 => self::$NUMERIC,
  64. 0x2 => self::$ALPHANUMERIC,
  65. 0x3 => self::$STRUCTURED_APPEND,
  66. 0x4 => self::$BYTE,
  67. 0x5 => self::$FNC1_FIRST_POSITION,
  68. 0x7 => self::$ECI,
  69. 0x8 => self::$KANJI,
  70. 0x9 => self::$FNC1_SECOND_POSITION,
  71. 0xD => self::$HANZI,
  72. default => throw new \InvalidArgumentException(),
  73. };
  74. }
  75. /**
  76. * @param version $version in question
  77. *
  78. * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
  79. * count of characters that will follow encoded in this Mode
  80. */
  81. public function getCharacterCountBits($version)
  82. {
  83. $number = $version->getVersionNumber();
  84. $offset = 0;
  85. if ($number <= 9) {
  86. $offset = 0;
  87. } elseif ($number <= 26) {
  88. $offset = 1;
  89. } else {
  90. $offset = 2;
  91. }
  92. return $this->characterCountBitsForVersions[$offset];
  93. }
  94. public function getBits()
  95. {
  96. return $this->bits;
  97. }
  98. }
  99. Mode::Init();