#245: Basic table support in RTF writer

This commit is contained in:
Ivan Lanin 2014-05-31 12:37:38 +07:00
parent e7540c079e
commit 2205377259
11 changed files with 177 additions and 10 deletions

View File

@ -34,6 +34,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Four
- Element: New `Line` element - @basjan GH-253
- Title: Ability to apply numbering in heading - @ivanlanin GH-193
- HTML Reader: Basic HTML reader - @ivanlanin GH-80 GH-254
- RTF Writer: Basic table writing - @ivanlanin GH-245
### Bugfixes

View File

@ -25,7 +25,7 @@ column shows the containers while the rows lists the elements.
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 8 | List | v | v | v | v | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 9 | Table | v | v | v | ? | - | - |
| 9 | Table | v | v | v | v | - | - |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 10 | Image | v | v | v | v | v | v |
+-------+-----------------+-----------+----------+----------+---------+------------+------------+

View File

@ -81,7 +81,7 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | List | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Table | ✓ | ✓ | | ✓ | ✓ |
| | Table | ✓ | ✓ | | ✓ | ✓ |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Image | ✓ | ✓ | ✓ | ✓ | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+

View File

@ -90,9 +90,9 @@ Below are the supported features for each file formats.
| | Link | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Preserve Text | ✓ | | | | |
| | Text Break | ✓ | ✓ | ✓ | ✓ | ✓ |
| | Page Break | ✓ | | ✓ | | |
| | Page Break | ✓ | | ✓ | | |
| | List | ✓ | | | | |
| | Table | ✓ | ✓ | | ✓ | ✓ |
| | Table | ✓ | ✓ | | ✓ | ✓ |
| | Image | ✓ | ✓ | ✓ | ✓ | |
| | Object | ✓ | | | | |
| | Watermark | ✓ | | | | |
@ -454,7 +454,7 @@ Below are the matrix of element availability in each container. The column shows
| 6 | Text Break | v | v | v | v | v | v |
| 7 | Page Break | v | - | - | - | - | - |
| 8 | List | v | v | v | v | - | - |
| 9 | Table | v | v | v | ? | - | - |
| 9 | Table | v | v | v | v | - | - |
| 10 | Image | v | v | v | v | v | v |
| 11 | Watermark | - | v | - | - | - | - |
| 12 | Object | v | v | v | v | v | v |

View File

@ -98,6 +98,7 @@ abstract class AbstractElement extends HTMLAbstractElement
}
$styleWriter = new ParagraphStyleWriter($this->paragraphStyle);
$styleWriter->setNestedLevel($this->element->getNestedLevel());
return $styleWriter->write();
}

View File

@ -0,0 +1,142 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\RTF\Element;
use PhpOffice\PhpWord\Element\Cell as CellElement;
use PhpOffice\PhpWord\Element\Row as RowElement;
use PhpOffice\PhpWord\Element\Table as TableElement;
/**
* Table element RTF writer
*
* @since 0.11.0
*/
class Table extends AbstractElement
{
/**
* Write element
*
* @return string
*/
public function write()
{
if (!$this->element instanceof TableElement) {
return '';
}
$element = $this->element;
// No nesting table for now
if ($element->getNestedLevel() >= 1) {
return '';
}
$content = '';
$rows = $element->getRows();
$rowCount = count($rows);
if ($rowCount > 0) {
$content .= '\pard' . PHP_EOL;
for ($i = 0; $i < $rowCount; $i++) {
$content .= '\trowd ';
$content .= $this->writeRowDef($rows[$i]);
$content .= PHP_EOL;
$content .= $this->writeRow($rows[$i]);
$content .= '\row' . PHP_EOL;
}
}
return $content;
}
/**
* Write column
*
* @return string
*/
private function writeRowDef(RowElement $row)
{
$content = '';
$rightMargin = 0;
foreach ($row->getCells() as $cell) {
$width = $cell->getWidth();
$vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
if ($width === null) {
$width = 720; // Arbitrary default width
}
$rightMargin += $width;
$content .= "{$vMerge}\cellx{$rightMargin} ";
}
return $content;
}
/**
* Write row
*
* @return string
*/
private function writeRow(RowElement $row)
{
$content = '';
// Write cells
foreach ($row->getCells() as $cell) {
$content .= $this->writeCell($cell);
}
return $content;
}
/**
* Write cell
*
* @return string
*/
private function writeCell(CellElement $cell)
{
$content = '\intbl' . PHP_EOL;
// Write content
$writer = new Container($this->parentWriter, $cell);
$content .= $writer->write();
$content .= '\cell' . PHP_EOL;
return $content;
}
/**
* Get vertical merge style
*
* @param string $value
* @return string
* @todo Move to style
*/
private function getVMerge($value)
{
$style = '';
if ($value == 'restart') {
$style = '\clvmgf';
} elseif ($value == 'continue') {
$style = '\clvmrg';
}
return $style;
}
}

View File

@ -35,6 +35,6 @@ class TextBreak extends AbstractElement
$parentWriter = $this->parentWriter;
$parentWriter->setLastParagraphStyle();
return '\par' . PHP_EOL;
return '\pard\par' . PHP_EOL;
}
}

View File

@ -26,6 +26,16 @@ use PhpOffice\PhpWord\Style\Alignment;
*/
class Paragraph extends AbstractStyle
{
/**
* Depth of table container nested level; Primarily used for RTF writer/reader
*
* 0 = Not in a table; 1 = in a table; 2 = in a table inside another table, etc.
*
* @var int
*/
private $nestedLevel = 0;
/**
* Write style
*
@ -49,7 +59,10 @@ class Paragraph extends AbstractStyle
$spaceAfter = $style->getSpaceAfter();
$spaceBefore = $style->getSpaceBefore();
$content = '\pard\nowidctlpar';
$content = '';
if ($this->nestedLevel == 0) {
$content .= '\pard\nowidctlpar ';
}
if (isset($alignments[$align])) {
$content .= $alignments[$align];
}
@ -58,4 +71,14 @@ class Paragraph extends AbstractStyle
return $content;
}
/**
* Set nested level
*
* @param int $value
*/
public function setNestedLevel($value)
{
$this->nestedLevel = $value;
}
}

View File

@ -79,7 +79,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
$section->addLink('http://test.com');
$section->addTitle('Test', 1);
$section->addPageBreak();
$section->addTable();
$section->addTable()->addRow()->addCell()->addText('Test');
$section->addListItem('Test');
$section->addImage($imageSrc);
$section->addObject($objectSrc);

View File

@ -28,7 +28,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
*/
public function testUnmatchedElements()
{
$elements = array('Container', 'Text', 'Title', 'Link');
$elements = array('Container', 'Text', 'Title', 'Link', 'Table');
foreach ($elements as $element) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $element;
$parentWriter = new RTF();

View File

@ -68,7 +68,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase
$section->addLink('http://test.com');
$section->addTitle('Test', 1);
$section->addPageBreak();
$section->addTable();
$section->addTable()->addRow()->addCell()->addText('Test');
$section->addListItem('Test');
$section->addImage($imageSrc);
$section->addObject($objectSrc);