Skip to content
This repository was archived by the owner on Mar 22, 2019. It is now read-only.

refactoring to draw elements #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions library/ZendPdf/Drawings/DrawingAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Pdf
*/

namespace ZendPdf\Drawings;

use ZendPdf\Page;
use ZendPdf\Style;

/**
* Drawing Abstract
*
* @package ZendPdf
* @subpackage ZendPdf\Drawings
*/
abstract class DrawingAbstract implements DrawingInterface
{
/**
* @var float
*/
protected $xPosition;
/**
* @var float
*/
protected $yPosition;
/**
* @var Style
*/
protected $style;

/**
* @inheritdoc
*/
public function setPosition($x, $y)
{
$this->setX($x);
$this->setY($y);
}

/**
* @inheritdoc
*/
public function setX($position)
{
$this->xPosition = (float)$position;
}

/**
* @inheritdoc
*/
public function setY($position)
{
$this->yPosition = (float)$position;
}

/**
* @inheritdoc
*/
public function setStyle(Style $style)
{
$this->style = $style;
return $this;
}

/**
* @inheritdoc
*/
final public function draw(Page $page)
{
$content = $this->drawStyle($page);
$content .= $this->drawElement($page);
return $content;
}

/**
* Draw element with Pdf elements.
* @param Page $page
* @return string
* @throws \ZendPdf\Exception\ExceptionInterface
*/
abstract protected function drawElement(Page $page);

/**
* Draw style with Pdf elements.
* @param Page $page
* @return string
*/
protected function drawStyle(Page $page)
{
if ($this->style == null) {
return '';
}
$page->addProcedureSet('Text');
$page->addProcedureSet('PDF');
if ($this->style->getFont() !== null) {
$page->setFont($this->style->getFont(), $this->style->getFontSize());
}
return $this->style->instructions();
}
}
37 changes: 37 additions & 0 deletions library/ZendPdf/Drawings/DrawingInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Pdf
*/

namespace ZendPdf\Drawings;

use ZendPdf\Page;
use ZendPdf\Style;

/**
* Drawing Interface
*
* @package ZendPdf
* @subpackage ZendPdf\Drawings
*/
interface DrawingInterface extends PositionInterface
{
/**
* Set the style to use for current drawing operation
* @param Style $style
* @return void
*/
public function setStyle(Style $style);

/**
* Translate drawing into Pdf elements.
* @param Page $page
* @return string
*/
public function draw(Page $page);
}
179 changes: 179 additions & 0 deletions library/ZendPdf/Drawings/Ellipse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Pdf
*/

namespace ZendPdf\Drawings;

use ZendPdf\InternalType\NumericObject;
use ZendPdf\Page;

