/** * Write the default style information for a Table shape * * @param XMLWriter $objWriter * @param Table $shape */ public function writeTableStyle(XMLWriter $objWriter, Table $shape) { foreach ($shape->getRows() as $keyRow => $shapeRow) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'gr' . $this->shapeId . 'r' . $keyRow); $objWriter->writeAttribute('style:family', 'table-row'); // style:table-row-properties $objWriter->startElement('style:table-row-properties'); $objWriter->writeAttribute('style:row-height', String::numberFormat(CommonDrawing::pixelsToCentimeters(CommonDrawing::pointsToPixels($shapeRow->getHeight())), 3) . 'cm'); $objWriter->endElement(); $objWriter->endElement(); foreach ($shapeRow->getCells() as $keyCell => $shapeCell) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'gr' . $this->shapeId . 'r' . $keyRow . 'c' . $keyCell); $objWriter->writeAttribute('style:family', 'table-cell'); // style:graphic-properties $objWriter->startElement('style:graphic-properties'); if ($shapeCell->getFill()->getFillType() == Fill::FILL_SOLID) { $objWriter->writeAttribute('draw:fill', 'solid'); $objWriter->writeAttribute('draw:fill-color', '#' . $shapeCell->getFill()->getStartColor()->getRGB()); } if ($shapeCell->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR) { $objWriter->writeAttribute('draw:fill', 'gradient'); $objWriter->writeAttribute('draw:fill-gradient-name', 'gradient_' . $shapeCell->getFill()->getHashCode()); } $objWriter->endElement(); // <style:graphic-properties // style:paragraph-properties $objWriter->startElement('style:paragraph-properties'); if ($shapeCell->getBorders()->getBottom()->getHashCode() == $shapeCell->getBorders()->getTop()->getHashCode() && $shapeCell->getBorders()->getBottom()->getHashCode() == $shapeCell->getBorders()->getLeft()->getHashCode() && $shapeCell->getBorders()->getBottom()->getHashCode() == $shapeCell->getBorders()->getRight()->getHashCode()) { $lineStyle = 'none'; $lineWidth = String::numberFormat($shapeCell->getBorders()->getBottom()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getBottom()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getBottom()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); } else { $lineStyle = 'none'; $lineWidth = String::numberFormat($shapeCell->getBorders()->getBottom()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getBottom()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getBottom()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-bottom', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); // TOP $lineStyle = 'none'; $lineWidth = String::numberFormat($shapeCell->getBorders()->getTop()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getTop()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getTop()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-top', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); // RIGHT $lineStyle = 'none'; $lineWidth = String::numberFormat($shapeCell->getBorders()->getRight()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getRight()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getRight()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-right', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); // LEFT $lineStyle = 'none'; $lineWidth = String::numberFormat($shapeCell->getBorders()->getLeft()->getLineWidth() / 1.75, 2); $lineColor = $shapeCell->getBorders()->getLeft()->getColor()->getRGB(); switch ($shapeCell->getBorders()->getLeft()->getLineStyle()) { case Border::LINE_SINGLE: $lineStyle = 'solid'; } $objWriter->writeAttribute('fo:border-left', $lineWidth . 'pt ' . $lineStyle . ' #' . $lineColor); } $objWriter->endElement(); $objWriter->endElement(); foreach ($shapeCell->getParagraphs() as $shapeParagraph) { foreach ($shapeParagraph->getRichTextElements() as $shapeRichText) { if ($shapeRichText instanceof Run) { // Style des font text if (!isset($this->arrStyleTextFont[$shapeRichText->getFont()->getHashCode()])) { $this->arrStyleTextFont[$shapeRichText->getFont()->getHashCode()] = $shapeRichText->getFont(); } } } } } } }
/** * Write table * * @param \PhpOffice\PhpPowerpoint\Shared\XMLWriter $objWriter XML Writer * @param \PhpOffice\PhpPowerpoint\Shape\Table $shape * @param int $shapeId * @throws \Exception */ private function writeShapeTable(XMLWriter $objWriter, Table $shape, $shapeId) { // p:graphicFrame $objWriter->startElement('p:graphicFrame'); // p:nvGraphicFramePr $objWriter->startElement('p:nvGraphicFramePr'); // p:cNvPr $objWriter->startElement('p:cNvPr'); $objWriter->writeAttribute('id', $shapeId); $objWriter->writeAttribute('name', $shape->getName()); $objWriter->writeAttribute('descr', $shape->getDescription()); $objWriter->endElement(); // p:cNvGraphicFramePr $objWriter->startElement('p:cNvGraphicFramePr'); // a:graphicFrameLocks $objWriter->startElement('a:graphicFrameLocks'); $objWriter->writeAttribute('noGrp', '1'); $objWriter->endElement(); $objWriter->endElement(); // p:nvPr $objWriter->writeElement('p:nvPr', null); $objWriter->endElement(); // p:xfrm $objWriter->startElement('p:xfrm'); // a:off $objWriter->startElement('a:off'); $objWriter->writeAttribute('x', SharedDrawing::pixelsToEmu($shape->getOffsetX())); $objWriter->writeAttribute('y', SharedDrawing::pixelsToEmu($shape->getOffsetY())); $objWriter->endElement(); // a:ext $objWriter->startElement('a:ext'); $objWriter->writeAttribute('cx', SharedDrawing::pixelsToEmu($shape->getWidth())); $objWriter->writeAttribute('cy', SharedDrawing::pixelsToEmu($shape->getHeight())); $objWriter->endElement(); $objWriter->endElement(); // a:graphic $objWriter->startElement('a:graphic'); // a:graphicData $objWriter->startElement('a:graphicData'); $objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/table'); // a:tbl $objWriter->startElement('a:tbl'); // a:tblPr $objWriter->startElement('a:tblPr'); $objWriter->writeAttribute('firstRow', '1'); $objWriter->writeAttribute('bandRow', '1'); $objWriter->endElement(); // a:tblGrid $objWriter->startElement('a:tblGrid'); // Write cell widths for ($cell = 0; $cell < count($shape->getRow(0)->getCells()); $cell++) { // a:gridCol $objWriter->startElement('a:gridCol'); // Calculate column width $width = $shape->getRow(0)->getCell($cell)->getWidth(); if ($width == 0) { $colCount = count($shape->getRow(0)->getCells()); $totalWidth = $shape->getWidth(); $width = $totalWidth / $colCount; } $objWriter->writeAttribute('w', SharedDrawing::pixelsToEmu($width)); $objWriter->endElement(); } $objWriter->endElement(); // Colspan / rowspan containers $colSpan = array(); $rowSpan = array(); // Default border style $defaultBorder = new Border(); // Write rows for ($row = 0; $row < count($shape->getRows()); $row++) { // a:tr $objWriter->startElement('a:tr'); $objWriter->writeAttribute('h', SharedDrawing::pixelsToEmu($shape->getRow($row)->getHeight())); // Write cells for ($cell = 0; $cell < count($shape->getRow($row)->getCells()); $cell++) { // Current cell $currentCell = $shape->getRow($row)->getCell($cell); // Next cell right $nextCellRight = $shape->getRow($row)->getCell($cell + 1, true); // Next cell below $nextRowBelow = $shape->getRow($row + 1, true); $nextCellBelow = null; if ($nextRowBelow != null) { $nextCellBelow = $nextRowBelow->getCell($cell, true); } // a:tc $objWriter->startElement('a:tc'); // Colspan if ($currentCell->getColSpan() > 1) { $objWriter->writeAttribute('gridSpan', $currentCell->getColSpan()); $colSpan[$row] = $currentCell->getColSpan() - 1; } elseif (isset($colSpan[$row]) && $colSpan[$row] > 0) { $colSpan[$row]--; $objWriter->writeAttribute('hMerge', '1'); } // Rowspan if ($currentCell->getRowSpan() > 1) { $objWriter->writeAttribute('rowSpan', $currentCell->getRowSpan()); $rowSpan[$cell] = $currentCell->getRowSpan() - 1; } elseif (isset($rowSpan[$cell]) && $rowSpan[$cell] > 0) { $rowSpan[$cell]--; $objWriter->writeAttribute('vMerge', '1'); } // a:txBody $objWriter->startElement('a:txBody'); // a:bodyPr $objWriter->startElement('a:bodyPr'); $objWriter->writeAttribute('wrap', 'square'); $objWriter->writeAttribute('rtlCol', '0'); // a:spAutoFit $objWriter->writeElement('a:spAutoFit', null); $objWriter->endElement(); // a:lstStyle $objWriter->writeElement('a:lstStyle', null); // Write paragraphs $this->writeParagraphs($objWriter, $currentCell->getParagraphs()); $objWriter->endElement(); // a:tcPr $objWriter->startElement('a:tcPr'); // Alignment (horizontal) $firstParagraph = $currentCell->getParagraph(0); $horizontalAlign = $firstParagraph->getAlignment()->getVertical(); if ($horizontalAlign != Alignment::VERTICAL_BASE && $horizontalAlign != Alignment::VERTICAL_AUTO) { $objWriter->writeAttribute('anchor', $horizontalAlign); } // Determine borders $borderLeft = $currentCell->getBorders()->getLeft(); $borderRight = $currentCell->getBorders()->getRight(); $borderTop = $currentCell->getBorders()->getTop(); $borderBottom = $currentCell->getBorders()->getBottom(); $borderDiagonalDown = $currentCell->getBorders()->getDiagonalDown(); $borderDiagonalUp = $currentCell->getBorders()->getDiagonalUp(); // Fix PowerPoint implementation if (!is_null($nextCellRight) && $nextCellRight->getBorders()->getRight()->getHashCode() != $defaultBorder->getHashCode()) { $borderRight = $nextCellRight->getBorders()->getLeft(); } if (!is_null($nextCellBelow) && $nextCellBelow->getBorders()->getBottom()->getHashCode() != $defaultBorder->getHashCode()) { $borderBottom = $nextCellBelow->getBorders()->getTop(); } // Write borders $this->writeBorder($objWriter, $borderLeft, 'L'); $this->writeBorder($objWriter, $borderRight, 'R'); $this->writeBorder($objWriter, $borderTop, 'T'); $this->writeBorder($objWriter, $borderBottom, 'B'); $this->writeBorder($objWriter, $borderDiagonalDown, 'TlToBr'); $this->writeBorder($objWriter, $borderDiagonalUp, 'BlToTr'); // Fill $this->writeFill($objWriter, $currentCell->getFill()); $objWriter->endElement(); $objWriter->endElement(); } $objWriter->endElement(); } $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); }
/** * Write table Shape * @param XMLWriter $objWriter * @param Table $shape * @param integer $shapeId */ public function writeShapeTable(XMLWriter $objWriter, Table $shape, $shapeId) { // draw:frame $objWriter->startElement('draw:frame'); $objWriter->writeAttribute('svg:x', StringHelper::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm'); $objWriter->writeAttribute('svg:y', StringHelper::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm'); $objWriter->writeAttribute('svg:height', StringHelper::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm'); $objWriter->writeAttribute('svg:width', StringHelper::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm'); // table:table $objWriter->startElement('table:table'); foreach ($shape->getRows() as $keyRow => $shapeRow) { // table:table-row $objWriter->startElement('table:table-row'); $objWriter->writeAttribute('table:style-name', 'gr' . $shapeId . 'r' . $keyRow); //@todo getFill $numColspan = 0; foreach ($shapeRow->getCells() as $keyCell => $shapeCell) { if ($numColspan == 0) { // table:table-cell $objWriter->startElement('table:table-cell'); $objWriter->writeAttribute('table:style-name', 'gr' . $shapeId . 'r' . $keyRow . 'c' . $keyCell); if ($shapeCell->getColspan() > 1) { $objWriter->writeAttribute('table:number-columns-spanned', $shapeCell->getColspan()); $numColspan = $shapeCell->getColspan() - 1; } // text:p $objWriter->startElement('text:p'); // text:span foreach ($shapeCell->getParagraphs() as $shapeParagraph) { foreach ($shapeParagraph->getRichTextElements() as $shapeRichText) { $objWriter->startElement('text:span'); $objWriter->writeAttribute('text:style-name', 'T_' . $shapeRichText->getFont()->getHashCode()); $objWriter->text($shapeRichText->getText()); $objWriter->endElement(); } } // > text:p $objWriter->endElement(); // > table:table-cell $objWriter->endElement(); } else { // table:covered-table-cell $objWriter->writeElement('table:covered-table-cell'); $numColspan--; } } // > table:table-row $objWriter->endElement(); } // > table:table $objWriter->endElement(); // > draw:frame $objWriter->endElement(); }
/** * @expectedException \Exception * expectedExceptionMessage Row number out of bounds. */ public function testGetRowException() { $object = new Table(); $object->getRow(); }
/** * Write the default style information for a Table shape * * @param XMLWriter $objWriter * @param Table $shape */ public function writeTableStyle(XMLWriter $objWriter, Table $shape) { foreach ($shape->getRows() as $row) { foreach ($row->getCells() as $cell) { if ($cell->getFill()->getFillType() == Fill::FILL_GRADIENT_LINEAR) { if (!in_array($cell->getFill()->getHashCode(), $this->arrayGradient)) { $this->writeGradientFill($objWriter, $cell->getFill()); } } } } }