Skip to content

Commit 321bf4f

Browse files
committed
#42 : Fix "BUG: Repair Error / Wrong anchor if you don't set vertical alignment different to VERTICAL_BASE"
1 parent b346a4a commit 321bf4f

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Bugfix
1111
- PowerPoint2007 Writer : Powerpoint Repair Error in Office 2010 - @Progi1984 GH-39
12+
- PowerPoint2007 Writer : BUG: Repair Error / Wrong anchor if you don't set vertical alignment different to VERTICAL_BASE - @fregge GH-42
1213

1314
### Miscellaneous
1415
- QA : Move AbstractType for Chart - @Progi1984

src/PhpPowerpoint/Writer/PowerPoint2007/Slide.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ private function writeShapeText(XMLWriter $objWriter, RichText $shape, $shapeId)
419419

420420
// a:bodyPr
421421
$objWriter->startElement('a:bodyPr');
422-
$objWriter->writeAttribute('anchor', $shape->getActiveParagraph()->getAlignment()->getVertical());
422+
$verticalAlign = $shape->getActiveParagraph()->getAlignment()->getVertical();
423+
if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) {
424+
$objWriter->writeAttribute('anchor', $verticalAlign);
425+
}
423426
$objWriter->writeAttribute('wrap', $shape->getWrap());
424427
$objWriter->writeAttribute('rtlCol', '0');
425428

@@ -624,9 +627,9 @@ private function writeShapeTable(XMLWriter $objWriter, Table $shape, $shapeId)
624627
$objWriter->startElement('a:tcPr');
625628
// Alignment (horizontal)
626629
$firstParagraph = $currentCell->getParagraph(0);
627-
$horizontalAlign = $firstParagraph->getAlignment()->getVertical();
628-
if ($horizontalAlign != Alignment::VERTICAL_BASE && $horizontalAlign != Alignment::VERTICAL_AUTO) {
629-
$objWriter->writeAttribute('anchor', $horizontalAlign);
630+
$verticalAlign = $firstParagraph->getAlignment()->getVertical();
631+
if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) {
632+
$objWriter->writeAttribute('anchor', $verticalAlign);
630633
}
631634

632635
// Determine borders

tests/PhpPowerpoint/Tests/Writer/PowerPoint2007/SlideTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ public function testConstructException()
5353
$oSlide = new Slide();
5454
$oSlide->writeSlide();
5555
}
56+
57+
/**
58+
* @link https://github.com/PHPOffice/PHPPowerPoint/issues/42
59+
*/
60+
public function testAlignmentShapeAuto()
61+
{
62+
$phpPowerPoint = new PhpPowerpoint();
63+
$oSlide = $phpPowerPoint->getActiveSlide();
64+
$oShape = $oSlide->createRichTextShape()->setWidth(400)->setHeight(400)->setOffsetX(100)->setOffsetY(100);
65+
$oShape->createTextRun('this text should be vertically aligned');
66+
$oShape->getActiveParagraph()->getAlignment()->setVertical(Alignment::VERTICAL_AUTO);
67+
68+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'PowerPoint2007');
69+
$element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:bodyPr';
70+
$this->assertTrue($pres->elementExists($element, 'ppt/slides/slide1.xml'));
71+
$this->assertFalse($pres->attributeElementExists($element, 'anchor', 'ppt/slides/slide1.xml'));
72+
}
73+
74+
/**
75+
* @link https://github.com/PHPOffice/PHPPowerPoint/issues/42
76+
*/
77+
public function testAlignmentShapeBase()
78+
{
79+
$phpPowerPoint = new PhpPowerpoint();
80+
$oSlide = $phpPowerPoint->getActiveSlide();
81+
$oShape = $oSlide->createRichTextShape()->setWidth(400)->setHeight(400)->setOffsetX(100)->setOffsetY(100);
82+
$oShape->createTextRun('this text should be vertically aligned');
83+
$oShape->getActiveParagraph()->getAlignment()->setVertical(Alignment::VERTICAL_BASE);
84+
85+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'PowerPoint2007');
86+
$element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:bodyPr';
87+
$this->assertTrue($pres->elementExists($element, 'ppt/slides/slide1.xml'));
88+
$this->assertFalse($pres->attributeElementExists($element, 'anchor', 'ppt/slides/slide1.xml'));
89+
}
5690

5791
/**
5892
* @link https://github.com/PHPOffice/PHPPowerPoint/issues/35

0 commit comments

Comments
 (0)