Style: Remove `bgColor` from `Font`, `Table`, and `Cell` and put it into the new `Shading` style

This commit is contained in:
Ivan Lanin 2014-05-02 09:45:16 +07:00
parent 9afa46105e
commit 999a9c5037
14 changed files with 429 additions and 222 deletions

View File

@ -42,6 +42,7 @@ This release marked heavy refactorings on internal code structure with the creat
- Section: Ability to define gutter and line numbering - @ivanlanin
- Font: Small caps, all caps, and double strikethrough - @ivanlanin GH-151
- Settings: Ability to use measurement unit other than twips with `setMeasurementUnit` - @ivanlanin GH-199
- Style: Remove `bgColor` from `Font`, `Table`, and `Cell` and put it into the new `Shading` style - @ivanlanin
### Bugfixes

View File

@ -8,7 +8,7 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord();
// Ads styles
$phpWord->addParagraphStyle('pStyle', array('spacing'=>100));
$phpWord->addFontStyle('BoldText', array('bold'=>true));
$phpWord->addFontStyle('ColoredText', array('color'=>'FF8080'));
$phpWord->addFontStyle('ColoredText', array('color'=>'FF8080', 'bgColor' => 'FFFFCC'));
$phpWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
// New portrait section

View File

@ -55,7 +55,7 @@ $section->addTextBreak(1);
$section->addText("Table with colspan and rowspan", $header);
$styleTable = array('borderSize' => 6, 'borderColor' => '999999');
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center');
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center', 'bgColor' => 'FFFF00');
$cellRowContinue = array('vMerge' => 'continue');
$cellColSpan = array('gridSpan' => 2, 'valign' => 'center');
$cellHCentered = array('align' => 'center');

View File

@ -9,7 +9,7 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Shading;
/**
* Table cell style
@ -33,13 +33,6 @@ class Cell extends Border
*/
private $textDirection;
/**
* Background-Color
*
* @var string
*/
private $bgColor;
/**
* Border Default Color
*
@ -64,6 +57,13 @@ class Cell extends Border
*/
private $vMerge = null;
/**
* Shading
*
* @var \PhpOffice\PhpWord\Style\Shading
*/
private $shading;
/**
* Create a new Cell Style
*/
@ -71,7 +71,6 @@ class Cell extends Border
{
$this->valign = null;
$this->textDirection = null;
$this->bgColor = null;
$this->borderTopSize = null;
$this->borderTopColor = null;
$this->borderLeftSize = null;
@ -83,24 +82,6 @@ class Cell extends Border
$this->defaultBorderColor = '000000';
}
/**
* Set style value
*
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
$key = String::removeUnderscorePrefix($key);
if ($key == 'borderSize') {
$this->setBorderSize($value);
} elseif ($key == 'borderColor') {
$this->setBorderColor($value);
} else {
$this->$key = $value;
}
}
/**
* Get vertical align
*/
@ -138,21 +119,26 @@ class Cell extends Border
}
/**
* Get background color
* Get background
*
* @return string
*/
public function getBgColor()
{
return $this->bgColor;
if (!is_null($this->shading)) {
return $this->shading->getFill();
}
}
/**
* Set background color
* Set background
*
* @param string $pValue
* @param string $value
* @return \PhpOffice\PhpWord\Style\Table
*/
public function setBgColor($pValue = null)
public function setBgColor($value = null)
{
$this->bgColor = $pValue;
$this->setShading(array('fill' => $value));
}
/**
@ -198,4 +184,34 @@ class Cell extends Border
{
return $this->vMerge;
}
/**
* Get shading
*
* @return \PhpOffice\PhpWord\Style\Shading
*/
public function getShading()
{
return $this->shading;
}
/**
* Set shading
*
* @param array $value
* @return self
*/
public function setShading($value = null)
{
if (is_array($value)) {
if (!$this->shading instanceof Shading) {
$this->shading = new Shading();
}
$this->shading->setStyleByArray($value);
} else {
$this->shading = null;
}
return $this;
}
}

View File

