Skip to content

Commit

Permalink
#279 : ODPresentation Writer : Support for rotation for RichText
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jul 25, 2021
1 parent 6c46ff2 commit 10e4ec4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
20 changes: 20 additions & 0 deletions samples/Sample_11_Shape.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ function fnSlideRichTextLineSpacing(PhpPresentation $objPHPPresentation)
$shape->createTextRun('Line Spacing 300');
}

function fnSlideRichTextRotation(PhpPresentation $objPHPPresentation)
{
// Create templated slide
echo date('H:i:s') . ' Create templated slide' . EOL;
$currentSlide = createTemplatedSlide($objPHPPresentation);

// Create a shape (text)
echo date('H:i:s') . ' Create a shape (rich text) with rotation' . EOL;
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(100);
$shape->setWidth(400);
$shape->setOffsetX(100);
$shape->setOffsetY(100);
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
$shape->setRotation(90);

$shape->createTextRun('RichText with rotation');
}

function fnSlideRichTextShadow(PhpPresentation $objPHPPresentation)
{
// Create templated slide
Expand Down Expand Up @@ -143,6 +162,7 @@ function fnSlideRichTextList(PhpPresentation $objPHPPresentation)

fnSlideRichText($objPHPPresentation);
fnSlideRichTextLineSpacing($objPHPPresentation);
fnSlideRichTextRotation($objPHPPresentation);
fnSlideRichTextShadow($objPHPPresentation);
fnSlideRichTextList($objPHPPresentation);

Expand Down
14 changes: 11 additions & 3 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@
if (preg_match('/^Sample_\d+_/', $file)) {
$name = str_replace('_', ' ', preg_replace('/(Sample_|\.php)/', '', $file));
$group = substr($name, 0, 1);
$id = substr($name, 0, 2);
if (!isset($files[$group])) {
$files[$group] = '';
$files[$group] = array();
}
$files[$group] .= "<li><a href='{$file}'>{$name}</a></li>";
if (!isset($files[$group][$id])) {
$files[$group][$id] = '';
}
$files[$group][$id] .= "<li><a href='{$file}'>{$name}</a></li>";
ksort($files[$group]);
}
}
closedir($handle);
foreach ($files as $keyGroup => $arrayGroup) {
$files[$keyGroup] = implode('', $arrayGroup);
}
}

/**
Expand Down Expand Up @@ -183,7 +191,7 @@ function createTemplatedSlide(PhpOffice\PhpPresentation\PhpPresentation $objPHPP
$shape = $slide->createDrawingShape();
$shape->setName('PHPPresentation logo')
->setDescription('PHPPresentation logo')
->setPath('./resources/phppowerpoint_logo.gif')
->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif')
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
Expand Down
46 changes: 46 additions & 0 deletions src/PhpPresentation/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ public function writeShapeTxt(XMLWriter $objWriter, RichText $shape): void
$objWriter->writeAttribute('svg:height', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm');
$objWriter->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm');
$objWriter->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm');
if ($shape->getRotation() != 0) {
$rotRad = deg2rad($shape->getRotation());

$translateX = Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getWidth() / 2), 3). 'cm';
$translateY = Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetX() / 2), 3). 'cm';
$objWriter->writeAttribute(
'draw:transform',
'rotate (-'.$rotRad.')' .
'translate (' .
$translateX.
'-' . $translateY
.')'
);
}
// draw:text-box
$objWriter->startElement('draw:text-box');

Expand Down Expand Up @@ -593,6 +607,38 @@ public function writeShapeTxt(XMLWriter $objWriter, RichText $shape): void
}
}

if ($shape->getRotation() != 0) {
$objWriter->startElement('draw:enhanced-geometry');
$objWriter->writeAttribute('draw:mirror-horizontal', 'false');
$objWriter->writeAttribute('draw:mirror-vertical', 'false');
$objWriter->writeAttribute('svg:viewBox', '0 0 0 0');
$objWriter->writeAttribute('draw:text-areas', '0 0 ?f3 ?f2');
$objWriter->writeAttribute('draw:type', 'ooxml-rect');
$objWriter->writeAttribute('draw:enhanced-path', 'M 0 0 L ?f3 0 ?f3 ?f2 0 ?f2 Z N');

$objWriter->startElement('draw:equation');
$objWriter->writeAttribute('draw:name', 'f0');
$objWriter->writeAttribute('draw:formula', 'logwidth/2');
$objWriter->endElement();

$objWriter->startElement('draw:equation');
$objWriter->writeAttribute('draw:name', 'f1');
$objWriter->writeAttribute('draw:formula', 'logheight/2');
$objWriter->endElement();

$objWriter->startElement('draw:equation');
$objWriter->writeAttribute('draw:name', 'f2');
$objWriter->writeAttribute('draw:formula', 'logheight');
$objWriter->endElement();

$objWriter->startElement('draw:equation');
$objWriter->writeAttribute('draw:name', 'f3');
$objWriter->writeAttribute('draw:formula', 'logwidth');
$objWriter->endElement();

$objWriter->endElement();
}

// > draw:text-box
$objWriter->endElement();
// > draw:frame
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpPresentation/Tests/Writer/ODPresentation/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ public function testRichTextBorder(): void
$this->assertIsSchemaOpenDocumentValid('1.2');
}

public function testRichTextRotation(): void
{
$expectedValue = rand(1, 360);
$oRichText1 = $this->oPresentation->getActiveSlide()->createRichTextShape();
$oRichText1->setRotation($expectedValue);

$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame';
$this->assertZipXmlElementExists('content.xml', $element);
$this->assertZipXmlAttributeExists('content.xml', $element, 'draw:transform');
$this->assertZipXmlAttributeEquals('content.xml', $element, 'draw:transform', 'rotate ('.deg2rad(360 - $expectedValue).')');
}

public function testRichTextShadow(): void
{
$randAlpha = mt_rand(0, 100);
Expand Down

0 comments on commit 10e4ec4

Please sign in to comment.