Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.Trashes
Thumbs.db
Desktop.ini
.idea/

### Continuous Integration
build/
Expand Down
1 change: 1 addition & 0 deletions docs/changes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- PowerPoint2007 Reader : Support for BarChart by [@Progi1984](https://github.com/Progi1984) fixing [#824](https://github.com/PHPOffice/PHPPresentation/pull/824) in [#856](https://github.com/PHPOffice/PHPPresentation/pull/856)
- `phpoffice/phpspreadsheet`: Allow version 4.0 by [@nreynis](https://github.com/nreynis) in [#861](https://github.com/PHPOffice/PHPPresentation/pull/861)
- Smaller package size by [@nreynis](https://github.com/nreynis) in [#862](https://github.com/PHPOffice/PHPPresentation/pull/862)
- Added setFirstSliceAngle to Doughnut Chart by [@seanlynchwv](http://github.com/seanlynchwv) in [#872](https://github.com/PHPOffice/PHPPresentation/pull/872)

## Bug fixes

Expand Down
27 changes: 27 additions & 0 deletions src/PhpPresentation/Shape/AutoShape.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

/**
Expand Down Expand Up @@ -273,4 +273,31 @@

return $this;
}

private ?int $roundRectAdj = null;

public function getRoundRectAdj(): ?int
{
return $this->roundRectAdj;
}

/**
* Set corner radius in pixels. We map px to the OOXML 'adj' 0..50000 scale.
* adj is relative to (min(width,height)/2).
*/
public function setRoundRectCorner(int $px): self
{
$minHalf = (int) floor(min($this->width, $this->height) / 2);
if ($minHalf > 0) {
$this->roundRectAdj = max(0, min(50000, (int) round($px / $minHalf * 50000)));
}
return $this;
}

// override the hash so radius works
public function getHashCode(): string
{
// parent::getHashCode() already includes geometry/fill/shadow, etc.
return md5(parent::getHashCode() . $this->type . $this->text . (string) $this->roundRectAdj . __CLASS__);
}
}
19 changes: 19 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Doughnut.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Doughnut extends AbstractTypePie implements ComparableInterface
*/
protected $holeSize = 50;

/**
* Starting angle of the first slice.
*
* @var int
*/
private $firstSliceAngle = 0;

/**
* @return int
*/
Expand Down Expand Up @@ -71,4 +78,16 @@ public function getHashCode(): string
{
return md5(parent::getHashCode() . __CLASS__);
}

public function setFirstSliceAngle(int $angle): self
{
$this->firstSliceAngle = (($angle % 360) + 360) % 360;

return $this;
}

public function getFirstSliceAngle(): ?int
{
return $this->firstSliceAngle;
}
}
18 changes: 15 additions & 3 deletions src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

/**
Expand Down Expand Up @@ -27,7 +27,7 @@
use PhpOffice\PhpPresentation\AbstractShape;
use PhpOffice\PhpPresentation\Exception\UndefinedChartTypeException;
use PhpOffice\PhpPresentation\Shape\AbstractGraphic;
use PhpOffice\PhpPresentation\Shape\AutoShape;

Check failure on line 30 in src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

ParseError (syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST)) thrown while looking for class PhpOffice\PhpPresentation\Shape\AutoShape.
use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart;
use PhpOffice\PhpPresentation\Shape\Comment;
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
Expand Down Expand Up @@ -1187,9 +1187,21 @@
// p:sp\p:spPr\a:prstGeom
$objWriter->startElement('a:prstGeom');
$objWriter->writeAttribute('prst', $shape->getType());
// p:sp\p:spPr\a:prstGeom\a:avLst
$objWriter->writeElement('a:avLst');
// p:sp\p:spPr\a:prstGeom\

// a:avLst (+ optional adj for roundRect)
$needsAdj = ($shape->getType() === \PhpOffice\PhpPresentation\Shape\AutoShape::TYPE_ROUNDED_RECTANGLE);
$adj = $shape->getRoundRectAdj();

if ($needsAdj && $adj !== null) {
$objWriter->startElement('a:avLst');
$objWriter->startElement('a:gd');
$objWriter->writeAttribute('name', 'adj');
$objWriter->writeAttribute('fmla', 'val ' . (string) $adj); // 0..50000
$objWriter->endElement(); // a:gd
$objWriter->endElement(); // a:avLst
} else {
$objWriter->writeElement('a:avLst');
}
$objWriter->endElement();
// Fill
$this->writeFill($objWriter, $shape->getFill());
Expand Down
11 changes: 8 additions & 3 deletions src/PhpPresentation/Writer/PowerPoint2007/ContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function render(): ZipInterface
// XML
$this->writeDefaultContentType($objWriter, 'xml', 'application/xml');

// SVG
$this->writeDefaultContentType($objWriter, 'svg', 'image/svg+xml');
// SVG will pre-register it in $aMediaContentTypes

// Presentation
$this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml');
Expand Down Expand Up @@ -106,11 +105,12 @@ public function render(): ZipInterface
// Add media content-types
$aMediaContentTypes = [];

// GIF, JPEG, PNG
// GIF, JPEG, PNG, SVG
$aMediaContentTypes['gif'] = 'image/gif';
$aMediaContentTypes['jpg'] = 'image/jpeg';
$aMediaContentTypes['jpeg'] = 'image/jpeg';
$aMediaContentTypes['png'] = 'image/png';
$aMediaContentTypes['svg'] = 'image/svg+xml';
foreach ($aMediaContentTypes as $key => $value) {
$this->writeDefaultContentType($objWriter, $key, $value);
}
Expand All @@ -133,6 +133,11 @@ public function render(): ZipInterface
$extension = strtolower($shapeIndex->getExtension());
$mimeType = $shapeIndex->getMimeType();

// Normalize any odd returns (some environments report "image/svg")
if ($extension === 'svg') {
$mimeType = 'image/svg+xml';
}

if (!isset($aMediaContentTypes[$extension])) {
$aMediaContentTypes[$extension] = $mimeType;

Expand Down
57 changes: 31 additions & 26 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,38 +115,40 @@ protected function writeChart(Chart $chart): string
$objWriter->endElement();

// c:view3D
$objWriter->startElement('c:view3D');
if ($chart->getPlotArea()->getType() instanceof Bar3D || $chart->getPlotArea()->getType() instanceof Pie3D) {
$objWriter->startElement('c:view3D');

// c:rotX
$objWriter->startElement('c:rotX');
$objWriter->writeAttribute('val', $chart->getView3D()->getRotationX());
$objWriter->endElement();
// c:rotX
$objWriter->startElement('c:rotX');
$objWriter->writeAttribute('val', $chart->getView3D()->getRotationX());
$objWriter->endElement();

// c:hPercent
$hPercent = $chart->getView3D()->getHeightPercent();
$objWriter->writeElementIf(null != $hPercent, 'c:hPercent', 'val', $hPercent);
// c:hPercent
$hPercent = $chart->getView3D()->getHeightPercent();
$objWriter->writeElementIf(null != $hPercent, 'c:hPercent', 'val', $hPercent);

// c:rotY
$objWriter->startElement('c:rotY');
$objWriter->writeAttribute('val', $chart->getView3D()->getRotationY());
$objWriter->endElement();
// c:rotY
$objWriter->startElement('c:rotY');
$objWriter->writeAttribute('val', $chart->getView3D()->getRotationY());
$objWriter->endElement();

// c:depthPercent
$objWriter->startElement('c:depthPercent');
$objWriter->writeAttribute('val', $chart->getView3D()->getDepthPercent());
$objWriter->endElement();
// c:depthPercent
$objWriter->startElement('c:depthPercent');
$objWriter->writeAttribute('val', $chart->getView3D()->getDepthPercent());
$objWriter->endElement();

// c:rAngAx
$objWriter->startElement('c:rAngAx');
$objWriter->writeAttribute('val', $chart->getView3D()->hasRightAngleAxes() ? '1' : '0');
$objWriter->endElement();
// c:rAngAx
$objWriter->startElement('c:rAngAx');
$objWriter->writeAttribute('val', $chart->getView3D()->hasRightAngleAxes() ? '1' : '0');
$objWriter->endElement();

// c:perspective
$objWriter->startElement('c:perspective');
$objWriter->writeAttribute('val', $chart->getView3D()->getPerspective());
$objWriter->endElement();
// c:perspective
$objWriter->startElement('c:perspective');
$objWriter->writeAttribute('val', $chart->getView3D()->getPerspective());
$objWriter->endElement();

$objWriter->endElement();
$objWriter->endElement();
}

// Write plot area
$this->writePlotArea($objWriter, $chart->getPlotArea(), $chart);
Expand Down Expand Up @@ -1352,7 +1354,10 @@ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bo
$objWriter->endElement();
}

$this->writeElementWithValAttribute($objWriter, 'c:firstSliceAng', '0');
if (($angle = $subject->getFirstSliceAngle()) !== null) {
$this->writeElementWithValAttribute($objWriter, 'c:firstSliceAng', (string) $angle);
}

$this->writeElementWithValAttribute($objWriter, 'c:holeSize', (string) $subject->getHoleSize());

$objWriter->endElement();
Expand Down
80 changes: 80 additions & 0 deletions tests/PhpPresentation/Tests/Shape/AutoShapeTest.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

/**
Expand All @@ -20,9 +20,14 @@

namespace PhpOffice\PhpPresentation\Tests\Shape;

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\AutoShape;

Check failure on line 24 in tests/PhpPresentation/Tests/Shape/AutoShapeTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

ParseError (syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST)) thrown while looking for class PhpOffice\PhpPresentation\Shape\AutoShape.
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Fill;
use PhpOffice\PhpPresentation\Style\Outline;
use PhpOffice\PhpPresentation\Writer\PowerPoint2007;
use PHPUnit\Framework\TestCase;
use ZipArchive;

class AutoShapeTest extends TestCase
{
Expand Down Expand Up @@ -64,4 +69,79 @@
self::assertInstanceOf(AutoShape::class, $object->setType(AutoShape::TYPE_HEXAGON));
self::assertEquals(AutoShape::TYPE_HEXAGON, $object->getType());
}

public function testPixelSetterComputesAdjAndAffectsHash(): void
{
$w = 200; // px
$h = 100; // px
$px1 = 5; // softer radius
$px2 = 10; // larger radius

$s1 = (new AutoShape())
->setType(AutoShape::TYPE_ROUNDED_RECTANGLE)
->setWidth($w)->setHeight($h)
->setRoundRectCorner($px1);

$s2 = (clone $s1)->setRoundRectCorner($px2);

// adj expected: round(px / (min(w,h)/2) * 50000)
$minHalf = (int) floor(min($w, $h) / 2); // 50
$expectedAdj1 = (int) round($px1 / $minHalf * 50000); // 5/50 * 50000 = 5000
$expectedAdj2 = (int) round($px2 / $minHalf * 50000); // 10/50 * 50000 = 10000

self::assertSame($expectedAdj1, $s1->getRoundRectAdj());
self::assertSame($expectedAdj2, $s2->getRoundRectAdj());

// Hash must differ when radius differs
self::assertNotSame($s1->getHashCode(), $s2->getHashCode());
}

public function testNoRadiusByDefaultIsNull(): void
{
$shape = new AutoShape();
self::assertNull($shape->getRoundRectAdj());
}

public function testWriterEmitsAdjGuideForRoundRect(): void
{
$ppt = new PhpPresentation();
$slide = $ppt->getActiveSlide();

$width = 200;
$height = 100;
$padding = 5;
$minHalf = (int) floor(min($width, $height) / 2);
$expectedAdj = (int) round($padding / $minHalf * 50000); // 5000

$shape = (new AutoShape())
->setType(AutoShape::TYPE_ROUNDED_RECTANGLE)
->setWidth($width)->setHeight($height)
->setRoundRectCorner($padding);

// Give it a fill so it's an obvious shape
$shape->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFFFFFFF'));
$slide->addShape($shape);

$tmpFile = tempnam(sys_get_temp_dir(), 'pptx_');
$writer = new PowerPoint2007($ppt);
$writer->save($tmpFile);

// Open the pptx and read slide1.xml
$zip = new ZipArchive();
$this->assertTrue($zip->open($tmpFile) === true, 'Failed to open pptx zip');
$xml = $zip->getFromName('ppt/slides/slide1.xml');
$zip->close();
@unlink($tmpFile);

$this->assertIsString($xml);

// Must contain roundRect geometry and the adj guide with expected value
$this->assertStringContainsString('<a:prstGeom prst="roundRect">', $xml);

// fmla="val N" (there is a space after 'val' in writer)
$this->assertMatchesRegularExpression(
sprintf('/<a:gd[^>]+name="adj"[^>]+fmla="val %d"/', $expectedAdj),
$xml
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1809,20 +1809,38 @@ public function testTypeScatterSuperScript(): void
public function testView3D(): void
{
$oSlide = $this->oPresentation->getActiveSlide();
$oLine = new Line();
$oLine->addSeries(new Series('Downloads', $this->seriesData));
$oShape = $oSlide->createChartShape();
$oShape->getPlotArea()->setType($oLine);
$shape3d = $oSlide->createChartShape();
$shape3d->getPlotArea()->setType(new Bar3D());

$element = '/c:chartSpace/c:chart/c:view3D/c:hPercent';
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', '100');
$this->assertZipXmlElementExists('ppt/charts/' . $shape3d->getIndexedFilename(), $element);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $shape3d->getIndexedFilename(), $element, 'val', '100');
$this->assertIsSchemaECMA376Valid();

$oShape->getView3D()->setHeightPercent(null);
$shape3d->getView3D()->setHeightPercent(null);
$this->resetPresentationFile();

$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
$this->assertZipXmlElementNotExists('ppt/charts/' . $shape3d->getIndexedFilename(), $element);
$this->assertIsSchemaECMA376Valid();
}

public function testDoughnutFirstSliceAngle(): void
{
$slide = $this->oPresentation->getActiveSlide();
$shape = $slide->createChartShape();

$doughnut = new Doughnut();
$doughnut->setFirstSliceAngle(90);
$doughnut->addSeries(new Series('Downloads', $this->seriesData));

$shape->getPlotArea()->setType($doughnut);

$path = 'ppt/charts/' . $shape->getIndexedFilename();
$elt = '/c:chartSpace/c:chart/c:plotArea/c:doughnutChart/c:firstSliceAng';

$this->assertZipXmlElementExists($path, $elt);
$this->assertZipXmlAttributeEquals($path, $elt, 'val', '90');

$this->assertIsSchemaECMA376Valid();
}

Expand Down
Loading