From 0112f8bc5bb74f1f952c18e0df4fd04df12765db Mon Sep 17 00:00:00 2001 From: Amir-Asus Date: Fri, 16 Apr 2021 23:20:39 +0430 Subject: [PATCH] add Font Format support; add RTL Direction Support; --- samples/Sample_01_Simple.php | 9 ++-- src/PhpPresentation/Style/Alignment.php | 34 +++++++++++++++ src/PhpPresentation/Style/Font.php | 41 ++++++++++++++++++- .../Writer/PowerPoint2007/AbstractSlide.php | 14 ++++++- 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/samples/Sample_01_Simple.php b/samples/Sample_01_Simple.php index e7ebd0b9a5..798fd5e962 100644 --- a/samples/Sample_01_Simple.php +++ b/samples/Sample_01_Simple.php @@ -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 @@ -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); diff --git a/src/PhpPresentation/Style/Alignment.php b/src/PhpPresentation/Style/Alignment.php index 89cde6a780..d0ef8bc08a 100644 --- a/src/PhpPresentation/Style/Alignment.php +++ b/src/PhpPresentation/Style/Alignment.php @@ -51,6 +51,12 @@ class Alignment implements ComparableInterface self::HORIZONTAL_RIGHT, ); + /** + * RTL Direction Support + * @var boolean + */ + private $RTL; + /** * Horizontal * @var string @@ -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 * @@ -357,6 +390,7 @@ public function getHashCode() { return md5( $this->horizontal + . ($this->RTL ? 't' : 'f') . $this->vertical . $this->level . $this->indent diff --git a/src/PhpPresentation/Style/Font.php b/src/PhpPresentation/Style/Font.php index e4b49d0f36..5338f2a24c 100644 --- a/src/PhpPresentation/Style/Font.php +++ b/src/PhpPresentation/Style/Font.php @@ -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 @@ -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; @@ -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 @@ -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__); } /** diff --git a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php index 0c220f00f0..a73f29bbb1 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php @@ -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; @@ -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())); @@ -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();