From f9b55fb5c37dcb7070cd0cc90c310be5178a4304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Janu=C5=A1kauskas?= Date: Thu, 28 Oct 2021 16:39:14 +0300 Subject: [PATCH 1/6] Hyperlink color fix for PowerPoint2007 writer --- src/PhpPresentation/Shape/Hyperlink.php | 35 +++++++++++++++++++ .../Writer/PowerPoint2007/AbstractSlide.php | 13 +++++++ 2 files changed, 48 insertions(+) diff --git a/src/PhpPresentation/Shape/Hyperlink.php b/src/PhpPresentation/Shape/Hyperlink.php index 694fe2acf..f7aeba094 100644 --- a/src/PhpPresentation/Shape/Hyperlink.php +++ b/src/PhpPresentation/Shape/Hyperlink.php @@ -60,6 +60,13 @@ class Hyperlink */ private $hashIndex; + /** + * If true, uses the text color, instead of theme color + * + * @var bool + */ + private $useTextColor = false; + /** * Create a new \PhpOffice\PhpPresentation\Shape\Hyperlink. * @@ -195,4 +202,32 @@ public function setHashIndex(int $value) return $this; } + + /** + * Get whether or not to use text color for a hyperlink, instead of theme color. + * + * @see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-odrawxml/014fbc20-3705-4812-b8cd-93f5af05b504 + * + * @return bool Whether or not to use text color for a hyperlink, instead of theme color. + */ + public function getUseTextColor(): bool + { + return $this->useTextColor; + } + + /** + * Set whether or not to use text color for a hyperlink, instead of theme color. + * + * @see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-odrawxml/014fbc20-3705-4812-b8cd-93f5af05b504 + * + * @param bool $useTextColor + * + * @return self + */ + public function setUseTextColor(bool $useTextColor): self + { + $this->useTextColor = $useTextColor; + + return $this; + } } diff --git a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php index d7f533108..566f1627a 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php @@ -788,6 +788,19 @@ protected function writeHyperlink(XMLWriter $objWriter, $shape): void if ($shape->getHyperlink()->isInternal()) { $objWriter->writeAttribute('action', $shape->getHyperlink()->getUrl()); } + + if ($shape->getHyperlink()->getUseTextColor()) { + $objWriter->startElement('a:extLst'); + $objWriter->startElement('a:ext'); + $objWriter->writeAttribute('uri', '{A12FA001-AC4F-418D-AE19-62706E023703}'); + $objWriter->startElement('ahyp:hlinkClr'); + $objWriter->writeAttribute('xmlns:ahyp', 'http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor'); + $objWriter->writeAttribute('val', 'tx'); + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + } + $objWriter->endElement(); } From 3d87979348f3dbb617e4534349a4f69e052ee50e Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 25 Nov 2021 21:05:57 +0100 Subject: [PATCH 2/6] Fixed PHP MD Errors --- src/PhpPresentation/Shape/Hyperlink.php | 13 ++++++------- .../Writer/PowerPoint2007/AbstractSlide.php | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/PhpPresentation/Shape/Hyperlink.php b/src/PhpPresentation/Shape/Hyperlink.php index f7aeba094..13b7cc937 100644 --- a/src/PhpPresentation/Shape/Hyperlink.php +++ b/src/PhpPresentation/Shape/Hyperlink.php @@ -65,7 +65,7 @@ class Hyperlink * * @var bool */ - private $useTextColor = false; + private $isTextColorUsed = false; /** * Create a new \PhpOffice\PhpPresentation\Shape\Hyperlink. @@ -75,7 +75,6 @@ class Hyperlink */ public function __construct(string $pUrl = '', string $pTooltip = '') { - // Initialise member variables $this->setUrl($pUrl); $this->setTooltip($pTooltip); } @@ -210,9 +209,9 @@ public function setHashIndex(int $value) * * @return bool Whether or not to use text color for a hyperlink, instead of theme color. */ - public function getUseTextColor(): bool + public function isTextColorUsed(): bool { - return $this->useTextColor; + return $this->isTextColorUsed; } /** @@ -220,13 +219,13 @@ public function getUseTextColor(): bool * * @see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-odrawxml/014fbc20-3705-4812-b8cd-93f5af05b504 * - * @param bool $useTextColor + * @param bool $isTextColorUsed * * @return self */ - public function setUseTextColor(bool $useTextColor): self + public function setIsTextColorUsed(bool $isTextColorUsed): self { - $this->useTextColor = $useTextColor; + $this->isTextColorUsed = $isTextColorUsed; return $this; } diff --git a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php index 566f1627a..c310379bd 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php @@ -789,7 +789,7 @@ protected function writeHyperlink(XMLWriter $objWriter, $shape): void $objWriter->writeAttribute('action', $shape->getHyperlink()->getUrl()); } - if ($shape->getHyperlink()->getUseTextColor()) { + if ($shape->getHyperlink()->isTextColorUsed()) { $objWriter->startElement('a:extLst'); $objWriter->startElement('a:ext'); $objWriter->writeAttribute('uri', '{A12FA001-AC4F-418D-AE19-62706E023703}'); From a0e9b67f73d9856cc6c4f484a0b7e79df3c9ea83 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 25 Nov 2021 21:06:25 +0100 Subject: [PATCH 3/6] Added Unit Tests --- .../Tests/Shape/HyperlinkTest.php | 12 ++++++++ .../Writer/PowerPoint2007/PptSlidesTest.php | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/tests/PhpPresentation/Tests/Shape/HyperlinkTest.php b/tests/PhpPresentation/Tests/Shape/HyperlinkTest.php index 991f72852..0b027e140 100644 --- a/tests/PhpPresentation/Tests/Shape/HyperlinkTest.php +++ b/tests/PhpPresentation/Tests/Shape/HyperlinkTest.php @@ -121,4 +121,16 @@ public function testIsInternal(): void $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Hyperlink', $object->setUrl('http://www.github.com')); $this->assertFalse($object->isInternal()); } + + public function testIsTextColorUsed(): void + { + $object = new Hyperlink(); + $this->assertFalse($object->isTextColorUsed()); + + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Hyperlink', $object->setIsTextColorUsed(true)); + $this->assertTrue($object->isTextColorUsed()); + + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Hyperlink', $object->setIsTextColorUsed(false)); + $this->assertFalse($object->isTextColorUsed()); + } } diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptSlidesTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptSlidesTest.php index 0d0f60681..c1651a84c 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptSlidesTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptSlidesTest.php @@ -452,6 +452,36 @@ public function testHyperlinkInternal(): void $this->assertIsSchemaECMA376Valid(); } + public function testHyperlinkTextColorUsed(): void + { + $oSlide = $this->oPresentation->getActiveSlide(); + $oRichText = $oSlide->createRichTextShape(); + $oRun = $oRichText->createTextRun('Delta'); + $oRun->getHyperlink()->setIsTextColorUsed(true); + + $element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr/a:hlinkClick'; + $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); + + $element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr/a:hlinkClick/a:extLst'; + $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); + + $element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr/a:hlinkClick/a:extLst/a:ext'; + $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); + $this->assertZipXmlAttributeEquals('ppt/slides/slide1.xml', $element, 'uri', '{A12FA001-AC4F-418D-AE19-62706E023703}'); + + $this->assertIsSchemaECMA376Valid(); + + $this->resetPresentationFile(); + + $oRun->getHyperlink()->setIsTextColorUsed(false); + + $element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr/a:hlinkClick'; + $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); + + $element = '/p:sld/p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:rPr/a:hlinkClick/a:extLst'; + $this->assertZipXmlElementNotExists('ppt/slides/slide1.xml', $element); + } + public function testListBullet(): void { $oSlide = $this->oPresentation->getActiveSlide(); From 87db7de427a56b742b1ec1c6182f3d5fb287e1ca Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 25 Nov 2021 21:06:45 +0100 Subject: [PATCH 4/6] Added Support of Reader --- src/PhpPresentation/Reader/PowerPoint2007.php | 36 ++++++++++++------- .../Tests/Reader/PowerPoint2007Test.php | 1 + 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/PhpPresentation/Reader/PowerPoint2007.php b/src/PhpPresentation/Reader/PowerPoint2007.php index 3b3a6f5c5..6f42b2dd5 100644 --- a/src/PhpPresentation/Reader/PowerPoint2007.php +++ b/src/PhpPresentation/Reader/PowerPoint2007.php @@ -35,6 +35,7 @@ use PhpOffice\PhpPresentation\PresentationProperties; use PhpOffice\PhpPresentation\Shape\Drawing\Base64; use PhpOffice\PhpPresentation\Shape\Drawing\Gd; +use PhpOffice\PhpPresentation\Shape\Hyperlink; use PhpOffice\PhpPresentation\Shape\Placeholder; use PhpOffice\PhpPresentation\Shape\RichText; use PhpOffice\PhpPresentation\Shape\RichText\Paragraph; @@ -777,12 +778,9 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr // Hyperlink $oElementHlinkClick = $document->getElement('a:hlinkClick', $oElement); if (is_object($oElementHlinkClick)) { - if ($oElementHlinkClick->hasAttribute('tooltip')) { - $oShape->getHyperlink()->setTooltip($oElementHlinkClick->getAttribute('tooltip')); - } - if ($oElementHlinkClick->hasAttribute('r:id') && isset($this->arrayRels[$fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target'])) { - $oShape->getHyperlink()->setUrl($this->arrayRels[$fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target']); - } + $oShape->setHyperlink( + $this->loadHyperlink($document, $oElementHlinkClick, $oShape->getHyperlink()) + ); } } @@ -1211,12 +1209,9 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh // Hyperlink $oElementHlinkClick = $document->getElement('a:hlinkClick', $oElementrPr); if (is_object($oElementHlinkClick)) { - if ($oElementHlinkClick->hasAttribute('tooltip')) { - $oText->getHyperlink()->setTooltip($oElementHlinkClick->getAttribute('tooltip')); - } - if ($oElementHlinkClick->hasAttribute('r:id') && isset($this->arrayRels[$this->fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target'])) { - $oText->getHyperlink()->setUrl($this->arrayRels[$this->fileRels][$oElementHlinkClick->getAttribute('r:id')]['Target']); - } + $oText->setHyperlink( + $this->loadHyperlink($document, $oElementHlinkClick, $oText->getHyperlink()) + ); } // Font $oElementFontFormat = null; @@ -1249,6 +1244,23 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh } } + protected function loadHyperlink(XMLReader $xmlReader, DOMElement $element, Hyperlink $hyperlink): Hyperlink + { + if ($element->hasAttribute('tooltip')) { + $hyperlink->setTooltip($element->getAttribute('tooltip')); + } + if ($element->hasAttribute('r:id') && isset($this->arrayRels[$this->fileRels][$element->getAttribute('r:id')]['Target'])) { + $hyperlink->setUrl($this->arrayRels[$this->fileRels][$element->getAttribute('r:id')]['Target']); + } + if ($subElementExt = $xmlReader->getElement('a:extLst/a:ext', $element)) { + if ($subElementExt->hasAttribute('uri') && $subElementExt->getAttribute('uri') == '{A12FA001-AC4F-418D-AE19-62706E023703}') { + $hyperlink->setIsTextColorUsed(true); + } + } + + return $hyperlink; + } + protected function loadStyleBorder(XMLReader $xmlReader, DOMElement $oElement, Border $oBorder): void { if ($oElement->hasAttribute('w')) { diff --git a/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php b/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php index 7db18c9e1..dea2f7537 100644 --- a/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php +++ b/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php @@ -568,6 +568,7 @@ public function testLoadFile01(): void $this->assertTrue($oRichText->hasHyperlink()); $this->assertEquals('https://github.com/PHPOffice/PHPPresentation/', $oRichText->getHyperlink()->getUrl()); $this->assertEquals('PHPPresentation', $oRichText->getHyperlink()->getTooltip()); + $this->assertFalse($oRichText->getHyperlink()->isTextColorUsed()); $this->assertEquals('Calibri', $oRichText->getFont()->getName()); $this->assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat()); } From c62543017d8c3e8f39c86eaa82d7be986129b86e Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 25 Nov 2021 21:06:56 +0100 Subject: [PATCH 5/6] Added documentation --- docs/changes/1.0.0.md | 8 ++++---- docs/changes/1.1.0.md | 7 +++++++ docs/usage/shapes/richtext.md | 36 +++++++++++++++++++++++++++++++++++ mkdocs.yml | 3 ++- 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 docs/changes/1.1.0.md diff --git a/docs/changes/1.0.0.md b/docs/changes/1.0.0.md index c23af1e93..270ebbd8a 100644 --- a/docs/changes/1.0.0.md +++ b/docs/changes/1.0.0.md @@ -1,9 +1,9 @@ -# 1.0.0 - WIP +# 1.0.0 ## Bugfix -- PowerPoint2007 Writer : Text is subscripted when set superscript to false - [[@qmachard]](https://github.com/qmachard])(https://github.com/qmachard) GH-360 -- Core : Defining width & height of a shape don't return any error if width & height were equal to 0 - [[@surger]](https://github.com/surger])(https://github.com/surger) GH-555 -- ODPresentation Writer : Display axis title depending the visibility - [[@Progi1984]](https://github.com/Progi1984])(https://github.com/Progi1984) GH-410 +- PowerPoint2007 Writer : Text is subscripted when set superscript to false - [@qmachard](https://github.com/qmachard) GH-360 +- Core : Defining width & height of a shape don't return any error if width & height were equal to 0 - [@surger](https://github.com/surger) GH-555 +- ODPresentation Writer : Display axis title depending the visibility - [@Progi1984](https://github.com/Progi1984) GH-410 ## Changes - Dropped support for HHVM - [@sunspikes](https://github.com/sunspikes) GH-556 diff --git a/docs/changes/1.1.0.md b/docs/changes/1.1.0.md new file mode 100644 index 000000000..8f5267ad2 --- /dev/null +++ b/docs/changes/1.1.0.md @@ -0,0 +1,7 @@ +# 1.0.0 - WIP + +## Features + +- Support for Hyperlink Text Color - [@MartynasJanu](https://github.com/MartynasJanu) & [@Progi1984](https://github.com/Progi1984) GH-682 + - PowerPoint2007 Reader + - PowerPoint2007 Writer \ No newline at end of file diff --git a/docs/usage/shapes/richtext.md b/docs/usage/shapes/richtext.md index e44f358e3..5ee4393a3 100644 --- a/docs/usage/shapes/richtext.md +++ b/docs/usage/shapes/richtext.md @@ -50,6 +50,42 @@ $richText->setColumnSpacing(200); $columnSpacing = $richText->getColumnSpacing(); ``` +## Hyperlink + +For a rich text, you can define the hyperlink. + +Example: + +```php +getHyperlink()->setUrl('https://phpoffice.github.io/PHPPresentation/'); + +``` + +### Use of Text Color + +!!! warning + Available only on the PowerPoint2007 Reader/Writer + +Hyperlinks can be set to use the text color instead of the default theme color. + +Example: + +```php +getHyperlink()->setUrl('https://phpoffice.github.io/PHPPresentation/'); +$richText->getHyperlink()->setIsTextColorUsed(true); + +``` + ## Paragraph ### Bullet diff --git a/mkdocs.yml b/mkdocs.yml index c60fad741..b1888ee68 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -67,7 +67,8 @@ nav: - FAQ: 'faq.md' - Credits: 'credits.md' - Releases: - - '1.0.0 (WIP)': 'changes/1.0.0.md' + - '1.1.0 (WIP)': 'changes/1.1.0.md' + - '1.0.0': 'changes/1.0.0.md' - '0.9.0': 'changes/0.9.0.md' - '0.8.0': 'changes/0.8.0.md' - '0.7.0': 'changes/0.7.0.md' From 7848fed55bd37b3407cbea441e33747102592d08 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 25 Nov 2021 21:14:32 +0100 Subject: [PATCH 6/6] Fixed PHPCS errors --- src/PhpPresentation/Shape/Hyperlink.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpPresentation/Shape/Hyperlink.php b/src/PhpPresentation/Shape/Hyperlink.php index 13b7cc937..06e0be9ad 100644 --- a/src/PhpPresentation/Shape/Hyperlink.php +++ b/src/PhpPresentation/Shape/Hyperlink.php @@ -207,7 +207,7 @@ public function setHashIndex(int $value) * * @see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-odrawxml/014fbc20-3705-4812-b8cd-93f5af05b504 * - * @return bool Whether or not to use text color for a hyperlink, instead of theme color. + * @return bool whether or not to use text color for a hyperlink, instead of theme color */ public function isTextColorUsed(): bool {