Skip to content

Commit

Permalink
Merge pull request PHPOffice#682 from MartynasJanu/ppt2007-hyperlink-…
Browse files Browse the repository at this point in the history
…colors-fix

Fix: Hyperlink text color for PowerPoint2007 writer
  • Loading branch information
Progi1984 authored Nov 25, 2021
2 parents c34154c + 7848fed commit 47046dd
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 18 deletions.
8 changes: 4 additions & 4 deletions docs/changes/1.0.0.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/changes/1.1.0.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions docs/usage/shapes/richtext.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,42 @@ $richText->setColumnSpacing(200);
$columnSpacing = $richText->getColumnSpacing();
```

## Hyperlink

For a rich text, you can define the hyperlink.

Example:

```php
<?php

use PhpOffice\PhpPresentation\Shape\RichText;

$richText = new RichText();
$richText->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
<?php

use PhpOffice\PhpPresentation\Shape\RichText;

$richText = new RichText();
$richText->getHyperlink()->setUrl('https://phpoffice.github.io/PHPPresentation/');
$richText->getHyperlink()->setIsTextColorUsed(true);

```

## Paragraph
### Bullet

Expand Down
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
36 changes: 24 additions & 12 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
36 changes: 35 additions & 1 deletion src/PhpPresentation/Shape/Hyperlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class Hyperlink
*/
private $hashIndex;

/**
* If true, uses the text color, instead of theme color
*
* @var bool
*/
private $isTextColorUsed = false;

/**
* Create a new \PhpOffice\PhpPresentation\Shape\Hyperlink.
*
Expand All @@ -68,7 +75,6 @@ class Hyperlink
*/
public function __construct(string $pUrl = '', string $pTooltip = '')
{
// Initialise member variables
$this->setUrl($pUrl);
$this->setTooltip($pTooltip);
}
Expand Down Expand Up @@ -195,4 +201,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 isTextColorUsed(): bool
{
return $this->isTextColorUsed;
}

/**
* 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 $isTextColorUsed
*
* @return self
*/
public function setIsTextColorUsed(bool $isTextColorUsed): self
{
$this->isTextColorUsed = $isTextColorUsed;

return $this;
}
}
13 changes: 13 additions & 0 deletions src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,19 @@ protected function writeHyperlink(XMLWriter $objWriter, $shape): void
if ($shape->getHyperlink()->isInternal()) {
$objWriter->writeAttribute('action', $shape->getHyperlink()->getUrl());
}

if ($shape->getHyperlink()->isTextColorUsed()) {
$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();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpPresentation/Tests/Shape/HyperlinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 47046dd

Please sign in to comment.