Skip to content

Commit a87953a

Browse files
committed
Merge pull request #129 from ivanlanin/develop
Add TextBreak styling
2 parents 3a1a056 + 44d2501 commit a87953a

File tree

18 files changed

+296
-38
lines changed

18 files changed

+296
-38
lines changed

Classes/PHPWord/HashTable.php

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
/**
2929
* PHPWord_HashTable
30+
*
31+
* @codeCoverageIgnore Legacy from PHPExcel
3032
*/
3133
class PHPWord_HashTable
3234
{

Classes/PHPWord/Section.php

+5-3
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

+6-4
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

+6-4
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

+8-4
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

+81-1
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

+7-5
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/Shared/ZipStreamWrapper.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
* @version 0.7.0
2626
*/
2727

28-
/** Register new zip wrapper */
29-
PHPWord_Shared_ZipStreamWrapper::register();
30-
3128
/**
3229
* Class PHPWord_Shared_ZipStreamWrapper
30+
*
31+
* @codeCoverageIgnore Legacy from PHPExcel
3332
*/
3433
class PHPWord_Shared_ZipStreamWrapper
3534
{

Classes/PHPWord/Writer/ODText.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public function save($pFilename = null)
141141
// Add META-INF/manifest.xml
142142
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_document));
143143

144-
// Add media
144+
// Add media. Has not used yet. Legacy from PHPExcel.
145+
// @codeCoverageIgnoreStart
145146
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
146147
if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
147148
$imageContents = null;

Classes/PHPWord/Writer/ODText/Manifest.php

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function writeManifest(PHPWord $pPHPWord = null)
7777
$objWriter->writeAttribute('manifest:full-path', 'styles.xml');
7878
$objWriter->endElement();
7979

80+
// Not used yet. Legacy from PHPExcel
81+
// @codeCoverageIgnoreStart
8082
for ($i = 0; $i < $this->getParentWriter()->getDrawingHashTable()->count(); ++$i) {
8183
if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
8284
$extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());

Classes/PHPWord/Writer/Word2007/Base.php

+44-3
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

+1-1
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

+1-1
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

+1-1
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 ||

Tests/PHPWord/Section/TextBreakTest.php

+46-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,59 @@
22
namespace PHPWord\Tests\Section;
33

44
use PHPWord_Section_TextBreak;
5+
use PHPWord_Style_Paragraph;
6+
use PHPWord_Style_Font;
57

8+
/**
9+
* @package PHPWord\Tests
10+
* @coversDefaultClass PHPWord_Section_TextBreak
11+
* @runTestsInSeparateProcesses
12+
*/
613
class TextBreakTest extends \PHPUnit_Framework_TestCase
714
{
815
/**
9-
* Executed before each method of the class
16+
* Construct with empty value
1017
*/
1118
public function testConstruct()
1219
{
13-
// Section Settings
14-
$oTextBreak = new PHPWord_Section_TextBreak();
20+
$object = new PHPWord_Section_TextBreak();
21+
$this->assertNull($object->getFontStyle());
22+
$this->assertNull($object->getParagraphStyle());
23+
}
24+
25+
/**
26+
* Construct with style object
27+
*/
28+
public function testConstructWithStyleObject()
29+
{
30+
$fStyle = new PHPWord_Style_Font();
31+
$pStyle = new PHPWord_Style_Paragraph();
32+
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
33+
$this->assertEquals($fStyle, $object->getFontStyle());
34+
$this->assertEquals($pStyle, $object->getParagraphStyle());
35+
}
36+
37+
/**
38+
* Construct with style array
39+
*/
40+
public function testConstructWithStyleArray()
41+
{
42+
$fStyle = array('size' => 12);
43+
$pStyle = array('spacing' => 240);
44+
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
45+
$this->assertInstanceOf('PHPWord_Style_Font', $object->getFontStyle());
46+
$this->assertInstanceOf('PHPWord_Style_Paragraph', $object->getParagraphStyle());
47+
}
1548

16-
$this->assertInstanceOf('PHPWord_Section_TextBreak', $oTextBreak);
49+
/**
50+
* Construct with style name
51+
*/
52+
public function testConstructWithStyleName()
53+
{
54+
$fStyle = 'fStyle';
55+
$pStyle = 'pStyle';
56+
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
57+
$this->assertEquals($fStyle, $object->getFontStyle());
58+
$this->assertEquals($pStyle, $object->getParagraphStyle());
1759
}
1860
}

0 commit comments

Comments
 (0)