Skip to content

Commit

Permalink
PHPOffice#240 : PowerPoint2007 Reader : Support of Table
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Mar 5, 2017
1 parent 42f02f6 commit 42d8f44
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 264 deletions.
124 changes: 103 additions & 21 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use PhpOffice\PhpPresentation\Slide\SlideLayout;
use PhpOffice\PhpPresentation\Slide\SlideMaster;
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Borders;
use PhpOffice\PhpPresentation\Style\Fill;
use PhpOffice\PhpPresentation\Style\SchemeColor;
use PhpOffice\PhpPresentation\Style\TextStyle;
Expand Down Expand Up @@ -899,10 +901,12 @@ protected function loadParagraph(XMLReader $document, \DOMElement $oElement, $oS
$oText = $oParagraph->createTextRun();

if ($oElementrPr->hasAttribute('b')) {
$oText->getFont()->setBold($oElementrPr->getAttribute('b') == 'true' ? true : false);
$att = $oElementrPr->getAttribute('b');
$oText->getFont()->setBold($att == 'true' || $att == '1' ? true : false);
}
if ($oElementrPr->hasAttribute('i')) {
$oText->getFont()->setItalic($oElementrPr->getAttribute('i') == 'true' ? true : false);
$att = $oElementrPr->getAttribute('i');
$oText->getFont()->setItalic($att == 'true' || $att == '1' ? true : false);
}
if ($oElementrPr->hasAttribute('strike')) {
$oText->getFont()->setStrikethrough($oElementrPr->getAttribute('strike') == 'noStrike' ? false : true);
Expand Down Expand Up @@ -1009,6 +1013,7 @@ protected function loadShapeTable(XMLReader $document, \DOMElement $node, Abstra
continue;
}
$oCell = $oRow->getCell($keyCell);
$oCell->setParagraphs(array());
if ($oElementCell->hasAttribute('gridSpan')) {
$oCell->setColSpan($oElementCell->getAttribute('gridSpan'));
}
Expand All @@ -1022,10 +1027,56 @@ protected function loadShapeTable(XMLReader $document, \DOMElement $node, Abstra

$oElementTcPr = $document->getElement('a:tcPr', $oElementCell);
if ($oElementTcPr instanceof \DOMElement) {
$numParagraphs = count($oCell->getParagraphs());
if ($numParagraphs > 0) {
if ($oElementTcPr->hasAttribute('anchor')) {
$oCell->getParagraph(0)->getAlignment()->setVertical($oElementTcPr->getAttribute('anchor'));
}
if ($oElementTcPr->hasAttribute('marB')) {
$oCell->getParagraph(0)->getAlignment()->setMarginBottom($oElementTcPr->getAttribute('marB'));
}
if ($oElementTcPr->hasAttribute('marL')) {
$oCell->getParagraph(0)->getAlignment()->setMarginLeft($oElementTcPr->getAttribute('marL'));
}
if ($oElementTcPr->hasAttribute('marR')) {
$oCell->getParagraph(0)->getAlignment()->setMarginRight($oElementTcPr->getAttribute('marR'));
}
if ($oElementTcPr->hasAttribute('marT')) {
$oCell->getParagraph(0)->getAlignment()->setMarginTop($oElementTcPr->getAttribute('marT'));
}
}

$oFill = $this->loadStyleFill($document, $oElementTcPr, $oSlide);
if ($oFill instanceof Fill) {
$oCell->setFill($oFill);
}

$oBorders = new Borders();
$oElementBorderL = $document->getElement('a:lnL', $oElementTcPr);
if ($oElementBorderL instanceof \DOMElement) {
$this->loadBorder($document, $oElementBorderL, $oBorders->getLeft());
}
$oElementBorderR = $document->getElement('a:lnR', $oElementTcPr);
if ($oElementBorderR instanceof \DOMElement) {
$this->loadBorder($document, $oElementBorderR, $oBorders->getRight());
}
$oElementBorderT = $document->getElement('a:lnT', $oElementTcPr);
if ($oElementBorderT instanceof \DOMElement) {
$this->loadBorder($document, $oElementBorderT, $oBorders->getTop());
}
$oElementBorderB = $document->getElement('a:lnB', $oElementTcPr);
if ($oElementBorderB instanceof \DOMElement) {
$this->loadBorder($document, $oElementBorderB, $oBorders->getBottom());
}
$oElementBorderDiagDown = $document->getElement('a:lnTlToBr', $oElementTcPr);
if ($oElementBorderDiagDown instanceof \DOMElement) {
$this->loadBorder($document, $oElementBorderDiagDown, $oBorders->getDiagonalDown());
}
$oElementBorderDiagUp = $document->getElement('a:lnBlToTr', $oElementTcPr);
if ($oElementBorderDiagUp instanceof \DOMElement) {
$this->loadBorder($document, $oElementBorderDiagUp, $oBorders->getDiagonalUp());
}
$oCell->setBorders($oBorders);
}
}
}
Expand All @@ -1047,22 +1098,12 @@ protected function loadStyleFill(XMLReader $xmlReader, \DOMElement $oElement, Ab

$oElementColor = $xmlReader->getElement('a:gsLst/a:gs[@pos="0"]/a:srgbClr', $oElementFill);
if ($oElementColor instanceof \DOMElement && $oElementColor->hasAttribute('val')) {
$oFill->getStartColor()->setRGB($oElementColor->getAttribute('val'));
$oElementAlpha = $xmlReader->getElement('a:alpha', $oElementColor);
if ($oElementAlpha instanceof \DOMElement && $oElementAlpha->hasAttribute('val')) {
$alpha = strtoupper(dechex((($oElementAlpha->getAttribute('val') / 1000) / 100) * 255));
$oFill->getStartColor()->setRGB($oElementColor->getAttribute('val'), $alpha);
}
$oFill->setStartColor($this->loadStyleColor($xmlReader, $oElementColor));
}

$oElementColor = $xmlReader->getElement('a:gsLst/a:gs[@pos="100000"]/a:srgbClr', $oElementFill);
if ($oElementColor instanceof \DOMElement && $oElementColor->hasAttribute('val')) {
$oFill->getEndColor()->setRGB($oElementColor->getAttribute('val'));
$oElementAlpha = $xmlReader->getElement('a:alpha', $oElementColor);
if ($oElementAlpha instanceof \DOMElement && $oElementAlpha->hasAttribute('val')) {
$alpha = strtoupper(dechex((($oElementAlpha->getAttribute('val') / 1000) / 100) * 255));
$oFill->getEndColor()->setRGB($oElementColor->getAttribute('val'), $alpha);
}
$oFill->setEndColor($this->loadStyleColor($xmlReader, $oElementColor));
}

$oRotation = $xmlReader->getElement('a:lin', $oElementFill);
Expand All @@ -1079,19 +1120,60 @@ protected function loadStyleFill(XMLReader $xmlReader, \DOMElement $oElement, Ab
$oFill->setFillType(Fill::FILL_SOLID);

$oElementColor = $xmlReader->getElement('a:srgbClr', $oElementFill);
if ($oElementColor instanceof \DOMElement && $oElementColor->hasAttribute('val')) {
$oFill->getStartColor()->setRGB($oElementColor->getAttribute('val'));
$oElementAlpha = $xmlReader->getElement('a:alpha', $oElementColor);
if ($oElementAlpha instanceof \DOMElement && $oElementAlpha->hasAttribute('val')) {
$alpha = strtoupper(dechex((($oElementAlpha->getAttribute('val') / 1000) / 100) * 255));
$oFill->getStartColor()->setRGB($oElementColor->getAttribute('val'), $alpha);
}
if ($oElementColor instanceof \DOMElement) {
$oFill->setStartColor($this->loadStyleColor($xmlReader, $oElementColor));
}
return $oFill;
}
return null;
}

/**
* @param XMLReader $xmlReader
* @param \DOMElement $oElement
* @return Color
*/
protected function loadStyleColor(XMLReader $xmlReader, \DOMElement $oElement)
{
$oColor = new Color();
$oColor->setRGB($oElement->getAttribute('val'));
$oElementAlpha = $xmlReader->getElement('a:alpha', $oElement);
if ($oElementAlpha instanceof \DOMElement && $oElementAlpha->hasAttribute('val')) {
$alpha = strtoupper(dechex((($oElementAlpha->getAttribute('val') / 1000) / 100) * 255));
$oColor->setRGB($oElement->getAttribute('val'), $alpha);
}
return $oColor;
}

/**
* @param XMLReader $xmlReader
* @param \DOMElement $oElement
* @param Border $oBorder
*/
protected function loadBorder(XMLReader $xmlReader, \DOMElement $oElement, Border $oBorder)
{
if ($oElement->hasAttribute('w')) {
$oBorder->setLineWidth($oElement->getAttribute('w') / 12700);
}
if ($oElement->hasAttribute('cmpd')) {
$oBorder->setLineStyle($oElement->getAttribute('cmpd'));
}

$oElementNoFill = $xmlReader->getElement('a:noFill', $oElement);
if ($oElementNoFill instanceof \DOMElement && $oBorder->getLineStyle() == Border::LINE_SINGLE) {
$oBorder->setLineStyle(Border::LINE_NONE);
}

$oElementColor = $xmlReader->getElement('a:solidFill/a:srgbClr', $oElement);
if ($oElementColor instanceof \DOMElement) {
$oBorder->setColor($this->loadStyleColor($xmlReader, $oElementColor));
}

$oElementDashStyle = $xmlReader->getElement('a:prstDash', $oElement);
if ($oElementDashStyle instanceof \DOMElement && $oElementDashStyle->hasAttribute('val')) {
$oBorder->setDashStyle($oElementDashStyle->getAttribute('val'));
}
}
/**
*
* @param string $fileRels
Expand Down
21 changes: 13 additions & 8 deletions src/PhpPresentation/Shape/Table/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,21 @@ public function getParagraph($index = 0)
*/
public function createParagraph()
{
$alignment = clone $this->getActiveParagraph()->getAlignment();
$font = clone $this->getActiveParagraph()->getFont();
$bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();

$this->richTextParagraphs[] = new Paragraph();
$this->activeParagraph = count($this->richTextParagraphs) - 1;

$this->getActiveParagraph()->setAlignment($alignment);
$this->getActiveParagraph()->setFont($font);
$this->getActiveParagraph()->setBulletStyle($bulletStyle);
if (count($this->richTextParagraphs) > 1) {
$alignment = clone $this->getActiveParagraph()->getAlignment();
$font = clone $this->getActiveParagraph()->getFont();
$bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();

$this->activeParagraph = count($this->richTextParagraphs) - 1;

$this->getActiveParagraph()->setAlignment($alignment);
$this->getActiveParagraph()->setFont($font);
$this->getActiveParagraph()->setBulletStyle($bulletStyle);
} else {
$this->activeParagraph = count($this->richTextParagraphs) - 1;
}

return $this->getActiveParagraph();
}
Expand Down
16 changes: 15 additions & 1 deletion src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,13 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr/
$objWriter->endElement();
// p:graphicFrame/p:nvGraphicFramePr/p:nvPr
$objWriter->writeElement('p:nvPr', null);
$objWriter->startElement('p:nvPr');
if ($shape->isPlaceholder()) {
$objWriter->startElement('p:ph');
$objWriter->writeAttribute('type', $shape->getPlaceholder()->getType());
$objWriter->endElement();
}
$objWriter->endElement();
// p:graphicFrame/p:nvGraphicFramePr/
$objWriter->endElement();
// p:graphicFrame/p:xfrm
Expand Down Expand Up @@ -433,6 +439,14 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) {
$objWriter->writeAttribute('anchor', $verticalAlign);
}

// Margins
$alignment = $firstParagraph->getAlignment();
$objWriter->writeAttribute('marL', $alignment->getMarginLeft());
$objWriter->writeAttribute('marR', $alignment->getMarginRight());
$objWriter->writeAttribute('marT', $alignment->getMarginTop());
$objWriter->writeAttribute('marB', $alignment->getMarginBottom());

// Determine borders
$borderLeft = $currentCell->getBorders()->getLeft();
$borderRight = $currentCell->getBorders()->getRight();
Expand Down
Loading

0 comments on commit 42d8f44

Please sign in to comment.