From 6a926e26f136a41937c8f5aa2eaef79d40cadcd8 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 14 Feb 2018 00:39:37 +0100 Subject: [PATCH] refactor attribute name to layout, add doc and tests --- CHANGELOG.md | 1 + docs/styles.rst | 1 + src/PhpWord/Reader/Word2007/AbstractPart.php | 1 + src/PhpWord/Style/Table.php | 36 +++++++----- src/PhpWord/Writer/Word2007/Style/Table.php | 7 +-- tests/PhpWord/Element/ImageTest.php | 21 +++++++ tests/PhpWord/Reader/Word2007/StyleTest.php | 46 +++++++++++++++ tests/PhpWord/Style/TableTest.php | 11 ++++ .../Writer/Word2007/Style/TableTest.php | 58 +++++++++++++++++++ 9 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 tests/PhpWord/Reader/Word2007/StyleTest.php create mode 100644 tests/PhpWord/Writer/Word2007/Style/TableTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 344517c7..762bf560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/styles.rst b/docs/styles.rst index 6166f5c9..031f3759 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -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: diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 9276823b..09d32dbb 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -425,6 +425,7 @@ abstract class AbstractPart $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); } } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index aa397949..d0ff47ed 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -28,8 +28,20 @@ class Table extends Border const WIDTH_AUTO = 'auto'; // Automatically determined width 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) - const STRETCH_AUTO = 'autofit'; // Automatically stretch the table to fit the page width - const STRETCH_FIXED = 'fixed'; // Do not stretch the table to fit the page width + + //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? @@ -124,9 +136,9 @@ class Table extends Border private $unit = self::WIDTH_AUTO; /** - * @var string Stretch the table to the page width + * @var string Table Layout */ - private $stretch = self::STRETCH_AUTO; + private $layout = self::LAYOUT_AUTO; /** * Create new table style @@ -590,27 +602,25 @@ class Table extends Border } /** - * Get stretch + * Get layout * * @return string */ - public function getStretch() + public function getLayout() { - return $this->stretch; + return $this->layout; } /** - * Set stretch - * - * Stretch the table to the page width + * Set layout * * @param string $value * @return self */ - public function setStretch($value = null) + public function setLayout($value = null) { - $enum = array(self::STRETCH_AUTO, self::STRETCH_FIXED); - $this->stretch = $this->setEnumVal($value, $enum, $this->stretch); + $enum = array(self::LAYOUT_AUTO, self::LAYOUT_FIXED); + $this->layout = $this->setEnumVal($value, $enum, $this->layout); return $this; } diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 26df8436..e2e8f978 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -77,7 +77,7 @@ class Table extends AbstractStyle } $this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit()); - $this->writeLayout($xmlWriter, $style->getStretch()); + $this->writeLayout($xmlWriter, $style->getLayout()); $this->writeMargin($xmlWriter, $style); $this->writeBorder($xmlWriter, $style); @@ -112,12 +112,11 @@ class Table extends AbstractStyle * * @param \PhpOffice\Common\XMLWriter $xmlWriter * @param string $layout autofit / fixed - * @return void */ - private function writeLayout(XMLWriter $xmlWriter, $stretch) + private function writeLayout(XMLWriter $xmlWriter, $layout) { $xmlWriter->startElement('w:tblLayout'); - $xmlWriter->writeAttribute('w:type', $stretch); + $xmlWriter->writeAttribute('w:type', $layout); $xmlWriter->endElement(); // w:tblLayout } diff --git a/tests/PhpWord/Element/ImageTest.php b/tests/PhpWord/Element/ImageTest.php index 0966ea4d..8bebce91 100644 --- a/tests/PhpWord/Element/ImageTest.php +++ b/tests/PhpWord/Element/ImageTest.php @@ -210,6 +210,27 @@ class ImageTest extends \PHPUnit\Framework\TestCase $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 * diff --git a/tests/PhpWord/Reader/Word2007/StyleTest.php b/tests/PhpWord/Reader/Word2007/StyleTest.php new file mode 100644 index 00000000..ce59f4c3 --- /dev/null +++ b/tests/PhpWord/Reader/Word2007/StyleTest.php @@ -0,0 +1,46 @@ + + + + + '; + + $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()); + } +} diff --git a/tests/PhpWord/Style/TableTest.php b/tests/PhpWord/Style/TableTest.php index 2d57b1b8..dafdeb31 100644 --- a/tests/PhpWord/Style/TableTest.php +++ b/tests/PhpWord/Style/TableTest.php @@ -172,4 +172,15 @@ class TableTest extends \PHPUnit\Framework\TestCase $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()); + } } diff --git a/tests/PhpWord/Writer/Word2007/Style/TableTest.php b/tests/PhpWord/Writer/Word2007/Style/TableTest.php new file mode 100644 index 00000000..a89dd610 --- /dev/null +++ b/tests/PhpWord/Writer/Word2007/Style/TableTest.php @@ -0,0 +1,58 @@ +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')); + } +}