Skip to content

Commit

Permalink
#331 : Word2007 Writer : Support for RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 12, 2014
1 parent 6f2d444 commit 8d9e85b
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ phpword.ini
/.settings
/build
/vendor
/phpunit.bat
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
- Setting: Ability to remove [Compatibility Mode] text in the MS Word title bar - @ivanlanin
- SDT: Ability to add structured document tag elements (comboBox, dropDownList, date) - @ivanlanin
- Paragraph: Support for paragraph with borders - @ivanlanin GH-294
- Word2007 Writer : Support for RTL - @Progi1984 GH-331

### Bugfixes

Expand Down
1 change: 1 addition & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Features
run) that contains other elements
- Insert titles (headers) and table of contents
- Insert text breaks and page breaks
- Insert right-to-left text
- Insert and format images, either local, remote, or as page watermarks
- Insert binary OLE Objects such as Excel or Visio
- Insert and format table with customized properties for each rows
Expand Down
1 change: 1 addition & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Available font styles:
- ``bgColor`` Font background color, e.g. *FF0000*
- ``smallCaps`` Small caps, *true* or *false*
- ``allCaps`` All caps, *true* or *false*
- ``rtl`` Right to Left language, *true* or *false*

Paragraph
---------
Expand Down
19 changes: 19 additions & 0 deletions samples/Sample_36_RTL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
include_once 'Sample_Header.php';

// New Word document
echo date('H:i:s'), " Create new PhpWord object", EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();

$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText('This is a Left to Right paragraph.');

$textrun = $section->addTextRun(array('align' => 'right'));
$textrun->addText('سلام این یک پاراگراف راست به چپ است', array('rtl' => true));

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
2 changes: 2 additions & 0 deletions samples/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
include_once 'Sample_Header.php';

use PhpOffice\PhpWord\Settings;
$requirements = array(
'php' => array('PHP 5.3.0', version_compare(phpversion(), '5.3.0', '>=')),
'xml' => array('PHP extension XML', extension_loaded('xml')),
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
'superScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'superscript'),
'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'),
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
'rtl' => array(self::READ_TRUE, 'w:rtl'),
);

return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
Expand Down
30 changes: 30 additions & 0 deletions src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ class Font extends AbstractStyle
*/
private $shading;

/**
* Right to left languages
* @var boolean
*/
private $rtl = false;

/**
* Create new font style
*
Expand Down Expand Up @@ -268,6 +274,7 @@ public function getStyleValues()
'kerning' => $this->getKerning(),
),
'paragraph' => $this->getParagraph(),
'rtl' => $this->isRTL(),
'shading' => $this->getShading(),
);

Expand Down Expand Up @@ -730,6 +737,29 @@ public function setParagraph($value = null)
return $this;
}

/**
* Get rtl
*
* @return bool
*/
public function isRTL()
{
return $this->rtl;
}

/**
* Set rtl
*
* @param bool $value
* @return self
*/
public function setRTL($value = true)
{
$this->rtl = $this->setBoolVal($value, $this->rtl);

return $this;
}

/**
* Get shading
*
Expand Down
6 changes: 6 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ private function writeStyle()
$styleWriter = new Shading($xmlWriter, $shading);
$styleWriter->write();
}

// RTL
if ($this->isInline === true) {
$styleName = $style->getStyleName();
$xmlWriter->writeElementIf($styleName === null && $style->isRTL(), 'w:rtl');
}

$xmlWriter->endElement();
}
Expand Down
54 changes: 54 additions & 0 deletions tests/PhpWord/Tests/Writer/Word2007/Style/FontTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http:https://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Writer\Word2007\Part;

use PhpOffice\PhpWord\Tests\TestHelperDOCX;
use PhpOffice\PhpWord\Writer\Word2007;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font;

/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Style\Font
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Style\Font
* @runTestsInSeparateProcesses
*/
class FontTest extends \PHPUnit_Framework_TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}

/**
* Test write styles
*/
public function testFontRTL()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText('سلام این یک پاراگراف راست به چپ است', array('rtl' => true));
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$file = 'word/document.xml';
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:rtl';
$this->assertTrue($doc->elementExists($path, $file));
}
}

0 comments on commit 8d9e85b

Please sign in to comment.