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
This commit is contained in:
smaug1985 2018-07-14 02:13:45 +02:00 committed by troosan
parent cdc18522a2
commit 3906be19ee
5 changed files with 189 additions and 1 deletions

View File

@ -24,6 +24,7 @@ v0.15.0 (?? ??? 2018)
- Added parsing of internal links in HTML reader @lalop #1336
- Several improvements to charts @JAEK-S #1332
- Add parsing of html image in base64 format @jgpATs2w #1382
- Added Support for Indentation & Tabs on RTF Writer. @smaug1985 #1405
### Fixed
- Fix reading of docx default style - @troosan #1238

View File

@ -0,0 +1,45 @@
<?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
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\RTF\Style;
/**
* RTF indentation style writer
*
* @since 0.11.0
*/
class Indentation extends AbstractStyle
{
/**
* Write style
*
* @return string
*/
public function write()
{
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Indentation) {
return;
}
$content = '\fi' . $style->getFirstLine();
$content .= '\li' . $style->getLeft();
$content .= '\ri' . $style->getRight();
return $content . ' ';
}
}

View File

@ -64,9 +64,49 @@ class Paragraph extends AbstractStyle
if (isset($alignments[$style->getAlignment()])) {
$content .= $alignments[$style->getAlignment()];
}
$content .= $this->writeIndentation($style->getIndentation());
$content .= $this->getValueIf($spaceBefore !== null, '\sb' . $spaceBefore);
$content .= $this->getValueIf($spaceAfter !== null, '\sa' . $spaceAfter);
$styles = $style->getStyleValues();
$content .= $this->writeTabs($styles['tabs']);
return $content;
}
/**
* Writes an \PhpOffice\PhpWord\Style\Indentation
*
* @param null|\PhpOffice\PhpWord\Style\Indentation $indent
* @return string
*/
private function writeIndentation($indent = null)
{
if (isset($indent) && $indent instanceof \PhpOffice\PhpWord\Style\Indentation) {
$writer = new Indentation($indent);
return $writer->write();
}
return '';
}
/**
* Writes tabs
*
* @param \PhpOffice\PhpWord\Style\Tab[] $tabs
* @return string
*/
private function writeTabs($tabs = null)
{
$content = '';
if (!empty($tabs)) {
foreach ($tabs as $tab) {
$styleWriter = new Tab($tab);
$content .= $styleWriter->write();
}
}
return $content;
}

View File

@ -0,0 +1,49 @@
<?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
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\RTF\Style;
/**
* Line numbering style writer
*
* @since 0.10.0
*/
class Tab extends AbstractStyle
{
/**
* Write style.
*/
public function write()
{
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Tab) {
return;
}
$tabs = array(
\PhpOffice\PhpWord\Style\Tab::TAB_STOP_RIGHT => '\tqr',
\PhpOffice\PhpWord\Style\Tab::TAB_STOP_CENTER => '\tqc',
\PhpOffice\PhpWord\Style\Tab::TAB_STOP_DECIMAL => '\tqdec',
);
$content = '';
if (isset($tabs[$style->getType()])) {
$content .= $tabs[$style->getType()];
}
$content .= '\tx' . $style->getPosition();
return $content;
}
}

View File

@ -17,7 +17,9 @@
namespace PhpOffice\PhpWord\Writer\RTF;
use PhpOffice\PhpWord\Writer\RTF;
use PhpOffice\PhpWord\Writer\RTF\Style\Border;
use PHPUnit\Framework\Assert;
/**
* Test class for PhpOffice\PhpWord\Writer\RTF\Style subnamespace
@ -29,7 +31,7 @@ class StyleTest extends \PHPUnit\Framework\TestCase
*/
public function testEmptyStyles()
{
$styles = array('Font', 'Paragraph', 'Section');
$styles = array('Font', 'Paragraph', 'Section', 'Tab', 'Indentation');
foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Style\\' . $style;
$object = new $objectClass();
@ -55,4 +57,55 @@ class StyleTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($expected, $content);
}
public function testIndentation()
{
$indentation = new \PhpOffice\PhpWord\Style\Indentation();
$indentation->setLeft(1);
$indentation->setRight(2);
$indentation->setFirstLine(3);
$indentWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Indentation($indentation);
$indentWriter->setParentWriter(new RTF());
$result = $indentWriter->write();
Assert::assertEquals('\fi3\li1\ri2 ', $result);
}
public function testRightTab()
{
$tabRight = new \PhpOffice\PhpWord\Style\Tab();
$tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_RIGHT);
$tabRight->setPosition(5);
$tabWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Tab($tabRight);
$tabWriter->setParentWriter(new RTF());
$result = $tabWriter->write();
Assert::assertEquals('\tqr\tx5', $result);
}
public function testCenterTab()
{
$tabRight = new \PhpOffice\PhpWord\Style\Tab();
$tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_CENTER);
$tabWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Tab($tabRight);
$tabWriter->setParentWriter(new RTF());
$result = $tabWriter->write();
Assert::assertEquals('\tqc\tx0', $result);
}
public function testDecimalTab()
{
$tabRight = new \PhpOffice\PhpWord\Style\Tab();
$tabRight->setType(\PhpOffice\PhpWord\Style\Tab::TAB_STOP_DECIMAL);
$tabWriter = new \PhpOffice\PhpWord\Writer\RTF\Style\Tab($tabRight);
$tabWriter->setParentWriter(new RTF());
$result = $tabWriter->write();
Assert::assertEquals('\tqdec\tx0', $result);
}
}