Skip to content

Commit

Permalink
Merge pull request #139 from Progi1984/develop
Browse files Browse the repository at this point in the history
#122 : Set default zoom value for presentation
  • Loading branch information
Progi1984 committed Oct 30, 2015
2 parents f3a1e1e + b2afa86 commit 1a6006a
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features
- ODPresentation Reader/Writer : Name of the slide - @Progi1984 GH-121
- PowerPoint2007 Reader/Writer : Mark as final - @Progi1984 GH-118
- PowerPoint2007 Reader/Writer : Set default zoom value for presentation - @Progi1984 GH-122

## 0.5.0 - 2015-10-08

Expand Down
21 changes: 21 additions & 0 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
Recipes
=======

How to define the zoom of a presentation ?
------------------------------------------

You must define the zoom of your presentation with the method ``setZoom()``

.. code-block:: php
// Default
$zoom = $oPHPPresentation->getZoom();
// $zoom = 1
// Without parameter
$oPHPPresentation->setZoom();
$zoom = $oPHPPresentation->getZoom();
// $zoom = true
// Parameter = false
$oPHPPresentation->setZoom(2.8);
$zoom = $oPHPPresentation->getZoom();
// $zoom = 2.8
How to mark a presentation as final ?
-------------------------------------

Expand Down
51 changes: 51 additions & 0 deletions samples/Sample_14_Zoom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

include_once 'Sample_Header.php';

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Color;

// Create new PHPPresentation object
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
$objPHPPresentation = new PhpPresentation();

// Set the zoom to 200%
$objPHPPresentation->setZoom(3);

// Create slide
echo date('H:i:s') . ' Create slide'.EOL;
$currentSlide = $objPHPPresentation->getActiveSlide();

// Create a shape (drawing)
echo date('H:i:s') . ' Create a shape (drawing)'.EOL;
$shape = $currentSlide->createDrawingShape();
$shape->setName('PHPPresentation logo')
->setDescription('PHPPresentation logo')
->setPath('./resources/phppowerpoint_logo.gif')
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
$shape->getShadow()->setVisible(true)
->setDirection(45)
->setDistance(10);
$shape->getHyperlink()->setUrl('https:/PHPOffice/PHPPresentation/')->setTooltip('PHPPresentation');

// Create a shape (text)
echo date('H:i:s') . ' Create a shape (rich text)'.EOL;
$shape = $currentSlide->createRichTextShape()
->setHeight(300)
->setWidth(600)
->setOffsetX(170)
->setOffsetY(180);
$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
$textRun = $shape->createTextRun('Thank you for using PHPPresentation!');
$textRun->getFont()->setBold(true)
->setSize(60)
->setColor( new Color( 'FFE06B20' ) );

// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
28 changes: 28 additions & 0 deletions src/PhpPresentation/PhpPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class PhpPresentation
*/
private $markAsFinal = false;

/**
* Zoom
* @var float
*/
private $zoom = 1;

/**
* Create a new PhpPresentation with one Slide
*/
Expand Down Expand Up @@ -319,4 +325,26 @@ public function isMarkedAsFinal()
{
return $this->markAsFinal;
}

/**
* Set the zoom of the document (in percentage)
* @param float $zoom
* @return PhpPresentation
*/
public function setZoom($zoom = 1)
{
if (is_numeric($zoom)) {
$this->zoom = $zoom;
}
return $this;
}

/**
* Return the zoom (in percentage)
* @return float
*/
public function getZoom()
{
return $this->zoom;
}
}
24 changes: 22 additions & 2 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ protected function loadFile($pFilename)
if ($docPropsCustom !== false) {
$this->loadCustomProperties($docPropsCustom);
}

$pptViewProps = $this->oZip->getFromName('ppt/viewProps.xml');
if ($pptViewProps !== false) {
$this->loadViewProperties($pptViewProps);
}

$pptPresentation = $this->oZip->getFromName('ppt/presentation.xml');
if ($pptPresentation !== false) {
Expand Down Expand Up @@ -166,8 +171,6 @@ protected function loadDocumentProperties($sPart)
}
}



/**
* Read Custom Properties
* @param string $sPart
Expand All @@ -185,6 +188,23 @@ protected function loadCustomProperties($sPart)
}
}
}

/**
* Read View Properties
* @param string $sPart
*/
protected function loadViewProperties($sPart)
{
$xmlReader = new XMLReader();
if ($xmlReader->getDomFromString($sPart)) {
$pathZoom = '/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx';
if (is_object($oElement = $xmlReader->getElement($pathZoom))) {
if ($oElement->hasAttribute('d') && $oElement->hasAttribute('n')) {
$this->oPhpPresentation->setZoom($oElement->getAttribute('n') / $oElement->getAttribute('d'));
}
}
}
}

