Skip to content

RTL Support and Font Format Support #629

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions samples/Sample_01_Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Font;
use PhpOffice\PhpPresentation\Style\Color;

// Create new PHPPresentation object
Expand Down Expand Up @@ -45,11 +46,13 @@
->setWidth(600)
->setOffsetX(170)
->setOffsetY(180);
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$textRun = $shape->createTextRun('Thank you for using PHPPresentation!');
$shape->getActiveParagraph()->getAlignment()->setRTL(true) //add RTL Direction to Paragraph
->setHorizontal(Alignment::HORIZONTAL_RIGHT);
$textRun = $shape->createTextRun('تست فونت فارسی و دایرکشن راست چین');
$textRun->getFont()->setBold(true)
->setFormat(Font::FONT_FORMAT_COMPLEX_SCRIPT) // add cs for Complex Script Fonts
->setSize(60)
->setColor(new Color('FFE06B20'));
->setColor(new Color('FFE06B20'))->setName('B Nazanin');

// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
Expand Down
34 changes: 34 additions & 0 deletions src/PhpPresentation/Style/Alignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Alignment implements ComparableInterface
self::HORIZONTAL_RIGHT,
);

/**
* RTL Direction Support
* @var boolean
*/
private $RTL;

/**
* Horizontal
* @var string
Expand Down Expand Up @@ -119,8 +125,35 @@ public function __construct()
// Initialise values
$this->horizontal = self::HORIZONTAL_LEFT;
$this->vertical = self::VERTICAL_BASE;
$this->RTL = false;
}

/**
* Get RTL
*
* @return boolean
*/
public function isRTL()
{
return $this->RTL;
}

/**
* Set RTL
*
* @param boolean $pValue
* @return \PhpOffice\PhpPresentation\Style\Alignment
*/
public function setRTL($pValue = false)
{
if ($pValue == '') {
$pValue = false;
}
$this->RTL = $pValue;

return $this;
}

/**
* Get Horizontal
*
Expand Down Expand Up @@ -357,6 +390,7 @@ public function getHashCode()
{
return md5(
$this->horizontal
. ($this->RTL ? 't' : 'f')
. $this->vertical
. $this->level
. $this->indent
Expand Down
41 changes: 40 additions & 1 deletion src/PhpPresentation/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ class Font implements ComparableInterface
const UNDERLINE_WAVYHEAVY = 'wavyHeavy';
const UNDERLINE_WORDS = 'words';

const FONT_FORMAT_LATIN = 'latin';
const FONT_FORMAT_EAST_ASIAN = 'ea';
const FONT_FORMAT_COMPLEX_SCRIPT = 'cs';

/**
* Name
*
* @var string
*/
private $name;

/**
* Format
*
* @var string
*/
private $format;

/**
* Font Size
Expand Down Expand Up @@ -128,6 +139,7 @@ public function __construct()
{
// Initialise values
$this->name = 'Calibri';
$this->format = self::FONT_FORMAT_LATIN;
$this->size = 10;
$this->characterSpacing = 0;
$this->bold = false;
Expand Down Expand Up @@ -164,6 +176,33 @@ public function setName($pValue = 'Calibri')

return $this;
}


/**
* Get Font Format
*
* @return string
*/
public function getFormat()
{
return $this->format;
}

/**
* Set Font Format
*
* @param string $pValue
* @return \PhpOffice\PhpPresentation\Style\Font
*/
public function setFormat($pValue = self::FONT_FORMAT_LATIN)
{
if ($pValue == '') {
$pValue = self::FONT_FORMAT_LATIN;
}
$this->format = $pValue;

return $this;
}

/**
* Get Character Spacing
Expand Down Expand Up @@ -419,7 +458,7 @@ public function setColor($pValue = null)
*/
public function getHashCode()
{
return md5($this->name . $this->size . ($this->bold ? 't' : 'f') . ($this->italic ? 't' : 'f') . ($this->superScript ? 't' : 'f') . ($this->subScript ? 't' : 'f') . $this->underline . ($this->strikethrough ? 't' : 'f') . $this->color->getHashCode() . __CLASS__);
return md5($this->name . $this->format . $this->size . ($this->bold ? 't' : 'f') . ($this->italic ? 't' : 'f') . ($this->superScript ? 't' : 'f') . ($this->subScript ? 't' : 'f') . $this->underline . ($this->strikethrough ? 't' : 'f') . $this->color->getHashCode() . __CLASS__);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Font;
use PhpOffice\PhpPresentation\Style\Shadow;
use PhpOffice\PhpPresentation\Slide\AbstractSlide as AbstractSlideAlias;

Expand Down Expand Up @@ -516,6 +517,7 @@ protected function writeParagraphs(XMLWriter $objWriter, $paragraphs, $bIsPlaceh
if (!$bIsPlaceholder) {
$objWriter->startElement('a:pPr');
$objWriter->writeAttribute('algn', $paragraph->getAlignment()->getHorizontal());
$objWriter->writeAttributeIf($paragraph->getAlignment()->isRTL(), 'rtl', '1');
$objWriter->writeAttribute('fontAlgn', $paragraph->getAlignment()->getVertical());
$objWriter->writeAttribute('marL', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getMarginLeft()));
$objWriter->writeAttribute('marR', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getMarginRight()));
Expand Down Expand Up @@ -594,7 +596,17 @@ protected function writeParagraphs(XMLWriter $objWriter, $paragraphs, $bIsPlaceh
$objWriter->endElement();

// Font - a:latin
$objWriter->startElement('a:latin');
//$objWriter->startElement('a:latin');
$fontFormat = 'latin';
$elementFontFormat = $element->getFont()->getFormat();
if($elementFontFormat == Font::FONT_FORMAT_LATIN){
$fontFormat = Font::FONT_FORMAT_LATIN;
}elseif($elementFontFormat == Font::FONT_FORMAT_EAST_ASIAN){
$fontFormat = Font::FONT_FORMAT_EAST_ASIAN;
}elseif($elementFontFormat == Font::FONT_FORMAT_COMPLEX_SCRIPT){
$fontFormat = Font::FONT_FORMAT_COMPLEX_SCRIPT;
}
$objWriter->startElement('a:'.$fontFormat);
$objWriter->writeAttribute('typeface', $element->getFont()->getName());
$objWriter->endElement();

Expand Down