Skip to content

Commit

Permalink
RTF Writer : Support for Table Border Style
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 13, 2024
1 parent c917d03 commit de666b9
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 66 deletions.
1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Word2007 Reader: Support for Paragraph Border Style by [@damienfa](https://github.com/damienfa) in [#2651](https://github.com/PHPOffice/PHPWord/pull/2651)
- Word2007 Writer: Support for field REF by [@crystoline](https://github.com/crystoline) in [#2652](https://github.com/PHPOffice/PHPWord/pull/2652)
- Word2007 Reader : Support for FormFields by [@vincentKool](https://github.com/vincentKool) in [#2653](https://github.com/PHPOffice/PHPWord/pull/2653)
- 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)

### Bug fixes

Expand Down
28 changes: 14 additions & 14 deletions src/PhpWord/Style/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Border extends AbstractStyle
/**
* Border Top Color.
*
* @var string
* @var null|string
*/
protected $borderTopColor;

Expand All @@ -55,7 +55,7 @@ class Border extends AbstractStyle
/**
* Border Left Color.
*
* @var string
* @var null|string
*/
protected $borderLeftColor;

Expand All @@ -76,7 +76,7 @@ class Border extends AbstractStyle
/**
* Border Right Color.
*
* @var string
* @var null|string
*/
protected $borderRightColor;

Expand All @@ -97,7 +97,7 @@ class Border extends AbstractStyle
/**
* Border Bottom Color.
*
* @var string
* @var null|string
*/
protected $borderBottomColor;

Expand Down Expand Up @@ -171,7 +171,7 @@ public function setBorderSize($value = null)
/**
* Get border color.
*
* @return string[]
* @return array<null|string>
*/
public function getBorderColor()
{
Expand All @@ -186,7 +186,7 @@ public function getBorderColor()
/**
* Set border color.
*
* @param string $value
* @param null|string $value
*
* @return self
*/
Expand Down Expand Up @@ -259,7 +259,7 @@ public function setBorderTopSize($value = null)
/**
* Get border top color.
*
* @return string
* @return null|string
*/
public function getBorderTopColor()
{
Expand All @@ -269,7 +269,7 @@ public function getBorderTopColor()
/**
* Set border top color.
*
* @param string $value
* @param null|string $value
*
* @return self
*/
Expand Down Expand Up @@ -331,7 +331,7 @@ public function setBorderLeftSize($value = null)
/**
* Get border left color.
*
* @return string
* @return null|string
*/
public function getBorderLeftColor()
{
Expand All @@ -341,7 +341,7 @@ public function getBorderLeftColor()
/**
* Set border left color.
*
* @param string $value
* @param null|string $value
*
* @return self
*/
Expand Down Expand Up @@ -403,7 +403,7 @@ public function setBorderRightSize($value = null)
/**
* Get border right color.
*
* @return string
* @return null|string
*/
public function getBorderRightColor()
{
Expand All @@ -413,7 +413,7 @@ public function getBorderRightColor()
/**
* Set border right color.
*
* @param string $value
* @param null|string $value
*
* @return self
*/
Expand Down Expand Up @@ -475,7 +475,7 @@ public function setBorderBottomSize($value = null)
/**
* Get border bottom color.
*
* @return string
* @return null|string
*/
public function getBorderBottomColor()
{
Expand All @@ -485,7 +485,7 @@ public function getBorderBottomColor()
/**
* Set border bottom color.
*
* @param string $value
* @param null|string $value
*
* @return self
*/
Expand Down
4 changes: 1 addition & 3 deletions src/PhpWord/Writer/HTML/Element/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ private function getTableStyle($tableStyle = null): string
return '';
}
if (is_string($tableStyle)) {
$style = ' class="' . $tableStyle;

return $style . '"';
return ' class="' . $tableStyle . '"';
}

$styleWriter = new TableStyleWriter($tableStyle);
Expand Down
6 changes: 3 additions & 3 deletions src/PhpWord/Writer/RTF/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
use PhpOffice\PhpWord\Writer\AbstractWriter;
use PhpOffice\PhpWord\Writer\RTF as WriterRTF;
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;

Expand All @@ -38,7 +38,7 @@ abstract class AbstractElement
/**
* Parent writer.
*
* @var \PhpOffice\PhpWord\Writer\AbstractWriter
* @var WriterRTF
*/
protected $parentWriter;

Expand Down Expand Up @@ -82,7 +82,7 @@ abstract public function write();
*/
protected $escaper;

public function __construct(AbstractWriter $parentWriter, Element $element, bool $withoutP = false)
public function __construct(WriterRTF $parentWriter, Element $element, bool $withoutP = false)
{
$this->parentWriter = $parentWriter;
$this->element = $element;
Expand Down
114 changes: 114 additions & 0 deletions src/PhpWord/Writer/RTF/Element/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
use PhpOffice\PhpWord\Element\Row as RowElement;
use PhpOffice\PhpWord\Element\Table as TableElement;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Border;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Cell as CellStyle;
use PhpOffice\PhpWord\Style\Table as TableStyle;

/**
* Table element RTF writer.
Expand All @@ -30,6 +33,11 @@
*/
class Table extends AbstractElement
{
/**
* @var TableElement
*/
protected $element;

/**
* Write element.
*
Expand Down Expand Up @@ -77,9 +85,18 @@ public function write()
private function writeRowDef(RowElement $row)
{
$content = '';
$tableStyle = $this->element->getStyle();
if (is_string($tableStyle)) {
$tableStyle = Style::getStyle($tableStyle);
if (!($tableStyle instanceof TableStyle)) {
$tableStyle = null;
}
}

$rightMargin = 0;
foreach ($row->getCells() as $cell) {
$content .= $this->writeCellStyle($cell->getStyle(), $tableStyle);

$width = $cell->getWidth();
$vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
if ($width === null) {
Expand Down Expand Up @@ -127,6 +144,103 @@ private function writeCell(CellElement $cell)
return $content;
}

private function writeCellStyle(CellStyle $cell, ?TableStyle $table): string
{
$content = $this->writeCellBorder(
't',
$cell->getBorderTopStyle() ?: ($table ? $table->getBorderTopStyle() : null),
(int) round($cell->getBorderTopSize() ?: ($table ? ($table->getBorderTopSize() ?: 0) : 0)),
$cell->getBorderTopColor() ?? ($table ? $table->getBorderTopColor() : null)
);
$content .= $this->writeCellBorder(
'l',
$cell->getBorderLeftStyle() ?: ($table ? $table->getBorderLeftStyle() : null),
(int) round($cell->getBorderLeftSize() ?: ($table ? ($table->getBorderLeftSize() ?: 0) : 0)),
$cell->getBorderLeftColor() ?? ($table ? $table->getBorderLeftColor() : null)
);
$content .= $this->writeCellBorder(
'b',
$cell->getBorderBottomStyle() ?: ($table ? $table->getBorderBottomStyle() : null),
(int) round($cell->getBorderBottomSize() ?: ($table ? ($table->getBorderBottomSize() ?: 0) : 0)),
$cell->getBorderBottomColor() ?? ($table ? $table->getBorderBottomColor() : null)
);
$content .= $this->writeCellBorder(
'r',
$cell->getBorderRightStyle() ?: ($table ? $table->getBorderRightStyle() : null),
(int) round($cell->getBorderRightSize() ?: ($table ? ($table->getBorderRightSize() ?: 0) : 0)),
$cell->getBorderRightColor() ?? ($table ? $table->getBorderRightColor() : null)
);

return $content;
}

private function writeCellBorder(string $prefix, ?string $borderStyle, int $borderSize, ?string $borderColor): string
{
if ($borderSize == 0) {
return '';
}

$content = '\clbrdr' . $prefix;
/**
* \brdrs Single-thickness border.
* \brdrth Double-thickness border.
* \brdrsh Shadowed border.
* \brdrdb Double border.
* \brdrdot Dotted border.
* \brdrdash Dashed border.
* \brdrhair Hairline border.
* \brdrinset Inset border.
* \brdrdashsm Dash border (small).
* \brdrdashd Dot dash border.
* \brdrdashdd Dot dot dash border.
* \brdroutset Outset border.
* \brdrtriple Triple border.
* \brdrtnthsg Thick thin border (small).
* \brdrthtnsg Thin thick border (small).
* \brdrtnthtnsg Thin thick thin border (small).
* \brdrtnthmg Thick thin border (medium).
* \brdrthtnmg Thin thick border (medium).
* \brdrtnthtnmg Thin thick thin border (medium).
* \brdrtnthlg Thick thin border (large).
* \brdrthtnlg Thin thick border (large).
* \brdrtnthtnlg Thin thick thin border (large).
* \brdrwavy Wavy border.
* \brdrwavydb Double wavy border.
* \brdrdashdotstr Striped border.
* \brdremboss Emboss border.
* \brdrengrave Engrave border.
*/
switch ($borderStyle) {
case Border::DOTTED:
$content .= '\brdrdot';

break;
case Border::SINGLE:
default:
$content .= '\brdrs';

break;
}

// \brdrwN N is the width in twips (1/20 pt) of the pen used to draw the paragraph border line.
// N cannot be greater than 75.
// To obtain a larger border width, the \brdth control word can be used to obtain a width double that of N.
// $borderSize is in eights of a point, i.e. 4 / 8 = .5pt
// 1/20 pt => 1/8 / 2.5
$content .= '\brdrw' . (int) ($borderSize / 2.5);

// \brdrcfN N is the color of the paragraph border, specified as an index into the color table in the RTF header.
$colorIndex = 0;
$index = array_search($borderColor, $this->parentWriter->getColorTable());
if ($index !== false) {
$colorIndex = (int) $index + 1;
}
$content .= '\brdrcf' . $colorIndex;
$content .= PHP_EOL;

return $content;
}

/**
* Get vertical merge style.
*
Expand Down
9 changes: 9 additions & 0 deletions src/PhpWord/Writer/RTF/Part/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Table;

/**
* RTF header part writer.
Expand Down Expand Up @@ -236,6 +237,14 @@ private function registerFontItems($style): void
$this->registerTableItem($this->fontTable, $style->getName(), $defaultFont);
$this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor);
$this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor);

return;
}
if ($style instanceof Table) {
$this->registerTableItem($this->colorTable, $style->getBorderTopColor(), $defaultColor);
$this->registerTableItem($this->colorTable, $style->getBorderRightColor(), $defaultColor);
$this->registerTableItem($this->colorTable, $style->getBorderLeftColor(), $defaultColor);
$this->registerTableItem($this->colorTable, $style->getBorderBottomColor(), $defaultColor);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/Word2007/Style/MarginBorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function setSizes($value): void
/**
* Set colors.
*
* @param string[] $value
* @param array<null|string> $value
*/
public function setColors($value): void
{
Expand Down
Loading

0 comments on commit de666b9

Please sign in to comment.