diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index b96865bada..d7f2290238 100644 --- a/docs/changes/1.x/1.5.0.md +++ b/docs/changes/1.x/1.5.0.md @@ -7,6 +7,7 @@ ### Bug fixes - Set writeAttribute return type by [@radarhere](https://github.com/radarhere) fixing [#2204](https://github.com/PHPOffice/PHPWord/issues/2204) in [#2776](https://github.com/PHPOffice/PHPWord/pull/2776) +- Writer RTF: Border - Improve add missing features, also adds spaces to Styles > Border and fixes hasBorder to check all border variables by [@rasamassen](https://github.com/rasamassen) in [#2838](https://github.com/PHPOffice/PHPWord/pull/2838) ### Miscellaneous @@ -16,4 +17,4 @@ ### BC Breaks -### Notes \ No newline at end of file +### Notes diff --git a/src/PhpWord/SimpleType/Border.php b/src/PhpWord/SimpleType/Border.php index acd1c1a1b1..03925e87d6 100644 --- a/src/PhpWord/SimpleType/Border.php +++ b/src/PhpWord/SimpleType/Border.php @@ -48,7 +48,7 @@ final class Border extends AbstractEnum const THIN_THICK_LARGE_GAP = 'thinThickLargeGap'; //A thin line contained within a thick line with a large-sized intermediate gap const THIN_THICK_MEDIUM_GAP = 'thinThickMediumGap'; //A thick line contained within a thin line with a medium-sized intermediate gap const THIN_THICK_SMALL_GAP = 'thinThickSmallGap'; //A thick line contained within a thin line with a small intermediate gap - const THIN_THICK_THINLARGE_GAP = 'thinThickThinLargeGap'; //A thin-thick-thin line with a large gap + const THIN_THICK_THIN_LARGE_GAP = 'thinThickThinLargeGap'; //A thin-thick-thin line with a large gap const THIN_THICK_THIN_MEDIUM_GAP = 'thinThickThinMediumGap'; //A thin-thick-thin line with a medium gap const THIN_THICK_THIN_SMALL_GAP = 'thinThickThinSmallGap'; //A thin-thick-thin line with a small gap const THREE_D_EMBOSS = 'threeDEmboss'; //A three-staged gradient line, getting darker towards the paragraph diff --git a/src/PhpWord/Style/Border.php b/src/PhpWord/Style/Border.php index 722e09931a..aa92ad386f 100644 --- a/src/PhpWord/Style/Border.php +++ b/src/PhpWord/Style/Border.php @@ -46,6 +46,13 @@ class Border extends AbstractStyle */ protected $borderTopStyle; + /** + * Border Top Space. For section and paragraph borders only. + * + * @var float|int + */ + protected $borderTopSpace; + /** * Border Left Size. * @@ -67,6 +74,13 @@ class Border extends AbstractStyle */ protected $borderLeftStyle; + /** + * Border Left Space. For section and paragraph borders only. + * + * @var float|int + */ + protected $borderLeftSpace; + /** * Border Right Size. * @@ -88,6 +102,13 @@ class Border extends AbstractStyle */ protected $borderRightStyle; + /** + * Border Right Space. For section and paragraph borders only. + * + * @var float|int + */ + protected $borderRightSpace; + /** * Border Bottom Size. * @@ -109,6 +130,13 @@ class Border extends AbstractStyle */ protected $borderBottomStyle; + /** + * Border Bottom Space. For section and paragraph borders only. + * + * @var float|int + */ + protected $borderBottomSpace; + /** * Top margin spacing. * @@ -140,7 +168,7 @@ class Border extends AbstractStyle /** * Get border size. * - * @return int[] + * @return array */ public function getBorderSize() { @@ -233,6 +261,38 @@ public function setBorderStyle($value = null) return $this; } + /** + * Get border space. + * + * @return array + */ + public function getBorderSpace() + { + return [ + $this->getBorderTopSpace(), + $this->getBorderLeftSpace(), + $this->getBorderRightSpace(), + $this->getBorderBottomSpace(), + ]; + } + + /** + * Set border space. + * + * @param float|int $value + * + * @return self + */ + public function setBorderSpace($value = null) + { + $this->setBorderTopSpace($value); + $this->setBorderLeftSpace($value); + $this->setBorderRightSpace($value); + $this->setBorderBottomSpace($value); + + return $this; + } + /** * Get border top size. * @@ -292,7 +352,7 @@ public function getBorderTopStyle() } /** - * Set border top Style. + * Set border top style. * * @param string $value * @@ -305,6 +365,30 @@ public function setBorderTopStyle($value = null) return $this; } + /** + * Get border top space. + * + * @return float|int + */ + public function getBorderTopSpace() + { + return $this->borderTopSpace; + } + + /** + * Set border top space. + * + * @param float|int $value + * + * @return self + */ + public function setBorderTopSpace($value = null) + { + $this->borderTopSpace = $value; + + return $this; + } + /** * Get border left size. * @@ -377,6 +461,30 @@ public function setBorderLeftStyle($value = null) return $this; } + /** + * Get border left space. + * + * @return float|int + */ + public function getBorderLeftSpace() + { + return $this->borderLeftSpace; + } + + /** + * Set border left space. + * + * @param float|int $value + * + * @return self + */ + public function setBorderLeftSpace($value = null) + { + $this->borderLeftSpace = $value; + + return $this; + } + /** * Get border right size. * @@ -449,6 +557,30 @@ public function setBorderRightStyle($value = null) return $this; } + /** + * Get border right space. + * + * @return float|int + */ + public function getBorderRightSpace() + { + return $this->borderRightSpace; + } + + /** + * Set border right space. + * + * @param float|int $value + * + * @return self + */ + public function setBorderRightSpace($value = null) + { + $this->borderRightSpace = $value; + + return $this; + } + /** * Get border bottom size. * @@ -521,6 +653,30 @@ public function setBorderBottomStyle($value = null) return $this; } + /** + * Get border bottom space. + * + * @return float|int + */ + public function getBorderBottomSpace() + { + return $this->borderBottomSpace; + } + + /** + * Set border bottom space. + * + * @param float|int $value + * + * @return self + */ + public function setBorderBottomSpace($value = null) + { + $this->borderBottomSpace = $value; + + return $this; + } + /** * Check if any of the border is not null. * @@ -528,7 +684,7 @@ public function setBorderBottomStyle($value = null) */ public function hasBorder() { - $borders = $this->getBorderSize(); + $borders = array_merge($this->getBorderSize(), $this->getBorderColor(), $this->getBorderStyle(), $this->getBorderSpace()); return $borders !== array_filter($borders, 'is_null'); } diff --git a/src/PhpWord/Writer/RTF/Style/Border.php b/src/PhpWord/Writer/RTF/Style/Border.php index c8dc943579..a2b5c7695c 100644 --- a/src/PhpWord/Writer/RTF/Style/Border.php +++ b/src/PhpWord/Writer/RTF/Style/Border.php @@ -18,6 +18,8 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; +use PhpOffice\PhpWord\SimpleType\Border as BorderType; + /** * Border style writer. * @@ -26,18 +28,11 @@ class Border extends AbstractStyle { /** - * Sizes. - * - * @var array - */ - private $sizes = []; - - /** - * Colors. + * Type. Can be section, paragraph, font, row, or cell. * - * @var array + * @var string */ - private $colors = []; + private $type = 'paragraph'; /** * Write style. @@ -46,23 +41,33 @@ class Border extends AbstractStyle */ public function write() { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Border) { + return ''; + } + $content = ''; + if ($this->type == 'section') { + // Page border measure + // 8 = from text, infront off; 32 = from edge, infront on; 40 = from edge, infront off + $content .= '\pgbrdropt32'; + } + $sides = ['top', 'left', 'right', 'bottom']; - $sizeCount = count($this->sizes); + $sizeCount = 4; + + if ($this->type == 'font') { + $sizeCount = 1; + } - // Page border measure - // 8 = from text, infront off; 32 = from edge, infront on; 40 = from edge, infront off - $content .= '\pgbrdropt32'; + $sizes = $style->getBorderSize(); + $colors = $style->getBorderColor(); + $styles = $style->getBorderStyle(); + $spaces = $style->getBorderSpace(); for ($i = 0; $i < $sizeCount; ++$i) { - if ($this->sizes[$i] !== null) { - $color = null; - if (isset($this->colors[$i])) { - $color = $this->colors[$i]; - } - $content .= $this->writeSide($sides[$i], $this->sizes[$i], $color); - } + $content .= $this->writeSide($sides[$i], $sizes[$i], $colors[$i], $styles[$i], $spaces[$i]); } return $content; @@ -72,53 +77,114 @@ public function write() * Write side. * * @param string $side - * @param int $width + * @param float|int $width * @param string $color + * @param string $style + * @param float|int $space * * @return string */ - private function writeSide($side, $width, $color = '') + private function writeSide($side, $width, $color, $style, $space) { - /** @var \PhpOffice\PhpWord\Writer\RTF $rtfWriter */ - $rtfWriter = $this->getParentWriter(); + if ($width == null && $color === null && $style === null) { + if ($this->type == 'font' || $this->type == 'row' || $this->type == 'cell') { + return ''; + } elseif ($space === null) { + return ''; + } + } + + $types = [ + 'section' => '\pgbrdr', + 'paragraph' => '\brdr', + 'font' => '\chbrdr', + 'row' => '\trbrdr', + 'cell' => '\clbrdr', + ]; + + $styles = [ + BorderType::SINGLE => '\brdrs', + BorderType::DASH_DOT_STROKED => '\brdrdashdotstr', + BorderType::DASHED => '\brdrdash', + BorderType::DASH_SMALL_GAP => '\brdrdashsm', + BorderType::DOT_DASH => '\brdrdashd', + BorderType::DOT_DOT_DASH => '\brdrdashdd', + BorderType::DOTTED => '\brdrdot', + BorderType::DOUBLE => '\brdrdb', + BorderType::DOUBLE_WAVE => '\brdrwavydb', + BorderType::INSET => '\brdrinset', + BorderType::NIL => '\brdrnil', + BorderType::NONE => '\brdrnone', + BorderType::OUTSET => '\brdroutset', + BorderType::THICK => '\brdrth', + BorderType::THICK_THIN_LARGE_GAP => '\brdrtnthlg', + BorderType::THICK_THIN_MEDIUM_GAP => '\brdrtnthmg', + BorderType::THICK_THIN_SMALL_GAP => '\brdrtnthsg', + BorderType::THIN_THICK_LARGE_GAP => '\brdrthtnlg', + BorderType::THIN_THICK_MEDIUM_GAP => '\brdrthtnmg', + BorderType::THIN_THICK_SMALL_GAP => '\brdrthtnsg', + BorderType::THIN_THICK_THIN_LARGE_GAP => '\brdrtnthtnlg', + BorderType::THIN_THICK_THIN_MEDIUM_GAP => '\brdrtnthtnmg', + BorderType::THIN_THICK_THIN_SMALL_GAP => '\brdrtnthtnsg', + BorderType::THREE_D_EMBOSS => '\brdremboss', + BorderType::THREE_D_ENGRAVE => '\brdrengrave', + BorderType::TRIPLE => '\brdrtriple', + BorderType::WAVE => '\brdrwavy', + ]; + + /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter */ + $parentWriter = $this->getParentWriter(); $colorIndex = 0; - if ($rtfWriter !== null) { - $colorTable = $rtfWriter->getColorTable(); - $index = array_search($color, $colorTable); + if ($parentWriter !== null) { + $index = array_search($color, $parentWriter->getColorTable()); if ($index !== false) { $colorIndex = $index + 1; } } $content = ''; + if (isset($types[$this->type])) { + if ($this->type == 'font') { + $content .= $types[$this->type]; // character borders cannot vary by side + } else { + $content .= $types[$this->type] . substr($side, 0, 1); + } + } else { + return ''; + } + + if (isset($styles[$style])) { + $content .= $styles[$style]; + } else { + $content .= '\brdrs'; // default style + } + $content .= $this->getValueIf($width !== null, '\brdrw' . round($width ?? 0)); // Width + $content .= $this->getValueIf($color !== null, '\brdrcf' . $colorIndex); // Color + + // Space + if ($this->type == 'section') { + $space = $space !== null ? $space : '480'; // section default is 480 + } elseif ($this->type == 'paragraph') { + if ($side == 'top' || $side == 'bottom') { + $space = $space !== null ? $space : '20'; // paragraph top|bottom default is 20 + } elseif ($side == 'left' || $side == 'right') { + $space = $space !== null ? $space : '80'; // paragraph left|rigth default is 80 + } + } + $content .= $this->getValueIf($space !== null, '\brsp' . round($space ?? 0)); - $content .= '\pgbrdr' . substr($side, 0, 1); - $content .= '\brdrs'; // Single-thickness border; @todo Get other type of border - $content .= '\brdrw' . round($width); // Width - $content .= '\brdrcf' . $colorIndex; // Color - $content .= '\brsp480'; // Space in twips between borders and the paragraph (24pt, following OOXML) $content .= ' '; return $content; } /** - * Set sizes. - * - * @param int[] $value - */ - public function setSizes($value): void - { - $this->sizes = $value; - } - - /** - * Set colors. + * Set type. * - * @param string[] $value + * @param string $value */ - public function setColors($value): void + public function setType($value = 'paragraph'): void { - $this->colors = $value; + $this->type = $value; } } diff --git a/src/PhpWord/Writer/RTF/Style/Paragraph.php b/src/PhpWord/Writer/RTF/Style/Paragraph.php index 040c60b5aa..41b570cecf 100644 --- a/src/PhpWord/Writer/RTF/Style/Paragraph.php +++ b/src/PhpWord/Writer/RTF/Style/Paragraph.php @@ -101,6 +101,14 @@ public function write() $styles = $style->getStyleValues(); $content .= $this->writeTabs($styles['tabs']); + // Borders + if ($style->hasBorder()) { + $styleWriter = new Border($style); + $styleWriter->setParentWriter($this->getParentWriter()); + $styleWriter->setType('paragraph'); + $content .= $styleWriter->write(); + } + return $content; } diff --git a/src/PhpWord/Writer/RTF/Style/Section.php b/src/PhpWord/Writer/RTF/Style/Section.php index 598015ed4d..47525ed289 100644 --- a/src/PhpWord/Writer/RTF/Style/Section.php +++ b/src/PhpWord/Writer/RTF/Style/Section.php @@ -61,8 +61,7 @@ public function write() if ($style->hasBorder()) { $styleWriter = new Border($style); $styleWriter->setParentWriter($this->getParentWriter()); - $styleWriter->setSizes($style->getBorderSize()); - $styleWriter->setColors($style->getBorderColor()); + $styleWriter->setType('section'); $content .= $styleWriter->write(); } diff --git a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php index b464e66ffb..f472e15cd4 100644 --- a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php +++ b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php @@ -30,7 +30,7 @@ class MarginBorder extends AbstractStyle /** * Sizes. * - * @var int[] + * @var array */ private $sizes = []; @@ -80,7 +80,7 @@ public function write(): void * Write side. * * @param string $side - * @param int $width + * @param float|int $width * @param string $color * @param string $borderStyle */ @@ -111,7 +111,7 @@ private function writeSide(XMLWriter $xmlWriter, $side, $width, $color = null, $ /** * Set sizes. * - * @param int[] $value + * @param array $value */ public function setSizes($value): void { diff --git a/tests/PhpWordTests/Writer/RTF/Style/BorderTest.php b/tests/PhpWordTests/Writer/RTF/Style/BorderTest.php new file mode 100644 index 0000000000..b1f2837c44 --- /dev/null +++ b/tests/PhpWordTests/Writer/RTF/Style/BorderTest.php @@ -0,0 +1,301 @@ +write()); + } + + /** + * Test Border styles in paragraph. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + */ + public function testBorderBasic(): void + { + $parentWriter = new RTF(); + $style = new BorderStyle(); + $writer = new BorderWriter($style); + $writer->setParentWriter($parentWriter); + + $expect = ''; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderSize(1); + $expect = '\brdrt\brdrs\brdrw1\brsp20 '; + $expect .= '\brdrl\brdrs\brdrw1\brsp80 '; + $expect .= '\brdrr\brdrs\brdrw1\brsp80 '; + $expect .= '\brdrb\brdrs\brdrw1\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + } + + /** + * Test Border not all all four sides. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + */ + public function testBorderSide(): void + { + $parentWriter = new RTF(); + $style = new BorderStyle(); + $writer = new BorderWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setBorderLeftSize(1); + $expect = '\brdrl\brdrs\brdrw1\brsp80 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderBottomSize(100); + $expect = '\brdrl\brdrs\brdrw1\brsp80 '; + $expect .= '\brdrb\brdrs\brdrw100\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + } + + /** + * Test Border styles in paragraph. + * See page 76 of RTF Specification 1.9.1 for Page Borders. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + * See page 103 of RTF Specification 1.9.1 for Row Borders and Cell Borders. + * See page 139 of RTF Specification 1.9.1 for Character Borders. + */ + public function testBorderType(): void + { + $parentWriter = new RTF(); + $style = new BorderStyle(); + $writer = new BorderWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setBorderSize(1); + + $writer->setType('section'); + $expect = '\pgbrdropt32'; + $expect .= '\pgbrdrt\brdrs\brdrw1\brsp480 '; + $expect .= '\pgbrdrl\brdrs\brdrw1\brsp480 '; + $expect .= '\pgbrdrr\brdrs\brdrw1\brsp480 '; + $expect .= '\pgbrdrb\brdrs\brdrw1\brsp480 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $writer->setType('paragraph'); + $expect = '\brdrt\brdrs\brdrw1\brsp20 '; + $expect .= '\brdrl\brdrs\brdrw1\brsp80 '; + $expect .= '\brdrr\brdrs\brdrw1\brsp80 '; + $expect .= '\brdrb\brdrs\brdrw1\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $writer->setType('font'); + $expect = '\chbrdr\brdrs\brdrw1 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $writer->setType('row'); + $expect = '\trbrdrt\brdrs\brdrw1 '; + $expect .= '\trbrdrl\brdrs\brdrw1 '; + $expect .= '\trbrdrr\brdrs\brdrw1 '; + $expect .= '\trbrdrb\brdrs\brdrw1 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $writer->setType('cell'); + $expect = '\clbrdrt\brdrs\brdrw1 '; + $expect .= '\clbrdrl\brdrs\brdrw1 '; + $expect .= '\clbrdrr\brdrs\brdrw1 '; + $expect .= '\clbrdrb\brdrs\brdrw1 '; + self::assertEquals($expect, $this->removeCr($writer)); + } + + /** + * Test Border style. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + */ + public function testBorderStyle(): void + { + $parentWriter = new RTF(); + $style = new BorderStyle(); + $writer = new BorderWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setBorderStyle(BorderType::DASH_DOT_STROKED); + $expect = '\brdrt\brdrdashdotstr\brsp20 '; + $expect .= '\brdrl\brdrdashdotstr\brsp80 '; + $expect .= '\brdrr\brdrdashdotstr\brsp80 '; + $expect .= '\brdrb\brdrdashdotstr\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopStyle(BorderType::DASHED); + $style->setBorderLeftStyle(BorderType::DASH_SMALL_GAP); + $style->setBorderRightStyle(BorderType::DOT_DASH); + $style->setBorderBottomStyle(BorderType::DOT_DOT_DASH); + $expect = '\brdrt\brdrdash\brsp20 '; + $expect .= '\brdrl\brdrdashsm\brsp80 '; + $expect .= '\brdrr\brdrdashd\brsp80 '; + $expect .= '\brdrb\brdrdashdd\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopStyle(BorderType::DOTTED); + $style->setBorderLeftStyle(BorderType::DOUBLE); + $style->setBorderRightStyle(BorderType::DOUBLE_WAVE); + $style->setBorderBottomStyle(BorderType::INSET); + $expect = '\brdrt\brdrdot\brsp20 '; + $expect .= '\brdrl\brdrdb\brsp80 '; + $expect .= '\brdrr\brdrwavydb\brsp80 '; + $expect .= '\brdrb\brdrinset\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopStyle(BorderType::NIL); + $style->setBorderLeftStyle(BorderType::NONE); + $style->setBorderRightStyle(BorderType::OUTSET); + $style->setBorderBottomStyle(BorderType::THICK); + $expect = '\brdrt\brdrnil\brsp20 '; + $expect .= '\brdrl\brdrnone\brsp80 '; + $expect .= '\brdrr\brdroutset\brsp80 '; + $expect .= '\brdrb\brdrth\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopStyle(BorderType::THICK_THIN_LARGE_GAP); + $style->setBorderLeftStyle(BorderType::THICK_THIN_MEDIUM_GAP); + $style->setBorderRightStyle(BorderType::THICK_THIN_SMALL_GAP); + $style->setBorderBottomStyle(BorderType::THIN_THICK_LARGE_GAP); + $expect = '\brdrt\brdrtnthlg\brsp20 '; + $expect .= '\brdrl\brdrtnthmg\brsp80 '; + $expect .= '\brdrr\brdrtnthsg\brsp80 '; + $expect .= '\brdrb\brdrthtnlg\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopStyle(BorderType::THIN_THICK_MEDIUM_GAP); + $style->setBorderLeftStyle(BorderType::THIN_THICK_SMALL_GAP); + $style->setBorderRightStyle(BorderType::THIN_THICK_THIN_LARGE_GAP); + $style->setBorderBottomStyle(BorderType::THIN_THICK_THIN_MEDIUM_GAP); + $expect = '\brdrt\brdrthtnmg\brsp20 '; + $expect .= '\brdrl\brdrthtnsg\brsp80 '; + $expect .= '\brdrr\brdrtnthtnlg\brsp80 '; + $expect .= '\brdrb\brdrtnthtnmg\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopStyle(BorderType::THIN_THICK_THIN_SMALL_GAP); + $style->setBorderLeftStyle(BorderType::THREE_D_EMBOSS); + $style->setBorderRightStyle(BorderType::THREE_D_ENGRAVE); + $style->setBorderBottomStyle(BorderType::TRIPLE); + $expect = '\brdrt\brdrtnthtnsg\brsp20 '; + $expect .= '\brdrl\brdremboss\brsp80 '; + $expect .= '\brdrr\brdrengrave\brsp80 '; + $expect .= '\brdrb\brdrtriple\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderStyle(BorderType::WAVE); + $expect = '\brdrt\brdrwavy\brsp20 '; + $expect .= '\brdrl\brdrwavy\brsp80 '; + $expect .= '\brdrr\brdrwavy\brsp80 '; + $expect .= '\brdrb\brdrwavy\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + } + + /** + * Test Border size. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + */ + public function testBorderSize(): void + { + $parentWriter = new RTF(); + $style = new BorderStyle(); + $writer = new BorderWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setBorderSize(100); + $expect = '\brdrt\brdrs\brdrw100\brsp20 '; + $expect .= '\brdrl\brdrs\brdrw100\brsp80 '; + $expect .= '\brdrr\brdrs\brdrw100\brsp80 '; + $expect .= '\brdrb\brdrs\brdrw100\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopSize(200); + $style->setBorderLeftSize(150); + $style->setBorderRightSize(50); + $style->setBorderBottomSize(20); + $expect = '\brdrt\brdrs\brdrw200\brsp20 '; + $expect .= '\brdrl\brdrs\brdrw150\brsp80 '; + $expect .= '\brdrr\brdrs\brdrw50\brsp80 '; + $expect .= '\brdrb\brdrs\brdrw20\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + } + + /** + * Test Border colors. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + * + * Create test when paragraph inherits border. + */ + + /** + * Test Border space. + * See page 89-90 of RTF Specification 1.9.1 for Paragraph Borders. + */ + public function testBorderSpace(): void + { + $parentWriter = new RTF(); + $style = new BorderStyle(); + $writer = new BorderWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setBorderSpace(100); + $expect = '\brdrt\brdrs\brsp100 '; + $expect .= '\brdrl\brdrs\brsp100 '; + $expect .= '\brdrr\brdrs\brsp100 '; + $expect .= '\brdrb\brdrs\brsp100 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setBorderTopSpace(200); + $style->setBorderLeftSpace(150); + $style->setBorderRightSpace(50); + $style->setBorderBottomSpace(20); + $expect = '\brdrt\brdrs\brsp200 '; + $expect .= '\brdrl\brdrs\brsp150 '; + $expect .= '\brdrr\brdrs\brsp50 '; + $expect .= '\brdrb\brdrs\brsp20 '; + self::assertEquals($expect, $this->removeCr($writer)); + + // Space doesn't matter for fonts. + $writer->setType('font'); + $expect = ''; + self::assertEquals($expect, $this->removeCr($writer)); + + // Space doesn't matter for rows. + $writer->setType('row'); + $expect = ''; + self::assertEquals($expect, $this->removeCr($writer)); + + // Space doesn't matter for cells. + $writer->setType('cell'); + $expect = ''; + self::assertEquals($expect, $this->removeCr($writer)); + } +} diff --git a/tests/PhpWordTests/Writer/RTF/StyleTest.php b/tests/PhpWordTests/Writer/RTF/StyleTest.php index 8ba2bcb9c9..ceeae5ee19 100644 --- a/tests/PhpWordTests/Writer/RTF/StyleTest.php +++ b/tests/PhpWordTests/Writer/RTF/StyleTest.php @@ -20,7 +20,6 @@ use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\RTF; -use PhpOffice\PhpWord\Writer\RTF\Style\Border; use PHPUnit\Framework\Assert; /** @@ -52,24 +51,6 @@ public function testEmptyStyles(): void } } - public function testBorderWithNonRegisteredColors(): void - { - $border = new Border(); - $border->setSizes([1, 2, 3, 4]); - $border->setColors(['#FF0000', '#FF0000', '#FF0000', '#FF0000']); - $border->setSizes([20, 20, 20, 20]); - - $content = $border->write(); - - $expected = '\pgbrdropt32'; - $expected .= '\pgbrdrt\brdrs\brdrw20\brdrcf0\brsp480 '; - $expected .= '\pgbrdrl\brdrs\brdrw20\brdrcf0\brsp480 '; - $expected .= '\pgbrdrr\brdrs\brdrw20\brdrcf0\brsp480 '; - $expected .= '\pgbrdrb\brdrs\brdrw20\brdrcf0\brsp480 '; - - self::assertEquals($expected, $content); - } - public function testIndentation(): void { $indentation = new \PhpOffice\PhpWord\Style\Indentation();