Refactor and unit test PHPWord_Style_Font
This commit is contained in:
parent
9042bb553c
commit
b39f9daee2
|
|
@ -40,7 +40,8 @@ class PHPWord
|
|||
{
|
||||
|
||||
const DEFAULT_FONT_NAME = 'Arial';
|
||||
const DEFAULT_FONT_SIZE = 20;
|
||||
const DEFAULT_FONT_SIZE = 10;
|
||||
const DEFAULT_FONT_COLOR = '000000';
|
||||
|
||||
/**
|
||||
* Document properties
|
||||
|
|
|
|||
|
|
@ -80,17 +80,82 @@ class PHPWord_Style_Font
|
|||
*/
|
||||
private $_paragraphStyle;
|
||||
|
||||
private $_size;
|
||||
/**
|
||||
* Font name
|
||||
*
|
||||
* @var int|float
|
||||
*/
|
||||
private $_name;
|
||||
|
||||
/**
|
||||
* Font size
|
||||
*
|
||||
* @var int|float
|
||||
*/
|
||||
private $_size;
|
||||
|
||||
/**
|
||||
* Bold
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_bold;
|
||||
|
||||
/**
|
||||
* Italics
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_italic;
|
||||
|
||||
/**
|
||||
* Superscript
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_superScript;
|
||||
|
||||
/**
|
||||
* Subscript
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_subScript;
|
||||
|
||||
/**
|
||||
* Underline mode
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_underline;
|
||||
|
||||
/**
|
||||
* Strikethrough
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_strikethrough;
|
||||
|
||||
/**
|
||||
* Font color
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_color;
|
||||
|
||||
/**
|
||||
* Foreground/highlight
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_fgColor;
|
||||
|
||||
/**
|
||||
* New font style
|
||||
*
|
||||
* @param string $type Type of font
|
||||
* @param array $styleParagraph Paragraph styles definition
|
||||
*/
|
||||
public function __construct($type = 'text', $styleParagraph = null)
|
||||
{
|
||||
$this->_type = $type;
|
||||
|
|
@ -102,7 +167,7 @@ class PHPWord_Style_Font
|
|||
$this->_subScript = false;
|
||||
$this->_underline = PHPWord_Style_Font::UNDERLINE_NONE;
|
||||
$this->_strikethrough = false;
|
||||
$this->_color = '000000';
|
||||
$this->_color = PHPWord::DEFAULT_FONT_COLOR;
|
||||
$this->_fgColor = null;
|
||||
|
||||
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()
|
||||
{
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
public function setStyleValue($key, $value)
|
||||
{
|
||||
if ($key == '_size') {
|
||||
$value *= 2;
|
||||
}
|
||||
$this->$key = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set font name
|
||||
*
|
||||
* @param string $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (is_null($pValue) || $pValue == '') {
|
||||
$pValue = PHPWord::DEFAULT_FONT_NAME;
|
||||
}
|
||||
$this->_name = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font size
|
||||
*
|
||||
* @return int|float
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return $this->_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set font size
|
||||
*
|
||||
* @param int|float $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (!is_numeric($pValue)) {
|
||||
$pValue = PHPWord::DEFAULT_FONT_SIZE;
|
||||
}
|
||||
$this->_size = ($pValue * 2);
|
||||
$this->_size = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bold
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getBold()
|
||||
{
|
||||
return $this->_bold;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set bold
|
||||
*
|
||||
* @param bool $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setBold($pValue = false)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (!is_bool($pValue)) {
|
||||
$pValue = false;
|
||||
}
|
||||
$this->_bold = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get italics
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getItalic()
|
||||
{
|
||||
return $this->_italic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set italics
|
||||
*
|
||||
* @param bool $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setItalic($pValue = false)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (!is_bool($pValue)) {
|
||||
$pValue = false;
|
||||
}
|
||||
$this->_italic = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get superscript
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSuperScript()
|
||||
{
|
||||
return $this->_superScript;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set superscript
|
||||
*
|
||||
* @param bool $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setSuperScript($pValue = false)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (!is_bool($pValue)) {
|
||||
$pValue = false;
|
||||
}
|
||||
$this->_superScript = $pValue;
|
||||
|
|
@ -198,14 +324,25 @@ class PHPWord_Style_Font
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get superscript
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSubScript()
|
||||
{
|
||||
return $this->_subScript;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set subscript
|
||||
*
|
||||
* @param bool $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setSubScript($pValue = false)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (!is_bool($pValue)) {
|
||||
$pValue = false;
|
||||
}
|
||||
$this->_subScript = $pValue;
|
||||
|
|
@ -213,11 +350,22 @@ class PHPWord_Style_Font
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get underline
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUnderline()
|
||||
{
|
||||
return $this->_underline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set underline
|
||||
*
|
||||
* @param string $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
|
|
@ -227,49 +375,90 @@ class PHPWord_Style_Font
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get strikethrough
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getStrikethrough()
|
||||
{
|
||||
return $this->_strikethrough;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set strikethrough
|
||||
*
|
||||
* @param bool $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setStrikethrough($pValue = false)
|
||||
{
|
||||
if ($pValue == '') {
|
||||
if (!is_bool($pValue)) {
|
||||
$pValue = false;
|
||||
}
|
||||
$this->_strikethrough = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font color
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getColor()
|
||||
{
|
||||
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;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get foreground/highlight color
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getFgColor()
|
||||
{
|
||||
return $this->_fgColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set foreground/highlight color
|
||||
*
|
||||
* @param string $pValue
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setFgColor($pValue = null)
|
||||
{
|
||||
$this->_fgColor = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get style type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getStyleType()
|
||||
{
|
||||
return $this->_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Paragraph style
|
||||
* Get paragraph style
|
||||
*
|
||||
* @return PHPWord_Style_Paragraph
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -133,16 +133,16 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
|
|||
$objWriter->startElement('style:text-properties');
|
||||
$objWriter->writeAttribute('style:use-window-font-color', 'true');
|
||||
$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:country', 'FR');
|
||||
$objWriter->writeAttribute('style:letter-kerning', 'true');
|
||||
$objWriter->writeAttribute('style:font-name-asian', 'Arial2');
|
||||
$objWriter->writeAttribute('style:font-size-asian', '10pt');
|
||||
$objWriter->writeAttribute('style:font-name-asian', PHPWord::DEFAULT_FONT_NAME . '2');
|
||||
$objWriter->writeAttribute('style:font-size-asian', PHPWord::DEFAULT_FONT_SIZE . 'pt');
|
||||
$objWriter->writeAttribute('style:language-asian', 'zh');
|
||||
$objWriter->writeAttribute('style:country-asian', 'CN');
|
||||
$objWriter->writeAttribute('style:font-name-complex', 'Arial2');
|
||||
$objWriter->writeAttribute('style:font-size-complex', '10pt');
|
||||
$objWriter->writeAttribute('style:font-name-complex', PHPWord::DEFAULT_FONT_NAME . '2');
|
||||
$objWriter->writeAttribute('style:font-size-complex', PHPWord::DEFAULT_FONT_SIZE . 'pt');
|
||||
$objWriter->writeAttribute('style:language-complex', 'hi');
|
||||
$objWriter->writeAttribute('style:country-complex', 'IN');
|
||||
$objWriter->writeAttribute('fo:hyphenate', 'false');
|
||||
|
|
@ -168,9 +168,9 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
|
|||
|
||||
// style:text-properties
|
||||
$objWriter->startElement('style:text-properties');
|
||||
$objWriter->writeAttribute('fo:font-size', ($style->getSize() / 2) . 'pt');
|
||||
$objWriter->writeAttribute('style:font-size-asian', ($style->getSize() / 2) . 'pt');
|
||||
$objWriter->writeAttribute('style:font-size-complex', ($style->getSize() / 2) . 'pt');
|
||||
$objWriter->writeAttribute('fo:font-size', ($style->getSize()) . 'pt');
|
||||
$objWriter->writeAttribute('style:font-size-asian', ($style->getSize()) . 'pt');
|
||||
$objWriter->writeAttribute('style:font-size-complex', ($style->getSize()) . 'pt');
|
||||
if ($style->getItalic()) {
|
||||
$objWriter->writeAttribute('fo:font-style', 'italic');
|
||||
$objWriter->writeAttribute('style:font-style-asian', 'italic');
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
|
|||
// Point size (in half-points) above which to kern character pairs
|
||||
$sRTFContent .= '\kerning1';
|
||||
// Set the font size in half-points
|
||||
$sRTFContent .= '\fs20';
|
||||
$sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
|
||||
// Body
|
||||
$sRTFContent .= $this->_getDataContent();
|
||||
|
||||
|
|
@ -252,10 +252,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
|
|||
if ($style instanceof PHPWord_Style_Font) {
|
||||
$color = $style->getColor();
|
||||
$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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -400,7 +400,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
|
|||
$sRTFText .= '\i';
|
||||
}
|
||||
if ($styleFont->getSize()) {
|
||||
$sRTFText .= '\fs' . $styleFont->getSize();
|
||||
$sRTFText .= '\fs' . ($styleFont->getSize() * 2);
|
||||
}
|
||||
}
|
||||
if ($this->_lastParagraphStyle != '' || $styleFont) {
|
||||
|
|
@ -419,7 +419,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
|
|||
$sRTFText .= '\i0';
|
||||
}
|
||||
if ($styleFont->getSize()) {
|
||||
$sRTFText .= '\fs20';
|
||||
$sRTFText .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -373,7 +373,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||
$color = $style->getColor();
|
||||
$size = $style->getSize();
|
||||
$fgColor = $style->getFgColor();
|
||||
$striketrough = $style->getStrikethrough();
|
||||
$strikethrough = $style->getStrikethrough();
|
||||
$underline = $style->getUnderline();
|
||||
$superscript = $style->getSuperScript();
|
||||
$subscript = $style->getSubScript();
|
||||
|
|
@ -390,7 +390,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||
}
|
||||
|
||||
// Color
|
||||
if ($color != '000000') {
|
||||
if ($color != PHPWord::DEFAULT_FONT_COLOR) {
|
||||
$objWriter->startElement('w:color');
|
||||
$objWriter->writeAttribute('w:val', $color);
|
||||
$objWriter->endElement();
|
||||
|
|
@ -399,10 +399,10 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||
// Size
|
||||
if ($size != PHPWord::DEFAULT_FONT_SIZE) {
|
||||
$objWriter->startElement('w:sz');
|
||||
$objWriter->writeAttribute('w:val', $size);
|
||||
$objWriter->writeAttribute('w:val', $size * 2);
|
||||
$objWriter->endElement();
|
||||
$objWriter->startElement('w:szCs');
|
||||
$objWriter->writeAttribute('w:val', $size);
|
||||
$objWriter->writeAttribute('w:val', $size * 2);
|
||||
$objWriter->endElement();
|
||||
}
|
||||
|
||||
|
|
@ -424,8 +424,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||
$objWriter->endElement();
|
||||
}
|
||||
|
||||
// Striketrough
|
||||
if ($striketrough) {
|
||||
// Strikethrough
|
||||
if ($strikethrough) {
|
||||
$objWriter->writeElement('w:strike', null);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -380,11 +380,11 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
|
|||
$objWriter->endElement();
|
||||
|
||||
$objWriter->startElement('w:sz');
|
||||
$objWriter->writeAttribute('w:val', $fontSize);
|
||||
$objWriter->writeAttribute('w:val', $fontSize * 2);
|
||||
$objWriter->endElement();
|
||||
|
||||
$objWriter->startElement('w:szCs');
|
||||
$objWriter->writeAttribute('w:val', $fontSize);
|
||||
$objWriter->writeAttribute('w:val', $fontSize * 2);
|
||||
$objWriter->endElement();
|
||||
|
||||
$objWriter->endElement();
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue