Skip to content

Commit

Permalink
#28 : ODPresentation Writer : Ability to set auto shrink text
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 4, 2014
1 parent 4adb5bd commit 8646927
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/shapes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Below are the properties that you can set for a rich text shape.
- ``leftInset`` in pixels
- ``rightInset`` in pixels
- ``topInset`` in pixels
- ``autoShrinkHorizontal`` (boolean)
- ``autoShrinkVertical`` (boolean)

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

Expand Down
50 changes: 50 additions & 0 deletions src/PhpPowerpoint/Shape/RichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ class RichText extends AbstractShape implements ComparableInterface
*/
private $topInset = 4.8;

/**
* Horizontal Auto Shrink
* @var boolean
*/
private $autoShrinkHorizontal;

/**
* Vertical Auto Shrink
* @var boolean
*/
private $autoShrinkVertical;

/**
* Create a new \PhpOffice\PhpPowerpoint\Shape\RichText instance
*/
Expand Down Expand Up @@ -587,6 +599,44 @@ public function setInsetTop($value = 4.8)
return $this;
}

/**
* Set horizontal auto shrink
* @param bool $value
*/
public function setAutoShrinkHorizontal($value = null){
if(is_bool($value)){
$this->autoShrinkHorizontal = $value;
}
return $this;
}

/**
* Get horizontal auto shrink
* @return bool
*/
public function getAutoShrinkHorizontal(){
return $this->autoShrinkHorizontal;
}

/**
* Set vertical auto shrink
* @param bool $value
*/
public function setAutoShrinkVertical($value = null){
if(is_bool($value)){
$this->autoShrinkVertical = $value;
}
return $this;
}

/**
* Set vertical auto shrink
* @return bool
*/
public function getAutoShrinkVertical(){
return $this->autoShrinkVertical;
}

/**
* Get hash code
*
Expand Down
40 changes: 33 additions & 7 deletions src/PhpPowerpoint/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,30 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
$objWriter->writeAttribute('style:parent-style-name', 'standard');
// style:graphic-properties
$objWriter->startElement('style:graphic-properties');
$objWriter->writeAttribute('draw:auto-grow-height', 'true');
if(is_bool($shape->getAutoShrinkVertical())){
$objWriter->writeAttribute('draw:auto-grow-height', var_export($shape->getAutoShrinkVertical(), true));
}
if(is_bool($shape->getAutoShrinkHorizontal())){
$objWriter->writeAttribute('draw:auto-grow-width', var_export($shape->getAutoShrinkHorizontal(), true));
}
switch ($shape->getFill()->getFillType()){
case Fill::FILL_NONE:
default:
$objWriter->writeAttribute('draw:fill', 'none');
$objWriter->writeAttribute('draw:fill-color', '#'.$shape->getFill()->getStartColor()->getRGB());
break;
}
switch ($shape->getBorder()->getLineStyle()){
case Border::LINE_NONE:
default:
$objWriter->writeAttribute('draw:stroke', 'none');
$objWriter->writeAttribute('svg:stroke-color', '#'.$shape->getBorder()->getColor()->getRGB());
}

$objWriter->writeAttribute('fo:wrap-option', 'wrap');

// > style:graphic-properties
$objWriter->endElement();
// > style:style
$objWriter->endElement();

$paragraphs = $shape->getParagraphs();
Expand Down Expand Up @@ -517,16 +537,18 @@ public function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $sha
*/
public function writeShapeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
{
// draw:custom-shape
$objWriter->startElement('draw:custom-shape');
// draw:frame
$objWriter->startElement('draw:frame');
$objWriter->writeAttribute('draw:style-name', 'gr' . $shapeId);
$objWriter->writeAttribute('svg:width', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm');
$objWriter->writeAttribute('svg:height', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm');
$objWriter->writeAttribute('svg:x', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm');
$objWriter->writeAttribute('svg:y', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm');

$paragraphs = $shape->getParagraphs();
$paragraphId = 0;
// draw:text-box
$objWriter->startElement('draw:text-box');

$paragraphs = $shape->getParagraphs();
$paragraphId = 0;
$sCstShpLastBullet = '';
$iCstShpLastBulletLvl = 0;
$bCstShpHasBullet = false;
Expand Down Expand Up @@ -662,6 +684,10 @@ public function writeShapeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
$objWriter->endElement();
}
}

// > draw:text-box
$objWriter->endElement();
// > draw:frame
$objWriter->endElement();
}

Expand Down
33 changes: 33 additions & 0 deletions tests/PhpPowerpoint/Tests/Shape/RichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ public function testGetSetAutoFit()
$this->assertEquals(RichText::AUTOFIT_NORMAL, $object->getAutoFit());
}

public function testGetSetHAutoShrink()
{
$object = new RichText();

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal());
$this->assertNull($object->getAutoShrinkHorizontal());

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal(2));
$this->assertNull($object->getAutoShrinkHorizontal());

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal(true));
$this->assertTrue($object->getAutoShrinkHorizontal());

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkHorizontal(false));
$this->assertFalse($object->getAutoShrinkHorizontal());
}
public function testGetSetVAutoShrink()
{
$object = new RichText();

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical());
$this->assertNull($object->getAutoShrinkVertical());

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical(2));
$this->assertNull($object->getAutoShrinkVertical());

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical(true));
$this->assertTrue($object->getAutoShrinkVertical());

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\RichText', $object->setAutoShrinkVertical(false));
$this->assertFalse($object->getAutoShrinkVertical());
}

