Skip to content

Commit 8646927

Browse files
committed
#28 : ODPresentation Writer : Ability to set auto shrink text
1 parent 4adb5bd commit 8646927

File tree

6 files changed

+170
-15
lines changed

6 files changed

+170
-15
lines changed

docs/shapes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Below are the properties that you can set for a rich text shape.
4444
- ``leftInset`` in pixels
4545
- ``rightInset`` in pixels
4646
- ``topInset`` in pixels
47+
- ``autoShrinkHorizontal`` (boolean)
48+
- ``autoShrinkVertical`` (boolean)
4749

4850
Properties that can be set for each paragraphs are as follow.
4951

src/PhpPowerpoint/Shape/RichText.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ class RichText extends AbstractShape implements ComparableInterface
132132
*/
133133
private $topInset = 4.8;
134134

135+
/**
136+
* Horizontal Auto Shrink
137+
* @var boolean
138+
*/
139+
private $autoShrinkHorizontal;
140+
141+
/**
142+
* Vertical Auto Shrink
143+
* @var boolean
144+
*/
145+
private $autoShrinkVertical;
146+
135147
/**
136148
* Create a new \PhpOffice\PhpPowerpoint\Shape\RichText instance
137149
*/
@@ -587,6 +599,44 @@ public function setInsetTop($value = 4.8)
587599
return $this;
588600
}
589601

602+
/**
603+
* Set horizontal auto shrink
604+
* @param bool $value
605+
*/
606+
public function setAutoShrinkHorizontal($value = null){
607+
if(is_bool($value)){
608+
$this->autoShrinkHorizontal = $value;
609+
}
610+
return $this;
611+
}
612+
613+
/**
614+
* Get horizontal auto shrink
615+
* @return bool
616+
*/
617+
public function getAutoShrinkHorizontal(){
618+
return $this->autoShrinkHorizontal;
619+
}
620+
621+
/**
622+
* Set vertical auto shrink
623+
* @param bool $value
624+
*/
625+
public function setAutoShrinkVertical($value = null){
626+
if(is_bool($value)){
627+
$this->autoShrinkVertical = $value;
628+
}
629+
return $this;
630+
}
631+
632+
/**
633+
* Set vertical auto shrink
634+
* @return bool
635+
*/
636+
public function getAutoShrinkVertical(){
637+
return $this->autoShrinkVertical;
638+
}
639+
590640
/**
591641
* Get hash code
592642
*

src/PhpPowerpoint/Writer/ODPresentation/Content.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,30 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
119119
$objWriter->writeAttribute('style:parent-style-name', 'standard');
120120
// style:graphic-properties
121121
$objWriter->startElement('style:graphic-properties');
122-
$objWriter->writeAttribute('draw:auto-grow-height', 'true');
122+
if(is_bool($shape->getAutoShrinkVertical())){
123+
$objWriter->writeAttribute('draw:auto-grow-height', var_export($shape->getAutoShrinkVertical(), true));
124+
}
125+
if(is_bool($shape->getAutoShrinkHorizontal())){
126+
$objWriter->writeAttribute('draw:auto-grow-width', var_export($shape->getAutoShrinkHorizontal(), true));
127+
}
128+
switch ($shape->getFill()->getFillType()){
129+
case Fill::FILL_NONE:
130+
default:
131+
$objWriter->writeAttribute('draw:fill', 'none');
132+
$objWriter->writeAttribute('draw:fill-color', '#'.$shape->getFill()->getStartColor()->getRGB());
133+
break;
134+
}
135+
switch ($shape->getBorder()->getLineStyle()){
136+
case Border::LINE_NONE:
137+
default:
138+
$objWriter->writeAttribute('draw:stroke', 'none');
139+
$objWriter->writeAttribute('svg:stroke-color', '#'.$shape->getBorder()->getColor()->getRGB());
140+
}
141+
123142
$objWriter->writeAttribute('fo:wrap-option', 'wrap');
124-
143+
// > style:graphic-properties
125144
$objWriter->endElement();
145+
// > style:style
126146
$objWriter->endElement();
127147

128148
$paragraphs = $shape->getParagraphs();
@@ -517,16 +537,18 @@ public function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $sha
517537
*/
518538
public function writeShapeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
519539
{
520-
// draw:custom-shape
521-
$objWriter->startElement('draw:custom-shape');
540+
// draw:frame
541+
$objWriter->startElement('draw:frame');
522542
$objWriter->writeAttribute('draw:style-name', 'gr' . $shapeId);
523543
$objWriter->writeAttribute('svg:width', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm');
524544
$objWriter->writeAttribute('svg:height', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm');
525545
$objWriter->writeAttribute('svg:x', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm');
526546
$objWriter->writeAttribute('svg:y', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm');
527-
528-
$paragraphs = $shape->getParagraphs();
529-
$paragraphId = 0;
547+
// draw:text-box
548+
$objWriter->startElement('draw:text-box');
549+
550+
$paragraphs = $shape->getParagraphs();
551+
$paragraphId = 0;
530552
$sCstShpLastBullet = '';
531553
$iCstShpLastBulletLvl = 0;
532554
$bCstShpHasBullet = false;
@@ -662,6 +684,10 @@ public function writeShapeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
662684
$objWriter->endElement();
663685
}
664686
}
687+
688+
// > draw:text-box
689+
$objWriter->endElement();
690+
// > draw:frame
665691
$objWriter->endElement();
666692
}
667693

tests/PhpPowerpoint/Tests/Shape/RichTextTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,39 @@ public function testGetSetAutoFit()
146146
$this->assertEquals(RichText::AUTOFIT_NORMAL, $object->getAutoFit());
147147
}
148148

149+
public function testGetSetHAutoShrink()
150+
{
151+
$object = new RichText();
152+
153+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal());
154+
$this->assertNull($object->getAutoShrinkHorizontal());
155+
156+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal(2));
157+
$this->assertNull($object->getAutoShrinkHorizontal());
158+
159+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal(true));
160+
$this->assertTrue($object->getAutoShrinkHorizontal());
161+
162+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal(false));
163+
$this->assertFalse($object->getAutoShrinkHorizontal());
164+
}
165+
public function testGetSetVAutoShrink()
166+
{
167+
$object = new RichText();
168+
169+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical());
170+
$this->assertNull($object->getAutoShrinkVertical());
171+
172+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical(2));
173+
$this->assertNull($object->getAutoShrinkVertical());
174+
175+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical(true));
176+
$this->assertTrue($object->getAutoShrinkVertical());
177+
178+
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical(false));
179+
$this->assertFalse($object->getAutoShrinkVertical());
180+
}
181+
149182
public function testGetSetHOverflow()
150183
{
151184
$object = new RichText();

tests/PhpPowerpoint/Tests/Writer/ODPresentation/ContentTest.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public function testList()
5555
$element = '/office:document-content/office:automatic-styles/text:list-style/text:list-level-style-bullet';
5656
$this->assertTrue($pres->elementExists($element, 'content.xml'));
5757

58-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape';
58+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box';
5959
$this->assertTrue($pres->elementExists($element, 'content.xml'));
6060

61-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:list/text:list-item/text:p/text:span';
61+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box/text:list/text:list-item/text:p/text:span';
6262
$this->assertTrue($pres->elementExists($element, 'content.xml'));
6363
}
6464

