Add support for table indent
This commit is contained in:
parent
67b18c32e7
commit
081c6722f6
|
|
@ -20,6 +20,7 @@ v0.15.0 (?? ??? 2018)
|
|||
- Added support for Image text wrapping distance @troosan #1310
|
||||
- Added parsing of CSS line-height and text-indent in HTML reader @troosan #1316
|
||||
- Added the ability to enable gridlines and axislabels on charts @FrankMeyer #576
|
||||
- Add support for table indent (tblInd) @Trainmaster
|
||||
|
||||
### Fixed
|
||||
- Fix reading of docx default style - @troosan #1238
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ Available Table style options:
|
|||
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
|
||||
- ``border(Top|Right|Bottom|Left)Size``. Border size in *twip*.
|
||||
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in *twip*.
|
||||
- ``indent``. Table indent from leading margin. Must be an instance of ``\PhpOffice\PhpWord\ComplexType\TblWidth``.
|
||||
- ``width``. Table width in percent.
|
||||
- ``unit``. The unit to use for the width. One of ``\PhpOffice\PhpWord\SimpleType\TblWidth``. Defaults to *auto*.
|
||||
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
<?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.
|
||||
*
|
||||
* @see https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2010-2018 PHPWord contributors
|
||||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\ComplexType;
|
||||
|
||||
use PhpOffice\PhpWord\SimpleType\TblWidth as TblWidthSimpleType;
|
||||
|
||||
/**
|
||||
* @see http://www.datypic.com/sc/ooxml/t-w_CT_TblWidth.html
|
||||
*/
|
||||
final class TblWidth
|
||||
{
|
||||
/** @var string */
|
||||
private $type;
|
||||
|
||||
/** @var int */
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param int $value If omitted, then its value shall be assumed to be 0.
|
||||
* @param string $type If omitted, then its value shall be assumed to be dxa.
|
||||
*/
|
||||
public function __construct($value = 0, $type = TblWidthSimpleType::TWIP)
|
||||
{
|
||||
$this->value = $value;
|
||||
TblWidthSimpleType::validate($type);
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@
|
|||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||
|
||||
use PhpOffice\Common\XMLReader;
|
||||
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
|
||||
use PhpOffice\PhpWord\Element\AbstractContainer;
|
||||
use PhpOffice\PhpWord\Element\TextRun;
|
||||
use PhpOffice\PhpWord\Element\TrackChange;
|
||||
|
|
@ -472,6 +473,11 @@ abstract class AbstractPart
|
|||
if ($tablePositionNode !== null) {
|
||||
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
|
||||
}
|
||||
|
||||
$indentNode = $xmlReader->getElement('w:tblInd', $styleNode);
|
||||
if ($indentNode !== null) {
|
||||
$style['indent'] = $this->readTableIndent($xmlReader, $indentNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -503,6 +509,24 @@ abstract class AbstractPart
|
|||
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:tblInd
|
||||
*
|
||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
||||
* @param \DOMElement $domNode
|
||||
* @return TblWidthComplexType
|
||||
*/
|
||||
private function readTableIndent(XMLReader $xmlReader, \DOMElement $domNode)
|
||||
{
|
||||
$styleDefs = array(
|
||||
'value' => array(self::READ_VALUE, '.', 'w:w'),
|
||||
'type' => array(self::READ_VALUE, '.', 'w:type'),
|
||||
);
|
||||
$styleDefs = $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
|
||||
|
||||
return new TblWidthComplexType((int) $styleDefs['value'], $styleDefs['type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:tcPr
|
||||
*
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Style;
|
||||
|
||||
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
|
||||
use PhpOffice\PhpWord\SimpleType\Jc;
|
||||
use PhpOffice\PhpWord\SimpleType\JcTable;
|
||||
use PhpOffice\PhpWord\SimpleType\TblWidth;
|
||||
|
|
@ -159,6 +160,9 @@ class Table extends Border
|
|||
*/
|
||||
private $position;
|
||||
|
||||
/** @var TblWidthComplexType|null */
|
||||
private $indent;
|
||||
|
||||
/**
|
||||
* Create new table style
|
||||
*
|
||||
|
|
@ -724,4 +728,24 @@ class Table extends Border
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TblWidthComplexType
|
||||
*/
|
||||
public function getIndent()
|
||||
{
|
||||
return $this->indent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TblWidthComplexType $indent
|
||||
* @return self
|
||||
* @see http://www.datypic.com/sc/ooxml/e-w_tblInd-1.html
|
||||
*/
|
||||
public function setIndent(TblWidthComplexType $indent)
|
||||
{
|
||||
$this->indent = $indent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ class Table extends AbstractStyle
|
|||
|
||||
$this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth());
|
||||
$this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing());
|
||||
$this->writeIndent($xmlWriter, $style);
|
||||
$this->writeLayout($xmlWriter, $style->getLayout());
|
||||
|
||||
// Position
|
||||
|
|
@ -216,4 +217,19 @@ class Table extends AbstractStyle
|
|||
{
|
||||
$this->width = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param XMLWriter $xmlWriter
|
||||
* @param TableStyle $style
|
||||
*/
|
||||
private function writeIndent(XMLWriter $xmlWriter, TableStyle $style)
|
||||
{
|
||||
$indent = $style->getIndent();
|
||||
|
||||
if ($indent === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->writeTblWidth($xmlWriter, 'w:tblInd', $indent->getType(), $indent->getValue());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,4 +126,23 @@ class StyleTest extends AbstractTestReader
|
|||
$fontStyle = $textRun->getElement(0)->getFontStyle();
|
||||
$this->assertEquals(15, $fontStyle->getPosition());
|
||||
}
|
||||
|
||||
public function testReadIndent()
|
||||
{
|
||||
$documentXml = '<w:tbl>
|
||||
<w:tblPr>
|
||||
<w:tblInd w:w="2160" w:type="dxa"/>
|
||||
</w:tblPr>
|
||||
</w:tbl>';
|
||||
|
||||
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
|
||||
|
||||
$elements = $phpWord->getSection(0)->getElements();
|
||||
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
|
||||
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
|
||||
/** @var \PhpOffice\PhpWord\Style\Table $tableStyle */
|
||||
$tableStyle = $elements[0]->getStyle();
|
||||
$this->assertSame(TblWidth::TWIP, $tableStyle->getIndent()->getType());
|
||||
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Style;
|
||||
|
||||
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
|
||||
use PhpOffice\PhpWord\SimpleType\JcTable;
|
||||
use PhpOffice\PhpWord\SimpleType\TblWidth;
|
||||
|
||||
|
|
@ -57,6 +58,7 @@ class TableTest extends \PHPUnit\Framework\TestCase
|
|||
$this->assertNull($object->getBgColor());
|
||||
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
|
||||
$this->assertEquals(TblWidth::AUTO, $object->getUnit());
|
||||
$this->assertNull($object->getIndent());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -208,4 +210,13 @@ class TableTest extends \PHPUnit\Framework\TestCase
|
|||
$this->assertNotNull($object->getPosition());
|
||||
$this->assertEquals(TablePosition::VANCHOR_PAGE, $object->getPosition()->getVertAnchor());
|
||||
}
|
||||
|
||||
public function testIndent()
|
||||
{
|
||||
$indent = new TblWidthComplexType(100, TblWidth::TWIP);
|
||||
|
||||
$table = new Table(array('indent' => $indent));
|
||||
|
||||
$this->assertSame($indent, $table->getIndent());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
||||
|
||||
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
|
||||
use PhpOffice\PhpWord\SimpleType\TblWidth;
|
||||
use PhpOffice\PhpWord\Style\Table;
|
||||
use PhpOffice\PhpWord\Style\TablePosition;
|
||||
use PhpOffice\PhpWord\TestHelperDOCX;
|
||||
|
|
@ -75,7 +77,7 @@ class TableTest extends \PHPUnit\Framework\TestCase
|
|||
$path = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing';
|
||||
$this->assertTrue($doc->elementExists($path));
|
||||
$this->assertEquals(10.3, $doc->getElementAttribute($path, 'w:w'));
|
||||
$this->assertEquals(\PhpOffice\PhpWord\SimpleType\TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
|
||||
$this->assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,4 +120,25 @@ class TableTest extends \PHPUnit\Framework\TestCase
|
|||
$this->assertEquals(TablePosition::YALIGN_TOP, $doc->getElementAttribute($path, 'w:tblpYSpec'));
|
||||
$this->assertEquals(60, $doc->getElementAttribute($path, 'w:tblpY'));
|
||||
}
|
||||
|
||||
public function testIndent()
|
||||
{
|
||||
$value = 100;
|
||||
$type = TblWidth::TWIP;
|
||||
|
||||
$tableStyle = new Table();
|
||||
$tableStyle->setIndent(new TblWidthComplexType($value, $type));
|
||||
|
||||
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||
$section = $phpWord->addSection();
|
||||
$table = $section->addTable($tableStyle);
|
||||
$table->addRow();
|
||||
|
||||
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
|
||||
|
||||
$path = '/w:document/w:body/w:tbl/w:tblPr/w:tblInd';
|
||||
$this->assertTrue($doc->elementExists($path));
|
||||
$this->assertSame($value, (int) $doc->getElementAttribute($path, 'w:w'));
|
||||
$this->assertSame($type, $doc->getElementAttribute($path, 'w:type'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue