Skip to content

Commit

Permalink
ODPresentation Writer : Support for fill in RichText - @Progi1984 GH-79
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jun 9, 2015
1 parent d452897 commit ae19e01
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added support for hyperlink on richtext (PowerPoint2007) - @JewrassicPark GH-49
- Added support for notes slide (ODPresentation & PowerPoint2007) - @Progi1984 @JewrassicPark GH-63
- Added option for explosion in Pie3D Chart (ODPresentation & PowerPoint2007) - @Progi1984 GH-76
- ODPresentation Writer : Support for fill in RichText - @Progi1984 GH-79

### Bugfix
- PSR-0 via composer broken - @Progi1984 GH-51
Expand Down
9 changes: 9 additions & 0 deletions src/PhpPowerpoint/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,15 @@ public function writeTxtStyle(XMLWriter $objWriter, RichText $shape)
$objWriter->writeAttribute('draw:auto-grow-width', var_export($shape->hasAutoShrinkHorizontal(), true));
}
switch ($shape->getFill()->getFillType()){
case Fill::FILL_GRADIENT_LINEAR:
case Fill::FILL_GRADIENT_PATH:
$objWriter->writeAttribute('draw:fill', 'gradient');
$objWriter->writeAttribute('draw:fill-gradient-name', 'gradient_'.$shape->getFill()->getHashCode());
break;
case Fill::FILL_SOLID:
$objWriter->writeAttribute('draw:fill', 'solid');
$objWriter->writeAttribute('draw:fill-color', '#'.$shape->getFill()->getStartColor()->getRGB());
break;
case Fill::FILL_NONE:
default:
$objWriter->writeAttribute('draw:fill', 'none');
Expand Down
52 changes: 40 additions & 12 deletions src/PhpPowerpoint/Writer/ODPresentation/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PhpOffice\PhpPowerpoint\Shared\String;
use PhpOffice\PhpPowerpoint\Shared\XMLWriter;
use PhpOffice\PhpPowerpoint\Style\Fill;
use PhpOffice\PhpPowerpoint\Shape\RichText;

/**
* \PhpOffice\PhpPowerpoint\Writer\ODPresentation\Styles
Expand Down Expand Up @@ -111,6 +112,8 @@ public function writePart(PHPPowerPoint $pPHPPowerPoint)
$this->writeTableStyle($objWriter, $shape);
} elseif ($shape instanceof Group) {
$this->writeGroupStyle($objWriter, $shape);
} elseif ($shape instanceof RichText) {
$this->writeRichTextStyle($objWriter, $shape);
}
}
}
Expand Down Expand Up @@ -164,6 +167,21 @@ public function writePart(PHPPowerPoint $pPHPPowerPoint)
return $objWriter->getData();
}

/**
* Write the default style information for a RichText shape
*
* @param XMLWriter $objWriter
* @param RichText $shape
*/
public function writeRichTextStyle(XMLWriter $objWriter, RichText $shape)
{
if ($shape->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR || $shape->getFill()->getFillType() == Fill::FILL_GRADIENT_PATH) {
if (!in_array($shape->getFill()->getHashCode(), $this->arrayGradient)) {
$this->writeGradientFill($objWriter, $shape->getFill());
}
}
}

/**
* Write the default style information for a Table shape
*
Expand All @@ -176,18 +194,7 @@ public function writeTableStyle(XMLWriter $objWriter, Table $shape)
foreach ($row->getCells() as $cell) {
if ($cell->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR) {
if (!in_array($cell->getFill()->getHashCode(), $this->arrayGradient)) {
$objWriter->startElement('draw:gradient');
$objWriter->writeAttribute('draw:name', 'gradient_'.$cell->getFill()->getHashCode());
$objWriter->writeAttribute('draw:display-name', 'gradient_'.$cell->getFill()->getHashCode());
$objWriter->writeAttribute('draw:style', 'linear');
$objWriter->writeAttribute('draw:start-intensity', '100%');
$objWriter->writeAttribute('draw:end-intensity', '100%');
$objWriter->writeAttribute('draw:start-color', '#'.$cell->getFill()->getStartColor()->getRGB());
$objWriter->writeAttribute('draw:end-color', '#'.$cell->getFill()->getEndColor()->getRGB());
$objWriter->writeAttribute('draw:border', '0%');
$objWriter->writeAttribute('draw:angle', $cell->getFill()->getRotation() - 90);
$objWriter->endElement();
$this->arrayGradient[] = $cell->getFill()->getHashCode();
$this->writeGradientFill($objWriter, $cell->getFill());
}
}
}
Expand All @@ -211,4 +218,25 @@ public function writeGroupStyle(XMLWriter $objWriter, Group $group)
}
}
}

/**
* Write the gradient style
* @param XMLWriter $objWriter
* @param Fill $oFill
*/
protected function writeGradientFill(XMLWriter $objWriter, Fill $oFill)
{
$objWriter->startElement('draw:gradient');
$objWriter->writeAttribute('draw:name', 'gradient_'.$oFill->getHashCode());
$objWriter->writeAttribute('draw:display-name', 'gradient_'.$oFill->getHashCode());
$objWriter->writeAttribute('draw:style', 'linear');
$objWriter->writeAttribute('draw:start-intensity', '100%');
$objWriter->writeAttribute('draw:end-intensity', '100%');
$objWriter->writeAttribute('draw:start-color', '#'.$oFill->getStartColor()->getRGB());
$objWriter->writeAttribute('draw:end-color', '#'.$oFill->getEndColor()->getRGB());
$objWriter->writeAttribute('draw:border', '0%');
$objWriter->writeAttribute('draw:angle', $oFill->getRotation() - 90);
$objWriter->endElement();
$this->arrayGradient[] = $oFill->getHashCode();
}
}
60 changes: 46 additions & 14 deletions tests/PhpPowerpoint/Tests/Writer/ODPresentation/StylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ public function tearDown()
TestHelperDOCX::clear();
}

