public function testOffsetX() { $object = new RichText(); $value = rand(1, 100); $this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\AbstractShape', $object->setOffsetX()); $this->assertEquals(0, $object->getOffsetX()); $this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\AbstractShape', $object->setOffsetX($value)); $this->assertEquals($value, $object->getOffsetX()); }
/** * Write text * * @param \PhpOffice\Common\XMLWriter $objWriter * @param \PhpOffice\PhpPowerpoint\Shape\RichText $shape */ public function writeShapeTxt(XMLWriter $objWriter, RichText $shape) { // draw:frame $objWriter->startElement('draw:frame'); $objWriter->writeAttribute('draw:style-name', 'gr' . $this->shapeId); $objWriter->writeAttribute('svg:width', String::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm'); $objWriter->writeAttribute('svg:height', String::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm'); $objWriter->writeAttribute('svg:x', String::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm'); $objWriter->writeAttribute('svg:y', String::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm'); // draw:text-box $objWriter->startElement('draw:text-box'); $paragraphs = $shape->getParagraphs(); $paragraphId = 0; $sCstShpLastBullet = ''; $iCstShpLastBulletLvl = 0; $bCstShpHasBullet = false; foreach ($paragraphs as $paragraph) { // Close the bullet list if ($sCstShpLastBullet != 'bullet' && $bCstShpHasBullet === true) { for ($iInc = $iCstShpLastBulletLvl; $iInc >= 0; $iInc--) { // text:list-item $objWriter->endElement(); // text:list $objWriter->endElement(); } } //=============================================== // Paragraph //=============================================== if ($paragraph->getBulletStyle()->getBulletType() == 'none') { ++$paragraphId; // text:p $objWriter->startElement('text:p'); $objWriter->writeAttribute('text:style-name', 'P_' . $paragraph->getHashCode()); // Loop trough rich text elements $richtexts = $paragraph->getRichTextElements(); $richtextId = 0; foreach ($richtexts as $richtext) { ++$richtextId; if ($richtext instanceof TextElement || $richtext instanceof Run) { // text:span $objWriter->startElement('text:span'); if ($richtext instanceof Run) { $objWriter->writeAttribute('text:style-name', 'T_' . $richtext->getFont()->getHashCode()); } if ($richtext->hasHyperlink() === true && $richtext->getHyperlink()->getUrl() != '') { // text:a $objWriter->startElement('text:a'); $objWriter->writeAttribute('xlink:href', $richtext->getHyperlink()->getUrl()); $objWriter->text($richtext->getText()); $objWriter->endElement(); } else { $objWriter->text($richtext->getText()); } $objWriter->endElement(); } elseif ($richtext instanceof BreakElement) { // text:span $objWriter->startElement('text:span'); // text:line-break $objWriter->startElement('text:line-break'); $objWriter->endElement(); $objWriter->endElement(); } else { //echo '<pre>'.print_r($richtext, true).'</pre>'; } } $objWriter->endElement(); //=============================================== // Bullet list //=============================================== } elseif ($paragraph->getBulletStyle()->getBulletType() == 'bullet') { $bCstShpHasBullet = true; // Open the bullet list if ($sCstShpLastBullet != 'bullet' || $sCstShpLastBullet == $paragraph->getBulletStyle()->getBulletType() && $iCstShpLastBulletLvl < $paragraph->getAlignment()->getLevel()) { // text:list $objWriter->startElement('text:list'); $objWriter->writeAttribute('text:style-name', 'L_' . $paragraph->getBulletStyle()->getHashCode()); } if ($sCstShpLastBullet == 'bullet') { if ($iCstShpLastBulletLvl == $paragraph->getAlignment()->getLevel()) { // text:list-item $objWriter->endElement(); } elseif ($iCstShpLastBulletLvl > $paragraph->getAlignment()->getLevel()) { // text:list-item $objWriter->endElement(); // text:list $objWriter->endElement(); // text:list-item $objWriter->endElement(); } } // text:list-item $objWriter->startElement('text:list-item'); ++$paragraphId; // text:p $objWriter->startElement('text:p'); $objWriter->writeAttribute('text:style-name', 'P_' . $paragraph->getHashCode()); // Loop trough rich text elements $richtexts = $paragraph->getRichTextElements(); $richtextId = 0; foreach ($richtexts as $richtext) { ++$richtextId; if ($richtext instanceof TextElement || $richtext instanceof Run) { // text:span $objWriter->startElement('text:span'); if ($richtext instanceof Run) { $objWriter->writeAttribute('text:style-name', 'T_' . $richtext->getFont()->getHashCode()); } if ($richtext->hasHyperlink() === true && $richtext->getHyperlink()->getUrl() != '') { // text:a $objWriter->startElement('text:a'); $objWriter->writeAttribute('xlink:href', $richtext->getHyperlink()->getUrl()); $objWriter->text($richtext->getText()); $objWriter->endElement(); } else { $objWriter->text($richtext->getText()); } $objWriter->endElement(); } elseif ($richtext instanceof BreakElement) { // text:span $objWriter->startElement('text:span'); // text:line-break $objWriter->startElement('text:line-break'); $objWriter->endElement(); $objWriter->endElement(); } else { //echo '<pre>'.print_r($richtext, true).'</pre>'; } } $objWriter->endElement(); } $sCstShpLastBullet = $paragraph->getBulletStyle()->getBulletType(); $iCstShpLastBulletLvl = $paragraph->getAlignment()->getLevel(); } // Close the bullet list if ($sCstShpLastBullet == 'bullet' && $bCstShpHasBullet === true) { for ($iInc = $iCstShpLastBulletLvl; $iInc >= 0; $iInc--) { // text:list-item $objWriter->endElement(); // text:list $objWriter->endElement(); } } // > draw:text-box $objWriter->endElement(); // > draw:frame $objWriter->endElement(); }
/** * Write txt * * @param \PhpOffice\PhpPowerpoint\Shared\XMLWriter $objWriter XML Writer * @param \PhpOffice\PhpPowerpoint\Shape\RichText $shape * @param int $shapeId * @throws \Exception */ private function writeShapeText(XMLWriter $objWriter, RichText $shape, $shapeId) { // p:sp $objWriter->startElement('p:sp'); // p:nvSpPr $objWriter->startElement('p:nvSpPr'); // p:cNvPr $objWriter->startElement('p:cNvPr'); $objWriter->writeAttribute('id', $shapeId); $objWriter->writeAttribute('name', ''); $objWriter->endElement(); // p:cNvSpPr $objWriter->startElement('p:cNvSpPr'); $objWriter->writeAttribute('txBox', '1'); $objWriter->endElement(); // p:nvPr $objWriter->writeElement('p:nvPr', null); $objWriter->endElement(); // p:spPr $objWriter->startElement('p:spPr'); // a:xfrm $objWriter->startElement('a:xfrm'); $objWriter->writeAttribute('rot', SharedDrawing::degreesToAngle($shape->getRotation())); // a:off $objWriter->startElement('a:off'); $objWriter->writeAttribute('x', SharedDrawing::pixelsToEmu($shape->getOffsetX())); $objWriter->writeAttribute('y', SharedDrawing::pixelsToEmu($shape->getOffsetY())); $objWriter->endElement(); // a:ext $objWriter->startElement('a:ext'); $objWriter->writeAttribute('cx', SharedDrawing::pixelsToEmu($shape->getWidth())); $objWriter->writeAttribute('cy', SharedDrawing::pixelsToEmu($shape->getHeight())); $objWriter->endElement(); $objWriter->endElement(); // a:prstGeom $objWriter->startElement('a:prstGeom'); $objWriter->writeAttribute('prst', 'rect'); $objWriter->endElement(); $objWriter->endElement(); // p:txBody $objWriter->startElement('p:txBody'); // a:bodyPr $objWriter->startElement('a:bodyPr'); $objWriter->writeAttribute('anchor', $shape->getActiveParagraph()->getAlignment()->getVertical()); $objWriter->writeAttribute('wrap', $shape->getWrap()); $objWriter->writeAttribute('rtlCol', '0'); $objWriter->writeAttribute('horzOverflow', $shape->getHorizontalOverflow()); $objWriter->writeAttribute('vertOverflow', $shape->getVerticalOverflow()); if ($shape->isUpright()) { $objWriter->writeAttribute('upright', '1'); } if ($shape->isVertical()) { $objWriter->writeAttribute('vert', 'vert'); } $objWriter->writeAttribute('bIns', SharedDrawing::pixelsToEmu($shape->getInsetBottom())); $objWriter->writeAttribute('lIns', SharedDrawing::pixelsToEmu($shape->getInsetLeft())); $objWriter->writeAttribute('rIns', SharedDrawing::pixelsToEmu($shape->getInsetRight())); $objWriter->writeAttribute('tIns', SharedDrawing::pixelsToEmu($shape->getInsetTop())); $objWriter->writeAttribute('numCol', $shape->getColumns()); // a:spAutoFit $objWriter->writeElement('a:' . $shape->getAutoFit(), null); $objWriter->endElement(); // a:lstStyle $objWriter->writeElement('a:lstStyle', null); // Write paragraphs $this->writeParagraphs($objWriter, $shape->getParagraphs()); $objWriter->endElement(); $objWriter->endElement(); }