Skip to content

Commit 79ba793

Browse files
committed
Merge pull request #96 from gabrielbull/develop
Added line height methods to mirror the line height settings in Word in the paragraph styling
2 parents 8883667 + 70588f2 commit 79ba793

24 files changed

+1559
-1273
lines changed

Classes/PHPWord/Section/Footer/PreserveText.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ public function __construct($text = null, $styleFont = null, $styleParagraph = n
8989
$this->_styleParagraph = $styleParagraph;
9090
}
9191

92-
$pattern = '/({.*?})/';
93-
$this->_text = preg_split($pattern, $text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
92+
$matches = preg_split('/({.*?})/', $text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
93+
if (isset($matches[0])) {
94+
$this->_text = $matches[0];
95+
}
9496

9597
return $this;
9698
}

Classes/PHPWord/Section/Text.php

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,63 @@
3030
*/
3131
class PHPWord_Section_Text
3232
{
33-
3433
/**
3534
* Text content
3635
*
3736
* @var string
3837
*/
39-
private $_text;
38+
private $text;
4039

4140
/**
4241
* Text style
4342
*
4443
* @var PHPWord_Style_Font
4544
*/
46-
private $_styleFont;
45+
private $fontStyle;
4746

4847
/**
4948
* Paragraph style
5049
*
51-
* @var \PHPWord_Style_Paragraph
50+
* @var PHPWord_Style_Paragraph
5251
*/
53-
private $_styleParagraph;
54-
52+
private $paragraphStyle;
5553

5654
/**
5755
* Create a new Text Element
5856
*
59-
* @var string $text
60-
* @var mixed $style
57+
* @param string $text
58+
* @param null|array|\PHPWord_Style_Font $fontStyle
59+
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
6160
*/
62-
public function __construct($text = null, $styleFont = null, $styleParagraph = null)
61+
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
6362
{
64-
// Set font style
65-
$this->setFontStyle($styleFont);
66-
67-
// Set paragraph style
68-
$this->setParagraphStyle($styleParagraph);
69-
70-
$this->_text = $text;
63+
$this->setText($text);
64+
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
65+
$this->setFontStyle($fontStyle, $paragraphStyle);
66+
}
7167

72-
return $this;
68+
/**
69+
* Set Text style
70+
*
71+
* @param null|array|\PHPWord_Style_Font $style
72+
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
73+
* @return PHPWord_Style_Font
74+
*/
75+
public function setFontStyle($style = null, $paragraphStyle = null)
76+
{
77+
if ($style instanceof PHPWord_Style_Font) {
78+
$this->fontStyle = $style;
79+
$this->setParagraphStyle($paragraphStyle);
80+
} elseif (is_array($style)) {
81+
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
82+
$this->fontStyle->setArrayStyle($style);
83+
} elseif (null === $style) {
84+
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
85+
} else {
86+
$this->fontStyle = $style;
87+
$this->setParagraphStyle($paragraphStyle);
88+
}
89+
return $this->fontStyle;
7390
}
7491

7592
/**
@@ -79,28 +96,28 @@ public function __construct($text = null, $styleFont = null, $styleParagraph = n
7996
*/
8097
public function getFontStyle()
8198
{
82-
return $this->_styleFont;
99+
return $this->fontStyle;
83100
}
84101

85102
/**
86-
* Set Text style
103+
* Set Paragraph style
87104
*
88-
* @return PHPWord_Style_Font
105+
* @param null|array|\PHPWord_Style_Paragraph $style
106+
* @return null|\PHPWord_Style_Paragraph
89107
*/
90-
public function setFontStyle($styleFont)
108+
public function setParagraphStyle($style = null)
91109
{
92-
if (is_array($styleFont)) {
93-
$this->_styleFont = new PHPWord_Style_Font('text');
94-
95-
foreach ($styleFont as $key => $value) {
96-
if (substr($key, 0, 1) != '_') {
97-
$key = '_' . $key;
98-
}
99-
$this->_styleFont->setStyleValue($key, $value);
100-
}
110+
if (is_array($style)) {
111+
$this->paragraphStyle = new PHPWord_Style_Paragraph;
112+
$this->paragraphStyle->setArrayStyle($style);
113+
} elseif ($style instanceof PHPWord_Style_Paragraph) {
114+
$this->paragraphStyle = $style;
115+
} elseif (null === $style) {
116+
$this->paragraphStyle = new PHPWord_Style_Paragraph;
101117
} else {
102-
$this->_styleFont = $styleFont;
118+
$this->paragraphStyle = $style;
103119
}
120+
return $this->paragraphStyle;
104121
}
105122

106123
/**
@@ -110,35 +127,17 @@ public function setFontStyle($styleFont)
110127
*/
111128
public function getParagraphStyle()
112129
{
113-
return $this->_styleParagraph;
130+
return $this->paragraphStyle;
114131
}
115132

116133
/**
117-
* Set Paragraph style
118-
*
119-
* @param array|\PHPWord_Style_Paragraph $styleParagraph
120-
* @return \PHPWord_Style_Paragraph
121-
* @throws \Exception
134+
* @param string $text
135+
* @return $this
122136
*/
123-
public function setParagraphStyle($styleParagraph)
137+
public function setText($text)
124138
{
125-
if (is_array($styleParagraph)) {
126-
$this->_styleParagraph = new PHPWord_Style_Paragraph();
127-
128-
foreach ($styleParagraph as $key => $value) {
129-
if ($key === 'line-height') {
130-
null;
131-
} elseif (substr($key, 0, 1) != '_') {
132-
$key = '_' . $key;
133-
}
134-
$this->_styleParagraph->setStyleValue($key, $value);
135-
}
136-
} elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
137-
$this->_styleParagraph = $styleParagraph;
138-
} else {
139-
throw new Exception('Expected array or PHPWord_Style_Paragraph');
140-
}
141-
return $this->_styleParagraph;
139+
$this->text = $text;
140+
return $this;
142141
}
143142

144143
/**
@@ -148,6 +147,6 @@ public function setParagraphStyle($styleParagraph)
148147
*/
149148
public function getText()
150149
{
151-
return $this->_text;
150+
return $this->text;
152151
}
153152
}

Classes/PHPWord/Style/Font.php

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* @version 0.7.0
2626
*/
2727

28+
use PHPWord\Exceptions\InvalidStyleException;
29+
2830
/**
2931
* Class PHPWord_Style_Font
3032
*/
@@ -84,105 +86,119 @@ class PHPWord_Style_Font
8486
*
8587
* @var int|float
8688
*/
87-
private $_name;
89+
private $_name = PHPWord::DEFAULT_FONT_NAME;
8890

8991
/**
9092
* Font size
9193
*
9294
* @var int|float
9395
*/
94-
private $_size;
96+
private $_size = PHPWord::DEFAULT_FONT_SIZE;
9597

9698
/**
9799
* Bold
98100
*
99101
* @var bool
100102
*/
101-
private $_bold;
103+
private $_bold = false;
102104

103105
/**
104106
* Italics
105107
*
106108
* @var bool
107109
*/
108-
private $_italic;
110+
private $_italic = false;
109111

110112
/**
111113
* Superscript
112114
*
113115
* @var bool
114116
*/
115-
private $_superScript;
117+
private $_superScript = false;
116118

117119
/**
118120
* Subscript
119121
*
120122
* @var bool
121123
*/
122-
private $_subScript;
124+
private $_subScript = false;
123125

124126
/**
125127
* Underline mode
126128
*
127129
* @var string
128130
*/
129-
private $_underline;
131+
private $_underline = PHPWord_Style_Font::UNDERLINE_NONE;
130132

131133
/**
132134
* Strikethrough
133135
*
134136
* @var bool
135137
*/
136-
private $_strikethrough;
138+
private $_strikethrough = false;
137139

138140
/**
139141
* Font color
140142
*
141143
* @var string
142144
*/
143-
private $_color;
145+
private $_color = PHPWord::DEFAULT_FONT_COLOR;
144146

145147
/**
146148
* Foreground/highlight
147149
*
148150
* @var string
149151
*/
150-
private $_fgColor;
152+
private $_fgColor = null;
153+
154+
/**
155+
* Text line height
156+
*
157+
* @var int
158+
*/
159+
private $lineHeight = 1.0;
151160

152161
/**
153162
* New font style
154163
*
155-
* @param string $type Type of font
156-
* @param array $styleParagraph Paragraph styles definition
164+
* @param string $type Type of font
165+
* @param array $paragraphStyle Paragraph styles definition
157166
*/
158-
public function __construct($type = 'text', $styleParagraph = null)
167+
public function __construct($type = 'text', $paragraphStyle = null)
159168
{
160169
$this->_type = $type;
161-
$this->_name = PHPWord::DEFAULT_FONT_NAME;
162-
$this->_size = PHPWord::DEFAULT_FONT_SIZE;
163-
$this->_bold = false;
164-
$this->_italic = false;
165-
$this->_superScript = false;
166-
$this->_subScript = false;
167-
$this->_underline = PHPWord_Style_Font::UNDERLINE_NONE;
168-
$this->_strikethrough = false;
169-
$this->_color = PHPWord::DEFAULT_FONT_COLOR;
170-
$this->_fgColor = null;
171-
172-
if (!is_null($styleParagraph)) {
173-
$paragraph = new PHPWord_Style_Paragraph();
174-
foreach ($styleParagraph as $key => $value) {
175-
if (substr($key, 0, 1) != '_') {
176-
$key = '_' . $key;
177-
}
178-
$paragraph->setStyleValue($key, $value);
179-
}
180-
$this->_paragraphStyle = $paragraph;
170+
171+
if ($paragraphStyle instanceof PHPWord_Style_Paragraph) {
172+
$this->_paragraphStyle = $paragraphStyle;
173+
} elseif (is_array($paragraphStyle)) {
174+
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
175+
$this->_paragraphStyle->setArrayStyle($paragraphStyle);
176+
} elseif (null === $paragraphStyle) {
177+
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
181178
} else {
182-
$this->_paragraphStyle = null;
179+
$this->_paragraphStyle = $paragraphStyle;
183180
}
184181
}
185182

183+
/**
184+
* @param array $style
185+
* @return $this
186+
*/
187+
public function setArrayStyle(array $style = array())
188+
{
189+
foreach ($style as $key => $value) {
190+
if ($key === 'line-height') {
191+
$this->setLineHeight($value);
192+
null;
193+
} elseif (substr($key, 0, 1) !== '_') {
194+
$key = '_' . $key;
195+
}
196+
$this->setStyleValue($key, $value);
197+
}
198+
199+
return $this;
200+
}
201+
186202
/**
187203
* Set style value
188204
*
@@ -465,4 +481,35 @@ public function getParagraphStyle()
465481
{
466482
return $this->_paragraphStyle;
467483
}
484+
485+
486+
/**
487+
* Set the line height
488+
*
489+
* @param int|float|string $lineHeight
490+
* @return $this
491+
* @throws \PHPWord\Exceptions\InvalidStyleException
492+
*/
493+
public function setLineHeight($lineHeight)
494+
{
495+
if (is_string($lineHeight)) {
496+
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
497+
}
498+
499+
if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
500+
throw new InvalidStyleException('Line height must be a valid number');
501+
}
502+
503+
$this->lineHeight = $lineHeight;
504+
$this->getParagraphStyle()->setLineHeight($lineHeight);
505+
return $this;
506+
}
507+
508+
/**
509+
* @return int|float
510+
*/
511+
public function getLineHeight()
512+
{
513+
return $this->lineHeight;
514+
}
468515
}

0 commit comments

Comments
 (0)