Add RTL aligning of tables
This commit is contained in:
parent
4d76baea42
commit
f91863ed64
|
|
@ -14,6 +14,29 @@ $textrun->addText('This is a Left to Right paragraph.');
|
||||||
$textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END));
|
$textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END));
|
||||||
$textrun->addText('سلام این یک پاراگراف راست به چپ است', array('rtl' => true));
|
$textrun->addText('سلام این یک پاراگراف راست به چپ است', array('rtl' => true));
|
||||||
|
|
||||||
|
$section->addText('Table visually presented as RTL');
|
||||||
|
$style = array('rtl' => true, 'size' => 12);
|
||||||
|
$tableStyle = array('borderSize' => 6, 'borderColor' => '000000', 'width' => 5000, 'unit' => \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT, 'bidiVisual' => true);
|
||||||
|
|
||||||
|
$table = $section->addTable($tableStyle);
|
||||||
|
$cellHCentered = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
|
||||||
|
$cellHEnd = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END);
|
||||||
|
$cellVCentered = array('valign' => \PhpOffice\PhpWord\Style\Cell::VALIGN_CENTER);
|
||||||
|
|
||||||
|
//Vidually bidirectinal table
|
||||||
|
$table->addRow();
|
||||||
|
$cell = $table->addCell(500, $cellVCentered);
|
||||||
|
$textrun = $cell->addTextRun($cellHCentered);
|
||||||
|
$textrun->addText('ردیف', $style);
|
||||||
|
|
||||||
|
$cell = $table->addCell(11000);
|
||||||
|
$textrun = $cell->addTextRun($cellHEnd);
|
||||||
|
$textrun->addText('سوالات', $style);
|
||||||
|
|
||||||
|
$cell = $table->addCell(500, $cellVCentered);
|
||||||
|
$textrun = $cell->addTextRun($cellHCentered);
|
||||||
|
$textrun->addText('بارم', $style);
|
||||||
|
|
||||||
// Save file
|
// Save file
|
||||||
echo write($phpWord, basename(__FILE__, '.php'), $writers);
|
echo write($phpWord, basename(__FILE__, '.php'), $writers);
|
||||||
if (!CLI) {
|
if (!CLI) {
|
||||||
|
|
|
||||||
|
|
@ -483,6 +483,7 @@ abstract class AbstractPart
|
||||||
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
|
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
|
||||||
}
|
}
|
||||||
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
|
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
|
||||||
|
$styleDefs['bidiVisual'] = array(self::READ_TRUE, 'w:bidiVisual');
|
||||||
$styleDefs['cellSpacing'] = array(self::READ_VALUE, 'w:tblCellSpacing', 'w:w');
|
$styleDefs['cellSpacing'] = array(self::READ_VALUE, 'w:tblCellSpacing', 'w:w');
|
||||||
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
|
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,14 @@ class Table extends Border
|
||||||
*/
|
*/
|
||||||
private $columnWidths;
|
private $columnWidths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visually Right to Left Table
|
||||||
|
*
|
||||||
|
* @see http://www.datypic.com/sc/ooxml/e-w_bidiVisual-1.html
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $bidiVisual = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new table style
|
* Create new table style
|
||||||
*
|
*
|
||||||
|
|
@ -775,4 +783,28 @@ class Table extends Border
|
||||||
{
|
{
|
||||||
$this->columnWidths = $value;
|
$this->columnWidths = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get bidiVisual
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isBidiVisual()
|
||||||
|
{
|
||||||
|
return $this->bidiVisual;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set bidiVisual
|
||||||
|
*
|
||||||
|
* @param bool $bidi
|
||||||
|
* Set to true to visually present table as Right to Left
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setBidiVisual($bidi)
|
||||||
|
{
|
||||||
|
$this->bidiVisual = $bidi;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,10 @@ class Paragraph extends AbstractStyle
|
||||||
$xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm');
|
$xmlWriter->writeAttribute('fo:margin-bottom', $marginBottom . 'cm');
|
||||||
$xmlWriter->writeAttribute('fo:text-align', $style->getAlignment());
|
$xmlWriter->writeAttribute('fo:text-align', $style->getAlignment());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Right to left
|
||||||
|
$xmlWriter->writeAttributeIf($style->isBidi(), 'style:writing-mode', 'rl-tb');
|
||||||
|
|
||||||
$xmlWriter->endElement(); //style:paragraph-properties
|
$xmlWriter->endElement(); //style:paragraph-properties
|
||||||
|
|
||||||
$xmlWriter->endElement(); //style:style
|
$xmlWriter->endElement(); //style:style
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ class Table extends AbstractStyle
|
||||||
//$xmlWriter->writeAttribute('style:width', 'table');
|
//$xmlWriter->writeAttribute('style:width', 'table');
|
||||||
$xmlWriter->writeAttribute('style:rel-width', 100);
|
$xmlWriter->writeAttribute('style:rel-width', 100);
|
||||||
$xmlWriter->writeAttribute('table:align', 'center');
|
$xmlWriter->writeAttribute('table:align', 'center');
|
||||||
|
$xmlWriter->writeAttributeIf($style->isBidiVisual(), 'style:writing-mode', 'rl-tb');
|
||||||
$xmlWriter->endElement(); // style:table-properties
|
$xmlWriter->endElement(); // style:table-properties
|
||||||
$xmlWriter->endElement(); // style:style
|
$xmlWriter->endElement(); // style:style
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,9 @@ class Table extends AbstractStyle
|
||||||
$styleWriter = new TablePosition($xmlWriter, $style->getPosition());
|
$styleWriter = new TablePosition($xmlWriter, $style->getPosition());
|
||||||
$styleWriter->write();
|
$styleWriter->write();
|
||||||
|
|
||||||
|
//Right to left
|
||||||
|
$xmlWriter->writeElementIf($style->isBidiVisual() !== null, 'w:bidiVisual', 'w:val', $this->writeOnOf($style->isBidiVisual()));
|
||||||
|
|
||||||
$this->writeMargin($xmlWriter, $style);
|
$this->writeMargin($xmlWriter, $style);
|
||||||
$this->writeBorder($xmlWriter, $style);
|
$this->writeBorder($xmlWriter, $style);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,24 @@ class StyleTest extends AbstractTestReader
|
||||||
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
|
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testReadTableRTL()
|
||||||
|
{
|
||||||
|
$documentXml = '<w:tbl>
|
||||||
|
<w:tblPr>
|
||||||
|
<w:bidiVisual w:val="1"/>
|
||||||
|
</w:tblPr>
|
||||||
|
</w:tbl>';
|
||||||
|
|
||||||
|
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
|
||||||
|
|
||||||
|
$elements = $phpWord->getSection(0)->getElements();
|
||||||
|
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
|
||||||
|
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
|
||||||
|
/** @var \PhpOffice\PhpWord\Style\Table $tableStyle */
|
||||||
|
$tableStyle = $elements[0]->getStyle();
|
||||||
|
$this->assertTrue($tableStyle->isBidiVisual());
|
||||||
|
}
|
||||||
|
|
||||||
public function testReadHidden()
|
public function testReadHidden()
|
||||||
{
|
{
|
||||||
$documentXml = '<w:p>
|
$documentXml = '<w:p>
|
||||||
|
|
|
||||||
|
|
@ -141,4 +141,21 @@ class TableTest extends \PHPUnit\Framework\TestCase
|
||||||
$this->assertSame($value, (int) $doc->getElementAttribute($path, 'w:w'));
|
$this->assertSame($value, (int) $doc->getElementAttribute($path, 'w:w'));
|
||||||
$this->assertSame($type, $doc->getElementAttribute($path, 'w:type'));
|
$this->assertSame($type, $doc->getElementAttribute($path, 'w:type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRigthToLeft()
|
||||||
|
{
|
||||||
|
$tableStyle = new Table();
|
||||||
|
$tableStyle->setBidiVisual(true);
|
||||||
|
|
||||||
|
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||||
|
$section = $phpWord->addSection();
|
||||||
|
$table = $section->addTable($tableStyle);
|
||||||
|
$table->addRow();
|
||||||
|
|
||||||
|
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
|
||||||
|
|
||||||
|
$path = '/w:document/w:body/w:tbl/w:tblPr/w:bidiVisual';
|
||||||
|
$this->assertTrue($doc->elementExists($path));
|
||||||
|
$this->assertEquals('1', $doc->getElementAttribute($path, 'w:val'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue