Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/changes/1.x/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,4 +17,4 @@

### BC Breaks

### Notes
### Notes
24 changes: 19 additions & 5 deletions src/PhpWord/Writer/RTF/Style/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace PhpOffice\PhpWord\Writer\RTF\Style;

use PhpOffice\PhpWord\Style\Tab as TabStyle;

/**
* Line numbering style writer.
*
Expand All @@ -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;
}
Expand Down
119 changes: 119 additions & 0 deletions tests/PhpWordTests/Writer/RTF/Style/TabTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWordTests\Writer\RTF\Style;

use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style\Tab as TabStyle;
use PhpOffice\PhpWord\Writer\RTF;
use PhpOffice\PhpWord\Writer\RTF\Style\Tab as TabWriter;
use PHPUnit\Framework\TestCase;

class TabTest extends TestCase
{
protected function tearDown(): void
{
Settings::setDefaultRtl(null);
}

/**
* @param TabWriter $field
*/
public function removeCr($field): string
{
return str_replace("\r\n", "\n", $field->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));
}
}
37 changes: 0 additions & 37 deletions tests/PhpWordTests/Writer/RTF/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading