$writers = array('PowerPoint2007' => 'pptx', 'ODPresentation' => 'odp'); // Return to the caller script when runs by CLI if (CLI) { return; } // Set titles and names $pageHeading = str_replace('_', ' ', SCRIPT_FILENAME); $pageTitle = IS_INDEX ? 'Welcome to ' : "{$pageHeading} - "; $pageTitle .= 'PHPPresentation'; $pageHeading = IS_INDEX ? '' : "<h1>{$pageHeading}</h1>"; $oShapeDrawing = new Drawing\File(); $oShapeDrawing->setName('PHPPresentation logo')->setDescription('PHPPresentation logo')->setPath('./resources/phppowerpoint_logo.gif')->setHeight(36)->setOffsetX(10)->setOffsetY(10); $oShapeDrawing->getShadow()->setVisible(true)->setDirection(45)->setDistance(10); $oShapeDrawing->getHyperlink()->setUrl('https://github.com/PHPOffice/PHPPresentation/')->setTooltip('PHPPresentation'); // Create a shape (text) $oShapeRichText = new RichText(); $oShapeRichText->setHeight(300)->setWidth(600)->setOffsetX(170)->setOffsetY(180); $oShapeRichText->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $textRun = $oShapeRichText->createTextRun('Thank you for using PHPPresentation!'); $textRun->getFont()->setBold(true)->setSize(60)->setColor(new Color('FFE06B20')); // Populate samples $files = ''; if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if (preg_match('/^Sample_\\d+_/', $file)) { $name = str_replace('_', ' ', preg_replace('/(Sample_|\\.php)/', '', $file)); $files .= "<li><a href='{$file}'>{$name}</a></li>"; } } closedir($handle); }
public function testHashCode() { $object = new RichText(); $hash = $object->getActiveParagraph()->getHashCode(); $hash .= RichText::WRAP_SQUARE . RichText::AUTOFIT_DEFAULT . RichText::OVERFLOW_OVERFLOW . RichText::OVERFLOW_OVERFLOW . '0014.89.69.64.8'; $hash .= md5('00000' . $object->getFill()->getHashCode() . $object->getShadow()->getHashCode() . '' . get_parent_class($object)); $hash .= get_class($object); $this->assertEquals(md5($hash), $object->getHashCode()); }
public function testPlaceholder() { $object = new RichText(); $this->assertFalse($object->isPlaceholder(), 'Standard Shape should not be a placeholder object'); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\AbstractShape', $object->setPlaceHolder(new Placeholder(Placeholder::PH_TYPE_TITLE))); $this->assertTrue($object->isPlaceholder()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Placeholder', $object->getPlaceholder()); $this->assertEquals('title', $object->getPlaceholder()->getType()); $object = new RichText(); $this->assertFalse($object->isPlaceholder(), 'Standard Shape should not be a placeholder object'); $placeholder = new Placeholder(Placeholder::PH_TYPE_TITLE); $placeholder->setType(Placeholder::PH_TYPE_SUBTITLE); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\AbstractShape', $object->setPlaceHolder($placeholder)); $this->assertTrue($object->isPlaceholder()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Placeholder', $object->getPlaceholder()); $this->assertEquals('subTitle', $object->getPlaceholder()->getType()); }
/** * Write txt * * @param \PhpOffice\Common\XMLWriter $objWriter XML Writer * @param \PhpOffice\PhpPresentation\Shape\RichText $shape * @param int $shapeId * @throws \Exception */ private function writeShapeText(XMLWriter $objWriter, RichText $shape, $shapeId) { // p:sp $objWriter->startElement('p:sp'); // p:sp\p:nvSpPr $objWriter->startElement('p:nvSpPr'); // p:sp\p:nvSpPr\p:cNvPr $objWriter->startElement('p:cNvPr'); $objWriter->writeAttribute('id', $shapeId); $objWriter->writeAttribute('name', ''); // Hyperlink if ($shape->hasHyperlink()) { $this->writeHyperlink($objWriter, $shape); } // > p:sp\p:nvSpPr $objWriter->endElement(); // p:sp\p:cNvSpPr $objWriter->startElement('p:cNvSpPr'); $objWriter->writeAttribute('txBox', '1'); $objWriter->endElement(); // p:sp\p:cNvSpPr\p:nvPr $objWriter->writeElement('p:nvPr', null); // > p:sp\p:cNvSpPr $objWriter->endElement(); // p:sp\p:spPr $objWriter->startElement('p:spPr'); // p:sp\p:spPr\a:xfrm $objWriter->startElement('a:xfrm'); $objWriter->writeAttribute('rot', CommonDrawing::degreesToAngle($shape->getRotation())); // p:sp\p:spPr\a:xfrm\a:off $objWriter->startElement('a:off'); $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX())); $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY())); $objWriter->endElement(); // p:sp\p:spPr\a:xfrm\a:ext $objWriter->startElement('a:ext'); $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth())); $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight())); $objWriter->endElement(); // > p:sp\p:spPr\a:xfrm $objWriter->endElement(); // p:sp\p:spPr\a:prstGeom $objWriter->startElement('a:prstGeom'); $objWriter->writeAttribute('prst', 'rect'); $objWriter->endElement(); if ($shape->getFill()) { $this->writeFill($objWriter, $shape->getFill()); } if ($shape->getBorder()->getLineStyle() != Border::LINE_NONE) { $this->writeBorder($objWriter, $shape->getBorder(), ''); } if ($shape->getShadow()->isVisible()) { $this->writeShadow($objWriter, $shape->getShadow()); } // > p:sp\p:spPr $objWriter->endElement(); // p:txBody $objWriter->startElement('p:txBody'); // a:bodyPr //@link :http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.bodyproperties%28v=office.14%29.aspx $objWriter->startElement('a:bodyPr'); $verticalAlign = $shape->getActiveParagraph()->getAlignment()->getVertical(); if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) { $objWriter->writeAttribute('anchor', $verticalAlign); } if ($shape->getWrap() != RichText::WRAP_SQUARE) { $objWriter->writeAttribute('wrap', $shape->getWrap()); } $objWriter->writeAttribute('rtlCol', '0'); if ($shape->getHorizontalOverflow() != RichText::OVERFLOW_OVERFLOW) { $objWriter->writeAttribute('horzOverflow', $shape->getHorizontalOverflow()); } if ($shape->getVerticalOverflow() != RichText::OVERFLOW_OVERFLOW) { $objWriter->writeAttribute('vertOverflow', $shape->getVerticalOverflow()); } if ($shape->isUpright()) { $objWriter->writeAttribute('upright', '1'); } if ($shape->isVertical()) { $objWriter->writeAttribute('vert', 'vert'); } $objWriter->writeAttribute('bIns', CommonDrawing::pixelsToEmu($shape->getInsetBottom())); $objWriter->writeAttribute('lIns', CommonDrawing::pixelsToEmu($shape->getInsetLeft())); $objWriter->writeAttribute('rIns', CommonDrawing::pixelsToEmu($shape->getInsetRight())); $objWriter->writeAttribute('tIns', CommonDrawing::pixelsToEmu($shape->getInsetTop())); if ($shape->getColumns() != 1) { $objWriter->writeAttribute('numCol', $shape->getColumns()); } // a:spAutoFit $objWriter->startElement('a:' . $shape->getAutoFit()); if ($shape->getAutoFit() == RichText::AUTOFIT_NORMAL) { if (!is_null($shape->getFontScale())) { $objWriter->writeAttribute('fontScale', (int) ($shape->getFontScale() * 1000)); } if (!is_null($shape->getLineSpaceReduction())) { $objWriter->writeAttribute('lnSpcReduction', (int) ($shape->getLineSpaceReduction() * 1000)); } } $objWriter->endElement(); $objWriter->endElement(); // a:lstStyle $objWriter->writeElement('a:lstStyle', null); // Write paragraphs $this->writeParagraphs($objWriter, $shape->getParagraphs()); $objWriter->endElement(); $objWriter->endElement(); }
/** * Write the default style information for a RichText shape * * @param XMLWriter $objWriter * @param RichText $shape */ protected function writeRichTextStyle(XMLWriter $objWriter, RichText $shape) { $oFill = $shape->getFill(); if ($oFill->getFillType() == Fill::FILL_GRADIENT_LINEAR || $oFill->getFillType() == Fill::FILL_GRADIENT_PATH) { if (!in_array($oFill->getHashCode(), $this->arrayGradient)) { $this->writeGradientFill($objWriter, $oFill); } } $oBorder = $shape->getBorder(); if ($oBorder->getDashStyle() != Border::DASH_SOLID) { if (!in_array($oBorder->getDashStyle(), $this->arrayStrokeDash)) { $objWriter->startElement('draw:stroke-dash'); $objWriter->writeAttribute('draw:name', 'strokeDash_' . $oBorder->getDashStyle()); $objWriter->writeAttribute('draw:style', 'rect'); switch ($oBorder->getDashStyle()) { case Border::DASH_DASH: $objWriter->writeAttribute('draw:distance', '0.105cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.14cm'); break; case Border::DASH_DASHDOT: $objWriter->writeAttribute('draw:distance', '0.105cm'); $objWriter->writeAttribute('draw:dots1', '1'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.14cm'); break; case Border::DASH_DOT: $objWriter->writeAttribute('draw:distance', '0.105cm'); $objWriter->writeAttribute('draw:dots1', '1'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); break; case Border::DASH_LARGEDASH: $objWriter->writeAttribute('draw:distance', '0.105cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.28cm'); break; case Border::DASH_LARGEDASHDOT: $objWriter->writeAttribute('draw:distance', '0.105cm'); $objWriter->writeAttribute('draw:dots1', '1'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.28cm'); break; case Border::DASH_LARGEDASHDOTDOT: $objWriter->writeAttribute('draw:distance', '0.105cm'); $objWriter->writeAttribute('draw:dots1', '2'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.28cm'); break; case Border::DASH_SYSDASH: $objWriter->writeAttribute('draw:distance', '0.035cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.105cm'); break; case Border::DASH_SYSDASHDOT: $objWriter->writeAttribute('draw:distance', '0.035cm'); $objWriter->writeAttribute('draw:dots1', '1'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.105cm'); break; case Border::DASH_SYSDASHDOTDOT: $objWriter->writeAttribute('draw:distance', '0.035cm'); $objWriter->writeAttribute('draw:dots1', '2'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); $objWriter->writeAttribute('draw:dots2', '1'); $objWriter->writeAttribute('draw:dots2-length', '0.105cm'); break; case Border::DASH_SYSDOT: $objWriter->writeAttribute('draw:distance', '0.035cm'); $objWriter->writeAttribute('draw:dots1', '1'); $objWriter->writeAttribute('draw:dots1-length', '0.035cm'); break; } $objWriter->endElement(); $this->arrayStrokeDash[] = $oBorder->getDashStyle(); } } }
/** * Read List Item * @param RichText $oShape * @param \DOMElement $oNodeParent * @param \DOMElement $oNodeParagraph */ protected function readListItem(RichText $oShape, \DOMElement $oNodeParent, \DOMElement $oNodeParagraph) { $oParagraph = $oShape->createParagraph(); if ($oNodeParagraph->hasAttribute('text:style-name')) { $keyStyle = $oNodeParagraph->getAttribute('text:style-name'); if (isset($this->arrayStyles[$keyStyle])) { $oParagraph->setAlignment($this->arrayStyles[$keyStyle]['listStyle'][$this->levelParagraph]['alignment']); $oParagraph->setBulletStyle($this->arrayStyles[$keyStyle]['listStyle'][$this->levelParagraph]['bullet']); } } foreach ($this->oXMLReader->getElements('text:span', $oNodeParent) as $oNodeRichTextElement) { $this->readParagraphItem($oParagraph, $oNodeRichTextElement); } }
/** * Write the default style information for a RichText shape * * @param \PhpOffice\Common\XMLWriter $objWriter * @param \PhpOffice\PhpPresentation\Shape\RichText $shape */ public function writeTxtStyle(XMLWriter $objWriter, RichText $shape) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'gr' . $this->shapeId); $objWriter->writeAttribute('style:family', 'graphic'); $objWriter->writeAttribute('style:parent-style-name', 'standard'); // style:graphic-properties $objWriter->startElement('style:graphic-properties'); if ($shape->getShadow()->isVisible()) { $this->writeStylePartShadow($objWriter, $shape->getShadow()); } if (is_bool($shape->hasAutoShrinkVertical())) { $objWriter->writeAttribute('draw:auto-grow-height', var_export($shape->hasAutoShrinkVertical(), true)); } if (is_bool($shape->hasAutoShrinkHorizontal())) { $objWriter->writeAttribute('draw:auto-grow-width', var_export($shape->hasAutoShrinkHorizontal(), true)); } // Fill switch ($shape->getFill()->getFillType()) { case Fill::FILL_GRADIENT_LINEAR: case Fill::FILL_GRADIENT_PATH: $objWriter->writeAttribute('draw:fill', 'gradient'); $objWriter->writeAttribute('draw:fill-gradient-name', 'gradient_' . $shape->getFill()->getHashCode()); break; case Fill::FILL_SOLID: $objWriter->writeAttribute('draw:fill', 'solid'); $objWriter->writeAttribute('draw:fill-color', '#' . $shape->getFill()->getStartColor()->getRGB()); break; case Fill::FILL_NONE: default: $objWriter->writeAttribute('draw:fill', 'none'); $objWriter->writeAttribute('draw:fill-color', '#' . $shape->getFill()->getStartColor()->getRGB()); break; } // Border if ($shape->getBorder()->getLineStyle() == Border::LINE_NONE) { $objWriter->writeAttribute('draw:stroke', 'none'); } else { $objWriter->writeAttribute('svg:stroke-color', '#' . $shape->getBorder()->getColor()->getRGB()); $objWriter->writeAttribute('svg:stroke-width', number_format(CommonDrawing::pointsToCentimeters($shape->getBorder()->getLineWidth()), 3, '.', '') . 'cm'); switch ($shape->getBorder()->getDashStyle()) { case Border::DASH_SOLID: $objWriter->writeAttribute('draw:stroke', 'solid'); break; case Border::DASH_DASH: case Border::DASH_DASHDOT: case Border::DASH_DOT: case Border::DASH_LARGEDASH: case Border::DASH_LARGEDASHDOT: case Border::DASH_LARGEDASHDOTDOT: case Border::DASH_SYSDASH: case Border::DASH_SYSDASHDOT: case Border::DASH_SYSDASHDOTDOT: case Border::DASH_SYSDOT: $objWriter->writeAttribute('draw:stroke', 'dash'); $objWriter->writeAttribute('draw:stroke-dash', 'strokeDash_' . $shape->getBorder()->getDashStyle()); break; default: $objWriter->writeAttribute('draw:stroke', 'none'); break; } } $objWriter->writeAttribute('fo:wrap-option', 'wrap'); // > style:graphic-properties $objWriter->endElement(); // > style:style $objWriter->endElement(); $paragraphs = $shape->getParagraphs(); $paragraphId = 0; foreach ($paragraphs as $paragraph) { ++$paragraphId; // Style des paragraphes if (!isset($this->arrStyleParagraph[$paragraph->getHashCode()])) { $this->arrStyleParagraph[$paragraph->getHashCode()] = $paragraph; } // Style des listes if (!isset($this->arrStyleBullet[$paragraph->getBulletStyle()->getHashCode()])) { $this->arrStyleBullet[$paragraph->getBulletStyle()->getHashCode()]['oStyle'] = $paragraph->getBulletStyle(); $this->arrStyleBullet[$paragraph->getBulletStyle()->getHashCode()]['level'] = ''; } if (strpos($this->arrStyleBullet[$paragraph->getBulletStyle()->getHashCode()]['level'], ';' . $paragraph->getAlignment()->getLevel()) === false) { $this->arrStyleBullet[$paragraph->getBulletStyle()->getHashCode()]['level'] .= ';' . $paragraph->getAlignment()->getLevel(); $this->arrStyleBullet[$paragraph->getBulletStyle()->getHashCode()]['oAlign_' . $paragraph->getAlignment()->getLevel()] = $paragraph->getAlignment(); } $richtexts = $paragraph->getRichTextElements(); $richtextId = 0; foreach ($richtexts as $richtext) { ++$richtextId; // Not a line break if ($richtext instanceof Run) { // Style des font text if (!isset($this->arrStyleTextFont[$richtext->getFont()->getHashCode()])) { $this->arrStyleTextFont[$richtext->getFont()->getHashCode()] = $richtext->getFont(); } } } } }
public function testWidthAndHeight() { $object = new RichText(); $value = rand(1, 100); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\AbstractShape', $object->setWidthAndHeight()); $this->assertEquals(0, $object->getWidth()); $this->assertEquals(0, $object->getHeight()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\AbstractShape', $object->setWidthAndHeight($value)); $this->assertEquals($value, $object->getWidth()); $this->assertEquals(0, $object->getHeight()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\AbstractShape', $object->setWidthAndHeight($value, $value)); $this->assertEquals($value, $object->getWidth()); $this->assertEquals($value, $object->getHeight()); }