diff --git a/CHANGELOG.md b/CHANGELOG.md index d1db7a5e8e..25a0e4179a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,13 +3,14 @@ Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -v0.16.0 (xx xxx 2018) +v0.16.0 (xx dec 2018) ---------------------- ### Added ### 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 #1509 #1505 - Adding table layout to the generated HTML @aarangara #1441 - Fix loading of Sharepoint document @Garrcomm #1498 - RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493 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 8c5de7cb9d..60d6a0c1b5 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -81,7 +81,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* See ``\PhpOffice\PhpWord\SimpleType\LineSpacingRule`` class constants for possible values. - ``suppressAutoHyphens``. Hyphenation for paragraph, *true* or *false*. diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 60fd7e165e..3f07a0588d 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -23,6 +23,7 @@ use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\NumberFormat; +use PhpOffice\PhpWord\Style\Paragraph; /** * Common Html functions @@ -533,17 +534,25 @@ private static function parseStyle($attribute, $styles) case 'line-height': $matches = array(); if (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) { + //matches number with a unit, e.g. 12px, 15pt, 20mm, ... $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT; - $spacing = Converter::cssToTwip($matches[1]) / \PhpOffice\PhpWord\Style\Paragraph::LINE_HEIGHT; + $spacing = Converter::cssToTwip($matches[1]); } elseif (preg_match('/([0-9]+)%/', $cValue, $matches)) { + //matches percentages $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; - $spacing = ((int) $matches[1]) / 100; + //we are subtracting 1 line height because the Spacing writer is adding one line + $spacing = ((((int) $matches[1]) / 100) * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; } else { + //any other, wich is a multiplier. E.g. 1.2 $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO; - $spacing = $cValue; + //we are subtracting 1 line height because the Spacing writer is adding one line + $spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT; } $styles['spacingLineRule'] = $spacingLineRule; - $styles['lineHeight'] = $spacing; + $styles['line-spacing'] = $spacing; + break; + case 'letter-spacing': + $styles['letter-spacing'] = Converter::cssToTwip($cValue); break; case 'text-indent': $styles['indentation']['firstLine'] = Converter::cssToTwip($cValue); diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index e9f3c9d672..d60ebafe15 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -80,7 +80,7 @@ class Font extends AbstractStyle * * @var array */ - protected $aliases = array('line-height' => 'lineHeight'); + protected $aliases = array('line-height' => 'lineHeight', 'letter-spacing' => 'spacing'); /** * Font style type diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index ac58768695..6e9aaf15dc 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -61,7 +61,7 @@ class Paragraph extends Border * * @var array */ - protected $aliases = array('line-height' => 'lineHeight'); + protected $aliases = array('line-height' => 'lineHeight', 'line-spacing' => 'spacing'); /** * Parent style @@ -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); @@ -479,7 +477,7 @@ public function setSpaceAfter($value = null) /** * Get spacing between lines * - * @return int + * @return int|float */ public function getSpacing() { @@ -489,7 +487,7 @@ public function getSpacing() /** * Set spacing between lines * - * @param int $value + * @param int|float $value * @return self */ public function setSpacing($value = null) @@ -547,7 +545,8 @@ public function setLineHeight($lineHeight) } $this->lineHeight = $lineHeight; - $this->setSpacing($lineHeight * self::LINE_HEIGHT); + $this->setSpacing(($lineHeight - 1) * self::LINE_HEIGHT); + $this->setSpacingLineRule(\PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO); return $this; } diff --git a/src/PhpWord/Writer/Word2007/Style/Spacing.php b/src/PhpWord/Writer/Word2007/Style/Spacing.php index 0185cbcc7a..fdfb89ab87 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 += \PhpOffice\PhpWord\Style\Paragraph::LINE_HEIGHT; + } $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();