Skip to content

Commit 6a926e2

Browse files
committed
refactor attribute name to layout, add doc and tests
1 parent 615c1d5 commit 6a926e2

File tree

9 files changed

+165
-17
lines changed

9 files changed

+165
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ v0.15.0 (?? ??? 2018)
1010
- Parse formatting inside HTML lists - @troosan @samimussbach #1239 #945 #1215 #508
1111
- Parsing of CSS `direction` instruction, HTML `lang` attribute, formatting inside table cell - @troosan #1273 #1252 #1254
1212
- Add support for Track changes @Cip @troosan #354 #1262
13+
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276
1314

1415
### Fixed
1516
- Fix reading of docx default style - @troosan #1238

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Available Table style options:
103103
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
104104
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in twips.
105105
- ``width``. Table width in percent.
106+
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.
106107

107108
Available Row style options:
108109

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
425425
$styleDefs["border{$ucfSide}Color"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:color');
426426
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
427427
}
428+
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
428429
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
429430
}
430431
}

src/PhpWord/Style/Table.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,20 @@ class Table extends Border
2828
const WIDTH_AUTO = 'auto'; // Automatically determined width
2929
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
3030
const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
31-
const STRETCH_AUTO = 'autofit'; // Automatically stretch the table to fit the page width
32-
const STRETCH_FIXED = 'fixed'; // Do not stretch the table to fit the page width
31+
32+
//values for http://www.datypic.com/sc/ooxml/t-w_ST_TblLayoutType.html
33+
/**
34+
* AutoFit Table Layout
35+
*
36+
* @var string
37+
*/
38+
const LAYOUT_AUTO = 'autofit';
39+
/**
40+
* Fixed Width Table Layout
41+
*
42+
* @var string
43+
*/
44+
const LAYOUT_FIXED = 'fixed';
3345

3446
/**
3547
* Is this a first row style?
@@ -124,9 +136,9 @@ class Table extends Border
124136
private $unit = self::WIDTH_AUTO;
125137

126138
/**
127-
* @var string Stretch the table to the page width
139+
* @var string Table Layout
128140
*/
129-
private $stretch = self::STRETCH_AUTO;
141+
private $layout = self::LAYOUT_AUTO;
130142

131143
/**
132144
* Create new table style
@@ -590,27 +602,25 @@ public function setUnit($value = null)
590602
}
591603

592604
/**
593-
* Get stretch
605+
* Get layout
594606
*
595607
* @return string
596608
*/
597-
public function getStretch()
609+
public function getLayout()
598610
{
599-
return $this->stretch;
611+
return $this->layout;
600612
}
601613

602614
/**
603-
* Set stretch
604-
*
605-
* Stretch the table to the page width
615+
* Set layout
606616
*
607617
* @param string $value
608618
* @return self
609619
*/
610-
public function setStretch($value = null)
620+
public function setLayout($value = null)
611621
{
612-
$enum = array(self::STRETCH_AUTO, self::STRETCH_FIXED);
613-
$this->stretch = $this->setEnumVal($value, $enum, $this->stretch);
622+
$enum = array(self::LAYOUT_AUTO, self::LAYOUT_FIXED);
623+
$this->layout = $this->setEnumVal($value, $enum, $this->layout);
614624

615625
return $this;
616626
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
7777
}
7878

7979
$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
80-
$this->writeLayout($xmlWriter, $style->getStretch());
80+
$this->writeLayout($xmlWriter, $style->getLayout());
8181
$this->writeMargin($xmlWriter, $style);
8282
$this->writeBorder($xmlWriter, $style);
8383

@@ -112,12 +112,11 @@ private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
112112
*
113113
* @param \PhpOffice\Common\XMLWriter $xmlWriter
114114
* @param string $layout autofit / fixed
115-
* @return void
116115
*/
117-
private function writeLayout(XMLWriter $xmlWriter, $stretch)
116+
private function writeLayout(XMLWriter $xmlWriter, $layout)
118117
{
119118
$xmlWriter->startElement('w:tblLayout');
120-
$xmlWriter->writeAttribute('w:type', $stretch);
119+
$xmlWriter->writeAttribute('w:type', $layout);
121120
$xmlWriter->endElement(); // w:tblLayout
122121
}
123122

