Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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