public function testGradient()
{
$phpPowerPoint = new PhpPowerpoint();
$oSlide = $phpPowerPoint->getActiveSlide();
$oShape = $oSlide->createTableShape();
$oRow = $oShape->createRow();
$oCell = $oRow->getCell();
$oCell->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR)->setStartColor(new Color('FFFF7700'))->setEndColor(new Color('FFFFFFFF'));

$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = "/office:document-styles/office:styles/draw:gradient";
$this->assertEquals('gradient_'.$oCell->getFill()->getHashCode(), $pres->getElementAttribute($element, 'draw:name', 'styles.xml'));
}

public function testDocumentLayout()
{
$element = "/office:document-styles/office:automatic-styles/style:page-layout/style:page-layout-properties";
Expand Down Expand Up @@ -89,4 +75,50 @@ public function testCustomDocumentLayout()
$this->assertTrue($pres->elementExists($element, 'styles.xml'));
$this->assertEquals('sPL0', $pres->getElementAttribute($element, 'style:page-layout-name', 'styles.xml'));
}

public function testFillGradientLinearRichText()
{
$phpPowerPoint = new PhpPowerpoint();
$oSlide = $phpPowerPoint->getActiveSlide();
$oShape = $oSlide->createRichTextShape();
$oShape->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR)->setStartColor(new Color('FFFF7700'))->setEndColor(new Color('FFFFFFFF'));

$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = '/office:document-styles/office:styles/draw:gradient';
$this->assertEquals('gradient_'.$oShape->getFill()->getHashCode(), $pres->getElementAttribute($element, 'draw:name', 'styles.xml'));

$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
$this->assertTrue($pres->elementExists($element, 'content.xml'));
$this->assertEquals('gradient', $pres->getElementAttribute($element, 'draw:fill', 'content.xml'));
$this->assertEquals('gradient_'.$oShape->getFill()->getHashCode(), $pres->getElementAttribute($element, 'draw:fill-gradient-name', 'content.xml'));
}

public function testFillSolidRichText()
{
$phpPowerPoint = new PhpPowerpoint();
$oSlide = $phpPowerPoint->getActiveSlide();
$oShape = $oSlide->createRichTextShape();
$oShape->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color('FF4672A8'))->setEndColor(new Color('FF4672A8'));

$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'gr1\']/style:graphic-properties';
$this->assertTrue($pres->elementExists($element, 'content.xml'));
$this->assertEquals('solid', $pres->getElementAttribute($element, 'draw:fill', 'content.xml'));
$this->assertEquals('#'.$oShape->getFill()->getStartColor()->getRGB(), $pres->getElementAttribute($element, 'draw:fill-color', 'content.xml'));
$this->assertEquals('#'.$oShape->getFill()->getEndColor()->getRGB(), $pres->getElementAttribute($element, 'draw:fill-color', 'content.xml'));
}

public function testGradientTable()
{
$phpPowerPoint = new PhpPowerpoint();
$oSlide = $phpPowerPoint->getActiveSlide();
$oShape = $oSlide->createTableShape();
$oRow = $oShape->createRow();
$oCell = $oRow->getCell();
$oCell->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR)->setStartColor(new Color('FFFF7700'))->setEndColor(new Color('FFFFFFFF'));

$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
$element = "/office:document-styles/office:styles/draw:gradient";
$this->assertEquals('gradient_'.$oCell->getFill()->getHashCode(), $pres->getElementAttribute($element, 'draw:name', 'styles.xml'));
}
}

0 comments on commit ae19e01

Please sign in to comment.