Encoder.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCode\Encoder;
  4. use BaconQrCode\Common\BitArray;
  5. use BaconQrCode\Common\CharacterSetEci;
  6. use BaconQrCode\Common\ErrorCorrectionLevel;
  7. use BaconQrCode\Common\Mode;
  8. use BaconQrCode\Common\ReedSolomonCodec;
  9. use BaconQrCode\Common\Version;
  10. use BaconQrCode\Exception\WriterException;
  11. use SplFixedArray;
  12. /**
  13. * Encoder.
  14. */
  15. final class Encoder
  16. {
  17. /**
  18. * Default byte encoding.
  19. */
  20. public const DEFAULT_BYTE_MODE_ECODING = 'ISO-8859-1';
  21. /**
  22. * The original table is defined in the table 5 of JISX0510:2004 (p.19).
  23. */
  24. private const ALPHANUMERIC_TABLE = [
  25. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00-0x0f
  26. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x10-0x1f
  27. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, // 0x20-0x2f
  28. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, // 0x30-0x3f
  29. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40-0x4f
  30. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f
  31. ];
  32. /**
  33. * Codec cache.
  34. *
  35. * @var array
  36. */
  37. private static $codecs = [];
  38. /**
  39. * Encodes "content" with the error correction level "ecLevel".
  40. */
  41. public static function encode(
  42. string $content,
  43. ErrorCorrectionLevel $ecLevel,
  44. string $encoding = self::DEFAULT_BYTE_MODE_ECODING
  45. ) : QrCode {
  46. // Pick an encoding mode appropriate for the content. Note that this
  47. // will not attempt to use multiple modes / segments even if that were
  48. // more efficient. Would be nice.
  49. $mode = self::chooseMode($content, $encoding);
  50. // This will store the header information, like mode and length, as well
  51. // as "header" segments like an ECI segment.
  52. $headerBits = new BitArray();
  53. // Append ECI segment if applicable
  54. if (Mode::BYTE() === $mode && self::DEFAULT_BYTE_MODE_ECODING !== $encoding) {
  55. $eci = CharacterSetEci::getCharacterSetEciByName($encoding);
  56. if (null !== $eci) {
  57. self::appendEci($eci, $headerBits);
  58. }
  59. }
  60. // (With ECI in place,) Write the mode marker
  61. self::appendModeInfo($mode, $headerBits);
  62. // Collect data within the main segment, separately, to count its size
  63. // if needed. Don't add it to main payload yet.
  64. $dataBits = new BitArray();
  65. self::appendBytes($content, $mode, $dataBits, $encoding);
  66. // Hard part: need to know version to know how many bits length takes.
  67. // But need to know how many bits it takes to know version. First we
  68. // take a guess at version by assuming version will be the minimum, 1:
  69. $provisionalBitsNeeded = $headerBits->getSize()
  70. + $mode->getCharacterCountBits(Version::getVersionForNumber(1))
  71. + $dataBits->getSize();
  72. $provisionalVersion = self::chooseVersion($provisionalBitsNeeded, $ecLevel);
  73. // Use that guess to calculate the right version. I am still not sure
  74. // this works in 100% of cases.
  75. $bitsNeeded = $headerBits->getSize()
  76. + $mode->getCharacterCountBits($provisionalVersion)
  77. + $dataBits->getSize();
  78. $version = self::chooseVersion($bitsNeeded, $ecLevel);
  79. $headerAndDataBits = new BitArray();
  80. $headerAndDataBits->appendBitArray($headerBits);
  81. // Find "length" of main segment and write it.
  82. $numLetters = (Mode::BYTE() === $mode ? $dataBits->getSizeInBytes() : strlen($content));
  83. self::appendLengthInfo($numLetters, $version, $mode, $headerAndDataBits);
  84. // Put data together into the overall payload.
  85. $headerAndDataBits->appendBitArray($dataBits);
  86. $ecBlocks = $version->getEcBlocksForLevel($ecLevel);
  87. $numDataBytes = $version->getTotalCodewords() - $ecBlocks->getTotalEcCodewords();
  88. // Terminate the bits properly.
  89. self::terminateBits($numDataBytes, $headerAndDataBits);
  90. // Interleave data bits with error correction code.
  91. $finalBits = self::interleaveWithEcBytes(
  92. $headerAndDataBits,
  93. $version->getTotalCodewords(),
  94. $numDataBytes,
  95. $ecBlocks->getNumBlocks()
  96. );
  97. // Choose the mask pattern.
  98. $dimension = $version->getDimensionForVersion();
  99. $matrix = new ByteMatrix($dimension, $dimension);
  100. $maskPattern = self::chooseMaskPattern($finalBits, $ecLevel, $version, $matrix);
  101. // Build the matrix.
  102. MatrixUtil::buildMatrix($finalBits, $ecLevel, $version, $maskPattern, $matrix);
  103. return new QrCode($mode, $ecLevel, $version, $maskPattern, $matrix);
  104. }
  105. /**
  106. * Gets the alphanumeric code for a byte.
  107. */
  108. private static function getAlphanumericCode(int $code) : int
  109. {
  110. if (isset(self::ALPHANUMERIC_TABLE[$code])) {
  111. return self::ALPHANUMERIC_TABLE[$code];
  112. }
  113. return -1;
  114. }
  115. /**
  116. * Chooses the best mode for a given content.
  117. */
  118. private static function chooseMode(string $content, string $encoding = null) : Mode
  119. {
  120. if (null !== $encoding && 0 === strcasecmp($encoding, 'SHIFT-JIS')) {
  121. return self::isOnlyDoubleByteKanji($content) ? Mode::KANJI() : Mode::BYTE();
  122. }
  123. $hasNumeric = false;
  124. $hasAlphanumeric = false;
  125. $contentLength = strlen($content);
  126. for ($i = 0; $i < $contentLength; ++$i) {
  127. $char = $content[$i];
  128. if (ctype_digit($char)) {
  129. $hasNumeric = true;
  130. } elseif (-1 !== self::getAlphanumericCode(ord($char))) {
  131. $hasAlphanumeric = true;
  132. } else {
  133. return Mode::BYTE();
  134. }
  135. }
  136. if ($hasAlphanumeric) {
  137. return Mode::ALPHANUMERIC();
  138. } elseif ($hasNumeric) {
  139. return Mode::NUMERIC();
  140. }
  141. return Mode::BYTE();
  142. }
  143. /**
  144. * Calculates the mask penalty for a matrix.
  145. */
  146. private static function calculateMaskPenalty(ByteMatrix $matrix) : int
  147. {
  148. return (
  149. MaskUtil::applyMaskPenaltyRule1($matrix)
  150. + MaskUtil::applyMaskPenaltyRule2($matrix)
  151. + MaskUtil::applyMaskPenaltyRule3($matrix)
  152. + MaskUtil::applyMaskPenaltyRule4($matrix)
  153. );
  154. }
  155. /**
  156. * Checks if content only consists of double-byte kanji characters.
  157. */
  158. private static function isOnlyDoubleByteKanji(string $content) : bool
  159. {
  160. $bytes = @iconv('utf-8', 'SHIFT-JIS', $content);
  161. if (false === $bytes) {
  162. return false;
  163. }
  164. $length = strlen($bytes);
  165. if (0 !== $length % 2) {
  166. return false;
  167. }
  168. for ($i = 0; $i < $length; $i += 2) {
  169. $byte = $bytes[$i] & 0xff;
  170. if (($byte < 0x81 || $byte > 0x9f) && $byte < 0xe0 || $byte > 0xeb) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. /**
  177. * Chooses the best mask pattern for a matrix.
  178. */
  179. private static function chooseMaskPattern(
  180. BitArray $bits,
  181. ErrorCorrectionLevel $ecLevel,
  182. Version $version,
  183. ByteMatrix $matrix
  184. ) : int {
  185. $minPenalty = PHP_INT_MAX;
  186. $bestMaskPattern = -1;
  187. for ($maskPattern = 0; $maskPattern < QrCode::NUM_MASK_PATTERNS; ++$maskPattern) {
  188. MatrixUtil::buildMatrix($bits, $ecLevel, $version, $maskPattern, $matrix);
  189. $penalty = self::calculateMaskPenalty($matrix);
  190. if ($penalty < $minPenalty) {
  191. $minPenalty = $penalty;
  192. $bestMaskPattern = $maskPattern;
  193. }
  194. }
  195. return $bestMaskPattern;
  196. }
  197. /**
  198. * Chooses the best version for the input.
  199. *
  200. * @throws WriterException if data is too big
  201. */
  202. private static function chooseVersion(int $numInputBits, ErrorCorrectionLevel $ecLevel) : Version
  203. {
  204. for ($versionNum = 1; $versionNum <= 40; ++$versionNum) {
  205. $version = Version::getVersionForNumber($versionNum);
  206. $numBytes = $version->getTotalCodewords();
  207. $ecBlocks = $version->getEcBlocksForLevel($ecLevel);
  208. $numEcBytes = $ecBlocks->getTotalEcCodewords();
  209. $numDataBytes = $numBytes - $numEcBytes;
  210. $totalInputBytes = intdiv($numInputBits + 8, 8);
  211. if ($numDataBytes >= $totalInputBytes) {
  212. return $version;
  213. }
  214. }
  215. throw new WriterException('Data too big');
  216. }
  217. /**
  218. * Terminates the bits in a bit array.
  219. *
  220. * @throws WriterException if data bits cannot fit in the QR code
  221. * @throws WriterException if bits size does not equal the capacity
  222. */
  223. private static function terminateBits(int $numDataBytes, BitArray $bits) : void
  224. {
  225. $capacity = $numDataBytes << 3;
  226. if ($bits->getSize() > $capacity) {
  227. throw new WriterException('Data bits cannot fit in the QR code');
  228. }
  229. for ($i = 0; $i < 4 && $bits->getSize() < $capacity; ++$i) {
  230. $bits->appendBit(false);
  231. }
  232. $numBitsInLastByte = $bits->getSize() & 0x7;
  233. if ($numBitsInLastByte > 0) {
  234. for ($i = $numBitsInLastByte; $i < 8; ++$i) {
  235. $bits->appendBit(false);
  236. }
  237. }
  238. $numPaddingBytes = $numDataBytes - $bits->getSizeInBytes();
  239. for ($i = 0; $i < $numPaddingBytes; ++$i) {
  240. $bits->appendBits(0 === ($i & 0x1) ? 0xec : 0x11, 8);
  241. }
  242. if ($bits->getSize() !== $capacity) {
  243. throw new WriterException('Bits size does not equal capacity');
  244. }
  245. }
  246. /**
  247. * Gets number of data- and EC bytes for a block ID.
  248. *
  249. * @return int[]
  250. * @throws WriterException if block ID is too large
  251. * @throws WriterException if EC bytes mismatch
  252. * @throws WriterException if RS blocks mismatch
  253. * @throws WriterException if total bytes mismatch
  254. */
  255. private static function getNumDataBytesAndNumEcBytesForBlockId(
  256. int $numTotalBytes,
  257. int $numDataBytes,
  258. int $numRsBlocks,
  259. int $blockId
  260. ) : array {
  261. if ($blockId >= $numRsBlocks) {
  262. throw new WriterException('Block ID too large');
  263. }
  264. $numRsBlocksInGroup2 = $numTotalBytes % $numRsBlocks;
  265. $numRsBlocksInGroup1 = $numRsBlocks - $numRsBlocksInGroup2;
  266. $numTotalBytesInGroup1 = intdiv($numTotalBytes, $numRsBlocks);
  267. $numTotalBytesInGroup2 = $numTotalBytesInGroup1 + 1;
  268. $numDataBytesInGroup1 = intdiv($numDataBytes, $numRsBlocks);
  269. $numDataBytesInGroup2 = $numDataBytesInGroup1 + 1;
  270. $numEcBytesInGroup1 = $numTotalBytesInGroup1 - $numDataBytesInGroup1;
  271. $numEcBytesInGroup2 = $numTotalBytesInGroup2 - $numDataBytesInGroup2;
  272. if ($numEcBytesInGroup1 !== $numEcBytesInGroup2) {
  273. throw new WriterException('EC bytes mismatch');
  274. }
  275. if ($numRsBlocks !== $numRsBlocksInGroup1 + $numRsBlocksInGroup2) {
  276. throw new WriterException('RS blocks mismatch');
  277. }
  278. if ($numTotalBytes !==
  279. (($numDataBytesInGroup1 + $numEcBytesInGroup1) * $numRsBlocksInGroup1)
  280. + (($numDataBytesInGroup2 + $numEcBytesInGroup2) * $numRsBlocksInGroup2)
  281. ) {
  282. throw new WriterException('Total bytes mismatch');
  283. }
  284. if ($blockId < $numRsBlocksInGroup1) {
  285. return [$numDataBytesInGroup1, $numEcBytesInGroup1];
  286. } else {
  287. return [$numDataBytesInGroup2, $numEcBytesInGroup2];
  288. }
  289. }
  290. /**
  291. * Interleaves data with EC bytes.
  292. *
  293. * @throws WriterException if number of bits and data bytes does not match
  294. * @throws WriterException if data bytes does not match offset
  295. * @throws WriterException if an interleaving error occurs
  296. */
  297. private static function interleaveWithEcBytes(
  298. BitArray $bits,
  299. int $numTotalBytes,
  300. int $numDataBytes,
  301. int $numRsBlocks
  302. ) : BitArray {
  303. if ($bits->getSizeInBytes() !== $numDataBytes) {
  304. throw new WriterException('Number of bits and data bytes does not match');
  305. }
  306. $dataBytesOffset = 0;
  307. $maxNumDataBytes = 0;
  308. $maxNumEcBytes = 0;
  309. $blocks = new SplFixedArray($numRsBlocks);
  310. for ($i = 0; $i < $numRsBlocks; ++$i) {
  311. list($numDataBytesInBlock, $numEcBytesInBlock) = self::getNumDataBytesAndNumEcBytesForBlockId(
  312. $numTotalBytes,
  313. $numDataBytes,
  314. $numRsBlocks,
  315. $i
  316. );
  317. $size = $numDataBytesInBlock;
  318. $dataBytes = $bits->toBytes(8 * $dataBytesOffset, $size);
  319. $ecBytes = self::generateEcBytes($dataBytes, $numEcBytesInBlock);
  320. $blocks[$i] = new BlockPair($dataBytes, $ecBytes);
  321. $maxNumDataBytes = max($maxNumDataBytes, $size);
  322. $maxNumEcBytes = max($maxNumEcBytes, count($ecBytes));
  323. $dataBytesOffset += $numDataBytesInBlock;
  324. }
  325. if ($numDataBytes !== $dataBytesOffset) {
  326. throw new WriterException('Data bytes does not match offset');
  327. }
  328. $result = new BitArray();
  329. for ($i = 0; $i < $maxNumDataBytes; ++$i) {
  330. foreach ($blocks as $block) {
  331. $dataBytes = $block->getDataBytes();
  332. if ($i < count($dataBytes)) {
  333. $result->appendBits($dataBytes[$i], 8);
  334. }
  335. }
  336. }
  337. for ($i = 0; $i < $maxNumEcBytes; ++$i) {
  338. foreach ($blocks as $block) {
  339. $ecBytes = $block->getErrorCorrectionBytes();
  340. if ($i < count($ecBytes)) {
  341. $result->appendBits($ecBytes[$i], 8);
  342. }
  343. }
  344. }
  345. if ($numTotalBytes !== $result->getSizeInBytes()) {
  346. throw new WriterException(
  347. 'Interleaving error: ' . $numTotalBytes . ' and ' . $result->getSizeInBytes() . ' differ'
  348. );
  349. }
  350. return $result;
  351. }
  352. /**
  353. * Generates EC bytes for given data.
  354. *
  355. * @param SplFixedArray<int> $dataBytes
  356. * @return SplFixedArray<int>
  357. */
  358. private static function generateEcBytes(SplFixedArray $dataBytes, int $numEcBytesInBlock) : SplFixedArray
  359. {
  360. $numDataBytes = count($dataBytes);
  361. $toEncode = new SplFixedArray($numDataBytes + $numEcBytesInBlock);
  362. for ($i = 0; $i < $numDataBytes; $i++) {
  363. $toEncode[$i] = $dataBytes[$i] & 0xff;
  364. }
  365. $ecBytes = new SplFixedArray($numEcBytesInBlock);
  366. $codec = self::getCodec($numDataBytes, $numEcBytesInBlock);
  367. $codec->encode($toEncode, $ecBytes);
  368. return $ecBytes;
  369. }
  370. /**
  371. * Gets an RS codec and caches it.
  372. */
  373. private static function getCodec(int $numDataBytes, int $numEcBytesInBlock) : ReedSolomonCodec
  374. {
  375. $cacheId = $numDataBytes . '-' . $numEcBytesInBlock;
  376. if (isset(self::$codecs[$cacheId])) {
  377. return self::$codecs[$cacheId];
  378. }
  379. return self::$codecs[$cacheId] = new ReedSolomonCodec(
  380. 8,
  381. 0x11d,
  382. 0,
  383. 1,
  384. $numEcBytesInBlock,
  385. 255 - $numDataBytes - $numEcBytesInBlock
  386. );
  387. }
  388. /**
  389. * Appends mode information to a bit array.
  390. */
  391. private static function appendModeInfo(Mode $mode, BitArray $bits) : void
  392. {
  393. $bits->appendBits($mode->getBits(), 4);
  394. }
  395. /**
  396. * Appends length information to a bit array.
  397. *
  398. * @throws WriterException if num letters is bigger than expected
  399. */
  400. private static function appendLengthInfo(int $numLetters, Version $version, Mode $mode, BitArray $bits) : void
  401. {
  402. $numBits = $mode->getCharacterCountBits($version);
  403. if ($numLetters >= (1 << $numBits)) {
  404. throw new WriterException($numLetters . ' is bigger than ' . ((1 << $numBits) - 1));
  405. }
  406. $bits->appendBits($numLetters, $numBits);
  407. }
  408. /**
  409. * Appends bytes to a bit array in a specific mode.
  410. *
  411. * @throws WriterException if an invalid mode was supplied
  412. */
  413. private static function appendBytes(string $content, Mode $mode, BitArray $bits, string $encoding) : void
  414. {
  415. switch ($mode) {
  416. case Mode::NUMERIC():
  417. self::appendNumericBytes($content, $bits);
  418. break;
  419. case Mode::ALPHANUMERIC():
  420. self::appendAlphanumericBytes($content, $bits);
  421. break;
  422. case Mode::BYTE():
  423. self::append8BitBytes($content, $bits, $encoding);
  424. break;
  425. case Mode::KANJI():
  426. self::appendKanjiBytes($content, $bits);
  427. break;
  428. default:
  429. throw new WriterException('Invalid mode: ' . $mode);
  430. }
  431. }
  432. /**
  433. * Appends numeric bytes to a bit array.
  434. */
  435. private static function appendNumericBytes(string $content, BitArray $bits) : void
  436. {
  437. $length = strlen($content);
  438. $i = 0;
  439. while ($i < $length) {
  440. $num1 = (int) $content[$i];
  441. if ($i + 2 < $length) {
  442. // Encode three numeric letters in ten bits.
  443. $num2 = (int) $content[$i + 1];
  444. $num3 = (int) $content[$i + 2];
  445. $bits->appendBits($num1 * 100 + $num2 * 10 + $num3, 10);
  446. $i += 3;
  447. } elseif ($i + 1 < $length) {
  448. // Encode two numeric letters in seven bits.
  449. $num2 = (int) $content[$i + 1];
  450. $bits->appendBits($num1 * 10 + $num2, 7);
  451. $i += 2;
  452. } else {
  453. // Encode one numeric letter in four bits.
  454. $bits->appendBits($num1, 4);
  455. ++$i;
  456. }
  457. }
  458. }
  459. /**
  460. * Appends alpha-numeric bytes to a bit array.
  461. *
  462. * @throws WriterException if an invalid alphanumeric code was found
  463. */
  464. private static function appendAlphanumericBytes(string $content, BitArray $bits) : void
  465. {
  466. $length = strlen($content);
  467. $i = 0;
  468. while ($i < $length) {
  469. $code1 = self::getAlphanumericCode(ord($content[$i]));
  470. if (-1 === $code1) {
  471. throw new WriterException('Invalid alphanumeric code');
  472. }
  473. if ($i + 1 < $length) {
  474. $code2 = self::getAlphanumericCode(ord($content[$i + 1]));
  475. if (-1 === $code2) {
  476. throw new WriterException('Invalid alphanumeric code');
  477. }
  478. // Encode two alphanumeric letters in 11 bits.
  479. $bits->appendBits($code1 * 45 + $code2, 11);
  480. $i += 2;
  481. } else {
  482. // Encode one alphanumeric letter in six bits.
  483. $bits->appendBits($code1, 6);
  484. ++$i;
  485. }
  486. }
  487. }
  488. /**
  489. * Appends regular 8-bit bytes to a bit array.
  490. *
  491. * @throws WriterException if content cannot be encoded to target encoding
  492. */
  493. private static function append8BitBytes(string $content, BitArray $bits, string $encoding) : void
  494. {
  495. $bytes = @iconv('utf-8', $encoding, $content);
  496. if (false === $bytes) {
  497. throw new WriterException('Could not encode content to ' . $encoding);
  498. }
  499. $length = strlen($bytes);
  500. for ($i = 0; $i < $length; $i++) {
  501. $bits->appendBits(ord($bytes[$i]), 8);
  502. }
  503. }
  504. /**
  505. * Appends KANJI bytes to a bit array.
  506. *
  507. * @throws WriterException if content does not seem to be encoded in SHIFT-JIS
  508. * @throws WriterException if an invalid byte sequence occurs
  509. */
  510. private static function appendKanjiBytes(string $content, BitArray $bits) : void
  511. {
  512. if (strlen($content) % 2 > 0) {
  513. // We just do a simple length check here. The for loop will check
  514. // individual characters.
  515. throw new WriterException('Content does not seem to be encoded in SHIFT-JIS');
  516. }
  517. $length = strlen($content);
  518. for ($i = 0; $i < $length; $i += 2) {
  519. $byte1 = ord($content[$i]) & 0xff;
  520. $byte2 = ord($content[$i + 1]) & 0xff;
  521. $code = ($byte1 << 8) | $byte2;
  522. if ($code >= 0x8140 && $code <= 0x9ffc) {
  523. $subtracted = $code - 0x8140;
  524. } elseif ($code >= 0xe040 && $code <= 0xebbf) {
  525. $subtracted = $code - 0xc140;
  526. } else {
  527. throw new WriterException('Invalid byte sequence');
  528. }
  529. $encoded = (($subtracted >> 8) * 0xc0) + ($subtracted & 0xff);
  530. $bits->appendBits($encoded, 13);
  531. }
  532. }
  533. /**
  534. * Appends ECI information to a bit array.
  535. */
  536. private static function appendEci(CharacterSetEci $eci, BitArray $bits) : void
  537. {
  538. $mode = Mode::ECI();
  539. $bits->appendBits($mode->getBits(), 4);
  540. $bits->appendBits($eci->getValue(), 8);
  541. }
  542. }