Skip to content

Commit 69a8192

Browse files
committed
Added line height methods to mirror the line height settings in Word in the paragraph styling
1 parent 3519477 commit 69a8192

File tree

3 files changed

+101
-34
lines changed

3 files changed

+101
-34
lines changed

Classes/PHPWord/Section/Text.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PHPWord_Section_Text
4848
/**
4949
* Paragraph style
5050
*
51-
* @var PHPWord_Style_Font
51+
* @var \PHPWord_Style_Paragraph
5252
*/
5353
private $_styleParagraph;
5454

@@ -116,22 +116,29 @@ public function getParagraphStyle()
116116
/**
117117
* Set Paragraph style
118118
*
119-
* @return PHPWord_Style_Paragraph
119+
* @param array|\PHPWord_Style_Paragraph $styleParagraph
120+
* @return \PHPWord_Style_Paragraph
121+
* @throws \Exception
120122
*/
121123
public function setParagraphStyle($styleParagraph)
122124
{
123125
if (is_array($styleParagraph)) {
124126
$this->_styleParagraph = new PHPWord_Style_Paragraph();
125127

126128
foreach ($styleParagraph as $key => $value) {
127-
if (substr($key, 0, 1) != '_') {
129+
if ($key === 'line-height') {
130+
null;
131+
} elseif (substr($key, 0, 1) != '_') {
128132
$key = '_' . $key;
129133
}
130134
$this->_styleParagraph->setStyleValue($key, $value);
131135
}
132-
} else {
136+
} elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
133137
$this->_styleParagraph = $styleParagraph;
138+
} else {
139+
throw new Exception('Expected array or PHPWord_Style_Paragraph');
134140
}
141+
return $this->_styleParagraph;
135142
}
136143

137144
/**
@@ -143,4 +150,4 @@ public function getText()
143150
{
144151
return $this->_text;
145152
}
146-
}
153+
}

Classes/PHPWord/Style/Paragraph.php

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@
2525
* @version 0.7.0
2626
*/
2727

28+
use PHPWord\Exceptions\InvalidStyleException;
29+
2830
/**
2931
* PHPWord_Style_Paragraph
3032
*/
3133
class PHPWord_Style_Paragraph
3234
{
35+
const LINE_HEIGHT = 240;
36+
37+
/*
38+
* Text line height
39+
*
40+
* @var int
41+
*/
42+
private $lineHeight;
3343

3444
/**
3545
* Paragraph alignment
@@ -85,7 +95,7 @@ class PHPWord_Style_Paragraph
8595
*
8696
* @var string
8797
*/
88-
private $_basedOn;
98+
private $_basedOn = 'Normal';
8999

90100
/**
91101
* Style for next paragraph
@@ -99,63 +109,46 @@ class PHPWord_Style_Paragraph
99109
*
100110
* @var bool
101111
*/
102-
private $_widowControl;
112+
private $_widowControl = true;
103113

104114
/**
105115
* Keep paragraph with next paragraph
106116
*
107117
* @var bool
108118
*/
109-
private $_keepNext;
119+
private $_keepNext = false;
110120

111121
/**
112122
* Keep all lines on one page
113123
*
114124
* @var bool
115125
*/
116-
private $_keepLines;
126+
private $_keepLines = false;
117127

118128
/**
119129
* Start paragraph on next page
120130
*
121131
* @var bool
122132
*/
123-
private $_pageBreakBefore;
124-
125-
/**
126-
* New Paragraph Style
127-
*/
128-
public function __construct()
129-
{
130-
$this->_align = null;
131-
$this->_spaceBefore = null;
132-
$this->_spaceAfter = null;
133-
$this->_spacing = null;
134-
$this->_tabs = null;
135-
$this->_indent = null;
136-
$this->_hanging = null;
137-
$this->_basedOn = 'Normal';
138-
$this->_next = null;
139-
$this->_widowControl = true;
140-
$this->_keepNext = false;
141-
$this->_keepLines = false;
142-
$this->_pageBreakBefore = false;
143-
}
133+
private $_pageBreakBefore = false;
144134

145135
/**
146136
* Set Style value
147137
*
148-
* @param string $key
149-
* @param mixed $value
138+
* @param string $key
139+
* @param mixed $value
150140
*/
151141
public function setStyleValue($key, $value)
152142
{
153143
if ($key == '_indent' || $key == '_hanging') {
154144
$value = $value * 720;
155-
}
156-
if ($key == '_spacing') {
145+
} elseif ($key == '_spacing') {
157146
$value += 240; // because line height of 1 matches 240 twips
147+
} elseif ($key === 'line-height') {
148+
$this->setLineHeight($value);
149+
return;
158150
}
151+
$this->$key = $value;
159152
$method = 'set' . substr($key, 1);
160153
if (method_exists($this, $method)) {
161154
$this->$method($value);
@@ -466,4 +459,33 @@ public function setPageBreakBefore($pValue = false)
466459
return $this;
467460
}
468461

462+
/**
463+
* Set the line height
464+
*
465+
* @param int|float|string $lineHeight
466+
* @return $this
467+
* @throws \PHPWord\Exceptions\InvalidStyleException
468+
*/
469+
public function setLineHeight($lineHeight)
470+
{
471+
if (is_string($lineHeight)) {
472+
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
473+
}
474+
475+
if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
476+
throw new InvalidStyleException('Line height must be a valid number');
477+
}
478+
479+
$this->lineHeight = $lineHeight;
480+
$this->setSpacing($lineHeight * self::LINE_HEIGHT);
481+
return $this;
482+
}
483+
484+
/**
485+
* @return int
486+
*/
487+
public function getLineHeight()
488+
{
489+
return $this->lineHeight;
490+
}
469491
}

Tests/PHPWord/Style/ParagraphTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
namespace PHPWord\Tests\Style;
33

44
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
56
use PHPWord_Style_Paragraph;
67
use PHPWord_Style_Tab;
8+
use PHPWord\Tests\TestHelperDOCX;
79

810
/**
911
* Class ParagraphTest
@@ -13,6 +15,11 @@
1315
*/
1416
class ParagraphTest extends \PHPUnit_Framework_TestCase
1517
{
18+
public function tearDown()
19+
{
20+
TestHelperDOCX::clear();
21+
}
22+
1623
/**
1724
* Test setting style values with null or empty value
1825
*/
@@ -85,4 +92,35 @@ public function testTabs()
8592
));
8693
$this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs());
8794
}
95+
96+
public function testLineHeight()
97+
{
98+
$PHPWord = new PHPWord();
99+
$section = $PHPWord->createSection();
100+
101+
// Test style array
102+
$text = $section->addText('This is a test', array(), array(
103+
'line-height' => 2.0
104+
));
105+
106+
$doc = TestHelperDOCX::getDocument($PHPWord);
107+
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
108+
109+
$lineHeight = $element->getAttribute('w:line');
110+
$lineRule = $element->getAttribute('w:lineRule');
111+
112+
$this->assertEquals(480, $lineHeight);
113+
$this->assertEquals('auto', $lineRule);
114+
115+
// Test setter
116+
$text->getParagraphStyle()->setLineHeight(3.0);
117+
$doc = TestHelperDOCX::getDocument($PHPWord);
118+
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
119+
120+
$lineHeight = $element->getAttribute('w:line');
121+
$lineRule = $element->getAttribute('w:lineRule');
122+
123+
$this->assertEquals(720, $lineHeight);
124+
$this->assertEquals('auto', $lineRule);
125+
}
88126
}

0 commit comments

Comments
 (0)