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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
v0.15.0 (?? ??? 2018)
----------------------
### Added
- Parsing of "align" HTML attribute - @troosan #1231
- Parsing of `align` HTML attribute - @troosan #1231
- 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
- Add support for Cell Spacing @dox07 @troosan #1040

### Fixed
- Fix reading of docx default style - @troosan #1238
Expand All @@ -19,6 +20,7 @@ v0.15.0 (?? ??? 2018)
- Save PNG alpha information when using remote images. @samsullivan #779
- Fix parsing of `<w:br/>` tag. @troosan #1274
- Bookmark are not writton as internal link in html writer @troosan #1263
- It should be possible to add a Footnote in a ListItemRun @troosan #1287 #1287



Expand Down
2 changes: 2 additions & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ Available Table style options:
- ``border(Top|Right|Bottom|Left)Size``. Border size in *twip*.
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in *twip*.
- ``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.
- ``cellSpacing`` Cell spacing in *twip*

Available Row style options:

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample_09_Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$section->addText('Fancy table', $header);

$fancyTableStyleName = 'Fancy Table';
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50);
$fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
$fancyTableCellStyle = array('valign' => 'center');
$fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR);
Expand Down
2 changes: 2 additions & 0 deletions samples/Sample_14_ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
$listItemRun = $section->addListItemRun();
$listItemRun->addText('List item 2');
$listItemRun->addText(' in italic', array('italic' => true));
$footnote = $listItemRun->addFootnote();
$footnote->addText('this is a footnote on a list item');
$listItemRun = $section->addListItemRun();
$listItemRun->addText('List item 3');
$listItemRun->addText(' underlined', array('underline' => 'dash'));
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private function checkValidity($method)
'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'CheckBox' => array('Section', 'Header', 'Footer', 'Cell', 'TextRun'),
'TextBox' => array('Section', 'Header', 'Footer', 'Cell'),
'Footnote' => array('Section', 'TextRun', 'Cell'),
'Footnote' => array('Section', 'TextRun', 'Cell', 'ListItemRun'),
'Endnote' => array('Section', 'TextRun', 'Cell'),
'PreserveText' => array('Section', 'Header', 'Footer', 'Cell'),
'Title' => array('Section', 'Cell'),
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 @@ -426,6 +426,7 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
}
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
$styleDefs['cellSpacing'] = array(self::READ_VALUE, 'w:tblCellSpacing', 'w:w');
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ private static function parseList($node, $element, &$styles, &$data)
}
}

