Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 #1343
- Added parsing of internal links in HTML reader @lalop #1336

### Fixed
Expand Down
1 change: 1 addition & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 59 additions & 0 deletions src/PhpWord/ComplexType/TblWidth.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -472,6 +473,11 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
if ($tablePositionNode !== null) {
$style['position'] = $this->readTablePosition($xmlReader, $tablePositionNode);
}

$indentNode = $xmlReader->getElement('w:tblInd', $styleNode);
if ($indentNode !== null) {
$style['indent'] = $this->readTableIndent($xmlReader, $indentNode);
}
}
}

Expand Down Expand Up @@ -503,6 +509,24 @@ private function readTablePosition(XMLReader $xmlReader, \DOMElement $domNode)
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
*
Expand Down
24 changes: 24 additions & 0 deletions src/PhpWord/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,6 +160,9 @@ class Table extends Border
*/
private $position;

/** @var TblWidthComplexType|null */
private $indent;

/**
* Create new table style
*
Expand Down Expand Up @@ -724,4 +728,24 @@ public function setPosition($value = null)

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;
}
}
16 changes: 16 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)

$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
Expand Down Expand Up @@ -216,4 +217,19 @@ public function setWidth($value = null)
{
$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());
}
}
19 changes: 19 additions & 0 deletions tests/PhpWord/Reader/Word2007/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,23 @@ public function testReadPosition()
$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());
}
}
11 changes: 11 additions & 0 deletions tests/PhpWord/Style/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,6 +58,7 @@ public function testDefaultValues()
$this->assertNull($object->getBgColor());
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
$this->assertEquals(TblWidth::AUTO, $object->getUnit());
$this->assertNull($object->getIndent());
}

/**
Expand Down Expand Up @@ -208,4 +210,13 @@ public function testTablePosition()
$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());
}
}
25 changes: 24 additions & 1 deletion tests/PhpWord/Writer/Word2007/Style/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,7 +77,7 @@ public function testCellSpacing()
$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'));
}

/**
Expand Down Expand Up @@ -118,4 +120,25 @@ public function testTablePosition()
$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'));
}
}