-
Notifications
You must be signed in to change notification settings - Fork 536
Set Rounded Corners on Rectangle #880
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
base: master
Are you sure you want to change the base?
Changes from all commits
e7e0df3
f816c7c
e2260e2
7d64ebe
084d472
edec62f
1e3a0e5
23d5eff
9a38b44
010a2a5
a48a8a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,4 +273,31 @@ public function setOutline(Outline $outline): self | |
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** @var null|int */ | ||
| protected $roundRectAdj; | ||
|
|
||
| public function getRoundRectAdj(): ?int | ||
| { | ||
| return $this->roundRectAdj; | ||
| } | ||
|
|
||
| /** | ||
| * Set corner radius. | ||
| */ | ||
| public function setRoundRectCorner(int $pixels): self | ||
| { | ||
| $minHalf = (int) floor(min($this->width, $this->height) / 2); | ||
| if ($minHalf > 0) { | ||
| $this->roundRectAdj = max(0, min(50000, (int) round($pixels / $minHalf * 50000))); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does come from 50000 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must move this calcul on the writer side, not on the autoshape class. |
||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| // override the hash so radius works | ||
| public function getHashCode(): string | ||
| { | ||
| return md5(parent::getHashCode() . $this->type . $this->text . (string) $this->roundRectAdj . __CLASS__); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,9 +20,14 @@ | |
|
|
||
| namespace PhpOffice\PhpPresentation\Tests\Shape; | ||
|
|
||
| use PhpOffice\PhpPresentation\PhpPresentation; | ||
| use 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 | ||
| { | ||
|
|
@@ -64,4 +69,83 @@ public function testType(): void | |
| self::assertInstanceOf(AutoShape::class, $object->setType(AutoShape::TYPE_HEXAGON)); | ||
| self::assertEquals(AutoShape::TYPE_HEXAGON, $object->getType()); | ||
| } | ||
|
|
||
| public function testPixelSetterComputesAdjAndAffectsHash(): void | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| $width = 200; // px | ||
| $height = 100; // px | ||
| $px1 = 5; // softer radius | ||
| $px2 = 10; // larger radius | ||
|
|
||
| $shape1 = (new AutoShape()) | ||
| ->setType(AutoShape::TYPE_ROUNDED_RECTANGLE) | ||
| ->setWidth($width) | ||
| ->setHeight($height) | ||
| ->setRoundRectCorner($px1); | ||
|
|
||
| $shape2 = (clone $shape1)->setRoundRectCorner($px2); | ||
|
|
||
| // adj expected: round(px / (min(w,h)/2) * 50000) | ||
| $minHalf = (int) floor(min($width, $height) / 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, $shape1->getRoundRectAdj()); | ||
| self::assertSame($expectedAdj2, $shape2->getRoundRectAdj()); | ||
|
|
||
| // Hash must differ when radius differs | ||
| self::assertNotSame($shape1->getHashCode(), $shape2->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(); | ||
| self::assertTrue($zip->open($tmpFile) === true, 'Failed to open pptx zip'); | ||
| $xml = $zip->getFromName('ppt/slides/slide1.xml'); | ||
| $zip->close(); | ||
| @unlink($tmpFile); | ||
|
|
||
| self::assertIsString($xml); | ||
|
|
||
| // Must contain roundRect geometry and the adj guide with expected value | ||
| self::assertStringContainsString('<a:prstGeom prst="roundRect">', $xml); | ||
|
|
||
| // fmla="val N" (there is a space after 'val' in writer) | ||
| self::assertSame( | ||
| 1, | ||
| preg_match( | ||
| sprintf('/<a:gd[^>]+name="adj"[^>]+fmla="val %d"/', $expectedAdj), | ||
| (string) $xml | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seanlynchwv Could you move your change to 1.3.0.md, please ?