Skip to content

Support for RTL in Alignment & Font Format (Latin/East Asian/Complex Script) #657

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

Merged
merged 1 commit into from
Jul 23, 2021
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
5 changes: 5 additions & 0 deletions docs/changes/1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
- ODPresentation Writer
- PowerPoint2007 Reader
- PowerPoint2007 Writer
- Support for RTL in Alignment & Font Format (Latin/East Asian/Complex Script) - @amirakbari GH-629 & @Progi1986 GH-657
- ODPresentation Reader
- ODPresentation Writer
- PowerPoint2007 Reader
- PowerPoint2007 Writer

## Project Management
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635
Expand Down
1 change: 1 addition & 0 deletions docs/credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Library of Congress :
### OpenDocument

- [Oasis OpenDocument Standard Version 1.2](http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os.html)
- [Schema Central Open Document 1.1](http://www.datypic.com/sc/odf/ss.html)

### PowerPoint 97

Expand Down
43 changes: 42 additions & 1 deletion docs/usage/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ Properties:

Use this style to define border of a shape as example below.

.. code-block:: php
``` php
<?php

$shape->getBorder()
->setLineStyle(Border::LINE_SINGLE)
->setLineWidth(4)
->getColor()->setARGB('FFC00000');
```

Properties:

Expand Down Expand Up @@ -71,6 +73,25 @@ Properties:
- `marginLeft`
- `marginRight`

### RTL / LTR

You can define if the alignment is RTL or LTR.

``` php
<?php

use PhpOffice\PhpPresentation\Style\Alignment;

$alignment = new Alignment();

// Set alignment to RTL
$alignment->setIsRTL(true);
// Set alignment to LTR
$alignment->setIsRTL(false);
// Is the alignment RTL?
echo $alignment->isRTL();
```

## Font

- `name`
Expand All @@ -82,6 +103,26 @@ Properties:
- `strikethrough`
- `color`

### Format

Some formats are available :

* `Font::FORMAT_LATIN`
* `Font::FORMAT_EAST_ASIAN`
* `Font::FORMAT_COMPLEX_SCRIPT`

``` php
<?php

use PhpOffice\PhpPresentation\Style\Font;

$font = new Font();

// Set format of font
$font->setFormat(Font::FORMAT_EAST_ASIAN);
// Get format of font
echo $font->getFormat();
```
## Bullet

- `bulletType`
Expand Down
24 changes: 22 additions & 2 deletions samples/Sample_01_Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Font;

// Create new PHPPresentation object
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
Expand Down Expand Up @@ -44,13 +45,32 @@
->setHeight(300)
->setWidth(600)
->setOffsetX(170)
->setOffsetY(180);
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
->setOffsetY(100);
$shape->getActiveParagraph()->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);
$textRun = $shape->createTextRun('Thank you for using PHPPresentation!');
$textRun->getFont()->setBold(true)
->setSize(60)
->setColor(new Color('FFE06B20'));

// Create a shape (text)
echo date('H:i:s') . ' Create a shape (rich text)' . EOL;
$shape = $currentSlide->createRichTextShape()
->setHeight(300)
->setWidth(600)
->setOffsetX(170)
->setOffsetY(550);
$shape->getActiveParagraph()->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_RIGHT)
->setIsRTL(true);
$textRun = $shape->createTextRun('تست فونت فارسی');
$textRun->getFont()
->setBold(true)
->setSize(60)
->setColor(new Color('FFE06B20'))
->setFormat(Font::FORMAT_COMPLEX_SCRIPT)
->setName('B Nazanin');

// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
if (!CLI) {
Expand Down
76 changes: 73 additions & 3 deletions src/PhpPresentation/Reader/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,66 @@ protected function loadStyle(DOMElement $nodeStyle)
if ($nodeTextProperties->hasAttribute('fo:color')) {
$oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6));
}
// Font Latin
if ($nodeTextProperties->hasAttribute('fo:font-family')) {
$oFont->setName($nodeTextProperties->getAttribute('fo:font-family'));
$oFont
->setName($nodeTextProperties->getAttribute('fo:font-family'))
->setFormat(Font::FORMAT_LATIN);
}
if ($nodeTextProperties->hasAttribute('fo:font-weight') && 'bold' == $nodeTextProperties->getAttribute('fo:font-weight')) {
$oFont->setBold(true);
$oFont
->setBold(true)
->setFormat(Font::FORMAT_LATIN);
}
if ($nodeTextProperties->hasAttribute('fo:font-size')) {
$oFont->setSize((int) substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2));
$oFont
->setSize((int) substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2))
->setFormat(Font::FORMAT_LATIN);
}
// Font East Asian
if ($nodeTextProperties->hasAttribute('style:font-family-asian')) {
$oFont
->setName($nodeTextProperties->getAttribute('style:font-family-asian'))
->setFormat(Font::FORMAT_EAST_ASIAN);
}
if ($nodeTextProperties->hasAttribute('style:font-weight-asian') && 'bold' == $nodeTextProperties->getAttribute('style:font-weight-asian')) {
$oFont
->setBold(true)
->setFormat(Font::FORMAT_EAST_ASIAN);
}
if ($nodeTextProperties->hasAttribute('style:font-size-asian')) {
$oFont
->setSize((int) substr($nodeTextProperties->getAttribute('style:font-size-asian'), 0, -2))
->setFormat(Font::FORMAT_EAST_ASIAN);
}
// Font Complex Script
if ($nodeTextProperties->hasAttribute('style:font-family-complex')) {
$oFont
->setName($nodeTextProperties->getAttribute('style:font-family-complex'))
->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
}
if ($nodeTextProperties->hasAttribute('style:font-weight-complex') && 'bold' == $nodeTextProperties->getAttribute('style:font-weight-complex')) {
$oFont
->setBold(true)
->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
}
if ($nodeTextProperties->hasAttribute('style:font-size-complex')) {
$oFont
->setSize((int) substr($nodeTextProperties->getAttribute('style:font-size-complex'), 0, -2))
->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
}
if ($nodeTextProperties->hasAttribute('style:script-type')) {
switch ($nodeTextProperties->getAttribute('style:script-type')) {
case 'latin':
$oFont->setFormat(Font::FORMAT_LATIN);
break;
case 'asian':
$oFont->setFormat(Font::FORMAT_EAST_ASIAN);
break;
case 'complex':
$oFont->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
break;
}
}
}

