1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\shape; |
3
|
|
|
|
4
|
|
|
use nstdio\svg\ElementInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Rect |
8
|
|
|
* The 'rect' element defines a rectangle which is axis-aligned with the current user coordinate system. Rounded |
9
|
|
|
* rectangles can be achieved by setting appropriate values for attributes 'rx' and 'ry'. |
10
|
|
|
* |
11
|
|
|
* @link https://www.w3.org/TR/SVG11/shapes.html#RectElement |
12
|
|
|
* @property float rx For rounded rectangles, the x-axis radius of the ellipse used to round off the corners of the |
13
|
|
|
* rectangle. A negative value is an error |
14
|
|
|
* @property float ry For rounded rectangles, the y-axis radius of the ellipse used to round off the corners of the |
15
|
|
|
* rectangle. A negative value is an error |
16
|
|
|
* @property float x The x-axis coordinate of the side of the rectangle which has the smaller x-axis coordinate value |
17
|
|
|
* in the current user coordinate system. If the attribute is not specified, the effect is as if a value of |
18
|
|
|
* "0" were specified. |
19
|
|
|
* @property float y The y-axis coordinate of the side of the rectangle which has the smaller y-axis coordinate value |
20
|
|
|
* in the current user coordinate system. If the attribute is not specified, the effect is as if a value of |
21
|
|
|
* "0" were specified. |
22
|
|
|
* @package nstdio\svg\shape |
23
|
|
|
* @author Edgar Asatryan <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Rect extends Shape |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Rect constructor. |
29
|
|
|
* |
30
|
|
|
* @param ElementInterface $parent |
31
|
|
|
* @param $height |
32
|
|
|
* @param $width |
33
|
|
|
* @param int $x |
34
|
|
|
* @param int $y |
35
|
|
|
*/ |
36
|
27 |
|
public function __construct(ElementInterface $parent, $height, $width, $x = 0, $y = 0) |
37
|
|
|
{ |
38
|
27 |
|
parent::__construct($parent); |
39
|
|
|
|
40
|
27 |
|
$this->height = $height; |
41
|
27 |
|
$this->width = $width; |
42
|
27 |
|
$this->x = $x; |
43
|
27 |
|
$this->y = $y; |
44
|
27 |
|
} |
45
|
|
|
|
46
|
|
|
public static function createFromPointsArray(ElementInterface $parent, $box) |
47
|
|
|
{ |
48
|
|
|
$bbox = self::boxFromPointsArray($box); |
49
|
|
|
|
50
|
|
|
return self::create($parent, $bbox['height'], $bbox['width'], $bbox['x'], $bbox['y']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $box array The array with size equals 4. |
55
|
|
|
* |
56
|
|
|
* @return array|null |
57
|
|
|
*/ |
58
|
|
|
public static function boxFromPointsArray(array $box) |
59
|
|
|
{ |
60
|
|
|
if (count($box) !== 4) { |
61
|
|
|
throw new \InvalidArgumentException('$box must have 4 elements'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return self::boxFromPoints($box[0], $box[1], $box[2], $box[3]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $x1 |
69
|
|
|
* @param $y1 |
70
|
|
|
* @param $x2 |
71
|
|
|
* @param $y2 |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
17 |
|
public static function boxFromPoints($x1, $y1, $x2, $y2) |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
17 |
|
'width' => max($x1, $x2) - min($x1, $x2), |
79
|
17 |
|
'height' => max($y1, $y2) - min($y1, $y2), |
80
|
17 |
|
'x' => min($x1, $x2), |
81
|
17 |
|
'y' => min($y1, $y2), |
82
|
17 |
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ElementInterface $parent |
87
|
|
|
* @param $height |
88
|
|
|
* @param $width |
89
|
|
|
* @param int $x |
90
|
|
|
* @param int $y |
91
|
|
|
* |
92
|
|
|
* @return Rect |
93
|
|
|
*/ |
94
|
|
|
public static function create(ElementInterface $parent, $height, $width, $x = 0, $y = 0) |
95
|
|
|
{ |
96
|
|
|
return new Rect($parent, $height, $width, $x, $y); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param float $radius |
101
|
|
|
*/ |
102
|
3 |
|
public function setBorderRadius($radius) |
103
|
|
|
{ |
104
|
3 |
|
$this->rx = $radius; |
105
|
3 |
|
$this->ry = $radius; |
106
|
3 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Path |
110
|
|
|
*/ |
111
|
2 |
|
public function toPath() |
112
|
|
|
{ |
113
|
2 |
|
$path = new Path($this->getRoot(), $this->x + $this->rx, $this->y); |
|
|
|
|
114
|
2 |
|
list($x, $y, $width, $height, $rx, $ry) = [$this->x, $this->y, $this->width, $this->height, $this->rx, $this->ry]; |
115
|
2 |
|
if ($rx === null) { |
116
|
1 |
|
$rx = 0; |
117
|
1 |
|
} |
118
|
2 |
|
if ($ry === null) { |
119
|
1 |
|
$ry = 0; |
120
|
1 |
|
} |
121
|
2 |
|
$path->hLineTo($x + $width - $rx) |
122
|
2 |
|
->arcTo($rx, $ry, 0, false, true, $x + $width, $y + $ry) |
123
|
2 |
|
->lineTo($x + $width, $y + $height - $ry) |
124
|
2 |
|
->arcTo($rx, $ry, 0, false, true, $x + $width - $rx, $y + $height) |
125
|
2 |
|
->lineTo($x + $rx, $y + $height) |
126
|
2 |
|
->arcTo($rx, $ry, 0, false, true, $x, $y + $height - $ry) |
127
|
2 |
|
->lineTo($x, $y + $ry) |
128
|
2 |
|
->arcTo($rx, $ry, 0, false, true, $x + $rx, $y); |
129
|
|
|
|
130
|
2 |
|
$attributes = $this->allAttributes(['width', 'height', 'x', 'y', 'rx', 'ry']); |
131
|
|
|
|
132
|
2 |
|
foreach ($attributes as $key => $value) { |
133
|
1 |
|
$path->{$key} = $value; |
134
|
2 |
|
} |
135
|
|
|
|
136
|
2 |
|
return $path; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
27 |
|
public function getName() |
143
|
|
|
{ |
144
|
27 |
|
return 'rect'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $rect1 |
149
|
|
|
* @param array $rect2 |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
15 |
|
public static function union(array $rect1, array $rect2) |
154
|
|
|
{ |
155
|
15 |
|
$result = []; |
156
|
15 |
|
$result['x'] = min($rect1['x'], $rect2['x']); |
157
|
15 |
|
$result['y'] = min($rect1['y'], $rect2['y']); |
158
|
|
|
|
159
|
15 |
|
$rect1Width = $rect1['x'] + $rect1['width'] - min($rect1['x'], $rect2['x']); |
160
|
15 |
|
$rect2Width = $rect2['x'] + $rect2['width'] - min($rect1['x'], $rect2['x']); |
161
|
|
|
|
162
|
|
|
|
163
|
15 |
|
$rect1Height = $rect1['y'] + $rect1['height'] - min($rect1['y'], $rect2['y']); |
164
|
15 |
|
$rect2Height = $rect2['y'] + $rect2['height'] - min($rect1['y'], $rect2['y']); |
165
|
|
|
|
166
|
15 |
|
$result['width'] = max($rect1Width, $rect2Width); |
167
|
15 |
|
$result['height'] = max($rect1Height, $rect2Height); |
168
|
|
|
|
169
|
15 |
|
return $result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return float |
174
|
|
|
*/ |
175
|
1 |
|
public function getCenterX() |
176
|
|
|
{ |
177
|
1 |
|
return $this->x + ($this->width / 2); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
1 |
|
public function getBoundingBox() |
184
|
|
|
{ |
185
|
|
|
return [ |
186
|
1 |
|
'width' => $this->width, |
187
|
1 |
|
'height' => $this->height, |
188
|
1 |
|
'x' => $this->x, |
189
|
1 |
|
'y' => $this->y, |
190
|
1 |
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return float |
195
|
|
|
*/ |
196
|
1 |
|
protected function getCenterY() |
197
|
|
|
{ |
198
|
1 |
|
return $this->y + ($this->width / 2); |
199
|
|
|
} |
200
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: