Line spacing is wrong when using "exact" line spacing rule (#1509)

* Only add 240 twips when in auto lineRule
* don't add 1 line when using EXACT line spacing rule
* fix style & scrutinizer warning
This commit is contained in:
troosan 2018-12-03 16:09:20 +01:00 committed by GitHub
parent c15d4d155d
commit af5a271e9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 16 deletions

View File

@ -3,7 +3,7 @@ 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
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
@ -11,6 +11,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 #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

View File

@ -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

View File

@ -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*.

View File

@ -23,6 +23,7 @@ use PhpOffice\PhpWord\Element\Table;
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 @@ class Html
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);

View File

@ -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

View File

@ -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 @@ class Paragraph extends Border
$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 @@ class Paragraph extends Border
/**
* Get spacing between lines
*
* @return int
* @return int|float
*/
public function getSpacing()
{
@ -489,7 +487,7 @@ class Paragraph extends Border
/**
* Set spacing between lines
*
* @param int $value
* @param int|float $value
* @return self
*/
public function setSpacing($value = null)
@ -547,7 +545,8 @@ class Paragraph extends Border
}
$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;
}

View File

@ -44,6 +44,10 @@ class Spacing extends AbstractStyle
$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());

View File

@ -91,8 +91,6 @@ class ParagraphTest extends \PHPUnit\Framework\TestCase
$object->setStyleValue("$key", $value);
if ('indent' == $key || 'hanging' == $key) {
$value = $value * 720;
} elseif ('spacing' == $key) {
$value += 240;
}
$this->assertEquals($value, $object->$get());
}

View File

@ -51,6 +51,32 @@ class ParagraphTest extends \PHPUnit\Framework\TestCase
$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();