Skip to content

Commit d2166d9

Browse files
Moving a Group (shape container) now moves all contained shapes.
OffsetX/Y, ExtentX/Y, Width and Height are always calculated to prevent desync between Group and contained shapes. Fixed writing Group size into PowerPoint2007 format.
1 parent f5907e1 commit d2166d9

File tree

2 files changed

+73
-31
lines changed

2 files changed

+73
-31
lines changed

src/PhpPresentation/Shape/Group.php

+69-27
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,33 @@ public function addShape(AbstractShape $shape): AbstractShape
8383
*/
8484
public function getOffsetX(): int
8585
{
86-
if (empty($this->offsetX)) {
87-
$offsets = GeometryCalculator::calculateOffsets($this);
88-
$this->offsetX = $offsets[GeometryCalculator::X];
89-
$this->offsetY = $offsets[GeometryCalculator::Y];
86+
$offsetX = null;
87+
88+
foreach ($this->getShapeCollection() as $shape) {
89+
if ($offsetX === null) {
90+
$offsetX = $shape->getOffsetX();
91+
} else {
92+
$offsetX = \min($offsetX, $shape->getOffsetX());
93+
}
9094
}
9195

92-
return $this->offsetX;
96+
return $offsetX ?? 0;
9397
}
9498

9599
/**
96-
* Ignores setting the X Offset, preserving the default behavior.
100+
* Change the X offset by moving all contained shapes
97101
*
98102
* @return $this
99103
*/
100-
public function setOffsetX(int $pValue = 0)
104+
public function setOffsetX(int $pValue = 0): self
101105
{
106+
$offsetX = $this->getOffsetX();
107+
$diff = $pValue - $offsetX;
108+
109+
foreach ($this->getShapeCollection() as $shape) {
110+
$shape->setOffsetX($shape->getOffsetX() + $diff);
111+
}
112+
102113
return $this;
103114
}
104115

@@ -107,22 +118,33 @@ public function setOffsetX(int $pValue = 0)
107118
*/
108119
public function getOffsetY(): int
109120
{
110-
if (empty($this->offsetY)) {
111-
$offsets = GeometryCalculator::calculateOffsets($this);
112-
$this->offsetX = $offsets[GeometryCalculator::X];
113-
$this->offsetY = $offsets[GeometryCalculator::Y];
121+
$offsetY = null;
122+
123+
foreach ($this->getShapeCollection() as $shape) {
124+
if ($offsetY === null) {
125+
$offsetY = $shape->getOffsetY();
126+
} else {
127+
$offsetY = \min($offsetY, $shape->getOffsetY());
128+
}
114129
}
115130

116-
return $this->offsetY;
131+
return $offsetY ?? 0;
117132
}
118133

119134
/**
120-
* Ignores setting the Y Offset, preserving the default behavior.
135+
* Change the Y offset by moving all contained shapes
121136
*
122137
* @return $this
123138
*/
124-
public function setOffsetY(int $pValue = 0)
139+
public function setOffsetY(int $pValue = 0): self
125140
{
141+
$offsetY = $this->getOffsetY();
142+
$diff = $pValue - $offsetY;
143+
144+
foreach ($this->getShapeCollection() as $shape) {
145+
$shape->setOffsetY($shape->getOffsetY() + $diff);
146+
}
147+
126148
return $this;
127149
}
128150

@@ -131,35 +153,55 @@ public function setOffsetY(int $pValue = 0)
131153
*/
132154
public function getExtentX(): int
133155
{
134-
if (null === $this->extentX) {
135-
$extents = GeometryCalculator::calculateExtents($this);
136-
$this->extentX = $extents[GeometryCalculator::X] - $this->getOffsetX();
137-
$this->extentY = $extents[GeometryCalculator::Y] - $this->getOffsetY();
156+
$extentX = 0;
157+
158+
foreach ($this->getShapeCollection() as $shape) {
159+
$extentX = \max($extentX, $shape->getOffsetX() + $shape->getWidth());
138160
}
139161

140-
return $this->extentX;
162+
return $extentX;
141163
}
142164

143165
/**
144166
* Get Y Extent.
145167
*/
146168
public function getExtentY(): int
147169
{
148-
if (null === $this->extentY) {
149-
$extents = GeometryCalculator::calculateExtents($this);
150-
$this->extentX = $extents[GeometryCalculator::X] - $this->getOffsetX();
151-
$this->extentY = $extents[GeometryCalculator::Y] - $this->getOffsetY();
170+
$extentY = 0;
171+
172+
foreach ($this->getShapeCollection() as $shape) {
173+
$extentY = \max($extentY, $shape->getOffsetY() + $shape->getHeight());
152174
}
153175

154-
return $this->extentY;
176+
return $extentY;
177+
}
178+
179+
/**
180+
* Calculate the width based on the size/position of the contained shapes
181+
*
182+
* @return int
183+
*/
184+
public function getWidth(): int
185+
{
186+
return $this->getExtentX() - $this->getOffsetX();
155187
}
156188

189+
/**
190+
* Calculate the height based on the size/position of the contained shapes
191+
*
192+
* @return int
193+
*/
194+
public function getHeight(): int
195+
{
196+
return $this->getExtentY() - $this->getOffsetY();
197+
}
198+
157199
/**
158200
* Ignores setting the width, preserving the default behavior.
159201
*
160-
* @return self
202+
* @return $this
161203
*/
162-
public function setWidth(int $pValue = 0)
204+
public function setWidth(int $pValue = 0): self
163205
{
164206
return $this;
165207
}
@@ -169,7 +211,7 @@ public function setWidth(int $pValue = 0)
169211
*
170212
* @return $this
171213
*/
172-
public function setHeight(int $pValue = 0)
214+
public function setHeight(int $pValue = 0): self
173215
{
174216
return $this;
175217
}

src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1446,8 +1446,8 @@ protected function writeShapeGroup(XMLWriter $objWriter, Group $group, int &$sha
14461446
$objWriter->endElement(); // a:off
14471447
// a:ext
14481448
$objWriter->startElement('a:ext');
1449-
$objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($group->getExtentX()));
1450-
$objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($group->getExtentY()));
1449+
$objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($group->getWidth()));
1450+
$objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($group->getHeight()));
14511451
$objWriter->endElement(); // a:ext
14521452
// a:chOff
14531453
$objWriter->startElement('a:chOff');
@@ -1456,8 +1456,8 @@ protected function writeShapeGroup(XMLWriter $objWriter, Group $group, int &$sha
14561456
$objWriter->endElement(); // a:chOff
14571457
// a:chExt
14581458
$objWriter->startElement('a:chExt');
1459-
$objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($group->getExtentX()));
1460-
$objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($group->getExtentY()));
1459+
$objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($group->getWidth()));
1460+
$objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($group->getHeight()));
14611461
$objWriter->endElement(); // a:chExt
14621462
$objWriter->endElement(); // a:xfrm
14631463
$objWriter->endElement(); // p:grpSpPr

0 commit comments

Comments
 (0)