diff --git a/docs/changes/1.1.0.md b/docs/changes/1.1.0.md index eb19d3b37..45c2e2671 100644 --- a/docs/changes/1.1.0.md +++ b/docs/changes/1.1.0.md @@ -10,6 +10,7 @@ - PowerPoint2007 Reader - 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) ## Bugfixes diff --git a/docs/usage/shapes/chart.md b/docs/usage/shapes/chart.md index 636f33315..af5ca4024 100644 --- a/docs/usage/shapes/chart.md +++ b/docs/usage/shapes/chart.md @@ -165,7 +165,8 @@ $shape->getPlotArea()->setType($line); $shape->getPlotArea()->getAxisX()->getOutline()->setWidth(10); $shape->getPlotArea()->getAxisX()->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE)); ``` -#### Tick Label Position +#### Tick Label +##### Position You can define the tick label position with the `setTickLabelPosition` method. For resetting it, you pass `Axis::TICK_LABEL_POSITION_NEXT_TO` as parameter to this method. @@ -187,6 +188,28 @@ $shape = $slide->createChartShape(); $shape->getPlotArea()->setType($line); $shape->getPlotArea()->getAxisY()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_LOW); ``` + +##### Font + +You can define the tick label font with the `setTickLabelFont` method. +For resetting it, you pass `null` as parameter to this method. + +``` php +setColor(new Color('C00000')) + +$shape = $slide->createChartShape(); +$shape->getPlotArea()->setType($line); +$shape->getPlotArea()->getAxisY()->setTickLabelFont($font); +``` + #### Tick Marks For Axis Y, you can define tick mark with `setMinorTickMark` & `setMajorTickMark` methods. diff --git a/src/PhpPresentation/Shape/Chart/Axis.php b/src/PhpPresentation/Shape/Chart/Axis.php index 671b67ba3..8576a0cff 100644 --- a/src/PhpPresentation/Shape/Chart/Axis.php +++ b/src/PhpPresentation/Shape/Chart/Axis.php @@ -69,6 +69,13 @@ class Axis implements ComparableInterface */ private $font; + /** + * Tick lable font. + * + * @var Font + */ + protected $tickLabelFont; + /** * @var null|Gridlines */ @@ -144,6 +151,7 @@ public function __construct(string $title = 'Axis Title') $this->title = $title; $this->outline = new Outline(); $this->font = new Font(); + $this->tickLabelFont = new Font(); } /** @@ -172,6 +180,24 @@ public function getFont(): ?Font return $this->font; } + /** + * Set tick label font. + */ + public function setTickLabelFont(?Font $font = null): self + { + $this->tickLabelFont = $font; + + return $this; + } + + /** + * Get tick label font. + */ + public function getTickLabelFont(): ?Font + { + return $this->tickLabelFont; + } + /** * Set font. */ diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index c65081ed7..97f505c37 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -883,7 +883,6 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ // a:defRPr $objWriter->startElement('a:defRPr'); - $objWriter->writeAttribute('b', ($series->getFont()->isBold() ? 'true' : 'false')); $objWriter->writeAttribute('i', ($series->getFont()->isItalic() ? 'true' : 'false')); $objWriter->writeAttribute('strike', ($series->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike')); @@ -895,12 +894,16 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ // a:solidFill $objWriter->startElement('a:solidFill'); $this->writeColor($objWriter, $series->getFont()->getColor()); - // >a:solidFill $objWriter->endElement(); + // a:latin $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); - // >a:latin + $objWriter->endElement(); + + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); // >a:defRPr @@ -1106,6 +1109,10 @@ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $in $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -1309,6 +1316,10 @@ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bo $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); // c:dLbls\c:txPr\a:p\a:pPr\a:defRPr\ $objWriter->endElement(); @@ -1450,6 +1461,10 @@ protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includ $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -1610,6 +1625,10 @@ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $in $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -1759,6 +1778,10 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -1931,6 +1954,10 @@ protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $in $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -2093,6 +2120,10 @@ protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $series->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $series->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -2366,6 +2397,10 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty $objWriter->startElement('a:latin'); $objWriter->writeAttribute('typeface', $oAxis->getFont()->getName()); $objWriter->endElement(); + // a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $oAxis->getFont()->getName()); + $objWriter->endElement(); $objWriter->endElement(); @@ -2429,9 +2464,65 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty // c:spPr $objWriter->startElement('c:spPr'); - // Outline $this->writeOutline($objWriter, $oAxis->getOutline()); - // ##c:spPr + $objWriter->endElement(); + + // c:txPr + $objWriter->startElement('c:txPr'); + + // a:bodyPr + $objWriter->writeElement('a:bodyPr', null); + + // a:lstStyle + $objWriter->writeElement('a:lstStyle', null); + + // a:p + $objWriter->startElement('a:p'); + + // a:pPr + $objWriter->startElement('a:pPr'); + + // a:defRPr + $objWriter->startElement('a:defRPr'); + $objWriter->writeAttribute('b', ($oAxis->getTickLabelFont()->isBold() ? 'true' : 'false')); + $objWriter->writeAttribute('i', ($oAxis->getTickLabelFont()->isItalic() ? 'true' : 'false')); + $objWriter->writeAttribute('strike', ($oAxis->getTickLabelFont()->isStrikethrough() ? 'sngStrike' : 'noStrike')); + $objWriter->writeAttribute('sz', ($oAxis->getTickLabelFont()->getSize() * 100)); + $objWriter->writeAttribute('u', $oAxis->getTickLabelFont()->getUnderline()); + $objWriter->writeAttributeIf($oAxis->getTickLabelFont()->isSuperScript(), 'baseline', '300000'); + $objWriter->writeAttributeIf($oAxis->getTickLabelFont()->isSubScript(), 'baseline', '-250000'); + + // Font - a:solidFill + $objWriter->startElement('a:solidFill'); + $this->writeColor($objWriter, $oAxis->getTickLabelFont()->getColor()); + $objWriter->endElement(); + + // Font - a:latin + $objWriter->startElement('a:latin'); + $objWriter->writeAttribute('typeface', $oAxis->getTickLabelFont()->getName()); + $objWriter->endElement(); + + // Font - a:ea + $objWriter->startElement('a:ea'); + $objWriter->writeAttribute('typeface', $oAxis->getTickLabelFont()->getName()); + $objWriter->endElement(); + + //## a:defRPr + $objWriter->endElement(); + + //## a:pPr + $objWriter->endElement(); + + // a:endParaRPr + $objWriter->startElement('a:endParaRPr'); + $objWriter->writeAttribute('lang', 'en-US'); + $objWriter->writeAttribute('dirty', '0'); + $objWriter->endElement(); + + // ## a:p + $objWriter->endElement(); + + // ## c:txPr $objWriter->endElement(); // c:crossAx @@ -2462,7 +2553,7 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty $objWriter->endElement(); // c:majorUnit - if ($oAxis->getMajorUnit() != null) { + if ($oAxis->getMajorUnit() !== null) { $objWriter->startElement('c:tickLblSkip'); $objWriter->writeAttribute('val', $oAxis->getMajorUnit()); $objWriter->endElement(); @@ -2482,14 +2573,14 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty $objWriter->endElement(); // c:majorUnit - if (null != $oAxis->getMajorUnit()) { + if ($oAxis->getMajorUnit() !== null) { $objWriter->startElement('c:majorUnit'); $objWriter->writeAttribute('val', $oAxis->getMajorUnit()); $objWriter->endElement(); } // c:minorUnit - if (null != $oAxis->getMinorUnit()) { + if ($oAxis->getMinorUnit() !== null) { $objWriter->startElement('c:minorUnit'); $objWriter->writeAttribute('val', $oAxis->getMinorUnit()); $objWriter->endElement(); diff --git a/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php b/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php index 0e481404d..59b360fe9 100644 --- a/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php +++ b/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php @@ -156,6 +156,17 @@ public function testOutline(): void self::assertInstanceOf(Outline::class, $object->getOutline()); } + public function testTickLabelFont(): void + { + $object = new Axis(); + + self::assertInstanceOf(Font::class, $object->getTickLabelFont()); + self::assertInstanceOf(Axis::class, $object->setTickLabelFont()); + self::assertNull($object->getTickLabelFont()); + self::assertInstanceOf(Axis::class, $object->setTickLabelFont(new Font())); + self::assertInstanceOf(Font::class, $object->getTickLabelFont()); + } + public function testTickLabelPosition(): void { $object = new Axis(); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index 56a2d2e33..e4ddde5b0 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -448,6 +448,14 @@ public function testAxisFont(): void $this->assertZipXmlAttributeEquals($pathShape, $element, 'strike', 'sngStrike'); $this->assertZipXmlAttributeEquals($pathShape, $element, 'u', Font::UNDERLINE_NONE); + $element = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:title/c:tx/c:rich/a:p/a:pPr/a:defRPr/a:latin'; + $this->assertZipXmlElementExists($pathShape, $element); + $this->assertZipXmlAttributeEquals($pathShape, $element, 'typeface', 'Calibri'); + + $element = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:title/c:tx/c:rich/a:p/a:pPr/a:defRPr/a:ea'; + $this->assertZipXmlElementExists($pathShape, $element); + $this->assertZipXmlAttributeEquals($pathShape, $element, 'typeface', 'Calibri'); + $element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:title/c:tx/c:rich/a:p/a:pPr/a:defRPr/a:solidFill/a:srgbClr'; $this->assertZipXmlElementExists($pathShape, $element); $this->assertZipXmlAttributeEquals($pathShape, $element, 'val', '00FF00'); @@ -459,6 +467,14 @@ public function testAxisFont(): void $this->assertZipXmlAttributeEquals($pathShape, $element, 'strike', 'noStrike'); $this->assertZipXmlAttributeEquals($pathShape, $element, 'u', Font::UNDERLINE_DASH); + $element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:title/c:tx/c:rich/a:p/a:pPr/a:defRPr/a:latin'; + $this->assertZipXmlElementExists($pathShape, $element); + $this->assertZipXmlAttributeEquals($pathShape, $element, 'typeface', 'Calibri'); + + $element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:title/c:tx/c:rich/a:p/a:pPr/a:defRPr/a:ea'; + $this->assertZipXmlElementExists($pathShape, $element); + $this->assertZipXmlAttributeEquals($pathShape, $element, 'typeface', 'Calibri'); + $this->assertIsSchemaECMA376Valid(); } @@ -502,6 +518,26 @@ public function testAxisOutline(): void $this->assertIsSchemaECMA376Valid(); } + public function testAxisTickLabelFont(): void + { + $oLine = new Line(); + $oLine->addSeries(new Series('Downloads', $this->seriesData)); + $oShape = $this->oPresentation->getActiveSlide()->createChartShape(); + $oShape->getPlotArea()->setType($oLine); + $oShape->getPlotArea()->getAxisY()->getTickLabelFont()->setName('RandomFont'); + + $pathShape = 'ppt/charts/' . $oShape->getIndexedFilename(); + $element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:txPr/a:p/a:pPr/a:defRPr/a:latin'; + $this->assertZipXmlElementExists($pathShape, $element); + $this->assertZipXmlAttributeEquals($pathShape, $element, 'typeface', 'RandomFont'); + + $element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:txPr/a:p/a:pPr/a:defRPr/a:ea'; + $this->assertZipXmlElementExists($pathShape, $element); + $this->assertZipXmlAttributeEquals($pathShape, $element, 'typeface', 'RandomFont'); + + $this->assertIsSchemaECMA376Valid(); + } + public function testAxisTickLabelPosition(): void { $element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:tickLblPos'; @@ -716,6 +752,52 @@ public function testAxisYUnit(): void $this->assertIsSchemaECMA376Valid(); } + /** + * @dataProvider dataProviderFont + */ + public function testSeriesFont(string $chartType, string $chartElementName): void + { + $oSlide = $this->oPresentation->getActiveSlide(); + $oShape = $oSlide->createChartShape(); + $oShape->setResizeProportional(false)->setHeight(550)->setWidth(700)->setOffsetX(120)->setOffsetY(80); + /** @var AbstractType $oChart */ + $oChart = new $chartType(); + $oChart->addSeries(new Series('Downloads', $this->seriesData)); + $oShape->getPlotArea()->setType($oChart); + + $this->assertZipFileExists('ppt/charts/' . $oShape->getIndexedFilename()); + + $element = '/p:sld/p:cSld/p:spTree/p:graphicFrame/a:graphic/a:graphicData'; + $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); + + $chartBaseXmlPath = sprintf( + '/c:chartSpace/c:chart/c:plotArea/%s%s', + $chartElementName, + $chartType === Doughnut::class ? '' : '/c:ser' + ); + $element = $chartBaseXmlPath; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $element = $chartBaseXmlPath . '/c:dLbls'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $element = $chartBaseXmlPath . '/c:dLbls/c:txPr'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $element = $chartBaseXmlPath . '/c:dLbls/c:txPr/a:p'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $element = $chartBaseXmlPath . '/c:dLbls/c:txPr/a:p/a:pPr'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $element = $chartBaseXmlPath . '/c:dLbls/c:txPr/a:p/a:pPr/a:defRPr'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $element = $chartBaseXmlPath . '/c:dLbls/c:txPr/a:p/a:pPr/a:defRPr/a:latin'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'typeface', 'Calibri'); + $element = $chartBaseXmlPath . '/c:dLbls/c:txPr/a:p/a:pPr/a:defRPr/a:ea'; + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'typeface', 'Calibri'); + $this->assertIsSchemaECMA376Valid(); + + $this->assertIsSchemaECMA376Valid(); + } + public function testTypeArea(): void { $oSlide = $this->oPresentation->getActiveSlide(); @@ -1641,6 +1723,25 @@ public static function dataProviderMarkerSymbol(): iterable } } + /** + * @return array> + */ + public function dataProviderFont(): iterable + { + foreach ([ + [Bar::class, 'c:barChart'], + [Bar3D::class, 'c:bar3DChart'], + [Doughnut::class, 'c:doughnutChart'], + [Pie::class, 'c:pieChart'], + [Pie3D::class, 'c:pie3DChart'], + [Line::class, 'c:lineChart'], + [Radar::class, 'c:radarChart'], + [Scatter::class, 'c:scatterChart'], + ] as $chartType) { + yield $chartType; + } + } + /** * @return array> */