/**
* @param bool $isOrderedList
* @return array
*/
private static function getListStyle($isOrderedList)
{
if ($isOrderedList) {
Expand Down Expand Up @@ -547,13 +551,13 @@ private static function parseStyle($attribute, $styles)
case 'width':
if (preg_match('/([0-9]+[a-z]+)/', $cValue, $matches)) {
$styles['width'] = Converter::cssToTwip($matches[1]);
$styles['unit'] = \PhpOffice\PhpWord\Style\Table::WIDTH_TWIP;
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP;
} elseif (preg_match('/([0-9]+)%/', $cValue, $matches)) {
$styles['width'] = $matches[1] * 50;
$styles['unit'] = \PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT;
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT;
} elseif (preg_match('/([0-9]+)/', $cValue, $matches)) {
$styles['width'] = $matches[1];
$styles['unit'] = \PhpOffice\PhpWord\Style\Table::WIDTH_AUTO;
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO;
}
break;
case 'border':
Expand Down
42 changes: 42 additions & 0 deletions src/PhpWord/SimpleType/TblWidth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\SimpleType;

use PhpOffice\PhpWord\Shared\AbstractEnum;

/**
* Table Width Units
*
* @since 0.15.0
*
* @see http://www.datypic.com/sc/ooxml/t-w_ST_TblWidth.html
*/
final class TblWidth extends AbstractEnum
{
//No Width
const NIL = 'nil';

//Automatically Determined Width
const AUTO = 'auto';

//Width in Fiftieths of a Percent
const PERCENT = 'pct';

//Width in Twentieths of a Point
const TWIP = 'dxa';
}
6 changes: 4 additions & 2 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord\Style;

use PhpOffice\PhpWord\SimpleType\TblWidth;

/**
* Table cell style
*/
Expand Down Expand Up @@ -123,7 +125,7 @@ class Cell extends Border
*
* @var string
*/
private $unit = Table::WIDTH_TWIP;
private $unit = TblWidth::TWIP;

/**
* Get vertical align.
Expand Down Expand Up @@ -308,7 +310,7 @@ public function getUnit()
*/
public function setUnit($value)
{
$this->unit = $this->setEnumVal($value, array(Table::WIDTH_AUTO, Table::WIDTH_PERCENT, Table::WIDTH_TWIP), Table::WIDTH_TWIP);
$this->unit = $this->setEnumVal($value, array(TblWidth::AUTO, TblWidth::PERCENT, TblWidth::TWIP), TblWidth::TWIP);

return $this;
}
Expand Down
38 changes: 33 additions & 5 deletions src/PhpWord/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@

use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\SimpleType\TblWidth;

class Table extends Border
{
/**
* @const string Table width units http://www.schemacentral.com/sc/ooxml/t-w_ST_TblWidth.html
* @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO instead
*/
const WIDTH_AUTO = 'auto'; // Automatically determined width
/**
* @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT instead
*/
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
/**
* @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP instead
*/
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
Expand Down Expand Up @@ -133,7 +140,12 @@ class Table extends Border
/**
* @var string Width unit
*/
private $unit = self::WIDTH_AUTO;
private $unit = TblWidth::AUTO;

/**
* @var int|float cell spacing value
*/
protected $cellSpacing = null;

/**
* @var string Table Layout
Expand All @@ -152,7 +164,7 @@ public function __construct($tableStyle = null, $firstRowStyle = null)
if ($firstRowStyle !== null && is_array($firstRowStyle)) {
$this->firstRowStyle = clone $this;
$this->firstRowStyle->isFirstRow = true;
unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom);
unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom, $this->firstRowStyle->cellSpacing);
$this->firstRowStyle->setStyleByArray($firstRowStyle);
}

Expand All @@ -161,6 +173,22 @@ public function __construct($tableStyle = null, $firstRowStyle = null)
}
}

/**
* @param float|int $cellSpacing
*/
public function setCellSpacing($cellSpacing = null)
{
$this->cellSpacing = $cellSpacing;
}

/**
* @return float|int
*/
public function getCellSpacing()
{
return $this->cellSpacing;
}

/**
* Set first row
*
Expand Down Expand Up @@ -595,8 +623,8 @@ public function getUnit()
*/
public function setUnit($value = null)
{
$enum = array(self::WIDTH_AUTO, self::WIDTH_PERCENT, self::WIDTH_TWIP);
$this->unit = $this->setEnumVal($value, $enum, $this->unit);
TblWidth::validate($value);
$this->unit = $value;

return $this;
}
Expand Down
40 changes: 23 additions & 17 deletions src/PhpWord/Writer/Word2007/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;

use PhpOffice\Common\XMLWriter;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\Style\Table as TableStyle;
use PhpOffice\PhpWord\Writer\Word2007\Element\TableAlignment;

Expand Down Expand Up @@ -49,7 +50,7 @@ public function write()
$xmlWriter->writeAttribute('w:val', $style);
$xmlWriter->endElement();
if (null !== $this->width) {
$this->writeWidth($xmlWriter, $this->width, 'pct');
$this->writeTblWidth($xmlWriter, 'w:tblW', TblWidth::PERCENT, $this->width);
}
$xmlWriter->endElement();
}
Expand All @@ -76,7 +77,8 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
$xmlWriter->endElement();
}

$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
$this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth());
$this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing());
$this->writeLayout($xmlWriter, $style->getLayout());
$this->writeMargin($xmlWriter, $style);
$this->writeBorder($xmlWriter, $style);
Expand All @@ -92,21 +94,6 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
}
}

/**
* Write width.
*
* @param \PhpOffice\Common\XMLWriter $xmlWriter
* @param int $width
* @param string $unit
*/
private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
{
$xmlWriter->startElement('w:tblW');
$xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', $unit);
$xmlWriter->endElement(); // w:tblW
}

/**
* Enable/Disable automatic resizing of the table
*
Expand Down Expand Up @@ -159,6 +146,25 @@ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
}
}

/**
* Writes a table width
*
* @param \PhpOffice\Common\XMLWriter $xmlWriter
* @param string $elementName
* @param string $unit
* @param int|float $width
*/
private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width = null)
{
if (null === $width) {
return;
}
$xmlWriter->startElement($elementName);
$xmlWriter->writeAttributeIf(null !== $width, 'w:w', $width);
$xmlWriter->writeAttribute('w:type', $unit);
$xmlWriter->endElement();
}

/**
* Write row style.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/PhpWord/Reader/Word2007/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Reader\Word2007;

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

/**
Expand All @@ -43,4 +44,26 @@ public function testReadTableLayout()
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
$this->assertEquals(Table::LAYOUT_FIXED, $elements[0]->getStyle()->getLayout());
}

/**
* Test reading of cell spacing
*/
public function testReadCellSpacing()
{
$documentXml = '<w:tbl>
<w:tblPr>
<w:tblCellSpacing w:w="10.5" w:type="dxa"/>
</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(TblWidth::AUTO, $elements[0]->getStyle()->getUnit());
/** @var \PhpOffice\PhpWord\Style\Table $tableStyle */
$tableStyle = $elements[0]->getStyle();
$this->assertEquals(10.5, $tableStyle->getCellSpacing());
}
}
Loading