From 1b7776c159774242c34a25ff1d29e9b3e28ff375 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Mon, 23 Feb 2015 11:01:47 -0300 Subject: [PATCH 1/8] append drawing into page the Page no need to know how to draw a text. This code creates Drawings namespace to segregate those responsibilities --- library/ZendPdf/Drawings/DrawingAbstract.php | 106 ++++++++++++++++++ library/ZendPdf/Drawings/DrawingInterface.php | 37 ++++++ .../ZendPdf/Drawings/PositionInterface.php | 43 +++++++ library/ZendPdf/Drawings/SimpleText.php | 71 ++++++++++++ library/ZendPdf/Page.php | 40 ++++--- tests/ZendPdf/Drawing/SimpleTextTest.php | 80 +++++++++++++ 6 files changed, 363 insertions(+), 14 deletions(-) create mode 100644 library/ZendPdf/Drawings/DrawingAbstract.php create mode 100644 library/ZendPdf/Drawings/DrawingInterface.php create mode 100644 library/ZendPdf/Drawings/PositionInterface.php create mode 100644 library/ZendPdf/Drawings/SimpleText.php create mode 100644 tests/ZendPdf/Drawing/SimpleTextTest.php diff --git a/library/ZendPdf/Drawings/DrawingAbstract.php b/library/ZendPdf/Drawings/DrawingAbstract.php new file mode 100644 index 0000000..ef65117 --- /dev/null +++ b/library/ZendPdf/Drawings/DrawingAbstract.php @@ -0,0 +1,106 @@ +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(); + } +} diff --git a/library/ZendPdf/Drawings/DrawingInterface.php b/library/ZendPdf/Drawings/DrawingInterface.php new file mode 100644 index 0000000..e5e66f8 --- /dev/null +++ b/library/ZendPdf/Drawings/DrawingInterface.php @@ -0,0 +1,37 @@ +text = $text; + $this->charEncoding = $charEncoding; + } + + /** + * Draw a line of text at the specified position. + * @param Page $page + * @return string + * @throws \ZendPdf\Exception\ExceptionInterface + */ + protected function drawElement(Page $page) + { + if ($page->getFont() === null) { + throw new LogicException('Font has not been set'); + } + + $page->addProcedureSet('Text'); + + $textObj = new StringObject($page->getFont()->encodeString($this->text, $this->charEncoding)); + $xCoordinate = new NumericObject($this->xPosition); + $yCoordinate = new NumericObject($this->yPosition); + + return "BT\n" + . $xCoordinate->toString() . ' ' . $yCoordinate->toString() . " Td\n" + . $textObj->toString() . " Tj\n" + . "ET\n"; + } +} diff --git a/library/ZendPdf/Page.php b/library/ZendPdf/Page.php index cc5473a..d65f250 100644 --- a/library/ZendPdf/Page.php +++ b/library/ZendPdf/Page.php @@ -10,6 +10,8 @@ namespace ZendPdf; +use ZendPdf\Drawings\DrawingInterface; +use ZendPdf\Drawings\SimpleText; use ZendPdf\Exception; use ZendPdf\InternalType; @@ -1574,20 +1576,8 @@ public function drawRoundedRectangle($x1, $y1, $x2, $y2, $radius, */ public function drawText($text, $x, $y, $charEncoding = '') { - if ($this->_font === null) { - throw new Exception\LogicException('Font has not been set'); - } - - $this->_addProcSet('Text'); - - $textObj = new InternalType\StringObject($this->_font->encodeString($text, $charEncoding)); - $xObj = new InternalType\NumericObject($x); - $yObj = new InternalType\NumericObject($y); - - $this->_contents .= "BT\n" - . $xObj->toString() . ' ' . $yObj->toString() . " Td\n" - . $textObj->toString() . " Tj\n" - . "ET\n"; + $simpleText = new SimpleText($text,$charEncoding); + $this->draw($x, $y, $simpleText); return $this; } @@ -1793,4 +1783,26 @@ public function skew($x, $y, $xAngle, $yAngle) return $this; } + + /** + * Add procedureSet to the Page description + * @param $name + * @return void + */ + public function addProcedureSet($name) + { + $this->_addProcSet($name); + } + + /** + * Draw element in specific position. + * @param float $x x position + * @param float $y y position + * @param DrawingInterface $drawing Element to draw + */ + public function draw($x, $y, DrawingInterface $drawing) + { + $drawing->setPosition($x, $y); + $this->_contents .= $drawing->draw($this); + } } diff --git a/tests/ZendPdf/Drawing/SimpleTextTest.php b/tests/ZendPdf/Drawing/SimpleTextTest.php new file mode 100644 index 0000000..177f51f --- /dev/null +++ b/tests/ZendPdf/Drawing/SimpleTextTest.php @@ -0,0 +1,80 @@ +newPage(Page::SIZE_A4); + $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); + return $page; + } + + public function testDrawASimpleText() + { + $pdf = new PdfDocument(); + $page = $pdf->newPage(Page::SIZE_A4); + $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); + + $text = new SimpleText('testing my text'); + $text->setPosition(10, 10); + + $expected = <<assertEquals($expected, $text->draw($page)); + } + + public function testDrawASimpleTextWithColor() + { + $page = $this->getPage(); + + $style = new Style(); + $style->setFillColor(Html::color('#FF0000')); + + $text = new SimpleText('testing my text'); + $text->setPosition(10, 10); + $text->setStyle($style); + + $expected = <<assertEquals($expected, $text->draw($page)); + } +} From 2d054fb7ccad74c5cdea3f957e9ab9a89ecf7616 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Mon, 23 Feb 2015 22:12:34 -0300 Subject: [PATCH 2/8] create a rectangle object to draw a rectangle --- library/ZendPdf/Drawings/Rectangle.php | 50 ++++++++++++ library/ZendPdf/Drawings/ShapeAbstract.php | 85 +++++++++++++++++++++ library/ZendPdf/Drawings/ShapeInterface.php | 36 +++++++++ library/ZendPdf/Page.php | 27 +------ tests/ZendPdf/Drawing/RectangleTest.php | 53 +++++++++++++ 5 files changed, 228 insertions(+), 23 deletions(-) create mode 100644 library/ZendPdf/Drawings/Rectangle.php create mode 100644 library/ZendPdf/Drawings/ShapeAbstract.php create mode 100644 library/ZendPdf/Drawings/ShapeInterface.php create mode 100644 tests/ZendPdf/Drawing/RectangleTest.php diff --git a/library/ZendPdf/Drawings/Rectangle.php b/library/ZendPdf/Drawings/Rectangle.php new file mode 100644 index 0000000..ae84dd2 --- /dev/null +++ b/library/ZendPdf/Drawings/Rectangle.php @@ -0,0 +1,50 @@ +width = (float)$width; + $this->height = (float)$height; + $this->fillType = $fillType; + } + + /** + * @inheritdoc + */ + protected function drawElement(Page $page) + { + $page->addProcedureSet('PDF'); + + $xCoordinate = new NumericObject($this->xPosition); + $yCoordinate = new NumericObject($this->yPosition); + $width = new NumericObject($this->width - $this->xPosition); + $height = new NumericObject($this->height - $this->yPosition); + + $content = $xCoordinate->toString() . ' ' . $yCoordinate->toString() . ' ' + . $width->toString() . ' ' . $height->toString() . " re\n"; + + $content .= $this->drawFillType($this->fillType); + return $content; + } +} diff --git a/library/ZendPdf/Drawings/ShapeAbstract.php b/library/ZendPdf/Drawings/ShapeAbstract.php new file mode 100644 index 0000000..01aed8b --- /dev/null +++ b/library/ZendPdf/Drawings/ShapeAbstract.php @@ -0,0 +1,85 @@ +width = $width; + return $this; + } + + /** + * @param float $height + * @return ShapeAbstract + */ + public function setHeight($height) + { + $this->height = $height; + return $this; + } + + /** + * @param int $fillType + * @return ShapeAbstract + */ + public function setFillType($fillType) + { + $this->fillType = $fillType; + return $this; + } + + /** + * @param int $fillType + * @return string + */ + protected function drawFillType($fillType) + { + $content = ''; + switch ($fillType) { + case static::DRAW_FILL_AND_STROKE: + $content .= " B*\n"; + break; + case static::DRAW_FILL: + $content .= " f*\n"; + break; + case static::DRAW_STROKE: + $content .= " S\n"; + break; + } + return $content; + } + +} diff --git a/library/ZendPdf/Drawings/ShapeInterface.php b/library/ZendPdf/Drawings/ShapeInterface.php new file mode 100644 index 0000000..58e398a --- /dev/null +++ b/library/ZendPdf/Drawings/ShapeInterface.php @@ -0,0 +1,36 @@ +_addProcSet('PDF'); - - $x1Obj = new InternalType\NumericObject($x1); - $y1Obj = new InternalType\NumericObject($y1); - $widthObj = new InternalType\NumericObject($x2 - $x1); - $height2Obj = new InternalType\NumericObject($y2 - $y1); - - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' - . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n"; - - switch ($fillType) { - case self::SHAPE_DRAW_FILL_AND_STROKE: - $this->_contents .= " B*\n"; - break; - case self::SHAPE_DRAW_FILL: - $this->_contents .= " f*\n"; - break; - case self::SHAPE_DRAW_STROKE: - $this->_contents .= " S\n"; - break; - } - + $rectangle = new Rectangle($x2, $y2,$fillType); + $this->draw($x1, $y1, $rectangle); return $this; } diff --git a/tests/ZendPdf/Drawing/RectangleTest.php b/tests/ZendPdf/Drawing/RectangleTest.php new file mode 100644 index 0000000..b635e99 --- /dev/null +++ b/tests/ZendPdf/Drawing/RectangleTest.php @@ -0,0 +1,53 @@ +newPage(Page::SIZE_A4); + $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); + return $page; + } + + public function testDrawARectangle() + { + $pdf = new PdfDocument(); + $page = $pdf->newPage(Page::SIZE_A4); + $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); + + $text = new Rectangle(100, 50); + $text->setPosition(10, 10); + + $expected = <<assertEquals($expected, $text->draw($page)); + } +} From 3b047294be0d3c62f8d8ac20c89dff558846175e Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Mon, 23 Feb 2015 22:21:10 -0300 Subject: [PATCH 3/8] create rounded rectangle object to draw rounded rectangle --- library/ZendPdf/Drawings/RoundedRectangle.php | 187 ++++++++++++++++++ library/ZendPdf/Page.php | 119 +---------- .../ZendPdf/Drawing/RoundedRectangleTest.php | 61 ++++++ 3 files changed, 253 insertions(+), 114 deletions(-) create mode 100644 library/ZendPdf/Drawings/RoundedRectangle.php create mode 100644 tests/ZendPdf/Drawing/RoundedRectangleTest.php diff --git a/library/ZendPdf/Drawings/RoundedRectangle.php b/library/ZendPdf/Drawings/RoundedRectangle.php new file mode 100644 index 0000000..737b99b --- /dev/null +++ b/library/ZendPdf/Drawings/RoundedRectangle.php @@ -0,0 +1,187 @@ + 0, + self::CORNER_TOP_RIGHT => 0, + self::CORNER_BOTTOM_RIGHT => 0, + self::CORNER_BOTTOM_LEFT => 0, + ); + + public function __construct($width, $height, $radius, $fillType = self::DRAW_FILL_AND_STROKE) + { + $this->width = (float)$width; + $this->height = (float)$height; + $this->fillType = $fillType; + $this->setAllRadius($radius); + } + + /** + * @param float $radius + */ + public function setAllRadius($radius) + { + $this->setTopLeft($radius); + $this->setTopRight($radius); + $this->setBottomRight($radius); + $this->setBottomLeft($radius); + } + + /** + * @param float $radius + */ + public function setTopLeft($radius) + { + $this->radius[static::CORNER_TOP_LEFT] = (float)$radius; + } + + /** + * @param float $radius + */ + public function setTopRight($radius) + { + $this->radius[static::CORNER_TOP_RIGHT] = (float)$radius; + } + + /** + * @param float $radius + */ + public function setBottomRight($radius) + { + $this->radius[static::CORNER_BOTTOM_RIGHT] = (float)$radius; + } + + /** + * @param float $radius + */ + public function setBottomLeft($radius) + { + $this->radius[static::CORNER_BOTTOM_LEFT] = (float)$radius; + } + + /** + * @param Page $page + * @return string + */ + protected function drawElement(Page $page) + { + $page->addProcedureSet('PDF'); + + $topLeftX = $this->xPosition; + $topLeftY = $this->height; + $topRightX = $this->width; + $topRightY = $this->height; + $bottomRightX = $this->width; + $bottomRightY = $this->yPosition; + $bottomLeftX = $this->xPosition; + $bottomLeftY = $this->yPosition; + $radius = $this->radius; + + //draw top side + $x1Obj = new NumericObject($topLeftX + $radius[0]); + $y1Obj = new NumericObject($topLeftY); + $content = $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n"; + $x1Obj = new NumericObject($topRightX - $radius[1]); + $y1Obj = new NumericObject($topRightY); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; + + //draw top right corner if needed + if ($radius[1] != 0) { + $x1Obj = new NumericObject($topRightX); + $y1Obj = new NumericObject($topRightY); + $x2Obj = new NumericObject($topRightX); + $y2Obj = new NumericObject($topRightY); + $x3Obj = new NumericObject($topRightX); + $y3Obj = new NumericObject($topRightY - $radius[1]); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' + . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' + . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' + . " c\n"; + } + + //draw right side + $x1Obj = new NumericObject($bottomRightX); + $y1Obj = new NumericObject($bottomRightY + $radius[2]); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; + + //draw bottom right corner if needed + if ($radius[2] != 0) { + $x1Obj = new NumericObject($bottomRightX); + $y1Obj = new NumericObject($bottomRightY); + $x2Obj = new NumericObject($bottomRightX); + $y2Obj = new NumericObject($bottomRightY); + $x3Obj = new NumericObject($bottomRightX - $radius[2]); + $y3Obj = new NumericObject($bottomRightY); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' + . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' + . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' + . " c\n"; + } + + //draw bottom side + $x1Obj = new NumericObject($bottomLeftX + $radius[3]); + $y1Obj = new NumericObject($bottomLeftY); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; + + //draw bottom left corner if needed + if ($radius[3] != 0) { + $x1Obj = new NumericObject($bottomLeftX); + $y1Obj = new NumericObject($bottomLeftY); + $x2Obj = new NumericObject($bottomLeftX); + $y2Obj = new NumericObject($bottomLeftY); + $x3Obj = new NumericObject($bottomLeftX); + $y3Obj = new NumericObject($bottomLeftY + $radius[3]); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' + . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' + . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' + . " c\n"; + } + + //draw left side + $x1Obj = new NumericObject($topLeftX); + $y1Obj = new NumericObject($topLeftY - $radius[0]); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; + + //draw top left corner if needed + if ($radius[0] != 0) { + $x1Obj = new NumericObject($topLeftX); + $y1Obj = new NumericObject($topLeftY); + $x2Obj = new NumericObject($topLeftX); + $y2Obj = new NumericObject($topLeftY); + $x3Obj = new NumericObject($topLeftX + $radius[0]); + $y3Obj = new NumericObject($topLeftY); + $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' + . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' + . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' + . " c\n"; + } + + $content .= $this->drawFillType($this->fillType); + return $content; + } +} diff --git a/library/ZendPdf/Page.php b/library/ZendPdf/Page.php index 0e6446a..a25cf04 100644 --- a/library/ZendPdf/Page.php +++ b/library/ZendPdf/Page.php @@ -11,7 +11,9 @@ namespace ZendPdf; use ZendPdf\Drawings\DrawingInterface; +use ZendPdf\Drawings\Ellipse; use ZendPdf\Drawings\Rectangle; +use ZendPdf\Drawings\RoundedRectangle; use ZendPdf\Drawings\SimpleText; use ZendPdf\Exception; use ZendPdf\InternalType; @@ -1426,121 +1428,10 @@ public function drawRectangle($x1, $y1, $x2, $y2, $fillType = Rectangle::DRAW_FI * @return \ZendPdf\Page */ public function drawRoundedRectangle($x1, $y1, $x2, $y2, $radius, - $fillType = self::SHAPE_DRAW_FILL_AND_STROKE) + $fillType = RoundedRectangle::DRAW_FILL_AND_STROKE) { - - $this->_addProcSet('PDF'); - - if(!is_array($radius)) { - $radius = array($radius, $radius, $radius, $radius); - } else { - for ($i = 0; $i < 4; $i++) { - if(!isset($radius[$i])) { - $radius[$i] = 0; - } - } - } - - $topLeftX = $x1; - $topLeftY = $y2; - $topRightX = $x2; - $topRightY = $y2; - $bottomRightX = $x2; - $bottomRightY = $y1; - $bottomLeftX = $x1; - $bottomLeftY = $y1; - - //draw top side - $x1Obj = new InternalType\NumericObject($topLeftX + $radius[0]); - $y1Obj = new InternalType\NumericObject($topLeftY); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n"; - $x1Obj = new InternalType\NumericObject($topRightX - $radius[1]); - $y1Obj = new InternalType\NumericObject($topRightY); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; - - //draw top right corner if needed - if ($radius[1] != 0) { - $x1Obj = new InternalType\NumericObject($topRightX); - $y1Obj = new InternalType\NumericObject($topRightY); - $x2Obj = new InternalType\NumericObject($topRightX); - $y2Obj = new InternalType\NumericObject($topRightY); - $x3Obj = new InternalType\NumericObject($topRightX); - $y3Obj = new InternalType\NumericObject($topRightY - $radius[1]); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' - . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' - . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' - . " c\n"; - } - - //draw right side - $x1Obj = new InternalType\NumericObject($bottomRightX); - $y1Obj = new InternalType\NumericObject($bottomRightY + $radius[2]); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; - - //draw bottom right corner if needed - if ($radius[2] != 0) { - $x1Obj = new InternalType\NumericObject($bottomRightX); - $y1Obj = new InternalType\NumericObject($bottomRightY); - $x2Obj = new InternalType\NumericObject($bottomRightX); - $y2Obj = new InternalType\NumericObject($bottomRightY); - $x3Obj = new InternalType\NumericObject($bottomRightX - $radius[2]); - $y3Obj = new InternalType\NumericObject($bottomRightY); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' - . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' - . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' - . " c\n"; - } - - //draw bottom side - $x1Obj = new InternalType\NumericObject($bottomLeftX + $radius[3]); - $y1Obj = new InternalType\NumericObject($bottomLeftY); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; - - //draw bottom left corner if needed - if ($radius[3] != 0) { - $x1Obj = new InternalType\NumericObject($bottomLeftX); - $y1Obj = new InternalType\NumericObject($bottomLeftY); - $x2Obj = new InternalType\NumericObject($bottomLeftX); - $y2Obj = new InternalType\NumericObject($bottomLeftY); - $x3Obj = new InternalType\NumericObject($bottomLeftX); - $y3Obj = new InternalType\NumericObject($bottomLeftY + $radius[3]); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' - . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' - . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' - . " c\n"; - } - - //draw left side - $x1Obj = new InternalType\NumericObject($topLeftX); - $y1Obj = new InternalType\NumericObject($topLeftY - $radius[0]); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; - - //draw top left corner if needed - if ($radius[0] != 0) { - $x1Obj = new InternalType\NumericObject($topLeftX); - $y1Obj = new InternalType\NumericObject($topLeftY); - $x2Obj = new InternalType\NumericObject($topLeftX); - $y2Obj = new InternalType\NumericObject($topLeftY); - $x3Obj = new InternalType\NumericObject($topLeftX + $radius[0]); - $y3Obj = new InternalType\NumericObject($topLeftY); - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' - . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' - . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' - . " c\n"; - } - - switch ($fillType) { - case self::SHAPE_DRAW_FILL_AND_STROKE: - $this->_contents .= " B*\n"; - break; - case self::SHAPE_DRAW_FILL: - $this->_contents .= " f*\n"; - break; - case self::SHAPE_DRAW_STROKE: - $this->_contents .= " S\n"; - break; - } - + $roundedRectangle = new RoundedRectangle($x2, $y2, $radius,$fillType); + $this->draw($x1, $y1, $roundedRectangle); return $this; } diff --git a/tests/ZendPdf/Drawing/RoundedRectangleTest.php b/tests/ZendPdf/Drawing/RoundedRectangleTest.php new file mode 100644 index 0000000..654cc9f --- /dev/null +++ b/tests/ZendPdf/Drawing/RoundedRectangleTest.php @@ -0,0 +1,61 @@ +newPage(Page::SIZE_A4); + $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); + return $page; + } + + public function testDrawARectangle() + { + $pdf = new PdfDocument(); + $page = $pdf->newPage(Page::SIZE_A4); + $page->setFont(Font::fontWithName(Font::FONT_HELVETICA), 10); + + $text = new RoundedRectangle(100, 50, 10); + $text->setPosition(10, 10); + + $expected = <<assertEquals($expected, $text->draw($page)); + } +} From d00798163750279522d74204c331230fb63df101 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Mon, 23 Feb 2015 23:15:35 -0300 Subject: [PATCH 4/8] create an ellipse object to draw an ellipse --- library/ZendPdf/Drawings/Ellipse.php | 179 ++++++++++++++++++++++++++ library/ZendPdf/Page.php | 88 +------------ tests/ZendPdf/Drawing/EllipseTest.php | 91 +++++++++++++ 3 files changed, 274 insertions(+), 84 deletions(-) create mode 100644 library/ZendPdf/Drawings/Ellipse.php create mode 100644 tests/ZendPdf/Drawing/EllipseTest.php diff --git a/library/ZendPdf/Drawings/Ellipse.php b/library/ZendPdf/Drawings/Ellipse.php new file mode 100644 index 0000000..286f14d --- /dev/null +++ b/library/ZendPdf/Drawings/Ellipse.php @@ -0,0 +1,179 @@ +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"; + } +} diff --git a/library/ZendPdf/Page.php b/library/ZendPdf/Page.php index a25cf04..86caf46 100644 --- a/library/ZendPdf/Page.php +++ b/library/ZendPdf/Page.php @@ -1151,10 +1151,12 @@ public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, if ($param5 === null) { // drawEllipse($x1, $y1, $x2, $y2); $startAngle = null; + $endAngle = null; $fillType = self::SHAPE_DRAW_FILL_AND_STROKE; } elseif ($param6 === null) { // drawEllipse($x1, $y1, $x2, $y2, $fillType); $startAngle = null; + $endAngle = null; $fillType = $param5; } else { // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle); @@ -1169,90 +1171,8 @@ public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, } } - $this->_addProcSet('PDF'); - - if ($x2 < $x1) { - $temp = $x1; - $x1 = $x2; - $x2 = $temp; - } - if ($y2 < $y1) { - $temp = $y1; - $y1 = $y2; - $y2 = $temp; - } - - $x = ($x1 + $x2)/2.; - $y = ($y1 + $y2)/2.; - - $xC = new InternalType\NumericObject($x); - $yC = new InternalType\NumericObject($y); - - if ($startAngle !== null) { - if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); } - if ($endAngle != 0) { $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($x2 - $x1, $y2 - $y1); - - for($count = 0; $count <= $clipSectors; $count++) { - $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors; - - $pX = new InternalType\NumericObject($x + cos($pAngle)*$clipRadius); - $pY = new InternalType\NumericObject($y + sin($pAngle)*$clipRadius); - $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n"; - } - - $this->_contents .= "q\n" . $clipPath . "h\nW\nn\n"; - } - - $xLeft = new InternalType\NumericObject($x1); - $xRight = new InternalType\NumericObject($x2); - $yUp = new InternalType\NumericObject($y2); - $yDown = new InternalType\NumericObject($y1); - - $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.; - $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.; - $xr = new InternalType\NumericObject($x + $xDelta); - $xl = new InternalType\NumericObject($x - $xDelta); - $yu = new InternalType\NumericObject($y + $yDelta); - $yd = new InternalType\NumericObject($y - $yDelta); - - $this->_contents .= $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"; - - switch ($fillType) { - case self::SHAPE_DRAW_FILL_AND_STROKE: - $this->_contents .= " B*\n"; - break; - case self::SHAPE_DRAW_FILL: - $this->_contents .= " f*\n"; - break; - case self::SHAPE_DRAW_STROKE: - $this->_contents .= " S\n"; - break; - } - - if ($startAngle !== null) { - $this->_contents .= "Q\n"; - } - + $ellipse = new Ellipse($x2, $y2, $startAngle, $endAngle, $fillType); + $this->draw($x1, $y1, $ellipse); return $this; } diff --git a/tests/ZendPdf/Drawing/EllipseTest.php b/tests/ZendPdf/Drawing/EllipseTest.php new file mode 100644 index 0000000..c4de94f --- /dev/null +++ b/tests/ZendPdf/Drawing/EllipseTest.php @@ -0,0 +1,91 @@ +newPage(Page::SIZE_A4); + + $ellipse1 = new Ellipse(400, 350); + $ellipse1->setPosition(250, 400); + $actualContent = $ellipse1->draw($page); + + $ellipse2 = new Ellipse(400, 350, M_PI / 6, 2 * M_PI / 3); + $ellipse2->setPosition(250, 400); + $actualContent .= $ellipse2->draw($page); + + $ellipse3 = new Ellipse(400, 350, -M_PI / 6, M_PI / 6); + $ellipse3->setPosition(250, 400); + $actualContent .= $ellipse3->draw($page); + + + $expected = <<assertEquals($expected, $actualContent); + } +} From 603a52b3fb76b8d75dcc3d56cc907740adad990a Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Mon, 23 Feb 2015 23:38:40 -0300 Subject: [PATCH 5/8] create image drawing object to draw an image from file --- library/ZendPdf/Drawings/Image.php | 58 +++++++++++++++++++++++++++++ library/ZendPdf/Page.php | 32 ++++++++-------- tests/ZendPdf/Drawing/ImageTest.php | 50 +++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 library/ZendPdf/Drawings/Image.php create mode 100644 tests/ZendPdf/Drawing/ImageTest.php diff --git a/library/ZendPdf/Drawings/Image.php b/library/ZendPdf/Drawings/Image.php new file mode 100644 index 0000000..ae76bac --- /dev/null +++ b/library/ZendPdf/Drawings/Image.php @@ -0,0 +1,58 @@ +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"; + } +} diff --git a/library/ZendPdf/Page.php b/library/ZendPdf/Page.php index 86caf46..e537f2f 100644 --- a/library/ZendPdf/Page.php +++ b/library/ZendPdf/Page.php @@ -12,6 +12,7 @@ use ZendPdf\Drawings\DrawingInterface; use ZendPdf\Drawings\Ellipse; +use ZendPdf\Drawings\Image; use ZendPdf\Drawings\Rectangle; use ZendPdf\Drawings\RoundedRectangle; use ZendPdf\Drawings\SimpleText; @@ -1188,22 +1189,8 @@ public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, */ public function drawImage(Resource\Image\AbstractImage $image, $x1, $y1, $x2, $y2) { - $this->_addProcSet('PDF'); - - $imageName = $this->_attachResource('XObject', $image); - $imageNameObj = new InternalType\NameObject($imageName); - - $x1Obj = new InternalType\NumericObject($x1); - $y1Obj = new InternalType\NumericObject($y1); - $widthObj = new InternalType\NumericObject($x2 - $x1); - $heightObj = new InternalType\NumericObject($y2 - $y1); - - $this->_contents .= "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"; - + $drawImage = new Image($image, $x2, $y2); + $this->draw($x1, $y1, $drawImage); return $this; } @@ -1597,4 +1584,17 @@ public function draw($x, $y, DrawingInterface $drawing) $drawing->setPosition($x, $y); $this->_contents .= $drawing->draw($this); } + + + /** + * Attach resource to the page + * + * @param string $type + * @param \ZendPdf\Resource\AbstractResource $resource + * @return string + */ + public function attachResource($type, Resource\AbstractResource $resource) + { + return $this->_attachResource($type, $resource); + } } diff --git a/tests/ZendPdf/Drawing/ImageTest.php b/tests/ZendPdf/Drawing/ImageTest.php new file mode 100644 index 0000000..de066e0 --- /dev/null +++ b/tests/ZendPdf/Drawing/ImageTest.php @@ -0,0 +1,50 @@ +newPage(Page::SIZE_A4); + + $stampImagePNG = FileImage::imageWithPath(__DIR__ . '/../_files/stamp.png'); + + $ellipse1 = new Image($stampImagePNG, 300, 550); + $ellipse1->setPosition(200, 450); + $actualContent = $ellipse1->draw($page); + + $expected = <<assertEquals($expected, $actualContent); + } +} From 9e0b7bf4ccf702ae6819f60693197c021feca3bc Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Mon, 23 Feb 2015 23:47:01 -0300 Subject: [PATCH 6/8] create a line object to draw a line --- library/ZendPdf/Drawings/Line.php | 51 ++++++++++++++++++++++++++++++ library/ZendPdf/Page.php | 13 ++------ tests/ZendPdf/Drawing/LineTest.php | 45 ++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 library/ZendPdf/Drawings/Line.php create mode 100644 tests/ZendPdf/Drawing/LineTest.php diff --git a/library/ZendPdf/Drawings/Line.php b/library/ZendPdf/Drawings/Line.php new file mode 100644 index 0000000..8e7a386 --- /dev/null +++ b/library/ZendPdf/Drawings/Line.php @@ -0,0 +1,51 @@ +width = (float)$width; + $this->height = (float)$height; + } + + /** + * @inheritdoc + */ + protected function drawElement(Page $page) + { + $page->addProcedureSet('PDF'); + + $x1Obj = new NumericObject($this->xPosition); + $y1Obj = new NumericObject($this->yPosition); + $x2Obj = new NumericObject($this->width); + $y2Obj = new NumericObject($this->height); + + return $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n" + . $x2Obj->toString() . ' ' . $y2Obj->toString() . " l\n S\n"; + + } +} diff --git a/library/ZendPdf/Page.php b/library/ZendPdf/Page.php index e537f2f..6bec655 100644 --- a/library/ZendPdf/Page.php +++ b/library/ZendPdf/Page.php @@ -13,6 +13,7 @@ use ZendPdf\Drawings\DrawingInterface; use ZendPdf\Drawings\Ellipse; use ZendPdf\Drawings\Image; +use ZendPdf\Drawings\Line; use ZendPdf\Drawings\Rectangle; use ZendPdf\Drawings\RoundedRectangle; use ZendPdf\Drawings\SimpleText; @@ -1219,16 +1220,8 @@ public function drawLayoutBox($box, $x, $y) */ public function drawLine($x1, $y1, $x2, $y2) { - $this->_addProcSet('PDF'); - - $x1Obj = new InternalType\NumericObject($x1); - $y1Obj = new InternalType\NumericObject($y1); - $x2Obj = new InternalType\NumericObject($x2); - $y2Obj = new InternalType\NumericObject($y2); - - $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n" - . $x2Obj->toString() . ' ' . $y2Obj->toString() . " l\n S\n"; - + $line = new Line($x2, $y2); + $this->draw($x1, $y1, $line); return $this; } diff --git a/tests/ZendPdf/Drawing/LineTest.php b/tests/ZendPdf/Drawing/LineTest.php new file mode 100644 index 0000000..800e6de --- /dev/null +++ b/tests/ZendPdf/Drawing/LineTest.php @@ -0,0 +1,45 @@ +newPage(Page::SIZE_A4); + + $ellipse1 = new Line(300, 550); + $ellipse1->setPosition(200, 450); + $actualContent = $ellipse1->draw($page); + + $expected = <<assertEquals($expected, $actualContent); + } +} From 0e53c0c1823bfbd436ea6ae82d3ef4da474cf548 Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Tue, 24 Feb 2015 00:20:35 -0300 Subject: [PATCH 7/8] create a polygon class class to draw a polygon --- library/ZendPdf/Drawings/Polygon.php | 65 +++++++++++++++++++++ library/ZendPdf/Drawings/ShapeAbstract.php | 17 +++++- library/ZendPdf/Drawings/ShapeInterface.php | 9 +++ library/ZendPdf/Page.php | 50 +++------------- tests/ZendPdf/Drawing/PolygonTest.php | 55 +++++++++++++++++ 5 files changed, 152 insertions(+), 44 deletions(-) create mode 100644 library/ZendPdf/Drawings/Polygon.php create mode 100644 tests/ZendPdf/Drawing/PolygonTest.php diff --git a/library/ZendPdf/Drawings/Polygon.php b/library/ZendPdf/Drawings/Polygon.php new file mode 100644 index 0000000..4cb577a --- /dev/null +++ b/library/ZendPdf/Drawings/Polygon.php @@ -0,0 +1,65 @@ +xCoordinates = $xCoordinates; + $this->yCoordinates = $yCoordinates; + $this->fillType = $fillType; + $this->fillMethod = $fillMethod; + } + + /** + * @inheritdoc + */ + protected function drawElement(Page $page) + { + $page->addProcedureSet('PDF'); + + $firstPoint = true; + $path = ''; + $content = ''; + foreach ($this->xCoordinates as $id => $xVal) { + $xObj = new NumericObject($xVal); + $yObj = new NumericObject($this->yCoordinates[$id]); + + if ($firstPoint) { + $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n"; + $firstPoint = false; + } else { + $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n"; + } + } + $content .= $path; + $content .= $this->drawFillType($this->fillType, $this->fillMethod); + + return $content; + } +} diff --git a/library/ZendPdf/Drawings/ShapeAbstract.php b/library/ZendPdf/Drawings/ShapeAbstract.php index 01aed8b..ae9870a 100644 --- a/library/ZendPdf/Drawings/ShapeAbstract.php +++ b/library/ZendPdf/Drawings/ShapeAbstract.php @@ -63,12 +63,26 @@ public function setFillType($fillType) /** * @param int $fillType + * @param int $fillMethod * @return string */ - protected function drawFillType($fillType) + protected function drawFillType($fillType, $fillMethod = null) { $content = ''; + switch ($fillType) { + case static::DRAW_FILL_AND_STROKE && $fillMethod === static::FILL_METHOD_NON_ZERO_WINDING: + $content .= " b\n"; + break; + case static::DRAW_FILL_AND_STROKE && $fillMethod === static::FILL_METHOD_EVEN_ODD: + $content .= " b*\n"; + break; + case static::DRAW_FILL && $fillMethod === static::FILL_METHOD_NON_ZERO_WINDING: + $content .= " h\n f\n"; + break; + case static::DRAW_FILL && $fillMethod === static::FILL_METHOD_EVEN_ODD: + $content .= " h\n f*\n"; + break; case static::DRAW_FILL_AND_STROKE: $content .= " B*\n"; break; @@ -81,5 +95,4 @@ protected function drawFillType($fillType) } return $content; } - } diff --git a/library/ZendPdf/Drawings/ShapeInterface.php b/library/ZendPdf/Drawings/ShapeInterface.php index 58e398a..97ceceb 100644 --- a/library/ZendPdf/Drawings/ShapeInterface.php +++ b/library/ZendPdf/Drawings/ShapeInterface.php @@ -33,4 +33,13 @@ interface ShapeInterface extends DrawingInterface */ const DRAW_FILL_AND_STROKE = 2; + /** + * Fill the path using the non-zero winding rule. + */ + const FILL_METHOD_NON_ZERO_WINDING = 0; + + /** + * Fill the path using the even-odd rule. + */ + const FILL_METHOD_EVEN_ODD = 1; } diff --git a/library/ZendPdf/Page.php b/library/ZendPdf/Page.php index 6bec655..0d25f2e 100644 --- a/library/ZendPdf/Page.php +++ b/library/ZendPdf/Page.php @@ -14,6 +14,7 @@ use ZendPdf\Drawings\Ellipse; use ZendPdf\Drawings\Image; use ZendPdf\Drawings\Line; +use ZendPdf\Drawings\Polygon; use ZendPdf\Drawings\Rectangle; use ZendPdf\Drawings\RoundedRectangle; use ZendPdf\Drawings\SimpleText; @@ -1239,49 +1240,14 @@ public function drawLine($x1, $y1, $x2, $y2) * @param integer $fillMethod * @return \ZendPdf\Page */ - public function drawPolygon($x, $y, - $fillType = self::SHAPE_DRAW_FILL_AND_STROKE, - $fillMethod = self::FILL_METHOD_NON_ZERO_WINDING) + public function drawPolygon( + $x, + $y, + $fillType = Polygon::DRAW_FILL_AND_STROKE, + $fillMethod = Polygon::FILL_METHOD_NON_ZERO_WINDING) { - $this->_addProcSet('PDF'); - - $firstPoint = true; - foreach ($x as $id => $xVal) { - $xObj = new InternalType\NumericObject($xVal); - $yObj = new InternalType\NumericObject($y[$id]); - - if ($firstPoint) { - $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n"; - $firstPoint = false; - } else { - $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n"; - } - } - - $this->_contents .= $path; - - switch ($fillType) { - case self::SHAPE_DRAW_FILL_AND_STROKE: - if ($fillMethod == self::FILL_METHOD_NON_ZERO_WINDING) { - $this->_contents .= " b\n"; - } else { - // Even-Odd fill method. - $this->_contents .= " b*\n"; - } - break; - case self::SHAPE_DRAW_FILL: - if ($fillMethod == self::FILL_METHOD_NON_ZERO_WINDING) { - $this->_contents .= " h\n f\n"; - } else { - // Even-Odd fill method. - $this->_contents .= " h\n f*\n"; - } - break; - case self::SHAPE_DRAW_STROKE: - $this->_contents .= " S\n"; - break; - } - + $polygon = new Polygon($x, $y, $fillType, $fillMethod); + $this->draw(null, null, $polygon); return $this; } diff --git a/tests/ZendPdf/Drawing/PolygonTest.php b/tests/ZendPdf/Drawing/PolygonTest.php new file mode 100644 index 0000000..3c787ef --- /dev/null +++ b/tests/ZendPdf/Drawing/PolygonTest.php @@ -0,0 +1,55 @@ +newPage(Page::SIZE_A4); + + $x = array(); + $y = array(); + for ($count = 0; $count < 8; $count++) { + $x[] = 140 + 25*cos(3*M_PI_4*$count); + $y[] = 375 + 25*sin(3*M_PI_4*$count); + } + $polygon = new Polygon($x, $y, Polygon::DRAW_FILL_AND_STROKE, Polygon::FILL_METHOD_EVEN_ODD); + + $expected = <<assertEquals($expected, $polygon->draw($page)); + } +} From 87a95ad21a81b538ee5680562cf49c9ff24115ae Mon Sep 17 00:00:00 2001 From: Antonio Spinelli Date: Tue, 24 Feb 2015 00:38:10 -0300 Subject: [PATCH 8/8] fix typo --- library/ZendPdf/Drawings/Image.php | 2 +- library/ZendPdf/Drawings/Line.php | 16 ++++++------- library/ZendPdf/Drawings/RoundedRectangle.php | 24 +++++++++---------- tests/ZendPdf/Drawing/EllipseTest.php | 8 ++----- tests/ZendPdf/Drawing/RectangleTest.php | 4 ++-- .../ZendPdf/Drawing/RoundedRectangleTest.php | 4 ++-- tests/ZendPdf/Drawing/SimpleTextTest.php | 8 +++---- 7 files changed, 31 insertions(+), 35 deletions(-) diff --git a/library/ZendPdf/Drawings/Image.php b/library/ZendPdf/Drawings/Image.php index ae76bac..5167705 100644 --- a/library/ZendPdf/Drawings/Image.php +++ b/library/ZendPdf/Drawings/Image.php @@ -16,7 +16,7 @@ use ZendPdf\Resource\Image\AbstractImage; /** - * Draw a rectangle at the specified position. + * Draw an image at the specified position. * * @package ZendPdf * @subpackage ZendPdf\Drawings diff --git a/library/ZendPdf/Drawings/Line.php b/library/ZendPdf/Drawings/Line.php index 8e7a386..9596eea 100644 --- a/library/ZendPdf/Drawings/Line.php +++ b/library/ZendPdf/Drawings/Line.php @@ -16,20 +16,20 @@ use ZendPdf\Resource\Image\AbstractImage; /** - * Draw a rectangle at the specified position. + * Draw a line at the specified position. * * @package ZendPdf * @subpackage ZendPdf\Drawings */ class Line extends DrawingAbstract { - protected $width; - protected $height; + protected $horizontalPoint; + protected $verticalPoint; - public function __construct($width, $height) + public function __construct($horizontalPoint, $verticalPoint) { - $this->width = (float)$width; - $this->height = (float)$height; + $this->horizontalPoint = (float)$horizontalPoint; + $this->verticalPoint = (float)$verticalPoint; } /** @@ -41,8 +41,8 @@ protected function drawElement(Page $page) $x1Obj = new NumericObject($this->xPosition); $y1Obj = new NumericObject($this->yPosition); - $x2Obj = new NumericObject($this->width); - $y2Obj = new NumericObject($this->height); + $x2Obj = new NumericObject($this->horizontalPoint); + $y2Obj = new NumericObject($this->verticalPoint); return $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n" . $x2Obj->toString() . ' ' . $y2Obj->toString() . " l\n S\n"; diff --git a/library/ZendPdf/Drawings/RoundedRectangle.php b/library/ZendPdf/Drawings/RoundedRectangle.php index 737b99b..9351aa3 100644 --- a/library/ZendPdf/Drawings/RoundedRectangle.php +++ b/library/ZendPdf/Drawings/RoundedRectangle.php @@ -103,21 +103,21 @@ protected function drawElement(Page $page) $radius = $this->radius; //draw top side - $x1Obj = new NumericObject($topLeftX + $radius[0]); + $x1Obj = new NumericObject($topLeftX + $radius[static::CORNER_TOP_LEFT]); $y1Obj = new NumericObject($topLeftY); $content = $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n"; - $x1Obj = new NumericObject($topRightX - $radius[1]); + $x1Obj = new NumericObject($topRightX - $radius[static::CORNER_TOP_RIGHT]); $y1Obj = new NumericObject($topRightY); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; //draw top right corner if needed - if ($radius[1] != 0) { + if ($radius[static::CORNER_TOP_RIGHT] != 0) { $x1Obj = new NumericObject($topRightX); $y1Obj = new NumericObject($topRightY); $x2Obj = new NumericObject($topRightX); $y2Obj = new NumericObject($topRightY); $x3Obj = new NumericObject($topRightX); - $y3Obj = new NumericObject($topRightY - $radius[1]); + $y3Obj = new NumericObject($topRightY - $radius[static::CORNER_TOP_RIGHT]); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' . $x3Obj->toString() . ' ' . $y3Obj->toString() . ' ' @@ -126,16 +126,16 @@ protected function drawElement(Page $page) //draw right side $x1Obj = new NumericObject($bottomRightX); - $y1Obj = new NumericObject($bottomRightY + $radius[2]); + $y1Obj = new NumericObject($bottomRightY + $radius[static::CORNER_BOTTOM_RIGHT]); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; //draw bottom right corner if needed - if ($radius[2] != 0) { + if ($radius[static::CORNER_BOTTOM_RIGHT] != 0) { $x1Obj = new NumericObject($bottomRightX); $y1Obj = new NumericObject($bottomRightY); $x2Obj = new NumericObject($bottomRightX); $y2Obj = new NumericObject($bottomRightY); - $x3Obj = new NumericObject($bottomRightX - $radius[2]); + $x3Obj = new NumericObject($bottomRightX - $radius[static::CORNER_BOTTOM_RIGHT]); $y3Obj = new NumericObject($bottomRightY); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' @@ -144,12 +144,12 @@ protected function drawElement(Page $page) } //draw bottom side - $x1Obj = new NumericObject($bottomLeftX + $radius[3]); + $x1Obj = new NumericObject($bottomLeftX + $radius[static::CORNER_BOTTOM_LEFT]); $y1Obj = new NumericObject($bottomLeftY); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; //draw bottom left corner if needed - if ($radius[3] != 0) { + if ($radius[static::CORNER_BOTTOM_LEFT] != 0) { $x1Obj = new NumericObject($bottomLeftX); $y1Obj = new NumericObject($bottomLeftY); $x2Obj = new NumericObject($bottomLeftX); @@ -164,16 +164,16 @@ protected function drawElement(Page $page) //draw left side $x1Obj = new NumericObject($topLeftX); - $y1Obj = new NumericObject($topLeftY - $radius[0]); + $y1Obj = new NumericObject($topLeftY - $radius[static::CORNER_TOP_LEFT]); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " l\n"; //draw top left corner if needed - if ($radius[0] != 0) { + if ($radius[static::CORNER_TOP_LEFT] != 0) { $x1Obj = new NumericObject($topLeftX); $y1Obj = new NumericObject($topLeftY); $x2Obj = new NumericObject($topLeftX); $y2Obj = new NumericObject($topLeftY); - $x3Obj = new NumericObject($topLeftX + $radius[0]); + $x3Obj = new NumericObject($topLeftX + $radius[static::CORNER_TOP_LEFT]); $y3Obj = new NumericObject($topLeftY); $content .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' ' . $x2Obj->toString() . ' ' . $y2Obj->toString() . ' ' diff --git a/tests/ZendPdf/Drawing/EllipseTest.php b/tests/ZendPdf/Drawing/EllipseTest.php index c4de94f..a0ddb86 100644 --- a/tests/ZendPdf/Drawing/EllipseTest.php +++ b/tests/ZendPdf/Drawing/EllipseTest.php @@ -13,11 +13,7 @@ /** * PHPUnit Test Case */ -use ZendPdf\Color\Cmyk; -use ZendPdf\Color\Html; -use ZendPdf\Color\Rgb; use ZendPdf\Drawings\Ellipse; -use ZendPdf\Font; use ZendPdf\Page; use ZendPdf\PdfDocument; @@ -47,7 +43,7 @@ public function testDrawAnEllipse() $actualContent .= $ellipse3->draw($page); - $expected = <<assertEquals($expected, $actualContent); } } diff --git a/tests/ZendPdf/Drawing/RectangleTest.php b/tests/ZendPdf/Drawing/RectangleTest.php index b635e99..6c27668 100644 --- a/tests/ZendPdf/Drawing/RectangleTest.php +++ b/tests/ZendPdf/Drawing/RectangleTest.php @@ -43,11 +43,11 @@ public function testDrawARectangle() $text = new Rectangle(100, 50); $text->setPosition(10, 10); - $expected = <<assertEquals($expected, $text->draw($page)); } } diff --git a/tests/ZendPdf/Drawing/RoundedRectangleTest.php b/tests/ZendPdf/Drawing/RoundedRectangleTest.php index 654cc9f..b8cab30 100644 --- a/tests/ZendPdf/Drawing/RoundedRectangleTest.php +++ b/tests/ZendPdf/Drawing/RoundedRectangleTest.php @@ -43,7 +43,7 @@ public function testDrawARectangle() $text = new RoundedRectangle(100, 50, 10); $text->setPosition(10, 10); - $expected = <<assertEquals($expected, $text->draw($page)); } } diff --git a/tests/ZendPdf/Drawing/SimpleTextTest.php b/tests/ZendPdf/Drawing/SimpleTextTest.php index 177f51f..b62157c 100644 --- a/tests/ZendPdf/Drawing/SimpleTextTest.php +++ b/tests/ZendPdf/Drawing/SimpleTextTest.php @@ -46,13 +46,13 @@ public function testDrawASimpleText() $text = new SimpleText('testing my text'); $text->setPosition(10, 10); - $expected = <<assertEquals($expected, $text->draw($page)); } @@ -67,14 +67,14 @@ public function testDrawASimpleTextWithColor() $text->setPosition(10, 10); $text->setStyle($style); - $expected = <<assertEquals($expected, $text->draw($page)); } }