Skip to content

Commit e15d411

Browse files
authored
Merge pull request #657 from Progi1984/rtlSupport
Support for RTL in Alignment & Font Format (Latin/East Asian/Complex Script)
2 parents c0d5bbd + be48bb3 commit e15d411

File tree

16 files changed

+810
-105
lines changed

16 files changed

+810
-105
lines changed

docs/changes/1.0.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
- ODPresentation Writer
3131
- PowerPoint2007 Reader
3232
- PowerPoint2007 Writer
33+
- Support for RTL in Alignment & Font Format (Latin/East Asian/Complex Script) - @amirakbari GH-629 & @Progi1986 GH-657
34+
- ODPresentation Reader
35+
- ODPresentation Writer
36+
- PowerPoint2007 Reader
37+
- PowerPoint2007 Writer
3338

3439
## Project Management
3540
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635

docs/credits.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Library of Congress :
3232
### OpenDocument
3333

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

3637
### PowerPoint 97
3738

docs/usage/styles.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ Properties:
2525

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

28-
.. code-block:: php
28+
``` php
29+
<?php
2930

3031
$shape->getBorder()
3132
->setLineStyle(Border::LINE_SINGLE)
3233
->setLineWidth(4)
3334
->getColor()->setARGB('FFC00000');
35+
```
3436

3537
Properties:
3638

@@ -71,6 +73,25 @@ Properties:
7173
- `marginLeft`
7274
- `marginRight`
7375

76+
### RTL / LTR
77+
78+
You can define if the alignment is RTL or LTR.
79+
80+
``` php
81+
<?php
82+
83+
use PhpOffice\PhpPresentation\Style\Alignment;
84+
85+
$alignment = new Alignment();
86+
87+
// Set alignment to RTL
88+
$alignment->setIsRTL(true);
89+
// Set alignment to LTR
90+
$alignment->setIsRTL(false);
91+
// Is the alignment RTL?
92+
echo $alignment->isRTL();
93+
```
94+
7495
## Font
7596

7697
- `name`
@@ -82,6 +103,26 @@ Properties:
82103
- `strikethrough`
83104
- `color`
84105

106+
### Format
107+
108+
Some formats are available :
109+
110+
* `Font::FORMAT_LATIN`
111+
* `Font::FORMAT_EAST_ASIAN`
112+
* `Font::FORMAT_COMPLEX_SCRIPT`
113+
114+
``` php
115+
<?php
116+
117+
use PhpOffice\PhpPresentation\Style\Font;
118+
119+
$font = new Font();
120+
121+
// Set format of font
122+
$font->setFormat(Font::FORMAT_EAST_ASIAN);
123+
// Get format of font
124+
echo $font->getFormat();
125+
```
85126
## Bullet
86127

87128
- `bulletType`

samples/Sample_01_Simple.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpOffice\PhpPresentation\PhpPresentation;
66
use PhpOffice\PhpPresentation\Style\Alignment;
77
use PhpOffice\PhpPresentation\Style\Color;
8+
use PhpOffice\PhpPresentation\Style\Font;
89

910
// Create new PHPPresentation object
1011
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
@@ -44,13 +45,32 @@
4445
->setHeight(300)
4546
->setWidth(600)
4647
->setOffsetX(170)
47-
->setOffsetY(180);
48-
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
48+
->setOffsetY(100);
49+
$shape->getActiveParagraph()->getAlignment()
50+
->setHorizontal(Alignment::HORIZONTAL_CENTER);
4951
$textRun = $shape->createTextRun('Thank you for using PHPPresentation!');
5052
$textRun->getFont()->setBold(true)
5153
->setSize(60)
5254
->setColor(new Color('FFE06B20'));
5355