public function testGetSetHOverflow()
{
$object = new RichText();
Expand Down
47 changes: 39 additions & 8 deletions tests/PhpPowerpoint/Tests/Writer/ODPresentation/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public function testList()
$element = '/office:document-content/office:automatic-styles/text:list-style/text:list-level-style-bullet';
$this->assertTrue($pres->elementExists($element, 'content.xml'));

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

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

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

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

$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';
$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';
$this->assertTrue($pres->elementExists($element, 'content.xml'));
}

Expand All @@ -110,10 +110,10 @@ public function testParagraphRichText()

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

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

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

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

$element = '/office:document-content/office:body/office:presentation/draw:page/draw:custom-shape/text:list/text:list-item/text:p/text:span/text:a';
$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';
$this->assertTrue($pres->elementExists($element, 'content.xml'));
$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';
$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';
$this->assertTrue($pres->elementExists($element, 'content.xml'));
}

public function testRichtextAutoShrink()
{
$phpPowerPoint = new PhpPowerpoint();
$oSlide = $phpPowerPoint->getActiveSlide();
$oRichText1 = $oSlide->createRichTextShape();

$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
$this->assertFalse($pres->attributeElementExists($element, 'draw:auto-grow-height', 'content.xml'));
$this->assertFalse($pres->attributeElementExists($element, 'draw:auto-grow-width', 'content.xml'));

$oRichText1->setAutoShrinkHorizontal(false);
$oRichText1->setAutoShrinkVertical(true);
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-height', 'content.xml'));
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-width', 'content.xml'));
$this->assertEquals('true', $pres->getElementAttribute($element, 'draw:auto-grow-height', 'content.xml'));
$this->assertEquals('false', $pres->getElementAttribute($element, 'draw:auto-grow-width', 'content.xml'));


$oRichText1->setAutoShrinkHorizontal(true);
$oRichText1->setAutoShrinkVertical(false);
$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-height', 'content.xml'));
$this->assertTrue($pres->attributeElementExists($element, 'draw:auto-grow-width', 'content.xml'));
$this->assertEquals('false', $pres->getElementAttribute($element, 'draw:auto-grow-height', 'content.xml'));
$this->assertEquals('true', $pres->getElementAttribute($element, 'draw:auto-grow-width', 'content.xml'));
}

public function testStyleAlignment()
{
$phpPowerPoint = new PhpPowerpoint();
Expand Down
13 changes: 13 additions & 0 deletions tests/PhpPowerpoint/Tests/_includes/XmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ public function getElementAttribute($path, $attribute, $file = 'word/document.xm
return $this->getElement($path, $file)->getAttribute($attribute);
}

/**
* Get element attribute
*
* @param string $path
* @param string $attribute
* @param string $file
* @return string
*/
public function attributeElementExists($path, $attribute, $file = 'word/document.xml')
{
return $this->getElement($path, $file)->hasAttribute($attribute);
}

/**
* Check if element exists
*
Expand Down

0 comments on commit 8646927

Please sign in to comment.