-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathTable.php
219 lines (186 loc) · 6.38 KB
/
Table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\Style\Table as TableStyle;
use PhpOffice\PhpWord\Writer\Word2007\Element\TableAlignment;
/**
* Table style writer.
*
* @since 0.10.0
*/
class Table extends AbstractStyle
{
/**
* @var int Table width
*/
private $width;
/**
* Write style.
*/
public function write(): void
{
$style = $this->getStyle();
$xmlWriter = $this->getXmlWriter();
if ($style instanceof TableStyle) {
$this->writeStyle($xmlWriter, $style);
} elseif (is_string($style)) {
$xmlWriter->startElement('w:tblPr');
$xmlWriter->startElement('w:tblStyle');
$xmlWriter->writeAttribute('w:val', $style);
$xmlWriter->endElement();
if (null !== $this->width) {
$this->writeTblWidth($xmlWriter, 'w:tblW', TblWidth::PERCENT, $this->width);
}
$xmlWriter->endElement();
}
}
/**
* Write full style.
*/
private function writeStyle(XMLWriter $xmlWriter, TableStyle $style): void
{
// w:tblPr
$xmlWriter->startElement('w:tblPr');
// Table alignment
if ('' !== $style->getAlignment()) {
$tableAlignment = new TableAlignment($style->getAlignment());
$xmlWriter->startElement($tableAlignment->getName());
foreach ($tableAlignment->getAttributes() as $attributeName => $attributeValue) {
$xmlWriter->writeAttribute($attributeName, $attributeValue);
}
$xmlWriter->endElement();
}
$this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth());
$this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing());
$this->writeIndent($xmlWriter, $style);
$this->writeLayout($xmlWriter, $style->getLayout());
// Position
$styleWriter = new TablePosition($xmlWriter, $style->getPosition());
$styleWriter->write();
//Right to left
$xmlWriter->writeElementIf($style->isBidiVisual() !== null, 'w:bidiVisual', 'w:val', $this->writeOnOf($style->isBidiVisual()));
$this->writeMargin($xmlWriter, $style);
$this->writeBorder($xmlWriter, $style);
$xmlWriter->endElement(); // w:tblPr
$this->writeShading($xmlWriter, $style);
// First row style
$firstRow = $style->getFirstRow();
if ($firstRow instanceof TableStyle) {
$this->writeFirstRow($xmlWriter, $firstRow);
}
}
/**
* Enable/Disable automatic resizing of the table.
*
* @param string $layout autofit / fixed
*/
private function writeLayout(XMLWriter $xmlWriter, $layout): void
{
$xmlWriter->startElement('w:tblLayout');
$xmlWriter->writeAttribute('w:type', $layout);
$xmlWriter->endElement(); // w:tblLayout
}
/**
* Write margin.
*/
private function writeMargin(XMLWriter $xmlWriter, TableStyle $style): void
{
if ($style->hasMargin()) {
$xmlWriter->startElement('w:tblCellMar');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getCellMargin());
$styleWriter->write();
$xmlWriter->endElement(); // w:tblCellMar
}
}
/**
* Write border.
*/
private function writeBorder(XMLWriter $xmlWriter, TableStyle $style): void
{
if ($style->hasBorder()) {
$xmlWriter->startElement('w:tblBorders');
$styleWriter = new MarginBorder($xmlWriter);
$styleWriter->setSizes($style->getBorderSize());
$styleWriter->setColors($style->getBorderColor());
$styleWriter->setStyles($style->getBorderStyle());
$styleWriter->write();
$xmlWriter->endElement(); // w:tblBorders
}
}
/**
* Writes a table width.
*
* @param string $elementName
* @param string $unit
* @param float|int $width
*/
private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width = null): void
{
if (null === $width) {
return;
}
$xmlWriter->startElement($elementName);
$xmlWriter->writeAttributeIf(null !== $width, 'w:w', $width);
$xmlWriter->writeAttribute('w:type', $unit);
$xmlWriter->endElement();
}
/**
* Write row style.
*/
private function writeFirstRow(XMLWriter $xmlWriter, TableStyle $style): void
{
$xmlWriter->startElement('w:tblStylePr');
$xmlWriter->writeAttribute('w:type', 'firstRow');
$xmlWriter->startElement('w:tcPr');
$this->writeBorder($xmlWriter, $style);
$this->writeShading($xmlWriter, $style);
$xmlWriter->endElement(); // w:tcPr
$xmlWriter->endElement(); // w:tblStylePr
}
/**
* Write shading.
*/
private function writeShading(XMLWriter $xmlWriter, TableStyle $style): void
{
if (null !== $style->getShading()) {
$xmlWriter->startElement('w:tcPr');
$styleWriter = new Shading($xmlWriter, $style->getShading());
$styleWriter->write();
$xmlWriter->endElement();
}
}
/**
* Set width.
*
* @param int $value
*/
public function setWidth($value = null): void
{
$this->width = $value;
}
private function writeIndent(XMLWriter $xmlWriter, TableStyle $style): void
{
$indent = $style->getIndent();
if ($indent === null) {
return;
}
$this->writeTblWidth($xmlWriter, 'w:tblInd', $indent->getType(), $indent->getValue());
}
}