Skip to content

Commit 3906be1

Browse files
smaug1985troosan
authored andcommitted
Added Support for Indentation & Tabs on RTF Writer. (#1405)
* Added Support for Indentation & Tabs on RTF Writer. * add decimal tab writer + tests * Update CHANGELOG.md
1 parent cdc1852 commit 3906be1

File tree

5 files changed

+189
-1
lines changed

5 files changed

+189
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ v0.15.0 (?? ??? 2018)
2424
- Added parsing of internal links in HTML reader @lalop #1336
2525
- Several improvements to charts @JAEK-S #1332
2626
- Add parsing of html image in base64 format @jgpATs2w #1382
27+
- Added Support for Indentation & Tabs on RTF Writer. @smaug1985 #1405
2728

2829
### Fixed
2930
- Fix reading of docx default style - @troosan #1238
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\RTF\Style;
19+
20+
/**
21+
* RTF indentation style writer
22+
*
23+
* @since 0.11.0
24+
*/
25+
class Indentation extends AbstractStyle
26+
{
27+
/**
28+
* Write style
29+
*
30+
* @return string
31+
*/
32+
public function write()
33+
{
34+
$style = $this->getStyle();
35+
if (!$style instanceof \PhpOffice\PhpWord\Style\Indentation) {
36+
return;
37+
}
38+
39+
$content = '\fi' . $style->getFirstLine();
40+
$content .= '\li' . $style->getLeft();
41+
$content .= '\ri' . $style->getRight();
42+
43+
return $content . ' ';
44+
}
45+
}

src/PhpWord/Writer/RTF/Style/Paragraph.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,49 @@ public function write()
6464
if (isset($alignments[$style->getAlignment()])) {
6565
$content .= $alignments[$style->getAlignment()];
6666
}
67+
$content .= $this->writeIndentation($style->getIndentation());
6768
$content .= $this->getValueIf($spaceBefore !== null, '\sb' . $spaceBefore);
6869
$content .= $this->getValueIf($spaceAfter !== null, '\sa' . $spaceAfter);
6970

71+
$styles = $style->getStyleValues();
72+
$content .= $this->writeTabs($styles['tabs']);
73+
74+
return $content;
75+
}
76+
77+
/**
78+
* Writes an \PhpOffice\PhpWord\Style\Indentation
79+
*
80+
* @param null|\PhpOffice\PhpWord\Style\Indentation $indent
81+
* @return string
82+
*/
83+
private function writeIndentation($indent = null)
84+
{
85+
if (isset($indent) && $indent instanceof \PhpOffice\PhpWord\Style\Indentation) {
86+
$writer = new Indentation($indent);
87+
88+
return $writer->write();
89+
}
90+
91+
return '';
92+
}
93+
94+
/**
95+
* Writes tabs
96+
*
97+
* @param \PhpOffice\PhpWord\Style\Tab[] $tabs
98+
* @return string
99+
*/
100+
private function writeTabs($tabs = null)
101+
{
102+
$content = '';
103+
if (!empty($tabs)) {
104+
foreach ($tabs as $tab) {
105+
$styleWriter = new Tab($tab);
106+
$content .= $styleWriter->write();
107+
}
108+
}
109+
70110
return $content;
71111
}
72112

src/PhpWord/Writer/RTF/Style/Tab.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\RTF\Style;
19+
20+
/**
21+
* Line numbering style writer
22+
*
23+
* @since 0.10.0
24+
*/
25+
class Tab extends AbstractStyle
26+
{
27+
/**
28+
* Write style.
29+
*/
30+
public function write()
31+
{
32+
$style = $this->getStyle();
33+
if (!$style instanceof \PhpOffice\PhpWord\Style\Tab) {
34+
return;
35+
}
36+
$tabs = array(
37+
\PhpOffice\PhpWord\Style\Tab::TAB_STOP_RIGHT => '\tqr',
38+
\PhpOffice\PhpWord\Style\Tab::TAB_STOP_CENTER => '\tqc',
39+
\PhpOffice\PhpWord\Style\Tab::TAB_STOP_DECIMAL => '\tqdec',
40+
);
41+
$content = '';
42+
if (isset($tabs[$style->getType()])) {
43+
$content .= $tabs[$style->getType()];
44+
}
45+
$content .= '\tx' . $style->getPosition();
46+
47+
return $content;
48+
}
49+
}

tests/PhpWord/Writer/RTF/StyleTest.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\RTF;
1919

20+
use PhpOffice\PhpWord\Writer\RTF;
2021
use PhpOffice\PhpWord\Writer\RTF\Style\Border;
22+
use PHPUnit\Framework\Assert;
2123

2224
/**
2325
* Test class for PhpOffice\PhpWord\Writer\RTF\Style subnamespace
@@ -29,7 +31,7 @@ class StyleTest extends \PHPUnit\Framework\TestCase
2931
*/
3032
public function testEmptyStyles()
3133
{
32-
$styles = array('Font', 'Paragraph', 'Section');
34+
$styles = array('Font', 'Paragraph', 'Section', 'Tab', 'Indentation');
3335
foreach ($styles as $style) {
3436
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Style\\' . $style;
3537
$object = new $objectClass();
@@ -55,4 +57,55 @@ public function testBorderWithNonRegisteredColors()
5557

5658
$this->assertEquals($expected, $content);
5759
}
60+
61+
public function testIndentation()
62+
{
63+
$indentation = new \PhpOffice\PhpWord\Style\Indentation();
64+
$indentation->setLeft(1);
65+
$indentation->setRight(2);
66+
$indentation->setFirstLine(3);
67+
68+
$indentWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Indentation($indentation);
69+
$indentWriter->setParentWriter(new RTF());
70+
$result = $indentWriter->write();
71+
72+
Assert::assertEquals('\fi3\li1\ri2 ', $result);
73+
}
74+
75+
public function testRightTab()
76+
{
77+
$tabRight = new \PhpOffice\PhpWord\Style\Tab();
78+
$tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_RIGHT);
79+
$tabRight->setPosition(5);
80+
81+
$tabWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Tab($tabRight);
82+
$tabWriter->setParentWriter(new RTF());
83+
$result = $tabWriter->write();
84+
85+
Assert::assertEquals('\tqr\tx5', $result);
86+
}
87+
88+
public function testCenterTab()
89+
{
90+
$tabRight = new \PhpOffice\PhpWord\Style\Tab();
91+
$tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_CENTER);
92+
93+
$tabWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Tab($tabRight);
94+
$tabWriter->setParentWriter(new RTF());
95+
$result = $tabWriter->write();
96+
97+
Assert::assertEquals('\tqc\tx0', $result);
98+
}
99+
100+
public function testDecimalTab()
101+
{
102+
$tabRight = new \PhpOffice\PhpWord\Style\Tab();
103+
$tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_DECIMAL);
104+
105+
$tabWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Tab($tabRight);
106+
$tabWriter->setParentWriter(new RTF());
107+
$result = $tabWriter->write();
108+
109+
Assert::assertEquals('\tqdec\tx0', $result);
110+
}
58111
}

0 commit comments

Comments
 (0)