Skip to content

Commit 44d2501

Browse files
committed
TextBreak: Allow font/paragraph styling for text break
1 parent 088caa2 commit 44d2501

File tree

14 files changed

+288
-34
lines changed

14 files changed

+288
-34
lines changed

Classes/PHPWord/Section.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,14 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null, $stylePar
156156
/**
157157
* Add a TextBreak Element
158158
*
159-
* @param int $count
159+
* @param int $count
160+
* @param null|string|array|PHPWord_Style_Font $fontStyle
161+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
160162
*/
161-
public function addTextBreak($count = 1)
163+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
162164
{
163165
for ($i = 1; $i <= $count; $i++) {
164-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
166+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
165167
}
166168
}
167169

Classes/PHPWord/Section/Footer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ public function addText($text, $styleFont = null, $styleParagraph = null)
7979
}
8080

8181
/**
82-
* Add a TextBreak Element
82+
* Add TextBreak
8383
*
84-
* @param int $count
84+
* @param int $count
85+
* @param null|string|array|PHPWord_Style_Font $fontStyle
86+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
8587
*/
86-
public function addTextBreak($count = 1)
88+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
8789
{
8890
for ($i = 1; $i <= $count; $i++) {
89-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
91+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
9092
}
9193
}
9294

Classes/PHPWord/Section/Header.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ public function addText($text, $styleFont = null, $styleParagraph = null)
108108
}
109109

110110
/**
111-
* Add a TextBreak Element
111+
* Add TextBreak
112112
*
113-
* @param int $count
113+
* @param int $count
114+
* @param null|string|array|PHPWord_Style_Font $fontStyle
115+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
114116
*/
115-
public function addTextBreak($count = 1)
117+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
116118
{
117119
for ($i = 1; $i <= $count; $i++) {
118-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
120+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
119121
}
120122
}
121123

Classes/PHPWord/Section/Table/Cell.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ public function addLink($linkSrc, $linkName = null, $style = null)
146146
}
147147

148148
/**
149-
* Add a TextBreak Element
149+
* Add TextBreak
150150
*
151-
* @param int $count
151+
* @param int $count
152+
* @param null|string|array|PHPWord_Style_Font $fontStyle
153+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
152154
*/
153-
public function addTextBreak()
155+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
154156
{
155-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
157+
for ($i = 1; $i <= $count; $i++) {
158+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
159+
}
156160
}
157161

158162
/**

Classes/PHPWord/Section/TextBreak.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,91 @@
3030
*/
3131
class PHPWord_Section_TextBreak
3232
{
33+
/**
34+
* Paragraph style
35+
*
36+
* @var PHPWord_Style_Pagaraph
37+
*/
38+
private $paragraphStyle = null;
39+
40+
/**
41+
* Text style
42+
*
43+
* @var PHPWord_Style_Font
44+
*/
45+
private $fontStyle = null;
3346

3447
/**
3548
* Create a new TextBreak Element
3649
*/
37-
public function __construct()
50+
public function __construct($fontStyle = null, $paragraphStyle = null)
51+
{
52+
if (!is_null($paragraphStyle)) {
53+
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
54+
}
55+
if (!is_null($fontStyle)) {
56+
$this->setFontStyle($fontStyle, $paragraphStyle);
57+
}
58+
}
59+
60+
/**
61+
* Set Text style
62+
*
63+
* @param null|array|\PHPWord_Style_Font $style
64+
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
65+
* @return PHPWord_Style_Font
66+
*/
67+
public function setFontStyle($style = null, $paragraphStyle = null)
68+
{
69+
if ($style instanceof PHPWord_Style_Font) {
70+
$this->fontStyle = $style;
71+
$this->setParagraphStyle($paragraphStyle);
72+
} elseif (is_array($style)) {
73+
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
74+
$this->fontStyle->setArrayStyle($style);
75+
} else {
76+
$this->fontStyle = $style;
77+
$this->setParagraphStyle($paragraphStyle);
78+
}
79+
return $this->fontStyle;
80+
}
81+
82+
/**
83+
* Get Text style
84+
*
85+
* @return PHPWord_Style_Font
86+
*/
87+
public function getFontStyle()
88+
{
89+
return $this->fontStyle;
90+
}
91+
92+
/**
93+
* Set Paragraph style
94+
*
95+
* @param null|array|\PHPWord_Style_Paragraph $style
96+
* @return null|\PHPWord_Style_Paragraph
97+
*/
98+
public function setParagraphStyle($style = null)
99+
{
100+
if (is_array($style)) {
101+
$this->paragraphStyle = new PHPWord_Style_Paragraph;
102+
$this->paragraphStyle->setArrayStyle($style);
103+
} elseif ($style instanceof PHPWord_Style_Paragraph) {
104+
$this->paragraphStyle = $style;
105+
} else {
106+
$this->paragraphStyle = $style;
107+
}
108+
return $this->paragraphStyle;
109+
}
110+
111+
/**
112+
* Get Paragraph style
113+
*
114+
* @return PHPWord_Style_Paragraph
115+
*/
116+
public function getParagraphStyle()
38117
{
118+
return $this->paragraphStyle;
39119
}
40120
}