/**
* Extract all slides
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Writer/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function save($pFilename)
// Add PPT properties and styles to ZIP file - Required for Apple Keynote compatibility.
$objZip->addFromString('ppt/presProps.xml', $wPptProps->writePresProps());
$objZip->addFromString('ppt/tableStyles.xml', $wPptProps->writeTableStyles());
$objZip->addFromString('ppt/viewProps.xml', $wPptProps->writeViewProps());
$objZip->addFromString('ppt/viewProps.xml', $wPptProps->writeViewProps($this->presentation));

// Add relationships to ZIP file
$objZip->addFromString('_rels/.rels', $wPartRels->writeRelationships());
Expand Down
43 changes: 41 additions & 2 deletions src/PhpPresentation/Writer/PowerPoint2007/PptProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function writeTableStyles()
* @return string XML Output
* @throws \Exception
*/
public function writeViewProps()
public function writeViewProps(PhpPresentation $oPhpPresentation)
{
// Create XML writer
$objWriter = $this->getXMLWriter();
Expand All @@ -120,7 +120,46 @@ public function writeViewProps()
$objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
$objWriter->writeAttribute('showComments', '0');

// > p:viewPr
// p:viewPr > p:slideViewPr
$objWriter->startElement('p:slideViewPr');

// p:viewPr > p:slideViewPr > p:cSldViewPr
$objWriter->startElement('p:cSldViewPr');

// p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr
$objWriter->startElement('p:cViewPr');

// p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr > p:scale
$objWriter->startElement('p:scale');

$objWriter->startElement('a:sx');
$objWriter->writeAttribute('d', '100');
$objWriter->writeAttribute('n', (int)($oPhpPresentation->getZoom() * 100));
$objWriter->endElement();

$objWriter->startElement('a:sy');
$objWriter->writeAttribute('d', '100');
$objWriter->writeAttribute('n', (int)($oPhpPresentation->getZoom() * 100));
$objWriter->endElement();

// > // p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr > p:scale
$objWriter->endElement();

$objWriter->startElement('p:origin');
$objWriter->writeAttribute('x', '0');
$objWriter->writeAttribute('y', '0');
$objWriter->endElement();

// > // p:viewPr > p:slideViewPr > p:cSldViewPr > p:cViewPr
$objWriter->endElement();

// > // p:viewPr > p:slideViewPr > p:cSldViewPr
$objWriter->endElement();

// > // p:viewPr > p:slideViewPr
$objWriter->endElement();

// > // p:viewPr
$objWriter->endElement();

return $objWriter->getData();
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpPresentation/Tests/PhpPowerpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,16 @@ public function testSetActiveSlideIndexException()
$object = new PhpPresentation();
$object->setActiveSlideIndex(1);
}

public function testZoom()
{
$object = new PhpPresentation();
$this->assertEquals(1, $object->getZoom());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setZoom('AAAA'));
$this->assertEquals(1, $object->getZoom());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setZoom(2.3));
$this->assertEquals(2.3, $object->getZoom());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setZoom());
$this->assertEquals(1, $object->getZoom());
}
}
16 changes: 16 additions & 0 deletions tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,20 @@ public function testMarkAsFinal()
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
$this->assertTrue($oPhpPresentation->isMarkedAsFinal());
}

public function testZoom()
{
$file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/Sample_12.pptx';
$object = new PowerPoint2007();
$oPhpPresentation = $object->load($file);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
$this->assertEquals(1, $oPhpPresentation->getZoom());


$file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/PPTX_Zoom.pptx';
$object = new PowerPoint2007();
$oPhpPresentation = $object->load($file);
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $oPhpPresentation);
$this->assertEquals(2.68, $oPhpPresentation->getZoom());
}
}
24 changes: 24 additions & 0 deletions tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,28 @@ public function testMarkAsFinal()
$this->assertFalse($pres->elementExists('/Properties/property[@name="_MarkAsFinal"]', 'docProps/custom.xml'));
$this->assertFalse($pres->elementExists('/cp:coreProperties/cp:contentStatus', 'docProps/core.xml'));
}

public function testZoom()
{
$oPhpPresentation = new PhpPresentation();

$pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007');
$this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'ppt/viewProps.xml'));
$this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'n', 'ppt/viewProps.xml'));
$this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'd', 'ppt/viewProps.xml'));
$this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'ppt/viewProps.xml'));
$this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'n', 'ppt/viewProps.xml'));
$this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'd', 'ppt/viewProps.xml'));

$value = rand(1, 100);
$oPhpPresentation->setZoom($value);

$pres = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007');
$this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'ppt/viewProps.xml'));
$this->assertEquals($value * 100, $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'n', 'ppt/viewProps.xml'));
$this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx', 'd', 'ppt/viewProps.xml'));
$this->assertTrue($pres->elementExists('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'ppt/viewProps.xml'));
$this->assertEquals($value * 100, $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'n', 'ppt/viewProps.xml'));
$this->assertEquals('100', $pres->getElementAttribute('/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sy', 'd', 'ppt/viewProps.xml'));
}
}
Binary file added tests/resources/files/PPTX_Zoom.pptx
Binary file not shown.

0 comments on commit 1a6006a

Please sign in to comment.