Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

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