Skip to content
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

Support for Capitalization for Font #779

Merged
merged 1 commit into from
Dec 8, 2023
Merged
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
5 changes: 5 additions & 0 deletions docs/changes/1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
- PowerPoint2007 Writer
- Added support for PHP 8.2 & 8.3 - [@Progi1984](https://github.com/Progi1984) in [#769](https://github.com/PHPOffice/PHPPresentation/pull/769)
- PowerPoint 2007 Writer : Added support to Font for Axis tick label & chinese font support - [@zhengwhizz](https://github.com/zhengwhizz) in [#774](https://github.com/PHPOffice/PHPPresentation/pull/774)
- Support for Capitalization for Font - [@CxRxExO](https://github.com/CxRxExO) & [@Progi1984](https://github.com/Progi1984) in [#779](https://github.com/PHPOffice/PHPPresentation/pull/779)
- ODPresentation Reader
- ODPresentation Writer
- PowerPoint2007 Reader
- PowerPoint2007 Writer

## Improvements
- Slide : Raised max value for identifier rand call - [@Scheissy](https://github.com/Scheissy) in [#777](https://github.com/PHPOffice/PHPPresentation/pull/777)
Expand Down
22 changes: 22 additions & 0 deletions docs/usage/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ echo $alignment->isRTL();
- `underline`
- `strikethrough`
- `color`
- `capitalization`

### Capitalization

Some formats are available :

* `Font::CAPITALIZATION_NONE`
* `Font::CAPITALIZATION_ALL`
* `Font::CAPITALIZATION_SMALL`

``` php
<?php

use PhpOffice\PhpPresentation\Style\Font;

$font = new Font();

// Set capitalization of font
$font->setCapitalization(Font::CAPITALIZATION_ALL);
// Get capitalization of font
echo $font->getCapitalization();
```

### Format

Expand Down
35 changes: 35 additions & 0 deletions src/PhpPresentation/Exception/NotAllowedValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @see https://github.com/PHPOffice/PHPPresentation
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

declare(strict_types=1);

namespace PhpOffice\PhpPresentation\Exception;

class NotAllowedValueException extends PhpPresentationException
{
/**
* @param array<string> $value
*/
public function __construct(string $parameter, array $value)
{
parent::__construct(sprintf(
'The value "%s" is not in allowed values ("%s")',
$parameter,
implode('", "', $value)
));
}
}
16 changes: 16 additions & 0 deletions src/PhpPresentation/Reader/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,22 @@ protected function loadStyle(DOMElement $nodeStyle): bool
if ($nodeTextProperties->hasAttribute('fo:color')) {
$oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6));
}
if ($nodeTextProperties->hasAttribute('fo:text-transform')) {
switch ($nodeTextProperties->getAttribute('fo:text-transform')) {
case 'none':
$oFont->setCapitalization(Font::CAPITALIZATION_NONE);

break;
case 'lowercase':
$oFont->setCapitalization(Font::CAPITALIZATION_SMALL);

break;
case 'uppercase':
$oFont->setCapitalization(Font::CAPITALIZATION_ALL);

break;
}
}
// Font Latin
if ($nodeTextProperties->hasAttribute('fo:font-family')) {
$oFont
Expand Down
3 changes: 3 additions & 0 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,9 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
if ($oElementrPr->hasAttribute('u')) {
$oText->getFont()->setUnderline($oElementrPr->getAttribute('u'));
}
if ($oElementrPr->hasAttribute('cap')) {
$oText->getFont()->setCapitalization($oElementrPr->getAttribute('cap'));
}
// Color
$oElementSrgbClr = $document->getElement('a:solidFill/a:srgbClr', $oElementrPr);
if (is_object($oElementSrgbClr) && $oElementSrgbClr->hasAttribute('val')) {
Expand Down
37 changes: 37 additions & 0 deletions src/PhpPresentation/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace PhpOffice\PhpPresentation\Style;

use PhpOffice\PhpPresentation\ComparableInterface;
use PhpOffice\PhpPresentation\Exception\NotAllowedValueException;

/**
* \PhpOffice\PhpPresentation\Style\Font.
Expand Down Expand Up @@ -50,6 +51,10 @@ class Font implements ComparableInterface
public const FORMAT_EAST_ASIAN = 'ea';
public const FORMAT_COMPLEX_SCRIPT = 'cs';

public const CAPITALIZATION_NONE = 'none';
public const CAPITALIZATION_SMALL = 'small';
public const CAPITALIZATION_ALL = 'all';

/**
* Name.
*
Expand Down Expand Up @@ -92,6 +97,13 @@ class Font implements ComparableInterface
*/
private $subScript = false;

/**
* Capitalization.
*
* @var string
*/
private $capitalization = self::CAPITALIZATION_NONE;

/**
* Underline.
*
Expand Down Expand Up @@ -273,6 +285,31 @@ public function setSubScript(bool $pValue = false): self
return $this;
}

/**
* Get Capitalization.
*/
public function getCapitalization(): string
{
return $this->capitalization;
}

/**
* Set Capitalization.
*/
public function setCapitalization(string $pValue = self::CAPITALIZATION_NONE): self
{
if (!in_array(
$pValue,
[self::CAPITALIZATION_NONE, self::CAPITALIZATION_ALL, self::CAPITALIZATION_SMALL]
)) {
throw new NotAllowedValueException($pValue, [self::CAPITALIZATION_NONE, self::CAPITALIZATION_ALL, self::CAPITALIZATION_SMALL]);
}

$this->capitalization = $pValue;

return $this;
}

/**
* Get Underline.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/PhpPresentation/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ protected function writeContent(): string
// style:style > style:text-properties
$objWriter->startElement('style:text-properties');
$objWriter->writeAttribute('fo:color', '#' . $item->getFont()->getColor()->getRGB());
switch ($item->getFont()->getCapitalization()) {
case Font::CAPITALIZATION_NONE:
$objWriter->writeAttribute('fo:text-transform', 'none');

break;
case Font::CAPITALIZATION_ALL:
$objWriter->writeAttribute('fo:text-transform', 'uppercase');

break;
case Font::CAPITALIZATION_SMALL:
$objWriter->writeAttribute('fo:text-transform', 'lowercase');

break;
}
switch ($item->getFont()->getFormat()) {
case Font::FORMAT_LATIN:
$objWriter->writeAttribute('fo:font-family', $item->getFont()->getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ protected function writeParagraphs(XMLWriter $objWriter, array $paragraphs, bool
$objWriter->writeAttribute('sz', ($element->getFont()->getSize() * 100));
$objWriter->writeAttribute('spc', $element->getFont()->getCharacterSpacing());
$objWriter->writeAttribute('u', $element->getFont()->getUnderline());
$objWriter->writeAttribute('cap', $element->getFont()->getCapitalization());
$objWriter->writeAttributeIf($element->getFont()->isSuperScript(), 'baseline', '300000');
$objWriter->writeAttributeIf($element->getFont()->isSubScript(), 'baseline', '-250000');

// Color - a:solidFill
$objWriter->startElement('a:solidFill');
$this->writeColor($objWriter, $element->getFont()->getColor());
Expand Down
1 change: 1 addition & 0 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ protected function writeTitle(XMLWriter $objWriter, Title $subject): void
$objWriter->writeAttribute('u', $subject->getFont()->getUnderline());
$objWriter->writeAttributeIf($subject->getFont()->isSuperScript(), 'baseline', '300000');
$objWriter->writeAttributeIf($subject->getFont()->isSubScript(), 'baseline', '-250000');
$objWriter->writeAttribute('cap', $subject->getFont()->getCapitalization());

// Font - a:solidFill
$objWriter->startElement('a:solidFill');
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpPresentation/Tests/Reader/ODPresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 1 : Shape 2 : Paragraph 2
$oRichText = $arrayRichText[1];
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $oRichText);
Expand All @@ -168,6 +169,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());

// Slide 2
$oSlide2 = $oPhpPresentation->getSlide(1);
Expand Down Expand Up @@ -234,6 +236,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 2 : Shape 3 : Paragraph 2
$oParagraph = $arrayParagraphs[1];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -250,6 +253,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 2 : Shape 3 : Paragraph 3
$oParagraph = $arrayParagraphs[2];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -266,6 +270,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 2 : Shape 3 : Paragraph 4
$oParagraph = $arrayParagraphs[3];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -282,6 +287,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());

// Slide 3
$oSlide2 = $oPhpPresentation->getSlide(2);
Expand Down Expand Up @@ -328,6 +334,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 2
/** @var RichText $oShape */
$oShape = $arrayShape[2];
Expand Down Expand Up @@ -355,6 +362,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 2
$oParagraph = $arrayParagraphs[1];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -372,6 +380,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 3
$oParagraph = $arrayParagraphs[2];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -389,6 +398,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 4
$oParagraph = $arrayParagraphs[3];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -406,6 +416,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 5
$oParagraph = $arrayParagraphs[4];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -423,6 +434,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 6
$oParagraph = $arrayParagraphs[5];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -440,6 +452,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 7
$oParagraph = $arrayParagraphs[6];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -457,6 +470,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 3 : Shape 3 : Paragraph 8
$oParagraph = $arrayParagraphs[7];
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
Expand All @@ -474,6 +488,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());

// Slide 4
$oSlide3 = $oPhpPresentation->getSlide(3);
Expand Down Expand Up @@ -520,6 +535,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 4 : Shape 3
/** @var RichText $oShape */
$oShape = $arrayShape[2];
Expand Down Expand Up @@ -548,6 +564,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
// Slide 4 : Shape 3 : Paragraph 2
$oRichText = $arrayRichText[1];
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $oRichText);
Expand All @@ -561,6 +578,7 @@ public function testLoadFile01(): void
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
self::assertEquals('Calibri', $oRichText->getFont()->getName());
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
self::assertTrue($oRichText->hasHyperlink());
self::assertEquals('https://github.com/PHPOffice/PHPPresentation/', $oRichText->getHyperlink()->getUrl());
//$this->assertEquals('PHPPresentation', $oRichText->getHyperlink()->getTooltip());
Expand Down
Loading
Loading