refactor attribute name to layout, add doc and tests

This commit is contained in:
troosan 2018-02-14 00:39:37 +01:00
parent 615c1d5576
commit 6a926e26f1
9 changed files with 165 additions and 17 deletions

View File

@ -10,6 +10,7 @@ v0.15.0 (?? ??? 2018)
- Parse formatting inside HTML lists - @troosan @samimussbach #1239 #945 #1215 #508 - 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 - 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 Track changes @Cip @troosan #354 #1262
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276
### Fixed ### Fixed
- Fix reading of docx default style - @troosan #1238 - Fix reading of docx default style - @troosan #1238

View File

@ -103,6 +103,7 @@ Available Table style options:
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips. - ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in twips. - ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in twips.
- ``width``. Table width in percent. - ``width``. Table width in percent.
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.
Available Row style options: Available Row style options:

View File

@ -425,6 +425,7 @@ abstract class AbstractPart
$styleDefs["border{$ucfSide}Color"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:color'); $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["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); $style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
} }
} }

View File

@ -28,8 +28,20 @@ class Table extends Border
const WIDTH_AUTO = 'auto'; // Automatically determined width const WIDTH_AUTO = 'auto'; // Automatically determined width
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit) 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 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? * Is this a first row style?
@ -124,9 +136,9 @@ class Table extends Border
private $unit = self::WIDTH_AUTO; 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 * Create new table style
@ -590,27 +602,25 @@ class Table extends Border
} }
/** /**
* Get stretch * Get layout
* *
* @return string * @return string
*/ */
public function getStretch() public function getLayout()
{ {
return $this->stretch; return $this->layout;
} }
/** /**
* Set stretch * Set layout
*
* Stretch the table to the page width
* *
* @param string $value * @param string $value
* @return self * @return self
*/ */
public function setStretch($value = null) public function setLayout($value = null)
{ {
$enum = array(self::STRETCH_AUTO, self::STRETCH_FIXED); $enum = array(self::LAYOUT_AUTO, self::LAYOUT_FIXED);
$this->stretch = $this->setEnumVal($value, $enum, $this->stretch); $this->layout = $this->setEnumVal($value, $enum, $this->layout);
return $this; return $this;
} }

View File

@ -77,7 +77,7 @@ class Table extends AbstractStyle
} }
$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit()); $this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
$this->writeLayout($xmlWriter, $style->getStretch()); $this->writeLayout($xmlWriter, $style->getLayout());
$this->writeMargin($xmlWriter, $style); $this->writeMargin($xmlWriter, $style);
$this->writeBorder($xmlWriter, $style); $this->writeBorder($xmlWriter, $style);
@ -112,12 +112,11 @@ class Table extends AbstractStyle
* *
* @param \PhpOffice\Common\XMLWriter $xmlWriter * @param \PhpOffice\Common\XMLWriter $xmlWriter
* @param string $layout autofit / fixed * @param string $layout autofit / fixed
* @return void
*/ */
private function writeLayout(XMLWriter $xmlWriter, $stretch) private function writeLayout(XMLWriter $xmlWriter, $layout)
{ {
$xmlWriter->startElement('w:tblLayout'); $xmlWriter->startElement('w:tblLayout');
$xmlWriter->writeAttribute('w:type', $stretch); $xmlWriter->writeAttribute('w:type', $layout);
$xmlWriter->endElement(); // w:tblLayout $xmlWriter->endElement(); // w:tblLayout
} }

View File

@ -210,6 +210,27 @@ class ImageTest extends \PHPUnit\Framework\TestCase
$this->assertNotNull($image->getImageStringData(true)); $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 * Test invalid string image
* *

View File

@ -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());
}
}

View File

@ -172,4 +172,15 @@ class TableTest extends \PHPUnit\Framework\TestCase
$object->getBorderColor() $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());
}
} }

View File

@ -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'));
}
}