Skip to content

Commit 3dfc5f5

Browse files
devX2712Progi1984
authored andcommitted
ADD Effect managment to Shape,Paragraph and Text
ADD Effect mangment Effect is a collection of effect into each element (innerShadow, outerShadow, reflection, glos, etc. Actualy only shadow are supported, but the strucure is desgin for other effect
1 parent cdaa211 commit 3dfc5f5

File tree

6 files changed

+598
-44
lines changed

6 files changed

+598
-44
lines changed

src/PhpPresentation/AbstractShape.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ abstract class AbstractShape implements ComparableInterface
103103
protected $placeholder;
104104

105105
/**
106-
* Hash index.
106+
* List of effect apply to shape
107+
* @var array \PhpOffice\PhpPresentation\Style\Effect[]
108+
*/
109+
protected ?array $effectCollection = null;
110+
111+
/**
112+
* Hash index
107113
*
108114
* @var int
109115
*/
@@ -125,6 +131,7 @@ public function __construct()
125131
$this->fill = new Fill();
126132
$this->shadow = new Shadow();
127133
$this->border = new Border();
134+
$this->effectCollection = null;
128135

129136
$this->border->setLineStyle(Style\Border::LINE_NONE);
130137
}
@@ -411,6 +418,46 @@ public function setHyperlink(?Hyperlink $pHyperlink = null): self
411418

412419
return $this;
413420
}
421+
422+
/**
423+
* Add an effect to the shpae
424+
*
425+
* @param \PhpOffice\PhpPresentation\Style\Effect $effect
426+
* @return $this
427+
*/
428+
public function addEffect(Shape\Effect $effect)
429+
{
430+
if (!isset($this->effectCollection)) {
431+
$this->effectCollection = array();
432+
}
433+
$this->effectCollection[] = $effect;
434+
return $this;
435+
}
436+
437+
/**
438+
* Get the effect collection
439+
*
440+
* @return array \PhpOffice\PhpPresentation\Style\Effect[]
441+
*/
442+
public function getEffectCollection():?array
443+
{
444+
return $this->effectCollection;
445+
}
446+
447+
/**
448+
* Set the effect collection
449+
*
450+
* @param array \PhpOffice\PhpPresentation\Style\Effect $effectCollection
451+
* @return $this
452+
*/
453+
public function setEffectCollection(array $effectCollection)
454+
{
455+
if ( isset($effectCollection)
456+
&& is_array($effectCollection)) {
457+
$this->effectCollection = $effectCollection;
458+
}
459+
return $this;
460+
}
414461

