Skip to content

Commit 4d9e9bc

Browse files
authored
Merge branch 'develop' into column_width_for_odt
2 parents b147919 + 84fa440 commit 4d9e9bc

File tree

10 files changed

+180
-2
lines changed

10 files changed

+180
-2
lines changed

.travis_shell_after_success.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ echo "TRAVIS_REPO_SLUG: $TRAVIS_REPO_SLUG"
55
echo "TRAVIS_PHP_VERSION: $TRAVIS_PHP_VERSION"
66
echo "TRAVIS_PULL_REQUEST: $TRAVIS_PULL_REQUEST"
77

8-
if [ "$TRAVIS_REPO_SLUG" == "PHPOffice/PHPWord" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then
8+
if [ "$TRAVIS_REPO_SLUG" == "PHPOffice/PHPWord" ] && [ "$TRAVIS_BRANCH" != "develop" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then
99

1010
echo -e "Publishing PHPDoc...\n"
1111

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ v0.15.0 (?? ??? 2018)
2020
- Added support for Image text wrapping distance @troosan #1310
2121
- Added parsing of CSS line-height and text-indent in HTML reader @troosan #1316
2222
- Added the ability to enable gridlines and axislabels on charts @FrankMeyer #576
23+
- Add support for table indent (tblInd) @Trainmaster #1343
2324
- Added parsing of internal links in HTML reader @lalop #1336
2425

2526
### Fixed

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Available Table style options:
104104
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
105105
- ``border(Top|Right|Bottom|Left)Size``. Border size in *twip*.
106106
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in *twip*.
107+
- ``indent``. Table indent from leading margin. Must be an instance of ``\PhpOffice\PhpWord\ComplexType\TblWidth``.
107108
- ``width``. Table width in percent.
108109
- ``unit``. The unit to use for the width. One of ``\PhpOffice\PhpWord\SimpleType\TblWidth``. Defaults to *auto*.
109110
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.

src/PhpWord/ComplexType/TblWidth.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\ComplexType;
19+
20+
use PhpOffice\PhpWord\SimpleType\TblWidth as TblWidthSimpleType;
21+
22+
/**
23+
* @see http://www.datypic.com/sc/ooxml/t-w_CT_TblWidth.html
24+
*/
25+
final class TblWidth
26+
{
27+
/** @var string */
28+
private $type;
29+
30+
/** @var int */
31+
private $value;
32+
33+
/**
34+
* @param int $value If omitted, then its value shall be assumed to be 0
35+
* @param string $type If omitted, then its value shall be assumed to be dxa
36+
*/
37+
public function __construct($value = 0, $type = TblWidthSimpleType::TWIP)
38+
{
39+
$this->value = $value;
40+
TblWidthSimpleType::validate($type);
41+
$this->type = $type;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getType()
48+
{
49+
return $this->type;
50+
}
51+
52+
/**
53+
* @return int
54+
*/
55+
public function getValue()
56+
{
57+
return $this->value;
58+
}
59+
}

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Reader\Word2007;
1919

2020
use PhpOffice\Common\XMLReader;
21+
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
2122
use PhpOffice\PhpWord\Element\AbstractContainer;
2223
use PhpOffice\PhpWord\Element\TextRun;
2324
use PhpOffice\PhpWord\Element\TrackChange;
@@ -472,6 +473,11 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
472473
if ($tablePositionNode !== null) {
473474
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
474475
}
476+
477+
$indentNode = $xmlReader->getElement('w:tblInd', $styleNode);
478+
if ($indentNode !== null) {
479+
$style['indent'] = $this->readTableIndent($xmlReader, $indentNode);
480+
}
475481
}
476482
}
477483

@@ -503,6 +509,24 @@ private function readTablePosition(XMLReader $xmlReader, \DOMElement $domNode)
503509
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
504510
}
505511

512+
/**
513+
* Read w:tblInd
514+
*
515+
* @param \PhpOffice\Common\XMLReader $xmlReader
516+
* @param \DOMElement $domNode
517+
* @return TblWidthComplexType
518+
*/
519+
private function readTableIndent(XMLReader $xmlReader, \DOMElement $domNode)
520+
{
521+
$styleDefs = array(
522+
'value' => array(self::READ_VALUE, '.', 'w:w'),
523+
'type' => array(self::READ_VALUE, '.', 'w:type'),
524+
);
525+
$styleDefs = $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
526+
527+
return new TblWidthComplexType((int) $styleDefs['value'], $styleDefs['type']);
528+
}
529+
506530
/**
507531
* Read w:tcPr
508532
*

src/PhpWord/Style/Table.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
2021
use PhpOffice\PhpWord\SimpleType\Jc;
2122
use PhpOffice\PhpWord\SimpleType\JcTable;
2223
use PhpOffice\PhpWord\SimpleType\TblWidth;
@@ -159,6 +160,9 @@ class Table extends Border
159160
*/
160161
private $position;
161162

