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 @@ -10,6 +10,7 @@ v0.15.0 (?? ??? 2018)
- Parse formatting inside HTML lists - @troosan @samimussbach #1239 #945 #1215 #508
- Parsing of CSS `direction` instruction, HTML `lang` attribute, formatting inside table cell - @troosan #1273 #1252 #1254
- Add support for Track changes @Cip @troosan #354 #1262
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276

### Fixed
- Fix reading of docx default style - @troosan #1238
Expand Down
1 change: 1 addition & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Available Table style options:
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in twips.
- ``width``. Table width in percent.
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.

Available Row style options:

Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
$styleDefs["border{$ucfSide}Color"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:color');
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
}
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/PhpWord/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ class Table extends Border
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)

//values for http://www.datypic.com/sc/ooxml/t-w_ST_TblLayoutType.html
/**
* AutoFit Table Layout
*
* @var string
*/
const LAYOUT_AUTO = 'autofit';
/**
* Fixed Width Table Layout
*
* @var string
*/
const LAYOUT_FIXED = 'fixed';

/**
* Is this a first row style?
*
Expand Down Expand Up @@ -121,6 +135,11 @@ class Table extends Border
*/
private $unit = self::WIDTH_AUTO;

/**
* @var string Table Layout
*/
private $layout = self::LAYOUT_AUTO;

/**
* Create new table style
*
Expand Down Expand Up @@ -582,6 +601,30 @@ public function setUnit($value = null)
return $this;
}

/**
* Get layout
*
* @return string
*/
public function getLayout()
{
return $this->layout;
}

/**
* Set layout
*
* @param string $value
* @return self
*/
public function setLayout($value = null)
{
$enum = array(self::LAYOUT_AUTO, self::LAYOUT_FIXED);
$this->layout = $this->setEnumVal($value, $enum, $this->layout);

return $this;
}

/**
* Get table style only property by checking if it's a firstRow
*
Expand Down
14 changes: 14 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
}

$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
$this->writeLayout($xmlWriter, $style->getLayout());
$this->writeMargin($xmlWriter, $style);
$this->writeBorder($xmlWriter, $style);

Expand Down Expand Up @@ -106,6 +107,19 @@ private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
$xmlWriter->endElement(); // w:tblW
}

/**
* Enable/Disable automatic resizing of the table
*
* @param \PhpOffice\Common\XMLWriter $xmlWriter
* @param string $layout autofit / fixed
*/
private function writeLayout(XMLWriter $xmlWriter, $layout)
{
$xmlWriter->startElement('w:tblLayout');
$xmlWriter->writeAttribute('w:type', $layout);
$xmlWriter->endElement(); // w:tblLayout
}

/**
* Write margin.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpWord/Element/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ public function testConstructFromString()
$this->assertNotNull($image->getImageStringData(true));
}

/**
* Test construct from GD
*/
public function testConstructFromGd()
{
$source = 'http://php.net/images/logos/php-icon.png';

$image = new Image($source);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);
$this->assertEquals($source, $image->getSource());
$this->assertEquals(md5($source), $image->getMediaId());
$this->assertEquals('image/png', $image->getImageType());
$this->assertEquals('png', $image->getImageExtension());
$this->assertEquals('imagecreatefrompng', $image->getImageCreateFunction());
$this->assertEquals('imagepng', $image->getImageFunction());
$this->assertTrue($image->isMemImage());

$this->assertNotNull($image->getImageStringData());
$this->assertNotNull($image->getImageStringData(true));
}

/**
* Test invalid string image
*
Expand Down
46 changes: 46 additions & 0 deletions tests/PhpWord/Reader/Word2007/StyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Reader\Word2007;

use PhpOffice\PhpWord\AbstractTestReader;
use PhpOffice\PhpWord\Style\Table;

/**
* Test class for PhpOffice\PhpWord\Reader\Word2007\Styles
*/
class StyleTest extends AbstractTestReader
{
/**
* Test reading of table layout
*/
public function testReadTableLayout()
{
$documentXml = '<w:tbl>
<w:tblPr>
<w:tblLayout w:type="fixed"/>
</w:tblPr>
</w:tbl>';

$phpWord = $this->getDocumentFromString($documentXml);

$elements = $this->get($phpWord->getSections(), 0)->getElements();
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
$this->assertEquals(Table::LAYOUT_FIXED, $elements[0]->getStyle()->getLayout());
}
}
11 changes: 11 additions & 0 deletions tests/PhpWord/Style/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,15 @@ public function testSetStyleValue()
$object->getBorderColor()
);
}

/**
* Tests table layout
*/
public function testTableLayout()
{
$object = new Table();
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
$object->setLayout(Table::LAYOUT_FIXED);
$this->assertEquals(Table::LAYOUT_FIXED, $object->getLayout());
}
}
58 changes: 58 additions & 0 deletions tests/PhpWord/Writer/Word2007/Style/TableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\Word2007\Style;

use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\TestHelperDOCX;

/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Style\Table
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Style\Table
* @runTestsInSeparateProcesses
*/
class TableTest extends \PHPUnit\Framework\TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}

/**
* Test write styles
*/
public function testTableLayout()
{
$tableStyle = new Table();
$tableStyle->setLayout(Table::LAYOUT_FIXED);

$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:tblLayout';
$this->assertTrue($doc->elementExists($path));
$this->assertEquals(Table::LAYOUT_FIXED, $doc->getElementAttribute($path, 'w:type'));
}
}