415462
/**
416463
* Get hash code.

src/PhpPresentation/Reader/PowerPoint2007.php

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -877,46 +877,81 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr
877877
$oShape->setHeight(CommonDrawing::emuToPixels((int) $oElement->getAttribute('cy')));
878878
}
879879
}
880-
881-
$oElement = $document->getElement('p:spPr/a:effectLst', $node);
882-
if ($oElement instanceof DOMElement) {
883-
$oShape->getShadow()->setVisible(true);
884-
885-
$oSubElement = $document->getElement('a:outerShdw', $oElement);
886-
if ($oSubElement instanceof DOMElement) {
887-
if ($oSubElement->hasAttribute('blurRad')) {
888-
$oShape->getShadow()->setBlurRadius(CommonDrawing::emuToPixels((int) $oSubElement->getAttribute('blurRad')));
889-
}
890-
if ($oSubElement->hasAttribute('dist')) {
891-
$oShape->getShadow()->setDistance(CommonDrawing::emuToPixels((int) $oSubElement->getAttribute('dist')));
892-
}
893-
if ($oSubElement->hasAttribute('dir')) {
894-
$oShape->getShadow()->setDirection((int) CommonDrawing::angleToDegrees((int) $oSubElement->getAttribute('dir')));
895-
}
896-
if ($oSubElement->hasAttribute('algn')) {
897-
$oShape->getShadow()->setAlignment($oSubElement->getAttribute('algn'));
898-
}
899-
}
900-
901-
$oSubElement = $document->getElement('a:outerShdw/a:srgbClr', $oElement);
902-
if ($oSubElement instanceof DOMElement) {
903-
if ($oSubElement->hasAttribute('val')) {
904-
$oColor = new Color();
905-
$oColor->setRGB($oSubElement->getAttribute('val'));
906-
$oShape->getShadow()->setColor($oColor);
907-
}
908-
}
909-
910-
$oSubElement = $document->getElement('a:outerShdw/a:srgbClr/a:alpha', $oElement);
911-
if ($oSubElement instanceof DOMElement) {
912-
if ($oSubElement->hasAttribute('val')) {
913-
$oShape->getShadow()->setAlpha((int) $oSubElement->getAttribute('val') / 1000);
914-
}
915-
}
880+
// Load shape effects
881+
$oEffect = $document->getElement('p:spPr/a:effectLst', $node);
882+
if ($oEffect instanceof \DOMElement) {
883+
$aEffect = $this->loadEffect($document, $oEffect);
884+
if (isset($aEffect) && is_array($aEffect)) {
885+
$oShape->setEffectCollection($aEffect);
886+
}
916887
}
917-
918888
$oSlide->addShape($oShape);
919889
}
890+
891+
/**
892+
* Load Effect for shape or paragraph
893+
*
894+
* @param XMLReader $document
895+
* @param \DOMElement $node
896+
* @return array \PhpOffice\PhpPresentation\Style\Effect[]
897+
*/
898+
protected function loadEffect(XMLReader $document, \DOMElement $nodeEffect)
899+
{
900+
$aEffect = null;
901+
if ($nodeEffect instanceof \DOMElement) {
902+
903+
$aNodes = $document->getElements('*', $nodeEffect);
904+
foreach ($aNodes as $node) {
905+
906+
$type = explode(':', $node->tagName);
907+
$type = array_pop($type);
908+
if ( $type == 'outerShdw'
909+
|| $type == 'innerShdw') {
910+
// @TODO || $type == 'reflection') {
911+
912+
// Create a new effect
913+
$effect = new \PhpOffice\PhpPresentation\Style\Effect($type);
914+
// load blur radius
915+
if ($node->hasAttribute('blurRad')) {
916+
$effect->setBlurRadius(CommonDrawing::emuToPixels($node->getAttribute('blurRad')));
917+
}
918+
// load distance
919+
if ($node->hasAttribute('dist')) {
920+
$effect->setDistance(CommonDrawing::emuToPixels($node->getAttribute('dist')));
921+
}
922+
// load direction
923+
if ($node->hasAttribute('dir')) {
924+
$effect->setDirection(CommonDrawing::angleToDegrees($node->getAttribute('dir')));
925+
}
926+
// load alignment
927+
if ($node->hasAttribute('algn')) {
928+
$effect->setAlignment($node->getAttribute('algn'));
929+
}
930+
931+
// Get color define by prstClr
932+
$oSubElement = $document->getElement('a:prstClr', $node);
933+
if ($oSubElement instanceof \DOMElement && $oSubElement->hasAttribute('val')) {
934+
$oColor = new Color();
935+
$oColor->setRGB($oSubElement->getAttribute('val'));
936+
$effect->setColor($oColor);
937+
// Get Alpha
938+
$oSubElt = $document->getElement('a:alpha', $oSubElement);
939+
if ($oSubElt instanceof \DOMElement && $oSubElt->hasAttribute('val')) {
940+
$effect->setAlpha((int)$oSubElt->getAttribute('val') / 1000);
941+
}
942+
}
943+
// Load reflection atributs
944+
// @TODO future implementation
945+
if ($node->tagName == 'a:reflection') {
946+
}
947+
948+
if (!isset($aEffect)) $aEffect = array();
949+
$aEffect[] = $effect;
950+
}
951+
}
952+
}
953+
return $aEffect;
954+
}
920955

921956
/**
922957
* @param AbstractSlide|Note $oSlide
@@ -968,6 +1003,16 @@ protected function loadShapeRichText(XMLReader $document, DOMElement $node, $oSl
9681003
}
9691004
}
9701005

1006+
// Load shape effects
1007+
$oEffect = $document->getElement('p:spPr/a:effectLst', $node);
1008+
if ($oEffect instanceof \DOMElement) {
1009+
$aEffect = $this->loadEffect($document, $oEffect);
1010+
if (isset($aEffect) && is_array($aEffect)) {
1011+
$oShape->setEffectCollection($aEffect);
1012+
}
1013+
}
1014+
1015+
// FBU-20210202+ Read body definitions
9711016
$bodyPr = $document->getElement('p:txBody/a:bodyPr', $node);
9721017
if (($bodyPr instanceof \DOMElement) && $bodyPr->hasAttribute('lIns')) {
9731018
$oShape->setInsetLeft((int)$bodyPr->getAttribute('lIns'));
@@ -1317,6 +1362,14 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
13171362
if ($oElementFont->hasAttribute('typeface')) {
13181363
$oText->getFont()->setName($oElementFont->getAttribute('typeface'));
13191364
}
1365+
}
1366+
// Load shape effects
1367+
$oEffect = $document->getElement('a:effectLst', $oElementrPr);
1368+
if ($oEffect instanceof \DOMElement) {
1369+
$aEffect = $this->loadEffect($document, $oEffect);
1370+
if (isset($aEffect) && is_array($aEffect)) {
1371+
$oText->setEffectCollection($aEffect);
1372+
}
13201373
}
13211374
//} else {
13221375
// $oText = $oParagraph->createText();

src/PhpPresentation/Shape/RichText/Paragraph.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class Paragraph implements ComparableInterface
6666
private $lineSpacing = 100;
6767

6868
/**
69+
* List of effect apply to paragraph
70+
* @var array \PhpOffice\PhpPresentation\Style\Effect[]
71+
*/
72+
protected ?array $effectCollection = null;
73+
74+
/**
75+
* Hash index
76+
*
6977
* @var string
7078
*/
7179
private $lineSpacingMode = self::LINE_SPACING_MODE_PERCENT;
@@ -95,6 +103,7 @@ public function __construct()
95103
$this->alignment = new Alignment();
96104
$this->font = new Font();
97105
$this->bulletStyle = new Bullet();
106+
$this->effectCollection = null;
98107
}
99108

