Skip to content

Commit 38a15e5

Browse files
committed
Word2007 : Support for tab stops
1 parent a610c34 commit 38a15e5

File tree

10 files changed

+332
-33
lines changed

10 files changed

+332
-33
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
*.odt
88
*.docx
99
*.rtf
10-
*.txt
10+
*.txt
11+
*.xml

Classes/PHPWord/Style/Paragraph.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ class PHPWord_Style_Paragraph {
6262
* @var int
6363
*/
6464
private $_spacing;
65-
66-
65+
66+
/**
67+
* Set of Custom Tab Stops
68+
*
69+
* @var array
70+
*/
71+
private $_tabs;
72+
6773
/**
6874
* Indent by how much
6975
*
@@ -80,6 +86,7 @@ public function __construct() {
8086
$this->_spaceBefore = null;
8187
$this->_spaceAfter = null;
8288
$this->_spacing = null;
89+
$this->_tabs = null;
8390
$this->_indent = null;
8491
}
8592

@@ -90,13 +97,16 @@ public function __construct() {
9097
* @param mixed $value
9198
*/
9299
public function setStyleValue($key, $value) {
93-
if($key == '_spacing') {
94-
$value += 240; // because line height of 1 matches 240 twips
95-
}
96100
if($key == '_indent') {
97-
$value = (int)$value * 720; // 720 twips per indent
101+
$value = (int)$value * 720; // 720 twips per indent
98102
}
99-
$this->$key = $value;
103+
if($key == '_spacing') {
104+
$value += 240; // because line height of 1 matches 240 twips
105+
}
106+
if($key === '_tabs') {
107+
$value = new PHPWord_Style_Tabs($value);
108+
}
109+
$this->$key = $value;
100110
}
101111

102112
/**
@@ -202,5 +212,14 @@ public function setIndent($pValue = null) {
202212
$this->_indent = $pValue;
203213
return $this;
204214
}
215+
216+
/**
217+
* Get tabs
218+
*
219+
* @return PHPWord_Style_Tabs
220+
*/
221+
public function getTabs() {
222+
return $this->_tabs;
223+
}
205224
}
206225
?>

Classes/PHPWord/Style/Tab.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2011 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 010 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version {something}
26+
*/
27+
28+
/**
29+
* PHPWord_Style_Tabs
30+
*
31+
* @category PHPWord
32+
* @package PHPWord_Style_Paragraph
33+
* @copyright Copyright (c) 2011 PHPWord
34+
* @link http://www.schemacentral.com/sc/ooxml/e-w_tab-1.html w:tab
35+
*/
36+
class PHPWord_Style_Tab {
37+
38+
/**
39+
* Tab Stop Type
40+
*
41+
* @var string
42+
*/
43+
private $_val;
44+
45+
/**
46+
* Tab Leader Character
47+
*
48+
* @var string
49+
*/
50+
private $_leader;
51+
52+
/**
53+
* Tab Stop Position
54+
*
55+
* @var int
56+
*/
57+
private $_position;
58+
59+
/**
60+
* Tab Stop Type
61+
*
62+
* @var array
63+
* @link http://www.schemacentral.com/sc/ooxml/a-w_val-26.html Tab Stop Type
64+
*/
65+
private static $_possibleStopTypes = array(
66+
'clear', // No Tab Stop
67+
'left', // Left Tab Stop
68+
'center', // Center Tab Stop
69+
'right', // Right Tab Stop
70+
'decimal', // Decimal Tab
71+
'bar', // Bar Tab
72+
'num' // List tab
73+
);
74+
75+
/**
76+
* Tab Leader Character
77+
*
78+
* @var array
79+
* @link http://www.schemacentral.com/sc/ooxml/a-w_leader-1.html Tab Leader Character
80+
*/
81+
private static $_possibleLeaders = array(
82+
'none', // No tab stop leader
83+
'dot', // Dotted leader line
84+
'hyphen', // Dashed tab stop leader line
85+
'underscore', // Solid leader line
86+
'heavy', // Heavy solid leader line
87+
'middleDot' // Middle dot leader line
88+
);
89+
90+
/**
91+
* Create a new instance of PHPWord_Style_Tab. Both $val and $leader
92+
* must conform to the values put forth in the schema. If they do not
93+
* they will be changed to default values.
94+
*
95+
* @param string $val Defaults to 'clear' if value is not possible.
96+
* @param int $position Must be an integer; otherwise defaults to 0.
97+
* @param string $leader Defaults to NULL if value is not possible.
98+
*/
99+
public function __construct($val = NULL, $position = 0, $leader = NULL) {
100+
// Default to clear if the stop type is not matched
101+
$this->_val = (self::isStopType($val)) ? $val : 'clear';
102+
103+
// Default to 0 if the position is non-numeric
104+
$this->_position = (is_numeric($position)) ? intval($position) : 0;
105+
106+
// Default to NULL if no tab leader
107+
$this->_leader = (self::isLeaderType($leader)) ? $leader : NULL;
108+
}
109+
110+
/**
111+
* Creates the XML DOM for the instance of PHPWord_Style_Tab.
112+
*
113+
* @param PHPWord_Shared_XMLWriter $objWriter
114+
*/
115+
public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) {
116+
if(isset($objWriter)) {
117+
$objWriter->startElement("w:tab");
118+
$objWriter->writeAttribute("w:val", $this->_val);
119+
if(!is_null($this->_leader)) {
120+
$objWriter->writeAttribute("w:leader", $this->_leader);
121+
}
122+
$objWriter->writeAttribute("w:pos", $this->_position);
123+
$objWriter->endElement();
124+
}
125+
}
126+
127+
/**
128+
* Test if attribute is a valid stop type.
129+
*
130+
* @param string $attribute
131+
* @return bool True if it is; false otherwise.
132+
*/
133+
private static function isStopType($attribute) {
134+
return in_array($attribute, self::$_possibleStopTypes);
135+
}
136+
137+
/**
138+
* Test if attribute is a valid leader type.
139+
*
140+
* @param string $attribute
141+
* @return bool True if it is; false otherwise.
142+
*/
143+
private static function isLeaderType($attribute) {
144+
return in_array($attribute, self::$_possibleLeaders);
145+
}
146+
}
147+
?>

