20
20
namespace PhpOffice \PhpPresentation \Shape ;
21
21
22
22
use PhpOffice \PhpPresentation \AbstractShape ;
23
- use PhpOffice \PhpPresentation \GeometryCalculator ;
24
23
use PhpOffice \PhpPresentation \ShapeContainerInterface ;
25
24
use PhpOffice \PhpPresentation \Traits \ShapeCollection ;
26
25
27
26
class Group extends AbstractShape implements ShapeContainerInterface
28
27
{
29
28
use ShapeCollection;
30
29
31
- /**
32
- * Extent X.
33
- *
34
- * @var int
35
- */
36
- protected $ extentX ;
37
-
38
- /**
39
- * Extent Y.
40
- *
41
- * @var int
42
- */
43
- protected $ extentY ;
44
-
45
30
public function __construct ()
46
31
{
47
32
parent ::__construct ();
@@ -52,22 +37,33 @@ public function __construct()
52
37
*/
53
38
public function getOffsetX (): int
54
39
{
55
- if (empty ($ this ->offsetX )) {
56
- $ offsets = GeometryCalculator::calculateOffsets ($ this );
57
- $ this ->offsetX = $ offsets [GeometryCalculator::X];
58
- $ this ->offsetY = $ offsets [GeometryCalculator::Y];
40
+ $ offsetX = null ;
41
+
42
+ foreach ($ this ->getShapeCollection () as $ shape ) {
43
+ if ($ offsetX === null ) {
44
+ $ offsetX = $ shape ->getOffsetX ();
45
+ } else {
46
+ $ offsetX = \min ($ offsetX , $ shape ->getOffsetX ());
47
+ }
59
48
}
60
49
61
- return $ this -> offsetX ;
50
+ return $ offsetX ?? 0 ;
62
51
}
63
52
64
53
/**
65
- * Ignores setting the X Offset, preserving the default behavior .
54
+ * Change the X offset by moving all contained shapes .
66
55
*
67
56
* @return $this
68
57
*/
69
- public function setOffsetX (int $ pValue = 0 )
58
+ public function setOffsetX (int $ pValue = 0 ): self
70
59
{
60
+ $ offsetX = $ this ->getOffsetX ();
61
+ $ diff = $ pValue - $ offsetX ;
62
+
63
+ foreach ($ this ->getShapeCollection () as $ shape ) {
64
+ $ shape ->setOffsetX ($ shape ->getOffsetX () + $ diff );
65
+ }
66
+
71
67
return $ this ;
72
68
}
73
69
@@ -76,22 +72,33 @@ public function setOffsetX(int $pValue = 0)
76
72
*/
77
73
public function getOffsetY (): int
78
74
{
79
- if (empty ($ this ->offsetY )) {
80
- $ offsets = GeometryCalculator::calculateOffsets ($ this );
81
- $ this ->offsetX = $ offsets [GeometryCalculator::X];
82
- $ this ->offsetY = $ offsets [GeometryCalculator::Y];
75
+ $ offsetY = null ;
76
+
77
+ foreach ($ this ->getShapeCollection () as $ shape ) {
78
+ if ($ offsetY === null ) {
79
+ $ offsetY = $ shape ->getOffsetY ();
80
+ } else {
81
+ $ offsetY = \min ($ offsetY , $ shape ->getOffsetY ());
82
+ }
83
83
}
84
84
85
- return $ this -> offsetY ;
85
+ return $ offsetY ?? 0 ;
86
86
}
87
87
88
88
/**
89
- * Ignores setting the Y Offset, preserving the default behavior .
89
+ * Change the Y offset by moving all contained shapes .
90
90
*
91
91
* @return $this
92
92
*/
93
- public function setOffsetY (int $ pValue = 0 )
93
+ public function setOffsetY (int $ pValue = 0 ): self
94
94
{
95
+ $ offsetY = $ this ->getOffsetY ();
96
+ $ diff = $ pValue - $ offsetY ;
97
+
98
+ foreach ($ this ->getShapeCollection () as $ shape ) {
99
+ $ shape ->setOffsetY ($ shape ->getOffsetY () + $ diff );
100
+ }
101
+
95
102
return $ this ;
96
103
}
97
104
@@ -100,35 +107,51 @@ public function setOffsetY(int $pValue = 0)
100
107
*/
101
108
public function getExtentX (): int
102
109
{
103
- if ( null === $ this -> extentX ) {
104
- $ extents = GeometryCalculator:: calculateExtents ( $ this );
105
- $ this ->extentX = $ extents [GeometryCalculator::X] - $ this -> getOffsetX ();
106
- $ this -> extentY = $ extents [GeometryCalculator::Y] - $ this -> getOffsetY ( );
110
+ $ extentX = 0 ;
111
+
112
+ foreach ( $ this ->getShapeCollection () as $ shape ) {
113
+ $ extentX = \max ( $ extentX , $ shape -> getOffsetX () + $ shape -> getWidth () );
107
114
}
108
115
109
- return $ this ->extentX ;
116
+ return $ extentX - $ this ->getOffsetX () ;
110
117
}
111
118
112
119
/**
113
120
* Get Y Extent.
114
121
*/
115
122
public function getExtentY (): int
116
123
{
117
- if ( null === $ this -> extentY ) {
118
- $ extents = GeometryCalculator:: calculateExtents ( $ this );
119
- $ this ->extentX = $ extents [GeometryCalculator::X] - $ this -> getOffsetX ();
120
- $ this -> extentY = $ extents [GeometryCalculator::Y] - $ this -> getOffsetY ( );
124
+ $ extentY = 0 ;
125
+
126
+ foreach ( $ this ->getShapeCollection () as $ shape ) {
127
+ $ extentY = \max ( $ extentY , $ shape -> getOffsetY () + $ shape -> getHeight () );
121
128
}
122
129
123
- return $ this ->extentY ;
130
+ return $ extentY - $ this ->getOffsetY ();
131
+ }
132
+
133
+ /**
134
+ * Calculate the width based on the size/position of the contained shapes.
135
+ */
136
+ public function getWidth (): int
137
+ {
138
+ return $ this ->getExtentX ();
139
+ }
140
+
141
+ /**
142
+ * Calculate the height based on the size/position of the contained shapes.
143
+ */
144
+ public function getHeight (): int
145
+ {
146
+ return $ this ->getExtentY ();
124
147
}
125
148
126
149
/**
127
150
* Ignores setting the width, preserving the default behavior.
128
151
*
129
- * @return self
152
+ * @return $this
130
153
*/
131
- public function setWidth (int $ pValue = 0 )
154
+ public function setWidth (int $ pValue = 0 ): self
132
155
{
133
156
return $ this ;
134
157
}
@@ -138,7 +161,7 @@ public function setWidth(int $pValue = 0)
138
161
*
139
162
* @return $this
140
163
*/
141
- public function setHeight (int $ pValue = 0 )
164
+ public function setHeight (int $ pValue = 0 ): self
142
165
{
143
166
return $ this ;
144
167
}
0 commit comments