Refactor and unit test PHPWord_Style_Font

This commit is contained in:
Ivan Lanin 2014-03-08 23:43:27 +07:00
parent 9042bb553c
commit b39f9daee2
7 changed files with 314 additions and 42 deletions

View File

@ -40,7 +40,8 @@ class PHPWord
{ {
const DEFAULT_FONT_NAME = 'Arial'; const DEFAULT_FONT_NAME = 'Arial';
const DEFAULT_FONT_SIZE = 20; const DEFAULT_FONT_SIZE = 10;
const DEFAULT_FONT_COLOR = '000000';
/** /**
* Document properties * Document properties

View File

@ -80,17 +80,82 @@ class PHPWord_Style_Font
*/ */
private $_paragraphStyle; private $_paragraphStyle;
private $_size; /**
* Font name
*
* @var int|float
*/
private $_name; private $_name;
/**
* Font size
*
* @var int|float
*/
private $_size;
/**
* Bold
*
* @var bool
*/
private $_bold; private $_bold;
/**
* Italics
*
* @var bool
*/
private $_italic; private $_italic;
/**
* Superscript
*
* @var bool
*/
private $_superScript; private $_superScript;
/**
* Subscript
*
* @var bool
*/
private $_subScript; private $_subScript;
/**
* Underline mode
*
* @var string
*/
private $_underline; private $_underline;
/**
* Strikethrough
*
* @var bool
*/
private $_strikethrough; private $_strikethrough;
/**
* Font color
*
* @var string
*/
private $_color; private $_color;
/**
* Foreground/highlight
*
* @var string
*/
private $_fgColor; private $_fgColor;
/**
* New font style
*
* @param string $type Type of font
* @param array $styleParagraph Paragraph styles definition
*/
public function __construct($type = 'text', $styleParagraph = null) public function __construct($type = 'text', $styleParagraph = null)
{ {
$this->_type = $type; $this->_type = $type;
@ -102,7 +167,7 @@ class PHPWord_Style_Font
$this->_subScript = false; $this->_subScript = false;
$this->_underline = PHPWord_Style_Font::UNDERLINE_NONE; $this->_underline = PHPWord_Style_Font::UNDERLINE_NONE;
$this->_strikethrough = false; $this->_strikethrough = false;
$this->_color = '000000'; $this->_color = PHPWord::DEFAULT_FONT_COLOR;
$this->_fgColor = null; $this->_fgColor = null;
if (!is_null($styleParagraph)) { if (!is_null($styleParagraph)) {
@ -119,78 +184,139 @@ class PHPWord_Style_Font
} }
} }
/**
* Set style value
*
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
$method = 'set' . ucwords(substr($key, 1));
if (method_exists($this, $method)) {
$this->$method($value);
}
}
/**
* Get font name
*
* @return bool
*/
public function getName() public function getName()
{ {
return $this->_name; return $this->_name;
} }
public function setStyleValue($key, $value) /**
{ * Set font name
if ($key == '_size') { *
$value *= 2; * @param string $pValue
} * @return PHPWord_Style_Font
$this->$key = $value; */
}
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME) public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
{ {
if ($pValue == '') { if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_NAME; $pValue = PHPWord::DEFAULT_FONT_NAME;
} }
$this->_name = $pValue; $this->_name = $pValue;
return $this; return $this;
} }
/**
* Get font size
*
* @return int|float
*/
public function getSize() public function getSize()
{ {
return $this->_size; return $this->_size;
} }
/**
* Set font size
*
* @param int|float $pValue
* @return PHPWord_Style_Font
*/
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE) public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
{ {
if ($pValue == '') { if (!is_numeric($pValue)) {
$pValue = PHPWord::DEFAULT_FONT_SIZE; $pValue = PHPWord::DEFAULT_FONT_SIZE;
} }
$this->_size = ($pValue * 2); $this->_size = $pValue;
return $this; return $this;
} }
/**
* Get bold
*
* @return bool
*/
public function getBold() public function getBold()
{ {
return $this->_bold; return $this->_bold;
} }
/**
* Set bold
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setBold($pValue = false) public function setBold($pValue = false)
{ {
if ($pValue == '') { if (!is_bool($pValue)) {
$pValue = false; $pValue = false;
} }
$this->_bold = $pValue; $this->_bold = $pValue;
return $this; return $this;
} }
/**
* Get italics
*
* @return bool
*/
public function getItalic() public function getItalic()
{ {
return $this->_italic; return $this->_italic;
} }
/**
* Set italics
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setItalic($pValue = false) public function setItalic($pValue = false)
{ {
if ($pValue == '') { if (!is_bool($pValue)) {
$pValue = false; $pValue = false;
} }
$this->_italic = $pValue; $this->_italic = $pValue;
return $this; return $this;
} }
/**
* Get superscript
*
* @return bool
*/
public function getSuperScript() public function getSuperScript()
{ {
return $this->_superScript; return $this->_superScript;
} }
/**
* Set superscript
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSuperScript($pValue = false) public function setSuperScript($pValue = false)
{ {
if ($pValue == '') { if (!is_bool($pValue)) {
$pValue = false; $pValue = false;
} }
$this->_superScript = $pValue; $this->_superScript = $pValue;
@ -198,14 +324,25 @@ class PHPWord_Style_Font
return $this; return $this;
} }
/**
* Get superscript
*
* @return bool
*/
public function getSubScript() public function getSubScript()
{ {
return $this->_subScript; return $this->_subScript;
} }
/**
* Set subscript
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSubScript($pValue = false) public function setSubScript($pValue = false)
{ {
if ($pValue == '') { if (!is_bool($pValue)) {
$pValue = false; $pValue = false;
} }
$this->_subScript = $pValue; $this->_subScript = $pValue;
@ -213,11 +350,22 @@ class PHPWord_Style_Font
return $this; return $this;
} }
/**
* Get underline
*
* @return string
*/
public function getUnderline() public function getUnderline()
{ {
return $this->_underline; return $this->_underline;
} }
/**
* Set underline
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE) public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
{ {
if ($pValue == '') { if ($pValue == '') {
@ -227,49 +375,90 @@ class PHPWord_Style_Font
return $this; return $this;
} }
/**
* Get strikethrough
*
* @return bool
*/
public function getStrikethrough() public function getStrikethrough()
{ {
return $this->_strikethrough; return $this->_strikethrough;
} }
/**
* Set strikethrough
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setStrikethrough($pValue = false) public function setStrikethrough($pValue = false)
{ {
if ($pValue == '') { if (!is_bool($pValue)) {
$pValue = false; $pValue = false;
} }
$this->_strikethrough = $pValue; $this->_strikethrough = $pValue;
return $this; return $this;
} }
/**
* Get font color
*
* @return string
*/
public function getColor() public function getColor()
{ {
return $this->_color; return $this->_color;
} }
public function setColor($pValue = '000000') /**
* Set font color
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR)
{ {
if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_COLOR;
}
$this->_color = $pValue; $this->_color = $pValue;
return $this; return $this;
} }
/**
* Get foreground/highlight color
*
* @return bool
*/
public function getFgColor() public function getFgColor()
{ {
return $this->_fgColor; return $this->_fgColor;
} }
/**
* Set foreground/highlight color
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setFgColor($pValue = null) public function setFgColor($pValue = null)
{ {
$this->_fgColor = $pValue; $this->_fgColor = $pValue;
return $this; return $this;
} }
/**
* Get style type
*
* @return string
*/
public function getStyleType() public function getStyleType()
{ {
return $this->_type; return $this->_type;
} }
/** /**
* Get Paragraph style * Get paragraph style
* *
* @return PHPWord_Style_Paragraph * @return PHPWord_Style_Paragraph
*/ */

