From 081c6722f62aa15a88af8fdff8dca3b58b136bb8 Mon Sep 17 00:00:00 2001 From: Frank Liepert Date: Wed, 11 Apr 2018 09:56:02 +0200 Subject: [PATCH 1/3] Add support for table indent --- CHANGELOG.md | 1 + docs/styles.rst | 1 + src/PhpWord/ComplexType/TblWidth.php | 59 +++++++++++++++++++ src/PhpWord/Reader/Word2007/AbstractPart.php | 24 ++++++++ src/PhpWord/Style/Table.php | 24 ++++++++ src/PhpWord/Writer/Word2007/Style/Table.php | 16 +++++ tests/PhpWord/Reader/Word2007/StyleTest.php | 19 ++++++ tests/PhpWord/Style/TableTest.php | 11 ++++ .../Writer/Word2007/Style/TableTest.php | 25 +++++++- 9 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 src/PhpWord/ComplexType/TblWidth.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d0b39ce1..e1d0937f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/styles.rst b/docs/styles.rst index 0bda3faf..88caeaeb 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -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. diff --git a/src/PhpWord/ComplexType/TblWidth.php b/src/PhpWord/ComplexType/TblWidth.php new file mode 100644 index 00000000..91dedc3d --- /dev/null +++ b/src/PhpWord/ComplexType/TblWidth.php @@ -0,0 +1,59 @@ +value = $value; + TblWidthSimpleType::validate($type); + $this->type = $type; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return int + */ + public function getValue() + { + return $this->value; + } +} diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index f64886cf..48a84ff2 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -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 * diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index feb028da..b622c78b 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -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; + } } diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index eb5af86d..7f49be7c 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -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()); + } } diff --git a/tests/PhpWord/Reader/Word2007/StyleTest.php b/tests/PhpWord/Reader/Word2007/StyleTest.php index 9bb6d3bd..d64079fa 100644 --- a/tests/PhpWord/Reader/Word2007/StyleTest.php +++ b/tests/PhpWord/Reader/Word2007/StyleTest.php @@ -126,4 +126,23 @@ class StyleTest extends AbstractTestReader $fontStyle = $textRun->getElement(0)->getFontStyle(); $this->assertEquals(15, $fontStyle->getPosition()); } + + public function testReadIndent() + { + $documentXml = ' + + + + '; + + $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()); + } } diff --git a/tests/PhpWord/Style/TableTest.php b/tests/PhpWord/Style/TableTest.php index 332d31aa..91fc3550 100644 --- a/tests/PhpWord/Style/TableTest.php +++ b/tests/PhpWord/Style/TableTest.php @@ -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()); + } } diff --git a/tests/PhpWord/Writer/Word2007/Style/TableTest.php b/tests/PhpWord/Writer/Word2007/Style/TableTest.php index 364a34d6..ec3b2483 100644 --- a/tests/PhpWord/Writer/Word2007/Style/TableTest.php +++ b/tests/PhpWord/Writer/Word2007/Style/TableTest.php @@ -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')); + } } From 85e1e5c280b89b45104ad7c270a9c07cada8d718 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 14 Apr 2018 23:21:02 +0200 Subject: [PATCH 2/3] fix warning --- src/PhpWord/ComplexType/TblWidth.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/ComplexType/TblWidth.php b/src/PhpWord/ComplexType/TblWidth.php index 91dedc3d..0d1a2419 100644 --- a/src/PhpWord/ComplexType/TblWidth.php +++ b/src/PhpWord/ComplexType/TblWidth.php @@ -31,8 +31,8 @@ final class TblWidth 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. + * @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) { From 5ec2c8560efbcc7c0daa888f9025f6e5164ffbd8 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 25 Apr 2018 22:33:48 +0200 Subject: [PATCH 3/3] do not push code coverage after build of develop branch [ci skip] --- .travis_shell_after_success.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis_shell_after_success.sh b/.travis_shell_after_success.sh index 12728526..06feddaa 100644 --- a/.travis_shell_after_success.sh +++ b/.travis_shell_after_success.sh @@ -5,7 +5,7 @@ echo "TRAVIS_REPO_SLUG: $TRAVIS_REPO_SLUG" echo "TRAVIS_PHP_VERSION: $TRAVIS_PHP_VERSION" echo "TRAVIS_PULL_REQUEST: $TRAVIS_PULL_REQUEST" -if [ "$TRAVIS_REPO_SLUG" == "PHPOffice/PHPWord" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then +if [ "$TRAVIS_REPO_SLUG" == "PHPOffice/PHPWord" ] && [ "$TRAVIS_BRANCH" != "develop" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then echo -e "Publishing PHPDoc...\n"