100109
/**
@@ -255,7 +264,47 @@ public function setRichTextElements(array $pElements = []): self
255264
}
256265

257266
/**
258-
* Get hash code.
267+
* Add an effect to the shpae
268+
*
269+
* @param \PhpOffice\PhpPresentation\Style\Effect $effect
270+
* @return $this
271+
*/
272+
public function addEffect(Shape\Effect $effect)
273+
{
274+
if (!isset($this->effectCollection)) {
275+
$this->effectCollection = array();
276+
}
277+
$this->effectCollection[] = $effect;
278+
return $this;
279+
}
280+
281+
/**
282+
* Get the effect collection
283+
*
284+
* @return array \PhpOffice\PhpPresentation\Style\Effect[]
285+
*/
286+
public function getEffectCollection():?array
287+
{
288+
return $this->effectCollection;
289+
}
290+
291+
/**
292+
* Set the effect collection
293+
*
294+
* @param array \PhpOffice\PhpPresentation\Style\Effect $effectCollection
295+
* @return $this
296+
*/
297+
public function setEffectCollection(array $effectCollection)
298+
{
299+
if ( isset($effectCollection)
300+
&& is_array($effectCollection)) {
301+
$this->effectCollection = $effectCollection;
302+
}
303+
return $this;
304+
}
305+
306+
/**
307+
* Get hash code
259308
*
260309
* @return string Hash code
261310
*/

src/PhpPresentation/Shape/RichText/Run.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ class Run extends TextElement implements TextElementInterface
3434
private $font;
3535

3636
/**
37-
* Create a new \PhpOffice\PhpPresentation\Shape\RichText\Run instance.
37+
* List of effect apply to paragraph
38+
* @var array \PhpOffice\PhpPresentation\Style\Effect[]
39+
*/
40+
protected ?array $effectCollection = null;
41+
42+
/**
43+
* Create a new \PhpOffice\PhpPresentation\Shape\RichText\Run instance
3844
*
3945
* @param string $pText Text
4046
*/
@@ -43,6 +49,7 @@ public function __construct($pText = '')
4349
// Initialise variables
4450
$this->setText($pText);
4551
$this->font = new Font();
52+
$this->effectCollection = null;
4653
}
4754

4855
/**
@@ -68,7 +75,47 @@ public function setFont(?Font $pFont = null)
6875
}
6976

7077
/**
71-
* Get hash code.
78+
* Add an effect to the shpae
79+
*
80+
* @param \PhpOffice\PhpPresentation\Style\Effect $effect
81+
* @return $this
82+
*/
83+
public function addEffect(Shape\Effect $effect)
84+
{
85+
if (!isset($this->effectCollection)) {
86+
$this->effectCollection = array();
87+
}
88+
$this->effectCollection[] = $effect;
89+
return $this;
90+
}
91+
92+
/**
93+
* Get the effect collection
94+
*
95+
* @return array \PhpOffice\PhpPresentation\Style\Effect[]
96+
*/
97+
public function getEffectCollection():?array
98+
{
99+
return $this->effectCollection;
100+
}
101+
102+
/**
103+
* Set the effect collection
104+
*
105+
* @param array \PhpOffice\PhpPresentation\Style\Effect $effectCollection
106+
* @return $this
107+
*/
108+
public function setEffectCollection(array $effectCollection)
109+
{
110+
if ( isset($effectCollection)
111+
&& is_array($effectCollection)) {
112+
$this->effectCollection = $effectCollection;
113+
}
114+
return $this;
115+
}
116+
117+
/**
118+
* Get hash code
72119
*
73120
* @return string Hash code
74121
*/

0 commit comments

Comments
 (0)