From 47c7fab091244bd236df0db75bb69f3f69d2a60a Mon Sep 17 00:00:00 2001 From: Jeroen Moors Date: Fri, 7 Mar 2014 23:23:42 +0100 Subject: [PATCH 01/17] Replace call to save with saveAs to save the document. --- samples/Sample_07_TemplateCloneRow.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Sample_07_TemplateCloneRow.php b/samples/Sample_07_TemplateCloneRow.php index 1fa5bd56..ccdee2c3 100755 --- a/samples/Sample_07_TemplateCloneRow.php +++ b/samples/Sample_07_TemplateCloneRow.php @@ -51,4 +51,4 @@ $document->setValue('userFirstName#3', 'Michael'); $document->setValue('userName#3', 'Ray'); $document->setValue('userPhone#3', '+1 428 889 775'); -$document->save('Sample_03_TemplateCloneRow_result.docx'); +$document->saveAs('Sample_07_TemplateCloneRow_result.docx'); From 423c3349719e3b52792fe79dbfffde682d7bf0e7 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 8 Mar 2014 12:12:08 +0700 Subject: [PATCH 02/17] Fix bug in directory whitelist. Run `phpunit --coverage-html` to reproduce the error. --- phpunit.xml.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e57799ce..908304ad 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,7 +15,7 @@ - ../Classes + ./Classes From df7083b64758f2f3a85a827058234c75062626d0 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 8 Mar 2014 17:18:36 +0700 Subject: [PATCH 03/17] Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before. --- Classes/PHPWord/Style/Paragraph.php | 132 +++++++++++++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 53 ++++++++- Tests/PHPWord/Writer/Word2007/BaseTest.php | 31 ++++- changelog.txt | 1 + samples/Sample_08_ParagraphPagination.php | 76 ++++++++++++ 5 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 samples/Sample_08_ParagraphPagination.php diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 632ef8b2..70732f86 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -94,6 +94,34 @@ class PHPWord_Style_Paragraph */ private $_next; + /** + * Allow first/last line to display on a separate page + * + * @var bool + */ + private $_widowControl; + + /** + * Keep paragraph with next paragraph + * + * @var bool + */ + private $_keepNext; + + /** + * Keep all lines on one page + * + * @var bool + */ + private $_keepLines; + + /** + * Start paragraph on next page + * + * @var bool + */ + private $_pageBreakBefore; + /** * New Paragraph Style */ @@ -108,6 +136,10 @@ class PHPWord_Style_Paragraph $this->_hanging = null; $this->_basedOn = 'Normal'; $this->_next = null; + $this->_widowControl = true; + $this->_keepNext = false; + $this->_keepLines = false; + $this->_pageBreakBefore = false; } /** @@ -323,4 +355,104 @@ class PHPWord_Style_Paragraph return $this; } + /** + * Get allow first/last line to display on a separate page setting + * + * @return bool + */ + public function getWidowControl() + { + return $this->_widowControl; + } + + /** + * Set keep paragraph with next paragraph setting + * + * @param bool $pValue + * @return PHPWord_Style_Paragraph + */ + public function setWidowControl($pValue = true) + { + if (!is_bool($pValue)) { + $pValue = false; + } + $this->_widowControl = $pValue; + return $this; + } + + /** + * Get keep paragraph with next paragraph setting + * + * @return bool + */ + public function getKeepNext() + { + return $this->_keepNext; + } + + /** + * Set keep paragraph with next paragraph setting + * + * @param bool $pValue + * @return PHPWord_Style_Paragraph + */ + public function setKeepNext($pValue = true) + { + if (!is_bool($pValue)) { + $pValue = false; + } + $this->_keepNext = $pValue; + return $this; + } + + /** + * Get keep all lines on one page setting + * + * @return bool + */ + public function getKeepLines() + { + return $this->_keepLines; + } + + /** + * Set keep all lines on one page setting + * + * @param bool $pValue + * @return PHPWord_Style_Paragraph + */ + public function setKeepLines($pValue = true) + { + if (!is_bool($pValue)) { + $pValue = false; + } + $this->_keepLines = $pValue; + return $this; + } + + /** + * Get start paragraph on next page setting + * + * @return bool + */ + public function getPageBreakBefore() + { + return $this->_pageBreakBefore; + } + + /** + * Set start paragraph on next page setting + * + * @param bool $pValue + * @return PHPWord_Style_Paragraph + */ + public function setPageBreakBefore($pValue = true) + { + if (!is_bool($pValue)) { + $pValue = false; + } + $this->_pageBreakBefore = $pValue; + return $this; + } + } \ No newline at end of file diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 47c31a81..2b99ef45 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -117,27 +117,47 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } - protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Paragraph $style, $withoutPPR = false) + /** + * Write paragraph style + * + * @param PHPWord_Shared_XMLWriter $objWriter + * @param PHPWord_Style_Paragraph $style + * @param bool $withoutPPR + * @return void + */ + protected function _writeParagraphStyle( + PHPWord_Shared_XMLWriter $objWriter = null, + PHPWord_Style_Paragraph $style, + $withoutPPR = false) { $align = $style->getAlign(); + $spacing = $style->getSpacing(); $spaceBefore = $style->getSpaceBefore(); $spaceAfter = $style->getSpaceAfter(); - $spacing = $style->getSpacing(); $indent = $style->getIndent(); $hanging = $style->getHanging(); $tabs = $style->getTabs(); + $widowControl = $style->getWidowControl(); + $keepNext = $style->getKeepNext(); + $keepLines = $style->getKeepLines(); + $pageBreakBefore = $style->getPageBreakBefore(); - if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) { + if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || + !is_null($spaceAfter) || !is_null($indent) || !is_null($hanging) || + !is_null($tabs) || !is_null($widowControl) || !is_null($keepNext) || + !is_null($keepLines) || !is_null($pageBreakBefore)) { if (!$withoutPPR) { $objWriter->startElement('w:pPr'); } + // Alignment if (!is_null($align)) { $objWriter->startElement('w:jc'); $objWriter->writeAttribute('w:val', $align); $objWriter->endElement(); } + // Indentation if (!is_null($indent) || !is_null($hanging)) { $objWriter->startElement('w:ind'); $objWriter->writeAttribute('w:firstLine', 0); @@ -150,7 +170,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } - if (!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) { + // Spacing + if (!is_null($spaceBefore) || !is_null($spaceAfter) || + !is_null($spacing)) { $objWriter->startElement('w:spacing'); if (!is_null($spaceBefore)) { $objWriter->writeAttribute('w:before', $spaceBefore); @@ -165,6 +187,29 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } + // Pagination + if (!$widowControl) { + $objWriter->startElement('w:widowControl'); + $objWriter->writeAttribute('w:val', '0'); + $objWriter->endElement(); + } + if ($keepNext) { + $objWriter->startElement('w:keepNext'); + $objWriter->writeAttribute('w:val', '1'); + $objWriter->endElement(); + } + if ($keepLines) { + $objWriter->startElement('w:keepLines'); + $objWriter->writeAttribute('w:val', '1'); + $objWriter->endElement(); + } + if ($pageBreakBefore) { + $objWriter->startElement('w:pageBreakBefore'); + $objWriter->writeAttribute('w:val', '1'); + $objWriter->endElement(); + } + + // Tabs if (!is_null($tabs)) { $tabs->toXml($objWriter); } diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index 8788af8b..0179a6a4 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -77,6 +77,35 @@ class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase { $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); $this->assertEquals(5, $element->getAttribute('w:val')); + } + + /** + * Test write paragraph pagination + */ + public function testWriteParagraphStyle_Pagination() + { + // Create the doc + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + $attributes = array( + 'widowControl' => 0, + 'keepNext' => 1, + 'keepLines' => 1, + 'pageBreakBefore' => 1, + ); + foreach ($attributes as $attribute => $value) { + $section->addText('Test', null, array($attribute => $value)); + } + $doc = TestHelperDOCX::getDocument($PHPWord); + + // Test the attributes + $i = 0; + foreach ($attributes as $attribute => $value) { + $i++; + $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$attribute}"; + $element = $doc->getElement($path); + $this->assertEquals($value, $element->getAttribute('w:val')); + } } + } - \ No newline at end of file diff --git a/changelog.txt b/changelog.txt index c5c66fc3..81f9982b 100755 --- a/changelog.txt +++ b/changelog.txt @@ -44,6 +44,7 @@ Changes in branch for release 0.7.1 : - Feature: (ivanlanin) GH-87 - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next) - Feature: (jeroenmoors) GH-44 GH-88 - Clone table rows on the fly when using a template document - Feature: (deds) GH-16 - Initial addition of basic footnote support +- Feature: (ivanlanin) GH-16 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : diff --git a/samples/Sample_08_ParagraphPagination.php b/samples/Sample_08_ParagraphPagination.php new file mode 100644 index 00000000..97b43032 --- /dev/null +++ b/samples/Sample_08_ParagraphPagination.php @@ -0,0 +1,76 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word document +echo date('H:i:s') , " Create new PHPWord object" , EOL; +$PHPWord = new PHPWord(); +$PHPWord->setDefaultParagraphStyle(array( + 'align' => 'both', + 'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(12), + 'spacing' => 120, +)); + +// Sample +$section = $PHPWord->createSection(); + +$section->addText('Below are the samples on how to control your paragraph ' . + 'pagination. See "Line and Page Break" tab on paragraph properties ' . + 'window to see the attribute set by these controls.', + array('bold' => true), null); + +$section->addText('Paragraph with widowControl = false (default: true). ' . + 'A "widow" is the last line of a paragraph printed by itself at the top ' . + 'of a page. An "orphan" is the first line of a paragraph printed by ' . + 'itself at the bottom of a page. Set this option to "false" if you want ' . + 'to disable this automatic control.', + null, array('widowControl' => false)); + +$section->addText('Paragraph with keepNext = true (default: false). ' . + '"Keep with next" is used to prevent Word from inserting automatic page ' . + 'breaks between paragraphs. Set this option to "true" if you do not want ' . + 'your paragraph to be separated with the next paragraph.', + null, array('keepNext' => true)); + +$section->addText('Paragraph with keepLines = true (default: false). ' . + '"Keep lines together" will prevent Word from inserting an automatic page ' . + 'break within a paragraph. Set this option to "true" if you do not want ' . + 'all lines of your paragraph to be in the same page.', + null, array('keepLines' => true)); + +$section->addText('Keep scrolling. More below.'); + +$section->addText('Paragraph with pageBreakBefore = true (default: false). ' . + 'Different with all other control above, "page break before" separates ' . + 'your paragraph into the next page. This option is most useful for ' . + 'heading styles.', + null, array('pageBreakBefore' => true)); + +// Save File +echo date('H:i:s') , " Write to Word2007 format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save(str_replace('.php', '.docx', __FILE__)); + +// echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; +// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +// $objWriter->save(str_replace('.php', '.odt', __FILE__)); + +// echo date('H:i:s') , " Write to RTF format" , EOL; +// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +// $objWriter->save(str_replace('.php', '.rtf', __FILE__)); + + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; + +// Echo done +echo date('H:i:s') , " Done writing file" , EOL; From 0e26da918ab4a0240374287fb6a758bc72cc465a Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 8 Mar 2014 17:26:49 +0700 Subject: [PATCH 04/17] Fix issue number on changelog.txt --- changelog.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index 81f9982b..4ad31be9 100755 --- a/changelog.txt +++ b/changelog.txt @@ -44,7 +44,7 @@ Changes in branch for release 0.7.1 : - Feature: (ivanlanin) GH-87 - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next) - Feature: (jeroenmoors) GH-44 GH-88 - Clone table rows on the fly when using a template document - Feature: (deds) GH-16 - Initial addition of basic footnote support -- Feature: (ivanlanin) GH-16 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before +- Feature: (ivanlanin) GH-91 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : From b39f9daee20b78a5fe604dfe2e2b6f4f2086ff58 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 8 Mar 2014 23:43:27 +0700 Subject: [PATCH 05/17] Refactor and unit test PHPWord_Style_Font --- Classes/PHPWord.php | 3 +- Classes/PHPWord/Style/Font.php | 229 +++++++++++++++++++-- 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, 314 insertions(+), 42 deletions(-) create mode 100644 Tests/PHPWord/Style/FontTest.php diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 51489224..12d8af4b 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 3363dc66..062fe839 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 @@ 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 */ diff --git a/Classes/PHPWord/Writer/ODText/Styles.php b/Classes/PHPWord/Writer/ODText/Styles.php index fd56b7fa..745da08e 100755 --- a/Classes/PHPWord/Writer/ODText/Styles.php +++ b/Classes/PHPWord/Writer/ODText/Styles.php @@ -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'); diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index 42b544fb..12400907 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -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); } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 2b99ef45..77f4d0d7 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -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); } diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php index 2ae07d24..c87d94bf 100755 --- a/Classes/PHPWord/Writer/Word2007/Styles.php +++ b/Classes/PHPWord/Writer/Word2007/Styles.php @@ -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(); diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php new file mode 100644 index 00000000..1238f34b --- /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 06/17] Add section settings documentation --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a7be92df..09556559 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 07/17] Constants explanation --- Classes/PHPWord.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 12d8af4b..64f9d9a2 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -39,8 +39,22 @@ if (!defined('PHPWORD_BASE_PATH')) { 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'; /** From 4dcde8c0a9283a4b4d94adf6eeafd937b187e65c Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 16:06:35 +0700 Subject: [PATCH 08/17] PHPWord_Shared_Drawing::centimetersToPixels() conversion --- Classes/PHPWord/Shared/Drawing.php | 2 +- changelog.txt | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Classes/PHPWord/Shared/Drawing.php b/Classes/PHPWord/Shared/Drawing.php index dba204e3..064e6fcc 100755 --- a/Classes/PHPWord/Shared/Drawing.php +++ b/Classes/PHPWord/Shared/Drawing.php @@ -128,7 +128,7 @@ class PHPWord_Shared_Drawing public static function centimetersToPixels($pValue = 0) { if ($pValue != 0) { - return $pValue * 0.028; + return $pValue / 0.028; } else { return 0; } diff --git a/changelog.txt b/changelog.txt index 4ad31be9..09ffc81f 100755 --- a/changelog.txt +++ b/changelog.txt @@ -44,7 +44,10 @@ Changes in branch for release 0.7.1 : - Feature: (ivanlanin) GH-87 - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next) - Feature: (jeroenmoors) GH-44 GH-88 - Clone table rows on the fly when using a template document - Feature: (deds) GH-16 - Initial addition of basic footnote support -- Feature: (ivanlanin) GH-91 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before +- Feature: (ivanlanin) GH-92 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before +- General: (ivanlanin) GH-93 - General: PHPWord_Style_Font refactoring +- General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing. +- Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : From 537f49eeb4639c58c9b0d36ff4c4c7e7701f3747 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 17:44:16 +0700 Subject: [PATCH 09/17] Fix non-existing functions in Writer/ODText/Content.php --- Classes/PHPWord/Writer/ODText/Content.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Writer/ODText/Content.php b/Classes/PHPWord/Writer/ODText/Content.php index 84806f48..fad07d5a 100755 --- a/Classes/PHPWord/Writer/ODText/Content.php +++ b/Classes/PHPWord/Writer/ODText/Content.php @@ -115,7 +115,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart $numPStyles++; $pPHPWord->addParagraphStyle('P' . $numPStyles, array()); - $element->setParagraph('P' . $numPStyles); + $element->setParagraphStyle('P' . $numPStyles); } } } @@ -338,4 +338,15 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) { } + + /** + * Dummy function just to make all samples produce ODT + * + * @todo Create the real function + */ + private function _writeSection( + PHPWord_Shared_XMLWriter $objWriter = null, + PHPWord_Section $section) + { + } } From d177b4744b6668c7cc7f349bb389d3510b8dadce Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 17:47:04 +0700 Subject: [PATCH 10/17] New function PHPWord_Style_Paragraph::setTabs() and unit test for PHPWord_Style_Paragraph --- Classes/PHPWord/Style/Paragraph.php | 37 +++++++---- Tests/PHPWord/Style/ParagraphTest.php | 90 +++++++++++++++++++++++++++ changelog.txt | 1 + 3 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 Tests/PHPWord/Style/ParagraphTest.php diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 70732f86..5942ec34 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -145,24 +145,21 @@ class PHPWord_Style_Paragraph /** * Set Style value * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value */ public function setStyleValue($key, $value) { - if ($key == '_indent') { - $value = $value * 720; // 720 twips per indent - } - if ($key == '_hanging') { + if ($key == '_indent' || $key == '_hanging') { $value = $value * 720; } if ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips } - if ($key === '_tabs') { - $value = new PHPWord_Style_Tabs($value); + $method = 'set' . ucwords(substr($key, 1)); + if (method_exists($this, $method)) { + $this->$method($value); } - $this->$key = $value; } /** @@ -311,6 +308,20 @@ class PHPWord_Style_Paragraph return $this->_tabs; } + /* + * Set tabs + * + * @param array $pValue + * @return PHPWord_Style_Paragraph + */ + public function setTabs($pValue = null) + { + if (is_array($pValue)) { + $this->_tabs = new PHPWord_Style_Tabs($pValue); + } + return $this; + } + /** * Get parent style ID * @@ -374,7 +385,7 @@ class PHPWord_Style_Paragraph public function setWidowControl($pValue = true) { if (!is_bool($pValue)) { - $pValue = false; + $pValue = true; } $this->_widowControl = $pValue; return $this; @@ -396,7 +407,7 @@ class PHPWord_Style_Paragraph * @param bool $pValue * @return PHPWord_Style_Paragraph */ - public function setKeepNext($pValue = true) + public function setKeepNext($pValue = false) { if (!is_bool($pValue)) { $pValue = false; @@ -421,7 +432,7 @@ class PHPWord_Style_Paragraph * @param bool $pValue * @return PHPWord_Style_Paragraph */ - public function setKeepLines($pValue = true) + public function setKeepLines($pValue = false) { if (!is_bool($pValue)) { $pValue = false; @@ -446,7 +457,7 @@ class PHPWord_Style_Paragraph * @param bool $pValue * @return PHPWord_Style_Paragraph */ - public function setPageBreakBefore($pValue = true) + public function setPageBreakBefore($pValue = false) { if (!is_bool($pValue)) { $pValue = false; diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php new file mode 100644 index 00000000..c0850cde --- /dev/null +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -0,0 +1,90 @@ + null, + 'widowControl' => true, + 'keepNext' => false, + 'keepLines' => false, + 'pageBreakBefore' => false, + ); + 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_Paragraph(); + + $attributes = array( + 'align' => 'justify', + 'spaceAfter' => 240, + 'spaceBefore' => 240, + 'indent' => 1, + 'hanging' => 1, + 'spacing' => 120, + 'basedOn' => 'Normal', + 'next' => 'Normal', + 'widowControl' => false, + 'keepNext' => true, + 'keepLines' => true, + 'pageBreakBefore' => true, + ); + foreach ($attributes as $key => $value) { + $method = 'get' . ucwords($key); + $object->setStyleValue("_$key", $value); + if ($key == 'align') { + if ($value == 'justify') { + $value = 'both'; + } + } elseif ($key == 'indent' || $key == 'hanging') { + $value = $value * 720; + } elseif ($key == 'spacing') { + $value += 240; + } + $this->assertEquals($value, $object->$method()); + } + } + + /** + * Test tabs + */ + public function testTabs() + { + $object = new PHPWord_Style_Paragraph(); + $object->setTabs(array( + new PHPWord_Style_Tab('left', 1550), + new PHPWord_Style_Tab('right', 5300), + )); + $this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs()); + } + +} diff --git a/changelog.txt b/changelog.txt index 09ffc81f..a6a63214 100755 --- a/changelog.txt +++ b/changelog.txt @@ -48,6 +48,7 @@ Changes in branch for release 0.7.1 : - General: (ivanlanin) GH-93 - General: PHPWord_Style_Font refactoring - General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing. - Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion +- Feature: (ivanlanin) - Paragraph: setTabs() function - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : From 68a4387dcfd8fc8b482c0f71da7b1ebd2bbc7382 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 17:47:42 +0700 Subject: [PATCH 11/17] Reformat code samples on README.md to max 80 char per line --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 09556559..111070fd 100755 --- a/README.md +++ b/README.md @@ -50,21 +50,25 @@ The following is a basic example of the PHPWord library. ```php $PHPWord = new PHPWord(); -// Every element you want to append to the word document is placed in a section. So you need a section: +// Every element you want to append to the word document is placed in a section. +// To create a basic section: $section = $PHPWord->createSection(); // After creating a section, you can append elements: $section->addText('Hello world!'); // You can directly style your text by giving the addText function an array: -$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); +$section->addText('Hello world! I am formatted.', + array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); -// If you often need the same style again you can create a user defined style to the word document -// and give the addText function the name of the style: -$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); -$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); +// If you often need the same style again you can create a user defined style +// to the word document and give the addText function the name of the style: +$PHPWord->addFontStyle('myOwnStyle', + array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); +$section->addText('Hello world! I am formatted by a user defined style', + 'myOwnStyle'); -// You can also putthe appended element to local object an call functions like this: +// You can also put the appended element to local object like this: $fontStyle = new PHPWord_Style_Font(); $fontStyle->setBold(true); $fontStyle->setName('Verdana'); @@ -72,7 +76,7 @@ $fontStyle->setSize(22); $myTextElement = $section->addText('Hello World!'); $myTextElement->setFontStyle($fontStyle); -// At least write the document to webspace: +// Finally, write the document: $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save('helloWorld.docx'); ``` From 26330807f71d303e3138541c18f1479703920384 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 17:48:20 +0700 Subject: [PATCH 12/17] Fix parameter input for testWriteParagraphStyle_Pagination --- Tests/PHPWord/Writer/Word2007/BaseTest.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index 0179a6a4..95ca94e1 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -88,10 +88,10 @@ class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); $attributes = array( - 'widowControl' => 0, - 'keepNext' => 1, - 'keepLines' => 1, - 'pageBreakBefore' => 1, + 'widowControl' => false, + 'keepNext' => true, + 'keepLines' => true, + 'pageBreakBefore' => true, ); foreach ($attributes as $attribute => $value) { $section->addText('Test', null, array($attribute => $value)); @@ -100,11 +100,12 @@ class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase { // Test the attributes $i = 0; - foreach ($attributes as $attribute => $value) { + foreach ($attributes as $key => $value) { $i++; - $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$attribute}"; + $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}"; $element = $doc->getElement($path); - $this->assertEquals($value, $element->getAttribute('w:val')); + $expected = $value ? 1 : 0; + $this->assertEquals($expected, $element->getAttribute('w:val')); } } From c3644909550918e120acd335933db7ec92e0290d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 21:57:27 +0700 Subject: [PATCH 13/17] Remove PHPWord_Style_Cell::setHeight() since it's irrelevant and never used. Should be in PHPWord_Style_Row --- Classes/PHPWord/Style/Cell.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Classes/PHPWord/Style/Cell.php b/Classes/PHPWord/Style/Cell.php index e7d2bf91..1e534e23 100755 --- a/Classes/PHPWord/Style/Cell.php +++ b/Classes/PHPWord/Style/Cell.php @@ -198,11 +198,6 @@ class PHPWord_Style_Cell $this->_bgColor = $pValue; } - public function setHeight($pValue = null) - { - $this->_height = $pValue; - } - public function setBorderSize($pValue = null) { $this->_borderTopSize = $pValue; From fa1663a2b185b5f96a2314581cc9bbbf7b3fa75d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 21:59:23 +0700 Subject: [PATCH 14/17] PHP method IS NOT case sensitive :) --- Classes/PHPWord/Style/Font.php | 2 +- Classes/PHPWord/Style/Paragraph.php | 2 +- Tests/PHPWord/Style/FontTest.php | 10 +++++----- Tests/PHPWord/Style/ParagraphTest.php | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index 062fe839..43495cdc 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -192,7 +192,7 @@ class PHPWord_Style_Font */ public function setStyleValue($key, $value) { - $method = 'set' . ucwords(substr($key, 1)); + $method = 'set' . substr($key, 1); if (method_exists($this, $method)) { $this->$method($value); } diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 5942ec34..c7104e8e 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -156,7 +156,7 @@ class PHPWord_Style_Paragraph if ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips } - $method = 'set' . ucwords(substr($key, 1)); + $method = 'set' . substr($key, 1); if (method_exists($this, $method)) { $this->$method($value); } diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php index 1238f34b..a3fa2086 100644 --- a/Tests/PHPWord/Style/FontTest.php +++ b/Tests/PHPWord/Style/FontTest.php @@ -45,11 +45,11 @@ class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase 'fgColor' => null, ); foreach ($attributes as $key => $default) { - $method = 'get' . ucwords($key); + $get = "get{$key}"; $object->setStyleValue("_$key", null); - $this->assertEquals($default, $object->$method()); + $this->assertEquals($default, $object->$get()); $object->setStyleValue("_$key", ''); - $this->assertEquals($default, $object->$method()); + $this->assertEquals($default, $object->$get()); } } @@ -73,9 +73,9 @@ class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase 'fgColor' => '999999', ); foreach ($attributes as $key => $value) { - $method = 'get' . ucwords($key); + $get = "get{$key}"; $object->setStyleValue("_$key", $value); - $this->assertEquals($value, $object->$method()); + $this->assertEquals($value, $object->$get()); } } diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php index c0850cde..23d0b713 100644 --- a/Tests/PHPWord/Style/ParagraphTest.php +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -29,11 +29,11 @@ class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase 'pageBreakBefore' => false, ); foreach ($attributes as $key => $default) { - $method = 'get' . ucwords($key); + $get = "get{$key}"; $object->setStyleValue("_$key", null); - $this->assertEquals($default, $object->$method()); + $this->assertEquals($default, $object->$get()); $object->setStyleValue("_$key", ''); - $this->assertEquals($default, $object->$method()); + $this->assertEquals($default, $object->$get()); } } @@ -59,7 +59,7 @@ class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase 'pageBreakBefore' => true, ); foreach ($attributes as $key => $value) { - $method = 'get' . ucwords($key); + $get = "get{$key}"; $object->setStyleValue("_$key", $value); if ($key == 'align') { if ($value == 'justify') { @@ -70,7 +70,7 @@ class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase } elseif ($key == 'spacing') { $value += 240; } - $this->assertEquals($value, $object->$method()); + $this->assertEquals($value, $object->$get()); } } From 3d8f09dbc17282ec82d1674d6d57b999a7bf2b17 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 22:00:43 +0700 Subject: [PATCH 15/17] PHPWord_Style_CellTest 100% code coverage --- Tests/PHPWord/Style/CellTest.php | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Tests/PHPWord/Style/CellTest.php diff --git a/Tests/PHPWord/Style/CellTest.php b/Tests/PHPWord/Style/CellTest.php new file mode 100644 index 00000000..9cdd7e5e --- /dev/null +++ b/Tests/PHPWord/Style/CellTest.php @@ -0,0 +1,80 @@ + null, + 'textDirection' => null, + 'bgColor' => null, + 'borderTopSize' => null, + 'borderTopColor' => null, + 'borderLeftSize' => null, + 'borderLeftColor' => null, + 'borderRightSize' => null, + 'borderRightColor' => null, + 'borderBottomSize' => null, + 'borderBottomColor' => null, + 'gridSpan' => null, + 'vMerge' => null, + ); + //'defaultBorderColor' => null, + foreach ($attributes as $key => $value) { + $set = "set{$key}"; + $get = "get{$key}"; + $object->$set($value); + $this->assertEquals($value, $object->$get()); + } + } + + /** + * Test border color + */ + public function testBorderColor() + { + $object = new PHPWord_Style_Cell(); + + $default = '000000'; + $value = 'FF0000'; + + $this->assertEquals($default, $object->getDefaultBorderColor()); + + $object->setStyleValue('_defaultBorderColor', $value); + $this->assertEquals($value, $object->getDefaultBorderColor()); + + $object->setStyleValue('_borderColor', $value); + $expected = array($value, $value, $value, $value); + $this->assertEquals($expected, $object->getBorderColor()); + } + + /** + * Test border size + */ + public function testBorderSize() + { + $object = new PHPWord_Style_Cell(); + + $value = 120; + $expected = array($value, $value, $value, $value); + $object->setStyleValue('_borderSize', $value); + $this->assertEquals($expected, $object->getBorderSize()); + } + +} From 34d91214b4ab4bb59605966e68b008868a32a635 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 22:17:21 +0700 Subject: [PATCH 16/17] Change normal value for cell style tests --- Tests/PHPWord/Style/CellTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Tests/PHPWord/Style/CellTest.php b/Tests/PHPWord/Style/CellTest.php index 9cdd7e5e..4db86f35 100644 --- a/Tests/PHPWord/Style/CellTest.php +++ b/Tests/PHPWord/Style/CellTest.php @@ -21,19 +21,19 @@ class PHPWord_Style_CellTest extends \PHPUnit_Framework_TestCase $object = new PHPWord_Style_Cell(); $attributes = array( - 'valign' => null, - 'textDirection' => null, - 'bgColor' => null, - 'borderTopSize' => null, - 'borderTopColor' => null, - 'borderLeftSize' => null, - 'borderLeftColor' => null, - 'borderRightSize' => null, - 'borderRightColor' => null, - 'borderBottomSize' => null, - 'borderBottomColor' => null, - 'gridSpan' => null, - 'vMerge' => null, + 'valign' => 'left', + 'textDirection' => PHPWord_Style_Cell::TEXT_DIR_BTLR, + 'bgColor' => 'FFFF00', + 'borderTopSize' => 120, + 'borderTopColor' => 'FFFF00', + 'borderLeftSize' => 120, + 'borderLeftColor' => 'FFFF00', + 'borderRightSize' => 120, + 'borderRightColor' => 'FFFF00', + 'borderBottomSize' => 120, + 'borderBottomColor' => 'FFFF00', + 'gridSpan' => 2, + 'vMerge' => 2, ); //'defaultBorderColor' => null, foreach ($attributes as $key => $value) { From 712e09a2e55e15e9b03d032ee5cb0b6cd8330a84 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 9 Mar 2014 22:43:35 +0700 Subject: [PATCH 17/17] Basic README for `addText` and `createTextRun` --- README.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 111070fd..2f02013e 100755 --- a/README.md +++ b/README.md @@ -38,9 +38,10 @@ the following lines to your ``composer.json``. 2. [Sections](#sections) * [Section settings](#section-settings) * [Section page numbering](#section-page-numbering) -3. [Tables](#tables) +3. [Texts](#texts) +4. [Tables](#tables) * [Cell Style](#tables-cell-style) -4. [Images](#images) +5. [Images](#images) #### Basic usage @@ -158,6 +159,27 @@ $section = $PHPWord->createSection(); $section->getSettings()->setPageNumberingStart(1); ``` + +#### Texts + +Text can be added by using `addText` and `createTextRun` method. `addText` is used for creating simple paragraphs that only contain texts with the same style. `createTextRun` is used for creating complex paragraphs that contain text with different style (some bold, other italics, etc) or other elements, e.g. images or links. + +`addText` sample: + +```php +$fontStyle = array('name' => 'Times New Roman', 'size' => 9); +$paragraphStyle = array('align' => 'both'); +$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle); +``` + +`createTextRun` sample: + +```php +$textrun = $section->createTextRun(); +$textrun->addText('I am bold', array('bold' => true)); +$textrun->addText('I am italic, array('italic' => true)); +$textrun->addText('I am colored, array('color' => 'AACC00')); +``` #### Tables