Classes/PHPWord/Section/TextRun.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,16 @@ public function addImage($imageSrc, $style = null)
132132
}
133133

134134
/**
135-
* Add a Text Break
135+
* Add TextBreak
136136
*
137-
* @param int $count
137+
* @param int $count
138+
* @param null|string|array|PHPWord_Style_Font $fontStyle
139+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
138140
*/
139-
public function addTextBreak($count = 1)
141+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
140142
{
141-
for ($i=1; $i<=$count; $i++) {
142-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
143+
for ($i = 1; $i <= $count; $i++) {
144+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
143145
}
144146
}
145147

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,51 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
477477

478478
/**
479479
* Write text break
480+
*
481+
* @param PHPWord_Shared_XMLWriter $objWriter
482+
* @param PHPWord_Section_TextBreak $element
480483
*/
481-
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
484+
protected function _writeTextBreak($objWriter, $element = null)
482485
{
483-
$objWriter->writeElement('w:p', null);
486+
$hasStyle = false;
487+
if (!is_null($element)) {
488+
$fontStyle = $element->getFontStyle();
489+
$sfIsObject = ($fontStyle instanceof PHPWord_Style_Font) ? true : false;
490+
$paragraphStyle = $element->getParagraphStyle();
491+
$spIsObject = ($paragraphStyle instanceof PHPWord_Style_Paragraph) ? true : false;
492+
$hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle);
493+
}
494+
if ($hasStyle) {
495+
// Paragraph style
496+
$objWriter->startElement('w:p');
497+
if ($spIsObject) {
498+
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
499+
} elseif (!$spIsObject && !is_null($paragraphStyle)) {
500+
$objWriter->startElement('w:pPr');
501+
$objWriter->startElement('w:pStyle');
502+
$objWriter->writeAttribute('w:val', $paragraphStyle);
503+
$objWriter->endElement(); // w:pStyle
504+
$objWriter->endElement(); // w:pPr
505+
}
506+
// Font style
507+
if (!is_null($fontStyle)) {
508+
$objWriter->startElement('w:pPr');
509+
if ($sfIsObject) {
510+
$this->_writeTextStyle($objWriter, $fontStyle);
511+
} elseif (!$sfIsObject && !is_null($fontStyle)) {
512+
$objWriter->startElement('w:rPr');
513+
$objWriter->startElement('w:rStyle');
514+
$objWriter->writeAttribute('w:val', $fontStyle);
515+
$objWriter->endElement(); // w:rStyle
516+
$objWriter->endElement(); // w:rPr
517+
}
518+
$objWriter->endElement(); // w:pPr
519+
}
520+
$objWriter->endElement(); // w:p
521+
} else {
522+
// Null element. No paragraph nor font style
523+
$objWriter->writeElement('w:p', null);
524+
}
484525
}
485526

486527
/**
@@ -570,7 +611,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
570611
} elseif ($element instanceof PHPWord_Section_Link) {
571612
$this->_writeLink($objWriter, $element);
572613
} elseif ($element instanceof PHPWord_Section_TextBreak) {
573-
$this->_writeTextBreak($objWriter);
614+
$this->_writeTextBreak($objWriter, $element);
574615
} elseif ($element instanceof PHPWord_Section_ListItem) {
575616
$this->_writeListItem($objWriter, $element);
576617
} elseif ($element instanceof PHPWord_Section_Image ||

Classes/PHPWord/Writer/Word2007/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function writeDocument(PHPWord $pPHPWord = null)
7979
} elseif ($element instanceof PHPWord_Section_Title) {
8080
$this->_writeTitle($objWriter, $element);
8181
} elseif ($element instanceof PHPWord_Section_TextBreak) {
82-
$this->_writeTextBreak($objWriter);
82+
$this->_writeTextBreak($objWriter, $element);
8383
} elseif ($element instanceof PHPWord_Section_PageBreak) {
8484
$this->_writePageBreak($objWriter);
8585
} elseif ($element instanceof PHPWord_Section_Table) {

Classes/PHPWord/Writer/Word2007/Footer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function writeFooter(PHPWord_Section_Footer $footer)
6363
} elseif ($element instanceof PHPWord_Section_TextRun) {
6464
$this->_writeTextRun($objWriter, $element);
6565
} elseif ($element instanceof PHPWord_Section_TextBreak) {
66-
$this->_writeTextBreak($objWriter);
66+
$this->_writeTextBreak($objWriter, $element);
6767
} elseif ($element instanceof PHPWord_Section_Table) {
6868
$this->_writeTable($objWriter, $element);
6969
} elseif ($element instanceof PHPWord_Section_Image ||

Classes/PHPWord/Writer/Word2007/Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function writeHeader(PHPWord_Section_Header $header)
6363
} elseif ($element instanceof PHPWord_Section_TextRun) {
6464
$this->_writeTextRun($objWriter, $element);
6565
} elseif ($element instanceof PHPWord_Section_TextBreak) {
66-
$this->_writeTextBreak($objWriter);
66+
$this->_writeTextBreak($objWriter, $element);
6767
} elseif ($element instanceof PHPWord_Section_Table) {
6868
$this->_writeTable($objWriter, $element);
6969
} elseif ($element instanceof PHPWord_Section_Image ||

0 commit comments

Comments
 (0)