From b39f9daee20b78a5fe604dfe2e2b6f4f2086ff58 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 8 Mar 2014 23:43:27 +0700 Subject: [PATCH 1/3] Refactor and unit test PHPWord_Style_Font --- Classes/PHPWord.php | 3 +- Classes/PHPWord/Style/Font.php | 227 +++++++++++++++++++-- Classes/PHPWord/Writer/ODText/Styles.php | 16 +- Classes/PHPWord/Writer/RTF.php | 10 +- Classes/PHPWord/Writer/Word2007/Base.php | 12 +- Classes/PHPWord/Writer/Word2007/Styles.php | 4 +- Tests/PHPWord/Style/FontTest.php | 82 ++++++++ 7 files changed, 313 insertions(+), 41 deletions(-) create mode 100644 Tests/PHPWord/Style/FontTest.php diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 5148922419..12d8af4b54 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -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 diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index 3363dc66d9..062fe839aa 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -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 @@ public function __construct($type = 'text', $styleParagraph = null) $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 @@ public function __construct($type = 'text', $styleParagraph = null) } } - public function getName() + /** + * Set style value + * + * @param string $key + * @param mixed $value + */ + public function setStyleValue($key, $value) { - return $this->_name; + $method = 'set' . ucwords(substr($key, 1)); + if (method_exists($this, $method)) { + $this->$method($value); + } } - public function setStyleValue($key, $value) + /** + * Get font name + * + * @return bool + */ + public function getName() { - if ($key == '_size') { - $value *= 2; - } - $this->$key = $value; + return $this->_name; } + /** + * 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 @@ public function setSuperScript($pValue = false) 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 @@ public function setSubScript($pValue = false) 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 @@ public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE) 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 */ diff --git a/Classes/PHPWord/Writer/ODText/Styles.php b/Classes/PHPWord/Writer/ODText/Styles.php index fd56b7fa11..745da08ee2 100755 --- a/Classes/PHPWord/Writer/ODText/Styles.php +++ b/Classes/PHPWord/Writer/ODText/Styles.php @@ -133,16 +133,16 @@ public function writeStyles(PHPWord $pPHPWord = null) $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 @@ public function writeStyles(PHPWord $pPHPWord = null) // 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'); diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index 42b544fba7..1240090769 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -176,7 +176,7 @@ private function _getData() // 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 @@ private function _getDataColor() 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 @@ private function _getDataContent_writeText(PHPWord_Section_Text $text) $sRTFText .= '\i'; } if ($styleFont->getSize()) { - $sRTFText .= '\fs' . $styleFont->getSize(); + $sRTFText .= '\fs' . ($styleFont->getSize() * 2); } } if ($this->_lastParagraphStyle != '' || $styleFont) { @@ -419,7 +419,7 @@ private function _getDataContent_writeText(PHPWord_Section_Text $text) $sRTFText .= '\i0'; } if ($styleFont->getSize()) { - $sRTFText .= '\fs20'; + $sRTFText .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2); } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 2b99ef452b..77f4d0d7fa 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -373,7 +373,7 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P $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 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P } // 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 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P // 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 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P $objWriter->endElement(); } - // Striketrough - if ($striketrough) { + // Strikethrough + if ($strikethrough) { $objWriter->writeElement('w:strike', null); } diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php index 2ae07d2440..c87d94bfa2 100755 --- a/Classes/PHPWord/Writer/Word2007/Styles.php +++ b/Classes/PHPWord/Writer/Word2007/Styles.php @@ -380,11 +380,11 @@ private function _writeDocDefaults(PHPWord_Shared_XMLWriter $objWriter = null) $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(); diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php new file mode 100644 index 0000000000..1238f34bd1 --- /dev/null +++ b/Tests/PHPWord/Style/FontTest.php @@ -0,0 +1,82 @@ + '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()); + } + } + +} From 074dfabbbafc2c5bf6a8a44637859a27fd092aa0 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 00:39:49 +0700 Subject: [PATCH 2/3] Add section settings documentation --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a7be92df99..0955655924 100755 --- a/README.md +++ b/README.md @@ -34,8 +34,10 @@ the following lines to your ``composer.json``. ### Table of contents 1. [Basic usage](#basic-usage) + * [Measurement units](#measurement-units) 2. [Sections](#sections) - * [Change Section Page Numbering](#sections-page-numbering) + * [Section settings](#section-settings) + * [Section page numbering](#section-page-numbering) 3. [Tables](#tables) * [Cell Style](#tables-cell-style) 4. [Images](#images) @@ -75,6 +77,7 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save('helloWorld.docx'); ``` + ##### Measurement units The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch. @@ -98,8 +101,51 @@ $sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2)); #### Sections - -##### Change Section Page Numbering +Every visible element in word is placed inside of a section. To create a section, use the following code: + +```php +$section = $PHPWord->createSection($sectionSettings); +``` +The `$sectionSettings` is an optional associative array that sets the section. Example: + +```php +$sectionSettings = array( + 'orientation' => 'landscape', + 'marginTop' => 600, + 'colsNum' => 2, +); +``` + +##### Section settings + +Below are the available settings for section: + +* `orientation` Page orientation, i.e. 'portrait' (default) or 'landscape' +* `marginTop` Page margin top in twips +* `marginLeft` Page margin left in twips +* `marginRight` Page margin right in twips +* `marginBottom` Page margin bottom in twips +* `borderTopSize` Border top size in twips +* `borderTopColor` Border top color +* `borderLeftSize` Border left size in twips +* `borderLeftColor` Border left color +* `borderRightSize` Border right size in twips +* `borderRightColor` Border right color +* `borderBottomSize` Border bottom size in twips +* `borderBottomColor` Border bottom color +* `headerHeight` Spacing to top of header +* `footerHeight` Spacing to bottom of footer +* `colsNum` Number of columns +* `colsSpace` Spacing between columns +* `breakType` Section break type (nextPage, nextColumn, continuous, evenPage, oddPage) + +The following two settings are automatically set by the use of the `orientation` setting. You can alter them but that's not recommended. + +* `pageSizeW` Page width in twips +* `pageSizeH` Page height in twips + + +##### Section page numbering You can change a section page numbering. From 9fd138494e66f241d796d55d0c242bc6051dcfeb Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 15:21:53 +0700 Subject: [PATCH 3/3] Constants explanation --- Classes/PHPWord.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 12d8af4b54..64f9d9a249 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -39,8 +39,22 @@ class PHPWord { + /** + * Default font name (Arial) + */ const DEFAULT_FONT_NAME = 'Arial'; + + /** + * Default font size in points (10pt) + * + * OOXML defined font size values in halfpoints, i.e. twice of what PHPWord + * use, and the conversion will be conducted during XML writing. + */ const DEFAULT_FONT_SIZE = 10; + + /** + * Default font color (black) + */ const DEFAULT_FONT_COLOR = '000000'; /**