View File

@ -133,16 +133,16 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
$objWriter->startElement('style:text-properties'); $objWriter->startElement('style:text-properties');
$objWriter->writeAttribute('style:use-window-font-color', 'true'); $objWriter->writeAttribute('style:use-window-font-color', 'true');
$objWriter->writeAttribute('style:font-name', PHPWord::DEFAULT_FONT_NAME); $objWriter->writeAttribute('style:font-name', PHPWord::DEFAULT_FONT_NAME);
$objWriter->writeAttribute('fo:font-size', '10pt'); $objWriter->writeAttribute('fo:font-size', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('fo:language', 'fr'); $objWriter->writeAttribute('fo:language', 'fr');
$objWriter->writeAttribute('fo:country', 'FR'); $objWriter->writeAttribute('fo:country', 'FR');
$objWriter->writeAttribute('style:letter-kerning', 'true'); $objWriter->writeAttribute('style:letter-kerning', 'true');
$objWriter->writeAttribute('style:font-name-asian', 'Arial2'); $objWriter->writeAttribute('style:font-name-asian', PHPWord::DEFAULT_FONT_NAME . '2');
$objWriter->writeAttribute('style:font-size-asian', '10pt'); $objWriter->writeAttribute('style:font-size-asian', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('style:language-asian', 'zh'); $objWriter->writeAttribute('style:language-asian', 'zh');
$objWriter->writeAttribute('style:country-asian', 'CN'); $objWriter->writeAttribute('style:country-asian', 'CN');
$objWriter->writeAttribute('style:font-name-complex', 'Arial2'); $objWriter->writeAttribute('style:font-name-complex', PHPWord::DEFAULT_FONT_NAME . '2');
$objWriter->writeAttribute('style:font-size-complex', '10pt'); $objWriter->writeAttribute('style:font-size-complex', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('style:language-complex', 'hi'); $objWriter->writeAttribute('style:language-complex', 'hi');
$objWriter->writeAttribute('style:country-complex', 'IN'); $objWriter->writeAttribute('style:country-complex', 'IN');
$objWriter->writeAttribute('fo:hyphenate', 'false'); $objWriter->writeAttribute('fo:hyphenate', 'false');
@ -168,9 +168,9 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
// style:text-properties // style:text-properties
$objWriter->startElement('style:text-properties'); $objWriter->startElement('style:text-properties');
$objWriter->writeAttribute('fo:font-size', ($style->getSize() / 2) . 'pt'); $objWriter->writeAttribute('fo:font-size', ($style->getSize()) . 'pt');
$objWriter->writeAttribute('style:font-size-asian', ($style->getSize() / 2) . 'pt'); $objWriter->writeAttribute('style:font-size-asian', ($style->getSize()) . 'pt');
$objWriter->writeAttribute('style:font-size-complex', ($style->getSize() / 2) . 'pt'); $objWriter->writeAttribute('style:font-size-complex', ($style->getSize()) . 'pt');
if ($style->getItalic()) { if ($style->getItalic()) {
$objWriter->writeAttribute('fo:font-style', 'italic'); $objWriter->writeAttribute('fo:font-style', 'italic');
$objWriter->writeAttribute('style:font-style-asian', 'italic'); $objWriter->writeAttribute('style:font-style-asian', 'italic');

View File

@ -176,7 +176,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
// Point size (in half-points) above which to kern character pairs // Point size (in half-points) above which to kern character pairs
$sRTFContent .= '\kerning1'; $sRTFContent .= '\kerning1';
// Set the font size in half-points // Set the font size in half-points
$sRTFContent .= '\fs20'; $sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
// Body // Body
$sRTFContent .= $this->_getDataContent(); $sRTFContent .= $this->_getDataContent();
@ -252,10 +252,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
if ($style instanceof PHPWord_Style_Font) { if ($style instanceof PHPWord_Style_Font) {
$color = $style->getColor(); $color = $style->getColor();
$fgcolor = $style->getFgColor(); $fgcolor = $style->getFgColor();
if (in_array($color, $arrColors) == FALSE && $color != '000000' && !empty($color)) { if (in_array($color, $arrColors) == FALSE && $color != PHPWord::DEFAULT_FONT_COLOR && !empty($color)) {
$arrColors[] = $color; $arrColors[] = $color;
} }
if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != '000000' && !empty($fgcolor)) { if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != PHPWord::DEFAULT_FONT_COLOR && !empty($fgcolor)) {
$arrColors[] = $fgcolor; $arrColors[] = $fgcolor;
} }
} }
@ -400,7 +400,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFText .= '\i'; $sRTFText .= '\i';
} }
if ($styleFont->getSize()) { if ($styleFont->getSize()) {
$sRTFText .= '\fs' . $styleFont->getSize(); $sRTFText .= '\fs' . ($styleFont->getSize() * 2);
} }
} }
if ($this->_lastParagraphStyle != '' || $styleFont) { if ($this->_lastParagraphStyle != '' || $styleFont) {
@ -419,7 +419,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFText .= '\i0'; $sRTFText .= '\i0';
} }
if ($styleFont->getSize()) { if ($styleFont->getSize()) {
$sRTFText .= '\fs20'; $sRTFText .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
} }
} }

View File

@ -373,7 +373,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$color = $style->getColor(); $color = $style->getColor();
$size = $style->getSize(); $size = $style->getSize();
$fgColor = $style->getFgColor(); $fgColor = $style->getFgColor();
$striketrough = $style->getStrikethrough(); $strikethrough = $style->getStrikethrough();
$underline = $style->getUnderline(); $underline = $style->getUnderline();
$superscript = $style->getSuperScript(); $superscript = $style->getSuperScript();
$subscript = $style->getSubScript(); $subscript = $style->getSubScript();
@ -390,7 +390,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
} }
// Color // Color
if ($color != '000000') { if ($color != PHPWord::DEFAULT_FONT_COLOR) {
$objWriter->startElement('w:color'); $objWriter->startElement('w:color');
$objWriter->writeAttribute('w:val', $color); $objWriter->writeAttribute('w:val', $color);
$objWriter->endElement(); $objWriter->endElement();
@ -399,10 +399,10 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
// Size // Size
if ($size != PHPWord::DEFAULT_FONT_SIZE) { if ($size != PHPWord::DEFAULT_FONT_SIZE) {
$objWriter->startElement('w:sz'); $objWriter->startElement('w:sz');
$objWriter->writeAttribute('w:val', $size); $objWriter->writeAttribute('w:val', $size * 2);
$objWriter->endElement(); $objWriter->endElement();
$objWriter->startElement('w:szCs'); $objWriter->startElement('w:szCs');
$objWriter->writeAttribute('w:val', $size); $objWriter->writeAttribute('w:val', $size * 2);
$objWriter->endElement(); $objWriter->endElement();
} }
@ -424,8 +424,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement(); $objWriter->endElement();
} }
// Striketrough // Strikethrough
if ($striketrough) { if ($strikethrough) {
$objWriter->writeElement('w:strike', null); $objWriter->writeElement('w:strike', null);
} }

