From be4b01b6525df91fa76ada9aa9dcf3877440c239 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 11:14:45 +0700 Subject: [PATCH 1/5] PHPWord_Shared_Font::pointSizeToTwips --- Classes/PHPWord/Shared/Font.php | 12 ++++++++++++ README.md | 20 ++++++++++++++++++++ changelog.txt | 1 + 3 files changed, 33 insertions(+) diff --git a/Classes/PHPWord/Shared/Font.php b/Classes/PHPWord/Shared/Font.php index be3cf1a8..ccb19bc7 100755 --- a/Classes/PHPWord/Shared/Font.php +++ b/Classes/PHPWord/Shared/Font.php @@ -77,4 +77,16 @@ class PHPWord_Shared_Font { return self::centimeterSizeToTwips($sizeInPixel / 37.795275591); } + + /** + * Calculate twip based on point size, used mainly for paragraph spacing + * + * @param int|float $sizeInPoint Size in point + * @return int|float Size (in twips) + */ + public static function pointSizeToTwips($sizeInPoint = 1) + { + return ($sizeInPoint * 20); + } + } diff --git a/README.md b/README.md index dc1a611a..bd44a446 100755 --- a/README.md +++ b/README.md @@ -73,6 +73,26 @@ $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. + +You can use PHPWord helper functions to convert inches, centimeters, or points to twips. + +``` +// Paragraph with 6 points space after +$PHPWord->addParagraphStyle('My Style', array( + 'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6)) +); + +$section = $PHPWord->createSection(); +$sectionStyle = $section->getSettings(); +// half inch left margin +$sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5)); +// 2 cm right margin +$sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2)); +``` + #### Sections diff --git a/changelog.txt b/changelog.txt index a3752abd..8676eb9b 100755 --- a/changelog.txt +++ b/changelog.txt @@ -38,6 +38,7 @@ 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 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 From 09ba95bd88c08170b2488a70fb593df75941daa1 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 18:02:47 +0700 Subject: [PATCH 2/5] 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 From 58893137432d7e3cb2a171cc3b51f19bd473782d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 18:09:25 +0700 Subject: [PATCH 3/5] php syntax higlighting on README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bd44a446..f0f1e49c 100755 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inc You can use PHPWord helper functions to convert inches, centimeters, or points to twips. -``` +```php // Paragraph with 6 points space after $PHPWord->addParagraphStyle('My Style', array( 'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6)) From 0d1c368c41f9716a930301b760dc597414067b64 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 19:31:13 +0700 Subject: [PATCH 4/5] setNormalStyle > setDefaultParagraphStyle --- Classes/PHPWord.php | 6 +++--- Classes/PHPWord/Style.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 8f3cc922..51489224 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -199,13 +199,13 @@ class PHPWord } /** - * Set normal paragraph style definition to styles.xml + * Set default paragraph style definition to styles.xml * * @param array $styles Paragraph style definition */ - public function setNormalStyle($styles) + public function setDefaultParagraphStyle($styles) { - PHPWord_Style::setNormalStyle($styles); + PHPWord_Style::setDefaultParagraphStyle($styles); } /** diff --git a/Classes/PHPWord/Style.php b/Classes/PHPWord/Style.php index 6941b1bc..daabd46b 100755 --- a/Classes/PHPWord/Style.php +++ b/Classes/PHPWord/Style.php @@ -141,11 +141,11 @@ class PHPWord_Style } /** - * Set normal (default) paragraph style + * Set default paragraph style * * @param array $styles Paragraph style definition */ - public static function setNormalStyle($styles) + public static function setDefaultParagraphStyle($styles) { self::addParagraphStyle('Normal', $styles); } From 307f5689d9dd4f32fa54b5ea8a443f2306aca91d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 8 Mar 2014 01:26:02 +0700 Subject: [PATCH 5/5] Add unit tests for Shared/Font and Writer/Word2007/Styles --- Tests/PHPWord/Shared/FontTest.php | 48 +++++++++++++++++ Tests/PHPWord/Writer/Word2007/StylesTest.php | 55 ++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Tests/PHPWord/Shared/FontTest.php create mode 100644 Tests/PHPWord/Writer/Word2007/StylesTest.php diff --git a/Tests/PHPWord/Shared/FontTest.php b/Tests/PHPWord/Shared/FontTest.php new file mode 100644 index 00000000..09603d5f --- /dev/null +++ b/Tests/PHPWord/Shared/FontTest.php @@ -0,0 +1,48 @@ +assertEquals($original * 16 / 12, $result); + + $result = PHPWord_Shared_Font::inchSizeToPixels($original); + $this->assertEquals($original * 96, $result); + + $result = PHPWord_Shared_Font::centimeterSizeToPixels($original); + $this->assertEquals($original * 37.795275591, $result); + + $result = PHPWord_Shared_Font::centimeterSizeToTwips($original); + $this->assertEquals($original * 565.217, $result); + + $result = PHPWord_Shared_Font::inchSizeToTwips($original); + $this->assertEquals($original * 565.217 * 2.54, $result); + + $result = PHPWord_Shared_Font::pixelSizeToTwips($original); + $this->assertEquals($original * 565.217 / 37.795275591, $result); + + $result = PHPWord_Shared_Font::pointSizeToTwips($original); + $this->assertEquals($original * 20, $result); + } + +} diff --git a/Tests/PHPWord/Writer/Word2007/StylesTest.php b/Tests/PHPWord/Writer/Word2007/StylesTest.php new file mode 100644 index 00000000..42bde30f --- /dev/null +++ b/Tests/PHPWord/Writer/Word2007/StylesTest.php @@ -0,0 +1,55 @@ + 'both'); + $baseStyle = array('basedOn' => 'Normal'); + $newStyle = array('basedOn' => 'Base Style', 'next' => 'Normal'); + $PHPWord->setDefaultParagraphStyle($defaultStyle); + $PHPWord->addParagraphStyle('Base Style', $baseStyle); + $PHPWord->addParagraphStyle('New Style', $newStyle); + $doc = TestHelperDOCX::getDocument($PHPWord); + $file = 'word/styles.xml'; + + // Normal style generated? + $path = '/w:styles/w:style[@w:styleId="Normal"]/w:name'; + $element = $doc->getElement($path, $file); + $this->assertEquals('Normal', $element->getAttribute('w:val')); + + // Parent style referenced? + $path = '/w:styles/w:style[@w:styleId="New Style"]/w:basedOn'; + $element = $doc->getElement($path, $file); + $this->assertEquals('Base Style', $element->getAttribute('w:val')); + + // Next paragraph style correct? + $path = '/w:styles/w:style[@w:styleId="New Style"]/w:next'; + $element = $doc->getElement($path, $file); + $this->assertEquals('Normal', $element->getAttribute('w:val')); + } + +}