@@ -89,10 +89,10 @@ public function testInnerList()
8989
$element = '/office:document-content/office:automatic-styles/text:list-style/text:list-level-style-bullet';
9090
$this->assertTrue($pres->elementExists($element, 'content.xml'));
9191

92-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape';
92+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box';
9393
$this->assertTrue($pres->elementExists($element, 'content.xml'));
9494

95-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:list/text:list-item/text:list/text:list-item/text:p/text:span';
95+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box/text:list/text:list-item/text:list/text:list-item/text:p/text:span';
9696
$this->assertTrue($pres->elementExists($element, 'content.xml'));
9797
}
9898

@@ -110,10 +110,10 @@ public function testParagraphRichText()
110110

111111
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
112112

113-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:p/text:span/text:line-break';
113+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box/text:p/text:span/text:line-break';
114114
$this->assertTrue($pres->elementExists($element, 'content.xml'));
115115

116-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:p/text:span/text:a';
116+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box/text:p/text:span/text:a';
117117
$this->assertTrue($pres->elementExists($element, 'content.xml'));
118118
$this->assertEquals('http://www.google.fr', $pres->getElementAttribute($element, 'xlink:href', 'content.xml'));
119119
}
@@ -131,12 +131,43 @@ public function testListWithRichText()
131131

132132
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
133133

134-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:list/text:list-item/text:p/text:span/text:a';
134+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box/text:list/text:list-item/text:p/text:span/text:a';
135135
$this->assertTrue($pres->elementExists($element, 'content.xml'));
136-
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:list/text:list-item/text:p/text:span/text:line-break';
136+
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:text-box/text:list/text:list-item/text:p/text:span/text:line-break';
137137
$this->assertTrue($pres->elementExists($element, 'content.xml'));
138138
}
139139

140+
public function testRichtextAutoShrink()
141+
{
142+
$phpPowerPoint = new PhpPowerpoint();
143+
$oSlide = $phpPowerPoint->getActiveSlide();
144+
$oRichText1 = $oSlide->createRichTextShape();
145+
146+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
147+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
148+
$this->assertFalse($pres->attributeElementExists($element, 'draw:auto-grow-height', 'content.xml'));
149+
$this->assertFalse($pres->attributeElementExists($element, 'draw:auto-grow-width', 'content.xml'));
150+
151+
$oRichText1->setAutoShrinkHorizontal(false);
152+
$oRichText1->setAutoShrinkVertical(true);
153+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
154+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
155+
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-height', 'content.xml'));
156+
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-width', 'content.xml'));
157+
$this->assertEquals('true', $pres->getElementAttribute($element, 'draw:auto-grow-height', 'content.xml'));
158+
$this->assertEquals('false', $pres->getElementAttribute($element, 'draw:auto-grow-width', 'content.xml'));
159+
160+
161+
$oRichText1->setAutoShrinkHorizontal(true);
162+
$oRichText1->setAutoShrinkVertical(false);
163+
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
164+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
165+
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-height', 'content.xml'));
166+
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-width', 'content.xml'));
167+
$this->assertEquals('false', $pres->getElementAttribute($element, 'draw:auto-grow-height', 'content.xml'));
168+
$this->assertEquals('true', $pres->getElementAttribute($element, 'draw:auto-grow-width', 'content.xml'));
169+
}
170+
140171
public function testStyleAlignment()
141172
{
142173
$phpPowerPoint = new PhpPowerpoint();

tests/PhpPowerpoint/Tests/_includes/XmlDocument.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ public function getElementAttribute($path, $attribute, $file = 'word/document.xm
147147
return $this->getElement($path, $file)->getAttribute($attribute);
148148
}
149149

150+
/**
151+
* Get element attribute
152+
*
153+
* @param string $path
154+
* @param string $attribute
155+
* @param string $file
156+
* @return string
157+
*/
158+
public function attributeElementExists($path, $attribute, $file = 'word/document.xml')
159+
{
160+
return $this->getElement($path, $file)->hasAttribute($attribute);
161+
}
162+
150163
/**
151164
* Check if element exists
152165
*

0 commit comments

Comments
 (0)