@ -11,6 +11,7 @@ namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\Style\Shading;
/**
* Font style
@ -153,12 +154,6 @@ class Font extends AbstractStyle
*/
private $fgColor = null;
/**
* Background color
*
* @var string
*/
private $bgColor = null;
/**
* Text line height
*
@ -195,6 +190,13 @@ class Font extends AbstractStyle
*/
private $allCaps = false;
/**
* Shading
*
* @var \PhpOffice\PhpWord\Style\Shading
*/
private $shading;
/**
* Create new font style
*
@ -501,26 +503,26 @@ class Font extends AbstractStyle
}
/**
* Get background color
* Get background
*
* @return string
* @return string
*/
public function getBgColor()
{
return $this->bgColor;
if (!is_null($this->shading)) {
return $this->shading->getFill();
}
}
/**
* Set background color
* Set background
*
* @param string $value
* @return $this
* @return \PhpOffice\PhpWord\Style\Table
*/
public function setBgColor($value = null)
{
$this->bgColor = $value;
return $this;
$this->setShading(array('fill' => $value));
}
/**
@ -649,4 +651,34 @@ class Font extends AbstractStyle
return $this;
}
/**
* Get shading
*
* @return \PhpOffice\PhpWord\Style\Shading
*/
public function getShading()
{
return $this->shading;
}
/**
* Set shading
*
* @param array $value
* @return self
*/
public function setShading($value = null)
{
if (is_array($value)) {
if (!$this->shading instanceof Shading) {
$this->shading = new Shading();
}
$this->shading->setStyleByArray($value);
} else {
$this->shading = null;
}
return $this;
}
}

View File

