diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index b96865bada..6140f64e08 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: Improve Indentation and add missing commands by [@rasamassen](https://github.com/rasamassen) in [#2836](https://github.com/PHPOffice/PHPWord/pull/2836) ### Miscellaneous @@ -16,4 +17,4 @@ ### BC Breaks -### Notes \ No newline at end of file +### Notes diff --git a/src/PhpWord/Writer/RTF/Style/Indentation.php b/src/PhpWord/Writer/RTF/Style/Indentation.php index 589125a26a..60a95d599f 100644 --- a/src/PhpWord/Writer/RTF/Style/Indentation.php +++ b/src/PhpWord/Writer/RTF/Style/Indentation.php @@ -37,9 +37,13 @@ public function write() return ''; } - $content = '\fi' . round($style->getFirstLine()); - $content .= '\li' . round($style->getLeft()); - $content .= '\ri' . round($style->getRight()); + $content = ''; + + $content .= $this->getValueIf($style->getFirstLine() != 0, '\fi' . round($style->getFirstLine())); + $content .= $this->getValueIf($style->getFirstLineChars() != 0, '\cufi' . round($style->getFirstLineChars())); + $content .= $this->getValueIf($style->getHanging() != 0, '\fi-' . round($style->getHanging())); + $content .= $this->getValueIf($style->getLeft() != 0, '\li' . round($style->getLeft())); + $content .= $this->getValueIf($style->getRight() != 0, '\ri' . round($style->getRight())); return $content . ' '; } diff --git a/tests/PhpWordTests/Writer/RTF/Style/IndentationTest.php b/tests/PhpWordTests/Writer/RTF/Style/IndentationTest.php new file mode 100644 index 0000000000..f555263ddb --- /dev/null +++ b/tests/PhpWordTests/Writer/RTF/Style/IndentationTest.php @@ -0,0 +1,73 @@ +write()); + } + + /** + * Test indentation. + * See page 79 of RTF Specification 1.9.1 for Indentation. + */ + public function testIndentation(): void + { + $parentWriter = new RTF(); + $style = new IndentationStyle(); + $writer = new IndentationWriter($style); + $writer->setParentWriter($parentWriter); + + $expect = ' '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setLeft(1440); + $style->setRight(720); + $style->setFirstLine(360); + $style->setFirstLineChars(180); + $expect = '\fi360\cufi180\li1440\ri720 '; + self::assertEquals($expect, $this->removeCr($writer)); + + $style = new IndentationStyle(); + $writer = new IndentationWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setLeft(1440); + $style->setRight(720); + $style->setHanging(360); + $expect = '\fi-360\li1440\ri720 '; + self::assertEquals($expect, $this->removeCr($writer)); + } +} diff --git a/tests/PhpWordTests/Writer/RTF/StyleTest.php b/tests/PhpWordTests/Writer/RTF/StyleTest.php index 8ba2bcb9c9..377caed924 100644 --- a/tests/PhpWordTests/Writer/RTF/StyleTest.php +++ b/tests/PhpWordTests/Writer/RTF/StyleTest.php @@ -70,20 +70,6 @@ public function testBorderWithNonRegisteredColors(): void self::assertEquals($expected, $content); } - public function testIndentation(): void - { - $indentation = new \PhpOffice\PhpWord\Style\Indentation(); - $indentation->setLeft(1); - $indentation->setRight(2); - $indentation->setFirstLine(3); - - $indentWriter = new RTF\Style\Indentation($indentation); - $indentWriter->setParentWriter(new RTF()); - $result = $indentWriter->write(); - - Assert::assertEquals('\fi3\li1\ri2 ', $result); - } - public function testRightTab(): void { $tabRight = new \PhpOffice\PhpWord\Style\Tab();