Skip to content

Commit 09ba95b

Browse files
committed
Paragraph: Ability to define (1) normal paragraph style with PHPWord::setNormalStyle() and (2) parent style (basedOn) and style for following paragraph (next)
1 parent be4b01b commit 09ba95b

File tree

5 files changed

+134
-9
lines changed

5 files changed

+134
-9
lines changed

Classes/PHPWord.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
198198
PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
199199
}
200200

201+
/**
202+
* Set normal paragraph style definition to styles.xml
203+
*
204+
* @param array $styles Paragraph style definition
205+
*/
206+
public function setNormalStyle($styles)
207+
{
208+
PHPWord_Style::setNormalStyle($styles);
209+
}
210+
201211
/**
202212
* Adds a hyperlink style to styles.xml
203213
*

Classes/PHPWord/Style.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public static function addTitleStyle($titleCount, $styleFont, $styleParagraph =
140140
}
141141
}
142142

143+
/**
144+
* Set normal (default) paragraph style
145+
*
146+
* @param array $styles Paragraph style definition
147+
*/
148+
public static function setNormalStyle($styles)
149+
{
150+
self::addParagraphStyle('Normal', $styles);
151+
}
152+
143153
/**
144154
* Get all styles
145155
*

Classes/PHPWord/Style/Paragraph.php

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ class PHPWord_Style_Paragraph
8080
*/
8181
private $_hanging;
8282

83+
/**
84+
* Parent style
85+
*
86+
* @var string
87+
*/
88+
private $_basedOn;
89+
90+
/**
91+
* Style for next paragraph
92+
*
93+
* @var string
94+
*/
95+
private $_next;
96+
8397
/**
8498
* New Paragraph Style
8599
*/
@@ -92,6 +106,8 @@ public function __construct()
92106
$this->_tabs = null;
93107
$this->_indent = null;
94108
$this->_hanging = null;
109+
$this->_basedOn = 'Normal';
110+
$this->_next = null;
95111
}
96112

97113
/**
@@ -231,6 +247,16 @@ public function setIndent($pValue = null)
231247
return $this;
232248
}
233249

250+
/**
251+
* Get hanging
252+
*
253+
* @return int
254+
*/
255+
public function getHanging()
256+
{
257+
return $this->_hanging;
258+
}
259+
234260
/**
235261
* Set hanging
236262
*
@@ -244,22 +270,57 @@ public function setHanging($pValue = null)
244270
}
245271

246272
/**
247-
* Get hanging
273+
* Get tabs
248274
*
249-
* @return int
275+
* @return PHPWord_Style_Tabs
250276
*/
251-
public function getHanging()
277+
public function getTabs()
252278
{
253-
return $this->_hanging;
279+
return $this->_tabs;
254280
}
255281

256282
/**
257-
* Get tabs
283+
* Get parent style ID
258284
*
259-
* @return PHPWord_Style_Tabs
285+
* @return string
260286
*/
261-
public function getTabs()
287+
public function getBasedOn()
262288
{
263-
return $this->_tabs;
289+
return $this->_basedOn;
290+
}
291+
292+
/**
293+
* Set parent style ID
294+
*
295+
* @param string $pValue
296+
* @return PHPWord_Style_Paragraph
297+
*/
298+
public function setBasedOn($pValue = 'Normal')
299+
{
300+
$this->_basedOn = $pValue;
301+
return $this;
264302
}
303+
304+
/**
305+
* Get style for next paragraph
306+
*
307+
* @return string
308+
*/
309+
public function getNext()
310+
{
311+
return $this->_next;
312+
}
313+
314+
/**
315+
* Set style for next paragraph
316+
*
317+
* @param string $pValue
318+
* @return PHPWord_Style_Paragraph
319+
*/
320+
public function setNext($pValue = null)
321+
{
322+
$this->_next = $pValue;
323+
return $this;
324+
}
325+
265326
}

Classes/PHPWord/Writer/Word2007/Styles.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,30 @@ public function writeStyles(PHPWord $pPHPWord = null)
5959

6060
// Write Style Definitions
6161
$styles = PHPWord_Style::getStyles();
62+
63+
// Write normal paragraph style
64+
$normalStyle = null;
65+
if (array_key_exists('Normal', $styles)) {
66+
$normalStyle = $styles['Normal'];
67+
}
68+
$objWriter->startElement('w:style');
69+
$objWriter->writeAttribute('w:type', 'paragraph');
70+
$objWriter->writeAttribute('w:default', '1');
71+
$objWriter->writeAttribute('w:styleId', 'Normal');
72+
$objWriter->startElement('w:name');
73+
$objWriter->writeAttribute('w:val', 'Normal');
74+
$objWriter->endElement();
75+
if (!is_null($normalStyle)) {
76+
$this->_writeParagraphStyle($objWriter, $normalStyle);
77+
}
78+
$objWriter->endElement();
79+
80+
// Write other styles
6281
if (count($styles) > 0) {
6382
foreach ($styles as $styleName => $style) {
83+
if ($styleName == 'Normal') {
84+
continue;
85+
}
6486
if ($style instanceof PHPWord_Style_Font) {
6587

6688
$paragraphStyle = $style->getParagraphStyle();
@@ -92,6 +114,10 @@ public function writeStyles(PHPWord $pPHPWord = null)
92114
$objWriter->endElement();
93115

94116
if (!is_null($paragraphStyle)) {
117+
// Point parent style to Normal
118+
$objWriter->startElement('w:basedOn');
119+
$objWriter->writeAttribute('w:val', 'Normal');
120+
$objWriter->endElement();
95121
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
96122
}
97123

@@ -109,6 +135,22 @@ public function writeStyles(PHPWord $pPHPWord = null)
109135
$objWriter->writeAttribute('w:val', $styleName);
110136
$objWriter->endElement();
111137

138+
// Parent style
139+
$basedOn = $style->getBasedOn();
140+
if (!is_null($basedOn)) {
141+
$objWriter->startElement('w:basedOn');
142+
$objWriter->writeAttribute('w:val', $basedOn);
143+
$objWriter->endElement();
144+
}
145+
146+
// Next paragraph style
147+
$next = $style->getNext();
148+
if (!is_null($next)) {
149+
$objWriter->startElement('w:next');
150+
$objWriter->writeAttribute('w:val', $next);
151+
$objWriter->endElement();
152+
}
153+
112154
$this->_writeParagraphStyle($objWriter, $style);
113155
$objWriter->endElement();
114156

changelog.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ Changes in branch for release 0.7.1 :
3838
- Feature: (ivanlanin) GH-48 GH-86 - Paragraph: Hanging paragraph
3939
- Feature: (ivanlanin) GH-48 GH-86 - Section: Multicolumn and section break
4040
- QA: (Progi1984) - UnitTests
41-
- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips converter
41+
- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips() converter
42+
- Feature: (ivanlanin) - Paragraph: Ability to define normal paragraph style with PHPWord::setNormalStyle()
43+
- Feature: (ivanlanin) - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next)
4244

4345
Changes in branch for release 0.7.0 :
4446
- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found

0 commit comments

Comments
 (0)