56+
// Create a shape (text)
57+
echo date('H:i:s') . ' Create a shape (rich text)' . EOL;
58+
$shape = $currentSlide->createRichTextShape()
59+
->setHeight(300)
60+
->setWidth(600)
61+
->setOffsetX(170)
62+
->setOffsetY(550);
63+
$shape->getActiveParagraph()->getAlignment()
64+
->setHorizontal(Alignment::HORIZONTAL_RIGHT)
65+
->setIsRTL(true);
66+
$textRun = $shape->createTextRun('تست فونت فارسی');
67+
$textRun->getFont()
68+
->setBold(true)
69+
->setSize(60)
70+
->setColor(new Color('FFE06B20'))
71+
->setFormat(Font::FORMAT_COMPLEX_SCRIPT)
72+
->setName('B Nazanin');
73+
5474
// Save file
5575
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
5676
if (!CLI) {

src/PhpPresentation/Reader/ODPresentation.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,66 @@ protected function loadStyle(DOMElement $nodeStyle)
300300
if ($nodeTextProperties->hasAttribute('fo:color')) {
301301
$oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6));
302302
}
303+
// Font Latin
303304
if ($nodeTextProperties->hasAttribute('fo:font-family')) {
304-
$oFont->setName($nodeTextProperties->getAttribute('fo:font-family'));
305+
$oFont
306+
->setName($nodeTextProperties->getAttribute('fo:font-family'))
307+
->setFormat(Font::FORMAT_LATIN);
305308
}
306309
if ($nodeTextProperties->hasAttribute('fo:font-weight') && 'bold' == $nodeTextProperties->getAttribute('fo:font-weight')) {
307-
$oFont->setBold(true);
310+
$oFont
311+
->setBold(true)
312+
->setFormat(Font::FORMAT_LATIN);
308313
}
309314
if ($nodeTextProperties->hasAttribute('fo:font-size')) {
310-
$oFont->setSize((int) substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2));
315+
$oFont
316+
->setSize((int) substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2))
317+
->setFormat(Font::FORMAT_LATIN);
318+
}
319+
// Font East Asian
320+
if ($nodeTextProperties->hasAttribute('style:font-family-asian')) {
321+
$oFont
322+
->setName($nodeTextProperties->getAttribute('style:font-family-asian'))
323+
->setFormat(Font::FORMAT_EAST_ASIAN);
324+
}
325+
if ($nodeTextProperties->hasAttribute('style:font-weight-asian') && 'bold' == $nodeTextProperties->getAttribute('style:font-weight-asian')) {
326+
$oFont
327+
->setBold(true)
328+
->setFormat(Font::FORMAT_EAST_ASIAN);
329+
}
330+
if ($nodeTextProperties->hasAttribute('style:font-size-asian')) {
331+
$oFont
332+
->setSize((int) substr($nodeTextProperties->getAttribute('style:font-size-asian'), 0, -2))
333+
->setFormat(Font::FORMAT_EAST_ASIAN);
334+
}
335+
// Font Complex Script
336+
if ($nodeTextProperties->hasAttribute('style:font-family-complex')) {
337+
$oFont
338+
->setName($nodeTextProperties->getAttribute('style:font-family-complex'))
339+
->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
340+
}
341+
if ($nodeTextProperties->hasAttribute('style:font-weight-complex') && 'bold' == $nodeTextProperties->getAttribute('style:font-weight-complex')) {
342+
$oFont
343+
->setBold(true)
344+
->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
345+
}
346+
if ($nodeTextProperties->hasAttribute('style:font-size-complex')) {
347+
$oFont
348+
->setSize((int) substr($nodeTextProperties->getAttribute('style:font-size-complex'), 0, -2))
349+
->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
350+
}
351+
if ($nodeTextProperties->hasAttribute('style:script-type')) {
352+
switch ($nodeTextProperties->getAttribute('style:script-type')) {
353+
case 'latin':
354+
$oFont->setFormat(Font::FORMAT_LATIN);
355+
break;
356+
case 'asian':
357+
$oFont->setFormat(Font::FORMAT_EAST_ASIAN);
358+
break;
359+
case 'complex':
360+
$oFont->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
361+
break;
362+
}
311363
}
312364
}
313365

@@ -317,6 +369,24 @@ protected function loadStyle(DOMElement $nodeStyle)
317369
if ($nodeParagraphProps->hasAttribute('fo:text-align')) {
318370
$oAlignment->setHorizontal($nodeParagraphProps->getAttribute('fo:text-align'));
319371
}
372+
if ($nodeParagraphProps->hasAttribute('style:writing-mode')) {
373+
switch ($nodeParagraphProps->getAttribute('style:writing-mode')) {
374+
case 'lr-tb':
375+
case 'tb-lr':
376+
case 'lr':
377+
$oAlignment->setIsRTL(false);
378+
break;
379+
case 'rl-tb':
380+
case 'tb-rl':
381+
case 'rl':
382+
$oAlignment->setIsRTL(false);
383+
break;
384+
case 'tb':
385+
case 'page':
386+
default:
387+
break;
388+
}
389+
}
320390
}
321391

