File indexing completed on 2024-12-22 05:36:29

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Barcode
0017  * @subpackage Renderer
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** @see Zend_Barcode_Renderer_RendererAbstract */
0024 // require_once 'Zend/Barcode/Renderer/RendererAbstract.php';
0025 
0026 /** @see Zend_Pdf */
0027 // require_once 'Zend/Pdf.php';
0028 
0029 /** @see Zend_Pdf_Page */
0030 // require_once 'Zend/Pdf/Page.php';
0031 
0032 /** @see Zend_Pdf_Color_Rgb */
0033 // require_once 'Zend/Pdf/Color/Rgb.php';
0034 
0035 /**
0036  * Class for rendering the barcode in PDF resource
0037  *
0038  * @category   Zend
0039  * @package    Zend_Barcode
0040  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0041  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0042  */
0043 class Zend_Barcode_Renderer_Pdf extends Zend_Barcode_Renderer_RendererAbstract
0044 {
0045     /**
0046      * PDF resource
0047      * @var Zend_Pdf
0048      */
0049     protected $_resource = null;
0050 
0051     /**
0052      * Page number in PDF resource
0053      * @var integer
0054      */
0055     protected $_page = 0;
0056 
0057     /**
0058      * Module size rendering
0059      * @var float
0060      */
0061     protected $_moduleSize = 0.5;
0062 
0063     /**
0064      * Set an image resource to draw the barcode inside
0065      *
0066      * @param Zend_Pdf $pdf
0067      * @param int      $page
0068      * @return Zend_Barcode_Renderer
0069      * @throws Zend_Barcode_Renderer_Exception
0070      */
0071     public function setResource($pdf, $page = 0)
0072     {
0073         if (!$pdf instanceof Zend_Pdf) {
0074             // require_once 'Zend/Barcode/Renderer/Exception.php';
0075             throw new Zend_Barcode_Renderer_Exception(
0076                 'Invalid Zend_Pdf resource provided to setResource()'
0077             );
0078         }
0079 
0080         $this->_resource = $pdf;
0081         $this->_page     = intval($page);
0082 
0083         if (!count($this->_resource->pages)) {
0084             $this->_page = 0;
0085             $this->_resource->pages[] = new Zend_Pdf_Page(
0086                 Zend_Pdf_Page::SIZE_A4
0087             );
0088         }
0089         return $this;
0090     }
0091 
0092     /**
0093      * Check renderer parameters
0094      *
0095      * @return void
0096      */
0097     protected function _checkParams()
0098     {
0099     }
0100 
0101     /**
0102      * Draw the barcode in the PDF, send headers and the PDF
0103      * @return mixed
0104      */
0105     public function render()
0106     {
0107         $this->draw();
0108         header("Content-Type: application/pdf");
0109         echo $this->_resource->render();
0110     }
0111 
0112     /**
0113      * Initialize the PDF resource
0114      * @return void
0115      */
0116     protected function _initRenderer()
0117     {
0118         if ($this->_resource === null) {
0119             $this->_resource = new Zend_Pdf();
0120             $this->_resource->pages[] = new Zend_Pdf_Page(
0121                 Zend_Pdf_Page::SIZE_A4
0122             );
0123         }
0124 
0125         $pdfPage = $this->_resource->pages[$this->_page];
0126         $this->_adjustPosition($pdfPage->getHeight(), $pdfPage->getWidth());
0127     }
0128 
0129     /**
0130      * Draw a polygon in the rendering resource
0131      * @param array $points
0132      * @param integer $color
0133      * @param boolean $filled
0134      */
0135     protected function _drawPolygon($points, $color, $filled = true)
0136     {
0137         $page = $this->_resource->pages[$this->_page];
0138         foreach ($points as $point) {
0139             $x[] = $point[0] * $this->_moduleSize + $this->_leftOffset;
0140             $y[] = $page->getHeight() - $point[1] * $this->_moduleSize - $this->_topOffset;
0141         }
0142         if (count($y) == 4) {
0143             if ($x[0] != $x[3] && $y[0] == $y[3]) {
0144                 $y[0] -= ($this->_moduleSize / 2);
0145                 $y[3] -= ($this->_moduleSize / 2);
0146             }
0147             if ($x[1] != $x[2] && $y[1] == $y[2]) {
0148                 $y[1] += ($this->_moduleSize / 2);
0149                 $y[2] += ($this->_moduleSize / 2);
0150             }
0151         }
0152 
0153         $color = new Zend_Pdf_Color_Rgb(
0154             (($color & 0xFF0000) >> 16) / 255.0,
0155             (($color & 0x00FF00) >> 8) / 255.0,
0156             ($color & 0x0000FF) / 255.0
0157         );
0158 
0159         $page->setLineColor($color);
0160         $page->setFillColor($color);
0161         $page->setLineWidth($this->_moduleSize);
0162 
0163         $fillType = ($filled)
0164                   ? Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE
0165                   : Zend_Pdf_Page::SHAPE_DRAW_STROKE;
0166 
0167         $page->drawPolygon($x, $y, $fillType);
0168     }
0169 
0170     /**
0171      * Draw a text in the rendering resource
0172      *
0173      * @param string    $text
0174      * @param float     $size
0175      * @param array     $position
0176      * @param string    $font
0177      * @param integer   $color
0178      * @param string    $alignment
0179      * @param float|int $orientation
0180      */
0181     protected function _drawText(
0182         $text,
0183         $size,
0184         $position,
0185         $font,
0186         $color,
0187         $alignment = 'center',
0188         $orientation = 0
0189     ) {
0190         $page  = $this->_resource->pages[$this->_page];
0191         $color = new Zend_Pdf_Color_Rgb(
0192             (($color & 0xFF0000) >> 16) / 255.0,
0193             (($color & 0x00FF00) >> 8) / 255.0,
0194             ($color & 0x0000FF) / 255.0
0195         );
0196 
0197         $page->setLineColor($color);
0198         $page->setFillColor($color);
0199         $page->setFont(Zend_Pdf_Font::fontWithPath($font), $size * $this->_moduleSize * 1.2);
0200 
0201         $width = $this->widthForStringUsingFontSize(
0202             $text,
0203             Zend_Pdf_Font::fontWithPath($font),
0204             $size * $this->_moduleSize
0205         );
0206 
0207         $angle = pi() * $orientation / 180;
0208         $left = $position[0] * $this->_moduleSize + $this->_leftOffset;
0209         $top  = $page->getHeight() - $position[1] * $this->_moduleSize - $this->_topOffset;
0210 
0211         switch ($alignment) {
0212             case 'center':
0213                 $left -= ($width / 2) * cos($angle);
0214                 $top  -= ($width / 2) * sin($angle);
0215                 break;
0216             case 'right':
0217                 $left -= $width;
0218                 break;
0219         }
0220         $page->rotate($left, $top, $angle);
0221         $page->drawText($text, $left, $top);
0222         $page->rotate($left, $top, - $angle);
0223     }
0224 
0225     /**
0226      * Calculate the width of a string:
0227      * in case of using alignment parameter in drawText
0228      * @param string $text
0229      * @param Zend_Pdf_Font $font
0230      * @param float $fontSize
0231      * @return float
0232      */
0233     public function widthForStringUsingFontSize($text, $font, $fontSize)
0234     {
0235         $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $text);
0236         $characters    = array();
0237         for ($i = 0; $i < strlen($drawingString); $i ++) {
0238             $characters[] = (ord($drawingString[$i ++]) << 8) | ord($drawingString[$i]);
0239         }
0240         $glyphs = $font->glyphNumbersForCharacters($characters);
0241         $widths = $font->widthsForGlyphs($glyphs);
0242         $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
0243         return $stringWidth;
0244     }
0245 }