Skip to content

Commit 6cc39c4

Browse files
committed
RTF Writer : Support for Table Border Style
1 parent c917d03 commit 6cc39c4

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Word2007 Reader: Support for Paragraph Border Style by [@damienfa](https://github.com/damienfa) in [#2651](https://github.com/PHPOffice/PHPWord/pull/2651)
1010
- Word2007 Writer: Support for field REF by [@crystoline](https://github.com/crystoline) in [#2652](https://github.com/PHPOffice/PHPWord/pull/2652)
1111
- Word2007 Reader : Support for FormFields by [@vincentKool](https://github.com/vincentKool) in [#2653](https://github.com/PHPOffice/PHPWord/pull/2653)
12+
- RTF Writer : Support for Table Border Style fixing [#345](https://github.com/PHPOffice/PHPWord/issues/345) by [@Progi1984](https://github.com/Progi1984) in [#2656](https://github.com/PHPOffice/PHPWord/pull/2656)
1213

1314
### Bug fixes
1415

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ private function getTableStyle($tableStyle = null): string
120120
return '';
121121
}
122122
if (is_string($tableStyle)) {
123-
$style = ' class="' . $tableStyle;
124-
125-
return $style . '"';
123+
return ' class="' . $tableStyle . '"';
126124
}
127125

128126
$styleWriter = new TableStyleWriter($tableStyle);

src/PhpWord/Writer/RTF/Element/Table.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
use PhpOffice\PhpWord\Element\Row as RowElement;
2222
use PhpOffice\PhpWord\Element\Table as TableElement;
2323
use PhpOffice\PhpWord\Settings;
24+
use PhpOffice\PhpWord\SimpleType\Border;
2425
use PhpOffice\PhpWord\Style;
26+
use PhpOffice\PhpWord\Style\Cell as CellStyle;
27+
use PhpOffice\PhpWord\Style\Table as TableStyle;
2528

2629
/**
2730
* Table element RTF writer.
@@ -77,9 +80,15 @@ public function write()
7780
private function writeRowDef(RowElement $row)
7881
{
7982
$content = '';
83+
$tableStyle = $this->element->getStyle();
84+
if (is_string($tableStyle)) {
85+
$tableStyle = Style::getStyle($tableStyle);
86+
}
8087

8188
$rightMargin = 0;
8289
foreach ($row->getCells() as $cell) {
90+
$content .= $this->writeCellStyle($cell->getStyle(), $tableStyle);
91+
8392
$width = $cell->getWidth();
8493
$vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
8594
if ($width === null) {
@@ -127,6 +136,102 @@ private function writeCell(CellElement $cell)
127136
return $content;
128137
}
129138

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+
130235
/**
131236
* Get vertical merge style.
132237
*

src/PhpWord/Writer/RTF/Part/Header.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpOffice\PhpWord\Shared\Converter;
2222
use PhpOffice\PhpWord\Style;
2323
use PhpOffice\PhpWord\Style\Font;
24+
use PhpOffice\PhpWord\Style\Table;
2425

2526
/**
2627
* RTF header part writer.
@@ -236,6 +237,14 @@ private function registerFontItems($style): void
236237
$this->registerTableItem($this->fontTable, $style->getName(), $defaultFont);
237238
$this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor);
238239
$this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor);
240+
241+
return;
242+
}
243+
if ($style instanceof Table) {
244+
$this->registerTableItem($this->colorTable, $style->getBorderTopColor(), $defaultColor);
245+
$this->registerTableItem($this->colorTable, $style->getBorderRightColor(), $defaultColor);
246+
$this->registerTableItem($this->colorTable, $style->getBorderLeftColor(), $defaultColor);
247+
$this->registerTableItem($this->colorTable, $style->getBorderBottomColor(), $defaultColor);
239248
}
240249
}
241250

0 commit comments

Comments
 (0)