Skip to content

Commit e89e11f

Browse files
committed
Only add 240 twips when in auto lineRule
1 parent ea6edf9 commit e89e11f

File tree

8 files changed

+43
-77
lines changed

8 files changed

+43
-77
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
1010
### Fixed
1111
- Fix regex in `cloneBlock` function @nicoder #1269
1212
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
13+
- 240 twips are being added to line spacing, should not happen when using lineRule fixed @troosan #1508 #1505
1314

1415
v0.15.0 (14 Jul 2018)
1516
----------------------

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ $objWriter->save('helloWorld.html');
161161
```
162162

163163
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.
164-
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.
164+
You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) for more detail.
165165

166166
## Contributing
167167

docs/styles.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Available Paragraph style options:
8080
- ``pageBreakBefore``. Start paragraph on next page, *true* or *false*.
8181
- ``spaceBefore``. Space before paragraph in *twip*.
8282
- ``spaceAfter``. Space after paragraph in *twip*.
83-
- ``spacing``. Space between lines.
83+
- ``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.
8484
- ``spacingLineRule``. Line Spacing Rule. *auto*, *exact*, *atLeast*
8585
- ``suppressAutoHyphens``. Hyphenation for paragraph, *true* or *false*.
8686
- ``tabs``. Set of custom tab stops.

samples/Sample_01_SimpleText.php

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,79 +9,18 @@
99
$languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB);
1010

1111
$phpWord = new \PhpOffice\PhpWord\PhpWord();
12-
$phpWord->getSettings()->setThemeFontLang($languageEnGb);
1312

14-
$fontStyleName = 'rStyle';
15-
$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
16-
17-
$paragraphStyleName = 'pStyle';
18-
$phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));
19-
20-
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
21-
22-
// New portrait section
2313
$section = $phpWord->addSection();
24-
25-
// Simple text
26-
$section->addTitle('Welcome to PhpWord', 1);
27-
$section->addText('Hello World!');
28-
29-
// $pStyle = new Font();
30-
// $pStyle->setLang()
31-
$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE));
32-
33-
// Two text break
34-
$section->addTextBreak(2);
35-
36-
// Define styles
37-
$section->addText('I am styled by a font style definition.', $fontStyleName);
38-
$section->addText('I am styled by a paragraph style definition.', null, $paragraphStyleName);
39-
$section->addText('I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);
40-
41-
$section->addTextBreak();
42-
43-
// Inline font style
44-
$fontStyle['name'] = 'Times New Roman';
45-
$fontStyle['size'] = 20;
46-
47-
$textrun = $section->addTextRun();
48-
$textrun->addText('I am inline styled ', $fontStyle);
49-
$textrun->addText('with ');
50-
$textrun->addText('color', array('color' => '996699'));
51-
$textrun->addText(', ');
52-
$textrun->addText('bold', array('bold' => true));
53-
$textrun->addText(', ');
54-
$textrun->addText('italic', array('italic' => true));
55-
$textrun->addText(', ');
56-
$textrun->addText('underline', array('underline' => 'dash'));
57-
$textrun->addText(', ');
58-
$textrun->addText('strikethrough', array('strikethrough' => true));
59-
$textrun->addText(', ');
60-
$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));
61-
$textrun->addText(', ');
62-
$textrun->addText('superScript', array('superScript' => true));
63-
$textrun->addText(', ');
64-
$textrun->addText('subScript', array('subScript' => true));
65-
$textrun->addText(', ');
66-
$textrun->addText('smallCaps', array('smallCaps' => true));
67-
$textrun->addText(', ');
68-
$textrun->addText('allCaps', array('allCaps' => true));
69-
$textrun->addText(', ');
70-
$textrun->addText('fgColor', array('fgColor' => 'yellow'));
71-
$textrun->addText(', ');
72-
$textrun->addText('scale', array('scale' => 200));
73-
$textrun->addText(', ');
74-
$textrun->addText('spacing', array('spacing' => 120));
75-
$textrun->addText(', ');
76-
$textrun->addText('kerning', array('kerning' => 10));
77-
$textrun->addText('. ');
78-
79-
// Link
80-
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
81-
$section->addTextBreak();
82-
83-
// Image
84-
$section->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18));
14+
$section->addText(
15+
'"Learn from yesterday, live for today, hope for tomorrow. '
16+
. 'The important thing is not to stop questioning." '
17+
. '(Albert Einstein)',
18+
[],
19+
[
20+
'spacing' => \PhpOffice\Common\Font::pointSizeToTwips(1),
21+
'spacingLineRule' => \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT
22+
]
23+
);
8524

8625
// Save file
8726
echo write($phpWord, basename(__FILE__, '.php'), $writers);

src/PhpWord/Style/Paragraph.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ public function setStyleValue($key, $value)
199199
$key = Text::removeUnderscorePrefix($key);
200200
if ('indent' == $key || 'hanging' == $key) {
201201
$value = $value * 720;
202-
} elseif ('spacing' == $key) {
203-
$value += 240; // because line height of 1 matches 240 twips
204202
}
205203

206204
return parent::setStyleValue($key, $value);

src/PhpWord/Writer/Word2007/Style/Spacing.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function write()
4444
$xmlWriter->writeAttributeIf(!is_null($after), 'w:after', $this->convertTwip($after));
4545

4646
$line = $style->getLine();
47+
//if linerule is auto, the spacing is supposed to include the height of the line itself, which is 240 twips
48+
if (null !== $line && 'auto' === $style->getLineRule()) {
49+
$line += 240;
50+
}
4751
$xmlWriter->writeAttributeIf(!is_null($line), 'w:line', $line);
4852

4953
$xmlWriter->writeAttributeIf(!is_null($line), 'w:lineRule', $style->getLineRule());

tests/PhpWord/Style/ParagraphTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ public function testSetStyleValueNormal()
9191
$object->setStyleValue("$key", $value);
9292
if ('indent' == $key || 'hanging' == $key) {
9393
$value = $value * 720;
94-
} elseif ('spacing' == $key) {
95-
$value += 240;
9694
}
9795
$this->assertEquals($value, $object->$get());
9896
}

tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ public function testParagraphNumbering()
5151
$this->assertTrue($doc->elementExists($path));
5252
}
5353

54+
public function testLineSpacingExact()
55+
{
56+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
57+
$section = $phpWord->addSection();
58+
$section->addText('test', null, array('spacing' => 240, 'spacingLineRule' => 'exact'));
59+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
60+
61+
$path = '/w:document/w:body/w:p/w:pPr/w:spacing';
62+
$this->assertTrue($doc->elementExists($path));
63+
$this->assertEquals('exact', $doc->getElementAttribute($path, 'w:lineRule'));
64+
$this->assertEquals('240', $doc->getElementAttribute($path, 'w:line'));
65+
}
66+
67+
public function testLineSpacingAuto()
68+
{
69+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
70+
$section = $phpWord->addSection();
71+
$section->addText('test', null, array('spacing' => 240, 'spacingLineRule' => 'auto'));
72+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
73+
74+
$path = '/w:document/w:body/w:p/w:pPr/w:spacing';
75+
$this->assertTrue($doc->elementExists($path));
76+
$this->assertEquals('auto', $doc->getElementAttribute($path, 'w:lineRule'));
77+
$this->assertEquals('480', $doc->getElementAttribute($path, 'w:line'));
78+
}
79+
5480
public function testSuppressAutoHyphens()
5581
{
5682
$paragraphStyle = new ParagraphStyle();

0 commit comments

Comments
 (0)