diff --git a/.gitignore b/.gitignore index 810a7b0a..a74da9de 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ phpword.ini /.settings /build /vendor +/phpunit.bat diff --git a/CHANGELOG.md b/CHANGELOG.md index e7564f68..6040c46e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/intro.rst b/docs/intro.rst index b604298f..03821c91 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -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 diff --git a/docs/styles.rst b/docs/styles.rst index cd07a9f7..91d5ed7a 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -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 --------- diff --git a/samples/Sample_36_RTL.php b/samples/Sample_36_RTL.php new file mode 100644 index 00000000..9b85fb1e --- /dev/null +++ b/samples/Sample_36_RTL.php @@ -0,0 +1,19 @@ +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'; +} diff --git a/samples/index.php b/samples/index.php index 4281f6fb..f25f7f33 100644 --- a/samples/index.php +++ b/samples/index.php @@ -1,5 +1,7 @@ array('PHP 5.3.0', version_compare(phpversion(), '5.3.0', '>=')), 'xml' => array('PHP extension XML', extension_loaded('xml')), diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 8a409379..7d3e0f66 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -373,6 +373,7 @@ abstract class AbstractPart '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); diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 000e2bb6..8980258b 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -222,6 +222,12 @@ class Font extends AbstractStyle */ private $shading; + /** + * Right to left languages + * @var boolean + */ + private $rtl = false; + /** * Create new font style * @@ -268,6 +274,7 @@ class Font extends AbstractStyle 'kerning' => $this->getKerning(), ), 'paragraph' => $this->getParagraph(), + 'rtl' => $this->isRTL(), 'shading' => $this->getShading(), ); @@ -730,6 +737,29 @@ class Font extends AbstractStyle 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 * diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 67a04829..9371f970 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -129,6 +129,12 @@ class Font extends AbstractStyle $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(); } diff --git a/tests/PhpWord/Tests/Writer/Word2007/Style/FontTest.php b/tests/PhpWord/Tests/Writer/Word2007/Style/FontTest.php new file mode 100644 index 00000000..af11c054 --- /dev/null +++ b/tests/PhpWord/Tests/Writer/Word2007/Style/FontTest.php @@ -0,0 +1,54 @@ +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)); + } +}