Skip to content

Commit

Permalink
Revert "Only add 240 twips when in auto lineRule"
Browse files Browse the repository at this point in the history
This reverts commit e89e11f.
  • Loading branch information
troosan committed Nov 17, 2018
1 parent e89e11f commit eaf027b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 43 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
----------------------
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/) 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

Expand Down
2 changes: 1 addition & 1 deletion docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
81 changes: 71 additions & 10 deletions samples/Sample_01_SimpleText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/PhpWord/Writer/Word2007/Style/Spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpWord/Style/ParagraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
26 changes: 0 additions & 26 deletions tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit eaf027b

Please sign in to comment.