@ -142,7 +142,7 @@ class LineNumbering extends AbstractStyle
/**
* Set distance
*
* @param int|float $value
* @param string $value
* @return self
*/
public function setRestart($value = null)

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\LineNumbering;
/**
* Section settings
*/
@ -146,7 +148,7 @@ class Section extends Border
/**
* Line numbering
*
* @var array
* @var \PhpOffice\PhpWord\Style\LineNumbering
* @link http://www.schemacentral.com/sc/ooxml/e-w_lnNumType-1.html
*/
private $lineNumbering;
@ -491,7 +493,7 @@ class Section extends Border
/**
* Get line numbering
*
* @return self
* @return \PhpOffice\PhpWord\Style\LineNumbering
*/
public function getLineNumbering()
{
@ -506,20 +508,15 @@ class Section extends Border
*/
public function setLineNumbering($value = null)
{
if ($this->lineNumbering instanceof LineNumbering) {
if (is_array($value)) {
if (!$this->lineNumbering instanceof LineNumbering) {
$this->lineNumbering = new LineNumbering($value);
}
$this->lineNumbering->setStyleByArray($value);
} else {
$this->lineNumbering = new LineNumbering($value);
$this->lineNumbering = null;
}
return $this;
}
/**
* Remove line numbering
*/
public function removeLineNumbering()
{
$this->lineNumbering = null;
}
}

View File

@ -0,0 +1,120 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Style;
/**
* Shading style
*
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_Shd.html
* @since 0.10.0
*/
class Shading extends AbstractStyle
{
/**
* Shading pattern
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/t-w_ST_Shd.html
*/
private $pattern = 'clear';
/**
* Shading pattern color
*
* @var string
*/
private $color = 'auto';
/**
* Shading background color
*
* @var string
*/
private $fill;
/**
* Create a new instance
*
* @param array $style
*/
public function __construct($style = array())
{
$this->setStyleByArray($style);
}
/**
* Get pattern
*
* @return string
*/
public function getPattern()
{
return $this->pattern;
}
/**
* Set pattern
*
* @param string $value
* @return self
*/
public function setPattern($value = null)
{
$this->pattern = $value;
return $this;
}
/**
* Get color
*
* @return string
*/
public function getColor()
{
return $this->color;
}
/**
* Set pattern
*
* @param string $value
* @return self
*/
public function setColor($value = null)
{
$this->color = $value;
return $this;
}
/**
* Get fill
*
* @return string
*/
public function getFill()
{
return $this->fill;
}
/**
* Set fill
*
* @param string $value
* @return self
*/
public function setFill($value = null)
{
$this->fill = $value;
return $this;
}
}

View File

@ -9,7 +9,7 @@
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\String;
use PhpOffice\PhpWord\Style\Shading;
/**
* Table style
@ -51,13 +51,6 @@ class Table extends Border
*/
private $cellMarginBottom = null;
/**
* Background color
*
* @var string
*/
private $bgColor;
/**
* Border size inside horizontal
*
@ -86,6 +79,13 @@ class Table extends Border
*/
private $borderInsideVColor;
/**
* Shading
*
* @var \PhpOffice\PhpWord\Style\Shading
*/
private $shading;
/**
* Create new table style
*
@ -118,26 +118,6 @@ class Table extends Border
}
}
/**
* Set style value
*
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
$key = String::removeUnderscorePrefix($key);
if ($key == 'borderSize') {
$this->setBorderSize($value);
} elseif ($key == 'borderColor') {
$this->setBorderColor($value);
} elseif ($key == 'cellMargin') {
$this->setCellMargin($value);
} else {
$this->$key = $value;
}
}
/**
* Get First Row Style
*
@ -155,131 +135,100 @@ class Table extends Border
*/
public function getBgColor()
{
return $this->bgColor;
if (!is_null($this->shading)) {
return $this->shading->getFill();
}
}
/**
* Set background
*
* @param string $pValue
* @param string $value
* @return \PhpOffice\PhpWord\Style\Table
*/
public function setBgColor($pValue = null)
public function setBgColor($value = null)
{
$this->bgColor = $pValue;
$this->setShading(array('fill' => $value));
}
/**
* Set TLRBVH Border Size
* Set TLRBHV Border Size
*
* @param int $pValue Border size in eighths of a point (1/8 point)
* @param int $value Border size in eighths of a point (1/8 point)
* @return self
*/
public function setBorderSize($pValue = null)
public function setBorderSize($value = null)
{
$this->borderTopSize = $pValue;
$this->borderLeftSize = $pValue;
$this->borderRightSize = $pValue;
$this->borderBottomSize = $pValue;
$this->borderInsideHSize = $pValue;
$this->borderInsideVSize = $pValue;
$this->setBorderTopSize($value);
$this->setBorderLeftSize($value);
$this->setBorderRightSize($value);
$this->setBorderBottomSize($value);
$this->setBorderInsideHSize($value);
$this->setBorderInsideVSize($value);
return $this;
}
/**
* Get TLRBVH Border Size
* Get TLRBHV Border Size
*
* @return int[]
*/
public function getBorderSize()
{
$top = $this->getBorderTopSize();
$left = $this->getBorderLeftSize();
$right = $this->getBorderRightSize();
$bottom = $this->getBorderBottomSize();
$insideH = $this->getBorderInsideHSize();
$insideV = $this->getBorderInsideVSize();
return array($top, $left, $right, $bottom, $insideH, $insideV);
return array(
$this->getBorderTopSize(),
$this->getBorderLeftSize(),
$this->getBorderRightSize(),
$this->getBorderBottomSize(),
$this->getBorderInsideHSize(),
$this->getBorderInsideVSize(),
);
}
/**
* Set TLRBVH Border Color
* @param string $pValue
* Set TLRBHV Border Color
*
* @param string $value
* @return self
*/
public function setBorderColor($pValue = null)
public function setBorderColor($value = null)
{
$this->borderTopColor = $pValue;
$this->borderLeftColor = $pValue;
$this->borderRightColor = $pValue;
$this->borderBottomColor = $pValue;
$this->borderInsideHColor = $pValue;
$this->borderInsideVColor = $pValue;
$this->setBorderTopColor($value);
$this->setBorderLeftColor($value);
$this->setBorderRightColor($value);
$this->setBorderBottomColor($value);
$this->setBorderInsideHColor($value);
$this->setBorderInsideVColor($value);
return $this;
}
/**
* Get TLRB Border Color
* Get TLRBHV Border Color
*
* @return string[]
*/
public function getBorderColor()
{
$top = $this->getBorderTopColor();
$left = $this->getBorderLeftColor();
$right = $this->getBorderRightColor();
$bottom = $this->getBorderBottomColor();
$insideH = $this->getBorderInsideHColor();
$insideV = $this->getBorderInsideVColor();
return array($top, $left, $right, $bottom, $insideH, $insideV);
}
/**
* Set border color inside horizontal
*
* @param $pValue
*/
public function setBorderInsideHColor($pValue = null)
{
$this->borderInsideHColor = $pValue;
}
/**
* Get border color inside horizontal
*
* @return
*/
public function getBorderInsideHColor()
{
return (isset($this->borderInsideHColor)) ? $this->borderInsideHColor : null;
}
/**
* Set border color inside vertical
*
* @param $pValue
*/
public function setBorderInsideVColor($pValue = null)
{
$this->borderInsideVColor = $pValue;
}
/**
* Get border color inside vertical
*
* @return
*/
public function getBorderInsideVColor()
{
return (isset($this->borderInsideVColor)) ? $this->borderInsideVColor : null;
return array(
$this->getBorderTopColor(),
$this->getBorderLeftColor(),
$this->getBorderRightColor(),
$this->getBorderBottomColor(),
$this->getBorderInsideHColor(),
$this->getBorderInsideVColor(),
);
}
/**
* Set border size inside horizontal
*
* @param $pValue
* @param $value
*/
public function setBorderInsideHSize($pValue = null)
public function setBorderInsideHSize($value = null)
{
$this->borderInsideHSize = $pValue;
$this->borderInsideHSize = $value;
}
/**
@ -295,11 +244,11 @@ class Table extends Border
/**
* Set border size inside vertical
*
* @param $pValue
* @param $value
*/
public function setBorderInsideVSize($pValue = null)
public function setBorderInsideVSize($value = null)
{
$this->borderInsideVSize = $pValue;
$this->borderInsideVSize = $value;
}
/**
@ -312,14 +261,54 @@ class Table extends Border
return (isset($this->borderInsideVSize)) ? $this->borderInsideVSize : null;
}
/**
* Set border color inside horizontal
*
* @param $value
*/
public function setBorderInsideHColor($value = null)
{
$this->borderInsideHColor = $value;
}
/**
* Get border color inside horizontal
*
* @return
*/
public function getBorderInsideHColor()
{
return (isset($this->borderInsideHColor)) ? $this->borderInsideHColor : null;
}
/**
* Set border color inside vertical
*
* @param $value
*/
public function setBorderInsideVColor($value = null)
{
$this->borderInsideVColor = $value;
}
/**
* Get border color inside vertical
*
* @return
*/
public function getBorderInsideVColor()
{
return (isset($this->borderInsideVColor)) ? $this->borderInsideVColor : null;
}
/**
* Set cell margin top
*
* @param int $pValue
* @param int $value
*/
public function setCellMarginTop($pValue = null)
public function setCellMarginTop($value = null)
{
$this->cellMarginTop = $pValue;
$this->cellMarginTop = $value;
}
/**
@ -335,11 +324,11 @@ class Table extends Border
/**
* Set cell margin left
*
* @param int $pValue
* @param int $value
*/
public function setCellMarginLeft($pValue = null)
public function setCellMarginLeft($value = null)
{
$this->cellMarginLeft = $pValue;
$this->cellMarginLeft = $value;
}
/**
@ -355,11 +344,11 @@ class Table extends Border
/**
* Set cell margin right
*
* @param int $pValue
* @param int $value
*/
public function setCellMarginRight($pValue = null)
public function setCellMarginRight($value = null)
{
$this->cellMarginRight = $pValue;
$this->cellMarginRight = $value;
}
/**
@ -375,11 +364,11 @@ class Table extends Border
/**
* Set cell margin bottom
*
* @param int $pValue
* @param int $value
*/
public function setCellMarginBottom($pValue = null)
public function setCellMarginBottom($value = null)
{
$this->cellMarginBottom = $pValue;
$this->cellMarginBottom = $value;
}
/**
@ -395,14 +384,14 @@ class Table extends Border
/**
* Set TLRB cell margin
*
* @param int $pValue Margin in twips
* @param int $value Margin in twips
*/
public function setCellMargin($pValue = null)
public function setCellMargin($value = null)
{
$this->cellMarginTop = $pValue;
$this->cellMarginLeft = $pValue;
$this->cellMarginRight = $pValue;
$this->cellMarginBottom = $pValue;
$this->setCellMarginTop($value);
$this->setCellMarginLeft($value);
$this->setCellMarginRight($value);
$this->setCellMarginBottom($value);
}
/**
@ -414,4 +403,34 @@ class Table extends Border
{
return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom);
}
/**
* Get shading
*
* @return \PhpOffice\PhpWord\Style\Shading
*/
public function getShading()
{
return $this->shading;
}
/**
* Set shading
*
* @param array $value
* @return self
*/
public function setShading($value = null)
{
if (is_array($value)) {
if (!$this->shading instanceof Shading) {
$this->shading = new Shading();
}
$this->shading->setStyleByArray($value);
} else {
$this->shading = null;
}
return $this;
}
}

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Writer\Word2007\Style\Shading;
/**
* Cell style writer
*
@ -48,11 +50,8 @@ class Cell extends AbstractStyle
}
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', 'clear');
$this->xmlWriter->writeAttribute('w:color', 'auto');
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement();
$styleWriter = new Shading($this->xmlWriter, $this->style->getShading());
$styleWriter->write();
}
if (!is_null($valign)) {

View File

@ -54,7 +54,6 @@ class Font extends AbstractStyle
$font = $this->style->getName();
$color = $this->style->getColor();
$size = $this->style->getSize();
$bgColor = $this->style->getBgColor();
$this->xmlWriter->startElement('w:rPr');
@ -125,12 +124,9 @@ class Font extends AbstractStyle
}
// Background-Color
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', "clear");
$this->xmlWriter->writeAttribute('w:color', "auto");
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement();
if (!is_null($this->style->getShading())) {
$styleWriter = new Shading($this->xmlWriter, $this->style->getShading());
$styleWriter->write();
}
// Superscript/subscript

View File

@ -0,0 +1,34 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
/**
* Shading style writer
*
* @since 0.10.0
*/
class Shading extends AbstractStyle
{
/**
* Write style
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Shading)) {
return;
}
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', $this->style->getPattern());
$this->xmlWriter->writeAttribute('w:color', $this->style->getColor());
$this->xmlWriter->writeAttribute('w:fill', $this->style->getFill());
$this->xmlWriter->endElement();
}
}

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Writer\Word2007\Style\Shading;
/**
* Table style writer
*
@ -32,7 +34,6 @@ class Table extends AbstractStyle
return;
}
$bgColor = $this->style->getBgColor();
$brdCol = $this->style->getBorderColor();
$brdSz = $this->style->getBorderSize();
$cellMargin = $this->style->getCellMargin();
@ -76,13 +77,10 @@ class Table extends AbstractStyle
// Only write background color and first row for full style
if ($this->isFullStyle) {
// Background color
if (!is_null($bgColor)) {
if (!is_null($this->style->getShading())) {
$this->xmlWriter->startElement('w:tcPr');
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', 'clear');
$this->xmlWriter->writeAttribute('w:color', 'auto');
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement();
$styleWriter = new Shading($this->xmlWriter, $this->style->getShading());
$styleWriter->write();
$this->xmlWriter->endElement();
}
// First Row
@ -110,17 +108,12 @@ class Table extends AbstractStyle
*/
private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style, $type)
{
$bgColor = $style->getBgColor();
$this->xmlWriter->startElement('w:tblStylePr');
$this->xmlWriter->writeAttribute('w:type', $type);
$this->xmlWriter->startElement('w:tcPr');
if (!is_null($bgColor)) {
$this->xmlWriter->startElement('w:shd');
$this->xmlWriter->writeAttribute('w:val', 'clear');
$this->xmlWriter->writeAttribute('w:color', 'auto');
$this->xmlWriter->writeAttribute('w:fill', $bgColor);
$this->xmlWriter->endElement(); // w:shd
if (!is_null($style->getShading())) {
$styleWriter = new Shading($this->xmlWriter, $style->getShading());
$styleWriter->write();
}
// Borders

View File

@ -58,7 +58,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$oSettings->setSettingValue('lineNumbering', array('start' => 1, 'increment' => 1, 'distance' => 240, 'restart' => 'newPage'));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\LineNumbering', $oSettings->getLineNumbering());
$oSettings->removeLineNumbering();
$oSettings->setSettingValue('lineNumbering', null);
$this->assertNull($oSettings->getLineNumbering());
}