Rectangle.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package Fpdi
  6. * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. */
  9. namespace Fpdi\PdfReader\DataStructure;
  10. use Fpdi\PdfParser\CrossReference\CrossReferenceException;
  11. use Fpdi\PdfParser\PdfParser;
  12. use Fpdi\PdfParser\PdfParserException;
  13. use Fpdi\PdfParser\Type\PdfArray;
  14. use Fpdi\PdfParser\Type\PdfNumeric;
  15. use Fpdi\PdfParser\Type\PdfType;
  16. use Fpdi\PdfParser\Type\PdfTypeException;
  17. /**
  18. * Class representing a rectangle
  19. */
  20. class Rectangle
  21. {
  22. /**
  23. * @var int|float
  24. */
  25. protected $llx;
  26. /**
  27. * @var int|float
  28. */
  29. protected $lly;
  30. /**
  31. * @var int|float
  32. */
  33. protected $urx;
  34. /**
  35. * @var int|float
  36. */
  37. protected $ury;
  38. /**
  39. * Create a rectangle instance by a PdfArray.
  40. *
  41. * @param PdfArray|mixed $array
  42. * @param PdfParser $parser
  43. * @return Rectangle
  44. * @throws PdfTypeException
  45. * @throws CrossReferenceException
  46. * @throws PdfParserException
  47. */
  48. public static function byPdfArray($array, PdfParser $parser)
  49. {
  50. $array = PdfArray::ensure(PdfType::resolve($array, $parser), 4)->value;
  51. $ax = PdfNumeric::ensure(PdfType::resolve($array[0], $parser))->value;
  52. $ay = PdfNumeric::ensure(PdfType::resolve($array[1], $parser))->value;
  53. $bx = PdfNumeric::ensure(PdfType::resolve($array[2], $parser))->value;
  54. $by = PdfNumeric::ensure(PdfType::resolve($array[3], $parser))->value;
  55. return new self($ax, $ay, $bx, $by);
  56. }
  57. /**
  58. * Rectangle constructor.
  59. *
  60. * @param float|int $ax
  61. * @param float|int $ay
  62. * @param float|int $bx
  63. * @param float|int $by
  64. */
  65. public function __construct($ax, $ay, $bx, $by)
  66. {
  67. $this->llx = \min($ax, $bx);
  68. $this->lly = \min($ay, $by);
  69. $this->urx = \max($ax, $bx);
  70. $this->ury = \max($ay, $by);
  71. }
  72. /**
  73. * Get the width of the rectangle.
  74. *
  75. * @return float|int
  76. */
  77. public function getWidth()
  78. {
  79. return $this->urx - $this->llx;
  80. }
  81. /**
  82. * Get the height of the rectangle.
  83. *
  84. * @return float|int
  85. */
  86. public function getHeight()
  87. {
  88. return $this->ury - $this->lly;
  89. }
  90. /**
  91. * Get the lower left abscissa.
  92. *
  93. * @return float|int
  94. */
  95. public function getLlx()
  96. {
  97. return $this->llx;
  98. }
  99. /**
  100. * Get the lower left ordinate.
  101. *
  102. * @return float|int
  103. */
  104. public function getLly()
  105. {
  106. return $this->lly;
  107. }
  108. /**
  109. * Get the upper right abscissa.
  110. *
  111. * @return float|int
  112. */
  113. public function getUrx()
  114. {
  115. return $this->urx;
  116. }
  117. /**
  118. * Get the upper right ordinate.
  119. *
  120. * @return float|int
  121. */
  122. public function getUry()
  123. {
  124. return $this->ury;
  125. }
  126. /**
  127. * Get the rectangle as an array.
  128. *
  129. * @return array
  130. */
  131. public function toArray()
  132. {
  133. return [
  134. $this->llx,
  135. $this->lly,
  136. $this->urx,
  137. $this->ury
  138. ];
  139. }
  140. /**
  141. * Get the rectangle as a PdfArray.
  142. *
  143. * @return PdfArray
  144. */
  145. public function toPdfArray()
  146. {
  147. $array = new PdfArray();
  148. $array->value[] = PdfNumeric::create($this->llx);
  149. $array->value[] = PdfNumeric::create($this->lly);
  150. $array->value[] = PdfNumeric::create($this->urx);
  151. $array->value[] = PdfNumeric::create($this->ury);
  152. return $array;
  153. }
  154. }