Skip to content

Commit e6e6a0a

Browse files
authored
Merge pull request #779 from Progi1984/pr719
Support for Capitalization for Font
2 parents d9cc4dd + aa8cd69 commit e6e6a0a

File tree

14 files changed

+253
-1
lines changed

14 files changed

+253
-1
lines changed

docs/changes/1.1.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
- PowerPoint2007 Writer
1212
- Added support for PHP 8.2 & 8.3 - [@Progi1984](https://github.com/Progi1984) in [#769](https://github.com/PHPOffice/PHPPresentation/pull/769)
1313
- 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)
14+
- Support for Capitalization for Font - [@CxRxExO](https://github.com/CxRxExO) & [@Progi1984](https://github.com/Progi1984) in [#779](https://github.com/PHPOffice/PHPPresentation/pull/779)
15+
- ODPresentation Reader
16+
- ODPresentation Writer
17+
- PowerPoint2007 Reader
18+
- PowerPoint2007 Writer
1419

1520
## Improvements
1621
- Slide : Raised max value for identifier rand call - [@Scheissy](https://github.com/Scheissy) in [#777](https://github.com/PHPOffice/PHPPresentation/pull/777)

docs/usage/styles.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ echo $alignment->isRTL();
102102
- `underline`
103103
- `strikethrough`
104104
- `color`
105+
- `capitalization`
106+
107+
### Capitalization
108+
109+
Some formats are available :
110+
111+
* `Font::CAPITALIZATION_NONE`
112+
* `Font::CAPITALIZATION_ALL`
113+
* `Font::CAPITALIZATION_SMALL`
114+
115+
``` php
116+
<?php
117+
118+
use PhpOffice\PhpPresentation\Style\Font;
119+
120+
$font = new Font();
121+
122+
// Set capitalization of font
123+
$font->setCapitalization(Font::CAPITALIZATION_ALL);
124+
// Get capitalization of font
125+
echo $font->getCapitalization();
126+
```
105127

106128
### Format
107129

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* This file is part of PHPPresentation - A pure PHP library for reading and writing
4+
* presentations documents.
5+
*
6+
* PHPPresentation is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPPresentation
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace PhpOffice\PhpPresentation\Exception;
21+
22+
class NotAllowedValueException extends PhpPresentationException
23+
{
24+
/**
25+
* @param array<string> $value
26+
*/
27+
public function __construct(string $parameter, array $value)
28+
{
29+
parent::__construct(sprintf(
30+
'The value "%s" is not in allowed values ("%s")',
31+
$parameter,
32+
implode('", "', $value)
33+
));
34+
}
35+
}

src/PhpPresentation/Reader/ODPresentation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,22 @@ protected function loadStyle(DOMElement $nodeStyle): bool
332332
if ($nodeTextProperties->hasAttribute('fo:color')) {
333333
$oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6));
334334
}
335+
if ($nodeTextProperties->hasAttribute('fo:text-transform')) {
336+
switch ($nodeTextProperties->getAttribute('fo:text-transform')) {
337+
case 'none':
338+
$oFont->setCapitalization(Font::CAPITALIZATION_NONE);
339+
340+
break;
341+
case 'lowercase':
342+
$oFont->setCapitalization(Font::CAPITALIZATION_SMALL);
343+
344+
break;
345+
case 'uppercase':
346+
$oFont->setCapitalization(Font::CAPITALIZATION_ALL);
347+
348+
break;
349+
}
350+
}
335351
// Font Latin
336352
if ($nodeTextProperties->hasAttribute('fo:font-family')) {
337353
$oFont

src/PhpPresentation/Reader/PowerPoint2007.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,9 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh
12061206
if ($oElementrPr->hasAttribute('u')) {
12071207
$oText->getFont()->setUnderline($oElementrPr->getAttribute('u'));
12081208
}
1209+
if ($oElementrPr->hasAttribute('cap')) {
1210+
$oText->getFont()->setCapitalization($oElementrPr->getAttribute('cap'));
1211+
}
12091212
// Color
12101213
$oElementSrgbClr = $document->getElement('a:solidFill/a:srgbClr', $oElementrPr);
12111214
if (is_object($oElementSrgbClr) && $oElementSrgbClr->hasAttribute('val')) {

src/PhpPresentation/Style/Font.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace PhpOffice\PhpPresentation\Style;
2121

2222
use PhpOffice\PhpPresentation\ComparableInterface;
23+
use PhpOffice\PhpPresentation\Exception\NotAllowedValueException;
2324

2425
/**
2526
* \PhpOffice\PhpPresentation\Style\Font.
@@ -50,6 +51,10 @@ class Font implements ComparableInterface
5051
public const FORMAT_EAST_ASIAN = 'ea';
5152
public const FORMAT_COMPLEX_SCRIPT = 'cs';
5253

54+
public const CAPITALIZATION_NONE = 'none';
55+
public const CAPITALIZATION_SMALL = 'small';
56+
public const CAPITALIZATION_ALL = 'all';
57+
5358
/**
5459
* Name.
5560
*
@@ -92,6 +97,13 @@ class Font implements ComparableInterface
9297
*/
9398
private $subScript = false;
9499

100+
/**
101+
* Capitalization.
102+
*
103+
* @var string
104+
*/
105+
private $capitalization = self::CAPITALIZATION_NONE;
106+
95107
/**
96108
* Underline.
97109
*
@@ -273,6 +285,31 @@ public function setSubScript(bool $pValue = false): self
273285
return $this;
274286
}
275287

288+
/**
289+
* Get Capitalization.
290+
*/
291+
public function getCapitalization(): string
292+
{
293+
return $this->capitalization;
294+
}
295+
296+
/**
297+
* Set Capitalization.
298+
*/
299+
public function setCapitalization(string $pValue = self::CAPITALIZATION_NONE): self
300+
{
301+
if (!in_array(
302+
$pValue,
303+
[self::CAPITALIZATION_NONE, self::CAPITALIZATION_ALL, self::CAPITALIZATION_SMALL]
304+
)) {
305+
throw new NotAllowedValueException($pValue, [self::CAPITALIZATION_NONE, self::CAPITALIZATION_ALL, self::CAPITALIZATION_SMALL]);
306+
}
307+
308+
$this->capitalization = $pValue;
309+
310+
return $this;
311+
}
312+
276313
/**
277314
* Get Underline.
278315
*/

src/PhpPresentation/Writer/ODPresentation/Content.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,20 @@ protected function writeContent(): string
276276
// style:style > style:text-properties
277277
$objWriter->startElement('style:text-properties');
278278
$objWriter->writeAttribute('fo:color', '#' . $item->getFont()->getColor()->getRGB());
279+
switch ($item->getFont()->getCapitalization()) {
280+
case Font::CAPITALIZATION_NONE:
281+
$objWriter->writeAttribute('fo:text-transform', 'none');
282+
283+
break;
284+
case Font::CAPITALIZATION_ALL:
285+
$objWriter->writeAttribute('fo:text-transform', 'uppercase');
286+
287+
break;
288+
case Font::CAPITALIZATION_SMALL:
289+
$objWriter->writeAttribute('fo:text-transform', 'lowercase');
290+
291+
break;
292+
}
279293
switch ($item->getFont()->getFormat()) {
280294
case Font::FORMAT_LATIN:
281295
$objWriter->writeAttribute('fo:font-family', $item->getFont()->getName());

src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ protected function writeParagraphs(XMLWriter $objWriter, array $paragraphs, bool
605605
$objWriter->writeAttribute('sz', ($element->getFont()->getSize() * 100));
606606
$objWriter->writeAttribute('spc', $element->getFont()->getCharacterSpacing());
607607
$objWriter->writeAttribute('u', $element->getFont()->getUnderline());
608+
$objWriter->writeAttribute('cap', $element->getFont()->getCapitalization());
608609
$objWriter->writeAttributeIf($element->getFont()->isSuperScript(), 'baseline', '300000');
609610
$objWriter->writeAttributeIf($element->getFont()->isSubScript(), 'baseline', '-250000');
610-
611611
// Color - a:solidFill
612612
$objWriter->startElement('a:solidFill');
613613
$this->writeColor($objWriter, $element->getFont()->getColor());

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ protected function writeTitle(XMLWriter $objWriter, Title $subject): void
452452
$objWriter->writeAttribute('u', $subject->getFont()->getUnderline());
453453
$objWriter->writeAttributeIf($subject->getFont()->isSuperScript(), 'baseline', '300000');
454454
$objWriter->writeAttributeIf($subject->getFont()->isSubScript(), 'baseline', '-250000');
455+
$objWriter->writeAttribute('cap', $subject->getFont()->getCapitalization());
455456

456457
// Font - a:solidFill
457458
$objWriter->startElement('a:solidFill');

tests/PhpPresentation/Tests/Reader/ODPresentationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public function testLoadFile01(): void
156156
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
157157
self::assertEquals('Calibri', $oRichText->getFont()->getName());
158158
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
159+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
159160
// Slide 1 : Shape 2 : Paragraph 2
160161
$oRichText = $arrayRichText[1];
161162
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $oRichText);
@@ -168,6 +169,7 @@ public function testLoadFile01(): void
168169
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
169170
self::assertEquals('Calibri', $oRichText->getFont()->getName());
170171
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
172+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
171173

172174
// Slide 2
173175
$oSlide2 = $oPhpPresentation->getSlide(1);
@@ -234,6 +236,7 @@ public function testLoadFile01(): void
234236
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
235237
self::assertEquals('Calibri', $oRichText->getFont()->getName());
236238
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
239+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
237240
// Slide 2 : Shape 3 : Paragraph 2
238241
$oParagraph = $arrayParagraphs[1];
239242
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -250,6 +253,7 @@ public function testLoadFile01(): void
250253
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
251254
self::assertEquals('Calibri', $oRichText->getFont()->getName());
252255
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
256+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
253257
// Slide 2 : Shape 3 : Paragraph 3
254258
$oParagraph = $arrayParagraphs[2];
255259
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -266,6 +270,7 @@ public function testLoadFile01(): void
266270
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
267271
self::assertEquals('Calibri', $oRichText->getFont()->getName());
268272
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
273+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
269274
// Slide 2 : Shape 3 : Paragraph 4
270275
$oParagraph = $arrayParagraphs[3];
271276
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -282,6 +287,7 @@ public function testLoadFile01(): void
282287
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
283288
self::assertEquals('Calibri', $oRichText->getFont()->getName());
284289
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
290+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
285291

286292
// Slide 3
287293
$oSlide2 = $oPhpPresentation->getSlide(2);
@@ -328,6 +334,7 @@ public function testLoadFile01(): void
328334
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
329335
self::assertEquals('Calibri', $oRichText->getFont()->getName());
330336
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
337+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
331338
// Slide 3 : Shape 2
332339
/** @var RichText $oShape */
333340
$oShape = $arrayShape[2];
@@ -355,6 +362,7 @@ public function testLoadFile01(): void
355362
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
356363
self::assertEquals('Calibri', $oRichText->getFont()->getName());
357364
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
365+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
358366
// Slide 3 : Shape 3 : Paragraph 2
359367
$oParagraph = $arrayParagraphs[1];
360368
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -372,6 +380,7 @@ public function testLoadFile01(): void
372380
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
373381
self::assertEquals('Calibri', $oRichText->getFont()->getName());
374382
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
383+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
375384
// Slide 3 : Shape 3 : Paragraph 3
376385
$oParagraph = $arrayParagraphs[2];
377386
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -389,6 +398,7 @@ public function testLoadFile01(): void
389398
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
390399
self::assertEquals('Calibri', $oRichText->getFont()->getName());
391400
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
401+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
392402
// Slide 3 : Shape 3 : Paragraph 4
393403
$oParagraph = $arrayParagraphs[3];
394404
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -406,6 +416,7 @@ public function testLoadFile01(): void
406416
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
407417
self::assertEquals('Calibri', $oRichText->getFont()->getName());
408418
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
419+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
409420
// Slide 3 : Shape 3 : Paragraph 5
410421
$oParagraph = $arrayParagraphs[4];
411422
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -423,6 +434,7 @@ public function testLoadFile01(): void
423434
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
424435
self::assertEquals('Calibri', $oRichText->getFont()->getName());
425436
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
437+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
426438
// Slide 3 : Shape 3 : Paragraph 6
427439
$oParagraph = $arrayParagraphs[5];
428440
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -440,6 +452,7 @@ public function testLoadFile01(): void
440452
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
441453
self::assertEquals('Calibri', $oRichText->getFont()->getName());
442454
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
455+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
443456
// Slide 3 : Shape 3 : Paragraph 7
444457
$oParagraph = $arrayParagraphs[6];
445458
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -457,6 +470,7 @@ public function testLoadFile01(): void
457470
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
458471
self::assertEquals('Calibri', $oRichText->getFont()->getName());
459472
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
473+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
460474
// Slide 3 : Shape 3 : Paragraph 8
461475
$oParagraph = $arrayParagraphs[7];
462476
self::assertEquals(Alignment::HORIZONTAL_LEFT, $oParagraph->getAlignment()->getHorizontal());
@@ -474,6 +488,7 @@ public function testLoadFile01(): void
474488
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
475489
self::assertEquals('Calibri', $oRichText->getFont()->getName());
476490
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
491+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
477492

478493
// Slide 4
479494
$oSlide3 = $oPhpPresentation->getSlide(3);
@@ -520,6 +535,7 @@ public function testLoadFile01(): void
520535
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
521536
self::assertEquals('Calibri', $oRichText->getFont()->getName());
522537
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
538+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
523539
// Slide 4 : Shape 3
524540
/** @var RichText $oShape */
525541
$oShape = $arrayShape[2];
@@ -548,6 +564,7 @@ public function testLoadFile01(): void
548564
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
549565
self::assertEquals('Calibri', $oRichText->getFont()->getName());
550566
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
567+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
551568
// Slide 4 : Shape 3 : Paragraph 2
552569
$oRichText = $arrayRichText[1];
553570
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $oRichText);
@@ -561,6 +578,7 @@ public function testLoadFile01(): void
561578
self::assertEquals('FF000000', $oRichText->getFont()->getColor()->getARGB());
562579
self::assertEquals('Calibri', $oRichText->getFont()->getName());
563580
self::assertEquals(Font::FORMAT_LATIN, $oRichText->getFont()->getFormat());
581+
self::assertEquals(Font::CAPITALIZATION_NONE, $oRichText->getFont()->getCapitalization());
564582
self::assertTrue($oRichText->hasHyperlink());
565583
self::assertEquals('https://github.com/PHPOffice/PHPPresentation/', $oRichText->getHyperlink()->getUrl());
566584
//$this->assertEquals('PHPPresentation', $oRichText->getHyperlink()->getTooltip());

0 commit comments

Comments
 (0)