Expand All @@ -317,6 +369,24 @@ protected function loadStyle(DOMElement $nodeStyle)
if ($nodeParagraphProps->hasAttribute('fo:text-align')) {
$oAlignment->setHorizontal($nodeParagraphProps->getAttribute('fo:text-align'));
}
if ($nodeParagraphProps->hasAttribute('style:writing-mode')) {
switch ($nodeParagraphProps->getAttribute('style:writing-mode')) {
case 'lr-tb':
case 'tb-lr':
case 'lr':
$oAlignment->setIsRTL(false);
break;
case 'rl-tb':
case 'tb-rl':
case 'rl':
$oAlignment->setIsRTL(false);
break;
case 'tb':
case 'page':
default:
break;
}
}
}

if ('text:list-style' == $nodeStyle->nodeName) {
Expand Down
25 changes: 25 additions & 0 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Fill;
use PhpOffice\PhpPresentation\Style\Font;
use PhpOffice\PhpPresentation\Style\SchemeColor;
use PhpOffice\PhpPresentation\Style\TextStyle;
use ZipArchive;
Expand Down Expand Up @@ -1093,6 +1094,9 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
if ($oSubElement->hasAttribute('lvl')) {
$oParagraph->getAlignment()->setLevel((int) $oSubElement->getAttribute('lvl'));
}
if ($oSubElement->hasAttribute('rtl')) {
$oParagraph->getAlignment()->setIsRTL((bool) $oSubElement->getAttribute('rtl'));
}

$oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_NONE);

Expand Down Expand Up @@ -1179,6 +1183,27 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
$oText->getHyperlink()->setUrl($this->arrayRels[$this->fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target']);
}
}
// Font
$oElementFontFormat = null;
$oElementFontFormatLatin = $document->getElement('a:latin', $oElementrPr);
if (is_object($oElementFontFormatLatin)) {
$oText->getFont()->setFormat(Font::FORMAT_LATIN);
$oElementFontFormat = $oElementFontFormatLatin;
}
$oElementFontFormatEastAsian = $document->getElement('a:ea', $oElementrPr);
if (is_object($oElementFontFormatEastAsian)) {
$oText->getFont()->setFormat(Font::FORMAT_EAST_ASIAN);
$oElementFontFormat = $oElementFontFormatEastAsian;
}
$oElementFontFormatComplexScript = $document->getElement('a:cs', $oElementrPr);
if (is_object($oElementFontFormatComplexScript)) {
$oText->getFont()->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
$oElementFontFormat = $oElementFontFormatComplexScript;
}
if (is_object($oElementFontFormat) && $oElementFontFormat->hasAttribute('typeface')) {
$oText->getFont()->setName($oElementFontFormat->getAttribute('typeface'));
}

//} else {
// $oText = $oParagraph->createText();

Expand Down
28 changes: 28 additions & 0 deletions src/PhpPresentation/Style/Alignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ class Alignment implements ComparableInterface
*/
private $marginBottom = 0;

/**
* RTL Direction Support
*
* @var bool
*/
private $isRTL = false;

/**
* Hash index.
*
Expand Down Expand Up @@ -319,6 +326,26 @@ public function setTextDirection(string $pValue = self::TEXT_DIRECTION_HORIZONTA
return $this;
}

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

/**
* @param bool $value
*
* @return self
*/
public function setIsRTL(bool $value = false): self
{
$this->isRTL = $value;

return $this;
}

/**
* Get hash code.
*
Expand All @@ -333,6 +360,7 @@ public function getHashCode(): string
. $this->indent
. $this->marginLeft
. $this->marginRight
. ($this->isRTL ? '1' : '0')
. __CLASS__
);
}
Expand Down
Loading