Classes/PHPWord/Style/Tabs.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2011 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 010 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version {something}
26+
*/
27+
28+
/**
29+
* PHPWord_Style_Tabs
30+
*
31+
* @category PHPWord
32+
* @package PHPWord_Style_Paragraph
33+
* @copyright Copyright (c) 2011 PHPWord
34+
* @link http://www.schemacentral.com/sc/ooxml/e-w_tabs-1.html w:tabs
35+
*/
36+
class PHPWord_Style_Tabs {
37+
38+
/**
39+
* Tabs
40+
*
41+
* @var array
42+
*/
43+
private $_tabs;
44+
45+
/**
46+
*
47+
* @param array $tabs
48+
*/
49+
public function __construct(array $tabs) {
50+
$this->_tabs = $tabs;
51+
}
52+
53+
/**
54+
*
55+
* @param PHPWord_Shared_XMLWriter $objWriter
56+
*/
57+
public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) {
58+
if(isset($objWriter)) {
59+
$objWriter->startElement("w:tabs");
60+
foreach ($this->_tabs as &$tab) {
61+
$tab->toXml($objWriter);
62+
}
63+
$objWriter->endElement();
64+
}
65+
}
66+
}
67+
?>

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,12 @@ protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = nu
114114
$spaceAfter = $style->getSpaceAfter();
115115
$spacing = $style->getSpacing();
116116
$indent = $style->getIndent();
117-
117+
$tabs = $style->getTabs();
118118

119-
if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent)) {
120-
121-
if(!$withoutPPR) {
122-
$objWriter->startElement('w:pPr');
123-
}
119+
if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) {
120+
if(!$withoutPPR) {
121+
$objWriter->startElement('w:pPr');
122+
}
124123

125124
if(!is_null($align)) {
126125
$objWriter->startElement('w:jc');
@@ -136,26 +135,27 @@ protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = nu
136135
}
137136

138137
if(!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) {
139-
140138
$objWriter->startElement('w:spacing');
141-
142-
if(!is_null($spaceBefore)) {
143-
$objWriter->writeAttribute('w:before', $spaceBefore);
144-
}
145-
if(!is_null($spaceAfter)) {
146-
$objWriter->writeAttribute('w:after', $spaceAfter);
147-
}
148-
if(!is_null($spacing)) {
149-
$objWriter->writeAttribute('w:line', $spacing);
150-
$objWriter->writeAttribute('w:lineRule', 'auto');
151-
}
152-
139+
if(!is_null($spaceBefore)) {
140+
$objWriter->writeAttribute('w:before', $spaceBefore);
141+
}
142+
if(!is_null($spaceAfter)) {
143+
$objWriter->writeAttribute('w:after', $spaceAfter);
144+
}
145+
if(!is_null($spacing)) {
146+
$objWriter->writeAttribute('w:line', $spacing);
147+
$objWriter->writeAttribute('w:lineRule', 'auto');
148+
}
153149
$objWriter->endElement();
154150
}
155-
156-
if(!$withoutPPR) {
157-
$objWriter->endElement(); // w:pPr
158-
}
151+
152+
if(!is_null($tabs)) {
153+
$tabs->toXml($objWriter);
154+
}
155+
156+
if(!$withoutPPR) {
157+
$objWriter->endElement(); // w:pPr
158+
}
159159
}
160160
}
161161

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Fixed in branch for release 0.7 :
2727
- Feature: (Progi1984) GH-1 - Implement RTF Writer
2828
- Feature: (Progi1984) GH-2 - Implement ODT Writer
2929
- Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells
30+
- Feature: (RLovelett) - Word2007 : Support for tab stops
3031
- General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer
3132
- General: (deds) - add indentation support to paragraphs
3233
- General: (Progi1984) GH-27 - Support for Composer

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
],
2222
"require": {
23-
"php": ">=5.2.0",
23+
"php": ">=5.3.0",
2424
"ext-xml": "*"
2525
},
2626
"recommend": {

0 commit comments

Comments
 (0)