From 09ba95bd88c08170b2488a70fb593df75941daa1 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 18:02:47 +0700 Subject: [PATCH] Paragraph: Ability to define (1) normal paragraph style with PHPWord::setNormalStyle() and (2) parent style (basedOn) and style for following paragraph (next) --- Classes/PHPWord.php | 10 +++ Classes/PHPWord/Style.php | 10 +++ Classes/PHPWord/Style/Paragraph.php | 81 +++++++++++++++++++--- Classes/PHPWord/Writer/Word2007/Styles.php | 42 +++++++++++ changelog.txt | 4 +- 5 files changed, 136 insertions(+), 11 deletions(-) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 658585b7..8f3cc922 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -198,6 +198,16 @@ class PHPWord PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph); } + /** + * Set normal paragraph style definition to styles.xml + * + * @param array $styles Paragraph style definition + */ + public function setNormalStyle($styles) + { + PHPWord_Style::setNormalStyle($styles); + } + /** * Adds a hyperlink style to styles.xml * diff --git a/Classes/PHPWord/Style.php b/Classes/PHPWord/Style.php index 7b20f8ab..6941b1bc 100755 --- a/Classes/PHPWord/Style.php +++ b/Classes/PHPWord/Style.php @@ -140,6 +140,16 @@ class PHPWord_Style } } + /** + * Set normal (default) paragraph style + * + * @param array $styles Paragraph style definition + */ + public static function setNormalStyle($styles) + { + self::addParagraphStyle('Normal', $styles); + } + /** * Get all styles * diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 3c2639be..632ef8b2 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -80,6 +80,20 @@ class PHPWord_Style_Paragraph */ private $_hanging; + /** + * Parent style + * + * @var string + */ + private $_basedOn; + + /** + * Style for next paragraph + * + * @var string + */ + private $_next; + /** * New Paragraph Style */ @@ -92,6 +106,8 @@ class PHPWord_Style_Paragraph $this->_tabs = null; $this->_indent = null; $this->_hanging = null; + $this->_basedOn = 'Normal'; + $this->_next = null; } /** @@ -231,6 +247,16 @@ class PHPWord_Style_Paragraph return $this; } + /** + * Get hanging + * + * @return int + */ + public function getHanging() + { + return $this->_hanging; + } + /** * Set hanging * @@ -243,16 +269,6 @@ class PHPWord_Style_Paragraph return $this; } - /** - * Get hanging - * - * @return int - */ - public function getHanging() - { - return $this->_hanging; - } - /** * Get tabs * @@ -262,4 +278,49 @@ class PHPWord_Style_Paragraph { return $this->_tabs; } + + /** + * Get parent style ID + * + * @return string + */ + public function getBasedOn() + { + return $this->_basedOn; + } + + /** + * Set parent style ID + * + * @param string $pValue + * @return PHPWord_Style_Paragraph + */ + public function setBasedOn($pValue = 'Normal') + { + $this->_basedOn = $pValue; + return $this; + } + + /** + * Get style for next paragraph + * + * @return string + */ + public function getNext() + { + return $this->_next; + } + + /** + * Set style for next paragraph + * + * @param string $pValue + * @return PHPWord_Style_Paragraph + */ + public function setNext($pValue = null) + { + $this->_next = $pValue; + return $this; + } + } \ No newline at end of file diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php index d2e28539..2ae07d24 100755 --- a/Classes/PHPWord/Writer/Word2007/Styles.php +++ b/Classes/PHPWord/Writer/Word2007/Styles.php @@ -59,8 +59,30 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base // Write Style Definitions $styles = PHPWord_Style::getStyles(); + + // Write normal paragraph style + $normalStyle = null; + if (array_key_exists('Normal', $styles)) { + $normalStyle = $styles['Normal']; + } + $objWriter->startElement('w:style'); + $objWriter->writeAttribute('w:type', 'paragraph'); + $objWriter->writeAttribute('w:default', '1'); + $objWriter->writeAttribute('w:styleId', 'Normal'); + $objWriter->startElement('w:name'); + $objWriter->writeAttribute('w:val', 'Normal'); + $objWriter->endElement(); + if (!is_null($normalStyle)) { + $this->_writeParagraphStyle($objWriter, $normalStyle); + } + $objWriter->endElement(); + + // Write other styles if (count($styles) > 0) { foreach ($styles as $styleName => $style) { + if ($styleName == 'Normal') { + continue; + } if ($style instanceof PHPWord_Style_Font) { $paragraphStyle = $style->getParagraphStyle(); @@ -92,6 +114,10 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); if (!is_null($paragraphStyle)) { + // Point parent style to Normal + $objWriter->startElement('w:basedOn'); + $objWriter->writeAttribute('w:val', 'Normal'); + $objWriter->endElement(); $this->_writeParagraphStyle($objWriter, $paragraphStyle); } @@ -109,6 +135,22 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->writeAttribute('w:val', $styleName); $objWriter->endElement(); + // Parent style + $basedOn = $style->getBasedOn(); + if (!is_null($basedOn)) { + $objWriter->startElement('w:basedOn'); + $objWriter->writeAttribute('w:val', $basedOn); + $objWriter->endElement(); + } + + // Next paragraph style + $next = $style->getNext(); + if (!is_null($next)) { + $objWriter->startElement('w:next'); + $objWriter->writeAttribute('w:val', $next); + $objWriter->endElement(); + } + $this->_writeParagraphStyle($objWriter, $style); $objWriter->endElement(); diff --git a/changelog.txt b/changelog.txt index 8676eb9b..3edb75a8 100755 --- a/changelog.txt +++ b/changelog.txt @@ -38,7 +38,9 @@ Changes in branch for release 0.7.1 : - Feature: (ivanlanin) GH-48 GH-86 - Paragraph: Hanging paragraph - Feature: (ivanlanin) GH-48 GH-86 - Section: Multicolumn and section break - QA: (Progi1984) - UnitTests -- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips converter +- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips() converter +- Feature: (ivanlanin) - Paragraph: Ability to define normal paragraph style with PHPWord::setNormalStyle() +- Feature: (ivanlanin) - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next) Changes in branch for release 0.7.0 : - Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found