From e89e11f9dc6cc3f8125d6f27d0eea49b293eb3cf Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 17 Nov 2018 14:28:18 +0100 Subject: [PATCH 1/2] Only add 240 twips when in auto lineRule --- CHANGELOG.md | 1 + README.md | 2 +- docs/styles.rst | 2 +- samples/Sample_01_SimpleText.php | 81 +++---------------- src/PhpWord/Style/Paragraph.php | 2 - src/PhpWord/Writer/Word2007/Style/Spacing.php | 4 + tests/PhpWord/Style/ParagraphTest.php | 2 - .../Writer/Word2007/Style/ParagraphTest.php | 26 ++++++ 8 files changed, 43 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abf3483418..86f774e3c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018) ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 - HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436 +- 240 twips are being added to line spacing, should not happen when using lineRule fixed @troosan #1508 #1505 v0.15.0 (14 Jul 2018) ---------------------- diff --git a/README.md b/README.md index 7531a6bc11..0509bce292 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ $objWriter->save('helloWorld.html'); ``` More examples are provided in the [samples folder](samples/). For an easy access to those samples launch `php -S localhost:8000` in the samples directory then browse to [http://localhost:8000](http://localhost:8000) to view the samples. -You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) and the [API Documentation](http://phpoffice.github.io/PHPWord/docs/master/) for more detail. +You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) for more detail. ## Contributing diff --git a/docs/styles.rst b/docs/styles.rst index 0336642722..070007b4ba 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -80,7 +80,7 @@ Available Paragraph style options: - ``pageBreakBefore``. Start paragraph on next page, *true* or *false*. - ``spaceBefore``. Space before paragraph in *twip*. - ``spaceAfter``. Space after paragraph in *twip*. -- ``spacing``. Space between lines. +- ``spacing``. Space between lines in *twip*. If spacingLineRule is auto, 240 (height of 1 line) will be added, so if you want a double line height, set this to 240. - ``spacingLineRule``. Line Spacing Rule. *auto*, *exact*, *atLeast* - ``suppressAutoHyphens``. Hyphenation for paragraph, *true* or *false*. - ``tabs``. Set of custom tab stops. diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 8af44d20aa..7559a20ac0 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -9,79 +9,18 @@ $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB); $phpWord = new \PhpOffice\PhpWord\PhpWord(); -$phpWord->getSettings()->setThemeFontLang($languageEnGb); -$fontStyleName = 'rStyle'; -$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true)); - -$paragraphStyleName = 'pStyle'; -$phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100)); - -$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); - -// New portrait section $section = $phpWord->addSection(); - -// Simple text -$section->addTitle('Welcome to PhpWord', 1); -$section->addText('Hello World!'); - -// $pStyle = new Font(); -// $pStyle->setLang() -$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE)); - -// Two text break -$section->addTextBreak(2); - -// Define styles -$section->addText('I am styled by a font style definition.', $fontStyleName); -$section->addText('I am styled by a paragraph style definition.', null, $paragraphStyleName); -$section->addText('I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName); - -$section->addTextBreak(); - -// Inline font style -$fontStyle['name'] = 'Times New Roman'; -$fontStyle['size'] = 20; - -$textrun = $section->addTextRun(); -$textrun->addText('I am inline styled ', $fontStyle); -$textrun->addText('with '); -$textrun->addText('color', array('color' => '996699')); -$textrun->addText(', '); -$textrun->addText('bold', array('bold' => true)); -$textrun->addText(', '); -$textrun->addText('italic', array('italic' => true)); -$textrun->addText(', '); -$textrun->addText('underline', array('underline' => 'dash')); -$textrun->addText(', '); -$textrun->addText('strikethrough', array('strikethrough' => true)); -$textrun->addText(', '); -$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true)); -$textrun->addText(', '); -$textrun->addText('superScript', array('superScript' => true)); -$textrun->addText(', '); -$textrun->addText('subScript', array('subScript' => true)); -$textrun->addText(', '); -$textrun->addText('smallCaps', array('smallCaps' => true)); -$textrun->addText(', '); -$textrun->addText('allCaps', array('allCaps' => true)); -$textrun->addText(', '); -$textrun->addText('fgColor', array('fgColor' => 'yellow')); -$textrun->addText(', '); -$textrun->addText('scale', array('scale' => 200)); -$textrun->addText(', '); -$textrun->addText('spacing', array('spacing' => 120)); -$textrun->addText(', '); -$textrun->addText('kerning', array('kerning' => 10)); -$textrun->addText('. '); - -// Link -$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub'); -$section->addTextBreak(); - -// Image -$section->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18)); +$section->addText( + '"Learn from yesterday, live for today, hope for tomorrow. ' + . 'The important thing is not to stop questioning." ' + . '(Albert Einstein)', + [], + [ + 'spacing' => \PhpOffice\Common\Font::pointSizeToTwips(1), + 'spacingLineRule' => \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT + ] + ); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index ac58768695..9e2c722e84 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -199,8 +199,6 @@ public function setStyleValue($key, $value) $key = Text::removeUnderscorePrefix($key); if ('indent' == $key || 'hanging' == $key) { $value = $value * 720; - } elseif ('spacing' == $key) { - $value += 240; // because line height of 1 matches 240 twips } return parent::setStyleValue($key, $value); diff --git a/src/PhpWord/Writer/Word2007/Style/Spacing.php b/src/PhpWord/Writer/Word2007/Style/Spacing.php index 0185cbcc7a..ef897970c3 100644 --- a/src/PhpWord/Writer/Word2007/Style/Spacing.php +++ b/src/PhpWord/Writer/Word2007/Style/Spacing.php @@ -44,6 +44,10 @@ public function write() $xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after)); $line = $style->getLine(); + //if linerule is auto, the spacing is supposed to include the height of the line itself, which is 240 twips + if (null !== $line && 'auto' === $style->getLineRule()) { + $line += 240; + } $xmlWriter->writeAttributeIf(!is_null($line), 'w:line', $line); $xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getLineRule()); diff --git a/tests/PhpWord/Style/ParagraphTest.php b/tests/PhpWord/Style/ParagraphTest.php index 624607381e..4fa0ef5a70 100644 --- a/tests/PhpWord/Style/ParagraphTest.php +++ b/tests/PhpWord/Style/ParagraphTest.php @@ -91,8 +91,6 @@ public function testSetStyleValueNormal() $object->setStyleValue("$key", $value); if ('indent' == $key || 'hanging' == $key) { $value = $value * 720; - } elseif ('spacing' == $key) { - $value += 240; } $this->assertEquals($value, $object->$get()); } diff --git a/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php b/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php index 8443bbcaad..843f98807f 100644 --- a/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php +++ b/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php @@ -51,6 +51,32 @@ public function testParagraphNumbering() $this->assertTrue($doc->elementExists($path)); } + public function testLineSpacingExact() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $section->addText('test', null, array('spacing' => 240, 'spacingLineRule' => 'exact')); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $path = '/w:document/w:body/w:p/w:pPr/w:spacing'; + $this->assertTrue($doc->elementExists($path)); + $this->assertEquals('exact', $doc->getElementAttribute($path, 'w:lineRule')); + $this->assertEquals('240', $doc->getElementAttribute($path, 'w:line')); + } + + public function testLineSpacingAuto() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $section->addText('test', null, array('spacing' => 240, 'spacingLineRule' => 'auto')); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $path = '/w:document/w:body/w:p/w:pPr/w:spacing'; + $this->assertTrue($doc->elementExists($path)); + $this->assertEquals('auto', $doc->getElementAttribute($path, 'w:lineRule')); + $this->assertEquals('480', $doc->getElementAttribute($path, 'w:line')); + } + public function testSuppressAutoHyphens() { $paragraphStyle = new ParagraphStyle(); From eaf027b8dcda004fec563f44893e56e0048c4d76 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 17 Nov 2018 14:32:21 +0100 Subject: [PATCH 2/2] Revert "Only add 240 twips when in auto lineRule" This reverts commit e89e11f9dc6cc3f8125d6f27d0eea49b293eb3cf. --- CHANGELOG.md | 1 - README.md | 2 +- docs/styles.rst | 2 +- samples/Sample_01_SimpleText.php | 81 ++++++++++++++++--- src/PhpWord/Style/Paragraph.php | 2 + src/PhpWord/Writer/Word2007/Style/Spacing.php | 4 - tests/PhpWord/Style/ParagraphTest.php | 2 + .../Writer/Word2007/Style/ParagraphTest.php | 26 ------ 8 files changed, 77 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86f774e3c1..abf3483418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,6 @@ v0.16.0 (xx xxx 2018) ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 - HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436 -- 240 twips are being added to line spacing, should not happen when using lineRule fixed @troosan #1508 #1505 v0.15.0 (14 Jul 2018) ---------------------- diff --git a/README.md b/README.md index 0509bce292..7531a6bc11 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ $objWriter->save('helloWorld.html'); ``` More examples are provided in the [samples folder](samples/). For an easy access to those samples launch `php -S localhost:8000` in the samples directory then browse to [http://localhost:8000](http://localhost:8000) to view the samples. -You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) for more detail. +You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) and the [API Documentation](http://phpoffice.github.io/PHPWord/docs/master/) for more detail. ## Contributing diff --git a/docs/styles.rst b/docs/styles.rst index 070007b4ba..0336642722 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -80,7 +80,7 @@ Available Paragraph style options: - ``pageBreakBefore``. Start paragraph on next page, *true* or *false*. - ``spaceBefore``. Space before paragraph in *twip*. - ``spaceAfter``. Space after paragraph in *twip*. -- ``spacing``. Space between lines in *twip*. If spacingLineRule is auto, 240 (height of 1 line) will be added, so if you want a double line height, set this to 240. +- ``spacing``. Space between lines. - ``spacingLineRule``. Line Spacing Rule. *auto*, *exact*, *atLeast* - ``suppressAutoHyphens``. Hyphenation for paragraph, *true* or *false*. - ``tabs``. Set of custom tab stops. diff --git a/samples/Sample_01_SimpleText.php b/samples/Sample_01_SimpleText.php index 7559a20ac0..8af44d20aa 100644 --- a/samples/Sample_01_SimpleText.php +++ b/samples/Sample_01_SimpleText.php @@ -9,18 +9,79 @@ $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB); $phpWord = new \PhpOffice\PhpWord\PhpWord(); +$phpWord->getSettings()->setThemeFontLang($languageEnGb); +$fontStyleName = 'rStyle'; +$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true)); + +$paragraphStyleName = 'pStyle'; +$phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100)); + +$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); + +// New portrait section $section = $phpWord->addSection(); -$section->addText( - '"Learn from yesterday, live for today, hope for tomorrow. ' - . 'The important thing is not to stop questioning." ' - . '(Albert Einstein)', - [], - [ - 'spacing' => \PhpOffice\Common\Font::pointSizeToTwips(1), - 'spacingLineRule' => \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT - ] - ); + +// Simple text +$section->addTitle('Welcome to PhpWord', 1); +$section->addText('Hello World!'); + +// $pStyle = new Font(); +// $pStyle->setLang() +$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE)); + +// Two text break +$section->addTextBreak(2); + +// Define styles +$section->addText('I am styled by a font style definition.', $fontStyleName); +$section->addText('I am styled by a paragraph style definition.', null, $paragraphStyleName); +$section->addText('I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName); + +$section->addTextBreak(); + +// Inline font style +$fontStyle['name'] = 'Times New Roman'; +$fontStyle['size'] = 20; + +$textrun = $section->addTextRun(); +$textrun->addText('I am inline styled ', $fontStyle); +$textrun->addText('with '); +$textrun->addText('color', array('color' => '996699')); +$textrun->addText(', '); +$textrun->addText('bold', array('bold' => true)); +$textrun->addText(', '); +$textrun->addText('italic', array('italic' => true)); +$textrun->addText(', '); +$textrun->addText('underline', array('underline' => 'dash')); +$textrun->addText(', '); +$textrun->addText('strikethrough', array('strikethrough' => true)); +$textrun->addText(', '); +$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true)); +$textrun->addText(', '); +$textrun->addText('superScript', array('superScript' => true)); +$textrun->addText(', '); +$textrun->addText('subScript', array('subScript' => true)); +$textrun->addText(', '); +$textrun->addText('smallCaps', array('smallCaps' => true)); +$textrun->addText(', '); +$textrun->addText('allCaps', array('allCaps' => true)); +$textrun->addText(', '); +$textrun->addText('fgColor', array('fgColor' => 'yellow')); +$textrun->addText(', '); +$textrun->addText('scale', array('scale' => 200)); +$textrun->addText(', '); +$textrun->addText('spacing', array('spacing' => 120)); +$textrun->addText(', '); +$textrun->addText('kerning', array('kerning' => 10)); +$textrun->addText('. '); + +// Link +$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub'); +$section->addTextBreak(); + +// Image +$section->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18)); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 9e2c722e84..ac58768695 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -199,6 +199,8 @@ public function setStyleValue($key, $value) $key = Text::removeUnderscorePrefix($key); if ('indent' == $key || 'hanging' == $key) { $value = $value * 720; + } elseif ('spacing' == $key) { + $value += 240; // because line height of 1 matches 240 twips } return parent::setStyleValue($key, $value); diff --git a/src/PhpWord/Writer/Word2007/Style/Spacing.php b/src/PhpWord/Writer/Word2007/Style/Spacing.php index ef897970c3..0185cbcc7a 100644 --- a/src/PhpWord/Writer/Word2007/Style/Spacing.php +++ b/src/PhpWord/Writer/Word2007/Style/Spacing.php @@ -44,10 +44,6 @@ public function write() $xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after)); $line = $style->getLine(); - //if linerule is auto, the spacing is supposed to include the height of the line itself, which is 240 twips - if (null !== $line && 'auto' === $style->getLineRule()) { - $line += 240; - } $xmlWriter->writeAttributeIf(!is_null($line), 'w:line', $line); $xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getLineRule()); diff --git a/tests/PhpWord/Style/ParagraphTest.php b/tests/PhpWord/Style/ParagraphTest.php index 4fa0ef5a70..624607381e 100644 --- a/tests/PhpWord/Style/ParagraphTest.php +++ b/tests/PhpWord/Style/ParagraphTest.php @@ -91,6 +91,8 @@ public function testSetStyleValueNormal() $object->setStyleValue("$key", $value); if ('indent' == $key || 'hanging' == $key) { $value = $value * 720; + } elseif ('spacing' == $key) { + $value += 240; } $this->assertEquals($value, $object->$get()); } diff --git a/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php b/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php index 843f98807f..8443bbcaad 100644 --- a/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php +++ b/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php @@ -51,32 +51,6 @@ public function testParagraphNumbering() $this->assertTrue($doc->elementExists($path)); } - public function testLineSpacingExact() - { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); - $section = $phpWord->addSection(); - $section->addText('test', null, array('spacing' => 240, 'spacingLineRule' => 'exact')); - $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); - - $path = '/w:document/w:body/w:p/w:pPr/w:spacing'; - $this->assertTrue($doc->elementExists($path)); - $this->assertEquals('exact', $doc->getElementAttribute($path, 'w:lineRule')); - $this->assertEquals('240', $doc->getElementAttribute($path, 'w:line')); - } - - public function testLineSpacingAuto() - { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); - $section = $phpWord->addSection(); - $section->addText('test', null, array('spacing' => 240, 'spacingLineRule' => 'auto')); - $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); - - $path = '/w:document/w:body/w:p/w:pPr/w:spacing'; - $this->assertTrue($doc->elementExists($path)); - $this->assertEquals('auto', $doc->getElementAttribute($path, 'w:lineRule')); - $this->assertEquals('480', $doc->getElementAttribute($path, 'w:line')); - } - public function testSuppressAutoHyphens() { $paragraphStyle = new ParagraphStyle();