#237: Ability to define table width (in percent and twip) and position
This commit is contained in:
parent
e589961e68
commit
f8f98cccab
|
|
@ -16,6 +16,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
|
||||||
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235
|
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235
|
||||||
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
|
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
|
||||||
- RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
|
- RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
|
||||||
|
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
|
||||||
|
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -87,11 +87,12 @@ $table->addCell(null, $cellRowContinue);
|
||||||
// 4. Nested table
|
// 4. Nested table
|
||||||
|
|
||||||
$section->addTextBreak(2);
|
$section->addTextBreak(2);
|
||||||
$section->addText('Nested table', $header);
|
$section->addText('Nested table in a centered and 50% width table.', $header);
|
||||||
|
|
||||||
$cell = $section->addTable()->addRow()->addCell();
|
$table = $section->addTable(array('width' => 50 * 50, 'unit' => 'pct', 'align' => 'center'));
|
||||||
|
$cell = $table->addRow()->addCell();
|
||||||
$cell->addText('This cell contains nested table.');
|
$cell->addText('This cell contains nested table.');
|
||||||
$innerCell = $cell->addTable()->addRow()->addCell();
|
$innerCell = $cell->addTable(array('align' => 'center'))->addRow()->addCell();
|
||||||
$innerCell->addText('Inside nested table');
|
$innerCell->addText('Inside nested table');
|
||||||
|
|
||||||
// Save file
|
// Save file
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,8 @@ class Html
|
||||||
*
|
*
|
||||||
* Note: $stylesheet parameter is removed to avoid PHPMD error for unused parameter
|
* Note: $stylesheet parameter is removed to avoid PHPMD error for unused parameter
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\PhpWord\Element\AbstractElement $object Where the parts need to be added
|
* @param \PhpOffice\PhpWord\Element\AbstractContainer $object Where the parts need to be added
|
||||||
* @param string $html the code to parse
|
* @param string $html the code to parse
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public static function addHtml($object, $html)
|
public static function addHtml($object, $html)
|
||||||
{
|
{
|
||||||
|
|
@ -53,7 +52,6 @@ class Html
|
||||||
*
|
*
|
||||||
* @param \DOMNode $node Node to check on attributes and to compile a style array
|
* @param \DOMNode $node Node to check on attributes and to compile a style array
|
||||||
* @param array $style is supplied, the inline style attributes are added to the already existing style
|
* @param array $style is supplied, the inline style attributes are added to the already existing style
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
protected static function parseInlineStyle($node, $style = array())
|
protected static function parseInlineStyle($node, $style = array())
|
||||||
{
|
{
|
||||||
|
|
@ -100,10 +98,9 @@ class Html
|
||||||
* parse a node and add a corresponding element to the object
|
* parse a node and add a corresponding element to the object
|
||||||
*
|
*
|
||||||
* @param \DOMNode $node node to parse
|
* @param \DOMNode $node node to parse
|
||||||
* @param \PhpOffice\PhpWord\Element\AbstractElement $object object to add an element corresponding with the node
|
* @param \PhpOffice\PhpWord\Element\AbstractContainer $object object to add an element corresponding with the node
|
||||||
* @param array $styles Array with all styles
|
* @param array $styles Array with all styles
|
||||||
* @param array $data Array to transport data to a next level in the DOM tree, for example level of listitems
|
* @param array $data Array to transport data to a next level in the DOM tree, for example level of listitems
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
protected static function parseNode(
|
protected static function parseNode(
|
||||||
$node,
|
$node,
|
||||||
|
|
@ -171,17 +168,26 @@ class Html
|
||||||
case 'table':
|
case 'table':
|
||||||
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
|
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
|
||||||
$newobject = $object->addTable();
|
$newobject = $object->addTable();
|
||||||
// if ($attributes->getNamedItem('width') !== null)$newobject->setWidth($attributes->getNamedItem('width')->value);
|
// if ($attributes->getNamedItem('width') !== null) {
|
||||||
|
// $newobject->setWidth($attributes->getNamedItem('width')->value);
|
||||||
|
// }
|
||||||
break;
|
break;
|
||||||
case 'tr':
|
case 'tr':
|
||||||
|
/** @var \PhpOffice\PhpWord\Element\Table $object Scrutinizer type hint */
|
||||||
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
|
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
|
||||||
$newobject = $object->addRow();
|
$newobject = $object->addRow();
|
||||||
// if ($attributes->getNamedItem('height') !== null)$newobject->setHeight($attributes->getNamedItem('height')->value);
|
// if ($attributes->getNamedItem('height') !== null) {
|
||||||
|
// $newobject->setHeight($attributes->getNamedItem('height')->value);
|
||||||
|
// }
|
||||||
break;
|
break;
|
||||||
case 'td':
|
case 'td':
|
||||||
|
/** @var \PhpOffice\PhpWord\Element\Row $object Scrutinizer type hint */
|
||||||
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
|
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
|
||||||
// if ($attributes->getNamedItem('width') !== null)$newobject=$object->addCell($width=$attributes->getNamedItem('width')->value);
|
// if ($attributes->getNamedItem('width') !== null) {
|
||||||
// else $newobject=$object->addCell();
|
// $newobject=$object->addCell($width=$attributes->getNamedItem('width')->value);
|
||||||
|
// } else {
|
||||||
|
// $newobject=$object->addCell();
|
||||||
|
// }
|
||||||
$newobject = $object->addCell();
|
$newobject = $object->addCell();
|
||||||
break;
|
break;
|
||||||
case 'ul':
|
case 'ul':
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
<?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.
|
||||||
|
*
|
||||||
|
* @link https://github.com/PHPOffice/PHPWord
|
||||||
|
* @copyright 2010-2014 PHPWord contributors
|
||||||
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpWord\Style;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alignment style
|
||||||
|
*
|
||||||
|
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_Jc.html
|
||||||
|
* @since 0.11.0
|
||||||
|
*/
|
||||||
|
class Alignment extends AbstractStyle
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @const string Alignment http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html
|
||||||
|
*/
|
||||||
|
const ALIGN_LEFT = 'left'; // Align left
|
||||||
|
const ALIGN_RIGHT = 'right'; // Align right
|
||||||
|
const ALIGN_CENTER = 'center'; // Align center
|
||||||
|
const ALIGN_BOTH = 'both'; // Align both
|
||||||
|
const ALIGN_JUSTIFY = 'justify'; // Alias for align both
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string Alignment
|
||||||
|
*/
|
||||||
|
private $value = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new instance
|
||||||
|
*
|
||||||
|
* @param array $style
|
||||||
|
*/
|
||||||
|
public function __construct($style = array())
|
||||||
|
{
|
||||||
|
$this->setStyleByArray($style);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get alignment
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getValue()
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set alignment
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setValue($value = null)
|
||||||
|
{
|
||||||
|
if (strtolower($value) == self::ALIGN_JUSTIFY) {
|
||||||
|
$value = self::ALIGN_BOTH;
|
||||||
|
}
|
||||||
|
$enum = array(self::ALIGN_LEFT, self::ALIGN_RIGHT, self::ALIGN_CENTER, self::ALIGN_BOTH, self::ALIGN_JUSTIFY);
|
||||||
|
$this->value = $this->setEnumVal($value, $enum, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -30,15 +30,6 @@ class Paragraph extends AbstractStyle
|
||||||
*/
|
*/
|
||||||
const LINE_HEIGHT = 240;
|
const LINE_HEIGHT = 240;
|
||||||
|
|
||||||
/**
|
|
||||||
* @const string Alignment http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html
|
|
||||||
*/
|
|
||||||
const ALIGN_LEFT = 'left'; // Align left
|
|
||||||
const ALIGN_RIGHT = 'right'; // Align right
|
|
||||||
const ALIGN_CENTER = 'center'; // Align center
|
|
||||||
const ALIGN_BOTH = 'both'; // Align both
|
|
||||||
const ALIGN_JUSTIFY = 'justify'; // Alias for align both
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aliases
|
* Aliases
|
||||||
*
|
*
|
||||||
|
|
@ -46,13 +37,6 @@ class Paragraph extends AbstractStyle
|
||||||
*/
|
*/
|
||||||
protected $aliases = array('line-height' => 'lineHeight');
|
protected $aliases = array('line-height' => 'lineHeight');
|
||||||
|
|
||||||
/**
|
|
||||||
* Paragraph alignment
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $align;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text line height
|
* Text line height
|
||||||
*
|
*
|
||||||
|
|
@ -123,6 +107,21 @@ class Paragraph extends AbstractStyle
|
||||||
*/
|
*/
|
||||||
private $spacing;
|
private $spacing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alignment
|
||||||
|
*
|
||||||
|
* @var \PhpOffice\PhpWord\Style\Alignment
|
||||||
|
*/
|
||||||
|
private $alignment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new instance
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->alignment = new Alignment();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set Style value
|
* Set Style value
|
||||||
*
|
*
|
||||||
|
|
@ -142,28 +141,24 @@ class Paragraph extends AbstractStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Paragraph Alignment
|
* Get alignment
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getAlign()
|
public function getAlign()
|
||||||
{
|
{
|
||||||
return $this->align;
|
return $this->alignment->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set Paragraph Alignment
|
* Set alignment
|
||||||
*
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function setAlign($value = null)
|
public function setAlign($value = null)
|
||||||
{
|
{
|
||||||
if (strtolower($value) == self::ALIGN_JUSTIFY) {
|
$this->alignment->setValue($value);
|
||||||
$value = self::ALIGN_BOTH;
|
|
||||||
}
|
|
||||||
$enum = array(self::ALIGN_LEFT, self::ALIGN_RIGHT, self::ALIGN_CENTER, self::ALIGN_BOTH, self::ALIGN_JUSTIFY);
|
|
||||||
$this->align = $this->setEnumVal($value, $enum, $this->align);
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,13 @@ namespace PhpOffice\PhpWord\Style;
|
||||||
*/
|
*/
|
||||||
class Table extends Border
|
class Table extends Border
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @const string Table width units http://www.schemacentral.com/sc/ooxml/t-w_ST_TblWidth.html
|
||||||
|
*/
|
||||||
|
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)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Style for first row
|
* Style for first row
|
||||||
*
|
*
|
||||||
|
|
@ -92,15 +99,31 @@ class Table extends Border
|
||||||
*/
|
*/
|
||||||
private $shading;
|
private $shading;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \PhpOffice\PhpWord\Style\Alignment Alignment
|
||||||
|
*/
|
||||||
|
private $alignment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int|float Width value
|
||||||
|
*/
|
||||||
|
private $width = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string Width unit
|
||||||
|
*/
|
||||||
|
private $unit = self::WIDTH_AUTO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new table style
|
* Create new table style
|
||||||
*
|
*
|
||||||
* @param mixed $styleTable
|
* @param mixed $tableStyle
|
||||||
* @param mixed $styleFirstRow
|
* @param mixed $firstRowStyle
|
||||||
*/
|
*/
|
||||||
public function __construct($styleTable = null, $styleFirstRow = null)
|
public function __construct($tableStyle = null, $firstRowStyle = null)
|
||||||
{
|
{
|
||||||
if (!is_null($styleFirstRow) && is_array($styleFirstRow)) {
|
$this->alignment = new Alignment();
|
||||||
|
if (!is_null($firstRowStyle) && is_array($firstRowStyle)) {
|
||||||
$this->firstRow = clone $this;
|
$this->firstRow = clone $this;
|
||||||
|
|
||||||
unset($this->firstRow->firstRow);
|
unset($this->firstRow->firstRow);
|
||||||
|
|
@ -112,11 +135,11 @@ class Table extends Border
|
||||||
unset($this->firstRow->borderInsideVSize);
|
unset($this->firstRow->borderInsideVSize);
|
||||||
unset($this->firstRow->borderInsideHColor);
|
unset($this->firstRow->borderInsideHColor);
|
||||||
unset($this->firstRow->borderInsideHSize);
|
unset($this->firstRow->borderInsideHSize);
|
||||||
$this->firstRow->setStyleByArray($styleFirstRow);
|
$this->firstRow->setStyleByArray($firstRowStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_null($styleTable) && is_array($styleTable)) {
|
if (!is_null($tableStyle) && is_array($tableStyle)) {
|
||||||
$this->setStyleByArray($styleTable);
|
$this->setStyleByArray($tableStyle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -408,6 +431,24 @@ class Table extends Border
|
||||||
return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom);
|
return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has margins?
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasMargins()
|
||||||
|
{
|
||||||
|
$hasMargins = false;
|
||||||
|
$margins = $this->getCellMargin();
|
||||||
|
for ($i = 0; $i < count($margins); $i++) {
|
||||||
|
if (!is_null($margins[$i])) {
|
||||||
|
$hasMargins = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hasMargins;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get shading
|
* Get shading
|
||||||
*
|
*
|
||||||
|
|
@ -432,20 +473,72 @@ class Table extends Border
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Has margins?
|
* Get alignment
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function hasMargins()
|
public function getAlign()
|
||||||
{
|
{
|
||||||
$hasMargins = false;
|
return $this->alignment->getValue();
|
||||||
$margins = $this->getCellMargin();
|
|
||||||
for ($i = 0; $i < count($margins); $i++) {
|
|
||||||
if (!is_null($margins[$i])) {
|
|
||||||
$hasMargins = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $hasMargins;
|
/**
|
||||||
|
* Set alignment
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setAlign($value = null)
|
||||||
|
{
|
||||||
|
$this->alignment->setValue($value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get width
|
||||||
|
*
|
||||||
|
* @return int|float
|
||||||
|
*/
|
||||||
|
public function getWidth()
|
||||||
|
{
|
||||||
|
return $this->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set width
|
||||||
|
*
|
||||||
|
* @param int|float $value
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setWidth($value = null)
|
||||||
|
{
|
||||||
|
$this->width = $this->setNumericVal($value, $this->width);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get width unit
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUnit()
|
||||||
|
{
|
||||||
|
return $this->unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set width unit
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setUnit($value = null)
|
||||||
|
{
|
||||||
|
$enum = array(self::WIDTH_AUTO, self::WIDTH_PERCENT, self::WIDTH_TWIP);
|
||||||
|
$this->unit = $this->setEnumVal($value, $enum, $this->unit);
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,6 @@ class Text extends AbstractElement
|
||||||
$content .= String::toUnicode($this->element->getText());
|
$content .= String::toUnicode($this->element->getText());
|
||||||
$content .= '}';
|
$content .= '}';
|
||||||
|
|
||||||
// Remarked to test using closure {} to avoid closing tags
|
|
||||||
// @since 0.11.0
|
|
||||||
// $content .= $this->writeFontStyleClosing($fontStyle);
|
|
||||||
|
|
||||||
if (!$this->withoutP) {
|
if (!$this->withoutP) {
|
||||||
$content .= '\par' . PHP_EOL;
|
$content .= '\par' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
@ -136,24 +132,6 @@ class Text extends AbstractElement
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Write font style ending
|
|
||||||
*
|
|
||||||
* @param \PhpOffice\PhpWord\Style\Font $style
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function writeFontStyleClosing($style)
|
|
||||||
{
|
|
||||||
if (!$style instanceof FontStyle) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$styleWriter = new FontStyleWriter($style);
|
|
||||||
$content = $styleWriter->writeClosing();
|
|
||||||
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get font style
|
* Get font style
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Shared\String;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TextBreak element RTF writer
|
* TextBreak element RTF writer
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -66,35 +66,6 @@ class Font extends AbstractStyle
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Write end style
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function writeClosing()
|
|
||||||
{
|
|
||||||
$style = $this->getStyle();
|
|
||||||
if (!$style instanceof \PhpOffice\PhpWord\Style\Font) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$content = '';
|
|
||||||
$content .= '\cf0';
|
|
||||||
$content .= '\f0';
|
|
||||||
|
|
||||||
$size = $style->getSize();
|
|
||||||
$content .= $this->getValueIf(is_numeric($size), '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2));
|
|
||||||
|
|
||||||
$content .= $this->getValueIf($style->isBold(), '\b0');
|
|
||||||
$content .= $this->getValueIf($style->isItalic(), '\i0');
|
|
||||||
$content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul0');
|
|
||||||
$content .= $this->getValueIf($style->isStrikethrough(), '\strike0');
|
|
||||||
$content .= $this->getValueIf($style->isSuperScript(), '\super0');
|
|
||||||
$content .= $this->getValueIf($style->isSubScript(), '\sub0');
|
|
||||||
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set font name index
|
* Set font name index
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Writer\RTF\Style;
|
namespace PhpOffice\PhpWord\Writer\RTF\Style;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Style\Alignment;
|
||||||
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -39,10 +40,10 @@ class Paragraph extends AbstractStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
$alignments = array(
|
$alignments = array(
|
||||||
ParagraphStyle::ALIGN_LEFT => '\ql',
|
Alignment::ALIGN_LEFT => '\ql',
|
||||||
ParagraphStyle::ALIGN_RIGHT => '\qr',
|
Alignment::ALIGN_RIGHT => '\qr',
|
||||||
ParagraphStyle::ALIGN_CENTER => '\qc',
|
Alignment::ALIGN_CENTER => '\qc',
|
||||||
ParagraphStyle::ALIGN_BOTH => '\qj',
|
Alignment::ALIGN_BOTH => '\qj',
|
||||||
);
|
);
|
||||||
|
|
||||||
$align = $style->getAlign();
|
$align = $style->getAlign();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?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.
|
||||||
|
*
|
||||||
|
* @link https://github.com/PHPOffice/PHPWord
|
||||||
|
* @copyright 2010-2014 PHPWord contributors
|
||||||
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alignment style writer
|
||||||
|
*
|
||||||
|
* @since 0.11.0
|
||||||
|
*/
|
||||||
|
class Alignment extends AbstractStyle
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Write style
|
||||||
|
*/
|
||||||
|
public function write()
|
||||||
|
{
|
||||||
|
$style = $this->getStyle();
|
||||||
|
if (!$style instanceof \PhpOffice\PhpWord\Style\Alignment) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$value = $style->getValue();
|
||||||
|
if ($value !== null) {
|
||||||
|
$xmlWriter = $this->getXmlWriter();
|
||||||
|
$xmlWriter->startElement('w:jc');
|
||||||
|
$xmlWriter->writeAttribute('w:val', $value);
|
||||||
|
$xmlWriter->endElement(); // w:jc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paragraph style writer
|
* Paragraph style writer
|
||||||
*
|
*
|
||||||
|
|
@ -81,8 +83,8 @@ class Paragraph extends AbstractStyle
|
||||||
$xmlWriter->writeElementIf(!is_null($styleName), 'w:pStyle', 'w:val', $styleName);
|
$xmlWriter->writeElementIf(!is_null($styleName), 'w:pStyle', 'w:val', $styleName);
|
||||||
|
|
||||||
// Alignment
|
// Alignment
|
||||||
$align = $style->getAlign();
|
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign())));
|
||||||
$xmlWriter->writeElementIf(!is_null($align), 'w:jc', 'w:val', $align);
|
$styleWriter->write();
|
||||||
|
|
||||||
// Pagination
|
// Pagination
|
||||||
$xmlWriter->writeElementIf(!$style->hasWidowControl(), 'w:widowControl', 'w:val', '0');
|
$xmlWriter->writeElementIf(!$style->hasWidowControl(), 'w:widowControl', 'w:val', '0');
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Table style writer
|
* Table style writer
|
||||||
*
|
*
|
||||||
|
|
@ -42,10 +44,24 @@ class Table extends AbstractStyle
|
||||||
}
|
}
|
||||||
$xmlWriter = $this->getXmlWriter();
|
$xmlWriter = $this->getXmlWriter();
|
||||||
|
|
||||||
$hasBorders = $style->hasBorders();
|
|
||||||
|
// w:tblPr
|
||||||
$hasMargins = $style->hasMargins();
|
$hasMargins = $style->hasMargins();
|
||||||
if ($hasMargins || $hasBorders) {
|
$hasBorders = $style->hasBorders();
|
||||||
|
$align = $style->getAlign();
|
||||||
|
|
||||||
$xmlWriter->startElement('w:tblPr');
|
$xmlWriter->startElement('w:tblPr');
|
||||||
|
|
||||||
|
$xmlWriter->startElement('w:tblW');
|
||||||
|
$xmlWriter->writeAttribute('w:w', $style->getWidth());
|
||||||
|
$xmlWriter->writeAttribute('w:type', $style->getUnit());
|
||||||
|
$xmlWriter->endElement(); // w:tblW
|
||||||
|
|
||||||
|
// Alignment
|
||||||
|
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $align)));
|
||||||
|
$styleWriter->write();
|
||||||
|
|
||||||
|
// Margins
|
||||||
if ($hasMargins) {
|
if ($hasMargins) {
|
||||||
$mbWriter = new MarginBorder($xmlWriter);
|
$mbWriter = new MarginBorder($xmlWriter);
|
||||||
$mbWriter->setSizes($style->getCellMargin());
|
$mbWriter->setSizes($style->getCellMargin());
|
||||||
|
|
@ -54,6 +70,8 @@ class Table extends AbstractStyle
|
||||||
$mbWriter->write();
|
$mbWriter->write();
|
||||||
$xmlWriter->endElement(); // w:tblCellMar
|
$xmlWriter->endElement(); // w:tblCellMar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Borders
|
||||||
if ($hasBorders) {
|
if ($hasBorders) {
|
||||||
$mbWriter = new MarginBorder($xmlWriter);
|
$mbWriter = new MarginBorder($xmlWriter);
|
||||||
$mbWriter->setSizes($style->getBorderSize());
|
$mbWriter->setSizes($style->getBorderSize());
|
||||||
|
|
@ -63,8 +81,8 @@ class Table extends AbstractStyle
|
||||||
$mbWriter->write();
|
$mbWriter->write();
|
||||||
$xmlWriter->endElement(); // w:tblBorders
|
$xmlWriter->endElement(); // w:tblBorders
|
||||||
}
|
}
|
||||||
|
|
||||||
$xmlWriter->endElement(); // w:tblPr
|
$xmlWriter->endElement(); // w:tblPr
|
||||||
}
|
|
||||||
|
|
||||||
// Only write background color and first row for full style
|
// Only write background color and first row for full style
|
||||||
if ($this->isFullStyle) {
|
if ($this->isFullStyle) {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
|
||||||
public function testEmptyStyles()
|
public function testEmptyStyles()
|
||||||
{
|
{
|
||||||
$styles = array(
|
$styles = array(
|
||||||
'Cell', 'Font', 'Image', 'Indentation', 'LineNumbering',
|
'Alignment', 'Cell', 'Font', 'Image', 'Indentation', 'LineNumbering',
|
||||||
'Paragraph', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', 'TextBox'
|
'Paragraph', 'Section', 'Shading', 'Spacing', 'Tab', 'Table', 'TextBox'
|
||||||
);
|
);
|
||||||
foreach ($styles as $style) {
|
foreach ($styles as $style) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue