Skip to content

Commit 3a7b8ab

Browse files
authored
Merge pull request #1550 from troosan/add_support_for_table_rtl
Add RightToLeft table presentation
2 parents 4d76bae + 6aae8bd commit 3a7b8ab

File tree

11 files changed

+103
-2
lines changed

11 files changed

+103
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
v0.17.0 (?? ??? 2019)
77
----------------------
88
### Added
9+
- Add RightToLeft table presentation. @troosan #1550
910

1011
### Fixed
1112

docs/styles.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ Available Table style options:
108108
- ``border(Top|Right|Bottom|Left)Size``. Border size in *twip*.
109109
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in *twip*.
110110
- ``indent``. Table indent from leading margin. Must be an instance of ``\PhpOffice\PhpWord\ComplexType\TblWidth``.
111-
- ``width``. Table width in percent.
111+
- ``width``. Table width in Fiftieths of a Percent or Twentieths of a Point.
112112
- ``unit``. The unit to use for the width. One of ``\PhpOffice\PhpWord\SimpleType\TblWidth``. Defaults to *auto*.
113113
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.
114114
- ``cellSpacing`` Cell spacing in *twip*
115115
- ``position`` Floating Table Positioning, see below for options
116+
- ``bidiVisual`` Present table as Right-To-Left
116117

117118
Floating Table Positioning options:
118119

samples/Sample_36_RTL.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414
$textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END));
1515
$textrun->addText('سلام این یک پاراگراف راست به چپ است', array('rtl' => true));
1616

17+
$section->addText('Table visually presented as RTL');
18+
$style = array('rtl' => true, 'size' => 12);
19+
$tableStyle = array('borderSize' => 6, 'borderColor' => '000000', 'width' => 5000, 'unit' => \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT, 'bidiVisual' => true);
20+
21+
$table = $section->addTable($tableStyle);
22+
$cellHCentered = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
23+
$cellHEnd = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END);
24+
$cellVCentered = array('valign' => \PhpOffice\PhpWord\Style\Cell::VALIGN_CENTER);
25+
26+
//Vidually bidirectinal table
27+
$table->addRow();
28+
$cell = $table->addCell(500, $cellVCentered);
29+
$textrun = $cell->addTextRun($cellHCentered);
30+
$textrun->addText('ردیف', $style);
31+
32+
$cell = $table->addCell(11000);
33+
$textrun = $cell->addTextRun($cellHEnd);
34+
$textrun->addText('سوالات', $style);
35+
36+
$cell = $table->addCell(500, $cellVCentered);
37+
$textrun = $cell->addTextRun($cellHCentered);
38+
$textrun->addText('بارم', $style);
39+
1740
// Save file
1841
echo write($phpWord, basename(__FILE__, '.php'), $writers);
1942
if (!CLI) {

samples/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<a class="btn btn-lg btn-primary" href="http://phpword.readthedocs.org/" role="button"><i class="fa fa-book fa-lg" title="Docs"></i> Read the Docs</a>
2323
</p>
2424
</div>
25-
<?php
25+
<?php
2626
}
2727
if (!CLI) {
2828
echo '<h3>Requirement check:</h3>';

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
483483
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
484484
}
485485
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
486+
$styleDefs['bidiVisual'] = array(self::READ_TRUE, 'w:bidiVisual');
486487
$styleDefs['cellSpacing'] = array(self::READ_VALUE, 'w:tblCellSpacing', 'w:w');
487488
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
488489

src/PhpWord/Style/Table.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ class Table extends Border
170170
*/
171171
private $columnWidths;
172172

173+
/**
174+
* Visually Right to Left Table
175+
*
176+
* @see http://www.datypic.com/sc/ooxml/e-w_bidiVisual-1.html
177+
* @var bool
178+
*/
179+
private $bidiVisual = false;
180+
173181
/**
174182
* Create new table style
175183
*
@@ -775,4 +783,28 @@ public function setColumnWidths(array $value = null)
775783
{
776784
$this->columnWidths = $value;
777785
}
786+
787+
/**
788+
* Get bidiVisual
789+
*
790+
* @return bool
791+
*/
792+
public function isBidiVisual()
793+
{
794+
return $this->bidiVisual;
795+
}
796+
797+
/**
798+
* Set bidiVisual
799+
*
800+
* @param bool $bidi
801+
* Set to true to visually present table as Right to Left
802+
* @return self
803+
*/
804+
public function setBidiVisual($bidi)
805+
{
806+
$this->bidiVisual = $bidi;
807+
808+
return $this;
809+
}
778810
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function write()
5454
$xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm');
5555
$xmlWriter->writeAttribute('fo:text-align', $style->getAlignment());
5656
}
57+
58+
//Right to left
59+
$xmlWriter->writeAttributeIf($style->isBidi(), 'style:writing-mode', 'rl-tb');
60+
5761
$xmlWriter->endElement(); //style:paragraph-properties
5862

5963
$xmlWriter->endElement(); //style:style

src/PhpWord/Writer/ODText/Style/Table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function write()
4343
//$xmlWriter->writeAttribute('style:width', 'table');
4444
$xmlWriter->writeAttribute('style:rel-width', 100);
4545
$xmlWriter->writeAttribute('table:align', 'center');
46+
$xmlWriter->writeAttributeIf($style->isBidiVisual(), 'style:writing-mode', 'rl-tb');
4647
$xmlWriter->endElement(); // style:table-properties
4748
$xmlWriter->endElement(); // style:style
4849

src/PhpWord/Writer/Word2007/Style/Table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
8686
$styleWriter = new TablePosition($xmlWriter, $style->getPosition());
8787
$styleWriter->write();
8888

89+
//Right to left
90+
$xmlWriter->writeElementIf($style->isBidiVisual() !== null, 'w:bidiVisual', 'w:val', $this->writeOnOf($style->isBidiVisual()));
91+
8992
$this->writeMargin($xmlWriter, $style);
9093
$this->writeBorder($xmlWriter, $style);
9194

tests/PhpWord/Reader/Word2007/StyleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ public function testReadIndent()
147147
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
148148
}
149149

150+
public function testReadTableRTL()
151+
{
152+
$documentXml = '<w:tbl>
153+
<w:tblPr>
154+
<w:bidiVisual w:val="1"/>
155+
</w:tblPr>
156+
</w:tbl>';
157+
158+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
159+
160+
$elements = $phpWord->getSection(0)->getElements();
161+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
162+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
163+
/** @var \PhpOffice\PhpWord\Style\Table $tableStyle */
164+
$tableStyle = $elements[0]->getStyle();
165+
$this->assertTrue($tableStyle->isBidiVisual());
166+
}
167+
150168
public function testReadHidden()
151169
{
152170
$documentXml = '<w:p>

0 commit comments

Comments
 (0)