View File

@ -380,11 +380,11 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
$objWriter->endElement(); $objWriter->endElement();
$objWriter->startElement('w:sz'); $objWriter->startElement('w:sz');
$objWriter->writeAttribute('w:val', $fontSize); $objWriter->writeAttribute('w:val', $fontSize * 2);
$objWriter->endElement(); $objWriter->endElement();
$objWriter->startElement('w:szCs'); $objWriter->startElement('w:szCs');
$objWriter->writeAttribute('w:val', $fontSize); $objWriter->writeAttribute('w:val', $fontSize * 2);
$objWriter->endElement(); $objWriter->endElement();
$objWriter->endElement(); $objWriter->endElement();

View File

@ -0,0 +1,82 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord;
use PHPWord_Style_Font;
/**
* Class PHPWord_Style_FontTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase
{
/**
* Test initiation for style type and paragraph style
*/
public function testInitiation()
{
$object = new PHPWord_Style_Font('text', array('align' => 'both'));
$this->assertEquals('text', $object->getStyleType());
$this->assertInstanceOf('PHPWord_Style_Paragraph', $object->getParagraphStyle());
}
/**
* Test setting style values with null or empty value
*/
public function testSetStyleValueWithNullOrEmpty()
{
$object = new PHPWord_Style_Font();
$attributes = array(
'name' => PHPWord::DEFAULT_FONT_NAME,
'size' => PHPWord::DEFAULT_FONT_SIZE,
'bold' => false,
'italic' => false,
'superScript' => false,
'subScript' => false,
'underline' => PHPWord_Style_Font::UNDERLINE_NONE,
'strikethrough' => false,
'color' => PHPWord::DEFAULT_FONT_COLOR,
'fgColor' => null,
);
foreach ($attributes as $key => $default) {
$method = 'get' . ucwords($key);
$object->setStyleValue("_$key", null);
$this->assertEquals($default, $object->$method());
$object->setStyleValue("_$key", '');
$this->assertEquals($default, $object->$method());
}
}
/**
* Test setting style values with normal value
*/
public function testSetStyleValueNormal()
{
$object = new PHPWord_Style_Font();
$attributes = array(
'name' => 'Times New Roman',
'size' => 9,
'bold' => true,
'italic' => true,
'superScript' => true,
'subScript' => true,
'underline' => PHPWord_Style_Font::UNDERLINE_HEAVY,
'strikethrough' => true,
'color' => '999999',
'fgColor' => '999999',
);
foreach ($attributes as $key => $value) {
$method = 'get' . ucwords($key);
$object->setStyleValue("_$key", $value);
$this->assertEquals($value, $object->$method());
}
}
}