|
21 | 21 | use PhpOffice\PhpWord\Element\Row as RowElement;
|
22 | 22 | use PhpOffice\PhpWord\Element\Table as TableElement;
|
23 | 23 | use PhpOffice\PhpWord\Settings;
|
| 24 | +use PhpOffice\PhpWord\SimpleType\Border; |
24 | 25 | use PhpOffice\PhpWord\Style;
|
| 26 | +use PhpOffice\PhpWord\Style\Cell as CellStyle; |
| 27 | +use PhpOffice\PhpWord\Style\Table as TableStyle; |
25 | 28 |
|
26 | 29 | /**
|
27 | 30 | * Table element RTF writer.
|
|
30 | 33 | */
|
31 | 34 | class Table extends AbstractElement
|
32 | 35 | {
|
| 36 | + /** |
| 37 | + * @var TableElement |
| 38 | + */ |
| 39 | + protected $element; |
| 40 | + |
33 | 41 | /**
|
34 | 42 | * Write element.
|
35 | 43 | *
|
@@ -77,9 +85,18 @@ public function write()
|
77 | 85 | private function writeRowDef(RowElement $row)
|
78 | 86 | {
|
79 | 87 | $content = '';
|
| 88 | + $tableStyle = $this->element->getStyle(); |
| 89 | + if (is_string($tableStyle)) { |
| 90 | + $tableStyle = Style::getStyle($tableStyle); |
| 91 | + if (!($tableStyle instanceof TableStyle)) { |
| 92 | + $tableStyle = null; |
| 93 | + } |
| 94 | + } |
80 | 95 |
|
81 | 96 | $rightMargin = 0;
|
82 | 97 | foreach ($row->getCells() as $cell) {
|
| 98 | + $content .= $this->writeCellStyle($cell->getStyle(), $tableStyle); |
| 99 | + |
83 | 100 | $width = $cell->getWidth();
|
84 | 101 | $vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
|
85 | 102 | if ($width === null) {
|
@@ -127,6 +144,103 @@ private function writeCell(CellElement $cell)
|
127 | 144 | return $content;
|
128 | 145 | }
|
129 | 146 |
|
| 147 | + private function writeCellStyle(CellStyle $cell, ?TableStyle $table): string |
| 148 | + { |
| 149 | + $content = $this->writeCellBorder( |
| 150 | + 't', |
| 151 | + $cell->getBorderTopStyle() ?: ($table ? $table->getBorderTopStyle() : null), |
| 152 | + (int) round($cell->getBorderTopSize() ?: ($table ? ($table->getBorderTopSize() ?: 0) : 0)), |
| 153 | + $cell->getBorderTopColor() ?? ($table ? $table->getBorderTopColor() : null) |
| 154 | + ); |
| 155 | + $content .= $this->writeCellBorder( |
| 156 | + 'l', |
| 157 | + $cell->getBorderLeftStyle() ?: ($table ? $table->getBorderLeftStyle() : null), |
| 158 | + (int) round($cell->getBorderLeftSize() ?: ($table ? ($table->getBorderLeftSize() ?: 0) : 0)), |
| 159 | + $cell->getBorderLeftColor() ?? ($table ? $table->getBorderLeftColor() : null) |
| 160 | + ); |
| 161 | + $content .= $this->writeCellBorder( |
| 162 | + 'b', |
| 163 | + $cell->getBorderBottomStyle() ?: ($table ? $table->getBorderBottomStyle() : null), |
| 164 | + (int) round($cell->getBorderBottomSize() ?: ($table ? ($table->getBorderBottomSize() ?: 0) : 0)), |
| 165 | + $cell->getBorderBottomColor() ?? ($table ? $table->getBorderBottomColor() : null) |
| 166 | + ); |
| 167 | + $content .= $this->writeCellBorder( |
| 168 | + 'r', |
| 169 | + $cell->getBorderRightStyle() ?: ($table ? $table->getBorderRightStyle() : null), |
| 170 | + (int) round($cell->getBorderRightSize() ?: ($table ? ($table->getBorderRightSize() ?: 0) : 0)), |
| 171 | + $cell->getBorderRightColor() ?? ($table ? $table->getBorderRightColor() : null) |
| 172 | + ); |
| 173 | + |
| 174 | + return $content; |
| 175 | + } |
| 176 | + |
| 177 | + private function writeCellBorder(string $prefix, ?string $borderStyle, int $borderSize, ?string $borderColor): string |
| 178 | + { |
| 179 | + if ($borderSize == 0) { |
| 180 | + return ''; |
| 181 | + } |
| 182 | + |
| 183 | + $content = '\clbrdr' . $prefix; |
| 184 | + /** |
| 185 | + * \brdrs Single-thickness border. |
| 186 | + * \brdrth Double-thickness border. |
| 187 | + * \brdrsh Shadowed border. |
| 188 | + * \brdrdb Double border. |
| 189 | + * \brdrdot Dotted border. |
| 190 | + * \brdrdash Dashed border. |
| 191 | + * \brdrhair Hairline border. |
| 192 | + * \brdrinset Inset border. |
| 193 | + * \brdrdashsm Dash border (small). |
| 194 | + * \brdrdashd Dot dash border. |
| 195 | + * \brdrdashdd Dot dot dash border. |
| 196 | + * \brdroutset Outset border. |
| 197 | + * \brdrtriple Triple border. |
| 198 | + * \brdrtnthsg Thick thin border (small). |
| 199 | + * \brdrthtnsg Thin thick border (small). |
| 200 | + * \brdrtnthtnsg Thin thick thin border (small). |
| 201 | + * \brdrtnthmg Thick thin border (medium). |
| 202 | + * \brdrthtnmg Thin thick border (medium). |
| 203 | + * \brdrtnthtnmg Thin thick thin border (medium). |
| 204 | + * \brdrtnthlg Thick thin border (large). |
| 205 | + * \brdrthtnlg Thin thick border (large). |
| 206 | + * \brdrtnthtnlg Thin thick thin border (large). |
| 207 | + * \brdrwavy Wavy border. |
| 208 | + * \brdrwavydb Double wavy border. |
| 209 | + * \brdrdashdotstr Striped border. |
| 210 | + * \brdremboss Emboss border. |
| 211 | + * \brdrengrave Engrave border. |
| 212 | + */ |
| 213 | + switch ($borderStyle) { |
| 214 | + case Border::DOTTED: |
| 215 | + $content .= '\brdrdot'; |
| 216 | + |
| 217 | + break; |
| 218 | + case Border::SINGLE: |
| 219 | + default: |
| 220 | + $content .= '\brdrs'; |
| 221 | + |
| 222 | + break; |
| 223 | + } |
| 224 | + |
| 225 | + // \brdrwN N is the width in twips (1/20 pt) of the pen used to draw the paragraph border line. |
| 226 | + // N cannot be greater than 75. |
| 227 | + // To obtain a larger border width, the \brdth control word can be used to obtain a width double that of N. |
| 228 | + // $borderSize is in eights of a point, i.e. 4 / 8 = .5pt |
| 229 | + // 1/20 pt => 1/8 / 2.5 |
| 230 | + $content .= '\brdrw' . (int) ($borderSize / 2.5); |
| 231 | + |
| 232 | + // \brdrcfN N is the color of the paragraph border, specified as an index into the color table in the RTF header. |
| 233 | + $colorIndex = 0; |
| 234 | + $index = array_search($borderColor, $this->parentWriter->getColorTable()); |
| 235 | + if ($index !== false) { |
| 236 | + $colorIndex = (int) $index + 1; |
| 237 | + } |
| 238 | + $content .= '\brdrcf' . $colorIndex; |
| 239 | + $content .= PHP_EOL; |
| 240 | + |
| 241 | + return $content; |
| 242 | + } |
| 243 | + |
130 | 244 | /**
|
131 | 245 | * Get vertical merge style.
|
132 | 246 | *
|
|
0 commit comments