/** * @test * @dataProvider offsetProvider */ public function drawingAfterEvaluating($offset) { $pageMock = $this->getMock('PHPPdf\\Core\\Node\\Page', array('getContext')); $contextMock = $this->getMock('PHPPdf\\Core\\Node\\PageContext', array('getPageNumber'), array(5, new DynamicPage())); $pageMock->expects($this->atLeastOnce())->method('getContext')->will($this->returnValue($contextMock)); $pageNumber = 5; $contextMock->expects($this->atLeastOnce())->method('getPageNumber')->will($this->returnValue($pageNumber)); $format = 'abc%s.'; $this->node->setAttribute('format', $format); $this->node->setAttribute('offset', $offset); $this->node->setParent($pageMock); $linePart = $this->getMockBuilder('PHPPdf\\Core\\Node\\Paragraph\\LinePart')->setMethods(array('setWords', 'collectOrderedDrawingTasks'))->disableOriginalConstructor()->getMock(); $expectedText = sprintf($format, $pageNumber + $offset); $linePart->expects($this->at(0))->method('setWords')->with($expectedText); $document = $this->createDocumentStub(); $drawingTaskStub = new DrawingTask(function () { }); $tasks = new DrawingTaskHeap(); $linePart->expects($this->at(1))->method('collectOrderedDrawingTasks')->with($this->isInstanceOf('PHPPdf\\Core\\Document'), $this->isInstanceOf('PHPPdf\\Core\\DrawingTaskHeap'))->will($this->returnCallback(function () use($tasks, $drawingTaskStub) { $tasks->insert($drawingTaskStub); })); $this->node->addLinePart($linePart); $this->node->evaluate(); $this->node->collectOrderedDrawingTasks($this->createDocumentStub(), $tasks); $this->assertEquals(1, count($tasks)); $this->assertEquals($expectedText, $this->node->getText()); }
public function collectOrderedDrawingTasks(Document $document, DrawingTaskHeap $tasks) { $tasks->insert(new DrawingTask(function (Text $text, $point, $words, $width, $document, $linePartWordSpacing, Point $translation) { $gc = $text->getGraphicsContext(); $gc->saveGS(); $fontSize = $text->getFontSizeRecursively(); $font = $text->getFont($document); $gc->setFont($font, $fontSize); $color = $text->getRecurseAttribute('color'); if ($color) { $gc->setFillColor($color); } $alpha = $text->getAlpha(); if ($alpha !== null) { $gc->setAlpha($alpha); } $rotationNode = $text->getAncestorWithRotation(); if ($rotationNode) { $middlePoint = $rotationNode->getMiddlePoint(); $gc->rotate($middlePoint->getX(), $middlePoint->getY(), $rotationNode->getRotate()); } if (!$translation->isZero()) { $point = $point->translate($translation->getX(), $translation->getY()); } $yCoord = $point->getY() - $fontSize; $wordSpacing = 0; if ($linePartWordSpacing !== null) { $wordSpacing = $linePartWordSpacing; } $gc->drawText($words, $point->getX(), $point->getY() - $fontSize, $text->getEncoding(), $wordSpacing); $textDecoration = $text->getTextDecorationRecursively(); switch ($textDecoration) { case Node::TEXT_DECORATION_NONE: $lineDecorationYTranslation = false; break; case Node::TEXT_DECORATION_UNDERLINE: $lineDecorationYTranslation = -1; break; case Node::TEXT_DECORATION_LINE_THROUGH: $lineDecorationYTranslation = $fontSize / 3; break; case Node::TEXT_DECORATION_OVERLINE: $lineDecorationYTranslation = $fontSize - 1; break; default: //FIXME: throw exception? $lineDecorationYTranslation = false; break; } if ($lineDecorationYTranslation !== false) { $gc->setLineWidth(0.5); if ($color) { $gc->setLineColor($color); } $yCoord = $yCoord + $lineDecorationYTranslation; $gc->drawLine($point->getX(), $yCoord, $point->getX() + $width, $yCoord); } $gc->restoreGS(); }, array($this->text, $this->getFirstPoint(), $this->words, $this->getWidth(), $document, $this->wordSpacing, $this->text->getPositionTranslation()))); }
protected function doDraw(Document $document, DrawingTaskHeap $tasks) { $sourceImage = $this->createSource($document); $callback = function (Node $node, $sourceImage) { $gc = $node->getGraphicsContext(); $alpha = $node->getAlpha(); $isAlphaSet = $alpha != 1 && $alpha !== null; $keepRatio = $node->getAttribute('keep-ratio'); $rotationNode = $node->getAncestorWithRotation(); $translation = $node->getPositionTranslation(); if ($isAlphaSet || $rotationNode || $keepRatio) { $gc->saveGS(); $gc->setAlpha($alpha); } if ($rotationNode) { $middlePoint = $rotationNode->getMiddlePoint()->translate($translation->getX(), $translation->getY()); $gc->rotate($middlePoint->getX(), $middlePoint->getY(), $rotationNode->getRotate()); } $height = $originalHeight = $node->getHeight(); $width = $originalWidth = $node->getWidth(); list($x, $y) = $node->getStartDrawingPoint(); $x += $translation->getX(); $y -= $translation->getY(); if ($keepRatio) { $gc->clipRectangle($x, $y, $x + $originalWidth, $y - $originalHeight); $sourceRatio = $sourceImage->getOriginalHeight() / $sourceImage->getOriginalWidth(); if ($sourceRatio > 1) { $height = $width * $sourceRatio; $y += ($height - $originalHeight) / 2; } else { $width = $height / $sourceRatio; $x -= ($width - $originalWidth) / 2; } } $gc->drawImage($sourceImage, $x, $y - $height, $x + $width, $y); if ($isAlphaSet || $rotationNode || $keepRatio) { $gc->restoreGS(); } }; $drawingTask = new DrawingTask($callback, array($this, $sourceImage)); $tasks->insert($drawingTask); }
/** * @test */ public function getDrawingTasksFromTextObjects() { $documentStub = $this->createDocumentStub(); $expectedTasks = array(); $tasks = new DrawingTaskHeap(); for ($i = 0; $i < 3; $i++) { $text = $this->getMockBuilder('PHPPdf\\Core\\Node\\Text')->setMethods(array('collectOrderedDrawingTasks'))->disableOriginalConstructor()->getMock(); $taskStub = new DrawingTask(function () { }); $expectedTasks[] = $taskStub; $text->expects($this->once())->method('collectOrderedDrawingTasks')->with($documentStub, $this->isInstanceOf('PHPPdf\\Core\\DrawingTaskHeap'))->will($this->returnCallback(function () use($tasks, $taskStub) { $tasks->insert($taskStub); })); $this->paragraph->add($text); } $this->paragraph->collectOrderedDrawingTasks($documentStub, $tasks); $this->assertEquals(count($expectedTasks), count($tasks)); }
protected function postDraw(Document $document, DrawingTaskHeap $tasks) { if ($this->getAttribute('dump')) { $tasks->insert($this->createDumpTask()); } }
protected function doDraw(Document $document, DrawingTaskHeap $tasks) { $this->prepareGraphicsContext($document); $document->attachGraphicsContext($this->getGraphicsContext()); if (!$this->preparedTemplate) { foreach ($this->getTemplateDrawingTasksAndFormatPlaceholders($document) as $task) { $tasks->insert($task); } } parent::doDraw($document, $tasks); }
/** * @test */ public function getUnorderedTasksFromPages() { $expectedTasks = new DrawingTaskHeap(); $expectedTasks->insert(new DrawingTask(function () { })); $page = $this->getMockBuilder('PHPPdf\\Core\\Node\\Page')->setMethods(array('copy', 'collectUnorderedDrawingTasks'))->getMock(); $page->expects($this->once())->method('copy')->will($this->returnValue($page)); $page->expects($this->once())->method('collectUnorderedDrawingTasks')->will($this->returnValue($expectedTasks)); $dynamicPage = new DynamicPage($page); $document = $this->createDocumentStub(); $tasks = new DrawingTaskHeap(); $dynamicPage->collectUnorderedDrawingTasks($document, $tasks); $this->assertEquals(0, count($tasks)); $dynamicPage->createNextPage(); $dynamicPage->collectUnorderedDrawingTasks($document, $tasks); }