Skip to content

Commit 7533d30

Browse files
committed
Changes:
- Use constants instead of hardcoded name and size for font (Arial, 20) - Section: Multicolumn - Section: Different section break - Paragraph: Hanging paragraph - Font: Superscript and subscript - Table: Table width in percentage - Table: Repeat as header row - Table: Allow row to break across pages
1 parent d3f6256 commit 7533d30

File tree

7 files changed

+144
-51
lines changed

7 files changed

+144
-51
lines changed

Classes/PHPWord.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
class PHPWord
4040
{
4141

42-
const DEFAULT_FONT_NAME = 'Times New Roman';
43-
const DEFAULT_FONT_SIZE = 24;
42+
const DEFAULT_FONT_NAME = 'Arial';
43+
const DEFAULT_FONT_SIZE = 20;
4444

4545
/**
4646
* Document properties

Classes/PHPWord/Section/Settings.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,34 @@ class PHPWord_Section_Settings
168168
*/
169169
private $footerHeight;
170170

171+
/**
172+
* Section columns count
173+
*
174+
* @var int
175+
*/
176+
private $_colsNum;
177+
178+
/**
179+
* Section spacing between columns
180+
*
181+
* @var int
182+
*/
183+
private $_colsSpace;
184+
185+
/**
186+
* Section break type
187+
*
188+
* Options:
189+
* - nextPage: Next page section break
190+
* - nextColumn: Column section break
191+
* - continuous: Continuous section break
192+
* - evenPage: Even page section break
193+
* - oddPage: Odd page section break
194+
*
195+
* @var string
196+
*/
197+
private $_breakType;
198+
171199
/**
172200
* Create new Section Settings
173201
*/
@@ -190,6 +218,9 @@ public function __construct()
190218
$this->_borderBottomColor = null;
191219
$this->headerHeight = 720; // set default header and footer to 720 twips (.5 inches)
192220
$this->footerHeight = 720;
221+
$this->_colsNum = 1;
222+
$this->_colsSpace = 720;
223+
$this->_breakType = null;
193224
}
194225

195226
/**
@@ -618,4 +649,62 @@ public function setFooterHeight($pValue = '') {
618649
$this->footerHeight = $pValue;
619650
return $this;
620651
}
652+
653+
/**
654+
* Set Section Columns Count
655+
*
656+
* @param in $pValue
657+
*/
658+
public function setColsNum($pValue = '') {
659+
$this->_colsNum = $pValue;
660+
return $this;
661+
}
662+
663+
/**
664+
* Get Section Columns Count
665+
*
666+
* @return int
667+
*/
668+
public function getColsNum() {
669+
return $this->_colsNum;
670+
}
671+
672+
/**
673+
* Set Section Space Between Columns
674+
*
675+
* @param int $pValue
676+
*/
677+
public function setColsSpace($pValue = '') {
678+
$this->_colsSpace = $pValue;
679+
return $this;
680+
}
681+
682+
/**
683+
* Get Section Space Between Columns
684+
*
685+
* @return int
686+
*/
687+
public function getColsSpace() {
688+
return $this->_colsSpace;
689+
}
690+
691+
/**
692+
* Set Break Type
693+
*
694+
* @param string $pValue
695+
*/
696+
public function setBreakType($pValue = null) {
697+
$this->_breakType = $pValue;
698+
return $this;
699+
}
700+
701+
/**
702+
* Get Break Type
703+
*
704+
* @return string
705+
*/
706+
public function getBreakType() {
707+
return $this->_breakType;
708+
}
709+
621710
}

Classes/PHPWord/Style/Table.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
*/
3131
class PHPWord_Style_Table
3232
{
33-
const WIDTH_TYPE_NIL = 'nil'; // No Width
34-
const WIDTH_TYPE_PERCENT = 'pct'; // Width in Fiftieths of a Percent
35-
const WIDTH_TYPE_POINT = 'dxa'; // Width in Twentieths of a Point
36-
const WIDTH_TYPE_AUTO = 'auto'; // Automatically Determined Width
3733

3834
private $_cellMarginTop;
3935
private $_cellMarginLeft;

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = nu
122122
$spaceAfter = $style->getSpaceAfter();
123123
$spacing = $style->getSpacing();
124124
$indent = $style->getIndent();
125+
$hanging = $style->getHanging();
125126
$tabs = $style->getTabs();
126127

127128
if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) {
@@ -135,10 +136,15 @@ protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = nu
135136
$objWriter->endElement();
136137
}
137138

138-
if (!is_null($indent)) {
139+
if (!is_null($indent) || !is_null($hanging)) {
139140
$objWriter->startElement('w:ind');
140141
$objWriter->writeAttribute('w:firstLine', 0);
141-
$objWriter->writeAttribute('w:left', $indent);
142+
if (!is_null($indent)) {
143+
$objWriter->writeAttribute('w:left', $indent);
144+
}
145+
if (!is_null($hanging)) {
146+
$objWriter->writeAttribute('w:hanging', $hanging);
147+
}
142148
$objWriter->endElement();
143149
}
144150

@@ -322,11 +328,13 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
322328
$fgColor = $style->getFgColor();
323329
$striketrough = $style->getStrikethrough();
324330
$underline = $style->getUnderline();
331+
$superscript = $style->getSuperScript();
332+
$subscript = $style->getSubScript();
325333

326334
$objWriter->startElement('w:rPr');
327335

328336
// Font
329-
if ($font != 'Arial') {
337+
if ($font != PHPWord::DEFAULT_FONT_NAME) {
330338
$objWriter->startElement('w:rFonts');
331339
$objWriter->writeAttribute('w:ascii', $font);
332340
$objWriter->writeAttribute('w:hAnsi', $font);
@@ -342,7 +350,7 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
342350
}
343351

344352
// Size
345-
if ($size != 20) {
353+
if ($size != PHPWord::DEFAULT_FONT_SIZE) {
346354
$objWriter->startElement('w:sz');
347355
$objWriter->writeAttribute('w:val', $size);
348356
$objWriter->endElement();
@@ -381,6 +389,13 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
381389
$objWriter->endElement();
382390
}
383391

392+
// Superscript/subscript
393+
if ($superscript || $subscript) {
394+
$objWriter->startElement('w:vertAlign');
395+
$objWriter->writeAttribute('w:val', $superscript ? 'superscript' : 'subscript');
396+
$objWriter->endElement();
397+
}
398+
384399
$objWriter->endElement();
385400
}
386401

@@ -397,6 +412,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
397412
if ($_cRows > 0) {
398413
$objWriter->startElement('w:tbl');
399414
$tblStyle = $table->getStyle();
415+
$tblWidth = $table->getWidth();
400416
if ($tblStyle instanceof PHPWord_Style_Table) {
401417
$this->_writeTableStyle($objWriter, $tblStyle);
402418
} else {
@@ -405,26 +421,46 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
405421
$objWriter->startElement('w:tblStyle');
406422
$objWriter->writeAttribute('w:val', $tblStyle);
407423
$objWriter->endElement();
424+
if (!is_null($tblWidth)) {
425+
$objWriter->startElement('w:tblW');
426+
$objWriter->writeAttribute('w:w', $tblWidth);
427+
$objWriter->writeAttribute('w:type', 'pct');
428+
$objWriter->endElement();
429+
}
408430
$objWriter->endElement();
409431
}
410432
}
411433

412-
$_heights = $table->getRowHeights();
413434
for ($i = 0; $i < $_cRows; $i++) {
414435
$row = $_rows[$i];
415-
$height = $_heights[$i];
436+
$height = $row->getHeight();
437+
$rowStyle = $row->getStyle();
438+
$tblHeader = $rowStyle->getTblHeader();
439+
$cantSplit = $rowStyle->getCantSplit();
416440

417441
$objWriter->startElement('w:tr');
418442

419-
if (!is_null($height)) {
443+
if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) {
420444
$objWriter->startElement('w:trPr');
421-
$objWriter->startElement('w:trHeight');
422-
$objWriter->writeAttribute('w:val', $height);
423-
$objWriter->endElement();
445+
if (!is_null($height)) {
446+
$objWriter->startElement('w:trHeight');
447+
$objWriter->writeAttribute('w:val', $height);
448+
$objWriter->endElement();
449+
}
450+
if (!is_null($tblHeader)) {
451+
$objWriter->startElement('w:tblHeader');
452+
$objWriter->writeAttribute('w:val', $tblHeader);
453+
$objWriter->endElement();
454+
}
455+
if (!is_null($cantSplit)) {
456+
$objWriter->startElement('w:cantSplit');
457+
$objWriter->writeAttribute('w:val', $cantSplit);
458+
$objWriter->endElement();
459+
}
424460
$objWriter->endElement();
425461
}
426462

427-
foreach ($row as $cell) {
463+
foreach ($row->getCells() as $cell) {
428464
$objWriter->startElement('w:tc');
429465

430466
$cellStyle = $cell->getStyle();

Classes/PHPWord/Writer/Word2007/Document.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
140140

141141
$borders = $settings->getBorderSize();
142142

143-
$colsNum = $_settings->getColsNum();
144-
$colsSpace = $_settings->getColsSpace();
145-
$breakType = $_settings->getBreakType();
143+
$colsNum = $settings->getColsNum();
144+
$colsSpace = $settings->getColsSpace();
145+
$breakType = $settings->getBreakType();
146146

147147
$objWriter->startElement('w:sectPr');
148148

@@ -159,7 +159,6 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
159159
$objWriter->endElement();
160160
}
161161

162-
// http://www.schemacentral.com/sc/ooxml/a-w_val-43.html
163162
if (!is_null($breakType)) {
164163
$objWriter->startElement('w:type');
165164
$objWriter->writeAttribute('w:val', $breakType);
@@ -247,12 +246,8 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
247246
}
248247

249248
$objWriter->startElement('w:cols');
250-
if($colsNum > 1){
251-
$objWriter->writeAttribute('w:num', $colsNum);
252-
$objWriter->writeAttribute('w:space', $colsSpace);
253-
} else {
254-
$objWriter->writeAttribute('w:space', '720');
255-
}
249+
$objWriter->writeAttribute('w:num', $colsNum);
250+
$objWriter->writeAttribute('w:space', $colsSpace);
256251
$objWriter->endElement();
257252

258253

Classes/PHPWord/Writer/Word2007/Styles.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ public function writeStyles(PHPWord $pPHPWord = null)
5656
// Write DocDefaults
5757
$this->_writeDocDefaults($objWriter);
5858

59-
// Reset Normal Paragraph Style
60-
$objWriter->startElement('w:style');
61-
$objWriter->writeAttribute('w:type', 'paragraph');
62-
$objWriter->writeAttribute('w:default', '1');
63-
$objWriter->writeAttribute('w:styleId', 'Normal');
64-
$objWriter->startElement('w:name');
65-
$objWriter->writeAttribute('w:val', 'Normal');
66-
$objWriter->endElement(); // w:name
67-
$objWriter->startElement('w:pPr');
68-
$objWriter->startElement('w:spacing');
69-
$objWriter->writeAttribute('w:before', 0);
70-
$objWriter->writeAttribute('w:after', 0);
71-
$objWriter->writeAttribute('w:line', 240);
72-
$objWriter->endElement(); // w:spacing
73-
$objWriter->endElement(); // w:pPr
74-
$objWriter->endElement(); // w:style
7559

7660
// Write Style Definitions
7761
$styles = PHPWord_Style::getStyles();
@@ -108,9 +92,6 @@ public function writeStyles(PHPWord $pPHPWord = null)
10892
$objWriter->endElement();
10993

11094
if (!is_null($paragraphStyle)) {
111-
$objWriter->startElement('w:basedOn');
112-
$objWriter->writeAttribute('w:val', 'Normal');
113-
$objWriter->endElement();
11495
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
11596
}
11697

@@ -128,10 +109,6 @@ public function writeStyles(PHPWord $pPHPWord = null)
128109
$objWriter->writeAttribute('w:val', $styleName);
129110
$objWriter->endElement();
130111

131-
$objWriter->startElement('w:basedOn');
132-
$objWriter->writeAttribute('w:val', 'Normal');
133-
$objWriter->endElement();
134-
135112
$this->_writeParagraphStyle($objWriter, $style);
136113
$objWriter->endElement();
137114

Classes/PHPWord/_staticDocParts/settings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
22
<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
33
<w:zoom w:percent="100" />
44
<w:embedSystemFonts />

0 commit comments

Comments
 (0)