163+
/** @var TblWidthComplexType|null */
164+
private $indent;
165+
162166
/**
163167
* The width of each column, computed based on the max cell width of each column
164168
*
@@ -751,4 +755,24 @@ public function setColumnWidths(array $value = null)
751755
{
752756
$this->columnWidths = $value;
753757
}
758+
759+
/**
760+
* @return TblWidthComplexType
761+
*/
762+
public function getIndent()
763+
{
764+
return $this->indent;
765+
}
766+
767+
/**
768+
* @param TblWidthComplexType $indent
769+
* @return self
770+
* @see http://www.datypic.com/sc/ooxml/e-w_tblInd-1.html
771+
*/
772+
public function setIndent(TblWidthComplexType $indent)
773+
{
774+
$this->indent = $indent;
775+
776+
return $this;
777+
}
754778
}

src/PhpWord/Writer/Word2007/Style/Table.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
7979

8080
$this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth());
8181
$this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing());
82+
$this->writeIndent($xmlWriter, $style);
8283
$this->writeLayout($xmlWriter, $style->getLayout());
8384

8485
// Position
@@ -216,4 +217,19 @@ public function setWidth($value = null)
216217
{
217218
$this->width = $value;
218219
}
220+
221+
/**
222+
* @param XMLWriter $xmlWriter
223+
* @param TableStyle $style
224+
*/
225+
private function writeIndent(XMLWriter $xmlWriter, TableStyle $style)
226+
{
227+
$indent = $style->getIndent();
228+
229+
if ($indent === null) {
230+
return;
231+
}
232+
233+
$this->writeTblWidth($xmlWriter, 'w:tblInd', $indent->getType(), $indent->getValue());
234+
}
219235
}

tests/PhpWord/Reader/Word2007/StyleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,23 @@ public function testReadPosition()
126126
$fontStyle = $textRun->getElement(0)->getFontStyle();
127127
$this->assertEquals(15, $fontStyle->getPosition());
128128
}
129+
130+
public function testReadIndent()
131+
{
132+
$documentXml = '<w:tbl>
133+
<w:tblPr>
134+
<w:tblInd w:w="2160" w:type="dxa"/>
135+
</w:tblPr>
136+
</w:tbl>';
137+
138+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
139+
140+
$elements = $phpWord->getSection(0)->getElements();
141+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
142+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
143+
/** @var \PhpOffice\PhpWord\Style\Table $tableStyle */
144+
$tableStyle = $elements[0]->getStyle();
145+
$this->assertSame(TblWidth::TWIP, $tableStyle->getIndent()->getType());
146+
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
147+
}
129148
}

tests/PhpWord/Style/TableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
2021
use PhpOffice\PhpWord\SimpleType\JcTable;
2122
use PhpOffice\PhpWord\SimpleType\TblWidth;
2223

@@ -57,6 +58,7 @@ public function testDefaultValues()
5758
$this->assertNull($object->getBgColor());
5859
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
5960
$this->assertEquals(TblWidth::AUTO, $object->getUnit());
61+
$this->assertNull($object->getIndent());
6062
}
6163

6264
/**
@@ -208,4 +210,13 @@ public function testTablePosition()
208210
$this->assertNotNull($object->getPosition());
209211
$this->assertEquals(TablePosition::VANCHOR_PAGE, $object->getPosition()->getVertAnchor());
210212
}
213+
214+
public function testIndent()
215+
{
216+
$indent = new TblWidthComplexType(100, TblWidth::TWIP);
217+
218+
$table = new Table(array('indent' => $indent));
219+
220+
$this->assertSame($indent, $table->getIndent());
221+
}
211222
}

tests/PhpWord/Writer/Word2007/Style/TableTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
1919

20+
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
21+
use PhpOffice\PhpWord\SimpleType\TblWidth;
2022
use PhpOffice\PhpWord\Style\Table;
2123
use PhpOffice\PhpWord\Style\TablePosition;
2224
use PhpOffice\PhpWord\TestHelperDOCX;
@@ -75,7 +77,7 @@ public function testCellSpacing()
7577
$path = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing';
7678
$this->assertTrue($doc->elementExists($path));
7779
$this->assertEquals(10.3, $doc->getElementAttribute($path, 'w:w'));
78-
$this->assertEquals(\PhpOffice\PhpWord\SimpleType\TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
80+
$this->assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
7981
}
8082

8183
/**
@@ -118,4 +120,25 @@ public function testTablePosition()
118120
$this->assertEquals(TablePosition::YALIGN_TOP, $doc->getElementAttribute($path, 'w:tblpYSpec'));
119121
$this->assertEquals(60, $doc->getElementAttribute($path, 'w:tblpY'));
120122
}
123+
124+
public function testIndent()
125+
{
126+
$value = 100;
127+
$type = TblWidth::TWIP;
128+
129+
$tableStyle = new Table();
130+
$tableStyle->setIndent(new TblWidthComplexType($value, $type));
131+
132+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
133+
$section = $phpWord->addSection();
134+
$table = $section->addTable($tableStyle);
135+
$table->addRow();
136+
137+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
138+
139+
$path = '/w:document/w:body/w:tbl/w:tblPr/w:tblInd';
140+
$this->assertTrue($doc->elementExists($path));
141+
$this->assertSame($value, (int) $doc->getElementAttribute($path, 'w:w'));
142+
$this->assertSame($type, $doc->getElementAttribute($path, 'w:type'));
143+
}
121144
}

0 commit comments

Comments
 (0)