Skip to content

Commit 9966508

Browse files
committed
Added line height to font style
1 parent 5efcec8 commit 9966508

File tree

5 files changed

+211
-92
lines changed

5 files changed

+211
-92
lines changed

Classes/PHPWord/Section/Text.php

Lines changed: 56 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;
52+
private $paragraphStyle;
5453

5554

5655
/**
5756
* Create a new Text Element
5857
*
59-
* @var string $text
60-
* @var mixed $style
58+
* @param string $text
59+
* @param null|array|\PHPWord_Style_Font $fontStyle
60+
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
6161
*/
62-
public function __construct($text = null, $styleFont = null, $styleParagraph = null)
62+
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
6363
{
64-
// Set font style
65-
$this->setFontStyle($styleFont);
66-
67-
// Set paragraph style
68-
$this->setParagraphStyle($styleParagraph);
69-
70-
$this->_text = $text;
64+
$this->setText($text);
65+
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
66+
$this->setFontStyle($fontStyle, $paragraphStyle);
67+
}
7168

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

7592
/**
@@ -79,28 +96,29 @@ 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
107+
* @throws \Exception
89108
*/
90-
public function setFontStyle($styleFont)
109+
public function setParagraphStyle($style = null)
91110
{
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-
}
111+
if (is_array($style)) {
112+
$this->paragraphStyle = new PHPWord_Style_Paragraph;
113+
$this->paragraphStyle->setArrayStyle($style);
114+
} elseif ($style instanceof PHPWord_Style_Paragraph) {
115+
$this->paragraphStyle = $style;
116+
} elseif (null === $style) {
117+
$this->paragraphStyle = new PHPWord_Style_Paragraph;
101118
} else {
102-
$this->_styleFont = $styleFont;
119+
throw new Exception('Expected array or PHPWord_Style_Paragraph');
103120
}
121+
return $this->paragraphStyle;
104122
}
105123

106124
/**
@@ -110,35 +128,17 @@ public function setFontStyle($styleFont)
110128
*/
111129
public function getParagraphStyle()
112130
{
113-
return $this->_styleParagraph;
131+
return $this->paragraphStyle;
114132
}
115133

116134
/**
117-
* Set Paragraph style
118-
*
119-
* @param array|\PHPWord_Style_Paragraph $styleParagraph
120-
* @return \PHPWord_Style_Paragraph
121-
* @throws \Exception
135+
* @param string $text
136+
* @return $this
122137
*/
123-
public function setParagraphStyle($styleParagraph)
138+
public function setText($text)
124139
{
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;
140+
$this->text = $text;
141+
return $this;
142142
}
143143

144144
/**
@@ -148,6 +148,6 @@ public function setParagraphStyle($styleParagraph)
148148
*/
149149
public function getText()
150150
{
151-
return $this->_text;
151+
return $this->text;
152152
}
153-
}
153+
}

Classes/PHPWord/Style/Font.php

Lines changed: 82 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,120 @@ 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
166+
* @throws \Exception
157167
*/
158-
public function __construct($type = 'text', $styleParagraph = null)
168+
public function __construct($type = 'text', $paragraphStyle = null)
159169
{
160170
$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;
171+
172+
if ($paragraphStyle instanceof PHPWord_Style_Paragraph) {
173+
$this->_paragraphStyle = $paragraphStyle;
174+
} elseif (is_array($paragraphStyle)) {
175+
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
176+
$this->_paragraphStyle->setArrayStyle($paragraphStyle);
177+
} elseif (null === $paragraphStyle) {
178+
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
181179
} else {
182-
$this->_paragraphStyle = null;
180+
throw new Exception('Expected array or PHPWord_Style_Paragraph');
183181
}
184182
}
185183

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

0 commit comments

Comments
 (0)