Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Expand Down
17 changes: 13 additions & 4 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -479,7 +477,7 @@ public function setSpaceAfter($value = null)
/**
* Get spacing between lines
*
* @return int
* @return int|float
*/
public function getSpacing()
{
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 0 additions & 2 deletions tests/PhpWord/Style/ParagraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
26 changes: 26 additions & 0 deletions tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down