322392
if ('text:list-style' == $nodeStyle->nodeName) {

src/PhpPresentation/Reader/PowerPoint2007.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use PhpOffice\PhpPresentation\Style\Bullet;
4343
use PhpOffice\PhpPresentation\Style\Color;
4444
use PhpOffice\PhpPresentation\Style\Fill;
45+
use PhpOffice\PhpPresentation\Style\Font;
4546
use PhpOffice\PhpPresentation\Style\SchemeColor;
4647
use PhpOffice\PhpPresentation\Style\TextStyle;
4748
use ZipArchive;
@@ -1093,6 +1094,9 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
10931094
if ($oSubElement->hasAttribute('lvl')) {
10941095
$oParagraph->getAlignment()->setLevel((int) $oSubElement->getAttribute('lvl'));
10951096
}
1097+
if ($oSubElement->hasAttribute('rtl')) {
1098+
$oParagraph->getAlignment()->setIsRTL((bool) $oSubElement->getAttribute('rtl'));
1099+
}
10961100

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

@@ -1179,6 +1183,27 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
11791183
$oText->getHyperlink()->setUrl($this->arrayRels[$this->fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target']);
11801184
}
11811185
}
1186+
// Font
1187+
$oElementFontFormat = null;
1188+
$oElementFontFormatLatin = $document->getElement('a:latin', $oElementrPr);
1189+
if (is_object($oElementFontFormatLatin)) {
1190+
$oText->getFont()->setFormat(Font::FORMAT_LATIN);
1191+
$oElementFontFormat = $oElementFontFormatLatin;
1192+
}
1193+
$oElementFontFormatEastAsian = $document->getElement('a:ea', $oElementrPr);
1194+
if (is_object($oElementFontFormatEastAsian)) {
1195+
$oText->getFont()->setFormat(Font::FORMAT_EAST_ASIAN);
1196+
$oElementFontFormat = $oElementFontFormatEastAsian;
1197+
}
1198+
$oElementFontFormatComplexScript = $document->getElement('a:cs', $oElementrPr);
1199+
if (is_object($oElementFontFormatComplexScript)) {
1200+
$oText->getFont()->setFormat(Font::FORMAT_COMPLEX_SCRIPT);
1201+
$oElementFontFormat = $oElementFontFormatComplexScript;
1202+
}
1203+
if (is_object($oElementFontFormat) && $oElementFontFormat->hasAttribute('typeface')) {
1204+
$oText->getFont()->setName($oElementFontFormat->getAttribute('typeface'));
1205+
}
1206+
11821207
//} else {
11831208
// $oText = $oParagraph->createText();
11841209

src/PhpPresentation/Style/Alignment.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ class Alignment implements ComparableInterface
118118
*/
119119
private $marginBottom = 0;
120120

121+
/**
122+
* RTL Direction Support
123+
*
124+
* @var bool
125+
*/
126+
private $isRTL = false;
127+
121128
/**
122129
* Hash index.
123130
*
@@ -319,6 +326,26 @@ public function setTextDirection(string $pValue = self::TEXT_DIRECTION_HORIZONTA
319326
return $this;
320327
}
321328

329+
/**
330+
* @return bool
331+
*/
332+
public function isRTL(): bool
333+
{
334+
return $this->isRTL;
335+
}
336+
337+
/**
338+
* @param bool $value
339+
*
340+
* @return self
341+
*/
342+
public function setIsRTL(bool $value = false): self
343+
{
344+
$this->isRTL = $value;
345+
346+
return $this;
347+
}
348+
322349
/**
323350
* Get hash code.
324351
*
@@ -333,6 +360,7 @@ public function getHashCode(): string
333360
. $this->indent
334361
. $this->marginLeft
335362
. $this->marginRight
363+
. ($this->isRTL ? '1' : '0')
336364
. __CLASS__
337365
);
338366
}

0 commit comments

Comments
 (0)