FrameFiller.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * FrameFiller.php
  4. *
  5. * Created by arielferrandini
  6. */
  7. namespace PHPQRCode;
  8. class FrameFiller {
  9. public $width;
  10. public $frame;
  11. public $x;
  12. public $y;
  13. public $dir;
  14. public $bit;
  15. //----------------------------------------------------------------------
  16. public function __construct($width, &$frame)
  17. {
  18. $this->width = $width;
  19. $this->frame = $frame;
  20. $this->x = $width - 1;
  21. $this->y = $width - 1;
  22. $this->dir = -1;
  23. $this->bit = -1;
  24. }
  25. //----------------------------------------------------------------------
  26. public function setFrameAt($at, $val)
  27. {
  28. $this->frame[$at['y']][$at['x']] = chr($val);
  29. }
  30. //----------------------------------------------------------------------
  31. public function getFrameAt($at)
  32. {
  33. return ord($this->frame[$at['y']][$at['x']]);
  34. }
  35. //----------------------------------------------------------------------
  36. public function next()
  37. {
  38. do {
  39. if($this->bit == -1) {
  40. $this->bit = 0;
  41. return array('x'=>$this->x, 'y'=>$this->y);
  42. }
  43. $x = $this->x;
  44. $y = $this->y;
  45. $w = $this->width;
  46. if($this->bit == 0) {
  47. $x--;
  48. $this->bit++;
  49. } else {
  50. $x++;
  51. $y += $this->dir;
  52. $this->bit--;
  53. }
  54. if($this->dir < 0) {
  55. if($y < 0) {
  56. $y = 0;
  57. $x -= 2;
  58. $this->dir = 1;
  59. if($x == 6) {
  60. $x--;
  61. $y = 9;
  62. }
  63. }
  64. } else {
  65. if($y == $w) {
  66. $y = $w - 1;
  67. $x -= 2;
  68. $this->dir = -1;
  69. if($x == 6) {
  70. $x--;
  71. $y -= 8;
  72. }
  73. }
  74. }
  75. if($x < 0 || $y < 0) return null;
  76. $this->x = $x;
  77. $this->y = $y;
  78. } while(ord($this->frame[$y][$x]) & 0x80);
  79. return array('x'=>$x, 'y'=>$y);
  80. }
  81. } ;