Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements in RTF writer #1755

Merged
merged 3 commits into from
Dec 8, 2019
Merged
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
12 changes: 9 additions & 3 deletions src/PhpWord/Escaper/Rtf.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ class Rtf extends AbstractEscaper
{
protected function escapeAsciiCharacter($code)
{
if (20 > $code || $code >= 80) {
return '{\u' . $code . '}';
if ($code == 9) {
return '{\\tab}';
}
if (0x20 > $code || $code >= 0x80) {
return '{\\u' . $code . '}';
}
if ($code == 123 || $code == 125 || $code == 92) { // open or close brace or backslash
return '\\' . chr($code);
}

return chr($code);
}

protected function escapeMultibyteCharacter($code)
{
return '\uc0{\u' . $code . '}';
return '\\uc0{\\u' . $code . '}';
}

/**
Expand Down
83 changes: 83 additions & 0 deletions tests/PhpWord/Escaper/RtfEscaper2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\Escaper;

/**
* Test class for PhpOffice\PhpWord\Escaper\RTF
*/
class RtfEscaper2Test extends \PHPUnit\Framework\TestCase
{
const HEADER = '\\pard\\nowidctlpar {\\cf0\\f0 ';
const TRAILER = '}\\par';

public function escapestring($str)
{
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
$parentWriter = new \PhpOffice\PhpWord\Writer\RTF();
$element = new \PhpOffice\PhpWord\Element\Text($str);
$txt = new \PhpOffice\PhpWord\Writer\RTF\Element\Text($parentWriter, $element);
$txt2 = trim($txt->write());

return $txt2;
}

public function expect($str)
{
return self::HEADER . $str . self::TRAILER;
}

/**
* Test special characters which require escaping
*/
public function testSpecial()
{
$str = 'Special characters { open brace } close brace \\ backslash';
$expect = $this->expect('Special characters \\{ open brace \\} close brace \\\\ backslash');
$this->assertEquals($expect, $this->escapestring($str));
}

/**
* Test accented character
*/
public function testAccent()
{
$str = 'Voilà - string with accented char';
$expect = $this->expect('Voil\\uc0{\\u224} - string with accented char');
$this->assertEquals($expect, $this->escapestring($str));
}

/**
* Test Hebrew
*/
public function testHebrew()
{
$str = 'Hebrew - שלום';
$expect = $this->expect('Hebrew - \\uc0{\\u1513}\\uc0{\\u1500}\\uc0{\\u1493}\\uc0{\\u1501}');
$this->assertEquals($expect, $this->escapestring($str));
}

/**
* Test tab
*/
public function testTab()
{
$str = "Here's a tab\tfollowed by more characters.";
$expect = $this->expect("Here's a tab{\\tab}followed by more characters.");
$this->assertEquals($expect, $this->escapestring($str));
}
}
13 changes: 9 additions & 4 deletions tests/PhpWord/Writer/RTF/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
class ElementTest extends \PHPUnit\Framework\TestCase
{
public function removeCr($field)
{
return str_replace("\r\n", "\n", $field->write());
}

/**
* Test unmatched elements
*/
Expand All @@ -46,7 +51,7 @@ public function testPageField()
$element = new \PhpOffice\PhpWord\Element\Field('PAGE');
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);

$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $field->write());
$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $this->removeCr($field));
}

public function testNumpageField()
Expand All @@ -55,7 +60,7 @@ public function testNumpageField()
$element = new \PhpOffice\PhpWord\Element\Field('NUMPAGES');
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);

$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $field->write());
$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $this->removeCr($field));
}

public function testDateField()
Expand All @@ -64,7 +69,7 @@ public function testDateField()
$element = new \PhpOffice\PhpWord\Element\Field('DATE', array('dateformat' => 'd MM yyyy H:mm:ss'));
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);

$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $field->write());
$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $this->removeCr($field));
}

public function testIndexField()
Expand All @@ -73,6 +78,6 @@ public function testIndexField()
$element = new \PhpOffice\PhpWord\Element\Field('INDEX');
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);

$this->assertEquals("{}\\par\n", $field->write());
$this->assertEquals("{}\\par\n", $this->removeCr($field));
}
}