tests/PhpWord/Element/ImageTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,27 @@ public function testConstructFromString()
210210
$this->assertNotNull($image->getImageStringData(true));
211211
}
212212

213+
/**
214+
* Test construct from GD
215+
*/
216+
public function testConstructFromGd()
217+
{
218+
$source = 'http://php.net/images/logos/php-icon.png';
219+
220+
$image = new Image($source);
221+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image);
222+
$this->assertEquals($source, $image->getSource());
223+
$this->assertEquals(md5($source), $image->getMediaId());
224+
$this->assertEquals('image/png', $image->getImageType());
225+
$this->assertEquals('png', $image->getImageExtension());
226+
$this->assertEquals('imagecreatefrompng', $image->getImageCreateFunction());
227+
$this->assertEquals('imagepng', $image->getImageFunction());
228+
$this->assertTrue($image->isMemImage());
229+
230+
$this->assertNotNull($image->getImageStringData());
231+
$this->assertNotNull($image->getImageStringData(true));
232+
}
233+
213234
/**
214235
* Test invalid string image
215236
*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Reader\Word2007;
19+
20+
use PhpOffice\PhpWord\AbstractTestReader;
21+
use PhpOffice\PhpWord\Style\Table;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Reader\Word2007\Styles
25+
*/
26+
class StyleTest extends AbstractTestReader
27+
{
28+
/**
29+
* Test reading of table layout
30+
*/
31+
public function testReadTableLayout()
32+
{
33+
$documentXml = '<w:tbl>
34+
<w:tblPr>
35+
<w:tblLayout w:type="fixed"/>
36+
</w:tblPr>
37+
</w:tbl>';
38+
39+
$phpWord = $this->getDocumentFromString($documentXml);
40+
41+
$elements = $this->get($phpWord->getSections(), 0)->getElements();
42+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
43+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
44+
$this->assertEquals(Table::LAYOUT_FIXED, $elements[0]->getStyle()->getLayout());
45+
}
46+
}

tests/PhpWord/Style/TableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,15 @@ public function testSetStyleValue()
172172
$object->getBorderColor()
173173
);
174174
}
175+
176+
/**
177+
* Tests table layout
178+
*/
179+
public function testTableLayout()
180+
{
181+
$object = new Table();
182+
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
183+
$object->setLayout(Table::LAYOUT_FIXED);
184+
$this->assertEquals(Table::LAYOUT_FIXED, $object->getLayout());
185+
}
175186
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
19+
20+
use PhpOffice\PhpWord\Style\Table;
21+
use PhpOffice\PhpWord\TestHelperDOCX;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Writer\Word2007\Style\Table
25+
*
26+
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Style\Table
27+
* @runTestsInSeparateProcesses
28+
*/
29+
class TableTest extends \PHPUnit\Framework\TestCase
30+
{
31+
/**
32+
* Executed before each method of the class
33+
*/
34+
public function tearDown()
35+
{
36+
TestHelperDOCX::clear();
37+
}
38+
39+
/**
40+
* Test write styles
41+
*/
42+
public function testTableLayout()
43+
{
44+
$tableStyle = new Table();
45+
$tableStyle->setLayout(Table::LAYOUT_FIXED);
46+
47+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
48+
$section = $phpWord->addSection();
49+
$table = $section->addTable($tableStyle);
50+
$table->addRow();
51+
52+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
53+
54+
$path = '/w:document/w:body/w:tbl/w:tblPr/w:tblLayout';
55+
$this->assertTrue($doc->elementExists($path));
56+
$this->assertEquals(Table::LAYOUT_FIXED, $doc->getElementAttribute($path, 'w:type'));
57+
}
58+
}

0 commit comments

Comments
 (0)