Kanji.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Class Kanji
  4. *
  5. * @filesource Kanji.php
  6. * @created 25.11.2015
  7. * @package chillerlan\QRCode\Data
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode\Data;
  13. use chillerlan\QRCode\BitBuffer;
  14. /**
  15. *
  16. */
  17. class Kanji extends QRDataAbstract{
  18. /**
  19. * @var int
  20. */
  21. public $mode = self::MODE_KANJI;
  22. /**
  23. * @var array
  24. */
  25. protected $lengthBits = [8, 10, 12];
  26. /**
  27. * @param \chillerlan\QRCode\BitBuffer $buffer
  28. *
  29. * @return void
  30. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  31. */
  32. public function write(BitBuffer &$buffer){
  33. $i = 0;
  34. while($i + 1 < $this->dataLength){
  35. $c = ((0xff&ord($this->data[$i])) << 8)|(0xff&ord($this->data[$i + 1]));
  36. if(0x8140 <= $c && $c <= 0x9FFC){
  37. $c -= 0x8140;
  38. }
  39. else if(0xE040 <= $c && $c <= 0xEBBF){
  40. $c -= 0xC140;
  41. }
  42. else{
  43. throw new QRCodeDataException('illegal char at '.($i + 1).' ('.$c.')');
  44. }
  45. $buffer->put((($c >> 8)&0xff) * 0xC0 + ($c&0xff), 13);
  46. $i += 2;
  47. }
  48. if($i < $this->dataLength){
  49. throw new QRCodeDataException('illegal char at '.($i + 1));
  50. }
  51. }
  52. }