diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index b96865bada..3067e807d4 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: Support for type 'bar' and leaders in Tab by [@rasamassen](https://github.com/rasamassen) in [#2815](https://github.com/PHPOffice/PHPWord/pull/2815), partially fixing [#862](https://github.com/PHPOffice/PHPWord/issues/862) ### Miscellaneous @@ -16,4 +17,4 @@ ### BC Breaks -### Notes \ No newline at end of file +### Notes diff --git a/src/PhpWord/Writer/RTF/Style/Tab.php b/src/PhpWord/Writer/RTF/Style/Tab.php index 95e1f10a5c..1dc5c5730b 100644 --- a/src/PhpWord/Writer/RTF/Style/Tab.php +++ b/src/PhpWord/Writer/RTF/Style/Tab.php @@ -18,6 +18,8 @@ namespace PhpOffice\PhpWord\Writer\RTF\Style; +use PhpOffice\PhpWord\Style\Tab as TabStyle; + /** * Line numbering style writer. * @@ -31,19 +33,31 @@ class Tab extends AbstractStyle public function write() { $style = $this->getStyle(); - if (!$style instanceof \PhpOffice\PhpWord\Style\Tab) { + if (!$style instanceof TabStyle) { return; } $tabs = [ - \PhpOffice\PhpWord\Style\Tab::TAB_STOP_RIGHT => '\tqr', - \PhpOffice\PhpWord\Style\Tab::TAB_STOP_CENTER => '\tqc', - \PhpOffice\PhpWord\Style\Tab::TAB_STOP_DECIMAL => '\tqdec', + TabStyle::TAB_STOP_RIGHT => '\tqr', + TabStyle::TAB_STOP_CENTER => '\tqc', + TabStyle::TAB_STOP_DECIMAL => '\tqdec', + TabStyle::TAB_LEADER_DOT => '\tldot', + TabStyle::TAB_LEADER_HYPHEN => '\tlhyph', + TabStyle::TAB_LEADER_UNDERSCORE => '\tlul', + TabStyle::TAB_LEADER_HEAVY => '\tlth', + TabStyle::TAB_LEADER_MIDDLEDOT => '\tlmdot', ]; $content = ''; if (isset($tabs[$style->getType()])) { $content .= $tabs[$style->getType()]; } - $content .= '\tx' . round($style->getPosition()); + if (isset($tabs[$style->getLeader()]) && $style->getType() != TabStyle::TAB_STOP_BAR) { + $content .= $tabs[$style->getLeader()]; + } + if ($style->getType() == TabStyle::TAB_STOP_BAR) { + $content .= '\tb' . round($style->getPosition()); + } else { + $content .= '\tx' . round($style->getPosition()); + } return $content; } diff --git a/tests/PhpWordTests/Writer/RTF/Style/TabTest.php b/tests/PhpWordTests/Writer/RTF/Style/TabTest.php new file mode 100644 index 0000000000..809224f679 --- /dev/null +++ b/tests/PhpWordTests/Writer/RTF/Style/TabTest.php @@ -0,0 +1,119 @@ +write()); + } + + /** + * Test tab stops. + * See page 83 of RTF Specification 1.9.1 for Tabs. + */ + public function testTabStop(): void + { + $parentWriter = new RTF(); + $style = new TabStyle(); + $writer = new TabWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setPosition(3000); + $style->setType($style::TAB_STOP_CLEAR); + $expect = '\tx3000'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setType(TabStyle::TAB_STOP_LEFT); + $expect = '\tx3000'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setType(TabStyle::TAB_STOP_CENTER); + $expect = '\tqc\tx3000'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setType(TabStyle::TAB_STOP_RIGHT); + $expect = '\tqr\tx3000'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setType(TabStyle::TAB_STOP_DECIMAL); + $expect = '\tqdec\tx3000'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setType(TabStyle::TAB_STOP_BAR); + $expect = '\tb3000'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setType(TabStyle::TAB_STOP_NUM); // No equivalent specified in RTF + $expect = '\tx3000'; + self::assertEquals($expect, $this->removeCr($writer)); + } + + /** + * Test tab leaders. + * See page 83 of RTF Specification 1.9.1 for Tabs. + */ + public function testTabLeader(): void + { + $parentWriter = new RTF(); + $style = new TabStyle(); + $writer = new TabWriter($style); + $writer->setParentWriter($parentWriter); + + $style->setPosition(600); + $style->setLeader(TabStyle::TAB_LEADER_NONE); + $expect = '\tx600'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setLeader(TabStyle::TAB_LEADER_DOT); + $expect = '\tldot\tx600'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setLeader(TabStyle::TAB_LEADER_HYPHEN); + $expect = '\tlhyph\tx600'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setLeader(TabStyle::TAB_LEADER_UNDERSCORE); + $expect = '\tlul\tx600'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setLeader(TabStyle::TAB_LEADER_HEAVY); + $expect = '\tlth\tx600'; + self::assertEquals($expect, $this->removeCr($writer)); + + $style->setLeader(TabStyle::TAB_LEADER_MIDDLEDOT); + $expect = '\tlmdot\tx600'; + self::assertEquals($expect, $this->removeCr($writer)); + } +} diff --git a/tests/PhpWordTests/Writer/RTF/StyleTest.php b/tests/PhpWordTests/Writer/RTF/StyleTest.php index 8ba2bcb9c9..aa44d648c9 100644 --- a/tests/PhpWordTests/Writer/RTF/StyleTest.php +++ b/tests/PhpWordTests/Writer/RTF/StyleTest.php @@ -84,43 +84,6 @@ public function testIndentation(): void Assert::assertEquals('\fi3\li1\ri2 ', $result); } - public function testRightTab(): void - { - $tabRight = new \PhpOffice\PhpWord\Style\Tab(); - $tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_RIGHT); - $tabRight->setPosition(5); - - $tabWriter = new RTF\Style\Tab($tabRight); - $tabWriter->setParentWriter(new RTF()); - $result = $tabWriter->write(); - - Assert::assertEquals('\tqr\tx5', $result); - } - - public function testCenterTab(): void - { - $tabRight = new \PhpOffice\PhpWord\Style\Tab(); - $tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_CENTER); - - $tabWriter = new RTF\Style\Tab($tabRight); - $tabWriter->setParentWriter(new RTF()); - $result = $tabWriter->write(); - - Assert::assertEquals('\tqc\tx0', $result); - } - - public function testDecimalTab(): void - { - $tabRight = new \PhpOffice\PhpWord\Style\Tab(); - $tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_DECIMAL); - - $tabWriter = new RTF\Style\Tab($tabRight); - $tabWriter->setParentWriter(new RTF()); - $result = $tabWriter->write(); - - Assert::assertEquals('\tqdec\tx0', $result); - } - public function testRTL(): void { $parentWriter = new RTF();