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