/**
* Draw an ellipse at the specified position.
*
* @package ZendPdf
* @subpackage ZendPdf\Drawings
*/
class Ellipse extends ShapeAbstract
{
/**
* @var float
*/
protected $startAngle;
/**
* @var float
*/
protected $endAngle;

public function __construct($width, $height, $startAngle = null, $endAngle = null, $fillType = self::DRAW_FILL_AND_STROKE)
{
$this->width = (float)$width;
$this->height = (float)$height;
$this->fillType = $fillType;
$this->startAngle = (float)$startAngle;
$this->endAngle = (float)$endAngle;
}

protected function drawElement(Page $page)
{
$startAngle = $this->startAngle;
$endAngle = $this->endAngle;
$xCoordinate = $this->xPosition;
$yCoordinate = $this->yPosition;
$width = $this->width;
$height = $this->height;

$page->addProcedureSet('PDF');

if ($width < $xCoordinate) {
$temp = $xCoordinate;
$xCoordinate = $width;
$width = $temp;
}
if ($height < $yCoordinate) {
$temp = $yCoordinate;
$yCoordinate = $height;
$height = $temp;
}

$x = ($xCoordinate + $width) / 2.;
$y = ($yCoordinate + $height) / 2.;

$content = '';

if ($startAngle !== .0) {
$content .= $this->drawClipSector(
$x,
$y,
$width,
$height,
$xCoordinate,
$yCoordinate,
$startAngle,
$endAngle
);
}

$content .= $this->drawEllipse($x, $y, $width, $height, $xCoordinate, $yCoordinate);
$content .= $this->drawFillType($this->fillType);
$content .= $this->drawCloseEllipse();

return $content;
}

/**
* Get close clip if have start angle.
* @return string
*/
private function drawCloseEllipse()
{
if ($this->startAngle !== .0) {
return 'Q' . PHP_EOL;
}
return '';
}

/**
* @param float $x
* @param float $y
* @param float $width
* @param float $height
* @param float $xCoordinate
* @param float $yCoordinate
* @param float $startAngle
* @param float $endAngle
* @return string
*/
private function drawClipSector($x, $y, $width, $height, $xCoordinate, $yCoordinate, $startAngle, $endAngle)
{
$xC = new NumericObject($x);
$yC = new NumericObject($y);

$startAngle = fmod($startAngle, M_PI * 2);
$endAngle = fmod($endAngle, M_PI * 2);

if ($startAngle > $endAngle) {
$endAngle += M_PI * 2;
}

$clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
$clipSectors = (int)ceil(($endAngle - $startAngle) / M_PI_4);
$clipRadius = max($width - $xCoordinate, $height - $yCoordinate);

for ($count = 0; $count <= $clipSectors; $count++) {
$pAngle = $startAngle + ($endAngle - $startAngle) * $count / (float)$clipSectors;

$pX = new NumericObject($x + cos($pAngle) * $clipRadius);
$pY = new NumericObject($y + sin($pAngle) * $clipRadius);
$clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
}

return "q\n" . $clipPath . "h\nW\nn\n";
}

/**
* @param float $x
* @param float $y
* @param float $width
* @param float $height
* @param float $xCoordinate
* @param float $yCoordinate
* @return string
*/
private function drawEllipse($x, $y, $width, $height, $xCoordinate, $yCoordinate)
{
$xC = new NumericObject($x);
$yC = new NumericObject($y);

$xLeft = new NumericObject($xCoordinate);
$xRight = new NumericObject($width);
$yUp = new NumericObject($height);
$yDown = new NumericObject($yCoordinate);

$xDelta = 2 * (M_SQRT2 - 1) * ($width - $xCoordinate) / 3.;
$yDelta = 2 * (M_SQRT2 - 1) * ($height - $yCoordinate) / 3.;
$xr = new NumericObject($x + $xDelta);
$xl = new NumericObject($x - $xDelta);
$yu = new NumericObject($y + $yDelta);
$yd = new NumericObject($y - $yDelta);

return $xC->toString() . ' ' . $yUp->toString() . " m\n"
. $xr->toString() . ' ' . $yUp->toString() . ' '
. $xRight->toString() . ' ' . $yu->toString() . ' '
. $xRight->toString() . ' ' . $yC->toString() . " c\n"
. $xRight->toString() . ' ' . $yd->toString() . ' '
. $xr->toString() . ' ' . $yDown->toString() . ' '
. $xC->toString() . ' ' . $yDown->toString() . " c\n"
. $xl->toString() . ' ' . $yDown->toString() . ' '
. $xLeft->toString() . ' ' . $yd->toString() . ' '
. $xLeft->toString() . ' ' . $yC->toString() . " c\n"
. $xLeft->toString() . ' ' . $yu->toString() . ' '
. $xl->toString() . ' ' . $yUp->toString() . ' '
. $xC->toString() . ' ' . $yUp->toString() . " c\n";
}
}
58 changes: 58 additions & 0 deletions library/ZendPdf/Drawings/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Pdf
*/

namespace ZendPdf\Drawings;

use ZendPdf\InternalType\NameObject;
use ZendPdf\InternalType\NumericObject;
use ZendPdf\Page;
use ZendPdf\Resource\Image\AbstractImage;

/**
* Draw an image at the specified position.
*
* @package ZendPdf
* @subpackage ZendPdf\Drawings
*/
class Image extends DrawingAbstract
{
protected $image;
protected $width;
protected $height;

public function __construct(AbstractImage $image, $width, $height)
{
$this->image = $image;
$this->width = (float)$width;
$this->height = (float)$height;
}

/**
* @inheritdoc
*/
protected function drawElement(Page $page)
{
$page->addProcedureSet('PDF');

$imageName = $page->attachResource('XObject', $this->image);
$imageNameObj = new NameObject($imageName);

$x1Obj = new NumericObject($this->xPosition);
$y1Obj = new NumericObject($this->yPosition);
$widthObj = new NumericObject($this->width - $this->xPosition);
$heightObj = new NumericObject($this->height - $this->yPosition);

return "q\n"
. '1 0 0 1 ' . $x1Obj->toString() . ' ' . $y1Obj->toString() . " cm\n"
. $widthObj->toString() . ' 0 0 ' . $heightObj->toString() . " 0 0 cm\n"
. $imageNameObj->toString() . " Do\n"
. "Q\n";
}
}
Loading